blob: 5ef5d7b5cba9d230478741cf38ea20f46e4058c0 [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
Marin Shalamanovbed7fd32020-12-21 20:02:20 +010017// TODO(b/129481165): remove the #pragma below and fix conversion issues
18#pragma clang diagnostic push
19#pragma clang diagnostic ignored "-Wextra"
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;
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +020040constexpr DisplayId DEFAULT_DISPLAY_ID = PhysicalDisplayId(123u);
Lloyd Pique31cb2942018-10-19 17:23:03 -070041const 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() {
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +020055 EXPECT_CALL(mDisplay, getId()).WillRepeatedly(Return(DEFAULT_DISPLAY_ID));
Lloyd Pique31cb2942018-10-19 17:23:03 -070056 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));
John Reck44418f52020-09-15 18:02:17 -070089 EXPECT_CALL(*mNativeWindow, setUsage(GRALLOC_USAGE_HW_RENDER | GRALLOC_USAGE_HW_TEXTURE))
90 .WillOnce(Return(NO_ERROR));
Lloyd Pique31cb2942018-10-19 17:23:03 -070091
92 mSurface.initialize();
93}
94
Lloyd Pique66d68602019-02-13 14:23:31 -080095/*
Lloyd Pique31cb2942018-10-19 17:23:03 -070096 * RenderSurface::getSize()
97 */
98
99TEST_F(RenderSurfaceTest, sizeReturnsConstructedSize) {
100 const ui::Size expected{DEFAULT_DISPLAY_WIDTH, DEFAULT_DISPLAY_HEIGHT};
101
102 EXPECT_EQ(expected, mSurface.getSize());
103}
104
Lloyd Pique66d68602019-02-13 14:23:31 -0800105/*
Lloyd Pique31cb2942018-10-19 17:23:03 -0700106 * RenderSurface::getClientTargetAcquireFence()
107 */
108
109TEST_F(RenderSurfaceTest, getClientTargetAcquireFenceForwardsCall) {
110 sp<Fence> fence = new Fence();
111
112 EXPECT_CALL(*mDisplaySurface, getClientTargetAcquireFence()).WillOnce(ReturnRef(fence));
113
114 EXPECT_EQ(fence.get(), mSurface.getClientTargetAcquireFence().get());
115}
116
Lloyd Pique66d68602019-02-13 14:23:31 -0800117/*
Lloyd Pique31cb2942018-10-19 17:23:03 -0700118 * RenderSurface::setDisplaySize()
119 */
120
121TEST_F(RenderSurfaceTest, setDisplaySizeAppliesChange) {
Marin Shalamanov045b7002021-01-07 16:56:24 +0100122 const ui::Size size(640, 480);
123 EXPECT_CALL(*mDisplaySurface, resizeBuffers(size)).Times(1);
Lloyd Pique31cb2942018-10-19 17:23:03 -0700124
Marin Shalamanov045b7002021-01-07 16:56:24 +0100125 mSurface.setDisplaySize(size);
Lloyd Pique31cb2942018-10-19 17:23:03 -0700126}
127
Lloyd Pique66d68602019-02-13 14:23:31 -0800128/*
Lloyd Pique31cb2942018-10-19 17:23:03 -0700129 * RenderSurface::setBufferDataspace()
130 */
131
132TEST_F(RenderSurfaceTest, setBufferDataspaceAppliesChange) {
133 EXPECT_CALL(*mNativeWindow, setBuffersDataSpace(ui::Dataspace::DISPLAY_P3))
134 .WillOnce(Return(NO_ERROR));
135
136 mSurface.setBufferDataspace(ui::Dataspace::DISPLAY_P3);
137}
138
Lloyd Pique66d68602019-02-13 14:23:31 -0800139/*
Lloyd Pique31cb2942018-10-19 17:23:03 -0700140 * RenderSurface::setProtected()
141 */
142
143TEST_F(RenderSurfaceTest, setProtectedTrueEnablesProtection) {
Peiyong Lin52010312019-05-02 14:22:16 -0700144 EXPECT_FALSE(mSurface.isProtected());
John Reck44418f52020-09-15 18:02:17 -0700145 EXPECT_CALL(*mNativeWindow,
146 setUsage(GRALLOC_USAGE_HW_RENDER | GRALLOC_USAGE_HW_TEXTURE |
147 GRALLOC_USAGE_PROTECTED))
Lloyd Pique31cb2942018-10-19 17:23:03 -0700148 .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());
John Reck44418f52020-09-15 18:02:17 -0700156 EXPECT_CALL(*mNativeWindow, setUsage(GRALLOC_USAGE_HW_RENDER | GRALLOC_USAGE_HW_TEXTURE))
157 .WillOnce(Return(NO_ERROR));
Lloyd Pique31cb2942018-10-19 17:23:03 -0700158
159 mSurface.setProtected(false);
Peiyong Lin52010312019-05-02 14:22:16 -0700160 EXPECT_FALSE(mSurface.isProtected());
161}
162
163TEST_F(RenderSurfaceTest, setProtectedEnableAndDisable) {
164 EXPECT_FALSE(mSurface.isProtected());
John Reck44418f52020-09-15 18:02:17 -0700165 EXPECT_CALL(*mNativeWindow,
166 setUsage(GRALLOC_USAGE_HW_RENDER | GRALLOC_USAGE_HW_TEXTURE |
167 GRALLOC_USAGE_PROTECTED))
Peiyong Lin52010312019-05-02 14:22:16 -0700168 .WillOnce(Return(NO_ERROR));
John Reck44418f52020-09-15 18:02:17 -0700169 EXPECT_CALL(*mNativeWindow, setUsage(GRALLOC_USAGE_HW_RENDER | GRALLOC_USAGE_HW_TEXTURE))
170 .WillOnce(Return(NO_ERROR));
Peiyong Lin52010312019-05-02 14:22:16 -0700171
172 mSurface.setProtected(true);
173 EXPECT_TRUE(mSurface.isProtected());
174 mSurface.setProtected(false);
175 EXPECT_FALSE(mSurface.isProtected());
176}
177
178TEST_F(RenderSurfaceTest, setProtectedEnableWithError) {
179 EXPECT_FALSE(mSurface.isProtected());
John Reck44418f52020-09-15 18:02:17 -0700180 EXPECT_CALL(*mNativeWindow,
181 setUsage(GRALLOC_USAGE_HW_RENDER | GRALLOC_USAGE_HW_TEXTURE |
182 GRALLOC_USAGE_PROTECTED))
Peiyong Lin52010312019-05-02 14:22:16 -0700183 .WillOnce(Return(INVALID_OPERATION));
184 mSurface.setProtected(true);
185 EXPECT_FALSE(mSurface.isProtected());
Lloyd Pique31cb2942018-10-19 17:23:03 -0700186}
187
Lloyd Pique66d68602019-02-13 14:23:31 -0800188/*
Lloyd Pique31cb2942018-10-19 17:23:03 -0700189 * RenderSurface::beginFrame()
190 */
191
192TEST_F(RenderSurfaceTest, beginFrameAppliesChange) {
193 EXPECT_CALL(*mDisplaySurface, beginFrame(true)).WillOnce(Return(NO_ERROR));
194
195 EXPECT_EQ(NO_ERROR, mSurface.beginFrame(true));
196}
197
Lloyd Pique66d68602019-02-13 14:23:31 -0800198/*
Lloyd Pique31cb2942018-10-19 17:23:03 -0700199 * RenderSurface::prepareFrame()
200 */
201
Lloyd Pique31cb2942018-10-19 17:23:03 -0700202TEST_F(RenderSurfaceTest, prepareFrameHandlesMixedComposition) {
Lloyd Pique31cb2942018-10-19 17:23:03 -0700203 EXPECT_CALL(*mDisplaySurface, prepareFrame(DisplaySurface::COMPOSITION_MIXED))
Lloyd Pique66d68602019-02-13 14:23:31 -0800204 .WillOnce(Return(NO_ERROR));
Lloyd Pique31cb2942018-10-19 17:23:03 -0700205
Lloyd Pique66d68602019-02-13 14:23:31 -0800206 mSurface.prepareFrame(true, true);
Lloyd Pique31cb2942018-10-19 17:23:03 -0700207}
208
Peiyong Linf3ffc4e2019-12-13 00:46:24 -0800209TEST_F(RenderSurfaceTest, prepareFrameHandlesOnlyGpuComposition) {
210 EXPECT_CALL(*mDisplaySurface, prepareFrame(DisplaySurface::COMPOSITION_GPU))
Lloyd Pique31cb2942018-10-19 17:23:03 -0700211 .WillOnce(Return(NO_ERROR));
212
Lloyd Pique66d68602019-02-13 14:23:31 -0800213 mSurface.prepareFrame(true, false);
Lloyd Pique31cb2942018-10-19 17:23:03 -0700214}
215
216TEST_F(RenderSurfaceTest, prepareFrameHandlesOnlyHwcComposition) {
Lloyd Pique31cb2942018-10-19 17:23:03 -0700217 EXPECT_CALL(*mDisplaySurface, prepareFrame(DisplaySurface::COMPOSITION_HWC))
218 .WillOnce(Return(NO_ERROR));
219
Lloyd Pique66d68602019-02-13 14:23:31 -0800220 mSurface.prepareFrame(false, true);
Lloyd Pique31cb2942018-10-19 17:23:03 -0700221}
222
223TEST_F(RenderSurfaceTest, prepareFrameHandlesNoComposition) {
Lloyd Pique31cb2942018-10-19 17:23:03 -0700224 EXPECT_CALL(*mDisplaySurface, prepareFrame(DisplaySurface::COMPOSITION_HWC))
225 .WillOnce(Return(NO_ERROR));
226
Lloyd Pique66d68602019-02-13 14:23:31 -0800227 mSurface.prepareFrame(false, false);
Lloyd Pique31cb2942018-10-19 17:23:03 -0700228}
229
Lloyd Pique66d68602019-02-13 14:23:31 -0800230/*
Lloyd Pique31cb2942018-10-19 17:23:03 -0700231 * RenderSurface::dequeueBuffer()
232 */
233
234TEST_F(RenderSurfaceTest, dequeueBufferObtainsABuffer) {
235 sp<GraphicBuffer> buffer = new GraphicBuffer();
236
237 EXPECT_CALL(*mNativeWindow, dequeueBuffer(_, _))
238 .WillOnce(
239 DoAll(SetArgPointee<0>(buffer.get()), SetArgPointee<1>(-1), Return(NO_ERROR)));
240
Alec Mouri6338c9d2019-02-07 16:57:51 -0800241 base::unique_fd fence;
242 EXPECT_EQ(buffer.get(), mSurface.dequeueBuffer(&fence).get());
Lloyd Pique31cb2942018-10-19 17:23:03 -0700243
244 EXPECT_EQ(buffer.get(), mSurface.mutableGraphicBufferForTest().get());
245}
246
Lloyd Pique66d68602019-02-13 14:23:31 -0800247/*
Lloyd Pique31cb2942018-10-19 17:23:03 -0700248 * RenderSurface::queueBuffer()
249 */
250
251TEST_F(RenderSurfaceTest, queueBufferHandlesNoClientComposition) {
252 sp<GraphicBuffer> buffer = new GraphicBuffer();
253 mSurface.mutableGraphicBufferForTest() = buffer;
254
Lloyd Pique66d68602019-02-13 14:23:31 -0800255 impl::OutputCompositionState state;
256 state.usesClientComposition = false;
257 state.flipClientTarget = false;
258
259 EXPECT_CALL(mDisplay, getState()).WillOnce(ReturnRef(state));
Lloyd Pique31cb2942018-10-19 17:23:03 -0700260 EXPECT_CALL(*mDisplaySurface, advanceFrame()).Times(1);
261
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000262 mSurface.queueBuffer(base::unique_fd());
Lloyd Pique31cb2942018-10-19 17:23:03 -0700263
264 EXPECT_EQ(buffer.get(), mSurface.mutableGraphicBufferForTest().get());
265}
266
267TEST_F(RenderSurfaceTest, queueBufferHandlesClientComposition) {
268 sp<GraphicBuffer> buffer = new GraphicBuffer();
269 mSurface.mutableGraphicBufferForTest() = buffer;
270
Lloyd Pique66d68602019-02-13 14:23:31 -0800271 impl::OutputCompositionState state;
272 state.usesClientComposition = true;
273 state.flipClientTarget = false;
274
275 EXPECT_CALL(mDisplay, getState()).WillOnce(ReturnRef(state));
Lloyd Pique31cb2942018-10-19 17:23:03 -0700276 EXPECT_CALL(*mNativeWindow, queueBuffer(buffer->getNativeBuffer(), -1))
277 .WillOnce(Return(NO_ERROR));
278 EXPECT_CALL(*mDisplaySurface, advanceFrame()).Times(1);
279
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000280 mSurface.queueBuffer(base::unique_fd());
Lloyd Pique31cb2942018-10-19 17:23:03 -0700281
282 EXPECT_EQ(nullptr, mSurface.mutableGraphicBufferForTest().get());
283}
284
285TEST_F(RenderSurfaceTest, queueBufferHandlesFlipClientTargetRequest) {
286 sp<GraphicBuffer> buffer = new GraphicBuffer();
287 mSurface.mutableGraphicBufferForTest() = buffer;
288
Lloyd Pique66d68602019-02-13 14:23:31 -0800289 impl::OutputCompositionState state;
290 state.usesClientComposition = false;
291 state.flipClientTarget = true;
292
293 EXPECT_CALL(mDisplay, getState()).WillOnce(ReturnRef(state));
Lloyd Pique31cb2942018-10-19 17:23:03 -0700294 EXPECT_CALL(*mNativeWindow, queueBuffer(buffer->getNativeBuffer(), -1))
295 .WillOnce(Return(NO_ERROR));
296 EXPECT_CALL(*mDisplaySurface, advanceFrame()).Times(1);
297
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000298 mSurface.queueBuffer(base::unique_fd());
Lloyd Pique31cb2942018-10-19 17:23:03 -0700299
300 EXPECT_EQ(nullptr, mSurface.mutableGraphicBufferForTest().get());
301}
302
303TEST_F(RenderSurfaceTest, queueBufferHandlesFlipClientTargetRequestWithNoBufferYetDequeued) {
304 sp<GraphicBuffer> buffer = new GraphicBuffer();
305
Lloyd Pique66d68602019-02-13 14:23:31 -0800306 impl::OutputCompositionState state;
307 state.usesClientComposition = false;
308 state.flipClientTarget = true;
309
310 EXPECT_CALL(mDisplay, getState()).WillOnce(ReturnRef(state));
Lloyd Pique31cb2942018-10-19 17:23:03 -0700311 EXPECT_CALL(*mNativeWindow, dequeueBuffer(_, _))
312 .WillOnce(
313 DoAll(SetArgPointee<0>(buffer.get()), SetArgPointee<1>(-1), Return(NO_ERROR)));
314 EXPECT_CALL(*mNativeWindow, queueBuffer(buffer->getNativeBuffer(), -1))
315 .WillOnce(Return(NO_ERROR));
316 EXPECT_CALL(*mDisplaySurface, advanceFrame()).Times(1);
317
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000318 mSurface.queueBuffer(base::unique_fd());
Lloyd Pique31cb2942018-10-19 17:23:03 -0700319
320 EXPECT_EQ(nullptr, mSurface.mutableGraphicBufferForTest().get());
321}
322
323TEST_F(RenderSurfaceTest, queueBufferHandlesNativeWindowQueueBufferFailureOnVirtualDisplay) {
324 sp<GraphicBuffer> buffer = new GraphicBuffer();
325 mSurface.mutableGraphicBufferForTest() = buffer;
326
Lloyd Pique66d68602019-02-13 14:23:31 -0800327 impl::OutputCompositionState state;
328 state.usesClientComposition = true;
329
330 EXPECT_CALL(mDisplay, getState()).WillOnce(ReturnRef(state));
Lloyd Pique31cb2942018-10-19 17:23:03 -0700331 EXPECT_CALL(*mNativeWindow, queueBuffer(buffer->getNativeBuffer(), -1))
332 .WillOnce(Return(INVALID_OPERATION));
333 EXPECT_CALL(mDisplay, isVirtual()).WillOnce(Return(true));
334 EXPECT_CALL(*mNativeWindow, cancelBuffer(buffer->getNativeBuffer(), -1))
335 .WillOnce(Return(NO_ERROR));
336 EXPECT_CALL(*mDisplaySurface, advanceFrame()).Times(1);
337
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000338 mSurface.queueBuffer(base::unique_fd());
Lloyd Pique31cb2942018-10-19 17:23:03 -0700339
340 EXPECT_EQ(nullptr, mSurface.mutableGraphicBufferForTest().get());
341}
342
Lloyd Pique66d68602019-02-13 14:23:31 -0800343/*
Lloyd Pique31cb2942018-10-19 17:23:03 -0700344 * RenderSurface::onPresentDisplayCompleted()
345 */
346
347TEST_F(RenderSurfaceTest, onPresentDisplayCompletedForwardsSignal) {
348 EXPECT_CALL(*mDisplaySurface, onFrameCommitted()).Times(1);
349
350 mSurface.onPresentDisplayCompleted();
351}
352
Lloyd Pique66d68602019-02-13 14:23:31 -0800353/*
Lloyd Pique31cb2942018-10-19 17:23:03 -0700354 * RenderSurface::flip()
355 */
356
357TEST_F(RenderSurfaceTest, flipForwardsSignal) {
358 mSurface.setPageFlipCountForTest(500);
359
360 mSurface.flip();
361
362 EXPECT_EQ(501, mSurface.getPageFlipCount());
363}
364
365} // namespace
366} // namespace android::compositionengine
Marin Shalamanovbed7fd32020-12-21 20:02:20 +0100367
368// TODO(b/129481165): remove the #pragma below and fix conversion issues
369#pragma clang diagnostic pop // ignored "-Wextra"