blob: 8a0276ac8651b29b49f9f0b15deeee76bfb599c8 [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 Srinivasan2db3c1b2020-11-18 12:43:17 -080045#include "mock/MockFrameTracer.h"
Yiwei Zhang7e666a52018-11-15 13:33:42 -080046
Lloyd Piquef58625d2017-12-19 13:22:33 -080047namespace android {
48
Lloyd Piquee39cad22017-12-20 17:01:29 -080049class EventThread;
50
Peiyong Lin833074a2018-08-28 11:53:54 -070051namespace renderengine {
Lloyd Pique90c115d2018-09-18 21:39:42 -070052
Lloyd Piquee39cad22017-12-20 17:01:29 -080053class RenderEngine;
Lloyd Pique90c115d2018-09-18 21:39:42 -070054
55} // namespace renderengine
Lloyd Piquee39cad22017-12-20 17:01:29 -080056
57namespace Hwc2 {
Lloyd Pique90c115d2018-09-18 21:39:42 -070058
Lloyd Piquee39cad22017-12-20 17:01:29 -080059class Composer;
Lloyd Pique90c115d2018-09-18 21:39:42 -070060
61} // namespace Hwc2
62
Peiyong Line9d809e2020-04-14 13:10:48 -070063namespace hal = android::hardware::graphics::composer::hal;
64
Lloyd Pique90c115d2018-09-18 21:39:42 -070065namespace surfaceflinger::test {
66
67class Factory final : public surfaceflinger::Factory {
68public:
69 ~Factory() = default;
70
Lloyd Pique90c115d2018-09-18 21:39:42 -070071 std::unique_ptr<HWComposer> createHWComposer(const std::string&) override {
Lloyd Pique90c115d2018-09-18 21:39:42 -070072 return nullptr;
73 }
74
75 std::unique_ptr<MessageQueue> createMessageQueue() override {
Lloyd Pique90c115d2018-09-18 21:39:42 -070076 return std::make_unique<android::impl::MessageQueue>();
77 }
78
Ady Abraham8287e852020-08-12 14:44:58 -070079 std::unique_ptr<scheduler::VsyncConfiguration> createVsyncConfiguration(
Ady Abraham9e16a482019-12-03 17:19:41 -080080 const scheduler::RefreshRateConfigs& /*refreshRateConfigs*/) override {
Ana Krulec757f63a2019-01-25 10:46:18 -080081 return std::make_unique<scheduler::FakePhaseOffsets>();
82 }
83
Dominik Laskowski8b01cc02020-07-14 19:02:41 -070084 std::unique_ptr<Scheduler> createScheduler(const scheduler::RefreshRateConfigs&,
Ady Abraham3a77a7b2019-12-02 18:46:59 -080085 ISchedulerCallback&) override {
Lloyd Pique90c115d2018-09-18 21:39:42 -070086 return nullptr;
87 }
88
Pablo Gamitoc351d6f2020-09-17 15:34:26 +000089 sp<SurfaceInterceptor> createSurfaceInterceptor() override {
90 return new android::impl::SurfaceInterceptor();
Lloyd Pique90c115d2018-09-18 21:39:42 -070091 }
92
93 sp<StartPropertySetThread> createStartPropertySetThread(bool timestampPropertyValue) override {
Lloyd Pique90c115d2018-09-18 21:39:42 -070094 return new StartPropertySetThread(timestampPropertyValue);
95 }
96
Lloyd Pique9370a482019-10-03 17:58:30 -070097 sp<DisplayDevice> createDisplayDevice(DisplayDeviceCreationArgs& creationArgs) override {
98 return new DisplayDevice(creationArgs);
Lloyd Pique90c115d2018-09-18 21:39:42 -070099 }
100
101 sp<GraphicBuffer> createGraphicBuffer(uint32_t width, uint32_t height, PixelFormat format,
102 uint32_t layerCount, uint64_t usage,
103 std::string requestorName) override {
Lloyd Pique90c115d2018-09-18 21:39:42 -0700104 return new GraphicBuffer(width, height, format, layerCount, usage, requestorName);
105 }
106
107 void createBufferQueue(sp<IGraphicBufferProducer>* outProducer,
108 sp<IGraphicBufferConsumer>* outConsumer,
109 bool consumerIsSurfaceFlinger) override {
Lloyd Piquee300b312019-10-03 13:03:45 -0700110 if (!mCreateBufferQueue) {
111 BufferQueue::createBufferQueue(outProducer, outConsumer, consumerIsSurfaceFlinger);
112 return;
113 }
Lloyd Pique90c115d2018-09-18 21:39:42 -0700114 mCreateBufferQueue(outProducer, outConsumer, consumerIsSurfaceFlinger);
115 }
116
Lloyd Piquee300b312019-10-03 13:03:45 -0700117 sp<IGraphicBufferProducer> createMonitoredProducer(const sp<IGraphicBufferProducer>& producer,
118 const sp<SurfaceFlinger>& flinger,
119 const wp<Layer>& layer) override {
120 return new MonitoredProducer(producer, flinger, layer);
121 }
122
123 sp<BufferLayerConsumer> createBufferLayerConsumer(const sp<IGraphicBufferConsumer>& consumer,
124 renderengine::RenderEngine& renderEngine,
125 uint32_t textureName, Layer* layer) override {
126 return new BufferLayerConsumer(consumer, renderEngine, textureName, layer);
127 }
128
Lloyd Pique90c115d2018-09-18 21:39:42 -0700129 std::unique_ptr<surfaceflinger::NativeWindowSurface> createNativeWindowSurface(
130 const sp<IGraphicBufferProducer>& producer) override {
131 if (!mCreateNativeWindowSurface) return nullptr;
132 return mCreateNativeWindowSurface(producer);
133 }
134
Lloyd Pique70d91362018-10-18 16:02:55 -0700135 std::unique_ptr<compositionengine::CompositionEngine> createCompositionEngine() override {
136 return compositionengine::impl::createCompositionEngine();
137 }
138
Lloyd Pique90c115d2018-09-18 21:39:42 -0700139 sp<BufferQueueLayer> createBufferQueueLayer(const LayerCreationArgs&) override {
Lloyd Pique90c115d2018-09-18 21:39:42 -0700140 return nullptr;
141 }
142
143 sp<BufferStateLayer> createBufferStateLayer(const LayerCreationArgs&) override {
Lloyd Pique90c115d2018-09-18 21:39:42 -0700144 return nullptr;
145 }
146
Vishnu Nairfa247b12020-02-11 08:58:26 -0800147 sp<EffectLayer> createEffectLayer(const LayerCreationArgs&) override { return nullptr; }
Lloyd Pique90c115d2018-09-18 21:39:42 -0700148
149 sp<ContainerLayer> createContainerLayer(const LayerCreationArgs&) override {
Lloyd Pique90c115d2018-09-18 21:39:42 -0700150 return nullptr;
151 }
152
Adithya Srinivasan2db3c1b2020-11-18 12:43:17 -0800153 std::unique_ptr<FrameTracer> createFrameTracer() override {
154 return std::make_unique<mock::FrameTracer>();
155 }
156
Lloyd Pique90c115d2018-09-18 21:39:42 -0700157 using CreateBufferQueueFunction =
158 std::function<void(sp<IGraphicBufferProducer>* /* outProducer */,
159 sp<IGraphicBufferConsumer>* /* outConsumer */,
160 bool /* consumerIsSurfaceFlinger */)>;
161 CreateBufferQueueFunction mCreateBufferQueue;
162
163 using CreateNativeWindowSurfaceFunction =
164 std::function<std::unique_ptr<surfaceflinger::NativeWindowSurface>(
165 const sp<IGraphicBufferProducer>&)>;
166 CreateNativeWindowSurfaceFunction mCreateNativeWindowSurface;
Lloyd Pique70d91362018-10-18 16:02:55 -0700167
168 using CreateCompositionEngineFunction =
169 std::function<std::unique_ptr<compositionengine::CompositionEngine>()>;
170 CreateCompositionEngineFunction mCreateCompositionEngine;
Lloyd Pique90c115d2018-09-18 21:39:42 -0700171};
172
173} // namespace surfaceflinger::test
Lloyd Piquee39cad22017-12-20 17:01:29 -0800174
Dominik Laskowski8b01cc02020-07-14 19:02:41 -0700175class TestableSurfaceFlinger final : private ISchedulerCallback {
Lloyd Piquef58625d2017-12-19 13:22:33 -0800176public:
Dominik Laskowskib7251f42020-04-20 17:42:59 -0700177 using HotplugEvent = SurfaceFlinger::HotplugEvent;
178
Dominik Laskowskif7a09ed2019-10-07 13:54:18 -0700179 SurfaceFlinger* flinger() { return mFlinger.get(); }
Dominik Laskowski7c9dbf92019-08-01 17:57:31 -0700180 TestableScheduler* scheduler() { return mScheduler; }
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200181 mock::DisplayIdGenerator<GpuVirtualDisplayId>& gpuVirtualDisplayIdGenerator() {
182 return mGpuVirtualDisplayIdGenerator;
183 }
Dominik Laskowski7c9dbf92019-08-01 17:57:31 -0700184
Lloyd Piquef58625d2017-12-19 13:22:33 -0800185 // Extend this as needed for accessing SurfaceFlinger private (and public)
186 // functions.
187
Peiyong Lin833074a2018-08-28 11:53:54 -0700188 void setupRenderEngine(std::unique_ptr<renderengine::RenderEngine> renderEngine) {
Lloyd Piqueb97e04f2018-10-18 17:07:05 -0700189 mFlinger->mCompositionEngine->setRenderEngine(std::move(renderEngine));
Lloyd Piquee39cad22017-12-20 17:01:29 -0800190 }
191
192 void setupComposer(std::unique_ptr<Hwc2::Composer> composer) {
Lloyd Pique441d5042018-10-18 16:49:51 -0700193 mFlinger->mCompositionEngine->setHwComposer(
194 std::make_unique<impl::HWComposer>(std::move(composer)));
Lloyd Piquee39cad22017-12-20 17:01:29 -0800195 }
196
Alec Mourie4034bb2019-11-19 12:45:54 -0800197 void setupTimeStats(const std::shared_ptr<TimeStats>& timeStats) {
198 mFlinger->mCompositionEngine->setTimeStats(timeStats);
199 }
200
Dominik Laskowski8b01cc02020-07-14 19:02:41 -0700201 // The ISchedulerCallback argument can be nullptr for a no-op implementation.
Ady Abraham8cb21882020-08-26 18:22:05 -0700202 void setupScheduler(std::unique_ptr<scheduler::VsyncController> vsyncController,
203 std::unique_ptr<scheduler::VSyncTracker> vsyncTracker,
Dominik Laskowski7c9dbf92019-08-01 17:57:31 -0700204 std::unique_ptr<EventThread> appEventThread,
Ady Abraham8a82ba62020-01-17 12:43:17 -0800205 std::unique_ptr<EventThread> sfEventThread,
Dominik Laskowski8b01cc02020-07-14 19:02:41 -0700206 ISchedulerCallback* callback = nullptr, bool hasMultipleConfigs = false) {
Ady Abrahamabc27602020-04-08 17:20:29 -0700207 std::vector<std::shared_ptr<const HWC2::Display::Config>> configs{
208 HWC2::Display::Config::Builder(mDisplay, 0)
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700209 .setVsyncPeriod(16'666'667)
Ady Abrahamabc27602020-04-08 17:20:29 -0700210 .setConfigGroup(0)
211 .build()};
212
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700213 if (hasMultipleConfigs) {
214 configs.emplace_back(HWC2::Display::Config::Builder(mDisplay, 1)
215 .setVsyncPeriod(11'111'111)
216 .setConfigGroup(0)
217 .build());
218 }
219
Ady Abraham2139f732019-11-13 18:56:40 -0800220 mFlinger->mRefreshRateConfigs = std::make_unique<
Ana Krulec3f6a2062020-01-23 15:48:01 -0800221 scheduler::RefreshRateConfigs>(configs, /*currentConfig=*/HwcConfigIndexType(0));
Ady Abraham2139f732019-11-13 18:56:40 -0800222 mFlinger->mRefreshRateStats = std::make_unique<
223 scheduler::RefreshRateStats>(*mFlinger->mRefreshRateConfigs, *mFlinger->mTimeStats,
224 /*currentConfig=*/HwcConfigIndexType(0),
Peiyong Lin65248e02020-04-18 21:15:07 -0700225 /*powerMode=*/hal::PowerMode::OFF);
Ady Abraham8287e852020-08-12 14:44:58 -0700226 mFlinger->mVsyncConfiguration =
227 mFactory.createVsyncConfiguration(*mFlinger->mRefreshRateConfigs);
228 mFlinger->mVsyncModulator.emplace(mFlinger->mVsyncConfiguration->getCurrentConfigs());
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700229
Ady Abraham8cb21882020-08-26 18:22:05 -0700230 mScheduler = new TestableScheduler(std::move(vsyncController), std::move(vsyncTracker),
Marin Shalamanov27fa3de2020-11-20 16:22:48 +0100231 *mFlinger->mRefreshRateConfigs, *(callback ?: this));
Dominik Laskowski7c9dbf92019-08-01 17:57:31 -0700232
Dominik Laskowski98041832019-08-01 18:35:59 -0700233 mFlinger->mAppConnectionHandle = mScheduler->createConnection(std::move(appEventThread));
234 mFlinger->mSfConnectionHandle = mScheduler->createConnection(std::move(sfEventThread));
Dominik Laskowskif7a09ed2019-10-07 13:54:18 -0700235 resetScheduler(mScheduler);
Dominik Laskowski7c9dbf92019-08-01 17:57:31 -0700236 }
237
Dominik Laskowskif7a09ed2019-10-07 13:54:18 -0700238 void resetScheduler(Scheduler* scheduler) { mFlinger->mScheduler.reset(scheduler); }
239
Lloyd Pique90c115d2018-09-18 21:39:42 -0700240 using CreateBufferQueueFunction = surfaceflinger::test::Factory::CreateBufferQueueFunction;
Lloyd Pique5b36f3f2018-01-17 11:57:07 -0800241 void setCreateBufferQueueFunction(CreateBufferQueueFunction f) {
Lloyd Pique90c115d2018-09-18 21:39:42 -0700242 mFactory.mCreateBufferQueue = f;
Lloyd Pique5b36f3f2018-01-17 11:57:07 -0800243 }
244
Lloyd Pique90c115d2018-09-18 21:39:42 -0700245 using CreateNativeWindowSurfaceFunction =
246 surfaceflinger::test::Factory::CreateNativeWindowSurfaceFunction;
Lloyd Pique0b1fe702018-01-22 18:03:16 -0800247 void setCreateNativeWindowSurface(CreateNativeWindowSurfaceFunction f) {
Lloyd Pique90c115d2018-09-18 21:39:42 -0700248 mFactory.mCreateNativeWindowSurface = f;
Lloyd Pique0b1fe702018-01-22 18:03:16 -0800249 }
250
Daniel Solomon42d04562019-01-20 21:03:19 -0800251 void setInternalDisplayPrimaries(const ui::DisplayPrimaries& primaries) {
252 memcpy(&mFlinger->mInternalDisplayPrimaries, &primaries, sizeof(ui::DisplayPrimaries));
253 }
254
Dominik Laskowskib7251f42020-04-20 17:42:59 -0700255 static auto& mutableLayerCurrentState(const sp<Layer>& layer) { return layer->mCurrentState; }
256 static auto& mutableLayerDrawingState(const sp<Layer>& layer) { return layer->mDrawingState; }
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700257
Valerie Haud251afb2019-03-29 14:19:02 -0700258 auto& mutableStateLock() { return mFlinger->mStateLock; }
259
Dominik Laskowskib7251f42020-04-20 17:42:59 -0700260 static auto findOutputLayerForDisplay(const sp<Layer>& layer,
261 const sp<const DisplayDevice>& display) {
262 return layer->findOutputLayerForDisplay(display.get());
263 }
264
265 static void setLayerSidebandStream(const sp<Layer>& layer,
266 const sp<NativeHandle>& sidebandStream) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800267 layer->mDrawingState.sidebandStream = sidebandStream;
Lloyd Pique0b785d82018-12-04 17:25:27 -0800268 layer->mSidebandStream = sidebandStream;
Lloyd Piquede196652020-01-22 17:29:58 -0800269 layer->editCompositionState()->sidebandStream = sidebandStream;
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700270 }
271
Dominik Laskowskib7251f42020-04-20 17:42:59 -0700272 void setLayerCompositionType(const sp<Layer>& layer, hal::Composition type) {
273 auto outputLayer = findOutputLayerForDisplay(layer, mFlinger->getDefaultDisplayDevice());
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800274 LOG_ALWAYS_FATAL_IF(!outputLayer);
275 auto& state = outputLayer->editState();
276 LOG_ALWAYS_FATAL_IF(!outputLayer->getState().hwc);
Peiyong Line9d809e2020-04-14 13:10:48 -0700277 (*state.hwc).hwcCompositionType = type;
Dominik Laskowskib7251f42020-04-20 17:42:59 -0700278 }
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800279
Dominik Laskowskib7251f42020-04-20 17:42:59 -0700280 static void setLayerPotentialCursor(const sp<Layer>& layer, bool potentialCursor) {
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700281 layer->mPotentialCursor = potentialCursor;
282 }
283
Garfield Tande619fa2020-10-02 17:13:53 -0700284 static void setLayerDrawingParent(const sp<Layer>& layer, const sp<Layer>& drawingParent) {
285 layer->mDrawingParent = drawingParent;
286 }
287
Lloyd Piquef58625d2017-12-19 13:22:33 -0800288 /* ------------------------------------------------------------------------
289 * Forwarding for functions being tested
290 */
Lloyd Pique0b1fe702018-01-22 18:03:16 -0800291
Lloyd Piquea482f992018-01-22 19:00:34 -0800292 auto createDisplay(const String8& displayName, bool secure) {
293 return mFlinger->createDisplay(displayName, secure);
294 }
295
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700296 auto destroyDisplay(const sp<IBinder>& displayToken) {
297 return mFlinger->destroyDisplay(displayToken);
298 }
Lloyd Piquea482f992018-01-22 19:00:34 -0800299
Lloyd Pique9370a482019-10-03 17:58:30 -0700300 auto setupNewDisplayDeviceInternal(
301 const wp<IBinder>& displayToken,
302 std::shared_ptr<compositionengine::Display> compositionDisplay,
303 const DisplayDeviceState& state,
304 const sp<compositionengine::DisplaySurface>& dispSurface,
Dominik Laskowski0a1435d2020-04-21 00:27:31 -0700305 const sp<IGraphicBufferProducer>& producer) NO_THREAD_SAFETY_ANALYSIS {
Lloyd Pique9370a482019-10-03 17:58:30 -0700306 return mFlinger->setupNewDisplayDeviceInternal(displayToken, compositionDisplay, state,
307 dispSurface, producer);
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800308 }
309
Lloyd Pique0b1fe702018-01-22 18:03:16 -0800310 auto handleTransactionLocked(uint32_t transactionFlags) {
Vishnu Nairfa5f8df2019-02-13 17:02:51 -0800311 Mutex::Autolock _l(mFlinger->mStateLock);
Lloyd Pique0b1fe702018-01-22 18:03:16 -0800312 return mFlinger->handleTransactionLocked(transactionFlags);
313 }
Lloyd Piquef58625d2017-12-19 13:22:33 -0800314
Peiyong Line9d809e2020-04-14 13:10:48 -0700315 auto onHotplugReceived(int32_t sequenceId, hal::HWDisplayId display,
316 hal::Connection connection) {
Lloyd Pique6cf11032018-01-22 18:57:44 -0800317 return mFlinger->onHotplugReceived(sequenceId, display, connection);
318 }
319
Vishnu Nairce5d0cc2019-02-28 14:38:41 -0800320 auto setDisplayStateLocked(const DisplayState& s) {
321 Mutex::Autolock _l(mFlinger->mStateLock);
322 return mFlinger->setDisplayStateLocked(s);
323 }
Lloyd Pique9d9cf402018-02-16 17:47:13 -0800324
Dominik Laskowski83b88212018-12-11 13:34:06 -0800325 // Allow reading display state without locking, as if called on the SF main thread.
326 auto onInitializeDisplays() NO_THREAD_SAFETY_ANALYSIS {
327 return mFlinger->onInitializeDisplays();
328 }
Lloyd Pique86016da2018-03-01 16:09:38 -0800329
Lais Andrade3a6e47d2020-04-02 11:20:16 +0100330 auto notifyPowerBoost(int32_t boostId) { return mFlinger->notifyPowerBoost(boostId); }
331
Dominik Laskowski83b88212018-12-11 13:34:06 -0800332 // Allow reading display state without locking, as if called on the SF main thread.
333 auto setPowerModeInternal(const sp<DisplayDevice>& display,
Peiyong Lin65248e02020-04-18 21:15:07 -0700334 hal::PowerMode mode) NO_THREAD_SAFETY_ANALYSIS {
Dominik Laskowskie9774092018-12-11 10:04:24 -0800335 return mFlinger->setPowerModeInternal(display, mode);
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -0800336 }
337
Adithya Srinivasan5f683cf2020-09-15 14:21:04 -0700338 auto onMessageReceived(int32_t what) {
339 return mFlinger->onMessageReceived(what, /*vsyncId=*/0, systemTime());
340 }
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700341
chaviw426b5c02020-07-27 11:20:15 -0700342 auto renderScreenImplLocked(const RenderArea& renderArea,
343 SurfaceFlinger::TraverseLayersFunction traverseLayers,
chaviwc6ad8af2020-08-03 11:33:30 -0700344 const sp<GraphicBuffer>& buffer, bool forSystem, int* outSyncFd,
345 bool regionSampling) {
chaviw426b5c02020-07-27 11:20:15 -0700346 ScreenCaptureResults captureResults;
chaviwc6ad8af2020-08-03 11:33:30 -0700347 return mFlinger->renderScreenImplLocked(renderArea, traverseLayers, buffer, forSystem,
348 outSyncFd, regionSampling, captureResults);
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700349 }
350
chaviw4b9d5e12020-08-04 18:30:35 -0700351 auto traverseLayersInLayerStack(ui::LayerStack layerStack, int32_t uid,
Marin Shalamanov1c434292020-06-12 01:47:29 +0200352 const LayerVector::Visitor& visitor) {
chaviw4b9d5e12020-08-04 18:30:35 -0700353 return mFlinger->SurfaceFlinger::traverseLayersInLayerStack(layerStack, uid, visitor);
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700354 }
355
Daniel Solomon42d04562019-01-20 21:03:19 -0800356 auto getDisplayNativePrimaries(const sp<IBinder>& displayToken,
357 ui::DisplayPrimaries &primaries) {
358 return mFlinger->SurfaceFlinger::getDisplayNativePrimaries(displayToken, primaries);
359 }
360
Arthur Hung1bedccb2020-09-24 10:09:25 +0000361 auto& getTransactionQueue() { return mFlinger->mTransactionQueues; }
Valerie Haud251afb2019-03-29 14:19:02 -0700362
Ady Abraham22c7b5c2020-09-22 19:33:40 -0700363 auto setTransactionState(int64_t frameTimelineVsyncId, const Vector<ComposerState>& states,
Valerie Haud251afb2019-03-29 14:19:02 -0700364 const Vector<DisplayState>& displays, uint32_t flags,
365 const sp<IBinder>& applyToken,
366 const InputWindowCommands& inputWindowCommands,
367 int64_t desiredPresentTime, const client_cache_t& uncacheBuffer,
368 bool hasListenerCallbacks,
Pablo Gamito7eb7ee72020-08-05 10:57:05 +0000369 std::vector<ListenerCallbacks>& listenerCallbacks,
370 uint64_t transactionId) {
Ady Abraham22c7b5c2020-09-22 19:33:40 -0700371 return mFlinger->setTransactionState(frameTimelineVsyncId, states, displays, flags,
372 applyToken, inputWindowCommands, desiredPresentTime,
373 uncacheBuffer, hasListenerCallbacks, listenerCallbacks,
Pablo Gamito7eb7ee72020-08-05 10:57:05 +0000374 transactionId);
Valerie Haud251afb2019-03-29 14:19:02 -0700375 }
376
Arthur Hung1bedccb2020-09-24 10:09:25 +0000377 auto flushTransactionQueues() { return mFlinger->flushTransactionQueues(); };
Valerie Haud251afb2019-03-29 14:19:02 -0700378
Eduardo Rochaac867192020-09-14 20:12:17 +0000379 auto onTransact(uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) {
380 return mFlinger->onTransact(code, data, reply, flags);
381 }
382
Lloyd Pique86016da2018-03-01 16:09:38 -0800383 /* ------------------------------------------------------------------------
384 * Read-only access to private data to assert post-conditions.
385 */
386
387 const auto& getAnimFrameTracker() const { return mFlinger->mAnimFrameTracker; }
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -0800388 const auto& getHasPoweredOff() const { return mFlinger->mHasPoweredOff; }
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -0800389 const auto& getVisibleRegionsDirty() const { return mFlinger->mVisibleRegionsDirty; }
Lloyd Pique441d5042018-10-18 16:49:51 -0700390 auto& getHwComposer() const {
391 return static_cast<impl::HWComposer&>(mFlinger->getHwComposer());
392 }
Lloyd Pique9370a482019-10-03 17:58:30 -0700393 auto& getCompositionEngine() const { return mFlinger->getCompositionEngine(); }
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -0800394
Lloyd Pique86016da2018-03-01 16:09:38 -0800395 const auto& getCompositorTiming() const { return mFlinger->getBE().mCompositorTiming; }
396
Lloyd Piquef58625d2017-12-19 13:22:33 -0800397 /* ------------------------------------------------------------------------
398 * Read-write access to private data to set up preconditions and assert
399 * post-conditions.
400 */
Lloyd Pique0b1fe702018-01-22 18:03:16 -0800401
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800402 auto& mutableHasWideColorDisplay() { return SurfaceFlinger::hasWideColorDisplay; }
Peiyong Lin13effd12018-07-24 17:01:47 -0700403 auto& mutableUseColorManagement() { return SurfaceFlinger::useColorManagement; }
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800404
Lloyd Piquee39cad22017-12-20 17:01:29 -0800405 auto& mutableCurrentState() { return mFlinger->mCurrentState; }
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800406 auto& mutableDisplayColorSetting() { return mFlinger->mDisplayColorSetting; }
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700407 auto& mutableDisplays() { return mFlinger->mDisplays; }
Lloyd Piquee39cad22017-12-20 17:01:29 -0800408 auto& mutableDrawingState() { return mFlinger->mDrawingState; }
Lloyd Piquee39cad22017-12-20 17:01:29 -0800409 auto& mutableEventQueue() { return mFlinger->mEventQueue; }
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700410 auto& mutableGeometryInvalid() { return mFlinger->mGeometryInvalid; }
Lloyd Pique0b1fe702018-01-22 18:03:16 -0800411 auto& mutableInterceptor() { return mFlinger->mInterceptor; }
Lloyd Pique6cf11032018-01-22 18:57:44 -0800412 auto& mutableMainThreadId() { return mFlinger->mMainThreadId; }
Lloyd Pique0b1fe702018-01-22 18:03:16 -0800413 auto& mutablePendingHotplugEvents() { return mFlinger->mPendingHotplugEvents; }
Dominik Laskowski075d3172018-05-24 15:50:06 -0700414 auto& mutablePhysicalDisplayTokens() { return mFlinger->mPhysicalDisplayTokens; }
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700415 auto& mutableTexturePool() { return mFlinger->mTexturePool; }
Lloyd Pique0b1fe702018-01-22 18:03:16 -0800416 auto& mutableTransactionFlags() { return mFlinger->mTransactionFlags; }
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800417 auto& mutableUseHwcVirtualDisplays() { return mFlinger->mUseHwcVirtualDisplays; }
Peiyong Lin74ca2f42019-01-14 19:36:57 -0800418 auto& mutablePowerAdvisor() { return mFlinger->mPowerAdvisor; }
Lloyd Pique4fe29402019-08-12 16:51:24 -0700419 auto& mutableDebugDisableHWC() { return mFlinger->mDebugDisableHWC; }
Lloyd Piquee39cad22017-12-20 17:01:29 -0800420
Lloyd Pique6cf11032018-01-22 18:57:44 -0800421 auto& mutableComposerSequenceId() { return mFlinger->getBE().mComposerSequenceId; }
Lloyd Pique441d5042018-10-18 16:49:51 -0700422 auto& mutableHwcDisplayData() { return getHwComposer().mDisplayData; }
423 auto& mutableHwcPhysicalDisplayIdMap() { return getHwComposer().mPhysicalDisplayIdMap; }
424 auto& mutableInternalHwcDisplayId() { return getHwComposer().mInternalHwcDisplayId; }
425 auto& mutableExternalHwcDisplayId() { return getHwComposer().mExternalHwcDisplayId; }
Ady Abraham60e42ea2020-03-09 19:17:31 -0700426 auto& mutableUseFrameRateApi() { return mFlinger->useFrameRateApi; }
Lloyd Piquea618d852018-01-17 11:52:30 -0800427
Valerie Hau09e60052019-12-15 14:51:15 -0800428 auto fromHandle(const sp<IBinder>& handle) {
Valerie Hau09e60052019-12-15 14:51:15 -0800429 return mFlinger->fromHandle(handle);
430 }
431
Lloyd Piquee39cad22017-12-20 17:01:29 -0800432 ~TestableSurfaceFlinger() {
433 // All these pointer and container clears help ensure that GMock does
434 // not report a leaked object, since the SurfaceFlinger instance may
435 // still be referenced by something despite our best efforts to destroy
436 // it after each test is done.
437 mutableDisplays().clear();
Ady Abraham2939f092019-03-04 17:44:06 -0800438 mutableCurrentState().displays.clear();
439 mutableDrawingState().displays.clear();
Lloyd Pique0b1fe702018-01-22 18:03:16 -0800440 mutableEventQueue().reset();
Pablo Gamito5cfc6e52020-09-10 11:18:03 +0000441 mutableInterceptor().clear();
Lloyd Pique2eb71eb2019-08-13 13:23:03 -0700442 mFlinger->mScheduler.reset();
Lloyd Pique441d5042018-10-18 16:49:51 -0700443 mFlinger->mCompositionEngine->setHwComposer(std::unique_ptr<HWComposer>());
Lloyd Piqueb97e04f2018-10-18 17:07:05 -0700444 mFlinger->mCompositionEngine->setRenderEngine(
445 std::unique_ptr<renderengine::RenderEngine>());
Lloyd Piquee39cad22017-12-20 17:01:29 -0800446 }
Lloyd Piquef58625d2017-12-19 13:22:33 -0800447
Lloyd Piquea618d852018-01-17 11:52:30 -0800448 /* ------------------------------------------------------------------------
449 * Wrapper classes for Read-write access to private data to set up
450 * preconditions and assert post-conditions.
451 */
Ana Krulec4593b692019-01-11 22:07:25 -0800452 struct HWC2Display : public HWC2::impl::Display {
Peiyong Lin74ca2f42019-01-14 19:36:57 -0800453 HWC2Display(Hwc2::Composer& composer,
Peiyong Line9d809e2020-04-14 13:10:48 -0700454 const std::unordered_set<hal::Capability>& capabilities, hal::HWDisplayId id,
455 hal::DisplayType type)
Peiyong Lin74ca2f42019-01-14 19:36:57 -0800456 : HWC2::impl::Display(composer, capabilities, id, type) {}
Lloyd Piquea618d852018-01-17 11:52:30 -0800457 ~HWC2Display() {
458 // Prevents a call to disable vsyncs.
Peiyong Line9d809e2020-04-14 13:10:48 -0700459 mType = hal::DisplayType::INVALID;
Lloyd Piquea618d852018-01-17 11:52:30 -0800460 }
461
462 auto& mutableIsConnected() { return this->mIsConnected; }
463 auto& mutableConfigs() { return this->mConfigs; }
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700464 auto& mutableLayers() { return this->mLayers; }
Lloyd Piquea618d852018-01-17 11:52:30 -0800465 };
466
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800467 class FakeHwcDisplayInjector {
468 public:
Peiyong Line9d809e2020-04-14 13:10:48 -0700469 static constexpr hal::HWDisplayId DEFAULT_HWC_DISPLAY_ID = 1000;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800470 static constexpr int32_t DEFAULT_WIDTH = 1920;
471 static constexpr int32_t DEFAULT_HEIGHT = 1280;
472 static constexpr int32_t DEFAULT_REFRESH_RATE = 16'666'666;
Ady Abraham2139f732019-11-13 18:56:40 -0800473 static constexpr int32_t DEFAULT_CONFIG_GROUP = 7;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800474 static constexpr int32_t DEFAULT_DPI = 320;
Peiyong Line9d809e2020-04-14 13:10:48 -0700475 static constexpr hal::HWConfigId DEFAULT_ACTIVE_CONFIG = 0;
Peiyong Lin65248e02020-04-18 21:15:07 -0700476 static constexpr hal::PowerMode DEFAULT_POWER_MODE = hal::PowerMode::ON;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800477
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200478 FakeHwcDisplayInjector(HalDisplayId displayId, hal::DisplayType hwcDisplayType,
479 bool isPrimary)
Dominik Laskowski075d3172018-05-24 15:50:06 -0700480 : mDisplayId(displayId), mHwcDisplayType(hwcDisplayType), mIsPrimary(isPrimary) {}
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800481
Peiyong Line9d809e2020-04-14 13:10:48 -0700482 auto& setHwcDisplayId(hal::HWDisplayId displayId) {
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800483 mHwcDisplayId = displayId;
484 return *this;
485 }
486
487 auto& setWidth(int32_t width) {
488 mWidth = width;
489 return *this;
490 }
491
492 auto& setHeight(int32_t height) {
493 mHeight = height;
494 return *this;
495 }
496
Ady Abraham8a82ba62020-01-17 12:43:17 -0800497 auto& setRefreshRate(int32_t refreshRate) {
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800498 mRefreshRate = refreshRate;
499 return *this;
500 }
501
502 auto& setDpiX(int32_t dpi) {
503 mDpiX = dpi;
504 return *this;
505 }
506
507 auto& setDpiY(int32_t dpi) {
508 mDpiY = dpi;
509 return *this;
510 }
511
Peiyong Line9d809e2020-04-14 13:10:48 -0700512 auto& setActiveConfig(hal::HWConfigId config) {
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800513 mActiveConfig = config;
514 return *this;
515 }
516
Peiyong Line9d809e2020-04-14 13:10:48 -0700517 auto& setCapabilities(const std::unordered_set<hal::Capability>* capabilities) {
Lloyd Piquee22f0332018-07-16 16:35:56 -0700518 mCapabilities = capabilities;
519 return *this;
520 }
521
Peiyong Lin65248e02020-04-18 21:15:07 -0700522 auto& setPowerMode(hal::PowerMode mode) {
Peiyong Lin1336e6e2019-05-28 09:23:50 -0700523 mPowerMode = mode;
524 return *this;
525 }
526
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800527 void inject(TestableSurfaceFlinger* flinger, Hwc2::Composer* composer) {
Peiyong Line9d809e2020-04-14 13:10:48 -0700528 static const std::unordered_set<hal::Capability> defaultCapabilities;
Lloyd Piquee22f0332018-07-16 16:35:56 -0700529 if (mCapabilities == nullptr) mCapabilities = &defaultCapabilities;
530
531 // Caution - Make sure that any values passed by reference here do
532 // not refer to an instance owned by FakeHwcDisplayInjector. This
533 // class has temporary lifetime, while the constructed HWC2::Display
534 // is much longer lived.
Peiyong Lin74ca2f42019-01-14 19:36:57 -0800535 auto display = std::make_unique<HWC2Display>(*composer, *mCapabilities, mHwcDisplayId,
536 mHwcDisplayType);
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800537
538 auto config = HWC2::Display::Config::Builder(*display, mActiveConfig);
539 config.setWidth(mWidth);
540 config.setHeight(mHeight);
541 config.setVsyncPeriod(mRefreshRate);
542 config.setDpiX(mDpiX);
543 config.setDpiY(mDpiY);
Ady Abraham2139f732019-11-13 18:56:40 -0800544 config.setConfigGroup(mConfigGroup);
Ady Abraham8a82ba62020-01-17 12:43:17 -0800545 display->mutableConfigs().emplace(static_cast<int32_t>(mActiveConfig), config.build());
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800546 display->mutableIsConnected() = true;
Peiyong Lin65248e02020-04-18 21:15:07 -0700547 display->setPowerMode(mPowerMode);
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800548
Peiyong Linbdd08cc2019-12-17 21:35:14 -0800549 flinger->mutableHwcDisplayData()[mDisplayId].hwcDisplay = std::move(display);
Dominik Laskowski075d3172018-05-24 15:50:06 -0700550
Peiyong Line9d809e2020-04-14 13:10:48 -0700551 if (mHwcDisplayType == hal::DisplayType::PHYSICAL) {
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200552 const auto physicalId = PhysicalDisplayId::tryCast(mDisplayId);
553 LOG_ALWAYS_FATAL_IF(!physicalId);
554 flinger->mutableHwcPhysicalDisplayIdMap().emplace(mHwcDisplayId, *physicalId);
Dominik Laskowski075d3172018-05-24 15:50:06 -0700555 (mIsPrimary ? flinger->mutableInternalHwcDisplayId()
556 : flinger->mutableExternalHwcDisplayId()) = mHwcDisplayId;
557 }
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800558 }
559
560 private:
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200561 const HalDisplayId mDisplayId;
Peiyong Line9d809e2020-04-14 13:10:48 -0700562 const hal::DisplayType mHwcDisplayType;
Dominik Laskowski075d3172018-05-24 15:50:06 -0700563 const bool mIsPrimary;
564
Peiyong Line9d809e2020-04-14 13:10:48 -0700565 hal::HWDisplayId mHwcDisplayId = DEFAULT_HWC_DISPLAY_ID;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800566 int32_t mWidth = DEFAULT_WIDTH;
567 int32_t mHeight = DEFAULT_HEIGHT;
Ady Abraham8a82ba62020-01-17 12:43:17 -0800568 int32_t mRefreshRate = DEFAULT_REFRESH_RATE;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800569 int32_t mDpiX = DEFAULT_DPI;
Ady Abraham2139f732019-11-13 18:56:40 -0800570 int32_t mConfigGroup = DEFAULT_CONFIG_GROUP;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800571 int32_t mDpiY = DEFAULT_DPI;
Peiyong Line9d809e2020-04-14 13:10:48 -0700572 hal::HWConfigId mActiveConfig = DEFAULT_ACTIVE_CONFIG;
Peiyong Lin65248e02020-04-18 21:15:07 -0700573 hal::PowerMode mPowerMode = DEFAULT_POWER_MODE;
Peiyong Line9d809e2020-04-14 13:10:48 -0700574 const std::unordered_set<hal::Capability>* mCapabilities = nullptr;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800575 };
576
577 class FakeDisplayDeviceInjector {
578 public:
Dominik Laskowski075d3172018-05-24 15:50:06 -0700579 FakeDisplayDeviceInjector(TestableSurfaceFlinger& flinger,
Lloyd Pique9370a482019-10-03 17:58:30 -0700580 std::shared_ptr<compositionengine::Display> compositionDisplay,
Dominik Laskowski55c85402020-01-21 16:25:47 -0800581 std::optional<DisplayConnectionType> connectionType,
Peiyong Line9d809e2020-04-14 13:10:48 -0700582 std::optional<hal::HWDisplayId> hwcDisplayId, bool isPrimary)
Lloyd Pique9370a482019-10-03 17:58:30 -0700583 : mFlinger(flinger),
Marin Shalamanov700e6392020-02-12 20:22:26 +0100584 mCreationArgs(flinger.mFlinger.get(), mDisplayToken, compositionDisplay),
585 mHwcDisplayId(hwcDisplayId) {
Dominik Laskowski55c85402020-01-21 16:25:47 -0800586 mCreationArgs.connectionType = connectionType;
Dominik Laskowski075d3172018-05-24 15:50:06 -0700587 mCreationArgs.isPrimary = isPrimary;
588 }
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800589
590 sp<IBinder> token() const { return mDisplayToken; }
591
592 DisplayDeviceState& mutableDrawingDisplayState() {
593 return mFlinger.mutableDrawingState().displays.editValueFor(mDisplayToken);
594 }
595
596 DisplayDeviceState& mutableCurrentDisplayState() {
597 return mFlinger.mutableCurrentState().displays.editValueFor(mDisplayToken);
598 }
599
Lloyd Pique9d9cf402018-02-16 17:47:13 -0800600 const auto& getDrawingDisplayState() {
601 return mFlinger.mutableDrawingState().displays.valueFor(mDisplayToken);
602 }
603
604 const auto& getCurrentDisplayState() {
605 return mFlinger.mutableCurrentState().displays.valueFor(mDisplayToken);
606 }
607
Dominik Laskowski9fae1022018-05-29 13:17:40 -0700608 auto& mutableDisplayDevice() { return mFlinger.mutableDisplays()[mDisplayToken]; }
Lloyd Pique9d9cf402018-02-16 17:47:13 -0800609
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800610 auto& setNativeWindow(const sp<ANativeWindow>& nativeWindow) {
Lloyd Pique2eef1d22018-09-18 21:30:04 -0700611 mCreationArgs.nativeWindow = nativeWindow;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800612 return *this;
613 }
614
Lloyd Pique542307f2018-10-19 13:24:08 -0700615 auto& setDisplaySurface(const sp<compositionengine::DisplaySurface>& displaySurface) {
Lloyd Pique2eef1d22018-09-18 21:30:04 -0700616 mCreationArgs.displaySurface = displaySurface;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800617 return *this;
618 }
619
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800620 auto& setSecure(bool secure) {
Lloyd Pique2eef1d22018-09-18 21:30:04 -0700621 mCreationArgs.isSecure = secure;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800622 return *this;
623 }
624
Peiyong Lin65248e02020-04-18 21:15:07 -0700625 auto& setPowerMode(hal::PowerMode mode) {
Lloyd Pique2eef1d22018-09-18 21:30:04 -0700626 mCreationArgs.initialPowerMode = mode;
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700627 return *this;
628 }
629
Valerie Hau9758ae02018-10-09 16:05:09 -0700630 auto& setHwcColorModes(
631 const std::unordered_map<ui::ColorMode, std::vector<ui::RenderIntent>>
632 hwcColorModes) {
633 mCreationArgs.hwcColorModes = hwcColorModes;
634 return *this;
635 }
636
637 auto& setHasWideColorGamut(bool hasWideColorGamut) {
638 mCreationArgs.hasWideColorGamut = hasWideColorGamut;
639 return *this;
640 }
641
Lloyd Pique33050472019-12-19 17:12:44 -0800642 auto& setPhysicalOrientation(ui::Rotation orientation) {
643 mCreationArgs.physicalOrientation = orientation;
644 return *this;
645 }
646
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800647 sp<DisplayDevice> inject() {
Lloyd Pique9370a482019-10-03 17:58:30 -0700648 const auto displayId = mCreationArgs.compositionDisplay->getDisplayId();
649
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800650 DisplayDeviceState state;
Dominik Laskowski55c85402020-01-21 16:25:47 -0800651 if (const auto type = mCreationArgs.connectionType) {
Lloyd Pique9370a482019-10-03 17:58:30 -0700652 LOG_ALWAYS_FATAL_IF(!displayId);
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200653 const auto physicalId = PhysicalDisplayId::tryCast(*displayId);
654 LOG_ALWAYS_FATAL_IF(!physicalId);
Marin Shalamanov700e6392020-02-12 20:22:26 +0100655 LOG_ALWAYS_FATAL_IF(!mHwcDisplayId);
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200656 state.physical = {.id = *physicalId, .type = *type, .hwcDisplayId = *mHwcDisplayId};
Dominik Laskowski55c85402020-01-21 16:25:47 -0800657 }
658
Lloyd Pique2eef1d22018-09-18 21:30:04 -0700659 state.isSecure = mCreationArgs.isSecure;
660
Lloyd Pique9370a482019-10-03 17:58:30 -0700661 sp<DisplayDevice> device = new DisplayDevice(mCreationArgs);
Lloyd Pique2eef1d22018-09-18 21:30:04 -0700662 mFlinger.mutableDisplays().emplace(mDisplayToken, device);
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800663 mFlinger.mutableCurrentState().displays.add(mDisplayToken, state);
664 mFlinger.mutableDrawingState().displays.add(mDisplayToken, state);
665
Dominik Laskowski55c85402020-01-21 16:25:47 -0800666 if (const auto& physical = state.physical) {
667 mFlinger.mutablePhysicalDisplayTokens()[physical->id] = mDisplayToken;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800668 }
669
670 return device;
671 }
672
673 private:
674 TestableSurfaceFlinger& mFlinger;
675 sp<BBinder> mDisplayToken = new BBinder();
Lloyd Pique2eef1d22018-09-18 21:30:04 -0700676 DisplayDeviceCreationArgs mCreationArgs;
Peiyong Line9d809e2020-04-14 13:10:48 -0700677 const std::optional<hal::HWDisplayId> mHwcDisplayId;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800678 };
679
Dominik Laskowski8b01cc02020-07-14 19:02:41 -0700680private:
681 void setVsyncEnabled(bool) override {}
682 void changeRefreshRate(const Scheduler::RefreshRate&, Scheduler::ConfigEvent) override {}
683 void repaintEverythingForHWC() override {}
684 void kernelTimerChanged(bool) override {}
685
Lloyd Pique90c115d2018-09-18 21:39:42 -0700686 surfaceflinger::test::Factory mFactory;
687 sp<SurfaceFlinger> mFlinger = new SurfaceFlinger(mFactory, SurfaceFlinger::SkipInitialization);
Dominik Laskowski7c9dbf92019-08-01 17:57:31 -0700688 TestableScheduler* mScheduler = nullptr;
Ady Abrahamabc27602020-04-08 17:20:29 -0700689 Hwc2::mock::Display mDisplay;
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200690 mock::DisplayIdGenerator<GpuVirtualDisplayId> mGpuVirtualDisplayIdGenerator;
Lloyd Piquef58625d2017-12-19 13:22:33 -0800691};
692
693} // namespace android