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