blob: b9e75be57d4ee65dcf4ce320f33dd19e293184c1 [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
120 void syncNextTransaction(std::function<void(Transaction*)> callback,
121 bool acquireSingleBuffer = true) {
122 mBlastBufferQueueAdapter->syncNextTransaction(callback, acquireSingleBuffer);
123 }
124
125 void stopContinuousSyncTransaction() {
126 mBlastBufferQueueAdapter->stopContinuousSyncTransaction();
Valerie Hauc5011f92019-10-11 09:52:07 -0700127 }
128
Vishnu Nairea0de002020-11-17 17:42:37 -0800129 int getWidth() { return mBlastBufferQueueAdapter->mSize.width; }
Valerie Hauda3446e2019-10-14 15:49:22 -0700130
Vishnu Nairea0de002020-11-17 17:42:37 -0800131 int getHeight() { return mBlastBufferQueueAdapter->mSize.height; }
Valerie Hauda3446e2019-10-14 15:49:22 -0700132
Tianhao Yao4861b102022-02-03 20:18:35 +0000133 std::function<void(Transaction*)> getTransactionReadyCallback() {
134 return mBlastBufferQueueAdapter->mTransactionReadyCallback;
135 }
Valerie Hauda3446e2019-10-14 15:49:22 -0700136
137 sp<IGraphicBufferProducer> getIGraphicBufferProducer() {
138 return mBlastBufferQueueAdapter->getIGraphicBufferProducer();
139 }
140
Valerie Hauc5011f92019-10-11 09:52:07 -0700141 const sp<SurfaceControl> getSurfaceControl() {
142 return mBlastBufferQueueAdapter->mSurfaceControl;
143 }
144
Vishnu Naira4fbca52021-07-07 16:52:34 -0700145 sp<Surface> getSurface() {
146 return mBlastBufferQueueAdapter->getSurface(false /* includeSurfaceControlHandle */);
147 }
148
Valerie Haud3b90d22019-11-06 09:37:31 -0800149 void waitForCallbacks() {
Valerie Hauda3446e2019-10-14 15:49:22 -0700150 std::unique_lock lock{mBlastBufferQueueAdapter->mMutex};
Vishnu Nair1506b182021-02-22 14:35:15 -0800151 // Wait until all but one of the submitted buffers have been released.
152 while (mBlastBufferQueueAdapter->mSubmitted.size() > 1) {
Valerie Haud3b90d22019-11-06 09:37:31 -0800153 mBlastBufferQueueAdapter->mCallbackCV.wait(lock);
154 }
Valerie Hauda3446e2019-10-14 15:49:22 -0700155 }
156
Vishnu Nair1506b182021-02-22 14:35:15 -0800157 void waitForCallback(int64_t frameNumber) {
chaviwf10b9042021-10-13 15:48:59 -0500158 mBlastBufferQueueAdapter->waitForCallback(frameNumber);
Vishnu Nair1506b182021-02-22 14:35:15 -0800159 }
160
chaviw0acd33a2021-11-02 11:55:37 -0500161 void validateNumFramesSubmitted(int64_t numFramesSubmitted) {
162 std::unique_lock lock{mBlastBufferQueueAdapter->mMutex};
163 ASSERT_EQ(numFramesSubmitted, mBlastBufferQueueAdapter->mSubmitted.size());
164 }
165
chaviwc1cf4022022-06-03 13:32:33 -0500166 void mergeWithNextTransaction(Transaction* merge, uint64_t frameNumber) {
167 mBlastBufferQueueAdapter->mergeWithNextTransaction(merge, frameNumber);
168 }
169
Valerie Hauc5011f92019-10-11 09:52:07 -0700170private:
chaviwf10b9042021-10-13 15:48:59 -0500171 sp<TestBLASTBufferQueue> mBlastBufferQueueAdapter;
Valerie Hauc5011f92019-10-11 09:52:07 -0700172};
173
174class BLASTBufferQueueTest : public ::testing::Test {
175public:
176protected:
177 BLASTBufferQueueTest() {
178 const ::testing::TestInfo* const testInfo =
179 ::testing::UnitTest::GetInstance()->current_test_info();
180 ALOGV("Begin test: %s.%s", testInfo->test_case_name(), testInfo->name());
181 }
182
183 ~BLASTBufferQueueTest() {
184 const ::testing::TestInfo* const testInfo =
185 ::testing::UnitTest::GetInstance()->current_test_info();
186 ALOGV("End test: %s.%s", testInfo->test_case_name(), testInfo->name());
187 }
188
189 void SetUp() {
Valerie Hauda3446e2019-10-14 15:49:22 -0700190 mComposer = ComposerService::getComposerService();
Valerie Hauc5011f92019-10-11 09:52:07 -0700191 mClient = new SurfaceComposerClient();
Huihong Luo31b5ac22022-08-15 20:38:10 -0700192 const auto ids = SurfaceComposerClient::getPhysicalDisplayIds();
193 ASSERT_FALSE(ids.empty());
194 // display 0 is picked as this test is not much display depedent
195 mDisplayToken = SurfaceComposerClient::getPhysicalDisplayToken(ids.front());
Valerie Hauda3446e2019-10-14 15:49:22 -0700196 ASSERT_NE(nullptr, mDisplayToken.get());
197 Transaction t;
Dominik Laskowski29fa1462021-04-27 15:51:50 -0700198 t.setDisplayLayerStack(mDisplayToken, ui::DEFAULT_LAYER_STACK);
Valerie Hauda3446e2019-10-14 15:49:22 -0700199 t.apply();
200 t.clear();
201
Vishnu Nair5b5f6932023-04-12 16:28:19 -0700202 ui::DisplayState displayState;
203 ASSERT_EQ(NO_ERROR, SurfaceComposerClient::getDisplayState(mDisplayToken, &displayState));
204 const ui::Size& resolution = displayState.layerStackSpaceRect;
Dominik Laskowski3cb3d4e2019-11-21 11:14:45 -0800205 mDisplayWidth = resolution.getWidth();
206 mDisplayHeight = resolution.getHeight();
Vishnu Nair5b5f6932023-04-12 16:28:19 -0700207 ALOGV("Display: %dx%d orientation:%d", mDisplayWidth, mDisplayHeight,
208 displayState.orientation);
Valerie Hauda3446e2019-10-14 15:49:22 -0700209
210 mSurfaceControl = mClient->createSurface(String8("TestSurface"), mDisplayWidth,
211 mDisplayHeight, PIXEL_FORMAT_RGBA_8888,
212 ISurfaceComposerClient::eFXSurfaceBufferState,
213 /*parent*/ nullptr);
Dominik Laskowski29fa1462021-04-27 15:51:50 -0700214 t.setLayerStack(mSurfaceControl, ui::DEFAULT_LAYER_STACK)
Valerie Hauda3446e2019-10-14 15:49:22 -0700215 .setLayer(mSurfaceControl, std::numeric_limits<int32_t>::max())
Valerie Hauda3446e2019-10-14 15:49:22 -0700216 .show(mSurfaceControl)
217 .setDataspace(mSurfaceControl, ui::Dataspace::V0_SRGB)
218 .apply();
chaviwd2432892020-07-24 17:42:39 -0700219
220 mCaptureArgs.displayToken = mDisplayToken;
arthurhung6fa58b72020-11-05 11:56:00 +0800221 mCaptureArgs.dataspace = ui::Dataspace::V0_SRGB;
Valerie Hauda3446e2019-10-14 15:49:22 -0700222 }
223
chaviwd7deef72021-10-06 11:53:40 -0500224 void setUpProducer(BLASTBufferQueueHelper& adapter, sp<IGraphicBufferProducer>& producer,
225 int32_t maxBufferCount = 2) {
Vishnu Nair083efd32021-02-12 09:32:30 -0800226 producer = adapter.getIGraphicBufferProducer();
chaviwd7deef72021-10-06 11:53:40 -0500227 setUpProducer(producer, maxBufferCount);
Vishnu Nair083efd32021-02-12 09:32:30 -0800228 }
229
chaviwd7deef72021-10-06 11:53:40 -0500230 void setUpProducer(sp<IGraphicBufferProducer>& igbProducer, int32_t maxBufferCount) {
Valerie Haud3b90d22019-11-06 09:37:31 -0800231 ASSERT_NE(nullptr, igbProducer.get());
chaviwd7deef72021-10-06 11:53:40 -0500232 ASSERT_EQ(NO_ERROR, igbProducer->setMaxDequeuedBufferCount(maxBufferCount));
Valerie Haud3b90d22019-11-06 09:37:31 -0800233 IGraphicBufferProducer::QueueBufferOutput qbOutput;
chaviwd7deef72021-10-06 11:53:40 -0500234 mProducerListener = new CountProducerListener();
Valerie Haud3b90d22019-11-06 09:37:31 -0800235 ASSERT_EQ(NO_ERROR,
chaviwd7deef72021-10-06 11:53:40 -0500236 igbProducer->connect(mProducerListener, NATIVE_WINDOW_API_CPU, false, &qbOutput));
Dominik Laskowski718f9602019-11-09 20:01:35 -0800237 ASSERT_NE(ui::Transform::ROT_INVALID, qbOutput.transformHint);
Valerie Haud3b90d22019-11-06 09:37:31 -0800238 }
239
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800240 void fillBuffer(uint32_t* bufData, Rect rect, uint32_t stride, uint8_t r, uint8_t g,
241 uint8_t b) {
242 for (uint32_t row = rect.top; row < rect.bottom; row++) {
243 for (uint32_t col = rect.left; col < rect.right; col++) {
Valerie Hauda3446e2019-10-14 15:49:22 -0700244 uint8_t* pixel = (uint8_t*)(bufData + (row * stride) + col);
245 *pixel = r;
246 *(pixel + 1) = g;
247 *(pixel + 2) = b;
248 *(pixel + 3) = 255;
249 }
250 }
251 }
252
Valerie Hau5977fc82019-12-05 15:56:39 -0800253 void fillQuadrants(sp<GraphicBuffer>& buf) {
254 const auto bufWidth = buf->getWidth();
255 const auto bufHeight = buf->getHeight();
256 uint32_t* bufData;
257 buf->lock(static_cast<uint32_t>(GraphicBuffer::USAGE_SW_WRITE_OFTEN),
258 reinterpret_cast<void**>(&bufData));
259 fillBuffer(bufData, Rect(0, 0, bufWidth / 2, bufHeight / 2), buf->getStride(), 0, 0, 0);
260 fillBuffer(bufData, Rect(bufWidth / 2, 0, bufWidth, bufHeight / 2), buf->getStride(), 255,
261 0, 0);
262 fillBuffer(bufData, Rect(bufWidth / 2, bufHeight / 2, bufWidth, bufHeight),
263 buf->getStride(), 0, 255, 0);
264 fillBuffer(bufData, Rect(0, bufHeight / 2, bufWidth / 2, bufHeight), buf->getStride(), 0, 0,
265 255);
266 buf->unlock();
267 }
268
269 void checkScreenCapture(uint8_t r, uint8_t g, uint8_t b, Rect region, int32_t border = 0,
270 bool outsideRegion = false) {
chaviwd2432892020-07-24 17:42:39 -0700271 sp<GraphicBuffer>& captureBuf = mCaptureResults.buffer;
Valerie Hau5977fc82019-12-05 15:56:39 -0800272 const auto epsilon = 3;
chaviwd2432892020-07-24 17:42:39 -0700273 const auto width = captureBuf->getWidth();
274 const auto height = captureBuf->getHeight();
275 const auto stride = captureBuf->getStride();
Valerie Hauda3446e2019-10-14 15:49:22 -0700276
277 uint32_t* bufData;
chaviwd2432892020-07-24 17:42:39 -0700278 captureBuf->lock(static_cast<uint32_t>(GraphicBuffer::USAGE_SW_READ_OFTEN),
279 reinterpret_cast<void**>(&bufData));
Valerie Hauda3446e2019-10-14 15:49:22 -0700280
281 for (uint32_t row = 0; row < height; row++) {
282 for (uint32_t col = 0; col < width; col++) {
283 uint8_t* pixel = (uint8_t*)(bufData + (row * stride) + col);
arthurhung6fa58b72020-11-05 11:56:00 +0800284 ASSERT_NE(nullptr, pixel);
Valerie Hau5977fc82019-12-05 15:56:39 -0800285 bool inRegion;
286 if (!outsideRegion) {
287 inRegion = row >= region.top + border && row < region.bottom - border &&
288 col >= region.left + border && col < region.right - border;
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800289 } else {
Valerie Hau5977fc82019-12-05 15:56:39 -0800290 inRegion = row >= region.top - border && row < region.bottom + border &&
291 col >= region.left - border && col < region.right + border;
292 }
293 if (!outsideRegion && inRegion) {
Chavi Weingartena5aedbd2021-04-09 13:37:33 +0000294 ASSERT_GE(epsilon, abs(r - *(pixel)));
295 ASSERT_GE(epsilon, abs(g - *(pixel + 1)));
296 ASSERT_GE(epsilon, abs(b - *(pixel + 2)));
Valerie Hau5977fc82019-12-05 15:56:39 -0800297 } else if (outsideRegion && !inRegion) {
Chavi Weingartena5aedbd2021-04-09 13:37:33 +0000298 ASSERT_GE(epsilon, abs(r - *(pixel)));
299 ASSERT_GE(epsilon, abs(g - *(pixel + 1)));
300 ASSERT_GE(epsilon, abs(b - *(pixel + 2)));
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800301 }
Vishnu Nair1506b182021-02-22 14:35:15 -0800302 ASSERT_EQ(false, ::testing::Test::HasFailure());
Valerie Hauda3446e2019-10-14 15:49:22 -0700303 }
304 }
chaviwd2432892020-07-24 17:42:39 -0700305 captureBuf->unlock();
Valerie Hauc5011f92019-10-11 09:52:07 -0700306 }
307
chaviw8ffc7b82020-08-18 11:25:37 -0700308 static status_t captureDisplay(DisplayCaptureArgs& captureArgs,
309 ScreenCaptureResults& captureResults) {
Huihong Luo9e84f332021-12-16 14:33:46 -0800310 const auto sf = ComposerServiceAIDL::getComposerService();
chaviw8ffc7b82020-08-18 11:25:37 -0700311 SurfaceComposerClient::Transaction().apply(true);
312
313 const sp<SyncScreenCaptureListener> captureListener = new SyncScreenCaptureListener();
Huihong Luo9e84f332021-12-16 14:33:46 -0800314 binder::Status status = sf->captureDisplay(captureArgs, captureListener);
Huihong Luo3bdef862022-03-03 11:57:19 -0800315 status_t err = gui::aidl_utils::statusTFromBinderStatus(status);
316 if (err != NO_ERROR) {
317 return err;
chaviw8ffc7b82020-08-18 11:25:37 -0700318 }
319 captureResults = captureListener->waitForResults();
Patrick Williamsfdb57bb2022-08-09 22:48:18 +0000320 return fenceStatus(captureResults.fenceResult);
chaviw8ffc7b82020-08-18 11:25:37 -0700321 }
322
Vishnu Nair277142c2021-01-05 18:35:29 -0800323 void queueBuffer(sp<IGraphicBufferProducer> igbp, uint8_t r, uint8_t g, uint8_t b,
324 nsecs_t presentTimeDelay) {
325 int slot;
326 sp<Fence> fence;
327 sp<GraphicBuffer> buf;
328 auto ret = igbp->dequeueBuffer(&slot, &fence, mDisplayWidth, mDisplayHeight,
329 PIXEL_FORMAT_RGBA_8888, GRALLOC_USAGE_SW_WRITE_OFTEN,
330 nullptr, nullptr);
chaviw0acd33a2021-11-02 11:55:37 -0500331 ASSERT_TRUE(ret == IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION || ret == NO_ERROR);
Vishnu Nair277142c2021-01-05 18:35:29 -0800332 ASSERT_EQ(OK, igbp->requestBuffer(slot, &buf));
333
334 uint32_t* bufData;
335 buf->lock(static_cast<uint32_t>(GraphicBuffer::USAGE_SW_WRITE_OFTEN),
336 reinterpret_cast<void**>(&bufData));
337 fillBuffer(bufData, Rect(buf->getWidth(), buf->getHeight() / 2), buf->getStride(), r, g, b);
338 buf->unlock();
339
340 IGraphicBufferProducer::QueueBufferOutput qbOutput;
341 nsecs_t timestampNanos = systemTime() + presentTimeDelay;
342 IGraphicBufferProducer::QueueBufferInput input(timestampNanos, false, HAL_DATASPACE_UNKNOWN,
343 Rect(mDisplayWidth, mDisplayHeight / 2),
344 NATIVE_WINDOW_SCALING_MODE_FREEZE, 0,
345 Fence::NO_FENCE);
346 igbp->queueBuffer(slot, input, &qbOutput);
347 }
348
Valerie Hauc5011f92019-10-11 09:52:07 -0700349 sp<SurfaceComposerClient> mClient;
Valerie Hauda3446e2019-10-14 15:49:22 -0700350 sp<ISurfaceComposer> mComposer;
351
352 sp<IBinder> mDisplayToken;
353
Valerie Hauc5011f92019-10-11 09:52:07 -0700354 sp<SurfaceControl> mSurfaceControl;
Valerie Hauda3446e2019-10-14 15:49:22 -0700355
356 uint32_t mDisplayWidth;
357 uint32_t mDisplayHeight;
chaviwd2432892020-07-24 17:42:39 -0700358
359 DisplayCaptureArgs mCaptureArgs;
360 ScreenCaptureResults mCaptureResults;
chaviwd7deef72021-10-06 11:53:40 -0500361 sp<CountProducerListener> mProducerListener;
Valerie Hauc5011f92019-10-11 09:52:07 -0700362};
363
364TEST_F(BLASTBufferQueueTest, CreateBLASTBufferQueue) {
365 // create BLASTBufferQueue adapter associated with this surface
Valerie Hauda3446e2019-10-14 15:49:22 -0700366 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
Valerie Hauc5011f92019-10-11 09:52:07 -0700367 ASSERT_EQ(mSurfaceControl, adapter.getSurfaceControl());
Valerie Hauda3446e2019-10-14 15:49:22 -0700368 ASSERT_EQ(mDisplayWidth, adapter.getWidth());
369 ASSERT_EQ(mDisplayHeight, adapter.getHeight());
Tianhao Yao4861b102022-02-03 20:18:35 +0000370 ASSERT_EQ(nullptr, adapter.getTransactionReadyCallback());
Valerie Hauc5011f92019-10-11 09:52:07 -0700371}
372
373TEST_F(BLASTBufferQueueTest, Update) {
Valerie Hauda3446e2019-10-14 15:49:22 -0700374 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
Valerie Hauc5011f92019-10-11 09:52:07 -0700375 sp<SurfaceControl> updateSurface =
Valerie Hauda3446e2019-10-14 15:49:22 -0700376 mClient->createSurface(String8("UpdateTest"), mDisplayWidth / 2, mDisplayHeight / 2,
377 PIXEL_FORMAT_RGBA_8888);
378 adapter.update(updateSurface, mDisplayWidth / 2, mDisplayHeight / 2);
Valerie Hauc5011f92019-10-11 09:52:07 -0700379 ASSERT_EQ(updateSurface, adapter.getSurfaceControl());
Vishnu Nairea0de002020-11-17 17:42:37 -0800380 sp<IGraphicBufferProducer> igbProducer;
381 setUpProducer(adapter, igbProducer);
382
383 int32_t width;
384 igbProducer->query(NATIVE_WINDOW_WIDTH, &width);
385 ASSERT_EQ(mDisplayWidth / 2, width);
386 int32_t height;
387 igbProducer->query(NATIVE_WINDOW_HEIGHT, &height);
388 ASSERT_EQ(mDisplayHeight / 2, height);
Valerie Hauc5011f92019-10-11 09:52:07 -0700389}
390
Tianhao Yao4861b102022-02-03 20:18:35 +0000391TEST_F(BLASTBufferQueueTest, SyncNextTransaction) {
Valerie Hauda3446e2019-10-14 15:49:22 -0700392 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
Tianhao Yao4861b102022-02-03 20:18:35 +0000393 ASSERT_EQ(nullptr, adapter.getTransactionReadyCallback());
394 auto callback = [](Transaction*) {};
395 adapter.syncNextTransaction(callback);
396 ASSERT_NE(nullptr, adapter.getTransactionReadyCallback());
Valerie Hauc5011f92019-10-11 09:52:07 -0700397}
Valerie Hauda3446e2019-10-14 15:49:22 -0700398
Valerie Haubf29e042020-02-06 11:40:38 -0800399TEST_F(BLASTBufferQueueTest, DISABLED_onFrameAvailable_ApplyDesiredPresentTime) {
Valerie Hau181abd32020-01-27 14:18:28 -0800400 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
401 sp<IGraphicBufferProducer> igbProducer;
402 setUpProducer(adapter, igbProducer);
403
404 int slot;
405 sp<Fence> fence;
406 sp<GraphicBuffer> buf;
407 auto ret = igbProducer->dequeueBuffer(&slot, &fence, mDisplayWidth, mDisplayHeight,
408 PIXEL_FORMAT_RGBA_8888, GRALLOC_USAGE_SW_WRITE_OFTEN,
409 nullptr, nullptr);
410 ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, ret);
411 ASSERT_EQ(OK, igbProducer->requestBuffer(slot, &buf));
412
413 nsecs_t desiredPresentTime = systemTime() + nsecs_t(5 * 1e8);
414 IGraphicBufferProducer::QueueBufferOutput qbOutput;
Vishnu Nair1506b182021-02-22 14:35:15 -0800415 IGraphicBufferProducer::QueueBufferInput input(desiredPresentTime, true /* autotimestamp */,
416 HAL_DATASPACE_UNKNOWN,
Valerie Hau181abd32020-01-27 14:18:28 -0800417 Rect(mDisplayWidth, mDisplayHeight),
418 NATIVE_WINDOW_SCALING_MODE_FREEZE, 0,
419 Fence::NO_FENCE);
420 igbProducer->queueBuffer(slot, input, &qbOutput);
421 ASSERT_NE(ui::Transform::ROT_INVALID, qbOutput.transformHint);
422
423 adapter.waitForCallbacks();
424 ASSERT_GE(systemTime(), desiredPresentTime);
425}
426
Valerie Hauda3446e2019-10-14 15:49:22 -0700427TEST_F(BLASTBufferQueueTest, onFrameAvailable_Apply) {
428 uint8_t r = 255;
429 uint8_t g = 0;
430 uint8_t b = 0;
431
432 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
Valerie Haud3b90d22019-11-06 09:37:31 -0800433 sp<IGraphicBufferProducer> igbProducer;
434 setUpProducer(adapter, igbProducer);
Valerie Hauda3446e2019-10-14 15:49:22 -0700435
436 int slot;
437 sp<Fence> fence;
438 sp<GraphicBuffer> buf;
439 auto ret = igbProducer->dequeueBuffer(&slot, &fence, mDisplayWidth, mDisplayHeight,
440 PIXEL_FORMAT_RGBA_8888, GRALLOC_USAGE_SW_WRITE_OFTEN,
441 nullptr, nullptr);
442 ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, ret);
443 ASSERT_EQ(OK, igbProducer->requestBuffer(slot, &buf));
444
445 uint32_t* bufData;
446 buf->lock(static_cast<uint32_t>(GraphicBuffer::USAGE_SW_WRITE_OFTEN),
447 reinterpret_cast<void**>(&bufData));
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800448 fillBuffer(bufData, Rect(buf->getWidth(), buf->getHeight()), buf->getStride(), r, g, b);
Valerie Hauda3446e2019-10-14 15:49:22 -0700449 buf->unlock();
450
Valerie Haud3b90d22019-11-06 09:37:31 -0800451 IGraphicBufferProducer::QueueBufferOutput qbOutput;
Vishnu Nair1506b182021-02-22 14:35:15 -0800452 IGraphicBufferProducer::QueueBufferInput input(systemTime(), true /* autotimestamp */,
453 HAL_DATASPACE_UNKNOWN,
Valerie Hauda3446e2019-10-14 15:49:22 -0700454 Rect(mDisplayWidth, mDisplayHeight),
455 NATIVE_WINDOW_SCALING_MODE_FREEZE, 0,
456 Fence::NO_FENCE);
457 igbProducer->queueBuffer(slot, input, &qbOutput);
Dominik Laskowski718f9602019-11-09 20:01:35 -0800458 ASSERT_NE(ui::Transform::ROT_INVALID, qbOutput.transformHint);
Valerie Hauda3446e2019-10-14 15:49:22 -0700459
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800460 adapter.waitForCallbacks();
Valerie Hauda3446e2019-10-14 15:49:22 -0700461
462 // capture screen and verify that it is red
chaviw8ffc7b82020-08-18 11:25:37 -0700463 ASSERT_EQ(NO_ERROR, captureDisplay(mCaptureArgs, mCaptureResults));
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800464 ASSERT_NO_FATAL_FAILURE(
465 checkScreenCapture(r, g, b, {0, 0, (int32_t)mDisplayWidth, (int32_t)mDisplayHeight}));
Valerie Hauda3446e2019-10-14 15:49:22 -0700466}
Valerie Haud3b90d22019-11-06 09:37:31 -0800467
468TEST_F(BLASTBufferQueueTest, TripleBuffering) {
469 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
470 sp<IGraphicBufferProducer> igbProducer;
471 setUpProducer(adapter, igbProducer);
472
473 std::vector<std::pair<int, sp<Fence>>> allocated;
Ady Abraham0bde6b52021-05-18 13:57:02 -0700474 int minUndequeuedBuffers = 0;
475 ASSERT_EQ(OK, igbProducer->query(NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS, &minUndequeuedBuffers));
476 const auto bufferCount = minUndequeuedBuffers + 2;
477
478 for (int i = 0; i < bufferCount; i++) {
Valerie Haud3b90d22019-11-06 09:37:31 -0800479 int slot;
480 sp<Fence> fence;
481 sp<GraphicBuffer> buf;
482 auto ret = igbProducer->dequeueBuffer(&slot, &fence, mDisplayWidth, mDisplayHeight,
483 PIXEL_FORMAT_RGBA_8888, GRALLOC_USAGE_SW_WRITE_OFTEN,
484 nullptr, nullptr);
485 ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, ret);
486 ASSERT_EQ(OK, igbProducer->requestBuffer(slot, &buf));
487 allocated.push_back({slot, fence});
488 }
489 for (int i = 0; i < allocated.size(); i++) {
490 igbProducer->cancelBuffer(allocated[i].first, allocated[i].second);
491 }
492
Valerie Haua32c5522019-12-09 10:11:08 -0800493 for (int i = 0; i < 100; i++) {
Valerie Haud3b90d22019-11-06 09:37:31 -0800494 int slot;
495 sp<Fence> fence;
496 sp<GraphicBuffer> buf;
497 auto ret = igbProducer->dequeueBuffer(&slot, &fence, mDisplayWidth, mDisplayHeight,
498 PIXEL_FORMAT_RGBA_8888, GRALLOC_USAGE_SW_WRITE_OFTEN,
499 nullptr, nullptr);
500 ASSERT_EQ(NO_ERROR, ret);
501 IGraphicBufferProducer::QueueBufferOutput qbOutput;
Vishnu Nair1506b182021-02-22 14:35:15 -0800502 IGraphicBufferProducer::QueueBufferInput input(systemTime(), true /* autotimestamp */,
503 HAL_DATASPACE_UNKNOWN,
Valerie Haud3b90d22019-11-06 09:37:31 -0800504 Rect(mDisplayWidth, mDisplayHeight),
505 NATIVE_WINDOW_SCALING_MODE_FREEZE, 0,
506 Fence::NO_FENCE);
507 igbProducer->queueBuffer(slot, input, &qbOutput);
508 }
509 adapter.waitForCallbacks();
510}
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800511
512TEST_F(BLASTBufferQueueTest, SetCrop_Item) {
513 uint8_t r = 255;
514 uint8_t g = 0;
515 uint8_t b = 0;
516
517 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
518 sp<IGraphicBufferProducer> igbProducer;
519 setUpProducer(adapter, igbProducer);
520 int slot;
521 sp<Fence> fence;
522 sp<GraphicBuffer> buf;
523 auto ret = igbProducer->dequeueBuffer(&slot, &fence, mDisplayWidth, mDisplayHeight,
524 PIXEL_FORMAT_RGBA_8888, GRALLOC_USAGE_SW_WRITE_OFTEN,
525 nullptr, nullptr);
526 ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, ret);
527 ASSERT_EQ(OK, igbProducer->requestBuffer(slot, &buf));
528
529 uint32_t* bufData;
530 buf->lock(static_cast<uint32_t>(GraphicBuffer::USAGE_SW_WRITE_OFTEN),
531 reinterpret_cast<void**>(&bufData));
532 fillBuffer(bufData, Rect(buf->getWidth(), buf->getHeight() / 2), buf->getStride(), r, g, b);
533 buf->unlock();
534
535 IGraphicBufferProducer::QueueBufferOutput qbOutput;
Vishnu Nair1506b182021-02-22 14:35:15 -0800536 IGraphicBufferProducer::QueueBufferInput input(systemTime(), true /* autotimestamp */,
537 HAL_DATASPACE_UNKNOWN,
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800538 Rect(mDisplayWidth, mDisplayHeight / 2),
539 NATIVE_WINDOW_SCALING_MODE_FREEZE, 0,
540 Fence::NO_FENCE);
541 igbProducer->queueBuffer(slot, input, &qbOutput);
Dominik Laskowski718f9602019-11-09 20:01:35 -0800542 ASSERT_NE(ui::Transform::ROT_INVALID, qbOutput.transformHint);
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800543
544 adapter.waitForCallbacks();
545 // capture screen and verify that it is red
chaviw8ffc7b82020-08-18 11:25:37 -0700546 ASSERT_EQ(NO_ERROR, captureDisplay(mCaptureArgs, mCaptureResults));
chaviwd2432892020-07-24 17:42:39 -0700547
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800548 ASSERT_NO_FATAL_FAILURE(
Chavi Weingartena5aedbd2021-04-09 13:37:33 +0000549 checkScreenCapture(r, g, b,
550 {0, 0, (int32_t)mDisplayWidth, (int32_t)mDisplayHeight / 2}));
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800551}
552
553TEST_F(BLASTBufferQueueTest, SetCrop_ScalingModeScaleCrop) {
554 uint8_t r = 255;
555 uint8_t g = 0;
556 uint8_t b = 0;
557
558 int32_t bufferSideLength =
559 (mDisplayWidth < mDisplayHeight) ? mDisplayWidth / 2 : mDisplayHeight / 2;
560 int32_t finalCropSideLength = bufferSideLength / 2;
561
562 auto bg = mClient->createSurface(String8("BGTest"), 0, 0, PIXEL_FORMAT_RGBA_8888,
Vishnu Nairfa247b12020-02-11 08:58:26 -0800563 ISurfaceComposerClient::eFXSurfaceEffect);
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800564 ASSERT_NE(nullptr, bg.get());
565 Transaction t;
Dominik Laskowski29fa1462021-04-27 15:51:50 -0700566 t.setLayerStack(bg, ui::DEFAULT_LAYER_STACK)
chaviw25714502021-02-11 10:01:08 -0800567 .setCrop(bg, Rect(0, 0, mDisplayWidth, mDisplayHeight))
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800568 .setColor(bg, half3{0, 0, 0})
569 .setLayer(bg, 0)
570 .apply();
571
572 BLASTBufferQueueHelper adapter(mSurfaceControl, bufferSideLength, bufferSideLength);
573 sp<IGraphicBufferProducer> igbProducer;
574 setUpProducer(adapter, igbProducer);
575 int slot;
576 sp<Fence> fence;
577 sp<GraphicBuffer> buf;
578 auto ret = igbProducer->dequeueBuffer(&slot, &fence, bufferSideLength, bufferSideLength,
579 PIXEL_FORMAT_RGBA_8888, GRALLOC_USAGE_SW_WRITE_OFTEN,
580 nullptr, nullptr);
581 ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, ret);
582 ASSERT_EQ(OK, igbProducer->requestBuffer(slot, &buf));
583
584 uint32_t* bufData;
585 buf->lock(static_cast<uint32_t>(GraphicBuffer::USAGE_SW_WRITE_OFTEN),
586 reinterpret_cast<void**>(&bufData));
587 fillBuffer(bufData, Rect(buf->getWidth(), buf->getHeight()), buf->getStride(), 0, 0, 0);
588 fillBuffer(bufData,
589 Rect(finalCropSideLength / 2, 0, buf->getWidth() - finalCropSideLength / 2,
590 buf->getHeight()),
591 buf->getStride(), r, g, b);
592 buf->unlock();
593
594 IGraphicBufferProducer::QueueBufferOutput qbOutput;
Vishnu Nair1506b182021-02-22 14:35:15 -0800595 IGraphicBufferProducer::QueueBufferInput input(systemTime(), true /* autotimestamp */,
596 HAL_DATASPACE_UNKNOWN,
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800597 Rect(bufferSideLength, finalCropSideLength),
598 NATIVE_WINDOW_SCALING_MODE_SCALE_CROP, 0,
599 Fence::NO_FENCE);
600 igbProducer->queueBuffer(slot, input, &qbOutput);
Dominik Laskowski718f9602019-11-09 20:01:35 -0800601 ASSERT_NE(ui::Transform::ROT_INVALID, qbOutput.transformHint);
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800602
603 adapter.waitForCallbacks();
604 // capture screen and verify that it is red
chaviw8ffc7b82020-08-18 11:25:37 -0700605 ASSERT_EQ(NO_ERROR, captureDisplay(mCaptureArgs, mCaptureResults));
Vishnu Nair5cc9ac02021-04-19 13:23:38 -0700606 ASSERT_NO_FATAL_FAILURE(checkScreenCapture(r, g, b,
607 {10, 10, (int32_t)bufferSideLength - 10,
608 (int32_t)bufferSideLength - 10}));
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800609 ASSERT_NO_FATAL_FAILURE(
Vishnu Nair5cc9ac02021-04-19 13:23:38 -0700610 checkScreenCapture(0, 0, 0,
611 {0, 0, (int32_t)bufferSideLength, (int32_t)bufferSideLength},
612 /*border*/ 0, /*outsideRegion*/ true));
613}
614
615TEST_F(BLASTBufferQueueTest, ScaleCroppedBufferToBufferSize) {
616 // add black background
617 auto bg = mClient->createSurface(String8("BGTest"), 0, 0, PIXEL_FORMAT_RGBA_8888,
618 ISurfaceComposerClient::eFXSurfaceEffect);
619 ASSERT_NE(nullptr, bg.get());
620 Transaction t;
Dominik Laskowski29fa1462021-04-27 15:51:50 -0700621 t.setLayerStack(bg, ui::DEFAULT_LAYER_STACK)
Vishnu Nair5cc9ac02021-04-19 13:23:38 -0700622 .setCrop(bg, Rect(0, 0, mDisplayWidth, mDisplayHeight))
623 .setColor(bg, half3{0, 0, 0})
624 .setLayer(bg, 0)
625 .apply();
626
627 Rect windowSize(1000, 1000);
628 Rect bufferSize(windowSize);
629 Rect bufferCrop(200, 200, 700, 700);
630
631 BLASTBufferQueueHelper adapter(mSurfaceControl, windowSize.getWidth(), windowSize.getHeight());
632 sp<IGraphicBufferProducer> igbProducer;
633 setUpProducer(adapter, igbProducer);
634 int slot;
635 sp<Fence> fence;
636 sp<GraphicBuffer> buf;
637 auto ret = igbProducer->dequeueBuffer(&slot, &fence, bufferSize.getWidth(),
638 bufferSize.getHeight(), PIXEL_FORMAT_RGBA_8888,
639 GRALLOC_USAGE_SW_WRITE_OFTEN, nullptr, nullptr);
640 ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, ret);
641 ASSERT_EQ(OK, igbProducer->requestBuffer(slot, &buf));
642
643 uint32_t* bufData;
644 buf->lock(static_cast<uint32_t>(GraphicBuffer::USAGE_SW_WRITE_OFTEN),
645 reinterpret_cast<void**>(&bufData));
646 // fill buffer with grey
647 fillBuffer(bufData, bufferSize, buf->getStride(), 127, 127, 127);
648
649 // fill crop area with different colors so we can verify the cropped region has been scaled
650 // correctly.
651 fillBuffer(bufData, Rect(200, 200, 450, 450), buf->getStride(), /* rgb */ 255, 0, 0);
652 fillBuffer(bufData, Rect(200, 451, 450, 700), buf->getStride(), /* rgb */ 0, 255, 0);
653 fillBuffer(bufData, Rect(451, 200, 700, 450), buf->getStride(), /* rgb */ 0, 0, 255);
654 fillBuffer(bufData, Rect(451, 451, 700, 700), buf->getStride(), /* rgb */ 255, 0, 0);
655 buf->unlock();
656
657 IGraphicBufferProducer::QueueBufferOutput qbOutput;
658 IGraphicBufferProducer::QueueBufferInput input(systemTime(), true /* autotimestamp */,
659 HAL_DATASPACE_UNKNOWN,
660 bufferCrop /* Rect::INVALID_RECT */,
661 NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW, 0,
662 Fence::NO_FENCE);
663 igbProducer->queueBuffer(slot, input, &qbOutput);
664 ASSERT_NE(ui::Transform::ROT_INVALID, qbOutput.transformHint);
665
666 adapter.waitForCallbacks();
667
668 ASSERT_EQ(NO_ERROR, captureDisplay(mCaptureArgs, mCaptureResults));
669
670 // Verify cropped region is scaled correctly.
671 ASSERT_NO_FATAL_FAILURE(checkScreenCapture(255, 0, 0, {10, 10, 490, 490}));
672 ASSERT_NO_FATAL_FAILURE(checkScreenCapture(0, 255, 0, {10, 510, 490, 990}));
673 ASSERT_NO_FATAL_FAILURE(checkScreenCapture(0, 0, 255, {510, 10, 990, 490}));
674 ASSERT_NO_FATAL_FAILURE(checkScreenCapture(255, 0, 0, {510, 510, 990, 990}));
675 // Verify outside region is black.
676 ASSERT_NO_FATAL_FAILURE(checkScreenCapture(0, 0, 0,
677 {0, 0, (int32_t)windowSize.getWidth(),
678 (int32_t)windowSize.getHeight()},
679 /*border*/ 0, /*outsideRegion*/ true));
680}
681
682TEST_F(BLASTBufferQueueTest, ScaleCroppedBufferToWindowSize) {
683 // add black background
684 auto bg = mClient->createSurface(String8("BGTest"), 0, 0, PIXEL_FORMAT_RGBA_8888,
685 ISurfaceComposerClient::eFXSurfaceEffect);
686 ASSERT_NE(nullptr, bg.get());
687 Transaction t;
Dominik Laskowski29fa1462021-04-27 15:51:50 -0700688 t.setLayerStack(bg, ui::DEFAULT_LAYER_STACK)
Vishnu Nair5cc9ac02021-04-19 13:23:38 -0700689 .setCrop(bg, Rect(0, 0, mDisplayWidth, mDisplayHeight))
690 .setColor(bg, half3{0, 0, 0})
691 .setLayer(bg, 0)
692 .apply();
693
694 Rect windowSize(1000, 1000);
695 Rect bufferSize(500, 500);
696 Rect bufferCrop(100, 100, 350, 350);
697
698 BLASTBufferQueueHelper adapter(mSurfaceControl, windowSize.getWidth(), windowSize.getHeight());
699 sp<IGraphicBufferProducer> igbProducer;
700 setUpProducer(adapter, igbProducer);
701 int slot;
702 sp<Fence> fence;
703 sp<GraphicBuffer> buf;
704 auto ret = igbProducer->dequeueBuffer(&slot, &fence, bufferSize.getWidth(),
705 bufferSize.getHeight(), PIXEL_FORMAT_RGBA_8888,
706 GRALLOC_USAGE_SW_WRITE_OFTEN, nullptr, nullptr);
707 ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, ret);
708 ASSERT_EQ(OK, igbProducer->requestBuffer(slot, &buf));
709
710 uint32_t* bufData;
711 buf->lock(static_cast<uint32_t>(GraphicBuffer::USAGE_SW_WRITE_OFTEN),
712 reinterpret_cast<void**>(&bufData));
713 // fill buffer with grey
714 fillBuffer(bufData, bufferSize, buf->getStride(), 127, 127, 127);
715
716 // fill crop area with different colors so we can verify the cropped region has been scaled
717 // correctly.
718 fillBuffer(bufData, Rect(100, 100, 225, 225), buf->getStride(), /* rgb */ 255, 0, 0);
719 fillBuffer(bufData, Rect(100, 226, 225, 350), buf->getStride(), /* rgb */ 0, 255, 0);
720 fillBuffer(bufData, Rect(226, 100, 350, 225), buf->getStride(), /* rgb */ 0, 0, 255);
721 fillBuffer(bufData, Rect(226, 226, 350, 350), buf->getStride(), /* rgb */ 255, 0, 0);
722 buf->unlock();
723
724 IGraphicBufferProducer::QueueBufferOutput qbOutput;
725 IGraphicBufferProducer::QueueBufferInput input(systemTime(), true /* autotimestamp */,
726 HAL_DATASPACE_UNKNOWN,
727 bufferCrop /* Rect::INVALID_RECT */,
728 NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW, 0,
729 Fence::NO_FENCE);
730 igbProducer->queueBuffer(slot, input, &qbOutput);
731 ASSERT_NE(ui::Transform::ROT_INVALID, qbOutput.transformHint);
732
733 adapter.waitForCallbacks();
734
735 ASSERT_EQ(NO_ERROR, captureDisplay(mCaptureArgs, mCaptureResults));
736 // Verify cropped region is scaled correctly.
737 ASSERT_NO_FATAL_FAILURE(checkScreenCapture(255, 0, 0, {10, 10, 490, 490}));
738 ASSERT_NO_FATAL_FAILURE(checkScreenCapture(0, 255, 0, {10, 510, 490, 990}));
739 ASSERT_NO_FATAL_FAILURE(checkScreenCapture(0, 0, 255, {510, 10, 990, 490}));
740 ASSERT_NO_FATAL_FAILURE(checkScreenCapture(255, 0, 0, {510, 510, 990, 990}));
741 // Verify outside region is black.
742 ASSERT_NO_FATAL_FAILURE(checkScreenCapture(0, 0, 0,
743 {0, 0, (int32_t)windowSize.getWidth(),
744 (int32_t)windowSize.getHeight()},
745 /*border*/ 0, /*outsideRegion*/ true));
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800746}
747
Vishnu Nair932f6ae2021-09-29 17:33:10 -0700748// b/196339769 verify we can can update the requested size while the in FREEZE scaling mode and
749// scale the buffer properly when the mode changes to SCALE_TO_WINDOW
750TEST_F(BLASTBufferQueueTest, ScalingModeChanges) {
751 uint8_t r = 255;
752 uint8_t g = 0;
753 uint8_t b = 0;
754
755 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight / 4);
756 sp<IGraphicBufferProducer> igbProducer;
757 setUpProducer(adapter, igbProducer);
758 {
759 int slot;
760 sp<Fence> fence;
761 sp<GraphicBuffer> buf;
762 auto ret = igbProducer->dequeueBuffer(&slot, &fence, mDisplayWidth, mDisplayHeight / 4,
763 PIXEL_FORMAT_RGBA_8888, GRALLOC_USAGE_SW_WRITE_OFTEN,
764 nullptr, nullptr);
765 ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, ret);
766 ASSERT_EQ(OK, igbProducer->requestBuffer(slot, &buf));
767
768 uint32_t* bufData;
769 buf->lock(static_cast<uint32_t>(GraphicBuffer::USAGE_SW_WRITE_OFTEN),
770 reinterpret_cast<void**>(&bufData));
771 fillBuffer(bufData, Rect(buf->getWidth(), buf->getHeight()), buf->getStride(), r, g, b);
772 buf->unlock();
773
774 IGraphicBufferProducer::QueueBufferOutput qbOutput;
775 IGraphicBufferProducer::QueueBufferInput input(systemTime(), true /* autotimestamp */,
776 HAL_DATASPACE_UNKNOWN, {},
777 NATIVE_WINDOW_SCALING_MODE_FREEZE, 0,
778 Fence::NO_FENCE);
779 igbProducer->queueBuffer(slot, input, &qbOutput);
780 adapter.waitForCallbacks();
781 }
782 // capture screen and verify that it is red
783 ASSERT_EQ(NO_ERROR, captureDisplay(mCaptureArgs, mCaptureResults));
784
785 ASSERT_NO_FATAL_FAILURE(
786 checkScreenCapture(r, g, b,
787 {0, 0, (int32_t)mDisplayWidth, (int32_t)mDisplayHeight / 4}));
788
789 // update the size to half the display and dequeue a buffer quarter of the display.
790 adapter.update(mSurfaceControl, mDisplayWidth, mDisplayHeight / 2);
791
792 {
793 int slot;
794 sp<Fence> fence;
795 sp<GraphicBuffer> buf;
796 auto ret = igbProducer->dequeueBuffer(&slot, &fence, mDisplayWidth, mDisplayHeight / 8,
797 PIXEL_FORMAT_RGBA_8888, GRALLOC_USAGE_SW_WRITE_OFTEN,
798 nullptr, nullptr);
799 ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, ret);
800 ASSERT_EQ(OK, igbProducer->requestBuffer(slot, &buf));
801
802 uint32_t* bufData;
803 buf->lock(static_cast<uint32_t>(GraphicBuffer::USAGE_SW_WRITE_OFTEN),
804 reinterpret_cast<void**>(&bufData));
805 g = 255;
806 fillBuffer(bufData, Rect(buf->getWidth(), buf->getHeight()), buf->getStride(), r, g, b);
807 buf->unlock();
808
809 IGraphicBufferProducer::QueueBufferOutput qbOutput;
810 IGraphicBufferProducer::QueueBufferInput input(systemTime(), true /* autotimestamp */,
811 HAL_DATASPACE_UNKNOWN, {},
812 NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW,
813 0, Fence::NO_FENCE);
814 igbProducer->queueBuffer(slot, input, &qbOutput);
815 adapter.waitForCallbacks();
816 }
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*) {};
1114 adapter.syncNextTransaction(callback2);
1115
1116 std::unique_lock<std::mutex> lock(mutex);
1117 if (!receivedCallback) {
1118 ASSERT_NE(callbackReceivedCv.wait_for(lock, std::chrono::seconds(3)),
1119 std::cv_status::timeout)
1120 << "did not receive callback";
1121 }
1122
1123 ASSERT_TRUE(receivedCallback);
1124}
1125
chaviwc1cf4022022-06-03 13:32:33 -05001126TEST_F(BLASTBufferQueueTest, SyncNextTransactionDropBuffer) {
1127 uint8_t r = 255;
1128 uint8_t g = 0;
1129 uint8_t b = 0;
1130
1131 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
1132
1133 sp<IGraphicBufferProducer> igbProducer;
1134 setUpProducer(adapter, igbProducer);
1135
1136 Transaction sync;
1137 adapter.setSyncTransaction(sync);
1138 queueBuffer(igbProducer, 0, 255, 0, 0);
1139
1140 // Merge a transaction that has a complete callback into the next frame so we can get notified
1141 // when to take a screenshot
1142 CallbackHelper transactionCallback;
1143 Transaction t;
1144 t.addTransactionCompletedCallback(transactionCallback.function,
1145 transactionCallback.getContext());
1146 adapter.mergeWithNextTransaction(&t, 2);
1147 queueBuffer(igbProducer, r, g, b, 0);
1148
1149 // Drop the buffer, but ensure the next one continues to get processed.
1150 sync.setBuffer(mSurfaceControl, nullptr);
1151
1152 CallbackData callbackData;
1153 transactionCallback.getCallbackData(&callbackData);
1154 ASSERT_EQ(NO_ERROR, captureDisplay(mCaptureArgs, mCaptureResults));
1155 ASSERT_NO_FATAL_FAILURE(
1156 checkScreenCapture(r, g, b, {0, 0, (int32_t)mDisplayWidth, (int32_t)mDisplayHeight}));
Rob Carr4f797ab2022-07-07 18:29:22 +00001157 sync.apply();
chaviwc1cf4022022-06-03 13:32:33 -05001158}
1159
Vishnu Nair1e8bf102021-12-28 14:36:59 -08001160// This test will currently fail because the old surfacecontrol will steal the last presented buffer
1161// until the old surface control is destroyed. This is not necessarily a bug but to document a
1162// limitation with the update API and to test any changes to make the api more robust. The current
1163// approach for the client is to recreate the blastbufferqueue when the surfacecontrol updates.
1164TEST_F(BLASTBufferQueueTest, DISABLED_DisconnectProducerTest) {
1165 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
1166 std::vector<sp<SurfaceControl>> surfaceControls;
1167 sp<IGraphicBufferProducer> igbProducer;
1168 for (int i = 0; i < 10; i++) {
1169 sp<SurfaceControl> sc =
1170 mClient->createSurface(String8("TestSurface"), mDisplayWidth, mDisplayHeight,
1171 PIXEL_FORMAT_RGBA_8888,
1172 ISurfaceComposerClient::eFXSurfaceBufferState,
1173 /*parent*/ nullptr);
1174 Transaction()
1175 .setLayerStack(mSurfaceControl, ui::DEFAULT_LAYER_STACK)
1176 .setLayer(mSurfaceControl, std::numeric_limits<int32_t>::max())
1177 .show(mSurfaceControl)
1178 .setDataspace(mSurfaceControl, ui::Dataspace::V0_SRGB)
1179 .apply(true);
1180 surfaceControls.push_back(sc);
1181 adapter.update(sc, mDisplayWidth, mDisplayHeight);
1182
1183 setUpProducer(adapter, igbProducer);
1184 Transaction next;
1185 queueBuffer(igbProducer, 0, 255, 0, 0);
1186 queueBuffer(igbProducer, 0, 0, 255, 0);
Tianhao Yao4861b102022-02-03 20:18:35 +00001187 adapter.setSyncTransaction(next, true);
Vishnu Nair1e8bf102021-12-28 14:36:59 -08001188 queueBuffer(igbProducer, 255, 0, 0, 0);
1189
1190 CallbackHelper transactionCallback;
1191 next.addTransactionCompletedCallback(transactionCallback.function,
1192 transactionCallback.getContext())
1193 .apply();
1194
1195 CallbackData callbackData;
1196 transactionCallback.getCallbackData(&callbackData);
1197 // capture screen and verify that it is red
1198 ASSERT_EQ(NO_ERROR, captureDisplay(mCaptureArgs, mCaptureResults));
1199 ASSERT_NO_FATAL_FAILURE(
1200 checkScreenCapture(255, 0, 0,
1201 {0, 0, (int32_t)mDisplayWidth, (int32_t)mDisplayHeight}));
1202 igbProducer->disconnect(NATIVE_WINDOW_API_CPU);
1203 }
1204}
1205
1206// See DISABLED_DisconnectProducerTest
1207TEST_F(BLASTBufferQueueTest, DISABLED_UpdateSurfaceControlTest) {
1208 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
1209 std::vector<sp<SurfaceControl>> surfaceControls;
1210 sp<IGraphicBufferProducer> igbProducer;
1211 for (int i = 0; i < 10; i++) {
1212 sp<SurfaceControl> sc =
1213 mClient->createSurface(String8("TestSurface"), mDisplayWidth, mDisplayHeight,
1214 PIXEL_FORMAT_RGBA_8888,
1215 ISurfaceComposerClient::eFXSurfaceBufferState,
1216 /*parent*/ nullptr);
1217 Transaction()
1218 .setLayerStack(mSurfaceControl, ui::DEFAULT_LAYER_STACK)
1219 .setLayer(mSurfaceControl, std::numeric_limits<int32_t>::max())
1220 .show(mSurfaceControl)
1221 .setDataspace(mSurfaceControl, ui::Dataspace::V0_SRGB)
1222 .apply(true);
1223 surfaceControls.push_back(sc);
1224 adapter.update(sc, mDisplayWidth, mDisplayHeight);
1225 setUpProducer(adapter, igbProducer);
1226
1227 Transaction next;
1228 queueBuffer(igbProducer, 0, 255, 0, 0);
1229 queueBuffer(igbProducer, 0, 0, 255, 0);
Tianhao Yao4861b102022-02-03 20:18:35 +00001230 adapter.setSyncTransaction(next, true);
Vishnu Nair1e8bf102021-12-28 14:36:59 -08001231 queueBuffer(igbProducer, 255, 0, 0, 0);
1232
1233 CallbackHelper transactionCallback;
1234 next.addTransactionCompletedCallback(transactionCallback.function,
1235 transactionCallback.getContext())
1236 .apply();
1237
1238 CallbackData callbackData;
1239 transactionCallback.getCallbackData(&callbackData);
1240 // capture screen and verify that it is red
1241 ASSERT_EQ(NO_ERROR, captureDisplay(mCaptureArgs, mCaptureResults));
1242 ASSERT_NO_FATAL_FAILURE(
1243 checkScreenCapture(255, 0, 0,
1244 {0, 0, (int32_t)mDisplayWidth, (int32_t)mDisplayHeight}));
1245 }
1246}
1247
Vishnu Nair89496122020-12-14 17:14:53 -08001248class TestProducerListener : public BnProducerListener {
1249public:
1250 sp<IGraphicBufferProducer> mIgbp;
1251 TestProducerListener(const sp<IGraphicBufferProducer>& igbp) : mIgbp(igbp) {}
1252 void onBufferReleased() override {
1253 sp<GraphicBuffer> buffer;
1254 sp<Fence> fence;
1255 mIgbp->detachNextBuffer(&buffer, &fence);
1256 }
1257};
1258
1259TEST_F(BLASTBufferQueueTest, CustomProducerListener) {
1260 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
1261 sp<IGraphicBufferProducer> igbProducer = adapter.getIGraphicBufferProducer();
1262 ASSERT_NE(nullptr, igbProducer.get());
1263 ASSERT_EQ(NO_ERROR, igbProducer->setMaxDequeuedBufferCount(2));
1264 IGraphicBufferProducer::QueueBufferOutput qbOutput;
1265 ASSERT_EQ(NO_ERROR,
1266 igbProducer->connect(new TestProducerListener(igbProducer), NATIVE_WINDOW_API_CPU,
1267 false, &qbOutput));
1268 ASSERT_NE(ui::Transform::ROT_INVALID, qbOutput.transformHint);
1269 for (int i = 0; i < 3; i++) {
1270 int slot;
1271 sp<Fence> fence;
1272 sp<GraphicBuffer> buf;
1273 auto ret = igbProducer->dequeueBuffer(&slot, &fence, mDisplayWidth, mDisplayHeight,
1274 PIXEL_FORMAT_RGBA_8888, GRALLOC_USAGE_SW_WRITE_OFTEN,
1275 nullptr, nullptr);
1276 ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, ret);
1277 ASSERT_EQ(OK, igbProducer->requestBuffer(slot, &buf));
1278 IGraphicBufferProducer::QueueBufferOutput qbOutput;
Vishnu Nair1506b182021-02-22 14:35:15 -08001279 IGraphicBufferProducer::QueueBufferInput input(systemTime(), true /* autotimestamp */,
1280 HAL_DATASPACE_UNKNOWN,
Vishnu Nair89496122020-12-14 17:14:53 -08001281 Rect(mDisplayWidth, mDisplayHeight),
1282 NATIVE_WINDOW_SCALING_MODE_FREEZE, 0,
1283 Fence::NO_FENCE);
1284 igbProducer->queueBuffer(slot, input, &qbOutput);
1285 }
1286 adapter.waitForCallbacks();
1287}
1288
Vishnu Nair17dde612020-12-28 11:39:59 -08001289TEST_F(BLASTBufferQueueTest, QueryNativeWindowQueuesToWindowComposer) {
1290 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
1291
1292 sp<android::Surface> surface = new Surface(adapter.getIGraphicBufferProducer());
1293 ANativeWindow* nativeWindow = (ANativeWindow*)(surface.get());
1294 int queuesToNativeWindow = 0;
1295 int err = nativeWindow->query(nativeWindow, NATIVE_WINDOW_QUEUES_TO_WINDOW_COMPOSER,
1296 &queuesToNativeWindow);
1297 ASSERT_EQ(NO_ERROR, err);
1298 ASSERT_EQ(queuesToNativeWindow, 1);
1299}
1300
Vishnu Nair083efd32021-02-12 09:32:30 -08001301// Test a slow producer doesn't hold up a faster producer from the same client. Essentially tests
1302// BBQ uses separate transaction queues.
Vishnu Nair277142c2021-01-05 18:35:29 -08001303TEST_F(BLASTBufferQueueTest, OutOfOrderTransactionTest) {
1304 sp<SurfaceControl> bgSurface =
1305 mClient->createSurface(String8("BGTest"), 0, 0, PIXEL_FORMAT_RGBA_8888,
1306 ISurfaceComposerClient::eFXSurfaceBufferState);
1307 ASSERT_NE(nullptr, bgSurface.get());
1308 Transaction t;
Dominik Laskowski29fa1462021-04-27 15:51:50 -07001309 t.setLayerStack(bgSurface, ui::DEFAULT_LAYER_STACK)
Vishnu Nair277142c2021-01-05 18:35:29 -08001310 .show(bgSurface)
1311 .setDataspace(bgSurface, ui::Dataspace::V0_SRGB)
Vishnu Nair277142c2021-01-05 18:35:29 -08001312 .setLayer(bgSurface, std::numeric_limits<int32_t>::max() - 1)
1313 .apply();
1314
1315 BLASTBufferQueueHelper slowAdapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
1316 sp<IGraphicBufferProducer> slowIgbProducer;
1317 setUpProducer(slowAdapter, slowIgbProducer);
1318 nsecs_t presentTimeDelay = std::chrono::nanoseconds(500ms).count();
Vishnu Nair1506b182021-02-22 14:35:15 -08001319 queueBuffer(slowIgbProducer, 0 /* r */, 255 /* g */, 0 /* b */, presentTimeDelay);
Vishnu Nair277142c2021-01-05 18:35:29 -08001320
1321 BLASTBufferQueueHelper fastAdapter(bgSurface, mDisplayWidth, mDisplayHeight);
1322 sp<IGraphicBufferProducer> fastIgbProducer;
1323 setUpProducer(fastAdapter, fastIgbProducer);
1324 uint8_t r = 255;
1325 uint8_t g = 0;
1326 uint8_t b = 0;
1327 queueBuffer(fastIgbProducer, r, g, b, 0 /* presentTimeDelay */);
1328 fastAdapter.waitForCallbacks();
1329
1330 // capture screen and verify that it is red
1331 ASSERT_EQ(NO_ERROR, captureDisplay(mCaptureArgs, mCaptureResults));
1332
1333 ASSERT_NO_FATAL_FAILURE(
Chavi Weingartena5aedbd2021-04-09 13:37:33 +00001334 checkScreenCapture(r, g, b,
1335 {0, 0, (int32_t)mDisplayWidth, (int32_t)mDisplayHeight / 2}));
Vishnu Nair277142c2021-01-05 18:35:29 -08001336}
1337
Vishnu Naira4fbca52021-07-07 16:52:34 -07001338TEST_F(BLASTBufferQueueTest, TransformHint) {
1339 // Transform hint is provided to BBQ via the surface control passed by WM
1340 mSurfaceControl->setTransformHint(ui::Transform::ROT_90);
1341
1342 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
1343 sp<IGraphicBufferProducer> igbProducer = adapter.getIGraphicBufferProducer();
1344 ASSERT_NE(nullptr, igbProducer.get());
1345 ASSERT_EQ(NO_ERROR, igbProducer->setMaxDequeuedBufferCount(2));
1346 sp<Surface> surface = adapter.getSurface();
1347
1348 // Before connecting to the surface, we do not get a valid transform hint
1349 int transformHint;
1350 surface->query(NATIVE_WINDOW_TRANSFORM_HINT, &transformHint);
1351 ASSERT_EQ(ui::Transform::ROT_0, transformHint);
1352
1353 ASSERT_EQ(NO_ERROR,
1354 surface->connect(NATIVE_WINDOW_API_CPU, new TestProducerListener(igbProducer)));
1355
1356 // After connecting to the surface, we should get the correct hint.
1357 surface->query(NATIVE_WINDOW_TRANSFORM_HINT, &transformHint);
1358 ASSERT_EQ(ui::Transform::ROT_90, transformHint);
1359
1360 ANativeWindow_Buffer buffer;
1361 surface->lock(&buffer, nullptr /* inOutDirtyBounds */);
1362
1363 // Transform hint is updated via callbacks or surface control updates
1364 mSurfaceControl->setTransformHint(ui::Transform::ROT_0);
1365 adapter.update(mSurfaceControl, mDisplayWidth, mDisplayHeight);
1366
1367 // The hint does not change and matches the value used when dequeueing the buffer.
1368 surface->query(NATIVE_WINDOW_TRANSFORM_HINT, &transformHint);
1369 ASSERT_EQ(ui::Transform::ROT_90, transformHint);
1370
1371 surface->unlockAndPost();
1372
1373 // After queuing the buffer, we get the updated transform hint
1374 surface->query(NATIVE_WINDOW_TRANSFORM_HINT, &transformHint);
1375 ASSERT_EQ(ui::Transform::ROT_0, transformHint);
1376
1377 adapter.waitForCallbacks();
1378}
1379
Valerie Hau5977fc82019-12-05 15:56:39 -08001380class BLASTBufferQueueTransformTest : public BLASTBufferQueueTest {
1381public:
1382 void test(uint32_t tr) {
1383 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
1384 sp<IGraphicBufferProducer> igbProducer;
1385 setUpProducer(adapter, igbProducer);
1386
1387 auto bufWidth = mDisplayWidth;
1388 auto bufHeight = mDisplayHeight;
1389 int slot;
1390 sp<Fence> fence;
1391 sp<GraphicBuffer> buf;
1392
1393 auto ret = igbProducer->dequeueBuffer(&slot, &fence, bufWidth, bufHeight,
1394 PIXEL_FORMAT_RGBA_8888, GRALLOC_USAGE_SW_WRITE_OFTEN,
1395 nullptr, nullptr);
1396 ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, ret);
1397 ASSERT_EQ(OK, igbProducer->requestBuffer(slot, &buf));
1398
1399 fillQuadrants(buf);
1400
1401 IGraphicBufferProducer::QueueBufferOutput qbOutput;
Vishnu Nair1506b182021-02-22 14:35:15 -08001402 IGraphicBufferProducer::QueueBufferInput input(systemTime(), true /* autotimestamp */,
1403 HAL_DATASPACE_UNKNOWN,
Valerie Hau5977fc82019-12-05 15:56:39 -08001404 Rect(bufWidth, bufHeight),
Vishnu Naire1a42322020-10-02 17:42:04 -07001405 NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW,
1406 tr, Fence::NO_FENCE);
Valerie Hau5977fc82019-12-05 15:56:39 -08001407 igbProducer->queueBuffer(slot, input, &qbOutput);
Dominik Laskowski718f9602019-11-09 20:01:35 -08001408 ASSERT_NE(ui::Transform::ROT_INVALID, qbOutput.transformHint);
Valerie Hau5977fc82019-12-05 15:56:39 -08001409
1410 adapter.waitForCallbacks();
chaviw8ffc7b82020-08-18 11:25:37 -07001411 ASSERT_EQ(NO_ERROR, captureDisplay(mCaptureArgs, mCaptureResults));
chaviwd2432892020-07-24 17:42:39 -07001412
Valerie Hau5977fc82019-12-05 15:56:39 -08001413 switch (tr) {
1414 case ui::Transform::ROT_0:
1415 ASSERT_NO_FATAL_FAILURE(checkScreenCapture(0, 0, 0,
1416 {0, 0, (int32_t)mDisplayWidth / 2,
1417 (int32_t)mDisplayHeight / 2},
1418 1));
1419 ASSERT_NO_FATAL_FAILURE(
1420 checkScreenCapture(255, 0, 0,
1421 {(int32_t)mDisplayWidth / 2, 0, (int32_t)mDisplayWidth,
1422 (int32_t)mDisplayHeight / 2},
1423 1));
1424 ASSERT_NO_FATAL_FAILURE(
1425 checkScreenCapture(0, 255, 0,
1426 {(int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight / 2,
1427 (int32_t)mDisplayWidth, (int32_t)mDisplayHeight},
1428 1));
1429 ASSERT_NO_FATAL_FAILURE(
1430 checkScreenCapture(0, 0, 255,
1431 {0, (int32_t)mDisplayHeight / 2,
1432 (int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight},
1433 1));
1434 break;
1435 case ui::Transform::FLIP_H:
1436 ASSERT_NO_FATAL_FAILURE(checkScreenCapture(255, 0, 0,
1437 {0, 0, (int32_t)mDisplayWidth / 2,
1438 (int32_t)mDisplayHeight / 2},
1439 1));
1440 ASSERT_NO_FATAL_FAILURE(
1441 checkScreenCapture(0, 0, 0,
1442 {(int32_t)mDisplayWidth / 2, 0, (int32_t)mDisplayWidth,
1443 (int32_t)mDisplayHeight / 2},
1444 1));
1445 ASSERT_NO_FATAL_FAILURE(
1446 checkScreenCapture(0, 0, 255,
1447 {(int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight / 2,
1448 (int32_t)mDisplayWidth, (int32_t)mDisplayHeight},
1449 1));
1450 ASSERT_NO_FATAL_FAILURE(
1451 checkScreenCapture(0, 255, 0,
1452 {0, (int32_t)mDisplayHeight / 2,
1453 (int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight},
1454 1));
1455 break;
1456 case ui::Transform::FLIP_V:
1457 ASSERT_NO_FATAL_FAILURE(checkScreenCapture(0, 0, 255,
1458 {0, 0, (int32_t)mDisplayWidth / 2,
1459 (int32_t)mDisplayHeight / 2},
1460 1));
1461 ASSERT_NO_FATAL_FAILURE(
1462 checkScreenCapture(0, 255, 0,
1463 {(int32_t)mDisplayWidth / 2, 0, (int32_t)mDisplayWidth,
1464 (int32_t)mDisplayHeight / 2},
1465 1));
1466 ASSERT_NO_FATAL_FAILURE(
1467 checkScreenCapture(255, 0, 0,
1468 {(int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight / 2,
1469 (int32_t)mDisplayWidth, (int32_t)mDisplayHeight},
1470 1));
1471 ASSERT_NO_FATAL_FAILURE(
1472 checkScreenCapture(0, 0, 0,
1473 {0, (int32_t)mDisplayHeight / 2,
1474 (int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight},
1475 1));
1476 break;
1477 case ui::Transform::ROT_90:
1478 ASSERT_NO_FATAL_FAILURE(checkScreenCapture(0, 0, 255,
1479 {0, 0, (int32_t)mDisplayWidth / 2,
1480 (int32_t)mDisplayHeight / 2},
1481 1));
1482 ASSERT_NO_FATAL_FAILURE(
1483 checkScreenCapture(0, 0, 0,
1484 {(int32_t)mDisplayWidth / 2, 0, (int32_t)mDisplayWidth,
1485 (int32_t)mDisplayHeight / 2},
1486 1));
1487 ASSERT_NO_FATAL_FAILURE(
1488 checkScreenCapture(255, 0, 0,
1489 {(int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight / 2,
1490 (int32_t)mDisplayWidth, (int32_t)mDisplayHeight},
1491 1));
1492 ASSERT_NO_FATAL_FAILURE(
1493 checkScreenCapture(0, 255, 0,
1494 {0, (int32_t)mDisplayHeight / 2,
1495 (int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight},
1496 1));
1497 break;
1498 case ui::Transform::ROT_180:
1499 ASSERT_NO_FATAL_FAILURE(checkScreenCapture(0, 255, 0,
1500 {0, 0, (int32_t)mDisplayWidth / 2,
1501 (int32_t)mDisplayHeight / 2},
1502 1));
1503 ASSERT_NO_FATAL_FAILURE(
1504 checkScreenCapture(0, 0, 255,
1505 {(int32_t)mDisplayWidth / 2, 0, (int32_t)mDisplayWidth,
1506 (int32_t)mDisplayHeight / 2},
1507 1));
1508 ASSERT_NO_FATAL_FAILURE(
1509 checkScreenCapture(0, 0, 0,
1510 {(int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight / 2,
1511 (int32_t)mDisplayWidth, (int32_t)mDisplayHeight},
1512 1));
1513 ASSERT_NO_FATAL_FAILURE(
1514 checkScreenCapture(255, 0, 0,
1515 {0, (int32_t)mDisplayHeight / 2,
1516 (int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight},
1517 1));
1518 break;
1519 case ui::Transform::ROT_270:
1520 ASSERT_NO_FATAL_FAILURE(checkScreenCapture(255, 0, 0,
1521 {0, 0, (int32_t)mDisplayWidth / 2,
1522 (int32_t)mDisplayHeight / 2},
1523 1));
1524 ASSERT_NO_FATAL_FAILURE(
1525 checkScreenCapture(0, 255, 0,
1526 {(int32_t)mDisplayWidth / 2, 0, (int32_t)mDisplayWidth,
1527 (int32_t)mDisplayHeight / 2},
1528 1));
1529 ASSERT_NO_FATAL_FAILURE(
1530 checkScreenCapture(0, 0, 255,
1531 {(int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight / 2,
1532 (int32_t)mDisplayWidth, (int32_t)mDisplayHeight},
1533 1));
1534 ASSERT_NO_FATAL_FAILURE(
1535 checkScreenCapture(0, 0, 0,
1536 {0, (int32_t)mDisplayHeight / 2,
1537 (int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight},
1538 1));
1539 }
1540 }
1541};
1542
1543TEST_F(BLASTBufferQueueTransformTest, setTransform_ROT_0) {
1544 test(ui::Transform::ROT_0);
1545}
1546
1547TEST_F(BLASTBufferQueueTransformTest, setTransform_FLIP_H) {
1548 test(ui::Transform::FLIP_H);
1549}
1550
1551TEST_F(BLASTBufferQueueTransformTest, setTransform_FLIP_V) {
1552 test(ui::Transform::FLIP_V);
1553}
1554
1555TEST_F(BLASTBufferQueueTransformTest, setTransform_ROT_90) {
1556 test(ui::Transform::ROT_90);
1557}
1558
1559TEST_F(BLASTBufferQueueTransformTest, setTransform_ROT_180) {
1560 test(ui::Transform::ROT_180);
1561}
1562
1563TEST_F(BLASTBufferQueueTransformTest, setTransform_ROT_270) {
1564 test(ui::Transform::ROT_270);
1565}
Valerie Hau871d6352020-01-29 08:44:02 -08001566
1567class BLASTFrameEventHistoryTest : public BLASTBufferQueueTest {
1568public:
1569 void setUpAndQueueBuffer(const sp<IGraphicBufferProducer>& igbProducer,
Vishnu Nairde66dc72021-06-17 17:54:41 -07001570 nsecs_t* outRequestedPresentTime, nsecs_t* postedTime,
Valerie Hau871d6352020-01-29 08:44:02 -08001571 IGraphicBufferProducer::QueueBufferOutput* qbOutput,
Vishnu Nairde66dc72021-06-17 17:54:41 -07001572 bool getFrameTimestamps, nsecs_t requestedPresentTime = systemTime()) {
Valerie Hau871d6352020-01-29 08:44:02 -08001573 int slot;
1574 sp<Fence> fence;
1575 sp<GraphicBuffer> buf;
1576 auto ret = igbProducer->dequeueBuffer(&slot, &fence, mDisplayWidth, mDisplayHeight,
1577 PIXEL_FORMAT_RGBA_8888, GRALLOC_USAGE_SW_WRITE_OFTEN,
1578 nullptr, nullptr);
Vishnu Nairde66dc72021-06-17 17:54:41 -07001579 if (IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION == ret) {
1580 ASSERT_EQ(OK, igbProducer->requestBuffer(slot, &buf));
1581 }
Valerie Hau871d6352020-01-29 08:44:02 -08001582
Vishnu Nairde66dc72021-06-17 17:54:41 -07001583 *outRequestedPresentTime = requestedPresentTime;
1584 IGraphicBufferProducer::QueueBufferInput input(requestedPresentTime, false,
1585 HAL_DATASPACE_UNKNOWN,
Valerie Hau871d6352020-01-29 08:44:02 -08001586 Rect(mDisplayWidth, mDisplayHeight),
1587 NATIVE_WINDOW_SCALING_MODE_FREEZE, 0,
1588 Fence::NO_FENCE, /*sticky*/ 0,
1589 getFrameTimestamps);
1590 if (postedTime) *postedTime = systemTime();
1591 igbProducer->queueBuffer(slot, input, qbOutput);
1592 }
Vishnu Nair083efd32021-02-12 09:32:30 -08001593 sp<SurfaceControl> mBufferQueueSurfaceControl;
Valerie Hau871d6352020-01-29 08:44:02 -08001594};
1595
1596TEST_F(BLASTFrameEventHistoryTest, FrameEventHistory_Basic) {
1597 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
1598 sp<IGraphicBufferProducer> igbProducer;
1599 ProducerFrameEventHistory history;
1600 setUpProducer(adapter, igbProducer);
1601
1602 IGraphicBufferProducer::QueueBufferOutput qbOutput;
1603 nsecs_t requestedPresentTimeA = 0;
1604 nsecs_t postedTimeA = 0;
1605 setUpAndQueueBuffer(igbProducer, &requestedPresentTimeA, &postedTimeA, &qbOutput, true);
1606 history.applyDelta(qbOutput.frameTimestamps);
1607
1608 FrameEvents* events = nullptr;
1609 events = history.getFrame(1);
1610 ASSERT_NE(nullptr, events);
1611 ASSERT_EQ(1, events->frameNumber);
1612 ASSERT_EQ(requestedPresentTimeA, events->requestedPresentTime);
1613 ASSERT_GE(events->postedTime, postedTimeA);
1614
Vishnu Nair1506b182021-02-22 14:35:15 -08001615 adapter.waitForCallback(1);
Valerie Hau871d6352020-01-29 08:44:02 -08001616
1617 // queue another buffer so we query for frame event deltas
1618 nsecs_t requestedPresentTimeB = 0;
1619 nsecs_t postedTimeB = 0;
1620 setUpAndQueueBuffer(igbProducer, &requestedPresentTimeB, &postedTimeB, &qbOutput, true);
1621 history.applyDelta(qbOutput.frameTimestamps);
1622 events = history.getFrame(1);
1623 ASSERT_NE(nullptr, events);
1624
1625 // frame number, requestedPresentTime, and postTime should not have changed
1626 ASSERT_EQ(1, events->frameNumber);
1627 ASSERT_EQ(requestedPresentTimeA, events->requestedPresentTime);
1628 ASSERT_GE(events->postedTime, postedTimeA);
1629
1630 ASSERT_GE(events->latchTime, postedTimeA);
1631 ASSERT_GE(events->dequeueReadyTime, events->latchTime);
1632 ASSERT_NE(nullptr, events->gpuCompositionDoneFence);
1633 ASSERT_NE(nullptr, events->displayPresentFence);
1634 ASSERT_NE(nullptr, events->releaseFence);
1635
1636 // we should also have gotten the initial values for the next frame
1637 events = history.getFrame(2);
1638 ASSERT_NE(nullptr, events);
1639 ASSERT_EQ(2, events->frameNumber);
1640 ASSERT_EQ(requestedPresentTimeB, events->requestedPresentTime);
1641 ASSERT_GE(events->postedTime, postedTimeB);
Valerie Hau78491e92020-04-15 13:10:56 -07001642
1643 // wait for any callbacks that have not been received
1644 adapter.waitForCallbacks();
Valerie Hau871d6352020-01-29 08:44:02 -08001645}
Vishnu Nair083efd32021-02-12 09:32:30 -08001646
Vishnu Nair083efd32021-02-12 09:32:30 -08001647TEST_F(BLASTFrameEventHistoryTest, FrameEventHistory_DroppedFrame) {
1648 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
1649 sp<IGraphicBufferProducer> igbProducer;
1650 setUpProducer(adapter, igbProducer);
1651
1652 ProducerFrameEventHistory history;
1653 IGraphicBufferProducer::QueueBufferOutput qbOutput;
1654 nsecs_t requestedPresentTimeA = 0;
1655 nsecs_t postedTimeA = 0;
Vishnu Nairde66dc72021-06-17 17:54:41 -07001656 // Present the frame sometime in the future so we can add two frames to the queue so the older
1657 // one will be dropped.
1658 nsecs_t presentTime = systemTime() + std::chrono::nanoseconds(500ms).count();
Vishnu Nair083efd32021-02-12 09:32:30 -08001659 setUpAndQueueBuffer(igbProducer, &requestedPresentTimeA, &postedTimeA, &qbOutput, true,
Vishnu Nairde66dc72021-06-17 17:54:41 -07001660 presentTime);
Vishnu Nair083efd32021-02-12 09:32:30 -08001661 history.applyDelta(qbOutput.frameTimestamps);
1662
1663 FrameEvents* events = nullptr;
1664 events = history.getFrame(1);
1665 ASSERT_NE(nullptr, events);
1666 ASSERT_EQ(1, events->frameNumber);
1667 ASSERT_EQ(requestedPresentTimeA, events->requestedPresentTime);
1668 ASSERT_GE(events->postedTime, postedTimeA);
1669
1670 // queue another buffer so the first can be dropped
1671 nsecs_t requestedPresentTimeB = 0;
1672 nsecs_t postedTimeB = 0;
Vishnu Nairde66dc72021-06-17 17:54:41 -07001673 presentTime = systemTime() + std::chrono::nanoseconds(1ms).count();
1674 setUpAndQueueBuffer(igbProducer, &requestedPresentTimeB, &postedTimeB, &qbOutput, true,
1675 presentTime);
Vishnu Nair083efd32021-02-12 09:32:30 -08001676 history.applyDelta(qbOutput.frameTimestamps);
1677 events = history.getFrame(1);
1678 ASSERT_NE(nullptr, events);
1679
1680 // frame number, requestedPresentTime, and postTime should not have changed
1681 ASSERT_EQ(1, events->frameNumber);
1682 ASSERT_EQ(requestedPresentTimeA, events->requestedPresentTime);
1683 ASSERT_GE(events->postedTime, postedTimeA);
1684
Vishnu Nairde66dc72021-06-17 17:54:41 -07001685 // a valid latchtime and pre and post composition info should not be set for the dropped frame
Vishnu Nair083efd32021-02-12 09:32:30 -08001686 ASSERT_FALSE(events->hasLatchInfo());
1687 ASSERT_FALSE(events->hasDequeueReadyInfo());
Vishnu Nairde66dc72021-06-17 17:54:41 -07001688 ASSERT_FALSE(events->hasGpuCompositionDoneInfo());
1689 ASSERT_FALSE(events->hasDisplayPresentInfo());
1690 ASSERT_FALSE(events->hasReleaseInfo());
Vishnu Nair083efd32021-02-12 09:32:30 -08001691
Vishnu Nairde66dc72021-06-17 17:54:41 -07001692 // wait for the last transaction to be completed.
1693 adapter.waitForCallback(2);
Vishnu Nair083efd32021-02-12 09:32:30 -08001694
Vishnu Nairde66dc72021-06-17 17:54:41 -07001695 // queue another buffer so we query for frame event deltas
1696 nsecs_t requestedPresentTimeC = 0;
1697 nsecs_t postedTimeC = 0;
1698 setUpAndQueueBuffer(igbProducer, &requestedPresentTimeC, &postedTimeC, &qbOutput, true);
1699 history.applyDelta(qbOutput.frameTimestamps);
1700
1701 // frame number, requestedPresentTime, and postTime should not have changed
1702 ASSERT_EQ(1, events->frameNumber);
1703 ASSERT_EQ(requestedPresentTimeA, events->requestedPresentTime);
1704 ASSERT_GE(events->postedTime, postedTimeA);
1705
1706 // a valid latchtime and pre and post composition info should not be set for the dropped frame
1707 ASSERT_FALSE(events->hasLatchInfo());
1708 ASSERT_FALSE(events->hasDequeueReadyInfo());
1709 ASSERT_FALSE(events->hasGpuCompositionDoneInfo());
1710 ASSERT_FALSE(events->hasDisplayPresentInfo());
1711 ASSERT_FALSE(events->hasReleaseInfo());
1712
1713 // we should also have gotten values for the presented frame
Vishnu Nair083efd32021-02-12 09:32:30 -08001714 events = history.getFrame(2);
1715 ASSERT_NE(nullptr, events);
1716 ASSERT_EQ(2, events->frameNumber);
1717 ASSERT_EQ(requestedPresentTimeB, events->requestedPresentTime);
1718 ASSERT_GE(events->postedTime, postedTimeB);
Vishnu Nairde66dc72021-06-17 17:54:41 -07001719 ASSERT_GE(events->latchTime, postedTimeB);
1720 ASSERT_GE(events->dequeueReadyTime, events->latchTime);
1721 ASSERT_NE(nullptr, events->gpuCompositionDoneFence);
1722 ASSERT_NE(nullptr, events->displayPresentFence);
1723 ASSERT_NE(nullptr, events->releaseFence);
1724
1725 // wait for any callbacks that have not been received
1726 adapter.waitForCallbacks();
Vishnu Nair083efd32021-02-12 09:32:30 -08001727}
1728
Vishnu Nair9a69a042021-06-18 13:19:49 -07001729TEST_F(BLASTFrameEventHistoryTest, FrameEventHistory_CompositorTimings) {
1730 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
1731 sp<IGraphicBufferProducer> igbProducer;
1732 ProducerFrameEventHistory history;
1733 setUpProducer(adapter, igbProducer);
1734
1735 IGraphicBufferProducer::QueueBufferOutput qbOutput;
1736 nsecs_t requestedPresentTimeA = 0;
1737 nsecs_t postedTimeA = 0;
Vishnu Nair9a69a042021-06-18 13:19:49 -07001738 setUpAndQueueBuffer(igbProducer, &requestedPresentTimeA, &postedTimeA, &qbOutput, true);
1739 history.applyDelta(qbOutput.frameTimestamps);
1740 adapter.waitForCallback(1);
1741
1742 // queue another buffer so we query for frame event deltas
1743 nsecs_t requestedPresentTimeB = 0;
1744 nsecs_t postedTimeB = 0;
1745 setUpAndQueueBuffer(igbProducer, &requestedPresentTimeB, &postedTimeB, &qbOutput, true);
1746 history.applyDelta(qbOutput.frameTimestamps);
1747
1748 // check for a valid compositor deadline
1749 ASSERT_NE(0, history.getReportedCompositeDeadline());
1750
1751 // wait for any callbacks that have not been received
1752 adapter.waitForCallbacks();
1753}
1754
Valerie Hauc5011f92019-10-11 09:52:07 -07001755} // namespace android