blob: d004b9d9eb3801aba94238e1f8e5c19b6b82bc6b [file] [log] [blame]
Lloyd Piquef58625d2017-12-19 13:22:33 -08001/*
2 * Copyright (C) 2018 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#pragma once
18
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080019#include <compositionengine/Display.h>
Lloyd Pique9755fb72019-03-26 14:44:40 -070020#include <compositionengine/LayerFECompositionState.h>
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080021#include <compositionengine/OutputLayer.h>
Lloyd Pique70d91362018-10-18 16:02:55 -070022#include <compositionengine/impl/CompositionEngine.h>
Lloyd Pique9370a482019-10-03 17:58:30 -070023#include <compositionengine/impl/Display.h>
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080024#include <compositionengine/impl/OutputLayerCompositionState.h>
Dominik Laskowskif7a09ed2019-10-07 13:54:18 -070025#include <compositionengine/mock/DisplaySurface.h>
chaviw0ef7caa2021-01-05 11:04:50 -080026#include <gui/ScreenCaptureResults.h>
Lloyd Pique70d91362018-10-18 16:02:55 -070027
Lloyd Pique90c115d2018-09-18 21:39:42 -070028#include "BufferQueueLayer.h"
29#include "BufferStateLayer.h"
Lloyd Pique90c115d2018-09-18 21:39:42 -070030#include "ContainerLayer.h"
Lloyd Piquef58625d2017-12-19 13:22:33 -080031#include "DisplayDevice.h"
Vishnu Nairfa247b12020-02-11 08:58:26 -080032#include "EffectLayer.h"
Ady Abrahamc581d3c2020-08-06 17:34:27 -070033#include "FakeVsyncConfiguration.h"
Adithya Srinivasan2db3c1b2020-11-18 12:43:17 -080034#include "FrameTracer/FrameTracer.h"
Lloyd Piqued6b579f2018-04-06 15:29:10 -070035#include "Layer.h"
Lloyd Pique90c115d2018-09-18 21:39:42 -070036#include "NativeWindowSurface.h"
Dominik Laskowski9dab3432019-03-27 13:21:10 -070037#include "Scheduler/MessageQueue.h"
Steven Thomas2bbaabe2019-08-28 16:08:35 -070038#include "Scheduler/RefreshRateConfigs.h"
Lloyd Pique90c115d2018-09-18 21:39:42 -070039#include "StartPropertySetThread.h"
Lloyd Piquef58625d2017-12-19 13:22:33 -080040#include "SurfaceFlinger.h"
Lloyd Pique24f3bfe2019-10-02 19:29:10 -070041#include "SurfaceFlingerDefaultFactory.h"
Lloyd Pique90c115d2018-09-18 21:39:42 -070042#include "SurfaceInterceptor.h"
Dominik Laskowski7c9dbf92019-08-01 17:57:31 -070043#include "TestableScheduler.h"
Marin Shalamanov045b7002021-01-07 16:56:24 +010044#include "mock/DisplayHardware/MockComposer.h"
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +020045#include "mock/MockDisplayIdGenerator.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
77 std::unique_ptr<MessageQueue> createMessageQueue() override {
Lloyd Pique90c115d2018-09-18 21:39:42 -070078 return std::make_unique<android::impl::MessageQueue>();
79 }
80
Ady Abraham8287e852020-08-12 14:44:58 -070081 std::unique_ptr<scheduler::VsyncConfiguration> createVsyncConfiguration(
Marin Shalamanov526c3382020-12-10 15:22:29 +010082 Fps /*currentRefreshRate*/) override {
Ana Krulec757f63a2019-01-25 10:46:18 -080083 return std::make_unique<scheduler::FakePhaseOffsets>();
84 }
85
Dominik Laskowski8b01cc02020-07-14 19:02:41 -070086 std::unique_ptr<Scheduler> createScheduler(const scheduler::RefreshRateConfigs&,
Ady Abraham3a77a7b2019-12-02 18:46:59 -080087 ISchedulerCallback&) override {
Lloyd Pique90c115d2018-09-18 21:39:42 -070088 return nullptr;
89 }
90
Pablo Gamitoc351d6f2020-09-17 15:34:26 +000091 sp<SurfaceInterceptor> createSurfaceInterceptor() override {
92 return new android::impl::SurfaceInterceptor();
Lloyd Pique90c115d2018-09-18 21:39:42 -070093 }
94
95 sp<StartPropertySetThread> createStartPropertySetThread(bool timestampPropertyValue) override {
Lloyd Pique90c115d2018-09-18 21:39:42 -070096 return new StartPropertySetThread(timestampPropertyValue);
97 }
98
Lloyd Pique9370a482019-10-03 17:58:30 -070099 sp<DisplayDevice> createDisplayDevice(DisplayDeviceCreationArgs& creationArgs) override {
100 return new DisplayDevice(creationArgs);
Lloyd Pique90c115d2018-09-18 21:39:42 -0700101 }
102
103 sp<GraphicBuffer> createGraphicBuffer(uint32_t width, uint32_t height, PixelFormat format,
104 uint32_t layerCount, uint64_t usage,
105 std::string requestorName) override {
Lloyd Pique90c115d2018-09-18 21:39:42 -0700106 return new GraphicBuffer(width, height, format, layerCount, usage, requestorName);
107 }
108
109 void createBufferQueue(sp<IGraphicBufferProducer>* outProducer,
110 sp<IGraphicBufferConsumer>* outConsumer,
111 bool consumerIsSurfaceFlinger) override {
Lloyd Piquee300b312019-10-03 13:03:45 -0700112 if (!mCreateBufferQueue) {
113 BufferQueue::createBufferQueue(outProducer, outConsumer, consumerIsSurfaceFlinger);
114 return;
115 }
Lloyd Pique90c115d2018-09-18 21:39:42 -0700116 mCreateBufferQueue(outProducer, outConsumer, consumerIsSurfaceFlinger);
117 }
118
Lloyd Piquee300b312019-10-03 13:03:45 -0700119 sp<IGraphicBufferProducer> createMonitoredProducer(const sp<IGraphicBufferProducer>& producer,
120 const sp<SurfaceFlinger>& flinger,
121 const wp<Layer>& layer) override {
122 return new MonitoredProducer(producer, flinger, layer);
123 }
124
125 sp<BufferLayerConsumer> createBufferLayerConsumer(const sp<IGraphicBufferConsumer>& consumer,
126 renderengine::RenderEngine& renderEngine,
127 uint32_t textureName, Layer* layer) override {
128 return new BufferLayerConsumer(consumer, renderEngine, textureName, layer);
129 }
130
Lloyd Pique90c115d2018-09-18 21:39:42 -0700131 std::unique_ptr<surfaceflinger::NativeWindowSurface> createNativeWindowSurface(
132 const sp<IGraphicBufferProducer>& producer) override {
133 if (!mCreateNativeWindowSurface) return nullptr;
134 return mCreateNativeWindowSurface(producer);
135 }
136
Lloyd Pique70d91362018-10-18 16:02:55 -0700137 std::unique_ptr<compositionengine::CompositionEngine> createCompositionEngine() override {
138 return compositionengine::impl::createCompositionEngine();
139 }
140
Lloyd Pique90c115d2018-09-18 21:39:42 -0700141 sp<BufferQueueLayer> createBufferQueueLayer(const LayerCreationArgs&) override {
Lloyd Pique90c115d2018-09-18 21:39:42 -0700142 return nullptr;
143 }
144
145 sp<BufferStateLayer> createBufferStateLayer(const LayerCreationArgs&) override {
Lloyd Pique90c115d2018-09-18 21:39:42 -0700146 return nullptr;
147 }
148
Vishnu Nairfa247b12020-02-11 08:58:26 -0800149 sp<EffectLayer> createEffectLayer(const LayerCreationArgs&) override { return nullptr; }
Lloyd Pique90c115d2018-09-18 21:39:42 -0700150
151 sp<ContainerLayer> createContainerLayer(const LayerCreationArgs&) override {
Lloyd Pique90c115d2018-09-18 21:39:42 -0700152 return nullptr;
153 }
154
Adithya Srinivasan2db3c1b2020-11-18 12:43:17 -0800155 std::unique_ptr<FrameTracer> createFrameTracer() override {
156 return std::make_unique<mock::FrameTracer>();
157 }
158
Adithya Srinivasan01189672020-10-20 14:23:05 -0700159 std::unique_ptr<frametimeline::FrameTimeline> createFrameTimeline(
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -0800160 std::shared_ptr<TimeStats> timeStats, pid_t surfaceFlingerPid = 0) override {
161 return std::make_unique<mock::FrameTimeline>(timeStats, surfaceFlingerPid);
Adithya Srinivasan01189672020-10-20 14:23:05 -0700162 }
163
Lloyd Pique90c115d2018-09-18 21:39:42 -0700164 using CreateBufferQueueFunction =
165 std::function<void(sp<IGraphicBufferProducer>* /* outProducer */,
166 sp<IGraphicBufferConsumer>* /* outConsumer */,
167 bool /* consumerIsSurfaceFlinger */)>;
168 CreateBufferQueueFunction mCreateBufferQueue;
169
170 using CreateNativeWindowSurfaceFunction =
171 std::function<std::unique_ptr<surfaceflinger::NativeWindowSurface>(
172 const sp<IGraphicBufferProducer>&)>;
173 CreateNativeWindowSurfaceFunction mCreateNativeWindowSurface;
Lloyd Pique70d91362018-10-18 16:02:55 -0700174
175 using CreateCompositionEngineFunction =
176 std::function<std::unique_ptr<compositionengine::CompositionEngine>()>;
177 CreateCompositionEngineFunction mCreateCompositionEngine;
Lloyd Pique90c115d2018-09-18 21:39:42 -0700178};
179
180} // namespace surfaceflinger::test
Lloyd Piquee39cad22017-12-20 17:01:29 -0800181
Dominik Laskowski8b01cc02020-07-14 19:02:41 -0700182class TestableSurfaceFlinger final : private ISchedulerCallback {
Lloyd Piquef58625d2017-12-19 13:22:33 -0800183public:
Dominik Laskowskib7251f42020-04-20 17:42:59 -0700184 using HotplugEvent = SurfaceFlinger::HotplugEvent;
185
Dominik Laskowskif7a09ed2019-10-07 13:54:18 -0700186 SurfaceFlinger* flinger() { return mFlinger.get(); }
Dominik Laskowski7c9dbf92019-08-01 17:57:31 -0700187 TestableScheduler* scheduler() { return mScheduler; }
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200188 mock::DisplayIdGenerator<GpuVirtualDisplayId>& gpuVirtualDisplayIdGenerator() {
189 return mGpuVirtualDisplayIdGenerator;
190 }
Dominik Laskowski7c9dbf92019-08-01 17:57:31 -0700191
Lloyd Piquef58625d2017-12-19 13:22:33 -0800192 // Extend this as needed for accessing SurfaceFlinger private (and public)
193 // functions.
194
Peiyong Lin833074a2018-08-28 11:53:54 -0700195 void setupRenderEngine(std::unique_ptr<renderengine::RenderEngine> renderEngine) {
Lloyd Piqueb97e04f2018-10-18 17:07:05 -0700196 mFlinger->mCompositionEngine->setRenderEngine(std::move(renderEngine));
Lloyd Piquee39cad22017-12-20 17:01:29 -0800197 }
198
199 void setupComposer(std::unique_ptr<Hwc2::Composer> composer) {
Lloyd Pique441d5042018-10-18 16:49:51 -0700200 mFlinger->mCompositionEngine->setHwComposer(
201 std::make_unique<impl::HWComposer>(std::move(composer)));
Lloyd Piquee39cad22017-12-20 17:01:29 -0800202 }
203
Alec Mourie4034bb2019-11-19 12:45:54 -0800204 void setupTimeStats(const std::shared_ptr<TimeStats>& timeStats) {
205 mFlinger->mCompositionEngine->setTimeStats(timeStats);
206 }
207
Dominik Laskowski8b01cc02020-07-14 19:02:41 -0700208 // The ISchedulerCallback argument can be nullptr for a no-op implementation.
Ady Abraham8cb21882020-08-26 18:22:05 -0700209 void setupScheduler(std::unique_ptr<scheduler::VsyncController> vsyncController,
210 std::unique_ptr<scheduler::VSyncTracker> vsyncTracker,
Dominik Laskowski7c9dbf92019-08-01 17:57:31 -0700211 std::unique_ptr<EventThread> appEventThread,
Ady Abraham8a82ba62020-01-17 12:43:17 -0800212 std::unique_ptr<EventThread> sfEventThread,
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100213 ISchedulerCallback* callback = nullptr, bool hasMultipleModes = false) {
214 DisplayModes modes{DisplayMode::Builder(0)
215 .setId(DisplayModeId(0))
216 .setVsyncPeriod(16'666'667)
217 .setGroup(0)
218 .build()};
Ady Abrahamabc27602020-04-08 17:20:29 -0700219
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100220 if (hasMultipleModes) {
221 modes.emplace_back(DisplayMode::Builder(1)
222 .setId(DisplayModeId(1))
223 .setVsyncPeriod(11'111'111)
224 .setGroup(0)
225 .build());
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700226 }
227
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100228 const auto currMode = DisplayModeId(0);
Marin Shalamanov68a94092020-11-24 17:48:00 +0100229 mFlinger->mRefreshRateConfigs =
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100230 std::make_unique<scheduler::RefreshRateConfigs>(modes, currMode);
Marin Shalamanov68a94092020-11-24 17:48:00 +0100231 const auto currFps =
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100232 mFlinger->mRefreshRateConfigs->getRefreshRateFromModeId(currMode).getFps();
Marin Shalamanov68a94092020-11-24 17:48:00 +0100233 mFlinger->mRefreshRateStats =
234 std::make_unique<scheduler::RefreshRateStats>(*mFlinger->mTimeStats, currFps,
235 /*powerMode=*/hal::PowerMode::OFF);
Marin Shalamanov526c3382020-12-10 15:22:29 +0100236 mFlinger->mVsyncConfiguration = mFactory.createVsyncConfiguration(currFps);
Ady Abraham8287e852020-08-12 14:44:58 -0700237 mFlinger->mVsyncModulator.emplace(mFlinger->mVsyncConfiguration->getCurrentConfigs());
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700238
Ady Abraham8cb21882020-08-26 18:22:05 -0700239 mScheduler = new TestableScheduler(std::move(vsyncController), std::move(vsyncTracker),
Marin Shalamanov27fa3de2020-11-20 16:22:48 +0100240 *mFlinger->mRefreshRateConfigs, *(callback ?: this));
Dominik Laskowski7c9dbf92019-08-01 17:57:31 -0700241
Dominik Laskowski98041832019-08-01 18:35:59 -0700242 mFlinger->mAppConnectionHandle = mScheduler->createConnection(std::move(appEventThread));
243 mFlinger->mSfConnectionHandle = mScheduler->createConnection(std::move(sfEventThread));
Dominik Laskowskif7a09ed2019-10-07 13:54:18 -0700244 resetScheduler(mScheduler);
Dominik Laskowski7c9dbf92019-08-01 17:57:31 -0700245 }
246
Dominik Laskowskif7a09ed2019-10-07 13:54:18 -0700247 void resetScheduler(Scheduler* scheduler) { mFlinger->mScheduler.reset(scheduler); }
248
Ady Abraham44e9f3b2021-02-16 15:22:58 -0800249 TestableScheduler& mutableScheduler() const { return *mScheduler; }
250
Lloyd Pique90c115d2018-09-18 21:39:42 -0700251 using CreateBufferQueueFunction = surfaceflinger::test::Factory::CreateBufferQueueFunction;
Lloyd Pique5b36f3f2018-01-17 11:57:07 -0800252 void setCreateBufferQueueFunction(CreateBufferQueueFunction f) {
Lloyd Pique90c115d2018-09-18 21:39:42 -0700253 mFactory.mCreateBufferQueue = f;
Lloyd Pique5b36f3f2018-01-17 11:57:07 -0800254 }
255
Lloyd Pique90c115d2018-09-18 21:39:42 -0700256 using CreateNativeWindowSurfaceFunction =
257 surfaceflinger::test::Factory::CreateNativeWindowSurfaceFunction;
Lloyd Pique0b1fe702018-01-22 18:03:16 -0800258 void setCreateNativeWindowSurface(CreateNativeWindowSurfaceFunction f) {
Lloyd Pique90c115d2018-09-18 21:39:42 -0700259 mFactory.mCreateNativeWindowSurface = f;
Lloyd Pique0b1fe702018-01-22 18:03:16 -0800260 }
261
Daniel Solomon42d04562019-01-20 21:03:19 -0800262 void setInternalDisplayPrimaries(const ui::DisplayPrimaries& primaries) {
263 memcpy(&mFlinger->mInternalDisplayPrimaries, &primaries, sizeof(ui::DisplayPrimaries));
264 }
265
Dominik Laskowskib7251f42020-04-20 17:42:59 -0700266 static auto& mutableLayerCurrentState(const sp<Layer>& layer) { return layer->mCurrentState; }
267 static auto& mutableLayerDrawingState(const sp<Layer>& layer) { return layer->mDrawingState; }
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700268
Valerie Haud251afb2019-03-29 14:19:02 -0700269 auto& mutableStateLock() { return mFlinger->mStateLock; }
270
Dominik Laskowskib7251f42020-04-20 17:42:59 -0700271 static auto findOutputLayerForDisplay(const sp<Layer>& layer,
272 const sp<const DisplayDevice>& display) {
273 return layer->findOutputLayerForDisplay(display.get());
274 }
275
276 static void setLayerSidebandStream(const sp<Layer>& layer,
277 const sp<NativeHandle>& sidebandStream) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800278 layer->mDrawingState.sidebandStream = sidebandStream;
Lloyd Pique0b785d82018-12-04 17:25:27 -0800279 layer->mSidebandStream = sidebandStream;
Lloyd Piquede196652020-01-22 17:29:58 -0800280 layer->editCompositionState()->sidebandStream = sidebandStream;
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700281 }
282
Dominik Laskowskib7251f42020-04-20 17:42:59 -0700283 void setLayerCompositionType(const sp<Layer>& layer, hal::Composition type) {
284 auto outputLayer = findOutputLayerForDisplay(layer, mFlinger->getDefaultDisplayDevice());
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800285 LOG_ALWAYS_FATAL_IF(!outputLayer);
286 auto& state = outputLayer->editState();
287 LOG_ALWAYS_FATAL_IF(!outputLayer->getState().hwc);
Peiyong Line9d809e2020-04-14 13:10:48 -0700288 (*state.hwc).hwcCompositionType = type;
Dominik Laskowskib7251f42020-04-20 17:42:59 -0700289 }
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800290
Dominik Laskowskib7251f42020-04-20 17:42:59 -0700291 static void setLayerPotentialCursor(const sp<Layer>& layer, bool potentialCursor) {
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700292 layer->mPotentialCursor = potentialCursor;
293 }
294
Garfield Tande619fa2020-10-02 17:13:53 -0700295 static void setLayerDrawingParent(const sp<Layer>& layer, const sp<Layer>& drawingParent) {
296 layer->mDrawingParent = drawingParent;
297 }
298
Lloyd Piquef58625d2017-12-19 13:22:33 -0800299 /* ------------------------------------------------------------------------
300 * Forwarding for functions being tested
301 */
Lloyd Pique0b1fe702018-01-22 18:03:16 -0800302
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
Lloyd Pique9370a482019-10-03 17:58:30 -0700311 auto setupNewDisplayDeviceInternal(
312 const wp<IBinder>& displayToken,
313 std::shared_ptr<compositionengine::Display> compositionDisplay,
314 const DisplayDeviceState& state,
315 const sp<compositionengine::DisplaySurface>& dispSurface,
Dominik Laskowski0a1435d2020-04-21 00:27:31 -0700316 const sp<IGraphicBufferProducer>& producer) NO_THREAD_SAFETY_ANALYSIS {
Lloyd Pique9370a482019-10-03 17:58:30 -0700317 return mFlinger->setupNewDisplayDeviceInternal(displayToken, compositionDisplay, state,
318 dispSurface, producer);
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800319 }
320
Lloyd Pique0b1fe702018-01-22 18:03:16 -0800321 auto handleTransactionLocked(uint32_t transactionFlags) {
Vishnu Nairfa5f8df2019-02-13 17:02:51 -0800322 Mutex::Autolock _l(mFlinger->mStateLock);
Lloyd Pique0b1fe702018-01-22 18:03:16 -0800323 return mFlinger->handleTransactionLocked(transactionFlags);
324 }
Lloyd Piquef58625d2017-12-19 13:22:33 -0800325
Peiyong Line9d809e2020-04-14 13:10:48 -0700326 auto onHotplugReceived(int32_t sequenceId, hal::HWDisplayId display,
327 hal::Connection connection) {
Lloyd Pique6cf11032018-01-22 18:57:44 -0800328 return mFlinger->onHotplugReceived(sequenceId, display, connection);
329 }
330
Vishnu Nairce5d0cc2019-02-28 14:38:41 -0800331 auto setDisplayStateLocked(const DisplayState& s) {
332 Mutex::Autolock _l(mFlinger->mStateLock);
333 return mFlinger->setDisplayStateLocked(s);
334 }
Lloyd Pique9d9cf402018-02-16 17:47:13 -0800335
Dominik Laskowski83b88212018-12-11 13:34:06 -0800336 // Allow reading display state without locking, as if called on the SF main thread.
337 auto onInitializeDisplays() NO_THREAD_SAFETY_ANALYSIS {
338 return mFlinger->onInitializeDisplays();
339 }
Lloyd Pique86016da2018-03-01 16:09:38 -0800340
Lais Andrade3a6e47d2020-04-02 11:20:16 +0100341 auto notifyPowerBoost(int32_t boostId) { return mFlinger->notifyPowerBoost(boostId); }
342
Dominik Laskowski83b88212018-12-11 13:34:06 -0800343 // Allow reading display state without locking, as if called on the SF main thread.
344 auto setPowerModeInternal(const sp<DisplayDevice>& display,
Peiyong Lin65248e02020-04-18 21:15:07 -0700345 hal::PowerMode mode) NO_THREAD_SAFETY_ANALYSIS {
Dominik Laskowskie9774092018-12-11 10:04:24 -0800346 return mFlinger->setPowerModeInternal(display, mode);
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -0800347 }
348
Adithya Srinivasan5f683cf2020-09-15 14:21:04 -0700349 auto onMessageReceived(int32_t what) {
350 return mFlinger->onMessageReceived(what, /*vsyncId=*/0, systemTime());
351 }
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700352
chaviw426b5c02020-07-27 11:20:15 -0700353 auto renderScreenImplLocked(const RenderArea& renderArea,
354 SurfaceFlinger::TraverseLayersFunction traverseLayers,
Alec Mouria90a5702021-04-16 16:36:21 +0000355 const std::shared_ptr<renderengine::ExternalTexture>& buffer,
356 bool forSystem, bool regionSampling) {
chaviw426b5c02020-07-27 11:20:15 -0700357 ScreenCaptureResults captureResults;
chaviwc6ad8af2020-08-03 11:33:30 -0700358 return mFlinger->renderScreenImplLocked(renderArea, traverseLayers, buffer, forSystem,
chaviw17ac24b2021-01-28 18:50:05 -0800359 regionSampling, false /* grayscale */,
360 captureResults);
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700361 }
362
chaviw4b9d5e12020-08-04 18:30:35 -0700363 auto traverseLayersInLayerStack(ui::LayerStack layerStack, int32_t uid,
Marin Shalamanov1c434292020-06-12 01:47:29 +0200364 const LayerVector::Visitor& visitor) {
chaviw4b9d5e12020-08-04 18:30:35 -0700365 return mFlinger->SurfaceFlinger::traverseLayersInLayerStack(layerStack, uid, visitor);
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700366 }
367
Daniel Solomon42d04562019-01-20 21:03:19 -0800368 auto getDisplayNativePrimaries(const sp<IBinder>& displayToken,
369 ui::DisplayPrimaries &primaries) {
370 return mFlinger->SurfaceFlinger::getDisplayNativePrimaries(displayToken, primaries);
371 }
372
Arthur Hung58144272021-01-16 03:43:53 +0000373 auto& getTransactionQueue() { return mFlinger->mTransactionQueue; }
374 auto& getPendingTransactionQueue() { return mFlinger->mPendingTransactionQueues; }
Valerie Haud251afb2019-03-29 14:19:02 -0700375
Siarhei Vishniakoufc434ac2021-01-13 10:28:00 -1000376 auto setTransactionState(
377 const FrameTimelineInfo& frameTimelineInfo, const Vector<ComposerState>& states,
378 const Vector<DisplayState>& displays, uint32_t flags, const sp<IBinder>& applyToken,
379 const InputWindowCommands& inputWindowCommands, int64_t desiredPresentTime,
380 bool isAutoTimestamp, const client_cache_t& uncacheBuffer, bool hasListenerCallbacks,
381 std::vector<ListenerCallbacks>& listenerCallbacks, uint64_t transactionId) {
382 return mFlinger->setTransactionState(frameTimelineInfo, states, displays, flags, applyToken,
383 inputWindowCommands, desiredPresentTime,
Ady Abrahamf0c56492020-12-17 18:04:15 -0800384 isAutoTimestamp, uncacheBuffer, hasListenerCallbacks,
385 listenerCallbacks, transactionId);
Valerie Haud251afb2019-03-29 14:19:02 -0700386 }
387
Zhuoyao Zhang3d3540d2021-01-14 05:14:54 +0000388 auto flushTransactionQueues() { return mFlinger->flushTransactionQueues(); };
Valerie Haud251afb2019-03-29 14:19:02 -0700389
Eduardo Rochaac867192020-09-14 20:12:17 +0000390 auto onTransact(uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) {
391 return mFlinger->onTransact(code, data, reply, flags);
392 }
393
Ana Krulec31f2b3c2020-12-14 14:30:09 -0800394 auto getGPUContextPriority() { return mFlinger->getGPUContextPriority(); }
395
Ady Abraham564f9de2021-02-03 18:34:33 -0800396 auto calculateExtraBufferCount(Fps maxSupportedRefreshRate,
397 std::chrono::nanoseconds presentLatency) const {
398 return SurfaceFlinger::calculateExtraBufferCount(maxSupportedRefreshRate, presentLatency);
399 }
400
Lloyd Pique86016da2018-03-01 16:09:38 -0800401 /* ------------------------------------------------------------------------
402 * Read-only access to private data to assert post-conditions.
403 */
404
405 const auto& getAnimFrameTracker() const { return mFlinger->mAnimFrameTracker; }
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -0800406 const auto& getHasPoweredOff() const { return mFlinger->mHasPoweredOff; }
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -0800407 const auto& getVisibleRegionsDirty() const { return mFlinger->mVisibleRegionsDirty; }
Lloyd Pique441d5042018-10-18 16:49:51 -0700408 auto& getHwComposer() const {
409 return static_cast<impl::HWComposer&>(mFlinger->getHwComposer());
410 }
Lloyd Pique9370a482019-10-03 17:58:30 -0700411 auto& getCompositionEngine() const { return mFlinger->getCompositionEngine(); }
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -0800412
Lloyd Pique86016da2018-03-01 16:09:38 -0800413 const auto& getCompositorTiming() const { return mFlinger->getBE().mCompositorTiming; }
414
Adithya Srinivasanb238cd52021-02-04 17:54:05 +0000415 mock::FrameTracer* getFrameTracer() const {
416 return static_cast<mock::FrameTracer*>(mFlinger->mFrameTracer.get());
417 }
418
Lloyd Piquef58625d2017-12-19 13:22:33 -0800419 /* ------------------------------------------------------------------------
420 * Read-write access to private data to set up preconditions and assert
421 * post-conditions.
422 */
Lloyd Pique0b1fe702018-01-22 18:03:16 -0800423
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800424 auto& mutableHasWideColorDisplay() { return SurfaceFlinger::hasWideColorDisplay; }
Peiyong Lin13effd12018-07-24 17:01:47 -0700425 auto& mutableUseColorManagement() { return SurfaceFlinger::useColorManagement; }
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800426
Lloyd Piquee39cad22017-12-20 17:01:29 -0800427 auto& mutableCurrentState() { return mFlinger->mCurrentState; }
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800428 auto& mutableDisplayColorSetting() { return mFlinger->mDisplayColorSetting; }
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700429 auto& mutableDisplays() { return mFlinger->mDisplays; }
Lloyd Piquee39cad22017-12-20 17:01:29 -0800430 auto& mutableDrawingState() { return mFlinger->mDrawingState; }
Lloyd Piquee39cad22017-12-20 17:01:29 -0800431 auto& mutableEventQueue() { return mFlinger->mEventQueue; }
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700432 auto& mutableGeometryInvalid() { return mFlinger->mGeometryInvalid; }
Lloyd Pique0b1fe702018-01-22 18:03:16 -0800433 auto& mutableInterceptor() { return mFlinger->mInterceptor; }
Lloyd Pique6cf11032018-01-22 18:57:44 -0800434 auto& mutableMainThreadId() { return mFlinger->mMainThreadId; }
Lloyd Pique0b1fe702018-01-22 18:03:16 -0800435 auto& mutablePendingHotplugEvents() { return mFlinger->mPendingHotplugEvents; }
Dominik Laskowski075d3172018-05-24 15:50:06 -0700436 auto& mutablePhysicalDisplayTokens() { return mFlinger->mPhysicalDisplayTokens; }
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700437 auto& mutableTexturePool() { return mFlinger->mTexturePool; }
Lloyd Pique0b1fe702018-01-22 18:03:16 -0800438 auto& mutableTransactionFlags() { return mFlinger->mTransactionFlags; }
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800439 auto& mutableUseHwcVirtualDisplays() { return mFlinger->mUseHwcVirtualDisplays; }
Peiyong Lin74ca2f42019-01-14 19:36:57 -0800440 auto& mutablePowerAdvisor() { return mFlinger->mPowerAdvisor; }
Lloyd Pique4fe29402019-08-12 16:51:24 -0700441 auto& mutableDebugDisableHWC() { return mFlinger->mDebugDisableHWC; }
Lloyd Piquee39cad22017-12-20 17:01:29 -0800442
Lloyd Pique6cf11032018-01-22 18:57:44 -0800443 auto& mutableComposerSequenceId() { return mFlinger->getBE().mComposerSequenceId; }
Lloyd Pique441d5042018-10-18 16:49:51 -0700444 auto& mutableHwcDisplayData() { return getHwComposer().mDisplayData; }
445 auto& mutableHwcPhysicalDisplayIdMap() { return getHwComposer().mPhysicalDisplayIdMap; }
446 auto& mutableInternalHwcDisplayId() { return getHwComposer().mInternalHwcDisplayId; }
447 auto& mutableExternalHwcDisplayId() { return getHwComposer().mExternalHwcDisplayId; }
Ady Abraham60e42ea2020-03-09 19:17:31 -0700448 auto& mutableUseFrameRateApi() { return mFlinger->useFrameRateApi; }
Lloyd Piquea618d852018-01-17 11:52:30 -0800449
Valerie Hau09e60052019-12-15 14:51:15 -0800450 auto fromHandle(const sp<IBinder>& handle) {
Valerie Hau09e60052019-12-15 14:51:15 -0800451 return mFlinger->fromHandle(handle);
452 }
453
Lloyd Piquee39cad22017-12-20 17:01:29 -0800454 ~TestableSurfaceFlinger() {
455 // All these pointer and container clears help ensure that GMock does
456 // not report a leaked object, since the SurfaceFlinger instance may
457 // still be referenced by something despite our best efforts to destroy
458 // it after each test is done.
459 mutableDisplays().clear();
Ady Abraham2939f092019-03-04 17:44:06 -0800460 mutableCurrentState().displays.clear();
461 mutableDrawingState().displays.clear();
Lloyd Pique0b1fe702018-01-22 18:03:16 -0800462 mutableEventQueue().reset();
Pablo Gamito5cfc6e52020-09-10 11:18:03 +0000463 mutableInterceptor().clear();
Lloyd Pique2eb71eb2019-08-13 13:23:03 -0700464 mFlinger->mScheduler.reset();
Lloyd Pique441d5042018-10-18 16:49:51 -0700465 mFlinger->mCompositionEngine->setHwComposer(std::unique_ptr<HWComposer>());
Lloyd Piqueb97e04f2018-10-18 17:07:05 -0700466 mFlinger->mCompositionEngine->setRenderEngine(
467 std::unique_ptr<renderengine::RenderEngine>());
Lloyd Piquee39cad22017-12-20 17:01:29 -0800468 }
Lloyd Piquef58625d2017-12-19 13:22:33 -0800469
Lloyd Piquea618d852018-01-17 11:52:30 -0800470 /* ------------------------------------------------------------------------
471 * Wrapper classes for Read-write access to private data to set up
472 * preconditions and assert post-conditions.
473 */
Ana Krulec4593b692019-01-11 22:07:25 -0800474 struct HWC2Display : public HWC2::impl::Display {
Peiyong Lin74ca2f42019-01-14 19:36:57 -0800475 HWC2Display(Hwc2::Composer& composer,
Peiyong Line9d809e2020-04-14 13:10:48 -0700476 const std::unordered_set<hal::Capability>& capabilities, hal::HWDisplayId id,
477 hal::DisplayType type)
Peiyong Lin74ca2f42019-01-14 19:36:57 -0800478 : HWC2::impl::Display(composer, capabilities, id, type) {}
Lloyd Piquea618d852018-01-17 11:52:30 -0800479 ~HWC2Display() {
480 // Prevents a call to disable vsyncs.
Peiyong Line9d809e2020-04-14 13:10:48 -0700481 mType = hal::DisplayType::INVALID;
Lloyd Piquea618d852018-01-17 11:52:30 -0800482 }
483
484 auto& mutableIsConnected() { return this->mIsConnected; }
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700485 auto& mutableLayers() { return this->mLayers; }
Lloyd Piquea618d852018-01-17 11:52:30 -0800486 };
487
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800488 class FakeHwcDisplayInjector {
489 public:
Peiyong Line9d809e2020-04-14 13:10:48 -0700490 static constexpr hal::HWDisplayId DEFAULT_HWC_DISPLAY_ID = 1000;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800491 static constexpr int32_t DEFAULT_WIDTH = 1920;
492 static constexpr int32_t DEFAULT_HEIGHT = 1280;
Marin Shalamanov045b7002021-01-07 16:56:24 +0100493 static constexpr int32_t DEFAULT_VSYNC_PERIOD = 16'666'666;
Ady Abraham2139f732019-11-13 18:56:40 -0800494 static constexpr int32_t DEFAULT_CONFIG_GROUP = 7;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800495 static constexpr int32_t DEFAULT_DPI = 320;
Peiyong Line9d809e2020-04-14 13:10:48 -0700496 static constexpr hal::HWConfigId DEFAULT_ACTIVE_CONFIG = 0;
Peiyong Lin65248e02020-04-18 21:15:07 -0700497 static constexpr hal::PowerMode DEFAULT_POWER_MODE = hal::PowerMode::ON;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800498
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200499 FakeHwcDisplayInjector(HalDisplayId displayId, hal::DisplayType hwcDisplayType,
500 bool isPrimary)
Dominik Laskowski075d3172018-05-24 15:50:06 -0700501 : mDisplayId(displayId), mHwcDisplayType(hwcDisplayType), mIsPrimary(isPrimary) {}
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800502
Peiyong Line9d809e2020-04-14 13:10:48 -0700503 auto& setHwcDisplayId(hal::HWDisplayId displayId) {
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800504 mHwcDisplayId = displayId;
505 return *this;
506 }
507
508 auto& setWidth(int32_t width) {
509 mWidth = width;
510 return *this;
511 }
512
513 auto& setHeight(int32_t height) {
514 mHeight = height;
515 return *this;
516 }
517
Marin Shalamanov045b7002021-01-07 16:56:24 +0100518 auto& setVsyncPeriod(int32_t vsyncPeriod) {
519 mVsyncPeriod = vsyncPeriod;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800520 return *this;
521 }
522
523 auto& setDpiX(int32_t dpi) {
524 mDpiX = dpi;
525 return *this;
526 }
527
528 auto& setDpiY(int32_t dpi) {
529 mDpiY = dpi;
530 return *this;
531 }
532
Peiyong Line9d809e2020-04-14 13:10:48 -0700533 auto& setActiveConfig(hal::HWConfigId config) {
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800534 mActiveConfig = config;
535 return *this;
536 }
537
Peiyong Line9d809e2020-04-14 13:10:48 -0700538 auto& setCapabilities(const std::unordered_set<hal::Capability>* capabilities) {
Lloyd Piquee22f0332018-07-16 16:35:56 -0700539 mCapabilities = capabilities;
540 return *this;
541 }
542
Peiyong Lin65248e02020-04-18 21:15:07 -0700543 auto& setPowerMode(hal::PowerMode mode) {
Peiyong Lin1336e6e2019-05-28 09:23:50 -0700544 mPowerMode = mode;
545 return *this;
546 }
547
Marin Shalamanov045b7002021-01-07 16:56:24 +0100548 void inject(TestableSurfaceFlinger* flinger, Hwc2::mock::Composer* composer) {
549 using ::testing::_;
550 using ::testing::DoAll;
551 using ::testing::Return;
552 using ::testing::SetArgPointee;
553
Peiyong Line9d809e2020-04-14 13:10:48 -0700554 static const std::unordered_set<hal::Capability> defaultCapabilities;
Lloyd Piquee22f0332018-07-16 16:35:56 -0700555 if (mCapabilities == nullptr) mCapabilities = &defaultCapabilities;
556
557 // Caution - Make sure that any values passed by reference here do
558 // not refer to an instance owned by FakeHwcDisplayInjector. This
559 // class has temporary lifetime, while the constructed HWC2::Display
560 // is much longer lived.
Peiyong Lin74ca2f42019-01-14 19:36:57 -0800561 auto display = std::make_unique<HWC2Display>(*composer, *mCapabilities, mHwcDisplayId,
562 mHwcDisplayType);
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800563
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800564 display->mutableIsConnected() = true;
Peiyong Lin65248e02020-04-18 21:15:07 -0700565 display->setPowerMode(mPowerMode);
Peiyong Linbdd08cc2019-12-17 21:35:14 -0800566 flinger->mutableHwcDisplayData()[mDisplayId].hwcDisplay = std::move(display);
Dominik Laskowski075d3172018-05-24 15:50:06 -0700567
Marin Shalamanov045b7002021-01-07 16:56:24 +0100568 EXPECT_CALL(*composer, getDisplayConfigs(mHwcDisplayId, _))
569 .WillRepeatedly(
570 DoAll(SetArgPointee<1>(std::vector<hal::HWConfigId>{mActiveConfig}),
571 Return(hal::Error::NONE)));
572
573 EXPECT_CALL(*composer,
574 getDisplayAttribute(mHwcDisplayId, mActiveConfig, hal::Attribute::WIDTH, _))
575 .WillRepeatedly(DoAll(SetArgPointee<3>(mWidth), Return(hal::Error::NONE)));
576
577 EXPECT_CALL(*composer,
578 getDisplayAttribute(mHwcDisplayId, mActiveConfig, hal::Attribute::HEIGHT,
579 _))
580 .WillRepeatedly(DoAll(SetArgPointee<3>(mHeight), Return(hal::Error::NONE)));
581
582 EXPECT_CALL(*composer,
583 getDisplayAttribute(mHwcDisplayId, mActiveConfig,
584 hal::Attribute::VSYNC_PERIOD, _))
585 .WillRepeatedly(
586 DoAll(SetArgPointee<3>(mVsyncPeriod), Return(hal::Error::NONE)));
587
588 EXPECT_CALL(*composer,
589 getDisplayAttribute(mHwcDisplayId, mActiveConfig, hal::Attribute::DPI_X, _))
590 .WillRepeatedly(DoAll(SetArgPointee<3>(mDpiX), Return(hal::Error::NONE)));
591
592 EXPECT_CALL(*composer,
593 getDisplayAttribute(mHwcDisplayId, mActiveConfig, hal::Attribute::DPI_Y, _))
594 .WillRepeatedly(DoAll(SetArgPointee<3>(mDpiY), Return(hal::Error::NONE)));
595
596 EXPECT_CALL(*composer,
597 getDisplayAttribute(mHwcDisplayId, mActiveConfig,
598 hal::Attribute::CONFIG_GROUP, _))
599 .WillRepeatedly(
600 DoAll(SetArgPointee<3>(mConfigGroup), Return(hal::Error::NONE)));
Marin Shalamanov3ea1d602020-12-16 19:59:39 +0100601
Peiyong Line9d809e2020-04-14 13:10:48 -0700602 if (mHwcDisplayType == hal::DisplayType::PHYSICAL) {
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200603 const auto physicalId = PhysicalDisplayId::tryCast(mDisplayId);
604 LOG_ALWAYS_FATAL_IF(!physicalId);
605 flinger->mutableHwcPhysicalDisplayIdMap().emplace(mHwcDisplayId, *physicalId);
Marin Shalamanov6e840172020-12-14 22:13:28 +0100606 if (mIsPrimary) {
607 flinger->mutableInternalHwcDisplayId() = mHwcDisplayId;
608 } else {
609 // If there is an external HWC display there should always be an internal ID
610 // as well. Set it to some arbitrary value.
611 auto& internalId = flinger->mutableInternalHwcDisplayId();
612 if (!internalId) internalId = mHwcDisplayId - 1;
613 flinger->mutableExternalHwcDisplayId() = mHwcDisplayId;
614 }
Dominik Laskowski075d3172018-05-24 15:50:06 -0700615 }
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800616 }
617
618 private:
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200619 const HalDisplayId mDisplayId;
Peiyong Line9d809e2020-04-14 13:10:48 -0700620 const hal::DisplayType mHwcDisplayType;
Dominik Laskowski075d3172018-05-24 15:50:06 -0700621 const bool mIsPrimary;
622
Peiyong Line9d809e2020-04-14 13:10:48 -0700623 hal::HWDisplayId mHwcDisplayId = DEFAULT_HWC_DISPLAY_ID;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800624 int32_t mWidth = DEFAULT_WIDTH;
625 int32_t mHeight = DEFAULT_HEIGHT;
Marin Shalamanov045b7002021-01-07 16:56:24 +0100626 int32_t mVsyncPeriod = DEFAULT_VSYNC_PERIOD;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800627 int32_t mDpiX = DEFAULT_DPI;
628 int32_t mDpiY = DEFAULT_DPI;
Marin Shalamanov045b7002021-01-07 16:56:24 +0100629 int32_t mConfigGroup = DEFAULT_CONFIG_GROUP;
Peiyong Line9d809e2020-04-14 13:10:48 -0700630 hal::HWConfigId mActiveConfig = DEFAULT_ACTIVE_CONFIG;
Peiyong Lin65248e02020-04-18 21:15:07 -0700631 hal::PowerMode mPowerMode = DEFAULT_POWER_MODE;
Peiyong Line9d809e2020-04-14 13:10:48 -0700632 const std::unordered_set<hal::Capability>* mCapabilities = nullptr;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800633 };
634
635 class FakeDisplayDeviceInjector {
636 public:
Dominik Laskowski075d3172018-05-24 15:50:06 -0700637 FakeDisplayDeviceInjector(TestableSurfaceFlinger& flinger,
Lloyd Pique9370a482019-10-03 17:58:30 -0700638 std::shared_ptr<compositionengine::Display> compositionDisplay,
Marin Shalamanov228f46b2021-01-28 21:11:45 +0100639 std::optional<ui::DisplayConnectionType> connectionType,
Peiyong Line9d809e2020-04-14 13:10:48 -0700640 std::optional<hal::HWDisplayId> hwcDisplayId, bool isPrimary)
Lloyd Pique9370a482019-10-03 17:58:30 -0700641 : mFlinger(flinger),
Marin Shalamanov12c9e5a2021-01-07 00:25:35 +0100642 mCreationArgs(flinger.mFlinger.get(), flinger.mFlinger->getHwComposer(),
643 mDisplayToken, compositionDisplay),
Marin Shalamanov700e6392020-02-12 20:22:26 +0100644 mHwcDisplayId(hwcDisplayId) {
Dominik Laskowski55c85402020-01-21 16:25:47 -0800645 mCreationArgs.connectionType = connectionType;
Dominik Laskowski075d3172018-05-24 15:50:06 -0700646 mCreationArgs.isPrimary = isPrimary;
Marin Shalamanov045b7002021-01-07 16:56:24 +0100647
648 mActiveModeId = DisplayModeId(0);
649 DisplayModePtr activeMode =
650 DisplayMode::Builder(FakeHwcDisplayInjector::DEFAULT_ACTIVE_CONFIG)
651 .setId(mActiveModeId)
652 .setWidth(FakeHwcDisplayInjector::DEFAULT_WIDTH)
653 .setHeight(FakeHwcDisplayInjector::DEFAULT_HEIGHT)
654 .setVsyncPeriod(FakeHwcDisplayInjector::DEFAULT_VSYNC_PERIOD)
655 .setDpiX(FakeHwcDisplayInjector::DEFAULT_DPI)
656 .setDpiY(FakeHwcDisplayInjector::DEFAULT_DPI)
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100657 .setGroup(0)
Marin Shalamanov045b7002021-01-07 16:56:24 +0100658 .build();
659
660 DisplayModes modes{activeMode};
661 mCreationArgs.supportedModes = modes;
Dominik Laskowski075d3172018-05-24 15:50:06 -0700662 }
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800663
664 sp<IBinder> token() const { return mDisplayToken; }
665
666 DisplayDeviceState& mutableDrawingDisplayState() {
667 return mFlinger.mutableDrawingState().displays.editValueFor(mDisplayToken);
668 }
669
670 DisplayDeviceState& mutableCurrentDisplayState() {
671 return mFlinger.mutableCurrentState().displays.editValueFor(mDisplayToken);
672 }
673
Lloyd Pique9d9cf402018-02-16 17:47:13 -0800674 const auto& getDrawingDisplayState() {
675 return mFlinger.mutableDrawingState().displays.valueFor(mDisplayToken);
676 }
677
678 const auto& getCurrentDisplayState() {
679 return mFlinger.mutableCurrentState().displays.valueFor(mDisplayToken);
680 }
681
Dominik Laskowski9fae1022018-05-29 13:17:40 -0700682 auto& mutableDisplayDevice() { return mFlinger.mutableDisplays()[mDisplayToken]; }
Lloyd Pique9d9cf402018-02-16 17:47:13 -0800683
Marin Shalamanov045b7002021-01-07 16:56:24 +0100684 auto& setActiveMode(DisplayModeId mode) {
685 mActiveModeId = mode;
686 return *this;
687 }
688
689 auto& setSupportedModes(DisplayModes mode) {
690 mCreationArgs.supportedModes = mode;
691 return *this;
692 }
693
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800694 auto& setNativeWindow(const sp<ANativeWindow>& nativeWindow) {
Lloyd Pique2eef1d22018-09-18 21:30:04 -0700695 mCreationArgs.nativeWindow = nativeWindow;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800696 return *this;
697 }
698
Lloyd Pique542307f2018-10-19 13:24:08 -0700699 auto& setDisplaySurface(const sp<compositionengine::DisplaySurface>& displaySurface) {
Lloyd Pique2eef1d22018-09-18 21:30:04 -0700700 mCreationArgs.displaySurface = displaySurface;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800701 return *this;
702 }
703
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800704 auto& setSecure(bool secure) {
Lloyd Pique2eef1d22018-09-18 21:30:04 -0700705 mCreationArgs.isSecure = secure;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800706 return *this;
707 }
708
Peiyong Lin65248e02020-04-18 21:15:07 -0700709 auto& setPowerMode(hal::PowerMode mode) {
Lloyd Pique2eef1d22018-09-18 21:30:04 -0700710 mCreationArgs.initialPowerMode = mode;
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700711 return *this;
712 }
713
Valerie Hau9758ae02018-10-09 16:05:09 -0700714 auto& setHwcColorModes(
715 const std::unordered_map<ui::ColorMode, std::vector<ui::RenderIntent>>
716 hwcColorModes) {
717 mCreationArgs.hwcColorModes = hwcColorModes;
718 return *this;
719 }
720
721 auto& setHasWideColorGamut(bool hasWideColorGamut) {
722 mCreationArgs.hasWideColorGamut = hasWideColorGamut;
723 return *this;
724 }
725
Lloyd Pique33050472019-12-19 17:12:44 -0800726 auto& setPhysicalOrientation(ui::Rotation orientation) {
727 mCreationArgs.physicalOrientation = orientation;
728 return *this;
729 }
730
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800731 sp<DisplayDevice> inject() {
Lloyd Pique9370a482019-10-03 17:58:30 -0700732 const auto displayId = mCreationArgs.compositionDisplay->getDisplayId();
733
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800734 DisplayDeviceState state;
Dominik Laskowski55c85402020-01-21 16:25:47 -0800735 if (const auto type = mCreationArgs.connectionType) {
Lloyd Pique9370a482019-10-03 17:58:30 -0700736 LOG_ALWAYS_FATAL_IF(!displayId);
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200737 const auto physicalId = PhysicalDisplayId::tryCast(*displayId);
738 LOG_ALWAYS_FATAL_IF(!physicalId);
Marin Shalamanov700e6392020-02-12 20:22:26 +0100739 LOG_ALWAYS_FATAL_IF(!mHwcDisplayId);
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200740 state.physical = {.id = *physicalId, .type = *type, .hwcDisplayId = *mHwcDisplayId};
Dominik Laskowski55c85402020-01-21 16:25:47 -0800741 }
742
Lloyd Pique2eef1d22018-09-18 21:30:04 -0700743 state.isSecure = mCreationArgs.isSecure;
744
Lloyd Pique9370a482019-10-03 17:58:30 -0700745 sp<DisplayDevice> device = new DisplayDevice(mCreationArgs);
Marin Shalamanov045b7002021-01-07 16:56:24 +0100746 if (!device->isVirtual()) {
747 device->setActiveMode(mActiveModeId);
748 }
Lloyd Pique2eef1d22018-09-18 21:30:04 -0700749 mFlinger.mutableDisplays().emplace(mDisplayToken, device);
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800750 mFlinger.mutableCurrentState().displays.add(mDisplayToken, state);
751 mFlinger.mutableDrawingState().displays.add(mDisplayToken, state);
752
Dominik Laskowski55c85402020-01-21 16:25:47 -0800753 if (const auto& physical = state.physical) {
754 mFlinger.mutablePhysicalDisplayTokens()[physical->id] = mDisplayToken;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800755 }
756
757 return device;
758 }
759
760 private:
761 TestableSurfaceFlinger& mFlinger;
762 sp<BBinder> mDisplayToken = new BBinder();
Lloyd Pique2eef1d22018-09-18 21:30:04 -0700763 DisplayDeviceCreationArgs mCreationArgs;
Peiyong Line9d809e2020-04-14 13:10:48 -0700764 const std::optional<hal::HWDisplayId> mHwcDisplayId;
Marin Shalamanov045b7002021-01-07 16:56:24 +0100765 DisplayModeId mActiveModeId;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800766 };
767
Dominik Laskowski8b01cc02020-07-14 19:02:41 -0700768private:
769 void setVsyncEnabled(bool) override {}
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100770 void changeRefreshRate(const Scheduler::RefreshRate&, Scheduler::ModeEvent) override {}
Dominik Laskowski8b01cc02020-07-14 19:02:41 -0700771 void repaintEverythingForHWC() override {}
772 void kernelTimerChanged(bool) override {}
Ady Abraham62a0be22020-12-08 16:54:10 -0800773 void triggerOnFrameRateOverridesChanged() {}
Dominik Laskowski8b01cc02020-07-14 19:02:41 -0700774
Lloyd Pique90c115d2018-09-18 21:39:42 -0700775 surfaceflinger::test::Factory mFactory;
776 sp<SurfaceFlinger> mFlinger = new SurfaceFlinger(mFactory, SurfaceFlinger::SkipInitialization);
Dominik Laskowski7c9dbf92019-08-01 17:57:31 -0700777 TestableScheduler* mScheduler = nullptr;
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200778 mock::DisplayIdGenerator<GpuVirtualDisplayId> mGpuVirtualDisplayIdGenerator;
Lloyd Piquef58625d2017-12-19 13:22:33 -0800779};
780
781} // namespace android