1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
ash / wallpaper / wallpaper_utils / wallpaper_color_calculator.h [blame]
// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef ASH_WALLPAPER_WALLPAPER_UTILS_WALLPAPER_COLOR_CALCULATOR_H_
#define ASH_WALLPAPER_WALLPAPER_UTILS_WALLPAPER_COLOR_CALCULATOR_H_
#include <optional>
#include "ash/ash_export.h"
#include "ash/wallpaper/wallpaper_utils/wallpaper_calculated_colors.h"
#include "base/functional/callback_forward.h"
#include "base/memory/weak_ptr.h"
#include "base/time/time.h"
#include "ui/gfx/image/image_skia.h"
namespace base {
class TaskRunner;
}
namespace ash {
// Calculates colors based on a wallpaper image.
class ASH_EXPORT WallpaperColorCalculator {
public:
// Passes `image` as a param to the color calculation. Uses the default color
// profiles provided from GetProminentColorProfiles().
explicit WallpaperColorCalculator(const gfx::ImageSkia& image);
WallpaperColorCalculator(const WallpaperColorCalculator&) = delete;
WallpaperColorCalculator& operator=(const WallpaperColorCalculator&) = delete;
~WallpaperColorCalculator();
using WallpaperColorCallback =
base::OnceCallback<void(const WallpaperCalculatedColors&)>;
// Initiates the calculation and returns false if the calculation fails to be
// initiated. Observers may be notified synchronously or asynchronously.
// Callers should be aware that this will make |image_| read-only.
[[nodiscard]] bool StartCalculation(WallpaperColorCallback callback);
std::optional<const WallpaperCalculatedColors> get_calculated_colors() {
return calculated_colors_;
}
void set_calculated_colors_for_test(
const WallpaperCalculatedColors& calculated_colors) {
calculated_colors_ = calculated_colors;
}
// Explicitly sets the |task_runner_| for testing.
void SetTaskRunnerForTest(scoped_refptr<base::TaskRunner> task_runner);
private:
// Handles asynchronous calculation results. |async_start_time| is used to
// record duration metrics.
void OnAsyncCalculationComplete(
base::TimeTicks async_start_time,
WallpaperColorCallback callback,
const WallpaperCalculatedColors& calculated_colors);
// The result of the color calculation.
std::optional<WallpaperCalculatedColors> calculated_colors_;
// The image to calculate colors from.
gfx::ImageSkia image_;
// The task runner to run the calculation on.
scoped_refptr<base::TaskRunner> task_runner_;
base::WeakPtrFactory<WallpaperColorCalculator> weak_ptr_factory_{this};
};
} // namespace ash
#endif // ASH_WALLPAPER_WALLPAPER_UTILS_WALLPAPER_COLOR_CALCULATOR_H_