blob: 0f618f181c8cf0cc152f4b5c33dcd747440bfe54 [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());
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 Hau5977fc82019-12-05 15:56:39 -0800147 void fillQuadrants(sp<GraphicBuffer>& buf) {
148 const auto bufWidth = buf->getWidth();
149 const auto bufHeight = buf->getHeight();
150 uint32_t* bufData;
151 buf->lock(static_cast<uint32_t>(GraphicBuffer::USAGE_SW_WRITE_OFTEN),
152 reinterpret_cast<void**>(&bufData));
153 fillBuffer(bufData, Rect(0, 0, bufWidth / 2, bufHeight / 2), buf->getStride(), 0, 0, 0);
154 fillBuffer(bufData, Rect(bufWidth / 2, 0, bufWidth, bufHeight / 2), buf->getStride(), 255,
155 0, 0);
156 fillBuffer(bufData, Rect(bufWidth / 2, bufHeight / 2, bufWidth, bufHeight),
157 buf->getStride(), 0, 255, 0);
158 fillBuffer(bufData, Rect(0, bufHeight / 2, bufWidth / 2, bufHeight), buf->getStride(), 0, 0,
159 255);
160 buf->unlock();
161 }
162
163 void checkScreenCapture(uint8_t r, uint8_t g, uint8_t b, Rect region, int32_t border = 0,
164 bool outsideRegion = false) {
165 const auto epsilon = 3;
Valerie Hauda3446e2019-10-14 15:49:22 -0700166 const auto width = mScreenCaptureBuf->getWidth();
167 const auto height = mScreenCaptureBuf->getHeight();
168 const auto stride = mScreenCaptureBuf->getStride();
169
170 uint32_t* bufData;
171 mScreenCaptureBuf->lock(static_cast<uint32_t>(GraphicBuffer::USAGE_SW_READ_OFTEN),
172 reinterpret_cast<void**>(&bufData));
173
174 for (uint32_t row = 0; row < height; row++) {
175 for (uint32_t col = 0; col < width; col++) {
176 uint8_t* pixel = (uint8_t*)(bufData + (row * stride) + col);
Valerie Hau5977fc82019-12-05 15:56:39 -0800177 bool inRegion;
178 if (!outsideRegion) {
179 inRegion = row >= region.top + border && row < region.bottom - border &&
180 col >= region.left + border && col < region.right - border;
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800181 } else {
Valerie Hau5977fc82019-12-05 15:56:39 -0800182 inRegion = row >= region.top - border && row < region.bottom + border &&
183 col >= region.left - border && col < region.right + border;
184 }
185 if (!outsideRegion && inRegion) {
186 EXPECT_GE(epsilon, abs(r - *(pixel)));
187 EXPECT_GE(epsilon, abs(g - *(pixel + 1)));
188 EXPECT_GE(epsilon, abs(b - *(pixel + 2)));
189 } else if (outsideRegion && !inRegion) {
190 EXPECT_GE(epsilon, abs(r - *(pixel)));
191 EXPECT_GE(epsilon, abs(g - *(pixel + 1)));
192 EXPECT_GE(epsilon, abs(b - *(pixel + 2)));
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800193 }
Valerie Hauda3446e2019-10-14 15:49:22 -0700194 }
195 }
196 mScreenCaptureBuf->unlock();
197 ASSERT_EQ(false, ::testing::Test::HasFailure());
Valerie Hauc5011f92019-10-11 09:52:07 -0700198 }
199
200 sp<SurfaceComposerClient> mClient;
Valerie Hauda3446e2019-10-14 15:49:22 -0700201 sp<ISurfaceComposer> mComposer;
202
203 sp<IBinder> mDisplayToken;
204
Valerie Hauc5011f92019-10-11 09:52:07 -0700205 sp<SurfaceControl> mSurfaceControl;
Valerie Hauda3446e2019-10-14 15:49:22 -0700206 sp<GraphicBuffer> mScreenCaptureBuf;
207
208 uint32_t mDisplayWidth;
209 uint32_t mDisplayHeight;
Valerie Hauc5011f92019-10-11 09:52:07 -0700210};
211
212TEST_F(BLASTBufferQueueTest, CreateBLASTBufferQueue) {
213 // create BLASTBufferQueue adapter associated with this surface
Valerie Hauda3446e2019-10-14 15:49:22 -0700214 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
Valerie Hauc5011f92019-10-11 09:52:07 -0700215 ASSERT_EQ(mSurfaceControl, adapter.getSurfaceControl());
Valerie Hauda3446e2019-10-14 15:49:22 -0700216 ASSERT_EQ(mDisplayWidth, adapter.getWidth());
217 ASSERT_EQ(mDisplayHeight, adapter.getHeight());
Valerie Hauc5011f92019-10-11 09:52:07 -0700218 ASSERT_EQ(nullptr, adapter.getNextTransaction());
219}
220
221TEST_F(BLASTBufferQueueTest, Update) {
Valerie Hauda3446e2019-10-14 15:49:22 -0700222 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
Valerie Hauc5011f92019-10-11 09:52:07 -0700223 sp<SurfaceControl> updateSurface =
Valerie Hauda3446e2019-10-14 15:49:22 -0700224 mClient->createSurface(String8("UpdateTest"), mDisplayWidth / 2, mDisplayHeight / 2,
225 PIXEL_FORMAT_RGBA_8888);
226 adapter.update(updateSurface, mDisplayWidth / 2, mDisplayHeight / 2);
Valerie Hauc5011f92019-10-11 09:52:07 -0700227 ASSERT_EQ(updateSurface, adapter.getSurfaceControl());
Valerie Hauda3446e2019-10-14 15:49:22 -0700228 ASSERT_EQ(mDisplayWidth / 2, adapter.getWidth());
229 ASSERT_EQ(mDisplayHeight / 2, adapter.getHeight());
Valerie Hauc5011f92019-10-11 09:52:07 -0700230}
231
232TEST_F(BLASTBufferQueueTest, SetNextTransaction) {
Valerie Hauda3446e2019-10-14 15:49:22 -0700233 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
Valerie Hauc5011f92019-10-11 09:52:07 -0700234 Transaction next;
235 adapter.setNextTransaction(&next);
236 ASSERT_EQ(&next, adapter.getNextTransaction());
237}
Valerie Hauda3446e2019-10-14 15:49:22 -0700238
239TEST_F(BLASTBufferQueueTest, onFrameAvailable_Apply) {
240 uint8_t r = 255;
241 uint8_t g = 0;
242 uint8_t b = 0;
243
244 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
Valerie Haud3b90d22019-11-06 09:37:31 -0800245 sp<IGraphicBufferProducer> igbProducer;
246 setUpProducer(adapter, igbProducer);
Valerie Hauda3446e2019-10-14 15:49:22 -0700247
248 int slot;
249 sp<Fence> fence;
250 sp<GraphicBuffer> buf;
251 auto ret = igbProducer->dequeueBuffer(&slot, &fence, mDisplayWidth, mDisplayHeight,
252 PIXEL_FORMAT_RGBA_8888, GRALLOC_USAGE_SW_WRITE_OFTEN,
253 nullptr, nullptr);
254 ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, ret);
255 ASSERT_EQ(OK, igbProducer->requestBuffer(slot, &buf));
256
257 uint32_t* bufData;
258 buf->lock(static_cast<uint32_t>(GraphicBuffer::USAGE_SW_WRITE_OFTEN),
259 reinterpret_cast<void**>(&bufData));
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800260 fillBuffer(bufData, Rect(buf->getWidth(), buf->getHeight()), buf->getStride(), r, g, b);
Valerie Hauda3446e2019-10-14 15:49:22 -0700261 buf->unlock();
262
Valerie Haud3b90d22019-11-06 09:37:31 -0800263 IGraphicBufferProducer::QueueBufferOutput qbOutput;
Valerie Hauda3446e2019-10-14 15:49:22 -0700264 IGraphicBufferProducer::QueueBufferInput input(systemTime(), false, HAL_DATASPACE_UNKNOWN,
265 Rect(mDisplayWidth, mDisplayHeight),
266 NATIVE_WINDOW_SCALING_MODE_FREEZE, 0,
267 Fence::NO_FENCE);
268 igbProducer->queueBuffer(slot, input, &qbOutput);
Valerie Hau8cee3f92019-11-06 10:06:28 -0800269 ASSERT_NE(ui::Transform::orientation_flags::ROT_INVALID, qbOutput.transformHint);
Valerie Hauda3446e2019-10-14 15:49:22 -0700270
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800271 adapter.waitForCallbacks();
Valerie Hauda3446e2019-10-14 15:49:22 -0700272
273 // capture screen and verify that it is red
274 bool capturedSecureLayers;
275 ASSERT_EQ(NO_ERROR,
276 mComposer->captureScreen(mDisplayToken, &mScreenCaptureBuf, capturedSecureLayers,
277 ui::Dataspace::V0_SRGB, ui::PixelFormat::RGBA_8888, Rect(),
278 mDisplayWidth, mDisplayHeight,
279 /*useIdentityTransform*/ false));
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800280 ASSERT_NO_FATAL_FAILURE(
281 checkScreenCapture(r, g, b, {0, 0, (int32_t)mDisplayWidth, (int32_t)mDisplayHeight}));
Valerie Hauda3446e2019-10-14 15:49:22 -0700282}
Valerie Haud3b90d22019-11-06 09:37:31 -0800283
284TEST_F(BLASTBufferQueueTest, TripleBuffering) {
285 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
286 sp<IGraphicBufferProducer> igbProducer;
287 setUpProducer(adapter, igbProducer);
288
289 std::vector<std::pair<int, sp<Fence>>> allocated;
290 for (int i = 0; i < 3; i++) {
291 int slot;
292 sp<Fence> fence;
293 sp<GraphicBuffer> buf;
294 auto ret = igbProducer->dequeueBuffer(&slot, &fence, mDisplayWidth, mDisplayHeight,
295 PIXEL_FORMAT_RGBA_8888, GRALLOC_USAGE_SW_WRITE_OFTEN,
296 nullptr, nullptr);
297 ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, ret);
298 ASSERT_EQ(OK, igbProducer->requestBuffer(slot, &buf));
299 allocated.push_back({slot, fence});
300 }
301 for (int i = 0; i < allocated.size(); i++) {
302 igbProducer->cancelBuffer(allocated[i].first, allocated[i].second);
303 }
304
Valerie Haua32c5522019-12-09 10:11:08 -0800305 for (int i = 0; i < 100; i++) {
Valerie Haud3b90d22019-11-06 09:37:31 -0800306 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(NO_ERROR, ret);
313 IGraphicBufferProducer::QueueBufferOutput qbOutput;
314 IGraphicBufferProducer::QueueBufferInput input(systemTime(), false, HAL_DATASPACE_UNKNOWN,
315 Rect(mDisplayWidth, mDisplayHeight),
316 NATIVE_WINDOW_SCALING_MODE_FREEZE, 0,
317 Fence::NO_FENCE);
318 igbProducer->queueBuffer(slot, input, &qbOutput);
319 }
320 adapter.waitForCallbacks();
321}
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800322
323TEST_F(BLASTBufferQueueTest, SetCrop_Item) {
324 uint8_t r = 255;
325 uint8_t g = 0;
326 uint8_t b = 0;
327
328 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
329 sp<IGraphicBufferProducer> igbProducer;
330 setUpProducer(adapter, igbProducer);
331 int slot;
332 sp<Fence> fence;
333 sp<GraphicBuffer> buf;
334 auto ret = igbProducer->dequeueBuffer(&slot, &fence, mDisplayWidth, mDisplayHeight,
335 PIXEL_FORMAT_RGBA_8888, GRALLOC_USAGE_SW_WRITE_OFTEN,
336 nullptr, nullptr);
337 ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, ret);
338 ASSERT_EQ(OK, igbProducer->requestBuffer(slot, &buf));
339
340 uint32_t* bufData;
341 buf->lock(static_cast<uint32_t>(GraphicBuffer::USAGE_SW_WRITE_OFTEN),
342 reinterpret_cast<void**>(&bufData));
343 fillBuffer(bufData, Rect(buf->getWidth(), buf->getHeight() / 2), buf->getStride(), r, g, b);
344 buf->unlock();
345
346 IGraphicBufferProducer::QueueBufferOutput qbOutput;
347 IGraphicBufferProducer::QueueBufferInput input(systemTime(), false, HAL_DATASPACE_UNKNOWN,
348 Rect(mDisplayWidth, mDisplayHeight / 2),
349 NATIVE_WINDOW_SCALING_MODE_FREEZE, 0,
350 Fence::NO_FENCE);
351 igbProducer->queueBuffer(slot, input, &qbOutput);
352 ASSERT_NE(ui::Transform::orientation_flags::ROT_INVALID, qbOutput.transformHint);
353
354 adapter.waitForCallbacks();
355 // capture screen and verify that it is red
356 bool capturedSecureLayers;
357 ASSERT_EQ(NO_ERROR,
358 mComposer->captureScreen(mDisplayToken, &mScreenCaptureBuf, capturedSecureLayers,
359 ui::Dataspace::V0_SRGB, ui::PixelFormat::RGBA_8888, Rect(),
360 mDisplayWidth, mDisplayHeight,
361 /*useIdentityTransform*/ false));
362 ASSERT_NO_FATAL_FAILURE(
363 checkScreenCapture(r, g, b, {0, 0, (int32_t)mDisplayWidth, (int32_t)mDisplayHeight}));
364}
365
366TEST_F(BLASTBufferQueueTest, SetCrop_ScalingModeScaleCrop) {
367 uint8_t r = 255;
368 uint8_t g = 0;
369 uint8_t b = 0;
370
371 int32_t bufferSideLength =
372 (mDisplayWidth < mDisplayHeight) ? mDisplayWidth / 2 : mDisplayHeight / 2;
373 int32_t finalCropSideLength = bufferSideLength / 2;
374
375 auto bg = mClient->createSurface(String8("BGTest"), 0, 0, PIXEL_FORMAT_RGBA_8888,
376 ISurfaceComposerClient::eFXSurfaceColor);
377 ASSERT_NE(nullptr, bg.get());
378 Transaction t;
379 t.setLayerStack(bg, 0)
380 .setCrop_legacy(bg, Rect(0, 0, mDisplayWidth, mDisplayHeight))
381 .setColor(bg, half3{0, 0, 0})
382 .setLayer(bg, 0)
383 .apply();
384
385 BLASTBufferQueueHelper adapter(mSurfaceControl, bufferSideLength, bufferSideLength);
386 sp<IGraphicBufferProducer> igbProducer;
387 setUpProducer(adapter, igbProducer);
388 int slot;
389 sp<Fence> fence;
390 sp<GraphicBuffer> buf;
391 auto ret = igbProducer->dequeueBuffer(&slot, &fence, bufferSideLength, bufferSideLength,
392 PIXEL_FORMAT_RGBA_8888, GRALLOC_USAGE_SW_WRITE_OFTEN,
393 nullptr, nullptr);
394 ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, ret);
395 ASSERT_EQ(OK, igbProducer->requestBuffer(slot, &buf));
396
397 uint32_t* bufData;
398 buf->lock(static_cast<uint32_t>(GraphicBuffer::USAGE_SW_WRITE_OFTEN),
399 reinterpret_cast<void**>(&bufData));
400 fillBuffer(bufData, Rect(buf->getWidth(), buf->getHeight()), buf->getStride(), 0, 0, 0);
401 fillBuffer(bufData,
402 Rect(finalCropSideLength / 2, 0, buf->getWidth() - finalCropSideLength / 2,
403 buf->getHeight()),
404 buf->getStride(), r, g, b);
405 buf->unlock();
406
407 IGraphicBufferProducer::QueueBufferOutput qbOutput;
408 IGraphicBufferProducer::QueueBufferInput input(systemTime(), false, HAL_DATASPACE_UNKNOWN,
409 Rect(bufferSideLength, finalCropSideLength),
410 NATIVE_WINDOW_SCALING_MODE_SCALE_CROP, 0,
411 Fence::NO_FENCE);
412 igbProducer->queueBuffer(slot, input, &qbOutput);
413 ASSERT_NE(ui::Transform::orientation_flags::ROT_INVALID, qbOutput.transformHint);
414
415 adapter.waitForCallbacks();
416 // capture screen and verify that it is red
417 bool capturedSecureLayers;
418 ASSERT_EQ(NO_ERROR,
419 mComposer->captureScreen(mDisplayToken, &mScreenCaptureBuf, capturedSecureLayers,
420 ui::Dataspace::V0_SRGB, ui::PixelFormat::RGBA_8888, Rect(),
421 mDisplayWidth, mDisplayHeight,
422 /*useIdentityTransform*/ false));
423 ASSERT_NO_FATAL_FAILURE(
424 checkScreenCapture(r, g, b,
425 {0, 0, (int32_t)bufferSideLength, (int32_t)bufferSideLength}));
Valerie Hau5977fc82019-12-05 15:56:39 -0800426 ASSERT_NO_FATAL_FAILURE(
427 checkScreenCapture(0, 0, 0,
428 {0, 0, (int32_t)bufferSideLength, (int32_t)bufferSideLength},
429 /*border*/ 0, /*outsideRegion*/ true));
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800430}
431
Valerie Hau5977fc82019-12-05 15:56:39 -0800432class BLASTBufferQueueTransformTest : public BLASTBufferQueueTest {
433public:
434 void test(uint32_t tr) {
435 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
436 sp<IGraphicBufferProducer> igbProducer;
437 setUpProducer(adapter, igbProducer);
438
439 auto bufWidth = mDisplayWidth;
440 auto bufHeight = mDisplayHeight;
441 int slot;
442 sp<Fence> fence;
443 sp<GraphicBuffer> buf;
444
445 auto ret = igbProducer->dequeueBuffer(&slot, &fence, bufWidth, bufHeight,
446 PIXEL_FORMAT_RGBA_8888, GRALLOC_USAGE_SW_WRITE_OFTEN,
447 nullptr, nullptr);
448 ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, ret);
449 ASSERT_EQ(OK, igbProducer->requestBuffer(slot, &buf));
450
451 fillQuadrants(buf);
452
453 IGraphicBufferProducer::QueueBufferOutput qbOutput;
454 IGraphicBufferProducer::QueueBufferInput input(systemTime(), false, HAL_DATASPACE_UNKNOWN,
455 Rect(bufWidth, bufHeight),
456 NATIVE_WINDOW_SCALING_MODE_FREEZE, tr,
457 Fence::NO_FENCE);
458 igbProducer->queueBuffer(slot, input, &qbOutput);
459 ASSERT_NE(ui::Transform::orientation_flags::ROT_INVALID, qbOutput.transformHint);
460
461 adapter.waitForCallbacks();
462 bool capturedSecureLayers;
463 ASSERT_EQ(NO_ERROR,
464 mComposer->captureScreen(mDisplayToken, &mScreenCaptureBuf, capturedSecureLayers,
465 ui::Dataspace::V0_SRGB, ui::PixelFormat::RGBA_8888,
466 Rect(), mDisplayWidth, mDisplayHeight,
467 /*useIdentityTransform*/ false));
468 switch (tr) {
469 case ui::Transform::ROT_0:
470 ASSERT_NO_FATAL_FAILURE(checkScreenCapture(0, 0, 0,
471 {0, 0, (int32_t)mDisplayWidth / 2,
472 (int32_t)mDisplayHeight / 2},
473 1));
474 ASSERT_NO_FATAL_FAILURE(
475 checkScreenCapture(255, 0, 0,
476 {(int32_t)mDisplayWidth / 2, 0, (int32_t)mDisplayWidth,
477 (int32_t)mDisplayHeight / 2},
478 1));
479 ASSERT_NO_FATAL_FAILURE(
480 checkScreenCapture(0, 255, 0,
481 {(int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight / 2,
482 (int32_t)mDisplayWidth, (int32_t)mDisplayHeight},
483 1));
484 ASSERT_NO_FATAL_FAILURE(
485 checkScreenCapture(0, 0, 255,
486 {0, (int32_t)mDisplayHeight / 2,
487 (int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight},
488 1));
489 break;
490 case ui::Transform::FLIP_H:
491 ASSERT_NO_FATAL_FAILURE(checkScreenCapture(255, 0, 0,
492 {0, 0, (int32_t)mDisplayWidth / 2,
493 (int32_t)mDisplayHeight / 2},
494 1));
495 ASSERT_NO_FATAL_FAILURE(
496 checkScreenCapture(0, 0, 0,
497 {(int32_t)mDisplayWidth / 2, 0, (int32_t)mDisplayWidth,
498 (int32_t)mDisplayHeight / 2},
499 1));
500 ASSERT_NO_FATAL_FAILURE(
501 checkScreenCapture(0, 0, 255,
502 {(int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight / 2,
503 (int32_t)mDisplayWidth, (int32_t)mDisplayHeight},
504 1));
505 ASSERT_NO_FATAL_FAILURE(
506 checkScreenCapture(0, 255, 0,
507 {0, (int32_t)mDisplayHeight / 2,
508 (int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight},
509 1));
510 break;
511 case ui::Transform::FLIP_V:
512 ASSERT_NO_FATAL_FAILURE(checkScreenCapture(0, 0, 255,
513 {0, 0, (int32_t)mDisplayWidth / 2,
514 (int32_t)mDisplayHeight / 2},
515 1));
516 ASSERT_NO_FATAL_FAILURE(
517 checkScreenCapture(0, 255, 0,
518 {(int32_t)mDisplayWidth / 2, 0, (int32_t)mDisplayWidth,
519 (int32_t)mDisplayHeight / 2},
520 1));
521 ASSERT_NO_FATAL_FAILURE(
522 checkScreenCapture(255, 0, 0,
523 {(int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight / 2,
524 (int32_t)mDisplayWidth, (int32_t)mDisplayHeight},
525 1));
526 ASSERT_NO_FATAL_FAILURE(
527 checkScreenCapture(0, 0, 0,
528 {0, (int32_t)mDisplayHeight / 2,
529 (int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight},
530 1));
531 break;
532 case ui::Transform::ROT_90:
533 ASSERT_NO_FATAL_FAILURE(checkScreenCapture(0, 0, 255,
534 {0, 0, (int32_t)mDisplayWidth / 2,
535 (int32_t)mDisplayHeight / 2},
536 1));
537 ASSERT_NO_FATAL_FAILURE(
538 checkScreenCapture(0, 0, 0,
539 {(int32_t)mDisplayWidth / 2, 0, (int32_t)mDisplayWidth,
540 (int32_t)mDisplayHeight / 2},
541 1));
542 ASSERT_NO_FATAL_FAILURE(
543 checkScreenCapture(255, 0, 0,
544 {(int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight / 2,
545 (int32_t)mDisplayWidth, (int32_t)mDisplayHeight},
546 1));
547 ASSERT_NO_FATAL_FAILURE(
548 checkScreenCapture(0, 255, 0,
549 {0, (int32_t)mDisplayHeight / 2,
550 (int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight},
551 1));
552 break;
553 case ui::Transform::ROT_180:
554 ASSERT_NO_FATAL_FAILURE(checkScreenCapture(0, 255, 0,
555 {0, 0, (int32_t)mDisplayWidth / 2,
556 (int32_t)mDisplayHeight / 2},
557 1));
558 ASSERT_NO_FATAL_FAILURE(
559 checkScreenCapture(0, 0, 255,
560 {(int32_t)mDisplayWidth / 2, 0, (int32_t)mDisplayWidth,
561 (int32_t)mDisplayHeight / 2},
562 1));
563 ASSERT_NO_FATAL_FAILURE(
564 checkScreenCapture(0, 0, 0,
565 {(int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight / 2,
566 (int32_t)mDisplayWidth, (int32_t)mDisplayHeight},
567 1));
568 ASSERT_NO_FATAL_FAILURE(
569 checkScreenCapture(255, 0, 0,
570 {0, (int32_t)mDisplayHeight / 2,
571 (int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight},
572 1));
573 break;
574 case ui::Transform::ROT_270:
575 ASSERT_NO_FATAL_FAILURE(checkScreenCapture(255, 0, 0,
576 {0, 0, (int32_t)mDisplayWidth / 2,
577 (int32_t)mDisplayHeight / 2},
578 1));
579 ASSERT_NO_FATAL_FAILURE(
580 checkScreenCapture(0, 255, 0,
581 {(int32_t)mDisplayWidth / 2, 0, (int32_t)mDisplayWidth,
582 (int32_t)mDisplayHeight / 2},
583 1));
584 ASSERT_NO_FATAL_FAILURE(
585 checkScreenCapture(0, 0, 255,
586 {(int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight / 2,
587 (int32_t)mDisplayWidth, (int32_t)mDisplayHeight},
588 1));
589 ASSERT_NO_FATAL_FAILURE(
590 checkScreenCapture(0, 0, 0,
591 {0, (int32_t)mDisplayHeight / 2,
592 (int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight},
593 1));
594 }
595 }
596};
597
598TEST_F(BLASTBufferQueueTransformTest, setTransform_ROT_0) {
599 test(ui::Transform::ROT_0);
600}
601
602TEST_F(BLASTBufferQueueTransformTest, setTransform_FLIP_H) {
603 test(ui::Transform::FLIP_H);
604}
605
606TEST_F(BLASTBufferQueueTransformTest, setTransform_FLIP_V) {
607 test(ui::Transform::FLIP_V);
608}
609
610TEST_F(BLASTBufferQueueTransformTest, setTransform_ROT_90) {
611 test(ui::Transform::ROT_90);
612}
613
614TEST_F(BLASTBufferQueueTransformTest, setTransform_ROT_180) {
615 test(ui::Transform::ROT_180);
616}
617
618TEST_F(BLASTBufferQueueTransformTest, setTransform_ROT_270) {
619 test(ui::Transform::ROT_270);
620}
Valerie Hauc5011f92019-10-11 09:52:07 -0700621} // namespace android