blob: da3f4fb4cd732fe64aaa4a111fc983a8e9f83b0b [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>
Lloyd Pique66d68602019-02-13 14:23:31 -080021#include <compositionengine/impl/OutputCompositionState.h>
Lloyd Pique31cb2942018-10-19 17:23:03 -070022#include <compositionengine/impl/RenderSurface.h>
23#include <compositionengine/mock/CompositionEngine.h>
24#include <compositionengine/mock/Display.h>
25#include <compositionengine/mock/DisplaySurface.h>
chaviw8beb4142019-04-11 13:09:05 -070026#include <compositionengine/mock/NativeWindow.h>
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080027#include <compositionengine/mock/OutputLayer.h>
Lloyd Pique31cb2942018-10-19 17:23:03 -070028#include <gtest/gtest.h>
29#include <renderengine/mock/RenderEngine.h>
Lloyd Pique31cb2942018-10-19 17:23:03 -070030
Lloyd Pique31cb2942018-10-19 17:23:03 -070031namespace android::compositionengine {
32namespace {
33
Lloyd Pique31cb2942018-10-19 17:23:03 -070034constexpr int32_t DEFAULT_DISPLAY_WIDTH = 1920;
35constexpr int32_t DEFAULT_DISPLAY_HEIGHT = 1080;
36constexpr std::optional<DisplayId> DEFAULT_DISPLAY_ID = std::make_optional(DisplayId{123u});
37const std::string DEFAULT_DISPLAY_NAME = "Mock Display";
38
39using testing::_;
40using testing::ByMove;
41using testing::DoAll;
42using testing::Ref;
43using testing::Return;
44using testing::ReturnRef;
45using testing::SetArgPointee;
46using testing::StrictMock;
47
48class RenderSurfaceTest : public testing::Test {
49public:
50 RenderSurfaceTest() {
51 EXPECT_CALL(mDisplay, getId()).WillRepeatedly(ReturnRef(DEFAULT_DISPLAY_ID));
52 EXPECT_CALL(mDisplay, getName()).WillRepeatedly(ReturnRef(DEFAULT_DISPLAY_NAME));
Lloyd Pique31cb2942018-10-19 17:23:03 -070053 EXPECT_CALL(mCompositionEngine, getRenderEngine).WillRepeatedly(ReturnRef(mRenderEngine));
chaviw8beb4142019-04-11 13:09:05 -070054 EXPECT_CALL(*mNativeWindow, disconnect(NATIVE_WINDOW_API_EGL))
55 .WillRepeatedly(Return(NO_ERROR));
Lloyd Pique31cb2942018-10-19 17:23:03 -070056 }
Lloyd Pique31cb2942018-10-19 17:23:03 -070057
Lloyd Pique31cb2942018-10-19 17:23:03 -070058 StrictMock<renderengine::mock::RenderEngine> mRenderEngine;
59 StrictMock<mock::CompositionEngine> mCompositionEngine;
60 StrictMock<mock::Display> mDisplay;
chaviw8beb4142019-04-11 13:09:05 -070061 sp<mock::NativeWindow> mNativeWindow = new StrictMock<mock::NativeWindow>();
Lloyd Pique31cb2942018-10-19 17:23:03 -070062 sp<mock::DisplaySurface> mDisplaySurface = new StrictMock<mock::DisplaySurface>();
63 impl::RenderSurface mSurface{mCompositionEngine, mDisplay,
64 RenderSurfaceCreationArgs{DEFAULT_DISPLAY_WIDTH,
65 DEFAULT_DISPLAY_HEIGHT, mNativeWindow,
66 mDisplaySurface}};
67};
68
Lloyd Pique66d68602019-02-13 14:23:31 -080069/*
Lloyd Pique31cb2942018-10-19 17:23:03 -070070 * Basic construction
71 */
72
73TEST_F(RenderSurfaceTest, canInstantiate) {
74 EXPECT_TRUE(mSurface.isValid());
75}
76
Lloyd Pique66d68602019-02-13 14:23:31 -080077/*
Lloyd Pique31cb2942018-10-19 17:23:03 -070078 * RenderSurface::initialize()
79 */
80
81TEST_F(RenderSurfaceTest, initializeConfiguresNativeWindow) {
82 EXPECT_CALL(*mNativeWindow, connect(NATIVE_WINDOW_API_EGL)).WillOnce(Return(NO_ERROR));
83 EXPECT_CALL(*mNativeWindow, setBuffersFormat(HAL_PIXEL_FORMAT_RGBA_8888))
84 .WillOnce(Return(NO_ERROR));
85 EXPECT_CALL(*mNativeWindow, setUsage(GRALLOC_USAGE_HW_RENDER)).WillOnce(Return(NO_ERROR));
86
87 mSurface.initialize();
88}
89
Lloyd Pique66d68602019-02-13 14:23:31 -080090/*
Lloyd Pique31cb2942018-10-19 17:23:03 -070091 * RenderSurface::getSize()
92 */
93
94TEST_F(RenderSurfaceTest, sizeReturnsConstructedSize) {
95 const ui::Size expected{DEFAULT_DISPLAY_WIDTH, DEFAULT_DISPLAY_HEIGHT};
96
97 EXPECT_EQ(expected, mSurface.getSize());
98}
99
Lloyd Pique66d68602019-02-13 14:23:31 -0800100/*
Lloyd Pique31cb2942018-10-19 17:23:03 -0700101 * RenderSurface::getClientTargetAcquireFence()
102 */
103
104TEST_F(RenderSurfaceTest, getClientTargetAcquireFenceForwardsCall) {
105 sp<Fence> fence = new Fence();
106
107 EXPECT_CALL(*mDisplaySurface, getClientTargetAcquireFence()).WillOnce(ReturnRef(fence));
108
109 EXPECT_EQ(fence.get(), mSurface.getClientTargetAcquireFence().get());
110}
111
Lloyd Pique66d68602019-02-13 14:23:31 -0800112/*
Lloyd Pique31cb2942018-10-19 17:23:03 -0700113 * RenderSurface::setDisplaySize()
114 */
115
116TEST_F(RenderSurfaceTest, setDisplaySizeAppliesChange) {
117 EXPECT_CALL(*mDisplaySurface, resizeBuffers(640, 480)).Times(1);
118
119 mSurface.setDisplaySize(ui::Size(640, 480));
120}
121
Lloyd Pique66d68602019-02-13 14:23:31 -0800122/*
Lloyd Pique31cb2942018-10-19 17:23:03 -0700123 * RenderSurface::setBufferDataspace()
124 */
125
126TEST_F(RenderSurfaceTest, setBufferDataspaceAppliesChange) {
127 EXPECT_CALL(*mNativeWindow, setBuffersDataSpace(ui::Dataspace::DISPLAY_P3))
128 .WillOnce(Return(NO_ERROR));
129
130 mSurface.setBufferDataspace(ui::Dataspace::DISPLAY_P3);
131}
132
Lloyd Pique66d68602019-02-13 14:23:31 -0800133/*
Lloyd Pique31cb2942018-10-19 17:23:03 -0700134 * RenderSurface::setProtected()
135 */
136
137TEST_F(RenderSurfaceTest, setProtectedTrueEnablesProtection) {
Peiyong Lin52010312019-05-02 14:22:16 -0700138 EXPECT_FALSE(mSurface.isProtected());
Lloyd Pique31cb2942018-10-19 17:23:03 -0700139 EXPECT_CALL(*mNativeWindow, setUsage(GRALLOC_USAGE_HW_RENDER | GRALLOC_USAGE_PROTECTED))
140 .WillOnce(Return(NO_ERROR));
141
142 mSurface.setProtected(true);
Peiyong Lin52010312019-05-02 14:22:16 -0700143 EXPECT_TRUE(mSurface.isProtected());
Lloyd Pique31cb2942018-10-19 17:23:03 -0700144}
145
146TEST_F(RenderSurfaceTest, setProtectedFalseDisablesProtection) {
Peiyong Lin52010312019-05-02 14:22:16 -0700147 EXPECT_FALSE(mSurface.isProtected());
Lloyd Pique31cb2942018-10-19 17:23:03 -0700148 EXPECT_CALL(*mNativeWindow, setUsage(GRALLOC_USAGE_HW_RENDER)).WillOnce(Return(NO_ERROR));
149
150 mSurface.setProtected(false);
Peiyong Lin52010312019-05-02 14:22:16 -0700151 EXPECT_FALSE(mSurface.isProtected());
152}
153
154TEST_F(RenderSurfaceTest, setProtectedEnableAndDisable) {
155 EXPECT_FALSE(mSurface.isProtected());
156 EXPECT_CALL(*mNativeWindow, setUsage(GRALLOC_USAGE_HW_RENDER | GRALLOC_USAGE_PROTECTED))
157 .WillOnce(Return(NO_ERROR));
158 EXPECT_CALL(*mNativeWindow, setUsage(GRALLOC_USAGE_HW_RENDER)).WillOnce(Return(NO_ERROR));
159
160 mSurface.setProtected(true);
161 EXPECT_TRUE(mSurface.isProtected());
162 mSurface.setProtected(false);
163 EXPECT_FALSE(mSurface.isProtected());
164}
165
166TEST_F(RenderSurfaceTest, setProtectedEnableWithError) {
167 EXPECT_FALSE(mSurface.isProtected());
168 EXPECT_CALL(*mNativeWindow, setUsage(GRALLOC_USAGE_HW_RENDER | GRALLOC_USAGE_PROTECTED))
169 .WillOnce(Return(INVALID_OPERATION));
170 mSurface.setProtected(true);
171 EXPECT_FALSE(mSurface.isProtected());
Lloyd Pique31cb2942018-10-19 17:23:03 -0700172}
173
Lloyd Pique66d68602019-02-13 14:23:31 -0800174/*
Lloyd Pique31cb2942018-10-19 17:23:03 -0700175 * RenderSurface::beginFrame()
176 */
177
178TEST_F(RenderSurfaceTest, beginFrameAppliesChange) {
179 EXPECT_CALL(*mDisplaySurface, beginFrame(true)).WillOnce(Return(NO_ERROR));
180
181 EXPECT_EQ(NO_ERROR, mSurface.beginFrame(true));
182}
183
Lloyd Pique66d68602019-02-13 14:23:31 -0800184/*
Lloyd Pique31cb2942018-10-19 17:23:03 -0700185 * RenderSurface::prepareFrame()
186 */
187
Lloyd Pique31cb2942018-10-19 17:23:03 -0700188TEST_F(RenderSurfaceTest, prepareFrameHandlesMixedComposition) {
Lloyd Pique31cb2942018-10-19 17:23:03 -0700189 EXPECT_CALL(*mDisplaySurface, prepareFrame(DisplaySurface::COMPOSITION_MIXED))
Lloyd Pique66d68602019-02-13 14:23:31 -0800190 .WillOnce(Return(NO_ERROR));
Lloyd Pique31cb2942018-10-19 17:23:03 -0700191
Lloyd Pique66d68602019-02-13 14:23:31 -0800192 mSurface.prepareFrame(true, true);
Lloyd Pique31cb2942018-10-19 17:23:03 -0700193}
194
195TEST_F(RenderSurfaceTest, prepareFrameHandlesOnlyGlesComposition) {
Lloyd Pique31cb2942018-10-19 17:23:03 -0700196 EXPECT_CALL(*mDisplaySurface, prepareFrame(DisplaySurface::COMPOSITION_GLES))
197 .WillOnce(Return(NO_ERROR));
198
Lloyd Pique66d68602019-02-13 14:23:31 -0800199 mSurface.prepareFrame(true, false);
Lloyd Pique31cb2942018-10-19 17:23:03 -0700200}
201
202TEST_F(RenderSurfaceTest, prepareFrameHandlesOnlyHwcComposition) {
Lloyd Pique31cb2942018-10-19 17:23:03 -0700203 EXPECT_CALL(*mDisplaySurface, prepareFrame(DisplaySurface::COMPOSITION_HWC))
204 .WillOnce(Return(NO_ERROR));
205
Lloyd Pique66d68602019-02-13 14:23:31 -0800206 mSurface.prepareFrame(false, true);
Lloyd Pique31cb2942018-10-19 17:23:03 -0700207}
208
209TEST_F(RenderSurfaceTest, prepareFrameHandlesNoComposition) {
Lloyd Pique31cb2942018-10-19 17:23:03 -0700210 EXPECT_CALL(*mDisplaySurface, prepareFrame(DisplaySurface::COMPOSITION_HWC))
211 .WillOnce(Return(NO_ERROR));
212
Lloyd Pique66d68602019-02-13 14:23:31 -0800213 mSurface.prepareFrame(false, false);
Lloyd Pique31cb2942018-10-19 17:23:03 -0700214}
215
Lloyd Pique66d68602019-02-13 14:23:31 -0800216/*
Lloyd Pique31cb2942018-10-19 17:23:03 -0700217 * RenderSurface::dequeueBuffer()
218 */
219
220TEST_F(RenderSurfaceTest, dequeueBufferObtainsABuffer) {
221 sp<GraphicBuffer> buffer = new GraphicBuffer();
222
223 EXPECT_CALL(*mNativeWindow, dequeueBuffer(_, _))
224 .WillOnce(
225 DoAll(SetArgPointee<0>(buffer.get()), SetArgPointee<1>(-1), Return(NO_ERROR)));
226
Alec Mouri6338c9d2019-02-07 16:57:51 -0800227 base::unique_fd fence;
228 EXPECT_EQ(buffer.get(), mSurface.dequeueBuffer(&fence).get());
Lloyd Pique31cb2942018-10-19 17:23:03 -0700229
230 EXPECT_EQ(buffer.get(), mSurface.mutableGraphicBufferForTest().get());
231}
232
Lloyd Pique66d68602019-02-13 14:23:31 -0800233/*
Lloyd Pique31cb2942018-10-19 17:23:03 -0700234 * RenderSurface::queueBuffer()
235 */
236
237TEST_F(RenderSurfaceTest, queueBufferHandlesNoClientComposition) {
238 sp<GraphicBuffer> buffer = new GraphicBuffer();
239 mSurface.mutableGraphicBufferForTest() = buffer;
240
Lloyd Pique66d68602019-02-13 14:23:31 -0800241 impl::OutputCompositionState state;
242 state.usesClientComposition = false;
243 state.flipClientTarget = false;
244
245 EXPECT_CALL(mDisplay, getState()).WillOnce(ReturnRef(state));
Lloyd Pique31cb2942018-10-19 17:23:03 -0700246 EXPECT_CALL(*mDisplaySurface, advanceFrame()).Times(1);
247
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000248 mSurface.queueBuffer(base::unique_fd());
Lloyd Pique31cb2942018-10-19 17:23:03 -0700249
250 EXPECT_EQ(buffer.get(), mSurface.mutableGraphicBufferForTest().get());
251}
252
253TEST_F(RenderSurfaceTest, queueBufferHandlesClientComposition) {
254 sp<GraphicBuffer> buffer = new GraphicBuffer();
255 mSurface.mutableGraphicBufferForTest() = buffer;
256
Lloyd Pique66d68602019-02-13 14:23:31 -0800257 impl::OutputCompositionState state;
258 state.usesClientComposition = true;
259 state.flipClientTarget = false;
260
261 EXPECT_CALL(mDisplay, getState()).WillOnce(ReturnRef(state));
Lloyd Pique31cb2942018-10-19 17:23:03 -0700262 EXPECT_CALL(*mNativeWindow, queueBuffer(buffer->getNativeBuffer(), -1))
263 .WillOnce(Return(NO_ERROR));
264 EXPECT_CALL(*mDisplaySurface, advanceFrame()).Times(1);
265
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000266 mSurface.queueBuffer(base::unique_fd());
Lloyd Pique31cb2942018-10-19 17:23:03 -0700267
268 EXPECT_EQ(nullptr, mSurface.mutableGraphicBufferForTest().get());
269}
270
271TEST_F(RenderSurfaceTest, queueBufferHandlesFlipClientTargetRequest) {
272 sp<GraphicBuffer> buffer = new GraphicBuffer();
273 mSurface.mutableGraphicBufferForTest() = buffer;
274
Lloyd Pique66d68602019-02-13 14:23:31 -0800275 impl::OutputCompositionState state;
276 state.usesClientComposition = false;
277 state.flipClientTarget = true;
278
279 EXPECT_CALL(mDisplay, getState()).WillOnce(ReturnRef(state));
Lloyd Pique31cb2942018-10-19 17:23:03 -0700280 EXPECT_CALL(*mNativeWindow, queueBuffer(buffer->getNativeBuffer(), -1))
281 .WillOnce(Return(NO_ERROR));
282 EXPECT_CALL(*mDisplaySurface, advanceFrame()).Times(1);
283
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000284 mSurface.queueBuffer(base::unique_fd());
Lloyd Pique31cb2942018-10-19 17:23:03 -0700285
286 EXPECT_EQ(nullptr, mSurface.mutableGraphicBufferForTest().get());
287}
288
289TEST_F(RenderSurfaceTest, queueBufferHandlesFlipClientTargetRequestWithNoBufferYetDequeued) {
290 sp<GraphicBuffer> buffer = new GraphicBuffer();
291
Lloyd Pique66d68602019-02-13 14:23:31 -0800292 impl::OutputCompositionState state;
293 state.usesClientComposition = false;
294 state.flipClientTarget = true;
295
296 EXPECT_CALL(mDisplay, getState()).WillOnce(ReturnRef(state));
Lloyd Pique31cb2942018-10-19 17:23:03 -0700297 EXPECT_CALL(*mNativeWindow, dequeueBuffer(_, _))
298 .WillOnce(
299 DoAll(SetArgPointee<0>(buffer.get()), SetArgPointee<1>(-1), Return(NO_ERROR)));
300 EXPECT_CALL(*mNativeWindow, queueBuffer(buffer->getNativeBuffer(), -1))
301 .WillOnce(Return(NO_ERROR));
302 EXPECT_CALL(*mDisplaySurface, advanceFrame()).Times(1);
303
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000304 mSurface.queueBuffer(base::unique_fd());
Lloyd Pique31cb2942018-10-19 17:23:03 -0700305
306 EXPECT_EQ(nullptr, mSurface.mutableGraphicBufferForTest().get());
307}
308
309TEST_F(RenderSurfaceTest, queueBufferHandlesNativeWindowQueueBufferFailureOnVirtualDisplay) {
310 sp<GraphicBuffer> buffer = new GraphicBuffer();
311 mSurface.mutableGraphicBufferForTest() = buffer;
312
Lloyd Pique66d68602019-02-13 14:23:31 -0800313 impl::OutputCompositionState state;
314 state.usesClientComposition = true;
315
316 EXPECT_CALL(mDisplay, getState()).WillOnce(ReturnRef(state));
Lloyd Pique31cb2942018-10-19 17:23:03 -0700317 EXPECT_CALL(*mNativeWindow, queueBuffer(buffer->getNativeBuffer(), -1))
318 .WillOnce(Return(INVALID_OPERATION));
319 EXPECT_CALL(mDisplay, isVirtual()).WillOnce(Return(true));
320 EXPECT_CALL(*mNativeWindow, cancelBuffer(buffer->getNativeBuffer(), -1))
321 .WillOnce(Return(NO_ERROR));
322 EXPECT_CALL(*mDisplaySurface, advanceFrame()).Times(1);
323
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000324 mSurface.queueBuffer(base::unique_fd());
Lloyd Pique31cb2942018-10-19 17:23:03 -0700325
326 EXPECT_EQ(nullptr, mSurface.mutableGraphicBufferForTest().get());
327}
328
Lloyd Pique66d68602019-02-13 14:23:31 -0800329/*
Lloyd Pique31cb2942018-10-19 17:23:03 -0700330 * RenderSurface::onPresentDisplayCompleted()
331 */
332
333TEST_F(RenderSurfaceTest, onPresentDisplayCompletedForwardsSignal) {
334 EXPECT_CALL(*mDisplaySurface, onFrameCommitted()).Times(1);
335
336 mSurface.onPresentDisplayCompleted();
337}
338
Lloyd Pique66d68602019-02-13 14:23:31 -0800339/*
Lloyd Pique31cb2942018-10-19 17:23:03 -0700340 * RenderSurface::flip()
341 */
342
343TEST_F(RenderSurfaceTest, flipForwardsSignal) {
344 mSurface.setPageFlipCountForTest(500);
345
346 mSurface.flip();
347
348 EXPECT_EQ(501, mSurface.getPageFlipCount());
349}
350
351} // namespace
352} // namespace android::compositionengine