summaryrefslogtreecommitdiff
path: root/services/surfaceflinger/Scheduler/PhaseOffsets.cpp
blob: fe2e4067750dc2cefff262b99ed49621230351d6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
/*
 * Copyright 2019 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "PhaseOffsets.h"

#include <cutils/properties.h>

#include <optional>

#include "SurfaceFlingerProperties.h"

namespace {

std::optional<nsecs_t> getProperty(const char* name) {
    char value[PROPERTY_VALUE_MAX];
    property_get(name, value, "-1");
    if (const int i = atoi(value); i != -1) return i;
    return std::nullopt;
}

bool fpsEqualsWithMargin(float fpsA, float fpsB) {
    static constexpr float MARGIN = 0.01f;
    return std::abs(fpsA - fpsB) <= MARGIN;
}

std::vector<float> getRefreshRatesFromConfigs(
        const android::scheduler::RefreshRateConfigs& refreshRateConfigs) {
    const auto& allRefreshRates = refreshRateConfigs.getAllRefreshRates();
    std::vector<float> refreshRates;
    refreshRates.reserve(allRefreshRates.size());

    for (const auto& [ignored, refreshRate] : allRefreshRates) {
        refreshRates.emplace_back(refreshRate->getFps());
    }

    return refreshRates;
}

} // namespace

namespace android::scheduler {

PhaseConfiguration::~PhaseConfiguration() = default;

namespace impl {

PhaseOffsets::PhaseOffsets(const scheduler::RefreshRateConfigs& refreshRateConfigs)
      : PhaseOffsets(getRefreshRatesFromConfigs(refreshRateConfigs),
                     refreshRateConfigs.getCurrentRefreshRate().getFps(),
                     sysprop::vsync_event_phase_offset_ns(1000000),
                     sysprop::vsync_sf_event_phase_offset_ns(1000000),
                     getProperty("debug.sf.early_phase_offset_ns"),
                     getProperty("debug.sf.early_gl_phase_offset_ns"),
                     getProperty("debug.sf.early_app_phase_offset_ns"),
                     getProperty("debug.sf.early_gl_app_phase_offset_ns"),
                     // Below defines the threshold when an offset is considered to be negative,
                     // i.e. targeting for the N+2 vsync instead of N+1. This means that: For offset
                     // < threshold, SF wake up (vsync_duration - offset) before HW vsync. For
                     // offset >= threshold, SF wake up (2 * vsync_duration - offset) before HW
                     // vsync.
                     getProperty("debug.sf.phase_offset_threshold_for_next_vsync_ns")
                             .value_or(std::numeric_limits<nsecs_t>::max())) {}

PhaseOffsets::PhaseOffsets(const std::vector<float>& refreshRates, float currentFps,
                           nsecs_t vsyncPhaseOffsetNs, nsecs_t sfVSyncPhaseOffsetNs,
                           std::optional<nsecs_t> earlySfOffsetNs,
                           std::optional<nsecs_t> earlyGlSfOffsetNs,
                           std::optional<nsecs_t> earlyAppOffsetNs,
                           std::optional<nsecs_t> earlyGlAppOffsetNs, nsecs_t thresholdForNextVsync)
      : mVSyncPhaseOffsetNs(vsyncPhaseOffsetNs),
        mSfVSyncPhaseOffsetNs(sfVSyncPhaseOffsetNs),
        mEarlySfOffsetNs(earlySfOffsetNs),
        mEarlyGlSfOffsetNs(earlyGlSfOffsetNs),
        mEarlyAppOffsetNs(earlyAppOffsetNs),
        mEarlyGlAppOffsetNs(earlyGlAppOffsetNs),
        mThresholdForNextVsync(thresholdForNextVsync),
        mOffsets(initializeOffsets(refreshRates)),
        mRefreshRateFps(currentFps) {}

void PhaseOffsets::dump(std::string& result) const {
    const auto [early, earlyGl, late] = getCurrentOffsets();
    using base::StringAppendF;
    StringAppendF(&result,
                  "           app phase: %9" PRId64 " ns\t         SF phase: %9" PRId64 " ns\n"
                  "     early app phase: %9" PRId64 " ns\t   early SF phase: %9" PRId64 " ns\n"
                  "  GL early app phase: %9" PRId64 " ns\tGL early SF phase: %9" PRId64 " ns\n"
                  "next VSYNC threshold: %9" PRId64 " ns\n",
                  late.app, late.sf, early.app, early.sf, earlyGl.app, earlyGl.sf,
                  mThresholdForNextVsync);
}

std::unordered_map<float, PhaseOffsets::Offsets> PhaseOffsets::initializeOffsets(
        const std::vector<float>& refreshRates) const {
    std::unordered_map<float, Offsets> offsets;

    for (const auto& refreshRate : refreshRates) {
        offsets.emplace(refreshRate,
                        getPhaseOffsets(refreshRate, static_cast<nsecs_t>(1e9f / refreshRate)));
    }
    return offsets;
}

PhaseOffsets::Offsets PhaseOffsets::getPhaseOffsets(float fps, nsecs_t vsyncPeriod) const {
    if (fps > 65.0f) {
        return getHighFpsOffsets(vsyncPeriod);
    } else {
        return getDefaultOffsets(vsyncPeriod);
    }
}

PhaseOffsets::Offsets PhaseOffsets::getDefaultOffsets(nsecs_t vsyncDuration) const {
    return {
            {
                    mEarlySfOffsetNs.value_or(mSfVSyncPhaseOffsetNs) < mThresholdForNextVsync
                            ? mEarlySfOffsetNs.value_or(mSfVSyncPhaseOffsetNs)
                            : mEarlySfOffsetNs.value_or(mSfVSyncPhaseOffsetNs) - vsyncDuration,

                    mEarlyAppOffsetNs.value_or(mVSyncPhaseOffsetNs),
            },
            {
                    mEarlyGlSfOffsetNs.value_or(mSfVSyncPhaseOffsetNs) < mThresholdForNextVsync
                            ? mEarlyGlSfOffsetNs.value_or(mSfVSyncPhaseOffsetNs)
                            : mEarlyGlSfOffsetNs.value_or(mSfVSyncPhaseOffsetNs) - vsyncDuration,

                    mEarlyGlAppOffsetNs.value_or(mVSyncPhaseOffsetNs),
            },
            {
                    mSfVSyncPhaseOffsetNs < mThresholdForNextVsync
                            ? mSfVSyncPhaseOffsetNs
                            : mSfVSyncPhaseOffsetNs - vsyncDuration,

                    mVSyncPhaseOffsetNs,
            },
    };
}

PhaseOffsets::Offsets PhaseOffsets::getHighFpsOffsets(nsecs_t vsyncDuration) const {
    const auto highFpsLateAppOffsetNs =
            getProperty("debug.sf.high_fps_late_app_phase_offset_ns").value_or(2000000);
    const auto highFpsLateSfOffsetNs =
            getProperty("debug.sf.high_fps_late_sf_phase_offset_ns").value_or(1000000);

    const auto highFpsEarlySfOffsetNs = getProperty("debug.sf.high_fps_early_phase_offset_ns");
    const auto highFpsEarlyGlSfOffsetNs = getProperty("debug.sf.high_fps_early_gl_phase_offset_ns");
    const auto highFpsEarlyAppOffsetNs = getProperty("debug.sf.high_fps_early_app_phase_offset_ns");
    const auto highFpsEarlyGlAppOffsetNs =
            getProperty("debug.sf.high_fps_early_gl_app_phase_offset_ns");

    return {
            {
                    highFpsEarlySfOffsetNs.value_or(highFpsLateSfOffsetNs) < mThresholdForNextVsync
                            ? highFpsEarlySfOffsetNs.value_or(highFpsLateSfOffsetNs)
                            : highFpsEarlySfOffsetNs.value_or(highFpsLateSfOffsetNs) -
                                    vsyncDuration,

                    highFpsEarlyAppOffsetNs.value_or(highFpsLateAppOffsetNs),
            },
            {
                    highFpsEarlyGlSfOffsetNs.value_or(highFpsLateSfOffsetNs) <
                                    mThresholdForNextVsync
                            ? highFpsEarlyGlSfOffsetNs.value_or(highFpsLateSfOffsetNs)
                            : highFpsEarlyGlSfOffsetNs.value_or(highFpsLateSfOffsetNs) -
                                    vsyncDuration,

                    highFpsEarlyGlAppOffsetNs.value_or(highFpsLateAppOffsetNs),
            },
            {
                    highFpsLateSfOffsetNs < mThresholdForNextVsync
                            ? highFpsLateSfOffsetNs
                            : highFpsLateSfOffsetNs - vsyncDuration,

                    highFpsLateAppOffsetNs,
            },
    };
}

PhaseOffsets::Offsets PhaseOffsets::getOffsetsForRefreshRate(float fps) const {
    const auto iter = std::find_if(mOffsets.begin(), mOffsets.end(),
                                   [&fps](const std::pair<float, Offsets>& candidateFps) {
                                       return fpsEqualsWithMargin(fps, candidateFps.first);
                                   });

    if (iter != mOffsets.end()) {
        return iter->second;
    }

    // Unknown refresh rate. This might happen if we get a hotplug event for an external display.
    // In this case just construct the offset.
    ALOGW("Can't find offset for %.2f fps", fps);
    return getPhaseOffsets(fps, static_cast<nsecs_t>(1e9f / fps));
}

static void validateSysprops() {
    const auto validatePropertyBool = [](const char* prop) {
        LOG_ALWAYS_FATAL_IF(!property_get_bool(prop, false), "%s is false", prop);
    };

    validatePropertyBool("debug.sf.use_phase_offsets_as_durations");

    LOG_ALWAYS_FATAL_IF(sysprop::vsync_event_phase_offset_ns(-1) != -1,
                        "ro.surface_flinger.vsync_event_phase_offset_ns is set but expecting "
                        "duration");

    LOG_ALWAYS_FATAL_IF(sysprop::vsync_sf_event_phase_offset_ns(-1) != -1,
                        "ro.surface_flinger.vsync_sf_event_phase_offset_ns is set but expecting "
                        "duration");

    const auto validateProperty = [](const char* prop) {
        LOG_ALWAYS_FATAL_IF(getProperty(prop).has_value(),
                            "%s is set to %" PRId64 " but expecting duration", prop,
                            getProperty(prop).value_or(-1));
    };

    validateProperty("debug.sf.early_phase_offset_ns");
    validateProperty("debug.sf.early_gl_phase_offset_ns");
    validateProperty("debug.sf.early_app_phase_offset_ns");
    validateProperty("debug.sf.early_gl_app_phase_offset_ns");
    validateProperty("debug.sf.high_fps_late_app_phase_offset_ns");
    validateProperty("debug.sf.high_fps_late_sf_phase_offset_ns");
    validateProperty("debug.sf.high_fps_early_phase_offset_ns");
    validateProperty("debug.sf.high_fps_early_gl_phase_offset_ns");
    validateProperty("debug.sf.high_fps_early_app_phase_offset_ns");
    validateProperty("debug.sf.high_fps_early_gl_app_phase_offset_ns");
}

static nsecs_t sfDurationToOffset(nsecs_t sfDuration, nsecs_t vsyncDuration) {
    return sfDuration == -1 ? 1'000'000 : vsyncDuration - sfDuration % vsyncDuration;
}

static nsecs_t appDurationToOffset(nsecs_t appDuration, nsecs_t sfDuration, nsecs_t vsyncDuration) {
    return sfDuration == -1 ? 1'000'000
                            : vsyncDuration - (appDuration + sfDuration) % vsyncDuration;
}

PhaseDurations::Offsets PhaseDurations::constructOffsets(nsecs_t vsyncDuration) const {
    return Offsets{
            {
                    mSfEarlyDuration < vsyncDuration
                            ? sfDurationToOffset(mSfEarlyDuration, vsyncDuration)
                            : sfDurationToOffset(mSfEarlyDuration, vsyncDuration) - vsyncDuration,

                    appDurationToOffset(mAppEarlyDuration, mSfEarlyDuration, vsyncDuration),
            },
            {
                    mSfEarlyGlDuration < vsyncDuration
                            ? sfDurationToOffset(mSfEarlyGlDuration, vsyncDuration)
                            : sfDurationToOffset(mSfEarlyGlDuration, vsyncDuration) - vsyncDuration,

                    appDurationToOffset(mAppEarlyGlDuration, mSfEarlyGlDuration, vsyncDuration),
            },
            {
                    mSfDuration < vsyncDuration
                            ? sfDurationToOffset(mSfDuration, vsyncDuration)
                            : sfDurationToOffset(mSfDuration, vsyncDuration) - vsyncDuration,

                    appDurationToOffset(mAppDuration, mSfDuration, vsyncDuration),
            },
    };
}

std::unordered_map<float, PhaseDurations::Offsets> PhaseDurations::initializeOffsets(
        const std::vector<float>& refreshRates) const {
    std::unordered_map<float, Offsets> offsets;

    for (const auto fps : refreshRates) {
        offsets.emplace(fps, constructOffsets(static_cast<nsecs_t>(1e9f / fps)));
    }
    return offsets;
}

PhaseDurations::PhaseDurations(const scheduler::RefreshRateConfigs& refreshRateConfigs)
      : PhaseDurations(getRefreshRatesFromConfigs(refreshRateConfigs),
                       refreshRateConfigs.getCurrentRefreshRate().getFps(),
                       getProperty("debug.sf.late.sf.duration").value_or(-1),
                       getProperty("debug.sf.late.app.duration").value_or(-1),
                       getProperty("debug.sf.early.sf.duration").value_or(mSfDuration),
                       getProperty("debug.sf.early.app.duration").value_or(mAppDuration),
                       getProperty("debug.sf.earlyGl.sf.duration").value_or(mSfDuration),
                       getProperty("debug.sf.earlyGl.app.duration").value_or(mAppDuration)) {
    validateSysprops();
}

PhaseDurations::PhaseDurations(const std::vector<float>& refreshRates, float currentFps,
                               nsecs_t sfDuration, nsecs_t appDuration, nsecs_t sfEarlyDuration,
                               nsecs_t appEarlyDuration, nsecs_t sfEarlyGlDuration,
                               nsecs_t appEarlyGlDuration)
      : mSfDuration(sfDuration),
        mAppDuration(appDuration),
        mSfEarlyDuration(sfEarlyDuration),
        mAppEarlyDuration(appEarlyDuration),
        mSfEarlyGlDuration(sfEarlyGlDuration),
        mAppEarlyGlDuration(appEarlyGlDuration),
        mOffsets(initializeOffsets(refreshRates)),
        mRefreshRateFps(currentFps) {}

PhaseOffsets::Offsets PhaseDurations::getOffsetsForRefreshRate(float fps) const {
    const auto iter = std::find_if(mOffsets.begin(), mOffsets.end(), [=](const auto& candidateFps) {
        return fpsEqualsWithMargin(fps, candidateFps.first);
    });

    if (iter != mOffsets.end()) {
        return iter->second;
    }

    // Unknown refresh rate. This might happen if we get a hotplug event for an external display.
    // In this case just construct the offset.
    ALOGW("Can't find offset for %.2f fps", fps);
    return constructOffsets(static_cast<nsecs_t>(1e9f / fps));
}

void PhaseDurations::dump(std::string& result) const {
    const auto [early, earlyGl, late] = getCurrentOffsets();
    using base::StringAppendF;
    StringAppendF(&result,
                  "           app phase:    %9" PRId64 " ns\t         SF phase:    %9" PRId64
                  " ns\n"
                  "           app duration: %9" PRId64 " ns\t         SF duration: %9" PRId64
                  " ns\n"
                  "     early app phase:    %9" PRId64 " ns\t   early SF phase:    %9" PRId64
                  " ns\n"
                  "     early app duration: %9" PRId64 " ns\t   early SF duration: %9" PRId64
                  " ns\n"
                  "  GL early app phase:    %9" PRId64 " ns\tGL early SF phase:    %9" PRId64
                  " ns\n"
                  "  GL early app duration: %9" PRId64 " ns\tGL early SF duration: %9" PRId64
                  " ns\n",
                  late.app,

                  late.sf,

                  mAppDuration, mSfDuration,

                  early.app, early.sf,

                  mAppEarlyDuration, mSfEarlyDuration,

                  earlyGl.app,

                  earlyGl.sf,

                  mAppEarlyGlDuration, mSfEarlyGlDuration);
}

} // namespace impl
} // namespace android::scheduler