blob: 3addd61e5522e28a7035864a56c7384bb364c883 [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
36#include "TestableSurfaceFlinger.h"
37#include "mock/DisplayHardware/MockComposer.h"
Lloyd Piqued6b579f2018-04-06 15:29:10 -070038#include "mock/MockDispSync.h"
39#include "mock/MockEventControlThread.h"
40#include "mock/MockEventThread.h"
41#include "mock/MockMessageQueue.h"
Alec Mouriba013fa2018-10-16 12:43:11 -070042#include "mock/system/window/MockNativeWindow.h"
Lloyd Piqued6b579f2018-04-06 15:29:10 -070043
44namespace android {
45namespace {
46
47using testing::_;
David Sodman15094112018-10-11 09:39:37 -070048using testing::AtLeast;
Alec Mourie7d1d4a2019-02-05 01:13:46 +000049using testing::Between;
Lloyd Piqued6b579f2018-04-06 15:29:10 -070050using testing::ByMove;
51using testing::DoAll;
Alec Mourie7d1d4a2019-02-05 01:13:46 +000052using testing::Field;
Alec Mouri0a9c7b82018-11-16 13:05:25 -080053using testing::Invoke;
Lloyd Piqued6b579f2018-04-06 15:29:10 -070054using testing::IsNull;
55using testing::Mock;
56using testing::NotNull;
57using testing::Ref;
58using testing::Return;
59using testing::ReturnRef;
60using testing::SetArgPointee;
61
62using android::Hwc2::Error;
63using android::Hwc2::IComposer;
64using android::Hwc2::IComposerClient;
65using android::Hwc2::Transform;
66
67using FakeHwcDisplayInjector = TestableSurfaceFlinger::FakeHwcDisplayInjector;
68using FakeDisplayDeviceInjector = TestableSurfaceFlinger::FakeDisplayDeviceInjector;
69
70constexpr hwc2_display_t HWC_DISPLAY = FakeHwcDisplayInjector::DEFAULT_HWC_DISPLAY_ID;
71constexpr hwc2_layer_t HWC_LAYER = 5000;
72constexpr Transform DEFAULT_TRANSFORM = static_cast<Transform>(0);
73
Dominik Laskowski34157762018-10-31 13:07:19 -070074constexpr DisplayId DEFAULT_DISPLAY_ID = DisplayId{42};
Lloyd Piqued6b579f2018-04-06 15:29:10 -070075constexpr int DEFAULT_DISPLAY_WIDTH = 1920;
76constexpr int DEFAULT_DISPLAY_HEIGHT = 1024;
77
78constexpr int DEFAULT_CONFIG_ID = 0;
79constexpr int DEFAULT_TEXTURE_ID = 6000;
80constexpr int DEFAULT_LAYER_STACK = 7000;
81
82constexpr int DEFAULT_DISPLAY_MAX_LUMINANCE = 500;
83
84constexpr int DEFAULT_SIDEBAND_STREAM = 51;
85
86class CompositionTest : public testing::Test {
87public:
88 CompositionTest() {
89 const ::testing::TestInfo* const test_info =
90 ::testing::UnitTest::GetInstance()->current_test_info();
91 ALOGD("**** Setting up for %s.%s\n", test_info->test_case_name(), test_info->name());
92
93 mFlinger.mutableEventControlThread().reset(mEventControlThread);
94 mFlinger.mutableEventThread().reset(mEventThread);
95 mFlinger.mutableEventQueue().reset(mMessageQueue);
96
97 mFlinger.mutablePrimaryDispSync().reset(mPrimaryDispSync);
98 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
127 void setupForceGeometryDirty() {
128 // TODO: This requires the visible region and other related
129 // state to be set, and is problematic for BufferLayers since they are
130 // not visible without a buffer (and setting up a buffer looks like a
131 // pain)
132 // mFlinger.mutableVisibleRegionsDirty() = true;
133
134 mFlinger.mutableGeometryInvalid() = true;
135 }
136
137 template <typename Case>
138 void displayRefreshCompositionDirtyGeometry();
139
140 template <typename Case>
141 void displayRefreshCompositionDirtyFrame();
142
143 template <typename Case>
144 void captureScreenComposition();
145
146 std::unordered_set<HWC2::Capability> mDefaultCapabilities = {HWC2::Capability::SidebandStream};
147
148 TestableSurfaceFlinger mFlinger;
149 sp<DisplayDevice> mDisplay;
150 sp<DisplayDevice> mExternalDisplay;
Lloyd Pique542307f2018-10-19 13:24:08 -0700151 sp<compositionengine::mock::DisplaySurface> mDisplaySurface =
152 new compositionengine::mock::DisplaySurface();
Alec Mouriba013fa2018-10-16 12:43:11 -0700153 mock::NativeWindow* mNativeWindow = new mock::NativeWindow();
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700154
Alec Mouri0a9c7b82018-11-16 13:05:25 -0800155 sp<GraphicBuffer> mBuffer = new GraphicBuffer();
156 ANativeWindowBuffer* mNativeWindowBuffer = mBuffer->getNativeBuffer();
157
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700158 mock::EventThread* mEventThread = new mock::EventThread();
159 mock::EventControlThread* mEventControlThread = new mock::EventControlThread();
160
161 Hwc2::mock::Composer* mComposer = nullptr;
162 renderengine::mock::RenderEngine* mRenderEngine = new renderengine::mock::RenderEngine();
163 mock::MessageQueue* mMessageQueue = new mock::MessageQueue();
164 mock::DispSync* mPrimaryDispSync = new mock::DispSync();
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700165 renderengine::mock::Framebuffer* mReFrameBuffer = new renderengine::mock::Framebuffer();
166
167 sp<Fence> mClientTargetAcquireFence = Fence::NO_FENCE;
168
169 sp<GraphicBuffer> mCaptureScreenBuffer;
170};
171
172template <typename LayerCase>
173void CompositionTest::displayRefreshCompositionDirtyGeometry() {
174 setupForceGeometryDirty();
175 LayerCase::setupForDirtyGeometry(this);
176
177 // --------------------------------------------------------------------
178 // Invocation
179
180 mFlinger.onMessageReceived(MessageQueue::INVALIDATE);
181 mFlinger.onMessageReceived(MessageQueue::REFRESH);
182
183 LayerCase::cleanup(this);
184}
185
186template <typename LayerCase>
187void CompositionTest::displayRefreshCompositionDirtyFrame() {
188 LayerCase::setupForDirtyFrame(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::captureScreenComposition() {
201 LayerCase::setupForScreenCapture(this);
202
203 const Rect sourceCrop(0, 0, DEFAULT_DISPLAY_WIDTH, DEFAULT_DISPLAY_HEIGHT);
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700204 constexpr bool useIdentityTransform = true;
205 constexpr bool forSystem = true;
206
207 DisplayRenderArea renderArea(mDisplay, sourceCrop, DEFAULT_DISPLAY_WIDTH,
Peiyong Lin0e003c92018-09-17 11:09:51 -0700208 DEFAULT_DISPLAY_HEIGHT, ui::Dataspace::V0_SRGB,
209 ui::Transform::ROT_0);
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700210
211 auto traverseLayers = [this](const LayerVector::Visitor& visitor) {
chaviw0e3479f2018-09-10 16:49:30 -0700212 return mFlinger.traverseLayersInDisplay(mDisplay, visitor);
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700213 };
214
215 // TODO: Eliminate expensive/real allocation if possible.
216 const uint32_t usage = GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN |
217 GRALLOC_USAGE_HW_RENDER | GRALLOC_USAGE_HW_TEXTURE;
218 mCaptureScreenBuffer = new GraphicBuffer(renderArea.getReqWidth(), renderArea.getReqHeight(),
219 HAL_PIXEL_FORMAT_RGBA_8888, 1, usage, "screenshot");
220
221 int fd = -1;
222 status_t result =
223 mFlinger.captureScreenImplLocked(renderArea, traverseLayers, mCaptureScreenBuffer.get(),
224 useIdentityTransform, forSystem, &fd);
225 if (fd >= 0) {
226 close(fd);
227 }
228
229 EXPECT_EQ(NO_ERROR, result);
230
231 LayerCase::cleanup(this);
232}
233
234/* ------------------------------------------------------------------------
235 * Variants for each display configuration which can be tested
236 */
237
238template <typename Derived>
239struct BaseDisplayVariant {
240 static constexpr bool IS_SECURE = true;
241 static constexpr int INIT_POWER_MODE = HWC_POWER_MODE_NORMAL;
242
243 static void setupPreconditions(CompositionTest* test) {
Lloyd Pique86fa3db2019-02-04 18:46:01 -0800244 EXPECT_CALL(*test->mComposer, getDisplayCapabilities(HWC_DISPLAY, _))
245 .WillOnce(DoAll(SetArgPointee<1>(std::vector<Hwc2::DisplayCapability>({})),
246 Return(Error::NONE)));
Alec Mouriba013fa2018-10-16 12:43:11 -0700247
Dominik Laskowski075d3172018-05-24 15:50:06 -0700248 FakeHwcDisplayInjector(DEFAULT_DISPLAY_ID, HWC2::DisplayType::Physical,
249 true /* isPrimary */)
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700250 .setCapabilities(&test->mDefaultCapabilities)
251 .inject(&test->mFlinger, test->mComposer);
Lloyd Pique86fa3db2019-02-04 18:46:01 -0800252 Mock::VerifyAndClear(test->mComposer);
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700253
Lloyd Pique86fa3db2019-02-04 18:46:01 -0800254 EXPECT_CALL(*test->mNativeWindow, query(NATIVE_WINDOW_WIDTH, _))
255 .WillRepeatedly(DoAll(SetArgPointee<1>(DEFAULT_DISPLAY_WIDTH), Return(0)));
256 EXPECT_CALL(*test->mNativeWindow, query(NATIVE_WINDOW_HEIGHT, _))
257 .WillRepeatedly(DoAll(SetArgPointee<1>(DEFAULT_DISPLAY_HEIGHT), Return(0)));
258 EXPECT_CALL(*test->mNativeWindow, perform(NATIVE_WINDOW_SET_BUFFERS_FORMAT)).Times(1);
259 EXPECT_CALL(*test->mNativeWindow, perform(NATIVE_WINDOW_API_CONNECT)).Times(1);
260 EXPECT_CALL(*test->mNativeWindow, perform(NATIVE_WINDOW_SET_USAGE64)).Times(1);
Dominik Laskowski075d3172018-05-24 15:50:06 -0700261 test->mDisplay = FakeDisplayDeviceInjector(test->mFlinger, DEFAULT_DISPLAY_ID,
262 false /* isVirtual */, true /* isPrimary */)
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700263 .setDisplaySurface(test->mDisplaySurface)
Alec Mouriba013fa2018-10-16 12:43:11 -0700264 .setNativeWindow(test->mNativeWindow)
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700265 .setSecure(Derived::IS_SECURE)
266 .setPowerMode(Derived::INIT_POWER_MODE)
267 .inject();
Lloyd Pique86fa3db2019-02-04 18:46:01 -0800268 Mock::VerifyAndClear(test->mNativeWindow);
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700269 test->mDisplay->setLayerStack(DEFAULT_LAYER_STACK);
270 }
271
272 template <typename Case>
273 static void setupCommonCompositionCallExpectations(CompositionTest* test) {
274 EXPECT_CALL(*test->mComposer,
275 setColorTransform(HWC_DISPLAY, _, Hwc2::ColorTransform::IDENTITY))
276 .Times(1);
277 EXPECT_CALL(*test->mComposer, presentOrValidateDisplay(HWC_DISPLAY, _, _, _, _)).Times(1);
278 EXPECT_CALL(*test->mComposer, getDisplayRequests(HWC_DISPLAY, _, _, _)).Times(1);
279 EXPECT_CALL(*test->mComposer, acceptDisplayChanges(HWC_DISPLAY)).Times(1);
280 EXPECT_CALL(*test->mComposer, presentDisplay(HWC_DISPLAY, _)).Times(1);
281 EXPECT_CALL(*test->mComposer, getReleaseFences(HWC_DISPLAY, _, _)).Times(1);
282
283 EXPECT_CALL(*test->mRenderEngine, useNativeFenceSync()).WillRepeatedly(Return(true));
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000284 // TODO: remove once we verify that we can just grab the fence from the
285 // FramebufferSurface.
Alec Mouri0a9c7b82018-11-16 13:05:25 -0800286 EXPECT_CALL(*test->mRenderEngine, flush()).WillRepeatedly(Invoke([]() {
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000287 return base::unique_fd();
Alec Mouri0a9c7b82018-11-16 13:05:25 -0800288 }));
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700289
290 EXPECT_CALL(*test->mDisplaySurface, onFrameCommitted()).Times(1);
291 EXPECT_CALL(*test->mDisplaySurface, advanceFrame()).Times(1);
292
293 Case::CompositionType::setupHwcSetCallExpectations(test);
294 Case::CompositionType::setupHwcGetCallExpectations(test);
295 }
296
297 template <typename Case>
298 static void setupCommonScreensCaptureCallExpectations(CompositionTest* test) {
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000299 EXPECT_CALL(*test->mRenderEngine, drawLayers)
300 .WillRepeatedly(
301 [](const renderengine::DisplaySettings& displaySettings,
302 const std::vector<renderengine::LayerSettings>& /*layerSettings*/,
303 ANativeWindowBuffer*, base::unique_fd*) -> status_t {
304 EXPECT_EQ(DEFAULT_DISPLAY_MAX_LUMINANCE, displaySettings.maxLuminance);
305 EXPECT_EQ(Rect(DEFAULT_DISPLAY_WIDTH, DEFAULT_DISPLAY_HEIGHT),
306 displaySettings.physicalDisplay);
307 EXPECT_EQ(Rect(DEFAULT_DISPLAY_WIDTH, DEFAULT_DISPLAY_HEIGHT),
308 displaySettings.clip);
309 return NO_ERROR;
310 });
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700311 }
312
313 static void setupNonEmptyFrameCompositionCallExpectations(CompositionTest* test) {
314 EXPECT_CALL(*test->mDisplaySurface, beginFrame(true)).Times(1);
315 }
316
317 static void setupEmptyFrameCompositionCallExpectations(CompositionTest* test) {
318 EXPECT_CALL(*test->mDisplaySurface, beginFrame(false)).Times(1);
319 }
320
321 static void setupHwcCompositionCallExpectations(CompositionTest* test) {
Lloyd Pique542307f2018-10-19 13:24:08 -0700322 EXPECT_CALL(*test->mDisplaySurface,
323 prepareFrame(compositionengine::DisplaySurface::COMPOSITION_HWC))
324 .Times(1);
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700325 }
326
327 static void setupRECompositionCallExpectations(CompositionTest* test) {
Lloyd Pique542307f2018-10-19 13:24:08 -0700328 EXPECT_CALL(*test->mDisplaySurface,
329 prepareFrame(compositionengine::DisplaySurface::COMPOSITION_GLES))
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700330 .Times(1);
331 EXPECT_CALL(*test->mDisplaySurface, getClientTargetAcquireFence())
332 .WillRepeatedly(ReturnRef(test->mClientTargetAcquireFence));
333
Alec Mouri0a9c7b82018-11-16 13:05:25 -0800334 EXPECT_CALL(*test->mNativeWindow, queueBuffer(_, _)).WillOnce(Return(0));
335 EXPECT_CALL(*test->mNativeWindow, dequeueBuffer(_, _))
336 .WillOnce(DoAll(SetArgPointee<0>(test->mNativeWindowBuffer), SetArgPointee<1>(-1),
337 Return(0)));
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000338 EXPECT_CALL(*test->mRenderEngine, drawLayers)
339 .WillRepeatedly(
340 [](const renderengine::DisplaySettings& displaySettings,
341 const std::vector<renderengine::LayerSettings>& /*layerSettings*/,
342 ANativeWindowBuffer*, base::unique_fd*) -> status_t {
343 EXPECT_EQ(DEFAULT_DISPLAY_MAX_LUMINANCE, displaySettings.maxLuminance);
344 EXPECT_EQ(Rect(DEFAULT_DISPLAY_WIDTH, DEFAULT_DISPLAY_HEIGHT),
345 displaySettings.physicalDisplay);
346 EXPECT_EQ(Rect(DEFAULT_DISPLAY_WIDTH, DEFAULT_DISPLAY_HEIGHT),
347 displaySettings.clip);
348 EXPECT_EQ(ui::Dataspace::UNKNOWN, displaySettings.outputDataspace);
349 return NO_ERROR;
350 });
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700351 }
352
353 template <typename Case>
354 static void setupRELayerCompositionCallExpectations(CompositionTest* test) {
355 Case::Layer::setupRECompositionCallExpectations(test);
356 }
357
358 template <typename Case>
359 static void setupRELayerScreenshotCompositionCallExpectations(CompositionTest* test) {
360 Case::Layer::setupREScreenshotCompositionCallExpectations(test);
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700361 }
362};
363
364struct DefaultDisplaySetupVariant : public BaseDisplayVariant<DefaultDisplaySetupVariant> {};
365
366struct InsecureDisplaySetupVariant : public BaseDisplayVariant<InsecureDisplaySetupVariant> {
367 static constexpr bool IS_SECURE = false;
368
369 template <typename Case>
370 static void setupRELayerCompositionCallExpectations(CompositionTest* test) {
371 Case::Layer::setupInsecureRECompositionCallExpectations(test);
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700372 }
373
374 template <typename Case>
375 static void setupRELayerScreenshotCompositionCallExpectations(CompositionTest* test) {
376 Case::Layer::setupInsecureREScreenshotCompositionCallExpectations(test);
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700377 }
378};
379
380struct PoweredOffDisplaySetupVariant : public BaseDisplayVariant<PoweredOffDisplaySetupVariant> {
381 static constexpr int INIT_POWER_MODE = HWC_POWER_MODE_OFF;
382
383 template <typename Case>
384 static void setupCommonCompositionCallExpectations(CompositionTest* test) {
Lloyd Pique86fa3db2019-02-04 18:46:01 -0800385 EXPECT_CALL(*test->mRenderEngine, useNativeFenceSync()).WillRepeatedly(Return(true));
386
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700387 // TODO: This seems like an unnecessary call if display is powered off.
388 EXPECT_CALL(*test->mComposer,
389 setColorTransform(HWC_DISPLAY, _, Hwc2::ColorTransform::IDENTITY))
390 .Times(1);
391
392 // TODO: This seems like an unnecessary call if display is powered off.
393 Case::CompositionType::setupHwcSetCallExpectations(test);
394 }
395
396 static void setupHwcCompositionCallExpectations(CompositionTest*) {}
397
398 static void setupRECompositionCallExpectations(CompositionTest* test) {
Lloyd Pique86fa3db2019-02-04 18:46:01 -0800399 EXPECT_CALL(*test->mRenderEngine, useNativeFenceSync()).WillRepeatedly(Return(true));
400
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700401 // TODO: This seems like an unnecessary call if display is powered off.
402 EXPECT_CALL(*test->mDisplaySurface, getClientTargetAcquireFence())
403 .WillRepeatedly(ReturnRef(test->mClientTargetAcquireFence));
404 }
405
406 template <typename Case>
407 static void setupRELayerCompositionCallExpectations(CompositionTest*) {}
408};
409
410/* ------------------------------------------------------------------------
411 * Variants for each layer configuration which can be tested
412 */
413
414template <typename LayerProperties>
415struct BaseLayerProperties {
416 static constexpr uint32_t WIDTH = 100;
417 static constexpr uint32_t HEIGHT = 100;
418 static constexpr PixelFormat FORMAT = PIXEL_FORMAT_RGBA_8888;
419 static constexpr uint64_t USAGE =
420 GraphicBuffer::USAGE_SW_READ_NEVER | GraphicBuffer::USAGE_SW_WRITE_NEVER;
421 static constexpr android_dataspace DATASPACE = HAL_DATASPACE_UNKNOWN;
422 static constexpr uint32_t SCALING_MODE = 0;
423 static constexpr uint32_t TRANSFORM = 0;
424 static constexpr uint32_t LAYER_FLAGS = 0;
425 static constexpr float COLOR[] = {1.f, 1.f, 1.f, 1.f};
426 static constexpr IComposerClient::BlendMode BLENDMODE =
427 IComposerClient::BlendMode::PREMULTIPLIED;
428
429 static void enqueueBuffer(CompositionTest*, sp<BufferQueueLayer> layer) {
430 auto producer = layer->getProducer();
431
432 IGraphicBufferProducer::QueueBufferOutput qbo;
433 status_t result = producer->connect(nullptr, NATIVE_WINDOW_API_EGL, false, &qbo);
434 if (result != NO_ERROR) {
435 ALOGE("Failed to connect() (%d)", result);
436 return;
437 }
438
439 int slot;
440 sp<Fence> fence;
441 result = producer->dequeueBuffer(&slot, &fence, LayerProperties::WIDTH,
442 LayerProperties::HEIGHT, LayerProperties::FORMAT,
443 LayerProperties::USAGE, nullptr, nullptr);
444 if (result != IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION) {
445 ALOGE("Failed to dequeueBuffer() (%d)", result);
446 return;
447 }
448
449 sp<GraphicBuffer> buffer;
450 result = producer->requestBuffer(slot, &buffer);
451 if (result != NO_ERROR) {
452 ALOGE("Failed to requestBuffer() (%d)", result);
453 return;
454 }
455
456 IGraphicBufferProducer::QueueBufferInput qbi(systemTime(), false /* isAutoTimestamp */,
457 LayerProperties::DATASPACE,
458 Rect(LayerProperties::WIDTH,
459 LayerProperties::HEIGHT),
460 LayerProperties::SCALING_MODE,
461 LayerProperties::TRANSFORM, Fence::NO_FENCE);
462 result = producer->queueBuffer(slot, qbi, &qbo);
463 if (result != NO_ERROR) {
464 ALOGE("Failed to queueBuffer (%d)", result);
465 return;
466 }
467 }
468
469 static void setupLatchedBuffer(CompositionTest* test, sp<BufferQueueLayer> layer) {
470 // TODO: Eliminate the complexity of actually creating a buffer
471 EXPECT_CALL(*test->mRenderEngine, getMaxTextureSize()).WillOnce(Return(16384));
472 EXPECT_CALL(*test->mRenderEngine, getMaxViewportDims()).WillOnce(Return(16384));
473 status_t err =
474 layer->setDefaultBufferProperties(LayerProperties::WIDTH, LayerProperties::HEIGHT,
475 LayerProperties::FORMAT);
476 ASSERT_EQ(NO_ERROR, err);
477 Mock::VerifyAndClear(test->mRenderEngine);
478
479 EXPECT_CALL(*test->mMessageQueue, invalidate()).Times(1);
480 enqueueBuffer(test, layer);
481 Mock::VerifyAndClear(test->mMessageQueue);
482
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700483 EXPECT_CALL(*test->mRenderEngine, useNativeFenceSync()).WillRepeatedly(Return(true));
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700484 bool ignoredRecomputeVisibleRegions;
Alec Mouri86770e52018-09-24 22:40:58 +0000485 layer->latchBuffer(ignoredRecomputeVisibleRegions, 0, Fence::NO_FENCE);
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700486 Mock::VerifyAndClear(test->mRenderEngine);
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700487 }
488
489 static void setupLayerState(CompositionTest* test, sp<BufferQueueLayer> layer) {
490 setupLatchedBuffer(test, layer);
491 }
492
493 static void setupBufferLayerPostFrameCallExpectations(CompositionTest* test) {
494 // BufferLayer::onPostComposition(), when there is no present fence
495 EXPECT_CALL(*test->mComposer, getActiveConfig(HWC_DISPLAY, _))
496 .WillOnce(DoAll(SetArgPointee<1>(DEFAULT_CONFIG_ID), Return(Error::NONE)));
497 }
498
499 static void setupHwcSetGeometryCallExpectations(CompositionTest* test) {
500 // TODO: Coverage of other values
501 EXPECT_CALL(*test->mComposer,
502 setLayerBlendMode(HWC_DISPLAY, HWC_LAYER, LayerProperties::BLENDMODE))
503 .Times(1);
504 // TODO: Coverage of other values for origin
505 EXPECT_CALL(*test->mComposer,
506 setLayerDisplayFrame(HWC_DISPLAY, HWC_LAYER,
507 IComposerClient::Rect({0, 0, LayerProperties::WIDTH,
508 LayerProperties::HEIGHT})))
509 .Times(1);
510 EXPECT_CALL(*test->mComposer,
511 setLayerPlaneAlpha(HWC_DISPLAY, HWC_LAYER, LayerProperties::COLOR[3]))
512 .Times(1);
513 // TODO: Coverage of other values
514 EXPECT_CALL(*test->mComposer, setLayerZOrder(HWC_DISPLAY, HWC_LAYER, 0u)).Times(1);
515 // TODO: Coverage of other values
516 EXPECT_CALL(*test->mComposer, setLayerInfo(HWC_DISPLAY, HWC_LAYER, 0u, 0u)).Times(1);
517
518 // These expectations retire on saturation as the code path these
519 // expectations are for appears to make an extra call to them.
520 // TODO: Investigate this extra call
521 EXPECT_CALL(*test->mComposer, setLayerTransform(HWC_DISPLAY, HWC_LAYER, DEFAULT_TRANSFORM))
David Sodman15094112018-10-11 09:39:37 -0700522 .Times(AtLeast(1))
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700523 .RetiresOnSaturation();
524 }
525
526 static void setupHwcSetSourceCropBufferCallExpectations(CompositionTest* test) {
527 EXPECT_CALL(*test->mComposer,
528 setLayerSourceCrop(HWC_DISPLAY, HWC_LAYER,
529 IComposerClient::FRect({0.f, 0.f, LayerProperties::WIDTH,
530 LayerProperties::HEIGHT})))
531 .Times(1);
532 }
533
534 static void setupHwcSetSourceCropColorCallExpectations(CompositionTest* test) {
535 EXPECT_CALL(*test->mComposer,
536 setLayerSourceCrop(HWC_DISPLAY, HWC_LAYER,
537 IComposerClient::FRect({0.f, 0.f, 0.f, 0.f})))
538 .Times(1);
539 }
540
541 static void setupHwcSetPerFrameCallExpectations(CompositionTest* test) {
542 EXPECT_CALL(*test->mComposer,
543 setLayerVisibleRegion(HWC_DISPLAY, HWC_LAYER,
544 std::vector<IComposerClient::Rect>({IComposerClient::Rect(
545 {0, 0, LayerProperties::WIDTH,
546 LayerProperties::HEIGHT})})))
547 .Times(1);
548 }
549
550 static void setupHwcSetPerFrameColorCallExpectations(CompositionTest* test) {
551 EXPECT_CALL(*test->mComposer, setLayerSurfaceDamage(HWC_DISPLAY, HWC_LAYER, _)).Times(1);
552
553 // TODO: use COLOR
554 EXPECT_CALL(*test->mComposer,
555 setLayerColor(HWC_DISPLAY, HWC_LAYER,
556 IComposerClient::Color({0xff, 0xff, 0xff, 0xff})))
557 .Times(1);
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700558 }
559
560 static void setupHwcSetPerFrameBufferCallExpectations(CompositionTest* test) {
561 EXPECT_CALL(*test->mComposer, setLayerSurfaceDamage(HWC_DISPLAY, HWC_LAYER, _)).Times(1);
562 EXPECT_CALL(*test->mComposer, setLayerBuffer(HWC_DISPLAY, HWC_LAYER, _, _, _)).Times(1);
563
564 setupBufferLayerPostFrameCallExpectations(test);
565 }
566
567 static void setupREBufferCompositionCommonCallExpectations(CompositionTest* test) {
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000568 EXPECT_CALL(*test->mRenderEngine, drawLayers)
569 .WillOnce([](const renderengine::DisplaySettings& displaySettings,
570 const std::vector<renderengine::LayerSettings>& layerSettings,
571 ANativeWindowBuffer*, base::unique_fd*) -> status_t {
572 EXPECT_EQ(DEFAULT_DISPLAY_MAX_LUMINANCE, displaySettings.maxLuminance);
573 EXPECT_EQ(Rect(DEFAULT_DISPLAY_WIDTH, DEFAULT_DISPLAY_HEIGHT),
574 displaySettings.physicalDisplay);
575 EXPECT_EQ(Rect(DEFAULT_DISPLAY_WIDTH, DEFAULT_DISPLAY_HEIGHT),
576 displaySettings.clip);
577 // screen capture adds an additional color layer as an alpha
578 // prefill, so gtet the back layer.
579 renderengine::LayerSettings layer = layerSettings.back();
580 EXPECT_THAT(layer.source.buffer.buffer, Not(IsNull()));
581 EXPECT_THAT(layer.source.buffer.fence, Not(IsNull()));
582 EXPECT_EQ(renderengine::Buffer::CachingHint::NO_CACHE,
583 layer.source.buffer.cacheHint);
584 EXPECT_EQ(DEFAULT_TEXTURE_ID, layer.source.buffer.textureName);
585 EXPECT_EQ(false, layer.source.buffer.isY410BT2020);
586 EXPECT_EQ(true, layer.source.buffer.usePremultipliedAlpha);
587 EXPECT_EQ(false, layer.source.buffer.isOpaque);
588 EXPECT_EQ(0.0, layer.geometry.roundedCornersRadius);
589 EXPECT_EQ(ui::Dataspace::UNKNOWN, layer.sourceDataspace);
590 EXPECT_EQ(LayerProperties::COLOR[3], layer.alpha);
591 return NO_ERROR;
592 });
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700593 }
594
595 static void setupREBufferCompositionCallExpectations(CompositionTest* test) {
596 LayerProperties::setupREBufferCompositionCommonCallExpectations(test);
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700597 }
598
599 static void setupInsecureREBufferCompositionCallExpectations(CompositionTest* test) {
600 setupREBufferCompositionCallExpectations(test);
601 }
602
603 static void setupREBufferScreenshotCompositionCallExpectations(CompositionTest* test) {
604 LayerProperties::setupREBufferCompositionCommonCallExpectations(test);
605 }
606
607 static void setupInsecureREBufferScreenshotCompositionCallExpectations(CompositionTest* test) {
608 LayerProperties::setupREBufferCompositionCommonCallExpectations(test);
609 }
610
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700611 static void setupREColorCompositionCallExpectations(CompositionTest* test) {
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000612 EXPECT_CALL(*test->mRenderEngine, drawLayers)
613 .WillOnce([](const renderengine::DisplaySettings& displaySettings,
614 const std::vector<renderengine::LayerSettings>& layerSettings,
615 ANativeWindowBuffer*, base::unique_fd*) -> status_t {
616 EXPECT_EQ(DEFAULT_DISPLAY_MAX_LUMINANCE, displaySettings.maxLuminance);
617 EXPECT_EQ(Rect(DEFAULT_DISPLAY_WIDTH, DEFAULT_DISPLAY_HEIGHT),
618 displaySettings.physicalDisplay);
619 EXPECT_EQ(Rect(DEFAULT_DISPLAY_WIDTH, DEFAULT_DISPLAY_HEIGHT),
620 displaySettings.clip);
621 // screen capture adds an additional color layer as an alpha
622 // prefill, so get the back layer.
623 renderengine::LayerSettings layer = layerSettings.back();
624 EXPECT_THAT(layer.source.buffer.buffer, IsNull());
625 EXPECT_EQ(half3(LayerProperties::COLOR[0], LayerProperties::COLOR[1],
626 LayerProperties::COLOR[2]),
627 layer.source.solidColor);
628 EXPECT_EQ(0.0, layer.geometry.roundedCornersRadius);
629 EXPECT_EQ(ui::Dataspace::UNKNOWN, layer.sourceDataspace);
630 EXPECT_EQ(LayerProperties::COLOR[3], layer.alpha);
631 return NO_ERROR;
632 });
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700633 }
634
635 static void setupREColorScreenshotCompositionCallExpectations(CompositionTest* test) {
636 setupREColorCompositionCallExpectations(test);
637 }
638};
639
640struct DefaultLayerProperties : public BaseLayerProperties<DefaultLayerProperties> {};
641
642struct ColorLayerProperties : public BaseLayerProperties<ColorLayerProperties> {};
643
644struct SidebandLayerProperties : public BaseLayerProperties<SidebandLayerProperties> {
645 using Base = BaseLayerProperties<SidebandLayerProperties>;
646 static constexpr IComposerClient::BlendMode BLENDMODE = IComposerClient::BlendMode::NONE;
647
648 static void setupLayerState(CompositionTest* test, sp<BufferQueueLayer> layer) {
649 sp<NativeHandle> stream =
650 NativeHandle::create(reinterpret_cast<native_handle_t*>(DEFAULT_SIDEBAND_STREAM),
651 false);
652 test->mFlinger.setLayerSidebandStream(layer, stream);
653 }
654
655 static void setupHwcSetSourceCropBufferCallExpectations(CompositionTest* test) {
656 EXPECT_CALL(*test->mComposer,
657 setLayerSourceCrop(HWC_DISPLAY, HWC_LAYER,
658 IComposerClient::FRect({0.f, 0.f, -1.f, -1.f})))
659 .Times(1);
660 }
661
662 static void setupHwcSetPerFrameBufferCallExpectations(CompositionTest* test) {
663 EXPECT_CALL(*test->mComposer,
664 setLayerSidebandStream(HWC_DISPLAY, HWC_LAYER,
665 reinterpret_cast<native_handle_t*>(
666 DEFAULT_SIDEBAND_STREAM)))
667 .WillOnce(Return(Error::NONE));
668
669 EXPECT_CALL(*test->mComposer, setLayerSurfaceDamage(HWC_DISPLAY, HWC_LAYER, _)).Times(1);
670 }
671
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000672 static void setupREBufferCompositionCommonCallExpectations(CompositionTest* /*test*/) {}
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700673};
674
675struct SecureLayerProperties : public BaseLayerProperties<SecureLayerProperties> {
676 using Base = BaseLayerProperties<SecureLayerProperties>;
677
678 static constexpr uint32_t LAYER_FLAGS = ISurfaceComposerClient::eSecure;
679
680 static void setupInsecureREBufferCompositionCommonCallExpectations(CompositionTest* test) {
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000681 EXPECT_CALL(*test->mRenderEngine, drawLayers)
682 .WillOnce([](const renderengine::DisplaySettings& displaySettings,
683 const std::vector<renderengine::LayerSettings>& layerSettings,
684 ANativeWindowBuffer*, base::unique_fd*) -> status_t {
685 EXPECT_EQ(DEFAULT_DISPLAY_MAX_LUMINANCE, displaySettings.maxLuminance);
686 EXPECT_EQ(Rect(DEFAULT_DISPLAY_WIDTH, DEFAULT_DISPLAY_HEIGHT),
687 displaySettings.physicalDisplay);
688 EXPECT_EQ(Rect(DEFAULT_DISPLAY_WIDTH, DEFAULT_DISPLAY_HEIGHT),
689 displaySettings.clip);
690 // screen capture adds an additional color layer as an alpha
691 // prefill, so get the back layer.
692 renderengine::LayerSettings layer = layerSettings.back();
693 EXPECT_THAT(layer.source.buffer.buffer, IsNull());
694 EXPECT_EQ(half3(0.0f, 0.0f, 0.0f), layer.source.solidColor);
695 EXPECT_EQ(0.0, layer.geometry.roundedCornersRadius);
696 EXPECT_EQ(ui::Dataspace::UNKNOWN, layer.sourceDataspace);
697 EXPECT_EQ(1.0f, layer.alpha);
698 return NO_ERROR;
699 });
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700700 }
701
702 static void setupInsecureREBufferCompositionCallExpectations(CompositionTest* test) {
703 setupInsecureREBufferCompositionCommonCallExpectations(test);
704 Base::setupBufferLayerPostFrameCallExpectations(test);
705 }
706
707 static void setupInsecureREBufferScreenshotCompositionCallExpectations(CompositionTest* test) {
708 setupInsecureREBufferCompositionCommonCallExpectations(test);
709 }
710};
711
712struct CursorLayerProperties : public BaseLayerProperties<CursorLayerProperties> {
713 using Base = BaseLayerProperties<CursorLayerProperties>;
714
715 static void setupLayerState(CompositionTest* test, sp<BufferQueueLayer> layer) {
716 Base::setupLayerState(test, layer);
717 test->mFlinger.setLayerPotentialCursor(layer, true);
718 }
719};
720
721struct NoLayerVariant {
722 using FlingerLayerType = sp<BufferQueueLayer>;
723
724 static FlingerLayerType createLayer(CompositionTest*) { return FlingerLayerType(); }
725 static void injectLayer(CompositionTest*, FlingerLayerType) {}
726 static void cleanupInjectedLayers(CompositionTest*) {}
727
728 static void setupCallExpectationsForDirtyGeometry(CompositionTest*) {}
729 static void setupCallExpectationsForDirtyFrame(CompositionTest*) {}
730};
731
732template <typename LayerProperties>
733struct BaseLayerVariant {
734 template <typename L, typename F>
735 static sp<L> createLayerWithFactory(CompositionTest* test, F factory) {
736 EXPECT_CALL(*test->mMessageQueue, postMessage(_, 0)).Times(0);
737
738 sp<L> layer = factory();
739
740 Mock::VerifyAndClear(test->mComposer);
741 Mock::VerifyAndClear(test->mRenderEngine);
742 Mock::VerifyAndClear(test->mMessageQueue);
743
744 auto& layerDrawingState = test->mFlinger.mutableLayerDrawingState(layer);
745 layerDrawingState.layerStack = DEFAULT_LAYER_STACK;
746 layerDrawingState.active.w = 100;
747 layerDrawingState.active.h = 100;
748 layerDrawingState.color = half4(LayerProperties::COLOR[0], LayerProperties::COLOR[1],
749 LayerProperties::COLOR[2], LayerProperties::COLOR[3]);
Vishnu Nair4351ad52019-02-11 14:13:02 -0800750 layer->computeBounds(FloatRect(0, 0, 100, 100), ui::Transform());
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700751 layer->setVisibleRegion(Region(Rect(0, 0, 100, 100)));
752
753 return layer;
754 }
755
756 static void injectLayer(CompositionTest* test, sp<Layer> layer) {
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800757 std::vector<std::unique_ptr<compositionengine::OutputLayer>> outputLayers;
758 outputLayers.emplace_back(
759 test->mDisplay->getCompositionDisplay()
760 ->getOrCreateOutputLayer(layer->getCompositionLayer(), layer));
761 test->mDisplay->getCompositionDisplay()->setOutputLayersOrderedByZ(std::move(outputLayers));
762
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700763 EXPECT_CALL(*test->mComposer, createLayer(HWC_DISPLAY, _))
764 .WillOnce(DoAll(SetArgPointee<1>(HWC_LAYER), Return(Error::NONE)));
765
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800766 layer->createHwcLayer(&test->mFlinger.getHwComposer(), test->mDisplay);
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700767
768 Mock::VerifyAndClear(test->mComposer);
769
770 Vector<sp<Layer>> layers;
771 layers.add(layer);
772 test->mDisplay->setVisibleLayersSortedByZ(layers);
773 test->mFlinger.mutableDrawingState().layersSortedByZ.add(layer);
774 }
775
776 static void cleanupInjectedLayers(CompositionTest* test) {
777 EXPECT_CALL(*test->mComposer, destroyLayer(HWC_DISPLAY, HWC_LAYER))
778 .WillOnce(Return(Error::NONE));
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800779
780 test->mDisplay->getCompositionDisplay()->setOutputLayersOrderedByZ(
781 std::vector<std::unique_ptr<compositionengine::OutputLayer>>());
782
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700783 for (auto layer : test->mFlinger.mutableDrawingState().layersSortedByZ) {
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800784 layer->destroyHwcLayer(test->mDisplay);
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700785 }
786 test->mFlinger.mutableDrawingState().layersSortedByZ.clear();
787 }
788};
789
790template <typename LayerProperties>
791struct ColorLayerVariant : public BaseLayerVariant<LayerProperties> {
792 using Base = BaseLayerVariant<LayerProperties>;
793 using FlingerLayerType = sp<ColorLayer>;
794
795 static FlingerLayerType createLayer(CompositionTest* test) {
796 FlingerLayerType layer = Base::template createLayerWithFactory<ColorLayer>(test, [test]() {
Lloyd Pique42ab75e2018-09-12 20:46:03 -0700797 return new ColorLayer(LayerCreationArgs(test->mFlinger.mFlinger.get(), sp<Client>(),
798 String8("test-layer"), LayerProperties::WIDTH,
799 LayerProperties::HEIGHT,
800 LayerProperties::LAYER_FLAGS));
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700801 });
Vishnu Nair60356342018-11-13 13:00:45 -0800802
803 auto& layerDrawingState = test->mFlinger.mutableLayerDrawingState(layer);
804 layerDrawingState.crop_legacy = Rect(0, 0, LayerProperties::HEIGHT, LayerProperties::WIDTH);
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700805 return layer;
806 }
807
808 static void setupRECompositionCallExpectations(CompositionTest* test) {
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700809 LayerProperties::setupREColorCompositionCallExpectations(test);
810 }
811
812 static void setupREScreenshotCompositionCallExpectations(CompositionTest* test) {
813 LayerProperties::setupREColorScreenshotCompositionCallExpectations(test);
814 }
815
816 static void setupCallExpectationsForDirtyGeometry(CompositionTest* test) {
817 LayerProperties::setupHwcSetGeometryCallExpectations(test);
818 LayerProperties::setupHwcSetSourceCropColorCallExpectations(test);
819 }
820
821 static void setupCallExpectationsForDirtyFrame(CompositionTest* test) {
822 LayerProperties::setupHwcSetPerFrameCallExpectations(test);
823 LayerProperties::setupHwcSetPerFrameColorCallExpectations(test);
824 }
825};
826
827template <typename LayerProperties>
828struct BufferLayerVariant : public BaseLayerVariant<LayerProperties> {
829 using Base = BaseLayerVariant<LayerProperties>;
830 using FlingerLayerType = sp<BufferQueueLayer>;
831
832 static FlingerLayerType createLayer(CompositionTest* test) {
833 test->mFlinger.mutableTexturePool().push_back(DEFAULT_TEXTURE_ID);
834
835 FlingerLayerType layer =
836 Base::template createLayerWithFactory<BufferQueueLayer>(test, [test]() {
Lloyd Pique42ab75e2018-09-12 20:46:03 -0700837 return new BufferQueueLayer(
838 LayerCreationArgs(test->mFlinger.mFlinger.get(), sp<Client>(),
839 String8("test-layer"), LayerProperties::WIDTH,
840 LayerProperties::HEIGHT,
841 LayerProperties::LAYER_FLAGS));
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700842 });
843
844 LayerProperties::setupLayerState(test, layer);
845
846 return layer;
847 }
848
849 static void cleanupInjectedLayers(CompositionTest* test) {
850 EXPECT_CALL(*test->mMessageQueue, postMessage(_, 0)).Times(2);
851 Base::cleanupInjectedLayers(test);
852 }
853
854 static void setupCallExpectationsForDirtyGeometry(CompositionTest* test) {
855 LayerProperties::setupHwcSetGeometryCallExpectations(test);
856 LayerProperties::setupHwcSetSourceCropBufferCallExpectations(test);
857 }
858
859 static void setupCallExpectationsForDirtyFrame(CompositionTest* test) {
860 LayerProperties::setupHwcSetPerFrameCallExpectations(test);
861 LayerProperties::setupHwcSetPerFrameBufferCallExpectations(test);
862 }
863
864 static void setupRECompositionCallExpectations(CompositionTest* test) {
865 LayerProperties::setupREBufferCompositionCallExpectations(test);
866 }
867
868 static void setupInsecureRECompositionCallExpectations(CompositionTest* test) {
869 LayerProperties::setupInsecureREBufferCompositionCallExpectations(test);
870 }
871
872 static void setupREScreenshotCompositionCallExpectations(CompositionTest* test) {
873 LayerProperties::setupREBufferScreenshotCompositionCallExpectations(test);
874 }
875
876 static void setupInsecureREScreenshotCompositionCallExpectations(CompositionTest* test) {
877 LayerProperties::setupInsecureREBufferScreenshotCompositionCallExpectations(test);
878 }
879};
880
881/* ------------------------------------------------------------------------
882 * Variants to control how the composition type is changed
883 */
884
885struct NoCompositionTypeVariant {
886 static void setupHwcSetCallExpectations(CompositionTest*) {}
887
888 static void setupHwcGetCallExpectations(CompositionTest* test) {
889 EXPECT_CALL(*test->mComposer, getChangedCompositionTypes(HWC_DISPLAY, _, _)).Times(1);
890 }
891};
892
893template <IComposerClient::Composition CompositionType>
894struct KeepCompositionTypeVariant {
895 static constexpr HWC2::Composition TYPE = static_cast<HWC2::Composition>(CompositionType);
896
897 static void setupHwcSetCallExpectations(CompositionTest* test) {
898 EXPECT_CALL(*test->mComposer,
899 setLayerCompositionType(HWC_DISPLAY, HWC_LAYER, CompositionType))
900 .Times(1);
901 }
902
903 static void setupHwcGetCallExpectations(CompositionTest* test) {
904 EXPECT_CALL(*test->mComposer, getChangedCompositionTypes(HWC_DISPLAY, _, _)).Times(1);
905 }
906};
907
908template <IComposerClient::Composition InitialCompositionType,
909 IComposerClient::Composition FinalCompositionType>
910struct ChangeCompositionTypeVariant {
911 static constexpr HWC2::Composition TYPE = static_cast<HWC2::Composition>(FinalCompositionType);
912
913 static void setupHwcSetCallExpectations(CompositionTest* test) {
914 EXPECT_CALL(*test->mComposer,
915 setLayerCompositionType(HWC_DISPLAY, HWC_LAYER, InitialCompositionType))
916 .Times(1);
917 }
918
919 static void setupHwcGetCallExpectations(CompositionTest* test) {
920 EXPECT_CALL(*test->mComposer, getChangedCompositionTypes(HWC_DISPLAY, _, _))
921 .WillOnce(DoAll(SetArgPointee<1>(std::vector<Hwc2::Layer>{
922 static_cast<Hwc2::Layer>(HWC_LAYER)}),
923 SetArgPointee<2>(std::vector<IComposerClient::Composition>{
924 FinalCompositionType}),
925 Return(Error::NONE)));
926 }
927};
928
929/* ------------------------------------------------------------------------
930 * Variants to select how the composition is expected to be handled
931 */
932
933struct CompositionResultBaseVariant {
934 static void setupLayerState(CompositionTest*, sp<Layer>) {}
935
936 template <typename Case>
937 static void setupCallExpectationsForDirtyGeometry(CompositionTest* test) {
938 Case::Layer::setupCallExpectationsForDirtyGeometry(test);
939 }
940
941 template <typename Case>
942 static void setupCallExpectationsForDirtyFrame(CompositionTest* test) {
943 Case::Layer::setupCallExpectationsForDirtyFrame(test);
944 }
945};
946
947struct NoCompositionResultVariant : public CompositionResultBaseVariant {
948 template <typename Case>
949 static void setupCallExpectations(CompositionTest* test) {
950 Case::Display::setupEmptyFrameCompositionCallExpectations(test);
951 Case::Display::setupHwcCompositionCallExpectations(test);
952 }
953};
954
955struct HwcCompositionResultVariant : public CompositionResultBaseVariant {
956 template <typename Case>
957 static void setupCallExpectations(CompositionTest* test) {
958 Case::Display::setupNonEmptyFrameCompositionCallExpectations(test);
959 Case::Display::setupHwcCompositionCallExpectations(test);
960 }
961};
962
963struct RECompositionResultVariant : public CompositionResultBaseVariant {
964 template <typename Case>
965 static void setupCallExpectations(CompositionTest* test) {
966 Case::Display::setupNonEmptyFrameCompositionCallExpectations(test);
967 Case::Display::setupRECompositionCallExpectations(test);
968 Case::Display::template setupRELayerCompositionCallExpectations<Case>(test);
969 }
970};
971
972struct ForcedClientCompositionResultVariant : public RECompositionResultVariant {
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800973 static void setupLayerState(CompositionTest* test, sp<Layer> layer) {
974 layer->forceClientComposition(test->mDisplay);
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700975 }
976
977 template <typename Case>
978 static void setupCallExpectationsForDirtyGeometry(CompositionTest*) {}
979
980 template <typename Case>
981 static void setupCallExpectationsForDirtyFrame(CompositionTest*) {}
982};
983
984struct EmptyScreenshotResultVariant {
985 static void setupLayerState(CompositionTest*, sp<Layer>) {}
986
987 template <typename Case>
988 static void setupCallExpectations(CompositionTest*) {}
989};
990
991struct REScreenshotResultVariant : public EmptyScreenshotResultVariant {
992 using Base = EmptyScreenshotResultVariant;
993
994 template <typename Case>
995 static void setupCallExpectations(CompositionTest* test) {
996 Base::template setupCallExpectations<Case>(test);
997 Case::Display::template setupRELayerScreenshotCompositionCallExpectations<Case>(test);
998 }
999};
1000
1001/* ------------------------------------------------------------------------
1002 * Composition test case, containing all the variants being tested
1003 */
1004
1005template <typename DisplayCase, typename LayerCase, typename CompositionTypeCase,
1006 typename CompositionResultCase>
1007struct CompositionCase {
1008 using ThisCase =
1009 CompositionCase<DisplayCase, LayerCase, CompositionTypeCase, CompositionResultCase>;
1010 using Display = DisplayCase;
1011 using Layer = LayerCase;
1012 using CompositionType = CompositionTypeCase;
1013 using CompositionResult = CompositionResultCase;
1014
1015 static void setupCommon(CompositionTest* test) {
1016 Display::setupPreconditions(test);
1017
1018 auto layer = Layer::createLayer(test);
1019 Layer::injectLayer(test, layer);
1020 CompositionResult::setupLayerState(test, layer);
1021 }
1022
1023 static void setupForDirtyGeometry(CompositionTest* test) {
1024 setupCommon(test);
1025
1026 Display::template setupCommonCompositionCallExpectations<ThisCase>(test);
1027 CompositionResult::template setupCallExpectationsForDirtyGeometry<ThisCase>(test);
1028 CompositionResult::template setupCallExpectationsForDirtyFrame<ThisCase>(test);
1029 CompositionResult::template setupCallExpectations<ThisCase>(test);
1030 }
1031
1032 static void setupForDirtyFrame(CompositionTest* test) {
1033 setupCommon(test);
1034
1035 Display::template setupCommonCompositionCallExpectations<ThisCase>(test);
1036 CompositionResult::template setupCallExpectationsForDirtyFrame<ThisCase>(test);
1037 CompositionResult::template setupCallExpectations<ThisCase>(test);
1038 }
1039
1040 static void setupForScreenCapture(CompositionTest* test) {
1041 setupCommon(test);
1042
1043 Display::template setupCommonScreensCaptureCallExpectations<ThisCase>(test);
1044 CompositionResult::template setupCallExpectations<ThisCase>(test);
1045 }
1046
1047 static void cleanup(CompositionTest* test) {
1048 Layer::cleanupInjectedLayers(test);
1049
1050 for (auto& hwcDisplay : test->mFlinger.mFakeHwcDisplays) {
1051 hwcDisplay->mutableLayers().clear();
1052 }
1053
1054 test->mDisplay->setVisibleLayersSortedByZ(Vector<sp<android::Layer>>());
1055 }
1056};
1057
1058/* ------------------------------------------------------------------------
1059 * Composition cases to test
1060 */
1061
1062TEST_F(CompositionTest, noLayersDoesMinimalWorkWithDirtyGeometry) {
1063 displayRefreshCompositionDirtyGeometry<
1064 CompositionCase<DefaultDisplaySetupVariant, NoLayerVariant, NoCompositionTypeVariant,
1065 NoCompositionResultVariant>>();
1066}
1067
1068TEST_F(CompositionTest, noLayersDoesMinimalWorkWithDirtyFrame) {
1069 displayRefreshCompositionDirtyFrame<
1070 CompositionCase<DefaultDisplaySetupVariant, NoLayerVariant, NoCompositionTypeVariant,
1071 NoCompositionResultVariant>>();
1072}
1073
1074TEST_F(CompositionTest, noLayersDoesMinimalWorkToCaptureScreen) {
1075 captureScreenComposition<
1076 CompositionCase<DefaultDisplaySetupVariant, NoLayerVariant, NoCompositionTypeVariant,
1077 EmptyScreenshotResultVariant>>();
1078}
1079
1080/* ------------------------------------------------------------------------
1081 * Simple buffer layers
1082 */
1083
1084TEST_F(CompositionTest, HWCComposedNormalBufferLayerWithDirtyGeometry) {
1085 displayRefreshCompositionDirtyGeometry<
1086 CompositionCase<DefaultDisplaySetupVariant, BufferLayerVariant<DefaultLayerProperties>,
1087 KeepCompositionTypeVariant<IComposerClient::Composition::DEVICE>,
1088 HwcCompositionResultVariant>>();
1089}
1090
1091TEST_F(CompositionTest, HWCComposedNormalBufferLayerWithDirtyFrame) {
1092 displayRefreshCompositionDirtyFrame<
1093 CompositionCase<DefaultDisplaySetupVariant, BufferLayerVariant<DefaultLayerProperties>,
1094 KeepCompositionTypeVariant<IComposerClient::Composition::DEVICE>,
1095 HwcCompositionResultVariant>>();
1096}
1097
1098TEST_F(CompositionTest, REComposedNormalBufferLayer) {
1099 displayRefreshCompositionDirtyFrame<
1100 CompositionCase<DefaultDisplaySetupVariant, BufferLayerVariant<DefaultLayerProperties>,
1101 ChangeCompositionTypeVariant<IComposerClient::Composition::DEVICE,
1102 IComposerClient::Composition::CLIENT>,
1103 RECompositionResultVariant>>();
1104}
1105
1106TEST_F(CompositionTest, captureScreenNormalBufferLayer) {
1107 captureScreenComposition<
1108 CompositionCase<DefaultDisplaySetupVariant, BufferLayerVariant<DefaultLayerProperties>,
1109 NoCompositionTypeVariant, REScreenshotResultVariant>>();
1110}
1111
1112/* ------------------------------------------------------------------------
1113 * Single-color layers
1114 */
1115
1116TEST_F(CompositionTest, HWCComposedColorLayerWithDirtyGeometry) {
1117 displayRefreshCompositionDirtyGeometry<
1118 CompositionCase<DefaultDisplaySetupVariant, ColorLayerVariant<ColorLayerProperties>,
1119 KeepCompositionTypeVariant<IComposerClient::Composition::SOLID_COLOR>,
1120 HwcCompositionResultVariant>>();
1121}
1122
1123TEST_F(CompositionTest, HWCComposedColorLayerWithDirtyFrame) {
1124 displayRefreshCompositionDirtyFrame<
1125 CompositionCase<DefaultDisplaySetupVariant, ColorLayerVariant<ColorLayerProperties>,
1126 KeepCompositionTypeVariant<IComposerClient::Composition::SOLID_COLOR>,
1127 HwcCompositionResultVariant>>();
1128}
1129
1130TEST_F(CompositionTest, REComposedColorLayer) {
1131 displayRefreshCompositionDirtyFrame<
1132 CompositionCase<DefaultDisplaySetupVariant, ColorLayerVariant<ColorLayerProperties>,
1133 ChangeCompositionTypeVariant<IComposerClient::Composition::SOLID_COLOR,
1134 IComposerClient::Composition::CLIENT>,
1135 RECompositionResultVariant>>();
1136}
1137
1138TEST_F(CompositionTest, captureScreenColorLayer) {
1139 captureScreenComposition<
1140 CompositionCase<DefaultDisplaySetupVariant, ColorLayerVariant<ColorLayerProperties>,
1141 NoCompositionTypeVariant, REScreenshotResultVariant>>();
1142}
1143
1144/* ------------------------------------------------------------------------
1145 * Layers with sideband buffers
1146 */
1147
1148TEST_F(CompositionTest, HWCComposedSidebandBufferLayerWithDirtyGeometry) {
1149 displayRefreshCompositionDirtyGeometry<
1150 CompositionCase<DefaultDisplaySetupVariant, BufferLayerVariant<SidebandLayerProperties>,
1151 KeepCompositionTypeVariant<IComposerClient::Composition::SIDEBAND>,
1152 HwcCompositionResultVariant>>();
1153}
1154
1155TEST_F(CompositionTest, HWCComposedSidebandBufferLayerWithDirtyFrame) {
1156 displayRefreshCompositionDirtyFrame<
1157 CompositionCase<DefaultDisplaySetupVariant, BufferLayerVariant<SidebandLayerProperties>,
1158 KeepCompositionTypeVariant<IComposerClient::Composition::SIDEBAND>,
1159 HwcCompositionResultVariant>>();
1160}
1161
1162TEST_F(CompositionTest, REComposedSidebandBufferLayer) {
1163 displayRefreshCompositionDirtyFrame<
1164 CompositionCase<DefaultDisplaySetupVariant, BufferLayerVariant<SidebandLayerProperties>,
1165 ChangeCompositionTypeVariant<IComposerClient::Composition::SIDEBAND,
1166 IComposerClient::Composition::CLIENT>,
1167 RECompositionResultVariant>>();
1168}
1169
1170TEST_F(CompositionTest, captureScreenSidebandBufferLayer) {
1171 captureScreenComposition<
1172 CompositionCase<DefaultDisplaySetupVariant, BufferLayerVariant<SidebandLayerProperties>,
1173 NoCompositionTypeVariant, REScreenshotResultVariant>>();
1174}
1175
1176/* ------------------------------------------------------------------------
1177 * Layers with ISurfaceComposerClient::eSecure, on a secure display
1178 */
1179
1180TEST_F(CompositionTest, HWCComposedSecureBufferLayerWithDirtyGeometry) {
1181 displayRefreshCompositionDirtyGeometry<
1182 CompositionCase<DefaultDisplaySetupVariant, BufferLayerVariant<SecureLayerProperties>,
1183 KeepCompositionTypeVariant<IComposerClient::Composition::DEVICE>,
1184 HwcCompositionResultVariant>>();
1185}
1186
1187TEST_F(CompositionTest, HWCComposedSecureBufferLayerWithDirtyFrame) {
1188 displayRefreshCompositionDirtyFrame<
1189 CompositionCase<DefaultDisplaySetupVariant, BufferLayerVariant<SecureLayerProperties>,
1190 KeepCompositionTypeVariant<IComposerClient::Composition::DEVICE>,
1191 HwcCompositionResultVariant>>();
1192}
1193
1194TEST_F(CompositionTest, REComposedSecureBufferLayer) {
1195 displayRefreshCompositionDirtyFrame<
1196 CompositionCase<DefaultDisplaySetupVariant, BufferLayerVariant<SecureLayerProperties>,
1197 ChangeCompositionTypeVariant<IComposerClient::Composition::DEVICE,
1198 IComposerClient::Composition::CLIENT>,
1199 RECompositionResultVariant>>();
1200}
1201
1202TEST_F(CompositionTest, captureScreenSecureBufferLayerOnSecureDisplay) {
1203 captureScreenComposition<
1204 CompositionCase<DefaultDisplaySetupVariant, BufferLayerVariant<SecureLayerProperties>,
1205 NoCompositionTypeVariant, REScreenshotResultVariant>>();
1206}
1207
1208/* ------------------------------------------------------------------------
1209 * Layers with ISurfaceComposerClient::eSecure, on a non-secure display
1210 */
1211
1212TEST_F(CompositionTest, HWCComposedSecureBufferLayerOnInsecureDisplayWithDirtyGeometry) {
1213 displayRefreshCompositionDirtyGeometry<
1214 CompositionCase<InsecureDisplaySetupVariant, BufferLayerVariant<SecureLayerProperties>,
1215 KeepCompositionTypeVariant<IComposerClient::Composition::CLIENT>,
1216 ForcedClientCompositionResultVariant>>();
1217}
1218
1219TEST_F(CompositionTest, HWCComposedSecureBufferLayerOnInsecureDisplayWithDirtyFrame) {
1220 displayRefreshCompositionDirtyFrame<
1221 CompositionCase<InsecureDisplaySetupVariant, BufferLayerVariant<SecureLayerProperties>,
1222 KeepCompositionTypeVariant<IComposerClient::Composition::CLIENT>,
1223 ForcedClientCompositionResultVariant>>();
1224}
1225
1226TEST_F(CompositionTest, captureScreenSecureBufferLayerOnInsecureDisplay) {
1227 captureScreenComposition<
1228 CompositionCase<InsecureDisplaySetupVariant, BufferLayerVariant<SecureLayerProperties>,
1229 NoCompositionTypeVariant, REScreenshotResultVariant>>();
1230}
1231
1232/* ------------------------------------------------------------------------
1233 * Cursor layers
1234 */
1235
1236TEST_F(CompositionTest, HWCComposedCursorLayerWithDirtyGeometry) {
1237 displayRefreshCompositionDirtyGeometry<
1238 CompositionCase<DefaultDisplaySetupVariant, BufferLayerVariant<CursorLayerProperties>,
1239 KeepCompositionTypeVariant<IComposerClient::Composition::CURSOR>,
1240 HwcCompositionResultVariant>>();
1241}
1242
1243TEST_F(CompositionTest, HWCComposedCursorLayerWithDirtyFrame) {
1244 displayRefreshCompositionDirtyFrame<
1245 CompositionCase<DefaultDisplaySetupVariant, BufferLayerVariant<CursorLayerProperties>,
1246 KeepCompositionTypeVariant<IComposerClient::Composition::CURSOR>,
1247 HwcCompositionResultVariant>>();
1248}
1249
1250TEST_F(CompositionTest, REComposedCursorLayer) {
1251 displayRefreshCompositionDirtyFrame<
1252 CompositionCase<DefaultDisplaySetupVariant, BufferLayerVariant<CursorLayerProperties>,
1253 ChangeCompositionTypeVariant<IComposerClient::Composition::CURSOR,
1254 IComposerClient::Composition::CLIENT>,
1255 RECompositionResultVariant>>();
1256}
1257
1258TEST_F(CompositionTest, captureScreenCursorLayer) {
1259 captureScreenComposition<
1260 CompositionCase<DefaultDisplaySetupVariant, BufferLayerVariant<CursorLayerProperties>,
1261 NoCompositionTypeVariant, REScreenshotResultVariant>>();
1262}
1263
1264/* ------------------------------------------------------------------------
1265 * Simple buffer layer on a display which is powered off.
1266 */
1267
1268TEST_F(CompositionTest, displayOffHWCComposedNormalBufferLayerWithDirtyGeometry) {
1269 displayRefreshCompositionDirtyGeometry<CompositionCase<
1270 PoweredOffDisplaySetupVariant, BufferLayerVariant<DefaultLayerProperties>,
1271 KeepCompositionTypeVariant<IComposerClient::Composition::DEVICE>,
1272 HwcCompositionResultVariant>>();
1273}
1274
1275TEST_F(CompositionTest, displayOffHWCComposedNormalBufferLayerWithDirtyFrame) {
1276 displayRefreshCompositionDirtyFrame<CompositionCase<
1277 PoweredOffDisplaySetupVariant, BufferLayerVariant<DefaultLayerProperties>,
1278 KeepCompositionTypeVariant<IComposerClient::Composition::DEVICE>,
1279 HwcCompositionResultVariant>>();
1280}
1281
1282TEST_F(CompositionTest, displayOffREComposedNormalBufferLayer) {
1283 displayRefreshCompositionDirtyFrame<CompositionCase<
1284 PoweredOffDisplaySetupVariant, BufferLayerVariant<DefaultLayerProperties>,
1285 ChangeCompositionTypeVariant<IComposerClient::Composition::DEVICE,
1286 IComposerClient::Composition::CLIENT>,
1287 RECompositionResultVariant>>();
1288}
1289
1290TEST_F(CompositionTest, captureScreenNormalBufferLayerOnPoweredOffDisplay) {
1291 captureScreenComposition<CompositionCase<
1292 PoweredOffDisplaySetupVariant, BufferLayerVariant<DefaultLayerProperties>,
1293 NoCompositionTypeVariant, REScreenshotResultVariant>>();
1294}
1295
1296} // namespace
1297} // namespace android