blob: 70e656da9ba3e6a447e358378c9fcf03622bddd7 [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>
Valerie Hauda3446e2019-10-14 15:49:22 -070030#include <private/gui/ComposerService.h>
Dominik Laskowski3cb3d4e2019-11-21 11:14:45 -080031#include <ui/DisplayConfig.h>
Valerie Hauc5011f92019-10-11 09:52:07 -070032#include <ui/GraphicBuffer.h>
Valerie Hauda3446e2019-10-14 15:49:22 -070033#include <ui/GraphicTypes.h>
Valerie Hau8cee3f92019-11-06 10:06:28 -080034#include <ui/Transform.h>
Valerie Hauc5011f92019-10-11 09:52:07 -070035
36#include <gtest/gtest.h>
37
38using namespace std::chrono_literals;
39
40namespace android {
41
Valerie Hauc5011f92019-10-11 09:52:07 -070042using Transaction = SurfaceComposerClient::Transaction;
Valerie Hauda3446e2019-10-14 15:49:22 -070043using android::hardware::graphics::common::V1_2::BufferUsage;
Valerie Hauc5011f92019-10-11 09:52:07 -070044
45class BLASTBufferQueueHelper {
46public:
47 BLASTBufferQueueHelper(const sp<SurfaceControl>& sc, int width, int height) {
chaviw565ee542021-01-14 10:21:23 -080048 mBlastBufferQueueAdapter = new BLASTBufferQueue("TestBLASTBufferQueue", sc, width, height,
49 PIXEL_FORMAT_RGBA_8888);
Valerie Hauc5011f92019-10-11 09:52:07 -070050 }
51
52 void update(const sp<SurfaceControl>& sc, int width, int height) {
chaviw565ee542021-01-14 10:21:23 -080053 mBlastBufferQueueAdapter->update(sc, width, height, PIXEL_FORMAT_RGBA_8888);
Valerie Hauc5011f92019-10-11 09:52:07 -070054 }
55
56 void setNextTransaction(Transaction* next) {
57 mBlastBufferQueueAdapter->setNextTransaction(next);
58 }
59
Vishnu Nairea0de002020-11-17 17:42:37 -080060 int getWidth() { return mBlastBufferQueueAdapter->mSize.width; }
Valerie Hauda3446e2019-10-14 15:49:22 -070061
Vishnu Nairea0de002020-11-17 17:42:37 -080062 int getHeight() { return mBlastBufferQueueAdapter->mSize.height; }
Valerie Hauda3446e2019-10-14 15:49:22 -070063
Valerie Hauc5011f92019-10-11 09:52:07 -070064 Transaction* getNextTransaction() { return mBlastBufferQueueAdapter->mNextTransaction; }
Valerie Hauda3446e2019-10-14 15:49:22 -070065
66 sp<IGraphicBufferProducer> getIGraphicBufferProducer() {
67 return mBlastBufferQueueAdapter->getIGraphicBufferProducer();
68 }
69
Valerie Hauc5011f92019-10-11 09:52:07 -070070 const sp<SurfaceControl> getSurfaceControl() {
71 return mBlastBufferQueueAdapter->mSurfaceControl;
72 }
73
Valerie Haud3b90d22019-11-06 09:37:31 -080074 void waitForCallbacks() {
Valerie Hauda3446e2019-10-14 15:49:22 -070075 std::unique_lock lock{mBlastBufferQueueAdapter->mMutex};
Valerie Haua32c5522019-12-09 10:11:08 -080076 while (mBlastBufferQueueAdapter->mSubmitted.size() > 0) {
Valerie Haud3b90d22019-11-06 09:37:31 -080077 mBlastBufferQueueAdapter->mCallbackCV.wait(lock);
78 }
Valerie Hauda3446e2019-10-14 15:49:22 -070079 }
80
Valerie Hauc5011f92019-10-11 09:52:07 -070081private:
82 sp<BLASTBufferQueue> mBlastBufferQueueAdapter;
83};
84
85class BLASTBufferQueueTest : public ::testing::Test {
86public:
87protected:
88 BLASTBufferQueueTest() {
89 const ::testing::TestInfo* const testInfo =
90 ::testing::UnitTest::GetInstance()->current_test_info();
91 ALOGV("Begin test: %s.%s", testInfo->test_case_name(), testInfo->name());
92 }
93
94 ~BLASTBufferQueueTest() {
95 const ::testing::TestInfo* const testInfo =
96 ::testing::UnitTest::GetInstance()->current_test_info();
97 ALOGV("End test: %s.%s", testInfo->test_case_name(), testInfo->name());
98 }
99
100 void SetUp() {
Valerie Hauda3446e2019-10-14 15:49:22 -0700101 mComposer = ComposerService::getComposerService();
Valerie Hauc5011f92019-10-11 09:52:07 -0700102 mClient = new SurfaceComposerClient();
Valerie Hauda3446e2019-10-14 15:49:22 -0700103 mDisplayToken = mClient->getInternalDisplayToken();
104 ASSERT_NE(nullptr, mDisplayToken.get());
105 Transaction t;
106 t.setDisplayLayerStack(mDisplayToken, 0);
107 t.apply();
108 t.clear();
109
Dominik Laskowski3cb3d4e2019-11-21 11:14:45 -0800110 DisplayConfig config;
111 ASSERT_EQ(NO_ERROR, SurfaceComposerClient::getActiveDisplayConfig(mDisplayToken, &config));
112 const ui::Size& resolution = config.resolution;
113 mDisplayWidth = resolution.getWidth();
114 mDisplayHeight = resolution.getHeight();
Valerie Hauda3446e2019-10-14 15:49:22 -0700115
116 mSurfaceControl = mClient->createSurface(String8("TestSurface"), mDisplayWidth,
117 mDisplayHeight, PIXEL_FORMAT_RGBA_8888,
118 ISurfaceComposerClient::eFXSurfaceBufferState,
119 /*parent*/ nullptr);
120 t.setLayerStack(mSurfaceControl, 0)
121 .setLayer(mSurfaceControl, std::numeric_limits<int32_t>::max())
Dominik Laskowski3cb3d4e2019-11-21 11:14:45 -0800122 .setFrame(mSurfaceControl, Rect(resolution))
Valerie Hauda3446e2019-10-14 15:49:22 -0700123 .show(mSurfaceControl)
124 .setDataspace(mSurfaceControl, ui::Dataspace::V0_SRGB)
125 .apply();
chaviwd2432892020-07-24 17:42:39 -0700126
127 mCaptureArgs.displayToken = mDisplayToken;
arthurhung6fa58b72020-11-05 11:56:00 +0800128 mCaptureArgs.dataspace = ui::Dataspace::V0_SRGB;
Valerie Hauda3446e2019-10-14 15:49:22 -0700129 }
130
Valerie Haud3b90d22019-11-06 09:37:31 -0800131 void setUpProducer(BLASTBufferQueueHelper adapter, sp<IGraphicBufferProducer>& producer) {
132 auto igbProducer = adapter.getIGraphicBufferProducer();
133 ASSERT_NE(nullptr, igbProducer.get());
Valerie Hauc78c43a2020-01-09 17:34:14 -0800134 ASSERT_EQ(NO_ERROR, igbProducer->setMaxDequeuedBufferCount(2));
Valerie Haud3b90d22019-11-06 09:37:31 -0800135 IGraphicBufferProducer::QueueBufferOutput qbOutput;
136 ASSERT_EQ(NO_ERROR,
Peiyong Lind8460c82020-07-28 16:04:22 -0700137 igbProducer->connect(new StubProducerListener, NATIVE_WINDOW_API_CPU, false,
Valerie Haud3b90d22019-11-06 09:37:31 -0800138 &qbOutput));
Dominik Laskowski718f9602019-11-09 20:01:35 -0800139 ASSERT_NE(ui::Transform::ROT_INVALID, qbOutput.transformHint);
Valerie Haud3b90d22019-11-06 09:37:31 -0800140 producer = igbProducer;
141 }
142
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800143 void fillBuffer(uint32_t* bufData, Rect rect, uint32_t stride, uint8_t r, uint8_t g,
144 uint8_t b) {
145 for (uint32_t row = rect.top; row < rect.bottom; row++) {
146 for (uint32_t col = rect.left; col < rect.right; col++) {
Valerie Hauda3446e2019-10-14 15:49:22 -0700147 uint8_t* pixel = (uint8_t*)(bufData + (row * stride) + col);
148 *pixel = r;
149 *(pixel + 1) = g;
150 *(pixel + 2) = b;
151 *(pixel + 3) = 255;
152 }
153 }
154 }
155
Valerie Hau5977fc82019-12-05 15:56:39 -0800156 void fillQuadrants(sp<GraphicBuffer>& buf) {
157 const auto bufWidth = buf->getWidth();
158 const auto bufHeight = buf->getHeight();
159 uint32_t* bufData;
160 buf->lock(static_cast<uint32_t>(GraphicBuffer::USAGE_SW_WRITE_OFTEN),
161 reinterpret_cast<void**>(&bufData));
162 fillBuffer(bufData, Rect(0, 0, bufWidth / 2, bufHeight / 2), buf->getStride(), 0, 0, 0);
163 fillBuffer(bufData, Rect(bufWidth / 2, 0, bufWidth, bufHeight / 2), buf->getStride(), 255,
164 0, 0);
165 fillBuffer(bufData, Rect(bufWidth / 2, bufHeight / 2, bufWidth, bufHeight),
166 buf->getStride(), 0, 255, 0);
167 fillBuffer(bufData, Rect(0, bufHeight / 2, bufWidth / 2, bufHeight), buf->getStride(), 0, 0,
168 255);
169 buf->unlock();
170 }
171
172 void checkScreenCapture(uint8_t r, uint8_t g, uint8_t b, Rect region, int32_t border = 0,
173 bool outsideRegion = false) {
chaviwd2432892020-07-24 17:42:39 -0700174 sp<GraphicBuffer>& captureBuf = mCaptureResults.buffer;
Valerie Hau5977fc82019-12-05 15:56:39 -0800175 const auto epsilon = 3;
chaviwd2432892020-07-24 17:42:39 -0700176 const auto width = captureBuf->getWidth();
177 const auto height = captureBuf->getHeight();
178 const auto stride = captureBuf->getStride();
Valerie Hauda3446e2019-10-14 15:49:22 -0700179
180 uint32_t* bufData;
chaviwd2432892020-07-24 17:42:39 -0700181 captureBuf->lock(static_cast<uint32_t>(GraphicBuffer::USAGE_SW_READ_OFTEN),
182 reinterpret_cast<void**>(&bufData));
Valerie Hauda3446e2019-10-14 15:49:22 -0700183
184 for (uint32_t row = 0; row < height; row++) {
185 for (uint32_t col = 0; col < width; col++) {
186 uint8_t* pixel = (uint8_t*)(bufData + (row * stride) + col);
arthurhung6fa58b72020-11-05 11:56:00 +0800187 ASSERT_NE(nullptr, pixel);
Valerie Hau5977fc82019-12-05 15:56:39 -0800188 bool inRegion;
189 if (!outsideRegion) {
190 inRegion = row >= region.top + border && row < region.bottom - border &&
191 col >= region.left + border && col < region.right - border;
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800192 } else {
Valerie Hau5977fc82019-12-05 15:56:39 -0800193 inRegion = row >= region.top - border && row < region.bottom + border &&
194 col >= region.left - border && col < region.right + border;
195 }
196 if (!outsideRegion && inRegion) {
197 EXPECT_GE(epsilon, abs(r - *(pixel)));
198 EXPECT_GE(epsilon, abs(g - *(pixel + 1)));
199 EXPECT_GE(epsilon, abs(b - *(pixel + 2)));
200 } else if (outsideRegion && !inRegion) {
201 EXPECT_GE(epsilon, abs(r - *(pixel)));
202 EXPECT_GE(epsilon, abs(g - *(pixel + 1)));
203 EXPECT_GE(epsilon, abs(b - *(pixel + 2)));
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800204 }
Valerie Hauda3446e2019-10-14 15:49:22 -0700205 }
206 }
chaviwd2432892020-07-24 17:42:39 -0700207 captureBuf->unlock();
Valerie Hauda3446e2019-10-14 15:49:22 -0700208 ASSERT_EQ(false, ::testing::Test::HasFailure());
Valerie Hauc5011f92019-10-11 09:52:07 -0700209 }
210
chaviw8ffc7b82020-08-18 11:25:37 -0700211 static status_t captureDisplay(DisplayCaptureArgs& captureArgs,
212 ScreenCaptureResults& captureResults) {
213 const auto sf = ComposerService::getComposerService();
214 SurfaceComposerClient::Transaction().apply(true);
215
216 const sp<SyncScreenCaptureListener> captureListener = new SyncScreenCaptureListener();
217 status_t status = sf->captureDisplay(captureArgs, captureListener);
218 if (status != NO_ERROR) {
219 return status;
220 }
221 captureResults = captureListener->waitForResults();
222 return captureResults.result;
223 }
224
Vishnu Nair277142c2021-01-05 18:35:29 -0800225 void queueBuffer(sp<IGraphicBufferProducer> igbp, uint8_t r, uint8_t g, uint8_t b,
226 nsecs_t presentTimeDelay) {
227 int slot;
228 sp<Fence> fence;
229 sp<GraphicBuffer> buf;
230 auto ret = igbp->dequeueBuffer(&slot, &fence, mDisplayWidth, mDisplayHeight,
231 PIXEL_FORMAT_RGBA_8888, GRALLOC_USAGE_SW_WRITE_OFTEN,
232 nullptr, nullptr);
233 ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, ret);
234 ASSERT_EQ(OK, igbp->requestBuffer(slot, &buf));
235
236 uint32_t* bufData;
237 buf->lock(static_cast<uint32_t>(GraphicBuffer::USAGE_SW_WRITE_OFTEN),
238 reinterpret_cast<void**>(&bufData));
239 fillBuffer(bufData, Rect(buf->getWidth(), buf->getHeight() / 2), buf->getStride(), r, g, b);
240 buf->unlock();
241
242 IGraphicBufferProducer::QueueBufferOutput qbOutput;
243 nsecs_t timestampNanos = systemTime() + presentTimeDelay;
244 IGraphicBufferProducer::QueueBufferInput input(timestampNanos, false, HAL_DATASPACE_UNKNOWN,
245 Rect(mDisplayWidth, mDisplayHeight / 2),
246 NATIVE_WINDOW_SCALING_MODE_FREEZE, 0,
247 Fence::NO_FENCE);
248 igbp->queueBuffer(slot, input, &qbOutput);
249 }
250
Valerie Hauc5011f92019-10-11 09:52:07 -0700251 sp<SurfaceComposerClient> mClient;
Valerie Hauda3446e2019-10-14 15:49:22 -0700252 sp<ISurfaceComposer> mComposer;
253
254 sp<IBinder> mDisplayToken;
255
Valerie Hauc5011f92019-10-11 09:52:07 -0700256 sp<SurfaceControl> mSurfaceControl;
Valerie Hauda3446e2019-10-14 15:49:22 -0700257
258 uint32_t mDisplayWidth;
259 uint32_t mDisplayHeight;
chaviwd2432892020-07-24 17:42:39 -0700260
261 DisplayCaptureArgs mCaptureArgs;
262 ScreenCaptureResults mCaptureResults;
Valerie Hauc5011f92019-10-11 09:52:07 -0700263};
264
265TEST_F(BLASTBufferQueueTest, CreateBLASTBufferQueue) {
266 // create BLASTBufferQueue adapter associated with this surface
Valerie Hauda3446e2019-10-14 15:49:22 -0700267 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
Valerie Hauc5011f92019-10-11 09:52:07 -0700268 ASSERT_EQ(mSurfaceControl, adapter.getSurfaceControl());
Valerie Hauda3446e2019-10-14 15:49:22 -0700269 ASSERT_EQ(mDisplayWidth, adapter.getWidth());
270 ASSERT_EQ(mDisplayHeight, adapter.getHeight());
Valerie Hauc5011f92019-10-11 09:52:07 -0700271 ASSERT_EQ(nullptr, adapter.getNextTransaction());
272}
273
274TEST_F(BLASTBufferQueueTest, Update) {
Valerie Hauda3446e2019-10-14 15:49:22 -0700275 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
Valerie Hauc5011f92019-10-11 09:52:07 -0700276 sp<SurfaceControl> updateSurface =
Valerie Hauda3446e2019-10-14 15:49:22 -0700277 mClient->createSurface(String8("UpdateTest"), mDisplayWidth / 2, mDisplayHeight / 2,
278 PIXEL_FORMAT_RGBA_8888);
279 adapter.update(updateSurface, mDisplayWidth / 2, mDisplayHeight / 2);
Valerie Hauc5011f92019-10-11 09:52:07 -0700280 ASSERT_EQ(updateSurface, adapter.getSurfaceControl());
Vishnu Nairea0de002020-11-17 17:42:37 -0800281 sp<IGraphicBufferProducer> igbProducer;
282 setUpProducer(adapter, igbProducer);
283
284 int32_t width;
285 igbProducer->query(NATIVE_WINDOW_WIDTH, &width);
286 ASSERT_EQ(mDisplayWidth / 2, width);
287 int32_t height;
288 igbProducer->query(NATIVE_WINDOW_HEIGHT, &height);
289 ASSERT_EQ(mDisplayHeight / 2, height);
Valerie Hauc5011f92019-10-11 09:52:07 -0700290}
291
292TEST_F(BLASTBufferQueueTest, SetNextTransaction) {
Valerie Hauda3446e2019-10-14 15:49:22 -0700293 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
Valerie Hauc5011f92019-10-11 09:52:07 -0700294 Transaction next;
295 adapter.setNextTransaction(&next);
296 ASSERT_EQ(&next, adapter.getNextTransaction());
297}
Valerie Hauda3446e2019-10-14 15:49:22 -0700298
Valerie Haubf29e042020-02-06 11:40:38 -0800299TEST_F(BLASTBufferQueueTest, DISABLED_onFrameAvailable_ApplyDesiredPresentTime) {
Valerie Hau181abd32020-01-27 14:18:28 -0800300 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
301 sp<IGraphicBufferProducer> igbProducer;
302 setUpProducer(adapter, igbProducer);
303
304 int slot;
305 sp<Fence> fence;
306 sp<GraphicBuffer> buf;
307 auto ret = igbProducer->dequeueBuffer(&slot, &fence, mDisplayWidth, mDisplayHeight,
308 PIXEL_FORMAT_RGBA_8888, GRALLOC_USAGE_SW_WRITE_OFTEN,
309 nullptr, nullptr);
310 ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, ret);
311 ASSERT_EQ(OK, igbProducer->requestBuffer(slot, &buf));
312
313 nsecs_t desiredPresentTime = systemTime() + nsecs_t(5 * 1e8);
314 IGraphicBufferProducer::QueueBufferOutput qbOutput;
315 IGraphicBufferProducer::QueueBufferInput input(desiredPresentTime, false, HAL_DATASPACE_UNKNOWN,
316 Rect(mDisplayWidth, mDisplayHeight),
317 NATIVE_WINDOW_SCALING_MODE_FREEZE, 0,
318 Fence::NO_FENCE);
319 igbProducer->queueBuffer(slot, input, &qbOutput);
320 ASSERT_NE(ui::Transform::ROT_INVALID, qbOutput.transformHint);
321
322 adapter.waitForCallbacks();
323 ASSERT_GE(systemTime(), desiredPresentTime);
324}
325
Valerie Hauda3446e2019-10-14 15:49:22 -0700326TEST_F(BLASTBufferQueueTest, onFrameAvailable_Apply) {
327 uint8_t r = 255;
328 uint8_t g = 0;
329 uint8_t b = 0;
330
331 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
Valerie Haud3b90d22019-11-06 09:37:31 -0800332 sp<IGraphicBufferProducer> igbProducer;
333 setUpProducer(adapter, igbProducer);
Valerie Hauda3446e2019-10-14 15:49:22 -0700334
335 int slot;
336 sp<Fence> fence;
337 sp<GraphicBuffer> buf;
338 auto ret = igbProducer->dequeueBuffer(&slot, &fence, mDisplayWidth, mDisplayHeight,
339 PIXEL_FORMAT_RGBA_8888, GRALLOC_USAGE_SW_WRITE_OFTEN,
340 nullptr, nullptr);
341 ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, ret);
342 ASSERT_EQ(OK, igbProducer->requestBuffer(slot, &buf));
343
344 uint32_t* bufData;
345 buf->lock(static_cast<uint32_t>(GraphicBuffer::USAGE_SW_WRITE_OFTEN),
346 reinterpret_cast<void**>(&bufData));
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800347 fillBuffer(bufData, Rect(buf->getWidth(), buf->getHeight()), buf->getStride(), r, g, b);
Valerie Hauda3446e2019-10-14 15:49:22 -0700348 buf->unlock();
349
Valerie Haud3b90d22019-11-06 09:37:31 -0800350 IGraphicBufferProducer::QueueBufferOutput qbOutput;
Valerie Hauda3446e2019-10-14 15:49:22 -0700351 IGraphicBufferProducer::QueueBufferInput input(systemTime(), false, HAL_DATASPACE_UNKNOWN,
352 Rect(mDisplayWidth, mDisplayHeight),
353 NATIVE_WINDOW_SCALING_MODE_FREEZE, 0,
354 Fence::NO_FENCE);
355 igbProducer->queueBuffer(slot, input, &qbOutput);
Dominik Laskowski718f9602019-11-09 20:01:35 -0800356 ASSERT_NE(ui::Transform::ROT_INVALID, qbOutput.transformHint);
Valerie Hauda3446e2019-10-14 15:49:22 -0700357
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800358 adapter.waitForCallbacks();
Valerie Hauda3446e2019-10-14 15:49:22 -0700359
360 // capture screen and verify that it is red
chaviw8ffc7b82020-08-18 11:25:37 -0700361 ASSERT_EQ(NO_ERROR, captureDisplay(mCaptureArgs, mCaptureResults));
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800362 ASSERT_NO_FATAL_FAILURE(
363 checkScreenCapture(r, g, b, {0, 0, (int32_t)mDisplayWidth, (int32_t)mDisplayHeight}));
Valerie Hauda3446e2019-10-14 15:49:22 -0700364}
Valerie Haud3b90d22019-11-06 09:37:31 -0800365
366TEST_F(BLASTBufferQueueTest, TripleBuffering) {
367 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
368 sp<IGraphicBufferProducer> igbProducer;
369 setUpProducer(adapter, igbProducer);
370
371 std::vector<std::pair<int, sp<Fence>>> allocated;
372 for (int i = 0; i < 3; i++) {
373 int slot;
374 sp<Fence> fence;
375 sp<GraphicBuffer> buf;
376 auto ret = igbProducer->dequeueBuffer(&slot, &fence, mDisplayWidth, mDisplayHeight,
377 PIXEL_FORMAT_RGBA_8888, GRALLOC_USAGE_SW_WRITE_OFTEN,
378 nullptr, nullptr);
379 ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, ret);
380 ASSERT_EQ(OK, igbProducer->requestBuffer(slot, &buf));
381 allocated.push_back({slot, fence});
382 }
383 for (int i = 0; i < allocated.size(); i++) {
384 igbProducer->cancelBuffer(allocated[i].first, allocated[i].second);
385 }
386
Valerie Haua32c5522019-12-09 10:11:08 -0800387 for (int i = 0; i < 100; i++) {
Valerie Haud3b90d22019-11-06 09:37:31 -0800388 int slot;
389 sp<Fence> fence;
390 sp<GraphicBuffer> buf;
391 auto ret = igbProducer->dequeueBuffer(&slot, &fence, mDisplayWidth, mDisplayHeight,
392 PIXEL_FORMAT_RGBA_8888, GRALLOC_USAGE_SW_WRITE_OFTEN,
393 nullptr, nullptr);
394 ASSERT_EQ(NO_ERROR, ret);
395 IGraphicBufferProducer::QueueBufferOutput qbOutput;
396 IGraphicBufferProducer::QueueBufferInput input(systemTime(), false, HAL_DATASPACE_UNKNOWN,
397 Rect(mDisplayWidth, mDisplayHeight),
398 NATIVE_WINDOW_SCALING_MODE_FREEZE, 0,
399 Fence::NO_FENCE);
400 igbProducer->queueBuffer(slot, input, &qbOutput);
401 }
402 adapter.waitForCallbacks();
403}
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800404
405TEST_F(BLASTBufferQueueTest, SetCrop_Item) {
406 uint8_t r = 255;
407 uint8_t g = 0;
408 uint8_t b = 0;
409
410 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
411 sp<IGraphicBufferProducer> igbProducer;
412 setUpProducer(adapter, igbProducer);
413 int slot;
414 sp<Fence> fence;
415 sp<GraphicBuffer> buf;
416 auto ret = igbProducer->dequeueBuffer(&slot, &fence, mDisplayWidth, mDisplayHeight,
417 PIXEL_FORMAT_RGBA_8888, GRALLOC_USAGE_SW_WRITE_OFTEN,
418 nullptr, nullptr);
419 ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, ret);
420 ASSERT_EQ(OK, igbProducer->requestBuffer(slot, &buf));
421
422 uint32_t* bufData;
423 buf->lock(static_cast<uint32_t>(GraphicBuffer::USAGE_SW_WRITE_OFTEN),
424 reinterpret_cast<void**>(&bufData));
425 fillBuffer(bufData, Rect(buf->getWidth(), buf->getHeight() / 2), buf->getStride(), r, g, b);
426 buf->unlock();
427
428 IGraphicBufferProducer::QueueBufferOutput qbOutput;
429 IGraphicBufferProducer::QueueBufferInput input(systemTime(), false, HAL_DATASPACE_UNKNOWN,
430 Rect(mDisplayWidth, mDisplayHeight / 2),
431 NATIVE_WINDOW_SCALING_MODE_FREEZE, 0,
432 Fence::NO_FENCE);
433 igbProducer->queueBuffer(slot, input, &qbOutput);
Dominik Laskowski718f9602019-11-09 20:01:35 -0800434 ASSERT_NE(ui::Transform::ROT_INVALID, qbOutput.transformHint);
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800435
436 adapter.waitForCallbacks();
437 // capture screen and verify that it is red
chaviw8ffc7b82020-08-18 11:25:37 -0700438 ASSERT_EQ(NO_ERROR, captureDisplay(mCaptureArgs, mCaptureResults));
chaviwd2432892020-07-24 17:42:39 -0700439
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800440 ASSERT_NO_FATAL_FAILURE(
441 checkScreenCapture(r, g, b, {0, 0, (int32_t)mDisplayWidth, (int32_t)mDisplayHeight}));
442}
443
444TEST_F(BLASTBufferQueueTest, SetCrop_ScalingModeScaleCrop) {
445 uint8_t r = 255;
446 uint8_t g = 0;
447 uint8_t b = 0;
448
449 int32_t bufferSideLength =
450 (mDisplayWidth < mDisplayHeight) ? mDisplayWidth / 2 : mDisplayHeight / 2;
451 int32_t finalCropSideLength = bufferSideLength / 2;
452
453 auto bg = mClient->createSurface(String8("BGTest"), 0, 0, PIXEL_FORMAT_RGBA_8888,
Vishnu Nairfa247b12020-02-11 08:58:26 -0800454 ISurfaceComposerClient::eFXSurfaceEffect);
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800455 ASSERT_NE(nullptr, bg.get());
456 Transaction t;
457 t.setLayerStack(bg, 0)
458 .setCrop_legacy(bg, Rect(0, 0, mDisplayWidth, mDisplayHeight))
459 .setColor(bg, half3{0, 0, 0})
460 .setLayer(bg, 0)
461 .apply();
462
463 BLASTBufferQueueHelper adapter(mSurfaceControl, bufferSideLength, bufferSideLength);
464 sp<IGraphicBufferProducer> igbProducer;
465 setUpProducer(adapter, igbProducer);
466 int slot;
467 sp<Fence> fence;
468 sp<GraphicBuffer> buf;
469 auto ret = igbProducer->dequeueBuffer(&slot, &fence, bufferSideLength, bufferSideLength,
470 PIXEL_FORMAT_RGBA_8888, GRALLOC_USAGE_SW_WRITE_OFTEN,
471 nullptr, nullptr);
472 ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, ret);
473 ASSERT_EQ(OK, igbProducer->requestBuffer(slot, &buf));
474
475 uint32_t* bufData;
476 buf->lock(static_cast<uint32_t>(GraphicBuffer::USAGE_SW_WRITE_OFTEN),
477 reinterpret_cast<void**>(&bufData));
478 fillBuffer(bufData, Rect(buf->getWidth(), buf->getHeight()), buf->getStride(), 0, 0, 0);
479 fillBuffer(bufData,
480 Rect(finalCropSideLength / 2, 0, buf->getWidth() - finalCropSideLength / 2,
481 buf->getHeight()),
482 buf->getStride(), r, g, b);
483 buf->unlock();
484
485 IGraphicBufferProducer::QueueBufferOutput qbOutput;
486 IGraphicBufferProducer::QueueBufferInput input(systemTime(), false, HAL_DATASPACE_UNKNOWN,
487 Rect(bufferSideLength, finalCropSideLength),
488 NATIVE_WINDOW_SCALING_MODE_SCALE_CROP, 0,
489 Fence::NO_FENCE);
490 igbProducer->queueBuffer(slot, input, &qbOutput);
Dominik Laskowski718f9602019-11-09 20:01:35 -0800491 ASSERT_NE(ui::Transform::ROT_INVALID, qbOutput.transformHint);
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800492
493 adapter.waitForCallbacks();
494 // capture screen and verify that it is red
chaviw8ffc7b82020-08-18 11:25:37 -0700495 ASSERT_EQ(NO_ERROR, captureDisplay(mCaptureArgs, mCaptureResults));
chaviwd2432892020-07-24 17:42:39 -0700496
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800497 ASSERT_NO_FATAL_FAILURE(
498 checkScreenCapture(r, g, b,
499 {0, 0, (int32_t)bufferSideLength, (int32_t)bufferSideLength}));
Valerie Hau5977fc82019-12-05 15:56:39 -0800500 ASSERT_NO_FATAL_FAILURE(
501 checkScreenCapture(0, 0, 0,
502 {0, 0, (int32_t)bufferSideLength, (int32_t)bufferSideLength},
503 /*border*/ 0, /*outsideRegion*/ true));
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800504}
505
Vishnu Nair89496122020-12-14 17:14:53 -0800506class TestProducerListener : public BnProducerListener {
507public:
508 sp<IGraphicBufferProducer> mIgbp;
509 TestProducerListener(const sp<IGraphicBufferProducer>& igbp) : mIgbp(igbp) {}
510 void onBufferReleased() override {
511 sp<GraphicBuffer> buffer;
512 sp<Fence> fence;
513 mIgbp->detachNextBuffer(&buffer, &fence);
514 }
515};
516
517TEST_F(BLASTBufferQueueTest, CustomProducerListener) {
518 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
519 sp<IGraphicBufferProducer> igbProducer = adapter.getIGraphicBufferProducer();
520 ASSERT_NE(nullptr, igbProducer.get());
521 ASSERT_EQ(NO_ERROR, igbProducer->setMaxDequeuedBufferCount(2));
522 IGraphicBufferProducer::QueueBufferOutput qbOutput;
523 ASSERT_EQ(NO_ERROR,
524 igbProducer->connect(new TestProducerListener(igbProducer), NATIVE_WINDOW_API_CPU,
525 false, &qbOutput));
526 ASSERT_NE(ui::Transform::ROT_INVALID, qbOutput.transformHint);
527 for (int i = 0; i < 3; i++) {
528 int slot;
529 sp<Fence> fence;
530 sp<GraphicBuffer> buf;
531 auto ret = igbProducer->dequeueBuffer(&slot, &fence, mDisplayWidth, mDisplayHeight,
532 PIXEL_FORMAT_RGBA_8888, GRALLOC_USAGE_SW_WRITE_OFTEN,
533 nullptr, nullptr);
534 ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, ret);
535 ASSERT_EQ(OK, igbProducer->requestBuffer(slot, &buf));
536 IGraphicBufferProducer::QueueBufferOutput qbOutput;
537 IGraphicBufferProducer::QueueBufferInput input(systemTime(), false, HAL_DATASPACE_UNKNOWN,
538 Rect(mDisplayWidth, mDisplayHeight),
539 NATIVE_WINDOW_SCALING_MODE_FREEZE, 0,
540 Fence::NO_FENCE);
541 igbProducer->queueBuffer(slot, input, &qbOutput);
542 }
543 adapter.waitForCallbacks();
544}
545
Vishnu Nair17dde612020-12-28 11:39:59 -0800546TEST_F(BLASTBufferQueueTest, QueryNativeWindowQueuesToWindowComposer) {
547 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
548
549 sp<android::Surface> surface = new Surface(adapter.getIGraphicBufferProducer());
550 ANativeWindow* nativeWindow = (ANativeWindow*)(surface.get());
551 int queuesToNativeWindow = 0;
552 int err = nativeWindow->query(nativeWindow, NATIVE_WINDOW_QUEUES_TO_WINDOW_COMPOSER,
553 &queuesToNativeWindow);
554 ASSERT_EQ(NO_ERROR, err);
555 ASSERT_EQ(queuesToNativeWindow, 1);
556}
557
Vishnu Nair277142c2021-01-05 18:35:29 -0800558TEST_F(BLASTBufferQueueTest, OutOfOrderTransactionTest) {
559 sp<SurfaceControl> bgSurface =
560 mClient->createSurface(String8("BGTest"), 0, 0, PIXEL_FORMAT_RGBA_8888,
561 ISurfaceComposerClient::eFXSurfaceBufferState);
562 ASSERT_NE(nullptr, bgSurface.get());
563 Transaction t;
564 t.setLayerStack(bgSurface, 0)
565 .show(bgSurface)
566 .setDataspace(bgSurface, ui::Dataspace::V0_SRGB)
567 .setFrame(bgSurface, Rect(0, 0, mDisplayWidth, mDisplayHeight))
568 .setLayer(bgSurface, std::numeric_limits<int32_t>::max() - 1)
569 .apply();
570
571 BLASTBufferQueueHelper slowAdapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
572 sp<IGraphicBufferProducer> slowIgbProducer;
573 setUpProducer(slowAdapter, slowIgbProducer);
574 nsecs_t presentTimeDelay = std::chrono::nanoseconds(500ms).count();
575 queueBuffer(slowIgbProducer, 0 /* r */, 0 /* g */, 0 /* b */, presentTimeDelay);
576
577 BLASTBufferQueueHelper fastAdapter(bgSurface, mDisplayWidth, mDisplayHeight);
578 sp<IGraphicBufferProducer> fastIgbProducer;
579 setUpProducer(fastAdapter, fastIgbProducer);
580 uint8_t r = 255;
581 uint8_t g = 0;
582 uint8_t b = 0;
583 queueBuffer(fastIgbProducer, r, g, b, 0 /* presentTimeDelay */);
584 fastAdapter.waitForCallbacks();
585
586 // capture screen and verify that it is red
587 ASSERT_EQ(NO_ERROR, captureDisplay(mCaptureArgs, mCaptureResults));
588
589 ASSERT_NO_FATAL_FAILURE(
590 checkScreenCapture(r, g, b, {0, 0, (int32_t)mDisplayWidth, (int32_t)mDisplayHeight}));
591}
592
Valerie Hau5977fc82019-12-05 15:56:39 -0800593class BLASTBufferQueueTransformTest : public BLASTBufferQueueTest {
594public:
595 void test(uint32_t tr) {
596 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
597 sp<IGraphicBufferProducer> igbProducer;
598 setUpProducer(adapter, igbProducer);
599
600 auto bufWidth = mDisplayWidth;
601 auto bufHeight = mDisplayHeight;
602 int slot;
603 sp<Fence> fence;
604 sp<GraphicBuffer> buf;
605
606 auto ret = igbProducer->dequeueBuffer(&slot, &fence, bufWidth, bufHeight,
607 PIXEL_FORMAT_RGBA_8888, GRALLOC_USAGE_SW_WRITE_OFTEN,
608 nullptr, nullptr);
609 ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, ret);
610 ASSERT_EQ(OK, igbProducer->requestBuffer(slot, &buf));
611
612 fillQuadrants(buf);
613
614 IGraphicBufferProducer::QueueBufferOutput qbOutput;
615 IGraphicBufferProducer::QueueBufferInput input(systemTime(), false, HAL_DATASPACE_UNKNOWN,
616 Rect(bufWidth, bufHeight),
Vishnu Naire1a42322020-10-02 17:42:04 -0700617 NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW,
618 tr, Fence::NO_FENCE);
Valerie Hau5977fc82019-12-05 15:56:39 -0800619 igbProducer->queueBuffer(slot, input, &qbOutput);
Dominik Laskowski718f9602019-11-09 20:01:35 -0800620 ASSERT_NE(ui::Transform::ROT_INVALID, qbOutput.transformHint);
Valerie Hau5977fc82019-12-05 15:56:39 -0800621
622 adapter.waitForCallbacks();
chaviw8ffc7b82020-08-18 11:25:37 -0700623 ASSERT_EQ(NO_ERROR, captureDisplay(mCaptureArgs, mCaptureResults));
chaviwd2432892020-07-24 17:42:39 -0700624
Valerie Hau5977fc82019-12-05 15:56:39 -0800625 switch (tr) {
626 case ui::Transform::ROT_0:
627 ASSERT_NO_FATAL_FAILURE(checkScreenCapture(0, 0, 0,
628 {0, 0, (int32_t)mDisplayWidth / 2,
629 (int32_t)mDisplayHeight / 2},
630 1));
631 ASSERT_NO_FATAL_FAILURE(
632 checkScreenCapture(255, 0, 0,
633 {(int32_t)mDisplayWidth / 2, 0, (int32_t)mDisplayWidth,
634 (int32_t)mDisplayHeight / 2},
635 1));
636 ASSERT_NO_FATAL_FAILURE(
637 checkScreenCapture(0, 255, 0,
638 {(int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight / 2,
639 (int32_t)mDisplayWidth, (int32_t)mDisplayHeight},
640 1));
641 ASSERT_NO_FATAL_FAILURE(
642 checkScreenCapture(0, 0, 255,
643 {0, (int32_t)mDisplayHeight / 2,
644 (int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight},
645 1));
646 break;
647 case ui::Transform::FLIP_H:
648 ASSERT_NO_FATAL_FAILURE(checkScreenCapture(255, 0, 0,
649 {0, 0, (int32_t)mDisplayWidth / 2,
650 (int32_t)mDisplayHeight / 2},
651 1));
652 ASSERT_NO_FATAL_FAILURE(
653 checkScreenCapture(0, 0, 0,
654 {(int32_t)mDisplayWidth / 2, 0, (int32_t)mDisplayWidth,
655 (int32_t)mDisplayHeight / 2},
656 1));
657 ASSERT_NO_FATAL_FAILURE(
658 checkScreenCapture(0, 0, 255,
659 {(int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight / 2,
660 (int32_t)mDisplayWidth, (int32_t)mDisplayHeight},
661 1));
662 ASSERT_NO_FATAL_FAILURE(
663 checkScreenCapture(0, 255, 0,
664 {0, (int32_t)mDisplayHeight / 2,
665 (int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight},
666 1));
667 break;
668 case ui::Transform::FLIP_V:
669 ASSERT_NO_FATAL_FAILURE(checkScreenCapture(0, 0, 255,
670 {0, 0, (int32_t)mDisplayWidth / 2,
671 (int32_t)mDisplayHeight / 2},
672 1));
673 ASSERT_NO_FATAL_FAILURE(
674 checkScreenCapture(0, 255, 0,
675 {(int32_t)mDisplayWidth / 2, 0, (int32_t)mDisplayWidth,
676 (int32_t)mDisplayHeight / 2},
677 1));
678 ASSERT_NO_FATAL_FAILURE(
679 checkScreenCapture(255, 0, 0,
680 {(int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight / 2,
681 (int32_t)mDisplayWidth, (int32_t)mDisplayHeight},
682 1));
683 ASSERT_NO_FATAL_FAILURE(
684 checkScreenCapture(0, 0, 0,
685 {0, (int32_t)mDisplayHeight / 2,
686 (int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight},
687 1));
688 break;
689 case ui::Transform::ROT_90:
690 ASSERT_NO_FATAL_FAILURE(checkScreenCapture(0, 0, 255,
691 {0, 0, (int32_t)mDisplayWidth / 2,
692 (int32_t)mDisplayHeight / 2},
693 1));
694 ASSERT_NO_FATAL_FAILURE(
695 checkScreenCapture(0, 0, 0,
696 {(int32_t)mDisplayWidth / 2, 0, (int32_t)mDisplayWidth,
697 (int32_t)mDisplayHeight / 2},
698 1));
699 ASSERT_NO_FATAL_FAILURE(
700 checkScreenCapture(255, 0, 0,
701 {(int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight / 2,
702 (int32_t)mDisplayWidth, (int32_t)mDisplayHeight},
703 1));
704 ASSERT_NO_FATAL_FAILURE(
705 checkScreenCapture(0, 255, 0,
706 {0, (int32_t)mDisplayHeight / 2,
707 (int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight},
708 1));
709 break;
710 case ui::Transform::ROT_180:
711 ASSERT_NO_FATAL_FAILURE(checkScreenCapture(0, 255, 0,
712 {0, 0, (int32_t)mDisplayWidth / 2,
713 (int32_t)mDisplayHeight / 2},
714 1));
715 ASSERT_NO_FATAL_FAILURE(
716 checkScreenCapture(0, 0, 255,
717 {(int32_t)mDisplayWidth / 2, 0, (int32_t)mDisplayWidth,
718 (int32_t)mDisplayHeight / 2},
719 1));
720 ASSERT_NO_FATAL_FAILURE(
721 checkScreenCapture(0, 0, 0,
722 {(int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight / 2,
723 (int32_t)mDisplayWidth, (int32_t)mDisplayHeight},
724 1));
725 ASSERT_NO_FATAL_FAILURE(
726 checkScreenCapture(255, 0, 0,
727 {0, (int32_t)mDisplayHeight / 2,
728 (int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight},
729 1));
730 break;
731 case ui::Transform::ROT_270:
732 ASSERT_NO_FATAL_FAILURE(checkScreenCapture(255, 0, 0,
733 {0, 0, (int32_t)mDisplayWidth / 2,
734 (int32_t)mDisplayHeight / 2},
735 1));
736 ASSERT_NO_FATAL_FAILURE(
737 checkScreenCapture(0, 255, 0,
738 {(int32_t)mDisplayWidth / 2, 0, (int32_t)mDisplayWidth,
739 (int32_t)mDisplayHeight / 2},
740 1));
741 ASSERT_NO_FATAL_FAILURE(
742 checkScreenCapture(0, 0, 255,
743 {(int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight / 2,
744 (int32_t)mDisplayWidth, (int32_t)mDisplayHeight},
745 1));
746 ASSERT_NO_FATAL_FAILURE(
747 checkScreenCapture(0, 0, 0,
748 {0, (int32_t)mDisplayHeight / 2,
749 (int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight},
750 1));
751 }
752 }
753};
754
755TEST_F(BLASTBufferQueueTransformTest, setTransform_ROT_0) {
756 test(ui::Transform::ROT_0);
757}
758
759TEST_F(BLASTBufferQueueTransformTest, setTransform_FLIP_H) {
760 test(ui::Transform::FLIP_H);
761}
762
763TEST_F(BLASTBufferQueueTransformTest, setTransform_FLIP_V) {
764 test(ui::Transform::FLIP_V);
765}
766
767TEST_F(BLASTBufferQueueTransformTest, setTransform_ROT_90) {
768 test(ui::Transform::ROT_90);
769}
770
771TEST_F(BLASTBufferQueueTransformTest, setTransform_ROT_180) {
772 test(ui::Transform::ROT_180);
773}
774
775TEST_F(BLASTBufferQueueTransformTest, setTransform_ROT_270) {
776 test(ui::Transform::ROT_270);
777}
Valerie Hau871d6352020-01-29 08:44:02 -0800778
779class BLASTFrameEventHistoryTest : public BLASTBufferQueueTest {
780public:
781 void setUpAndQueueBuffer(const sp<IGraphicBufferProducer>& igbProducer,
782 nsecs_t* requestedPresentTime, nsecs_t* postedTime,
783 IGraphicBufferProducer::QueueBufferOutput* qbOutput,
784 bool getFrameTimestamps) {
785 int slot;
786 sp<Fence> fence;
787 sp<GraphicBuffer> buf;
788 auto ret = igbProducer->dequeueBuffer(&slot, &fence, mDisplayWidth, mDisplayHeight,
789 PIXEL_FORMAT_RGBA_8888, GRALLOC_USAGE_SW_WRITE_OFTEN,
790 nullptr, nullptr);
791 ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, ret);
792 ASSERT_EQ(OK, igbProducer->requestBuffer(slot, &buf));
793
794 nsecs_t requestedTime = systemTime();
795 if (requestedPresentTime) *requestedPresentTime = requestedTime;
796 IGraphicBufferProducer::QueueBufferInput input(requestedTime, false, HAL_DATASPACE_UNKNOWN,
797 Rect(mDisplayWidth, mDisplayHeight),
798 NATIVE_WINDOW_SCALING_MODE_FREEZE, 0,
799 Fence::NO_FENCE, /*sticky*/ 0,
800 getFrameTimestamps);
801 if (postedTime) *postedTime = systemTime();
802 igbProducer->queueBuffer(slot, input, qbOutput);
803 }
804};
805
806TEST_F(BLASTFrameEventHistoryTest, FrameEventHistory_Basic) {
807 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
808 sp<IGraphicBufferProducer> igbProducer;
809 ProducerFrameEventHistory history;
810 setUpProducer(adapter, igbProducer);
811
812 IGraphicBufferProducer::QueueBufferOutput qbOutput;
813 nsecs_t requestedPresentTimeA = 0;
814 nsecs_t postedTimeA = 0;
815 setUpAndQueueBuffer(igbProducer, &requestedPresentTimeA, &postedTimeA, &qbOutput, true);
816 history.applyDelta(qbOutput.frameTimestamps);
817
818 FrameEvents* events = nullptr;
819 events = history.getFrame(1);
820 ASSERT_NE(nullptr, events);
821 ASSERT_EQ(1, events->frameNumber);
822 ASSERT_EQ(requestedPresentTimeA, events->requestedPresentTime);
823 ASSERT_GE(events->postedTime, postedTimeA);
824
825 adapter.waitForCallbacks();
826
827 // queue another buffer so we query for frame event deltas
828 nsecs_t requestedPresentTimeB = 0;
829 nsecs_t postedTimeB = 0;
830 setUpAndQueueBuffer(igbProducer, &requestedPresentTimeB, &postedTimeB, &qbOutput, true);
831 history.applyDelta(qbOutput.frameTimestamps);
832 events = history.getFrame(1);
833 ASSERT_NE(nullptr, events);
834
835 // frame number, requestedPresentTime, and postTime should not have changed
836 ASSERT_EQ(1, events->frameNumber);
837 ASSERT_EQ(requestedPresentTimeA, events->requestedPresentTime);
838 ASSERT_GE(events->postedTime, postedTimeA);
839
840 ASSERT_GE(events->latchTime, postedTimeA);
841 ASSERT_GE(events->dequeueReadyTime, events->latchTime);
842 ASSERT_NE(nullptr, events->gpuCompositionDoneFence);
843 ASSERT_NE(nullptr, events->displayPresentFence);
844 ASSERT_NE(nullptr, events->releaseFence);
845
846 // we should also have gotten the initial values for the next frame
847 events = history.getFrame(2);
848 ASSERT_NE(nullptr, events);
849 ASSERT_EQ(2, events->frameNumber);
850 ASSERT_EQ(requestedPresentTimeB, events->requestedPresentTime);
851 ASSERT_GE(events->postedTime, postedTimeB);
Valerie Hau78491e92020-04-15 13:10:56 -0700852
853 // wait for any callbacks that have not been received
854 adapter.waitForCallbacks();
Valerie Hau871d6352020-01-29 08:44:02 -0800855}
Valerie Hauc5011f92019-10-11 09:52:07 -0700856} // namespace android