blob: 4282ef9e99785615278eb4a5a1bc09156304728a [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
Vishnu Nairea0de002020-11-17 17:42:37 -080058 int getWidth() { return mBlastBufferQueueAdapter->mSize.width; }
Valerie Hauda3446e2019-10-14 15:49:22 -070059
Vishnu Nairea0de002020-11-17 17:42:37 -080060 int getHeight() { return mBlastBufferQueueAdapter->mSize.height; }
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());
Vishnu Nairea0de002020-11-17 17:42:37 -0800253 sp<IGraphicBufferProducer> igbProducer;
254 setUpProducer(adapter, igbProducer);
255
256 int32_t width;
257 igbProducer->query(NATIVE_WINDOW_WIDTH, &width);
258 ASSERT_EQ(mDisplayWidth / 2, width);
259 int32_t height;
260 igbProducer->query(NATIVE_WINDOW_HEIGHT, &height);
261 ASSERT_EQ(mDisplayHeight / 2, height);
Valerie Hauc5011f92019-10-11 09:52:07 -0700262}
263
264TEST_F(BLASTBufferQueueTest, SetNextTransaction) {
Valerie Hauda3446e2019-10-14 15:49:22 -0700265 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
Valerie Hauc5011f92019-10-11 09:52:07 -0700266 Transaction next;
267 adapter.setNextTransaction(&next);
268 ASSERT_EQ(&next, adapter.getNextTransaction());
269}
Valerie Hauda3446e2019-10-14 15:49:22 -0700270
Valerie Haubf29e042020-02-06 11:40:38 -0800271TEST_F(BLASTBufferQueueTest, DISABLED_onFrameAvailable_ApplyDesiredPresentTime) {
Valerie Hau181abd32020-01-27 14:18:28 -0800272 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
273 sp<IGraphicBufferProducer> igbProducer;
274 setUpProducer(adapter, igbProducer);
275
276 int slot;
277 sp<Fence> fence;
278 sp<GraphicBuffer> buf;
279 auto ret = igbProducer->dequeueBuffer(&slot, &fence, mDisplayWidth, mDisplayHeight,
280 PIXEL_FORMAT_RGBA_8888, GRALLOC_USAGE_SW_WRITE_OFTEN,
281 nullptr, nullptr);
282 ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, ret);
283 ASSERT_EQ(OK, igbProducer->requestBuffer(slot, &buf));
284
285 nsecs_t desiredPresentTime = systemTime() + nsecs_t(5 * 1e8);
286 IGraphicBufferProducer::QueueBufferOutput qbOutput;
287 IGraphicBufferProducer::QueueBufferInput input(desiredPresentTime, false, HAL_DATASPACE_UNKNOWN,
288 Rect(mDisplayWidth, mDisplayHeight),
289 NATIVE_WINDOW_SCALING_MODE_FREEZE, 0,
290 Fence::NO_FENCE);
291 igbProducer->queueBuffer(slot, input, &qbOutput);
292 ASSERT_NE(ui::Transform::ROT_INVALID, qbOutput.transformHint);
293
294 adapter.waitForCallbacks();
295 ASSERT_GE(systemTime(), desiredPresentTime);
296}
297
Valerie Hauda3446e2019-10-14 15:49:22 -0700298TEST_F(BLASTBufferQueueTest, onFrameAvailable_Apply) {
299 uint8_t r = 255;
300 uint8_t g = 0;
301 uint8_t b = 0;
302
303 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
Valerie Haud3b90d22019-11-06 09:37:31 -0800304 sp<IGraphicBufferProducer> igbProducer;
305 setUpProducer(adapter, igbProducer);
Valerie Hauda3446e2019-10-14 15:49:22 -0700306
307 int slot;
308 sp<Fence> fence;
309 sp<GraphicBuffer> buf;
310 auto ret = igbProducer->dequeueBuffer(&slot, &fence, mDisplayWidth, mDisplayHeight,
311 PIXEL_FORMAT_RGBA_8888, GRALLOC_USAGE_SW_WRITE_OFTEN,
312 nullptr, nullptr);
313 ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, ret);
314 ASSERT_EQ(OK, igbProducer->requestBuffer(slot, &buf));
315
316 uint32_t* bufData;
317 buf->lock(static_cast<uint32_t>(GraphicBuffer::USAGE_SW_WRITE_OFTEN),
318 reinterpret_cast<void**>(&bufData));
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800319 fillBuffer(bufData, Rect(buf->getWidth(), buf->getHeight()), buf->getStride(), r, g, b);
Valerie Hauda3446e2019-10-14 15:49:22 -0700320 buf->unlock();
321
Valerie Haud3b90d22019-11-06 09:37:31 -0800322 IGraphicBufferProducer::QueueBufferOutput qbOutput;
Valerie Hauda3446e2019-10-14 15:49:22 -0700323 IGraphicBufferProducer::QueueBufferInput input(systemTime(), false, HAL_DATASPACE_UNKNOWN,
324 Rect(mDisplayWidth, mDisplayHeight),
325 NATIVE_WINDOW_SCALING_MODE_FREEZE, 0,
326 Fence::NO_FENCE);
327 igbProducer->queueBuffer(slot, input, &qbOutput);
Dominik Laskowski718f9602019-11-09 20:01:35 -0800328 ASSERT_NE(ui::Transform::ROT_INVALID, qbOutput.transformHint);
Valerie Hauda3446e2019-10-14 15:49:22 -0700329
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800330 adapter.waitForCallbacks();
Valerie Hauda3446e2019-10-14 15:49:22 -0700331
332 // capture screen and verify that it is red
chaviw8ffc7b82020-08-18 11:25:37 -0700333 ASSERT_EQ(NO_ERROR, captureDisplay(mCaptureArgs, mCaptureResults));
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800334 ASSERT_NO_FATAL_FAILURE(
335 checkScreenCapture(r, g, b, {0, 0, (int32_t)mDisplayWidth, (int32_t)mDisplayHeight}));
Valerie Hauda3446e2019-10-14 15:49:22 -0700336}
Valerie Haud3b90d22019-11-06 09:37:31 -0800337
338TEST_F(BLASTBufferQueueTest, TripleBuffering) {
339 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
340 sp<IGraphicBufferProducer> igbProducer;
341 setUpProducer(adapter, igbProducer);
342
343 std::vector<std::pair<int, sp<Fence>>> allocated;
344 for (int i = 0; i < 3; i++) {
345 int slot;
346 sp<Fence> fence;
347 sp<GraphicBuffer> buf;
348 auto ret = igbProducer->dequeueBuffer(&slot, &fence, mDisplayWidth, mDisplayHeight,
349 PIXEL_FORMAT_RGBA_8888, GRALLOC_USAGE_SW_WRITE_OFTEN,
350 nullptr, nullptr);
351 ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, ret);
352 ASSERT_EQ(OK, igbProducer->requestBuffer(slot, &buf));
353 allocated.push_back({slot, fence});
354 }
355 for (int i = 0; i < allocated.size(); i++) {
356 igbProducer->cancelBuffer(allocated[i].first, allocated[i].second);
357 }
358
Valerie Haua32c5522019-12-09 10:11:08 -0800359 for (int i = 0; i < 100; i++) {
Valerie Haud3b90d22019-11-06 09:37:31 -0800360 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(NO_ERROR, ret);
367 IGraphicBufferProducer::QueueBufferOutput qbOutput;
368 IGraphicBufferProducer::QueueBufferInput input(systemTime(), false, HAL_DATASPACE_UNKNOWN,
369 Rect(mDisplayWidth, mDisplayHeight),
370 NATIVE_WINDOW_SCALING_MODE_FREEZE, 0,
371 Fence::NO_FENCE);
372 igbProducer->queueBuffer(slot, input, &qbOutput);
373 }
374 adapter.waitForCallbacks();
375}
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800376
377TEST_F(BLASTBufferQueueTest, SetCrop_Item) {
378 uint8_t r = 255;
379 uint8_t g = 0;
380 uint8_t b = 0;
381
382 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
383 sp<IGraphicBufferProducer> igbProducer;
384 setUpProducer(adapter, igbProducer);
385 int slot;
386 sp<Fence> fence;
387 sp<GraphicBuffer> buf;
388 auto ret = igbProducer->dequeueBuffer(&slot, &fence, mDisplayWidth, mDisplayHeight,
389 PIXEL_FORMAT_RGBA_8888, GRALLOC_USAGE_SW_WRITE_OFTEN,
390 nullptr, nullptr);
391 ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, ret);
392 ASSERT_EQ(OK, igbProducer->requestBuffer(slot, &buf));
393
394 uint32_t* bufData;
395 buf->lock(static_cast<uint32_t>(GraphicBuffer::USAGE_SW_WRITE_OFTEN),
396 reinterpret_cast<void**>(&bufData));
397 fillBuffer(bufData, Rect(buf->getWidth(), buf->getHeight() / 2), buf->getStride(), r, g, b);
398 buf->unlock();
399
400 IGraphicBufferProducer::QueueBufferOutput qbOutput;
401 IGraphicBufferProducer::QueueBufferInput input(systemTime(), false, HAL_DATASPACE_UNKNOWN,
402 Rect(mDisplayWidth, mDisplayHeight / 2),
403 NATIVE_WINDOW_SCALING_MODE_FREEZE, 0,
404 Fence::NO_FENCE);
405 igbProducer->queueBuffer(slot, input, &qbOutput);
Dominik Laskowski718f9602019-11-09 20:01:35 -0800406 ASSERT_NE(ui::Transform::ROT_INVALID, qbOutput.transformHint);
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800407
408 adapter.waitForCallbacks();
409 // capture screen and verify that it is red
chaviw8ffc7b82020-08-18 11:25:37 -0700410 ASSERT_EQ(NO_ERROR, captureDisplay(mCaptureArgs, mCaptureResults));
chaviwd2432892020-07-24 17:42:39 -0700411
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800412 ASSERT_NO_FATAL_FAILURE(
413 checkScreenCapture(r, g, b, {0, 0, (int32_t)mDisplayWidth, (int32_t)mDisplayHeight}));
414}
415
416TEST_F(BLASTBufferQueueTest, SetCrop_ScalingModeScaleCrop) {
417 uint8_t r = 255;
418 uint8_t g = 0;
419 uint8_t b = 0;
420
421 int32_t bufferSideLength =
422 (mDisplayWidth < mDisplayHeight) ? mDisplayWidth / 2 : mDisplayHeight / 2;
423 int32_t finalCropSideLength = bufferSideLength / 2;
424
425 auto bg = mClient->createSurface(String8("BGTest"), 0, 0, PIXEL_FORMAT_RGBA_8888,
Vishnu Nairfa247b12020-02-11 08:58:26 -0800426 ISurfaceComposerClient::eFXSurfaceEffect);
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800427 ASSERT_NE(nullptr, bg.get());
428 Transaction t;
429 t.setLayerStack(bg, 0)
430 .setCrop_legacy(bg, Rect(0, 0, mDisplayWidth, mDisplayHeight))
431 .setColor(bg, half3{0, 0, 0})
432 .setLayer(bg, 0)
433 .apply();
434
435 BLASTBufferQueueHelper adapter(mSurfaceControl, bufferSideLength, bufferSideLength);
436 sp<IGraphicBufferProducer> igbProducer;
437 setUpProducer(adapter, igbProducer);
438 int slot;
439 sp<Fence> fence;
440 sp<GraphicBuffer> buf;
441 auto ret = igbProducer->dequeueBuffer(&slot, &fence, bufferSideLength, bufferSideLength,
442 PIXEL_FORMAT_RGBA_8888, GRALLOC_USAGE_SW_WRITE_OFTEN,
443 nullptr, nullptr);
444 ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, ret);
445 ASSERT_EQ(OK, igbProducer->requestBuffer(slot, &buf));
446
447 uint32_t* bufData;
448 buf->lock(static_cast<uint32_t>(GraphicBuffer::USAGE_SW_WRITE_OFTEN),
449 reinterpret_cast<void**>(&bufData));
450 fillBuffer(bufData, Rect(buf->getWidth(), buf->getHeight()), buf->getStride(), 0, 0, 0);
451 fillBuffer(bufData,
452 Rect(finalCropSideLength / 2, 0, buf->getWidth() - finalCropSideLength / 2,
453 buf->getHeight()),
454 buf->getStride(), r, g, b);
455 buf->unlock();
456
457 IGraphicBufferProducer::QueueBufferOutput qbOutput;
458 IGraphicBufferProducer::QueueBufferInput input(systemTime(), false, HAL_DATASPACE_UNKNOWN,
459 Rect(bufferSideLength, finalCropSideLength),
460 NATIVE_WINDOW_SCALING_MODE_SCALE_CROP, 0,
461 Fence::NO_FENCE);
462 igbProducer->queueBuffer(slot, input, &qbOutput);
Dominik Laskowski718f9602019-11-09 20:01:35 -0800463 ASSERT_NE(ui::Transform::ROT_INVALID, qbOutput.transformHint);
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800464
465 adapter.waitForCallbacks();
466 // capture screen and verify that it is red
chaviw8ffc7b82020-08-18 11:25:37 -0700467 ASSERT_EQ(NO_ERROR, captureDisplay(mCaptureArgs, mCaptureResults));
chaviwd2432892020-07-24 17:42:39 -0700468
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800469 ASSERT_NO_FATAL_FAILURE(
470 checkScreenCapture(r, g, b,
471 {0, 0, (int32_t)bufferSideLength, (int32_t)bufferSideLength}));
Valerie Hau5977fc82019-12-05 15:56:39 -0800472 ASSERT_NO_FATAL_FAILURE(
473 checkScreenCapture(0, 0, 0,
474 {0, 0, (int32_t)bufferSideLength, (int32_t)bufferSideLength},
475 /*border*/ 0, /*outsideRegion*/ true));
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800476}
477
Valerie Hau5977fc82019-12-05 15:56:39 -0800478class BLASTBufferQueueTransformTest : public BLASTBufferQueueTest {
479public:
480 void test(uint32_t tr) {
481 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
482 sp<IGraphicBufferProducer> igbProducer;
483 setUpProducer(adapter, igbProducer);
484
485 auto bufWidth = mDisplayWidth;
486 auto bufHeight = mDisplayHeight;
487 int slot;
488 sp<Fence> fence;
489 sp<GraphicBuffer> buf;
490
491 auto ret = igbProducer->dequeueBuffer(&slot, &fence, bufWidth, bufHeight,
492 PIXEL_FORMAT_RGBA_8888, GRALLOC_USAGE_SW_WRITE_OFTEN,
493 nullptr, nullptr);
494 ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, ret);
495 ASSERT_EQ(OK, igbProducer->requestBuffer(slot, &buf));
496
497 fillQuadrants(buf);
498
499 IGraphicBufferProducer::QueueBufferOutput qbOutput;
500 IGraphicBufferProducer::QueueBufferInput input(systemTime(), false, HAL_DATASPACE_UNKNOWN,
501 Rect(bufWidth, bufHeight),
Vishnu Naire1a42322020-10-02 17:42:04 -0700502 NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW,
503 tr, Fence::NO_FENCE);
Valerie Hau5977fc82019-12-05 15:56:39 -0800504 igbProducer->queueBuffer(slot, input, &qbOutput);
Dominik Laskowski718f9602019-11-09 20:01:35 -0800505 ASSERT_NE(ui::Transform::ROT_INVALID, qbOutput.transformHint);
Valerie Hau5977fc82019-12-05 15:56:39 -0800506
507 adapter.waitForCallbacks();
chaviw8ffc7b82020-08-18 11:25:37 -0700508 ASSERT_EQ(NO_ERROR, captureDisplay(mCaptureArgs, mCaptureResults));
chaviwd2432892020-07-24 17:42:39 -0700509
Valerie Hau5977fc82019-12-05 15:56:39 -0800510 switch (tr) {
511 case ui::Transform::ROT_0:
512 ASSERT_NO_FATAL_FAILURE(checkScreenCapture(0, 0, 0,
513 {0, 0, (int32_t)mDisplayWidth / 2,
514 (int32_t)mDisplayHeight / 2},
515 1));
516 ASSERT_NO_FATAL_FAILURE(
517 checkScreenCapture(255, 0, 0,
518 {(int32_t)mDisplayWidth / 2, 0, (int32_t)mDisplayWidth,
519 (int32_t)mDisplayHeight / 2},
520 1));
521 ASSERT_NO_FATAL_FAILURE(
522 checkScreenCapture(0, 255, 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, 255,
528 {0, (int32_t)mDisplayHeight / 2,
529 (int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight},
530 1));
531 break;
532 case ui::Transform::FLIP_H:
533 ASSERT_NO_FATAL_FAILURE(checkScreenCapture(255, 0, 0,
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(0, 0, 255,
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::FLIP_V:
554 ASSERT_NO_FATAL_FAILURE(checkScreenCapture(0, 0, 255,
555 {0, 0, (int32_t)mDisplayWidth / 2,
556 (int32_t)mDisplayHeight / 2},
557 1));
558 ASSERT_NO_FATAL_FAILURE(
559 checkScreenCapture(0, 255, 0,
560 {(int32_t)mDisplayWidth / 2, 0, (int32_t)mDisplayWidth,
561 (int32_t)mDisplayHeight / 2},
562 1));
563 ASSERT_NO_FATAL_FAILURE(
564 checkScreenCapture(255, 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(0, 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_90:
575 ASSERT_NO_FATAL_FAILURE(checkScreenCapture(0, 0, 255,
576 {0, 0, (int32_t)mDisplayWidth / 2,
577 (int32_t)mDisplayHeight / 2},
578 1));
579 ASSERT_NO_FATAL_FAILURE(
580 checkScreenCapture(0, 0, 0,
581 {(int32_t)mDisplayWidth / 2, 0, (int32_t)mDisplayWidth,
582 (int32_t)mDisplayHeight / 2},
583 1));
584 ASSERT_NO_FATAL_FAILURE(
585 checkScreenCapture(255, 0, 0,
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, 255, 0,
591 {0, (int32_t)mDisplayHeight / 2,
592 (int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight},
593 1));
594 break;
595 case ui::Transform::ROT_180:
596 ASSERT_NO_FATAL_FAILURE(checkScreenCapture(0, 255, 0,
597 {0, 0, (int32_t)mDisplayWidth / 2,
598 (int32_t)mDisplayHeight / 2},
599 1));
600 ASSERT_NO_FATAL_FAILURE(
601 checkScreenCapture(0, 0, 255,
602 {(int32_t)mDisplayWidth / 2, 0, (int32_t)mDisplayWidth,
603 (int32_t)mDisplayHeight / 2},
604 1));
605 ASSERT_NO_FATAL_FAILURE(
606 checkScreenCapture(0, 0, 0,
607 {(int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight / 2,
608 (int32_t)mDisplayWidth, (int32_t)mDisplayHeight},
609 1));
610 ASSERT_NO_FATAL_FAILURE(
611 checkScreenCapture(255, 0, 0,
612 {0, (int32_t)mDisplayHeight / 2,
613 (int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight},
614 1));
615 break;
616 case ui::Transform::ROT_270:
617 ASSERT_NO_FATAL_FAILURE(checkScreenCapture(255, 0, 0,
618 {0, 0, (int32_t)mDisplayWidth / 2,
619 (int32_t)mDisplayHeight / 2},
620 1));
621 ASSERT_NO_FATAL_FAILURE(
622 checkScreenCapture(0, 255, 0,
623 {(int32_t)mDisplayWidth / 2, 0, (int32_t)mDisplayWidth,
624 (int32_t)mDisplayHeight / 2},
625 1));
626 ASSERT_NO_FATAL_FAILURE(
627 checkScreenCapture(0, 0, 255,
628 {(int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight / 2,
629 (int32_t)mDisplayWidth, (int32_t)mDisplayHeight},
630 1));
631 ASSERT_NO_FATAL_FAILURE(
632 checkScreenCapture(0, 0, 0,
633 {0, (int32_t)mDisplayHeight / 2,
634 (int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight},
635 1));
636 }
637 }
638};
639
640TEST_F(BLASTBufferQueueTransformTest, setTransform_ROT_0) {
641 test(ui::Transform::ROT_0);
642}
643
644TEST_F(BLASTBufferQueueTransformTest, setTransform_FLIP_H) {
645 test(ui::Transform::FLIP_H);
646}
647
648TEST_F(BLASTBufferQueueTransformTest, setTransform_FLIP_V) {
649 test(ui::Transform::FLIP_V);
650}
651
652TEST_F(BLASTBufferQueueTransformTest, setTransform_ROT_90) {
653 test(ui::Transform::ROT_90);
654}
655
656TEST_F(BLASTBufferQueueTransformTest, setTransform_ROT_180) {
657 test(ui::Transform::ROT_180);
658}
659
660TEST_F(BLASTBufferQueueTransformTest, setTransform_ROT_270) {
661 test(ui::Transform::ROT_270);
662}
Valerie Hau871d6352020-01-29 08:44:02 -0800663
664class BLASTFrameEventHistoryTest : public BLASTBufferQueueTest {
665public:
666 void setUpAndQueueBuffer(const sp<IGraphicBufferProducer>& igbProducer,
667 nsecs_t* requestedPresentTime, nsecs_t* postedTime,
668 IGraphicBufferProducer::QueueBufferOutput* qbOutput,
669 bool getFrameTimestamps) {
670 int slot;
671 sp<Fence> fence;
672 sp<GraphicBuffer> buf;
673 auto ret = igbProducer->dequeueBuffer(&slot, &fence, mDisplayWidth, mDisplayHeight,
674 PIXEL_FORMAT_RGBA_8888, GRALLOC_USAGE_SW_WRITE_OFTEN,
675 nullptr, nullptr);
676 ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, ret);
677 ASSERT_EQ(OK, igbProducer->requestBuffer(slot, &buf));
678
679 nsecs_t requestedTime = systemTime();
680 if (requestedPresentTime) *requestedPresentTime = requestedTime;
681 IGraphicBufferProducer::QueueBufferInput input(requestedTime, false, HAL_DATASPACE_UNKNOWN,
682 Rect(mDisplayWidth, mDisplayHeight),
683 NATIVE_WINDOW_SCALING_MODE_FREEZE, 0,
684 Fence::NO_FENCE, /*sticky*/ 0,
685 getFrameTimestamps);
686 if (postedTime) *postedTime = systemTime();
687 igbProducer->queueBuffer(slot, input, qbOutput);
688 }
689};
690
691TEST_F(BLASTFrameEventHistoryTest, FrameEventHistory_Basic) {
692 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
693 sp<IGraphicBufferProducer> igbProducer;
694 ProducerFrameEventHistory history;
695 setUpProducer(adapter, igbProducer);
696
697 IGraphicBufferProducer::QueueBufferOutput qbOutput;
698 nsecs_t requestedPresentTimeA = 0;
699 nsecs_t postedTimeA = 0;
700 setUpAndQueueBuffer(igbProducer, &requestedPresentTimeA, &postedTimeA, &qbOutput, true);
701 history.applyDelta(qbOutput.frameTimestamps);
702
703 FrameEvents* events = nullptr;
704 events = history.getFrame(1);
705 ASSERT_NE(nullptr, events);
706 ASSERT_EQ(1, events->frameNumber);
707 ASSERT_EQ(requestedPresentTimeA, events->requestedPresentTime);
708 ASSERT_GE(events->postedTime, postedTimeA);
709
710 adapter.waitForCallbacks();
711
712 // queue another buffer so we query for frame event deltas
713 nsecs_t requestedPresentTimeB = 0;
714 nsecs_t postedTimeB = 0;
715 setUpAndQueueBuffer(igbProducer, &requestedPresentTimeB, &postedTimeB, &qbOutput, true);
716 history.applyDelta(qbOutput.frameTimestamps);
717 events = history.getFrame(1);
718 ASSERT_NE(nullptr, events);
719
720 // frame number, requestedPresentTime, and postTime should not have changed
721 ASSERT_EQ(1, events->frameNumber);
722 ASSERT_EQ(requestedPresentTimeA, events->requestedPresentTime);
723 ASSERT_GE(events->postedTime, postedTimeA);
724
725 ASSERT_GE(events->latchTime, postedTimeA);
726 ASSERT_GE(events->dequeueReadyTime, events->latchTime);
727 ASSERT_NE(nullptr, events->gpuCompositionDoneFence);
728 ASSERT_NE(nullptr, events->displayPresentFence);
729 ASSERT_NE(nullptr, events->releaseFence);
730
731 // we should also have gotten the initial values for the next frame
732 events = history.getFrame(2);
733 ASSERT_NE(nullptr, events);
734 ASSERT_EQ(2, events->frameNumber);
735 ASSERT_EQ(requestedPresentTimeB, events->requestedPresentTime);
736 ASSERT_GE(events->postedTime, postedTimeB);
Valerie Hau78491e92020-04-15 13:10:56 -0700737
738 // wait for any callbacks that have not been received
739 adapter.waitForCallbacks();
Valerie Hau871d6352020-01-29 08:44:02 -0800740}
Valerie Hauc5011f92019-10-11 09:52:07 -0700741} // namespace android