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 <gmock/gmock.h>
20 #include <gui/ISurfaceComposer.h>
21 
22 #include "Scheduler/DispSync.h"
23 #include "Scheduler/EventThread.h"
24 #include "Scheduler/LayerHistory.h"
25 #include "Scheduler/Scheduler.h"
26 
27 namespace android {
28 
29 class TestableScheduler : public Scheduler, private ISchedulerCallback {
30 public:
TestableScheduler(const scheduler::RefreshRateConfigs & configs,bool useContentDetectionV2)31     TestableScheduler(const scheduler::RefreshRateConfigs& configs, bool useContentDetectionV2)
32           : Scheduler([](bool) {}, configs, *this, useContentDetectionV2, true) {
33         if (mUseContentDetectionV2) {
34             mLayerHistory = std::make_unique<scheduler::impl::LayerHistoryV2>(configs);
35         } else {
36             mLayerHistory = std::make_unique<scheduler::impl::LayerHistory>();
37         }
38     }
39 
TestableScheduler(std::unique_ptr<DispSync> primaryDispSync,std::unique_ptr<EventControlThread> eventControlThread,const scheduler::RefreshRateConfigs & configs,bool useContentDetectionV2)40     TestableScheduler(std::unique_ptr<DispSync> primaryDispSync,
41                       std::unique_ptr<EventControlThread> eventControlThread,
42                       const scheduler::RefreshRateConfigs& configs, bool useContentDetectionV2)
43           : Scheduler(std::move(primaryDispSync), std::move(eventControlThread), configs, *this,
44                       useContentDetectionV2, true) {
45         if (mUseContentDetectionV2) {
46             mLayerHistory = std::make_unique<scheduler::impl::LayerHistoryV2>(configs);
47         } else {
48             mLayerHistory = std::make_unique<scheduler::impl::LayerHistory>();
49         }
50     }
51 
52     // Used to inject mock event thread.
createConnection(std::unique_ptr<EventThread> eventThread)53     ConnectionHandle createConnection(std::unique_ptr<EventThread> eventThread) {
54         return Scheduler::createConnection(std::move(eventThread));
55     }
56 
layerHistorySize()57     size_t layerHistorySize() const NO_THREAD_SAFETY_ANALYSIS {
58         if (mUseContentDetectionV2) {
59             return static_cast<scheduler::impl::LayerHistoryV2*>(mLayerHistory.get())
60                     ->mLayerInfos.size();
61         } else {
62             return static_cast<scheduler::impl::LayerHistory*>(mLayerHistory.get())
63                     ->mLayerInfos.size();
64         }
65     }
66 
67     /* ------------------------------------------------------------------------
68      * Read-write access to private data to set up preconditions and assert
69      * post-conditions.
70      */
mutablePrimaryHWVsyncEnabled()71     auto& mutablePrimaryHWVsyncEnabled() { return mPrimaryHWVsyncEnabled; }
mutableEventControlThread()72     auto& mutableEventControlThread() { return mEventControlThread; }
mutablePrimaryDispSync()73     auto& mutablePrimaryDispSync() { return mPrimaryDispSync; }
mutableHWVsyncAvailable()74     auto& mutableHWVsyncAvailable() { return mHWVsyncAvailable; }
mutableLayerHistory()75     auto mutableLayerHistory() {
76         return static_cast<scheduler::impl::LayerHistory*>(mLayerHistory.get());
77     }
mutableLayerHistoryV2()78     auto mutableLayerHistoryV2() {
79         return static_cast<scheduler::impl::LayerHistoryV2*>(mLayerHistory.get());
80     }
81 
~TestableScheduler()82     ~TestableScheduler() {
83         // All these pointer and container clears help ensure that GMock does
84         // not report a leaked object, since the Scheduler instance may
85         // still be referenced by something despite our best efforts to destroy
86         // it after each test is done.
87         mutableEventControlThread().reset();
88         mutablePrimaryDispSync().reset();
89         mConnections.clear();
90     }
91 
92 private:
changeRefreshRate(const RefreshRate &,ConfigEvent)93     void changeRefreshRate(const RefreshRate&, ConfigEvent) override {}
repaintEverythingForHWC()94     void repaintEverythingForHWC() override {}
kernelTimerChanged(bool)95     void kernelTimerChanged(bool /*expired*/) override {}
96 };
97 
98 } // namespace android
99