blob: 3a4f349d4505a431f7f2db4b77b4c15b74b36449 [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 Pique37c2c9b2018-12-04 17:25:10 -080023#include <compositionengine/impl/OutputLayerCompositionState.h>
Dominik Laskowskif7a09ed2019-10-07 13:54:18 -070024#include <compositionengine/mock/DisplaySurface.h>
Lloyd Pique70d91362018-10-18 16:02:55 -070025
Lloyd Pique90c115d2018-09-18 21:39:42 -070026#include "BufferQueueLayer.h"
27#include "BufferStateLayer.h"
Lloyd Pique90c115d2018-09-18 21:39:42 -070028#include "ContainerLayer.h"
Lloyd Piquef58625d2017-12-19 13:22:33 -080029#include "DisplayDevice.h"
Vishnu Nairfa247b12020-02-11 08:58:26 -080030#include "EffectLayer.h"
Ana Krulec757f63a2019-01-25 10:46:18 -080031#include "FakePhaseOffsets.h"
Lloyd Piqued6b579f2018-04-06 15:29:10 -070032#include "Layer.h"
Lloyd Pique90c115d2018-09-18 21:39:42 -070033#include "NativeWindowSurface.h"
Dominik Laskowski9dab3432019-03-27 13:21:10 -070034#include "Scheduler/MessageQueue.h"
Steven Thomas2bbaabe2019-08-28 16:08:35 -070035#include "Scheduler/RefreshRateConfigs.h"
Lloyd Pique90c115d2018-09-18 21:39:42 -070036#include "StartPropertySetThread.h"
Lloyd Piquef58625d2017-12-19 13:22:33 -080037#include "SurfaceFlinger.h"
Lloyd Pique24f3bfe2019-10-02 19:29:10 -070038#include "SurfaceFlingerDefaultFactory.h"
Lloyd Pique90c115d2018-09-18 21:39:42 -070039#include "SurfaceInterceptor.h"
Dominik Laskowski7c9dbf92019-08-01 17:57:31 -070040#include "TestableScheduler.h"
Yiwei Zhang7e666a52018-11-15 13:33:42 -080041
Lloyd Piquef58625d2017-12-19 13:22:33 -080042namespace android {
43
Lloyd Piquee39cad22017-12-20 17:01:29 -080044class EventThread;
45
Peiyong Lin833074a2018-08-28 11:53:54 -070046namespace renderengine {
Lloyd Pique90c115d2018-09-18 21:39:42 -070047
Lloyd Piquee39cad22017-12-20 17:01:29 -080048class RenderEngine;
Lloyd Pique90c115d2018-09-18 21:39:42 -070049
50} // namespace renderengine
Lloyd Piquee39cad22017-12-20 17:01:29 -080051
52namespace Hwc2 {
Lloyd Pique90c115d2018-09-18 21:39:42 -070053
Lloyd Piquee39cad22017-12-20 17:01:29 -080054class Composer;
Lloyd Pique90c115d2018-09-18 21:39:42 -070055
56} // namespace Hwc2
57
58namespace surfaceflinger::test {
59
60class Factory final : public surfaceflinger::Factory {
61public:
62 ~Factory() = default;
63
Dominik Laskowski98041832019-08-01 18:35:59 -070064 std::unique_ptr<DispSync> createDispSync(const char*, bool) override {
Lloyd Pique90c115d2018-09-18 21:39:42 -070065 return nullptr;
66 }
67
68 std::unique_ptr<EventControlThread> createEventControlThread(
69 std::function<void(bool)>) override {
Lloyd Pique90c115d2018-09-18 21:39:42 -070070 return nullptr;
71 }
72
73 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 Abraham9e16a482019-12-03 17:19:41 -080081 std::unique_ptr<scheduler::PhaseConfiguration> createPhaseConfiguration(
82 const scheduler::RefreshRateConfigs& /*refreshRateConfigs*/) override {
Ana Krulec757f63a2019-01-25 10:46:18 -080083 return std::make_unique<scheduler::FakePhaseOffsets>();
84 }
85
Ady Abraham09bd3922019-04-08 10:44:56 -070086 std::unique_ptr<Scheduler> createScheduler(std::function<void(bool)>,
Ady Abraham3a77a7b2019-12-02 18:46:59 -080087 const scheduler::RefreshRateConfigs&,
88 ISchedulerCallback&) override {
Lloyd Pique90c115d2018-09-18 21:39:42 -070089 return nullptr;
90 }
91
92 std::unique_ptr<SurfaceInterceptor> createSurfaceInterceptor(SurfaceFlinger* flinger) override {
Lloyd Pique90c115d2018-09-18 21:39:42 -070093 return std::make_unique<android::impl::SurfaceInterceptor>(flinger);
94 }
95
96 sp<StartPropertySetThread> createStartPropertySetThread(bool timestampPropertyValue) override {
Lloyd Pique90c115d2018-09-18 21:39:42 -070097 return new StartPropertySetThread(timestampPropertyValue);
98 }
99
100 sp<DisplayDevice> createDisplayDevice(DisplayDeviceCreationArgs&& creationArgs) override {
Lloyd Pique90c115d2018-09-18 21:39:42 -0700101 return new DisplayDevice(std::move(creationArgs));
102 }
103
104 sp<GraphicBuffer> createGraphicBuffer(uint32_t width, uint32_t height, PixelFormat format,
105 uint32_t layerCount, uint64_t usage,
106 std::string requestorName) override {
Lloyd Pique90c115d2018-09-18 21:39:42 -0700107 return new GraphicBuffer(width, height, format, layerCount, usage, requestorName);
108 }
109
110 void createBufferQueue(sp<IGraphicBufferProducer>* outProducer,
111 sp<IGraphicBufferConsumer>* outConsumer,
112 bool consumerIsSurfaceFlinger) override {
Lloyd Piquee300b312019-10-03 13:03:45 -0700113 if (!mCreateBufferQueue) {
114 BufferQueue::createBufferQueue(outProducer, outConsumer, consumerIsSurfaceFlinger);
115 return;
116 }
Lloyd Pique90c115d2018-09-18 21:39:42 -0700117 mCreateBufferQueue(outProducer, outConsumer, consumerIsSurfaceFlinger);
118 }
119
Lloyd Piquee300b312019-10-03 13:03:45 -0700120 sp<IGraphicBufferProducer> createMonitoredProducer(const sp<IGraphicBufferProducer>& producer,
121 const sp<SurfaceFlinger>& flinger,
122 const wp<Layer>& layer) override {
123 return new MonitoredProducer(producer, flinger, layer);
124 }
125
126 sp<BufferLayerConsumer> createBufferLayerConsumer(const sp<IGraphicBufferConsumer>& consumer,
127 renderengine::RenderEngine& renderEngine,
128 uint32_t textureName, Layer* layer) override {
129 return new BufferLayerConsumer(consumer, renderEngine, textureName, layer);
130 }
131
Lloyd Pique90c115d2018-09-18 21:39:42 -0700132 std::unique_ptr<surfaceflinger::NativeWindowSurface> createNativeWindowSurface(
133 const sp<IGraphicBufferProducer>& producer) override {
134 if (!mCreateNativeWindowSurface) return nullptr;
135 return mCreateNativeWindowSurface(producer);
136 }
137
Lloyd Pique70d91362018-10-18 16:02:55 -0700138 std::unique_ptr<compositionengine::CompositionEngine> createCompositionEngine() override {
139 return compositionengine::impl::createCompositionEngine();
140 }
141
Lloyd Pique90c115d2018-09-18 21:39:42 -0700142 sp<BufferQueueLayer> createBufferQueueLayer(const LayerCreationArgs&) override {
Lloyd Pique90c115d2018-09-18 21:39:42 -0700143 return nullptr;
144 }
145
146 sp<BufferStateLayer> createBufferStateLayer(const LayerCreationArgs&) override {
Lloyd Pique90c115d2018-09-18 21:39:42 -0700147 return nullptr;
148 }
149
Vishnu Nairfa247b12020-02-11 08:58:26 -0800150 sp<EffectLayer> createEffectLayer(const LayerCreationArgs&) override { return nullptr; }
Lloyd Pique90c115d2018-09-18 21:39:42 -0700151
152 sp<ContainerLayer> createContainerLayer(const LayerCreationArgs&) override {
Lloyd Pique90c115d2018-09-18 21:39:42 -0700153 return nullptr;
154 }
155
156 using CreateBufferQueueFunction =
157 std::function<void(sp<IGraphicBufferProducer>* /* outProducer */,
158 sp<IGraphicBufferConsumer>* /* outConsumer */,
159 bool /* consumerIsSurfaceFlinger */)>;
160 CreateBufferQueueFunction mCreateBufferQueue;
161
162 using CreateNativeWindowSurfaceFunction =
163 std::function<std::unique_ptr<surfaceflinger::NativeWindowSurface>(
164 const sp<IGraphicBufferProducer>&)>;
165 CreateNativeWindowSurfaceFunction mCreateNativeWindowSurface;
Lloyd Pique70d91362018-10-18 16:02:55 -0700166
167 using CreateCompositionEngineFunction =
168 std::function<std::unique_ptr<compositionengine::CompositionEngine>()>;
169 CreateCompositionEngineFunction mCreateCompositionEngine;
Lloyd Pique90c115d2018-09-18 21:39:42 -0700170};
171
172} // namespace surfaceflinger::test
Lloyd Piquee39cad22017-12-20 17:01:29 -0800173
Lloyd Piquef58625d2017-12-19 13:22:33 -0800174class TestableSurfaceFlinger {
175public:
Dominik Laskowskif7a09ed2019-10-07 13:54:18 -0700176 SurfaceFlinger* flinger() { return mFlinger.get(); }
Dominik Laskowski7c9dbf92019-08-01 17:57:31 -0700177 TestableScheduler* scheduler() { return mScheduler; }
178
Lloyd Piquef58625d2017-12-19 13:22:33 -0800179 // Extend this as needed for accessing SurfaceFlinger private (and public)
180 // functions.
181
Peiyong Lin833074a2018-08-28 11:53:54 -0700182 void setupRenderEngine(std::unique_ptr<renderengine::RenderEngine> renderEngine) {
Lloyd Piqueb97e04f2018-10-18 17:07:05 -0700183 mFlinger->mCompositionEngine->setRenderEngine(std::move(renderEngine));
Lloyd Piquee39cad22017-12-20 17:01:29 -0800184 }
185
186 void setupComposer(std::unique_ptr<Hwc2::Composer> composer) {
Lloyd Pique441d5042018-10-18 16:49:51 -0700187 mFlinger->mCompositionEngine->setHwComposer(
188 std::make_unique<impl::HWComposer>(std::move(composer)));
Lloyd Piquee39cad22017-12-20 17:01:29 -0800189 }
190
Alec Mourie4034bb2019-11-19 12:45:54 -0800191 void setupTimeStats(const std::shared_ptr<TimeStats>& timeStats) {
192 mFlinger->mCompositionEngine->setTimeStats(timeStats);
193 }
194
Dominik Laskowski7c9dbf92019-08-01 17:57:31 -0700195 void setupScheduler(std::unique_ptr<DispSync> primaryDispSync,
196 std::unique_ptr<EventControlThread> eventControlThread,
197 std::unique_ptr<EventThread> appEventThread,
Ady Abraham8a82ba62020-01-17 12:43:17 -0800198 std::unique_ptr<EventThread> sfEventThread,
199 bool useContentDetectionV2 = false) {
Ady Abraham2139f732019-11-13 18:56:40 -0800200 std::vector<scheduler::RefreshRateConfigs::InputConfig> configs{
201 {{HwcConfigIndexType(0), HwcConfigGroupType(0), 16666667}}};
202 mFlinger->mRefreshRateConfigs = std::make_unique<
Ana Krulec3f6a2062020-01-23 15:48:01 -0800203 scheduler::RefreshRateConfigs>(configs, /*currentConfig=*/HwcConfigIndexType(0));
Ady Abraham2139f732019-11-13 18:56:40 -0800204 mFlinger->mRefreshRateStats = std::make_unique<
205 scheduler::RefreshRateStats>(*mFlinger->mRefreshRateConfigs, *mFlinger->mTimeStats,
206 /*currentConfig=*/HwcConfigIndexType(0),
207 /*powerMode=*/HWC_POWER_MODE_OFF);
Ady Abraham9e16a482019-12-03 17:19:41 -0800208 mFlinger->mPhaseConfiguration =
209 mFactory.createPhaseConfiguration(*mFlinger->mRefreshRateConfigs);
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700210
Dominik Laskowski7c9dbf92019-08-01 17:57:31 -0700211 mScheduler =
212 new TestableScheduler(std::move(primaryDispSync), std::move(eventControlThread),
Ady Abraham8a82ba62020-01-17 12:43:17 -0800213 *mFlinger->mRefreshRateConfigs, useContentDetectionV2);
Dominik Laskowski7c9dbf92019-08-01 17:57:31 -0700214
Dominik Laskowski98041832019-08-01 18:35:59 -0700215 mFlinger->mAppConnectionHandle = mScheduler->createConnection(std::move(appEventThread));
216 mFlinger->mSfConnectionHandle = mScheduler->createConnection(std::move(sfEventThread));
Dominik Laskowskif7a09ed2019-10-07 13:54:18 -0700217 resetScheduler(mScheduler);
Dominik Laskowski7c9dbf92019-08-01 17:57:31 -0700218
Dominik Laskowski7c9dbf92019-08-01 17:57:31 -0700219 mFlinger->mVSyncModulator.emplace(*mScheduler, mFlinger->mAppConnectionHandle,
220 mFlinger->mSfConnectionHandle,
Ady Abraham9e16a482019-12-03 17:19:41 -0800221 mFlinger->mPhaseConfiguration->getCurrentOffsets());
Dominik Laskowski7c9dbf92019-08-01 17:57:31 -0700222 }
223
Dominik Laskowskif7a09ed2019-10-07 13:54:18 -0700224 void resetScheduler(Scheduler* scheduler) { mFlinger->mScheduler.reset(scheduler); }
225
Lloyd Pique90c115d2018-09-18 21:39:42 -0700226 using CreateBufferQueueFunction = surfaceflinger::test::Factory::CreateBufferQueueFunction;
Lloyd Pique5b36f3f2018-01-17 11:57:07 -0800227 void setCreateBufferQueueFunction(CreateBufferQueueFunction f) {
Lloyd Pique90c115d2018-09-18 21:39:42 -0700228 mFactory.mCreateBufferQueue = f;
Lloyd Pique5b36f3f2018-01-17 11:57:07 -0800229 }
230
Lloyd Pique90c115d2018-09-18 21:39:42 -0700231 using CreateNativeWindowSurfaceFunction =
232 surfaceflinger::test::Factory::CreateNativeWindowSurfaceFunction;
Lloyd Pique0b1fe702018-01-22 18:03:16 -0800233 void setCreateNativeWindowSurface(CreateNativeWindowSurfaceFunction f) {
Lloyd Pique90c115d2018-09-18 21:39:42 -0700234 mFactory.mCreateNativeWindowSurface = f;
Lloyd Pique0b1fe702018-01-22 18:03:16 -0800235 }
236
Daniel Solomon42d04562019-01-20 21:03:19 -0800237 void setInternalDisplayPrimaries(const ui::DisplayPrimaries& primaries) {
238 memcpy(&mFlinger->mInternalDisplayPrimaries, &primaries, sizeof(ui::DisplayPrimaries));
239 }
240
Lloyd Pique0b1fe702018-01-22 18:03:16 -0800241 using HotplugEvent = SurfaceFlinger::HotplugEvent;
242
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800243 auto& mutableLayerCurrentState(sp<Layer> layer) { return layer->mCurrentState; }
244 auto& mutableLayerDrawingState(sp<Layer> layer) { return layer->mDrawingState; }
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700245
Valerie Haud251afb2019-03-29 14:19:02 -0700246 auto& mutableStateLock() { return mFlinger->mStateLock; }
247
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700248 void setLayerSidebandStream(sp<Layer> layer, sp<NativeHandle> sidebandStream) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800249 layer->mDrawingState.sidebandStream = sidebandStream;
Lloyd Pique0b785d82018-12-04 17:25:27 -0800250 layer->mSidebandStream = sidebandStream;
Lloyd Piquede196652020-01-22 17:29:58 -0800251 layer->editCompositionState()->sidebandStream = sidebandStream;
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700252 }
253
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800254 void setLayerCompositionType(sp<Layer> layer, HWC2::Composition type) {
255 auto outputLayer = layer->findOutputLayerForDisplay(mFlinger->getDefaultDisplayDevice());
256 LOG_ALWAYS_FATAL_IF(!outputLayer);
257 auto& state = outputLayer->editState();
258 LOG_ALWAYS_FATAL_IF(!outputLayer->getState().hwc);
259 (*state.hwc).hwcCompositionType = static_cast<Hwc2::IComposerClient::Composition>(type);
260 };
261
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700262 void setLayerPotentialCursor(sp<Layer> layer, bool potentialCursor) {
263 layer->mPotentialCursor = potentialCursor;
264 }
265
Lloyd Piquef58625d2017-12-19 13:22:33 -0800266 /* ------------------------------------------------------------------------
267 * Forwarding for functions being tested
268 */
Lloyd Pique0b1fe702018-01-22 18:03:16 -0800269
Lloyd Piquea482f992018-01-22 19:00:34 -0800270 auto createDisplay(const String8& displayName, bool secure) {
271 return mFlinger->createDisplay(displayName, secure);
272 }
273
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700274 auto destroyDisplay(const sp<IBinder>& displayToken) {
275 return mFlinger->destroyDisplay(displayToken);
276 }
Lloyd Piquea482f992018-01-22 19:00:34 -0800277
Lloyd Piqued6fbb8a2018-01-22 19:08:36 -0800278 auto resetDisplayState() { return mFlinger->resetDisplayState(); }
279
Dominik Laskowski075d3172018-05-24 15:50:06 -0700280 auto setupNewDisplayDeviceInternal(const wp<IBinder>& displayToken,
281 const std::optional<DisplayId>& displayId,
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800282 const DisplayDeviceState& state,
Lloyd Pique542307f2018-10-19 13:24:08 -0700283 const sp<compositionengine::DisplaySurface>& dispSurface,
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800284 const sp<IGraphicBufferProducer>& producer) {
Dominik Laskowski7e045462018-05-30 13:02:02 -0700285 return mFlinger->setupNewDisplayDeviceInternal(displayToken, displayId, state, dispSurface,
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800286 producer);
287 }
288
Lloyd Pique0b1fe702018-01-22 18:03:16 -0800289 auto handleTransactionLocked(uint32_t transactionFlags) {
Vishnu Nairfa5f8df2019-02-13 17:02:51 -0800290 Mutex::Autolock _l(mFlinger->mStateLock);
Lloyd Pique0b1fe702018-01-22 18:03:16 -0800291 return mFlinger->handleTransactionLocked(transactionFlags);
292 }
Lloyd Piquef58625d2017-12-19 13:22:33 -0800293
Lloyd Pique6cf11032018-01-22 18:57:44 -0800294 auto onHotplugReceived(int32_t sequenceId, hwc2_display_t display,
295 HWC2::Connection connection) {
296 return mFlinger->onHotplugReceived(sequenceId, display, connection);
297 }
298
Vishnu Nairce5d0cc2019-02-28 14:38:41 -0800299 auto setDisplayStateLocked(const DisplayState& s) {
300 Mutex::Autolock _l(mFlinger->mStateLock);
301 return mFlinger->setDisplayStateLocked(s);
302 }
Lloyd Pique9d9cf402018-02-16 17:47:13 -0800303
Dominik Laskowski83b88212018-12-11 13:34:06 -0800304 // Allow reading display state without locking, as if called on the SF main thread.
305 auto onInitializeDisplays() NO_THREAD_SAFETY_ANALYSIS {
306 return mFlinger->onInitializeDisplays();
307 }
Lloyd Pique86016da2018-03-01 16:09:38 -0800308
Dominik Laskowski83b88212018-12-11 13:34:06 -0800309 // Allow reading display state without locking, as if called on the SF main thread.
310 auto setPowerModeInternal(const sp<DisplayDevice>& display,
311 int mode) NO_THREAD_SAFETY_ANALYSIS {
Dominik Laskowskie9774092018-12-11 10:04:24 -0800312 return mFlinger->setPowerModeInternal(display, mode);
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -0800313 }
314
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700315 auto onMessageReceived(int32_t what) { return mFlinger->onMessageReceived(what); }
316
Robert Carr108b2c72019-04-02 16:32:58 -0700317 auto captureScreenImplLocked(
318 const RenderArea& renderArea, SurfaceFlinger::TraverseLayersFunction traverseLayers,
319 ANativeWindowBuffer* buffer, bool useIdentityTransform, bool forSystem, int* outSyncFd) {
320 bool ignored;
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700321 return mFlinger->captureScreenImplLocked(renderArea, traverseLayers, buffer,
Robert Carr108b2c72019-04-02 16:32:58 -0700322 useIdentityTransform, forSystem, outSyncFd,
323 ignored);
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700324 }
325
chaviw0e3479f2018-09-10 16:49:30 -0700326 auto traverseLayersInDisplay(const sp<const DisplayDevice>& display,
327 const LayerVector::Visitor& visitor) {
328 return mFlinger->SurfaceFlinger::traverseLayersInDisplay(display, visitor);
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700329 }
330
Daniel Solomon42d04562019-01-20 21:03:19 -0800331 auto getDisplayNativePrimaries(const sp<IBinder>& displayToken,
332 ui::DisplayPrimaries &primaries) {
333 return mFlinger->SurfaceFlinger::getDisplayNativePrimaries(displayToken, primaries);
334 }
335
Valerie Haud251afb2019-03-29 14:19:02 -0700336 auto& getTransactionQueue() { return mFlinger->mTransactionQueues; }
337
338 auto setTransactionState(const Vector<ComposerState>& states,
339 const Vector<DisplayState>& displays, uint32_t flags,
340 const sp<IBinder>& applyToken,
341 const InputWindowCommands& inputWindowCommands,
342 int64_t desiredPresentTime, const client_cache_t& uncacheBuffer,
343 bool hasListenerCallbacks,
344 std::vector<ListenerCallbacks>& listenerCallbacks) {
345 return mFlinger->setTransactionState(states, displays, flags, applyToken,
346 inputWindowCommands, desiredPresentTime, uncacheBuffer,
347 hasListenerCallbacks, listenerCallbacks);
348 }
349
350 auto flushTransactionQueues() { return mFlinger->flushTransactionQueues(); };
351
Lloyd Pique86016da2018-03-01 16:09:38 -0800352 /* ------------------------------------------------------------------------
353 * Read-only access to private data to assert post-conditions.
354 */
355
356 const auto& getAnimFrameTracker() const { return mFlinger->mAnimFrameTracker; }
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -0800357 const auto& getHasPoweredOff() const { return mFlinger->mHasPoweredOff; }
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -0800358 const auto& getVisibleRegionsDirty() const { return mFlinger->mVisibleRegionsDirty; }
Lloyd Pique441d5042018-10-18 16:49:51 -0700359 auto& getHwComposer() const {
360 return static_cast<impl::HWComposer&>(mFlinger->getHwComposer());
361 }
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -0800362
Lloyd Pique86016da2018-03-01 16:09:38 -0800363 const auto& getCompositorTiming() const { return mFlinger->getBE().mCompositorTiming; }
364
Lloyd Piquef58625d2017-12-19 13:22:33 -0800365 /* ------------------------------------------------------------------------
366 * Read-write access to private data to set up preconditions and assert
367 * post-conditions.
368 */
Lloyd Pique0b1fe702018-01-22 18:03:16 -0800369
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800370 auto& mutableHasWideColorDisplay() { return SurfaceFlinger::hasWideColorDisplay; }
Peiyong Lin13effd12018-07-24 17:01:47 -0700371 auto& mutableUseColorManagement() { return SurfaceFlinger::useColorManagement; }
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800372
Lloyd Piquee39cad22017-12-20 17:01:29 -0800373 auto& mutableCurrentState() { return mFlinger->mCurrentState; }
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800374 auto& mutableDisplayColorSetting() { return mFlinger->mDisplayColorSetting; }
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700375 auto& mutableDisplays() { return mFlinger->mDisplays; }
Lloyd Piquee39cad22017-12-20 17:01:29 -0800376 auto& mutableDrawingState() { return mFlinger->mDrawingState; }
Lloyd Piquee39cad22017-12-20 17:01:29 -0800377 auto& mutableEventQueue() { return mFlinger->mEventQueue; }
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700378 auto& mutableGeometryInvalid() { return mFlinger->mGeometryInvalid; }
Lloyd Pique0b1fe702018-01-22 18:03:16 -0800379 auto& mutableInterceptor() { return mFlinger->mInterceptor; }
Lloyd Pique6cf11032018-01-22 18:57:44 -0800380 auto& mutableMainThreadId() { return mFlinger->mMainThreadId; }
Lloyd Pique0b1fe702018-01-22 18:03:16 -0800381 auto& mutablePendingHotplugEvents() { return mFlinger->mPendingHotplugEvents; }
Dominik Laskowski075d3172018-05-24 15:50:06 -0700382 auto& mutablePhysicalDisplayTokens() { return mFlinger->mPhysicalDisplayTokens; }
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700383 auto& mutableTexturePool() { return mFlinger->mTexturePool; }
Lloyd Pique0b1fe702018-01-22 18:03:16 -0800384 auto& mutableTransactionFlags() { return mFlinger->mTransactionFlags; }
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800385 auto& mutableUseHwcVirtualDisplays() { return mFlinger->mUseHwcVirtualDisplays; }
Peiyong Lin74ca2f42019-01-14 19:36:57 -0800386 auto& mutablePowerAdvisor() { return mFlinger->mPowerAdvisor; }
Lloyd Pique4fe29402019-08-12 16:51:24 -0700387 auto& mutableDebugDisableHWC() { return mFlinger->mDebugDisableHWC; }
Lloyd Piquee39cad22017-12-20 17:01:29 -0800388
Lloyd Pique6cf11032018-01-22 18:57:44 -0800389 auto& mutableComposerSequenceId() { return mFlinger->getBE().mComposerSequenceId; }
Lloyd Pique441d5042018-10-18 16:49:51 -0700390 auto& mutableHwcDisplayData() { return getHwComposer().mDisplayData; }
391 auto& mutableHwcPhysicalDisplayIdMap() { return getHwComposer().mPhysicalDisplayIdMap; }
392 auto& mutableInternalHwcDisplayId() { return getHwComposer().mInternalHwcDisplayId; }
393 auto& mutableExternalHwcDisplayId() { return getHwComposer().mExternalHwcDisplayId; }
Lloyd Piquea618d852018-01-17 11:52:30 -0800394
Valerie Hau09e60052019-12-15 14:51:15 -0800395 auto fromHandle(const sp<IBinder>& handle) {
396 Mutex::Autolock _l(mFlinger->mStateLock);
397 return mFlinger->fromHandle(handle);
398 }
399
Lloyd Piquee39cad22017-12-20 17:01:29 -0800400 ~TestableSurfaceFlinger() {
401 // All these pointer and container clears help ensure that GMock does
402 // not report a leaked object, since the SurfaceFlinger instance may
403 // still be referenced by something despite our best efforts to destroy
404 // it after each test is done.
405 mutableDisplays().clear();
Ady Abraham2939f092019-03-04 17:44:06 -0800406 mutableCurrentState().displays.clear();
407 mutableDrawingState().displays.clear();
Lloyd Pique0b1fe702018-01-22 18:03:16 -0800408 mutableEventQueue().reset();
Lloyd Pique0b1fe702018-01-22 18:03:16 -0800409 mutableInterceptor().reset();
Lloyd Pique2eb71eb2019-08-13 13:23:03 -0700410 mFlinger->mScheduler.reset();
Lloyd Pique441d5042018-10-18 16:49:51 -0700411 mFlinger->mCompositionEngine->setHwComposer(std::unique_ptr<HWComposer>());
Lloyd Piqueb97e04f2018-10-18 17:07:05 -0700412 mFlinger->mCompositionEngine->setRenderEngine(
413 std::unique_ptr<renderengine::RenderEngine>());
Lloyd Piquee39cad22017-12-20 17:01:29 -0800414 }
Lloyd Piquef58625d2017-12-19 13:22:33 -0800415
Lloyd Piquea618d852018-01-17 11:52:30 -0800416 /* ------------------------------------------------------------------------
417 * Wrapper classes for Read-write access to private data to set up
418 * preconditions and assert post-conditions.
419 */
Ana Krulec4593b692019-01-11 22:07:25 -0800420 struct HWC2Display : public HWC2::impl::Display {
Peiyong Lin74ca2f42019-01-14 19:36:57 -0800421 HWC2Display(Hwc2::Composer& composer,
Lloyd Piquea618d852018-01-17 11:52:30 -0800422 const std::unordered_set<HWC2::Capability>& capabilities, hwc2_display_t id,
423 HWC2::DisplayType type)
Peiyong Lin74ca2f42019-01-14 19:36:57 -0800424 : HWC2::impl::Display(composer, capabilities, id, type) {}
Lloyd Piquea618d852018-01-17 11:52:30 -0800425 ~HWC2Display() {
426 // Prevents a call to disable vsyncs.
427 mType = HWC2::DisplayType::Invalid;
428 }
429
430 auto& mutableIsConnected() { return this->mIsConnected; }
431 auto& mutableConfigs() { return this->mConfigs; }
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700432 auto& mutableLayers() { return this->mLayers; }
Lloyd Piquea618d852018-01-17 11:52:30 -0800433 };
434
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800435 class FakeHwcDisplayInjector {
436 public:
437 static constexpr hwc2_display_t DEFAULT_HWC_DISPLAY_ID = 1000;
438 static constexpr int32_t DEFAULT_WIDTH = 1920;
439 static constexpr int32_t DEFAULT_HEIGHT = 1280;
440 static constexpr int32_t DEFAULT_REFRESH_RATE = 16'666'666;
Ady Abraham2139f732019-11-13 18:56:40 -0800441 static constexpr int32_t DEFAULT_CONFIG_GROUP = 7;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800442 static constexpr int32_t DEFAULT_DPI = 320;
Ady Abraham8a82ba62020-01-17 12:43:17 -0800443 static constexpr hwc2_config_t DEFAULT_ACTIVE_CONFIG = 0;
Peiyong Lin1336e6e2019-05-28 09:23:50 -0700444 static constexpr int32_t DEFAULT_POWER_MODE = 2;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800445
Dominik Laskowski075d3172018-05-24 15:50:06 -0700446 FakeHwcDisplayInjector(DisplayId displayId, HWC2::DisplayType hwcDisplayType,
447 bool isPrimary)
448 : mDisplayId(displayId), mHwcDisplayType(hwcDisplayType), mIsPrimary(isPrimary) {}
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800449
450 auto& setHwcDisplayId(hwc2_display_t displayId) {
451 mHwcDisplayId = displayId;
452 return *this;
453 }
454
455 auto& setWidth(int32_t width) {
456 mWidth = width;
457 return *this;
458 }
459
460 auto& setHeight(int32_t height) {
461 mHeight = height;
462 return *this;
463 }
464
Ady Abraham8a82ba62020-01-17 12:43:17 -0800465 auto& setRefreshRate(int32_t refreshRate) {
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800466 mRefreshRate = refreshRate;
467 return *this;
468 }
469
470 auto& setDpiX(int32_t dpi) {
471 mDpiX = dpi;
472 return *this;
473 }
474
475 auto& setDpiY(int32_t dpi) {
476 mDpiY = dpi;
477 return *this;
478 }
479
Ady Abraham8a82ba62020-01-17 12:43:17 -0800480 auto& setActiveConfig(hwc2_config_t config) {
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800481 mActiveConfig = config;
482 return *this;
483 }
484
Lloyd Piquee22f0332018-07-16 16:35:56 -0700485 auto& setCapabilities(const std::unordered_set<HWC2::Capability>* capabilities) {
486 mCapabilities = capabilities;
487 return *this;
488 }
489
Peiyong Lin1336e6e2019-05-28 09:23:50 -0700490 auto& setPowerMode(int mode) {
491 mPowerMode = mode;
492 return *this;
493 }
494
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800495 void inject(TestableSurfaceFlinger* flinger, Hwc2::Composer* composer) {
Lloyd Piquee22f0332018-07-16 16:35:56 -0700496 static const std::unordered_set<HWC2::Capability> defaultCapabilities;
497 if (mCapabilities == nullptr) mCapabilities = &defaultCapabilities;
498
499 // Caution - Make sure that any values passed by reference here do
500 // not refer to an instance owned by FakeHwcDisplayInjector. This
501 // class has temporary lifetime, while the constructed HWC2::Display
502 // is much longer lived.
Peiyong Lin74ca2f42019-01-14 19:36:57 -0800503 auto display = std::make_unique<HWC2Display>(*composer, *mCapabilities, mHwcDisplayId,
504 mHwcDisplayType);
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800505
506 auto config = HWC2::Display::Config::Builder(*display, mActiveConfig);
507 config.setWidth(mWidth);
508 config.setHeight(mHeight);
509 config.setVsyncPeriod(mRefreshRate);
510 config.setDpiX(mDpiX);
511 config.setDpiY(mDpiY);
Ady Abraham2139f732019-11-13 18:56:40 -0800512 config.setConfigGroup(mConfigGroup);
Ady Abraham8a82ba62020-01-17 12:43:17 -0800513 display->mutableConfigs().emplace(static_cast<int32_t>(mActiveConfig), config.build());
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800514 display->mutableIsConnected() = true;
Peiyong Lin1336e6e2019-05-28 09:23:50 -0700515 display->setPowerMode(static_cast<HWC2::PowerMode>(mPowerMode));
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800516
Peiyong Linbdd08cc2019-12-17 21:35:14 -0800517 flinger->mutableHwcDisplayData()[mDisplayId].hwcDisplay = std::move(display);
Dominik Laskowski075d3172018-05-24 15:50:06 -0700518
519 if (mHwcDisplayType == HWC2::DisplayType::Physical) {
520 flinger->mutableHwcPhysicalDisplayIdMap().emplace(mHwcDisplayId, mDisplayId);
521 (mIsPrimary ? flinger->mutableInternalHwcDisplayId()
522 : flinger->mutableExternalHwcDisplayId()) = mHwcDisplayId;
523 }
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800524 }
525
526 private:
Dominik Laskowski075d3172018-05-24 15:50:06 -0700527 const DisplayId mDisplayId;
528 const HWC2::DisplayType mHwcDisplayType;
529 const bool mIsPrimary;
530
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800531 hwc2_display_t mHwcDisplayId = DEFAULT_HWC_DISPLAY_ID;
532 int32_t mWidth = DEFAULT_WIDTH;
533 int32_t mHeight = DEFAULT_HEIGHT;
Ady Abraham8a82ba62020-01-17 12:43:17 -0800534 int32_t mRefreshRate = DEFAULT_REFRESH_RATE;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800535 int32_t mDpiX = DEFAULT_DPI;
Ady Abraham2139f732019-11-13 18:56:40 -0800536 int32_t mConfigGroup = DEFAULT_CONFIG_GROUP;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800537 int32_t mDpiY = DEFAULT_DPI;
Ady Abraham8a82ba62020-01-17 12:43:17 -0800538 hwc2_config_t mActiveConfig = DEFAULT_ACTIVE_CONFIG;
Peiyong Lin1336e6e2019-05-28 09:23:50 -0700539 int32_t mPowerMode = DEFAULT_POWER_MODE;
Lloyd Piquee22f0332018-07-16 16:35:56 -0700540 const std::unordered_set<HWC2::Capability>* mCapabilities = nullptr;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800541 };
542
543 class FakeDisplayDeviceInjector {
544 public:
Dominik Laskowski075d3172018-05-24 15:50:06 -0700545 FakeDisplayDeviceInjector(TestableSurfaceFlinger& flinger,
Dominik Laskowski55c85402020-01-21 16:25:47 -0800546 std::optional<DisplayId> displayId,
547 std::optional<DisplayConnectionType> connectionType,
Dominik Laskowski075d3172018-05-24 15:50:06 -0700548 bool isPrimary)
549 : mFlinger(flinger), mCreationArgs(flinger.mFlinger.get(), mDisplayToken, displayId) {
Dominik Laskowski55c85402020-01-21 16:25:47 -0800550 mCreationArgs.connectionType = connectionType;
Dominik Laskowski075d3172018-05-24 15:50:06 -0700551 mCreationArgs.isPrimary = isPrimary;
552 }
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800553
554 sp<IBinder> token() const { return mDisplayToken; }
555
556 DisplayDeviceState& mutableDrawingDisplayState() {
557 return mFlinger.mutableDrawingState().displays.editValueFor(mDisplayToken);
558 }
559
560 DisplayDeviceState& mutableCurrentDisplayState() {
561 return mFlinger.mutableCurrentState().displays.editValueFor(mDisplayToken);
562 }
563
Lloyd Pique9d9cf402018-02-16 17:47:13 -0800564 const auto& getDrawingDisplayState() {
565 return mFlinger.mutableDrawingState().displays.valueFor(mDisplayToken);
566 }
567
568 const auto& getCurrentDisplayState() {
569 return mFlinger.mutableCurrentState().displays.valueFor(mDisplayToken);
570 }
571
Dominik Laskowski9fae1022018-05-29 13:17:40 -0700572 auto& mutableDisplayDevice() { return mFlinger.mutableDisplays()[mDisplayToken]; }
Lloyd Pique9d9cf402018-02-16 17:47:13 -0800573
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800574 auto& setNativeWindow(const sp<ANativeWindow>& nativeWindow) {
Lloyd Pique2eef1d22018-09-18 21:30:04 -0700575 mCreationArgs.nativeWindow = nativeWindow;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800576 return *this;
577 }
578
Lloyd Pique542307f2018-10-19 13:24:08 -0700579 auto& setDisplaySurface(const sp<compositionengine::DisplaySurface>& displaySurface) {
Lloyd Pique2eef1d22018-09-18 21:30:04 -0700580 mCreationArgs.displaySurface = displaySurface;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800581 return *this;
582 }
583
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800584 auto& setSecure(bool secure) {
Lloyd Pique2eef1d22018-09-18 21:30:04 -0700585 mCreationArgs.isSecure = secure;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800586 return *this;
587 }
588
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700589 auto& setPowerMode(int mode) {
Lloyd Pique2eef1d22018-09-18 21:30:04 -0700590 mCreationArgs.initialPowerMode = mode;
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700591 return *this;
592 }
593
Valerie Hau9758ae02018-10-09 16:05:09 -0700594 auto& setHwcColorModes(
595 const std::unordered_map<ui::ColorMode, std::vector<ui::RenderIntent>>
596 hwcColorModes) {
597 mCreationArgs.hwcColorModes = hwcColorModes;
598 return *this;
599 }
600
601 auto& setHasWideColorGamut(bool hasWideColorGamut) {
602 mCreationArgs.hasWideColorGamut = hasWideColorGamut;
603 return *this;
604 }
605
Lloyd Pique33050472019-12-19 17:12:44 -0800606 auto& setPhysicalOrientation(ui::Rotation orientation) {
607 mCreationArgs.physicalOrientation = orientation;
608 return *this;
609 }
610
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800611 sp<DisplayDevice> inject() {
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800612 DisplayDeviceState state;
Dominik Laskowski55c85402020-01-21 16:25:47 -0800613 if (const auto type = mCreationArgs.connectionType) {
614 const auto id = mCreationArgs.displayId;
615 LOG_ALWAYS_FATAL_IF(!id);
616 state.physical = {*id, *type};
617 }
618
Lloyd Pique2eef1d22018-09-18 21:30:04 -0700619 state.isSecure = mCreationArgs.isSecure;
620
621 sp<DisplayDevice> device = new DisplayDevice(std::move(mCreationArgs));
622 mFlinger.mutableDisplays().emplace(mDisplayToken, device);
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800623 mFlinger.mutableCurrentState().displays.add(mDisplayToken, state);
624 mFlinger.mutableDrawingState().displays.add(mDisplayToken, state);
625
Dominik Laskowski55c85402020-01-21 16:25:47 -0800626 if (const auto& physical = state.physical) {
627 mFlinger.mutablePhysicalDisplayTokens()[physical->id] = mDisplayToken;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800628 }
629
630 return device;
631 }
632
633 private:
634 TestableSurfaceFlinger& mFlinger;
635 sp<BBinder> mDisplayToken = new BBinder();
Lloyd Pique2eef1d22018-09-18 21:30:04 -0700636 DisplayDeviceCreationArgs mCreationArgs;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800637 };
638
Lloyd Pique90c115d2018-09-18 21:39:42 -0700639 surfaceflinger::test::Factory mFactory;
640 sp<SurfaceFlinger> mFlinger = new SurfaceFlinger(mFactory, SurfaceFlinger::SkipInitialization);
Dominik Laskowski7c9dbf92019-08-01 17:57:31 -0700641 TestableScheduler* mScheduler = nullptr;
Lloyd Piquef58625d2017-12-19 13:22:33 -0800642};
643
644} // namespace android