blob: a273914cafc17ce24c3f4461315740643f3beae1 [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>
Dominik Laskowski3cb3d4e2019-11-21 11:14:45 -080028#include <ui/DisplayConfig.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
Dominik Laskowski3cb3d4e2019-11-21 11:14:45 -0800106 DisplayConfig config;
107 ASSERT_EQ(NO_ERROR, SurfaceComposerClient::getActiveDisplayConfig(mDisplayToken, &config));
108 const ui::Size& resolution = config.resolution;
109 mDisplayWidth = resolution.getWidth();
110 mDisplayHeight = resolution.getHeight();
Valerie Hauda3446e2019-10-14 15:49:22 -0700111
112 mSurfaceControl = mClient->createSurface(String8("TestSurface"), mDisplayWidth,
113 mDisplayHeight, PIXEL_FORMAT_RGBA_8888,
114 ISurfaceComposerClient::eFXSurfaceBufferState,
115 /*parent*/ nullptr);
116 t.setLayerStack(mSurfaceControl, 0)
117 .setLayer(mSurfaceControl, std::numeric_limits<int32_t>::max())
Dominik Laskowski3cb3d4e2019-11-21 11:14:45 -0800118 .setFrame(mSurfaceControl, Rect(resolution))
Valerie Hauda3446e2019-10-14 15:49:22 -0700119 .show(mSurfaceControl)
120 .setDataspace(mSurfaceControl, ui::Dataspace::V0_SRGB)
121 .apply();
122 }
123
Valerie Haud3b90d22019-11-06 09:37:31 -0800124 void setUpProducer(BLASTBufferQueueHelper adapter, sp<IGraphicBufferProducer>& producer) {
125 auto igbProducer = adapter.getIGraphicBufferProducer();
126 ASSERT_NE(nullptr, igbProducer.get());
Valerie Hauc78c43a2020-01-09 17:34:14 -0800127 ASSERT_EQ(NO_ERROR, igbProducer->setMaxDequeuedBufferCount(2));
Valerie Haud3b90d22019-11-06 09:37:31 -0800128 IGraphicBufferProducer::QueueBufferOutput qbOutput;
129 ASSERT_EQ(NO_ERROR,
130 igbProducer->connect(new DummyProducerListener, NATIVE_WINDOW_API_CPU, false,
131 &qbOutput));
Dominik Laskowski718f9602019-11-09 20:01:35 -0800132 ASSERT_NE(ui::Transform::ROT_INVALID, qbOutput.transformHint);
Valerie Haud3b90d22019-11-06 09:37:31 -0800133 producer = igbProducer;
134 }
135
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800136 void fillBuffer(uint32_t* bufData, Rect rect, uint32_t stride, uint8_t r, uint8_t g,
137 uint8_t b) {
138 for (uint32_t row = rect.top; row < rect.bottom; row++) {
139 for (uint32_t col = rect.left; col < rect.right; col++) {
Valerie Hauda3446e2019-10-14 15:49:22 -0700140 uint8_t* pixel = (uint8_t*)(bufData + (row * stride) + col);
141 *pixel = r;
142 *(pixel + 1) = g;
143 *(pixel + 2) = b;
144 *(pixel + 3) = 255;
145 }
146 }
147 }
148
Valerie Hau5977fc82019-12-05 15:56:39 -0800149 void fillQuadrants(sp<GraphicBuffer>& buf) {
150 const auto bufWidth = buf->getWidth();
151 const auto bufHeight = buf->getHeight();
152 uint32_t* bufData;
153 buf->lock(static_cast<uint32_t>(GraphicBuffer::USAGE_SW_WRITE_OFTEN),
154 reinterpret_cast<void**>(&bufData));
155 fillBuffer(bufData, Rect(0, 0, bufWidth / 2, bufHeight / 2), buf->getStride(), 0, 0, 0);
156 fillBuffer(bufData, Rect(bufWidth / 2, 0, bufWidth, bufHeight / 2), buf->getStride(), 255,
157 0, 0);
158 fillBuffer(bufData, Rect(bufWidth / 2, bufHeight / 2, bufWidth, bufHeight),
159 buf->getStride(), 0, 255, 0);
160 fillBuffer(bufData, Rect(0, bufHeight / 2, bufWidth / 2, bufHeight), buf->getStride(), 0, 0,
161 255);
162 buf->unlock();
163 }
164
165 void checkScreenCapture(uint8_t r, uint8_t g, uint8_t b, Rect region, int32_t border = 0,
166 bool outsideRegion = false) {
167 const auto epsilon = 3;
Valerie Hauda3446e2019-10-14 15:49:22 -0700168 const auto width = mScreenCaptureBuf->getWidth();
169 const auto height = mScreenCaptureBuf->getHeight();
170 const auto stride = mScreenCaptureBuf->getStride();
171
172 uint32_t* bufData;
173 mScreenCaptureBuf->lock(static_cast<uint32_t>(GraphicBuffer::USAGE_SW_READ_OFTEN),
174 reinterpret_cast<void**>(&bufData));
175
176 for (uint32_t row = 0; row < height; row++) {
177 for (uint32_t col = 0; col < width; col++) {
178 uint8_t* pixel = (uint8_t*)(bufData + (row * stride) + col);
Valerie Hau5977fc82019-12-05 15:56:39 -0800179 bool inRegion;
180 if (!outsideRegion) {
181 inRegion = row >= region.top + border && row < region.bottom - border &&
182 col >= region.left + border && col < region.right - border;
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800183 } else {
Valerie Hau5977fc82019-12-05 15:56:39 -0800184 inRegion = row >= region.top - border && row < region.bottom + border &&
185 col >= region.left - border && col < region.right + border;
186 }
187 if (!outsideRegion && inRegion) {
188 EXPECT_GE(epsilon, abs(r - *(pixel)));
189 EXPECT_GE(epsilon, abs(g - *(pixel + 1)));
190 EXPECT_GE(epsilon, abs(b - *(pixel + 2)));
191 } else if (outsideRegion && !inRegion) {
192 EXPECT_GE(epsilon, abs(r - *(pixel)));
193 EXPECT_GE(epsilon, abs(g - *(pixel + 1)));
194 EXPECT_GE(epsilon, abs(b - *(pixel + 2)));
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800195 }
Valerie Hauda3446e2019-10-14 15:49:22 -0700196 }
197 }
198 mScreenCaptureBuf->unlock();
199 ASSERT_EQ(false, ::testing::Test::HasFailure());
Valerie Hauc5011f92019-10-11 09:52:07 -0700200 }
201
202 sp<SurfaceComposerClient> mClient;
Valerie Hauda3446e2019-10-14 15:49:22 -0700203 sp<ISurfaceComposer> mComposer;
204
205 sp<IBinder> mDisplayToken;
206
Valerie Hauc5011f92019-10-11 09:52:07 -0700207 sp<SurfaceControl> mSurfaceControl;
Valerie Hauda3446e2019-10-14 15:49:22 -0700208 sp<GraphicBuffer> mScreenCaptureBuf;
209
210 uint32_t mDisplayWidth;
211 uint32_t mDisplayHeight;
Valerie Hauc5011f92019-10-11 09:52:07 -0700212};
213
214TEST_F(BLASTBufferQueueTest, CreateBLASTBufferQueue) {
215 // create BLASTBufferQueue adapter associated with this surface
Valerie Hauda3446e2019-10-14 15:49:22 -0700216 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
Valerie Hauc5011f92019-10-11 09:52:07 -0700217 ASSERT_EQ(mSurfaceControl, adapter.getSurfaceControl());
Valerie Hauda3446e2019-10-14 15:49:22 -0700218 ASSERT_EQ(mDisplayWidth, adapter.getWidth());
219 ASSERT_EQ(mDisplayHeight, adapter.getHeight());
Valerie Hauc5011f92019-10-11 09:52:07 -0700220 ASSERT_EQ(nullptr, adapter.getNextTransaction());
221}
222
223TEST_F(BLASTBufferQueueTest, Update) {
Valerie Hauda3446e2019-10-14 15:49:22 -0700224 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
Valerie Hauc5011f92019-10-11 09:52:07 -0700225 sp<SurfaceControl> updateSurface =
Valerie Hauda3446e2019-10-14 15:49:22 -0700226 mClient->createSurface(String8("UpdateTest"), mDisplayWidth / 2, mDisplayHeight / 2,
227 PIXEL_FORMAT_RGBA_8888);
228 adapter.update(updateSurface, mDisplayWidth / 2, mDisplayHeight / 2);
Valerie Hauc5011f92019-10-11 09:52:07 -0700229 ASSERT_EQ(updateSurface, adapter.getSurfaceControl());
Valerie Hauda3446e2019-10-14 15:49:22 -0700230 ASSERT_EQ(mDisplayWidth / 2, adapter.getWidth());
231 ASSERT_EQ(mDisplayHeight / 2, adapter.getHeight());
Valerie Hauc5011f92019-10-11 09:52:07 -0700232}
233
234TEST_F(BLASTBufferQueueTest, SetNextTransaction) {
Valerie Hauda3446e2019-10-14 15:49:22 -0700235 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
Valerie Hauc5011f92019-10-11 09:52:07 -0700236 Transaction next;
237 adapter.setNextTransaction(&next);
238 ASSERT_EQ(&next, adapter.getNextTransaction());
239}
Valerie Hauda3446e2019-10-14 15:49:22 -0700240
Valerie Hau181abd32020-01-27 14:18:28 -0800241TEST_F(BLASTBufferQueueTest, onFrameAvailable_ApplyDesiredPresentTime) {
242 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
243 sp<IGraphicBufferProducer> igbProducer;
244 setUpProducer(adapter, igbProducer);
245
246 int slot;
247 sp<Fence> fence;
248 sp<GraphicBuffer> buf;
249 auto ret = igbProducer->dequeueBuffer(&slot, &fence, mDisplayWidth, mDisplayHeight,
250 PIXEL_FORMAT_RGBA_8888, GRALLOC_USAGE_SW_WRITE_OFTEN,
251 nullptr, nullptr);
252 ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, ret);
253 ASSERT_EQ(OK, igbProducer->requestBuffer(slot, &buf));
254
255 nsecs_t desiredPresentTime = systemTime() + nsecs_t(5 * 1e8);
256 IGraphicBufferProducer::QueueBufferOutput qbOutput;
257 IGraphicBufferProducer::QueueBufferInput input(desiredPresentTime, false, HAL_DATASPACE_UNKNOWN,
258 Rect(mDisplayWidth, mDisplayHeight),
259 NATIVE_WINDOW_SCALING_MODE_FREEZE, 0,
260 Fence::NO_FENCE);
261 igbProducer->queueBuffer(slot, input, &qbOutput);
262 ASSERT_NE(ui::Transform::ROT_INVALID, qbOutput.transformHint);
263
264 adapter.waitForCallbacks();
265 ASSERT_GE(systemTime(), desiredPresentTime);
266}
267
Valerie Hauda3446e2019-10-14 15:49:22 -0700268TEST_F(BLASTBufferQueueTest, onFrameAvailable_Apply) {
269 uint8_t r = 255;
270 uint8_t g = 0;
271 uint8_t b = 0;
272
273 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
Valerie Haud3b90d22019-11-06 09:37:31 -0800274 sp<IGraphicBufferProducer> igbProducer;
275 setUpProducer(adapter, igbProducer);
Valerie Hauda3446e2019-10-14 15:49:22 -0700276
277 int slot;
278 sp<Fence> fence;
279 sp<GraphicBuffer> buf;
280 auto ret = igbProducer->dequeueBuffer(&slot, &fence, mDisplayWidth, mDisplayHeight,
281 PIXEL_FORMAT_RGBA_8888, GRALLOC_USAGE_SW_WRITE_OFTEN,
282 nullptr, nullptr);
283 ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, ret);
284 ASSERT_EQ(OK, igbProducer->requestBuffer(slot, &buf));
285
286 uint32_t* bufData;
287 buf->lock(static_cast<uint32_t>(GraphicBuffer::USAGE_SW_WRITE_OFTEN),
288 reinterpret_cast<void**>(&bufData));
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800289 fillBuffer(bufData, Rect(buf->getWidth(), buf->getHeight()), buf->getStride(), r, g, b);
Valerie Hauda3446e2019-10-14 15:49:22 -0700290 buf->unlock();
291
Valerie Haud3b90d22019-11-06 09:37:31 -0800292 IGraphicBufferProducer::QueueBufferOutput qbOutput;
Valerie Hauda3446e2019-10-14 15:49:22 -0700293 IGraphicBufferProducer::QueueBufferInput input(systemTime(), false, HAL_DATASPACE_UNKNOWN,
294 Rect(mDisplayWidth, mDisplayHeight),
295 NATIVE_WINDOW_SCALING_MODE_FREEZE, 0,
296 Fence::NO_FENCE);
297 igbProducer->queueBuffer(slot, input, &qbOutput);
Dominik Laskowski718f9602019-11-09 20:01:35 -0800298 ASSERT_NE(ui::Transform::ROT_INVALID, qbOutput.transformHint);
Valerie Hauda3446e2019-10-14 15:49:22 -0700299
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800300 adapter.waitForCallbacks();
Valerie Hauda3446e2019-10-14 15:49:22 -0700301
302 // capture screen and verify that it is red
303 bool capturedSecureLayers;
304 ASSERT_EQ(NO_ERROR,
305 mComposer->captureScreen(mDisplayToken, &mScreenCaptureBuf, capturedSecureLayers,
306 ui::Dataspace::V0_SRGB, ui::PixelFormat::RGBA_8888, Rect(),
307 mDisplayWidth, mDisplayHeight,
308 /*useIdentityTransform*/ false));
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800309 ASSERT_NO_FATAL_FAILURE(
310 checkScreenCapture(r, g, b, {0, 0, (int32_t)mDisplayWidth, (int32_t)mDisplayHeight}));
Valerie Hauda3446e2019-10-14 15:49:22 -0700311}
Valerie Haud3b90d22019-11-06 09:37:31 -0800312
313TEST_F(BLASTBufferQueueTest, TripleBuffering) {
314 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
315 sp<IGraphicBufferProducer> igbProducer;
316 setUpProducer(adapter, igbProducer);
317
318 std::vector<std::pair<int, sp<Fence>>> allocated;
319 for (int i = 0; i < 3; i++) {
320 int slot;
321 sp<Fence> fence;
322 sp<GraphicBuffer> buf;
323 auto ret = igbProducer->dequeueBuffer(&slot, &fence, mDisplayWidth, mDisplayHeight,
324 PIXEL_FORMAT_RGBA_8888, GRALLOC_USAGE_SW_WRITE_OFTEN,
325 nullptr, nullptr);
326 ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, ret);
327 ASSERT_EQ(OK, igbProducer->requestBuffer(slot, &buf));
328 allocated.push_back({slot, fence});
329 }
330 for (int i = 0; i < allocated.size(); i++) {
331 igbProducer->cancelBuffer(allocated[i].first, allocated[i].second);
332 }
333
Valerie Haua32c5522019-12-09 10:11:08 -0800334 for (int i = 0; i < 100; i++) {
Valerie Haud3b90d22019-11-06 09:37:31 -0800335 int slot;
336 sp<Fence> fence;
337 sp<GraphicBuffer> buf;
338 auto ret = igbProducer->dequeueBuffer(&slot, &fence, mDisplayWidth, mDisplayHeight,
339 PIXEL_FORMAT_RGBA_8888, GRALLOC_USAGE_SW_WRITE_OFTEN,
340 nullptr, nullptr);
341 ASSERT_EQ(NO_ERROR, ret);
342 IGraphicBufferProducer::QueueBufferOutput qbOutput;
343 IGraphicBufferProducer::QueueBufferInput input(systemTime(), false, HAL_DATASPACE_UNKNOWN,
344 Rect(mDisplayWidth, mDisplayHeight),
345 NATIVE_WINDOW_SCALING_MODE_FREEZE, 0,
346 Fence::NO_FENCE);
347 igbProducer->queueBuffer(slot, input, &qbOutput);
348 }
349 adapter.waitForCallbacks();
350}
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800351
352TEST_F(BLASTBufferQueueTest, SetCrop_Item) {
353 uint8_t r = 255;
354 uint8_t g = 0;
355 uint8_t b = 0;
356
357 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
358 sp<IGraphicBufferProducer> igbProducer;
359 setUpProducer(adapter, igbProducer);
360 int slot;
361 sp<Fence> fence;
362 sp<GraphicBuffer> buf;
363 auto ret = igbProducer->dequeueBuffer(&slot, &fence, mDisplayWidth, mDisplayHeight,
364 PIXEL_FORMAT_RGBA_8888, GRALLOC_USAGE_SW_WRITE_OFTEN,
365 nullptr, nullptr);
366 ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, ret);
367 ASSERT_EQ(OK, igbProducer->requestBuffer(slot, &buf));
368
369 uint32_t* bufData;
370 buf->lock(static_cast<uint32_t>(GraphicBuffer::USAGE_SW_WRITE_OFTEN),
371 reinterpret_cast<void**>(&bufData));
372 fillBuffer(bufData, Rect(buf->getWidth(), buf->getHeight() / 2), buf->getStride(), r, g, b);
373 buf->unlock();
374
375 IGraphicBufferProducer::QueueBufferOutput qbOutput;
376 IGraphicBufferProducer::QueueBufferInput input(systemTime(), false, HAL_DATASPACE_UNKNOWN,
377 Rect(mDisplayWidth, mDisplayHeight / 2),
378 NATIVE_WINDOW_SCALING_MODE_FREEZE, 0,
379 Fence::NO_FENCE);
380 igbProducer->queueBuffer(slot, input, &qbOutput);
Dominik Laskowski718f9602019-11-09 20:01:35 -0800381 ASSERT_NE(ui::Transform::ROT_INVALID, qbOutput.transformHint);
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800382
383 adapter.waitForCallbacks();
384 // capture screen and verify that it is red
385 bool capturedSecureLayers;
386 ASSERT_EQ(NO_ERROR,
387 mComposer->captureScreen(mDisplayToken, &mScreenCaptureBuf, capturedSecureLayers,
388 ui::Dataspace::V0_SRGB, ui::PixelFormat::RGBA_8888, Rect(),
389 mDisplayWidth, mDisplayHeight,
390 /*useIdentityTransform*/ false));
391 ASSERT_NO_FATAL_FAILURE(
392 checkScreenCapture(r, g, b, {0, 0, (int32_t)mDisplayWidth, (int32_t)mDisplayHeight}));
393}
394
395TEST_F(BLASTBufferQueueTest, SetCrop_ScalingModeScaleCrop) {
396 uint8_t r = 255;
397 uint8_t g = 0;
398 uint8_t b = 0;
399
400 int32_t bufferSideLength =
401 (mDisplayWidth < mDisplayHeight) ? mDisplayWidth / 2 : mDisplayHeight / 2;
402 int32_t finalCropSideLength = bufferSideLength / 2;
403
404 auto bg = mClient->createSurface(String8("BGTest"), 0, 0, PIXEL_FORMAT_RGBA_8888,
405 ISurfaceComposerClient::eFXSurfaceColor);
406 ASSERT_NE(nullptr, bg.get());
407 Transaction t;
408 t.setLayerStack(bg, 0)
409 .setCrop_legacy(bg, Rect(0, 0, mDisplayWidth, mDisplayHeight))
410 .setColor(bg, half3{0, 0, 0})
411 .setLayer(bg, 0)
412 .apply();
413
414 BLASTBufferQueueHelper adapter(mSurfaceControl, bufferSideLength, bufferSideLength);
415 sp<IGraphicBufferProducer> igbProducer;
416 setUpProducer(adapter, igbProducer);
417 int slot;
418 sp<Fence> fence;
419 sp<GraphicBuffer> buf;
420 auto ret = igbProducer->dequeueBuffer(&slot, &fence, bufferSideLength, bufferSideLength,
421 PIXEL_FORMAT_RGBA_8888, GRALLOC_USAGE_SW_WRITE_OFTEN,
422 nullptr, nullptr);
423 ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, ret);
424 ASSERT_EQ(OK, igbProducer->requestBuffer(slot, &buf));
425
426 uint32_t* bufData;
427 buf->lock(static_cast<uint32_t>(GraphicBuffer::USAGE_SW_WRITE_OFTEN),
428 reinterpret_cast<void**>(&bufData));
429 fillBuffer(bufData, Rect(buf->getWidth(), buf->getHeight()), buf->getStride(), 0, 0, 0);
430 fillBuffer(bufData,
431 Rect(finalCropSideLength / 2, 0, buf->getWidth() - finalCropSideLength / 2,
432 buf->getHeight()),
433 buf->getStride(), r, g, b);
434 buf->unlock();
435
436 IGraphicBufferProducer::QueueBufferOutput qbOutput;
437 IGraphicBufferProducer::QueueBufferInput input(systemTime(), false, HAL_DATASPACE_UNKNOWN,
438 Rect(bufferSideLength, finalCropSideLength),
439 NATIVE_WINDOW_SCALING_MODE_SCALE_CROP, 0,
440 Fence::NO_FENCE);
441 igbProducer->queueBuffer(slot, input, &qbOutput);
Dominik Laskowski718f9602019-11-09 20:01:35 -0800442 ASSERT_NE(ui::Transform::ROT_INVALID, qbOutput.transformHint);
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800443
444 adapter.waitForCallbacks();
445 // capture screen and verify that it is red
446 bool capturedSecureLayers;
447 ASSERT_EQ(NO_ERROR,
448 mComposer->captureScreen(mDisplayToken, &mScreenCaptureBuf, capturedSecureLayers,
449 ui::Dataspace::V0_SRGB, ui::PixelFormat::RGBA_8888, Rect(),
450 mDisplayWidth, mDisplayHeight,
451 /*useIdentityTransform*/ false));
452 ASSERT_NO_FATAL_FAILURE(
453 checkScreenCapture(r, g, b,
454 {0, 0, (int32_t)bufferSideLength, (int32_t)bufferSideLength}));
Valerie Hau5977fc82019-12-05 15:56:39 -0800455 ASSERT_NO_FATAL_FAILURE(
456 checkScreenCapture(0, 0, 0,
457 {0, 0, (int32_t)bufferSideLength, (int32_t)bufferSideLength},
458 /*border*/ 0, /*outsideRegion*/ true));
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800459}
460
Valerie Hau5977fc82019-12-05 15:56:39 -0800461class BLASTBufferQueueTransformTest : public BLASTBufferQueueTest {
462public:
463 void test(uint32_t tr) {
464 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
465 sp<IGraphicBufferProducer> igbProducer;
466 setUpProducer(adapter, igbProducer);
467
468 auto bufWidth = mDisplayWidth;
469 auto bufHeight = mDisplayHeight;
470 int slot;
471 sp<Fence> fence;
472 sp<GraphicBuffer> buf;
473
474 auto ret = igbProducer->dequeueBuffer(&slot, &fence, bufWidth, bufHeight,
475 PIXEL_FORMAT_RGBA_8888, GRALLOC_USAGE_SW_WRITE_OFTEN,
476 nullptr, nullptr);
477 ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, ret);
478 ASSERT_EQ(OK, igbProducer->requestBuffer(slot, &buf));
479
480 fillQuadrants(buf);
481
482 IGraphicBufferProducer::QueueBufferOutput qbOutput;
483 IGraphicBufferProducer::QueueBufferInput input(systemTime(), false, HAL_DATASPACE_UNKNOWN,
484 Rect(bufWidth, bufHeight),
485 NATIVE_WINDOW_SCALING_MODE_FREEZE, tr,
486 Fence::NO_FENCE);
487 igbProducer->queueBuffer(slot, input, &qbOutput);
Dominik Laskowski718f9602019-11-09 20:01:35 -0800488 ASSERT_NE(ui::Transform::ROT_INVALID, qbOutput.transformHint);
Valerie Hau5977fc82019-12-05 15:56:39 -0800489
490 adapter.waitForCallbacks();
491 bool capturedSecureLayers;
492 ASSERT_EQ(NO_ERROR,
493 mComposer->captureScreen(mDisplayToken, &mScreenCaptureBuf, capturedSecureLayers,
494 ui::Dataspace::V0_SRGB, ui::PixelFormat::RGBA_8888,
495 Rect(), mDisplayWidth, mDisplayHeight,
496 /*useIdentityTransform*/ false));
497 switch (tr) {
498 case ui::Transform::ROT_0:
499 ASSERT_NO_FATAL_FAILURE(checkScreenCapture(0, 0, 0,
500 {0, 0, (int32_t)mDisplayWidth / 2,
501 (int32_t)mDisplayHeight / 2},
502 1));
503 ASSERT_NO_FATAL_FAILURE(
504 checkScreenCapture(255, 0, 0,
505 {(int32_t)mDisplayWidth / 2, 0, (int32_t)mDisplayWidth,
506 (int32_t)mDisplayHeight / 2},
507 1));
508 ASSERT_NO_FATAL_FAILURE(
509 checkScreenCapture(0, 255, 0,
510 {(int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight / 2,
511 (int32_t)mDisplayWidth, (int32_t)mDisplayHeight},
512 1));
513 ASSERT_NO_FATAL_FAILURE(
514 checkScreenCapture(0, 0, 255,
515 {0, (int32_t)mDisplayHeight / 2,
516 (int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight},
517 1));
518 break;
519 case ui::Transform::FLIP_H:
520 ASSERT_NO_FATAL_FAILURE(checkScreenCapture(255, 0, 0,
521 {0, 0, (int32_t)mDisplayWidth / 2,
522 (int32_t)mDisplayHeight / 2},
523 1));
524 ASSERT_NO_FATAL_FAILURE(
525 checkScreenCapture(0, 0, 0,
526 {(int32_t)mDisplayWidth / 2, 0, (int32_t)mDisplayWidth,
527 (int32_t)mDisplayHeight / 2},
528 1));
529 ASSERT_NO_FATAL_FAILURE(
530 checkScreenCapture(0, 0, 255,
531 {(int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight / 2,
532 (int32_t)mDisplayWidth, (int32_t)mDisplayHeight},
533 1));
534 ASSERT_NO_FATAL_FAILURE(
535 checkScreenCapture(0, 255, 0,
536 {0, (int32_t)mDisplayHeight / 2,
537 (int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight},
538 1));
539 break;
540 case ui::Transform::FLIP_V:
541 ASSERT_NO_FATAL_FAILURE(checkScreenCapture(0, 0, 255,
542 {0, 0, (int32_t)mDisplayWidth / 2,
543 (int32_t)mDisplayHeight / 2},
544 1));
545 ASSERT_NO_FATAL_FAILURE(
546 checkScreenCapture(0, 255, 0,
547 {(int32_t)mDisplayWidth / 2, 0, (int32_t)mDisplayWidth,
548 (int32_t)mDisplayHeight / 2},
549 1));
550 ASSERT_NO_FATAL_FAILURE(
551 checkScreenCapture(255, 0, 0,
552 {(int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight / 2,
553 (int32_t)mDisplayWidth, (int32_t)mDisplayHeight},
554 1));
555 ASSERT_NO_FATAL_FAILURE(
556 checkScreenCapture(0, 0, 0,
557 {0, (int32_t)mDisplayHeight / 2,
558 (int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight},
559 1));
560 break;
561 case ui::Transform::ROT_90:
562 ASSERT_NO_FATAL_FAILURE(checkScreenCapture(0, 0, 255,
563 {0, 0, (int32_t)mDisplayWidth / 2,
564 (int32_t)mDisplayHeight / 2},
565 1));
566 ASSERT_NO_FATAL_FAILURE(
567 checkScreenCapture(0, 0, 0,
568 {(int32_t)mDisplayWidth / 2, 0, (int32_t)mDisplayWidth,
569 (int32_t)mDisplayHeight / 2},
570 1));
571 ASSERT_NO_FATAL_FAILURE(
572 checkScreenCapture(255, 0, 0,
573 {(int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight / 2,
574 (int32_t)mDisplayWidth, (int32_t)mDisplayHeight},
575 1));
576 ASSERT_NO_FATAL_FAILURE(
577 checkScreenCapture(0, 255, 0,
578 {0, (int32_t)mDisplayHeight / 2,
579 (int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight},
580 1));
581 break;
582 case ui::Transform::ROT_180:
583 ASSERT_NO_FATAL_FAILURE(checkScreenCapture(0, 255, 0,
584 {0, 0, (int32_t)mDisplayWidth / 2,
585 (int32_t)mDisplayHeight / 2},
586 1));
587 ASSERT_NO_FATAL_FAILURE(
588 checkScreenCapture(0, 0, 255,
589 {(int32_t)mDisplayWidth / 2, 0, (int32_t)mDisplayWidth,
590 (int32_t)mDisplayHeight / 2},
591 1));
592 ASSERT_NO_FATAL_FAILURE(
593 checkScreenCapture(0, 0, 0,
594 {(int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight / 2,
595 (int32_t)mDisplayWidth, (int32_t)mDisplayHeight},
596 1));
597 ASSERT_NO_FATAL_FAILURE(
598 checkScreenCapture(255, 0, 0,
599 {0, (int32_t)mDisplayHeight / 2,
600 (int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight},
601 1));
602 break;
603 case ui::Transform::ROT_270:
604 ASSERT_NO_FATAL_FAILURE(checkScreenCapture(255, 0, 0,
605 {0, 0, (int32_t)mDisplayWidth / 2,
606 (int32_t)mDisplayHeight / 2},
607 1));
608 ASSERT_NO_FATAL_FAILURE(
609 checkScreenCapture(0, 255, 0,
610 {(int32_t)mDisplayWidth / 2, 0, (int32_t)mDisplayWidth,
611 (int32_t)mDisplayHeight / 2},
612 1));
613 ASSERT_NO_FATAL_FAILURE(
614 checkScreenCapture(0, 0, 255,
615 {(int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight / 2,
616 (int32_t)mDisplayWidth, (int32_t)mDisplayHeight},
617 1));
618 ASSERT_NO_FATAL_FAILURE(
619 checkScreenCapture(0, 0, 0,
620 {0, (int32_t)mDisplayHeight / 2,
621 (int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight},
622 1));
623 }
624 }
625};
626
627TEST_F(BLASTBufferQueueTransformTest, setTransform_ROT_0) {
628 test(ui::Transform::ROT_0);
629}
630
631TEST_F(BLASTBufferQueueTransformTest, setTransform_FLIP_H) {
632 test(ui::Transform::FLIP_H);
633}
634
635TEST_F(BLASTBufferQueueTransformTest, setTransform_FLIP_V) {
636 test(ui::Transform::FLIP_V);
637}
638
639TEST_F(BLASTBufferQueueTransformTest, setTransform_ROT_90) {
640 test(ui::Transform::ROT_90);
641}
642
643TEST_F(BLASTBufferQueueTransformTest, setTransform_ROT_180) {
644 test(ui::Transform::ROT_180);
645}
646
647TEST_F(BLASTBufferQueueTransformTest, setTransform_ROT_270) {
648 test(ui::Transform::ROT_270);
649}
Valerie Hauc5011f92019-10-11 09:52:07 -0700650} // namespace android