blob: ebb2d76d1e8b05db41a80f78c6616177064748b4 [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,
Ady Abraham49cb7d52020-07-22 18:37:07 -070045 .useContentDetectionV2 = useContentDetectionV2}) {}
Ana Krulecafb45842019-02-13 13:33:03 -080046
Dominik Laskowski98041832019-08-01 18:35:59 -070047 // Used to inject mock event thread.
48 ConnectionHandle createConnection(std::unique_ptr<EventThread> eventThread) {
Steven Thomas2bbaabe2019-08-28 16:08:35 -070049 return Scheduler::createConnection(std::move(eventThread));
Ana Krulecafb45842019-02-13 13:33:03 -080050 }
51
52 /* ------------------------------------------------------------------------
53 * Read-write access to private data to set up preconditions and assert
54 * post-conditions.
55 */
56 auto& mutablePrimaryHWVsyncEnabled() { return mPrimaryHWVsyncEnabled; }
Ana Krulecafb45842019-02-13 13:33:03 -080057 auto& mutableHWVsyncAvailable() { return mHWVsyncAvailable; }
Lais Andrade3a6e47d2020-04-02 11:20:16 +010058
Dominik Laskowski983f2b52020-06-25 16:54:06 -070059 bool hasLayerHistory() const { return static_cast<bool>(mLayerHistory); }
60
61 auto* mutableLayerHistory() {
Dominik Laskowski8b01cc02020-07-14 19:02:41 -070062 LOG_ALWAYS_FATAL_IF(mOptions.useContentDetectionV2);
Ady Abrahame3ed2f92020-01-06 17:01:28 -080063 return static_cast<scheduler::impl::LayerHistory*>(mLayerHistory.get());
64 }
Lais Andrade3a6e47d2020-04-02 11:20:16 +010065
Dominik Laskowski983f2b52020-06-25 16:54:06 -070066 auto* mutableLayerHistoryV2() {
Dominik Laskowski8b01cc02020-07-14 19:02:41 -070067 LOG_ALWAYS_FATAL_IF(!mOptions.useContentDetectionV2);
Ady Abraham8a82ba62020-01-17 12:43:17 -080068 return static_cast<scheduler::impl::LayerHistoryV2*>(mLayerHistory.get());
69 }
Ana Krulecafb45842019-02-13 13:33:03 -080070
Dominik Laskowski983f2b52020-06-25 16:54:06 -070071 size_t layerHistorySize() NO_THREAD_SAFETY_ANALYSIS {
72 if (!mLayerHistory) return 0;
Dominik Laskowski8b01cc02020-07-14 19:02:41 -070073 return mOptions.useContentDetectionV2 ? mutableLayerHistoryV2()->mLayerInfos.size()
74 : mutableLayerHistory()->mLayerInfos.size();
Dominik Laskowski983f2b52020-06-25 16:54:06 -070075 }
76
Lais Andrade3a6e47d2020-04-02 11:20:16 +010077 void replaceTouchTimer(int64_t millis) {
78 if (mTouchTimer) {
79 mTouchTimer.reset();
80 }
81 mTouchTimer.emplace(
82 std::chrono::milliseconds(millis),
83 [this] { touchTimerCallback(TimerState::Reset); },
84 [this] { touchTimerCallback(TimerState::Expired); });
85 mTouchTimer->start();
86 }
87
88 bool isTouchActive() {
89 std::lock_guard<std::mutex> lock(mFeatureStateLock);
90 return mFeatures.touch == Scheduler::TouchState::Active;
91 }
92
Ana Krulecafb45842019-02-13 13:33:03 -080093 ~TestableScheduler() {
94 // All these pointer and container clears help ensure that GMock does
95 // not report a leaked object, since the Scheduler instance may
96 // still be referenced by something despite our best efforts to destroy
97 // it after each test is done.
Dominik Laskowski8b01cc02020-07-14 19:02:41 -070098 mVsyncSchedule.sync.reset();
Ana Krulecafb45842019-02-13 13:33:03 -080099 mConnections.clear();
Dominik Laskowski7c9dbf92019-08-01 17:57:31 -0700100 }
Ana Krulecafb45842019-02-13 13:33:03 -0800101};
102
103} // namespace android