blob: 361d629f1ebd9f2389ba602b0187c26d55006e04 [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
Leon Scroggins III2e1aa182021-12-01 17:33:12 -0500273 void setLayerCompositionType(const sp<Layer>& layer,
274 aidl::android::hardware::graphics::composer3::Composition type) {
Dominik Laskowskib7251f42020-04-20 17:42:59 -0700275 auto outputLayer = findOutputLayerForDisplay(layer, mFlinger->getDefaultDisplayDevice());
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800276 LOG_ALWAYS_FATAL_IF(!outputLayer);
277 auto& state = outputLayer->editState();
278 LOG_ALWAYS_FATAL_IF(!outputLayer->getState().hwc);
Peiyong Line9d809e2020-04-14 13:10:48 -0700279 (*state.hwc).hwcCompositionType = type;
Dominik Laskowskib7251f42020-04-20 17:42:59 -0700280 }
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800281
Dominik Laskowskib7251f42020-04-20 17:42:59 -0700282 static void setLayerPotentialCursor(const sp<Layer>& layer, bool potentialCursor) {
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700283 layer->mPotentialCursor = potentialCursor;
284 }
285
Garfield Tande619fa2020-10-02 17:13:53 -0700286 static void setLayerDrawingParent(const sp<Layer>& layer, const sp<Layer>& drawingParent) {
Rob Carrc6d2d2b2021-10-25 16:51:49 +0000287 layer->mDrawingParent = drawingParent;
Garfield Tande619fa2020-10-02 17:13:53 -0700288 }
289
Lloyd Piquef58625d2017-12-19 13:22:33 -0800290 /* ------------------------------------------------------------------------
291 * Forwarding for functions being tested
292 */
Lloyd Pique0b1fe702018-01-22 18:03:16 -0800293
Dominik Laskowskie0e0cde2021-07-30 10:42:05 -0700294 nsecs_t commit() {
295 constexpr int64_t kVsyncId = 123;
296 const nsecs_t now = systemTime();
297 const nsecs_t expectedVsyncTime = now + 10'000'000;
298 mFlinger->commit(now, kVsyncId, expectedVsyncTime);
299 return now;
300 }
301
302 void commitAndComposite() { mFlinger->composite(commit()); }
303
Lloyd Piquea482f992018-01-22 19:00:34 -0800304 auto createDisplay(const String8& displayName, bool secure) {
305 return mFlinger->createDisplay(displayName, secure);
306 }
307
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700308 auto destroyDisplay(const sp<IBinder>& displayToken) {
309 return mFlinger->destroyDisplay(displayToken);
310 }
Lloyd Piquea482f992018-01-22 19:00:34 -0800311
Marin Shalamanovc7e9f822021-11-16 20:07:55 +0100312 auto getDisplay(const sp<IBinder>& displayToken) {
313 Mutex::Autolock lock(mFlinger->mStateLock);
314 return mFlinger->getDisplayDeviceLocked(displayToken);
315 }
316
Dominik Laskowski13948602021-03-08 20:48:28 -0800317 void enableHalVirtualDisplays(bool enable) { mFlinger->enableHalVirtualDisplays(enable); }
318
Lloyd Pique9370a482019-10-03 17:58:30 -0700319 auto setupNewDisplayDeviceInternal(
320 const wp<IBinder>& displayToken,
321 std::shared_ptr<compositionengine::Display> compositionDisplay,
322 const DisplayDeviceState& state,
323 const sp<compositionengine::DisplaySurface>& dispSurface,
Dominik Laskowski0a1435d2020-04-21 00:27:31 -0700324 const sp<IGraphicBufferProducer>& producer) NO_THREAD_SAFETY_ANALYSIS {
Lloyd Pique9370a482019-10-03 17:58:30 -0700325 return mFlinger->setupNewDisplayDeviceInternal(displayToken, compositionDisplay, state,
326 dispSurface, producer);
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800327 }
328
Dominik Laskowski9e168db2021-05-27 16:05:12 -0700329 auto commitTransactionsLocked(uint32_t transactionFlags) {
330 Mutex::Autolock lock(mFlinger->mStateLock);
331 return mFlinger->commitTransactionsLocked(transactionFlags);
Lloyd Pique0b1fe702018-01-22 18:03:16 -0800332 }
Lloyd Piquef58625d2017-12-19 13:22:33 -0800333
Dominik Laskowskiebc8d3a2021-04-16 23:18:31 -0700334 void onComposerHalHotplug(hal::HWDisplayId hwcDisplayId, hal::Connection connection) {
335 mFlinger->onComposerHalHotplug(hwcDisplayId, connection);
Lloyd Pique6cf11032018-01-22 18:57:44 -0800336 }
337
Vishnu Nairce5d0cc2019-02-28 14:38:41 -0800338 auto setDisplayStateLocked(const DisplayState& s) {
Dominik Laskowski9e168db2021-05-27 16:05:12 -0700339 Mutex::Autolock lock(mFlinger->mStateLock);
Vishnu Nairce5d0cc2019-02-28 14:38:41 -0800340 return mFlinger->setDisplayStateLocked(s);
341 }
Lloyd Pique9d9cf402018-02-16 17:47:13 -0800342
Dominik Laskowski83b88212018-12-11 13:34:06 -0800343 // Allow reading display state without locking, as if called on the SF main thread.
344 auto onInitializeDisplays() NO_THREAD_SAFETY_ANALYSIS {
345 return mFlinger->onInitializeDisplays();
346 }
Lloyd Pique86016da2018-03-01 16:09:38 -0800347
Lais Andrade3a6e47d2020-04-02 11:20:16 +0100348 auto notifyPowerBoost(int32_t boostId) { return mFlinger->notifyPowerBoost(boostId); }
349
Alec Mouricdf16792021-12-10 13:16:06 -0800350 auto setDisplayBrightness(const sp<IBinder>& display,
351 const gui::DisplayBrightness& brightness) {
352 return mFlinger->setDisplayBrightness(display, brightness);
353 }
354
Dominik Laskowski83b88212018-12-11 13:34:06 -0800355 // Allow reading display state without locking, as if called on the SF main thread.
356 auto setPowerModeInternal(const sp<DisplayDevice>& display,
Peiyong Lin65248e02020-04-18 21:15:07 -0700357 hal::PowerMode mode) NO_THREAD_SAFETY_ANALYSIS {
Dominik Laskowskie9774092018-12-11 10:04:24 -0800358 return mFlinger->setPowerModeInternal(display, mode);
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -0800359 }
360
chaviw426b5c02020-07-27 11:20:15 -0700361 auto renderScreenImplLocked(const RenderArea& renderArea,
362 SurfaceFlinger::TraverseLayersFunction traverseLayers,
Alec Mouria90a5702021-04-16 16:36:21 +0000363 const std::shared_ptr<renderengine::ExternalTexture>& buffer,
364 bool forSystem, bool regionSampling) {
chaviw426b5c02020-07-27 11:20:15 -0700365 ScreenCaptureResults captureResults;
chaviwc6ad8af2020-08-03 11:33:30 -0700366 return mFlinger->renderScreenImplLocked(renderArea, traverseLayers, buffer, forSystem,
chaviw17ac24b2021-01-28 18:50:05 -0800367 regionSampling, false /* grayscale */,
368 captureResults);
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700369 }
370
chaviw4b9d5e12020-08-04 18:30:35 -0700371 auto traverseLayersInLayerStack(ui::LayerStack layerStack, int32_t uid,
Marin Shalamanov1c434292020-06-12 01:47:29 +0200372 const LayerVector::Visitor& visitor) {
chaviw4b9d5e12020-08-04 18:30:35 -0700373 return mFlinger->SurfaceFlinger::traverseLayersInLayerStack(layerStack, uid, visitor);
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700374 }
375
Daniel Solomon42d04562019-01-20 21:03:19 -0800376 auto getDisplayNativePrimaries(const sp<IBinder>& displayToken,
377 ui::DisplayPrimaries &primaries) {
378 return mFlinger->SurfaceFlinger::getDisplayNativePrimaries(displayToken, primaries);
379 }
380
Arthur Hung58144272021-01-16 03:43:53 +0000381 auto& getTransactionQueue() { return mFlinger->mTransactionQueue; }
382 auto& getPendingTransactionQueue() { return mFlinger->mPendingTransactionQueues; }
ramindani4d48f902021-09-20 21:07:45 +0000383 auto& getTransactionCommittedSignals() { return mFlinger->mTransactionCommittedSignals; }
Valerie Haud251afb2019-03-29 14:19:02 -0700384
Siarhei Vishniakoufc434ac2021-01-13 10:28:00 -1000385 auto setTransactionState(
386 const FrameTimelineInfo& frameTimelineInfo, const Vector<ComposerState>& states,
387 const Vector<DisplayState>& displays, uint32_t flags, const sp<IBinder>& applyToken,
388 const InputWindowCommands& inputWindowCommands, int64_t desiredPresentTime,
389 bool isAutoTimestamp, const client_cache_t& uncacheBuffer, bool hasListenerCallbacks,
390 std::vector<ListenerCallbacks>& listenerCallbacks, uint64_t transactionId) {
391 return mFlinger->setTransactionState(frameTimelineInfo, states, displays, flags, applyToken,
392 inputWindowCommands, desiredPresentTime,
Ady Abrahamf0c56492020-12-17 18:04:15 -0800393 isAutoTimestamp, uncacheBuffer, hasListenerCallbacks,
394 listenerCallbacks, transactionId);
Valerie Haud251afb2019-03-29 14:19:02 -0700395 }
396
Vishnu Nair7891e962021-11-11 12:07:21 -0800397 auto flushTransactionQueues() { return mFlinger->flushTransactionQueues(0); };
Valerie Haud251afb2019-03-29 14:19:02 -0700398
Eduardo Rochaac867192020-09-14 20:12:17 +0000399 auto onTransact(uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) {
400 return mFlinger->onTransact(code, data, reply, flags);
401 }
402
Ana Krulec31f2b3c2020-12-14 14:30:09 -0800403 auto getGPUContextPriority() { return mFlinger->getGPUContextPriority(); }
404
Ady Abraham899dcdb2021-06-15 16:56:21 -0700405 auto calculateMaxAcquiredBufferCount(Fps refreshRate,
406 std::chrono::nanoseconds presentLatency) const {
407 return SurfaceFlinger::calculateMaxAcquiredBufferCount(refreshRate, presentLatency);
Ady Abraham564f9de2021-02-03 18:34:33 -0800408 }
409
Marin Shalamanovc7e9f822021-11-16 20:07:55 +0100410 auto setDesiredDisplayModeSpecs(const sp<IBinder>& displayToken, ui::DisplayModeId defaultMode,
411 bool allowGroupSwitching, float primaryRefreshRateMin,
412 float primaryRefreshRateMax, float appRequestRefreshRateMin,
413 float appRequestRefreshRateMax) {
414 return mFlinger->setDesiredDisplayModeSpecs(displayToken, defaultMode, allowGroupSwitching,
415 primaryRefreshRateMin, primaryRefreshRateMax,
416 appRequestRefreshRateMin,
417 appRequestRefreshRateMax);
418 }
419
420 void onActiveDisplayChanged(const sp<DisplayDevice>& activeDisplay) {
421 Mutex::Autolock lock(mFlinger->mStateLock);
422 mFlinger->onActiveDisplayChangedLocked(activeDisplay);
423 }
424
Lloyd Pique86016da2018-03-01 16:09:38 -0800425 /* ------------------------------------------------------------------------
426 * Read-only access to private data to assert post-conditions.
427 */
428
429 const auto& getAnimFrameTracker() const { return mFlinger->mAnimFrameTracker; }
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -0800430 const auto& getHasPoweredOff() const { return mFlinger->mHasPoweredOff; }
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -0800431 const auto& getVisibleRegionsDirty() const { return mFlinger->mVisibleRegionsDirty; }
Lloyd Pique441d5042018-10-18 16:49:51 -0700432 auto& getHwComposer() const {
433 return static_cast<impl::HWComposer&>(mFlinger->getHwComposer());
434 }
Lloyd Pique9370a482019-10-03 17:58:30 -0700435 auto& getCompositionEngine() const { return mFlinger->getCompositionEngine(); }
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -0800436
Lloyd Pique86016da2018-03-01 16:09:38 -0800437 const auto& getCompositorTiming() const { return mFlinger->getBE().mCompositorTiming; }
438
Adithya Srinivasanb238cd52021-02-04 17:54:05 +0000439 mock::FrameTracer* getFrameTracer() const {
440 return static_cast<mock::FrameTracer*>(mFlinger->mFrameTracer.get());
441 }
442
Lloyd Piquef58625d2017-12-19 13:22:33 -0800443 /* ------------------------------------------------------------------------
444 * Read-write access to private data to set up preconditions and assert
445 * post-conditions.
446 */
Lloyd Pique0b1fe702018-01-22 18:03:16 -0800447
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800448 auto& mutableHasWideColorDisplay() { return SurfaceFlinger::hasWideColorDisplay; }
449
Lloyd Piquee39cad22017-12-20 17:01:29 -0800450 auto& mutableCurrentState() { return mFlinger->mCurrentState; }
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800451 auto& mutableDisplayColorSetting() { return mFlinger->mDisplayColorSetting; }
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700452 auto& mutableDisplays() { return mFlinger->mDisplays; }
Lloyd Piquee39cad22017-12-20 17:01:29 -0800453 auto& mutableDrawingState() { return mFlinger->mDrawingState; }
Dominik Laskowski8da6b0e2021-05-12 15:34:13 -0700454 auto& mutableGeometryDirty() { return mFlinger->mGeometryDirty; }
Lloyd Pique0b1fe702018-01-22 18:03:16 -0800455 auto& mutableInterceptor() { return mFlinger->mInterceptor; }
Lloyd Pique6cf11032018-01-22 18:57:44 -0800456 auto& mutableMainThreadId() { return mFlinger->mMainThreadId; }
Lloyd Pique0b1fe702018-01-22 18:03:16 -0800457 auto& mutablePendingHotplugEvents() { return mFlinger->mPendingHotplugEvents; }
Dominik Laskowski075d3172018-05-24 15:50:06 -0700458 auto& mutablePhysicalDisplayTokens() { return mFlinger->mPhysicalDisplayTokens; }
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700459 auto& mutableTexturePool() { return mFlinger->mTexturePool; }
Lloyd Pique0b1fe702018-01-22 18:03:16 -0800460 auto& mutableTransactionFlags() { return mFlinger->mTransactionFlags; }
Peiyong Lin74ca2f42019-01-14 19:36:57 -0800461 auto& mutablePowerAdvisor() { return mFlinger->mPowerAdvisor; }
Lloyd Pique4fe29402019-08-12 16:51:24 -0700462 auto& mutableDebugDisableHWC() { return mFlinger->mDebugDisableHWC; }
Garfield Tan9c9c1912021-07-19 12:02:16 -0700463 auto& mutableMaxRenderTargetSize() { return mFlinger->mMaxRenderTargetSize; }
Lloyd Piquee39cad22017-12-20 17:01:29 -0800464
Lloyd Pique441d5042018-10-18 16:49:51 -0700465 auto& mutableHwcDisplayData() { return getHwComposer().mDisplayData; }
466 auto& mutableHwcPhysicalDisplayIdMap() { return getHwComposer().mPhysicalDisplayIdMap; }
Dominik Laskowskif8db0f02021-04-19 11:05:25 -0700467 auto& mutablePrimaryHwcDisplayId() { return getHwComposer().mPrimaryHwcDisplayId; }
Ady Abrahamadc914c2021-10-13 17:04:27 -0700468 auto& mutableActiveDisplayToken() { return mFlinger->mActiveDisplayToken; }
Lloyd Piquea618d852018-01-17 11:52:30 -0800469
Valerie Hau09e60052019-12-15 14:51:15 -0800470 auto fromHandle(const sp<IBinder>& handle) {
Valerie Hau09e60052019-12-15 14:51:15 -0800471 return mFlinger->fromHandle(handle);
472 }
473
Lloyd Piquee39cad22017-12-20 17:01:29 -0800474 ~TestableSurfaceFlinger() {
475 // All these pointer and container clears help ensure that GMock does
476 // not report a leaked object, since the SurfaceFlinger instance may
477 // still be referenced by something despite our best efforts to destroy
478 // it after each test is done.
479 mutableDisplays().clear();
Ady Abraham2939f092019-03-04 17:44:06 -0800480 mutableCurrentState().displays.clear();
481 mutableDrawingState().displays.clear();
Pablo Gamito5cfc6e52020-09-10 11:18:03 +0000482 mutableInterceptor().clear();
Lloyd Pique2eb71eb2019-08-13 13:23:03 -0700483 mFlinger->mScheduler.reset();
Lloyd Pique441d5042018-10-18 16:49:51 -0700484 mFlinger->mCompositionEngine->setHwComposer(std::unique_ptr<HWComposer>());
Lloyd Piqueb97e04f2018-10-18 17:07:05 -0700485 mFlinger->mCompositionEngine->setRenderEngine(
486 std::unique_ptr<renderengine::RenderEngine>());
Lloyd Piquee39cad22017-12-20 17:01:29 -0800487 }
Lloyd Piquef58625d2017-12-19 13:22:33 -0800488
Lloyd Piquea618d852018-01-17 11:52:30 -0800489 /* ------------------------------------------------------------------------
490 * Wrapper classes for Read-write access to private data to set up
491 * preconditions and assert post-conditions.
492 */
Ana Krulec4593b692019-01-11 22:07:25 -0800493 struct HWC2Display : public HWC2::impl::Display {
Peiyong Lin74ca2f42019-01-14 19:36:57 -0800494 HWC2Display(Hwc2::Composer& composer,
Peiyong Line9d809e2020-04-14 13:10:48 -0700495 const std::unordered_set<hal::Capability>& capabilities, hal::HWDisplayId id,
496 hal::DisplayType type)
Peiyong Lin74ca2f42019-01-14 19:36:57 -0800497 : HWC2::impl::Display(composer, capabilities, id, type) {}
Lloyd Piquea618d852018-01-17 11:52:30 -0800498 ~HWC2Display() {
499 // Prevents a call to disable vsyncs.
Peiyong Line9d809e2020-04-14 13:10:48 -0700500 mType = hal::DisplayType::INVALID;
Lloyd Piquea618d852018-01-17 11:52:30 -0800501 }
502
503 auto& mutableIsConnected() { return this->mIsConnected; }
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700504 auto& mutableLayers() { return this->mLayers; }
Lloyd Piquea618d852018-01-17 11:52:30 -0800505 };
506
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800507 class FakeHwcDisplayInjector {
508 public:
Peiyong Line9d809e2020-04-14 13:10:48 -0700509 static constexpr hal::HWDisplayId DEFAULT_HWC_DISPLAY_ID = 1000;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800510 static constexpr int32_t DEFAULT_WIDTH = 1920;
511 static constexpr int32_t DEFAULT_HEIGHT = 1280;
Marin Shalamanovc7e9f822021-11-16 20:07:55 +0100512 static constexpr int32_t DEFAULT_VSYNC_PERIOD = 16'666'667;
Ady Abraham2139f732019-11-13 18:56:40 -0800513 static constexpr int32_t DEFAULT_CONFIG_GROUP = 7;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800514 static constexpr int32_t DEFAULT_DPI = 320;
Peiyong Line9d809e2020-04-14 13:10:48 -0700515 static constexpr hal::HWConfigId DEFAULT_ACTIVE_CONFIG = 0;
Peiyong Lin65248e02020-04-18 21:15:07 -0700516 static constexpr hal::PowerMode DEFAULT_POWER_MODE = hal::PowerMode::ON;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800517
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200518 FakeHwcDisplayInjector(HalDisplayId displayId, hal::DisplayType hwcDisplayType,
519 bool isPrimary)
Dominik Laskowski075d3172018-05-24 15:50:06 -0700520 : mDisplayId(displayId), mHwcDisplayType(hwcDisplayType), mIsPrimary(isPrimary) {}
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800521
Peiyong Line9d809e2020-04-14 13:10:48 -0700522 auto& setHwcDisplayId(hal::HWDisplayId displayId) {
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800523 mHwcDisplayId = displayId;
524 return *this;
525 }
526
527 auto& setWidth(int32_t width) {
528 mWidth = width;
529 return *this;
530 }
531
532 auto& setHeight(int32_t height) {
533 mHeight = height;
534 return *this;
535 }
536
Marin Shalamanov045b7002021-01-07 16:56:24 +0100537 auto& setVsyncPeriod(int32_t vsyncPeriod) {
538 mVsyncPeriod = vsyncPeriod;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800539 return *this;
540 }
541
542 auto& setDpiX(int32_t dpi) {
543 mDpiX = dpi;
544 return *this;
545 }
546
547 auto& setDpiY(int32_t dpi) {
548 mDpiY = dpi;
549 return *this;
550 }
551
Peiyong Line9d809e2020-04-14 13:10:48 -0700552 auto& setActiveConfig(hal::HWConfigId config) {
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800553 mActiveConfig = config;
554 return *this;
555 }
556
Peiyong Line9d809e2020-04-14 13:10:48 -0700557 auto& setCapabilities(const std::unordered_set<hal::Capability>* capabilities) {
Lloyd Piquee22f0332018-07-16 16:35:56 -0700558 mCapabilities = capabilities;
559 return *this;
560 }
561
Peiyong Lin65248e02020-04-18 21:15:07 -0700562 auto& setPowerMode(hal::PowerMode mode) {
Peiyong Lin1336e6e2019-05-28 09:23:50 -0700563 mPowerMode = mode;
564 return *this;
565 }
566
Marin Shalamanov045b7002021-01-07 16:56:24 +0100567 void inject(TestableSurfaceFlinger* flinger, Hwc2::mock::Composer* composer) {
568 using ::testing::_;
569 using ::testing::DoAll;
570 using ::testing::Return;
571 using ::testing::SetArgPointee;
572
Peiyong Line9d809e2020-04-14 13:10:48 -0700573 static const std::unordered_set<hal::Capability> defaultCapabilities;
Lloyd Piquee22f0332018-07-16 16:35:56 -0700574 if (mCapabilities == nullptr) mCapabilities = &defaultCapabilities;
575
576 // Caution - Make sure that any values passed by reference here do
577 // not refer to an instance owned by FakeHwcDisplayInjector. This
578 // class has temporary lifetime, while the constructed HWC2::Display
579 // is much longer lived.
Peiyong Lin74ca2f42019-01-14 19:36:57 -0800580 auto display = std::make_unique<HWC2Display>(*composer, *mCapabilities, mHwcDisplayId,
581 mHwcDisplayType);
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800582
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800583 display->mutableIsConnected() = true;
Peiyong Lin65248e02020-04-18 21:15:07 -0700584 display->setPowerMode(mPowerMode);
Peiyong Linbdd08cc2019-12-17 21:35:14 -0800585 flinger->mutableHwcDisplayData()[mDisplayId].hwcDisplay = std::move(display);
Dominik Laskowski075d3172018-05-24 15:50:06 -0700586
Marin Shalamanov045b7002021-01-07 16:56:24 +0100587 EXPECT_CALL(*composer, getDisplayConfigs(mHwcDisplayId, _))
588 .WillRepeatedly(
589 DoAll(SetArgPointee<1>(std::vector<hal::HWConfigId>{mActiveConfig}),
590 Return(hal::Error::NONE)));
591
592 EXPECT_CALL(*composer,
593 getDisplayAttribute(mHwcDisplayId, mActiveConfig, hal::Attribute::WIDTH, _))
594 .WillRepeatedly(DoAll(SetArgPointee<3>(mWidth), Return(hal::Error::NONE)));
595
596 EXPECT_CALL(*composer,
597 getDisplayAttribute(mHwcDisplayId, mActiveConfig, hal::Attribute::HEIGHT,
598 _))
599 .WillRepeatedly(DoAll(SetArgPointee<3>(mHeight), Return(hal::Error::NONE)));
600
601 EXPECT_CALL(*composer,
602 getDisplayAttribute(mHwcDisplayId, mActiveConfig,
603 hal::Attribute::VSYNC_PERIOD, _))
604 .WillRepeatedly(
605 DoAll(SetArgPointee<3>(mVsyncPeriod), Return(hal::Error::NONE)));
606
607 EXPECT_CALL(*composer,
608 getDisplayAttribute(mHwcDisplayId, mActiveConfig, hal::Attribute::DPI_X, _))
609 .WillRepeatedly(DoAll(SetArgPointee<3>(mDpiX), Return(hal::Error::NONE)));
610
611 EXPECT_CALL(*composer,
612 getDisplayAttribute(mHwcDisplayId, mActiveConfig, hal::Attribute::DPI_Y, _))
613 .WillRepeatedly(DoAll(SetArgPointee<3>(mDpiY), Return(hal::Error::NONE)));
614
615 EXPECT_CALL(*composer,
616 getDisplayAttribute(mHwcDisplayId, mActiveConfig,
617 hal::Attribute::CONFIG_GROUP, _))
618 .WillRepeatedly(
619 DoAll(SetArgPointee<3>(mConfigGroup), Return(hal::Error::NONE)));
Marin Shalamanov3ea1d602020-12-16 19:59:39 +0100620
Peiyong Line9d809e2020-04-14 13:10:48 -0700621 if (mHwcDisplayType == hal::DisplayType::PHYSICAL) {
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200622 const auto physicalId = PhysicalDisplayId::tryCast(mDisplayId);
623 LOG_ALWAYS_FATAL_IF(!physicalId);
624 flinger->mutableHwcPhysicalDisplayIdMap().emplace(mHwcDisplayId, *physicalId);
Marin Shalamanov6e840172020-12-14 22:13:28 +0100625 if (mIsPrimary) {
Dominik Laskowskif8db0f02021-04-19 11:05:25 -0700626 flinger->mutablePrimaryHwcDisplayId() = mHwcDisplayId;
Marin Shalamanov6e840172020-12-14 22:13:28 +0100627 } else {
Dominik Laskowskif8db0f02021-04-19 11:05:25 -0700628 // If there is an external HWC display, there should always be a primary ID
Marin Shalamanov6e840172020-12-14 22:13:28 +0100629 // as well. Set it to some arbitrary value.
Dominik Laskowskif8db0f02021-04-19 11:05:25 -0700630 auto& primaryId = flinger->mutablePrimaryHwcDisplayId();
631 if (!primaryId) primaryId = mHwcDisplayId - 1;
Marin Shalamanov6e840172020-12-14 22:13:28 +0100632 }
Dominik Laskowski075d3172018-05-24 15:50:06 -0700633 }
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800634 }
635
636 private:
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200637 const HalDisplayId mDisplayId;
Peiyong Line9d809e2020-04-14 13:10:48 -0700638 const hal::DisplayType mHwcDisplayType;
Dominik Laskowski075d3172018-05-24 15:50:06 -0700639 const bool mIsPrimary;
640
Peiyong Line9d809e2020-04-14 13:10:48 -0700641 hal::HWDisplayId mHwcDisplayId = DEFAULT_HWC_DISPLAY_ID;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800642 int32_t mWidth = DEFAULT_WIDTH;
643 int32_t mHeight = DEFAULT_HEIGHT;
Marin Shalamanov045b7002021-01-07 16:56:24 +0100644 int32_t mVsyncPeriod = DEFAULT_VSYNC_PERIOD;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800645 int32_t mDpiX = DEFAULT_DPI;
646 int32_t mDpiY = DEFAULT_DPI;
Marin Shalamanov045b7002021-01-07 16:56:24 +0100647 int32_t mConfigGroup = DEFAULT_CONFIG_GROUP;
Peiyong Line9d809e2020-04-14 13:10:48 -0700648 hal::HWConfigId mActiveConfig = DEFAULT_ACTIVE_CONFIG;
Peiyong Lin65248e02020-04-18 21:15:07 -0700649 hal::PowerMode mPowerMode = DEFAULT_POWER_MODE;
Peiyong Line9d809e2020-04-14 13:10:48 -0700650 const std::unordered_set<hal::Capability>* mCapabilities = nullptr;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800651 };
652
653 class FakeDisplayDeviceInjector {
654 public:
Dominik Laskowski075d3172018-05-24 15:50:06 -0700655 FakeDisplayDeviceInjector(TestableSurfaceFlinger& flinger,
Lloyd Pique9370a482019-10-03 17:58:30 -0700656 std::shared_ptr<compositionengine::Display> compositionDisplay,
Marin Shalamanov228f46b2021-01-28 21:11:45 +0100657 std::optional<ui::DisplayConnectionType> connectionType,
Peiyong Line9d809e2020-04-14 13:10:48 -0700658 std::optional<hal::HWDisplayId> hwcDisplayId, bool isPrimary)
Lloyd Pique9370a482019-10-03 17:58:30 -0700659 : mFlinger(flinger),
Marin Shalamanov12c9e5a2021-01-07 00:25:35 +0100660 mCreationArgs(flinger.mFlinger.get(), flinger.mFlinger->getHwComposer(),
661 mDisplayToken, compositionDisplay),
Marin Shalamanov700e6392020-02-12 20:22:26 +0100662 mHwcDisplayId(hwcDisplayId) {
Dominik Laskowski55c85402020-01-21 16:25:47 -0800663 mCreationArgs.connectionType = connectionType;
Dominik Laskowski075d3172018-05-24 15:50:06 -0700664 mCreationArgs.isPrimary = isPrimary;
Marin Shalamanov045b7002021-01-07 16:56:24 +0100665
Marin Shalamanovc7e9f822021-11-16 20:07:55 +0100666 mCreationArgs.activeModeId = DisplayModeId(0);
Marin Shalamanov045b7002021-01-07 16:56:24 +0100667 DisplayModePtr activeMode =
668 DisplayMode::Builder(FakeHwcDisplayInjector::DEFAULT_ACTIVE_CONFIG)
Marin Shalamanovc7e9f822021-11-16 20:07:55 +0100669 .setId(mCreationArgs.activeModeId)
Ady Abraham5e7ee862021-06-23 17:43:41 -0700670 .setPhysicalDisplayId(PhysicalDisplayId::fromPort(0))
Marin Shalamanov045b7002021-01-07 16:56:24 +0100671 .setWidth(FakeHwcDisplayInjector::DEFAULT_WIDTH)
672 .setHeight(FakeHwcDisplayInjector::DEFAULT_HEIGHT)
673 .setVsyncPeriod(FakeHwcDisplayInjector::DEFAULT_VSYNC_PERIOD)
674 .setDpiX(FakeHwcDisplayInjector::DEFAULT_DPI)
675 .setDpiY(FakeHwcDisplayInjector::DEFAULT_DPI)
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100676 .setGroup(0)
Marin Shalamanov045b7002021-01-07 16:56:24 +0100677 .build();
678
679 DisplayModes modes{activeMode};
680 mCreationArgs.supportedModes = modes;
Ady Abraham3efa3942021-06-24 19:01:25 -0700681 mCreationArgs.refreshRateConfigs = flinger.mRefreshRateConfigs;
Dominik Laskowski075d3172018-05-24 15:50:06 -0700682 }
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800683
684 sp<IBinder> token() const { return mDisplayToken; }
685
686 DisplayDeviceState& mutableDrawingDisplayState() {
687 return mFlinger.mutableDrawingState().displays.editValueFor(mDisplayToken);
688 }
689
690 DisplayDeviceState& mutableCurrentDisplayState() {
691 return mFlinger.mutableCurrentState().displays.editValueFor(mDisplayToken);
692 }
693
Lloyd Pique9d9cf402018-02-16 17:47:13 -0800694 const auto& getDrawingDisplayState() {
695 return mFlinger.mutableDrawingState().displays.valueFor(mDisplayToken);
696 }
697
698 const auto& getCurrentDisplayState() {
699 return mFlinger.mutableCurrentState().displays.valueFor(mDisplayToken);
700 }
701
Dominik Laskowski9fae1022018-05-29 13:17:40 -0700702 auto& mutableDisplayDevice() { return mFlinger.mutableDisplays()[mDisplayToken]; }
Lloyd Pique9d9cf402018-02-16 17:47:13 -0800703
Marin Shalamanov045b7002021-01-07 16:56:24 +0100704 auto& setActiveMode(DisplayModeId mode) {
Marin Shalamanovc7e9f822021-11-16 20:07:55 +0100705 mCreationArgs.activeModeId = mode;
Marin Shalamanov045b7002021-01-07 16:56:24 +0100706 return *this;
707 }
708
709 auto& setSupportedModes(DisplayModes mode) {
710 mCreationArgs.supportedModes = mode;
711 return *this;
712 }
713
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800714 auto& setNativeWindow(const sp<ANativeWindow>& nativeWindow) {
Lloyd Pique2eef1d22018-09-18 21:30:04 -0700715 mCreationArgs.nativeWindow = nativeWindow;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800716 return *this;
717 }
718
Lloyd Pique542307f2018-10-19 13:24:08 -0700719 auto& setDisplaySurface(const sp<compositionengine::DisplaySurface>& displaySurface) {
Lloyd Pique2eef1d22018-09-18 21:30:04 -0700720 mCreationArgs.displaySurface = displaySurface;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800721 return *this;
722 }
723
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800724 auto& setSecure(bool secure) {
Lloyd Pique2eef1d22018-09-18 21:30:04 -0700725 mCreationArgs.isSecure = secure;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800726 return *this;
727 }
728
Peiyong Lin65248e02020-04-18 21:15:07 -0700729 auto& setPowerMode(hal::PowerMode mode) {
Lloyd Pique2eef1d22018-09-18 21:30:04 -0700730 mCreationArgs.initialPowerMode = mode;
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700731 return *this;
732 }
733
Valerie Hau9758ae02018-10-09 16:05:09 -0700734 auto& setHwcColorModes(
735 const std::unordered_map<ui::ColorMode, std::vector<ui::RenderIntent>>
736 hwcColorModes) {
737 mCreationArgs.hwcColorModes = hwcColorModes;
738 return *this;
739 }
740
741 auto& setHasWideColorGamut(bool hasWideColorGamut) {
742 mCreationArgs.hasWideColorGamut = hasWideColorGamut;
743 return *this;
744 }
745
Lloyd Pique33050472019-12-19 17:12:44 -0800746 auto& setPhysicalOrientation(ui::Rotation orientation) {
747 mCreationArgs.physicalOrientation = orientation;
748 return *this;
749 }
750
Ady Abraham690f4612021-07-01 23:24:03 -0700751 sp<DisplayDevice> inject() NO_THREAD_SAFETY_ANALYSIS {
Lloyd Pique9370a482019-10-03 17:58:30 -0700752 const auto displayId = mCreationArgs.compositionDisplay->getDisplayId();
753
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800754 DisplayDeviceState state;
Dominik Laskowski55c85402020-01-21 16:25:47 -0800755 if (const auto type = mCreationArgs.connectionType) {
Lloyd Pique9370a482019-10-03 17:58:30 -0700756 LOG_ALWAYS_FATAL_IF(!displayId);
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200757 const auto physicalId = PhysicalDisplayId::tryCast(*displayId);
758 LOG_ALWAYS_FATAL_IF(!physicalId);
Marin Shalamanov700e6392020-02-12 20:22:26 +0100759 LOG_ALWAYS_FATAL_IF(!mHwcDisplayId);
Marin Shalamanovc7e9f822021-11-16 20:07:55 +0100760
761 const DisplayModePtr activeModePtr =
762 *std::find_if(mCreationArgs.supportedModes.begin(),
763 mCreationArgs.supportedModes.end(), [&](DisplayModePtr mode) {
764 return mode->getId() == mCreationArgs.activeModeId;
765 });
766 state.physical = {.id = *physicalId,
767 .type = *type,
768 .hwcDisplayId = *mHwcDisplayId,
769 .deviceProductInfo = {},
770 .supportedModes = mCreationArgs.supportedModes,
771 .activeMode = activeModePtr};
Dominik Laskowski55c85402020-01-21 16:25:47 -0800772 }
773
Lloyd Pique2eef1d22018-09-18 21:30:04 -0700774 state.isSecure = mCreationArgs.isSecure;
775
Marin Shalamanovc7e9f822021-11-16 20:07:55 +0100776 mCreationArgs.refreshRateConfigs =
777 std::make_shared<scheduler::RefreshRateConfigs>(mCreationArgs.supportedModes,
778 mCreationArgs.activeModeId);
779
Lloyd Pique9370a482019-10-03 17:58:30 -0700780 sp<DisplayDevice> device = new DisplayDevice(mCreationArgs);
Marin Shalamanov045b7002021-01-07 16:56:24 +0100781 if (!device->isVirtual()) {
Marin Shalamanovc7e9f822021-11-16 20:07:55 +0100782 device->setActiveMode(mCreationArgs.activeModeId);
Marin Shalamanov045b7002021-01-07 16:56:24 +0100783 }
Lloyd Pique2eef1d22018-09-18 21:30:04 -0700784 mFlinger.mutableDisplays().emplace(mDisplayToken, device);
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800785 mFlinger.mutableCurrentState().displays.add(mDisplayToken, state);
786 mFlinger.mutableDrawingState().displays.add(mDisplayToken, state);
787
Dominik Laskowski55c85402020-01-21 16:25:47 -0800788 if (const auto& physical = state.physical) {
789 mFlinger.mutablePhysicalDisplayTokens()[physical->id] = mDisplayToken;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800790 }
791
792 return device;
793 }
794
795 private:
796 TestableSurfaceFlinger& mFlinger;
797 sp<BBinder> mDisplayToken = new BBinder();
Lloyd Pique2eef1d22018-09-18 21:30:04 -0700798 DisplayDeviceCreationArgs mCreationArgs;
Peiyong Line9d809e2020-04-14 13:10:48 -0700799 const std::optional<hal::HWDisplayId> mHwcDisplayId;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800800 };
801
Dominik Laskowski8b01cc02020-07-14 19:02:41 -0700802private:
Dominik Laskowskie0e0cde2021-07-30 10:42:05 -0700803 void scheduleComposite(FrameHint) override {}
Dominik Laskowski8b01cc02020-07-14 19:02:41 -0700804 void setVsyncEnabled(bool) override {}
Dominik Laskowski068173d2021-08-11 17:22:59 -0700805 void changeRefreshRate(const RefreshRate&, DisplayModeEvent) override {}
Dominik Laskowski8b01cc02020-07-14 19:02:41 -0700806 void kernelTimerChanged(bool) override {}
Ady Abraham62a0be22020-12-08 16:54:10 -0800807 void triggerOnFrameRateOverridesChanged() {}
Dominik Laskowski8b01cc02020-07-14 19:02:41 -0700808
Lloyd Pique90c115d2018-09-18 21:39:42 -0700809 surfaceflinger::test::Factory mFactory;
810 sp<SurfaceFlinger> mFlinger = new SurfaceFlinger(mFactory, SurfaceFlinger::SkipInitialization);
Dominik Laskowski068173d2021-08-11 17:22:59 -0700811 scheduler::TestableScheduler* mScheduler = nullptr;
Ady Abraham3efa3942021-06-24 19:01:25 -0700812 std::shared_ptr<scheduler::RefreshRateConfigs> mRefreshRateConfigs;
Lloyd Piquef58625d2017-12-19 13:22:33 -0800813};
814
815} // namespace android