blob: ea2818d532dce4452ce879e41543e82153af9cae [file] [log] [blame]
Lloyd Piqued6b579f2018-04-06 15:29:10 -07001/*
2 * Copyright 2018 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#undef LOG_TAG
18#define LOG_TAG "CompositionTest"
19
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080020#include <compositionengine/Display.h>
Lloyd Pique542307f2018-10-19 13:24:08 -070021#include <compositionengine/mock/DisplaySurface.h>
Lloyd Piqued6b579f2018-04-06 15:29:10 -070022#include <gmock/gmock.h>
23#include <gtest/gtest.h>
Lloyd Piqued6b579f2018-04-06 15:29:10 -070024#include <gui/IProducerListener.h>
Evan Roskya1f1e152019-01-24 16:17:46 -080025#include <gui/LayerMetadata.h>
Lloyd Piqued6b579f2018-04-06 15:29:10 -070026#include <log/log.h>
Lloyd Pique3823e7b2018-10-18 16:58:10 -070027#include <renderengine/mock/Framebuffer.h>
28#include <renderengine/mock/Image.h>
29#include <renderengine/mock/RenderEngine.h>
Lloyd Piqued6b579f2018-04-06 15:29:10 -070030#include <system/window.h>
31#include <utils/String8.h>
32
33#include "BufferQueueLayer.h"
34#include "ColorLayer.h"
35#include "Layer.h"
36
Ana Krulecafb45842019-02-13 13:33:03 -080037#include "TestableScheduler.h"
Lloyd Piqued6b579f2018-04-06 15:29:10 -070038#include "TestableSurfaceFlinger.h"
39#include "mock/DisplayHardware/MockComposer.h"
Lloyd Piqued6b579f2018-04-06 15:29:10 -070040#include "mock/MockDispSync.h"
41#include "mock/MockEventControlThread.h"
42#include "mock/MockEventThread.h"
43#include "mock/MockMessageQueue.h"
Alec Mouriba013fa2018-10-16 12:43:11 -070044#include "mock/system/window/MockNativeWindow.h"
Lloyd Piqued6b579f2018-04-06 15:29:10 -070045
46namespace android {
47namespace {
48
49using testing::_;
David Sodman15094112018-10-11 09:39:37 -070050using testing::AtLeast;
Alec Mourie7d1d4a2019-02-05 01:13:46 +000051using testing::Between;
Lloyd Piqued6b579f2018-04-06 15:29:10 -070052using testing::ByMove;
53using testing::DoAll;
Alec Mourie7d1d4a2019-02-05 01:13:46 +000054using testing::Field;
Alec Mouri0a9c7b82018-11-16 13:05:25 -080055using testing::Invoke;
Lloyd Piqued6b579f2018-04-06 15:29:10 -070056using testing::IsNull;
57using testing::Mock;
58using testing::NotNull;
59using testing::Ref;
60using testing::Return;
61using testing::ReturnRef;
62using testing::SetArgPointee;
63
64using android::Hwc2::Error;
65using android::Hwc2::IComposer;
66using android::Hwc2::IComposerClient;
67using android::Hwc2::Transform;
68
69using FakeHwcDisplayInjector = TestableSurfaceFlinger::FakeHwcDisplayInjector;
70using FakeDisplayDeviceInjector = TestableSurfaceFlinger::FakeDisplayDeviceInjector;
71
72constexpr hwc2_display_t HWC_DISPLAY = FakeHwcDisplayInjector::DEFAULT_HWC_DISPLAY_ID;
73constexpr hwc2_layer_t HWC_LAYER = 5000;
74constexpr Transform DEFAULT_TRANSFORM = static_cast<Transform>(0);
75
Dominik Laskowski34157762018-10-31 13:07:19 -070076constexpr DisplayId DEFAULT_DISPLAY_ID = DisplayId{42};
Lloyd Piqued6b579f2018-04-06 15:29:10 -070077constexpr int DEFAULT_DISPLAY_WIDTH = 1920;
78constexpr int DEFAULT_DISPLAY_HEIGHT = 1024;
79
80constexpr int DEFAULT_CONFIG_ID = 0;
81constexpr int DEFAULT_TEXTURE_ID = 6000;
82constexpr int DEFAULT_LAYER_STACK = 7000;
83
84constexpr int DEFAULT_DISPLAY_MAX_LUMINANCE = 500;
85
86constexpr int DEFAULT_SIDEBAND_STREAM = 51;
87
88class CompositionTest : public testing::Test {
89public:
90 CompositionTest() {
91 const ::testing::TestInfo* const test_info =
92 ::testing::UnitTest::GetInstance()->current_test_info();
93 ALOGD("**** Setting up for %s.%s\n", test_info->test_case_name(), test_info->name());
94
Lloyd Piqued6b579f2018-04-06 15:29:10 -070095 mFlinger.mutableEventQueue().reset(mMessageQueue);
Ana Krulecafb45842019-02-13 13:33:03 -080096 setupScheduler();
Lloyd Piqued6b579f2018-04-06 15:29:10 -070097
Lloyd Piqued6b579f2018-04-06 15:29:10 -070098 EXPECT_CALL(*mPrimaryDispSync, computeNextRefresh(0)).WillRepeatedly(Return(0));
99 EXPECT_CALL(*mPrimaryDispSync, getPeriod())
100 .WillRepeatedly(Return(FakeHwcDisplayInjector::DEFAULT_REFRESH_RATE));
Lloyd Pique86fa3db2019-02-04 18:46:01 -0800101 EXPECT_CALL(*mPrimaryDispSync, expectedPresentTime()).WillRepeatedly(Return(0));
Alec Mourif6fd29e2018-11-17 04:56:41 +0000102 EXPECT_CALL(*mNativeWindow, query(NATIVE_WINDOW_WIDTH, _))
103 .WillRepeatedly(DoAll(SetArgPointee<1>(DEFAULT_DISPLAY_WIDTH), Return(0)));
104 EXPECT_CALL(*mNativeWindow, query(NATIVE_WINDOW_HEIGHT, _))
105 .WillRepeatedly(DoAll(SetArgPointee<1>(DEFAULT_DISPLAY_HEIGHT), Return(0)));
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700106
107 mFlinger.setupRenderEngine(std::unique_ptr<renderengine::RenderEngine>(mRenderEngine));
108 setupComposer(0);
109 }
110
111 ~CompositionTest() {
112 const ::testing::TestInfo* const test_info =
113 ::testing::UnitTest::GetInstance()->current_test_info();
114 ALOGD("**** Tearing down after %s.%s\n", test_info->test_case_name(), test_info->name());
115 }
116
117 void setupComposer(int virtualDisplayCount) {
118 mComposer = new Hwc2::mock::Composer();
119 EXPECT_CALL(*mComposer, getCapabilities())
120 .WillOnce(Return(std::vector<IComposer::Capability>()));
121 EXPECT_CALL(*mComposer, getMaxVirtualDisplayCount()).WillOnce(Return(virtualDisplayCount));
122 mFlinger.setupComposer(std::unique_ptr<Hwc2::Composer>(mComposer));
123
124 Mock::VerifyAndClear(mComposer);
125 }
126
Ana Krulecafb45842019-02-13 13:33:03 -0800127 void setupScheduler() {
128 mScheduler = new TestableScheduler();
129 mScheduler->mutableEventControlThread().reset(mEventControlThread);
130 mScheduler->mutablePrimaryDispSync().reset(mPrimaryDispSync);
131 EXPECT_CALL(*mEventThread.get(), registerDisplayEventConnection(_));
132 sp<Scheduler::ConnectionHandle> connectionHandle =
133 mScheduler->addConnection(std::move(mEventThread));
134 mFlinger.mutableSfConnectionHandle() = std::move(connectionHandle);
135
136 mFlinger.mutableScheduler().reset(mScheduler);
137 }
138
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700139 void setupForceGeometryDirty() {
140 // TODO: This requires the visible region and other related
141 // state to be set, and is problematic for BufferLayers since they are
142 // not visible without a buffer (and setting up a buffer looks like a
143 // pain)
144 // mFlinger.mutableVisibleRegionsDirty() = true;
145
146 mFlinger.mutableGeometryInvalid() = true;
147 }
148
149 template <typename Case>
150 void displayRefreshCompositionDirtyGeometry();
151
152 template <typename Case>
153 void displayRefreshCompositionDirtyFrame();
154
155 template <typename Case>
156 void captureScreenComposition();
157
158 std::unordered_set<HWC2::Capability> mDefaultCapabilities = {HWC2::Capability::SidebandStream};
159
Ana Krulecafb45842019-02-13 13:33:03 -0800160 TestableScheduler* mScheduler;
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700161 TestableSurfaceFlinger mFlinger;
162 sp<DisplayDevice> mDisplay;
163 sp<DisplayDevice> mExternalDisplay;
Lloyd Pique542307f2018-10-19 13:24:08 -0700164 sp<compositionengine::mock::DisplaySurface> mDisplaySurface =
165 new compositionengine::mock::DisplaySurface();
Alec Mouriba013fa2018-10-16 12:43:11 -0700166 mock::NativeWindow* mNativeWindow = new mock::NativeWindow();
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700167
Alec Mouri0a9c7b82018-11-16 13:05:25 -0800168 sp<GraphicBuffer> mBuffer = new GraphicBuffer();
169 ANativeWindowBuffer* mNativeWindowBuffer = mBuffer->getNativeBuffer();
170
Ana Krulecafb45842019-02-13 13:33:03 -0800171 std::unique_ptr<mock::EventThread> mEventThread = std::make_unique<mock::EventThread>();
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700172 mock::EventControlThread* mEventControlThread = new mock::EventControlThread();
173
174 Hwc2::mock::Composer* mComposer = nullptr;
175 renderengine::mock::RenderEngine* mRenderEngine = new renderengine::mock::RenderEngine();
176 mock::MessageQueue* mMessageQueue = new mock::MessageQueue();
177 mock::DispSync* mPrimaryDispSync = new mock::DispSync();
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700178 renderengine::mock::Framebuffer* mReFrameBuffer = new renderengine::mock::Framebuffer();
179
180 sp<Fence> mClientTargetAcquireFence = Fence::NO_FENCE;
181
182 sp<GraphicBuffer> mCaptureScreenBuffer;
183};
184
185template <typename LayerCase>
186void CompositionTest::displayRefreshCompositionDirtyGeometry() {
187 setupForceGeometryDirty();
188 LayerCase::setupForDirtyGeometry(this);
189
190 // --------------------------------------------------------------------
191 // Invocation
192
193 mFlinger.onMessageReceived(MessageQueue::INVALIDATE);
194 mFlinger.onMessageReceived(MessageQueue::REFRESH);
195
196 LayerCase::cleanup(this);
197}
198
199template <typename LayerCase>
200void CompositionTest::displayRefreshCompositionDirtyFrame() {
201 LayerCase::setupForDirtyFrame(this);
202
203 // --------------------------------------------------------------------
204 // Invocation
205
206 mFlinger.onMessageReceived(MessageQueue::INVALIDATE);
207 mFlinger.onMessageReceived(MessageQueue::REFRESH);
208
209 LayerCase::cleanup(this);
210}
211
212template <typename LayerCase>
213void CompositionTest::captureScreenComposition() {
214 LayerCase::setupForScreenCapture(this);
215
216 const Rect sourceCrop(0, 0, DEFAULT_DISPLAY_WIDTH, DEFAULT_DISPLAY_HEIGHT);
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700217 constexpr bool useIdentityTransform = true;
218 constexpr bool forSystem = true;
219
220 DisplayRenderArea renderArea(mDisplay, sourceCrop, DEFAULT_DISPLAY_WIDTH,
Peiyong Lin0e003c92018-09-17 11:09:51 -0700221 DEFAULT_DISPLAY_HEIGHT, ui::Dataspace::V0_SRGB,
222 ui::Transform::ROT_0);
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700223
224 auto traverseLayers = [this](const LayerVector::Visitor& visitor) {
chaviw0e3479f2018-09-10 16:49:30 -0700225 return mFlinger.traverseLayersInDisplay(mDisplay, visitor);
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700226 };
227
228 // TODO: Eliminate expensive/real allocation if possible.
229 const uint32_t usage = GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN |
230 GRALLOC_USAGE_HW_RENDER | GRALLOC_USAGE_HW_TEXTURE;
231 mCaptureScreenBuffer = new GraphicBuffer(renderArea.getReqWidth(), renderArea.getReqHeight(),
232 HAL_PIXEL_FORMAT_RGBA_8888, 1, usage, "screenshot");
233
234 int fd = -1;
235 status_t result =
236 mFlinger.captureScreenImplLocked(renderArea, traverseLayers, mCaptureScreenBuffer.get(),
237 useIdentityTransform, forSystem, &fd);
238 if (fd >= 0) {
239 close(fd);
240 }
241
242 EXPECT_EQ(NO_ERROR, result);
243
244 LayerCase::cleanup(this);
245}
246
247/* ------------------------------------------------------------------------
248 * Variants for each display configuration which can be tested
249 */
250
251template <typename Derived>
252struct BaseDisplayVariant {
253 static constexpr bool IS_SECURE = true;
254 static constexpr int INIT_POWER_MODE = HWC_POWER_MODE_NORMAL;
255
256 static void setupPreconditions(CompositionTest* test) {
Lloyd Pique86fa3db2019-02-04 18:46:01 -0800257 EXPECT_CALL(*test->mComposer, getDisplayCapabilities(HWC_DISPLAY, _))
258 .WillOnce(DoAll(SetArgPointee<1>(std::vector<Hwc2::DisplayCapability>({})),
259 Return(Error::NONE)));
Alec Mouriba013fa2018-10-16 12:43:11 -0700260
Dominik Laskowski075d3172018-05-24 15:50:06 -0700261 FakeHwcDisplayInjector(DEFAULT_DISPLAY_ID, HWC2::DisplayType::Physical,
262 true /* isPrimary */)
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700263 .setCapabilities(&test->mDefaultCapabilities)
264 .inject(&test->mFlinger, test->mComposer);
Lloyd Pique86fa3db2019-02-04 18:46:01 -0800265 Mock::VerifyAndClear(test->mComposer);
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700266
Lloyd Pique86fa3db2019-02-04 18:46:01 -0800267 EXPECT_CALL(*test->mNativeWindow, query(NATIVE_WINDOW_WIDTH, _))
268 .WillRepeatedly(DoAll(SetArgPointee<1>(DEFAULT_DISPLAY_WIDTH), Return(0)));
269 EXPECT_CALL(*test->mNativeWindow, query(NATIVE_WINDOW_HEIGHT, _))
270 .WillRepeatedly(DoAll(SetArgPointee<1>(DEFAULT_DISPLAY_HEIGHT), Return(0)));
271 EXPECT_CALL(*test->mNativeWindow, perform(NATIVE_WINDOW_SET_BUFFERS_FORMAT)).Times(1);
272 EXPECT_CALL(*test->mNativeWindow, perform(NATIVE_WINDOW_API_CONNECT)).Times(1);
273 EXPECT_CALL(*test->mNativeWindow, perform(NATIVE_WINDOW_SET_USAGE64)).Times(1);
Dominik Laskowski075d3172018-05-24 15:50:06 -0700274 test->mDisplay = FakeDisplayDeviceInjector(test->mFlinger, DEFAULT_DISPLAY_ID,
275 false /* isVirtual */, true /* isPrimary */)
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700276 .setDisplaySurface(test->mDisplaySurface)
Alec Mouriba013fa2018-10-16 12:43:11 -0700277 .setNativeWindow(test->mNativeWindow)
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700278 .setSecure(Derived::IS_SECURE)
279 .setPowerMode(Derived::INIT_POWER_MODE)
280 .inject();
Lloyd Pique86fa3db2019-02-04 18:46:01 -0800281 Mock::VerifyAndClear(test->mNativeWindow);
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700282 test->mDisplay->setLayerStack(DEFAULT_LAYER_STACK);
283 }
284
285 template <typename Case>
286 static void setupCommonCompositionCallExpectations(CompositionTest* test) {
287 EXPECT_CALL(*test->mComposer,
288 setColorTransform(HWC_DISPLAY, _, Hwc2::ColorTransform::IDENTITY))
289 .Times(1);
290 EXPECT_CALL(*test->mComposer, presentOrValidateDisplay(HWC_DISPLAY, _, _, _, _)).Times(1);
291 EXPECT_CALL(*test->mComposer, getDisplayRequests(HWC_DISPLAY, _, _, _)).Times(1);
292 EXPECT_CALL(*test->mComposer, acceptDisplayChanges(HWC_DISPLAY)).Times(1);
293 EXPECT_CALL(*test->mComposer, presentDisplay(HWC_DISPLAY, _)).Times(1);
294 EXPECT_CALL(*test->mComposer, getReleaseFences(HWC_DISPLAY, _, _)).Times(1);
295
296 EXPECT_CALL(*test->mRenderEngine, useNativeFenceSync()).WillRepeatedly(Return(true));
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000297 // TODO: remove once we verify that we can just grab the fence from the
298 // FramebufferSurface.
Alec Mouri0a9c7b82018-11-16 13:05:25 -0800299 EXPECT_CALL(*test->mRenderEngine, flush()).WillRepeatedly(Invoke([]() {
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000300 return base::unique_fd();
Alec Mouri0a9c7b82018-11-16 13:05:25 -0800301 }));
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700302
303 EXPECT_CALL(*test->mDisplaySurface, onFrameCommitted()).Times(1);
304 EXPECT_CALL(*test->mDisplaySurface, advanceFrame()).Times(1);
305
306 Case::CompositionType::setupHwcSetCallExpectations(test);
307 Case::CompositionType::setupHwcGetCallExpectations(test);
308 }
309
310 template <typename Case>
311 static void setupCommonScreensCaptureCallExpectations(CompositionTest* test) {
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000312 EXPECT_CALL(*test->mRenderEngine, drawLayers)
313 .WillRepeatedly(
314 [](const renderengine::DisplaySettings& displaySettings,
315 const std::vector<renderengine::LayerSettings>& /*layerSettings*/,
Alec Mouri6338c9d2019-02-07 16:57:51 -0800316 ANativeWindowBuffer*, base::unique_fd&&, base::unique_fd*) -> status_t {
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000317 EXPECT_EQ(DEFAULT_DISPLAY_MAX_LUMINANCE, displaySettings.maxLuminance);
318 EXPECT_EQ(Rect(DEFAULT_DISPLAY_WIDTH, DEFAULT_DISPLAY_HEIGHT),
319 displaySettings.physicalDisplay);
320 EXPECT_EQ(Rect(DEFAULT_DISPLAY_WIDTH, DEFAULT_DISPLAY_HEIGHT),
321 displaySettings.clip);
322 return NO_ERROR;
323 });
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700324 }
325
326 static void setupNonEmptyFrameCompositionCallExpectations(CompositionTest* test) {
327 EXPECT_CALL(*test->mDisplaySurface, beginFrame(true)).Times(1);
328 }
329
330 static void setupEmptyFrameCompositionCallExpectations(CompositionTest* test) {
331 EXPECT_CALL(*test->mDisplaySurface, beginFrame(false)).Times(1);
332 }
333
334 static void setupHwcCompositionCallExpectations(CompositionTest* test) {
Lloyd Pique542307f2018-10-19 13:24:08 -0700335 EXPECT_CALL(*test->mDisplaySurface,
336 prepareFrame(compositionengine::DisplaySurface::COMPOSITION_HWC))
337 .Times(1);
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700338 }
339
340 static void setupRECompositionCallExpectations(CompositionTest* test) {
Lloyd Pique542307f2018-10-19 13:24:08 -0700341 EXPECT_CALL(*test->mDisplaySurface,
342 prepareFrame(compositionengine::DisplaySurface::COMPOSITION_GLES))
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700343 .Times(1);
344 EXPECT_CALL(*test->mDisplaySurface, getClientTargetAcquireFence())
345 .WillRepeatedly(ReturnRef(test->mClientTargetAcquireFence));
346
Alec Mouri0a9c7b82018-11-16 13:05:25 -0800347 EXPECT_CALL(*test->mNativeWindow, queueBuffer(_, _)).WillOnce(Return(0));
348 EXPECT_CALL(*test->mNativeWindow, dequeueBuffer(_, _))
349 .WillOnce(DoAll(SetArgPointee<0>(test->mNativeWindowBuffer), SetArgPointee<1>(-1),
350 Return(0)));
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000351 EXPECT_CALL(*test->mRenderEngine, drawLayers)
352 .WillRepeatedly(
353 [](const renderengine::DisplaySettings& displaySettings,
354 const std::vector<renderengine::LayerSettings>& /*layerSettings*/,
Alec Mouri6338c9d2019-02-07 16:57:51 -0800355 ANativeWindowBuffer*, base::unique_fd&&, base::unique_fd*) -> status_t {
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000356 EXPECT_EQ(DEFAULT_DISPLAY_MAX_LUMINANCE, displaySettings.maxLuminance);
357 EXPECT_EQ(Rect(DEFAULT_DISPLAY_WIDTH, DEFAULT_DISPLAY_HEIGHT),
358 displaySettings.physicalDisplay);
359 EXPECT_EQ(Rect(DEFAULT_DISPLAY_WIDTH, DEFAULT_DISPLAY_HEIGHT),
360 displaySettings.clip);
361 EXPECT_EQ(ui::Dataspace::UNKNOWN, displaySettings.outputDataspace);
362 return NO_ERROR;
363 });
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700364 }
365
366 template <typename Case>
367 static void setupRELayerCompositionCallExpectations(CompositionTest* test) {
368 Case::Layer::setupRECompositionCallExpectations(test);
369 }
370
371 template <typename Case>
372 static void setupRELayerScreenshotCompositionCallExpectations(CompositionTest* test) {
373 Case::Layer::setupREScreenshotCompositionCallExpectations(test);
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700374 }
375};
376
377struct DefaultDisplaySetupVariant : public BaseDisplayVariant<DefaultDisplaySetupVariant> {};
378
379struct InsecureDisplaySetupVariant : public BaseDisplayVariant<InsecureDisplaySetupVariant> {
380 static constexpr bool IS_SECURE = false;
381
382 template <typename Case>
383 static void setupRELayerCompositionCallExpectations(CompositionTest* test) {
384 Case::Layer::setupInsecureRECompositionCallExpectations(test);
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700385 }
386
387 template <typename Case>
388 static void setupRELayerScreenshotCompositionCallExpectations(CompositionTest* test) {
389 Case::Layer::setupInsecureREScreenshotCompositionCallExpectations(test);
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700390 }
391};
392
393struct PoweredOffDisplaySetupVariant : public BaseDisplayVariant<PoweredOffDisplaySetupVariant> {
394 static constexpr int INIT_POWER_MODE = HWC_POWER_MODE_OFF;
395
396 template <typename Case>
397 static void setupCommonCompositionCallExpectations(CompositionTest* test) {
Lloyd Pique86fa3db2019-02-04 18:46:01 -0800398 EXPECT_CALL(*test->mRenderEngine, useNativeFenceSync()).WillRepeatedly(Return(true));
399
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700400 // TODO: This seems like an unnecessary call if display is powered off.
401 EXPECT_CALL(*test->mComposer,
402 setColorTransform(HWC_DISPLAY, _, Hwc2::ColorTransform::IDENTITY))
403 .Times(1);
404
405 // TODO: This seems like an unnecessary call if display is powered off.
406 Case::CompositionType::setupHwcSetCallExpectations(test);
407 }
408
409 static void setupHwcCompositionCallExpectations(CompositionTest*) {}
410
411 static void setupRECompositionCallExpectations(CompositionTest* test) {
Lloyd Pique86fa3db2019-02-04 18:46:01 -0800412 EXPECT_CALL(*test->mRenderEngine, useNativeFenceSync()).WillRepeatedly(Return(true));
413
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700414 // TODO: This seems like an unnecessary call if display is powered off.
415 EXPECT_CALL(*test->mDisplaySurface, getClientTargetAcquireFence())
416 .WillRepeatedly(ReturnRef(test->mClientTargetAcquireFence));
417 }
418
419 template <typename Case>
420 static void setupRELayerCompositionCallExpectations(CompositionTest*) {}
421};
422
423/* ------------------------------------------------------------------------
424 * Variants for each layer configuration which can be tested
425 */
426
427template <typename LayerProperties>
428struct BaseLayerProperties {
429 static constexpr uint32_t WIDTH = 100;
430 static constexpr uint32_t HEIGHT = 100;
431 static constexpr PixelFormat FORMAT = PIXEL_FORMAT_RGBA_8888;
432 static constexpr uint64_t USAGE =
433 GraphicBuffer::USAGE_SW_READ_NEVER | GraphicBuffer::USAGE_SW_WRITE_NEVER;
434 static constexpr android_dataspace DATASPACE = HAL_DATASPACE_UNKNOWN;
435 static constexpr uint32_t SCALING_MODE = 0;
436 static constexpr uint32_t TRANSFORM = 0;
437 static constexpr uint32_t LAYER_FLAGS = 0;
438 static constexpr float COLOR[] = {1.f, 1.f, 1.f, 1.f};
439 static constexpr IComposerClient::BlendMode BLENDMODE =
440 IComposerClient::BlendMode::PREMULTIPLIED;
441
442 static void enqueueBuffer(CompositionTest*, sp<BufferQueueLayer> layer) {
443 auto producer = layer->getProducer();
444
445 IGraphicBufferProducer::QueueBufferOutput qbo;
446 status_t result = producer->connect(nullptr, NATIVE_WINDOW_API_EGL, false, &qbo);
447 if (result != NO_ERROR) {
448 ALOGE("Failed to connect() (%d)", result);
449 return;
450 }
451
452 int slot;
453 sp<Fence> fence;
454 result = producer->dequeueBuffer(&slot, &fence, LayerProperties::WIDTH,
455 LayerProperties::HEIGHT, LayerProperties::FORMAT,
456 LayerProperties::USAGE, nullptr, nullptr);
457 if (result != IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION) {
458 ALOGE("Failed to dequeueBuffer() (%d)", result);
459 return;
460 }
461
462 sp<GraphicBuffer> buffer;
463 result = producer->requestBuffer(slot, &buffer);
464 if (result != NO_ERROR) {
465 ALOGE("Failed to requestBuffer() (%d)", result);
466 return;
467 }
468
469 IGraphicBufferProducer::QueueBufferInput qbi(systemTime(), false /* isAutoTimestamp */,
470 LayerProperties::DATASPACE,
471 Rect(LayerProperties::WIDTH,
472 LayerProperties::HEIGHT),
473 LayerProperties::SCALING_MODE,
474 LayerProperties::TRANSFORM, Fence::NO_FENCE);
475 result = producer->queueBuffer(slot, qbi, &qbo);
476 if (result != NO_ERROR) {
477 ALOGE("Failed to queueBuffer (%d)", result);
478 return;
479 }
480 }
481
482 static void setupLatchedBuffer(CompositionTest* test, sp<BufferQueueLayer> layer) {
483 // TODO: Eliminate the complexity of actually creating a buffer
484 EXPECT_CALL(*test->mRenderEngine, getMaxTextureSize()).WillOnce(Return(16384));
485 EXPECT_CALL(*test->mRenderEngine, getMaxViewportDims()).WillOnce(Return(16384));
486 status_t err =
487 layer->setDefaultBufferProperties(LayerProperties::WIDTH, LayerProperties::HEIGHT,
488 LayerProperties::FORMAT);
489 ASSERT_EQ(NO_ERROR, err);
490 Mock::VerifyAndClear(test->mRenderEngine);
491
492 EXPECT_CALL(*test->mMessageQueue, invalidate()).Times(1);
493 enqueueBuffer(test, layer);
494 Mock::VerifyAndClear(test->mMessageQueue);
495
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700496 EXPECT_CALL(*test->mRenderEngine, useNativeFenceSync()).WillRepeatedly(Return(true));
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700497 bool ignoredRecomputeVisibleRegions;
Alec Mouri56e538f2019-01-14 15:22:01 -0800498 layer->latchBuffer(ignoredRecomputeVisibleRegions, 0);
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700499 Mock::VerifyAndClear(test->mRenderEngine);
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700500 }
501
502 static void setupLayerState(CompositionTest* test, sp<BufferQueueLayer> layer) {
503 setupLatchedBuffer(test, layer);
504 }
505
506 static void setupBufferLayerPostFrameCallExpectations(CompositionTest* test) {
507 // BufferLayer::onPostComposition(), when there is no present fence
508 EXPECT_CALL(*test->mComposer, getActiveConfig(HWC_DISPLAY, _))
509 .WillOnce(DoAll(SetArgPointee<1>(DEFAULT_CONFIG_ID), Return(Error::NONE)));
510 }
511
512 static void setupHwcSetGeometryCallExpectations(CompositionTest* test) {
513 // TODO: Coverage of other values
514 EXPECT_CALL(*test->mComposer,
515 setLayerBlendMode(HWC_DISPLAY, HWC_LAYER, LayerProperties::BLENDMODE))
516 .Times(1);
517 // TODO: Coverage of other values for origin
518 EXPECT_CALL(*test->mComposer,
519 setLayerDisplayFrame(HWC_DISPLAY, HWC_LAYER,
520 IComposerClient::Rect({0, 0, LayerProperties::WIDTH,
521 LayerProperties::HEIGHT})))
522 .Times(1);
523 EXPECT_CALL(*test->mComposer,
524 setLayerPlaneAlpha(HWC_DISPLAY, HWC_LAYER, LayerProperties::COLOR[3]))
525 .Times(1);
526 // TODO: Coverage of other values
527 EXPECT_CALL(*test->mComposer, setLayerZOrder(HWC_DISPLAY, HWC_LAYER, 0u)).Times(1);
528 // TODO: Coverage of other values
529 EXPECT_CALL(*test->mComposer, setLayerInfo(HWC_DISPLAY, HWC_LAYER, 0u, 0u)).Times(1);
530
531 // These expectations retire on saturation as the code path these
532 // expectations are for appears to make an extra call to them.
533 // TODO: Investigate this extra call
534 EXPECT_CALL(*test->mComposer, setLayerTransform(HWC_DISPLAY, HWC_LAYER, DEFAULT_TRANSFORM))
David Sodman15094112018-10-11 09:39:37 -0700535 .Times(AtLeast(1))
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700536 .RetiresOnSaturation();
537 }
538
539 static void setupHwcSetSourceCropBufferCallExpectations(CompositionTest* test) {
540 EXPECT_CALL(*test->mComposer,
541 setLayerSourceCrop(HWC_DISPLAY, HWC_LAYER,
542 IComposerClient::FRect({0.f, 0.f, LayerProperties::WIDTH,
543 LayerProperties::HEIGHT})))
544 .Times(1);
545 }
546
547 static void setupHwcSetSourceCropColorCallExpectations(CompositionTest* test) {
548 EXPECT_CALL(*test->mComposer,
549 setLayerSourceCrop(HWC_DISPLAY, HWC_LAYER,
550 IComposerClient::FRect({0.f, 0.f, 0.f, 0.f})))
551 .Times(1);
552 }
553
554 static void setupHwcSetPerFrameCallExpectations(CompositionTest* test) {
555 EXPECT_CALL(*test->mComposer,
556 setLayerVisibleRegion(HWC_DISPLAY, HWC_LAYER,
557 std::vector<IComposerClient::Rect>({IComposerClient::Rect(
558 {0, 0, LayerProperties::WIDTH,
559 LayerProperties::HEIGHT})})))
560 .Times(1);
561 }
562
563 static void setupHwcSetPerFrameColorCallExpectations(CompositionTest* test) {
564 EXPECT_CALL(*test->mComposer, setLayerSurfaceDamage(HWC_DISPLAY, HWC_LAYER, _)).Times(1);
565
566 // TODO: use COLOR
567 EXPECT_CALL(*test->mComposer,
568 setLayerColor(HWC_DISPLAY, HWC_LAYER,
569 IComposerClient::Color({0xff, 0xff, 0xff, 0xff})))
570 .Times(1);
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700571 }
572
573 static void setupHwcSetPerFrameBufferCallExpectations(CompositionTest* test) {
574 EXPECT_CALL(*test->mComposer, setLayerSurfaceDamage(HWC_DISPLAY, HWC_LAYER, _)).Times(1);
575 EXPECT_CALL(*test->mComposer, setLayerBuffer(HWC_DISPLAY, HWC_LAYER, _, _, _)).Times(1);
576
577 setupBufferLayerPostFrameCallExpectations(test);
578 }
579
580 static void setupREBufferCompositionCommonCallExpectations(CompositionTest* test) {
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000581 EXPECT_CALL(*test->mRenderEngine, drawLayers)
582 .WillOnce([](const renderengine::DisplaySettings& displaySettings,
583 const std::vector<renderengine::LayerSettings>& layerSettings,
Alec Mouri6338c9d2019-02-07 16:57:51 -0800584 ANativeWindowBuffer*, base::unique_fd&&,
585 base::unique_fd*) -> status_t {
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000586 EXPECT_EQ(DEFAULT_DISPLAY_MAX_LUMINANCE, displaySettings.maxLuminance);
587 EXPECT_EQ(Rect(DEFAULT_DISPLAY_WIDTH, DEFAULT_DISPLAY_HEIGHT),
588 displaySettings.physicalDisplay);
589 EXPECT_EQ(Rect(DEFAULT_DISPLAY_WIDTH, DEFAULT_DISPLAY_HEIGHT),
590 displaySettings.clip);
591 // screen capture adds an additional color layer as an alpha
592 // prefill, so gtet the back layer.
593 renderengine::LayerSettings layer = layerSettings.back();
594 EXPECT_THAT(layer.source.buffer.buffer, Not(IsNull()));
595 EXPECT_THAT(layer.source.buffer.fence, Not(IsNull()));
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000596 EXPECT_EQ(DEFAULT_TEXTURE_ID, layer.source.buffer.textureName);
597 EXPECT_EQ(false, layer.source.buffer.isY410BT2020);
598 EXPECT_EQ(true, layer.source.buffer.usePremultipliedAlpha);
599 EXPECT_EQ(false, layer.source.buffer.isOpaque);
600 EXPECT_EQ(0.0, layer.geometry.roundedCornersRadius);
601 EXPECT_EQ(ui::Dataspace::UNKNOWN, layer.sourceDataspace);
602 EXPECT_EQ(LayerProperties::COLOR[3], layer.alpha);
603 return NO_ERROR;
604 });
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700605 }
606
607 static void setupREBufferCompositionCallExpectations(CompositionTest* test) {
608 LayerProperties::setupREBufferCompositionCommonCallExpectations(test);
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700609 }
610
611 static void setupInsecureREBufferCompositionCallExpectations(CompositionTest* test) {
612 setupREBufferCompositionCallExpectations(test);
613 }
614
615 static void setupREBufferScreenshotCompositionCallExpectations(CompositionTest* test) {
616 LayerProperties::setupREBufferCompositionCommonCallExpectations(test);
617 }
618
619 static void setupInsecureREBufferScreenshotCompositionCallExpectations(CompositionTest* test) {
620 LayerProperties::setupREBufferCompositionCommonCallExpectations(test);
621 }
622
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700623 static void setupREColorCompositionCallExpectations(CompositionTest* test) {
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000624 EXPECT_CALL(*test->mRenderEngine, drawLayers)
625 .WillOnce([](const renderengine::DisplaySettings& displaySettings,
626 const std::vector<renderengine::LayerSettings>& layerSettings,
Alec Mouri6338c9d2019-02-07 16:57:51 -0800627 ANativeWindowBuffer*, base::unique_fd&&,
628 base::unique_fd*) -> status_t {
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000629 EXPECT_EQ(DEFAULT_DISPLAY_MAX_LUMINANCE, displaySettings.maxLuminance);
630 EXPECT_EQ(Rect(DEFAULT_DISPLAY_WIDTH, DEFAULT_DISPLAY_HEIGHT),
631 displaySettings.physicalDisplay);
632 EXPECT_EQ(Rect(DEFAULT_DISPLAY_WIDTH, DEFAULT_DISPLAY_HEIGHT),
633 displaySettings.clip);
634 // screen capture adds an additional color layer as an alpha
635 // prefill, so get the back layer.
636 renderengine::LayerSettings layer = layerSettings.back();
637 EXPECT_THAT(layer.source.buffer.buffer, IsNull());
638 EXPECT_EQ(half3(LayerProperties::COLOR[0], LayerProperties::COLOR[1],
639 LayerProperties::COLOR[2]),
640 layer.source.solidColor);
641 EXPECT_EQ(0.0, layer.geometry.roundedCornersRadius);
642 EXPECT_EQ(ui::Dataspace::UNKNOWN, layer.sourceDataspace);
643 EXPECT_EQ(LayerProperties::COLOR[3], layer.alpha);
644 return NO_ERROR;
645 });
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700646 }
647
648 static void setupREColorScreenshotCompositionCallExpectations(CompositionTest* test) {
649 setupREColorCompositionCallExpectations(test);
650 }
651};
652
653struct DefaultLayerProperties : public BaseLayerProperties<DefaultLayerProperties> {};
654
655struct ColorLayerProperties : public BaseLayerProperties<ColorLayerProperties> {};
656
657struct SidebandLayerProperties : public BaseLayerProperties<SidebandLayerProperties> {
658 using Base = BaseLayerProperties<SidebandLayerProperties>;
659 static constexpr IComposerClient::BlendMode BLENDMODE = IComposerClient::BlendMode::NONE;
660
661 static void setupLayerState(CompositionTest* test, sp<BufferQueueLayer> layer) {
662 sp<NativeHandle> stream =
663 NativeHandle::create(reinterpret_cast<native_handle_t*>(DEFAULT_SIDEBAND_STREAM),
664 false);
665 test->mFlinger.setLayerSidebandStream(layer, stream);
666 }
667
668 static void setupHwcSetSourceCropBufferCallExpectations(CompositionTest* test) {
669 EXPECT_CALL(*test->mComposer,
670 setLayerSourceCrop(HWC_DISPLAY, HWC_LAYER,
671 IComposerClient::FRect({0.f, 0.f, -1.f, -1.f})))
672 .Times(1);
673 }
674
675 static void setupHwcSetPerFrameBufferCallExpectations(CompositionTest* test) {
676 EXPECT_CALL(*test->mComposer,
677 setLayerSidebandStream(HWC_DISPLAY, HWC_LAYER,
678 reinterpret_cast<native_handle_t*>(
679 DEFAULT_SIDEBAND_STREAM)))
680 .WillOnce(Return(Error::NONE));
681
682 EXPECT_CALL(*test->mComposer, setLayerSurfaceDamage(HWC_DISPLAY, HWC_LAYER, _)).Times(1);
683 }
684
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000685 static void setupREBufferCompositionCommonCallExpectations(CompositionTest* /*test*/) {}
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700686};
687
688struct SecureLayerProperties : public BaseLayerProperties<SecureLayerProperties> {
689 using Base = BaseLayerProperties<SecureLayerProperties>;
690
691 static constexpr uint32_t LAYER_FLAGS = ISurfaceComposerClient::eSecure;
692
693 static void setupInsecureREBufferCompositionCommonCallExpectations(CompositionTest* test) {
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000694 EXPECT_CALL(*test->mRenderEngine, drawLayers)
695 .WillOnce([](const renderengine::DisplaySettings& displaySettings,
696 const std::vector<renderengine::LayerSettings>& layerSettings,
Alec Mouri6338c9d2019-02-07 16:57:51 -0800697 ANativeWindowBuffer*, base::unique_fd&&,
698 base::unique_fd*) -> status_t {
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000699 EXPECT_EQ(DEFAULT_DISPLAY_MAX_LUMINANCE, displaySettings.maxLuminance);
700 EXPECT_EQ(Rect(DEFAULT_DISPLAY_WIDTH, DEFAULT_DISPLAY_HEIGHT),
701 displaySettings.physicalDisplay);
702 EXPECT_EQ(Rect(DEFAULT_DISPLAY_WIDTH, DEFAULT_DISPLAY_HEIGHT),
703 displaySettings.clip);
704 // screen capture adds an additional color layer as an alpha
705 // prefill, so get the back layer.
706 renderengine::LayerSettings layer = layerSettings.back();
707 EXPECT_THAT(layer.source.buffer.buffer, IsNull());
708 EXPECT_EQ(half3(0.0f, 0.0f, 0.0f), layer.source.solidColor);
709 EXPECT_EQ(0.0, layer.geometry.roundedCornersRadius);
710 EXPECT_EQ(ui::Dataspace::UNKNOWN, layer.sourceDataspace);
711 EXPECT_EQ(1.0f, layer.alpha);
712 return NO_ERROR;
713 });
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700714 }
715
716 static void setupInsecureREBufferCompositionCallExpectations(CompositionTest* test) {
717 setupInsecureREBufferCompositionCommonCallExpectations(test);
718 Base::setupBufferLayerPostFrameCallExpectations(test);
719 }
720
721 static void setupInsecureREBufferScreenshotCompositionCallExpectations(CompositionTest* test) {
722 setupInsecureREBufferCompositionCommonCallExpectations(test);
723 }
724};
725
726struct CursorLayerProperties : public BaseLayerProperties<CursorLayerProperties> {
727 using Base = BaseLayerProperties<CursorLayerProperties>;
728
729 static void setupLayerState(CompositionTest* test, sp<BufferQueueLayer> layer) {
730 Base::setupLayerState(test, layer);
731 test->mFlinger.setLayerPotentialCursor(layer, true);
732 }
733};
734
735struct NoLayerVariant {
736 using FlingerLayerType = sp<BufferQueueLayer>;
737
738 static FlingerLayerType createLayer(CompositionTest*) { return FlingerLayerType(); }
739 static void injectLayer(CompositionTest*, FlingerLayerType) {}
740 static void cleanupInjectedLayers(CompositionTest*) {}
741
742 static void setupCallExpectationsForDirtyGeometry(CompositionTest*) {}
743 static void setupCallExpectationsForDirtyFrame(CompositionTest*) {}
744};
745
746template <typename LayerProperties>
747struct BaseLayerVariant {
748 template <typename L, typename F>
749 static sp<L> createLayerWithFactory(CompositionTest* test, F factory) {
750 EXPECT_CALL(*test->mMessageQueue, postMessage(_, 0)).Times(0);
751
752 sp<L> layer = factory();
753
754 Mock::VerifyAndClear(test->mComposer);
755 Mock::VerifyAndClear(test->mRenderEngine);
756 Mock::VerifyAndClear(test->mMessageQueue);
757
758 auto& layerDrawingState = test->mFlinger.mutableLayerDrawingState(layer);
759 layerDrawingState.layerStack = DEFAULT_LAYER_STACK;
760 layerDrawingState.active.w = 100;
761 layerDrawingState.active.h = 100;
762 layerDrawingState.color = half4(LayerProperties::COLOR[0], LayerProperties::COLOR[1],
763 LayerProperties::COLOR[2], LayerProperties::COLOR[3]);
Vishnu Nair4351ad52019-02-11 14:13:02 -0800764 layer->computeBounds(FloatRect(0, 0, 100, 100), ui::Transform());
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700765 layer->setVisibleRegion(Region(Rect(0, 0, 100, 100)));
766
767 return layer;
768 }
769
770 static void injectLayer(CompositionTest* test, sp<Layer> layer) {
771 EXPECT_CALL(*test->mComposer, createLayer(HWC_DISPLAY, _))
772 .WillOnce(DoAll(SetArgPointee<1>(HWC_LAYER), Return(Error::NONE)));
773
Lloyd Pique07e33212018-12-18 16:33:37 -0800774 std::vector<std::unique_ptr<compositionengine::OutputLayer>> outputLayers;
775 outputLayers.emplace_back(test->mDisplay->getCompositionDisplay()
776 ->getOrCreateOutputLayer(DEFAULT_DISPLAY_ID,
777 layer->getCompositionLayer(),
778 layer));
779
780 test->mDisplay->getCompositionDisplay()->setOutputLayersOrderedByZ(std::move(outputLayers));
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700781
782 Mock::VerifyAndClear(test->mComposer);
783
784 Vector<sp<Layer>> layers;
785 layers.add(layer);
786 test->mDisplay->setVisibleLayersSortedByZ(layers);
787 test->mFlinger.mutableDrawingState().layersSortedByZ.add(layer);
788 }
789
790 static void cleanupInjectedLayers(CompositionTest* test) {
791 EXPECT_CALL(*test->mComposer, destroyLayer(HWC_DISPLAY, HWC_LAYER))
792 .WillOnce(Return(Error::NONE));
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800793
794 test->mDisplay->getCompositionDisplay()->setOutputLayersOrderedByZ(
795 std::vector<std::unique_ptr<compositionengine::OutputLayer>>());
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700796 test->mFlinger.mutableDrawingState().layersSortedByZ.clear();
797 }
798};
799
800template <typename LayerProperties>
801struct ColorLayerVariant : public BaseLayerVariant<LayerProperties> {
802 using Base = BaseLayerVariant<LayerProperties>;
803 using FlingerLayerType = sp<ColorLayer>;
804
805 static FlingerLayerType createLayer(CompositionTest* test) {
806 FlingerLayerType layer = Base::template createLayerWithFactory<ColorLayer>(test, [test]() {
Lloyd Pique42ab75e2018-09-12 20:46:03 -0700807 return new ColorLayer(LayerCreationArgs(test->mFlinger.mFlinger.get(), sp<Client>(),
808 String8("test-layer"), LayerProperties::WIDTH,
809 LayerProperties::HEIGHT,
Evan Roskya1f1e152019-01-24 16:17:46 -0800810 LayerProperties::LAYER_FLAGS, LayerMetadata()));
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700811 });
Vishnu Nair60356342018-11-13 13:00:45 -0800812
813 auto& layerDrawingState = test->mFlinger.mutableLayerDrawingState(layer);
814 layerDrawingState.crop_legacy = Rect(0, 0, LayerProperties::HEIGHT, LayerProperties::WIDTH);
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700815 return layer;
816 }
817
818 static void setupRECompositionCallExpectations(CompositionTest* test) {
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700819 LayerProperties::setupREColorCompositionCallExpectations(test);
820 }
821
822 static void setupREScreenshotCompositionCallExpectations(CompositionTest* test) {
823 LayerProperties::setupREColorScreenshotCompositionCallExpectations(test);
824 }
825
826 static void setupCallExpectationsForDirtyGeometry(CompositionTest* test) {
827 LayerProperties::setupHwcSetGeometryCallExpectations(test);
828 LayerProperties::setupHwcSetSourceCropColorCallExpectations(test);
829 }
830
831 static void setupCallExpectationsForDirtyFrame(CompositionTest* test) {
832 LayerProperties::setupHwcSetPerFrameCallExpectations(test);
833 LayerProperties::setupHwcSetPerFrameColorCallExpectations(test);
834 }
835};
836
837template <typename LayerProperties>
838struct BufferLayerVariant : public BaseLayerVariant<LayerProperties> {
839 using Base = BaseLayerVariant<LayerProperties>;
840 using FlingerLayerType = sp<BufferQueueLayer>;
841
842 static FlingerLayerType createLayer(CompositionTest* test) {
843 test->mFlinger.mutableTexturePool().push_back(DEFAULT_TEXTURE_ID);
844
845 FlingerLayerType layer =
846 Base::template createLayerWithFactory<BufferQueueLayer>(test, [test]() {
Lloyd Pique42ab75e2018-09-12 20:46:03 -0700847 return new BufferQueueLayer(
848 LayerCreationArgs(test->mFlinger.mFlinger.get(), sp<Client>(),
849 String8("test-layer"), LayerProperties::WIDTH,
850 LayerProperties::HEIGHT,
Evan Roskya1f1e152019-01-24 16:17:46 -0800851 LayerProperties::LAYER_FLAGS, LayerMetadata()));
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700852 });
853
854 LayerProperties::setupLayerState(test, layer);
855
856 return layer;
857 }
858
859 static void cleanupInjectedLayers(CompositionTest* test) {
860 EXPECT_CALL(*test->mMessageQueue, postMessage(_, 0)).Times(2);
861 Base::cleanupInjectedLayers(test);
862 }
863
864 static void setupCallExpectationsForDirtyGeometry(CompositionTest* test) {
865 LayerProperties::setupHwcSetGeometryCallExpectations(test);
866 LayerProperties::setupHwcSetSourceCropBufferCallExpectations(test);
867 }
868
869 static void setupCallExpectationsForDirtyFrame(CompositionTest* test) {
870 LayerProperties::setupHwcSetPerFrameCallExpectations(test);
871 LayerProperties::setupHwcSetPerFrameBufferCallExpectations(test);
872 }
873
874 static void setupRECompositionCallExpectations(CompositionTest* test) {
875 LayerProperties::setupREBufferCompositionCallExpectations(test);
876 }
877
878 static void setupInsecureRECompositionCallExpectations(CompositionTest* test) {
879 LayerProperties::setupInsecureREBufferCompositionCallExpectations(test);
880 }
881
882 static void setupREScreenshotCompositionCallExpectations(CompositionTest* test) {
883 LayerProperties::setupREBufferScreenshotCompositionCallExpectations(test);
884 }
885
886 static void setupInsecureREScreenshotCompositionCallExpectations(CompositionTest* test) {
887 LayerProperties::setupInsecureREBufferScreenshotCompositionCallExpectations(test);
888 }
889};
890
891/* ------------------------------------------------------------------------
892 * Variants to control how the composition type is changed
893 */
894
895struct NoCompositionTypeVariant {
896 static void setupHwcSetCallExpectations(CompositionTest*) {}
897
898 static void setupHwcGetCallExpectations(CompositionTest* test) {
899 EXPECT_CALL(*test->mComposer, getChangedCompositionTypes(HWC_DISPLAY, _, _)).Times(1);
900 }
901};
902
903template <IComposerClient::Composition CompositionType>
904struct KeepCompositionTypeVariant {
905 static constexpr HWC2::Composition TYPE = static_cast<HWC2::Composition>(CompositionType);
906
907 static void setupHwcSetCallExpectations(CompositionTest* test) {
908 EXPECT_CALL(*test->mComposer,
909 setLayerCompositionType(HWC_DISPLAY, HWC_LAYER, CompositionType))
910 .Times(1);
911 }
912
913 static void setupHwcGetCallExpectations(CompositionTest* test) {
914 EXPECT_CALL(*test->mComposer, getChangedCompositionTypes(HWC_DISPLAY, _, _)).Times(1);
915 }
916};
917
918template <IComposerClient::Composition InitialCompositionType,
919 IComposerClient::Composition FinalCompositionType>
920struct ChangeCompositionTypeVariant {
921 static constexpr HWC2::Composition TYPE = static_cast<HWC2::Composition>(FinalCompositionType);
922
923 static void setupHwcSetCallExpectations(CompositionTest* test) {
924 EXPECT_CALL(*test->mComposer,
925 setLayerCompositionType(HWC_DISPLAY, HWC_LAYER, InitialCompositionType))
926 .Times(1);
927 }
928
929 static void setupHwcGetCallExpectations(CompositionTest* test) {
930 EXPECT_CALL(*test->mComposer, getChangedCompositionTypes(HWC_DISPLAY, _, _))
931 .WillOnce(DoAll(SetArgPointee<1>(std::vector<Hwc2::Layer>{
932 static_cast<Hwc2::Layer>(HWC_LAYER)}),
933 SetArgPointee<2>(std::vector<IComposerClient::Composition>{
934 FinalCompositionType}),
935 Return(Error::NONE)));
936 }
937};
938
939/* ------------------------------------------------------------------------
940 * Variants to select how the composition is expected to be handled
941 */
942
943struct CompositionResultBaseVariant {
944 static void setupLayerState(CompositionTest*, sp<Layer>) {}
945
946 template <typename Case>
947 static void setupCallExpectationsForDirtyGeometry(CompositionTest* test) {
948 Case::Layer::setupCallExpectationsForDirtyGeometry(test);
949 }
950
951 template <typename Case>
952 static void setupCallExpectationsForDirtyFrame(CompositionTest* test) {
953 Case::Layer::setupCallExpectationsForDirtyFrame(test);
954 }
955};
956
957struct NoCompositionResultVariant : public CompositionResultBaseVariant {
958 template <typename Case>
959 static void setupCallExpectations(CompositionTest* test) {
960 Case::Display::setupEmptyFrameCompositionCallExpectations(test);
961 Case::Display::setupHwcCompositionCallExpectations(test);
962 }
963};
964
965struct HwcCompositionResultVariant : public CompositionResultBaseVariant {
966 template <typename Case>
967 static void setupCallExpectations(CompositionTest* test) {
968 Case::Display::setupNonEmptyFrameCompositionCallExpectations(test);
969 Case::Display::setupHwcCompositionCallExpectations(test);
970 }
971};
972
973struct RECompositionResultVariant : public CompositionResultBaseVariant {
974 template <typename Case>
975 static void setupCallExpectations(CompositionTest* test) {
976 Case::Display::setupNonEmptyFrameCompositionCallExpectations(test);
977 Case::Display::setupRECompositionCallExpectations(test);
978 Case::Display::template setupRELayerCompositionCallExpectations<Case>(test);
979 }
980};
981
982struct ForcedClientCompositionResultVariant : public RECompositionResultVariant {
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800983 static void setupLayerState(CompositionTest* test, sp<Layer> layer) {
984 layer->forceClientComposition(test->mDisplay);
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700985 }
986
987 template <typename Case>
988 static void setupCallExpectationsForDirtyGeometry(CompositionTest*) {}
989
990 template <typename Case>
991 static void setupCallExpectationsForDirtyFrame(CompositionTest*) {}
992};
993
994struct EmptyScreenshotResultVariant {
995 static void setupLayerState(CompositionTest*, sp<Layer>) {}
996
997 template <typename Case>
998 static void setupCallExpectations(CompositionTest*) {}
999};
1000
1001struct REScreenshotResultVariant : public EmptyScreenshotResultVariant {
1002 using Base = EmptyScreenshotResultVariant;
1003
1004 template <typename Case>
1005 static void setupCallExpectations(CompositionTest* test) {
1006 Base::template setupCallExpectations<Case>(test);
1007 Case::Display::template setupRELayerScreenshotCompositionCallExpectations<Case>(test);
1008 }
1009};
1010
1011/* ------------------------------------------------------------------------
1012 * Composition test case, containing all the variants being tested
1013 */
1014
1015template <typename DisplayCase, typename LayerCase, typename CompositionTypeCase,
1016 typename CompositionResultCase>
1017struct CompositionCase {
1018 using ThisCase =
1019 CompositionCase<DisplayCase, LayerCase, CompositionTypeCase, CompositionResultCase>;
1020 using Display = DisplayCase;
1021 using Layer = LayerCase;
1022 using CompositionType = CompositionTypeCase;
1023 using CompositionResult = CompositionResultCase;
1024
1025 static void setupCommon(CompositionTest* test) {
1026 Display::setupPreconditions(test);
1027
1028 auto layer = Layer::createLayer(test);
1029 Layer::injectLayer(test, layer);
1030 CompositionResult::setupLayerState(test, layer);
1031 }
1032
1033 static void setupForDirtyGeometry(CompositionTest* test) {
1034 setupCommon(test);
1035
1036 Display::template setupCommonCompositionCallExpectations<ThisCase>(test);
1037 CompositionResult::template setupCallExpectationsForDirtyGeometry<ThisCase>(test);
1038 CompositionResult::template setupCallExpectationsForDirtyFrame<ThisCase>(test);
1039 CompositionResult::template setupCallExpectations<ThisCase>(test);
1040 }
1041
1042 static void setupForDirtyFrame(CompositionTest* test) {
1043 setupCommon(test);
1044
1045 Display::template setupCommonCompositionCallExpectations<ThisCase>(test);
1046 CompositionResult::template setupCallExpectationsForDirtyFrame<ThisCase>(test);
1047 CompositionResult::template setupCallExpectations<ThisCase>(test);
1048 }
1049
1050 static void setupForScreenCapture(CompositionTest* test) {
1051 setupCommon(test);
1052
1053 Display::template setupCommonScreensCaptureCallExpectations<ThisCase>(test);
1054 CompositionResult::template setupCallExpectations<ThisCase>(test);
1055 }
1056
1057 static void cleanup(CompositionTest* test) {
1058 Layer::cleanupInjectedLayers(test);
1059
1060 for (auto& hwcDisplay : test->mFlinger.mFakeHwcDisplays) {
1061 hwcDisplay->mutableLayers().clear();
1062 }
1063
1064 test->mDisplay->setVisibleLayersSortedByZ(Vector<sp<android::Layer>>());
1065 }
1066};
1067
1068/* ------------------------------------------------------------------------
1069 * Composition cases to test
1070 */
1071
1072TEST_F(CompositionTest, noLayersDoesMinimalWorkWithDirtyGeometry) {
1073 displayRefreshCompositionDirtyGeometry<
1074 CompositionCase<DefaultDisplaySetupVariant, NoLayerVariant, NoCompositionTypeVariant,
1075 NoCompositionResultVariant>>();
1076}
1077
1078TEST_F(CompositionTest, noLayersDoesMinimalWorkWithDirtyFrame) {
1079 displayRefreshCompositionDirtyFrame<
1080 CompositionCase<DefaultDisplaySetupVariant, NoLayerVariant, NoCompositionTypeVariant,
1081 NoCompositionResultVariant>>();
1082}
1083
1084TEST_F(CompositionTest, noLayersDoesMinimalWorkToCaptureScreen) {
1085 captureScreenComposition<
1086 CompositionCase<DefaultDisplaySetupVariant, NoLayerVariant, NoCompositionTypeVariant,
1087 EmptyScreenshotResultVariant>>();
1088}
1089
1090/* ------------------------------------------------------------------------
1091 * Simple buffer layers
1092 */
1093
1094TEST_F(CompositionTest, HWCComposedNormalBufferLayerWithDirtyGeometry) {
1095 displayRefreshCompositionDirtyGeometry<
1096 CompositionCase<DefaultDisplaySetupVariant, BufferLayerVariant<DefaultLayerProperties>,
1097 KeepCompositionTypeVariant<IComposerClient::Composition::DEVICE>,
1098 HwcCompositionResultVariant>>();
1099}
1100
1101TEST_F(CompositionTest, HWCComposedNormalBufferLayerWithDirtyFrame) {
1102 displayRefreshCompositionDirtyFrame<
1103 CompositionCase<DefaultDisplaySetupVariant, BufferLayerVariant<DefaultLayerProperties>,
1104 KeepCompositionTypeVariant<IComposerClient::Composition::DEVICE>,
1105 HwcCompositionResultVariant>>();
1106}
1107
1108TEST_F(CompositionTest, REComposedNormalBufferLayer) {
1109 displayRefreshCompositionDirtyFrame<
1110 CompositionCase<DefaultDisplaySetupVariant, BufferLayerVariant<DefaultLayerProperties>,
1111 ChangeCompositionTypeVariant<IComposerClient::Composition::DEVICE,
1112 IComposerClient::Composition::CLIENT>,
1113 RECompositionResultVariant>>();
1114}
1115
1116TEST_F(CompositionTest, captureScreenNormalBufferLayer) {
1117 captureScreenComposition<
1118 CompositionCase<DefaultDisplaySetupVariant, BufferLayerVariant<DefaultLayerProperties>,
1119 NoCompositionTypeVariant, REScreenshotResultVariant>>();
1120}
1121
1122/* ------------------------------------------------------------------------
1123 * Single-color layers
1124 */
1125
1126TEST_F(CompositionTest, HWCComposedColorLayerWithDirtyGeometry) {
1127 displayRefreshCompositionDirtyGeometry<
1128 CompositionCase<DefaultDisplaySetupVariant, ColorLayerVariant<ColorLayerProperties>,
1129 KeepCompositionTypeVariant<IComposerClient::Composition::SOLID_COLOR>,
1130 HwcCompositionResultVariant>>();
1131}
1132
1133TEST_F(CompositionTest, HWCComposedColorLayerWithDirtyFrame) {
1134 displayRefreshCompositionDirtyFrame<
1135 CompositionCase<DefaultDisplaySetupVariant, ColorLayerVariant<ColorLayerProperties>,
1136 KeepCompositionTypeVariant<IComposerClient::Composition::SOLID_COLOR>,
1137 HwcCompositionResultVariant>>();
1138}
1139
1140TEST_F(CompositionTest, REComposedColorLayer) {
1141 displayRefreshCompositionDirtyFrame<
1142 CompositionCase<DefaultDisplaySetupVariant, ColorLayerVariant<ColorLayerProperties>,
1143 ChangeCompositionTypeVariant<IComposerClient::Composition::SOLID_COLOR,
1144 IComposerClient::Composition::CLIENT>,
1145 RECompositionResultVariant>>();
1146}
1147
1148TEST_F(CompositionTest, captureScreenColorLayer) {
1149 captureScreenComposition<
1150 CompositionCase<DefaultDisplaySetupVariant, ColorLayerVariant<ColorLayerProperties>,
1151 NoCompositionTypeVariant, REScreenshotResultVariant>>();
1152}
1153
1154/* ------------------------------------------------------------------------
1155 * Layers with sideband buffers
1156 */
1157
1158TEST_F(CompositionTest, HWCComposedSidebandBufferLayerWithDirtyGeometry) {
1159 displayRefreshCompositionDirtyGeometry<
1160 CompositionCase<DefaultDisplaySetupVariant, BufferLayerVariant<SidebandLayerProperties>,
1161 KeepCompositionTypeVariant<IComposerClient::Composition::SIDEBAND>,
1162 HwcCompositionResultVariant>>();
1163}
1164
1165TEST_F(CompositionTest, HWCComposedSidebandBufferLayerWithDirtyFrame) {
1166 displayRefreshCompositionDirtyFrame<
1167 CompositionCase<DefaultDisplaySetupVariant, BufferLayerVariant<SidebandLayerProperties>,
1168 KeepCompositionTypeVariant<IComposerClient::Composition::SIDEBAND>,
1169 HwcCompositionResultVariant>>();
1170}
1171
1172TEST_F(CompositionTest, REComposedSidebandBufferLayer) {
1173 displayRefreshCompositionDirtyFrame<
1174 CompositionCase<DefaultDisplaySetupVariant, BufferLayerVariant<SidebandLayerProperties>,
1175 ChangeCompositionTypeVariant<IComposerClient::Composition::SIDEBAND,
1176 IComposerClient::Composition::CLIENT>,
1177 RECompositionResultVariant>>();
1178}
1179
1180TEST_F(CompositionTest, captureScreenSidebandBufferLayer) {
1181 captureScreenComposition<
1182 CompositionCase<DefaultDisplaySetupVariant, BufferLayerVariant<SidebandLayerProperties>,
1183 NoCompositionTypeVariant, REScreenshotResultVariant>>();
1184}
1185
1186/* ------------------------------------------------------------------------
1187 * Layers with ISurfaceComposerClient::eSecure, on a secure display
1188 */
1189
1190TEST_F(CompositionTest, HWCComposedSecureBufferLayerWithDirtyGeometry) {
1191 displayRefreshCompositionDirtyGeometry<
1192 CompositionCase<DefaultDisplaySetupVariant, BufferLayerVariant<SecureLayerProperties>,
1193 KeepCompositionTypeVariant<IComposerClient::Composition::DEVICE>,
1194 HwcCompositionResultVariant>>();
1195}
1196
1197TEST_F(CompositionTest, HWCComposedSecureBufferLayerWithDirtyFrame) {
1198 displayRefreshCompositionDirtyFrame<
1199 CompositionCase<DefaultDisplaySetupVariant, BufferLayerVariant<SecureLayerProperties>,
1200 KeepCompositionTypeVariant<IComposerClient::Composition::DEVICE>,
1201 HwcCompositionResultVariant>>();
1202}
1203
1204TEST_F(CompositionTest, REComposedSecureBufferLayer) {
1205 displayRefreshCompositionDirtyFrame<
1206 CompositionCase<DefaultDisplaySetupVariant, BufferLayerVariant<SecureLayerProperties>,
1207 ChangeCompositionTypeVariant<IComposerClient::Composition::DEVICE,
1208 IComposerClient::Composition::CLIENT>,
1209 RECompositionResultVariant>>();
1210}
1211
1212TEST_F(CompositionTest, captureScreenSecureBufferLayerOnSecureDisplay) {
1213 captureScreenComposition<
1214 CompositionCase<DefaultDisplaySetupVariant, BufferLayerVariant<SecureLayerProperties>,
1215 NoCompositionTypeVariant, REScreenshotResultVariant>>();
1216}
1217
1218/* ------------------------------------------------------------------------
1219 * Layers with ISurfaceComposerClient::eSecure, on a non-secure display
1220 */
1221
1222TEST_F(CompositionTest, HWCComposedSecureBufferLayerOnInsecureDisplayWithDirtyGeometry) {
1223 displayRefreshCompositionDirtyGeometry<
1224 CompositionCase<InsecureDisplaySetupVariant, BufferLayerVariant<SecureLayerProperties>,
1225 KeepCompositionTypeVariant<IComposerClient::Composition::CLIENT>,
1226 ForcedClientCompositionResultVariant>>();
1227}
1228
1229TEST_F(CompositionTest, HWCComposedSecureBufferLayerOnInsecureDisplayWithDirtyFrame) {
1230 displayRefreshCompositionDirtyFrame<
1231 CompositionCase<InsecureDisplaySetupVariant, BufferLayerVariant<SecureLayerProperties>,
1232 KeepCompositionTypeVariant<IComposerClient::Composition::CLIENT>,
1233 ForcedClientCompositionResultVariant>>();
1234}
1235
1236TEST_F(CompositionTest, captureScreenSecureBufferLayerOnInsecureDisplay) {
1237 captureScreenComposition<
1238 CompositionCase<InsecureDisplaySetupVariant, BufferLayerVariant<SecureLayerProperties>,
1239 NoCompositionTypeVariant, REScreenshotResultVariant>>();
1240}
1241
1242/* ------------------------------------------------------------------------
1243 * Cursor layers
1244 */
1245
1246TEST_F(CompositionTest, HWCComposedCursorLayerWithDirtyGeometry) {
1247 displayRefreshCompositionDirtyGeometry<
1248 CompositionCase<DefaultDisplaySetupVariant, BufferLayerVariant<CursorLayerProperties>,
1249 KeepCompositionTypeVariant<IComposerClient::Composition::CURSOR>,
1250 HwcCompositionResultVariant>>();
1251}
1252
1253TEST_F(CompositionTest, HWCComposedCursorLayerWithDirtyFrame) {
1254 displayRefreshCompositionDirtyFrame<
1255 CompositionCase<DefaultDisplaySetupVariant, BufferLayerVariant<CursorLayerProperties>,
1256 KeepCompositionTypeVariant<IComposerClient::Composition::CURSOR>,
1257 HwcCompositionResultVariant>>();
1258}
1259
1260TEST_F(CompositionTest, REComposedCursorLayer) {
1261 displayRefreshCompositionDirtyFrame<
1262 CompositionCase<DefaultDisplaySetupVariant, BufferLayerVariant<CursorLayerProperties>,
1263 ChangeCompositionTypeVariant<IComposerClient::Composition::CURSOR,
1264 IComposerClient::Composition::CLIENT>,
1265 RECompositionResultVariant>>();
1266}
1267
1268TEST_F(CompositionTest, captureScreenCursorLayer) {
1269 captureScreenComposition<
1270 CompositionCase<DefaultDisplaySetupVariant, BufferLayerVariant<CursorLayerProperties>,
1271 NoCompositionTypeVariant, REScreenshotResultVariant>>();
1272}
1273
1274/* ------------------------------------------------------------------------
1275 * Simple buffer layer on a display which is powered off.
1276 */
1277
1278TEST_F(CompositionTest, displayOffHWCComposedNormalBufferLayerWithDirtyGeometry) {
1279 displayRefreshCompositionDirtyGeometry<CompositionCase<
1280 PoweredOffDisplaySetupVariant, BufferLayerVariant<DefaultLayerProperties>,
1281 KeepCompositionTypeVariant<IComposerClient::Composition::DEVICE>,
1282 HwcCompositionResultVariant>>();
1283}
1284
1285TEST_F(CompositionTest, displayOffHWCComposedNormalBufferLayerWithDirtyFrame) {
1286 displayRefreshCompositionDirtyFrame<CompositionCase<
1287 PoweredOffDisplaySetupVariant, BufferLayerVariant<DefaultLayerProperties>,
1288 KeepCompositionTypeVariant<IComposerClient::Composition::DEVICE>,
1289 HwcCompositionResultVariant>>();
1290}
1291
1292TEST_F(CompositionTest, displayOffREComposedNormalBufferLayer) {
1293 displayRefreshCompositionDirtyFrame<CompositionCase<
1294 PoweredOffDisplaySetupVariant, BufferLayerVariant<DefaultLayerProperties>,
1295 ChangeCompositionTypeVariant<IComposerClient::Composition::DEVICE,
1296 IComposerClient::Composition::CLIENT>,
1297 RECompositionResultVariant>>();
1298}
1299
1300TEST_F(CompositionTest, captureScreenNormalBufferLayerOnPoweredOffDisplay) {
1301 captureScreenComposition<CompositionCase<
1302 PoweredOffDisplaySetupVariant, BufferLayerVariant<DefaultLayerProperties>,
1303 NoCompositionTypeVariant, REScreenshotResultVariant>>();
1304}
1305
1306} // namespace
1307} // namespace android