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 | |
| 29 | namespace android { |
| 30 | static constexpr const char* kExperimentNamespace = "surface_flinger_native_boot"; |
rnlee | 81d3260 | 2021-07-27 13:24:07 -0700 | [diff] [blame] | 31 | |
Ady Abraham | 53b0c49 | 2023-10-23 11:47:14 -0700 | [diff] [blame] | 32 | std::unique_ptr<FlagManager> FlagManager::mInstance; |
| 33 | std::once_flag FlagManager::mOnce; |
| 34 | |
| 35 | FlagManager::FlagManager(ConstructorTag) {} |
rnlee | 81d3260 | 2021-07-27 13:24:07 -0700 | [diff] [blame] | 36 | FlagManager::~FlagManager() = default; |
| 37 | |
rnlee | 81d3260 | 2021-07-27 13:24:07 -0700 | [diff] [blame] | 38 | namespace { |
Ady Abraham | c589dc4 | 2023-10-26 16:20:53 -0700 | [diff] [blame^] | 39 | std::optional<bool> parseBool(const char* str) { |
rnlee | 81d3260 | 2021-07-27 13:24:07 -0700 | [diff] [blame] | 40 | base::ParseBoolResult parseResult = base::ParseBool(str); |
| 41 | switch (parseResult) { |
| 42 | case base::ParseBoolResult::kTrue: |
| 43 | return std::make_optional(true); |
| 44 | case base::ParseBoolResult::kFalse: |
| 45 | return std::make_optional(false); |
| 46 | case base::ParseBoolResult::kError: |
| 47 | return std::nullopt; |
| 48 | } |
| 49 | } |
Ady Abraham | c589dc4 | 2023-10-26 16:20:53 -0700 | [diff] [blame^] | 50 | |
| 51 | void dumpFlag(std::string& result, const char* name, std::function<bool()> getter) { |
| 52 | base::StringAppendF(&result, "%s: %s\n", name, getter() ? "true" : "false"); |
| 53 | } |
| 54 | |
rnlee | 81d3260 | 2021-07-27 13:24:07 -0700 | [diff] [blame] | 55 | } // namespace |
| 56 | |
Ady Abraham | c589dc4 | 2023-10-26 16:20:53 -0700 | [diff] [blame^] | 57 | const FlagManager& FlagManager::getInstance() { |
| 58 | return getMutableInstance(); |
rnlee | 81d3260 | 2021-07-27 13:24:07 -0700 | [diff] [blame] | 59 | } |
| 60 | |
Ady Abraham | c589dc4 | 2023-10-26 16:20:53 -0700 | [diff] [blame^] | 61 | FlagManager& FlagManager::getMutableInstance() { |
| 62 | std::call_once(mOnce, [&] { |
| 63 | LOG_ALWAYS_FATAL_IF(mInstance, "Instance already created"); |
| 64 | mInstance = std::make_unique<FlagManager>(ConstructorTag{}); |
| 65 | }); |
| 66 | |
| 67 | return *mInstance; |
| 68 | } |
| 69 | |
| 70 | void FlagManager::markBootCompleted() { |
| 71 | mBootCompleted = true; |
| 72 | } |
| 73 | |
| 74 | void FlagManager::dump(std::string& result) const { |
| 75 | #define DUMP_FLAG(name) dumpFlag(result, #name, std::bind(&FlagManager::name, this)); |
| 76 | |
| 77 | base::StringAppendF(&result, "FlagManager values: \n"); |
| 78 | DUMP_FLAG(use_adpf_cpu_hint); |
| 79 | DUMP_FLAG(use_skia_tracing); |
| 80 | |
| 81 | #undef DUMP_FLAG |
| 82 | } |
| 83 | |
| 84 | std::optional<bool> FlagManager::getBoolProperty(const char* property) const { |
| 85 | return parseBool(base::GetProperty(property, "").c_str()); |
| 86 | } |
| 87 | |
| 88 | bool FlagManager::getServerConfigurableFlag(const char* experimentFlagName) const { |
| 89 | const auto value = server_configurable_flags::GetServerConfigurableFlag(kExperimentNamespace, |
| 90 | experimentFlagName, ""); |
| 91 | const auto res = parseBool(value.c_str()); |
| 92 | return res.has_value() && res.value(); |
| 93 | } |
| 94 | |
| 95 | #define FLAG_MANAGER_SERVER_FLAG(name, syspropOverride, serverFlagName) \ |
| 96 | bool FlagManager::name() const { \ |
| 97 | LOG_ALWAYS_FATAL_IF(!mBootCompleted, \ |
| 98 | "Can't read %s before boot completed as it is server writable", \ |
| 99 | __func__); \ |
| 100 | const auto debugOverride = getBoolProperty(syspropOverride); \ |
| 101 | if (debugOverride.has_value()) return debugOverride.value(); \ |
| 102 | return getServerConfigurableFlag(serverFlagName); \ |
rnlee | 81d3260 | 2021-07-27 13:24:07 -0700 | [diff] [blame] | 103 | } |
rnlee | 81d3260 | 2021-07-27 13:24:07 -0700 | [diff] [blame] | 104 | |
Ady Abraham | c589dc4 | 2023-10-26 16:20:53 -0700 | [diff] [blame^] | 105 | FLAG_MANAGER_SERVER_FLAG(test_flag, "", "") |
| 106 | FLAG_MANAGER_SERVER_FLAG(use_adpf_cpu_hint, "debug.sf.enable_adpf_cpu_hint", |
| 107 | "AdpfFeature__adpf_cpu_hint") |
| 108 | FLAG_MANAGER_SERVER_FLAG(use_skia_tracing, PROPERTY_SKIA_ATRACE_ENABLED, |
| 109 | "SkiaTracingFeature__use_skia_tracing") |
Matt Buckley | d23c796 | 2021-09-21 20:43:00 +0000 | [diff] [blame] | 110 | |
Ady Abraham | c589dc4 | 2023-10-26 16:20:53 -0700 | [diff] [blame^] | 111 | #undef FLAG_MANAGER_SERVER_FLAG |
Leon Scroggins III | a37ca99 | 2022-02-02 18:08:20 -0500 | [diff] [blame] | 112 | |
rnlee | 81d3260 | 2021-07-27 13:24:07 -0700 | [diff] [blame] | 113 | } // namespace android |