1 #include <android/hardware_buffer.h>
2 #include <dvr/dvr_buffer.h>
3 #include <dvr/dvr_config.h>
4 #include <dvr/dvr_shared_buffers.h>
5 #include <dvr/dvr_surface.h>
6 #include <system/graphics.h>
7 
8 #include <gtest/gtest.h>
9 
10 namespace android {
11 namespace dvr {
12 
13 namespace {
14 
TEST(DvrGlobalBufferTest,TestGlobalBuffersSameName)15 TEST(DvrGlobalBufferTest, TestGlobalBuffersSameName) {
16   const DvrGlobalBufferKey buffer_key = 101;
17   DvrBuffer* buffer1 = nullptr;
18   int ret1 = dvrSetupGlobalBuffer(buffer_key, 10, 0, &buffer1);
19   ASSERT_EQ(0, ret1);
20   ASSERT_NE(nullptr, buffer1);
21 
22   DvrBuffer* buffer2 = nullptr;
23   int ret2 = dvrSetupGlobalBuffer(buffer_key, 10, 0, &buffer2);
24   ASSERT_EQ(0, ret2);
25   ASSERT_NE(nullptr, buffer2);
26 
27   AHardwareBuffer* hardware_buffer1 = nullptr;
28   int e1 = dvrBufferGetAHardwareBuffer(buffer1, &hardware_buffer1);
29   ASSERT_EQ(0, e1);
30   ASSERT_NE(nullptr, hardware_buffer1);
31 
32   AHardwareBuffer* hardware_buffer2 = nullptr;
33   int e2 = dvrBufferGetAHardwareBuffer(buffer2, &hardware_buffer2);
34   ASSERT_EQ(0, e2);
35   ASSERT_NE(nullptr, hardware_buffer2);
36 
37   AHardwareBuffer_Desc desc1 = {};
38   AHardwareBuffer_describe(hardware_buffer1, &desc1);
39   AHardwareBuffer_Desc desc2 = {};
40   AHardwareBuffer_describe(hardware_buffer2, &desc2);
41   ASSERT_EQ(desc1.width, 10u);
42   ASSERT_EQ(desc1.height, 1u);
43   ASSERT_EQ(desc1.layers, 1u);
44   ASSERT_EQ(desc1.format, HAL_PIXEL_FORMAT_BLOB);
45   ASSERT_EQ(desc1.usage, 0u);
46   ASSERT_EQ(desc2.width, 10u);
47   ASSERT_EQ(desc2.height, 1u);
48   ASSERT_EQ(desc2.layers, 1u);
49   ASSERT_EQ(desc2.format, HAL_PIXEL_FORMAT_BLOB);
50   ASSERT_EQ(desc2.usage, 0u);
51 
52   dvrBufferDestroy(buffer1);
53   dvrBufferDestroy(buffer2);
54 
55   DvrBuffer* buffer3 = nullptr;
56   int e3 = dvrGetGlobalBuffer(buffer_key, &buffer3);
57   ASSERT_NE(nullptr, buffer3);
58   ASSERT_EQ(0, e3);
59 
60   AHardwareBuffer* hardware_buffer3 = nullptr;
61   int e4 = dvrBufferGetAHardwareBuffer(buffer3, &hardware_buffer3);
62   ASSERT_EQ(0, e4);
63   ASSERT_NE(nullptr, hardware_buffer3);
64 
65   AHardwareBuffer_Desc desc3 = {};
66   AHardwareBuffer_describe(hardware_buffer3, &desc3);
67   ASSERT_EQ(desc3.width, 10u);
68   ASSERT_EQ(desc3.height, 1u);
69   ASSERT_EQ(desc3.layers, 1u);
70   ASSERT_EQ(desc3.format, HAL_PIXEL_FORMAT_BLOB);
71   ASSERT_EQ(desc3.usage, 0u);
72 
73   dvrBufferDestroy(buffer3);
74 
75   AHardwareBuffer_release(hardware_buffer1);
76   AHardwareBuffer_release(hardware_buffer2);
77   AHardwareBuffer_release(hardware_buffer3);
78 }
79 
TEST(DvrGlobalBufferTest,TestMultipleGlobalBuffers)80 TEST(DvrGlobalBufferTest, TestMultipleGlobalBuffers) {
81   const DvrGlobalBufferKey buffer_key1 = 102;
82   const DvrGlobalBufferKey buffer_key2 = 103;
83   DvrBuffer* setup_buffer1 = nullptr;
84   int ret1 = dvrSetupGlobalBuffer(buffer_key1, 10, 0, &setup_buffer1);
85   ASSERT_EQ(0, ret1);
86   ASSERT_NE(nullptr, setup_buffer1);
87   dvrBufferDestroy(setup_buffer1);
88 
89   DvrBuffer* setup_buffer2 = nullptr;
90   int ret2 = dvrSetupGlobalBuffer(buffer_key2, 10, 0, &setup_buffer2);
91   ASSERT_EQ(0, ret2);
92   ASSERT_NE(nullptr, setup_buffer2);
93   dvrBufferDestroy(setup_buffer2);
94 
95   DvrBuffer* buffer1 = nullptr;
96   int e1 = dvrGetGlobalBuffer(buffer_key1, &buffer1);
97   ASSERT_NE(nullptr, buffer1);
98   ASSERT_EQ(0, e1);
99   dvrBufferDestroy(buffer1);
100 
101   DvrBuffer* buffer2 = nullptr;
102   int e2 = dvrGetGlobalBuffer(buffer_key2, &buffer2);
103   ASSERT_NE(nullptr, buffer2);
104   ASSERT_EQ(0, e2);
105   dvrBufferDestroy(buffer2);
106 }
107 
TEST(DvrGlobalBufferTest,TestGlobalBufferUsage)108 TEST(DvrGlobalBufferTest, TestGlobalBufferUsage) {
109   const DvrGlobalBufferKey buffer_key = 100;
110 
111   // Set usage to AHARDWAREBUFFER_USAGE_VIDEO_ENCODE. We use this because
112   // internally AHARDWAREBUFFER_USAGE_VIDEO_ENCODE is converted to
113   // GRALLOC1_CONSUMER_USAGE_VIDEO_ENCODER, and these two values are different.
114   // If all is good, when we get the AHardwareBuffer, it should be converted
115   // back to AHARDWAREBUFFER_USAGE_VIDEO_ENCODE.
116   const uint64_t usage = AHARDWAREBUFFER_USAGE_VIDEO_ENCODE;
117 
118   DvrBuffer* setup_buffer = nullptr;
119   int e1 = dvrSetupGlobalBuffer(buffer_key, 10, usage, &setup_buffer);
120   ASSERT_NE(nullptr, setup_buffer);
121   ASSERT_EQ(0, e1);
122 
123   AHardwareBuffer* hardware_buffer = nullptr;
124   int e2 = dvrBufferGetAHardwareBuffer(setup_buffer, &hardware_buffer);
125   ASSERT_EQ(0, e2);
126   ASSERT_NE(nullptr, hardware_buffer);
127 
128   AHardwareBuffer_Desc desc = {};
129   AHardwareBuffer_describe(hardware_buffer, &desc);
130   ASSERT_EQ(usage, desc.usage);
131 
132   dvrBufferDestroy(setup_buffer);
133   AHardwareBuffer_release(hardware_buffer);
134 }
135 
TEST(DvrGlobalBufferTest,TestGlobalBufferCarriesData)136 TEST(DvrGlobalBufferTest, TestGlobalBufferCarriesData) {
137   const DvrGlobalBufferKey buffer_name = 110;
138 
139   uint64_t usage = AHARDWAREBUFFER_USAGE_CPU_READ_OFTEN |
140                    AHARDWAREBUFFER_USAGE_CPU_WRITE_OFTEN;
141   constexpr size_t size = 1024 * sizeof(uint64_t);
142   constexpr uint64_t value = 0x123456787654321;
143 
144   {
145     // Allocate some data and set it to something.
146     DvrBuffer* setup_buffer = nullptr;
147     int e1 = dvrSetupGlobalBuffer(buffer_name, size, usage, &setup_buffer);
148     ASSERT_NE(nullptr, setup_buffer);
149     ASSERT_EQ(0, e1);
150 
151     AHardwareBuffer* hardware_buffer = nullptr;
152     int e2 = dvrBufferGetAHardwareBuffer(setup_buffer, &hardware_buffer);
153     ASSERT_EQ(0, e2);
154     ASSERT_NE(nullptr, hardware_buffer);
155 
156     void* buffer;
157     int e3 = AHardwareBuffer_lock(hardware_buffer, usage, -1, nullptr, &buffer);
158     ASSERT_EQ(0, e3);
159     ASSERT_NE(nullptr, buffer);
160     // Verify that the buffer pointer is at least 16 byte aligned.
161     ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(buffer) & (16 - 1));
162 
163     uint64_t* data = static_cast<uint64_t*>(buffer);
164     constexpr size_t num_values = size / sizeof(uint64_t);
165     for (size_t i = 0; i < num_values; ++i) {
166       data[i] = value;
167     }
168 
169     int32_t fence = -1;
170     int e4 = AHardwareBuffer_unlock(hardware_buffer, &fence);
171     ASSERT_EQ(0, e4);
172 
173     dvrBufferDestroy(setup_buffer);
174     AHardwareBuffer_release(hardware_buffer);
175   }
176 
177   {
178     // Get the buffer and check that all the data is still present.
179     DvrBuffer* setup_buffer = nullptr;
180     int e1 = dvrGetGlobalBuffer(buffer_name, &setup_buffer);
181     ASSERT_NE(nullptr, setup_buffer);
182     ASSERT_EQ(0, e1);
183 
184     AHardwareBuffer* hardware_buffer = nullptr;
185     int e2 = dvrBufferGetAHardwareBuffer(setup_buffer, &hardware_buffer);
186     ASSERT_EQ(0, e2);
187     ASSERT_NE(nullptr, hardware_buffer);
188 
189     void* buffer;
190     int e3 = AHardwareBuffer_lock(hardware_buffer, usage, -1, nullptr, &buffer);
191     ASSERT_EQ(0, e3);
192     ASSERT_NE(nullptr, buffer);
193     // Verify that the buffer pointer is at least 16 byte aligned.
194     ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(buffer) & (16 - 1));
195 
196     uint64_t* data = static_cast<uint64_t*>(buffer);
197     constexpr size_t num_values = size / sizeof(uint64_t);
198     bool is_equal = true;
199     for (size_t i = 0; i < num_values; ++i) {
200       is_equal &= (data[i] == value);
201     }
202     ASSERT_TRUE(is_equal);
203 
204     int32_t fence = -1;
205     int e4 = AHardwareBuffer_unlock(hardware_buffer, &fence);
206     ASSERT_EQ(0, e4);
207 
208     dvrBufferDestroy(setup_buffer);
209     AHardwareBuffer_release(hardware_buffer);
210   }
211 }
212 
TEST(DvrGlobalBufferTest,TestGlobalBufferZeroed)213 TEST(DvrGlobalBufferTest, TestGlobalBufferZeroed) {
214   const DvrGlobalBufferKey buffer_name = 120;
215 
216   // Allocate 1MB and check that it is all zeros.
217   uint64_t usage = AHARDWAREBUFFER_USAGE_CPU_READ_OFTEN |
218                    AHARDWAREBUFFER_USAGE_CPU_WRITE_OFTEN;
219   constexpr size_t size = 1024 * 1024;
220   DvrBuffer* setup_buffer = nullptr;
221   int e1 = dvrSetupGlobalBuffer(buffer_name, size, usage, &setup_buffer);
222   ASSERT_NE(nullptr, setup_buffer);
223   ASSERT_EQ(0, e1);
224 
225   AHardwareBuffer* hardware_buffer = nullptr;
226   int e2 = dvrBufferGetAHardwareBuffer(setup_buffer, &hardware_buffer);
227   ASSERT_EQ(0, e2);
228   ASSERT_NE(nullptr, hardware_buffer);
229 
230   void* buffer;
231   int e3 = AHardwareBuffer_lock(hardware_buffer, usage, -1, nullptr, &buffer);
232   ASSERT_EQ(0, e3);
233   ASSERT_NE(nullptr, buffer);
234   // Verify that the buffer pointer is at least 16 byte aligned.
235   ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(buffer) & (16 - 1));
236 
237   uint64_t* data = static_cast<uint64_t*>(buffer);
238   constexpr size_t num_values = size / sizeof(uint64_t);
239   uint64_t zero = 0;
240   for (size_t i = 0; i < num_values; ++i) {
241     zero |= data[i];
242   }
243   ASSERT_EQ(0U, zero);
244 
245   int32_t fence = -1;
246   int e4 = AHardwareBuffer_unlock(hardware_buffer, &fence);
247   ASSERT_EQ(0, e4);
248 
249   dvrBufferDestroy(setup_buffer);
250   AHardwareBuffer_release(hardware_buffer);
251 }
252 
TEST(DvrGlobalBufferTest,TestVrflingerConfigBuffer)253 TEST(DvrGlobalBufferTest, TestVrflingerConfigBuffer) {
254   const DvrGlobalBufferKey buffer_name =
255       DvrGlobalBuffers::kVrFlingerConfigBufferKey;
256 
257   // First delete any existing buffer so we can test the failure case.
258   dvrDeleteGlobalBuffer(buffer_name);
259 
260   const uint64_t usage = AHARDWAREBUFFER_USAGE_CPU_READ_OFTEN |
261                          AHARDWAREBUFFER_USAGE_CPU_WRITE_RARELY;
262 
263   size_t correct_size = DvrConfigRing::MemorySize();
264   size_t wrong_size = DvrConfigRing::MemorySize(0);
265 
266   // Setup an invalid config buffer (too small) and assert that it fails.
267   DvrBuffer* setup_buffer = nullptr;
268   int e1 = dvrSetupGlobalBuffer(buffer_name, wrong_size, usage, &setup_buffer);
269   ASSERT_EQ(nullptr, setup_buffer);
270   ASSERT_GT(0, e1);
271 
272   // Setup a correct config buffer.
273   int e2 =
274       dvrSetupGlobalBuffer(buffer_name, correct_size, usage, &setup_buffer);
275   ASSERT_NE(nullptr, setup_buffer);
276   ASSERT_EQ(0, e2);
277 
278   dvrBufferDestroy(setup_buffer);
279 }
280 
281 }  // namespace
282 
283 }  // namespace dvr
284 }  // namespace android
285