xref: /aosp_15_r20/external/mesa3d/src/gallium/drivers/d3d12/d3d12_video_texture_array_dpb_manager.h (revision 6104692788411f58d303aa86923a9ff6ecaded22)
1 /*
2  * Copyright © Microsoft Corporation
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21  * IN THE SOFTWARE.
22  */
23 
24 
25 #ifndef D3D12_VIDEO_TEXTURE_ARRAY_DPB_MANAGER_H
26 #define D3D12_VIDEO_TEXTURE_ARRAY_DPB_MANAGER_H
27 
28 #include "d3d12_video_dpb_storage_manager.h"
29 #include "d3d12_video_types.h"
30 
31 class d3d12_texture_array_dpb_manager : public d3d12_video_dpb_storage_manager_interface
32 {
33    // d3d12_video_dpb_storage_manager_interface
34  public:
35    // Adds a new reference frame at a given position
36    void insert_reference_frame(d3d12_video_reconstructed_picture pReconPicture, uint32_t dpbPosition);
37 
38    // Assigns a reference frame at a given position
39    void assign_reference_frame(d3d12_video_reconstructed_picture pReconPicture, uint32_t dpbPosition);
40 
41    // Gets a reference frame at a given position
42    d3d12_video_reconstructed_picture get_reference_frame(uint32_t dpbPosition);
43 
44    // Removes a new reference frame at a given position and returns operation success
45    // pResourceUntracked is an optional output indicating if the removed resource was being tracked by the pool
46    bool remove_reference_frame(uint32_t dpbPosition, bool *pResourceUntracked = nullptr);
47 
48    // Returns the resource allocation for a NEW picture
49    d3d12_video_reconstructed_picture get_new_tracked_picture_allocation();
50 
51    // Returns true if the trackedItem was allocated (and is being tracked) by this class
52    bool is_tracked_allocation(d3d12_video_reconstructed_picture trackedItem);
53 
54    // Returns whether it found the tracked resource on this instance pool tracking and was able to free it
55    bool untrack_reconstructed_picture_allocation(d3d12_video_reconstructed_picture trackedItem);
56 
57    // Returns the number of pictures currently stored in the DPB
58    uint32_t get_number_of_pics_in_dpb();
59 
60    // Returns all the current reference frames stored
61    d3d12_video_reference_frames get_current_reference_frames();
62 
63    // Removes all pictures from DPB
64    // returns the number of resources marked as reusable
65    uint32_t clear_decode_picture_buffer();
66 
67    // number of resources in the pool that are marked as in use
68    uint32_t get_number_of_in_use_allocations();
69 
70    uint32_t get_number_of_tracked_allocations();
71 
72    // d3d12_texture_array_dpb_manager
73  public:
74    d3d12_texture_array_dpb_manager(
75       uint16_t dpbInitialSize,   // Maximum in use resources for a DPB of size x should be x+1 for cases when a P frame
76                                  // is using the x references in the L0 list and also using an extra resource to output
77                                  // it's own recon pic.
78       ID3D12Device *                              pDevice,
79       DXGI_FORMAT                                 encodeSessionFormat,
80       D3D12_VIDEO_ENCODER_PICTURE_RESOLUTION_DESC encodeSessionResolution,
81       D3D12_RESOURCE_FLAGS                        resourceAllocFlags = D3D12_RESOURCE_FLAG_NONE,
82       uint32_t                                    nodeMask           = 0);
83    ~d3d12_texture_array_dpb_manager();
84 
85    // d3d12_texture_array_dpb_manager
86  private:
87    void create_reconstructed_picture_allocations(ID3D12Resource **ppResource, uint16_t texArraySize);
88 
89    ID3D12Device *                              m_pDevice;
90    DXGI_FORMAT                                 m_encodeFormat;
91    D3D12_VIDEO_ENCODER_PICTURE_RESOLUTION_DESC m_encodeResolution;
92    uint16_t                                    m_dpbTextureArraySize = 0;
93 
94    // DPB with array of resources backing storage
95 
96    struct d3d12_video_dpb
97    {
98       std::vector<ID3D12Resource *> pResources;
99       std::vector<uint32_t>         pSubresources;
100       std::vector<IUnknown *>       pHeaps;
101    };
102 
103    d3d12_video_dpb m_D3D12DPB;
104 
105    // Flags used when creating the resource pool
106    // Usually if reference only is needed for d3d12 video use
107    // D3D12_RESOURCE_FLAG_VIDEO_DECODE_REFERENCE_ONLY | D3D12_RESOURCE_FLAG_DENY_SHADER_RESOURCE
108    // D3D12_RESOURCE_FLAG_VIDEO_ENCODE_REFERENCE_ONLY | D3D12_RESOURCE_FLAG_DENY_SHADER_RESOURCE
109    D3D12_RESOURCE_FLAGS m_resourceAllocFlags;
110 
111    // Pool of resources to be aliased by the DPB without giving memory ownership
112    // This resources are allocated and released by this implementation
113    struct d3d12_reusable_resource
114    {
115       ComPtr<ID3D12Resource> pResource;
116       uint32_t               subresource;
117       bool                   isFree;
118    };
119 
120    ComPtr<ID3D12Resource>               m_baseTexArrayResource;
121    std::vector<d3d12_reusable_resource> m_ResourcesPool;
122 
123    uint32_t m_nodeMask = 0u;
124 };
125 
126 #endif
127