blob: 53525f3f6ddc1aaa20874d709b147ac7c3bcbfee [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>
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"
Yiwei Zhang7e666a52018-11-15 13:33:42 -080042
Lloyd Piquef58625d2017-12-19 13:22:33 -080043namespace android {
44
Lloyd Piquee39cad22017-12-20 17:01:29 -080045class EventThread;
46
Peiyong Lin833074a2018-08-28 11:53:54 -070047namespace renderengine {
Lloyd Pique90c115d2018-09-18 21:39:42 -070048
Lloyd Piquee39cad22017-12-20 17:01:29 -080049class RenderEngine;
Lloyd Pique90c115d2018-09-18 21:39:42 -070050
51} // namespace renderengine
Lloyd Piquee39cad22017-12-20 17:01:29 -080052
53namespace Hwc2 {
Lloyd Pique90c115d2018-09-18 21:39:42 -070054
Lloyd Piquee39cad22017-12-20 17:01:29 -080055class Composer;
Lloyd Pique90c115d2018-09-18 21:39:42 -070056
57} // namespace Hwc2
58
59namespace surfaceflinger::test {
60
61class Factory final : public surfaceflinger::Factory {
62public:
63 ~Factory() = default;
64
Dominik Laskowski98041832019-08-01 18:35:59 -070065 std::unique_ptr<DispSync> createDispSync(const char*, bool) override {
Lloyd Pique90c115d2018-09-18 21:39:42 -070066 return nullptr;
67 }
68
69 std::unique_ptr<EventControlThread> createEventControlThread(
70 std::function<void(bool)>) override {
Lloyd Pique90c115d2018-09-18 21:39:42 -070071 return nullptr;
72 }
73
74 std::unique_ptr<HWComposer> createHWComposer(const std::string&) override {
Lloyd Pique90c115d2018-09-18 21:39:42 -070075 return nullptr;
76 }
77
78 std::unique_ptr<MessageQueue> createMessageQueue() override {
Lloyd Pique90c115d2018-09-18 21:39:42 -070079 return std::make_unique<android::impl::MessageQueue>();
80 }
81
Ady Abraham9e16a482019-12-03 17:19:41 -080082 std::unique_ptr<scheduler::PhaseConfiguration> createPhaseConfiguration(
83 const scheduler::RefreshRateConfigs& /*refreshRateConfigs*/) override {
Ana Krulec757f63a2019-01-25 10:46:18 -080084 return std::make_unique<scheduler::FakePhaseOffsets>();
85 }
86
Ady Abraham09bd3922019-04-08 10:44:56 -070087 std::unique_ptr<Scheduler> createScheduler(std::function<void(bool)>,
Ady Abraham3a77a7b2019-12-02 18:46:59 -080088 const scheduler::RefreshRateConfigs&,
89 ISchedulerCallback&) override {
Lloyd Pique90c115d2018-09-18 21:39:42 -070090 return nullptr;
91 }
92
93 std::unique_ptr<SurfaceInterceptor> createSurfaceInterceptor(SurfaceFlinger* flinger) override {
Lloyd Pique90c115d2018-09-18 21:39:42 -070094 return std::make_unique<android::impl::SurfaceInterceptor>(flinger);
95 }
96
97 sp<StartPropertySetThread> createStartPropertySetThread(bool timestampPropertyValue) override {
Lloyd Pique90c115d2018-09-18 21:39:42 -070098 return new StartPropertySetThread(timestampPropertyValue);
99 }
100
Lloyd Pique9370a482019-10-03 17:58:30 -0700101 sp<DisplayDevice> createDisplayDevice(DisplayDeviceCreationArgs& creationArgs) override {
102 return new DisplayDevice(creationArgs);
Lloyd Pique90c115d2018-09-18 21:39:42 -0700103 }
104
105 sp<GraphicBuffer> createGraphicBuffer(uint32_t width, uint32_t height, PixelFormat format,
106 uint32_t layerCount, uint64_t usage,
107 std::string requestorName) override {
Lloyd Pique90c115d2018-09-18 21:39:42 -0700108 return new GraphicBuffer(width, height, format, layerCount, usage, requestorName);
109 }
110
111 void createBufferQueue(sp<IGraphicBufferProducer>* outProducer,
112 sp<IGraphicBufferConsumer>* outConsumer,
113 bool consumerIsSurfaceFlinger) override {
Lloyd Piquee300b312019-10-03 13:03:45 -0700114 if (!mCreateBufferQueue) {
115 BufferQueue::createBufferQueue(outProducer, outConsumer, consumerIsSurfaceFlinger);
116 return;
117 }
Lloyd Pique90c115d2018-09-18 21:39:42 -0700118 mCreateBufferQueue(outProducer, outConsumer, consumerIsSurfaceFlinger);
119 }
120
Lloyd Piquee300b312019-10-03 13:03:45 -0700121 sp<IGraphicBufferProducer> createMonitoredProducer(const sp<IGraphicBufferProducer>& producer,
122 const sp<SurfaceFlinger>& flinger,
123 const wp<Layer>& layer) override {
124 return new MonitoredProducer(producer, flinger, layer);
125 }
126
127 sp<BufferLayerConsumer> createBufferLayerConsumer(const sp<IGraphicBufferConsumer>& consumer,
128 renderengine::RenderEngine& renderEngine,
129 uint32_t textureName, Layer* layer) override {
130 return new BufferLayerConsumer(consumer, renderEngine, textureName, layer);
131 }
132
Lloyd Pique90c115d2018-09-18 21:39:42 -0700133 std::unique_ptr<surfaceflinger::NativeWindowSurface> createNativeWindowSurface(
134 const sp<IGraphicBufferProducer>& producer) override {
135 if (!mCreateNativeWindowSurface) return nullptr;
136 return mCreateNativeWindowSurface(producer);
137 }
138
Lloyd Pique70d91362018-10-18 16:02:55 -0700139 std::unique_ptr<compositionengine::CompositionEngine> createCompositionEngine() override {
140 return compositionengine::impl::createCompositionEngine();
141 }
142
Lloyd Pique90c115d2018-09-18 21:39:42 -0700143 sp<BufferQueueLayer> createBufferQueueLayer(const LayerCreationArgs&) override {
Lloyd Pique90c115d2018-09-18 21:39:42 -0700144 return nullptr;
145 }
146
147 sp<BufferStateLayer> createBufferStateLayer(const LayerCreationArgs&) override {
Lloyd Pique90c115d2018-09-18 21:39:42 -0700148 return nullptr;
149 }
150
Vishnu Nairfa247b12020-02-11 08:58:26 -0800151 sp<EffectLayer> createEffectLayer(const LayerCreationArgs&) override { return nullptr; }
Lloyd Pique90c115d2018-09-18 21:39:42 -0700152
153 sp<ContainerLayer> createContainerLayer(const LayerCreationArgs&) override {
Lloyd Pique90c115d2018-09-18 21:39:42 -0700154 return nullptr;
155 }
156
157 using CreateBufferQueueFunction =
158 std::function<void(sp<IGraphicBufferProducer>* /* outProducer */,
159 sp<IGraphicBufferConsumer>* /* outConsumer */,
160 bool /* consumerIsSurfaceFlinger */)>;
161 CreateBufferQueueFunction mCreateBufferQueue;
162
163 using CreateNativeWindowSurfaceFunction =
164 std::function<std::unique_ptr<surfaceflinger::NativeWindowSurface>(
165 const sp<IGraphicBufferProducer>&)>;
166 CreateNativeWindowSurfaceFunction mCreateNativeWindowSurface;
Lloyd Pique70d91362018-10-18 16:02:55 -0700167
168 using CreateCompositionEngineFunction =
169 std::function<std::unique_ptr<compositionengine::CompositionEngine>()>;
170 CreateCompositionEngineFunction mCreateCompositionEngine;
Lloyd Pique90c115d2018-09-18 21:39:42 -0700171};
172
173} // namespace surfaceflinger::test
Lloyd Piquee39cad22017-12-20 17:01:29 -0800174
Lloyd Piquef58625d2017-12-19 13:22:33 -0800175class TestableSurfaceFlinger {
176public:
Dominik Laskowskif7a09ed2019-10-07 13:54:18 -0700177 SurfaceFlinger* flinger() { return mFlinger.get(); }
Dominik Laskowski7c9dbf92019-08-01 17:57:31 -0700178 TestableScheduler* scheduler() { return mScheduler; }
179
Lloyd Piquef58625d2017-12-19 13:22:33 -0800180 // Extend this as needed for accessing SurfaceFlinger private (and public)
181 // functions.
182
Peiyong Lin833074a2018-08-28 11:53:54 -0700183 void setupRenderEngine(std::unique_ptr<renderengine::RenderEngine> renderEngine) {
Lloyd Piqueb97e04f2018-10-18 17:07:05 -0700184 mFlinger->mCompositionEngine->setRenderEngine(std::move(renderEngine));
Lloyd Piquee39cad22017-12-20 17:01:29 -0800185 }
186
187 void setupComposer(std::unique_ptr<Hwc2::Composer> composer) {
Lloyd Pique441d5042018-10-18 16:49:51 -0700188 mFlinger->mCompositionEngine->setHwComposer(
189 std::make_unique<impl::HWComposer>(std::move(composer)));
Lloyd Piquee39cad22017-12-20 17:01:29 -0800190 }
191
Alec Mourie4034bb2019-11-19 12:45:54 -0800192 void setupTimeStats(const std::shared_ptr<TimeStats>& timeStats) {
193 mFlinger->mCompositionEngine->setTimeStats(timeStats);
194 }
195
Dominik Laskowski7c9dbf92019-08-01 17:57:31 -0700196 void setupScheduler(std::unique_ptr<DispSync> primaryDispSync,
197 std::unique_ptr<EventControlThread> eventControlThread,
198 std::unique_ptr<EventThread> appEventThread,
Ady Abraham8a82ba62020-01-17 12:43:17 -0800199 std::unique_ptr<EventThread> sfEventThread,
200 bool useContentDetectionV2 = false) {
Ady Abraham2139f732019-11-13 18:56:40 -0800201 std::vector<scheduler::RefreshRateConfigs::InputConfig> configs{
202 {{HwcConfigIndexType(0), HwcConfigGroupType(0), 16666667}}};
203 mFlinger->mRefreshRateConfigs = std::make_unique<
Ana Krulec3f6a2062020-01-23 15:48:01 -0800204 scheduler::RefreshRateConfigs>(configs, /*currentConfig=*/HwcConfigIndexType(0));
Ady Abraham2139f732019-11-13 18:56:40 -0800205 mFlinger->mRefreshRateStats = std::make_unique<
206 scheduler::RefreshRateStats>(*mFlinger->mRefreshRateConfigs, *mFlinger->mTimeStats,
207 /*currentConfig=*/HwcConfigIndexType(0),
208 /*powerMode=*/HWC_POWER_MODE_OFF);
Ady Abraham9e16a482019-12-03 17:19:41 -0800209 mFlinger->mPhaseConfiguration =
210 mFactory.createPhaseConfiguration(*mFlinger->mRefreshRateConfigs);
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700211
Dominik Laskowski7c9dbf92019-08-01 17:57:31 -0700212 mScheduler =
213 new TestableScheduler(std::move(primaryDispSync), std::move(eventControlThread),
Ady Abraham8a82ba62020-01-17 12:43:17 -0800214 *mFlinger->mRefreshRateConfigs, useContentDetectionV2);
Dominik Laskowski7c9dbf92019-08-01 17:57:31 -0700215
Dominik Laskowski98041832019-08-01 18:35:59 -0700216 mFlinger->mAppConnectionHandle = mScheduler->createConnection(std::move(appEventThread));
217 mFlinger->mSfConnectionHandle = mScheduler->createConnection(std::move(sfEventThread));
Dominik Laskowskif7a09ed2019-10-07 13:54:18 -0700218 resetScheduler(mScheduler);
Dominik Laskowski7c9dbf92019-08-01 17:57:31 -0700219
Dominik Laskowski7c9dbf92019-08-01 17:57:31 -0700220 mFlinger->mVSyncModulator.emplace(*mScheduler, mFlinger->mAppConnectionHandle,
221 mFlinger->mSfConnectionHandle,
Ady Abraham9e16a482019-12-03 17:19:41 -0800222 mFlinger->mPhaseConfiguration->getCurrentOffsets());
Dominik Laskowski7c9dbf92019-08-01 17:57:31 -0700223 }
224
Dominik Laskowskif7a09ed2019-10-07 13:54:18 -0700225 void resetScheduler(Scheduler* scheduler) { mFlinger->mScheduler.reset(scheduler); }
226
Lloyd Pique90c115d2018-09-18 21:39:42 -0700227 using CreateBufferQueueFunction = surfaceflinger::test::Factory::CreateBufferQueueFunction;
Lloyd Pique5b36f3f2018-01-17 11:57:07 -0800228 void setCreateBufferQueueFunction(CreateBufferQueueFunction f) {
Lloyd Pique90c115d2018-09-18 21:39:42 -0700229 mFactory.mCreateBufferQueue = f;
Lloyd Pique5b36f3f2018-01-17 11:57:07 -0800230 }
231
Lloyd Pique90c115d2018-09-18 21:39:42 -0700232 using CreateNativeWindowSurfaceFunction =
233 surfaceflinger::test::Factory::CreateNativeWindowSurfaceFunction;
Lloyd Pique0b1fe702018-01-22 18:03:16 -0800234 void setCreateNativeWindowSurface(CreateNativeWindowSurfaceFunction f) {
Lloyd Pique90c115d2018-09-18 21:39:42 -0700235 mFactory.mCreateNativeWindowSurface = f;
Lloyd Pique0b1fe702018-01-22 18:03:16 -0800236 }
237
Daniel Solomon42d04562019-01-20 21:03:19 -0800238 void setInternalDisplayPrimaries(const ui::DisplayPrimaries& primaries) {
239 memcpy(&mFlinger->mInternalDisplayPrimaries, &primaries, sizeof(ui::DisplayPrimaries));
240 }
241
Lloyd Pique0b1fe702018-01-22 18:03:16 -0800242 using HotplugEvent = SurfaceFlinger::HotplugEvent;
243
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800244 auto& mutableLayerCurrentState(sp<Layer> layer) { return layer->mCurrentState; }
245 auto& mutableLayerDrawingState(sp<Layer> layer) { return layer->mDrawingState; }
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700246
Valerie Haud251afb2019-03-29 14:19:02 -0700247 auto& mutableStateLock() { return mFlinger->mStateLock; }
248
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700249 void setLayerSidebandStream(sp<Layer> layer, sp<NativeHandle> sidebandStream) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800250 layer->mDrawingState.sidebandStream = sidebandStream;
Lloyd Pique0b785d82018-12-04 17:25:27 -0800251 layer->mSidebandStream = sidebandStream;
Lloyd Piquede196652020-01-22 17:29:58 -0800252 layer->editCompositionState()->sidebandStream = sidebandStream;
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700253 }
254
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800255 void setLayerCompositionType(sp<Layer> layer, HWC2::Composition type) {
256 auto outputLayer = layer->findOutputLayerForDisplay(mFlinger->getDefaultDisplayDevice());
257 LOG_ALWAYS_FATAL_IF(!outputLayer);
258 auto& state = outputLayer->editState();
259 LOG_ALWAYS_FATAL_IF(!outputLayer->getState().hwc);
260 (*state.hwc).hwcCompositionType = static_cast<Hwc2::IComposerClient::Composition>(type);
261 };
262
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700263 void setLayerPotentialCursor(sp<Layer> layer, bool potentialCursor) {
264 layer->mPotentialCursor = potentialCursor;
265 }
266
Lloyd Piquef58625d2017-12-19 13:22:33 -0800267 /* ------------------------------------------------------------------------
268 * Forwarding for functions being tested
269 */
Lloyd Pique0b1fe702018-01-22 18:03:16 -0800270
Lloyd Piquea482f992018-01-22 19:00:34 -0800271 auto createDisplay(const String8& displayName, bool secure) {
272 return mFlinger->createDisplay(displayName, secure);
273 }
274
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700275 auto destroyDisplay(const sp<IBinder>& displayToken) {
276 return mFlinger->destroyDisplay(displayToken);
277 }
Lloyd Piquea482f992018-01-22 19:00:34 -0800278
Lloyd Piqued6fbb8a2018-01-22 19:08:36 -0800279 auto resetDisplayState() { return mFlinger->resetDisplayState(); }
280
Lloyd Pique9370a482019-10-03 17:58:30 -0700281 auto setupNewDisplayDeviceInternal(
282 const wp<IBinder>& displayToken,
283 std::shared_ptr<compositionengine::Display> compositionDisplay,
284 const DisplayDeviceState& state,
285 const sp<compositionengine::DisplaySurface>& dispSurface,
286 const sp<IGraphicBufferProducer>& producer) {
287 return mFlinger->setupNewDisplayDeviceInternal(displayToken, compositionDisplay, state,
288 dispSurface, producer);
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800289 }
290
Lloyd Pique0b1fe702018-01-22 18:03:16 -0800291 auto handleTransactionLocked(uint32_t transactionFlags) {
Vishnu Nairfa5f8df2019-02-13 17:02:51 -0800292 Mutex::Autolock _l(mFlinger->mStateLock);
Lloyd Pique0b1fe702018-01-22 18:03:16 -0800293 return mFlinger->handleTransactionLocked(transactionFlags);
294 }
Lloyd Piquef58625d2017-12-19 13:22:33 -0800295
Lloyd Pique6cf11032018-01-22 18:57:44 -0800296 auto onHotplugReceived(int32_t sequenceId, hwc2_display_t display,
297 HWC2::Connection connection) {
298 return mFlinger->onHotplugReceived(sequenceId, display, connection);
299 }
300
Vishnu Nairce5d0cc2019-02-28 14:38:41 -0800301 auto setDisplayStateLocked(const DisplayState& s) {
302 Mutex::Autolock _l(mFlinger->mStateLock);
303 return mFlinger->setDisplayStateLocked(s);
304 }
Lloyd Pique9d9cf402018-02-16 17:47:13 -0800305
Dominik Laskowski83b88212018-12-11 13:34:06 -0800306 // Allow reading display state without locking, as if called on the SF main thread.
307 auto onInitializeDisplays() NO_THREAD_SAFETY_ANALYSIS {
308 return mFlinger->onInitializeDisplays();
309 }
Lloyd Pique86016da2018-03-01 16:09:38 -0800310
Dominik Laskowski83b88212018-12-11 13:34:06 -0800311 // Allow reading display state without locking, as if called on the SF main thread.
312 auto setPowerModeInternal(const sp<DisplayDevice>& display,
313 int mode) NO_THREAD_SAFETY_ANALYSIS {
Dominik Laskowskie9774092018-12-11 10:04:24 -0800314 return mFlinger->setPowerModeInternal(display, mode);
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -0800315 }
316
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700317 auto onMessageReceived(int32_t what) { return mFlinger->onMessageReceived(what); }
318
Ana Krulecfc874ae2020-02-22 15:39:32 -0800319 auto captureScreenImplLocked(const RenderArea& renderArea,
320 SurfaceFlinger::TraverseLayersFunction traverseLayers,
321 const sp<GraphicBuffer>& buffer, bool useIdentityTransform,
322 bool forSystem, int* outSyncFd) {
Robert Carr108b2c72019-04-02 16:32:58 -0700323 bool ignored;
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700324 return mFlinger->captureScreenImplLocked(renderArea, traverseLayers, buffer,
Robert Carr108b2c72019-04-02 16:32:58 -0700325 useIdentityTransform, forSystem, outSyncFd,
326 ignored);
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700327 }
328
chaviw0e3479f2018-09-10 16:49:30 -0700329 auto traverseLayersInDisplay(const sp<const DisplayDevice>& display,
330 const LayerVector::Visitor& visitor) {
331 return mFlinger->SurfaceFlinger::traverseLayersInDisplay(display, visitor);
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700332 }
333
Daniel Solomon42d04562019-01-20 21:03:19 -0800334 auto getDisplayNativePrimaries(const sp<IBinder>& displayToken,
335 ui::DisplayPrimaries &primaries) {
336 return mFlinger->SurfaceFlinger::getDisplayNativePrimaries(displayToken, primaries);
337 }
338
Valerie Haud251afb2019-03-29 14:19:02 -0700339 auto& getTransactionQueue() { return mFlinger->mTransactionQueues; }
340
341 auto setTransactionState(const Vector<ComposerState>& states,
342 const Vector<DisplayState>& displays, uint32_t flags,
343 const sp<IBinder>& applyToken,
344 const InputWindowCommands& inputWindowCommands,
345 int64_t desiredPresentTime, const client_cache_t& uncacheBuffer,
346 bool hasListenerCallbacks,
347 std::vector<ListenerCallbacks>& listenerCallbacks) {
348 return mFlinger->setTransactionState(states, displays, flags, applyToken,
349 inputWindowCommands, desiredPresentTime, uncacheBuffer,
350 hasListenerCallbacks, listenerCallbacks);
351 }
352
353 auto flushTransactionQueues() { return mFlinger->flushTransactionQueues(); };
354
Lloyd Pique86016da2018-03-01 16:09:38 -0800355 /* ------------------------------------------------------------------------
356 * Read-only access to private data to assert post-conditions.
357 */
358
359 const auto& getAnimFrameTracker() const { return mFlinger->mAnimFrameTracker; }
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -0800360 const auto& getHasPoweredOff() const { return mFlinger->mHasPoweredOff; }
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -0800361 const auto& getVisibleRegionsDirty() const { return mFlinger->mVisibleRegionsDirty; }
Lloyd Pique441d5042018-10-18 16:49:51 -0700362 auto& getHwComposer() const {
363 return static_cast<impl::HWComposer&>(mFlinger->getHwComposer());
364 }
Lloyd Pique9370a482019-10-03 17:58:30 -0700365 auto& getCompositionEngine() const { return mFlinger->getCompositionEngine(); }
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -0800366
Lloyd Pique86016da2018-03-01 16:09:38 -0800367 const auto& getCompositorTiming() const { return mFlinger->getBE().mCompositorTiming; }
368
Lloyd Piquef58625d2017-12-19 13:22:33 -0800369 /* ------------------------------------------------------------------------
370 * Read-write access to private data to set up preconditions and assert
371 * post-conditions.
372 */
Lloyd Pique0b1fe702018-01-22 18:03:16 -0800373
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800374 auto& mutableHasWideColorDisplay() { return SurfaceFlinger::hasWideColorDisplay; }
Peiyong Lin13effd12018-07-24 17:01:47 -0700375 auto& mutableUseColorManagement() { return SurfaceFlinger::useColorManagement; }
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800376
Lloyd Piquee39cad22017-12-20 17:01:29 -0800377 auto& mutableCurrentState() { return mFlinger->mCurrentState; }
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800378 auto& mutableDisplayColorSetting() { return mFlinger->mDisplayColorSetting; }
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700379 auto& mutableDisplays() { return mFlinger->mDisplays; }
Lloyd Piquee39cad22017-12-20 17:01:29 -0800380 auto& mutableDrawingState() { return mFlinger->mDrawingState; }
Lloyd Piquee39cad22017-12-20 17:01:29 -0800381 auto& mutableEventQueue() { return mFlinger->mEventQueue; }
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700382 auto& mutableGeometryInvalid() { return mFlinger->mGeometryInvalid; }
Lloyd Pique0b1fe702018-01-22 18:03:16 -0800383 auto& mutableInterceptor() { return mFlinger->mInterceptor; }
Lloyd Pique6cf11032018-01-22 18:57:44 -0800384 auto& mutableMainThreadId() { return mFlinger->mMainThreadId; }
Lloyd Pique0b1fe702018-01-22 18:03:16 -0800385 auto& mutablePendingHotplugEvents() { return mFlinger->mPendingHotplugEvents; }
Dominik Laskowski075d3172018-05-24 15:50:06 -0700386 auto& mutablePhysicalDisplayTokens() { return mFlinger->mPhysicalDisplayTokens; }
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700387 auto& mutableTexturePool() { return mFlinger->mTexturePool; }
Lloyd Pique0b1fe702018-01-22 18:03:16 -0800388 auto& mutableTransactionFlags() { return mFlinger->mTransactionFlags; }
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800389 auto& mutableUseHwcVirtualDisplays() { return mFlinger->mUseHwcVirtualDisplays; }
Peiyong Lin74ca2f42019-01-14 19:36:57 -0800390 auto& mutablePowerAdvisor() { return mFlinger->mPowerAdvisor; }
Lloyd Pique4fe29402019-08-12 16:51:24 -0700391 auto& mutableDebugDisableHWC() { return mFlinger->mDebugDisableHWC; }
Lloyd Piquee39cad22017-12-20 17:01:29 -0800392
Lloyd Pique6cf11032018-01-22 18:57:44 -0800393 auto& mutableComposerSequenceId() { return mFlinger->getBE().mComposerSequenceId; }
Lloyd Pique441d5042018-10-18 16:49:51 -0700394 auto& mutableHwcDisplayData() { return getHwComposer().mDisplayData; }
395 auto& mutableHwcPhysicalDisplayIdMap() { return getHwComposer().mPhysicalDisplayIdMap; }
396 auto& mutableInternalHwcDisplayId() { return getHwComposer().mInternalHwcDisplayId; }
397 auto& mutableExternalHwcDisplayId() { return getHwComposer().mExternalHwcDisplayId; }
Lloyd Piquea618d852018-01-17 11:52:30 -0800398
Valerie Hau09e60052019-12-15 14:51:15 -0800399 auto fromHandle(const sp<IBinder>& handle) {
400 Mutex::Autolock _l(mFlinger->mStateLock);
401 return mFlinger->fromHandle(handle);
402 }
403
Lloyd Piquee39cad22017-12-20 17:01:29 -0800404 ~TestableSurfaceFlinger() {
405 // All these pointer and container clears help ensure that GMock does
406 // not report a leaked object, since the SurfaceFlinger instance may
407 // still be referenced by something despite our best efforts to destroy
408 // it after each test is done.
409 mutableDisplays().clear();
Ady Abraham2939f092019-03-04 17:44:06 -0800410 mutableCurrentState().displays.clear();
411 mutableDrawingState().displays.clear();
Lloyd Pique0b1fe702018-01-22 18:03:16 -0800412 mutableEventQueue().reset();
Lloyd Pique0b1fe702018-01-22 18:03:16 -0800413 mutableInterceptor().reset();
Lloyd Pique2eb71eb2019-08-13 13:23:03 -0700414 mFlinger->mScheduler.reset();
Lloyd Pique441d5042018-10-18 16:49:51 -0700415 mFlinger->mCompositionEngine->setHwComposer(std::unique_ptr<HWComposer>());
Lloyd Piqueb97e04f2018-10-18 17:07:05 -0700416 mFlinger->mCompositionEngine->setRenderEngine(
417 std::unique_ptr<renderengine::RenderEngine>());
Lloyd Piquee39cad22017-12-20 17:01:29 -0800418 }
Lloyd Piquef58625d2017-12-19 13:22:33 -0800419
Lloyd Piquea618d852018-01-17 11:52:30 -0800420 /* ------------------------------------------------------------------------
421 * Wrapper classes for Read-write access to private data to set up
422 * preconditions and assert post-conditions.
423 */
Ana Krulec4593b692019-01-11 22:07:25 -0800424 struct HWC2Display : public HWC2::impl::Display {
Peiyong Lin74ca2f42019-01-14 19:36:57 -0800425 HWC2Display(Hwc2::Composer& composer,
Lloyd Piquea618d852018-01-17 11:52:30 -0800426 const std::unordered_set<HWC2::Capability>& capabilities, hwc2_display_t id,
427 HWC2::DisplayType type)
Peiyong Lin74ca2f42019-01-14 19:36:57 -0800428 : HWC2::impl::Display(composer, capabilities, id, type) {}
Lloyd Piquea618d852018-01-17 11:52:30 -0800429 ~HWC2Display() {
430 // Prevents a call to disable vsyncs.
431 mType = HWC2::DisplayType::Invalid;
432 }
433
434 auto& mutableIsConnected() { return this->mIsConnected; }
435 auto& mutableConfigs() { return this->mConfigs; }
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700436 auto& mutableLayers() { return this->mLayers; }
Lloyd Piquea618d852018-01-17 11:52:30 -0800437 };
438
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800439 class FakeHwcDisplayInjector {
440 public:
441 static constexpr hwc2_display_t DEFAULT_HWC_DISPLAY_ID = 1000;
442 static constexpr int32_t DEFAULT_WIDTH = 1920;
443 static constexpr int32_t DEFAULT_HEIGHT = 1280;
444 static constexpr int32_t DEFAULT_REFRESH_RATE = 16'666'666;
Ady Abraham2139f732019-11-13 18:56:40 -0800445 static constexpr int32_t DEFAULT_CONFIG_GROUP = 7;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800446 static constexpr int32_t DEFAULT_DPI = 320;
Ady Abraham8a82ba62020-01-17 12:43:17 -0800447 static constexpr hwc2_config_t DEFAULT_ACTIVE_CONFIG = 0;
Peiyong Lin1336e6e2019-05-28 09:23:50 -0700448 static constexpr int32_t DEFAULT_POWER_MODE = 2;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800449
Dominik Laskowski075d3172018-05-24 15:50:06 -0700450 FakeHwcDisplayInjector(DisplayId displayId, HWC2::DisplayType hwcDisplayType,
451 bool isPrimary)
452 : mDisplayId(displayId), mHwcDisplayType(hwcDisplayType), mIsPrimary(isPrimary) {}
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800453
454 auto& setHwcDisplayId(hwc2_display_t displayId) {
455 mHwcDisplayId = displayId;
456 return *this;
457 }
458
459 auto& setWidth(int32_t width) {
460 mWidth = width;
461 return *this;
462 }
463
464 auto& setHeight(int32_t height) {
465 mHeight = height;
466 return *this;
467 }
468
Ady Abraham8a82ba62020-01-17 12:43:17 -0800469 auto& setRefreshRate(int32_t refreshRate) {
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800470 mRefreshRate = refreshRate;
471 return *this;
472 }
473
474 auto& setDpiX(int32_t dpi) {
475 mDpiX = dpi;
476 return *this;
477 }
478
479 auto& setDpiY(int32_t dpi) {
480 mDpiY = dpi;
481 return *this;
482 }
483
Ady Abraham8a82ba62020-01-17 12:43:17 -0800484 auto& setActiveConfig(hwc2_config_t config) {
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800485 mActiveConfig = config;
486 return *this;
487 }
488
Lloyd Piquee22f0332018-07-16 16:35:56 -0700489 auto& setCapabilities(const std::unordered_set<HWC2::Capability>* capabilities) {
490 mCapabilities = capabilities;
491 return *this;
492 }
493
Peiyong Lin1336e6e2019-05-28 09:23:50 -0700494 auto& setPowerMode(int mode) {
495 mPowerMode = mode;
496 return *this;
497 }
498
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800499 void inject(TestableSurfaceFlinger* flinger, Hwc2::Composer* composer) {
Lloyd Piquee22f0332018-07-16 16:35:56 -0700500 static const std::unordered_set<HWC2::Capability> defaultCapabilities;
501 if (mCapabilities == nullptr) mCapabilities = &defaultCapabilities;
502
503 // Caution - Make sure that any values passed by reference here do
504 // not refer to an instance owned by FakeHwcDisplayInjector. This
505 // class has temporary lifetime, while the constructed HWC2::Display
506 // is much longer lived.
Peiyong Lin74ca2f42019-01-14 19:36:57 -0800507 auto display = std::make_unique<HWC2Display>(*composer, *mCapabilities, mHwcDisplayId,
508 mHwcDisplayType);
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800509
510 auto config = HWC2::Display::Config::Builder(*display, mActiveConfig);
511 config.setWidth(mWidth);
512 config.setHeight(mHeight);
513 config.setVsyncPeriod(mRefreshRate);
514 config.setDpiX(mDpiX);
515 config.setDpiY(mDpiY);
Ady Abraham2139f732019-11-13 18:56:40 -0800516 config.setConfigGroup(mConfigGroup);
Ady Abraham8a82ba62020-01-17 12:43:17 -0800517 display->mutableConfigs().emplace(static_cast<int32_t>(mActiveConfig), config.build());
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800518 display->mutableIsConnected() = true;
Peiyong Lin1336e6e2019-05-28 09:23:50 -0700519 display->setPowerMode(static_cast<HWC2::PowerMode>(mPowerMode));
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800520
Peiyong Linbdd08cc2019-12-17 21:35:14 -0800521 flinger->mutableHwcDisplayData()[mDisplayId].hwcDisplay = std::move(display);
Dominik Laskowski075d3172018-05-24 15:50:06 -0700522
523 if (mHwcDisplayType == HWC2::DisplayType::Physical) {
524 flinger->mutableHwcPhysicalDisplayIdMap().emplace(mHwcDisplayId, mDisplayId);
525 (mIsPrimary ? flinger->mutableInternalHwcDisplayId()
526 : flinger->mutableExternalHwcDisplayId()) = mHwcDisplayId;
527 }
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800528 }
529
530 private:
Dominik Laskowski075d3172018-05-24 15:50:06 -0700531 const DisplayId mDisplayId;
532 const HWC2::DisplayType mHwcDisplayType;
533 const bool mIsPrimary;
534
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800535 hwc2_display_t mHwcDisplayId = DEFAULT_HWC_DISPLAY_ID;
536 int32_t mWidth = DEFAULT_WIDTH;
537 int32_t mHeight = DEFAULT_HEIGHT;
Ady Abraham8a82ba62020-01-17 12:43:17 -0800538 int32_t mRefreshRate = DEFAULT_REFRESH_RATE;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800539 int32_t mDpiX = DEFAULT_DPI;
Ady Abraham2139f732019-11-13 18:56:40 -0800540 int32_t mConfigGroup = DEFAULT_CONFIG_GROUP;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800541 int32_t mDpiY = DEFAULT_DPI;
Ady Abraham8a82ba62020-01-17 12:43:17 -0800542 hwc2_config_t mActiveConfig = DEFAULT_ACTIVE_CONFIG;
Peiyong Lin1336e6e2019-05-28 09:23:50 -0700543 int32_t mPowerMode = DEFAULT_POWER_MODE;
Lloyd Piquee22f0332018-07-16 16:35:56 -0700544 const std::unordered_set<HWC2::Capability>* mCapabilities = nullptr;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800545 };
546
547 class FakeDisplayDeviceInjector {
548 public:
Dominik Laskowski075d3172018-05-24 15:50:06 -0700549 FakeDisplayDeviceInjector(TestableSurfaceFlinger& flinger,
Lloyd Pique9370a482019-10-03 17:58:30 -0700550 std::shared_ptr<compositionengine::Display> compositionDisplay,
Dominik Laskowski55c85402020-01-21 16:25:47 -0800551 std::optional<DisplayConnectionType> connectionType,
Dominik Laskowski075d3172018-05-24 15:50:06 -0700552 bool isPrimary)
Lloyd Pique9370a482019-10-03 17:58:30 -0700553 : mFlinger(flinger),
554 mCreationArgs(flinger.mFlinger.get(), mDisplayToken, compositionDisplay) {
Dominik Laskowski55c85402020-01-21 16:25:47 -0800555 mCreationArgs.connectionType = connectionType;
Dominik Laskowski075d3172018-05-24 15:50:06 -0700556 mCreationArgs.isPrimary = isPrimary;
557 }
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800558
559 sp<IBinder> token() const { return mDisplayToken; }
560
561 DisplayDeviceState& mutableDrawingDisplayState() {
562 return mFlinger.mutableDrawingState().displays.editValueFor(mDisplayToken);
563 }
564
565 DisplayDeviceState& mutableCurrentDisplayState() {
566 return mFlinger.mutableCurrentState().displays.editValueFor(mDisplayToken);
567 }
568
Lloyd Pique9d9cf402018-02-16 17:47:13 -0800569 const auto& getDrawingDisplayState() {
570 return mFlinger.mutableDrawingState().displays.valueFor(mDisplayToken);
571 }
572
573 const auto& getCurrentDisplayState() {
574 return mFlinger.mutableCurrentState().displays.valueFor(mDisplayToken);
575 }
576
Dominik Laskowski9fae1022018-05-29 13:17:40 -0700577 auto& mutableDisplayDevice() { return mFlinger.mutableDisplays()[mDisplayToken]; }
Lloyd Pique9d9cf402018-02-16 17:47:13 -0800578
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800579 auto& setNativeWindow(const sp<ANativeWindow>& nativeWindow) {
Lloyd Pique2eef1d22018-09-18 21:30:04 -0700580 mCreationArgs.nativeWindow = nativeWindow;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800581 return *this;
582 }
583
Lloyd Pique542307f2018-10-19 13:24:08 -0700584 auto& setDisplaySurface(const sp<compositionengine::DisplaySurface>& displaySurface) {
Lloyd Pique2eef1d22018-09-18 21:30:04 -0700585 mCreationArgs.displaySurface = displaySurface;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800586 return *this;
587 }
588
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800589 auto& setSecure(bool secure) {
Lloyd Pique2eef1d22018-09-18 21:30:04 -0700590 mCreationArgs.isSecure = secure;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800591 return *this;
592 }
593
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700594 auto& setPowerMode(int mode) {
Lloyd Pique2eef1d22018-09-18 21:30:04 -0700595 mCreationArgs.initialPowerMode = mode;
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700596 return *this;
597 }
598
Valerie Hau9758ae02018-10-09 16:05:09 -0700599 auto& setHwcColorModes(
600 const std::unordered_map<ui::ColorMode, std::vector<ui::RenderIntent>>
601 hwcColorModes) {
602 mCreationArgs.hwcColorModes = hwcColorModes;
603 return *this;
604 }
605
606 auto& setHasWideColorGamut(bool hasWideColorGamut) {
607 mCreationArgs.hasWideColorGamut = hasWideColorGamut;
608 return *this;
609 }
610
Lloyd Pique33050472019-12-19 17:12:44 -0800611 auto& setPhysicalOrientation(ui::Rotation orientation) {
612 mCreationArgs.physicalOrientation = orientation;
613 return *this;
614 }
615
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800616 sp<DisplayDevice> inject() {
Lloyd Pique9370a482019-10-03 17:58:30 -0700617 const auto displayId = mCreationArgs.compositionDisplay->getDisplayId();
618
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800619 DisplayDeviceState state;
Dominik Laskowski55c85402020-01-21 16:25:47 -0800620 if (const auto type = mCreationArgs.connectionType) {
Lloyd Pique9370a482019-10-03 17:58:30 -0700621 LOG_ALWAYS_FATAL_IF(!displayId);
622 state.physical = {*displayId, *type};
Dominik Laskowski55c85402020-01-21 16:25:47 -0800623 }
624
Lloyd Pique2eef1d22018-09-18 21:30:04 -0700625 state.isSecure = mCreationArgs.isSecure;
626
Lloyd Pique9370a482019-10-03 17:58:30 -0700627 sp<DisplayDevice> device = new DisplayDevice(mCreationArgs);
Lloyd Pique2eef1d22018-09-18 21:30:04 -0700628 mFlinger.mutableDisplays().emplace(mDisplayToken, device);
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800629 mFlinger.mutableCurrentState().displays.add(mDisplayToken, state);
630 mFlinger.mutableDrawingState().displays.add(mDisplayToken, state);
631
Dominik Laskowski55c85402020-01-21 16:25:47 -0800632 if (const auto& physical = state.physical) {
633 mFlinger.mutablePhysicalDisplayTokens()[physical->id] = mDisplayToken;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800634 }
635
636 return device;
637 }
638
639 private:
640 TestableSurfaceFlinger& mFlinger;
641 sp<BBinder> mDisplayToken = new BBinder();
Lloyd Pique2eef1d22018-09-18 21:30:04 -0700642 DisplayDeviceCreationArgs mCreationArgs;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800643 };
644
Lloyd Pique90c115d2018-09-18 21:39:42 -0700645 surfaceflinger::test::Factory mFactory;
646 sp<SurfaceFlinger> mFlinger = new SurfaceFlinger(mFactory, SurfaceFlinger::SkipInitialization);
Dominik Laskowski7c9dbf92019-08-01 17:57:31 -0700647 TestableScheduler* mScheduler = nullptr;
Lloyd Piquef58625d2017-12-19 13:22:33 -0800648};
649
650} // namespace android