1 /*
2 * Copyright 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 #define LOG_TAG "GraphicBufferTest"
18
19 #include <ui/BufferHubBuffer.h>
20 #include <ui/GraphicBuffer.h>
21
22 #include <gtest/gtest.h>
23
24 namespace android {
25
26 namespace {
27
28 constexpr uint32_t kTestWidth = 1024;
29 constexpr uint32_t kTestHeight = 1;
30 constexpr uint32_t kTestFormat = HAL_PIXEL_FORMAT_BLOB;
31 constexpr uint32_t kTestLayerCount = 1;
32 constexpr uint64_t kTestUsage = GraphicBuffer::USAGE_SW_WRITE_OFTEN;
33
34 } // namespace
35
36 class GraphicBufferTest : public testing::Test {};
37
TEST_F(GraphicBufferTest,AllocateNoError)38 TEST_F(GraphicBufferTest, AllocateNoError) {
39 PixelFormat format = PIXEL_FORMAT_RGBA_8888;
40 sp<GraphicBuffer> gb(new GraphicBuffer(kTestWidth, kTestHeight, format, kTestLayerCount,
41 kTestUsage, std::string("test")));
42 ASSERT_EQ(NO_ERROR, gb->initCheck());
43 }
44
TEST_F(GraphicBufferTest,AllocateBadDimensions)45 TEST_F(GraphicBufferTest, AllocateBadDimensions) {
46 PixelFormat format = PIXEL_FORMAT_RGBA_8888;
47 uint32_t width, height;
48 width = height = std::numeric_limits<uint32_t>::max();
49 sp<GraphicBuffer> gb(new GraphicBuffer(width, height, format, kTestLayerCount, kTestUsage,
50 std::string("test")));
51 ASSERT_EQ(BAD_VALUE, gb->initCheck());
52 }
53
TEST_F(GraphicBufferTest,CreateFromBufferHubBuffer)54 TEST_F(GraphicBufferTest, CreateFromBufferHubBuffer) {
55 std::unique_ptr<BufferHubBuffer> b1 =
56 BufferHubBuffer::create(kTestWidth, kTestHeight, kTestLayerCount, kTestFormat,
57 kTestUsage, /*userMetadataSize=*/0);
58 ASSERT_NE(b1, nullptr);
59 EXPECT_TRUE(b1->isValid());
60
61 sp<GraphicBuffer> gb(new GraphicBuffer(std::move(b1)));
62 EXPECT_TRUE(gb->isBufferHubBuffer());
63
64 EXPECT_EQ(gb->getWidth(), kTestWidth);
65 EXPECT_EQ(gb->getHeight(), kTestHeight);
66 EXPECT_EQ(static_cast<uint32_t>(gb->getPixelFormat()), kTestFormat);
67 EXPECT_EQ(gb->getUsage(), kTestUsage);
68 EXPECT_EQ(gb->getLayerCount(), kTestLayerCount);
69 }
70
TEST_F(GraphicBufferTest,InvalidBufferIdForNoneBufferHubBuffer)71 TEST_F(GraphicBufferTest, InvalidBufferIdForNoneBufferHubBuffer) {
72 sp<GraphicBuffer> gb(
73 new GraphicBuffer(kTestWidth, kTestHeight, kTestFormat, kTestLayerCount, kTestUsage));
74 EXPECT_FALSE(gb->isBufferHubBuffer());
75 EXPECT_EQ(gb->getBufferId(), -1);
76 }
77
TEST_F(GraphicBufferTest,BufferIdMatchesBufferHubBufferId)78 TEST_F(GraphicBufferTest, BufferIdMatchesBufferHubBufferId) {
79 std::unique_ptr<BufferHubBuffer> b1 =
80 BufferHubBuffer::create(kTestWidth, kTestHeight, kTestLayerCount, kTestFormat,
81 kTestUsage, /*userMetadataSize=*/0);
82 EXPECT_NE(b1, nullptr);
83 EXPECT_TRUE(b1->isValid());
84
85 int b1_id = b1->id();
86 EXPECT_GE(b1_id, 0);
87
88 sp<GraphicBuffer> gb(new GraphicBuffer(std::move(b1)));
89 EXPECT_TRUE(gb->isBufferHubBuffer());
90 EXPECT_EQ(gb->getBufferId(), b1_id);
91 }
92
TEST_F(GraphicBufferTest,flattenAndUnflatten)93 TEST_F(GraphicBufferTest, flattenAndUnflatten) {
94 std::unique_ptr<BufferHubBuffer> b1 =
95 BufferHubBuffer::create(kTestWidth, kTestHeight, kTestLayerCount, kTestFormat,
96 kTestUsage, /*userMetadataSize=*/0);
97 ASSERT_NE(b1, nullptr);
98 sp<GraphicBuffer> gb1(new GraphicBuffer(std::move(b1)));
99 gb1->setGenerationNumber(42);
100
101 size_t flattenedSize = gb1->getFlattenedSize();
102 EXPECT_EQ(flattenedSize, 48);
103 size_t fdCount = gb1->getFdCount();
104 EXPECT_EQ(fdCount, 0);
105
106 int data[flattenedSize];
107 int fds[0];
108
109 // Make copies of needed items since flatten modifies them.
110 size_t flattenedSizeCopy = flattenedSize;
111 size_t fdCountCopy = fdCount;
112 void* dataStart = data;
113 int* fdsStart = fds;
114 status_t err = gb1->flatten(dataStart, flattenedSizeCopy, fdsStart, fdCountCopy);
115 ASSERT_EQ(err, NO_ERROR);
116 EXPECT_EQ(flattenedSizeCopy, 0);
117 EXPECT_EQ(fdCountCopy, 0);
118
119 size_t unflattenSize = flattenedSize;
120 size_t unflattenFdCount = fdCount;
121 const void* unflattenData = static_cast<const void*>(dataStart);
122 const int* unflattenFdData = static_cast<const int*>(fdsStart);
123
124 GraphicBuffer* gb2 = new GraphicBuffer();
125 err = gb2->unflatten(unflattenData, unflattenSize, unflattenFdData, unflattenFdCount);
126 ASSERT_EQ(err, NO_ERROR);
127 EXPECT_TRUE(gb2->isBufferHubBuffer());
128
129 EXPECT_EQ(gb2->getWidth(), kTestWidth);
130 EXPECT_EQ(gb2->getHeight(), kTestHeight);
131 EXPECT_EQ(static_cast<uint32_t>(gb2->getPixelFormat()), kTestFormat);
132 EXPECT_EQ(gb2->getUsage(), kTestUsage);
133 EXPECT_EQ(gb2->getLayerCount(), kTestLayerCount);
134 EXPECT_EQ(gb1->getBufferId(), gb2->getBufferId());
135 EXPECT_EQ(gb2->getGenerationNumber(), 42);
136 }
137
138 } // namespace android
139