blob: d69b7c38a3d92c99ed0cace590dad0bf91fb5aee [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) {
Vishnu Nairdab94092020-09-29 16:09:04 -070048 mBlastBufferQueueAdapter = new BLASTBufferQueue("TestBLASTBufferQueue", sc, width, height);
Valerie Hauc5011f92019-10-11 09:52:07 -070049 }
50
51 void update(const sp<SurfaceControl>& sc, int width, int height) {
52 mBlastBufferQueueAdapter->update(sc, width, height);
53 }
54
55 void setNextTransaction(Transaction* next) {
56 mBlastBufferQueueAdapter->setNextTransaction(next);
57 }
58
Vishnu Nairea0de002020-11-17 17:42:37 -080059 int getWidth() { return mBlastBufferQueueAdapter->mSize.width; }
Valerie Hauda3446e2019-10-14 15:49:22 -070060
Vishnu Nairea0de002020-11-17 17:42:37 -080061 int getHeight() { return mBlastBufferQueueAdapter->mSize.height; }
Valerie Hauda3446e2019-10-14 15:49:22 -070062
Valerie Hauc5011f92019-10-11 09:52:07 -070063 Transaction* getNextTransaction() { return mBlastBufferQueueAdapter->mNextTransaction; }
Valerie Hauda3446e2019-10-14 15:49:22 -070064
65 sp<IGraphicBufferProducer> getIGraphicBufferProducer() {
66 return mBlastBufferQueueAdapter->getIGraphicBufferProducer();
67 }
68
Valerie Hauc5011f92019-10-11 09:52:07 -070069 const sp<SurfaceControl> getSurfaceControl() {
70 return mBlastBufferQueueAdapter->mSurfaceControl;
71 }
72
Valerie Haud3b90d22019-11-06 09:37:31 -080073 void waitForCallbacks() {
Valerie Hauda3446e2019-10-14 15:49:22 -070074 std::unique_lock lock{mBlastBufferQueueAdapter->mMutex};
Valerie Haua32c5522019-12-09 10:11:08 -080075 while (mBlastBufferQueueAdapter->mSubmitted.size() > 0) {
Valerie Haud3b90d22019-11-06 09:37:31 -080076 mBlastBufferQueueAdapter->mCallbackCV.wait(lock);
77 }
Valerie Hauda3446e2019-10-14 15:49:22 -070078 }
79
Valerie Hauc5011f92019-10-11 09:52:07 -070080private:
81 sp<BLASTBufferQueue> mBlastBufferQueueAdapter;
82};
83
84class BLASTBufferQueueTest : public ::testing::Test {
85public:
86protected:
87 BLASTBufferQueueTest() {
88 const ::testing::TestInfo* const testInfo =
89 ::testing::UnitTest::GetInstance()->current_test_info();
90 ALOGV("Begin test: %s.%s", testInfo->test_case_name(), testInfo->name());
91 }
92
93 ~BLASTBufferQueueTest() {
94 const ::testing::TestInfo* const testInfo =
95 ::testing::UnitTest::GetInstance()->current_test_info();
96 ALOGV("End test: %s.%s", testInfo->test_case_name(), testInfo->name());
97 }
98
99 void SetUp() {
Valerie Hauda3446e2019-10-14 15:49:22 -0700100 mComposer = ComposerService::getComposerService();
Valerie Hauc5011f92019-10-11 09:52:07 -0700101 mClient = new SurfaceComposerClient();
Valerie Hauda3446e2019-10-14 15:49:22 -0700102 mDisplayToken = mClient->getInternalDisplayToken();
103 ASSERT_NE(nullptr, mDisplayToken.get());
104 Transaction t;
105 t.setDisplayLayerStack(mDisplayToken, 0);
106 t.apply();
107 t.clear();
108
Dominik Laskowski3cb3d4e2019-11-21 11:14:45 -0800109 DisplayConfig config;
110 ASSERT_EQ(NO_ERROR, SurfaceComposerClient::getActiveDisplayConfig(mDisplayToken, &config));
111 const ui::Size& resolution = config.resolution;
112 mDisplayWidth = resolution.getWidth();
113 mDisplayHeight = resolution.getHeight();
Valerie Hauda3446e2019-10-14 15:49:22 -0700114
115 mSurfaceControl = mClient->createSurface(String8("TestSurface"), mDisplayWidth,
116 mDisplayHeight, PIXEL_FORMAT_RGBA_8888,
117 ISurfaceComposerClient::eFXSurfaceBufferState,
118 /*parent*/ nullptr);
119 t.setLayerStack(mSurfaceControl, 0)
120 .setLayer(mSurfaceControl, std::numeric_limits<int32_t>::max())
Dominik Laskowski3cb3d4e2019-11-21 11:14:45 -0800121 .setFrame(mSurfaceControl, Rect(resolution))
Valerie Hauda3446e2019-10-14 15:49:22 -0700122 .show(mSurfaceControl)
123 .setDataspace(mSurfaceControl, ui::Dataspace::V0_SRGB)
124 .apply();
chaviwd2432892020-07-24 17:42:39 -0700125
126 mCaptureArgs.displayToken = mDisplayToken;
arthurhung6fa58b72020-11-05 11:56:00 +0800127 mCaptureArgs.dataspace = ui::Dataspace::V0_SRGB;
Valerie Hauda3446e2019-10-14 15:49:22 -0700128 }
129
Valerie Haud3b90d22019-11-06 09:37:31 -0800130 void setUpProducer(BLASTBufferQueueHelper adapter, sp<IGraphicBufferProducer>& producer) {
131 auto igbProducer = adapter.getIGraphicBufferProducer();
132 ASSERT_NE(nullptr, igbProducer.get());
Valerie Hauc78c43a2020-01-09 17:34:14 -0800133 ASSERT_EQ(NO_ERROR, igbProducer->setMaxDequeuedBufferCount(2));
Valerie Haud3b90d22019-11-06 09:37:31 -0800134 IGraphicBufferProducer::QueueBufferOutput qbOutput;
135 ASSERT_EQ(NO_ERROR,
Peiyong Lind8460c82020-07-28 16:04:22 -0700136 igbProducer->connect(new StubProducerListener, NATIVE_WINDOW_API_CPU, false,
Valerie Haud3b90d22019-11-06 09:37:31 -0800137 &qbOutput));
Dominik Laskowski718f9602019-11-09 20:01:35 -0800138 ASSERT_NE(ui::Transform::ROT_INVALID, qbOutput.transformHint);
Valerie Haud3b90d22019-11-06 09:37:31 -0800139 producer = igbProducer;
140 }
141
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800142 void fillBuffer(uint32_t* bufData, Rect rect, uint32_t stride, uint8_t r, uint8_t g,
143 uint8_t b) {
144 for (uint32_t row = rect.top; row < rect.bottom; row++) {
145 for (uint32_t col = rect.left; col < rect.right; col++) {
Valerie Hauda3446e2019-10-14 15:49:22 -0700146 uint8_t* pixel = (uint8_t*)(bufData + (row * stride) + col);
147 *pixel = r;
148 *(pixel + 1) = g;
149 *(pixel + 2) = b;
150 *(pixel + 3) = 255;
151 }
152 }
153 }
154
Valerie Hau5977fc82019-12-05 15:56:39 -0800155 void fillQuadrants(sp<GraphicBuffer>& buf) {
156 const auto bufWidth = buf->getWidth();
157 const auto bufHeight = buf->getHeight();
158 uint32_t* bufData;
159 buf->lock(static_cast<uint32_t>(GraphicBuffer::USAGE_SW_WRITE_OFTEN),
160 reinterpret_cast<void**>(&bufData));
161 fillBuffer(bufData, Rect(0, 0, bufWidth / 2, bufHeight / 2), buf->getStride(), 0, 0, 0);
162 fillBuffer(bufData, Rect(bufWidth / 2, 0, bufWidth, bufHeight / 2), buf->getStride(), 255,
163 0, 0);
164 fillBuffer(bufData, Rect(bufWidth / 2, bufHeight / 2, bufWidth, bufHeight),
165 buf->getStride(), 0, 255, 0);
166 fillBuffer(bufData, Rect(0, bufHeight / 2, bufWidth / 2, bufHeight), buf->getStride(), 0, 0,
167 255);
168 buf->unlock();
169 }
170
171 void checkScreenCapture(uint8_t r, uint8_t g, uint8_t b, Rect region, int32_t border = 0,
172 bool outsideRegion = false) {
chaviwd2432892020-07-24 17:42:39 -0700173 sp<GraphicBuffer>& captureBuf = mCaptureResults.buffer;
Valerie Hau5977fc82019-12-05 15:56:39 -0800174 const auto epsilon = 3;
chaviwd2432892020-07-24 17:42:39 -0700175 const auto width = captureBuf->getWidth();
176 const auto height = captureBuf->getHeight();
177 const auto stride = captureBuf->getStride();
Valerie Hauda3446e2019-10-14 15:49:22 -0700178
179 uint32_t* bufData;
chaviwd2432892020-07-24 17:42:39 -0700180 captureBuf->lock(static_cast<uint32_t>(GraphicBuffer::USAGE_SW_READ_OFTEN),
181 reinterpret_cast<void**>(&bufData));
Valerie Hauda3446e2019-10-14 15:49:22 -0700182
183 for (uint32_t row = 0; row < height; row++) {
184 for (uint32_t col = 0; col < width; col++) {
185 uint8_t* pixel = (uint8_t*)(bufData + (row * stride) + col);
arthurhung6fa58b72020-11-05 11:56:00 +0800186 ASSERT_NE(nullptr, pixel);
Valerie Hau5977fc82019-12-05 15:56:39 -0800187 bool inRegion;
188 if (!outsideRegion) {
189 inRegion = row >= region.top + border && row < region.bottom - border &&
190 col >= region.left + border && col < region.right - border;
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800191 } else {
Valerie Hau5977fc82019-12-05 15:56:39 -0800192 inRegion = row >= region.top - border && row < region.bottom + border &&
193 col >= region.left - border && col < region.right + border;
194 }
195 if (!outsideRegion && inRegion) {
196 EXPECT_GE(epsilon, abs(r - *(pixel)));
197 EXPECT_GE(epsilon, abs(g - *(pixel + 1)));
198 EXPECT_GE(epsilon, abs(b - *(pixel + 2)));
199 } else if (outsideRegion && !inRegion) {
200 EXPECT_GE(epsilon, abs(r - *(pixel)));
201 EXPECT_GE(epsilon, abs(g - *(pixel + 1)));
202 EXPECT_GE(epsilon, abs(b - *(pixel + 2)));
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800203 }
Valerie Hauda3446e2019-10-14 15:49:22 -0700204 }
205 }
chaviwd2432892020-07-24 17:42:39 -0700206 captureBuf->unlock();
Valerie Hauda3446e2019-10-14 15:49:22 -0700207 ASSERT_EQ(false, ::testing::Test::HasFailure());
Valerie Hauc5011f92019-10-11 09:52:07 -0700208 }
209
chaviw8ffc7b82020-08-18 11:25:37 -0700210 static status_t captureDisplay(DisplayCaptureArgs& captureArgs,
211 ScreenCaptureResults& captureResults) {
212 const auto sf = ComposerService::getComposerService();
213 SurfaceComposerClient::Transaction().apply(true);
214
215 const sp<SyncScreenCaptureListener> captureListener = new SyncScreenCaptureListener();
216 status_t status = sf->captureDisplay(captureArgs, captureListener);
217 if (status != NO_ERROR) {
218 return status;
219 }
220 captureResults = captureListener->waitForResults();
221 return captureResults.result;
222 }
223
Vishnu Nair277142c2021-01-05 18:35:29 -0800224 void queueBuffer(sp<IGraphicBufferProducer> igbp, uint8_t r, uint8_t g, uint8_t b,
225 nsecs_t presentTimeDelay) {
226 int slot;
227 sp<Fence> fence;
228 sp<GraphicBuffer> buf;
229 auto ret = igbp->dequeueBuffer(&slot, &fence, mDisplayWidth, mDisplayHeight,
230 PIXEL_FORMAT_RGBA_8888, GRALLOC_USAGE_SW_WRITE_OFTEN,
231 nullptr, nullptr);
232 ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, ret);
233 ASSERT_EQ(OK, igbp->requestBuffer(slot, &buf));
234
235 uint32_t* bufData;
236 buf->lock(static_cast<uint32_t>(GraphicBuffer::USAGE_SW_WRITE_OFTEN),
237 reinterpret_cast<void**>(&bufData));
238 fillBuffer(bufData, Rect(buf->getWidth(), buf->getHeight() / 2), buf->getStride(), r, g, b);
239 buf->unlock();
240
241 IGraphicBufferProducer::QueueBufferOutput qbOutput;
242 nsecs_t timestampNanos = systemTime() + presentTimeDelay;
243 IGraphicBufferProducer::QueueBufferInput input(timestampNanos, false, HAL_DATASPACE_UNKNOWN,
244 Rect(mDisplayWidth, mDisplayHeight / 2),
245 NATIVE_WINDOW_SCALING_MODE_FREEZE, 0,
246 Fence::NO_FENCE);
247 igbp->queueBuffer(slot, input, &qbOutput);
248 }
249
Valerie Hauc5011f92019-10-11 09:52:07 -0700250 sp<SurfaceComposerClient> mClient;
Valerie Hauda3446e2019-10-14 15:49:22 -0700251 sp<ISurfaceComposer> mComposer;
252
253 sp<IBinder> mDisplayToken;
254
Valerie Hauc5011f92019-10-11 09:52:07 -0700255 sp<SurfaceControl> mSurfaceControl;
Valerie Hauda3446e2019-10-14 15:49:22 -0700256
257 uint32_t mDisplayWidth;
258 uint32_t mDisplayHeight;
chaviwd2432892020-07-24 17:42:39 -0700259
260 DisplayCaptureArgs mCaptureArgs;
261 ScreenCaptureResults mCaptureResults;
Valerie Hauc5011f92019-10-11 09:52:07 -0700262};
263
264TEST_F(BLASTBufferQueueTest, CreateBLASTBufferQueue) {
265 // create BLASTBufferQueue adapter associated with this surface
Valerie Hauda3446e2019-10-14 15:49:22 -0700266 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
Valerie Hauc5011f92019-10-11 09:52:07 -0700267 ASSERT_EQ(mSurfaceControl, adapter.getSurfaceControl());
Valerie Hauda3446e2019-10-14 15:49:22 -0700268 ASSERT_EQ(mDisplayWidth, adapter.getWidth());
269 ASSERT_EQ(mDisplayHeight, adapter.getHeight());
Valerie Hauc5011f92019-10-11 09:52:07 -0700270 ASSERT_EQ(nullptr, adapter.getNextTransaction());
271}
272
273TEST_F(BLASTBufferQueueTest, Update) {
Valerie Hauda3446e2019-10-14 15:49:22 -0700274 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
Valerie Hauc5011f92019-10-11 09:52:07 -0700275 sp<SurfaceControl> updateSurface =
Valerie Hauda3446e2019-10-14 15:49:22 -0700276 mClient->createSurface(String8("UpdateTest"), mDisplayWidth / 2, mDisplayHeight / 2,
277 PIXEL_FORMAT_RGBA_8888);
278 adapter.update(updateSurface, mDisplayWidth / 2, mDisplayHeight / 2);
Valerie Hauc5011f92019-10-11 09:52:07 -0700279 ASSERT_EQ(updateSurface, adapter.getSurfaceControl());
Vishnu Nairea0de002020-11-17 17:42:37 -0800280 sp<IGraphicBufferProducer> igbProducer;
281 setUpProducer(adapter, igbProducer);
282
283 int32_t width;
284 igbProducer->query(NATIVE_WINDOW_WIDTH, &width);
285 ASSERT_EQ(mDisplayWidth / 2, width);
286 int32_t height;
287 igbProducer->query(NATIVE_WINDOW_HEIGHT, &height);
288 ASSERT_EQ(mDisplayHeight / 2, height);
Valerie Hauc5011f92019-10-11 09:52:07 -0700289}
290
291TEST_F(BLASTBufferQueueTest, SetNextTransaction) {
Valerie Hauda3446e2019-10-14 15:49:22 -0700292 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
Valerie Hauc5011f92019-10-11 09:52:07 -0700293 Transaction next;
294 adapter.setNextTransaction(&next);
295 ASSERT_EQ(&next, adapter.getNextTransaction());
296}
Valerie Hauda3446e2019-10-14 15:49:22 -0700297
Valerie Haubf29e042020-02-06 11:40:38 -0800298TEST_F(BLASTBufferQueueTest, DISABLED_onFrameAvailable_ApplyDesiredPresentTime) {
Valerie Hau181abd32020-01-27 14:18:28 -0800299 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
300 sp<IGraphicBufferProducer> igbProducer;
301 setUpProducer(adapter, igbProducer);
302
303 int slot;
304 sp<Fence> fence;
305 sp<GraphicBuffer> buf;
306 auto ret = igbProducer->dequeueBuffer(&slot, &fence, mDisplayWidth, mDisplayHeight,
307 PIXEL_FORMAT_RGBA_8888, GRALLOC_USAGE_SW_WRITE_OFTEN,
308 nullptr, nullptr);
309 ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, ret);
310 ASSERT_EQ(OK, igbProducer->requestBuffer(slot, &buf));
311
312 nsecs_t desiredPresentTime = systemTime() + nsecs_t(5 * 1e8);
313 IGraphicBufferProducer::QueueBufferOutput qbOutput;
314 IGraphicBufferProducer::QueueBufferInput input(desiredPresentTime, false, HAL_DATASPACE_UNKNOWN,
315 Rect(mDisplayWidth, mDisplayHeight),
316 NATIVE_WINDOW_SCALING_MODE_FREEZE, 0,
317 Fence::NO_FENCE);
318 igbProducer->queueBuffer(slot, input, &qbOutput);
319 ASSERT_NE(ui::Transform::ROT_INVALID, qbOutput.transformHint);
320
321 adapter.waitForCallbacks();
322 ASSERT_GE(systemTime(), desiredPresentTime);
323}
324
Valerie Hauda3446e2019-10-14 15:49:22 -0700325TEST_F(BLASTBufferQueueTest, onFrameAvailable_Apply) {
326 uint8_t r = 255;
327 uint8_t g = 0;
328 uint8_t b = 0;
329
330 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
Valerie Haud3b90d22019-11-06 09:37:31 -0800331 sp<IGraphicBufferProducer> igbProducer;
332 setUpProducer(adapter, igbProducer);
Valerie Hauda3446e2019-10-14 15:49:22 -0700333
334 int slot;
335 sp<Fence> fence;
336 sp<GraphicBuffer> buf;
337 auto ret = igbProducer->dequeueBuffer(&slot, &fence, mDisplayWidth, mDisplayHeight,
338 PIXEL_FORMAT_RGBA_8888, GRALLOC_USAGE_SW_WRITE_OFTEN,
339 nullptr, nullptr);
340 ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, ret);
341 ASSERT_EQ(OK, igbProducer->requestBuffer(slot, &buf));
342
343 uint32_t* bufData;
344 buf->lock(static_cast<uint32_t>(GraphicBuffer::USAGE_SW_WRITE_OFTEN),
345 reinterpret_cast<void**>(&bufData));
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800346 fillBuffer(bufData, Rect(buf->getWidth(), buf->getHeight()), buf->getStride(), r, g, b);
Valerie Hauda3446e2019-10-14 15:49:22 -0700347 buf->unlock();
348
Valerie Haud3b90d22019-11-06 09:37:31 -0800349 IGraphicBufferProducer::QueueBufferOutput qbOutput;
Valerie Hauda3446e2019-10-14 15:49:22 -0700350 IGraphicBufferProducer::QueueBufferInput input(systemTime(), false, HAL_DATASPACE_UNKNOWN,
351 Rect(mDisplayWidth, mDisplayHeight),
352 NATIVE_WINDOW_SCALING_MODE_FREEZE, 0,
353 Fence::NO_FENCE);
354 igbProducer->queueBuffer(slot, input, &qbOutput);
Dominik Laskowski718f9602019-11-09 20:01:35 -0800355 ASSERT_NE(ui::Transform::ROT_INVALID, qbOutput.transformHint);
Valerie Hauda3446e2019-10-14 15:49:22 -0700356
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800357 adapter.waitForCallbacks();
Valerie Hauda3446e2019-10-14 15:49:22 -0700358
359 // capture screen and verify that it is red
chaviw8ffc7b82020-08-18 11:25:37 -0700360 ASSERT_EQ(NO_ERROR, captureDisplay(mCaptureArgs, mCaptureResults));
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800361 ASSERT_NO_FATAL_FAILURE(
362 checkScreenCapture(r, g, b, {0, 0, (int32_t)mDisplayWidth, (int32_t)mDisplayHeight}));
Valerie Hauda3446e2019-10-14 15:49:22 -0700363}
Valerie Haud3b90d22019-11-06 09:37:31 -0800364
365TEST_F(BLASTBufferQueueTest, TripleBuffering) {
366 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
367 sp<IGraphicBufferProducer> igbProducer;
368 setUpProducer(adapter, igbProducer);
369
370 std::vector<std::pair<int, sp<Fence>>> allocated;
371 for (int i = 0; i < 3; i++) {
372 int slot;
373 sp<Fence> fence;
374 sp<GraphicBuffer> buf;
375 auto ret = igbProducer->dequeueBuffer(&slot, &fence, mDisplayWidth, mDisplayHeight,
376 PIXEL_FORMAT_RGBA_8888, GRALLOC_USAGE_SW_WRITE_OFTEN,
377 nullptr, nullptr);
378 ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, ret);
379 ASSERT_EQ(OK, igbProducer->requestBuffer(slot, &buf));
380 allocated.push_back({slot, fence});
381 }
382 for (int i = 0; i < allocated.size(); i++) {
383 igbProducer->cancelBuffer(allocated[i].first, allocated[i].second);
384 }
385
Valerie Haua32c5522019-12-09 10:11:08 -0800386 for (int i = 0; i < 100; i++) {
Valerie Haud3b90d22019-11-06 09:37:31 -0800387 int slot;
388 sp<Fence> fence;
389 sp<GraphicBuffer> buf;
390 auto ret = igbProducer->dequeueBuffer(&slot, &fence, mDisplayWidth, mDisplayHeight,
391 PIXEL_FORMAT_RGBA_8888, GRALLOC_USAGE_SW_WRITE_OFTEN,
392 nullptr, nullptr);
393 ASSERT_EQ(NO_ERROR, ret);
394 IGraphicBufferProducer::QueueBufferOutput qbOutput;
395 IGraphicBufferProducer::QueueBufferInput input(systemTime(), false, HAL_DATASPACE_UNKNOWN,
396 Rect(mDisplayWidth, mDisplayHeight),
397 NATIVE_WINDOW_SCALING_MODE_FREEZE, 0,
398 Fence::NO_FENCE);
399 igbProducer->queueBuffer(slot, input, &qbOutput);
400 }
401 adapter.waitForCallbacks();
402}
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800403
404TEST_F(BLASTBufferQueueTest, SetCrop_Item) {
405 uint8_t r = 255;
406 uint8_t g = 0;
407 uint8_t b = 0;
408
409 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
410 sp<IGraphicBufferProducer> igbProducer;
411 setUpProducer(adapter, igbProducer);
412 int slot;
413 sp<Fence> fence;
414 sp<GraphicBuffer> buf;
415 auto ret = igbProducer->dequeueBuffer(&slot, &fence, mDisplayWidth, mDisplayHeight,
416 PIXEL_FORMAT_RGBA_8888, GRALLOC_USAGE_SW_WRITE_OFTEN,
417 nullptr, nullptr);
418 ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, ret);
419 ASSERT_EQ(OK, igbProducer->requestBuffer(slot, &buf));
420
421 uint32_t* bufData;
422 buf->lock(static_cast<uint32_t>(GraphicBuffer::USAGE_SW_WRITE_OFTEN),
423 reinterpret_cast<void**>(&bufData));
424 fillBuffer(bufData, Rect(buf->getWidth(), buf->getHeight() / 2), buf->getStride(), r, g, b);
425 buf->unlock();
426
427 IGraphicBufferProducer::QueueBufferOutput qbOutput;
428 IGraphicBufferProducer::QueueBufferInput input(systemTime(), false, HAL_DATASPACE_UNKNOWN,
429 Rect(mDisplayWidth, mDisplayHeight / 2),
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 Hau45e4b3b2019-12-03 10:49:17 -0800434
435 adapter.waitForCallbacks();
436 // capture screen and verify that it is red
chaviw8ffc7b82020-08-18 11:25:37 -0700437 ASSERT_EQ(NO_ERROR, captureDisplay(mCaptureArgs, mCaptureResults));
chaviwd2432892020-07-24 17:42:39 -0700438
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}));
441}
442
443TEST_F(BLASTBufferQueueTest, SetCrop_ScalingModeScaleCrop) {
444 uint8_t r = 255;
445 uint8_t g = 0;
446 uint8_t b = 0;
447
448 int32_t bufferSideLength =
449 (mDisplayWidth < mDisplayHeight) ? mDisplayWidth / 2 : mDisplayHeight / 2;
450 int32_t finalCropSideLength = bufferSideLength / 2;
451
452 auto bg = mClient->createSurface(String8("BGTest"), 0, 0, PIXEL_FORMAT_RGBA_8888,
Vishnu Nairfa247b12020-02-11 08:58:26 -0800453 ISurfaceComposerClient::eFXSurfaceEffect);
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800454 ASSERT_NE(nullptr, bg.get());
455 Transaction t;
456 t.setLayerStack(bg, 0)
457 .setCrop_legacy(bg, Rect(0, 0, mDisplayWidth, mDisplayHeight))
458 .setColor(bg, half3{0, 0, 0})
459 .setLayer(bg, 0)
460 .apply();
461
462 BLASTBufferQueueHelper adapter(mSurfaceControl, bufferSideLength, bufferSideLength);
463 sp<IGraphicBufferProducer> igbProducer;
464 setUpProducer(adapter, igbProducer);
465 int slot;
466 sp<Fence> fence;
467 sp<GraphicBuffer> buf;
468 auto ret = igbProducer->dequeueBuffer(&slot, &fence, bufferSideLength, bufferSideLength,
469 PIXEL_FORMAT_RGBA_8888, GRALLOC_USAGE_SW_WRITE_OFTEN,
470 nullptr, nullptr);
471 ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, ret);
472 ASSERT_EQ(OK, igbProducer->requestBuffer(slot, &buf));
473
474 uint32_t* bufData;
475 buf->lock(static_cast<uint32_t>(GraphicBuffer::USAGE_SW_WRITE_OFTEN),
476 reinterpret_cast<void**>(&bufData));
477 fillBuffer(bufData, Rect(buf->getWidth(), buf->getHeight()), buf->getStride(), 0, 0, 0);
478 fillBuffer(bufData,
479 Rect(finalCropSideLength / 2, 0, buf->getWidth() - finalCropSideLength / 2,
480 buf->getHeight()),
481 buf->getStride(), r, g, b);
482 buf->unlock();
483
484 IGraphicBufferProducer::QueueBufferOutput qbOutput;
485 IGraphicBufferProducer::QueueBufferInput input(systemTime(), false, HAL_DATASPACE_UNKNOWN,
486 Rect(bufferSideLength, finalCropSideLength),
487 NATIVE_WINDOW_SCALING_MODE_SCALE_CROP, 0,
488 Fence::NO_FENCE);
489 igbProducer->queueBuffer(slot, input, &qbOutput);
Dominik Laskowski718f9602019-11-09 20:01:35 -0800490 ASSERT_NE(ui::Transform::ROT_INVALID, qbOutput.transformHint);
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800491
492 adapter.waitForCallbacks();
493 // capture screen and verify that it is red
chaviw8ffc7b82020-08-18 11:25:37 -0700494 ASSERT_EQ(NO_ERROR, captureDisplay(mCaptureArgs, mCaptureResults));
chaviwd2432892020-07-24 17:42:39 -0700495
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800496 ASSERT_NO_FATAL_FAILURE(
497 checkScreenCapture(r, g, b,
498 {0, 0, (int32_t)bufferSideLength, (int32_t)bufferSideLength}));
Valerie Hau5977fc82019-12-05 15:56:39 -0800499 ASSERT_NO_FATAL_FAILURE(
500 checkScreenCapture(0, 0, 0,
501 {0, 0, (int32_t)bufferSideLength, (int32_t)bufferSideLength},
502 /*border*/ 0, /*outsideRegion*/ true));
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800503}
504
Vishnu Nair89496122020-12-14 17:14:53 -0800505class TestProducerListener : public BnProducerListener {
506public:
507 sp<IGraphicBufferProducer> mIgbp;
508 TestProducerListener(const sp<IGraphicBufferProducer>& igbp) : mIgbp(igbp) {}
509 void onBufferReleased() override {
510 sp<GraphicBuffer> buffer;
511 sp<Fence> fence;
512 mIgbp->detachNextBuffer(&buffer, &fence);
513 }
514};
515
516TEST_F(BLASTBufferQueueTest, CustomProducerListener) {
517 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
518 sp<IGraphicBufferProducer> igbProducer = adapter.getIGraphicBufferProducer();
519 ASSERT_NE(nullptr, igbProducer.get());
520 ASSERT_EQ(NO_ERROR, igbProducer->setMaxDequeuedBufferCount(2));
521 IGraphicBufferProducer::QueueBufferOutput qbOutput;
522 ASSERT_EQ(NO_ERROR,
523 igbProducer->connect(new TestProducerListener(igbProducer), NATIVE_WINDOW_API_CPU,
524 false, &qbOutput));
525 ASSERT_NE(ui::Transform::ROT_INVALID, qbOutput.transformHint);
526 for (int i = 0; i < 3; i++) {
527 int slot;
528 sp<Fence> fence;
529 sp<GraphicBuffer> buf;
530 auto ret = igbProducer->dequeueBuffer(&slot, &fence, mDisplayWidth, mDisplayHeight,
531 PIXEL_FORMAT_RGBA_8888, GRALLOC_USAGE_SW_WRITE_OFTEN,
532 nullptr, nullptr);
533 ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, ret);
534 ASSERT_EQ(OK, igbProducer->requestBuffer(slot, &buf));
535 IGraphicBufferProducer::QueueBufferOutput qbOutput;
536 IGraphicBufferProducer::QueueBufferInput input(systemTime(), false, HAL_DATASPACE_UNKNOWN,
537 Rect(mDisplayWidth, mDisplayHeight),
538 NATIVE_WINDOW_SCALING_MODE_FREEZE, 0,
539 Fence::NO_FENCE);
540 igbProducer->queueBuffer(slot, input, &qbOutput);
541 }
542 adapter.waitForCallbacks();
543}
544
Vishnu Nair17dde612020-12-28 11:39:59 -0800545TEST_F(BLASTBufferQueueTest, QueryNativeWindowQueuesToWindowComposer) {
546 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
547
548 sp<android::Surface> surface = new Surface(adapter.getIGraphicBufferProducer());
549 ANativeWindow* nativeWindow = (ANativeWindow*)(surface.get());
550 int queuesToNativeWindow = 0;
551 int err = nativeWindow->query(nativeWindow, NATIVE_WINDOW_QUEUES_TO_WINDOW_COMPOSER,
552 &queuesToNativeWindow);
553 ASSERT_EQ(NO_ERROR, err);
554 ASSERT_EQ(queuesToNativeWindow, 1);
555}
556
Vishnu Nair277142c2021-01-05 18:35:29 -0800557TEST_F(BLASTBufferQueueTest, OutOfOrderTransactionTest) {
558 sp<SurfaceControl> bgSurface =
559 mClient->createSurface(String8("BGTest"), 0, 0, PIXEL_FORMAT_RGBA_8888,
560 ISurfaceComposerClient::eFXSurfaceBufferState);
561 ASSERT_NE(nullptr, bgSurface.get());
562 Transaction t;
563 t.setLayerStack(bgSurface, 0)
564 .show(bgSurface)
565 .setDataspace(bgSurface, ui::Dataspace::V0_SRGB)
566 .setFrame(bgSurface, Rect(0, 0, mDisplayWidth, mDisplayHeight))
567 .setLayer(bgSurface, std::numeric_limits<int32_t>::max() - 1)
568 .apply();
569
570 BLASTBufferQueueHelper slowAdapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
571 sp<IGraphicBufferProducer> slowIgbProducer;
572 setUpProducer(slowAdapter, slowIgbProducer);
573 nsecs_t presentTimeDelay = std::chrono::nanoseconds(500ms).count();
574 queueBuffer(slowIgbProducer, 0 /* r */, 0 /* g */, 0 /* b */, presentTimeDelay);
575
576 BLASTBufferQueueHelper fastAdapter(bgSurface, mDisplayWidth, mDisplayHeight);
577 sp<IGraphicBufferProducer> fastIgbProducer;
578 setUpProducer(fastAdapter, fastIgbProducer);
579 uint8_t r = 255;
580 uint8_t g = 0;
581 uint8_t b = 0;
582 queueBuffer(fastIgbProducer, r, g, b, 0 /* presentTimeDelay */);
583 fastAdapter.waitForCallbacks();
584
585 // capture screen and verify that it is red
586 ASSERT_EQ(NO_ERROR, captureDisplay(mCaptureArgs, mCaptureResults));
587
588 ASSERT_NO_FATAL_FAILURE(
589 checkScreenCapture(r, g, b, {0, 0, (int32_t)mDisplayWidth, (int32_t)mDisplayHeight}));
590}
591
Valerie Hau5977fc82019-12-05 15:56:39 -0800592class BLASTBufferQueueTransformTest : public BLASTBufferQueueTest {
593public:
594 void test(uint32_t tr) {
595 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
596 sp<IGraphicBufferProducer> igbProducer;
597 setUpProducer(adapter, igbProducer);
598
599 auto bufWidth = mDisplayWidth;
600 auto bufHeight = mDisplayHeight;
601 int slot;
602 sp<Fence> fence;
603 sp<GraphicBuffer> buf;
604
605 auto ret = igbProducer->dequeueBuffer(&slot, &fence, bufWidth, bufHeight,
606 PIXEL_FORMAT_RGBA_8888, GRALLOC_USAGE_SW_WRITE_OFTEN,
607 nullptr, nullptr);
608 ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, ret);
609 ASSERT_EQ(OK, igbProducer->requestBuffer(slot, &buf));
610
611 fillQuadrants(buf);
612
613 IGraphicBufferProducer::QueueBufferOutput qbOutput;
614 IGraphicBufferProducer::QueueBufferInput input(systemTime(), false, HAL_DATASPACE_UNKNOWN,
615 Rect(bufWidth, bufHeight),
Vishnu Naire1a42322020-10-02 17:42:04 -0700616 NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW,
617 tr, Fence::NO_FENCE);
Valerie Hau5977fc82019-12-05 15:56:39 -0800618 igbProducer->queueBuffer(slot, input, &qbOutput);
Dominik Laskowski718f9602019-11-09 20:01:35 -0800619 ASSERT_NE(ui::Transform::ROT_INVALID, qbOutput.transformHint);
Valerie Hau5977fc82019-12-05 15:56:39 -0800620
621 adapter.waitForCallbacks();
chaviw8ffc7b82020-08-18 11:25:37 -0700622 ASSERT_EQ(NO_ERROR, captureDisplay(mCaptureArgs, mCaptureResults));
chaviwd2432892020-07-24 17:42:39 -0700623
Valerie Hau5977fc82019-12-05 15:56:39 -0800624 switch (tr) {
625 case ui::Transform::ROT_0:
626 ASSERT_NO_FATAL_FAILURE(checkScreenCapture(0, 0, 0,
627 {0, 0, (int32_t)mDisplayWidth / 2,
628 (int32_t)mDisplayHeight / 2},
629 1));
630 ASSERT_NO_FATAL_FAILURE(
631 checkScreenCapture(255, 0, 0,
632 {(int32_t)mDisplayWidth / 2, 0, (int32_t)mDisplayWidth,
633 (int32_t)mDisplayHeight / 2},
634 1));
635 ASSERT_NO_FATAL_FAILURE(
636 checkScreenCapture(0, 255, 0,
637 {(int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight / 2,
638 (int32_t)mDisplayWidth, (int32_t)mDisplayHeight},
639 1));
640 ASSERT_NO_FATAL_FAILURE(
641 checkScreenCapture(0, 0, 255,
642 {0, (int32_t)mDisplayHeight / 2,
643 (int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight},
644 1));
645 break;
646 case ui::Transform::FLIP_H:
647 ASSERT_NO_FATAL_FAILURE(checkScreenCapture(255, 0, 0,
648 {0, 0, (int32_t)mDisplayWidth / 2,
649 (int32_t)mDisplayHeight / 2},
650 1));
651 ASSERT_NO_FATAL_FAILURE(
652 checkScreenCapture(0, 0, 0,
653 {(int32_t)mDisplayWidth / 2, 0, (int32_t)mDisplayWidth,
654 (int32_t)mDisplayHeight / 2},
655 1));
656 ASSERT_NO_FATAL_FAILURE(
657 checkScreenCapture(0, 0, 255,
658 {(int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight / 2,
659 (int32_t)mDisplayWidth, (int32_t)mDisplayHeight},
660 1));
661 ASSERT_NO_FATAL_FAILURE(
662 checkScreenCapture(0, 255, 0,
663 {0, (int32_t)mDisplayHeight / 2,
664 (int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight},
665 1));
666 break;
667 case ui::Transform::FLIP_V:
668 ASSERT_NO_FATAL_FAILURE(checkScreenCapture(0, 0, 255,
669 {0, 0, (int32_t)mDisplayWidth / 2,
670 (int32_t)mDisplayHeight / 2},
671 1));
672 ASSERT_NO_FATAL_FAILURE(
673 checkScreenCapture(0, 255, 0,
674 {(int32_t)mDisplayWidth / 2, 0, (int32_t)mDisplayWidth,
675 (int32_t)mDisplayHeight / 2},
676 1));
677 ASSERT_NO_FATAL_FAILURE(
678 checkScreenCapture(255, 0, 0,
679 {(int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight / 2,
680 (int32_t)mDisplayWidth, (int32_t)mDisplayHeight},
681 1));
682 ASSERT_NO_FATAL_FAILURE(
683 checkScreenCapture(0, 0, 0,
684 {0, (int32_t)mDisplayHeight / 2,
685 (int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight},
686 1));
687 break;
688 case ui::Transform::ROT_90:
689 ASSERT_NO_FATAL_FAILURE(checkScreenCapture(0, 0, 255,
690 {0, 0, (int32_t)mDisplayWidth / 2,
691 (int32_t)mDisplayHeight / 2},
692 1));
693 ASSERT_NO_FATAL_FAILURE(
694 checkScreenCapture(0, 0, 0,
695 {(int32_t)mDisplayWidth / 2, 0, (int32_t)mDisplayWidth,
696 (int32_t)mDisplayHeight / 2},
697 1));
698 ASSERT_NO_FATAL_FAILURE(
699 checkScreenCapture(255, 0, 0,
700 {(int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight / 2,
701 (int32_t)mDisplayWidth, (int32_t)mDisplayHeight},
702 1));
703 ASSERT_NO_FATAL_FAILURE(
704 checkScreenCapture(0, 255, 0,
705 {0, (int32_t)mDisplayHeight / 2,
706 (int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight},
707 1));
708 break;
709 case ui::Transform::ROT_180:
710 ASSERT_NO_FATAL_FAILURE(checkScreenCapture(0, 255, 0,
711 {0, 0, (int32_t)mDisplayWidth / 2,
712 (int32_t)mDisplayHeight / 2},
713 1));
714 ASSERT_NO_FATAL_FAILURE(
715 checkScreenCapture(0, 0, 255,
716 {(int32_t)mDisplayWidth / 2, 0, (int32_t)mDisplayWidth,
717 (int32_t)mDisplayHeight / 2},
718 1));
719 ASSERT_NO_FATAL_FAILURE(
720 checkScreenCapture(0, 0, 0,
721 {(int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight / 2,
722 (int32_t)mDisplayWidth, (int32_t)mDisplayHeight},
723 1));
724 ASSERT_NO_FATAL_FAILURE(
725 checkScreenCapture(255, 0, 0,
726 {0, (int32_t)mDisplayHeight / 2,
727 (int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight},
728 1));
729 break;
730 case ui::Transform::ROT_270:
731 ASSERT_NO_FATAL_FAILURE(checkScreenCapture(255, 0, 0,
732 {0, 0, (int32_t)mDisplayWidth / 2,
733 (int32_t)mDisplayHeight / 2},
734 1));
735 ASSERT_NO_FATAL_FAILURE(
736 checkScreenCapture(0, 255, 0,
737 {(int32_t)mDisplayWidth / 2, 0, (int32_t)mDisplayWidth,
738 (int32_t)mDisplayHeight / 2},
739 1));
740 ASSERT_NO_FATAL_FAILURE(
741 checkScreenCapture(0, 0, 255,
742 {(int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight / 2,
743 (int32_t)mDisplayWidth, (int32_t)mDisplayHeight},
744 1));
745 ASSERT_NO_FATAL_FAILURE(
746 checkScreenCapture(0, 0, 0,
747 {0, (int32_t)mDisplayHeight / 2,
748 (int32_t)mDisplayWidth / 2, (int32_t)mDisplayHeight},
749 1));
750 }
751 }
752};
753
754TEST_F(BLASTBufferQueueTransformTest, setTransform_ROT_0) {
755 test(ui::Transform::ROT_0);
756}
757
758TEST_F(BLASTBufferQueueTransformTest, setTransform_FLIP_H) {
759 test(ui::Transform::FLIP_H);
760}
761
762TEST_F(BLASTBufferQueueTransformTest, setTransform_FLIP_V) {
763 test(ui::Transform::FLIP_V);
764}
765
766TEST_F(BLASTBufferQueueTransformTest, setTransform_ROT_90) {
767 test(ui::Transform::ROT_90);
768}
769
770TEST_F(BLASTBufferQueueTransformTest, setTransform_ROT_180) {
771 test(ui::Transform::ROT_180);
772}
773
774TEST_F(BLASTBufferQueueTransformTest, setTransform_ROT_270) {
775 test(ui::Transform::ROT_270);
776}
Valerie Hau871d6352020-01-29 08:44:02 -0800777
778class BLASTFrameEventHistoryTest : public BLASTBufferQueueTest {
779public:
780 void setUpAndQueueBuffer(const sp<IGraphicBufferProducer>& igbProducer,
781 nsecs_t* requestedPresentTime, nsecs_t* postedTime,
782 IGraphicBufferProducer::QueueBufferOutput* qbOutput,
783 bool getFrameTimestamps) {
784 int slot;
785 sp<Fence> fence;
786 sp<GraphicBuffer> buf;
787 auto ret = igbProducer->dequeueBuffer(&slot, &fence, mDisplayWidth, mDisplayHeight,
788 PIXEL_FORMAT_RGBA_8888, GRALLOC_USAGE_SW_WRITE_OFTEN,
789 nullptr, nullptr);
790 ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, ret);
791 ASSERT_EQ(OK, igbProducer->requestBuffer(slot, &buf));
792
793 nsecs_t requestedTime = systemTime();
794 if (requestedPresentTime) *requestedPresentTime = requestedTime;
795 IGraphicBufferProducer::QueueBufferInput input(requestedTime, false, HAL_DATASPACE_UNKNOWN,
796 Rect(mDisplayWidth, mDisplayHeight),
797 NATIVE_WINDOW_SCALING_MODE_FREEZE, 0,
798 Fence::NO_FENCE, /*sticky*/ 0,
799 getFrameTimestamps);
800 if (postedTime) *postedTime = systemTime();
801 igbProducer->queueBuffer(slot, input, qbOutput);
802 }
803};
804
805TEST_F(BLASTFrameEventHistoryTest, FrameEventHistory_Basic) {
806 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
807 sp<IGraphicBufferProducer> igbProducer;
808 ProducerFrameEventHistory history;
809 setUpProducer(adapter, igbProducer);
810
811 IGraphicBufferProducer::QueueBufferOutput qbOutput;
812 nsecs_t requestedPresentTimeA = 0;
813 nsecs_t postedTimeA = 0;
814 setUpAndQueueBuffer(igbProducer, &requestedPresentTimeA, &postedTimeA, &qbOutput, true);
815 history.applyDelta(qbOutput.frameTimestamps);
816
817 FrameEvents* events = nullptr;
818 events = history.getFrame(1);
819 ASSERT_NE(nullptr, events);
820 ASSERT_EQ(1, events->frameNumber);
821 ASSERT_EQ(requestedPresentTimeA, events->requestedPresentTime);
822 ASSERT_GE(events->postedTime, postedTimeA);
823
824 adapter.waitForCallbacks();
825
826 // queue another buffer so we query for frame event deltas
827 nsecs_t requestedPresentTimeB = 0;
828 nsecs_t postedTimeB = 0;
829 setUpAndQueueBuffer(igbProducer, &requestedPresentTimeB, &postedTimeB, &qbOutput, true);
830 history.applyDelta(qbOutput.frameTimestamps);
831 events = history.getFrame(1);
832 ASSERT_NE(nullptr, events);
833
834 // frame number, requestedPresentTime, and postTime should not have changed
835 ASSERT_EQ(1, events->frameNumber);
836 ASSERT_EQ(requestedPresentTimeA, events->requestedPresentTime);
837 ASSERT_GE(events->postedTime, postedTimeA);
838
839 ASSERT_GE(events->latchTime, postedTimeA);
840 ASSERT_GE(events->dequeueReadyTime, events->latchTime);
841 ASSERT_NE(nullptr, events->gpuCompositionDoneFence);
842 ASSERT_NE(nullptr, events->displayPresentFence);
843 ASSERT_NE(nullptr, events->releaseFence);
844
845 // we should also have gotten the initial values for the next frame
846 events = history.getFrame(2);
847 ASSERT_NE(nullptr, events);
848 ASSERT_EQ(2, events->frameNumber);
849 ASSERT_EQ(requestedPresentTimeB, events->requestedPresentTime);
850 ASSERT_GE(events->postedTime, postedTimeB);
Valerie Hau78491e92020-04-15 13:10:56 -0700851
852 // wait for any callbacks that have not been received
853 adapter.waitForCallbacks();
Valerie Hau871d6352020-01-29 08:44:02 -0800854}
Valerie Hauc5011f92019-10-11 09:52:07 -0700855} // namespace android