1 /*
2 * Copyright (C) 2018 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <errno.h>
18 #include <sys/types.h>
19 #include <sys/socket.h>
20 #include <sys/un.h>
21 #include <unistd.h>
22
23 #include <limits>
24 #include <sstream>
25 #include <string>
26
27 #include <android/hardware_buffer.h>
28 #include <android/log.h>
29 #include <gtest/gtest.h>
30 #include <gmock/gmock.h>
31
32 //#define LOG_NDEBUG 0
33
34 #define BAD_VALUE -EINVAL
35 #define INVALID_OPERATION -ENOSYS
36 #define NO_ERROR 0
37
38 #define LOG_TAG "AHBTest"
39 #define ALOGI(...) __android_log_print(ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__)
40
41 namespace {
42
43 using testing::AnyOf;
44 using testing::Eq;
45 using testing::Ge;
46 using testing::NotNull;
47
48 #define FORMAT_CASE(x) case AHARDWAREBUFFER_FORMAT_ ## x: os << #x ; break
49
PrintAhbFormat(std::ostream & os,uint64_t format)50 void PrintAhbFormat(std::ostream& os, uint64_t format) {
51 switch (format) {
52 FORMAT_CASE(R8G8B8A8_UNORM);
53 FORMAT_CASE(R8G8B8X8_UNORM);
54 FORMAT_CASE(R8G8B8_UNORM);
55 FORMAT_CASE(R5G6B5_UNORM);
56 FORMAT_CASE(R16G16B16A16_FLOAT);
57 FORMAT_CASE(R10G10B10A2_UNORM);
58 FORMAT_CASE(BLOB);
59 FORMAT_CASE(D16_UNORM);
60 FORMAT_CASE(D24_UNORM);
61 FORMAT_CASE(D24_UNORM_S8_UINT);
62 FORMAT_CASE(D32_FLOAT);
63 FORMAT_CASE(D32_FLOAT_S8_UINT);
64 FORMAT_CASE(S8_UINT);
65 FORMAT_CASE(Y8Cb8Cr8_420);
66 FORMAT_CASE(YCbCr_P010);
67 FORMAT_CASE(R8_UNORM);
68 FORMAT_CASE(R16_UINT);
69 FORMAT_CASE(R16G16_UINT);
70 FORMAT_CASE(R10G10B10A10_UNORM);
71 default: os << "unknown"; break;
72 }
73 }
74
75 #define BITS_CASE(x) case AHARDWAREBUFFER_USAGE_ ## x: os << #x ; break
76 #define PRINT_FLAG(x) \
77 do { if (usage & AHARDWAREBUFFER_USAGE_ ## x) { os << #x << " "; } } while (0)
78
PrintAhbUsage(std::ostream & os,uint64_t usage)79 void PrintAhbUsage(std::ostream& os, uint64_t usage) {
80 if (usage == 0) {
81 os << "none";
82 return;
83 }
84 switch (usage & AHARDWAREBUFFER_USAGE_CPU_READ_MASK) {
85 BITS_CASE(CPU_READ_NEVER);
86 BITS_CASE(CPU_READ_RARELY);
87 BITS_CASE(CPU_READ_OFTEN);
88 default: break;
89 }
90 switch (usage & AHARDWAREBUFFER_USAGE_CPU_WRITE_MASK) {
91 BITS_CASE(CPU_WRITE_NEVER);
92 BITS_CASE(CPU_WRITE_RARELY);
93 BITS_CASE(CPU_WRITE_OFTEN);
94 default: break;
95 }
96
97 PRINT_FLAG(GPU_SAMPLED_IMAGE);
98 PRINT_FLAG(GPU_COLOR_OUTPUT);
99 PRINT_FLAG(PROTECTED_CONTENT);
100 PRINT_FLAG(VIDEO_ENCODE);
101 PRINT_FLAG(SENSOR_DIRECT_DATA);
102 PRINT_FLAG(GPU_DATA_BUFFER);
103 PRINT_FLAG(GPU_CUBE_MAP);
104 PRINT_FLAG(GPU_MIPMAP_COMPLETE);
105
106 PRINT_FLAG(VENDOR_0);
107 PRINT_FLAG(VENDOR_1);
108 PRINT_FLAG(VENDOR_2);
109 PRINT_FLAG(VENDOR_3);
110 PRINT_FLAG(VENDOR_4);
111 PRINT_FLAG(VENDOR_5);
112 PRINT_FLAG(VENDOR_6);
113 PRINT_FLAG(VENDOR_7);
114 PRINT_FLAG(VENDOR_8);
115 PRINT_FLAG(VENDOR_9);
116 PRINT_FLAG(VENDOR_10);
117 PRINT_FLAG(VENDOR_11);
118 PRINT_FLAG(VENDOR_12);
119 PRINT_FLAG(VENDOR_13);
120 PRINT_FLAG(VENDOR_14);
121 PRINT_FLAG(VENDOR_15);
122 PRINT_FLAG(VENDOR_16);
123 PRINT_FLAG(VENDOR_17);
124 PRINT_FLAG(VENDOR_18);
125 PRINT_FLAG(VENDOR_19);
126 }
127
GetDescription(AHardwareBuffer * buffer)128 AHardwareBuffer_Desc GetDescription(AHardwareBuffer* buffer) {
129 AHardwareBuffer_Desc description;
130 AHardwareBuffer_describe(buffer, &description);
131 return description;
132 }
133
134 } // namespace
135
136 // GTest printer for AHardwareBuffer_Desc. Has to be in the global namespace.
PrintTo(const AHardwareBuffer_Desc & desc,::std::ostream * os)137 void PrintTo(const AHardwareBuffer_Desc& desc, ::std::ostream* os) {
138 *os << "AHardwareBuffer_Desc " << desc.width << "x" << desc.height;
139 if (desc.layers > 1) {
140 *os << ", " << desc.layers << " layers";
141 }
142 *os << ", usage = ";
143 PrintAhbUsage(*os, desc.usage);
144 *os << ", format = ";
145 PrintAhbFormat(*os, desc.format);
146 }
147
148 // Equality operators for AHardwareBuffer_Desc. Have to be in the global namespace.
operator ==(const AHardwareBuffer_Desc & a,const AHardwareBuffer_Desc & b)149 bool operator==(const AHardwareBuffer_Desc& a, const AHardwareBuffer_Desc& b) {
150 return a.width == b.width && a.height == b.height && a.layers == b.layers &&
151 a.usage == b.usage && a.format == b.format;
152 }
operator !=(const AHardwareBuffer_Desc & a,const AHardwareBuffer_Desc & b)153 bool operator!=(const AHardwareBuffer_Desc& a, const AHardwareBuffer_Desc& b) {
154 return !(a == b);
155 }
156
157 namespace android {
158
159 // Test that passing in NULL values to allocate works as expected.
TEST(AHardwareBufferTest,AllocateFailsWithNullInput)160 TEST(AHardwareBufferTest, AllocateFailsWithNullInput) {
161 AHardwareBuffer* buffer;
162 AHardwareBuffer_Desc desc;
163
164 memset(&desc, 0, sizeof(AHardwareBuffer_Desc));
165
166 int res = AHardwareBuffer_allocate(&desc, (AHardwareBuffer * * _Nonnull)NULL);
167 EXPECT_EQ(BAD_VALUE, res);
168 res = AHardwareBuffer_allocate((AHardwareBuffer_Desc* _Nonnull)NULL, &buffer);
169 EXPECT_EQ(BAD_VALUE, res);
170 res = AHardwareBuffer_allocate((AHardwareBuffer_Desc* _Nonnull)NULL,
171 (AHardwareBuffer * * _Nonnull)NULL);
172 EXPECT_EQ(BAD_VALUE, res);
173 }
174
175 // Test that passing in NULL values to allocate works as expected.
TEST(AHardwareBufferTest,BlobFormatRequiresHeight1)176 TEST(AHardwareBufferTest, BlobFormatRequiresHeight1) {
177 AHardwareBuffer* buffer;
178 AHardwareBuffer_Desc desc = {};
179
180 desc.width = 2;
181 desc.height = 4;
182 desc.layers = 1;
183 desc.usage = AHARDWAREBUFFER_USAGE_CPU_READ_RARELY;
184 desc.format = AHARDWAREBUFFER_FORMAT_BLOB;
185 int res = AHardwareBuffer_allocate(&desc, &buffer);
186 EXPECT_EQ(BAD_VALUE, res);
187
188 desc.height = 1;
189 res = AHardwareBuffer_allocate(&desc, &buffer);
190 EXPECT_EQ(NO_ERROR, res);
191 EXPECT_EQ(desc, GetDescription(buffer));
192 AHardwareBuffer_release(buffer);
193 buffer = NULL;
194 }
195
196 // Test that allocate can create an AHardwareBuffer correctly.
TEST(AHardwareBufferTest,AllocateSucceeds)197 TEST(AHardwareBufferTest, AllocateSucceeds) {
198 AHardwareBuffer* buffer = NULL;
199 AHardwareBuffer_Desc desc = {};
200
201 desc.width = 2;
202 desc.height = 4;
203 desc.layers = 1;
204 desc.usage = AHARDWAREBUFFER_USAGE_GPU_SAMPLED_IMAGE;
205 desc.format = AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM;
206 int res = AHardwareBuffer_allocate(&desc, &buffer);
207 EXPECT_EQ(NO_ERROR, res);
208 EXPECT_EQ(desc, GetDescription(buffer));
209 AHardwareBuffer_release(buffer);
210 buffer = NULL;
211
212 desc.width = 4;
213 desc.height = 12;
214 desc.layers = 1;
215 desc.usage = AHARDWAREBUFFER_USAGE_GPU_SAMPLED_IMAGE;
216 desc.format = AHARDWAREBUFFER_FORMAT_R5G6B5_UNORM;
217 res = AHardwareBuffer_allocate(&desc, &buffer);
218 EXPECT_EQ(NO_ERROR, res);
219 EXPECT_EQ(desc, GetDescription(buffer));
220 AHardwareBuffer_release(buffer);
221 }
222
223 // Test that allocate can create YUV AHardwareBuffers correctly.
TEST(AHardwareBufferTest,YuvAllocateSucceeds)224 TEST(AHardwareBufferTest, YuvAllocateSucceeds) {
225 AHardwareBuffer* buffer = NULL;
226 AHardwareBuffer_Desc desc = {};
227
228 desc.width = 16;
229 desc.height = 16;
230 desc.layers = 1;
231 desc.usage = AHARDWAREBUFFER_USAGE_GPU_SAMPLED_IMAGE;
232 desc.format = AHARDWAREBUFFER_FORMAT_Y8Cb8Cr8_420;
233 int res = AHardwareBuffer_allocate(&desc, &buffer);
234 EXPECT_EQ(NO_ERROR, res);
235 EXPECT_EQ(desc, GetDescription(buffer));
236 AHardwareBuffer_release(buffer);
237 buffer = NULL;
238 }
239
TEST(AHardwareBufferTest,DescribeSucceeds)240 TEST(AHardwareBufferTest, DescribeSucceeds) {
241 AHardwareBuffer* buffer = NULL;
242 AHardwareBuffer_Desc desc = {};
243
244 desc.width = 2;
245 desc.height = 4;
246 desc.layers = 1;
247 desc.usage = AHARDWAREBUFFER_USAGE_GPU_SAMPLED_IMAGE;
248 desc.format = AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM;
249 int res = AHardwareBuffer_allocate(&desc, &buffer);
250 EXPECT_EQ(NO_ERROR, res);
251
252 // Description of a null buffer should be all zeros.
253 AHardwareBuffer_Desc scratch_desc;
254 memset(&scratch_desc, 0, sizeof(AHardwareBuffer_Desc));
255 AHardwareBuffer_describe((AHardwareBuffer* _Nonnull)NULL, &scratch_desc);
256 EXPECT_EQ(0U, scratch_desc.width);
257 EXPECT_EQ(0U, scratch_desc.height);
258
259 // This shouldn't crash.
260 AHardwareBuffer_describe(buffer, (AHardwareBuffer_Desc* _Nonnull)NULL);
261
262 // Description of created buffer should match requsted description.
263 EXPECT_EQ(desc, GetDescription(buffer));
264 AHardwareBuffer_release(buffer);
265 }
266
267 struct ClientData {
268 int fd;
269 AHardwareBuffer* buffer;
ClientDataandroid::ClientData270 ClientData(int fd_in, AHardwareBuffer* buffer_in)
271 : fd(fd_in), buffer(buffer_in) {}
272 };
273
clientFunction(void * data)274 static void* clientFunction(void* data) {
275 ClientData* pdata = reinterpret_cast<ClientData*>(data);
276 int err = AHardwareBuffer_sendHandleToUnixSocket(pdata->buffer, pdata->fd);
277 EXPECT_EQ(NO_ERROR, err);
278 close(pdata->fd);
279 return 0;
280 }
281
TEST(AHardwareBufferTest,SendAndRecvSucceeds)282 TEST(AHardwareBufferTest, SendAndRecvSucceeds) {
283 AHardwareBuffer* buffer = NULL;
284 AHardwareBuffer_Desc desc = {};
285
286 desc.width = 2;
287 desc.height = 4;
288 desc.layers = 1;
289 desc.usage = AHARDWAREBUFFER_USAGE_GPU_SAMPLED_IMAGE;
290 desc.format = AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM;
291
292 // Test that an invalid buffer fails.
293 int err = AHardwareBuffer_sendHandleToUnixSocket((AHardwareBuffer* _Nonnull)NULL, 0);
294 EXPECT_EQ(BAD_VALUE, err);
295 err = 0;
296 err = AHardwareBuffer_sendHandleToUnixSocket(buffer, 0);
297 EXPECT_EQ(BAD_VALUE, err);
298
299 // Allocate the buffer.
300 err = AHardwareBuffer_allocate(&desc, &buffer);
301 EXPECT_EQ(NO_ERROR, err);
302
303 int fds[2];
304 err = socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, fds);
305
306 // Launch a client that will send the buffer back.
307 ClientData data(fds[1], buffer);
308 pthread_t thread;
309 EXPECT_EQ(0, pthread_create(&thread, NULL, clientFunction, &data));
310
311 // Receive the buffer.
312 err = AHardwareBuffer_recvHandleFromUnixSocket(fds[0], (AHardwareBuffer * * _Nonnull)NULL);
313 EXPECT_EQ(BAD_VALUE, err);
314
315 AHardwareBuffer* received = NULL;
316 err = AHardwareBuffer_recvHandleFromUnixSocket(fds[0], &received);
317 EXPECT_EQ(NO_ERROR, err);
318 EXPECT_TRUE(received != NULL);
319 EXPECT_EQ(desc, GetDescription(received));
320
321 void* ret_val;
322 EXPECT_EQ(0, pthread_join(thread, &ret_val));
323 EXPECT_EQ(NULL, ret_val);
324 close(fds[0]);
325
326 AHardwareBuffer_release(buffer);
327 AHardwareBuffer_release(received);
328 }
329
TEST(AHardwareBufferTest,LockAndGetInfoAndUnlockSucceed)330 TEST(AHardwareBufferTest, LockAndGetInfoAndUnlockSucceed) {
331 AHardwareBuffer* buffer = nullptr;
332 AHardwareBuffer_Desc desc = {};
333
334 desc.width = 2;
335 desc.height = 4;
336 desc.layers = 1;
337 desc.usage = AHARDWAREBUFFER_USAGE_GPU_SAMPLED_IMAGE | AHARDWAREBUFFER_USAGE_CPU_READ_RARELY;
338 desc.format = AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM;
339
340 int32_t bytesPerPixel = std::numeric_limits<int32_t>::min();
341 int32_t bytesPerStride = std::numeric_limits<int32_t>::min();
342
343 // Test that an invalid buffer fails.
344 int err =
345 AHardwareBuffer_lockAndGetInfo((AHardwareBuffer* _Nonnull)NULL, 0, -1, NULL,
346 (void** _Nonnull)NULL, &bytesPerPixel, &bytesPerStride);
347 EXPECT_EQ(BAD_VALUE, err);
348
349 err = AHardwareBuffer_allocate(&desc, &buffer);
350 EXPECT_EQ(NO_ERROR, err);
351 void* bufferData = NULL;
352
353 // Test invalid usage flag
354 err = AHardwareBuffer_lockAndGetInfo(buffer, ~(AHARDWAREBUFFER_USAGE_CPU_WRITE_MASK | AHARDWAREBUFFER_USAGE_CPU_READ_MASK), -1, NULL, &bufferData, &bytesPerPixel, &bytesPerStride);
355 EXPECT_EQ(BAD_VALUE, err);
356
357 err = AHardwareBuffer_lockAndGetInfo(buffer, AHARDWAREBUFFER_USAGE_CPU_READ_RARELY, -1, NULL, &bufferData, &bytesPerPixel, &bytesPerStride);
358
359 if (bytesPerPixel == -1 || bytesPerStride == -1) {
360 EXPECT_EQ(INVALID_OPERATION, err);
361 } else {
362 EXPECT_EQ(NO_ERROR, err);
363 EXPECT_LE(0, bytesPerPixel);
364 EXPECT_LE(0, bytesPerStride);
365 EXPECT_TRUE(bufferData != NULL);
366
367 err = AHardwareBuffer_unlock(buffer, nullptr);
368 EXPECT_EQ(NO_ERROR, err);
369 }
370 AHardwareBuffer_release(buffer);
371 }
372
TEST(AHardwareBufferTest,LockAndUnlockSucceed)373 TEST(AHardwareBufferTest, LockAndUnlockSucceed) {
374 AHardwareBuffer* buffer = NULL;
375 AHardwareBuffer_Desc desc = {};
376
377 desc.width = 2;
378 desc.height = 4;
379 desc.layers = 1;
380 desc.usage = AHARDWAREBUFFER_USAGE_GPU_SAMPLED_IMAGE | AHARDWAREBUFFER_USAGE_CPU_READ_RARELY;
381 desc.format = AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM;
382
383 // Test that an invalid buffer fails.
384 int err = AHardwareBuffer_lock((AHardwareBuffer* _Nonnull)NULL, 0, -1, NULL,
385 (void** _Nonnull)NULL);
386 EXPECT_EQ(BAD_VALUE, err);
387 err = 0;
388
389 // Allocate the buffer.
390 err = AHardwareBuffer_allocate(&desc, &buffer);
391 EXPECT_EQ(NO_ERROR, err);
392 void* bufferData = NULL;
393 err = AHardwareBuffer_lock(buffer, AHARDWAREBUFFER_USAGE_CPU_READ_RARELY, -1,
394 NULL, &bufferData);
395 EXPECT_EQ(NO_ERROR, err);
396 EXPECT_TRUE(bufferData != NULL);
397 err = AHardwareBuffer_unlock(buffer, nullptr);
398
399 AHardwareBuffer_release(buffer);
400 }
401
TEST(AHardwareBufferTest,PlanarLockAndUnlockYuvSucceed)402 TEST(AHardwareBufferTest, PlanarLockAndUnlockYuvSucceed) {
403 AHardwareBuffer* buffer = NULL;
404 AHardwareBuffer_Desc desc = {};
405
406 desc.width = 16;
407 desc.height = 32;
408 desc.layers = 1;
409 desc.usage = AHARDWAREBUFFER_USAGE_GPU_SAMPLED_IMAGE | AHARDWAREBUFFER_USAGE_CPU_READ_RARELY;
410 desc.format = AHARDWAREBUFFER_FORMAT_Y8Cb8Cr8_420;
411
412 // Test that an invalid buffer fails.
413 int err = AHardwareBuffer_lock((AHardwareBuffer* _Nonnull)NULL, 0, -1, NULL,
414 (void** _Nonnull)NULL);
415 EXPECT_EQ(BAD_VALUE, err);
416 err = 0;
417
418 // Allocate the buffer.
419 err = AHardwareBuffer_allocate(&desc, &buffer);
420 EXPECT_EQ(NO_ERROR, err);
421
422 // Lock its planes
423 AHardwareBuffer_Planes planes;
424 err = AHardwareBuffer_lockPlanes(buffer, AHARDWAREBUFFER_USAGE_CPU_READ_RARELY, -1, NULL,
425 &planes);
426
427 // Make sure everything looks right
428 EXPECT_EQ(NO_ERROR, err);
429 EXPECT_EQ(3U, planes.planeCount);
430
431 EXPECT_TRUE(planes.planes[0].data != NULL);
432 EXPECT_EQ(1U, planes.planes[0].pixelStride);
433 EXPECT_TRUE(planes.planes[0].rowStride >= 16);
434
435 EXPECT_TRUE(planes.planes[1].data != NULL);
436 EXPECT_THAT(planes.planes[1].pixelStride, AnyOf(Eq(1U), Eq(2U)));
437 EXPECT_TRUE(planes.planes[1].rowStride >= 8);
438
439 EXPECT_TRUE(planes.planes[2].data != NULL);
440 EXPECT_THAT(planes.planes[2].pixelStride, AnyOf(Eq(1U), Eq(2U)));
441 EXPECT_TRUE(planes.planes[2].rowStride >= 8);
442
443 // Unlock
444 err = AHardwareBuffer_unlock(buffer, nullptr);
445 EXPECT_EQ(NO_ERROR, err);
446
447 AHardwareBuffer_release(buffer);
448 }
449
TEST(AHardwareBufferTest,PlanarLockAndUnlockYuvP010Succeed)450 TEST(AHardwareBufferTest, PlanarLockAndUnlockYuvP010Succeed) {
451 AHardwareBuffer* buffer = NULL;
452 AHardwareBuffer_Desc desc = {};
453
454 desc.width = 32;
455 desc.height = 32;
456 desc.layers = 1;
457 desc.usage = AHARDWAREBUFFER_USAGE_CPU_READ_RARELY;
458 desc.format = AHARDWAREBUFFER_FORMAT_YCbCr_P010;
459
460 if (!AHardwareBuffer_isSupported(&desc)) {
461 ALOGI("Test skipped: AHARDWAREBUFFER_FORMAT_YCbCr_P010 not supported.");
462 return;
463 }
464
465 // Allocate the buffer.
466 int err = AHardwareBuffer_allocate(&desc, &buffer);
467 EXPECT_EQ(NO_ERROR, err);
468
469 // Lock its planes
470 AHardwareBuffer_Planes planes;
471 err = AHardwareBuffer_lockPlanes(buffer, AHARDWAREBUFFER_USAGE_CPU_READ_RARELY, -1, NULL,
472 &planes);
473
474 // Make sure everything looks right
475 EXPECT_EQ(NO_ERROR, err);
476 EXPECT_EQ(3U, planes.planeCount);
477
478 const uint32_t yPlaneWidth = desc.width;
479 const uint32_t cPlaneWidth = desc.width / 2;
480 const uint32_t bytesPerPixel = 2;
481
482 EXPECT_THAT(planes.planes[0].data, NotNull());
483 EXPECT_THAT(planes.planes[0].pixelStride, Eq(bytesPerPixel));
484 EXPECT_THAT(planes.planes[0].rowStride, Ge(yPlaneWidth * bytesPerPixel));
485
486 EXPECT_THAT(planes.planes[1].data, NotNull());
487 EXPECT_THAT(planes.planes[1].pixelStride, Eq(bytesPerPixel * /*interleaved=*/2));
488 EXPECT_THAT(planes.planes[1].rowStride, Ge(cPlaneWidth * bytesPerPixel));
489
490 EXPECT_THAT(planes.planes[2].data, NotNull());
491 EXPECT_THAT(planes.planes[2].pixelStride, Eq(bytesPerPixel * /*interleaved=*/2));
492 EXPECT_THAT(planes.planes[2].rowStride, Ge(cPlaneWidth * bytesPerPixel));
493
494 // Unlock
495 err = AHardwareBuffer_unlock(buffer, nullptr);
496 EXPECT_EQ(NO_ERROR, err);
497
498 AHardwareBuffer_release(buffer);
499 }
500
TEST(AHardwareBufferTest,PlanarLockAndUnlockRgbaSucceed)501 TEST(AHardwareBufferTest, PlanarLockAndUnlockRgbaSucceed) {
502 AHardwareBuffer* buffer = NULL;
503 AHardwareBuffer_Desc desc = {};
504
505 desc.width = 16;
506 desc.height = 32;
507 desc.layers = 1;
508 desc.usage = AHARDWAREBUFFER_USAGE_GPU_SAMPLED_IMAGE | AHARDWAREBUFFER_USAGE_CPU_READ_RARELY;
509 desc.format = AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM;
510
511 // Test that an invalid buffer fails.
512 int err = AHardwareBuffer_lock((AHardwareBuffer* _Nonnull)NULL, 0, -1, NULL,
513 (void** _Nonnull)NULL);
514 EXPECT_EQ(BAD_VALUE, err);
515 err = 0;
516
517 // Allocate the buffer.
518 err = AHardwareBuffer_allocate(&desc, &buffer);
519 EXPECT_EQ(NO_ERROR, err);
520
521 // Lock its planes
522 AHardwareBuffer_Planes planes;
523 err = AHardwareBuffer_lockPlanes(buffer, AHARDWAREBUFFER_USAGE_CPU_READ_RARELY, -1, NULL,
524 &planes);
525
526 // Make sure everything looks right
527 EXPECT_EQ(NO_ERROR, err);
528 EXPECT_EQ(1U, planes.planeCount);
529
530 EXPECT_TRUE(planes.planes[0].data != NULL);
531 EXPECT_EQ(4U, planes.planes[0].pixelStride);
532 EXPECT_TRUE(planes.planes[0].rowStride >= 64);
533
534 // Unlock
535 err = AHardwareBuffer_unlock(buffer, nullptr);
536 EXPECT_EQ(NO_ERROR, err);
537
538 AHardwareBuffer_release(buffer);
539 }
540
TEST(AHardwareBufferTest,ProtectedContentAndCpuReadIncompatible)541 TEST(AHardwareBufferTest, ProtectedContentAndCpuReadIncompatible) {
542 AHardwareBuffer* buffer = NULL;
543 AHardwareBuffer_Desc desc = {};
544 desc.width = 120;
545 desc.height = 240;
546 desc.layers = 1;
547 desc.usage = AHARDWAREBUFFER_USAGE_GPU_COLOR_OUTPUT | AHARDWAREBUFFER_USAGE_CPU_READ_OFTEN;
548 desc.format = AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM;
549
550 // Allocation of a CPU-readable buffer should succeed...
551 int err = AHardwareBuffer_allocate(&desc, &buffer);
552 EXPECT_EQ(NO_ERROR, err);
553 AHardwareBuffer_release(buffer);
554 buffer = nullptr;
555
556 // ...but not if it's a protected buffer.
557 desc.usage =
558 AHARDWAREBUFFER_USAGE_GPU_COLOR_OUTPUT |
559 AHARDWAREBUFFER_USAGE_CPU_READ_OFTEN |
560 AHARDWAREBUFFER_USAGE_PROTECTED_CONTENT;
561 err = AHardwareBuffer_allocate(&desc, &buffer);
562 EXPECT_NE(NO_ERROR, err);
563
564 desc.usage =
565 AHARDWAREBUFFER_USAGE_GPU_COLOR_OUTPUT |
566 AHARDWAREBUFFER_USAGE_CPU_READ_RARELY |
567 AHARDWAREBUFFER_USAGE_PROTECTED_CONTENT;
568 err = AHardwareBuffer_allocate(&desc, &buffer);
569 EXPECT_NE(NO_ERROR, err);
570 }
571
TEST(AHardwareBufferTest,GetIdSucceed)572 TEST(AHardwareBufferTest, GetIdSucceed) {
573 AHardwareBuffer* buffer1 = nullptr;
574 uint64_t id1 = 0;
575 const AHardwareBuffer_Desc desc = {
576 .width = 4,
577 .height = 4,
578 .layers = 1,
579 .format = AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM,
580 .usage = AHARDWAREBUFFER_USAGE_CPU_READ_RARELY,
581 };
582 int err = AHardwareBuffer_allocate(&desc, &buffer1);
583 EXPECT_EQ(NO_ERROR, err);
584 EXPECT_NE(nullptr, buffer1);
585 EXPECT_EQ(0, AHardwareBuffer_getId(buffer1, &id1));
586 EXPECT_NE(id1, 0ULL);
587
588 AHardwareBuffer* buffer2 = nullptr;
589 uint64_t id2 = 0;
590 err = AHardwareBuffer_allocate(&desc, &buffer2);
591 EXPECT_EQ(NO_ERROR, err);
592 EXPECT_NE(nullptr, buffer2);
593 EXPECT_EQ(0, AHardwareBuffer_getId(buffer2, &id2));
594 EXPECT_NE(id2, 0ULL);
595
596 EXPECT_NE(id1, id2);
597 }
598
599 } // namespace android
600