blob: 6be88f89f9d3f507052997f2a4b65bf790de4c2b [file] [log] [blame]
Ana Krulec757f63a2019-01-25 10:46:18 -08001/*
2 * Copyright 2019 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 "PhaseOffsets.h"
18
19#include <cutils/properties.h>
20
Dominik Laskowskieddeda12019-07-19 11:54:13 -070021#include <optional>
22
Ana Krulec757f63a2019-01-25 10:46:18 -080023#include "SurfaceFlingerProperties.h"
24
Dominik Laskowskieddeda12019-07-19 11:54:13 -070025namespace {
Ana Krulec757f63a2019-01-25 10:46:18 -080026
Dominik Laskowskif83570c2019-08-26 12:04:07 -070027std::optional<nsecs_t> getProperty(const char* name) {
Dominik Laskowskieddeda12019-07-19 11:54:13 -070028 char value[PROPERTY_VALUE_MAX];
29 property_get(name, value, "-1");
30 if (const int i = atoi(value); i != -1) return i;
31 return std::nullopt;
32}
Ana Krulec757f63a2019-01-25 10:46:18 -080033
Dominik Laskowskieddeda12019-07-19 11:54:13 -070034} // namespace
35
36namespace android::scheduler {
37
Ana Krulec757f63a2019-01-25 10:46:18 -080038PhaseOffsets::~PhaseOffsets() = default;
39
40namespace impl {
Dominik Laskowskieddeda12019-07-19 11:54:13 -070041
Ana Krulec757f63a2019-01-25 10:46:18 -080042PhaseOffsets::PhaseOffsets() {
Ady Abrahambe0f9482019-04-24 15:41:53 -070043 // Below defines the threshold when an offset is considered to be negative, i.e. targeting
44 // for the N+2 vsync instead of N+1. This means that:
45 // For offset < threshold, SF wake up (vsync_duration - offset) before HW vsync.
46 // For offset >= threshold, SF wake up (2 * vsync_duration - offset) before HW vsync.
Dominik Laskowskieddeda12019-07-19 11:54:13 -070047 const nsecs_t thresholdForNextVsync =
48 getProperty("debug.sf.phase_offset_threshold_for_next_vsync_ns")
49 .value_or(std::numeric_limits<nsecs_t>::max());
Ady Abrahambe0f9482019-04-24 15:41:53 -070050
Dominik Laskowskieddeda12019-07-19 11:54:13 -070051 const Offsets defaultOffsets = getDefaultOffsets(thresholdForNextVsync);
52 const Offsets highFpsOffsets = getHighFpsOffsets(thresholdForNextVsync);
Alec Mourid7599d82019-05-22 19:58:00 -070053
Alec Mourid7599d82019-05-22 19:58:00 -070054 mOffsets.insert({RefreshRateType::DEFAULT, defaultOffsets});
55 mOffsets.insert({RefreshRateType::PERFORMANCE, highFpsOffsets});
Ana Krulec757f63a2019-01-25 10:46:18 -080056}
57
Ady Abraham796beb02019-04-11 15:23:07 -070058PhaseOffsets::Offsets PhaseOffsets::getOffsetsForRefreshRate(
Dominik Laskowskieddeda12019-07-19 11:54:13 -070059 RefreshRateType refreshRateType) const {
Alec Mourid7599d82019-05-22 19:58:00 -070060 return mOffsets.at(refreshRateType);
Ana Krulec757f63a2019-01-25 10:46:18 -080061}
62
63void PhaseOffsets::dump(std::string& result) const {
Dominik Laskowskieddeda12019-07-19 11:54:13 -070064 const auto [early, earlyGl, late, threshold] = getCurrentOffsets();
Dominik Laskowski98041832019-08-01 18:35:59 -070065 using base::StringAppendF;
66 StringAppendF(&result,
67 " app phase: %9" PRId64 " ns\t SF phase: %9" PRId64 " ns\n"
68 " early app phase: %9" PRId64 " ns\t early SF phase: %9" PRId64 " ns\n"
69 " GL early app phase: %9" PRId64 " ns\tGL early SF phase: %9" PRId64 " ns\n"
70 "next VSYNC threshold: %9" PRId64 " ns\n",
71 late.app, late.sf, early.app, early.sf, earlyGl.app, earlyGl.sf, threshold);
Ana Krulec757f63a2019-01-25 10:46:18 -080072}
73
Dominik Laskowskieddeda12019-07-19 11:54:13 -070074PhaseOffsets::Offsets PhaseOffsets::getDefaultOffsets(nsecs_t thresholdForNextVsync) {
75 const int64_t vsyncPhaseOffsetNs = sysprop::vsync_event_phase_offset_ns(1000000);
76 const int64_t sfVsyncPhaseOffsetNs = sysprop::vsync_sf_event_phase_offset_ns(1000000);
77
78 const auto earlySfOffsetNs = getProperty("debug.sf.early_phase_offset_ns");
79 const auto earlyGlSfOffsetNs = getProperty("debug.sf.early_gl_phase_offset_ns");
80 const auto earlyAppOffsetNs = getProperty("debug.sf.early_app_phase_offset_ns");
81 const auto earlyGlAppOffsetNs = getProperty("debug.sf.early_gl_app_phase_offset_ns");
82
83 return {{RefreshRateType::DEFAULT, earlySfOffsetNs.value_or(sfVsyncPhaseOffsetNs),
84 earlyAppOffsetNs.value_or(vsyncPhaseOffsetNs)},
85
86 {RefreshRateType::DEFAULT, earlyGlSfOffsetNs.value_or(sfVsyncPhaseOffsetNs),
87 earlyGlAppOffsetNs.value_or(vsyncPhaseOffsetNs)},
88
89 {RefreshRateType::DEFAULT, sfVsyncPhaseOffsetNs, vsyncPhaseOffsetNs},
90
91 thresholdForNextVsync};
Ana Krulec757f63a2019-01-25 10:46:18 -080092}
93
Dominik Laskowskieddeda12019-07-19 11:54:13 -070094PhaseOffsets::Offsets PhaseOffsets::getHighFpsOffsets(nsecs_t thresholdForNextVsync) {
95 // TODO(b/122905996): Define these in device.mk.
96 const int highFpsLateAppOffsetNs =
97 getProperty("debug.sf.high_fps_late_app_phase_offset_ns").value_or(2000000);
98 const int highFpsLateSfOffsetNs =
99 getProperty("debug.sf.high_fps_late_sf_phase_offset_ns").value_or(1000000);
100
101 const auto highFpsEarlySfOffsetNs = getProperty("debug.sf.high_fps_early_phase_offset_ns");
102 const auto highFpsEarlyGlSfOffsetNs = getProperty("debug.sf.high_fps_early_gl_phase_offset_ns");
103 const auto highFpsEarlyAppOffsetNs = getProperty("debug.sf.high_fps_early_app_phase_offset_ns");
104 const auto highFpsEarlyGlAppOffsetNs =
105 getProperty("debug.sf.high_fps_early_gl_app_phase_offset_ns");
106
107 return {{RefreshRateType::PERFORMANCE, highFpsEarlySfOffsetNs.value_or(highFpsLateSfOffsetNs),
108 highFpsEarlyAppOffsetNs.value_or(highFpsLateAppOffsetNs)},
109
110 {RefreshRateType::PERFORMANCE, highFpsEarlyGlSfOffsetNs.value_or(highFpsLateSfOffsetNs),
111 highFpsEarlyGlAppOffsetNs.value_or(highFpsLateAppOffsetNs)},
112
113 {RefreshRateType::PERFORMANCE, highFpsLateSfOffsetNs, highFpsLateAppOffsetNs},
114
115 thresholdForNextVsync};
Ana Krulec757f63a2019-01-25 10:46:18 -0800116}
117
118} // namespace impl
Dominik Laskowskieddeda12019-07-19 11:54:13 -0700119} // namespace android::scheduler