blob: d88c47758094208c7be038f987bbacc16a22c062 [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>
Valerie Hauda3446e2019-10-14 15:49:22 -070028#include <private/gui/ComposerService.h>
Dominik Laskowski3cb3d4e2019-11-21 11:14:45 -080029#include <ui/DisplayConfig.h>
Valerie Hauc5011f92019-10-11 09:52:07 -070030#include <ui/GraphicBuffer.h>
Valerie Hauda3446e2019-10-14 15:49:22 -070031#include <ui/GraphicTypes.h>
Valerie Hau8cee3f92019-11-06 10:06:28 -080032#include <ui/Transform.h>
Valerie Hauc5011f92019-10-11 09:52:07 -070033
34#include <gtest/gtest.h>
35
36using namespace std::chrono_literals;
37
38namespace android {
39
Valerie Hauc5011f92019-10-11 09:52:07 -070040using Transaction = SurfaceComposerClient::Transaction;
Valerie Hauda3446e2019-10-14 15:49:22 -070041using android::hardware::graphics::common::V1_2::BufferUsage;
Valerie Hauc5011f92019-10-11 09:52:07 -070042
43class BLASTBufferQueueHelper {
44public:
45 BLASTBufferQueueHelper(const sp<SurfaceControl>& sc, int width, int height) {
46 mBlastBufferQueueAdapter = new BLASTBufferQueue(sc, width, height);
47 }
48
49 void update(const sp<SurfaceControl>& sc, int width, int height) {
50 mBlastBufferQueueAdapter->update(sc, width, height);
51 }
52
53 void setNextTransaction(Transaction* next) {
54 mBlastBufferQueueAdapter->setNextTransaction(next);
55 }
56
57 int getWidth() { return mBlastBufferQueueAdapter->mWidth; }
Valerie Hauda3446e2019-10-14 15:49:22 -070058
Valerie Hauc5011f92019-10-11 09:52:07 -070059 int getHeight() { return mBlastBufferQueueAdapter->mHeight; }
Valerie Hauda3446e2019-10-14 15:49:22 -070060
Valerie Hauc5011f92019-10-11 09:52:07 -070061 Transaction* getNextTransaction() { return mBlastBufferQueueAdapter->mNextTransaction; }
Valerie Hauda3446e2019-10-14 15:49:22 -070062
63 sp<IGraphicBufferProducer> getIGraphicBufferProducer() {
64 return mBlastBufferQueueAdapter->getIGraphicBufferProducer();
65 }
66
Valerie Hauc5011f92019-10-11 09:52:07 -070067 const sp<SurfaceControl> getSurfaceControl() {
68 return mBlastBufferQueueAdapter->mSurfaceControl;
69 }
70
Valerie Haud3b90d22019-11-06 09:37:31 -080071 void waitForCallbacks() {
Valerie Hauda3446e2019-10-14 15:49:22 -070072 std::unique_lock lock{mBlastBufferQueueAdapter->mMutex};
Valerie Haua32c5522019-12-09 10:11:08 -080073 while (mBlastBufferQueueAdapter->mSubmitted.size() > 0) {
Valerie Haud3b90d22019-11-06 09:37:31 -080074 mBlastBufferQueueAdapter->mCallbackCV.wait(lock);
75 }
Valerie Hauda3446e2019-10-14 15:49:22 -070076 }
77
Valerie Hauc5011f92019-10-11 09:52:07 -070078private:
79 sp<BLASTBufferQueue> mBlastBufferQueueAdapter;
80};
81
82class BLASTBufferQueueTest : public ::testing::Test {
83public:
84protected:
85 BLASTBufferQueueTest() {
86 const ::testing::TestInfo* const testInfo =
87 ::testing::UnitTest::GetInstance()->current_test_info();
88 ALOGV("Begin test: %s.%s", testInfo->test_case_name(), testInfo->name());
89 }
90
91 ~BLASTBufferQueueTest() {
92 const ::testing::TestInfo* const testInfo =
93 ::testing::UnitTest::GetInstance()->current_test_info();
94 ALOGV("End test: %s.%s", testInfo->test_case_name(), testInfo->name());
95 }
96
97 void SetUp() {
Valerie Hauda3446e2019-10-14 15:49:22 -070098 mComposer = ComposerService::getComposerService();
Valerie Hauc5011f92019-10-11 09:52:07 -070099 mClient = new SurfaceComposerClient();
Valerie Hauda3446e2019-10-14 15:49:22 -0700100 mDisplayToken = mClient->getInternalDisplayToken();
101 ASSERT_NE(nullptr, mDisplayToken.get());
102 Transaction t;
103 t.setDisplayLayerStack(mDisplayToken, 0);
104 t.apply();
105 t.clear();
106
Dominik Laskowski3cb3d4e2019-11-21 11:14:45 -0800107 DisplayConfig config;
108 ASSERT_EQ(NO_ERROR, SurfaceComposerClient::getActiveDisplayConfig(mDisplayToken, &config));
109 const ui::Size& resolution = config.resolution;
110 mDisplayWidth = resolution.getWidth();
111 mDisplayHeight = resolution.getHeight();
Valerie Hauda3446e2019-10-14 15:49:22 -0700112
113 mSurfaceControl = mClient->createSurface(String8("TestSurface"), mDisplayWidth,
114 mDisplayHeight, PIXEL_FORMAT_RGBA_8888,
115 ISurfaceComposerClient::eFXSurfaceBufferState,
116 /*parent*/ nullptr);
117 t.setLayerStack(mSurfaceControl, 0)
118 .setLayer(mSurfaceControl, std::numeric_limits<int32_t>::max())
Dominik Laskowski3cb3d4e2019-11-21 11:14:45 -0800119 .setFrame(mSurfaceControl, Rect(resolution))
Valerie Hauda3446e2019-10-14 15:49:22 -0700120 .show(mSurfaceControl)
121 .setDataspace(mSurfaceControl, ui::Dataspace::V0_SRGB)
122 .apply();
chaviwd2432892020-07-24 17:42:39 -0700123
124 mCaptureArgs.displayToken = mDisplayToken;
Valerie Hauda3446e2019-10-14 15:49:22 -0700125 }
126
Valerie Haud3b90d22019-11-06 09:37:31 -0800127 void setUpProducer(BLASTBufferQueueHelper adapter, sp<IGraphicBufferProducer>& producer) {
128 auto igbProducer = adapter.getIGraphicBufferProducer();
129 ASSERT_NE(nullptr, igbProducer.get());
Valerie Hauc78c43a2020-01-09 17:34:14 -0800130 ASSERT_EQ(NO_ERROR, igbProducer->setMaxDequeuedBufferCount(2));
Valerie Haud3b90d22019-11-06 09:37:31 -0800131 IGraphicBufferProducer::QueueBufferOutput qbOutput;
132 ASSERT_EQ(NO_ERROR,
Peiyong Lind8460c82020-07-28 16:04:22 -0700133 igbProducer->connect(new StubProducerListener, NATIVE_WINDOW_API_CPU, false,
Valerie Haud3b90d22019-11-06 09:37:31 -0800134 &qbOutput));
Dominik Laskowski718f9602019-11-09 20:01:35 -0800135 ASSERT_NE(ui::Transform::ROT_INVALID, qbOutput.transformHint);
Valerie Haud3b90d22019-11-06 09:37:31 -0800136 producer = igbProducer;
137 }
138
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800139 void fillBuffer(uint32_t* bufData, Rect rect, uint32_t stride, uint8_t r, uint8_t g,
140 uint8_t b) {
141 for (uint32_t row = rect.top; row < rect.bottom; row++) {
142 for (uint32_t col = rect.left; col < rect.right; col++) {
Valerie Hauda3446e2019-10-14 15:49:22 -0700143 uint8_t* pixel = (uint8_t*)(bufData + (row * stride) + col);
144 *pixel = r;
145 *(pixel + 1) = g;
146 *(pixel + 2) = b;
147 *(pixel + 3) = 255;
148 }
149 }
150 }
151
Valerie Hau5977fc82019-12-05 15:56:39 -0800152 void fillQuadrants(sp<GraphicBuffer>& buf) {
153 const auto bufWidth = buf->getWidth();
154 const auto bufHeight = buf->getHeight();
155 uint32_t* bufData;
156 buf->lock(static_cast<uint32_t>(GraphicBuffer::USAGE_SW_WRITE_OFTEN),
157 reinterpret_cast<void**>(&bufData));
158 fillBuffer(bufData, Rect(0, 0, bufWidth / 2, bufHeight / 2), buf->getStride(), 0, 0, 0);
159 fillBuffer(bufData, Rect(bufWidth / 2, 0, bufWidth, bufHeight / 2), buf->getStride(), 255,
160 0, 0);
161 fillBuffer(bufData, Rect(bufWidth / 2, bufHeight / 2, bufWidth, bufHeight),
162 buf->getStride(), 0, 255, 0);
163 fillBuffer(bufData, Rect(0, bufHeight / 2, bufWidth / 2, bufHeight), buf->getStride(), 0, 0,
164 255);
165 buf->unlock();
166 }
167
168 void checkScreenCapture(uint8_t r, uint8_t g, uint8_t b, Rect region, int32_t border = 0,
169 bool outsideRegion = false) {
chaviwd2432892020-07-24 17:42:39 -0700170 sp<GraphicBuffer>& captureBuf = mCaptureResults.buffer;
Valerie Hau5977fc82019-12-05 15:56:39 -0800171 const auto epsilon = 3;
chaviwd2432892020-07-24 17:42:39 -0700172 const auto width = captureBuf->getWidth();
173 const auto height = captureBuf->getHeight();
174 const auto stride = captureBuf->getStride();
Valerie Hauda3446e2019-10-14 15:49:22 -0700175
176 uint32_t* bufData;
chaviwd2432892020-07-24 17:42:39 -0700177 captureBuf->lock(static_cast<uint32_t>(GraphicBuffer::USAGE_SW_READ_OFTEN),
178 reinterpret_cast<void**>(&bufData));
Valerie Hauda3446e2019-10-14 15:49:22 -0700179
180 for (uint32_t row = 0; row < height; row++) {
181 for (uint32_t col = 0; col < width; col++) {
182 uint8_t* pixel = (uint8_t*)(bufData + (row * stride) + col);
Valerie Hau5977fc82019-12-05 15:56:39 -0800183 bool inRegion;
184 if (!outsideRegion) {
185 inRegion = row >= region.top + border && row < region.bottom - border &&
186 col >= region.left + border && col < region.right - border;
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800187 } else {
Valerie Hau5977fc82019-12-05 15:56:39 -0800188 inRegion = row >= region.top - border && row < region.bottom + border &&
189 col >= region.left - border && col < region.right + border;
190 }
191 if (!outsideRegion && inRegion) {
192 EXPECT_GE(epsilon, abs(r - *(pixel)));
193 EXPECT_GE(epsilon, abs(g - *(pixel + 1)));
194 EXPECT_GE(epsilon, abs(b - *(pixel + 2)));
195 } else if (outsideRegion && !inRegion) {
196 EXPECT_GE(epsilon, abs(r - *(pixel)));
197 EXPECT_GE(epsilon, abs(g - *(pixel + 1)));
198 EXPECT_GE(epsilon, abs(b - *(pixel + 2)));
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800199 }
Valerie Hauda3446e2019-10-14 15:49:22 -0700200 }
201 }
chaviwd2432892020-07-24 17:42:39 -0700202 captureBuf->unlock();
Valerie Hauda3446e2019-10-14 15:49:22 -0700203 ASSERT_EQ(false, ::testing::Test::HasFailure());
Valerie Hauc5011f92019-10-11 09:52:07 -0700204 }
205
206 sp<SurfaceComposerClient> mClient;
Valerie Hauda3446e2019-10-14 15:49:22 -0700207 sp<ISurfaceComposer> mComposer;
208
209 sp<IBinder> mDisplayToken;
210
Valerie Hauc5011f92019-10-11 09:52:07 -0700211 sp<SurfaceControl> mSurfaceControl;
Valerie Hauda3446e2019-10-14 15:49:22 -0700212
213 uint32_t mDisplayWidth;
214 uint32_t mDisplayHeight;
chaviwd2432892020-07-24 17:42:39 -0700215
216 DisplayCaptureArgs mCaptureArgs;
217 ScreenCaptureResults mCaptureResults;
Valerie Hauc5011f92019-10-11 09:52:07 -0700218};
219
220TEST_F(BLASTBufferQueueTest, CreateBLASTBufferQueue) {
221 // create BLASTBufferQueue adapter associated with this surface
Valerie Hauda3446e2019-10-14 15:49:22 -0700222 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
Valerie Hauc5011f92019-10-11 09:52:07 -0700223 ASSERT_EQ(mSurfaceControl, adapter.getSurfaceControl());
Valerie Hauda3446e2019-10-14 15:49:22 -0700224 ASSERT_EQ(mDisplayWidth, adapter.getWidth());
225 ASSERT_EQ(mDisplayHeight, adapter.getHeight());
Valerie Hauc5011f92019-10-11 09:52:07 -0700226 ASSERT_EQ(nullptr, adapter.getNextTransaction());
227}
228
229TEST_F(BLASTBufferQueueTest, Update) {
Valerie Hauda3446e2019-10-14 15:49:22 -0700230 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
Valerie Hauc5011f92019-10-11 09:52:07 -0700231 sp<SurfaceControl> updateSurface =
Valerie Hauda3446e2019-10-14 15:49:22 -0700232 mClient->createSurface(String8("UpdateTest"), mDisplayWidth / 2, mDisplayHeight / 2,
233 PIXEL_FORMAT_RGBA_8888);
234 adapter.update(updateSurface, mDisplayWidth / 2, mDisplayHeight / 2);
Valerie Hauc5011f92019-10-11 09:52:07 -0700235 ASSERT_EQ(updateSurface, adapter.getSurfaceControl());
Valerie Hauda3446e2019-10-14 15:49:22 -0700236 ASSERT_EQ(mDisplayWidth / 2, adapter.getWidth());
237 ASSERT_EQ(mDisplayHeight / 2, adapter.getHeight());
Valerie Hauc5011f92019-10-11 09:52:07 -0700238}
239
240TEST_F(BLASTBufferQueueTest, SetNextTransaction) {
Valerie Hauda3446e2019-10-14 15:49:22 -0700241 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
Valerie Hauc5011f92019-10-11 09:52:07 -0700242 Transaction next;
243 adapter.setNextTransaction(&next);
244 ASSERT_EQ(&next, adapter.getNextTransaction());
245}
Valerie Hauda3446e2019-10-14 15:49:22 -0700246
Valerie Haubf29e042020-02-06 11:40:38 -0800247TEST_F(BLASTBufferQueueTest, DISABLED_onFrameAvailable_ApplyDesiredPresentTime) {
Valerie Hau181abd32020-01-27 14:18:28 -0800248 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
249 sp<IGraphicBufferProducer> igbProducer;
250 setUpProducer(adapter, igbProducer);
251
252 int slot;
253 sp<Fence> fence;
254 sp<GraphicBuffer> buf;
255 auto ret = igbProducer->dequeueBuffer(&slot, &fence, mDisplayWidth, mDisplayHeight,
256 PIXEL_FORMAT_RGBA_8888, GRALLOC_USAGE_SW_WRITE_OFTEN,
257 nullptr, nullptr);
258 ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, ret);
259 ASSERT_EQ(OK, igbProducer->requestBuffer(slot, &buf));
260
261 nsecs_t desiredPresentTime = systemTime() + nsecs_t(5 * 1e8);
262 IGraphicBufferProducer::QueueBufferOutput qbOutput;
263 IGraphicBufferProducer::QueueBufferInput input(desiredPresentTime, false, HAL_DATASPACE_UNKNOWN,
264 Rect(mDisplayWidth, mDisplayHeight),
265 NATIVE_WINDOW_SCALING_MODE_FREEZE, 0,
266 Fence::NO_FENCE);
267 igbProducer->queueBuffer(slot, input, &qbOutput);
268 ASSERT_NE(ui::Transform::ROT_INVALID, qbOutput.transformHint);
269
270 adapter.waitForCallbacks();
271 ASSERT_GE(systemTime(), desiredPresentTime);
272}
273
Valerie Hauda3446e2019-10-14 15:49:22 -0700274TEST_F(BLASTBufferQueueTest, onFrameAvailable_Apply) {
275 uint8_t r = 255;
276 uint8_t g = 0;
277 uint8_t b = 0;
278
279 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
Valerie Haud3b90d22019-11-06 09:37:31 -0800280 sp<IGraphicBufferProducer> igbProducer;
281 setUpProducer(adapter, igbProducer);
Valerie Hauda3446e2019-10-14 15:49:22 -0700282
283 int slot;
284 sp<Fence> fence;
285 sp<GraphicBuffer> buf;
286 auto ret = igbProducer->dequeueBuffer(&slot, &fence, mDisplayWidth, mDisplayHeight,
287 PIXEL_FORMAT_RGBA_8888, GRALLOC_USAGE_SW_WRITE_OFTEN,
288 nullptr, nullptr);
289 ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, ret);
290 ASSERT_EQ(OK, igbProducer->requestBuffer(slot, &buf));
291
292 uint32_t* bufData;
293 buf->lock(static_cast<uint32_t>(GraphicBuffer::USAGE_SW_WRITE_OFTEN),
294 reinterpret_cast<void**>(&bufData));
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800295 fillBuffer(bufData, Rect(buf->getWidth(), buf->getHeight()), buf->getStride(), r, g, b);
Valerie Hauda3446e2019-10-14 15:49:22 -0700296 buf->unlock();
297
Valerie Haud3b90d22019-11-06 09:37:31 -0800298 IGraphicBufferProducer::QueueBufferOutput qbOutput;
Valerie Hauda3446e2019-10-14 15:49:22 -0700299 IGraphicBufferProducer::QueueBufferInput input(systemTime(), false, HAL_DATASPACE_UNKNOWN,
300 Rect(mDisplayWidth, mDisplayHeight),
301 NATIVE_WINDOW_SCALING_MODE_FREEZE, 0,
302 Fence::NO_FENCE);
303 igbProducer->queueBuffer(slot, input, &qbOutput);
Dominik Laskowski718f9602019-11-09 20:01:35 -0800304 ASSERT_NE(ui::Transform::ROT_INVALID, qbOutput.transformHint);
Valerie Hauda3446e2019-10-14 15:49:22 -0700305
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800306 adapter.waitForCallbacks();
Valerie Hauda3446e2019-10-14 15:49:22 -0700307
308 // capture screen and verify that it is red
chaviwd2432892020-07-24 17:42:39 -0700309 ASSERT_EQ(NO_ERROR, mComposer->captureDisplay(mCaptureArgs, mCaptureResults));
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800310 ASSERT_NO_FATAL_FAILURE(
311 checkScreenCapture(r, g, b, {0, 0, (int32_t)mDisplayWidth, (int32_t)mDisplayHeight}));
Valerie Hauda3446e2019-10-14 15:49:22 -0700312}
Valerie Haud3b90d22019-11-06 09:37:31 -0800313
314TEST_F(BLASTBufferQueueTest, TripleBuffering) {
315 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
316 sp<IGraphicBufferProducer> igbProducer;
317 setUpProducer(adapter, igbProducer);
318
319 std::vector<std::pair<int, sp<Fence>>> allocated;
320 for (int i = 0; i < 3; i++) {
321 int slot;
322 sp<Fence> fence;
323 sp<GraphicBuffer> buf;
324 auto ret = igbProducer->dequeueBuffer(&slot, &fence, mDisplayWidth, mDisplayHeight,
325 PIXEL_FORMAT_RGBA_8888, GRALLOC_USAGE_SW_WRITE_OFTEN,
326 nullptr, nullptr);
327 ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, ret);
328 ASSERT_EQ(OK, igbProducer->requestBuffer(slot, &buf));
329 allocated.push_back({slot, fence});
330 }
331 for (int i = 0; i < allocated.size(); i++) {
332 igbProducer->cancelBuffer(allocated[i].first, allocated[i].second);
333 }
334
Valerie Haua32c5522019-12-09 10:11:08 -0800335 for (int i = 0; i < 100; i++) {
Valerie Haud3b90d22019-11-06 09:37:31 -0800336 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(NO_ERROR, ret);
343 IGraphicBufferProducer::QueueBufferOutput qbOutput;
344 IGraphicBufferProducer::QueueBufferInput input(systemTime(), false, HAL_DATASPACE_UNKNOWN,
345 Rect(mDisplayWidth, mDisplayHeight),
346 NATIVE_WINDOW_SCALING_MODE_FREEZE, 0,
347 Fence::NO_FENCE);
348 igbProducer->queueBuffer(slot, input, &qbOutput);
349 }
350 adapter.waitForCallbacks();
351}
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800352
353TEST_F(BLASTBufferQueueTest, SetCrop_Item) {
354 uint8_t r = 255;
355 uint8_t g = 0;
356 uint8_t b = 0;
357
358 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
359 sp<IGraphicBufferProducer> igbProducer;
360 setUpProducer(adapter, igbProducer);
361 int slot;
362 sp<Fence> fence;
363 sp<GraphicBuffer> buf;
364 auto ret = igbProducer->dequeueBuffer(&slot, &fence, mDisplayWidth, mDisplayHeight,
365 PIXEL_FORMAT_RGBA_8888, GRALLOC_USAGE_SW_WRITE_OFTEN,
366 nullptr, nullptr);
367 ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, ret);
368 ASSERT_EQ(OK, igbProducer->requestBuffer(slot, &buf));
369
370 uint32_t* bufData;
371 buf->lock(static_cast<uint32_t>(GraphicBuffer::USAGE_SW_WRITE_OFTEN),
372 reinterpret_cast<void**>(&bufData));
373 fillBuffer(bufData, Rect(buf->getWidth(), buf->getHeight() / 2), buf->getStride(), r, g, b);
374 buf->unlock();
375
376 IGraphicBufferProducer::QueueBufferOutput qbOutput;
377 IGraphicBufferProducer::QueueBufferInput input(systemTime(), false, HAL_DATASPACE_UNKNOWN,
378 Rect(mDisplayWidth, mDisplayHeight / 2),
379 NATIVE_WINDOW_SCALING_MODE_FREEZE, 0,
380 Fence::NO_FENCE);
381 igbProducer->queueBuffer(slot, input, &qbOutput);
Dominik Laskowski718f9602019-11-09 20:01:35 -0800382 ASSERT_NE(ui::Transform::ROT_INVALID, qbOutput.transformHint);
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800383
384 adapter.waitForCallbacks();
385 // capture screen and verify that it is red
chaviwd2432892020-07-24 17:42:39 -0700386 ASSERT_EQ(NO_ERROR, mComposer->captureDisplay(mCaptureArgs, mCaptureResults));
387
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800388 ASSERT_NO_FATAL_FAILURE(
389 checkScreenCapture(r, g, b, {0, 0, (int32_t)mDisplayWidth, (int32_t)mDisplayHeight}));
390}
391
392TEST_F(BLASTBufferQueueTest, SetCrop_ScalingModeScaleCrop) {
393 uint8_t r = 255;
394 uint8_t g = 0;
395 uint8_t b = 0;
396
397 int32_t bufferSideLength =
398 (mDisplayWidth < mDisplayHeight) ? mDisplayWidth / 2 : mDisplayHeight / 2;
399 int32_t finalCropSideLength = bufferSideLength / 2;
400
401 auto bg = mClient->createSurface(String8("BGTest"), 0, 0, PIXEL_FORMAT_RGBA_8888,
Vishnu Nairfa247b12020-02-11 08:58:26 -0800402 ISurfaceComposerClient::eFXSurfaceEffect);
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800403 ASSERT_NE(nullptr, bg.get());
404 Transaction t;
405 t.setLayerStack(bg, 0)
406 .setCrop_legacy(bg, Rect(0, 0, mDisplayWidth, mDisplayHeight))
407 .setColor(bg, half3{0, 0, 0})
408 .setLayer(bg, 0)
409 .apply();
410
411 BLASTBufferQueueHelper adapter(mSurfaceControl, bufferSideLength, bufferSideLength);
412 sp<IGraphicBufferProducer> igbProducer;
413 setUpProducer(adapter, igbProducer);
414 int slot;
415 sp<Fence> fence;
416 sp<GraphicBuffer> buf;
417 auto ret = igbProducer->dequeueBuffer(&slot, &fence, bufferSideLength, bufferSideLength,
418 PIXEL_FORMAT_RGBA_8888, GRALLOC_USAGE_SW_WRITE_OFTEN,
419 nullptr, nullptr);
420 ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, ret);
421 ASSERT_EQ(OK, igbProducer->requestBuffer(slot, &buf));
422
423 uint32_t* bufData;
424 buf->lock(static_cast<uint32_t>(GraphicBuffer::USAGE_SW_WRITE_OFTEN),
425 reinterpret_cast<void**>(&bufData));
426 fillBuffer(bufData, Rect(buf->getWidth(), buf->getHeight()), buf->getStride(), 0, 0, 0);
427 fillBuffer(bufData,
428 Rect(finalCropSideLength / 2, 0, buf->getWidth() - finalCropSideLength / 2,
429 buf->getHeight()),
430 buf->getStride(), r, g, b);
431 buf->unlock();
432
433 IGraphicBufferProducer::QueueBufferOutput qbOutput;
434 IGraphicBufferProducer::QueueBufferInput input(systemTime(), false, HAL_DATASPACE_UNKNOWN,
435 Rect(bufferSideLength, finalCropSideLength),
436 NATIVE_WINDOW_SCALING_MODE_SCALE_CROP, 0,
437 Fence::NO_FENCE);
438 igbProducer->queueBuffer(slot, input, &qbOutput);
Dominik Laskowski718f9602019-11-09 20:01:35 -0800439 ASSERT_NE(ui::Transform::ROT_INVALID, qbOutput.transformHint);
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800440
441 adapter.waitForCallbacks();
442 // capture screen and verify that it is red
chaviwd2432892020-07-24 17:42:39 -0700443 ASSERT_EQ(NO_ERROR, mComposer->captureDisplay(mCaptureArgs, mCaptureResults));
444
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800445 ASSERT_NO_FATAL_FAILURE(
446 checkScreenCapture(r, g, b,
447 {0, 0, (int32_t)bufferSideLength, (int32_t)bufferSideLength}));
Valerie Hau5977fc82019-12-05 15:56:39 -0800448 ASSERT_NO_FATAL_FAILURE(
449 checkScreenCapture(0, 0, 0,
450 {0, 0, (int32_t)bufferSideLength, (int32_t)bufferSideLength},
451 /*border*/ 0, /*outsideRegion*/ true));
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800452}
453
Valerie Hau5977fc82019-12-05 15:56:39 -0800454class BLASTBufferQueueTransformTest : public BLASTBufferQueueTest {
455public:
456 void test(uint32_t tr) {
457 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
458 sp<IGraphicBufferProducer> igbProducer;
459 setUpProducer(adapter, igbProducer);
460
461 auto bufWidth = mDisplayWidth;
462 auto bufHeight = mDisplayHeight;
463 int slot;
464 sp<Fence> fence;
465 sp<GraphicBuffer> buf;
466
467 auto ret = igbProducer->dequeueBuffer(&slot, &fence, bufWidth, bufHeight,
468 PIXEL_FORMAT_RGBA_8888, GRALLOC_USAGE_SW_WRITE_OFTEN,
469 nullptr, nullptr);
470 ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, ret);
471 ASSERT_EQ(OK, igbProducer->requestBuffer(slot, &buf));
472
473 fillQuadrants(buf);
474
475 IGraphicBufferProducer::QueueBufferOutput qbOutput;
476 IGraphicBufferProducer::QueueBufferInput input(systemTime(), false, HAL_DATASPACE_UNKNOWN,
477 Rect(bufWidth, bufHeight),
478 NATIVE_WINDOW_SCALING_MODE_FREEZE, tr,
479 Fence::NO_FENCE);
480 igbProducer->queueBuffer(slot, input, &qbOutput);
Dominik Laskowski718f9602019-11-09 20:01:35 -0800481 ASSERT_NE(ui::Transform::ROT_INVALID, qbOutput.transformHint);
Valerie Hau5977fc82019-12-05 15:56:39 -0800482
483 adapter.waitForCallbacks();
chaviwd2432892020-07-24 17:42:39 -0700484 ASSERT_EQ(NO_ERROR, mComposer->captureDisplay(mCaptureArgs, mCaptureResults));
485
Valerie Hau5977fc82019-12-05 15:56:39 -0800486 switch (tr) {
487 case ui::Transform::ROT_0:
488 ASSERT_NO_FATAL_FAILURE(checkScreenCapture(0, 0, 0,
489 {0, 0, (int32_t)mDisplayWidth / 2,
490 (int32_t)mDisplayHeight / 2},
491 1));
492 ASSERT_NO_FATAL_FAILURE(
493 checkScreenCapture(255, 0, 0,
494 {(int32_t)mDisplayWidth / 2, 0, (int32_t)mDisplayWidth,
495 (int32_t)mDisplayHeight / 2},
496 1));
497 ASSERT_NO_FATAL_FAILURE(
498 checkScreenCapture(0, 255, 0,
499 {(int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight / 2,
500 (int32_t)mDisplayWidth, (int32_t)mDisplayHeight},
501 1));
502 ASSERT_NO_FATAL_FAILURE(
503 checkScreenCapture(0, 0, 255,
504 {0, (int32_t)mDisplayHeight / 2,
505 (int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight},
506 1));
507 break;
508 case ui::Transform::FLIP_H:
509 ASSERT_NO_FATAL_FAILURE(checkScreenCapture(255, 0, 0,
510 {0, 0, (int32_t)mDisplayWidth / 2,
511 (int32_t)mDisplayHeight / 2},
512 1));
513 ASSERT_NO_FATAL_FAILURE(
514 checkScreenCapture(0, 0, 0,
515 {(int32_t)mDisplayWidth / 2, 0, (int32_t)mDisplayWidth,
516 (int32_t)mDisplayHeight / 2},
517 1));
518 ASSERT_NO_FATAL_FAILURE(
519 checkScreenCapture(0, 0, 255,
520 {(int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight / 2,
521 (int32_t)mDisplayWidth, (int32_t)mDisplayHeight},
522 1));
523 ASSERT_NO_FATAL_FAILURE(
524 checkScreenCapture(0, 255, 0,
525 {0, (int32_t)mDisplayHeight / 2,
526 (int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight},
527 1));
528 break;
529 case ui::Transform::FLIP_V:
530 ASSERT_NO_FATAL_FAILURE(checkScreenCapture(0, 0, 255,
531 {0, 0, (int32_t)mDisplayWidth / 2,
532 (int32_t)mDisplayHeight / 2},
533 1));
534 ASSERT_NO_FATAL_FAILURE(
535 checkScreenCapture(0, 255, 0,
536 {(int32_t)mDisplayWidth / 2, 0, (int32_t)mDisplayWidth,
537 (int32_t)mDisplayHeight / 2},
538 1));
539 ASSERT_NO_FATAL_FAILURE(
540 checkScreenCapture(255, 0, 0,
541 {(int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight / 2,
542 (int32_t)mDisplayWidth, (int32_t)mDisplayHeight},
543 1));
544 ASSERT_NO_FATAL_FAILURE(
545 checkScreenCapture(0, 0, 0,
546 {0, (int32_t)mDisplayHeight / 2,
547 (int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight},
548 1));
549 break;
550 case ui::Transform::ROT_90:
551 ASSERT_NO_FATAL_FAILURE(checkScreenCapture(0, 0, 255,
552 {0, 0, (int32_t)mDisplayWidth / 2,
553 (int32_t)mDisplayHeight / 2},
554 1));
555 ASSERT_NO_FATAL_FAILURE(
556 checkScreenCapture(0, 0, 0,
557 {(int32_t)mDisplayWidth / 2, 0, (int32_t)mDisplayWidth,
558 (int32_t)mDisplayHeight / 2},
559 1));
560 ASSERT_NO_FATAL_FAILURE(
561 checkScreenCapture(255, 0, 0,
562 {(int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight / 2,
563 (int32_t)mDisplayWidth, (int32_t)mDisplayHeight},
564 1));
565 ASSERT_NO_FATAL_FAILURE(
566 checkScreenCapture(0, 255, 0,
567 {0, (int32_t)mDisplayHeight / 2,
568 (int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight},
569 1));
570 break;
571 case ui::Transform::ROT_180:
572 ASSERT_NO_FATAL_FAILURE(checkScreenCapture(0, 255, 0,
573 {0, 0, (int32_t)mDisplayWidth / 2,
574 (int32_t)mDisplayHeight / 2},
575 1));
576 ASSERT_NO_FATAL_FAILURE(
577 checkScreenCapture(0, 0, 255,
578 {(int32_t)mDisplayWidth / 2, 0, (int32_t)mDisplayWidth,
579 (int32_t)mDisplayHeight / 2},
580 1));
581 ASSERT_NO_FATAL_FAILURE(
582 checkScreenCapture(0, 0, 0,
583 {(int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight / 2,
584 (int32_t)mDisplayWidth, (int32_t)mDisplayHeight},
585 1));
586 ASSERT_NO_FATAL_FAILURE(
587 checkScreenCapture(255, 0, 0,
588 {0, (int32_t)mDisplayHeight / 2,
589 (int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight},
590 1));
591 break;
592 case ui::Transform::ROT_270:
593 ASSERT_NO_FATAL_FAILURE(checkScreenCapture(255, 0, 0,
594 {0, 0, (int32_t)mDisplayWidth / 2,
595 (int32_t)mDisplayHeight / 2},
596 1));
597 ASSERT_NO_FATAL_FAILURE(
598 checkScreenCapture(0, 255, 0,
599 {(int32_t)mDisplayWidth / 2, 0, (int32_t)mDisplayWidth,
600 (int32_t)mDisplayHeight / 2},
601 1));
602 ASSERT_NO_FATAL_FAILURE(
603 checkScreenCapture(0, 0, 255,
604 {(int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight / 2,
605 (int32_t)mDisplayWidth, (int32_t)mDisplayHeight},
606 1));
607 ASSERT_NO_FATAL_FAILURE(
608 checkScreenCapture(0, 0, 0,
609 {0, (int32_t)mDisplayHeight / 2,
610 (int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight},
611 1));
612 }
613 }
614};
615
616TEST_F(BLASTBufferQueueTransformTest, setTransform_ROT_0) {
617 test(ui::Transform::ROT_0);
618}
619
620TEST_F(BLASTBufferQueueTransformTest, setTransform_FLIP_H) {
621 test(ui::Transform::FLIP_H);
622}
623
624TEST_F(BLASTBufferQueueTransformTest, setTransform_FLIP_V) {
625 test(ui::Transform::FLIP_V);
626}
627
628TEST_F(BLASTBufferQueueTransformTest, setTransform_ROT_90) {
629 test(ui::Transform::ROT_90);
630}
631
632TEST_F(BLASTBufferQueueTransformTest, setTransform_ROT_180) {
633 test(ui::Transform::ROT_180);
634}
635
636TEST_F(BLASTBufferQueueTransformTest, setTransform_ROT_270) {
637 test(ui::Transform::ROT_270);
638}
Valerie Hau871d6352020-01-29 08:44:02 -0800639
640class BLASTFrameEventHistoryTest : public BLASTBufferQueueTest {
641public:
642 void setUpAndQueueBuffer(const sp<IGraphicBufferProducer>& igbProducer,
643 nsecs_t* requestedPresentTime, nsecs_t* postedTime,
644 IGraphicBufferProducer::QueueBufferOutput* qbOutput,
645 bool getFrameTimestamps) {
646 int slot;
647 sp<Fence> fence;
648 sp<GraphicBuffer> buf;
649 auto ret = igbProducer->dequeueBuffer(&slot, &fence, mDisplayWidth, mDisplayHeight,
650 PIXEL_FORMAT_RGBA_8888, GRALLOC_USAGE_SW_WRITE_OFTEN,
651 nullptr, nullptr);
652 ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, ret);
653 ASSERT_EQ(OK, igbProducer->requestBuffer(slot, &buf));
654
655 nsecs_t requestedTime = systemTime();
656 if (requestedPresentTime) *requestedPresentTime = requestedTime;
657 IGraphicBufferProducer::QueueBufferInput input(requestedTime, false, HAL_DATASPACE_UNKNOWN,
658 Rect(mDisplayWidth, mDisplayHeight),
659 NATIVE_WINDOW_SCALING_MODE_FREEZE, 0,
660 Fence::NO_FENCE, /*sticky*/ 0,
661 getFrameTimestamps);
662 if (postedTime) *postedTime = systemTime();
663 igbProducer->queueBuffer(slot, input, qbOutput);
664 }
665};
666
667TEST_F(BLASTFrameEventHistoryTest, FrameEventHistory_Basic) {
668 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
669 sp<IGraphicBufferProducer> igbProducer;
670 ProducerFrameEventHistory history;
671 setUpProducer(adapter, igbProducer);
672
673 IGraphicBufferProducer::QueueBufferOutput qbOutput;
674 nsecs_t requestedPresentTimeA = 0;
675 nsecs_t postedTimeA = 0;
676 setUpAndQueueBuffer(igbProducer, &requestedPresentTimeA, &postedTimeA, &qbOutput, true);
677 history.applyDelta(qbOutput.frameTimestamps);
678
679 FrameEvents* events = nullptr;
680 events = history.getFrame(1);
681 ASSERT_NE(nullptr, events);
682 ASSERT_EQ(1, events->frameNumber);
683 ASSERT_EQ(requestedPresentTimeA, events->requestedPresentTime);
684 ASSERT_GE(events->postedTime, postedTimeA);
685
686 adapter.waitForCallbacks();
687
688 // queue another buffer so we query for frame event deltas
689 nsecs_t requestedPresentTimeB = 0;
690 nsecs_t postedTimeB = 0;
691 setUpAndQueueBuffer(igbProducer, &requestedPresentTimeB, &postedTimeB, &qbOutput, true);
692 history.applyDelta(qbOutput.frameTimestamps);
693 events = history.getFrame(1);
694 ASSERT_NE(nullptr, events);
695
696 // frame number, requestedPresentTime, and postTime should not have changed
697 ASSERT_EQ(1, events->frameNumber);
698 ASSERT_EQ(requestedPresentTimeA, events->requestedPresentTime);
699 ASSERT_GE(events->postedTime, postedTimeA);
700
701 ASSERT_GE(events->latchTime, postedTimeA);
702 ASSERT_GE(events->dequeueReadyTime, events->latchTime);
703 ASSERT_NE(nullptr, events->gpuCompositionDoneFence);
704 ASSERT_NE(nullptr, events->displayPresentFence);
705 ASSERT_NE(nullptr, events->releaseFence);
706
707 // we should also have gotten the initial values for the next frame
708 events = history.getFrame(2);
709 ASSERT_NE(nullptr, events);
710 ASSERT_EQ(2, events->frameNumber);
711 ASSERT_EQ(requestedPresentTimeB, events->requestedPresentTime);
712 ASSERT_GE(events->postedTime, postedTimeB);
Valerie Hau78491e92020-04-15 13:10:56 -0700713
714 // wait for any callbacks that have not been received
715 adapter.waitForCallbacks();
Valerie Hau871d6352020-01-29 08:44:02 -0800716}
Valerie Hauc5011f92019-10-11 09:52:07 -0700717} // namespace android