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