blob: 3ba115e79dcacf3222a440fc95b23e5fedea0b29 [file] [log] [blame]
Lloyd Piquef58625d2017-12-19 13:22:33 -08001/*
2 * Copyright (C) 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#pragma once
18
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080019#include <compositionengine/Display.h>
Lloyd Pique9755fb72019-03-26 14:44:40 -070020#include <compositionengine/LayerFECompositionState.h>
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080021#include <compositionengine/OutputLayer.h>
Lloyd Pique70d91362018-10-18 16:02:55 -070022#include <compositionengine/impl/CompositionEngine.h>
Lloyd Pique9370a482019-10-03 17:58:30 -070023#include <compositionengine/impl/Display.h>
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080024#include <compositionengine/impl/OutputLayerCompositionState.h>
Dominik Laskowskif7a09ed2019-10-07 13:54:18 -070025#include <compositionengine/mock/DisplaySurface.h>
Lloyd Pique70d91362018-10-18 16:02:55 -070026
Lloyd Pique90c115d2018-09-18 21:39:42 -070027#include "BufferQueueLayer.h"
28#include "BufferStateLayer.h"
Lloyd Pique90c115d2018-09-18 21:39:42 -070029#include "ContainerLayer.h"
Lloyd Piquef58625d2017-12-19 13:22:33 -080030#include "DisplayDevice.h"
Vishnu Nairfa247b12020-02-11 08:58:26 -080031#include "EffectLayer.h"
Ady Abrahamc581d3c2020-08-06 17:34:27 -070032#include "FakeVsyncConfiguration.h"
Adithya Srinivasan2db3c1b2020-11-18 12:43:17 -080033#include "FrameTracer/FrameTracer.h"
Lloyd Piqued6b579f2018-04-06 15:29:10 -070034#include "Layer.h"
Lloyd Pique90c115d2018-09-18 21:39:42 -070035#include "NativeWindowSurface.h"
Dominik Laskowski9dab3432019-03-27 13:21:10 -070036#include "Scheduler/MessageQueue.h"
Steven Thomas2bbaabe2019-08-28 16:08:35 -070037#include "Scheduler/RefreshRateConfigs.h"
Lloyd Pique90c115d2018-09-18 21:39:42 -070038#include "StartPropertySetThread.h"
Lloyd Piquef58625d2017-12-19 13:22:33 -080039#include "SurfaceFlinger.h"
Lloyd Pique24f3bfe2019-10-02 19:29:10 -070040#include "SurfaceFlingerDefaultFactory.h"
Lloyd Pique90c115d2018-09-18 21:39:42 -070041#include "SurfaceInterceptor.h"
Dominik Laskowski7c9dbf92019-08-01 17:57:31 -070042#include "TestableScheduler.h"
Ady Abrahamabc27602020-04-08 17:20:29 -070043#include "mock/DisplayHardware/MockDisplay.h"
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +020044#include "mock/MockDisplayIdGenerator.h"
Adithya Srinivasan01189672020-10-20 14:23:05 -070045#include "mock/MockFrameTimeline.h"
Adithya Srinivasan2db3c1b2020-11-18 12:43:17 -080046#include "mock/MockFrameTracer.h"
Yiwei Zhang7e666a52018-11-15 13:33:42 -080047
Lloyd Piquef58625d2017-12-19 13:22:33 -080048namespace android {
49
Lloyd Piquee39cad22017-12-20 17:01:29 -080050class EventThread;
51
Peiyong Lin833074a2018-08-28 11:53:54 -070052namespace renderengine {
Lloyd Pique90c115d2018-09-18 21:39:42 -070053
Lloyd Piquee39cad22017-12-20 17:01:29 -080054class RenderEngine;
Lloyd Pique90c115d2018-09-18 21:39:42 -070055
56} // namespace renderengine
Lloyd Piquee39cad22017-12-20 17:01:29 -080057
58namespace Hwc2 {
Lloyd Pique90c115d2018-09-18 21:39:42 -070059
Lloyd Piquee39cad22017-12-20 17:01:29 -080060class Composer;
Lloyd Pique90c115d2018-09-18 21:39:42 -070061
62} // namespace Hwc2
63
Peiyong Line9d809e2020-04-14 13:10:48 -070064namespace hal = android::hardware::graphics::composer::hal;
65
Lloyd Pique90c115d2018-09-18 21:39:42 -070066namespace surfaceflinger::test {
67
68class Factory final : public surfaceflinger::Factory {
69public:
70 ~Factory() = default;
71
Lloyd Pique90c115d2018-09-18 21:39:42 -070072 std::unique_ptr<HWComposer> createHWComposer(const std::string&) override {
Lloyd Pique90c115d2018-09-18 21:39:42 -070073 return nullptr;
74 }
75
76 std::unique_ptr<MessageQueue> createMessageQueue() override {
Lloyd Pique90c115d2018-09-18 21:39:42 -070077 return std::make_unique<android::impl::MessageQueue>();
78 }
79
Ady Abraham8287e852020-08-12 14:44:58 -070080 std::unique_ptr<scheduler::VsyncConfiguration> createVsyncConfiguration(
Ady Abraham9e16a482019-12-03 17:19:41 -080081 const scheduler::RefreshRateConfigs& /*refreshRateConfigs*/) override {
Ana Krulec757f63a2019-01-25 10:46:18 -080082 return std::make_unique<scheduler::FakePhaseOffsets>();
83 }
84
Dominik Laskowski8b01cc02020-07-14 19:02:41 -070085 std::unique_ptr<Scheduler> createScheduler(const scheduler::RefreshRateConfigs&,
Ady Abraham3a77a7b2019-12-02 18:46:59 -080086 ISchedulerCallback&) override {
Lloyd Pique90c115d2018-09-18 21:39:42 -070087 return nullptr;
88 }
89
Pablo Gamitoc351d6f2020-09-17 15:34:26 +000090 sp<SurfaceInterceptor> createSurfaceInterceptor() override {
91 return new android::impl::SurfaceInterceptor();
Lloyd Pique90c115d2018-09-18 21:39:42 -070092 }
93
94 sp<StartPropertySetThread> createStartPropertySetThread(bool timestampPropertyValue) override {
Lloyd Pique90c115d2018-09-18 21:39:42 -070095 return new StartPropertySetThread(timestampPropertyValue);
96 }
97
Lloyd Pique9370a482019-10-03 17:58:30 -070098 sp<DisplayDevice> createDisplayDevice(DisplayDeviceCreationArgs& creationArgs) override {
99 return new DisplayDevice(creationArgs);
Lloyd Pique90c115d2018-09-18 21:39:42 -0700100 }
101
102 sp<GraphicBuffer> createGraphicBuffer(uint32_t width, uint32_t height, PixelFormat format,
103 uint32_t layerCount, uint64_t usage,
104 std::string requestorName) override {
Lloyd Pique90c115d2018-09-18 21:39:42 -0700105 return new GraphicBuffer(width, height, format, layerCount, usage, requestorName);
106 }
107
108 void createBufferQueue(sp<IGraphicBufferProducer>* outProducer,
109 sp<IGraphicBufferConsumer>* outConsumer,
110 bool consumerIsSurfaceFlinger) override {
Lloyd Piquee300b312019-10-03 13:03:45 -0700111 if (!mCreateBufferQueue) {
112 BufferQueue::createBufferQueue(outProducer, outConsumer, consumerIsSurfaceFlinger);
113 return;
114 }
Lloyd Pique90c115d2018-09-18 21:39:42 -0700115 mCreateBufferQueue(outProducer, outConsumer, consumerIsSurfaceFlinger);
116 }
117
Lloyd Piquee300b312019-10-03 13:03:45 -0700118 sp<IGraphicBufferProducer> createMonitoredProducer(const sp<IGraphicBufferProducer>& producer,
119 const sp<SurfaceFlinger>& flinger,
120 const wp<Layer>& layer) override {
121 return new MonitoredProducer(producer, flinger, layer);
122 }
123
124 sp<BufferLayerConsumer> createBufferLayerConsumer(const sp<IGraphicBufferConsumer>& consumer,
125 renderengine::RenderEngine& renderEngine,
126 uint32_t textureName, Layer* layer) override {
127 return new BufferLayerConsumer(consumer, renderEngine, textureName, layer);
128 }
129
Lloyd Pique90c115d2018-09-18 21:39:42 -0700130 std::unique_ptr<surfaceflinger::NativeWindowSurface> createNativeWindowSurface(
131 const sp<IGraphicBufferProducer>& producer) override {
132 if (!mCreateNativeWindowSurface) return nullptr;
133 return mCreateNativeWindowSurface(producer);
134 }
135
Lloyd Pique70d91362018-10-18 16:02:55 -0700136 std::unique_ptr<compositionengine::CompositionEngine> createCompositionEngine() override {
137 return compositionengine::impl::createCompositionEngine();
138 }
139
Lloyd Pique90c115d2018-09-18 21:39:42 -0700140 sp<BufferQueueLayer> createBufferQueueLayer(const LayerCreationArgs&) override {
Lloyd Pique90c115d2018-09-18 21:39:42 -0700141 return nullptr;
142 }
143
144 sp<BufferStateLayer> createBufferStateLayer(const LayerCreationArgs&) override {
Lloyd Pique90c115d2018-09-18 21:39:42 -0700145 return nullptr;
146 }
147
Vishnu Nairfa247b12020-02-11 08:58:26 -0800148 sp<EffectLayer> createEffectLayer(const LayerCreationArgs&) override { return nullptr; }
Lloyd Pique90c115d2018-09-18 21:39:42 -0700149
150 sp<ContainerLayer> createContainerLayer(const LayerCreationArgs&) override {
Lloyd Pique90c115d2018-09-18 21:39:42 -0700151 return nullptr;
152 }
153
Adithya Srinivasan2db3c1b2020-11-18 12:43:17 -0800154 std::unique_ptr<FrameTracer> createFrameTracer() override {
155 return std::make_unique<mock::FrameTracer>();
156 }
157
Adithya Srinivasan01189672020-10-20 14:23:05 -0700158 std::unique_ptr<frametimeline::FrameTimeline> createFrameTimeline(
159 std::shared_ptr<TimeStats> timeStats) override {
160 return std::make_unique<mock::FrameTimeline>(timeStats);
161 }
162
Lloyd Pique90c115d2018-09-18 21:39:42 -0700163 using CreateBufferQueueFunction =
164 std::function<void(sp<IGraphicBufferProducer>* /* outProducer */,
165 sp<IGraphicBufferConsumer>* /* outConsumer */,
166 bool /* consumerIsSurfaceFlinger */)>;
167 CreateBufferQueueFunction mCreateBufferQueue;
168
169 using CreateNativeWindowSurfaceFunction =
170 std::function<std::unique_ptr<surfaceflinger::NativeWindowSurface>(
171 const sp<IGraphicBufferProducer>&)>;
172 CreateNativeWindowSurfaceFunction mCreateNativeWindowSurface;
Lloyd Pique70d91362018-10-18 16:02:55 -0700173
174 using CreateCompositionEngineFunction =
175 std::function<std::unique_ptr<compositionengine::CompositionEngine>()>;
176 CreateCompositionEngineFunction mCreateCompositionEngine;
Lloyd Pique90c115d2018-09-18 21:39:42 -0700177};
178
179} // namespace surfaceflinger::test
Lloyd Piquee39cad22017-12-20 17:01:29 -0800180
Dominik Laskowski8b01cc02020-07-14 19:02:41 -0700181class TestableSurfaceFlinger final : private ISchedulerCallback {
Lloyd Piquef58625d2017-12-19 13:22:33 -0800182public:
Dominik Laskowskib7251f42020-04-20 17:42:59 -0700183 using HotplugEvent = SurfaceFlinger::HotplugEvent;
184
Dominik Laskowskif7a09ed2019-10-07 13:54:18 -0700185 SurfaceFlinger* flinger() { return mFlinger.get(); }
Dominik Laskowski7c9dbf92019-08-01 17:57:31 -0700186 TestableScheduler* scheduler() { return mScheduler; }
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200187 mock::DisplayIdGenerator<GpuVirtualDisplayId>& gpuVirtualDisplayIdGenerator() {
188 return mGpuVirtualDisplayIdGenerator;
189 }
Dominik Laskowski7c9dbf92019-08-01 17:57:31 -0700190
Lloyd Piquef58625d2017-12-19 13:22:33 -0800191 // Extend this as needed for accessing SurfaceFlinger private (and public)
192 // functions.
193
Peiyong Lin833074a2018-08-28 11:53:54 -0700194 void setupRenderEngine(std::unique_ptr<renderengine::RenderEngine> renderEngine) {
Lloyd Piqueb97e04f2018-10-18 17:07:05 -0700195 mFlinger->mCompositionEngine->setRenderEngine(std::move(renderEngine));
Lloyd Piquee39cad22017-12-20 17:01:29 -0800196 }
197
198 void setupComposer(std::unique_ptr<Hwc2::Composer> composer) {
Lloyd Pique441d5042018-10-18 16:49:51 -0700199 mFlinger->mCompositionEngine->setHwComposer(
200 std::make_unique<impl::HWComposer>(std::move(composer)));
Lloyd Piquee39cad22017-12-20 17:01:29 -0800201 }
202
Alec Mourie4034bb2019-11-19 12:45:54 -0800203 void setupTimeStats(const std::shared_ptr<TimeStats>& timeStats) {
204 mFlinger->mCompositionEngine->setTimeStats(timeStats);
205 }
206
Dominik Laskowski8b01cc02020-07-14 19:02:41 -0700207 // The ISchedulerCallback argument can be nullptr for a no-op implementation.
Ady Abraham8cb21882020-08-26 18:22:05 -0700208 void setupScheduler(std::unique_ptr<scheduler::VsyncController> vsyncController,
209 std::unique_ptr<scheduler::VSyncTracker> vsyncTracker,
Dominik Laskowski7c9dbf92019-08-01 17:57:31 -0700210 std::unique_ptr<EventThread> appEventThread,
Ady Abraham8a82ba62020-01-17 12:43:17 -0800211 std::unique_ptr<EventThread> sfEventThread,
Dominik Laskowski8b01cc02020-07-14 19:02:41 -0700212 ISchedulerCallback* callback = nullptr, bool hasMultipleConfigs = false) {
Ady Abrahamabc27602020-04-08 17:20:29 -0700213 std::vector<std::shared_ptr<const HWC2::Display::Config>> configs{
214 HWC2::Display::Config::Builder(mDisplay, 0)
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700215 .setVsyncPeriod(16'666'667)
Ady Abrahamabc27602020-04-08 17:20:29 -0700216 .setConfigGroup(0)
217 .build()};
218
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700219 if (hasMultipleConfigs) {
220 configs.emplace_back(HWC2::Display::Config::Builder(mDisplay, 1)
221 .setVsyncPeriod(11'111'111)
222 .setConfigGroup(0)
223 .build());
224 }
225
Ady Abraham2139f732019-11-13 18:56:40 -0800226 mFlinger->mRefreshRateConfigs = std::make_unique<
Ana Krulec3f6a2062020-01-23 15:48:01 -0800227 scheduler::RefreshRateConfigs>(configs, /*currentConfig=*/HwcConfigIndexType(0));
Ady Abraham2139f732019-11-13 18:56:40 -0800228 mFlinger->mRefreshRateStats = std::make_unique<
229 scheduler::RefreshRateStats>(*mFlinger->mRefreshRateConfigs, *mFlinger->mTimeStats,
230 /*currentConfig=*/HwcConfigIndexType(0),
Peiyong Lin65248e02020-04-18 21:15:07 -0700231 /*powerMode=*/hal::PowerMode::OFF);
Ady Abraham8287e852020-08-12 14:44:58 -0700232 mFlinger->mVsyncConfiguration =
233 mFactory.createVsyncConfiguration(*mFlinger->mRefreshRateConfigs);
234 mFlinger->mVsyncModulator.emplace(mFlinger->mVsyncConfiguration->getCurrentConfigs());
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700235
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700236 constexpr bool kUseContentDetectionV2 = false;
Ady Abraham8cb21882020-08-26 18:22:05 -0700237 mScheduler = new TestableScheduler(std::move(vsyncController), std::move(vsyncTracker),
238 *mFlinger->mRefreshRateConfigs, *(callback ?: this),
239 kUseContentDetectionV2);
Dominik Laskowski7c9dbf92019-08-01 17:57:31 -0700240
Dominik Laskowski98041832019-08-01 18:35:59 -0700241 mFlinger->mAppConnectionHandle = mScheduler->createConnection(std::move(appEventThread));
242 mFlinger->mSfConnectionHandle = mScheduler->createConnection(std::move(sfEventThread));
Dominik Laskowskif7a09ed2019-10-07 13:54:18 -0700243 resetScheduler(mScheduler);
Dominik Laskowski7c9dbf92019-08-01 17:57:31 -0700244 }
245
Dominik Laskowskif7a09ed2019-10-07 13:54:18 -0700246 void resetScheduler(Scheduler* scheduler) { mFlinger->mScheduler.reset(scheduler); }
247
Lloyd Pique90c115d2018-09-18 21:39:42 -0700248 using CreateBufferQueueFunction = surfaceflinger::test::Factory::CreateBufferQueueFunction;
Lloyd Pique5b36f3f2018-01-17 11:57:07 -0800249 void setCreateBufferQueueFunction(CreateBufferQueueFunction f) {
Lloyd Pique90c115d2018-09-18 21:39:42 -0700250 mFactory.mCreateBufferQueue = f;
Lloyd Pique5b36f3f2018-01-17 11:57:07 -0800251 }
252
Lloyd Pique90c115d2018-09-18 21:39:42 -0700253 using CreateNativeWindowSurfaceFunction =
254 surfaceflinger::test::Factory::CreateNativeWindowSurfaceFunction;
Lloyd Pique0b1fe702018-01-22 18:03:16 -0800255 void setCreateNativeWindowSurface(CreateNativeWindowSurfaceFunction f) {
Lloyd Pique90c115d2018-09-18 21:39:42 -0700256 mFactory.mCreateNativeWindowSurface = f;
Lloyd Pique0b1fe702018-01-22 18:03:16 -0800257 }
258
Daniel Solomon42d04562019-01-20 21:03:19 -0800259 void setInternalDisplayPrimaries(const ui::DisplayPrimaries& primaries) {
260 memcpy(&mFlinger->mInternalDisplayPrimaries, &primaries, sizeof(ui::DisplayPrimaries));
261 }
262
Dominik Laskowskib7251f42020-04-20 17:42:59 -0700263 static auto& mutableLayerCurrentState(const sp<Layer>& layer) { return layer->mCurrentState; }
264 static auto& mutableLayerDrawingState(const sp<Layer>& layer) { return layer->mDrawingState; }
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700265
Valerie Haud251afb2019-03-29 14:19:02 -0700266 auto& mutableStateLock() { return mFlinger->mStateLock; }
267
Dominik Laskowskib7251f42020-04-20 17:42:59 -0700268 static auto findOutputLayerForDisplay(const sp<Layer>& layer,
269 const sp<const DisplayDevice>& display) {
270 return layer->findOutputLayerForDisplay(display.get());
271 }
272
273 static void setLayerSidebandStream(const sp<Layer>& layer,
274 const sp<NativeHandle>& sidebandStream) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800275 layer->mDrawingState.sidebandStream = sidebandStream;
Lloyd Pique0b785d82018-12-04 17:25:27 -0800276 layer->mSidebandStream = sidebandStream;
Lloyd Piquede196652020-01-22 17:29:58 -0800277 layer->editCompositionState()->sidebandStream = sidebandStream;
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700278 }
279
Dominik Laskowskib7251f42020-04-20 17:42:59 -0700280 void setLayerCompositionType(const sp<Layer>& layer, hal::Composition type) {
281 auto outputLayer = findOutputLayerForDisplay(layer, mFlinger->getDefaultDisplayDevice());
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800282 LOG_ALWAYS_FATAL_IF(!outputLayer);
283 auto& state = outputLayer->editState();
284 LOG_ALWAYS_FATAL_IF(!outputLayer->getState().hwc);
Peiyong Line9d809e2020-04-14 13:10:48 -0700285 (*state.hwc).hwcCompositionType = type;
Dominik Laskowskib7251f42020-04-20 17:42:59 -0700286 }
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800287
Dominik Laskowskib7251f42020-04-20 17:42:59 -0700288 static void setLayerPotentialCursor(const sp<Layer>& layer, bool potentialCursor) {
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700289 layer->mPotentialCursor = potentialCursor;
290 }
291
Garfield Tande619fa2020-10-02 17:13:53 -0700292 static void setLayerDrawingParent(const sp<Layer>& layer, const sp<Layer>& drawingParent) {
293 layer->mDrawingParent = drawingParent;
294 }
295
Lloyd Piquef58625d2017-12-19 13:22:33 -0800296 /* ------------------------------------------------------------------------
297 * Forwarding for functions being tested
298 */
Lloyd Pique0b1fe702018-01-22 18:03:16 -0800299
Lloyd Piquea482f992018-01-22 19:00:34 -0800300 auto createDisplay(const String8& displayName, bool secure) {
301 return mFlinger->createDisplay(displayName, secure);
302 }
303
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700304 auto destroyDisplay(const sp<IBinder>& displayToken) {
305 return mFlinger->destroyDisplay(displayToken);
306 }
Lloyd Piquea482f992018-01-22 19:00:34 -0800307
Lloyd Pique9370a482019-10-03 17:58:30 -0700308 auto setupNewDisplayDeviceInternal(
309 const wp<IBinder>& displayToken,
310 std::shared_ptr<compositionengine::Display> compositionDisplay,
311 const DisplayDeviceState& state,
312 const sp<compositionengine::DisplaySurface>& dispSurface,
Dominik Laskowski0a1435d2020-04-21 00:27:31 -0700313 const sp<IGraphicBufferProducer>& producer) NO_THREAD_SAFETY_ANALYSIS {
Lloyd Pique9370a482019-10-03 17:58:30 -0700314 return mFlinger->setupNewDisplayDeviceInternal(displayToken, compositionDisplay, state,
315 dispSurface, producer);
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800316 }
317
Lloyd Pique0b1fe702018-01-22 18:03:16 -0800318 auto handleTransactionLocked(uint32_t transactionFlags) {
Vishnu Nairfa5f8df2019-02-13 17:02:51 -0800319 Mutex::Autolock _l(mFlinger->mStateLock);
Lloyd Pique0b1fe702018-01-22 18:03:16 -0800320 return mFlinger->handleTransactionLocked(transactionFlags);
321 }
Lloyd Piquef58625d2017-12-19 13:22:33 -0800322
Peiyong Line9d809e2020-04-14 13:10:48 -0700323 auto onHotplugReceived(int32_t sequenceId, hal::HWDisplayId display,
324 hal::Connection connection) {
Lloyd Pique6cf11032018-01-22 18:57:44 -0800325 return mFlinger->onHotplugReceived(sequenceId, display, connection);
326 }
327
Vishnu Nairce5d0cc2019-02-28 14:38:41 -0800328 auto setDisplayStateLocked(const DisplayState& s) {
329 Mutex::Autolock _l(mFlinger->mStateLock);
330 return mFlinger->setDisplayStateLocked(s);
331 }
Lloyd Pique9d9cf402018-02-16 17:47:13 -0800332
Dominik Laskowski83b88212018-12-11 13:34:06 -0800333 // Allow reading display state without locking, as if called on the SF main thread.
334 auto onInitializeDisplays() NO_THREAD_SAFETY_ANALYSIS {
335 return mFlinger->onInitializeDisplays();
336 }
Lloyd Pique86016da2018-03-01 16:09:38 -0800337
Lais Andrade3a6e47d2020-04-02 11:20:16 +0100338 auto notifyPowerBoost(int32_t boostId) { return mFlinger->notifyPowerBoost(boostId); }
339
Dominik Laskowski83b88212018-12-11 13:34:06 -0800340 // Allow reading display state without locking, as if called on the SF main thread.
341 auto setPowerModeInternal(const sp<DisplayDevice>& display,
Peiyong Lin65248e02020-04-18 21:15:07 -0700342 hal::PowerMode mode) NO_THREAD_SAFETY_ANALYSIS {
Dominik Laskowskie9774092018-12-11 10:04:24 -0800343 return mFlinger->setPowerModeInternal(display, mode);
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -0800344 }
345
Adithya Srinivasan5f683cf2020-09-15 14:21:04 -0700346 auto onMessageReceived(int32_t what) {
347 return mFlinger->onMessageReceived(what, /*vsyncId=*/0, systemTime());
348 }
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700349
chaviw426b5c02020-07-27 11:20:15 -0700350 auto renderScreenImplLocked(const RenderArea& renderArea,
351 SurfaceFlinger::TraverseLayersFunction traverseLayers,
chaviwc6ad8af2020-08-03 11:33:30 -0700352 const sp<GraphicBuffer>& buffer, bool forSystem, int* outSyncFd,
353 bool regionSampling) {
chaviw426b5c02020-07-27 11:20:15 -0700354 ScreenCaptureResults captureResults;
chaviwc6ad8af2020-08-03 11:33:30 -0700355 return mFlinger->renderScreenImplLocked(renderArea, traverseLayers, buffer, forSystem,
356 outSyncFd, regionSampling, captureResults);
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700357 }
358
chaviw4b9d5e12020-08-04 18:30:35 -0700359 auto traverseLayersInLayerStack(ui::LayerStack layerStack, int32_t uid,
Marin Shalamanov1c434292020-06-12 01:47:29 +0200360 const LayerVector::Visitor& visitor) {
chaviw4b9d5e12020-08-04 18:30:35 -0700361 return mFlinger->SurfaceFlinger::traverseLayersInLayerStack(layerStack, uid, visitor);
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700362 }
363
Daniel Solomon42d04562019-01-20 21:03:19 -0800364 auto getDisplayNativePrimaries(const sp<IBinder>& displayToken,
365 ui::DisplayPrimaries &primaries) {
366 return mFlinger->SurfaceFlinger::getDisplayNativePrimaries(displayToken, primaries);
367 }
368
Arthur Hung1bedccb2020-09-24 10:09:25 +0000369 auto& getTransactionQueue() { return mFlinger->mTransactionQueues; }
Valerie Haud251afb2019-03-29 14:19:02 -0700370
Ady Abraham22c7b5c2020-09-22 19:33:40 -0700371 auto setTransactionState(int64_t frameTimelineVsyncId, const Vector<ComposerState>& states,
Valerie Haud251afb2019-03-29 14:19:02 -0700372 const Vector<DisplayState>& displays, uint32_t flags,
373 const sp<IBinder>& applyToken,
374 const InputWindowCommands& inputWindowCommands,
375 int64_t desiredPresentTime, const client_cache_t& uncacheBuffer,
376 bool hasListenerCallbacks,
Pablo Gamito7eb7ee72020-08-05 10:57:05 +0000377 std::vector<ListenerCallbacks>& listenerCallbacks,
378 uint64_t transactionId) {
Ady Abraham22c7b5c2020-09-22 19:33:40 -0700379 return mFlinger->setTransactionState(frameTimelineVsyncId, states, displays, flags,
380 applyToken, inputWindowCommands, desiredPresentTime,
381 uncacheBuffer, hasListenerCallbacks, listenerCallbacks,
Pablo Gamito7eb7ee72020-08-05 10:57:05 +0000382 transactionId);
Valerie Haud251afb2019-03-29 14:19:02 -0700383 }
384
Arthur Hung1bedccb2020-09-24 10:09:25 +0000385 auto flushTransactionQueues() { return mFlinger->flushTransactionQueues(); };
Valerie Haud251afb2019-03-29 14:19:02 -0700386
Eduardo Rochaac867192020-09-14 20:12:17 +0000387 auto onTransact(uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) {
388 return mFlinger->onTransact(code, data, reply, flags);
389 }
390
Lloyd Pique86016da2018-03-01 16:09:38 -0800391 /* ------------------------------------------------------------------------
392 * Read-only access to private data to assert post-conditions.
393 */
394
395 const auto& getAnimFrameTracker() const { return mFlinger->mAnimFrameTracker; }
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -0800396 const auto& getHasPoweredOff() const { return mFlinger->mHasPoweredOff; }
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -0800397 const auto& getVisibleRegionsDirty() const { return mFlinger->mVisibleRegionsDirty; }
Lloyd Pique441d5042018-10-18 16:49:51 -0700398 auto& getHwComposer() const {
399 return static_cast<impl::HWComposer&>(mFlinger->getHwComposer());
400 }
Lloyd Pique9370a482019-10-03 17:58:30 -0700401 auto& getCompositionEngine() const { return mFlinger->getCompositionEngine(); }
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -0800402
Lloyd Pique86016da2018-03-01 16:09:38 -0800403 const auto& getCompositorTiming() const { return mFlinger->getBE().mCompositorTiming; }
404
Lloyd Piquef58625d2017-12-19 13:22:33 -0800405 /* ------------------------------------------------------------------------
406 * Read-write access to private data to set up preconditions and assert
407 * post-conditions.
408 */
Lloyd Pique0b1fe702018-01-22 18:03:16 -0800409
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800410 auto& mutableHasWideColorDisplay() { return SurfaceFlinger::hasWideColorDisplay; }
Peiyong Lin13effd12018-07-24 17:01:47 -0700411 auto& mutableUseColorManagement() { return SurfaceFlinger::useColorManagement; }
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800412
Lloyd Piquee39cad22017-12-20 17:01:29 -0800413 auto& mutableCurrentState() { return mFlinger->mCurrentState; }
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800414 auto& mutableDisplayColorSetting() { return mFlinger->mDisplayColorSetting; }
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700415 auto& mutableDisplays() { return mFlinger->mDisplays; }
Lloyd Piquee39cad22017-12-20 17:01:29 -0800416 auto& mutableDrawingState() { return mFlinger->mDrawingState; }
Lloyd Piquee39cad22017-12-20 17:01:29 -0800417 auto& mutableEventQueue() { return mFlinger->mEventQueue; }
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700418 auto& mutableGeometryInvalid() { return mFlinger->mGeometryInvalid; }
Lloyd Pique0b1fe702018-01-22 18:03:16 -0800419 auto& mutableInterceptor() { return mFlinger->mInterceptor; }
Lloyd Pique6cf11032018-01-22 18:57:44 -0800420 auto& mutableMainThreadId() { return mFlinger->mMainThreadId; }
Lloyd Pique0b1fe702018-01-22 18:03:16 -0800421 auto& mutablePendingHotplugEvents() { return mFlinger->mPendingHotplugEvents; }
Dominik Laskowski075d3172018-05-24 15:50:06 -0700422 auto& mutablePhysicalDisplayTokens() { return mFlinger->mPhysicalDisplayTokens; }
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700423 auto& mutableTexturePool() { return mFlinger->mTexturePool; }
Lloyd Pique0b1fe702018-01-22 18:03:16 -0800424 auto& mutableTransactionFlags() { return mFlinger->mTransactionFlags; }
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800425 auto& mutableUseHwcVirtualDisplays() { return mFlinger->mUseHwcVirtualDisplays; }
Peiyong Lin74ca2f42019-01-14 19:36:57 -0800426 auto& mutablePowerAdvisor() { return mFlinger->mPowerAdvisor; }
Lloyd Pique4fe29402019-08-12 16:51:24 -0700427 auto& mutableDebugDisableHWC() { return mFlinger->mDebugDisableHWC; }
Lloyd Piquee39cad22017-12-20 17:01:29 -0800428
Lloyd Pique6cf11032018-01-22 18:57:44 -0800429 auto& mutableComposerSequenceId() { return mFlinger->getBE().mComposerSequenceId; }
Lloyd Pique441d5042018-10-18 16:49:51 -0700430 auto& mutableHwcDisplayData() { return getHwComposer().mDisplayData; }
431 auto& mutableHwcPhysicalDisplayIdMap() { return getHwComposer().mPhysicalDisplayIdMap; }
432 auto& mutableInternalHwcDisplayId() { return getHwComposer().mInternalHwcDisplayId; }
433 auto& mutableExternalHwcDisplayId() { return getHwComposer().mExternalHwcDisplayId; }
Ady Abraham60e42ea2020-03-09 19:17:31 -0700434 auto& mutableUseFrameRateApi() { return mFlinger->useFrameRateApi; }
Lloyd Piquea618d852018-01-17 11:52:30 -0800435
Valerie Hau09e60052019-12-15 14:51:15 -0800436 auto fromHandle(const sp<IBinder>& handle) {
Valerie Hau09e60052019-12-15 14:51:15 -0800437 return mFlinger->fromHandle(handle);
438 }
439
Lloyd Piquee39cad22017-12-20 17:01:29 -0800440 ~TestableSurfaceFlinger() {
441 // All these pointer and container clears help ensure that GMock does
442 // not report a leaked object, since the SurfaceFlinger instance may
443 // still be referenced by something despite our best efforts to destroy
444 // it after each test is done.
445 mutableDisplays().clear();
Ady Abraham2939f092019-03-04 17:44:06 -0800446 mutableCurrentState().displays.clear();
447 mutableDrawingState().displays.clear();
Lloyd Pique0b1fe702018-01-22 18:03:16 -0800448 mutableEventQueue().reset();
Pablo Gamito5cfc6e52020-09-10 11:18:03 +0000449 mutableInterceptor().clear();
Lloyd Pique2eb71eb2019-08-13 13:23:03 -0700450 mFlinger->mScheduler.reset();
Lloyd Pique441d5042018-10-18 16:49:51 -0700451 mFlinger->mCompositionEngine->setHwComposer(std::unique_ptr<HWComposer>());
Lloyd Piqueb97e04f2018-10-18 17:07:05 -0700452 mFlinger->mCompositionEngine->setRenderEngine(
453 std::unique_ptr<renderengine::RenderEngine>());
Lloyd Piquee39cad22017-12-20 17:01:29 -0800454 }
Lloyd Piquef58625d2017-12-19 13:22:33 -0800455
Lloyd Piquea618d852018-01-17 11:52:30 -0800456 /* ------------------------------------------------------------------------
457 * Wrapper classes for Read-write access to private data to set up
458 * preconditions and assert post-conditions.
459 */
Ana Krulec4593b692019-01-11 22:07:25 -0800460 struct HWC2Display : public HWC2::impl::Display {
Peiyong Lin74ca2f42019-01-14 19:36:57 -0800461 HWC2Display(Hwc2::Composer& composer,
Peiyong Line9d809e2020-04-14 13:10:48 -0700462 const std::unordered_set<hal::Capability>& capabilities, hal::HWDisplayId id,
463 hal::DisplayType type)
Peiyong Lin74ca2f42019-01-14 19:36:57 -0800464 : HWC2::impl::Display(composer, capabilities, id, type) {}
Lloyd Piquea618d852018-01-17 11:52:30 -0800465 ~HWC2Display() {
466 // Prevents a call to disable vsyncs.
Peiyong Line9d809e2020-04-14 13:10:48 -0700467 mType = hal::DisplayType::INVALID;
Lloyd Piquea618d852018-01-17 11:52:30 -0800468 }
469
470 auto& mutableIsConnected() { return this->mIsConnected; }
471 auto& mutableConfigs() { return this->mConfigs; }
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700472 auto& mutableLayers() { return this->mLayers; }
Lloyd Piquea618d852018-01-17 11:52:30 -0800473 };
474
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800475 class FakeHwcDisplayInjector {
476 public:
Peiyong Line9d809e2020-04-14 13:10:48 -0700477 static constexpr hal::HWDisplayId DEFAULT_HWC_DISPLAY_ID = 1000;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800478 static constexpr int32_t DEFAULT_WIDTH = 1920;
479 static constexpr int32_t DEFAULT_HEIGHT = 1280;
480 static constexpr int32_t DEFAULT_REFRESH_RATE = 16'666'666;
Ady Abraham2139f732019-11-13 18:56:40 -0800481 static constexpr int32_t DEFAULT_CONFIG_GROUP = 7;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800482 static constexpr int32_t DEFAULT_DPI = 320;
Peiyong Line9d809e2020-04-14 13:10:48 -0700483 static constexpr hal::HWConfigId DEFAULT_ACTIVE_CONFIG = 0;
Peiyong Lin65248e02020-04-18 21:15:07 -0700484 static constexpr hal::PowerMode DEFAULT_POWER_MODE = hal::PowerMode::ON;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800485
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200486 FakeHwcDisplayInjector(HalDisplayId displayId, hal::DisplayType hwcDisplayType,
487 bool isPrimary)
Dominik Laskowski075d3172018-05-24 15:50:06 -0700488 : mDisplayId(displayId), mHwcDisplayType(hwcDisplayType), mIsPrimary(isPrimary) {}
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800489
Peiyong Line9d809e2020-04-14 13:10:48 -0700490 auto& setHwcDisplayId(hal::HWDisplayId displayId) {
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800491 mHwcDisplayId = displayId;
492 return *this;
493 }
494
495 auto& setWidth(int32_t width) {
496 mWidth = width;
497 return *this;
498 }
499
500 auto& setHeight(int32_t height) {
501 mHeight = height;
502 return *this;
503 }
504
Ady Abraham8a82ba62020-01-17 12:43:17 -0800505 auto& setRefreshRate(int32_t refreshRate) {
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800506 mRefreshRate = refreshRate;
507 return *this;
508 }
509
510 auto& setDpiX(int32_t dpi) {
511 mDpiX = dpi;
512 return *this;
513 }
514
515 auto& setDpiY(int32_t dpi) {
516 mDpiY = dpi;
517 return *this;
518 }
519
Peiyong Line9d809e2020-04-14 13:10:48 -0700520 auto& setActiveConfig(hal::HWConfigId config) {
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800521 mActiveConfig = config;
522 return *this;
523 }
524
Peiyong Line9d809e2020-04-14 13:10:48 -0700525 auto& setCapabilities(const std::unordered_set<hal::Capability>* capabilities) {
Lloyd Piquee22f0332018-07-16 16:35:56 -0700526 mCapabilities = capabilities;
527 return *this;
528 }
529
Peiyong Lin65248e02020-04-18 21:15:07 -0700530 auto& setPowerMode(hal::PowerMode mode) {
Peiyong Lin1336e6e2019-05-28 09:23:50 -0700531 mPowerMode = mode;
532 return *this;
533 }
534
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800535 void inject(TestableSurfaceFlinger* flinger, Hwc2::Composer* composer) {
Peiyong Line9d809e2020-04-14 13:10:48 -0700536 static const std::unordered_set<hal::Capability> defaultCapabilities;
Lloyd Piquee22f0332018-07-16 16:35:56 -0700537 if (mCapabilities == nullptr) mCapabilities = &defaultCapabilities;
538
539 // Caution - Make sure that any values passed by reference here do
540 // not refer to an instance owned by FakeHwcDisplayInjector. This
541 // class has temporary lifetime, while the constructed HWC2::Display
542 // is much longer lived.
Peiyong Lin74ca2f42019-01-14 19:36:57 -0800543 auto display = std::make_unique<HWC2Display>(*composer, *mCapabilities, mHwcDisplayId,
544 mHwcDisplayType);
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800545
546 auto config = HWC2::Display::Config::Builder(*display, mActiveConfig);
547 config.setWidth(mWidth);
548 config.setHeight(mHeight);
549 config.setVsyncPeriod(mRefreshRate);
550 config.setDpiX(mDpiX);
551 config.setDpiY(mDpiY);
Ady Abraham2139f732019-11-13 18:56:40 -0800552 config.setConfigGroup(mConfigGroup);
Ady Abraham8a82ba62020-01-17 12:43:17 -0800553 display->mutableConfigs().emplace(static_cast<int32_t>(mActiveConfig), config.build());
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800554 display->mutableIsConnected() = true;
Peiyong Lin65248e02020-04-18 21:15:07 -0700555 display->setPowerMode(mPowerMode);
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800556
Peiyong Linbdd08cc2019-12-17 21:35:14 -0800557 flinger->mutableHwcDisplayData()[mDisplayId].hwcDisplay = std::move(display);
Dominik Laskowski075d3172018-05-24 15:50:06 -0700558
Peiyong Line9d809e2020-04-14 13:10:48 -0700559 if (mHwcDisplayType == hal::DisplayType::PHYSICAL) {
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200560 const auto physicalId = PhysicalDisplayId::tryCast(mDisplayId);
561 LOG_ALWAYS_FATAL_IF(!physicalId);
562 flinger->mutableHwcPhysicalDisplayIdMap().emplace(mHwcDisplayId, *physicalId);
Dominik Laskowski075d3172018-05-24 15:50:06 -0700563 (mIsPrimary ? flinger->mutableInternalHwcDisplayId()
564 : flinger->mutableExternalHwcDisplayId()) = mHwcDisplayId;
565 }
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800566 }
567
568 private:
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200569 const HalDisplayId mDisplayId;
Peiyong Line9d809e2020-04-14 13:10:48 -0700570 const hal::DisplayType mHwcDisplayType;
Dominik Laskowski075d3172018-05-24 15:50:06 -0700571 const bool mIsPrimary;
572
Peiyong Line9d809e2020-04-14 13:10:48 -0700573 hal::HWDisplayId mHwcDisplayId = DEFAULT_HWC_DISPLAY_ID;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800574 int32_t mWidth = DEFAULT_WIDTH;
575 int32_t mHeight = DEFAULT_HEIGHT;
Ady Abraham8a82ba62020-01-17 12:43:17 -0800576 int32_t mRefreshRate = DEFAULT_REFRESH_RATE;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800577 int32_t mDpiX = DEFAULT_DPI;
Ady Abraham2139f732019-11-13 18:56:40 -0800578 int32_t mConfigGroup = DEFAULT_CONFIG_GROUP;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800579 int32_t mDpiY = DEFAULT_DPI;
Peiyong Line9d809e2020-04-14 13:10:48 -0700580 hal::HWConfigId mActiveConfig = DEFAULT_ACTIVE_CONFIG;
Peiyong Lin65248e02020-04-18 21:15:07 -0700581 hal::PowerMode mPowerMode = DEFAULT_POWER_MODE;
Peiyong Line9d809e2020-04-14 13:10:48 -0700582 const std::unordered_set<hal::Capability>* mCapabilities = nullptr;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800583 };
584
585 class FakeDisplayDeviceInjector {
586 public:
Dominik Laskowski075d3172018-05-24 15:50:06 -0700587 FakeDisplayDeviceInjector(TestableSurfaceFlinger& flinger,
Lloyd Pique9370a482019-10-03 17:58:30 -0700588 std::shared_ptr<compositionengine::Display> compositionDisplay,
Dominik Laskowski55c85402020-01-21 16:25:47 -0800589 std::optional<DisplayConnectionType> connectionType,
Peiyong Line9d809e2020-04-14 13:10:48 -0700590 std::optional<hal::HWDisplayId> hwcDisplayId, bool isPrimary)
Lloyd Pique9370a482019-10-03 17:58:30 -0700591 : mFlinger(flinger),
Marin Shalamanov700e6392020-02-12 20:22:26 +0100592 mCreationArgs(flinger.mFlinger.get(), mDisplayToken, compositionDisplay),
593 mHwcDisplayId(hwcDisplayId) {
Dominik Laskowski55c85402020-01-21 16:25:47 -0800594 mCreationArgs.connectionType = connectionType;
Dominik Laskowski075d3172018-05-24 15:50:06 -0700595 mCreationArgs.isPrimary = isPrimary;
596 }
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800597
598 sp<IBinder> token() const { return mDisplayToken; }
599
600 DisplayDeviceState& mutableDrawingDisplayState() {
601 return mFlinger.mutableDrawingState().displays.editValueFor(mDisplayToken);
602 }
603
604 DisplayDeviceState& mutableCurrentDisplayState() {
605 return mFlinger.mutableCurrentState().displays.editValueFor(mDisplayToken);
606 }
607
Lloyd Pique9d9cf402018-02-16 17:47:13 -0800608 const auto& getDrawingDisplayState() {
609 return mFlinger.mutableDrawingState().displays.valueFor(mDisplayToken);
610 }
611
612 const auto& getCurrentDisplayState() {
613 return mFlinger.mutableCurrentState().displays.valueFor(mDisplayToken);
614 }
615
Dominik Laskowski9fae1022018-05-29 13:17:40 -0700616 auto& mutableDisplayDevice() { return mFlinger.mutableDisplays()[mDisplayToken]; }
Lloyd Pique9d9cf402018-02-16 17:47:13 -0800617
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800618 auto& setNativeWindow(const sp<ANativeWindow>& nativeWindow) {
Lloyd Pique2eef1d22018-09-18 21:30:04 -0700619 mCreationArgs.nativeWindow = nativeWindow;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800620 return *this;
621 }
622
Lloyd Pique542307f2018-10-19 13:24:08 -0700623 auto& setDisplaySurface(const sp<compositionengine::DisplaySurface>& displaySurface) {
Lloyd Pique2eef1d22018-09-18 21:30:04 -0700624 mCreationArgs.displaySurface = displaySurface;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800625 return *this;
626 }
627
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800628 auto& setSecure(bool secure) {
Lloyd Pique2eef1d22018-09-18 21:30:04 -0700629 mCreationArgs.isSecure = secure;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800630 return *this;
631 }
632
Peiyong Lin65248e02020-04-18 21:15:07 -0700633 auto& setPowerMode(hal::PowerMode mode) {
Lloyd Pique2eef1d22018-09-18 21:30:04 -0700634 mCreationArgs.initialPowerMode = mode;
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700635 return *this;
636 }
637
Valerie Hau9758ae02018-10-09 16:05:09 -0700638 auto& setHwcColorModes(
639 const std::unordered_map<ui::ColorMode, std::vector<ui::RenderIntent>>
640 hwcColorModes) {
641 mCreationArgs.hwcColorModes = hwcColorModes;
642 return *this;
643 }
644
645 auto& setHasWideColorGamut(bool hasWideColorGamut) {
646 mCreationArgs.hasWideColorGamut = hasWideColorGamut;
647 return *this;
648 }
649
Lloyd Pique33050472019-12-19 17:12:44 -0800650 auto& setPhysicalOrientation(ui::Rotation orientation) {
651 mCreationArgs.physicalOrientation = orientation;
652 return *this;
653 }
654
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800655 sp<DisplayDevice> inject() {
Lloyd Pique9370a482019-10-03 17:58:30 -0700656 const auto displayId = mCreationArgs.compositionDisplay->getDisplayId();
657
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800658 DisplayDeviceState state;
Dominik Laskowski55c85402020-01-21 16:25:47 -0800659 if (const auto type = mCreationArgs.connectionType) {
Lloyd Pique9370a482019-10-03 17:58:30 -0700660 LOG_ALWAYS_FATAL_IF(!displayId);
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200661 const auto physicalId = PhysicalDisplayId::tryCast(*displayId);
662 LOG_ALWAYS_FATAL_IF(!physicalId);
Marin Shalamanov700e6392020-02-12 20:22:26 +0100663 LOG_ALWAYS_FATAL_IF(!mHwcDisplayId);
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200664 state.physical = {.id = *physicalId, .type = *type, .hwcDisplayId = *mHwcDisplayId};
Dominik Laskowski55c85402020-01-21 16:25:47 -0800665 }
666
Lloyd Pique2eef1d22018-09-18 21:30:04 -0700667 state.isSecure = mCreationArgs.isSecure;
668
Lloyd Pique9370a482019-10-03 17:58:30 -0700669 sp<DisplayDevice> device = new DisplayDevice(mCreationArgs);
Lloyd Pique2eef1d22018-09-18 21:30:04 -0700670 mFlinger.mutableDisplays().emplace(mDisplayToken, device);
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800671 mFlinger.mutableCurrentState().displays.add(mDisplayToken, state);
672 mFlinger.mutableDrawingState().displays.add(mDisplayToken, state);
673
Dominik Laskowski55c85402020-01-21 16:25:47 -0800674 if (const auto& physical = state.physical) {
675 mFlinger.mutablePhysicalDisplayTokens()[physical->id] = mDisplayToken;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800676 }
677
678 return device;
679 }
680
681 private:
682 TestableSurfaceFlinger& mFlinger;
683 sp<BBinder> mDisplayToken = new BBinder();
Lloyd Pique2eef1d22018-09-18 21:30:04 -0700684 DisplayDeviceCreationArgs mCreationArgs;
Peiyong Line9d809e2020-04-14 13:10:48 -0700685 const std::optional<hal::HWDisplayId> mHwcDisplayId;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800686 };
687
Dominik Laskowski8b01cc02020-07-14 19:02:41 -0700688private:
689 void setVsyncEnabled(bool) override {}
690 void changeRefreshRate(const Scheduler::RefreshRate&, Scheduler::ConfigEvent) override {}
691 void repaintEverythingForHWC() override {}
692 void kernelTimerChanged(bool) override {}
693
Lloyd Pique90c115d2018-09-18 21:39:42 -0700694 surfaceflinger::test::Factory mFactory;
695 sp<SurfaceFlinger> mFlinger = new SurfaceFlinger(mFactory, SurfaceFlinger::SkipInitialization);
Dominik Laskowski7c9dbf92019-08-01 17:57:31 -0700696 TestableScheduler* mScheduler = nullptr;
Ady Abrahamabc27602020-04-08 17:20:29 -0700697 Hwc2::mock::Display mDisplay;
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200698 mock::DisplayIdGenerator<GpuVirtualDisplayId> mGpuVirtualDisplayIdGenerator;
Lloyd Piquef58625d2017-12-19 13:22:33 -0800699};
700
701} // namespace android