blob: 9618502b6b5aa81822d6764a707ae08976480456 [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
354TEST_F(BLASTBufferQueueTest, CreateBLASTBufferQueue) {
355 // create BLASTBufferQueue adapter associated with this surface
Valerie Hauda3446e2019-10-14 15:49:22 -0700356 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
Valerie Hauc5011f92019-10-11 09:52:07 -0700357 ASSERT_EQ(mSurfaceControl, adapter.getSurfaceControl());
Valerie Hauda3446e2019-10-14 15:49:22 -0700358 ASSERT_EQ(mDisplayWidth, adapter.getWidth());
359 ASSERT_EQ(mDisplayHeight, adapter.getHeight());
Tianhao Yao4861b102022-02-03 20:18:35 +0000360 ASSERT_EQ(nullptr, adapter.getTransactionReadyCallback());
Valerie Hauc5011f92019-10-11 09:52:07 -0700361}
362
363TEST_F(BLASTBufferQueueTest, Update) {
Valerie Hauda3446e2019-10-14 15:49:22 -0700364 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
Valerie Hauc5011f92019-10-11 09:52:07 -0700365 sp<SurfaceControl> updateSurface =
Valerie Hauda3446e2019-10-14 15:49:22 -0700366 mClient->createSurface(String8("UpdateTest"), mDisplayWidth / 2, mDisplayHeight / 2,
367 PIXEL_FORMAT_RGBA_8888);
368 adapter.update(updateSurface, mDisplayWidth / 2, mDisplayHeight / 2);
Valerie Hauc5011f92019-10-11 09:52:07 -0700369 ASSERT_EQ(updateSurface, adapter.getSurfaceControl());
Vishnu Nairea0de002020-11-17 17:42:37 -0800370 sp<IGraphicBufferProducer> igbProducer;
371 setUpProducer(adapter, igbProducer);
372
373 int32_t width;
374 igbProducer->query(NATIVE_WINDOW_WIDTH, &width);
375 ASSERT_EQ(mDisplayWidth / 2, width);
376 int32_t height;
377 igbProducer->query(NATIVE_WINDOW_HEIGHT, &height);
378 ASSERT_EQ(mDisplayHeight / 2, height);
Valerie Hauc5011f92019-10-11 09:52:07 -0700379}
380
Tianhao Yao4861b102022-02-03 20:18:35 +0000381TEST_F(BLASTBufferQueueTest, SyncNextTransaction) {
Valerie Hauda3446e2019-10-14 15:49:22 -0700382 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
Tianhao Yao4861b102022-02-03 20:18:35 +0000383 ASSERT_EQ(nullptr, adapter.getTransactionReadyCallback());
384 auto callback = [](Transaction*) {};
385 adapter.syncNextTransaction(callback);
386 ASSERT_NE(nullptr, adapter.getTransactionReadyCallback());
Valerie Hauc5011f92019-10-11 09:52:07 -0700387}
Valerie Hauda3446e2019-10-14 15:49:22 -0700388
Valerie Haubf29e042020-02-06 11:40:38 -0800389TEST_F(BLASTBufferQueueTest, DISABLED_onFrameAvailable_ApplyDesiredPresentTime) {
Valerie Hau181abd32020-01-27 14:18:28 -0800390 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
391 sp<IGraphicBufferProducer> igbProducer;
392 setUpProducer(adapter, igbProducer);
393
394 int slot;
395 sp<Fence> fence;
396 sp<GraphicBuffer> buf;
397 auto ret = igbProducer->dequeueBuffer(&slot, &fence, mDisplayWidth, mDisplayHeight,
398 PIXEL_FORMAT_RGBA_8888, GRALLOC_USAGE_SW_WRITE_OFTEN,
399 nullptr, nullptr);
400 ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, ret);
401 ASSERT_EQ(OK, igbProducer->requestBuffer(slot, &buf));
402
403 nsecs_t desiredPresentTime = systemTime() + nsecs_t(5 * 1e8);
404 IGraphicBufferProducer::QueueBufferOutput qbOutput;
Vishnu Nair1506b182021-02-22 14:35:15 -0800405 IGraphicBufferProducer::QueueBufferInput input(desiredPresentTime, true /* autotimestamp */,
406 HAL_DATASPACE_UNKNOWN,
Valerie Hau181abd32020-01-27 14:18:28 -0800407 Rect(mDisplayWidth, mDisplayHeight),
408 NATIVE_WINDOW_SCALING_MODE_FREEZE, 0,
409 Fence::NO_FENCE);
410 igbProducer->queueBuffer(slot, input, &qbOutput);
411 ASSERT_NE(ui::Transform::ROT_INVALID, qbOutput.transformHint);
412
413 adapter.waitForCallbacks();
414 ASSERT_GE(systemTime(), desiredPresentTime);
415}
416
Valerie Hauda3446e2019-10-14 15:49:22 -0700417TEST_F(BLASTBufferQueueTest, onFrameAvailable_Apply) {
418 uint8_t r = 255;
419 uint8_t g = 0;
420 uint8_t b = 0;
421
422 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
Valerie Haud3b90d22019-11-06 09:37:31 -0800423 sp<IGraphicBufferProducer> igbProducer;
424 setUpProducer(adapter, igbProducer);
Valerie Hauda3446e2019-10-14 15:49:22 -0700425
426 int slot;
427 sp<Fence> fence;
428 sp<GraphicBuffer> buf;
429 auto ret = igbProducer->dequeueBuffer(&slot, &fence, mDisplayWidth, mDisplayHeight,
430 PIXEL_FORMAT_RGBA_8888, GRALLOC_USAGE_SW_WRITE_OFTEN,
431 nullptr, nullptr);
432 ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, ret);
433 ASSERT_EQ(OK, igbProducer->requestBuffer(slot, &buf));
434
435 uint32_t* bufData;
436 buf->lock(static_cast<uint32_t>(GraphicBuffer::USAGE_SW_WRITE_OFTEN),
437 reinterpret_cast<void**>(&bufData));
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800438 fillBuffer(bufData, Rect(buf->getWidth(), buf->getHeight()), buf->getStride(), r, g, b);
Valerie Hauda3446e2019-10-14 15:49:22 -0700439 buf->unlock();
440
Valerie Haud3b90d22019-11-06 09:37:31 -0800441 IGraphicBufferProducer::QueueBufferOutput qbOutput;
Vishnu Nair1506b182021-02-22 14:35:15 -0800442 IGraphicBufferProducer::QueueBufferInput input(systemTime(), true /* autotimestamp */,
443 HAL_DATASPACE_UNKNOWN,
Valerie Hauda3446e2019-10-14 15:49:22 -0700444 Rect(mDisplayWidth, mDisplayHeight),
445 NATIVE_WINDOW_SCALING_MODE_FREEZE, 0,
446 Fence::NO_FENCE);
447 igbProducer->queueBuffer(slot, input, &qbOutput);
Dominik Laskowski718f9602019-11-09 20:01:35 -0800448 ASSERT_NE(ui::Transform::ROT_INVALID, qbOutput.transformHint);
Valerie Hauda3446e2019-10-14 15:49:22 -0700449
Patrick Williams0eb09c62023-07-28 11:24:35 -0500450 // ensure the buffer queue transaction has been committed
451 Transaction().apply(true /* synchronous */);
Valerie Hauda3446e2019-10-14 15:49:22 -0700452
453 // capture screen and verify that it is red
chaviw8ffc7b82020-08-18 11:25:37 -0700454 ASSERT_EQ(NO_ERROR, captureDisplay(mCaptureArgs, mCaptureResults));
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800455 ASSERT_NO_FATAL_FAILURE(
456 checkScreenCapture(r, g, b, {0, 0, (int32_t)mDisplayWidth, (int32_t)mDisplayHeight}));
Valerie Hauda3446e2019-10-14 15:49:22 -0700457}
Valerie Haud3b90d22019-11-06 09:37:31 -0800458
459TEST_F(BLASTBufferQueueTest, TripleBuffering) {
460 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
461 sp<IGraphicBufferProducer> igbProducer;
462 setUpProducer(adapter, igbProducer);
463
464 std::vector<std::pair<int, sp<Fence>>> allocated;
Ady Abraham0bde6b52021-05-18 13:57:02 -0700465 int minUndequeuedBuffers = 0;
466 ASSERT_EQ(OK, igbProducer->query(NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS, &minUndequeuedBuffers));
467 const auto bufferCount = minUndequeuedBuffers + 2;
468
469 for (int i = 0; i < bufferCount; i++) {
Valerie Haud3b90d22019-11-06 09:37:31 -0800470 int slot;
471 sp<Fence> fence;
472 sp<GraphicBuffer> buf;
473 auto ret = igbProducer->dequeueBuffer(&slot, &fence, mDisplayWidth, mDisplayHeight,
474 PIXEL_FORMAT_RGBA_8888, GRALLOC_USAGE_SW_WRITE_OFTEN,
475 nullptr, nullptr);
476 ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, ret);
477 ASSERT_EQ(OK, igbProducer->requestBuffer(slot, &buf));
478 allocated.push_back({slot, fence});
479 }
480 for (int i = 0; i < allocated.size(); i++) {
481 igbProducer->cancelBuffer(allocated[i].first, allocated[i].second);
482 }
483
Valerie Haua32c5522019-12-09 10:11:08 -0800484 for (int i = 0; i < 100; i++) {
Valerie Haud3b90d22019-11-06 09:37:31 -0800485 int slot;
486 sp<Fence> fence;
487 sp<GraphicBuffer> buf;
488 auto ret = igbProducer->dequeueBuffer(&slot, &fence, mDisplayWidth, mDisplayHeight,
489 PIXEL_FORMAT_RGBA_8888, GRALLOC_USAGE_SW_WRITE_OFTEN,
490 nullptr, nullptr);
491 ASSERT_EQ(NO_ERROR, ret);
492 IGraphicBufferProducer::QueueBufferOutput qbOutput;
Vishnu Nair1506b182021-02-22 14:35:15 -0800493 IGraphicBufferProducer::QueueBufferInput input(systemTime(), true /* autotimestamp */,
494 HAL_DATASPACE_UNKNOWN,
Valerie Haud3b90d22019-11-06 09:37:31 -0800495 Rect(mDisplayWidth, mDisplayHeight),
496 NATIVE_WINDOW_SCALING_MODE_FREEZE, 0,
497 Fence::NO_FENCE);
498 igbProducer->queueBuffer(slot, input, &qbOutput);
499 }
500 adapter.waitForCallbacks();
501}
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800502
503TEST_F(BLASTBufferQueueTest, SetCrop_Item) {
504 uint8_t r = 255;
505 uint8_t g = 0;
506 uint8_t b = 0;
507
508 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
509 sp<IGraphicBufferProducer> igbProducer;
510 setUpProducer(adapter, igbProducer);
511 int slot;
512 sp<Fence> fence;
513 sp<GraphicBuffer> buf;
514 auto ret = igbProducer->dequeueBuffer(&slot, &fence, mDisplayWidth, mDisplayHeight,
515 PIXEL_FORMAT_RGBA_8888, GRALLOC_USAGE_SW_WRITE_OFTEN,
516 nullptr, nullptr);
517 ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, ret);
518 ASSERT_EQ(OK, igbProducer->requestBuffer(slot, &buf));
519
520 uint32_t* bufData;
521 buf->lock(static_cast<uint32_t>(GraphicBuffer::USAGE_SW_WRITE_OFTEN),
522 reinterpret_cast<void**>(&bufData));
523 fillBuffer(bufData, Rect(buf->getWidth(), buf->getHeight() / 2), buf->getStride(), r, g, b);
524 buf->unlock();
525
526 IGraphicBufferProducer::QueueBufferOutput qbOutput;
Vishnu Nair1506b182021-02-22 14:35:15 -0800527 IGraphicBufferProducer::QueueBufferInput input(systemTime(), true /* autotimestamp */,
528 HAL_DATASPACE_UNKNOWN,
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800529 Rect(mDisplayWidth, mDisplayHeight / 2),
530 NATIVE_WINDOW_SCALING_MODE_FREEZE, 0,
531 Fence::NO_FENCE);
532 igbProducer->queueBuffer(slot, input, &qbOutput);
Dominik Laskowski718f9602019-11-09 20:01:35 -0800533 ASSERT_NE(ui::Transform::ROT_INVALID, qbOutput.transformHint);
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800534
Patrick Williams0eb09c62023-07-28 11:24:35 -0500535 // ensure the buffer queue transaction has been committed
536 Transaction().apply(true /* synchronous */);
537
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800538 // capture screen and verify that it is red
chaviw8ffc7b82020-08-18 11:25:37 -0700539 ASSERT_EQ(NO_ERROR, captureDisplay(mCaptureArgs, mCaptureResults));
chaviwd2432892020-07-24 17:42:39 -0700540
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800541 ASSERT_NO_FATAL_FAILURE(
Chavi Weingartena5aedbd2021-04-09 13:37:33 +0000542 checkScreenCapture(r, g, b,
543 {0, 0, (int32_t)mDisplayWidth, (int32_t)mDisplayHeight / 2}));
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800544}
545
546TEST_F(BLASTBufferQueueTest, SetCrop_ScalingModeScaleCrop) {
547 uint8_t r = 255;
548 uint8_t g = 0;
549 uint8_t b = 0;
550
551 int32_t bufferSideLength =
552 (mDisplayWidth < mDisplayHeight) ? mDisplayWidth / 2 : mDisplayHeight / 2;
553 int32_t finalCropSideLength = bufferSideLength / 2;
554
555 auto bg = mClient->createSurface(String8("BGTest"), 0, 0, PIXEL_FORMAT_RGBA_8888,
Vishnu Nairfa247b12020-02-11 08:58:26 -0800556 ISurfaceComposerClient::eFXSurfaceEffect);
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800557 ASSERT_NE(nullptr, bg.get());
558 Transaction t;
Dominik Laskowski29fa1462021-04-27 15:51:50 -0700559 t.setLayerStack(bg, ui::DEFAULT_LAYER_STACK)
chaviw25714502021-02-11 10:01:08 -0800560 .setCrop(bg, Rect(0, 0, mDisplayWidth, mDisplayHeight))
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800561 .setColor(bg, half3{0, 0, 0})
562 .setLayer(bg, 0)
563 .apply();
564
565 BLASTBufferQueueHelper adapter(mSurfaceControl, bufferSideLength, bufferSideLength);
566 sp<IGraphicBufferProducer> igbProducer;
567 setUpProducer(adapter, igbProducer);
568 int slot;
569 sp<Fence> fence;
570 sp<GraphicBuffer> buf;
571 auto ret = igbProducer->dequeueBuffer(&slot, &fence, bufferSideLength, bufferSideLength,
572 PIXEL_FORMAT_RGBA_8888, GRALLOC_USAGE_SW_WRITE_OFTEN,
573 nullptr, nullptr);
574 ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, ret);
575 ASSERT_EQ(OK, igbProducer->requestBuffer(slot, &buf));
576
577 uint32_t* bufData;
578 buf->lock(static_cast<uint32_t>(GraphicBuffer::USAGE_SW_WRITE_OFTEN),
579 reinterpret_cast<void**>(&bufData));
580 fillBuffer(bufData, Rect(buf->getWidth(), buf->getHeight()), buf->getStride(), 0, 0, 0);
581 fillBuffer(bufData,
582 Rect(finalCropSideLength / 2, 0, buf->getWidth() - finalCropSideLength / 2,
583 buf->getHeight()),
584 buf->getStride(), r, g, b);
585 buf->unlock();
586
587 IGraphicBufferProducer::QueueBufferOutput qbOutput;
Vishnu Nair1506b182021-02-22 14:35:15 -0800588 IGraphicBufferProducer::QueueBufferInput input(systemTime(), true /* autotimestamp */,
589 HAL_DATASPACE_UNKNOWN,
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800590 Rect(bufferSideLength, finalCropSideLength),
591 NATIVE_WINDOW_SCALING_MODE_SCALE_CROP, 0,
592 Fence::NO_FENCE);
593 igbProducer->queueBuffer(slot, input, &qbOutput);
Dominik Laskowski718f9602019-11-09 20:01:35 -0800594 ASSERT_NE(ui::Transform::ROT_INVALID, qbOutput.transformHint);
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800595
Patrick Williams0eb09c62023-07-28 11:24:35 -0500596 // ensure the buffer queue transaction has been committed
597 Transaction().apply(true /* synchronous */);
598
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800599 // capture screen and verify that it is red
chaviw8ffc7b82020-08-18 11:25:37 -0700600 ASSERT_EQ(NO_ERROR, captureDisplay(mCaptureArgs, mCaptureResults));
Vishnu Nair5cc9ac02021-04-19 13:23:38 -0700601 ASSERT_NO_FATAL_FAILURE(checkScreenCapture(r, g, b,
602 {10, 10, (int32_t)bufferSideLength - 10,
603 (int32_t)bufferSideLength - 10}));
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800604 ASSERT_NO_FATAL_FAILURE(
Vishnu Nair5cc9ac02021-04-19 13:23:38 -0700605 checkScreenCapture(0, 0, 0,
606 {0, 0, (int32_t)bufferSideLength, (int32_t)bufferSideLength},
607 /*border*/ 0, /*outsideRegion*/ true));
608}
609
610TEST_F(BLASTBufferQueueTest, ScaleCroppedBufferToBufferSize) {
611 // add black background
612 auto bg = mClient->createSurface(String8("BGTest"), 0, 0, PIXEL_FORMAT_RGBA_8888,
613 ISurfaceComposerClient::eFXSurfaceEffect);
614 ASSERT_NE(nullptr, bg.get());
615 Transaction t;
Dominik Laskowski29fa1462021-04-27 15:51:50 -0700616 t.setLayerStack(bg, ui::DEFAULT_LAYER_STACK)
Vishnu Nair5cc9ac02021-04-19 13:23:38 -0700617 .setCrop(bg, Rect(0, 0, mDisplayWidth, mDisplayHeight))
618 .setColor(bg, half3{0, 0, 0})
619 .setLayer(bg, 0)
620 .apply();
621
622 Rect windowSize(1000, 1000);
623 Rect bufferSize(windowSize);
624 Rect bufferCrop(200, 200, 700, 700);
625
626 BLASTBufferQueueHelper adapter(mSurfaceControl, windowSize.getWidth(), windowSize.getHeight());
627 sp<IGraphicBufferProducer> igbProducer;
628 setUpProducer(adapter, igbProducer);
629 int slot;
630 sp<Fence> fence;
631 sp<GraphicBuffer> buf;
632 auto ret = igbProducer->dequeueBuffer(&slot, &fence, bufferSize.getWidth(),
633 bufferSize.getHeight(), PIXEL_FORMAT_RGBA_8888,
634 GRALLOC_USAGE_SW_WRITE_OFTEN, nullptr, nullptr);
635 ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, ret);
636 ASSERT_EQ(OK, igbProducer->requestBuffer(slot, &buf));
637
638 uint32_t* bufData;
639 buf->lock(static_cast<uint32_t>(GraphicBuffer::USAGE_SW_WRITE_OFTEN),
640 reinterpret_cast<void**>(&bufData));
641 // fill buffer with grey
642 fillBuffer(bufData, bufferSize, buf->getStride(), 127, 127, 127);
643
644 // fill crop area with different colors so we can verify the cropped region has been scaled
645 // correctly.
646 fillBuffer(bufData, Rect(200, 200, 450, 450), buf->getStride(), /* rgb */ 255, 0, 0);
647 fillBuffer(bufData, Rect(200, 451, 450, 700), buf->getStride(), /* rgb */ 0, 255, 0);
648 fillBuffer(bufData, Rect(451, 200, 700, 450), buf->getStride(), /* rgb */ 0, 0, 255);
649 fillBuffer(bufData, Rect(451, 451, 700, 700), buf->getStride(), /* rgb */ 255, 0, 0);
650 buf->unlock();
651
652 IGraphicBufferProducer::QueueBufferOutput qbOutput;
653 IGraphicBufferProducer::QueueBufferInput input(systemTime(), true /* autotimestamp */,
654 HAL_DATASPACE_UNKNOWN,
655 bufferCrop /* Rect::INVALID_RECT */,
656 NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW, 0,
657 Fence::NO_FENCE);
658 igbProducer->queueBuffer(slot, input, &qbOutput);
659 ASSERT_NE(ui::Transform::ROT_INVALID, qbOutput.transformHint);
660
Patrick Williams0eb09c62023-07-28 11:24:35 -0500661 // ensure the buffer queue transaction has been committed
662 Transaction().apply(true /* synchronous */);
Vishnu Nair5cc9ac02021-04-19 13:23:38 -0700663
664 ASSERT_EQ(NO_ERROR, captureDisplay(mCaptureArgs, mCaptureResults));
665
666 // Verify cropped region is scaled correctly.
667 ASSERT_NO_FATAL_FAILURE(checkScreenCapture(255, 0, 0, {10, 10, 490, 490}));
668 ASSERT_NO_FATAL_FAILURE(checkScreenCapture(0, 255, 0, {10, 510, 490, 990}));
669 ASSERT_NO_FATAL_FAILURE(checkScreenCapture(0, 0, 255, {510, 10, 990, 490}));
670 ASSERT_NO_FATAL_FAILURE(checkScreenCapture(255, 0, 0, {510, 510, 990, 990}));
671 // Verify outside region is black.
672 ASSERT_NO_FATAL_FAILURE(checkScreenCapture(0, 0, 0,
673 {0, 0, (int32_t)windowSize.getWidth(),
674 (int32_t)windowSize.getHeight()},
675 /*border*/ 0, /*outsideRegion*/ true));
676}
677
678TEST_F(BLASTBufferQueueTest, ScaleCroppedBufferToWindowSize) {
679 // add black background
680 auto bg = mClient->createSurface(String8("BGTest"), 0, 0, PIXEL_FORMAT_RGBA_8888,
681 ISurfaceComposerClient::eFXSurfaceEffect);
682 ASSERT_NE(nullptr, bg.get());
683 Transaction t;
Dominik Laskowski29fa1462021-04-27 15:51:50 -0700684 t.setLayerStack(bg, ui::DEFAULT_LAYER_STACK)
Vishnu Nair5cc9ac02021-04-19 13:23:38 -0700685 .setCrop(bg, Rect(0, 0, mDisplayWidth, mDisplayHeight))
686 .setColor(bg, half3{0, 0, 0})
687 .setLayer(bg, 0)
688 .apply();
689
690 Rect windowSize(1000, 1000);
691 Rect bufferSize(500, 500);
692 Rect bufferCrop(100, 100, 350, 350);
693
694 BLASTBufferQueueHelper adapter(mSurfaceControl, windowSize.getWidth(), windowSize.getHeight());
695 sp<IGraphicBufferProducer> igbProducer;
696 setUpProducer(adapter, igbProducer);
697 int slot;
698 sp<Fence> fence;
699 sp<GraphicBuffer> buf;
700 auto ret = igbProducer->dequeueBuffer(&slot, &fence, bufferSize.getWidth(),
701 bufferSize.getHeight(), PIXEL_FORMAT_RGBA_8888,
702 GRALLOC_USAGE_SW_WRITE_OFTEN, nullptr, nullptr);
703 ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, ret);
704 ASSERT_EQ(OK, igbProducer->requestBuffer(slot, &buf));
705
706 uint32_t* bufData;
707 buf->lock(static_cast<uint32_t>(GraphicBuffer::USAGE_SW_WRITE_OFTEN),
708 reinterpret_cast<void**>(&bufData));
709 // fill buffer with grey
710 fillBuffer(bufData, bufferSize, buf->getStride(), 127, 127, 127);
711
712 // fill crop area with different colors so we can verify the cropped region has been scaled
713 // correctly.
714 fillBuffer(bufData, Rect(100, 100, 225, 225), buf->getStride(), /* rgb */ 255, 0, 0);
715 fillBuffer(bufData, Rect(100, 226, 225, 350), buf->getStride(), /* rgb */ 0, 255, 0);
716 fillBuffer(bufData, Rect(226, 100, 350, 225), buf->getStride(), /* rgb */ 0, 0, 255);
717 fillBuffer(bufData, Rect(226, 226, 350, 350), buf->getStride(), /* rgb */ 255, 0, 0);
718 buf->unlock();
719
720 IGraphicBufferProducer::QueueBufferOutput qbOutput;
721 IGraphicBufferProducer::QueueBufferInput input(systemTime(), true /* autotimestamp */,
722 HAL_DATASPACE_UNKNOWN,
723 bufferCrop /* Rect::INVALID_RECT */,
724 NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW, 0,
725 Fence::NO_FENCE);
726 igbProducer->queueBuffer(slot, input, &qbOutput);
727 ASSERT_NE(ui::Transform::ROT_INVALID, qbOutput.transformHint);
728
Patrick Williams0eb09c62023-07-28 11:24:35 -0500729 // ensure the buffer queue transaction has been committed
730 Transaction().apply(true /* synchronous */);
Vishnu Nair5cc9ac02021-04-19 13:23:38 -0700731
732 ASSERT_EQ(NO_ERROR, captureDisplay(mCaptureArgs, mCaptureResults));
733 // Verify cropped region is scaled correctly.
734 ASSERT_NO_FATAL_FAILURE(checkScreenCapture(255, 0, 0, {10, 10, 490, 490}));
735 ASSERT_NO_FATAL_FAILURE(checkScreenCapture(0, 255, 0, {10, 510, 490, 990}));
736 ASSERT_NO_FATAL_FAILURE(checkScreenCapture(0, 0, 255, {510, 10, 990, 490}));
737 ASSERT_NO_FATAL_FAILURE(checkScreenCapture(255, 0, 0, {510, 510, 990, 990}));
738 // Verify outside region is black.
739 ASSERT_NO_FATAL_FAILURE(checkScreenCapture(0, 0, 0,
740 {0, 0, (int32_t)windowSize.getWidth(),
741 (int32_t)windowSize.getHeight()},
742 /*border*/ 0, /*outsideRegion*/ true));
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800743}
744
Vishnu Nair932f6ae2021-09-29 17:33:10 -0700745// b/196339769 verify we can can update the requested size while the in FREEZE scaling mode and
746// scale the buffer properly when the mode changes to SCALE_TO_WINDOW
747TEST_F(BLASTBufferQueueTest, ScalingModeChanges) {
748 uint8_t r = 255;
749 uint8_t g = 0;
750 uint8_t b = 0;
751
752 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight / 4);
753 sp<IGraphicBufferProducer> igbProducer;
754 setUpProducer(adapter, igbProducer);
755 {
756 int slot;
757 sp<Fence> fence;
758 sp<GraphicBuffer> buf;
759 auto ret = igbProducer->dequeueBuffer(&slot, &fence, mDisplayWidth, mDisplayHeight / 4,
760 PIXEL_FORMAT_RGBA_8888, GRALLOC_USAGE_SW_WRITE_OFTEN,
761 nullptr, nullptr);
762 ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, ret);
763 ASSERT_EQ(OK, igbProducer->requestBuffer(slot, &buf));
764
765 uint32_t* bufData;
766 buf->lock(static_cast<uint32_t>(GraphicBuffer::USAGE_SW_WRITE_OFTEN),
767 reinterpret_cast<void**>(&bufData));
768 fillBuffer(bufData, Rect(buf->getWidth(), buf->getHeight()), buf->getStride(), r, g, b);
769 buf->unlock();
770
771 IGraphicBufferProducer::QueueBufferOutput qbOutput;
772 IGraphicBufferProducer::QueueBufferInput input(systemTime(), true /* autotimestamp */,
773 HAL_DATASPACE_UNKNOWN, {},
774 NATIVE_WINDOW_SCALING_MODE_FREEZE, 0,
775 Fence::NO_FENCE);
776 igbProducer->queueBuffer(slot, input, &qbOutput);
Patrick Williams0eb09c62023-07-28 11:24:35 -0500777
778 // ensure the buffer queue transaction has been committed
779 Transaction().apply(true /* synchronous */);
Vishnu Nair932f6ae2021-09-29 17:33:10 -0700780 }
781 // capture screen and verify that it is red
782 ASSERT_EQ(NO_ERROR, captureDisplay(mCaptureArgs, mCaptureResults));
783
784 ASSERT_NO_FATAL_FAILURE(
785 checkScreenCapture(r, g, b,
786 {0, 0, (int32_t)mDisplayWidth, (int32_t)mDisplayHeight / 4}));
787
788 // update the size to half the display and dequeue a buffer quarter of the display.
789 adapter.update(mSurfaceControl, mDisplayWidth, mDisplayHeight / 2);
790
791 {
792 int slot;
793 sp<Fence> fence;
794 sp<GraphicBuffer> buf;
795 auto ret = igbProducer->dequeueBuffer(&slot, &fence, mDisplayWidth, mDisplayHeight / 8,
796 PIXEL_FORMAT_RGBA_8888, GRALLOC_USAGE_SW_WRITE_OFTEN,
797 nullptr, nullptr);
798 ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, ret);
799 ASSERT_EQ(OK, igbProducer->requestBuffer(slot, &buf));
800
801 uint32_t* bufData;
802 buf->lock(static_cast<uint32_t>(GraphicBuffer::USAGE_SW_WRITE_OFTEN),
803 reinterpret_cast<void**>(&bufData));
804 g = 255;
805 fillBuffer(bufData, Rect(buf->getWidth(), buf->getHeight()), buf->getStride(), r, g, b);
806 buf->unlock();
807
808 IGraphicBufferProducer::QueueBufferOutput qbOutput;
809 IGraphicBufferProducer::QueueBufferInput input(systemTime(), true /* autotimestamp */,
810 HAL_DATASPACE_UNKNOWN, {},
811 NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW,
812 0, Fence::NO_FENCE);
813 igbProducer->queueBuffer(slot, input, &qbOutput);
Patrick Williams0eb09c62023-07-28 11:24:35 -0500814 // ensure the buffer queue transaction has been committed
815 Transaction().apply(true /* synchronous */);
Vishnu Nair932f6ae2021-09-29 17:33:10 -0700816 }
817 // capture screen and verify that it is red
818 ASSERT_EQ(NO_ERROR, captureDisplay(mCaptureArgs, mCaptureResults));
819 // verify we still scale the buffer to the new size (half the screen height)
820 ASSERT_NO_FATAL_FAILURE(
821 checkScreenCapture(r, g, b,
822 {0, 0, (int32_t)mDisplayWidth, (int32_t)mDisplayHeight / 2}));
823}
824
chaviwd7deef72021-10-06 11:53:40 -0500825TEST_F(BLASTBufferQueueTest, SyncThenNoSync) {
826 uint8_t r = 255;
827 uint8_t g = 0;
828 uint8_t b = 0;
829
830 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
831
832 sp<IGraphicBufferProducer> igbProducer;
833 setUpProducer(adapter, igbProducer);
834
chaviwa1c4c822021-11-10 18:11:58 -0600835 Transaction sync;
Tianhao Yao4861b102022-02-03 20:18:35 +0000836 adapter.setSyncTransaction(sync);
chaviwd7deef72021-10-06 11:53:40 -0500837 queueBuffer(igbProducer, 0, 255, 0, 0);
838
839 // queue non sync buffer, so this one should get blocked
840 // Add a present delay to allow the first screenshot to get taken.
841 nsecs_t presentTimeDelay = std::chrono::nanoseconds(500ms).count();
842 queueBuffer(igbProducer, r, g, b, presentTimeDelay);
843
844 CallbackHelper transactionCallback;
chaviwa1c4c822021-11-10 18:11:58 -0600845 sync.addTransactionCompletedCallback(transactionCallback.function,
chaviwd7deef72021-10-06 11:53:40 -0500846 transactionCallback.getContext())
847 .apply();
848
849 CallbackData callbackData;
850 transactionCallback.getCallbackData(&callbackData);
851
chaviw0acd33a2021-11-02 11:55:37 -0500852 // capture screen and verify that it is green
chaviwd7deef72021-10-06 11:53:40 -0500853 ASSERT_EQ(NO_ERROR, captureDisplay(mCaptureArgs, mCaptureResults));
854 ASSERT_NO_FATAL_FAILURE(
855 checkScreenCapture(0, 255, 0, {0, 0, (int32_t)mDisplayWidth, (int32_t)mDisplayHeight}));
856
857 mProducerListener->waitOnNumberReleased(1);
858 ASSERT_EQ(NO_ERROR, captureDisplay(mCaptureArgs, mCaptureResults));
859 ASSERT_NO_FATAL_FAILURE(
860 checkScreenCapture(r, g, b, {0, 0, (int32_t)mDisplayWidth, (int32_t)mDisplayHeight}));
861}
862
863TEST_F(BLASTBufferQueueTest, MultipleSyncTransactions) {
864 uint8_t r = 255;
865 uint8_t g = 0;
866 uint8_t b = 0;
867
868 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
869
870 sp<IGraphicBufferProducer> igbProducer;
871 setUpProducer(adapter, igbProducer);
872
873 Transaction mainTransaction;
874
chaviwa1c4c822021-11-10 18:11:58 -0600875 Transaction sync;
Tianhao Yao4861b102022-02-03 20:18:35 +0000876 adapter.setSyncTransaction(sync);
chaviwd7deef72021-10-06 11:53:40 -0500877 queueBuffer(igbProducer, 0, 255, 0, 0);
878
chaviwa1c4c822021-11-10 18:11:58 -0600879 mainTransaction.merge(std::move(sync));
chaviwd7deef72021-10-06 11:53:40 -0500880
Tianhao Yao4861b102022-02-03 20:18:35 +0000881 adapter.setSyncTransaction(sync);
chaviwd7deef72021-10-06 11:53:40 -0500882 queueBuffer(igbProducer, r, g, b, 0);
883
chaviwa1c4c822021-11-10 18:11:58 -0600884 mainTransaction.merge(std::move(sync));
chaviwd7deef72021-10-06 11:53:40 -0500885 // Expect 1 buffer to be released even before sending to SurfaceFlinger
886 mProducerListener->waitOnNumberReleased(1);
887
888 CallbackHelper transactionCallback;
889 mainTransaction
890 .addTransactionCompletedCallback(transactionCallback.function,
891 transactionCallback.getContext())
892 .apply();
893
894 CallbackData callbackData;
895 transactionCallback.getCallbackData(&callbackData);
896
897 // capture screen and verify that it is red
898 ASSERT_EQ(NO_ERROR, captureDisplay(mCaptureArgs, mCaptureResults));
899 ASSERT_NO_FATAL_FAILURE(
900 checkScreenCapture(r, g, b, {0, 0, (int32_t)mDisplayWidth, (int32_t)mDisplayHeight}));
901}
902
903TEST_F(BLASTBufferQueueTest, MultipleSyncTransactionWithNonSync) {
904 uint8_t r = 255;
905 uint8_t g = 0;
906 uint8_t b = 0;
907
908 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
909
910 sp<IGraphicBufferProducer> igbProducer;
911 setUpProducer(adapter, igbProducer);
912
913 Transaction mainTransaction;
914
chaviwa1c4c822021-11-10 18:11:58 -0600915 Transaction sync;
chaviwd7deef72021-10-06 11:53:40 -0500916 // queue a sync transaction
Tianhao Yao4861b102022-02-03 20:18:35 +0000917 adapter.setSyncTransaction(sync);
chaviwd7deef72021-10-06 11:53:40 -0500918 queueBuffer(igbProducer, 0, 255, 0, 0);
919
chaviwa1c4c822021-11-10 18:11:58 -0600920 mainTransaction.merge(std::move(sync));
chaviwd7deef72021-10-06 11:53:40 -0500921
chaviwa1c4c822021-11-10 18:11:58 -0600922 // queue another buffer without setting sync transaction
chaviwd7deef72021-10-06 11:53:40 -0500923 queueBuffer(igbProducer, 0, 0, 255, 0);
924
925 // queue another sync transaction
Tianhao Yao4861b102022-02-03 20:18:35 +0000926 adapter.setSyncTransaction(sync);
chaviwd7deef72021-10-06 11:53:40 -0500927 queueBuffer(igbProducer, r, g, b, 0);
928 // Expect 1 buffer to be released because the non sync transaction should merge
929 // with the sync
930 mProducerListener->waitOnNumberReleased(1);
931
chaviwa1c4c822021-11-10 18:11:58 -0600932 mainTransaction.merge(std::move(sync));
chaviwd7deef72021-10-06 11:53:40 -0500933 // Expect 2 buffers to be released due to merging the two syncs.
934 mProducerListener->waitOnNumberReleased(2);
935
936 CallbackHelper transactionCallback;
937 mainTransaction
938 .addTransactionCompletedCallback(transactionCallback.function,
939 transactionCallback.getContext())
940 .apply();
941
942 CallbackData callbackData;
943 transactionCallback.getCallbackData(&callbackData);
944
945 // capture screen and verify that it is red
946 ASSERT_EQ(NO_ERROR, captureDisplay(mCaptureArgs, mCaptureResults));
947 ASSERT_NO_FATAL_FAILURE(
948 checkScreenCapture(r, g, b, {0, 0, (int32_t)mDisplayWidth, (int32_t)mDisplayHeight}));
949}
950
951TEST_F(BLASTBufferQueueTest, MultipleSyncRunOutOfBuffers) {
952 uint8_t r = 255;
953 uint8_t g = 0;
954 uint8_t b = 0;
955
956 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
957
958 sp<IGraphicBufferProducer> igbProducer;
959 setUpProducer(adapter, igbProducer, 3);
960
961 Transaction mainTransaction;
962
chaviwa1c4c822021-11-10 18:11:58 -0600963 Transaction sync;
chaviwd7deef72021-10-06 11:53:40 -0500964 // queue a sync transaction
Tianhao Yao4861b102022-02-03 20:18:35 +0000965 adapter.setSyncTransaction(sync);
chaviwd7deef72021-10-06 11:53:40 -0500966 queueBuffer(igbProducer, 0, 255, 0, 0);
967
chaviwa1c4c822021-11-10 18:11:58 -0600968 mainTransaction.merge(std::move(sync));
chaviwd7deef72021-10-06 11:53:40 -0500969
chaviwa1c4c822021-11-10 18:11:58 -0600970 // queue a few buffers without setting sync transaction
chaviwd7deef72021-10-06 11:53:40 -0500971 queueBuffer(igbProducer, 0, 0, 255, 0);
972 queueBuffer(igbProducer, 0, 0, 255, 0);
973 queueBuffer(igbProducer, 0, 0, 255, 0);
974
975 // queue another sync transaction
Tianhao Yao4861b102022-02-03 20:18:35 +0000976 adapter.setSyncTransaction(sync);
chaviwd7deef72021-10-06 11:53:40 -0500977 queueBuffer(igbProducer, r, g, b, 0);
978 // Expect 3 buffers to be released because the non sync transactions should merge
979 // with the sync
980 mProducerListener->waitOnNumberReleased(3);
981
chaviwa1c4c822021-11-10 18:11:58 -0600982 mainTransaction.merge(std::move(sync));
chaviwd7deef72021-10-06 11:53:40 -0500983 // Expect 4 buffers to be released due to merging the two syncs.
984 mProducerListener->waitOnNumberReleased(4);
985
986 CallbackHelper transactionCallback;
987 mainTransaction
988 .addTransactionCompletedCallback(transactionCallback.function,
989 transactionCallback.getContext())
990 .apply();
991
992 CallbackData callbackData;
993 transactionCallback.getCallbackData(&callbackData);
994
995 // capture screen and verify that it is red
996 ASSERT_EQ(NO_ERROR, captureDisplay(mCaptureArgs, mCaptureResults));
997 ASSERT_NO_FATAL_FAILURE(
998 checkScreenCapture(r, g, b, {0, 0, (int32_t)mDisplayWidth, (int32_t)mDisplayHeight}));
999}
1000
1001// Tests BBQ with a sync transaction when the buffers acquired reaches max and the only way to
1002// continue processing is for a release callback from SurfaceFlinger.
1003// This is done by sending a buffer to SF so it can release the previous one and allow BBQ to
1004// continue acquiring buffers.
1005TEST_F(BLASTBufferQueueTest, RunOutOfBuffersWaitingOnSF) {
1006 uint8_t r = 255;
1007 uint8_t g = 0;
1008 uint8_t b = 0;
1009
1010 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
1011
1012 sp<IGraphicBufferProducer> igbProducer;
1013 setUpProducer(adapter, igbProducer, 4);
1014
1015 Transaction mainTransaction;
1016
1017 // Send a buffer to SF
1018 queueBuffer(igbProducer, 0, 255, 0, 0);
1019
chaviwa1c4c822021-11-10 18:11:58 -06001020 Transaction sync;
chaviwd7deef72021-10-06 11:53:40 -05001021 // queue a sync transaction
Tianhao Yao4861b102022-02-03 20:18:35 +00001022 adapter.setSyncTransaction(sync);
chaviwd7deef72021-10-06 11:53:40 -05001023 queueBuffer(igbProducer, 0, 255, 0, 0);
1024
chaviwa1c4c822021-11-10 18:11:58 -06001025 mainTransaction.merge(std::move(sync));
chaviwd7deef72021-10-06 11:53:40 -05001026
chaviwa1c4c822021-11-10 18:11:58 -06001027 // queue a few buffers without setting sync transaction
chaviwd7deef72021-10-06 11:53:40 -05001028 queueBuffer(igbProducer, 0, 0, 255, 0);
1029 queueBuffer(igbProducer, 0, 0, 255, 0);
1030 queueBuffer(igbProducer, 0, 0, 255, 0);
1031
1032 // apply the first synced buffer to ensure we have to wait on SF
1033 mainTransaction.apply();
1034
1035 // queue another sync transaction
Tianhao Yao4861b102022-02-03 20:18:35 +00001036 adapter.setSyncTransaction(sync);
chaviwd7deef72021-10-06 11:53:40 -05001037 queueBuffer(igbProducer, r, g, b, 0);
1038 // Expect 2 buffers to be released because the non sync transactions should merge
1039 // with the sync
1040 mProducerListener->waitOnNumberReleased(3);
1041
chaviwa1c4c822021-11-10 18:11:58 -06001042 mainTransaction.merge(std::move(sync));
chaviwd7deef72021-10-06 11:53:40 -05001043
1044 CallbackHelper transactionCallback;
1045 mainTransaction
1046 .addTransactionCompletedCallback(transactionCallback.function,
1047 transactionCallback.getContext())
1048 .apply();
1049
1050 CallbackData callbackData;
1051 transactionCallback.getCallbackData(&callbackData);
1052
1053 // capture screen and verify that it is red
1054 ASSERT_EQ(NO_ERROR, captureDisplay(mCaptureArgs, mCaptureResults));
1055 ASSERT_NO_FATAL_FAILURE(
1056 checkScreenCapture(r, g, b, {0, 0, (int32_t)mDisplayWidth, (int32_t)mDisplayHeight}));
1057}
1058
Tianhao Yao4861b102022-02-03 20:18:35 +00001059TEST_F(BLASTBufferQueueTest, SyncNextTransactionAcquireMultipleBuffers) {
chaviw0acd33a2021-11-02 11:55:37 -05001060 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
1061
1062 sp<IGraphicBufferProducer> igbProducer;
1063 setUpProducer(adapter, igbProducer);
1064
1065 Transaction next;
Tianhao Yao4861b102022-02-03 20:18:35 +00001066 adapter.setSyncTransaction(next, false);
chaviw0acd33a2021-11-02 11:55:37 -05001067 queueBuffer(igbProducer, 0, 255, 0, 0);
1068 queueBuffer(igbProducer, 0, 0, 255, 0);
1069 // There should only be one frame submitted since the first frame will be released.
1070 adapter.validateNumFramesSubmitted(1);
Tianhao Yao4861b102022-02-03 20:18:35 +00001071 adapter.stopContinuousSyncTransaction();
chaviw0acd33a2021-11-02 11:55:37 -05001072
1073 // queue non sync buffer, so this one should get blocked
1074 // Add a present delay to allow the first screenshot to get taken.
1075 nsecs_t presentTimeDelay = std::chrono::nanoseconds(500ms).count();
1076 queueBuffer(igbProducer, 255, 0, 0, presentTimeDelay);
1077
1078 CallbackHelper transactionCallback;
1079 next.addTransactionCompletedCallback(transactionCallback.function,
1080 transactionCallback.getContext())
1081 .apply();
1082
1083 CallbackData callbackData;
1084 transactionCallback.getCallbackData(&callbackData);
1085
1086 // capture screen and verify that it is blue
1087 ASSERT_EQ(NO_ERROR, captureDisplay(mCaptureArgs, mCaptureResults));
1088 ASSERT_NO_FATAL_FAILURE(
1089 checkScreenCapture(0, 0, 255, {0, 0, (int32_t)mDisplayWidth, (int32_t)mDisplayHeight}));
1090
1091 mProducerListener->waitOnNumberReleased(2);
1092 // capture screen and verify that it is red
1093 ASSERT_EQ(NO_ERROR, captureDisplay(mCaptureArgs, mCaptureResults));
1094 ASSERT_NO_FATAL_FAILURE(
1095 checkScreenCapture(255, 0, 0, {0, 0, (int32_t)mDisplayWidth, (int32_t)mDisplayHeight}));
1096}
1097
chaviw3b4bdcf2022-03-17 09:27:03 -05001098TEST_F(BLASTBufferQueueTest, SyncNextTransactionOverwrite) {
1099 std::mutex mutex;
1100 std::condition_variable callbackReceivedCv;
1101 bool receivedCallback = false;
1102
1103 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
1104 ASSERT_EQ(nullptr, adapter.getTransactionReadyCallback());
1105 auto callback = [&](Transaction*) {
1106 std::unique_lock<std::mutex> lock(mutex);
1107 receivedCallback = true;
1108 callbackReceivedCv.notify_one();
1109 };
1110 adapter.syncNextTransaction(callback);
1111 ASSERT_NE(nullptr, adapter.getTransactionReadyCallback());
1112
1113 auto callback2 = [](Transaction*) {};
Chavi Weingartenc398c012023-04-12 17:26:02 +00001114 ASSERT_FALSE(adapter.syncNextTransaction(callback2));
1115
1116 sp<IGraphicBufferProducer> igbProducer;
1117 setUpProducer(adapter, igbProducer);
1118 queueBuffer(igbProducer, 0, 255, 0, 0);
chaviw3b4bdcf2022-03-17 09:27:03 -05001119
1120 std::unique_lock<std::mutex> lock(mutex);
1121 if (!receivedCallback) {
1122 ASSERT_NE(callbackReceivedCv.wait_for(lock, std::chrono::seconds(3)),
1123 std::cv_status::timeout)
1124 << "did not receive callback";
1125 }
1126
1127 ASSERT_TRUE(receivedCallback);
1128}
1129
Chavi Weingartenc398c012023-04-12 17:26:02 +00001130TEST_F(BLASTBufferQueueTest, ClearSyncTransaction) {
1131 std::mutex mutex;
1132 std::condition_variable callbackReceivedCv;
1133 bool receivedCallback = false;
1134
1135 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
1136 ASSERT_EQ(nullptr, adapter.getTransactionReadyCallback());
1137 auto callback = [&](Transaction*) {
1138 std::unique_lock<std::mutex> lock(mutex);
1139 receivedCallback = true;
1140 callbackReceivedCv.notify_one();
1141 };
1142 adapter.syncNextTransaction(callback);
1143 ASSERT_NE(nullptr, adapter.getTransactionReadyCallback());
1144
1145 adapter.clearSyncTransaction();
1146
1147 sp<IGraphicBufferProducer> igbProducer;
1148 setUpProducer(adapter, igbProducer);
1149 queueBuffer(igbProducer, 0, 255, 0, 0);
1150
1151 std::unique_lock<std::mutex> lock(mutex);
1152 if (!receivedCallback) {
1153 ASSERT_EQ(callbackReceivedCv.wait_for(lock, std::chrono::seconds(3)),
1154 std::cv_status::timeout)
1155 << "did not receive callback";
1156 }
1157
1158 ASSERT_FALSE(receivedCallback);
1159}
1160
chaviwc1cf4022022-06-03 13:32:33 -05001161TEST_F(BLASTBufferQueueTest, SyncNextTransactionDropBuffer) {
1162 uint8_t r = 255;
1163 uint8_t g = 0;
1164 uint8_t b = 0;
1165
1166 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
1167
1168 sp<IGraphicBufferProducer> igbProducer;
1169 setUpProducer(adapter, igbProducer);
1170
1171 Transaction sync;
1172 adapter.setSyncTransaction(sync);
1173 queueBuffer(igbProducer, 0, 255, 0, 0);
1174
1175 // Merge a transaction that has a complete callback into the next frame so we can get notified
1176 // when to take a screenshot
1177 CallbackHelper transactionCallback;
1178 Transaction t;
1179 t.addTransactionCompletedCallback(transactionCallback.function,
1180 transactionCallback.getContext());
1181 adapter.mergeWithNextTransaction(&t, 2);
1182 queueBuffer(igbProducer, r, g, b, 0);
1183
1184 // Drop the buffer, but ensure the next one continues to get processed.
1185 sync.setBuffer(mSurfaceControl, nullptr);
1186
1187 CallbackData callbackData;
1188 transactionCallback.getCallbackData(&callbackData);
1189 ASSERT_EQ(NO_ERROR, captureDisplay(mCaptureArgs, mCaptureResults));
1190 ASSERT_NO_FATAL_FAILURE(
1191 checkScreenCapture(r, g, b, {0, 0, (int32_t)mDisplayWidth, (int32_t)mDisplayHeight}));
Rob Carr4f797ab2022-07-07 18:29:22 +00001192 sync.apply();
chaviwc1cf4022022-06-03 13:32:33 -05001193}
1194
Vishnu Nair1e8bf102021-12-28 14:36:59 -08001195// This test will currently fail because the old surfacecontrol will steal the last presented buffer
1196// until the old surface control is destroyed. This is not necessarily a bug but to document a
1197// limitation with the update API and to test any changes to make the api more robust. The current
1198// approach for the client is to recreate the blastbufferqueue when the surfacecontrol updates.
1199TEST_F(BLASTBufferQueueTest, DISABLED_DisconnectProducerTest) {
1200 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
1201 std::vector<sp<SurfaceControl>> surfaceControls;
1202 sp<IGraphicBufferProducer> igbProducer;
1203 for (int i = 0; i < 10; i++) {
1204 sp<SurfaceControl> sc =
1205 mClient->createSurface(String8("TestSurface"), mDisplayWidth, mDisplayHeight,
1206 PIXEL_FORMAT_RGBA_8888,
1207 ISurfaceComposerClient::eFXSurfaceBufferState,
1208 /*parent*/ nullptr);
1209 Transaction()
1210 .setLayerStack(mSurfaceControl, ui::DEFAULT_LAYER_STACK)
1211 .setLayer(mSurfaceControl, std::numeric_limits<int32_t>::max())
1212 .show(mSurfaceControl)
1213 .setDataspace(mSurfaceControl, ui::Dataspace::V0_SRGB)
1214 .apply(true);
1215 surfaceControls.push_back(sc);
1216 adapter.update(sc, mDisplayWidth, mDisplayHeight);
1217
1218 setUpProducer(adapter, igbProducer);
1219 Transaction next;
1220 queueBuffer(igbProducer, 0, 255, 0, 0);
1221 queueBuffer(igbProducer, 0, 0, 255, 0);
Tianhao Yao4861b102022-02-03 20:18:35 +00001222 adapter.setSyncTransaction(next, true);
Vishnu Nair1e8bf102021-12-28 14:36:59 -08001223 queueBuffer(igbProducer, 255, 0, 0, 0);
1224
1225 CallbackHelper transactionCallback;
1226 next.addTransactionCompletedCallback(transactionCallback.function,
1227 transactionCallback.getContext())
1228 .apply();
1229
1230 CallbackData callbackData;
1231 transactionCallback.getCallbackData(&callbackData);
1232 // capture screen and verify that it is red
1233 ASSERT_EQ(NO_ERROR, captureDisplay(mCaptureArgs, mCaptureResults));
1234 ASSERT_NO_FATAL_FAILURE(
1235 checkScreenCapture(255, 0, 0,
1236 {0, 0, (int32_t)mDisplayWidth, (int32_t)mDisplayHeight}));
1237 igbProducer->disconnect(NATIVE_WINDOW_API_CPU);
1238 }
1239}
1240
1241// See DISABLED_DisconnectProducerTest
1242TEST_F(BLASTBufferQueueTest, DISABLED_UpdateSurfaceControlTest) {
1243 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
1244 std::vector<sp<SurfaceControl>> surfaceControls;
1245 sp<IGraphicBufferProducer> igbProducer;
1246 for (int i = 0; i < 10; i++) {
1247 sp<SurfaceControl> sc =
1248 mClient->createSurface(String8("TestSurface"), mDisplayWidth, mDisplayHeight,
1249 PIXEL_FORMAT_RGBA_8888,
1250 ISurfaceComposerClient::eFXSurfaceBufferState,
1251 /*parent*/ nullptr);
1252 Transaction()
1253 .setLayerStack(mSurfaceControl, ui::DEFAULT_LAYER_STACK)
1254 .setLayer(mSurfaceControl, std::numeric_limits<int32_t>::max())
1255 .show(mSurfaceControl)
1256 .setDataspace(mSurfaceControl, ui::Dataspace::V0_SRGB)
1257 .apply(true);
1258 surfaceControls.push_back(sc);
1259 adapter.update(sc, mDisplayWidth, mDisplayHeight);
1260 setUpProducer(adapter, igbProducer);
1261
1262 Transaction next;
1263 queueBuffer(igbProducer, 0, 255, 0, 0);
1264 queueBuffer(igbProducer, 0, 0, 255, 0);
Tianhao Yao4861b102022-02-03 20:18:35 +00001265 adapter.setSyncTransaction(next, true);
Vishnu Nair1e8bf102021-12-28 14:36:59 -08001266 queueBuffer(igbProducer, 255, 0, 0, 0);
1267
1268 CallbackHelper transactionCallback;
1269 next.addTransactionCompletedCallback(transactionCallback.function,
1270 transactionCallback.getContext())
1271 .apply();
1272
1273 CallbackData callbackData;
1274 transactionCallback.getCallbackData(&callbackData);
1275 // capture screen and verify that it is red
1276 ASSERT_EQ(NO_ERROR, captureDisplay(mCaptureArgs, mCaptureResults));
1277 ASSERT_NO_FATAL_FAILURE(
1278 checkScreenCapture(255, 0, 0,
1279 {0, 0, (int32_t)mDisplayWidth, (int32_t)mDisplayHeight}));
1280 }
1281}
1282
Vishnu Nair89496122020-12-14 17:14:53 -08001283class TestProducerListener : public BnProducerListener {
1284public:
1285 sp<IGraphicBufferProducer> mIgbp;
1286 TestProducerListener(const sp<IGraphicBufferProducer>& igbp) : mIgbp(igbp) {}
1287 void onBufferReleased() override {
1288 sp<GraphicBuffer> buffer;
1289 sp<Fence> fence;
1290 mIgbp->detachNextBuffer(&buffer, &fence);
1291 }
1292};
1293
1294TEST_F(BLASTBufferQueueTest, CustomProducerListener) {
1295 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
1296 sp<IGraphicBufferProducer> igbProducer = adapter.getIGraphicBufferProducer();
1297 ASSERT_NE(nullptr, igbProducer.get());
1298 ASSERT_EQ(NO_ERROR, igbProducer->setMaxDequeuedBufferCount(2));
1299 IGraphicBufferProducer::QueueBufferOutput qbOutput;
1300 ASSERT_EQ(NO_ERROR,
1301 igbProducer->connect(new TestProducerListener(igbProducer), NATIVE_WINDOW_API_CPU,
1302 false, &qbOutput));
1303 ASSERT_NE(ui::Transform::ROT_INVALID, qbOutput.transformHint);
1304 for (int i = 0; i < 3; i++) {
1305 int slot;
1306 sp<Fence> fence;
1307 sp<GraphicBuffer> buf;
1308 auto ret = igbProducer->dequeueBuffer(&slot, &fence, mDisplayWidth, mDisplayHeight,
1309 PIXEL_FORMAT_RGBA_8888, GRALLOC_USAGE_SW_WRITE_OFTEN,
1310 nullptr, nullptr);
1311 ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, ret);
1312 ASSERT_EQ(OK, igbProducer->requestBuffer(slot, &buf));
1313 IGraphicBufferProducer::QueueBufferOutput qbOutput;
Vishnu Nair1506b182021-02-22 14:35:15 -08001314 IGraphicBufferProducer::QueueBufferInput input(systemTime(), true /* autotimestamp */,
1315 HAL_DATASPACE_UNKNOWN,
Vishnu Nair89496122020-12-14 17:14:53 -08001316 Rect(mDisplayWidth, mDisplayHeight),
1317 NATIVE_WINDOW_SCALING_MODE_FREEZE, 0,
1318 Fence::NO_FENCE);
1319 igbProducer->queueBuffer(slot, input, &qbOutput);
1320 }
1321 adapter.waitForCallbacks();
1322}
1323
Vishnu Nair17dde612020-12-28 11:39:59 -08001324TEST_F(BLASTBufferQueueTest, QueryNativeWindowQueuesToWindowComposer) {
1325 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
1326
1327 sp<android::Surface> surface = new Surface(adapter.getIGraphicBufferProducer());
1328 ANativeWindow* nativeWindow = (ANativeWindow*)(surface.get());
1329 int queuesToNativeWindow = 0;
1330 int err = nativeWindow->query(nativeWindow, NATIVE_WINDOW_QUEUES_TO_WINDOW_COMPOSER,
1331 &queuesToNativeWindow);
1332 ASSERT_EQ(NO_ERROR, err);
1333 ASSERT_EQ(queuesToNativeWindow, 1);
1334}
1335
Vishnu Naira4fbca52021-07-07 16:52:34 -07001336TEST_F(BLASTBufferQueueTest, TransformHint) {
1337 // Transform hint is provided to BBQ via the surface control passed by WM
1338 mSurfaceControl->setTransformHint(ui::Transform::ROT_90);
1339
1340 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
1341 sp<IGraphicBufferProducer> igbProducer = adapter.getIGraphicBufferProducer();
1342 ASSERT_NE(nullptr, igbProducer.get());
1343 ASSERT_EQ(NO_ERROR, igbProducer->setMaxDequeuedBufferCount(2));
1344 sp<Surface> surface = adapter.getSurface();
1345
1346 // Before connecting to the surface, we do not get a valid transform hint
1347 int transformHint;
1348 surface->query(NATIVE_WINDOW_TRANSFORM_HINT, &transformHint);
1349 ASSERT_EQ(ui::Transform::ROT_0, transformHint);
1350
1351 ASSERT_EQ(NO_ERROR,
1352 surface->connect(NATIVE_WINDOW_API_CPU, new TestProducerListener(igbProducer)));
1353
1354 // After connecting to the surface, we should get the correct hint.
1355 surface->query(NATIVE_WINDOW_TRANSFORM_HINT, &transformHint);
1356 ASSERT_EQ(ui::Transform::ROT_90, transformHint);
1357
1358 ANativeWindow_Buffer buffer;
1359 surface->lock(&buffer, nullptr /* inOutDirtyBounds */);
1360
1361 // Transform hint is updated via callbacks or surface control updates
1362 mSurfaceControl->setTransformHint(ui::Transform::ROT_0);
1363 adapter.update(mSurfaceControl, mDisplayWidth, mDisplayHeight);
1364
1365 // The hint does not change and matches the value used when dequeueing the buffer.
1366 surface->query(NATIVE_WINDOW_TRANSFORM_HINT, &transformHint);
1367 ASSERT_EQ(ui::Transform::ROT_90, transformHint);
1368
1369 surface->unlockAndPost();
1370
1371 // After queuing the buffer, we get the updated transform hint
1372 surface->query(NATIVE_WINDOW_TRANSFORM_HINT, &transformHint);
1373 ASSERT_EQ(ui::Transform::ROT_0, transformHint);
1374
1375 adapter.waitForCallbacks();
1376}
1377
Valerie Hau5977fc82019-12-05 15:56:39 -08001378class BLASTBufferQueueTransformTest : public BLASTBufferQueueTest {
1379public:
1380 void test(uint32_t tr) {
1381 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
1382 sp<IGraphicBufferProducer> igbProducer;
1383 setUpProducer(adapter, igbProducer);
1384
1385 auto bufWidth = mDisplayWidth;
1386 auto bufHeight = mDisplayHeight;
1387 int slot;
1388 sp<Fence> fence;
1389 sp<GraphicBuffer> buf;
1390
1391 auto ret = igbProducer->dequeueBuffer(&slot, &fence, bufWidth, bufHeight,
1392 PIXEL_FORMAT_RGBA_8888, GRALLOC_USAGE_SW_WRITE_OFTEN,
1393 nullptr, nullptr);
1394 ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, ret);
1395 ASSERT_EQ(OK, igbProducer->requestBuffer(slot, &buf));
1396
1397 fillQuadrants(buf);
1398
1399 IGraphicBufferProducer::QueueBufferOutput qbOutput;
Vishnu Nair1506b182021-02-22 14:35:15 -08001400 IGraphicBufferProducer::QueueBufferInput input(systemTime(), true /* autotimestamp */,
1401 HAL_DATASPACE_UNKNOWN,
Valerie Hau5977fc82019-12-05 15:56:39 -08001402 Rect(bufWidth, bufHeight),
Vishnu Naire1a42322020-10-02 17:42:04 -07001403 NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW,
1404 tr, Fence::NO_FENCE);
Valerie Hau5977fc82019-12-05 15:56:39 -08001405 igbProducer->queueBuffer(slot, input, &qbOutput);
Dominik Laskowski718f9602019-11-09 20:01:35 -08001406 ASSERT_NE(ui::Transform::ROT_INVALID, qbOutput.transformHint);
Valerie Hau5977fc82019-12-05 15:56:39 -08001407
Patrick Williams0eb09c62023-07-28 11:24:35 -05001408 Transaction().apply(true /* synchronous */);
chaviw8ffc7b82020-08-18 11:25:37 -07001409 ASSERT_EQ(NO_ERROR, captureDisplay(mCaptureArgs, mCaptureResults));
chaviwd2432892020-07-24 17:42:39 -07001410
Valerie Hau5977fc82019-12-05 15:56:39 -08001411 switch (tr) {
1412 case ui::Transform::ROT_0:
1413 ASSERT_NO_FATAL_FAILURE(checkScreenCapture(0, 0, 0,
1414 {0, 0, (int32_t)mDisplayWidth / 2,
1415 (int32_t)mDisplayHeight / 2},
1416 1));
1417 ASSERT_NO_FATAL_FAILURE(
1418 checkScreenCapture(255, 0, 0,
1419 {(int32_t)mDisplayWidth / 2, 0, (int32_t)mDisplayWidth,
1420 (int32_t)mDisplayHeight / 2},
1421 1));
1422 ASSERT_NO_FATAL_FAILURE(
1423 checkScreenCapture(0, 255, 0,
1424 {(int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight / 2,
1425 (int32_t)mDisplayWidth, (int32_t)mDisplayHeight},
1426 1));
1427 ASSERT_NO_FATAL_FAILURE(
1428 checkScreenCapture(0, 0, 255,
1429 {0, (int32_t)mDisplayHeight / 2,
1430 (int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight},
1431 1));
1432 break;
1433 case ui::Transform::FLIP_H:
1434 ASSERT_NO_FATAL_FAILURE(checkScreenCapture(255, 0, 0,
1435 {0, 0, (int32_t)mDisplayWidth / 2,
1436 (int32_t)mDisplayHeight / 2},
1437 1));
1438 ASSERT_NO_FATAL_FAILURE(
1439 checkScreenCapture(0, 0, 0,
1440 {(int32_t)mDisplayWidth / 2, 0, (int32_t)mDisplayWidth,
1441 (int32_t)mDisplayHeight / 2},
1442 1));
1443 ASSERT_NO_FATAL_FAILURE(
1444 checkScreenCapture(0, 0, 255,
1445 {(int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight / 2,
1446 (int32_t)mDisplayWidth, (int32_t)mDisplayHeight},
1447 1));
1448 ASSERT_NO_FATAL_FAILURE(
1449 checkScreenCapture(0, 255, 0,
1450 {0, (int32_t)mDisplayHeight / 2,
1451 (int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight},
1452 1));
1453 break;
1454 case ui::Transform::FLIP_V:
1455 ASSERT_NO_FATAL_FAILURE(checkScreenCapture(0, 0, 255,
1456 {0, 0, (int32_t)mDisplayWidth / 2,
1457 (int32_t)mDisplayHeight / 2},
1458 1));
1459 ASSERT_NO_FATAL_FAILURE(
1460 checkScreenCapture(0, 255, 0,
1461 {(int32_t)mDisplayWidth / 2, 0, (int32_t)mDisplayWidth,
1462 (int32_t)mDisplayHeight / 2},
1463 1));
1464 ASSERT_NO_FATAL_FAILURE(
1465 checkScreenCapture(255, 0, 0,
1466 {(int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight / 2,
1467 (int32_t)mDisplayWidth, (int32_t)mDisplayHeight},
1468 1));
1469 ASSERT_NO_FATAL_FAILURE(
1470 checkScreenCapture(0, 0, 0,
1471 {0, (int32_t)mDisplayHeight / 2,
1472 (int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight},
1473 1));
1474 break;
1475 case ui::Transform::ROT_90:
1476 ASSERT_NO_FATAL_FAILURE(checkScreenCapture(0, 0, 255,
1477 {0, 0, (int32_t)mDisplayWidth / 2,
1478 (int32_t)mDisplayHeight / 2},
1479 1));
1480 ASSERT_NO_FATAL_FAILURE(
1481 checkScreenCapture(0, 0, 0,
1482 {(int32_t)mDisplayWidth / 2, 0, (int32_t)mDisplayWidth,
1483 (int32_t)mDisplayHeight / 2},
1484 1));
1485 ASSERT_NO_FATAL_FAILURE(
1486 checkScreenCapture(255, 0, 0,
1487 {(int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight / 2,
1488 (int32_t)mDisplayWidth, (int32_t)mDisplayHeight},
1489 1));
1490 ASSERT_NO_FATAL_FAILURE(
1491 checkScreenCapture(0, 255, 0,
1492 {0, (int32_t)mDisplayHeight / 2,
1493 (int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight},
1494 1));
1495 break;
1496 case ui::Transform::ROT_180:
1497 ASSERT_NO_FATAL_FAILURE(checkScreenCapture(0, 255, 0,
1498 {0, 0, (int32_t)mDisplayWidth / 2,
1499 (int32_t)mDisplayHeight / 2},
1500 1));
1501 ASSERT_NO_FATAL_FAILURE(
1502 checkScreenCapture(0, 0, 255,
1503 {(int32_t)mDisplayWidth / 2, 0, (int32_t)mDisplayWidth,
1504 (int32_t)mDisplayHeight / 2},
1505 1));
1506 ASSERT_NO_FATAL_FAILURE(
1507 checkScreenCapture(0, 0, 0,
1508 {(int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight / 2,
1509 (int32_t)mDisplayWidth, (int32_t)mDisplayHeight},
1510 1));
1511 ASSERT_NO_FATAL_FAILURE(
1512 checkScreenCapture(255, 0, 0,
1513 {0, (int32_t)mDisplayHeight / 2,
1514 (int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight},
1515 1));
1516 break;
1517 case ui::Transform::ROT_270:
1518 ASSERT_NO_FATAL_FAILURE(checkScreenCapture(255, 0, 0,
1519 {0, 0, (int32_t)mDisplayWidth / 2,
1520 (int32_t)mDisplayHeight / 2},
1521 1));
1522 ASSERT_NO_FATAL_FAILURE(
1523 checkScreenCapture(0, 255, 0,
1524 {(int32_t)mDisplayWidth / 2, 0, (int32_t)mDisplayWidth,
1525 (int32_t)mDisplayHeight / 2},
1526 1));
1527 ASSERT_NO_FATAL_FAILURE(
1528 checkScreenCapture(0, 0, 255,
1529 {(int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight / 2,
1530 (int32_t)mDisplayWidth, (int32_t)mDisplayHeight},
1531 1));
1532 ASSERT_NO_FATAL_FAILURE(
1533 checkScreenCapture(0, 0, 0,
1534 {0, (int32_t)mDisplayHeight / 2,
1535 (int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight},
1536 1));
1537 }
1538 }
1539};
1540
1541TEST_F(BLASTBufferQueueTransformTest, setTransform_ROT_0) {
1542 test(ui::Transform::ROT_0);
1543}
1544
1545TEST_F(BLASTBufferQueueTransformTest, setTransform_FLIP_H) {
1546 test(ui::Transform::FLIP_H);
1547}
1548
1549TEST_F(BLASTBufferQueueTransformTest, setTransform_FLIP_V) {
1550 test(ui::Transform::FLIP_V);
1551}
1552
1553TEST_F(BLASTBufferQueueTransformTest, setTransform_ROT_90) {
1554 test(ui::Transform::ROT_90);
1555}
1556
1557TEST_F(BLASTBufferQueueTransformTest, setTransform_ROT_180) {
1558 test(ui::Transform::ROT_180);
1559}
1560
1561TEST_F(BLASTBufferQueueTransformTest, setTransform_ROT_270) {
1562 test(ui::Transform::ROT_270);
1563}
Valerie Hau871d6352020-01-29 08:44:02 -08001564
1565class BLASTFrameEventHistoryTest : public BLASTBufferQueueTest {
1566public:
1567 void setUpAndQueueBuffer(const sp<IGraphicBufferProducer>& igbProducer,
Vishnu Nairde66dc72021-06-17 17:54:41 -07001568 nsecs_t* outRequestedPresentTime, nsecs_t* postedTime,
Valerie Hau871d6352020-01-29 08:44:02 -08001569 IGraphicBufferProducer::QueueBufferOutput* qbOutput,
Vishnu Nairde66dc72021-06-17 17:54:41 -07001570 bool getFrameTimestamps, nsecs_t requestedPresentTime = systemTime()) {
Valerie Hau871d6352020-01-29 08:44:02 -08001571 int slot;
1572 sp<Fence> fence;
1573 sp<GraphicBuffer> buf;
1574 auto ret = igbProducer->dequeueBuffer(&slot, &fence, mDisplayWidth, mDisplayHeight,
1575 PIXEL_FORMAT_RGBA_8888, GRALLOC_USAGE_SW_WRITE_OFTEN,
1576 nullptr, nullptr);
Vishnu Nairde66dc72021-06-17 17:54:41 -07001577 if (IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION == ret) {
1578 ASSERT_EQ(OK, igbProducer->requestBuffer(slot, &buf));
1579 }
Valerie Hau871d6352020-01-29 08:44:02 -08001580
Vishnu Nairde66dc72021-06-17 17:54:41 -07001581 *outRequestedPresentTime = requestedPresentTime;
1582 IGraphicBufferProducer::QueueBufferInput input(requestedPresentTime, false,
1583 HAL_DATASPACE_UNKNOWN,
Valerie Hau871d6352020-01-29 08:44:02 -08001584 Rect(mDisplayWidth, mDisplayHeight),
1585 NATIVE_WINDOW_SCALING_MODE_FREEZE, 0,
1586 Fence::NO_FENCE, /*sticky*/ 0,
1587 getFrameTimestamps);
1588 if (postedTime) *postedTime = systemTime();
1589 igbProducer->queueBuffer(slot, input, qbOutput);
1590 }
Vishnu Nair083efd32021-02-12 09:32:30 -08001591 sp<SurfaceControl> mBufferQueueSurfaceControl;
Valerie Hau871d6352020-01-29 08:44:02 -08001592};
1593
1594TEST_F(BLASTFrameEventHistoryTest, FrameEventHistory_Basic) {
1595 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
1596 sp<IGraphicBufferProducer> igbProducer;
1597 ProducerFrameEventHistory history;
1598 setUpProducer(adapter, igbProducer);
1599
1600 IGraphicBufferProducer::QueueBufferOutput qbOutput;
1601 nsecs_t requestedPresentTimeA = 0;
1602 nsecs_t postedTimeA = 0;
1603 setUpAndQueueBuffer(igbProducer, &requestedPresentTimeA, &postedTimeA, &qbOutput, true);
1604 history.applyDelta(qbOutput.frameTimestamps);
1605
1606 FrameEvents* events = nullptr;
1607 events = history.getFrame(1);
1608 ASSERT_NE(nullptr, events);
1609 ASSERT_EQ(1, events->frameNumber);
1610 ASSERT_EQ(requestedPresentTimeA, events->requestedPresentTime);
1611 ASSERT_GE(events->postedTime, postedTimeA);
1612
Vishnu Nair1506b182021-02-22 14:35:15 -08001613 adapter.waitForCallback(1);
Valerie Hau871d6352020-01-29 08:44:02 -08001614
1615 // queue another buffer so we query for frame event deltas
1616 nsecs_t requestedPresentTimeB = 0;
1617 nsecs_t postedTimeB = 0;
1618 setUpAndQueueBuffer(igbProducer, &requestedPresentTimeB, &postedTimeB, &qbOutput, true);
1619 history.applyDelta(qbOutput.frameTimestamps);
1620 events = history.getFrame(1);
1621 ASSERT_NE(nullptr, events);
1622
1623 // frame number, requestedPresentTime, and postTime should not have changed
1624 ASSERT_EQ(1, events->frameNumber);
1625 ASSERT_EQ(requestedPresentTimeA, events->requestedPresentTime);
1626 ASSERT_GE(events->postedTime, postedTimeA);
1627
1628 ASSERT_GE(events->latchTime, postedTimeA);
1629 ASSERT_GE(events->dequeueReadyTime, events->latchTime);
1630 ASSERT_NE(nullptr, events->gpuCompositionDoneFence);
1631 ASSERT_NE(nullptr, events->displayPresentFence);
1632 ASSERT_NE(nullptr, events->releaseFence);
1633
1634 // we should also have gotten the initial values for the next frame
1635 events = history.getFrame(2);
1636 ASSERT_NE(nullptr, events);
1637 ASSERT_EQ(2, events->frameNumber);
1638 ASSERT_EQ(requestedPresentTimeB, events->requestedPresentTime);
1639 ASSERT_GE(events->postedTime, postedTimeB);
Valerie Hau78491e92020-04-15 13:10:56 -07001640
1641 // wait for any callbacks that have not been received
1642 adapter.waitForCallbacks();
Valerie Hau871d6352020-01-29 08:44:02 -08001643}
Vishnu Nair083efd32021-02-12 09:32:30 -08001644
Vishnu Nair083efd32021-02-12 09:32:30 -08001645TEST_F(BLASTFrameEventHistoryTest, FrameEventHistory_DroppedFrame) {
1646 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
1647 sp<IGraphicBufferProducer> igbProducer;
1648 setUpProducer(adapter, igbProducer);
1649
1650 ProducerFrameEventHistory history;
1651 IGraphicBufferProducer::QueueBufferOutput qbOutput;
1652 nsecs_t requestedPresentTimeA = 0;
1653 nsecs_t postedTimeA = 0;
Vishnu Nairde66dc72021-06-17 17:54:41 -07001654 // Present the frame sometime in the future so we can add two frames to the queue so the older
1655 // one will be dropped.
1656 nsecs_t presentTime = systemTime() + std::chrono::nanoseconds(500ms).count();
Vishnu Nair083efd32021-02-12 09:32:30 -08001657 setUpAndQueueBuffer(igbProducer, &requestedPresentTimeA, &postedTimeA, &qbOutput, true,
Vishnu Nairde66dc72021-06-17 17:54:41 -07001658 presentTime);
Vishnu Nair083efd32021-02-12 09:32:30 -08001659 history.applyDelta(qbOutput.frameTimestamps);
1660
1661 FrameEvents* events = nullptr;
1662 events = history.getFrame(1);
1663 ASSERT_NE(nullptr, events);
1664 ASSERT_EQ(1, events->frameNumber);
1665 ASSERT_EQ(requestedPresentTimeA, events->requestedPresentTime);
1666 ASSERT_GE(events->postedTime, postedTimeA);
1667
1668 // queue another buffer so the first can be dropped
1669 nsecs_t requestedPresentTimeB = 0;
1670 nsecs_t postedTimeB = 0;
Vishnu Nairde66dc72021-06-17 17:54:41 -07001671 presentTime = systemTime() + std::chrono::nanoseconds(1ms).count();
1672 setUpAndQueueBuffer(igbProducer, &requestedPresentTimeB, &postedTimeB, &qbOutput, true,
1673 presentTime);
Vishnu Nair083efd32021-02-12 09:32:30 -08001674 history.applyDelta(qbOutput.frameTimestamps);
1675 events = history.getFrame(1);
1676 ASSERT_NE(nullptr, events);
1677
1678 // frame number, requestedPresentTime, and postTime should not have changed
1679 ASSERT_EQ(1, events->frameNumber);
1680 ASSERT_EQ(requestedPresentTimeA, events->requestedPresentTime);
1681 ASSERT_GE(events->postedTime, postedTimeA);
1682
Vishnu Nairde66dc72021-06-17 17:54:41 -07001683 // a valid latchtime and pre and post composition info should not be set for the dropped frame
Vishnu Nair083efd32021-02-12 09:32:30 -08001684 ASSERT_FALSE(events->hasLatchInfo());
1685 ASSERT_FALSE(events->hasDequeueReadyInfo());
Vishnu Nairde66dc72021-06-17 17:54:41 -07001686 ASSERT_FALSE(events->hasGpuCompositionDoneInfo());
1687 ASSERT_FALSE(events->hasDisplayPresentInfo());
1688 ASSERT_FALSE(events->hasReleaseInfo());
Vishnu Nair083efd32021-02-12 09:32:30 -08001689
Vishnu Nairde66dc72021-06-17 17:54:41 -07001690 // wait for the last transaction to be completed.
1691 adapter.waitForCallback(2);
Vishnu Nair083efd32021-02-12 09:32:30 -08001692
Vishnu Nairde66dc72021-06-17 17:54:41 -07001693 // queue another buffer so we query for frame event deltas
1694 nsecs_t requestedPresentTimeC = 0;
1695 nsecs_t postedTimeC = 0;
1696 setUpAndQueueBuffer(igbProducer, &requestedPresentTimeC, &postedTimeC, &qbOutput, true);
1697 history.applyDelta(qbOutput.frameTimestamps);
1698
1699 // frame number, requestedPresentTime, and postTime should not have changed
1700 ASSERT_EQ(1, events->frameNumber);
1701 ASSERT_EQ(requestedPresentTimeA, events->requestedPresentTime);
1702 ASSERT_GE(events->postedTime, postedTimeA);
1703
1704 // a valid latchtime and pre and post composition info should not be set for the dropped frame
1705 ASSERT_FALSE(events->hasLatchInfo());
1706 ASSERT_FALSE(events->hasDequeueReadyInfo());
1707 ASSERT_FALSE(events->hasGpuCompositionDoneInfo());
1708 ASSERT_FALSE(events->hasDisplayPresentInfo());
1709 ASSERT_FALSE(events->hasReleaseInfo());
1710
1711 // we should also have gotten values for the presented frame
Vishnu Nair083efd32021-02-12 09:32:30 -08001712 events = history.getFrame(2);
1713 ASSERT_NE(nullptr, events);
1714 ASSERT_EQ(2, events->frameNumber);
1715 ASSERT_EQ(requestedPresentTimeB, events->requestedPresentTime);
1716 ASSERT_GE(events->postedTime, postedTimeB);
Vishnu Nairde66dc72021-06-17 17:54:41 -07001717 ASSERT_GE(events->latchTime, postedTimeB);
1718 ASSERT_GE(events->dequeueReadyTime, events->latchTime);
1719 ASSERT_NE(nullptr, events->gpuCompositionDoneFence);
1720 ASSERT_NE(nullptr, events->displayPresentFence);
1721 ASSERT_NE(nullptr, events->releaseFence);
1722
1723 // wait for any callbacks that have not been received
1724 adapter.waitForCallbacks();
Vishnu Nair083efd32021-02-12 09:32:30 -08001725}
1726
Vishnu Nair9a69a042021-06-18 13:19:49 -07001727TEST_F(BLASTFrameEventHistoryTest, FrameEventHistory_CompositorTimings) {
1728 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
1729 sp<IGraphicBufferProducer> igbProducer;
1730 ProducerFrameEventHistory history;
1731 setUpProducer(adapter, igbProducer);
1732
1733 IGraphicBufferProducer::QueueBufferOutput qbOutput;
1734 nsecs_t requestedPresentTimeA = 0;
1735 nsecs_t postedTimeA = 0;
Vishnu Nair9a69a042021-06-18 13:19:49 -07001736 setUpAndQueueBuffer(igbProducer, &requestedPresentTimeA, &postedTimeA, &qbOutput, true);
1737 history.applyDelta(qbOutput.frameTimestamps);
1738 adapter.waitForCallback(1);
1739
1740 // queue another buffer so we query for frame event deltas
1741 nsecs_t requestedPresentTimeB = 0;
1742 nsecs_t postedTimeB = 0;
1743 setUpAndQueueBuffer(igbProducer, &requestedPresentTimeB, &postedTimeB, &qbOutput, true);
1744 history.applyDelta(qbOutput.frameTimestamps);
1745
1746 // check for a valid compositor deadline
1747 ASSERT_NE(0, history.getReportedCompositeDeadline());
1748
1749 // wait for any callbacks that have not been received
1750 adapter.waitForCallbacks();
1751}
1752
Valerie Hauc5011f92019-10-11 09:52:07 -07001753} // namespace android