blob: 4fba10bf66ddc548db4683cd78ec6196bb8bec8f [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
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -080017// TODO(b/129481165): remove the #pragma below and fix conversion issues
18#pragma clang diagnostic push
19#pragma clang diagnostic ignored "-Wconversion"
20
Lloyd Pique31cb2942018-10-19 17:23:03 -070021#include <cstdarg>
22#include <cstdint>
23
24#include <compositionengine/RenderSurfaceCreationArgs.h>
Lloyd Pique66d68602019-02-13 14:23:31 -080025#include <compositionengine/impl/OutputCompositionState.h>
Lloyd Pique31cb2942018-10-19 17:23:03 -070026#include <compositionengine/impl/RenderSurface.h>
27#include <compositionengine/mock/CompositionEngine.h>
28#include <compositionengine/mock/Display.h>
29#include <compositionengine/mock/DisplaySurface.h>
chaviw8beb4142019-04-11 13:09:05 -070030#include <compositionengine/mock/NativeWindow.h>
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080031#include <compositionengine/mock/OutputLayer.h>
Lloyd Pique31cb2942018-10-19 17:23:03 -070032#include <gtest/gtest.h>
33#include <renderengine/mock/RenderEngine.h>
Lloyd Pique31cb2942018-10-19 17:23:03 -070034
Lloyd Pique31cb2942018-10-19 17:23:03 -070035namespace android::compositionengine {
36namespace {
37
Lloyd Pique31cb2942018-10-19 17:23:03 -070038constexpr int32_t DEFAULT_DISPLAY_WIDTH = 1920;
39constexpr int32_t DEFAULT_DISPLAY_HEIGHT = 1080;
40constexpr std::optional<DisplayId> DEFAULT_DISPLAY_ID = std::make_optional(DisplayId{123u});
41const std::string DEFAULT_DISPLAY_NAME = "Mock Display";
42
43using testing::_;
44using testing::ByMove;
45using testing::DoAll;
46using testing::Ref;
47using testing::Return;
48using testing::ReturnRef;
49using testing::SetArgPointee;
50using testing::StrictMock;
51
52class RenderSurfaceTest : public testing::Test {
53public:
54 RenderSurfaceTest() {
55 EXPECT_CALL(mDisplay, getId()).WillRepeatedly(ReturnRef(DEFAULT_DISPLAY_ID));
56 EXPECT_CALL(mDisplay, getName()).WillRepeatedly(ReturnRef(DEFAULT_DISPLAY_NAME));
Lloyd Pique31cb2942018-10-19 17:23:03 -070057 EXPECT_CALL(mCompositionEngine, getRenderEngine).WillRepeatedly(ReturnRef(mRenderEngine));
chaviw8beb4142019-04-11 13:09:05 -070058 EXPECT_CALL(*mNativeWindow, disconnect(NATIVE_WINDOW_API_EGL))
59 .WillRepeatedly(Return(NO_ERROR));
Lloyd Pique31cb2942018-10-19 17:23:03 -070060 }
Lloyd Pique31cb2942018-10-19 17:23:03 -070061
Lloyd Pique31cb2942018-10-19 17:23:03 -070062 StrictMock<renderengine::mock::RenderEngine> mRenderEngine;
63 StrictMock<mock::CompositionEngine> mCompositionEngine;
64 StrictMock<mock::Display> mDisplay;
chaviw8beb4142019-04-11 13:09:05 -070065 sp<mock::NativeWindow> mNativeWindow = new StrictMock<mock::NativeWindow>();
Lloyd Pique31cb2942018-10-19 17:23:03 -070066 sp<mock::DisplaySurface> mDisplaySurface = new StrictMock<mock::DisplaySurface>();
67 impl::RenderSurface mSurface{mCompositionEngine, mDisplay,
68 RenderSurfaceCreationArgs{DEFAULT_DISPLAY_WIDTH,
69 DEFAULT_DISPLAY_HEIGHT, mNativeWindow,
70 mDisplaySurface}};
71};
72
Lloyd Pique66d68602019-02-13 14:23:31 -080073/*
Lloyd Pique31cb2942018-10-19 17:23:03 -070074 * Basic construction
75 */
76
77TEST_F(RenderSurfaceTest, canInstantiate) {
78 EXPECT_TRUE(mSurface.isValid());
79}
80
Lloyd Pique66d68602019-02-13 14:23:31 -080081/*
Lloyd Pique31cb2942018-10-19 17:23:03 -070082 * RenderSurface::initialize()
83 */
84
85TEST_F(RenderSurfaceTest, initializeConfiguresNativeWindow) {
86 EXPECT_CALL(*mNativeWindow, connect(NATIVE_WINDOW_API_EGL)).WillOnce(Return(NO_ERROR));
87 EXPECT_CALL(*mNativeWindow, setBuffersFormat(HAL_PIXEL_FORMAT_RGBA_8888))
88 .WillOnce(Return(NO_ERROR));
89 EXPECT_CALL(*mNativeWindow, setUsage(GRALLOC_USAGE_HW_RENDER)).WillOnce(Return(NO_ERROR));
90
91 mSurface.initialize();
92}
93
Lloyd Pique66d68602019-02-13 14:23:31 -080094/*
Lloyd Pique31cb2942018-10-19 17:23:03 -070095 * RenderSurface::getSize()
96 */
97
98TEST_F(RenderSurfaceTest, sizeReturnsConstructedSize) {
99 const ui::Size expected{DEFAULT_DISPLAY_WIDTH, DEFAULT_DISPLAY_HEIGHT};
100
101 EXPECT_EQ(expected, mSurface.getSize());
102}
103
Lloyd Pique66d68602019-02-13 14:23:31 -0800104/*
Lloyd Pique31cb2942018-10-19 17:23:03 -0700105 * RenderSurface::getClientTargetAcquireFence()
106 */
107
108TEST_F(RenderSurfaceTest, getClientTargetAcquireFenceForwardsCall) {
109 sp<Fence> fence = new Fence();
110
111 EXPECT_CALL(*mDisplaySurface, getClientTargetAcquireFence()).WillOnce(ReturnRef(fence));
112
113 EXPECT_EQ(fence.get(), mSurface.getClientTargetAcquireFence().get());
114}
115
Lloyd Pique66d68602019-02-13 14:23:31 -0800116/*
Lloyd Pique31cb2942018-10-19 17:23:03 -0700117 * RenderSurface::setDisplaySize()
118 */
119
120TEST_F(RenderSurfaceTest, setDisplaySizeAppliesChange) {
121 EXPECT_CALL(*mDisplaySurface, resizeBuffers(640, 480)).Times(1);
122
123 mSurface.setDisplaySize(ui::Size(640, 480));
124}
125
Lloyd Pique66d68602019-02-13 14:23:31 -0800126/*
Lloyd Pique31cb2942018-10-19 17:23:03 -0700127 * RenderSurface::setBufferDataspace()
128 */
129
130TEST_F(RenderSurfaceTest, setBufferDataspaceAppliesChange) {
131 EXPECT_CALL(*mNativeWindow, setBuffersDataSpace(ui::Dataspace::DISPLAY_P3))
132 .WillOnce(Return(NO_ERROR));
133
134 mSurface.setBufferDataspace(ui::Dataspace::DISPLAY_P3);
135}
136
Lloyd Pique66d68602019-02-13 14:23:31 -0800137/*
Lloyd Pique31cb2942018-10-19 17:23:03 -0700138 * RenderSurface::setProtected()
139 */
140
141TEST_F(RenderSurfaceTest, setProtectedTrueEnablesProtection) {
Peiyong Lin52010312019-05-02 14:22:16 -0700142 EXPECT_FALSE(mSurface.isProtected());
Lloyd Pique31cb2942018-10-19 17:23:03 -0700143 EXPECT_CALL(*mNativeWindow, setUsage(GRALLOC_USAGE_HW_RENDER | GRALLOC_USAGE_PROTECTED))
144 .WillOnce(Return(NO_ERROR));
145
146 mSurface.setProtected(true);
Peiyong Lin52010312019-05-02 14:22:16 -0700147 EXPECT_TRUE(mSurface.isProtected());
Lloyd Pique31cb2942018-10-19 17:23:03 -0700148}
149
150TEST_F(RenderSurfaceTest, setProtectedFalseDisablesProtection) {
Peiyong Lin52010312019-05-02 14:22:16 -0700151 EXPECT_FALSE(mSurface.isProtected());
Lloyd Pique31cb2942018-10-19 17:23:03 -0700152 EXPECT_CALL(*mNativeWindow, setUsage(GRALLOC_USAGE_HW_RENDER)).WillOnce(Return(NO_ERROR));
153
154 mSurface.setProtected(false);
Peiyong Lin52010312019-05-02 14:22:16 -0700155 EXPECT_FALSE(mSurface.isProtected());
156}
157
158TEST_F(RenderSurfaceTest, setProtectedEnableAndDisable) {
159 EXPECT_FALSE(mSurface.isProtected());
160 EXPECT_CALL(*mNativeWindow, setUsage(GRALLOC_USAGE_HW_RENDER | GRALLOC_USAGE_PROTECTED))
161 .WillOnce(Return(NO_ERROR));
162 EXPECT_CALL(*mNativeWindow, setUsage(GRALLOC_USAGE_HW_RENDER)).WillOnce(Return(NO_ERROR));
163
164 mSurface.setProtected(true);
165 EXPECT_TRUE(mSurface.isProtected());
166 mSurface.setProtected(false);
167 EXPECT_FALSE(mSurface.isProtected());
168}
169
170TEST_F(RenderSurfaceTest, setProtectedEnableWithError) {
171 EXPECT_FALSE(mSurface.isProtected());
172 EXPECT_CALL(*mNativeWindow, setUsage(GRALLOC_USAGE_HW_RENDER | GRALLOC_USAGE_PROTECTED))
173 .WillOnce(Return(INVALID_OPERATION));
174 mSurface.setProtected(true);
175 EXPECT_FALSE(mSurface.isProtected());
Lloyd Pique31cb2942018-10-19 17:23:03 -0700176}
177
Lloyd Pique66d68602019-02-13 14:23:31 -0800178/*
Lloyd Pique31cb2942018-10-19 17:23:03 -0700179 * RenderSurface::beginFrame()
180 */
181
182TEST_F(RenderSurfaceTest, beginFrameAppliesChange) {
183 EXPECT_CALL(*mDisplaySurface, beginFrame(true)).WillOnce(Return(NO_ERROR));
184
185 EXPECT_EQ(NO_ERROR, mSurface.beginFrame(true));
186}
187
Lloyd Pique66d68602019-02-13 14:23:31 -0800188/*
Lloyd Pique31cb2942018-10-19 17:23:03 -0700189 * RenderSurface::prepareFrame()
190 */
191
Lloyd Pique31cb2942018-10-19 17:23:03 -0700192TEST_F(RenderSurfaceTest, prepareFrameHandlesMixedComposition) {
Lloyd Pique31cb2942018-10-19 17:23:03 -0700193 EXPECT_CALL(*mDisplaySurface, prepareFrame(DisplaySurface::COMPOSITION_MIXED))
Lloyd Pique66d68602019-02-13 14:23:31 -0800194 .WillOnce(Return(NO_ERROR));
Lloyd Pique31cb2942018-10-19 17:23:03 -0700195
Lloyd Pique66d68602019-02-13 14:23:31 -0800196 mSurface.prepareFrame(true, true);
Lloyd Pique31cb2942018-10-19 17:23:03 -0700197}
198
Peiyong Linf3ffc4e2019-12-13 00:46:24 -0800199TEST_F(RenderSurfaceTest, prepareFrameHandlesOnlyGpuComposition) {
200 EXPECT_CALL(*mDisplaySurface, prepareFrame(DisplaySurface::COMPOSITION_GPU))
Lloyd Pique31cb2942018-10-19 17:23:03 -0700201 .WillOnce(Return(NO_ERROR));
202
Lloyd Pique66d68602019-02-13 14:23:31 -0800203 mSurface.prepareFrame(true, false);
Lloyd Pique31cb2942018-10-19 17:23:03 -0700204}
205
206TEST_F(RenderSurfaceTest, prepareFrameHandlesOnlyHwcComposition) {
Lloyd Pique31cb2942018-10-19 17:23:03 -0700207 EXPECT_CALL(*mDisplaySurface, prepareFrame(DisplaySurface::COMPOSITION_HWC))
208 .WillOnce(Return(NO_ERROR));
209
Lloyd Pique66d68602019-02-13 14:23:31 -0800210 mSurface.prepareFrame(false, true);
Lloyd Pique31cb2942018-10-19 17:23:03 -0700211}
212
213TEST_F(RenderSurfaceTest, prepareFrameHandlesNoComposition) {
Lloyd Pique31cb2942018-10-19 17:23:03 -0700214 EXPECT_CALL(*mDisplaySurface, prepareFrame(DisplaySurface::COMPOSITION_HWC))
215 .WillOnce(Return(NO_ERROR));
216
Lloyd Pique66d68602019-02-13 14:23:31 -0800217 mSurface.prepareFrame(false, false);
Lloyd Pique31cb2942018-10-19 17:23:03 -0700218}
219
Lloyd Pique66d68602019-02-13 14:23:31 -0800220/*
Lloyd Pique31cb2942018-10-19 17:23:03 -0700221 * RenderSurface::dequeueBuffer()
222 */
223
224TEST_F(RenderSurfaceTest, dequeueBufferObtainsABuffer) {
225 sp<GraphicBuffer> buffer = new GraphicBuffer();
226
227 EXPECT_CALL(*mNativeWindow, dequeueBuffer(_, _))
228 .WillOnce(
229 DoAll(SetArgPointee<0>(buffer.get()), SetArgPointee<1>(-1), Return(NO_ERROR)));
230
Alec Mouri6338c9d2019-02-07 16:57:51 -0800231 base::unique_fd fence;
232 EXPECT_EQ(buffer.get(), mSurface.dequeueBuffer(&fence).get());
Lloyd Pique31cb2942018-10-19 17:23:03 -0700233
234 EXPECT_EQ(buffer.get(), mSurface.mutableGraphicBufferForTest().get());
235}
236
Lloyd Pique66d68602019-02-13 14:23:31 -0800237/*
Lloyd Pique31cb2942018-10-19 17:23:03 -0700238 * RenderSurface::queueBuffer()
239 */
240
241TEST_F(RenderSurfaceTest, queueBufferHandlesNoClientComposition) {
242 sp<GraphicBuffer> buffer = new GraphicBuffer();
243 mSurface.mutableGraphicBufferForTest() = buffer;
244
Lloyd Pique66d68602019-02-13 14:23:31 -0800245 impl::OutputCompositionState state;
246 state.usesClientComposition = false;
247 state.flipClientTarget = false;
248
249 EXPECT_CALL(mDisplay, getState()).WillOnce(ReturnRef(state));
Lloyd Pique31cb2942018-10-19 17:23:03 -0700250 EXPECT_CALL(*mDisplaySurface, advanceFrame()).Times(1);
251
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000252 mSurface.queueBuffer(base::unique_fd());
Lloyd Pique31cb2942018-10-19 17:23:03 -0700253
254 EXPECT_EQ(buffer.get(), mSurface.mutableGraphicBufferForTest().get());
255}
256
257TEST_F(RenderSurfaceTest, queueBufferHandlesClientComposition) {
258 sp<GraphicBuffer> buffer = new GraphicBuffer();
259 mSurface.mutableGraphicBufferForTest() = buffer;
260
Lloyd Pique66d68602019-02-13 14:23:31 -0800261 impl::OutputCompositionState state;
262 state.usesClientComposition = true;
263 state.flipClientTarget = false;
264
265 EXPECT_CALL(mDisplay, getState()).WillOnce(ReturnRef(state));
Lloyd Pique31cb2942018-10-19 17:23:03 -0700266 EXPECT_CALL(*mNativeWindow, queueBuffer(buffer->getNativeBuffer(), -1))
267 .WillOnce(Return(NO_ERROR));
268 EXPECT_CALL(*mDisplaySurface, advanceFrame()).Times(1);
269
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000270 mSurface.queueBuffer(base::unique_fd());
Lloyd Pique31cb2942018-10-19 17:23:03 -0700271
272 EXPECT_EQ(nullptr, mSurface.mutableGraphicBufferForTest().get());
273}
274
275TEST_F(RenderSurfaceTest, queueBufferHandlesFlipClientTargetRequest) {
276 sp<GraphicBuffer> buffer = new GraphicBuffer();
277 mSurface.mutableGraphicBufferForTest() = buffer;
278
Lloyd Pique66d68602019-02-13 14:23:31 -0800279 impl::OutputCompositionState state;
280 state.usesClientComposition = false;
281 state.flipClientTarget = true;
282
283 EXPECT_CALL(mDisplay, getState()).WillOnce(ReturnRef(state));
Lloyd Pique31cb2942018-10-19 17:23:03 -0700284 EXPECT_CALL(*mNativeWindow, queueBuffer(buffer->getNativeBuffer(), -1))
285 .WillOnce(Return(NO_ERROR));
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(nullptr, mSurface.mutableGraphicBufferForTest().get());
291}
292
293TEST_F(RenderSurfaceTest, queueBufferHandlesFlipClientTargetRequestWithNoBufferYetDequeued) {
294 sp<GraphicBuffer> buffer = new GraphicBuffer();
295
Lloyd Pique66d68602019-02-13 14:23:31 -0800296 impl::OutputCompositionState state;
297 state.usesClientComposition = false;
298 state.flipClientTarget = true;
299
300 EXPECT_CALL(mDisplay, getState()).WillOnce(ReturnRef(state));
Lloyd Pique31cb2942018-10-19 17:23:03 -0700301 EXPECT_CALL(*mNativeWindow, dequeueBuffer(_, _))
302 .WillOnce(
303 DoAll(SetArgPointee<0>(buffer.get()), SetArgPointee<1>(-1), Return(NO_ERROR)));
304 EXPECT_CALL(*mNativeWindow, queueBuffer(buffer->getNativeBuffer(), -1))
305 .WillOnce(Return(NO_ERROR));
306 EXPECT_CALL(*mDisplaySurface, advanceFrame()).Times(1);
307
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000308 mSurface.queueBuffer(base::unique_fd());
Lloyd Pique31cb2942018-10-19 17:23:03 -0700309
310 EXPECT_EQ(nullptr, mSurface.mutableGraphicBufferForTest().get());
311}
312
313TEST_F(RenderSurfaceTest, queueBufferHandlesNativeWindowQueueBufferFailureOnVirtualDisplay) {
314 sp<GraphicBuffer> buffer = new GraphicBuffer();
315 mSurface.mutableGraphicBufferForTest() = buffer;
316
Lloyd Pique66d68602019-02-13 14:23:31 -0800317 impl::OutputCompositionState state;
318 state.usesClientComposition = true;
319
320 EXPECT_CALL(mDisplay, getState()).WillOnce(ReturnRef(state));
Lloyd Pique31cb2942018-10-19 17:23:03 -0700321 EXPECT_CALL(*mNativeWindow, queueBuffer(buffer->getNativeBuffer(), -1))
322 .WillOnce(Return(INVALID_OPERATION));
323 EXPECT_CALL(mDisplay, isVirtual()).WillOnce(Return(true));
324 EXPECT_CALL(*mNativeWindow, cancelBuffer(buffer->getNativeBuffer(), -1))
325 .WillOnce(Return(NO_ERROR));
326 EXPECT_CALL(*mDisplaySurface, advanceFrame()).Times(1);
327
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000328 mSurface.queueBuffer(base::unique_fd());
Lloyd Pique31cb2942018-10-19 17:23:03 -0700329
330 EXPECT_EQ(nullptr, mSurface.mutableGraphicBufferForTest().get());
331}
332
Lloyd Pique66d68602019-02-13 14:23:31 -0800333/*
Lloyd Pique31cb2942018-10-19 17:23:03 -0700334 * RenderSurface::onPresentDisplayCompleted()
335 */
336
337TEST_F(RenderSurfaceTest, onPresentDisplayCompletedForwardsSignal) {
338 EXPECT_CALL(*mDisplaySurface, onFrameCommitted()).Times(1);
339
340 mSurface.onPresentDisplayCompleted();
341}
342
Lloyd Pique66d68602019-02-13 14:23:31 -0800343/*
Lloyd Pique31cb2942018-10-19 17:23:03 -0700344 * RenderSurface::flip()
345 */
346
347TEST_F(RenderSurfaceTest, flipForwardsSignal) {
348 mSurface.setPageFlipCountForTest(500);
349
350 mSurface.flip();
351
352 EXPECT_EQ(501, mSurface.getPageFlipCount());
353}
354
355} // namespace
356} // namespace android::compositionengine
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -0800357
358// TODO(b/129481165): remove the #pragma below and fix conversion issues
359#pragma clang diagnostic pop // ignored "-Wconversion"