xref: /aosp_15_r20/external/deqp/external/vulkancts/modules/vulkan/video/vktBitstreamBufferImpl.hpp (revision 35238bce31c2a825756842865a792f8cf7f89930)
1 #ifndef _VKTBITSTREAMBUFFERIMPL_HPP
2 #define _VKTBITSTREAMBUFFERIMPL_HPP
3 /*------------------------------------------------------------------------
4  * Vulkan Conformance Tests
5  * ------------------------
6  *
7  * Copyright (c) 2023 The Khronos Group Inc.
8  *
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  *
21  *//*!
22  * \file
23  * \brief Bitstream buffer implementation for the CTS.
24  *//*--------------------------------------------------------------------*/
25 /*
26  * Copyright 2023 NVIDIA Corporation.
27  *
28  * Licensed under the Apache License, Version 2.0 (the "License");
29  * you may not use this file except in compliance with the License.
30  * You may obtain a copy of the License at
31  *
32  *    http://www.apache.org/licenses/LICENSE-2.0
33  *
34  * Unless required by applicable law or agreed to in writing, software
35  * distributed under the License is distributed on an "AS IS" BASIS,
36  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
37  * See the License for the specific language governing permissions and
38  * limitations under the License.
39  */
40 
41 #include <atomic>
42 #include <iostream>
43 #include <vector>
44 
45 #include "vkvideo_parser/VulkanBitstreamBuffer.h"
46 
47 #include "vktVideoTestUtils.hpp"
48 
49 namespace vkt
50 {
51 namespace video
52 {
53 
54 using BufferPtr = de::MovePtr<BufferWithMemory>;
55 
56 // This class is required by the NVIDIA sample decoder interface.
57 // The following is a CTS implementation of the VulkanBitstreamBuffer interface upstream.
58 class BitstreamBufferImpl : public VulkanBitstreamBuffer
59 {
60 public:
61     static VkResult Create(DeviceContext *devctx, uint32_t queueFamilyIndex, VkDeviceSize bufferSize,
62                            VkDeviceSize bufferOffsetAlignment, VkDeviceSize bufferSizeAlignment,
63                            VkSharedBaseObj<BitstreamBufferImpl> &vulkanBitstreamBuffer,
64                            const VkVideoProfileListInfoKHR *profileList, bool resourcesWithoutProfiles = false);
65 
AddRef()66     int32_t AddRef() override
67     {
68         return ++m_refCount;
69     }
70 
Release()71     int32_t Release() override
72     {
73         DE_ASSERT(m_refCount > 0);
74         uint32_t ret = --m_refCount;
75         if (ret == 0)
76         {
77             delete this;
78         }
79         return ret;
80     }
81 
GetRefCount()82     int32_t GetRefCount() override
83     {
84         DE_ASSERT(m_refCount > 0);
85         return m_refCount;
86     }
87 
88     VkDeviceSize GetMaxSize() const override;
89     VkDeviceSize GetOffsetAlignment() const override;
90     VkDeviceSize GetSizeAlignment() const override;
91     VkDeviceSize Resize(VkDeviceSize newSize, VkDeviceSize copySize = 0, VkDeviceSize copyOffset = 0) override;
92     VkDeviceSize Clone(VkDeviceSize newSize, VkDeviceSize copySize, VkDeviceSize copyOffset,
93                        VkSharedBaseObj<VulkanBitstreamBuffer> &vulkanBitstreamBuffer) override;
94 
95     int64_t MemsetData(uint32_t value, VkDeviceSize offset, VkDeviceSize size) override;
96     int64_t CopyDataToBuffer(uint8_t *dstBuffer, VkDeviceSize dstOffset, VkDeviceSize srcOffset,
97                              VkDeviceSize size) const override;
98     int64_t CopyDataToBuffer(VkSharedBaseObj<VulkanBitstreamBuffer> &dstBuffer, VkDeviceSize dstOffset,
99                              VkDeviceSize srcOffset, VkDeviceSize size) const override;
100     int64_t CopyDataFromBuffer(const uint8_t *sourceBuffer, VkDeviceSize srcOffset, VkDeviceSize dstOffset,
101                                VkDeviceSize size) override;
102     int64_t CopyDataFromBuffer(const VkSharedBaseObj<VulkanBitstreamBuffer> &sourceBuffer, VkDeviceSize srcOffset,
103                                VkDeviceSize dstOffset, VkDeviceSize size) override;
104     uint8_t *GetDataPtr(VkDeviceSize offset, VkDeviceSize &maxSize) override;
105     const uint8_t *GetReadOnlyDataPtr(VkDeviceSize offset, VkDeviceSize &maxSize) const override;
106 
107     void FlushRange(VkDeviceSize offset, VkDeviceSize size) const override;
108     void InvalidateRange(VkDeviceSize offset, VkDeviceSize size) const override;
109 
GetBuffer() const110     VkBuffer GetBuffer() const override
111     {
112         return m_bitstreamBuffer->get();
113     }
GetDeviceMemory() const114     VkDeviceMemory GetDeviceMemory() const override
115     {
116         return m_bitstreamBuffer->getAllocation().getMemory();
117     }
118 
119     uint32_t AddStreamMarker(uint32_t streamOffset) override;
120     uint32_t SetStreamMarker(uint32_t streamOffset, uint32_t index) override;
121     uint32_t GetStreamMarker(uint32_t index) const override;
122     uint32_t GetStreamMarkersCount() const override;
123     const uint32_t *GetStreamMarkersPtr(uint32_t startIndex, uint32_t &maxCount) const override;
124     uint32_t ResetStreamMarkers() override;
125 
operator VkDeviceMemory()126     operator VkDeviceMemory()
127     {
128         return GetDeviceMemory();
129     }
operator bool()130     operator bool()
131     {
132         return !!m_bitstreamBuffer;
133     }
134 
135     VkResult CopyDataToBuffer(const uint8_t *pData, VkDeviceSize size, VkDeviceSize &dstBufferOffset) const;
136 
137 private:
138     VkResult CreateBuffer(DeviceContext *ctx, uint32_t queueFamilyIndex, VkDeviceSize &bufferSize,
139                           VkDeviceSize bufferSizeAlignment, const VkVideoProfileListInfoKHR *profileList);
140 
141     uint8_t *CheckAccess(VkDeviceSize offset, VkDeviceSize size) const;
142 
143     VkResult Initialize(VkDeviceSize bufferSize);
144 
BitstreamBufferImpl(DeviceContext * devctx,uint32_t queueFamilyIndex,VkDeviceSize bufferOffsetAlignment,VkDeviceSize bufferSizeAlignment,const VkVideoProfileListInfoKHR * profileList,bool resourcesWithoutProfiles)145     BitstreamBufferImpl(DeviceContext *devctx, uint32_t queueFamilyIndex, VkDeviceSize bufferOffsetAlignment,
146                         VkDeviceSize bufferSizeAlignment, const VkVideoProfileListInfoKHR *profileList,
147                         bool resourcesWithoutProfiles)
148         : VulkanBitstreamBuffer()
149         , m_refCount(0)
150         , m_devctx(devctx)
151         , m_profileList(profileList)
152         , m_resourcesWithoutProfiles(resourcesWithoutProfiles)
153         , m_queueFamilyIndex(queueFamilyIndex)
154         , m_bufferOffsetAlignment(bufferOffsetAlignment)
155         , m_bufferSizeAlignment(bufferSizeAlignment)
156         , m_bufferSize(0)
157         , m_streamMarkers(256)
158     {
159     }
160 
161 private:
162     std::atomic<int32_t> m_refCount;
163     DeviceContext *m_devctx;
164     const VkVideoProfileListInfoKHR *m_profileList;
165     bool m_resourcesWithoutProfiles;
166     uint32_t m_queueFamilyIndex;
167     VkDeviceSize m_bufferOffsetAlignment;
168     VkDeviceSize m_bufferSizeAlignment;
169     BufferPtr m_bitstreamBuffer;
170     VkDeviceSize m_bufferSize;
171     std::vector<uint32_t> m_streamMarkers;
172 };
173 
174 } // namespace video
175 } // namespace vkt
176 
177 #endif // _VKTBITSTREAMBUFFERIMPL_HPP
178