blob: 41f0d40adda7f863fad385797384a1e8c773d957 [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 Haua32c5522019-12-09 10:11:08 -080072 while (mBlastBufferQueueAdapter->mSubmitted.size() > 0) {
Valerie Haud3b90d22019-11-06 09:37:31 -080073 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());
Valerie Hauc78c43a2020-01-09 17:34:14 -0800126 ASSERT_EQ(NO_ERROR, igbProducer->setMaxDequeuedBufferCount(2));
Valerie Haud3b90d22019-11-06 09:37:31 -0800127 IGraphicBufferProducer::QueueBufferOutput qbOutput;
128 ASSERT_EQ(NO_ERROR,
129 igbProducer->connect(new DummyProducerListener, NATIVE_WINDOW_API_CPU, false,
130 &qbOutput));
Dominik Laskowski718f9602019-11-09 20:01:35 -0800131 ASSERT_NE(ui::Transform::ROT_INVALID, qbOutput.transformHint);
Valerie Haud3b90d22019-11-06 09:37:31 -0800132 producer = igbProducer;
133 }
134
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800135 void fillBuffer(uint32_t* bufData, Rect rect, uint32_t stride, uint8_t r, uint8_t g,
136 uint8_t b) {
137 for (uint32_t row = rect.top; row < rect.bottom; row++) {
138 for (uint32_t col = rect.left; col < rect.right; col++) {
Valerie Hauda3446e2019-10-14 15:49:22 -0700139 uint8_t* pixel = (uint8_t*)(bufData + (row * stride) + col);
140 *pixel = r;
141 *(pixel + 1) = g;
142 *(pixel + 2) = b;
143 *(pixel + 3) = 255;
144 }
145 }
146 }
147
Valerie Hau5977fc82019-12-05 15:56:39 -0800148 void fillQuadrants(sp<GraphicBuffer>& buf) {
149 const auto bufWidth = buf->getWidth();
150 const auto bufHeight = buf->getHeight();
151 uint32_t* bufData;
152 buf->lock(static_cast<uint32_t>(GraphicBuffer::USAGE_SW_WRITE_OFTEN),
153 reinterpret_cast<void**>(&bufData));
154 fillBuffer(bufData, Rect(0, 0, bufWidth / 2, bufHeight / 2), buf->getStride(), 0, 0, 0);
155 fillBuffer(bufData, Rect(bufWidth / 2, 0, bufWidth, bufHeight / 2), buf->getStride(), 255,
156 0, 0);
157 fillBuffer(bufData, Rect(bufWidth / 2, bufHeight / 2, bufWidth, bufHeight),
158 buf->getStride(), 0, 255, 0);
159 fillBuffer(bufData, Rect(0, bufHeight / 2, bufWidth / 2, bufHeight), buf->getStride(), 0, 0,
160 255);
161 buf->unlock();
162 }
163
164 void checkScreenCapture(uint8_t r, uint8_t g, uint8_t b, Rect region, int32_t border = 0,
165 bool outsideRegion = false) {
166 const auto epsilon = 3;
Valerie Hauda3446e2019-10-14 15:49:22 -0700167 const auto width = mScreenCaptureBuf->getWidth();
168 const auto height = mScreenCaptureBuf->getHeight();
169 const auto stride = mScreenCaptureBuf->getStride();
170
171 uint32_t* bufData;
172 mScreenCaptureBuf->lock(static_cast<uint32_t>(GraphicBuffer::USAGE_SW_READ_OFTEN),
173 reinterpret_cast<void**>(&bufData));
174
175 for (uint32_t row = 0; row < height; row++) {
176 for (uint32_t col = 0; col < width; col++) {
177 uint8_t* pixel = (uint8_t*)(bufData + (row * stride) + col);
Valerie Hau5977fc82019-12-05 15:56:39 -0800178 bool inRegion;
179 if (!outsideRegion) {
180 inRegion = row >= region.top + border && row < region.bottom - border &&
181 col >= region.left + border && col < region.right - border;
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800182 } else {
Valerie Hau5977fc82019-12-05 15:56:39 -0800183 inRegion = row >= region.top - border && row < region.bottom + border &&
184 col >= region.left - border && col < region.right + border;
185 }
186 if (!outsideRegion && inRegion) {
187 EXPECT_GE(epsilon, abs(r - *(pixel)));
188 EXPECT_GE(epsilon, abs(g - *(pixel + 1)));
189 EXPECT_GE(epsilon, abs(b - *(pixel + 2)));
190 } else if (outsideRegion && !inRegion) {
191 EXPECT_GE(epsilon, abs(r - *(pixel)));
192 EXPECT_GE(epsilon, abs(g - *(pixel + 1)));
193 EXPECT_GE(epsilon, abs(b - *(pixel + 2)));
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800194 }
Valerie Hauda3446e2019-10-14 15:49:22 -0700195 }
196 }
197 mScreenCaptureBuf->unlock();
198 ASSERT_EQ(false, ::testing::Test::HasFailure());
Valerie Hauc5011f92019-10-11 09:52:07 -0700199 }
200
201 sp<SurfaceComposerClient> mClient;
Valerie Hauda3446e2019-10-14 15:49:22 -0700202 sp<ISurfaceComposer> mComposer;
203
204 sp<IBinder> mDisplayToken;
205
Valerie Hauc5011f92019-10-11 09:52:07 -0700206 sp<SurfaceControl> mSurfaceControl;
Valerie Hauda3446e2019-10-14 15:49:22 -0700207 sp<GraphicBuffer> mScreenCaptureBuf;
208
209 uint32_t mDisplayWidth;
210 uint32_t mDisplayHeight;
Valerie Hauc5011f92019-10-11 09:52:07 -0700211};
212
213TEST_F(BLASTBufferQueueTest, CreateBLASTBufferQueue) {
214 // create BLASTBufferQueue adapter associated with this surface
Valerie Hauda3446e2019-10-14 15:49:22 -0700215 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
Valerie Hauc5011f92019-10-11 09:52:07 -0700216 ASSERT_EQ(mSurfaceControl, adapter.getSurfaceControl());
Valerie Hauda3446e2019-10-14 15:49:22 -0700217 ASSERT_EQ(mDisplayWidth, adapter.getWidth());
218 ASSERT_EQ(mDisplayHeight, adapter.getHeight());
Valerie Hauc5011f92019-10-11 09:52:07 -0700219 ASSERT_EQ(nullptr, adapter.getNextTransaction());
220}
221
222TEST_F(BLASTBufferQueueTest, Update) {
Valerie Hauda3446e2019-10-14 15:49:22 -0700223 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
Valerie Hauc5011f92019-10-11 09:52:07 -0700224 sp<SurfaceControl> updateSurface =
Valerie Hauda3446e2019-10-14 15:49:22 -0700225 mClient->createSurface(String8("UpdateTest"), mDisplayWidth / 2, mDisplayHeight / 2,
226 PIXEL_FORMAT_RGBA_8888);
227 adapter.update(updateSurface, mDisplayWidth / 2, mDisplayHeight / 2);
Valerie Hauc5011f92019-10-11 09:52:07 -0700228 ASSERT_EQ(updateSurface, adapter.getSurfaceControl());
Valerie Hauda3446e2019-10-14 15:49:22 -0700229 ASSERT_EQ(mDisplayWidth / 2, adapter.getWidth());
230 ASSERT_EQ(mDisplayHeight / 2, adapter.getHeight());
Valerie Hauc5011f92019-10-11 09:52:07 -0700231}
232
233TEST_F(BLASTBufferQueueTest, SetNextTransaction) {
Valerie Hauda3446e2019-10-14 15:49:22 -0700234 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
Valerie Hauc5011f92019-10-11 09:52:07 -0700235 Transaction next;
236 adapter.setNextTransaction(&next);
237 ASSERT_EQ(&next, adapter.getNextTransaction());
238}
Valerie Hauda3446e2019-10-14 15:49:22 -0700239
Valerie Hau181abd32020-01-27 14:18:28 -0800240TEST_F(BLASTBufferQueueTest, onFrameAvailable_ApplyDesiredPresentTime) {
241 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
242 sp<IGraphicBufferProducer> igbProducer;
243 setUpProducer(adapter, igbProducer);
244
245 int slot;
246 sp<Fence> fence;
247 sp<GraphicBuffer> buf;
248 auto ret = igbProducer->dequeueBuffer(&slot, &fence, mDisplayWidth, mDisplayHeight,
249 PIXEL_FORMAT_RGBA_8888, GRALLOC_USAGE_SW_WRITE_OFTEN,
250 nullptr, nullptr);
251 ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, ret);
252 ASSERT_EQ(OK, igbProducer->requestBuffer(slot, &buf));
253
254 nsecs_t desiredPresentTime = systemTime() + nsecs_t(5 * 1e8);
255 IGraphicBufferProducer::QueueBufferOutput qbOutput;
256 IGraphicBufferProducer::QueueBufferInput input(desiredPresentTime, false, HAL_DATASPACE_UNKNOWN,
257 Rect(mDisplayWidth, mDisplayHeight),
258 NATIVE_WINDOW_SCALING_MODE_FREEZE, 0,
259 Fence::NO_FENCE);
260 igbProducer->queueBuffer(slot, input, &qbOutput);
261 ASSERT_NE(ui::Transform::ROT_INVALID, qbOutput.transformHint);
262
263 adapter.waitForCallbacks();
264 ASSERT_GE(systemTime(), desiredPresentTime);
265}
266
Valerie Hauda3446e2019-10-14 15:49:22 -0700267TEST_F(BLASTBufferQueueTest, onFrameAvailable_Apply) {
268 uint8_t r = 255;
269 uint8_t g = 0;
270 uint8_t b = 0;
271
272 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
Valerie Haud3b90d22019-11-06 09:37:31 -0800273 sp<IGraphicBufferProducer> igbProducer;
274 setUpProducer(adapter, igbProducer);
Valerie Hauda3446e2019-10-14 15:49:22 -0700275
276 int slot;
277 sp<Fence> fence;
278 sp<GraphicBuffer> buf;
279 auto ret = igbProducer->dequeueBuffer(&slot, &fence, mDisplayWidth, mDisplayHeight,
280 PIXEL_FORMAT_RGBA_8888, GRALLOC_USAGE_SW_WRITE_OFTEN,
281 nullptr, nullptr);
282 ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, ret);
283 ASSERT_EQ(OK, igbProducer->requestBuffer(slot, &buf));
284
285 uint32_t* bufData;
286 buf->lock(static_cast<uint32_t>(GraphicBuffer::USAGE_SW_WRITE_OFTEN),
287 reinterpret_cast<void**>(&bufData));
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800288 fillBuffer(bufData, Rect(buf->getWidth(), buf->getHeight()), buf->getStride(), r, g, b);
Valerie Hauda3446e2019-10-14 15:49:22 -0700289 buf->unlock();
290
Valerie Haud3b90d22019-11-06 09:37:31 -0800291 IGraphicBufferProducer::QueueBufferOutput qbOutput;
Valerie Hauda3446e2019-10-14 15:49:22 -0700292 IGraphicBufferProducer::QueueBufferInput input(systemTime(), false, HAL_DATASPACE_UNKNOWN,
293 Rect(mDisplayWidth, mDisplayHeight),
294 NATIVE_WINDOW_SCALING_MODE_FREEZE, 0,
295 Fence::NO_FENCE);
296 igbProducer->queueBuffer(slot, input, &qbOutput);
Dominik Laskowski718f9602019-11-09 20:01:35 -0800297 ASSERT_NE(ui::Transform::ROT_INVALID, qbOutput.transformHint);
Valerie Hauda3446e2019-10-14 15:49:22 -0700298
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800299 adapter.waitForCallbacks();
Valerie Hauda3446e2019-10-14 15:49:22 -0700300
301 // capture screen and verify that it is red
302 bool capturedSecureLayers;
303 ASSERT_EQ(NO_ERROR,
304 mComposer->captureScreen(mDisplayToken, &mScreenCaptureBuf, capturedSecureLayers,
305 ui::Dataspace::V0_SRGB, ui::PixelFormat::RGBA_8888, Rect(),
306 mDisplayWidth, mDisplayHeight,
307 /*useIdentityTransform*/ false));
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800308 ASSERT_NO_FATAL_FAILURE(
309 checkScreenCapture(r, g, b, {0, 0, (int32_t)mDisplayWidth, (int32_t)mDisplayHeight}));
Valerie Hauda3446e2019-10-14 15:49:22 -0700310}
Valerie Haud3b90d22019-11-06 09:37:31 -0800311
312TEST_F(BLASTBufferQueueTest, TripleBuffering) {
313 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
314 sp<IGraphicBufferProducer> igbProducer;
315 setUpProducer(adapter, igbProducer);
316
317 std::vector<std::pair<int, sp<Fence>>> allocated;
318 for (int i = 0; i < 3; i++) {
319 int slot;
320 sp<Fence> fence;
321 sp<GraphicBuffer> buf;
322 auto ret = igbProducer->dequeueBuffer(&slot, &fence, mDisplayWidth, mDisplayHeight,
323 PIXEL_FORMAT_RGBA_8888, GRALLOC_USAGE_SW_WRITE_OFTEN,
324 nullptr, nullptr);
325 ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, ret);
326 ASSERT_EQ(OK, igbProducer->requestBuffer(slot, &buf));
327 allocated.push_back({slot, fence});
328 }
329 for (int i = 0; i < allocated.size(); i++) {
330 igbProducer->cancelBuffer(allocated[i].first, allocated[i].second);
331 }
332
Valerie Haua32c5522019-12-09 10:11:08 -0800333 for (int i = 0; i < 100; i++) {
Valerie Haud3b90d22019-11-06 09:37:31 -0800334 int slot;
335 sp<Fence> fence;
336 sp<GraphicBuffer> buf;
337 auto ret = igbProducer->dequeueBuffer(&slot, &fence, mDisplayWidth, mDisplayHeight,
338 PIXEL_FORMAT_RGBA_8888, GRALLOC_USAGE_SW_WRITE_OFTEN,
339 nullptr, nullptr);
340 ASSERT_EQ(NO_ERROR, ret);
341 IGraphicBufferProducer::QueueBufferOutput qbOutput;
342 IGraphicBufferProducer::QueueBufferInput input(systemTime(), false, HAL_DATASPACE_UNKNOWN,
343 Rect(mDisplayWidth, mDisplayHeight),
344 NATIVE_WINDOW_SCALING_MODE_FREEZE, 0,
345 Fence::NO_FENCE);
346 igbProducer->queueBuffer(slot, input, &qbOutput);
347 }
348 adapter.waitForCallbacks();
349}
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800350
351TEST_F(BLASTBufferQueueTest, SetCrop_Item) {
352 uint8_t r = 255;
353 uint8_t g = 0;
354 uint8_t b = 0;
355
356 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
357 sp<IGraphicBufferProducer> igbProducer;
358 setUpProducer(adapter, igbProducer);
359 int slot;
360 sp<Fence> fence;
361 sp<GraphicBuffer> buf;
362 auto ret = igbProducer->dequeueBuffer(&slot, &fence, mDisplayWidth, mDisplayHeight,
363 PIXEL_FORMAT_RGBA_8888, GRALLOC_USAGE_SW_WRITE_OFTEN,
364 nullptr, nullptr);
365 ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, ret);
366 ASSERT_EQ(OK, igbProducer->requestBuffer(slot, &buf));
367
368 uint32_t* bufData;
369 buf->lock(static_cast<uint32_t>(GraphicBuffer::USAGE_SW_WRITE_OFTEN),
370 reinterpret_cast<void**>(&bufData));
371 fillBuffer(bufData, Rect(buf->getWidth(), buf->getHeight() / 2), buf->getStride(), r, g, b);
372 buf->unlock();
373
374 IGraphicBufferProducer::QueueBufferOutput qbOutput;
375 IGraphicBufferProducer::QueueBufferInput input(systemTime(), false, HAL_DATASPACE_UNKNOWN,
376 Rect(mDisplayWidth, mDisplayHeight / 2),
377 NATIVE_WINDOW_SCALING_MODE_FREEZE, 0,
378 Fence::NO_FENCE);
379 igbProducer->queueBuffer(slot, input, &qbOutput);
Dominik Laskowski718f9602019-11-09 20:01:35 -0800380 ASSERT_NE(ui::Transform::ROT_INVALID, qbOutput.transformHint);
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800381
382 adapter.waitForCallbacks();
383 // capture screen and verify that it is red
384 bool capturedSecureLayers;
385 ASSERT_EQ(NO_ERROR,
386 mComposer->captureScreen(mDisplayToken, &mScreenCaptureBuf, capturedSecureLayers,
387 ui::Dataspace::V0_SRGB, ui::PixelFormat::RGBA_8888, Rect(),
388 mDisplayWidth, mDisplayHeight,
389 /*useIdentityTransform*/ false));
390 ASSERT_NO_FATAL_FAILURE(
391 checkScreenCapture(r, g, b, {0, 0, (int32_t)mDisplayWidth, (int32_t)mDisplayHeight}));
392}
393
394TEST_F(BLASTBufferQueueTest, SetCrop_ScalingModeScaleCrop) {
395 uint8_t r = 255;
396 uint8_t g = 0;
397 uint8_t b = 0;
398
399 int32_t bufferSideLength =
400 (mDisplayWidth < mDisplayHeight) ? mDisplayWidth / 2 : mDisplayHeight / 2;
401 int32_t finalCropSideLength = bufferSideLength / 2;
402
403 auto bg = mClient->createSurface(String8("BGTest"), 0, 0, PIXEL_FORMAT_RGBA_8888,
404 ISurfaceComposerClient::eFXSurfaceColor);
405 ASSERT_NE(nullptr, bg.get());
406 Transaction t;
407 t.setLayerStack(bg, 0)
408 .setCrop_legacy(bg, Rect(0, 0, mDisplayWidth, mDisplayHeight))
409 .setColor(bg, half3{0, 0, 0})
410 .setLayer(bg, 0)
411 .apply();
412
413 BLASTBufferQueueHelper adapter(mSurfaceControl, bufferSideLength, bufferSideLength);
414 sp<IGraphicBufferProducer> igbProducer;
415 setUpProducer(adapter, igbProducer);
416 int slot;
417 sp<Fence> fence;
418 sp<GraphicBuffer> buf;
419 auto ret = igbProducer->dequeueBuffer(&slot, &fence, bufferSideLength, bufferSideLength,
420 PIXEL_FORMAT_RGBA_8888, GRALLOC_USAGE_SW_WRITE_OFTEN,
421 nullptr, nullptr);
422 ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, ret);
423 ASSERT_EQ(OK, igbProducer->requestBuffer(slot, &buf));
424
425 uint32_t* bufData;
426 buf->lock(static_cast<uint32_t>(GraphicBuffer::USAGE_SW_WRITE_OFTEN),
427 reinterpret_cast<void**>(&bufData));
428 fillBuffer(bufData, Rect(buf->getWidth(), buf->getHeight()), buf->getStride(), 0, 0, 0);
429 fillBuffer(bufData,
430 Rect(finalCropSideLength / 2, 0, buf->getWidth() - finalCropSideLength / 2,
431 buf->getHeight()),
432 buf->getStride(), r, g, b);
433 buf->unlock();
434
435 IGraphicBufferProducer::QueueBufferOutput qbOutput;
436 IGraphicBufferProducer::QueueBufferInput input(systemTime(), false, HAL_DATASPACE_UNKNOWN,
437 Rect(bufferSideLength, finalCropSideLength),
438 NATIVE_WINDOW_SCALING_MODE_SCALE_CROP, 0,
439 Fence::NO_FENCE);
440 igbProducer->queueBuffer(slot, input, &qbOutput);
Dominik Laskowski718f9602019-11-09 20:01:35 -0800441 ASSERT_NE(ui::Transform::ROT_INVALID, qbOutput.transformHint);
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800442
443 adapter.waitForCallbacks();
444 // capture screen and verify that it is red
445 bool capturedSecureLayers;
446 ASSERT_EQ(NO_ERROR,
447 mComposer->captureScreen(mDisplayToken, &mScreenCaptureBuf, capturedSecureLayers,
448 ui::Dataspace::V0_SRGB, ui::PixelFormat::RGBA_8888, Rect(),
449 mDisplayWidth, mDisplayHeight,
450 /*useIdentityTransform*/ false));
451 ASSERT_NO_FATAL_FAILURE(
452 checkScreenCapture(r, g, b,
453 {0, 0, (int32_t)bufferSideLength, (int32_t)bufferSideLength}));
Valerie Hau5977fc82019-12-05 15:56:39 -0800454 ASSERT_NO_FATAL_FAILURE(
455 checkScreenCapture(0, 0, 0,
456 {0, 0, (int32_t)bufferSideLength, (int32_t)bufferSideLength},
457 /*border*/ 0, /*outsideRegion*/ true));
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800458}
459
Valerie Hau5977fc82019-12-05 15:56:39 -0800460class BLASTBufferQueueTransformTest : public BLASTBufferQueueTest {
461public:
462 void test(uint32_t tr) {
463 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
464 sp<IGraphicBufferProducer> igbProducer;
465 setUpProducer(adapter, igbProducer);
466
467 auto bufWidth = mDisplayWidth;
468 auto bufHeight = mDisplayHeight;
469 int slot;
470 sp<Fence> fence;
471 sp<GraphicBuffer> buf;
472
473 auto ret = igbProducer->dequeueBuffer(&slot, &fence, bufWidth, bufHeight,
474 PIXEL_FORMAT_RGBA_8888, GRALLOC_USAGE_SW_WRITE_OFTEN,
475 nullptr, nullptr);
476 ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, ret);
477 ASSERT_EQ(OK, igbProducer->requestBuffer(slot, &buf));
478
479 fillQuadrants(buf);
480
481 IGraphicBufferProducer::QueueBufferOutput qbOutput;
482 IGraphicBufferProducer::QueueBufferInput input(systemTime(), false, HAL_DATASPACE_UNKNOWN,
483 Rect(bufWidth, bufHeight),
484 NATIVE_WINDOW_SCALING_MODE_FREEZE, tr,
485 Fence::NO_FENCE);
486 igbProducer->queueBuffer(slot, input, &qbOutput);
Dominik Laskowski718f9602019-11-09 20:01:35 -0800487 ASSERT_NE(ui::Transform::ROT_INVALID, qbOutput.transformHint);
Valerie Hau5977fc82019-12-05 15:56:39 -0800488
489 adapter.waitForCallbacks();
490 bool capturedSecureLayers;
491 ASSERT_EQ(NO_ERROR,
492 mComposer->captureScreen(mDisplayToken, &mScreenCaptureBuf, capturedSecureLayers,
493 ui::Dataspace::V0_SRGB, ui::PixelFormat::RGBA_8888,
494 Rect(), mDisplayWidth, mDisplayHeight,
495 /*useIdentityTransform*/ false));
496 switch (tr) {
497 case ui::Transform::ROT_0:
498 ASSERT_NO_FATAL_FAILURE(checkScreenCapture(0, 0, 0,
499 {0, 0, (int32_t)mDisplayWidth / 2,
500 (int32_t)mDisplayHeight / 2},
501 1));
502 ASSERT_NO_FATAL_FAILURE(
503 checkScreenCapture(255, 0, 0,
504 {(int32_t)mDisplayWidth / 2, 0, (int32_t)mDisplayWidth,
505 (int32_t)mDisplayHeight / 2},
506 1));
507 ASSERT_NO_FATAL_FAILURE(
508 checkScreenCapture(0, 255, 0,
509 {(int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight / 2,
510 (int32_t)mDisplayWidth, (int32_t)mDisplayHeight},
511 1));
512 ASSERT_NO_FATAL_FAILURE(
513 checkScreenCapture(0, 0, 255,
514 {0, (int32_t)mDisplayHeight / 2,
515 (int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight},
516 1));
517 break;
518 case ui::Transform::FLIP_H:
519 ASSERT_NO_FATAL_FAILURE(checkScreenCapture(255, 0, 0,
520 {0, 0, (int32_t)mDisplayWidth / 2,
521 (int32_t)mDisplayHeight / 2},
522 1));
523 ASSERT_NO_FATAL_FAILURE(
524 checkScreenCapture(0, 0, 0,
525 {(int32_t)mDisplayWidth / 2, 0, (int32_t)mDisplayWidth,
526 (int32_t)mDisplayHeight / 2},
527 1));
528 ASSERT_NO_FATAL_FAILURE(
529 checkScreenCapture(0, 0, 255,
530 {(int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight / 2,
531 (int32_t)mDisplayWidth, (int32_t)mDisplayHeight},
532 1));
533 ASSERT_NO_FATAL_FAILURE(
534 checkScreenCapture(0, 255, 0,
535 {0, (int32_t)mDisplayHeight / 2,
536 (int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight},
537 1));
538 break;
539 case ui::Transform::FLIP_V:
540 ASSERT_NO_FATAL_FAILURE(checkScreenCapture(0, 0, 255,
541 {0, 0, (int32_t)mDisplayWidth / 2,
542 (int32_t)mDisplayHeight / 2},
543 1));
544 ASSERT_NO_FATAL_FAILURE(
545 checkScreenCapture(0, 255, 0,
546 {(int32_t)mDisplayWidth / 2, 0, (int32_t)mDisplayWidth,
547 (int32_t)mDisplayHeight / 2},
548 1));
549 ASSERT_NO_FATAL_FAILURE(
550 checkScreenCapture(255, 0, 0,
551 {(int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight / 2,
552 (int32_t)mDisplayWidth, (int32_t)mDisplayHeight},
553 1));
554 ASSERT_NO_FATAL_FAILURE(
555 checkScreenCapture(0, 0, 0,
556 {0, (int32_t)mDisplayHeight / 2,
557 (int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight},
558 1));
559 break;
560 case ui::Transform::ROT_90:
561 ASSERT_NO_FATAL_FAILURE(checkScreenCapture(0, 0, 255,
562 {0, 0, (int32_t)mDisplayWidth / 2,
563 (int32_t)mDisplayHeight / 2},
564 1));
565 ASSERT_NO_FATAL_FAILURE(
566 checkScreenCapture(0, 0, 0,
567 {(int32_t)mDisplayWidth / 2, 0, (int32_t)mDisplayWidth,
568 (int32_t)mDisplayHeight / 2},
569 1));
570 ASSERT_NO_FATAL_FAILURE(
571 checkScreenCapture(255, 0, 0,
572 {(int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight / 2,
573 (int32_t)mDisplayWidth, (int32_t)mDisplayHeight},
574 1));
575 ASSERT_NO_FATAL_FAILURE(
576 checkScreenCapture(0, 255, 0,
577 {0, (int32_t)mDisplayHeight / 2,
578 (int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight},
579 1));
580 break;
581 case ui::Transform::ROT_180:
582 ASSERT_NO_FATAL_FAILURE(checkScreenCapture(0, 255, 0,
583 {0, 0, (int32_t)mDisplayWidth / 2,
584 (int32_t)mDisplayHeight / 2},
585 1));
586 ASSERT_NO_FATAL_FAILURE(
587 checkScreenCapture(0, 0, 255,
588 {(int32_t)mDisplayWidth / 2, 0, (int32_t)mDisplayWidth,
589 (int32_t)mDisplayHeight / 2},
590 1));
591 ASSERT_NO_FATAL_FAILURE(
592 checkScreenCapture(0, 0, 0,
593 {(int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight / 2,
594 (int32_t)mDisplayWidth, (int32_t)mDisplayHeight},
595 1));
596 ASSERT_NO_FATAL_FAILURE(
597 checkScreenCapture(255, 0, 0,
598 {0, (int32_t)mDisplayHeight / 2,
599 (int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight},
600 1));
601 break;
602 case ui::Transform::ROT_270:
603 ASSERT_NO_FATAL_FAILURE(checkScreenCapture(255, 0, 0,
604 {0, 0, (int32_t)mDisplayWidth / 2,
605 (int32_t)mDisplayHeight / 2},
606 1));
607 ASSERT_NO_FATAL_FAILURE(
608 checkScreenCapture(0, 255, 0,
609 {(int32_t)mDisplayWidth / 2, 0, (int32_t)mDisplayWidth,
610 (int32_t)mDisplayHeight / 2},
611 1));
612 ASSERT_NO_FATAL_FAILURE(
613 checkScreenCapture(0, 0, 255,
614 {(int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight / 2,
615 (int32_t)mDisplayWidth, (int32_t)mDisplayHeight},
616 1));
617 ASSERT_NO_FATAL_FAILURE(
618 checkScreenCapture(0, 0, 0,
619 {0, (int32_t)mDisplayHeight / 2,
620 (int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight},
621 1));
622 }
623 }
624};
625
626TEST_F(BLASTBufferQueueTransformTest, setTransform_ROT_0) {
627 test(ui::Transform::ROT_0);
628}
629
630TEST_F(BLASTBufferQueueTransformTest, setTransform_FLIP_H) {
631 test(ui::Transform::FLIP_H);
632}
633
634TEST_F(BLASTBufferQueueTransformTest, setTransform_FLIP_V) {
635 test(ui::Transform::FLIP_V);
636}
637
638TEST_F(BLASTBufferQueueTransformTest, setTransform_ROT_90) {
639 test(ui::Transform::ROT_90);
640}
641
642TEST_F(BLASTBufferQueueTransformTest, setTransform_ROT_180) {
643 test(ui::Transform::ROT_180);
644}
645
646TEST_F(BLASTBufferQueueTransformTest, setTransform_ROT_270) {
647 test(ui::Transform::ROT_270);
648}
Valerie Hauc5011f92019-10-11 09:52:07 -0700649} // namespace android