blob: 194757fe6bc521022803e5554c95faf0d320c9b3 [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>
Vishnu Nair17dde612020-12-28 11:39:59 -080027#include <gui/Surface.h>
Valerie Hauc5011f92019-10-11 09:52:07 -070028#include <gui/SurfaceComposerClient.h>
chaviwe7b9f272020-08-18 16:08:59 -070029#include <gui/SyncScreenCaptureListener.h>
chaviwd7deef72021-10-06 11:53:40 -050030#include <gui/test/CallbackUtils.h>
Valerie Hauda3446e2019-10-14 15:49:22 -070031#include <private/gui/ComposerService.h>
Marin Shalamanova7fe3042021-01-29 21:02:08 +010032#include <ui/DisplayMode.h>
Valerie Hauc5011f92019-10-11 09:52:07 -070033#include <ui/GraphicBuffer.h>
Valerie Hauda3446e2019-10-14 15:49:22 -070034#include <ui/GraphicTypes.h>
Valerie Hau8cee3f92019-11-06 10:06:28 -080035#include <ui/Transform.h>
Valerie Hauc5011f92019-10-11 09:52:07 -070036
37#include <gtest/gtest.h>
38
39using namespace std::chrono_literals;
40
41namespace android {
42
Valerie Hauc5011f92019-10-11 09:52:07 -070043using Transaction = SurfaceComposerClient::Transaction;
Valerie Hauda3446e2019-10-14 15:49:22 -070044using android::hardware::graphics::common::V1_2::BufferUsage;
Valerie Hauc5011f92019-10-11 09:52:07 -070045
chaviwd7deef72021-10-06 11:53:40 -050046class CountProducerListener : public BnProducerListener {
47public:
48 void onBufferReleased() override {
49 std::scoped_lock<std::mutex> lock(mMutex);
50 mNumReleased++;
51 mReleaseCallback.notify_one();
52 }
53
54 void waitOnNumberReleased(int32_t expectedNumReleased) {
55 std::unique_lock<std::mutex> lock(mMutex);
56 while (mNumReleased < expectedNumReleased) {
57 ASSERT_NE(mReleaseCallback.wait_for(lock, std::chrono::seconds(3)),
58 std::cv_status::timeout)
59 << "did not receive release";
60 }
61 }
62
63private:
64 std::mutex mMutex;
65 std::condition_variable mReleaseCallback;
66 int32_t mNumReleased GUARDED_BY(mMutex) = 0;
67};
68
chaviwf10b9042021-10-13 15:48:59 -050069class TestBLASTBufferQueue : public BLASTBufferQueue {
70public:
71 TestBLASTBufferQueue(const std::string& name, const sp<SurfaceControl>& surface, int width,
72 int height, int32_t format)
73 : BLASTBufferQueue(name, surface, width, height, format) {}
74
chaviw6b9ffea2021-11-08 09:25:48 -060075 void transactionCallback(nsecs_t latchTime, const sp<Fence>& presentFence,
76 const std::vector<SurfaceControlStats>& stats) override {
77 BLASTBufferQueue::transactionCallback(latchTime, presentFence, stats);
chaviwf10b9042021-10-13 15:48:59 -050078 uint64_t frameNumber = stats[0].frameEventStats.frameNumber;
79
80 {
81 std::unique_lock lock{frameNumberMutex};
chaviw6b9ffea2021-11-08 09:25:48 -060082 mLastTransactionFrameNumber = frameNumber;
83 mWaitForCallbackCV.notify_all();
chaviwf10b9042021-10-13 15:48:59 -050084 }
85 }
86
87 void waitForCallback(int64_t frameNumber) {
88 std::unique_lock lock{frameNumberMutex};
89 // Wait until all but one of the submitted buffers have been released.
chaviw6b9ffea2021-11-08 09:25:48 -060090 while (mLastTransactionFrameNumber < frameNumber) {
91 mWaitForCallbackCV.wait(lock);
chaviwf10b9042021-10-13 15:48:59 -050092 }
93 }
94
95private:
96 std::mutex frameNumberMutex;
chaviw6b9ffea2021-11-08 09:25:48 -060097 std::condition_variable mWaitForCallbackCV;
98 int64_t mLastTransactionFrameNumber = -1;
chaviwf10b9042021-10-13 15:48:59 -050099};
100
Valerie Hauc5011f92019-10-11 09:52:07 -0700101class BLASTBufferQueueHelper {
102public:
103 BLASTBufferQueueHelper(const sp<SurfaceControl>& sc, int width, int height) {
chaviwf10b9042021-10-13 15:48:59 -0500104 mBlastBufferQueueAdapter = new TestBLASTBufferQueue("TestBLASTBufferQueue", sc, width,
105 height, PIXEL_FORMAT_RGBA_8888);
Valerie Hauc5011f92019-10-11 09:52:07 -0700106 }
107
108 void update(const sp<SurfaceControl>& sc, int width, int height) {
chaviw565ee542021-01-14 10:21:23 -0800109 mBlastBufferQueueAdapter->update(sc, width, height, PIXEL_FORMAT_RGBA_8888);
Valerie Hauc5011f92019-10-11 09:52:07 -0700110 }
111
chaviwa1c4c822021-11-10 18:11:58 -0600112 void setSyncTransaction(Transaction* sync) {
113 mBlastBufferQueueAdapter->setSyncTransaction(sync);
Valerie Hauc5011f92019-10-11 09:52:07 -0700114 }
115
Vishnu Nairea0de002020-11-17 17:42:37 -0800116 int getWidth() { return mBlastBufferQueueAdapter->mSize.width; }
Valerie Hauda3446e2019-10-14 15:49:22 -0700117
Vishnu Nairea0de002020-11-17 17:42:37 -0800118 int getHeight() { return mBlastBufferQueueAdapter->mSize.height; }
Valerie Hauda3446e2019-10-14 15:49:22 -0700119
chaviwa1c4c822021-11-10 18:11:58 -0600120 Transaction* getSyncTransaction() { return mBlastBufferQueueAdapter->mSyncTransaction; }
Valerie Hauda3446e2019-10-14 15:49:22 -0700121
122 sp<IGraphicBufferProducer> getIGraphicBufferProducer() {
123 return mBlastBufferQueueAdapter->getIGraphicBufferProducer();
124 }
125
Valerie Hauc5011f92019-10-11 09:52:07 -0700126 const sp<SurfaceControl> getSurfaceControl() {
127 return mBlastBufferQueueAdapter->mSurfaceControl;
128 }
129
Vishnu Naira4fbca52021-07-07 16:52:34 -0700130 sp<Surface> getSurface() {
131 return mBlastBufferQueueAdapter->getSurface(false /* includeSurfaceControlHandle */);
132 }
133
Valerie Haud3b90d22019-11-06 09:37:31 -0800134 void waitForCallbacks() {
Valerie Hauda3446e2019-10-14 15:49:22 -0700135 std::unique_lock lock{mBlastBufferQueueAdapter->mMutex};
Vishnu Nair1506b182021-02-22 14:35:15 -0800136 // Wait until all but one of the submitted buffers have been released.
137 while (mBlastBufferQueueAdapter->mSubmitted.size() > 1) {
Valerie Haud3b90d22019-11-06 09:37:31 -0800138 mBlastBufferQueueAdapter->mCallbackCV.wait(lock);
139 }
Valerie Hauda3446e2019-10-14 15:49:22 -0700140 }
141
Vishnu Nair1506b182021-02-22 14:35:15 -0800142 void waitForCallback(int64_t frameNumber) {
chaviwf10b9042021-10-13 15:48:59 -0500143 mBlastBufferQueueAdapter->waitForCallback(frameNumber);
Vishnu Nair1506b182021-02-22 14:35:15 -0800144 }
145
Valerie Hauc5011f92019-10-11 09:52:07 -0700146private:
chaviwf10b9042021-10-13 15:48:59 -0500147 sp<TestBLASTBufferQueue> mBlastBufferQueueAdapter;
Valerie Hauc5011f92019-10-11 09:52:07 -0700148};
149
150class BLASTBufferQueueTest : public ::testing::Test {
151public:
152protected:
153 BLASTBufferQueueTest() {
154 const ::testing::TestInfo* const testInfo =
155 ::testing::UnitTest::GetInstance()->current_test_info();
156 ALOGV("Begin test: %s.%s", testInfo->test_case_name(), testInfo->name());
157 }
158
159 ~BLASTBufferQueueTest() {
160 const ::testing::TestInfo* const testInfo =
161 ::testing::UnitTest::GetInstance()->current_test_info();
162 ALOGV("End test: %s.%s", testInfo->test_case_name(), testInfo->name());
163 }
164
165 void SetUp() {
Valerie Hauda3446e2019-10-14 15:49:22 -0700166 mComposer = ComposerService::getComposerService();
Valerie Hauc5011f92019-10-11 09:52:07 -0700167 mClient = new SurfaceComposerClient();
Valerie Hauda3446e2019-10-14 15:49:22 -0700168 mDisplayToken = mClient->getInternalDisplayToken();
169 ASSERT_NE(nullptr, mDisplayToken.get());
170 Transaction t;
Dominik Laskowski29fa1462021-04-27 15:51:50 -0700171 t.setDisplayLayerStack(mDisplayToken, ui::DEFAULT_LAYER_STACK);
Valerie Hauda3446e2019-10-14 15:49:22 -0700172 t.apply();
173 t.clear();
174
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100175 ui::DisplayMode mode;
176 ASSERT_EQ(NO_ERROR, SurfaceComposerClient::getActiveDisplayMode(mDisplayToken, &mode));
177 const ui::Size& resolution = mode.resolution;
Dominik Laskowski3cb3d4e2019-11-21 11:14:45 -0800178 mDisplayWidth = resolution.getWidth();
179 mDisplayHeight = resolution.getHeight();
Valerie Hauda3446e2019-10-14 15:49:22 -0700180
181 mSurfaceControl = mClient->createSurface(String8("TestSurface"), mDisplayWidth,
182 mDisplayHeight, PIXEL_FORMAT_RGBA_8888,
183 ISurfaceComposerClient::eFXSurfaceBufferState,
184 /*parent*/ nullptr);
Dominik Laskowski29fa1462021-04-27 15:51:50 -0700185 t.setLayerStack(mSurfaceControl, ui::DEFAULT_LAYER_STACK)
Valerie Hauda3446e2019-10-14 15:49:22 -0700186 .setLayer(mSurfaceControl, std::numeric_limits<int32_t>::max())
Valerie Hauda3446e2019-10-14 15:49:22 -0700187 .show(mSurfaceControl)
188 .setDataspace(mSurfaceControl, ui::Dataspace::V0_SRGB)
189 .apply();
chaviwd2432892020-07-24 17:42:39 -0700190
191 mCaptureArgs.displayToken = mDisplayToken;
arthurhung6fa58b72020-11-05 11:56:00 +0800192 mCaptureArgs.dataspace = ui::Dataspace::V0_SRGB;
Valerie Hauda3446e2019-10-14 15:49:22 -0700193 }
194
chaviwd7deef72021-10-06 11:53:40 -0500195 void setUpProducer(BLASTBufferQueueHelper& adapter, sp<IGraphicBufferProducer>& producer,
196 int32_t maxBufferCount = 2) {
Vishnu Nair083efd32021-02-12 09:32:30 -0800197 producer = adapter.getIGraphicBufferProducer();
chaviwd7deef72021-10-06 11:53:40 -0500198 setUpProducer(producer, maxBufferCount);
Vishnu Nair083efd32021-02-12 09:32:30 -0800199 }
200
chaviwd7deef72021-10-06 11:53:40 -0500201 void setUpProducer(sp<IGraphicBufferProducer>& igbProducer, int32_t maxBufferCount) {
Valerie Haud3b90d22019-11-06 09:37:31 -0800202 ASSERT_NE(nullptr, igbProducer.get());
chaviwd7deef72021-10-06 11:53:40 -0500203 ASSERT_EQ(NO_ERROR, igbProducer->setMaxDequeuedBufferCount(maxBufferCount));
Valerie Haud3b90d22019-11-06 09:37:31 -0800204 IGraphicBufferProducer::QueueBufferOutput qbOutput;
chaviwd7deef72021-10-06 11:53:40 -0500205 mProducerListener = new CountProducerListener();
Valerie Haud3b90d22019-11-06 09:37:31 -0800206 ASSERT_EQ(NO_ERROR,
chaviwd7deef72021-10-06 11:53:40 -0500207 igbProducer->connect(mProducerListener, NATIVE_WINDOW_API_CPU, false, &qbOutput));
Dominik Laskowski718f9602019-11-09 20:01:35 -0800208 ASSERT_NE(ui::Transform::ROT_INVALID, qbOutput.transformHint);
Valerie Haud3b90d22019-11-06 09:37:31 -0800209 }
210
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800211 void fillBuffer(uint32_t* bufData, Rect rect, uint32_t stride, uint8_t r, uint8_t g,
212 uint8_t b) {
213 for (uint32_t row = rect.top; row < rect.bottom; row++) {
214 for (uint32_t col = rect.left; col < rect.right; col++) {
Valerie Hauda3446e2019-10-14 15:49:22 -0700215 uint8_t* pixel = (uint8_t*)(bufData + (row * stride) + col);
216 *pixel = r;
217 *(pixel + 1) = g;
218 *(pixel + 2) = b;
219 *(pixel + 3) = 255;
220 }
221 }
222 }
223
Valerie Hau5977fc82019-12-05 15:56:39 -0800224 void fillQuadrants(sp<GraphicBuffer>& buf) {
225 const auto bufWidth = buf->getWidth();
226 const auto bufHeight = buf->getHeight();
227 uint32_t* bufData;
228 buf->lock(static_cast<uint32_t>(GraphicBuffer::USAGE_SW_WRITE_OFTEN),
229 reinterpret_cast<void**>(&bufData));
230 fillBuffer(bufData, Rect(0, 0, bufWidth / 2, bufHeight / 2), buf->getStride(), 0, 0, 0);
231 fillBuffer(bufData, Rect(bufWidth / 2, 0, bufWidth, bufHeight / 2), buf->getStride(), 255,
232 0, 0);
233 fillBuffer(bufData, Rect(bufWidth / 2, bufHeight / 2, bufWidth, bufHeight),
234 buf->getStride(), 0, 255, 0);
235 fillBuffer(bufData, Rect(0, bufHeight / 2, bufWidth / 2, bufHeight), buf->getStride(), 0, 0,
236 255);
237 buf->unlock();
238 }
239
240 void checkScreenCapture(uint8_t r, uint8_t g, uint8_t b, Rect region, int32_t border = 0,
241 bool outsideRegion = false) {
chaviwd2432892020-07-24 17:42:39 -0700242 sp<GraphicBuffer>& captureBuf = mCaptureResults.buffer;
Valerie Hau5977fc82019-12-05 15:56:39 -0800243 const auto epsilon = 3;
chaviwd2432892020-07-24 17:42:39 -0700244 const auto width = captureBuf->getWidth();
245 const auto height = captureBuf->getHeight();
246 const auto stride = captureBuf->getStride();
Valerie Hauda3446e2019-10-14 15:49:22 -0700247
248 uint32_t* bufData;
chaviwd2432892020-07-24 17:42:39 -0700249 captureBuf->lock(static_cast<uint32_t>(GraphicBuffer::USAGE_SW_READ_OFTEN),
250 reinterpret_cast<void**>(&bufData));
Valerie Hauda3446e2019-10-14 15:49:22 -0700251
252 for (uint32_t row = 0; row < height; row++) {
253 for (uint32_t col = 0; col < width; col++) {
254 uint8_t* pixel = (uint8_t*)(bufData + (row * stride) + col);
arthurhung6fa58b72020-11-05 11:56:00 +0800255 ASSERT_NE(nullptr, pixel);
Valerie Hau5977fc82019-12-05 15:56:39 -0800256 bool inRegion;
257 if (!outsideRegion) {
258 inRegion = row >= region.top + border && row < region.bottom - border &&
259 col >= region.left + border && col < region.right - border;
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800260 } else {
Valerie Hau5977fc82019-12-05 15:56:39 -0800261 inRegion = row >= region.top - border && row < region.bottom + border &&
262 col >= region.left - border && col < region.right + border;
263 }
264 if (!outsideRegion && inRegion) {
Chavi Weingartena5aedbd2021-04-09 13:37:33 +0000265 ASSERT_GE(epsilon, abs(r - *(pixel)));
266 ASSERT_GE(epsilon, abs(g - *(pixel + 1)));
267 ASSERT_GE(epsilon, abs(b - *(pixel + 2)));
Valerie Hau5977fc82019-12-05 15:56:39 -0800268 } else if (outsideRegion && !inRegion) {
Chavi Weingartena5aedbd2021-04-09 13:37:33 +0000269 ASSERT_GE(epsilon, abs(r - *(pixel)));
270 ASSERT_GE(epsilon, abs(g - *(pixel + 1)));
271 ASSERT_GE(epsilon, abs(b - *(pixel + 2)));
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800272 }
Vishnu Nair1506b182021-02-22 14:35:15 -0800273 ASSERT_EQ(false, ::testing::Test::HasFailure());
Valerie Hauda3446e2019-10-14 15:49:22 -0700274 }
275 }
chaviwd2432892020-07-24 17:42:39 -0700276 captureBuf->unlock();
Valerie Hauc5011f92019-10-11 09:52:07 -0700277 }
278
chaviw8ffc7b82020-08-18 11:25:37 -0700279 static status_t captureDisplay(DisplayCaptureArgs& captureArgs,
280 ScreenCaptureResults& captureResults) {
281 const auto sf = ComposerService::getComposerService();
282 SurfaceComposerClient::Transaction().apply(true);
283
284 const sp<SyncScreenCaptureListener> captureListener = new SyncScreenCaptureListener();
285 status_t status = sf->captureDisplay(captureArgs, captureListener);
286 if (status != NO_ERROR) {
287 return status;
288 }
289 captureResults = captureListener->waitForResults();
290 return captureResults.result;
291 }
292
Vishnu Nair277142c2021-01-05 18:35:29 -0800293 void queueBuffer(sp<IGraphicBufferProducer> igbp, uint8_t r, uint8_t g, uint8_t b,
294 nsecs_t presentTimeDelay) {
295 int slot;
296 sp<Fence> fence;
297 sp<GraphicBuffer> buf;
298 auto ret = igbp->dequeueBuffer(&slot, &fence, mDisplayWidth, mDisplayHeight,
299 PIXEL_FORMAT_RGBA_8888, GRALLOC_USAGE_SW_WRITE_OFTEN,
300 nullptr, nullptr);
301 ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, ret);
302 ASSERT_EQ(OK, igbp->requestBuffer(slot, &buf));
303
304 uint32_t* bufData;
305 buf->lock(static_cast<uint32_t>(GraphicBuffer::USAGE_SW_WRITE_OFTEN),
306 reinterpret_cast<void**>(&bufData));
307 fillBuffer(bufData, Rect(buf->getWidth(), buf->getHeight() / 2), buf->getStride(), r, g, b);
308 buf->unlock();
309
310 IGraphicBufferProducer::QueueBufferOutput qbOutput;
311 nsecs_t timestampNanos = systemTime() + presentTimeDelay;
312 IGraphicBufferProducer::QueueBufferInput input(timestampNanos, false, HAL_DATASPACE_UNKNOWN,
313 Rect(mDisplayWidth, mDisplayHeight / 2),
314 NATIVE_WINDOW_SCALING_MODE_FREEZE, 0,
315 Fence::NO_FENCE);
316 igbp->queueBuffer(slot, input, &qbOutput);
317 }
318
Valerie Hauc5011f92019-10-11 09:52:07 -0700319 sp<SurfaceComposerClient> mClient;
Valerie Hauda3446e2019-10-14 15:49:22 -0700320 sp<ISurfaceComposer> mComposer;
321
322 sp<IBinder> mDisplayToken;
323
Valerie Hauc5011f92019-10-11 09:52:07 -0700324 sp<SurfaceControl> mSurfaceControl;
Valerie Hauda3446e2019-10-14 15:49:22 -0700325
326 uint32_t mDisplayWidth;
327 uint32_t mDisplayHeight;
chaviwd2432892020-07-24 17:42:39 -0700328
329 DisplayCaptureArgs mCaptureArgs;
330 ScreenCaptureResults mCaptureResults;
chaviwd7deef72021-10-06 11:53:40 -0500331 sp<CountProducerListener> mProducerListener;
Valerie Hauc5011f92019-10-11 09:52:07 -0700332};
333
334TEST_F(BLASTBufferQueueTest, CreateBLASTBufferQueue) {
335 // create BLASTBufferQueue adapter associated with this surface
Valerie Hauda3446e2019-10-14 15:49:22 -0700336 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
Valerie Hauc5011f92019-10-11 09:52:07 -0700337 ASSERT_EQ(mSurfaceControl, adapter.getSurfaceControl());
Valerie Hauda3446e2019-10-14 15:49:22 -0700338 ASSERT_EQ(mDisplayWidth, adapter.getWidth());
339 ASSERT_EQ(mDisplayHeight, adapter.getHeight());
chaviwa1c4c822021-11-10 18:11:58 -0600340 ASSERT_EQ(nullptr, adapter.getSyncTransaction());
Valerie Hauc5011f92019-10-11 09:52:07 -0700341}
342
343TEST_F(BLASTBufferQueueTest, Update) {
Valerie Hauda3446e2019-10-14 15:49:22 -0700344 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
Valerie Hauc5011f92019-10-11 09:52:07 -0700345 sp<SurfaceControl> updateSurface =
Valerie Hauda3446e2019-10-14 15:49:22 -0700346 mClient->createSurface(String8("UpdateTest"), mDisplayWidth / 2, mDisplayHeight / 2,
347 PIXEL_FORMAT_RGBA_8888);
348 adapter.update(updateSurface, mDisplayWidth / 2, mDisplayHeight / 2);
Valerie Hauc5011f92019-10-11 09:52:07 -0700349 ASSERT_EQ(updateSurface, adapter.getSurfaceControl());
Vishnu Nairea0de002020-11-17 17:42:37 -0800350 sp<IGraphicBufferProducer> igbProducer;
351 setUpProducer(adapter, igbProducer);
352
353 int32_t width;
354 igbProducer->query(NATIVE_WINDOW_WIDTH, &width);
355 ASSERT_EQ(mDisplayWidth / 2, width);
356 int32_t height;
357 igbProducer->query(NATIVE_WINDOW_HEIGHT, &height);
358 ASSERT_EQ(mDisplayHeight / 2, height);
Valerie Hauc5011f92019-10-11 09:52:07 -0700359}
360
chaviwa1c4c822021-11-10 18:11:58 -0600361TEST_F(BLASTBufferQueueTest, SetSyncTransaction) {
Valerie Hauda3446e2019-10-14 15:49:22 -0700362 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
chaviwa1c4c822021-11-10 18:11:58 -0600363 Transaction sync;
364 adapter.setSyncTransaction(&sync);
365 ASSERT_EQ(&sync, adapter.getSyncTransaction());
Valerie Hauc5011f92019-10-11 09:52:07 -0700366}
Valerie Hauda3446e2019-10-14 15:49:22 -0700367
Valerie Haubf29e042020-02-06 11:40:38 -0800368TEST_F(BLASTBufferQueueTest, DISABLED_onFrameAvailable_ApplyDesiredPresentTime) {
Valerie Hau181abd32020-01-27 14:18:28 -0800369 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
370 sp<IGraphicBufferProducer> igbProducer;
371 setUpProducer(adapter, igbProducer);
372
373 int slot;
374 sp<Fence> fence;
375 sp<GraphicBuffer> buf;
376 auto ret = igbProducer->dequeueBuffer(&slot, &fence, mDisplayWidth, mDisplayHeight,
377 PIXEL_FORMAT_RGBA_8888, GRALLOC_USAGE_SW_WRITE_OFTEN,
378 nullptr, nullptr);
379 ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, ret);
380 ASSERT_EQ(OK, igbProducer->requestBuffer(slot, &buf));
381
382 nsecs_t desiredPresentTime = systemTime() + nsecs_t(5 * 1e8);
383 IGraphicBufferProducer::QueueBufferOutput qbOutput;
Vishnu Nair1506b182021-02-22 14:35:15 -0800384 IGraphicBufferProducer::QueueBufferInput input(desiredPresentTime, true /* autotimestamp */,
385 HAL_DATASPACE_UNKNOWN,
Valerie Hau181abd32020-01-27 14:18:28 -0800386 Rect(mDisplayWidth, mDisplayHeight),
387 NATIVE_WINDOW_SCALING_MODE_FREEZE, 0,
388 Fence::NO_FENCE);
389 igbProducer->queueBuffer(slot, input, &qbOutput);
390 ASSERT_NE(ui::Transform::ROT_INVALID, qbOutput.transformHint);
391
392 adapter.waitForCallbacks();
393 ASSERT_GE(systemTime(), desiredPresentTime);
394}
395
Valerie Hauda3446e2019-10-14 15:49:22 -0700396TEST_F(BLASTBufferQueueTest, onFrameAvailable_Apply) {
397 uint8_t r = 255;
398 uint8_t g = 0;
399 uint8_t b = 0;
400
401 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
Valerie Haud3b90d22019-11-06 09:37:31 -0800402 sp<IGraphicBufferProducer> igbProducer;
403 setUpProducer(adapter, igbProducer);
Valerie Hauda3446e2019-10-14 15:49:22 -0700404
405 int slot;
406 sp<Fence> fence;
407 sp<GraphicBuffer> buf;
408 auto ret = igbProducer->dequeueBuffer(&slot, &fence, mDisplayWidth, mDisplayHeight,
409 PIXEL_FORMAT_RGBA_8888, GRALLOC_USAGE_SW_WRITE_OFTEN,
410 nullptr, nullptr);
411 ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, ret);
412 ASSERT_EQ(OK, igbProducer->requestBuffer(slot, &buf));
413
414 uint32_t* bufData;
415 buf->lock(static_cast<uint32_t>(GraphicBuffer::USAGE_SW_WRITE_OFTEN),
416 reinterpret_cast<void**>(&bufData));
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800417 fillBuffer(bufData, Rect(buf->getWidth(), buf->getHeight()), buf->getStride(), r, g, b);
Valerie Hauda3446e2019-10-14 15:49:22 -0700418 buf->unlock();
419
Valerie Haud3b90d22019-11-06 09:37:31 -0800420 IGraphicBufferProducer::QueueBufferOutput qbOutput;
Vishnu Nair1506b182021-02-22 14:35:15 -0800421 IGraphicBufferProducer::QueueBufferInput input(systemTime(), true /* autotimestamp */,
422 HAL_DATASPACE_UNKNOWN,
Valerie Hauda3446e2019-10-14 15:49:22 -0700423 Rect(mDisplayWidth, mDisplayHeight),
424 NATIVE_WINDOW_SCALING_MODE_FREEZE, 0,
425 Fence::NO_FENCE);
426 igbProducer->queueBuffer(slot, input, &qbOutput);
Dominik Laskowski718f9602019-11-09 20:01:35 -0800427 ASSERT_NE(ui::Transform::ROT_INVALID, qbOutput.transformHint);
Valerie Hauda3446e2019-10-14 15:49:22 -0700428
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800429 adapter.waitForCallbacks();
Valerie Hauda3446e2019-10-14 15:49:22 -0700430
431 // capture screen and verify that it is red
chaviw8ffc7b82020-08-18 11:25:37 -0700432 ASSERT_EQ(NO_ERROR, captureDisplay(mCaptureArgs, mCaptureResults));
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800433 ASSERT_NO_FATAL_FAILURE(
434 checkScreenCapture(r, g, b, {0, 0, (int32_t)mDisplayWidth, (int32_t)mDisplayHeight}));
Valerie Hauda3446e2019-10-14 15:49:22 -0700435}
Valerie Haud3b90d22019-11-06 09:37:31 -0800436
437TEST_F(BLASTBufferQueueTest, TripleBuffering) {
438 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
439 sp<IGraphicBufferProducer> igbProducer;
440 setUpProducer(adapter, igbProducer);
441
442 std::vector<std::pair<int, sp<Fence>>> allocated;
Ady Abraham0bde6b52021-05-18 13:57:02 -0700443 int minUndequeuedBuffers = 0;
444 ASSERT_EQ(OK, igbProducer->query(NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS, &minUndequeuedBuffers));
445 const auto bufferCount = minUndequeuedBuffers + 2;
446
447 for (int i = 0; i < bufferCount; i++) {
Valerie Haud3b90d22019-11-06 09:37:31 -0800448 int slot;
449 sp<Fence> fence;
450 sp<GraphicBuffer> buf;
451 auto ret = igbProducer->dequeueBuffer(&slot, &fence, mDisplayWidth, mDisplayHeight,
452 PIXEL_FORMAT_RGBA_8888, GRALLOC_USAGE_SW_WRITE_OFTEN,
453 nullptr, nullptr);
454 ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, ret);
455 ASSERT_EQ(OK, igbProducer->requestBuffer(slot, &buf));
456 allocated.push_back({slot, fence});
457 }
458 for (int i = 0; i < allocated.size(); i++) {
459 igbProducer->cancelBuffer(allocated[i].first, allocated[i].second);
460 }
461
Valerie Haua32c5522019-12-09 10:11:08 -0800462 for (int i = 0; i < 100; i++) {
Valerie Haud3b90d22019-11-06 09:37:31 -0800463 int slot;
464 sp<Fence> fence;
465 sp<GraphicBuffer> buf;
466 auto ret = igbProducer->dequeueBuffer(&slot, &fence, mDisplayWidth, mDisplayHeight,
467 PIXEL_FORMAT_RGBA_8888, GRALLOC_USAGE_SW_WRITE_OFTEN,
468 nullptr, nullptr);
469 ASSERT_EQ(NO_ERROR, ret);
470 IGraphicBufferProducer::QueueBufferOutput qbOutput;
Vishnu Nair1506b182021-02-22 14:35:15 -0800471 IGraphicBufferProducer::QueueBufferInput input(systemTime(), true /* autotimestamp */,
472 HAL_DATASPACE_UNKNOWN,
Valerie Haud3b90d22019-11-06 09:37:31 -0800473 Rect(mDisplayWidth, mDisplayHeight),
474 NATIVE_WINDOW_SCALING_MODE_FREEZE, 0,
475 Fence::NO_FENCE);
476 igbProducer->queueBuffer(slot, input, &qbOutput);
477 }
478 adapter.waitForCallbacks();
479}
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800480
481TEST_F(BLASTBufferQueueTest, SetCrop_Item) {
482 uint8_t r = 255;
483 uint8_t g = 0;
484 uint8_t b = 0;
485
486 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
487 sp<IGraphicBufferProducer> igbProducer;
488 setUpProducer(adapter, igbProducer);
489 int slot;
490 sp<Fence> fence;
491 sp<GraphicBuffer> buf;
492 auto ret = igbProducer->dequeueBuffer(&slot, &fence, mDisplayWidth, mDisplayHeight,
493 PIXEL_FORMAT_RGBA_8888, GRALLOC_USAGE_SW_WRITE_OFTEN,
494 nullptr, nullptr);
495 ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, ret);
496 ASSERT_EQ(OK, igbProducer->requestBuffer(slot, &buf));
497
498 uint32_t* bufData;
499 buf->lock(static_cast<uint32_t>(GraphicBuffer::USAGE_SW_WRITE_OFTEN),
500 reinterpret_cast<void**>(&bufData));
501 fillBuffer(bufData, Rect(buf->getWidth(), buf->getHeight() / 2), buf->getStride(), r, g, b);
502 buf->unlock();
503
504 IGraphicBufferProducer::QueueBufferOutput qbOutput;
Vishnu Nair1506b182021-02-22 14:35:15 -0800505 IGraphicBufferProducer::QueueBufferInput input(systemTime(), true /* autotimestamp */,
506 HAL_DATASPACE_UNKNOWN,
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800507 Rect(mDisplayWidth, mDisplayHeight / 2),
508 NATIVE_WINDOW_SCALING_MODE_FREEZE, 0,
509 Fence::NO_FENCE);
510 igbProducer->queueBuffer(slot, input, &qbOutput);
Dominik Laskowski718f9602019-11-09 20:01:35 -0800511 ASSERT_NE(ui::Transform::ROT_INVALID, qbOutput.transformHint);
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800512
513 adapter.waitForCallbacks();
514 // capture screen and verify that it is red
chaviw8ffc7b82020-08-18 11:25:37 -0700515 ASSERT_EQ(NO_ERROR, captureDisplay(mCaptureArgs, mCaptureResults));
chaviwd2432892020-07-24 17:42:39 -0700516
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800517 ASSERT_NO_FATAL_FAILURE(
Chavi Weingartena5aedbd2021-04-09 13:37:33 +0000518 checkScreenCapture(r, g, b,
519 {0, 0, (int32_t)mDisplayWidth, (int32_t)mDisplayHeight / 2}));
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800520}
521
522TEST_F(BLASTBufferQueueTest, SetCrop_ScalingModeScaleCrop) {
523 uint8_t r = 255;
524 uint8_t g = 0;
525 uint8_t b = 0;
526
527 int32_t bufferSideLength =
528 (mDisplayWidth < mDisplayHeight) ? mDisplayWidth / 2 : mDisplayHeight / 2;
529 int32_t finalCropSideLength = bufferSideLength / 2;
530
531 auto bg = mClient->createSurface(String8("BGTest"), 0, 0, PIXEL_FORMAT_RGBA_8888,
Vishnu Nairfa247b12020-02-11 08:58:26 -0800532 ISurfaceComposerClient::eFXSurfaceEffect);
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800533 ASSERT_NE(nullptr, bg.get());
534 Transaction t;
Dominik Laskowski29fa1462021-04-27 15:51:50 -0700535 t.setLayerStack(bg, ui::DEFAULT_LAYER_STACK)
chaviw25714502021-02-11 10:01:08 -0800536 .setCrop(bg, Rect(0, 0, mDisplayWidth, mDisplayHeight))
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800537 .setColor(bg, half3{0, 0, 0})
538 .setLayer(bg, 0)
539 .apply();
540
541 BLASTBufferQueueHelper adapter(mSurfaceControl, bufferSideLength, bufferSideLength);
542 sp<IGraphicBufferProducer> igbProducer;
543 setUpProducer(adapter, igbProducer);
544 int slot;
545 sp<Fence> fence;
546 sp<GraphicBuffer> buf;
547 auto ret = igbProducer->dequeueBuffer(&slot, &fence, bufferSideLength, bufferSideLength,
548 PIXEL_FORMAT_RGBA_8888, GRALLOC_USAGE_SW_WRITE_OFTEN,
549 nullptr, nullptr);
550 ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, ret);
551 ASSERT_EQ(OK, igbProducer->requestBuffer(slot, &buf));
552
553 uint32_t* bufData;
554 buf->lock(static_cast<uint32_t>(GraphicBuffer::USAGE_SW_WRITE_OFTEN),
555 reinterpret_cast<void**>(&bufData));
556 fillBuffer(bufData, Rect(buf->getWidth(), buf->getHeight()), buf->getStride(), 0, 0, 0);
557 fillBuffer(bufData,
558 Rect(finalCropSideLength / 2, 0, buf->getWidth() - finalCropSideLength / 2,
559 buf->getHeight()),
560 buf->getStride(), r, g, b);
561 buf->unlock();
562
563 IGraphicBufferProducer::QueueBufferOutput qbOutput;
Vishnu Nair1506b182021-02-22 14:35:15 -0800564 IGraphicBufferProducer::QueueBufferInput input(systemTime(), true /* autotimestamp */,
565 HAL_DATASPACE_UNKNOWN,
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800566 Rect(bufferSideLength, finalCropSideLength),
567 NATIVE_WINDOW_SCALING_MODE_SCALE_CROP, 0,
568 Fence::NO_FENCE);
569 igbProducer->queueBuffer(slot, input, &qbOutput);
Dominik Laskowski718f9602019-11-09 20:01:35 -0800570 ASSERT_NE(ui::Transform::ROT_INVALID, qbOutput.transformHint);
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800571
572 adapter.waitForCallbacks();
573 // capture screen and verify that it is red
chaviw8ffc7b82020-08-18 11:25:37 -0700574 ASSERT_EQ(NO_ERROR, captureDisplay(mCaptureArgs, mCaptureResults));
Vishnu Nair5cc9ac02021-04-19 13:23:38 -0700575 ASSERT_NO_FATAL_FAILURE(checkScreenCapture(r, g, b,
576 {10, 10, (int32_t)bufferSideLength - 10,
577 (int32_t)bufferSideLength - 10}));
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800578 ASSERT_NO_FATAL_FAILURE(
Vishnu Nair5cc9ac02021-04-19 13:23:38 -0700579 checkScreenCapture(0, 0, 0,
580 {0, 0, (int32_t)bufferSideLength, (int32_t)bufferSideLength},
581 /*border*/ 0, /*outsideRegion*/ true));
582}
583
584TEST_F(BLASTBufferQueueTest, ScaleCroppedBufferToBufferSize) {
585 // add black background
586 auto bg = mClient->createSurface(String8("BGTest"), 0, 0, PIXEL_FORMAT_RGBA_8888,
587 ISurfaceComposerClient::eFXSurfaceEffect);
588 ASSERT_NE(nullptr, bg.get());
589 Transaction t;
Dominik Laskowski29fa1462021-04-27 15:51:50 -0700590 t.setLayerStack(bg, ui::DEFAULT_LAYER_STACK)
Vishnu Nair5cc9ac02021-04-19 13:23:38 -0700591 .setCrop(bg, Rect(0, 0, mDisplayWidth, mDisplayHeight))
592 .setColor(bg, half3{0, 0, 0})
593 .setLayer(bg, 0)
594 .apply();
595
596 Rect windowSize(1000, 1000);
597 Rect bufferSize(windowSize);
598 Rect bufferCrop(200, 200, 700, 700);
599
600 BLASTBufferQueueHelper adapter(mSurfaceControl, windowSize.getWidth(), windowSize.getHeight());
601 sp<IGraphicBufferProducer> igbProducer;
602 setUpProducer(adapter, igbProducer);
603 int slot;
604 sp<Fence> fence;
605 sp<GraphicBuffer> buf;
606 auto ret = igbProducer->dequeueBuffer(&slot, &fence, bufferSize.getWidth(),
607 bufferSize.getHeight(), PIXEL_FORMAT_RGBA_8888,
608 GRALLOC_USAGE_SW_WRITE_OFTEN, nullptr, nullptr);
609 ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, ret);
610 ASSERT_EQ(OK, igbProducer->requestBuffer(slot, &buf));
611
612 uint32_t* bufData;
613 buf->lock(static_cast<uint32_t>(GraphicBuffer::USAGE_SW_WRITE_OFTEN),
614 reinterpret_cast<void**>(&bufData));
615 // fill buffer with grey
616 fillBuffer(bufData, bufferSize, buf->getStride(), 127, 127, 127);
617
618 // fill crop area with different colors so we can verify the cropped region has been scaled
619 // correctly.
620 fillBuffer(bufData, Rect(200, 200, 450, 450), buf->getStride(), /* rgb */ 255, 0, 0);
621 fillBuffer(bufData, Rect(200, 451, 450, 700), buf->getStride(), /* rgb */ 0, 255, 0);
622 fillBuffer(bufData, Rect(451, 200, 700, 450), buf->getStride(), /* rgb */ 0, 0, 255);
623 fillBuffer(bufData, Rect(451, 451, 700, 700), buf->getStride(), /* rgb */ 255, 0, 0);
624 buf->unlock();
625
626 IGraphicBufferProducer::QueueBufferOutput qbOutput;
627 IGraphicBufferProducer::QueueBufferInput input(systemTime(), true /* autotimestamp */,
628 HAL_DATASPACE_UNKNOWN,
629 bufferCrop /* Rect::INVALID_RECT */,
630 NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW, 0,
631 Fence::NO_FENCE);
632 igbProducer->queueBuffer(slot, input, &qbOutput);
633 ASSERT_NE(ui::Transform::ROT_INVALID, qbOutput.transformHint);
634
635 adapter.waitForCallbacks();
636
637 ASSERT_EQ(NO_ERROR, captureDisplay(mCaptureArgs, mCaptureResults));
638
639 // Verify cropped region is scaled correctly.
640 ASSERT_NO_FATAL_FAILURE(checkScreenCapture(255, 0, 0, {10, 10, 490, 490}));
641 ASSERT_NO_FATAL_FAILURE(checkScreenCapture(0, 255, 0, {10, 510, 490, 990}));
642 ASSERT_NO_FATAL_FAILURE(checkScreenCapture(0, 0, 255, {510, 10, 990, 490}));
643 ASSERT_NO_FATAL_FAILURE(checkScreenCapture(255, 0, 0, {510, 510, 990, 990}));
644 // Verify outside region is black.
645 ASSERT_NO_FATAL_FAILURE(checkScreenCapture(0, 0, 0,
646 {0, 0, (int32_t)windowSize.getWidth(),
647 (int32_t)windowSize.getHeight()},
648 /*border*/ 0, /*outsideRegion*/ true));
649}
650
651TEST_F(BLASTBufferQueueTest, ScaleCroppedBufferToWindowSize) {
652 // add black background
653 auto bg = mClient->createSurface(String8("BGTest"), 0, 0, PIXEL_FORMAT_RGBA_8888,
654 ISurfaceComposerClient::eFXSurfaceEffect);
655 ASSERT_NE(nullptr, bg.get());
656 Transaction t;
Dominik Laskowski29fa1462021-04-27 15:51:50 -0700657 t.setLayerStack(bg, ui::DEFAULT_LAYER_STACK)
Vishnu Nair5cc9ac02021-04-19 13:23:38 -0700658 .setCrop(bg, Rect(0, 0, mDisplayWidth, mDisplayHeight))
659 .setColor(bg, half3{0, 0, 0})
660 .setLayer(bg, 0)
661 .apply();
662
663 Rect windowSize(1000, 1000);
664 Rect bufferSize(500, 500);
665 Rect bufferCrop(100, 100, 350, 350);
666
667 BLASTBufferQueueHelper adapter(mSurfaceControl, windowSize.getWidth(), windowSize.getHeight());
668 sp<IGraphicBufferProducer> igbProducer;
669 setUpProducer(adapter, igbProducer);
670 int slot;
671 sp<Fence> fence;
672 sp<GraphicBuffer> buf;
673 auto ret = igbProducer->dequeueBuffer(&slot, &fence, bufferSize.getWidth(),
674 bufferSize.getHeight(), PIXEL_FORMAT_RGBA_8888,
675 GRALLOC_USAGE_SW_WRITE_OFTEN, nullptr, nullptr);
676 ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, ret);
677 ASSERT_EQ(OK, igbProducer->requestBuffer(slot, &buf));
678
679 uint32_t* bufData;
680 buf->lock(static_cast<uint32_t>(GraphicBuffer::USAGE_SW_WRITE_OFTEN),
681 reinterpret_cast<void**>(&bufData));
682 // fill buffer with grey
683 fillBuffer(bufData, bufferSize, buf->getStride(), 127, 127, 127);
684
685 // fill crop area with different colors so we can verify the cropped region has been scaled
686 // correctly.
687 fillBuffer(bufData, Rect(100, 100, 225, 225), buf->getStride(), /* rgb */ 255, 0, 0);
688 fillBuffer(bufData, Rect(100, 226, 225, 350), buf->getStride(), /* rgb */ 0, 255, 0);
689 fillBuffer(bufData, Rect(226, 100, 350, 225), buf->getStride(), /* rgb */ 0, 0, 255);
690 fillBuffer(bufData, Rect(226, 226, 350, 350), buf->getStride(), /* rgb */ 255, 0, 0);
691 buf->unlock();
692
693 IGraphicBufferProducer::QueueBufferOutput qbOutput;
694 IGraphicBufferProducer::QueueBufferInput input(systemTime(), true /* autotimestamp */,
695 HAL_DATASPACE_UNKNOWN,
696 bufferCrop /* Rect::INVALID_RECT */,
697 NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW, 0,
698 Fence::NO_FENCE);
699 igbProducer->queueBuffer(slot, input, &qbOutput);
700 ASSERT_NE(ui::Transform::ROT_INVALID, qbOutput.transformHint);
701
702 adapter.waitForCallbacks();
703
704 ASSERT_EQ(NO_ERROR, captureDisplay(mCaptureArgs, mCaptureResults));
705 // Verify cropped region is scaled correctly.
706 ASSERT_NO_FATAL_FAILURE(checkScreenCapture(255, 0, 0, {10, 10, 490, 490}));
707 ASSERT_NO_FATAL_FAILURE(checkScreenCapture(0, 255, 0, {10, 510, 490, 990}));
708 ASSERT_NO_FATAL_FAILURE(checkScreenCapture(0, 0, 255, {510, 10, 990, 490}));
709 ASSERT_NO_FATAL_FAILURE(checkScreenCapture(255, 0, 0, {510, 510, 990, 990}));
710 // Verify outside region is black.
711 ASSERT_NO_FATAL_FAILURE(checkScreenCapture(0, 0, 0,
712 {0, 0, (int32_t)windowSize.getWidth(),
713 (int32_t)windowSize.getHeight()},
714 /*border*/ 0, /*outsideRegion*/ true));
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800715}
716
Vishnu Nair932f6ae2021-09-29 17:33:10 -0700717// b/196339769 verify we can can update the requested size while the in FREEZE scaling mode and
718// scale the buffer properly when the mode changes to SCALE_TO_WINDOW
719TEST_F(BLASTBufferQueueTest, ScalingModeChanges) {
720 uint8_t r = 255;
721 uint8_t g = 0;
722 uint8_t b = 0;
723
724 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight / 4);
725 sp<IGraphicBufferProducer> igbProducer;
726 setUpProducer(adapter, igbProducer);
727 {
728 int slot;
729 sp<Fence> fence;
730 sp<GraphicBuffer> buf;
731 auto ret = igbProducer->dequeueBuffer(&slot, &fence, mDisplayWidth, mDisplayHeight / 4,
732 PIXEL_FORMAT_RGBA_8888, GRALLOC_USAGE_SW_WRITE_OFTEN,
733 nullptr, nullptr);
734 ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, ret);
735 ASSERT_EQ(OK, igbProducer->requestBuffer(slot, &buf));
736
737 uint32_t* bufData;
738 buf->lock(static_cast<uint32_t>(GraphicBuffer::USAGE_SW_WRITE_OFTEN),
739 reinterpret_cast<void**>(&bufData));
740 fillBuffer(bufData, Rect(buf->getWidth(), buf->getHeight()), buf->getStride(), r, g, b);
741 buf->unlock();
742
743 IGraphicBufferProducer::QueueBufferOutput qbOutput;
744 IGraphicBufferProducer::QueueBufferInput input(systemTime(), true /* autotimestamp */,
745 HAL_DATASPACE_UNKNOWN, {},
746 NATIVE_WINDOW_SCALING_MODE_FREEZE, 0,
747 Fence::NO_FENCE);
748 igbProducer->queueBuffer(slot, input, &qbOutput);
749 adapter.waitForCallbacks();
750 }
751 // capture screen and verify that it is red
752 ASSERT_EQ(NO_ERROR, captureDisplay(mCaptureArgs, mCaptureResults));
753
754 ASSERT_NO_FATAL_FAILURE(
755 checkScreenCapture(r, g, b,
756 {0, 0, (int32_t)mDisplayWidth, (int32_t)mDisplayHeight / 4}));
757
758 // update the size to half the display and dequeue a buffer quarter of the display.
759 adapter.update(mSurfaceControl, mDisplayWidth, mDisplayHeight / 2);
760
761 {
762 int slot;
763 sp<Fence> fence;
764 sp<GraphicBuffer> buf;
765 auto ret = igbProducer->dequeueBuffer(&slot, &fence, mDisplayWidth, mDisplayHeight / 8,
766 PIXEL_FORMAT_RGBA_8888, GRALLOC_USAGE_SW_WRITE_OFTEN,
767 nullptr, nullptr);
768 ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, ret);
769 ASSERT_EQ(OK, igbProducer->requestBuffer(slot, &buf));
770
771 uint32_t* bufData;
772 buf->lock(static_cast<uint32_t>(GraphicBuffer::USAGE_SW_WRITE_OFTEN),
773 reinterpret_cast<void**>(&bufData));
774 g = 255;
775 fillBuffer(bufData, Rect(buf->getWidth(), buf->getHeight()), buf->getStride(), r, g, b);
776 buf->unlock();
777
778 IGraphicBufferProducer::QueueBufferOutput qbOutput;
779 IGraphicBufferProducer::QueueBufferInput input(systemTime(), true /* autotimestamp */,
780 HAL_DATASPACE_UNKNOWN, {},
781 NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW,
782 0, Fence::NO_FENCE);
783 igbProducer->queueBuffer(slot, input, &qbOutput);
784 adapter.waitForCallbacks();
785 }
786 // capture screen and verify that it is red
787 ASSERT_EQ(NO_ERROR, captureDisplay(mCaptureArgs, mCaptureResults));
788 // verify we still scale the buffer to the new size (half the screen height)
789 ASSERT_NO_FATAL_FAILURE(
790 checkScreenCapture(r, g, b,
791 {0, 0, (int32_t)mDisplayWidth, (int32_t)mDisplayHeight / 2}));
792}
793
chaviwd7deef72021-10-06 11:53:40 -0500794TEST_F(BLASTBufferQueueTest, SyncThenNoSync) {
795 uint8_t r = 255;
796 uint8_t g = 0;
797 uint8_t b = 0;
798
799 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
800
801 sp<IGraphicBufferProducer> igbProducer;
802 setUpProducer(adapter, igbProducer);
803
chaviwa1c4c822021-11-10 18:11:58 -0600804 Transaction sync;
805 adapter.setSyncTransaction(&sync);
chaviwd7deef72021-10-06 11:53:40 -0500806 queueBuffer(igbProducer, 0, 255, 0, 0);
807
808 // queue non sync buffer, so this one should get blocked
809 // Add a present delay to allow the first screenshot to get taken.
810 nsecs_t presentTimeDelay = std::chrono::nanoseconds(500ms).count();
811 queueBuffer(igbProducer, r, g, b, presentTimeDelay);
812
813 CallbackHelper transactionCallback;
chaviwa1c4c822021-11-10 18:11:58 -0600814 sync.addTransactionCompletedCallback(transactionCallback.function,
chaviwd7deef72021-10-06 11:53:40 -0500815 transactionCallback.getContext())
816 .apply();
817
818 CallbackData callbackData;
819 transactionCallback.getCallbackData(&callbackData);
820
821 // capture screen and verify that it is red
822 ASSERT_EQ(NO_ERROR, captureDisplay(mCaptureArgs, mCaptureResults));
823 ASSERT_NO_FATAL_FAILURE(
824 checkScreenCapture(0, 255, 0, {0, 0, (int32_t)mDisplayWidth, (int32_t)mDisplayHeight}));
825
826 mProducerListener->waitOnNumberReleased(1);
827 ASSERT_EQ(NO_ERROR, captureDisplay(mCaptureArgs, mCaptureResults));
828 ASSERT_NO_FATAL_FAILURE(
829 checkScreenCapture(r, g, b, {0, 0, (int32_t)mDisplayWidth, (int32_t)mDisplayHeight}));
830}
831
832TEST_F(BLASTBufferQueueTest, MultipleSyncTransactions) {
833 uint8_t r = 255;
834 uint8_t g = 0;
835 uint8_t b = 0;
836
837 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
838
839 sp<IGraphicBufferProducer> igbProducer;
840 setUpProducer(adapter, igbProducer);
841
842 Transaction mainTransaction;
843
chaviwa1c4c822021-11-10 18:11:58 -0600844 Transaction sync;
845 adapter.setSyncTransaction(&sync);
chaviwd7deef72021-10-06 11:53:40 -0500846 queueBuffer(igbProducer, 0, 255, 0, 0);
847
chaviwa1c4c822021-11-10 18:11:58 -0600848 mainTransaction.merge(std::move(sync));
chaviwd7deef72021-10-06 11:53:40 -0500849
chaviwa1c4c822021-11-10 18:11:58 -0600850 adapter.setSyncTransaction(&sync);
chaviwd7deef72021-10-06 11:53:40 -0500851 queueBuffer(igbProducer, r, g, b, 0);
852
chaviwa1c4c822021-11-10 18:11:58 -0600853 mainTransaction.merge(std::move(sync));
chaviwd7deef72021-10-06 11:53:40 -0500854 // Expect 1 buffer to be released even before sending to SurfaceFlinger
855 mProducerListener->waitOnNumberReleased(1);
856
857 CallbackHelper transactionCallback;
858 mainTransaction
859 .addTransactionCompletedCallback(transactionCallback.function,
860 transactionCallback.getContext())
861 .apply();
862
863 CallbackData callbackData;
864 transactionCallback.getCallbackData(&callbackData);
865
866 // capture screen and verify that it is red
867 ASSERT_EQ(NO_ERROR, captureDisplay(mCaptureArgs, mCaptureResults));
868 ASSERT_NO_FATAL_FAILURE(
869 checkScreenCapture(r, g, b, {0, 0, (int32_t)mDisplayWidth, (int32_t)mDisplayHeight}));
870}
871
872TEST_F(BLASTBufferQueueTest, MultipleSyncTransactionWithNonSync) {
873 uint8_t r = 255;
874 uint8_t g = 0;
875 uint8_t b = 0;
876
877 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
878
879 sp<IGraphicBufferProducer> igbProducer;
880 setUpProducer(adapter, igbProducer);
881
882 Transaction mainTransaction;
883
chaviwa1c4c822021-11-10 18:11:58 -0600884 Transaction sync;
chaviwd7deef72021-10-06 11:53:40 -0500885 // queue a sync transaction
chaviwa1c4c822021-11-10 18:11:58 -0600886 adapter.setSyncTransaction(&sync);
chaviwd7deef72021-10-06 11:53:40 -0500887 queueBuffer(igbProducer, 0, 255, 0, 0);
888
chaviwa1c4c822021-11-10 18:11:58 -0600889 mainTransaction.merge(std::move(sync));
chaviwd7deef72021-10-06 11:53:40 -0500890
chaviwa1c4c822021-11-10 18:11:58 -0600891 // queue another buffer without setting sync transaction
chaviwd7deef72021-10-06 11:53:40 -0500892 queueBuffer(igbProducer, 0, 0, 255, 0);
893
894 // queue another sync transaction
chaviwa1c4c822021-11-10 18:11:58 -0600895 adapter.setSyncTransaction(&sync);
chaviwd7deef72021-10-06 11:53:40 -0500896 queueBuffer(igbProducer, r, g, b, 0);
897 // Expect 1 buffer to be released because the non sync transaction should merge
898 // with the sync
899 mProducerListener->waitOnNumberReleased(1);
900
chaviwa1c4c822021-11-10 18:11:58 -0600901 mainTransaction.merge(std::move(sync));
chaviwd7deef72021-10-06 11:53:40 -0500902 // Expect 2 buffers to be released due to merging the two syncs.
903 mProducerListener->waitOnNumberReleased(2);
904
905 CallbackHelper transactionCallback;
906 mainTransaction
907 .addTransactionCompletedCallback(transactionCallback.function,
908 transactionCallback.getContext())
909 .apply();
910
911 CallbackData callbackData;
912 transactionCallback.getCallbackData(&callbackData);
913
914 // capture screen and verify that it is red
915 ASSERT_EQ(NO_ERROR, captureDisplay(mCaptureArgs, mCaptureResults));
916 ASSERT_NO_FATAL_FAILURE(
917 checkScreenCapture(r, g, b, {0, 0, (int32_t)mDisplayWidth, (int32_t)mDisplayHeight}));
918}
919
920TEST_F(BLASTBufferQueueTest, MultipleSyncRunOutOfBuffers) {
921 uint8_t r = 255;
922 uint8_t g = 0;
923 uint8_t b = 0;
924
925 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
926
927 sp<IGraphicBufferProducer> igbProducer;
928 setUpProducer(adapter, igbProducer, 3);
929
930 Transaction mainTransaction;
931
chaviwa1c4c822021-11-10 18:11:58 -0600932 Transaction sync;
chaviwd7deef72021-10-06 11:53:40 -0500933 // queue a sync transaction
chaviwa1c4c822021-11-10 18:11:58 -0600934 adapter.setSyncTransaction(&sync);
chaviwd7deef72021-10-06 11:53:40 -0500935 queueBuffer(igbProducer, 0, 255, 0, 0);
936
chaviwa1c4c822021-11-10 18:11:58 -0600937 mainTransaction.merge(std::move(sync));
chaviwd7deef72021-10-06 11:53:40 -0500938
chaviwa1c4c822021-11-10 18:11:58 -0600939 // queue a few buffers without setting sync transaction
chaviwd7deef72021-10-06 11:53:40 -0500940 queueBuffer(igbProducer, 0, 0, 255, 0);
941 queueBuffer(igbProducer, 0, 0, 255, 0);
942 queueBuffer(igbProducer, 0, 0, 255, 0);
943
944 // queue another sync transaction
chaviwa1c4c822021-11-10 18:11:58 -0600945 adapter.setSyncTransaction(&sync);
chaviwd7deef72021-10-06 11:53:40 -0500946 queueBuffer(igbProducer, r, g, b, 0);
947 // Expect 3 buffers to be released because the non sync transactions should merge
948 // with the sync
949 mProducerListener->waitOnNumberReleased(3);
950
chaviwa1c4c822021-11-10 18:11:58 -0600951 mainTransaction.merge(std::move(sync));
chaviwd7deef72021-10-06 11:53:40 -0500952 // Expect 4 buffers to be released due to merging the two syncs.
953 mProducerListener->waitOnNumberReleased(4);
954
955 CallbackHelper transactionCallback;
956 mainTransaction
957 .addTransactionCompletedCallback(transactionCallback.function,
958 transactionCallback.getContext())
959 .apply();
960
961 CallbackData callbackData;
962 transactionCallback.getCallbackData(&callbackData);
963
964 // capture screen and verify that it is red
965 ASSERT_EQ(NO_ERROR, captureDisplay(mCaptureArgs, mCaptureResults));
966 ASSERT_NO_FATAL_FAILURE(
967 checkScreenCapture(r, g, b, {0, 0, (int32_t)mDisplayWidth, (int32_t)mDisplayHeight}));
968}
969
970// Tests BBQ with a sync transaction when the buffers acquired reaches max and the only way to
971// continue processing is for a release callback from SurfaceFlinger.
972// This is done by sending a buffer to SF so it can release the previous one and allow BBQ to
973// continue acquiring buffers.
974TEST_F(BLASTBufferQueueTest, RunOutOfBuffersWaitingOnSF) {
975 uint8_t r = 255;
976 uint8_t g = 0;
977 uint8_t b = 0;
978
979 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
980
981 sp<IGraphicBufferProducer> igbProducer;
982 setUpProducer(adapter, igbProducer, 4);
983
984 Transaction mainTransaction;
985
986 // Send a buffer to SF
987 queueBuffer(igbProducer, 0, 255, 0, 0);
988
chaviwa1c4c822021-11-10 18:11:58 -0600989 Transaction sync;
chaviwd7deef72021-10-06 11:53:40 -0500990 // queue a sync transaction
chaviwa1c4c822021-11-10 18:11:58 -0600991 adapter.setSyncTransaction(&sync);
chaviwd7deef72021-10-06 11:53:40 -0500992 queueBuffer(igbProducer, 0, 255, 0, 0);
993
chaviwa1c4c822021-11-10 18:11:58 -0600994 mainTransaction.merge(std::move(sync));
chaviwd7deef72021-10-06 11:53:40 -0500995
chaviwa1c4c822021-11-10 18:11:58 -0600996 // queue a few buffers without setting sync transaction
chaviwd7deef72021-10-06 11:53:40 -0500997 queueBuffer(igbProducer, 0, 0, 255, 0);
998 queueBuffer(igbProducer, 0, 0, 255, 0);
999 queueBuffer(igbProducer, 0, 0, 255, 0);
1000
1001 // apply the first synced buffer to ensure we have to wait on SF
1002 mainTransaction.apply();
1003
1004 // queue another sync transaction
chaviwa1c4c822021-11-10 18:11:58 -06001005 adapter.setSyncTransaction(&sync);
chaviwd7deef72021-10-06 11:53:40 -05001006 queueBuffer(igbProducer, r, g, b, 0);
1007 // Expect 2 buffers to be released because the non sync transactions should merge
1008 // with the sync
1009 mProducerListener->waitOnNumberReleased(3);
1010
chaviwa1c4c822021-11-10 18:11:58 -06001011 mainTransaction.merge(std::move(sync));
chaviwd7deef72021-10-06 11:53:40 -05001012
1013 CallbackHelper transactionCallback;
1014 mainTransaction
1015 .addTransactionCompletedCallback(transactionCallback.function,
1016 transactionCallback.getContext())
1017 .apply();
1018
1019 CallbackData callbackData;
1020 transactionCallback.getCallbackData(&callbackData);
1021
1022 // capture screen and verify that it is red
1023 ASSERT_EQ(NO_ERROR, captureDisplay(mCaptureArgs, mCaptureResults));
1024 ASSERT_NO_FATAL_FAILURE(
1025 checkScreenCapture(r, g, b, {0, 0, (int32_t)mDisplayWidth, (int32_t)mDisplayHeight}));
1026}
1027
Vishnu Nair89496122020-12-14 17:14:53 -08001028class TestProducerListener : public BnProducerListener {
1029public:
1030 sp<IGraphicBufferProducer> mIgbp;
1031 TestProducerListener(const sp<IGraphicBufferProducer>& igbp) : mIgbp(igbp) {}
1032 void onBufferReleased() override {
1033 sp<GraphicBuffer> buffer;
1034 sp<Fence> fence;
1035 mIgbp->detachNextBuffer(&buffer, &fence);
1036 }
1037};
1038
1039TEST_F(BLASTBufferQueueTest, CustomProducerListener) {
1040 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
1041 sp<IGraphicBufferProducer> igbProducer = adapter.getIGraphicBufferProducer();
1042 ASSERT_NE(nullptr, igbProducer.get());
1043 ASSERT_EQ(NO_ERROR, igbProducer->setMaxDequeuedBufferCount(2));
1044 IGraphicBufferProducer::QueueBufferOutput qbOutput;
1045 ASSERT_EQ(NO_ERROR,
1046 igbProducer->connect(new TestProducerListener(igbProducer), NATIVE_WINDOW_API_CPU,
1047 false, &qbOutput));
1048 ASSERT_NE(ui::Transform::ROT_INVALID, qbOutput.transformHint);
1049 for (int i = 0; i < 3; i++) {
1050 int slot;
1051 sp<Fence> fence;
1052 sp<GraphicBuffer> buf;
1053 auto ret = igbProducer->dequeueBuffer(&slot, &fence, mDisplayWidth, mDisplayHeight,
1054 PIXEL_FORMAT_RGBA_8888, GRALLOC_USAGE_SW_WRITE_OFTEN,
1055 nullptr, nullptr);
1056 ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, ret);
1057 ASSERT_EQ(OK, igbProducer->requestBuffer(slot, &buf));
1058 IGraphicBufferProducer::QueueBufferOutput qbOutput;
Vishnu Nair1506b182021-02-22 14:35:15 -08001059 IGraphicBufferProducer::QueueBufferInput input(systemTime(), true /* autotimestamp */,
1060 HAL_DATASPACE_UNKNOWN,
Vishnu Nair89496122020-12-14 17:14:53 -08001061 Rect(mDisplayWidth, mDisplayHeight),
1062 NATIVE_WINDOW_SCALING_MODE_FREEZE, 0,
1063 Fence::NO_FENCE);
1064 igbProducer->queueBuffer(slot, input, &qbOutput);
1065 }
1066 adapter.waitForCallbacks();
1067}
1068
Vishnu Nair17dde612020-12-28 11:39:59 -08001069TEST_F(BLASTBufferQueueTest, QueryNativeWindowQueuesToWindowComposer) {
1070 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
1071
1072 sp<android::Surface> surface = new Surface(adapter.getIGraphicBufferProducer());
1073 ANativeWindow* nativeWindow = (ANativeWindow*)(surface.get());
1074 int queuesToNativeWindow = 0;
1075 int err = nativeWindow->query(nativeWindow, NATIVE_WINDOW_QUEUES_TO_WINDOW_COMPOSER,
1076 &queuesToNativeWindow);
1077 ASSERT_EQ(NO_ERROR, err);
1078 ASSERT_EQ(queuesToNativeWindow, 1);
1079}
1080
Vishnu Nair083efd32021-02-12 09:32:30 -08001081// Test a slow producer doesn't hold up a faster producer from the same client. Essentially tests
1082// BBQ uses separate transaction queues.
Vishnu Nair277142c2021-01-05 18:35:29 -08001083TEST_F(BLASTBufferQueueTest, OutOfOrderTransactionTest) {
1084 sp<SurfaceControl> bgSurface =
1085 mClient->createSurface(String8("BGTest"), 0, 0, PIXEL_FORMAT_RGBA_8888,
1086 ISurfaceComposerClient::eFXSurfaceBufferState);
1087 ASSERT_NE(nullptr, bgSurface.get());
1088 Transaction t;
Dominik Laskowski29fa1462021-04-27 15:51:50 -07001089 t.setLayerStack(bgSurface, ui::DEFAULT_LAYER_STACK)
Vishnu Nair277142c2021-01-05 18:35:29 -08001090 .show(bgSurface)
1091 .setDataspace(bgSurface, ui::Dataspace::V0_SRGB)
Vishnu Nair277142c2021-01-05 18:35:29 -08001092 .setLayer(bgSurface, std::numeric_limits<int32_t>::max() - 1)
1093 .apply();
1094
1095 BLASTBufferQueueHelper slowAdapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
1096 sp<IGraphicBufferProducer> slowIgbProducer;
1097 setUpProducer(slowAdapter, slowIgbProducer);
1098 nsecs_t presentTimeDelay = std::chrono::nanoseconds(500ms).count();
Vishnu Nair1506b182021-02-22 14:35:15 -08001099 queueBuffer(slowIgbProducer, 0 /* r */, 255 /* g */, 0 /* b */, presentTimeDelay);
Vishnu Nair277142c2021-01-05 18:35:29 -08001100
1101 BLASTBufferQueueHelper fastAdapter(bgSurface, mDisplayWidth, mDisplayHeight);
1102 sp<IGraphicBufferProducer> fastIgbProducer;
1103 setUpProducer(fastAdapter, fastIgbProducer);
1104 uint8_t r = 255;
1105 uint8_t g = 0;
1106 uint8_t b = 0;
1107 queueBuffer(fastIgbProducer, r, g, b, 0 /* presentTimeDelay */);
1108 fastAdapter.waitForCallbacks();
1109
1110 // capture screen and verify that it is red
1111 ASSERT_EQ(NO_ERROR, captureDisplay(mCaptureArgs, mCaptureResults));
1112
1113 ASSERT_NO_FATAL_FAILURE(
Chavi Weingartena5aedbd2021-04-09 13:37:33 +00001114 checkScreenCapture(r, g, b,
1115 {0, 0, (int32_t)mDisplayWidth, (int32_t)mDisplayHeight / 2}));
Vishnu Nair277142c2021-01-05 18:35:29 -08001116}
1117
Vishnu Naira4fbca52021-07-07 16:52:34 -07001118TEST_F(BLASTBufferQueueTest, TransformHint) {
1119 // Transform hint is provided to BBQ via the surface control passed by WM
1120 mSurfaceControl->setTransformHint(ui::Transform::ROT_90);
1121
1122 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
1123 sp<IGraphicBufferProducer> igbProducer = adapter.getIGraphicBufferProducer();
1124 ASSERT_NE(nullptr, igbProducer.get());
1125 ASSERT_EQ(NO_ERROR, igbProducer->setMaxDequeuedBufferCount(2));
1126 sp<Surface> surface = adapter.getSurface();
1127
1128 // Before connecting to the surface, we do not get a valid transform hint
1129 int transformHint;
1130 surface->query(NATIVE_WINDOW_TRANSFORM_HINT, &transformHint);
1131 ASSERT_EQ(ui::Transform::ROT_0, transformHint);
1132
1133 ASSERT_EQ(NO_ERROR,
1134 surface->connect(NATIVE_WINDOW_API_CPU, new TestProducerListener(igbProducer)));
1135
1136 // After connecting to the surface, we should get the correct hint.
1137 surface->query(NATIVE_WINDOW_TRANSFORM_HINT, &transformHint);
1138 ASSERT_EQ(ui::Transform::ROT_90, transformHint);
1139
1140 ANativeWindow_Buffer buffer;
1141 surface->lock(&buffer, nullptr /* inOutDirtyBounds */);
1142
1143 // Transform hint is updated via callbacks or surface control updates
1144 mSurfaceControl->setTransformHint(ui::Transform::ROT_0);
1145 adapter.update(mSurfaceControl, mDisplayWidth, mDisplayHeight);
1146
1147 // The hint does not change and matches the value used when dequeueing the buffer.
1148 surface->query(NATIVE_WINDOW_TRANSFORM_HINT, &transformHint);
1149 ASSERT_EQ(ui::Transform::ROT_90, transformHint);
1150
1151 surface->unlockAndPost();
1152
1153 // After queuing the buffer, we get the updated transform hint
1154 surface->query(NATIVE_WINDOW_TRANSFORM_HINT, &transformHint);
1155 ASSERT_EQ(ui::Transform::ROT_0, transformHint);
1156
1157 adapter.waitForCallbacks();
1158}
1159
Valerie Hau5977fc82019-12-05 15:56:39 -08001160class BLASTBufferQueueTransformTest : public BLASTBufferQueueTest {
1161public:
1162 void test(uint32_t tr) {
1163 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
1164 sp<IGraphicBufferProducer> igbProducer;
1165 setUpProducer(adapter, igbProducer);
1166
1167 auto bufWidth = mDisplayWidth;
1168 auto bufHeight = mDisplayHeight;
1169 int slot;
1170 sp<Fence> fence;
1171 sp<GraphicBuffer> buf;
1172
1173 auto ret = igbProducer->dequeueBuffer(&slot, &fence, bufWidth, bufHeight,
1174 PIXEL_FORMAT_RGBA_8888, GRALLOC_USAGE_SW_WRITE_OFTEN,
1175 nullptr, nullptr);
1176 ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, ret);
1177 ASSERT_EQ(OK, igbProducer->requestBuffer(slot, &buf));
1178
1179 fillQuadrants(buf);
1180
1181 IGraphicBufferProducer::QueueBufferOutput qbOutput;
Vishnu Nair1506b182021-02-22 14:35:15 -08001182 IGraphicBufferProducer::QueueBufferInput input(systemTime(), true /* autotimestamp */,
1183 HAL_DATASPACE_UNKNOWN,
Valerie Hau5977fc82019-12-05 15:56:39 -08001184 Rect(bufWidth, bufHeight),
Vishnu Naire1a42322020-10-02 17:42:04 -07001185 NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW,
1186 tr, Fence::NO_FENCE);
Valerie Hau5977fc82019-12-05 15:56:39 -08001187 igbProducer->queueBuffer(slot, input, &qbOutput);
Dominik Laskowski718f9602019-11-09 20:01:35 -08001188 ASSERT_NE(ui::Transform::ROT_INVALID, qbOutput.transformHint);
Valerie Hau5977fc82019-12-05 15:56:39 -08001189
1190 adapter.waitForCallbacks();
chaviw8ffc7b82020-08-18 11:25:37 -07001191 ASSERT_EQ(NO_ERROR, captureDisplay(mCaptureArgs, mCaptureResults));
chaviwd2432892020-07-24 17:42:39 -07001192
Valerie Hau5977fc82019-12-05 15:56:39 -08001193 switch (tr) {
1194 case ui::Transform::ROT_0:
1195 ASSERT_NO_FATAL_FAILURE(checkScreenCapture(0, 0, 0,
1196 {0, 0, (int32_t)mDisplayWidth / 2,
1197 (int32_t)mDisplayHeight / 2},
1198 1));
1199 ASSERT_NO_FATAL_FAILURE(
1200 checkScreenCapture(255, 0, 0,
1201 {(int32_t)mDisplayWidth / 2, 0, (int32_t)mDisplayWidth,
1202 (int32_t)mDisplayHeight / 2},
1203 1));
1204 ASSERT_NO_FATAL_FAILURE(
1205 checkScreenCapture(0, 255, 0,
1206 {(int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight / 2,
1207 (int32_t)mDisplayWidth, (int32_t)mDisplayHeight},
1208 1));
1209 ASSERT_NO_FATAL_FAILURE(
1210 checkScreenCapture(0, 0, 255,
1211 {0, (int32_t)mDisplayHeight / 2,
1212 (int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight},
1213 1));
1214 break;
1215 case ui::Transform::FLIP_H:
1216 ASSERT_NO_FATAL_FAILURE(checkScreenCapture(255, 0, 0,
1217 {0, 0, (int32_t)mDisplayWidth / 2,
1218 (int32_t)mDisplayHeight / 2},
1219 1));
1220 ASSERT_NO_FATAL_FAILURE(
1221 checkScreenCapture(0, 0, 0,
1222 {(int32_t)mDisplayWidth / 2, 0, (int32_t)mDisplayWidth,
1223 (int32_t)mDisplayHeight / 2},
1224 1));
1225 ASSERT_NO_FATAL_FAILURE(
1226 checkScreenCapture(0, 0, 255,
1227 {(int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight / 2,
1228 (int32_t)mDisplayWidth, (int32_t)mDisplayHeight},
1229 1));
1230 ASSERT_NO_FATAL_FAILURE(
1231 checkScreenCapture(0, 255, 0,
1232 {0, (int32_t)mDisplayHeight / 2,
1233 (int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight},
1234 1));
1235 break;
1236 case ui::Transform::FLIP_V:
1237 ASSERT_NO_FATAL_FAILURE(checkScreenCapture(0, 0, 255,
1238 {0, 0, (int32_t)mDisplayWidth / 2,
1239 (int32_t)mDisplayHeight / 2},
1240 1));
1241 ASSERT_NO_FATAL_FAILURE(
1242 checkScreenCapture(0, 255, 0,
1243 {(int32_t)mDisplayWidth / 2, 0, (int32_t)mDisplayWidth,
1244 (int32_t)mDisplayHeight / 2},
1245 1));
1246 ASSERT_NO_FATAL_FAILURE(
1247 checkScreenCapture(255, 0, 0,
1248 {(int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight / 2,
1249 (int32_t)mDisplayWidth, (int32_t)mDisplayHeight},
1250 1));
1251 ASSERT_NO_FATAL_FAILURE(
1252 checkScreenCapture(0, 0, 0,
1253 {0, (int32_t)mDisplayHeight / 2,
1254 (int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight},
1255 1));
1256 break;
1257 case ui::Transform::ROT_90:
1258 ASSERT_NO_FATAL_FAILURE(checkScreenCapture(0, 0, 255,
1259 {0, 0, (int32_t)mDisplayWidth / 2,
1260 (int32_t)mDisplayHeight / 2},
1261 1));
1262 ASSERT_NO_FATAL_FAILURE(
1263 checkScreenCapture(0, 0, 0,
1264 {(int32_t)mDisplayWidth / 2, 0, (int32_t)mDisplayWidth,
1265 (int32_t)mDisplayHeight / 2},
1266 1));
1267 ASSERT_NO_FATAL_FAILURE(
1268 checkScreenCapture(255, 0, 0,
1269 {(int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight / 2,
1270 (int32_t)mDisplayWidth, (int32_t)mDisplayHeight},
1271 1));
1272 ASSERT_NO_FATAL_FAILURE(
1273 checkScreenCapture(0, 255, 0,
1274 {0, (int32_t)mDisplayHeight / 2,
1275 (int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight},
1276 1));
1277 break;
1278 case ui::Transform::ROT_180:
1279 ASSERT_NO_FATAL_FAILURE(checkScreenCapture(0, 255, 0,
1280 {0, 0, (int32_t)mDisplayWidth / 2,
1281 (int32_t)mDisplayHeight / 2},
1282 1));
1283 ASSERT_NO_FATAL_FAILURE(
1284 checkScreenCapture(0, 0, 255,
1285 {(int32_t)mDisplayWidth / 2, 0, (int32_t)mDisplayWidth,
1286 (int32_t)mDisplayHeight / 2},
1287 1));
1288 ASSERT_NO_FATAL_FAILURE(
1289 checkScreenCapture(0, 0, 0,
1290 {(int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight / 2,
1291 (int32_t)mDisplayWidth, (int32_t)mDisplayHeight},
1292 1));
1293 ASSERT_NO_FATAL_FAILURE(
1294 checkScreenCapture(255, 0, 0,
1295 {0, (int32_t)mDisplayHeight / 2,
1296 (int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight},
1297 1));
1298 break;
1299 case ui::Transform::ROT_270:
1300 ASSERT_NO_FATAL_FAILURE(checkScreenCapture(255, 0, 0,
1301 {0, 0, (int32_t)mDisplayWidth / 2,
1302 (int32_t)mDisplayHeight / 2},
1303 1));
1304 ASSERT_NO_FATAL_FAILURE(
1305 checkScreenCapture(0, 255, 0,
1306 {(int32_t)mDisplayWidth / 2, 0, (int32_t)mDisplayWidth,
1307 (int32_t)mDisplayHeight / 2},
1308 1));
1309 ASSERT_NO_FATAL_FAILURE(
1310 checkScreenCapture(0, 0, 255,
1311 {(int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight / 2,
1312 (int32_t)mDisplayWidth, (int32_t)mDisplayHeight},
1313 1));
1314 ASSERT_NO_FATAL_FAILURE(
1315 checkScreenCapture(0, 0, 0,
1316 {0, (int32_t)mDisplayHeight / 2,
1317 (int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight},
1318 1));
1319 }
1320 }
1321};
1322
1323TEST_F(BLASTBufferQueueTransformTest, setTransform_ROT_0) {
1324 test(ui::Transform::ROT_0);
1325}
1326
1327TEST_F(BLASTBufferQueueTransformTest, setTransform_FLIP_H) {
1328 test(ui::Transform::FLIP_H);
1329}
1330
1331TEST_F(BLASTBufferQueueTransformTest, setTransform_FLIP_V) {
1332 test(ui::Transform::FLIP_V);
1333}
1334
1335TEST_F(BLASTBufferQueueTransformTest, setTransform_ROT_90) {
1336 test(ui::Transform::ROT_90);
1337}
1338
1339TEST_F(BLASTBufferQueueTransformTest, setTransform_ROT_180) {
1340 test(ui::Transform::ROT_180);
1341}
1342
1343TEST_F(BLASTBufferQueueTransformTest, setTransform_ROT_270) {
1344 test(ui::Transform::ROT_270);
1345}
Valerie Hau871d6352020-01-29 08:44:02 -08001346
1347class BLASTFrameEventHistoryTest : public BLASTBufferQueueTest {
1348public:
1349 void setUpAndQueueBuffer(const sp<IGraphicBufferProducer>& igbProducer,
Vishnu Nairde66dc72021-06-17 17:54:41 -07001350 nsecs_t* outRequestedPresentTime, nsecs_t* postedTime,
Valerie Hau871d6352020-01-29 08:44:02 -08001351 IGraphicBufferProducer::QueueBufferOutput* qbOutput,
Vishnu Nairde66dc72021-06-17 17:54:41 -07001352 bool getFrameTimestamps, nsecs_t requestedPresentTime = systemTime()) {
Valerie Hau871d6352020-01-29 08:44:02 -08001353 int slot;
1354 sp<Fence> fence;
1355 sp<GraphicBuffer> buf;
1356 auto ret = igbProducer->dequeueBuffer(&slot, &fence, mDisplayWidth, mDisplayHeight,
1357 PIXEL_FORMAT_RGBA_8888, GRALLOC_USAGE_SW_WRITE_OFTEN,
1358 nullptr, nullptr);
Vishnu Nairde66dc72021-06-17 17:54:41 -07001359 if (IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION == ret) {
1360 ASSERT_EQ(OK, igbProducer->requestBuffer(slot, &buf));
1361 }
Valerie Hau871d6352020-01-29 08:44:02 -08001362
Vishnu Nairde66dc72021-06-17 17:54:41 -07001363 *outRequestedPresentTime = requestedPresentTime;
1364 IGraphicBufferProducer::QueueBufferInput input(requestedPresentTime, false,
1365 HAL_DATASPACE_UNKNOWN,
Valerie Hau871d6352020-01-29 08:44:02 -08001366 Rect(mDisplayWidth, mDisplayHeight),
1367 NATIVE_WINDOW_SCALING_MODE_FREEZE, 0,
1368 Fence::NO_FENCE, /*sticky*/ 0,
1369 getFrameTimestamps);
1370 if (postedTime) *postedTime = systemTime();
1371 igbProducer->queueBuffer(slot, input, qbOutput);
1372 }
Vishnu Nair083efd32021-02-12 09:32:30 -08001373 sp<SurfaceControl> mBufferQueueSurfaceControl;
Valerie Hau871d6352020-01-29 08:44:02 -08001374};
1375
1376TEST_F(BLASTFrameEventHistoryTest, FrameEventHistory_Basic) {
1377 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
1378 sp<IGraphicBufferProducer> igbProducer;
1379 ProducerFrameEventHistory history;
1380 setUpProducer(adapter, igbProducer);
1381
1382 IGraphicBufferProducer::QueueBufferOutput qbOutput;
1383 nsecs_t requestedPresentTimeA = 0;
1384 nsecs_t postedTimeA = 0;
1385 setUpAndQueueBuffer(igbProducer, &requestedPresentTimeA, &postedTimeA, &qbOutput, true);
1386 history.applyDelta(qbOutput.frameTimestamps);
1387
1388 FrameEvents* events = nullptr;
1389 events = history.getFrame(1);
1390 ASSERT_NE(nullptr, events);
1391 ASSERT_EQ(1, events->frameNumber);
1392 ASSERT_EQ(requestedPresentTimeA, events->requestedPresentTime);
1393 ASSERT_GE(events->postedTime, postedTimeA);
1394
Vishnu Nair1506b182021-02-22 14:35:15 -08001395 adapter.waitForCallback(1);
Valerie Hau871d6352020-01-29 08:44:02 -08001396
1397 // queue another buffer so we query for frame event deltas
1398 nsecs_t requestedPresentTimeB = 0;
1399 nsecs_t postedTimeB = 0;
1400 setUpAndQueueBuffer(igbProducer, &requestedPresentTimeB, &postedTimeB, &qbOutput, true);
1401 history.applyDelta(qbOutput.frameTimestamps);
1402 events = history.getFrame(1);
1403 ASSERT_NE(nullptr, events);
1404
1405 // frame number, requestedPresentTime, and postTime should not have changed
1406 ASSERT_EQ(1, events->frameNumber);
1407 ASSERT_EQ(requestedPresentTimeA, events->requestedPresentTime);
1408 ASSERT_GE(events->postedTime, postedTimeA);
1409
1410 ASSERT_GE(events->latchTime, postedTimeA);
1411 ASSERT_GE(events->dequeueReadyTime, events->latchTime);
1412 ASSERT_NE(nullptr, events->gpuCompositionDoneFence);
1413 ASSERT_NE(nullptr, events->displayPresentFence);
1414 ASSERT_NE(nullptr, events->releaseFence);
1415
1416 // we should also have gotten the initial values for the next frame
1417 events = history.getFrame(2);
1418 ASSERT_NE(nullptr, events);
1419 ASSERT_EQ(2, events->frameNumber);
1420 ASSERT_EQ(requestedPresentTimeB, events->requestedPresentTime);
1421 ASSERT_GE(events->postedTime, postedTimeB);
Valerie Hau78491e92020-04-15 13:10:56 -07001422
1423 // wait for any callbacks that have not been received
1424 adapter.waitForCallbacks();
Valerie Hau871d6352020-01-29 08:44:02 -08001425}
Vishnu Nair083efd32021-02-12 09:32:30 -08001426
Vishnu Nair083efd32021-02-12 09:32:30 -08001427TEST_F(BLASTFrameEventHistoryTest, FrameEventHistory_DroppedFrame) {
1428 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
1429 sp<IGraphicBufferProducer> igbProducer;
1430 setUpProducer(adapter, igbProducer);
1431
1432 ProducerFrameEventHistory history;
1433 IGraphicBufferProducer::QueueBufferOutput qbOutput;
1434 nsecs_t requestedPresentTimeA = 0;
1435 nsecs_t postedTimeA = 0;
Vishnu Nairde66dc72021-06-17 17:54:41 -07001436 // Present the frame sometime in the future so we can add two frames to the queue so the older
1437 // one will be dropped.
1438 nsecs_t presentTime = systemTime() + std::chrono::nanoseconds(500ms).count();
Vishnu Nair083efd32021-02-12 09:32:30 -08001439 setUpAndQueueBuffer(igbProducer, &requestedPresentTimeA, &postedTimeA, &qbOutput, true,
Vishnu Nairde66dc72021-06-17 17:54:41 -07001440 presentTime);
Vishnu Nair083efd32021-02-12 09:32:30 -08001441 history.applyDelta(qbOutput.frameTimestamps);
1442
1443 FrameEvents* events = nullptr;
1444 events = history.getFrame(1);
1445 ASSERT_NE(nullptr, events);
1446 ASSERT_EQ(1, events->frameNumber);
1447 ASSERT_EQ(requestedPresentTimeA, events->requestedPresentTime);
1448 ASSERT_GE(events->postedTime, postedTimeA);
1449
1450 // queue another buffer so the first can be dropped
1451 nsecs_t requestedPresentTimeB = 0;
1452 nsecs_t postedTimeB = 0;
Vishnu Nairde66dc72021-06-17 17:54:41 -07001453 presentTime = systemTime() + std::chrono::nanoseconds(1ms).count();
1454 setUpAndQueueBuffer(igbProducer, &requestedPresentTimeB, &postedTimeB, &qbOutput, true,
1455 presentTime);
Vishnu Nair083efd32021-02-12 09:32:30 -08001456 history.applyDelta(qbOutput.frameTimestamps);
1457 events = history.getFrame(1);
1458 ASSERT_NE(nullptr, events);
1459
1460 // frame number, requestedPresentTime, and postTime should not have changed
1461 ASSERT_EQ(1, events->frameNumber);
1462 ASSERT_EQ(requestedPresentTimeA, events->requestedPresentTime);
1463 ASSERT_GE(events->postedTime, postedTimeA);
1464
Vishnu Nairde66dc72021-06-17 17:54:41 -07001465 // a valid latchtime and pre and post composition info should not be set for the dropped frame
Vishnu Nair083efd32021-02-12 09:32:30 -08001466 ASSERT_FALSE(events->hasLatchInfo());
1467 ASSERT_FALSE(events->hasDequeueReadyInfo());
Vishnu Nairde66dc72021-06-17 17:54:41 -07001468 ASSERT_FALSE(events->hasGpuCompositionDoneInfo());
1469 ASSERT_FALSE(events->hasDisplayPresentInfo());
1470 ASSERT_FALSE(events->hasReleaseInfo());
Vishnu Nair083efd32021-02-12 09:32:30 -08001471
Vishnu Nairde66dc72021-06-17 17:54:41 -07001472 // wait for the last transaction to be completed.
1473 adapter.waitForCallback(2);
Vishnu Nair083efd32021-02-12 09:32:30 -08001474
Vishnu Nairde66dc72021-06-17 17:54:41 -07001475 // queue another buffer so we query for frame event deltas
1476 nsecs_t requestedPresentTimeC = 0;
1477 nsecs_t postedTimeC = 0;
1478 setUpAndQueueBuffer(igbProducer, &requestedPresentTimeC, &postedTimeC, &qbOutput, true);
1479 history.applyDelta(qbOutput.frameTimestamps);
1480
1481 // frame number, requestedPresentTime, and postTime should not have changed
1482 ASSERT_EQ(1, events->frameNumber);
1483 ASSERT_EQ(requestedPresentTimeA, events->requestedPresentTime);
1484 ASSERT_GE(events->postedTime, postedTimeA);
1485
1486 // a valid latchtime and pre and post composition info should not be set for the dropped frame
1487 ASSERT_FALSE(events->hasLatchInfo());
1488 ASSERT_FALSE(events->hasDequeueReadyInfo());
1489 ASSERT_FALSE(events->hasGpuCompositionDoneInfo());
1490 ASSERT_FALSE(events->hasDisplayPresentInfo());
1491 ASSERT_FALSE(events->hasReleaseInfo());
1492
1493 // we should also have gotten values for the presented frame
Vishnu Nair083efd32021-02-12 09:32:30 -08001494 events = history.getFrame(2);
1495 ASSERT_NE(nullptr, events);
1496 ASSERT_EQ(2, events->frameNumber);
1497 ASSERT_EQ(requestedPresentTimeB, events->requestedPresentTime);
1498 ASSERT_GE(events->postedTime, postedTimeB);
Vishnu Nairde66dc72021-06-17 17:54:41 -07001499 ASSERT_GE(events->latchTime, postedTimeB);
1500 ASSERT_GE(events->dequeueReadyTime, events->latchTime);
1501 ASSERT_NE(nullptr, events->gpuCompositionDoneFence);
1502 ASSERT_NE(nullptr, events->displayPresentFence);
1503 ASSERT_NE(nullptr, events->releaseFence);
1504
1505 // wait for any callbacks that have not been received
1506 adapter.waitForCallbacks();
Vishnu Nair083efd32021-02-12 09:32:30 -08001507}
1508
Vishnu Nair9a69a042021-06-18 13:19:49 -07001509TEST_F(BLASTFrameEventHistoryTest, FrameEventHistory_CompositorTimings) {
1510 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
1511 sp<IGraphicBufferProducer> igbProducer;
1512 ProducerFrameEventHistory history;
1513 setUpProducer(adapter, igbProducer);
1514
1515 IGraphicBufferProducer::QueueBufferOutput qbOutput;
1516 nsecs_t requestedPresentTimeA = 0;
1517 nsecs_t postedTimeA = 0;
Vishnu Nair9a69a042021-06-18 13:19:49 -07001518 setUpAndQueueBuffer(igbProducer, &requestedPresentTimeA, &postedTimeA, &qbOutput, true);
1519 history.applyDelta(qbOutput.frameTimestamps);
1520 adapter.waitForCallback(1);
1521
1522 // queue another buffer so we query for frame event deltas
1523 nsecs_t requestedPresentTimeB = 0;
1524 nsecs_t postedTimeB = 0;
1525 setUpAndQueueBuffer(igbProducer, &requestedPresentTimeB, &postedTimeB, &qbOutput, true);
1526 history.applyDelta(qbOutput.frameTimestamps);
1527
1528 // check for a valid compositor deadline
1529 ASSERT_NE(0, history.getReportedCompositeDeadline());
1530
1531 // wait for any callbacks that have not been received
1532 adapter.waitForCallbacks();
1533}
1534
Valerie Hauc5011f92019-10-11 09:52:07 -07001535} // namespace android