blob: cd3973383921e8390ee97297c5295770161e80f7 [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) {
122 EXPECT_CALL(*mDisplaySurface, resizeBuffers(640, 480)).Times(1);
123
124 mSurface.setDisplaySize(ui::Size(640, 480));
125}
126
Lloyd Pique66d68602019-02-13 14:23:31 -0800127/*
Lloyd Pique31cb2942018-10-19 17:23:03 -0700128 * RenderSurface::setBufferDataspace()
129 */
130
131TEST_F(RenderSurfaceTest, setBufferDataspaceAppliesChange) {
132 EXPECT_CALL(*mNativeWindow, setBuffersDataSpace(ui::Dataspace::DISPLAY_P3))
133 .WillOnce(Return(NO_ERROR));
134
135 mSurface.setBufferDataspace(ui::Dataspace::DISPLAY_P3);
136}
137
Lloyd Pique66d68602019-02-13 14:23:31 -0800138/*
Lloyd Pique31cb2942018-10-19 17:23:03 -0700139 * RenderSurface::setProtected()
140 */
141
142TEST_F(RenderSurfaceTest, setProtectedTrueEnablesProtection) {
Peiyong Lin52010312019-05-02 14:22:16 -0700143 EXPECT_FALSE(mSurface.isProtected());
John Reck44418f52020-09-15 18:02:17 -0700144 EXPECT_CALL(*mNativeWindow,
145 setUsage(GRALLOC_USAGE_HW_RENDER | GRALLOC_USAGE_HW_TEXTURE |
146 GRALLOC_USAGE_PROTECTED))
Lloyd Pique31cb2942018-10-19 17:23:03 -0700147 .WillOnce(Return(NO_ERROR));
148
149 mSurface.setProtected(true);
Peiyong Lin52010312019-05-02 14:22:16 -0700150 EXPECT_TRUE(mSurface.isProtected());
Lloyd Pique31cb2942018-10-19 17:23:03 -0700151}
152
153TEST_F(RenderSurfaceTest, setProtectedFalseDisablesProtection) {
Peiyong Lin52010312019-05-02 14:22:16 -0700154 EXPECT_FALSE(mSurface.isProtected());
John Reck44418f52020-09-15 18:02:17 -0700155 EXPECT_CALL(*mNativeWindow, setUsage(GRALLOC_USAGE_HW_RENDER | GRALLOC_USAGE_HW_TEXTURE))
156 .WillOnce(Return(NO_ERROR));
Lloyd Pique31cb2942018-10-19 17:23:03 -0700157
158 mSurface.setProtected(false);
Peiyong Lin52010312019-05-02 14:22:16 -0700159 EXPECT_FALSE(mSurface.isProtected());
160}
161
162TEST_F(RenderSurfaceTest, setProtectedEnableAndDisable) {
163 EXPECT_FALSE(mSurface.isProtected());
John Reck44418f52020-09-15 18:02:17 -0700164 EXPECT_CALL(*mNativeWindow,
165 setUsage(GRALLOC_USAGE_HW_RENDER | GRALLOC_USAGE_HW_TEXTURE |
166 GRALLOC_USAGE_PROTECTED))
Peiyong Lin52010312019-05-02 14:22:16 -0700167 .WillOnce(Return(NO_ERROR));
John Reck44418f52020-09-15 18:02:17 -0700168 EXPECT_CALL(*mNativeWindow, setUsage(GRALLOC_USAGE_HW_RENDER | GRALLOC_USAGE_HW_TEXTURE))
169 .WillOnce(Return(NO_ERROR));
Peiyong Lin52010312019-05-02 14:22:16 -0700170
171 mSurface.setProtected(true);
172 EXPECT_TRUE(mSurface.isProtected());
173 mSurface.setProtected(false);
174 EXPECT_FALSE(mSurface.isProtected());
175}
176
177TEST_F(RenderSurfaceTest, setProtectedEnableWithError) {
178 EXPECT_FALSE(mSurface.isProtected());
John Reck44418f52020-09-15 18:02:17 -0700179 EXPECT_CALL(*mNativeWindow,
180 setUsage(GRALLOC_USAGE_HW_RENDER | GRALLOC_USAGE_HW_TEXTURE |
181 GRALLOC_USAGE_PROTECTED))
Peiyong Lin52010312019-05-02 14:22:16 -0700182 .WillOnce(Return(INVALID_OPERATION));
183 mSurface.setProtected(true);
184 EXPECT_FALSE(mSurface.isProtected());
Lloyd Pique31cb2942018-10-19 17:23:03 -0700185}
186
Lloyd Pique66d68602019-02-13 14:23:31 -0800187/*
Lloyd Pique31cb2942018-10-19 17:23:03 -0700188 * RenderSurface::beginFrame()
189 */
190
191TEST_F(RenderSurfaceTest, beginFrameAppliesChange) {
192 EXPECT_CALL(*mDisplaySurface, beginFrame(true)).WillOnce(Return(NO_ERROR));
193
194 EXPECT_EQ(NO_ERROR, mSurface.beginFrame(true));
195}
196
Lloyd Pique66d68602019-02-13 14:23:31 -0800197/*
Lloyd Pique31cb2942018-10-19 17:23:03 -0700198 * RenderSurface::prepareFrame()
199 */
200
Lloyd Pique31cb2942018-10-19 17:23:03 -0700201TEST_F(RenderSurfaceTest, prepareFrameHandlesMixedComposition) {
Lloyd Pique31cb2942018-10-19 17:23:03 -0700202 EXPECT_CALL(*mDisplaySurface, prepareFrame(DisplaySurface::COMPOSITION_MIXED))
Lloyd Pique66d68602019-02-13 14:23:31 -0800203 .WillOnce(Return(NO_ERROR));
Lloyd Pique31cb2942018-10-19 17:23:03 -0700204
Lloyd Pique66d68602019-02-13 14:23:31 -0800205 mSurface.prepareFrame(true, true);
Lloyd Pique31cb2942018-10-19 17:23:03 -0700206}
207
Peiyong Linf3ffc4e2019-12-13 00:46:24 -0800208TEST_F(RenderSurfaceTest, prepareFrameHandlesOnlyGpuComposition) {
209 EXPECT_CALL(*mDisplaySurface, prepareFrame(DisplaySurface::COMPOSITION_GPU))
Lloyd Pique31cb2942018-10-19 17:23:03 -0700210 .WillOnce(Return(NO_ERROR));
211
Lloyd Pique66d68602019-02-13 14:23:31 -0800212 mSurface.prepareFrame(true, false);
Lloyd Pique31cb2942018-10-19 17:23:03 -0700213}
214
215TEST_F(RenderSurfaceTest, prepareFrameHandlesOnlyHwcComposition) {
Lloyd Pique31cb2942018-10-19 17:23:03 -0700216 EXPECT_CALL(*mDisplaySurface, prepareFrame(DisplaySurface::COMPOSITION_HWC))
217 .WillOnce(Return(NO_ERROR));
218
Lloyd Pique66d68602019-02-13 14:23:31 -0800219 mSurface.prepareFrame(false, true);
Lloyd Pique31cb2942018-10-19 17:23:03 -0700220}
221
222TEST_F(RenderSurfaceTest, prepareFrameHandlesNoComposition) {
Lloyd Pique31cb2942018-10-19 17:23:03 -0700223 EXPECT_CALL(*mDisplaySurface, prepareFrame(DisplaySurface::COMPOSITION_HWC))
224 .WillOnce(Return(NO_ERROR));
225
Lloyd Pique66d68602019-02-13 14:23:31 -0800226 mSurface.prepareFrame(false, false);
Lloyd Pique31cb2942018-10-19 17:23:03 -0700227}
228
Lloyd Pique66d68602019-02-13 14:23:31 -0800229/*
Lloyd Pique31cb2942018-10-19 17:23:03 -0700230 * RenderSurface::dequeueBuffer()
231 */
232
233TEST_F(RenderSurfaceTest, dequeueBufferObtainsABuffer) {
234 sp<GraphicBuffer> buffer = new GraphicBuffer();
235
236 EXPECT_CALL(*mNativeWindow, dequeueBuffer(_, _))
237 .WillOnce(
238 DoAll(SetArgPointee<0>(buffer.get()), SetArgPointee<1>(-1), Return(NO_ERROR)));
239
Alec Mouri6338c9d2019-02-07 16:57:51 -0800240 base::unique_fd fence;
241 EXPECT_EQ(buffer.get(), mSurface.dequeueBuffer(&fence).get());
Lloyd Pique31cb2942018-10-19 17:23:03 -0700242
243 EXPECT_EQ(buffer.get(), mSurface.mutableGraphicBufferForTest().get());
244}
245
Lloyd Pique66d68602019-02-13 14:23:31 -0800246/*
Lloyd Pique31cb2942018-10-19 17:23:03 -0700247 * RenderSurface::queueBuffer()
248 */
249
250TEST_F(RenderSurfaceTest, queueBufferHandlesNoClientComposition) {
251 sp<GraphicBuffer> buffer = new GraphicBuffer();
252 mSurface.mutableGraphicBufferForTest() = buffer;
253
Lloyd Pique66d68602019-02-13 14:23:31 -0800254 impl::OutputCompositionState state;
255 state.usesClientComposition = false;
256 state.flipClientTarget = false;
257
258 EXPECT_CALL(mDisplay, getState()).WillOnce(ReturnRef(state));
Lloyd Pique31cb2942018-10-19 17:23:03 -0700259 EXPECT_CALL(*mDisplaySurface, advanceFrame()).Times(1);
260
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000261 mSurface.queueBuffer(base::unique_fd());
Lloyd Pique31cb2942018-10-19 17:23:03 -0700262
263 EXPECT_EQ(buffer.get(), mSurface.mutableGraphicBufferForTest().get());
264}
265
266TEST_F(RenderSurfaceTest, queueBufferHandlesClientComposition) {
267 sp<GraphicBuffer> buffer = new GraphicBuffer();
268 mSurface.mutableGraphicBufferForTest() = buffer;
269
Lloyd Pique66d68602019-02-13 14:23:31 -0800270 impl::OutputCompositionState state;
271 state.usesClientComposition = true;
272 state.flipClientTarget = false;
273
274 EXPECT_CALL(mDisplay, getState()).WillOnce(ReturnRef(state));
Lloyd Pique31cb2942018-10-19 17:23:03 -0700275 EXPECT_CALL(*mNativeWindow, queueBuffer(buffer->getNativeBuffer(), -1))
276 .WillOnce(Return(NO_ERROR));
277 EXPECT_CALL(*mDisplaySurface, advanceFrame()).Times(1);
278
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000279 mSurface.queueBuffer(base::unique_fd());
Lloyd Pique31cb2942018-10-19 17:23:03 -0700280
281 EXPECT_EQ(nullptr, mSurface.mutableGraphicBufferForTest().get());
282}
283
284TEST_F(RenderSurfaceTest, queueBufferHandlesFlipClientTargetRequest) {
285 sp<GraphicBuffer> buffer = new GraphicBuffer();
286 mSurface.mutableGraphicBufferForTest() = buffer;
287
Lloyd Pique66d68602019-02-13 14:23:31 -0800288 impl::OutputCompositionState state;
289 state.usesClientComposition = false;
290 state.flipClientTarget = true;
291
292 EXPECT_CALL(mDisplay, getState()).WillOnce(ReturnRef(state));
Lloyd Pique31cb2942018-10-19 17:23:03 -0700293 EXPECT_CALL(*mNativeWindow, queueBuffer(buffer->getNativeBuffer(), -1))
294 .WillOnce(Return(NO_ERROR));
295 EXPECT_CALL(*mDisplaySurface, advanceFrame()).Times(1);
296
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000297 mSurface.queueBuffer(base::unique_fd());
Lloyd Pique31cb2942018-10-19 17:23:03 -0700298
299 EXPECT_EQ(nullptr, mSurface.mutableGraphicBufferForTest().get());
300}
301
302TEST_F(RenderSurfaceTest, queueBufferHandlesFlipClientTargetRequestWithNoBufferYetDequeued) {
303 sp<GraphicBuffer> buffer = new GraphicBuffer();
304
Lloyd Pique66d68602019-02-13 14:23:31 -0800305 impl::OutputCompositionState state;
306 state.usesClientComposition = false;
307 state.flipClientTarget = true;
308
309 EXPECT_CALL(mDisplay, getState()).WillOnce(ReturnRef(state));
Lloyd Pique31cb2942018-10-19 17:23:03 -0700310 EXPECT_CALL(*mNativeWindow, dequeueBuffer(_, _))
311 .WillOnce(
312 DoAll(SetArgPointee<0>(buffer.get()), SetArgPointee<1>(-1), Return(NO_ERROR)));
313 EXPECT_CALL(*mNativeWindow, queueBuffer(buffer->getNativeBuffer(), -1))
314 .WillOnce(Return(NO_ERROR));
315 EXPECT_CALL(*mDisplaySurface, advanceFrame()).Times(1);
316
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000317 mSurface.queueBuffer(base::unique_fd());
Lloyd Pique31cb2942018-10-19 17:23:03 -0700318
319 EXPECT_EQ(nullptr, mSurface.mutableGraphicBufferForTest().get());
320}
321
322TEST_F(RenderSurfaceTest, queueBufferHandlesNativeWindowQueueBufferFailureOnVirtualDisplay) {
323 sp<GraphicBuffer> buffer = new GraphicBuffer();
324 mSurface.mutableGraphicBufferForTest() = buffer;
325
Lloyd Pique66d68602019-02-13 14:23:31 -0800326 impl::OutputCompositionState state;
327 state.usesClientComposition = true;
328
329 EXPECT_CALL(mDisplay, getState()).WillOnce(ReturnRef(state));
Lloyd Pique31cb2942018-10-19 17:23:03 -0700330 EXPECT_CALL(*mNativeWindow, queueBuffer(buffer->getNativeBuffer(), -1))
331 .WillOnce(Return(INVALID_OPERATION));
332 EXPECT_CALL(mDisplay, isVirtual()).WillOnce(Return(true));
333 EXPECT_CALL(*mNativeWindow, cancelBuffer(buffer->getNativeBuffer(), -1))
334 .WillOnce(Return(NO_ERROR));
335 EXPECT_CALL(*mDisplaySurface, advanceFrame()).Times(1);
336
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000337 mSurface.queueBuffer(base::unique_fd());
Lloyd Pique31cb2942018-10-19 17:23:03 -0700338
339 EXPECT_EQ(nullptr, mSurface.mutableGraphicBufferForTest().get());
340}
341
Lloyd Pique66d68602019-02-13 14:23:31 -0800342/*
Lloyd Pique31cb2942018-10-19 17:23:03 -0700343 * RenderSurface::onPresentDisplayCompleted()
344 */
345
346TEST_F(RenderSurfaceTest, onPresentDisplayCompletedForwardsSignal) {
347 EXPECT_CALL(*mDisplaySurface, onFrameCommitted()).Times(1);
348
349 mSurface.onPresentDisplayCompleted();
350}
351
Lloyd Pique66d68602019-02-13 14:23:31 -0800352/*
Lloyd Pique31cb2942018-10-19 17:23:03 -0700353 * RenderSurface::flip()
354 */
355
356TEST_F(RenderSurfaceTest, flipForwardsSignal) {
357 mSurface.setPageFlipCountForTest(500);
358
359 mSurface.flip();
360
361 EXPECT_EQ(501, mSurface.getPageFlipCount());
362}
363
364} // namespace
365} // namespace android::compositionengine
Marin Shalamanovbed7fd32020-12-21 20:02:20 +0100366
367// TODO(b/129481165): remove the #pragma below and fix conversion issues
368#pragma clang diagnostic pop // ignored "-Wextra"