blob: 303714ce2f4481643d478ccb5c8689fa922f84a6 [file] [log] [blame]
rnlee81d32602021-07-27 13:24:07 -07001/*
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 IIIa37ca992022-02-02 18:08:20 -050022#include <android-base/properties.h>
rnlee81d32602021-07-27 13:24:07 -070023#include <android-base/stringprintf.h>
24#include <log/log.h>
Leon Scroggins IIIa37ca992022-02-02 18:08:20 -050025#include <renderengine/RenderEngine.h>
rnlee81d32602021-07-27 13:24:07 -070026#include <server_configurable_flags/get_flags.h>
27#include <cinttypes>
28
29namespace android {
30static constexpr const char* kExperimentNamespace = "surface_flinger_native_boot";
31static constexpr const int64_t kDemoFlag = -1;
32
Ady Abraham53b0c492023-10-23 11:47:14 -070033std::unique_ptr<FlagManager> FlagManager::mInstance;
34std::once_flag FlagManager::mOnce;
35
36FlagManager::FlagManager(ConstructorTag) {}
rnlee81d32602021-07-27 13:24:07 -070037FlagManager::~FlagManager() = default;
38
Ady Abraham53b0c492023-10-23 11:47:14 -070039FlagManager& FlagManager::getInstance() {
40 std::call_once(mOnce, [&] {
41 LOG_ALWAYS_FATAL_IF(mInstance, "Instance already created");
42 mInstance = std::make_unique<FlagManager>(ConstructorTag{});
43 });
44
45 return *mInstance;
46}
47
rnlee81d32602021-07-27 13:24:07 -070048void FlagManager::dump(std::string& result) const {
49 base::StringAppendF(&result, "FlagManager values: \n");
50 base::StringAppendF(&result, "demo_flag: %" PRId64 "\n", demo_flag());
Matt Buckleyd23c7962021-09-21 20:43:00 +000051 base::StringAppendF(&result, "use_adpf_cpu_hint: %s\n", use_adpf_cpu_hint() ? "true" : "false");
Leon Scroggins IIIa37ca992022-02-02 18:08:20 -050052 base::StringAppendF(&result, "use_skia_tracing: %s\n", use_skia_tracing() ? "true" : "false");
rnlee81d32602021-07-27 13:24:07 -070053}
54
55namespace {
56template <typename T>
57std::optional<T> doParse(const char* str);
58
59template <>
60[[maybe_unused]] std::optional<int32_t> doParse(const char* str) {
61 int32_t ret;
62 return base::ParseInt(str, &ret) ? std::make_optional(ret) : std::nullopt;
63}
64
65template <>
66[[maybe_unused]] std::optional<int64_t> doParse(const char* str) {
67 int64_t ret;
68 return base::ParseInt(str, &ret) ? std::make_optional(ret) : std::nullopt;
69}
70
71template <>
72[[maybe_unused]] std::optional<bool> doParse(const char* str) {
73 base::ParseBoolResult parseResult = base::ParseBool(str);
74 switch (parseResult) {
75 case base::ParseBoolResult::kTrue:
76 return std::make_optional(true);
77 case base::ParseBoolResult::kFalse:
78 return std::make_optional(false);
79 case base::ParseBoolResult::kError:
80 return std::nullopt;
81 }
82}
83} // namespace
84
85std::string FlagManager::getServerConfigurableFlag(const std::string& experimentFlagName) const {
86 return server_configurable_flags::GetServerConfigurableFlag(kExperimentNamespace,
87 experimentFlagName, "");
88}
89
90template int32_t FlagManager::getValue<int32_t>(const std::string&, std::optional<int32_t>,
91 int32_t) const;
92template int64_t FlagManager::getValue<int64_t>(const std::string&, std::optional<int64_t>,
93 int64_t) const;
94template bool FlagManager::getValue<bool>(const std::string&, std::optional<bool>, bool) const;
95template <typename T>
96T FlagManager::getValue(const std::string& experimentFlagName, std::optional<T> systemPropertyOpt,
97 T defaultValue) const {
98 // System property takes precedence over the experiment config server value.
99 if (systemPropertyOpt.has_value()) {
100 return *systemPropertyOpt;
101 }
102 std::string str = getServerConfigurableFlag(experimentFlagName);
103 return str.empty() ? defaultValue : doParse<T>(str.c_str()).value_or(defaultValue);
104}
105
106int64_t FlagManager::demo_flag() const {
107 std::optional<int64_t> sysPropVal = std::nullopt;
108 return getValue("DemoFeature__demo_flag", sysPropVal, kDemoFlag);
109}
Matt Buckleyd23c7962021-09-21 20:43:00 +0000110
111bool FlagManager::use_adpf_cpu_hint() const {
Xiang Wang65a2e6f2022-04-18 21:19:17 +0000112 std::optional<bool> sysPropVal =
113 doParse<bool>(base::GetProperty("debug.sf.enable_adpf_cpu_hint", "").c_str());
Matt Buckleyd23c7962021-09-21 20:43:00 +0000114 return getValue("AdpfFeature__adpf_cpu_hint", sysPropVal, false);
115}
116
Leon Scroggins IIIa37ca992022-02-02 18:08:20 -0500117bool FlagManager::use_skia_tracing() const {
Leon Scroggins IIIa37ca992022-02-02 18:08:20 -0500118 std::optional<bool> sysPropVal =
119 doParse<bool>(base::GetProperty(PROPERTY_SKIA_ATRACE_ENABLED, "").c_str());
120 return getValue("SkiaTracingFeature__use_skia_tracing", sysPropVal, false);
121}
122
rnlee81d32602021-07-27 13:24:07 -0700123} // namespace android