Chethan Kumar R E | 70750f2 | 2021-11-23 17:39:01 +0530 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2021 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 | |
| 18 | /* |
| 19 | Reference for some of the classes and functions has been taken from unittests |
| 20 | present in frameworks/native/services/surfaceflinger/tests/unittests |
| 21 | */ |
| 22 | |
| 23 | #pragma once |
| 24 | |
Dominik Laskowski | b0054a2 | 2022-03-03 09:03:06 -0800 | [diff] [blame] | 25 | #include <scheduler/TimeKeeper.h> |
| 26 | |
Chethan Kumar R E | 70750f2 | 2021-11-23 17:39:01 +0530 | [diff] [blame] | 27 | #include "Clock.h" |
| 28 | #include "Layer.h" |
| 29 | #include "Scheduler/EventThread.h" |
| 30 | #include "Scheduler/RefreshRateConfigs.h" |
| 31 | #include "Scheduler/Scheduler.h" |
| 32 | #include "Scheduler/VSyncTracker.h" |
| 33 | #include "Scheduler/VsyncModulator.h" |
Chethan Kumar R E | 70750f2 | 2021-11-23 17:39:01 +0530 | [diff] [blame] | 34 | |
| 35 | namespace android::fuzz { |
| 36 | |
Chethan Kumar R E | 70750f2 | 2021-11-23 17:39:01 +0530 | [diff] [blame] | 37 | class FuzzImplClock : public android::scheduler::Clock { |
| 38 | public: |
| 39 | nsecs_t now() const { return 1; } |
| 40 | }; |
| 41 | |
| 42 | class ClockWrapper : public android::scheduler::Clock { |
| 43 | public: |
| 44 | ClockWrapper(std::shared_ptr<android::scheduler::Clock> const& clock) : mClock(clock) {} |
| 45 | |
| 46 | nsecs_t now() const { return mClock->now(); } |
| 47 | |
| 48 | private: |
| 49 | std::shared_ptr<android::scheduler::Clock> const mClock; |
| 50 | }; |
| 51 | |
| 52 | } // namespace android::fuzz |
| 53 | |
| 54 | namespace android { |
| 55 | |
| 56 | using namespace std::chrono_literals; |
| 57 | |
| 58 | class FakeClock : public Clock { |
| 59 | public: |
| 60 | virtual ~FakeClock() = default; |
| 61 | std::chrono::steady_clock::time_point now() const override { return mNow; } |
| 62 | |
| 63 | void advanceTime(std::chrono::nanoseconds delta) { mNow += delta; } |
| 64 | |
| 65 | private: |
| 66 | std::chrono::steady_clock::time_point mNow; |
| 67 | }; |
| 68 | |
| 69 | class FuzzImplLayer : public Layer { |
| 70 | public: |
| 71 | FuzzImplLayer(SurfaceFlinger* flinger, std::string name) |
| 72 | : Layer(LayerCreationArgs(flinger, nullptr, std::move(name), 0, {})) {} |
| 73 | explicit FuzzImplLayer(SurfaceFlinger* flinger) : FuzzImplLayer(flinger, "FuzzLayer") {} |
| 74 | |
| 75 | const char* getType() const override { return ""; } |
| 76 | |
| 77 | bool isVisible() const override { return true; } |
| 78 | |
| 79 | sp<Layer> createClone() override { return nullptr; } |
| 80 | }; |
| 81 | |
| 82 | class FuzzImplVSyncSource : public VSyncSource { |
| 83 | public: |
| 84 | const char* getName() const override { return "fuzz"; } |
| 85 | |
| 86 | void setVSyncEnabled(bool /* enable */) override {} |
| 87 | |
| 88 | void setCallback(Callback* /* callback */) override {} |
| 89 | |
| 90 | void setDuration(std::chrono::nanoseconds /* workDuration */, |
| 91 | std::chrono::nanoseconds /* readyDuration */) override {} |
| 92 | |
Rachel Lee | ef2e21f | 2022-02-01 14:51:34 -0800 | [diff] [blame] | 93 | VSyncData getLatestVSyncData() const override { return {}; } |
| 94 | |
Chethan Kumar R E | 70750f2 | 2021-11-23 17:39:01 +0530 | [diff] [blame] | 95 | void dump(std::string& /* result */) const override {} |
| 96 | }; |
| 97 | |
| 98 | class FuzzImplVSyncTracker : public scheduler::VSyncTracker { |
| 99 | public: |
| 100 | FuzzImplVSyncTracker(nsecs_t period) { mPeriod = period; } |
| 101 | |
| 102 | FuzzImplVSyncTracker() = default; |
| 103 | |
| 104 | bool addVsyncTimestamp(nsecs_t /* timestamp */) override { return true; } |
| 105 | |
| 106 | nsecs_t nextAnticipatedVSyncTimeFrom(nsecs_t /* timePoint */) const override { return 1; } |
| 107 | |
| 108 | nsecs_t currentPeriod() const override { return 1; } |
| 109 | |
| 110 | void setPeriod(nsecs_t /* period */) override {} |
| 111 | |
| 112 | void resetModel() override {} |
| 113 | |
| 114 | bool needsMoreSamples() const override { return true; } |
| 115 | |
| 116 | bool isVSyncInPhase(nsecs_t /* timePoint */, Fps /* frameRate */) const override { |
| 117 | return true; |
| 118 | } |
| 119 | |
| 120 | nsecs_t nextVSyncTime(nsecs_t timePoint) const { |
| 121 | if (timePoint % mPeriod == 0) { |
| 122 | return timePoint; |
| 123 | } |
| 124 | return (timePoint - (timePoint % mPeriod) + mPeriod); |
| 125 | } |
| 126 | |
| 127 | void dump(std::string& /* result */) const override {} |
| 128 | |
| 129 | protected: |
| 130 | nsecs_t mPeriod; |
| 131 | }; |
| 132 | |
| 133 | class FuzzImplVSyncDispatch : public scheduler::VSyncDispatch { |
| 134 | public: |
| 135 | CallbackToken registerCallback(Callback /* callbackFn */, |
| 136 | std::string /* callbackName */) override { |
| 137 | return CallbackToken{}; |
| 138 | } |
| 139 | |
| 140 | void unregisterCallback(CallbackToken /* token */) override {} |
| 141 | |
| 142 | scheduler::ScheduleResult schedule(CallbackToken /* token */, |
| 143 | ScheduleTiming /* scheduleTiming */) override { |
| 144 | return (scheduler::ScheduleResult)0; |
| 145 | } |
| 146 | |
| 147 | scheduler::CancelResult cancel(CallbackToken /* token */) override { |
| 148 | return (scheduler::CancelResult)0; |
| 149 | } |
| 150 | |
| 151 | void dump(std::string& /* result */) const override {} |
| 152 | }; |
| 153 | |
| 154 | } // namespace android |
| 155 | |
| 156 | namespace android::scheduler { |
| 157 | |
Chethan Kumar R E | 70750f2 | 2021-11-23 17:39:01 +0530 | [diff] [blame] | 158 | class ControllableClock : public TimeKeeper { |
| 159 | public: |
| 160 | nsecs_t now() const { return 1; }; |
| 161 | void alarmAt(std::function<void()> /* callback */, nsecs_t /* time */) override {} |
| 162 | void alarmCancel() override {} |
| 163 | void dump(std::string& /* result */) const override {} |
| 164 | |
| 165 | void alarmAtDefaultBehavior(std::function<void()> const& callback, nsecs_t time) { |
| 166 | mCallback = callback; |
| 167 | mNextCallbackTime = time; |
| 168 | } |
| 169 | |
| 170 | nsecs_t fakeTime() const { return mCurrentTime; } |
| 171 | |
| 172 | void advanceToNextCallback() { |
| 173 | mCurrentTime = mNextCallbackTime; |
| 174 | if (mCallback) { |
| 175 | mCallback(); |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | void advanceBy(nsecs_t advancement) { |
| 180 | mCurrentTime += advancement; |
| 181 | if (mCurrentTime >= (mNextCallbackTime + mLag) && mCallback) { |
| 182 | mCallback(); |
| 183 | } |
| 184 | }; |
| 185 | |
| 186 | void setLag(nsecs_t lag) { mLag = lag; } |
| 187 | |
| 188 | private: |
| 189 | std::function<void()> mCallback; |
| 190 | nsecs_t mNextCallbackTime = 0; |
| 191 | nsecs_t mCurrentTime = 0; |
| 192 | nsecs_t mLag = 0; |
| 193 | }; |
| 194 | |
| 195 | static VsyncModulator::TimePoint Now() { |
| 196 | static VsyncModulator::TimePoint now; |
| 197 | return now += VsyncModulator::MIN_EARLY_TRANSACTION_TIME; |
| 198 | } |
| 199 | |
| 200 | class FuzzImplVsyncModulator : public VsyncModulator { |
| 201 | public: |
| 202 | FuzzImplVsyncModulator(const VsyncConfigSet& config, Now now) : VsyncModulator(config, now) {} |
| 203 | |
| 204 | void binderDied(const wp<IBinder>& token) { VsyncModulator::binderDied(token); } |
| 205 | }; |
| 206 | } // namespace android::scheduler |