blob: 3e563b2aa284a418fabba2a7c07a84a8dbaba45d [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}));
Rob Carr4f797ab2022-07-07 18:29:22 +00001151 sync.apply();
chaviwc1cf4022022-06-03 13:32:33 -05001152}
1153
Vishnu Nair1e8bf102021-12-28 14:36:59 -08001154// This test will currently fail because the old surfacecontrol will steal the last presented buffer
1155// until the old surface control is destroyed. This is not necessarily a bug but to document a
1156// limitation with the update API and to test any changes to make the api more robust. The current
1157// approach for the client is to recreate the blastbufferqueue when the surfacecontrol updates.
1158TEST_F(BLASTBufferQueueTest, DISABLED_DisconnectProducerTest) {
1159 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
1160 std::vector<sp<SurfaceControl>> surfaceControls;
1161 sp<IGraphicBufferProducer> igbProducer;
1162 for (int i = 0; i < 10; i++) {
1163 sp<SurfaceControl> sc =
1164 mClient->createSurface(String8("TestSurface"), mDisplayWidth, mDisplayHeight,
1165 PIXEL_FORMAT_RGBA_8888,
1166 ISurfaceComposerClient::eFXSurfaceBufferState,
1167 /*parent*/ nullptr);
1168 Transaction()
1169 .setLayerStack(mSurfaceControl, ui::DEFAULT_LAYER_STACK)
1170 .setLayer(mSurfaceControl, std::numeric_limits<int32_t>::max())
1171 .show(mSurfaceControl)
1172 .setDataspace(mSurfaceControl, ui::Dataspace::V0_SRGB)
1173 .apply(true);
1174 surfaceControls.push_back(sc);
1175 adapter.update(sc, mDisplayWidth, mDisplayHeight);
1176
1177 setUpProducer(adapter, igbProducer);
1178 Transaction next;
1179 queueBuffer(igbProducer, 0, 255, 0, 0);
1180 queueBuffer(igbProducer, 0, 0, 255, 0);
Tianhao Yao4861b102022-02-03 20:18:35 +00001181 adapter.setSyncTransaction(next, true);
Vishnu Nair1e8bf102021-12-28 14:36:59 -08001182 queueBuffer(igbProducer, 255, 0, 0, 0);
1183
1184 CallbackHelper transactionCallback;
1185 next.addTransactionCompletedCallback(transactionCallback.function,
1186 transactionCallback.getContext())
1187 .apply();
1188
1189 CallbackData callbackData;
1190 transactionCallback.getCallbackData(&callbackData);
1191 // capture screen and verify that it is red
1192 ASSERT_EQ(NO_ERROR, captureDisplay(mCaptureArgs, mCaptureResults));
1193 ASSERT_NO_FATAL_FAILURE(
1194 checkScreenCapture(255, 0, 0,
1195 {0, 0, (int32_t)mDisplayWidth, (int32_t)mDisplayHeight}));
1196 igbProducer->disconnect(NATIVE_WINDOW_API_CPU);
1197 }
1198}
1199
1200// See DISABLED_DisconnectProducerTest
1201TEST_F(BLASTBufferQueueTest, DISABLED_UpdateSurfaceControlTest) {
1202 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
1203 std::vector<sp<SurfaceControl>> surfaceControls;
1204 sp<IGraphicBufferProducer> igbProducer;
1205 for (int i = 0; i < 10; i++) {
1206 sp<SurfaceControl> sc =
1207 mClient->createSurface(String8("TestSurface"), mDisplayWidth, mDisplayHeight,
1208 PIXEL_FORMAT_RGBA_8888,
1209 ISurfaceComposerClient::eFXSurfaceBufferState,
1210 /*parent*/ nullptr);
1211 Transaction()
1212 .setLayerStack(mSurfaceControl, ui::DEFAULT_LAYER_STACK)
1213 .setLayer(mSurfaceControl, std::numeric_limits<int32_t>::max())
1214 .show(mSurfaceControl)
1215 .setDataspace(mSurfaceControl, ui::Dataspace::V0_SRGB)
1216 .apply(true);
1217 surfaceControls.push_back(sc);
1218 adapter.update(sc, mDisplayWidth, mDisplayHeight);
1219 setUpProducer(adapter, igbProducer);
1220
1221 Transaction next;
1222 queueBuffer(igbProducer, 0, 255, 0, 0);
1223 queueBuffer(igbProducer, 0, 0, 255, 0);
Tianhao Yao4861b102022-02-03 20:18:35 +00001224 adapter.setSyncTransaction(next, true);
Vishnu Nair1e8bf102021-12-28 14:36:59 -08001225 queueBuffer(igbProducer, 255, 0, 0, 0);
1226
1227 CallbackHelper transactionCallback;
1228 next.addTransactionCompletedCallback(transactionCallback.function,
1229 transactionCallback.getContext())
1230 .apply();
1231
1232 CallbackData callbackData;
1233 transactionCallback.getCallbackData(&callbackData);
1234 // capture screen and verify that it is red
1235 ASSERT_EQ(NO_ERROR, captureDisplay(mCaptureArgs, mCaptureResults));
1236 ASSERT_NO_FATAL_FAILURE(
1237 checkScreenCapture(255, 0, 0,
1238 {0, 0, (int32_t)mDisplayWidth, (int32_t)mDisplayHeight}));
1239 }
1240}
1241
Vishnu Nair89496122020-12-14 17:14:53 -08001242class TestProducerListener : public BnProducerListener {
1243public:
1244 sp<IGraphicBufferProducer> mIgbp;
1245 TestProducerListener(const sp<IGraphicBufferProducer>& igbp) : mIgbp(igbp) {}
1246 void onBufferReleased() override {
1247 sp<GraphicBuffer> buffer;
1248 sp<Fence> fence;
1249 mIgbp->detachNextBuffer(&buffer, &fence);
1250 }
1251};
1252
1253TEST_F(BLASTBufferQueueTest, CustomProducerListener) {
1254 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
1255 sp<IGraphicBufferProducer> igbProducer = adapter.getIGraphicBufferProducer();
1256 ASSERT_NE(nullptr, igbProducer.get());
1257 ASSERT_EQ(NO_ERROR, igbProducer->setMaxDequeuedBufferCount(2));
1258 IGraphicBufferProducer::QueueBufferOutput qbOutput;
1259 ASSERT_EQ(NO_ERROR,
1260 igbProducer->connect(new TestProducerListener(igbProducer), NATIVE_WINDOW_API_CPU,
1261 false, &qbOutput));
1262 ASSERT_NE(ui::Transform::ROT_INVALID, qbOutput.transformHint);
1263 for (int i = 0; i < 3; i++) {
1264 int slot;
1265 sp<Fence> fence;
1266 sp<GraphicBuffer> buf;
1267 auto ret = igbProducer->dequeueBuffer(&slot, &fence, mDisplayWidth, mDisplayHeight,
1268 PIXEL_FORMAT_RGBA_8888, GRALLOC_USAGE_SW_WRITE_OFTEN,
1269 nullptr, nullptr);
1270 ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, ret);
1271 ASSERT_EQ(OK, igbProducer->requestBuffer(slot, &buf));
1272 IGraphicBufferProducer::QueueBufferOutput qbOutput;
Vishnu Nair1506b182021-02-22 14:35:15 -08001273 IGraphicBufferProducer::QueueBufferInput input(systemTime(), true /* autotimestamp */,
1274 HAL_DATASPACE_UNKNOWN,
Vishnu Nair89496122020-12-14 17:14:53 -08001275 Rect(mDisplayWidth, mDisplayHeight),
1276 NATIVE_WINDOW_SCALING_MODE_FREEZE, 0,
1277 Fence::NO_FENCE);
1278 igbProducer->queueBuffer(slot, input, &qbOutput);
1279 }
1280 adapter.waitForCallbacks();
1281}
1282
Vishnu Nair17dde612020-12-28 11:39:59 -08001283TEST_F(BLASTBufferQueueTest, QueryNativeWindowQueuesToWindowComposer) {
1284 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
1285
1286 sp<android::Surface> surface = new Surface(adapter.getIGraphicBufferProducer());
1287 ANativeWindow* nativeWindow = (ANativeWindow*)(surface.get());
1288 int queuesToNativeWindow = 0;
1289 int err = nativeWindow->query(nativeWindow, NATIVE_WINDOW_QUEUES_TO_WINDOW_COMPOSER,
1290 &queuesToNativeWindow);
1291 ASSERT_EQ(NO_ERROR, err);
1292 ASSERT_EQ(queuesToNativeWindow, 1);
1293}
1294
Vishnu Nair083efd32021-02-12 09:32:30 -08001295// Test a slow producer doesn't hold up a faster producer from the same client. Essentially tests
1296// BBQ uses separate transaction queues.
Vishnu Nair277142c2021-01-05 18:35:29 -08001297TEST_F(BLASTBufferQueueTest, OutOfOrderTransactionTest) {
1298 sp<SurfaceControl> bgSurface =
1299 mClient->createSurface(String8("BGTest"), 0, 0, PIXEL_FORMAT_RGBA_8888,
1300 ISurfaceComposerClient::eFXSurfaceBufferState);
1301 ASSERT_NE(nullptr, bgSurface.get());
1302 Transaction t;
Dominik Laskowski29fa1462021-04-27 15:51:50 -07001303 t.setLayerStack(bgSurface, ui::DEFAULT_LAYER_STACK)
Vishnu Nair277142c2021-01-05 18:35:29 -08001304 .show(bgSurface)
1305 .setDataspace(bgSurface, ui::Dataspace::V0_SRGB)
Vishnu Nair277142c2021-01-05 18:35:29 -08001306 .setLayer(bgSurface, std::numeric_limits<int32_t>::max() - 1)
1307 .apply();
1308
1309 BLASTBufferQueueHelper slowAdapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
1310 sp<IGraphicBufferProducer> slowIgbProducer;
1311 setUpProducer(slowAdapter, slowIgbProducer);
1312 nsecs_t presentTimeDelay = std::chrono::nanoseconds(500ms).count();
Vishnu Nair1506b182021-02-22 14:35:15 -08001313 queueBuffer(slowIgbProducer, 0 /* r */, 255 /* g */, 0 /* b */, presentTimeDelay);
Vishnu Nair277142c2021-01-05 18:35:29 -08001314
1315 BLASTBufferQueueHelper fastAdapter(bgSurface, mDisplayWidth, mDisplayHeight);
1316 sp<IGraphicBufferProducer> fastIgbProducer;
1317 setUpProducer(fastAdapter, fastIgbProducer);
1318 uint8_t r = 255;
1319 uint8_t g = 0;
1320 uint8_t b = 0;
1321 queueBuffer(fastIgbProducer, r, g, b, 0 /* presentTimeDelay */);
1322 fastAdapter.waitForCallbacks();
1323
1324 // capture screen and verify that it is red
1325 ASSERT_EQ(NO_ERROR, captureDisplay(mCaptureArgs, mCaptureResults));
1326
1327 ASSERT_NO_FATAL_FAILURE(
Chavi Weingartena5aedbd2021-04-09 13:37:33 +00001328 checkScreenCapture(r, g, b,
1329 {0, 0, (int32_t)mDisplayWidth, (int32_t)mDisplayHeight / 2}));
Vishnu Nair277142c2021-01-05 18:35:29 -08001330}
1331
Vishnu Naira4fbca52021-07-07 16:52:34 -07001332TEST_F(BLASTBufferQueueTest, TransformHint) {
1333 // Transform hint is provided to BBQ via the surface control passed by WM
1334 mSurfaceControl->setTransformHint(ui::Transform::ROT_90);
1335
1336 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
1337 sp<IGraphicBufferProducer> igbProducer = adapter.getIGraphicBufferProducer();
1338 ASSERT_NE(nullptr, igbProducer.get());
1339 ASSERT_EQ(NO_ERROR, igbProducer->setMaxDequeuedBufferCount(2));
1340 sp<Surface> surface = adapter.getSurface();
1341
1342 // Before connecting to the surface, we do not get a valid transform hint
1343 int transformHint;
1344 surface->query(NATIVE_WINDOW_TRANSFORM_HINT, &transformHint);
1345 ASSERT_EQ(ui::Transform::ROT_0, transformHint);
1346
1347 ASSERT_EQ(NO_ERROR,
1348 surface->connect(NATIVE_WINDOW_API_CPU, new TestProducerListener(igbProducer)));
1349
1350 // After connecting to the surface, we should get the correct hint.
1351 surface->query(NATIVE_WINDOW_TRANSFORM_HINT, &transformHint);
1352 ASSERT_EQ(ui::Transform::ROT_90, transformHint);
1353
1354 ANativeWindow_Buffer buffer;
1355 surface->lock(&buffer, nullptr /* inOutDirtyBounds */);
1356
1357 // Transform hint is updated via callbacks or surface control updates
1358 mSurfaceControl->setTransformHint(ui::Transform::ROT_0);
1359 adapter.update(mSurfaceControl, mDisplayWidth, mDisplayHeight);
1360
1361 // The hint does not change and matches the value used when dequeueing the buffer.
1362 surface->query(NATIVE_WINDOW_TRANSFORM_HINT, &transformHint);
1363 ASSERT_EQ(ui::Transform::ROT_90, transformHint);
1364
1365 surface->unlockAndPost();
1366
1367 // After queuing the buffer, we get the updated transform hint
1368 surface->query(NATIVE_WINDOW_TRANSFORM_HINT, &transformHint);
1369 ASSERT_EQ(ui::Transform::ROT_0, transformHint);
1370
1371 adapter.waitForCallbacks();
1372}
1373
Valerie Hau5977fc82019-12-05 15:56:39 -08001374class BLASTBufferQueueTransformTest : public BLASTBufferQueueTest {
1375public:
1376 void test(uint32_t tr) {
1377 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
1378 sp<IGraphicBufferProducer> igbProducer;
1379 setUpProducer(adapter, igbProducer);
1380
1381 auto bufWidth = mDisplayWidth;
1382 auto bufHeight = mDisplayHeight;
1383 int slot;
1384 sp<Fence> fence;
1385 sp<GraphicBuffer> buf;
1386
1387 auto ret = igbProducer->dequeueBuffer(&slot, &fence, bufWidth, bufHeight,
1388 PIXEL_FORMAT_RGBA_8888, GRALLOC_USAGE_SW_WRITE_OFTEN,
1389 nullptr, nullptr);
1390 ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, ret);
1391 ASSERT_EQ(OK, igbProducer->requestBuffer(slot, &buf));
1392
1393 fillQuadrants(buf);
1394
1395 IGraphicBufferProducer::QueueBufferOutput qbOutput;
Vishnu Nair1506b182021-02-22 14:35:15 -08001396 IGraphicBufferProducer::QueueBufferInput input(systemTime(), true /* autotimestamp */,
1397 HAL_DATASPACE_UNKNOWN,
Valerie Hau5977fc82019-12-05 15:56:39 -08001398 Rect(bufWidth, bufHeight),
Vishnu Naire1a42322020-10-02 17:42:04 -07001399 NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW,
1400 tr, Fence::NO_FENCE);
Valerie Hau5977fc82019-12-05 15:56:39 -08001401 igbProducer->queueBuffer(slot, input, &qbOutput);
Dominik Laskowski718f9602019-11-09 20:01:35 -08001402 ASSERT_NE(ui::Transform::ROT_INVALID, qbOutput.transformHint);
Valerie Hau5977fc82019-12-05 15:56:39 -08001403
1404 adapter.waitForCallbacks();
chaviw8ffc7b82020-08-18 11:25:37 -07001405 ASSERT_EQ(NO_ERROR, captureDisplay(mCaptureArgs, mCaptureResults));
chaviwd2432892020-07-24 17:42:39 -07001406
Valerie Hau5977fc82019-12-05 15:56:39 -08001407 switch (tr) {
1408 case ui::Transform::ROT_0:
1409 ASSERT_NO_FATAL_FAILURE(checkScreenCapture(0, 0, 0,
1410 {0, 0, (int32_t)mDisplayWidth / 2,
1411 (int32_t)mDisplayHeight / 2},
1412 1));
1413 ASSERT_NO_FATAL_FAILURE(
1414 checkScreenCapture(255, 0, 0,
1415 {(int32_t)mDisplayWidth / 2, 0, (int32_t)mDisplayWidth,
1416 (int32_t)mDisplayHeight / 2},
1417 1));
1418 ASSERT_NO_FATAL_FAILURE(
1419 checkScreenCapture(0, 255, 0,
1420 {(int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight / 2,
1421 (int32_t)mDisplayWidth, (int32_t)mDisplayHeight},
1422 1));
1423 ASSERT_NO_FATAL_FAILURE(
1424 checkScreenCapture(0, 0, 255,
1425 {0, (int32_t)mDisplayHeight / 2,
1426 (int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight},
1427 1));
1428 break;
1429 case ui::Transform::FLIP_H:
1430 ASSERT_NO_FATAL_FAILURE(checkScreenCapture(255, 0, 0,
1431 {0, 0, (int32_t)mDisplayWidth / 2,
1432 (int32_t)mDisplayHeight / 2},
1433 1));
1434 ASSERT_NO_FATAL_FAILURE(
1435 checkScreenCapture(0, 0, 0,
1436 {(int32_t)mDisplayWidth / 2, 0, (int32_t)mDisplayWidth,
1437 (int32_t)mDisplayHeight / 2},
1438 1));
1439 ASSERT_NO_FATAL_FAILURE(
1440 checkScreenCapture(0, 0, 255,
1441 {(int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight / 2,
1442 (int32_t)mDisplayWidth, (int32_t)mDisplayHeight},
1443 1));
1444 ASSERT_NO_FATAL_FAILURE(
1445 checkScreenCapture(0, 255, 0,
1446 {0, (int32_t)mDisplayHeight / 2,
1447 (int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight},
1448 1));
1449 break;
1450 case ui::Transform::FLIP_V:
1451 ASSERT_NO_FATAL_FAILURE(checkScreenCapture(0, 0, 255,
1452 {0, 0, (int32_t)mDisplayWidth / 2,
1453 (int32_t)mDisplayHeight / 2},
1454 1));
1455 ASSERT_NO_FATAL_FAILURE(
1456 checkScreenCapture(0, 255, 0,
1457 {(int32_t)mDisplayWidth / 2, 0, (int32_t)mDisplayWidth,
1458 (int32_t)mDisplayHeight / 2},
1459 1));
1460 ASSERT_NO_FATAL_FAILURE(
1461 checkScreenCapture(255, 0, 0,
1462 {(int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight / 2,
1463 (int32_t)mDisplayWidth, (int32_t)mDisplayHeight},
1464 1));
1465 ASSERT_NO_FATAL_FAILURE(
1466 checkScreenCapture(0, 0, 0,
1467 {0, (int32_t)mDisplayHeight / 2,
1468 (int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight},
1469 1));
1470 break;
1471 case ui::Transform::ROT_90:
1472 ASSERT_NO_FATAL_FAILURE(checkScreenCapture(0, 0, 255,
1473 {0, 0, (int32_t)mDisplayWidth / 2,
1474 (int32_t)mDisplayHeight / 2},
1475 1));
1476 ASSERT_NO_FATAL_FAILURE(
1477 checkScreenCapture(0, 0, 0,
1478 {(int32_t)mDisplayWidth / 2, 0, (int32_t)mDisplayWidth,
1479 (int32_t)mDisplayHeight / 2},
1480 1));
1481 ASSERT_NO_FATAL_FAILURE(
1482 checkScreenCapture(255, 0, 0,
1483 {(int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight / 2,
1484 (int32_t)mDisplayWidth, (int32_t)mDisplayHeight},
1485 1));
1486 ASSERT_NO_FATAL_FAILURE(
1487 checkScreenCapture(0, 255, 0,
1488 {0, (int32_t)mDisplayHeight / 2,
1489 (int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight},
1490 1));
1491 break;
1492 case ui::Transform::ROT_180:
1493 ASSERT_NO_FATAL_FAILURE(checkScreenCapture(0, 255, 0,
1494 {0, 0, (int32_t)mDisplayWidth / 2,
1495 (int32_t)mDisplayHeight / 2},
1496 1));
1497 ASSERT_NO_FATAL_FAILURE(
1498 checkScreenCapture(0, 0, 255,
1499 {(int32_t)mDisplayWidth / 2, 0, (int32_t)mDisplayWidth,
1500 (int32_t)mDisplayHeight / 2},
1501 1));
1502 ASSERT_NO_FATAL_FAILURE(
1503 checkScreenCapture(0, 0, 0,
1504 {(int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight / 2,
1505 (int32_t)mDisplayWidth, (int32_t)mDisplayHeight},
1506 1));
1507 ASSERT_NO_FATAL_FAILURE(
1508 checkScreenCapture(255, 0, 0,
1509 {0, (int32_t)mDisplayHeight / 2,
1510 (int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight},
1511 1));
1512 break;
1513 case ui::Transform::ROT_270:
1514 ASSERT_NO_FATAL_FAILURE(checkScreenCapture(255, 0, 0,
1515 {0, 0, (int32_t)mDisplayWidth / 2,
1516 (int32_t)mDisplayHeight / 2},
1517 1));
1518 ASSERT_NO_FATAL_FAILURE(
1519 checkScreenCapture(0, 255, 0,
1520 {(int32_t)mDisplayWidth / 2, 0, (int32_t)mDisplayWidth,
1521 (int32_t)mDisplayHeight / 2},
1522 1));
1523 ASSERT_NO_FATAL_FAILURE(
1524 checkScreenCapture(0, 0, 255,
1525 {(int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight / 2,
1526 (int32_t)mDisplayWidth, (int32_t)mDisplayHeight},
1527 1));
1528 ASSERT_NO_FATAL_FAILURE(
1529 checkScreenCapture(0, 0, 0,
1530 {0, (int32_t)mDisplayHeight / 2,
1531 (int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight},
1532 1));
1533 }
1534 }
1535};
1536
1537TEST_F(BLASTBufferQueueTransformTest, setTransform_ROT_0) {
1538 test(ui::Transform::ROT_0);
1539}
1540
1541TEST_F(BLASTBufferQueueTransformTest, setTransform_FLIP_H) {
1542 test(ui::Transform::FLIP_H);
1543}
1544
1545TEST_F(BLASTBufferQueueTransformTest, setTransform_FLIP_V) {
1546 test(ui::Transform::FLIP_V);
1547}
1548
1549TEST_F(BLASTBufferQueueTransformTest, setTransform_ROT_90) {
1550 test(ui::Transform::ROT_90);
1551}
1552
1553TEST_F(BLASTBufferQueueTransformTest, setTransform_ROT_180) {
1554 test(ui::Transform::ROT_180);
1555}
1556
1557TEST_F(BLASTBufferQueueTransformTest, setTransform_ROT_270) {
1558 test(ui::Transform::ROT_270);
1559}
Valerie Hau871d6352020-01-29 08:44:02 -08001560
1561class BLASTFrameEventHistoryTest : public BLASTBufferQueueTest {
1562public:
1563 void setUpAndQueueBuffer(const sp<IGraphicBufferProducer>& igbProducer,
Vishnu Nairde66dc72021-06-17 17:54:41 -07001564 nsecs_t* outRequestedPresentTime, nsecs_t* postedTime,
Valerie Hau871d6352020-01-29 08:44:02 -08001565 IGraphicBufferProducer::QueueBufferOutput* qbOutput,
Vishnu Nairde66dc72021-06-17 17:54:41 -07001566 bool getFrameTimestamps, nsecs_t requestedPresentTime = systemTime()) {
Valerie Hau871d6352020-01-29 08:44:02 -08001567 int slot;
1568 sp<Fence> fence;
1569 sp<GraphicBuffer> buf;
1570 auto ret = igbProducer->dequeueBuffer(&slot, &fence, mDisplayWidth, mDisplayHeight,
1571 PIXEL_FORMAT_RGBA_8888, GRALLOC_USAGE_SW_WRITE_OFTEN,
1572 nullptr, nullptr);
Vishnu Nairde66dc72021-06-17 17:54:41 -07001573 if (IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION == ret) {
1574 ASSERT_EQ(OK, igbProducer->requestBuffer(slot, &buf));
1575 }
Valerie Hau871d6352020-01-29 08:44:02 -08001576
Vishnu Nairde66dc72021-06-17 17:54:41 -07001577 *outRequestedPresentTime = requestedPresentTime;
1578 IGraphicBufferProducer::QueueBufferInput input(requestedPresentTime, false,
1579 HAL_DATASPACE_UNKNOWN,
Valerie Hau871d6352020-01-29 08:44:02 -08001580 Rect(mDisplayWidth, mDisplayHeight),
1581 NATIVE_WINDOW_SCALING_MODE_FREEZE, 0,
1582 Fence::NO_FENCE, /*sticky*/ 0,
1583 getFrameTimestamps);
1584 if (postedTime) *postedTime = systemTime();
1585 igbProducer->queueBuffer(slot, input, qbOutput);
1586 }
Vishnu Nair083efd32021-02-12 09:32:30 -08001587 sp<SurfaceControl> mBufferQueueSurfaceControl;
Valerie Hau871d6352020-01-29 08:44:02 -08001588};
1589
1590TEST_F(BLASTFrameEventHistoryTest, FrameEventHistory_Basic) {
1591 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
1592 sp<IGraphicBufferProducer> igbProducer;
1593 ProducerFrameEventHistory history;
1594 setUpProducer(adapter, igbProducer);
1595
1596 IGraphicBufferProducer::QueueBufferOutput qbOutput;
1597 nsecs_t requestedPresentTimeA = 0;
1598 nsecs_t postedTimeA = 0;
1599 setUpAndQueueBuffer(igbProducer, &requestedPresentTimeA, &postedTimeA, &qbOutput, true);
1600 history.applyDelta(qbOutput.frameTimestamps);
1601
1602 FrameEvents* events = nullptr;
1603 events = history.getFrame(1);
1604 ASSERT_NE(nullptr, events);
1605 ASSERT_EQ(1, events->frameNumber);
1606 ASSERT_EQ(requestedPresentTimeA, events->requestedPresentTime);
1607 ASSERT_GE(events->postedTime, postedTimeA);
1608
Vishnu Nair1506b182021-02-22 14:35:15 -08001609 adapter.waitForCallback(1);
Valerie Hau871d6352020-01-29 08:44:02 -08001610
1611 // queue another buffer so we query for frame event deltas
1612 nsecs_t requestedPresentTimeB = 0;
1613 nsecs_t postedTimeB = 0;
1614 setUpAndQueueBuffer(igbProducer, &requestedPresentTimeB, &postedTimeB, &qbOutput, true);
1615 history.applyDelta(qbOutput.frameTimestamps);
1616 events = history.getFrame(1);
1617 ASSERT_NE(nullptr, events);
1618
1619 // frame number, requestedPresentTime, and postTime should not have changed
1620 ASSERT_EQ(1, events->frameNumber);
1621 ASSERT_EQ(requestedPresentTimeA, events->requestedPresentTime);
1622 ASSERT_GE(events->postedTime, postedTimeA);
1623
1624 ASSERT_GE(events->latchTime, postedTimeA);
1625 ASSERT_GE(events->dequeueReadyTime, events->latchTime);
1626 ASSERT_NE(nullptr, events->gpuCompositionDoneFence);
1627 ASSERT_NE(nullptr, events->displayPresentFence);
1628 ASSERT_NE(nullptr, events->releaseFence);
1629
1630 // we should also have gotten the initial values for the next frame
1631 events = history.getFrame(2);
1632 ASSERT_NE(nullptr, events);
1633 ASSERT_EQ(2, events->frameNumber);
1634 ASSERT_EQ(requestedPresentTimeB, events->requestedPresentTime);
1635 ASSERT_GE(events->postedTime, postedTimeB);
Valerie Hau78491e92020-04-15 13:10:56 -07001636
1637 // wait for any callbacks that have not been received
1638 adapter.waitForCallbacks();
Valerie Hau871d6352020-01-29 08:44:02 -08001639}
Vishnu Nair083efd32021-02-12 09:32:30 -08001640
Vishnu Nair083efd32021-02-12 09:32:30 -08001641TEST_F(BLASTFrameEventHistoryTest, FrameEventHistory_DroppedFrame) {
1642 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
1643 sp<IGraphicBufferProducer> igbProducer;
1644 setUpProducer(adapter, igbProducer);
1645
1646 ProducerFrameEventHistory history;
1647 IGraphicBufferProducer::QueueBufferOutput qbOutput;
1648 nsecs_t requestedPresentTimeA = 0;
1649 nsecs_t postedTimeA = 0;
Vishnu Nairde66dc72021-06-17 17:54:41 -07001650 // Present the frame sometime in the future so we can add two frames to the queue so the older
1651 // one will be dropped.
1652 nsecs_t presentTime = systemTime() + std::chrono::nanoseconds(500ms).count();
Vishnu Nair083efd32021-02-12 09:32:30 -08001653 setUpAndQueueBuffer(igbProducer, &requestedPresentTimeA, &postedTimeA, &qbOutput, true,
Vishnu Nairde66dc72021-06-17 17:54:41 -07001654 presentTime);
Vishnu Nair083efd32021-02-12 09:32:30 -08001655 history.applyDelta(qbOutput.frameTimestamps);
1656
1657 FrameEvents* events = nullptr;
1658 events = history.getFrame(1);
1659 ASSERT_NE(nullptr, events);
1660 ASSERT_EQ(1, events->frameNumber);
1661 ASSERT_EQ(requestedPresentTimeA, events->requestedPresentTime);
1662 ASSERT_GE(events->postedTime, postedTimeA);
1663
1664 // queue another buffer so the first can be dropped
1665 nsecs_t requestedPresentTimeB = 0;
1666 nsecs_t postedTimeB = 0;
Vishnu Nairde66dc72021-06-17 17:54:41 -07001667 presentTime = systemTime() + std::chrono::nanoseconds(1ms).count();
1668 setUpAndQueueBuffer(igbProducer, &requestedPresentTimeB, &postedTimeB, &qbOutput, true,
1669 presentTime);
Vishnu Nair083efd32021-02-12 09:32:30 -08001670 history.applyDelta(qbOutput.frameTimestamps);
1671 events = history.getFrame(1);
1672 ASSERT_NE(nullptr, events);
1673
1674 // frame number, requestedPresentTime, and postTime should not have changed
1675 ASSERT_EQ(1, events->frameNumber);
1676 ASSERT_EQ(requestedPresentTimeA, events->requestedPresentTime);
1677 ASSERT_GE(events->postedTime, postedTimeA);
1678
Vishnu Nairde66dc72021-06-17 17:54:41 -07001679 // a valid latchtime and pre and post composition info should not be set for the dropped frame
Vishnu Nair083efd32021-02-12 09:32:30 -08001680 ASSERT_FALSE(events->hasLatchInfo());
1681 ASSERT_FALSE(events->hasDequeueReadyInfo());
Vishnu Nairde66dc72021-06-17 17:54:41 -07001682 ASSERT_FALSE(events->hasGpuCompositionDoneInfo());
1683 ASSERT_FALSE(events->hasDisplayPresentInfo());
1684 ASSERT_FALSE(events->hasReleaseInfo());
Vishnu Nair083efd32021-02-12 09:32:30 -08001685
Vishnu Nairde66dc72021-06-17 17:54:41 -07001686 // wait for the last transaction to be completed.
1687 adapter.waitForCallback(2);
Vishnu Nair083efd32021-02-12 09:32:30 -08001688
Vishnu Nairde66dc72021-06-17 17:54:41 -07001689 // queue another buffer so we query for frame event deltas
1690 nsecs_t requestedPresentTimeC = 0;
1691 nsecs_t postedTimeC = 0;
1692 setUpAndQueueBuffer(igbProducer, &requestedPresentTimeC, &postedTimeC, &qbOutput, true);
1693 history.applyDelta(qbOutput.frameTimestamps);
1694
1695 // frame number, requestedPresentTime, and postTime should not have changed
1696 ASSERT_EQ(1, events->frameNumber);
1697 ASSERT_EQ(requestedPresentTimeA, events->requestedPresentTime);
1698 ASSERT_GE(events->postedTime, postedTimeA);
1699
1700 // a valid latchtime and pre and post composition info should not be set for the dropped frame
1701 ASSERT_FALSE(events->hasLatchInfo());
1702 ASSERT_FALSE(events->hasDequeueReadyInfo());
1703 ASSERT_FALSE(events->hasGpuCompositionDoneInfo());
1704 ASSERT_FALSE(events->hasDisplayPresentInfo());
1705 ASSERT_FALSE(events->hasReleaseInfo());
1706
1707 // we should also have gotten values for the presented frame
Vishnu Nair083efd32021-02-12 09:32:30 -08001708 events = history.getFrame(2);
1709 ASSERT_NE(nullptr, events);
1710 ASSERT_EQ(2, events->frameNumber);
1711 ASSERT_EQ(requestedPresentTimeB, events->requestedPresentTime);
1712 ASSERT_GE(events->postedTime, postedTimeB);
Vishnu Nairde66dc72021-06-17 17:54:41 -07001713 ASSERT_GE(events->latchTime, postedTimeB);
1714 ASSERT_GE(events->dequeueReadyTime, events->latchTime);
1715 ASSERT_NE(nullptr, events->gpuCompositionDoneFence);
1716 ASSERT_NE(nullptr, events->displayPresentFence);
1717 ASSERT_NE(nullptr, events->releaseFence);
1718
1719 // wait for any callbacks that have not been received
1720 adapter.waitForCallbacks();
Vishnu Nair083efd32021-02-12 09:32:30 -08001721}
1722
Vishnu Nair9a69a042021-06-18 13:19:49 -07001723TEST_F(BLASTFrameEventHistoryTest, FrameEventHistory_CompositorTimings) {
1724 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
1725 sp<IGraphicBufferProducer> igbProducer;
1726 ProducerFrameEventHistory history;
1727 setUpProducer(adapter, igbProducer);
1728
1729 IGraphicBufferProducer::QueueBufferOutput qbOutput;
1730 nsecs_t requestedPresentTimeA = 0;
1731 nsecs_t postedTimeA = 0;
Vishnu Nair9a69a042021-06-18 13:19:49 -07001732 setUpAndQueueBuffer(igbProducer, &requestedPresentTimeA, &postedTimeA, &qbOutput, true);
1733 history.applyDelta(qbOutput.frameTimestamps);
1734 adapter.waitForCallback(1);
1735
1736 // queue another buffer so we query for frame event deltas
1737 nsecs_t requestedPresentTimeB = 0;
1738 nsecs_t postedTimeB = 0;
1739 setUpAndQueueBuffer(igbProducer, &requestedPresentTimeB, &postedTimeB, &qbOutput, true);
1740 history.applyDelta(qbOutput.frameTimestamps);
1741
1742 // check for a valid compositor deadline
1743 ASSERT_NE(0, history.getReportedCompositeDeadline());
1744
1745 // wait for any callbacks that have not been received
1746 adapter.waitForCallbacks();
1747}
1748
Valerie Hauc5011f92019-10-11 09:52:07 -07001749} // namespace android