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