blob: db1ac249b5306936cf159add61fe08e0d50f08c1 [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>
22#include <gui/IGraphicBufferProducer.h>
23#include <gui/IProducerListener.h>
Valerie Hauc5011f92019-10-11 09:52:07 -070024#include <gui/SurfaceComposerClient.h>
Valerie Hauda3446e2019-10-14 15:49:22 -070025#include <private/gui/ComposerService.h>
26#include <ui/DisplayInfo.h>
Valerie Hauc5011f92019-10-11 09:52:07 -070027#include <ui/GraphicBuffer.h>
Valerie Hauda3446e2019-10-14 15:49:22 -070028#include <ui/GraphicTypes.h>
Valerie Hauc5011f92019-10-11 09:52:07 -070029
30#include <gtest/gtest.h>
31
32using namespace std::chrono_literals;
33
34namespace android {
35
Valerie Hauc5011f92019-10-11 09:52:07 -070036using Transaction = SurfaceComposerClient::Transaction;
Valerie Hauda3446e2019-10-14 15:49:22 -070037using android::hardware::graphics::common::V1_2::BufferUsage;
Valerie Hauc5011f92019-10-11 09:52:07 -070038
39class BLASTBufferQueueHelper {
40public:
41 BLASTBufferQueueHelper(const sp<SurfaceControl>& sc, int width, int height) {
42 mBlastBufferQueueAdapter = new BLASTBufferQueue(sc, width, height);
43 }
44
45 void update(const sp<SurfaceControl>& sc, int width, int height) {
46 mBlastBufferQueueAdapter->update(sc, width, height);
47 }
48
49 void setNextTransaction(Transaction* next) {
50 mBlastBufferQueueAdapter->setNextTransaction(next);
51 }
52
53 int getWidth() { return mBlastBufferQueueAdapter->mWidth; }
Valerie Hauda3446e2019-10-14 15:49:22 -070054
Valerie Hauc5011f92019-10-11 09:52:07 -070055 int getHeight() { return mBlastBufferQueueAdapter->mHeight; }
Valerie Hauda3446e2019-10-14 15:49:22 -070056
Valerie Hauc5011f92019-10-11 09:52:07 -070057 Transaction* getNextTransaction() { return mBlastBufferQueueAdapter->mNextTransaction; }
Valerie Hauda3446e2019-10-14 15:49:22 -070058
59 sp<IGraphicBufferProducer> getIGraphicBufferProducer() {
60 return mBlastBufferQueueAdapter->getIGraphicBufferProducer();
61 }
62
Valerie Hauc5011f92019-10-11 09:52:07 -070063 const sp<SurfaceControl> getSurfaceControl() {
64 return mBlastBufferQueueAdapter->mSurfaceControl;
65 }
66
Valerie Hauda3446e2019-10-14 15:49:22 -070067 void waitForCallback() {
68 std::unique_lock lock{mBlastBufferQueueAdapter->mMutex};
69 mBlastBufferQueueAdapter->mDequeueWaitCV.wait_for(lock, 1s);
70 }
71
Valerie Hauc5011f92019-10-11 09:52:07 -070072private:
73 sp<BLASTBufferQueue> mBlastBufferQueueAdapter;
74};
75
76class BLASTBufferQueueTest : public ::testing::Test {
77public:
78protected:
79 BLASTBufferQueueTest() {
80 const ::testing::TestInfo* const testInfo =
81 ::testing::UnitTest::GetInstance()->current_test_info();
82 ALOGV("Begin test: %s.%s", testInfo->test_case_name(), testInfo->name());
83 }
84
85 ~BLASTBufferQueueTest() {
86 const ::testing::TestInfo* const testInfo =
87 ::testing::UnitTest::GetInstance()->current_test_info();
88 ALOGV("End test: %s.%s", testInfo->test_case_name(), testInfo->name());
89 }
90
91 void SetUp() {
Valerie Hauda3446e2019-10-14 15:49:22 -070092 mComposer = ComposerService::getComposerService();
Valerie Hauc5011f92019-10-11 09:52:07 -070093 mClient = new SurfaceComposerClient();
Valerie Hauda3446e2019-10-14 15:49:22 -070094 mDisplayToken = mClient->getInternalDisplayToken();
95 ASSERT_NE(nullptr, mDisplayToken.get());
96 Transaction t;
97 t.setDisplayLayerStack(mDisplayToken, 0);
98 t.apply();
99 t.clear();
100
101 DisplayInfo info;
102 ASSERT_EQ(NO_ERROR, SurfaceComposerClient::getDisplayInfo(mDisplayToken, &info));
103 mDisplayWidth = info.w;
104 mDisplayHeight = info.h;
105
106 mSurfaceControl = mClient->createSurface(String8("TestSurface"), mDisplayWidth,
107 mDisplayHeight, PIXEL_FORMAT_RGBA_8888,
108 ISurfaceComposerClient::eFXSurfaceBufferState,
109 /*parent*/ nullptr);
110 t.setLayerStack(mSurfaceControl, 0)
111 .setLayer(mSurfaceControl, std::numeric_limits<int32_t>::max())
112 .setFrame(mSurfaceControl, Rect(0, 0, mDisplayWidth, mDisplayHeight))
113 .show(mSurfaceControl)
114 .setDataspace(mSurfaceControl, ui::Dataspace::V0_SRGB)
115 .apply();
116 }
117
118 void fillBuffer(uint32_t* bufData, uint32_t width, uint32_t height, uint32_t stride, uint8_t r,
119 uint8_t g, uint8_t b) {
120 for (uint32_t row = 0; row < height; row++) {
121 for (uint32_t col = 0; col < width; col++) {
122 uint8_t* pixel = (uint8_t*)(bufData + (row * stride) + col);
123 *pixel = r;
124 *(pixel + 1) = g;
125 *(pixel + 2) = b;
126 *(pixel + 3) = 255;
127 }
128 }
129 }
130
131 void checkScreenCapture(uint8_t r, uint8_t g, uint8_t b) {
132 const auto width = mScreenCaptureBuf->getWidth();
133 const auto height = mScreenCaptureBuf->getHeight();
134 const auto stride = mScreenCaptureBuf->getStride();
135
136 uint32_t* bufData;
137 mScreenCaptureBuf->lock(static_cast<uint32_t>(GraphicBuffer::USAGE_SW_READ_OFTEN),
138 reinterpret_cast<void**>(&bufData));
139
140 for (uint32_t row = 0; row < height; row++) {
141 for (uint32_t col = 0; col < width; col++) {
142 uint8_t* pixel = (uint8_t*)(bufData + (row * stride) + col);
143 EXPECT_EQ(r, *(pixel));
144 EXPECT_EQ(g, *(pixel + 1));
145 EXPECT_EQ(b, *(pixel + 2));
146 }
147 }
148 mScreenCaptureBuf->unlock();
149 ASSERT_EQ(false, ::testing::Test::HasFailure());
Valerie Hauc5011f92019-10-11 09:52:07 -0700150 }
151
152 sp<SurfaceComposerClient> mClient;
Valerie Hauda3446e2019-10-14 15:49:22 -0700153 sp<ISurfaceComposer> mComposer;
154
155 sp<IBinder> mDisplayToken;
156
Valerie Hauc5011f92019-10-11 09:52:07 -0700157 sp<SurfaceControl> mSurfaceControl;
Valerie Hauda3446e2019-10-14 15:49:22 -0700158 sp<GraphicBuffer> mScreenCaptureBuf;
159
160 uint32_t mDisplayWidth;
161 uint32_t mDisplayHeight;
Valerie Hauc5011f92019-10-11 09:52:07 -0700162};
163
164TEST_F(BLASTBufferQueueTest, CreateBLASTBufferQueue) {
165 // create BLASTBufferQueue adapter associated with this surface
Valerie Hauda3446e2019-10-14 15:49:22 -0700166 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
Valerie Hauc5011f92019-10-11 09:52:07 -0700167 ASSERT_EQ(mSurfaceControl, adapter.getSurfaceControl());
Valerie Hauda3446e2019-10-14 15:49:22 -0700168 ASSERT_EQ(mDisplayWidth, adapter.getWidth());
169 ASSERT_EQ(mDisplayHeight, adapter.getHeight());
Valerie Hauc5011f92019-10-11 09:52:07 -0700170 ASSERT_EQ(nullptr, adapter.getNextTransaction());
171}
172
173TEST_F(BLASTBufferQueueTest, Update) {
Valerie Hauda3446e2019-10-14 15:49:22 -0700174 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
Valerie Hauc5011f92019-10-11 09:52:07 -0700175 sp<SurfaceControl> updateSurface =
Valerie Hauda3446e2019-10-14 15:49:22 -0700176 mClient->createSurface(String8("UpdateTest"), mDisplayWidth / 2, mDisplayHeight / 2,
177 PIXEL_FORMAT_RGBA_8888);
178 adapter.update(updateSurface, mDisplayWidth / 2, mDisplayHeight / 2);
Valerie Hauc5011f92019-10-11 09:52:07 -0700179 ASSERT_EQ(updateSurface, adapter.getSurfaceControl());
Valerie Hauda3446e2019-10-14 15:49:22 -0700180 ASSERT_EQ(mDisplayWidth / 2, adapter.getWidth());
181 ASSERT_EQ(mDisplayHeight / 2, adapter.getHeight());
Valerie Hauc5011f92019-10-11 09:52:07 -0700182}
183
184TEST_F(BLASTBufferQueueTest, SetNextTransaction) {
Valerie Hauda3446e2019-10-14 15:49:22 -0700185 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
Valerie Hauc5011f92019-10-11 09:52:07 -0700186 Transaction next;
187 adapter.setNextTransaction(&next);
188 ASSERT_EQ(&next, adapter.getNextTransaction());
189}
Valerie Hauda3446e2019-10-14 15:49:22 -0700190
191TEST_F(BLASTBufferQueueTest, onFrameAvailable_Apply) {
192 uint8_t r = 255;
193 uint8_t g = 0;
194 uint8_t b = 0;
195
196 BLASTBufferQueueHelper adapter(mSurfaceControl, mDisplayWidth, mDisplayHeight);
197 auto igbProducer = adapter.getIGraphicBufferProducer();
198 ASSERT_NE(nullptr, igbProducer.get());
199 IGraphicBufferProducer::QueueBufferOutput qbOutput;
200 ASSERT_EQ(NO_ERROR,
201 igbProducer->connect(new DummyProducerListener, NATIVE_WINDOW_API_CPU, false,
202 &qbOutput));
203 ASSERT_EQ(NO_ERROR, igbProducer->setMaxDequeuedBufferCount(3));
204
205 int slot;
206 sp<Fence> fence;
207 sp<GraphicBuffer> buf;
208 auto ret = igbProducer->dequeueBuffer(&slot, &fence, mDisplayWidth, mDisplayHeight,
209 PIXEL_FORMAT_RGBA_8888, GRALLOC_USAGE_SW_WRITE_OFTEN,
210 nullptr, nullptr);
211 ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, ret);
212 ASSERT_EQ(OK, igbProducer->requestBuffer(slot, &buf));
213
214 uint32_t* bufData;
215 buf->lock(static_cast<uint32_t>(GraphicBuffer::USAGE_SW_WRITE_OFTEN),
216 reinterpret_cast<void**>(&bufData));
217 fillBuffer(bufData, buf->getWidth(), buf->getHeight(), buf->getStride(), r, g, b);
218 buf->unlock();
219
220 IGraphicBufferProducer::QueueBufferInput input(systemTime(), false, HAL_DATASPACE_UNKNOWN,
221 Rect(mDisplayWidth, mDisplayHeight),
222 NATIVE_WINDOW_SCALING_MODE_FREEZE, 0,
223 Fence::NO_FENCE);
224 igbProducer->queueBuffer(slot, input, &qbOutput);
225
226 adapter.waitForCallback();
227
228 // capture screen and verify that it is red
229 bool capturedSecureLayers;
230 ASSERT_EQ(NO_ERROR,
231 mComposer->captureScreen(mDisplayToken, &mScreenCaptureBuf, capturedSecureLayers,
232 ui::Dataspace::V0_SRGB, ui::PixelFormat::RGBA_8888, Rect(),
233 mDisplayWidth, mDisplayHeight,
234 /*useIdentityTransform*/ false));
235 ASSERT_NO_FATAL_FAILURE(checkScreenCapture(r, g, b));
236}
Valerie Hauc5011f92019-10-11 09:52:07 -0700237} // namespace android