1 /* 2 * Copyright 2019 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 <Scheduler/Scheduler.h> 20 #include <gmock/gmock.h> 21 #include <gui/ISurfaceComposer.h> 22 23 #include "Scheduler/EventThread.h" 24 #include "Scheduler/LayerHistory.h" 25 #include "Scheduler/Scheduler.h" 26 #include "Scheduler/VSyncTracker.h" 27 #include "Scheduler/VsyncController.h" 28 #include "mock/MockVSyncTracker.h" 29 #include "mock/MockVsyncController.h" 30 31 namespace android { 32 33 class TestableScheduler : public Scheduler { 34 public: TestableScheduler(const scheduler::RefreshRateConfigs & configs,ISchedulerCallback & callback)35 TestableScheduler(const scheduler::RefreshRateConfigs& configs, ISchedulerCallback& callback) 36 : TestableScheduler(std::make_unique<mock::VsyncController>(), 37 std::make_unique<mock::VSyncTracker>(), configs, callback) {} 38 TestableScheduler(std::unique_ptr<scheduler::VsyncController> vsyncController,std::unique_ptr<scheduler::VSyncTracker> vsyncTracker,const scheduler::RefreshRateConfigs & configs,ISchedulerCallback & callback)39 TestableScheduler(std::unique_ptr<scheduler::VsyncController> vsyncController, 40 std::unique_ptr<scheduler::VSyncTracker> vsyncTracker, 41 const scheduler::RefreshRateConfigs& configs, ISchedulerCallback& callback) 42 : Scheduler({std::move(vsyncController), std::move(vsyncTracker), nullptr}, configs, 43 callback, createLayerHistory(configs), 44 {.supportKernelTimer = false, .useContentDetection = true}) {} 45 46 // Used to inject mock event thread. createConnection(std::unique_ptr<EventThread> eventThread)47 ConnectionHandle createConnection(std::unique_ptr<EventThread> eventThread) { 48 return Scheduler::createConnection(std::move(eventThread)); 49 } 50 51 /* ------------------------------------------------------------------------ 52 * Read-write access to private data to set up preconditions and assert 53 * post-conditions. 54 */ mutablePrimaryHWVsyncEnabled()55 auto& mutablePrimaryHWVsyncEnabled() { return mPrimaryHWVsyncEnabled; } mutableHWVsyncAvailable()56 auto& mutableHWVsyncAvailable() { return mHWVsyncAvailable; } 57 hasLayerHistory()58 bool hasLayerHistory() const { return static_cast<bool>(mLayerHistory); } 59 mutableLayerHistory()60 auto* mutableLayerHistory() { return mLayerHistory.get(); } 61 layerHistorySize()62 size_t layerHistorySize() NO_THREAD_SAFETY_ANALYSIS { 63 if (!mLayerHistory) return 0; 64 return mutableLayerHistory()->mLayerInfos.size(); 65 } 66 getNumActiveLayers()67 size_t getNumActiveLayers() NO_THREAD_SAFETY_ANALYSIS { 68 if (!mLayerHistory) return 0; 69 return mutableLayerHistory()->mActiveLayersEnd; 70 } 71 replaceTouchTimer(int64_t millis)72 void replaceTouchTimer(int64_t millis) { 73 if (mTouchTimer) { 74 mTouchTimer.reset(); 75 } 76 mTouchTimer.emplace( 77 "Testable Touch timer", std::chrono::milliseconds(millis), 78 [this] { touchTimerCallback(TimerState::Reset); }, 79 [this] { touchTimerCallback(TimerState::Expired); }); 80 mTouchTimer->start(); 81 } 82 isTouchActive()83 bool isTouchActive() { 84 std::lock_guard<std::mutex> lock(mFeatureStateLock); 85 return mFeatures.touch == Scheduler::TouchState::Active; 86 } 87 dispatchCachedReportedMode()88 void dispatchCachedReportedMode() { 89 std::lock_guard<std::mutex> lock(mFeatureStateLock); 90 return Scheduler::dispatchCachedReportedMode(); 91 } 92 clearOptionalFieldsInFeatures()93 void clearOptionalFieldsInFeatures() { 94 std::lock_guard<std::mutex> lock(mFeatureStateLock); 95 mFeatures.cachedModeChangedParams.reset(); 96 } 97 onNonPrimaryDisplayModeChanged(ConnectionHandle handle,PhysicalDisplayId displayId,DisplayModeId modeId,nsecs_t vsyncPeriod)98 void onNonPrimaryDisplayModeChanged(ConnectionHandle handle, PhysicalDisplayId displayId, 99 DisplayModeId modeId, nsecs_t vsyncPeriod) { 100 return Scheduler::onNonPrimaryDisplayModeChanged(handle, displayId, modeId, vsyncPeriod); 101 } 102 ~TestableScheduler()103 ~TestableScheduler() { 104 // All these pointer and container clears help ensure that GMock does 105 // not report a leaked object, since the Scheduler instance may 106 // still be referenced by something despite our best efforts to destroy 107 // it after each test is done. 108 mVsyncSchedule.controller.reset(); 109 mConnections.clear(); 110 } 111 }; 112 113 } // namespace android 114