rnlee | 81d3260 | 2021-07-27 13:24:07 -0700 | [diff] [blame] | 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 "FlagManager.h" |
| 18 | |
| 19 | #include <SurfaceFlingerProperties.sysprop.h> |
| 20 | #include <android-base/parsebool.h> |
| 21 | #include <android-base/parseint.h> |
Leon Scroggins III | a37ca99 | 2022-02-02 18:08:20 -0500 | [diff] [blame] | 22 | #include <android-base/properties.h> |
rnlee | 81d3260 | 2021-07-27 13:24:07 -0700 | [diff] [blame] | 23 | #include <android-base/stringprintf.h> |
| 24 | #include <log/log.h> |
Leon Scroggins III | a37ca99 | 2022-02-02 18:08:20 -0500 | [diff] [blame] | 25 | #include <renderengine/RenderEngine.h> |
rnlee | 81d3260 | 2021-07-27 13:24:07 -0700 | [diff] [blame] | 26 | #include <server_configurable_flags/get_flags.h> |
| 27 | #include <cinttypes> |
| 28 | |
Ady Abraham | d6d8016 | 2023-10-23 12:57:41 -0700 | [diff] [blame] | 29 | #include <com_android_graphics_surfaceflinger_flags.h> |
| 30 | |
rnlee | 81d3260 | 2021-07-27 13:24:07 -0700 | [diff] [blame] | 31 | namespace android { |
Ady Abraham | d6d8016 | 2023-10-23 12:57:41 -0700 | [diff] [blame] | 32 | using namespace com::android::graphics::surfaceflinger; |
| 33 | |
rnlee | 81d3260 | 2021-07-27 13:24:07 -0700 | [diff] [blame] | 34 | static constexpr const char* kExperimentNamespace = "surface_flinger_native_boot"; |
rnlee | 81d3260 | 2021-07-27 13:24:07 -0700 | [diff] [blame] | 35 | |
Ady Abraham | 53b0c49 | 2023-10-23 11:47:14 -0700 | [diff] [blame] | 36 | std::unique_ptr<FlagManager> FlagManager::mInstance; |
| 37 | std::once_flag FlagManager::mOnce; |
| 38 | |
| 39 | FlagManager::FlagManager(ConstructorTag) {} |
rnlee | 81d3260 | 2021-07-27 13:24:07 -0700 | [diff] [blame] | 40 | FlagManager::~FlagManager() = default; |
| 41 | |
rnlee | 81d3260 | 2021-07-27 13:24:07 -0700 | [diff] [blame] | 42 | namespace { |
Ady Abraham | c589dc4 | 2023-10-26 16:20:53 -0700 | [diff] [blame] | 43 | std::optional<bool> parseBool(const char* str) { |
rnlee | 81d3260 | 2021-07-27 13:24:07 -0700 | [diff] [blame] | 44 | 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 Abraham | c589dc4 | 2023-10-26 16:20:53 -0700 | [diff] [blame] | 54 | |
Ady Abraham | d6d8016 | 2023-10-23 12:57:41 -0700 | [diff] [blame] | 55 | bool getFlagValue(std::function<bool()> getter, std::optional<bool> overrideValue) { |
| 56 | if (overrideValue.has_value()) { |
| 57 | return *overrideValue; |
| 58 | } |
| 59 | |
| 60 | return getter(); |
| 61 | } |
| 62 | |
Ady Abraham | c589dc4 | 2023-10-26 16:20:53 -0700 | [diff] [blame] | 63 | void dumpFlag(std::string& result, const char* name, std::function<bool()> getter) { |
| 64 | base::StringAppendF(&result, "%s: %s\n", name, getter() ? "true" : "false"); |
| 65 | } |
| 66 | |
rnlee | 81d3260 | 2021-07-27 13:24:07 -0700 | [diff] [blame] | 67 | } // namespace |
| 68 | |
Ady Abraham | c589dc4 | 2023-10-26 16:20:53 -0700 | [diff] [blame] | 69 | const FlagManager& FlagManager::getInstance() { |
| 70 | return getMutableInstance(); |
rnlee | 81d3260 | 2021-07-27 13:24:07 -0700 | [diff] [blame] | 71 | } |
| 72 | |
Ady Abraham | c589dc4 | 2023-10-26 16:20:53 -0700 | [diff] [blame] | 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 | |
| 82 | void FlagManager::markBootCompleted() { |
| 83 | mBootCompleted = true; |
| 84 | } |
| 85 | |
Ady Abraham | d6d8016 | 2023-10-23 12:57:41 -0700 | [diff] [blame] | 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 | |
Ady Abraham | c589dc4 | 2023-10-26 16:20:53 -0700 | [diff] [blame] | 93 | void FlagManager::dump(std::string& result) const { |
Ady Abraham | d6d8016 | 2023-10-23 12:57:41 -0700 | [diff] [blame] | 94 | #define DUMP_FLAG(name) dumpFlag(result, #name, std::bind(&FlagManager::name, this)) |
Ady Abraham | c589dc4 | 2023-10-26 16:20:53 -0700 | [diff] [blame] | 95 | |
| 96 | base::StringAppendF(&result, "FlagManager values: \n"); |
| 97 | DUMP_FLAG(use_adpf_cpu_hint); |
| 98 | DUMP_FLAG(use_skia_tracing); |
Ady Abraham | d6d8016 | 2023-10-23 12:57:41 -0700 | [diff] [blame] | 99 | DUMP_FLAG(connected_display); |
| 100 | DUMP_FLAG(dont_skip_on_early); |
| 101 | DUMP_FLAG(enable_small_area_detection); |
| 102 | DUMP_FLAG(misc1); |
| 103 | DUMP_FLAG(late_boot_misc2); |
| 104 | DUMP_FLAG(vrr_config); |
Brian Johnson | 8c14400 | 2023-10-30 15:47:44 -0700 | [diff] [blame] | 105 | DUMP_FLAG(hotplug2); |
Brian Johnson | 4f09546 | 2023-10-31 10:12:55 -0700 | [diff] [blame] | 106 | DUMP_FLAG(hdcp_level_hal); |
Leon Scroggins III | b315af5 | 2023-11-02 10:03:23 -0400 | [diff] [blame] | 107 | DUMP_FLAG(multithreaded_present); |
Ady Abraham | c589dc4 | 2023-10-26 16:20:53 -0700 | [diff] [blame] | 108 | |
| 109 | #undef DUMP_FLAG |
| 110 | } |
| 111 | |
| 112 | std::optional<bool> FlagManager::getBoolProperty(const char* property) const { |
| 113 | return parseBool(base::GetProperty(property, "").c_str()); |
| 114 | } |
| 115 | |
| 116 | bool FlagManager::getServerConfigurableFlag(const char* experimentFlagName) const { |
| 117 | const auto value = server_configurable_flags::GetServerConfigurableFlag(kExperimentNamespace, |
| 118 | experimentFlagName, ""); |
| 119 | const auto res = parseBool(value.c_str()); |
| 120 | return res.has_value() && res.value(); |
| 121 | } |
| 122 | |
Ady Abraham | d6d8016 | 2023-10-23 12:57:41 -0700 | [diff] [blame] | 123 | #define FLAG_MANAGER_LEGACY_SERVER_FLAG(name, syspropOverride, serverFlagName) \ |
Ady Abraham | c589dc4 | 2023-10-26 16:20:53 -0700 | [diff] [blame] | 124 | bool FlagManager::name() const { \ |
| 125 | LOG_ALWAYS_FATAL_IF(!mBootCompleted, \ |
| 126 | "Can't read %s before boot completed as it is server writable", \ |
| 127 | __func__); \ |
| 128 | const auto debugOverride = getBoolProperty(syspropOverride); \ |
| 129 | if (debugOverride.has_value()) return debugOverride.value(); \ |
| 130 | return getServerConfigurableFlag(serverFlagName); \ |
rnlee | 81d3260 | 2021-07-27 13:24:07 -0700 | [diff] [blame] | 131 | } |
rnlee | 81d3260 | 2021-07-27 13:24:07 -0700 | [diff] [blame] | 132 | |
Ady Abraham | d6d8016 | 2023-10-23 12:57:41 -0700 | [diff] [blame] | 133 | #define FLAG_MANAGER_FLAG_INTERNAL(name, syspropOverride, checkForBootCompleted) \ |
| 134 | bool FlagManager::name() const { \ |
| 135 | if (checkForBootCompleted) { \ |
| 136 | LOG_ALWAYS_FATAL_IF(!mBootCompleted, \ |
| 137 | "Can't read %s before boot completed as it is server writable", \ |
| 138 | __func__); \ |
| 139 | } \ |
| 140 | static std::optional<bool> debugOverride = getBoolProperty(syspropOverride); \ |
| 141 | static bool value = getFlagValue([] { return flags::name(); }, debugOverride); \ |
| 142 | if (mUnitTestMode) { \ |
| 143 | /* \ |
| 144 | * When testing, we don't want to rely on the cached values stored in the static \ |
| 145 | * variables. \ |
| 146 | */ \ |
| 147 | debugOverride = getBoolProperty(syspropOverride); \ |
| 148 | value = getFlagValue([] { return flags::name(); }, debugOverride); \ |
| 149 | } \ |
| 150 | return value; \ |
| 151 | } |
Matt Buckley | d23c796 | 2021-09-21 20:43:00 +0000 | [diff] [blame] | 152 | |
Ady Abraham | d6d8016 | 2023-10-23 12:57:41 -0700 | [diff] [blame] | 153 | #define FLAG_MANAGER_SERVER_FLAG(name, syspropOverride) \ |
| 154 | FLAG_MANAGER_FLAG_INTERNAL(name, syspropOverride, true) |
| 155 | |
| 156 | #define FLAG_MANAGER_READ_ONLY_FLAG(name, syspropOverride) \ |
| 157 | FLAG_MANAGER_FLAG_INTERNAL(name, syspropOverride, false) |
| 158 | |
| 159 | /// Legacy server flags /// |
| 160 | FLAG_MANAGER_LEGACY_SERVER_FLAG(test_flag, "", "") |
| 161 | FLAG_MANAGER_LEGACY_SERVER_FLAG(use_adpf_cpu_hint, "debug.sf.enable_adpf_cpu_hint", |
| 162 | "AdpfFeature__adpf_cpu_hint") |
| 163 | FLAG_MANAGER_LEGACY_SERVER_FLAG(use_skia_tracing, PROPERTY_SKIA_ATRACE_ENABLED, |
| 164 | "SkiaTracingFeature__use_skia_tracing") |
| 165 | |
| 166 | /// Trunk stable readonly flags /// |
| 167 | FLAG_MANAGER_READ_ONLY_FLAG(connected_display, "") |
| 168 | FLAG_MANAGER_READ_ONLY_FLAG(enable_small_area_detection, "") |
| 169 | FLAG_MANAGER_READ_ONLY_FLAG(misc1, "") |
| 170 | FLAG_MANAGER_READ_ONLY_FLAG(vrr_config, "debug.sf.enable_vrr_config") |
Brian Johnson | 8c14400 | 2023-10-30 15:47:44 -0700 | [diff] [blame] | 171 | FLAG_MANAGER_READ_ONLY_FLAG(hotplug2, "") |
Brian Johnson | 4f09546 | 2023-10-31 10:12:55 -0700 | [diff] [blame] | 172 | FLAG_MANAGER_READ_ONLY_FLAG(hdcp_level_hal, "") |
Leon Scroggins III | b315af5 | 2023-11-02 10:03:23 -0400 | [diff] [blame] | 173 | FLAG_MANAGER_READ_ONLY_FLAG(multithreaded_present, "debug.sf.multithreaded_present") |
Ady Abraham | d6d8016 | 2023-10-23 12:57:41 -0700 | [diff] [blame] | 174 | |
| 175 | /// Trunk stable server flags /// |
| 176 | FLAG_MANAGER_SERVER_FLAG(late_boot_misc2, "") |
| 177 | |
| 178 | /// Exceptions /// |
| 179 | bool FlagManager::dont_skip_on_early() const { |
| 180 | // Even though this is a server writable flag, we do call it before boot completed, but that's |
| 181 | // fine since the decision is done per frame. We can't do caching though. |
| 182 | return flags::dont_skip_on_early(); |
| 183 | } |
Leon Scroggins III | a37ca99 | 2022-02-02 18:08:20 -0500 | [diff] [blame] | 184 | |
rnlee | 81d3260 | 2021-07-27 13:24:07 -0700 | [diff] [blame] | 185 | } // namespace android |