blob: 94fc5f7273312f828f4d7e9fd0e9c8b4357a135e [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 Pique0b785d82018-12-04 17:25:27 -080020#include <compositionengine/Layer.h>
Lloyd Pique9755fb72019-03-26 14:44:40 -070021#include <compositionengine/LayerFECompositionState.h>
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080022#include <compositionengine/OutputLayer.h>
Lloyd Pique70d91362018-10-18 16:02:55 -070023#include <compositionengine/impl/CompositionEngine.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"
29#include "ColorLayer.h"
30#include "ContainerLayer.h"
Lloyd Piquef58625d2017-12-19 13:22:33 -080031#include "DisplayDevice.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
Ana Krulec757f63a2019-01-25 10:46:18 -080082 std::unique_ptr<scheduler::PhaseOffsets> createPhaseOffsets() override {
83 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)>,
87 const scheduler::RefreshRateConfigs&) override {
Lloyd Pique90c115d2018-09-18 21:39:42 -070088 return nullptr;
89 }
90
91 std::unique_ptr<SurfaceInterceptor> createSurfaceInterceptor(SurfaceFlinger* flinger) override {
Lloyd Pique90c115d2018-09-18 21:39:42 -070092 return std::make_unique<android::impl::SurfaceInterceptor>(flinger);
93 }
94
95 sp<StartPropertySetThread> createStartPropertySetThread(bool timestampPropertyValue) override {
Lloyd Pique90c115d2018-09-18 21:39:42 -070096 return new StartPropertySetThread(timestampPropertyValue);
97 }
98
99 sp<DisplayDevice> createDisplayDevice(DisplayDeviceCreationArgs&& creationArgs) override {
Lloyd Pique90c115d2018-09-18 21:39:42 -0700100 return new DisplayDevice(std::move(creationArgs));
101 }
102
103 sp<GraphicBuffer> createGraphicBuffer(uint32_t width, uint32_t height, PixelFormat format,
104 uint32_t layerCount, uint64_t usage,
105 std::string requestorName) override {
Lloyd Pique90c115d2018-09-18 21:39:42 -0700106 return new GraphicBuffer(width, height, format, layerCount, usage, requestorName);
107 }
108
109 void createBufferQueue(sp<IGraphicBufferProducer>* outProducer,
110 sp<IGraphicBufferConsumer>* outConsumer,
111 bool consumerIsSurfaceFlinger) override {
Lloyd Piquee300b312019-10-03 13:03:45 -0700112 if (!mCreateBufferQueue) {
113 BufferQueue::createBufferQueue(outProducer, outConsumer, consumerIsSurfaceFlinger);
114 return;
115 }
Lloyd Pique90c115d2018-09-18 21:39:42 -0700116 mCreateBufferQueue(outProducer, outConsumer, consumerIsSurfaceFlinger);
117 }
118
Lloyd Piquee300b312019-10-03 13:03:45 -0700119 sp<IGraphicBufferProducer> createMonitoredProducer(const sp<IGraphicBufferProducer>& producer,
120 const sp<SurfaceFlinger>& flinger,
121 const wp<Layer>& layer) override {
122 return new MonitoredProducer(producer, flinger, layer);
123 }
124
125 sp<BufferLayerConsumer> createBufferLayerConsumer(const sp<IGraphicBufferConsumer>& consumer,
126 renderengine::RenderEngine& renderEngine,
127 uint32_t textureName, Layer* layer) override {
128 return new BufferLayerConsumer(consumer, renderEngine, textureName, layer);
129 }
130
Lloyd Pique90c115d2018-09-18 21:39:42 -0700131 std::unique_ptr<surfaceflinger::NativeWindowSurface> createNativeWindowSurface(
132 const sp<IGraphicBufferProducer>& producer) override {
133 if (!mCreateNativeWindowSurface) return nullptr;
134 return mCreateNativeWindowSurface(producer);
135 }
136
Lloyd Pique70d91362018-10-18 16:02:55 -0700137 std::unique_ptr<compositionengine::CompositionEngine> createCompositionEngine() override {
138 return compositionengine::impl::createCompositionEngine();
139 }
140
Lloyd Pique90c115d2018-09-18 21:39:42 -0700141 sp<BufferQueueLayer> createBufferQueueLayer(const LayerCreationArgs&) override {
Lloyd Pique90c115d2018-09-18 21:39:42 -0700142 return nullptr;
143 }
144
145 sp<BufferStateLayer> createBufferStateLayer(const LayerCreationArgs&) override {
Lloyd Pique90c115d2018-09-18 21:39:42 -0700146 return nullptr;
147 }
148
149 sp<ColorLayer> createColorLayer(const LayerCreationArgs&) override {
Lloyd Pique90c115d2018-09-18 21:39:42 -0700150 return nullptr;
151 }
152
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
Dominik Laskowski7c9dbf92019-08-01 17:57:31 -0700192 void setupScheduler(std::unique_ptr<DispSync> primaryDispSync,
193 std::unique_ptr<EventControlThread> eventControlThread,
194 std::unique_ptr<EventThread> appEventThread,
195 std::unique_ptr<EventThread> sfEventThread) {
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700196 std::vector<scheduler::RefreshRateConfigs::InputConfig> configs{{/*hwcId=*/0, 16666667}};
197 mFlinger->mRefreshRateConfigs =
198 std::make_unique<scheduler::RefreshRateConfigs>(/*refreshRateSwitching=*/false,
199 configs, /*currentConfig=*/0);
200 mFlinger->mRefreshRateStats =
201 std::make_unique<scheduler::RefreshRateStats>(*mFlinger->mRefreshRateConfigs,
202 *mFlinger->mTimeStats,
203 /*currentConfig=*/0,
204 /*powerMode=*/HWC_POWER_MODE_OFF);
205
Dominik Laskowski7c9dbf92019-08-01 17:57:31 -0700206 mScheduler =
207 new TestableScheduler(std::move(primaryDispSync), std::move(eventControlThread),
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700208 *mFlinger->mRefreshRateConfigs);
Dominik Laskowski7c9dbf92019-08-01 17:57:31 -0700209
Dominik Laskowski98041832019-08-01 18:35:59 -0700210 mFlinger->mAppConnectionHandle = mScheduler->createConnection(std::move(appEventThread));
211 mFlinger->mSfConnectionHandle = mScheduler->createConnection(std::move(sfEventThread));
Dominik Laskowskif7a09ed2019-10-07 13:54:18 -0700212 resetScheduler(mScheduler);
Dominik Laskowski7c9dbf92019-08-01 17:57:31 -0700213
Dominik Laskowski7c9dbf92019-08-01 17:57:31 -0700214 mFlinger->mVSyncModulator.emplace(*mScheduler, mFlinger->mAppConnectionHandle,
215 mFlinger->mSfConnectionHandle,
216 mFlinger->mPhaseOffsets->getCurrentOffsets());
217 }
218
Dominik Laskowskif7a09ed2019-10-07 13:54:18 -0700219 void resetScheduler(Scheduler* scheduler) { mFlinger->mScheduler.reset(scheduler); }
220
Lloyd Pique90c115d2018-09-18 21:39:42 -0700221 using CreateBufferQueueFunction = surfaceflinger::test::Factory::CreateBufferQueueFunction;
Lloyd Pique5b36f3f2018-01-17 11:57:07 -0800222 void setCreateBufferQueueFunction(CreateBufferQueueFunction f) {
Lloyd Pique90c115d2018-09-18 21:39:42 -0700223 mFactory.mCreateBufferQueue = f;
Lloyd Pique5b36f3f2018-01-17 11:57:07 -0800224 }
225
Lloyd Pique90c115d2018-09-18 21:39:42 -0700226 using CreateNativeWindowSurfaceFunction =
227 surfaceflinger::test::Factory::CreateNativeWindowSurfaceFunction;
Lloyd Pique0b1fe702018-01-22 18:03:16 -0800228 void setCreateNativeWindowSurface(CreateNativeWindowSurfaceFunction f) {
Lloyd Pique90c115d2018-09-18 21:39:42 -0700229 mFactory.mCreateNativeWindowSurface = f;
Lloyd Pique0b1fe702018-01-22 18:03:16 -0800230 }
231
Daniel Solomon42d04562019-01-20 21:03:19 -0800232 void setInternalDisplayPrimaries(const ui::DisplayPrimaries& primaries) {
233 memcpy(&mFlinger->mInternalDisplayPrimaries, &primaries, sizeof(ui::DisplayPrimaries));
234 }
235
Lloyd Pique0b1fe702018-01-22 18:03:16 -0800236 using HotplugEvent = SurfaceFlinger::HotplugEvent;
237
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800238 auto& mutableLayerCurrentState(sp<Layer> layer) { return layer->mCurrentState; }
239 auto& mutableLayerDrawingState(sp<Layer> layer) { return layer->mDrawingState; }
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700240
Valerie Haud251afb2019-03-29 14:19:02 -0700241 auto& mutableStateLock() { return mFlinger->mStateLock; }
242
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700243 void setLayerSidebandStream(sp<Layer> layer, sp<NativeHandle> sidebandStream) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800244 layer->mDrawingState.sidebandStream = sidebandStream;
Lloyd Pique0b785d82018-12-04 17:25:27 -0800245 layer->mSidebandStream = sidebandStream;
Lloyd Pique9755fb72019-03-26 14:44:40 -0700246 layer->getCompositionLayer()->editFEState().sidebandStream = sidebandStream;
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700247 }
248
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800249 void setLayerCompositionType(sp<Layer> layer, HWC2::Composition type) {
250 auto outputLayer = layer->findOutputLayerForDisplay(mFlinger->getDefaultDisplayDevice());
251 LOG_ALWAYS_FATAL_IF(!outputLayer);
252 auto& state = outputLayer->editState();
253 LOG_ALWAYS_FATAL_IF(!outputLayer->getState().hwc);
254 (*state.hwc).hwcCompositionType = static_cast<Hwc2::IComposerClient::Composition>(type);
255 };
256
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700257 void setLayerPotentialCursor(sp<Layer> layer, bool potentialCursor) {
258 layer->mPotentialCursor = potentialCursor;
259 }
260
Lloyd Piquef58625d2017-12-19 13:22:33 -0800261 /* ------------------------------------------------------------------------
262 * Forwarding for functions being tested
263 */
Lloyd Pique0b1fe702018-01-22 18:03:16 -0800264
Lloyd Piquea482f992018-01-22 19:00:34 -0800265 auto createDisplay(const String8& displayName, bool secure) {
266 return mFlinger->createDisplay(displayName, secure);
267 }
268
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700269 auto destroyDisplay(const sp<IBinder>& displayToken) {
270 return mFlinger->destroyDisplay(displayToken);
271 }
Lloyd Piquea482f992018-01-22 19:00:34 -0800272
Lloyd Piqued6fbb8a2018-01-22 19:08:36 -0800273 auto resetDisplayState() { return mFlinger->resetDisplayState(); }
274
Dominik Laskowski075d3172018-05-24 15:50:06 -0700275 auto setupNewDisplayDeviceInternal(const wp<IBinder>& displayToken,
276 const std::optional<DisplayId>& displayId,
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800277 const DisplayDeviceState& state,
Lloyd Pique542307f2018-10-19 13:24:08 -0700278 const sp<compositionengine::DisplaySurface>& dispSurface,
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800279 const sp<IGraphicBufferProducer>& producer) {
Dominik Laskowski7e045462018-05-30 13:02:02 -0700280 return mFlinger->setupNewDisplayDeviceInternal(displayToken, displayId, state, dispSurface,
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800281 producer);
282 }
283
Lloyd Pique0b1fe702018-01-22 18:03:16 -0800284 auto handleTransactionLocked(uint32_t transactionFlags) {
Vishnu Nairfa5f8df2019-02-13 17:02:51 -0800285 Mutex::Autolock _l(mFlinger->mStateLock);
Lloyd Pique0b1fe702018-01-22 18:03:16 -0800286 return mFlinger->handleTransactionLocked(transactionFlags);
287 }
Lloyd Piquef58625d2017-12-19 13:22:33 -0800288
Lloyd Pique6cf11032018-01-22 18:57:44 -0800289 auto onHotplugReceived(int32_t sequenceId, hwc2_display_t display,
290 HWC2::Connection connection) {
291 return mFlinger->onHotplugReceived(sequenceId, display, connection);
292 }
293
Vishnu Nairce5d0cc2019-02-28 14:38:41 -0800294 auto setDisplayStateLocked(const DisplayState& s) {
295 Mutex::Autolock _l(mFlinger->mStateLock);
296 return mFlinger->setDisplayStateLocked(s);
297 }
Lloyd Pique9d9cf402018-02-16 17:47:13 -0800298
Dominik Laskowski83b88212018-12-11 13:34:06 -0800299 // Allow reading display state without locking, as if called on the SF main thread.
300 auto onInitializeDisplays() NO_THREAD_SAFETY_ANALYSIS {
301 return mFlinger->onInitializeDisplays();
302 }
Lloyd Pique86016da2018-03-01 16:09:38 -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 setPowerModeInternal(const sp<DisplayDevice>& display,
306 int mode) NO_THREAD_SAFETY_ANALYSIS {
Dominik Laskowskie9774092018-12-11 10:04:24 -0800307 return mFlinger->setPowerModeInternal(display, mode);
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -0800308 }
309
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700310 auto onMessageReceived(int32_t what) { return mFlinger->onMessageReceived(what); }
311
Robert Carr108b2c72019-04-02 16:32:58 -0700312 auto captureScreenImplLocked(
313 const RenderArea& renderArea, SurfaceFlinger::TraverseLayersFunction traverseLayers,
314 ANativeWindowBuffer* buffer, bool useIdentityTransform, bool forSystem, int* outSyncFd) {
315 bool ignored;
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700316 return mFlinger->captureScreenImplLocked(renderArea, traverseLayers, buffer,
Robert Carr108b2c72019-04-02 16:32:58 -0700317 useIdentityTransform, forSystem, outSyncFd,
318 ignored);
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700319 }
320
chaviw0e3479f2018-09-10 16:49:30 -0700321 auto traverseLayersInDisplay(const sp<const DisplayDevice>& display,
322 const LayerVector::Visitor& visitor) {
323 return mFlinger->SurfaceFlinger::traverseLayersInDisplay(display, visitor);
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700324 }
325
Daniel Solomon42d04562019-01-20 21:03:19 -0800326 auto getDisplayNativePrimaries(const sp<IBinder>& displayToken,
327 ui::DisplayPrimaries &primaries) {
328 return mFlinger->SurfaceFlinger::getDisplayNativePrimaries(displayToken, primaries);
329 }
330
Valerie Haud251afb2019-03-29 14:19:02 -0700331 auto& getTransactionQueue() { return mFlinger->mTransactionQueues; }
332
333 auto setTransactionState(const Vector<ComposerState>& states,
334 const Vector<DisplayState>& displays, uint32_t flags,
335 const sp<IBinder>& applyToken,
336 const InputWindowCommands& inputWindowCommands,
337 int64_t desiredPresentTime, const client_cache_t& uncacheBuffer,
338 bool hasListenerCallbacks,
339 std::vector<ListenerCallbacks>& listenerCallbacks) {
340 return mFlinger->setTransactionState(states, displays, flags, applyToken,
341 inputWindowCommands, desiredPresentTime, uncacheBuffer,
342 hasListenerCallbacks, listenerCallbacks);
343 }
344
345 auto flushTransactionQueues() { return mFlinger->flushTransactionQueues(); };
346
Lloyd Pique86016da2018-03-01 16:09:38 -0800347 /* ------------------------------------------------------------------------
348 * Read-only access to private data to assert post-conditions.
349 */
350
351 const auto& getAnimFrameTracker() const { return mFlinger->mAnimFrameTracker; }
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -0800352 const auto& getHasPoweredOff() const { return mFlinger->mHasPoweredOff; }
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -0800353 const auto& getVisibleRegionsDirty() const { return mFlinger->mVisibleRegionsDirty; }
Lloyd Pique441d5042018-10-18 16:49:51 -0700354 auto& getHwComposer() const {
355 return static_cast<impl::HWComposer&>(mFlinger->getHwComposer());
356 }
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -0800357
Lloyd Pique86016da2018-03-01 16:09:38 -0800358 const auto& getCompositorTiming() const { return mFlinger->getBE().mCompositorTiming; }
359
Lloyd Piquef58625d2017-12-19 13:22:33 -0800360 /* ------------------------------------------------------------------------
361 * Read-write access to private data to set up preconditions and assert
362 * post-conditions.
363 */
Lloyd Pique0b1fe702018-01-22 18:03:16 -0800364
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800365 auto& mutableHasWideColorDisplay() { return SurfaceFlinger::hasWideColorDisplay; }
Chia-I Wu304d9cd2018-08-27 14:38:14 -0700366 auto& mutablePrimaryDisplayOrientation() { return SurfaceFlinger::primaryDisplayOrientation; }
Peiyong Lin13effd12018-07-24 17:01:47 -0700367 auto& mutableUseColorManagement() { return SurfaceFlinger::useColorManagement; }
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800368
Lloyd Piquee39cad22017-12-20 17:01:29 -0800369 auto& mutableCurrentState() { return mFlinger->mCurrentState; }
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800370 auto& mutableDisplayColorSetting() { return mFlinger->mDisplayColorSetting; }
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700371 auto& mutableDisplays() { return mFlinger->mDisplays; }
Lloyd Piquee39cad22017-12-20 17:01:29 -0800372 auto& mutableDrawingState() { return mFlinger->mDrawingState; }
Lloyd Piquee39cad22017-12-20 17:01:29 -0800373 auto& mutableEventQueue() { return mFlinger->mEventQueue; }
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700374 auto& mutableGeometryInvalid() { return mFlinger->mGeometryInvalid; }
Lloyd Pique0b1fe702018-01-22 18:03:16 -0800375 auto& mutableInterceptor() { return mFlinger->mInterceptor; }
Lloyd Pique6cf11032018-01-22 18:57:44 -0800376 auto& mutableMainThreadId() { return mFlinger->mMainThreadId; }
Lloyd Pique0b1fe702018-01-22 18:03:16 -0800377 auto& mutablePendingHotplugEvents() { return mFlinger->mPendingHotplugEvents; }
Dominik Laskowski075d3172018-05-24 15:50:06 -0700378 auto& mutablePhysicalDisplayTokens() { return mFlinger->mPhysicalDisplayTokens; }
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700379 auto& mutableTexturePool() { return mFlinger->mTexturePool; }
Lloyd Pique0b1fe702018-01-22 18:03:16 -0800380 auto& mutableTransactionFlags() { return mFlinger->mTransactionFlags; }
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800381 auto& mutableUseHwcVirtualDisplays() { return mFlinger->mUseHwcVirtualDisplays; }
Peiyong Lin74ca2f42019-01-14 19:36:57 -0800382 auto& mutablePowerAdvisor() { return mFlinger->mPowerAdvisor; }
Lloyd Pique4fe29402019-08-12 16:51:24 -0700383 auto& mutableDebugDisableHWC() { return mFlinger->mDebugDisableHWC; }
Lloyd Piquee39cad22017-12-20 17:01:29 -0800384
Lloyd Pique6cf11032018-01-22 18:57:44 -0800385 auto& mutableComposerSequenceId() { return mFlinger->getBE().mComposerSequenceId; }
Lloyd Pique441d5042018-10-18 16:49:51 -0700386 auto& mutableHwcDisplayData() { return getHwComposer().mDisplayData; }
387 auto& mutableHwcPhysicalDisplayIdMap() { return getHwComposer().mPhysicalDisplayIdMap; }
388 auto& mutableInternalHwcDisplayId() { return getHwComposer().mInternalHwcDisplayId; }
389 auto& mutableExternalHwcDisplayId() { return getHwComposer().mExternalHwcDisplayId; }
Lloyd Piquea618d852018-01-17 11:52:30 -0800390
Lloyd Piquee39cad22017-12-20 17:01:29 -0800391 ~TestableSurfaceFlinger() {
392 // All these pointer and container clears help ensure that GMock does
393 // not report a leaked object, since the SurfaceFlinger instance may
394 // still be referenced by something despite our best efforts to destroy
395 // it after each test is done.
396 mutableDisplays().clear();
Ady Abraham2939f092019-03-04 17:44:06 -0800397 mutableCurrentState().displays.clear();
398 mutableDrawingState().displays.clear();
Lloyd Pique0b1fe702018-01-22 18:03:16 -0800399 mutableEventQueue().reset();
Lloyd Pique0b1fe702018-01-22 18:03:16 -0800400 mutableInterceptor().reset();
Lloyd Pique2eb71eb2019-08-13 13:23:03 -0700401 mFlinger->mScheduler.reset();
Lloyd Pique441d5042018-10-18 16:49:51 -0700402 mFlinger->mCompositionEngine->setHwComposer(std::unique_ptr<HWComposer>());
Lloyd Piqueb97e04f2018-10-18 17:07:05 -0700403 mFlinger->mCompositionEngine->setRenderEngine(
404 std::unique_ptr<renderengine::RenderEngine>());
Lloyd Piquee39cad22017-12-20 17:01:29 -0800405 }
Lloyd Piquef58625d2017-12-19 13:22:33 -0800406
Lloyd Piquea618d852018-01-17 11:52:30 -0800407 /* ------------------------------------------------------------------------
408 * Wrapper classes for Read-write access to private data to set up
409 * preconditions and assert post-conditions.
410 */
Ana Krulec4593b692019-01-11 22:07:25 -0800411 struct HWC2Display : public HWC2::impl::Display {
Peiyong Lin74ca2f42019-01-14 19:36:57 -0800412 HWC2Display(Hwc2::Composer& composer,
Lloyd Piquea618d852018-01-17 11:52:30 -0800413 const std::unordered_set<HWC2::Capability>& capabilities, hwc2_display_t id,
414 HWC2::DisplayType type)
Peiyong Lin74ca2f42019-01-14 19:36:57 -0800415 : HWC2::impl::Display(composer, capabilities, id, type) {}
Lloyd Piquea618d852018-01-17 11:52:30 -0800416 ~HWC2Display() {
417 // Prevents a call to disable vsyncs.
418 mType = HWC2::DisplayType::Invalid;
419 }
420
421 auto& mutableIsConnected() { return this->mIsConnected; }
422 auto& mutableConfigs() { return this->mConfigs; }
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700423 auto& mutableLayers() { return this->mLayers; }
Lloyd Piquea618d852018-01-17 11:52:30 -0800424 };
425
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800426 class FakeHwcDisplayInjector {
427 public:
428 static constexpr hwc2_display_t DEFAULT_HWC_DISPLAY_ID = 1000;
429 static constexpr int32_t DEFAULT_WIDTH = 1920;
430 static constexpr int32_t DEFAULT_HEIGHT = 1280;
431 static constexpr int32_t DEFAULT_REFRESH_RATE = 16'666'666;
432 static constexpr int32_t DEFAULT_DPI = 320;
433 static constexpr int32_t DEFAULT_ACTIVE_CONFIG = 0;
Peiyong Lin1336e6e2019-05-28 09:23:50 -0700434 static constexpr int32_t DEFAULT_POWER_MODE = 2;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800435
Dominik Laskowski075d3172018-05-24 15:50:06 -0700436 FakeHwcDisplayInjector(DisplayId displayId, HWC2::DisplayType hwcDisplayType,
437 bool isPrimary)
438 : mDisplayId(displayId), mHwcDisplayType(hwcDisplayType), mIsPrimary(isPrimary) {}
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800439
440 auto& setHwcDisplayId(hwc2_display_t displayId) {
441 mHwcDisplayId = displayId;
442 return *this;
443 }
444
445 auto& setWidth(int32_t width) {
446 mWidth = width;
447 return *this;
448 }
449
450 auto& setHeight(int32_t height) {
451 mHeight = height;
452 return *this;
453 }
454
455 auto& setRefreshRate(int32_t refreshRate) {
456 mRefreshRate = refreshRate;
457 return *this;
458 }
459
460 auto& setDpiX(int32_t dpi) {
461 mDpiX = dpi;
462 return *this;
463 }
464
465 auto& setDpiY(int32_t dpi) {
466 mDpiY = dpi;
467 return *this;
468 }
469
470 auto& setActiveConfig(int32_t config) {
471 mActiveConfig = config;
472 return *this;
473 }
474
Lloyd Piquee22f0332018-07-16 16:35:56 -0700475 auto& setCapabilities(const std::unordered_set<HWC2::Capability>* capabilities) {
476 mCapabilities = capabilities;
477 return *this;
478 }
479
Peiyong Lin1336e6e2019-05-28 09:23:50 -0700480 auto& setPowerMode(int mode) {
481 mPowerMode = mode;
482 return *this;
483 }
484
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800485 void inject(TestableSurfaceFlinger* flinger, Hwc2::Composer* composer) {
Lloyd Piquee22f0332018-07-16 16:35:56 -0700486 static const std::unordered_set<HWC2::Capability> defaultCapabilities;
487 if (mCapabilities == nullptr) mCapabilities = &defaultCapabilities;
488
489 // Caution - Make sure that any values passed by reference here do
490 // not refer to an instance owned by FakeHwcDisplayInjector. This
491 // class has temporary lifetime, while the constructed HWC2::Display
492 // is much longer lived.
Peiyong Lin74ca2f42019-01-14 19:36:57 -0800493 auto display = std::make_unique<HWC2Display>(*composer, *mCapabilities, mHwcDisplayId,
494 mHwcDisplayType);
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800495
496 auto config = HWC2::Display::Config::Builder(*display, mActiveConfig);
497 config.setWidth(mWidth);
498 config.setHeight(mHeight);
499 config.setVsyncPeriod(mRefreshRate);
500 config.setDpiX(mDpiX);
501 config.setDpiY(mDpiY);
502 display->mutableConfigs().emplace(mActiveConfig, config.build());
503 display->mutableIsConnected() = true;
Peiyong Lin1336e6e2019-05-28 09:23:50 -0700504 display->setPowerMode(static_cast<HWC2::PowerMode>(mPowerMode));
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800505
Dominik Laskowski075d3172018-05-24 15:50:06 -0700506 flinger->mutableHwcDisplayData()[mDisplayId].hwcDisplay = display.get();
507
508 if (mHwcDisplayType == HWC2::DisplayType::Physical) {
509 flinger->mutableHwcPhysicalDisplayIdMap().emplace(mHwcDisplayId, mDisplayId);
510 (mIsPrimary ? flinger->mutableInternalHwcDisplayId()
511 : flinger->mutableExternalHwcDisplayId()) = mHwcDisplayId;
512 }
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800513
514 flinger->mFakeHwcDisplays.push_back(std::move(display));
515 }
516
517 private:
Dominik Laskowski075d3172018-05-24 15:50:06 -0700518 const DisplayId mDisplayId;
519 const HWC2::DisplayType mHwcDisplayType;
520 const bool mIsPrimary;
521
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800522 hwc2_display_t mHwcDisplayId = DEFAULT_HWC_DISPLAY_ID;
523 int32_t mWidth = DEFAULT_WIDTH;
524 int32_t mHeight = DEFAULT_HEIGHT;
525 int32_t mRefreshRate = DEFAULT_REFRESH_RATE;
526 int32_t mDpiX = DEFAULT_DPI;
527 int32_t mDpiY = DEFAULT_DPI;
528 int32_t mActiveConfig = DEFAULT_ACTIVE_CONFIG;
Peiyong Lin1336e6e2019-05-28 09:23:50 -0700529 int32_t mPowerMode = DEFAULT_POWER_MODE;
Lloyd Piquee22f0332018-07-16 16:35:56 -0700530 const std::unordered_set<HWC2::Capability>* mCapabilities = nullptr;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800531 };
532
533 class FakeDisplayDeviceInjector {
534 public:
Dominik Laskowski075d3172018-05-24 15:50:06 -0700535 FakeDisplayDeviceInjector(TestableSurfaceFlinger& flinger,
536 const std::optional<DisplayId>& displayId, bool isVirtual,
537 bool isPrimary)
538 : mFlinger(flinger), mCreationArgs(flinger.mFlinger.get(), mDisplayToken, displayId) {
539 mCreationArgs.isVirtual = isVirtual;
540 mCreationArgs.isPrimary = isPrimary;
541 }
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800542
543 sp<IBinder> token() const { return mDisplayToken; }
544
545 DisplayDeviceState& mutableDrawingDisplayState() {
546 return mFlinger.mutableDrawingState().displays.editValueFor(mDisplayToken);
547 }
548
549 DisplayDeviceState& mutableCurrentDisplayState() {
550 return mFlinger.mutableCurrentState().displays.editValueFor(mDisplayToken);
551 }
552
Lloyd Pique9d9cf402018-02-16 17:47:13 -0800553 const auto& getDrawingDisplayState() {
554 return mFlinger.mutableDrawingState().displays.valueFor(mDisplayToken);
555 }
556
557 const auto& getCurrentDisplayState() {
558 return mFlinger.mutableCurrentState().displays.valueFor(mDisplayToken);
559 }
560
Dominik Laskowski9fae1022018-05-29 13:17:40 -0700561 auto& mutableDisplayDevice() { return mFlinger.mutableDisplays()[mDisplayToken]; }
Lloyd Pique9d9cf402018-02-16 17:47:13 -0800562
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800563 auto& setNativeWindow(const sp<ANativeWindow>& nativeWindow) {
Lloyd Pique2eef1d22018-09-18 21:30:04 -0700564 mCreationArgs.nativeWindow = nativeWindow;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800565 return *this;
566 }
567
Lloyd Pique542307f2018-10-19 13:24:08 -0700568 auto& setDisplaySurface(const sp<compositionengine::DisplaySurface>& displaySurface) {
Lloyd Pique2eef1d22018-09-18 21:30:04 -0700569 mCreationArgs.displaySurface = displaySurface;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800570 return *this;
571 }
572
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800573 auto& setSecure(bool secure) {
Lloyd Pique2eef1d22018-09-18 21:30:04 -0700574 mCreationArgs.isSecure = secure;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800575 return *this;
576 }
577
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700578 auto& setPowerMode(int mode) {
Lloyd Pique2eef1d22018-09-18 21:30:04 -0700579 mCreationArgs.initialPowerMode = mode;
Lloyd Piqued6b579f2018-04-06 15:29:10 -0700580 return *this;
581 }
582
Valerie Hau9758ae02018-10-09 16:05:09 -0700583 auto& setHwcColorModes(
584 const std::unordered_map<ui::ColorMode, std::vector<ui::RenderIntent>>
585 hwcColorModes) {
586 mCreationArgs.hwcColorModes = hwcColorModes;
587 return *this;
588 }
589
590 auto& setHasWideColorGamut(bool hasWideColorGamut) {
591 mCreationArgs.hasWideColorGamut = hasWideColorGamut;
592 return *this;
593 }
594
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800595 sp<DisplayDevice> inject() {
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800596 DisplayDeviceState state;
Dominik Laskowski075d3172018-05-24 15:50:06 -0700597 state.displayId = mCreationArgs.isVirtual ? std::nullopt : mCreationArgs.displayId;
Lloyd Pique2eef1d22018-09-18 21:30:04 -0700598 state.isSecure = mCreationArgs.isSecure;
599
600 sp<DisplayDevice> device = new DisplayDevice(std::move(mCreationArgs));
601 mFlinger.mutableDisplays().emplace(mDisplayToken, device);
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800602 mFlinger.mutableCurrentState().displays.add(mDisplayToken, state);
603 mFlinger.mutableDrawingState().displays.add(mDisplayToken, state);
604
Dominik Laskowski075d3172018-05-24 15:50:06 -0700605 if (!mCreationArgs.isVirtual) {
606 LOG_ALWAYS_FATAL_IF(!state.displayId);
607 mFlinger.mutablePhysicalDisplayTokens()[*state.displayId] = mDisplayToken;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800608 }
609
610 return device;
611 }
612
613 private:
614 TestableSurfaceFlinger& mFlinger;
615 sp<BBinder> mDisplayToken = new BBinder();
Lloyd Pique2eef1d22018-09-18 21:30:04 -0700616 DisplayDeviceCreationArgs mCreationArgs;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800617 };
618
Lloyd Pique90c115d2018-09-18 21:39:42 -0700619 surfaceflinger::test::Factory mFactory;
620 sp<SurfaceFlinger> mFlinger = new SurfaceFlinger(mFactory, SurfaceFlinger::SkipInitialization);
Dominik Laskowski7c9dbf92019-08-01 17:57:31 -0700621 TestableScheduler* mScheduler = nullptr;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800622
623 // We need to keep a reference to these so they are properly destroyed.
624 std::vector<std::unique_ptr<HWC2Display>> mFakeHwcDisplays;
Lloyd Piquef58625d2017-12-19 13:22:33 -0800625};
626
627} // namespace android