blob: c23fcc7d58288e2970a7a4b51d84c7b22887d23b [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>
chaviw0ef7caa2021-01-05 11:04:50 -080026#include <gui/ScreenCaptureResults.h>
Lloyd Pique70d91362018-10-18 16:02:55 -070027
Lloyd Pique90c115d2018-09-18 21:39:42 -070028#include "BufferQueueLayer.h"
29#include "BufferStateLayer.h"
Lloyd Pique90c115d2018-09-18 21:39:42 -070030#include "ContainerLayer.h"
Lloyd Piquef58625d2017-12-19 13:22:33 -080031#include "DisplayDevice.h"
Vishnu Nairfa247b12020-02-11 08:58:26 -080032#include "EffectLayer.h"
Ady Abrahamc581d3c2020-08-06 17:34:27 -070033#include "FakeVsyncConfiguration.h"
Adithya Srinivasan2db3c1b2020-11-18 12:43:17 -080034#include "FrameTracer/FrameTracer.h"
Lloyd Piqued6b579f2018-04-06 15:29:10 -070035#include "Layer.h"
Lloyd Pique90c115d2018-09-18 21:39:42 -070036#include "NativeWindowSurface.h"
Dominik Laskowski9dab3432019-03-27 13:21:10 -070037#include "Scheduler/MessageQueue.h"
Steven Thomas2bbaabe2019-08-28 16:08:35 -070038#include "Scheduler/RefreshRateConfigs.h"
Lloyd Pique90c115d2018-09-18 21:39:42 -070039#include "StartPropertySetThread.h"
Lloyd Piquef58625d2017-12-19 13:22:33 -080040#include "SurfaceFlinger.h"
Lloyd Pique24f3bfe2019-10-02 19:29:10 -070041#include "SurfaceFlingerDefaultFactory.h"
Lloyd Pique90c115d2018-09-18 21:39:42 -070042#include "SurfaceInterceptor.h"
Dominik Laskowski7c9dbf92019-08-01 17:57:31 -070043#include "TestableScheduler.h"
Marin Shalamanov045b7002021-01-07 16:56:24 +010044#include "mock/DisplayHardware/MockComposer.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
Dominik Laskowskie0e0cde2021-07-30 10:42:05 -070076 std::unique_ptr<MessageQueue> createMessageQueue(ICompositor& compositor) override {
77 return std::make_unique<android::impl::MessageQueue>(compositor);
Lloyd Pique90c115d2018-09-18 21:39:42 -070078 }
79
Ady Abraham8287e852020-08-12 14:44:58 -070080 std::unique_ptr<scheduler::VsyncConfiguration> createVsyncConfiguration(
Marin Shalamanov526c3382020-12-10 15:22:29 +010081 Fps /*currentRefreshRate*/) override {
Ana Krulec757f63a2019-01-25 10:46:18 -080082 return std::make_unique<scheduler::FakePhaseOffsets>();
83 }
84
Ady Abraham3efa3942021-06-24 19:01:25 -070085 std::unique_ptr<Scheduler> createScheduler(
86 const std::shared_ptr<scheduler::RefreshRateConfigs>&, 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(
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -0800159 std::shared_ptr<TimeStats> timeStats, pid_t surfaceFlingerPid = 0) override {
160 return std::make_unique<mock::FrameTimeline>(timeStats, surfaceFlingerPid);
Adithya Srinivasan01189672020-10-20 14:23:05 -0700161 }
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; }
187
Lloyd Piquef58625d2017-12-19 13:22:33 -0800188 // Extend this as needed for accessing SurfaceFlinger private (and public)
189 // functions.
190
Peiyong Lin833074a2018-08-28 11:53:54 -0700191 void setupRenderEngine(std::unique_ptr<renderengine::RenderEngine> renderEngine) {
Lloyd Piqueb97e04f2018-10-18 17:07:05 -0700192 mFlinger->mCompositionEngine->setRenderEngine(std::move(renderEngine));
Lloyd Piquee39cad22017-12-20 17:01:29 -0800193 }
194
195 void setupComposer(std::unique_ptr<Hwc2::Composer> composer) {
Lloyd Pique441d5042018-10-18 16:49:51 -0700196 mFlinger->mCompositionEngine->setHwComposer(
197 std::make_unique<impl::HWComposer>(std::move(composer)));
Lloyd Piquee39cad22017-12-20 17:01:29 -0800198 }
199
Alec Mourie4034bb2019-11-19 12:45:54 -0800200 void setupTimeStats(const std::shared_ptr<TimeStats>& timeStats) {
201 mFlinger->mCompositionEngine->setTimeStats(timeStats);
202 }
203
Dominik Laskowski8b01cc02020-07-14 19:02:41 -0700204 // The ISchedulerCallback argument can be nullptr for a no-op implementation.
Ady Abraham8cb21882020-08-26 18:22:05 -0700205 void setupScheduler(std::unique_ptr<scheduler::VsyncController> vsyncController,
206 std::unique_ptr<scheduler::VSyncTracker> vsyncTracker,
Dominik Laskowski7c9dbf92019-08-01 17:57:31 -0700207 std::unique_ptr<EventThread> appEventThread,
Ady Abraham8a82ba62020-01-17 12:43:17 -0800208 std::unique_ptr<EventThread> sfEventThread,
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100209 ISchedulerCallback* callback = nullptr, bool hasMultipleModes = false) {
210 DisplayModes modes{DisplayMode::Builder(0)
211 .setId(DisplayModeId(0))
Ady Abraham5e7ee862021-06-23 17:43:41 -0700212 .setPhysicalDisplayId(PhysicalDisplayId::fromPort(0))
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100213 .setVsyncPeriod(16'666'667)
214 .setGroup(0)
215 .build()};
Ady Abrahamabc27602020-04-08 17:20:29 -0700216
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100217 if (hasMultipleModes) {
218 modes.emplace_back(DisplayMode::Builder(1)
219 .setId(DisplayModeId(1))
Ady Abraham5e7ee862021-06-23 17:43:41 -0700220 .setPhysicalDisplayId(PhysicalDisplayId::fromPort(0))
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100221 .setVsyncPeriod(11'111'111)
222 .setGroup(0)
223 .build());
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700224 }
225
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100226 const auto currMode = DisplayModeId(0);
Ady Abraham3efa3942021-06-24 19:01:25 -0700227 mRefreshRateConfigs = std::make_shared<scheduler::RefreshRateConfigs>(modes, currMode);
228 const auto currFps = mRefreshRateConfigs->getCurrentRefreshRate().getFps();
229 mFlinger->mVsyncConfiguration = mFactory.createVsyncConfiguration(currFps);
Ady Abraham23ea9da2021-07-14 16:32:56 -0700230 mFlinger->mVsyncModulator = sp<scheduler::VsyncModulator>::make(
231 mFlinger->mVsyncConfiguration->getCurrentConfigs());
Marin Shalamanov68a94092020-11-24 17:48:00 +0100232 mFlinger->mRefreshRateStats =
233 std::make_unique<scheduler::RefreshRateStats>(*mFlinger->mTimeStats, currFps,
234 /*powerMode=*/hal::PowerMode::OFF);
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700235
Ady Abraham8cb21882020-08-26 18:22:05 -0700236 mScheduler = new TestableScheduler(std::move(vsyncController), std::move(vsyncTracker),
Ady Abraham3efa3942021-06-24 19:01:25 -0700237 mRefreshRateConfigs, *(callback ?: this));
Dominik Laskowski7c9dbf92019-08-01 17:57:31 -0700238
Dominik Laskowski98041832019-08-01 18:35:59 -0700239 mFlinger->mAppConnectionHandle = mScheduler->createConnection(std::move(appEventThread));
240 mFlinger->mSfConnectionHandle = mScheduler->createConnection(std::move(sfEventThread));
Dominik Laskowskif7a09ed2019-10-07 13:54:18 -0700241 resetScheduler(mScheduler);
Dominik Laskowski7c9dbf92019-08-01 17:57:31 -0700242 }
243
Dominik Laskowskif7a09ed2019-10-07 13:54:18 -0700244 void resetScheduler(Scheduler* scheduler) { mFlinger->mScheduler.reset(scheduler); }
245
Ady Abraham44e9f3b2021-02-16 15:22:58 -0800246 TestableScheduler& mutableScheduler() const { return *mScheduler; }
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& mutableLayerDrawingState(const sp<Layer>& layer) { return layer->mDrawingState; }
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700264
Valerie Haud251afb2019-03-29 14:19:02 -0700265 auto& mutableStateLock() { return mFlinger->mStateLock; }
266
Dominik Laskowskib7251f42020-04-20 17:42:59 -0700267 static auto findOutputLayerForDisplay(const sp<Layer>& layer,
268 const sp<const DisplayDevice>& display) {
269 return layer->findOutputLayerForDisplay(display.get());
270 }
271
272 static void setLayerSidebandStream(const sp<Layer>& layer,
273 const sp<NativeHandle>& sidebandStream) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800274 layer->mDrawingState.sidebandStream = sidebandStream;
Lloyd Pique0b785d82018-12-04 17:25:27 -0800275 layer->mSidebandStream = sidebandStream;
Lloyd Piquede196652020-01-22 17:29:58 -0800276 layer->editCompositionState()->sidebandStream = sidebandStream;
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700277 }
278
Dominik Laskowskib7251f42020-04-20 17:42:59 -0700279 void setLayerCompositionType(const sp<Layer>& layer, hal::Composition type) {
280 auto outputLayer = findOutputLayerForDisplay(layer, mFlinger->getDefaultDisplayDevice());
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800281 LOG_ALWAYS_FATAL_IF(!outputLayer);
282 auto& state = outputLayer->editState();
283 LOG_ALWAYS_FATAL_IF(!outputLayer->getState().hwc);
Peiyong Line9d809e2020-04-14 13:10:48 -0700284 (*state.hwc).hwcCompositionType = type;
Dominik Laskowskib7251f42020-04-20 17:42:59 -0700285 }
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800286
Dominik Laskowskib7251f42020-04-20 17:42:59 -0700287 static void setLayerPotentialCursor(const sp<Layer>& layer, bool potentialCursor) {
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700288 layer->mPotentialCursor = potentialCursor;
289 }
290
Garfield Tande619fa2020-10-02 17:13:53 -0700291 static void setLayerDrawingParent(const sp<Layer>& layer, const sp<Layer>& drawingParent) {
Rob Carrc6d2d2b2021-10-25 16:51:49 +0000292 layer->mDrawingParent = drawingParent;
Garfield Tande619fa2020-10-02 17:13:53 -0700293 }
294
Lloyd Piquef58625d2017-12-19 13:22:33 -0800295 /* ------------------------------------------------------------------------
296 * Forwarding for functions being tested
297 */
Lloyd Pique0b1fe702018-01-22 18:03:16 -0800298
Dominik Laskowskie0e0cde2021-07-30 10:42:05 -0700299 nsecs_t commit() {
300 constexpr int64_t kVsyncId = 123;
301 const nsecs_t now = systemTime();
302 const nsecs_t expectedVsyncTime = now + 10'000'000;
303 mFlinger->commit(now, kVsyncId, expectedVsyncTime);
304 return now;
305 }
306
307 void commitAndComposite() { mFlinger->composite(commit()); }
308
Lloyd Piquea482f992018-01-22 19:00:34 -0800309 auto createDisplay(const String8& displayName, bool secure) {
310 return mFlinger->createDisplay(displayName, secure);
311 }
312
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700313 auto destroyDisplay(const sp<IBinder>& displayToken) {
314 return mFlinger->destroyDisplay(displayToken);
315 }
Lloyd Piquea482f992018-01-22 19:00:34 -0800316
Dominik Laskowski13948602021-03-08 20:48:28 -0800317 void enableHalVirtualDisplays(bool enable) { mFlinger->enableHalVirtualDisplays(enable); }
318
Lloyd Pique9370a482019-10-03 17:58:30 -0700319 auto setupNewDisplayDeviceInternal(
320 const wp<IBinder>& displayToken,
321 std::shared_ptr<compositionengine::Display> compositionDisplay,
322 const DisplayDeviceState& state,
323 const sp<compositionengine::DisplaySurface>& dispSurface,
Dominik Laskowski0a1435d2020-04-21 00:27:31 -0700324 const sp<IGraphicBufferProducer>& producer) NO_THREAD_SAFETY_ANALYSIS {
Lloyd Pique9370a482019-10-03 17:58:30 -0700325 return mFlinger->setupNewDisplayDeviceInternal(displayToken, compositionDisplay, state,
326 dispSurface, producer);
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800327 }
328
Dominik Laskowski9e168db2021-05-27 16:05:12 -0700329 auto commitTransactionsLocked(uint32_t transactionFlags) {
330 Mutex::Autolock lock(mFlinger->mStateLock);
331 return mFlinger->commitTransactionsLocked(transactionFlags);
Lloyd Pique0b1fe702018-01-22 18:03:16 -0800332 }
Lloyd Piquef58625d2017-12-19 13:22:33 -0800333
Dominik Laskowskiebc8d3a2021-04-16 23:18:31 -0700334 void onComposerHalHotplug(hal::HWDisplayId hwcDisplayId, hal::Connection connection) {
335 mFlinger->onComposerHalHotplug(hwcDisplayId, connection);
Lloyd Pique6cf11032018-01-22 18:57:44 -0800336 }
337
Vishnu Nairce5d0cc2019-02-28 14:38:41 -0800338 auto setDisplayStateLocked(const DisplayState& s) {
Dominik Laskowski9e168db2021-05-27 16:05:12 -0700339 Mutex::Autolock lock(mFlinger->mStateLock);
Vishnu Nairce5d0cc2019-02-28 14:38:41 -0800340 return mFlinger->setDisplayStateLocked(s);
341 }
Lloyd Pique9d9cf402018-02-16 17:47:13 -0800342
Dominik Laskowski83b88212018-12-11 13:34:06 -0800343 // Allow reading display state without locking, as if called on the SF main thread.
344 auto onInitializeDisplays() NO_THREAD_SAFETY_ANALYSIS {
345 return mFlinger->onInitializeDisplays();
346 }
Lloyd Pique86016da2018-03-01 16:09:38 -0800347
Lais Andrade3a6e47d2020-04-02 11:20:16 +0100348 auto notifyPowerBoost(int32_t boostId) { return mFlinger->notifyPowerBoost(boostId); }
349
Dominik Laskowski83b88212018-12-11 13:34:06 -0800350 // Allow reading display state without locking, as if called on the SF main thread.
351 auto setPowerModeInternal(const sp<DisplayDevice>& display,
Peiyong Lin65248e02020-04-18 21:15:07 -0700352 hal::PowerMode mode) NO_THREAD_SAFETY_ANALYSIS {
Dominik Laskowskie9774092018-12-11 10:04:24 -0800353 return mFlinger->setPowerModeInternal(display, mode);
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -0800354 }
355
chaviw426b5c02020-07-27 11:20:15 -0700356 auto renderScreenImplLocked(const RenderArea& renderArea,
357 SurfaceFlinger::TraverseLayersFunction traverseLayers,
Alec Mouria90a5702021-04-16 16:36:21 +0000358 const std::shared_ptr<renderengine::ExternalTexture>& buffer,
359 bool forSystem, bool regionSampling) {
chaviw426b5c02020-07-27 11:20:15 -0700360 ScreenCaptureResults captureResults;
chaviwc6ad8af2020-08-03 11:33:30 -0700361 return mFlinger->renderScreenImplLocked(renderArea, traverseLayers, buffer, forSystem,
chaviw17ac24b2021-01-28 18:50:05 -0800362 regionSampling, false /* grayscale */,
363 captureResults);
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700364 }
365
chaviw4b9d5e12020-08-04 18:30:35 -0700366 auto traverseLayersInLayerStack(ui::LayerStack layerStack, int32_t uid,
Marin Shalamanov1c434292020-06-12 01:47:29 +0200367 const LayerVector::Visitor& visitor) {
chaviw4b9d5e12020-08-04 18:30:35 -0700368 return mFlinger->SurfaceFlinger::traverseLayersInLayerStack(layerStack, uid, visitor);
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700369 }
370
Daniel Solomon42d04562019-01-20 21:03:19 -0800371 auto getDisplayNativePrimaries(const sp<IBinder>& displayToken,
372 ui::DisplayPrimaries &primaries) {
373 return mFlinger->SurfaceFlinger::getDisplayNativePrimaries(displayToken, primaries);
374 }
375
Arthur Hung58144272021-01-16 03:43:53 +0000376 auto& getTransactionQueue() { return mFlinger->mTransactionQueue; }
377 auto& getPendingTransactionQueue() { return mFlinger->mPendingTransactionQueues; }
Valerie Haud251afb2019-03-29 14:19:02 -0700378
Siarhei Vishniakoufc434ac2021-01-13 10:28:00 -1000379 auto setTransactionState(
380 const FrameTimelineInfo& frameTimelineInfo, const Vector<ComposerState>& states,
381 const Vector<DisplayState>& displays, uint32_t flags, const sp<IBinder>& applyToken,
382 const InputWindowCommands& inputWindowCommands, int64_t desiredPresentTime,
383 bool isAutoTimestamp, const client_cache_t& uncacheBuffer, bool hasListenerCallbacks,
384 std::vector<ListenerCallbacks>& listenerCallbacks, uint64_t transactionId) {
385 return mFlinger->setTransactionState(frameTimelineInfo, states, displays, flags, applyToken,
386 inputWindowCommands, desiredPresentTime,
Ady Abrahamf0c56492020-12-17 18:04:15 -0800387 isAutoTimestamp, uncacheBuffer, hasListenerCallbacks,
388 listenerCallbacks, transactionId);
Valerie Haud251afb2019-03-29 14:19:02 -0700389 }
390
Zhuoyao Zhang3d3540d2021-01-14 05:14:54 +0000391 auto flushTransactionQueues() { return mFlinger->flushTransactionQueues(); };
Valerie Haud251afb2019-03-29 14:19:02 -0700392
Eduardo Rochaac867192020-09-14 20:12:17 +0000393 auto onTransact(uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) {
394 return mFlinger->onTransact(code, data, reply, flags);
395 }
396
Ana Krulec31f2b3c2020-12-14 14:30:09 -0800397 auto getGPUContextPriority() { return mFlinger->getGPUContextPriority(); }
398
Ady Abraham899dcdb2021-06-15 16:56:21 -0700399 auto calculateMaxAcquiredBufferCount(Fps refreshRate,
400 std::chrono::nanoseconds presentLatency) const {
401 return SurfaceFlinger::calculateMaxAcquiredBufferCount(refreshRate, presentLatency);
Ady Abraham564f9de2021-02-03 18:34:33 -0800402 }
403
Lloyd Pique86016da2018-03-01 16:09:38 -0800404 /* ------------------------------------------------------------------------
405 * Read-only access to private data to assert post-conditions.
406 */
407
408 const auto& getAnimFrameTracker() const { return mFlinger->mAnimFrameTracker; }
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -0800409 const auto& getHasPoweredOff() const { return mFlinger->mHasPoweredOff; }
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -0800410 const auto& getVisibleRegionsDirty() const { return mFlinger->mVisibleRegionsDirty; }
Lloyd Pique441d5042018-10-18 16:49:51 -0700411 auto& getHwComposer() const {
412 return static_cast<impl::HWComposer&>(mFlinger->getHwComposer());
413 }
Lloyd Pique9370a482019-10-03 17:58:30 -0700414 auto& getCompositionEngine() const { return mFlinger->getCompositionEngine(); }
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -0800415
Lloyd Pique86016da2018-03-01 16:09:38 -0800416 const auto& getCompositorTiming() const { return mFlinger->getBE().mCompositorTiming; }
417
Adithya Srinivasanb238cd52021-02-04 17:54:05 +0000418 mock::FrameTracer* getFrameTracer() const {
419 return static_cast<mock::FrameTracer*>(mFlinger->mFrameTracer.get());
420 }
421
Lloyd Piquef58625d2017-12-19 13:22:33 -0800422 /* ------------------------------------------------------------------------
423 * Read-write access to private data to set up preconditions and assert
424 * post-conditions.
425 */
Lloyd Pique0b1fe702018-01-22 18:03:16 -0800426
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800427 auto& mutableHasWideColorDisplay() { return SurfaceFlinger::hasWideColorDisplay; }
428
Lloyd Piquee39cad22017-12-20 17:01:29 -0800429 auto& mutableCurrentState() { return mFlinger->mCurrentState; }
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800430 auto& mutableDisplayColorSetting() { return mFlinger->mDisplayColorSetting; }
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700431 auto& mutableDisplays() { return mFlinger->mDisplays; }
Lloyd Piquee39cad22017-12-20 17:01:29 -0800432 auto& mutableDrawingState() { return mFlinger->mDrawingState; }
Lloyd Piquee39cad22017-12-20 17:01:29 -0800433 auto& mutableEventQueue() { return mFlinger->mEventQueue; }
Dominik Laskowski8da6b0e2021-05-12 15:34:13 -0700434 auto& mutableGeometryDirty() { return mFlinger->mGeometryDirty; }
Lloyd Pique0b1fe702018-01-22 18:03:16 -0800435 auto& mutableInterceptor() { return mFlinger->mInterceptor; }
Lloyd Pique6cf11032018-01-22 18:57:44 -0800436 auto& mutableMainThreadId() { return mFlinger->mMainThreadId; }
Lloyd Pique0b1fe702018-01-22 18:03:16 -0800437 auto& mutablePendingHotplugEvents() { return mFlinger->mPendingHotplugEvents; }
Dominik Laskowski075d3172018-05-24 15:50:06 -0700438 auto& mutablePhysicalDisplayTokens() { return mFlinger->mPhysicalDisplayTokens; }
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700439 auto& mutableTexturePool() { return mFlinger->mTexturePool; }
Lloyd Pique0b1fe702018-01-22 18:03:16 -0800440 auto& mutableTransactionFlags() { return mFlinger->mTransactionFlags; }
Peiyong Lin74ca2f42019-01-14 19:36:57 -0800441 auto& mutablePowerAdvisor() { return mFlinger->mPowerAdvisor; }
Lloyd Pique4fe29402019-08-12 16:51:24 -0700442 auto& mutableDebugDisableHWC() { return mFlinger->mDebugDisableHWC; }
Garfield Tan9c9c1912021-07-19 12:02:16 -0700443 auto& mutableMaxRenderTargetSize() { return mFlinger->mMaxRenderTargetSize; }
Lloyd Piquee39cad22017-12-20 17:01:29 -0800444
Lloyd Pique441d5042018-10-18 16:49:51 -0700445 auto& mutableHwcDisplayData() { return getHwComposer().mDisplayData; }
446 auto& mutableHwcPhysicalDisplayIdMap() { return getHwComposer().mPhysicalDisplayIdMap; }
Dominik Laskowskif8db0f02021-04-19 11:05:25 -0700447 auto& mutablePrimaryHwcDisplayId() { return getHwComposer().mPrimaryHwcDisplayId; }
Ady Abraham60e42ea2020-03-09 19:17:31 -0700448 auto& mutableUseFrameRateApi() { return mFlinger->useFrameRateApi; }
Ady Abrahamadc914c2021-10-13 17:04:27 -0700449 auto& mutableActiveDisplayToken() { return mFlinger->mActiveDisplayToken; }
Lloyd Piquea618d852018-01-17 11:52:30 -0800450
Valerie Hau09e60052019-12-15 14:51:15 -0800451 auto fromHandle(const sp<IBinder>& handle) {
Valerie Hau09e60052019-12-15 14:51:15 -0800452 return mFlinger->fromHandle(handle);
453 }
454
Lloyd Piquee39cad22017-12-20 17:01:29 -0800455 ~TestableSurfaceFlinger() {
456 // All these pointer and container clears help ensure that GMock does
457 // not report a leaked object, since the SurfaceFlinger instance may
458 // still be referenced by something despite our best efforts to destroy
459 // it after each test is done.
460 mutableDisplays().clear();
Ady Abraham2939f092019-03-04 17:44:06 -0800461 mutableCurrentState().displays.clear();
462 mutableDrawingState().displays.clear();
Lloyd Pique0b1fe702018-01-22 18:03:16 -0800463 mutableEventQueue().reset();
Pablo Gamito5cfc6e52020-09-10 11:18:03 +0000464 mutableInterceptor().clear();
Lloyd Pique2eb71eb2019-08-13 13:23:03 -0700465 mFlinger->mScheduler.reset();
Lloyd Pique441d5042018-10-18 16:49:51 -0700466 mFlinger->mCompositionEngine->setHwComposer(std::unique_ptr<HWComposer>());
Lloyd Piqueb97e04f2018-10-18 17:07:05 -0700467 mFlinger->mCompositionEngine->setRenderEngine(
468 std::unique_ptr<renderengine::RenderEngine>());
Lloyd Piquee39cad22017-12-20 17:01:29 -0800469 }
Lloyd Piquef58625d2017-12-19 13:22:33 -0800470
Lloyd Piquea618d852018-01-17 11:52:30 -0800471 /* ------------------------------------------------------------------------
472 * Wrapper classes for Read-write access to private data to set up
473 * preconditions and assert post-conditions.
474 */
Ana Krulec4593b692019-01-11 22:07:25 -0800475 struct HWC2Display : public HWC2::impl::Display {
Peiyong Lin74ca2f42019-01-14 19:36:57 -0800476 HWC2Display(Hwc2::Composer& composer,
Peiyong Line9d809e2020-04-14 13:10:48 -0700477 const std::unordered_set<hal::Capability>& capabilities, hal::HWDisplayId id,
478 hal::DisplayType type)
Peiyong Lin74ca2f42019-01-14 19:36:57 -0800479 : HWC2::impl::Display(composer, capabilities, id, type) {}
Lloyd Piquea618d852018-01-17 11:52:30 -0800480 ~HWC2Display() {
481 // Prevents a call to disable vsyncs.
Peiyong Line9d809e2020-04-14 13:10:48 -0700482 mType = hal::DisplayType::INVALID;
Lloyd Piquea618d852018-01-17 11:52:30 -0800483 }
484
485 auto& mutableIsConnected() { return this->mIsConnected; }
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700486 auto& mutableLayers() { return this->mLayers; }
Lloyd Piquea618d852018-01-17 11:52:30 -0800487 };
488
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800489 class FakeHwcDisplayInjector {
490 public:
Peiyong Line9d809e2020-04-14 13:10:48 -0700491 static constexpr hal::HWDisplayId DEFAULT_HWC_DISPLAY_ID = 1000;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800492 static constexpr int32_t DEFAULT_WIDTH = 1920;
493 static constexpr int32_t DEFAULT_HEIGHT = 1280;
Marin Shalamanov045b7002021-01-07 16:56:24 +0100494 static constexpr int32_t DEFAULT_VSYNC_PERIOD = 16'666'666;
Ady Abraham2139f732019-11-13 18:56:40 -0800495 static constexpr int32_t DEFAULT_CONFIG_GROUP = 7;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800496 static constexpr int32_t DEFAULT_DPI = 320;
Peiyong Line9d809e2020-04-14 13:10:48 -0700497 static constexpr hal::HWConfigId DEFAULT_ACTIVE_CONFIG = 0;
Peiyong Lin65248e02020-04-18 21:15:07 -0700498 static constexpr hal::PowerMode DEFAULT_POWER_MODE = hal::PowerMode::ON;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800499
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200500 FakeHwcDisplayInjector(HalDisplayId displayId, hal::DisplayType hwcDisplayType,
501 bool isPrimary)
Dominik Laskowski075d3172018-05-24 15:50:06 -0700502 : mDisplayId(displayId), mHwcDisplayType(hwcDisplayType), mIsPrimary(isPrimary) {}
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800503
Peiyong Line9d809e2020-04-14 13:10:48 -0700504 auto& setHwcDisplayId(hal::HWDisplayId displayId) {
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800505 mHwcDisplayId = displayId;
506 return *this;
507 }
508
509 auto& setWidth(int32_t width) {
510 mWidth = width;
511 return *this;
512 }
513
514 auto& setHeight(int32_t height) {
515 mHeight = height;
516 return *this;
517 }
518
Marin Shalamanov045b7002021-01-07 16:56:24 +0100519 auto& setVsyncPeriod(int32_t vsyncPeriod) {
520 mVsyncPeriod = vsyncPeriod;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800521 return *this;
522 }
523
524 auto& setDpiX(int32_t dpi) {
525 mDpiX = dpi;
526 return *this;
527 }
528
529 auto& setDpiY(int32_t dpi) {
530 mDpiY = dpi;
531 return *this;
532 }
533
Peiyong Line9d809e2020-04-14 13:10:48 -0700534 auto& setActiveConfig(hal::HWConfigId config) {
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800535 mActiveConfig = config;
536 return *this;
537 }
538
Peiyong Line9d809e2020-04-14 13:10:48 -0700539 auto& setCapabilities(const std::unordered_set<hal::Capability>* capabilities) {
Lloyd Piquee22f0332018-07-16 16:35:56 -0700540 mCapabilities = capabilities;
541 return *this;
542 }
543
Peiyong Lin65248e02020-04-18 21:15:07 -0700544 auto& setPowerMode(hal::PowerMode mode) {
Peiyong Lin1336e6e2019-05-28 09:23:50 -0700545 mPowerMode = mode;
546 return *this;
547 }
548
Marin Shalamanov045b7002021-01-07 16:56:24 +0100549 void inject(TestableSurfaceFlinger* flinger, Hwc2::mock::Composer* composer) {
550 using ::testing::_;
551 using ::testing::DoAll;
552 using ::testing::Return;
553 using ::testing::SetArgPointee;
554
Peiyong Line9d809e2020-04-14 13:10:48 -0700555 static const std::unordered_set<hal::Capability> defaultCapabilities;
Lloyd Piquee22f0332018-07-16 16:35:56 -0700556 if (mCapabilities == nullptr) mCapabilities = &defaultCapabilities;
557
558 // Caution - Make sure that any values passed by reference here do
559 // not refer to an instance owned by FakeHwcDisplayInjector. This
560 // class has temporary lifetime, while the constructed HWC2::Display
561 // is much longer lived.
Peiyong Lin74ca2f42019-01-14 19:36:57 -0800562 auto display = std::make_unique<HWC2Display>(*composer, *mCapabilities, mHwcDisplayId,
563 mHwcDisplayType);
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800564
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800565 display->mutableIsConnected() = true;
Peiyong Lin65248e02020-04-18 21:15:07 -0700566 display->setPowerMode(mPowerMode);
Peiyong Linbdd08cc2019-12-17 21:35:14 -0800567 flinger->mutableHwcDisplayData()[mDisplayId].hwcDisplay = std::move(display);
Dominik Laskowski075d3172018-05-24 15:50:06 -0700568
Marin Shalamanov045b7002021-01-07 16:56:24 +0100569 EXPECT_CALL(*composer, getDisplayConfigs(mHwcDisplayId, _))
570 .WillRepeatedly(
571 DoAll(SetArgPointee<1>(std::vector<hal::HWConfigId>{mActiveConfig}),
572 Return(hal::Error::NONE)));
573
574 EXPECT_CALL(*composer,
575 getDisplayAttribute(mHwcDisplayId, mActiveConfig, hal::Attribute::WIDTH, _))
576 .WillRepeatedly(DoAll(SetArgPointee<3>(mWidth), Return(hal::Error::NONE)));
577
578 EXPECT_CALL(*composer,
579 getDisplayAttribute(mHwcDisplayId, mActiveConfig, hal::Attribute::HEIGHT,
580 _))
581 .WillRepeatedly(DoAll(SetArgPointee<3>(mHeight), Return(hal::Error::NONE)));
582
583 EXPECT_CALL(*composer,
584 getDisplayAttribute(mHwcDisplayId, mActiveConfig,
585 hal::Attribute::VSYNC_PERIOD, _))
586 .WillRepeatedly(
587 DoAll(SetArgPointee<3>(mVsyncPeriod), Return(hal::Error::NONE)));
588
589 EXPECT_CALL(*composer,
590 getDisplayAttribute(mHwcDisplayId, mActiveConfig, hal::Attribute::DPI_X, _))
591 .WillRepeatedly(DoAll(SetArgPointee<3>(mDpiX), Return(hal::Error::NONE)));
592
593 EXPECT_CALL(*composer,
594 getDisplayAttribute(mHwcDisplayId, mActiveConfig, hal::Attribute::DPI_Y, _))
595 .WillRepeatedly(DoAll(SetArgPointee<3>(mDpiY), Return(hal::Error::NONE)));
596
597 EXPECT_CALL(*composer,
598 getDisplayAttribute(mHwcDisplayId, mActiveConfig,
599 hal::Attribute::CONFIG_GROUP, _))
600 .WillRepeatedly(
601 DoAll(SetArgPointee<3>(mConfigGroup), Return(hal::Error::NONE)));
Marin Shalamanov3ea1d602020-12-16 19:59:39 +0100602
Peiyong Line9d809e2020-04-14 13:10:48 -0700603 if (mHwcDisplayType == hal::DisplayType::PHYSICAL) {
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200604 const auto physicalId = PhysicalDisplayId::tryCast(mDisplayId);
605 LOG_ALWAYS_FATAL_IF(!physicalId);
606 flinger->mutableHwcPhysicalDisplayIdMap().emplace(mHwcDisplayId, *physicalId);
Marin Shalamanov6e840172020-12-14 22:13:28 +0100607 if (mIsPrimary) {
Dominik Laskowskif8db0f02021-04-19 11:05:25 -0700608 flinger->mutablePrimaryHwcDisplayId() = mHwcDisplayId;
Marin Shalamanov6e840172020-12-14 22:13:28 +0100609 } else {
Dominik Laskowskif8db0f02021-04-19 11:05:25 -0700610 // If there is an external HWC display, there should always be a primary ID
Marin Shalamanov6e840172020-12-14 22:13:28 +0100611 // as well. Set it to some arbitrary value.
Dominik Laskowskif8db0f02021-04-19 11:05:25 -0700612 auto& primaryId = flinger->mutablePrimaryHwcDisplayId();
613 if (!primaryId) primaryId = mHwcDisplayId - 1;
Marin Shalamanov6e840172020-12-14 22:13:28 +0100614 }
Dominik Laskowski075d3172018-05-24 15:50:06 -0700615 }
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800616 }
617
618 private:
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200619 const HalDisplayId mDisplayId;
Peiyong Line9d809e2020-04-14 13:10:48 -0700620 const hal::DisplayType mHwcDisplayType;
Dominik Laskowski075d3172018-05-24 15:50:06 -0700621 const bool mIsPrimary;
622
Peiyong Line9d809e2020-04-14 13:10:48 -0700623 hal::HWDisplayId mHwcDisplayId = DEFAULT_HWC_DISPLAY_ID;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800624 int32_t mWidth = DEFAULT_WIDTH;
625 int32_t mHeight = DEFAULT_HEIGHT;
Marin Shalamanov045b7002021-01-07 16:56:24 +0100626 int32_t mVsyncPeriod = DEFAULT_VSYNC_PERIOD;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800627 int32_t mDpiX = DEFAULT_DPI;
628 int32_t mDpiY = DEFAULT_DPI;
Marin Shalamanov045b7002021-01-07 16:56:24 +0100629 int32_t mConfigGroup = DEFAULT_CONFIG_GROUP;
Peiyong Line9d809e2020-04-14 13:10:48 -0700630 hal::HWConfigId mActiveConfig = DEFAULT_ACTIVE_CONFIG;
Peiyong Lin65248e02020-04-18 21:15:07 -0700631 hal::PowerMode mPowerMode = DEFAULT_POWER_MODE;
Peiyong Line9d809e2020-04-14 13:10:48 -0700632 const std::unordered_set<hal::Capability>* mCapabilities = nullptr;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800633 };
634
635 class FakeDisplayDeviceInjector {
636 public:
Dominik Laskowski075d3172018-05-24 15:50:06 -0700637 FakeDisplayDeviceInjector(TestableSurfaceFlinger& flinger,
Lloyd Pique9370a482019-10-03 17:58:30 -0700638 std::shared_ptr<compositionengine::Display> compositionDisplay,
Marin Shalamanov228f46b2021-01-28 21:11:45 +0100639 std::optional<ui::DisplayConnectionType> connectionType,
Peiyong Line9d809e2020-04-14 13:10:48 -0700640 std::optional<hal::HWDisplayId> hwcDisplayId, bool isPrimary)
Lloyd Pique9370a482019-10-03 17:58:30 -0700641 : mFlinger(flinger),
Marin Shalamanov12c9e5a2021-01-07 00:25:35 +0100642 mCreationArgs(flinger.mFlinger.get(), flinger.mFlinger->getHwComposer(),
643 mDisplayToken, compositionDisplay),
Marin Shalamanov700e6392020-02-12 20:22:26 +0100644 mHwcDisplayId(hwcDisplayId) {
Dominik Laskowski55c85402020-01-21 16:25:47 -0800645 mCreationArgs.connectionType = connectionType;
Dominik Laskowski075d3172018-05-24 15:50:06 -0700646 mCreationArgs.isPrimary = isPrimary;
Marin Shalamanov045b7002021-01-07 16:56:24 +0100647
648 mActiveModeId = DisplayModeId(0);
649 DisplayModePtr activeMode =
650 DisplayMode::Builder(FakeHwcDisplayInjector::DEFAULT_ACTIVE_CONFIG)
651 .setId(mActiveModeId)
Ady Abraham5e7ee862021-06-23 17:43:41 -0700652 .setPhysicalDisplayId(PhysicalDisplayId::fromPort(0))
Marin Shalamanov045b7002021-01-07 16:56:24 +0100653 .setWidth(FakeHwcDisplayInjector::DEFAULT_WIDTH)
654 .setHeight(FakeHwcDisplayInjector::DEFAULT_HEIGHT)
655 .setVsyncPeriod(FakeHwcDisplayInjector::DEFAULT_VSYNC_PERIOD)
656 .setDpiX(FakeHwcDisplayInjector::DEFAULT_DPI)
657 .setDpiY(FakeHwcDisplayInjector::DEFAULT_DPI)
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100658 .setGroup(0)
Marin Shalamanov045b7002021-01-07 16:56:24 +0100659 .build();
660
661 DisplayModes modes{activeMode};
662 mCreationArgs.supportedModes = modes;
Ady Abraham3efa3942021-06-24 19:01:25 -0700663 mCreationArgs.refreshRateConfigs = flinger.mRefreshRateConfigs;
Dominik Laskowski075d3172018-05-24 15:50:06 -0700664 }
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800665
666 sp<IBinder> token() const { return mDisplayToken; }
667
668 DisplayDeviceState& mutableDrawingDisplayState() {
669 return mFlinger.mutableDrawingState().displays.editValueFor(mDisplayToken);
670 }
671
672 DisplayDeviceState& mutableCurrentDisplayState() {
673 return mFlinger.mutableCurrentState().displays.editValueFor(mDisplayToken);
674 }
675
Lloyd Pique9d9cf402018-02-16 17:47:13 -0800676 const auto& getDrawingDisplayState() {
677 return mFlinger.mutableDrawingState().displays.valueFor(mDisplayToken);
678 }
679
680 const auto& getCurrentDisplayState() {
681 return mFlinger.mutableCurrentState().displays.valueFor(mDisplayToken);
682 }
683
Dominik Laskowski9fae1022018-05-29 13:17:40 -0700684 auto& mutableDisplayDevice() { return mFlinger.mutableDisplays()[mDisplayToken]; }
Lloyd Pique9d9cf402018-02-16 17:47:13 -0800685
Marin Shalamanov045b7002021-01-07 16:56:24 +0100686 auto& setActiveMode(DisplayModeId mode) {
687 mActiveModeId = mode;
688 return *this;
689 }
690
691 auto& setSupportedModes(DisplayModes mode) {
692 mCreationArgs.supportedModes = mode;
693 return *this;
694 }
695
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800696 auto& setNativeWindow(const sp<ANativeWindow>& nativeWindow) {
Lloyd Pique2eef1d22018-09-18 21:30:04 -0700697 mCreationArgs.nativeWindow = nativeWindow;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800698 return *this;
699 }
700
Lloyd Pique542307f2018-10-19 13:24:08 -0700701 auto& setDisplaySurface(const sp<compositionengine::DisplaySurface>& displaySurface) {
Lloyd Pique2eef1d22018-09-18 21:30:04 -0700702 mCreationArgs.displaySurface = displaySurface;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800703 return *this;
704 }
705
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800706 auto& setSecure(bool secure) {
Lloyd Pique2eef1d22018-09-18 21:30:04 -0700707 mCreationArgs.isSecure = secure;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800708 return *this;
709 }
710
Peiyong Lin65248e02020-04-18 21:15:07 -0700711 auto& setPowerMode(hal::PowerMode mode) {
Lloyd Pique2eef1d22018-09-18 21:30:04 -0700712 mCreationArgs.initialPowerMode = mode;
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700713 return *this;
714 }
715
Valerie Hau9758ae02018-10-09 16:05:09 -0700716 auto& setHwcColorModes(
717 const std::unordered_map<ui::ColorMode, std::vector<ui::RenderIntent>>
718 hwcColorModes) {
719 mCreationArgs.hwcColorModes = hwcColorModes;
720 return *this;
721 }
722
723 auto& setHasWideColorGamut(bool hasWideColorGamut) {
724 mCreationArgs.hasWideColorGamut = hasWideColorGamut;
725 return *this;
726 }
727
Lloyd Pique33050472019-12-19 17:12:44 -0800728 auto& setPhysicalOrientation(ui::Rotation orientation) {
729 mCreationArgs.physicalOrientation = orientation;
730 return *this;
731 }
732
Ady Abraham690f4612021-07-01 23:24:03 -0700733 sp<DisplayDevice> inject() NO_THREAD_SAFETY_ANALYSIS {
Lloyd Pique9370a482019-10-03 17:58:30 -0700734 const auto displayId = mCreationArgs.compositionDisplay->getDisplayId();
735
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800736 DisplayDeviceState state;
Dominik Laskowski55c85402020-01-21 16:25:47 -0800737 if (const auto type = mCreationArgs.connectionType) {
Lloyd Pique9370a482019-10-03 17:58:30 -0700738 LOG_ALWAYS_FATAL_IF(!displayId);
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200739 const auto physicalId = PhysicalDisplayId::tryCast(*displayId);
740 LOG_ALWAYS_FATAL_IF(!physicalId);
Marin Shalamanov700e6392020-02-12 20:22:26 +0100741 LOG_ALWAYS_FATAL_IF(!mHwcDisplayId);
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200742 state.physical = {.id = *physicalId, .type = *type, .hwcDisplayId = *mHwcDisplayId};
Dominik Laskowski55c85402020-01-21 16:25:47 -0800743 }
744
Lloyd Pique2eef1d22018-09-18 21:30:04 -0700745 state.isSecure = mCreationArgs.isSecure;
746
Lloyd Pique9370a482019-10-03 17:58:30 -0700747 sp<DisplayDevice> device = new DisplayDevice(mCreationArgs);
Marin Shalamanov045b7002021-01-07 16:56:24 +0100748 if (!device->isVirtual()) {
749 device->setActiveMode(mActiveModeId);
750 }
Lloyd Pique2eef1d22018-09-18 21:30:04 -0700751 mFlinger.mutableDisplays().emplace(mDisplayToken, device);
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800752 mFlinger.mutableCurrentState().displays.add(mDisplayToken, state);
753 mFlinger.mutableDrawingState().displays.add(mDisplayToken, state);
754
Dominik Laskowski55c85402020-01-21 16:25:47 -0800755 if (const auto& physical = state.physical) {
756 mFlinger.mutablePhysicalDisplayTokens()[physical->id] = mDisplayToken;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800757 }
758
759 return device;
760 }
761
762 private:
763 TestableSurfaceFlinger& mFlinger;
764 sp<BBinder> mDisplayToken = new BBinder();
Lloyd Pique2eef1d22018-09-18 21:30:04 -0700765 DisplayDeviceCreationArgs mCreationArgs;
Peiyong Line9d809e2020-04-14 13:10:48 -0700766 const std::optional<hal::HWDisplayId> mHwcDisplayId;
Marin Shalamanov045b7002021-01-07 16:56:24 +0100767 DisplayModeId mActiveModeId;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800768 };
769
Dominik Laskowski8b01cc02020-07-14 19:02:41 -0700770private:
Dominik Laskowskie0e0cde2021-07-30 10:42:05 -0700771 void scheduleComposite(FrameHint) override {}
Dominik Laskowski8b01cc02020-07-14 19:02:41 -0700772 void setVsyncEnabled(bool) override {}
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100773 void changeRefreshRate(const Scheduler::RefreshRate&, Scheduler::ModeEvent) override {}
Dominik Laskowski8b01cc02020-07-14 19:02:41 -0700774 void kernelTimerChanged(bool) override {}
Ady Abraham62a0be22020-12-08 16:54:10 -0800775 void triggerOnFrameRateOverridesChanged() {}
Dominik Laskowski8b01cc02020-07-14 19:02:41 -0700776
Lloyd Pique90c115d2018-09-18 21:39:42 -0700777 surfaceflinger::test::Factory mFactory;
778 sp<SurfaceFlinger> mFlinger = new SurfaceFlinger(mFactory, SurfaceFlinger::SkipInitialization);
Dominik Laskowski7c9dbf92019-08-01 17:57:31 -0700779 TestableScheduler* mScheduler = nullptr;
Ady Abraham3efa3942021-06-24 19:01:25 -0700780 std::shared_ptr<scheduler::RefreshRateConfigs> mRefreshRateConfigs;
Lloyd Piquef58625d2017-12-19 13:22:33 -0800781};
782
783} // namespace android