blob: d292e0879da5411e1d05f1462827357ad6fa6b1c [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
Dominik Laskowski83bd7712022-01-07 14:30:53 -080019#include <algorithm>
20#include <variant>
21
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080022#include <compositionengine/Display.h>
Lloyd Pique9755fb72019-03-26 14:44:40 -070023#include <compositionengine/LayerFECompositionState.h>
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080024#include <compositionengine/OutputLayer.h>
Lloyd Pique70d91362018-10-18 16:02:55 -070025#include <compositionengine/impl/CompositionEngine.h>
Lloyd Pique9370a482019-10-03 17:58:30 -070026#include <compositionengine/impl/Display.h>
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080027#include <compositionengine/impl/OutputLayerCompositionState.h>
Dominik Laskowskif7a09ed2019-10-07 13:54:18 -070028#include <compositionengine/mock/DisplaySurface.h>
chaviw0ef7caa2021-01-05 11:04:50 -080029#include <gui/ScreenCaptureResults.h>
Lloyd Pique70d91362018-10-18 16:02:55 -070030
Lloyd Pique90c115d2018-09-18 21:39:42 -070031#include "BufferQueueLayer.h"
32#include "BufferStateLayer.h"
Lloyd Pique90c115d2018-09-18 21:39:42 -070033#include "ContainerLayer.h"
Lloyd Piquef58625d2017-12-19 13:22:33 -080034#include "DisplayDevice.h"
Vishnu Nairfa247b12020-02-11 08:58:26 -080035#include "EffectLayer.h"
Ady Abrahamc581d3c2020-08-06 17:34:27 -070036#include "FakeVsyncConfiguration.h"
Adithya Srinivasan2db3c1b2020-11-18 12:43:17 -080037#include "FrameTracer/FrameTracer.h"
Lloyd Piqued6b579f2018-04-06 15:29:10 -070038#include "Layer.h"
Lloyd Pique90c115d2018-09-18 21:39:42 -070039#include "NativeWindowSurface.h"
Dominik Laskowski9dab3432019-03-27 13:21:10 -070040#include "Scheduler/MessageQueue.h"
Steven Thomas2bbaabe2019-08-28 16:08:35 -070041#include "Scheduler/RefreshRateConfigs.h"
Lloyd Pique90c115d2018-09-18 21:39:42 -070042#include "StartPropertySetThread.h"
Lloyd Piquef58625d2017-12-19 13:22:33 -080043#include "SurfaceFlinger.h"
Lloyd Pique24f3bfe2019-10-02 19:29:10 -070044#include "SurfaceFlingerDefaultFactory.h"
Lloyd Pique90c115d2018-09-18 21:39:42 -070045#include "SurfaceInterceptor.h"
Dominik Laskowski7c9dbf92019-08-01 17:57:31 -070046#include "TestableScheduler.h"
Marin Shalamanov045b7002021-01-07 16:56:24 +010047#include "mock/DisplayHardware/MockComposer.h"
Adithya Srinivasan01189672020-10-20 14:23:05 -070048#include "mock/MockFrameTimeline.h"
Adithya Srinivasan2db3c1b2020-11-18 12:43:17 -080049#include "mock/MockFrameTracer.h"
Dominik Laskowski83bd7712022-01-07 14:30:53 -080050#include "mock/MockSchedulerCallback.h"
Yiwei Zhang7e666a52018-11-15 13:33:42 -080051
Lloyd Piquef58625d2017-12-19 13:22:33 -080052namespace android {
53
Lloyd Piquee39cad22017-12-20 17:01:29 -080054class EventThread;
55
Peiyong Lin833074a2018-08-28 11:53:54 -070056namespace renderengine {
Lloyd Pique90c115d2018-09-18 21:39:42 -070057
Lloyd Piquee39cad22017-12-20 17:01:29 -080058class RenderEngine;
Lloyd Pique90c115d2018-09-18 21:39:42 -070059
60} // namespace renderengine
Lloyd Piquee39cad22017-12-20 17:01:29 -080061
62namespace Hwc2 {
Lloyd Pique90c115d2018-09-18 21:39:42 -070063
Lloyd Piquee39cad22017-12-20 17:01:29 -080064class Composer;
Lloyd Pique90c115d2018-09-18 21:39:42 -070065
66} // namespace Hwc2
67
Peiyong Line9d809e2020-04-14 13:10:48 -070068namespace hal = android::hardware::graphics::composer::hal;
69
Lloyd Pique90c115d2018-09-18 21:39:42 -070070namespace surfaceflinger::test {
71
72class Factory final : public surfaceflinger::Factory {
73public:
74 ~Factory() = default;
75
Lloyd Pique90c115d2018-09-18 21:39:42 -070076 std::unique_ptr<HWComposer> createHWComposer(const std::string&) override {
Lloyd Pique90c115d2018-09-18 21:39:42 -070077 return nullptr;
78 }
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
Pablo Gamitoc351d6f2020-09-17 15:34:26 +000085 sp<SurfaceInterceptor> createSurfaceInterceptor() override {
86 return new android::impl::SurfaceInterceptor();
Lloyd Pique90c115d2018-09-18 21:39:42 -070087 }
88
89 sp<StartPropertySetThread> createStartPropertySetThread(bool timestampPropertyValue) override {
Lloyd Pique90c115d2018-09-18 21:39:42 -070090 return new StartPropertySetThread(timestampPropertyValue);
91 }
92
Lloyd Pique9370a482019-10-03 17:58:30 -070093 sp<DisplayDevice> createDisplayDevice(DisplayDeviceCreationArgs& creationArgs) override {
94 return new DisplayDevice(creationArgs);
Lloyd Pique90c115d2018-09-18 21:39:42 -070095 }
96
97 sp<GraphicBuffer> createGraphicBuffer(uint32_t width, uint32_t height, PixelFormat format,
98 uint32_t layerCount, uint64_t usage,
99 std::string requestorName) override {
Lloyd Pique90c115d2018-09-18 21:39:42 -0700100 return new GraphicBuffer(width, height, format, layerCount, usage, requestorName);
101 }
102
103 void createBufferQueue(sp<IGraphicBufferProducer>* outProducer,
104 sp<IGraphicBufferConsumer>* outConsumer,
105 bool consumerIsSurfaceFlinger) override {
Lloyd Piquee300b312019-10-03 13:03:45 -0700106 if (!mCreateBufferQueue) {
107 BufferQueue::createBufferQueue(outProducer, outConsumer, consumerIsSurfaceFlinger);
108 return;
109 }
Lloyd Pique90c115d2018-09-18 21:39:42 -0700110 mCreateBufferQueue(outProducer, outConsumer, consumerIsSurfaceFlinger);
111 }
112
Lloyd Piquee300b312019-10-03 13:03:45 -0700113 sp<IGraphicBufferProducer> createMonitoredProducer(const sp<IGraphicBufferProducer>& producer,
114 const sp<SurfaceFlinger>& flinger,
115 const wp<Layer>& layer) override {
116 return new MonitoredProducer(producer, flinger, layer);
117 }
118
119 sp<BufferLayerConsumer> createBufferLayerConsumer(const sp<IGraphicBufferConsumer>& consumer,
120 renderengine::RenderEngine& renderEngine,
121 uint32_t textureName, Layer* layer) override {
122 return new BufferLayerConsumer(consumer, renderEngine, textureName, layer);
123 }
124
Lloyd Pique90c115d2018-09-18 21:39:42 -0700125 std::unique_ptr<surfaceflinger::NativeWindowSurface> createNativeWindowSurface(
126 const sp<IGraphicBufferProducer>& producer) override {
127 if (!mCreateNativeWindowSurface) return nullptr;
128 return mCreateNativeWindowSurface(producer);
129 }
130
Lloyd Pique70d91362018-10-18 16:02:55 -0700131 std::unique_ptr<compositionengine::CompositionEngine> createCompositionEngine() override {
132 return compositionengine::impl::createCompositionEngine();
133 }
134
Lloyd Pique90c115d2018-09-18 21:39:42 -0700135 sp<BufferQueueLayer> createBufferQueueLayer(const LayerCreationArgs&) override {
Lloyd Pique90c115d2018-09-18 21:39:42 -0700136 return nullptr;
137 }
138
139 sp<BufferStateLayer> createBufferStateLayer(const LayerCreationArgs&) override {
Lloyd Pique90c115d2018-09-18 21:39:42 -0700140 return nullptr;
141 }
142
Vishnu Nairfa247b12020-02-11 08:58:26 -0800143 sp<EffectLayer> createEffectLayer(const LayerCreationArgs&) override { return nullptr; }
Lloyd Pique90c115d2018-09-18 21:39:42 -0700144
145 sp<ContainerLayer> createContainerLayer(const LayerCreationArgs&) override {
Lloyd Pique90c115d2018-09-18 21:39:42 -0700146 return nullptr;
147 }
148
Adithya Srinivasan2db3c1b2020-11-18 12:43:17 -0800149 std::unique_ptr<FrameTracer> createFrameTracer() override {
150 return std::make_unique<mock::FrameTracer>();
151 }
152
Adithya Srinivasan01189672020-10-20 14:23:05 -0700153 std::unique_ptr<frametimeline::FrameTimeline> createFrameTimeline(
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -0800154 std::shared_ptr<TimeStats> timeStats, pid_t surfaceFlingerPid = 0) override {
155 return std::make_unique<mock::FrameTimeline>(timeStats, surfaceFlingerPid);
Adithya Srinivasan01189672020-10-20 14:23:05 -0700156 }
157
Lloyd Pique90c115d2018-09-18 21:39:42 -0700158 using CreateBufferQueueFunction =
159 std::function<void(sp<IGraphicBufferProducer>* /* outProducer */,
160 sp<IGraphicBufferConsumer>* /* outConsumer */,
161 bool /* consumerIsSurfaceFlinger */)>;
162 CreateBufferQueueFunction mCreateBufferQueue;
163
164 using CreateNativeWindowSurfaceFunction =
165 std::function<std::unique_ptr<surfaceflinger::NativeWindowSurface>(
166 const sp<IGraphicBufferProducer>&)>;
167 CreateNativeWindowSurfaceFunction mCreateNativeWindowSurface;
Lloyd Pique70d91362018-10-18 16:02:55 -0700168
169 using CreateCompositionEngineFunction =
170 std::function<std::unique_ptr<compositionengine::CompositionEngine>()>;
171 CreateCompositionEngineFunction mCreateCompositionEngine;
Lloyd Pique90c115d2018-09-18 21:39:42 -0700172};
173
174} // namespace surfaceflinger::test
Lloyd Piquee39cad22017-12-20 17:01:29 -0800175
Dominik Laskowski83bd7712022-01-07 14:30:53 -0800176class TestableSurfaceFlinger {
Lloyd Piquef58625d2017-12-19 13:22:33 -0800177public:
Dominik Laskowskib7251f42020-04-20 17:42:59 -0700178 using HotplugEvent = SurfaceFlinger::HotplugEvent;
179
Dominik Laskowskif7a09ed2019-10-07 13:54:18 -0700180 SurfaceFlinger* flinger() { return mFlinger.get(); }
Dominik Laskowski068173d2021-08-11 17:22:59 -0700181 scheduler::TestableScheduler* scheduler() { return mScheduler; }
Dominik Laskowski7c9dbf92019-08-01 17:57:31 -0700182
Lloyd Piquef58625d2017-12-19 13:22:33 -0800183 // Extend this as needed for accessing SurfaceFlinger private (and public)
184 // functions.
185
Peiyong Lin833074a2018-08-28 11:53:54 -0700186 void setupRenderEngine(std::unique_ptr<renderengine::RenderEngine> renderEngine) {
Lloyd Piqueb97e04f2018-10-18 17:07:05 -0700187 mFlinger->mCompositionEngine->setRenderEngine(std::move(renderEngine));
Lloyd Piquee39cad22017-12-20 17:01:29 -0800188 }
189
190 void setupComposer(std::unique_ptr<Hwc2::Composer> composer) {
Lloyd Pique441d5042018-10-18 16:49:51 -0700191 mFlinger->mCompositionEngine->setHwComposer(
192 std::make_unique<impl::HWComposer>(std::move(composer)));
Lloyd Piquee39cad22017-12-20 17:01:29 -0800193 }
194
Alec Mourie4034bb2019-11-19 12:45:54 -0800195 void setupTimeStats(const std::shared_ptr<TimeStats>& timeStats) {
196 mFlinger->mCompositionEngine->setTimeStats(timeStats);
197 }
198
Dominik Laskowski83bd7712022-01-07 14:30:53 -0800199 enum class SchedulerCallbackImpl { kNoOp, kMock };
200
201 static constexpr struct OneDisplayMode {
202 } kOneDisplayMode;
203
204 static constexpr struct TwoDisplayModes {
205 } kTwoDisplayModes;
206
207 using RefreshRateConfigsPtr = std::shared_ptr<scheduler::RefreshRateConfigs>;
208
209 using DisplayModesVariant =
210 std::variant<OneDisplayMode, TwoDisplayModes, RefreshRateConfigsPtr>;
211
Ady Abraham8cb21882020-08-26 18:22:05 -0700212 void setupScheduler(std::unique_ptr<scheduler::VsyncController> vsyncController,
213 std::unique_ptr<scheduler::VSyncTracker> vsyncTracker,
Dominik Laskowski7c9dbf92019-08-01 17:57:31 -0700214 std::unique_ptr<EventThread> appEventThread,
Ady Abraham8a82ba62020-01-17 12:43:17 -0800215 std::unique_ptr<EventThread> sfEventThread,
Dominik Laskowski83bd7712022-01-07 14:30:53 -0800216 SchedulerCallbackImpl callbackImpl = SchedulerCallbackImpl::kNoOp,
217 DisplayModesVariant modesVariant = kOneDisplayMode) {
218 RefreshRateConfigsPtr configs;
219 if (std::holds_alternative<RefreshRateConfigsPtr>(modesVariant)) {
220 configs = std::move(std::get<RefreshRateConfigsPtr>(modesVariant));
221 } else {
222 DisplayModes modes = {DisplayMode::Builder(0)
223 .setId(DisplayModeId(0))
224 .setPhysicalDisplayId(PhysicalDisplayId::fromPort(0))
225 .setVsyncPeriod(16'666'667)
226 .setGroup(0)
227 .build()};
Ady Abrahamabc27602020-04-08 17:20:29 -0700228
Dominik Laskowski83bd7712022-01-07 14:30:53 -0800229 if (std::holds_alternative<TwoDisplayModes>(modesVariant)) {
230 modes.emplace_back(DisplayMode::Builder(1)
231 .setId(DisplayModeId(1))
232 .setPhysicalDisplayId(PhysicalDisplayId::fromPort(0))
233 .setVsyncPeriod(11'111'111)
234 .setGroup(0)
235 .build());
236 }
237
238 configs = std::make_shared<scheduler::RefreshRateConfigs>(modes, DisplayModeId(0));
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700239 }
240
Dominik Laskowski83bd7712022-01-07 14:30:53 -0800241 const auto currFps = configs->getCurrentRefreshRate().getFps();
Ady Abraham3efa3942021-06-24 19:01:25 -0700242 mFlinger->mVsyncConfiguration = mFactory.createVsyncConfiguration(currFps);
Ady Abraham23ea9da2021-07-14 16:32:56 -0700243 mFlinger->mVsyncModulator = sp<scheduler::VsyncModulator>::make(
244 mFlinger->mVsyncConfiguration->getCurrentConfigs());
Marin Shalamanov68a94092020-11-24 17:48:00 +0100245 mFlinger->mRefreshRateStats =
246 std::make_unique<scheduler::RefreshRateStats>(*mFlinger->mTimeStats, currFps,
Dominik Laskowski83bd7712022-01-07 14:30:53 -0800247 hal::PowerMode::OFF);
248
249 using Callback = scheduler::ISchedulerCallback;
250 Callback& callback = callbackImpl == SchedulerCallbackImpl::kNoOp
251 ? static_cast<Callback&>(mNoOpSchedulerCallback)
252 : static_cast<Callback&>(mSchedulerCallback);
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700253
Dominik Laskowski068173d2021-08-11 17:22:59 -0700254 mScheduler = new scheduler::TestableScheduler(std::move(vsyncController),
Dominik Laskowski83bd7712022-01-07 14:30:53 -0800255 std::move(vsyncTracker), std::move(configs),
256 callback);
Dominik Laskowski7c9dbf92019-08-01 17:57:31 -0700257
Dominik Laskowski98041832019-08-01 18:35:59 -0700258 mFlinger->mAppConnectionHandle = mScheduler->createConnection(std::move(appEventThread));
259 mFlinger->mSfConnectionHandle = mScheduler->createConnection(std::move(sfEventThread));
Dominik Laskowskif7a09ed2019-10-07 13:54:18 -0700260 resetScheduler(mScheduler);
Dominik Laskowski7c9dbf92019-08-01 17:57:31 -0700261 }
262
Dominik Laskowski068173d2021-08-11 17:22:59 -0700263 void resetScheduler(scheduler::Scheduler* scheduler) { mFlinger->mScheduler.reset(scheduler); }
Dominik Laskowskif7a09ed2019-10-07 13:54:18 -0700264
Dominik Laskowski83bd7712022-01-07 14:30:53 -0800265 scheduler::TestableScheduler& mutableScheduler() { return *mScheduler; }
266 scheduler::mock::SchedulerCallback& mockSchedulerCallback() { return mSchedulerCallback; }
Ady Abraham44e9f3b2021-02-16 15:22:58 -0800267
Lloyd Pique90c115d2018-09-18 21:39:42 -0700268 using CreateBufferQueueFunction = surfaceflinger::test::Factory::CreateBufferQueueFunction;
Lloyd Pique5b36f3f2018-01-17 11:57:07 -0800269 void setCreateBufferQueueFunction(CreateBufferQueueFunction f) {
Lloyd Pique90c115d2018-09-18 21:39:42 -0700270 mFactory.mCreateBufferQueue = f;
Lloyd Pique5b36f3f2018-01-17 11:57:07 -0800271 }
272
Lloyd Pique90c115d2018-09-18 21:39:42 -0700273 using CreateNativeWindowSurfaceFunction =
274 surfaceflinger::test::Factory::CreateNativeWindowSurfaceFunction;
Lloyd Pique0b1fe702018-01-22 18:03:16 -0800275 void setCreateNativeWindowSurface(CreateNativeWindowSurfaceFunction f) {
Lloyd Pique90c115d2018-09-18 21:39:42 -0700276 mFactory.mCreateNativeWindowSurface = f;
Lloyd Pique0b1fe702018-01-22 18:03:16 -0800277 }
278
Daniel Solomon42d04562019-01-20 21:03:19 -0800279 void setInternalDisplayPrimaries(const ui::DisplayPrimaries& primaries) {
280 memcpy(&mFlinger->mInternalDisplayPrimaries, &primaries, sizeof(ui::DisplayPrimaries));
281 }
282
Dominik Laskowskib7251f42020-04-20 17:42:59 -0700283 static auto& mutableLayerDrawingState(const sp<Layer>& layer) { return layer->mDrawingState; }
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700284
Valerie Haud251afb2019-03-29 14:19:02 -0700285 auto& mutableStateLock() { return mFlinger->mStateLock; }
286
Dominik Laskowskib7251f42020-04-20 17:42:59 -0700287 static auto findOutputLayerForDisplay(const sp<Layer>& layer,
288 const sp<const DisplayDevice>& display) {
289 return layer->findOutputLayerForDisplay(display.get());
290 }
291
292 static void setLayerSidebandStream(const sp<Layer>& layer,
293 const sp<NativeHandle>& sidebandStream) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800294 layer->mDrawingState.sidebandStream = sidebandStream;
Lloyd Pique0b785d82018-12-04 17:25:27 -0800295 layer->mSidebandStream = sidebandStream;
Lloyd Piquede196652020-01-22 17:29:58 -0800296 layer->editCompositionState()->sidebandStream = sidebandStream;
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700297 }
298
Leon Scroggins III2e1aa182021-12-01 17:33:12 -0500299 void setLayerCompositionType(const sp<Layer>& layer,
300 aidl::android::hardware::graphics::composer3::Composition type) {
Dominik Laskowskib7251f42020-04-20 17:42:59 -0700301 auto outputLayer = findOutputLayerForDisplay(layer, mFlinger->getDefaultDisplayDevice());
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800302 LOG_ALWAYS_FATAL_IF(!outputLayer);
303 auto& state = outputLayer->editState();
304 LOG_ALWAYS_FATAL_IF(!outputLayer->getState().hwc);
Peiyong Line9d809e2020-04-14 13:10:48 -0700305 (*state.hwc).hwcCompositionType = type;
Dominik Laskowskib7251f42020-04-20 17:42:59 -0700306 }
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800307
Dominik Laskowskib7251f42020-04-20 17:42:59 -0700308 static void setLayerPotentialCursor(const sp<Layer>& layer, bool potentialCursor) {
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700309 layer->mPotentialCursor = potentialCursor;
310 }
311
Garfield Tande619fa2020-10-02 17:13:53 -0700312 static void setLayerDrawingParent(const sp<Layer>& layer, const sp<Layer>& drawingParent) {
Rob Carrc6d2d2b2021-10-25 16:51:49 +0000313 layer->mDrawingParent = drawingParent;
Garfield Tande619fa2020-10-02 17:13:53 -0700314 }
315
Lloyd Piquef58625d2017-12-19 13:22:33 -0800316 /* ------------------------------------------------------------------------
317 * Forwarding for functions being tested
318 */
Lloyd Pique0b1fe702018-01-22 18:03:16 -0800319
Dominik Laskowskie0e0cde2021-07-30 10:42:05 -0700320 nsecs_t commit() {
321 constexpr int64_t kVsyncId = 123;
322 const nsecs_t now = systemTime();
323 const nsecs_t expectedVsyncTime = now + 10'000'000;
324 mFlinger->commit(now, kVsyncId, expectedVsyncTime);
325 return now;
326 }
327
328 void commitAndComposite() { mFlinger->composite(commit()); }
329
Lloyd Piquea482f992018-01-22 19:00:34 -0800330 auto createDisplay(const String8& displayName, bool secure) {
331 return mFlinger->createDisplay(displayName, secure);
332 }
333
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700334 auto destroyDisplay(const sp<IBinder>& displayToken) {
335 return mFlinger->destroyDisplay(displayToken);
336 }
Lloyd Piquea482f992018-01-22 19:00:34 -0800337
Marin Shalamanovc7e9f822021-11-16 20:07:55 +0100338 auto getDisplay(const sp<IBinder>& displayToken) {
339 Mutex::Autolock lock(mFlinger->mStateLock);
340 return mFlinger->getDisplayDeviceLocked(displayToken);
341 }
342
Dominik Laskowski13948602021-03-08 20:48:28 -0800343 void enableHalVirtualDisplays(bool enable) { mFlinger->enableHalVirtualDisplays(enable); }
344
Lloyd Pique9370a482019-10-03 17:58:30 -0700345 auto setupNewDisplayDeviceInternal(
346 const wp<IBinder>& displayToken,
347 std::shared_ptr<compositionengine::Display> compositionDisplay,
348 const DisplayDeviceState& state,
349 const sp<compositionengine::DisplaySurface>& dispSurface,
Dominik Laskowski0a1435d2020-04-21 00:27:31 -0700350 const sp<IGraphicBufferProducer>& producer) NO_THREAD_SAFETY_ANALYSIS {
Lloyd Pique9370a482019-10-03 17:58:30 -0700351 return mFlinger->setupNewDisplayDeviceInternal(displayToken, compositionDisplay, state,
352 dispSurface, producer);
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800353 }
354
Dominik Laskowski9e168db2021-05-27 16:05:12 -0700355 auto commitTransactionsLocked(uint32_t transactionFlags) {
356 Mutex::Autolock lock(mFlinger->mStateLock);
357 return mFlinger->commitTransactionsLocked(transactionFlags);
Lloyd Pique0b1fe702018-01-22 18:03:16 -0800358 }
Lloyd Piquef58625d2017-12-19 13:22:33 -0800359
Dominik Laskowskiebc8d3a2021-04-16 23:18:31 -0700360 void onComposerHalHotplug(hal::HWDisplayId hwcDisplayId, hal::Connection connection) {
361 mFlinger->onComposerHalHotplug(hwcDisplayId, connection);
Lloyd Pique6cf11032018-01-22 18:57:44 -0800362 }
363
Vishnu Nairce5d0cc2019-02-28 14:38:41 -0800364 auto setDisplayStateLocked(const DisplayState& s) {
Dominik Laskowski9e168db2021-05-27 16:05:12 -0700365 Mutex::Autolock lock(mFlinger->mStateLock);
Vishnu Nairce5d0cc2019-02-28 14:38:41 -0800366 return mFlinger->setDisplayStateLocked(s);
367 }
Lloyd Pique9d9cf402018-02-16 17:47:13 -0800368
Dominik Laskowski83b88212018-12-11 13:34:06 -0800369 // Allow reading display state without locking, as if called on the SF main thread.
370 auto onInitializeDisplays() NO_THREAD_SAFETY_ANALYSIS {
371 return mFlinger->onInitializeDisplays();
372 }
Lloyd Pique86016da2018-03-01 16:09:38 -0800373
Lais Andrade3a6e47d2020-04-02 11:20:16 +0100374 auto notifyPowerBoost(int32_t boostId) { return mFlinger->notifyPowerBoost(boostId); }
375
Alec Mouricdf16792021-12-10 13:16:06 -0800376 auto setDisplayBrightness(const sp<IBinder>& display,
377 const gui::DisplayBrightness& brightness) {
378 return mFlinger->setDisplayBrightness(display, brightness);
379 }
380
Dominik Laskowski83b88212018-12-11 13:34:06 -0800381 // Allow reading display state without locking, as if called on the SF main thread.
382 auto setPowerModeInternal(const sp<DisplayDevice>& display,
Peiyong Lin65248e02020-04-18 21:15:07 -0700383 hal::PowerMode mode) NO_THREAD_SAFETY_ANALYSIS {
Dominik Laskowskie9774092018-12-11 10:04:24 -0800384 return mFlinger->setPowerModeInternal(display, mode);
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -0800385 }
386
chaviw426b5c02020-07-27 11:20:15 -0700387 auto renderScreenImplLocked(const RenderArea& renderArea,
388 SurfaceFlinger::TraverseLayersFunction traverseLayers,
Alec Mouria90a5702021-04-16 16:36:21 +0000389 const std::shared_ptr<renderengine::ExternalTexture>& buffer,
390 bool forSystem, bool regionSampling) {
chaviw426b5c02020-07-27 11:20:15 -0700391 ScreenCaptureResults captureResults;
chaviwc6ad8af2020-08-03 11:33:30 -0700392 return mFlinger->renderScreenImplLocked(renderArea, traverseLayers, buffer, forSystem,
chaviw17ac24b2021-01-28 18:50:05 -0800393 regionSampling, false /* grayscale */,
394 captureResults);
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700395 }
396
chaviw4b9d5e12020-08-04 18:30:35 -0700397 auto traverseLayersInLayerStack(ui::LayerStack layerStack, int32_t uid,
Marin Shalamanov1c434292020-06-12 01:47:29 +0200398 const LayerVector::Visitor& visitor) {
chaviw4b9d5e12020-08-04 18:30:35 -0700399 return mFlinger->SurfaceFlinger::traverseLayersInLayerStack(layerStack, uid, visitor);
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700400 }
401
Daniel Solomon42d04562019-01-20 21:03:19 -0800402 auto getDisplayNativePrimaries(const sp<IBinder>& displayToken,
403 ui::DisplayPrimaries &primaries) {
404 return mFlinger->SurfaceFlinger::getDisplayNativePrimaries(displayToken, primaries);
405 }
406
Arthur Hung58144272021-01-16 03:43:53 +0000407 auto& getTransactionQueue() { return mFlinger->mTransactionQueue; }
408 auto& getPendingTransactionQueue() { return mFlinger->mPendingTransactionQueues; }
ramindani4d48f902021-09-20 21:07:45 +0000409 auto& getTransactionCommittedSignals() { return mFlinger->mTransactionCommittedSignals; }
Valerie Haud251afb2019-03-29 14:19:02 -0700410
Siarhei Vishniakoufc434ac2021-01-13 10:28:00 -1000411 auto setTransactionState(
412 const FrameTimelineInfo& frameTimelineInfo, const Vector<ComposerState>& states,
413 const Vector<DisplayState>& displays, uint32_t flags, const sp<IBinder>& applyToken,
414 const InputWindowCommands& inputWindowCommands, int64_t desiredPresentTime,
415 bool isAutoTimestamp, const client_cache_t& uncacheBuffer, bool hasListenerCallbacks,
416 std::vector<ListenerCallbacks>& listenerCallbacks, uint64_t transactionId) {
417 return mFlinger->setTransactionState(frameTimelineInfo, states, displays, flags, applyToken,
418 inputWindowCommands, desiredPresentTime,
Ady Abrahamf0c56492020-12-17 18:04:15 -0800419 isAutoTimestamp, uncacheBuffer, hasListenerCallbacks,
420 listenerCallbacks, transactionId);
Valerie Haud251afb2019-03-29 14:19:02 -0700421 }
422
Vishnu Nair7891e962021-11-11 12:07:21 -0800423 auto flushTransactionQueues() { return mFlinger->flushTransactionQueues(0); };
Valerie Haud251afb2019-03-29 14:19:02 -0700424
Eduardo Rochaac867192020-09-14 20:12:17 +0000425 auto onTransact(uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) {
426 return mFlinger->onTransact(code, data, reply, flags);
427 }
428
Ana Krulec31f2b3c2020-12-14 14:30:09 -0800429 auto getGPUContextPriority() { return mFlinger->getGPUContextPriority(); }
430
Ady Abraham899dcdb2021-06-15 16:56:21 -0700431 auto calculateMaxAcquiredBufferCount(Fps refreshRate,
432 std::chrono::nanoseconds presentLatency) const {
433 return SurfaceFlinger::calculateMaxAcquiredBufferCount(refreshRate, presentLatency);
Ady Abraham564f9de2021-02-03 18:34:33 -0800434 }
435
Marin Shalamanovc7e9f822021-11-16 20:07:55 +0100436 auto setDesiredDisplayModeSpecs(const sp<IBinder>& displayToken, ui::DisplayModeId defaultMode,
437 bool allowGroupSwitching, float primaryRefreshRateMin,
438 float primaryRefreshRateMax, float appRequestRefreshRateMin,
439 float appRequestRefreshRateMax) {
440 return mFlinger->setDesiredDisplayModeSpecs(displayToken, defaultMode, allowGroupSwitching,
441 primaryRefreshRateMin, primaryRefreshRateMax,
442 appRequestRefreshRateMin,
443 appRequestRefreshRateMax);
444 }
445
446 void onActiveDisplayChanged(const sp<DisplayDevice>& activeDisplay) {
447 Mutex::Autolock lock(mFlinger->mStateLock);
448 mFlinger->onActiveDisplayChangedLocked(activeDisplay);
449 }
450
Lloyd Pique86016da2018-03-01 16:09:38 -0800451 /* ------------------------------------------------------------------------
452 * Read-only access to private data to assert post-conditions.
453 */
454
455 const auto& getAnimFrameTracker() const { return mFlinger->mAnimFrameTracker; }
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -0800456 const auto& getHasPoweredOff() const { return mFlinger->mHasPoweredOff; }
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -0800457 const auto& getVisibleRegionsDirty() const { return mFlinger->mVisibleRegionsDirty; }
Lloyd Pique441d5042018-10-18 16:49:51 -0700458 auto& getHwComposer() const {
459 return static_cast<impl::HWComposer&>(mFlinger->getHwComposer());
460 }
Lloyd Pique9370a482019-10-03 17:58:30 -0700461 auto& getCompositionEngine() const { return mFlinger->getCompositionEngine(); }
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -0800462
Lloyd Pique86016da2018-03-01 16:09:38 -0800463 const auto& getCompositorTiming() const { return mFlinger->getBE().mCompositorTiming; }
464
Adithya Srinivasanb238cd52021-02-04 17:54:05 +0000465 mock::FrameTracer* getFrameTracer() const {
466 return static_cast<mock::FrameTracer*>(mFlinger->mFrameTracer.get());
467 }
468
Lloyd Piquef58625d2017-12-19 13:22:33 -0800469 /* ------------------------------------------------------------------------
470 * Read-write access to private data to set up preconditions and assert
471 * post-conditions.
472 */
Lloyd Pique0b1fe702018-01-22 18:03:16 -0800473
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800474 auto& mutableHasWideColorDisplay() { return SurfaceFlinger::hasWideColorDisplay; }
475
Lloyd Piquee39cad22017-12-20 17:01:29 -0800476 auto& mutableCurrentState() { return mFlinger->mCurrentState; }
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800477 auto& mutableDisplayColorSetting() { return mFlinger->mDisplayColorSetting; }
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700478 auto& mutableDisplays() { return mFlinger->mDisplays; }
Lloyd Piquee39cad22017-12-20 17:01:29 -0800479 auto& mutableDrawingState() { return mFlinger->mDrawingState; }
Dominik Laskowski8da6b0e2021-05-12 15:34:13 -0700480 auto& mutableGeometryDirty() { return mFlinger->mGeometryDirty; }
Lloyd Pique0b1fe702018-01-22 18:03:16 -0800481 auto& mutableInterceptor() { return mFlinger->mInterceptor; }
Lloyd Pique6cf11032018-01-22 18:57:44 -0800482 auto& mutableMainThreadId() { return mFlinger->mMainThreadId; }
Lloyd Pique0b1fe702018-01-22 18:03:16 -0800483 auto& mutablePendingHotplugEvents() { return mFlinger->mPendingHotplugEvents; }
Dominik Laskowski075d3172018-05-24 15:50:06 -0700484 auto& mutablePhysicalDisplayTokens() { return mFlinger->mPhysicalDisplayTokens; }
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700485 auto& mutableTexturePool() { return mFlinger->mTexturePool; }
Lloyd Pique0b1fe702018-01-22 18:03:16 -0800486 auto& mutableTransactionFlags() { return mFlinger->mTransactionFlags; }
Peiyong Lin74ca2f42019-01-14 19:36:57 -0800487 auto& mutablePowerAdvisor() { return mFlinger->mPowerAdvisor; }
Lloyd Pique4fe29402019-08-12 16:51:24 -0700488 auto& mutableDebugDisableHWC() { return mFlinger->mDebugDisableHWC; }
Garfield Tan9c9c1912021-07-19 12:02:16 -0700489 auto& mutableMaxRenderTargetSize() { return mFlinger->mMaxRenderTargetSize; }
Lloyd Piquee39cad22017-12-20 17:01:29 -0800490
Lloyd Pique441d5042018-10-18 16:49:51 -0700491 auto& mutableHwcDisplayData() { return getHwComposer().mDisplayData; }
492 auto& mutableHwcPhysicalDisplayIdMap() { return getHwComposer().mPhysicalDisplayIdMap; }
Dominik Laskowskif8db0f02021-04-19 11:05:25 -0700493 auto& mutablePrimaryHwcDisplayId() { return getHwComposer().mPrimaryHwcDisplayId; }
Ady Abrahamadc914c2021-10-13 17:04:27 -0700494 auto& mutableActiveDisplayToken() { return mFlinger->mActiveDisplayToken; }
Lloyd Piquea618d852018-01-17 11:52:30 -0800495
Valerie Hau09e60052019-12-15 14:51:15 -0800496 auto fromHandle(const sp<IBinder>& handle) {
Valerie Hau09e60052019-12-15 14:51:15 -0800497 return mFlinger->fromHandle(handle);
498 }
499
Lloyd Piquee39cad22017-12-20 17:01:29 -0800500 ~TestableSurfaceFlinger() {
501 // All these pointer and container clears help ensure that GMock does
502 // not report a leaked object, since the SurfaceFlinger instance may
503 // still be referenced by something despite our best efforts to destroy
504 // it after each test is done.
505 mutableDisplays().clear();
Ady Abraham2939f092019-03-04 17:44:06 -0800506 mutableCurrentState().displays.clear();
507 mutableDrawingState().displays.clear();
Pablo Gamito5cfc6e52020-09-10 11:18:03 +0000508 mutableInterceptor().clear();
Lloyd Pique2eb71eb2019-08-13 13:23:03 -0700509 mFlinger->mScheduler.reset();
Lloyd Pique441d5042018-10-18 16:49:51 -0700510 mFlinger->mCompositionEngine->setHwComposer(std::unique_ptr<HWComposer>());
Lloyd Piqueb97e04f2018-10-18 17:07:05 -0700511 mFlinger->mCompositionEngine->setRenderEngine(
512 std::unique_ptr<renderengine::RenderEngine>());
Lloyd Piquee39cad22017-12-20 17:01:29 -0800513 }
Lloyd Piquef58625d2017-12-19 13:22:33 -0800514
Lloyd Piquea618d852018-01-17 11:52:30 -0800515 /* ------------------------------------------------------------------------
516 * Wrapper classes for Read-write access to private data to set up
517 * preconditions and assert post-conditions.
518 */
Ana Krulec4593b692019-01-11 22:07:25 -0800519 struct HWC2Display : public HWC2::impl::Display {
Peiyong Lin74ca2f42019-01-14 19:36:57 -0800520 HWC2Display(Hwc2::Composer& composer,
Peiyong Line9d809e2020-04-14 13:10:48 -0700521 const std::unordered_set<hal::Capability>& capabilities, hal::HWDisplayId id,
522 hal::DisplayType type)
Peiyong Lin74ca2f42019-01-14 19:36:57 -0800523 : HWC2::impl::Display(composer, capabilities, id, type) {}
Lloyd Piquea618d852018-01-17 11:52:30 -0800524 ~HWC2Display() {
525 // Prevents a call to disable vsyncs.
Peiyong Line9d809e2020-04-14 13:10:48 -0700526 mType = hal::DisplayType::INVALID;
Lloyd Piquea618d852018-01-17 11:52:30 -0800527 }
528
529 auto& mutableIsConnected() { return this->mIsConnected; }
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700530 auto& mutableLayers() { return this->mLayers; }
Lloyd Piquea618d852018-01-17 11:52:30 -0800531 };
532
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800533 class FakeHwcDisplayInjector {
534 public:
Peiyong Line9d809e2020-04-14 13:10:48 -0700535 static constexpr hal::HWDisplayId DEFAULT_HWC_DISPLAY_ID = 1000;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800536 static constexpr int32_t DEFAULT_WIDTH = 1920;
537 static constexpr int32_t DEFAULT_HEIGHT = 1280;
Marin Shalamanovc7e9f822021-11-16 20:07:55 +0100538 static constexpr int32_t DEFAULT_VSYNC_PERIOD = 16'666'667;
Ady Abraham2139f732019-11-13 18:56:40 -0800539 static constexpr int32_t DEFAULT_CONFIG_GROUP = 7;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800540 static constexpr int32_t DEFAULT_DPI = 320;
Peiyong Line9d809e2020-04-14 13:10:48 -0700541 static constexpr hal::HWConfigId DEFAULT_ACTIVE_CONFIG = 0;
Peiyong Lin65248e02020-04-18 21:15:07 -0700542 static constexpr hal::PowerMode DEFAULT_POWER_MODE = hal::PowerMode::ON;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800543
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200544 FakeHwcDisplayInjector(HalDisplayId displayId, hal::DisplayType hwcDisplayType,
545 bool isPrimary)
Dominik Laskowski075d3172018-05-24 15:50:06 -0700546 : mDisplayId(displayId), mHwcDisplayType(hwcDisplayType), mIsPrimary(isPrimary) {}
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800547
Peiyong Line9d809e2020-04-14 13:10:48 -0700548 auto& setHwcDisplayId(hal::HWDisplayId displayId) {
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800549 mHwcDisplayId = displayId;
550 return *this;
551 }
552
553 auto& setWidth(int32_t width) {
554 mWidth = width;
555 return *this;
556 }
557
558 auto& setHeight(int32_t height) {
559 mHeight = height;
560 return *this;
561 }
562
Marin Shalamanov045b7002021-01-07 16:56:24 +0100563 auto& setVsyncPeriod(int32_t vsyncPeriod) {
564 mVsyncPeriod = vsyncPeriod;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800565 return *this;
566 }
567
568 auto& setDpiX(int32_t dpi) {
569 mDpiX = dpi;
570 return *this;
571 }
572
573 auto& setDpiY(int32_t dpi) {
574 mDpiY = dpi;
575 return *this;
576 }
577
Peiyong Line9d809e2020-04-14 13:10:48 -0700578 auto& setActiveConfig(hal::HWConfigId config) {
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800579 mActiveConfig = config;
580 return *this;
581 }
582
Peiyong Line9d809e2020-04-14 13:10:48 -0700583 auto& setCapabilities(const std::unordered_set<hal::Capability>* capabilities) {
Lloyd Piquee22f0332018-07-16 16:35:56 -0700584 mCapabilities = capabilities;
585 return *this;
586 }
587
Peiyong Lin65248e02020-04-18 21:15:07 -0700588 auto& setPowerMode(hal::PowerMode mode) {
Peiyong Lin1336e6e2019-05-28 09:23:50 -0700589 mPowerMode = mode;
590 return *this;
591 }
592
Marin Shalamanov045b7002021-01-07 16:56:24 +0100593 void inject(TestableSurfaceFlinger* flinger, Hwc2::mock::Composer* composer) {
594 using ::testing::_;
595 using ::testing::DoAll;
596 using ::testing::Return;
597 using ::testing::SetArgPointee;
598
Peiyong Line9d809e2020-04-14 13:10:48 -0700599 static const std::unordered_set<hal::Capability> defaultCapabilities;
Lloyd Piquee22f0332018-07-16 16:35:56 -0700600 if (mCapabilities == nullptr) mCapabilities = &defaultCapabilities;
601
602 // Caution - Make sure that any values passed by reference here do
603 // not refer to an instance owned by FakeHwcDisplayInjector. This
604 // class has temporary lifetime, while the constructed HWC2::Display
605 // is much longer lived.
Peiyong Lin74ca2f42019-01-14 19:36:57 -0800606 auto display = std::make_unique<HWC2Display>(*composer, *mCapabilities, mHwcDisplayId,
607 mHwcDisplayType);
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800608
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800609 display->mutableIsConnected() = true;
Peiyong Lin65248e02020-04-18 21:15:07 -0700610 display->setPowerMode(mPowerMode);
Peiyong Linbdd08cc2019-12-17 21:35:14 -0800611 flinger->mutableHwcDisplayData()[mDisplayId].hwcDisplay = std::move(display);
Dominik Laskowski075d3172018-05-24 15:50:06 -0700612
Marin Shalamanov045b7002021-01-07 16:56:24 +0100613 EXPECT_CALL(*composer, getDisplayConfigs(mHwcDisplayId, _))
614 .WillRepeatedly(
615 DoAll(SetArgPointee<1>(std::vector<hal::HWConfigId>{mActiveConfig}),
616 Return(hal::Error::NONE)));
617
618 EXPECT_CALL(*composer,
619 getDisplayAttribute(mHwcDisplayId, mActiveConfig, hal::Attribute::WIDTH, _))
620 .WillRepeatedly(DoAll(SetArgPointee<3>(mWidth), Return(hal::Error::NONE)));
621
622 EXPECT_CALL(*composer,
623 getDisplayAttribute(mHwcDisplayId, mActiveConfig, hal::Attribute::HEIGHT,
624 _))
625 .WillRepeatedly(DoAll(SetArgPointee<3>(mHeight), Return(hal::Error::NONE)));
626
627 EXPECT_CALL(*composer,
628 getDisplayAttribute(mHwcDisplayId, mActiveConfig,
629 hal::Attribute::VSYNC_PERIOD, _))
630 .WillRepeatedly(
631 DoAll(SetArgPointee<3>(mVsyncPeriod), Return(hal::Error::NONE)));
632
633 EXPECT_CALL(*composer,
634 getDisplayAttribute(mHwcDisplayId, mActiveConfig, hal::Attribute::DPI_X, _))
635 .WillRepeatedly(DoAll(SetArgPointee<3>(mDpiX), Return(hal::Error::NONE)));
636
637 EXPECT_CALL(*composer,
638 getDisplayAttribute(mHwcDisplayId, mActiveConfig, hal::Attribute::DPI_Y, _))
639 .WillRepeatedly(DoAll(SetArgPointee<3>(mDpiY), Return(hal::Error::NONE)));
640
641 EXPECT_CALL(*composer,
642 getDisplayAttribute(mHwcDisplayId, mActiveConfig,
643 hal::Attribute::CONFIG_GROUP, _))
644 .WillRepeatedly(
645 DoAll(SetArgPointee<3>(mConfigGroup), Return(hal::Error::NONE)));
Marin Shalamanov3ea1d602020-12-16 19:59:39 +0100646
Peiyong Line9d809e2020-04-14 13:10:48 -0700647 if (mHwcDisplayType == hal::DisplayType::PHYSICAL) {
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200648 const auto physicalId = PhysicalDisplayId::tryCast(mDisplayId);
649 LOG_ALWAYS_FATAL_IF(!physicalId);
650 flinger->mutableHwcPhysicalDisplayIdMap().emplace(mHwcDisplayId, *physicalId);
Marin Shalamanov6e840172020-12-14 22:13:28 +0100651 if (mIsPrimary) {
Dominik Laskowskif8db0f02021-04-19 11:05:25 -0700652 flinger->mutablePrimaryHwcDisplayId() = mHwcDisplayId;
Marin Shalamanov6e840172020-12-14 22:13:28 +0100653 } else {
Dominik Laskowskif8db0f02021-04-19 11:05:25 -0700654 // If there is an external HWC display, there should always be a primary ID
Marin Shalamanov6e840172020-12-14 22:13:28 +0100655 // as well. Set it to some arbitrary value.
Dominik Laskowskif8db0f02021-04-19 11:05:25 -0700656 auto& primaryId = flinger->mutablePrimaryHwcDisplayId();
657 if (!primaryId) primaryId = mHwcDisplayId - 1;
Marin Shalamanov6e840172020-12-14 22:13:28 +0100658 }
Dominik Laskowski075d3172018-05-24 15:50:06 -0700659 }
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800660 }
661
662 private:
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200663 const HalDisplayId mDisplayId;
Peiyong Line9d809e2020-04-14 13:10:48 -0700664 const hal::DisplayType mHwcDisplayType;
Dominik Laskowski075d3172018-05-24 15:50:06 -0700665 const bool mIsPrimary;
666
Peiyong Line9d809e2020-04-14 13:10:48 -0700667 hal::HWDisplayId mHwcDisplayId = DEFAULT_HWC_DISPLAY_ID;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800668 int32_t mWidth = DEFAULT_WIDTH;
669 int32_t mHeight = DEFAULT_HEIGHT;
Marin Shalamanov045b7002021-01-07 16:56:24 +0100670 int32_t mVsyncPeriod = DEFAULT_VSYNC_PERIOD;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800671 int32_t mDpiX = DEFAULT_DPI;
672 int32_t mDpiY = DEFAULT_DPI;
Marin Shalamanov045b7002021-01-07 16:56:24 +0100673 int32_t mConfigGroup = DEFAULT_CONFIG_GROUP;
Peiyong Line9d809e2020-04-14 13:10:48 -0700674 hal::HWConfigId mActiveConfig = DEFAULT_ACTIVE_CONFIG;
Peiyong Lin65248e02020-04-18 21:15:07 -0700675 hal::PowerMode mPowerMode = DEFAULT_POWER_MODE;
Peiyong Line9d809e2020-04-14 13:10:48 -0700676 const std::unordered_set<hal::Capability>* mCapabilities = nullptr;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800677 };
678
679 class FakeDisplayDeviceInjector {
680 public:
Dominik Laskowski075d3172018-05-24 15:50:06 -0700681 FakeDisplayDeviceInjector(TestableSurfaceFlinger& flinger,
Lloyd Pique9370a482019-10-03 17:58:30 -0700682 std::shared_ptr<compositionengine::Display> compositionDisplay,
Marin Shalamanov228f46b2021-01-28 21:11:45 +0100683 std::optional<ui::DisplayConnectionType> connectionType,
Peiyong Line9d809e2020-04-14 13:10:48 -0700684 std::optional<hal::HWDisplayId> hwcDisplayId, bool isPrimary)
Lloyd Pique9370a482019-10-03 17:58:30 -0700685 : mFlinger(flinger),
Marin Shalamanov12c9e5a2021-01-07 00:25:35 +0100686 mCreationArgs(flinger.mFlinger.get(), flinger.mFlinger->getHwComposer(),
687 mDisplayToken, compositionDisplay),
Marin Shalamanov700e6392020-02-12 20:22:26 +0100688 mHwcDisplayId(hwcDisplayId) {
Dominik Laskowski55c85402020-01-21 16:25:47 -0800689 mCreationArgs.connectionType = connectionType;
Dominik Laskowski075d3172018-05-24 15:50:06 -0700690 mCreationArgs.isPrimary = isPrimary;
691 }
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800692
693 sp<IBinder> token() const { return mDisplayToken; }
694
695 DisplayDeviceState& mutableDrawingDisplayState() {
696 return mFlinger.mutableDrawingState().displays.editValueFor(mDisplayToken);
697 }
698
699 DisplayDeviceState& mutableCurrentDisplayState() {
700 return mFlinger.mutableCurrentState().displays.editValueFor(mDisplayToken);
701 }
702
Lloyd Pique9d9cf402018-02-16 17:47:13 -0800703 const auto& getDrawingDisplayState() {
704 return mFlinger.mutableDrawingState().displays.valueFor(mDisplayToken);
705 }
706
707 const auto& getCurrentDisplayState() {
708 return mFlinger.mutableCurrentState().displays.valueFor(mDisplayToken);
709 }
710
Dominik Laskowski9fae1022018-05-29 13:17:40 -0700711 auto& mutableDisplayDevice() { return mFlinger.mutableDisplays()[mDisplayToken]; }
Lloyd Pique9d9cf402018-02-16 17:47:13 -0800712
Dominik Laskowski83bd7712022-01-07 14:30:53 -0800713 // If `configs` is nullptr, the injector creates RefreshRateConfigs from the `modes`.
714 // Otherwise, it uses `configs`, which the caller must create using the same `modes`.
715 //
716 // TODO(b/182939859): Once `modes` can be retrieved from RefreshRateConfigs, remove
717 // the `configs` parameter in favor of an alternative setRefreshRateConfigs API.
718 auto& setDisplayModes(DisplayModes modes, DisplayModeId activeModeId,
719 std::shared_ptr<scheduler::RefreshRateConfigs> configs = nullptr) {
720 mCreationArgs.supportedModes = std::move(modes);
721 mCreationArgs.activeModeId = activeModeId;
722 mCreationArgs.refreshRateConfigs = std::move(configs);
Marin Shalamanov045b7002021-01-07 16:56:24 +0100723 return *this;
724 }
725
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800726 auto& setNativeWindow(const sp<ANativeWindow>& nativeWindow) {
Lloyd Pique2eef1d22018-09-18 21:30:04 -0700727 mCreationArgs.nativeWindow = nativeWindow;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800728 return *this;
729 }
730
Lloyd Pique542307f2018-10-19 13:24:08 -0700731 auto& setDisplaySurface(const sp<compositionengine::DisplaySurface>& displaySurface) {
Lloyd Pique2eef1d22018-09-18 21:30:04 -0700732 mCreationArgs.displaySurface = displaySurface;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800733 return *this;
734 }
735
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800736 auto& setSecure(bool secure) {
Lloyd Pique2eef1d22018-09-18 21:30:04 -0700737 mCreationArgs.isSecure = secure;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800738 return *this;
739 }
740
Peiyong Lin65248e02020-04-18 21:15:07 -0700741 auto& setPowerMode(hal::PowerMode mode) {
Lloyd Pique2eef1d22018-09-18 21:30:04 -0700742 mCreationArgs.initialPowerMode = mode;
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700743 return *this;
744 }
745
Valerie Hau9758ae02018-10-09 16:05:09 -0700746 auto& setHwcColorModes(
747 const std::unordered_map<ui::ColorMode, std::vector<ui::RenderIntent>>
748 hwcColorModes) {
749 mCreationArgs.hwcColorModes = hwcColorModes;
750 return *this;
751 }
752
753 auto& setHasWideColorGamut(bool hasWideColorGamut) {
754 mCreationArgs.hasWideColorGamut = hasWideColorGamut;
755 return *this;
756 }
757
Lloyd Pique33050472019-12-19 17:12:44 -0800758 auto& setPhysicalOrientation(ui::Rotation orientation) {
759 mCreationArgs.physicalOrientation = orientation;
760 return *this;
761 }
762
Ady Abraham690f4612021-07-01 23:24:03 -0700763 sp<DisplayDevice> inject() NO_THREAD_SAFETY_ANALYSIS {
Dominik Laskowski83bd7712022-01-07 14:30:53 -0800764 auto& modes = mCreationArgs.supportedModes;
765 auto& activeModeId = mCreationArgs.activeModeId;
766
767 if (!mCreationArgs.refreshRateConfigs) {
768 if (modes.empty()) {
769 activeModeId = DisplayModeId(0);
770 modes.emplace_back(
771 DisplayMode::Builder(FakeHwcDisplayInjector::DEFAULT_ACTIVE_CONFIG)
772 .setId(activeModeId)
773 .setPhysicalDisplayId(PhysicalDisplayId::fromPort(0))
774 .setWidth(FakeHwcDisplayInjector::DEFAULT_WIDTH)
775 .setHeight(FakeHwcDisplayInjector::DEFAULT_HEIGHT)
776 .setVsyncPeriod(FakeHwcDisplayInjector::DEFAULT_VSYNC_PERIOD)
777 .setDpiX(FakeHwcDisplayInjector::DEFAULT_DPI)
778 .setDpiY(FakeHwcDisplayInjector::DEFAULT_DPI)
779 .setGroup(0)
780 .build());
781 }
782
783 mCreationArgs.refreshRateConfigs =
784 std::make_shared<scheduler::RefreshRateConfigs>(modes, activeModeId);
785 }
Lloyd Pique9370a482019-10-03 17:58:30 -0700786
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800787 DisplayDeviceState state;
Dominik Laskowski55c85402020-01-21 16:25:47 -0800788 if (const auto type = mCreationArgs.connectionType) {
Dominik Laskowski83bd7712022-01-07 14:30:53 -0800789 const auto displayId = mCreationArgs.compositionDisplay->getDisplayId();
Lloyd Pique9370a482019-10-03 17:58:30 -0700790 LOG_ALWAYS_FATAL_IF(!displayId);
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200791 const auto physicalId = PhysicalDisplayId::tryCast(*displayId);
792 LOG_ALWAYS_FATAL_IF(!physicalId);
Marin Shalamanov700e6392020-02-12 20:22:26 +0100793 LOG_ALWAYS_FATAL_IF(!mHwcDisplayId);
Marin Shalamanovc7e9f822021-11-16 20:07:55 +0100794
Dominik Laskowski83bd7712022-01-07 14:30:53 -0800795 const auto it = std::find_if(modes.begin(), modes.end(),
796 [&activeModeId](const DisplayModePtr& mode) {
797 return mode->getId() == activeModeId;
798 });
799 LOG_ALWAYS_FATAL_IF(it == modes.end());
800
Marin Shalamanovc7e9f822021-11-16 20:07:55 +0100801 state.physical = {.id = *physicalId,
802 .type = *type,
803 .hwcDisplayId = *mHwcDisplayId,
804 .deviceProductInfo = {},
Dominik Laskowski83bd7712022-01-07 14:30:53 -0800805 .supportedModes = modes,
806 .activeMode = *it};
Dominik Laskowski55c85402020-01-21 16:25:47 -0800807 }
808
Lloyd Pique2eef1d22018-09-18 21:30:04 -0700809 state.isSecure = mCreationArgs.isSecure;
810
Dominik Laskowski83bd7712022-01-07 14:30:53 -0800811 sp<DisplayDevice> display = sp<DisplayDevice>::make(mCreationArgs);
812 if (!display->isVirtual()) {
813 display->setActiveMode(activeModeId);
Marin Shalamanov045b7002021-01-07 16:56:24 +0100814 }
Dominik Laskowski83bd7712022-01-07 14:30:53 -0800815 mFlinger.mutableDisplays().emplace(mDisplayToken, display);
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800816 mFlinger.mutableCurrentState().displays.add(mDisplayToken, state);
817 mFlinger.mutableDrawingState().displays.add(mDisplayToken, state);
818
Dominik Laskowski55c85402020-01-21 16:25:47 -0800819 if (const auto& physical = state.physical) {
820 mFlinger.mutablePhysicalDisplayTokens()[physical->id] = mDisplayToken;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800821 }
822
Dominik Laskowski83bd7712022-01-07 14:30:53 -0800823 return display;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800824 }
825
826 private:
827 TestableSurfaceFlinger& mFlinger;
828 sp<BBinder> mDisplayToken = new BBinder();
Lloyd Pique2eef1d22018-09-18 21:30:04 -0700829 DisplayDeviceCreationArgs mCreationArgs;
Peiyong Line9d809e2020-04-14 13:10:48 -0700830 const std::optional<hal::HWDisplayId> mHwcDisplayId;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800831 };
832
Dominik Laskowski8b01cc02020-07-14 19:02:41 -0700833private:
Lloyd Pique90c115d2018-09-18 21:39:42 -0700834 surfaceflinger::test::Factory mFactory;
835 sp<SurfaceFlinger> mFlinger = new SurfaceFlinger(mFactory, SurfaceFlinger::SkipInitialization);
Dominik Laskowski83bd7712022-01-07 14:30:53 -0800836
837 scheduler::mock::SchedulerCallback mSchedulerCallback;
838 scheduler::mock::NoOpSchedulerCallback mNoOpSchedulerCallback;
Dominik Laskowski068173d2021-08-11 17:22:59 -0700839 scheduler::TestableScheduler* mScheduler = nullptr;
Lloyd Piquef58625d2017-12-19 13:22:33 -0800840};
841
842} // namespace android