xref: /aosp_15_r20/external/mesa3d/src/gallium/drivers/d3d12/d3d12_video_array_of_textures_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_ARRAY_OF_TEXTURES_DPB_MANAGER_H
26 #define D3D12_VIDEO_ARRAY_OF_TEXTURES_DPB_MANAGER_H
27 
28 #include "d3d12_video_dpb_storage_manager.h"
29 #include "d3d12_video_types.h"
30 
31 class d3d12_array_of_textures_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_array_of_textures_dpb_manager
73  public:
74    d3d12_array_of_textures_dpb_manager(
75       uint32_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       bool                                        setNullSubresourcesOnAllZero = false,
83       uint32_t                                    nodeMask                     = 0,
84       bool                                        allocatePool                 = true);
~d3d12_array_of_textures_dpb_manager()85    ~d3d12_array_of_textures_dpb_manager()
86    { }
87 
88    // d3d12_array_of_textures_dpb_manager
89  private:
90    void create_reconstructed_picture_allocations(ID3D12Resource **ppResource);
91 
92    size_t                                      m_dpbInitialSize = 0;
93    ID3D12Device *                              m_pDevice;
94    DXGI_FORMAT                                 m_encodeFormat;
95    D3D12_VIDEO_ENCODER_PICTURE_RESOLUTION_DESC m_encodeResolution;
96 
97    // DPB with array of resources backing storage
98 
99    struct d3d12_video_dpb
100    {
101       std::vector<ID3D12Resource *> pResources;
102       std::vector<uint32_t>         pSubresources;
103       std::vector<IUnknown *>       pHeaps;
104    };
105 
106    d3d12_video_dpb m_D3D12DPB;
107 
108    // Flags used when creating the resource pool
109    // Usually if reference only is needed for d3d12 video use
110    // D3D12_RESOURCE_FLAG_VIDEO_DECODE_REFERENCE_ONLY | D3D12_RESOURCE_FLAG_DENY_SHADER_RESOURCE
111    // D3D12_RESOURCE_FLAG_VIDEO_ENCODE_REFERENCE_ONLY | D3D12_RESOURCE_FLAG_DENY_SHADER_RESOURCE
112    D3D12_RESOURCE_FLAGS m_resourceAllocFlags;
113 
114    // Pool of resources to be aliased by the DPB without giving memory ownership
115    // This resources are allocated and released by this implementation
116    struct d3d12_reusable_resource
117    {
118       ComPtr<ID3D12Resource> pResource;
119       // subresource is always 0 on this AoT implementation of the resources pool
120       bool isFree;
121    };
122 
123    std::vector<d3d12_reusable_resource> m_ResourcesPool;
124 
125    // If all subresources are 0, the DPB is loaded with an array of individual textures, the D3D Encode API expects
126    // pSubresources to be null in this case The D3D Decode API expects it to be non-null even with all zeroes.
127    bool m_NullSubresourcesOnAllZero = false;
128 
129    uint32_t m_nodeMask = 0;
130 };
131 
132 #endif
133