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/graphics/common/1.0/types.h>
21 #include <gtest/gtest.h>
22 #include <private/android/AHardwareBufferHelpers.h>
23 #include <ui/GraphicBuffer.h>
24 #include <vndk/hardware_buffer.h>
25
26 using namespace android;
27 using android::hardware::graphics::common::V1_0::BufferUsage;
28
BuildHexFailureMessage(uint64_t expected,uint64_t actual,const char * type)29 static ::testing::AssertionResult BuildHexFailureMessage(uint64_t expected,
30 uint64_t actual, const char* type) {
31 std::ostringstream ss;
32 ss << type << " 0x" << std::hex << actual
33 << " does not match expected " << type << " 0x" << std::hex
34 << expected;
35 return ::testing::AssertionFailure() << ss.str();
36 }
37
TestUsageConversion(uint64_t grallocUsage,uint64_t hardwareBufferUsage)38 static ::testing::AssertionResult TestUsageConversion(
39 uint64_t grallocUsage, uint64_t hardwareBufferUsage) {
40 uint64_t convertedGrallocUsage = AHardwareBuffer_convertToGrallocUsageBits(hardwareBufferUsage);
41 if (convertedGrallocUsage != grallocUsage)
42 return BuildHexFailureMessage(grallocUsage, convertedGrallocUsage, "converToGralloc");
43
44 uint64_t convertedHArdwareBufferUsage = AHardwareBuffer_convertFromGrallocUsageBits(grallocUsage);
45 if (convertedHArdwareBufferUsage != grallocUsage)
46 return BuildHexFailureMessage(grallocUsage, convertedHArdwareBufferUsage, "convertFromGralloc");
47
48 return testing::AssertionSuccess();
49 }
50
51 // This is a unit test rather than going through AHardwareBuffer because not
52 // all flags may be supported by the host device.
TEST(AHardwareBufferTest,ConvertToAndFromGrallocBits)53 TEST(AHardwareBufferTest, ConvertToAndFromGrallocBits) {
54 EXPECT_TRUE(TestUsageConversion((uint64_t)BufferUsage::CPU_READ_RARELY,
55 AHARDWAREBUFFER_USAGE_CPU_READ_RARELY));
56 EXPECT_TRUE(TestUsageConversion((uint64_t)BufferUsage::CPU_READ_OFTEN,
57 AHARDWAREBUFFER_USAGE_CPU_READ_OFTEN));
58 EXPECT_TRUE(TestUsageConversion((uint64_t)BufferUsage::CPU_WRITE_RARELY,
59 AHARDWAREBUFFER_USAGE_CPU_WRITE_RARELY));
60 EXPECT_TRUE(TestUsageConversion((uint64_t)BufferUsage::CPU_WRITE_OFTEN,
61 AHARDWAREBUFFER_USAGE_CPU_WRITE_OFTEN));
62
63 EXPECT_TRUE(TestUsageConversion((uint64_t)BufferUsage::GPU_TEXTURE,
64 AHARDWAREBUFFER_USAGE_GPU_SAMPLED_IMAGE));
65 EXPECT_TRUE(TestUsageConversion((uint64_t)BufferUsage::GPU_RENDER_TARGET,
66 AHARDWAREBUFFER_USAGE_GPU_COLOR_OUTPUT));
67 EXPECT_TRUE(TestUsageConversion((uint64_t)BufferUsage::GPU_DATA_BUFFER,
68 AHARDWAREBUFFER_USAGE_GPU_DATA_BUFFER));
69 EXPECT_TRUE(TestUsageConversion((uint64_t)BufferUsage::PROTECTED,
70 AHARDWAREBUFFER_USAGE_PROTECTED_CONTENT));
71 EXPECT_TRUE(TestUsageConversion((uint64_t)BufferUsage::SENSOR_DIRECT_DATA,
72 AHARDWAREBUFFER_USAGE_SENSOR_DIRECT_DATA));
73 EXPECT_TRUE(TestUsageConversion((uint64_t)BufferUsage::VIDEO_ENCODER,
74 AHARDWAREBUFFER_USAGE_VIDEO_ENCODE));
75
76 EXPECT_TRUE(TestUsageConversion(1ull<<28, AHARDWAREBUFFER_USAGE_VENDOR_0));
77 EXPECT_TRUE(TestUsageConversion(1ull<<29, AHARDWAREBUFFER_USAGE_VENDOR_1));
78 EXPECT_TRUE(TestUsageConversion(1ull<<30, AHARDWAREBUFFER_USAGE_VENDOR_2));
79 EXPECT_TRUE(TestUsageConversion(1ull<<31, AHARDWAREBUFFER_USAGE_VENDOR_3));
80 EXPECT_TRUE(TestUsageConversion(1ull<<48, AHARDWAREBUFFER_USAGE_VENDOR_4));
81 EXPECT_TRUE(TestUsageConversion(1ull<<49, AHARDWAREBUFFER_USAGE_VENDOR_5));
82 EXPECT_TRUE(TestUsageConversion(1ull<<50, AHARDWAREBUFFER_USAGE_VENDOR_6));
83 EXPECT_TRUE(TestUsageConversion(1ull<<51, AHARDWAREBUFFER_USAGE_VENDOR_7));
84 EXPECT_TRUE(TestUsageConversion(1ull<<52, AHARDWAREBUFFER_USAGE_VENDOR_8));
85 EXPECT_TRUE(TestUsageConversion(1ull<<53, AHARDWAREBUFFER_USAGE_VENDOR_9));
86 EXPECT_TRUE(TestUsageConversion(1ull<<54, AHARDWAREBUFFER_USAGE_VENDOR_10));
87 EXPECT_TRUE(TestUsageConversion(1ull<<55, AHARDWAREBUFFER_USAGE_VENDOR_11));
88 EXPECT_TRUE(TestUsageConversion(1ull<<56, AHARDWAREBUFFER_USAGE_VENDOR_12));
89 EXPECT_TRUE(TestUsageConversion(1ull<<57, AHARDWAREBUFFER_USAGE_VENDOR_13));
90 EXPECT_TRUE(TestUsageConversion(1ull<<58, AHARDWAREBUFFER_USAGE_VENDOR_14));
91 EXPECT_TRUE(TestUsageConversion(1ull<<59, AHARDWAREBUFFER_USAGE_VENDOR_15));
92 EXPECT_TRUE(TestUsageConversion(1ull<<60, AHARDWAREBUFFER_USAGE_VENDOR_16));
93 EXPECT_TRUE(TestUsageConversion(1ull<<61, AHARDWAREBUFFER_USAGE_VENDOR_17));
94 EXPECT_TRUE(TestUsageConversion(1ull<<62, AHARDWAREBUFFER_USAGE_VENDOR_18));
95 EXPECT_TRUE(TestUsageConversion(1ull<<63, AHARDWAREBUFFER_USAGE_VENDOR_19));
96
97 // Test some more complex flag combinations.
98 EXPECT_TRUE(TestUsageConversion(
99 (uint64_t)BufferUsage::CPU_READ_RARELY |
100 (uint64_t)BufferUsage::CPU_WRITE_RARELY,
101 AHARDWAREBUFFER_USAGE_CPU_READ_RARELY | AHARDWAREBUFFER_USAGE_CPU_WRITE_RARELY));
102
103 EXPECT_TRUE(TestUsageConversion(
104 (uint64_t)BufferUsage::GPU_RENDER_TARGET | (uint64_t)BufferUsage::GPU_TEXTURE |
105 1ull << 29 | 1ull << 57,
106 AHARDWAREBUFFER_USAGE_GPU_COLOR_OUTPUT | AHARDWAREBUFFER_USAGE_GPU_SAMPLED_IMAGE |
107 AHARDWAREBUFFER_USAGE_VENDOR_1 | AHARDWAREBUFFER_USAGE_VENDOR_13));
108 }
109
TEST(AHardwareBufferTest,GetCreateHandleTest)110 TEST(AHardwareBufferTest, GetCreateHandleTest) {
111 AHardwareBuffer_Desc desc{
112 .width = 64,
113 .height = 1,
114 .layers = 1,
115 .format = AHARDWAREBUFFER_FORMAT_BLOB,
116 .usage = AHARDWAREBUFFER_USAGE_CPU_READ_OFTEN | AHARDWAREBUFFER_USAGE_CPU_WRITE_OFTEN,
117 .stride = 64,
118 };
119
120 AHardwareBuffer* buffer = nullptr;
121 EXPECT_EQ(0, AHardwareBuffer_allocate(&desc, &buffer));
122 const native_handle_t* handle = AHardwareBuffer_getNativeHandle(buffer);
123 EXPECT_NE(nullptr, handle);
124
125 AHardwareBuffer* otherBuffer = nullptr;
126 EXPECT_EQ(0, AHardwareBuffer_createFromHandle(
127 &desc, handle, AHARDWAREBUFFER_CREATE_FROM_HANDLE_METHOD_CLONE, &otherBuffer));
128 EXPECT_NE(nullptr, otherBuffer);
129
130 AHardwareBuffer_release(buffer);
131 AHardwareBuffer_release(otherBuffer);
132 }
133
TEST(AHardwareBufferTest,GetIdTest)134 TEST(AHardwareBufferTest, GetIdTest) {
135 const uint32_t testWidth = 4;
136 const uint32_t testHeight = 4;
137 const uint32_t testLayers = 1;
138
139 AHardwareBuffer* ahb1 = nullptr;
140 uint64_t id1 = 0;
141 const AHardwareBuffer_Desc desc = {
142 .width = testWidth,
143 .height = testHeight,
144 .layers = testLayers,
145 .format = AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM,
146 .usage = AHARDWAREBUFFER_USAGE_CPU_READ_RARELY,
147 };
148 int res = AHardwareBuffer_allocate(&desc, &ahb1);
149 EXPECT_EQ(NO_ERROR, res);
150 EXPECT_NE(nullptr, ahb1);
151 EXPECT_EQ(0, AHardwareBuffer_getId(ahb1, &id1));
152 const GraphicBuffer* gb1 = AHardwareBuffer_to_GraphicBuffer(ahb1);
153 EXPECT_NE(nullptr, gb1);
154 EXPECT_EQ(id1, gb1->getId());
155 EXPECT_NE(id1, 0);
156
157 sp<GraphicBuffer> gb2(new GraphicBuffer(testWidth,
158 testHeight,
159 PIXEL_FORMAT_RGBA_8888,
160 testLayers,
161 GraphicBuffer::USAGE_SW_READ_RARELY,
162 std::string("test")));
163 EXPECT_NE(nullptr, gb2.get());
164 const AHardwareBuffer* ahb2 = AHardwareBuffer_from_GraphicBuffer(gb2.get());
165 EXPECT_NE(nullptr, ahb2);
166 uint64_t id2 = 0;
167 EXPECT_EQ(0, AHardwareBuffer_getId(ahb2, &id2));
168 EXPECT_EQ(id2, gb2->getId());
169 EXPECT_NE(id2, 0);
170
171 EXPECT_NE(id1, id2);
172 }
173