1 /*
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
17 #include <common/FlagManager.h>
18
19 #include <SurfaceFlingerProperties.sysprop.h>
20 #include <android-base/parsebool.h>
21 #include <android-base/parseint.h>
22 #include <android-base/properties.h>
23 #include <android-base/stringprintf.h>
24 #include <log/log.h>
25 #include <renderengine/RenderEngine.h>
26 #include <server_configurable_flags/get_flags.h>
27 #include <cinttypes>
28
29 #include <android_companion_virtualdevice_flags.h>
30 #include <android_hardware_flags.h>
31 #include <android_os.h>
32 #include <com_android_graphics_libgui_flags.h>
33 #include <com_android_graphics_surfaceflinger_flags.h>
34 #include <com_android_server_display_feature_flags.h>
35
36 namespace android {
37 using namespace com::android::graphics::surfaceflinger;
38
39 static constexpr const char* kExperimentNamespace = "surface_flinger_native_boot";
40
41 std::unique_ptr<FlagManager> FlagManager::mInstance;
42 std::once_flag FlagManager::mOnce;
43
FlagManager(ConstructorTag)44 FlagManager::FlagManager(ConstructorTag) {}
45 FlagManager::~FlagManager() = default;
46
47 namespace {
parseBool(const char * str)48 std::optional<bool> parseBool(const char* str) {
49 base::ParseBoolResult parseResult = base::ParseBool(str);
50 switch (parseResult) {
51 case base::ParseBoolResult::kTrue:
52 return std::make_optional(true);
53 case base::ParseBoolResult::kFalse:
54 return std::make_optional(false);
55 case base::ParseBoolResult::kError:
56 return std::nullopt;
57 }
58 }
59
getFlagValue(std::function<bool ()> getter,std::optional<bool> overrideValue)60 bool getFlagValue(std::function<bool()> getter, std::optional<bool> overrideValue) {
61 if (overrideValue.has_value()) {
62 return *overrideValue;
63 }
64
65 return getter();
66 }
67
68 } // namespace
69
getInstance()70 const FlagManager& FlagManager::getInstance() {
71 return getMutableInstance();
72 }
73
getMutableInstance()74 FlagManager& FlagManager::getMutableInstance() {
75 std::call_once(mOnce, [&] {
76 LOG_ALWAYS_FATAL_IF(mInstance, "Instance already created");
77 mInstance = std::make_unique<FlagManager>(ConstructorTag{});
78 });
79
80 return *mInstance;
81 }
82
markBootCompleted()83 void FlagManager::markBootCompleted() {
84 mBootCompleted = true;
85 }
86
setUnitTestMode()87 void FlagManager::setUnitTestMode() {
88 mUnitTestMode = true;
89
90 // Also set boot completed as we don't really care about it in unit testing
91 mBootCompleted = true;
92 }
93
dumpFlag(std::string & result,bool aconfig,const char * name,std::function<bool ()> getter) const94 void FlagManager::dumpFlag(std::string& result, bool aconfig, const char* name,
95 std::function<bool()> getter) const {
96 if (aconfig || mBootCompleted) {
97 base::StringAppendF(&result, "%s: %s\n", name, getter() ? "true" : "false");
98 } else {
99 base::StringAppendF(&result, "%s: in progress (still booting)\n", name);
100 }
101 }
102
dump(std::string & result) const103 void FlagManager::dump(std::string& result) const {
104 #define DUMP_FLAG_INTERNAL(name, aconfig) \
105 dumpFlag(result, (aconfig), #name, std::bind(&FlagManager::name, this))
106 #define DUMP_LEGACY_SERVER_FLAG(name) DUMP_FLAG_INTERNAL(name, false)
107 #define DUMP_ACONFIG_FLAG(name) DUMP_FLAG_INTERNAL(name, true)
108 #define DUMP_SYSPROP_FLAG(name) \
109 dumpFlag(result, (true), "debug.sf." #name, std::bind(&FlagManager::name, this))
110
111 base::StringAppendF(&result, "FlagManager values: \n");
112
113 /// Sysprop flags ///
114 DUMP_SYSPROP_FLAG(disable_sched_fifo_sf);
115 DUMP_SYSPROP_FLAG(disable_sched_fifo_sf_binder);
116 DUMP_SYSPROP_FLAG(disable_sched_fifo_sf_sched);
117 DUMP_SYSPROP_FLAG(disable_sched_fifo_re);
118 DUMP_SYSPROP_FLAG(disable_sched_fifo_composer);
119 DUMP_SYSPROP_FLAG(disable_sched_fifo_composer_callback);
120
121 /// Legacy server flags ///
122 DUMP_LEGACY_SERVER_FLAG(use_adpf_cpu_hint);
123 DUMP_LEGACY_SERVER_FLAG(use_skia_tracing);
124
125 /// Trunk stable server (R/W) flags ///
126 DUMP_ACONFIG_FLAG(adpf_gpu_sf);
127 DUMP_ACONFIG_FLAG(adpf_native_session_manager);
128 DUMP_ACONFIG_FLAG(adpf_use_fmq_channel);
129 DUMP_ACONFIG_FLAG(correct_virtual_display_power_state);
130 DUMP_ACONFIG_FLAG(graphite_renderengine_preview_rollout);
131 DUMP_ACONFIG_FLAG(increase_missed_frame_jank_threshold);
132 DUMP_ACONFIG_FLAG(monitor_buffer_fences);
133 DUMP_ACONFIG_FLAG(refresh_rate_overlay_on_external_display);
134 DUMP_ACONFIG_FLAG(vsync_predictor_recovery);
135
136 /// Trunk stable readonly flags ///
137 /// IMPORTANT - please keep alphabetize to reduce merge conflicts
138 DUMP_ACONFIG_FLAG(add_sf_skipped_frames_to_trace);
139 DUMP_ACONFIG_FLAG(adpf_fmq_sf);
140 DUMP_ACONFIG_FLAG(allow_n_vsyncs_in_targeter);
141 DUMP_ACONFIG_FLAG(arr_setframerate_gte_enum);
142 DUMP_ACONFIG_FLAG(begone_bright_hlg);
143 DUMP_ACONFIG_FLAG(cache_when_source_crop_layer_only_moved);
144 DUMP_ACONFIG_FLAG(commit_not_composited);
145 DUMP_ACONFIG_FLAG(connected_display_hdr);
146 DUMP_ACONFIG_FLAG(correct_dpi_with_display_size);
147 DUMP_ACONFIG_FLAG(deprecate_frame_tracker);
148 DUMP_ACONFIG_FLAG(deprecate_vsync_sf);
149 DUMP_ACONFIG_FLAG(detached_mirror);
150 DUMP_ACONFIG_FLAG(display_config_error_hal);
151 DUMP_ACONFIG_FLAG(display_protected);
152 DUMP_ACONFIG_FLAG(dont_skip_on_early_ro);
153 DUMP_ACONFIG_FLAG(enable_fro_dependent_features);
154 DUMP_ACONFIG_FLAG(enable_layer_command_batching);
155 DUMP_ACONFIG_FLAG(enable_small_area_detection);
156 DUMP_ACONFIG_FLAG(filter_frames_before_trace_starts);
157 DUMP_ACONFIG_FLAG(flush_buffer_slots_to_uncache);
158 DUMP_ACONFIG_FLAG(force_compile_graphite_renderengine);
159 DUMP_ACONFIG_FLAG(fp16_client_target);
160 DUMP_ACONFIG_FLAG(frame_rate_category_mrr);
161 DUMP_ACONFIG_FLAG(game_default_frame_rate);
162 DUMP_ACONFIG_FLAG(graphite_renderengine);
163 DUMP_ACONFIG_FLAG(hdcp_level_hal);
164 DUMP_ACONFIG_FLAG(hdcp_negotiation);
165 DUMP_ACONFIG_FLAG(idle_screen_refresh_rate_timeout);
166 DUMP_ACONFIG_FLAG(latch_unsignaled_with_auto_refresh_changed);
167 DUMP_ACONFIG_FLAG(local_tonemap_screenshots);
168 DUMP_ACONFIG_FLAG(misc1);
169 DUMP_ACONFIG_FLAG(no_vsyncs_on_screen_off);
170 DUMP_ACONFIG_FLAG(override_trusted_overlay);
171 DUMP_ACONFIG_FLAG(protected_if_client);
172 DUMP_ACONFIG_FLAG(reject_dupe_layerstacks);
173 DUMP_ACONFIG_FLAG(renderable_buffer_usage);
174 DUMP_ACONFIG_FLAG(restore_blur_step);
175 DUMP_ACONFIG_FLAG(skip_invisible_windows_in_input);
176 DUMP_ACONFIG_FLAG(stable_edid_ids);
177 DUMP_ACONFIG_FLAG(synced_resolution_switch);
178 DUMP_ACONFIG_FLAG(trace_frame_rate_override);
179 DUMP_ACONFIG_FLAG(true_hdr_screenshots);
180 DUMP_ACONFIG_FLAG(use_known_refresh_rate_for_fps_consistency);
181 DUMP_ACONFIG_FLAG(vrr_bugfix_24q4);
182 DUMP_ACONFIG_FLAG(vrr_bugfix_dropped_frame);
183 DUMP_ACONFIG_FLAG(vrr_config);
184 DUMP_ACONFIG_FLAG(vulkan_renderengine);
185 DUMP_ACONFIG_FLAG(window_blur_kawase2);
186 /// IMPORTANT - please keep alphabetize to reduce merge conflicts
187
188 #undef DUMP_ACONFIG_FLAG
189 #undef DUMP_LEGACY_SERVER_FLAG
190 #undef DUMP_FLAG_INTERVAL
191 }
192
getBoolProperty(const char * property) const193 std::optional<bool> FlagManager::getBoolProperty(const char* property) const {
194 return parseBool(base::GetProperty(property, "").c_str());
195 }
196
getServerConfigurableFlag(const char * experimentFlagName) const197 bool FlagManager::getServerConfigurableFlag(const char* experimentFlagName) const {
198 const auto value = server_configurable_flags::GetServerConfigurableFlag(kExperimentNamespace,
199 experimentFlagName, "");
200 const auto res = parseBool(value.c_str());
201 return res.has_value() && res.value();
202 }
203 #define FLAG_MANAGER_SYSPROP_FLAG(name, defaultVal) \
204 bool FlagManager::name() const { \
205 static const bool kFlagValue = \
206 base::GetBoolProperty("debug.sf." #name, /* default value*/ defaultVal); \
207 return kFlagValue; \
208 }
209
210 #define FLAG_MANAGER_LEGACY_SERVER_FLAG(name, syspropOverride, serverFlagName) \
211 bool FlagManager::name() const { \
212 LOG_ALWAYS_FATAL_IF(!mBootCompleted, \
213 "Can't read %s before boot completed as it is server writable", \
214 __func__); \
215 const auto debugOverride = getBoolProperty(syspropOverride); \
216 if (debugOverride.has_value()) return debugOverride.value(); \
217 return getServerConfigurableFlag(serverFlagName); \
218 }
219
220 #define FLAG_MANAGER_ACONFIG_INTERNAL(name, syspropOverride, owner) \
221 bool FlagManager::name() const { \
222 static const std::optional<bool> debugOverride = getBoolProperty(syspropOverride); \
223 static const bool value = getFlagValue([] { return owner ::name(); }, debugOverride); \
224 if (mUnitTestMode) { \
225 /* \
226 * When testing, we don't want to rely on the cached `value` or the debugOverride. \
227 */ \
228 return owner ::name(); \
229 } \
230 return value; \
231 }
232
233 #define FLAG_MANAGER_ACONFIG_FLAG(name, syspropOverride) \
234 FLAG_MANAGER_ACONFIG_INTERNAL(name, syspropOverride, flags)
235
236 #define FLAG_MANAGER_ACONFIG_FLAG_IMPORTED(name, syspropOverride, owner) \
237 FLAG_MANAGER_ACONFIG_INTERNAL(name, syspropOverride, owner)
238
239 /// Debug sysprop flags - default value is always false ///
240 FLAG_MANAGER_SYSPROP_FLAG(disable_sched_fifo_sf, /* default */ false)
241 FLAG_MANAGER_SYSPROP_FLAG(disable_sched_fifo_sf_binder, /* default */ false)
242 FLAG_MANAGER_SYSPROP_FLAG(disable_sched_fifo_sf_sched, /* default */ false)
243 FLAG_MANAGER_SYSPROP_FLAG(disable_sched_fifo_re, /* default */ false)
244 FLAG_MANAGER_SYSPROP_FLAG(disable_sched_fifo_composer, /* default */ false)
245 FLAG_MANAGER_SYSPROP_FLAG(disable_sched_fifo_composer_callback, /* default */ false)
246
247 /// Legacy server flags ///
248 FLAG_MANAGER_LEGACY_SERVER_FLAG(test_flag, "", "")
249 FLAG_MANAGER_LEGACY_SERVER_FLAG(use_adpf_cpu_hint, "debug.sf.enable_adpf_cpu_hint",
250 "AdpfFeature__adpf_cpu_hint")
251 FLAG_MANAGER_LEGACY_SERVER_FLAG(use_skia_tracing, PROPERTY_SKIA_ATRACE_ENABLED,
252 "SkiaTracingFeature__use_skia_tracing")
253
254 /// Trunk stable readonly flags ///
255 FLAG_MANAGER_ACONFIG_FLAG(adpf_fmq_sf, "")
256 FLAG_MANAGER_ACONFIG_FLAG(arr_setframerate_gte_enum, "debug.sf.arr_setframerate_gte_enum")
257 FLAG_MANAGER_ACONFIG_FLAG(enable_small_area_detection, "")
258 FLAG_MANAGER_ACONFIG_FLAG(stable_edid_ids, "debug.sf.stable_edid_ids")
259 FLAG_MANAGER_ACONFIG_FLAG(frame_rate_category_mrr, "debug.sf.frame_rate_category_mrr")
260 FLAG_MANAGER_ACONFIG_FLAG(misc1, "")
261 FLAG_MANAGER_ACONFIG_FLAG(vrr_config, "debug.sf.enable_vrr_config")
262 FLAG_MANAGER_ACONFIG_FLAG(hdcp_level_hal, "")
263 FLAG_MANAGER_ACONFIG_FLAG(hdcp_negotiation, "debug.sf.hdcp_negotiation");
264 FLAG_MANAGER_ACONFIG_FLAG(add_sf_skipped_frames_to_trace, "")
265 FLAG_MANAGER_ACONFIG_FLAG(use_known_refresh_rate_for_fps_consistency, "")
266 FLAG_MANAGER_ACONFIG_FLAG(cache_when_source_crop_layer_only_moved,
267 "debug.sf.cache_source_crop_only_moved")
268 FLAG_MANAGER_ACONFIG_FLAG(enable_fro_dependent_features, "")
269 FLAG_MANAGER_ACONFIG_FLAG(display_protected, "")
270 FLAG_MANAGER_ACONFIG_FLAG(fp16_client_target, "debug.sf.fp16_client_target")
271 FLAG_MANAGER_ACONFIG_FLAG(game_default_frame_rate, "")
272 FLAG_MANAGER_ACONFIG_FLAG(enable_layer_command_batching, "debug.sf.enable_layer_command_batching")
273 FLAG_MANAGER_ACONFIG_FLAG(vulkan_renderengine, "debug.renderengine.vulkan")
274 FLAG_MANAGER_ACONFIG_FLAG(renderable_buffer_usage, "")
275 FLAG_MANAGER_ACONFIG_FLAG(restore_blur_step, "debug.renderengine.restore_blur_step")
276 FLAG_MANAGER_ACONFIG_FLAG(dont_skip_on_early_ro, "")
277 FLAG_MANAGER_ACONFIG_FLAG(no_vsyncs_on_screen_off, "debug.sf.no_vsyncs_on_screen_off")
278 FLAG_MANAGER_ACONFIG_FLAG(protected_if_client, "")
279 FLAG_MANAGER_ACONFIG_FLAG(vrr_bugfix_24q4, "");
280 FLAG_MANAGER_ACONFIG_FLAG(vrr_bugfix_dropped_frame, "")
281 FLAG_MANAGER_ACONFIG_FLAG(graphite_renderengine, "debug.renderengine.graphite")
282 FLAG_MANAGER_ACONFIG_FLAG(filter_frames_before_trace_starts, "")
283 FLAG_MANAGER_ACONFIG_FLAG(latch_unsignaled_with_auto_refresh_changed, "");
284 FLAG_MANAGER_ACONFIG_FLAG(deprecate_vsync_sf, "");
285 FLAG_MANAGER_ACONFIG_FLAG(allow_n_vsyncs_in_targeter, "");
286 FLAG_MANAGER_ACONFIG_FLAG(detached_mirror, "");
287 FLAG_MANAGER_ACONFIG_FLAG(commit_not_composited, "");
288 FLAG_MANAGER_ACONFIG_FLAG(correct_dpi_with_display_size, "");
289 FLAG_MANAGER_ACONFIG_FLAG(local_tonemap_screenshots, "debug.sf.local_tonemap_screenshots");
290 FLAG_MANAGER_ACONFIG_FLAG(override_trusted_overlay, "");
291 FLAG_MANAGER_ACONFIG_FLAG(flush_buffer_slots_to_uncache, "");
292 FLAG_MANAGER_ACONFIG_FLAG(force_compile_graphite_renderengine, "");
293 FLAG_MANAGER_ACONFIG_FLAG(true_hdr_screenshots, "debug.sf.true_hdr_screenshots");
294 FLAG_MANAGER_ACONFIG_FLAG(display_config_error_hal, "");
295 FLAG_MANAGER_ACONFIG_FLAG(connected_display_hdr, "debug.sf.connected_display_hdr");
296 FLAG_MANAGER_ACONFIG_FLAG(deprecate_frame_tracker, "");
297 FLAG_MANAGER_ACONFIG_FLAG(skip_invisible_windows_in_input, "");
298 FLAG_MANAGER_ACONFIG_FLAG(begone_bright_hlg, "debug.sf.begone_bright_hlg");
299 FLAG_MANAGER_ACONFIG_FLAG(window_blur_kawase2, "");
300 FLAG_MANAGER_ACONFIG_FLAG(reject_dupe_layerstacks, "");
301 FLAG_MANAGER_ACONFIG_FLAG(synced_resolution_switch, "");
302
303 /// Trunk stable server (R/W) flags ///
304 FLAG_MANAGER_ACONFIG_FLAG(refresh_rate_overlay_on_external_display, "")
305 FLAG_MANAGER_ACONFIG_FLAG(adpf_gpu_sf, "")
306 FLAG_MANAGER_ACONFIG_FLAG(adpf_native_session_manager, "");
307 FLAG_MANAGER_ACONFIG_FLAG(graphite_renderengine_preview_rollout, "");
308 FLAG_MANAGER_ACONFIG_FLAG(increase_missed_frame_jank_threshold, "");
309 FLAG_MANAGER_ACONFIG_FLAG(monitor_buffer_fences, "");
310 FLAG_MANAGER_ACONFIG_FLAG(vsync_predictor_recovery, "");
311
312 /// Trunk stable server (R/W) flags from outside SurfaceFlinger ///
313 FLAG_MANAGER_ACONFIG_FLAG_IMPORTED(adpf_use_fmq_channel, "", android::os)
314 FLAG_MANAGER_ACONFIG_FLAG_IMPORTED(correct_virtual_display_power_state, "",
315 android::companion::virtualdevice::flags)
316
317 /// Trunk stable readonly flags from outside SurfaceFlinger ///
318 FLAG_MANAGER_ACONFIG_FLAG_IMPORTED(idle_screen_refresh_rate_timeout, "",
319 com::android::server::display::feature::flags)
320 FLAG_MANAGER_ACONFIG_FLAG_IMPORTED(adpf_use_fmq_channel_fixed, "", android::os)
321 FLAG_MANAGER_ACONFIG_FLAG_IMPORTED(trace_frame_rate_override, "",
322 com::android::graphics::libgui::flags);
323 FLAG_MANAGER_ACONFIG_FLAG_IMPORTED(luts_api, "",
324 android::hardware::flags);
325 } // namespace android
326