1 /*
2 * Copyright 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 #include <cstdarg>
18 #include <cstdint>
19
20 #include <compositionengine/RenderSurfaceCreationArgs.h>
21 #include <compositionengine/impl/OutputCompositionState.h>
22 #include <compositionengine/impl/RenderSurface.h>
23 #include <compositionengine/mock/CompositionEngine.h>
24 #include <compositionengine/mock/Display.h>
25 #include <compositionengine/mock/DisplaySurface.h>
26 #include <compositionengine/mock/NativeWindow.h>
27 #include <compositionengine/mock/OutputLayer.h>
28 #include <gtest/gtest.h>
29 #include <renderengine/ExternalTexture.h>
30 #include <renderengine/mock/RenderEngine.h>
31 #include <ui/GraphicBuffer.h>
32
33 namespace android::compositionengine {
34 namespace {
35
36 constexpr int32_t DEFAULT_DISPLAY_WIDTH = 1920;
37 constexpr int32_t DEFAULT_DISPLAY_HEIGHT = 1080;
38 constexpr DisplayId DEFAULT_DISPLAY_ID = PhysicalDisplayId(123u);
39 const std::string DEFAULT_DISPLAY_NAME = "Mock Display";
40
41 using testing::_;
42 using testing::ByMove;
43 using testing::DoAll;
44 using testing::Ref;
45 using testing::Return;
46 using testing::ReturnRef;
47 using testing::SetArgPointee;
48 using testing::StrictMock;
49
50 class RenderSurfaceTest : public testing::Test {
51 public:
RenderSurfaceTest()52 RenderSurfaceTest() {
53 EXPECT_CALL(mDisplay, getId()).WillRepeatedly(Return(DEFAULT_DISPLAY_ID));
54 EXPECT_CALL(mDisplay, getName()).WillRepeatedly(ReturnRef(DEFAULT_DISPLAY_NAME));
55 EXPECT_CALL(mCompositionEngine, getRenderEngine).WillRepeatedly(ReturnRef(mRenderEngine));
56 EXPECT_CALL(*mNativeWindow, disconnect(NATIVE_WINDOW_API_EGL))
57 .WillRepeatedly(Return(NO_ERROR));
58 }
59
60 StrictMock<renderengine::mock::RenderEngine> mRenderEngine;
61 StrictMock<mock::CompositionEngine> mCompositionEngine;
62 StrictMock<mock::Display> mDisplay;
63 sp<mock::NativeWindow> mNativeWindow = new StrictMock<mock::NativeWindow>();
64 sp<mock::DisplaySurface> mDisplaySurface = new StrictMock<mock::DisplaySurface>();
65 impl::RenderSurface mSurface{mCompositionEngine, mDisplay,
66 RenderSurfaceCreationArgsBuilder()
67 .setDisplayWidth(DEFAULT_DISPLAY_WIDTH)
68 .setDisplayHeight(DEFAULT_DISPLAY_HEIGHT)
69 .setNativeWindow(mNativeWindow)
70 .setDisplaySurface(mDisplaySurface)
71 .build()};
72 };
73
74 /*
75 * Basic construction
76 */
77
TEST_F(RenderSurfaceTest,canInstantiate)78 TEST_F(RenderSurfaceTest, canInstantiate) {
79 EXPECT_TRUE(mSurface.isValid());
80 }
81
82 /*
83 * RenderSurface::initialize()
84 */
85
TEST_F(RenderSurfaceTest,initializeConfiguresNativeWindow)86 TEST_F(RenderSurfaceTest, initializeConfiguresNativeWindow) {
87 EXPECT_CALL(*mNativeWindow, connect(NATIVE_WINDOW_API_EGL)).WillOnce(Return(NO_ERROR));
88 EXPECT_CALL(*mNativeWindow, setBuffersFormat(HAL_PIXEL_FORMAT_RGBA_8888))
89 .WillOnce(Return(NO_ERROR));
90 EXPECT_CALL(*mNativeWindow, setUsage(GRALLOC_USAGE_HW_RENDER | GRALLOC_USAGE_HW_TEXTURE))
91 .WillOnce(Return(NO_ERROR));
92
93 mSurface.initialize();
94 }
95
96 /*
97 * RenderSurface::getSize()
98 */
99
TEST_F(RenderSurfaceTest,sizeReturnsConstructedSize)100 TEST_F(RenderSurfaceTest, sizeReturnsConstructedSize) {
101 const ui::Size expected{DEFAULT_DISPLAY_WIDTH, DEFAULT_DISPLAY_HEIGHT};
102
103 EXPECT_EQ(expected, mSurface.getSize());
104 }
105
106 /*
107 * RenderSurface::getClientTargetAcquireFence()
108 */
109
TEST_F(RenderSurfaceTest,getClientTargetAcquireFenceForwardsCall)110 TEST_F(RenderSurfaceTest, getClientTargetAcquireFenceForwardsCall) {
111 sp<Fence> fence = new Fence();
112
113 EXPECT_CALL(*mDisplaySurface, getClientTargetAcquireFence()).WillOnce(ReturnRef(fence));
114
115 EXPECT_EQ(fence.get(), mSurface.getClientTargetAcquireFence().get());
116 }
117
118 /*
119 * RenderSurface::setDisplaySize()
120 */
121
TEST_F(RenderSurfaceTest,setDisplaySizeAppliesChange)122 TEST_F(RenderSurfaceTest, setDisplaySizeAppliesChange) {
123 const ui::Size size(640, 480);
124 EXPECT_CALL(*mDisplaySurface, resizeBuffers(size)).Times(1);
125
126 mSurface.setDisplaySize(size);
127 }
128
129 /*
130 * RenderSurface::setBufferDataspace()
131 */
132
TEST_F(RenderSurfaceTest,setBufferDataspaceAppliesChange)133 TEST_F(RenderSurfaceTest, setBufferDataspaceAppliesChange) {
134 EXPECT_CALL(*mNativeWindow, setBuffersDataSpace(ui::Dataspace::DISPLAY_P3))
135 .WillOnce(Return(NO_ERROR));
136
137 mSurface.setBufferDataspace(ui::Dataspace::DISPLAY_P3);
138 }
139
140 /*
141 * RenderSurface::setProtected()
142 */
143
TEST_F(RenderSurfaceTest,setProtectedTrueEnablesProtection)144 TEST_F(RenderSurfaceTest, setProtectedTrueEnablesProtection) {
145 EXPECT_FALSE(mSurface.isProtected());
146 EXPECT_CALL(*mNativeWindow,
147 setUsage(GRALLOC_USAGE_HW_RENDER | GRALLOC_USAGE_HW_TEXTURE |
148 GRALLOC_USAGE_PROTECTED))
149 .WillOnce(Return(NO_ERROR));
150
151 mSurface.setProtected(true);
152 EXPECT_TRUE(mSurface.isProtected());
153 }
154
TEST_F(RenderSurfaceTest,setProtectedFalseDisablesProtection)155 TEST_F(RenderSurfaceTest, setProtectedFalseDisablesProtection) {
156 EXPECT_FALSE(mSurface.isProtected());
157 EXPECT_CALL(*mNativeWindow, setUsage(GRALLOC_USAGE_HW_RENDER | GRALLOC_USAGE_HW_TEXTURE))
158 .WillOnce(Return(NO_ERROR));
159
160 mSurface.setProtected(false);
161 EXPECT_FALSE(mSurface.isProtected());
162 }
163
TEST_F(RenderSurfaceTest,setProtectedEnableAndDisable)164 TEST_F(RenderSurfaceTest, setProtectedEnableAndDisable) {
165 EXPECT_FALSE(mSurface.isProtected());
166 EXPECT_CALL(*mNativeWindow,
167 setUsage(GRALLOC_USAGE_HW_RENDER | GRALLOC_USAGE_HW_TEXTURE |
168 GRALLOC_USAGE_PROTECTED))
169 .WillOnce(Return(NO_ERROR));
170 EXPECT_CALL(*mNativeWindow, setUsage(GRALLOC_USAGE_HW_RENDER | GRALLOC_USAGE_HW_TEXTURE))
171 .WillOnce(Return(NO_ERROR));
172
173 mSurface.setProtected(true);
174 EXPECT_TRUE(mSurface.isProtected());
175 mSurface.setProtected(false);
176 EXPECT_FALSE(mSurface.isProtected());
177 }
178
TEST_F(RenderSurfaceTest,setProtectedEnableWithError)179 TEST_F(RenderSurfaceTest, setProtectedEnableWithError) {
180 EXPECT_FALSE(mSurface.isProtected());
181 EXPECT_CALL(*mNativeWindow,
182 setUsage(GRALLOC_USAGE_HW_RENDER | GRALLOC_USAGE_HW_TEXTURE |
183 GRALLOC_USAGE_PROTECTED))
184 .WillOnce(Return(INVALID_OPERATION));
185 mSurface.setProtected(true);
186 EXPECT_FALSE(mSurface.isProtected());
187 }
188
189 /*
190 * RenderSurface::beginFrame()
191 */
192
TEST_F(RenderSurfaceTest,beginFrameAppliesChange)193 TEST_F(RenderSurfaceTest, beginFrameAppliesChange) {
194 EXPECT_CALL(*mDisplaySurface, beginFrame(true)).WillOnce(Return(NO_ERROR));
195
196 EXPECT_EQ(NO_ERROR, mSurface.beginFrame(true));
197 }
198
199 /*
200 * RenderSurface::prepareFrame()
201 */
202
TEST_F(RenderSurfaceTest,prepareFrameHandlesMixedComposition)203 TEST_F(RenderSurfaceTest, prepareFrameHandlesMixedComposition) {
204 EXPECT_CALL(*mDisplaySurface, prepareFrame(DisplaySurface::COMPOSITION_MIXED))
205 .WillOnce(Return(NO_ERROR));
206
207 mSurface.prepareFrame(true, true);
208 }
209
TEST_F(RenderSurfaceTest,prepareFrameHandlesOnlyGpuComposition)210 TEST_F(RenderSurfaceTest, prepareFrameHandlesOnlyGpuComposition) {
211 EXPECT_CALL(*mDisplaySurface, prepareFrame(DisplaySurface::COMPOSITION_GPU))
212 .WillOnce(Return(NO_ERROR));
213
214 mSurface.prepareFrame(true, false);
215 }
216
TEST_F(RenderSurfaceTest,prepareFrameHandlesOnlyHwcComposition)217 TEST_F(RenderSurfaceTest, prepareFrameHandlesOnlyHwcComposition) {
218 EXPECT_CALL(*mDisplaySurface, prepareFrame(DisplaySurface::COMPOSITION_HWC))
219 .WillOnce(Return(NO_ERROR));
220
221 mSurface.prepareFrame(false, true);
222 }
223
TEST_F(RenderSurfaceTest,prepareFrameHandlesNoComposition)224 TEST_F(RenderSurfaceTest, prepareFrameHandlesNoComposition) {
225 EXPECT_CALL(*mDisplaySurface, prepareFrame(DisplaySurface::COMPOSITION_HWC))
226 .WillOnce(Return(NO_ERROR));
227
228 mSurface.prepareFrame(false, false);
229 }
230
231 /*
232 * RenderSurface::dequeueBuffer()
233 */
234
TEST_F(RenderSurfaceTest,dequeueBufferObtainsABuffer)235 TEST_F(RenderSurfaceTest, dequeueBufferObtainsABuffer) {
236 sp<GraphicBuffer> buffer = new GraphicBuffer();
237
238 EXPECT_CALL(*mNativeWindow, dequeueBuffer(_, _))
239 .WillOnce(
240 DoAll(SetArgPointee<0>(buffer.get()), SetArgPointee<1>(-1), Return(NO_ERROR)));
241
242 base::unique_fd fence;
243 EXPECT_EQ(buffer.get(), mSurface.dequeueBuffer(&fence)->getBuffer().get());
244
245 EXPECT_EQ(buffer.get(), mSurface.mutableTextureForTest()->getBuffer().get());
246 }
247
248 /*
249 * RenderSurface::queueBuffer()
250 */
251
TEST_F(RenderSurfaceTest,queueBufferHandlesNoClientComposition)252 TEST_F(RenderSurfaceTest, queueBufferHandlesNoClientComposition) {
253 const auto buffer = std::make_shared<
254 renderengine::ExternalTexture>(new GraphicBuffer(), mRenderEngine,
255 renderengine::ExternalTexture::Usage::READABLE |
256 renderengine::ExternalTexture::Usage::WRITEABLE);
257 mSurface.mutableTextureForTest() = buffer;
258
259 impl::OutputCompositionState state;
260 state.usesClientComposition = false;
261 state.flipClientTarget = false;
262
263 EXPECT_CALL(mDisplay, getState()).WillOnce(ReturnRef(state));
264 EXPECT_CALL(*mDisplaySurface, advanceFrame()).Times(1);
265
266 mSurface.queueBuffer(base::unique_fd());
267
268 EXPECT_EQ(buffer.get(), mSurface.mutableTextureForTest().get());
269 }
270
TEST_F(RenderSurfaceTest,queueBufferHandlesClientComposition)271 TEST_F(RenderSurfaceTest, queueBufferHandlesClientComposition) {
272 const auto buffer = std::make_shared<renderengine::ExternalTexture>(new GraphicBuffer(),
273 mRenderEngine, false);
274 mSurface.mutableTextureForTest() = buffer;
275
276 impl::OutputCompositionState state;
277 state.usesClientComposition = true;
278 state.flipClientTarget = false;
279
280 EXPECT_CALL(mDisplay, getState()).WillOnce(ReturnRef(state));
281 EXPECT_CALL(*mNativeWindow, queueBuffer(buffer->getBuffer()->getNativeBuffer(), -1))
282 .WillOnce(Return(NO_ERROR));
283 EXPECT_CALL(*mDisplaySurface, advanceFrame()).Times(1);
284
285 mSurface.queueBuffer(base::unique_fd());
286
287 EXPECT_EQ(nullptr, mSurface.mutableTextureForTest().get());
288 }
289
TEST_F(RenderSurfaceTest,queueBufferHandlesFlipClientTargetRequest)290 TEST_F(RenderSurfaceTest, queueBufferHandlesFlipClientTargetRequest) {
291 const auto buffer = std::make_shared<renderengine::ExternalTexture>(new GraphicBuffer(),
292 mRenderEngine, false);
293 mSurface.mutableTextureForTest() = buffer;
294
295 impl::OutputCompositionState state;
296 state.usesClientComposition = false;
297 state.flipClientTarget = true;
298
299 EXPECT_CALL(mDisplay, getState()).WillOnce(ReturnRef(state));
300 EXPECT_CALL(*mNativeWindow, queueBuffer(buffer->getBuffer()->getNativeBuffer(), -1))
301 .WillOnce(Return(NO_ERROR));
302 EXPECT_CALL(*mDisplaySurface, advanceFrame()).Times(1);
303
304 mSurface.queueBuffer(base::unique_fd());
305
306 EXPECT_EQ(nullptr, mSurface.mutableTextureForTest().get());
307 }
308
TEST_F(RenderSurfaceTest,queueBufferHandlesFlipClientTargetRequestWithNoBufferYetDequeued)309 TEST_F(RenderSurfaceTest, queueBufferHandlesFlipClientTargetRequestWithNoBufferYetDequeued) {
310 sp<GraphicBuffer> buffer = new GraphicBuffer();
311
312 impl::OutputCompositionState state;
313 state.usesClientComposition = false;
314 state.flipClientTarget = true;
315
316 EXPECT_CALL(mDisplay, getState()).WillOnce(ReturnRef(state));
317 EXPECT_CALL(*mNativeWindow, dequeueBuffer(_, _))
318 .WillOnce(
319 DoAll(SetArgPointee<0>(buffer.get()), SetArgPointee<1>(-1), Return(NO_ERROR)));
320 EXPECT_CALL(*mNativeWindow, queueBuffer(buffer->getNativeBuffer(), -1))
321 .WillOnce(Return(NO_ERROR));
322 EXPECT_CALL(*mDisplaySurface, advanceFrame()).Times(1);
323
324 mSurface.queueBuffer(base::unique_fd());
325
326 EXPECT_EQ(nullptr, mSurface.mutableTextureForTest().get());
327 }
328
TEST_F(RenderSurfaceTest,queueBufferHandlesNativeWindowQueueBufferFailureOnVirtualDisplay)329 TEST_F(RenderSurfaceTest, queueBufferHandlesNativeWindowQueueBufferFailureOnVirtualDisplay) {
330 const auto buffer = std::make_shared<renderengine::ExternalTexture>(new GraphicBuffer(),
331 mRenderEngine, false);
332 mSurface.mutableTextureForTest() = buffer;
333
334 impl::OutputCompositionState state;
335 state.usesClientComposition = true;
336
337 EXPECT_CALL(mDisplay, getState()).WillOnce(ReturnRef(state));
338 EXPECT_CALL(*mNativeWindow, queueBuffer(buffer->getBuffer()->getNativeBuffer(), -1))
339 .WillOnce(Return(INVALID_OPERATION));
340 EXPECT_CALL(mDisplay, isVirtual()).WillOnce(Return(true));
341 EXPECT_CALL(*mNativeWindow, cancelBuffer(buffer->getBuffer()->getNativeBuffer(), -1))
342 .WillOnce(Return(NO_ERROR));
343 EXPECT_CALL(*mDisplaySurface, advanceFrame()).Times(1);
344
345 mSurface.queueBuffer(base::unique_fd());
346
347 EXPECT_EQ(nullptr, mSurface.mutableTextureForTest().get());
348 }
349
350 /*
351 * RenderSurface::onPresentDisplayCompleted()
352 */
353
TEST_F(RenderSurfaceTest,onPresentDisplayCompletedForwardsSignal)354 TEST_F(RenderSurfaceTest, onPresentDisplayCompletedForwardsSignal) {
355 EXPECT_CALL(*mDisplaySurface, onFrameCommitted()).Times(1);
356
357 mSurface.onPresentDisplayCompleted();
358 }
359
360 /*
361 * RenderSurface::flip()
362 */
363
TEST_F(RenderSurfaceTest,flipForwardsSignal)364 TEST_F(RenderSurfaceTest, flipForwardsSignal) {
365 mSurface.setPageFlipCountForTest(500);
366
367 mSurface.flip();
368
369 EXPECT_EQ(501u, mSurface.getPageFlipCount());
370 }
371
372 } // namespace
373 } // namespace android::compositionengine
374