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 "vktVideoTestUtils.hpp"
46 
47 namespace vkt
48 {
49 namespace video
50 {
51 
52 using BufferPtr = de::MovePtr<BufferWithMemory>;
53 
54 // This class is required by the NVIDIA sample decoder interface.
55 // The following is a CTS implementation of the VulkanBitstreamBuffer interface upstream.
56 class BitstreamBufferImpl : public VulkanBitstreamBuffer
57 {
58 public:
59     static VkResult Create(DeviceContext* devctx, deUint32 queueFamilyIndex,
60              VkDeviceSize bufferSize, VkDeviceSize bufferOffsetAlignment, VkDeviceSize bufferSizeAlignment,
61              VkSharedBaseObj<BitstreamBufferImpl>& vulkanBitstreamBuffer,
62 						   const VkVideoProfileListInfoKHR* profileList);
63 
64     int32_t AddRef() override
65     {
66         return ++m_refCount;
67     }
68 
69     int32_t Release() override
70     {
71         DE_ASSERT(m_refCount > 0);
72         deUint32 ret = --m_refCount;
73         if (ret == 0) {
74             delete this;
75         }
76         return ret;
77     }
78 
79     int32_t GetRefCount() override
80     {
81         DE_ASSERT(m_refCount > 0);
82         return m_refCount;
83     }
84 
85     VkDeviceSize GetMaxSize() const override;
86     VkDeviceSize GetOffsetAlignment() const override;
87     VkDeviceSize GetSizeAlignment() const override;
88     VkDeviceSize Resize(VkDeviceSize newSize, VkDeviceSize copySize = 0, VkDeviceSize copyOffset = 0) override;
89 	VkDeviceSize Clone(VkDeviceSize newSize, VkDeviceSize copySize, VkDeviceSize copyOffset, VkSharedBaseObj<VulkanBitstreamBuffer>& vulkanBitstreamBuffer) override;
90 
91     int64_t  MemsetData(deUint32 value, VkDeviceSize offset, VkDeviceSize size) override;
92     int64_t  CopyDataToBuffer(deUint8 *dstBuffer, VkDeviceSize dstOffset,
93                                       VkDeviceSize srcOffset, VkDeviceSize size) const override;
94     int64_t  CopyDataToBuffer(VkSharedBaseObj<VulkanBitstreamBuffer>& dstBuffer, VkDeviceSize dstOffset,
95                                       VkDeviceSize srcOffset, VkDeviceSize size) const override;
96     int64_t  CopyDataFromBuffer(const deUint8 *sourceBuffer, VkDeviceSize srcOffset,
97                                         VkDeviceSize dstOffset, VkDeviceSize size) override;
98     int64_t  CopyDataFromBuffer(const VkSharedBaseObj<VulkanBitstreamBuffer>& sourceBuffer, VkDeviceSize srcOffset,
99                                         VkDeviceSize dstOffset, VkDeviceSize size) override;
100     deUint8* GetDataPtr(VkDeviceSize offset, VkDeviceSize &maxSize) override;
101     const deUint8* GetReadOnlyDataPtr(VkDeviceSize offset, VkDeviceSize &maxSize) const override;
102 
103     void FlushRange(VkDeviceSize offset, VkDeviceSize size) const override;
104     void InvalidateRange(VkDeviceSize offset, VkDeviceSize size) const override;
105 
106     VkBuffer GetBuffer() const override { return m_bitstreamBuffer->get(); }
107     VkDeviceMemory GetDeviceMemory() const override { return m_bitstreamBuffer->getAllocation().getMemory(); }
108 
109     deUint32  AddStreamMarker(deUint32 streamOffset) override;
110     deUint32  SetStreamMarker(deUint32 streamOffset, deUint32 index) override;
111     deUint32  GetStreamMarker(deUint32 index) const override;
112     deUint32  GetStreamMarkersCount() const override;
113     const deUint32* GetStreamMarkersPtr(deUint32 startIndex, deUint32& maxCount) const override;
114     deUint32  ResetStreamMarkers() override;
115 
operator VkDeviceMemory()116     operator VkDeviceMemory() { return GetDeviceMemory(); }
operator bool()117     operator bool() { return !!m_bitstreamBuffer; }
118 
119     VkResult CopyDataToBuffer(const deUint8* pData, VkDeviceSize size,
120                               VkDeviceSize &dstBufferOffset) const;
121 
122 private:
123 
124     VkResult CreateBuffer(DeviceContext* ctx, deUint32 queueFamilyIndex,
125                                  VkDeviceSize& bufferSize,
126                                  VkDeviceSize bufferSizeAlignment,
127 								 const VkVideoProfileListInfoKHR* profileList);
128 
129     deUint8* CheckAccess(VkDeviceSize offset, VkDeviceSize size) const;
130 
131     VkResult Initialize(VkDeviceSize bufferSize);
132 
BitstreamBufferImpl(DeviceContext* devctx, deUint32 queueFamilyIndex, VkDeviceSize bufferOffsetAlignment, VkDeviceSize bufferSizeAlignment, const VkVideoProfileListInfoKHR* profileList)133 	BitstreamBufferImpl(DeviceContext* devctx,
134                               deUint32 queueFamilyIndex,
135                               VkDeviceSize bufferOffsetAlignment,
136                               VkDeviceSize bufferSizeAlignment,
137 							  const VkVideoProfileListInfoKHR* profileList)
138         : VulkanBitstreamBuffer()
139         , m_refCount(0)
140 		, m_devctx(devctx)
141 		, m_profileList(profileList)
142         , m_queueFamilyIndex(queueFamilyIndex)
143         , m_bufferOffsetAlignment(bufferOffsetAlignment)
144         , m_bufferSizeAlignment(bufferSizeAlignment)
145 		, m_bufferSize(0)
146         , m_streamMarkers(256) { }
147 
148 
149 private:
150 	std::atomic<int32_t>			 m_refCount;
151 	DeviceContext*					 m_devctx;
152 	const VkVideoProfileListInfoKHR* m_profileList;
153 	deUint32						 m_queueFamilyIndex;
154 	VkDeviceSize					 m_bufferOffsetAlignment;
155 	VkDeviceSize					 m_bufferSizeAlignment;
156 	BufferPtr						 m_bitstreamBuffer;
157 	VkDeviceSize					 m_bufferSize;
158 	std::vector<deUint32>			 m_streamMarkers;
159 };
160 
161 } // video
162 } // vkt
163 
164 #endif // _VKTBITSTREAMBUFFERIMPL_HPP
165