blob: a071eb994dd74ed455fdc59db517296eb8f30ccc [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>
Valerie Hauc5011f92019-10-11 09:52:07 -070035#include <ui/GraphicBuffer.h>
Valerie Hauda3446e2019-10-14 15:49:22 -070036#include <ui/GraphicTypes.h>
Valerie Hau8cee3f92019-11-06 10:06:28 -080037#include <ui/Transform.h>
Valerie Hauc5011f92019-10-11 09:52:07 -070038
39#include <gtest/gtest.h>
40
41using namespace std::chrono_literals;
42
43namespace android {
44
Valerie Hauc5011f92019-10-11 09:52:07 -070045using Transaction = SurfaceComposerClient::Transaction;
Valerie Hauda3446e2019-10-14 15:49:22 -070046using android::hardware::graphics::common::V1_2::BufferUsage;
Valerie Hauc5011f92019-10-11 09:52:07 -070047
chaviwd7deef72021-10-06 11:53:40 -050048class CountProducerListener : public BnProducerListener {
49public:
50 void onBufferReleased() override {
51 std::scoped_lock<std::mutex> lock(mMutex);
52 mNumReleased++;
53 mReleaseCallback.notify_one();
54 }
55
56 void waitOnNumberReleased(int32_t expectedNumReleased) {
57 std::unique_lock<std::mutex> lock(mMutex);
58 while (mNumReleased < expectedNumReleased) {
59 ASSERT_NE(mReleaseCallback.wait_for(lock, std::chrono::seconds(3)),
60 std::cv_status::timeout)
61 << "did not receive release";
62 }
63 }
64
65private:
66 std::mutex mMutex;
67 std::condition_variable mReleaseCallback;
68 int32_t mNumReleased GUARDED_BY(mMutex) = 0;
69};
70
chaviwf10b9042021-10-13 15:48:59 -050071class TestBLASTBufferQueue : public BLASTBufferQueue {
72public:
73 TestBLASTBufferQueue(const std::string& name, const sp<SurfaceControl>& surface, int width,
74 int height, int32_t format)
75 : BLASTBufferQueue(name, surface, width, height, format) {}
76
chaviw6b9ffea2021-11-08 09:25:48 -060077 void transactionCallback(nsecs_t latchTime, const sp<Fence>& presentFence,
78 const std::vector<SurfaceControlStats>& stats) override {
79 BLASTBufferQueue::transactionCallback(latchTime, presentFence, stats);
chaviwf10b9042021-10-13 15:48:59 -050080 uint64_t frameNumber = stats[0].frameEventStats.frameNumber;
81
82 {
83 std::unique_lock lock{frameNumberMutex};
chaviw6b9ffea2021-11-08 09:25:48 -060084 mLastTransactionFrameNumber = frameNumber;
85 mWaitForCallbackCV.notify_all();
chaviwf10b9042021-10-13 15:48:59 -050086 }
87 }
88
89 void waitForCallback(int64_t frameNumber) {
90 std::unique_lock lock{frameNumberMutex};
91 // Wait until all but one of the submitted buffers have been released.
chaviw6b9ffea2021-11-08 09:25:48 -060092 while (mLastTransactionFrameNumber < frameNumber) {
93 mWaitForCallbackCV.wait(lock);
chaviwf10b9042021-10-13 15:48:59 -050094 }
95 }
96
97private:
98 std::mutex frameNumberMutex;
chaviw6b9ffea2021-11-08 09:25:48 -060099 std::condition_variable mWaitForCallbackCV;
100 int64_t mLastTransactionFrameNumber = -1;
chaviwf10b9042021-10-13 15:48:59 -0500101};
102
Valerie Hauc5011f92019-10-11 09:52:07 -0700103class BLASTBufferQueueHelper {
104public:
105 BLASTBufferQueueHelper(const sp<SurfaceControl>& sc, int width, int height) {
chaviwf10b9042021-10-13 15:48:59 -0500106 mBlastBufferQueueAdapter = new TestBLASTBufferQueue("TestBLASTBufferQueue", sc, width,
107 height, PIXEL_FORMAT_RGBA_8888);
Valerie Hauc5011f92019-10-11 09:52:07 -0700108 }
109
110 void update(const sp<SurfaceControl>& sc, int width, int height) {
chaviw565ee542021-01-14 10:21:23 -0800111 mBlastBufferQueueAdapter->update(sc, width, height, PIXEL_FORMAT_RGBA_8888);
Valerie Hauc5011f92019-10-11 09:52:07 -0700112 }
113
Tianhao Yao4861b102022-02-03 20:18:35 +0000114 void setSyncTransaction(Transaction& next, bool acquireSingleBuffer = true) {
115 auto callback = [&next](Transaction* t) { next.merge(std::move(*t)); };
116 mBlastBufferQueueAdapter->syncNextTransaction(callback, acquireSingleBuffer);
117 }
118
119 void syncNextTransaction(std::function<void(Transaction*)> callback,
120 bool acquireSingleBuffer = true) {
121 mBlastBufferQueueAdapter->syncNextTransaction(callback, acquireSingleBuffer);
122 }
123
124 void stopContinuousSyncTransaction() {
125 mBlastBufferQueueAdapter->stopContinuousSyncTransaction();
Valerie Hauc5011f92019-10-11 09:52:07 -0700126 }
127
Vishnu Nairea0de002020-11-17 17:42:37 -0800128 int getWidth() { return mBlastBufferQueueAdapter->mSize.width; }
Valerie Hauda3446e2019-10-14 15:49:22 -0700129
Vishnu Nairea0de002020-11-17 17:42:37 -0800130 int getHeight() { return mBlastBufferQueueAdapter->mSize.height; }
Valerie Hauda3446e2019-10-14 15:49:22 -0700131
Tianhao Yao4861b102022-02-03 20:18:35 +0000132 std::function<void(Transaction*)> getTransactionReadyCallback() {
133 return mBlastBufferQueueAdapter->mTransactionReadyCallback;
134 }
Valerie Hauda3446e2019-10-14 15:49:22 -0700135
136 sp<IGraphicBufferProducer> getIGraphicBufferProducer() {
137 return mBlastBufferQueueAdapter->getIGraphicBufferProducer();
138 }
139
Valerie Hauc5011f92019-10-11 09:52:07 -0700140 const sp<SurfaceControl> getSurfaceControl() {
141 return mBlastBufferQueueAdapter->mSurfaceControl;
142 }
143
Vishnu Naira4fbca52021-07-07 16:52:34 -0700144 sp<Surface> getSurface() {
145 return mBlastBufferQueueAdapter->getSurface(false /* includeSurfaceControlHandle */);
146 }
147
Valerie Haud3b90d22019-11-06 09:37:31 -0800148 void waitForCallbacks() {
Valerie Hauda3446e2019-10-14 15:49:22 -0700149 std::unique_lock lock{mBlastBufferQueueAdapter->mMutex};
Vishnu Nair1506b182021-02-22 14:35:15 -0800150 // Wait until all but one of the submitted buffers have been released.
151 while (mBlastBufferQueueAdapter->mSubmitted.size() > 1) {
Valerie Haud3b90d22019-11-06 09:37:31 -0800152 mBlastBufferQueueAdapter->mCallbackCV.wait(lock);
153 }
Valerie Hauda3446e2019-10-14 15:49:22 -0700154 }
155
Vishnu Nair1506b182021-02-22 14:35:15 -0800156 void waitForCallback(int64_t frameNumber) {
chaviwf10b9042021-10-13 15:48:59 -0500157 mBlastBufferQueueAdapter->waitForCallback(frameNumber);
Vishnu Nair1506b182021-02-22 14:35:15 -0800158 }
159
chaviw0acd33a2021-11-02 11:55:37 -0500160 void validateNumFramesSubmitted(int64_t numFramesSubmitted) {
161 std::unique_lock lock{mBlastBufferQueueAdapter->mMutex};
162 ASSERT_EQ(numFramesSubmitted, mBlastBufferQueueAdapter->mSubmitted.size());
163 }
164
chaviwc1cf4022022-06-03 13:32:33 -0500165 void mergeWithNextTransaction(Transaction* merge, uint64_t frameNumber) {
166 mBlastBufferQueueAdapter->mergeWithNextTransaction(merge, frameNumber);
167 }
168
Valerie Hauc5011f92019-10-11 09:52:07 -0700169private:
chaviwf10b9042021-10-13 15:48:59 -0500170 sp<TestBLASTBufferQueue> mBlastBufferQueueAdapter;
Valerie Hauc5011f92019-10-11 09:52:07 -0700171};
172
173class BLASTBufferQueueTest : public ::testing::Test {
174public:
175protected:
176 BLASTBufferQueueTest() {
177 const ::testing::TestInfo* const testInfo =
178 ::testing::UnitTest::GetInstance()->current_test_info();
179 ALOGV("Begin test: %s.%s", testInfo->test_case_name(), testInfo->name());
180 }
181
182 ~BLASTBufferQueueTest() {
183 const ::testing::TestInfo* const testInfo =
184 ::testing::UnitTest::GetInstance()->current_test_info();
185 ALOGV("End test: %s.%s", testInfo->test_case_name(), testInfo->name());
186 }
187
188 void SetUp() {
Valerie Hauda3446e2019-10-14 15:49:22 -0700189 mComposer = ComposerService::getComposerService();
Valerie Hauc5011f92019-10-11 09:52:07 -0700190 mClient = new SurfaceComposerClient();
Valerie Hauda3446e2019-10-14 15:49:22 -0700191 mDisplayToken = mClient->getInternalDisplayToken();
192 ASSERT_NE(nullptr, mDisplayToken.get());
193 Transaction t;
Dominik Laskowski29fa1462021-04-27 15:51:50 -0700194 t.setDisplayLayerStack(mDisplayToken, ui::DEFAULT_LAYER_STACK);
Valerie Hauda3446e2019-10-14 15:49:22 -0700195 t.apply();
196 t.clear();
197
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100198 ui::DisplayMode mode;
199 ASSERT_EQ(NO_ERROR, SurfaceComposerClient::getActiveDisplayMode(mDisplayToken, &mode));
200 const ui::Size& resolution = mode.resolution;
Dominik Laskowski3cb3d4e2019-11-21 11:14:45 -0800201 mDisplayWidth = resolution.getWidth();
202 mDisplayHeight = resolution.getHeight();
Valerie Hauda3446e2019-10-14 15:49:22 -0700203
204 mSurfaceControl = mClient->createSurface(String8("TestSurface"), mDisplayWidth,
205 mDisplayHeight, PIXEL_FORMAT_RGBA_8888,
206 ISurfaceComposerClient::eFXSurfaceBufferState,
207 /*parent*/ nullptr);
Dominik Laskowski29fa1462021-04-27 15:51:50 -0700208 t.setLayerStack(mSurfaceControl, ui::DEFAULT_LAYER_STACK)
Valerie Hauda3446e2019-10-14 15:49:22 -0700209 .setLayer(mSurfaceControl, std::numeric_limits<int32_t>::max())
Valerie Hauda3446e2019-10-14 15:49:22 -0700210 .show(mSurfaceControl)
211 .setDataspace(mSurfaceControl, ui::Dataspace::V0_SRGB)
212 .apply();
chaviwd2432892020-07-24 17:42:39 -0700213
214 mCaptureArgs.displayToken = mDisplayToken;
arthurhung6fa58b72020-11-05 11:56:00 +0800215 mCaptureArgs.dataspace = ui::Dataspace::V0_SRGB;
Valerie Hauda3446e2019-10-14 15:49:22 -0700216 }
217
chaviwd7deef72021-10-06 11:53:40 -0500218 void setUpProducer(BLASTBufferQueueHelper& adapter, sp<IGraphicBufferProducer>& producer,
219 int32_t maxBufferCount = 2) {
Vishnu Nair083efd32021-02-12 09:32:30 -0800220 producer = adapter.getIGraphicBufferProducer();
chaviwd7deef72021-10-06 11:53:40 -0500221 setUpProducer(producer, maxBufferCount);
Vishnu Nair083efd32021-02-12 09:32:30 -0800222 }
223
chaviwd7deef72021-10-06 11:53:40 -0500224 void setUpProducer(sp<IGraphicBufferProducer>& igbProducer, int32_t maxBufferCount) {
Valerie Haud3b90d22019-11-06 09:37:31 -0800225 ASSERT_NE(nullptr, igbProducer.get());
chaviwd7deef72021-10-06 11:53:40 -0500226 ASSERT_EQ(NO_ERROR, igbProducer->setMaxDequeuedBufferCount(maxBufferCount));
Valerie Haud3b90d22019-11-06 09:37:31 -0800227 IGraphicBufferProducer::QueueBufferOutput qbOutput;
chaviwd7deef72021-10-06 11:53:40 -0500228 mProducerListener = new CountProducerListener();
Valerie Haud3b90d22019-11-06 09:37:31 -0800229 ASSERT_EQ(NO_ERROR,
chaviwd7deef72021-10-06 11:53:40 -0500230 igbProducer->connect(mProducerListener, NATIVE_WINDOW_API_CPU, false, &qbOutput));
Dominik Laskowski718f9602019-11-09 20:01:35 -0800231 ASSERT_NE(ui::Transform::ROT_INVALID, qbOutput.transformHint);
Valerie Haud3b90d22019-11-06 09:37:31 -0800232 }
233
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800234 void fillBuffer(uint32_t* bufData, Rect rect, uint32_t stride, uint8_t r, uint8_t g,
235 uint8_t b) {
236 for (uint32_t row = rect.top; row < rect.bottom; row++) {
237 for (uint32_t col = rect.left; col < rect.right; col++) {
Valerie Hauda3446e2019-10-14 15:49:22 -0700238 uint8_t* pixel = (uint8_t*)(bufData + (row * stride) + col);
239 *pixel = r;
240 *(pixel + 1) = g;
241 *(pixel + 2) = b;
242 *(pixel + 3) = 255;
243 }
244 }
245 }
246
Valerie Hau5977fc82019-12-05 15:56:39 -0800247 void fillQuadrants(sp<GraphicBuffer>& buf) {
248 const auto bufWidth = buf->getWidth();
249 const auto bufHeight = buf->getHeight();
250 uint32_t* bufData;
251 buf->lock(static_cast<uint32_t>(GraphicBuffer::USAGE_SW_WRITE_OFTEN),
252 reinterpret_cast<void**>(&bufData));
253 fillBuffer(bufData, Rect(0, 0, bufWidth / 2, bufHeight / 2), buf->getStride(), 0, 0, 0);
254 fillBuffer(bufData, Rect(bufWidth / 2, 0, bufWidth, bufHeight / 2), buf->getStride(), 255,
255 0, 0);
256 fillBuffer(bufData, Rect(bufWidth / 2, bufHeight / 2, bufWidth, bufHeight),
257 buf->getStride(), 0, 255, 0);
258 fillBuffer(bufData, Rect(0, bufHeight / 2, bufWidth / 2, bufHeight), buf->getStride(), 0, 0,
259 255);
260 buf->unlock();
261 }
262
263 void checkScreenCapture(uint8_t r, uint8_t g, uint8_t b, Rect region, int32_t border = 0,
264 bool outsideRegion = false) {
chaviwd2432892020-07-24 17:42:39 -0700265 sp<GraphicBuffer>& captureBuf = mCaptureResults.buffer;
Valerie Hau5977fc82019-12-05 15:56:39 -0800266 const auto epsilon = 3;
chaviwd2432892020-07-24 17:42:39 -0700267 const auto width = captureBuf->getWidth();
268 const auto height = captureBuf->getHeight();
269 const auto stride = captureBuf->getStride();
Valerie Hauda3446e2019-10-14 15:49:22 -0700270
271 uint32_t* bufData;
chaviwd2432892020-07-24 17:42:39 -0700272 captureBuf->lock(static_cast<uint32_t>(GraphicBuffer::USAGE_SW_READ_OFTEN),
273 reinterpret_cast<void**>(&bufData));
Valerie Hauda3446e2019-10-14 15:49:22 -0700274
275 for (uint32_t row = 0; row < height; row++) {
276 for (uint32_t col = 0; col < width; col++) {
277 uint8_t* pixel = (uint8_t*)(bufData + (row * stride) + col);
arthurhung6fa58b72020-11-05 11:56:00 +0800278 ASSERT_NE(nullptr, pixel);
Valerie Hau5977fc82019-12-05 15:56:39 -0800279 bool inRegion;
280 if (!outsideRegion) {
281 inRegion = row >= region.top + border && row < region.bottom - border &&
282 col >= region.left + border && col < region.right - border;
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800283 } else {
Valerie Hau5977fc82019-12-05 15:56:39 -0800284 inRegion = row >= region.top - border && row < region.bottom + border &&
285 col >= region.left - border && col < region.right + border;
286 }
287 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 Hau5977fc82019-12-05 15:56:39 -0800291 } else if (outsideRegion && !inRegion) {
Chavi Weingartena5aedbd2021-04-09 13:37:33 +0000292 ASSERT_GE(epsilon, abs(r - *(pixel)));
293 ASSERT_GE(epsilon, abs(g - *(pixel + 1)));
294 ASSERT_GE(epsilon, abs(b - *(pixel + 2)));
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800295 }
Vishnu Nair1506b182021-02-22 14:35:15 -0800296 ASSERT_EQ(false, ::testing::Test::HasFailure());
Valerie Hauda3446e2019-10-14 15:49:22 -0700297 }
298 }
chaviwd2432892020-07-24 17:42:39 -0700299 captureBuf->unlock();
Valerie Hauc5011f92019-10-11 09:52:07 -0700300 }
301
chaviw8ffc7b82020-08-18 11:25:37 -0700302 static status_t captureDisplay(DisplayCaptureArgs& captureArgs,
303 ScreenCaptureResults& captureResults) {
Huihong Luo9e84f332021-12-16 14:33:46 -0800304 const auto sf = ComposerServiceAIDL::getComposerService();
chaviw8ffc7b82020-08-18 11:25:37 -0700305 SurfaceComposerClient::Transaction().apply(true);
306
307 const sp<SyncScreenCaptureListener> captureListener = new SyncScreenCaptureListener();
Huihong Luo9e84f332021-12-16 14:33:46 -0800308 binder::Status status = sf->captureDisplay(captureArgs, captureListener);
Huihong Luo3bdef862022-03-03 11:57:19 -0800309 status_t err = gui::aidl_utils::statusTFromBinderStatus(status);
310 if (err != NO_ERROR) {
311 return err;
chaviw8ffc7b82020-08-18 11:25:37 -0700312 }
313 captureResults = captureListener->waitForResults();
314 return captureResults.result;
315 }
316
Vishnu Nair277142c2021-01-05 18:35:29 -0800317 void queueBuffer(sp<IGraphicBufferProducer> igbp, uint8_t r, uint8_t g, uint8_t b,
318 nsecs_t presentTimeDelay) {
319 int slot;
320 sp<Fence> fence;
321 sp<GraphicBuffer> buf;
322 auto ret = igbp->dequeueBuffer(&slot, &fence, mDisplayWidth, mDisplayHeight,
323 PIXEL_FORMAT_RGBA_8888, GRALLOC_USAGE_SW_WRITE_OFTEN,
324 nullptr, nullptr);
chaviw0acd33a2021-11-02 11:55:37 -0500325 ASSERT_TRUE(ret == IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION || ret == NO_ERROR);
Vishnu Nair277142c2021-01-05 18:35:29 -0800326 ASSERT_EQ(OK, igbp->requestBuffer(slot, &buf));
327
328 uint32_t* bufData;
329 buf->lock(static_cast<uint32_t>(GraphicBuffer::USAGE_SW_WRITE_OFTEN),
330 reinterpret_cast<void**>(&bufData));
331 fillBuffer(bufData, Rect(buf->getWidth(), buf->getHeight() / 2), buf->getStride(), r, g, b);
332 buf->unlock();
333
334 IGraphicBufferProducer::QueueBufferOutput qbOutput;
335 nsecs_t timestampNanos = systemTime() + presentTimeDelay;
336 IGraphicBufferProducer::QueueBufferInput input(timestampNanos, false, HAL_DATASPACE_UNKNOWN,
337 Rect(mDisplayWidth, mDisplayHeight / 2),
338 NATIVE_WINDOW_SCALING_MODE_FREEZE, 0,
339 Fence::NO_FENCE);
340 igbp->queueBuffer(slot, input, &qbOutput);
341 }
342
Valerie Hauc5011f92019-10-11 09:52:07 -0700343 sp<SurfaceComposerClient> mClient;
Valerie Hauda3446e2019-10-14 15:49:22 -0700344 sp<ISurfaceComposer> mComposer;
345
346 sp<IBinder> mDisplayToken;
347
Valerie Hauc5011f92019-10-11 09:52:07 -0700348 sp<SurfaceControl> mSurfaceControl;
Valerie Hauda3446e2019-10-14 15:49:22 -0700349
350 uint32_t mDisplayWidth;
351 uint32_t mDisplayHeight;
chaviwd2432892020-07-24 17:42:39 -0700352
353 DisplayCaptureArgs mCaptureArgs;
354 ScreenCaptureResults mCaptureResults;
chaviwd7deef72021-10-06 11:53:40 -0500355 sp<CountProducerListener> mProducerListener;
Valerie Hauc5011f92019-10-11 09:52:07 -0700356};
357
358TEST_F(BLASTBufferQueueTest, CreateBLASTBufferQueue) {
359 // create BLASTBufferQueue adapter associated with this surface
Valerie Hauda3446e2019-10-14 15:49:22 -0700360 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
Valerie Hauc5011f92019-10-11 09:52:07 -0700361 ASSERT_EQ(mSurfaceControl, adapter.getSurfaceControl());
Valerie Hauda3446e2019-10-14 15:49:22 -0700362 ASSERT_EQ(mDisplayWidth, adapter.getWidth());
363 ASSERT_EQ(mDisplayHeight, adapter.getHeight());
Tianhao Yao4861b102022-02-03 20:18:35 +0000364 ASSERT_EQ(nullptr, adapter.getTransactionReadyCallback());
Valerie Hauc5011f92019-10-11 09:52:07 -0700365}
366
367TEST_F(BLASTBufferQueueTest, Update) {
Valerie Hauda3446e2019-10-14 15:49:22 -0700368 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
Valerie Hauc5011f92019-10-11 09:52:07 -0700369 sp<SurfaceControl> updateSurface =
Valerie Hauda3446e2019-10-14 15:49:22 -0700370 mClient->createSurface(String8("UpdateTest"), mDisplayWidth / 2, mDisplayHeight / 2,
371 PIXEL_FORMAT_RGBA_8888);
372 adapter.update(updateSurface, mDisplayWidth / 2, mDisplayHeight / 2);
Valerie Hauc5011f92019-10-11 09:52:07 -0700373 ASSERT_EQ(updateSurface, adapter.getSurfaceControl());
Vishnu Nairea0de002020-11-17 17:42:37 -0800374 sp<IGraphicBufferProducer> igbProducer;
375 setUpProducer(adapter, igbProducer);
376
377 int32_t width;
378 igbProducer->query(NATIVE_WINDOW_WIDTH, &width);
379 ASSERT_EQ(mDisplayWidth / 2, width);
380 int32_t height;
381 igbProducer->query(NATIVE_WINDOW_HEIGHT, &height);
382 ASSERT_EQ(mDisplayHeight / 2, height);
Valerie Hauc5011f92019-10-11 09:52:07 -0700383}
384
Tianhao Yao4861b102022-02-03 20:18:35 +0000385TEST_F(BLASTBufferQueueTest, SyncNextTransaction) {
Valerie Hauda3446e2019-10-14 15:49:22 -0700386 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
Tianhao Yao4861b102022-02-03 20:18:35 +0000387 ASSERT_EQ(nullptr, adapter.getTransactionReadyCallback());
388 auto callback = [](Transaction*) {};
389 adapter.syncNextTransaction(callback);
390 ASSERT_NE(nullptr, adapter.getTransactionReadyCallback());
Valerie Hauc5011f92019-10-11 09:52:07 -0700391}
Valerie Hauda3446e2019-10-14 15:49:22 -0700392
Valerie Haubf29e042020-02-06 11:40:38 -0800393TEST_F(BLASTBufferQueueTest, DISABLED_onFrameAvailable_ApplyDesiredPresentTime) {
Valerie Hau181abd32020-01-27 14:18:28 -0800394 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
395 sp<IGraphicBufferProducer> igbProducer;
396 setUpProducer(adapter, igbProducer);
397
398 int slot;
399 sp<Fence> fence;
400 sp<GraphicBuffer> buf;
401 auto ret = igbProducer->dequeueBuffer(&slot, &fence, mDisplayWidth, mDisplayHeight,
402 PIXEL_FORMAT_RGBA_8888, GRALLOC_USAGE_SW_WRITE_OFTEN,
403 nullptr, nullptr);
404 ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, ret);
405 ASSERT_EQ(OK, igbProducer->requestBuffer(slot, &buf));
406
407 nsecs_t desiredPresentTime = systemTime() + nsecs_t(5 * 1e8);
408 IGraphicBufferProducer::QueueBufferOutput qbOutput;
Vishnu Nair1506b182021-02-22 14:35:15 -0800409 IGraphicBufferProducer::QueueBufferInput input(desiredPresentTime, true /* autotimestamp */,
410 HAL_DATASPACE_UNKNOWN,
Valerie Hau181abd32020-01-27 14:18:28 -0800411 Rect(mDisplayWidth, mDisplayHeight),
412 NATIVE_WINDOW_SCALING_MODE_FREEZE, 0,
413 Fence::NO_FENCE);
414 igbProducer->queueBuffer(slot, input, &qbOutput);
415 ASSERT_NE(ui::Transform::ROT_INVALID, qbOutput.transformHint);
416
417 adapter.waitForCallbacks();
418 ASSERT_GE(systemTime(), desiredPresentTime);
419}
420
Valerie Hauda3446e2019-10-14 15:49:22 -0700421TEST_F(BLASTBufferQueueTest, onFrameAvailable_Apply) {
422 uint8_t r = 255;
423 uint8_t g = 0;
424 uint8_t b = 0;
425
426 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
Valerie Haud3b90d22019-11-06 09:37:31 -0800427 sp<IGraphicBufferProducer> igbProducer;
428 setUpProducer(adapter, igbProducer);
Valerie Hauda3446e2019-10-14 15:49:22 -0700429
430 int slot;
431 sp<Fence> fence;
432 sp<GraphicBuffer> buf;
433 auto ret = igbProducer->dequeueBuffer(&slot, &fence, mDisplayWidth, mDisplayHeight,
434 PIXEL_FORMAT_RGBA_8888, GRALLOC_USAGE_SW_WRITE_OFTEN,
435 nullptr, nullptr);
436 ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, ret);
437 ASSERT_EQ(OK, igbProducer->requestBuffer(slot, &buf));
438
439 uint32_t* bufData;
440 buf->lock(static_cast<uint32_t>(GraphicBuffer::USAGE_SW_WRITE_OFTEN),
441 reinterpret_cast<void**>(&bufData));
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800442 fillBuffer(bufData, Rect(buf->getWidth(), buf->getHeight()), buf->getStride(), r, g, b);
Valerie Hauda3446e2019-10-14 15:49:22 -0700443 buf->unlock();
444
Valerie Haud3b90d22019-11-06 09:37:31 -0800445 IGraphicBufferProducer::QueueBufferOutput qbOutput;
Vishnu Nair1506b182021-02-22 14:35:15 -0800446 IGraphicBufferProducer::QueueBufferInput input(systemTime(), true /* autotimestamp */,
447 HAL_DATASPACE_UNKNOWN,
Valerie Hauda3446e2019-10-14 15:49:22 -0700448 Rect(mDisplayWidth, mDisplayHeight),
449 NATIVE_WINDOW_SCALING_MODE_FREEZE, 0,
450 Fence::NO_FENCE);
451 igbProducer->queueBuffer(slot, input, &qbOutput);
Dominik Laskowski718f9602019-11-09 20:01:35 -0800452 ASSERT_NE(ui::Transform::ROT_INVALID, qbOutput.transformHint);
Valerie Hauda3446e2019-10-14 15:49:22 -0700453
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800454 adapter.waitForCallbacks();
Valerie Hauda3446e2019-10-14 15:49:22 -0700455
456 // capture screen and verify that it is red
chaviw8ffc7b82020-08-18 11:25:37 -0700457 ASSERT_EQ(NO_ERROR, captureDisplay(mCaptureArgs, mCaptureResults));
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800458 ASSERT_NO_FATAL_FAILURE(
459 checkScreenCapture(r, g, b, {0, 0, (int32_t)mDisplayWidth, (int32_t)mDisplayHeight}));
Valerie Hauda3446e2019-10-14 15:49:22 -0700460}
Valerie Haud3b90d22019-11-06 09:37:31 -0800461
462TEST_F(BLASTBufferQueueTest, TripleBuffering) {
463 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
464 sp<IGraphicBufferProducer> igbProducer;
465 setUpProducer(adapter, igbProducer);
466
467 std::vector<std::pair<int, sp<Fence>>> allocated;
Ady Abraham0bde6b52021-05-18 13:57:02 -0700468 int minUndequeuedBuffers = 0;
469 ASSERT_EQ(OK, igbProducer->query(NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS, &minUndequeuedBuffers));
470 const auto bufferCount = minUndequeuedBuffers + 2;
471
472 for (int i = 0; i < bufferCount; i++) {
Valerie Haud3b90d22019-11-06 09:37:31 -0800473 int slot;
474 sp<Fence> fence;
475 sp<GraphicBuffer> buf;
476 auto ret = igbProducer->dequeueBuffer(&slot, &fence, mDisplayWidth, mDisplayHeight,
477 PIXEL_FORMAT_RGBA_8888, GRALLOC_USAGE_SW_WRITE_OFTEN,
478 nullptr, nullptr);
479 ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, ret);
480 ASSERT_EQ(OK, igbProducer->requestBuffer(slot, &buf));
481 allocated.push_back({slot, fence});
482 }
483 for (int i = 0; i < allocated.size(); i++) {
484 igbProducer->cancelBuffer(allocated[i].first, allocated[i].second);
485 }
486
Valerie Haua32c5522019-12-09 10:11:08 -0800487 for (int i = 0; i < 100; i++) {
Valerie Haud3b90d22019-11-06 09:37:31 -0800488 int slot;
489 sp<Fence> fence;
490 sp<GraphicBuffer> buf;
491 auto ret = igbProducer->dequeueBuffer(&slot, &fence, mDisplayWidth, mDisplayHeight,
492 PIXEL_FORMAT_RGBA_8888, GRALLOC_USAGE_SW_WRITE_OFTEN,
493 nullptr, nullptr);
494 ASSERT_EQ(NO_ERROR, ret);
495 IGraphicBufferProducer::QueueBufferOutput qbOutput;
Vishnu Nair1506b182021-02-22 14:35:15 -0800496 IGraphicBufferProducer::QueueBufferInput input(systemTime(), true /* autotimestamp */,
497 HAL_DATASPACE_UNKNOWN,
Valerie Haud3b90d22019-11-06 09:37:31 -0800498 Rect(mDisplayWidth, mDisplayHeight),
499 NATIVE_WINDOW_SCALING_MODE_FREEZE, 0,
500 Fence::NO_FENCE);
501 igbProducer->queueBuffer(slot, input, &qbOutput);
502 }
503 adapter.waitForCallbacks();
504}
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800505
506TEST_F(BLASTBufferQueueTest, SetCrop_Item) {
507 uint8_t r = 255;
508 uint8_t g = 0;
509 uint8_t b = 0;
510
511 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
512 sp<IGraphicBufferProducer> igbProducer;
513 setUpProducer(adapter, igbProducer);
514 int slot;
515 sp<Fence> fence;
516 sp<GraphicBuffer> buf;
517 auto ret = igbProducer->dequeueBuffer(&slot, &fence, mDisplayWidth, mDisplayHeight,
518 PIXEL_FORMAT_RGBA_8888, GRALLOC_USAGE_SW_WRITE_OFTEN,
519 nullptr, nullptr);
520 ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, ret);
521 ASSERT_EQ(OK, igbProducer->requestBuffer(slot, &buf));
522
523 uint32_t* bufData;
524 buf->lock(static_cast<uint32_t>(GraphicBuffer::USAGE_SW_WRITE_OFTEN),
525 reinterpret_cast<void**>(&bufData));
526 fillBuffer(bufData, Rect(buf->getWidth(), buf->getHeight() / 2), buf->getStride(), r, g, b);
527 buf->unlock();
528
529 IGraphicBufferProducer::QueueBufferOutput qbOutput;
Vishnu Nair1506b182021-02-22 14:35:15 -0800530 IGraphicBufferProducer::QueueBufferInput input(systemTime(), true /* autotimestamp */,
531 HAL_DATASPACE_UNKNOWN,
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800532 Rect(mDisplayWidth, mDisplayHeight / 2),
533 NATIVE_WINDOW_SCALING_MODE_FREEZE, 0,
534 Fence::NO_FENCE);
535 igbProducer->queueBuffer(slot, input, &qbOutput);
Dominik Laskowski718f9602019-11-09 20:01:35 -0800536 ASSERT_NE(ui::Transform::ROT_INVALID, qbOutput.transformHint);
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800537
538 adapter.waitForCallbacks();
539 // capture screen and verify that it is red
chaviw8ffc7b82020-08-18 11:25:37 -0700540 ASSERT_EQ(NO_ERROR, captureDisplay(mCaptureArgs, mCaptureResults));
chaviwd2432892020-07-24 17:42:39 -0700541
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800542 ASSERT_NO_FATAL_FAILURE(
Chavi Weingartena5aedbd2021-04-09 13:37:33 +0000543 checkScreenCapture(r, g, b,
544 {0, 0, (int32_t)mDisplayWidth, (int32_t)mDisplayHeight / 2}));
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800545}
546
547TEST_F(BLASTBufferQueueTest, SetCrop_ScalingModeScaleCrop) {
548 uint8_t r = 255;
549 uint8_t g = 0;
550 uint8_t b = 0;
551
552 int32_t bufferSideLength =
553 (mDisplayWidth < mDisplayHeight) ? mDisplayWidth / 2 : mDisplayHeight / 2;
554 int32_t finalCropSideLength = bufferSideLength / 2;
555
556 auto bg = mClient->createSurface(String8("BGTest"), 0, 0, PIXEL_FORMAT_RGBA_8888,
Vishnu Nairfa247b12020-02-11 08:58:26 -0800557 ISurfaceComposerClient::eFXSurfaceEffect);
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800558 ASSERT_NE(nullptr, bg.get());
559 Transaction t;
Dominik Laskowski29fa1462021-04-27 15:51:50 -0700560 t.setLayerStack(bg, ui::DEFAULT_LAYER_STACK)
chaviw25714502021-02-11 10:01:08 -0800561 .setCrop(bg, Rect(0, 0, mDisplayWidth, mDisplayHeight))
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800562 .setColor(bg, half3{0, 0, 0})
563 .setLayer(bg, 0)
564 .apply();
565
566 BLASTBufferQueueHelper adapter(mSurfaceControl, bufferSideLength, bufferSideLength);
567 sp<IGraphicBufferProducer> igbProducer;
568 setUpProducer(adapter, igbProducer);
569 int slot;
570 sp<Fence> fence;
571 sp<GraphicBuffer> buf;
572 auto ret = igbProducer->dequeueBuffer(&slot, &fence, bufferSideLength, bufferSideLength,
573 PIXEL_FORMAT_RGBA_8888, GRALLOC_USAGE_SW_WRITE_OFTEN,
574 nullptr, nullptr);
575 ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, ret);
576 ASSERT_EQ(OK, igbProducer->requestBuffer(slot, &buf));
577
578 uint32_t* bufData;
579 buf->lock(static_cast<uint32_t>(GraphicBuffer::USAGE_SW_WRITE_OFTEN),
580 reinterpret_cast<void**>(&bufData));
581 fillBuffer(bufData, Rect(buf->getWidth(), buf->getHeight()), buf->getStride(), 0, 0, 0);
582 fillBuffer(bufData,
583 Rect(finalCropSideLength / 2, 0, buf->getWidth() - finalCropSideLength / 2,
584 buf->getHeight()),
585 buf->getStride(), r, g, b);
586 buf->unlock();
587
588 IGraphicBufferProducer::QueueBufferOutput qbOutput;
Vishnu Nair1506b182021-02-22 14:35:15 -0800589 IGraphicBufferProducer::QueueBufferInput input(systemTime(), true /* autotimestamp */,
590 HAL_DATASPACE_UNKNOWN,
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800591 Rect(bufferSideLength, finalCropSideLength),
592 NATIVE_WINDOW_SCALING_MODE_SCALE_CROP, 0,
593 Fence::NO_FENCE);
594 igbProducer->queueBuffer(slot, input, &qbOutput);
Dominik Laskowski718f9602019-11-09 20:01:35 -0800595 ASSERT_NE(ui::Transform::ROT_INVALID, qbOutput.transformHint);
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800596
597 adapter.waitForCallbacks();
598 // capture screen and verify that it is red
chaviw8ffc7b82020-08-18 11:25:37 -0700599 ASSERT_EQ(NO_ERROR, captureDisplay(mCaptureArgs, mCaptureResults));
Vishnu Nair5cc9ac02021-04-19 13:23:38 -0700600 ASSERT_NO_FATAL_FAILURE(checkScreenCapture(r, g, b,
601 {10, 10, (int32_t)bufferSideLength - 10,
602 (int32_t)bufferSideLength - 10}));
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800603 ASSERT_NO_FATAL_FAILURE(
Vishnu Nair5cc9ac02021-04-19 13:23:38 -0700604 checkScreenCapture(0, 0, 0,
605 {0, 0, (int32_t)bufferSideLength, (int32_t)bufferSideLength},
606 /*border*/ 0, /*outsideRegion*/ true));
607}
608
609TEST_F(BLASTBufferQueueTest, ScaleCroppedBufferToBufferSize) {
610 // add black background
611 auto bg = mClient->createSurface(String8("BGTest"), 0, 0, PIXEL_FORMAT_RGBA_8888,
612 ISurfaceComposerClient::eFXSurfaceEffect);
613 ASSERT_NE(nullptr, bg.get());
614 Transaction t;
Dominik Laskowski29fa1462021-04-27 15:51:50 -0700615 t.setLayerStack(bg, ui::DEFAULT_LAYER_STACK)
Vishnu Nair5cc9ac02021-04-19 13:23:38 -0700616 .setCrop(bg, Rect(0, 0, mDisplayWidth, mDisplayHeight))
617 .setColor(bg, half3{0, 0, 0})
618 .setLayer(bg, 0)
619 .apply();
620
621 Rect windowSize(1000, 1000);
622 Rect bufferSize(windowSize);
623 Rect bufferCrop(200, 200, 700, 700);
624
625 BLASTBufferQueueHelper adapter(mSurfaceControl, windowSize.getWidth(), windowSize.getHeight());
626 sp<IGraphicBufferProducer> igbProducer;
627 setUpProducer(adapter, igbProducer);
628 int slot;
629 sp<Fence> fence;
630 sp<GraphicBuffer> buf;
631 auto ret = igbProducer->dequeueBuffer(&slot, &fence, bufferSize.getWidth(),
632 bufferSize.getHeight(), PIXEL_FORMAT_RGBA_8888,
633 GRALLOC_USAGE_SW_WRITE_OFTEN, nullptr, nullptr);
634 ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, ret);
635 ASSERT_EQ(OK, igbProducer->requestBuffer(slot, &buf));
636
637 uint32_t* bufData;
638 buf->lock(static_cast<uint32_t>(GraphicBuffer::USAGE_SW_WRITE_OFTEN),
639 reinterpret_cast<void**>(&bufData));
640 // fill buffer with grey
641 fillBuffer(bufData, bufferSize, buf->getStride(), 127, 127, 127);
642
643 // fill crop area with different colors so we can verify the cropped region has been scaled
644 // correctly.
645 fillBuffer(bufData, Rect(200, 200, 450, 450), buf->getStride(), /* rgb */ 255, 0, 0);
646 fillBuffer(bufData, Rect(200, 451, 450, 700), buf->getStride(), /* rgb */ 0, 255, 0);
647 fillBuffer(bufData, Rect(451, 200, 700, 450), buf->getStride(), /* rgb */ 0, 0, 255);
648 fillBuffer(bufData, Rect(451, 451, 700, 700), buf->getStride(), /* rgb */ 255, 0, 0);
649 buf->unlock();
650
651 IGraphicBufferProducer::QueueBufferOutput qbOutput;
652 IGraphicBufferProducer::QueueBufferInput input(systemTime(), true /* autotimestamp */,
653 HAL_DATASPACE_UNKNOWN,
654 bufferCrop /* Rect::INVALID_RECT */,
655 NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW, 0,
656 Fence::NO_FENCE);
657 igbProducer->queueBuffer(slot, input, &qbOutput);
658 ASSERT_NE(ui::Transform::ROT_INVALID, qbOutput.transformHint);
659
660 adapter.waitForCallbacks();
661
662 ASSERT_EQ(NO_ERROR, captureDisplay(mCaptureArgs, mCaptureResults));
663
664 // Verify cropped region is scaled correctly.
665 ASSERT_NO_FATAL_FAILURE(checkScreenCapture(255, 0, 0, {10, 10, 490, 490}));
666 ASSERT_NO_FATAL_FAILURE(checkScreenCapture(0, 255, 0, {10, 510, 490, 990}));
667 ASSERT_NO_FATAL_FAILURE(checkScreenCapture(0, 0, 255, {510, 10, 990, 490}));
668 ASSERT_NO_FATAL_FAILURE(checkScreenCapture(255, 0, 0, {510, 510, 990, 990}));
669 // Verify outside region is black.
670 ASSERT_NO_FATAL_FAILURE(checkScreenCapture(0, 0, 0,
671 {0, 0, (int32_t)windowSize.getWidth(),
672 (int32_t)windowSize.getHeight()},
673 /*border*/ 0, /*outsideRegion*/ true));
674}
675
676TEST_F(BLASTBufferQueueTest, ScaleCroppedBufferToWindowSize) {
677 // add black background
678 auto bg = mClient->createSurface(String8("BGTest"), 0, 0, PIXEL_FORMAT_RGBA_8888,
679 ISurfaceComposerClient::eFXSurfaceEffect);
680 ASSERT_NE(nullptr, bg.get());
681 Transaction t;
Dominik Laskowski29fa1462021-04-27 15:51:50 -0700682 t.setLayerStack(bg, ui::DEFAULT_LAYER_STACK)
Vishnu Nair5cc9ac02021-04-19 13:23:38 -0700683 .setCrop(bg, Rect(0, 0, mDisplayWidth, mDisplayHeight))
684 .setColor(bg, half3{0, 0, 0})
685 .setLayer(bg, 0)
686 .apply();
687
688 Rect windowSize(1000, 1000);
689 Rect bufferSize(500, 500);
690 Rect bufferCrop(100, 100, 350, 350);
691
692 BLASTBufferQueueHelper adapter(mSurfaceControl, windowSize.getWidth(), windowSize.getHeight());
693 sp<IGraphicBufferProducer> igbProducer;
694 setUpProducer(adapter, igbProducer);
695 int slot;
696 sp<Fence> fence;
697 sp<GraphicBuffer> buf;
698 auto ret = igbProducer->dequeueBuffer(&slot, &fence, bufferSize.getWidth(),
699 bufferSize.getHeight(), PIXEL_FORMAT_RGBA_8888,
700 GRALLOC_USAGE_SW_WRITE_OFTEN, nullptr, nullptr);
701 ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, ret);
702 ASSERT_EQ(OK, igbProducer->requestBuffer(slot, &buf));
703
704 uint32_t* bufData;
705 buf->lock(static_cast<uint32_t>(GraphicBuffer::USAGE_SW_WRITE_OFTEN),
706 reinterpret_cast<void**>(&bufData));
707 // fill buffer with grey
708 fillBuffer(bufData, bufferSize, buf->getStride(), 127, 127, 127);
709
710 // fill crop area with different colors so we can verify the cropped region has been scaled
711 // correctly.
712 fillBuffer(bufData, Rect(100, 100, 225, 225), buf->getStride(), /* rgb */ 255, 0, 0);
713 fillBuffer(bufData, Rect(100, 226, 225, 350), buf->getStride(), /* rgb */ 0, 255, 0);
714 fillBuffer(bufData, Rect(226, 100, 350, 225), buf->getStride(), /* rgb */ 0, 0, 255);
715 fillBuffer(bufData, Rect(226, 226, 350, 350), buf->getStride(), /* rgb */ 255, 0, 0);
716 buf->unlock();
717
718 IGraphicBufferProducer::QueueBufferOutput qbOutput;
719 IGraphicBufferProducer::QueueBufferInput input(systemTime(), true /* autotimestamp */,
720 HAL_DATASPACE_UNKNOWN,
721 bufferCrop /* Rect::INVALID_RECT */,
722 NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW, 0,
723 Fence::NO_FENCE);
724 igbProducer->queueBuffer(slot, input, &qbOutput);
725 ASSERT_NE(ui::Transform::ROT_INVALID, qbOutput.transformHint);
726
727 adapter.waitForCallbacks();
728
729 ASSERT_EQ(NO_ERROR, captureDisplay(mCaptureArgs, mCaptureResults));
730 // Verify cropped region is scaled correctly.
731 ASSERT_NO_FATAL_FAILURE(checkScreenCapture(255, 0, 0, {10, 10, 490, 490}));
732 ASSERT_NO_FATAL_FAILURE(checkScreenCapture(0, 255, 0, {10, 510, 490, 990}));
733 ASSERT_NO_FATAL_FAILURE(checkScreenCapture(0, 0, 255, {510, 10, 990, 490}));
734 ASSERT_NO_FATAL_FAILURE(checkScreenCapture(255, 0, 0, {510, 510, 990, 990}));
735 // Verify outside region is black.
736 ASSERT_NO_FATAL_FAILURE(checkScreenCapture(0, 0, 0,
737 {0, 0, (int32_t)windowSize.getWidth(),
738 (int32_t)windowSize.getHeight()},
739 /*border*/ 0, /*outsideRegion*/ true));
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800740}
741
Vishnu Nair932f6ae2021-09-29 17:33:10 -0700742// b/196339769 verify we can can update the requested size while the in FREEZE scaling mode and
743// scale the buffer properly when the mode changes to SCALE_TO_WINDOW
744TEST_F(BLASTBufferQueueTest, ScalingModeChanges) {
745 uint8_t r = 255;
746 uint8_t g = 0;
747 uint8_t b = 0;
748
749 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight / 4);
750 sp<IGraphicBufferProducer> igbProducer;
751 setUpProducer(adapter, igbProducer);
752 {
753 int slot;
754 sp<Fence> fence;
755 sp<GraphicBuffer> buf;
756 auto ret = igbProducer->dequeueBuffer(&slot, &fence, mDisplayWidth, mDisplayHeight / 4,
757 PIXEL_FORMAT_RGBA_8888, GRALLOC_USAGE_SW_WRITE_OFTEN,
758 nullptr, nullptr);
759 ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, ret);
760 ASSERT_EQ(OK, igbProducer->requestBuffer(slot, &buf));
761
762 uint32_t* bufData;
763 buf->lock(static_cast<uint32_t>(GraphicBuffer::USAGE_SW_WRITE_OFTEN),
764 reinterpret_cast<void**>(&bufData));
765 fillBuffer(bufData, Rect(buf->getWidth(), buf->getHeight()), buf->getStride(), r, g, b);
766 buf->unlock();
767
768 IGraphicBufferProducer::QueueBufferOutput qbOutput;
769 IGraphicBufferProducer::QueueBufferInput input(systemTime(), true /* autotimestamp */,
770 HAL_DATASPACE_UNKNOWN, {},
771 NATIVE_WINDOW_SCALING_MODE_FREEZE, 0,
772 Fence::NO_FENCE);
773 igbProducer->queueBuffer(slot, input, &qbOutput);
774 adapter.waitForCallbacks();
775 }
776 // capture screen and verify that it is red
777 ASSERT_EQ(NO_ERROR, captureDisplay(mCaptureArgs, mCaptureResults));
778
779 ASSERT_NO_FATAL_FAILURE(
780 checkScreenCapture(r, g, b,
781 {0, 0, (int32_t)mDisplayWidth, (int32_t)mDisplayHeight / 4}));
782
783 // update the size to half the display and dequeue a buffer quarter of the display.
784 adapter.update(mSurfaceControl, mDisplayWidth, mDisplayHeight / 2);
785
786 {
787 int slot;
788 sp<Fence> fence;
789 sp<GraphicBuffer> buf;
790 auto ret = igbProducer->dequeueBuffer(&slot, &fence, mDisplayWidth, mDisplayHeight / 8,
791 PIXEL_FORMAT_RGBA_8888, GRALLOC_USAGE_SW_WRITE_OFTEN,
792 nullptr, nullptr);
793 ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, ret);
794 ASSERT_EQ(OK, igbProducer->requestBuffer(slot, &buf));
795
796 uint32_t* bufData;
797 buf->lock(static_cast<uint32_t>(GraphicBuffer::USAGE_SW_WRITE_OFTEN),
798 reinterpret_cast<void**>(&bufData));
799 g = 255;
800 fillBuffer(bufData, Rect(buf->getWidth(), buf->getHeight()), buf->getStride(), r, g, b);
801 buf->unlock();
802
803 IGraphicBufferProducer::QueueBufferOutput qbOutput;
804 IGraphicBufferProducer::QueueBufferInput input(systemTime(), true /* autotimestamp */,
805 HAL_DATASPACE_UNKNOWN, {},
806 NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW,
807 0, Fence::NO_FENCE);
808 igbProducer->queueBuffer(slot, input, &qbOutput);
809 adapter.waitForCallbacks();
810 }
811 // capture screen and verify that it is red
812 ASSERT_EQ(NO_ERROR, captureDisplay(mCaptureArgs, mCaptureResults));
813 // verify we still scale the buffer to the new size (half the screen height)
814 ASSERT_NO_FATAL_FAILURE(
815 checkScreenCapture(r, g, b,
816 {0, 0, (int32_t)mDisplayWidth, (int32_t)mDisplayHeight / 2}));
817}
818
chaviwd7deef72021-10-06 11:53:40 -0500819TEST_F(BLASTBufferQueueTest, SyncThenNoSync) {
820 uint8_t r = 255;
821 uint8_t g = 0;
822 uint8_t b = 0;
823
824 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
825
826 sp<IGraphicBufferProducer> igbProducer;
827 setUpProducer(adapter, igbProducer);
828
chaviwa1c4c822021-11-10 18:11:58 -0600829 Transaction sync;
Tianhao Yao4861b102022-02-03 20:18:35 +0000830 adapter.setSyncTransaction(sync);
chaviwd7deef72021-10-06 11:53:40 -0500831 queueBuffer(igbProducer, 0, 255, 0, 0);
832
833 // queue non sync buffer, so this one should get blocked
834 // Add a present delay to allow the first screenshot to get taken.
835 nsecs_t presentTimeDelay = std::chrono::nanoseconds(500ms).count();
836 queueBuffer(igbProducer, r, g, b, presentTimeDelay);
837
838 CallbackHelper transactionCallback;
chaviwa1c4c822021-11-10 18:11:58 -0600839 sync.addTransactionCompletedCallback(transactionCallback.function,
chaviwd7deef72021-10-06 11:53:40 -0500840 transactionCallback.getContext())
841 .apply();
842
843 CallbackData callbackData;
844 transactionCallback.getCallbackData(&callbackData);
845
chaviw0acd33a2021-11-02 11:55:37 -0500846 // capture screen and verify that it is green
chaviwd7deef72021-10-06 11:53:40 -0500847 ASSERT_EQ(NO_ERROR, captureDisplay(mCaptureArgs, mCaptureResults));
848 ASSERT_NO_FATAL_FAILURE(
849 checkScreenCapture(0, 255, 0, {0, 0, (int32_t)mDisplayWidth, (int32_t)mDisplayHeight}));
850
851 mProducerListener->waitOnNumberReleased(1);
852 ASSERT_EQ(NO_ERROR, captureDisplay(mCaptureArgs, mCaptureResults));
853 ASSERT_NO_FATAL_FAILURE(
854 checkScreenCapture(r, g, b, {0, 0, (int32_t)mDisplayWidth, (int32_t)mDisplayHeight}));
855}
856
857TEST_F(BLASTBufferQueueTest, MultipleSyncTransactions) {
858 uint8_t r = 255;
859 uint8_t g = 0;
860 uint8_t b = 0;
861
862 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
863
864 sp<IGraphicBufferProducer> igbProducer;
865 setUpProducer(adapter, igbProducer);
866
867 Transaction mainTransaction;
868
chaviwa1c4c822021-11-10 18:11:58 -0600869 Transaction sync;
Tianhao Yao4861b102022-02-03 20:18:35 +0000870 adapter.setSyncTransaction(sync);
chaviwd7deef72021-10-06 11:53:40 -0500871 queueBuffer(igbProducer, 0, 255, 0, 0);
872
chaviwa1c4c822021-11-10 18:11:58 -0600873 mainTransaction.merge(std::move(sync));
chaviwd7deef72021-10-06 11:53:40 -0500874
Tianhao Yao4861b102022-02-03 20:18:35 +0000875 adapter.setSyncTransaction(sync);
chaviwd7deef72021-10-06 11:53:40 -0500876 queueBuffer(igbProducer, r, g, b, 0);
877
chaviwa1c4c822021-11-10 18:11:58 -0600878 mainTransaction.merge(std::move(sync));
chaviwd7deef72021-10-06 11:53:40 -0500879 // Expect 1 buffer to be released even before sending to SurfaceFlinger
880 mProducerListener->waitOnNumberReleased(1);
881
882 CallbackHelper transactionCallback;
883 mainTransaction
884 .addTransactionCompletedCallback(transactionCallback.function,
885 transactionCallback.getContext())
886 .apply();
887
888 CallbackData callbackData;
889 transactionCallback.getCallbackData(&callbackData);
890
891 // capture screen and verify that it is red
892 ASSERT_EQ(NO_ERROR, captureDisplay(mCaptureArgs, mCaptureResults));
893 ASSERT_NO_FATAL_FAILURE(
894 checkScreenCapture(r, g, b, {0, 0, (int32_t)mDisplayWidth, (int32_t)mDisplayHeight}));
895}
896
897TEST_F(BLASTBufferQueueTest, MultipleSyncTransactionWithNonSync) {
898 uint8_t r = 255;
899 uint8_t g = 0;
900 uint8_t b = 0;
901
902 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
903
904 sp<IGraphicBufferProducer> igbProducer;
905 setUpProducer(adapter, igbProducer);
906
907 Transaction mainTransaction;
908
chaviwa1c4c822021-11-10 18:11:58 -0600909 Transaction sync;
chaviwd7deef72021-10-06 11:53:40 -0500910 // queue a sync transaction
Tianhao Yao4861b102022-02-03 20:18:35 +0000911 adapter.setSyncTransaction(sync);
chaviwd7deef72021-10-06 11:53:40 -0500912 queueBuffer(igbProducer, 0, 255, 0, 0);
913
chaviwa1c4c822021-11-10 18:11:58 -0600914 mainTransaction.merge(std::move(sync));
chaviwd7deef72021-10-06 11:53:40 -0500915
chaviwa1c4c822021-11-10 18:11:58 -0600916 // queue another buffer without setting sync transaction
chaviwd7deef72021-10-06 11:53:40 -0500917 queueBuffer(igbProducer, 0, 0, 255, 0);
918
919 // queue another sync transaction
Tianhao Yao4861b102022-02-03 20:18:35 +0000920 adapter.setSyncTransaction(sync);
chaviwd7deef72021-10-06 11:53:40 -0500921 queueBuffer(igbProducer, r, g, b, 0);
922 // Expect 1 buffer to be released because the non sync transaction should merge
923 // with the sync
924 mProducerListener->waitOnNumberReleased(1);
925
chaviwa1c4c822021-11-10 18:11:58 -0600926 mainTransaction.merge(std::move(sync));
chaviwd7deef72021-10-06 11:53:40 -0500927 // Expect 2 buffers to be released due to merging the two syncs.
928 mProducerListener->waitOnNumberReleased(2);
929
930 CallbackHelper transactionCallback;
931 mainTransaction
932 .addTransactionCompletedCallback(transactionCallback.function,
933 transactionCallback.getContext())
934 .apply();
935
936 CallbackData callbackData;
937 transactionCallback.getCallbackData(&callbackData);
938
939 // capture screen and verify that it is red
940 ASSERT_EQ(NO_ERROR, captureDisplay(mCaptureArgs, mCaptureResults));
941 ASSERT_NO_FATAL_FAILURE(
942 checkScreenCapture(r, g, b, {0, 0, (int32_t)mDisplayWidth, (int32_t)mDisplayHeight}));
943}
944
945TEST_F(BLASTBufferQueueTest, MultipleSyncRunOutOfBuffers) {
946 uint8_t r = 255;
947 uint8_t g = 0;
948 uint8_t b = 0;
949
950 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
951
952 sp<IGraphicBufferProducer> igbProducer;
953 setUpProducer(adapter, igbProducer, 3);
954
955 Transaction mainTransaction;
956
chaviwa1c4c822021-11-10 18:11:58 -0600957 Transaction sync;
chaviwd7deef72021-10-06 11:53:40 -0500958 // queue a sync transaction
Tianhao Yao4861b102022-02-03 20:18:35 +0000959 adapter.setSyncTransaction(sync);
chaviwd7deef72021-10-06 11:53:40 -0500960 queueBuffer(igbProducer, 0, 255, 0, 0);
961
chaviwa1c4c822021-11-10 18:11:58 -0600962 mainTransaction.merge(std::move(sync));
chaviwd7deef72021-10-06 11:53:40 -0500963
chaviwa1c4c822021-11-10 18:11:58 -0600964 // queue a few buffers without setting sync transaction
chaviwd7deef72021-10-06 11:53:40 -0500965 queueBuffer(igbProducer, 0, 0, 255, 0);
966 queueBuffer(igbProducer, 0, 0, 255, 0);
967 queueBuffer(igbProducer, 0, 0, 255, 0);
968
969 // queue another sync transaction
Tianhao Yao4861b102022-02-03 20:18:35 +0000970 adapter.setSyncTransaction(sync);
chaviwd7deef72021-10-06 11:53:40 -0500971 queueBuffer(igbProducer, r, g, b, 0);
972 // Expect 3 buffers to be released because the non sync transactions should merge
973 // with the sync
974 mProducerListener->waitOnNumberReleased(3);
975
chaviwa1c4c822021-11-10 18:11:58 -0600976 mainTransaction.merge(std::move(sync));
chaviwd7deef72021-10-06 11:53:40 -0500977 // Expect 4 buffers to be released due to merging the two syncs.
978 mProducerListener->waitOnNumberReleased(4);
979
980 CallbackHelper transactionCallback;
981 mainTransaction
982 .addTransactionCompletedCallback(transactionCallback.function,
983 transactionCallback.getContext())
984 .apply();
985
986 CallbackData callbackData;
987 transactionCallback.getCallbackData(&callbackData);
988
989 // capture screen and verify that it is red
990 ASSERT_EQ(NO_ERROR, captureDisplay(mCaptureArgs, mCaptureResults));
991 ASSERT_NO_FATAL_FAILURE(
992 checkScreenCapture(r, g, b, {0, 0, (int32_t)mDisplayWidth, (int32_t)mDisplayHeight}));
993}
994
995// Tests BBQ with a sync transaction when the buffers acquired reaches max and the only way to
996// continue processing is for a release callback from SurfaceFlinger.
997// This is done by sending a buffer to SF so it can release the previous one and allow BBQ to
998// continue acquiring buffers.
999TEST_F(BLASTBufferQueueTest, RunOutOfBuffersWaitingOnSF) {
1000 uint8_t r = 255;
1001 uint8_t g = 0;
1002 uint8_t b = 0;
1003
1004 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
1005
1006 sp<IGraphicBufferProducer> igbProducer;
1007 setUpProducer(adapter, igbProducer, 4);
1008
1009 Transaction mainTransaction;
1010
1011 // Send a buffer to SF
1012 queueBuffer(igbProducer, 0, 255, 0, 0);
1013
chaviwa1c4c822021-11-10 18:11:58 -06001014 Transaction sync;
chaviwd7deef72021-10-06 11:53:40 -05001015 // queue a sync transaction
Tianhao Yao4861b102022-02-03 20:18:35 +00001016 adapter.setSyncTransaction(sync);
chaviwd7deef72021-10-06 11:53:40 -05001017 queueBuffer(igbProducer, 0, 255, 0, 0);
1018
chaviwa1c4c822021-11-10 18:11:58 -06001019 mainTransaction.merge(std::move(sync));
chaviwd7deef72021-10-06 11:53:40 -05001020
chaviwa1c4c822021-11-10 18:11:58 -06001021 // queue a few buffers without setting sync transaction
chaviwd7deef72021-10-06 11:53:40 -05001022 queueBuffer(igbProducer, 0, 0, 255, 0);
1023 queueBuffer(igbProducer, 0, 0, 255, 0);
1024 queueBuffer(igbProducer, 0, 0, 255, 0);
1025
1026 // apply the first synced buffer to ensure we have to wait on SF
1027 mainTransaction.apply();
1028
1029 // queue another sync transaction
Tianhao Yao4861b102022-02-03 20:18:35 +00001030 adapter.setSyncTransaction(sync);
chaviwd7deef72021-10-06 11:53:40 -05001031 queueBuffer(igbProducer, r, g, b, 0);
1032 // Expect 2 buffers to be released because the non sync transactions should merge
1033 // with the sync
1034 mProducerListener->waitOnNumberReleased(3);
1035
chaviwa1c4c822021-11-10 18:11:58 -06001036 mainTransaction.merge(std::move(sync));
chaviwd7deef72021-10-06 11:53:40 -05001037
1038 CallbackHelper transactionCallback;
1039 mainTransaction
1040 .addTransactionCompletedCallback(transactionCallback.function,
1041 transactionCallback.getContext())
1042 .apply();
1043
1044 CallbackData callbackData;
1045 transactionCallback.getCallbackData(&callbackData);
1046
1047 // capture screen and verify that it is red
1048 ASSERT_EQ(NO_ERROR, captureDisplay(mCaptureArgs, mCaptureResults));
1049 ASSERT_NO_FATAL_FAILURE(
1050 checkScreenCapture(r, g, b, {0, 0, (int32_t)mDisplayWidth, (int32_t)mDisplayHeight}));
1051}
1052
Tianhao Yao4861b102022-02-03 20:18:35 +00001053TEST_F(BLASTBufferQueueTest, SyncNextTransactionAcquireMultipleBuffers) {
chaviw0acd33a2021-11-02 11:55:37 -05001054 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
1055
1056 sp<IGraphicBufferProducer> igbProducer;
1057 setUpProducer(adapter, igbProducer);
1058
1059 Transaction next;
Tianhao Yao4861b102022-02-03 20:18:35 +00001060 adapter.setSyncTransaction(next, false);
chaviw0acd33a2021-11-02 11:55:37 -05001061 queueBuffer(igbProducer, 0, 255, 0, 0);
1062 queueBuffer(igbProducer, 0, 0, 255, 0);
1063 // There should only be one frame submitted since the first frame will be released.
1064 adapter.validateNumFramesSubmitted(1);
Tianhao Yao4861b102022-02-03 20:18:35 +00001065 adapter.stopContinuousSyncTransaction();
chaviw0acd33a2021-11-02 11:55:37 -05001066
1067 // queue non sync buffer, so this one should get blocked
1068 // Add a present delay to allow the first screenshot to get taken.
1069 nsecs_t presentTimeDelay = std::chrono::nanoseconds(500ms).count();
1070 queueBuffer(igbProducer, 255, 0, 0, presentTimeDelay);
1071
1072 CallbackHelper transactionCallback;
1073 next.addTransactionCompletedCallback(transactionCallback.function,
1074 transactionCallback.getContext())
1075 .apply();
1076
1077 CallbackData callbackData;
1078 transactionCallback.getCallbackData(&callbackData);
1079
1080 // capture screen and verify that it is blue
1081 ASSERT_EQ(NO_ERROR, captureDisplay(mCaptureArgs, mCaptureResults));
1082 ASSERT_NO_FATAL_FAILURE(
1083 checkScreenCapture(0, 0, 255, {0, 0, (int32_t)mDisplayWidth, (int32_t)mDisplayHeight}));
1084
1085 mProducerListener->waitOnNumberReleased(2);
1086 // capture screen and verify that it is red
1087 ASSERT_EQ(NO_ERROR, captureDisplay(mCaptureArgs, mCaptureResults));
1088 ASSERT_NO_FATAL_FAILURE(
1089 checkScreenCapture(255, 0, 0, {0, 0, (int32_t)mDisplayWidth, (int32_t)mDisplayHeight}));
1090}
1091
chaviw3b4bdcf2022-03-17 09:27:03 -05001092TEST_F(BLASTBufferQueueTest, SyncNextTransactionOverwrite) {
1093 std::mutex mutex;
1094 std::condition_variable callbackReceivedCv;
1095 bool receivedCallback = false;
1096
1097 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
1098 ASSERT_EQ(nullptr, adapter.getTransactionReadyCallback());
1099 auto callback = [&](Transaction*) {
1100 std::unique_lock<std::mutex> lock(mutex);
1101 receivedCallback = true;
1102 callbackReceivedCv.notify_one();
1103 };
1104 adapter.syncNextTransaction(callback);
1105 ASSERT_NE(nullptr, adapter.getTransactionReadyCallback());
1106
1107 auto callback2 = [](Transaction*) {};
1108 adapter.syncNextTransaction(callback2);
1109
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
chaviwc1cf4022022-06-03 13:32:33 -05001120TEST_F(BLASTBufferQueueTest, SyncNextTransactionDropBuffer) {
1121 uint8_t r = 255;
1122 uint8_t g = 0;
1123 uint8_t b = 0;
1124
1125 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
1126
1127 sp<IGraphicBufferProducer> igbProducer;
1128 setUpProducer(adapter, igbProducer);
1129
1130 Transaction sync;
1131 adapter.setSyncTransaction(sync);
1132 queueBuffer(igbProducer, 0, 255, 0, 0);
1133
1134 // Merge a transaction that has a complete callback into the next frame so we can get notified
1135 // when to take a screenshot
1136 CallbackHelper transactionCallback;
1137 Transaction t;
1138 t.addTransactionCompletedCallback(transactionCallback.function,
1139 transactionCallback.getContext());
1140 adapter.mergeWithNextTransaction(&t, 2);
1141 queueBuffer(igbProducer, r, g, b, 0);
1142
1143 // Drop the buffer, but ensure the next one continues to get processed.
1144 sync.setBuffer(mSurfaceControl, nullptr);
1145
1146 CallbackData callbackData;
1147 transactionCallback.getCallbackData(&callbackData);
1148 ASSERT_EQ(NO_ERROR, captureDisplay(mCaptureArgs, mCaptureResults));
1149 ASSERT_NO_FATAL_FAILURE(
1150 checkScreenCapture(r, g, b, {0, 0, (int32_t)mDisplayWidth, (int32_t)mDisplayHeight}));
1151}
1152
Vishnu Nair1e8bf102021-12-28 14:36:59 -08001153// This test will currently fail because the old surfacecontrol will steal the last presented buffer
1154// until the old surface control is destroyed. This is not necessarily a bug but to document a
1155// limitation with the update API and to test any changes to make the api more robust. The current
1156// approach for the client is to recreate the blastbufferqueue when the surfacecontrol updates.
1157TEST_F(BLASTBufferQueueTest, DISABLED_DisconnectProducerTest) {
1158 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
1159 std::vector<sp<SurfaceControl>> surfaceControls;
1160 sp<IGraphicBufferProducer> igbProducer;
1161 for (int i = 0; i < 10; i++) {
1162 sp<SurfaceControl> sc =
1163 mClient->createSurface(String8("TestSurface"), mDisplayWidth, mDisplayHeight,
1164 PIXEL_FORMAT_RGBA_8888,
1165 ISurfaceComposerClient::eFXSurfaceBufferState,
1166 /*parent*/ nullptr);
1167 Transaction()
1168 .setLayerStack(mSurfaceControl, ui::DEFAULT_LAYER_STACK)
1169 .setLayer(mSurfaceControl, std::numeric_limits<int32_t>::max())
1170 .show(mSurfaceControl)
1171 .setDataspace(mSurfaceControl, ui::Dataspace::V0_SRGB)
1172 .apply(true);
1173 surfaceControls.push_back(sc);
1174 adapter.update(sc, mDisplayWidth, mDisplayHeight);
1175
1176 setUpProducer(adapter, igbProducer);
1177 Transaction next;
1178 queueBuffer(igbProducer, 0, 255, 0, 0);
1179 queueBuffer(igbProducer, 0, 0, 255, 0);
Tianhao Yao4861b102022-02-03 20:18:35 +00001180 adapter.setSyncTransaction(next, true);
Vishnu Nair1e8bf102021-12-28 14:36:59 -08001181 queueBuffer(igbProducer, 255, 0, 0, 0);
1182
1183 CallbackHelper transactionCallback;
1184 next.addTransactionCompletedCallback(transactionCallback.function,
1185 transactionCallback.getContext())
1186 .apply();
1187
1188 CallbackData callbackData;
1189 transactionCallback.getCallbackData(&callbackData);
1190 // capture screen and verify that it is red
1191 ASSERT_EQ(NO_ERROR, captureDisplay(mCaptureArgs, mCaptureResults));
1192 ASSERT_NO_FATAL_FAILURE(
1193 checkScreenCapture(255, 0, 0,
1194 {0, 0, (int32_t)mDisplayWidth, (int32_t)mDisplayHeight}));
1195 igbProducer->disconnect(NATIVE_WINDOW_API_CPU);
1196 }
1197}
1198
1199// See DISABLED_DisconnectProducerTest
1200TEST_F(BLASTBufferQueueTest, DISABLED_UpdateSurfaceControlTest) {
1201 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
1202 std::vector<sp<SurfaceControl>> surfaceControls;
1203 sp<IGraphicBufferProducer> igbProducer;
1204 for (int i = 0; i < 10; i++) {
1205 sp<SurfaceControl> sc =
1206 mClient->createSurface(String8("TestSurface"), mDisplayWidth, mDisplayHeight,
1207 PIXEL_FORMAT_RGBA_8888,
1208 ISurfaceComposerClient::eFXSurfaceBufferState,
1209 /*parent*/ nullptr);
1210 Transaction()
1211 .setLayerStack(mSurfaceControl, ui::DEFAULT_LAYER_STACK)
1212 .setLayer(mSurfaceControl, std::numeric_limits<int32_t>::max())
1213 .show(mSurfaceControl)
1214 .setDataspace(mSurfaceControl, ui::Dataspace::V0_SRGB)
1215 .apply(true);
1216 surfaceControls.push_back(sc);
1217 adapter.update(sc, mDisplayWidth, mDisplayHeight);
1218 setUpProducer(adapter, igbProducer);
1219
1220 Transaction next;
1221 queueBuffer(igbProducer, 0, 255, 0, 0);
1222 queueBuffer(igbProducer, 0, 0, 255, 0);
Tianhao Yao4861b102022-02-03 20:18:35 +00001223 adapter.setSyncTransaction(next, true);
Vishnu Nair1e8bf102021-12-28 14:36:59 -08001224 queueBuffer(igbProducer, 255, 0, 0, 0);
1225
1226 CallbackHelper transactionCallback;
1227 next.addTransactionCompletedCallback(transactionCallback.function,
1228 transactionCallback.getContext())
1229 .apply();
1230
1231 CallbackData callbackData;
1232 transactionCallback.getCallbackData(&callbackData);
1233 // capture screen and verify that it is red
1234 ASSERT_EQ(NO_ERROR, captureDisplay(mCaptureArgs, mCaptureResults));
1235 ASSERT_NO_FATAL_FAILURE(
1236 checkScreenCapture(255, 0, 0,
1237 {0, 0, (int32_t)mDisplayWidth, (int32_t)mDisplayHeight}));
1238 }
1239}
1240
Vishnu Nair89496122020-12-14 17:14:53 -08001241class TestProducerListener : public BnProducerListener {
1242public:
1243 sp<IGraphicBufferProducer> mIgbp;
1244 TestProducerListener(const sp<IGraphicBufferProducer>& igbp) : mIgbp(igbp) {}
1245 void onBufferReleased() override {
1246 sp<GraphicBuffer> buffer;
1247 sp<Fence> fence;
1248 mIgbp->detachNextBuffer(&buffer, &fence);
1249 }
1250};
1251
1252TEST_F(BLASTBufferQueueTest, CustomProducerListener) {
1253 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
1254 sp<IGraphicBufferProducer> igbProducer = adapter.getIGraphicBufferProducer();
1255 ASSERT_NE(nullptr, igbProducer.get());
1256 ASSERT_EQ(NO_ERROR, igbProducer->setMaxDequeuedBufferCount(2));
1257 IGraphicBufferProducer::QueueBufferOutput qbOutput;
1258 ASSERT_EQ(NO_ERROR,
1259 igbProducer->connect(new TestProducerListener(igbProducer), NATIVE_WINDOW_API_CPU,
1260 false, &qbOutput));
1261 ASSERT_NE(ui::Transform::ROT_INVALID, qbOutput.transformHint);
1262 for (int i = 0; i < 3; i++) {
1263 int slot;
1264 sp<Fence> fence;
1265 sp<GraphicBuffer> buf;
1266 auto ret = igbProducer->dequeueBuffer(&slot, &fence, mDisplayWidth, mDisplayHeight,
1267 PIXEL_FORMAT_RGBA_8888, GRALLOC_USAGE_SW_WRITE_OFTEN,
1268 nullptr, nullptr);
1269 ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, ret);
1270 ASSERT_EQ(OK, igbProducer->requestBuffer(slot, &buf));
1271 IGraphicBufferProducer::QueueBufferOutput qbOutput;
Vishnu Nair1506b182021-02-22 14:35:15 -08001272 IGraphicBufferProducer::QueueBufferInput input(systemTime(), true /* autotimestamp */,
1273 HAL_DATASPACE_UNKNOWN,
Vishnu Nair89496122020-12-14 17:14:53 -08001274 Rect(mDisplayWidth, mDisplayHeight),
1275 NATIVE_WINDOW_SCALING_MODE_FREEZE, 0,
1276 Fence::NO_FENCE);
1277 igbProducer->queueBuffer(slot, input, &qbOutput);
1278 }
1279 adapter.waitForCallbacks();
1280}
1281
Vishnu Nair17dde612020-12-28 11:39:59 -08001282TEST_F(BLASTBufferQueueTest, QueryNativeWindowQueuesToWindowComposer) {
1283 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
1284
1285 sp<android::Surface> surface = new Surface(adapter.getIGraphicBufferProducer());
1286 ANativeWindow* nativeWindow = (ANativeWindow*)(surface.get());
1287 int queuesToNativeWindow = 0;
1288 int err = nativeWindow->query(nativeWindow, NATIVE_WINDOW_QUEUES_TO_WINDOW_COMPOSER,
1289 &queuesToNativeWindow);
1290 ASSERT_EQ(NO_ERROR, err);
1291 ASSERT_EQ(queuesToNativeWindow, 1);
1292}
1293
Vishnu Nair083efd32021-02-12 09:32:30 -08001294// Test a slow producer doesn't hold up a faster producer from the same client. Essentially tests
1295// BBQ uses separate transaction queues.
Vishnu Nair277142c2021-01-05 18:35:29 -08001296TEST_F(BLASTBufferQueueTest, OutOfOrderTransactionTest) {
1297 sp<SurfaceControl> bgSurface =
1298 mClient->createSurface(String8("BGTest"), 0, 0, PIXEL_FORMAT_RGBA_8888,
1299 ISurfaceComposerClient::eFXSurfaceBufferState);
1300 ASSERT_NE(nullptr, bgSurface.get());
1301 Transaction t;
Dominik Laskowski29fa1462021-04-27 15:51:50 -07001302 t.setLayerStack(bgSurface, ui::DEFAULT_LAYER_STACK)
Vishnu Nair277142c2021-01-05 18:35:29 -08001303 .show(bgSurface)
1304 .setDataspace(bgSurface, ui::Dataspace::V0_SRGB)
Vishnu Nair277142c2021-01-05 18:35:29 -08001305 .setLayer(bgSurface, std::numeric_limits<int32_t>::max() - 1)
1306 .apply();
1307
1308 BLASTBufferQueueHelper slowAdapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
1309 sp<IGraphicBufferProducer> slowIgbProducer;
1310 setUpProducer(slowAdapter, slowIgbProducer);
1311 nsecs_t presentTimeDelay = std::chrono::nanoseconds(500ms).count();
Vishnu Nair1506b182021-02-22 14:35:15 -08001312 queueBuffer(slowIgbProducer, 0 /* r */, 255 /* g */, 0 /* b */, presentTimeDelay);
Vishnu Nair277142c2021-01-05 18:35:29 -08001313
1314 BLASTBufferQueueHelper fastAdapter(bgSurface, mDisplayWidth, mDisplayHeight);
1315 sp<IGraphicBufferProducer> fastIgbProducer;
1316 setUpProducer(fastAdapter, fastIgbProducer);
1317 uint8_t r = 255;
1318 uint8_t g = 0;
1319 uint8_t b = 0;
1320 queueBuffer(fastIgbProducer, r, g, b, 0 /* presentTimeDelay */);
1321 fastAdapter.waitForCallbacks();
1322
1323 // capture screen and verify that it is red
1324 ASSERT_EQ(NO_ERROR, captureDisplay(mCaptureArgs, mCaptureResults));
1325
1326 ASSERT_NO_FATAL_FAILURE(
Chavi Weingartena5aedbd2021-04-09 13:37:33 +00001327 checkScreenCapture(r, g, b,
1328 {0, 0, (int32_t)mDisplayWidth, (int32_t)mDisplayHeight / 2}));
Vishnu Nair277142c2021-01-05 18:35:29 -08001329}
1330
Vishnu Naira4fbca52021-07-07 16:52:34 -07001331TEST_F(BLASTBufferQueueTest, TransformHint) {
1332 // Transform hint is provided to BBQ via the surface control passed by WM
1333 mSurfaceControl->setTransformHint(ui::Transform::ROT_90);
1334
1335 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
1336 sp<IGraphicBufferProducer> igbProducer = adapter.getIGraphicBufferProducer();
1337 ASSERT_NE(nullptr, igbProducer.get());
1338 ASSERT_EQ(NO_ERROR, igbProducer->setMaxDequeuedBufferCount(2));
1339 sp<Surface> surface = adapter.getSurface();
1340
1341 // Before connecting to the surface, we do not get a valid transform hint
1342 int transformHint;
1343 surface->query(NATIVE_WINDOW_TRANSFORM_HINT, &transformHint);
1344 ASSERT_EQ(ui::Transform::ROT_0, transformHint);
1345
1346 ASSERT_EQ(NO_ERROR,
1347 surface->connect(NATIVE_WINDOW_API_CPU, new TestProducerListener(igbProducer)));
1348
1349 // After connecting to the surface, we should get the correct hint.
1350 surface->query(NATIVE_WINDOW_TRANSFORM_HINT, &transformHint);
1351 ASSERT_EQ(ui::Transform::ROT_90, transformHint);
1352
1353 ANativeWindow_Buffer buffer;
1354 surface->lock(&buffer, nullptr /* inOutDirtyBounds */);
1355
1356 // Transform hint is updated via callbacks or surface control updates
1357 mSurfaceControl->setTransformHint(ui::Transform::ROT_0);
1358 adapter.update(mSurfaceControl, mDisplayWidth, mDisplayHeight);
1359
1360 // The hint does not change and matches the value used when dequeueing the buffer.
1361 surface->query(NATIVE_WINDOW_TRANSFORM_HINT, &transformHint);
1362 ASSERT_EQ(ui::Transform::ROT_90, transformHint);
1363
1364 surface->unlockAndPost();
1365
1366 // After queuing the buffer, we get the updated transform hint
1367 surface->query(NATIVE_WINDOW_TRANSFORM_HINT, &transformHint);
1368 ASSERT_EQ(ui::Transform::ROT_0, transformHint);
1369
1370 adapter.waitForCallbacks();
1371}
1372
Valerie Hau5977fc82019-12-05 15:56:39 -08001373class BLASTBufferQueueTransformTest : public BLASTBufferQueueTest {
1374public:
1375 void test(uint32_t tr) {
1376 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
1377 sp<IGraphicBufferProducer> igbProducer;
1378 setUpProducer(adapter, igbProducer);
1379
1380 auto bufWidth = mDisplayWidth;
1381 auto bufHeight = mDisplayHeight;
1382 int slot;
1383 sp<Fence> fence;
1384 sp<GraphicBuffer> buf;
1385
1386 auto ret = igbProducer->dequeueBuffer(&slot, &fence, bufWidth, bufHeight,
1387 PIXEL_FORMAT_RGBA_8888, GRALLOC_USAGE_SW_WRITE_OFTEN,
1388 nullptr, nullptr);
1389 ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, ret);
1390 ASSERT_EQ(OK, igbProducer->requestBuffer(slot, &buf));
1391
1392 fillQuadrants(buf);
1393
1394 IGraphicBufferProducer::QueueBufferOutput qbOutput;
Vishnu Nair1506b182021-02-22 14:35:15 -08001395 IGraphicBufferProducer::QueueBufferInput input(systemTime(), true /* autotimestamp */,
1396 HAL_DATASPACE_UNKNOWN,
Valerie Hau5977fc82019-12-05 15:56:39 -08001397 Rect(bufWidth, bufHeight),
Vishnu Naire1a42322020-10-02 17:42:04 -07001398 NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW,
1399 tr, Fence::NO_FENCE);
Valerie Hau5977fc82019-12-05 15:56:39 -08001400 igbProducer->queueBuffer(slot, input, &qbOutput);
Dominik Laskowski718f9602019-11-09 20:01:35 -08001401 ASSERT_NE(ui::Transform::ROT_INVALID, qbOutput.transformHint);
Valerie Hau5977fc82019-12-05 15:56:39 -08001402
1403 adapter.waitForCallbacks();
chaviw8ffc7b82020-08-18 11:25:37 -07001404 ASSERT_EQ(NO_ERROR, captureDisplay(mCaptureArgs, mCaptureResults));
chaviwd2432892020-07-24 17:42:39 -07001405
Valerie Hau5977fc82019-12-05 15:56:39 -08001406 switch (tr) {
1407 case ui::Transform::ROT_0:
1408 ASSERT_NO_FATAL_FAILURE(checkScreenCapture(0, 0, 0,
1409 {0, 0, (int32_t)mDisplayWidth / 2,
1410 (int32_t)mDisplayHeight / 2},
1411 1));
1412 ASSERT_NO_FATAL_FAILURE(
1413 checkScreenCapture(255, 0, 0,
1414 {(int32_t)mDisplayWidth / 2, 0, (int32_t)mDisplayWidth,
1415 (int32_t)mDisplayHeight / 2},
1416 1));
1417 ASSERT_NO_FATAL_FAILURE(
1418 checkScreenCapture(0, 255, 0,
1419 {(int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight / 2,
1420 (int32_t)mDisplayWidth, (int32_t)mDisplayHeight},
1421 1));
1422 ASSERT_NO_FATAL_FAILURE(
1423 checkScreenCapture(0, 0, 255,
1424 {0, (int32_t)mDisplayHeight / 2,
1425 (int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight},
1426 1));
1427 break;
1428 case ui::Transform::FLIP_H:
1429 ASSERT_NO_FATAL_FAILURE(checkScreenCapture(255, 0, 0,
1430 {0, 0, (int32_t)mDisplayWidth / 2,
1431 (int32_t)mDisplayHeight / 2},
1432 1));
1433 ASSERT_NO_FATAL_FAILURE(
1434 checkScreenCapture(0, 0, 0,
1435 {(int32_t)mDisplayWidth / 2, 0, (int32_t)mDisplayWidth,
1436 (int32_t)mDisplayHeight / 2},
1437 1));
1438 ASSERT_NO_FATAL_FAILURE(
1439 checkScreenCapture(0, 0, 255,
1440 {(int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight / 2,
1441 (int32_t)mDisplayWidth, (int32_t)mDisplayHeight},
1442 1));
1443 ASSERT_NO_FATAL_FAILURE(
1444 checkScreenCapture(0, 255, 0,
1445 {0, (int32_t)mDisplayHeight / 2,
1446 (int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight},
1447 1));
1448 break;
1449 case ui::Transform::FLIP_V:
1450 ASSERT_NO_FATAL_FAILURE(checkScreenCapture(0, 0, 255,
1451 {0, 0, (int32_t)mDisplayWidth / 2,
1452 (int32_t)mDisplayHeight / 2},
1453 1));
1454 ASSERT_NO_FATAL_FAILURE(
1455 checkScreenCapture(0, 255, 0,
1456 {(int32_t)mDisplayWidth / 2, 0, (int32_t)mDisplayWidth,
1457 (int32_t)mDisplayHeight / 2},
1458 1));
1459 ASSERT_NO_FATAL_FAILURE(
1460 checkScreenCapture(255, 0, 0,
1461 {(int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight / 2,
1462 (int32_t)mDisplayWidth, (int32_t)mDisplayHeight},
1463 1));
1464 ASSERT_NO_FATAL_FAILURE(
1465 checkScreenCapture(0, 0, 0,
1466 {0, (int32_t)mDisplayHeight / 2,
1467 (int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight},
1468 1));
1469 break;
1470 case ui::Transform::ROT_90:
1471 ASSERT_NO_FATAL_FAILURE(checkScreenCapture(0, 0, 255,
1472 {0, 0, (int32_t)mDisplayWidth / 2,
1473 (int32_t)mDisplayHeight / 2},
1474 1));
1475 ASSERT_NO_FATAL_FAILURE(
1476 checkScreenCapture(0, 0, 0,
1477 {(int32_t)mDisplayWidth / 2, 0, (int32_t)mDisplayWidth,
1478 (int32_t)mDisplayHeight / 2},
1479 1));
1480 ASSERT_NO_FATAL_FAILURE(
1481 checkScreenCapture(255, 0, 0,
1482 {(int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight / 2,
1483 (int32_t)mDisplayWidth, (int32_t)mDisplayHeight},
1484 1));
1485 ASSERT_NO_FATAL_FAILURE(
1486 checkScreenCapture(0, 255, 0,
1487 {0, (int32_t)mDisplayHeight / 2,
1488 (int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight},
1489 1));
1490 break;
1491 case ui::Transform::ROT_180:
1492 ASSERT_NO_FATAL_FAILURE(checkScreenCapture(0, 255, 0,
1493 {0, 0, (int32_t)mDisplayWidth / 2,
1494 (int32_t)mDisplayHeight / 2},
1495 1));
1496 ASSERT_NO_FATAL_FAILURE(
1497 checkScreenCapture(0, 0, 255,
1498 {(int32_t)mDisplayWidth / 2, 0, (int32_t)mDisplayWidth,
1499 (int32_t)mDisplayHeight / 2},
1500 1));
1501 ASSERT_NO_FATAL_FAILURE(
1502 checkScreenCapture(0, 0, 0,
1503 {(int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight / 2,
1504 (int32_t)mDisplayWidth, (int32_t)mDisplayHeight},
1505 1));
1506 ASSERT_NO_FATAL_FAILURE(
1507 checkScreenCapture(255, 0, 0,
1508 {0, (int32_t)mDisplayHeight / 2,
1509 (int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight},
1510 1));
1511 break;
1512 case ui::Transform::ROT_270:
1513 ASSERT_NO_FATAL_FAILURE(checkScreenCapture(255, 0, 0,
1514 {0, 0, (int32_t)mDisplayWidth / 2,
1515 (int32_t)mDisplayHeight / 2},
1516 1));
1517 ASSERT_NO_FATAL_FAILURE(
1518 checkScreenCapture(0, 255, 0,
1519 {(int32_t)mDisplayWidth / 2, 0, (int32_t)mDisplayWidth,
1520 (int32_t)mDisplayHeight / 2},
1521 1));
1522 ASSERT_NO_FATAL_FAILURE(
1523 checkScreenCapture(0, 0, 255,
1524 {(int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight / 2,
1525 (int32_t)mDisplayWidth, (int32_t)mDisplayHeight},
1526 1));
1527 ASSERT_NO_FATAL_FAILURE(
1528 checkScreenCapture(0, 0, 0,
1529 {0, (int32_t)mDisplayHeight / 2,
1530 (int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight},
1531 1));
1532 }
1533 }
1534};
1535
1536TEST_F(BLASTBufferQueueTransformTest, setTransform_ROT_0) {
1537 test(ui::Transform::ROT_0);
1538}
1539
1540TEST_F(BLASTBufferQueueTransformTest, setTransform_FLIP_H) {
1541 test(ui::Transform::FLIP_H);
1542}
1543
1544TEST_F(BLASTBufferQueueTransformTest, setTransform_FLIP_V) {
1545 test(ui::Transform::FLIP_V);
1546}
1547
1548TEST_F(BLASTBufferQueueTransformTest, setTransform_ROT_90) {
1549 test(ui::Transform::ROT_90);
1550}
1551
1552TEST_F(BLASTBufferQueueTransformTest, setTransform_ROT_180) {
1553 test(ui::Transform::ROT_180);
1554}
1555
1556TEST_F(BLASTBufferQueueTransformTest, setTransform_ROT_270) {
1557 test(ui::Transform::ROT_270);
1558}
Valerie Hau871d6352020-01-29 08:44:02 -08001559
1560class BLASTFrameEventHistoryTest : public BLASTBufferQueueTest {
1561public:
1562 void setUpAndQueueBuffer(const sp<IGraphicBufferProducer>& igbProducer,
Vishnu Nairde66dc72021-06-17 17:54:41 -07001563 nsecs_t* outRequestedPresentTime, nsecs_t* postedTime,
Valerie Hau871d6352020-01-29 08:44:02 -08001564 IGraphicBufferProducer::QueueBufferOutput* qbOutput,
Vishnu Nairde66dc72021-06-17 17:54:41 -07001565 bool getFrameTimestamps, nsecs_t requestedPresentTime = systemTime()) {
Valerie Hau871d6352020-01-29 08:44:02 -08001566 int slot;
1567 sp<Fence> fence;
1568 sp<GraphicBuffer> buf;
1569 auto ret = igbProducer->dequeueBuffer(&slot, &fence, mDisplayWidth, mDisplayHeight,
1570 PIXEL_FORMAT_RGBA_8888, GRALLOC_USAGE_SW_WRITE_OFTEN,
1571 nullptr, nullptr);
Vishnu Nairde66dc72021-06-17 17:54:41 -07001572 if (IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION == ret) {
1573 ASSERT_EQ(OK, igbProducer->requestBuffer(slot, &buf));
1574 }
Valerie Hau871d6352020-01-29 08:44:02 -08001575
Vishnu Nairde66dc72021-06-17 17:54:41 -07001576 *outRequestedPresentTime = requestedPresentTime;
1577 IGraphicBufferProducer::QueueBufferInput input(requestedPresentTime, false,
1578 HAL_DATASPACE_UNKNOWN,
Valerie Hau871d6352020-01-29 08:44:02 -08001579 Rect(mDisplayWidth, mDisplayHeight),
1580 NATIVE_WINDOW_SCALING_MODE_FREEZE, 0,
1581 Fence::NO_FENCE, /*sticky*/ 0,
1582 getFrameTimestamps);
1583 if (postedTime) *postedTime = systemTime();
1584 igbProducer->queueBuffer(slot, input, qbOutput);
1585 }
Vishnu Nair083efd32021-02-12 09:32:30 -08001586 sp<SurfaceControl> mBufferQueueSurfaceControl;
Valerie Hau871d6352020-01-29 08:44:02 -08001587};
1588
1589TEST_F(BLASTFrameEventHistoryTest, FrameEventHistory_Basic) {
1590 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
1591 sp<IGraphicBufferProducer> igbProducer;
1592 ProducerFrameEventHistory history;
1593 setUpProducer(adapter, igbProducer);
1594
1595 IGraphicBufferProducer::QueueBufferOutput qbOutput;
1596 nsecs_t requestedPresentTimeA = 0;
1597 nsecs_t postedTimeA = 0;
1598 setUpAndQueueBuffer(igbProducer, &requestedPresentTimeA, &postedTimeA, &qbOutput, true);
1599 history.applyDelta(qbOutput.frameTimestamps);
1600
1601 FrameEvents* events = nullptr;
1602 events = history.getFrame(1);
1603 ASSERT_NE(nullptr, events);
1604 ASSERT_EQ(1, events->frameNumber);
1605 ASSERT_EQ(requestedPresentTimeA, events->requestedPresentTime);
1606 ASSERT_GE(events->postedTime, postedTimeA);
1607
Vishnu Nair1506b182021-02-22 14:35:15 -08001608 adapter.waitForCallback(1);
Valerie Hau871d6352020-01-29 08:44:02 -08001609
1610 // queue another buffer so we query for frame event deltas
1611 nsecs_t requestedPresentTimeB = 0;
1612 nsecs_t postedTimeB = 0;
1613 setUpAndQueueBuffer(igbProducer, &requestedPresentTimeB, &postedTimeB, &qbOutput, true);
1614 history.applyDelta(qbOutput.frameTimestamps);
1615 events = history.getFrame(1);
1616 ASSERT_NE(nullptr, events);
1617
1618 // frame number, requestedPresentTime, and postTime should not have changed
1619 ASSERT_EQ(1, events->frameNumber);
1620 ASSERT_EQ(requestedPresentTimeA, events->requestedPresentTime);
1621 ASSERT_GE(events->postedTime, postedTimeA);
1622
1623 ASSERT_GE(events->latchTime, postedTimeA);
1624 ASSERT_GE(events->dequeueReadyTime, events->latchTime);
1625 ASSERT_NE(nullptr, events->gpuCompositionDoneFence);
1626 ASSERT_NE(nullptr, events->displayPresentFence);
1627 ASSERT_NE(nullptr, events->releaseFence);
1628
1629 // we should also have gotten the initial values for the next frame
1630 events = history.getFrame(2);
1631 ASSERT_NE(nullptr, events);
1632 ASSERT_EQ(2, events->frameNumber);
1633 ASSERT_EQ(requestedPresentTimeB, events->requestedPresentTime);
1634 ASSERT_GE(events->postedTime, postedTimeB);
Valerie Hau78491e92020-04-15 13:10:56 -07001635
1636 // wait for any callbacks that have not been received
1637 adapter.waitForCallbacks();
Valerie Hau871d6352020-01-29 08:44:02 -08001638}
Vishnu Nair083efd32021-02-12 09:32:30 -08001639
Vishnu Nair083efd32021-02-12 09:32:30 -08001640TEST_F(BLASTFrameEventHistoryTest, FrameEventHistory_DroppedFrame) {
1641 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
1642 sp<IGraphicBufferProducer> igbProducer;
1643 setUpProducer(adapter, igbProducer);
1644
1645 ProducerFrameEventHistory history;
1646 IGraphicBufferProducer::QueueBufferOutput qbOutput;
1647 nsecs_t requestedPresentTimeA = 0;
1648 nsecs_t postedTimeA = 0;
Vishnu Nairde66dc72021-06-17 17:54:41 -07001649 // Present the frame sometime in the future so we can add two frames to the queue so the older
1650 // one will be dropped.
1651 nsecs_t presentTime = systemTime() + std::chrono::nanoseconds(500ms).count();
Vishnu Nair083efd32021-02-12 09:32:30 -08001652 setUpAndQueueBuffer(igbProducer, &requestedPresentTimeA, &postedTimeA, &qbOutput, true,
Vishnu Nairde66dc72021-06-17 17:54:41 -07001653 presentTime);
Vishnu Nair083efd32021-02-12 09:32:30 -08001654 history.applyDelta(qbOutput.frameTimestamps);
1655
1656 FrameEvents* events = nullptr;
1657 events = history.getFrame(1);
1658 ASSERT_NE(nullptr, events);
1659 ASSERT_EQ(1, events->frameNumber);
1660 ASSERT_EQ(requestedPresentTimeA, events->requestedPresentTime);
1661 ASSERT_GE(events->postedTime, postedTimeA);
1662
1663 // queue another buffer so the first can be dropped
1664 nsecs_t requestedPresentTimeB = 0;
1665 nsecs_t postedTimeB = 0;
Vishnu Nairde66dc72021-06-17 17:54:41 -07001666 presentTime = systemTime() + std::chrono::nanoseconds(1ms).count();
1667 setUpAndQueueBuffer(igbProducer, &requestedPresentTimeB, &postedTimeB, &qbOutput, true,
1668 presentTime);
Vishnu Nair083efd32021-02-12 09:32:30 -08001669 history.applyDelta(qbOutput.frameTimestamps);
1670 events = history.getFrame(1);
1671 ASSERT_NE(nullptr, events);
1672
1673 // frame number, requestedPresentTime, and postTime should not have changed
1674 ASSERT_EQ(1, events->frameNumber);
1675 ASSERT_EQ(requestedPresentTimeA, events->requestedPresentTime);
1676 ASSERT_GE(events->postedTime, postedTimeA);
1677
Vishnu Nairde66dc72021-06-17 17:54:41 -07001678 // a valid latchtime and pre and post composition info should not be set for the dropped frame
Vishnu Nair083efd32021-02-12 09:32:30 -08001679 ASSERT_FALSE(events->hasLatchInfo());
1680 ASSERT_FALSE(events->hasDequeueReadyInfo());
Vishnu Nairde66dc72021-06-17 17:54:41 -07001681 ASSERT_FALSE(events->hasGpuCompositionDoneInfo());
1682 ASSERT_FALSE(events->hasDisplayPresentInfo());
1683 ASSERT_FALSE(events->hasReleaseInfo());
Vishnu Nair083efd32021-02-12 09:32:30 -08001684
Vishnu Nairde66dc72021-06-17 17:54:41 -07001685 // wait for the last transaction to be completed.
1686 adapter.waitForCallback(2);
Vishnu Nair083efd32021-02-12 09:32:30 -08001687
Vishnu Nairde66dc72021-06-17 17:54:41 -07001688 // queue another buffer so we query for frame event deltas
1689 nsecs_t requestedPresentTimeC = 0;
1690 nsecs_t postedTimeC = 0;
1691 setUpAndQueueBuffer(igbProducer, &requestedPresentTimeC, &postedTimeC, &qbOutput, true);
1692 history.applyDelta(qbOutput.frameTimestamps);
1693
1694 // frame number, requestedPresentTime, and postTime should not have changed
1695 ASSERT_EQ(1, events->frameNumber);
1696 ASSERT_EQ(requestedPresentTimeA, events->requestedPresentTime);
1697 ASSERT_GE(events->postedTime, postedTimeA);
1698
1699 // a valid latchtime and pre and post composition info should not be set for the dropped frame
1700 ASSERT_FALSE(events->hasLatchInfo());
1701 ASSERT_FALSE(events->hasDequeueReadyInfo());
1702 ASSERT_FALSE(events->hasGpuCompositionDoneInfo());
1703 ASSERT_FALSE(events->hasDisplayPresentInfo());
1704 ASSERT_FALSE(events->hasReleaseInfo());
1705
1706 // we should also have gotten values for the presented frame
Vishnu Nair083efd32021-02-12 09:32:30 -08001707 events = history.getFrame(2);
1708 ASSERT_NE(nullptr, events);
1709 ASSERT_EQ(2, events->frameNumber);
1710 ASSERT_EQ(requestedPresentTimeB, events->requestedPresentTime);
1711 ASSERT_GE(events->postedTime, postedTimeB);
Vishnu Nairde66dc72021-06-17 17:54:41 -07001712 ASSERT_GE(events->latchTime, postedTimeB);
1713 ASSERT_GE(events->dequeueReadyTime, events->latchTime);
1714 ASSERT_NE(nullptr, events->gpuCompositionDoneFence);
1715 ASSERT_NE(nullptr, events->displayPresentFence);
1716 ASSERT_NE(nullptr, events->releaseFence);
1717
1718 // wait for any callbacks that have not been received
1719 adapter.waitForCallbacks();
Vishnu Nair083efd32021-02-12 09:32:30 -08001720}
1721
Vishnu Nair9a69a042021-06-18 13:19:49 -07001722TEST_F(BLASTFrameEventHistoryTest, FrameEventHistory_CompositorTimings) {
1723 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
1724 sp<IGraphicBufferProducer> igbProducer;
1725 ProducerFrameEventHistory history;
1726 setUpProducer(adapter, igbProducer);
1727
1728 IGraphicBufferProducer::QueueBufferOutput qbOutput;
1729 nsecs_t requestedPresentTimeA = 0;
1730 nsecs_t postedTimeA = 0;
Vishnu Nair9a69a042021-06-18 13:19:49 -07001731 setUpAndQueueBuffer(igbProducer, &requestedPresentTimeA, &postedTimeA, &qbOutput, true);
1732 history.applyDelta(qbOutput.frameTimestamps);
1733 adapter.waitForCallback(1);
1734
1735 // queue another buffer so we query for frame event deltas
1736 nsecs_t requestedPresentTimeB = 0;
1737 nsecs_t postedTimeB = 0;
1738 setUpAndQueueBuffer(igbProducer, &requestedPresentTimeB, &postedTimeB, &qbOutput, true);
1739 history.applyDelta(qbOutput.frameTimestamps);
1740
1741 // check for a valid compositor deadline
1742 ASSERT_NE(0, history.getReportedCompositeDeadline());
1743
1744 // wait for any callbacks that have not been received
1745 adapter.waitForCallbacks();
1746}
1747
Valerie Hauc5011f92019-10-11 09:52:07 -07001748} // namespace android