blob: ff229132260cdee7c03fbe1babd8621d6d58fce4 [file] [log] [blame]
Valerie Hauc5011f92019-10-11 09:52:07 -07001/*
2 * Copyright (C) 2019 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 "BLASTBufferQueue_test"
18
19#include <gui/BLASTBufferQueue.h>
20
Valerie Hauda3446e2019-10-14 15:49:22 -070021#include <android/hardware/graphics/common/1.2/types.h>
22#include <gui/IGraphicBufferProducer.h>
23#include <gui/IProducerListener.h>
Valerie Hauc5011f92019-10-11 09:52:07 -070024#include <gui/SurfaceComposerClient.h>
Valerie Hauda3446e2019-10-14 15:49:22 -070025#include <private/gui/ComposerService.h>
26#include <ui/DisplayInfo.h>
Valerie Hauc5011f92019-10-11 09:52:07 -070027#include <ui/GraphicBuffer.h>
Valerie Hauda3446e2019-10-14 15:49:22 -070028#include <ui/GraphicTypes.h>
Valerie Hau8cee3f92019-11-06 10:06:28 -080029#include <ui/Transform.h>
Valerie Hauc5011f92019-10-11 09:52:07 -070030
31#include <gtest/gtest.h>
32
33using namespace std::chrono_literals;
34
35namespace android {
36
Valerie Hauc5011f92019-10-11 09:52:07 -070037using Transaction = SurfaceComposerClient::Transaction;
Valerie Hauda3446e2019-10-14 15:49:22 -070038using android::hardware::graphics::common::V1_2::BufferUsage;
Valerie Hauc5011f92019-10-11 09:52:07 -070039
40class BLASTBufferQueueHelper {
41public:
42 BLASTBufferQueueHelper(const sp<SurfaceControl>& sc, int width, int height) {
43 mBlastBufferQueueAdapter = new BLASTBufferQueue(sc, width, height);
44 }
45
46 void update(const sp<SurfaceControl>& sc, int width, int height) {
47 mBlastBufferQueueAdapter->update(sc, width, height);
48 }
49
50 void setNextTransaction(Transaction* next) {
51 mBlastBufferQueueAdapter->setNextTransaction(next);
52 }
53
54 int getWidth() { return mBlastBufferQueueAdapter->mWidth; }
Valerie Hauda3446e2019-10-14 15:49:22 -070055
Valerie Hauc5011f92019-10-11 09:52:07 -070056 int getHeight() { return mBlastBufferQueueAdapter->mHeight; }
Valerie Hauda3446e2019-10-14 15:49:22 -070057
Valerie Hauc5011f92019-10-11 09:52:07 -070058 Transaction* getNextTransaction() { return mBlastBufferQueueAdapter->mNextTransaction; }
Valerie Hauda3446e2019-10-14 15:49:22 -070059
60 sp<IGraphicBufferProducer> getIGraphicBufferProducer() {
61 return mBlastBufferQueueAdapter->getIGraphicBufferProducer();
62 }
63
Valerie Hauc5011f92019-10-11 09:52:07 -070064 const sp<SurfaceControl> getSurfaceControl() {
65 return mBlastBufferQueueAdapter->mSurfaceControl;
66 }
67
Valerie Hauda3446e2019-10-14 15:49:22 -070068 void waitForCallback() {
69 std::unique_lock lock{mBlastBufferQueueAdapter->mMutex};
70 mBlastBufferQueueAdapter->mDequeueWaitCV.wait_for(lock, 1s);
71 }
72
Valerie Hauc5011f92019-10-11 09:52:07 -070073private:
74 sp<BLASTBufferQueue> mBlastBufferQueueAdapter;
75};
76
77class BLASTBufferQueueTest : public ::testing::Test {
78public:
79protected:
80 BLASTBufferQueueTest() {
81 const ::testing::TestInfo* const testInfo =
82 ::testing::UnitTest::GetInstance()->current_test_info();
83 ALOGV("Begin test: %s.%s", testInfo->test_case_name(), testInfo->name());
84 }
85
86 ~BLASTBufferQueueTest() {
87 const ::testing::TestInfo* const testInfo =
88 ::testing::UnitTest::GetInstance()->current_test_info();
89 ALOGV("End test: %s.%s", testInfo->test_case_name(), testInfo->name());
90 }
91
92 void SetUp() {
Valerie Hauda3446e2019-10-14 15:49:22 -070093 mComposer = ComposerService::getComposerService();
Valerie Hauc5011f92019-10-11 09:52:07 -070094 mClient = new SurfaceComposerClient();
Valerie Hauda3446e2019-10-14 15:49:22 -070095 mDisplayToken = mClient->getInternalDisplayToken();
96 ASSERT_NE(nullptr, mDisplayToken.get());
97 Transaction t;
98 t.setDisplayLayerStack(mDisplayToken, 0);
99 t.apply();
100 t.clear();
101
102 DisplayInfo info;
103 ASSERT_EQ(NO_ERROR, SurfaceComposerClient::getDisplayInfo(mDisplayToken, &info));
104 mDisplayWidth = info.w;
105 mDisplayHeight = info.h;
106
107 mSurfaceControl = mClient->createSurface(String8("TestSurface"), mDisplayWidth,
108 mDisplayHeight, PIXEL_FORMAT_RGBA_8888,
109 ISurfaceComposerClient::eFXSurfaceBufferState,
110 /*parent*/ nullptr);
111 t.setLayerStack(mSurfaceControl, 0)
112 .setLayer(mSurfaceControl, std::numeric_limits<int32_t>::max())
113 .setFrame(mSurfaceControl, Rect(0, 0, mDisplayWidth, mDisplayHeight))
114 .show(mSurfaceControl)
115 .setDataspace(mSurfaceControl, ui::Dataspace::V0_SRGB)
116 .apply();
117 }
118
119 void fillBuffer(uint32_t* bufData, uint32_t width, uint32_t height, uint32_t stride, uint8_t r,
120 uint8_t g, uint8_t b) {
121 for (uint32_t row = 0; row < height; row++) {
122 for (uint32_t col = 0; col < width; col++) {
123 uint8_t* pixel = (uint8_t*)(bufData + (row * stride) + col);
124 *pixel = r;
125 *(pixel + 1) = g;
126 *(pixel + 2) = b;
127 *(pixel + 3) = 255;
128 }
129 }
130 }
131
132 void checkScreenCapture(uint8_t r, uint8_t g, uint8_t b) {
133 const auto width = mScreenCaptureBuf->getWidth();
134 const auto height = mScreenCaptureBuf->getHeight();
135 const auto stride = mScreenCaptureBuf->getStride();
136
137 uint32_t* bufData;
138 mScreenCaptureBuf->lock(static_cast<uint32_t>(GraphicBuffer::USAGE_SW_READ_OFTEN),
139 reinterpret_cast<void**>(&bufData));
140
141 for (uint32_t row = 0; row < height; row++) {
142 for (uint32_t col = 0; col < width; col++) {
143 uint8_t* pixel = (uint8_t*)(bufData + (row * stride) + col);
144 EXPECT_EQ(r, *(pixel));
145 EXPECT_EQ(g, *(pixel + 1));
146 EXPECT_EQ(b, *(pixel + 2));
147 }
148 }
149 mScreenCaptureBuf->unlock();
150 ASSERT_EQ(false, ::testing::Test::HasFailure());
Valerie Hauc5011f92019-10-11 09:52:07 -0700151 }
152
153 sp<SurfaceComposerClient> mClient;
Valerie Hauda3446e2019-10-14 15:49:22 -0700154 sp<ISurfaceComposer> mComposer;
155
156 sp<IBinder> mDisplayToken;
157
Valerie Hauc5011f92019-10-11 09:52:07 -0700158 sp<SurfaceControl> mSurfaceControl;
Valerie Hauda3446e2019-10-14 15:49:22 -0700159 sp<GraphicBuffer> mScreenCaptureBuf;
160
161 uint32_t mDisplayWidth;
162 uint32_t mDisplayHeight;
Valerie Hauc5011f92019-10-11 09:52:07 -0700163};
164
165TEST_F(BLASTBufferQueueTest, CreateBLASTBufferQueue) {
166 // create BLASTBufferQueue adapter associated with this surface
Valerie Hauda3446e2019-10-14 15:49:22 -0700167 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
Valerie Hauc5011f92019-10-11 09:52:07 -0700168 ASSERT_EQ(mSurfaceControl, adapter.getSurfaceControl());
Valerie Hauda3446e2019-10-14 15:49:22 -0700169 ASSERT_EQ(mDisplayWidth, adapter.getWidth());
170 ASSERT_EQ(mDisplayHeight, adapter.getHeight());
Valerie Hauc5011f92019-10-11 09:52:07 -0700171 ASSERT_EQ(nullptr, adapter.getNextTransaction());
172}
173
174TEST_F(BLASTBufferQueueTest, Update) {
Valerie Hauda3446e2019-10-14 15:49:22 -0700175 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
Valerie Hauc5011f92019-10-11 09:52:07 -0700176 sp<SurfaceControl> updateSurface =
Valerie Hauda3446e2019-10-14 15:49:22 -0700177 mClient->createSurface(String8("UpdateTest"), mDisplayWidth / 2, mDisplayHeight / 2,
178 PIXEL_FORMAT_RGBA_8888);
179 adapter.update(updateSurface, mDisplayWidth / 2, mDisplayHeight / 2);
Valerie Hauc5011f92019-10-11 09:52:07 -0700180 ASSERT_EQ(updateSurface, adapter.getSurfaceControl());
Valerie Hauda3446e2019-10-14 15:49:22 -0700181 ASSERT_EQ(mDisplayWidth / 2, adapter.getWidth());
182 ASSERT_EQ(mDisplayHeight / 2, adapter.getHeight());
Valerie Hauc5011f92019-10-11 09:52:07 -0700183}
184
185TEST_F(BLASTBufferQueueTest, SetNextTransaction) {
Valerie Hauda3446e2019-10-14 15:49:22 -0700186 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
Valerie Hauc5011f92019-10-11 09:52:07 -0700187 Transaction next;
188 adapter.setNextTransaction(&next);
189 ASSERT_EQ(&next, adapter.getNextTransaction());
190}
Valerie Hauda3446e2019-10-14 15:49:22 -0700191
192TEST_F(BLASTBufferQueueTest, onFrameAvailable_Apply) {
193 uint8_t r = 255;
194 uint8_t g = 0;
195 uint8_t b = 0;
196
197 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
198 auto igbProducer = adapter.getIGraphicBufferProducer();
199 ASSERT_NE(nullptr, igbProducer.get());
200 IGraphicBufferProducer::QueueBufferOutput qbOutput;
201 ASSERT_EQ(NO_ERROR,
202 igbProducer->connect(new DummyProducerListener, NATIVE_WINDOW_API_CPU, false,
203 &qbOutput));
204 ASSERT_EQ(NO_ERROR, igbProducer->setMaxDequeuedBufferCount(3));
Valerie Hau8cee3f92019-11-06 10:06:28 -0800205 ASSERT_NE(ui::Transform::orientation_flags::ROT_INVALID, qbOutput.transformHint);
Valerie Hauda3446e2019-10-14 15:49:22 -0700206
207 int slot;
208 sp<Fence> fence;
209 sp<GraphicBuffer> buf;
210 auto ret = igbProducer->dequeueBuffer(&slot, &fence, mDisplayWidth, mDisplayHeight,
211 PIXEL_FORMAT_RGBA_8888, GRALLOC_USAGE_SW_WRITE_OFTEN,
212 nullptr, nullptr);
213 ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, ret);
214 ASSERT_EQ(OK, igbProducer->requestBuffer(slot, &buf));
215
216 uint32_t* bufData;
217 buf->lock(static_cast<uint32_t>(GraphicBuffer::USAGE_SW_WRITE_OFTEN),
218 reinterpret_cast<void**>(&bufData));
219 fillBuffer(bufData, buf->getWidth(), buf->getHeight(), buf->getStride(), r, g, b);
220 buf->unlock();
221
222 IGraphicBufferProducer::QueueBufferInput input(systemTime(), false, HAL_DATASPACE_UNKNOWN,
223 Rect(mDisplayWidth, mDisplayHeight),
224 NATIVE_WINDOW_SCALING_MODE_FREEZE, 0,
225 Fence::NO_FENCE);
226 igbProducer->queueBuffer(slot, input, &qbOutput);
Valerie Hau8cee3f92019-11-06 10:06:28 -0800227 ASSERT_NE(ui::Transform::orientation_flags::ROT_INVALID, qbOutput.transformHint);
Valerie Hauda3446e2019-10-14 15:49:22 -0700228
229 adapter.waitForCallback();
230
231 // capture screen and verify that it is red
232 bool capturedSecureLayers;
233 ASSERT_EQ(NO_ERROR,
234 mComposer->captureScreen(mDisplayToken, &mScreenCaptureBuf, capturedSecureLayers,
235 ui::Dataspace::V0_SRGB, ui::PixelFormat::RGBA_8888, Rect(),
236 mDisplayWidth, mDisplayHeight,
237 /*useIdentityTransform*/ false));
238 ASSERT_NO_FATAL_FAILURE(checkScreenCapture(r, g, b));
239}
Valerie Hauc5011f92019-10-11 09:52:07 -0700240} // namespace android