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"; |
| 31 | static constexpr const int64_t kDemoFlag = -1; |
| 32 | |
| 33 | FlagManager::~FlagManager() = default; |
| 34 | |
| 35 | void FlagManager::dump(std::string& result) const { |
| 36 | base::StringAppendF(&result, "FlagManager values: \n"); |
| 37 | base::StringAppendF(&result, "demo_flag: %" PRId64 "\n", demo_flag()); |
Matt Buckley | d23c796 | 2021-09-21 20:43:00 +0000 | [diff] [blame] | 38 | base::StringAppendF(&result, "use_adpf_cpu_hint: %s\n", use_adpf_cpu_hint() ? "true" : "false"); |
Leon Scroggins III | a37ca99 | 2022-02-02 18:08:20 -0500 | [diff] [blame] | 39 | base::StringAppendF(&result, "use_skia_tracing: %s\n", use_skia_tracing() ? "true" : "false"); |
rnlee | 81d3260 | 2021-07-27 13:24:07 -0700 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | namespace { |
| 43 | template <typename T> |
| 44 | std::optional<T> doParse(const char* str); |
| 45 | |
| 46 | template <> |
| 47 | [[maybe_unused]] std::optional<int32_t> doParse(const char* str) { |
| 48 | int32_t ret; |
| 49 | return base::ParseInt(str, &ret) ? std::make_optional(ret) : std::nullopt; |
| 50 | } |
| 51 | |
| 52 | template <> |
| 53 | [[maybe_unused]] std::optional<int64_t> doParse(const char* str) { |
| 54 | int64_t ret; |
| 55 | return base::ParseInt(str, &ret) ? std::make_optional(ret) : std::nullopt; |
| 56 | } |
| 57 | |
| 58 | template <> |
| 59 | [[maybe_unused]] std::optional<bool> doParse(const char* str) { |
| 60 | base::ParseBoolResult parseResult = base::ParseBool(str); |
| 61 | switch (parseResult) { |
| 62 | case base::ParseBoolResult::kTrue: |
| 63 | return std::make_optional(true); |
| 64 | case base::ParseBoolResult::kFalse: |
| 65 | return std::make_optional(false); |
| 66 | case base::ParseBoolResult::kError: |
| 67 | return std::nullopt; |
| 68 | } |
| 69 | } |
| 70 | } // namespace |
| 71 | |
| 72 | std::string FlagManager::getServerConfigurableFlag(const std::string& experimentFlagName) const { |
| 73 | return server_configurable_flags::GetServerConfigurableFlag(kExperimentNamespace, |
| 74 | experimentFlagName, ""); |
| 75 | } |
| 76 | |
| 77 | template int32_t FlagManager::getValue<int32_t>(const std::string&, std::optional<int32_t>, |
| 78 | int32_t) const; |
| 79 | template int64_t FlagManager::getValue<int64_t>(const std::string&, std::optional<int64_t>, |
| 80 | int64_t) const; |
| 81 | template bool FlagManager::getValue<bool>(const std::string&, std::optional<bool>, bool) const; |
| 82 | template <typename T> |
| 83 | T FlagManager::getValue(const std::string& experimentFlagName, std::optional<T> systemPropertyOpt, |
| 84 | T defaultValue) const { |
| 85 | // System property takes precedence over the experiment config server value. |
| 86 | if (systemPropertyOpt.has_value()) { |
| 87 | return *systemPropertyOpt; |
| 88 | } |
| 89 | std::string str = getServerConfigurableFlag(experimentFlagName); |
| 90 | return str.empty() ? defaultValue : doParse<T>(str.c_str()).value_or(defaultValue); |
| 91 | } |
| 92 | |
| 93 | int64_t FlagManager::demo_flag() const { |
| 94 | std::optional<int64_t> sysPropVal = std::nullopt; |
| 95 | return getValue("DemoFeature__demo_flag", sysPropVal, kDemoFlag); |
| 96 | } |
Matt Buckley | d23c796 | 2021-09-21 20:43:00 +0000 | [diff] [blame] | 97 | |
| 98 | bool FlagManager::use_adpf_cpu_hint() const { |
Xiang Wang | 65a2e6f | 2022-04-18 21:19:17 +0000 | [diff] [blame^] | 99 | std::optional<bool> sysPropVal = |
| 100 | doParse<bool>(base::GetProperty("debug.sf.enable_adpf_cpu_hint", "").c_str()); |
Matt Buckley | d23c796 | 2021-09-21 20:43:00 +0000 | [diff] [blame] | 101 | return getValue("AdpfFeature__adpf_cpu_hint", sysPropVal, false); |
| 102 | } |
| 103 | |
Leon Scroggins III | a37ca99 | 2022-02-02 18:08:20 -0500 | [diff] [blame] | 104 | bool FlagManager::use_skia_tracing() const { |
Leon Scroggins III | a37ca99 | 2022-02-02 18:08:20 -0500 | [diff] [blame] | 105 | std::optional<bool> sysPropVal = |
| 106 | doParse<bool>(base::GetProperty(PROPERTY_SKIA_ATRACE_ENABLED, "").c_str()); |
| 107 | return getValue("SkiaTracingFeature__use_skia_tracing", sysPropVal, false); |
| 108 | } |
| 109 | |
rnlee | 81d3260 | 2021-07-27 13:24:07 -0700 | [diff] [blame] | 110 | } // namespace android |