blob: f3559fa6c10d7ef23ffeb6b1e73064fbdfa9f0f5 [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;
arthurhung6fa58b72020-11-05 11:56:00 +0800126 mCaptureArgs.dataspace = ui::Dataspace::V0_SRGB;
Valerie Hauda3446e2019-10-14 15:49:22 -0700127 }
128
Valerie Haud3b90d22019-11-06 09:37:31 -0800129 void setUpProducer(BLASTBufferQueueHelper adapter, sp<IGraphicBufferProducer>& producer) {
130 auto igbProducer = adapter.getIGraphicBufferProducer();
131 ASSERT_NE(nullptr, igbProducer.get());
Valerie Hauc78c43a2020-01-09 17:34:14 -0800132 ASSERT_EQ(NO_ERROR, igbProducer->setMaxDequeuedBufferCount(2));
Valerie Haud3b90d22019-11-06 09:37:31 -0800133 IGraphicBufferProducer::QueueBufferOutput qbOutput;
134 ASSERT_EQ(NO_ERROR,
Peiyong Lind8460c82020-07-28 16:04:22 -0700135 igbProducer->connect(new StubProducerListener, NATIVE_WINDOW_API_CPU, false,
Valerie Haud3b90d22019-11-06 09:37:31 -0800136 &qbOutput));
Dominik Laskowski718f9602019-11-09 20:01:35 -0800137 ASSERT_NE(ui::Transform::ROT_INVALID, qbOutput.transformHint);
Valerie Haud3b90d22019-11-06 09:37:31 -0800138 producer = igbProducer;
139 }
140
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800141 void fillBuffer(uint32_t* bufData, Rect rect, uint32_t stride, uint8_t r, uint8_t g,
142 uint8_t b) {
143 for (uint32_t row = rect.top; row < rect.bottom; row++) {
144 for (uint32_t col = rect.left; col < rect.right; col++) {
Valerie Hauda3446e2019-10-14 15:49:22 -0700145 uint8_t* pixel = (uint8_t*)(bufData + (row * stride) + col);
146 *pixel = r;
147 *(pixel + 1) = g;
148 *(pixel + 2) = b;
149 *(pixel + 3) = 255;
150 }
151 }
152 }
153
Valerie Hau5977fc82019-12-05 15:56:39 -0800154 void fillQuadrants(sp<GraphicBuffer>& buf) {
155 const auto bufWidth = buf->getWidth();
156 const auto bufHeight = buf->getHeight();
157 uint32_t* bufData;
158 buf->lock(static_cast<uint32_t>(GraphicBuffer::USAGE_SW_WRITE_OFTEN),
159 reinterpret_cast<void**>(&bufData));
160 fillBuffer(bufData, Rect(0, 0, bufWidth / 2, bufHeight / 2), buf->getStride(), 0, 0, 0);
161 fillBuffer(bufData, Rect(bufWidth / 2, 0, bufWidth, bufHeight / 2), buf->getStride(), 255,
162 0, 0);
163 fillBuffer(bufData, Rect(bufWidth / 2, bufHeight / 2, bufWidth, bufHeight),
164 buf->getStride(), 0, 255, 0);
165 fillBuffer(bufData, Rect(0, bufHeight / 2, bufWidth / 2, bufHeight), buf->getStride(), 0, 0,
166 255);
167 buf->unlock();
168 }
169
170 void checkScreenCapture(uint8_t r, uint8_t g, uint8_t b, Rect region, int32_t border = 0,
171 bool outsideRegion = false) {
chaviwd2432892020-07-24 17:42:39 -0700172 sp<GraphicBuffer>& captureBuf = mCaptureResults.buffer;
Valerie Hau5977fc82019-12-05 15:56:39 -0800173 const auto epsilon = 3;
chaviwd2432892020-07-24 17:42:39 -0700174 const auto width = captureBuf->getWidth();
175 const auto height = captureBuf->getHeight();
176 const auto stride = captureBuf->getStride();
Valerie Hauda3446e2019-10-14 15:49:22 -0700177
178 uint32_t* bufData;
chaviwd2432892020-07-24 17:42:39 -0700179 captureBuf->lock(static_cast<uint32_t>(GraphicBuffer::USAGE_SW_READ_OFTEN),
180 reinterpret_cast<void**>(&bufData));
Valerie Hauda3446e2019-10-14 15:49:22 -0700181
182 for (uint32_t row = 0; row < height; row++) {
183 for (uint32_t col = 0; col < width; col++) {
184 uint8_t* pixel = (uint8_t*)(bufData + (row * stride) + col);
arthurhung6fa58b72020-11-05 11:56:00 +0800185 ASSERT_NE(nullptr, pixel);
Valerie Hau5977fc82019-12-05 15:56:39 -0800186 bool inRegion;
187 if (!outsideRegion) {
188 inRegion = row >= region.top + border && row < region.bottom - border &&
189 col >= region.left + border && col < region.right - border;
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800190 } else {
Valerie Hau5977fc82019-12-05 15:56:39 -0800191 inRegion = row >= region.top - border && row < region.bottom + border &&
192 col >= region.left - border && col < region.right + border;
193 }
194 if (!outsideRegion && inRegion) {
195 EXPECT_GE(epsilon, abs(r - *(pixel)));
196 EXPECT_GE(epsilon, abs(g - *(pixel + 1)));
197 EXPECT_GE(epsilon, abs(b - *(pixel + 2)));
198 } else if (outsideRegion && !inRegion) {
199 EXPECT_GE(epsilon, abs(r - *(pixel)));
200 EXPECT_GE(epsilon, abs(g - *(pixel + 1)));
201 EXPECT_GE(epsilon, abs(b - *(pixel + 2)));
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800202 }
Valerie Hauda3446e2019-10-14 15:49:22 -0700203 }
204 }
chaviwd2432892020-07-24 17:42:39 -0700205 captureBuf->unlock();
Valerie Hauda3446e2019-10-14 15:49:22 -0700206 ASSERT_EQ(false, ::testing::Test::HasFailure());
Valerie Hauc5011f92019-10-11 09:52:07 -0700207 }
208
chaviw8ffc7b82020-08-18 11:25:37 -0700209 static status_t captureDisplay(DisplayCaptureArgs& captureArgs,
210 ScreenCaptureResults& captureResults) {
211 const auto sf = ComposerService::getComposerService();
212 SurfaceComposerClient::Transaction().apply(true);
213
214 const sp<SyncScreenCaptureListener> captureListener = new SyncScreenCaptureListener();
215 status_t status = sf->captureDisplay(captureArgs, captureListener);
216 if (status != NO_ERROR) {
217 return status;
218 }
219 captureResults = captureListener->waitForResults();
220 return captureResults.result;
221 }
222
Valerie Hauc5011f92019-10-11 09:52:07 -0700223 sp<SurfaceComposerClient> mClient;
Valerie Hauda3446e2019-10-14 15:49:22 -0700224 sp<ISurfaceComposer> mComposer;
225
226 sp<IBinder> mDisplayToken;
227
Valerie Hauc5011f92019-10-11 09:52:07 -0700228 sp<SurfaceControl> mSurfaceControl;
Valerie Hauda3446e2019-10-14 15:49:22 -0700229
230 uint32_t mDisplayWidth;
231 uint32_t mDisplayHeight;
chaviwd2432892020-07-24 17:42:39 -0700232
233 DisplayCaptureArgs mCaptureArgs;
234 ScreenCaptureResults mCaptureResults;
Valerie Hauc5011f92019-10-11 09:52:07 -0700235};
236
237TEST_F(BLASTBufferQueueTest, CreateBLASTBufferQueue) {
238 // create BLASTBufferQueue adapter associated with this surface
Valerie Hauda3446e2019-10-14 15:49:22 -0700239 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
Valerie Hauc5011f92019-10-11 09:52:07 -0700240 ASSERT_EQ(mSurfaceControl, adapter.getSurfaceControl());
Valerie Hauda3446e2019-10-14 15:49:22 -0700241 ASSERT_EQ(mDisplayWidth, adapter.getWidth());
242 ASSERT_EQ(mDisplayHeight, adapter.getHeight());
Valerie Hauc5011f92019-10-11 09:52:07 -0700243 ASSERT_EQ(nullptr, adapter.getNextTransaction());
244}
245
246TEST_F(BLASTBufferQueueTest, Update) {
Valerie Hauda3446e2019-10-14 15:49:22 -0700247 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
Valerie Hauc5011f92019-10-11 09:52:07 -0700248 sp<SurfaceControl> updateSurface =
Valerie Hauda3446e2019-10-14 15:49:22 -0700249 mClient->createSurface(String8("UpdateTest"), mDisplayWidth / 2, mDisplayHeight / 2,
250 PIXEL_FORMAT_RGBA_8888);
251 adapter.update(updateSurface, mDisplayWidth / 2, mDisplayHeight / 2);
Valerie Hauc5011f92019-10-11 09:52:07 -0700252 ASSERT_EQ(updateSurface, adapter.getSurfaceControl());
Valerie Hauda3446e2019-10-14 15:49:22 -0700253 ASSERT_EQ(mDisplayWidth / 2, adapter.getWidth());
254 ASSERT_EQ(mDisplayHeight / 2, adapter.getHeight());
Valerie Hauc5011f92019-10-11 09:52:07 -0700255}
256
257TEST_F(BLASTBufferQueueTest, SetNextTransaction) {
Valerie Hauda3446e2019-10-14 15:49:22 -0700258 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
Valerie Hauc5011f92019-10-11 09:52:07 -0700259 Transaction next;
260 adapter.setNextTransaction(&next);
261 ASSERT_EQ(&next, adapter.getNextTransaction());
262}
Valerie Hauda3446e2019-10-14 15:49:22 -0700263
Valerie Haubf29e042020-02-06 11:40:38 -0800264TEST_F(BLASTBufferQueueTest, DISABLED_onFrameAvailable_ApplyDesiredPresentTime) {
Valerie Hau181abd32020-01-27 14:18:28 -0800265 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
266 sp<IGraphicBufferProducer> igbProducer;
267 setUpProducer(adapter, igbProducer);
268
269 int slot;
270 sp<Fence> fence;
271 sp<GraphicBuffer> buf;
272 auto ret = igbProducer->dequeueBuffer(&slot, &fence, mDisplayWidth, mDisplayHeight,
273 PIXEL_FORMAT_RGBA_8888, GRALLOC_USAGE_SW_WRITE_OFTEN,
274 nullptr, nullptr);
275 ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, ret);
276 ASSERT_EQ(OK, igbProducer->requestBuffer(slot, &buf));
277
278 nsecs_t desiredPresentTime = systemTime() + nsecs_t(5 * 1e8);
279 IGraphicBufferProducer::QueueBufferOutput qbOutput;
280 IGraphicBufferProducer::QueueBufferInput input(desiredPresentTime, false, HAL_DATASPACE_UNKNOWN,
281 Rect(mDisplayWidth, mDisplayHeight),
282 NATIVE_WINDOW_SCALING_MODE_FREEZE, 0,
283 Fence::NO_FENCE);
284 igbProducer->queueBuffer(slot, input, &qbOutput);
285 ASSERT_NE(ui::Transform::ROT_INVALID, qbOutput.transformHint);
286
287 adapter.waitForCallbacks();
288 ASSERT_GE(systemTime(), desiredPresentTime);
289}
290
Valerie Hauda3446e2019-10-14 15:49:22 -0700291TEST_F(BLASTBufferQueueTest, onFrameAvailable_Apply) {
292 uint8_t r = 255;
293 uint8_t g = 0;
294 uint8_t b = 0;
295
296 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
Valerie Haud3b90d22019-11-06 09:37:31 -0800297 sp<IGraphicBufferProducer> igbProducer;
298 setUpProducer(adapter, igbProducer);
Valerie Hauda3446e2019-10-14 15:49:22 -0700299
300 int slot;
301 sp<Fence> fence;
302 sp<GraphicBuffer> buf;
303 auto ret = igbProducer->dequeueBuffer(&slot, &fence, mDisplayWidth, mDisplayHeight,
304 PIXEL_FORMAT_RGBA_8888, GRALLOC_USAGE_SW_WRITE_OFTEN,
305 nullptr, nullptr);
306 ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, ret);
307 ASSERT_EQ(OK, igbProducer->requestBuffer(slot, &buf));
308
309 uint32_t* bufData;
310 buf->lock(static_cast<uint32_t>(GraphicBuffer::USAGE_SW_WRITE_OFTEN),
311 reinterpret_cast<void**>(&bufData));
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800312 fillBuffer(bufData, Rect(buf->getWidth(), buf->getHeight()), buf->getStride(), r, g, b);
Valerie Hauda3446e2019-10-14 15:49:22 -0700313 buf->unlock();
314
Valerie Haud3b90d22019-11-06 09:37:31 -0800315 IGraphicBufferProducer::QueueBufferOutput qbOutput;
Valerie Hauda3446e2019-10-14 15:49:22 -0700316 IGraphicBufferProducer::QueueBufferInput input(systemTime(), false, HAL_DATASPACE_UNKNOWN,
317 Rect(mDisplayWidth, mDisplayHeight),
318 NATIVE_WINDOW_SCALING_MODE_FREEZE, 0,
319 Fence::NO_FENCE);
320 igbProducer->queueBuffer(slot, input, &qbOutput);
Dominik Laskowski718f9602019-11-09 20:01:35 -0800321 ASSERT_NE(ui::Transform::ROT_INVALID, qbOutput.transformHint);
Valerie Hauda3446e2019-10-14 15:49:22 -0700322
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800323 adapter.waitForCallbacks();
Valerie Hauda3446e2019-10-14 15:49:22 -0700324
325 // capture screen and verify that it is red
chaviw8ffc7b82020-08-18 11:25:37 -0700326 ASSERT_EQ(NO_ERROR, captureDisplay(mCaptureArgs, mCaptureResults));
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800327 ASSERT_NO_FATAL_FAILURE(
328 checkScreenCapture(r, g, b, {0, 0, (int32_t)mDisplayWidth, (int32_t)mDisplayHeight}));
Valerie Hauda3446e2019-10-14 15:49:22 -0700329}
Valerie Haud3b90d22019-11-06 09:37:31 -0800330
331TEST_F(BLASTBufferQueueTest, TripleBuffering) {
332 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
333 sp<IGraphicBufferProducer> igbProducer;
334 setUpProducer(adapter, igbProducer);
335
336 std::vector<std::pair<int, sp<Fence>>> allocated;
337 for (int i = 0; i < 3; i++) {
338 int slot;
339 sp<Fence> fence;
340 sp<GraphicBuffer> buf;
341 auto ret = igbProducer->dequeueBuffer(&slot, &fence, mDisplayWidth, mDisplayHeight,
342 PIXEL_FORMAT_RGBA_8888, GRALLOC_USAGE_SW_WRITE_OFTEN,
343 nullptr, nullptr);
344 ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, ret);
345 ASSERT_EQ(OK, igbProducer->requestBuffer(slot, &buf));
346 allocated.push_back({slot, fence});
347 }
348 for (int i = 0; i < allocated.size(); i++) {
349 igbProducer->cancelBuffer(allocated[i].first, allocated[i].second);
350 }
351
Valerie Haua32c5522019-12-09 10:11:08 -0800352 for (int i = 0; i < 100; i++) {
Valerie Haud3b90d22019-11-06 09:37:31 -0800353 int slot;
354 sp<Fence> fence;
355 sp<GraphicBuffer> buf;
356 auto ret = igbProducer->dequeueBuffer(&slot, &fence, mDisplayWidth, mDisplayHeight,
357 PIXEL_FORMAT_RGBA_8888, GRALLOC_USAGE_SW_WRITE_OFTEN,
358 nullptr, nullptr);
359 ASSERT_EQ(NO_ERROR, ret);
360 IGraphicBufferProducer::QueueBufferOutput qbOutput;
361 IGraphicBufferProducer::QueueBufferInput input(systemTime(), false, HAL_DATASPACE_UNKNOWN,
362 Rect(mDisplayWidth, mDisplayHeight),
363 NATIVE_WINDOW_SCALING_MODE_FREEZE, 0,
364 Fence::NO_FENCE);
365 igbProducer->queueBuffer(slot, input, &qbOutput);
366 }
367 adapter.waitForCallbacks();
368}
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800369
370TEST_F(BLASTBufferQueueTest, SetCrop_Item) {
371 uint8_t r = 255;
372 uint8_t g = 0;
373 uint8_t b = 0;
374
375 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
376 sp<IGraphicBufferProducer> igbProducer;
377 setUpProducer(adapter, igbProducer);
378 int slot;
379 sp<Fence> fence;
380 sp<GraphicBuffer> buf;
381 auto ret = igbProducer->dequeueBuffer(&slot, &fence, mDisplayWidth, mDisplayHeight,
382 PIXEL_FORMAT_RGBA_8888, GRALLOC_USAGE_SW_WRITE_OFTEN,
383 nullptr, nullptr);
384 ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, ret);
385 ASSERT_EQ(OK, igbProducer->requestBuffer(slot, &buf));
386
387 uint32_t* bufData;
388 buf->lock(static_cast<uint32_t>(GraphicBuffer::USAGE_SW_WRITE_OFTEN),
389 reinterpret_cast<void**>(&bufData));
390 fillBuffer(bufData, Rect(buf->getWidth(), buf->getHeight() / 2), buf->getStride(), r, g, b);
391 buf->unlock();
392
393 IGraphicBufferProducer::QueueBufferOutput qbOutput;
394 IGraphicBufferProducer::QueueBufferInput input(systemTime(), false, HAL_DATASPACE_UNKNOWN,
395 Rect(mDisplayWidth, mDisplayHeight / 2),
396 NATIVE_WINDOW_SCALING_MODE_FREEZE, 0,
397 Fence::NO_FENCE);
398 igbProducer->queueBuffer(slot, input, &qbOutput);
Dominik Laskowski718f9602019-11-09 20:01:35 -0800399 ASSERT_NE(ui::Transform::ROT_INVALID, qbOutput.transformHint);
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800400
401 adapter.waitForCallbacks();
402 // capture screen and verify that it is red
chaviw8ffc7b82020-08-18 11:25:37 -0700403 ASSERT_EQ(NO_ERROR, captureDisplay(mCaptureArgs, mCaptureResults));
chaviwd2432892020-07-24 17:42:39 -0700404
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800405 ASSERT_NO_FATAL_FAILURE(
406 checkScreenCapture(r, g, b, {0, 0, (int32_t)mDisplayWidth, (int32_t)mDisplayHeight}));
407}
408
409TEST_F(BLASTBufferQueueTest, SetCrop_ScalingModeScaleCrop) {
410 uint8_t r = 255;
411 uint8_t g = 0;
412 uint8_t b = 0;
413
414 int32_t bufferSideLength =
415 (mDisplayWidth < mDisplayHeight) ? mDisplayWidth / 2 : mDisplayHeight / 2;
416 int32_t finalCropSideLength = bufferSideLength / 2;
417
418 auto bg = mClient->createSurface(String8("BGTest"), 0, 0, PIXEL_FORMAT_RGBA_8888,
Vishnu Nairfa247b12020-02-11 08:58:26 -0800419 ISurfaceComposerClient::eFXSurfaceEffect);
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800420 ASSERT_NE(nullptr, bg.get());
421 Transaction t;
422 t.setLayerStack(bg, 0)
423 .setCrop_legacy(bg, Rect(0, 0, mDisplayWidth, mDisplayHeight))
424 .setColor(bg, half3{0, 0, 0})
425 .setLayer(bg, 0)
426 .apply();
427
428 BLASTBufferQueueHelper adapter(mSurfaceControl, bufferSideLength, bufferSideLength);
429 sp<IGraphicBufferProducer> igbProducer;
430 setUpProducer(adapter, igbProducer);
431 int slot;
432 sp<Fence> fence;
433 sp<GraphicBuffer> buf;
434 auto ret = igbProducer->dequeueBuffer(&slot, &fence, bufferSideLength, bufferSideLength,
435 PIXEL_FORMAT_RGBA_8888, GRALLOC_USAGE_SW_WRITE_OFTEN,
436 nullptr, nullptr);
437 ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, ret);
438 ASSERT_EQ(OK, igbProducer->requestBuffer(slot, &buf));
439
440 uint32_t* bufData;
441 buf->lock(static_cast<uint32_t>(GraphicBuffer::USAGE_SW_WRITE_OFTEN),
442 reinterpret_cast<void**>(&bufData));
443 fillBuffer(bufData, Rect(buf->getWidth(), buf->getHeight()), buf->getStride(), 0, 0, 0);
444 fillBuffer(bufData,
445 Rect(finalCropSideLength / 2, 0, buf->getWidth() - finalCropSideLength / 2,
446 buf->getHeight()),
447 buf->getStride(), r, g, b);
448 buf->unlock();
449
450 IGraphicBufferProducer::QueueBufferOutput qbOutput;
451 IGraphicBufferProducer::QueueBufferInput input(systemTime(), false, HAL_DATASPACE_UNKNOWN,
452 Rect(bufferSideLength, finalCropSideLength),
453 NATIVE_WINDOW_SCALING_MODE_SCALE_CROP, 0,
454 Fence::NO_FENCE);
455 igbProducer->queueBuffer(slot, input, &qbOutput);
Dominik Laskowski718f9602019-11-09 20:01:35 -0800456 ASSERT_NE(ui::Transform::ROT_INVALID, qbOutput.transformHint);
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800457
458 adapter.waitForCallbacks();
459 // capture screen and verify that it is red
chaviw8ffc7b82020-08-18 11:25:37 -0700460 ASSERT_EQ(NO_ERROR, captureDisplay(mCaptureArgs, mCaptureResults));
chaviwd2432892020-07-24 17:42:39 -0700461
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800462 ASSERT_NO_FATAL_FAILURE(
463 checkScreenCapture(r, g, b,
464 {0, 0, (int32_t)bufferSideLength, (int32_t)bufferSideLength}));
Valerie Hau5977fc82019-12-05 15:56:39 -0800465 ASSERT_NO_FATAL_FAILURE(
466 checkScreenCapture(0, 0, 0,
467 {0, 0, (int32_t)bufferSideLength, (int32_t)bufferSideLength},
468 /*border*/ 0, /*outsideRegion*/ true));
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800469}
470
Valerie Hau5977fc82019-12-05 15:56:39 -0800471class BLASTBufferQueueTransformTest : public BLASTBufferQueueTest {
472public:
473 void test(uint32_t tr) {
474 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
475 sp<IGraphicBufferProducer> igbProducer;
476 setUpProducer(adapter, igbProducer);
477
478 auto bufWidth = mDisplayWidth;
479 auto bufHeight = mDisplayHeight;
480 int slot;
481 sp<Fence> fence;
482 sp<GraphicBuffer> buf;
483
484 auto ret = igbProducer->dequeueBuffer(&slot, &fence, bufWidth, bufHeight,
485 PIXEL_FORMAT_RGBA_8888, GRALLOC_USAGE_SW_WRITE_OFTEN,
486 nullptr, nullptr);
487 ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, ret);
488 ASSERT_EQ(OK, igbProducer->requestBuffer(slot, &buf));
489
490 fillQuadrants(buf);
491
492 IGraphicBufferProducer::QueueBufferOutput qbOutput;
493 IGraphicBufferProducer::QueueBufferInput input(systemTime(), false, HAL_DATASPACE_UNKNOWN,
494 Rect(bufWidth, bufHeight),
Vishnu Naire1a42322020-10-02 17:42:04 -0700495 NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW,
496 tr, Fence::NO_FENCE);
Valerie Hau5977fc82019-12-05 15:56:39 -0800497 igbProducer->queueBuffer(slot, input, &qbOutput);
Dominik Laskowski718f9602019-11-09 20:01:35 -0800498 ASSERT_NE(ui::Transform::ROT_INVALID, qbOutput.transformHint);
Valerie Hau5977fc82019-12-05 15:56:39 -0800499
500 adapter.waitForCallbacks();
chaviw8ffc7b82020-08-18 11:25:37 -0700501 ASSERT_EQ(NO_ERROR, captureDisplay(mCaptureArgs, mCaptureResults));
chaviwd2432892020-07-24 17:42:39 -0700502
Valerie Hau5977fc82019-12-05 15:56:39 -0800503 switch (tr) {
504 case ui::Transform::ROT_0:
505 ASSERT_NO_FATAL_FAILURE(checkScreenCapture(0, 0, 0,
506 {0, 0, (int32_t)mDisplayWidth / 2,
507 (int32_t)mDisplayHeight / 2},
508 1));
509 ASSERT_NO_FATAL_FAILURE(
510 checkScreenCapture(255, 0, 0,
511 {(int32_t)mDisplayWidth / 2, 0, (int32_t)mDisplayWidth,
512 (int32_t)mDisplayHeight / 2},
513 1));
514 ASSERT_NO_FATAL_FAILURE(
515 checkScreenCapture(0, 255, 0,
516 {(int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight / 2,
517 (int32_t)mDisplayWidth, (int32_t)mDisplayHeight},
518 1));
519 ASSERT_NO_FATAL_FAILURE(
520 checkScreenCapture(0, 0, 255,
521 {0, (int32_t)mDisplayHeight / 2,
522 (int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight},
523 1));
524 break;
525 case ui::Transform::FLIP_H:
526 ASSERT_NO_FATAL_FAILURE(checkScreenCapture(255, 0, 0,
527 {0, 0, (int32_t)mDisplayWidth / 2,
528 (int32_t)mDisplayHeight / 2},
529 1));
530 ASSERT_NO_FATAL_FAILURE(
531 checkScreenCapture(0, 0, 0,
532 {(int32_t)mDisplayWidth / 2, 0, (int32_t)mDisplayWidth,
533 (int32_t)mDisplayHeight / 2},
534 1));
535 ASSERT_NO_FATAL_FAILURE(
536 checkScreenCapture(0, 0, 255,
537 {(int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight / 2,
538 (int32_t)mDisplayWidth, (int32_t)mDisplayHeight},
539 1));
540 ASSERT_NO_FATAL_FAILURE(
541 checkScreenCapture(0, 255, 0,
542 {0, (int32_t)mDisplayHeight / 2,
543 (int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight},
544 1));
545 break;
546 case ui::Transform::FLIP_V:
547 ASSERT_NO_FATAL_FAILURE(checkScreenCapture(0, 0, 255,
548 {0, 0, (int32_t)mDisplayWidth / 2,
549 (int32_t)mDisplayHeight / 2},
550 1));
551 ASSERT_NO_FATAL_FAILURE(
552 checkScreenCapture(0, 255, 0,
553 {(int32_t)mDisplayWidth / 2, 0, (int32_t)mDisplayWidth,
554 (int32_t)mDisplayHeight / 2},
555 1));
556 ASSERT_NO_FATAL_FAILURE(
557 checkScreenCapture(255, 0, 0,
558 {(int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight / 2,
559 (int32_t)mDisplayWidth, (int32_t)mDisplayHeight},
560 1));
561 ASSERT_NO_FATAL_FAILURE(
562 checkScreenCapture(0, 0, 0,
563 {0, (int32_t)mDisplayHeight / 2,
564 (int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight},
565 1));
566 break;
567 case ui::Transform::ROT_90:
568 ASSERT_NO_FATAL_FAILURE(checkScreenCapture(0, 0, 255,
569 {0, 0, (int32_t)mDisplayWidth / 2,
570 (int32_t)mDisplayHeight / 2},
571 1));
572 ASSERT_NO_FATAL_FAILURE(
573 checkScreenCapture(0, 0, 0,
574 {(int32_t)mDisplayWidth / 2, 0, (int32_t)mDisplayWidth,
575 (int32_t)mDisplayHeight / 2},
576 1));
577 ASSERT_NO_FATAL_FAILURE(
578 checkScreenCapture(255, 0, 0,
579 {(int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight / 2,
580 (int32_t)mDisplayWidth, (int32_t)mDisplayHeight},
581 1));
582 ASSERT_NO_FATAL_FAILURE(
583 checkScreenCapture(0, 255, 0,
584 {0, (int32_t)mDisplayHeight / 2,
585 (int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight},
586 1));
587 break;
588 case ui::Transform::ROT_180:
589 ASSERT_NO_FATAL_FAILURE(checkScreenCapture(0, 255, 0,
590 {0, 0, (int32_t)mDisplayWidth / 2,
591 (int32_t)mDisplayHeight / 2},
592 1));
593 ASSERT_NO_FATAL_FAILURE(
594 checkScreenCapture(0, 0, 255,
595 {(int32_t)mDisplayWidth / 2, 0, (int32_t)mDisplayWidth,
596 (int32_t)mDisplayHeight / 2},
597 1));
598 ASSERT_NO_FATAL_FAILURE(
599 checkScreenCapture(0, 0, 0,
600 {(int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight / 2,
601 (int32_t)mDisplayWidth, (int32_t)mDisplayHeight},
602 1));
603 ASSERT_NO_FATAL_FAILURE(
604 checkScreenCapture(255, 0, 0,
605 {0, (int32_t)mDisplayHeight / 2,
606 (int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight},
607 1));
608 break;
609 case ui::Transform::ROT_270:
610 ASSERT_NO_FATAL_FAILURE(checkScreenCapture(255, 0, 0,
611 {0, 0, (int32_t)mDisplayWidth / 2,
612 (int32_t)mDisplayHeight / 2},
613 1));
614 ASSERT_NO_FATAL_FAILURE(
615 checkScreenCapture(0, 255, 0,
616 {(int32_t)mDisplayWidth / 2, 0, (int32_t)mDisplayWidth,
617 (int32_t)mDisplayHeight / 2},
618 1));
619 ASSERT_NO_FATAL_FAILURE(
620 checkScreenCapture(0, 0, 255,
621 {(int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight / 2,
622 (int32_t)mDisplayWidth, (int32_t)mDisplayHeight},
623 1));
624 ASSERT_NO_FATAL_FAILURE(
625 checkScreenCapture(0, 0, 0,
626 {0, (int32_t)mDisplayHeight / 2,
627 (int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight},
628 1));
629 }
630 }
631};
632
633TEST_F(BLASTBufferQueueTransformTest, setTransform_ROT_0) {
634 test(ui::Transform::ROT_0);
635}
636
637TEST_F(BLASTBufferQueueTransformTest, setTransform_FLIP_H) {
638 test(ui::Transform::FLIP_H);
639}
640
641TEST_F(BLASTBufferQueueTransformTest, setTransform_FLIP_V) {
642 test(ui::Transform::FLIP_V);
643}
644
645TEST_F(BLASTBufferQueueTransformTest, setTransform_ROT_90) {
646 test(ui::Transform::ROT_90);
647}
648
649TEST_F(BLASTBufferQueueTransformTest, setTransform_ROT_180) {
650 test(ui::Transform::ROT_180);
651}
652
653TEST_F(BLASTBufferQueueTransformTest, setTransform_ROT_270) {
654 test(ui::Transform::ROT_270);
655}
Valerie Hau871d6352020-01-29 08:44:02 -0800656
657class BLASTFrameEventHistoryTest : public BLASTBufferQueueTest {
658public:
659 void setUpAndQueueBuffer(const sp<IGraphicBufferProducer>& igbProducer,
660 nsecs_t* requestedPresentTime, nsecs_t* postedTime,
661 IGraphicBufferProducer::QueueBufferOutput* qbOutput,
662 bool getFrameTimestamps) {
663 int slot;
664 sp<Fence> fence;
665 sp<GraphicBuffer> buf;
666 auto ret = igbProducer->dequeueBuffer(&slot, &fence, mDisplayWidth, mDisplayHeight,
667 PIXEL_FORMAT_RGBA_8888, GRALLOC_USAGE_SW_WRITE_OFTEN,
668 nullptr, nullptr);
669 ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, ret);
670 ASSERT_EQ(OK, igbProducer->requestBuffer(slot, &buf));
671
672 nsecs_t requestedTime = systemTime();
673 if (requestedPresentTime) *requestedPresentTime = requestedTime;
674 IGraphicBufferProducer::QueueBufferInput input(requestedTime, false, HAL_DATASPACE_UNKNOWN,
675 Rect(mDisplayWidth, mDisplayHeight),
676 NATIVE_WINDOW_SCALING_MODE_FREEZE, 0,
677 Fence::NO_FENCE, /*sticky*/ 0,
678 getFrameTimestamps);
679 if (postedTime) *postedTime = systemTime();
680 igbProducer->queueBuffer(slot, input, qbOutput);
681 }
682};
683
684TEST_F(BLASTFrameEventHistoryTest, FrameEventHistory_Basic) {
685 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
686 sp<IGraphicBufferProducer> igbProducer;
687 ProducerFrameEventHistory history;
688 setUpProducer(adapter, igbProducer);
689
690 IGraphicBufferProducer::QueueBufferOutput qbOutput;
691 nsecs_t requestedPresentTimeA = 0;
692 nsecs_t postedTimeA = 0;
693 setUpAndQueueBuffer(igbProducer, &requestedPresentTimeA, &postedTimeA, &qbOutput, true);
694 history.applyDelta(qbOutput.frameTimestamps);
695
696 FrameEvents* events = nullptr;
697 events = history.getFrame(1);
698 ASSERT_NE(nullptr, events);
699 ASSERT_EQ(1, events->frameNumber);
700 ASSERT_EQ(requestedPresentTimeA, events->requestedPresentTime);
701 ASSERT_GE(events->postedTime, postedTimeA);
702
703 adapter.waitForCallbacks();
704
705 // queue another buffer so we query for frame event deltas
706 nsecs_t requestedPresentTimeB = 0;
707 nsecs_t postedTimeB = 0;
708 setUpAndQueueBuffer(igbProducer, &requestedPresentTimeB, &postedTimeB, &qbOutput, true);
709 history.applyDelta(qbOutput.frameTimestamps);
710 events = history.getFrame(1);
711 ASSERT_NE(nullptr, events);
712
713 // frame number, requestedPresentTime, and postTime should not have changed
714 ASSERT_EQ(1, events->frameNumber);
715 ASSERT_EQ(requestedPresentTimeA, events->requestedPresentTime);
716 ASSERT_GE(events->postedTime, postedTimeA);
717
718 ASSERT_GE(events->latchTime, postedTimeA);
719 ASSERT_GE(events->dequeueReadyTime, events->latchTime);
720 ASSERT_NE(nullptr, events->gpuCompositionDoneFence);
721 ASSERT_NE(nullptr, events->displayPresentFence);
722 ASSERT_NE(nullptr, events->releaseFence);
723
724 // we should also have gotten the initial values for the next frame
725 events = history.getFrame(2);
726 ASSERT_NE(nullptr, events);
727 ASSERT_EQ(2, events->frameNumber);
728 ASSERT_EQ(requestedPresentTimeB, events->requestedPresentTime);
729 ASSERT_GE(events->postedTime, postedTimeB);
Valerie Hau78491e92020-04-15 13:10:56 -0700730
731 // wait for any callbacks that have not been received
732 adapter.waitForCallbacks();
Valerie Hau871d6352020-01-29 08:44:02 -0800733}
Valerie Hauc5011f92019-10-11 09:52:07 -0700734} // namespace android