Ana Krulec | afb4584 | 2019-02-13 13:33:03 -0800 | [diff] [blame] | 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> |
Ady Abraham | 0f4a1b1 | 2019-06-04 16:04:04 -0700 | [diff] [blame] | 20 | #include <gui/ISurfaceComposer.h> |
Ana Krulec | afb4584 | 2019-02-13 13:33:03 -0800 | [diff] [blame] | 21 | |
Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame] | 22 | #include "Scheduler/DispSync.h" |
Ana Krulec | afb4584 | 2019-02-13 13:33:03 -0800 | [diff] [blame] | 23 | #include "Scheduler/EventThread.h" |
Dominik Laskowski | 49cea51 | 2019-11-12 14:13:23 -0800 | [diff] [blame] | 24 | #include "Scheduler/LayerHistory.h" |
Ana Krulec | afb4584 | 2019-02-13 13:33:03 -0800 | [diff] [blame] | 25 | #include "Scheduler/Scheduler.h" |
Dominik Laskowski | 8b01cc0 | 2020-07-14 19:02:41 -0700 | [diff] [blame^] | 26 | #include "Scheduler/VSyncTracker.h" |
| 27 | #include "mock/MockDispSync.h" |
Ana Krulec | afb4584 | 2019-02-13 13:33:03 -0800 | [diff] [blame] | 28 | |
| 29 | namespace android { |
| 30 | |
Dominik Laskowski | 8b01cc0 | 2020-07-14 19:02:41 -0700 | [diff] [blame^] | 31 | class TestableScheduler : public Scheduler { |
Ana Krulec | afb4584 | 2019-02-13 13:33:03 -0800 | [diff] [blame] | 32 | public: |
Dominik Laskowski | 8b01cc0 | 2020-07-14 19:02:41 -0700 | [diff] [blame^] | 33 | TestableScheduler(const scheduler::RefreshRateConfigs& configs, ISchedulerCallback& callback, |
| 34 | bool useContentDetectionV2) |
| 35 | : TestableScheduler(std::make_unique<mock::DispSync>(), configs, callback, |
| 36 | useContentDetectionV2) {} |
Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame] | 37 | |
Dominik Laskowski | 7c9dbf9 | 2019-08-01 17:57:31 -0700 | [diff] [blame] | 38 | TestableScheduler(std::unique_ptr<DispSync> primaryDispSync, |
Dominik Laskowski | 8b01cc0 | 2020-07-14 19:02:41 -0700 | [diff] [blame^] | 39 | 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 Krulec | afb4584 | 2019-02-13 13:33:03 -0800 | [diff] [blame] | 47 | |
Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame] | 48 | // Used to inject mock event thread. |
| 49 | ConnectionHandle createConnection(std::unique_ptr<EventThread> eventThread) { |
Steven Thomas | 2bbaabe | 2019-08-28 16:08:35 -0700 | [diff] [blame] | 50 | return Scheduler::createConnection(std::move(eventThread)); |
Ana Krulec | afb4584 | 2019-02-13 13:33:03 -0800 | [diff] [blame] | 51 | } |
| 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 Krulec | afb4584 | 2019-02-13 13:33:03 -0800 | [diff] [blame] | 58 | auto& mutableHWVsyncAvailable() { return mHWVsyncAvailable; } |
Lais Andrade | 3a6e47d | 2020-04-02 11:20:16 +0100 | [diff] [blame] | 59 | |
Dominik Laskowski | 983f2b5 | 2020-06-25 16:54:06 -0700 | [diff] [blame] | 60 | bool hasLayerHistory() const { return static_cast<bool>(mLayerHistory); } |
| 61 | |
| 62 | auto* mutableLayerHistory() { |
Dominik Laskowski | 8b01cc0 | 2020-07-14 19:02:41 -0700 | [diff] [blame^] | 63 | LOG_ALWAYS_FATAL_IF(mOptions.useContentDetectionV2); |
Ady Abraham | e3ed2f9 | 2020-01-06 17:01:28 -0800 | [diff] [blame] | 64 | return static_cast<scheduler::impl::LayerHistory*>(mLayerHistory.get()); |
| 65 | } |
Lais Andrade | 3a6e47d | 2020-04-02 11:20:16 +0100 | [diff] [blame] | 66 | |
Dominik Laskowski | 983f2b5 | 2020-06-25 16:54:06 -0700 | [diff] [blame] | 67 | auto* mutableLayerHistoryV2() { |
Dominik Laskowski | 8b01cc0 | 2020-07-14 19:02:41 -0700 | [diff] [blame^] | 68 | LOG_ALWAYS_FATAL_IF(!mOptions.useContentDetectionV2); |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 69 | return static_cast<scheduler::impl::LayerHistoryV2*>(mLayerHistory.get()); |
| 70 | } |
Ana Krulec | afb4584 | 2019-02-13 13:33:03 -0800 | [diff] [blame] | 71 | |
Dominik Laskowski | 983f2b5 | 2020-06-25 16:54:06 -0700 | [diff] [blame] | 72 | size_t layerHistorySize() NO_THREAD_SAFETY_ANALYSIS { |
| 73 | if (!mLayerHistory) return 0; |
Dominik Laskowski | 8b01cc0 | 2020-07-14 19:02:41 -0700 | [diff] [blame^] | 74 | return mOptions.useContentDetectionV2 ? mutableLayerHistoryV2()->mLayerInfos.size() |
| 75 | : mutableLayerHistory()->mLayerInfos.size(); |
Dominik Laskowski | 983f2b5 | 2020-06-25 16:54:06 -0700 | [diff] [blame] | 76 | } |
| 77 | |
Lais Andrade | 3a6e47d | 2020-04-02 11:20:16 +0100 | [diff] [blame] | 78 | 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 Krulec | afb4584 | 2019-02-13 13:33:03 -0800 | [diff] [blame] | 94 | ~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 Laskowski | 8b01cc0 | 2020-07-14 19:02:41 -0700 | [diff] [blame^] | 99 | mVsyncSchedule.sync.reset(); |
Ana Krulec | afb4584 | 2019-02-13 13:33:03 -0800 | [diff] [blame] | 100 | mConnections.clear(); |
Dominik Laskowski | 7c9dbf9 | 2019-08-01 17:57:31 -0700 | [diff] [blame] | 101 | } |
Ana Krulec | afb4584 | 2019-02-13 13:33:03 -0800 | [diff] [blame] | 102 | }; |
| 103 | |
| 104 | } // namespace android |