blob: 6ce738a89f63cdcc9b2aa9eedd286a7e75408577 [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"
Lloyd Piqued6b579f2018-04-06 15:29:10 -070033#include "Layer.h"
Lloyd Pique90c115d2018-09-18 21:39:42 -070034#include "NativeWindowSurface.h"
Dominik Laskowski9dab3432019-03-27 13:21:10 -070035#include "Scheduler/MessageQueue.h"
Steven Thomas2bbaabe2019-08-28 16:08:35 -070036#include "Scheduler/RefreshRateConfigs.h"
Lloyd Pique90c115d2018-09-18 21:39:42 -070037#include "StartPropertySetThread.h"
Lloyd Piquef58625d2017-12-19 13:22:33 -080038#include "SurfaceFlinger.h"
Lloyd Pique24f3bfe2019-10-02 19:29:10 -070039#include "SurfaceFlingerDefaultFactory.h"
Lloyd Pique90c115d2018-09-18 21:39:42 -070040#include "SurfaceInterceptor.h"
Dominik Laskowski7c9dbf92019-08-01 17:57:31 -070041#include "TestableScheduler.h"
Ady Abrahamabc27602020-04-08 17:20:29 -070042#include "mock/DisplayHardware/MockDisplay.h"
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +020043#include "mock/MockDisplayIdGenerator.h"
Yiwei Zhang7e666a52018-11-15 13:33:42 -080044
Lloyd Piquef58625d2017-12-19 13:22:33 -080045namespace android {
46
Lloyd Piquee39cad22017-12-20 17:01:29 -080047class EventThread;
48
Peiyong Lin833074a2018-08-28 11:53:54 -070049namespace renderengine {
Lloyd Pique90c115d2018-09-18 21:39:42 -070050
Lloyd Piquee39cad22017-12-20 17:01:29 -080051class RenderEngine;
Lloyd Pique90c115d2018-09-18 21:39:42 -070052
53} // namespace renderengine
Lloyd Piquee39cad22017-12-20 17:01:29 -080054
55namespace Hwc2 {
Lloyd Pique90c115d2018-09-18 21:39:42 -070056
Lloyd Piquee39cad22017-12-20 17:01:29 -080057class Composer;
Lloyd Pique90c115d2018-09-18 21:39:42 -070058
59} // namespace Hwc2
60
Peiyong Line9d809e2020-04-14 13:10:48 -070061namespace hal = android::hardware::graphics::composer::hal;
62
Lloyd Pique90c115d2018-09-18 21:39:42 -070063namespace surfaceflinger::test {
64
65class Factory final : public surfaceflinger::Factory {
66public:
67 ~Factory() = default;
68
Lloyd Pique90c115d2018-09-18 21:39:42 -070069 std::unique_ptr<HWComposer> createHWComposer(const std::string&) override {
Lloyd Pique90c115d2018-09-18 21:39:42 -070070 return nullptr;
71 }
72
73 std::unique_ptr<MessageQueue> createMessageQueue() override {
Lloyd Pique90c115d2018-09-18 21:39:42 -070074 return std::make_unique<android::impl::MessageQueue>();
75 }
76
Ady Abraham8287e852020-08-12 14:44:58 -070077 std::unique_ptr<scheduler::VsyncConfiguration> createVsyncConfiguration(
Ady Abraham9e16a482019-12-03 17:19:41 -080078 const scheduler::RefreshRateConfigs& /*refreshRateConfigs*/) override {
Ana Krulec757f63a2019-01-25 10:46:18 -080079 return std::make_unique<scheduler::FakePhaseOffsets>();
80 }
81
Dominik Laskowski8b01cc02020-07-14 19:02:41 -070082 std::unique_ptr<Scheduler> createScheduler(const scheduler::RefreshRateConfigs&,
Ady Abraham3a77a7b2019-12-02 18:46:59 -080083 ISchedulerCallback&) override {
Lloyd Pique90c115d2018-09-18 21:39:42 -070084 return nullptr;
85 }
86
Pablo Gamitoc351d6f2020-09-17 15:34:26 +000087 sp<SurfaceInterceptor> createSurfaceInterceptor() override {
88 return new android::impl::SurfaceInterceptor();
Lloyd Pique90c115d2018-09-18 21:39:42 -070089 }
90
91 sp<StartPropertySetThread> createStartPropertySetThread(bool timestampPropertyValue) override {
Lloyd Pique90c115d2018-09-18 21:39:42 -070092 return new StartPropertySetThread(timestampPropertyValue);
93 }
94
Lloyd Pique9370a482019-10-03 17:58:30 -070095 sp<DisplayDevice> createDisplayDevice(DisplayDeviceCreationArgs& creationArgs) override {
96 return new DisplayDevice(creationArgs);
Lloyd Pique90c115d2018-09-18 21:39:42 -070097 }
98
99 sp<GraphicBuffer> createGraphicBuffer(uint32_t width, uint32_t height, PixelFormat format,
100 uint32_t layerCount, uint64_t usage,
101 std::string requestorName) override {
Lloyd Pique90c115d2018-09-18 21:39:42 -0700102 return new GraphicBuffer(width, height, format, layerCount, usage, requestorName);
103 }
104
105 void createBufferQueue(sp<IGraphicBufferProducer>* outProducer,
106 sp<IGraphicBufferConsumer>* outConsumer,
107 bool consumerIsSurfaceFlinger) override {
Lloyd Piquee300b312019-10-03 13:03:45 -0700108 if (!mCreateBufferQueue) {
109 BufferQueue::createBufferQueue(outProducer, outConsumer, consumerIsSurfaceFlinger);
110 return;
111 }
Lloyd Pique90c115d2018-09-18 21:39:42 -0700112 mCreateBufferQueue(outProducer, outConsumer, consumerIsSurfaceFlinger);
113 }
114
Lloyd Piquee300b312019-10-03 13:03:45 -0700115 sp<IGraphicBufferProducer> createMonitoredProducer(const sp<IGraphicBufferProducer>& producer,
116 const sp<SurfaceFlinger>& flinger,
117 const wp<Layer>& layer) override {
118 return new MonitoredProducer(producer, flinger, layer);
119 }
120
121 sp<BufferLayerConsumer> createBufferLayerConsumer(const sp<IGraphicBufferConsumer>& consumer,
122 renderengine::RenderEngine& renderEngine,
123 uint32_t textureName, Layer* layer) override {
124 return new BufferLayerConsumer(consumer, renderEngine, textureName, layer);
125 }
126
Lloyd Pique90c115d2018-09-18 21:39:42 -0700127 std::unique_ptr<surfaceflinger::NativeWindowSurface> createNativeWindowSurface(
128 const sp<IGraphicBufferProducer>& producer) override {
129 if (!mCreateNativeWindowSurface) return nullptr;
130 return mCreateNativeWindowSurface(producer);
131 }
132
Lloyd Pique70d91362018-10-18 16:02:55 -0700133 std::unique_ptr<compositionengine::CompositionEngine> createCompositionEngine() override {
134 return compositionengine::impl::createCompositionEngine();
135 }
136
Lloyd Pique90c115d2018-09-18 21:39:42 -0700137 sp<BufferQueueLayer> createBufferQueueLayer(const LayerCreationArgs&) override {
Lloyd Pique90c115d2018-09-18 21:39:42 -0700138 return nullptr;
139 }
140
141 sp<BufferStateLayer> createBufferStateLayer(const LayerCreationArgs&) override {
Lloyd Pique90c115d2018-09-18 21:39:42 -0700142 return nullptr;
143 }
144
Vishnu Nairfa247b12020-02-11 08:58:26 -0800145 sp<EffectLayer> createEffectLayer(const LayerCreationArgs&) override { return nullptr; }
Lloyd Pique90c115d2018-09-18 21:39:42 -0700146
147 sp<ContainerLayer> createContainerLayer(const LayerCreationArgs&) override {
Lloyd Pique90c115d2018-09-18 21:39:42 -0700148 return nullptr;
149 }
150
151 using CreateBufferQueueFunction =
152 std::function<void(sp<IGraphicBufferProducer>* /* outProducer */,
153 sp<IGraphicBufferConsumer>* /* outConsumer */,
154 bool /* consumerIsSurfaceFlinger */)>;
155 CreateBufferQueueFunction mCreateBufferQueue;
156
157 using CreateNativeWindowSurfaceFunction =
158 std::function<std::unique_ptr<surfaceflinger::NativeWindowSurface>(
159 const sp<IGraphicBufferProducer>&)>;
160 CreateNativeWindowSurfaceFunction mCreateNativeWindowSurface;
Lloyd Pique70d91362018-10-18 16:02:55 -0700161
162 using CreateCompositionEngineFunction =
163 std::function<std::unique_ptr<compositionengine::CompositionEngine>()>;
164 CreateCompositionEngineFunction mCreateCompositionEngine;
Lloyd Pique90c115d2018-09-18 21:39:42 -0700165};
166
167} // namespace surfaceflinger::test
Lloyd Piquee39cad22017-12-20 17:01:29 -0800168
Dominik Laskowski8b01cc02020-07-14 19:02:41 -0700169class TestableSurfaceFlinger final : private ISchedulerCallback {
Lloyd Piquef58625d2017-12-19 13:22:33 -0800170public:
Dominik Laskowskib7251f42020-04-20 17:42:59 -0700171 using HotplugEvent = SurfaceFlinger::HotplugEvent;
172
Dominik Laskowskif7a09ed2019-10-07 13:54:18 -0700173 SurfaceFlinger* flinger() { return mFlinger.get(); }
Dominik Laskowski7c9dbf92019-08-01 17:57:31 -0700174 TestableScheduler* scheduler() { return mScheduler; }
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200175 mock::DisplayIdGenerator<GpuVirtualDisplayId>& gpuVirtualDisplayIdGenerator() {
176 return mGpuVirtualDisplayIdGenerator;
177 }
Dominik Laskowski7c9dbf92019-08-01 17:57:31 -0700178
Lloyd Piquef58625d2017-12-19 13:22:33 -0800179 // Extend this as needed for accessing SurfaceFlinger private (and public)
180 // functions.
181
Peiyong Lin833074a2018-08-28 11:53:54 -0700182 void setupRenderEngine(std::unique_ptr<renderengine::RenderEngine> renderEngine) {
Lloyd Piqueb97e04f2018-10-18 17:07:05 -0700183 mFlinger->mCompositionEngine->setRenderEngine(std::move(renderEngine));
Lloyd Piquee39cad22017-12-20 17:01:29 -0800184 }
185
186 void setupComposer(std::unique_ptr<Hwc2::Composer> composer) {
Lloyd Pique441d5042018-10-18 16:49:51 -0700187 mFlinger->mCompositionEngine->setHwComposer(
188 std::make_unique<impl::HWComposer>(std::move(composer)));
Lloyd Piquee39cad22017-12-20 17:01:29 -0800189 }
190
Alec Mourie4034bb2019-11-19 12:45:54 -0800191 void setupTimeStats(const std::shared_ptr<TimeStats>& timeStats) {
192 mFlinger->mCompositionEngine->setTimeStats(timeStats);
193 }
194
Dominik Laskowski8b01cc02020-07-14 19:02:41 -0700195 // The ISchedulerCallback argument can be nullptr for a no-op implementation.
Ady Abraham8cb21882020-08-26 18:22:05 -0700196 void setupScheduler(std::unique_ptr<scheduler::VsyncController> vsyncController,
197 std::unique_ptr<scheduler::VSyncTracker> vsyncTracker,
Dominik Laskowski7c9dbf92019-08-01 17:57:31 -0700198 std::unique_ptr<EventThread> appEventThread,
Ady Abraham8a82ba62020-01-17 12:43:17 -0800199 std::unique_ptr<EventThread> sfEventThread,
Dominik Laskowski8b01cc02020-07-14 19:02:41 -0700200 ISchedulerCallback* callback = nullptr, bool hasMultipleConfigs = false) {
Ady Abrahamabc27602020-04-08 17:20:29 -0700201 std::vector<std::shared_ptr<const HWC2::Display::Config>> configs{
202 HWC2::Display::Config::Builder(mDisplay, 0)
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700203 .setVsyncPeriod(16'666'667)
Ady Abrahamabc27602020-04-08 17:20:29 -0700204 .setConfigGroup(0)
205 .build()};
206
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700207 if (hasMultipleConfigs) {
208 configs.emplace_back(HWC2::Display::Config::Builder(mDisplay, 1)
209 .setVsyncPeriod(11'111'111)
210 .setConfigGroup(0)
211 .build());
212 }
213
Ady Abraham2139f732019-11-13 18:56:40 -0800214 mFlinger->mRefreshRateConfigs = std::make_unique<
Ana Krulec3f6a2062020-01-23 15:48:01 -0800215 scheduler::RefreshRateConfigs>(configs, /*currentConfig=*/HwcConfigIndexType(0));
Ady Abraham2139f732019-11-13 18:56:40 -0800216 mFlinger->mRefreshRateStats = std::make_unique<
217 scheduler::RefreshRateStats>(*mFlinger->mRefreshRateConfigs, *mFlinger->mTimeStats,
218 /*currentConfig=*/HwcConfigIndexType(0),
Peiyong Lin65248e02020-04-18 21:15:07 -0700219 /*powerMode=*/hal::PowerMode::OFF);
Ady Abraham8287e852020-08-12 14:44:58 -0700220 mFlinger->mVsyncConfiguration =
221 mFactory.createVsyncConfiguration(*mFlinger->mRefreshRateConfigs);
222 mFlinger->mVsyncModulator.emplace(mFlinger->mVsyncConfiguration->getCurrentConfigs());
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700223
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700224 constexpr bool kUseContentDetectionV2 = false;
Ady Abraham8cb21882020-08-26 18:22:05 -0700225 mScheduler = new TestableScheduler(std::move(vsyncController), std::move(vsyncTracker),
226 *mFlinger->mRefreshRateConfigs, *(callback ?: this),
227 kUseContentDetectionV2);
Dominik Laskowski7c9dbf92019-08-01 17:57:31 -0700228
Dominik Laskowski98041832019-08-01 18:35:59 -0700229 mFlinger->mAppConnectionHandle = mScheduler->createConnection(std::move(appEventThread));
230 mFlinger->mSfConnectionHandle = mScheduler->createConnection(std::move(sfEventThread));
Dominik Laskowskif7a09ed2019-10-07 13:54:18 -0700231 resetScheduler(mScheduler);
Dominik Laskowski7c9dbf92019-08-01 17:57:31 -0700232 }
233
Dominik Laskowskif7a09ed2019-10-07 13:54:18 -0700234 void resetScheduler(Scheduler* scheduler) { mFlinger->mScheduler.reset(scheduler); }
235
Lloyd Pique90c115d2018-09-18 21:39:42 -0700236 using CreateBufferQueueFunction = surfaceflinger::test::Factory::CreateBufferQueueFunction;
Lloyd Pique5b36f3f2018-01-17 11:57:07 -0800237 void setCreateBufferQueueFunction(CreateBufferQueueFunction f) {
Lloyd Pique90c115d2018-09-18 21:39:42 -0700238 mFactory.mCreateBufferQueue = f;
Lloyd Pique5b36f3f2018-01-17 11:57:07 -0800239 }
240
Lloyd Pique90c115d2018-09-18 21:39:42 -0700241 using CreateNativeWindowSurfaceFunction =
242 surfaceflinger::test::Factory::CreateNativeWindowSurfaceFunction;
Lloyd Pique0b1fe702018-01-22 18:03:16 -0800243 void setCreateNativeWindowSurface(CreateNativeWindowSurfaceFunction f) {
Lloyd Pique90c115d2018-09-18 21:39:42 -0700244 mFactory.mCreateNativeWindowSurface = f;
Lloyd Pique0b1fe702018-01-22 18:03:16 -0800245 }
246
Daniel Solomon42d04562019-01-20 21:03:19 -0800247 void setInternalDisplayPrimaries(const ui::DisplayPrimaries& primaries) {
248 memcpy(&mFlinger->mInternalDisplayPrimaries, &primaries, sizeof(ui::DisplayPrimaries));
249 }
250
Dominik Laskowskib7251f42020-04-20 17:42:59 -0700251 static auto& mutableLayerCurrentState(const sp<Layer>& layer) { return layer->mCurrentState; }
252 static auto& mutableLayerDrawingState(const sp<Layer>& layer) { return layer->mDrawingState; }
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700253
Valerie Haud251afb2019-03-29 14:19:02 -0700254 auto& mutableStateLock() { return mFlinger->mStateLock; }
255
Dominik Laskowskib7251f42020-04-20 17:42:59 -0700256 static auto findOutputLayerForDisplay(const sp<Layer>& layer,
257 const sp<const DisplayDevice>& display) {
258 return layer->findOutputLayerForDisplay(display.get());
259 }
260
261 static void setLayerSidebandStream(const sp<Layer>& layer,
262 const sp<NativeHandle>& sidebandStream) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800263 layer->mDrawingState.sidebandStream = sidebandStream;
Lloyd Pique0b785d82018-12-04 17:25:27 -0800264 layer->mSidebandStream = sidebandStream;
Lloyd Piquede196652020-01-22 17:29:58 -0800265 layer->editCompositionState()->sidebandStream = sidebandStream;
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700266 }
267
Dominik Laskowskib7251f42020-04-20 17:42:59 -0700268 void setLayerCompositionType(const sp<Layer>& layer, hal::Composition type) {
269 auto outputLayer = findOutputLayerForDisplay(layer, mFlinger->getDefaultDisplayDevice());
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800270 LOG_ALWAYS_FATAL_IF(!outputLayer);
271 auto& state = outputLayer->editState();
272 LOG_ALWAYS_FATAL_IF(!outputLayer->getState().hwc);
Peiyong Line9d809e2020-04-14 13:10:48 -0700273 (*state.hwc).hwcCompositionType = type;
Dominik Laskowskib7251f42020-04-20 17:42:59 -0700274 }
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800275
Dominik Laskowskib7251f42020-04-20 17:42:59 -0700276 static void setLayerPotentialCursor(const sp<Layer>& layer, bool potentialCursor) {
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700277 layer->mPotentialCursor = potentialCursor;
278 }
279
Garfield Tande619fa2020-10-02 17:13:53 -0700280 static void setLayerDrawingParent(const sp<Layer>& layer, const sp<Layer>& drawingParent) {
281 layer->mDrawingParent = drawingParent;
282 }
283
Lloyd Piquef58625d2017-12-19 13:22:33 -0800284 /* ------------------------------------------------------------------------
285 * Forwarding for functions being tested
286 */
Lloyd Pique0b1fe702018-01-22 18:03:16 -0800287
Lloyd Piquea482f992018-01-22 19:00:34 -0800288 auto createDisplay(const String8& displayName, bool secure) {
289 return mFlinger->createDisplay(displayName, secure);
290 }
291
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700292 auto destroyDisplay(const sp<IBinder>& displayToken) {
293 return mFlinger->destroyDisplay(displayToken);
294 }
Lloyd Piquea482f992018-01-22 19:00:34 -0800295
Lloyd Pique9370a482019-10-03 17:58:30 -0700296 auto setupNewDisplayDeviceInternal(
297 const wp<IBinder>& displayToken,
298 std::shared_ptr<compositionengine::Display> compositionDisplay,
299 const DisplayDeviceState& state,
300 const sp<compositionengine::DisplaySurface>& dispSurface,
Dominik Laskowski0a1435d2020-04-21 00:27:31 -0700301 const sp<IGraphicBufferProducer>& producer) NO_THREAD_SAFETY_ANALYSIS {
Lloyd Pique9370a482019-10-03 17:58:30 -0700302 return mFlinger->setupNewDisplayDeviceInternal(displayToken, compositionDisplay, state,
303 dispSurface, producer);
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800304 }
305
Lloyd Pique0b1fe702018-01-22 18:03:16 -0800306 auto handleTransactionLocked(uint32_t transactionFlags) {
Vishnu Nairfa5f8df2019-02-13 17:02:51 -0800307 Mutex::Autolock _l(mFlinger->mStateLock);
Lloyd Pique0b1fe702018-01-22 18:03:16 -0800308 return mFlinger->handleTransactionLocked(transactionFlags);
309 }
Lloyd Piquef58625d2017-12-19 13:22:33 -0800310
Peiyong Line9d809e2020-04-14 13:10:48 -0700311 auto onHotplugReceived(int32_t sequenceId, hal::HWDisplayId display,
312 hal::Connection connection) {
Lloyd Pique6cf11032018-01-22 18:57:44 -0800313 return mFlinger->onHotplugReceived(sequenceId, display, connection);
314 }
315
Vishnu Nairce5d0cc2019-02-28 14:38:41 -0800316 auto setDisplayStateLocked(const DisplayState& s) {
317 Mutex::Autolock _l(mFlinger->mStateLock);
318 return mFlinger->setDisplayStateLocked(s);
319 }
Lloyd Pique9d9cf402018-02-16 17:47:13 -0800320
Dominik Laskowski83b88212018-12-11 13:34:06 -0800321 // Allow reading display state without locking, as if called on the SF main thread.
322 auto onInitializeDisplays() NO_THREAD_SAFETY_ANALYSIS {
323 return mFlinger->onInitializeDisplays();
324 }
Lloyd Pique86016da2018-03-01 16:09:38 -0800325
Lais Andrade3a6e47d2020-04-02 11:20:16 +0100326 auto notifyPowerBoost(int32_t boostId) { return mFlinger->notifyPowerBoost(boostId); }
327
Dominik Laskowski83b88212018-12-11 13:34:06 -0800328 // Allow reading display state without locking, as if called on the SF main thread.
329 auto setPowerModeInternal(const sp<DisplayDevice>& display,
Peiyong Lin65248e02020-04-18 21:15:07 -0700330 hal::PowerMode mode) NO_THREAD_SAFETY_ANALYSIS {
Dominik Laskowskie9774092018-12-11 10:04:24 -0800331 return mFlinger->setPowerModeInternal(display, mode);
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -0800332 }
333
Adithya Srinivasan5f683cf2020-09-15 14:21:04 -0700334 auto onMessageReceived(int32_t what) {
335 return mFlinger->onMessageReceived(what, /*vsyncId=*/0, systemTime());
336 }
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700337
chaviw426b5c02020-07-27 11:20:15 -0700338 auto renderScreenImplLocked(const RenderArea& renderArea,
339 SurfaceFlinger::TraverseLayersFunction traverseLayers,
chaviwc6ad8af2020-08-03 11:33:30 -0700340 const sp<GraphicBuffer>& buffer, bool forSystem, int* outSyncFd,
341 bool regionSampling) {
chaviw426b5c02020-07-27 11:20:15 -0700342 ScreenCaptureResults captureResults;
chaviwc6ad8af2020-08-03 11:33:30 -0700343 return mFlinger->renderScreenImplLocked(renderArea, traverseLayers, buffer, forSystem,
344 outSyncFd, regionSampling, captureResults);
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700345 }
346
chaviw4b9d5e12020-08-04 18:30:35 -0700347 auto traverseLayersInLayerStack(ui::LayerStack layerStack, int32_t uid,
Marin Shalamanov1c434292020-06-12 01:47:29 +0200348 const LayerVector::Visitor& visitor) {
chaviw4b9d5e12020-08-04 18:30:35 -0700349 return mFlinger->SurfaceFlinger::traverseLayersInLayerStack(layerStack, uid, visitor);
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700350 }
351
Daniel Solomon42d04562019-01-20 21:03:19 -0800352 auto getDisplayNativePrimaries(const sp<IBinder>& displayToken,
353 ui::DisplayPrimaries &primaries) {
354 return mFlinger->SurfaceFlinger::getDisplayNativePrimaries(displayToken, primaries);
355 }
356
Arthur Hung1bedccb2020-09-24 10:09:25 +0000357 auto& getTransactionQueue() { return mFlinger->mTransactionQueues; }
Valerie Haud251afb2019-03-29 14:19:02 -0700358
Ady Abraham22c7b5c2020-09-22 19:33:40 -0700359 auto setTransactionState(int64_t frameTimelineVsyncId, const Vector<ComposerState>& states,
Valerie Haud251afb2019-03-29 14:19:02 -0700360 const Vector<DisplayState>& displays, uint32_t flags,
361 const sp<IBinder>& applyToken,
362 const InputWindowCommands& inputWindowCommands,
363 int64_t desiredPresentTime, const client_cache_t& uncacheBuffer,
364 bool hasListenerCallbacks,
Pablo Gamito7eb7ee72020-08-05 10:57:05 +0000365 std::vector<ListenerCallbacks>& listenerCallbacks,
366 uint64_t transactionId) {
Ady Abraham22c7b5c2020-09-22 19:33:40 -0700367 return mFlinger->setTransactionState(frameTimelineVsyncId, states, displays, flags,
368 applyToken, inputWindowCommands, desiredPresentTime,
369 uncacheBuffer, hasListenerCallbacks, listenerCallbacks,
Pablo Gamito7eb7ee72020-08-05 10:57:05 +0000370 transactionId);
Valerie Haud251afb2019-03-29 14:19:02 -0700371 }
372
Arthur Hung1bedccb2020-09-24 10:09:25 +0000373 auto flushTransactionQueues() { return mFlinger->flushTransactionQueues(); };
Valerie Haud251afb2019-03-29 14:19:02 -0700374
Eduardo Rochaac867192020-09-14 20:12:17 +0000375 auto onTransact(uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) {
376 return mFlinger->onTransact(code, data, reply, flags);
377 }
378
Lloyd Pique86016da2018-03-01 16:09:38 -0800379 /* ------------------------------------------------------------------------
380 * Read-only access to private data to assert post-conditions.
381 */
382
383 const auto& getAnimFrameTracker() const { return mFlinger->mAnimFrameTracker; }
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -0800384 const auto& getHasPoweredOff() const { return mFlinger->mHasPoweredOff; }
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -0800385 const auto& getVisibleRegionsDirty() const { return mFlinger->mVisibleRegionsDirty; }
Lloyd Pique441d5042018-10-18 16:49:51 -0700386 auto& getHwComposer() const {
387 return static_cast<impl::HWComposer&>(mFlinger->getHwComposer());
388 }
Lloyd Pique9370a482019-10-03 17:58:30 -0700389 auto& getCompositionEngine() const { return mFlinger->getCompositionEngine(); }
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -0800390
Lloyd Pique86016da2018-03-01 16:09:38 -0800391 const auto& getCompositorTiming() const { return mFlinger->getBE().mCompositorTiming; }
392
Lloyd Piquef58625d2017-12-19 13:22:33 -0800393 /* ------------------------------------------------------------------------
394 * Read-write access to private data to set up preconditions and assert
395 * post-conditions.
396 */
Lloyd Pique0b1fe702018-01-22 18:03:16 -0800397
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800398 auto& mutableHasWideColorDisplay() { return SurfaceFlinger::hasWideColorDisplay; }
Peiyong Lin13effd12018-07-24 17:01:47 -0700399 auto& mutableUseColorManagement() { return SurfaceFlinger::useColorManagement; }
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800400
Lloyd Piquee39cad22017-12-20 17:01:29 -0800401 auto& mutableCurrentState() { return mFlinger->mCurrentState; }
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800402 auto& mutableDisplayColorSetting() { return mFlinger->mDisplayColorSetting; }
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700403 auto& mutableDisplays() { return mFlinger->mDisplays; }
Lloyd Piquee39cad22017-12-20 17:01:29 -0800404 auto& mutableDrawingState() { return mFlinger->mDrawingState; }
Lloyd Piquee39cad22017-12-20 17:01:29 -0800405 auto& mutableEventQueue() { return mFlinger->mEventQueue; }
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700406 auto& mutableGeometryInvalid() { return mFlinger->mGeometryInvalid; }
Lloyd Pique0b1fe702018-01-22 18:03:16 -0800407 auto& mutableInterceptor() { return mFlinger->mInterceptor; }
Lloyd Pique6cf11032018-01-22 18:57:44 -0800408 auto& mutableMainThreadId() { return mFlinger->mMainThreadId; }
Lloyd Pique0b1fe702018-01-22 18:03:16 -0800409 auto& mutablePendingHotplugEvents() { return mFlinger->mPendingHotplugEvents; }
Dominik Laskowski075d3172018-05-24 15:50:06 -0700410 auto& mutablePhysicalDisplayTokens() { return mFlinger->mPhysicalDisplayTokens; }
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700411 auto& mutableTexturePool() { return mFlinger->mTexturePool; }
Lloyd Pique0b1fe702018-01-22 18:03:16 -0800412 auto& mutableTransactionFlags() { return mFlinger->mTransactionFlags; }
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800413 auto& mutableUseHwcVirtualDisplays() { return mFlinger->mUseHwcVirtualDisplays; }
Peiyong Lin74ca2f42019-01-14 19:36:57 -0800414 auto& mutablePowerAdvisor() { return mFlinger->mPowerAdvisor; }
Lloyd Pique4fe29402019-08-12 16:51:24 -0700415 auto& mutableDebugDisableHWC() { return mFlinger->mDebugDisableHWC; }
Lloyd Piquee39cad22017-12-20 17:01:29 -0800416
Lloyd Pique6cf11032018-01-22 18:57:44 -0800417 auto& mutableComposerSequenceId() { return mFlinger->getBE().mComposerSequenceId; }
Lloyd Pique441d5042018-10-18 16:49:51 -0700418 auto& mutableHwcDisplayData() { return getHwComposer().mDisplayData; }
419 auto& mutableHwcPhysicalDisplayIdMap() { return getHwComposer().mPhysicalDisplayIdMap; }
420 auto& mutableInternalHwcDisplayId() { return getHwComposer().mInternalHwcDisplayId; }
421 auto& mutableExternalHwcDisplayId() { return getHwComposer().mExternalHwcDisplayId; }
Ady Abraham60e42ea2020-03-09 19:17:31 -0700422 auto& mutableUseFrameRateApi() { return mFlinger->useFrameRateApi; }
Lloyd Piquea618d852018-01-17 11:52:30 -0800423
Valerie Hau09e60052019-12-15 14:51:15 -0800424 auto fromHandle(const sp<IBinder>& handle) {
Valerie Hau09e60052019-12-15 14:51:15 -0800425 return mFlinger->fromHandle(handle);
426 }
427
Lloyd Piquee39cad22017-12-20 17:01:29 -0800428 ~TestableSurfaceFlinger() {
429 // All these pointer and container clears help ensure that GMock does
430 // not report a leaked object, since the SurfaceFlinger instance may
431 // still be referenced by something despite our best efforts to destroy
432 // it after each test is done.
433 mutableDisplays().clear();
Ady Abraham2939f092019-03-04 17:44:06 -0800434 mutableCurrentState().displays.clear();
435 mutableDrawingState().displays.clear();
Lloyd Pique0b1fe702018-01-22 18:03:16 -0800436 mutableEventQueue().reset();
Pablo Gamito5cfc6e52020-09-10 11:18:03 +0000437 mutableInterceptor().clear();
Lloyd Pique2eb71eb2019-08-13 13:23:03 -0700438 mFlinger->mScheduler.reset();
Lloyd Pique441d5042018-10-18 16:49:51 -0700439 mFlinger->mCompositionEngine->setHwComposer(std::unique_ptr<HWComposer>());
Lloyd Piqueb97e04f2018-10-18 17:07:05 -0700440 mFlinger->mCompositionEngine->setRenderEngine(
441 std::unique_ptr<renderengine::RenderEngine>());
Lloyd Piquee39cad22017-12-20 17:01:29 -0800442 }
Lloyd Piquef58625d2017-12-19 13:22:33 -0800443
Lloyd Piquea618d852018-01-17 11:52:30 -0800444 /* ------------------------------------------------------------------------
445 * Wrapper classes for Read-write access to private data to set up
446 * preconditions and assert post-conditions.
447 */
Ana Krulec4593b692019-01-11 22:07:25 -0800448 struct HWC2Display : public HWC2::impl::Display {
Peiyong Lin74ca2f42019-01-14 19:36:57 -0800449 HWC2Display(Hwc2::Composer& composer,
Peiyong Line9d809e2020-04-14 13:10:48 -0700450 const std::unordered_set<hal::Capability>& capabilities, hal::HWDisplayId id,
451 hal::DisplayType type)
Peiyong Lin74ca2f42019-01-14 19:36:57 -0800452 : HWC2::impl::Display(composer, capabilities, id, type) {}
Lloyd Piquea618d852018-01-17 11:52:30 -0800453 ~HWC2Display() {
454 // Prevents a call to disable vsyncs.
Peiyong Line9d809e2020-04-14 13:10:48 -0700455 mType = hal::DisplayType::INVALID;
Lloyd Piquea618d852018-01-17 11:52:30 -0800456 }
457
458 auto& mutableIsConnected() { return this->mIsConnected; }
459 auto& mutableConfigs() { return this->mConfigs; }
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700460 auto& mutableLayers() { return this->mLayers; }
Lloyd Piquea618d852018-01-17 11:52:30 -0800461 };
462
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800463 class FakeHwcDisplayInjector {
464 public:
Peiyong Line9d809e2020-04-14 13:10:48 -0700465 static constexpr hal::HWDisplayId DEFAULT_HWC_DISPLAY_ID = 1000;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800466 static constexpr int32_t DEFAULT_WIDTH = 1920;
467 static constexpr int32_t DEFAULT_HEIGHT = 1280;
468 static constexpr int32_t DEFAULT_REFRESH_RATE = 16'666'666;
Ady Abraham2139f732019-11-13 18:56:40 -0800469 static constexpr int32_t DEFAULT_CONFIG_GROUP = 7;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800470 static constexpr int32_t DEFAULT_DPI = 320;
Peiyong Line9d809e2020-04-14 13:10:48 -0700471 static constexpr hal::HWConfigId DEFAULT_ACTIVE_CONFIG = 0;
Peiyong Lin65248e02020-04-18 21:15:07 -0700472 static constexpr hal::PowerMode DEFAULT_POWER_MODE = hal::PowerMode::ON;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800473
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200474 FakeHwcDisplayInjector(HalDisplayId displayId, hal::DisplayType hwcDisplayType,
475 bool isPrimary)
Dominik Laskowski075d3172018-05-24 15:50:06 -0700476 : mDisplayId(displayId), mHwcDisplayType(hwcDisplayType), mIsPrimary(isPrimary) {}
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800477
Peiyong Line9d809e2020-04-14 13:10:48 -0700478 auto& setHwcDisplayId(hal::HWDisplayId displayId) {
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800479 mHwcDisplayId = displayId;
480 return *this;
481 }
482
483 auto& setWidth(int32_t width) {
484 mWidth = width;
485 return *this;
486 }
487
488 auto& setHeight(int32_t height) {
489 mHeight = height;
490 return *this;
491 }
492
Ady Abraham8a82ba62020-01-17 12:43:17 -0800493 auto& setRefreshRate(int32_t refreshRate) {
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800494 mRefreshRate = refreshRate;
495 return *this;
496 }
497
498 auto& setDpiX(int32_t dpi) {
499 mDpiX = dpi;
500 return *this;
501 }
502
503 auto& setDpiY(int32_t dpi) {
504 mDpiY = dpi;
505 return *this;
506 }
507
Peiyong Line9d809e2020-04-14 13:10:48 -0700508 auto& setActiveConfig(hal::HWConfigId config) {
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800509 mActiveConfig = config;
510 return *this;
511 }
512
Peiyong Line9d809e2020-04-14 13:10:48 -0700513 auto& setCapabilities(const std::unordered_set<hal::Capability>* capabilities) {
Lloyd Piquee22f0332018-07-16 16:35:56 -0700514 mCapabilities = capabilities;
515 return *this;
516 }
517
Peiyong Lin65248e02020-04-18 21:15:07 -0700518 auto& setPowerMode(hal::PowerMode mode) {
Peiyong Lin1336e6e2019-05-28 09:23:50 -0700519 mPowerMode = mode;
520 return *this;
521 }
522
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800523 void inject(TestableSurfaceFlinger* flinger, Hwc2::Composer* composer) {
Peiyong Line9d809e2020-04-14 13:10:48 -0700524 static const std::unordered_set<hal::Capability> defaultCapabilities;
Lloyd Piquee22f0332018-07-16 16:35:56 -0700525 if (mCapabilities == nullptr) mCapabilities = &defaultCapabilities;
526
527 // Caution - Make sure that any values passed by reference here do
528 // not refer to an instance owned by FakeHwcDisplayInjector. This
529 // class has temporary lifetime, while the constructed HWC2::Display
530 // is much longer lived.
Peiyong Lin74ca2f42019-01-14 19:36:57 -0800531 auto display = std::make_unique<HWC2Display>(*composer, *mCapabilities, mHwcDisplayId,
532 mHwcDisplayType);
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800533
534 auto config = HWC2::Display::Config::Builder(*display, mActiveConfig);
535 config.setWidth(mWidth);
536 config.setHeight(mHeight);
537 config.setVsyncPeriod(mRefreshRate);
538 config.setDpiX(mDpiX);
539 config.setDpiY(mDpiY);
Ady Abraham2139f732019-11-13 18:56:40 -0800540 config.setConfigGroup(mConfigGroup);
Ady Abraham8a82ba62020-01-17 12:43:17 -0800541 display->mutableConfigs().emplace(static_cast<int32_t>(mActiveConfig), config.build());
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800542 display->mutableIsConnected() = true;
Peiyong Lin65248e02020-04-18 21:15:07 -0700543 display->setPowerMode(mPowerMode);
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800544
Peiyong Linbdd08cc2019-12-17 21:35:14 -0800545 flinger->mutableHwcDisplayData()[mDisplayId].hwcDisplay = std::move(display);
Dominik Laskowski075d3172018-05-24 15:50:06 -0700546
Peiyong Line9d809e2020-04-14 13:10:48 -0700547 if (mHwcDisplayType == hal::DisplayType::PHYSICAL) {
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200548 const auto physicalId = PhysicalDisplayId::tryCast(mDisplayId);
549 LOG_ALWAYS_FATAL_IF(!physicalId);
550 flinger->mutableHwcPhysicalDisplayIdMap().emplace(mHwcDisplayId, *physicalId);
Dominik Laskowski075d3172018-05-24 15:50:06 -0700551 (mIsPrimary ? flinger->mutableInternalHwcDisplayId()
552 : flinger->mutableExternalHwcDisplayId()) = mHwcDisplayId;
553 }
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800554 }
555
556 private:
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200557 const HalDisplayId mDisplayId;
Peiyong Line9d809e2020-04-14 13:10:48 -0700558 const hal::DisplayType mHwcDisplayType;
Dominik Laskowski075d3172018-05-24 15:50:06 -0700559 const bool mIsPrimary;
560
Peiyong Line9d809e2020-04-14 13:10:48 -0700561 hal::HWDisplayId mHwcDisplayId = DEFAULT_HWC_DISPLAY_ID;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800562 int32_t mWidth = DEFAULT_WIDTH;
563 int32_t mHeight = DEFAULT_HEIGHT;
Ady Abraham8a82ba62020-01-17 12:43:17 -0800564 int32_t mRefreshRate = DEFAULT_REFRESH_RATE;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800565 int32_t mDpiX = DEFAULT_DPI;
Ady Abraham2139f732019-11-13 18:56:40 -0800566 int32_t mConfigGroup = DEFAULT_CONFIG_GROUP;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800567 int32_t mDpiY = DEFAULT_DPI;
Peiyong Line9d809e2020-04-14 13:10:48 -0700568 hal::HWConfigId mActiveConfig = DEFAULT_ACTIVE_CONFIG;
Peiyong Lin65248e02020-04-18 21:15:07 -0700569 hal::PowerMode mPowerMode = DEFAULT_POWER_MODE;
Peiyong Line9d809e2020-04-14 13:10:48 -0700570 const std::unordered_set<hal::Capability>* mCapabilities = nullptr;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800571 };
572
573 class FakeDisplayDeviceInjector {
574 public:
Dominik Laskowski075d3172018-05-24 15:50:06 -0700575 FakeDisplayDeviceInjector(TestableSurfaceFlinger& flinger,
Lloyd Pique9370a482019-10-03 17:58:30 -0700576 std::shared_ptr<compositionengine::Display> compositionDisplay,
Dominik Laskowski55c85402020-01-21 16:25:47 -0800577 std::optional<DisplayConnectionType> connectionType,
Peiyong Line9d809e2020-04-14 13:10:48 -0700578 std::optional<hal::HWDisplayId> hwcDisplayId, bool isPrimary)
Lloyd Pique9370a482019-10-03 17:58:30 -0700579 : mFlinger(flinger),
Marin Shalamanov700e6392020-02-12 20:22:26 +0100580 mCreationArgs(flinger.mFlinger.get(), mDisplayToken, compositionDisplay),
581 mHwcDisplayId(hwcDisplayId) {
Dominik Laskowski55c85402020-01-21 16:25:47 -0800582 mCreationArgs.connectionType = connectionType;
Dominik Laskowski075d3172018-05-24 15:50:06 -0700583 mCreationArgs.isPrimary = isPrimary;
584 }
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800585
586 sp<IBinder> token() const { return mDisplayToken; }
587
588 DisplayDeviceState& mutableDrawingDisplayState() {
589 return mFlinger.mutableDrawingState().displays.editValueFor(mDisplayToken);
590 }
591
592 DisplayDeviceState& mutableCurrentDisplayState() {
593 return mFlinger.mutableCurrentState().displays.editValueFor(mDisplayToken);
594 }
595
Lloyd Pique9d9cf402018-02-16 17:47:13 -0800596 const auto& getDrawingDisplayState() {
597 return mFlinger.mutableDrawingState().displays.valueFor(mDisplayToken);
598 }
599
600 const auto& getCurrentDisplayState() {
601 return mFlinger.mutableCurrentState().displays.valueFor(mDisplayToken);
602 }
603
Dominik Laskowski9fae1022018-05-29 13:17:40 -0700604 auto& mutableDisplayDevice() { return mFlinger.mutableDisplays()[mDisplayToken]; }
Lloyd Pique9d9cf402018-02-16 17:47:13 -0800605
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800606 auto& setNativeWindow(const sp<ANativeWindow>& nativeWindow) {
Lloyd Pique2eef1d22018-09-18 21:30:04 -0700607 mCreationArgs.nativeWindow = nativeWindow;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800608 return *this;
609 }
610
Lloyd Pique542307f2018-10-19 13:24:08 -0700611 auto& setDisplaySurface(const sp<compositionengine::DisplaySurface>& displaySurface) {
Lloyd Pique2eef1d22018-09-18 21:30:04 -0700612 mCreationArgs.displaySurface = displaySurface;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800613 return *this;
614 }
615
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800616 auto& setSecure(bool secure) {
Lloyd Pique2eef1d22018-09-18 21:30:04 -0700617 mCreationArgs.isSecure = secure;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800618 return *this;
619 }
620
Peiyong Lin65248e02020-04-18 21:15:07 -0700621 auto& setPowerMode(hal::PowerMode mode) {
Lloyd Pique2eef1d22018-09-18 21:30:04 -0700622 mCreationArgs.initialPowerMode = mode;
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700623 return *this;
624 }
625
Valerie Hau9758ae02018-10-09 16:05:09 -0700626 auto& setHwcColorModes(
627 const std::unordered_map<ui::ColorMode, std::vector<ui::RenderIntent>>
628 hwcColorModes) {
629 mCreationArgs.hwcColorModes = hwcColorModes;
630 return *this;
631 }
632
633 auto& setHasWideColorGamut(bool hasWideColorGamut) {
634 mCreationArgs.hasWideColorGamut = hasWideColorGamut;
635 return *this;
636 }
637
Lloyd Pique33050472019-12-19 17:12:44 -0800638 auto& setPhysicalOrientation(ui::Rotation orientation) {
639 mCreationArgs.physicalOrientation = orientation;
640 return *this;
641 }
642
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800643 sp<DisplayDevice> inject() {
Lloyd Pique9370a482019-10-03 17:58:30 -0700644 const auto displayId = mCreationArgs.compositionDisplay->getDisplayId();
645
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800646 DisplayDeviceState state;
Dominik Laskowski55c85402020-01-21 16:25:47 -0800647 if (const auto type = mCreationArgs.connectionType) {
Lloyd Pique9370a482019-10-03 17:58:30 -0700648 LOG_ALWAYS_FATAL_IF(!displayId);
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200649 const auto physicalId = PhysicalDisplayId::tryCast(*displayId);
650 LOG_ALWAYS_FATAL_IF(!physicalId);
Marin Shalamanov700e6392020-02-12 20:22:26 +0100651 LOG_ALWAYS_FATAL_IF(!mHwcDisplayId);
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200652 state.physical = {.id = *physicalId, .type = *type, .hwcDisplayId = *mHwcDisplayId};
Dominik Laskowski55c85402020-01-21 16:25:47 -0800653 }
654
Lloyd Pique2eef1d22018-09-18 21:30:04 -0700655 state.isSecure = mCreationArgs.isSecure;
656
Lloyd Pique9370a482019-10-03 17:58:30 -0700657 sp<DisplayDevice> device = new DisplayDevice(mCreationArgs);
Lloyd Pique2eef1d22018-09-18 21:30:04 -0700658 mFlinger.mutableDisplays().emplace(mDisplayToken, device);
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800659 mFlinger.mutableCurrentState().displays.add(mDisplayToken, state);
660 mFlinger.mutableDrawingState().displays.add(mDisplayToken, state);
661
Dominik Laskowski55c85402020-01-21 16:25:47 -0800662 if (const auto& physical = state.physical) {
663 mFlinger.mutablePhysicalDisplayTokens()[physical->id] = mDisplayToken;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800664 }
665
666 return device;
667 }
668
669 private:
670 TestableSurfaceFlinger& mFlinger;
671 sp<BBinder> mDisplayToken = new BBinder();
Lloyd Pique2eef1d22018-09-18 21:30:04 -0700672 DisplayDeviceCreationArgs mCreationArgs;
Peiyong Line9d809e2020-04-14 13:10:48 -0700673 const std::optional<hal::HWDisplayId> mHwcDisplayId;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800674 };
675
Dominik Laskowski8b01cc02020-07-14 19:02:41 -0700676private:
677 void setVsyncEnabled(bool) override {}
678 void changeRefreshRate(const Scheduler::RefreshRate&, Scheduler::ConfigEvent) override {}
679 void repaintEverythingForHWC() override {}
680 void kernelTimerChanged(bool) override {}
681
Lloyd Pique90c115d2018-09-18 21:39:42 -0700682 surfaceflinger::test::Factory mFactory;
683 sp<SurfaceFlinger> mFlinger = new SurfaceFlinger(mFactory, SurfaceFlinger::SkipInitialization);
Dominik Laskowski7c9dbf92019-08-01 17:57:31 -0700684 TestableScheduler* mScheduler = nullptr;
Ady Abrahamabc27602020-04-08 17:20:29 -0700685 Hwc2::mock::Display mDisplay;
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200686 mock::DisplayIdGenerator<GpuVirtualDisplayId> mGpuVirtualDisplayIdGenerator;
Lloyd Piquef58625d2017-12-19 13:22:33 -0800687};
688
689} // namespace android