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