blob: f75a4dcb858452484c4d4701bd4bf8211a87a5ec [file] [log] [blame]
Lloyd Pique31cb2942018-10-19 17:23:03 -07001/*
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>
chaviw8beb4142019-04-11 13:09:05 -070025#include <compositionengine/mock/NativeWindow.h>
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080026#include <compositionengine/mock/OutputLayer.h>
Lloyd Pique31cb2942018-10-19 17:23:03 -070027#include <gtest/gtest.h>
28#include <renderengine/mock/RenderEngine.h>
Lloyd Pique31cb2942018-10-19 17:23:03 -070029
30#include "MockHWComposer.h"
31
32namespace android::compositionengine {
33namespace {
34
35/* ------------------------------------------------------------------------
Lloyd Pique31cb2942018-10-19 17:23:03 -070036 * RenderSurfaceTest
37 */
38
39constexpr int32_t DEFAULT_DISPLAY_WIDTH = 1920;
40constexpr int32_t DEFAULT_DISPLAY_HEIGHT = 1080;
41constexpr std::optional<DisplayId> DEFAULT_DISPLAY_ID = std::make_optional(DisplayId{123u});
42const std::string DEFAULT_DISPLAY_NAME = "Mock Display";
43
44using testing::_;
45using testing::ByMove;
46using testing::DoAll;
47using testing::Ref;
48using testing::Return;
49using testing::ReturnRef;
50using testing::SetArgPointee;
51using testing::StrictMock;
52
53class RenderSurfaceTest : public testing::Test {
54public:
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));
chaviw8beb4142019-04-11 13:09:05 -070060 EXPECT_CALL(*mNativeWindow, disconnect(NATIVE_WINDOW_API_EGL))
61 .WillRepeatedly(Return(NO_ERROR));
Lloyd Pique31cb2942018-10-19 17:23:03 -070062 }
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;
chaviw8beb4142019-04-11 13:09:05 -070069 sp<mock::NativeWindow> mNativeWindow = new StrictMock<mock::NativeWindow>();
Lloyd Pique31cb2942018-10-19 17:23:03 -070070 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
81TEST_F(RenderSurfaceTest, canInstantiate) {
82 EXPECT_TRUE(mSurface.isValid());
83}
84
85/* ------------------------------------------------------------------------
86 * RenderSurface::initialize()
87 */
88
89TEST_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
102TEST_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
112TEST_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
124TEST_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
134TEST_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
145TEST_F(RenderSurfaceTest, setProtectedTrueEnablesProtection) {
Peiyong Lin52010312019-05-02 14:22:16 -0700146 EXPECT_FALSE(mSurface.isProtected());
Lloyd Pique31cb2942018-10-19 17:23:03 -0700147 EXPECT_CALL(*mNativeWindow, setUsage(GRALLOC_USAGE_HW_RENDER | GRALLOC_USAGE_PROTECTED))
148 .WillOnce(Return(NO_ERROR));
149
150 mSurface.setProtected(true);
Peiyong Lin52010312019-05-02 14:22:16 -0700151 EXPECT_TRUE(mSurface.isProtected());
Lloyd Pique31cb2942018-10-19 17:23:03 -0700152}
153
154TEST_F(RenderSurfaceTest, setProtectedFalseDisablesProtection) {
Peiyong Lin52010312019-05-02 14:22:16 -0700155 EXPECT_FALSE(mSurface.isProtected());
Lloyd Pique31cb2942018-10-19 17:23:03 -0700156 EXPECT_CALL(*mNativeWindow, setUsage(GRALLOC_USAGE_HW_RENDER)).WillOnce(Return(NO_ERROR));
157
158 mSurface.setProtected(false);
Peiyong Lin52010312019-05-02 14:22:16 -0700159 EXPECT_FALSE(mSurface.isProtected());
160}
161
162TEST_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
174TEST_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());
Lloyd Pique31cb2942018-10-19 17:23:03 -0700180}
181
182/* ------------------------------------------------------------------------
183 * RenderSurface::beginFrame()
184 */
185
186TEST_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
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800196TEST_F(RenderSurfaceTest, prepareFramePassesOutputLayersToHwc) {
197 EXPECT_CALL(mHwComposer, prepare(*DEFAULT_DISPLAY_ID, Ref(mDisplay)))
Lloyd Pique31cb2942018-10-19 17:23:03 -0700198 .WillOnce(Return(INVALID_OPERATION));
199
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800200 EXPECT_EQ(INVALID_OPERATION, mSurface.prepareFrame());
201}
202
203TEST_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());
Lloyd Pique31cb2942018-10-19 17:23:03 -0700208}
209
210TEST_F(RenderSurfaceTest, prepareFrameHandlesMixedComposition) {
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800211 EXPECT_CALL(mHwComposer, prepare(*DEFAULT_DISPLAY_ID, Ref(mDisplay)))
212 .WillOnce(Return(NO_ERROR));
Lloyd Pique31cb2942018-10-19 17:23:03 -0700213 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
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800219 EXPECT_EQ(INVALID_OPERATION, mSurface.prepareFrame());
Lloyd Pique31cb2942018-10-19 17:23:03 -0700220}
221
222TEST_F(RenderSurfaceTest, prepareFrameHandlesOnlyGlesComposition) {
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800223 EXPECT_CALL(mHwComposer, prepare(*DEFAULT_DISPLAY_ID, Ref(mDisplay)))
224 .WillOnce(Return(NO_ERROR));
Lloyd Pique31cb2942018-10-19 17:23:03 -0700225 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
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800231 EXPECT_EQ(NO_ERROR, mSurface.prepareFrame());
Lloyd Pique31cb2942018-10-19 17:23:03 -0700232}
233
234TEST_F(RenderSurfaceTest, prepareFrameHandlesOnlyHwcComposition) {
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800235 EXPECT_CALL(mHwComposer, prepare(*DEFAULT_DISPLAY_ID, Ref(mDisplay)))
236 .WillOnce(Return(NO_ERROR));
Lloyd Pique31cb2942018-10-19 17:23:03 -0700237 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
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800243 EXPECT_EQ(NO_ERROR, mSurface.prepareFrame());
Lloyd Pique31cb2942018-10-19 17:23:03 -0700244}
245
246TEST_F(RenderSurfaceTest, prepareFrameHandlesNoComposition) {
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800247 EXPECT_CALL(mHwComposer, prepare(*DEFAULT_DISPLAY_ID, Ref(mDisplay)))
248 .WillOnce(Return(NO_ERROR));
Lloyd Pique31cb2942018-10-19 17:23:03 -0700249 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
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800255 EXPECT_EQ(NO_ERROR, mSurface.prepareFrame());
Lloyd Pique31cb2942018-10-19 17:23:03 -0700256}
257
258/* ------------------------------------------------------------------------
259 * RenderSurface::dequeueBuffer()
260 */
261
262TEST_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
Alec Mouri6338c9d2019-02-07 16:57:51 -0800269 base::unique_fd fence;
270 EXPECT_EQ(buffer.get(), mSurface.dequeueBuffer(&fence).get());
Lloyd Pique31cb2942018-10-19 17:23:03 -0700271
272 EXPECT_EQ(buffer.get(), mSurface.mutableGraphicBufferForTest().get());
273}
274
275/* ------------------------------------------------------------------------
276 * RenderSurface::queueBuffer()
277 */
278
279TEST_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
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000288 mSurface.queueBuffer(base::unique_fd());
Lloyd Pique31cb2942018-10-19 17:23:03 -0700289
290 EXPECT_EQ(buffer.get(), mSurface.mutableGraphicBufferForTest().get());
291}
292
293TEST_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
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000302 mSurface.queueBuffer(base::unique_fd());
Lloyd Pique31cb2942018-10-19 17:23:03 -0700303
304 EXPECT_EQ(nullptr, mSurface.mutableGraphicBufferForTest().get());
305}
306
307TEST_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
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000317 mSurface.queueBuffer(base::unique_fd());
Lloyd Pique31cb2942018-10-19 17:23:03 -0700318
319 EXPECT_EQ(nullptr, mSurface.mutableGraphicBufferForTest().get());
320}
321
322TEST_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
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000334 mSurface.queueBuffer(base::unique_fd());
Lloyd Pique31cb2942018-10-19 17:23:03 -0700335
336 EXPECT_EQ(nullptr, mSurface.mutableGraphicBufferForTest().get());
337}
338
339TEST_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
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000351 mSurface.queueBuffer(base::unique_fd());
Lloyd Pique31cb2942018-10-19 17:23:03 -0700352
353 EXPECT_EQ(nullptr, mSurface.mutableGraphicBufferForTest().get());
354}
355
356/* ------------------------------------------------------------------------
357 * RenderSurface::onPresentDisplayCompleted()
358 */
359
360TEST_F(RenderSurfaceTest, onPresentDisplayCompletedForwardsSignal) {
361 EXPECT_CALL(*mDisplaySurface, onFrameCommitted()).Times(1);
362
363 mSurface.onPresentDisplayCompleted();
364}
365
366/* ------------------------------------------------------------------------
Lloyd Pique31cb2942018-10-19 17:23:03 -0700367 * RenderSurface::setViewportAndProjection()
368 */
369
370TEST_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
384TEST_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