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

[On-device adaptive brightness] Add path for unibuild

In this cl, we add model params path for unibuild models.

Bug: 881215,1062070
Change-Id: I0dafba8a870741e87e2ac1bb99e4721b6beb6f9a
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2160672Reviewed-by: default avatarThanh Nguyen <thanhdng@chromium.org>
Commit-Queue: Jia Meng <jiameng@chromium.org>
Cr-Commit-Position: refs/heads/master@{#765385}
parent 036d3b68
...@@ -29,6 +29,16 @@ namespace auto_screen_brightness { ...@@ -29,6 +29,16 @@ namespace auto_screen_brightness {
namespace { namespace {
// Model params are usually written to this path, unless it is a unibuild.
constexpr char kDefaultModelParamsPath[] =
"/usr/share/chromeos-assets/autobrightness/model_params.json";
// Model params for unibuild devices will be written to device-specific
// directories. We need to read the relevant directory from the following system
// file path.
constexpr char kSystemPath[] =
"/run/chromeos-config/v1/power/autobrightness/config-file/system-path";
// Reads string content from |model_params_path| if it exists. // Reads string content from |model_params_path| if it exists.
// This should run in another thread to be non-blocking to the main thread (if // This should run in another thread to be non-blocking to the main thread (if
// |is_testing| is false). // |is_testing| is false).
...@@ -36,13 +46,40 @@ std::string LoadModelParamsFromDisk(const base::FilePath& model_params_path, ...@@ -36,13 +46,40 @@ std::string LoadModelParamsFromDisk(const base::FilePath& model_params_path,
bool is_testing) { bool is_testing) {
DCHECK(is_testing || DCHECK(is_testing ||
!content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); !content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
if (!base::PathExists(model_params_path))
base::FilePath used_model_params_path = model_params_path;
if (used_model_params_path.empty()) {
// Only override supplied path if it is empty.
const base::FilePath default_path = base::FilePath(kDefaultModelParamsPath);
if (base::PathExists(default_path)) {
// If default path exists, use it.
used_model_params_path = default_path;
} else {
// If default path doesn't exist, try to get the path from the system
// file.
const base::FilePath system_path = base::FilePath(kSystemPath);
if (base::PathExists(system_path)) {
std::string dest_path_value;
if (base::ReadFileToString(system_path, &dest_path_value)) {
used_model_params_path = base::FilePath(dest_path_value);
}
}
}
}
if (used_model_params_path.empty() ||
!base::PathExists(used_model_params_path)) {
VLOG(1) << "ABLoader model params path does not exist";
return std::string(); return std::string();
}
std::string content; std::string content;
if (!base::ReadFileToString(model_params_path, &content)) { if (!base::ReadFileToString(used_model_params_path, &content)) {
VLOG(1) << "ABLoader cannot load params from " << used_model_params_path;
return std::string(); return std::string();
} }
VLOG(1) << "ABLoader successfully loaded params from "
<< used_model_params_path;
return content; return content;
} }
...@@ -91,8 +128,7 @@ struct ModelConfigFromJson { ...@@ -91,8 +128,7 @@ struct ModelConfigFromJson {
ModelConfigLoaderImpl::ModelConfigLoaderImpl() ModelConfigLoaderImpl::ModelConfigLoaderImpl()
: ModelConfigLoaderImpl( : ModelConfigLoaderImpl(
base::FilePath( base::FilePath(),
"/usr/share/chromeos-assets/autobrightness/model_params.json"),
base::ThreadPool::CreateSequencedTaskRunner( base::ThreadPool::CreateSequencedTaskRunner(
{base::TaskPriority::USER_VISIBLE, base::MayBlock(), {base::TaskPriority::USER_VISIBLE, base::MayBlock(),
base::TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN}), base::TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN}),
...@@ -131,18 +167,16 @@ ModelConfigLoaderImpl::ModelConfigLoaderImpl( ...@@ -131,18 +167,16 @@ ModelConfigLoaderImpl::ModelConfigLoaderImpl(
const base::FilePath& model_params_path, const base::FilePath& model_params_path,
scoped_refptr<base::SequencedTaskRunner> blocking_task_runner, scoped_refptr<base::SequencedTaskRunner> blocking_task_runner,
bool is_testing) bool is_testing)
: model_params_path_(model_params_path), : blocking_task_runner_(blocking_task_runner), is_testing_(is_testing) {
blocking_task_runner_(blocking_task_runner), Init(model_params_path);
is_testing_(is_testing) {
Init();
} }
void ModelConfigLoaderImpl::Init() { void ModelConfigLoaderImpl::Init(const base::FilePath& model_params_path) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
base::PostTaskAndReplyWithResult( base::PostTaskAndReplyWithResult(
blocking_task_runner_.get(), FROM_HERE, blocking_task_runner_.get(), FROM_HERE,
base::BindOnce(&LoadModelParamsFromDisk, model_params_path_, is_testing_), base::BindOnce(&LoadModelParamsFromDisk, model_params_path, is_testing_),
base::BindOnce(&ModelConfigLoaderImpl::OnModelParamsLoadedFromDisk, base::BindOnce(&ModelConfigLoaderImpl::OnModelParamsLoadedFromDisk,
weak_ptr_factory_.GetWeakPtr())); weak_ptr_factory_.GetWeakPtr()));
} }
......
...@@ -47,7 +47,9 @@ class ModelConfigLoaderImpl : public ModelConfigLoader { ...@@ -47,7 +47,9 @@ class ModelConfigLoaderImpl : public ModelConfigLoader {
bool is_testing); bool is_testing);
// Reads params from the disk and then calls InitFromParams. // Reads params from the disk and then calls InitFromParams.
void Init(); // Uses |model_params_path| if it's not empty. Otherwise uses predefined
// paths.
void Init(const base::FilePath& model_params_path);
// Overrides loaded model configs from experiment flags. It will call // Overrides loaded model configs from experiment flags. It will call
// OnInitializationComplete after completion. // OnInitializationComplete after completion.
...@@ -59,7 +61,6 @@ class ModelConfigLoaderImpl : public ModelConfigLoader { ...@@ -59,7 +61,6 @@ class ModelConfigLoaderImpl : public ModelConfigLoader {
// attempted to read from the disk and override with experiment flags. // attempted to read from the disk and override with experiment flags.
void OnInitializationComplete(); void OnInitializationComplete();
base::FilePath model_params_path_;
scoped_refptr<base::SequencedTaskRunner> blocking_task_runner_; scoped_refptr<base::SequencedTaskRunner> blocking_task_runner_;
bool is_testing_ = false; bool is_testing_ = false;
......
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