blob: 100a78d8544984e98f4d736821ea7d9da5801021 [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>
Marin Shalamanovc7e9f822021-11-16 20:07:55 +010027#include <algorithm>
Lloyd Pique70d91362018-10-18 16:02:55 -070028
Lloyd Pique90c115d2018-09-18 21:39:42 -070029#include "BufferQueueLayer.h"
30#include "BufferStateLayer.h"
Lloyd Pique90c115d2018-09-18 21:39:42 -070031#include "ContainerLayer.h"
Lloyd Piquef58625d2017-12-19 13:22:33 -080032#include "DisplayDevice.h"
Vishnu Nairfa247b12020-02-11 08:58:26 -080033#include "EffectLayer.h"
Ady Abrahamc581d3c2020-08-06 17:34:27 -070034#include "FakeVsyncConfiguration.h"
Adithya Srinivasan2db3c1b2020-11-18 12:43:17 -080035#include "FrameTracer/FrameTracer.h"
Lloyd Piqued6b579f2018-04-06 15:29:10 -070036#include "Layer.h"
Lloyd Pique90c115d2018-09-18 21:39:42 -070037#include "NativeWindowSurface.h"
Dominik Laskowski9dab3432019-03-27 13:21:10 -070038#include "Scheduler/MessageQueue.h"
Steven Thomas2bbaabe2019-08-28 16:08:35 -070039#include "Scheduler/RefreshRateConfigs.h"
Lloyd Pique90c115d2018-09-18 21:39:42 -070040#include "StartPropertySetThread.h"
Lloyd Piquef58625d2017-12-19 13:22:33 -080041#include "SurfaceFlinger.h"
Lloyd Pique24f3bfe2019-10-02 19:29:10 -070042#include "SurfaceFlingerDefaultFactory.h"
Lloyd Pique90c115d2018-09-18 21:39:42 -070043#include "SurfaceInterceptor.h"
Dominik Laskowski7c9dbf92019-08-01 17:57:31 -070044#include "TestableScheduler.h"
Marin Shalamanov045b7002021-01-07 16:56:24 +010045#include "mock/DisplayHardware/MockComposer.h"
Adithya Srinivasan01189672020-10-20 14:23:05 -070046#include "mock/MockFrameTimeline.h"
Adithya Srinivasan2db3c1b2020-11-18 12:43:17 -080047#include "mock/MockFrameTracer.h"
Yiwei Zhang7e666a52018-11-15 13:33:42 -080048
Lloyd Piquef58625d2017-12-19 13:22:33 -080049namespace android {
50
Lloyd Piquee39cad22017-12-20 17:01:29 -080051class EventThread;
52
Peiyong Lin833074a2018-08-28 11:53:54 -070053namespace renderengine {
Lloyd Pique90c115d2018-09-18 21:39:42 -070054
Lloyd Piquee39cad22017-12-20 17:01:29 -080055class RenderEngine;
Lloyd Pique90c115d2018-09-18 21:39:42 -070056
57} // namespace renderengine
Lloyd Piquee39cad22017-12-20 17:01:29 -080058
59namespace Hwc2 {
Lloyd Pique90c115d2018-09-18 21:39:42 -070060
Lloyd Piquee39cad22017-12-20 17:01:29 -080061class Composer;
Lloyd Pique90c115d2018-09-18 21:39:42 -070062
63} // namespace Hwc2
64
Peiyong Line9d809e2020-04-14 13:10:48 -070065namespace hal = android::hardware::graphics::composer::hal;
66
Lloyd Pique90c115d2018-09-18 21:39:42 -070067namespace surfaceflinger::test {
68
69class Factory final : public surfaceflinger::Factory {
70public:
71 ~Factory() = default;
72
Lloyd Pique90c115d2018-09-18 21:39:42 -070073 std::unique_ptr<HWComposer> createHWComposer(const std::string&) override {
Lloyd Pique90c115d2018-09-18 21:39:42 -070074 return nullptr;
75 }
76
Ady Abraham8287e852020-08-12 14:44:58 -070077 std::unique_ptr<scheduler::VsyncConfiguration> createVsyncConfiguration(
Marin Shalamanov526c3382020-12-10 15:22:29 +010078 Fps /*currentRefreshRate*/) override {
Ana Krulec757f63a2019-01-25 10:46:18 -080079 return std::make_unique<scheduler::FakePhaseOffsets>();
80 }
81
Pablo Gamitoc351d6f2020-09-17 15:34:26 +000082 sp<SurfaceInterceptor> createSurfaceInterceptor() override {
83 return new android::impl::SurfaceInterceptor();
Lloyd Pique90c115d2018-09-18 21:39:42 -070084 }
85
86 sp<StartPropertySetThread> createStartPropertySetThread(bool timestampPropertyValue) override {
Lloyd Pique90c115d2018-09-18 21:39:42 -070087 return new StartPropertySetThread(timestampPropertyValue);
88 }
89
Lloyd Pique9370a482019-10-03 17:58:30 -070090 sp<DisplayDevice> createDisplayDevice(DisplayDeviceCreationArgs& creationArgs) override {
91 return new DisplayDevice(creationArgs);
Lloyd Pique90c115d2018-09-18 21:39:42 -070092 }
93
94 sp<GraphicBuffer> createGraphicBuffer(uint32_t width, uint32_t height, PixelFormat format,
95 uint32_t layerCount, uint64_t usage,
96 std::string requestorName) override {
Lloyd Pique90c115d2018-09-18 21:39:42 -070097 return new GraphicBuffer(width, height, format, layerCount, usage, requestorName);
98 }
99
100 void createBufferQueue(sp<IGraphicBufferProducer>* outProducer,
101 sp<IGraphicBufferConsumer>* outConsumer,
102 bool consumerIsSurfaceFlinger) override {
Lloyd Piquee300b312019-10-03 13:03:45 -0700103 if (!mCreateBufferQueue) {
104 BufferQueue::createBufferQueue(outProducer, outConsumer, consumerIsSurfaceFlinger);
105 return;
106 }
Lloyd Pique90c115d2018-09-18 21:39:42 -0700107 mCreateBufferQueue(outProducer, outConsumer, consumerIsSurfaceFlinger);
108 }
109
Lloyd Piquee300b312019-10-03 13:03:45 -0700110 sp<IGraphicBufferProducer> createMonitoredProducer(const sp<IGraphicBufferProducer>& producer,
111 const sp<SurfaceFlinger>& flinger,
112 const wp<Layer>& layer) override {
113 return new MonitoredProducer(producer, flinger, layer);
114 }
115
116 sp<BufferLayerConsumer> createBufferLayerConsumer(const sp<IGraphicBufferConsumer>& consumer,
117 renderengine::RenderEngine& renderEngine,
118 uint32_t textureName, Layer* layer) override {
119 return new BufferLayerConsumer(consumer, renderEngine, textureName, layer);
120 }
121
Lloyd Pique90c115d2018-09-18 21:39:42 -0700122 std::unique_ptr<surfaceflinger::NativeWindowSurface> createNativeWindowSurface(
123 const sp<IGraphicBufferProducer>& producer) override {
124 if (!mCreateNativeWindowSurface) return nullptr;
125 return mCreateNativeWindowSurface(producer);
126 }
127
Lloyd Pique70d91362018-10-18 16:02:55 -0700128 std::unique_ptr<compositionengine::CompositionEngine> createCompositionEngine() override {
129 return compositionengine::impl::createCompositionEngine();
130 }
131
Lloyd Pique90c115d2018-09-18 21:39:42 -0700132 sp<BufferQueueLayer> createBufferQueueLayer(const LayerCreationArgs&) override {
Lloyd Pique90c115d2018-09-18 21:39:42 -0700133 return nullptr;
134 }
135
136 sp<BufferStateLayer> createBufferStateLayer(const LayerCreationArgs&) override {
Lloyd Pique90c115d2018-09-18 21:39:42 -0700137 return nullptr;
138 }
139
Vishnu Nairfa247b12020-02-11 08:58:26 -0800140 sp<EffectLayer> createEffectLayer(const LayerCreationArgs&) override { return nullptr; }
Lloyd Pique90c115d2018-09-18 21:39:42 -0700141
142 sp<ContainerLayer> createContainerLayer(const LayerCreationArgs&) override {
Lloyd Pique90c115d2018-09-18 21:39:42 -0700143 return nullptr;
144 }
145
Adithya Srinivasan2db3c1b2020-11-18 12:43:17 -0800146 std::unique_ptr<FrameTracer> createFrameTracer() override {
147 return std::make_unique<mock::FrameTracer>();
148 }
149
Adithya Srinivasan01189672020-10-20 14:23:05 -0700150 std::unique_ptr<frametimeline::FrameTimeline> createFrameTimeline(
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -0800151 std::shared_ptr<TimeStats> timeStats, pid_t surfaceFlingerPid = 0) override {
152 return std::make_unique<mock::FrameTimeline>(timeStats, surfaceFlingerPid);
Adithya Srinivasan01189672020-10-20 14:23:05 -0700153 }
154
Lloyd Pique90c115d2018-09-18 21:39:42 -0700155 using CreateBufferQueueFunction =
156 std::function<void(sp<IGraphicBufferProducer>* /* outProducer */,
157 sp<IGraphicBufferConsumer>* /* outConsumer */,
158 bool /* consumerIsSurfaceFlinger */)>;
159 CreateBufferQueueFunction mCreateBufferQueue;
160
161 using CreateNativeWindowSurfaceFunction =
162 std::function<std::unique_ptr<surfaceflinger::NativeWindowSurface>(
163 const sp<IGraphicBufferProducer>&)>;
164 CreateNativeWindowSurfaceFunction mCreateNativeWindowSurface;
Lloyd Pique70d91362018-10-18 16:02:55 -0700165
166 using CreateCompositionEngineFunction =
167 std::function<std::unique_ptr<compositionengine::CompositionEngine>()>;
168 CreateCompositionEngineFunction mCreateCompositionEngine;
Lloyd Pique90c115d2018-09-18 21:39:42 -0700169};
170
171} // namespace surfaceflinger::test
Lloyd Piquee39cad22017-12-20 17:01:29 -0800172
Dominik Laskowski068173d2021-08-11 17:22:59 -0700173class TestableSurfaceFlinger final : private scheduler::ISchedulerCallback {
Lloyd Piquef58625d2017-12-19 13:22:33 -0800174public:
Dominik Laskowskib7251f42020-04-20 17:42:59 -0700175 using HotplugEvent = SurfaceFlinger::HotplugEvent;
176
Dominik Laskowskif7a09ed2019-10-07 13:54:18 -0700177 SurfaceFlinger* flinger() { return mFlinger.get(); }
Dominik Laskowski068173d2021-08-11 17:22:59 -0700178 scheduler::TestableScheduler* scheduler() { return mScheduler; }
Dominik Laskowski7c9dbf92019-08-01 17:57:31 -0700179
Lloyd Piquef58625d2017-12-19 13:22:33 -0800180 // Extend this as needed for accessing SurfaceFlinger private (and public)
181 // functions.
182
Peiyong Lin833074a2018-08-28 11:53:54 -0700183 void setupRenderEngine(std::unique_ptr<renderengine::RenderEngine> renderEngine) {
Lloyd Piqueb97e04f2018-10-18 17:07:05 -0700184 mFlinger->mCompositionEngine->setRenderEngine(std::move(renderEngine));
Lloyd Piquee39cad22017-12-20 17:01:29 -0800185 }
186
187 void setupComposer(std::unique_ptr<Hwc2::Composer> composer) {
Lloyd Pique441d5042018-10-18 16:49:51 -0700188 mFlinger->mCompositionEngine->setHwComposer(
189 std::make_unique<impl::HWComposer>(std::move(composer)));
Lloyd Piquee39cad22017-12-20 17:01:29 -0800190 }
191
Alec Mourie4034bb2019-11-19 12:45:54 -0800192 void setupTimeStats(const std::shared_ptr<TimeStats>& timeStats) {
193 mFlinger->mCompositionEngine->setTimeStats(timeStats);
194 }
195
Dominik Laskowski8b01cc02020-07-14 19:02:41 -0700196 // The ISchedulerCallback argument can be nullptr for a no-op implementation.
Ady Abraham8cb21882020-08-26 18:22:05 -0700197 void setupScheduler(std::unique_ptr<scheduler::VsyncController> vsyncController,
198 std::unique_ptr<scheduler::VSyncTracker> vsyncTracker,
Dominik Laskowski7c9dbf92019-08-01 17:57:31 -0700199 std::unique_ptr<EventThread> appEventThread,
Ady Abraham8a82ba62020-01-17 12:43:17 -0800200 std::unique_ptr<EventThread> sfEventThread,
Dominik Laskowski068173d2021-08-11 17:22:59 -0700201 scheduler::ISchedulerCallback* callback = nullptr,
202 bool hasMultipleModes = false) {
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100203 DisplayModes modes{DisplayMode::Builder(0)
204 .setId(DisplayModeId(0))
Ady Abraham5e7ee862021-06-23 17:43:41 -0700205 .setPhysicalDisplayId(PhysicalDisplayId::fromPort(0))
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100206 .setVsyncPeriod(16'666'667)
207 .setGroup(0)
208 .build()};
Ady Abrahamabc27602020-04-08 17:20:29 -0700209
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100210 if (hasMultipleModes) {
211 modes.emplace_back(DisplayMode::Builder(1)
212 .setId(DisplayModeId(1))
Ady Abraham5e7ee862021-06-23 17:43:41 -0700213 .setPhysicalDisplayId(PhysicalDisplayId::fromPort(0))
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100214 .setVsyncPeriod(11'111'111)
215 .setGroup(0)
216 .build());
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700217 }
218
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100219 const auto currMode = DisplayModeId(0);
Ady Abraham3efa3942021-06-24 19:01:25 -0700220 mRefreshRateConfigs = std::make_shared<scheduler::RefreshRateConfigs>(modes, currMode);
221 const auto currFps = mRefreshRateConfigs->getCurrentRefreshRate().getFps();
222 mFlinger->mVsyncConfiguration = mFactory.createVsyncConfiguration(currFps);
Ady Abraham23ea9da2021-07-14 16:32:56 -0700223 mFlinger->mVsyncModulator = sp<scheduler::VsyncModulator>::make(
224 mFlinger->mVsyncConfiguration->getCurrentConfigs());
Marin Shalamanov68a94092020-11-24 17:48:00 +0100225 mFlinger->mRefreshRateStats =
226 std::make_unique<scheduler::RefreshRateStats>(*mFlinger->mTimeStats, currFps,
227 /*powerMode=*/hal::PowerMode::OFF);
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700228
Dominik Laskowski068173d2021-08-11 17:22:59 -0700229 mScheduler = new scheduler::TestableScheduler(std::move(vsyncController),
230 std::move(vsyncTracker), mRefreshRateConfigs,
231 *(callback ?: this));
Dominik Laskowski7c9dbf92019-08-01 17:57:31 -0700232
Dominik Laskowski98041832019-08-01 18:35:59 -0700233 mFlinger->mAppConnectionHandle = mScheduler->createConnection(std::move(appEventThread));
234 mFlinger->mSfConnectionHandle = mScheduler->createConnection(std::move(sfEventThread));
Dominik Laskowskif7a09ed2019-10-07 13:54:18 -0700235 resetScheduler(mScheduler);
Dominik Laskowski7c9dbf92019-08-01 17:57:31 -0700236 }
237
Dominik Laskowski068173d2021-08-11 17:22:59 -0700238 void resetScheduler(scheduler::Scheduler* scheduler) { mFlinger->mScheduler.reset(scheduler); }
Dominik Laskowskif7a09ed2019-10-07 13:54:18 -0700239
Dominik Laskowski068173d2021-08-11 17:22:59 -0700240 scheduler::TestableScheduler& mutableScheduler() const { return *mScheduler; }
Ady Abraham44e9f3b2021-02-16 15:22:58 -0800241
Lloyd Pique90c115d2018-09-18 21:39:42 -0700242 using CreateBufferQueueFunction = surfaceflinger::test::Factory::CreateBufferQueueFunction;
Lloyd Pique5b36f3f2018-01-17 11:57:07 -0800243 void setCreateBufferQueueFunction(CreateBufferQueueFunction f) {
Lloyd Pique90c115d2018-09-18 21:39:42 -0700244 mFactory.mCreateBufferQueue = f;
Lloyd Pique5b36f3f2018-01-17 11:57:07 -0800245 }
246
Lloyd Pique90c115d2018-09-18 21:39:42 -0700247 using CreateNativeWindowSurfaceFunction =
248 surfaceflinger::test::Factory::CreateNativeWindowSurfaceFunction;
Lloyd Pique0b1fe702018-01-22 18:03:16 -0800249 void setCreateNativeWindowSurface(CreateNativeWindowSurfaceFunction f) {
Lloyd Pique90c115d2018-09-18 21:39:42 -0700250 mFactory.mCreateNativeWindowSurface = f;
Lloyd Pique0b1fe702018-01-22 18:03:16 -0800251 }
252
Daniel Solomon42d04562019-01-20 21:03:19 -0800253 void setInternalDisplayPrimaries(const ui::DisplayPrimaries& primaries) {
254 memcpy(&mFlinger->mInternalDisplayPrimaries, &primaries, sizeof(ui::DisplayPrimaries));
255 }
256
Dominik Laskowskib7251f42020-04-20 17:42:59 -0700257 static auto& mutableLayerDrawingState(const sp<Layer>& layer) { return layer->mDrawingState; }
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700258
Valerie Haud251afb2019-03-29 14:19:02 -0700259 auto& mutableStateLock() { return mFlinger->mStateLock; }
260
Dominik Laskowskib7251f42020-04-20 17:42:59 -0700261 static auto findOutputLayerForDisplay(const sp<Layer>& layer,
262 const sp<const DisplayDevice>& display) {
263 return layer->findOutputLayerForDisplay(display.get());
264 }
265
266 static void setLayerSidebandStream(const sp<Layer>& layer,
267 const sp<NativeHandle>& sidebandStream) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800268 layer->mDrawingState.sidebandStream = sidebandStream;
Lloyd Pique0b785d82018-12-04 17:25:27 -0800269 layer->mSidebandStream = sidebandStream;
Lloyd Piquede196652020-01-22 17:29:58 -0800270 layer->editCompositionState()->sidebandStream = sidebandStream;
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700271 }
272
Dominik Laskowskib7251f42020-04-20 17:42:59 -0700273 void setLayerCompositionType(const sp<Layer>& layer, hal::Composition type) {
274 auto outputLayer = findOutputLayerForDisplay(layer, mFlinger->getDefaultDisplayDevice());
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800275 LOG_ALWAYS_FATAL_IF(!outputLayer);
276 auto& state = outputLayer->editState();
277 LOG_ALWAYS_FATAL_IF(!outputLayer->getState().hwc);
Peiyong Line9d809e2020-04-14 13:10:48 -0700278 (*state.hwc).hwcCompositionType = type;
Dominik Laskowskib7251f42020-04-20 17:42:59 -0700279 }
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800280
Dominik Laskowskib7251f42020-04-20 17:42:59 -0700281 static void setLayerPotentialCursor(const sp<Layer>& layer, bool potentialCursor) {
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700282 layer->mPotentialCursor = potentialCursor;
283 }
284
Garfield Tande619fa2020-10-02 17:13:53 -0700285 static void setLayerDrawingParent(const sp<Layer>& layer, const sp<Layer>& drawingParent) {
Rob Carrc6d2d2b2021-10-25 16:51:49 +0000286 layer->mDrawingParent = drawingParent;
Garfield Tande619fa2020-10-02 17:13:53 -0700287 }
288
Lloyd Piquef58625d2017-12-19 13:22:33 -0800289 /* ------------------------------------------------------------------------
290 * Forwarding for functions being tested
291 */
Lloyd Pique0b1fe702018-01-22 18:03:16 -0800292
Dominik Laskowskie0e0cde2021-07-30 10:42:05 -0700293 nsecs_t commit() {
294 constexpr int64_t kVsyncId = 123;
295 const nsecs_t now = systemTime();
296 const nsecs_t expectedVsyncTime = now + 10'000'000;
297 mFlinger->commit(now, kVsyncId, expectedVsyncTime);
298 return now;
299 }
300
301 void commitAndComposite() { mFlinger->composite(commit()); }
302
Lloyd Piquea482f992018-01-22 19:00:34 -0800303 auto createDisplay(const String8& displayName, bool secure) {
304 return mFlinger->createDisplay(displayName, secure);
305 }
306
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700307 auto destroyDisplay(const sp<IBinder>& displayToken) {
308 return mFlinger->destroyDisplay(displayToken);
309 }
Lloyd Piquea482f992018-01-22 19:00:34 -0800310
Marin Shalamanovc7e9f822021-11-16 20:07:55 +0100311 auto getDisplay(const sp<IBinder>& displayToken) {
312 Mutex::Autolock lock(mFlinger->mStateLock);
313 return mFlinger->getDisplayDeviceLocked(displayToken);
314 }
315
Dominik Laskowski13948602021-03-08 20:48:28 -0800316 void enableHalVirtualDisplays(bool enable) { mFlinger->enableHalVirtualDisplays(enable); }
317
Lloyd Pique9370a482019-10-03 17:58:30 -0700318 auto setupNewDisplayDeviceInternal(
319 const wp<IBinder>& displayToken,
320 std::shared_ptr<compositionengine::Display> compositionDisplay,
321 const DisplayDeviceState& state,
322 const sp<compositionengine::DisplaySurface>& dispSurface,
Dominik Laskowski0a1435d2020-04-21 00:27:31 -0700323 const sp<IGraphicBufferProducer>& producer) NO_THREAD_SAFETY_ANALYSIS {
Lloyd Pique9370a482019-10-03 17:58:30 -0700324 return mFlinger->setupNewDisplayDeviceInternal(displayToken, compositionDisplay, state,
325 dispSurface, producer);
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800326 }
327
Dominik Laskowski9e168db2021-05-27 16:05:12 -0700328 auto commitTransactionsLocked(uint32_t transactionFlags) {
329 Mutex::Autolock lock(mFlinger->mStateLock);
330 return mFlinger->commitTransactionsLocked(transactionFlags);
Lloyd Pique0b1fe702018-01-22 18:03:16 -0800331 }
Lloyd Piquef58625d2017-12-19 13:22:33 -0800332
Dominik Laskowskiebc8d3a2021-04-16 23:18:31 -0700333 void onComposerHalHotplug(hal::HWDisplayId hwcDisplayId, hal::Connection connection) {
334 mFlinger->onComposerHalHotplug(hwcDisplayId, connection);
Lloyd Pique6cf11032018-01-22 18:57:44 -0800335 }
336
Vishnu Nairce5d0cc2019-02-28 14:38:41 -0800337 auto setDisplayStateLocked(const DisplayState& s) {
Dominik Laskowski9e168db2021-05-27 16:05:12 -0700338 Mutex::Autolock lock(mFlinger->mStateLock);
Vishnu Nairce5d0cc2019-02-28 14:38:41 -0800339 return mFlinger->setDisplayStateLocked(s);
340 }
Lloyd Pique9d9cf402018-02-16 17:47:13 -0800341
Dominik Laskowski83b88212018-12-11 13:34:06 -0800342 // Allow reading display state without locking, as if called on the SF main thread.
343 auto onInitializeDisplays() NO_THREAD_SAFETY_ANALYSIS {
344 return mFlinger->onInitializeDisplays();
345 }
Lloyd Pique86016da2018-03-01 16:09:38 -0800346
Lais Andrade3a6e47d2020-04-02 11:20:16 +0100347 auto notifyPowerBoost(int32_t boostId) { return mFlinger->notifyPowerBoost(boostId); }
348
Dominik Laskowski83b88212018-12-11 13:34:06 -0800349 // Allow reading display state without locking, as if called on the SF main thread.
350 auto setPowerModeInternal(const sp<DisplayDevice>& display,
Peiyong Lin65248e02020-04-18 21:15:07 -0700351 hal::PowerMode mode) NO_THREAD_SAFETY_ANALYSIS {
Dominik Laskowskie9774092018-12-11 10:04:24 -0800352 return mFlinger->setPowerModeInternal(display, mode);
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -0800353 }
354
chaviw426b5c02020-07-27 11:20:15 -0700355 auto renderScreenImplLocked(const RenderArea& renderArea,
356 SurfaceFlinger::TraverseLayersFunction traverseLayers,
Alec Mouria90a5702021-04-16 16:36:21 +0000357 const std::shared_ptr<renderengine::ExternalTexture>& buffer,
358 bool forSystem, bool regionSampling) {
chaviw426b5c02020-07-27 11:20:15 -0700359 ScreenCaptureResults captureResults;
chaviwc6ad8af2020-08-03 11:33:30 -0700360 return mFlinger->renderScreenImplLocked(renderArea, traverseLayers, buffer, forSystem,
chaviw17ac24b2021-01-28 18:50:05 -0800361 regionSampling, false /* grayscale */,
362 captureResults);
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700363 }
364
chaviw4b9d5e12020-08-04 18:30:35 -0700365 auto traverseLayersInLayerStack(ui::LayerStack layerStack, int32_t uid,
Marin Shalamanov1c434292020-06-12 01:47:29 +0200366 const LayerVector::Visitor& visitor) {
chaviw4b9d5e12020-08-04 18:30:35 -0700367 return mFlinger->SurfaceFlinger::traverseLayersInLayerStack(layerStack, uid, visitor);
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700368 }
369
Daniel Solomon42d04562019-01-20 21:03:19 -0800370 auto getDisplayNativePrimaries(const sp<IBinder>& displayToken,
371 ui::DisplayPrimaries &primaries) {
372 return mFlinger->SurfaceFlinger::getDisplayNativePrimaries(displayToken, primaries);
373 }
374
Arthur Hung58144272021-01-16 03:43:53 +0000375 auto& getTransactionQueue() { return mFlinger->mTransactionQueue; }
376 auto& getPendingTransactionQueue() { return mFlinger->mPendingTransactionQueues; }
ramindani4d48f902021-09-20 21:07:45 +0000377 auto& getTransactionCommittedSignals() { return mFlinger->mTransactionCommittedSignals; }
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
Vishnu Nair7891e962021-11-11 12:07:21 -0800391 auto flushTransactionQueues() { return mFlinger->flushTransactionQueues(0); };
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
Marin Shalamanovc7e9f822021-11-16 20:07:55 +0100404 auto setDesiredDisplayModeSpecs(const sp<IBinder>& displayToken, ui::DisplayModeId defaultMode,
405 bool allowGroupSwitching, float primaryRefreshRateMin,
406 float primaryRefreshRateMax, float appRequestRefreshRateMin,
407 float appRequestRefreshRateMax) {
408 return mFlinger->setDesiredDisplayModeSpecs(displayToken, defaultMode, allowGroupSwitching,
409 primaryRefreshRateMin, primaryRefreshRateMax,
410 appRequestRefreshRateMin,
411 appRequestRefreshRateMax);
412 }
413
414 void onActiveDisplayChanged(const sp<DisplayDevice>& activeDisplay) {
415 Mutex::Autolock lock(mFlinger->mStateLock);
416 mFlinger->onActiveDisplayChangedLocked(activeDisplay);
417 }
418
Lloyd Pique86016da2018-03-01 16:09:38 -0800419 /* ------------------------------------------------------------------------
420 * Read-only access to private data to assert post-conditions.
421 */
422
423 const auto& getAnimFrameTracker() const { return mFlinger->mAnimFrameTracker; }
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -0800424 const auto& getHasPoweredOff() const { return mFlinger->mHasPoweredOff; }
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -0800425 const auto& getVisibleRegionsDirty() const { return mFlinger->mVisibleRegionsDirty; }
Lloyd Pique441d5042018-10-18 16:49:51 -0700426 auto& getHwComposer() const {
427 return static_cast<impl::HWComposer&>(mFlinger->getHwComposer());
428 }
Lloyd Pique9370a482019-10-03 17:58:30 -0700429 auto& getCompositionEngine() const { return mFlinger->getCompositionEngine(); }
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -0800430
Lloyd Pique86016da2018-03-01 16:09:38 -0800431 const auto& getCompositorTiming() const { return mFlinger->getBE().mCompositorTiming; }
432
Adithya Srinivasanb238cd52021-02-04 17:54:05 +0000433 mock::FrameTracer* getFrameTracer() const {
434 return static_cast<mock::FrameTracer*>(mFlinger->mFrameTracer.get());
435 }
436
Lloyd Piquef58625d2017-12-19 13:22:33 -0800437 /* ------------------------------------------------------------------------
438 * Read-write access to private data to set up preconditions and assert
439 * post-conditions.
440 */
Lloyd Pique0b1fe702018-01-22 18:03:16 -0800441
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800442 auto& mutableHasWideColorDisplay() { return SurfaceFlinger::hasWideColorDisplay; }
443
Lloyd Piquee39cad22017-12-20 17:01:29 -0800444 auto& mutableCurrentState() { return mFlinger->mCurrentState; }
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800445 auto& mutableDisplayColorSetting() { return mFlinger->mDisplayColorSetting; }
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700446 auto& mutableDisplays() { return mFlinger->mDisplays; }
Lloyd Piquee39cad22017-12-20 17:01:29 -0800447 auto& mutableDrawingState() { return mFlinger->mDrawingState; }
Dominik Laskowski8da6b0e2021-05-12 15:34:13 -0700448 auto& mutableGeometryDirty() { return mFlinger->mGeometryDirty; }
Lloyd Pique0b1fe702018-01-22 18:03:16 -0800449 auto& mutableInterceptor() { return mFlinger->mInterceptor; }
Lloyd Pique6cf11032018-01-22 18:57:44 -0800450 auto& mutableMainThreadId() { return mFlinger->mMainThreadId; }
Lloyd Pique0b1fe702018-01-22 18:03:16 -0800451 auto& mutablePendingHotplugEvents() { return mFlinger->mPendingHotplugEvents; }
Dominik Laskowski075d3172018-05-24 15:50:06 -0700452 auto& mutablePhysicalDisplayTokens() { return mFlinger->mPhysicalDisplayTokens; }
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700453 auto& mutableTexturePool() { return mFlinger->mTexturePool; }
Lloyd Pique0b1fe702018-01-22 18:03:16 -0800454 auto& mutableTransactionFlags() { return mFlinger->mTransactionFlags; }
Peiyong Lin74ca2f42019-01-14 19:36:57 -0800455 auto& mutablePowerAdvisor() { return mFlinger->mPowerAdvisor; }
Lloyd Pique4fe29402019-08-12 16:51:24 -0700456 auto& mutableDebugDisableHWC() { return mFlinger->mDebugDisableHWC; }
Garfield Tan9c9c1912021-07-19 12:02:16 -0700457 auto& mutableMaxRenderTargetSize() { return mFlinger->mMaxRenderTargetSize; }
Lloyd Piquee39cad22017-12-20 17:01:29 -0800458
Lloyd Pique441d5042018-10-18 16:49:51 -0700459 auto& mutableHwcDisplayData() { return getHwComposer().mDisplayData; }
460 auto& mutableHwcPhysicalDisplayIdMap() { return getHwComposer().mPhysicalDisplayIdMap; }
Dominik Laskowskif8db0f02021-04-19 11:05:25 -0700461 auto& mutablePrimaryHwcDisplayId() { return getHwComposer().mPrimaryHwcDisplayId; }
Ady Abrahamadc914c2021-10-13 17:04:27 -0700462 auto& mutableActiveDisplayToken() { return mFlinger->mActiveDisplayToken; }
Lloyd Piquea618d852018-01-17 11:52:30 -0800463
Valerie Hau09e60052019-12-15 14:51:15 -0800464 auto fromHandle(const sp<IBinder>& handle) {
Valerie Hau09e60052019-12-15 14:51:15 -0800465 return mFlinger->fromHandle(handle);
466 }
467
Lloyd Piquee39cad22017-12-20 17:01:29 -0800468 ~TestableSurfaceFlinger() {
469 // All these pointer and container clears help ensure that GMock does
470 // not report a leaked object, since the SurfaceFlinger instance may
471 // still be referenced by something despite our best efforts to destroy
472 // it after each test is done.
473 mutableDisplays().clear();
Ady Abraham2939f092019-03-04 17:44:06 -0800474 mutableCurrentState().displays.clear();
475 mutableDrawingState().displays.clear();
Pablo Gamito5cfc6e52020-09-10 11:18:03 +0000476 mutableInterceptor().clear();
Lloyd Pique2eb71eb2019-08-13 13:23:03 -0700477 mFlinger->mScheduler.reset();
Lloyd Pique441d5042018-10-18 16:49:51 -0700478 mFlinger->mCompositionEngine->setHwComposer(std::unique_ptr<HWComposer>());
Lloyd Piqueb97e04f2018-10-18 17:07:05 -0700479 mFlinger->mCompositionEngine->setRenderEngine(
480 std::unique_ptr<renderengine::RenderEngine>());
Lloyd Piquee39cad22017-12-20 17:01:29 -0800481 }
Lloyd Piquef58625d2017-12-19 13:22:33 -0800482
Lloyd Piquea618d852018-01-17 11:52:30 -0800483 /* ------------------------------------------------------------------------
484 * Wrapper classes for Read-write access to private data to set up
485 * preconditions and assert post-conditions.
486 */
Ana Krulec4593b692019-01-11 22:07:25 -0800487 struct HWC2Display : public HWC2::impl::Display {
Peiyong Lin74ca2f42019-01-14 19:36:57 -0800488 HWC2Display(Hwc2::Composer& composer,
Peiyong Line9d809e2020-04-14 13:10:48 -0700489 const std::unordered_set<hal::Capability>& capabilities, hal::HWDisplayId id,
490 hal::DisplayType type)
Peiyong Lin74ca2f42019-01-14 19:36:57 -0800491 : HWC2::impl::Display(composer, capabilities, id, type) {}
Lloyd Piquea618d852018-01-17 11:52:30 -0800492 ~HWC2Display() {
493 // Prevents a call to disable vsyncs.
Peiyong Line9d809e2020-04-14 13:10:48 -0700494 mType = hal::DisplayType::INVALID;
Lloyd Piquea618d852018-01-17 11:52:30 -0800495 }
496
497 auto& mutableIsConnected() { return this->mIsConnected; }
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700498 auto& mutableLayers() { return this->mLayers; }
Lloyd Piquea618d852018-01-17 11:52:30 -0800499 };
500
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800501 class FakeHwcDisplayInjector {
502 public:
Peiyong Line9d809e2020-04-14 13:10:48 -0700503 static constexpr hal::HWDisplayId DEFAULT_HWC_DISPLAY_ID = 1000;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800504 static constexpr int32_t DEFAULT_WIDTH = 1920;
505 static constexpr int32_t DEFAULT_HEIGHT = 1280;
Marin Shalamanovc7e9f822021-11-16 20:07:55 +0100506 static constexpr int32_t DEFAULT_VSYNC_PERIOD = 16'666'667;
Ady Abraham2139f732019-11-13 18:56:40 -0800507 static constexpr int32_t DEFAULT_CONFIG_GROUP = 7;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800508 static constexpr int32_t DEFAULT_DPI = 320;
Peiyong Line9d809e2020-04-14 13:10:48 -0700509 static constexpr hal::HWConfigId DEFAULT_ACTIVE_CONFIG = 0;
Peiyong Lin65248e02020-04-18 21:15:07 -0700510 static constexpr hal::PowerMode DEFAULT_POWER_MODE = hal::PowerMode::ON;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800511
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200512 FakeHwcDisplayInjector(HalDisplayId displayId, hal::DisplayType hwcDisplayType,
513 bool isPrimary)
Dominik Laskowski075d3172018-05-24 15:50:06 -0700514 : mDisplayId(displayId), mHwcDisplayType(hwcDisplayType), mIsPrimary(isPrimary) {}
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800515
Peiyong Line9d809e2020-04-14 13:10:48 -0700516 auto& setHwcDisplayId(hal::HWDisplayId displayId) {
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800517 mHwcDisplayId = displayId;
518 return *this;
519 }
520
521 auto& setWidth(int32_t width) {
522 mWidth = width;
523 return *this;
524 }
525
526 auto& setHeight(int32_t height) {
527 mHeight = height;
528 return *this;
529 }
530
Marin Shalamanov045b7002021-01-07 16:56:24 +0100531 auto& setVsyncPeriod(int32_t vsyncPeriod) {
532 mVsyncPeriod = vsyncPeriod;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800533 return *this;
534 }
535
536 auto& setDpiX(int32_t dpi) {
537 mDpiX = dpi;
538 return *this;
539 }
540
541 auto& setDpiY(int32_t dpi) {
542 mDpiY = dpi;
543 return *this;
544 }
545
Peiyong Line9d809e2020-04-14 13:10:48 -0700546 auto& setActiveConfig(hal::HWConfigId config) {
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800547 mActiveConfig = config;
548 return *this;
549 }
550
Peiyong Line9d809e2020-04-14 13:10:48 -0700551 auto& setCapabilities(const std::unordered_set<hal::Capability>* capabilities) {
Lloyd Piquee22f0332018-07-16 16:35:56 -0700552 mCapabilities = capabilities;
553 return *this;
554 }
555
Peiyong Lin65248e02020-04-18 21:15:07 -0700556 auto& setPowerMode(hal::PowerMode mode) {
Peiyong Lin1336e6e2019-05-28 09:23:50 -0700557 mPowerMode = mode;
558 return *this;
559 }
560
Marin Shalamanov045b7002021-01-07 16:56:24 +0100561 void inject(TestableSurfaceFlinger* flinger, Hwc2::mock::Composer* composer) {
562 using ::testing::_;
563 using ::testing::DoAll;
564 using ::testing::Return;
565 using ::testing::SetArgPointee;
566
Peiyong Line9d809e2020-04-14 13:10:48 -0700567 static const std::unordered_set<hal::Capability> defaultCapabilities;
Lloyd Piquee22f0332018-07-16 16:35:56 -0700568 if (mCapabilities == nullptr) mCapabilities = &defaultCapabilities;
569
570 // Caution - Make sure that any values passed by reference here do
571 // not refer to an instance owned by FakeHwcDisplayInjector. This
572 // class has temporary lifetime, while the constructed HWC2::Display
573 // is much longer lived.
Peiyong Lin74ca2f42019-01-14 19:36:57 -0800574 auto display = std::make_unique<HWC2Display>(*composer, *mCapabilities, mHwcDisplayId,
575 mHwcDisplayType);
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800576
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800577 display->mutableIsConnected() = true;
Peiyong Lin65248e02020-04-18 21:15:07 -0700578 display->setPowerMode(mPowerMode);
Peiyong Linbdd08cc2019-12-17 21:35:14 -0800579 flinger->mutableHwcDisplayData()[mDisplayId].hwcDisplay = std::move(display);
Dominik Laskowski075d3172018-05-24 15:50:06 -0700580
Marin Shalamanov045b7002021-01-07 16:56:24 +0100581 EXPECT_CALL(*composer, getDisplayConfigs(mHwcDisplayId, _))
582 .WillRepeatedly(
583 DoAll(SetArgPointee<1>(std::vector<hal::HWConfigId>{mActiveConfig}),
584 Return(hal::Error::NONE)));
585
586 EXPECT_CALL(*composer,
587 getDisplayAttribute(mHwcDisplayId, mActiveConfig, hal::Attribute::WIDTH, _))
588 .WillRepeatedly(DoAll(SetArgPointee<3>(mWidth), Return(hal::Error::NONE)));
589
590 EXPECT_CALL(*composer,
591 getDisplayAttribute(mHwcDisplayId, mActiveConfig, hal::Attribute::HEIGHT,
592 _))
593 .WillRepeatedly(DoAll(SetArgPointee<3>(mHeight), Return(hal::Error::NONE)));
594
595 EXPECT_CALL(*composer,
596 getDisplayAttribute(mHwcDisplayId, mActiveConfig,
597 hal::Attribute::VSYNC_PERIOD, _))
598 .WillRepeatedly(
599 DoAll(SetArgPointee<3>(mVsyncPeriod), Return(hal::Error::NONE)));
600
601 EXPECT_CALL(*composer,
602 getDisplayAttribute(mHwcDisplayId, mActiveConfig, hal::Attribute::DPI_X, _))
603 .WillRepeatedly(DoAll(SetArgPointee<3>(mDpiX), Return(hal::Error::NONE)));
604
605 EXPECT_CALL(*composer,
606 getDisplayAttribute(mHwcDisplayId, mActiveConfig, hal::Attribute::DPI_Y, _))
607 .WillRepeatedly(DoAll(SetArgPointee<3>(mDpiY), Return(hal::Error::NONE)));
608
609 EXPECT_CALL(*composer,
610 getDisplayAttribute(mHwcDisplayId, mActiveConfig,
611 hal::Attribute::CONFIG_GROUP, _))
612 .WillRepeatedly(
613 DoAll(SetArgPointee<3>(mConfigGroup), Return(hal::Error::NONE)));
Marin Shalamanov3ea1d602020-12-16 19:59:39 +0100614
Peiyong Line9d809e2020-04-14 13:10:48 -0700615 if (mHwcDisplayType == hal::DisplayType::PHYSICAL) {
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200616 const auto physicalId = PhysicalDisplayId::tryCast(mDisplayId);
617 LOG_ALWAYS_FATAL_IF(!physicalId);
618 flinger->mutableHwcPhysicalDisplayIdMap().emplace(mHwcDisplayId, *physicalId);
Marin Shalamanov6e840172020-12-14 22:13:28 +0100619 if (mIsPrimary) {
Dominik Laskowskif8db0f02021-04-19 11:05:25 -0700620 flinger->mutablePrimaryHwcDisplayId() = mHwcDisplayId;
Marin Shalamanov6e840172020-12-14 22:13:28 +0100621 } else {
Dominik Laskowskif8db0f02021-04-19 11:05:25 -0700622 // If there is an external HWC display, there should always be a primary ID
Marin Shalamanov6e840172020-12-14 22:13:28 +0100623 // as well. Set it to some arbitrary value.
Dominik Laskowskif8db0f02021-04-19 11:05:25 -0700624 auto& primaryId = flinger->mutablePrimaryHwcDisplayId();
625 if (!primaryId) primaryId = mHwcDisplayId - 1;
Marin Shalamanov6e840172020-12-14 22:13:28 +0100626 }
Dominik Laskowski075d3172018-05-24 15:50:06 -0700627 }
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800628 }
629
630 private:
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200631 const HalDisplayId mDisplayId;
Peiyong Line9d809e2020-04-14 13:10:48 -0700632 const hal::DisplayType mHwcDisplayType;
Dominik Laskowski075d3172018-05-24 15:50:06 -0700633 const bool mIsPrimary;
634
Peiyong Line9d809e2020-04-14 13:10:48 -0700635 hal::HWDisplayId mHwcDisplayId = DEFAULT_HWC_DISPLAY_ID;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800636 int32_t mWidth = DEFAULT_WIDTH;
637 int32_t mHeight = DEFAULT_HEIGHT;
Marin Shalamanov045b7002021-01-07 16:56:24 +0100638 int32_t mVsyncPeriod = DEFAULT_VSYNC_PERIOD;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800639 int32_t mDpiX = DEFAULT_DPI;
640 int32_t mDpiY = DEFAULT_DPI;
Marin Shalamanov045b7002021-01-07 16:56:24 +0100641 int32_t mConfigGroup = DEFAULT_CONFIG_GROUP;
Peiyong Line9d809e2020-04-14 13:10:48 -0700642 hal::HWConfigId mActiveConfig = DEFAULT_ACTIVE_CONFIG;
Peiyong Lin65248e02020-04-18 21:15:07 -0700643 hal::PowerMode mPowerMode = DEFAULT_POWER_MODE;
Peiyong Line9d809e2020-04-14 13:10:48 -0700644 const std::unordered_set<hal::Capability>* mCapabilities = nullptr;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800645 };
646
647 class FakeDisplayDeviceInjector {
648 public:
Dominik Laskowski075d3172018-05-24 15:50:06 -0700649 FakeDisplayDeviceInjector(TestableSurfaceFlinger& flinger,
Lloyd Pique9370a482019-10-03 17:58:30 -0700650 std::shared_ptr<compositionengine::Display> compositionDisplay,
Marin Shalamanov228f46b2021-01-28 21:11:45 +0100651 std::optional<ui::DisplayConnectionType> connectionType,
Peiyong Line9d809e2020-04-14 13:10:48 -0700652 std::optional<hal::HWDisplayId> hwcDisplayId, bool isPrimary)
Lloyd Pique9370a482019-10-03 17:58:30 -0700653 : mFlinger(flinger),
Marin Shalamanov12c9e5a2021-01-07 00:25:35 +0100654 mCreationArgs(flinger.mFlinger.get(), flinger.mFlinger->getHwComposer(),
655 mDisplayToken, compositionDisplay),
Marin Shalamanov700e6392020-02-12 20:22:26 +0100656 mHwcDisplayId(hwcDisplayId) {
Dominik Laskowski55c85402020-01-21 16:25:47 -0800657 mCreationArgs.connectionType = connectionType;
Dominik Laskowski075d3172018-05-24 15:50:06 -0700658 mCreationArgs.isPrimary = isPrimary;
Marin Shalamanov045b7002021-01-07 16:56:24 +0100659
Marin Shalamanovc7e9f822021-11-16 20:07:55 +0100660 mCreationArgs.activeModeId = DisplayModeId(0);
Marin Shalamanov045b7002021-01-07 16:56:24 +0100661 DisplayModePtr activeMode =
662 DisplayMode::Builder(FakeHwcDisplayInjector::DEFAULT_ACTIVE_CONFIG)
Marin Shalamanovc7e9f822021-11-16 20:07:55 +0100663 .setId(mCreationArgs.activeModeId)
Ady Abraham5e7ee862021-06-23 17:43:41 -0700664 .setPhysicalDisplayId(PhysicalDisplayId::fromPort(0))
Marin Shalamanov045b7002021-01-07 16:56:24 +0100665 .setWidth(FakeHwcDisplayInjector::DEFAULT_WIDTH)
666 .setHeight(FakeHwcDisplayInjector::DEFAULT_HEIGHT)
667 .setVsyncPeriod(FakeHwcDisplayInjector::DEFAULT_VSYNC_PERIOD)
668 .setDpiX(FakeHwcDisplayInjector::DEFAULT_DPI)
669 .setDpiY(FakeHwcDisplayInjector::DEFAULT_DPI)
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100670 .setGroup(0)
Marin Shalamanov045b7002021-01-07 16:56:24 +0100671 .build();
672
673 DisplayModes modes{activeMode};
674 mCreationArgs.supportedModes = modes;
Ady Abraham3efa3942021-06-24 19:01:25 -0700675 mCreationArgs.refreshRateConfigs = flinger.mRefreshRateConfigs;
Dominik Laskowski075d3172018-05-24 15:50:06 -0700676 }
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800677
678 sp<IBinder> token() const { return mDisplayToken; }
679
680 DisplayDeviceState& mutableDrawingDisplayState() {
681 return mFlinger.mutableDrawingState().displays.editValueFor(mDisplayToken);
682 }
683
684 DisplayDeviceState& mutableCurrentDisplayState() {
685 return mFlinger.mutableCurrentState().displays.editValueFor(mDisplayToken);
686 }
687
Lloyd Pique9d9cf402018-02-16 17:47:13 -0800688 const auto& getDrawingDisplayState() {
689 return mFlinger.mutableDrawingState().displays.valueFor(mDisplayToken);
690 }
691
692 const auto& getCurrentDisplayState() {
693 return mFlinger.mutableCurrentState().displays.valueFor(mDisplayToken);
694 }
695
Dominik Laskowski9fae1022018-05-29 13:17:40 -0700696 auto& mutableDisplayDevice() { return mFlinger.mutableDisplays()[mDisplayToken]; }
Lloyd Pique9d9cf402018-02-16 17:47:13 -0800697
Marin Shalamanov045b7002021-01-07 16:56:24 +0100698 auto& setActiveMode(DisplayModeId mode) {
Marin Shalamanovc7e9f822021-11-16 20:07:55 +0100699 mCreationArgs.activeModeId = mode;
Marin Shalamanov045b7002021-01-07 16:56:24 +0100700 return *this;
701 }
702
703 auto& setSupportedModes(DisplayModes mode) {
704 mCreationArgs.supportedModes = mode;
705 return *this;
706 }
707
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800708 auto& setNativeWindow(const sp<ANativeWindow>& nativeWindow) {
Lloyd Pique2eef1d22018-09-18 21:30:04 -0700709 mCreationArgs.nativeWindow = nativeWindow;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800710 return *this;
711 }
712
Lloyd Pique542307f2018-10-19 13:24:08 -0700713 auto& setDisplaySurface(const sp<compositionengine::DisplaySurface>& displaySurface) {
Lloyd Pique2eef1d22018-09-18 21:30:04 -0700714 mCreationArgs.displaySurface = displaySurface;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800715 return *this;
716 }
717
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800718 auto& setSecure(bool secure) {
Lloyd Pique2eef1d22018-09-18 21:30:04 -0700719 mCreationArgs.isSecure = secure;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800720 return *this;
721 }
722
Peiyong Lin65248e02020-04-18 21:15:07 -0700723 auto& setPowerMode(hal::PowerMode mode) {
Lloyd Pique2eef1d22018-09-18 21:30:04 -0700724 mCreationArgs.initialPowerMode = mode;
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700725 return *this;
726 }
727
Valerie Hau9758ae02018-10-09 16:05:09 -0700728 auto& setHwcColorModes(
729 const std::unordered_map<ui::ColorMode, std::vector<ui::RenderIntent>>
730 hwcColorModes) {
731 mCreationArgs.hwcColorModes = hwcColorModes;
732 return *this;
733 }
734
735 auto& setHasWideColorGamut(bool hasWideColorGamut) {
736 mCreationArgs.hasWideColorGamut = hasWideColorGamut;
737 return *this;
738 }
739
Lloyd Pique33050472019-12-19 17:12:44 -0800740 auto& setPhysicalOrientation(ui::Rotation orientation) {
741 mCreationArgs.physicalOrientation = orientation;
742 return *this;
743 }
744
Ady Abraham690f4612021-07-01 23:24:03 -0700745 sp<DisplayDevice> inject() NO_THREAD_SAFETY_ANALYSIS {
Lloyd Pique9370a482019-10-03 17:58:30 -0700746 const auto displayId = mCreationArgs.compositionDisplay->getDisplayId();
747
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800748 DisplayDeviceState state;
Dominik Laskowski55c85402020-01-21 16:25:47 -0800749 if (const auto type = mCreationArgs.connectionType) {
Lloyd Pique9370a482019-10-03 17:58:30 -0700750 LOG_ALWAYS_FATAL_IF(!displayId);
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200751 const auto physicalId = PhysicalDisplayId::tryCast(*displayId);
752 LOG_ALWAYS_FATAL_IF(!physicalId);
Marin Shalamanov700e6392020-02-12 20:22:26 +0100753 LOG_ALWAYS_FATAL_IF(!mHwcDisplayId);
Marin Shalamanovc7e9f822021-11-16 20:07:55 +0100754
755 const DisplayModePtr activeModePtr =
756 *std::find_if(mCreationArgs.supportedModes.begin(),
757 mCreationArgs.supportedModes.end(), [&](DisplayModePtr mode) {
758 return mode->getId() == mCreationArgs.activeModeId;
759 });
760 state.physical = {.id = *physicalId,
761 .type = *type,
762 .hwcDisplayId = *mHwcDisplayId,
763 .deviceProductInfo = {},
764 .supportedModes = mCreationArgs.supportedModes,
765 .activeMode = activeModePtr};
Dominik Laskowski55c85402020-01-21 16:25:47 -0800766 }
767
Lloyd Pique2eef1d22018-09-18 21:30:04 -0700768 state.isSecure = mCreationArgs.isSecure;
769
Marin Shalamanovc7e9f822021-11-16 20:07:55 +0100770 mCreationArgs.refreshRateConfigs =
771 std::make_shared<scheduler::RefreshRateConfigs>(mCreationArgs.supportedModes,
772 mCreationArgs.activeModeId);
773
Lloyd Pique9370a482019-10-03 17:58:30 -0700774 sp<DisplayDevice> device = new DisplayDevice(mCreationArgs);
Marin Shalamanov045b7002021-01-07 16:56:24 +0100775 if (!device->isVirtual()) {
Marin Shalamanovc7e9f822021-11-16 20:07:55 +0100776 device->setActiveMode(mCreationArgs.activeModeId);
Marin Shalamanov045b7002021-01-07 16:56:24 +0100777 }
Lloyd Pique2eef1d22018-09-18 21:30:04 -0700778 mFlinger.mutableDisplays().emplace(mDisplayToken, device);
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800779 mFlinger.mutableCurrentState().displays.add(mDisplayToken, state);
780 mFlinger.mutableDrawingState().displays.add(mDisplayToken, state);
781
Dominik Laskowski55c85402020-01-21 16:25:47 -0800782 if (const auto& physical = state.physical) {
783 mFlinger.mutablePhysicalDisplayTokens()[physical->id] = mDisplayToken;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800784 }
785
786 return device;
787 }
788
789 private:
790 TestableSurfaceFlinger& mFlinger;
791 sp<BBinder> mDisplayToken = new BBinder();
Lloyd Pique2eef1d22018-09-18 21:30:04 -0700792 DisplayDeviceCreationArgs mCreationArgs;
Peiyong Line9d809e2020-04-14 13:10:48 -0700793 const std::optional<hal::HWDisplayId> mHwcDisplayId;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800794 };
795
Dominik Laskowski8b01cc02020-07-14 19:02:41 -0700796private:
Dominik Laskowskie0e0cde2021-07-30 10:42:05 -0700797 void scheduleComposite(FrameHint) override {}
Dominik Laskowski8b01cc02020-07-14 19:02:41 -0700798 void setVsyncEnabled(bool) override {}
Dominik Laskowski068173d2021-08-11 17:22:59 -0700799 void changeRefreshRate(const RefreshRate&, DisplayModeEvent) override {}
Dominik Laskowski8b01cc02020-07-14 19:02:41 -0700800 void kernelTimerChanged(bool) override {}
Ady Abraham62a0be22020-12-08 16:54:10 -0800801 void triggerOnFrameRateOverridesChanged() {}
Dominik Laskowski8b01cc02020-07-14 19:02:41 -0700802
Lloyd Pique90c115d2018-09-18 21:39:42 -0700803 surfaceflinger::test::Factory mFactory;
804 sp<SurfaceFlinger> mFlinger = new SurfaceFlinger(mFactory, SurfaceFlinger::SkipInitialization);
Dominik Laskowski068173d2021-08-11 17:22:59 -0700805 scheduler::TestableScheduler* mScheduler = nullptr;
Ady Abraham3efa3942021-06-24 19:01:25 -0700806 std::shared_ptr<scheduler::RefreshRateConfigs> mRefreshRateConfigs;
Lloyd Piquef58625d2017-12-19 13:22:33 -0800807};
808
809} // namespace android