blob: 6ce8a6b2eae80de4e05a4d6e11d24bb9f98dd491 [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;
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +020036constexpr DisplayId DEFAULT_DISPLAY_ID = PhysicalDisplayId(123u);
Lloyd Pique31cb2942018-10-19 17:23:03 -070037const 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() {
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +020051 EXPECT_CALL(mDisplay, getId()).WillRepeatedly(Return(DEFAULT_DISPLAY_ID));
Lloyd Pique31cb2942018-10-19 17:23:03 -070052 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));
John Reck44418f52020-09-15 18:02:17 -070085 EXPECT_CALL(*mNativeWindow, setUsage(GRALLOC_USAGE_HW_RENDER | GRALLOC_USAGE_HW_TEXTURE))
86 .WillOnce(Return(NO_ERROR));
Lloyd Pique31cb2942018-10-19 17:23:03 -070087
88 mSurface.initialize();
89}
90
Lloyd Pique66d68602019-02-13 14:23:31 -080091/*
Lloyd Pique31cb2942018-10-19 17:23:03 -070092 * RenderSurface::getSize()
93 */
94
95TEST_F(RenderSurfaceTest, sizeReturnsConstructedSize) {
96 const ui::Size expected{DEFAULT_DISPLAY_WIDTH, DEFAULT_DISPLAY_HEIGHT};
97
98 EXPECT_EQ(expected, mSurface.getSize());
99}
100
Lloyd Pique66d68602019-02-13 14:23:31 -0800101/*
Lloyd Pique31cb2942018-10-19 17:23:03 -0700102 * RenderSurface::getClientTargetAcquireFence()
103 */
104
105TEST_F(RenderSurfaceTest, getClientTargetAcquireFenceForwardsCall) {
106 sp<Fence> fence = new Fence();
107
108 EXPECT_CALL(*mDisplaySurface, getClientTargetAcquireFence()).WillOnce(ReturnRef(fence));
109
110 EXPECT_EQ(fence.get(), mSurface.getClientTargetAcquireFence().get());
111}
112
Lloyd Pique66d68602019-02-13 14:23:31 -0800113/*
Lloyd Pique31cb2942018-10-19 17:23:03 -0700114 * RenderSurface::setDisplaySize()
115 */
116
117TEST_F(RenderSurfaceTest, setDisplaySizeAppliesChange) {
118 EXPECT_CALL(*mDisplaySurface, resizeBuffers(640, 480)).Times(1);
119
120 mSurface.setDisplaySize(ui::Size(640, 480));
121}
122
Lloyd Pique66d68602019-02-13 14:23:31 -0800123/*
Lloyd Pique31cb2942018-10-19 17:23:03 -0700124 * RenderSurface::setBufferDataspace()
125 */
126
127TEST_F(RenderSurfaceTest, setBufferDataspaceAppliesChange) {
128 EXPECT_CALL(*mNativeWindow, setBuffersDataSpace(ui::Dataspace::DISPLAY_P3))
129 .WillOnce(Return(NO_ERROR));
130
131 mSurface.setBufferDataspace(ui::Dataspace::DISPLAY_P3);
132}
133
Lloyd Pique66d68602019-02-13 14:23:31 -0800134/*
Lloyd Pique31cb2942018-10-19 17:23:03 -0700135 * RenderSurface::setProtected()
136 */
137
138TEST_F(RenderSurfaceTest, setProtectedTrueEnablesProtection) {
Peiyong Lin52010312019-05-02 14:22:16 -0700139 EXPECT_FALSE(mSurface.isProtected());
John Reck44418f52020-09-15 18:02:17 -0700140 EXPECT_CALL(*mNativeWindow,
141 setUsage(GRALLOC_USAGE_HW_RENDER | GRALLOC_USAGE_HW_TEXTURE |
142 GRALLOC_USAGE_PROTECTED))
Lloyd Pique31cb2942018-10-19 17:23:03 -0700143 .WillOnce(Return(NO_ERROR));
144
145 mSurface.setProtected(true);
Peiyong Lin52010312019-05-02 14:22:16 -0700146 EXPECT_TRUE(mSurface.isProtected());
Lloyd Pique31cb2942018-10-19 17:23:03 -0700147}
148
149TEST_F(RenderSurfaceTest, setProtectedFalseDisablesProtection) {
Peiyong Lin52010312019-05-02 14:22:16 -0700150 EXPECT_FALSE(mSurface.isProtected());
John Reck44418f52020-09-15 18:02:17 -0700151 EXPECT_CALL(*mNativeWindow, setUsage(GRALLOC_USAGE_HW_RENDER | GRALLOC_USAGE_HW_TEXTURE))
152 .WillOnce(Return(NO_ERROR));
Lloyd Pique31cb2942018-10-19 17:23:03 -0700153
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());
John Reck44418f52020-09-15 18:02:17 -0700160 EXPECT_CALL(*mNativeWindow,
161 setUsage(GRALLOC_USAGE_HW_RENDER | GRALLOC_USAGE_HW_TEXTURE |
162 GRALLOC_USAGE_PROTECTED))
Peiyong Lin52010312019-05-02 14:22:16 -0700163 .WillOnce(Return(NO_ERROR));
John Reck44418f52020-09-15 18:02:17 -0700164 EXPECT_CALL(*mNativeWindow, setUsage(GRALLOC_USAGE_HW_RENDER | GRALLOC_USAGE_HW_TEXTURE))
165 .WillOnce(Return(NO_ERROR));
Peiyong Lin52010312019-05-02 14:22:16 -0700166
167 mSurface.setProtected(true);
168 EXPECT_TRUE(mSurface.isProtected());
169 mSurface.setProtected(false);
170 EXPECT_FALSE(mSurface.isProtected());
171}
172
173TEST_F(RenderSurfaceTest, setProtectedEnableWithError) {
174 EXPECT_FALSE(mSurface.isProtected());
John Reck44418f52020-09-15 18:02:17 -0700175 EXPECT_CALL(*mNativeWindow,
176 setUsage(GRALLOC_USAGE_HW_RENDER | GRALLOC_USAGE_HW_TEXTURE |
177 GRALLOC_USAGE_PROTECTED))
Peiyong Lin52010312019-05-02 14:22:16 -0700178 .WillOnce(Return(INVALID_OPERATION));
179 mSurface.setProtected(true);
180 EXPECT_FALSE(mSurface.isProtected());
Lloyd Pique31cb2942018-10-19 17:23:03 -0700181}
182
Lloyd Pique66d68602019-02-13 14:23:31 -0800183/*
Lloyd Pique31cb2942018-10-19 17:23:03 -0700184 * RenderSurface::beginFrame()
185 */
186
187TEST_F(RenderSurfaceTest, beginFrameAppliesChange) {
188 EXPECT_CALL(*mDisplaySurface, beginFrame(true)).WillOnce(Return(NO_ERROR));
189
190 EXPECT_EQ(NO_ERROR, mSurface.beginFrame(true));
191}
192
Lloyd Pique66d68602019-02-13 14:23:31 -0800193/*
Lloyd Pique31cb2942018-10-19 17:23:03 -0700194 * RenderSurface::prepareFrame()
195 */
196
Lloyd Pique31cb2942018-10-19 17:23:03 -0700197TEST_F(RenderSurfaceTest, prepareFrameHandlesMixedComposition) {
Lloyd Pique31cb2942018-10-19 17:23:03 -0700198 EXPECT_CALL(*mDisplaySurface, prepareFrame(DisplaySurface::COMPOSITION_MIXED))
Lloyd Pique66d68602019-02-13 14:23:31 -0800199 .WillOnce(Return(NO_ERROR));
Lloyd Pique31cb2942018-10-19 17:23:03 -0700200
Lloyd Pique66d68602019-02-13 14:23:31 -0800201 mSurface.prepareFrame(true, true);
Lloyd Pique31cb2942018-10-19 17:23:03 -0700202}
203
Peiyong Linf3ffc4e2019-12-13 00:46:24 -0800204TEST_F(RenderSurfaceTest, prepareFrameHandlesOnlyGpuComposition) {
205 EXPECT_CALL(*mDisplaySurface, prepareFrame(DisplaySurface::COMPOSITION_GPU))
Lloyd Pique31cb2942018-10-19 17:23:03 -0700206 .WillOnce(Return(NO_ERROR));
207
Lloyd Pique66d68602019-02-13 14:23:31 -0800208 mSurface.prepareFrame(true, false);
Lloyd Pique31cb2942018-10-19 17:23:03 -0700209}
210
211TEST_F(RenderSurfaceTest, prepareFrameHandlesOnlyHwcComposition) {
Lloyd Pique31cb2942018-10-19 17:23:03 -0700212 EXPECT_CALL(*mDisplaySurface, prepareFrame(DisplaySurface::COMPOSITION_HWC))
213 .WillOnce(Return(NO_ERROR));
214
Lloyd Pique66d68602019-02-13 14:23:31 -0800215 mSurface.prepareFrame(false, true);
Lloyd Pique31cb2942018-10-19 17:23:03 -0700216}
217
218TEST_F(RenderSurfaceTest, prepareFrameHandlesNoComposition) {
Lloyd Pique31cb2942018-10-19 17:23:03 -0700219 EXPECT_CALL(*mDisplaySurface, prepareFrame(DisplaySurface::COMPOSITION_HWC))
220 .WillOnce(Return(NO_ERROR));
221
Lloyd Pique66d68602019-02-13 14:23:31 -0800222 mSurface.prepareFrame(false, false);
Lloyd Pique31cb2942018-10-19 17:23:03 -0700223}
224
Lloyd Pique66d68602019-02-13 14:23:31 -0800225/*
Lloyd Pique31cb2942018-10-19 17:23:03 -0700226 * RenderSurface::dequeueBuffer()
227 */
228
229TEST_F(RenderSurfaceTest, dequeueBufferObtainsABuffer) {
230 sp<GraphicBuffer> buffer = new GraphicBuffer();
231
232 EXPECT_CALL(*mNativeWindow, dequeueBuffer(_, _))
233 .WillOnce(
234 DoAll(SetArgPointee<0>(buffer.get()), SetArgPointee<1>(-1), Return(NO_ERROR)));
235
Alec Mouri6338c9d2019-02-07 16:57:51 -0800236 base::unique_fd fence;
237 EXPECT_EQ(buffer.get(), mSurface.dequeueBuffer(&fence).get());
Lloyd Pique31cb2942018-10-19 17:23:03 -0700238
239 EXPECT_EQ(buffer.get(), mSurface.mutableGraphicBufferForTest().get());
240}
241
Lloyd Pique66d68602019-02-13 14:23:31 -0800242/*
Lloyd Pique31cb2942018-10-19 17:23:03 -0700243 * RenderSurface::queueBuffer()
244 */
245
246TEST_F(RenderSurfaceTest, queueBufferHandlesNoClientComposition) {
247 sp<GraphicBuffer> buffer = new GraphicBuffer();
248 mSurface.mutableGraphicBufferForTest() = buffer;
249
Lloyd Pique66d68602019-02-13 14:23:31 -0800250 impl::OutputCompositionState state;
251 state.usesClientComposition = false;
252 state.flipClientTarget = false;
253
254 EXPECT_CALL(mDisplay, getState()).WillOnce(ReturnRef(state));
Lloyd Pique31cb2942018-10-19 17:23:03 -0700255 EXPECT_CALL(*mDisplaySurface, advanceFrame()).Times(1);
256
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000257 mSurface.queueBuffer(base::unique_fd());
Lloyd Pique31cb2942018-10-19 17:23:03 -0700258
259 EXPECT_EQ(buffer.get(), mSurface.mutableGraphicBufferForTest().get());
260}
261
262TEST_F(RenderSurfaceTest, queueBufferHandlesClientComposition) {
263 sp<GraphicBuffer> buffer = new GraphicBuffer();
264 mSurface.mutableGraphicBufferForTest() = buffer;
265
Lloyd Pique66d68602019-02-13 14:23:31 -0800266 impl::OutputCompositionState state;
267 state.usesClientComposition = true;
268 state.flipClientTarget = false;
269
270 EXPECT_CALL(mDisplay, getState()).WillOnce(ReturnRef(state));
Lloyd Pique31cb2942018-10-19 17:23:03 -0700271 EXPECT_CALL(*mNativeWindow, queueBuffer(buffer->getNativeBuffer(), -1))
272 .WillOnce(Return(NO_ERROR));
273 EXPECT_CALL(*mDisplaySurface, advanceFrame()).Times(1);
274
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000275 mSurface.queueBuffer(base::unique_fd());
Lloyd Pique31cb2942018-10-19 17:23:03 -0700276
277 EXPECT_EQ(nullptr, mSurface.mutableGraphicBufferForTest().get());
278}
279
280TEST_F(RenderSurfaceTest, queueBufferHandlesFlipClientTargetRequest) {
281 sp<GraphicBuffer> buffer = new GraphicBuffer();
282 mSurface.mutableGraphicBufferForTest() = buffer;
283
Lloyd Pique66d68602019-02-13 14:23:31 -0800284 impl::OutputCompositionState state;
285 state.usesClientComposition = false;
286 state.flipClientTarget = true;
287
288 EXPECT_CALL(mDisplay, getState()).WillOnce(ReturnRef(state));
Lloyd Pique31cb2942018-10-19 17:23:03 -0700289 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
Lloyd Pique66d68602019-02-13 14:23:31 -0800301 impl::OutputCompositionState state;
302 state.usesClientComposition = false;
303 state.flipClientTarget = true;
304
305 EXPECT_CALL(mDisplay, getState()).WillOnce(ReturnRef(state));
Lloyd Pique31cb2942018-10-19 17:23:03 -0700306 EXPECT_CALL(*mNativeWindow, dequeueBuffer(_, _))
307 .WillOnce(
308 DoAll(SetArgPointee<0>(buffer.get()), SetArgPointee<1>(-1), Return(NO_ERROR)));
309 EXPECT_CALL(*mNativeWindow, queueBuffer(buffer->getNativeBuffer(), -1))
310 .WillOnce(Return(NO_ERROR));
311 EXPECT_CALL(*mDisplaySurface, advanceFrame()).Times(1);
312
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000313 mSurface.queueBuffer(base::unique_fd());
Lloyd Pique31cb2942018-10-19 17:23:03 -0700314
315 EXPECT_EQ(nullptr, mSurface.mutableGraphicBufferForTest().get());
316}
317
318TEST_F(RenderSurfaceTest, queueBufferHandlesNativeWindowQueueBufferFailureOnVirtualDisplay) {
319 sp<GraphicBuffer> buffer = new GraphicBuffer();
320 mSurface.mutableGraphicBufferForTest() = buffer;
321
Lloyd Pique66d68602019-02-13 14:23:31 -0800322 impl::OutputCompositionState state;
323 state.usesClientComposition = true;
324
325 EXPECT_CALL(mDisplay, getState()).WillOnce(ReturnRef(state));
Lloyd Pique31cb2942018-10-19 17:23:03 -0700326 EXPECT_CALL(*mNativeWindow, queueBuffer(buffer->getNativeBuffer(), -1))
327 .WillOnce(Return(INVALID_OPERATION));
328 EXPECT_CALL(mDisplay, isVirtual()).WillOnce(Return(true));
329 EXPECT_CALL(*mNativeWindow, cancelBuffer(buffer->getNativeBuffer(), -1))
330 .WillOnce(Return(NO_ERROR));
331 EXPECT_CALL(*mDisplaySurface, advanceFrame()).Times(1);
332
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000333 mSurface.queueBuffer(base::unique_fd());
Lloyd Pique31cb2942018-10-19 17:23:03 -0700334
335 EXPECT_EQ(nullptr, mSurface.mutableGraphicBufferForTest().get());
336}
337
Lloyd Pique66d68602019-02-13 14:23:31 -0800338/*
Lloyd Pique31cb2942018-10-19 17:23:03 -0700339 * RenderSurface::onPresentDisplayCompleted()
340 */
341
342TEST_F(RenderSurfaceTest, onPresentDisplayCompletedForwardsSignal) {
343 EXPECT_CALL(*mDisplaySurface, onFrameCommitted()).Times(1);
344
345 mSurface.onPresentDisplayCompleted();
346}
347
Lloyd Pique66d68602019-02-13 14:23:31 -0800348/*
Lloyd Pique31cb2942018-10-19 17:23:03 -0700349 * RenderSurface::flip()
350 */
351
352TEST_F(RenderSurfaceTest, flipForwardsSignal) {
353 mSurface.setPageFlipCountForTest(500);
354
355 mSurface.flip();
356
357 EXPECT_EQ(501, mSurface.getPageFlipCount());
358}
359
360} // namespace
361} // namespace android::compositionengine