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 | |
Ana Krulec | 6ddd261 | 2020-09-24 13:06:33 -0700 | [diff] [blame^] | 19 | #include <Scheduler/Scheduler.h> |
Ana Krulec | afb4584 | 2019-02-13 13:33:03 -0800 | [diff] [blame] | 20 | #include <gmock/gmock.h> |
Ady Abraham | 0f4a1b1 | 2019-06-04 16:04:04 -0700 | [diff] [blame] | 21 | #include <gui/ISurfaceComposer.h> |
Ana Krulec | afb4584 | 2019-02-13 13:33:03 -0800 | [diff] [blame] | 22 | |
| 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" |
Ady Abraham | 8cb2188 | 2020-08-26 18:22:05 -0700 | [diff] [blame] | 27 | #include "Scheduler/VsyncController.h" |
| 28 | #include "mock/MockVSyncTracker.h" |
| 29 | #include "mock/MockVsyncController.h" |
Ana Krulec | afb4584 | 2019-02-13 13:33:03 -0800 | [diff] [blame] | 30 | |
| 31 | namespace android { |
| 32 | |
Dominik Laskowski | 8b01cc0 | 2020-07-14 19:02:41 -0700 | [diff] [blame] | 33 | class TestableScheduler : public Scheduler { |
Ana Krulec | afb4584 | 2019-02-13 13:33:03 -0800 | [diff] [blame] | 34 | public: |
Dominik Laskowski | 8b01cc0 | 2020-07-14 19:02:41 -0700 | [diff] [blame] | 35 | TestableScheduler(const scheduler::RefreshRateConfigs& configs, ISchedulerCallback& callback, |
| 36 | bool useContentDetectionV2) |
Ady Abraham | 8cb2188 | 2020-08-26 18:22:05 -0700 | [diff] [blame] | 37 | : TestableScheduler(std::make_unique<mock::VsyncController>(), |
| 38 | std::make_unique<mock::VSyncTracker>(), configs, callback, |
Dominik Laskowski | 8b01cc0 | 2020-07-14 19:02:41 -0700 | [diff] [blame] | 39 | useContentDetectionV2) {} |
Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame] | 40 | |
Ady Abraham | 8cb2188 | 2020-08-26 18:22:05 -0700 | [diff] [blame] | 41 | TestableScheduler(std::unique_ptr<scheduler::VsyncController> vsyncController, |
| 42 | std::unique_ptr<scheduler::VSyncTracker> vsyncTracker, |
Dominik Laskowski | 8b01cc0 | 2020-07-14 19:02:41 -0700 | [diff] [blame] | 43 | const scheduler::RefreshRateConfigs& configs, ISchedulerCallback& callback, |
| 44 | bool useContentDetectionV2) |
Ady Abraham | 8cb2188 | 2020-08-26 18:22:05 -0700 | [diff] [blame] | 45 | : Scheduler({std::move(vsyncController), std::move(vsyncTracker), nullptr}, configs, |
| 46 | callback, createLayerHistory(configs, useContentDetectionV2), |
Dominik Laskowski | 8b01cc0 | 2020-07-14 19:02:41 -0700 | [diff] [blame] | 47 | {.supportKernelTimer = false, |
| 48 | .useContentDetection = true, |
Ady Abraham | 49cb7d5 | 2020-07-22 18:37:07 -0700 | [diff] [blame] | 49 | .useContentDetectionV2 = useContentDetectionV2}) {} |
Ana Krulec | afb4584 | 2019-02-13 13:33:03 -0800 | [diff] [blame] | 50 | |
Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame] | 51 | // Used to inject mock event thread. |
| 52 | ConnectionHandle createConnection(std::unique_ptr<EventThread> eventThread) { |
Steven Thomas | 2bbaabe | 2019-08-28 16:08:35 -0700 | [diff] [blame] | 53 | return Scheduler::createConnection(std::move(eventThread)); |
Ana Krulec | afb4584 | 2019-02-13 13:33:03 -0800 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | /* ------------------------------------------------------------------------ |
| 57 | * Read-write access to private data to set up preconditions and assert |
| 58 | * post-conditions. |
| 59 | */ |
| 60 | auto& mutablePrimaryHWVsyncEnabled() { return mPrimaryHWVsyncEnabled; } |
Ana Krulec | afb4584 | 2019-02-13 13:33:03 -0800 | [diff] [blame] | 61 | auto& mutableHWVsyncAvailable() { return mHWVsyncAvailable; } |
Lais Andrade | 3a6e47d | 2020-04-02 11:20:16 +0100 | [diff] [blame] | 62 | |
Dominik Laskowski | 983f2b5 | 2020-06-25 16:54:06 -0700 | [diff] [blame] | 63 | bool hasLayerHistory() const { return static_cast<bool>(mLayerHistory); } |
| 64 | |
| 65 | auto* mutableLayerHistory() { |
Dominik Laskowski | 8b01cc0 | 2020-07-14 19:02:41 -0700 | [diff] [blame] | 66 | LOG_ALWAYS_FATAL_IF(mOptions.useContentDetectionV2); |
Ady Abraham | e3ed2f9 | 2020-01-06 17:01:28 -0800 | [diff] [blame] | 67 | return static_cast<scheduler::impl::LayerHistory*>(mLayerHistory.get()); |
| 68 | } |
Lais Andrade | 3a6e47d | 2020-04-02 11:20:16 +0100 | [diff] [blame] | 69 | |
Dominik Laskowski | 983f2b5 | 2020-06-25 16:54:06 -0700 | [diff] [blame] | 70 | auto* mutableLayerHistoryV2() { |
Dominik Laskowski | 8b01cc0 | 2020-07-14 19:02:41 -0700 | [diff] [blame] | 71 | LOG_ALWAYS_FATAL_IF(!mOptions.useContentDetectionV2); |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 72 | return static_cast<scheduler::impl::LayerHistoryV2*>(mLayerHistory.get()); |
| 73 | } |
Ana Krulec | afb4584 | 2019-02-13 13:33:03 -0800 | [diff] [blame] | 74 | |
Dominik Laskowski | 983f2b5 | 2020-06-25 16:54:06 -0700 | [diff] [blame] | 75 | size_t layerHistorySize() NO_THREAD_SAFETY_ANALYSIS { |
| 76 | if (!mLayerHistory) return 0; |
Dominik Laskowski | 8b01cc0 | 2020-07-14 19:02:41 -0700 | [diff] [blame] | 77 | return mOptions.useContentDetectionV2 ? mutableLayerHistoryV2()->mLayerInfos.size() |
| 78 | : mutableLayerHistory()->mLayerInfos.size(); |
Dominik Laskowski | 983f2b5 | 2020-06-25 16:54:06 -0700 | [diff] [blame] | 79 | } |
| 80 | |
Lais Andrade | 3a6e47d | 2020-04-02 11:20:16 +0100 | [diff] [blame] | 81 | void replaceTouchTimer(int64_t millis) { |
| 82 | if (mTouchTimer) { |
| 83 | mTouchTimer.reset(); |
| 84 | } |
| 85 | mTouchTimer.emplace( |
| 86 | std::chrono::milliseconds(millis), |
| 87 | [this] { touchTimerCallback(TimerState::Reset); }, |
| 88 | [this] { touchTimerCallback(TimerState::Expired); }); |
| 89 | mTouchTimer->start(); |
| 90 | } |
| 91 | |
| 92 | bool isTouchActive() { |
| 93 | std::lock_guard<std::mutex> lock(mFeatureStateLock); |
| 94 | return mFeatures.touch == Scheduler::TouchState::Active; |
| 95 | } |
| 96 | |
Ana Krulec | 6ddd261 | 2020-09-24 13:06:33 -0700 | [diff] [blame^] | 97 | void dispatchCachedReportedConfig() { |
| 98 | std::lock_guard<std::mutex> lock(mFeatureStateLock); |
| 99 | return Scheduler::dispatchCachedReportedConfig(); |
| 100 | } |
| 101 | |
| 102 | void clearOptionalFieldsInFeatures() { |
| 103 | std::lock_guard<std::mutex> lock(mFeatureStateLock); |
| 104 | mFeatures.cachedConfigChangedParams.reset(); |
| 105 | } |
| 106 | |
| 107 | void onNonPrimaryDisplayConfigChanged(ConnectionHandle handle, PhysicalDisplayId displayId, |
| 108 | HwcConfigIndexType configId, nsecs_t vsyncPeriod) { |
| 109 | return Scheduler::onNonPrimaryDisplayConfigChanged(handle, displayId, configId, |
| 110 | vsyncPeriod); |
| 111 | } |
| 112 | |
Ana Krulec | afb4584 | 2019-02-13 13:33:03 -0800 | [diff] [blame] | 113 | ~TestableScheduler() { |
| 114 | // All these pointer and container clears help ensure that GMock does |
| 115 | // not report a leaked object, since the Scheduler instance may |
| 116 | // still be referenced by something despite our best efforts to destroy |
| 117 | // it after each test is done. |
Ady Abraham | 8cb2188 | 2020-08-26 18:22:05 -0700 | [diff] [blame] | 118 | mVsyncSchedule.controller.reset(); |
Ana Krulec | afb4584 | 2019-02-13 13:33:03 -0800 | [diff] [blame] | 119 | mConnections.clear(); |
Dominik Laskowski | 7c9dbf9 | 2019-08-01 17:57:31 -0700 | [diff] [blame] | 120 | } |
Ana Krulec | afb4584 | 2019-02-13 13:33:03 -0800 | [diff] [blame] | 121 | }; |
| 122 | |
| 123 | } // namespace android |