blob: b3041a12552d3e054faf9c746c5430512cc185a0 [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 Abraham3f84c502023-11-30 18:18:06 -0800126 DUMP_READ_ONLY_FLAG(enable_fro_dependent_features);
Chavi Weingarten18fa7c62023-11-28 21:16:03 +0000127 DUMP_READ_ONLY_FLAG(display_protected);
Alec Mourif97df4d2023-09-06 02:10:05 +0000128 DUMP_READ_ONLY_FLAG(fp16_client_target);
Ady Abrahamb6041f62023-11-03 12:14:26 -0700129
130#undef DUMP_READ_ONLY_FLAG
131#undef DUMP_SERVER_FLAG
132#undef DUMP_FLAG_INTERVAL
Ady Abrahamc589dc42023-10-26 16:20:53 -0700133}
134
135std::optional<bool> FlagManager::getBoolProperty(const char* property) const {
136 return parseBool(base::GetProperty(property, "").c_str());
137}
138
139bool FlagManager::getServerConfigurableFlag(const char* experimentFlagName) const {
140 const auto value = server_configurable_flags::GetServerConfigurableFlag(kExperimentNamespace,
141 experimentFlagName, "");
142 const auto res = parseBool(value.c_str());
143 return res.has_value() && res.value();
144}
145
Ady Abrahamd6d80162023-10-23 12:57:41 -0700146#define FLAG_MANAGER_LEGACY_SERVER_FLAG(name, syspropOverride, serverFlagName) \
Ady Abrahamc589dc42023-10-26 16:20:53 -0700147 bool FlagManager::name() const { \
148 LOG_ALWAYS_FATAL_IF(!mBootCompleted, \
149 "Can't read %s before boot completed as it is server writable", \
150 __func__); \
151 const auto debugOverride = getBoolProperty(syspropOverride); \
152 if (debugOverride.has_value()) return debugOverride.value(); \
153 return getServerConfigurableFlag(serverFlagName); \
rnlee81d32602021-07-27 13:24:07 -0700154 }
rnlee81d32602021-07-27 13:24:07 -0700155
Ady Abrahamd6d80162023-10-23 12:57:41 -0700156#define FLAG_MANAGER_FLAG_INTERNAL(name, syspropOverride, checkForBootCompleted) \
157 bool FlagManager::name() const { \
158 if (checkForBootCompleted) { \
159 LOG_ALWAYS_FATAL_IF(!mBootCompleted, \
160 "Can't read %s before boot completed as it is server writable", \
161 __func__); \
162 } \
Leon Scroggins IIId8e36f32023-11-22 14:40:36 -0500163 static const std::optional<bool> debugOverride = getBoolProperty(syspropOverride); \
164 static const bool value = getFlagValue([] { return flags::name(); }, debugOverride); \
Ady Abrahamd6d80162023-10-23 12:57:41 -0700165 if (mUnitTestMode) { \
166 /* \
Leon Scroggins IIId8e36f32023-11-22 14:40:36 -0500167 * When testing, we don't want to rely on the cached `value` or the debugOverride. \
Ady Abrahamd6d80162023-10-23 12:57:41 -0700168 */ \
Leon Scroggins IIId8e36f32023-11-22 14:40:36 -0500169 return flags::name(); \
Ady Abrahamd6d80162023-10-23 12:57:41 -0700170 } \
171 return value; \
172 }
Matt Buckleyd23c7962021-09-21 20:43:00 +0000173
Ady Abrahamd6d80162023-10-23 12:57:41 -0700174#define FLAG_MANAGER_SERVER_FLAG(name, syspropOverride) \
175 FLAG_MANAGER_FLAG_INTERNAL(name, syspropOverride, true)
176
177#define FLAG_MANAGER_READ_ONLY_FLAG(name, syspropOverride) \
178 FLAG_MANAGER_FLAG_INTERNAL(name, syspropOverride, false)
179
180/// Legacy server flags ///
181FLAG_MANAGER_LEGACY_SERVER_FLAG(test_flag, "", "")
182FLAG_MANAGER_LEGACY_SERVER_FLAG(use_adpf_cpu_hint, "debug.sf.enable_adpf_cpu_hint",
183 "AdpfFeature__adpf_cpu_hint")
184FLAG_MANAGER_LEGACY_SERVER_FLAG(use_skia_tracing, PROPERTY_SKIA_ATRACE_ENABLED,
185 "SkiaTracingFeature__use_skia_tracing")
186
187/// Trunk stable readonly flags ///
188FLAG_MANAGER_READ_ONLY_FLAG(connected_display, "")
189FLAG_MANAGER_READ_ONLY_FLAG(enable_small_area_detection, "")
190FLAG_MANAGER_READ_ONLY_FLAG(misc1, "")
191FLAG_MANAGER_READ_ONLY_FLAG(vrr_config, "debug.sf.enable_vrr_config")
Brian Johnson8c144002023-10-30 15:47:44 -0700192FLAG_MANAGER_READ_ONLY_FLAG(hotplug2, "")
Brian Johnson4f095462023-10-31 10:12:55 -0700193FLAG_MANAGER_READ_ONLY_FLAG(hdcp_level_hal, "")
Leon Scroggins IIIb315af52023-11-02 10:03:23 -0400194FLAG_MANAGER_READ_ONLY_FLAG(multithreaded_present, "debug.sf.multithreaded_present")
Sally Qif5721252023-11-17 11:14:53 -0800195FLAG_MANAGER_READ_ONLY_FLAG(add_sf_skipped_frames_to_trace, "")
Jerry Chang04eb8e02023-11-15 08:06:07 +0000196FLAG_MANAGER_READ_ONLY_FLAG(use_known_refresh_rate_for_fps_consistency, "")
Sally Qi567cf842023-11-14 15:25:29 -0800197FLAG_MANAGER_READ_ONLY_FLAG(cache_if_source_crop_layer_only_moved,
198 "debug.sf.cache_source_crop_only_moved")
Ady Abraham3f84c502023-11-30 18:18:06 -0800199FLAG_MANAGER_READ_ONLY_FLAG(enable_fro_dependent_features, "")
Chavi Weingarten18fa7c62023-11-28 21:16:03 +0000200FLAG_MANAGER_READ_ONLY_FLAG(display_protected, "")
Alec Mourif97df4d2023-09-06 02:10:05 +0000201FLAG_MANAGER_READ_ONLY_FLAG(fp16_client_target, "debug.sf.fp16_client_target")
Ady Abrahamd6d80162023-10-23 12:57:41 -0700202
203/// Trunk stable server flags ///
204FLAG_MANAGER_SERVER_FLAG(late_boot_misc2, "")
Sally Qide329f22023-09-26 16:21:39 -0700205FLAG_MANAGER_SERVER_FLAG(refresh_rate_overlay_on_external_display, "")
Ady Abrahamd6d80162023-10-23 12:57:41 -0700206
207/// Exceptions ///
208bool FlagManager::dont_skip_on_early() const {
209 // Even though this is a server writable flag, we do call it before boot completed, but that's
210 // fine since the decision is done per frame. We can't do caching though.
211 return flags::dont_skip_on_early();
212}
Leon Scroggins IIIa37ca992022-02-02 18:08:20 -0500213
rnlee81d32602021-07-27 13:24:07 -0700214} // namespace android