blob: c463294de8769615a935d5d4d326ae17668df5a2 [file] [log] [blame]
Ana Krulecafb45842019-02-13 13:33:03 -08001/*
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>
Ady Abraham0f4a1b12019-06-04 16:04:04 -070020#include <gui/ISurfaceComposer.h>
Ana Krulecafb45842019-02-13 13:33:03 -080021
Dominik Laskowski98041832019-08-01 18:35:59 -070022#include "Scheduler/DispSync.h"
Ana Krulecafb45842019-02-13 13:33:03 -080023#include "Scheduler/EventThread.h"
Dominik Laskowski49cea512019-11-12 14:13:23 -080024#include "Scheduler/LayerHistory.h"
Ana Krulecafb45842019-02-13 13:33:03 -080025#include "Scheduler/Scheduler.h"
26
27namespace android {
28
Ady Abraham3a77a7b2019-12-02 18:46:59 -080029class TestableScheduler : public Scheduler, private ISchedulerCallback {
Ana Krulecafb45842019-02-13 13:33:03 -080030public:
Ady Abraham8a82ba62020-01-17 12:43:17 -080031 TestableScheduler(const scheduler::RefreshRateConfigs& configs, bool useContentDetectionV2)
Dominik Laskowski983f2b52020-06-25 16:54:06 -070032 : Scheduler([](bool) {}, configs, *this, useContentDetectionV2, true) {}
Dominik Laskowski98041832019-08-01 18:35:59 -070033
Dominik Laskowski7c9dbf92019-08-01 17:57:31 -070034 TestableScheduler(std::unique_ptr<DispSync> primaryDispSync,
35 std::unique_ptr<EventControlThread> eventControlThread,
Ady Abraham8a82ba62020-01-17 12:43:17 -080036 const scheduler::RefreshRateConfigs& configs, bool useContentDetectionV2)
37 : Scheduler(std::move(primaryDispSync), std::move(eventControlThread), configs, *this,
Dominik Laskowski983f2b52020-06-25 16:54:06 -070038 createLayerHistory(configs, useContentDetectionV2), useContentDetectionV2,
39 true) {}
Ana Krulecafb45842019-02-13 13:33:03 -080040
Dominik Laskowski98041832019-08-01 18:35:59 -070041 // Used to inject mock event thread.
42 ConnectionHandle createConnection(std::unique_ptr<EventThread> eventThread) {
Steven Thomas2bbaabe2019-08-28 16:08:35 -070043 return Scheduler::createConnection(std::move(eventThread));
Ana Krulecafb45842019-02-13 13:33:03 -080044 }
45
46 /* ------------------------------------------------------------------------
47 * Read-write access to private data to set up preconditions and assert
48 * post-conditions.
49 */
50 auto& mutablePrimaryHWVsyncEnabled() { return mPrimaryHWVsyncEnabled; }
51 auto& mutableEventControlThread() { return mEventControlThread; }
52 auto& mutablePrimaryDispSync() { return mPrimaryDispSync; }
53 auto& mutableHWVsyncAvailable() { return mHWVsyncAvailable; }
Lais Andrade3a6e47d2020-04-02 11:20:16 +010054
Dominik Laskowski983f2b52020-06-25 16:54:06 -070055 size_t refreshRateChangeCount() const { return mRefreshRateChangeCount; }
56
57 bool hasLayerHistory() const { return static_cast<bool>(mLayerHistory); }
58
59 auto* mutableLayerHistory() {
60 LOG_ALWAYS_FATAL_IF(mUseContentDetectionV2);
Ady Abrahame3ed2f92020-01-06 17:01:28 -080061 return static_cast<scheduler::impl::LayerHistory*>(mLayerHistory.get());
62 }
Lais Andrade3a6e47d2020-04-02 11:20:16 +010063
Dominik Laskowski983f2b52020-06-25 16:54:06 -070064 auto* mutableLayerHistoryV2() {
65 LOG_ALWAYS_FATAL_IF(!mUseContentDetectionV2);
Ady Abraham8a82ba62020-01-17 12:43:17 -080066 return static_cast<scheduler::impl::LayerHistoryV2*>(mLayerHistory.get());
67 }
Ana Krulecafb45842019-02-13 13:33:03 -080068
Dominik Laskowski983f2b52020-06-25 16:54:06 -070069 size_t layerHistorySize() NO_THREAD_SAFETY_ANALYSIS {
70 if (!mLayerHistory) return 0;
71 return mUseContentDetectionV2 ? mutableLayerHistoryV2()->mLayerInfos.size()
72 : mutableLayerHistory()->mLayerInfos.size();
73 }
74
Lais Andrade3a6e47d2020-04-02 11:20:16 +010075 void replaceTouchTimer(int64_t millis) {
76 if (mTouchTimer) {
77 mTouchTimer.reset();
78 }
79 mTouchTimer.emplace(
80 std::chrono::milliseconds(millis),
81 [this] { touchTimerCallback(TimerState::Reset); },
82 [this] { touchTimerCallback(TimerState::Expired); });
83 mTouchTimer->start();
84 }
85
86 bool isTouchActive() {
87 std::lock_guard<std::mutex> lock(mFeatureStateLock);
88 return mFeatures.touch == Scheduler::TouchState::Active;
89 }
90
Ana Krulecafb45842019-02-13 13:33:03 -080091 ~TestableScheduler() {
92 // All these pointer and container clears help ensure that GMock does
93 // not report a leaked object, since the Scheduler instance may
94 // still be referenced by something despite our best efforts to destroy
95 // it after each test is done.
96 mutableEventControlThread().reset();
97 mutablePrimaryDispSync().reset();
98 mConnections.clear();
Dominik Laskowski7c9dbf92019-08-01 17:57:31 -070099 }
Ady Abraham3a77a7b2019-12-02 18:46:59 -0800100
101private:
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700102 void changeRefreshRate(const RefreshRate&, ConfigEvent) override { mRefreshRateChangeCount++; }
103
Ady Abraham3a77a7b2019-12-02 18:46:59 -0800104 void repaintEverythingForHWC() override {}
Ady Abrahama09852a2020-02-20 14:23:42 -0800105 void kernelTimerChanged(bool /*expired*/) override {}
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700106
107 size_t mRefreshRateChangeCount = 0;
Ana Krulecafb45842019-02-13 13:33:03 -0800108};
109
110} // namespace android