blob: 919ea4244ee5a69d616afee82dec8416d5711ccd [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";
rnlee81d32602021-07-27 13:24:07 -070031
Ady Abraham53b0c492023-10-23 11:47:14 -070032std::unique_ptr<FlagManager> FlagManager::mInstance;
33std::once_flag FlagManager::mOnce;
34
35FlagManager::FlagManager(ConstructorTag) {}
rnlee81d32602021-07-27 13:24:07 -070036FlagManager::~FlagManager() = default;
37
rnlee81d32602021-07-27 13:24:07 -070038namespace {
Ady Abrahamc589dc42023-10-26 16:20:53 -070039std::optional<bool> parseBool(const char* str) {
rnlee81d32602021-07-27 13:24:07 -070040 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 Abrahamc589dc42023-10-26 16:20:53 -070050
51void dumpFlag(std::string& result, const char* name, std::function<bool()> getter) {
52 base::StringAppendF(&result, "%s: %s\n", name, getter() ? "true" : "false");
53}
54
rnlee81d32602021-07-27 13:24:07 -070055} // namespace
56
Ady Abrahamc589dc42023-10-26 16:20:53 -070057const FlagManager& FlagManager::getInstance() {
58 return getMutableInstance();
rnlee81d32602021-07-27 13:24:07 -070059}
60
Ady Abrahamc589dc42023-10-26 16:20:53 -070061FlagManager& 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
70void FlagManager::markBootCompleted() {
71 mBootCompleted = true;
72}
73
74void 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
84std::optional<bool> FlagManager::getBoolProperty(const char* property) const {
85 return parseBool(base::GetProperty(property, "").c_str());
86}
87
88bool 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); \
rnlee81d32602021-07-27 13:24:07 -0700103 }
rnlee81d32602021-07-27 13:24:07 -0700104
Ady Abrahamc589dc42023-10-26 16:20:53 -0700105FLAG_MANAGER_SERVER_FLAG(test_flag, "", "")
106FLAG_MANAGER_SERVER_FLAG(use_adpf_cpu_hint, "debug.sf.enable_adpf_cpu_hint",
107 "AdpfFeature__adpf_cpu_hint")
108FLAG_MANAGER_SERVER_FLAG(use_skia_tracing, PROPERTY_SKIA_ATRACE_ENABLED,
109 "SkiaTracingFeature__use_skia_tracing")
Matt Buckleyd23c7962021-09-21 20:43:00 +0000110
Ady Abrahamc589dc42023-10-26 16:20:53 -0700111#undef FLAG_MANAGER_SERVER_FLAG
Leon Scroggins IIIa37ca992022-02-02 18:08:20 -0500112
rnlee81d32602021-07-27 13:24:07 -0700113} // namespace android