blob: a5822323ce890c4404e30f196c1f000a591e1abc [file] [log] [blame]
rnlee81d32602021-07-27 13:24:07 -07001/*
2 * Copyright (C) 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
Alec Mouri9b133ca2023-11-14 19:00:01 +000017#include <common/FlagManager.h>
rnlee81d32602021-07-27 13:24:07 -070018
19#include <SurfaceFlingerProperties.sysprop.h>
20#include <android-base/parsebool.h>
21#include <android-base/parseint.h>
Leon Scroggins IIIa37ca992022-02-02 18:08:20 -050022#include <android-base/properties.h>
rnlee81d32602021-07-27 13:24:07 -070023#include <android-base/stringprintf.h>
24#include <log/log.h>
Leon Scroggins IIIa37ca992022-02-02 18:08:20 -050025#include <renderengine/RenderEngine.h>
rnlee81d32602021-07-27 13:24:07 -070026#include <server_configurable_flags/get_flags.h>
27#include <cinttypes>
28
Ady Abrahamd6d80162023-10-23 12:57:41 -070029#include <com_android_graphics_surfaceflinger_flags.h>
30
rnlee81d32602021-07-27 13:24:07 -070031namespace android {
Ady Abrahamd6d80162023-10-23 12:57:41 -070032using namespace com::android::graphics::surfaceflinger;
33
rnlee81d32602021-07-27 13:24:07 -070034static constexpr const char* kExperimentNamespace = "surface_flinger_native_boot";
rnlee81d32602021-07-27 13:24:07 -070035
Ady Abraham53b0c492023-10-23 11:47:14 -070036std::unique_ptr<FlagManager> FlagManager::mInstance;
37std::once_flag FlagManager::mOnce;
38
39FlagManager::FlagManager(ConstructorTag) {}
rnlee81d32602021-07-27 13:24:07 -070040FlagManager::~FlagManager() = default;
41
rnlee81d32602021-07-27 13:24:07 -070042namespace {
Ady Abrahamc589dc42023-10-26 16:20:53 -070043std::optional<bool> parseBool(const char* str) {
rnlee81d32602021-07-27 13:24:07 -070044 base::ParseBoolResult parseResult = base::ParseBool(str);
45 switch (parseResult) {
46 case base::ParseBoolResult::kTrue:
47 return std::make_optional(true);
48 case base::ParseBoolResult::kFalse:
49 return std::make_optional(false);
50 case base::ParseBoolResult::kError:
51 return std::nullopt;
52 }
53}
Ady Abrahamc589dc42023-10-26 16:20:53 -070054
Ady Abrahamd6d80162023-10-23 12:57:41 -070055bool getFlagValue(std::function<bool()> getter, std::optional<bool> overrideValue) {
56 if (overrideValue.has_value()) {
57 return *overrideValue;
58 }
59
60 return getter();
61}
62
rnlee81d32602021-07-27 13:24:07 -070063} // namespace
64
Ady Abrahamc589dc42023-10-26 16:20:53 -070065const FlagManager& FlagManager::getInstance() {
66 return getMutableInstance();
rnlee81d32602021-07-27 13:24:07 -070067}
68
Ady Abrahamc589dc42023-10-26 16:20:53 -070069FlagManager& FlagManager::getMutableInstance() {
70 std::call_once(mOnce, [&] {
71 LOG_ALWAYS_FATAL_IF(mInstance, "Instance already created");
72 mInstance = std::make_unique<FlagManager>(ConstructorTag{});
73 });
74
75 return *mInstance;
76}
77
78void FlagManager::markBootCompleted() {
79 mBootCompleted = true;
80}
81
Ady Abrahamd6d80162023-10-23 12:57:41 -070082void FlagManager::setUnitTestMode() {
83 mUnitTestMode = true;
84
85 // Also set boot completed as we don't really care about it in unit testing
86 mBootCompleted = true;
87}
88
Ady Abrahamb6041f62023-11-03 12:14:26 -070089void FlagManager::dumpFlag(std::string& result, bool readonly, const char* name,
90 std::function<bool()> getter) const {
91 if (readonly || mBootCompleted) {
92 base::StringAppendF(&result, "%s: %s\n", name, getter() ? "true" : "false");
93 } else {
94 base::StringAppendF(&result, "%s: in progress (still booting)\n", name);
95 }
96}
97
Ady Abrahamc589dc42023-10-26 16:20:53 -070098void FlagManager::dump(std::string& result) const {
Ady Abrahamb6041f62023-11-03 12:14:26 -070099#define DUMP_FLAG_INTERVAL(name, readonly) \
100 dumpFlag(result, (readonly), #name, std::bind(&FlagManager::name, this))
101#define DUMP_SERVER_FLAG(name) DUMP_FLAG_INTERVAL(name, false)
102#define DUMP_READ_ONLY_FLAG(name) DUMP_FLAG_INTERVAL(name, true)
Ady Abrahamc589dc42023-10-26 16:20:53 -0700103
104 base::StringAppendF(&result, "FlagManager values: \n");
Ady Abrahamc589dc42023-10-26 16:20:53 -0700105
Ady Abrahamb6041f62023-11-03 12:14:26 -0700106 /// Legacy server flags ///
107 DUMP_SERVER_FLAG(use_adpf_cpu_hint);
108 DUMP_SERVER_FLAG(use_skia_tracing);
109
110 /// Trunk stable server flags ///
111 DUMP_SERVER_FLAG(late_boot_misc2);
112 DUMP_SERVER_FLAG(dont_skip_on_early);
Sally Qide329f22023-09-26 16:21:39 -0700113 DUMP_SERVER_FLAG(refresh_rate_overlay_on_external_display);
Ady Abrahamb6041f62023-11-03 12:14:26 -0700114
115 /// Trunk stable readonly flags ///
116 DUMP_READ_ONLY_FLAG(connected_display);
117 DUMP_READ_ONLY_FLAG(enable_small_area_detection);
118 DUMP_READ_ONLY_FLAG(misc1);
119 DUMP_READ_ONLY_FLAG(vrr_config);
120 DUMP_READ_ONLY_FLAG(hotplug2);
121 DUMP_READ_ONLY_FLAG(hdcp_level_hal);
122 DUMP_READ_ONLY_FLAG(multithreaded_present);
Sally Qif5721252023-11-17 11:14:53 -0800123 DUMP_READ_ONLY_FLAG(add_sf_skipped_frames_to_trace);
Jerry Chang04eb8e02023-11-15 08:06:07 +0000124 DUMP_READ_ONLY_FLAG(use_known_refresh_rate_for_fps_consistency);
Sally Qi567cf842023-11-14 15:25:29 -0800125 DUMP_READ_ONLY_FLAG(cache_if_source_crop_layer_only_moved);
Ady Abrahamb6041f62023-11-03 12:14:26 -0700126
127#undef DUMP_READ_ONLY_FLAG
128#undef DUMP_SERVER_FLAG
129#undef DUMP_FLAG_INTERVAL
Ady Abrahamc589dc42023-10-26 16:20:53 -0700130}
131
132std::optional<bool> FlagManager::getBoolProperty(const char* property) const {
133 return parseBool(base::GetProperty(property, "").c_str());
134}
135
136bool FlagManager::getServerConfigurableFlag(const char* experimentFlagName) const {
137 const auto value = server_configurable_flags::GetServerConfigurableFlag(kExperimentNamespace,
138 experimentFlagName, "");
139 const auto res = parseBool(value.c_str());
140 return res.has_value() && res.value();
141}
142
Ady Abrahamd6d80162023-10-23 12:57:41 -0700143#define FLAG_MANAGER_LEGACY_SERVER_FLAG(name, syspropOverride, serverFlagName) \
Ady Abrahamc589dc42023-10-26 16:20:53 -0700144 bool FlagManager::name() const { \
145 LOG_ALWAYS_FATAL_IF(!mBootCompleted, \
146 "Can't read %s before boot completed as it is server writable", \
147 __func__); \
148 const auto debugOverride = getBoolProperty(syspropOverride); \
149 if (debugOverride.has_value()) return debugOverride.value(); \
150 return getServerConfigurableFlag(serverFlagName); \
rnlee81d32602021-07-27 13:24:07 -0700151 }
rnlee81d32602021-07-27 13:24:07 -0700152
Ady Abrahamd6d80162023-10-23 12:57:41 -0700153#define FLAG_MANAGER_FLAG_INTERNAL(name, syspropOverride, checkForBootCompleted) \
154 bool FlagManager::name() const { \
155 if (checkForBootCompleted) { \
156 LOG_ALWAYS_FATAL_IF(!mBootCompleted, \
157 "Can't read %s before boot completed as it is server writable", \
158 __func__); \
159 } \
160 static std::optional<bool> debugOverride = getBoolProperty(syspropOverride); \
161 static bool value = getFlagValue([] { return flags::name(); }, debugOverride); \
162 if (mUnitTestMode) { \
163 /* \
164 * When testing, we don't want to rely on the cached values stored in the static \
165 * variables. \
166 */ \
167 debugOverride = getBoolProperty(syspropOverride); \
168 value = getFlagValue([] { return flags::name(); }, debugOverride); \
169 } \
170 return value; \
171 }
Matt Buckleyd23c7962021-09-21 20:43:00 +0000172
Ady Abrahamd6d80162023-10-23 12:57:41 -0700173#define FLAG_MANAGER_SERVER_FLAG(name, syspropOverride) \
174 FLAG_MANAGER_FLAG_INTERNAL(name, syspropOverride, true)
175
176#define FLAG_MANAGER_READ_ONLY_FLAG(name, syspropOverride) \
177 FLAG_MANAGER_FLAG_INTERNAL(name, syspropOverride, false)
178
179/// Legacy server flags ///
180FLAG_MANAGER_LEGACY_SERVER_FLAG(test_flag, "", "")
181FLAG_MANAGER_LEGACY_SERVER_FLAG(use_adpf_cpu_hint, "debug.sf.enable_adpf_cpu_hint",
182 "AdpfFeature__adpf_cpu_hint")
183FLAG_MANAGER_LEGACY_SERVER_FLAG(use_skia_tracing, PROPERTY_SKIA_ATRACE_ENABLED,
184 "SkiaTracingFeature__use_skia_tracing")
185
186/// Trunk stable readonly flags ///
187FLAG_MANAGER_READ_ONLY_FLAG(connected_display, "")
188FLAG_MANAGER_READ_ONLY_FLAG(enable_small_area_detection, "")
189FLAG_MANAGER_READ_ONLY_FLAG(misc1, "")
190FLAG_MANAGER_READ_ONLY_FLAG(vrr_config, "debug.sf.enable_vrr_config")
Brian Johnson8c144002023-10-30 15:47:44 -0700191FLAG_MANAGER_READ_ONLY_FLAG(hotplug2, "")
Brian Johnson4f095462023-10-31 10:12:55 -0700192FLAG_MANAGER_READ_ONLY_FLAG(hdcp_level_hal, "")
Leon Scroggins IIIb315af52023-11-02 10:03:23 -0400193FLAG_MANAGER_READ_ONLY_FLAG(multithreaded_present, "debug.sf.multithreaded_present")
Sally Qif5721252023-11-17 11:14:53 -0800194FLAG_MANAGER_READ_ONLY_FLAG(add_sf_skipped_frames_to_trace, "")
Jerry Chang04eb8e02023-11-15 08:06:07 +0000195FLAG_MANAGER_READ_ONLY_FLAG(use_known_refresh_rate_for_fps_consistency, "")
Sally Qi567cf842023-11-14 15:25:29 -0800196FLAG_MANAGER_READ_ONLY_FLAG(cache_if_source_crop_layer_only_moved,
197 "debug.sf.cache_source_crop_only_moved")
Ady Abrahamd6d80162023-10-23 12:57:41 -0700198
199/// Trunk stable server flags ///
200FLAG_MANAGER_SERVER_FLAG(late_boot_misc2, "")
Sally Qide329f22023-09-26 16:21:39 -0700201FLAG_MANAGER_SERVER_FLAG(refresh_rate_overlay_on_external_display, "")
Ady Abrahamd6d80162023-10-23 12:57:41 -0700202
203/// Exceptions ///
204bool FlagManager::dont_skip_on_early() const {
205 // Even though this is a server writable flag, we do call it before boot completed, but that's
206 // fine since the decision is done per frame. We can't do caching though.
207 return flags::dont_skip_on_early();
208}
Leon Scroggins IIIa37ca992022-02-02 18:08:20 -0500209
rnlee81d32602021-07-27 13:24:07 -0700210} // namespace android