• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
24 #include <gtest/gtest.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