blob: cd90168784bf3a09da6d0013f7232cb1cd83976a [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
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800450 adapter.waitForCallbacks();
Valerie Hauda3446e2019-10-14 15:49:22 -0700451
452 // capture screen and verify that it is red
chaviw8ffc7b82020-08-18 11:25:37 -0700453 ASSERT_EQ(NO_ERROR, captureDisplay(mCaptureArgs, mCaptureResults));
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800454 ASSERT_NO_FATAL_FAILURE(
455 checkScreenCapture(r, g, b, {0, 0, (int32_t)mDisplayWidth, (int32_t)mDisplayHeight}));
Valerie Hauda3446e2019-10-14 15:49:22 -0700456}
Valerie Haud3b90d22019-11-06 09:37:31 -0800457
458TEST_F(BLASTBufferQueueTest, TripleBuffering) {
459 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
460 sp<IGraphicBufferProducer> igbProducer;
461 setUpProducer(adapter, igbProducer);
462
463 std::vector<std::pair<int, sp<Fence>>> allocated;
Ady Abraham0bde6b52021-05-18 13:57:02 -0700464 int minUndequeuedBuffers = 0;
465 ASSERT_EQ(OK, igbProducer->query(NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS, &minUndequeuedBuffers));
466 const auto bufferCount = minUndequeuedBuffers + 2;
467
468 for (int i = 0; i < bufferCount; i++) {
Valerie Haud3b90d22019-11-06 09:37:31 -0800469 int slot;
470 sp<Fence> fence;
471 sp<GraphicBuffer> buf;
472 auto ret = igbProducer->dequeueBuffer(&slot, &fence, mDisplayWidth, mDisplayHeight,
473 PIXEL_FORMAT_RGBA_8888, GRALLOC_USAGE_SW_WRITE_OFTEN,
474 nullptr, nullptr);
475 ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, ret);
476 ASSERT_EQ(OK, igbProducer->requestBuffer(slot, &buf));
477 allocated.push_back({slot, fence});
478 }
479 for (int i = 0; i < allocated.size(); i++) {
480 igbProducer->cancelBuffer(allocated[i].first, allocated[i].second);
481 }
482
Valerie Haua32c5522019-12-09 10:11:08 -0800483 for (int i = 0; i < 100; i++) {
Valerie Haud3b90d22019-11-06 09:37:31 -0800484 int slot;
485 sp<Fence> fence;
486 sp<GraphicBuffer> buf;
487 auto ret = igbProducer->dequeueBuffer(&slot, &fence, mDisplayWidth, mDisplayHeight,
488 PIXEL_FORMAT_RGBA_8888, GRALLOC_USAGE_SW_WRITE_OFTEN,
489 nullptr, nullptr);
490 ASSERT_EQ(NO_ERROR, ret);
491 IGraphicBufferProducer::QueueBufferOutput qbOutput;
Vishnu Nair1506b182021-02-22 14:35:15 -0800492 IGraphicBufferProducer::QueueBufferInput input(systemTime(), true /* autotimestamp */,
493 HAL_DATASPACE_UNKNOWN,
Valerie Haud3b90d22019-11-06 09:37:31 -0800494 Rect(mDisplayWidth, mDisplayHeight),
495 NATIVE_WINDOW_SCALING_MODE_FREEZE, 0,
496 Fence::NO_FENCE);
497 igbProducer->queueBuffer(slot, input, &qbOutput);
498 }
499 adapter.waitForCallbacks();
500}
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800501
502TEST_F(BLASTBufferQueueTest, SetCrop_Item) {
503 uint8_t r = 255;
504 uint8_t g = 0;
505 uint8_t b = 0;
506
507 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
508 sp<IGraphicBufferProducer> igbProducer;
509 setUpProducer(adapter, igbProducer);
510 int slot;
511 sp<Fence> fence;
512 sp<GraphicBuffer> buf;
513 auto ret = igbProducer->dequeueBuffer(&slot, &fence, mDisplayWidth, mDisplayHeight,
514 PIXEL_FORMAT_RGBA_8888, GRALLOC_USAGE_SW_WRITE_OFTEN,
515 nullptr, nullptr);
516 ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, ret);
517 ASSERT_EQ(OK, igbProducer->requestBuffer(slot, &buf));
518
519 uint32_t* bufData;
520 buf->lock(static_cast<uint32_t>(GraphicBuffer::USAGE_SW_WRITE_OFTEN),
521 reinterpret_cast<void**>(&bufData));
522 fillBuffer(bufData, Rect(buf->getWidth(), buf->getHeight() / 2), buf->getStride(), r, g, b);
523 buf->unlock();
524
525 IGraphicBufferProducer::QueueBufferOutput qbOutput;
Vishnu Nair1506b182021-02-22 14:35:15 -0800526 IGraphicBufferProducer::QueueBufferInput input(systemTime(), true /* autotimestamp */,
527 HAL_DATASPACE_UNKNOWN,
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800528 Rect(mDisplayWidth, mDisplayHeight / 2),
529 NATIVE_WINDOW_SCALING_MODE_FREEZE, 0,
530 Fence::NO_FENCE);
531 igbProducer->queueBuffer(slot, input, &qbOutput);
Dominik Laskowski718f9602019-11-09 20:01:35 -0800532 ASSERT_NE(ui::Transform::ROT_INVALID, qbOutput.transformHint);
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800533
534 adapter.waitForCallbacks();
535 // capture screen and verify that it is red
chaviw8ffc7b82020-08-18 11:25:37 -0700536 ASSERT_EQ(NO_ERROR, captureDisplay(mCaptureArgs, mCaptureResults));
chaviwd2432892020-07-24 17:42:39 -0700537
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800538 ASSERT_NO_FATAL_FAILURE(
Chavi Weingartena5aedbd2021-04-09 13:37:33 +0000539 checkScreenCapture(r, g, b,
540 {0, 0, (int32_t)mDisplayWidth, (int32_t)mDisplayHeight / 2}));
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800541}
542
543TEST_F(BLASTBufferQueueTest, SetCrop_ScalingModeScaleCrop) {
544 uint8_t r = 255;
545 uint8_t g = 0;
546 uint8_t b = 0;
547
548 int32_t bufferSideLength =
549 (mDisplayWidth < mDisplayHeight) ? mDisplayWidth / 2 : mDisplayHeight / 2;
550 int32_t finalCropSideLength = bufferSideLength / 2;
551
552 auto bg = mClient->createSurface(String8("BGTest"), 0, 0, PIXEL_FORMAT_RGBA_8888,
Vishnu Nairfa247b12020-02-11 08:58:26 -0800553 ISurfaceComposerClient::eFXSurfaceEffect);
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800554 ASSERT_NE(nullptr, bg.get());
555 Transaction t;
Dominik Laskowski29fa1462021-04-27 15:51:50 -0700556 t.setLayerStack(bg, ui::DEFAULT_LAYER_STACK)
chaviw25714502021-02-11 10:01:08 -0800557 .setCrop(bg, Rect(0, 0, mDisplayWidth, mDisplayHeight))
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800558 .setColor(bg, half3{0, 0, 0})
559 .setLayer(bg, 0)
560 .apply();
561
562 BLASTBufferQueueHelper adapter(mSurfaceControl, bufferSideLength, bufferSideLength);
563 sp<IGraphicBufferProducer> igbProducer;
564 setUpProducer(adapter, igbProducer);
565 int slot;
566 sp<Fence> fence;
567 sp<GraphicBuffer> buf;
568 auto ret = igbProducer->dequeueBuffer(&slot, &fence, bufferSideLength, bufferSideLength,
569 PIXEL_FORMAT_RGBA_8888, GRALLOC_USAGE_SW_WRITE_OFTEN,
570 nullptr, nullptr);
571 ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, ret);
572 ASSERT_EQ(OK, igbProducer->requestBuffer(slot, &buf));
573
574 uint32_t* bufData;
575 buf->lock(static_cast<uint32_t>(GraphicBuffer::USAGE_SW_WRITE_OFTEN),
576 reinterpret_cast<void**>(&bufData));
577 fillBuffer(bufData, Rect(buf->getWidth(), buf->getHeight()), buf->getStride(), 0, 0, 0);
578 fillBuffer(bufData,
579 Rect(finalCropSideLength / 2, 0, buf->getWidth() - finalCropSideLength / 2,
580 buf->getHeight()),
581 buf->getStride(), r, g, b);
582 buf->unlock();
583
584 IGraphicBufferProducer::QueueBufferOutput qbOutput;
Vishnu Nair1506b182021-02-22 14:35:15 -0800585 IGraphicBufferProducer::QueueBufferInput input(systemTime(), true /* autotimestamp */,
586 HAL_DATASPACE_UNKNOWN,
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800587 Rect(bufferSideLength, finalCropSideLength),
588 NATIVE_WINDOW_SCALING_MODE_SCALE_CROP, 0,
589 Fence::NO_FENCE);
590 igbProducer->queueBuffer(slot, input, &qbOutput);
Dominik Laskowski718f9602019-11-09 20:01:35 -0800591 ASSERT_NE(ui::Transform::ROT_INVALID, qbOutput.transformHint);
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800592
593 adapter.waitForCallbacks();
594 // capture screen and verify that it is red
chaviw8ffc7b82020-08-18 11:25:37 -0700595 ASSERT_EQ(NO_ERROR, captureDisplay(mCaptureArgs, mCaptureResults));
Vishnu Nair5cc9ac02021-04-19 13:23:38 -0700596 ASSERT_NO_FATAL_FAILURE(checkScreenCapture(r, g, b,
597 {10, 10, (int32_t)bufferSideLength - 10,
598 (int32_t)bufferSideLength - 10}));
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800599 ASSERT_NO_FATAL_FAILURE(
Vishnu Nair5cc9ac02021-04-19 13:23:38 -0700600 checkScreenCapture(0, 0, 0,
601 {0, 0, (int32_t)bufferSideLength, (int32_t)bufferSideLength},
602 /*border*/ 0, /*outsideRegion*/ true));
603}
604
605TEST_F(BLASTBufferQueueTest, ScaleCroppedBufferToBufferSize) {
606 // add black background
607 auto bg = mClient->createSurface(String8("BGTest"), 0, 0, PIXEL_FORMAT_RGBA_8888,
608 ISurfaceComposerClient::eFXSurfaceEffect);
609 ASSERT_NE(nullptr, bg.get());
610 Transaction t;
Dominik Laskowski29fa1462021-04-27 15:51:50 -0700611 t.setLayerStack(bg, ui::DEFAULT_LAYER_STACK)
Vishnu Nair5cc9ac02021-04-19 13:23:38 -0700612 .setCrop(bg, Rect(0, 0, mDisplayWidth, mDisplayHeight))
613 .setColor(bg, half3{0, 0, 0})
614 .setLayer(bg, 0)
615 .apply();
616
617 Rect windowSize(1000, 1000);
618 Rect bufferSize(windowSize);
619 Rect bufferCrop(200, 200, 700, 700);
620
621 BLASTBufferQueueHelper adapter(mSurfaceControl, windowSize.getWidth(), windowSize.getHeight());
622 sp<IGraphicBufferProducer> igbProducer;
623 setUpProducer(adapter, igbProducer);
624 int slot;
625 sp<Fence> fence;
626 sp<GraphicBuffer> buf;
627 auto ret = igbProducer->dequeueBuffer(&slot, &fence, bufferSize.getWidth(),
628 bufferSize.getHeight(), PIXEL_FORMAT_RGBA_8888,
629 GRALLOC_USAGE_SW_WRITE_OFTEN, nullptr, nullptr);
630 ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, ret);
631 ASSERT_EQ(OK, igbProducer->requestBuffer(slot, &buf));
632
633 uint32_t* bufData;
634 buf->lock(static_cast<uint32_t>(GraphicBuffer::USAGE_SW_WRITE_OFTEN),
635 reinterpret_cast<void**>(&bufData));
636 // fill buffer with grey
637 fillBuffer(bufData, bufferSize, buf->getStride(), 127, 127, 127);
638
639 // fill crop area with different colors so we can verify the cropped region has been scaled
640 // correctly.
641 fillBuffer(bufData, Rect(200, 200, 450, 450), buf->getStride(), /* rgb */ 255, 0, 0);
642 fillBuffer(bufData, Rect(200, 451, 450, 700), buf->getStride(), /* rgb */ 0, 255, 0);
643 fillBuffer(bufData, Rect(451, 200, 700, 450), buf->getStride(), /* rgb */ 0, 0, 255);
644 fillBuffer(bufData, Rect(451, 451, 700, 700), buf->getStride(), /* rgb */ 255, 0, 0);
645 buf->unlock();
646
647 IGraphicBufferProducer::QueueBufferOutput qbOutput;
648 IGraphicBufferProducer::QueueBufferInput input(systemTime(), true /* autotimestamp */,
649 HAL_DATASPACE_UNKNOWN,
650 bufferCrop /* Rect::INVALID_RECT */,
651 NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW, 0,
652 Fence::NO_FENCE);
653 igbProducer->queueBuffer(slot, input, &qbOutput);
654 ASSERT_NE(ui::Transform::ROT_INVALID, qbOutput.transformHint);
655
656 adapter.waitForCallbacks();
657
658 ASSERT_EQ(NO_ERROR, captureDisplay(mCaptureArgs, mCaptureResults));
659
660 // Verify cropped region is scaled correctly.
661 ASSERT_NO_FATAL_FAILURE(checkScreenCapture(255, 0, 0, {10, 10, 490, 490}));
662 ASSERT_NO_FATAL_FAILURE(checkScreenCapture(0, 255, 0, {10, 510, 490, 990}));
663 ASSERT_NO_FATAL_FAILURE(checkScreenCapture(0, 0, 255, {510, 10, 990, 490}));
664 ASSERT_NO_FATAL_FAILURE(checkScreenCapture(255, 0, 0, {510, 510, 990, 990}));
665 // Verify outside region is black.
666 ASSERT_NO_FATAL_FAILURE(checkScreenCapture(0, 0, 0,
667 {0, 0, (int32_t)windowSize.getWidth(),
668 (int32_t)windowSize.getHeight()},
669 /*border*/ 0, /*outsideRegion*/ true));
670}
671
672TEST_F(BLASTBufferQueueTest, ScaleCroppedBufferToWindowSize) {
673 // add black background
674 auto bg = mClient->createSurface(String8("BGTest"), 0, 0, PIXEL_FORMAT_RGBA_8888,
675 ISurfaceComposerClient::eFXSurfaceEffect);
676 ASSERT_NE(nullptr, bg.get());
677 Transaction t;
Dominik Laskowski29fa1462021-04-27 15:51:50 -0700678 t.setLayerStack(bg, ui::DEFAULT_LAYER_STACK)
Vishnu Nair5cc9ac02021-04-19 13:23:38 -0700679 .setCrop(bg, Rect(0, 0, mDisplayWidth, mDisplayHeight))
680 .setColor(bg, half3{0, 0, 0})
681 .setLayer(bg, 0)
682 .apply();
683
684 Rect windowSize(1000, 1000);
685 Rect bufferSize(500, 500);
686 Rect bufferCrop(100, 100, 350, 350);
687
688 BLASTBufferQueueHelper adapter(mSurfaceControl, windowSize.getWidth(), windowSize.getHeight());
689 sp<IGraphicBufferProducer> igbProducer;
690 setUpProducer(adapter, igbProducer);
691 int slot;
692 sp<Fence> fence;
693 sp<GraphicBuffer> buf;
694 auto ret = igbProducer->dequeueBuffer(&slot, &fence, bufferSize.getWidth(),
695 bufferSize.getHeight(), PIXEL_FORMAT_RGBA_8888,
696 GRALLOC_USAGE_SW_WRITE_OFTEN, nullptr, nullptr);
697 ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, ret);
698 ASSERT_EQ(OK, igbProducer->requestBuffer(slot, &buf));
699
700 uint32_t* bufData;
701 buf->lock(static_cast<uint32_t>(GraphicBuffer::USAGE_SW_WRITE_OFTEN),
702 reinterpret_cast<void**>(&bufData));
703 // fill buffer with grey
704 fillBuffer(bufData, bufferSize, buf->getStride(), 127, 127, 127);
705
706 // fill crop area with different colors so we can verify the cropped region has been scaled
707 // correctly.
708 fillBuffer(bufData, Rect(100, 100, 225, 225), buf->getStride(), /* rgb */ 255, 0, 0);
709 fillBuffer(bufData, Rect(100, 226, 225, 350), buf->getStride(), /* rgb */ 0, 255, 0);
710 fillBuffer(bufData, Rect(226, 100, 350, 225), buf->getStride(), /* rgb */ 0, 0, 255);
711 fillBuffer(bufData, Rect(226, 226, 350, 350), buf->getStride(), /* rgb */ 255, 0, 0);
712 buf->unlock();
713
714 IGraphicBufferProducer::QueueBufferOutput qbOutput;
715 IGraphicBufferProducer::QueueBufferInput input(systemTime(), true /* autotimestamp */,
716 HAL_DATASPACE_UNKNOWN,
717 bufferCrop /* Rect::INVALID_RECT */,
718 NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW, 0,
719 Fence::NO_FENCE);
720 igbProducer->queueBuffer(slot, input, &qbOutput);
721 ASSERT_NE(ui::Transform::ROT_INVALID, qbOutput.transformHint);
722
723 adapter.waitForCallbacks();
724
725 ASSERT_EQ(NO_ERROR, captureDisplay(mCaptureArgs, mCaptureResults));
726 // Verify cropped region is scaled correctly.
727 ASSERT_NO_FATAL_FAILURE(checkScreenCapture(255, 0, 0, {10, 10, 490, 490}));
728 ASSERT_NO_FATAL_FAILURE(checkScreenCapture(0, 255, 0, {10, 510, 490, 990}));
729 ASSERT_NO_FATAL_FAILURE(checkScreenCapture(0, 0, 255, {510, 10, 990, 490}));
730 ASSERT_NO_FATAL_FAILURE(checkScreenCapture(255, 0, 0, {510, 510, 990, 990}));
731 // Verify outside region is black.
732 ASSERT_NO_FATAL_FAILURE(checkScreenCapture(0, 0, 0,
733 {0, 0, (int32_t)windowSize.getWidth(),
734 (int32_t)windowSize.getHeight()},
735 /*border*/ 0, /*outsideRegion*/ true));
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800736}
737
Vishnu Nair932f6ae2021-09-29 17:33:10 -0700738// b/196339769 verify we can can update the requested size while the in FREEZE scaling mode and
739// scale the buffer properly when the mode changes to SCALE_TO_WINDOW
740TEST_F(BLASTBufferQueueTest, ScalingModeChanges) {
741 uint8_t r = 255;
742 uint8_t g = 0;
743 uint8_t b = 0;
744
745 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight / 4);
746 sp<IGraphicBufferProducer> igbProducer;
747 setUpProducer(adapter, igbProducer);
748 {
749 int slot;
750 sp<Fence> fence;
751 sp<GraphicBuffer> buf;
752 auto ret = igbProducer->dequeueBuffer(&slot, &fence, mDisplayWidth, mDisplayHeight / 4,
753 PIXEL_FORMAT_RGBA_8888, GRALLOC_USAGE_SW_WRITE_OFTEN,
754 nullptr, nullptr);
755 ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, ret);
756 ASSERT_EQ(OK, igbProducer->requestBuffer(slot, &buf));
757
758 uint32_t* bufData;
759 buf->lock(static_cast<uint32_t>(GraphicBuffer::USAGE_SW_WRITE_OFTEN),
760 reinterpret_cast<void**>(&bufData));
761 fillBuffer(bufData, Rect(buf->getWidth(), buf->getHeight()), buf->getStride(), r, g, b);
762 buf->unlock();
763
764 IGraphicBufferProducer::QueueBufferOutput qbOutput;
765 IGraphicBufferProducer::QueueBufferInput input(systemTime(), true /* autotimestamp */,
766 HAL_DATASPACE_UNKNOWN, {},
767 NATIVE_WINDOW_SCALING_MODE_FREEZE, 0,
768 Fence::NO_FENCE);
769 igbProducer->queueBuffer(slot, input, &qbOutput);
770 adapter.waitForCallbacks();
771 }
772 // capture screen and verify that it is red
773 ASSERT_EQ(NO_ERROR, captureDisplay(mCaptureArgs, mCaptureResults));
774
775 ASSERT_NO_FATAL_FAILURE(
776 checkScreenCapture(r, g, b,
777 {0, 0, (int32_t)mDisplayWidth, (int32_t)mDisplayHeight / 4}));
778
779 // update the size to half the display and dequeue a buffer quarter of the display.
780 adapter.update(mSurfaceControl, mDisplayWidth, mDisplayHeight / 2);
781
782 {
783 int slot;
784 sp<Fence> fence;
785 sp<GraphicBuffer> buf;
786 auto ret = igbProducer->dequeueBuffer(&slot, &fence, mDisplayWidth, mDisplayHeight / 8,
787 PIXEL_FORMAT_RGBA_8888, GRALLOC_USAGE_SW_WRITE_OFTEN,
788 nullptr, nullptr);
789 ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, ret);
790 ASSERT_EQ(OK, igbProducer->requestBuffer(slot, &buf));
791
792 uint32_t* bufData;
793 buf->lock(static_cast<uint32_t>(GraphicBuffer::USAGE_SW_WRITE_OFTEN),
794 reinterpret_cast<void**>(&bufData));
795 g = 255;
796 fillBuffer(bufData, Rect(buf->getWidth(), buf->getHeight()), buf->getStride(), r, g, b);
797 buf->unlock();
798
799 IGraphicBufferProducer::QueueBufferOutput qbOutput;
800 IGraphicBufferProducer::QueueBufferInput input(systemTime(), true /* autotimestamp */,
801 HAL_DATASPACE_UNKNOWN, {},
802 NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW,
803 0, Fence::NO_FENCE);
804 igbProducer->queueBuffer(slot, input, &qbOutput);
805 adapter.waitForCallbacks();
806 }
807 // capture screen and verify that it is red
808 ASSERT_EQ(NO_ERROR, captureDisplay(mCaptureArgs, mCaptureResults));
809 // verify we still scale the buffer to the new size (half the screen height)
810 ASSERT_NO_FATAL_FAILURE(
811 checkScreenCapture(r, g, b,
812 {0, 0, (int32_t)mDisplayWidth, (int32_t)mDisplayHeight / 2}));
813}
814
chaviwd7deef72021-10-06 11:53:40 -0500815TEST_F(BLASTBufferQueueTest, SyncThenNoSync) {
816 uint8_t r = 255;
817 uint8_t g = 0;
818 uint8_t b = 0;
819
820 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
821
822 sp<IGraphicBufferProducer> igbProducer;
823 setUpProducer(adapter, igbProducer);
824
chaviwa1c4c822021-11-10 18:11:58 -0600825 Transaction sync;
Tianhao Yao4861b102022-02-03 20:18:35 +0000826 adapter.setSyncTransaction(sync);
chaviwd7deef72021-10-06 11:53:40 -0500827 queueBuffer(igbProducer, 0, 255, 0, 0);
828
829 // queue non sync buffer, so this one should get blocked
830 // Add a present delay to allow the first screenshot to get taken.
831 nsecs_t presentTimeDelay = std::chrono::nanoseconds(500ms).count();
832 queueBuffer(igbProducer, r, g, b, presentTimeDelay);
833
834 CallbackHelper transactionCallback;
chaviwa1c4c822021-11-10 18:11:58 -0600835 sync.addTransactionCompletedCallback(transactionCallback.function,
chaviwd7deef72021-10-06 11:53:40 -0500836 transactionCallback.getContext())
837 .apply();
838
839 CallbackData callbackData;
840 transactionCallback.getCallbackData(&callbackData);
841
chaviw0acd33a2021-11-02 11:55:37 -0500842 // capture screen and verify that it is green
chaviwd7deef72021-10-06 11:53:40 -0500843 ASSERT_EQ(NO_ERROR, captureDisplay(mCaptureArgs, mCaptureResults));
844 ASSERT_NO_FATAL_FAILURE(
845 checkScreenCapture(0, 255, 0, {0, 0, (int32_t)mDisplayWidth, (int32_t)mDisplayHeight}));
846
847 mProducerListener->waitOnNumberReleased(1);
848 ASSERT_EQ(NO_ERROR, captureDisplay(mCaptureArgs, mCaptureResults));
849 ASSERT_NO_FATAL_FAILURE(
850 checkScreenCapture(r, g, b, {0, 0, (int32_t)mDisplayWidth, (int32_t)mDisplayHeight}));
851}
852
853TEST_F(BLASTBufferQueueTest, MultipleSyncTransactions) {
854 uint8_t r = 255;
855 uint8_t g = 0;
856 uint8_t b = 0;
857
858 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
859
860 sp<IGraphicBufferProducer> igbProducer;
861 setUpProducer(adapter, igbProducer);
862
863 Transaction mainTransaction;
864
chaviwa1c4c822021-11-10 18:11:58 -0600865 Transaction sync;
Tianhao Yao4861b102022-02-03 20:18:35 +0000866 adapter.setSyncTransaction(sync);
chaviwd7deef72021-10-06 11:53:40 -0500867 queueBuffer(igbProducer, 0, 255, 0, 0);
868
chaviwa1c4c822021-11-10 18:11:58 -0600869 mainTransaction.merge(std::move(sync));
chaviwd7deef72021-10-06 11:53:40 -0500870
Tianhao Yao4861b102022-02-03 20:18:35 +0000871 adapter.setSyncTransaction(sync);
chaviwd7deef72021-10-06 11:53:40 -0500872 queueBuffer(igbProducer, r, g, b, 0);
873
chaviwa1c4c822021-11-10 18:11:58 -0600874 mainTransaction.merge(std::move(sync));
chaviwd7deef72021-10-06 11:53:40 -0500875 // Expect 1 buffer to be released even before sending to SurfaceFlinger
876 mProducerListener->waitOnNumberReleased(1);
877
878 CallbackHelper transactionCallback;
879 mainTransaction
880 .addTransactionCompletedCallback(transactionCallback.function,
881 transactionCallback.getContext())
882 .apply();
883
884 CallbackData callbackData;
885 transactionCallback.getCallbackData(&callbackData);
886
887 // capture screen and verify that it is red
888 ASSERT_EQ(NO_ERROR, captureDisplay(mCaptureArgs, mCaptureResults));
889 ASSERT_NO_FATAL_FAILURE(
890 checkScreenCapture(r, g, b, {0, 0, (int32_t)mDisplayWidth, (int32_t)mDisplayHeight}));
891}
892
893TEST_F(BLASTBufferQueueTest, MultipleSyncTransactionWithNonSync) {
894 uint8_t r = 255;
895 uint8_t g = 0;
896 uint8_t b = 0;
897
898 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
899
900 sp<IGraphicBufferProducer> igbProducer;
901 setUpProducer(adapter, igbProducer);
902
903 Transaction mainTransaction;
904
chaviwa1c4c822021-11-10 18:11:58 -0600905 Transaction sync;
chaviwd7deef72021-10-06 11:53:40 -0500906 // queue a sync transaction
Tianhao Yao4861b102022-02-03 20:18:35 +0000907 adapter.setSyncTransaction(sync);
chaviwd7deef72021-10-06 11:53:40 -0500908 queueBuffer(igbProducer, 0, 255, 0, 0);
909
chaviwa1c4c822021-11-10 18:11:58 -0600910 mainTransaction.merge(std::move(sync));
chaviwd7deef72021-10-06 11:53:40 -0500911
chaviwa1c4c822021-11-10 18:11:58 -0600912 // queue another buffer without setting sync transaction
chaviwd7deef72021-10-06 11:53:40 -0500913 queueBuffer(igbProducer, 0, 0, 255, 0);
914
915 // queue another sync transaction
Tianhao Yao4861b102022-02-03 20:18:35 +0000916 adapter.setSyncTransaction(sync);
chaviwd7deef72021-10-06 11:53:40 -0500917 queueBuffer(igbProducer, r, g, b, 0);
918 // Expect 1 buffer to be released because the non sync transaction should merge
919 // with the sync
920 mProducerListener->waitOnNumberReleased(1);
921
chaviwa1c4c822021-11-10 18:11:58 -0600922 mainTransaction.merge(std::move(sync));
chaviwd7deef72021-10-06 11:53:40 -0500923 // Expect 2 buffers to be released due to merging the two syncs.
924 mProducerListener->waitOnNumberReleased(2);
925
926 CallbackHelper transactionCallback;
927 mainTransaction
928 .addTransactionCompletedCallback(transactionCallback.function,
929 transactionCallback.getContext())
930 .apply();
931
932 CallbackData callbackData;
933 transactionCallback.getCallbackData(&callbackData);
934
935 // capture screen and verify that it is red
936 ASSERT_EQ(NO_ERROR, captureDisplay(mCaptureArgs, mCaptureResults));
937 ASSERT_NO_FATAL_FAILURE(
938 checkScreenCapture(r, g, b, {0, 0, (int32_t)mDisplayWidth, (int32_t)mDisplayHeight}));
939}
940
941TEST_F(BLASTBufferQueueTest, MultipleSyncRunOutOfBuffers) {
942 uint8_t r = 255;
943 uint8_t g = 0;
944 uint8_t b = 0;
945
946 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
947
948 sp<IGraphicBufferProducer> igbProducer;
949 setUpProducer(adapter, igbProducer, 3);
950
951 Transaction mainTransaction;
952
chaviwa1c4c822021-11-10 18:11:58 -0600953 Transaction sync;
chaviwd7deef72021-10-06 11:53:40 -0500954 // queue a sync transaction
Tianhao Yao4861b102022-02-03 20:18:35 +0000955 adapter.setSyncTransaction(sync);
chaviwd7deef72021-10-06 11:53:40 -0500956 queueBuffer(igbProducer, 0, 255, 0, 0);
957
chaviwa1c4c822021-11-10 18:11:58 -0600958 mainTransaction.merge(std::move(sync));
chaviwd7deef72021-10-06 11:53:40 -0500959
chaviwa1c4c822021-11-10 18:11:58 -0600960 // queue a few buffers without setting sync transaction
chaviwd7deef72021-10-06 11:53:40 -0500961 queueBuffer(igbProducer, 0, 0, 255, 0);
962 queueBuffer(igbProducer, 0, 0, 255, 0);
963 queueBuffer(igbProducer, 0, 0, 255, 0);
964
965 // queue another sync transaction
Tianhao Yao4861b102022-02-03 20:18:35 +0000966 adapter.setSyncTransaction(sync);
chaviwd7deef72021-10-06 11:53:40 -0500967 queueBuffer(igbProducer, r, g, b, 0);
968 // Expect 3 buffers to be released because the non sync transactions should merge
969 // with the sync
970 mProducerListener->waitOnNumberReleased(3);
971
chaviwa1c4c822021-11-10 18:11:58 -0600972 mainTransaction.merge(std::move(sync));
chaviwd7deef72021-10-06 11:53:40 -0500973 // Expect 4 buffers to be released due to merging the two syncs.
974 mProducerListener->waitOnNumberReleased(4);
975
976 CallbackHelper transactionCallback;
977 mainTransaction
978 .addTransactionCompletedCallback(transactionCallback.function,
979 transactionCallback.getContext())
980 .apply();
981
982 CallbackData callbackData;
983 transactionCallback.getCallbackData(&callbackData);
984
985 // capture screen and verify that it is red
986 ASSERT_EQ(NO_ERROR, captureDisplay(mCaptureArgs, mCaptureResults));
987 ASSERT_NO_FATAL_FAILURE(
988 checkScreenCapture(r, g, b, {0, 0, (int32_t)mDisplayWidth, (int32_t)mDisplayHeight}));
989}
990
991// Tests BBQ with a sync transaction when the buffers acquired reaches max and the only way to
992// continue processing is for a release callback from SurfaceFlinger.
993// This is done by sending a buffer to SF so it can release the previous one and allow BBQ to
994// continue acquiring buffers.
995TEST_F(BLASTBufferQueueTest, RunOutOfBuffersWaitingOnSF) {
996 uint8_t r = 255;
997 uint8_t g = 0;
998 uint8_t b = 0;
999
1000 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
1001
1002 sp<IGraphicBufferProducer> igbProducer;
1003 setUpProducer(adapter, igbProducer, 4);
1004
1005 Transaction mainTransaction;
1006
1007 // Send a buffer to SF
1008 queueBuffer(igbProducer, 0, 255, 0, 0);
1009
chaviwa1c4c822021-11-10 18:11:58 -06001010 Transaction sync;
chaviwd7deef72021-10-06 11:53:40 -05001011 // queue a sync transaction
Tianhao Yao4861b102022-02-03 20:18:35 +00001012 adapter.setSyncTransaction(sync);
chaviwd7deef72021-10-06 11:53:40 -05001013 queueBuffer(igbProducer, 0, 255, 0, 0);
1014
chaviwa1c4c822021-11-10 18:11:58 -06001015 mainTransaction.merge(std::move(sync));
chaviwd7deef72021-10-06 11:53:40 -05001016
chaviwa1c4c822021-11-10 18:11:58 -06001017 // queue a few buffers without setting sync transaction
chaviwd7deef72021-10-06 11:53:40 -05001018 queueBuffer(igbProducer, 0, 0, 255, 0);
1019 queueBuffer(igbProducer, 0, 0, 255, 0);
1020 queueBuffer(igbProducer, 0, 0, 255, 0);
1021
1022 // apply the first synced buffer to ensure we have to wait on SF
1023 mainTransaction.apply();
1024
1025 // queue another sync transaction
Tianhao Yao4861b102022-02-03 20:18:35 +00001026 adapter.setSyncTransaction(sync);
chaviwd7deef72021-10-06 11:53:40 -05001027 queueBuffer(igbProducer, r, g, b, 0);
1028 // Expect 2 buffers to be released because the non sync transactions should merge
1029 // with the sync
1030 mProducerListener->waitOnNumberReleased(3);
1031
chaviwa1c4c822021-11-10 18:11:58 -06001032 mainTransaction.merge(std::move(sync));
chaviwd7deef72021-10-06 11:53:40 -05001033
1034 CallbackHelper transactionCallback;
1035 mainTransaction
1036 .addTransactionCompletedCallback(transactionCallback.function,
1037 transactionCallback.getContext())
1038 .apply();
1039
1040 CallbackData callbackData;
1041 transactionCallback.getCallbackData(&callbackData);
1042
1043 // capture screen and verify that it is red
1044 ASSERT_EQ(NO_ERROR, captureDisplay(mCaptureArgs, mCaptureResults));
1045 ASSERT_NO_FATAL_FAILURE(
1046 checkScreenCapture(r, g, b, {0, 0, (int32_t)mDisplayWidth, (int32_t)mDisplayHeight}));
1047}
1048
Tianhao Yao4861b102022-02-03 20:18:35 +00001049TEST_F(BLASTBufferQueueTest, SyncNextTransactionAcquireMultipleBuffers) {
chaviw0acd33a2021-11-02 11:55:37 -05001050 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
1051
1052 sp<IGraphicBufferProducer> igbProducer;
1053 setUpProducer(adapter, igbProducer);
1054
1055 Transaction next;
Tianhao Yao4861b102022-02-03 20:18:35 +00001056 adapter.setSyncTransaction(next, false);
chaviw0acd33a2021-11-02 11:55:37 -05001057 queueBuffer(igbProducer, 0, 255, 0, 0);
1058 queueBuffer(igbProducer, 0, 0, 255, 0);
1059 // There should only be one frame submitted since the first frame will be released.
1060 adapter.validateNumFramesSubmitted(1);
Tianhao Yao4861b102022-02-03 20:18:35 +00001061 adapter.stopContinuousSyncTransaction();
chaviw0acd33a2021-11-02 11:55:37 -05001062
1063 // queue non sync buffer, so this one should get blocked
1064 // Add a present delay to allow the first screenshot to get taken.
1065 nsecs_t presentTimeDelay = std::chrono::nanoseconds(500ms).count();
1066 queueBuffer(igbProducer, 255, 0, 0, presentTimeDelay);
1067
1068 CallbackHelper transactionCallback;
1069 next.addTransactionCompletedCallback(transactionCallback.function,
1070 transactionCallback.getContext())
1071 .apply();
1072
1073 CallbackData callbackData;
1074 transactionCallback.getCallbackData(&callbackData);
1075
1076 // capture screen and verify that it is blue
1077 ASSERT_EQ(NO_ERROR, captureDisplay(mCaptureArgs, mCaptureResults));
1078 ASSERT_NO_FATAL_FAILURE(
1079 checkScreenCapture(0, 0, 255, {0, 0, (int32_t)mDisplayWidth, (int32_t)mDisplayHeight}));
1080
1081 mProducerListener->waitOnNumberReleased(2);
1082 // capture screen and verify that it is red
1083 ASSERT_EQ(NO_ERROR, captureDisplay(mCaptureArgs, mCaptureResults));
1084 ASSERT_NO_FATAL_FAILURE(
1085 checkScreenCapture(255, 0, 0, {0, 0, (int32_t)mDisplayWidth, (int32_t)mDisplayHeight}));
1086}
1087
chaviw3b4bdcf2022-03-17 09:27:03 -05001088TEST_F(BLASTBufferQueueTest, SyncNextTransactionOverwrite) {
1089 std::mutex mutex;
1090 std::condition_variable callbackReceivedCv;
1091 bool receivedCallback = false;
1092
1093 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
1094 ASSERT_EQ(nullptr, adapter.getTransactionReadyCallback());
1095 auto callback = [&](Transaction*) {
1096 std::unique_lock<std::mutex> lock(mutex);
1097 receivedCallback = true;
1098 callbackReceivedCv.notify_one();
1099 };
1100 adapter.syncNextTransaction(callback);
1101 ASSERT_NE(nullptr, adapter.getTransactionReadyCallback());
1102
1103 auto callback2 = [](Transaction*) {};
Chavi Weingartenc398c012023-04-12 17:26:02 +00001104 ASSERT_FALSE(adapter.syncNextTransaction(callback2));
1105
1106 sp<IGraphicBufferProducer> igbProducer;
1107 setUpProducer(adapter, igbProducer);
1108 queueBuffer(igbProducer, 0, 255, 0, 0);
chaviw3b4bdcf2022-03-17 09:27:03 -05001109
1110 std::unique_lock<std::mutex> lock(mutex);
1111 if (!receivedCallback) {
1112 ASSERT_NE(callbackReceivedCv.wait_for(lock, std::chrono::seconds(3)),
1113 std::cv_status::timeout)
1114 << "did not receive callback";
1115 }
1116
1117 ASSERT_TRUE(receivedCallback);
1118}
1119
Chavi Weingartenc398c012023-04-12 17:26:02 +00001120TEST_F(BLASTBufferQueueTest, ClearSyncTransaction) {
1121 std::mutex mutex;
1122 std::condition_variable callbackReceivedCv;
1123 bool receivedCallback = false;
1124
1125 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
1126 ASSERT_EQ(nullptr, adapter.getTransactionReadyCallback());
1127 auto callback = [&](Transaction*) {
1128 std::unique_lock<std::mutex> lock(mutex);
1129 receivedCallback = true;
1130 callbackReceivedCv.notify_one();
1131 };
1132 adapter.syncNextTransaction(callback);
1133 ASSERT_NE(nullptr, adapter.getTransactionReadyCallback());
1134
1135 adapter.clearSyncTransaction();
1136
1137 sp<IGraphicBufferProducer> igbProducer;
1138 setUpProducer(adapter, igbProducer);
1139 queueBuffer(igbProducer, 0, 255, 0, 0);
1140
1141 std::unique_lock<std::mutex> lock(mutex);
1142 if (!receivedCallback) {
1143 ASSERT_EQ(callbackReceivedCv.wait_for(lock, std::chrono::seconds(3)),
1144 std::cv_status::timeout)
1145 << "did not receive callback";
1146 }
1147
1148 ASSERT_FALSE(receivedCallback);
1149}
1150
chaviwc1cf4022022-06-03 13:32:33 -05001151TEST_F(BLASTBufferQueueTest, SyncNextTransactionDropBuffer) {
1152 uint8_t r = 255;
1153 uint8_t g = 0;
1154 uint8_t b = 0;
1155
1156 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
1157
1158 sp<IGraphicBufferProducer> igbProducer;
1159 setUpProducer(adapter, igbProducer);
1160
1161 Transaction sync;
1162 adapter.setSyncTransaction(sync);
1163 queueBuffer(igbProducer, 0, 255, 0, 0);
1164
1165 // Merge a transaction that has a complete callback into the next frame so we can get notified
1166 // when to take a screenshot
1167 CallbackHelper transactionCallback;
1168 Transaction t;
1169 t.addTransactionCompletedCallback(transactionCallback.function,
1170 transactionCallback.getContext());
1171 adapter.mergeWithNextTransaction(&t, 2);
1172 queueBuffer(igbProducer, r, g, b, 0);
1173
1174 // Drop the buffer, but ensure the next one continues to get processed.
1175 sync.setBuffer(mSurfaceControl, nullptr);
1176
1177 CallbackData callbackData;
1178 transactionCallback.getCallbackData(&callbackData);
1179 ASSERT_EQ(NO_ERROR, captureDisplay(mCaptureArgs, mCaptureResults));
1180 ASSERT_NO_FATAL_FAILURE(
1181 checkScreenCapture(r, g, b, {0, 0, (int32_t)mDisplayWidth, (int32_t)mDisplayHeight}));
Rob Carr4f797ab2022-07-07 18:29:22 +00001182 sync.apply();
chaviwc1cf4022022-06-03 13:32:33 -05001183}
1184
Vishnu Nair1e8bf102021-12-28 14:36:59 -08001185// This test will currently fail because the old surfacecontrol will steal the last presented buffer
1186// until the old surface control is destroyed. This is not necessarily a bug but to document a
1187// limitation with the update API and to test any changes to make the api more robust. The current
1188// approach for the client is to recreate the blastbufferqueue when the surfacecontrol updates.
1189TEST_F(BLASTBufferQueueTest, DISABLED_DisconnectProducerTest) {
1190 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
1191 std::vector<sp<SurfaceControl>> surfaceControls;
1192 sp<IGraphicBufferProducer> igbProducer;
1193 for (int i = 0; i < 10; i++) {
1194 sp<SurfaceControl> sc =
1195 mClient->createSurface(String8("TestSurface"), mDisplayWidth, mDisplayHeight,
1196 PIXEL_FORMAT_RGBA_8888,
1197 ISurfaceComposerClient::eFXSurfaceBufferState,
1198 /*parent*/ nullptr);
1199 Transaction()
1200 .setLayerStack(mSurfaceControl, ui::DEFAULT_LAYER_STACK)
1201 .setLayer(mSurfaceControl, std::numeric_limits<int32_t>::max())
1202 .show(mSurfaceControl)
1203 .setDataspace(mSurfaceControl, ui::Dataspace::V0_SRGB)
1204 .apply(true);
1205 surfaceControls.push_back(sc);
1206 adapter.update(sc, mDisplayWidth, mDisplayHeight);
1207
1208 setUpProducer(adapter, igbProducer);
1209 Transaction next;
1210 queueBuffer(igbProducer, 0, 255, 0, 0);
1211 queueBuffer(igbProducer, 0, 0, 255, 0);
Tianhao Yao4861b102022-02-03 20:18:35 +00001212 adapter.setSyncTransaction(next, true);
Vishnu Nair1e8bf102021-12-28 14:36:59 -08001213 queueBuffer(igbProducer, 255, 0, 0, 0);
1214
1215 CallbackHelper transactionCallback;
1216 next.addTransactionCompletedCallback(transactionCallback.function,
1217 transactionCallback.getContext())
1218 .apply();
1219
1220 CallbackData callbackData;
1221 transactionCallback.getCallbackData(&callbackData);
1222 // capture screen and verify that it is red
1223 ASSERT_EQ(NO_ERROR, captureDisplay(mCaptureArgs, mCaptureResults));
1224 ASSERT_NO_FATAL_FAILURE(
1225 checkScreenCapture(255, 0, 0,
1226 {0, 0, (int32_t)mDisplayWidth, (int32_t)mDisplayHeight}));
1227 igbProducer->disconnect(NATIVE_WINDOW_API_CPU);
1228 }
1229}
1230
1231// See DISABLED_DisconnectProducerTest
1232TEST_F(BLASTBufferQueueTest, DISABLED_UpdateSurfaceControlTest) {
1233 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
1234 std::vector<sp<SurfaceControl>> surfaceControls;
1235 sp<IGraphicBufferProducer> igbProducer;
1236 for (int i = 0; i < 10; i++) {
1237 sp<SurfaceControl> sc =
1238 mClient->createSurface(String8("TestSurface"), mDisplayWidth, mDisplayHeight,
1239 PIXEL_FORMAT_RGBA_8888,
1240 ISurfaceComposerClient::eFXSurfaceBufferState,
1241 /*parent*/ nullptr);
1242 Transaction()
1243 .setLayerStack(mSurfaceControl, ui::DEFAULT_LAYER_STACK)
1244 .setLayer(mSurfaceControl, std::numeric_limits<int32_t>::max())
1245 .show(mSurfaceControl)
1246 .setDataspace(mSurfaceControl, ui::Dataspace::V0_SRGB)
1247 .apply(true);
1248 surfaceControls.push_back(sc);
1249 adapter.update(sc, mDisplayWidth, mDisplayHeight);
1250 setUpProducer(adapter, igbProducer);
1251
1252 Transaction next;
1253 queueBuffer(igbProducer, 0, 255, 0, 0);
1254 queueBuffer(igbProducer, 0, 0, 255, 0);
Tianhao Yao4861b102022-02-03 20:18:35 +00001255 adapter.setSyncTransaction(next, true);
Vishnu Nair1e8bf102021-12-28 14:36:59 -08001256 queueBuffer(igbProducer, 255, 0, 0, 0);
1257
1258 CallbackHelper transactionCallback;
1259 next.addTransactionCompletedCallback(transactionCallback.function,
1260 transactionCallback.getContext())
1261 .apply();
1262
1263 CallbackData callbackData;
1264 transactionCallback.getCallbackData(&callbackData);
1265 // capture screen and verify that it is red
1266 ASSERT_EQ(NO_ERROR, captureDisplay(mCaptureArgs, mCaptureResults));
1267 ASSERT_NO_FATAL_FAILURE(
1268 checkScreenCapture(255, 0, 0,
1269 {0, 0, (int32_t)mDisplayWidth, (int32_t)mDisplayHeight}));
1270 }
1271}
1272
Vishnu Nair89496122020-12-14 17:14:53 -08001273class TestProducerListener : public BnProducerListener {
1274public:
1275 sp<IGraphicBufferProducer> mIgbp;
1276 TestProducerListener(const sp<IGraphicBufferProducer>& igbp) : mIgbp(igbp) {}
1277 void onBufferReleased() override {
1278 sp<GraphicBuffer> buffer;
1279 sp<Fence> fence;
1280 mIgbp->detachNextBuffer(&buffer, &fence);
1281 }
1282};
1283
1284TEST_F(BLASTBufferQueueTest, CustomProducerListener) {
1285 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
1286 sp<IGraphicBufferProducer> igbProducer = adapter.getIGraphicBufferProducer();
1287 ASSERT_NE(nullptr, igbProducer.get());
1288 ASSERT_EQ(NO_ERROR, igbProducer->setMaxDequeuedBufferCount(2));
1289 IGraphicBufferProducer::QueueBufferOutput qbOutput;
1290 ASSERT_EQ(NO_ERROR,
1291 igbProducer->connect(new TestProducerListener(igbProducer), NATIVE_WINDOW_API_CPU,
1292 false, &qbOutput));
1293 ASSERT_NE(ui::Transform::ROT_INVALID, qbOutput.transformHint);
1294 for (int i = 0; i < 3; i++) {
1295 int slot;
1296 sp<Fence> fence;
1297 sp<GraphicBuffer> buf;
1298 auto ret = igbProducer->dequeueBuffer(&slot, &fence, mDisplayWidth, mDisplayHeight,
1299 PIXEL_FORMAT_RGBA_8888, GRALLOC_USAGE_SW_WRITE_OFTEN,
1300 nullptr, nullptr);
1301 ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, ret);
1302 ASSERT_EQ(OK, igbProducer->requestBuffer(slot, &buf));
1303 IGraphicBufferProducer::QueueBufferOutput qbOutput;
Vishnu Nair1506b182021-02-22 14:35:15 -08001304 IGraphicBufferProducer::QueueBufferInput input(systemTime(), true /* autotimestamp */,
1305 HAL_DATASPACE_UNKNOWN,
Vishnu Nair89496122020-12-14 17:14:53 -08001306 Rect(mDisplayWidth, mDisplayHeight),
1307 NATIVE_WINDOW_SCALING_MODE_FREEZE, 0,
1308 Fence::NO_FENCE);
1309 igbProducer->queueBuffer(slot, input, &qbOutput);
1310 }
1311 adapter.waitForCallbacks();
1312}
1313
Vishnu Nair17dde612020-12-28 11:39:59 -08001314TEST_F(BLASTBufferQueueTest, QueryNativeWindowQueuesToWindowComposer) {
1315 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
1316
1317 sp<android::Surface> surface = new Surface(adapter.getIGraphicBufferProducer());
1318 ANativeWindow* nativeWindow = (ANativeWindow*)(surface.get());
1319 int queuesToNativeWindow = 0;
1320 int err = nativeWindow->query(nativeWindow, NATIVE_WINDOW_QUEUES_TO_WINDOW_COMPOSER,
1321 &queuesToNativeWindow);
1322 ASSERT_EQ(NO_ERROR, err);
1323 ASSERT_EQ(queuesToNativeWindow, 1);
1324}
1325
Vishnu Nair083efd32021-02-12 09:32:30 -08001326// Test a slow producer doesn't hold up a faster producer from the same client. Essentially tests
1327// BBQ uses separate transaction queues.
Vishnu Nair277142c2021-01-05 18:35:29 -08001328TEST_F(BLASTBufferQueueTest, OutOfOrderTransactionTest) {
1329 sp<SurfaceControl> bgSurface =
1330 mClient->createSurface(String8("BGTest"), 0, 0, PIXEL_FORMAT_RGBA_8888,
1331 ISurfaceComposerClient::eFXSurfaceBufferState);
1332 ASSERT_NE(nullptr, bgSurface.get());
1333 Transaction t;
Dominik Laskowski29fa1462021-04-27 15:51:50 -07001334 t.setLayerStack(bgSurface, ui::DEFAULT_LAYER_STACK)
Vishnu Nair277142c2021-01-05 18:35:29 -08001335 .show(bgSurface)
1336 .setDataspace(bgSurface, ui::Dataspace::V0_SRGB)
Vishnu Nair277142c2021-01-05 18:35:29 -08001337 .setLayer(bgSurface, std::numeric_limits<int32_t>::max() - 1)
1338 .apply();
1339
1340 BLASTBufferQueueHelper slowAdapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
1341 sp<IGraphicBufferProducer> slowIgbProducer;
1342 setUpProducer(slowAdapter, slowIgbProducer);
1343 nsecs_t presentTimeDelay = std::chrono::nanoseconds(500ms).count();
Vishnu Nair1506b182021-02-22 14:35:15 -08001344 queueBuffer(slowIgbProducer, 0 /* r */, 255 /* g */, 0 /* b */, presentTimeDelay);
Vishnu Nair277142c2021-01-05 18:35:29 -08001345
1346 BLASTBufferQueueHelper fastAdapter(bgSurface, mDisplayWidth, mDisplayHeight);
1347 sp<IGraphicBufferProducer> fastIgbProducer;
1348 setUpProducer(fastAdapter, fastIgbProducer);
1349 uint8_t r = 255;
1350 uint8_t g = 0;
1351 uint8_t b = 0;
1352 queueBuffer(fastIgbProducer, r, g, b, 0 /* presentTimeDelay */);
1353 fastAdapter.waitForCallbacks();
1354
1355 // capture screen and verify that it is red
1356 ASSERT_EQ(NO_ERROR, captureDisplay(mCaptureArgs, mCaptureResults));
1357
1358 ASSERT_NO_FATAL_FAILURE(
Chavi Weingartena5aedbd2021-04-09 13:37:33 +00001359 checkScreenCapture(r, g, b,
1360 {0, 0, (int32_t)mDisplayWidth, (int32_t)mDisplayHeight / 2}));
Vishnu Nair277142c2021-01-05 18:35:29 -08001361}
1362
Vishnu Naira4fbca52021-07-07 16:52:34 -07001363TEST_F(BLASTBufferQueueTest, TransformHint) {
1364 // Transform hint is provided to BBQ via the surface control passed by WM
1365 mSurfaceControl->setTransformHint(ui::Transform::ROT_90);
1366
1367 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
1368 sp<IGraphicBufferProducer> igbProducer = adapter.getIGraphicBufferProducer();
1369 ASSERT_NE(nullptr, igbProducer.get());
1370 ASSERT_EQ(NO_ERROR, igbProducer->setMaxDequeuedBufferCount(2));
1371 sp<Surface> surface = adapter.getSurface();
1372
1373 // Before connecting to the surface, we do not get a valid transform hint
1374 int transformHint;
1375 surface->query(NATIVE_WINDOW_TRANSFORM_HINT, &transformHint);
1376 ASSERT_EQ(ui::Transform::ROT_0, transformHint);
1377
1378 ASSERT_EQ(NO_ERROR,
1379 surface->connect(NATIVE_WINDOW_API_CPU, new TestProducerListener(igbProducer)));
1380
1381 // After connecting to the surface, we should get the correct hint.
1382 surface->query(NATIVE_WINDOW_TRANSFORM_HINT, &transformHint);
1383 ASSERT_EQ(ui::Transform::ROT_90, transformHint);
1384
1385 ANativeWindow_Buffer buffer;
1386 surface->lock(&buffer, nullptr /* inOutDirtyBounds */);
1387
1388 // Transform hint is updated via callbacks or surface control updates
1389 mSurfaceControl->setTransformHint(ui::Transform::ROT_0);
1390 adapter.update(mSurfaceControl, mDisplayWidth, mDisplayHeight);
1391
1392 // The hint does not change and matches the value used when dequeueing the buffer.
1393 surface->query(NATIVE_WINDOW_TRANSFORM_HINT, &transformHint);
1394 ASSERT_EQ(ui::Transform::ROT_90, transformHint);
1395
1396 surface->unlockAndPost();
1397
1398 // After queuing the buffer, we get the updated transform hint
1399 surface->query(NATIVE_WINDOW_TRANSFORM_HINT, &transformHint);
1400 ASSERT_EQ(ui::Transform::ROT_0, transformHint);
1401
1402 adapter.waitForCallbacks();
1403}
1404
Valerie Hau5977fc82019-12-05 15:56:39 -08001405class BLASTBufferQueueTransformTest : public BLASTBufferQueueTest {
1406public:
1407 void test(uint32_t tr) {
1408 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
1409 sp<IGraphicBufferProducer> igbProducer;
1410 setUpProducer(adapter, igbProducer);
1411
1412 auto bufWidth = mDisplayWidth;
1413 auto bufHeight = mDisplayHeight;
1414 int slot;
1415 sp<Fence> fence;
1416 sp<GraphicBuffer> buf;
1417
1418 auto ret = igbProducer->dequeueBuffer(&slot, &fence, bufWidth, bufHeight,
1419 PIXEL_FORMAT_RGBA_8888, GRALLOC_USAGE_SW_WRITE_OFTEN,
1420 nullptr, nullptr);
1421 ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, ret);
1422 ASSERT_EQ(OK, igbProducer->requestBuffer(slot, &buf));
1423
1424 fillQuadrants(buf);
1425
1426 IGraphicBufferProducer::QueueBufferOutput qbOutput;
Vishnu Nair1506b182021-02-22 14:35:15 -08001427 IGraphicBufferProducer::QueueBufferInput input(systemTime(), true /* autotimestamp */,
1428 HAL_DATASPACE_UNKNOWN,
Valerie Hau5977fc82019-12-05 15:56:39 -08001429 Rect(bufWidth, bufHeight),
Vishnu Naire1a42322020-10-02 17:42:04 -07001430 NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW,
1431 tr, Fence::NO_FENCE);
Valerie Hau5977fc82019-12-05 15:56:39 -08001432 igbProducer->queueBuffer(slot, input, &qbOutput);
Dominik Laskowski718f9602019-11-09 20:01:35 -08001433 ASSERT_NE(ui::Transform::ROT_INVALID, qbOutput.transformHint);
Valerie Hau5977fc82019-12-05 15:56:39 -08001434
1435 adapter.waitForCallbacks();
chaviw8ffc7b82020-08-18 11:25:37 -07001436 ASSERT_EQ(NO_ERROR, captureDisplay(mCaptureArgs, mCaptureResults));
chaviwd2432892020-07-24 17:42:39 -07001437
Valerie Hau5977fc82019-12-05 15:56:39 -08001438 switch (tr) {
1439 case ui::Transform::ROT_0:
1440 ASSERT_NO_FATAL_FAILURE(checkScreenCapture(0, 0, 0,
1441 {0, 0, (int32_t)mDisplayWidth / 2,
1442 (int32_t)mDisplayHeight / 2},
1443 1));
1444 ASSERT_NO_FATAL_FAILURE(
1445 checkScreenCapture(255, 0, 0,
1446 {(int32_t)mDisplayWidth / 2, 0, (int32_t)mDisplayWidth,
1447 (int32_t)mDisplayHeight / 2},
1448 1));
1449 ASSERT_NO_FATAL_FAILURE(
1450 checkScreenCapture(0, 255, 0,
1451 {(int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight / 2,
1452 (int32_t)mDisplayWidth, (int32_t)mDisplayHeight},
1453 1));
1454 ASSERT_NO_FATAL_FAILURE(
1455 checkScreenCapture(0, 0, 255,
1456 {0, (int32_t)mDisplayHeight / 2,
1457 (int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight},
1458 1));
1459 break;
1460 case ui::Transform::FLIP_H:
1461 ASSERT_NO_FATAL_FAILURE(checkScreenCapture(255, 0, 0,
1462 {0, 0, (int32_t)mDisplayWidth / 2,
1463 (int32_t)mDisplayHeight / 2},
1464 1));
1465 ASSERT_NO_FATAL_FAILURE(
1466 checkScreenCapture(0, 0, 0,
1467 {(int32_t)mDisplayWidth / 2, 0, (int32_t)mDisplayWidth,
1468 (int32_t)mDisplayHeight / 2},
1469 1));
1470 ASSERT_NO_FATAL_FAILURE(
1471 checkScreenCapture(0, 0, 255,
1472 {(int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight / 2,
1473 (int32_t)mDisplayWidth, (int32_t)mDisplayHeight},
1474 1));
1475 ASSERT_NO_FATAL_FAILURE(
1476 checkScreenCapture(0, 255, 0,
1477 {0, (int32_t)mDisplayHeight / 2,
1478 (int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight},
1479 1));
1480 break;
1481 case ui::Transform::FLIP_V:
1482 ASSERT_NO_FATAL_FAILURE(checkScreenCapture(0, 0, 255,
1483 {0, 0, (int32_t)mDisplayWidth / 2,
1484 (int32_t)mDisplayHeight / 2},
1485 1));
1486 ASSERT_NO_FATAL_FAILURE(
1487 checkScreenCapture(0, 255, 0,
1488 {(int32_t)mDisplayWidth / 2, 0, (int32_t)mDisplayWidth,
1489 (int32_t)mDisplayHeight / 2},
1490 1));
1491 ASSERT_NO_FATAL_FAILURE(
1492 checkScreenCapture(255, 0, 0,
1493 {(int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight / 2,
1494 (int32_t)mDisplayWidth, (int32_t)mDisplayHeight},
1495 1));
1496 ASSERT_NO_FATAL_FAILURE(
1497 checkScreenCapture(0, 0, 0,
1498 {0, (int32_t)mDisplayHeight / 2,
1499 (int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight},
1500 1));
1501 break;
1502 case ui::Transform::ROT_90:
1503 ASSERT_NO_FATAL_FAILURE(checkScreenCapture(0, 0, 255,
1504 {0, 0, (int32_t)mDisplayWidth / 2,
1505 (int32_t)mDisplayHeight / 2},
1506 1));
1507 ASSERT_NO_FATAL_FAILURE(
1508 checkScreenCapture(0, 0, 0,
1509 {(int32_t)mDisplayWidth / 2, 0, (int32_t)mDisplayWidth,
1510 (int32_t)mDisplayHeight / 2},
1511 1));
1512 ASSERT_NO_FATAL_FAILURE(
1513 checkScreenCapture(255, 0, 0,
1514 {(int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight / 2,
1515 (int32_t)mDisplayWidth, (int32_t)mDisplayHeight},
1516 1));
1517 ASSERT_NO_FATAL_FAILURE(
1518 checkScreenCapture(0, 255, 0,
1519 {0, (int32_t)mDisplayHeight / 2,
1520 (int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight},
1521 1));
1522 break;
1523 case ui::Transform::ROT_180:
1524 ASSERT_NO_FATAL_FAILURE(checkScreenCapture(0, 255, 0,
1525 {0, 0, (int32_t)mDisplayWidth / 2,
1526 (int32_t)mDisplayHeight / 2},
1527 1));
1528 ASSERT_NO_FATAL_FAILURE(
1529 checkScreenCapture(0, 0, 255,
1530 {(int32_t)mDisplayWidth / 2, 0, (int32_t)mDisplayWidth,
1531 (int32_t)mDisplayHeight / 2},
1532 1));
1533 ASSERT_NO_FATAL_FAILURE(
1534 checkScreenCapture(0, 0, 0,
1535 {(int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight / 2,
1536 (int32_t)mDisplayWidth, (int32_t)mDisplayHeight},
1537 1));
1538 ASSERT_NO_FATAL_FAILURE(
1539 checkScreenCapture(255, 0, 0,
1540 {0, (int32_t)mDisplayHeight / 2,
1541 (int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight},
1542 1));
1543 break;
1544 case ui::Transform::ROT_270:
1545 ASSERT_NO_FATAL_FAILURE(checkScreenCapture(255, 0, 0,
1546 {0, 0, (int32_t)mDisplayWidth / 2,
1547 (int32_t)mDisplayHeight / 2},
1548 1));
1549 ASSERT_NO_FATAL_FAILURE(
1550 checkScreenCapture(0, 255, 0,
1551 {(int32_t)mDisplayWidth / 2, 0, (int32_t)mDisplayWidth,
1552 (int32_t)mDisplayHeight / 2},
1553 1));
1554 ASSERT_NO_FATAL_FAILURE(
1555 checkScreenCapture(0, 0, 255,
1556 {(int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight / 2,
1557 (int32_t)mDisplayWidth, (int32_t)mDisplayHeight},
1558 1));
1559 ASSERT_NO_FATAL_FAILURE(
1560 checkScreenCapture(0, 0, 0,
1561 {0, (int32_t)mDisplayHeight / 2,
1562 (int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight},
1563 1));
1564 }
1565 }
1566};
1567
1568TEST_F(BLASTBufferQueueTransformTest, setTransform_ROT_0) {
1569 test(ui::Transform::ROT_0);
1570}
1571
1572TEST_F(BLASTBufferQueueTransformTest, setTransform_FLIP_H) {
1573 test(ui::Transform::FLIP_H);
1574}
1575
1576TEST_F(BLASTBufferQueueTransformTest, setTransform_FLIP_V) {
1577 test(ui::Transform::FLIP_V);
1578}
1579
1580TEST_F(BLASTBufferQueueTransformTest, setTransform_ROT_90) {
1581 test(ui::Transform::ROT_90);
1582}
1583
1584TEST_F(BLASTBufferQueueTransformTest, setTransform_ROT_180) {
1585 test(ui::Transform::ROT_180);
1586}
1587
1588TEST_F(BLASTBufferQueueTransformTest, setTransform_ROT_270) {
1589 test(ui::Transform::ROT_270);
1590}
Valerie Hau871d6352020-01-29 08:44:02 -08001591
1592class BLASTFrameEventHistoryTest : public BLASTBufferQueueTest {
1593public:
1594 void setUpAndQueueBuffer(const sp<IGraphicBufferProducer>& igbProducer,
Vishnu Nairde66dc72021-06-17 17:54:41 -07001595 nsecs_t* outRequestedPresentTime, nsecs_t* postedTime,
Valerie Hau871d6352020-01-29 08:44:02 -08001596 IGraphicBufferProducer::QueueBufferOutput* qbOutput,
Vishnu Nairde66dc72021-06-17 17:54:41 -07001597 bool getFrameTimestamps, nsecs_t requestedPresentTime = systemTime()) {
Valerie Hau871d6352020-01-29 08:44:02 -08001598 int slot;
1599 sp<Fence> fence;
1600 sp<GraphicBuffer> buf;
1601 auto ret = igbProducer->dequeueBuffer(&slot, &fence, mDisplayWidth, mDisplayHeight,
1602 PIXEL_FORMAT_RGBA_8888, GRALLOC_USAGE_SW_WRITE_OFTEN,
1603 nullptr, nullptr);
Vishnu Nairde66dc72021-06-17 17:54:41 -07001604 if (IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION == ret) {
1605 ASSERT_EQ(OK, igbProducer->requestBuffer(slot, &buf));
1606 }
Valerie Hau871d6352020-01-29 08:44:02 -08001607
Vishnu Nairde66dc72021-06-17 17:54:41 -07001608 *outRequestedPresentTime = requestedPresentTime;
1609 IGraphicBufferProducer::QueueBufferInput input(requestedPresentTime, false,
1610 HAL_DATASPACE_UNKNOWN,
Valerie Hau871d6352020-01-29 08:44:02 -08001611 Rect(mDisplayWidth, mDisplayHeight),
1612 NATIVE_WINDOW_SCALING_MODE_FREEZE, 0,
1613 Fence::NO_FENCE, /*sticky*/ 0,
1614 getFrameTimestamps);
1615 if (postedTime) *postedTime = systemTime();
1616 igbProducer->queueBuffer(slot, input, qbOutput);
1617 }
Vishnu Nair083efd32021-02-12 09:32:30 -08001618 sp<SurfaceControl> mBufferQueueSurfaceControl;
Valerie Hau871d6352020-01-29 08:44:02 -08001619};
1620
1621TEST_F(BLASTFrameEventHistoryTest, FrameEventHistory_Basic) {
1622 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
1623 sp<IGraphicBufferProducer> igbProducer;
1624 ProducerFrameEventHistory history;
1625 setUpProducer(adapter, igbProducer);
1626
1627 IGraphicBufferProducer::QueueBufferOutput qbOutput;
1628 nsecs_t requestedPresentTimeA = 0;
1629 nsecs_t postedTimeA = 0;
1630 setUpAndQueueBuffer(igbProducer, &requestedPresentTimeA, &postedTimeA, &qbOutput, true);
1631 history.applyDelta(qbOutput.frameTimestamps);
1632
1633 FrameEvents* events = nullptr;
1634 events = history.getFrame(1);
1635 ASSERT_NE(nullptr, events);
1636 ASSERT_EQ(1, events->frameNumber);
1637 ASSERT_EQ(requestedPresentTimeA, events->requestedPresentTime);
1638 ASSERT_GE(events->postedTime, postedTimeA);
1639
Vishnu Nair1506b182021-02-22 14:35:15 -08001640 adapter.waitForCallback(1);
Valerie Hau871d6352020-01-29 08:44:02 -08001641
1642 // queue another buffer so we query for frame event deltas
1643 nsecs_t requestedPresentTimeB = 0;
1644 nsecs_t postedTimeB = 0;
1645 setUpAndQueueBuffer(igbProducer, &requestedPresentTimeB, &postedTimeB, &qbOutput, true);
1646 history.applyDelta(qbOutput.frameTimestamps);
1647 events = history.getFrame(1);
1648 ASSERT_NE(nullptr, events);
1649
1650 // frame number, requestedPresentTime, and postTime should not have changed
1651 ASSERT_EQ(1, events->frameNumber);
1652 ASSERT_EQ(requestedPresentTimeA, events->requestedPresentTime);
1653 ASSERT_GE(events->postedTime, postedTimeA);
1654
1655 ASSERT_GE(events->latchTime, postedTimeA);
1656 ASSERT_GE(events->dequeueReadyTime, events->latchTime);
1657 ASSERT_NE(nullptr, events->gpuCompositionDoneFence);
1658 ASSERT_NE(nullptr, events->displayPresentFence);
1659 ASSERT_NE(nullptr, events->releaseFence);
1660
1661 // we should also have gotten the initial values for the next frame
1662 events = history.getFrame(2);
1663 ASSERT_NE(nullptr, events);
1664 ASSERT_EQ(2, events->frameNumber);
1665 ASSERT_EQ(requestedPresentTimeB, events->requestedPresentTime);
1666 ASSERT_GE(events->postedTime, postedTimeB);
Valerie Hau78491e92020-04-15 13:10:56 -07001667
1668 // wait for any callbacks that have not been received
1669 adapter.waitForCallbacks();
Valerie Hau871d6352020-01-29 08:44:02 -08001670}
Vishnu Nair083efd32021-02-12 09:32:30 -08001671
Vishnu Nair083efd32021-02-12 09:32:30 -08001672TEST_F(BLASTFrameEventHistoryTest, FrameEventHistory_DroppedFrame) {
1673 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
1674 sp<IGraphicBufferProducer> igbProducer;
1675 setUpProducer(adapter, igbProducer);
1676
1677 ProducerFrameEventHistory history;
1678 IGraphicBufferProducer::QueueBufferOutput qbOutput;
1679 nsecs_t requestedPresentTimeA = 0;
1680 nsecs_t postedTimeA = 0;
Vishnu Nairde66dc72021-06-17 17:54:41 -07001681 // Present the frame sometime in the future so we can add two frames to the queue so the older
1682 // one will be dropped.
1683 nsecs_t presentTime = systemTime() + std::chrono::nanoseconds(500ms).count();
Vishnu Nair083efd32021-02-12 09:32:30 -08001684 setUpAndQueueBuffer(igbProducer, &requestedPresentTimeA, &postedTimeA, &qbOutput, true,
Vishnu Nairde66dc72021-06-17 17:54:41 -07001685 presentTime);
Vishnu Nair083efd32021-02-12 09:32:30 -08001686 history.applyDelta(qbOutput.frameTimestamps);
1687
1688 FrameEvents* events = nullptr;
1689 events = history.getFrame(1);
1690 ASSERT_NE(nullptr, events);
1691 ASSERT_EQ(1, events->frameNumber);
1692 ASSERT_EQ(requestedPresentTimeA, events->requestedPresentTime);
1693 ASSERT_GE(events->postedTime, postedTimeA);
1694
1695 // queue another buffer so the first can be dropped
1696 nsecs_t requestedPresentTimeB = 0;
1697 nsecs_t postedTimeB = 0;
Vishnu Nairde66dc72021-06-17 17:54:41 -07001698 presentTime = systemTime() + std::chrono::nanoseconds(1ms).count();
1699 setUpAndQueueBuffer(igbProducer, &requestedPresentTimeB, &postedTimeB, &qbOutput, true,
1700 presentTime);
Vishnu Nair083efd32021-02-12 09:32:30 -08001701 history.applyDelta(qbOutput.frameTimestamps);
1702 events = history.getFrame(1);
1703 ASSERT_NE(nullptr, events);
1704
1705 // frame number, requestedPresentTime, and postTime should not have changed
1706 ASSERT_EQ(1, events->frameNumber);
1707 ASSERT_EQ(requestedPresentTimeA, events->requestedPresentTime);
1708 ASSERT_GE(events->postedTime, postedTimeA);
1709
Vishnu Nairde66dc72021-06-17 17:54:41 -07001710 // a valid latchtime and pre and post composition info should not be set for the dropped frame
Vishnu Nair083efd32021-02-12 09:32:30 -08001711 ASSERT_FALSE(events->hasLatchInfo());
1712 ASSERT_FALSE(events->hasDequeueReadyInfo());
Vishnu Nairde66dc72021-06-17 17:54:41 -07001713 ASSERT_FALSE(events->hasGpuCompositionDoneInfo());
1714 ASSERT_FALSE(events->hasDisplayPresentInfo());
1715 ASSERT_FALSE(events->hasReleaseInfo());
Vishnu Nair083efd32021-02-12 09:32:30 -08001716
Vishnu Nairde66dc72021-06-17 17:54:41 -07001717 // wait for the last transaction to be completed.
1718 adapter.waitForCallback(2);
Vishnu Nair083efd32021-02-12 09:32:30 -08001719
Vishnu Nairde66dc72021-06-17 17:54:41 -07001720 // queue another buffer so we query for frame event deltas
1721 nsecs_t requestedPresentTimeC = 0;
1722 nsecs_t postedTimeC = 0;
1723 setUpAndQueueBuffer(igbProducer, &requestedPresentTimeC, &postedTimeC, &qbOutput, true);
1724 history.applyDelta(qbOutput.frameTimestamps);
1725
1726 // frame number, requestedPresentTime, and postTime should not have changed
1727 ASSERT_EQ(1, events->frameNumber);
1728 ASSERT_EQ(requestedPresentTimeA, events->requestedPresentTime);
1729 ASSERT_GE(events->postedTime, postedTimeA);
1730
1731 // a valid latchtime and pre and post composition info should not be set for the dropped frame
1732 ASSERT_FALSE(events->hasLatchInfo());
1733 ASSERT_FALSE(events->hasDequeueReadyInfo());
1734 ASSERT_FALSE(events->hasGpuCompositionDoneInfo());
1735 ASSERT_FALSE(events->hasDisplayPresentInfo());
1736 ASSERT_FALSE(events->hasReleaseInfo());
1737
1738 // we should also have gotten values for the presented frame
Vishnu Nair083efd32021-02-12 09:32:30 -08001739 events = history.getFrame(2);
1740 ASSERT_NE(nullptr, events);
1741 ASSERT_EQ(2, events->frameNumber);
1742 ASSERT_EQ(requestedPresentTimeB, events->requestedPresentTime);
1743 ASSERT_GE(events->postedTime, postedTimeB);
Vishnu Nairde66dc72021-06-17 17:54:41 -07001744 ASSERT_GE(events->latchTime, postedTimeB);
1745 ASSERT_GE(events->dequeueReadyTime, events->latchTime);
1746 ASSERT_NE(nullptr, events->gpuCompositionDoneFence);
1747 ASSERT_NE(nullptr, events->displayPresentFence);
1748 ASSERT_NE(nullptr, events->releaseFence);
1749
1750 // wait for any callbacks that have not been received
1751 adapter.waitForCallbacks();
Vishnu Nair083efd32021-02-12 09:32:30 -08001752}
1753
Vishnu Nair9a69a042021-06-18 13:19:49 -07001754TEST_F(BLASTFrameEventHistoryTest, FrameEventHistory_CompositorTimings) {
1755 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
1756 sp<IGraphicBufferProducer> igbProducer;
1757 ProducerFrameEventHistory history;
1758 setUpProducer(adapter, igbProducer);
1759
1760 IGraphicBufferProducer::QueueBufferOutput qbOutput;
1761 nsecs_t requestedPresentTimeA = 0;
1762 nsecs_t postedTimeA = 0;
Vishnu Nair9a69a042021-06-18 13:19:49 -07001763 setUpAndQueueBuffer(igbProducer, &requestedPresentTimeA, &postedTimeA, &qbOutput, true);
1764 history.applyDelta(qbOutput.frameTimestamps);
1765 adapter.waitForCallback(1);
1766
1767 // queue another buffer so we query for frame event deltas
1768 nsecs_t requestedPresentTimeB = 0;
1769 nsecs_t postedTimeB = 0;
1770 setUpAndQueueBuffer(igbProducer, &requestedPresentTimeB, &postedTimeB, &qbOutput, true);
1771 history.applyDelta(qbOutput.frameTimestamps);
1772
1773 // check for a valid compositor deadline
1774 ASSERT_NE(0, history.getReportedCompositeDeadline());
1775
1776 // wait for any callbacks that have not been received
1777 adapter.waitForCallbacks();
1778}
1779
Valerie Hauc5011f92019-10-11 09:52:07 -07001780} // namespace android