blob: 9299721be387eac316b6c5f4bad1bf57b5dbcc6e [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 Hau871d6352020-01-29 08:44:02 -080024#include <gui/FrameTimestamps.h>
Valerie Hauda3446e2019-10-14 15:49:22 -070025#include <gui/IGraphicBufferProducer.h>
26#include <gui/IProducerListener.h>
Valerie Hauc5011f92019-10-11 09:52:07 -070027#include <gui/SurfaceComposerClient.h>
chaviwe7b9f272020-08-18 16:08:59 -070028#include <gui/SyncScreenCaptureListener.h>
Valerie Hauda3446e2019-10-14 15:49:22 -070029#include <private/gui/ComposerService.h>
Dominik Laskowski3cb3d4e2019-11-21 11:14:45 -080030#include <ui/DisplayConfig.h>
Valerie Hauc5011f92019-10-11 09:52:07 -070031#include <ui/GraphicBuffer.h>
Valerie Hauda3446e2019-10-14 15:49:22 -070032#include <ui/GraphicTypes.h>
Valerie Hau8cee3f92019-11-06 10:06:28 -080033#include <ui/Transform.h>
Valerie Hauc5011f92019-10-11 09:52:07 -070034
35#include <gtest/gtest.h>
36
37using namespace std::chrono_literals;
38
39namespace android {
40
Valerie Hauc5011f92019-10-11 09:52:07 -070041using Transaction = SurfaceComposerClient::Transaction;
Valerie Hauda3446e2019-10-14 15:49:22 -070042using android::hardware::graphics::common::V1_2::BufferUsage;
Valerie Hauc5011f92019-10-11 09:52:07 -070043
44class BLASTBufferQueueHelper {
45public:
46 BLASTBufferQueueHelper(const sp<SurfaceControl>& sc, int width, int height) {
Vishnu Nairdab94092020-09-29 16:09:04 -070047 mBlastBufferQueueAdapter = new BLASTBufferQueue("TestBLASTBufferQueue", sc, width, height);
Valerie Hauc5011f92019-10-11 09:52:07 -070048 }
49
50 void update(const sp<SurfaceControl>& sc, int width, int height) {
51 mBlastBufferQueueAdapter->update(sc, width, height);
52 }
53
54 void setNextTransaction(Transaction* next) {
55 mBlastBufferQueueAdapter->setNextTransaction(next);
56 }
57
58 int getWidth() { return mBlastBufferQueueAdapter->mWidth; }
Valerie Hauda3446e2019-10-14 15:49:22 -070059
Valerie Hauc5011f92019-10-11 09:52:07 -070060 int getHeight() { return mBlastBufferQueueAdapter->mHeight; }
Valerie Hauda3446e2019-10-14 15:49:22 -070061
Valerie Hauc5011f92019-10-11 09:52:07 -070062 Transaction* getNextTransaction() { return mBlastBufferQueueAdapter->mNextTransaction; }
Valerie Hauda3446e2019-10-14 15:49:22 -070063
64 sp<IGraphicBufferProducer> getIGraphicBufferProducer() {
65 return mBlastBufferQueueAdapter->getIGraphicBufferProducer();
66 }
67
Valerie Hauc5011f92019-10-11 09:52:07 -070068 const sp<SurfaceControl> getSurfaceControl() {
69 return mBlastBufferQueueAdapter->mSurfaceControl;
70 }
71
Valerie Haud3b90d22019-11-06 09:37:31 -080072 void waitForCallbacks() {
Valerie Hauda3446e2019-10-14 15:49:22 -070073 std::unique_lock lock{mBlastBufferQueueAdapter->mMutex};
Valerie Haua32c5522019-12-09 10:11:08 -080074 while (mBlastBufferQueueAdapter->mSubmitted.size() > 0) {
Valerie Haud3b90d22019-11-06 09:37:31 -080075 mBlastBufferQueueAdapter->mCallbackCV.wait(lock);
76 }
Valerie Hauda3446e2019-10-14 15:49:22 -070077 }
78
Valerie Hauc5011f92019-10-11 09:52:07 -070079private:
80 sp<BLASTBufferQueue> mBlastBufferQueueAdapter;
81};
82
83class BLASTBufferQueueTest : public ::testing::Test {
84public:
85protected:
86 BLASTBufferQueueTest() {
87 const ::testing::TestInfo* const testInfo =
88 ::testing::UnitTest::GetInstance()->current_test_info();
89 ALOGV("Begin test: %s.%s", testInfo->test_case_name(), testInfo->name());
90 }
91
92 ~BLASTBufferQueueTest() {
93 const ::testing::TestInfo* const testInfo =
94 ::testing::UnitTest::GetInstance()->current_test_info();
95 ALOGV("End test: %s.%s", testInfo->test_case_name(), testInfo->name());
96 }
97
98 void SetUp() {
Valerie Hauda3446e2019-10-14 15:49:22 -070099 mComposer = ComposerService::getComposerService();
Valerie Hauc5011f92019-10-11 09:52:07 -0700100 mClient = new SurfaceComposerClient();
Valerie Hauda3446e2019-10-14 15:49:22 -0700101 mDisplayToken = mClient->getInternalDisplayToken();
102 ASSERT_NE(nullptr, mDisplayToken.get());
103 Transaction t;
104 t.setDisplayLayerStack(mDisplayToken, 0);
105 t.apply();
106 t.clear();
107
Dominik Laskowski3cb3d4e2019-11-21 11:14:45 -0800108 DisplayConfig config;
109 ASSERT_EQ(NO_ERROR, SurfaceComposerClient::getActiveDisplayConfig(mDisplayToken, &config));
110 const ui::Size& resolution = config.resolution;
111 mDisplayWidth = resolution.getWidth();
112 mDisplayHeight = resolution.getHeight();
Valerie Hauda3446e2019-10-14 15:49:22 -0700113
114 mSurfaceControl = mClient->createSurface(String8("TestSurface"), mDisplayWidth,
115 mDisplayHeight, PIXEL_FORMAT_RGBA_8888,
116 ISurfaceComposerClient::eFXSurfaceBufferState,
117 /*parent*/ nullptr);
118 t.setLayerStack(mSurfaceControl, 0)
119 .setLayer(mSurfaceControl, std::numeric_limits<int32_t>::max())
Dominik Laskowski3cb3d4e2019-11-21 11:14:45 -0800120 .setFrame(mSurfaceControl, Rect(resolution))
Valerie Hauda3446e2019-10-14 15:49:22 -0700121 .show(mSurfaceControl)
122 .setDataspace(mSurfaceControl, ui::Dataspace::V0_SRGB)
123 .apply();
chaviwd2432892020-07-24 17:42:39 -0700124
125 mCaptureArgs.displayToken = mDisplayToken;
Valerie Hauda3446e2019-10-14 15:49:22 -0700126 }
127
Valerie Haud3b90d22019-11-06 09:37:31 -0800128 void setUpProducer(BLASTBufferQueueHelper adapter, sp<IGraphicBufferProducer>& producer) {
129 auto igbProducer = adapter.getIGraphicBufferProducer();
130 ASSERT_NE(nullptr, igbProducer.get());
Valerie Hauc78c43a2020-01-09 17:34:14 -0800131 ASSERT_EQ(NO_ERROR, igbProducer->setMaxDequeuedBufferCount(2));
Valerie Haud3b90d22019-11-06 09:37:31 -0800132 IGraphicBufferProducer::QueueBufferOutput qbOutput;
133 ASSERT_EQ(NO_ERROR,
Peiyong Lind8460c82020-07-28 16:04:22 -0700134 igbProducer->connect(new StubProducerListener, NATIVE_WINDOW_API_CPU, false,
Valerie Haud3b90d22019-11-06 09:37:31 -0800135 &qbOutput));
Dominik Laskowski718f9602019-11-09 20:01:35 -0800136 ASSERT_NE(ui::Transform::ROT_INVALID, qbOutput.transformHint);
Valerie Haud3b90d22019-11-06 09:37:31 -0800137 producer = igbProducer;
138 }
139
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800140 void fillBuffer(uint32_t* bufData, Rect rect, uint32_t stride, uint8_t r, uint8_t g,
141 uint8_t b) {
142 for (uint32_t row = rect.top; row < rect.bottom; row++) {
143 for (uint32_t col = rect.left; col < rect.right; col++) {
Valerie Hauda3446e2019-10-14 15:49:22 -0700144 uint8_t* pixel = (uint8_t*)(bufData + (row * stride) + col);
145 *pixel = r;
146 *(pixel + 1) = g;
147 *(pixel + 2) = b;
148 *(pixel + 3) = 255;
149 }
150 }
151 }
152
Valerie Hau5977fc82019-12-05 15:56:39 -0800153 void fillQuadrants(sp<GraphicBuffer>& buf) {
154 const auto bufWidth = buf->getWidth();
155 const auto bufHeight = buf->getHeight();
156 uint32_t* bufData;
157 buf->lock(static_cast<uint32_t>(GraphicBuffer::USAGE_SW_WRITE_OFTEN),
158 reinterpret_cast<void**>(&bufData));
159 fillBuffer(bufData, Rect(0, 0, bufWidth / 2, bufHeight / 2), buf->getStride(), 0, 0, 0);
160 fillBuffer(bufData, Rect(bufWidth / 2, 0, bufWidth, bufHeight / 2), buf->getStride(), 255,
161 0, 0);
162 fillBuffer(bufData, Rect(bufWidth / 2, bufHeight / 2, bufWidth, bufHeight),
163 buf->getStride(), 0, 255, 0);
164 fillBuffer(bufData, Rect(0, bufHeight / 2, bufWidth / 2, bufHeight), buf->getStride(), 0, 0,
165 255);
166 buf->unlock();
167 }
168
169 void checkScreenCapture(uint8_t r, uint8_t g, uint8_t b, Rect region, int32_t border = 0,
170 bool outsideRegion = false) {
chaviwd2432892020-07-24 17:42:39 -0700171 sp<GraphicBuffer>& captureBuf = mCaptureResults.buffer;
Valerie Hau5977fc82019-12-05 15:56:39 -0800172 const auto epsilon = 3;
chaviwd2432892020-07-24 17:42:39 -0700173 const auto width = captureBuf->getWidth();
174 const auto height = captureBuf->getHeight();
175 const auto stride = captureBuf->getStride();
Valerie Hauda3446e2019-10-14 15:49:22 -0700176
177 uint32_t* bufData;
chaviwd2432892020-07-24 17:42:39 -0700178 captureBuf->lock(static_cast<uint32_t>(GraphicBuffer::USAGE_SW_READ_OFTEN),
179 reinterpret_cast<void**>(&bufData));
Valerie Hauda3446e2019-10-14 15:49:22 -0700180
181 for (uint32_t row = 0; row < height; row++) {
182 for (uint32_t col = 0; col < width; col++) {
183 uint8_t* pixel = (uint8_t*)(bufData + (row * stride) + col);
Valerie Hau5977fc82019-12-05 15:56:39 -0800184 bool inRegion;
185 if (!outsideRegion) {
186 inRegion = row >= region.top + border && row < region.bottom - border &&
187 col >= region.left + border && col < region.right - border;
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800188 } else {
Valerie Hau5977fc82019-12-05 15:56:39 -0800189 inRegion = row >= region.top - border && row < region.bottom + border &&
190 col >= region.left - border && col < region.right + border;
191 }
192 if (!outsideRegion && inRegion) {
193 EXPECT_GE(epsilon, abs(r - *(pixel)));
194 EXPECT_GE(epsilon, abs(g - *(pixel + 1)));
195 EXPECT_GE(epsilon, abs(b - *(pixel + 2)));
196 } else if (outsideRegion && !inRegion) {
197 EXPECT_GE(epsilon, abs(r - *(pixel)));
198 EXPECT_GE(epsilon, abs(g - *(pixel + 1)));
199 EXPECT_GE(epsilon, abs(b - *(pixel + 2)));
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800200 }
Valerie Hauda3446e2019-10-14 15:49:22 -0700201 }
202 }
chaviwd2432892020-07-24 17:42:39 -0700203 captureBuf->unlock();
Valerie Hauda3446e2019-10-14 15:49:22 -0700204 ASSERT_EQ(false, ::testing::Test::HasFailure());
Valerie Hauc5011f92019-10-11 09:52:07 -0700205 }
206
chaviw8ffc7b82020-08-18 11:25:37 -0700207 static status_t captureDisplay(DisplayCaptureArgs& captureArgs,
208 ScreenCaptureResults& captureResults) {
209 const auto sf = ComposerService::getComposerService();
210 SurfaceComposerClient::Transaction().apply(true);
211
212 const sp<SyncScreenCaptureListener> captureListener = new SyncScreenCaptureListener();
213 status_t status = sf->captureDisplay(captureArgs, captureListener);
214 if (status != NO_ERROR) {
215 return status;
216 }
217 captureResults = captureListener->waitForResults();
218 return captureResults.result;
219 }
220
Valerie Hauc5011f92019-10-11 09:52:07 -0700221 sp<SurfaceComposerClient> mClient;
Valerie Hauda3446e2019-10-14 15:49:22 -0700222 sp<ISurfaceComposer> mComposer;
223
224 sp<IBinder> mDisplayToken;
225
Valerie Hauc5011f92019-10-11 09:52:07 -0700226 sp<SurfaceControl> mSurfaceControl;
Valerie Hauda3446e2019-10-14 15:49:22 -0700227
228 uint32_t mDisplayWidth;
229 uint32_t mDisplayHeight;
chaviwd2432892020-07-24 17:42:39 -0700230
231 DisplayCaptureArgs mCaptureArgs;
232 ScreenCaptureResults mCaptureResults;
Valerie Hauc5011f92019-10-11 09:52:07 -0700233};
234
235TEST_F(BLASTBufferQueueTest, CreateBLASTBufferQueue) {
236 // create BLASTBufferQueue adapter associated with this surface
Valerie Hauda3446e2019-10-14 15:49:22 -0700237 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
Valerie Hauc5011f92019-10-11 09:52:07 -0700238 ASSERT_EQ(mSurfaceControl, adapter.getSurfaceControl());
Valerie Hauda3446e2019-10-14 15:49:22 -0700239 ASSERT_EQ(mDisplayWidth, adapter.getWidth());
240 ASSERT_EQ(mDisplayHeight, adapter.getHeight());
Valerie Hauc5011f92019-10-11 09:52:07 -0700241 ASSERT_EQ(nullptr, adapter.getNextTransaction());
242}
243
244TEST_F(BLASTBufferQueueTest, Update) {
Valerie Hauda3446e2019-10-14 15:49:22 -0700245 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
Valerie Hauc5011f92019-10-11 09:52:07 -0700246 sp<SurfaceControl> updateSurface =
Valerie Hauda3446e2019-10-14 15:49:22 -0700247 mClient->createSurface(String8("UpdateTest"), mDisplayWidth / 2, mDisplayHeight / 2,
248 PIXEL_FORMAT_RGBA_8888);
249 adapter.update(updateSurface, mDisplayWidth / 2, mDisplayHeight / 2);
Valerie Hauc5011f92019-10-11 09:52:07 -0700250 ASSERT_EQ(updateSurface, adapter.getSurfaceControl());
Valerie Hauda3446e2019-10-14 15:49:22 -0700251 ASSERT_EQ(mDisplayWidth / 2, adapter.getWidth());
252 ASSERT_EQ(mDisplayHeight / 2, adapter.getHeight());
Valerie Hauc5011f92019-10-11 09:52:07 -0700253}
254
255TEST_F(BLASTBufferQueueTest, SetNextTransaction) {
Valerie Hauda3446e2019-10-14 15:49:22 -0700256 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
Valerie Hauc5011f92019-10-11 09:52:07 -0700257 Transaction next;
258 adapter.setNextTransaction(&next);
259 ASSERT_EQ(&next, adapter.getNextTransaction());
260}
Valerie Hauda3446e2019-10-14 15:49:22 -0700261
Valerie Haubf29e042020-02-06 11:40:38 -0800262TEST_F(BLASTBufferQueueTest, DISABLED_onFrameAvailable_ApplyDesiredPresentTime) {
Valerie Hau181abd32020-01-27 14:18:28 -0800263 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
264 sp<IGraphicBufferProducer> igbProducer;
265 setUpProducer(adapter, igbProducer);
266
267 int slot;
268 sp<Fence> fence;
269 sp<GraphicBuffer> buf;
270 auto ret = igbProducer->dequeueBuffer(&slot, &fence, mDisplayWidth, mDisplayHeight,
271 PIXEL_FORMAT_RGBA_8888, GRALLOC_USAGE_SW_WRITE_OFTEN,
272 nullptr, nullptr);
273 ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, ret);
274 ASSERT_EQ(OK, igbProducer->requestBuffer(slot, &buf));
275
276 nsecs_t desiredPresentTime = systemTime() + nsecs_t(5 * 1e8);
277 IGraphicBufferProducer::QueueBufferOutput qbOutput;
278 IGraphicBufferProducer::QueueBufferInput input(desiredPresentTime, false, HAL_DATASPACE_UNKNOWN,
279 Rect(mDisplayWidth, mDisplayHeight),
280 NATIVE_WINDOW_SCALING_MODE_FREEZE, 0,
281 Fence::NO_FENCE);
282 igbProducer->queueBuffer(slot, input, &qbOutput);
283 ASSERT_NE(ui::Transform::ROT_INVALID, qbOutput.transformHint);
284
285 adapter.waitForCallbacks();
286 ASSERT_GE(systemTime(), desiredPresentTime);
287}
288
Valerie Hauda3446e2019-10-14 15:49:22 -0700289TEST_F(BLASTBufferQueueTest, onFrameAvailable_Apply) {
290 uint8_t r = 255;
291 uint8_t g = 0;
292 uint8_t b = 0;
293
294 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
Valerie Haud3b90d22019-11-06 09:37:31 -0800295 sp<IGraphicBufferProducer> igbProducer;
296 setUpProducer(adapter, igbProducer);
Valerie Hauda3446e2019-10-14 15:49:22 -0700297
298 int slot;
299 sp<Fence> fence;
300 sp<GraphicBuffer> buf;
301 auto ret = igbProducer->dequeueBuffer(&slot, &fence, mDisplayWidth, mDisplayHeight,
302 PIXEL_FORMAT_RGBA_8888, GRALLOC_USAGE_SW_WRITE_OFTEN,
303 nullptr, nullptr);
304 ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, ret);
305 ASSERT_EQ(OK, igbProducer->requestBuffer(slot, &buf));
306
307 uint32_t* bufData;
308 buf->lock(static_cast<uint32_t>(GraphicBuffer::USAGE_SW_WRITE_OFTEN),
309 reinterpret_cast<void**>(&bufData));
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800310 fillBuffer(bufData, Rect(buf->getWidth(), buf->getHeight()), buf->getStride(), r, g, b);
Valerie Hauda3446e2019-10-14 15:49:22 -0700311 buf->unlock();
312
Valerie Haud3b90d22019-11-06 09:37:31 -0800313 IGraphicBufferProducer::QueueBufferOutput qbOutput;
Valerie Hauda3446e2019-10-14 15:49:22 -0700314 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);
Dominik Laskowski718f9602019-11-09 20:01:35 -0800319 ASSERT_NE(ui::Transform::ROT_INVALID, qbOutput.transformHint);
Valerie Hauda3446e2019-10-14 15:49:22 -0700320
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800321 adapter.waitForCallbacks();
Valerie Hauda3446e2019-10-14 15:49:22 -0700322
323 // capture screen and verify that it is red
chaviw8ffc7b82020-08-18 11:25:37 -0700324 ASSERT_EQ(NO_ERROR, captureDisplay(mCaptureArgs, mCaptureResults));
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800325 ASSERT_NO_FATAL_FAILURE(
326 checkScreenCapture(r, g, b, {0, 0, (int32_t)mDisplayWidth, (int32_t)mDisplayHeight}));
Valerie Hauda3446e2019-10-14 15:49:22 -0700327}
Valerie Haud3b90d22019-11-06 09:37:31 -0800328
329TEST_F(BLASTBufferQueueTest, TripleBuffering) {
330 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
331 sp<IGraphicBufferProducer> igbProducer;
332 setUpProducer(adapter, igbProducer);
333
334 std::vector<std::pair<int, sp<Fence>>> allocated;
335 for (int i = 0; i < 3; i++) {
336 int slot;
337 sp<Fence> fence;
338 sp<GraphicBuffer> buf;
339 auto ret = igbProducer->dequeueBuffer(&slot, &fence, mDisplayWidth, mDisplayHeight,
340 PIXEL_FORMAT_RGBA_8888, GRALLOC_USAGE_SW_WRITE_OFTEN,
341 nullptr, nullptr);
342 ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, ret);
343 ASSERT_EQ(OK, igbProducer->requestBuffer(slot, &buf));
344 allocated.push_back({slot, fence});
345 }
346 for (int i = 0; i < allocated.size(); i++) {
347 igbProducer->cancelBuffer(allocated[i].first, allocated[i].second);
348 }
349
Valerie Haua32c5522019-12-09 10:11:08 -0800350 for (int i = 0; i < 100; i++) {
Valerie Haud3b90d22019-11-06 09:37:31 -0800351 int slot;
352 sp<Fence> fence;
353 sp<GraphicBuffer> buf;
354 auto ret = igbProducer->dequeueBuffer(&slot, &fence, mDisplayWidth, mDisplayHeight,
355 PIXEL_FORMAT_RGBA_8888, GRALLOC_USAGE_SW_WRITE_OFTEN,
356 nullptr, nullptr);
357 ASSERT_EQ(NO_ERROR, ret);
358 IGraphicBufferProducer::QueueBufferOutput qbOutput;
359 IGraphicBufferProducer::QueueBufferInput input(systemTime(), false, HAL_DATASPACE_UNKNOWN,
360 Rect(mDisplayWidth, mDisplayHeight),
361 NATIVE_WINDOW_SCALING_MODE_FREEZE, 0,
362 Fence::NO_FENCE);
363 igbProducer->queueBuffer(slot, input, &qbOutput);
364 }
365 adapter.waitForCallbacks();
366}
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800367
368TEST_F(BLASTBufferQueueTest, SetCrop_Item) {
369 uint8_t r = 255;
370 uint8_t g = 0;
371 uint8_t b = 0;
372
373 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
374 sp<IGraphicBufferProducer> igbProducer;
375 setUpProducer(adapter, igbProducer);
376 int slot;
377 sp<Fence> fence;
378 sp<GraphicBuffer> buf;
379 auto ret = igbProducer->dequeueBuffer(&slot, &fence, mDisplayWidth, mDisplayHeight,
380 PIXEL_FORMAT_RGBA_8888, GRALLOC_USAGE_SW_WRITE_OFTEN,
381 nullptr, nullptr);
382 ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, ret);
383 ASSERT_EQ(OK, igbProducer->requestBuffer(slot, &buf));
384
385 uint32_t* bufData;
386 buf->lock(static_cast<uint32_t>(GraphicBuffer::USAGE_SW_WRITE_OFTEN),
387 reinterpret_cast<void**>(&bufData));
388 fillBuffer(bufData, Rect(buf->getWidth(), buf->getHeight() / 2), buf->getStride(), r, g, b);
389 buf->unlock();
390
391 IGraphicBufferProducer::QueueBufferOutput qbOutput;
392 IGraphicBufferProducer::QueueBufferInput input(systemTime(), false, HAL_DATASPACE_UNKNOWN,
393 Rect(mDisplayWidth, mDisplayHeight / 2),
394 NATIVE_WINDOW_SCALING_MODE_FREEZE, 0,
395 Fence::NO_FENCE);
396 igbProducer->queueBuffer(slot, input, &qbOutput);
Dominik Laskowski718f9602019-11-09 20:01:35 -0800397 ASSERT_NE(ui::Transform::ROT_INVALID, qbOutput.transformHint);
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800398
399 adapter.waitForCallbacks();
400 // capture screen and verify that it is red
chaviw8ffc7b82020-08-18 11:25:37 -0700401 ASSERT_EQ(NO_ERROR, captureDisplay(mCaptureArgs, mCaptureResults));
chaviwd2432892020-07-24 17:42:39 -0700402
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800403 ASSERT_NO_FATAL_FAILURE(
404 checkScreenCapture(r, g, b, {0, 0, (int32_t)mDisplayWidth, (int32_t)mDisplayHeight}));
405}
406
407TEST_F(BLASTBufferQueueTest, SetCrop_ScalingModeScaleCrop) {
408 uint8_t r = 255;
409 uint8_t g = 0;
410 uint8_t b = 0;
411
412 int32_t bufferSideLength =
413 (mDisplayWidth < mDisplayHeight) ? mDisplayWidth / 2 : mDisplayHeight / 2;
414 int32_t finalCropSideLength = bufferSideLength / 2;
415
416 auto bg = mClient->createSurface(String8("BGTest"), 0, 0, PIXEL_FORMAT_RGBA_8888,
Vishnu Nairfa247b12020-02-11 08:58:26 -0800417 ISurfaceComposerClient::eFXSurfaceEffect);
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800418 ASSERT_NE(nullptr, bg.get());
419 Transaction t;
420 t.setLayerStack(bg, 0)
421 .setCrop_legacy(bg, Rect(0, 0, mDisplayWidth, mDisplayHeight))
422 .setColor(bg, half3{0, 0, 0})
423 .setLayer(bg, 0)
424 .apply();
425
426 BLASTBufferQueueHelper adapter(mSurfaceControl, bufferSideLength, bufferSideLength);
427 sp<IGraphicBufferProducer> igbProducer;
428 setUpProducer(adapter, igbProducer);
429 int slot;
430 sp<Fence> fence;
431 sp<GraphicBuffer> buf;
432 auto ret = igbProducer->dequeueBuffer(&slot, &fence, bufferSideLength, bufferSideLength,
433 PIXEL_FORMAT_RGBA_8888, GRALLOC_USAGE_SW_WRITE_OFTEN,
434 nullptr, nullptr);
435 ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, ret);
436 ASSERT_EQ(OK, igbProducer->requestBuffer(slot, &buf));
437
438 uint32_t* bufData;
439 buf->lock(static_cast<uint32_t>(GraphicBuffer::USAGE_SW_WRITE_OFTEN),
440 reinterpret_cast<void**>(&bufData));
441 fillBuffer(bufData, Rect(buf->getWidth(), buf->getHeight()), buf->getStride(), 0, 0, 0);
442 fillBuffer(bufData,
443 Rect(finalCropSideLength / 2, 0, buf->getWidth() - finalCropSideLength / 2,
444 buf->getHeight()),
445 buf->getStride(), r, g, b);
446 buf->unlock();
447
448 IGraphicBufferProducer::QueueBufferOutput qbOutput;
449 IGraphicBufferProducer::QueueBufferInput input(systemTime(), false, HAL_DATASPACE_UNKNOWN,
450 Rect(bufferSideLength, finalCropSideLength),
451 NATIVE_WINDOW_SCALING_MODE_SCALE_CROP, 0,
452 Fence::NO_FENCE);
453 igbProducer->queueBuffer(slot, input, &qbOutput);
Dominik Laskowski718f9602019-11-09 20:01:35 -0800454 ASSERT_NE(ui::Transform::ROT_INVALID, qbOutput.transformHint);
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800455
456 adapter.waitForCallbacks();
457 // capture screen and verify that it is red
chaviw8ffc7b82020-08-18 11:25:37 -0700458 ASSERT_EQ(NO_ERROR, captureDisplay(mCaptureArgs, mCaptureResults));
chaviwd2432892020-07-24 17:42:39 -0700459
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800460 ASSERT_NO_FATAL_FAILURE(
461 checkScreenCapture(r, g, b,
462 {0, 0, (int32_t)bufferSideLength, (int32_t)bufferSideLength}));
Valerie Hau5977fc82019-12-05 15:56:39 -0800463 ASSERT_NO_FATAL_FAILURE(
464 checkScreenCapture(0, 0, 0,
465 {0, 0, (int32_t)bufferSideLength, (int32_t)bufferSideLength},
466 /*border*/ 0, /*outsideRegion*/ true));
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800467}
468
Valerie Hau5977fc82019-12-05 15:56:39 -0800469class BLASTBufferQueueTransformTest : public BLASTBufferQueueTest {
470public:
471 void test(uint32_t tr) {
472 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
473 sp<IGraphicBufferProducer> igbProducer;
474 setUpProducer(adapter, igbProducer);
475
476 auto bufWidth = mDisplayWidth;
477 auto bufHeight = mDisplayHeight;
478 int slot;
479 sp<Fence> fence;
480 sp<GraphicBuffer> buf;
481
482 auto ret = igbProducer->dequeueBuffer(&slot, &fence, bufWidth, bufHeight,
483 PIXEL_FORMAT_RGBA_8888, GRALLOC_USAGE_SW_WRITE_OFTEN,
484 nullptr, nullptr);
485 ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, ret);
486 ASSERT_EQ(OK, igbProducer->requestBuffer(slot, &buf));
487
488 fillQuadrants(buf);
489
490 IGraphicBufferProducer::QueueBufferOutput qbOutput;
491 IGraphicBufferProducer::QueueBufferInput input(systemTime(), false, HAL_DATASPACE_UNKNOWN,
492 Rect(bufWidth, bufHeight),
Vishnu Naire1a42322020-10-02 17:42:04 -0700493 NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW,
494 tr, Fence::NO_FENCE);
Valerie Hau5977fc82019-12-05 15:56:39 -0800495 igbProducer->queueBuffer(slot, input, &qbOutput);
Dominik Laskowski718f9602019-11-09 20:01:35 -0800496 ASSERT_NE(ui::Transform::ROT_INVALID, qbOutput.transformHint);
Valerie Hau5977fc82019-12-05 15:56:39 -0800497
498 adapter.waitForCallbacks();
chaviw8ffc7b82020-08-18 11:25:37 -0700499 ASSERT_EQ(NO_ERROR, captureDisplay(mCaptureArgs, mCaptureResults));
chaviwd2432892020-07-24 17:42:39 -0700500
Valerie Hau5977fc82019-12-05 15:56:39 -0800501 switch (tr) {
502 case ui::Transform::ROT_0:
503 ASSERT_NO_FATAL_FAILURE(checkScreenCapture(0, 0, 0,
504 {0, 0, (int32_t)mDisplayWidth / 2,
505 (int32_t)mDisplayHeight / 2},
506 1));
507 ASSERT_NO_FATAL_FAILURE(
508 checkScreenCapture(255, 0, 0,
509 {(int32_t)mDisplayWidth / 2, 0, (int32_t)mDisplayWidth,
510 (int32_t)mDisplayHeight / 2},
511 1));
512 ASSERT_NO_FATAL_FAILURE(
513 checkScreenCapture(0, 255, 0,
514 {(int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight / 2,
515 (int32_t)mDisplayWidth, (int32_t)mDisplayHeight},
516 1));
517 ASSERT_NO_FATAL_FAILURE(
518 checkScreenCapture(0, 0, 255,
519 {0, (int32_t)mDisplayHeight / 2,
520 (int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight},
521 1));
522 break;
523 case ui::Transform::FLIP_H:
524 ASSERT_NO_FATAL_FAILURE(checkScreenCapture(255, 0, 0,
525 {0, 0, (int32_t)mDisplayWidth / 2,
526 (int32_t)mDisplayHeight / 2},
527 1));
528 ASSERT_NO_FATAL_FAILURE(
529 checkScreenCapture(0, 0, 0,
530 {(int32_t)mDisplayWidth / 2, 0, (int32_t)mDisplayWidth,
531 (int32_t)mDisplayHeight / 2},
532 1));
533 ASSERT_NO_FATAL_FAILURE(
534 checkScreenCapture(0, 0, 255,
535 {(int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight / 2,
536 (int32_t)mDisplayWidth, (int32_t)mDisplayHeight},
537 1));
538 ASSERT_NO_FATAL_FAILURE(
539 checkScreenCapture(0, 255, 0,
540 {0, (int32_t)mDisplayHeight / 2,
541 (int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight},
542 1));
543 break;
544 case ui::Transform::FLIP_V:
545 ASSERT_NO_FATAL_FAILURE(checkScreenCapture(0, 0, 255,
546 {0, 0, (int32_t)mDisplayWidth / 2,
547 (int32_t)mDisplayHeight / 2},
548 1));
549 ASSERT_NO_FATAL_FAILURE(
550 checkScreenCapture(0, 255, 0,
551 {(int32_t)mDisplayWidth / 2, 0, (int32_t)mDisplayWidth,
552 (int32_t)mDisplayHeight / 2},
553 1));
554 ASSERT_NO_FATAL_FAILURE(
555 checkScreenCapture(255, 0, 0,
556 {(int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight / 2,
557 (int32_t)mDisplayWidth, (int32_t)mDisplayHeight},
558 1));
559 ASSERT_NO_FATAL_FAILURE(
560 checkScreenCapture(0, 0, 0,
561 {0, (int32_t)mDisplayHeight / 2,
562 (int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight},
563 1));
564 break;
565 case ui::Transform::ROT_90:
566 ASSERT_NO_FATAL_FAILURE(checkScreenCapture(0, 0, 255,
567 {0, 0, (int32_t)mDisplayWidth / 2,
568 (int32_t)mDisplayHeight / 2},
569 1));
570 ASSERT_NO_FATAL_FAILURE(
571 checkScreenCapture(0, 0, 0,
572 {(int32_t)mDisplayWidth / 2, 0, (int32_t)mDisplayWidth,
573 (int32_t)mDisplayHeight / 2},
574 1));
575 ASSERT_NO_FATAL_FAILURE(
576 checkScreenCapture(255, 0, 0,
577 {(int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight / 2,
578 (int32_t)mDisplayWidth, (int32_t)mDisplayHeight},
579 1));
580 ASSERT_NO_FATAL_FAILURE(
581 checkScreenCapture(0, 255, 0,
582 {0, (int32_t)mDisplayHeight / 2,
583 (int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight},
584 1));
585 break;
586 case ui::Transform::ROT_180:
587 ASSERT_NO_FATAL_FAILURE(checkScreenCapture(0, 255, 0,
588 {0, 0, (int32_t)mDisplayWidth / 2,
589 (int32_t)mDisplayHeight / 2},
590 1));
591 ASSERT_NO_FATAL_FAILURE(
592 checkScreenCapture(0, 0, 255,
593 {(int32_t)mDisplayWidth / 2, 0, (int32_t)mDisplayWidth,
594 (int32_t)mDisplayHeight / 2},
595 1));
596 ASSERT_NO_FATAL_FAILURE(
597 checkScreenCapture(0, 0, 0,
598 {(int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight / 2,
599 (int32_t)mDisplayWidth, (int32_t)mDisplayHeight},
600 1));
601 ASSERT_NO_FATAL_FAILURE(
602 checkScreenCapture(255, 0, 0,
603 {0, (int32_t)mDisplayHeight / 2,
604 (int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight},
605 1));
606 break;
607 case ui::Transform::ROT_270:
608 ASSERT_NO_FATAL_FAILURE(checkScreenCapture(255, 0, 0,
609 {0, 0, (int32_t)mDisplayWidth / 2,
610 (int32_t)mDisplayHeight / 2},
611 1));
612 ASSERT_NO_FATAL_FAILURE(
613 checkScreenCapture(0, 255, 0,
614 {(int32_t)mDisplayWidth / 2, 0, (int32_t)mDisplayWidth,
615 (int32_t)mDisplayHeight / 2},
616 1));
617 ASSERT_NO_FATAL_FAILURE(
618 checkScreenCapture(0, 0, 255,
619 {(int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight / 2,
620 (int32_t)mDisplayWidth, (int32_t)mDisplayHeight},
621 1));
622 ASSERT_NO_FATAL_FAILURE(
623 checkScreenCapture(0, 0, 0,
624 {0, (int32_t)mDisplayHeight / 2,
625 (int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight},
626 1));
627 }
628 }
629};
630
631TEST_F(BLASTBufferQueueTransformTest, setTransform_ROT_0) {
632 test(ui::Transform::ROT_0);
633}
634
635TEST_F(BLASTBufferQueueTransformTest, setTransform_FLIP_H) {
636 test(ui::Transform::FLIP_H);
637}
638
639TEST_F(BLASTBufferQueueTransformTest, setTransform_FLIP_V) {
640 test(ui::Transform::FLIP_V);
641}
642
643TEST_F(BLASTBufferQueueTransformTest, setTransform_ROT_90) {
644 test(ui::Transform::ROT_90);
645}
646
647TEST_F(BLASTBufferQueueTransformTest, setTransform_ROT_180) {
648 test(ui::Transform::ROT_180);
649}
650
651TEST_F(BLASTBufferQueueTransformTest, setTransform_ROT_270) {
652 test(ui::Transform::ROT_270);
653}
Valerie Hau871d6352020-01-29 08:44:02 -0800654
655class BLASTFrameEventHistoryTest : public BLASTBufferQueueTest {
656public:
657 void setUpAndQueueBuffer(const sp<IGraphicBufferProducer>& igbProducer,
658 nsecs_t* requestedPresentTime, nsecs_t* postedTime,
659 IGraphicBufferProducer::QueueBufferOutput* qbOutput,
660 bool getFrameTimestamps) {
661 int slot;
662 sp<Fence> fence;
663 sp<GraphicBuffer> buf;
664 auto ret = igbProducer->dequeueBuffer(&slot, &fence, mDisplayWidth, mDisplayHeight,
665 PIXEL_FORMAT_RGBA_8888, GRALLOC_USAGE_SW_WRITE_OFTEN,
666 nullptr, nullptr);
667 ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, ret);
668 ASSERT_EQ(OK, igbProducer->requestBuffer(slot, &buf));
669
670 nsecs_t requestedTime = systemTime();
671 if (requestedPresentTime) *requestedPresentTime = requestedTime;
672 IGraphicBufferProducer::QueueBufferInput input(requestedTime, false, HAL_DATASPACE_UNKNOWN,
673 Rect(mDisplayWidth, mDisplayHeight),
674 NATIVE_WINDOW_SCALING_MODE_FREEZE, 0,
675 Fence::NO_FENCE, /*sticky*/ 0,
676 getFrameTimestamps);
677 if (postedTime) *postedTime = systemTime();
678 igbProducer->queueBuffer(slot, input, qbOutput);
679 }
680};
681
682TEST_F(BLASTFrameEventHistoryTest, FrameEventHistory_Basic) {
683 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
684 sp<IGraphicBufferProducer> igbProducer;
685 ProducerFrameEventHistory history;
686 setUpProducer(adapter, igbProducer);
687
688 IGraphicBufferProducer::QueueBufferOutput qbOutput;
689 nsecs_t requestedPresentTimeA = 0;
690 nsecs_t postedTimeA = 0;
691 setUpAndQueueBuffer(igbProducer, &requestedPresentTimeA, &postedTimeA, &qbOutput, true);
692 history.applyDelta(qbOutput.frameTimestamps);
693
694 FrameEvents* events = nullptr;
695 events = history.getFrame(1);
696 ASSERT_NE(nullptr, events);
697 ASSERT_EQ(1, events->frameNumber);
698 ASSERT_EQ(requestedPresentTimeA, events->requestedPresentTime);
699 ASSERT_GE(events->postedTime, postedTimeA);
700
701 adapter.waitForCallbacks();
702
703 // queue another buffer so we query for frame event deltas
704 nsecs_t requestedPresentTimeB = 0;
705 nsecs_t postedTimeB = 0;
706 setUpAndQueueBuffer(igbProducer, &requestedPresentTimeB, &postedTimeB, &qbOutput, true);
707 history.applyDelta(qbOutput.frameTimestamps);
708 events = history.getFrame(1);
709 ASSERT_NE(nullptr, events);
710
711 // frame number, requestedPresentTime, and postTime should not have changed
712 ASSERT_EQ(1, events->frameNumber);
713 ASSERT_EQ(requestedPresentTimeA, events->requestedPresentTime);
714 ASSERT_GE(events->postedTime, postedTimeA);
715
716 ASSERT_GE(events->latchTime, postedTimeA);
717 ASSERT_GE(events->dequeueReadyTime, events->latchTime);
718 ASSERT_NE(nullptr, events->gpuCompositionDoneFence);
719 ASSERT_NE(nullptr, events->displayPresentFence);
720 ASSERT_NE(nullptr, events->releaseFence);
721
722 // we should also have gotten the initial values for the next frame
723 events = history.getFrame(2);
724 ASSERT_NE(nullptr, events);
725 ASSERT_EQ(2, events->frameNumber);
726 ASSERT_EQ(requestedPresentTimeB, events->requestedPresentTime);
727 ASSERT_GE(events->postedTime, postedTimeB);
Valerie Hau78491e92020-04-15 13:10:56 -0700728
729 // wait for any callbacks that have not been received
730 adapter.waitForCallbacks();
Valerie Hau871d6352020-01-29 08:44:02 -0800731}
Valerie Hauc5011f92019-10-11 09:52:07 -0700732} // namespace android