Commit ffdd28dc authored by Jia's avatar Jia Committed by Commit Bot

[On-device adaptive brightness] Add enabled field to model config

Bug: 881215
Change-Id: I1857c6393a7faebfcc87610af3f455fc0f07bc98
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1627269Reviewed-by: default avatarAndrew Moylan <amoylan@chromium.org>
Commit-Queue: Jia Meng <jiameng@chromium.org>
Cr-Commit-Position: refs/heads/master@{#662965}
parent 94dbd9af
......@@ -12,14 +12,7 @@ namespace auto_screen_brightness {
ModelConfig::ModelConfig() = default;
ModelConfig::ModelConfig(const ModelConfig& config) {
auto_brightness_als_horizon_seconds =
config.auto_brightness_als_horizon_seconds;
log_lux = config.log_lux;
brightness = config.brightness;
metrics_key = config.metrics_key;
model_als_horizon_seconds = config.model_als_horizon_seconds;
}
ModelConfig::ModelConfig(const ModelConfig& config) = default;
ModelConfig::~ModelConfig() = default;
......@@ -29,6 +22,9 @@ bool ModelConfig::operator==(const ModelConfig& config) const {
config.auto_brightness_als_horizon_seconds) >= kTol)
return false;
if (enabled != config.enabled)
return false;
if (log_lux.size() != config.log_lux.size())
return false;
......
......@@ -16,6 +16,7 @@ namespace auto_screen_brightness {
// Model customization config.
struct ModelConfig {
double auto_brightness_als_horizon_seconds = -1.0;
bool enabled = false;
std::vector<double> log_lux;
std::vector<double> brightness;
std::string metrics_key;
......
......@@ -58,12 +58,14 @@ struct GlobalCurveFromJson {
struct ModelConfigFromJson {
double auto_brightness_als_horizon_seconds;
bool enabled;
GlobalCurveFromJson global_curve;
std::string metrics_key;
double model_als_horizon_seconds;
ModelConfigFromJson()
: auto_brightness_als_horizon_seconds(-1.0),
enabled(false),
model_als_horizon_seconds(-1.0) {}
static void RegisterJSONConverter(
......@@ -71,6 +73,7 @@ struct ModelConfigFromJson {
converter->RegisterDoubleField(
"auto_brightness_als_horizon_seconds",
&ModelConfigFromJson::auto_brightness_als_horizon_seconds);
converter->RegisterBoolField("enabled", &ModelConfigFromJson::enabled);
converter->RegisterNestedField("global_curve",
&ModelConfigFromJson::global_curve);
converter->RegisterStringField("metrics_key",
......@@ -158,6 +161,9 @@ void ModelConfigLoaderImpl::InitFromParams() {
"auto_brightness_als_horizon_seconds",
model_config_.auto_brightness_als_horizon_seconds);
model_config_.enabled = GetFieldTrialParamByFeatureAsBool(
features::kAutoScreenBrightness, "enabled", model_config_.enabled);
model_config_.model_als_horizon_seconds = GetFieldTrialParamByFeatureAsInt(
features::kAutoScreenBrightness, "model_als_horizon_seconds",
model_config_.model_als_horizon_seconds);
......@@ -228,6 +234,8 @@ void ModelConfigLoaderImpl::OnModelParamsLoadedFromDisk(
model_config_.auto_brightness_als_horizon_seconds =
loaded_model_configs.auto_brightness_als_horizon_seconds;
model_config_.enabled = loaded_model_configs.enabled;
std::vector<double> log_lux;
for (const auto& log_lux_val : loaded_model_configs.global_curve.log_lux) {
DCHECK(log_lux_val);
......
......@@ -109,24 +109,26 @@ class ModelConfigLoaderImplTest : public testing::Test {
};
TEST_F(ModelConfigLoaderImplTest, ValidModelParamsLoaded) {
const std::string model_params =
"{\n"
" \"auto_brightness_als_horizon_seconds\": 2, \n"
" \"global_curve\": { \n"
" \"log_lux\": [ \n"
" 1.0, \n"
" 2.0, \n"
" 3.0 \n"
" ], \n"
" \"brightness\": [ \n"
" 10.0, \n"
" 20.0, \n"
" 30.0 \n"
" ] \n"
" }, \n"
" \"metrics_key\": \"abc\", \n"
" \"model_als_horizon_seconds\": 5 \n"
"}\n";
const std::string model_params = R"(
{
"auto_brightness_als_horizon_seconds": 2,
"enabled": true,
"global_curve": {
"log_lux": [
1.0,
2.0,
3.0
],
"brightness": [
10.0,
20.0,
30.0
]
},
"metrics_key": "abc",
"model_als_horizon_seconds": 5
}
)";
Init(model_params);
EXPECT_TRUE(test_observer_->model_config_loader_initialized());
......@@ -136,6 +138,45 @@ TEST_F(ModelConfigLoaderImplTest, ValidModelParamsLoaded) {
ModelConfig expected_model_config;
expected_model_config.auto_brightness_als_horizon_seconds = 2.0;
expected_model_config.enabled = true;
expected_model_config.log_lux = expected_log_lux;
expected_model_config.brightness = expected_brightness;
expected_model_config.metrics_key = "abc";
expected_model_config.model_als_horizon_seconds = 5;
EXPECT_TRUE(test_observer_->model_config());
EXPECT_EQ(*test_observer_->model_config(), expected_model_config);
}
TEST_F(ModelConfigLoaderImplTest, MissingEnabledMeansFalse) {
const std::string model_params = R"(
{
"auto_brightness_als_horizon_seconds": 2,
"global_curve": {
"log_lux": [
1.0,
2.0,
3.0
],
"brightness": [
10.0,
20.0,
30.0
]
},
"metrics_key": "abc",
"model_als_horizon_seconds": 5
}
)";
Init(model_params);
EXPECT_TRUE(test_observer_->model_config_loader_initialized());
std::vector<double> expected_log_lux = {1.0, 2.0, 3.0};
std::vector<double> expected_brightness = {10.0, 20.0, 30.0};
ModelConfig expected_model_config;
expected_model_config.auto_brightness_als_horizon_seconds = 2.0;
expected_model_config.enabled = false;
expected_model_config.log_lux = expected_log_lux;
expected_model_config.brightness = expected_brightness;
expected_model_config.metrics_key = "abc";
......@@ -145,29 +186,32 @@ TEST_F(ModelConfigLoaderImplTest, ValidModelParamsLoaded) {
}
TEST_F(ModelConfigLoaderImplTest, ValidModelParamsLoadedThenOverriden) {
const std::string model_params =
"{\n"
" \"auto_brightness_als_horizon_seconds\": 2, \n"
" \"global_curve\": { \n"
" \"log_lux\": [ \n"
" 1.0, \n"
" 2.0, \n"
" 3.0 \n"
" ], \n"
" \"brightness\": [ \n"
" 10.0, \n"
" 20.0, \n"
" 30.0 \n"
" ] \n"
" }, \n"
" \"metrics_key\": \"abc\", \n"
" \"model_als_horizon_seconds\": 5 \n"
"}\n";
const std::string model_params = R"(
{
"auto_brightness_als_horizon_seconds": 2,
"enabled": true,
"global_curve": {
"log_lux": [
1.0,
2.0,
3.0
],
"brightness": [
10.0,
20.0,
30.0
]
},
"metrics_key": "abc",
"model_als_horizon_seconds": 5
}
)";
const std::string global_curve_spec("2:20,4:40,6:60");
const std::map<std::string, std::string> experiment_params = {
{"auto_brightness_als_horizon_seconds", "10"},
{"enabled", "false"},
{"model_als_horizon_seconds", "20"},
{"global_curve", global_curve_spec},
};
......@@ -180,6 +224,7 @@ TEST_F(ModelConfigLoaderImplTest, ValidModelParamsLoadedThenOverriden) {
ModelConfig expected_model_config;
expected_model_config.auto_brightness_als_horizon_seconds = 10.0;
expected_model_config.enabled = false;
expected_model_config.log_lux = expected_log_lux;
expected_model_config.brightness = expected_brightness;
expected_model_config.metrics_key = "abc";
......@@ -190,23 +235,24 @@ TEST_F(ModelConfigLoaderImplTest, ValidModelParamsLoadedThenOverriden) {
TEST_F(ModelConfigLoaderImplTest, InvalidModelParamsLoaded) {
// "auto_brightness_als_horizon_seconds" is missing.
const std::string model_params =
"{\n"
" \"global_curve\": { \n"
" \"log_lux\": [ \n"
" 1.0, \n"
" 2.0, \n"
" 3.0 \n"
" ], \n"
" \"brightness\": [ \n"
" 10.0, \n"
" 20.0, \n"
" 30.0 \n"
" ] \n"
" }, \n"
" \"metrics_key\": \"abc\", \n"
" \"model_als_horizon_seconds\": 5 \n"
"}\n";
const std::string model_params = R"(
{
"global_curve": {
"log_lux": [
1.0,
2.0,
3.0
],
"brightness": [
10.0,
20.0,
30.0
]
},
"metrics_key": "abc",
"model_als_horizon_seconds": 5
}
)";
Init(model_params);
EXPECT_TRUE(test_observer_->model_config_loader_initialized());
......@@ -216,23 +262,24 @@ TEST_F(ModelConfigLoaderImplTest, InvalidModelParamsLoaded) {
TEST_F(ModelConfigLoaderImplTest, InvalidModelParamsLoadedThenOverriden) {
// Same as InvalidModelParamsLoaded, but missing
// "auto_brightness_als_horizon_seconds" is specified in the experiment flags.
const std::string model_params =
"{\n"
" \"global_curve\": { \n"
" \"log_lux\": [ \n"
" 1.0, \n"
" 2.0, \n"
" 3.0 \n"
" ], \n"
" \"brightness\": [ \n"
" 10.0, \n"
" 20.0, \n"
" 30.0 \n"
" ] \n"
" }, \n"
" \"metrics_key\": \"abc\", \n"
" \"model_als_horizon_seconds\": 5 \n"
"}\n";
const std::string model_params = R"(
{
"global_curve": {
"log_lux": [
1.0,
2.0,
3.0
],
"brightness": [
10.0,
20.0,
30.0
]
},
"metrics_key": "abc",
"model_als_horizon_seconds": 5
}
)";
const std::map<std::string, std::string> experiment_params = {
{"auto_brightness_als_horizon_seconds", "10"},
......@@ -247,6 +294,7 @@ TEST_F(ModelConfigLoaderImplTest, InvalidModelParamsLoadedThenOverriden) {
ModelConfig expected_model_config;
expected_model_config.auto_brightness_als_horizon_seconds = 10.0;
expected_model_config.enabled = false;
expected_model_config.log_lux = expected_log_lux;
expected_model_config.brightness = expected_brightness;
expected_model_config.metrics_key = "abc";
......@@ -269,23 +317,24 @@ TEST_F(ModelConfigLoaderImplTest, MissingModelParams) {
}
TEST_F(ModelConfigLoaderImplTest, InvalidJsonFormat) {
const std::string model_params =
"{\n"
" \"global_curve\": { \n"
" \"log_lux\": [ \n"
" 1.0, \n"
" 2.0, \n"
" 3.0 \n"
" ], \n"
" \"brightness\": [ \n"
" 10.0, \n"
" 20.0, \n"
" 30.0 \n"
" ] \n"
" }, \n"
" \"metrics_key\": 10, \n"
" \"model_als_horizon_seconds\": 5 \n"
"}\n";
const std::string model_params = R"(
{
"global_curve": {
"log_lux": [
1.0,
2.0,
3.0
],
"brightness": [
10.0,
20.0,
30.0
]
},
"metrics_key": 10,
"model_als_horizon_seconds": 5
}
)";
const std::map<std::string, std::string> experiment_params = {
{"auto_brightness_als_horizon_seconds", "10"},
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment