blob: 179bdd76aa9e441485616d3f74193d9ad701f5ac [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>
Valerie Haud3b90d22019-11-06 09:37:31 -080022#include <gui/BufferQueueCore.h>
23#include <gui/BufferQueueProducer.h>
Valerie Hau871d6352020-01-29 08:44:02 -080024#include <gui/FrameTimestamps.h>
Valerie Hauda3446e2019-10-14 15:49:22 -070025#include <gui/IGraphicBufferProducer.h>
26#include <gui/IProducerListener.h>
Vishnu Nair17dde612020-12-28 11:39:59 -080027#include <gui/Surface.h>
Valerie Hauc5011f92019-10-11 09:52:07 -070028#include <gui/SurfaceComposerClient.h>
chaviwe7b9f272020-08-18 16:08:59 -070029#include <gui/SyncScreenCaptureListener.h>
chaviwd7deef72021-10-06 11:53:40 -050030#include <gui/test/CallbackUtils.h>
Valerie Hauda3446e2019-10-14 15:49:22 -070031#include <private/gui/ComposerService.h>
Huihong Luo9e84f332021-12-16 14:33:46 -080032#include <private/gui/ComposerServiceAIDL.h>
Marin Shalamanova7fe3042021-01-29 21:02:08 +010033#include <ui/DisplayMode.h>
Valerie Hauc5011f92019-10-11 09:52:07 -070034#include <ui/GraphicBuffer.h>
Valerie Hauda3446e2019-10-14 15:49:22 -070035#include <ui/GraphicTypes.h>
Valerie Hau8cee3f92019-11-06 10:06:28 -080036#include <ui/Transform.h>
Valerie Hauc5011f92019-10-11 09:52:07 -070037
38#include <gtest/gtest.h>
39
40using namespace std::chrono_literals;
41
42namespace android {
43
Valerie Hauc5011f92019-10-11 09:52:07 -070044using Transaction = SurfaceComposerClient::Transaction;
Valerie Hauda3446e2019-10-14 15:49:22 -070045using android::hardware::graphics::common::V1_2::BufferUsage;
Valerie Hauc5011f92019-10-11 09:52:07 -070046
chaviwd7deef72021-10-06 11:53:40 -050047class CountProducerListener : public BnProducerListener {
48public:
49 void onBufferReleased() override {
50 std::scoped_lock<std::mutex> lock(mMutex);
51 mNumReleased++;
52 mReleaseCallback.notify_one();
53 }
54
55 void waitOnNumberReleased(int32_t expectedNumReleased) {
56 std::unique_lock<std::mutex> lock(mMutex);
57 while (mNumReleased < expectedNumReleased) {
58 ASSERT_NE(mReleaseCallback.wait_for(lock, std::chrono::seconds(3)),
59 std::cv_status::timeout)
60 << "did not receive release";
61 }
62 }
63
64private:
65 std::mutex mMutex;
66 std::condition_variable mReleaseCallback;
67 int32_t mNumReleased GUARDED_BY(mMutex) = 0;
68};
69
chaviwf10b9042021-10-13 15:48:59 -050070class TestBLASTBufferQueue : public BLASTBufferQueue {
71public:
72 TestBLASTBufferQueue(const std::string& name, const sp<SurfaceControl>& surface, int width,
73 int height, int32_t format)
74 : BLASTBufferQueue(name, surface, width, height, format) {}
75
chaviw6b9ffea2021-11-08 09:25:48 -060076 void transactionCallback(nsecs_t latchTime, const sp<Fence>& presentFence,
77 const std::vector<SurfaceControlStats>& stats) override {
78 BLASTBufferQueue::transactionCallback(latchTime, presentFence, stats);
chaviwf10b9042021-10-13 15:48:59 -050079 uint64_t frameNumber = stats[0].frameEventStats.frameNumber;
80
81 {
82 std::unique_lock lock{frameNumberMutex};
chaviw6b9ffea2021-11-08 09:25:48 -060083 mLastTransactionFrameNumber = frameNumber;
84 mWaitForCallbackCV.notify_all();
chaviwf10b9042021-10-13 15:48:59 -050085 }
86 }
87
88 void waitForCallback(int64_t frameNumber) {
89 std::unique_lock lock{frameNumberMutex};
90 // Wait until all but one of the submitted buffers have been released.
chaviw6b9ffea2021-11-08 09:25:48 -060091 while (mLastTransactionFrameNumber < frameNumber) {
92 mWaitForCallbackCV.wait(lock);
chaviwf10b9042021-10-13 15:48:59 -050093 }
94 }
95
96private:
97 std::mutex frameNumberMutex;
chaviw6b9ffea2021-11-08 09:25:48 -060098 std::condition_variable mWaitForCallbackCV;
99 int64_t mLastTransactionFrameNumber = -1;
chaviwf10b9042021-10-13 15:48:59 -0500100};
101
Valerie Hauc5011f92019-10-11 09:52:07 -0700102class BLASTBufferQueueHelper {
103public:
104 BLASTBufferQueueHelper(const sp<SurfaceControl>& sc, int width, int height) {
chaviwf10b9042021-10-13 15:48:59 -0500105 mBlastBufferQueueAdapter = new TestBLASTBufferQueue("TestBLASTBufferQueue", sc, width,
106 height, PIXEL_FORMAT_RGBA_8888);
Valerie Hauc5011f92019-10-11 09:52:07 -0700107 }
108
109 void update(const sp<SurfaceControl>& sc, int width, int height) {
chaviw565ee542021-01-14 10:21:23 -0800110 mBlastBufferQueueAdapter->update(sc, width, height, PIXEL_FORMAT_RGBA_8888);
Valerie Hauc5011f92019-10-11 09:52:07 -0700111 }
112
chaviw0acd33a2021-11-02 11:55:37 -0500113 void setSyncTransaction(Transaction* next, bool acquireSingleBuffer = true) {
114 mBlastBufferQueueAdapter->setSyncTransaction(next, acquireSingleBuffer);
Valerie Hauc5011f92019-10-11 09:52:07 -0700115 }
116
Vishnu Nairea0de002020-11-17 17:42:37 -0800117 int getWidth() { return mBlastBufferQueueAdapter->mSize.width; }
Valerie Hauda3446e2019-10-14 15:49:22 -0700118
Vishnu Nairea0de002020-11-17 17:42:37 -0800119 int getHeight() { return mBlastBufferQueueAdapter->mSize.height; }
Valerie Hauda3446e2019-10-14 15:49:22 -0700120
chaviwa1c4c822021-11-10 18:11:58 -0600121 Transaction* getSyncTransaction() { return mBlastBufferQueueAdapter->mSyncTransaction; }
Valerie Hauda3446e2019-10-14 15:49:22 -0700122
123 sp<IGraphicBufferProducer> getIGraphicBufferProducer() {
124 return mBlastBufferQueueAdapter->getIGraphicBufferProducer();
125 }
126
Valerie Hauc5011f92019-10-11 09:52:07 -0700127 const sp<SurfaceControl> getSurfaceControl() {
128 return mBlastBufferQueueAdapter->mSurfaceControl;
129 }
130
Vishnu Naira4fbca52021-07-07 16:52:34 -0700131 sp<Surface> getSurface() {
132 return mBlastBufferQueueAdapter->getSurface(false /* includeSurfaceControlHandle */);
133 }
134
Valerie Haud3b90d22019-11-06 09:37:31 -0800135 void waitForCallbacks() {
Valerie Hauda3446e2019-10-14 15:49:22 -0700136 std::unique_lock lock{mBlastBufferQueueAdapter->mMutex};
Vishnu Nair1506b182021-02-22 14:35:15 -0800137 // Wait until all but one of the submitted buffers have been released.
138 while (mBlastBufferQueueAdapter->mSubmitted.size() > 1) {
Valerie Haud3b90d22019-11-06 09:37:31 -0800139 mBlastBufferQueueAdapter->mCallbackCV.wait(lock);
140 }
Valerie Hauda3446e2019-10-14 15:49:22 -0700141 }
142
Vishnu Nair1506b182021-02-22 14:35:15 -0800143 void waitForCallback(int64_t frameNumber) {
chaviwf10b9042021-10-13 15:48:59 -0500144 mBlastBufferQueueAdapter->waitForCallback(frameNumber);
Vishnu Nair1506b182021-02-22 14:35:15 -0800145 }
146
chaviw0acd33a2021-11-02 11:55:37 -0500147 void validateNumFramesSubmitted(int64_t numFramesSubmitted) {
148 std::unique_lock lock{mBlastBufferQueueAdapter->mMutex};
149 ASSERT_EQ(numFramesSubmitted, mBlastBufferQueueAdapter->mSubmitted.size());
150 }
151
Valerie Hauc5011f92019-10-11 09:52:07 -0700152private:
chaviwf10b9042021-10-13 15:48:59 -0500153 sp<TestBLASTBufferQueue> mBlastBufferQueueAdapter;
Valerie Hauc5011f92019-10-11 09:52:07 -0700154};
155
156class BLASTBufferQueueTest : public ::testing::Test {
157public:
158protected:
159 BLASTBufferQueueTest() {
160 const ::testing::TestInfo* const testInfo =
161 ::testing::UnitTest::GetInstance()->current_test_info();
162 ALOGV("Begin test: %s.%s", testInfo->test_case_name(), testInfo->name());
163 }
164
165 ~BLASTBufferQueueTest() {
166 const ::testing::TestInfo* const testInfo =
167 ::testing::UnitTest::GetInstance()->current_test_info();
168 ALOGV("End test: %s.%s", testInfo->test_case_name(), testInfo->name());
169 }
170
171 void SetUp() {
Valerie Hauda3446e2019-10-14 15:49:22 -0700172 mComposer = ComposerService::getComposerService();
Valerie Hauc5011f92019-10-11 09:52:07 -0700173 mClient = new SurfaceComposerClient();
Valerie Hauda3446e2019-10-14 15:49:22 -0700174 mDisplayToken = mClient->getInternalDisplayToken();
175 ASSERT_NE(nullptr, mDisplayToken.get());
176 Transaction t;
Dominik Laskowski29fa1462021-04-27 15:51:50 -0700177 t.setDisplayLayerStack(mDisplayToken, ui::DEFAULT_LAYER_STACK);
Valerie Hauda3446e2019-10-14 15:49:22 -0700178 t.apply();
179 t.clear();
180
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100181 ui::DisplayMode mode;
182 ASSERT_EQ(NO_ERROR, SurfaceComposerClient::getActiveDisplayMode(mDisplayToken, &mode));
183 const ui::Size& resolution = mode.resolution;
Dominik Laskowski3cb3d4e2019-11-21 11:14:45 -0800184 mDisplayWidth = resolution.getWidth();
185 mDisplayHeight = resolution.getHeight();
Valerie Hauda3446e2019-10-14 15:49:22 -0700186
187 mSurfaceControl = mClient->createSurface(String8("TestSurface"), mDisplayWidth,
188 mDisplayHeight, PIXEL_FORMAT_RGBA_8888,
189 ISurfaceComposerClient::eFXSurfaceBufferState,
190 /*parent*/ nullptr);
Dominik Laskowski29fa1462021-04-27 15:51:50 -0700191 t.setLayerStack(mSurfaceControl, ui::DEFAULT_LAYER_STACK)
Valerie Hauda3446e2019-10-14 15:49:22 -0700192 .setLayer(mSurfaceControl, std::numeric_limits<int32_t>::max())
Valerie Hauda3446e2019-10-14 15:49:22 -0700193 .show(mSurfaceControl)
194 .setDataspace(mSurfaceControl, ui::Dataspace::V0_SRGB)
195 .apply();
chaviwd2432892020-07-24 17:42:39 -0700196
197 mCaptureArgs.displayToken = mDisplayToken;
arthurhung6fa58b72020-11-05 11:56:00 +0800198 mCaptureArgs.dataspace = ui::Dataspace::V0_SRGB;
Valerie Hauda3446e2019-10-14 15:49:22 -0700199 }
200
chaviwd7deef72021-10-06 11:53:40 -0500201 void setUpProducer(BLASTBufferQueueHelper& adapter, sp<IGraphicBufferProducer>& producer,
202 int32_t maxBufferCount = 2) {
Vishnu Nair083efd32021-02-12 09:32:30 -0800203 producer = adapter.getIGraphicBufferProducer();
chaviwd7deef72021-10-06 11:53:40 -0500204 setUpProducer(producer, maxBufferCount);
Vishnu Nair083efd32021-02-12 09:32:30 -0800205 }
206
chaviwd7deef72021-10-06 11:53:40 -0500207 void setUpProducer(sp<IGraphicBufferProducer>& igbProducer, int32_t maxBufferCount) {
Valerie Haud3b90d22019-11-06 09:37:31 -0800208 ASSERT_NE(nullptr, igbProducer.get());
chaviwd7deef72021-10-06 11:53:40 -0500209 ASSERT_EQ(NO_ERROR, igbProducer->setMaxDequeuedBufferCount(maxBufferCount));
Valerie Haud3b90d22019-11-06 09:37:31 -0800210 IGraphicBufferProducer::QueueBufferOutput qbOutput;
chaviwd7deef72021-10-06 11:53:40 -0500211 mProducerListener = new CountProducerListener();
Valerie Haud3b90d22019-11-06 09:37:31 -0800212 ASSERT_EQ(NO_ERROR,
chaviwd7deef72021-10-06 11:53:40 -0500213 igbProducer->connect(mProducerListener, NATIVE_WINDOW_API_CPU, false, &qbOutput));
Dominik Laskowski718f9602019-11-09 20:01:35 -0800214 ASSERT_NE(ui::Transform::ROT_INVALID, qbOutput.transformHint);
Valerie Haud3b90d22019-11-06 09:37:31 -0800215 }
216
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800217 void fillBuffer(uint32_t* bufData, Rect rect, uint32_t stride, uint8_t r, uint8_t g,
218 uint8_t b) {
219 for (uint32_t row = rect.top; row < rect.bottom; row++) {
220 for (uint32_t col = rect.left; col < rect.right; col++) {
Valerie Hauda3446e2019-10-14 15:49:22 -0700221 uint8_t* pixel = (uint8_t*)(bufData + (row * stride) + col);
222 *pixel = r;
223 *(pixel + 1) = g;
224 *(pixel + 2) = b;
225 *(pixel + 3) = 255;
226 }
227 }
228 }
229
Valerie Hau5977fc82019-12-05 15:56:39 -0800230 void fillQuadrants(sp<GraphicBuffer>& buf) {
231 const auto bufWidth = buf->getWidth();
232 const auto bufHeight = buf->getHeight();
233 uint32_t* bufData;
234 buf->lock(static_cast<uint32_t>(GraphicBuffer::USAGE_SW_WRITE_OFTEN),
235 reinterpret_cast<void**>(&bufData));
236 fillBuffer(bufData, Rect(0, 0, bufWidth / 2, bufHeight / 2), buf->getStride(), 0, 0, 0);
237 fillBuffer(bufData, Rect(bufWidth / 2, 0, bufWidth, bufHeight / 2), buf->getStride(), 255,
238 0, 0);
239 fillBuffer(bufData, Rect(bufWidth / 2, bufHeight / 2, bufWidth, bufHeight),
240 buf->getStride(), 0, 255, 0);
241 fillBuffer(bufData, Rect(0, bufHeight / 2, bufWidth / 2, bufHeight), buf->getStride(), 0, 0,
242 255);
243 buf->unlock();
244 }
245
246 void checkScreenCapture(uint8_t r, uint8_t g, uint8_t b, Rect region, int32_t border = 0,
247 bool outsideRegion = false) {
chaviwd2432892020-07-24 17:42:39 -0700248 sp<GraphicBuffer>& captureBuf = mCaptureResults.buffer;
Valerie Hau5977fc82019-12-05 15:56:39 -0800249 const auto epsilon = 3;
chaviwd2432892020-07-24 17:42:39 -0700250 const auto width = captureBuf->getWidth();
251 const auto height = captureBuf->getHeight();
252 const auto stride = captureBuf->getStride();
Valerie Hauda3446e2019-10-14 15:49:22 -0700253
254 uint32_t* bufData;
chaviwd2432892020-07-24 17:42:39 -0700255 captureBuf->lock(static_cast<uint32_t>(GraphicBuffer::USAGE_SW_READ_OFTEN),
256 reinterpret_cast<void**>(&bufData));
Valerie Hauda3446e2019-10-14 15:49:22 -0700257
258 for (uint32_t row = 0; row < height; row++) {
259 for (uint32_t col = 0; col < width; col++) {
260 uint8_t* pixel = (uint8_t*)(bufData + (row * stride) + col);
arthurhung6fa58b72020-11-05 11:56:00 +0800261 ASSERT_NE(nullptr, pixel);
Valerie Hau5977fc82019-12-05 15:56:39 -0800262 bool inRegion;
263 if (!outsideRegion) {
264 inRegion = row >= region.top + border && row < region.bottom - border &&
265 col >= region.left + border && col < region.right - border;
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800266 } else {
Valerie Hau5977fc82019-12-05 15:56:39 -0800267 inRegion = row >= region.top - border && row < region.bottom + border &&
268 col >= region.left - border && col < region.right + border;
269 }
270 if (!outsideRegion && inRegion) {
Chavi Weingartena5aedbd2021-04-09 13:37:33 +0000271 ASSERT_GE(epsilon, abs(r - *(pixel)));
272 ASSERT_GE(epsilon, abs(g - *(pixel + 1)));
273 ASSERT_GE(epsilon, abs(b - *(pixel + 2)));
Valerie Hau5977fc82019-12-05 15:56:39 -0800274 } else if (outsideRegion && !inRegion) {
Chavi Weingartena5aedbd2021-04-09 13:37:33 +0000275 ASSERT_GE(epsilon, abs(r - *(pixel)));
276 ASSERT_GE(epsilon, abs(g - *(pixel + 1)));
277 ASSERT_GE(epsilon, abs(b - *(pixel + 2)));
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800278 }
Vishnu Nair1506b182021-02-22 14:35:15 -0800279 ASSERT_EQ(false, ::testing::Test::HasFailure());
Valerie Hauda3446e2019-10-14 15:49:22 -0700280 }
281 }
chaviwd2432892020-07-24 17:42:39 -0700282 captureBuf->unlock();
Valerie Hauc5011f92019-10-11 09:52:07 -0700283 }
284
chaviw8ffc7b82020-08-18 11:25:37 -0700285 static status_t captureDisplay(DisplayCaptureArgs& captureArgs,
286 ScreenCaptureResults& captureResults) {
Huihong Luo9e84f332021-12-16 14:33:46 -0800287 const auto sf = ComposerServiceAIDL::getComposerService();
chaviw8ffc7b82020-08-18 11:25:37 -0700288 SurfaceComposerClient::Transaction().apply(true);
289
290 const sp<SyncScreenCaptureListener> captureListener = new SyncScreenCaptureListener();
Huihong Luo9e84f332021-12-16 14:33:46 -0800291 binder::Status status = sf->captureDisplay(captureArgs, captureListener);
292 if (status.transactionError() != NO_ERROR) {
293 return status.transactionError();
chaviw8ffc7b82020-08-18 11:25:37 -0700294 }
295 captureResults = captureListener->waitForResults();
296 return captureResults.result;
297 }
298
Vishnu Nair277142c2021-01-05 18:35:29 -0800299 void queueBuffer(sp<IGraphicBufferProducer> igbp, uint8_t r, uint8_t g, uint8_t b,
300 nsecs_t presentTimeDelay) {
301 int slot;
302 sp<Fence> fence;
303 sp<GraphicBuffer> buf;
304 auto ret = igbp->dequeueBuffer(&slot, &fence, mDisplayWidth, mDisplayHeight,
305 PIXEL_FORMAT_RGBA_8888, GRALLOC_USAGE_SW_WRITE_OFTEN,
306 nullptr, nullptr);
chaviw0acd33a2021-11-02 11:55:37 -0500307 ASSERT_TRUE(ret == IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION || ret == NO_ERROR);
Vishnu Nair277142c2021-01-05 18:35:29 -0800308 ASSERT_EQ(OK, igbp->requestBuffer(slot, &buf));
309
310 uint32_t* bufData;
311 buf->lock(static_cast<uint32_t>(GraphicBuffer::USAGE_SW_WRITE_OFTEN),
312 reinterpret_cast<void**>(&bufData));
313 fillBuffer(bufData, Rect(buf->getWidth(), buf->getHeight() / 2), buf->getStride(), r, g, b);
314 buf->unlock();
315
316 IGraphicBufferProducer::QueueBufferOutput qbOutput;
317 nsecs_t timestampNanos = systemTime() + presentTimeDelay;
318 IGraphicBufferProducer::QueueBufferInput input(timestampNanos, false, HAL_DATASPACE_UNKNOWN,
319 Rect(mDisplayWidth, mDisplayHeight / 2),
320 NATIVE_WINDOW_SCALING_MODE_FREEZE, 0,
321 Fence::NO_FENCE);
322 igbp->queueBuffer(slot, input, &qbOutput);
323 }
324
Valerie Hauc5011f92019-10-11 09:52:07 -0700325 sp<SurfaceComposerClient> mClient;
Valerie Hauda3446e2019-10-14 15:49:22 -0700326 sp<ISurfaceComposer> mComposer;
327
328 sp<IBinder> mDisplayToken;
329
Valerie Hauc5011f92019-10-11 09:52:07 -0700330 sp<SurfaceControl> mSurfaceControl;
Valerie Hauda3446e2019-10-14 15:49:22 -0700331
332 uint32_t mDisplayWidth;
333 uint32_t mDisplayHeight;
chaviwd2432892020-07-24 17:42:39 -0700334
335 DisplayCaptureArgs mCaptureArgs;
336 ScreenCaptureResults mCaptureResults;
chaviwd7deef72021-10-06 11:53:40 -0500337 sp<CountProducerListener> mProducerListener;
Valerie Hauc5011f92019-10-11 09:52:07 -0700338};
339
340TEST_F(BLASTBufferQueueTest, CreateBLASTBufferQueue) {
341 // create BLASTBufferQueue adapter associated with this surface
Valerie Hauda3446e2019-10-14 15:49:22 -0700342 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
Valerie Hauc5011f92019-10-11 09:52:07 -0700343 ASSERT_EQ(mSurfaceControl, adapter.getSurfaceControl());
Valerie Hauda3446e2019-10-14 15:49:22 -0700344 ASSERT_EQ(mDisplayWidth, adapter.getWidth());
345 ASSERT_EQ(mDisplayHeight, adapter.getHeight());
chaviwa1c4c822021-11-10 18:11:58 -0600346 ASSERT_EQ(nullptr, adapter.getSyncTransaction());
Valerie Hauc5011f92019-10-11 09:52:07 -0700347}
348
349TEST_F(BLASTBufferQueueTest, Update) {
Valerie Hauda3446e2019-10-14 15:49:22 -0700350 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
Valerie Hauc5011f92019-10-11 09:52:07 -0700351 sp<SurfaceControl> updateSurface =
Valerie Hauda3446e2019-10-14 15:49:22 -0700352 mClient->createSurface(String8("UpdateTest"), mDisplayWidth / 2, mDisplayHeight / 2,
353 PIXEL_FORMAT_RGBA_8888);
354 adapter.update(updateSurface, mDisplayWidth / 2, mDisplayHeight / 2);
Valerie Hauc5011f92019-10-11 09:52:07 -0700355 ASSERT_EQ(updateSurface, adapter.getSurfaceControl());
Vishnu Nairea0de002020-11-17 17:42:37 -0800356 sp<IGraphicBufferProducer> igbProducer;
357 setUpProducer(adapter, igbProducer);
358
359 int32_t width;
360 igbProducer->query(NATIVE_WINDOW_WIDTH, &width);
361 ASSERT_EQ(mDisplayWidth / 2, width);
362 int32_t height;
363 igbProducer->query(NATIVE_WINDOW_HEIGHT, &height);
364 ASSERT_EQ(mDisplayHeight / 2, height);
Valerie Hauc5011f92019-10-11 09:52:07 -0700365}
366
chaviwa1c4c822021-11-10 18:11:58 -0600367TEST_F(BLASTBufferQueueTest, SetSyncTransaction) {
Valerie Hauda3446e2019-10-14 15:49:22 -0700368 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
chaviwa1c4c822021-11-10 18:11:58 -0600369 Transaction sync;
370 adapter.setSyncTransaction(&sync);
371 ASSERT_EQ(&sync, adapter.getSyncTransaction());
Valerie Hauc5011f92019-10-11 09:52:07 -0700372}
Valerie Hauda3446e2019-10-14 15:49:22 -0700373
Valerie Haubf29e042020-02-06 11:40:38 -0800374TEST_F(BLASTBufferQueueTest, DISABLED_onFrameAvailable_ApplyDesiredPresentTime) {
Valerie Hau181abd32020-01-27 14:18:28 -0800375 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
376 sp<IGraphicBufferProducer> igbProducer;
377 setUpProducer(adapter, igbProducer);
378
379 int slot;
380 sp<Fence> fence;
381 sp<GraphicBuffer> buf;
382 auto ret = igbProducer->dequeueBuffer(&slot, &fence, mDisplayWidth, mDisplayHeight,
383 PIXEL_FORMAT_RGBA_8888, GRALLOC_USAGE_SW_WRITE_OFTEN,
384 nullptr, nullptr);
385 ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, ret);
386 ASSERT_EQ(OK, igbProducer->requestBuffer(slot, &buf));
387
388 nsecs_t desiredPresentTime = systemTime() + nsecs_t(5 * 1e8);
389 IGraphicBufferProducer::QueueBufferOutput qbOutput;
Vishnu Nair1506b182021-02-22 14:35:15 -0800390 IGraphicBufferProducer::QueueBufferInput input(desiredPresentTime, true /* autotimestamp */,
391 HAL_DATASPACE_UNKNOWN,
Valerie Hau181abd32020-01-27 14:18:28 -0800392 Rect(mDisplayWidth, mDisplayHeight),
393 NATIVE_WINDOW_SCALING_MODE_FREEZE, 0,
394 Fence::NO_FENCE);
395 igbProducer->queueBuffer(slot, input, &qbOutput);
396 ASSERT_NE(ui::Transform::ROT_INVALID, qbOutput.transformHint);
397
398 adapter.waitForCallbacks();
399 ASSERT_GE(systemTime(), desiredPresentTime);
400}
401
Valerie Hauda3446e2019-10-14 15:49:22 -0700402TEST_F(BLASTBufferQueueTest, onFrameAvailable_Apply) {
403 uint8_t r = 255;
404 uint8_t g = 0;
405 uint8_t b = 0;
406
407 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
Valerie Haud3b90d22019-11-06 09:37:31 -0800408 sp<IGraphicBufferProducer> igbProducer;
409 setUpProducer(adapter, igbProducer);
Valerie Hauda3446e2019-10-14 15:49:22 -0700410
411 int slot;
412 sp<Fence> fence;
413 sp<GraphicBuffer> buf;
414 auto ret = igbProducer->dequeueBuffer(&slot, &fence, mDisplayWidth, mDisplayHeight,
415 PIXEL_FORMAT_RGBA_8888, GRALLOC_USAGE_SW_WRITE_OFTEN,
416 nullptr, nullptr);
417 ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, ret);
418 ASSERT_EQ(OK, igbProducer->requestBuffer(slot, &buf));
419
420 uint32_t* bufData;
421 buf->lock(static_cast<uint32_t>(GraphicBuffer::USAGE_SW_WRITE_OFTEN),
422 reinterpret_cast<void**>(&bufData));
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800423 fillBuffer(bufData, Rect(buf->getWidth(), buf->getHeight()), buf->getStride(), r, g, b);
Valerie Hauda3446e2019-10-14 15:49:22 -0700424 buf->unlock();
425
Valerie Haud3b90d22019-11-06 09:37:31 -0800426 IGraphicBufferProducer::QueueBufferOutput qbOutput;
Vishnu Nair1506b182021-02-22 14:35:15 -0800427 IGraphicBufferProducer::QueueBufferInput input(systemTime(), true /* autotimestamp */,
428 HAL_DATASPACE_UNKNOWN,
Valerie Hauda3446e2019-10-14 15:49:22 -0700429 Rect(mDisplayWidth, mDisplayHeight),
430 NATIVE_WINDOW_SCALING_MODE_FREEZE, 0,
431 Fence::NO_FENCE);
432 igbProducer->queueBuffer(slot, input, &qbOutput);
Dominik Laskowski718f9602019-11-09 20:01:35 -0800433 ASSERT_NE(ui::Transform::ROT_INVALID, qbOutput.transformHint);
Valerie Hauda3446e2019-10-14 15:49:22 -0700434
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800435 adapter.waitForCallbacks();
Valerie Hauda3446e2019-10-14 15:49:22 -0700436
437 // capture screen and verify that it is red
chaviw8ffc7b82020-08-18 11:25:37 -0700438 ASSERT_EQ(NO_ERROR, captureDisplay(mCaptureArgs, mCaptureResults));
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800439 ASSERT_NO_FATAL_FAILURE(
440 checkScreenCapture(r, g, b, {0, 0, (int32_t)mDisplayWidth, (int32_t)mDisplayHeight}));
Valerie Hauda3446e2019-10-14 15:49:22 -0700441}
Valerie Haud3b90d22019-11-06 09:37:31 -0800442
443TEST_F(BLASTBufferQueueTest, TripleBuffering) {
444 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
445 sp<IGraphicBufferProducer> igbProducer;
446 setUpProducer(adapter, igbProducer);
447
448 std::vector<std::pair<int, sp<Fence>>> allocated;
Ady Abraham0bde6b52021-05-18 13:57:02 -0700449 int minUndequeuedBuffers = 0;
450 ASSERT_EQ(OK, igbProducer->query(NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS, &minUndequeuedBuffers));
451 const auto bufferCount = minUndequeuedBuffers + 2;
452
453 for (int i = 0; i < bufferCount; i++) {
Valerie Haud3b90d22019-11-06 09:37:31 -0800454 int slot;
455 sp<Fence> fence;
456 sp<GraphicBuffer> buf;
457 auto ret = igbProducer->dequeueBuffer(&slot, &fence, mDisplayWidth, mDisplayHeight,
458 PIXEL_FORMAT_RGBA_8888, GRALLOC_USAGE_SW_WRITE_OFTEN,
459 nullptr, nullptr);
460 ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, ret);
461 ASSERT_EQ(OK, igbProducer->requestBuffer(slot, &buf));
462 allocated.push_back({slot, fence});
463 }
464 for (int i = 0; i < allocated.size(); i++) {
465 igbProducer->cancelBuffer(allocated[i].first, allocated[i].second);
466 }
467
Valerie Haua32c5522019-12-09 10:11:08 -0800468 for (int i = 0; i < 100; 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(NO_ERROR, ret);
476 IGraphicBufferProducer::QueueBufferOutput qbOutput;
Vishnu Nair1506b182021-02-22 14:35:15 -0800477 IGraphicBufferProducer::QueueBufferInput input(systemTime(), true /* autotimestamp */,
478 HAL_DATASPACE_UNKNOWN,
Valerie Haud3b90d22019-11-06 09:37:31 -0800479 Rect(mDisplayWidth, mDisplayHeight),
480 NATIVE_WINDOW_SCALING_MODE_FREEZE, 0,
481 Fence::NO_FENCE);
482 igbProducer->queueBuffer(slot, input, &qbOutput);
483 }
484 adapter.waitForCallbacks();
485}
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800486
487TEST_F(BLASTBufferQueueTest, SetCrop_Item) {
488 uint8_t r = 255;
489 uint8_t g = 0;
490 uint8_t b = 0;
491
492 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
493 sp<IGraphicBufferProducer> igbProducer;
494 setUpProducer(adapter, igbProducer);
495 int slot;
496 sp<Fence> fence;
497 sp<GraphicBuffer> buf;
498 auto ret = igbProducer->dequeueBuffer(&slot, &fence, mDisplayWidth, mDisplayHeight,
499 PIXEL_FORMAT_RGBA_8888, GRALLOC_USAGE_SW_WRITE_OFTEN,
500 nullptr, nullptr);
501 ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, ret);
502 ASSERT_EQ(OK, igbProducer->requestBuffer(slot, &buf));
503
504 uint32_t* bufData;
505 buf->lock(static_cast<uint32_t>(GraphicBuffer::USAGE_SW_WRITE_OFTEN),
506 reinterpret_cast<void**>(&bufData));
507 fillBuffer(bufData, Rect(buf->getWidth(), buf->getHeight() / 2), buf->getStride(), r, g, b);
508 buf->unlock();
509
510 IGraphicBufferProducer::QueueBufferOutput qbOutput;
Vishnu Nair1506b182021-02-22 14:35:15 -0800511 IGraphicBufferProducer::QueueBufferInput input(systemTime(), true /* autotimestamp */,
512 HAL_DATASPACE_UNKNOWN,
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800513 Rect(mDisplayWidth, mDisplayHeight / 2),
514 NATIVE_WINDOW_SCALING_MODE_FREEZE, 0,
515 Fence::NO_FENCE);
516 igbProducer->queueBuffer(slot, input, &qbOutput);
Dominik Laskowski718f9602019-11-09 20:01:35 -0800517 ASSERT_NE(ui::Transform::ROT_INVALID, qbOutput.transformHint);
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800518
519 adapter.waitForCallbacks();
520 // capture screen and verify that it is red
chaviw8ffc7b82020-08-18 11:25:37 -0700521 ASSERT_EQ(NO_ERROR, captureDisplay(mCaptureArgs, mCaptureResults));
chaviwd2432892020-07-24 17:42:39 -0700522
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800523 ASSERT_NO_FATAL_FAILURE(
Chavi Weingartena5aedbd2021-04-09 13:37:33 +0000524 checkScreenCapture(r, g, b,
525 {0, 0, (int32_t)mDisplayWidth, (int32_t)mDisplayHeight / 2}));
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800526}
527
528TEST_F(BLASTBufferQueueTest, SetCrop_ScalingModeScaleCrop) {
529 uint8_t r = 255;
530 uint8_t g = 0;
531 uint8_t b = 0;
532
533 int32_t bufferSideLength =
534 (mDisplayWidth < mDisplayHeight) ? mDisplayWidth / 2 : mDisplayHeight / 2;
535 int32_t finalCropSideLength = bufferSideLength / 2;
536
537 auto bg = mClient->createSurface(String8("BGTest"), 0, 0, PIXEL_FORMAT_RGBA_8888,
Vishnu Nairfa247b12020-02-11 08:58:26 -0800538 ISurfaceComposerClient::eFXSurfaceEffect);
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800539 ASSERT_NE(nullptr, bg.get());
540 Transaction t;
Dominik Laskowski29fa1462021-04-27 15:51:50 -0700541 t.setLayerStack(bg, ui::DEFAULT_LAYER_STACK)
chaviw25714502021-02-11 10:01:08 -0800542 .setCrop(bg, Rect(0, 0, mDisplayWidth, mDisplayHeight))
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800543 .setColor(bg, half3{0, 0, 0})
544 .setLayer(bg, 0)
545 .apply();
546
547 BLASTBufferQueueHelper adapter(mSurfaceControl, bufferSideLength, bufferSideLength);
548 sp<IGraphicBufferProducer> igbProducer;
549 setUpProducer(adapter, igbProducer);
550 int slot;
551 sp<Fence> fence;
552 sp<GraphicBuffer> buf;
553 auto ret = igbProducer->dequeueBuffer(&slot, &fence, bufferSideLength, bufferSideLength,
554 PIXEL_FORMAT_RGBA_8888, GRALLOC_USAGE_SW_WRITE_OFTEN,
555 nullptr, nullptr);
556 ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, ret);
557 ASSERT_EQ(OK, igbProducer->requestBuffer(slot, &buf));
558
559 uint32_t* bufData;
560 buf->lock(static_cast<uint32_t>(GraphicBuffer::USAGE_SW_WRITE_OFTEN),
561 reinterpret_cast<void**>(&bufData));
562 fillBuffer(bufData, Rect(buf->getWidth(), buf->getHeight()), buf->getStride(), 0, 0, 0);
563 fillBuffer(bufData,
564 Rect(finalCropSideLength / 2, 0, buf->getWidth() - finalCropSideLength / 2,
565 buf->getHeight()),
566 buf->getStride(), r, g, b);
567 buf->unlock();
568
569 IGraphicBufferProducer::QueueBufferOutput qbOutput;
Vishnu Nair1506b182021-02-22 14:35:15 -0800570 IGraphicBufferProducer::QueueBufferInput input(systemTime(), true /* autotimestamp */,
571 HAL_DATASPACE_UNKNOWN,
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800572 Rect(bufferSideLength, finalCropSideLength),
573 NATIVE_WINDOW_SCALING_MODE_SCALE_CROP, 0,
574 Fence::NO_FENCE);
575 igbProducer->queueBuffer(slot, input, &qbOutput);
Dominik Laskowski718f9602019-11-09 20:01:35 -0800576 ASSERT_NE(ui::Transform::ROT_INVALID, qbOutput.transformHint);
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800577
578 adapter.waitForCallbacks();
579 // capture screen and verify that it is red
chaviw8ffc7b82020-08-18 11:25:37 -0700580 ASSERT_EQ(NO_ERROR, captureDisplay(mCaptureArgs, mCaptureResults));
Vishnu Nair5cc9ac02021-04-19 13:23:38 -0700581 ASSERT_NO_FATAL_FAILURE(checkScreenCapture(r, g, b,
582 {10, 10, (int32_t)bufferSideLength - 10,
583 (int32_t)bufferSideLength - 10}));
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800584 ASSERT_NO_FATAL_FAILURE(
Vishnu Nair5cc9ac02021-04-19 13:23:38 -0700585 checkScreenCapture(0, 0, 0,
586 {0, 0, (int32_t)bufferSideLength, (int32_t)bufferSideLength},
587 /*border*/ 0, /*outsideRegion*/ true));
588}
589
590TEST_F(BLASTBufferQueueTest, ScaleCroppedBufferToBufferSize) {
591 // add black background
592 auto bg = mClient->createSurface(String8("BGTest"), 0, 0, PIXEL_FORMAT_RGBA_8888,
593 ISurfaceComposerClient::eFXSurfaceEffect);
594 ASSERT_NE(nullptr, bg.get());
595 Transaction t;
Dominik Laskowski29fa1462021-04-27 15:51:50 -0700596 t.setLayerStack(bg, ui::DEFAULT_LAYER_STACK)
Vishnu Nair5cc9ac02021-04-19 13:23:38 -0700597 .setCrop(bg, Rect(0, 0, mDisplayWidth, mDisplayHeight))
598 .setColor(bg, half3{0, 0, 0})
599 .setLayer(bg, 0)
600 .apply();
601
602 Rect windowSize(1000, 1000);
603 Rect bufferSize(windowSize);
604 Rect bufferCrop(200, 200, 700, 700);
605
606 BLASTBufferQueueHelper adapter(mSurfaceControl, windowSize.getWidth(), windowSize.getHeight());
607 sp<IGraphicBufferProducer> igbProducer;
608 setUpProducer(adapter, igbProducer);
609 int slot;
610 sp<Fence> fence;
611 sp<GraphicBuffer> buf;
612 auto ret = igbProducer->dequeueBuffer(&slot, &fence, bufferSize.getWidth(),
613 bufferSize.getHeight(), PIXEL_FORMAT_RGBA_8888,
614 GRALLOC_USAGE_SW_WRITE_OFTEN, nullptr, nullptr);
615 ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, ret);
616 ASSERT_EQ(OK, igbProducer->requestBuffer(slot, &buf));
617
618 uint32_t* bufData;
619 buf->lock(static_cast<uint32_t>(GraphicBuffer::USAGE_SW_WRITE_OFTEN),
620 reinterpret_cast<void**>(&bufData));
621 // fill buffer with grey
622 fillBuffer(bufData, bufferSize, buf->getStride(), 127, 127, 127);
623
624 // fill crop area with different colors so we can verify the cropped region has been scaled
625 // correctly.
626 fillBuffer(bufData, Rect(200, 200, 450, 450), buf->getStride(), /* rgb */ 255, 0, 0);
627 fillBuffer(bufData, Rect(200, 451, 450, 700), buf->getStride(), /* rgb */ 0, 255, 0);
628 fillBuffer(bufData, Rect(451, 200, 700, 450), buf->getStride(), /* rgb */ 0, 0, 255);
629 fillBuffer(bufData, Rect(451, 451, 700, 700), buf->getStride(), /* rgb */ 255, 0, 0);
630 buf->unlock();
631
632 IGraphicBufferProducer::QueueBufferOutput qbOutput;
633 IGraphicBufferProducer::QueueBufferInput input(systemTime(), true /* autotimestamp */,
634 HAL_DATASPACE_UNKNOWN,
635 bufferCrop /* Rect::INVALID_RECT */,
636 NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW, 0,
637 Fence::NO_FENCE);
638 igbProducer->queueBuffer(slot, input, &qbOutput);
639 ASSERT_NE(ui::Transform::ROT_INVALID, qbOutput.transformHint);
640
641 adapter.waitForCallbacks();
642
643 ASSERT_EQ(NO_ERROR, captureDisplay(mCaptureArgs, mCaptureResults));
644
645 // Verify cropped region is scaled correctly.
646 ASSERT_NO_FATAL_FAILURE(checkScreenCapture(255, 0, 0, {10, 10, 490, 490}));
647 ASSERT_NO_FATAL_FAILURE(checkScreenCapture(0, 255, 0, {10, 510, 490, 990}));
648 ASSERT_NO_FATAL_FAILURE(checkScreenCapture(0, 0, 255, {510, 10, 990, 490}));
649 ASSERT_NO_FATAL_FAILURE(checkScreenCapture(255, 0, 0, {510, 510, 990, 990}));
650 // Verify outside region is black.
651 ASSERT_NO_FATAL_FAILURE(checkScreenCapture(0, 0, 0,
652 {0, 0, (int32_t)windowSize.getWidth(),
653 (int32_t)windowSize.getHeight()},
654 /*border*/ 0, /*outsideRegion*/ true));
655}
656
657TEST_F(BLASTBufferQueueTest, ScaleCroppedBufferToWindowSize) {
658 // add black background
659 auto bg = mClient->createSurface(String8("BGTest"), 0, 0, PIXEL_FORMAT_RGBA_8888,
660 ISurfaceComposerClient::eFXSurfaceEffect);
661 ASSERT_NE(nullptr, bg.get());
662 Transaction t;
Dominik Laskowski29fa1462021-04-27 15:51:50 -0700663 t.setLayerStack(bg, ui::DEFAULT_LAYER_STACK)
Vishnu Nair5cc9ac02021-04-19 13:23:38 -0700664 .setCrop(bg, Rect(0, 0, mDisplayWidth, mDisplayHeight))
665 .setColor(bg, half3{0, 0, 0})
666 .setLayer(bg, 0)
667 .apply();
668
669 Rect windowSize(1000, 1000);
670 Rect bufferSize(500, 500);
671 Rect bufferCrop(100, 100, 350, 350);
672
673 BLASTBufferQueueHelper adapter(mSurfaceControl, windowSize.getWidth(), windowSize.getHeight());
674 sp<IGraphicBufferProducer> igbProducer;
675 setUpProducer(adapter, igbProducer);
676 int slot;
677 sp<Fence> fence;
678 sp<GraphicBuffer> buf;
679 auto ret = igbProducer->dequeueBuffer(&slot, &fence, bufferSize.getWidth(),
680 bufferSize.getHeight(), PIXEL_FORMAT_RGBA_8888,
681 GRALLOC_USAGE_SW_WRITE_OFTEN, nullptr, nullptr);
682 ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, ret);
683 ASSERT_EQ(OK, igbProducer->requestBuffer(slot, &buf));
684
685 uint32_t* bufData;
686 buf->lock(static_cast<uint32_t>(GraphicBuffer::USAGE_SW_WRITE_OFTEN),
687 reinterpret_cast<void**>(&bufData));
688 // fill buffer with grey
689 fillBuffer(bufData, bufferSize, buf->getStride(), 127, 127, 127);
690
691 // fill crop area with different colors so we can verify the cropped region has been scaled
692 // correctly.
693 fillBuffer(bufData, Rect(100, 100, 225, 225), buf->getStride(), /* rgb */ 255, 0, 0);
694 fillBuffer(bufData, Rect(100, 226, 225, 350), buf->getStride(), /* rgb */ 0, 255, 0);
695 fillBuffer(bufData, Rect(226, 100, 350, 225), buf->getStride(), /* rgb */ 0, 0, 255);
696 fillBuffer(bufData, Rect(226, 226, 350, 350), buf->getStride(), /* rgb */ 255, 0, 0);
697 buf->unlock();
698
699 IGraphicBufferProducer::QueueBufferOutput qbOutput;
700 IGraphicBufferProducer::QueueBufferInput input(systemTime(), true /* autotimestamp */,
701 HAL_DATASPACE_UNKNOWN,
702 bufferCrop /* Rect::INVALID_RECT */,
703 NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW, 0,
704 Fence::NO_FENCE);
705 igbProducer->queueBuffer(slot, input, &qbOutput);
706 ASSERT_NE(ui::Transform::ROT_INVALID, qbOutput.transformHint);
707
708 adapter.waitForCallbacks();
709
710 ASSERT_EQ(NO_ERROR, captureDisplay(mCaptureArgs, mCaptureResults));
711 // Verify cropped region is scaled correctly.
712 ASSERT_NO_FATAL_FAILURE(checkScreenCapture(255, 0, 0, {10, 10, 490, 490}));
713 ASSERT_NO_FATAL_FAILURE(checkScreenCapture(0, 255, 0, {10, 510, 490, 990}));
714 ASSERT_NO_FATAL_FAILURE(checkScreenCapture(0, 0, 255, {510, 10, 990, 490}));
715 ASSERT_NO_FATAL_FAILURE(checkScreenCapture(255, 0, 0, {510, 510, 990, 990}));
716 // Verify outside region is black.
717 ASSERT_NO_FATAL_FAILURE(checkScreenCapture(0, 0, 0,
718 {0, 0, (int32_t)windowSize.getWidth(),
719 (int32_t)windowSize.getHeight()},
720 /*border*/ 0, /*outsideRegion*/ true));
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800721}
722
Vishnu Nair932f6ae2021-09-29 17:33:10 -0700723// b/196339769 verify we can can update the requested size while the in FREEZE scaling mode and
724// scale the buffer properly when the mode changes to SCALE_TO_WINDOW
725TEST_F(BLASTBufferQueueTest, ScalingModeChanges) {
726 uint8_t r = 255;
727 uint8_t g = 0;
728 uint8_t b = 0;
729
730 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight / 4);
731 sp<IGraphicBufferProducer> igbProducer;
732 setUpProducer(adapter, igbProducer);
733 {
734 int slot;
735 sp<Fence> fence;
736 sp<GraphicBuffer> buf;
737 auto ret = igbProducer->dequeueBuffer(&slot, &fence, mDisplayWidth, mDisplayHeight / 4,
738 PIXEL_FORMAT_RGBA_8888, GRALLOC_USAGE_SW_WRITE_OFTEN,
739 nullptr, nullptr);
740 ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, ret);
741 ASSERT_EQ(OK, igbProducer->requestBuffer(slot, &buf));
742
743 uint32_t* bufData;
744 buf->lock(static_cast<uint32_t>(GraphicBuffer::USAGE_SW_WRITE_OFTEN),
745 reinterpret_cast<void**>(&bufData));
746 fillBuffer(bufData, Rect(buf->getWidth(), buf->getHeight()), buf->getStride(), r, g, b);
747 buf->unlock();
748
749 IGraphicBufferProducer::QueueBufferOutput qbOutput;
750 IGraphicBufferProducer::QueueBufferInput input(systemTime(), true /* autotimestamp */,
751 HAL_DATASPACE_UNKNOWN, {},
752 NATIVE_WINDOW_SCALING_MODE_FREEZE, 0,
753 Fence::NO_FENCE);
754 igbProducer->queueBuffer(slot, input, &qbOutput);
755 adapter.waitForCallbacks();
756 }
757 // capture screen and verify that it is red
758 ASSERT_EQ(NO_ERROR, captureDisplay(mCaptureArgs, mCaptureResults));
759
760 ASSERT_NO_FATAL_FAILURE(
761 checkScreenCapture(r, g, b,
762 {0, 0, (int32_t)mDisplayWidth, (int32_t)mDisplayHeight / 4}));
763
764 // update the size to half the display and dequeue a buffer quarter of the display.
765 adapter.update(mSurfaceControl, mDisplayWidth, mDisplayHeight / 2);
766
767 {
768 int slot;
769 sp<Fence> fence;
770 sp<GraphicBuffer> buf;
771 auto ret = igbProducer->dequeueBuffer(&slot, &fence, mDisplayWidth, mDisplayHeight / 8,
772 PIXEL_FORMAT_RGBA_8888, GRALLOC_USAGE_SW_WRITE_OFTEN,
773 nullptr, nullptr);
774 ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, ret);
775 ASSERT_EQ(OK, igbProducer->requestBuffer(slot, &buf));
776
777 uint32_t* bufData;
778 buf->lock(static_cast<uint32_t>(GraphicBuffer::USAGE_SW_WRITE_OFTEN),
779 reinterpret_cast<void**>(&bufData));
780 g = 255;
781 fillBuffer(bufData, Rect(buf->getWidth(), buf->getHeight()), buf->getStride(), r, g, b);
782 buf->unlock();
783
784 IGraphicBufferProducer::QueueBufferOutput qbOutput;
785 IGraphicBufferProducer::QueueBufferInput input(systemTime(), true /* autotimestamp */,
786 HAL_DATASPACE_UNKNOWN, {},
787 NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW,
788 0, Fence::NO_FENCE);
789 igbProducer->queueBuffer(slot, input, &qbOutput);
790 adapter.waitForCallbacks();
791 }
792 // capture screen and verify that it is red
793 ASSERT_EQ(NO_ERROR, captureDisplay(mCaptureArgs, mCaptureResults));
794 // verify we still scale the buffer to the new size (half the screen height)
795 ASSERT_NO_FATAL_FAILURE(
796 checkScreenCapture(r, g, b,
797 {0, 0, (int32_t)mDisplayWidth, (int32_t)mDisplayHeight / 2}));
798}
799
chaviwd7deef72021-10-06 11:53:40 -0500800TEST_F(BLASTBufferQueueTest, SyncThenNoSync) {
801 uint8_t r = 255;
802 uint8_t g = 0;
803 uint8_t b = 0;
804
805 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
806
807 sp<IGraphicBufferProducer> igbProducer;
808 setUpProducer(adapter, igbProducer);
809
chaviwa1c4c822021-11-10 18:11:58 -0600810 Transaction sync;
811 adapter.setSyncTransaction(&sync);
chaviwd7deef72021-10-06 11:53:40 -0500812 queueBuffer(igbProducer, 0, 255, 0, 0);
813
814 // queue non sync buffer, so this one should get blocked
815 // Add a present delay to allow the first screenshot to get taken.
816 nsecs_t presentTimeDelay = std::chrono::nanoseconds(500ms).count();
817 queueBuffer(igbProducer, r, g, b, presentTimeDelay);
818
819 CallbackHelper transactionCallback;
chaviwa1c4c822021-11-10 18:11:58 -0600820 sync.addTransactionCompletedCallback(transactionCallback.function,
chaviwd7deef72021-10-06 11:53:40 -0500821 transactionCallback.getContext())
822 .apply();
823
824 CallbackData callbackData;
825 transactionCallback.getCallbackData(&callbackData);
826
chaviw0acd33a2021-11-02 11:55:37 -0500827 // capture screen and verify that it is green
chaviwd7deef72021-10-06 11:53:40 -0500828 ASSERT_EQ(NO_ERROR, captureDisplay(mCaptureArgs, mCaptureResults));
829 ASSERT_NO_FATAL_FAILURE(
830 checkScreenCapture(0, 255, 0, {0, 0, (int32_t)mDisplayWidth, (int32_t)mDisplayHeight}));
831
832 mProducerListener->waitOnNumberReleased(1);
833 ASSERT_EQ(NO_ERROR, captureDisplay(mCaptureArgs, mCaptureResults));
834 ASSERT_NO_FATAL_FAILURE(
835 checkScreenCapture(r, g, b, {0, 0, (int32_t)mDisplayWidth, (int32_t)mDisplayHeight}));
836}
837
838TEST_F(BLASTBufferQueueTest, MultipleSyncTransactions) {
839 uint8_t r = 255;
840 uint8_t g = 0;
841 uint8_t b = 0;
842
843 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
844
845 sp<IGraphicBufferProducer> igbProducer;
846 setUpProducer(adapter, igbProducer);
847
848 Transaction mainTransaction;
849
chaviwa1c4c822021-11-10 18:11:58 -0600850 Transaction sync;
851 adapter.setSyncTransaction(&sync);
chaviwd7deef72021-10-06 11:53:40 -0500852 queueBuffer(igbProducer, 0, 255, 0, 0);
853
chaviwa1c4c822021-11-10 18:11:58 -0600854 mainTransaction.merge(std::move(sync));
chaviwd7deef72021-10-06 11:53:40 -0500855
chaviwa1c4c822021-11-10 18:11:58 -0600856 adapter.setSyncTransaction(&sync);
chaviwd7deef72021-10-06 11:53:40 -0500857 queueBuffer(igbProducer, r, g, b, 0);
858
chaviwa1c4c822021-11-10 18:11:58 -0600859 mainTransaction.merge(std::move(sync));
chaviwd7deef72021-10-06 11:53:40 -0500860 // Expect 1 buffer to be released even before sending to SurfaceFlinger
861 mProducerListener->waitOnNumberReleased(1);
862
863 CallbackHelper transactionCallback;
864 mainTransaction
865 .addTransactionCompletedCallback(transactionCallback.function,
866 transactionCallback.getContext())
867 .apply();
868
869 CallbackData callbackData;
870 transactionCallback.getCallbackData(&callbackData);
871
872 // capture screen and verify that it is red
873 ASSERT_EQ(NO_ERROR, captureDisplay(mCaptureArgs, mCaptureResults));
874 ASSERT_NO_FATAL_FAILURE(
875 checkScreenCapture(r, g, b, {0, 0, (int32_t)mDisplayWidth, (int32_t)mDisplayHeight}));
876}
877
878TEST_F(BLASTBufferQueueTest, MultipleSyncTransactionWithNonSync) {
879 uint8_t r = 255;
880 uint8_t g = 0;
881 uint8_t b = 0;
882
883 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
884
885 sp<IGraphicBufferProducer> igbProducer;
886 setUpProducer(adapter, igbProducer);
887
888 Transaction mainTransaction;
889
chaviwa1c4c822021-11-10 18:11:58 -0600890 Transaction sync;
chaviwd7deef72021-10-06 11:53:40 -0500891 // queue a sync transaction
chaviwa1c4c822021-11-10 18:11:58 -0600892 adapter.setSyncTransaction(&sync);
chaviwd7deef72021-10-06 11:53:40 -0500893 queueBuffer(igbProducer, 0, 255, 0, 0);
894
chaviwa1c4c822021-11-10 18:11:58 -0600895 mainTransaction.merge(std::move(sync));
chaviwd7deef72021-10-06 11:53:40 -0500896
chaviwa1c4c822021-11-10 18:11:58 -0600897 // queue another buffer without setting sync transaction
chaviwd7deef72021-10-06 11:53:40 -0500898 queueBuffer(igbProducer, 0, 0, 255, 0);
899
900 // queue another sync transaction
chaviwa1c4c822021-11-10 18:11:58 -0600901 adapter.setSyncTransaction(&sync);
chaviwd7deef72021-10-06 11:53:40 -0500902 queueBuffer(igbProducer, r, g, b, 0);
903 // Expect 1 buffer to be released because the non sync transaction should merge
904 // with the sync
905 mProducerListener->waitOnNumberReleased(1);
906
chaviwa1c4c822021-11-10 18:11:58 -0600907 mainTransaction.merge(std::move(sync));
chaviwd7deef72021-10-06 11:53:40 -0500908 // Expect 2 buffers to be released due to merging the two syncs.
909 mProducerListener->waitOnNumberReleased(2);
910
911 CallbackHelper transactionCallback;
912 mainTransaction
913 .addTransactionCompletedCallback(transactionCallback.function,
914 transactionCallback.getContext())
915 .apply();
916
917 CallbackData callbackData;
918 transactionCallback.getCallbackData(&callbackData);
919
920 // capture screen and verify that it is red
921 ASSERT_EQ(NO_ERROR, captureDisplay(mCaptureArgs, mCaptureResults));
922 ASSERT_NO_FATAL_FAILURE(
923 checkScreenCapture(r, g, b, {0, 0, (int32_t)mDisplayWidth, (int32_t)mDisplayHeight}));
924}
925
926TEST_F(BLASTBufferQueueTest, MultipleSyncRunOutOfBuffers) {
927 uint8_t r = 255;
928 uint8_t g = 0;
929 uint8_t b = 0;
930
931 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
932
933 sp<IGraphicBufferProducer> igbProducer;
934 setUpProducer(adapter, igbProducer, 3);
935
936 Transaction mainTransaction;
937
chaviwa1c4c822021-11-10 18:11:58 -0600938 Transaction sync;
chaviwd7deef72021-10-06 11:53:40 -0500939 // queue a sync transaction
chaviwa1c4c822021-11-10 18:11:58 -0600940 adapter.setSyncTransaction(&sync);
chaviwd7deef72021-10-06 11:53:40 -0500941 queueBuffer(igbProducer, 0, 255, 0, 0);
942
chaviwa1c4c822021-11-10 18:11:58 -0600943 mainTransaction.merge(std::move(sync));
chaviwd7deef72021-10-06 11:53:40 -0500944
chaviwa1c4c822021-11-10 18:11:58 -0600945 // queue a few buffers without setting sync transaction
chaviwd7deef72021-10-06 11:53:40 -0500946 queueBuffer(igbProducer, 0, 0, 255, 0);
947 queueBuffer(igbProducer, 0, 0, 255, 0);
948 queueBuffer(igbProducer, 0, 0, 255, 0);
949
950 // queue another sync transaction
chaviwa1c4c822021-11-10 18:11:58 -0600951 adapter.setSyncTransaction(&sync);
chaviwd7deef72021-10-06 11:53:40 -0500952 queueBuffer(igbProducer, r, g, b, 0);
953 // Expect 3 buffers to be released because the non sync transactions should merge
954 // with the sync
955 mProducerListener->waitOnNumberReleased(3);
956
chaviwa1c4c822021-11-10 18:11:58 -0600957 mainTransaction.merge(std::move(sync));
chaviwd7deef72021-10-06 11:53:40 -0500958 // Expect 4 buffers to be released due to merging the two syncs.
959 mProducerListener->waitOnNumberReleased(4);
960
961 CallbackHelper transactionCallback;
962 mainTransaction
963 .addTransactionCompletedCallback(transactionCallback.function,
964 transactionCallback.getContext())
965 .apply();
966
967 CallbackData callbackData;
968 transactionCallback.getCallbackData(&callbackData);
969
970 // capture screen and verify that it is red
971 ASSERT_EQ(NO_ERROR, captureDisplay(mCaptureArgs, mCaptureResults));
972 ASSERT_NO_FATAL_FAILURE(
973 checkScreenCapture(r, g, b, {0, 0, (int32_t)mDisplayWidth, (int32_t)mDisplayHeight}));
974}
975
976// Tests BBQ with a sync transaction when the buffers acquired reaches max and the only way to
977// continue processing is for a release callback from SurfaceFlinger.
978// This is done by sending a buffer to SF so it can release the previous one and allow BBQ to
979// continue acquiring buffers.
980TEST_F(BLASTBufferQueueTest, RunOutOfBuffersWaitingOnSF) {
981 uint8_t r = 255;
982 uint8_t g = 0;
983 uint8_t b = 0;
984
985 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
986
987 sp<IGraphicBufferProducer> igbProducer;
988 setUpProducer(adapter, igbProducer, 4);
989
990 Transaction mainTransaction;
991
992 // Send a buffer to SF
993 queueBuffer(igbProducer, 0, 255, 0, 0);
994
chaviwa1c4c822021-11-10 18:11:58 -0600995 Transaction sync;
chaviwd7deef72021-10-06 11:53:40 -0500996 // queue a sync transaction
chaviwa1c4c822021-11-10 18:11:58 -0600997 adapter.setSyncTransaction(&sync);
chaviwd7deef72021-10-06 11:53:40 -0500998 queueBuffer(igbProducer, 0, 255, 0, 0);
999
chaviwa1c4c822021-11-10 18:11:58 -06001000 mainTransaction.merge(std::move(sync));
chaviwd7deef72021-10-06 11:53:40 -05001001
chaviwa1c4c822021-11-10 18:11:58 -06001002 // queue a few buffers without setting sync transaction
chaviwd7deef72021-10-06 11:53:40 -05001003 queueBuffer(igbProducer, 0, 0, 255, 0);
1004 queueBuffer(igbProducer, 0, 0, 255, 0);
1005 queueBuffer(igbProducer, 0, 0, 255, 0);
1006
1007 // apply the first synced buffer to ensure we have to wait on SF
1008 mainTransaction.apply();
1009
1010 // queue another sync transaction
chaviwa1c4c822021-11-10 18:11:58 -06001011 adapter.setSyncTransaction(&sync);
chaviwd7deef72021-10-06 11:53:40 -05001012 queueBuffer(igbProducer, r, g, b, 0);
1013 // Expect 2 buffers to be released because the non sync transactions should merge
1014 // with the sync
1015 mProducerListener->waitOnNumberReleased(3);
1016
chaviwa1c4c822021-11-10 18:11:58 -06001017 mainTransaction.merge(std::move(sync));
chaviwd7deef72021-10-06 11:53:40 -05001018
1019 CallbackHelper transactionCallback;
1020 mainTransaction
1021 .addTransactionCompletedCallback(transactionCallback.function,
1022 transactionCallback.getContext())
1023 .apply();
1024
1025 CallbackData callbackData;
1026 transactionCallback.getCallbackData(&callbackData);
1027
1028 // capture screen and verify that it is red
1029 ASSERT_EQ(NO_ERROR, captureDisplay(mCaptureArgs, mCaptureResults));
1030 ASSERT_NO_FATAL_FAILURE(
1031 checkScreenCapture(r, g, b, {0, 0, (int32_t)mDisplayWidth, (int32_t)mDisplayHeight}));
1032}
1033
chaviw0acd33a2021-11-02 11:55:37 -05001034TEST_F(BLASTBufferQueueTest, SetSyncTransactionAcquireMultipleBuffers) {
1035 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
1036
1037 sp<IGraphicBufferProducer> igbProducer;
1038 setUpProducer(adapter, igbProducer);
1039
1040 Transaction next;
1041 adapter.setSyncTransaction(&next, false);
1042 queueBuffer(igbProducer, 0, 255, 0, 0);
1043 queueBuffer(igbProducer, 0, 0, 255, 0);
1044 // There should only be one frame submitted since the first frame will be released.
1045 adapter.validateNumFramesSubmitted(1);
1046 adapter.setSyncTransaction(nullptr);
1047
1048 // queue non sync buffer, so this one should get blocked
1049 // Add a present delay to allow the first screenshot to get taken.
1050 nsecs_t presentTimeDelay = std::chrono::nanoseconds(500ms).count();
1051 queueBuffer(igbProducer, 255, 0, 0, presentTimeDelay);
1052
1053 CallbackHelper transactionCallback;
1054 next.addTransactionCompletedCallback(transactionCallback.function,
1055 transactionCallback.getContext())
1056 .apply();
1057
1058 CallbackData callbackData;
1059 transactionCallback.getCallbackData(&callbackData);
1060
1061 // capture screen and verify that it is blue
1062 ASSERT_EQ(NO_ERROR, captureDisplay(mCaptureArgs, mCaptureResults));
1063 ASSERT_NO_FATAL_FAILURE(
1064 checkScreenCapture(0, 0, 255, {0, 0, (int32_t)mDisplayWidth, (int32_t)mDisplayHeight}));
1065
1066 mProducerListener->waitOnNumberReleased(2);
1067 // capture screen and verify that it is red
1068 ASSERT_EQ(NO_ERROR, captureDisplay(mCaptureArgs, mCaptureResults));
1069 ASSERT_NO_FATAL_FAILURE(
1070 checkScreenCapture(255, 0, 0, {0, 0, (int32_t)mDisplayWidth, (int32_t)mDisplayHeight}));
1071}
1072
Vishnu Nair1e8bf102021-12-28 14:36:59 -08001073// This test will currently fail because the old surfacecontrol will steal the last presented buffer
1074// until the old surface control is destroyed. This is not necessarily a bug but to document a
1075// limitation with the update API and to test any changes to make the api more robust. The current
1076// approach for the client is to recreate the blastbufferqueue when the surfacecontrol updates.
1077TEST_F(BLASTBufferQueueTest, DISABLED_DisconnectProducerTest) {
1078 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
1079 std::vector<sp<SurfaceControl>> surfaceControls;
1080 sp<IGraphicBufferProducer> igbProducer;
1081 for (int i = 0; i < 10; i++) {
1082 sp<SurfaceControl> sc =
1083 mClient->createSurface(String8("TestSurface"), mDisplayWidth, mDisplayHeight,
1084 PIXEL_FORMAT_RGBA_8888,
1085 ISurfaceComposerClient::eFXSurfaceBufferState,
1086 /*parent*/ nullptr);
1087 Transaction()
1088 .setLayerStack(mSurfaceControl, ui::DEFAULT_LAYER_STACK)
1089 .setLayer(mSurfaceControl, std::numeric_limits<int32_t>::max())
1090 .show(mSurfaceControl)
1091 .setDataspace(mSurfaceControl, ui::Dataspace::V0_SRGB)
1092 .apply(true);
1093 surfaceControls.push_back(sc);
1094 adapter.update(sc, mDisplayWidth, mDisplayHeight);
1095
1096 setUpProducer(adapter, igbProducer);
1097 Transaction next;
1098 queueBuffer(igbProducer, 0, 255, 0, 0);
1099 queueBuffer(igbProducer, 0, 0, 255, 0);
1100 adapter.setSyncTransaction(&next, false);
1101 queueBuffer(igbProducer, 255, 0, 0, 0);
1102
1103 CallbackHelper transactionCallback;
1104 next.addTransactionCompletedCallback(transactionCallback.function,
1105 transactionCallback.getContext())
1106 .apply();
1107
1108 CallbackData callbackData;
1109 transactionCallback.getCallbackData(&callbackData);
1110 // capture screen and verify that it is red
1111 ASSERT_EQ(NO_ERROR, captureDisplay(mCaptureArgs, mCaptureResults));
1112 ASSERT_NO_FATAL_FAILURE(
1113 checkScreenCapture(255, 0, 0,
1114 {0, 0, (int32_t)mDisplayWidth, (int32_t)mDisplayHeight}));
1115 igbProducer->disconnect(NATIVE_WINDOW_API_CPU);
1116 }
1117}
1118
1119// See DISABLED_DisconnectProducerTest
1120TEST_F(BLASTBufferQueueTest, DISABLED_UpdateSurfaceControlTest) {
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 setUpProducer(adapter, igbProducer);
1139
1140 Transaction next;
1141 queueBuffer(igbProducer, 0, 255, 0, 0);
1142 queueBuffer(igbProducer, 0, 0, 255, 0);
1143 adapter.setSyncTransaction(&next, false);
1144 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 }
1159}
1160
Vishnu Nair89496122020-12-14 17:14:53 -08001161class TestProducerListener : public BnProducerListener {
1162public:
1163 sp<IGraphicBufferProducer> mIgbp;
1164 TestProducerListener(const sp<IGraphicBufferProducer>& igbp) : mIgbp(igbp) {}
1165 void onBufferReleased() override {
1166 sp<GraphicBuffer> buffer;
1167 sp<Fence> fence;
1168 mIgbp->detachNextBuffer(&buffer, &fence);
1169 }
1170};
1171
1172TEST_F(BLASTBufferQueueTest, CustomProducerListener) {
1173 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
1174 sp<IGraphicBufferProducer> igbProducer = adapter.getIGraphicBufferProducer();
1175 ASSERT_NE(nullptr, igbProducer.get());
1176 ASSERT_EQ(NO_ERROR, igbProducer->setMaxDequeuedBufferCount(2));
1177 IGraphicBufferProducer::QueueBufferOutput qbOutput;
1178 ASSERT_EQ(NO_ERROR,
1179 igbProducer->connect(new TestProducerListener(igbProducer), NATIVE_WINDOW_API_CPU,
1180 false, &qbOutput));
1181 ASSERT_NE(ui::Transform::ROT_INVALID, qbOutput.transformHint);
1182 for (int i = 0; i < 3; i++) {
1183 int slot;
1184 sp<Fence> fence;
1185 sp<GraphicBuffer> buf;
1186 auto ret = igbProducer->dequeueBuffer(&slot, &fence, mDisplayWidth, mDisplayHeight,
1187 PIXEL_FORMAT_RGBA_8888, GRALLOC_USAGE_SW_WRITE_OFTEN,
1188 nullptr, nullptr);
1189 ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, ret);
1190 ASSERT_EQ(OK, igbProducer->requestBuffer(slot, &buf));
1191 IGraphicBufferProducer::QueueBufferOutput qbOutput;
Vishnu Nair1506b182021-02-22 14:35:15 -08001192 IGraphicBufferProducer::QueueBufferInput input(systemTime(), true /* autotimestamp */,
1193 HAL_DATASPACE_UNKNOWN,
Vishnu Nair89496122020-12-14 17:14:53 -08001194 Rect(mDisplayWidth, mDisplayHeight),
1195 NATIVE_WINDOW_SCALING_MODE_FREEZE, 0,
1196 Fence::NO_FENCE);
1197 igbProducer->queueBuffer(slot, input, &qbOutput);
1198 }
1199 adapter.waitForCallbacks();
1200}
1201
Vishnu Nair17dde612020-12-28 11:39:59 -08001202TEST_F(BLASTBufferQueueTest, QueryNativeWindowQueuesToWindowComposer) {
1203 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
1204
1205 sp<android::Surface> surface = new Surface(adapter.getIGraphicBufferProducer());
1206 ANativeWindow* nativeWindow = (ANativeWindow*)(surface.get());
1207 int queuesToNativeWindow = 0;
1208 int err = nativeWindow->query(nativeWindow, NATIVE_WINDOW_QUEUES_TO_WINDOW_COMPOSER,
1209 &queuesToNativeWindow);
1210 ASSERT_EQ(NO_ERROR, err);
1211 ASSERT_EQ(queuesToNativeWindow, 1);
1212}
1213
Vishnu Nair083efd32021-02-12 09:32:30 -08001214// Test a slow producer doesn't hold up a faster producer from the same client. Essentially tests
1215// BBQ uses separate transaction queues.
Vishnu Nair277142c2021-01-05 18:35:29 -08001216TEST_F(BLASTBufferQueueTest, OutOfOrderTransactionTest) {
1217 sp<SurfaceControl> bgSurface =
1218 mClient->createSurface(String8("BGTest"), 0, 0, PIXEL_FORMAT_RGBA_8888,
1219 ISurfaceComposerClient::eFXSurfaceBufferState);
1220 ASSERT_NE(nullptr, bgSurface.get());
1221 Transaction t;
Dominik Laskowski29fa1462021-04-27 15:51:50 -07001222 t.setLayerStack(bgSurface, ui::DEFAULT_LAYER_STACK)
Vishnu Nair277142c2021-01-05 18:35:29 -08001223 .show(bgSurface)
1224 .setDataspace(bgSurface, ui::Dataspace::V0_SRGB)
Vishnu Nair277142c2021-01-05 18:35:29 -08001225 .setLayer(bgSurface, std::numeric_limits<int32_t>::max() - 1)
1226 .apply();
1227
1228 BLASTBufferQueueHelper slowAdapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
1229 sp<IGraphicBufferProducer> slowIgbProducer;
1230 setUpProducer(slowAdapter, slowIgbProducer);
1231 nsecs_t presentTimeDelay = std::chrono::nanoseconds(500ms).count();
Vishnu Nair1506b182021-02-22 14:35:15 -08001232 queueBuffer(slowIgbProducer, 0 /* r */, 255 /* g */, 0 /* b */, presentTimeDelay);
Vishnu Nair277142c2021-01-05 18:35:29 -08001233
1234 BLASTBufferQueueHelper fastAdapter(bgSurface, mDisplayWidth, mDisplayHeight);
1235 sp<IGraphicBufferProducer> fastIgbProducer;
1236 setUpProducer(fastAdapter, fastIgbProducer);
1237 uint8_t r = 255;
1238 uint8_t g = 0;
1239 uint8_t b = 0;
1240 queueBuffer(fastIgbProducer, r, g, b, 0 /* presentTimeDelay */);
1241 fastAdapter.waitForCallbacks();
1242
1243 // capture screen and verify that it is red
1244 ASSERT_EQ(NO_ERROR, captureDisplay(mCaptureArgs, mCaptureResults));
1245
1246 ASSERT_NO_FATAL_FAILURE(
Chavi Weingartena5aedbd2021-04-09 13:37:33 +00001247 checkScreenCapture(r, g, b,
1248 {0, 0, (int32_t)mDisplayWidth, (int32_t)mDisplayHeight / 2}));
Vishnu Nair277142c2021-01-05 18:35:29 -08001249}
1250
Vishnu Naira4fbca52021-07-07 16:52:34 -07001251TEST_F(BLASTBufferQueueTest, TransformHint) {
1252 // Transform hint is provided to BBQ via the surface control passed by WM
1253 mSurfaceControl->setTransformHint(ui::Transform::ROT_90);
1254
1255 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
1256 sp<IGraphicBufferProducer> igbProducer = adapter.getIGraphicBufferProducer();
1257 ASSERT_NE(nullptr, igbProducer.get());
1258 ASSERT_EQ(NO_ERROR, igbProducer->setMaxDequeuedBufferCount(2));
1259 sp<Surface> surface = adapter.getSurface();
1260
1261 // Before connecting to the surface, we do not get a valid transform hint
1262 int transformHint;
1263 surface->query(NATIVE_WINDOW_TRANSFORM_HINT, &transformHint);
1264 ASSERT_EQ(ui::Transform::ROT_0, transformHint);
1265
1266 ASSERT_EQ(NO_ERROR,
1267 surface->connect(NATIVE_WINDOW_API_CPU, new TestProducerListener(igbProducer)));
1268
1269 // After connecting to the surface, we should get the correct hint.
1270 surface->query(NATIVE_WINDOW_TRANSFORM_HINT, &transformHint);
1271 ASSERT_EQ(ui::Transform::ROT_90, transformHint);
1272
1273 ANativeWindow_Buffer buffer;
1274 surface->lock(&buffer, nullptr /* inOutDirtyBounds */);
1275
1276 // Transform hint is updated via callbacks or surface control updates
1277 mSurfaceControl->setTransformHint(ui::Transform::ROT_0);
1278 adapter.update(mSurfaceControl, mDisplayWidth, mDisplayHeight);
1279
1280 // The hint does not change and matches the value used when dequeueing the buffer.
1281 surface->query(NATIVE_WINDOW_TRANSFORM_HINT, &transformHint);
1282 ASSERT_EQ(ui::Transform::ROT_90, transformHint);
1283
1284 surface->unlockAndPost();
1285
1286 // After queuing the buffer, we get the updated transform hint
1287 surface->query(NATIVE_WINDOW_TRANSFORM_HINT, &transformHint);
1288 ASSERT_EQ(ui::Transform::ROT_0, transformHint);
1289
1290 adapter.waitForCallbacks();
1291}
1292
Valerie Hau5977fc82019-12-05 15:56:39 -08001293class BLASTBufferQueueTransformTest : public BLASTBufferQueueTest {
1294public:
1295 void test(uint32_t tr) {
1296 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
1297 sp<IGraphicBufferProducer> igbProducer;
1298 setUpProducer(adapter, igbProducer);
1299
1300 auto bufWidth = mDisplayWidth;
1301 auto bufHeight = mDisplayHeight;
1302 int slot;
1303 sp<Fence> fence;
1304 sp<GraphicBuffer> buf;
1305
1306 auto ret = igbProducer->dequeueBuffer(&slot, &fence, bufWidth, bufHeight,
1307 PIXEL_FORMAT_RGBA_8888, GRALLOC_USAGE_SW_WRITE_OFTEN,
1308 nullptr, nullptr);
1309 ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, ret);
1310 ASSERT_EQ(OK, igbProducer->requestBuffer(slot, &buf));
1311
1312 fillQuadrants(buf);
1313
1314 IGraphicBufferProducer::QueueBufferOutput qbOutput;
Vishnu Nair1506b182021-02-22 14:35:15 -08001315 IGraphicBufferProducer::QueueBufferInput input(systemTime(), true /* autotimestamp */,
1316 HAL_DATASPACE_UNKNOWN,
Valerie Hau5977fc82019-12-05 15:56:39 -08001317 Rect(bufWidth, bufHeight),
Vishnu Naire1a42322020-10-02 17:42:04 -07001318 NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW,
1319 tr, Fence::NO_FENCE);
Valerie Hau5977fc82019-12-05 15:56:39 -08001320 igbProducer->queueBuffer(slot, input, &qbOutput);
Dominik Laskowski718f9602019-11-09 20:01:35 -08001321 ASSERT_NE(ui::Transform::ROT_INVALID, qbOutput.transformHint);
Valerie Hau5977fc82019-12-05 15:56:39 -08001322
1323 adapter.waitForCallbacks();
chaviw8ffc7b82020-08-18 11:25:37 -07001324 ASSERT_EQ(NO_ERROR, captureDisplay(mCaptureArgs, mCaptureResults));
chaviwd2432892020-07-24 17:42:39 -07001325
Valerie Hau5977fc82019-12-05 15:56:39 -08001326 switch (tr) {
1327 case ui::Transform::ROT_0:
1328 ASSERT_NO_FATAL_FAILURE(checkScreenCapture(0, 0, 0,
1329 {0, 0, (int32_t)mDisplayWidth / 2,
1330 (int32_t)mDisplayHeight / 2},
1331 1));
1332 ASSERT_NO_FATAL_FAILURE(
1333 checkScreenCapture(255, 0, 0,
1334 {(int32_t)mDisplayWidth / 2, 0, (int32_t)mDisplayWidth,
1335 (int32_t)mDisplayHeight / 2},
1336 1));
1337 ASSERT_NO_FATAL_FAILURE(
1338 checkScreenCapture(0, 255, 0,
1339 {(int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight / 2,
1340 (int32_t)mDisplayWidth, (int32_t)mDisplayHeight},
1341 1));
1342 ASSERT_NO_FATAL_FAILURE(
1343 checkScreenCapture(0, 0, 255,
1344 {0, (int32_t)mDisplayHeight / 2,
1345 (int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight},
1346 1));
1347 break;
1348 case ui::Transform::FLIP_H:
1349 ASSERT_NO_FATAL_FAILURE(checkScreenCapture(255, 0, 0,
1350 {0, 0, (int32_t)mDisplayWidth / 2,
1351 (int32_t)mDisplayHeight / 2},
1352 1));
1353 ASSERT_NO_FATAL_FAILURE(
1354 checkScreenCapture(0, 0, 0,
1355 {(int32_t)mDisplayWidth / 2, 0, (int32_t)mDisplayWidth,
1356 (int32_t)mDisplayHeight / 2},
1357 1));
1358 ASSERT_NO_FATAL_FAILURE(
1359 checkScreenCapture(0, 0, 255,
1360 {(int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight / 2,
1361 (int32_t)mDisplayWidth, (int32_t)mDisplayHeight},
1362 1));
1363 ASSERT_NO_FATAL_FAILURE(
1364 checkScreenCapture(0, 255, 0,
1365 {0, (int32_t)mDisplayHeight / 2,
1366 (int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight},
1367 1));
1368 break;
1369 case ui::Transform::FLIP_V:
1370 ASSERT_NO_FATAL_FAILURE(checkScreenCapture(0, 0, 255,
1371 {0, 0, (int32_t)mDisplayWidth / 2,
1372 (int32_t)mDisplayHeight / 2},
1373 1));
1374 ASSERT_NO_FATAL_FAILURE(
1375 checkScreenCapture(0, 255, 0,
1376 {(int32_t)mDisplayWidth / 2, 0, (int32_t)mDisplayWidth,
1377 (int32_t)mDisplayHeight / 2},
1378 1));
1379 ASSERT_NO_FATAL_FAILURE(
1380 checkScreenCapture(255, 0, 0,
1381 {(int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight / 2,
1382 (int32_t)mDisplayWidth, (int32_t)mDisplayHeight},
1383 1));
1384 ASSERT_NO_FATAL_FAILURE(
1385 checkScreenCapture(0, 0, 0,
1386 {0, (int32_t)mDisplayHeight / 2,
1387 (int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight},
1388 1));
1389 break;
1390 case ui::Transform::ROT_90:
1391 ASSERT_NO_FATAL_FAILURE(checkScreenCapture(0, 0, 255,
1392 {0, 0, (int32_t)mDisplayWidth / 2,
1393 (int32_t)mDisplayHeight / 2},
1394 1));
1395 ASSERT_NO_FATAL_FAILURE(
1396 checkScreenCapture(0, 0, 0,
1397 {(int32_t)mDisplayWidth / 2, 0, (int32_t)mDisplayWidth,
1398 (int32_t)mDisplayHeight / 2},
1399 1));
1400 ASSERT_NO_FATAL_FAILURE(
1401 checkScreenCapture(255, 0, 0,
1402 {(int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight / 2,
1403 (int32_t)mDisplayWidth, (int32_t)mDisplayHeight},
1404 1));
1405 ASSERT_NO_FATAL_FAILURE(
1406 checkScreenCapture(0, 255, 0,
1407 {0, (int32_t)mDisplayHeight / 2,
1408 (int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight},
1409 1));
1410 break;
1411 case ui::Transform::ROT_180:
1412 ASSERT_NO_FATAL_FAILURE(checkScreenCapture(0, 255, 0,
1413 {0, 0, (int32_t)mDisplayWidth / 2,
1414 (int32_t)mDisplayHeight / 2},
1415 1));
1416 ASSERT_NO_FATAL_FAILURE(
1417 checkScreenCapture(0, 0, 255,
1418 {(int32_t)mDisplayWidth / 2, 0, (int32_t)mDisplayWidth,
1419 (int32_t)mDisplayHeight / 2},
1420 1));
1421 ASSERT_NO_FATAL_FAILURE(
1422 checkScreenCapture(0, 0, 0,
1423 {(int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight / 2,
1424 (int32_t)mDisplayWidth, (int32_t)mDisplayHeight},
1425 1));
1426 ASSERT_NO_FATAL_FAILURE(
1427 checkScreenCapture(255, 0, 0,
1428 {0, (int32_t)mDisplayHeight / 2,
1429 (int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight},
1430 1));
1431 break;
1432 case ui::Transform::ROT_270:
1433 ASSERT_NO_FATAL_FAILURE(checkScreenCapture(255, 0, 0,
1434 {0, 0, (int32_t)mDisplayWidth / 2,
1435 (int32_t)mDisplayHeight / 2},
1436 1));
1437 ASSERT_NO_FATAL_FAILURE(
1438 checkScreenCapture(0, 255, 0,
1439 {(int32_t)mDisplayWidth / 2, 0, (int32_t)mDisplayWidth,
1440 (int32_t)mDisplayHeight / 2},
1441 1));
1442 ASSERT_NO_FATAL_FAILURE(
1443 checkScreenCapture(0, 0, 255,
1444 {(int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight / 2,
1445 (int32_t)mDisplayWidth, (int32_t)mDisplayHeight},
1446 1));
1447 ASSERT_NO_FATAL_FAILURE(
1448 checkScreenCapture(0, 0, 0,
1449 {0, (int32_t)mDisplayHeight / 2,
1450 (int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight},
1451 1));
1452 }
1453 }
1454};
1455
1456TEST_F(BLASTBufferQueueTransformTest, setTransform_ROT_0) {
1457 test(ui::Transform::ROT_0);
1458}
1459
1460TEST_F(BLASTBufferQueueTransformTest, setTransform_FLIP_H) {
1461 test(ui::Transform::FLIP_H);
1462}
1463
1464TEST_F(BLASTBufferQueueTransformTest, setTransform_FLIP_V) {
1465 test(ui::Transform::FLIP_V);
1466}
1467
1468TEST_F(BLASTBufferQueueTransformTest, setTransform_ROT_90) {
1469 test(ui::Transform::ROT_90);
1470}
1471
1472TEST_F(BLASTBufferQueueTransformTest, setTransform_ROT_180) {
1473 test(ui::Transform::ROT_180);
1474}
1475
1476TEST_F(BLASTBufferQueueTransformTest, setTransform_ROT_270) {
1477 test(ui::Transform::ROT_270);
1478}
Valerie Hau871d6352020-01-29 08:44:02 -08001479
1480class BLASTFrameEventHistoryTest : public BLASTBufferQueueTest {
1481public:
1482 void setUpAndQueueBuffer(const sp<IGraphicBufferProducer>& igbProducer,
Vishnu Nairde66dc72021-06-17 17:54:41 -07001483 nsecs_t* outRequestedPresentTime, nsecs_t* postedTime,
Valerie Hau871d6352020-01-29 08:44:02 -08001484 IGraphicBufferProducer::QueueBufferOutput* qbOutput,
Vishnu Nairde66dc72021-06-17 17:54:41 -07001485 bool getFrameTimestamps, nsecs_t requestedPresentTime = systemTime()) {
Valerie Hau871d6352020-01-29 08:44:02 -08001486 int slot;
1487 sp<Fence> fence;
1488 sp<GraphicBuffer> buf;
1489 auto ret = igbProducer->dequeueBuffer(&slot, &fence, mDisplayWidth, mDisplayHeight,
1490 PIXEL_FORMAT_RGBA_8888, GRALLOC_USAGE_SW_WRITE_OFTEN,
1491 nullptr, nullptr);
Vishnu Nairde66dc72021-06-17 17:54:41 -07001492 if (IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION == ret) {
1493 ASSERT_EQ(OK, igbProducer->requestBuffer(slot, &buf));
1494 }
Valerie Hau871d6352020-01-29 08:44:02 -08001495
Vishnu Nairde66dc72021-06-17 17:54:41 -07001496 *outRequestedPresentTime = requestedPresentTime;
1497 IGraphicBufferProducer::QueueBufferInput input(requestedPresentTime, false,
1498 HAL_DATASPACE_UNKNOWN,
Valerie Hau871d6352020-01-29 08:44:02 -08001499 Rect(mDisplayWidth, mDisplayHeight),
1500 NATIVE_WINDOW_SCALING_MODE_FREEZE, 0,
1501 Fence::NO_FENCE, /*sticky*/ 0,
1502 getFrameTimestamps);
1503 if (postedTime) *postedTime = systemTime();
1504 igbProducer->queueBuffer(slot, input, qbOutput);
1505 }
Vishnu Nair083efd32021-02-12 09:32:30 -08001506 sp<SurfaceControl> mBufferQueueSurfaceControl;
Valerie Hau871d6352020-01-29 08:44:02 -08001507};
1508
1509TEST_F(BLASTFrameEventHistoryTest, FrameEventHistory_Basic) {
1510 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
1511 sp<IGraphicBufferProducer> igbProducer;
1512 ProducerFrameEventHistory history;
1513 setUpProducer(adapter, igbProducer);
1514
1515 IGraphicBufferProducer::QueueBufferOutput qbOutput;
1516 nsecs_t requestedPresentTimeA = 0;
1517 nsecs_t postedTimeA = 0;
1518 setUpAndQueueBuffer(igbProducer, &requestedPresentTimeA, &postedTimeA, &qbOutput, true);
1519 history.applyDelta(qbOutput.frameTimestamps);
1520
1521 FrameEvents* events = nullptr;
1522 events = history.getFrame(1);
1523 ASSERT_NE(nullptr, events);
1524 ASSERT_EQ(1, events->frameNumber);
1525 ASSERT_EQ(requestedPresentTimeA, events->requestedPresentTime);
1526 ASSERT_GE(events->postedTime, postedTimeA);
1527
Vishnu Nair1506b182021-02-22 14:35:15 -08001528 adapter.waitForCallback(1);
Valerie Hau871d6352020-01-29 08:44:02 -08001529
1530 // queue another buffer so we query for frame event deltas
1531 nsecs_t requestedPresentTimeB = 0;
1532 nsecs_t postedTimeB = 0;
1533 setUpAndQueueBuffer(igbProducer, &requestedPresentTimeB, &postedTimeB, &qbOutput, true);
1534 history.applyDelta(qbOutput.frameTimestamps);
1535 events = history.getFrame(1);
1536 ASSERT_NE(nullptr, events);
1537
1538 // frame number, requestedPresentTime, and postTime should not have changed
1539 ASSERT_EQ(1, events->frameNumber);
1540 ASSERT_EQ(requestedPresentTimeA, events->requestedPresentTime);
1541 ASSERT_GE(events->postedTime, postedTimeA);
1542
1543 ASSERT_GE(events->latchTime, postedTimeA);
1544 ASSERT_GE(events->dequeueReadyTime, events->latchTime);
1545 ASSERT_NE(nullptr, events->gpuCompositionDoneFence);
1546 ASSERT_NE(nullptr, events->displayPresentFence);
1547 ASSERT_NE(nullptr, events->releaseFence);
1548
1549 // we should also have gotten the initial values for the next frame
1550 events = history.getFrame(2);
1551 ASSERT_NE(nullptr, events);
1552 ASSERT_EQ(2, events->frameNumber);
1553 ASSERT_EQ(requestedPresentTimeB, events->requestedPresentTime);
1554 ASSERT_GE(events->postedTime, postedTimeB);
Valerie Hau78491e92020-04-15 13:10:56 -07001555
1556 // wait for any callbacks that have not been received
1557 adapter.waitForCallbacks();
Valerie Hau871d6352020-01-29 08:44:02 -08001558}
Vishnu Nair083efd32021-02-12 09:32:30 -08001559
Vishnu Nair083efd32021-02-12 09:32:30 -08001560TEST_F(BLASTFrameEventHistoryTest, FrameEventHistory_DroppedFrame) {
1561 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
1562 sp<IGraphicBufferProducer> igbProducer;
1563 setUpProducer(adapter, igbProducer);
1564
1565 ProducerFrameEventHistory history;
1566 IGraphicBufferProducer::QueueBufferOutput qbOutput;
1567 nsecs_t requestedPresentTimeA = 0;
1568 nsecs_t postedTimeA = 0;
Vishnu Nairde66dc72021-06-17 17:54:41 -07001569 // Present the frame sometime in the future so we can add two frames to the queue so the older
1570 // one will be dropped.
1571 nsecs_t presentTime = systemTime() + std::chrono::nanoseconds(500ms).count();
Vishnu Nair083efd32021-02-12 09:32:30 -08001572 setUpAndQueueBuffer(igbProducer, &requestedPresentTimeA, &postedTimeA, &qbOutput, true,
Vishnu Nairde66dc72021-06-17 17:54:41 -07001573 presentTime);
Vishnu Nair083efd32021-02-12 09:32:30 -08001574 history.applyDelta(qbOutput.frameTimestamps);
1575
1576 FrameEvents* events = nullptr;
1577 events = history.getFrame(1);
1578 ASSERT_NE(nullptr, events);
1579 ASSERT_EQ(1, events->frameNumber);
1580 ASSERT_EQ(requestedPresentTimeA, events->requestedPresentTime);
1581 ASSERT_GE(events->postedTime, postedTimeA);
1582
1583 // queue another buffer so the first can be dropped
1584 nsecs_t requestedPresentTimeB = 0;
1585 nsecs_t postedTimeB = 0;
Vishnu Nairde66dc72021-06-17 17:54:41 -07001586 presentTime = systemTime() + std::chrono::nanoseconds(1ms).count();
1587 setUpAndQueueBuffer(igbProducer, &requestedPresentTimeB, &postedTimeB, &qbOutput, true,
1588 presentTime);
Vishnu Nair083efd32021-02-12 09:32:30 -08001589 history.applyDelta(qbOutput.frameTimestamps);
1590 events = history.getFrame(1);
1591 ASSERT_NE(nullptr, events);
1592
1593 // frame number, requestedPresentTime, and postTime should not have changed
1594 ASSERT_EQ(1, events->frameNumber);
1595 ASSERT_EQ(requestedPresentTimeA, events->requestedPresentTime);
1596 ASSERT_GE(events->postedTime, postedTimeA);
1597
Vishnu Nairde66dc72021-06-17 17:54:41 -07001598 // a valid latchtime and pre and post composition info should not be set for the dropped frame
Vishnu Nair083efd32021-02-12 09:32:30 -08001599 ASSERT_FALSE(events->hasLatchInfo());
1600 ASSERT_FALSE(events->hasDequeueReadyInfo());
Vishnu Nairde66dc72021-06-17 17:54:41 -07001601 ASSERT_FALSE(events->hasGpuCompositionDoneInfo());
1602 ASSERT_FALSE(events->hasDisplayPresentInfo());
1603 ASSERT_FALSE(events->hasReleaseInfo());
Vishnu Nair083efd32021-02-12 09:32:30 -08001604
Vishnu Nairde66dc72021-06-17 17:54:41 -07001605 // wait for the last transaction to be completed.
1606 adapter.waitForCallback(2);
Vishnu Nair083efd32021-02-12 09:32:30 -08001607
Vishnu Nairde66dc72021-06-17 17:54:41 -07001608 // queue another buffer so we query for frame event deltas
1609 nsecs_t requestedPresentTimeC = 0;
1610 nsecs_t postedTimeC = 0;
1611 setUpAndQueueBuffer(igbProducer, &requestedPresentTimeC, &postedTimeC, &qbOutput, true);
1612 history.applyDelta(qbOutput.frameTimestamps);
1613
1614 // frame number, requestedPresentTime, and postTime should not have changed
1615 ASSERT_EQ(1, events->frameNumber);
1616 ASSERT_EQ(requestedPresentTimeA, events->requestedPresentTime);
1617 ASSERT_GE(events->postedTime, postedTimeA);
1618
1619 // a valid latchtime and pre and post composition info should not be set for the dropped frame
1620 ASSERT_FALSE(events->hasLatchInfo());
1621 ASSERT_FALSE(events->hasDequeueReadyInfo());
1622 ASSERT_FALSE(events->hasGpuCompositionDoneInfo());
1623 ASSERT_FALSE(events->hasDisplayPresentInfo());
1624 ASSERT_FALSE(events->hasReleaseInfo());
1625
1626 // we should also have gotten values for the presented frame
Vishnu Nair083efd32021-02-12 09:32:30 -08001627 events = history.getFrame(2);
1628 ASSERT_NE(nullptr, events);
1629 ASSERT_EQ(2, events->frameNumber);
1630 ASSERT_EQ(requestedPresentTimeB, events->requestedPresentTime);
1631 ASSERT_GE(events->postedTime, postedTimeB);
Vishnu Nairde66dc72021-06-17 17:54:41 -07001632 ASSERT_GE(events->latchTime, postedTimeB);
1633 ASSERT_GE(events->dequeueReadyTime, events->latchTime);
1634 ASSERT_NE(nullptr, events->gpuCompositionDoneFence);
1635 ASSERT_NE(nullptr, events->displayPresentFence);
1636 ASSERT_NE(nullptr, events->releaseFence);
1637
1638 // wait for any callbacks that have not been received
1639 adapter.waitForCallbacks();
Vishnu Nair083efd32021-02-12 09:32:30 -08001640}
1641
Vishnu Nair9a69a042021-06-18 13:19:49 -07001642TEST_F(BLASTFrameEventHistoryTest, FrameEventHistory_CompositorTimings) {
1643 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
1644 sp<IGraphicBufferProducer> igbProducer;
1645 ProducerFrameEventHistory history;
1646 setUpProducer(adapter, igbProducer);
1647
1648 IGraphicBufferProducer::QueueBufferOutput qbOutput;
1649 nsecs_t requestedPresentTimeA = 0;
1650 nsecs_t postedTimeA = 0;
Vishnu Nair9a69a042021-06-18 13:19:49 -07001651 setUpAndQueueBuffer(igbProducer, &requestedPresentTimeA, &postedTimeA, &qbOutput, true);
1652 history.applyDelta(qbOutput.frameTimestamps);
1653 adapter.waitForCallback(1);
1654
1655 // queue another buffer so we query for frame event deltas
1656 nsecs_t requestedPresentTimeB = 0;
1657 nsecs_t postedTimeB = 0;
1658 setUpAndQueueBuffer(igbProducer, &requestedPresentTimeB, &postedTimeB, &qbOutput, true);
1659 history.applyDelta(qbOutput.frameTimestamps);
1660
1661 // check for a valid compositor deadline
1662 ASSERT_NE(0, history.getReportedCompositeDeadline());
1663
1664 // wait for any callbacks that have not been received
1665 adapter.waitForCallbacks();
1666}
1667
Valerie Hauc5011f92019-10-11 09:52:07 -07001668} // namespace android