blob: b07e7ace06ee755517eeba8401771eafc9585a6c [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 Qi4e20d7b2023-12-18 18:23:51 -0800125 DUMP_READ_ONLY_FLAG(cache_when_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);
Andy Yu8c2703d2023-11-03 11:22:46 -0700129 DUMP_READ_ONLY_FLAG(game_default_frame_rate);
Manali Bhutiyani96f866c2023-11-09 18:09:44 +0000130 DUMP_READ_ONLY_FLAG(enable_layer_command_batching);
Alec Mouri9892aac2023-12-11 21:16:59 +0000131 DUMP_READ_ONLY_FLAG(screenshot_fence_preservation);
Leon Scroggins III696bf932024-01-24 15:21:05 -0500132 DUMP_READ_ONLY_FLAG(vulkan_renderengine);
Ady Abrahamb6041f62023-11-03 12:14:26 -0700133#undef DUMP_READ_ONLY_FLAG
134#undef DUMP_SERVER_FLAG
135#undef DUMP_FLAG_INTERVAL
Ady Abrahamc589dc42023-10-26 16:20:53 -0700136}
137
138std::optional<bool> FlagManager::getBoolProperty(const char* property) const {
139 return parseBool(base::GetProperty(property, "").c_str());
140}
141
142bool FlagManager::getServerConfigurableFlag(const char* experimentFlagName) const {
143 const auto value = server_configurable_flags::GetServerConfigurableFlag(kExperimentNamespace,
144 experimentFlagName, "");
145 const auto res = parseBool(value.c_str());
146 return res.has_value() && res.value();
147}
148
Ady Abrahamd6d80162023-10-23 12:57:41 -0700149#define FLAG_MANAGER_LEGACY_SERVER_FLAG(name, syspropOverride, serverFlagName) \
Ady Abrahamc589dc42023-10-26 16:20:53 -0700150 bool FlagManager::name() const { \
151 LOG_ALWAYS_FATAL_IF(!mBootCompleted, \
152 "Can't read %s before boot completed as it is server writable", \
153 __func__); \
154 const auto debugOverride = getBoolProperty(syspropOverride); \
155 if (debugOverride.has_value()) return debugOverride.value(); \
156 return getServerConfigurableFlag(serverFlagName); \
rnlee81d32602021-07-27 13:24:07 -0700157 }
rnlee81d32602021-07-27 13:24:07 -0700158
Ady Abrahamd6d80162023-10-23 12:57:41 -0700159#define FLAG_MANAGER_FLAG_INTERNAL(name, syspropOverride, checkForBootCompleted) \
160 bool FlagManager::name() const { \
161 if (checkForBootCompleted) { \
162 LOG_ALWAYS_FATAL_IF(!mBootCompleted, \
163 "Can't read %s before boot completed as it is server writable", \
164 __func__); \
165 } \
Leon Scroggins IIId8e36f32023-11-22 14:40:36 -0500166 static const std::optional<bool> debugOverride = getBoolProperty(syspropOverride); \
167 static const bool value = getFlagValue([] { return flags::name(); }, debugOverride); \
Ady Abrahamd6d80162023-10-23 12:57:41 -0700168 if (mUnitTestMode) { \
169 /* \
Leon Scroggins IIId8e36f32023-11-22 14:40:36 -0500170 * When testing, we don't want to rely on the cached `value` or the debugOverride. \
Ady Abrahamd6d80162023-10-23 12:57:41 -0700171 */ \
Leon Scroggins IIId8e36f32023-11-22 14:40:36 -0500172 return flags::name(); \
Ady Abrahamd6d80162023-10-23 12:57:41 -0700173 } \
174 return value; \
175 }
Matt Buckleyd23c7962021-09-21 20:43:00 +0000176
Ady Abrahamd6d80162023-10-23 12:57:41 -0700177#define FLAG_MANAGER_SERVER_FLAG(name, syspropOverride) \
178 FLAG_MANAGER_FLAG_INTERNAL(name, syspropOverride, true)
179
180#define FLAG_MANAGER_READ_ONLY_FLAG(name, syspropOverride) \
181 FLAG_MANAGER_FLAG_INTERNAL(name, syspropOverride, false)
182
183/// Legacy server flags ///
184FLAG_MANAGER_LEGACY_SERVER_FLAG(test_flag, "", "")
185FLAG_MANAGER_LEGACY_SERVER_FLAG(use_adpf_cpu_hint, "debug.sf.enable_adpf_cpu_hint",
186 "AdpfFeature__adpf_cpu_hint")
187FLAG_MANAGER_LEGACY_SERVER_FLAG(use_skia_tracing, PROPERTY_SKIA_ATRACE_ENABLED,
188 "SkiaTracingFeature__use_skia_tracing")
189
190/// Trunk stable readonly flags ///
191FLAG_MANAGER_READ_ONLY_FLAG(connected_display, "")
192FLAG_MANAGER_READ_ONLY_FLAG(enable_small_area_detection, "")
193FLAG_MANAGER_READ_ONLY_FLAG(misc1, "")
194FLAG_MANAGER_READ_ONLY_FLAG(vrr_config, "debug.sf.enable_vrr_config")
Brian Johnson8c144002023-10-30 15:47:44 -0700195FLAG_MANAGER_READ_ONLY_FLAG(hotplug2, "")
Brian Johnson4f095462023-10-31 10:12:55 -0700196FLAG_MANAGER_READ_ONLY_FLAG(hdcp_level_hal, "")
Leon Scroggins IIIb315af52023-11-02 10:03:23 -0400197FLAG_MANAGER_READ_ONLY_FLAG(multithreaded_present, "debug.sf.multithreaded_present")
Sally Qif5721252023-11-17 11:14:53 -0800198FLAG_MANAGER_READ_ONLY_FLAG(add_sf_skipped_frames_to_trace, "")
Jerry Chang04eb8e02023-11-15 08:06:07 +0000199FLAG_MANAGER_READ_ONLY_FLAG(use_known_refresh_rate_for_fps_consistency, "")
Sally Qi4e20d7b2023-12-18 18:23:51 -0800200FLAG_MANAGER_READ_ONLY_FLAG(cache_when_source_crop_layer_only_moved,
Sally Qi567cf842023-11-14 15:25:29 -0800201 "debug.sf.cache_source_crop_only_moved")
Ady Abraham3f84c502023-11-30 18:18:06 -0800202FLAG_MANAGER_READ_ONLY_FLAG(enable_fro_dependent_features, "")
Chavi Weingarten18fa7c62023-11-28 21:16:03 +0000203FLAG_MANAGER_READ_ONLY_FLAG(display_protected, "")
Alec Mourif97df4d2023-09-06 02:10:05 +0000204FLAG_MANAGER_READ_ONLY_FLAG(fp16_client_target, "debug.sf.fp16_client_target")
Andy Yu8c2703d2023-11-03 11:22:46 -0700205FLAG_MANAGER_READ_ONLY_FLAG(game_default_frame_rate, "")
Manali Bhutiyani96f866c2023-11-09 18:09:44 +0000206FLAG_MANAGER_READ_ONLY_FLAG(enable_layer_command_batching, "")
Alec Mouri9892aac2023-12-11 21:16:59 +0000207FLAG_MANAGER_READ_ONLY_FLAG(screenshot_fence_preservation, "debug.sf.screenshot_fence_preservation")
Leon Scroggins III696bf932024-01-24 15:21:05 -0500208FLAG_MANAGER_READ_ONLY_FLAG(vulkan_renderengine, "debug.renderengine.vulkan")
Ady Abrahamd6d80162023-10-23 12:57:41 -0700209
210/// Trunk stable server flags ///
211FLAG_MANAGER_SERVER_FLAG(late_boot_misc2, "")
Sally Qide329f22023-09-26 16:21:39 -0700212FLAG_MANAGER_SERVER_FLAG(refresh_rate_overlay_on_external_display, "")
Ady Abrahamd6d80162023-10-23 12:57:41 -0700213
214/// Exceptions ///
215bool FlagManager::dont_skip_on_early() const {
216 // Even though this is a server writable flag, we do call it before boot completed, but that's
217 // fine since the decision is done per frame. We can't do caching though.
218 return flags::dont_skip_on_early();
219}
Leon Scroggins IIIa37ca992022-02-02 18:08:20 -0500220
rnlee81d32602021-07-27 13:24:07 -0700221} // namespace android