blob: e611658bfd271dc7ef1b6547941bc1e78ef0fad4 [file] [log] [blame]
Dominik Laskowski068173d2021-08-11 17:22:59 -07001/*
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#include <scheduler/Fps.h>
Dominik Laskowski4e0d20d2021-12-06 11:31:02 -080018#include <scheduler/Timer.h>
Dominik Laskowski068173d2021-08-11 17:22:59 -070019
20#include "VsyncSchedule.h"
21
Dominik Laskowski068173d2021-08-11 17:22:59 -070022#include "VSyncDispatchTimerQueue.h"
23#include "VSyncPredictor.h"
24#include "VSyncReactor.h"
25
26#include "../TracedOrdinal.h"
27
28namespace android::scheduler {
29
30class VsyncSchedule::PredictedVsyncTracer {
31 // Invoked from the thread of the VsyncDispatch owned by this VsyncSchedule.
32 constexpr auto makeVsyncCallback() {
33 return [this](nsecs_t, nsecs_t, nsecs_t) {
34 mParity = !mParity;
35 schedule();
36 };
37 }
38
39public:
40 explicit PredictedVsyncTracer(VsyncDispatch& dispatch)
41 : mRegistration(dispatch, makeVsyncCallback(), __func__) {
42 schedule();
43 }
44
45private:
46 void schedule() { mRegistration.schedule({0, 0, 0}); }
47
48 TracedOrdinal<bool> mParity = {"VSYNC-predicted", 0};
49 VSyncCallbackRegistration mRegistration;
50};
51
52VsyncSchedule::VsyncSchedule(FeatureFlags features)
53 : mTracker(createTracker()),
54 mDispatch(createDispatch(*mTracker)),
55 mController(createController(*mTracker, features)) {
56 if (features.test(Feature::kTracePredictedVsync)) {
57 mTracer = std::make_unique<PredictedVsyncTracer>(*mDispatch);
58 }
59}
60
61VsyncSchedule::VsyncSchedule(TrackerPtr tracker, DispatchPtr dispatch, ControllerPtr controller)
62 : mTracker(std::move(tracker)),
63 mDispatch(std::move(dispatch)),
64 mController(std::move(controller)) {}
65
66VsyncSchedule::VsyncSchedule(VsyncSchedule&&) = default;
67VsyncSchedule::~VsyncSchedule() = default;
68
69void VsyncSchedule::dump(std::string& out) const {
70 out.append("VsyncController:\n");
71 mController->dump(out);
72
73 out.append("VsyncDispatch:\n");
74 mDispatch->dump(out);
75}
76
77VsyncSchedule::TrackerPtr VsyncSchedule::createTracker() {
78 // TODO(b/144707443): Tune constants.
79 constexpr nsecs_t kInitialPeriod = (60_Hz).getPeriodNsecs();
80 constexpr size_t kHistorySize = 20;
81 constexpr size_t kMinSamplesForPrediction = 6;
82 constexpr uint32_t kDiscardOutlierPercent = 20;
83
84 return std::make_unique<VSyncPredictor>(kInitialPeriod, kHistorySize, kMinSamplesForPrediction,
85 kDiscardOutlierPercent);
86}
87
88VsyncSchedule::DispatchPtr VsyncSchedule::createDispatch(VsyncTracker& tracker) {
89 using namespace std::chrono_literals;
90
91 // TODO(b/144707443): Tune constants.
92 constexpr std::chrono::nanoseconds kGroupDispatchWithin = 500us;
93 constexpr std::chrono::nanoseconds kSnapToSameVsyncWithin = 3ms;
94
95 return std::make_unique<VSyncDispatchTimerQueue>(std::make_unique<Timer>(), tracker,
96 kGroupDispatchWithin.count(),
97 kSnapToSameVsyncWithin.count());
98}
99
100VsyncSchedule::ControllerPtr VsyncSchedule::createController(VsyncTracker& tracker,
101 FeatureFlags features) {
102 // TODO(b/144707443): Tune constants.
103 constexpr size_t kMaxPendingFences = 20;
104 const bool hasKernelIdleTimer = features.test(Feature::kKernelIdleTimer);
105
106 auto reactor = std::make_unique<VSyncReactor>(std::make_unique<SystemClock>(), tracker,
107 kMaxPendingFences, hasKernelIdleTimer);
108
109 reactor->setIgnorePresentFences(!features.test(Feature::kPresentFences));
110 return reactor;
111}
112
113} // namespace android::scheduler