blob: 3a9b2b8dcdb847c36812adc2492e63be9b3233cb [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
Valerie Hauc5011f92019-10-11 09:52:07 -0700165private:
chaviwf10b9042021-10-13 15:48:59 -0500166 sp<TestBLASTBufferQueue> mBlastBufferQueueAdapter;
Valerie Hauc5011f92019-10-11 09:52:07 -0700167};
168
169class BLASTBufferQueueTest : public ::testing::Test {
170public:
171protected:
172 BLASTBufferQueueTest() {
173 const ::testing::TestInfo* const testInfo =
174 ::testing::UnitTest::GetInstance()->current_test_info();
175 ALOGV("Begin test: %s.%s", testInfo->test_case_name(), testInfo->name());
176 }
177
178 ~BLASTBufferQueueTest() {
179 const ::testing::TestInfo* const testInfo =
180 ::testing::UnitTest::GetInstance()->current_test_info();
181 ALOGV("End test: %s.%s", testInfo->test_case_name(), testInfo->name());
182 }
183
184 void SetUp() {
Valerie Hauda3446e2019-10-14 15:49:22 -0700185 mComposer = ComposerService::getComposerService();
Valerie Hauc5011f92019-10-11 09:52:07 -0700186 mClient = new SurfaceComposerClient();
Valerie Hauda3446e2019-10-14 15:49:22 -0700187 mDisplayToken = mClient->getInternalDisplayToken();
188 ASSERT_NE(nullptr, mDisplayToken.get());
189 Transaction t;
Dominik Laskowski29fa1462021-04-27 15:51:50 -0700190 t.setDisplayLayerStack(mDisplayToken, ui::DEFAULT_LAYER_STACK);
Valerie Hauda3446e2019-10-14 15:49:22 -0700191 t.apply();
192 t.clear();
193
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100194 ui::DisplayMode mode;
195 ASSERT_EQ(NO_ERROR, SurfaceComposerClient::getActiveDisplayMode(mDisplayToken, &mode));
196 const ui::Size& resolution = mode.resolution;
Dominik Laskowski3cb3d4e2019-11-21 11:14:45 -0800197 mDisplayWidth = resolution.getWidth();
198 mDisplayHeight = resolution.getHeight();
Valerie Hauda3446e2019-10-14 15:49:22 -0700199
200 mSurfaceControl = mClient->createSurface(String8("TestSurface"), mDisplayWidth,
201 mDisplayHeight, PIXEL_FORMAT_RGBA_8888,
202 ISurfaceComposerClient::eFXSurfaceBufferState,
203 /*parent*/ nullptr);
Dominik Laskowski29fa1462021-04-27 15:51:50 -0700204 t.setLayerStack(mSurfaceControl, ui::DEFAULT_LAYER_STACK)
Valerie Hauda3446e2019-10-14 15:49:22 -0700205 .setLayer(mSurfaceControl, std::numeric_limits<int32_t>::max())
Valerie Hauda3446e2019-10-14 15:49:22 -0700206 .show(mSurfaceControl)
207 .setDataspace(mSurfaceControl, ui::Dataspace::V0_SRGB)
208 .apply();
chaviwd2432892020-07-24 17:42:39 -0700209
210 mCaptureArgs.displayToken = mDisplayToken;
arthurhung6fa58b72020-11-05 11:56:00 +0800211 mCaptureArgs.dataspace = ui::Dataspace::V0_SRGB;
Valerie Hauda3446e2019-10-14 15:49:22 -0700212 }
213
chaviwd7deef72021-10-06 11:53:40 -0500214 void setUpProducer(BLASTBufferQueueHelper& adapter, sp<IGraphicBufferProducer>& producer,
215 int32_t maxBufferCount = 2) {
Vishnu Nair083efd32021-02-12 09:32:30 -0800216 producer = adapter.getIGraphicBufferProducer();
chaviwd7deef72021-10-06 11:53:40 -0500217 setUpProducer(producer, maxBufferCount);
Vishnu Nair083efd32021-02-12 09:32:30 -0800218 }
219
chaviwd7deef72021-10-06 11:53:40 -0500220 void setUpProducer(sp<IGraphicBufferProducer>& igbProducer, int32_t maxBufferCount) {
Valerie Haud3b90d22019-11-06 09:37:31 -0800221 ASSERT_NE(nullptr, igbProducer.get());
chaviwd7deef72021-10-06 11:53:40 -0500222 ASSERT_EQ(NO_ERROR, igbProducer->setMaxDequeuedBufferCount(maxBufferCount));
Valerie Haud3b90d22019-11-06 09:37:31 -0800223 IGraphicBufferProducer::QueueBufferOutput qbOutput;
chaviwd7deef72021-10-06 11:53:40 -0500224 mProducerListener = new CountProducerListener();
Valerie Haud3b90d22019-11-06 09:37:31 -0800225 ASSERT_EQ(NO_ERROR,
chaviwd7deef72021-10-06 11:53:40 -0500226 igbProducer->connect(mProducerListener, NATIVE_WINDOW_API_CPU, false, &qbOutput));
Dominik Laskowski718f9602019-11-09 20:01:35 -0800227 ASSERT_NE(ui::Transform::ROT_INVALID, qbOutput.transformHint);
Valerie Haud3b90d22019-11-06 09:37:31 -0800228 }
229
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800230 void fillBuffer(uint32_t* bufData, Rect rect, uint32_t stride, uint8_t r, uint8_t g,
231 uint8_t b) {
232 for (uint32_t row = rect.top; row < rect.bottom; row++) {
233 for (uint32_t col = rect.left; col < rect.right; col++) {
Valerie Hauda3446e2019-10-14 15:49:22 -0700234 uint8_t* pixel = (uint8_t*)(bufData + (row * stride) + col);
235 *pixel = r;
236 *(pixel + 1) = g;
237 *(pixel + 2) = b;
238 *(pixel + 3) = 255;
239 }
240 }
241 }
242
Valerie Hau5977fc82019-12-05 15:56:39 -0800243 void fillQuadrants(sp<GraphicBuffer>& buf) {
244 const auto bufWidth = buf->getWidth();
245 const auto bufHeight = buf->getHeight();
246 uint32_t* bufData;
247 buf->lock(static_cast<uint32_t>(GraphicBuffer::USAGE_SW_WRITE_OFTEN),
248 reinterpret_cast<void**>(&bufData));
249 fillBuffer(bufData, Rect(0, 0, bufWidth / 2, bufHeight / 2), buf->getStride(), 0, 0, 0);
250 fillBuffer(bufData, Rect(bufWidth / 2, 0, bufWidth, bufHeight / 2), buf->getStride(), 255,
251 0, 0);
252 fillBuffer(bufData, Rect(bufWidth / 2, bufHeight / 2, bufWidth, bufHeight),
253 buf->getStride(), 0, 255, 0);
254 fillBuffer(bufData, Rect(0, bufHeight / 2, bufWidth / 2, bufHeight), buf->getStride(), 0, 0,
255 255);
256 buf->unlock();
257 }
258
259 void checkScreenCapture(uint8_t r, uint8_t g, uint8_t b, Rect region, int32_t border = 0,
260 bool outsideRegion = false) {
chaviwd2432892020-07-24 17:42:39 -0700261 sp<GraphicBuffer>& captureBuf = mCaptureResults.buffer;
Valerie Hau5977fc82019-12-05 15:56:39 -0800262 const auto epsilon = 3;
chaviwd2432892020-07-24 17:42:39 -0700263 const auto width = captureBuf->getWidth();
264 const auto height = captureBuf->getHeight();
265 const auto stride = captureBuf->getStride();
Valerie Hauda3446e2019-10-14 15:49:22 -0700266
267 uint32_t* bufData;
chaviwd2432892020-07-24 17:42:39 -0700268 captureBuf->lock(static_cast<uint32_t>(GraphicBuffer::USAGE_SW_READ_OFTEN),
269 reinterpret_cast<void**>(&bufData));
Valerie Hauda3446e2019-10-14 15:49:22 -0700270
271 for (uint32_t row = 0; row < height; row++) {
272 for (uint32_t col = 0; col < width; col++) {
273 uint8_t* pixel = (uint8_t*)(bufData + (row * stride) + col);
arthurhung6fa58b72020-11-05 11:56:00 +0800274 ASSERT_NE(nullptr, pixel);
Valerie Hau5977fc82019-12-05 15:56:39 -0800275 bool inRegion;
276 if (!outsideRegion) {
277 inRegion = row >= region.top + border && row < region.bottom - border &&
278 col >= region.left + border && col < region.right - border;
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800279 } else {
Valerie Hau5977fc82019-12-05 15:56:39 -0800280 inRegion = row >= region.top - border && row < region.bottom + border &&
281 col >= region.left - border && col < region.right + border;
282 }
283 if (!outsideRegion && inRegion) {
Chavi Weingartena5aedbd2021-04-09 13:37:33 +0000284 ASSERT_GE(epsilon, abs(r - *(pixel)));
285 ASSERT_GE(epsilon, abs(g - *(pixel + 1)));
286 ASSERT_GE(epsilon, abs(b - *(pixel + 2)));
Valerie Hau5977fc82019-12-05 15:56:39 -0800287 } else if (outsideRegion && !inRegion) {
Chavi Weingartena5aedbd2021-04-09 13:37:33 +0000288 ASSERT_GE(epsilon, abs(r - *(pixel)));
289 ASSERT_GE(epsilon, abs(g - *(pixel + 1)));
290 ASSERT_GE(epsilon, abs(b - *(pixel + 2)));
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800291 }
Vishnu Nair1506b182021-02-22 14:35:15 -0800292 ASSERT_EQ(false, ::testing::Test::HasFailure());
Valerie Hauda3446e2019-10-14 15:49:22 -0700293 }
294 }
chaviwd2432892020-07-24 17:42:39 -0700295 captureBuf->unlock();
Valerie Hauc5011f92019-10-11 09:52:07 -0700296 }
297
chaviw8ffc7b82020-08-18 11:25:37 -0700298 static status_t captureDisplay(DisplayCaptureArgs& captureArgs,
299 ScreenCaptureResults& captureResults) {
Huihong Luo9e84f332021-12-16 14:33:46 -0800300 const auto sf = ComposerServiceAIDL::getComposerService();
chaviw8ffc7b82020-08-18 11:25:37 -0700301 SurfaceComposerClient::Transaction().apply(true);
302
303 const sp<SyncScreenCaptureListener> captureListener = new SyncScreenCaptureListener();
Huihong Luo9e84f332021-12-16 14:33:46 -0800304 binder::Status status = sf->captureDisplay(captureArgs, captureListener);
Huihong Luo3bdef862022-03-03 11:57:19 -0800305 status_t err = gui::aidl_utils::statusTFromBinderStatus(status);
306 if (err != NO_ERROR) {
307 return err;
chaviw8ffc7b82020-08-18 11:25:37 -0700308 }
309 captureResults = captureListener->waitForResults();
310 return captureResults.result;
311 }
312
Vishnu Nair277142c2021-01-05 18:35:29 -0800313 void queueBuffer(sp<IGraphicBufferProducer> igbp, uint8_t r, uint8_t g, uint8_t b,
314 nsecs_t presentTimeDelay) {
315 int slot;
316 sp<Fence> fence;
317 sp<GraphicBuffer> buf;
318 auto ret = igbp->dequeueBuffer(&slot, &fence, mDisplayWidth, mDisplayHeight,
319 PIXEL_FORMAT_RGBA_8888, GRALLOC_USAGE_SW_WRITE_OFTEN,
320 nullptr, nullptr);
chaviw0acd33a2021-11-02 11:55:37 -0500321 ASSERT_TRUE(ret == IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION || ret == NO_ERROR);
Vishnu Nair277142c2021-01-05 18:35:29 -0800322 ASSERT_EQ(OK, igbp->requestBuffer(slot, &buf));
323
324 uint32_t* bufData;
325 buf->lock(static_cast<uint32_t>(GraphicBuffer::USAGE_SW_WRITE_OFTEN),
326 reinterpret_cast<void**>(&bufData));
327 fillBuffer(bufData, Rect(buf->getWidth(), buf->getHeight() / 2), buf->getStride(), r, g, b);
328 buf->unlock();
329
330 IGraphicBufferProducer::QueueBufferOutput qbOutput;
331 nsecs_t timestampNanos = systemTime() + presentTimeDelay;
332 IGraphicBufferProducer::QueueBufferInput input(timestampNanos, false, HAL_DATASPACE_UNKNOWN,
333 Rect(mDisplayWidth, mDisplayHeight / 2),
334 NATIVE_WINDOW_SCALING_MODE_FREEZE, 0,
335 Fence::NO_FENCE);
336 igbp->queueBuffer(slot, input, &qbOutput);
337 }
338
Valerie Hauc5011f92019-10-11 09:52:07 -0700339 sp<SurfaceComposerClient> mClient;
Valerie Hauda3446e2019-10-14 15:49:22 -0700340 sp<ISurfaceComposer> mComposer;
341
342 sp<IBinder> mDisplayToken;
343
Valerie Hauc5011f92019-10-11 09:52:07 -0700344 sp<SurfaceControl> mSurfaceControl;
Valerie Hauda3446e2019-10-14 15:49:22 -0700345
346 uint32_t mDisplayWidth;
347 uint32_t mDisplayHeight;
chaviwd2432892020-07-24 17:42:39 -0700348
349 DisplayCaptureArgs mCaptureArgs;
350 ScreenCaptureResults mCaptureResults;
chaviwd7deef72021-10-06 11:53:40 -0500351 sp<CountProducerListener> mProducerListener;
Valerie Hauc5011f92019-10-11 09:52:07 -0700352};
353
354TEST_F(BLASTBufferQueueTest, CreateBLASTBufferQueue) {
355 // create BLASTBufferQueue adapter associated with this surface
Valerie Hauda3446e2019-10-14 15:49:22 -0700356 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
Valerie Hauc5011f92019-10-11 09:52:07 -0700357 ASSERT_EQ(mSurfaceControl, adapter.getSurfaceControl());
Valerie Hauda3446e2019-10-14 15:49:22 -0700358 ASSERT_EQ(mDisplayWidth, adapter.getWidth());
359 ASSERT_EQ(mDisplayHeight, adapter.getHeight());
Tianhao Yao4861b102022-02-03 20:18:35 +0000360 ASSERT_EQ(nullptr, adapter.getTransactionReadyCallback());
Valerie Hauc5011f92019-10-11 09:52:07 -0700361}
362
363TEST_F(BLASTBufferQueueTest, Update) {
Valerie Hauda3446e2019-10-14 15:49:22 -0700364 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
Valerie Hauc5011f92019-10-11 09:52:07 -0700365 sp<SurfaceControl> updateSurface =
Valerie Hauda3446e2019-10-14 15:49:22 -0700366 mClient->createSurface(String8("UpdateTest"), mDisplayWidth / 2, mDisplayHeight / 2,
367 PIXEL_FORMAT_RGBA_8888);
368 adapter.update(updateSurface, mDisplayWidth / 2, mDisplayHeight / 2);
Valerie Hauc5011f92019-10-11 09:52:07 -0700369 ASSERT_EQ(updateSurface, adapter.getSurfaceControl());
Vishnu Nairea0de002020-11-17 17:42:37 -0800370 sp<IGraphicBufferProducer> igbProducer;
371 setUpProducer(adapter, igbProducer);
372
373 int32_t width;
374 igbProducer->query(NATIVE_WINDOW_WIDTH, &width);
375 ASSERT_EQ(mDisplayWidth / 2, width);
376 int32_t height;
377 igbProducer->query(NATIVE_WINDOW_HEIGHT, &height);
378 ASSERT_EQ(mDisplayHeight / 2, height);
Valerie Hauc5011f92019-10-11 09:52:07 -0700379}
380
Tianhao Yao4861b102022-02-03 20:18:35 +0000381TEST_F(BLASTBufferQueueTest, SyncNextTransaction) {
Valerie Hauda3446e2019-10-14 15:49:22 -0700382 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
Tianhao Yao4861b102022-02-03 20:18:35 +0000383 ASSERT_EQ(nullptr, adapter.getTransactionReadyCallback());
384 auto callback = [](Transaction*) {};
385 adapter.syncNextTransaction(callback);
386 ASSERT_NE(nullptr, adapter.getTransactionReadyCallback());
Valerie Hauc5011f92019-10-11 09:52:07 -0700387}
Valerie Hauda3446e2019-10-14 15:49:22 -0700388
Valerie Haubf29e042020-02-06 11:40:38 -0800389TEST_F(BLASTBufferQueueTest, DISABLED_onFrameAvailable_ApplyDesiredPresentTime) {
Valerie Hau181abd32020-01-27 14:18:28 -0800390 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
391 sp<IGraphicBufferProducer> igbProducer;
392 setUpProducer(adapter, igbProducer);
393
394 int slot;
395 sp<Fence> fence;
396 sp<GraphicBuffer> buf;
397 auto ret = igbProducer->dequeueBuffer(&slot, &fence, mDisplayWidth, mDisplayHeight,
398 PIXEL_FORMAT_RGBA_8888, GRALLOC_USAGE_SW_WRITE_OFTEN,
399 nullptr, nullptr);
400 ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, ret);
401 ASSERT_EQ(OK, igbProducer->requestBuffer(slot, &buf));
402
403 nsecs_t desiredPresentTime = systemTime() + nsecs_t(5 * 1e8);
404 IGraphicBufferProducer::QueueBufferOutput qbOutput;
Vishnu Nair1506b182021-02-22 14:35:15 -0800405 IGraphicBufferProducer::QueueBufferInput input(desiredPresentTime, true /* autotimestamp */,
406 HAL_DATASPACE_UNKNOWN,
Valerie Hau181abd32020-01-27 14:18:28 -0800407 Rect(mDisplayWidth, mDisplayHeight),
408 NATIVE_WINDOW_SCALING_MODE_FREEZE, 0,
409 Fence::NO_FENCE);
410 igbProducer->queueBuffer(slot, input, &qbOutput);
411 ASSERT_NE(ui::Transform::ROT_INVALID, qbOutput.transformHint);
412
413 adapter.waitForCallbacks();
414 ASSERT_GE(systemTime(), desiredPresentTime);
415}
416
Valerie Hauda3446e2019-10-14 15:49:22 -0700417TEST_F(BLASTBufferQueueTest, onFrameAvailable_Apply) {
418 uint8_t r = 255;
419 uint8_t g = 0;
420 uint8_t b = 0;
421
422 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
Valerie Haud3b90d22019-11-06 09:37:31 -0800423 sp<IGraphicBufferProducer> igbProducer;
424 setUpProducer(adapter, igbProducer);
Valerie Hauda3446e2019-10-14 15:49:22 -0700425
426 int slot;
427 sp<Fence> fence;
428 sp<GraphicBuffer> buf;
429 auto ret = igbProducer->dequeueBuffer(&slot, &fence, mDisplayWidth, mDisplayHeight,
430 PIXEL_FORMAT_RGBA_8888, GRALLOC_USAGE_SW_WRITE_OFTEN,
431 nullptr, nullptr);
432 ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, ret);
433 ASSERT_EQ(OK, igbProducer->requestBuffer(slot, &buf));
434
435 uint32_t* bufData;
436 buf->lock(static_cast<uint32_t>(GraphicBuffer::USAGE_SW_WRITE_OFTEN),
437 reinterpret_cast<void**>(&bufData));
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800438 fillBuffer(bufData, Rect(buf->getWidth(), buf->getHeight()), buf->getStride(), r, g, b);
Valerie Hauda3446e2019-10-14 15:49:22 -0700439 buf->unlock();
440
Valerie Haud3b90d22019-11-06 09:37:31 -0800441 IGraphicBufferProducer::QueueBufferOutput qbOutput;
Vishnu Nair1506b182021-02-22 14:35:15 -0800442 IGraphicBufferProducer::QueueBufferInput input(systemTime(), true /* autotimestamp */,
443 HAL_DATASPACE_UNKNOWN,
Valerie Hauda3446e2019-10-14 15:49:22 -0700444 Rect(mDisplayWidth, mDisplayHeight),
445 NATIVE_WINDOW_SCALING_MODE_FREEZE, 0,
446 Fence::NO_FENCE);
447 igbProducer->queueBuffer(slot, input, &qbOutput);
Dominik Laskowski718f9602019-11-09 20:01:35 -0800448 ASSERT_NE(ui::Transform::ROT_INVALID, qbOutput.transformHint);
Valerie Hauda3446e2019-10-14 15:49:22 -0700449
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800450 adapter.waitForCallbacks();
Valerie Hauda3446e2019-10-14 15:49:22 -0700451
452 // capture screen and verify that it is red
chaviw8ffc7b82020-08-18 11:25:37 -0700453 ASSERT_EQ(NO_ERROR, captureDisplay(mCaptureArgs, mCaptureResults));
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800454 ASSERT_NO_FATAL_FAILURE(
455 checkScreenCapture(r, g, b, {0, 0, (int32_t)mDisplayWidth, (int32_t)mDisplayHeight}));
Valerie Hauda3446e2019-10-14 15:49:22 -0700456}
Valerie Haud3b90d22019-11-06 09:37:31 -0800457
458TEST_F(BLASTBufferQueueTest, TripleBuffering) {
459 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
460 sp<IGraphicBufferProducer> igbProducer;
461 setUpProducer(adapter, igbProducer);
462
463 std::vector<std::pair<int, sp<Fence>>> allocated;
Ady Abraham0bde6b52021-05-18 13:57:02 -0700464 int minUndequeuedBuffers = 0;
465 ASSERT_EQ(OK, igbProducer->query(NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS, &minUndequeuedBuffers));
466 const auto bufferCount = minUndequeuedBuffers + 2;
467
468 for (int i = 0; i < bufferCount; i++) {
Valerie Haud3b90d22019-11-06 09:37:31 -0800469 int slot;
470 sp<Fence> fence;
471 sp<GraphicBuffer> buf;
472 auto ret = igbProducer->dequeueBuffer(&slot, &fence, mDisplayWidth, mDisplayHeight,
473 PIXEL_FORMAT_RGBA_8888, GRALLOC_USAGE_SW_WRITE_OFTEN,
474 nullptr, nullptr);
475 ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, ret);
476 ASSERT_EQ(OK, igbProducer->requestBuffer(slot, &buf));
477 allocated.push_back({slot, fence});
478 }
479 for (int i = 0; i < allocated.size(); i++) {
480 igbProducer->cancelBuffer(allocated[i].first, allocated[i].second);
481 }
482
Valerie Haua32c5522019-12-09 10:11:08 -0800483 for (int i = 0; i < 100; i++) {
Valerie Haud3b90d22019-11-06 09:37:31 -0800484 int slot;
485 sp<Fence> fence;
486 sp<GraphicBuffer> buf;
487 auto ret = igbProducer->dequeueBuffer(&slot, &fence, mDisplayWidth, mDisplayHeight,
488 PIXEL_FORMAT_RGBA_8888, GRALLOC_USAGE_SW_WRITE_OFTEN,
489 nullptr, nullptr);
490 ASSERT_EQ(NO_ERROR, ret);
491 IGraphicBufferProducer::QueueBufferOutput qbOutput;
Vishnu Nair1506b182021-02-22 14:35:15 -0800492 IGraphicBufferProducer::QueueBufferInput input(systemTime(), true /* autotimestamp */,
493 HAL_DATASPACE_UNKNOWN,
Valerie Haud3b90d22019-11-06 09:37:31 -0800494 Rect(mDisplayWidth, mDisplayHeight),
495 NATIVE_WINDOW_SCALING_MODE_FREEZE, 0,
496 Fence::NO_FENCE);
497 igbProducer->queueBuffer(slot, input, &qbOutput);
498 }
499 adapter.waitForCallbacks();
500}
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800501
502TEST_F(BLASTBufferQueueTest, SetCrop_Item) {
503 uint8_t r = 255;
504 uint8_t g = 0;
505 uint8_t b = 0;
506
507 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
508 sp<IGraphicBufferProducer> igbProducer;
509 setUpProducer(adapter, igbProducer);
510 int slot;
511 sp<Fence> fence;
512 sp<GraphicBuffer> buf;
513 auto ret = igbProducer->dequeueBuffer(&slot, &fence, mDisplayWidth, mDisplayHeight,
514 PIXEL_FORMAT_RGBA_8888, GRALLOC_USAGE_SW_WRITE_OFTEN,
515 nullptr, nullptr);
516 ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, ret);
517 ASSERT_EQ(OK, igbProducer->requestBuffer(slot, &buf));
518
519 uint32_t* bufData;
520 buf->lock(static_cast<uint32_t>(GraphicBuffer::USAGE_SW_WRITE_OFTEN),
521 reinterpret_cast<void**>(&bufData));
522 fillBuffer(bufData, Rect(buf->getWidth(), buf->getHeight() / 2), buf->getStride(), r, g, b);
523 buf->unlock();
524
525 IGraphicBufferProducer::QueueBufferOutput qbOutput;
Vishnu Nair1506b182021-02-22 14:35:15 -0800526 IGraphicBufferProducer::QueueBufferInput input(systemTime(), true /* autotimestamp */,
527 HAL_DATASPACE_UNKNOWN,
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800528 Rect(mDisplayWidth, mDisplayHeight / 2),
529 NATIVE_WINDOW_SCALING_MODE_FREEZE, 0,
530 Fence::NO_FENCE);
531 igbProducer->queueBuffer(slot, input, &qbOutput);
Dominik Laskowski718f9602019-11-09 20:01:35 -0800532 ASSERT_NE(ui::Transform::ROT_INVALID, qbOutput.transformHint);
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800533
534 adapter.waitForCallbacks();
535 // capture screen and verify that it is red
chaviw8ffc7b82020-08-18 11:25:37 -0700536 ASSERT_EQ(NO_ERROR, captureDisplay(mCaptureArgs, mCaptureResults));
chaviwd2432892020-07-24 17:42:39 -0700537
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800538 ASSERT_NO_FATAL_FAILURE(
Chavi Weingartena5aedbd2021-04-09 13:37:33 +0000539 checkScreenCapture(r, g, b,
540 {0, 0, (int32_t)mDisplayWidth, (int32_t)mDisplayHeight / 2}));
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800541}
542
543TEST_F(BLASTBufferQueueTest, SetCrop_ScalingModeScaleCrop) {
544 uint8_t r = 255;
545 uint8_t g = 0;
546 uint8_t b = 0;
547
548 int32_t bufferSideLength =
549 (mDisplayWidth < mDisplayHeight) ? mDisplayWidth / 2 : mDisplayHeight / 2;
550 int32_t finalCropSideLength = bufferSideLength / 2;
551
552 auto bg = mClient->createSurface(String8("BGTest"), 0, 0, PIXEL_FORMAT_RGBA_8888,
Vishnu Nairfa247b12020-02-11 08:58:26 -0800553 ISurfaceComposerClient::eFXSurfaceEffect);
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800554 ASSERT_NE(nullptr, bg.get());
555 Transaction t;
Dominik Laskowski29fa1462021-04-27 15:51:50 -0700556 t.setLayerStack(bg, ui::DEFAULT_LAYER_STACK)
chaviw25714502021-02-11 10:01:08 -0800557 .setCrop(bg, Rect(0, 0, mDisplayWidth, mDisplayHeight))
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800558 .setColor(bg, half3{0, 0, 0})
559 .setLayer(bg, 0)
560 .apply();
561
562 BLASTBufferQueueHelper adapter(mSurfaceControl, bufferSideLength, bufferSideLength);
563 sp<IGraphicBufferProducer> igbProducer;
564 setUpProducer(adapter, igbProducer);
565 int slot;
566 sp<Fence> fence;
567 sp<GraphicBuffer> buf;
568 auto ret = igbProducer->dequeueBuffer(&slot, &fence, bufferSideLength, bufferSideLength,
569 PIXEL_FORMAT_RGBA_8888, GRALLOC_USAGE_SW_WRITE_OFTEN,
570 nullptr, nullptr);
571 ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, ret);
572 ASSERT_EQ(OK, igbProducer->requestBuffer(slot, &buf));
573
574 uint32_t* bufData;
575 buf->lock(static_cast<uint32_t>(GraphicBuffer::USAGE_SW_WRITE_OFTEN),
576 reinterpret_cast<void**>(&bufData));
577 fillBuffer(bufData, Rect(buf->getWidth(), buf->getHeight()), buf->getStride(), 0, 0, 0);
578 fillBuffer(bufData,
579 Rect(finalCropSideLength / 2, 0, buf->getWidth() - finalCropSideLength / 2,
580 buf->getHeight()),
581 buf->getStride(), r, g, b);
582 buf->unlock();
583
584 IGraphicBufferProducer::QueueBufferOutput qbOutput;
Vishnu Nair1506b182021-02-22 14:35:15 -0800585 IGraphicBufferProducer::QueueBufferInput input(systemTime(), true /* autotimestamp */,
586 HAL_DATASPACE_UNKNOWN,
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800587 Rect(bufferSideLength, finalCropSideLength),
588 NATIVE_WINDOW_SCALING_MODE_SCALE_CROP, 0,
589 Fence::NO_FENCE);
590 igbProducer->queueBuffer(slot, input, &qbOutput);
Dominik Laskowski718f9602019-11-09 20:01:35 -0800591 ASSERT_NE(ui::Transform::ROT_INVALID, qbOutput.transformHint);
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800592
593 adapter.waitForCallbacks();
594 // capture screen and verify that it is red
chaviw8ffc7b82020-08-18 11:25:37 -0700595 ASSERT_EQ(NO_ERROR, captureDisplay(mCaptureArgs, mCaptureResults));
Vishnu Nair5cc9ac02021-04-19 13:23:38 -0700596 ASSERT_NO_FATAL_FAILURE(checkScreenCapture(r, g, b,
597 {10, 10, (int32_t)bufferSideLength - 10,
598 (int32_t)bufferSideLength - 10}));
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800599 ASSERT_NO_FATAL_FAILURE(
Vishnu Nair5cc9ac02021-04-19 13:23:38 -0700600 checkScreenCapture(0, 0, 0,
601 {0, 0, (int32_t)bufferSideLength, (int32_t)bufferSideLength},
602 /*border*/ 0, /*outsideRegion*/ true));
603}
604
605TEST_F(BLASTBufferQueueTest, ScaleCroppedBufferToBufferSize) {
606 // add black background
607 auto bg = mClient->createSurface(String8("BGTest"), 0, 0, PIXEL_FORMAT_RGBA_8888,
608 ISurfaceComposerClient::eFXSurfaceEffect);
609 ASSERT_NE(nullptr, bg.get());
610 Transaction t;
Dominik Laskowski29fa1462021-04-27 15:51:50 -0700611 t.setLayerStack(bg, ui::DEFAULT_LAYER_STACK)
Vishnu Nair5cc9ac02021-04-19 13:23:38 -0700612 .setCrop(bg, Rect(0, 0, mDisplayWidth, mDisplayHeight))
613 .setColor(bg, half3{0, 0, 0})
614 .setLayer(bg, 0)
615 .apply();
616
617 Rect windowSize(1000, 1000);
618 Rect bufferSize(windowSize);
619 Rect bufferCrop(200, 200, 700, 700);
620
621 BLASTBufferQueueHelper adapter(mSurfaceControl, windowSize.getWidth(), windowSize.getHeight());
622 sp<IGraphicBufferProducer> igbProducer;
623 setUpProducer(adapter, igbProducer);
624 int slot;
625 sp<Fence> fence;
626 sp<GraphicBuffer> buf;
627 auto ret = igbProducer->dequeueBuffer(&slot, &fence, bufferSize.getWidth(),
628 bufferSize.getHeight(), PIXEL_FORMAT_RGBA_8888,
629 GRALLOC_USAGE_SW_WRITE_OFTEN, nullptr, nullptr);
630 ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, ret);
631 ASSERT_EQ(OK, igbProducer->requestBuffer(slot, &buf));
632
633 uint32_t* bufData;
634 buf->lock(static_cast<uint32_t>(GraphicBuffer::USAGE_SW_WRITE_OFTEN),
635 reinterpret_cast<void**>(&bufData));
636 // fill buffer with grey
637 fillBuffer(bufData, bufferSize, buf->getStride(), 127, 127, 127);
638
639 // fill crop area with different colors so we can verify the cropped region has been scaled
640 // correctly.
641 fillBuffer(bufData, Rect(200, 200, 450, 450), buf->getStride(), /* rgb */ 255, 0, 0);
642 fillBuffer(bufData, Rect(200, 451, 450, 700), buf->getStride(), /* rgb */ 0, 255, 0);
643 fillBuffer(bufData, Rect(451, 200, 700, 450), buf->getStride(), /* rgb */ 0, 0, 255);
644 fillBuffer(bufData, Rect(451, 451, 700, 700), buf->getStride(), /* rgb */ 255, 0, 0);
645 buf->unlock();
646
647 IGraphicBufferProducer::QueueBufferOutput qbOutput;
648 IGraphicBufferProducer::QueueBufferInput input(systemTime(), true /* autotimestamp */,
649 HAL_DATASPACE_UNKNOWN,
650 bufferCrop /* Rect::INVALID_RECT */,
651 NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW, 0,
652 Fence::NO_FENCE);
653 igbProducer->queueBuffer(slot, input, &qbOutput);
654 ASSERT_NE(ui::Transform::ROT_INVALID, qbOutput.transformHint);
655
656 adapter.waitForCallbacks();
657
658 ASSERT_EQ(NO_ERROR, captureDisplay(mCaptureArgs, mCaptureResults));
659
660 // Verify cropped region is scaled correctly.
661 ASSERT_NO_FATAL_FAILURE(checkScreenCapture(255, 0, 0, {10, 10, 490, 490}));
662 ASSERT_NO_FATAL_FAILURE(checkScreenCapture(0, 255, 0, {10, 510, 490, 990}));
663 ASSERT_NO_FATAL_FAILURE(checkScreenCapture(0, 0, 255, {510, 10, 990, 490}));
664 ASSERT_NO_FATAL_FAILURE(checkScreenCapture(255, 0, 0, {510, 510, 990, 990}));
665 // Verify outside region is black.
666 ASSERT_NO_FATAL_FAILURE(checkScreenCapture(0, 0, 0,
667 {0, 0, (int32_t)windowSize.getWidth(),
668 (int32_t)windowSize.getHeight()},
669 /*border*/ 0, /*outsideRegion*/ true));
670}
671
672TEST_F(BLASTBufferQueueTest, ScaleCroppedBufferToWindowSize) {
673 // add black background
674 auto bg = mClient->createSurface(String8("BGTest"), 0, 0, PIXEL_FORMAT_RGBA_8888,
675 ISurfaceComposerClient::eFXSurfaceEffect);
676 ASSERT_NE(nullptr, bg.get());
677 Transaction t;
Dominik Laskowski29fa1462021-04-27 15:51:50 -0700678 t.setLayerStack(bg, ui::DEFAULT_LAYER_STACK)
Vishnu Nair5cc9ac02021-04-19 13:23:38 -0700679 .setCrop(bg, Rect(0, 0, mDisplayWidth, mDisplayHeight))
680 .setColor(bg, half3{0, 0, 0})
681 .setLayer(bg, 0)
682 .apply();
683
684 Rect windowSize(1000, 1000);
685 Rect bufferSize(500, 500);
686 Rect bufferCrop(100, 100, 350, 350);
687
688 BLASTBufferQueueHelper adapter(mSurfaceControl, windowSize.getWidth(), windowSize.getHeight());
689 sp<IGraphicBufferProducer> igbProducer;
690 setUpProducer(adapter, igbProducer);
691 int slot;
692 sp<Fence> fence;
693 sp<GraphicBuffer> buf;
694 auto ret = igbProducer->dequeueBuffer(&slot, &fence, bufferSize.getWidth(),
695 bufferSize.getHeight(), PIXEL_FORMAT_RGBA_8888,
696 GRALLOC_USAGE_SW_WRITE_OFTEN, nullptr, nullptr);
697 ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, ret);
698 ASSERT_EQ(OK, igbProducer->requestBuffer(slot, &buf));
699
700 uint32_t* bufData;
701 buf->lock(static_cast<uint32_t>(GraphicBuffer::USAGE_SW_WRITE_OFTEN),
702 reinterpret_cast<void**>(&bufData));
703 // fill buffer with grey
704 fillBuffer(bufData, bufferSize, buf->getStride(), 127, 127, 127);
705
706 // fill crop area with different colors so we can verify the cropped region has been scaled
707 // correctly.
708 fillBuffer(bufData, Rect(100, 100, 225, 225), buf->getStride(), /* rgb */ 255, 0, 0);
709 fillBuffer(bufData, Rect(100, 226, 225, 350), buf->getStride(), /* rgb */ 0, 255, 0);
710 fillBuffer(bufData, Rect(226, 100, 350, 225), buf->getStride(), /* rgb */ 0, 0, 255);
711 fillBuffer(bufData, Rect(226, 226, 350, 350), buf->getStride(), /* rgb */ 255, 0, 0);
712 buf->unlock();
713
714 IGraphicBufferProducer::QueueBufferOutput qbOutput;
715 IGraphicBufferProducer::QueueBufferInput input(systemTime(), true /* autotimestamp */,
716 HAL_DATASPACE_UNKNOWN,
717 bufferCrop /* Rect::INVALID_RECT */,
718 NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW, 0,
719 Fence::NO_FENCE);
720 igbProducer->queueBuffer(slot, input, &qbOutput);
721 ASSERT_NE(ui::Transform::ROT_INVALID, qbOutput.transformHint);
722
723 adapter.waitForCallbacks();
724
725 ASSERT_EQ(NO_ERROR, captureDisplay(mCaptureArgs, mCaptureResults));
726 // Verify cropped region is scaled correctly.
727 ASSERT_NO_FATAL_FAILURE(checkScreenCapture(255, 0, 0, {10, 10, 490, 490}));
728 ASSERT_NO_FATAL_FAILURE(checkScreenCapture(0, 255, 0, {10, 510, 490, 990}));
729 ASSERT_NO_FATAL_FAILURE(checkScreenCapture(0, 0, 255, {510, 10, 990, 490}));
730 ASSERT_NO_FATAL_FAILURE(checkScreenCapture(255, 0, 0, {510, 510, 990, 990}));
731 // Verify outside region is black.
732 ASSERT_NO_FATAL_FAILURE(checkScreenCapture(0, 0, 0,
733 {0, 0, (int32_t)windowSize.getWidth(),
734 (int32_t)windowSize.getHeight()},
735 /*border*/ 0, /*outsideRegion*/ true));
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800736}
737
Vishnu Nair932f6ae2021-09-29 17:33:10 -0700738// b/196339769 verify we can can update the requested size while the in FREEZE scaling mode and
739// scale the buffer properly when the mode changes to SCALE_TO_WINDOW
740TEST_F(BLASTBufferQueueTest, ScalingModeChanges) {
741 uint8_t r = 255;
742 uint8_t g = 0;
743 uint8_t b = 0;
744
745 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight / 4);
746 sp<IGraphicBufferProducer> igbProducer;
747 setUpProducer(adapter, igbProducer);
748 {
749 int slot;
750 sp<Fence> fence;
751 sp<GraphicBuffer> buf;
752 auto ret = igbProducer->dequeueBuffer(&slot, &fence, mDisplayWidth, mDisplayHeight / 4,
753 PIXEL_FORMAT_RGBA_8888, GRALLOC_USAGE_SW_WRITE_OFTEN,
754 nullptr, nullptr);
755 ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, ret);
756 ASSERT_EQ(OK, igbProducer->requestBuffer(slot, &buf));
757
758 uint32_t* bufData;
759 buf->lock(static_cast<uint32_t>(GraphicBuffer::USAGE_SW_WRITE_OFTEN),
760 reinterpret_cast<void**>(&bufData));
761 fillBuffer(bufData, Rect(buf->getWidth(), buf->getHeight()), buf->getStride(), r, g, b);
762 buf->unlock();
763
764 IGraphicBufferProducer::QueueBufferOutput qbOutput;
765 IGraphicBufferProducer::QueueBufferInput input(systemTime(), true /* autotimestamp */,
766 HAL_DATASPACE_UNKNOWN, {},
767 NATIVE_WINDOW_SCALING_MODE_FREEZE, 0,
768 Fence::NO_FENCE);
769 igbProducer->queueBuffer(slot, input, &qbOutput);
770 adapter.waitForCallbacks();
771 }
772 // capture screen and verify that it is red
773 ASSERT_EQ(NO_ERROR, captureDisplay(mCaptureArgs, mCaptureResults));
774
775 ASSERT_NO_FATAL_FAILURE(
776 checkScreenCapture(r, g, b,
777 {0, 0, (int32_t)mDisplayWidth, (int32_t)mDisplayHeight / 4}));
778
779 // update the size to half the display and dequeue a buffer quarter of the display.
780 adapter.update(mSurfaceControl, mDisplayWidth, mDisplayHeight / 2);
781
782 {
783 int slot;
784 sp<Fence> fence;
785 sp<GraphicBuffer> buf;
786 auto ret = igbProducer->dequeueBuffer(&slot, &fence, mDisplayWidth, mDisplayHeight / 8,
787 PIXEL_FORMAT_RGBA_8888, GRALLOC_USAGE_SW_WRITE_OFTEN,
788 nullptr, nullptr);
789 ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, ret);
790 ASSERT_EQ(OK, igbProducer->requestBuffer(slot, &buf));
791
792 uint32_t* bufData;
793 buf->lock(static_cast<uint32_t>(GraphicBuffer::USAGE_SW_WRITE_OFTEN),
794 reinterpret_cast<void**>(&bufData));
795 g = 255;
796 fillBuffer(bufData, Rect(buf->getWidth(), buf->getHeight()), buf->getStride(), r, g, b);
797 buf->unlock();
798
799 IGraphicBufferProducer::QueueBufferOutput qbOutput;
800 IGraphicBufferProducer::QueueBufferInput input(systemTime(), true /* autotimestamp */,
801 HAL_DATASPACE_UNKNOWN, {},
802 NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW,
803 0, Fence::NO_FENCE);
804 igbProducer->queueBuffer(slot, input, &qbOutput);
805 adapter.waitForCallbacks();
806 }
807 // capture screen and verify that it is red
808 ASSERT_EQ(NO_ERROR, captureDisplay(mCaptureArgs, mCaptureResults));
809 // verify we still scale the buffer to the new size (half the screen height)
810 ASSERT_NO_FATAL_FAILURE(
811 checkScreenCapture(r, g, b,
812 {0, 0, (int32_t)mDisplayWidth, (int32_t)mDisplayHeight / 2}));
813}
814
chaviwd7deef72021-10-06 11:53:40 -0500815TEST_F(BLASTBufferQueueTest, SyncThenNoSync) {
816 uint8_t r = 255;
817 uint8_t g = 0;
818 uint8_t b = 0;
819
820 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
821
822 sp<IGraphicBufferProducer> igbProducer;
823 setUpProducer(adapter, igbProducer);
824
chaviwa1c4c822021-11-10 18:11:58 -0600825 Transaction sync;
Tianhao Yao4861b102022-02-03 20:18:35 +0000826 adapter.setSyncTransaction(sync);
chaviwd7deef72021-10-06 11:53:40 -0500827 queueBuffer(igbProducer, 0, 255, 0, 0);
828
829 // queue non sync buffer, so this one should get blocked
830 // Add a present delay to allow the first screenshot to get taken.
831 nsecs_t presentTimeDelay = std::chrono::nanoseconds(500ms).count();
832 queueBuffer(igbProducer, r, g, b, presentTimeDelay);
833
834 CallbackHelper transactionCallback;
chaviwa1c4c822021-11-10 18:11:58 -0600835 sync.addTransactionCompletedCallback(transactionCallback.function,
chaviwd7deef72021-10-06 11:53:40 -0500836 transactionCallback.getContext())
837 .apply();
838
839 CallbackData callbackData;
840 transactionCallback.getCallbackData(&callbackData);
841
chaviw0acd33a2021-11-02 11:55:37 -0500842 // capture screen and verify that it is green
chaviwd7deef72021-10-06 11:53:40 -0500843 ASSERT_EQ(NO_ERROR, captureDisplay(mCaptureArgs, mCaptureResults));
844 ASSERT_NO_FATAL_FAILURE(
845 checkScreenCapture(0, 255, 0, {0, 0, (int32_t)mDisplayWidth, (int32_t)mDisplayHeight}));
846
847 mProducerListener->waitOnNumberReleased(1);
848 ASSERT_EQ(NO_ERROR, captureDisplay(mCaptureArgs, mCaptureResults));
849 ASSERT_NO_FATAL_FAILURE(
850 checkScreenCapture(r, g, b, {0, 0, (int32_t)mDisplayWidth, (int32_t)mDisplayHeight}));
851}
852
853TEST_F(BLASTBufferQueueTest, MultipleSyncTransactions) {
854 uint8_t r = 255;
855 uint8_t g = 0;
856 uint8_t b = 0;
857
858 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
859
860 sp<IGraphicBufferProducer> igbProducer;
861 setUpProducer(adapter, igbProducer);
862
863 Transaction mainTransaction;
864
chaviwa1c4c822021-11-10 18:11:58 -0600865 Transaction sync;
Tianhao Yao4861b102022-02-03 20:18:35 +0000866 adapter.setSyncTransaction(sync);
chaviwd7deef72021-10-06 11:53:40 -0500867 queueBuffer(igbProducer, 0, 255, 0, 0);
868
chaviwa1c4c822021-11-10 18:11:58 -0600869 mainTransaction.merge(std::move(sync));
chaviwd7deef72021-10-06 11:53:40 -0500870
Tianhao Yao4861b102022-02-03 20:18:35 +0000871 adapter.setSyncTransaction(sync);
chaviwd7deef72021-10-06 11:53:40 -0500872 queueBuffer(igbProducer, r, g, b, 0);
873
chaviwa1c4c822021-11-10 18:11:58 -0600874 mainTransaction.merge(std::move(sync));
chaviwd7deef72021-10-06 11:53:40 -0500875 // Expect 1 buffer to be released even before sending to SurfaceFlinger
876 mProducerListener->waitOnNumberReleased(1);
877
878 CallbackHelper transactionCallback;
879 mainTransaction
880 .addTransactionCompletedCallback(transactionCallback.function,
881 transactionCallback.getContext())
882 .apply();
883
884 CallbackData callbackData;
885 transactionCallback.getCallbackData(&callbackData);
886
887 // capture screen and verify that it is red
888 ASSERT_EQ(NO_ERROR, captureDisplay(mCaptureArgs, mCaptureResults));
889 ASSERT_NO_FATAL_FAILURE(
890 checkScreenCapture(r, g, b, {0, 0, (int32_t)mDisplayWidth, (int32_t)mDisplayHeight}));
891}
892
893TEST_F(BLASTBufferQueueTest, MultipleSyncTransactionWithNonSync) {
894 uint8_t r = 255;
895 uint8_t g = 0;
896 uint8_t b = 0;
897
898 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
899
900 sp<IGraphicBufferProducer> igbProducer;
901 setUpProducer(adapter, igbProducer);
902
903 Transaction mainTransaction;
904
chaviwa1c4c822021-11-10 18:11:58 -0600905 Transaction sync;
chaviwd7deef72021-10-06 11:53:40 -0500906 // queue a sync transaction
Tianhao Yao4861b102022-02-03 20:18:35 +0000907 adapter.setSyncTransaction(sync);
chaviwd7deef72021-10-06 11:53:40 -0500908 queueBuffer(igbProducer, 0, 255, 0, 0);
909
chaviwa1c4c822021-11-10 18:11:58 -0600910 mainTransaction.merge(std::move(sync));
chaviwd7deef72021-10-06 11:53:40 -0500911
chaviwa1c4c822021-11-10 18:11:58 -0600912 // queue another buffer without setting sync transaction
chaviwd7deef72021-10-06 11:53:40 -0500913 queueBuffer(igbProducer, 0, 0, 255, 0);
914
915 // queue another sync transaction
Tianhao Yao4861b102022-02-03 20:18:35 +0000916 adapter.setSyncTransaction(sync);
chaviwd7deef72021-10-06 11:53:40 -0500917 queueBuffer(igbProducer, r, g, b, 0);
918 // Expect 1 buffer to be released because the non sync transaction should merge
919 // with the sync
920 mProducerListener->waitOnNumberReleased(1);
921
chaviwa1c4c822021-11-10 18:11:58 -0600922 mainTransaction.merge(std::move(sync));
chaviwd7deef72021-10-06 11:53:40 -0500923 // Expect 2 buffers to be released due to merging the two syncs.
924 mProducerListener->waitOnNumberReleased(2);
925
926 CallbackHelper transactionCallback;
927 mainTransaction
928 .addTransactionCompletedCallback(transactionCallback.function,
929 transactionCallback.getContext())
930 .apply();
931
932 CallbackData callbackData;
933 transactionCallback.getCallbackData(&callbackData);
934
935 // capture screen and verify that it is red
936 ASSERT_EQ(NO_ERROR, captureDisplay(mCaptureArgs, mCaptureResults));
937 ASSERT_NO_FATAL_FAILURE(
938 checkScreenCapture(r, g, b, {0, 0, (int32_t)mDisplayWidth, (int32_t)mDisplayHeight}));
939}
940
941TEST_F(BLASTBufferQueueTest, MultipleSyncRunOutOfBuffers) {
942 uint8_t r = 255;
943 uint8_t g = 0;
944 uint8_t b = 0;
945
946 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
947
948 sp<IGraphicBufferProducer> igbProducer;
949 setUpProducer(adapter, igbProducer, 3);
950
951 Transaction mainTransaction;
952
chaviwa1c4c822021-11-10 18:11:58 -0600953 Transaction sync;
chaviwd7deef72021-10-06 11:53:40 -0500954 // queue a sync transaction
Tianhao Yao4861b102022-02-03 20:18:35 +0000955 adapter.setSyncTransaction(sync);
chaviwd7deef72021-10-06 11:53:40 -0500956 queueBuffer(igbProducer, 0, 255, 0, 0);
957
chaviwa1c4c822021-11-10 18:11:58 -0600958 mainTransaction.merge(std::move(sync));
chaviwd7deef72021-10-06 11:53:40 -0500959
chaviwa1c4c822021-11-10 18:11:58 -0600960 // queue a few buffers without setting sync transaction
chaviwd7deef72021-10-06 11:53:40 -0500961 queueBuffer(igbProducer, 0, 0, 255, 0);
962 queueBuffer(igbProducer, 0, 0, 255, 0);
963 queueBuffer(igbProducer, 0, 0, 255, 0);
964
965 // queue another sync transaction
Tianhao Yao4861b102022-02-03 20:18:35 +0000966 adapter.setSyncTransaction(sync);
chaviwd7deef72021-10-06 11:53:40 -0500967 queueBuffer(igbProducer, r, g, b, 0);
968 // Expect 3 buffers to be released because the non sync transactions should merge
969 // with the sync
970 mProducerListener->waitOnNumberReleased(3);
971
chaviwa1c4c822021-11-10 18:11:58 -0600972 mainTransaction.merge(std::move(sync));
chaviwd7deef72021-10-06 11:53:40 -0500973 // Expect 4 buffers to be released due to merging the two syncs.
974 mProducerListener->waitOnNumberReleased(4);
975
976 CallbackHelper transactionCallback;
977 mainTransaction
978 .addTransactionCompletedCallback(transactionCallback.function,
979 transactionCallback.getContext())
980 .apply();
981
982 CallbackData callbackData;
983 transactionCallback.getCallbackData(&callbackData);
984
985 // capture screen and verify that it is red
986 ASSERT_EQ(NO_ERROR, captureDisplay(mCaptureArgs, mCaptureResults));
987 ASSERT_NO_FATAL_FAILURE(
988 checkScreenCapture(r, g, b, {0, 0, (int32_t)mDisplayWidth, (int32_t)mDisplayHeight}));
989}
990
991// Tests BBQ with a sync transaction when the buffers acquired reaches max and the only way to
992// continue processing is for a release callback from SurfaceFlinger.
993// This is done by sending a buffer to SF so it can release the previous one and allow BBQ to
994// continue acquiring buffers.
995TEST_F(BLASTBufferQueueTest, RunOutOfBuffersWaitingOnSF) {
996 uint8_t r = 255;
997 uint8_t g = 0;
998 uint8_t b = 0;
999
1000 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
1001
1002 sp<IGraphicBufferProducer> igbProducer;
1003 setUpProducer(adapter, igbProducer, 4);
1004
1005 Transaction mainTransaction;
1006
1007 // Send a buffer to SF
1008 queueBuffer(igbProducer, 0, 255, 0, 0);
1009
chaviwa1c4c822021-11-10 18:11:58 -06001010 Transaction sync;
chaviwd7deef72021-10-06 11:53:40 -05001011 // queue a sync transaction
Tianhao Yao4861b102022-02-03 20:18:35 +00001012 adapter.setSyncTransaction(sync);
chaviwd7deef72021-10-06 11:53:40 -05001013 queueBuffer(igbProducer, 0, 255, 0, 0);
1014
chaviwa1c4c822021-11-10 18:11:58 -06001015 mainTransaction.merge(std::move(sync));
chaviwd7deef72021-10-06 11:53:40 -05001016
chaviwa1c4c822021-11-10 18:11:58 -06001017 // queue a few buffers without setting sync transaction
chaviwd7deef72021-10-06 11:53:40 -05001018 queueBuffer(igbProducer, 0, 0, 255, 0);
1019 queueBuffer(igbProducer, 0, 0, 255, 0);
1020 queueBuffer(igbProducer, 0, 0, 255, 0);
1021
1022 // apply the first synced buffer to ensure we have to wait on SF
1023 mainTransaction.apply();
1024
1025 // queue another sync transaction
Tianhao Yao4861b102022-02-03 20:18:35 +00001026 adapter.setSyncTransaction(sync);
chaviwd7deef72021-10-06 11:53:40 -05001027 queueBuffer(igbProducer, r, g, b, 0);
1028 // Expect 2 buffers to be released because the non sync transactions should merge
1029 // with the sync
1030 mProducerListener->waitOnNumberReleased(3);
1031
chaviwa1c4c822021-11-10 18:11:58 -06001032 mainTransaction.merge(std::move(sync));
chaviwd7deef72021-10-06 11:53:40 -05001033
1034 CallbackHelper transactionCallback;
1035 mainTransaction
1036 .addTransactionCompletedCallback(transactionCallback.function,
1037 transactionCallback.getContext())
1038 .apply();
1039
1040 CallbackData callbackData;
1041 transactionCallback.getCallbackData(&callbackData);
1042
1043 // capture screen and verify that it is red
1044 ASSERT_EQ(NO_ERROR, captureDisplay(mCaptureArgs, mCaptureResults));
1045 ASSERT_NO_FATAL_FAILURE(
1046 checkScreenCapture(r, g, b, {0, 0, (int32_t)mDisplayWidth, (int32_t)mDisplayHeight}));
1047}
1048
Tianhao Yao4861b102022-02-03 20:18:35 +00001049TEST_F(BLASTBufferQueueTest, SyncNextTransactionAcquireMultipleBuffers) {
chaviw0acd33a2021-11-02 11:55:37 -05001050 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
1051
1052 sp<IGraphicBufferProducer> igbProducer;
1053 setUpProducer(adapter, igbProducer);
1054
1055 Transaction next;
Tianhao Yao4861b102022-02-03 20:18:35 +00001056 adapter.setSyncTransaction(next, false);
chaviw0acd33a2021-11-02 11:55:37 -05001057 queueBuffer(igbProducer, 0, 255, 0, 0);
1058 queueBuffer(igbProducer, 0, 0, 255, 0);
1059 // There should only be one frame submitted since the first frame will be released.
1060 adapter.validateNumFramesSubmitted(1);
Tianhao Yao4861b102022-02-03 20:18:35 +00001061 adapter.stopContinuousSyncTransaction();
chaviw0acd33a2021-11-02 11:55:37 -05001062
1063 // queue non sync buffer, so this one should get blocked
1064 // Add a present delay to allow the first screenshot to get taken.
1065 nsecs_t presentTimeDelay = std::chrono::nanoseconds(500ms).count();
1066 queueBuffer(igbProducer, 255, 0, 0, presentTimeDelay);
1067
1068 CallbackHelper transactionCallback;
1069 next.addTransactionCompletedCallback(transactionCallback.function,
1070 transactionCallback.getContext())
1071 .apply();
1072
1073 CallbackData callbackData;
1074 transactionCallback.getCallbackData(&callbackData);
1075
1076 // capture screen and verify that it is blue
1077 ASSERT_EQ(NO_ERROR, captureDisplay(mCaptureArgs, mCaptureResults));
1078 ASSERT_NO_FATAL_FAILURE(
1079 checkScreenCapture(0, 0, 255, {0, 0, (int32_t)mDisplayWidth, (int32_t)mDisplayHeight}));
1080
1081 mProducerListener->waitOnNumberReleased(2);
1082 // capture screen and verify that it is red
1083 ASSERT_EQ(NO_ERROR, captureDisplay(mCaptureArgs, mCaptureResults));
1084 ASSERT_NO_FATAL_FAILURE(
1085 checkScreenCapture(255, 0, 0, {0, 0, (int32_t)mDisplayWidth, (int32_t)mDisplayHeight}));
1086}
1087
chaviw3b4bdcf2022-03-17 09:27:03 -05001088TEST_F(BLASTBufferQueueTest, SyncNextTransactionOverwrite) {
1089 std::mutex mutex;
1090 std::condition_variable callbackReceivedCv;
1091 bool receivedCallback = false;
1092
1093 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
1094 ASSERT_EQ(nullptr, adapter.getTransactionReadyCallback());
1095 auto callback = [&](Transaction*) {
1096 std::unique_lock<std::mutex> lock(mutex);
1097 receivedCallback = true;
1098 callbackReceivedCv.notify_one();
1099 };
1100 adapter.syncNextTransaction(callback);
1101 ASSERT_NE(nullptr, adapter.getTransactionReadyCallback());
1102
1103 auto callback2 = [](Transaction*) {};
1104 adapter.syncNextTransaction(callback2);
1105
1106 std::unique_lock<std::mutex> lock(mutex);
1107 if (!receivedCallback) {
1108 ASSERT_NE(callbackReceivedCv.wait_for(lock, std::chrono::seconds(3)),
1109 std::cv_status::timeout)
1110 << "did not receive callback";
1111 }
1112
1113 ASSERT_TRUE(receivedCallback);
1114}
1115
Vishnu Nair1e8bf102021-12-28 14:36:59 -08001116// This test will currently fail because the old surfacecontrol will steal the last presented buffer
1117// until the old surface control is destroyed. This is not necessarily a bug but to document a
1118// limitation with the update API and to test any changes to make the api more robust. The current
1119// approach for the client is to recreate the blastbufferqueue when the surfacecontrol updates.
1120TEST_F(BLASTBufferQueueTest, DISABLED_DisconnectProducerTest) {
1121 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
1122 std::vector<sp<SurfaceControl>> surfaceControls;
1123 sp<IGraphicBufferProducer> igbProducer;
1124 for (int i = 0; i < 10; i++) {
1125 sp<SurfaceControl> sc =
1126 mClient->createSurface(String8("TestSurface"), mDisplayWidth, mDisplayHeight,
1127 PIXEL_FORMAT_RGBA_8888,
1128 ISurfaceComposerClient::eFXSurfaceBufferState,
1129 /*parent*/ nullptr);
1130 Transaction()
1131 .setLayerStack(mSurfaceControl, ui::DEFAULT_LAYER_STACK)
1132 .setLayer(mSurfaceControl, std::numeric_limits<int32_t>::max())
1133 .show(mSurfaceControl)
1134 .setDataspace(mSurfaceControl, ui::Dataspace::V0_SRGB)
1135 .apply(true);
1136 surfaceControls.push_back(sc);
1137 adapter.update(sc, mDisplayWidth, mDisplayHeight);
1138
1139 setUpProducer(adapter, igbProducer);
1140 Transaction next;
1141 queueBuffer(igbProducer, 0, 255, 0, 0);
1142 queueBuffer(igbProducer, 0, 0, 255, 0);
Tianhao Yao4861b102022-02-03 20:18:35 +00001143 adapter.setSyncTransaction(next, true);
Vishnu Nair1e8bf102021-12-28 14:36:59 -08001144 queueBuffer(igbProducer, 255, 0, 0, 0);
1145
1146 CallbackHelper transactionCallback;
1147 next.addTransactionCompletedCallback(transactionCallback.function,
1148 transactionCallback.getContext())
1149 .apply();
1150
1151 CallbackData callbackData;
1152 transactionCallback.getCallbackData(&callbackData);
1153 // capture screen and verify that it is red
1154 ASSERT_EQ(NO_ERROR, captureDisplay(mCaptureArgs, mCaptureResults));
1155 ASSERT_NO_FATAL_FAILURE(
1156 checkScreenCapture(255, 0, 0,
1157 {0, 0, (int32_t)mDisplayWidth, (int32_t)mDisplayHeight}));
1158 igbProducer->disconnect(NATIVE_WINDOW_API_CPU);
1159 }
1160}
1161
1162// See DISABLED_DisconnectProducerTest
1163TEST_F(BLASTBufferQueueTest, DISABLED_UpdateSurfaceControlTest) {
1164 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
1165 std::vector<sp<SurfaceControl>> surfaceControls;
1166 sp<IGraphicBufferProducer> igbProducer;
1167 for (int i = 0; i < 10; i++) {
1168 sp<SurfaceControl> sc =
1169 mClient->createSurface(String8("TestSurface"), mDisplayWidth, mDisplayHeight,
1170 PIXEL_FORMAT_RGBA_8888,
1171 ISurfaceComposerClient::eFXSurfaceBufferState,
1172 /*parent*/ nullptr);
1173 Transaction()
1174 .setLayerStack(mSurfaceControl, ui::DEFAULT_LAYER_STACK)
1175 .setLayer(mSurfaceControl, std::numeric_limits<int32_t>::max())
1176 .show(mSurfaceControl)
1177 .setDataspace(mSurfaceControl, ui::Dataspace::V0_SRGB)
1178 .apply(true);
1179 surfaceControls.push_back(sc);
1180 adapter.update(sc, mDisplayWidth, mDisplayHeight);
1181 setUpProducer(adapter, igbProducer);
1182
1183 Transaction next;
1184 queueBuffer(igbProducer, 0, 255, 0, 0);
1185 queueBuffer(igbProducer, 0, 0, 255, 0);
Tianhao Yao4861b102022-02-03 20:18:35 +00001186 adapter.setSyncTransaction(next, true);
Vishnu Nair1e8bf102021-12-28 14:36:59 -08001187 queueBuffer(igbProducer, 255, 0, 0, 0);
1188
1189 CallbackHelper transactionCallback;
1190 next.addTransactionCompletedCallback(transactionCallback.function,
1191 transactionCallback.getContext())
1192 .apply();
1193
1194 CallbackData callbackData;
1195 transactionCallback.getCallbackData(&callbackData);
1196 // capture screen and verify that it is red
1197 ASSERT_EQ(NO_ERROR, captureDisplay(mCaptureArgs, mCaptureResults));
1198 ASSERT_NO_FATAL_FAILURE(
1199 checkScreenCapture(255, 0, 0,
1200 {0, 0, (int32_t)mDisplayWidth, (int32_t)mDisplayHeight}));
1201 }
1202}
1203
Vishnu Nair89496122020-12-14 17:14:53 -08001204class TestProducerListener : public BnProducerListener {
1205public:
1206 sp<IGraphicBufferProducer> mIgbp;
1207 TestProducerListener(const sp<IGraphicBufferProducer>& igbp) : mIgbp(igbp) {}
1208 void onBufferReleased() override {
1209 sp<GraphicBuffer> buffer;
1210 sp<Fence> fence;
1211 mIgbp->detachNextBuffer(&buffer, &fence);
1212 }
1213};
1214
1215TEST_F(BLASTBufferQueueTest, CustomProducerListener) {
1216 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
1217 sp<IGraphicBufferProducer> igbProducer = adapter.getIGraphicBufferProducer();
1218 ASSERT_NE(nullptr, igbProducer.get());
1219 ASSERT_EQ(NO_ERROR, igbProducer->setMaxDequeuedBufferCount(2));
1220 IGraphicBufferProducer::QueueBufferOutput qbOutput;
1221 ASSERT_EQ(NO_ERROR,
1222 igbProducer->connect(new TestProducerListener(igbProducer), NATIVE_WINDOW_API_CPU,
1223 false, &qbOutput));
1224 ASSERT_NE(ui::Transform::ROT_INVALID, qbOutput.transformHint);
1225 for (int i = 0; i < 3; i++) {
1226 int slot;
1227 sp<Fence> fence;
1228 sp<GraphicBuffer> buf;
1229 auto ret = igbProducer->dequeueBuffer(&slot, &fence, mDisplayWidth, mDisplayHeight,
1230 PIXEL_FORMAT_RGBA_8888, GRALLOC_USAGE_SW_WRITE_OFTEN,
1231 nullptr, nullptr);
1232 ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, ret);
1233 ASSERT_EQ(OK, igbProducer->requestBuffer(slot, &buf));
1234 IGraphicBufferProducer::QueueBufferOutput qbOutput;
Vishnu Nair1506b182021-02-22 14:35:15 -08001235 IGraphicBufferProducer::QueueBufferInput input(systemTime(), true /* autotimestamp */,
1236 HAL_DATASPACE_UNKNOWN,
Vishnu Nair89496122020-12-14 17:14:53 -08001237 Rect(mDisplayWidth, mDisplayHeight),
1238 NATIVE_WINDOW_SCALING_MODE_FREEZE, 0,
1239 Fence::NO_FENCE);
1240 igbProducer->queueBuffer(slot, input, &qbOutput);
1241 }
1242 adapter.waitForCallbacks();
1243}
1244
Vishnu Nair17dde612020-12-28 11:39:59 -08001245TEST_F(BLASTBufferQueueTest, QueryNativeWindowQueuesToWindowComposer) {
1246 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
1247
1248 sp<android::Surface> surface = new Surface(adapter.getIGraphicBufferProducer());
1249 ANativeWindow* nativeWindow = (ANativeWindow*)(surface.get());
1250 int queuesToNativeWindow = 0;
1251 int err = nativeWindow->query(nativeWindow, NATIVE_WINDOW_QUEUES_TO_WINDOW_COMPOSER,
1252 &queuesToNativeWindow);
1253 ASSERT_EQ(NO_ERROR, err);
1254 ASSERT_EQ(queuesToNativeWindow, 1);
1255}
1256
Vishnu Nair083efd32021-02-12 09:32:30 -08001257// Test a slow producer doesn't hold up a faster producer from the same client. Essentially tests
1258// BBQ uses separate transaction queues.
Vishnu Nair277142c2021-01-05 18:35:29 -08001259TEST_F(BLASTBufferQueueTest, OutOfOrderTransactionTest) {
1260 sp<SurfaceControl> bgSurface =
1261 mClient->createSurface(String8("BGTest"), 0, 0, PIXEL_FORMAT_RGBA_8888,
1262 ISurfaceComposerClient::eFXSurfaceBufferState);
1263 ASSERT_NE(nullptr, bgSurface.get());
1264 Transaction t;
Dominik Laskowski29fa1462021-04-27 15:51:50 -07001265 t.setLayerStack(bgSurface, ui::DEFAULT_LAYER_STACK)
Vishnu Nair277142c2021-01-05 18:35:29 -08001266 .show(bgSurface)
1267 .setDataspace(bgSurface, ui::Dataspace::V0_SRGB)
Vishnu Nair277142c2021-01-05 18:35:29 -08001268 .setLayer(bgSurface, std::numeric_limits<int32_t>::max() - 1)
1269 .apply();
1270
1271 BLASTBufferQueueHelper slowAdapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
1272 sp<IGraphicBufferProducer> slowIgbProducer;
1273 setUpProducer(slowAdapter, slowIgbProducer);
1274 nsecs_t presentTimeDelay = std::chrono::nanoseconds(500ms).count();
Vishnu Nair1506b182021-02-22 14:35:15 -08001275 queueBuffer(slowIgbProducer, 0 /* r */, 255 /* g */, 0 /* b */, presentTimeDelay);
Vishnu Nair277142c2021-01-05 18:35:29 -08001276
1277 BLASTBufferQueueHelper fastAdapter(bgSurface, mDisplayWidth, mDisplayHeight);
1278 sp<IGraphicBufferProducer> fastIgbProducer;
1279 setUpProducer(fastAdapter, fastIgbProducer);
1280 uint8_t r = 255;
1281 uint8_t g = 0;
1282 uint8_t b = 0;
1283 queueBuffer(fastIgbProducer, r, g, b, 0 /* presentTimeDelay */);
1284 fastAdapter.waitForCallbacks();
1285
1286 // capture screen and verify that it is red
1287 ASSERT_EQ(NO_ERROR, captureDisplay(mCaptureArgs, mCaptureResults));
1288
1289 ASSERT_NO_FATAL_FAILURE(
Chavi Weingartena5aedbd2021-04-09 13:37:33 +00001290 checkScreenCapture(r, g, b,
1291 {0, 0, (int32_t)mDisplayWidth, (int32_t)mDisplayHeight / 2}));
Vishnu Nair277142c2021-01-05 18:35:29 -08001292}
1293
Vishnu Naira4fbca52021-07-07 16:52:34 -07001294TEST_F(BLASTBufferQueueTest, TransformHint) {
1295 // Transform hint is provided to BBQ via the surface control passed by WM
1296 mSurfaceControl->setTransformHint(ui::Transform::ROT_90);
1297
1298 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
1299 sp<IGraphicBufferProducer> igbProducer = adapter.getIGraphicBufferProducer();
1300 ASSERT_NE(nullptr, igbProducer.get());
1301 ASSERT_EQ(NO_ERROR, igbProducer->setMaxDequeuedBufferCount(2));
1302 sp<Surface> surface = adapter.getSurface();
1303
1304 // Before connecting to the surface, we do not get a valid transform hint
1305 int transformHint;
1306 surface->query(NATIVE_WINDOW_TRANSFORM_HINT, &transformHint);
1307 ASSERT_EQ(ui::Transform::ROT_0, transformHint);
1308
1309 ASSERT_EQ(NO_ERROR,
1310 surface->connect(NATIVE_WINDOW_API_CPU, new TestProducerListener(igbProducer)));
1311
1312 // After connecting to the surface, we should get the correct hint.
1313 surface->query(NATIVE_WINDOW_TRANSFORM_HINT, &transformHint);
1314 ASSERT_EQ(ui::Transform::ROT_90, transformHint);
1315
1316 ANativeWindow_Buffer buffer;
1317 surface->lock(&buffer, nullptr /* inOutDirtyBounds */);
1318
1319 // Transform hint is updated via callbacks or surface control updates
1320 mSurfaceControl->setTransformHint(ui::Transform::ROT_0);
1321 adapter.update(mSurfaceControl, mDisplayWidth, mDisplayHeight);
1322
1323 // The hint does not change and matches the value used when dequeueing the buffer.
1324 surface->query(NATIVE_WINDOW_TRANSFORM_HINT, &transformHint);
1325 ASSERT_EQ(ui::Transform::ROT_90, transformHint);
1326
1327 surface->unlockAndPost();
1328
1329 // After queuing the buffer, we get the updated transform hint
1330 surface->query(NATIVE_WINDOW_TRANSFORM_HINT, &transformHint);
1331 ASSERT_EQ(ui::Transform::ROT_0, transformHint);
1332
1333 adapter.waitForCallbacks();
1334}
1335
Valerie Hau5977fc82019-12-05 15:56:39 -08001336class BLASTBufferQueueTransformTest : public BLASTBufferQueueTest {
1337public:
1338 void test(uint32_t tr) {
1339 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
1340 sp<IGraphicBufferProducer> igbProducer;
1341 setUpProducer(adapter, igbProducer);
1342
1343 auto bufWidth = mDisplayWidth;
1344 auto bufHeight = mDisplayHeight;
1345 int slot;
1346 sp<Fence> fence;
1347 sp<GraphicBuffer> buf;
1348
1349 auto ret = igbProducer->dequeueBuffer(&slot, &fence, bufWidth, bufHeight,
1350 PIXEL_FORMAT_RGBA_8888, GRALLOC_USAGE_SW_WRITE_OFTEN,
1351 nullptr, nullptr);
1352 ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, ret);
1353 ASSERT_EQ(OK, igbProducer->requestBuffer(slot, &buf));
1354
1355 fillQuadrants(buf);
1356
1357 IGraphicBufferProducer::QueueBufferOutput qbOutput;
Vishnu Nair1506b182021-02-22 14:35:15 -08001358 IGraphicBufferProducer::QueueBufferInput input(systemTime(), true /* autotimestamp */,
1359 HAL_DATASPACE_UNKNOWN,
Valerie Hau5977fc82019-12-05 15:56:39 -08001360 Rect(bufWidth, bufHeight),
Vishnu Naire1a42322020-10-02 17:42:04 -07001361 NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW,
1362 tr, Fence::NO_FENCE);
Valerie Hau5977fc82019-12-05 15:56:39 -08001363 igbProducer->queueBuffer(slot, input, &qbOutput);
Dominik Laskowski718f9602019-11-09 20:01:35 -08001364 ASSERT_NE(ui::Transform::ROT_INVALID, qbOutput.transformHint);
Valerie Hau5977fc82019-12-05 15:56:39 -08001365
1366 adapter.waitForCallbacks();
chaviw8ffc7b82020-08-18 11:25:37 -07001367 ASSERT_EQ(NO_ERROR, captureDisplay(mCaptureArgs, mCaptureResults));
chaviwd2432892020-07-24 17:42:39 -07001368
Valerie Hau5977fc82019-12-05 15:56:39 -08001369 switch (tr) {
1370 case ui::Transform::ROT_0:
1371 ASSERT_NO_FATAL_FAILURE(checkScreenCapture(0, 0, 0,
1372 {0, 0, (int32_t)mDisplayWidth / 2,
1373 (int32_t)mDisplayHeight / 2},
1374 1));
1375 ASSERT_NO_FATAL_FAILURE(
1376 checkScreenCapture(255, 0, 0,
1377 {(int32_t)mDisplayWidth / 2, 0, (int32_t)mDisplayWidth,
1378 (int32_t)mDisplayHeight / 2},
1379 1));
1380 ASSERT_NO_FATAL_FAILURE(
1381 checkScreenCapture(0, 255, 0,
1382 {(int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight / 2,
1383 (int32_t)mDisplayWidth, (int32_t)mDisplayHeight},
1384 1));
1385 ASSERT_NO_FATAL_FAILURE(
1386 checkScreenCapture(0, 0, 255,
1387 {0, (int32_t)mDisplayHeight / 2,
1388 (int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight},
1389 1));
1390 break;
1391 case ui::Transform::FLIP_H:
1392 ASSERT_NO_FATAL_FAILURE(checkScreenCapture(255, 0, 0,
1393 {0, 0, (int32_t)mDisplayWidth / 2,
1394 (int32_t)mDisplayHeight / 2},
1395 1));
1396 ASSERT_NO_FATAL_FAILURE(
1397 checkScreenCapture(0, 0, 0,
1398 {(int32_t)mDisplayWidth / 2, 0, (int32_t)mDisplayWidth,
1399 (int32_t)mDisplayHeight / 2},
1400 1));
1401 ASSERT_NO_FATAL_FAILURE(
1402 checkScreenCapture(0, 0, 255,
1403 {(int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight / 2,
1404 (int32_t)mDisplayWidth, (int32_t)mDisplayHeight},
1405 1));
1406 ASSERT_NO_FATAL_FAILURE(
1407 checkScreenCapture(0, 255, 0,
1408 {0, (int32_t)mDisplayHeight / 2,
1409 (int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight},
1410 1));
1411 break;
1412 case ui::Transform::FLIP_V:
1413 ASSERT_NO_FATAL_FAILURE(checkScreenCapture(0, 0, 255,
1414 {0, 0, (int32_t)mDisplayWidth / 2,
1415 (int32_t)mDisplayHeight / 2},
1416 1));
1417 ASSERT_NO_FATAL_FAILURE(
1418 checkScreenCapture(0, 255, 0,
1419 {(int32_t)mDisplayWidth / 2, 0, (int32_t)mDisplayWidth,
1420 (int32_t)mDisplayHeight / 2},
1421 1));
1422 ASSERT_NO_FATAL_FAILURE(
1423 checkScreenCapture(255, 0, 0,
1424 {(int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight / 2,
1425 (int32_t)mDisplayWidth, (int32_t)mDisplayHeight},
1426 1));
1427 ASSERT_NO_FATAL_FAILURE(
1428 checkScreenCapture(0, 0, 0,
1429 {0, (int32_t)mDisplayHeight / 2,
1430 (int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight},
1431 1));
1432 break;
1433 case ui::Transform::ROT_90:
1434 ASSERT_NO_FATAL_FAILURE(checkScreenCapture(0, 0, 255,
1435 {0, 0, (int32_t)mDisplayWidth / 2,
1436 (int32_t)mDisplayHeight / 2},
1437 1));
1438 ASSERT_NO_FATAL_FAILURE(
1439 checkScreenCapture(0, 0, 0,
1440 {(int32_t)mDisplayWidth / 2, 0, (int32_t)mDisplayWidth,
1441 (int32_t)mDisplayHeight / 2},
1442 1));
1443 ASSERT_NO_FATAL_FAILURE(
1444 checkScreenCapture(255, 0, 0,
1445 {(int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight / 2,
1446 (int32_t)mDisplayWidth, (int32_t)mDisplayHeight},
1447 1));
1448 ASSERT_NO_FATAL_FAILURE(
1449 checkScreenCapture(0, 255, 0,
1450 {0, (int32_t)mDisplayHeight / 2,
1451 (int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight},
1452 1));
1453 break;
1454 case ui::Transform::ROT_180:
1455 ASSERT_NO_FATAL_FAILURE(checkScreenCapture(0, 255, 0,
1456 {0, 0, (int32_t)mDisplayWidth / 2,
1457 (int32_t)mDisplayHeight / 2},
1458 1));
1459 ASSERT_NO_FATAL_FAILURE(
1460 checkScreenCapture(0, 0, 255,
1461 {(int32_t)mDisplayWidth / 2, 0, (int32_t)mDisplayWidth,
1462 (int32_t)mDisplayHeight / 2},
1463 1));
1464 ASSERT_NO_FATAL_FAILURE(
1465 checkScreenCapture(0, 0, 0,
1466 {(int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight / 2,
1467 (int32_t)mDisplayWidth, (int32_t)mDisplayHeight},
1468 1));
1469 ASSERT_NO_FATAL_FAILURE(
1470 checkScreenCapture(255, 0, 0,
1471 {0, (int32_t)mDisplayHeight / 2,
1472 (int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight},
1473 1));
1474 break;
1475 case ui::Transform::ROT_270:
1476 ASSERT_NO_FATAL_FAILURE(checkScreenCapture(255, 0, 0,
1477 {0, 0, (int32_t)mDisplayWidth / 2,
1478 (int32_t)mDisplayHeight / 2},
1479 1));
1480 ASSERT_NO_FATAL_FAILURE(
1481 checkScreenCapture(0, 255, 0,
1482 {(int32_t)mDisplayWidth / 2, 0, (int32_t)mDisplayWidth,
1483 (int32_t)mDisplayHeight / 2},
1484 1));
1485 ASSERT_NO_FATAL_FAILURE(
1486 checkScreenCapture(0, 0, 255,
1487 {(int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight / 2,
1488 (int32_t)mDisplayWidth, (int32_t)mDisplayHeight},
1489 1));
1490 ASSERT_NO_FATAL_FAILURE(
1491 checkScreenCapture(0, 0, 0,
1492 {0, (int32_t)mDisplayHeight / 2,
1493 (int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight},
1494 1));
1495 }
1496 }
1497};
1498
1499TEST_F(BLASTBufferQueueTransformTest, setTransform_ROT_0) {
1500 test(ui::Transform::ROT_0);
1501}
1502
1503TEST_F(BLASTBufferQueueTransformTest, setTransform_FLIP_H) {
1504 test(ui::Transform::FLIP_H);
1505}
1506
1507TEST_F(BLASTBufferQueueTransformTest, setTransform_FLIP_V) {
1508 test(ui::Transform::FLIP_V);
1509}
1510
1511TEST_F(BLASTBufferQueueTransformTest, setTransform_ROT_90) {
1512 test(ui::Transform::ROT_90);
1513}
1514
1515TEST_F(BLASTBufferQueueTransformTest, setTransform_ROT_180) {
1516 test(ui::Transform::ROT_180);
1517}
1518
1519TEST_F(BLASTBufferQueueTransformTest, setTransform_ROT_270) {
1520 test(ui::Transform::ROT_270);
1521}
Valerie Hau871d6352020-01-29 08:44:02 -08001522
1523class BLASTFrameEventHistoryTest : public BLASTBufferQueueTest {
1524public:
1525 void setUpAndQueueBuffer(const sp<IGraphicBufferProducer>& igbProducer,
Vishnu Nairde66dc72021-06-17 17:54:41 -07001526 nsecs_t* outRequestedPresentTime, nsecs_t* postedTime,
Valerie Hau871d6352020-01-29 08:44:02 -08001527 IGraphicBufferProducer::QueueBufferOutput* qbOutput,
Vishnu Nairde66dc72021-06-17 17:54:41 -07001528 bool getFrameTimestamps, nsecs_t requestedPresentTime = systemTime()) {
Valerie Hau871d6352020-01-29 08:44:02 -08001529 int slot;
1530 sp<Fence> fence;
1531 sp<GraphicBuffer> buf;
1532 auto ret = igbProducer->dequeueBuffer(&slot, &fence, mDisplayWidth, mDisplayHeight,
1533 PIXEL_FORMAT_RGBA_8888, GRALLOC_USAGE_SW_WRITE_OFTEN,
1534 nullptr, nullptr);
Vishnu Nairde66dc72021-06-17 17:54:41 -07001535 if (IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION == ret) {
1536 ASSERT_EQ(OK, igbProducer->requestBuffer(slot, &buf));
1537 }
Valerie Hau871d6352020-01-29 08:44:02 -08001538
Vishnu Nairde66dc72021-06-17 17:54:41 -07001539 *outRequestedPresentTime = requestedPresentTime;
1540 IGraphicBufferProducer::QueueBufferInput input(requestedPresentTime, false,
1541 HAL_DATASPACE_UNKNOWN,
Valerie Hau871d6352020-01-29 08:44:02 -08001542 Rect(mDisplayWidth, mDisplayHeight),
1543 NATIVE_WINDOW_SCALING_MODE_FREEZE, 0,
1544 Fence::NO_FENCE, /*sticky*/ 0,
1545 getFrameTimestamps);
1546 if (postedTime) *postedTime = systemTime();
1547 igbProducer->queueBuffer(slot, input, qbOutput);
1548 }
Vishnu Nair083efd32021-02-12 09:32:30 -08001549 sp<SurfaceControl> mBufferQueueSurfaceControl;
Valerie Hau871d6352020-01-29 08:44:02 -08001550};
1551
1552TEST_F(BLASTFrameEventHistoryTest, FrameEventHistory_Basic) {
1553 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
1554 sp<IGraphicBufferProducer> igbProducer;
1555 ProducerFrameEventHistory history;
1556 setUpProducer(adapter, igbProducer);
1557
1558 IGraphicBufferProducer::QueueBufferOutput qbOutput;
1559 nsecs_t requestedPresentTimeA = 0;
1560 nsecs_t postedTimeA = 0;
1561 setUpAndQueueBuffer(igbProducer, &requestedPresentTimeA, &postedTimeA, &qbOutput, true);
1562 history.applyDelta(qbOutput.frameTimestamps);
1563
1564 FrameEvents* events = nullptr;
1565 events = history.getFrame(1);
1566 ASSERT_NE(nullptr, events);
1567 ASSERT_EQ(1, events->frameNumber);
1568 ASSERT_EQ(requestedPresentTimeA, events->requestedPresentTime);
1569 ASSERT_GE(events->postedTime, postedTimeA);
1570
Vishnu Nair1506b182021-02-22 14:35:15 -08001571 adapter.waitForCallback(1);
Valerie Hau871d6352020-01-29 08:44:02 -08001572
1573 // queue another buffer so we query for frame event deltas
1574 nsecs_t requestedPresentTimeB = 0;
1575 nsecs_t postedTimeB = 0;
1576 setUpAndQueueBuffer(igbProducer, &requestedPresentTimeB, &postedTimeB, &qbOutput, true);
1577 history.applyDelta(qbOutput.frameTimestamps);
1578 events = history.getFrame(1);
1579 ASSERT_NE(nullptr, events);
1580
1581 // frame number, requestedPresentTime, and postTime should not have changed
1582 ASSERT_EQ(1, events->frameNumber);
1583 ASSERT_EQ(requestedPresentTimeA, events->requestedPresentTime);
1584 ASSERT_GE(events->postedTime, postedTimeA);
1585
1586 ASSERT_GE(events->latchTime, postedTimeA);
1587 ASSERT_GE(events->dequeueReadyTime, events->latchTime);
1588 ASSERT_NE(nullptr, events->gpuCompositionDoneFence);
1589 ASSERT_NE(nullptr, events->displayPresentFence);
1590 ASSERT_NE(nullptr, events->releaseFence);
1591
1592 // we should also have gotten the initial values for the next frame
1593 events = history.getFrame(2);
1594 ASSERT_NE(nullptr, events);
1595 ASSERT_EQ(2, events->frameNumber);
1596 ASSERT_EQ(requestedPresentTimeB, events->requestedPresentTime);
1597 ASSERT_GE(events->postedTime, postedTimeB);
Valerie Hau78491e92020-04-15 13:10:56 -07001598
1599 // wait for any callbacks that have not been received
1600 adapter.waitForCallbacks();
Valerie Hau871d6352020-01-29 08:44:02 -08001601}
Vishnu Nair083efd32021-02-12 09:32:30 -08001602
Vishnu Nair083efd32021-02-12 09:32:30 -08001603TEST_F(BLASTFrameEventHistoryTest, FrameEventHistory_DroppedFrame) {
1604 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
1605 sp<IGraphicBufferProducer> igbProducer;
1606 setUpProducer(adapter, igbProducer);
1607
1608 ProducerFrameEventHistory history;
1609 IGraphicBufferProducer::QueueBufferOutput qbOutput;
1610 nsecs_t requestedPresentTimeA = 0;
1611 nsecs_t postedTimeA = 0;
Vishnu Nairde66dc72021-06-17 17:54:41 -07001612 // Present the frame sometime in the future so we can add two frames to the queue so the older
1613 // one will be dropped.
1614 nsecs_t presentTime = systemTime() + std::chrono::nanoseconds(500ms).count();
Vishnu Nair083efd32021-02-12 09:32:30 -08001615 setUpAndQueueBuffer(igbProducer, &requestedPresentTimeA, &postedTimeA, &qbOutput, true,
Vishnu Nairde66dc72021-06-17 17:54:41 -07001616 presentTime);
Vishnu Nair083efd32021-02-12 09:32:30 -08001617 history.applyDelta(qbOutput.frameTimestamps);
1618
1619 FrameEvents* events = nullptr;
1620 events = history.getFrame(1);
1621 ASSERT_NE(nullptr, events);
1622 ASSERT_EQ(1, events->frameNumber);
1623 ASSERT_EQ(requestedPresentTimeA, events->requestedPresentTime);
1624 ASSERT_GE(events->postedTime, postedTimeA);
1625
1626 // queue another buffer so the first can be dropped
1627 nsecs_t requestedPresentTimeB = 0;
1628 nsecs_t postedTimeB = 0;
Vishnu Nairde66dc72021-06-17 17:54:41 -07001629 presentTime = systemTime() + std::chrono::nanoseconds(1ms).count();
1630 setUpAndQueueBuffer(igbProducer, &requestedPresentTimeB, &postedTimeB, &qbOutput, true,
1631 presentTime);
Vishnu Nair083efd32021-02-12 09:32:30 -08001632 history.applyDelta(qbOutput.frameTimestamps);
1633 events = history.getFrame(1);
1634 ASSERT_NE(nullptr, events);
1635
1636 // frame number, requestedPresentTime, and postTime should not have changed
1637 ASSERT_EQ(1, events->frameNumber);
1638 ASSERT_EQ(requestedPresentTimeA, events->requestedPresentTime);
1639 ASSERT_GE(events->postedTime, postedTimeA);
1640
Vishnu Nairde66dc72021-06-17 17:54:41 -07001641 // a valid latchtime and pre and post composition info should not be set for the dropped frame
Vishnu Nair083efd32021-02-12 09:32:30 -08001642 ASSERT_FALSE(events->hasLatchInfo());
1643 ASSERT_FALSE(events->hasDequeueReadyInfo());
Vishnu Nairde66dc72021-06-17 17:54:41 -07001644 ASSERT_FALSE(events->hasGpuCompositionDoneInfo());
1645 ASSERT_FALSE(events->hasDisplayPresentInfo());
1646 ASSERT_FALSE(events->hasReleaseInfo());
Vishnu Nair083efd32021-02-12 09:32:30 -08001647
Vishnu Nairde66dc72021-06-17 17:54:41 -07001648 // wait for the last transaction to be completed.
1649 adapter.waitForCallback(2);
Vishnu Nair083efd32021-02-12 09:32:30 -08001650
Vishnu Nairde66dc72021-06-17 17:54:41 -07001651 // queue another buffer so we query for frame event deltas
1652 nsecs_t requestedPresentTimeC = 0;
1653 nsecs_t postedTimeC = 0;
1654 setUpAndQueueBuffer(igbProducer, &requestedPresentTimeC, &postedTimeC, &qbOutput, true);
1655 history.applyDelta(qbOutput.frameTimestamps);
1656
1657 // frame number, requestedPresentTime, and postTime should not have changed
1658 ASSERT_EQ(1, events->frameNumber);
1659 ASSERT_EQ(requestedPresentTimeA, events->requestedPresentTime);
1660 ASSERT_GE(events->postedTime, postedTimeA);
1661
1662 // a valid latchtime and pre and post composition info should not be set for the dropped frame
1663 ASSERT_FALSE(events->hasLatchInfo());
1664 ASSERT_FALSE(events->hasDequeueReadyInfo());
1665 ASSERT_FALSE(events->hasGpuCompositionDoneInfo());
1666 ASSERT_FALSE(events->hasDisplayPresentInfo());
1667 ASSERT_FALSE(events->hasReleaseInfo());
1668
1669 // we should also have gotten values for the presented frame
Vishnu Nair083efd32021-02-12 09:32:30 -08001670 events = history.getFrame(2);
1671 ASSERT_NE(nullptr, events);
1672 ASSERT_EQ(2, events->frameNumber);
1673 ASSERT_EQ(requestedPresentTimeB, events->requestedPresentTime);
1674 ASSERT_GE(events->postedTime, postedTimeB);
Vishnu Nairde66dc72021-06-17 17:54:41 -07001675 ASSERT_GE(events->latchTime, postedTimeB);
1676 ASSERT_GE(events->dequeueReadyTime, events->latchTime);
1677 ASSERT_NE(nullptr, events->gpuCompositionDoneFence);
1678 ASSERT_NE(nullptr, events->displayPresentFence);
1679 ASSERT_NE(nullptr, events->releaseFence);
1680
1681 // wait for any callbacks that have not been received
1682 adapter.waitForCallbacks();
Vishnu Nair083efd32021-02-12 09:32:30 -08001683}
1684
Vishnu Nair9a69a042021-06-18 13:19:49 -07001685TEST_F(BLASTFrameEventHistoryTest, FrameEventHistory_CompositorTimings) {
1686 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
1687 sp<IGraphicBufferProducer> igbProducer;
1688 ProducerFrameEventHistory history;
1689 setUpProducer(adapter, igbProducer);
1690
1691 IGraphicBufferProducer::QueueBufferOutput qbOutput;
1692 nsecs_t requestedPresentTimeA = 0;
1693 nsecs_t postedTimeA = 0;
Vishnu Nair9a69a042021-06-18 13:19:49 -07001694 setUpAndQueueBuffer(igbProducer, &requestedPresentTimeA, &postedTimeA, &qbOutput, true);
1695 history.applyDelta(qbOutput.frameTimestamps);
1696 adapter.waitForCallback(1);
1697
1698 // queue another buffer so we query for frame event deltas
1699 nsecs_t requestedPresentTimeB = 0;
1700 nsecs_t postedTimeB = 0;
1701 setUpAndQueueBuffer(igbProducer, &requestedPresentTimeB, &postedTimeB, &qbOutput, true);
1702 history.applyDelta(qbOutput.frameTimestamps);
1703
1704 // check for a valid compositor deadline
1705 ASSERT_NE(0, history.getReportedCompositeDeadline());
1706
1707 // wait for any callbacks that have not been received
1708 adapter.waitForCallbacks();
1709}
1710
Valerie Hauc5011f92019-10-11 09:52:07 -07001711} // namespace android