blob: 996047898a4b8c09f35735a7c63e3f2ef39de3b8 [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) {
146 EXPECT_CALL(*mNativeWindow, setUsage(GRALLOC_USAGE_HW_RENDER | GRALLOC_USAGE_PROTECTED))
147 .WillOnce(Return(NO_ERROR));
148
149 mSurface.setProtected(true);
150}
151
152TEST_F(RenderSurfaceTest, setProtectedFalseDisablesProtection) {
153 EXPECT_CALL(*mNativeWindow, setUsage(GRALLOC_USAGE_HW_RENDER)).WillOnce(Return(NO_ERROR));
154
155 mSurface.setProtected(false);
156}
157
158/* ------------------------------------------------------------------------
159 * RenderSurface::beginFrame()
160 */
161
162TEST_F(RenderSurfaceTest, beginFrameAppliesChange) {
163 EXPECT_CALL(*mDisplaySurface, beginFrame(true)).WillOnce(Return(NO_ERROR));
164
165 EXPECT_EQ(NO_ERROR, mSurface.beginFrame(true));
166}
167
168/* ------------------------------------------------------------------------
169 * RenderSurface::prepareFrame()
170 */
171
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800172TEST_F(RenderSurfaceTest, prepareFramePassesOutputLayersToHwc) {
173 EXPECT_CALL(mHwComposer, prepare(*DEFAULT_DISPLAY_ID, Ref(mDisplay)))
Lloyd Pique31cb2942018-10-19 17:23:03 -0700174 .WillOnce(Return(INVALID_OPERATION));
175
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800176 EXPECT_EQ(INVALID_OPERATION, mSurface.prepareFrame());
177}
178
179TEST_F(RenderSurfaceTest, prepareFrameTakesEarlyOutOnHwcError) {
180 EXPECT_CALL(mHwComposer, prepare(*DEFAULT_DISPLAY_ID, Ref(mDisplay)))
181 .WillOnce(Return(INVALID_OPERATION));
182
183 EXPECT_EQ(INVALID_OPERATION, mSurface.prepareFrame());
Lloyd Pique31cb2942018-10-19 17:23:03 -0700184}
185
186TEST_F(RenderSurfaceTest, prepareFrameHandlesMixedComposition) {
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800187 EXPECT_CALL(mHwComposer, prepare(*DEFAULT_DISPLAY_ID, Ref(mDisplay)))
188 .WillOnce(Return(NO_ERROR));
Lloyd Pique31cb2942018-10-19 17:23:03 -0700189 EXPECT_CALL(mHwComposer, hasClientComposition(DEFAULT_DISPLAY_ID)).WillOnce(Return(true));
190 EXPECT_CALL(mHwComposer, hasDeviceComposition(DEFAULT_DISPLAY_ID)).WillOnce(Return(true));
191
192 EXPECT_CALL(*mDisplaySurface, prepareFrame(DisplaySurface::COMPOSITION_MIXED))
193 .WillOnce(Return(INVALID_OPERATION));
194
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800195 EXPECT_EQ(INVALID_OPERATION, mSurface.prepareFrame());
Lloyd Pique31cb2942018-10-19 17:23:03 -0700196}
197
198TEST_F(RenderSurfaceTest, prepareFrameHandlesOnlyGlesComposition) {
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800199 EXPECT_CALL(mHwComposer, prepare(*DEFAULT_DISPLAY_ID, Ref(mDisplay)))
200 .WillOnce(Return(NO_ERROR));
Lloyd Pique31cb2942018-10-19 17:23:03 -0700201 EXPECT_CALL(mHwComposer, hasClientComposition(DEFAULT_DISPLAY_ID)).WillOnce(Return(true));
202 EXPECT_CALL(mHwComposer, hasDeviceComposition(DEFAULT_DISPLAY_ID)).WillOnce(Return(false));
203
204 EXPECT_CALL(*mDisplaySurface, prepareFrame(DisplaySurface::COMPOSITION_GLES))
205 .WillOnce(Return(NO_ERROR));
206
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800207 EXPECT_EQ(NO_ERROR, mSurface.prepareFrame());
Lloyd Pique31cb2942018-10-19 17:23:03 -0700208}
209
210TEST_F(RenderSurfaceTest, prepareFrameHandlesOnlyHwcComposition) {
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(false));
214 EXPECT_CALL(mHwComposer, hasDeviceComposition(DEFAULT_DISPLAY_ID)).WillOnce(Return(true));
215
216 EXPECT_CALL(*mDisplaySurface, prepareFrame(DisplaySurface::COMPOSITION_HWC))
217 .WillOnce(Return(NO_ERROR));
218
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800219 EXPECT_EQ(NO_ERROR, mSurface.prepareFrame());
Lloyd Pique31cb2942018-10-19 17:23:03 -0700220}
221
222TEST_F(RenderSurfaceTest, prepareFrameHandlesNoComposition) {
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(false));
226 EXPECT_CALL(mHwComposer, hasDeviceComposition(DEFAULT_DISPLAY_ID)).WillOnce(Return(false));
227
228 EXPECT_CALL(*mDisplaySurface, prepareFrame(DisplaySurface::COMPOSITION_HWC))
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
234/* ------------------------------------------------------------------------
235 * RenderSurface::dequeueBuffer()
236 */
237
238TEST_F(RenderSurfaceTest, dequeueBufferObtainsABuffer) {
239 sp<GraphicBuffer> buffer = new GraphicBuffer();
240
241 EXPECT_CALL(*mNativeWindow, dequeueBuffer(_, _))
242 .WillOnce(
243 DoAll(SetArgPointee<0>(buffer.get()), SetArgPointee<1>(-1), Return(NO_ERROR)));
244
Alec Mouri6338c9d2019-02-07 16:57:51 -0800245 base::unique_fd fence;
246 EXPECT_EQ(buffer.get(), mSurface.dequeueBuffer(&fence).get());
Lloyd Pique31cb2942018-10-19 17:23:03 -0700247
248 EXPECT_EQ(buffer.get(), mSurface.mutableGraphicBufferForTest().get());
249}
250
251/* ------------------------------------------------------------------------
252 * RenderSurface::queueBuffer()
253 */
254
255TEST_F(RenderSurfaceTest, queueBufferHandlesNoClientComposition) {
256 sp<GraphicBuffer> buffer = new GraphicBuffer();
257 mSurface.mutableGraphicBufferForTest() = buffer;
258
259 EXPECT_CALL(mHwComposer, hasClientComposition(DEFAULT_DISPLAY_ID)).WillOnce(Return(false));
260 EXPECT_CALL(mHwComposer, hasFlipClientTargetRequest(DEFAULT_DISPLAY_ID))
261 .WillOnce(Return(false));
262 EXPECT_CALL(*mDisplaySurface, advanceFrame()).Times(1);
263
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000264 mSurface.queueBuffer(base::unique_fd());
Lloyd Pique31cb2942018-10-19 17:23:03 -0700265
266 EXPECT_EQ(buffer.get(), mSurface.mutableGraphicBufferForTest().get());
267}
268
269TEST_F(RenderSurfaceTest, queueBufferHandlesClientComposition) {
270 sp<GraphicBuffer> buffer = new GraphicBuffer();
271 mSurface.mutableGraphicBufferForTest() = buffer;
272
273 EXPECT_CALL(mHwComposer, hasClientComposition(DEFAULT_DISPLAY_ID)).WillOnce(Return(true));
274 EXPECT_CALL(*mNativeWindow, queueBuffer(buffer->getNativeBuffer(), -1))
275 .WillOnce(Return(NO_ERROR));
276 EXPECT_CALL(*mDisplaySurface, advanceFrame()).Times(1);
277
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000278 mSurface.queueBuffer(base::unique_fd());
Lloyd Pique31cb2942018-10-19 17:23:03 -0700279
280 EXPECT_EQ(nullptr, mSurface.mutableGraphicBufferForTest().get());
281}
282
283TEST_F(RenderSurfaceTest, queueBufferHandlesFlipClientTargetRequest) {
284 sp<GraphicBuffer> buffer = new GraphicBuffer();
285 mSurface.mutableGraphicBufferForTest() = buffer;
286
287 EXPECT_CALL(mHwComposer, hasClientComposition(DEFAULT_DISPLAY_ID)).WillOnce(Return(false));
288 EXPECT_CALL(mHwComposer, hasFlipClientTargetRequest(DEFAULT_DISPLAY_ID)).WillOnce(Return(true));
289 EXPECT_CALL(*mNativeWindow, queueBuffer(buffer->getNativeBuffer(), -1))
290 .WillOnce(Return(NO_ERROR));
291 EXPECT_CALL(*mDisplaySurface, advanceFrame()).Times(1);
292
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000293 mSurface.queueBuffer(base::unique_fd());
Lloyd Pique31cb2942018-10-19 17:23:03 -0700294
295 EXPECT_EQ(nullptr, mSurface.mutableGraphicBufferForTest().get());
296}
297
298TEST_F(RenderSurfaceTest, queueBufferHandlesFlipClientTargetRequestWithNoBufferYetDequeued) {
299 sp<GraphicBuffer> buffer = new GraphicBuffer();
300
301 EXPECT_CALL(mHwComposer, hasClientComposition(DEFAULT_DISPLAY_ID)).WillOnce(Return(false));
302 EXPECT_CALL(mHwComposer, hasFlipClientTargetRequest(DEFAULT_DISPLAY_ID)).WillOnce(Return(true));
303 EXPECT_CALL(*mNativeWindow, dequeueBuffer(_, _))
304 .WillOnce(
305 DoAll(SetArgPointee<0>(buffer.get()), SetArgPointee<1>(-1), Return(NO_ERROR)));
306 EXPECT_CALL(*mNativeWindow, queueBuffer(buffer->getNativeBuffer(), -1))
307 .WillOnce(Return(NO_ERROR));
308 EXPECT_CALL(*mDisplaySurface, advanceFrame()).Times(1);
309
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000310 mSurface.queueBuffer(base::unique_fd());
Lloyd Pique31cb2942018-10-19 17:23:03 -0700311
312 EXPECT_EQ(nullptr, mSurface.mutableGraphicBufferForTest().get());
313}
314
315TEST_F(RenderSurfaceTest, queueBufferHandlesNativeWindowQueueBufferFailureOnVirtualDisplay) {
316 sp<GraphicBuffer> buffer = new GraphicBuffer();
317 mSurface.mutableGraphicBufferForTest() = buffer;
318
319 EXPECT_CALL(mHwComposer, hasClientComposition(DEFAULT_DISPLAY_ID)).WillOnce(Return(true));
320 EXPECT_CALL(*mNativeWindow, queueBuffer(buffer->getNativeBuffer(), -1))
321 .WillOnce(Return(INVALID_OPERATION));
322 EXPECT_CALL(mDisplay, isVirtual()).WillOnce(Return(true));
323 EXPECT_CALL(*mNativeWindow, cancelBuffer(buffer->getNativeBuffer(), -1))
324 .WillOnce(Return(NO_ERROR));
325 EXPECT_CALL(*mDisplaySurface, advanceFrame()).Times(1);
326
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000327 mSurface.queueBuffer(base::unique_fd());
Lloyd Pique31cb2942018-10-19 17:23:03 -0700328
329 EXPECT_EQ(nullptr, mSurface.mutableGraphicBufferForTest().get());
330}
331
332/* ------------------------------------------------------------------------
333 * RenderSurface::onPresentDisplayCompleted()
334 */
335
336TEST_F(RenderSurfaceTest, onPresentDisplayCompletedForwardsSignal) {
337 EXPECT_CALL(*mDisplaySurface, onFrameCommitted()).Times(1);
338
339 mSurface.onPresentDisplayCompleted();
340}
341
342/* ------------------------------------------------------------------------
Lloyd Pique31cb2942018-10-19 17:23:03 -0700343 * RenderSurface::setViewportAndProjection()
344 */
345
346TEST_F(RenderSurfaceTest, setViewportAndProjectionAppliesChang) {
347 mSurface.setSizeForTest(ui::Size(100, 200));
348
349 EXPECT_CALL(mRenderEngine,
350 setViewportAndProjection(100, 200, Rect(100, 200), ui::Transform::ROT_0))
351 .Times(1);
352
353 mSurface.setViewportAndProjection();
354}
355
356/* ------------------------------------------------------------------------
357 * RenderSurface::flip()
358 */
359
360TEST_F(RenderSurfaceTest, flipForwardsSignal) {
361 mSurface.setPageFlipCountForTest(500);
362
363 mSurface.flip();
364
365 EXPECT_EQ(501, mSurface.getPageFlipCount());
366}
367
368} // namespace
369} // namespace android::compositionengine