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 
46 #define FORMAT_CASE(x) case AHARDWAREBUFFER_FORMAT_ ## x: os << #x ; break
47 
PrintAhbFormat(std::ostream & os,uint64_t format)48 void PrintAhbFormat(std::ostream& os, uint64_t format) {
49     switch (format) {
50         FORMAT_CASE(R8G8B8A8_UNORM);
51         FORMAT_CASE(R8G8B8X8_UNORM);
52         FORMAT_CASE(R8G8B8_UNORM);
53         FORMAT_CASE(R5G6B5_UNORM);
54         FORMAT_CASE(R16G16B16A16_FLOAT);
55         FORMAT_CASE(R10G10B10A2_UNORM);
56         FORMAT_CASE(BLOB);
57         FORMAT_CASE(D16_UNORM);
58         FORMAT_CASE(D24_UNORM);
59         FORMAT_CASE(D24_UNORM_S8_UINT);
60         FORMAT_CASE(D32_FLOAT);
61         FORMAT_CASE(D32_FLOAT_S8_UINT);
62         FORMAT_CASE(S8_UINT);
63         default: os << "unknown"; break;
64     }
65 }
66 
67 #define BITS_CASE(x) case AHARDWAREBUFFER_USAGE_ ## x: os << #x ; break
68 #define PRINT_FLAG(x) \
69     do { if (usage & AHARDWAREBUFFER_USAGE_ ## x) { os << #x << " "; } } while (0)
70 
PrintAhbUsage(std::ostream & os,uint64_t usage)71 void PrintAhbUsage(std::ostream& os, uint64_t usage) {
72     if (usage == 0) {
73         os << "none";
74         return;
75     }
76     switch (usage & AHARDWAREBUFFER_USAGE_CPU_READ_MASK) {
77         BITS_CASE(CPU_READ_NEVER);
78         BITS_CASE(CPU_READ_RARELY);
79         BITS_CASE(CPU_READ_OFTEN);
80         default: break;
81     }
82     switch (usage & AHARDWAREBUFFER_USAGE_CPU_WRITE_MASK) {
83         BITS_CASE(CPU_WRITE_NEVER);
84         BITS_CASE(CPU_WRITE_RARELY);
85         BITS_CASE(CPU_WRITE_OFTEN);
86         default: break;
87     }
88 
89     PRINT_FLAG(GPU_SAMPLED_IMAGE);
90     PRINT_FLAG(GPU_COLOR_OUTPUT);
91     PRINT_FLAG(PROTECTED_CONTENT);
92     PRINT_FLAG(VIDEO_ENCODE);
93     PRINT_FLAG(SENSOR_DIRECT_DATA);
94     PRINT_FLAG(GPU_DATA_BUFFER);
95     PRINT_FLAG(GPU_CUBE_MAP);
96     PRINT_FLAG(GPU_MIPMAP_COMPLETE);
97 
98     PRINT_FLAG(VENDOR_0);
99     PRINT_FLAG(VENDOR_1);
100     PRINT_FLAG(VENDOR_2);
101     PRINT_FLAG(VENDOR_3);
102     PRINT_FLAG(VENDOR_4);
103     PRINT_FLAG(VENDOR_5);
104     PRINT_FLAG(VENDOR_6);
105     PRINT_FLAG(VENDOR_7);
106     PRINT_FLAG(VENDOR_8);
107     PRINT_FLAG(VENDOR_9);
108     PRINT_FLAG(VENDOR_10);
109     PRINT_FLAG(VENDOR_11);
110     PRINT_FLAG(VENDOR_12);
111     PRINT_FLAG(VENDOR_13);
112     PRINT_FLAG(VENDOR_14);
113     PRINT_FLAG(VENDOR_15);
114     PRINT_FLAG(VENDOR_16);
115     PRINT_FLAG(VENDOR_17);
116     PRINT_FLAG(VENDOR_18);
117     PRINT_FLAG(VENDOR_19);
118 }
119 
GetDescription(AHardwareBuffer * buffer)120 AHardwareBuffer_Desc GetDescription(AHardwareBuffer* buffer) {
121     AHardwareBuffer_Desc description;
122     AHardwareBuffer_describe(buffer, &description);
123     return description;
124 }
125 
126 }  // namespace
127 
128 // GTest printer for AHardwareBuffer_Desc. Has to be in the global namespace.
PrintTo(const AHardwareBuffer_Desc & desc,::std::ostream * os)129 void PrintTo(const AHardwareBuffer_Desc& desc, ::std::ostream* os) {
130     *os << "AHardwareBuffer_Desc " << desc.width << "x" << desc.height;
131     if (desc.layers > 1) {
132         *os << ", " << desc.layers << " layers";
133     }
134     *os << ", usage = ";
135     PrintAhbUsage(*os, desc.usage);
136     *os << ", format = ";
137     PrintAhbFormat(*os, desc.format);
138 }
139 
140 // Equality operators for AHardwareBuffer_Desc. Have to be in the global namespace.
operator ==(const AHardwareBuffer_Desc & a,const AHardwareBuffer_Desc & b)141 bool operator==(const AHardwareBuffer_Desc& a, const AHardwareBuffer_Desc& b) {
142     return a.width == b.width && a.height == b.height && a.layers == b.layers &&
143         a.usage == b.usage && a.format == b.format;
144 }
operator !=(const AHardwareBuffer_Desc & a,const AHardwareBuffer_Desc & b)145 bool operator!=(const AHardwareBuffer_Desc& a, const AHardwareBuffer_Desc& b) {
146     return !(a == b);
147 }
148 
149 namespace android {
150 
151 // Test that passing in NULL values to allocate works as expected.
TEST(AHardwareBufferTest,AllocateFailsWithNullInput)152 TEST(AHardwareBufferTest, AllocateFailsWithNullInput) {
153     AHardwareBuffer* buffer;
154     AHardwareBuffer_Desc desc;
155 
156     memset(&desc, 0, sizeof(AHardwareBuffer_Desc));
157 
158     int res = AHardwareBuffer_allocate(&desc, (AHardwareBuffer * * _Nonnull)NULL);
159     EXPECT_EQ(BAD_VALUE, res);
160     res = AHardwareBuffer_allocate((AHardwareBuffer_Desc* _Nonnull)NULL, &buffer);
161     EXPECT_EQ(BAD_VALUE, res);
162     res = AHardwareBuffer_allocate((AHardwareBuffer_Desc* _Nonnull)NULL,
163                                    (AHardwareBuffer * * _Nonnull)NULL);
164     EXPECT_EQ(BAD_VALUE, res);
165 }
166 
167 // Test that passing in NULL values to allocate works as expected.
TEST(AHardwareBufferTest,BlobFormatRequiresHeight1)168 TEST(AHardwareBufferTest, BlobFormatRequiresHeight1) {
169     AHardwareBuffer* buffer;
170     AHardwareBuffer_Desc desc = {};
171 
172     desc.width = 2;
173     desc.height = 4;
174     desc.layers = 1;
175     desc.usage = AHARDWAREBUFFER_USAGE_CPU_READ_RARELY;
176     desc.format = AHARDWAREBUFFER_FORMAT_BLOB;
177     int res = AHardwareBuffer_allocate(&desc, &buffer);
178     EXPECT_EQ(BAD_VALUE, res);
179 
180     desc.height = 1;
181     res = AHardwareBuffer_allocate(&desc, &buffer);
182     EXPECT_EQ(NO_ERROR, res);
183     EXPECT_EQ(desc, GetDescription(buffer));
184     AHardwareBuffer_release(buffer);
185     buffer = NULL;
186 }
187 
188 // Test that allocate can create an AHardwareBuffer correctly.
TEST(AHardwareBufferTest,AllocateSucceeds)189 TEST(AHardwareBufferTest, AllocateSucceeds) {
190     AHardwareBuffer* buffer = NULL;
191     AHardwareBuffer_Desc desc = {};
192 
193     desc.width = 2;
194     desc.height = 4;
195     desc.layers = 1;
196     desc.usage = AHARDWAREBUFFER_USAGE_GPU_SAMPLED_IMAGE;
197     desc.format = AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM;
198     int res = AHardwareBuffer_allocate(&desc, &buffer);
199     EXPECT_EQ(NO_ERROR, res);
200     EXPECT_EQ(desc, GetDescription(buffer));
201     AHardwareBuffer_release(buffer);
202     buffer = NULL;
203 
204     desc.width = 4;
205     desc.height = 12;
206     desc.layers = 1;
207     desc.usage = AHARDWAREBUFFER_USAGE_GPU_SAMPLED_IMAGE;
208     desc.format = AHARDWAREBUFFER_FORMAT_R5G6B5_UNORM;
209     res = AHardwareBuffer_allocate(&desc, &buffer);
210     EXPECT_EQ(NO_ERROR, res);
211     EXPECT_EQ(desc, GetDescription(buffer));
212     AHardwareBuffer_release(buffer);
213 }
214 
215 // Test that allocate can create YUV AHardwareBuffers correctly.
TEST(AHardwareBufferTest,YuvAllocateSucceeds)216 TEST(AHardwareBufferTest, YuvAllocateSucceeds) {
217     AHardwareBuffer* buffer = NULL;
218     AHardwareBuffer_Desc desc = {};
219 
220     desc.width = 16;
221     desc.height = 16;
222     desc.layers = 1;
223     desc.usage = AHARDWAREBUFFER_USAGE_GPU_SAMPLED_IMAGE;
224     desc.format = AHARDWAREBUFFER_FORMAT_Y8Cb8Cr8_420;
225     int res = AHardwareBuffer_allocate(&desc, &buffer);
226     EXPECT_EQ(NO_ERROR, res);
227     EXPECT_EQ(desc, GetDescription(buffer));
228     AHardwareBuffer_release(buffer);
229     buffer = NULL;
230 }
231 
TEST(AHardwareBufferTest,DescribeSucceeds)232 TEST(AHardwareBufferTest, DescribeSucceeds) {
233     AHardwareBuffer* buffer = NULL;
234     AHardwareBuffer_Desc desc = {};
235 
236     desc.width = 2;
237     desc.height = 4;
238     desc.layers = 1;
239     desc.usage = AHARDWAREBUFFER_USAGE_GPU_SAMPLED_IMAGE;
240     desc.format = AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM;
241     int res = AHardwareBuffer_allocate(&desc, &buffer);
242     EXPECT_EQ(NO_ERROR, res);
243 
244     // Description of a null buffer should be all zeros.
245     AHardwareBuffer_Desc scratch_desc;
246     memset(&scratch_desc, 0, sizeof(AHardwareBuffer_Desc));
247     AHardwareBuffer_describe((AHardwareBuffer* _Nonnull)NULL, &scratch_desc);
248     EXPECT_EQ(0U, scratch_desc.width);
249     EXPECT_EQ(0U, scratch_desc.height);
250 
251     // This shouldn't crash.
252     AHardwareBuffer_describe(buffer, (AHardwareBuffer_Desc* _Nonnull)NULL);
253 
254     // Description of created buffer should match requsted description.
255     EXPECT_EQ(desc, GetDescription(buffer));
256     AHardwareBuffer_release(buffer);
257 }
258 
259 struct ClientData {
260     int fd;
261     AHardwareBuffer* buffer;
ClientDataandroid::ClientData262     ClientData(int fd_in, AHardwareBuffer* buffer_in)
263             : fd(fd_in), buffer(buffer_in) {}
264 };
265 
clientFunction(void * data)266 static void* clientFunction(void* data) {
267     ClientData* pdata = reinterpret_cast<ClientData*>(data);
268     int err = AHardwareBuffer_sendHandleToUnixSocket(pdata->buffer, pdata->fd);
269     EXPECT_EQ(NO_ERROR, err);
270     close(pdata->fd);
271     return 0;
272 }
273 
TEST(AHardwareBufferTest,SendAndRecvSucceeds)274 TEST(AHardwareBufferTest, SendAndRecvSucceeds) {
275     AHardwareBuffer* buffer = NULL;
276     AHardwareBuffer_Desc desc = {};
277 
278     desc.width = 2;
279     desc.height = 4;
280     desc.layers = 1;
281     desc.usage = AHARDWAREBUFFER_USAGE_GPU_SAMPLED_IMAGE;
282     desc.format = AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM;
283 
284     // Test that an invalid buffer fails.
285     int err = AHardwareBuffer_sendHandleToUnixSocket((AHardwareBuffer* _Nonnull)NULL, 0);
286     EXPECT_EQ(BAD_VALUE, err);
287     err = 0;
288     err = AHardwareBuffer_sendHandleToUnixSocket(buffer, 0);
289     EXPECT_EQ(BAD_VALUE, err);
290 
291     // Allocate the buffer.
292     err = AHardwareBuffer_allocate(&desc, &buffer);
293     EXPECT_EQ(NO_ERROR, err);
294 
295     int fds[2];
296     err = socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, fds);
297 
298     // Launch a client that will send the buffer back.
299     ClientData data(fds[1], buffer);
300     pthread_t thread;
301     EXPECT_EQ(0, pthread_create(&thread, NULL, clientFunction, &data));
302 
303     // Receive the buffer.
304     err = AHardwareBuffer_recvHandleFromUnixSocket(fds[0], (AHardwareBuffer * * _Nonnull)NULL);
305     EXPECT_EQ(BAD_VALUE, err);
306 
307     AHardwareBuffer* received = NULL;
308     err = AHardwareBuffer_recvHandleFromUnixSocket(fds[0], &received);
309     EXPECT_EQ(NO_ERROR, err);
310     EXPECT_TRUE(received != NULL);
311     EXPECT_EQ(desc, GetDescription(received));
312 
313     void* ret_val;
314     EXPECT_EQ(0, pthread_join(thread, &ret_val));
315     EXPECT_EQ(NULL, ret_val);
316     close(fds[0]);
317 
318     AHardwareBuffer_release(buffer);
319     AHardwareBuffer_release(received);
320 }
321 
TEST(AHardwareBufferTest,LockAndGetInfoAndUnlockSucceed)322 TEST(AHardwareBufferTest, LockAndGetInfoAndUnlockSucceed) {
323     AHardwareBuffer* buffer = nullptr;
324     AHardwareBuffer_Desc desc = {};
325 
326     desc.width = 2;
327     desc.height = 4;
328     desc.layers = 1;
329     desc.usage = AHARDWAREBUFFER_USAGE_GPU_SAMPLED_IMAGE | AHARDWAREBUFFER_USAGE_CPU_READ_RARELY;
330     desc.format = AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM;
331 
332     int32_t bytesPerPixel = std::numeric_limits<int32_t>::min();
333     int32_t bytesPerStride = std::numeric_limits<int32_t>::min();
334 
335     // Test that an invalid buffer fails.
336     int err =
337             AHardwareBuffer_lockAndGetInfo((AHardwareBuffer* _Nonnull)NULL, 0, -1, NULL,
338                                            (void** _Nonnull)NULL, &bytesPerPixel, &bytesPerStride);
339     EXPECT_EQ(BAD_VALUE, err);
340 
341     err = AHardwareBuffer_allocate(&desc, &buffer);
342     EXPECT_EQ(NO_ERROR, err);
343     void* bufferData = NULL;
344 
345     // Test invalid usage flag
346     err = AHardwareBuffer_lockAndGetInfo(buffer, ~(AHARDWAREBUFFER_USAGE_CPU_WRITE_MASK | AHARDWAREBUFFER_USAGE_CPU_READ_MASK), -1, NULL, &bufferData, &bytesPerPixel, &bytesPerStride);
347     EXPECT_EQ(BAD_VALUE, err);
348 
349     err = AHardwareBuffer_lockAndGetInfo(buffer, AHARDWAREBUFFER_USAGE_CPU_READ_RARELY, -1, NULL, &bufferData, &bytesPerPixel, &bytesPerStride);
350 
351     if (bytesPerPixel == -1 || bytesPerStride == -1) {
352         EXPECT_EQ(INVALID_OPERATION, err);
353     } else {
354         EXPECT_EQ(NO_ERROR, err);
355         EXPECT_LE(0, bytesPerPixel);
356         EXPECT_LE(0, bytesPerStride);
357         EXPECT_TRUE(bufferData != NULL);
358 
359         int32_t fence = -1;
360         err = AHardwareBuffer_unlock(buffer, &fence);
361         EXPECT_EQ(NO_ERROR, err);
362     }
363     AHardwareBuffer_release(buffer);
364 }
365 
TEST(AHardwareBufferTest,LockAndUnlockSucceed)366 TEST(AHardwareBufferTest, LockAndUnlockSucceed) {
367     AHardwareBuffer* buffer = NULL;
368     AHardwareBuffer_Desc desc = {};
369 
370     desc.width = 2;
371     desc.height = 4;
372     desc.layers = 1;
373     desc.usage = AHARDWAREBUFFER_USAGE_GPU_SAMPLED_IMAGE | AHARDWAREBUFFER_USAGE_CPU_READ_RARELY;
374     desc.format = AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM;
375 
376     // Test that an invalid buffer fails.
377     int err = AHardwareBuffer_lock((AHardwareBuffer* _Nonnull)NULL, 0, -1, NULL,
378                                    (void** _Nonnull)NULL);
379     EXPECT_EQ(BAD_VALUE, err);
380     err = 0;
381 
382     // Allocate the buffer.
383     err = AHardwareBuffer_allocate(&desc, &buffer);
384     EXPECT_EQ(NO_ERROR, err);
385     void* bufferData = NULL;
386     err = AHardwareBuffer_lock(buffer, AHARDWAREBUFFER_USAGE_CPU_READ_RARELY, -1,
387           NULL, &bufferData);
388     EXPECT_EQ(NO_ERROR, err);
389     EXPECT_TRUE(bufferData != NULL);
390     int32_t fence = -1;
391     err = AHardwareBuffer_unlock(buffer, &fence);
392 
393     AHardwareBuffer_release(buffer);
394 }
395 
TEST(AHardwareBufferTest,PlanarLockAndUnlockYuvSucceed)396 TEST(AHardwareBufferTest, PlanarLockAndUnlockYuvSucceed) {
397     AHardwareBuffer* buffer = NULL;
398     AHardwareBuffer_Desc desc = {};
399 
400     desc.width = 16;
401     desc.height = 32;
402     desc.layers = 1;
403     desc.usage = AHARDWAREBUFFER_USAGE_GPU_SAMPLED_IMAGE | AHARDWAREBUFFER_USAGE_CPU_READ_RARELY;
404     desc.format = AHARDWAREBUFFER_FORMAT_Y8Cb8Cr8_420;
405 
406     // Test that an invalid buffer fails.
407     int err = AHardwareBuffer_lock((AHardwareBuffer* _Nonnull)NULL, 0, -1, NULL,
408                                    (void** _Nonnull)NULL);
409     EXPECT_EQ(BAD_VALUE, err);
410     err = 0;
411 
412     // Allocate the buffer.
413     err = AHardwareBuffer_allocate(&desc, &buffer);
414     EXPECT_EQ(NO_ERROR, err);
415 
416     // Lock its planes
417     AHardwareBuffer_Planes planes;
418     err = AHardwareBuffer_lockPlanes(buffer, AHARDWAREBUFFER_USAGE_CPU_READ_RARELY, -1, NULL,
419         &planes);
420 
421     // Make sure everything looks right
422     EXPECT_EQ(NO_ERROR, err);
423     EXPECT_EQ(3U, planes.planeCount);
424 
425     EXPECT_TRUE(planes.planes[0].data != NULL);
426     EXPECT_EQ(1U, planes.planes[0].pixelStride);
427     EXPECT_TRUE(planes.planes[0].rowStride >= 16);
428 
429     EXPECT_TRUE(planes.planes[1].data != NULL);
430     EXPECT_THAT(planes.planes[1].pixelStride, AnyOf(Eq(1U), Eq(2U)));
431     EXPECT_TRUE(planes.planes[1].rowStride >= 8);
432 
433     EXPECT_TRUE(planes.planes[2].data != NULL);
434     EXPECT_THAT(planes.planes[2].pixelStride, AnyOf(Eq(1U), Eq(2U)));
435     EXPECT_TRUE(planes.planes[2].rowStride >= 8);
436 
437     // Unlock
438     int32_t fence = -1;
439     err = AHardwareBuffer_unlock(buffer, &fence);
440     EXPECT_EQ(NO_ERROR, err);
441 
442     AHardwareBuffer_release(buffer);
443 }
444 
TEST(AHardwareBufferTest,PlanarLockAndUnlockRgbaSucceed)445 TEST(AHardwareBufferTest, PlanarLockAndUnlockRgbaSucceed) {
446     AHardwareBuffer* buffer = NULL;
447     AHardwareBuffer_Desc desc = {};
448 
449     desc.width = 16;
450     desc.height = 32;
451     desc.layers = 1;
452     desc.usage = AHARDWAREBUFFER_USAGE_GPU_SAMPLED_IMAGE | AHARDWAREBUFFER_USAGE_CPU_READ_RARELY;
453     desc.format = AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM;
454 
455     // Test that an invalid buffer fails.
456     int err = AHardwareBuffer_lock((AHardwareBuffer* _Nonnull)NULL, 0, -1, NULL,
457                                    (void** _Nonnull)NULL);
458     EXPECT_EQ(BAD_VALUE, err);
459     err = 0;
460 
461     // Allocate the buffer.
462     err = AHardwareBuffer_allocate(&desc, &buffer);
463     EXPECT_EQ(NO_ERROR, err);
464 
465     // Lock its planes
466     AHardwareBuffer_Planes planes;
467     err = AHardwareBuffer_lockPlanes(buffer, AHARDWAREBUFFER_USAGE_CPU_READ_RARELY, -1, NULL,
468         &planes);
469 
470     // Make sure everything looks right
471     EXPECT_EQ(NO_ERROR, err);
472     EXPECT_EQ(1U, planes.planeCount);
473 
474     EXPECT_TRUE(planes.planes[0].data != NULL);
475     EXPECT_EQ(4U, planes.planes[0].pixelStride);
476     EXPECT_TRUE(planes.planes[0].rowStride >= 64);
477 
478     // Unlock
479     int32_t fence = -1;
480     err = AHardwareBuffer_unlock(buffer, &fence);
481     EXPECT_EQ(NO_ERROR, err);
482 
483     AHardwareBuffer_release(buffer);
484 }
485 
TEST(AHardwareBufferTest,ProtectedContentAndCpuReadIncompatible)486 TEST(AHardwareBufferTest, ProtectedContentAndCpuReadIncompatible) {
487     AHardwareBuffer* buffer = NULL;
488     AHardwareBuffer_Desc desc = {};
489     desc.width = 120;
490     desc.height = 240;
491     desc.layers = 1;
492     desc.usage = AHARDWAREBUFFER_USAGE_GPU_COLOR_OUTPUT | AHARDWAREBUFFER_USAGE_CPU_READ_OFTEN;
493     desc.format = AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM;
494 
495     // Allocation of a CPU-readable buffer should succeed...
496     int err = AHardwareBuffer_allocate(&desc, &buffer);
497     EXPECT_EQ(NO_ERROR, err);
498     AHardwareBuffer_release(buffer);
499     buffer = nullptr;
500 
501     // ...but not if it's a protected buffer.
502     desc.usage =
503         AHARDWAREBUFFER_USAGE_GPU_COLOR_OUTPUT |
504         AHARDWAREBUFFER_USAGE_CPU_READ_OFTEN |
505         AHARDWAREBUFFER_USAGE_PROTECTED_CONTENT;
506     err = AHardwareBuffer_allocate(&desc, &buffer);
507     EXPECT_NE(NO_ERROR, err);
508 
509     desc.usage =
510         AHARDWAREBUFFER_USAGE_GPU_COLOR_OUTPUT |
511         AHARDWAREBUFFER_USAGE_CPU_READ_RARELY |
512         AHARDWAREBUFFER_USAGE_PROTECTED_CONTENT;
513     err = AHardwareBuffer_allocate(&desc, &buffer);
514     EXPECT_NE(NO_ERROR, err);
515 }
516 
TEST(AHardwareBufferTest,GetIdSucceed)517 TEST(AHardwareBufferTest, GetIdSucceed) {
518     AHardwareBuffer* buffer1 = nullptr;
519     uint64_t id1 = 0;
520     const AHardwareBuffer_Desc desc = {
521             .width = 4,
522             .height = 4,
523             .layers = 1,
524             .format = AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM,
525             .usage = AHARDWAREBUFFER_USAGE_CPU_READ_RARELY,
526     };
527     int err = AHardwareBuffer_allocate(&desc, &buffer1);
528     EXPECT_EQ(NO_ERROR, err);
529     EXPECT_NE(nullptr, buffer1);
530     EXPECT_EQ(0, AHardwareBuffer_getId(buffer1, &id1));
531     EXPECT_NE(id1, 0ULL);
532 
533     AHardwareBuffer* buffer2 = nullptr;
534     uint64_t id2 = 0;
535     err = AHardwareBuffer_allocate(&desc, &buffer2);
536     EXPECT_EQ(NO_ERROR, err);
537     EXPECT_NE(nullptr, buffer2);
538     EXPECT_EQ(0, AHardwareBuffer_getId(buffer2, &id2));
539     EXPECT_NE(id2, 0ULL);
540 
541     EXPECT_NE(id1, id2);
542 }
543 
544 } // namespace android
545