blob: 6995ee00977810c05aae1d0052a40821bc3a96c1 [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
60namespace surfaceflinger::test {
61
62class Factory final : public surfaceflinger::Factory {
63public:
64 ~Factory() = default;
65
Dominik Laskowski98041832019-08-01 18:35:59 -070066 std::unique_ptr<DispSync> createDispSync(const char*, bool) override {
Lloyd Pique90c115d2018-09-18 21:39:42 -070067 return nullptr;
68 }
69
70 std::unique_ptr<EventControlThread> createEventControlThread(
71 std::function<void(bool)>) override {
Lloyd Pique90c115d2018-09-18 21:39:42 -070072 return nullptr;
73 }
74
75 std::unique_ptr<HWComposer> createHWComposer(const std::string&) override {
Lloyd Pique90c115d2018-09-18 21:39:42 -070076 return nullptr;
77 }
78
79 std::unique_ptr<MessageQueue> createMessageQueue() override {
Lloyd Pique90c115d2018-09-18 21:39:42 -070080 return std::make_unique<android::impl::MessageQueue>();
81 }
82
Ady Abraham9e16a482019-12-03 17:19:41 -080083 std::unique_ptr<scheduler::PhaseConfiguration> createPhaseConfiguration(
84 const scheduler::RefreshRateConfigs& /*refreshRateConfigs*/) override {
Ana Krulec757f63a2019-01-25 10:46:18 -080085 return std::make_unique<scheduler::FakePhaseOffsets>();
86 }
87
Ady Abraham09bd3922019-04-08 10:44:56 -070088 std::unique_ptr<Scheduler> createScheduler(std::function<void(bool)>,
Ady Abraham3a77a7b2019-12-02 18:46:59 -080089 const scheduler::RefreshRateConfigs&,
90 ISchedulerCallback&) override {
Lloyd Pique90c115d2018-09-18 21:39:42 -070091 return nullptr;
92 }
93
94 std::unique_ptr<SurfaceInterceptor> createSurfaceInterceptor(SurfaceFlinger* flinger) override {
Lloyd Pique90c115d2018-09-18 21:39:42 -070095 return std::make_unique<android::impl::SurfaceInterceptor>(flinger);
96 }
97
98 sp<StartPropertySetThread> createStartPropertySetThread(bool timestampPropertyValue) override {
Lloyd Pique90c115d2018-09-18 21:39:42 -070099 return new StartPropertySetThread(timestampPropertyValue);
100 }
101
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -0700102 sp<DisplayDevice> createDisplayDevice(DisplayDeviceCreationArgs& creationArgs) override {
103 return new DisplayDevice(creationArgs);
Lloyd Pique90c115d2018-09-18 21:39:42 -0700104 }
105
106 sp<GraphicBuffer> createGraphicBuffer(uint32_t width, uint32_t height, PixelFormat format,
107 uint32_t layerCount, uint64_t usage,
108 std::string requestorName) override {
Lloyd Pique90c115d2018-09-18 21:39:42 -0700109 return new GraphicBuffer(width, height, format, layerCount, usage, requestorName);
110 }
111
112 void createBufferQueue(sp<IGraphicBufferProducer>* outProducer,
113 sp<IGraphicBufferConsumer>* outConsumer,
114 bool consumerIsSurfaceFlinger) override {
Lloyd Piquee300b312019-10-03 13:03:45 -0700115 if (!mCreateBufferQueue) {
116 BufferQueue::createBufferQueue(outProducer, outConsumer, consumerIsSurfaceFlinger);
117 return;
118 }
Lloyd Pique90c115d2018-09-18 21:39:42 -0700119 mCreateBufferQueue(outProducer, outConsumer, consumerIsSurfaceFlinger);
120 }
121
Lloyd Piquee300b312019-10-03 13:03:45 -0700122 sp<IGraphicBufferProducer> createMonitoredProducer(const sp<IGraphicBufferProducer>& producer,
123 const sp<SurfaceFlinger>& flinger,
124 const wp<Layer>& layer) override {
125 return new MonitoredProducer(producer, flinger, layer);
126 }
127
128 sp<BufferLayerConsumer> createBufferLayerConsumer(const sp<IGraphicBufferConsumer>& consumer,
129 renderengine::RenderEngine& renderEngine,
130 uint32_t textureName, Layer* layer) override {
131 return new BufferLayerConsumer(consumer, renderEngine, textureName, layer);
132 }
133
Lloyd Pique90c115d2018-09-18 21:39:42 -0700134 std::unique_ptr<surfaceflinger::NativeWindowSurface> createNativeWindowSurface(
135 const sp<IGraphicBufferProducer>& producer) override {
136 if (!mCreateNativeWindowSurface) return nullptr;
137 return mCreateNativeWindowSurface(producer);
138 }
139
Lloyd Pique70d91362018-10-18 16:02:55 -0700140 std::unique_ptr<compositionengine::CompositionEngine> createCompositionEngine() override {
141 return compositionengine::impl::createCompositionEngine();
142 }
143
Lloyd Pique90c115d2018-09-18 21:39:42 -0700144 sp<BufferQueueLayer> createBufferQueueLayer(const LayerCreationArgs&) override {
Lloyd Pique90c115d2018-09-18 21:39:42 -0700145 return nullptr;
146 }
147
148 sp<BufferStateLayer> createBufferStateLayer(const LayerCreationArgs&) override {
Lloyd Pique90c115d2018-09-18 21:39:42 -0700149 return nullptr;
150 }
151
Vishnu Nairfa247b12020-02-11 08:58:26 -0800152 sp<EffectLayer> createEffectLayer(const LayerCreationArgs&) override { return nullptr; }
Lloyd Pique90c115d2018-09-18 21:39:42 -0700153
154 sp<ContainerLayer> createContainerLayer(const LayerCreationArgs&) override {
Lloyd Pique90c115d2018-09-18 21:39:42 -0700155 return nullptr;
156 }
157
158 using CreateBufferQueueFunction =
159 std::function<void(sp<IGraphicBufferProducer>* /* outProducer */,
160 sp<IGraphicBufferConsumer>* /* outConsumer */,
161 bool /* consumerIsSurfaceFlinger */)>;
162 CreateBufferQueueFunction mCreateBufferQueue;
163
164 using CreateNativeWindowSurfaceFunction =
165 std::function<std::unique_ptr<surfaceflinger::NativeWindowSurface>(
166 const sp<IGraphicBufferProducer>&)>;
167 CreateNativeWindowSurfaceFunction mCreateNativeWindowSurface;
Lloyd Pique70d91362018-10-18 16:02:55 -0700168
169 using CreateCompositionEngineFunction =
170 std::function<std::unique_ptr<compositionengine::CompositionEngine>()>;
171 CreateCompositionEngineFunction mCreateCompositionEngine;
Lloyd Pique90c115d2018-09-18 21:39:42 -0700172};
173
174} // namespace surfaceflinger::test
Lloyd Piquee39cad22017-12-20 17:01:29 -0800175
Lloyd Piquef58625d2017-12-19 13:22:33 -0800176class TestableSurfaceFlinger {
177public:
Dominik Laskowskif7a09ed2019-10-07 13:54:18 -0700178 SurfaceFlinger* flinger() { return mFlinger.get(); }
Dominik Laskowski7c9dbf92019-08-01 17:57:31 -0700179 TestableScheduler* scheduler() { return mScheduler; }
180
Lloyd Piquef58625d2017-12-19 13:22:33 -0800181 // Extend this as needed for accessing SurfaceFlinger private (and public)
182 // functions.
183
Peiyong Lin833074a2018-08-28 11:53:54 -0700184 void setupRenderEngine(std::unique_ptr<renderengine::RenderEngine> renderEngine) {
Lloyd Piqueb97e04f2018-10-18 17:07:05 -0700185 mFlinger->mCompositionEngine->setRenderEngine(std::move(renderEngine));
Lloyd Piquee39cad22017-12-20 17:01:29 -0800186 }
187
188 void setupComposer(std::unique_ptr<Hwc2::Composer> composer) {
Lloyd Pique441d5042018-10-18 16:49:51 -0700189 mFlinger->mCompositionEngine->setHwComposer(
190 std::make_unique<impl::HWComposer>(std::move(composer)));
Lloyd Piquee39cad22017-12-20 17:01:29 -0800191 }
192
Alec Mourie4034bb2019-11-19 12:45:54 -0800193 void setupTimeStats(const std::shared_ptr<TimeStats>& timeStats) {
194 mFlinger->mCompositionEngine->setTimeStats(timeStats);
195 }
196
Dominik Laskowski7c9dbf92019-08-01 17:57:31 -0700197 void setupScheduler(std::unique_ptr<DispSync> primaryDispSync,
198 std::unique_ptr<EventControlThread> eventControlThread,
199 std::unique_ptr<EventThread> appEventThread,
Ady Abraham8a82ba62020-01-17 12:43:17 -0800200 std::unique_ptr<EventThread> sfEventThread,
201 bool useContentDetectionV2 = false) {
Ady Abrahamabc27602020-04-08 17:20:29 -0700202 std::vector<std::shared_ptr<const HWC2::Display::Config>> configs{
203 HWC2::Display::Config::Builder(mDisplay, 0)
204 .setVsyncPeriod(int32_t(16666667))
205 .setConfigGroup(0)
206 .build()};
207
Ady Abraham2139f732019-11-13 18:56:40 -0800208 mFlinger->mRefreshRateConfigs = std::make_unique<
Ana Krulec3f6a2062020-01-23 15:48:01 -0800209 scheduler::RefreshRateConfigs>(configs, /*currentConfig=*/HwcConfigIndexType(0));
Ady Abraham2139f732019-11-13 18:56:40 -0800210 mFlinger->mRefreshRateStats = std::make_unique<
211 scheduler::RefreshRateStats>(*mFlinger->mRefreshRateConfigs, *mFlinger->mTimeStats,
212 /*currentConfig=*/HwcConfigIndexType(0),
213 /*powerMode=*/HWC_POWER_MODE_OFF);
Ady Abraham9e16a482019-12-03 17:19:41 -0800214 mFlinger->mPhaseConfiguration =
215 mFactory.createPhaseConfiguration(*mFlinger->mRefreshRateConfigs);
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700216
Dominik Laskowski7c9dbf92019-08-01 17:57:31 -0700217 mScheduler =
218 new TestableScheduler(std::move(primaryDispSync), std::move(eventControlThread),
Ady Abraham8a82ba62020-01-17 12:43:17 -0800219 *mFlinger->mRefreshRateConfigs, useContentDetectionV2);
Dominik Laskowski7c9dbf92019-08-01 17:57:31 -0700220
Dominik Laskowski98041832019-08-01 18:35:59 -0700221 mFlinger->mAppConnectionHandle = mScheduler->createConnection(std::move(appEventThread));
222 mFlinger->mSfConnectionHandle = mScheduler->createConnection(std::move(sfEventThread));
Dominik Laskowskif7a09ed2019-10-07 13:54:18 -0700223 resetScheduler(mScheduler);
Dominik Laskowski7c9dbf92019-08-01 17:57:31 -0700224
Dominik Laskowski7c9dbf92019-08-01 17:57:31 -0700225 mFlinger->mVSyncModulator.emplace(*mScheduler, mFlinger->mAppConnectionHandle,
226 mFlinger->mSfConnectionHandle,
Ady Abraham9e16a482019-12-03 17:19:41 -0800227 mFlinger->mPhaseConfiguration->getCurrentOffsets());
Dominik Laskowski7c9dbf92019-08-01 17:57:31 -0700228 }
229
Dominik Laskowskif7a09ed2019-10-07 13:54:18 -0700230 void resetScheduler(Scheduler* scheduler) { mFlinger->mScheduler.reset(scheduler); }
231
Lloyd Pique90c115d2018-09-18 21:39:42 -0700232 using CreateBufferQueueFunction = surfaceflinger::test::Factory::CreateBufferQueueFunction;
Lloyd Pique5b36f3f2018-01-17 11:57:07 -0800233 void setCreateBufferQueueFunction(CreateBufferQueueFunction f) {
Lloyd Pique90c115d2018-09-18 21:39:42 -0700234 mFactory.mCreateBufferQueue = f;
Lloyd Pique5b36f3f2018-01-17 11:57:07 -0800235 }
236
Lloyd Pique90c115d2018-09-18 21:39:42 -0700237 using CreateNativeWindowSurfaceFunction =
238 surfaceflinger::test::Factory::CreateNativeWindowSurfaceFunction;
Lloyd Pique0b1fe702018-01-22 18:03:16 -0800239 void setCreateNativeWindowSurface(CreateNativeWindowSurfaceFunction f) {
Lloyd Pique90c115d2018-09-18 21:39:42 -0700240 mFactory.mCreateNativeWindowSurface = f;
Lloyd Pique0b1fe702018-01-22 18:03:16 -0800241 }
242
Daniel Solomon42d04562019-01-20 21:03:19 -0800243 void setInternalDisplayPrimaries(const ui::DisplayPrimaries& primaries) {
244 memcpy(&mFlinger->mInternalDisplayPrimaries, &primaries, sizeof(ui::DisplayPrimaries));
245 }
246
Lloyd Pique0b1fe702018-01-22 18:03:16 -0800247 using HotplugEvent = SurfaceFlinger::HotplugEvent;
248
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800249 auto& mutableLayerCurrentState(sp<Layer> layer) { return layer->mCurrentState; }
250 auto& mutableLayerDrawingState(sp<Layer> layer) { return layer->mDrawingState; }
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700251
Valerie Haud251afb2019-03-29 14:19:02 -0700252 auto& mutableStateLock() { return mFlinger->mStateLock; }
253
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700254 void setLayerSidebandStream(sp<Layer> layer, sp<NativeHandle> sidebandStream) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800255 layer->mDrawingState.sidebandStream = sidebandStream;
Lloyd Pique0b785d82018-12-04 17:25:27 -0800256 layer->mSidebandStream = sidebandStream;
Lloyd Piquede196652020-01-22 17:29:58 -0800257 layer->editCompositionState()->sidebandStream = sidebandStream;
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700258 }
259
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800260 void setLayerCompositionType(sp<Layer> layer, HWC2::Composition type) {
261 auto outputLayer = layer->findOutputLayerForDisplay(mFlinger->getDefaultDisplayDevice());
262 LOG_ALWAYS_FATAL_IF(!outputLayer);
263 auto& state = outputLayer->editState();
264 LOG_ALWAYS_FATAL_IF(!outputLayer->getState().hwc);
265 (*state.hwc).hwcCompositionType = static_cast<Hwc2::IComposerClient::Composition>(type);
266 };
267
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700268 void setLayerPotentialCursor(sp<Layer> layer, bool potentialCursor) {
269 layer->mPotentialCursor = potentialCursor;
270 }
271
Lloyd Piquef58625d2017-12-19 13:22:33 -0800272 /* ------------------------------------------------------------------------
273 * Forwarding for functions being tested
274 */
Lloyd Pique0b1fe702018-01-22 18:03:16 -0800275
Lloyd Piquea482f992018-01-22 19:00:34 -0800276 auto createDisplay(const String8& displayName, bool secure) {
277 return mFlinger->createDisplay(displayName, secure);
278 }
279
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700280 auto destroyDisplay(const sp<IBinder>& displayToken) {
281 return mFlinger->destroyDisplay(displayToken);
282 }
Lloyd Piquea482f992018-01-22 19:00:34 -0800283
Lloyd Piqued6fbb8a2018-01-22 19:08:36 -0800284 auto resetDisplayState() { return mFlinger->resetDisplayState(); }
285
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -0700286 auto setupNewDisplayDeviceInternal(
287 const wp<IBinder>& displayToken,
288 std::shared_ptr<compositionengine::Display> compositionDisplay,
289 const DisplayDeviceState& state,
290 const sp<compositionengine::DisplaySurface>& dispSurface,
291 const sp<IGraphicBufferProducer>& producer) {
292 return mFlinger->setupNewDisplayDeviceInternal(displayToken, compositionDisplay, state,
293 dispSurface, producer);
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800294 }
295
Lloyd Pique0b1fe702018-01-22 18:03:16 -0800296 auto handleTransactionLocked(uint32_t transactionFlags) {
Vishnu Nairfa5f8df2019-02-13 17:02:51 -0800297 Mutex::Autolock _l(mFlinger->mStateLock);
Lloyd Pique0b1fe702018-01-22 18:03:16 -0800298 return mFlinger->handleTransactionLocked(transactionFlags);
299 }
Lloyd Piquef58625d2017-12-19 13:22:33 -0800300
Lloyd Pique6cf11032018-01-22 18:57:44 -0800301 auto onHotplugReceived(int32_t sequenceId, hwc2_display_t display,
302 HWC2::Connection connection) {
303 return mFlinger->onHotplugReceived(sequenceId, display, connection);
304 }
305
Vishnu Nairce5d0cc2019-02-28 14:38:41 -0800306 auto setDisplayStateLocked(const DisplayState& s) {
307 Mutex::Autolock _l(mFlinger->mStateLock);
308 return mFlinger->setDisplayStateLocked(s);
309 }
Lloyd Pique9d9cf402018-02-16 17:47:13 -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 onInitializeDisplays() NO_THREAD_SAFETY_ANALYSIS {
313 return mFlinger->onInitializeDisplays();
314 }
Lloyd Pique86016da2018-03-01 16:09:38 -0800315
Dominik Laskowski83b88212018-12-11 13:34:06 -0800316 // Allow reading display state without locking, as if called on the SF main thread.
317 auto setPowerModeInternal(const sp<DisplayDevice>& display,
318 int mode) NO_THREAD_SAFETY_ANALYSIS {
Dominik Laskowskie9774092018-12-11 10:04:24 -0800319 return mFlinger->setPowerModeInternal(display, mode);
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -0800320 }
321
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700322 auto onMessageReceived(int32_t what) { return mFlinger->onMessageReceived(what); }
323
Robert Carr108b2c72019-04-02 16:32:58 -0700324 auto captureScreenImplLocked(
325 const RenderArea& renderArea, SurfaceFlinger::TraverseLayersFunction traverseLayers,
326 ANativeWindowBuffer* buffer, bool useIdentityTransform, bool forSystem, int* outSyncFd) {
327 bool ignored;
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700328 return mFlinger->captureScreenImplLocked(renderArea, traverseLayers, buffer,
Robert Carr108b2c72019-04-02 16:32:58 -0700329 useIdentityTransform, forSystem, outSyncFd,
330 ignored);
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700331 }
332
chaviw0e3479f2018-09-10 16:49:30 -0700333 auto traverseLayersInDisplay(const sp<const DisplayDevice>& display,
334 const LayerVector::Visitor& visitor) {
335 return mFlinger->SurfaceFlinger::traverseLayersInDisplay(display, visitor);
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700336 }
337
Daniel Solomon42d04562019-01-20 21:03:19 -0800338 auto getDisplayNativePrimaries(const sp<IBinder>& displayToken,
339 ui::DisplayPrimaries &primaries) {
340 return mFlinger->SurfaceFlinger::getDisplayNativePrimaries(displayToken, primaries);
341 }
342
Valerie Haud251afb2019-03-29 14:19:02 -0700343 auto& getTransactionQueue() { return mFlinger->mTransactionQueues; }
344
345 auto setTransactionState(const Vector<ComposerState>& states,
346 const Vector<DisplayState>& displays, uint32_t flags,
347 const sp<IBinder>& applyToken,
348 const InputWindowCommands& inputWindowCommands,
349 int64_t desiredPresentTime, const client_cache_t& uncacheBuffer,
350 bool hasListenerCallbacks,
351 std::vector<ListenerCallbacks>& listenerCallbacks) {
352 return mFlinger->setTransactionState(states, displays, flags, applyToken,
353 inputWindowCommands, desiredPresentTime, uncacheBuffer,
354 hasListenerCallbacks, listenerCallbacks);
355 }
356
357 auto flushTransactionQueues() { return mFlinger->flushTransactionQueues(); };
358
Lloyd Pique86016da2018-03-01 16:09:38 -0800359 /* ------------------------------------------------------------------------
360 * Read-only access to private data to assert post-conditions.
361 */
362
363 const auto& getAnimFrameTracker() const { return mFlinger->mAnimFrameTracker; }
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -0800364 const auto& getHasPoweredOff() const { return mFlinger->mHasPoweredOff; }
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -0800365 const auto& getVisibleRegionsDirty() const { return mFlinger->mVisibleRegionsDirty; }
Lloyd Pique441d5042018-10-18 16:49:51 -0700366 auto& getHwComposer() const {
367 return static_cast<impl::HWComposer&>(mFlinger->getHwComposer());
368 }
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -0700369 auto& getCompositionEngine() const { return mFlinger->getCompositionEngine(); }
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -0800370
Lloyd Pique86016da2018-03-01 16:09:38 -0800371 const auto& getCompositorTiming() const { return mFlinger->getBE().mCompositorTiming; }
372
Lloyd Piquef58625d2017-12-19 13:22:33 -0800373 /* ------------------------------------------------------------------------
374 * Read-write access to private data to set up preconditions and assert
375 * post-conditions.
376 */
Lloyd Pique0b1fe702018-01-22 18:03:16 -0800377
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800378 auto& mutableHasWideColorDisplay() { return SurfaceFlinger::hasWideColorDisplay; }
Peiyong Lin13effd12018-07-24 17:01:47 -0700379 auto& mutableUseColorManagement() { return SurfaceFlinger::useColorManagement; }
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800380
Lloyd Piquee39cad22017-12-20 17:01:29 -0800381 auto& mutableCurrentState() { return mFlinger->mCurrentState; }
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800382 auto& mutableDisplayColorSetting() { return mFlinger->mDisplayColorSetting; }
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700383 auto& mutableDisplays() { return mFlinger->mDisplays; }
Lloyd Piquee39cad22017-12-20 17:01:29 -0800384 auto& mutableDrawingState() { return mFlinger->mDrawingState; }
Lloyd Piquee39cad22017-12-20 17:01:29 -0800385 auto& mutableEventQueue() { return mFlinger->mEventQueue; }
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700386 auto& mutableGeometryInvalid() { return mFlinger->mGeometryInvalid; }
Lloyd Pique0b1fe702018-01-22 18:03:16 -0800387 auto& mutableInterceptor() { return mFlinger->mInterceptor; }
Lloyd Pique6cf11032018-01-22 18:57:44 -0800388 auto& mutableMainThreadId() { return mFlinger->mMainThreadId; }
Lloyd Pique0b1fe702018-01-22 18:03:16 -0800389 auto& mutablePendingHotplugEvents() { return mFlinger->mPendingHotplugEvents; }
Dominik Laskowski075d3172018-05-24 15:50:06 -0700390 auto& mutablePhysicalDisplayTokens() { return mFlinger->mPhysicalDisplayTokens; }
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700391 auto& mutableTexturePool() { return mFlinger->mTexturePool; }
Lloyd Pique0b1fe702018-01-22 18:03:16 -0800392 auto& mutableTransactionFlags() { return mFlinger->mTransactionFlags; }
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800393 auto& mutableUseHwcVirtualDisplays() { return mFlinger->mUseHwcVirtualDisplays; }
Peiyong Lin74ca2f42019-01-14 19:36:57 -0800394 auto& mutablePowerAdvisor() { return mFlinger->mPowerAdvisor; }
Lloyd Pique4fe29402019-08-12 16:51:24 -0700395 auto& mutableDebugDisableHWC() { return mFlinger->mDebugDisableHWC; }
Lloyd Piquee39cad22017-12-20 17:01:29 -0800396
Lloyd Pique6cf11032018-01-22 18:57:44 -0800397 auto& mutableComposerSequenceId() { return mFlinger->getBE().mComposerSequenceId; }
Lloyd Pique441d5042018-10-18 16:49:51 -0700398 auto& mutableHwcDisplayData() { return getHwComposer().mDisplayData; }
399 auto& mutableHwcPhysicalDisplayIdMap() { return getHwComposer().mPhysicalDisplayIdMap; }
400 auto& mutableInternalHwcDisplayId() { return getHwComposer().mInternalHwcDisplayId; }
401 auto& mutableExternalHwcDisplayId() { return getHwComposer().mExternalHwcDisplayId; }
Ady Abraham60e42ea2020-03-09 19:17:31 -0700402 auto& mutableUseFrameRateApi() { return mFlinger->useFrameRateApi; }
Lloyd Piquea618d852018-01-17 11:52:30 -0800403
Valerie Hau09e60052019-12-15 14:51:15 -0800404 auto fromHandle(const sp<IBinder>& handle) {
405 Mutex::Autolock _l(mFlinger->mStateLock);
406 return mFlinger->fromHandle(handle);
407 }
408
Lloyd Piquee39cad22017-12-20 17:01:29 -0800409 ~TestableSurfaceFlinger() {
410 // All these pointer and container clears help ensure that GMock does
411 // not report a leaked object, since the SurfaceFlinger instance may
412 // still be referenced by something despite our best efforts to destroy
413 // it after each test is done.
414 mutableDisplays().clear();
Ady Abraham2939f092019-03-04 17:44:06 -0800415 mutableCurrentState().displays.clear();
416 mutableDrawingState().displays.clear();
Lloyd Pique0b1fe702018-01-22 18:03:16 -0800417 mutableEventQueue().reset();
Lloyd Pique0b1fe702018-01-22 18:03:16 -0800418 mutableInterceptor().reset();
Lloyd Pique2eb71eb2019-08-13 13:23:03 -0700419 mFlinger->mScheduler.reset();
Lloyd Pique441d5042018-10-18 16:49:51 -0700420 mFlinger->mCompositionEngine->setHwComposer(std::unique_ptr<HWComposer>());
Lloyd Piqueb97e04f2018-10-18 17:07:05 -0700421 mFlinger->mCompositionEngine->setRenderEngine(
422 std::unique_ptr<renderengine::RenderEngine>());
Lloyd Piquee39cad22017-12-20 17:01:29 -0800423 }
Lloyd Piquef58625d2017-12-19 13:22:33 -0800424
Lloyd Piquea618d852018-01-17 11:52:30 -0800425 /* ------------------------------------------------------------------------
426 * Wrapper classes for Read-write access to private data to set up
427 * preconditions and assert post-conditions.
428 */
Ana Krulec4593b692019-01-11 22:07:25 -0800429 struct HWC2Display : public HWC2::impl::Display {
Peiyong Lin74ca2f42019-01-14 19:36:57 -0800430 HWC2Display(Hwc2::Composer& composer,
Lloyd Piquea618d852018-01-17 11:52:30 -0800431 const std::unordered_set<HWC2::Capability>& capabilities, hwc2_display_t id,
432 HWC2::DisplayType type)
Peiyong Lin74ca2f42019-01-14 19:36:57 -0800433 : HWC2::impl::Display(composer, capabilities, id, type) {}
Lloyd Piquea618d852018-01-17 11:52:30 -0800434 ~HWC2Display() {
435 // Prevents a call to disable vsyncs.
436 mType = HWC2::DisplayType::Invalid;
437 }
438
439 auto& mutableIsConnected() { return this->mIsConnected; }
440 auto& mutableConfigs() { return this->mConfigs; }
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700441 auto& mutableLayers() { return this->mLayers; }
Lloyd Piquea618d852018-01-17 11:52:30 -0800442 };
443
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800444 class FakeHwcDisplayInjector {
445 public:
446 static constexpr hwc2_display_t DEFAULT_HWC_DISPLAY_ID = 1000;
447 static constexpr int32_t DEFAULT_WIDTH = 1920;
448 static constexpr int32_t DEFAULT_HEIGHT = 1280;
449 static constexpr int32_t DEFAULT_REFRESH_RATE = 16'666'666;
Ady Abraham2139f732019-11-13 18:56:40 -0800450 static constexpr int32_t DEFAULT_CONFIG_GROUP = 7;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800451 static constexpr int32_t DEFAULT_DPI = 320;
Ady Abraham8a82ba62020-01-17 12:43:17 -0800452 static constexpr hwc2_config_t DEFAULT_ACTIVE_CONFIG = 0;
Peiyong Lin1336e6e2019-05-28 09:23:50 -0700453 static constexpr int32_t DEFAULT_POWER_MODE = 2;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800454
Dominik Laskowski075d3172018-05-24 15:50:06 -0700455 FakeHwcDisplayInjector(DisplayId displayId, HWC2::DisplayType hwcDisplayType,
456 bool isPrimary)
457 : mDisplayId(displayId), mHwcDisplayType(hwcDisplayType), mIsPrimary(isPrimary) {}
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800458
459 auto& setHwcDisplayId(hwc2_display_t displayId) {
460 mHwcDisplayId = displayId;
461 return *this;
462 }
463
464 auto& setWidth(int32_t width) {
465 mWidth = width;
466 return *this;
467 }
468
469 auto& setHeight(int32_t height) {
470 mHeight = height;
471 return *this;
472 }
473
Ady Abraham8a82ba62020-01-17 12:43:17 -0800474 auto& setRefreshRate(int32_t refreshRate) {
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800475 mRefreshRate = refreshRate;
476 return *this;
477 }
478
479 auto& setDpiX(int32_t dpi) {
480 mDpiX = dpi;
481 return *this;
482 }
483
484 auto& setDpiY(int32_t dpi) {
485 mDpiY = dpi;
486 return *this;
487 }
488
Ady Abraham8a82ba62020-01-17 12:43:17 -0800489 auto& setActiveConfig(hwc2_config_t config) {
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800490 mActiveConfig = config;
491 return *this;
492 }
493
Lloyd Piquee22f0332018-07-16 16:35:56 -0700494 auto& setCapabilities(const std::unordered_set<HWC2::Capability>* capabilities) {
495 mCapabilities = capabilities;
496 return *this;
497 }
498
Peiyong Lin1336e6e2019-05-28 09:23:50 -0700499 auto& setPowerMode(int mode) {
500 mPowerMode = mode;
501 return *this;
502 }
503
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800504 void inject(TestableSurfaceFlinger* flinger, Hwc2::Composer* composer) {
Lloyd Piquee22f0332018-07-16 16:35:56 -0700505 static const std::unordered_set<HWC2::Capability> defaultCapabilities;
506 if (mCapabilities == nullptr) mCapabilities = &defaultCapabilities;
507
508 // Caution - Make sure that any values passed by reference here do
509 // not refer to an instance owned by FakeHwcDisplayInjector. This
510 // class has temporary lifetime, while the constructed HWC2::Display
511 // is much longer lived.
Peiyong Lin74ca2f42019-01-14 19:36:57 -0800512 auto display = std::make_unique<HWC2Display>(*composer, *mCapabilities, mHwcDisplayId,
513 mHwcDisplayType);
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800514
515 auto config = HWC2::Display::Config::Builder(*display, mActiveConfig);
516 config.setWidth(mWidth);
517 config.setHeight(mHeight);
518 config.setVsyncPeriod(mRefreshRate);
519 config.setDpiX(mDpiX);
520 config.setDpiY(mDpiY);
Ady Abraham2139f732019-11-13 18:56:40 -0800521 config.setConfigGroup(mConfigGroup);
Ady Abraham8a82ba62020-01-17 12:43:17 -0800522 display->mutableConfigs().emplace(static_cast<int32_t>(mActiveConfig), config.build());
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800523 display->mutableIsConnected() = true;
Peiyong Lin1336e6e2019-05-28 09:23:50 -0700524 display->setPowerMode(static_cast<HWC2::PowerMode>(mPowerMode));
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800525
Peiyong Linbdd08cc2019-12-17 21:35:14 -0800526 flinger->mutableHwcDisplayData()[mDisplayId].hwcDisplay = std::move(display);
Dominik Laskowski075d3172018-05-24 15:50:06 -0700527
528 if (mHwcDisplayType == HWC2::DisplayType::Physical) {
529 flinger->mutableHwcPhysicalDisplayIdMap().emplace(mHwcDisplayId, mDisplayId);
530 (mIsPrimary ? flinger->mutableInternalHwcDisplayId()
531 : flinger->mutableExternalHwcDisplayId()) = mHwcDisplayId;
532 }
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800533 }
534
535 private:
Dominik Laskowski075d3172018-05-24 15:50:06 -0700536 const DisplayId mDisplayId;
537 const HWC2::DisplayType mHwcDisplayType;
538 const bool mIsPrimary;
539
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800540 hwc2_display_t mHwcDisplayId = DEFAULT_HWC_DISPLAY_ID;
541 int32_t mWidth = DEFAULT_WIDTH;
542 int32_t mHeight = DEFAULT_HEIGHT;
Ady Abraham8a82ba62020-01-17 12:43:17 -0800543 int32_t mRefreshRate = DEFAULT_REFRESH_RATE;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800544 int32_t mDpiX = DEFAULT_DPI;
Ady Abraham2139f732019-11-13 18:56:40 -0800545 int32_t mConfigGroup = DEFAULT_CONFIG_GROUP;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800546 int32_t mDpiY = DEFAULT_DPI;
Ady Abraham8a82ba62020-01-17 12:43:17 -0800547 hwc2_config_t mActiveConfig = DEFAULT_ACTIVE_CONFIG;
Peiyong Lin1336e6e2019-05-28 09:23:50 -0700548 int32_t mPowerMode = DEFAULT_POWER_MODE;
Lloyd Piquee22f0332018-07-16 16:35:56 -0700549 const std::unordered_set<HWC2::Capability>* mCapabilities = nullptr;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800550 };
551
552 class FakeDisplayDeviceInjector {
553 public:
Dominik Laskowski075d3172018-05-24 15:50:06 -0700554 FakeDisplayDeviceInjector(TestableSurfaceFlinger& flinger,
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -0700555 std::shared_ptr<compositionengine::Display> compositionDisplay,
Dominik Laskowski55c85402020-01-21 16:25:47 -0800556 std::optional<DisplayConnectionType> connectionType,
Marin Shalamanov4a42d432020-02-12 20:22:26 +0100557 std::optional<hwc2_display_t> hwcDisplayId, bool isPrimary)
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -0700558 : mFlinger(flinger),
Marin Shalamanov4a42d432020-02-12 20:22:26 +0100559 mCreationArgs(flinger.mFlinger.get(), mDisplayToken, compositionDisplay),
560 mHwcDisplayId(hwcDisplayId) {
Dominik Laskowski55c85402020-01-21 16:25:47 -0800561 mCreationArgs.connectionType = connectionType;
Dominik Laskowski075d3172018-05-24 15:50:06 -0700562 mCreationArgs.isPrimary = isPrimary;
563 }
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800564
565 sp<IBinder> token() const { return mDisplayToken; }
566
567 DisplayDeviceState& mutableDrawingDisplayState() {
568 return mFlinger.mutableDrawingState().displays.editValueFor(mDisplayToken);
569 }
570
571 DisplayDeviceState& mutableCurrentDisplayState() {
572 return mFlinger.mutableCurrentState().displays.editValueFor(mDisplayToken);
573 }
574
Lloyd Pique9d9cf402018-02-16 17:47:13 -0800575 const auto& getDrawingDisplayState() {
576 return mFlinger.mutableDrawingState().displays.valueFor(mDisplayToken);
577 }
578
579 const auto& getCurrentDisplayState() {
580 return mFlinger.mutableCurrentState().displays.valueFor(mDisplayToken);
581 }
582
Dominik Laskowski9fae1022018-05-29 13:17:40 -0700583 auto& mutableDisplayDevice() { return mFlinger.mutableDisplays()[mDisplayToken]; }
Lloyd Pique9d9cf402018-02-16 17:47:13 -0800584
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800585 auto& setNativeWindow(const sp<ANativeWindow>& nativeWindow) {
Lloyd Pique2eef1d22018-09-18 21:30:04 -0700586 mCreationArgs.nativeWindow = nativeWindow;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800587 return *this;
588 }
589
Lloyd Pique542307f2018-10-19 13:24:08 -0700590 auto& setDisplaySurface(const sp<compositionengine::DisplaySurface>& displaySurface) {
Lloyd Pique2eef1d22018-09-18 21:30:04 -0700591 mCreationArgs.displaySurface = displaySurface;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800592 return *this;
593 }
594
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800595 auto& setSecure(bool secure) {
Lloyd Pique2eef1d22018-09-18 21:30:04 -0700596 mCreationArgs.isSecure = secure;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800597 return *this;
598 }
599
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700600 auto& setPowerMode(int mode) {
Lloyd Pique2eef1d22018-09-18 21:30:04 -0700601 mCreationArgs.initialPowerMode = mode;
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700602 return *this;
603 }
604
Valerie Hau9758ae02018-10-09 16:05:09 -0700605 auto& setHwcColorModes(
606 const std::unordered_map<ui::ColorMode, std::vector<ui::RenderIntent>>
607 hwcColorModes) {
608 mCreationArgs.hwcColorModes = hwcColorModes;
609 return *this;
610 }
611
612 auto& setHasWideColorGamut(bool hasWideColorGamut) {
613 mCreationArgs.hasWideColorGamut = hasWideColorGamut;
614 return *this;
615 }
616
Lloyd Pique33050472019-12-19 17:12:44 -0800617 auto& setPhysicalOrientation(ui::Rotation orientation) {
618 mCreationArgs.physicalOrientation = orientation;
619 return *this;
620 }
621
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800622 sp<DisplayDevice> inject() {
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -0700623 const auto displayId = mCreationArgs.compositionDisplay->getDisplayId();
624
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800625 DisplayDeviceState state;
Dominik Laskowski55c85402020-01-21 16:25:47 -0800626 if (const auto type = mCreationArgs.connectionType) {
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -0700627 LOG_ALWAYS_FATAL_IF(!displayId);
Marin Shalamanov4a42d432020-02-12 20:22:26 +0100628 LOG_ALWAYS_FATAL_IF(!mHwcDisplayId);
629 state.physical = {*displayId, *type, *mHwcDisplayId};
Dominik Laskowski55c85402020-01-21 16:25:47 -0800630 }
631
Lloyd Pique2eef1d22018-09-18 21:30:04 -0700632 state.isSecure = mCreationArgs.isSecure;
633
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -0700634 sp<DisplayDevice> device = new DisplayDevice(mCreationArgs);
Lloyd Pique2eef1d22018-09-18 21:30:04 -0700635 mFlinger.mutableDisplays().emplace(mDisplayToken, device);
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800636 mFlinger.mutableCurrentState().displays.add(mDisplayToken, state);
637 mFlinger.mutableDrawingState().displays.add(mDisplayToken, state);
638
Dominik Laskowski55c85402020-01-21 16:25:47 -0800639 if (const auto& physical = state.physical) {
640 mFlinger.mutablePhysicalDisplayTokens()[physical->id] = mDisplayToken;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800641 }
642
643 return device;
644 }
645
646 private:
647 TestableSurfaceFlinger& mFlinger;
648 sp<BBinder> mDisplayToken = new BBinder();
Lloyd Pique2eef1d22018-09-18 21:30:04 -0700649 DisplayDeviceCreationArgs mCreationArgs;
Marin Shalamanov4a42d432020-02-12 20:22:26 +0100650 const std::optional<hwc2_display_t> mHwcDisplayId;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800651 };
652
Lloyd Pique90c115d2018-09-18 21:39:42 -0700653 surfaceflinger::test::Factory mFactory;
654 sp<SurfaceFlinger> mFlinger = new SurfaceFlinger(mFactory, SurfaceFlinger::SkipInitialization);
Dominik Laskowski7c9dbf92019-08-01 17:57:31 -0700655 TestableScheduler* mScheduler = nullptr;
Ady Abrahamabc27602020-04-08 17:20:29 -0700656 Hwc2::mock::Display mDisplay;
Lloyd Piquef58625d2017-12-19 13:22:33 -0800657};
658
659} // namespace android