blob: 4583d803497f034134b63bd02a5b72add44a4af6 [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 Piqueaad4ebf2019-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>
Lloyd Pique70d91362018-10-18 16:02:55 -070026
Lloyd Pique90c115d2018-09-18 21:39:42 -070027#include "BufferQueueLayer.h"
28#include "BufferStateLayer.h"
Lloyd Pique90c115d2018-09-18 21:39:42 -070029#include "ContainerLayer.h"
Lloyd Piquef58625d2017-12-19 13:22:33 -080030#include "DisplayDevice.h"
Vishnu Nairfa247b12020-02-11 08:58:26 -080031#include "EffectLayer.h"
Ana Krulec757f63a2019-01-25 10:46:18 -080032#include "FakePhaseOffsets.h"
Lloyd Piqued6b579f2018-04-06 15:29:10 -070033#include "Layer.h"
Lloyd Pique90c115d2018-09-18 21:39:42 -070034#include "NativeWindowSurface.h"
Dominik Laskowski9dab3432019-03-27 13:21:10 -070035#include "Scheduler/MessageQueue.h"
Steven Thomas2bbaabe2019-08-28 16:08:35 -070036#include "Scheduler/RefreshRateConfigs.h"
Lloyd Pique90c115d2018-09-18 21:39:42 -070037#include "StartPropertySetThread.h"
Lloyd Piquef58625d2017-12-19 13:22:33 -080038#include "SurfaceFlinger.h"
Lloyd Pique24f3bfe2019-10-02 19:29:10 -070039#include "SurfaceFlingerDefaultFactory.h"
Lloyd Pique90c115d2018-09-18 21:39:42 -070040#include "SurfaceInterceptor.h"
Dominik Laskowski7c9dbf92019-08-01 17:57:31 -070041#include "TestableScheduler.h"
Ady Abrahamabc27602020-04-08 17:20:29 -070042#include "mock/DisplayHardware/MockDisplay.h"
Yiwei Zhang7e666a52018-11-15 13:33:42 -080043
Lloyd Piquef58625d2017-12-19 13:22:33 -080044namespace android {
45
Lloyd Piquee39cad22017-12-20 17:01:29 -080046class EventThread;
47
Peiyong Lin833074a2018-08-28 11:53:54 -070048namespace renderengine {
Lloyd Pique90c115d2018-09-18 21:39:42 -070049
Lloyd Piquee39cad22017-12-20 17:01:29 -080050class RenderEngine;
Lloyd Pique90c115d2018-09-18 21:39:42 -070051
52} // namespace renderengine
Lloyd Piquee39cad22017-12-20 17:01:29 -080053
54namespace Hwc2 {
Lloyd Pique90c115d2018-09-18 21:39:42 -070055
Lloyd Piquee39cad22017-12-20 17:01:29 -080056class Composer;
Lloyd Pique90c115d2018-09-18 21:39:42 -070057
58} // namespace Hwc2
59
Peiyong Line9d809e2020-04-14 13:10:48 -070060namespace hal = android::hardware::graphics::composer::hal;
61
Lloyd Pique90c115d2018-09-18 21:39:42 -070062namespace surfaceflinger::test {
63
64class Factory final : public surfaceflinger::Factory {
65public:
66 ~Factory() = default;
67
Dominik Laskowski98041832019-08-01 18:35:59 -070068 std::unique_ptr<DispSync> createDispSync(const char*, bool) override {
Lloyd Pique90c115d2018-09-18 21:39:42 -070069 return nullptr;
70 }
71
72 std::unique_ptr<EventControlThread> createEventControlThread(
73 std::function<void(bool)>) override {
Lloyd Pique90c115d2018-09-18 21:39:42 -070074 return nullptr;
75 }
76
77 std::unique_ptr<HWComposer> createHWComposer(const std::string&) override {
Lloyd Pique90c115d2018-09-18 21:39:42 -070078 return nullptr;
79 }
80
81 std::unique_ptr<MessageQueue> createMessageQueue() override {
Lloyd Pique90c115d2018-09-18 21:39:42 -070082 return std::make_unique<android::impl::MessageQueue>();
83 }
84
Ady Abraham9e16a482019-12-03 17:19:41 -080085 std::unique_ptr<scheduler::PhaseConfiguration> createPhaseConfiguration(
86 const scheduler::RefreshRateConfigs& /*refreshRateConfigs*/) override {
Ana Krulec757f63a2019-01-25 10:46:18 -080087 return std::make_unique<scheduler::FakePhaseOffsets>();
88 }
89
Ady Abraham09bd3922019-04-08 10:44:56 -070090 std::unique_ptr<Scheduler> createScheduler(std::function<void(bool)>,
Ady Abraham3a77a7b2019-12-02 18:46:59 -080091 const scheduler::RefreshRateConfigs&,
92 ISchedulerCallback&) override {
Lloyd Pique90c115d2018-09-18 21:39:42 -070093 return nullptr;
94 }
95
96 std::unique_ptr<SurfaceInterceptor> createSurfaceInterceptor(SurfaceFlinger* flinger) override {
Lloyd Pique90c115d2018-09-18 21:39:42 -070097 return std::make_unique<android::impl::SurfaceInterceptor>(flinger);
98 }
99
100 sp<StartPropertySetThread> createStartPropertySetThread(bool timestampPropertyValue) override {
Lloyd Pique90c115d2018-09-18 21:39:42 -0700101 return new StartPropertySetThread(timestampPropertyValue);
102 }
103
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -0700104 sp<DisplayDevice> createDisplayDevice(DisplayDeviceCreationArgs& creationArgs) override {
105 return new DisplayDevice(creationArgs);
Lloyd Pique90c115d2018-09-18 21:39:42 -0700106 }
107
108 sp<GraphicBuffer> createGraphicBuffer(uint32_t width, uint32_t height, PixelFormat format,
109 uint32_t layerCount, uint64_t usage,
110 std::string requestorName) override {
Lloyd Pique90c115d2018-09-18 21:39:42 -0700111 return new GraphicBuffer(width, height, format, layerCount, usage, requestorName);
112 }
113
114 void createBufferQueue(sp<IGraphicBufferProducer>* outProducer,
115 sp<IGraphicBufferConsumer>* outConsumer,
116 bool consumerIsSurfaceFlinger) override {
Lloyd Piquee300b312019-10-03 13:03:45 -0700117 if (!mCreateBufferQueue) {
118 BufferQueue::createBufferQueue(outProducer, outConsumer, consumerIsSurfaceFlinger);
119 return;
120 }
Lloyd Pique90c115d2018-09-18 21:39:42 -0700121 mCreateBufferQueue(outProducer, outConsumer, consumerIsSurfaceFlinger);
122 }
123
Lloyd Piquee300b312019-10-03 13:03:45 -0700124 sp<IGraphicBufferProducer> createMonitoredProducer(const sp<IGraphicBufferProducer>& producer,
125 const sp<SurfaceFlinger>& flinger,
126 const wp<Layer>& layer) override {
127 return new MonitoredProducer(producer, flinger, layer);
128 }
129
130 sp<BufferLayerConsumer> createBufferLayerConsumer(const sp<IGraphicBufferConsumer>& consumer,
131 renderengine::RenderEngine& renderEngine,
132 uint32_t textureName, Layer* layer) override {
133 return new BufferLayerConsumer(consumer, renderEngine, textureName, layer);
134 }
135
Lloyd Pique90c115d2018-09-18 21:39:42 -0700136 std::unique_ptr<surfaceflinger::NativeWindowSurface> createNativeWindowSurface(
137 const sp<IGraphicBufferProducer>& producer) override {
138 if (!mCreateNativeWindowSurface) return nullptr;
139 return mCreateNativeWindowSurface(producer);
140 }
141
Lloyd Pique70d91362018-10-18 16:02:55 -0700142 std::unique_ptr<compositionengine::CompositionEngine> createCompositionEngine() override {
143 return compositionengine::impl::createCompositionEngine();
144 }
145
Lloyd Pique90c115d2018-09-18 21:39:42 -0700146 sp<BufferQueueLayer> createBufferQueueLayer(const LayerCreationArgs&) override {
Lloyd Pique90c115d2018-09-18 21:39:42 -0700147 return nullptr;
148 }
149
150 sp<BufferStateLayer> createBufferStateLayer(const LayerCreationArgs&) override {
Lloyd Pique90c115d2018-09-18 21:39:42 -0700151 return nullptr;
152 }
153
Vishnu Nairfa247b12020-02-11 08:58:26 -0800154 sp<EffectLayer> createEffectLayer(const LayerCreationArgs&) override { return nullptr; }
Lloyd Pique90c115d2018-09-18 21:39:42 -0700155
156 sp<ContainerLayer> createContainerLayer(const LayerCreationArgs&) override {
Lloyd Pique90c115d2018-09-18 21:39:42 -0700157 return nullptr;
158 }
159
160 using CreateBufferQueueFunction =
161 std::function<void(sp<IGraphicBufferProducer>* /* outProducer */,
162 sp<IGraphicBufferConsumer>* /* outConsumer */,
163 bool /* consumerIsSurfaceFlinger */)>;
164 CreateBufferQueueFunction mCreateBufferQueue;
165
166 using CreateNativeWindowSurfaceFunction =
167 std::function<std::unique_ptr<surfaceflinger::NativeWindowSurface>(
168 const sp<IGraphicBufferProducer>&)>;
169 CreateNativeWindowSurfaceFunction mCreateNativeWindowSurface;
Lloyd Pique70d91362018-10-18 16:02:55 -0700170
171 using CreateCompositionEngineFunction =
172 std::function<std::unique_ptr<compositionengine::CompositionEngine>()>;
173 CreateCompositionEngineFunction mCreateCompositionEngine;
Lloyd Pique90c115d2018-09-18 21:39:42 -0700174};
175
176} // namespace surfaceflinger::test
Lloyd Piquee39cad22017-12-20 17:01:29 -0800177
Lloyd Piquef58625d2017-12-19 13:22:33 -0800178class TestableSurfaceFlinger {
179public:
Dominik Laskowskif7a09ed2019-10-07 13:54:18 -0700180 SurfaceFlinger* flinger() { return mFlinger.get(); }
Dominik Laskowski7c9dbf92019-08-01 17:57:31 -0700181 TestableScheduler* scheduler() { return mScheduler; }
182
Lloyd Piquef58625d2017-12-19 13:22:33 -0800183 // Extend this as needed for accessing SurfaceFlinger private (and public)
184 // functions.
185
Peiyong Lin833074a2018-08-28 11:53:54 -0700186 void setupRenderEngine(std::unique_ptr<renderengine::RenderEngine> renderEngine) {
Lloyd Piqueb97e04f2018-10-18 17:07:05 -0700187 mFlinger->mCompositionEngine->setRenderEngine(std::move(renderEngine));
Lloyd Piquee39cad22017-12-20 17:01:29 -0800188 }
189
190 void setupComposer(std::unique_ptr<Hwc2::Composer> composer) {
Lloyd Pique441d5042018-10-18 16:49:51 -0700191 mFlinger->mCompositionEngine->setHwComposer(
192 std::make_unique<impl::HWComposer>(std::move(composer)));
Lloyd Piquee39cad22017-12-20 17:01:29 -0800193 }
194
Alec Mourie4034bb2019-11-19 12:45:54 -0800195 void setupTimeStats(const std::shared_ptr<TimeStats>& timeStats) {
196 mFlinger->mCompositionEngine->setTimeStats(timeStats);
197 }
198
Dominik Laskowski7c9dbf92019-08-01 17:57:31 -0700199 void setupScheduler(std::unique_ptr<DispSync> primaryDispSync,
200 std::unique_ptr<EventControlThread> eventControlThread,
201 std::unique_ptr<EventThread> appEventThread,
Ady Abraham8a82ba62020-01-17 12:43:17 -0800202 std::unique_ptr<EventThread> sfEventThread,
203 bool useContentDetectionV2 = false) {
Ady Abrahamabc27602020-04-08 17:20:29 -0700204 std::vector<std::shared_ptr<const HWC2::Display::Config>> configs{
205 HWC2::Display::Config::Builder(mDisplay, 0)
206 .setVsyncPeriod(int32_t(16666667))
207 .setConfigGroup(0)
208 .build()};
209
Ady Abraham2139f732019-11-13 18:56:40 -0800210 mFlinger->mRefreshRateConfigs = std::make_unique<
Ana Krulec3f6a2062020-01-23 15:48:01 -0800211 scheduler::RefreshRateConfigs>(configs, /*currentConfig=*/HwcConfigIndexType(0));
Ady Abraham2139f732019-11-13 18:56:40 -0800212 mFlinger->mRefreshRateStats = std::make_unique<
213 scheduler::RefreshRateStats>(*mFlinger->mRefreshRateConfigs, *mFlinger->mTimeStats,
214 /*currentConfig=*/HwcConfigIndexType(0),
215 /*powerMode=*/HWC_POWER_MODE_OFF);
Ady Abraham9e16a482019-12-03 17:19:41 -0800216 mFlinger->mPhaseConfiguration =
217 mFactory.createPhaseConfiguration(*mFlinger->mRefreshRateConfigs);
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700218
Dominik Laskowski7c9dbf92019-08-01 17:57:31 -0700219 mScheduler =
220 new TestableScheduler(std::move(primaryDispSync), std::move(eventControlThread),
Ady Abraham8a82ba62020-01-17 12:43:17 -0800221 *mFlinger->mRefreshRateConfigs, useContentDetectionV2);
Dominik Laskowski7c9dbf92019-08-01 17:57:31 -0700222
Dominik Laskowski98041832019-08-01 18:35:59 -0700223 mFlinger->mAppConnectionHandle = mScheduler->createConnection(std::move(appEventThread));
224 mFlinger->mSfConnectionHandle = mScheduler->createConnection(std::move(sfEventThread));
Dominik Laskowskif7a09ed2019-10-07 13:54:18 -0700225 resetScheduler(mScheduler);
Dominik Laskowski7c9dbf92019-08-01 17:57:31 -0700226
Dominik Laskowski7c9dbf92019-08-01 17:57:31 -0700227 mFlinger->mVSyncModulator.emplace(*mScheduler, mFlinger->mAppConnectionHandle,
228 mFlinger->mSfConnectionHandle,
Ady Abraham9e16a482019-12-03 17:19:41 -0800229 mFlinger->mPhaseConfiguration->getCurrentOffsets());
Dominik Laskowski7c9dbf92019-08-01 17:57:31 -0700230 }
231
Dominik Laskowskif7a09ed2019-10-07 13:54:18 -0700232 void resetScheduler(Scheduler* scheduler) { mFlinger->mScheduler.reset(scheduler); }
233
Lloyd Pique90c115d2018-09-18 21:39:42 -0700234 using CreateBufferQueueFunction = surfaceflinger::test::Factory::CreateBufferQueueFunction;
Lloyd Pique5b36f3f2018-01-17 11:57:07 -0800235 void setCreateBufferQueueFunction(CreateBufferQueueFunction f) {
Lloyd Pique90c115d2018-09-18 21:39:42 -0700236 mFactory.mCreateBufferQueue = f;
Lloyd Pique5b36f3f2018-01-17 11:57:07 -0800237 }
238
Lloyd Pique90c115d2018-09-18 21:39:42 -0700239 using CreateNativeWindowSurfaceFunction =
240 surfaceflinger::test::Factory::CreateNativeWindowSurfaceFunction;
Lloyd Pique0b1fe702018-01-22 18:03:16 -0800241 void setCreateNativeWindowSurface(CreateNativeWindowSurfaceFunction f) {
Lloyd Pique90c115d2018-09-18 21:39:42 -0700242 mFactory.mCreateNativeWindowSurface = f;
Lloyd Pique0b1fe702018-01-22 18:03:16 -0800243 }
244
Daniel Solomon42d04562019-01-20 21:03:19 -0800245 void setInternalDisplayPrimaries(const ui::DisplayPrimaries& primaries) {
246 memcpy(&mFlinger->mInternalDisplayPrimaries, &primaries, sizeof(ui::DisplayPrimaries));
247 }
248
Lloyd Pique0b1fe702018-01-22 18:03:16 -0800249 using HotplugEvent = SurfaceFlinger::HotplugEvent;
250
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800251 auto& mutableLayerCurrentState(sp<Layer> layer) { return layer->mCurrentState; }
252 auto& mutableLayerDrawingState(sp<Layer> layer) { return layer->mDrawingState; }
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700253
Valerie Haud251afb2019-03-29 14:19:02 -0700254 auto& mutableStateLock() { return mFlinger->mStateLock; }
255
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700256 void setLayerSidebandStream(sp<Layer> layer, sp<NativeHandle> sidebandStream) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800257 layer->mDrawingState.sidebandStream = sidebandStream;
Lloyd Pique0b785d82018-12-04 17:25:27 -0800258 layer->mSidebandStream = sidebandStream;
Lloyd Piquede196652020-01-22 17:29:58 -0800259 layer->editCompositionState()->sidebandStream = sidebandStream;
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700260 }
261
Peiyong Line9d809e2020-04-14 13:10:48 -0700262 void setLayerCompositionType(sp<Layer> layer, hal::Composition type) {
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800263 auto outputLayer = layer->findOutputLayerForDisplay(mFlinger->getDefaultDisplayDevice());
264 LOG_ALWAYS_FATAL_IF(!outputLayer);
265 auto& state = outputLayer->editState();
266 LOG_ALWAYS_FATAL_IF(!outputLayer->getState().hwc);
Peiyong Line9d809e2020-04-14 13:10:48 -0700267 (*state.hwc).hwcCompositionType = type;
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800268 };
269
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700270 void setLayerPotentialCursor(sp<Layer> layer, bool potentialCursor) {
271 layer->mPotentialCursor = potentialCursor;
272 }
273
Lloyd Piquef58625d2017-12-19 13:22:33 -0800274 /* ------------------------------------------------------------------------
275 * Forwarding for functions being tested
276 */
Lloyd Pique0b1fe702018-01-22 18:03:16 -0800277
Lloyd Piquea482f992018-01-22 19:00:34 -0800278 auto createDisplay(const String8& displayName, bool secure) {
279 return mFlinger->createDisplay(displayName, secure);
280 }
281
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700282 auto destroyDisplay(const sp<IBinder>& displayToken) {
283 return mFlinger->destroyDisplay(displayToken);
284 }
Lloyd Piquea482f992018-01-22 19:00:34 -0800285
Lloyd Piqued6fbb8a2018-01-22 19:08:36 -0800286 auto resetDisplayState() { return mFlinger->resetDisplayState(); }
287
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -0700288 auto setupNewDisplayDeviceInternal(
289 const wp<IBinder>& displayToken,
290 std::shared_ptr<compositionengine::Display> compositionDisplay,
291 const DisplayDeviceState& state,
292 const sp<compositionengine::DisplaySurface>& dispSurface,
293 const sp<IGraphicBufferProducer>& producer) {
294 return mFlinger->setupNewDisplayDeviceInternal(displayToken, compositionDisplay, state,
295 dispSurface, producer);
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800296 }
297
Lloyd Pique0b1fe702018-01-22 18:03:16 -0800298 auto handleTransactionLocked(uint32_t transactionFlags) {
Vishnu Nairfa5f8df2019-02-13 17:02:51 -0800299 Mutex::Autolock _l(mFlinger->mStateLock);
Lloyd Pique0b1fe702018-01-22 18:03:16 -0800300 return mFlinger->handleTransactionLocked(transactionFlags);
301 }
Lloyd Piquef58625d2017-12-19 13:22:33 -0800302
Peiyong Line9d809e2020-04-14 13:10:48 -0700303 auto onHotplugReceived(int32_t sequenceId, hal::HWDisplayId display,
304 hal::Connection connection) {
Lloyd Pique6cf11032018-01-22 18:57:44 -0800305 return mFlinger->onHotplugReceived(sequenceId, display, connection);
306 }
307
Vishnu Nairce5d0cc2019-02-28 14:38:41 -0800308 auto setDisplayStateLocked(const DisplayState& s) {
309 Mutex::Autolock _l(mFlinger->mStateLock);
310 return mFlinger->setDisplayStateLocked(s);
311 }
Lloyd Pique9d9cf402018-02-16 17:47:13 -0800312
Dominik Laskowski83b88212018-12-11 13:34:06 -0800313 // Allow reading display state without locking, as if called on the SF main thread.
314 auto onInitializeDisplays() NO_THREAD_SAFETY_ANALYSIS {
315 return mFlinger->onInitializeDisplays();
316 }
Lloyd Pique86016da2018-03-01 16:09:38 -0800317
Dominik Laskowski83b88212018-12-11 13:34:06 -0800318 // Allow reading display state without locking, as if called on the SF main thread.
319 auto setPowerModeInternal(const sp<DisplayDevice>& display,
320 int mode) NO_THREAD_SAFETY_ANALYSIS {
Dominik Laskowskie9774092018-12-11 10:04:24 -0800321 return mFlinger->setPowerModeInternal(display, mode);
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -0800322 }
323
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700324 auto onMessageReceived(int32_t what) { return mFlinger->onMessageReceived(what); }
325
Robert Carr108b2c72019-04-02 16:32:58 -0700326 auto captureScreenImplLocked(
327 const RenderArea& renderArea, SurfaceFlinger::TraverseLayersFunction traverseLayers,
328 ANativeWindowBuffer* buffer, bool useIdentityTransform, bool forSystem, int* outSyncFd) {
329 bool ignored;
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700330 return mFlinger->captureScreenImplLocked(renderArea, traverseLayers, buffer,
Robert Carr108b2c72019-04-02 16:32:58 -0700331 useIdentityTransform, forSystem, outSyncFd,
332 ignored);
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700333 }
334
chaviw0e3479f2018-09-10 16:49:30 -0700335 auto traverseLayersInDisplay(const sp<const DisplayDevice>& display,
336 const LayerVector::Visitor& visitor) {
337 return mFlinger->SurfaceFlinger::traverseLayersInDisplay(display, visitor);
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700338 }
339
Daniel Solomon42d04562019-01-20 21:03:19 -0800340 auto getDisplayNativePrimaries(const sp<IBinder>& displayToken,
341 ui::DisplayPrimaries &primaries) {
342 return mFlinger->SurfaceFlinger::getDisplayNativePrimaries(displayToken, primaries);
343 }
344
Valerie Haud251afb2019-03-29 14:19:02 -0700345 auto& getTransactionQueue() { return mFlinger->mTransactionQueues; }
346
347 auto setTransactionState(const Vector<ComposerState>& states,
348 const Vector<DisplayState>& displays, uint32_t flags,
349 const sp<IBinder>& applyToken,
350 const InputWindowCommands& inputWindowCommands,
351 int64_t desiredPresentTime, const client_cache_t& uncacheBuffer,
352 bool hasListenerCallbacks,
353 std::vector<ListenerCallbacks>& listenerCallbacks) {
354 return mFlinger->setTransactionState(states, displays, flags, applyToken,
355 inputWindowCommands, desiredPresentTime, uncacheBuffer,
356 hasListenerCallbacks, listenerCallbacks);
357 }
358
359 auto flushTransactionQueues() { return mFlinger->flushTransactionQueues(); };
360
Lloyd Pique86016da2018-03-01 16:09:38 -0800361 /* ------------------------------------------------------------------------
362 * Read-only access to private data to assert post-conditions.
363 */
364
365 const auto& getAnimFrameTracker() const { return mFlinger->mAnimFrameTracker; }
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -0800366 const auto& getHasPoweredOff() const { return mFlinger->mHasPoweredOff; }
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -0800367 const auto& getVisibleRegionsDirty() const { return mFlinger->mVisibleRegionsDirty; }
Lloyd Pique441d5042018-10-18 16:49:51 -0700368 auto& getHwComposer() const {
369 return static_cast<impl::HWComposer&>(mFlinger->getHwComposer());
370 }
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -0700371 auto& getCompositionEngine() const { return mFlinger->getCompositionEngine(); }
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -0800372
Lloyd Pique86016da2018-03-01 16:09:38 -0800373 const auto& getCompositorTiming() const { return mFlinger->getBE().mCompositorTiming; }
374
Lloyd Piquef58625d2017-12-19 13:22:33 -0800375 /* ------------------------------------------------------------------------
376 * Read-write access to private data to set up preconditions and assert
377 * post-conditions.
378 */
Lloyd Pique0b1fe702018-01-22 18:03:16 -0800379
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800380 auto& mutableHasWideColorDisplay() { return SurfaceFlinger::hasWideColorDisplay; }
Peiyong Lin13effd12018-07-24 17:01:47 -0700381 auto& mutableUseColorManagement() { return SurfaceFlinger::useColorManagement; }
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800382
Lloyd Piquee39cad22017-12-20 17:01:29 -0800383 auto& mutableCurrentState() { return mFlinger->mCurrentState; }
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800384 auto& mutableDisplayColorSetting() { return mFlinger->mDisplayColorSetting; }
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700385 auto& mutableDisplays() { return mFlinger->mDisplays; }
Lloyd Piquee39cad22017-12-20 17:01:29 -0800386 auto& mutableDrawingState() { return mFlinger->mDrawingState; }
Lloyd Piquee39cad22017-12-20 17:01:29 -0800387 auto& mutableEventQueue() { return mFlinger->mEventQueue; }
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700388 auto& mutableGeometryInvalid() { return mFlinger->mGeometryInvalid; }
Lloyd Pique0b1fe702018-01-22 18:03:16 -0800389 auto& mutableInterceptor() { return mFlinger->mInterceptor; }
Lloyd Pique6cf11032018-01-22 18:57:44 -0800390 auto& mutableMainThreadId() { return mFlinger->mMainThreadId; }
Lloyd Pique0b1fe702018-01-22 18:03:16 -0800391 auto& mutablePendingHotplugEvents() { return mFlinger->mPendingHotplugEvents; }
Dominik Laskowski075d3172018-05-24 15:50:06 -0700392 auto& mutablePhysicalDisplayTokens() { return mFlinger->mPhysicalDisplayTokens; }
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700393 auto& mutableTexturePool() { return mFlinger->mTexturePool; }
Lloyd Pique0b1fe702018-01-22 18:03:16 -0800394 auto& mutableTransactionFlags() { return mFlinger->mTransactionFlags; }
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800395 auto& mutableUseHwcVirtualDisplays() { return mFlinger->mUseHwcVirtualDisplays; }
Peiyong Lin74ca2f42019-01-14 19:36:57 -0800396 auto& mutablePowerAdvisor() { return mFlinger->mPowerAdvisor; }
Lloyd Pique4fe29402019-08-12 16:51:24 -0700397 auto& mutableDebugDisableHWC() { return mFlinger->mDebugDisableHWC; }
Lloyd Piquee39cad22017-12-20 17:01:29 -0800398
Lloyd Pique6cf11032018-01-22 18:57:44 -0800399 auto& mutableComposerSequenceId() { return mFlinger->getBE().mComposerSequenceId; }
Lloyd Pique441d5042018-10-18 16:49:51 -0700400 auto& mutableHwcDisplayData() { return getHwComposer().mDisplayData; }
401 auto& mutableHwcPhysicalDisplayIdMap() { return getHwComposer().mPhysicalDisplayIdMap; }
402 auto& mutableInternalHwcDisplayId() { return getHwComposer().mInternalHwcDisplayId; }
403 auto& mutableExternalHwcDisplayId() { return getHwComposer().mExternalHwcDisplayId; }
Ady Abraham60e42ea2020-03-09 19:17:31 -0700404 auto& mutableUseFrameRateApi() { return mFlinger->useFrameRateApi; }
Lloyd Piquea618d852018-01-17 11:52:30 -0800405
Valerie Hau09e60052019-12-15 14:51:15 -0800406 auto fromHandle(const sp<IBinder>& handle) {
407 Mutex::Autolock _l(mFlinger->mStateLock);
408 return mFlinger->fromHandle(handle);
409 }
410
Lloyd Piquee39cad22017-12-20 17:01:29 -0800411 ~TestableSurfaceFlinger() {
412 // All these pointer and container clears help ensure that GMock does
413 // not report a leaked object, since the SurfaceFlinger instance may
414 // still be referenced by something despite our best efforts to destroy
415 // it after each test is done.
416 mutableDisplays().clear();
Ady Abraham2939f092019-03-04 17:44:06 -0800417 mutableCurrentState().displays.clear();
418 mutableDrawingState().displays.clear();
Lloyd Pique0b1fe702018-01-22 18:03:16 -0800419 mutableEventQueue().reset();
Lloyd Pique0b1fe702018-01-22 18:03:16 -0800420 mutableInterceptor().reset();
Lloyd Pique2eb71eb2019-08-13 13:23:03 -0700421 mFlinger->mScheduler.reset();
Lloyd Pique441d5042018-10-18 16:49:51 -0700422 mFlinger->mCompositionEngine->setHwComposer(std::unique_ptr<HWComposer>());
Lloyd Piqueb97e04f2018-10-18 17:07:05 -0700423 mFlinger->mCompositionEngine->setRenderEngine(
424 std::unique_ptr<renderengine::RenderEngine>());
Lloyd Piquee39cad22017-12-20 17:01:29 -0800425 }
Lloyd Piquef58625d2017-12-19 13:22:33 -0800426
Lloyd Piquea618d852018-01-17 11:52:30 -0800427 /* ------------------------------------------------------------------------
428 * Wrapper classes for Read-write access to private data to set up
429 * preconditions and assert post-conditions.
430 */
Ana Krulec4593b692019-01-11 22:07:25 -0800431 struct HWC2Display : public HWC2::impl::Display {
Peiyong Lin74ca2f42019-01-14 19:36:57 -0800432 HWC2Display(Hwc2::Composer& composer,
Peiyong Line9d809e2020-04-14 13:10:48 -0700433 const std::unordered_set<hal::Capability>& capabilities, hal::HWDisplayId id,
434 hal::DisplayType type)
Peiyong Lin74ca2f42019-01-14 19:36:57 -0800435 : HWC2::impl::Display(composer, capabilities, id, type) {}
Lloyd Piquea618d852018-01-17 11:52:30 -0800436 ~HWC2Display() {
437 // Prevents a call to disable vsyncs.
Peiyong Line9d809e2020-04-14 13:10:48 -0700438 mType = hal::DisplayType::INVALID;
Lloyd Piquea618d852018-01-17 11:52:30 -0800439 }
440
441 auto& mutableIsConnected() { return this->mIsConnected; }
442 auto& mutableConfigs() { return this->mConfigs; }
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700443 auto& mutableLayers() { return this->mLayers; }
Lloyd Piquea618d852018-01-17 11:52:30 -0800444 };
445
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800446 class FakeHwcDisplayInjector {
447 public:
Peiyong Line9d809e2020-04-14 13:10:48 -0700448 static constexpr hal::HWDisplayId DEFAULT_HWC_DISPLAY_ID = 1000;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800449 static constexpr int32_t DEFAULT_WIDTH = 1920;
450 static constexpr int32_t DEFAULT_HEIGHT = 1280;
451 static constexpr int32_t DEFAULT_REFRESH_RATE = 16'666'666;
Ady Abraham2139f732019-11-13 18:56:40 -0800452 static constexpr int32_t DEFAULT_CONFIG_GROUP = 7;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800453 static constexpr int32_t DEFAULT_DPI = 320;
Peiyong Line9d809e2020-04-14 13:10:48 -0700454 static constexpr hal::HWConfigId DEFAULT_ACTIVE_CONFIG = 0;
Peiyong Lin1336e6e2019-05-28 09:23:50 -0700455 static constexpr int32_t DEFAULT_POWER_MODE = 2;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800456
Peiyong Line9d809e2020-04-14 13:10:48 -0700457 FakeHwcDisplayInjector(DisplayId displayId, hal::DisplayType hwcDisplayType, bool isPrimary)
Dominik Laskowski075d3172018-05-24 15:50:06 -0700458 : mDisplayId(displayId), mHwcDisplayType(hwcDisplayType), mIsPrimary(isPrimary) {}
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800459
Peiyong Line9d809e2020-04-14 13:10:48 -0700460 auto& setHwcDisplayId(hal::HWDisplayId displayId) {
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800461 mHwcDisplayId = displayId;
462 return *this;
463 }
464
465 auto& setWidth(int32_t width) {
466 mWidth = width;
467 return *this;
468 }
469
470 auto& setHeight(int32_t height) {
471 mHeight = height;
472 return *this;
473 }
474
Ady Abraham8a82ba62020-01-17 12:43:17 -0800475 auto& setRefreshRate(int32_t refreshRate) {
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800476 mRefreshRate = refreshRate;
477 return *this;
478 }
479
480 auto& setDpiX(int32_t dpi) {
481 mDpiX = dpi;
482 return *this;
483 }
484
485 auto& setDpiY(int32_t dpi) {
486 mDpiY = dpi;
487 return *this;
488 }
489
Peiyong Line9d809e2020-04-14 13:10:48 -0700490 auto& setActiveConfig(hal::HWConfigId config) {
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800491 mActiveConfig = config;
492 return *this;
493 }
494
Peiyong Line9d809e2020-04-14 13:10:48 -0700495 auto& setCapabilities(const std::unordered_set<hal::Capability>* capabilities) {
Lloyd Piquee22f0332018-07-16 16:35:56 -0700496 mCapabilities = capabilities;
497 return *this;
498 }
499
Peiyong Lin1336e6e2019-05-28 09:23:50 -0700500 auto& setPowerMode(int mode) {
501 mPowerMode = mode;
502 return *this;
503 }
504
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800505 void inject(TestableSurfaceFlinger* flinger, Hwc2::Composer* composer) {
Peiyong Line9d809e2020-04-14 13:10:48 -0700506 static const std::unordered_set<hal::Capability> defaultCapabilities;
Lloyd Piquee22f0332018-07-16 16:35:56 -0700507 if (mCapabilities == nullptr) mCapabilities = &defaultCapabilities;
508
509 // Caution - Make sure that any values passed by reference here do
510 // not refer to an instance owned by FakeHwcDisplayInjector. This
511 // class has temporary lifetime, while the constructed HWC2::Display
512 // is much longer lived.
Peiyong Lin74ca2f42019-01-14 19:36:57 -0800513 auto display = std::make_unique<HWC2Display>(*composer, *mCapabilities, mHwcDisplayId,
514 mHwcDisplayType);
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800515
516 auto config = HWC2::Display::Config::Builder(*display, mActiveConfig);
517 config.setWidth(mWidth);
518 config.setHeight(mHeight);
519 config.setVsyncPeriod(mRefreshRate);
520 config.setDpiX(mDpiX);
521 config.setDpiY(mDpiY);
Ady Abraham2139f732019-11-13 18:56:40 -0800522 config.setConfigGroup(mConfigGroup);
Ady Abraham8a82ba62020-01-17 12:43:17 -0800523 display->mutableConfigs().emplace(static_cast<int32_t>(mActiveConfig), config.build());
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800524 display->mutableIsConnected() = true;
Peiyong Line9d809e2020-04-14 13:10:48 -0700525 display->setPowerMode(static_cast<hal::PowerMode>(mPowerMode));
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800526
Peiyong Linbdd08cc2019-12-17 21:35:14 -0800527 flinger->mutableHwcDisplayData()[mDisplayId].hwcDisplay = std::move(display);
Dominik Laskowski075d3172018-05-24 15:50:06 -0700528
Peiyong Line9d809e2020-04-14 13:10:48 -0700529 if (mHwcDisplayType == hal::DisplayType::PHYSICAL) {
Dominik Laskowski075d3172018-05-24 15:50:06 -0700530 flinger->mutableHwcPhysicalDisplayIdMap().emplace(mHwcDisplayId, mDisplayId);
531 (mIsPrimary ? flinger->mutableInternalHwcDisplayId()
532 : flinger->mutableExternalHwcDisplayId()) = mHwcDisplayId;
533 }
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800534 }
535
536 private:
Dominik Laskowski075d3172018-05-24 15:50:06 -0700537 const DisplayId mDisplayId;
Peiyong Line9d809e2020-04-14 13:10:48 -0700538 const hal::DisplayType mHwcDisplayType;
Dominik Laskowski075d3172018-05-24 15:50:06 -0700539 const bool mIsPrimary;
540
Peiyong Line9d809e2020-04-14 13:10:48 -0700541 hal::HWDisplayId mHwcDisplayId = DEFAULT_HWC_DISPLAY_ID;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800542 int32_t mWidth = DEFAULT_WIDTH;
543 int32_t mHeight = DEFAULT_HEIGHT;
Ady Abraham8a82ba62020-01-17 12:43:17 -0800544 int32_t mRefreshRate = DEFAULT_REFRESH_RATE;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800545 int32_t mDpiX = DEFAULT_DPI;
Ady Abraham2139f732019-11-13 18:56:40 -0800546 int32_t mConfigGroup = DEFAULT_CONFIG_GROUP;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800547 int32_t mDpiY = DEFAULT_DPI;
Peiyong Line9d809e2020-04-14 13:10:48 -0700548 hal::HWConfigId mActiveConfig = DEFAULT_ACTIVE_CONFIG;
Peiyong Lin1336e6e2019-05-28 09:23:50 -0700549 int32_t mPowerMode = DEFAULT_POWER_MODE;
Peiyong Line9d809e2020-04-14 13:10:48 -0700550 const std::unordered_set<hal::Capability>* mCapabilities = nullptr;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800551 };
552
553 class FakeDisplayDeviceInjector {
554 public:
Dominik Laskowski075d3172018-05-24 15:50:06 -0700555 FakeDisplayDeviceInjector(TestableSurfaceFlinger& flinger,
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -0700556 std::shared_ptr<compositionengine::Display> compositionDisplay,
Dominik Laskowski55c85402020-01-21 16:25:47 -0800557 std::optional<DisplayConnectionType> connectionType,
Peiyong Line9d809e2020-04-14 13:10:48 -0700558 std::optional<hal::HWDisplayId> hwcDisplayId, bool isPrimary)
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -0700559 : mFlinger(flinger),
Marin Shalamanov4a42d432020-02-12 20:22:26 +0100560 mCreationArgs(flinger.mFlinger.get(), mDisplayToken, compositionDisplay),
561 mHwcDisplayId(hwcDisplayId) {
Dominik Laskowski55c85402020-01-21 16:25:47 -0800562 mCreationArgs.connectionType = connectionType;
Dominik Laskowski075d3172018-05-24 15:50:06 -0700563 mCreationArgs.isPrimary = isPrimary;
564 }
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800565
566 sp<IBinder> token() const { return mDisplayToken; }
567
568 DisplayDeviceState& mutableDrawingDisplayState() {
569 return mFlinger.mutableDrawingState().displays.editValueFor(mDisplayToken);
570 }
571
572 DisplayDeviceState& mutableCurrentDisplayState() {
573 return mFlinger.mutableCurrentState().displays.editValueFor(mDisplayToken);
574 }
575
Lloyd Pique9d9cf402018-02-16 17:47:13 -0800576 const auto& getDrawingDisplayState() {
577 return mFlinger.mutableDrawingState().displays.valueFor(mDisplayToken);
578 }
579
580 const auto& getCurrentDisplayState() {
581 return mFlinger.mutableCurrentState().displays.valueFor(mDisplayToken);
582 }
583
Dominik Laskowski9fae1022018-05-29 13:17:40 -0700584 auto& mutableDisplayDevice() { return mFlinger.mutableDisplays()[mDisplayToken]; }
Lloyd Pique9d9cf402018-02-16 17:47:13 -0800585
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800586 auto& setNativeWindow(const sp<ANativeWindow>& nativeWindow) {
Lloyd Pique2eef1d22018-09-18 21:30:04 -0700587 mCreationArgs.nativeWindow = nativeWindow;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800588 return *this;
589 }
590
Lloyd Pique542307f2018-10-19 13:24:08 -0700591 auto& setDisplaySurface(const sp<compositionengine::DisplaySurface>& displaySurface) {
Lloyd Pique2eef1d22018-09-18 21:30:04 -0700592 mCreationArgs.displaySurface = displaySurface;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800593 return *this;
594 }
595
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800596 auto& setSecure(bool secure) {
Lloyd Pique2eef1d22018-09-18 21:30:04 -0700597 mCreationArgs.isSecure = secure;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800598 return *this;
599 }
600
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700601 auto& setPowerMode(int mode) {
Lloyd Pique2eef1d22018-09-18 21:30:04 -0700602 mCreationArgs.initialPowerMode = mode;
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700603 return *this;
604 }
605
Valerie Hau9758ae02018-10-09 16:05:09 -0700606 auto& setHwcColorModes(
607 const std::unordered_map<ui::ColorMode, std::vector<ui::RenderIntent>>
608 hwcColorModes) {
609 mCreationArgs.hwcColorModes = hwcColorModes;
610 return *this;
611 }
612
613 auto& setHasWideColorGamut(bool hasWideColorGamut) {
614 mCreationArgs.hasWideColorGamut = hasWideColorGamut;
615 return *this;
616 }
617
Lloyd Pique33050472019-12-19 17:12:44 -0800618 auto& setPhysicalOrientation(ui::Rotation orientation) {
619 mCreationArgs.physicalOrientation = orientation;
620 return *this;
621 }
622
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800623 sp<DisplayDevice> inject() {
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -0700624 const auto displayId = mCreationArgs.compositionDisplay->getDisplayId();
625
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800626 DisplayDeviceState state;
Dominik Laskowski55c85402020-01-21 16:25:47 -0800627 if (const auto type = mCreationArgs.connectionType) {
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -0700628 LOG_ALWAYS_FATAL_IF(!displayId);
Marin Shalamanov4a42d432020-02-12 20:22:26 +0100629 LOG_ALWAYS_FATAL_IF(!mHwcDisplayId);
630 state.physical = {*displayId, *type, *mHwcDisplayId};
Dominik Laskowski55c85402020-01-21 16:25:47 -0800631 }
632
Lloyd Pique2eef1d22018-09-18 21:30:04 -0700633 state.isSecure = mCreationArgs.isSecure;
634
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -0700635 sp<DisplayDevice> device = new DisplayDevice(mCreationArgs);
Lloyd Pique2eef1d22018-09-18 21:30:04 -0700636 mFlinger.mutableDisplays().emplace(mDisplayToken, device);
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800637 mFlinger.mutableCurrentState().displays.add(mDisplayToken, state);
638 mFlinger.mutableDrawingState().displays.add(mDisplayToken, state);
639
Dominik Laskowski55c85402020-01-21 16:25:47 -0800640 if (const auto& physical = state.physical) {
641 mFlinger.mutablePhysicalDisplayTokens()[physical->id] = mDisplayToken;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800642 }
643
644 return device;
645 }
646
647 private:
648 TestableSurfaceFlinger& mFlinger;
649 sp<BBinder> mDisplayToken = new BBinder();
Lloyd Pique2eef1d22018-09-18 21:30:04 -0700650 DisplayDeviceCreationArgs mCreationArgs;
Peiyong Line9d809e2020-04-14 13:10:48 -0700651 const std::optional<hal::HWDisplayId> mHwcDisplayId;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800652 };
653
Lloyd Pique90c115d2018-09-18 21:39:42 -0700654 surfaceflinger::test::Factory mFactory;
655 sp<SurfaceFlinger> mFlinger = new SurfaceFlinger(mFactory, SurfaceFlinger::SkipInitialization);
Dominik Laskowski7c9dbf92019-08-01 17:57:31 -0700656 TestableScheduler* mScheduler = nullptr;
Ady Abrahamabc27602020-04-08 17:20:29 -0700657 Hwc2::mock::Display mDisplay;
Lloyd Piquef58625d2017-12-19 13:22:33 -0800658};
659
660} // namespace android