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::scheduler { 32 33 class TestableScheduler : public Scheduler, private ICompositor { 34 public: TestableScheduler(std::shared_ptr<RefreshRateConfigs> configs,ISchedulerCallback & callback)35 TestableScheduler(std::shared_ptr<RefreshRateConfigs> configs, ISchedulerCallback& callback) 36 : TestableScheduler(std::make_unique<mock::VsyncController>(), 37 std::make_unique<mock::VSyncTracker>(), std::move(configs), 38 callback) {} 39 TestableScheduler(std::unique_ptr<VsyncController> controller,std::unique_ptr<VSyncTracker> tracker,std::shared_ptr<RefreshRateConfigs> configs,ISchedulerCallback & callback)40 TestableScheduler(std::unique_ptr<VsyncController> controller, 41 std::unique_ptr<VSyncTracker> tracker, 42 std::shared_ptr<RefreshRateConfigs> configs, ISchedulerCallback& callback) 43 : Scheduler(*this, callback, Feature::kContentDetection) { 44 mVsyncSchedule.emplace(VsyncSchedule(std::move(tracker), nullptr, std::move(controller))); 45 setRefreshRateConfigs(std::move(configs)); 46 47 ON_CALL(*this, postMessage).WillByDefault([](sp<MessageHandler>&& handler) { 48 // Execute task to prevent broken promise exception on destruction. 49 handler->handleMessage(Message()); 50 }); 51 } 52 53 MOCK_METHOD(void, scheduleFrame, (), (override)); 54 MOCK_METHOD(void, postMessage, (sp<MessageHandler>&&), (override)); 55 56 // Used to inject mock event thread. createConnection(std::unique_ptr<EventThread> eventThread)57 ConnectionHandle createConnection(std::unique_ptr<EventThread> eventThread) { 58 return Scheduler::createConnection(std::move(eventThread)); 59 } 60 61 /* ------------------------------------------------------------------------ 62 * Read-write access to private data to set up preconditions and assert 63 * post-conditions. 64 */ mutablePrimaryHWVsyncEnabled()65 auto& mutablePrimaryHWVsyncEnabled() { return mPrimaryHWVsyncEnabled; } mutableHWVsyncAvailable()66 auto& mutableHWVsyncAvailable() { return mHWVsyncAvailable; } 67 mutableLayerHistory()68 auto& mutableLayerHistory() { return mLayerHistory; } 69 layerHistorySize()70 size_t layerHistorySize() NO_THREAD_SAFETY_ANALYSIS { 71 return mLayerHistory.mActiveLayerInfos.size() + mLayerHistory.mInactiveLayerInfos.size(); 72 } 73 refreshRateConfigs()74 auto refreshRateConfigs() { return holdRefreshRateConfigs(); } 75 getNumActiveLayers()76 size_t getNumActiveLayers() NO_THREAD_SAFETY_ANALYSIS { 77 return mLayerHistory.mActiveLayerInfos.size(); 78 } 79 replaceTouchTimer(int64_t millis)80 void replaceTouchTimer(int64_t millis) { 81 if (mTouchTimer) { 82 mTouchTimer.reset(); 83 } 84 mTouchTimer.emplace( 85 "Testable Touch timer", std::chrono::milliseconds(millis), 86 [this] { touchTimerCallback(TimerState::Reset); }, 87 [this] { touchTimerCallback(TimerState::Expired); }); 88 mTouchTimer->start(); 89 } 90 isTouchActive()91 bool isTouchActive() { 92 std::lock_guard<std::mutex> lock(mPolicyLock); 93 return mPolicy.touch == Scheduler::TouchState::Active; 94 } 95 dispatchCachedReportedMode()96 void dispatchCachedReportedMode() { 97 std::lock_guard<std::mutex> lock(mPolicyLock); 98 return Scheduler::dispatchCachedReportedMode(); 99 } 100 clearCachedReportedMode()101 void clearCachedReportedMode() { 102 std::lock_guard<std::mutex> lock(mPolicyLock); 103 mPolicy.cachedModeChangedParams.reset(); 104 } 105 onNonPrimaryDisplayModeChanged(ConnectionHandle handle,DisplayModePtr mode)106 void onNonPrimaryDisplayModeChanged(ConnectionHandle handle, DisplayModePtr mode) { 107 return Scheduler::onNonPrimaryDisplayModeChanged(handle, mode); 108 } 109 110 private: 111 // ICompositor overrides: commit(nsecs_t,int64_t,nsecs_t)112 bool commit(nsecs_t, int64_t, nsecs_t) override { return false; } composite(nsecs_t,int64_t)113 void composite(nsecs_t, int64_t) override {} sample()114 void sample() override {} 115 }; 116 117 } // namespace android::scheduler 118