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