/* * Copyright (C) 2021 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "FlagManager.h" #include #include #include #include #include #include #include #include #include namespace android { static constexpr const char* kExperimentNamespace = "surface_flinger_native_boot"; static constexpr const int64_t kDemoFlag = -1; FlagManager::~FlagManager() = default; void FlagManager::dump(std::string& result) const { base::StringAppendF(&result, "FlagManager values: \n"); base::StringAppendF(&result, "demo_flag: %" PRId64 "\n", demo_flag()); base::StringAppendF(&result, "use_adpf_cpu_hint: %s\n", use_adpf_cpu_hint() ? "true" : "false"); base::StringAppendF(&result, "use_skia_tracing: %s\n", use_skia_tracing() ? "true" : "false"); } namespace { template std::optional doParse(const char* str); template <> [[maybe_unused]] std::optional doParse(const char* str) { int32_t ret; return base::ParseInt(str, &ret) ? std::make_optional(ret) : std::nullopt; } template <> [[maybe_unused]] std::optional doParse(const char* str) { int64_t ret; return base::ParseInt(str, &ret) ? std::make_optional(ret) : std::nullopt; } template <> [[maybe_unused]] std::optional doParse(const char* str) { base::ParseBoolResult parseResult = base::ParseBool(str); switch (parseResult) { case base::ParseBoolResult::kTrue: return std::make_optional(true); case base::ParseBoolResult::kFalse: return std::make_optional(false); case base::ParseBoolResult::kError: return std::nullopt; } } } // namespace std::string FlagManager::getServerConfigurableFlag(const std::string& experimentFlagName) const { return server_configurable_flags::GetServerConfigurableFlag(kExperimentNamespace, experimentFlagName, ""); } template int32_t FlagManager::getValue(const std::string&, std::optional, int32_t) const; template int64_t FlagManager::getValue(const std::string&, std::optional, int64_t) const; template bool FlagManager::getValue(const std::string&, std::optional, bool) const; template T FlagManager::getValue(const std::string& experimentFlagName, std::optional systemPropertyOpt, T defaultValue) const { // System property takes precedence over the experiment config server value. if (systemPropertyOpt.has_value()) { return *systemPropertyOpt; } std::string str = getServerConfigurableFlag(experimentFlagName); return str.empty() ? defaultValue : doParse(str.c_str()).value_or(defaultValue); } int64_t FlagManager::demo_flag() const { std::optional sysPropVal = std::nullopt; return getValue("DemoFeature__demo_flag", sysPropVal, kDemoFlag); } bool FlagManager::use_adpf_cpu_hint() const { std::optional sysPropVal = doParse(base::GetProperty("debug.sf.enable_adpf_cpu_hint", "").c_str()); return getValue("AdpfFeature__adpf_cpu_hint", sysPropVal, false); } bool FlagManager::use_skia_tracing() const { std::optional sysPropVal = doParse(base::GetProperty(PROPERTY_SKIA_ATRACE_ENABLED, "").c_str()); return getValue("SkiaTracingFeature__use_skia_tracing", sysPropVal, false); } } // namespace android