blob: ae6c5cff0cd531624183360f24d63b251af7ad7b [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>
Valerie Haud3b90d22019-11-06 09:37:31 -080022#include <gui/BufferQueueCore.h>
23#include <gui/BufferQueueProducer.h>
Valerie Hauda3446e2019-10-14 15:49:22 -070024#include <gui/IGraphicBufferProducer.h>
25#include <gui/IProducerListener.h>
Valerie Hauc5011f92019-10-11 09:52:07 -070026#include <gui/SurfaceComposerClient.h>
Valerie Hauda3446e2019-10-14 15:49:22 -070027#include <private/gui/ComposerService.h>
28#include <ui/DisplayInfo.h>
Valerie Hauc5011f92019-10-11 09:52:07 -070029#include <ui/GraphicBuffer.h>
Valerie Hauda3446e2019-10-14 15:49:22 -070030#include <ui/GraphicTypes.h>
Valerie Hau8cee3f92019-11-06 10:06:28 -080031#include <ui/Transform.h>
Valerie Hauc5011f92019-10-11 09:52:07 -070032
33#include <gtest/gtest.h>
34
35using namespace std::chrono_literals;
36
37namespace android {
38
Valerie Hauc5011f92019-10-11 09:52:07 -070039using Transaction = SurfaceComposerClient::Transaction;
Valerie Hauda3446e2019-10-14 15:49:22 -070040using android::hardware::graphics::common::V1_2::BufferUsage;
Valerie Hauc5011f92019-10-11 09:52:07 -070041
42class BLASTBufferQueueHelper {
43public:
44 BLASTBufferQueueHelper(const sp<SurfaceControl>& sc, int width, int height) {
45 mBlastBufferQueueAdapter = new BLASTBufferQueue(sc, width, height);
46 }
47
48 void update(const sp<SurfaceControl>& sc, int width, int height) {
49 mBlastBufferQueueAdapter->update(sc, width, height);
50 }
51
52 void setNextTransaction(Transaction* next) {
53 mBlastBufferQueueAdapter->setNextTransaction(next);
54 }
55
56 int getWidth() { return mBlastBufferQueueAdapter->mWidth; }
Valerie Hauda3446e2019-10-14 15:49:22 -070057
Valerie Hauc5011f92019-10-11 09:52:07 -070058 int getHeight() { return mBlastBufferQueueAdapter->mHeight; }
Valerie Hauda3446e2019-10-14 15:49:22 -070059
Valerie Hauc5011f92019-10-11 09:52:07 -070060 Transaction* getNextTransaction() { return mBlastBufferQueueAdapter->mNextTransaction; }
Valerie Hauda3446e2019-10-14 15:49:22 -070061
62 sp<IGraphicBufferProducer> getIGraphicBufferProducer() {
63 return mBlastBufferQueueAdapter->getIGraphicBufferProducer();
64 }
65
Valerie Hauc5011f92019-10-11 09:52:07 -070066 const sp<SurfaceControl> getSurfaceControl() {
67 return mBlastBufferQueueAdapter->mSurfaceControl;
68 }
69
Valerie Haud3b90d22019-11-06 09:37:31 -080070 void waitForCallbacks() {
Valerie Hauda3446e2019-10-14 15:49:22 -070071 std::unique_lock lock{mBlastBufferQueueAdapter->mMutex};
Valerie Haud3b90d22019-11-06 09:37:31 -080072 while (mBlastBufferQueueAdapter->mPendingCallbacks > 0) {
73 mBlastBufferQueueAdapter->mCallbackCV.wait(lock);
74 }
Valerie Hauda3446e2019-10-14 15:49:22 -070075 }
76
Valerie Hauc5011f92019-10-11 09:52:07 -070077private:
78 sp<BLASTBufferQueue> mBlastBufferQueueAdapter;
79};
80
81class BLASTBufferQueueTest : public ::testing::Test {
82public:
83protected:
84 BLASTBufferQueueTest() {
85 const ::testing::TestInfo* const testInfo =
86 ::testing::UnitTest::GetInstance()->current_test_info();
87 ALOGV("Begin test: %s.%s", testInfo->test_case_name(), testInfo->name());
88 }
89
90 ~BLASTBufferQueueTest() {
91 const ::testing::TestInfo* const testInfo =
92 ::testing::UnitTest::GetInstance()->current_test_info();
93 ALOGV("End test: %s.%s", testInfo->test_case_name(), testInfo->name());
94 }
95
96 void SetUp() {
Valerie Hauda3446e2019-10-14 15:49:22 -070097 mComposer = ComposerService::getComposerService();
Valerie Hauc5011f92019-10-11 09:52:07 -070098 mClient = new SurfaceComposerClient();
Valerie Hauda3446e2019-10-14 15:49:22 -070099 mDisplayToken = mClient->getInternalDisplayToken();
100 ASSERT_NE(nullptr, mDisplayToken.get());
101 Transaction t;
102 t.setDisplayLayerStack(mDisplayToken, 0);
103 t.apply();
104 t.clear();
105
106 DisplayInfo info;
107 ASSERT_EQ(NO_ERROR, SurfaceComposerClient::getDisplayInfo(mDisplayToken, &info));
108 mDisplayWidth = info.w;
109 mDisplayHeight = info.h;
110
111 mSurfaceControl = mClient->createSurface(String8("TestSurface"), mDisplayWidth,
112 mDisplayHeight, PIXEL_FORMAT_RGBA_8888,
113 ISurfaceComposerClient::eFXSurfaceBufferState,
114 /*parent*/ nullptr);
115 t.setLayerStack(mSurfaceControl, 0)
116 .setLayer(mSurfaceControl, std::numeric_limits<int32_t>::max())
117 .setFrame(mSurfaceControl, Rect(0, 0, mDisplayWidth, mDisplayHeight))
118 .show(mSurfaceControl)
119 .setDataspace(mSurfaceControl, ui::Dataspace::V0_SRGB)
120 .apply();
121 }
122
Valerie Haud3b90d22019-11-06 09:37:31 -0800123 void setUpProducer(BLASTBufferQueueHelper adapter, sp<IGraphicBufferProducer>& producer) {
124 auto igbProducer = adapter.getIGraphicBufferProducer();
125 ASSERT_NE(nullptr, igbProducer.get());
126 IGraphicBufferProducer::QueueBufferOutput qbOutput;
127 ASSERT_EQ(NO_ERROR,
128 igbProducer->connect(new DummyProducerListener, NATIVE_WINDOW_API_CPU, false,
129 &qbOutput));
130 ASSERT_NE(ui::Transform::orientation_flags::ROT_INVALID, qbOutput.transformHint);
131 producer = igbProducer;
132 }
133
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800134 void fillBuffer(uint32_t* bufData, Rect rect, uint32_t stride, uint8_t r, uint8_t g,
135 uint8_t b) {
136 for (uint32_t row = rect.top; row < rect.bottom; row++) {
137 for (uint32_t col = rect.left; col < rect.right; col++) {
Valerie Hauda3446e2019-10-14 15:49:22 -0700138 uint8_t* pixel = (uint8_t*)(bufData + (row * stride) + col);
139 *pixel = r;
140 *(pixel + 1) = g;
141 *(pixel + 2) = b;
142 *(pixel + 3) = 255;
143 }
144 }
145 }
146
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800147 void checkScreenCapture(uint8_t r, uint8_t g, uint8_t b, Rect region) {
Valerie Hauda3446e2019-10-14 15:49:22 -0700148 const auto width = mScreenCaptureBuf->getWidth();
149 const auto height = mScreenCaptureBuf->getHeight();
150 const auto stride = mScreenCaptureBuf->getStride();
151
152 uint32_t* bufData;
153 mScreenCaptureBuf->lock(static_cast<uint32_t>(GraphicBuffer::USAGE_SW_READ_OFTEN),
154 reinterpret_cast<void**>(&bufData));
155
156 for (uint32_t row = 0; row < height; row++) {
157 for (uint32_t col = 0; col < width; col++) {
158 uint8_t* pixel = (uint8_t*)(bufData + (row * stride) + col);
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800159 if (row >= region.top && row < region.bottom && col >= region.left &&
160 col < region.right) {
161 EXPECT_EQ(r, *(pixel));
162 EXPECT_EQ(g, *(pixel + 1));
163 EXPECT_EQ(b, *(pixel + 2));
164 } else {
165 EXPECT_EQ(0, *(pixel));
166 EXPECT_EQ(0, *(pixel + 1));
167 EXPECT_EQ(0, *(pixel + 2));
168 }
Valerie Hauda3446e2019-10-14 15:49:22 -0700169 }
170 }
171 mScreenCaptureBuf->unlock();
172 ASSERT_EQ(false, ::testing::Test::HasFailure());
Valerie Hauc5011f92019-10-11 09:52:07 -0700173 }
174
175 sp<SurfaceComposerClient> mClient;
Valerie Hauda3446e2019-10-14 15:49:22 -0700176 sp<ISurfaceComposer> mComposer;
177
178 sp<IBinder> mDisplayToken;
179
Valerie Hauc5011f92019-10-11 09:52:07 -0700180 sp<SurfaceControl> mSurfaceControl;
Valerie Hauda3446e2019-10-14 15:49:22 -0700181 sp<GraphicBuffer> mScreenCaptureBuf;
182
183 uint32_t mDisplayWidth;
184 uint32_t mDisplayHeight;
Valerie Hauc5011f92019-10-11 09:52:07 -0700185};
186
187TEST_F(BLASTBufferQueueTest, CreateBLASTBufferQueue) {
188 // create BLASTBufferQueue adapter associated with this surface
Valerie Hauda3446e2019-10-14 15:49:22 -0700189 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
Valerie Hauc5011f92019-10-11 09:52:07 -0700190 ASSERT_EQ(mSurfaceControl, adapter.getSurfaceControl());
Valerie Hauda3446e2019-10-14 15:49:22 -0700191 ASSERT_EQ(mDisplayWidth, adapter.getWidth());
192 ASSERT_EQ(mDisplayHeight, adapter.getHeight());
Valerie Hauc5011f92019-10-11 09:52:07 -0700193 ASSERT_EQ(nullptr, adapter.getNextTransaction());
194}
195
196TEST_F(BLASTBufferQueueTest, Update) {
Valerie Hauda3446e2019-10-14 15:49:22 -0700197 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
Valerie Hauc5011f92019-10-11 09:52:07 -0700198 sp<SurfaceControl> updateSurface =
Valerie Hauda3446e2019-10-14 15:49:22 -0700199 mClient->createSurface(String8("UpdateTest"), mDisplayWidth / 2, mDisplayHeight / 2,
200 PIXEL_FORMAT_RGBA_8888);
201 adapter.update(updateSurface, mDisplayWidth / 2, mDisplayHeight / 2);
Valerie Hauc5011f92019-10-11 09:52:07 -0700202 ASSERT_EQ(updateSurface, adapter.getSurfaceControl());
Valerie Hauda3446e2019-10-14 15:49:22 -0700203 ASSERT_EQ(mDisplayWidth / 2, adapter.getWidth());
204 ASSERT_EQ(mDisplayHeight / 2, adapter.getHeight());
Valerie Hauc5011f92019-10-11 09:52:07 -0700205}
206
207TEST_F(BLASTBufferQueueTest, SetNextTransaction) {
Valerie Hauda3446e2019-10-14 15:49:22 -0700208 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
Valerie Hauc5011f92019-10-11 09:52:07 -0700209 Transaction next;
210 adapter.setNextTransaction(&next);
211 ASSERT_EQ(&next, adapter.getNextTransaction());
212}
Valerie Hauda3446e2019-10-14 15:49:22 -0700213
214TEST_F(BLASTBufferQueueTest, onFrameAvailable_Apply) {
215 uint8_t r = 255;
216 uint8_t g = 0;
217 uint8_t b = 0;
218
219 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
Valerie Haud3b90d22019-11-06 09:37:31 -0800220 sp<IGraphicBufferProducer> igbProducer;
221 setUpProducer(adapter, igbProducer);
Valerie Hauda3446e2019-10-14 15:49:22 -0700222
223 int slot;
224 sp<Fence> fence;
225 sp<GraphicBuffer> buf;
226 auto ret = igbProducer->dequeueBuffer(&slot, &fence, mDisplayWidth, mDisplayHeight,
227 PIXEL_FORMAT_RGBA_8888, GRALLOC_USAGE_SW_WRITE_OFTEN,
228 nullptr, nullptr);
229 ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, ret);
230 ASSERT_EQ(OK, igbProducer->requestBuffer(slot, &buf));
231
232 uint32_t* bufData;
233 buf->lock(static_cast<uint32_t>(GraphicBuffer::USAGE_SW_WRITE_OFTEN),
234 reinterpret_cast<void**>(&bufData));
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800235 fillBuffer(bufData, Rect(buf->getWidth(), buf->getHeight()), buf->getStride(), r, g, b);
Valerie Hauda3446e2019-10-14 15:49:22 -0700236 buf->unlock();
237
Valerie Haud3b90d22019-11-06 09:37:31 -0800238 IGraphicBufferProducer::QueueBufferOutput qbOutput;
Valerie Hauda3446e2019-10-14 15:49:22 -0700239 IGraphicBufferProducer::QueueBufferInput input(systemTime(), false, HAL_DATASPACE_UNKNOWN,
240 Rect(mDisplayWidth, mDisplayHeight),
241 NATIVE_WINDOW_SCALING_MODE_FREEZE, 0,
242 Fence::NO_FENCE);
243 igbProducer->queueBuffer(slot, input, &qbOutput);
Valerie Hau8cee3f92019-11-06 10:06:28 -0800244 ASSERT_NE(ui::Transform::orientation_flags::ROT_INVALID, qbOutput.transformHint);
Valerie Hauda3446e2019-10-14 15:49:22 -0700245
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800246 adapter.waitForCallbacks();
Valerie Hauda3446e2019-10-14 15:49:22 -0700247
248 // capture screen and verify that it is red
249 bool capturedSecureLayers;
250 ASSERT_EQ(NO_ERROR,
251 mComposer->captureScreen(mDisplayToken, &mScreenCaptureBuf, capturedSecureLayers,
252 ui::Dataspace::V0_SRGB, ui::PixelFormat::RGBA_8888, Rect(),
253 mDisplayWidth, mDisplayHeight,
254 /*useIdentityTransform*/ false));
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800255 ASSERT_NO_FATAL_FAILURE(
256 checkScreenCapture(r, g, b, {0, 0, (int32_t)mDisplayWidth, (int32_t)mDisplayHeight}));
Valerie Hauda3446e2019-10-14 15:49:22 -0700257}
Valerie Haud3b90d22019-11-06 09:37:31 -0800258
259TEST_F(BLASTBufferQueueTest, TripleBuffering) {
260 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
261 sp<IGraphicBufferProducer> igbProducer;
262 setUpProducer(adapter, igbProducer);
263
264 std::vector<std::pair<int, sp<Fence>>> allocated;
265 for (int i = 0; i < 3; i++) {
266 int slot;
267 sp<Fence> fence;
268 sp<GraphicBuffer> buf;
269 auto ret = igbProducer->dequeueBuffer(&slot, &fence, mDisplayWidth, mDisplayHeight,
270 PIXEL_FORMAT_RGBA_8888, GRALLOC_USAGE_SW_WRITE_OFTEN,
271 nullptr, nullptr);
272 ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, ret);
273 ASSERT_EQ(OK, igbProducer->requestBuffer(slot, &buf));
274 allocated.push_back({slot, fence});
275 }
276 for (int i = 0; i < allocated.size(); i++) {
277 igbProducer->cancelBuffer(allocated[i].first, allocated[i].second);
278 }
279
280 for (int i = 0; i < 10; i++) {
281 int slot;
282 sp<Fence> fence;
283 sp<GraphicBuffer> buf;
284 auto ret = igbProducer->dequeueBuffer(&slot, &fence, mDisplayWidth, mDisplayHeight,
285 PIXEL_FORMAT_RGBA_8888, GRALLOC_USAGE_SW_WRITE_OFTEN,
286 nullptr, nullptr);
287 ASSERT_EQ(NO_ERROR, ret);
288 IGraphicBufferProducer::QueueBufferOutput qbOutput;
289 IGraphicBufferProducer::QueueBufferInput input(systemTime(), false, HAL_DATASPACE_UNKNOWN,
290 Rect(mDisplayWidth, mDisplayHeight),
291 NATIVE_WINDOW_SCALING_MODE_FREEZE, 0,
292 Fence::NO_FENCE);
293 igbProducer->queueBuffer(slot, input, &qbOutput);
294 }
295 adapter.waitForCallbacks();
296}
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800297
298TEST_F(BLASTBufferQueueTest, SetCrop_Item) {
299 uint8_t r = 255;
300 uint8_t g = 0;
301 uint8_t b = 0;
302
303 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
304 sp<IGraphicBufferProducer> igbProducer;
305 setUpProducer(adapter, igbProducer);
306 int slot;
307 sp<Fence> fence;
308 sp<GraphicBuffer> buf;
309 auto ret = igbProducer->dequeueBuffer(&slot, &fence, mDisplayWidth, mDisplayHeight,
310 PIXEL_FORMAT_RGBA_8888, GRALLOC_USAGE_SW_WRITE_OFTEN,
311 nullptr, nullptr);
312 ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, ret);
313 ASSERT_EQ(OK, igbProducer->requestBuffer(slot, &buf));
314
315 uint32_t* bufData;
316 buf->lock(static_cast<uint32_t>(GraphicBuffer::USAGE_SW_WRITE_OFTEN),
317 reinterpret_cast<void**>(&bufData));
318 fillBuffer(bufData, Rect(buf->getWidth(), buf->getHeight() / 2), buf->getStride(), r, g, b);
319 buf->unlock();
320
321 IGraphicBufferProducer::QueueBufferOutput qbOutput;
322 IGraphicBufferProducer::QueueBufferInput input(systemTime(), false, HAL_DATASPACE_UNKNOWN,
323 Rect(mDisplayWidth, mDisplayHeight / 2),
324 NATIVE_WINDOW_SCALING_MODE_FREEZE, 0,
325 Fence::NO_FENCE);
326 igbProducer->queueBuffer(slot, input, &qbOutput);
327 ASSERT_NE(ui::Transform::orientation_flags::ROT_INVALID, qbOutput.transformHint);
328
329 adapter.waitForCallbacks();
330 // capture screen and verify that it is red
331 bool capturedSecureLayers;
332 ASSERT_EQ(NO_ERROR,
333 mComposer->captureScreen(mDisplayToken, &mScreenCaptureBuf, capturedSecureLayers,
334 ui::Dataspace::V0_SRGB, ui::PixelFormat::RGBA_8888, Rect(),
335 mDisplayWidth, mDisplayHeight,
336 /*useIdentityTransform*/ false));
337 ASSERT_NO_FATAL_FAILURE(
338 checkScreenCapture(r, g, b, {0, 0, (int32_t)mDisplayWidth, (int32_t)mDisplayHeight}));
339}
340
341TEST_F(BLASTBufferQueueTest, SetCrop_ScalingModeScaleCrop) {
342 uint8_t r = 255;
343 uint8_t g = 0;
344 uint8_t b = 0;
345
346 int32_t bufferSideLength =
347 (mDisplayWidth < mDisplayHeight) ? mDisplayWidth / 2 : mDisplayHeight / 2;
348 int32_t finalCropSideLength = bufferSideLength / 2;
349
350 auto bg = mClient->createSurface(String8("BGTest"), 0, 0, PIXEL_FORMAT_RGBA_8888,
351 ISurfaceComposerClient::eFXSurfaceColor);
352 ASSERT_NE(nullptr, bg.get());
353 Transaction t;
354 t.setLayerStack(bg, 0)
355 .setCrop_legacy(bg, Rect(0, 0, mDisplayWidth, mDisplayHeight))
356 .setColor(bg, half3{0, 0, 0})
357 .setLayer(bg, 0)
358 .apply();
359
360 BLASTBufferQueueHelper adapter(mSurfaceControl, bufferSideLength, bufferSideLength);
361 sp<IGraphicBufferProducer> igbProducer;
362 setUpProducer(adapter, igbProducer);
363 int slot;
364 sp<Fence> fence;
365 sp<GraphicBuffer> buf;
366 auto ret = igbProducer->dequeueBuffer(&slot, &fence, bufferSideLength, bufferSideLength,
367 PIXEL_FORMAT_RGBA_8888, GRALLOC_USAGE_SW_WRITE_OFTEN,
368 nullptr, nullptr);
369 ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, ret);
370 ASSERT_EQ(OK, igbProducer->requestBuffer(slot, &buf));
371
372 uint32_t* bufData;
373 buf->lock(static_cast<uint32_t>(GraphicBuffer::USAGE_SW_WRITE_OFTEN),
374 reinterpret_cast<void**>(&bufData));
375 fillBuffer(bufData, Rect(buf->getWidth(), buf->getHeight()), buf->getStride(), 0, 0, 0);
376 fillBuffer(bufData,
377 Rect(finalCropSideLength / 2, 0, buf->getWidth() - finalCropSideLength / 2,
378 buf->getHeight()),
379 buf->getStride(), r, g, b);
380 buf->unlock();
381
382 IGraphicBufferProducer::QueueBufferOutput qbOutput;
383 IGraphicBufferProducer::QueueBufferInput input(systemTime(), false, HAL_DATASPACE_UNKNOWN,
384 Rect(bufferSideLength, finalCropSideLength),
385 NATIVE_WINDOW_SCALING_MODE_SCALE_CROP, 0,
386 Fence::NO_FENCE);
387 igbProducer->queueBuffer(slot, input, &qbOutput);
388 ASSERT_NE(ui::Transform::orientation_flags::ROT_INVALID, qbOutput.transformHint);
389
390 adapter.waitForCallbacks();
391 // capture screen and verify that it is red
392 bool capturedSecureLayers;
393 ASSERT_EQ(NO_ERROR,
394 mComposer->captureScreen(mDisplayToken, &mScreenCaptureBuf, capturedSecureLayers,
395 ui::Dataspace::V0_SRGB, ui::PixelFormat::RGBA_8888, Rect(),
396 mDisplayWidth, mDisplayHeight,
397 /*useIdentityTransform*/ false));
398 ASSERT_NO_FATAL_FAILURE(
399 checkScreenCapture(r, g, b,
400 {0, 0, (int32_t)bufferSideLength, (int32_t)bufferSideLength}));
401}
402
Valerie Hauc5011f92019-10-11 09:52:07 -0700403} // namespace android