blob: 3a918a1660f51ea19869e99a49060c17b6b65d99 [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
71void VsyncSchedule::dump(std::string& out) const {
72 out.append("VsyncController:\n");
73 mController->dump(out);
74
75 out.append("VsyncDispatch:\n");
76 mDispatch->dump(out);
77}
78
79VsyncSchedule::TrackerPtr VsyncSchedule::createTracker() {
80 // TODO(b/144707443): Tune constants.
81 constexpr nsecs_t kInitialPeriod = (60_Hz).getPeriodNsecs();
82 constexpr size_t kHistorySize = 20;
83 constexpr size_t kMinSamplesForPrediction = 6;
84 constexpr uint32_t kDiscardOutlierPercent = 20;
85
86 return std::make_unique<VSyncPredictor>(kInitialPeriod, kHistorySize, kMinSamplesForPrediction,
87 kDiscardOutlierPercent);
88}
89
90VsyncSchedule::DispatchPtr VsyncSchedule::createDispatch(VsyncTracker& tracker) {
91 using namespace std::chrono_literals;
92
93 // TODO(b/144707443): Tune constants.
94 constexpr std::chrono::nanoseconds kGroupDispatchWithin = 500us;
95 constexpr std::chrono::nanoseconds kSnapToSameVsyncWithin = 3ms;
96
97 return std::make_unique<VSyncDispatchTimerQueue>(std::make_unique<Timer>(), tracker,
98 kGroupDispatchWithin.count(),
99 kSnapToSameVsyncWithin.count());
100}
101
102VsyncSchedule::ControllerPtr VsyncSchedule::createController(VsyncTracker& tracker,
103 FeatureFlags features) {
104 // TODO(b/144707443): Tune constants.
105 constexpr size_t kMaxPendingFences = 20;
106 const bool hasKernelIdleTimer = features.test(Feature::kKernelIdleTimer);
107
108 auto reactor = std::make_unique<VSyncReactor>(std::make_unique<SystemClock>(), tracker,
109 kMaxPendingFences, hasKernelIdleTimer);
110
111 reactor->setIgnorePresentFences(!features.test(Feature::kPresentFences));
112 return reactor;
113}
114
115} // namespace android::scheduler