blob: 6ecdae5fd7bf4de8d41e4c47ce94116faf49a50d [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 Hauda3446e2019-10-14 15:49:22 -070024#include <gui/IGraphicBufferProducer.h>
25#include <gui/IProducerListener.h>
Valerie Hauc5011f92019-10-11 09:52:07 -070026#include <gui/SurfaceComposerClient.h>
Valerie Hauda3446e2019-10-14 15:49:22 -070027#include <private/gui/ComposerService.h>
28#include <ui/DisplayInfo.h>
Valerie Hauc5011f92019-10-11 09:52:07 -070029#include <ui/GraphicBuffer.h>
Valerie Hauda3446e2019-10-14 15:49:22 -070030#include <ui/GraphicTypes.h>
Valerie Hau8cee3f92019-11-06 10:06:28 -080031#include <ui/Transform.h>
Valerie Hauc5011f92019-10-11 09:52:07 -070032
33#include <gtest/gtest.h>
34
35using namespace std::chrono_literals;
36
37namespace android {
38
Valerie Hauc5011f92019-10-11 09:52:07 -070039using Transaction = SurfaceComposerClient::Transaction;
Valerie Hauda3446e2019-10-14 15:49:22 -070040using android::hardware::graphics::common::V1_2::BufferUsage;
Valerie Hauc5011f92019-10-11 09:52:07 -070041
42class BLASTBufferQueueHelper {
43public:
44 BLASTBufferQueueHelper(const sp<SurfaceControl>& sc, int width, int height) {
45 mBlastBufferQueueAdapter = new BLASTBufferQueue(sc, width, height);
46 }
47
48 void update(const sp<SurfaceControl>& sc, int width, int height) {
49 mBlastBufferQueueAdapter->update(sc, width, height);
50 }
51
52 void setNextTransaction(Transaction* next) {
53 mBlastBufferQueueAdapter->setNextTransaction(next);
54 }
55
56 int getWidth() { return mBlastBufferQueueAdapter->mWidth; }
Valerie Hauda3446e2019-10-14 15:49:22 -070057
Valerie Hauc5011f92019-10-11 09:52:07 -070058 int getHeight() { return mBlastBufferQueueAdapter->mHeight; }
Valerie Hauda3446e2019-10-14 15:49:22 -070059
Valerie Hauc5011f92019-10-11 09:52:07 -070060 Transaction* getNextTransaction() { return mBlastBufferQueueAdapter->mNextTransaction; }
Valerie Hauda3446e2019-10-14 15:49:22 -070061
62 sp<IGraphicBufferProducer> getIGraphicBufferProducer() {
63 return mBlastBufferQueueAdapter->getIGraphicBufferProducer();
64 }
65
Valerie Hauc5011f92019-10-11 09:52:07 -070066 const sp<SurfaceControl> getSurfaceControl() {
67 return mBlastBufferQueueAdapter->mSurfaceControl;
68 }
69
Valerie Haud3b90d22019-11-06 09:37:31 -080070 void waitForCallbacks() {
Valerie Hauda3446e2019-10-14 15:49:22 -070071 std::unique_lock lock{mBlastBufferQueueAdapter->mMutex};
Valerie Haud3b90d22019-11-06 09:37:31 -080072 while (mBlastBufferQueueAdapter->mPendingCallbacks > 0) {
73 mBlastBufferQueueAdapter->mCallbackCV.wait(lock);
74 }
Valerie Hauda3446e2019-10-14 15:49:22 -070075 }
76
Valerie Hauc5011f92019-10-11 09:52:07 -070077private:
78 sp<BLASTBufferQueue> mBlastBufferQueueAdapter;
79};
80
81class BLASTBufferQueueTest : public ::testing::Test {
82public:
83protected:
84 BLASTBufferQueueTest() {
85 const ::testing::TestInfo* const testInfo =
86 ::testing::UnitTest::GetInstance()->current_test_info();
87 ALOGV("Begin test: %s.%s", testInfo->test_case_name(), testInfo->name());
88 }
89
90 ~BLASTBufferQueueTest() {
91 const ::testing::TestInfo* const testInfo =
92 ::testing::UnitTest::GetInstance()->current_test_info();
93 ALOGV("End test: %s.%s", testInfo->test_case_name(), testInfo->name());
94 }
95
96 void SetUp() {
Valerie Hauda3446e2019-10-14 15:49:22 -070097 mComposer = ComposerService::getComposerService();
Valerie Hauc5011f92019-10-11 09:52:07 -070098 mClient = new SurfaceComposerClient();
Valerie Hauda3446e2019-10-14 15:49:22 -070099 mDisplayToken = mClient->getInternalDisplayToken();
100 ASSERT_NE(nullptr, mDisplayToken.get());
101 Transaction t;
102 t.setDisplayLayerStack(mDisplayToken, 0);
103 t.apply();
104 t.clear();
105
106 DisplayInfo info;
107 ASSERT_EQ(NO_ERROR, SurfaceComposerClient::getDisplayInfo(mDisplayToken, &info));
108 mDisplayWidth = info.w;
109 mDisplayHeight = info.h;
110
111 mSurfaceControl = mClient->createSurface(String8("TestSurface"), mDisplayWidth,
112 mDisplayHeight, PIXEL_FORMAT_RGBA_8888,
113 ISurfaceComposerClient::eFXSurfaceBufferState,
114 /*parent*/ nullptr);
115 t.setLayerStack(mSurfaceControl, 0)
116 .setLayer(mSurfaceControl, std::numeric_limits<int32_t>::max())
117 .setFrame(mSurfaceControl, Rect(0, 0, mDisplayWidth, mDisplayHeight))
118 .show(mSurfaceControl)
119 .setDataspace(mSurfaceControl, ui::Dataspace::V0_SRGB)
120 .apply();
121 }
122
Valerie Haud3b90d22019-11-06 09:37:31 -0800123 void setUpProducer(BLASTBufferQueueHelper adapter, sp<IGraphicBufferProducer>& producer) {
124 auto igbProducer = adapter.getIGraphicBufferProducer();
125 ASSERT_NE(nullptr, igbProducer.get());
126 IGraphicBufferProducer::QueueBufferOutput qbOutput;
127 ASSERT_EQ(NO_ERROR,
128 igbProducer->connect(new DummyProducerListener, NATIVE_WINDOW_API_CPU, false,
129 &qbOutput));
130 ASSERT_NE(ui::Transform::orientation_flags::ROT_INVALID, qbOutput.transformHint);
131 producer = igbProducer;
132 }
133
Valerie Hauda3446e2019-10-14 15:49:22 -0700134 void fillBuffer(uint32_t* bufData, uint32_t width, uint32_t height, uint32_t stride, uint8_t r,
135 uint8_t g, uint8_t b) {
136 for (uint32_t row = 0; row < height; row++) {
137 for (uint32_t col = 0; col < width; col++) {
138 uint8_t* pixel = (uint8_t*)(bufData + (row * stride) + col);
139 *pixel = r;
140 *(pixel + 1) = g;
141 *(pixel + 2) = b;
142 *(pixel + 3) = 255;
143 }
144 }
145 }
146
147 void checkScreenCapture(uint8_t r, uint8_t g, uint8_t b) {
148 const auto width = mScreenCaptureBuf->getWidth();
149 const auto height = mScreenCaptureBuf->getHeight();
150 const auto stride = mScreenCaptureBuf->getStride();
151
152 uint32_t* bufData;
153 mScreenCaptureBuf->lock(static_cast<uint32_t>(GraphicBuffer::USAGE_SW_READ_OFTEN),
154 reinterpret_cast<void**>(&bufData));
155
156 for (uint32_t row = 0; row < height; row++) {
157 for (uint32_t col = 0; col < width; col++) {
158 uint8_t* pixel = (uint8_t*)(bufData + (row * stride) + col);
159 EXPECT_EQ(r, *(pixel));
160 EXPECT_EQ(g, *(pixel + 1));
161 EXPECT_EQ(b, *(pixel + 2));
162 }
163 }
164 mScreenCaptureBuf->unlock();
165 ASSERT_EQ(false, ::testing::Test::HasFailure());
Valerie Hauc5011f92019-10-11 09:52:07 -0700166 }
167
168 sp<SurfaceComposerClient> mClient;
Valerie Hauda3446e2019-10-14 15:49:22 -0700169 sp<ISurfaceComposer> mComposer;
170
171 sp<IBinder> mDisplayToken;
172
Valerie Hauc5011f92019-10-11 09:52:07 -0700173 sp<SurfaceControl> mSurfaceControl;
Valerie Hauda3446e2019-10-14 15:49:22 -0700174 sp<GraphicBuffer> mScreenCaptureBuf;
175
176 uint32_t mDisplayWidth;
177 uint32_t mDisplayHeight;
Valerie Hauc5011f92019-10-11 09:52:07 -0700178};
179
180TEST_F(BLASTBufferQueueTest, CreateBLASTBufferQueue) {
181 // create BLASTBufferQueue adapter associated with this surface
Valerie Hauda3446e2019-10-14 15:49:22 -0700182 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
Valerie Hauc5011f92019-10-11 09:52:07 -0700183 ASSERT_EQ(mSurfaceControl, adapter.getSurfaceControl());
Valerie Hauda3446e2019-10-14 15:49:22 -0700184 ASSERT_EQ(mDisplayWidth, adapter.getWidth());
185 ASSERT_EQ(mDisplayHeight, adapter.getHeight());
Valerie Hauc5011f92019-10-11 09:52:07 -0700186 ASSERT_EQ(nullptr, adapter.getNextTransaction());
187}
188
189TEST_F(BLASTBufferQueueTest, Update) {
Valerie Hauda3446e2019-10-14 15:49:22 -0700190 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
Valerie Hauc5011f92019-10-11 09:52:07 -0700191 sp<SurfaceControl> updateSurface =
Valerie Hauda3446e2019-10-14 15:49:22 -0700192 mClient->createSurface(String8("UpdateTest"), mDisplayWidth / 2, mDisplayHeight / 2,
193 PIXEL_FORMAT_RGBA_8888);
194 adapter.update(updateSurface, mDisplayWidth / 2, mDisplayHeight / 2);
Valerie Hauc5011f92019-10-11 09:52:07 -0700195 ASSERT_EQ(updateSurface, adapter.getSurfaceControl());
Valerie Hauda3446e2019-10-14 15:49:22 -0700196 ASSERT_EQ(mDisplayWidth / 2, adapter.getWidth());
197 ASSERT_EQ(mDisplayHeight / 2, adapter.getHeight());
Valerie Hauc5011f92019-10-11 09:52:07 -0700198}
199
200TEST_F(BLASTBufferQueueTest, SetNextTransaction) {
Valerie Hauda3446e2019-10-14 15:49:22 -0700201 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
Valerie Hauc5011f92019-10-11 09:52:07 -0700202 Transaction next;
203 adapter.setNextTransaction(&next);
204 ASSERT_EQ(&next, adapter.getNextTransaction());
205}
Valerie Hauda3446e2019-10-14 15:49:22 -0700206
207TEST_F(BLASTBufferQueueTest, onFrameAvailable_Apply) {
208 uint8_t r = 255;
209 uint8_t g = 0;
210 uint8_t b = 0;
211
212 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
Valerie Haud3b90d22019-11-06 09:37:31 -0800213 sp<IGraphicBufferProducer> igbProducer;
214 setUpProducer(adapter, igbProducer);
Valerie Hauda3446e2019-10-14 15:49:22 -0700215
216 int slot;
217 sp<Fence> fence;
218 sp<GraphicBuffer> buf;
219 auto ret = igbProducer->dequeueBuffer(&slot, &fence, mDisplayWidth, mDisplayHeight,
220 PIXEL_FORMAT_RGBA_8888, GRALLOC_USAGE_SW_WRITE_OFTEN,
221 nullptr, nullptr);
222 ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, ret);
223 ASSERT_EQ(OK, igbProducer->requestBuffer(slot, &buf));
224
225 uint32_t* bufData;
226 buf->lock(static_cast<uint32_t>(GraphicBuffer::USAGE_SW_WRITE_OFTEN),
227 reinterpret_cast<void**>(&bufData));
228 fillBuffer(bufData, buf->getWidth(), buf->getHeight(), buf->getStride(), r, g, b);
229 buf->unlock();
230
Valerie Haud3b90d22019-11-06 09:37:31 -0800231 IGraphicBufferProducer::QueueBufferOutput qbOutput;
Valerie Hauda3446e2019-10-14 15:49:22 -0700232 IGraphicBufferProducer::QueueBufferInput input(systemTime(), false, HAL_DATASPACE_UNKNOWN,
233 Rect(mDisplayWidth, mDisplayHeight),
234 NATIVE_WINDOW_SCALING_MODE_FREEZE, 0,
235 Fence::NO_FENCE);
236 igbProducer->queueBuffer(slot, input, &qbOutput);
Valerie Hau8cee3f92019-11-06 10:06:28 -0800237 ASSERT_NE(ui::Transform::orientation_flags::ROT_INVALID, qbOutput.transformHint);
Valerie Hauda3446e2019-10-14 15:49:22 -0700238
Valerie Haud3b90d22019-11-06 09:37:31 -0800239 sleep(1);
Valerie Hauda3446e2019-10-14 15:49:22 -0700240
241 // capture screen and verify that it is red
242 bool capturedSecureLayers;
243 ASSERT_EQ(NO_ERROR,
244 mComposer->captureScreen(mDisplayToken, &mScreenCaptureBuf, capturedSecureLayers,
245 ui::Dataspace::V0_SRGB, ui::PixelFormat::RGBA_8888, Rect(),
246 mDisplayWidth, mDisplayHeight,
247 /*useIdentityTransform*/ false));
248 ASSERT_NO_FATAL_FAILURE(checkScreenCapture(r, g, b));
249}
Valerie Haud3b90d22019-11-06 09:37:31 -0800250
251TEST_F(BLASTBufferQueueTest, TripleBuffering) {
252 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
253 sp<IGraphicBufferProducer> igbProducer;
254 setUpProducer(adapter, igbProducer);
255
256 std::vector<std::pair<int, sp<Fence>>> allocated;
257 for (int i = 0; i < 3; i++) {
258 int slot;
259 sp<Fence> fence;
260 sp<GraphicBuffer> buf;
261 auto ret = igbProducer->dequeueBuffer(&slot, &fence, mDisplayWidth, mDisplayHeight,
262 PIXEL_FORMAT_RGBA_8888, GRALLOC_USAGE_SW_WRITE_OFTEN,
263 nullptr, nullptr);
264 ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, ret);
265 ASSERT_EQ(OK, igbProducer->requestBuffer(slot, &buf));
266 allocated.push_back({slot, fence});
267 }
268 for (int i = 0; i < allocated.size(); i++) {
269 igbProducer->cancelBuffer(allocated[i].first, allocated[i].second);
270 }
271
272 for (int i = 0; i < 10; i++) {
273 int slot;
274 sp<Fence> fence;
275 sp<GraphicBuffer> buf;
276 auto ret = igbProducer->dequeueBuffer(&slot, &fence, mDisplayWidth, mDisplayHeight,
277 PIXEL_FORMAT_RGBA_8888, GRALLOC_USAGE_SW_WRITE_OFTEN,
278 nullptr, nullptr);
279 ASSERT_EQ(NO_ERROR, ret);
280 IGraphicBufferProducer::QueueBufferOutput qbOutput;
281 IGraphicBufferProducer::QueueBufferInput input(systemTime(), false, HAL_DATASPACE_UNKNOWN,
282 Rect(mDisplayWidth, mDisplayHeight),
283 NATIVE_WINDOW_SCALING_MODE_FREEZE, 0,
284 Fence::NO_FENCE);
285 igbProducer->queueBuffer(slot, input, &qbOutput);
286 }
287 adapter.waitForCallbacks();
288}
Valerie Hauc5011f92019-10-11 09:52:07 -0700289} // namespace android