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,CreateFromBufferHubBuffer)38 TEST_F(GraphicBufferTest, CreateFromBufferHubBuffer) {
39 std::unique_ptr<BufferHubBuffer> b1 =
40 BufferHubBuffer::create(kTestWidth, kTestHeight, kTestLayerCount, kTestFormat,
41 kTestUsage, /*userMetadataSize=*/0);
42 ASSERT_NE(b1, nullptr);
43 EXPECT_TRUE(b1->isValid());
44
45 sp<GraphicBuffer> gb(new GraphicBuffer(std::move(b1)));
46 EXPECT_TRUE(gb->isBufferHubBuffer());
47
48 EXPECT_EQ(gb->getWidth(), kTestWidth);
49 EXPECT_EQ(gb->getHeight(), kTestHeight);
50 EXPECT_EQ(static_cast<uint32_t>(gb->getPixelFormat()), kTestFormat);
51 EXPECT_EQ(gb->getUsage(), kTestUsage);
52 EXPECT_EQ(gb->getLayerCount(), kTestLayerCount);
53 }
54
TEST_F(GraphicBufferTest,InvalidBufferIdForNoneBufferHubBuffer)55 TEST_F(GraphicBufferTest, InvalidBufferIdForNoneBufferHubBuffer) {
56 sp<GraphicBuffer> gb(
57 new GraphicBuffer(kTestWidth, kTestHeight, kTestFormat, kTestLayerCount, kTestUsage));
58 EXPECT_FALSE(gb->isBufferHubBuffer());
59 EXPECT_EQ(gb->getBufferId(), -1);
60 }
61
TEST_F(GraphicBufferTest,BufferIdMatchesBufferHubBufferId)62 TEST_F(GraphicBufferTest, BufferIdMatchesBufferHubBufferId) {
63 std::unique_ptr<BufferHubBuffer> b1 =
64 BufferHubBuffer::create(kTestWidth, kTestHeight, kTestLayerCount, kTestFormat,
65 kTestUsage, /*userMetadataSize=*/0);
66 EXPECT_NE(b1, nullptr);
67 EXPECT_TRUE(b1->isValid());
68
69 int b1_id = b1->id();
70 EXPECT_GE(b1_id, 0);
71
72 sp<GraphicBuffer> gb(new GraphicBuffer(std::move(b1)));
73 EXPECT_TRUE(gb->isBufferHubBuffer());
74 EXPECT_EQ(gb->getBufferId(), b1_id);
75 }
76
TEST_F(GraphicBufferTest,flattenAndUnflatten)77 TEST_F(GraphicBufferTest, flattenAndUnflatten) {
78 std::unique_ptr<BufferHubBuffer> b1 =
79 BufferHubBuffer::create(kTestWidth, kTestHeight, kTestLayerCount, kTestFormat,
80 kTestUsage, /*userMetadataSize=*/0);
81 ASSERT_NE(b1, nullptr);
82 sp<GraphicBuffer> gb1(new GraphicBuffer(std::move(b1)));
83 gb1->setGenerationNumber(42);
84
85 size_t flattenedSize = gb1->getFlattenedSize();
86 EXPECT_EQ(flattenedSize, 48);
87 size_t fdCount = gb1->getFdCount();
88 EXPECT_EQ(fdCount, 0);
89
90 int data[flattenedSize];
91 int fds[0];
92
93 // Make copies of needed items since flatten modifies them.
94 size_t flattenedSizeCopy = flattenedSize;
95 size_t fdCountCopy = fdCount;
96 void* dataStart = data;
97 int* fdsStart = fds;
98 status_t err = gb1->flatten(dataStart, flattenedSizeCopy, fdsStart, fdCountCopy);
99 ASSERT_EQ(err, NO_ERROR);
100 EXPECT_EQ(flattenedSizeCopy, 0);
101 EXPECT_EQ(fdCountCopy, 0);
102
103 size_t unflattenSize = flattenedSize;
104 size_t unflattenFdCount = fdCount;
105 const void* unflattenData = static_cast<const void*>(dataStart);
106 const int* unflattenFdData = static_cast<const int*>(fdsStart);
107
108 GraphicBuffer* gb2 = new GraphicBuffer();
109 err = gb2->unflatten(unflattenData, unflattenSize, unflattenFdData, unflattenFdCount);
110 ASSERT_EQ(err, NO_ERROR);
111 EXPECT_TRUE(gb2->isBufferHubBuffer());
112
113 EXPECT_EQ(gb2->getWidth(), kTestWidth);
114 EXPECT_EQ(gb2->getHeight(), kTestHeight);
115 EXPECT_EQ(static_cast<uint32_t>(gb2->getPixelFormat()), kTestFormat);
116 EXPECT_EQ(gb2->getUsage(), kTestUsage);
117 EXPECT_EQ(gb2->getLayerCount(), kTestLayerCount);
118 EXPECT_EQ(gb1->getBufferId(), gb2->getBufferId());
119 EXPECT_EQ(gb2->getGenerationNumber(), 42);
120 }
121
122 } // namespace android
123