blob: d78546d1fbd69c98073093732de0e66c431aaed7 [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"
Dominik Laskowski8b01cc02020-07-14 19:02:41 -070026#include "Scheduler/VSyncTracker.h"
27#include "mock/MockDispSync.h"
Ana Krulecafb45842019-02-13 13:33:03 -080028
29namespace android {
30
Dominik Laskowski8b01cc02020-07-14 19:02:41 -070031class TestableScheduler : public Scheduler {
Ana Krulecafb45842019-02-13 13:33:03 -080032public:
Dominik Laskowski8b01cc02020-07-14 19:02:41 -070033 TestableScheduler(const scheduler::RefreshRateConfigs& configs, ISchedulerCallback& callback,
34 bool useContentDetectionV2)
35 : TestableScheduler(std::make_unique<mock::DispSync>(), configs, callback,
36 useContentDetectionV2) {}
Dominik Laskowski98041832019-08-01 18:35:59 -070037
Dominik Laskowski7c9dbf92019-08-01 17:57:31 -070038 TestableScheduler(std::unique_ptr<DispSync> primaryDispSync,
Dominik Laskowski8b01cc02020-07-14 19:02:41 -070039 const scheduler::RefreshRateConfigs& configs, ISchedulerCallback& callback,
40 bool useContentDetectionV2)
41 : Scheduler({std::move(primaryDispSync), nullptr, nullptr}, configs, callback,
42 createLayerHistory(configs, useContentDetectionV2),
43 {.supportKernelTimer = false,
44 .useContentDetection = true,
45 .useContentDetectionV2 = useContentDetectionV2,
46 .useVsyncPredictor = false}) {}
Ana Krulecafb45842019-02-13 13:33:03 -080047
Dominik Laskowski98041832019-08-01 18:35:59 -070048 // Used to inject mock event thread.
49 ConnectionHandle createConnection(std::unique_ptr<EventThread> eventThread) {
Steven Thomas2bbaabe2019-08-28 16:08:35 -070050 return Scheduler::createConnection(std::move(eventThread));
Ana Krulecafb45842019-02-13 13:33:03 -080051 }
52
53 /* ------------------------------------------------------------------------
54 * Read-write access to private data to set up preconditions and assert
55 * post-conditions.
56 */
57 auto& mutablePrimaryHWVsyncEnabled() { return mPrimaryHWVsyncEnabled; }
Ana Krulecafb45842019-02-13 13:33:03 -080058 auto& mutableHWVsyncAvailable() { return mHWVsyncAvailable; }
Lais Andrade3a6e47d2020-04-02 11:20:16 +010059
Dominik Laskowski983f2b52020-06-25 16:54:06 -070060 bool hasLayerHistory() const { return static_cast<bool>(mLayerHistory); }
61
62 auto* mutableLayerHistory() {
Dominik Laskowski8b01cc02020-07-14 19:02:41 -070063 LOG_ALWAYS_FATAL_IF(mOptions.useContentDetectionV2);
Ady Abrahame3ed2f92020-01-06 17:01:28 -080064 return static_cast<scheduler::impl::LayerHistory*>(mLayerHistory.get());
65 }
Lais Andrade3a6e47d2020-04-02 11:20:16 +010066
Dominik Laskowski983f2b52020-06-25 16:54:06 -070067 auto* mutableLayerHistoryV2() {
Dominik Laskowski8b01cc02020-07-14 19:02:41 -070068 LOG_ALWAYS_FATAL_IF(!mOptions.useContentDetectionV2);
Ady Abraham8a82ba62020-01-17 12:43:17 -080069 return static_cast<scheduler::impl::LayerHistoryV2*>(mLayerHistory.get());
70 }
Ana Krulecafb45842019-02-13 13:33:03 -080071
Dominik Laskowski983f2b52020-06-25 16:54:06 -070072 size_t layerHistorySize() NO_THREAD_SAFETY_ANALYSIS {
73 if (!mLayerHistory) return 0;
Dominik Laskowski8b01cc02020-07-14 19:02:41 -070074 return mOptions.useContentDetectionV2 ? mutableLayerHistoryV2()->mLayerInfos.size()
75 : mutableLayerHistory()->mLayerInfos.size();
Dominik Laskowski983f2b52020-06-25 16:54:06 -070076 }
77
Lais Andrade3a6e47d2020-04-02 11:20:16 +010078 void replaceTouchTimer(int64_t millis) {
79 if (mTouchTimer) {
80 mTouchTimer.reset();
81 }
82 mTouchTimer.emplace(
83 std::chrono::milliseconds(millis),
84 [this] { touchTimerCallback(TimerState::Reset); },
85 [this] { touchTimerCallback(TimerState::Expired); });
86 mTouchTimer->start();
87 }
88
89 bool isTouchActive() {
90 std::lock_guard<std::mutex> lock(mFeatureStateLock);
91 return mFeatures.touch == Scheduler::TouchState::Active;
92 }
93
Ana Krulecafb45842019-02-13 13:33:03 -080094 ~TestableScheduler() {
95 // All these pointer and container clears help ensure that GMock does
96 // not report a leaked object, since the Scheduler instance may
97 // still be referenced by something despite our best efforts to destroy
98 // it after each test is done.
Dominik Laskowski8b01cc02020-07-14 19:02:41 -070099 mVsyncSchedule.sync.reset();
Ana Krulecafb45842019-02-13 13:33:03 -0800100 mConnections.clear();
Dominik Laskowski7c9dbf92019-08-01 17:57:31 -0700101 }
Ana Krulecafb45842019-02-13 13:33:03 -0800102};
103
104} // namespace android