blob: cce28926ea5feca93fd2c8c2f9aca0c049ac77e2 [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>
Huihong Luo3bdef862022-03-03 11:57:19 -080022#include <gui/AidlStatusUtil.h>
Valerie Haud3b90d22019-11-06 09:37:31 -080023#include <gui/BufferQueueCore.h>
24#include <gui/BufferQueueProducer.h>
Valerie Hau871d6352020-01-29 08:44:02 -080025#include <gui/FrameTimestamps.h>
Valerie Hauda3446e2019-10-14 15:49:22 -070026#include <gui/IGraphicBufferProducer.h>
27#include <gui/IProducerListener.h>
Vishnu Nair17dde612020-12-28 11:39:59 -080028#include <gui/Surface.h>
Valerie Hauc5011f92019-10-11 09:52:07 -070029#include <gui/SurfaceComposerClient.h>
chaviwe7b9f272020-08-18 16:08:59 -070030#include <gui/SyncScreenCaptureListener.h>
chaviwd7deef72021-10-06 11:53:40 -050031#include <gui/test/CallbackUtils.h>
Valerie Hauda3446e2019-10-14 15:49:22 -070032#include <private/gui/ComposerService.h>
Huihong Luo9e84f332021-12-16 14:33:46 -080033#include <private/gui/ComposerServiceAIDL.h>
Marin Shalamanova7fe3042021-01-29 21:02:08 +010034#include <ui/DisplayMode.h>
Vishnu Nair5b5f6932023-04-12 16:28:19 -070035#include <ui/DisplayState.h>
Valerie Hauc5011f92019-10-11 09:52:07 -070036#include <ui/GraphicBuffer.h>
Valerie Hauda3446e2019-10-14 15:49:22 -070037#include <ui/GraphicTypes.h>
Valerie Hau8cee3f92019-11-06 10:06:28 -080038#include <ui/Transform.h>
Valerie Hauc5011f92019-10-11 09:52:07 -070039
40#include <gtest/gtest.h>
41
42using namespace std::chrono_literals;
43
44namespace android {
45
Valerie Hauc5011f92019-10-11 09:52:07 -070046using Transaction = SurfaceComposerClient::Transaction;
Valerie Hauda3446e2019-10-14 15:49:22 -070047using android::hardware::graphics::common::V1_2::BufferUsage;
Valerie Hauc5011f92019-10-11 09:52:07 -070048
chaviwd7deef72021-10-06 11:53:40 -050049class CountProducerListener : public BnProducerListener {
50public:
51 void onBufferReleased() override {
52 std::scoped_lock<std::mutex> lock(mMutex);
53 mNumReleased++;
54 mReleaseCallback.notify_one();
55 }
56
57 void waitOnNumberReleased(int32_t expectedNumReleased) {
58 std::unique_lock<std::mutex> lock(mMutex);
59 while (mNumReleased < expectedNumReleased) {
60 ASSERT_NE(mReleaseCallback.wait_for(lock, std::chrono::seconds(3)),
61 std::cv_status::timeout)
62 << "did not receive release";
63 }
64 }
65
66private:
67 std::mutex mMutex;
68 std::condition_variable mReleaseCallback;
69 int32_t mNumReleased GUARDED_BY(mMutex) = 0;
70};
71
chaviwf10b9042021-10-13 15:48:59 -050072class TestBLASTBufferQueue : public BLASTBufferQueue {
73public:
74 TestBLASTBufferQueue(const std::string& name, const sp<SurfaceControl>& surface, int width,
75 int height, int32_t format)
76 : BLASTBufferQueue(name, surface, width, height, format) {}
77
chaviw6b9ffea2021-11-08 09:25:48 -060078 void transactionCallback(nsecs_t latchTime, const sp<Fence>& presentFence,
79 const std::vector<SurfaceControlStats>& stats) override {
80 BLASTBufferQueue::transactionCallback(latchTime, presentFence, stats);
chaviwf10b9042021-10-13 15:48:59 -050081 uint64_t frameNumber = stats[0].frameEventStats.frameNumber;
82
83 {
84 std::unique_lock lock{frameNumberMutex};
chaviw6b9ffea2021-11-08 09:25:48 -060085 mLastTransactionFrameNumber = frameNumber;
86 mWaitForCallbackCV.notify_all();
chaviwf10b9042021-10-13 15:48:59 -050087 }
88 }
89
90 void waitForCallback(int64_t frameNumber) {
91 std::unique_lock lock{frameNumberMutex};
92 // Wait until all but one of the submitted buffers have been released.
chaviw6b9ffea2021-11-08 09:25:48 -060093 while (mLastTransactionFrameNumber < frameNumber) {
94 mWaitForCallbackCV.wait(lock);
chaviwf10b9042021-10-13 15:48:59 -050095 }
96 }
97
98private:
99 std::mutex frameNumberMutex;
chaviw6b9ffea2021-11-08 09:25:48 -0600100 std::condition_variable mWaitForCallbackCV;
101 int64_t mLastTransactionFrameNumber = -1;
chaviwf10b9042021-10-13 15:48:59 -0500102};
103
Valerie Hauc5011f92019-10-11 09:52:07 -0700104class BLASTBufferQueueHelper {
105public:
106 BLASTBufferQueueHelper(const sp<SurfaceControl>& sc, int width, int height) {
chaviwf10b9042021-10-13 15:48:59 -0500107 mBlastBufferQueueAdapter = new TestBLASTBufferQueue("TestBLASTBufferQueue", sc, width,
108 height, PIXEL_FORMAT_RGBA_8888);
Valerie Hauc5011f92019-10-11 09:52:07 -0700109 }
110
111 void update(const sp<SurfaceControl>& sc, int width, int height) {
chaviw565ee542021-01-14 10:21:23 -0800112 mBlastBufferQueueAdapter->update(sc, width, height, PIXEL_FORMAT_RGBA_8888);
Valerie Hauc5011f92019-10-11 09:52:07 -0700113 }
114
Tianhao Yao4861b102022-02-03 20:18:35 +0000115 void setSyncTransaction(Transaction& next, bool acquireSingleBuffer = true) {
116 auto callback = [&next](Transaction* t) { next.merge(std::move(*t)); };
117 mBlastBufferQueueAdapter->syncNextTransaction(callback, acquireSingleBuffer);
118 }
119
Chavi Weingartenc398c012023-04-12 17:26:02 +0000120 bool syncNextTransaction(std::function<void(Transaction*)> callback,
Tianhao Yao4861b102022-02-03 20:18:35 +0000121 bool acquireSingleBuffer = true) {
Chavi Weingartenc398c012023-04-12 17:26:02 +0000122 return mBlastBufferQueueAdapter->syncNextTransaction(callback, acquireSingleBuffer);
Tianhao Yao4861b102022-02-03 20:18:35 +0000123 }
124
125 void stopContinuousSyncTransaction() {
126 mBlastBufferQueueAdapter->stopContinuousSyncTransaction();
Valerie Hauc5011f92019-10-11 09:52:07 -0700127 }
128
Chavi Weingartenc398c012023-04-12 17:26:02 +0000129 void clearSyncTransaction() { mBlastBufferQueueAdapter->clearSyncTransaction(); }
130
Vishnu Nairea0de002020-11-17 17:42:37 -0800131 int getWidth() { return mBlastBufferQueueAdapter->mSize.width; }
Valerie Hauda3446e2019-10-14 15:49:22 -0700132
Vishnu Nairea0de002020-11-17 17:42:37 -0800133 int getHeight() { return mBlastBufferQueueAdapter->mSize.height; }
Valerie Hauda3446e2019-10-14 15:49:22 -0700134
Tianhao Yao4861b102022-02-03 20:18:35 +0000135 std::function<void(Transaction*)> getTransactionReadyCallback() {
136 return mBlastBufferQueueAdapter->mTransactionReadyCallback;
137 }
Valerie Hauda3446e2019-10-14 15:49:22 -0700138
139 sp<IGraphicBufferProducer> getIGraphicBufferProducer() {
140 return mBlastBufferQueueAdapter->getIGraphicBufferProducer();
141 }
142
Valerie Hauc5011f92019-10-11 09:52:07 -0700143 const sp<SurfaceControl> getSurfaceControl() {
144 return mBlastBufferQueueAdapter->mSurfaceControl;
145 }
146
Vishnu Naira4fbca52021-07-07 16:52:34 -0700147 sp<Surface> getSurface() {
148 return mBlastBufferQueueAdapter->getSurface(false /* includeSurfaceControlHandle */);
149 }
150
Valerie Haud3b90d22019-11-06 09:37:31 -0800151 void waitForCallbacks() {
Valerie Hauda3446e2019-10-14 15:49:22 -0700152 std::unique_lock lock{mBlastBufferQueueAdapter->mMutex};
Vishnu Nair1506b182021-02-22 14:35:15 -0800153 // Wait until all but one of the submitted buffers have been released.
154 while (mBlastBufferQueueAdapter->mSubmitted.size() > 1) {
Valerie Haud3b90d22019-11-06 09:37:31 -0800155 mBlastBufferQueueAdapter->mCallbackCV.wait(lock);
156 }
Valerie Hauda3446e2019-10-14 15:49:22 -0700157 }
158
Vishnu Nair1506b182021-02-22 14:35:15 -0800159 void waitForCallback(int64_t frameNumber) {
chaviwf10b9042021-10-13 15:48:59 -0500160 mBlastBufferQueueAdapter->waitForCallback(frameNumber);
Vishnu Nair1506b182021-02-22 14:35:15 -0800161 }
162
chaviw0acd33a2021-11-02 11:55:37 -0500163 void validateNumFramesSubmitted(int64_t numFramesSubmitted) {
164 std::unique_lock lock{mBlastBufferQueueAdapter->mMutex};
165 ASSERT_EQ(numFramesSubmitted, mBlastBufferQueueAdapter->mSubmitted.size());
166 }
167
chaviwc1cf4022022-06-03 13:32:33 -0500168 void mergeWithNextTransaction(Transaction* merge, uint64_t frameNumber) {
169 mBlastBufferQueueAdapter->mergeWithNextTransaction(merge, frameNumber);
170 }
171
Valerie Hauc5011f92019-10-11 09:52:07 -0700172private:
chaviwf10b9042021-10-13 15:48:59 -0500173 sp<TestBLASTBufferQueue> mBlastBufferQueueAdapter;
Valerie Hauc5011f92019-10-11 09:52:07 -0700174};
175
176class BLASTBufferQueueTest : public ::testing::Test {
177public:
178protected:
Valerie Hauc5011f92019-10-11 09:52:07 -0700179 void SetUp() {
Valerie Hauda3446e2019-10-14 15:49:22 -0700180 mComposer = ComposerService::getComposerService();
Valerie Hauc5011f92019-10-11 09:52:07 -0700181 mClient = new SurfaceComposerClient();
Huihong Luo31b5ac22022-08-15 20:38:10 -0700182 const auto ids = SurfaceComposerClient::getPhysicalDisplayIds();
183 ASSERT_FALSE(ids.empty());
184 // display 0 is picked as this test is not much display depedent
185 mDisplayToken = SurfaceComposerClient::getPhysicalDisplayToken(ids.front());
Valerie Hauda3446e2019-10-14 15:49:22 -0700186 ASSERT_NE(nullptr, mDisplayToken.get());
187 Transaction t;
Dominik Laskowski29fa1462021-04-27 15:51:50 -0700188 t.setDisplayLayerStack(mDisplayToken, ui::DEFAULT_LAYER_STACK);
Valerie Hauda3446e2019-10-14 15:49:22 -0700189 t.apply();
190 t.clear();
191
Vishnu Nair5b5f6932023-04-12 16:28:19 -0700192 ui::DisplayState displayState;
193 ASSERT_EQ(NO_ERROR, SurfaceComposerClient::getDisplayState(mDisplayToken, &displayState));
194 const ui::Size& resolution = displayState.layerStackSpaceRect;
Dominik Laskowski3cb3d4e2019-11-21 11:14:45 -0800195 mDisplayWidth = resolution.getWidth();
196 mDisplayHeight = resolution.getHeight();
Nolan Scobief50aebc2023-04-13 17:49:18 +0000197 ALOGD("Display: %dx%d orientation:%d", mDisplayWidth, mDisplayHeight,
Vishnu Nair5b5f6932023-04-12 16:28:19 -0700198 displayState.orientation);
Valerie Hauda3446e2019-10-14 15:49:22 -0700199
200 mSurfaceControl = mClient->createSurface(String8("TestSurface"), mDisplayWidth,
201 mDisplayHeight, PIXEL_FORMAT_RGBA_8888,
202 ISurfaceComposerClient::eFXSurfaceBufferState,
203 /*parent*/ nullptr);
Dominik Laskowski29fa1462021-04-27 15:51:50 -0700204 t.setLayerStack(mSurfaceControl, ui::DEFAULT_LAYER_STACK)
Valerie Hauda3446e2019-10-14 15:49:22 -0700205 .setLayer(mSurfaceControl, std::numeric_limits<int32_t>::max())
Valerie Hauda3446e2019-10-14 15:49:22 -0700206 .show(mSurfaceControl)
207 .setDataspace(mSurfaceControl, ui::Dataspace::V0_SRGB)
208 .apply();
chaviwd2432892020-07-24 17:42:39 -0700209
210 mCaptureArgs.displayToken = mDisplayToken;
arthurhung6fa58b72020-11-05 11:56:00 +0800211 mCaptureArgs.dataspace = ui::Dataspace::V0_SRGB;
Valerie Hauda3446e2019-10-14 15:49:22 -0700212 }
213
chaviwd7deef72021-10-06 11:53:40 -0500214 void setUpProducer(BLASTBufferQueueHelper& adapter, sp<IGraphicBufferProducer>& producer,
215 int32_t maxBufferCount = 2) {
Vishnu Nair083efd32021-02-12 09:32:30 -0800216 producer = adapter.getIGraphicBufferProducer();
chaviwd7deef72021-10-06 11:53:40 -0500217 setUpProducer(producer, maxBufferCount);
Vishnu Nair083efd32021-02-12 09:32:30 -0800218 }
219
chaviwd7deef72021-10-06 11:53:40 -0500220 void setUpProducer(sp<IGraphicBufferProducer>& igbProducer, int32_t maxBufferCount) {
Valerie Haud3b90d22019-11-06 09:37:31 -0800221 ASSERT_NE(nullptr, igbProducer.get());
chaviwd7deef72021-10-06 11:53:40 -0500222 ASSERT_EQ(NO_ERROR, igbProducer->setMaxDequeuedBufferCount(maxBufferCount));
Valerie Haud3b90d22019-11-06 09:37:31 -0800223 IGraphicBufferProducer::QueueBufferOutput qbOutput;
chaviwd7deef72021-10-06 11:53:40 -0500224 mProducerListener = new CountProducerListener();
Valerie Haud3b90d22019-11-06 09:37:31 -0800225 ASSERT_EQ(NO_ERROR,
chaviwd7deef72021-10-06 11:53:40 -0500226 igbProducer->connect(mProducerListener, NATIVE_WINDOW_API_CPU, false, &qbOutput));
Dominik Laskowski718f9602019-11-09 20:01:35 -0800227 ASSERT_NE(ui::Transform::ROT_INVALID, qbOutput.transformHint);
Valerie Haud3b90d22019-11-06 09:37:31 -0800228 }
229
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800230 void fillBuffer(uint32_t* bufData, Rect rect, uint32_t stride, uint8_t r, uint8_t g,
231 uint8_t b) {
232 for (uint32_t row = rect.top; row < rect.bottom; row++) {
233 for (uint32_t col = rect.left; col < rect.right; col++) {
Valerie Hauda3446e2019-10-14 15:49:22 -0700234 uint8_t* pixel = (uint8_t*)(bufData + (row * stride) + col);
235 *pixel = r;
236 *(pixel + 1) = g;
237 *(pixel + 2) = b;
238 *(pixel + 3) = 255;
239 }
240 }
241 }
242
Valerie Hau5977fc82019-12-05 15:56:39 -0800243 void fillQuadrants(sp<GraphicBuffer>& buf) {
244 const auto bufWidth = buf->getWidth();
245 const auto bufHeight = buf->getHeight();
246 uint32_t* bufData;
247 buf->lock(static_cast<uint32_t>(GraphicBuffer::USAGE_SW_WRITE_OFTEN),
248 reinterpret_cast<void**>(&bufData));
249 fillBuffer(bufData, Rect(0, 0, bufWidth / 2, bufHeight / 2), buf->getStride(), 0, 0, 0);
250 fillBuffer(bufData, Rect(bufWidth / 2, 0, bufWidth, bufHeight / 2), buf->getStride(), 255,
251 0, 0);
252 fillBuffer(bufData, Rect(bufWidth / 2, bufHeight / 2, bufWidth, bufHeight),
253 buf->getStride(), 0, 255, 0);
254 fillBuffer(bufData, Rect(0, bufHeight / 2, bufWidth / 2, bufHeight), buf->getStride(), 0, 0,
255 255);
256 buf->unlock();
257 }
258
259 void checkScreenCapture(uint8_t r, uint8_t g, uint8_t b, Rect region, int32_t border = 0,
260 bool outsideRegion = false) {
chaviwd2432892020-07-24 17:42:39 -0700261 sp<GraphicBuffer>& captureBuf = mCaptureResults.buffer;
Valerie Hau5977fc82019-12-05 15:56:39 -0800262 const auto epsilon = 3;
chaviwd2432892020-07-24 17:42:39 -0700263 const auto width = captureBuf->getWidth();
264 const auto height = captureBuf->getHeight();
265 const auto stride = captureBuf->getStride();
Valerie Hauda3446e2019-10-14 15:49:22 -0700266
267 uint32_t* bufData;
chaviwd2432892020-07-24 17:42:39 -0700268 captureBuf->lock(static_cast<uint32_t>(GraphicBuffer::USAGE_SW_READ_OFTEN),
269 reinterpret_cast<void**>(&bufData));
Valerie Hauda3446e2019-10-14 15:49:22 -0700270
271 for (uint32_t row = 0; row < height; row++) {
272 for (uint32_t col = 0; col < width; col++) {
273 uint8_t* pixel = (uint8_t*)(bufData + (row * stride) + col);
arthurhung6fa58b72020-11-05 11:56:00 +0800274 ASSERT_NE(nullptr, pixel);
Valerie Hau5977fc82019-12-05 15:56:39 -0800275 bool inRegion;
276 if (!outsideRegion) {
277 inRegion = row >= region.top + border && row < region.bottom - border &&
278 col >= region.left + border && col < region.right - border;
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800279 } else {
Valerie Hau5977fc82019-12-05 15:56:39 -0800280 inRegion = row >= region.top - border && row < region.bottom + border &&
281 col >= region.left - border && col < region.right + border;
282 }
283 if (!outsideRegion && inRegion) {
Chavi Weingartena5aedbd2021-04-09 13:37:33 +0000284 ASSERT_GE(epsilon, abs(r - *(pixel)));
285 ASSERT_GE(epsilon, abs(g - *(pixel + 1)));
286 ASSERT_GE(epsilon, abs(b - *(pixel + 2)));
Valerie Hau5977fc82019-12-05 15:56:39 -0800287 } else if (outsideRegion && !inRegion) {
Chavi Weingartena5aedbd2021-04-09 13:37:33 +0000288 ASSERT_GE(epsilon, abs(r - *(pixel)));
289 ASSERT_GE(epsilon, abs(g - *(pixel + 1)));
290 ASSERT_GE(epsilon, abs(b - *(pixel + 2)));
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800291 }
Vishnu Nair1506b182021-02-22 14:35:15 -0800292 ASSERT_EQ(false, ::testing::Test::HasFailure());
Valerie Hauda3446e2019-10-14 15:49:22 -0700293 }
294 }
chaviwd2432892020-07-24 17:42:39 -0700295 captureBuf->unlock();
Valerie Hauc5011f92019-10-11 09:52:07 -0700296 }
297
chaviw8ffc7b82020-08-18 11:25:37 -0700298 static status_t captureDisplay(DisplayCaptureArgs& captureArgs,
299 ScreenCaptureResults& captureResults) {
Huihong Luo9e84f332021-12-16 14:33:46 -0800300 const auto sf = ComposerServiceAIDL::getComposerService();
chaviw8ffc7b82020-08-18 11:25:37 -0700301 SurfaceComposerClient::Transaction().apply(true);
302
303 const sp<SyncScreenCaptureListener> captureListener = new SyncScreenCaptureListener();
Huihong Luo9e84f332021-12-16 14:33:46 -0800304 binder::Status status = sf->captureDisplay(captureArgs, captureListener);
Huihong Luo3bdef862022-03-03 11:57:19 -0800305 status_t err = gui::aidl_utils::statusTFromBinderStatus(status);
306 if (err != NO_ERROR) {
307 return err;
chaviw8ffc7b82020-08-18 11:25:37 -0700308 }
309 captureResults = captureListener->waitForResults();
Patrick Williamsfdb57bb2022-08-09 22:48:18 +0000310 return fenceStatus(captureResults.fenceResult);
chaviw8ffc7b82020-08-18 11:25:37 -0700311 }
312
Vishnu Nair277142c2021-01-05 18:35:29 -0800313 void queueBuffer(sp<IGraphicBufferProducer> igbp, uint8_t r, uint8_t g, uint8_t b,
314 nsecs_t presentTimeDelay) {
315 int slot;
316 sp<Fence> fence;
317 sp<GraphicBuffer> buf;
318 auto ret = igbp->dequeueBuffer(&slot, &fence, mDisplayWidth, mDisplayHeight,
319 PIXEL_FORMAT_RGBA_8888, GRALLOC_USAGE_SW_WRITE_OFTEN,
320 nullptr, nullptr);
chaviw0acd33a2021-11-02 11:55:37 -0500321 ASSERT_TRUE(ret == IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION || ret == NO_ERROR);
Vishnu Nair277142c2021-01-05 18:35:29 -0800322 ASSERT_EQ(OK, igbp->requestBuffer(slot, &buf));
323
324 uint32_t* bufData;
325 buf->lock(static_cast<uint32_t>(GraphicBuffer::USAGE_SW_WRITE_OFTEN),
326 reinterpret_cast<void**>(&bufData));
327 fillBuffer(bufData, Rect(buf->getWidth(), buf->getHeight() / 2), buf->getStride(), r, g, b);
328 buf->unlock();
329
330 IGraphicBufferProducer::QueueBufferOutput qbOutput;
331 nsecs_t timestampNanos = systemTime() + presentTimeDelay;
332 IGraphicBufferProducer::QueueBufferInput input(timestampNanos, false, HAL_DATASPACE_UNKNOWN,
333 Rect(mDisplayWidth, mDisplayHeight / 2),
334 NATIVE_WINDOW_SCALING_MODE_FREEZE, 0,
335 Fence::NO_FENCE);
336 igbp->queueBuffer(slot, input, &qbOutput);
337 }
338
Valerie Hauc5011f92019-10-11 09:52:07 -0700339 sp<SurfaceComposerClient> mClient;
Valerie Hauda3446e2019-10-14 15:49:22 -0700340 sp<ISurfaceComposer> mComposer;
341
342 sp<IBinder> mDisplayToken;
343
Valerie Hauc5011f92019-10-11 09:52:07 -0700344 sp<SurfaceControl> mSurfaceControl;
Valerie Hauda3446e2019-10-14 15:49:22 -0700345
346 uint32_t mDisplayWidth;
347 uint32_t mDisplayHeight;
chaviwd2432892020-07-24 17:42:39 -0700348
349 DisplayCaptureArgs mCaptureArgs;
350 ScreenCaptureResults mCaptureResults;
chaviwd7deef72021-10-06 11:53:40 -0500351 sp<CountProducerListener> mProducerListener;
Valerie Hauc5011f92019-10-11 09:52:07 -0700352};
353
Patrick Williamsed4745d2023-07-27 17:35:19 -0500354// Helper class to ensure the provided BBQ frame number has been committed in SurfaceFlinger.
355class BBQSyncHelper {
356public:
357 BBQSyncHelper(BLASTBufferQueueHelper& bbqHelper, uint64_t frameNumber) {
358 t.addTransactionCompletedCallback(callbackHelper.function, callbackHelper.getContext());
359 bbqHelper.mergeWithNextTransaction(&t, frameNumber);
360 }
361
362 void wait() {
363 CallbackData callbackData;
364 callbackHelper.getCallbackData(&callbackData);
365 }
366
367private:
368 Transaction t;
369 CallbackHelper callbackHelper;
370};
371
Valerie Hauc5011f92019-10-11 09:52:07 -0700372TEST_F(BLASTBufferQueueTest, CreateBLASTBufferQueue) {
373 // create BLASTBufferQueue adapter associated with this surface
Valerie Hauda3446e2019-10-14 15:49:22 -0700374 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
Valerie Hauc5011f92019-10-11 09:52:07 -0700375 ASSERT_EQ(mSurfaceControl, adapter.getSurfaceControl());
Valerie Hauda3446e2019-10-14 15:49:22 -0700376 ASSERT_EQ(mDisplayWidth, adapter.getWidth());
377 ASSERT_EQ(mDisplayHeight, adapter.getHeight());
Tianhao Yao4861b102022-02-03 20:18:35 +0000378 ASSERT_EQ(nullptr, adapter.getTransactionReadyCallback());
Valerie Hauc5011f92019-10-11 09:52:07 -0700379}
380
381TEST_F(BLASTBufferQueueTest, Update) {
Valerie Hauda3446e2019-10-14 15:49:22 -0700382 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
Valerie Hauc5011f92019-10-11 09:52:07 -0700383 sp<SurfaceControl> updateSurface =
Valerie Hauda3446e2019-10-14 15:49:22 -0700384 mClient->createSurface(String8("UpdateTest"), mDisplayWidth / 2, mDisplayHeight / 2,
385 PIXEL_FORMAT_RGBA_8888);
386 adapter.update(updateSurface, mDisplayWidth / 2, mDisplayHeight / 2);
Valerie Hauc5011f92019-10-11 09:52:07 -0700387 ASSERT_EQ(updateSurface, adapter.getSurfaceControl());
Vishnu Nairea0de002020-11-17 17:42:37 -0800388 sp<IGraphicBufferProducer> igbProducer;
389 setUpProducer(adapter, igbProducer);
390
391 int32_t width;
392 igbProducer->query(NATIVE_WINDOW_WIDTH, &width);
393 ASSERT_EQ(mDisplayWidth / 2, width);
394 int32_t height;
395 igbProducer->query(NATIVE_WINDOW_HEIGHT, &height);
396 ASSERT_EQ(mDisplayHeight / 2, height);
Valerie Hauc5011f92019-10-11 09:52:07 -0700397}
398
Tianhao Yao4861b102022-02-03 20:18:35 +0000399TEST_F(BLASTBufferQueueTest, SyncNextTransaction) {
Valerie Hauda3446e2019-10-14 15:49:22 -0700400 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
Tianhao Yao4861b102022-02-03 20:18:35 +0000401 ASSERT_EQ(nullptr, adapter.getTransactionReadyCallback());
402 auto callback = [](Transaction*) {};
403 adapter.syncNextTransaction(callback);
404 ASSERT_NE(nullptr, adapter.getTransactionReadyCallback());
Valerie Hauc5011f92019-10-11 09:52:07 -0700405}
Valerie Hauda3446e2019-10-14 15:49:22 -0700406
Valerie Haubf29e042020-02-06 11:40:38 -0800407TEST_F(BLASTBufferQueueTest, DISABLED_onFrameAvailable_ApplyDesiredPresentTime) {
Valerie Hau181abd32020-01-27 14:18:28 -0800408 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
409 sp<IGraphicBufferProducer> igbProducer;
410 setUpProducer(adapter, igbProducer);
411
412 int slot;
413 sp<Fence> fence;
414 sp<GraphicBuffer> buf;
415 auto ret = igbProducer->dequeueBuffer(&slot, &fence, mDisplayWidth, mDisplayHeight,
416 PIXEL_FORMAT_RGBA_8888, GRALLOC_USAGE_SW_WRITE_OFTEN,
417 nullptr, nullptr);
418 ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, ret);
419 ASSERT_EQ(OK, igbProducer->requestBuffer(slot, &buf));
420
421 nsecs_t desiredPresentTime = systemTime() + nsecs_t(5 * 1e8);
422 IGraphicBufferProducer::QueueBufferOutput qbOutput;
Vishnu Nair1506b182021-02-22 14:35:15 -0800423 IGraphicBufferProducer::QueueBufferInput input(desiredPresentTime, true /* autotimestamp */,
424 HAL_DATASPACE_UNKNOWN,
Valerie Hau181abd32020-01-27 14:18:28 -0800425 Rect(mDisplayWidth, mDisplayHeight),
426 NATIVE_WINDOW_SCALING_MODE_FREEZE, 0,
427 Fence::NO_FENCE);
428 igbProducer->queueBuffer(slot, input, &qbOutput);
429 ASSERT_NE(ui::Transform::ROT_INVALID, qbOutput.transformHint);
430
431 adapter.waitForCallbacks();
432 ASSERT_GE(systemTime(), desiredPresentTime);
433}
434
Valerie Hauda3446e2019-10-14 15:49:22 -0700435TEST_F(BLASTBufferQueueTest, onFrameAvailable_Apply) {
436 uint8_t r = 255;
437 uint8_t g = 0;
438 uint8_t b = 0;
439
440 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
Valerie Haud3b90d22019-11-06 09:37:31 -0800441 sp<IGraphicBufferProducer> igbProducer;
442 setUpProducer(adapter, igbProducer);
Patrick Williamsed4745d2023-07-27 17:35:19 -0500443 BBQSyncHelper syncHelper{adapter, 1};
Valerie Hauda3446e2019-10-14 15:49:22 -0700444
445 int slot;
446 sp<Fence> fence;
447 sp<GraphicBuffer> buf;
448 auto ret = igbProducer->dequeueBuffer(&slot, &fence, mDisplayWidth, mDisplayHeight,
449 PIXEL_FORMAT_RGBA_8888, GRALLOC_USAGE_SW_WRITE_OFTEN,
450 nullptr, nullptr);
451 ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, ret);
452 ASSERT_EQ(OK, igbProducer->requestBuffer(slot, &buf));
453
454 uint32_t* bufData;
455 buf->lock(static_cast<uint32_t>(GraphicBuffer::USAGE_SW_WRITE_OFTEN),
456 reinterpret_cast<void**>(&bufData));
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800457 fillBuffer(bufData, Rect(buf->getWidth(), buf->getHeight()), buf->getStride(), r, g, b);
Valerie Hauda3446e2019-10-14 15:49:22 -0700458 buf->unlock();
459
Valerie Haud3b90d22019-11-06 09:37:31 -0800460 IGraphicBufferProducer::QueueBufferOutput qbOutput;
Vishnu Nair1506b182021-02-22 14:35:15 -0800461 IGraphicBufferProducer::QueueBufferInput input(systemTime(), true /* autotimestamp */,
462 HAL_DATASPACE_UNKNOWN,
Valerie Hauda3446e2019-10-14 15:49:22 -0700463 Rect(mDisplayWidth, mDisplayHeight),
464 NATIVE_WINDOW_SCALING_MODE_FREEZE, 0,
465 Fence::NO_FENCE);
466 igbProducer->queueBuffer(slot, input, &qbOutput);
Dominik Laskowski718f9602019-11-09 20:01:35 -0800467 ASSERT_NE(ui::Transform::ROT_INVALID, qbOutput.transformHint);
Valerie Hauda3446e2019-10-14 15:49:22 -0700468
Patrick Williamsed4745d2023-07-27 17:35:19 -0500469 syncHelper.wait();
Valerie Hauda3446e2019-10-14 15:49:22 -0700470
471 // capture screen and verify that it is red
chaviw8ffc7b82020-08-18 11:25:37 -0700472 ASSERT_EQ(NO_ERROR, captureDisplay(mCaptureArgs, mCaptureResults));
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800473 ASSERT_NO_FATAL_FAILURE(
474 checkScreenCapture(r, g, b, {0, 0, (int32_t)mDisplayWidth, (int32_t)mDisplayHeight}));
Valerie Hauda3446e2019-10-14 15:49:22 -0700475}
Valerie Haud3b90d22019-11-06 09:37:31 -0800476
477TEST_F(BLASTBufferQueueTest, TripleBuffering) {
478 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
479 sp<IGraphicBufferProducer> igbProducer;
480 setUpProducer(adapter, igbProducer);
481
482 std::vector<std::pair<int, sp<Fence>>> allocated;
Ady Abraham0bde6b52021-05-18 13:57:02 -0700483 int minUndequeuedBuffers = 0;
484 ASSERT_EQ(OK, igbProducer->query(NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS, &minUndequeuedBuffers));
485 const auto bufferCount = minUndequeuedBuffers + 2;
486
487 for (int i = 0; i < bufferCount; i++) {
Valerie Haud3b90d22019-11-06 09:37:31 -0800488 int slot;
489 sp<Fence> fence;
490 sp<GraphicBuffer> buf;
491 auto ret = igbProducer->dequeueBuffer(&slot, &fence, mDisplayWidth, mDisplayHeight,
492 PIXEL_FORMAT_RGBA_8888, GRALLOC_USAGE_SW_WRITE_OFTEN,
493 nullptr, nullptr);
494 ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, ret);
495 ASSERT_EQ(OK, igbProducer->requestBuffer(slot, &buf));
496 allocated.push_back({slot, fence});
497 }
498 for (int i = 0; i < allocated.size(); i++) {
499 igbProducer->cancelBuffer(allocated[i].first, allocated[i].second);
500 }
501
Valerie Haua32c5522019-12-09 10:11:08 -0800502 for (int i = 0; i < 100; i++) {
Valerie Haud3b90d22019-11-06 09:37:31 -0800503 int slot;
504 sp<Fence> fence;
505 sp<GraphicBuffer> buf;
506 auto ret = igbProducer->dequeueBuffer(&slot, &fence, mDisplayWidth, mDisplayHeight,
507 PIXEL_FORMAT_RGBA_8888, GRALLOC_USAGE_SW_WRITE_OFTEN,
508 nullptr, nullptr);
509 ASSERT_EQ(NO_ERROR, ret);
510 IGraphicBufferProducer::QueueBufferOutput qbOutput;
Vishnu Nair1506b182021-02-22 14:35:15 -0800511 IGraphicBufferProducer::QueueBufferInput input(systemTime(), true /* autotimestamp */,
512 HAL_DATASPACE_UNKNOWN,
Valerie Haud3b90d22019-11-06 09:37:31 -0800513 Rect(mDisplayWidth, mDisplayHeight),
514 NATIVE_WINDOW_SCALING_MODE_FREEZE, 0,
515 Fence::NO_FENCE);
516 igbProducer->queueBuffer(slot, input, &qbOutput);
517 }
518 adapter.waitForCallbacks();
519}
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800520
521TEST_F(BLASTBufferQueueTest, SetCrop_Item) {
522 uint8_t r = 255;
523 uint8_t g = 0;
524 uint8_t b = 0;
525
526 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
527 sp<IGraphicBufferProducer> igbProducer;
528 setUpProducer(adapter, igbProducer);
Patrick Williamsed4745d2023-07-27 17:35:19 -0500529 BBQSyncHelper syncHelper{adapter, 1};
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800530 int slot;
531 sp<Fence> fence;
532 sp<GraphicBuffer> buf;
533 auto ret = igbProducer->dequeueBuffer(&slot, &fence, mDisplayWidth, mDisplayHeight,
534 PIXEL_FORMAT_RGBA_8888, GRALLOC_USAGE_SW_WRITE_OFTEN,
535 nullptr, nullptr);
536 ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, ret);
537 ASSERT_EQ(OK, igbProducer->requestBuffer(slot, &buf));
538
539 uint32_t* bufData;
540 buf->lock(static_cast<uint32_t>(GraphicBuffer::USAGE_SW_WRITE_OFTEN),
541 reinterpret_cast<void**>(&bufData));
542 fillBuffer(bufData, Rect(buf->getWidth(), buf->getHeight() / 2), buf->getStride(), r, g, b);
543 buf->unlock();
544
545 IGraphicBufferProducer::QueueBufferOutput qbOutput;
Vishnu Nair1506b182021-02-22 14:35:15 -0800546 IGraphicBufferProducer::QueueBufferInput input(systemTime(), true /* autotimestamp */,
547 HAL_DATASPACE_UNKNOWN,
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800548 Rect(mDisplayWidth, mDisplayHeight / 2),
549 NATIVE_WINDOW_SCALING_MODE_FREEZE, 0,
550 Fence::NO_FENCE);
551 igbProducer->queueBuffer(slot, input, &qbOutput);
Dominik Laskowski718f9602019-11-09 20:01:35 -0800552 ASSERT_NE(ui::Transform::ROT_INVALID, qbOutput.transformHint);
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800553
Patrick Williamsed4745d2023-07-27 17:35:19 -0500554 syncHelper.wait();
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800555 // capture screen and verify that it is red
chaviw8ffc7b82020-08-18 11:25:37 -0700556 ASSERT_EQ(NO_ERROR, captureDisplay(mCaptureArgs, mCaptureResults));
chaviwd2432892020-07-24 17:42:39 -0700557
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800558 ASSERT_NO_FATAL_FAILURE(
Chavi Weingartena5aedbd2021-04-09 13:37:33 +0000559 checkScreenCapture(r, g, b,
560 {0, 0, (int32_t)mDisplayWidth, (int32_t)mDisplayHeight / 2}));
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800561}
562
563TEST_F(BLASTBufferQueueTest, SetCrop_ScalingModeScaleCrop) {
564 uint8_t r = 255;
565 uint8_t g = 0;
566 uint8_t b = 0;
567
568 int32_t bufferSideLength =
569 (mDisplayWidth < mDisplayHeight) ? mDisplayWidth / 2 : mDisplayHeight / 2;
570 int32_t finalCropSideLength = bufferSideLength / 2;
571
572 auto bg = mClient->createSurface(String8("BGTest"), 0, 0, PIXEL_FORMAT_RGBA_8888,
Vishnu Nairfa247b12020-02-11 08:58:26 -0800573 ISurfaceComposerClient::eFXSurfaceEffect);
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800574 ASSERT_NE(nullptr, bg.get());
575 Transaction t;
Dominik Laskowski29fa1462021-04-27 15:51:50 -0700576 t.setLayerStack(bg, ui::DEFAULT_LAYER_STACK)
chaviw25714502021-02-11 10:01:08 -0800577 .setCrop(bg, Rect(0, 0, mDisplayWidth, mDisplayHeight))
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800578 .setColor(bg, half3{0, 0, 0})
579 .setLayer(bg, 0)
580 .apply();
581
582 BLASTBufferQueueHelper adapter(mSurfaceControl, bufferSideLength, bufferSideLength);
583 sp<IGraphicBufferProducer> igbProducer;
584 setUpProducer(adapter, igbProducer);
Patrick Williamsed4745d2023-07-27 17:35:19 -0500585 BBQSyncHelper syncHelper{adapter, 1};
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800586 int slot;
587 sp<Fence> fence;
588 sp<GraphicBuffer> buf;
589 auto ret = igbProducer->dequeueBuffer(&slot, &fence, bufferSideLength, bufferSideLength,
590 PIXEL_FORMAT_RGBA_8888, GRALLOC_USAGE_SW_WRITE_OFTEN,
591 nullptr, nullptr);
592 ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, ret);
593 ASSERT_EQ(OK, igbProducer->requestBuffer(slot, &buf));
594
595 uint32_t* bufData;
596 buf->lock(static_cast<uint32_t>(GraphicBuffer::USAGE_SW_WRITE_OFTEN),
597 reinterpret_cast<void**>(&bufData));
598 fillBuffer(bufData, Rect(buf->getWidth(), buf->getHeight()), buf->getStride(), 0, 0, 0);
599 fillBuffer(bufData,
600 Rect(finalCropSideLength / 2, 0, buf->getWidth() - finalCropSideLength / 2,
601 buf->getHeight()),
602 buf->getStride(), r, g, b);
603 buf->unlock();
604
605 IGraphicBufferProducer::QueueBufferOutput qbOutput;
Vishnu Nair1506b182021-02-22 14:35:15 -0800606 IGraphicBufferProducer::QueueBufferInput input(systemTime(), true /* autotimestamp */,
607 HAL_DATASPACE_UNKNOWN,
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800608 Rect(bufferSideLength, finalCropSideLength),
609 NATIVE_WINDOW_SCALING_MODE_SCALE_CROP, 0,
610 Fence::NO_FENCE);
611 igbProducer->queueBuffer(slot, input, &qbOutput);
Dominik Laskowski718f9602019-11-09 20:01:35 -0800612 ASSERT_NE(ui::Transform::ROT_INVALID, qbOutput.transformHint);
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800613
Patrick Williamsed4745d2023-07-27 17:35:19 -0500614 syncHelper.wait();
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800615 // capture screen and verify that it is red
chaviw8ffc7b82020-08-18 11:25:37 -0700616 ASSERT_EQ(NO_ERROR, captureDisplay(mCaptureArgs, mCaptureResults));
Vishnu Nair5cc9ac02021-04-19 13:23:38 -0700617 ASSERT_NO_FATAL_FAILURE(checkScreenCapture(r, g, b,
618 {10, 10, (int32_t)bufferSideLength - 10,
619 (int32_t)bufferSideLength - 10}));
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800620 ASSERT_NO_FATAL_FAILURE(
Vishnu Nair5cc9ac02021-04-19 13:23:38 -0700621 checkScreenCapture(0, 0, 0,
622 {0, 0, (int32_t)bufferSideLength, (int32_t)bufferSideLength},
623 /*border*/ 0, /*outsideRegion*/ true));
624}
625
626TEST_F(BLASTBufferQueueTest, ScaleCroppedBufferToBufferSize) {
627 // add black background
628 auto bg = mClient->createSurface(String8("BGTest"), 0, 0, PIXEL_FORMAT_RGBA_8888,
629 ISurfaceComposerClient::eFXSurfaceEffect);
630 ASSERT_NE(nullptr, bg.get());
631 Transaction t;
Dominik Laskowski29fa1462021-04-27 15:51:50 -0700632 t.setLayerStack(bg, ui::DEFAULT_LAYER_STACK)
Vishnu Nair5cc9ac02021-04-19 13:23:38 -0700633 .setCrop(bg, Rect(0, 0, mDisplayWidth, mDisplayHeight))
634 .setColor(bg, half3{0, 0, 0})
635 .setLayer(bg, 0)
636 .apply();
637
638 Rect windowSize(1000, 1000);
639 Rect bufferSize(windowSize);
640 Rect bufferCrop(200, 200, 700, 700);
641
642 BLASTBufferQueueHelper adapter(mSurfaceControl, windowSize.getWidth(), windowSize.getHeight());
643 sp<IGraphicBufferProducer> igbProducer;
644 setUpProducer(adapter, igbProducer);
Patrick Williamsed4745d2023-07-27 17:35:19 -0500645 BBQSyncHelper syncHelper{adapter, 1};
Vishnu Nair5cc9ac02021-04-19 13:23:38 -0700646 int slot;
647 sp<Fence> fence;
648 sp<GraphicBuffer> buf;
649 auto ret = igbProducer->dequeueBuffer(&slot, &fence, bufferSize.getWidth(),
650 bufferSize.getHeight(), PIXEL_FORMAT_RGBA_8888,
651 GRALLOC_USAGE_SW_WRITE_OFTEN, nullptr, nullptr);
652 ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, ret);
653 ASSERT_EQ(OK, igbProducer->requestBuffer(slot, &buf));
654
655 uint32_t* bufData;
656 buf->lock(static_cast<uint32_t>(GraphicBuffer::USAGE_SW_WRITE_OFTEN),
657 reinterpret_cast<void**>(&bufData));
658 // fill buffer with grey
659 fillBuffer(bufData, bufferSize, buf->getStride(), 127, 127, 127);
660
661 // fill crop area with different colors so we can verify the cropped region has been scaled
662 // correctly.
663 fillBuffer(bufData, Rect(200, 200, 450, 450), buf->getStride(), /* rgb */ 255, 0, 0);
664 fillBuffer(bufData, Rect(200, 451, 450, 700), buf->getStride(), /* rgb */ 0, 255, 0);
665 fillBuffer(bufData, Rect(451, 200, 700, 450), buf->getStride(), /* rgb */ 0, 0, 255);
666 fillBuffer(bufData, Rect(451, 451, 700, 700), buf->getStride(), /* rgb */ 255, 0, 0);
667 buf->unlock();
668
669 IGraphicBufferProducer::QueueBufferOutput qbOutput;
670 IGraphicBufferProducer::QueueBufferInput input(systemTime(), true /* autotimestamp */,
671 HAL_DATASPACE_UNKNOWN,
672 bufferCrop /* Rect::INVALID_RECT */,
673 NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW, 0,
674 Fence::NO_FENCE);
675 igbProducer->queueBuffer(slot, input, &qbOutput);
676 ASSERT_NE(ui::Transform::ROT_INVALID, qbOutput.transformHint);
677
Patrick Williamsed4745d2023-07-27 17:35:19 -0500678 syncHelper.wait();
Vishnu Nair5cc9ac02021-04-19 13:23:38 -0700679
680 ASSERT_EQ(NO_ERROR, captureDisplay(mCaptureArgs, mCaptureResults));
681
682 // Verify cropped region is scaled correctly.
683 ASSERT_NO_FATAL_FAILURE(checkScreenCapture(255, 0, 0, {10, 10, 490, 490}));
684 ASSERT_NO_FATAL_FAILURE(checkScreenCapture(0, 255, 0, {10, 510, 490, 990}));
685 ASSERT_NO_FATAL_FAILURE(checkScreenCapture(0, 0, 255, {510, 10, 990, 490}));
686 ASSERT_NO_FATAL_FAILURE(checkScreenCapture(255, 0, 0, {510, 510, 990, 990}));
687 // Verify outside region is black.
688 ASSERT_NO_FATAL_FAILURE(checkScreenCapture(0, 0, 0,
689 {0, 0, (int32_t)windowSize.getWidth(),
690 (int32_t)windowSize.getHeight()},
691 /*border*/ 0, /*outsideRegion*/ true));
692}
693
694TEST_F(BLASTBufferQueueTest, ScaleCroppedBufferToWindowSize) {
695 // add black background
696 auto bg = mClient->createSurface(String8("BGTest"), 0, 0, PIXEL_FORMAT_RGBA_8888,
697 ISurfaceComposerClient::eFXSurfaceEffect);
698 ASSERT_NE(nullptr, bg.get());
699 Transaction t;
Dominik Laskowski29fa1462021-04-27 15:51:50 -0700700 t.setLayerStack(bg, ui::DEFAULT_LAYER_STACK)
Vishnu Nair5cc9ac02021-04-19 13:23:38 -0700701 .setCrop(bg, Rect(0, 0, mDisplayWidth, mDisplayHeight))
702 .setColor(bg, half3{0, 0, 0})
703 .setLayer(bg, 0)
704 .apply();
705
706 Rect windowSize(1000, 1000);
707 Rect bufferSize(500, 500);
708 Rect bufferCrop(100, 100, 350, 350);
709
710 BLASTBufferQueueHelper adapter(mSurfaceControl, windowSize.getWidth(), windowSize.getHeight());
711 sp<IGraphicBufferProducer> igbProducer;
712 setUpProducer(adapter, igbProducer);
Patrick Williamsed4745d2023-07-27 17:35:19 -0500713 BBQSyncHelper syncHelper{adapter, 1};
Vishnu Nair5cc9ac02021-04-19 13:23:38 -0700714 int slot;
715 sp<Fence> fence;
716 sp<GraphicBuffer> buf;
717 auto ret = igbProducer->dequeueBuffer(&slot, &fence, bufferSize.getWidth(),
718 bufferSize.getHeight(), PIXEL_FORMAT_RGBA_8888,
719 GRALLOC_USAGE_SW_WRITE_OFTEN, nullptr, nullptr);
720 ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, ret);
721 ASSERT_EQ(OK, igbProducer->requestBuffer(slot, &buf));
722
723 uint32_t* bufData;
724 buf->lock(static_cast<uint32_t>(GraphicBuffer::USAGE_SW_WRITE_OFTEN),
725 reinterpret_cast<void**>(&bufData));
726 // fill buffer with grey
727 fillBuffer(bufData, bufferSize, buf->getStride(), 127, 127, 127);
728
729 // fill crop area with different colors so we can verify the cropped region has been scaled
730 // correctly.
731 fillBuffer(bufData, Rect(100, 100, 225, 225), buf->getStride(), /* rgb */ 255, 0, 0);
732 fillBuffer(bufData, Rect(100, 226, 225, 350), buf->getStride(), /* rgb */ 0, 255, 0);
733 fillBuffer(bufData, Rect(226, 100, 350, 225), buf->getStride(), /* rgb */ 0, 0, 255);
734 fillBuffer(bufData, Rect(226, 226, 350, 350), buf->getStride(), /* rgb */ 255, 0, 0);
735 buf->unlock();
736
737 IGraphicBufferProducer::QueueBufferOutput qbOutput;
738 IGraphicBufferProducer::QueueBufferInput input(systemTime(), true /* autotimestamp */,
739 HAL_DATASPACE_UNKNOWN,
740 bufferCrop /* Rect::INVALID_RECT */,
741 NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW, 0,
742 Fence::NO_FENCE);
743 igbProducer->queueBuffer(slot, input, &qbOutput);
744 ASSERT_NE(ui::Transform::ROT_INVALID, qbOutput.transformHint);
745
Patrick Williamsed4745d2023-07-27 17:35:19 -0500746 syncHelper.wait();
Vishnu Nair5cc9ac02021-04-19 13:23:38 -0700747
748 ASSERT_EQ(NO_ERROR, captureDisplay(mCaptureArgs, mCaptureResults));
749 // Verify cropped region is scaled correctly.
750 ASSERT_NO_FATAL_FAILURE(checkScreenCapture(255, 0, 0, {10, 10, 490, 490}));
751 ASSERT_NO_FATAL_FAILURE(checkScreenCapture(0, 255, 0, {10, 510, 490, 990}));
752 ASSERT_NO_FATAL_FAILURE(checkScreenCapture(0, 0, 255, {510, 10, 990, 490}));
753 ASSERT_NO_FATAL_FAILURE(checkScreenCapture(255, 0, 0, {510, 510, 990, 990}));
754 // Verify outside region is black.
755 ASSERT_NO_FATAL_FAILURE(checkScreenCapture(0, 0, 0,
756 {0, 0, (int32_t)windowSize.getWidth(),
757 (int32_t)windowSize.getHeight()},
758 /*border*/ 0, /*outsideRegion*/ true));
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800759}
760
Vishnu Nair932f6ae2021-09-29 17:33:10 -0700761// b/196339769 verify we can can update the requested size while the in FREEZE scaling mode and
762// scale the buffer properly when the mode changes to SCALE_TO_WINDOW
763TEST_F(BLASTBufferQueueTest, ScalingModeChanges) {
764 uint8_t r = 255;
765 uint8_t g = 0;
766 uint8_t b = 0;
767
768 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight / 4);
769 sp<IGraphicBufferProducer> igbProducer;
770 setUpProducer(adapter, igbProducer);
771 {
Patrick Williamsed4745d2023-07-27 17:35:19 -0500772 BBQSyncHelper syncHelper{adapter, 1};
Vishnu Nair932f6ae2021-09-29 17:33:10 -0700773 int slot;
774 sp<Fence> fence;
775 sp<GraphicBuffer> buf;
776 auto ret = igbProducer->dequeueBuffer(&slot, &fence, mDisplayWidth, mDisplayHeight / 4,
777 PIXEL_FORMAT_RGBA_8888, GRALLOC_USAGE_SW_WRITE_OFTEN,
778 nullptr, nullptr);
779 ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, ret);
780 ASSERT_EQ(OK, igbProducer->requestBuffer(slot, &buf));
781
782 uint32_t* bufData;
783 buf->lock(static_cast<uint32_t>(GraphicBuffer::USAGE_SW_WRITE_OFTEN),
784 reinterpret_cast<void**>(&bufData));
785 fillBuffer(bufData, Rect(buf->getWidth(), buf->getHeight()), buf->getStride(), r, g, b);
786 buf->unlock();
787
788 IGraphicBufferProducer::QueueBufferOutput qbOutput;
789 IGraphicBufferProducer::QueueBufferInput input(systemTime(), true /* autotimestamp */,
790 HAL_DATASPACE_UNKNOWN, {},
791 NATIVE_WINDOW_SCALING_MODE_FREEZE, 0,
792 Fence::NO_FENCE);
793 igbProducer->queueBuffer(slot, input, &qbOutput);
Patrick Williamsed4745d2023-07-27 17:35:19 -0500794 syncHelper.wait();
Vishnu Nair932f6ae2021-09-29 17:33:10 -0700795 }
796 // capture screen and verify that it is red
797 ASSERT_EQ(NO_ERROR, captureDisplay(mCaptureArgs, mCaptureResults));
798
799 ASSERT_NO_FATAL_FAILURE(
800 checkScreenCapture(r, g, b,
801 {0, 0, (int32_t)mDisplayWidth, (int32_t)mDisplayHeight / 4}));
802
803 // update the size to half the display and dequeue a buffer quarter of the display.
804 adapter.update(mSurfaceControl, mDisplayWidth, mDisplayHeight / 2);
805
806 {
Patrick Williamsed4745d2023-07-27 17:35:19 -0500807 BBQSyncHelper syncHelper{adapter, 1};
Vishnu Nair932f6ae2021-09-29 17:33:10 -0700808 int slot;
809 sp<Fence> fence;
810 sp<GraphicBuffer> buf;
811 auto ret = igbProducer->dequeueBuffer(&slot, &fence, mDisplayWidth, mDisplayHeight / 8,
812 PIXEL_FORMAT_RGBA_8888, GRALLOC_USAGE_SW_WRITE_OFTEN,
813 nullptr, nullptr);
814 ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, ret);
815 ASSERT_EQ(OK, igbProducer->requestBuffer(slot, &buf));
816
817 uint32_t* bufData;
818 buf->lock(static_cast<uint32_t>(GraphicBuffer::USAGE_SW_WRITE_OFTEN),
819 reinterpret_cast<void**>(&bufData));
820 g = 255;
821 fillBuffer(bufData, Rect(buf->getWidth(), buf->getHeight()), buf->getStride(), r, g, b);
822 buf->unlock();
823
824 IGraphicBufferProducer::QueueBufferOutput qbOutput;
825 IGraphicBufferProducer::QueueBufferInput input(systemTime(), true /* autotimestamp */,
826 HAL_DATASPACE_UNKNOWN, {},
827 NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW,
828 0, Fence::NO_FENCE);
829 igbProducer->queueBuffer(slot, input, &qbOutput);
Patrick Williamsed4745d2023-07-27 17:35:19 -0500830 syncHelper.wait();
Vishnu Nair932f6ae2021-09-29 17:33:10 -0700831 }
832 // capture screen and verify that it is red
833 ASSERT_EQ(NO_ERROR, captureDisplay(mCaptureArgs, mCaptureResults));
834 // verify we still scale the buffer to the new size (half the screen height)
835 ASSERT_NO_FATAL_FAILURE(
836 checkScreenCapture(r, g, b,
837 {0, 0, (int32_t)mDisplayWidth, (int32_t)mDisplayHeight / 2}));
838}
839
chaviwd7deef72021-10-06 11:53:40 -0500840TEST_F(BLASTBufferQueueTest, SyncThenNoSync) {
841 uint8_t r = 255;
842 uint8_t g = 0;
843 uint8_t b = 0;
844
845 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
846
847 sp<IGraphicBufferProducer> igbProducer;
848 setUpProducer(adapter, igbProducer);
849
chaviwa1c4c822021-11-10 18:11:58 -0600850 Transaction sync;
Tianhao Yao4861b102022-02-03 20:18:35 +0000851 adapter.setSyncTransaction(sync);
chaviwd7deef72021-10-06 11:53:40 -0500852 queueBuffer(igbProducer, 0, 255, 0, 0);
853
854 // queue non sync buffer, so this one should get blocked
855 // Add a present delay to allow the first screenshot to get taken.
856 nsecs_t presentTimeDelay = std::chrono::nanoseconds(500ms).count();
857 queueBuffer(igbProducer, r, g, b, presentTimeDelay);
858
859 CallbackHelper transactionCallback;
chaviwa1c4c822021-11-10 18:11:58 -0600860 sync.addTransactionCompletedCallback(transactionCallback.function,
chaviwd7deef72021-10-06 11:53:40 -0500861 transactionCallback.getContext())
862 .apply();
863
864 CallbackData callbackData;
865 transactionCallback.getCallbackData(&callbackData);
866
chaviw0acd33a2021-11-02 11:55:37 -0500867 // capture screen and verify that it is green
chaviwd7deef72021-10-06 11:53:40 -0500868 ASSERT_EQ(NO_ERROR, captureDisplay(mCaptureArgs, mCaptureResults));
869 ASSERT_NO_FATAL_FAILURE(
870 checkScreenCapture(0, 255, 0, {0, 0, (int32_t)mDisplayWidth, (int32_t)mDisplayHeight}));
871
872 mProducerListener->waitOnNumberReleased(1);
873 ASSERT_EQ(NO_ERROR, captureDisplay(mCaptureArgs, mCaptureResults));
874 ASSERT_NO_FATAL_FAILURE(
875 checkScreenCapture(r, g, b, {0, 0, (int32_t)mDisplayWidth, (int32_t)mDisplayHeight}));
876}
877
878TEST_F(BLASTBufferQueueTest, MultipleSyncTransactions) {
879 uint8_t r = 255;
880 uint8_t g = 0;
881 uint8_t b = 0;
882
883 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
884
885 sp<IGraphicBufferProducer> igbProducer;
886 setUpProducer(adapter, igbProducer);
887
888 Transaction mainTransaction;
889
chaviwa1c4c822021-11-10 18:11:58 -0600890 Transaction sync;
Tianhao Yao4861b102022-02-03 20:18:35 +0000891 adapter.setSyncTransaction(sync);
chaviwd7deef72021-10-06 11:53:40 -0500892 queueBuffer(igbProducer, 0, 255, 0, 0);
893
chaviwa1c4c822021-11-10 18:11:58 -0600894 mainTransaction.merge(std::move(sync));
chaviwd7deef72021-10-06 11:53:40 -0500895
Tianhao Yao4861b102022-02-03 20:18:35 +0000896 adapter.setSyncTransaction(sync);
chaviwd7deef72021-10-06 11:53:40 -0500897 queueBuffer(igbProducer, r, g, b, 0);
898
chaviwa1c4c822021-11-10 18:11:58 -0600899 mainTransaction.merge(std::move(sync));
chaviwd7deef72021-10-06 11:53:40 -0500900 // Expect 1 buffer to be released even before sending to SurfaceFlinger
901 mProducerListener->waitOnNumberReleased(1);
902
903 CallbackHelper transactionCallback;
904 mainTransaction
905 .addTransactionCompletedCallback(transactionCallback.function,
906 transactionCallback.getContext())
907 .apply();
908
909 CallbackData callbackData;
910 transactionCallback.getCallbackData(&callbackData);
911
912 // capture screen and verify that it is red
913 ASSERT_EQ(NO_ERROR, captureDisplay(mCaptureArgs, mCaptureResults));
914 ASSERT_NO_FATAL_FAILURE(
915 checkScreenCapture(r, g, b, {0, 0, (int32_t)mDisplayWidth, (int32_t)mDisplayHeight}));
916}
917
918TEST_F(BLASTBufferQueueTest, MultipleSyncTransactionWithNonSync) {
919 uint8_t r = 255;
920 uint8_t g = 0;
921 uint8_t b = 0;
922
923 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
924
925 sp<IGraphicBufferProducer> igbProducer;
926 setUpProducer(adapter, igbProducer);
927
928 Transaction mainTransaction;
929
chaviwa1c4c822021-11-10 18:11:58 -0600930 Transaction sync;
chaviwd7deef72021-10-06 11:53:40 -0500931 // queue a sync transaction
Tianhao Yao4861b102022-02-03 20:18:35 +0000932 adapter.setSyncTransaction(sync);
chaviwd7deef72021-10-06 11:53:40 -0500933 queueBuffer(igbProducer, 0, 255, 0, 0);
934
chaviwa1c4c822021-11-10 18:11:58 -0600935 mainTransaction.merge(std::move(sync));
chaviwd7deef72021-10-06 11:53:40 -0500936
chaviwa1c4c822021-11-10 18:11:58 -0600937 // queue another buffer without setting sync transaction
chaviwd7deef72021-10-06 11:53:40 -0500938 queueBuffer(igbProducer, 0, 0, 255, 0);
939
940 // queue another sync transaction
Tianhao Yao4861b102022-02-03 20:18:35 +0000941 adapter.setSyncTransaction(sync);
chaviwd7deef72021-10-06 11:53:40 -0500942 queueBuffer(igbProducer, r, g, b, 0);
943 // Expect 1 buffer to be released because the non sync transaction should merge
944 // with the sync
945 mProducerListener->waitOnNumberReleased(1);
946
chaviwa1c4c822021-11-10 18:11:58 -0600947 mainTransaction.merge(std::move(sync));
chaviwd7deef72021-10-06 11:53:40 -0500948 // Expect 2 buffers to be released due to merging the two syncs.
949 mProducerListener->waitOnNumberReleased(2);
950
951 CallbackHelper transactionCallback;
952 mainTransaction
953 .addTransactionCompletedCallback(transactionCallback.function,
954 transactionCallback.getContext())
955 .apply();
956
957 CallbackData callbackData;
958 transactionCallback.getCallbackData(&callbackData);
959
960 // capture screen and verify that it is red
961 ASSERT_EQ(NO_ERROR, captureDisplay(mCaptureArgs, mCaptureResults));
962 ASSERT_NO_FATAL_FAILURE(
963 checkScreenCapture(r, g, b, {0, 0, (int32_t)mDisplayWidth, (int32_t)mDisplayHeight}));
964}
965
966TEST_F(BLASTBufferQueueTest, MultipleSyncRunOutOfBuffers) {
967 uint8_t r = 255;
968 uint8_t g = 0;
969 uint8_t b = 0;
970
971 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
972
973 sp<IGraphicBufferProducer> igbProducer;
974 setUpProducer(adapter, igbProducer, 3);
975
976 Transaction mainTransaction;
977
chaviwa1c4c822021-11-10 18:11:58 -0600978 Transaction sync;
chaviwd7deef72021-10-06 11:53:40 -0500979 // queue a sync transaction
Tianhao Yao4861b102022-02-03 20:18:35 +0000980 adapter.setSyncTransaction(sync);
chaviwd7deef72021-10-06 11:53:40 -0500981 queueBuffer(igbProducer, 0, 255, 0, 0);
982
chaviwa1c4c822021-11-10 18:11:58 -0600983 mainTransaction.merge(std::move(sync));
chaviwd7deef72021-10-06 11:53:40 -0500984
chaviwa1c4c822021-11-10 18:11:58 -0600985 // queue a few buffers without setting sync transaction
chaviwd7deef72021-10-06 11:53:40 -0500986 queueBuffer(igbProducer, 0, 0, 255, 0);
987 queueBuffer(igbProducer, 0, 0, 255, 0);
988 queueBuffer(igbProducer, 0, 0, 255, 0);
989
990 // queue another sync transaction
Tianhao Yao4861b102022-02-03 20:18:35 +0000991 adapter.setSyncTransaction(sync);
chaviwd7deef72021-10-06 11:53:40 -0500992 queueBuffer(igbProducer, r, g, b, 0);
993 // Expect 3 buffers to be released because the non sync transactions should merge
994 // with the sync
995 mProducerListener->waitOnNumberReleased(3);
996
chaviwa1c4c822021-11-10 18:11:58 -0600997 mainTransaction.merge(std::move(sync));
chaviwd7deef72021-10-06 11:53:40 -0500998 // Expect 4 buffers to be released due to merging the two syncs.
999 mProducerListener->waitOnNumberReleased(4);
1000
1001 CallbackHelper transactionCallback;
1002 mainTransaction
1003 .addTransactionCompletedCallback(transactionCallback.function,
1004 transactionCallback.getContext())
1005 .apply();
1006
1007 CallbackData callbackData;
1008 transactionCallback.getCallbackData(&callbackData);
1009
1010 // capture screen and verify that it is red
1011 ASSERT_EQ(NO_ERROR, captureDisplay(mCaptureArgs, mCaptureResults));
1012 ASSERT_NO_FATAL_FAILURE(
1013 checkScreenCapture(r, g, b, {0, 0, (int32_t)mDisplayWidth, (int32_t)mDisplayHeight}));
1014}
1015
1016// Tests BBQ with a sync transaction when the buffers acquired reaches max and the only way to
1017// continue processing is for a release callback from SurfaceFlinger.
1018// This is done by sending a buffer to SF so it can release the previous one and allow BBQ to
1019// continue acquiring buffers.
1020TEST_F(BLASTBufferQueueTest, RunOutOfBuffersWaitingOnSF) {
1021 uint8_t r = 255;
1022 uint8_t g = 0;
1023 uint8_t b = 0;
1024
1025 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
1026
1027 sp<IGraphicBufferProducer> igbProducer;
1028 setUpProducer(adapter, igbProducer, 4);
1029
1030 Transaction mainTransaction;
1031
1032 // Send a buffer to SF
1033 queueBuffer(igbProducer, 0, 255, 0, 0);
1034
chaviwa1c4c822021-11-10 18:11:58 -06001035 Transaction sync;
chaviwd7deef72021-10-06 11:53:40 -05001036 // queue a sync transaction
Tianhao Yao4861b102022-02-03 20:18:35 +00001037 adapter.setSyncTransaction(sync);
chaviwd7deef72021-10-06 11:53:40 -05001038 queueBuffer(igbProducer, 0, 255, 0, 0);
1039
chaviwa1c4c822021-11-10 18:11:58 -06001040 mainTransaction.merge(std::move(sync));
chaviwd7deef72021-10-06 11:53:40 -05001041
chaviwa1c4c822021-11-10 18:11:58 -06001042 // queue a few buffers without setting sync transaction
chaviwd7deef72021-10-06 11:53:40 -05001043 queueBuffer(igbProducer, 0, 0, 255, 0);
1044 queueBuffer(igbProducer, 0, 0, 255, 0);
1045 queueBuffer(igbProducer, 0, 0, 255, 0);
1046
1047 // apply the first synced buffer to ensure we have to wait on SF
1048 mainTransaction.apply();
1049
1050 // queue another sync transaction
Tianhao Yao4861b102022-02-03 20:18:35 +00001051 adapter.setSyncTransaction(sync);
chaviwd7deef72021-10-06 11:53:40 -05001052 queueBuffer(igbProducer, r, g, b, 0);
1053 // Expect 2 buffers to be released because the non sync transactions should merge
1054 // with the sync
1055 mProducerListener->waitOnNumberReleased(3);
1056
chaviwa1c4c822021-11-10 18:11:58 -06001057 mainTransaction.merge(std::move(sync));
chaviwd7deef72021-10-06 11:53:40 -05001058
1059 CallbackHelper transactionCallback;
1060 mainTransaction
1061 .addTransactionCompletedCallback(transactionCallback.function,
1062 transactionCallback.getContext())
1063 .apply();
1064
1065 CallbackData callbackData;
1066 transactionCallback.getCallbackData(&callbackData);
1067
1068 // capture screen and verify that it is red
1069 ASSERT_EQ(NO_ERROR, captureDisplay(mCaptureArgs, mCaptureResults));
1070 ASSERT_NO_FATAL_FAILURE(
1071 checkScreenCapture(r, g, b, {0, 0, (int32_t)mDisplayWidth, (int32_t)mDisplayHeight}));
1072}
1073
Tianhao Yao4861b102022-02-03 20:18:35 +00001074TEST_F(BLASTBufferQueueTest, SyncNextTransactionAcquireMultipleBuffers) {
chaviw0acd33a2021-11-02 11:55:37 -05001075 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
1076
1077 sp<IGraphicBufferProducer> igbProducer;
1078 setUpProducer(adapter, igbProducer);
1079
1080 Transaction next;
Tianhao Yao4861b102022-02-03 20:18:35 +00001081 adapter.setSyncTransaction(next, false);
chaviw0acd33a2021-11-02 11:55:37 -05001082 queueBuffer(igbProducer, 0, 255, 0, 0);
1083 queueBuffer(igbProducer, 0, 0, 255, 0);
1084 // There should only be one frame submitted since the first frame will be released.
1085 adapter.validateNumFramesSubmitted(1);
Tianhao Yao4861b102022-02-03 20:18:35 +00001086 adapter.stopContinuousSyncTransaction();
chaviw0acd33a2021-11-02 11:55:37 -05001087
1088 // queue non sync buffer, so this one should get blocked
1089 // Add a present delay to allow the first screenshot to get taken.
1090 nsecs_t presentTimeDelay = std::chrono::nanoseconds(500ms).count();
1091 queueBuffer(igbProducer, 255, 0, 0, presentTimeDelay);
1092
1093 CallbackHelper transactionCallback;
1094 next.addTransactionCompletedCallback(transactionCallback.function,
1095 transactionCallback.getContext())
1096 .apply();
1097
1098 CallbackData callbackData;
1099 transactionCallback.getCallbackData(&callbackData);
1100
1101 // capture screen and verify that it is blue
1102 ASSERT_EQ(NO_ERROR, captureDisplay(mCaptureArgs, mCaptureResults));
1103 ASSERT_NO_FATAL_FAILURE(
1104 checkScreenCapture(0, 0, 255, {0, 0, (int32_t)mDisplayWidth, (int32_t)mDisplayHeight}));
1105
1106 mProducerListener->waitOnNumberReleased(2);
1107 // capture screen and verify that it is red
1108 ASSERT_EQ(NO_ERROR, captureDisplay(mCaptureArgs, mCaptureResults));
1109 ASSERT_NO_FATAL_FAILURE(
1110 checkScreenCapture(255, 0, 0, {0, 0, (int32_t)mDisplayWidth, (int32_t)mDisplayHeight}));
1111}
1112
chaviw3b4bdcf2022-03-17 09:27:03 -05001113TEST_F(BLASTBufferQueueTest, SyncNextTransactionOverwrite) {
1114 std::mutex mutex;
1115 std::condition_variable callbackReceivedCv;
1116 bool receivedCallback = false;
1117
1118 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
1119 ASSERT_EQ(nullptr, adapter.getTransactionReadyCallback());
1120 auto callback = [&](Transaction*) {
1121 std::unique_lock<std::mutex> lock(mutex);
1122 receivedCallback = true;
1123 callbackReceivedCv.notify_one();
1124 };
1125 adapter.syncNextTransaction(callback);
1126 ASSERT_NE(nullptr, adapter.getTransactionReadyCallback());
1127
1128 auto callback2 = [](Transaction*) {};
Chavi Weingartenc398c012023-04-12 17:26:02 +00001129 ASSERT_FALSE(adapter.syncNextTransaction(callback2));
1130
1131 sp<IGraphicBufferProducer> igbProducer;
1132 setUpProducer(adapter, igbProducer);
1133 queueBuffer(igbProducer, 0, 255, 0, 0);
chaviw3b4bdcf2022-03-17 09:27:03 -05001134
1135 std::unique_lock<std::mutex> lock(mutex);
1136 if (!receivedCallback) {
1137 ASSERT_NE(callbackReceivedCv.wait_for(lock, std::chrono::seconds(3)),
1138 std::cv_status::timeout)
1139 << "did not receive callback";
1140 }
1141
1142 ASSERT_TRUE(receivedCallback);
1143}
1144
Chavi Weingartenc398c012023-04-12 17:26:02 +00001145TEST_F(BLASTBufferQueueTest, ClearSyncTransaction) {
1146 std::mutex mutex;
1147 std::condition_variable callbackReceivedCv;
1148 bool receivedCallback = false;
1149
1150 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
1151 ASSERT_EQ(nullptr, adapter.getTransactionReadyCallback());
1152 auto callback = [&](Transaction*) {
1153 std::unique_lock<std::mutex> lock(mutex);
1154 receivedCallback = true;
1155 callbackReceivedCv.notify_one();
1156 };
1157 adapter.syncNextTransaction(callback);
1158 ASSERT_NE(nullptr, adapter.getTransactionReadyCallback());
1159
1160 adapter.clearSyncTransaction();
1161
1162 sp<IGraphicBufferProducer> igbProducer;
1163 setUpProducer(adapter, igbProducer);
1164 queueBuffer(igbProducer, 0, 255, 0, 0);
1165
1166 std::unique_lock<std::mutex> lock(mutex);
1167 if (!receivedCallback) {
1168 ASSERT_EQ(callbackReceivedCv.wait_for(lock, std::chrono::seconds(3)),
1169 std::cv_status::timeout)
1170 << "did not receive callback";
1171 }
1172
1173 ASSERT_FALSE(receivedCallback);
1174}
1175
chaviwc1cf4022022-06-03 13:32:33 -05001176TEST_F(BLASTBufferQueueTest, SyncNextTransactionDropBuffer) {
1177 uint8_t r = 255;
1178 uint8_t g = 0;
1179 uint8_t b = 0;
1180
1181 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
1182
1183 sp<IGraphicBufferProducer> igbProducer;
1184 setUpProducer(adapter, igbProducer);
1185
1186 Transaction sync;
1187 adapter.setSyncTransaction(sync);
1188 queueBuffer(igbProducer, 0, 255, 0, 0);
1189
1190 // Merge a transaction that has a complete callback into the next frame so we can get notified
1191 // when to take a screenshot
1192 CallbackHelper transactionCallback;
1193 Transaction t;
1194 t.addTransactionCompletedCallback(transactionCallback.function,
1195 transactionCallback.getContext());
1196 adapter.mergeWithNextTransaction(&t, 2);
1197 queueBuffer(igbProducer, r, g, b, 0);
1198
1199 // Drop the buffer, but ensure the next one continues to get processed.
1200 sync.setBuffer(mSurfaceControl, nullptr);
1201
1202 CallbackData callbackData;
1203 transactionCallback.getCallbackData(&callbackData);
1204 ASSERT_EQ(NO_ERROR, captureDisplay(mCaptureArgs, mCaptureResults));
1205 ASSERT_NO_FATAL_FAILURE(
1206 checkScreenCapture(r, g, b, {0, 0, (int32_t)mDisplayWidth, (int32_t)mDisplayHeight}));
Rob Carr4f797ab2022-07-07 18:29:22 +00001207 sync.apply();
chaviwc1cf4022022-06-03 13:32:33 -05001208}
1209
Vishnu Nair1e8bf102021-12-28 14:36:59 -08001210// This test will currently fail because the old surfacecontrol will steal the last presented buffer
1211// until the old surface control is destroyed. This is not necessarily a bug but to document a
1212// limitation with the update API and to test any changes to make the api more robust. The current
1213// approach for the client is to recreate the blastbufferqueue when the surfacecontrol updates.
1214TEST_F(BLASTBufferQueueTest, DISABLED_DisconnectProducerTest) {
1215 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
1216 std::vector<sp<SurfaceControl>> surfaceControls;
1217 sp<IGraphicBufferProducer> igbProducer;
1218 for (int i = 0; i < 10; i++) {
1219 sp<SurfaceControl> sc =
1220 mClient->createSurface(String8("TestSurface"), mDisplayWidth, mDisplayHeight,
1221 PIXEL_FORMAT_RGBA_8888,
1222 ISurfaceComposerClient::eFXSurfaceBufferState,
1223 /*parent*/ nullptr);
1224 Transaction()
1225 .setLayerStack(mSurfaceControl, ui::DEFAULT_LAYER_STACK)
1226 .setLayer(mSurfaceControl, std::numeric_limits<int32_t>::max())
1227 .show(mSurfaceControl)
1228 .setDataspace(mSurfaceControl, ui::Dataspace::V0_SRGB)
1229 .apply(true);
1230 surfaceControls.push_back(sc);
1231 adapter.update(sc, mDisplayWidth, mDisplayHeight);
1232
1233 setUpProducer(adapter, igbProducer);
1234 Transaction next;
1235 queueBuffer(igbProducer, 0, 255, 0, 0);
1236 queueBuffer(igbProducer, 0, 0, 255, 0);
Tianhao Yao4861b102022-02-03 20:18:35 +00001237 adapter.setSyncTransaction(next, true);
Vishnu Nair1e8bf102021-12-28 14:36:59 -08001238 queueBuffer(igbProducer, 255, 0, 0, 0);
1239
1240 CallbackHelper transactionCallback;
1241 next.addTransactionCompletedCallback(transactionCallback.function,
1242 transactionCallback.getContext())
1243 .apply();
1244
1245 CallbackData callbackData;
1246 transactionCallback.getCallbackData(&callbackData);
1247 // capture screen and verify that it is red
1248 ASSERT_EQ(NO_ERROR, captureDisplay(mCaptureArgs, mCaptureResults));
1249 ASSERT_NO_FATAL_FAILURE(
1250 checkScreenCapture(255, 0, 0,
1251 {0, 0, (int32_t)mDisplayWidth, (int32_t)mDisplayHeight}));
1252 igbProducer->disconnect(NATIVE_WINDOW_API_CPU);
1253 }
1254}
1255
1256// See DISABLED_DisconnectProducerTest
1257TEST_F(BLASTBufferQueueTest, DISABLED_UpdateSurfaceControlTest) {
1258 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
1259 std::vector<sp<SurfaceControl>> surfaceControls;
1260 sp<IGraphicBufferProducer> igbProducer;
1261 for (int i = 0; i < 10; i++) {
1262 sp<SurfaceControl> sc =
1263 mClient->createSurface(String8("TestSurface"), mDisplayWidth, mDisplayHeight,
1264 PIXEL_FORMAT_RGBA_8888,
1265 ISurfaceComposerClient::eFXSurfaceBufferState,
1266 /*parent*/ nullptr);
1267 Transaction()
1268 .setLayerStack(mSurfaceControl, ui::DEFAULT_LAYER_STACK)
1269 .setLayer(mSurfaceControl, std::numeric_limits<int32_t>::max())
1270 .show(mSurfaceControl)
1271 .setDataspace(mSurfaceControl, ui::Dataspace::V0_SRGB)
1272 .apply(true);
1273 surfaceControls.push_back(sc);
1274 adapter.update(sc, mDisplayWidth, mDisplayHeight);
1275 setUpProducer(adapter, igbProducer);
1276
1277 Transaction next;
1278 queueBuffer(igbProducer, 0, 255, 0, 0);
1279 queueBuffer(igbProducer, 0, 0, 255, 0);
Tianhao Yao4861b102022-02-03 20:18:35 +00001280 adapter.setSyncTransaction(next, true);
Vishnu Nair1e8bf102021-12-28 14:36:59 -08001281 queueBuffer(igbProducer, 255, 0, 0, 0);
1282
1283 CallbackHelper transactionCallback;
1284 next.addTransactionCompletedCallback(transactionCallback.function,
1285 transactionCallback.getContext())
1286 .apply();
1287
1288 CallbackData callbackData;
1289 transactionCallback.getCallbackData(&callbackData);
1290 // capture screen and verify that it is red
1291 ASSERT_EQ(NO_ERROR, captureDisplay(mCaptureArgs, mCaptureResults));
1292 ASSERT_NO_FATAL_FAILURE(
1293 checkScreenCapture(255, 0, 0,
1294 {0, 0, (int32_t)mDisplayWidth, (int32_t)mDisplayHeight}));
1295 }
1296}
1297
Vishnu Nair89496122020-12-14 17:14:53 -08001298class TestProducerListener : public BnProducerListener {
1299public:
1300 sp<IGraphicBufferProducer> mIgbp;
1301 TestProducerListener(const sp<IGraphicBufferProducer>& igbp) : mIgbp(igbp) {}
1302 void onBufferReleased() override {
1303 sp<GraphicBuffer> buffer;
1304 sp<Fence> fence;
1305 mIgbp->detachNextBuffer(&buffer, &fence);
1306 }
1307};
1308
1309TEST_F(BLASTBufferQueueTest, CustomProducerListener) {
1310 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
1311 sp<IGraphicBufferProducer> igbProducer = adapter.getIGraphicBufferProducer();
1312 ASSERT_NE(nullptr, igbProducer.get());
1313 ASSERT_EQ(NO_ERROR, igbProducer->setMaxDequeuedBufferCount(2));
1314 IGraphicBufferProducer::QueueBufferOutput qbOutput;
1315 ASSERT_EQ(NO_ERROR,
1316 igbProducer->connect(new TestProducerListener(igbProducer), NATIVE_WINDOW_API_CPU,
1317 false, &qbOutput));
1318 ASSERT_NE(ui::Transform::ROT_INVALID, qbOutput.transformHint);
1319 for (int i = 0; i < 3; i++) {
1320 int slot;
1321 sp<Fence> fence;
1322 sp<GraphicBuffer> buf;
1323 auto ret = igbProducer->dequeueBuffer(&slot, &fence, mDisplayWidth, mDisplayHeight,
1324 PIXEL_FORMAT_RGBA_8888, GRALLOC_USAGE_SW_WRITE_OFTEN,
1325 nullptr, nullptr);
1326 ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, ret);
1327 ASSERT_EQ(OK, igbProducer->requestBuffer(slot, &buf));
1328 IGraphicBufferProducer::QueueBufferOutput qbOutput;
Vishnu Nair1506b182021-02-22 14:35:15 -08001329 IGraphicBufferProducer::QueueBufferInput input(systemTime(), true /* autotimestamp */,
1330 HAL_DATASPACE_UNKNOWN,
Vishnu Nair89496122020-12-14 17:14:53 -08001331 Rect(mDisplayWidth, mDisplayHeight),
1332 NATIVE_WINDOW_SCALING_MODE_FREEZE, 0,
1333 Fence::NO_FENCE);
1334 igbProducer->queueBuffer(slot, input, &qbOutput);
1335 }
1336 adapter.waitForCallbacks();
1337}
1338
Vishnu Nair17dde612020-12-28 11:39:59 -08001339TEST_F(BLASTBufferQueueTest, QueryNativeWindowQueuesToWindowComposer) {
1340 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
1341
1342 sp<android::Surface> surface = new Surface(adapter.getIGraphicBufferProducer());
1343 ANativeWindow* nativeWindow = (ANativeWindow*)(surface.get());
1344 int queuesToNativeWindow = 0;
1345 int err = nativeWindow->query(nativeWindow, NATIVE_WINDOW_QUEUES_TO_WINDOW_COMPOSER,
1346 &queuesToNativeWindow);
1347 ASSERT_EQ(NO_ERROR, err);
1348 ASSERT_EQ(queuesToNativeWindow, 1);
1349}
1350
Vishnu Nair083efd32021-02-12 09:32:30 -08001351// Test a slow producer doesn't hold up a faster producer from the same client. Essentially tests
1352// BBQ uses separate transaction queues.
Vishnu Nair277142c2021-01-05 18:35:29 -08001353TEST_F(BLASTBufferQueueTest, OutOfOrderTransactionTest) {
1354 sp<SurfaceControl> bgSurface =
1355 mClient->createSurface(String8("BGTest"), 0, 0, PIXEL_FORMAT_RGBA_8888,
1356 ISurfaceComposerClient::eFXSurfaceBufferState);
1357 ASSERT_NE(nullptr, bgSurface.get());
1358 Transaction t;
Dominik Laskowski29fa1462021-04-27 15:51:50 -07001359 t.setLayerStack(bgSurface, ui::DEFAULT_LAYER_STACK)
Vishnu Nair277142c2021-01-05 18:35:29 -08001360 .show(bgSurface)
1361 .setDataspace(bgSurface, ui::Dataspace::V0_SRGB)
Vishnu Nair277142c2021-01-05 18:35:29 -08001362 .setLayer(bgSurface, std::numeric_limits<int32_t>::max() - 1)
1363 .apply();
1364
1365 BLASTBufferQueueHelper slowAdapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
1366 sp<IGraphicBufferProducer> slowIgbProducer;
1367 setUpProducer(slowAdapter, slowIgbProducer);
1368 nsecs_t presentTimeDelay = std::chrono::nanoseconds(500ms).count();
Vishnu Nair1506b182021-02-22 14:35:15 -08001369 queueBuffer(slowIgbProducer, 0 /* r */, 255 /* g */, 0 /* b */, presentTimeDelay);
Vishnu Nair277142c2021-01-05 18:35:29 -08001370
1371 BLASTBufferQueueHelper fastAdapter(bgSurface, mDisplayWidth, mDisplayHeight);
1372 sp<IGraphicBufferProducer> fastIgbProducer;
1373 setUpProducer(fastAdapter, fastIgbProducer);
1374 uint8_t r = 255;
1375 uint8_t g = 0;
1376 uint8_t b = 0;
1377 queueBuffer(fastIgbProducer, r, g, b, 0 /* presentTimeDelay */);
1378 fastAdapter.waitForCallbacks();
1379
1380 // capture screen and verify that it is red
1381 ASSERT_EQ(NO_ERROR, captureDisplay(mCaptureArgs, mCaptureResults));
1382
1383 ASSERT_NO_FATAL_FAILURE(
Chavi Weingartena5aedbd2021-04-09 13:37:33 +00001384 checkScreenCapture(r, g, b,
1385 {0, 0, (int32_t)mDisplayWidth, (int32_t)mDisplayHeight / 2}));
Vishnu Nair277142c2021-01-05 18:35:29 -08001386}
1387
Vishnu Naira4fbca52021-07-07 16:52:34 -07001388TEST_F(BLASTBufferQueueTest, TransformHint) {
1389 // Transform hint is provided to BBQ via the surface control passed by WM
1390 mSurfaceControl->setTransformHint(ui::Transform::ROT_90);
1391
1392 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
1393 sp<IGraphicBufferProducer> igbProducer = adapter.getIGraphicBufferProducer();
1394 ASSERT_NE(nullptr, igbProducer.get());
1395 ASSERT_EQ(NO_ERROR, igbProducer->setMaxDequeuedBufferCount(2));
1396 sp<Surface> surface = adapter.getSurface();
1397
1398 // Before connecting to the surface, we do not get a valid transform hint
1399 int transformHint;
1400 surface->query(NATIVE_WINDOW_TRANSFORM_HINT, &transformHint);
1401 ASSERT_EQ(ui::Transform::ROT_0, transformHint);
1402
1403 ASSERT_EQ(NO_ERROR,
1404 surface->connect(NATIVE_WINDOW_API_CPU, new TestProducerListener(igbProducer)));
1405
1406 // After connecting to the surface, we should get the correct hint.
1407 surface->query(NATIVE_WINDOW_TRANSFORM_HINT, &transformHint);
1408 ASSERT_EQ(ui::Transform::ROT_90, transformHint);
1409
1410 ANativeWindow_Buffer buffer;
1411 surface->lock(&buffer, nullptr /* inOutDirtyBounds */);
1412
1413 // Transform hint is updated via callbacks or surface control updates
1414 mSurfaceControl->setTransformHint(ui::Transform::ROT_0);
1415 adapter.update(mSurfaceControl, mDisplayWidth, mDisplayHeight);
1416
1417 // The hint does not change and matches the value used when dequeueing the buffer.
1418 surface->query(NATIVE_WINDOW_TRANSFORM_HINT, &transformHint);
1419 ASSERT_EQ(ui::Transform::ROT_90, transformHint);
1420
1421 surface->unlockAndPost();
1422
1423 // After queuing the buffer, we get the updated transform hint
1424 surface->query(NATIVE_WINDOW_TRANSFORM_HINT, &transformHint);
1425 ASSERT_EQ(ui::Transform::ROT_0, transformHint);
1426
1427 adapter.waitForCallbacks();
1428}
1429
Valerie Hau5977fc82019-12-05 15:56:39 -08001430class BLASTBufferQueueTransformTest : public BLASTBufferQueueTest {
1431public:
1432 void test(uint32_t tr) {
1433 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
1434 sp<IGraphicBufferProducer> igbProducer;
1435 setUpProducer(adapter, igbProducer);
1436
1437 auto bufWidth = mDisplayWidth;
1438 auto bufHeight = mDisplayHeight;
1439 int slot;
1440 sp<Fence> fence;
1441 sp<GraphicBuffer> buf;
1442
1443 auto ret = igbProducer->dequeueBuffer(&slot, &fence, bufWidth, bufHeight,
1444 PIXEL_FORMAT_RGBA_8888, GRALLOC_USAGE_SW_WRITE_OFTEN,
1445 nullptr, nullptr);
1446 ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, ret);
1447 ASSERT_EQ(OK, igbProducer->requestBuffer(slot, &buf));
1448
1449 fillQuadrants(buf);
1450
1451 IGraphicBufferProducer::QueueBufferOutput qbOutput;
Vishnu Nair1506b182021-02-22 14:35:15 -08001452 IGraphicBufferProducer::QueueBufferInput input(systemTime(), true /* autotimestamp */,
1453 HAL_DATASPACE_UNKNOWN,
Valerie Hau5977fc82019-12-05 15:56:39 -08001454 Rect(bufWidth, bufHeight),
Vishnu Naire1a42322020-10-02 17:42:04 -07001455 NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW,
1456 tr, Fence::NO_FENCE);
Valerie Hau5977fc82019-12-05 15:56:39 -08001457 igbProducer->queueBuffer(slot, input, &qbOutput);
Dominik Laskowski718f9602019-11-09 20:01:35 -08001458 ASSERT_NE(ui::Transform::ROT_INVALID, qbOutput.transformHint);
Valerie Hau5977fc82019-12-05 15:56:39 -08001459
1460 adapter.waitForCallbacks();
chaviw8ffc7b82020-08-18 11:25:37 -07001461 ASSERT_EQ(NO_ERROR, captureDisplay(mCaptureArgs, mCaptureResults));
chaviwd2432892020-07-24 17:42:39 -07001462
Valerie Hau5977fc82019-12-05 15:56:39 -08001463 switch (tr) {
1464 case ui::Transform::ROT_0:
1465 ASSERT_NO_FATAL_FAILURE(checkScreenCapture(0, 0, 0,
1466 {0, 0, (int32_t)mDisplayWidth / 2,
1467 (int32_t)mDisplayHeight / 2},
1468 1));
1469 ASSERT_NO_FATAL_FAILURE(
1470 checkScreenCapture(255, 0, 0,
1471 {(int32_t)mDisplayWidth / 2, 0, (int32_t)mDisplayWidth,
1472 (int32_t)mDisplayHeight / 2},
1473 1));
1474 ASSERT_NO_FATAL_FAILURE(
1475 checkScreenCapture(0, 255, 0,
1476 {(int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight / 2,
1477 (int32_t)mDisplayWidth, (int32_t)mDisplayHeight},
1478 1));
1479 ASSERT_NO_FATAL_FAILURE(
1480 checkScreenCapture(0, 0, 255,
1481 {0, (int32_t)mDisplayHeight / 2,
1482 (int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight},
1483 1));
1484 break;
1485 case ui::Transform::FLIP_H:
1486 ASSERT_NO_FATAL_FAILURE(checkScreenCapture(255, 0, 0,
1487 {0, 0, (int32_t)mDisplayWidth / 2,
1488 (int32_t)mDisplayHeight / 2},
1489 1));
1490 ASSERT_NO_FATAL_FAILURE(
1491 checkScreenCapture(0, 0, 0,
1492 {(int32_t)mDisplayWidth / 2, 0, (int32_t)mDisplayWidth,
1493 (int32_t)mDisplayHeight / 2},
1494 1));
1495 ASSERT_NO_FATAL_FAILURE(
1496 checkScreenCapture(0, 0, 255,
1497 {(int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight / 2,
1498 (int32_t)mDisplayWidth, (int32_t)mDisplayHeight},
1499 1));
1500 ASSERT_NO_FATAL_FAILURE(
1501 checkScreenCapture(0, 255, 0,
1502 {0, (int32_t)mDisplayHeight / 2,
1503 (int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight},
1504 1));
1505 break;
1506 case ui::Transform::FLIP_V:
1507 ASSERT_NO_FATAL_FAILURE(checkScreenCapture(0, 0, 255,
1508 {0, 0, (int32_t)mDisplayWidth / 2,
1509 (int32_t)mDisplayHeight / 2},
1510 1));
1511 ASSERT_NO_FATAL_FAILURE(
1512 checkScreenCapture(0, 255, 0,
1513 {(int32_t)mDisplayWidth / 2, 0, (int32_t)mDisplayWidth,
1514 (int32_t)mDisplayHeight / 2},
1515 1));
1516 ASSERT_NO_FATAL_FAILURE(
1517 checkScreenCapture(255, 0, 0,
1518 {(int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight / 2,
1519 (int32_t)mDisplayWidth, (int32_t)mDisplayHeight},
1520 1));
1521 ASSERT_NO_FATAL_FAILURE(
1522 checkScreenCapture(0, 0, 0,
1523 {0, (int32_t)mDisplayHeight / 2,
1524 (int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight},
1525 1));
1526 break;
1527 case ui::Transform::ROT_90:
1528 ASSERT_NO_FATAL_FAILURE(checkScreenCapture(0, 0, 255,
1529 {0, 0, (int32_t)mDisplayWidth / 2,
1530 (int32_t)mDisplayHeight / 2},
1531 1));
1532 ASSERT_NO_FATAL_FAILURE(
1533 checkScreenCapture(0, 0, 0,
1534 {(int32_t)mDisplayWidth / 2, 0, (int32_t)mDisplayWidth,
1535 (int32_t)mDisplayHeight / 2},
1536 1));
1537 ASSERT_NO_FATAL_FAILURE(
1538 checkScreenCapture(255, 0, 0,
1539 {(int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight / 2,
1540 (int32_t)mDisplayWidth, (int32_t)mDisplayHeight},
1541 1));
1542 ASSERT_NO_FATAL_FAILURE(
1543 checkScreenCapture(0, 255, 0,
1544 {0, (int32_t)mDisplayHeight / 2,
1545 (int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight},
1546 1));
1547 break;
1548 case ui::Transform::ROT_180:
1549 ASSERT_NO_FATAL_FAILURE(checkScreenCapture(0, 255, 0,
1550 {0, 0, (int32_t)mDisplayWidth / 2,
1551 (int32_t)mDisplayHeight / 2},
1552 1));
1553 ASSERT_NO_FATAL_FAILURE(
1554 checkScreenCapture(0, 0, 255,
1555 {(int32_t)mDisplayWidth / 2, 0, (int32_t)mDisplayWidth,
1556 (int32_t)mDisplayHeight / 2},
1557 1));
1558 ASSERT_NO_FATAL_FAILURE(
1559 checkScreenCapture(0, 0, 0,
1560 {(int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight / 2,
1561 (int32_t)mDisplayWidth, (int32_t)mDisplayHeight},
1562 1));
1563 ASSERT_NO_FATAL_FAILURE(
1564 checkScreenCapture(255, 0, 0,
1565 {0, (int32_t)mDisplayHeight / 2,
1566 (int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight},
1567 1));
1568 break;
1569 case ui::Transform::ROT_270:
1570 ASSERT_NO_FATAL_FAILURE(checkScreenCapture(255, 0, 0,
1571 {0, 0, (int32_t)mDisplayWidth / 2,
1572 (int32_t)mDisplayHeight / 2},
1573 1));
1574 ASSERT_NO_FATAL_FAILURE(
1575 checkScreenCapture(0, 255, 0,
1576 {(int32_t)mDisplayWidth / 2, 0, (int32_t)mDisplayWidth,
1577 (int32_t)mDisplayHeight / 2},
1578 1));
1579 ASSERT_NO_FATAL_FAILURE(
1580 checkScreenCapture(0, 0, 255,
1581 {(int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight / 2,
1582 (int32_t)mDisplayWidth, (int32_t)mDisplayHeight},
1583 1));
1584 ASSERT_NO_FATAL_FAILURE(
1585 checkScreenCapture(0, 0, 0,
1586 {0, (int32_t)mDisplayHeight / 2,
1587 (int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight},
1588 1));
1589 }
1590 }
1591};
1592
1593TEST_F(BLASTBufferQueueTransformTest, setTransform_ROT_0) {
1594 test(ui::Transform::ROT_0);
1595}
1596
1597TEST_F(BLASTBufferQueueTransformTest, setTransform_FLIP_H) {
1598 test(ui::Transform::FLIP_H);
1599}
1600
1601TEST_F(BLASTBufferQueueTransformTest, setTransform_FLIP_V) {
1602 test(ui::Transform::FLIP_V);
1603}
1604
1605TEST_F(BLASTBufferQueueTransformTest, setTransform_ROT_90) {
1606 test(ui::Transform::ROT_90);
1607}
1608
1609TEST_F(BLASTBufferQueueTransformTest, setTransform_ROT_180) {
1610 test(ui::Transform::ROT_180);
1611}
1612
1613TEST_F(BLASTBufferQueueTransformTest, setTransform_ROT_270) {
1614 test(ui::Transform::ROT_270);
1615}
Valerie Hau871d6352020-01-29 08:44:02 -08001616
1617class BLASTFrameEventHistoryTest : public BLASTBufferQueueTest {
1618public:
1619 void setUpAndQueueBuffer(const sp<IGraphicBufferProducer>& igbProducer,
Vishnu Nairde66dc72021-06-17 17:54:41 -07001620 nsecs_t* outRequestedPresentTime, nsecs_t* postedTime,
Valerie Hau871d6352020-01-29 08:44:02 -08001621 IGraphicBufferProducer::QueueBufferOutput* qbOutput,
Vishnu Nairde66dc72021-06-17 17:54:41 -07001622 bool getFrameTimestamps, nsecs_t requestedPresentTime = systemTime()) {
Valerie Hau871d6352020-01-29 08:44:02 -08001623 int slot;
1624 sp<Fence> fence;
1625 sp<GraphicBuffer> buf;
1626 auto ret = igbProducer->dequeueBuffer(&slot, &fence, mDisplayWidth, mDisplayHeight,
1627 PIXEL_FORMAT_RGBA_8888, GRALLOC_USAGE_SW_WRITE_OFTEN,
1628 nullptr, nullptr);
Vishnu Nairde66dc72021-06-17 17:54:41 -07001629 if (IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION == ret) {
1630 ASSERT_EQ(OK, igbProducer->requestBuffer(slot, &buf));
1631 }
Valerie Hau871d6352020-01-29 08:44:02 -08001632
Vishnu Nairde66dc72021-06-17 17:54:41 -07001633 *outRequestedPresentTime = requestedPresentTime;
1634 IGraphicBufferProducer::QueueBufferInput input(requestedPresentTime, false,
1635 HAL_DATASPACE_UNKNOWN,
Valerie Hau871d6352020-01-29 08:44:02 -08001636 Rect(mDisplayWidth, mDisplayHeight),
1637 NATIVE_WINDOW_SCALING_MODE_FREEZE, 0,
1638 Fence::NO_FENCE, /*sticky*/ 0,
1639 getFrameTimestamps);
1640 if (postedTime) *postedTime = systemTime();
1641 igbProducer->queueBuffer(slot, input, qbOutput);
1642 }
Vishnu Nair083efd32021-02-12 09:32:30 -08001643 sp<SurfaceControl> mBufferQueueSurfaceControl;
Valerie Hau871d6352020-01-29 08:44:02 -08001644};
1645
1646TEST_F(BLASTFrameEventHistoryTest, FrameEventHistory_Basic) {
1647 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
1648 sp<IGraphicBufferProducer> igbProducer;
1649 ProducerFrameEventHistory history;
1650 setUpProducer(adapter, igbProducer);
1651
1652 IGraphicBufferProducer::QueueBufferOutput qbOutput;
1653 nsecs_t requestedPresentTimeA = 0;
1654 nsecs_t postedTimeA = 0;
1655 setUpAndQueueBuffer(igbProducer, &requestedPresentTimeA, &postedTimeA, &qbOutput, true);
1656 history.applyDelta(qbOutput.frameTimestamps);
1657
1658 FrameEvents* events = nullptr;
1659 events = history.getFrame(1);
1660 ASSERT_NE(nullptr, events);
1661 ASSERT_EQ(1, events->frameNumber);
1662 ASSERT_EQ(requestedPresentTimeA, events->requestedPresentTime);
1663 ASSERT_GE(events->postedTime, postedTimeA);
1664
Vishnu Nair1506b182021-02-22 14:35:15 -08001665 adapter.waitForCallback(1);
Valerie Hau871d6352020-01-29 08:44:02 -08001666
1667 // queue another buffer so we query for frame event deltas
1668 nsecs_t requestedPresentTimeB = 0;
1669 nsecs_t postedTimeB = 0;
1670 setUpAndQueueBuffer(igbProducer, &requestedPresentTimeB, &postedTimeB, &qbOutput, true);
1671 history.applyDelta(qbOutput.frameTimestamps);
1672 events = history.getFrame(1);
1673 ASSERT_NE(nullptr, events);
1674
1675 // frame number, requestedPresentTime, and postTime should not have changed
1676 ASSERT_EQ(1, events->frameNumber);
1677 ASSERT_EQ(requestedPresentTimeA, events->requestedPresentTime);
1678 ASSERT_GE(events->postedTime, postedTimeA);
1679
1680 ASSERT_GE(events->latchTime, postedTimeA);
1681 ASSERT_GE(events->dequeueReadyTime, events->latchTime);
1682 ASSERT_NE(nullptr, events->gpuCompositionDoneFence);
1683 ASSERT_NE(nullptr, events->displayPresentFence);
1684 ASSERT_NE(nullptr, events->releaseFence);
1685
1686 // we should also have gotten the initial values for the next frame
1687 events = history.getFrame(2);
1688 ASSERT_NE(nullptr, events);
1689 ASSERT_EQ(2, events->frameNumber);
1690 ASSERT_EQ(requestedPresentTimeB, events->requestedPresentTime);
1691 ASSERT_GE(events->postedTime, postedTimeB);
Valerie Hau78491e92020-04-15 13:10:56 -07001692
1693 // wait for any callbacks that have not been received
1694 adapter.waitForCallbacks();
Valerie Hau871d6352020-01-29 08:44:02 -08001695}
Vishnu Nair083efd32021-02-12 09:32:30 -08001696
Vishnu Nair083efd32021-02-12 09:32:30 -08001697TEST_F(BLASTFrameEventHistoryTest, FrameEventHistory_DroppedFrame) {
1698 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
1699 sp<IGraphicBufferProducer> igbProducer;
1700 setUpProducer(adapter, igbProducer);
1701
1702 ProducerFrameEventHistory history;
1703 IGraphicBufferProducer::QueueBufferOutput qbOutput;
1704 nsecs_t requestedPresentTimeA = 0;
1705 nsecs_t postedTimeA = 0;
Vishnu Nairde66dc72021-06-17 17:54:41 -07001706 // Present the frame sometime in the future so we can add two frames to the queue so the older
1707 // one will be dropped.
1708 nsecs_t presentTime = systemTime() + std::chrono::nanoseconds(500ms).count();
Vishnu Nair083efd32021-02-12 09:32:30 -08001709 setUpAndQueueBuffer(igbProducer, &requestedPresentTimeA, &postedTimeA, &qbOutput, true,
Vishnu Nairde66dc72021-06-17 17:54:41 -07001710 presentTime);
Vishnu Nair083efd32021-02-12 09:32:30 -08001711 history.applyDelta(qbOutput.frameTimestamps);
1712
1713 FrameEvents* events = nullptr;
1714 events = history.getFrame(1);
1715 ASSERT_NE(nullptr, events);
1716 ASSERT_EQ(1, events->frameNumber);
1717 ASSERT_EQ(requestedPresentTimeA, events->requestedPresentTime);
1718 ASSERT_GE(events->postedTime, postedTimeA);
1719
1720 // queue another buffer so the first can be dropped
1721 nsecs_t requestedPresentTimeB = 0;
1722 nsecs_t postedTimeB = 0;
Vishnu Nairde66dc72021-06-17 17:54:41 -07001723 presentTime = systemTime() + std::chrono::nanoseconds(1ms).count();
1724 setUpAndQueueBuffer(igbProducer, &requestedPresentTimeB, &postedTimeB, &qbOutput, true,
1725 presentTime);
Vishnu Nair083efd32021-02-12 09:32:30 -08001726 history.applyDelta(qbOutput.frameTimestamps);
1727 events = history.getFrame(1);
1728 ASSERT_NE(nullptr, events);
1729
1730 // frame number, requestedPresentTime, and postTime should not have changed
1731 ASSERT_EQ(1, events->frameNumber);
1732 ASSERT_EQ(requestedPresentTimeA, events->requestedPresentTime);
1733 ASSERT_GE(events->postedTime, postedTimeA);
1734
Vishnu Nairde66dc72021-06-17 17:54:41 -07001735 // a valid latchtime and pre and post composition info should not be set for the dropped frame
Vishnu Nair083efd32021-02-12 09:32:30 -08001736 ASSERT_FALSE(events->hasLatchInfo());
1737 ASSERT_FALSE(events->hasDequeueReadyInfo());
Vishnu Nairde66dc72021-06-17 17:54:41 -07001738 ASSERT_FALSE(events->hasGpuCompositionDoneInfo());
1739 ASSERT_FALSE(events->hasDisplayPresentInfo());
1740 ASSERT_FALSE(events->hasReleaseInfo());
Vishnu Nair083efd32021-02-12 09:32:30 -08001741
Vishnu Nairde66dc72021-06-17 17:54:41 -07001742 // wait for the last transaction to be completed.
1743 adapter.waitForCallback(2);
Vishnu Nair083efd32021-02-12 09:32:30 -08001744
Vishnu Nairde66dc72021-06-17 17:54:41 -07001745 // queue another buffer so we query for frame event deltas
1746 nsecs_t requestedPresentTimeC = 0;
1747 nsecs_t postedTimeC = 0;
1748 setUpAndQueueBuffer(igbProducer, &requestedPresentTimeC, &postedTimeC, &qbOutput, true);
1749 history.applyDelta(qbOutput.frameTimestamps);
1750
1751 // frame number, requestedPresentTime, and postTime should not have changed
1752 ASSERT_EQ(1, events->frameNumber);
1753 ASSERT_EQ(requestedPresentTimeA, events->requestedPresentTime);
1754 ASSERT_GE(events->postedTime, postedTimeA);
1755
1756 // a valid latchtime and pre and post composition info should not be set for the dropped frame
1757 ASSERT_FALSE(events->hasLatchInfo());
1758 ASSERT_FALSE(events->hasDequeueReadyInfo());
1759 ASSERT_FALSE(events->hasGpuCompositionDoneInfo());
1760 ASSERT_FALSE(events->hasDisplayPresentInfo());
1761 ASSERT_FALSE(events->hasReleaseInfo());
1762
1763 // we should also have gotten values for the presented frame
Vishnu Nair083efd32021-02-12 09:32:30 -08001764 events = history.getFrame(2);
1765 ASSERT_NE(nullptr, events);
1766 ASSERT_EQ(2, events->frameNumber);
1767 ASSERT_EQ(requestedPresentTimeB, events->requestedPresentTime);
1768 ASSERT_GE(events->postedTime, postedTimeB);
Vishnu Nairde66dc72021-06-17 17:54:41 -07001769 ASSERT_GE(events->latchTime, postedTimeB);
1770 ASSERT_GE(events->dequeueReadyTime, events->latchTime);
1771 ASSERT_NE(nullptr, events->gpuCompositionDoneFence);
1772 ASSERT_NE(nullptr, events->displayPresentFence);
1773 ASSERT_NE(nullptr, events->releaseFence);
1774
1775 // wait for any callbacks that have not been received
1776 adapter.waitForCallbacks();
Vishnu Nair083efd32021-02-12 09:32:30 -08001777}
1778
Vishnu Nair9a69a042021-06-18 13:19:49 -07001779TEST_F(BLASTFrameEventHistoryTest, FrameEventHistory_CompositorTimings) {
1780 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
1781 sp<IGraphicBufferProducer> igbProducer;
1782 ProducerFrameEventHistory history;
1783 setUpProducer(adapter, igbProducer);
1784
1785 IGraphicBufferProducer::QueueBufferOutput qbOutput;
1786 nsecs_t requestedPresentTimeA = 0;
1787 nsecs_t postedTimeA = 0;
Vishnu Nair9a69a042021-06-18 13:19:49 -07001788 setUpAndQueueBuffer(igbProducer, &requestedPresentTimeA, &postedTimeA, &qbOutput, true);
1789 history.applyDelta(qbOutput.frameTimestamps);
1790 adapter.waitForCallback(1);
1791
1792 // queue another buffer so we query for frame event deltas
1793 nsecs_t requestedPresentTimeB = 0;
1794 nsecs_t postedTimeB = 0;
1795 setUpAndQueueBuffer(igbProducer, &requestedPresentTimeB, &postedTimeB, &qbOutput, true);
1796 history.applyDelta(qbOutput.frameTimestamps);
1797
1798 // check for a valid compositor deadline
1799 ASSERT_NE(0, history.getReportedCompositeDeadline());
1800
1801 // wait for any callbacks that have not been received
1802 adapter.waitForCallbacks();
1803}
1804
Valerie Hauc5011f92019-10-11 09:52:07 -07001805} // namespace android