1 /*
2  * Copyright (C) 2017 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 #define LOG_TAG "AHardwareBuffer_test"
18 //#define LOG_NDEBUG 0
19 
20 #include <android/hardware_buffer.h>
21 #include <private/android/AHardwareBufferHelpers.h>
22 #include <android/hardware/graphics/common/1.0/types.h>
23 #include <vndk/hardware_buffer.h>
24 
25 #include <gtest/gtest.h>
26 
27 using namespace android;
28 using android::hardware::graphics::common::V1_0::BufferUsage;
29 
BuildHexFailureMessage(uint64_t expected,uint64_t actual,const char * type)30 static ::testing::AssertionResult BuildHexFailureMessage(uint64_t expected,
31         uint64_t actual, const char* type) {
32     std::ostringstream ss;
33     ss << type << " 0x" << std::hex << actual
34             << " does not match expected " << type << " 0x" << std::hex
35             << expected;
36     return ::testing::AssertionFailure() << ss.str();
37 }
38 
TestUsageConversion(uint64_t grallocUsage,uint64_t hardwareBufferUsage)39 static ::testing::AssertionResult TestUsageConversion(
40         uint64_t grallocUsage, uint64_t hardwareBufferUsage) {
41     uint64_t convertedGrallocUsage = AHardwareBuffer_convertToGrallocUsageBits(hardwareBufferUsage);
42     if (convertedGrallocUsage != grallocUsage)
43         return BuildHexFailureMessage(grallocUsage, convertedGrallocUsage, "converToGralloc");
44 
45     uint64_t convertedHArdwareBufferUsage = AHardwareBuffer_convertFromGrallocUsageBits(grallocUsage);
46     if (convertedHArdwareBufferUsage != grallocUsage)
47         return BuildHexFailureMessage(grallocUsage, convertedHArdwareBufferUsage, "convertFromGralloc");
48 
49     return testing::AssertionSuccess();
50 }
51 
52 // This is a unit test rather than going through AHardwareBuffer because not
53 // all flags may be supported by the host device.
TEST(AHardwareBufferTest,ConvertToAndFromGrallocBits)54 TEST(AHardwareBufferTest, ConvertToAndFromGrallocBits) {
55     EXPECT_TRUE(TestUsageConversion((uint64_t)BufferUsage::CPU_READ_RARELY,
56             AHARDWAREBUFFER_USAGE_CPU_READ_RARELY));
57     EXPECT_TRUE(TestUsageConversion((uint64_t)BufferUsage::CPU_READ_OFTEN,
58             AHARDWAREBUFFER_USAGE_CPU_READ_OFTEN));
59     EXPECT_TRUE(TestUsageConversion((uint64_t)BufferUsage::CPU_WRITE_RARELY,
60             AHARDWAREBUFFER_USAGE_CPU_WRITE_RARELY));
61     EXPECT_TRUE(TestUsageConversion((uint64_t)BufferUsage::CPU_WRITE_OFTEN,
62             AHARDWAREBUFFER_USAGE_CPU_WRITE_OFTEN));
63 
64     EXPECT_TRUE(TestUsageConversion((uint64_t)BufferUsage::GPU_TEXTURE,
65             AHARDWAREBUFFER_USAGE_GPU_SAMPLED_IMAGE));
66     EXPECT_TRUE(TestUsageConversion((uint64_t)BufferUsage::GPU_RENDER_TARGET,
67             AHARDWAREBUFFER_USAGE_GPU_COLOR_OUTPUT));
68     EXPECT_TRUE(TestUsageConversion((uint64_t)BufferUsage::GPU_DATA_BUFFER,
69             AHARDWAREBUFFER_USAGE_GPU_DATA_BUFFER));
70     EXPECT_TRUE(TestUsageConversion((uint64_t)BufferUsage::PROTECTED,
71             AHARDWAREBUFFER_USAGE_PROTECTED_CONTENT));
72     EXPECT_TRUE(TestUsageConversion((uint64_t)BufferUsage::SENSOR_DIRECT_DATA,
73             AHARDWAREBUFFER_USAGE_SENSOR_DIRECT_DATA));
74     EXPECT_TRUE(TestUsageConversion((uint64_t)BufferUsage::VIDEO_ENCODER,
75             AHARDWAREBUFFER_USAGE_VIDEO_ENCODE));
76 
77     EXPECT_TRUE(TestUsageConversion(1ull<<28, AHARDWAREBUFFER_USAGE_VENDOR_0));
78     EXPECT_TRUE(TestUsageConversion(1ull<<29, AHARDWAREBUFFER_USAGE_VENDOR_1));
79     EXPECT_TRUE(TestUsageConversion(1ull<<30, AHARDWAREBUFFER_USAGE_VENDOR_2));
80     EXPECT_TRUE(TestUsageConversion(1ull<<31, AHARDWAREBUFFER_USAGE_VENDOR_3));
81     EXPECT_TRUE(TestUsageConversion(1ull<<48, AHARDWAREBUFFER_USAGE_VENDOR_4));
82     EXPECT_TRUE(TestUsageConversion(1ull<<49, AHARDWAREBUFFER_USAGE_VENDOR_5));
83     EXPECT_TRUE(TestUsageConversion(1ull<<50, AHARDWAREBUFFER_USAGE_VENDOR_6));
84     EXPECT_TRUE(TestUsageConversion(1ull<<51, AHARDWAREBUFFER_USAGE_VENDOR_7));
85     EXPECT_TRUE(TestUsageConversion(1ull<<52, AHARDWAREBUFFER_USAGE_VENDOR_8));
86     EXPECT_TRUE(TestUsageConversion(1ull<<53, AHARDWAREBUFFER_USAGE_VENDOR_9));
87     EXPECT_TRUE(TestUsageConversion(1ull<<54, AHARDWAREBUFFER_USAGE_VENDOR_10));
88     EXPECT_TRUE(TestUsageConversion(1ull<<55, AHARDWAREBUFFER_USAGE_VENDOR_11));
89     EXPECT_TRUE(TestUsageConversion(1ull<<56, AHARDWAREBUFFER_USAGE_VENDOR_12));
90     EXPECT_TRUE(TestUsageConversion(1ull<<57, AHARDWAREBUFFER_USAGE_VENDOR_13));
91     EXPECT_TRUE(TestUsageConversion(1ull<<58, AHARDWAREBUFFER_USAGE_VENDOR_14));
92     EXPECT_TRUE(TestUsageConversion(1ull<<59, AHARDWAREBUFFER_USAGE_VENDOR_15));
93     EXPECT_TRUE(TestUsageConversion(1ull<<60, AHARDWAREBUFFER_USAGE_VENDOR_16));
94     EXPECT_TRUE(TestUsageConversion(1ull<<61, AHARDWAREBUFFER_USAGE_VENDOR_17));
95     EXPECT_TRUE(TestUsageConversion(1ull<<62, AHARDWAREBUFFER_USAGE_VENDOR_18));
96     EXPECT_TRUE(TestUsageConversion(1ull<<63, AHARDWAREBUFFER_USAGE_VENDOR_19));
97 
98     // Test some more complex flag combinations.
99     EXPECT_TRUE(TestUsageConversion(
100             (uint64_t)BufferUsage::CPU_READ_RARELY |
101             (uint64_t)BufferUsage::CPU_WRITE_RARELY,
102             AHARDWAREBUFFER_USAGE_CPU_READ_RARELY | AHARDWAREBUFFER_USAGE_CPU_WRITE_RARELY));
103 
104     EXPECT_TRUE(TestUsageConversion(
105         (uint64_t)BufferUsage::GPU_RENDER_TARGET | (uint64_t)BufferUsage::GPU_TEXTURE |
106         1ull << 29 | 1ull << 57,
107         AHARDWAREBUFFER_USAGE_GPU_COLOR_OUTPUT | AHARDWAREBUFFER_USAGE_GPU_SAMPLED_IMAGE |
108         AHARDWAREBUFFER_USAGE_VENDOR_1 | AHARDWAREBUFFER_USAGE_VENDOR_13));
109 }
110 
TEST(AHardwareBufferTest,GetCreateHandleTest)111 TEST(AHardwareBufferTest, GetCreateHandleTest) {
112     AHardwareBuffer_Desc desc{
113             .width = 64,
114             .height = 1,
115             .layers = 1,
116             .format = AHARDWAREBUFFER_FORMAT_BLOB,
117             .usage = AHARDWAREBUFFER_USAGE_CPU_READ_OFTEN | AHARDWAREBUFFER_USAGE_CPU_WRITE_OFTEN,
118             .stride = 64,
119     };
120 
121     AHardwareBuffer* buffer = nullptr;
122     EXPECT_EQ(0, AHardwareBuffer_allocate(&desc, &buffer));
123     const native_handle_t* handle = AHardwareBuffer_getNativeHandle(buffer);
124     EXPECT_NE(nullptr, handle);
125 
126     AHardwareBuffer* otherBuffer = nullptr;
127     EXPECT_EQ(0, AHardwareBuffer_createFromHandle(
128         &desc, handle, AHARDWAREBUFFER_CREATE_FROM_HANDLE_METHOD_CLONE, &otherBuffer));
129     EXPECT_NE(nullptr, otherBuffer);
130 
131     AHardwareBuffer_release(buffer);
132     AHardwareBuffer_release(otherBuffer);
133 }
134