| 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> | 
|  | 22 | #include <android-base/stringprintf.h> | 
|  | 23 | #include <log/log.h> | 
|  | 24 | #include <server_configurable_flags/get_flags.h> | 
|  | 25 | #include <cinttypes> | 
|  | 26 |  | 
|  | 27 | namespace android { | 
|  | 28 | static constexpr const char* kExperimentNamespace = "surface_flinger_native_boot"; | 
|  | 29 | static constexpr const int64_t kDemoFlag = -1; | 
|  | 30 |  | 
|  | 31 | FlagManager::~FlagManager() = default; | 
|  | 32 |  | 
|  | 33 | void FlagManager::dump(std::string& result) const { | 
|  | 34 | base::StringAppendF(&result, "FlagManager values: \n"); | 
|  | 35 | base::StringAppendF(&result, "demo_flag: %" PRId64 "\n", demo_flag()); | 
| Matt Buckley | d23c796 | 2021-09-21 20:43:00 +0000 | [diff] [blame] | 36 | base::StringAppendF(&result, "use_adpf_cpu_hint: %s\n", use_adpf_cpu_hint() ? "true" : "false"); | 
| rnlee | 81d3260 | 2021-07-27 13:24:07 -0700 | [diff] [blame] | 37 | } | 
|  | 38 |  | 
|  | 39 | namespace { | 
|  | 40 | template <typename T> | 
|  | 41 | std::optional<T> doParse(const char* str); | 
|  | 42 |  | 
|  | 43 | template <> | 
|  | 44 | [[maybe_unused]] std::optional<int32_t> doParse(const char* str) { | 
|  | 45 | int32_t ret; | 
|  | 46 | return base::ParseInt(str, &ret) ? std::make_optional(ret) : std::nullopt; | 
|  | 47 | } | 
|  | 48 |  | 
|  | 49 | template <> | 
|  | 50 | [[maybe_unused]] std::optional<int64_t> doParse(const char* str) { | 
|  | 51 | int64_t ret; | 
|  | 52 | return base::ParseInt(str, &ret) ? std::make_optional(ret) : std::nullopt; | 
|  | 53 | } | 
|  | 54 |  | 
|  | 55 | template <> | 
|  | 56 | [[maybe_unused]] std::optional<bool> doParse(const char* str) { | 
|  | 57 | base::ParseBoolResult parseResult = base::ParseBool(str); | 
|  | 58 | switch (parseResult) { | 
|  | 59 | case base::ParseBoolResult::kTrue: | 
|  | 60 | return std::make_optional(true); | 
|  | 61 | case base::ParseBoolResult::kFalse: | 
|  | 62 | return std::make_optional(false); | 
|  | 63 | case base::ParseBoolResult::kError: | 
|  | 64 | return std::nullopt; | 
|  | 65 | } | 
|  | 66 | } | 
|  | 67 | } // namespace | 
|  | 68 |  | 
|  | 69 | std::string FlagManager::getServerConfigurableFlag(const std::string& experimentFlagName) const { | 
|  | 70 | return server_configurable_flags::GetServerConfigurableFlag(kExperimentNamespace, | 
|  | 71 | experimentFlagName, ""); | 
|  | 72 | } | 
|  | 73 |  | 
|  | 74 | template int32_t FlagManager::getValue<int32_t>(const std::string&, std::optional<int32_t>, | 
|  | 75 | int32_t) const; | 
|  | 76 | template int64_t FlagManager::getValue<int64_t>(const std::string&, std::optional<int64_t>, | 
|  | 77 | int64_t) const; | 
|  | 78 | template bool FlagManager::getValue<bool>(const std::string&, std::optional<bool>, bool) const; | 
|  | 79 | template <typename T> | 
|  | 80 | T FlagManager::getValue(const std::string& experimentFlagName, std::optional<T> systemPropertyOpt, | 
|  | 81 | T defaultValue) const { | 
|  | 82 | // System property takes precedence over the experiment config server value. | 
|  | 83 | if (systemPropertyOpt.has_value()) { | 
|  | 84 | return *systemPropertyOpt; | 
|  | 85 | } | 
|  | 86 | std::string str = getServerConfigurableFlag(experimentFlagName); | 
|  | 87 | return str.empty() ? defaultValue : doParse<T>(str.c_str()).value_or(defaultValue); | 
|  | 88 | } | 
|  | 89 |  | 
|  | 90 | int64_t FlagManager::demo_flag() const { | 
|  | 91 | std::optional<int64_t> sysPropVal = std::nullopt; | 
|  | 92 | return getValue("DemoFeature__demo_flag", sysPropVal, kDemoFlag); | 
|  | 93 | } | 
| Matt Buckley | d23c796 | 2021-09-21 20:43:00 +0000 | [diff] [blame] | 94 |  | 
|  | 95 | bool FlagManager::use_adpf_cpu_hint() const { | 
|  | 96 | std::optional<bool> sysPropVal = std::nullopt; | 
|  | 97 | return getValue("AdpfFeature__adpf_cpu_hint", sysPropVal, false); | 
|  | 98 | } | 
|  | 99 |  | 
| rnlee | 81d3260 | 2021-07-27 13:24:07 -0700 | [diff] [blame] | 100 | } // namespace android |