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