1 /* 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 19 #include <compositionengine/Display.h> 20 #include <compositionengine/LayerFECompositionState.h> 21 #include <compositionengine/OutputLayer.h> 22 #include <compositionengine/impl/CompositionEngine.h> 23 #include <compositionengine/impl/Display.h> 24 #include <compositionengine/impl/OutputLayerCompositionState.h> 25 #include <compositionengine/mock/DisplaySurface.h> 26 27 #include "BufferQueueLayer.h" 28 #include "BufferStateLayer.h" 29 #include "ContainerLayer.h" 30 #include "DisplayDevice.h" 31 #include "EffectLayer.h" 32 #include "FakePhaseOffsets.h" 33 #include "Layer.h" 34 #include "NativeWindowSurface.h" 35 #include "Scheduler/MessageQueue.h" 36 #include "Scheduler/RefreshRateConfigs.h" 37 #include "StartPropertySetThread.h" 38 #include "SurfaceFlinger.h" 39 #include "SurfaceFlingerDefaultFactory.h" 40 #include "SurfaceInterceptor.h" 41 #include "TestableScheduler.h" 42 #include "mock/DisplayHardware/MockDisplay.h" 43 44 namespace android { 45 46 class EventThread; 47 48 namespace renderengine { 49 50 class RenderEngine; 51 52 } // namespace renderengine 53 54 namespace Hwc2 { 55 56 class Composer; 57 58 } // namespace Hwc2 59 60 namespace hal = android::hardware::graphics::composer::hal; 61 62 namespace surfaceflinger::test { 63 64 class Factory final : public surfaceflinger::Factory { 65 public: 66 ~Factory() = default; 67 createDispSync(const char *,bool)68 std::unique_ptr<DispSync> createDispSync(const char*, bool) override { 69 return nullptr; 70 } 71 createEventControlThread(std::function<void (bool)>)72 std::unique_ptr<EventControlThread> createEventControlThread( 73 std::function<void(bool)>) override { 74 return nullptr; 75 } 76 createHWComposer(const std::string &)77 std::unique_ptr<HWComposer> createHWComposer(const std::string&) override { 78 return nullptr; 79 } 80 createMessageQueue()81 std::unique_ptr<MessageQueue> createMessageQueue() override { 82 return std::make_unique<android::impl::MessageQueue>(); 83 } 84 createPhaseConfiguration(const scheduler::RefreshRateConfigs &)85 std::unique_ptr<scheduler::PhaseConfiguration> createPhaseConfiguration( 86 const scheduler::RefreshRateConfigs& /*refreshRateConfigs*/) override { 87 return std::make_unique<scheduler::FakePhaseOffsets>(); 88 } 89 createScheduler(std::function<void (bool)>,const scheduler::RefreshRateConfigs &,ISchedulerCallback &)90 std::unique_ptr<Scheduler> createScheduler(std::function<void(bool)>, 91 const scheduler::RefreshRateConfigs&, 92 ISchedulerCallback&) override { 93 return nullptr; 94 } 95 createSurfaceInterceptor(SurfaceFlinger * flinger)96 std::unique_ptr<SurfaceInterceptor> createSurfaceInterceptor(SurfaceFlinger* flinger) override { 97 return std::make_unique<android::impl::SurfaceInterceptor>(flinger); 98 } 99 createStartPropertySetThread(bool timestampPropertyValue)100 sp<StartPropertySetThread> createStartPropertySetThread(bool timestampPropertyValue) override { 101 return new StartPropertySetThread(timestampPropertyValue); 102 } 103 createDisplayDevice(DisplayDeviceCreationArgs & creationArgs)104 sp<DisplayDevice> createDisplayDevice(DisplayDeviceCreationArgs& creationArgs) override { 105 return new DisplayDevice(creationArgs); 106 } 107 createGraphicBuffer(uint32_t width,uint32_t height,PixelFormat format,uint32_t layerCount,uint64_t usage,std::string requestorName)108 sp<GraphicBuffer> createGraphicBuffer(uint32_t width, uint32_t height, PixelFormat format, 109 uint32_t layerCount, uint64_t usage, 110 std::string requestorName) override { 111 return new GraphicBuffer(width, height, format, layerCount, usage, requestorName); 112 } 113 createBufferQueue(sp<IGraphicBufferProducer> * outProducer,sp<IGraphicBufferConsumer> * outConsumer,bool consumerIsSurfaceFlinger)114 void createBufferQueue(sp<IGraphicBufferProducer>* outProducer, 115 sp<IGraphicBufferConsumer>* outConsumer, 116 bool consumerIsSurfaceFlinger) override { 117 if (!mCreateBufferQueue) { 118 BufferQueue::createBufferQueue(outProducer, outConsumer, consumerIsSurfaceFlinger); 119 return; 120 } 121 mCreateBufferQueue(outProducer, outConsumer, consumerIsSurfaceFlinger); 122 } 123 createMonitoredProducer(const sp<IGraphicBufferProducer> & producer,const sp<SurfaceFlinger> & flinger,const wp<Layer> & layer)124 sp<IGraphicBufferProducer> createMonitoredProducer(const sp<IGraphicBufferProducer>& producer, 125 const sp<SurfaceFlinger>& flinger, 126 const wp<Layer>& layer) override { 127 return new MonitoredProducer(producer, flinger, layer); 128 } 129 createBufferLayerConsumer(const sp<IGraphicBufferConsumer> & consumer,renderengine::RenderEngine & renderEngine,uint32_t textureName,Layer * layer)130 sp<BufferLayerConsumer> createBufferLayerConsumer(const sp<IGraphicBufferConsumer>& consumer, 131 renderengine::RenderEngine& renderEngine, 132 uint32_t textureName, Layer* layer) override { 133 return new BufferLayerConsumer(consumer, renderEngine, textureName, layer); 134 } 135 createNativeWindowSurface(const sp<IGraphicBufferProducer> & producer)136 std::unique_ptr<surfaceflinger::NativeWindowSurface> createNativeWindowSurface( 137 const sp<IGraphicBufferProducer>& producer) override { 138 if (!mCreateNativeWindowSurface) return nullptr; 139 return mCreateNativeWindowSurface(producer); 140 } 141 createCompositionEngine()142 std::unique_ptr<compositionengine::CompositionEngine> createCompositionEngine() override { 143 return compositionengine::impl::createCompositionEngine(); 144 } 145 createBufferQueueLayer(const LayerCreationArgs &)146 sp<BufferQueueLayer> createBufferQueueLayer(const LayerCreationArgs&) override { 147 return nullptr; 148 } 149 createBufferStateLayer(const LayerCreationArgs &)150 sp<BufferStateLayer> createBufferStateLayer(const LayerCreationArgs&) override { 151 return nullptr; 152 } 153 createEffectLayer(const LayerCreationArgs &)154 sp<EffectLayer> createEffectLayer(const LayerCreationArgs&) override { return nullptr; } 155 createContainerLayer(const LayerCreationArgs &)156 sp<ContainerLayer> createContainerLayer(const LayerCreationArgs&) override { 157 return nullptr; 158 } 159 160 using CreateBufferQueueFunction = 161 std::function<void(sp<IGraphicBufferProducer>* /* outProducer */, 162 sp<IGraphicBufferConsumer>* /* outConsumer */, 163 bool /* consumerIsSurfaceFlinger */)>; 164 CreateBufferQueueFunction mCreateBufferQueue; 165 166 using CreateNativeWindowSurfaceFunction = 167 std::function<std::unique_ptr<surfaceflinger::NativeWindowSurface>( 168 const sp<IGraphicBufferProducer>&)>; 169 CreateNativeWindowSurfaceFunction mCreateNativeWindowSurface; 170 171 using CreateCompositionEngineFunction = 172 std::function<std::unique_ptr<compositionengine::CompositionEngine>()>; 173 CreateCompositionEngineFunction mCreateCompositionEngine; 174 }; 175 176 } // namespace surfaceflinger::test 177 178 class TestableSurfaceFlinger { 179 public: 180 using HotplugEvent = SurfaceFlinger::HotplugEvent; 181 flinger()182 SurfaceFlinger* flinger() { return mFlinger.get(); } scheduler()183 TestableScheduler* scheduler() { return mScheduler; } 184 185 // Extend this as needed for accessing SurfaceFlinger private (and public) 186 // functions. 187 setupRenderEngine(std::unique_ptr<renderengine::RenderEngine> renderEngine)188 void setupRenderEngine(std::unique_ptr<renderengine::RenderEngine> renderEngine) { 189 mFlinger->mCompositionEngine->setRenderEngine(std::move(renderEngine)); 190 } 191 setupComposer(std::unique_ptr<Hwc2::Composer> composer)192 void setupComposer(std::unique_ptr<Hwc2::Composer> composer) { 193 mFlinger->mCompositionEngine->setHwComposer( 194 std::make_unique<impl::HWComposer>(std::move(composer))); 195 } 196 setupTimeStats(const std::shared_ptr<TimeStats> & timeStats)197 void setupTimeStats(const std::shared_ptr<TimeStats>& timeStats) { 198 mFlinger->mCompositionEngine->setTimeStats(timeStats); 199 } 200 201 void setupScheduler(std::unique_ptr<DispSync> primaryDispSync, 202 std::unique_ptr<EventControlThread> eventControlThread, 203 std::unique_ptr<EventThread> appEventThread, 204 std::unique_ptr<EventThread> sfEventThread, 205 bool useContentDetectionV2 = false) { 206 std::vector<std::shared_ptr<const HWC2::Display::Config>> configs{ 207 HWC2::Display::Config::Builder(mDisplay, 0) 208 .setVsyncPeriod(int32_t(16666667)) 209 .setConfigGroup(0) 210 .build()}; 211 212 mFlinger->mRefreshRateConfigs = std::make_unique< 213 scheduler::RefreshRateConfigs>(configs, /*currentConfig=*/HwcConfigIndexType(0)); 214 mFlinger->mRefreshRateStats = std::make_unique< 215 scheduler::RefreshRateStats>(*mFlinger->mRefreshRateConfigs, *mFlinger->mTimeStats, 216 /*currentConfig=*/HwcConfigIndexType(0), 217 /*powerMode=*/hal::PowerMode::OFF); 218 mFlinger->mPhaseConfiguration = 219 mFactory.createPhaseConfiguration(*mFlinger->mRefreshRateConfigs); 220 221 mScheduler = 222 new TestableScheduler(std::move(primaryDispSync), std::move(eventControlThread), 223 *mFlinger->mRefreshRateConfigs, useContentDetectionV2); 224 225 mFlinger->mAppConnectionHandle = mScheduler->createConnection(std::move(appEventThread)); 226 mFlinger->mSfConnectionHandle = mScheduler->createConnection(std::move(sfEventThread)); 227 resetScheduler(mScheduler); 228 229 mFlinger->mVSyncModulator.emplace(*mScheduler, mFlinger->mAppConnectionHandle, 230 mFlinger->mSfConnectionHandle, 231 mFlinger->mPhaseConfiguration->getCurrentOffsets()); 232 } 233 resetScheduler(Scheduler * scheduler)234 void resetScheduler(Scheduler* scheduler) { mFlinger->mScheduler.reset(scheduler); } 235 236 using CreateBufferQueueFunction = surfaceflinger::test::Factory::CreateBufferQueueFunction; setCreateBufferQueueFunction(CreateBufferQueueFunction f)237 void setCreateBufferQueueFunction(CreateBufferQueueFunction f) { 238 mFactory.mCreateBufferQueue = f; 239 } 240 241 using CreateNativeWindowSurfaceFunction = 242 surfaceflinger::test::Factory::CreateNativeWindowSurfaceFunction; setCreateNativeWindowSurface(CreateNativeWindowSurfaceFunction f)243 void setCreateNativeWindowSurface(CreateNativeWindowSurfaceFunction f) { 244 mFactory.mCreateNativeWindowSurface = f; 245 } 246 setInternalDisplayPrimaries(const ui::DisplayPrimaries & primaries)247 void setInternalDisplayPrimaries(const ui::DisplayPrimaries& primaries) { 248 memcpy(&mFlinger->mInternalDisplayPrimaries, &primaries, sizeof(ui::DisplayPrimaries)); 249 } 250 mutableLayerCurrentState(const sp<Layer> & layer)251 static auto& mutableLayerCurrentState(const sp<Layer>& layer) { return layer->mCurrentState; } mutableLayerDrawingState(const sp<Layer> & layer)252 static auto& mutableLayerDrawingState(const sp<Layer>& layer) { return layer->mDrawingState; } 253 mutableStateLock()254 auto& mutableStateLock() { return mFlinger->mStateLock; } 255 findOutputLayerForDisplay(const sp<Layer> & layer,const sp<const DisplayDevice> & display)256 static auto findOutputLayerForDisplay(const sp<Layer>& layer, 257 const sp<const DisplayDevice>& display) { 258 return layer->findOutputLayerForDisplay(display.get()); 259 } 260 setLayerSidebandStream(const sp<Layer> & layer,const sp<NativeHandle> & sidebandStream)261 static void setLayerSidebandStream(const sp<Layer>& layer, 262 const sp<NativeHandle>& sidebandStream) { 263 layer->mDrawingState.sidebandStream = sidebandStream; 264 layer->mSidebandStream = sidebandStream; 265 layer->editCompositionState()->sidebandStream = sidebandStream; 266 } 267 setLayerCompositionType(const sp<Layer> & layer,hal::Composition type)268 void setLayerCompositionType(const sp<Layer>& layer, hal::Composition type) { 269 auto outputLayer = findOutputLayerForDisplay(layer, mFlinger->getDefaultDisplayDevice()); 270 LOG_ALWAYS_FATAL_IF(!outputLayer); 271 auto& state = outputLayer->editState(); 272 LOG_ALWAYS_FATAL_IF(!outputLayer->getState().hwc); 273 (*state.hwc).hwcCompositionType = type; 274 } 275 setLayerPotentialCursor(const sp<Layer> & layer,bool potentialCursor)276 static void setLayerPotentialCursor(const sp<Layer>& layer, bool potentialCursor) { 277 layer->mPotentialCursor = potentialCursor; 278 } 279 280 /* ------------------------------------------------------------------------ 281 * Forwarding for functions being tested 282 */ 283 createDisplay(const String8 & displayName,bool secure)284 auto createDisplay(const String8& displayName, bool secure) { 285 return mFlinger->createDisplay(displayName, secure); 286 } 287 destroyDisplay(const sp<IBinder> & displayToken)288 auto destroyDisplay(const sp<IBinder>& displayToken) { 289 return mFlinger->destroyDisplay(displayToken); 290 } 291 resetDisplayState()292 auto resetDisplayState() NO_THREAD_SAFETY_ANALYSIS { return mFlinger->resetDisplayState(); } 293 setupNewDisplayDeviceInternal(const wp<IBinder> & displayToken,std::shared_ptr<compositionengine::Display> compositionDisplay,const DisplayDeviceState & state,const sp<compositionengine::DisplaySurface> & dispSurface,const sp<IGraphicBufferProducer> & producer)294 auto setupNewDisplayDeviceInternal( 295 const wp<IBinder>& displayToken, 296 std::shared_ptr<compositionengine::Display> compositionDisplay, 297 const DisplayDeviceState& state, 298 const sp<compositionengine::DisplaySurface>& dispSurface, 299 const sp<IGraphicBufferProducer>& producer) NO_THREAD_SAFETY_ANALYSIS { 300 return mFlinger->setupNewDisplayDeviceInternal(displayToken, compositionDisplay, state, 301 dispSurface, producer); 302 } 303 handleTransactionLocked(uint32_t transactionFlags)304 auto handleTransactionLocked(uint32_t transactionFlags) { 305 Mutex::Autolock _l(mFlinger->mStateLock); 306 return mFlinger->handleTransactionLocked(transactionFlags); 307 } 308 onHotplugReceived(int32_t sequenceId,hal::HWDisplayId display,hal::Connection connection)309 auto onHotplugReceived(int32_t sequenceId, hal::HWDisplayId display, 310 hal::Connection connection) { 311 return mFlinger->onHotplugReceived(sequenceId, display, connection); 312 } 313 setDisplayStateLocked(const DisplayState & s)314 auto setDisplayStateLocked(const DisplayState& s) { 315 Mutex::Autolock _l(mFlinger->mStateLock); 316 return mFlinger->setDisplayStateLocked(s); 317 } 318 319 // Allow reading display state without locking, as if called on the SF main thread. onInitializeDisplays()320 auto onInitializeDisplays() NO_THREAD_SAFETY_ANALYSIS { 321 return mFlinger->onInitializeDisplays(); 322 } 323 324 // Allow reading display state without locking, as if called on the SF main thread. setPowerModeInternal(const sp<DisplayDevice> & display,hal::PowerMode mode)325 auto setPowerModeInternal(const sp<DisplayDevice>& display, 326 hal::PowerMode mode) NO_THREAD_SAFETY_ANALYSIS { 327 return mFlinger->setPowerModeInternal(display, mode); 328 } 329 onMessageReceived(int32_t what)330 auto onMessageReceived(int32_t what) { return mFlinger->onMessageReceived(what, systemTime()); } 331 captureScreenImplLocked(const RenderArea & renderArea,SurfaceFlinger::TraverseLayersFunction traverseLayers,ANativeWindowBuffer * buffer,bool useIdentityTransform,bool forSystem,int * outSyncFd,bool regionSampling)332 auto captureScreenImplLocked(const RenderArea& renderArea, 333 SurfaceFlinger::TraverseLayersFunction traverseLayers, 334 ANativeWindowBuffer* buffer, bool useIdentityTransform, 335 bool forSystem, int* outSyncFd, bool regionSampling) { 336 bool ignored; 337 return mFlinger->captureScreenImplLocked(renderArea, traverseLayers, buffer, 338 useIdentityTransform, forSystem, outSyncFd, 339 regionSampling, ignored); 340 } 341 traverseLayersInDisplay(const sp<const DisplayDevice> & display,const LayerVector::Visitor & visitor)342 auto traverseLayersInDisplay(const sp<const DisplayDevice>& display, 343 const LayerVector::Visitor& visitor) { 344 return mFlinger->SurfaceFlinger::traverseLayersInDisplay(display, visitor); 345 } 346 getDisplayNativePrimaries(const sp<IBinder> & displayToken,ui::DisplayPrimaries & primaries)347 auto getDisplayNativePrimaries(const sp<IBinder>& displayToken, 348 ui::DisplayPrimaries &primaries) { 349 return mFlinger->SurfaceFlinger::getDisplayNativePrimaries(displayToken, primaries); 350 } 351 getTransactionQueue()352 auto& getTransactionQueue() { return mFlinger->mTransactionQueues; } 353 setTransactionState(const Vector<ComposerState> & states,const Vector<DisplayState> & displays,uint32_t flags,const sp<IBinder> & applyToken,const InputWindowCommands & inputWindowCommands,int64_t desiredPresentTime,const client_cache_t & uncacheBuffer,bool hasListenerCallbacks,std::vector<ListenerCallbacks> & listenerCallbacks)354 auto setTransactionState(const Vector<ComposerState>& states, 355 const Vector<DisplayState>& displays, uint32_t flags, 356 const sp<IBinder>& applyToken, 357 const InputWindowCommands& inputWindowCommands, 358 int64_t desiredPresentTime, const client_cache_t& uncacheBuffer, 359 bool hasListenerCallbacks, 360 std::vector<ListenerCallbacks>& listenerCallbacks) { 361 return mFlinger->setTransactionState(states, displays, flags, applyToken, 362 inputWindowCommands, desiredPresentTime, uncacheBuffer, 363 hasListenerCallbacks, listenerCallbacks); 364 } 365 flushTransactionQueues()366 auto flushTransactionQueues() { return mFlinger->flushTransactionQueues(); }; 367 368 /* ------------------------------------------------------------------------ 369 * Read-only access to private data to assert post-conditions. 370 */ 371 getAnimFrameTracker()372 const auto& getAnimFrameTracker() const { return mFlinger->mAnimFrameTracker; } getHasPoweredOff()373 const auto& getHasPoweredOff() const { return mFlinger->mHasPoweredOff; } getVisibleRegionsDirty()374 const auto& getVisibleRegionsDirty() const { return mFlinger->mVisibleRegionsDirty; } getHwComposer()375 auto& getHwComposer() const { 376 return static_cast<impl::HWComposer&>(mFlinger->getHwComposer()); 377 } getCompositionEngine()378 auto& getCompositionEngine() const { return mFlinger->getCompositionEngine(); } 379 getCompositorTiming()380 const auto& getCompositorTiming() const { return mFlinger->getBE().mCompositorTiming; } 381 382 /* ------------------------------------------------------------------------ 383 * Read-write access to private data to set up preconditions and assert 384 * post-conditions. 385 */ 386 mutableHasWideColorDisplay()387 auto& mutableHasWideColorDisplay() { return SurfaceFlinger::hasWideColorDisplay; } mutableUseColorManagement()388 auto& mutableUseColorManagement() { return SurfaceFlinger::useColorManagement; } 389 mutableCurrentState()390 auto& mutableCurrentState() { return mFlinger->mCurrentState; } mutableDisplayColorSetting()391 auto& mutableDisplayColorSetting() { return mFlinger->mDisplayColorSetting; } mutableDisplays()392 auto& mutableDisplays() { return mFlinger->mDisplays; } mutableDrawingState()393 auto& mutableDrawingState() { return mFlinger->mDrawingState; } mutableEventQueue()394 auto& mutableEventQueue() { return mFlinger->mEventQueue; } mutableGeometryInvalid()395 auto& mutableGeometryInvalid() { return mFlinger->mGeometryInvalid; } mutableInterceptor()396 auto& mutableInterceptor() { return mFlinger->mInterceptor; } mutableMainThreadId()397 auto& mutableMainThreadId() { return mFlinger->mMainThreadId; } mutablePendingHotplugEvents()398 auto& mutablePendingHotplugEvents() { return mFlinger->mPendingHotplugEvents; } mutablePhysicalDisplayTokens()399 auto& mutablePhysicalDisplayTokens() { return mFlinger->mPhysicalDisplayTokens; } mutableTexturePool()400 auto& mutableTexturePool() { return mFlinger->mTexturePool; } mutableTransactionFlags()401 auto& mutableTransactionFlags() { return mFlinger->mTransactionFlags; } mutableUseHwcVirtualDisplays()402 auto& mutableUseHwcVirtualDisplays() { return mFlinger->mUseHwcVirtualDisplays; } mutablePowerAdvisor()403 auto& mutablePowerAdvisor() { return mFlinger->mPowerAdvisor; } mutableDebugDisableHWC()404 auto& mutableDebugDisableHWC() { return mFlinger->mDebugDisableHWC; } 405 mutableComposerSequenceId()406 auto& mutableComposerSequenceId() { return mFlinger->getBE().mComposerSequenceId; } mutableHwcDisplayData()407 auto& mutableHwcDisplayData() { return getHwComposer().mDisplayData; } mutableHwcPhysicalDisplayIdMap()408 auto& mutableHwcPhysicalDisplayIdMap() { return getHwComposer().mPhysicalDisplayIdMap; } mutableInternalHwcDisplayId()409 auto& mutableInternalHwcDisplayId() { return getHwComposer().mInternalHwcDisplayId; } mutableExternalHwcDisplayId()410 auto& mutableExternalHwcDisplayId() { return getHwComposer().mExternalHwcDisplayId; } mutableUseFrameRateApi()411 auto& mutableUseFrameRateApi() { return mFlinger->useFrameRateApi; } 412 fromHandle(const sp<IBinder> & handle)413 auto fromHandle(const sp<IBinder>& handle) { 414 return mFlinger->fromHandle(handle); 415 } 416 ~TestableSurfaceFlinger()417 ~TestableSurfaceFlinger() { 418 // All these pointer and container clears help ensure that GMock does 419 // not report a leaked object, since the SurfaceFlinger instance may 420 // still be referenced by something despite our best efforts to destroy 421 // it after each test is done. 422 mutableDisplays().clear(); 423 mutableCurrentState().displays.clear(); 424 mutableDrawingState().displays.clear(); 425 mutableEventQueue().reset(); 426 mutableInterceptor().reset(); 427 mFlinger->mScheduler.reset(); 428 mFlinger->mCompositionEngine->setHwComposer(std::unique_ptr<HWComposer>()); 429 mFlinger->mCompositionEngine->setRenderEngine( 430 std::unique_ptr<renderengine::RenderEngine>()); 431 } 432 433 /* ------------------------------------------------------------------------ 434 * Wrapper classes for Read-write access to private data to set up 435 * preconditions and assert post-conditions. 436 */ 437 struct HWC2Display : public HWC2::impl::Display { HWC2DisplayHWC2Display438 HWC2Display(Hwc2::Composer& composer, 439 const std::unordered_set<hal::Capability>& capabilities, hal::HWDisplayId id, 440 hal::DisplayType type) 441 : HWC2::impl::Display(composer, capabilities, id, type) {} ~HWC2DisplayHWC2Display442 ~HWC2Display() { 443 // Prevents a call to disable vsyncs. 444 mType = hal::DisplayType::INVALID; 445 } 446 mutableIsConnectedHWC2Display447 auto& mutableIsConnected() { return this->mIsConnected; } mutableConfigsHWC2Display448 auto& mutableConfigs() { return this->mConfigs; } mutableLayersHWC2Display449 auto& mutableLayers() { return this->mLayers; } 450 }; 451 452 class FakeHwcDisplayInjector { 453 public: 454 static constexpr hal::HWDisplayId DEFAULT_HWC_DISPLAY_ID = 1000; 455 static constexpr int32_t DEFAULT_WIDTH = 1920; 456 static constexpr int32_t DEFAULT_HEIGHT = 1280; 457 static constexpr int32_t DEFAULT_REFRESH_RATE = 16'666'666; 458 static constexpr int32_t DEFAULT_CONFIG_GROUP = 7; 459 static constexpr int32_t DEFAULT_DPI = 320; 460 static constexpr hal::HWConfigId DEFAULT_ACTIVE_CONFIG = 0; 461 static constexpr hal::PowerMode DEFAULT_POWER_MODE = hal::PowerMode::ON; 462 FakeHwcDisplayInjector(DisplayId displayId,hal::DisplayType hwcDisplayType,bool isPrimary)463 FakeHwcDisplayInjector(DisplayId displayId, hal::DisplayType hwcDisplayType, bool isPrimary) 464 : mDisplayId(displayId), mHwcDisplayType(hwcDisplayType), mIsPrimary(isPrimary) {} 465 setHwcDisplayId(hal::HWDisplayId displayId)466 auto& setHwcDisplayId(hal::HWDisplayId displayId) { 467 mHwcDisplayId = displayId; 468 return *this; 469 } 470 setWidth(int32_t width)471 auto& setWidth(int32_t width) { 472 mWidth = width; 473 return *this; 474 } 475 setHeight(int32_t height)476 auto& setHeight(int32_t height) { 477 mHeight = height; 478 return *this; 479 } 480 setRefreshRate(int32_t refreshRate)481 auto& setRefreshRate(int32_t refreshRate) { 482 mRefreshRate = refreshRate; 483 return *this; 484 } 485 setDpiX(int32_t dpi)486 auto& setDpiX(int32_t dpi) { 487 mDpiX = dpi; 488 return *this; 489 } 490 setDpiY(int32_t dpi)491 auto& setDpiY(int32_t dpi) { 492 mDpiY = dpi; 493 return *this; 494 } 495 setActiveConfig(hal::HWConfigId config)496 auto& setActiveConfig(hal::HWConfigId config) { 497 mActiveConfig = config; 498 return *this; 499 } 500 setCapabilities(const std::unordered_set<hal::Capability> * capabilities)501 auto& setCapabilities(const std::unordered_set<hal::Capability>* capabilities) { 502 mCapabilities = capabilities; 503 return *this; 504 } 505 setPowerMode(hal::PowerMode mode)506 auto& setPowerMode(hal::PowerMode mode) { 507 mPowerMode = mode; 508 return *this; 509 } 510 inject(TestableSurfaceFlinger * flinger,Hwc2::Composer * composer)511 void inject(TestableSurfaceFlinger* flinger, Hwc2::Composer* composer) { 512 static const std::unordered_set<hal::Capability> defaultCapabilities; 513 if (mCapabilities == nullptr) mCapabilities = &defaultCapabilities; 514 515 // Caution - Make sure that any values passed by reference here do 516 // not refer to an instance owned by FakeHwcDisplayInjector. This 517 // class has temporary lifetime, while the constructed HWC2::Display 518 // is much longer lived. 519 auto display = std::make_unique<HWC2Display>(*composer, *mCapabilities, mHwcDisplayId, 520 mHwcDisplayType); 521 522 auto config = HWC2::Display::Config::Builder(*display, mActiveConfig); 523 config.setWidth(mWidth); 524 config.setHeight(mHeight); 525 config.setVsyncPeriod(mRefreshRate); 526 config.setDpiX(mDpiX); 527 config.setDpiY(mDpiY); 528 config.setConfigGroup(mConfigGroup); 529 display->mutableConfigs().emplace(static_cast<int32_t>(mActiveConfig), config.build()); 530 display->mutableIsConnected() = true; 531 display->setPowerMode(mPowerMode); 532 533 flinger->mutableHwcDisplayData()[mDisplayId].hwcDisplay = std::move(display); 534 535 if (mHwcDisplayType == hal::DisplayType::PHYSICAL) { 536 flinger->mutableHwcPhysicalDisplayIdMap().emplace(mHwcDisplayId, mDisplayId); 537 (mIsPrimary ? flinger->mutableInternalHwcDisplayId() 538 : flinger->mutableExternalHwcDisplayId()) = mHwcDisplayId; 539 } 540 } 541 542 private: 543 const DisplayId mDisplayId; 544 const hal::DisplayType mHwcDisplayType; 545 const bool mIsPrimary; 546 547 hal::HWDisplayId mHwcDisplayId = DEFAULT_HWC_DISPLAY_ID; 548 int32_t mWidth = DEFAULT_WIDTH; 549 int32_t mHeight = DEFAULT_HEIGHT; 550 int32_t mRefreshRate = DEFAULT_REFRESH_RATE; 551 int32_t mDpiX = DEFAULT_DPI; 552 int32_t mConfigGroup = DEFAULT_CONFIG_GROUP; 553 int32_t mDpiY = DEFAULT_DPI; 554 hal::HWConfigId mActiveConfig = DEFAULT_ACTIVE_CONFIG; 555 hal::PowerMode mPowerMode = DEFAULT_POWER_MODE; 556 const std::unordered_set<hal::Capability>* mCapabilities = nullptr; 557 }; 558 559 class FakeDisplayDeviceInjector { 560 public: FakeDisplayDeviceInjector(TestableSurfaceFlinger & flinger,std::shared_ptr<compositionengine::Display> compositionDisplay,std::optional<DisplayConnectionType> connectionType,std::optional<hal::HWDisplayId> hwcDisplayId,bool isPrimary)561 FakeDisplayDeviceInjector(TestableSurfaceFlinger& flinger, 562 std::shared_ptr<compositionengine::Display> compositionDisplay, 563 std::optional<DisplayConnectionType> connectionType, 564 std::optional<hal::HWDisplayId> hwcDisplayId, bool isPrimary) 565 : mFlinger(flinger), 566 mCreationArgs(flinger.mFlinger.get(), mDisplayToken, compositionDisplay), 567 mHwcDisplayId(hwcDisplayId) { 568 mCreationArgs.connectionType = connectionType; 569 mCreationArgs.isPrimary = isPrimary; 570 } 571 token()572 sp<IBinder> token() const { return mDisplayToken; } 573 mutableDrawingDisplayState()574 DisplayDeviceState& mutableDrawingDisplayState() { 575 return mFlinger.mutableDrawingState().displays.editValueFor(mDisplayToken); 576 } 577 mutableCurrentDisplayState()578 DisplayDeviceState& mutableCurrentDisplayState() { 579 return mFlinger.mutableCurrentState().displays.editValueFor(mDisplayToken); 580 } 581 getDrawingDisplayState()582 const auto& getDrawingDisplayState() { 583 return mFlinger.mutableDrawingState().displays.valueFor(mDisplayToken); 584 } 585 getCurrentDisplayState()586 const auto& getCurrentDisplayState() { 587 return mFlinger.mutableCurrentState().displays.valueFor(mDisplayToken); 588 } 589 mutableDisplayDevice()590 auto& mutableDisplayDevice() { return mFlinger.mutableDisplays()[mDisplayToken]; } 591 setNativeWindow(const sp<ANativeWindow> & nativeWindow)592 auto& setNativeWindow(const sp<ANativeWindow>& nativeWindow) { 593 mCreationArgs.nativeWindow = nativeWindow; 594 return *this; 595 } 596 setDisplaySurface(const sp<compositionengine::DisplaySurface> & displaySurface)597 auto& setDisplaySurface(const sp<compositionengine::DisplaySurface>& displaySurface) { 598 mCreationArgs.displaySurface = displaySurface; 599 return *this; 600 } 601 setSecure(bool secure)602 auto& setSecure(bool secure) { 603 mCreationArgs.isSecure = secure; 604 return *this; 605 } 606 setPowerMode(hal::PowerMode mode)607 auto& setPowerMode(hal::PowerMode mode) { 608 mCreationArgs.initialPowerMode = mode; 609 return *this; 610 } 611 setHwcColorModes(const std::unordered_map<ui::ColorMode,std::vector<ui::RenderIntent>> hwcColorModes)612 auto& setHwcColorModes( 613 const std::unordered_map<ui::ColorMode, std::vector<ui::RenderIntent>> 614 hwcColorModes) { 615 mCreationArgs.hwcColorModes = hwcColorModes; 616 return *this; 617 } 618 setHasWideColorGamut(bool hasWideColorGamut)619 auto& setHasWideColorGamut(bool hasWideColorGamut) { 620 mCreationArgs.hasWideColorGamut = hasWideColorGamut; 621 return *this; 622 } 623 setPhysicalOrientation(ui::Rotation orientation)624 auto& setPhysicalOrientation(ui::Rotation orientation) { 625 mCreationArgs.physicalOrientation = orientation; 626 return *this; 627 } 628 inject()629 sp<DisplayDevice> inject() { 630 const auto displayId = mCreationArgs.compositionDisplay->getDisplayId(); 631 632 DisplayDeviceState state; 633 if (const auto type = mCreationArgs.connectionType) { 634 LOG_ALWAYS_FATAL_IF(!displayId); 635 LOG_ALWAYS_FATAL_IF(!mHwcDisplayId); 636 state.physical = {*displayId, *type, *mHwcDisplayId}; 637 } 638 639 state.isSecure = mCreationArgs.isSecure; 640 641 sp<DisplayDevice> device = new DisplayDevice(mCreationArgs); 642 mFlinger.mutableDisplays().emplace(mDisplayToken, device); 643 mFlinger.mutableCurrentState().displays.add(mDisplayToken, state); 644 mFlinger.mutableDrawingState().displays.add(mDisplayToken, state); 645 646 if (const auto& physical = state.physical) { 647 mFlinger.mutablePhysicalDisplayTokens()[physical->id] = mDisplayToken; 648 } 649 650 return device; 651 } 652 653 private: 654 TestableSurfaceFlinger& mFlinger; 655 sp<BBinder> mDisplayToken = new BBinder(); 656 DisplayDeviceCreationArgs mCreationArgs; 657 const std::optional<hal::HWDisplayId> mHwcDisplayId; 658 }; 659 660 surfaceflinger::test::Factory mFactory; 661 sp<SurfaceFlinger> mFlinger = new SurfaceFlinger(mFactory, SurfaceFlinger::SkipInitialization); 662 TestableScheduler* mScheduler = nullptr; 663 Hwc2::mock::Display mDisplay; 664 }; 665 666 } // namespace android 667