• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
25 #include <scheduler/TimeKeeper.h>
26 
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"
34 
35 namespace android::fuzz {
36 
37 class FuzzImplClock : public android::scheduler::Clock {
38 public:
now()39     nsecs_t now() const { return 1; }
40 };
41 
42 class ClockWrapper : public android::scheduler::Clock {
43 public:
ClockWrapper(std::shared_ptr<android::scheduler::Clock> const & clock)44     ClockWrapper(std::shared_ptr<android::scheduler::Clock> const& clock) : mClock(clock) {}
45 
now()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;
now()61     std::chrono::steady_clock::time_point now() const override { return mNow; }
62 
advanceTime(std::chrono::nanoseconds delta)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:
FuzzImplLayer(SurfaceFlinger * flinger,std::string name)71     FuzzImplLayer(SurfaceFlinger* flinger, std::string name)
72           : Layer(LayerCreationArgs(flinger, nullptr, std::move(name), 0, {})) {}
FuzzImplLayer(SurfaceFlinger * flinger)73     explicit FuzzImplLayer(SurfaceFlinger* flinger) : FuzzImplLayer(flinger, "FuzzLayer") {}
74 
getType()75     const char* getType() const override { return ""; }
76 
isVisible()77     bool isVisible() const override { return true; }
78 
createClone()79     sp<Layer> createClone() override { return nullptr; }
80 };
81 
82 class FuzzImplVSyncSource : public VSyncSource {
83 public:
getName()84     const char* getName() const override { return "fuzz"; }
85 
setVSyncEnabled(bool)86     void setVSyncEnabled(bool /* enable */) override {}
87 
setCallback(Callback *)88     void setCallback(Callback* /* callback */) override {}
89 
setDuration(std::chrono::nanoseconds,std::chrono::nanoseconds)90     void setDuration(std::chrono::nanoseconds /* workDuration */,
91                      std::chrono::nanoseconds /* readyDuration */) override {}
92 
getLatestVSyncData()93     VSyncData getLatestVSyncData() const override { return {}; }
94 
dump(std::string &)95     void dump(std::string& /* result */) const override {}
96 };
97 
98 class FuzzImplVSyncTracker : public scheduler::VSyncTracker {
99 public:
FuzzImplVSyncTracker(nsecs_t period)100     FuzzImplVSyncTracker(nsecs_t period) { mPeriod = period; }
101 
102     FuzzImplVSyncTracker() = default;
103 
addVsyncTimestamp(nsecs_t)104     bool addVsyncTimestamp(nsecs_t /* timestamp */) override { return true; }
105 
nextAnticipatedVSyncTimeFrom(nsecs_t)106     nsecs_t nextAnticipatedVSyncTimeFrom(nsecs_t /* timePoint */) const override { return 1; }
107 
currentPeriod()108     nsecs_t currentPeriod() const override { return 1; }
109 
setPeriod(nsecs_t)110     void setPeriod(nsecs_t /* period */) override {}
111 
resetModel()112     void resetModel() override {}
113 
needsMoreSamples()114     bool needsMoreSamples() const override { return true; }
115 
isVSyncInPhase(nsecs_t,Fps)116     bool isVSyncInPhase(nsecs_t /* timePoint */, Fps /* frameRate */) const override {
117         return true;
118     }
119 
nextVSyncTime(nsecs_t timePoint)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 
dump(std::string &)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:
registerCallback(Callback,std::string)135     CallbackToken registerCallback(Callback /* callbackFn */,
136                                    std::string /* callbackName */) override {
137         return CallbackToken{};
138     }
139 
unregisterCallback(CallbackToken)140     void unregisterCallback(CallbackToken /* token */) override {}
141 
schedule(CallbackToken,ScheduleTiming)142     scheduler::ScheduleResult schedule(CallbackToken /* token */,
143                                        ScheduleTiming /* scheduleTiming */) override {
144         return (scheduler::ScheduleResult)0;
145     }
146 
cancel(CallbackToken)147     scheduler::CancelResult cancel(CallbackToken /* token */) override {
148         return (scheduler::CancelResult)0;
149     }
150 
dump(std::string &)151     void dump(std::string& /* result */) const override {}
152 };
153 
154 } // namespace android
155 
156 namespace android::scheduler {
157 
158 class ControllableClock : public TimeKeeper {
159 public:
now()160     nsecs_t now() const { return 1; };
alarmAt(std::function<void ()>,nsecs_t)161     void alarmAt(std::function<void()> /* callback */, nsecs_t /* time */) override {}
alarmCancel()162     void alarmCancel() override {}
dump(std::string &)163     void dump(std::string& /* result */) const override {}
164 
alarmAtDefaultBehavior(std::function<void ()> const & callback,nsecs_t time)165     void alarmAtDefaultBehavior(std::function<void()> const& callback, nsecs_t time) {
166         mCallback = callback;
167         mNextCallbackTime = time;
168     }
169 
fakeTime()170     nsecs_t fakeTime() const { return mCurrentTime; }
171 
advanceToNextCallback()172     void advanceToNextCallback() {
173         mCurrentTime = mNextCallbackTime;
174         if (mCallback) {
175             mCallback();
176         }
177     }
178 
advanceBy(nsecs_t advancement)179     void advanceBy(nsecs_t advancement) {
180         mCurrentTime += advancement;
181         if (mCurrentTime >= (mNextCallbackTime + mLag) && mCallback) {
182             mCallback();
183         }
184     };
185 
setLag(nsecs_t lag)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 
Now()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:
FuzzImplVsyncModulator(const VsyncConfigSet & config,Now now)202     FuzzImplVsyncModulator(const VsyncConfigSet& config, Now now) : VsyncModulator(config, now) {}
203 
binderDied(const wp<IBinder> & token)204     void binderDied(const wp<IBinder>& token) { VsyncModulator::binderDied(token); }
205 };
206 } // namespace android::scheduler
207