| OLD | NEW |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef COMPONENTS_WALLPAPER_WALLPAPER_COLOR_CALCULATOR_H_ | 5 #ifndef COMPONENTS_WALLPAPER_WALLPAPER_COLOR_CALCULATOR_H_ |
| 6 #define COMPONENTS_WALLPAPER_WALLPAPER_COLOR_CALCULATOR_H_ | 6 #define COMPONENTS_WALLPAPER_WALLPAPER_COLOR_CALCULATOR_H_ |
| 7 | 7 |
| 8 #include "base/macros.h" | 8 #include "base/macros.h" |
| 9 #include "base/memory/ref_counted.h" | 9 #include "base/memory/ref_counted.h" |
| 10 #include "base/memory/weak_ptr.h" | 10 #include "base/memory/weak_ptr.h" |
| 11 #include "base/observer_list.h" | 11 #include "base/observer_list.h" |
| 12 #include "base/time/time.h" | 12 #include "base/time/time.h" |
| 13 #include "components/wallpaper/wallpaper_export.h" | 13 #include "components/wallpaper/wallpaper_export.h" |
| 14 #include "third_party/skia/include/core/SkColor.h" | 14 #include "third_party/skia/include/core/SkColor.h" |
| 15 #include "ui/gfx/color_analysis.h" | |
| 16 #include "ui/gfx/image/image_skia.h" | 15 #include "ui/gfx/image/image_skia.h" |
| 17 | 16 |
| 18 namespace base { | 17 namespace base { |
| 19 class TaskRunner; | 18 class TaskRunner; |
| 20 } | 19 } |
| 21 | 20 |
| 21 namespace color_utils { |
| 22 struct ColorProfile; |
| 23 } |
| 24 |
| 22 namespace wallpaper { | 25 namespace wallpaper { |
| 23 class WallpaperColorCalculatorObserver; | 26 class WallpaperColorCalculatorObserver; |
| 24 | 27 |
| 25 // Calculates colors based on a wallpaper image. | 28 // Calculates colors based on a wallpaper image. |
| 26 class WALLPAPER_EXPORT WallpaperColorCalculator { | 29 class WALLPAPER_EXPORT WallpaperColorCalculator { |
| 27 public: | 30 public: |
| 28 // |image|, |luma| and |saturation| are the input parameters to the color | 31 // |image|, |color_profiles| are the input parameters to the color calculation |
| 29 // calculation that is executed on the |task_runner|. | 32 // that is executed on the |task_runner|. |
| 30 WallpaperColorCalculator(const gfx::ImageSkia& image, | 33 WallpaperColorCalculator( |
| 31 color_utils::LumaRange luma, | 34 const gfx::ImageSkia& image, |
| 32 color_utils::SaturationRange saturation, | 35 const std::vector<color_utils::ColorProfile>& color_profiles, |
| 33 scoped_refptr<base::TaskRunner> task_runner); | 36 scoped_refptr<base::TaskRunner> task_runner); |
| 34 ~WallpaperColorCalculator(); | 37 ~WallpaperColorCalculator(); |
| 35 | 38 |
| 36 void AddObserver(WallpaperColorCalculatorObserver* observer); | 39 void AddObserver(WallpaperColorCalculatorObserver* observer); |
| 37 | 40 |
| 38 void RemoveObserver(WallpaperColorCalculatorObserver* observer); | 41 void RemoveObserver(WallpaperColorCalculatorObserver* observer); |
| 39 | 42 |
| 40 // Initiates the calculation and returns false if the calculation fails to be | 43 // Initiates the calculation and returns false if the calculation fails to be |
| 41 // initiated. Observers may be notified synchronously or asynchronously. | 44 // initiated. Observers may be notified synchronously or asynchronously. |
| 42 // Callers should be aware that this will make |image_| read-only. | 45 // Callers should be aware that this will make |image_| read-only. |
| 43 bool StartCalculation() WARN_UNUSED_RESULT; | 46 bool StartCalculation() WARN_UNUSED_RESULT; |
| 44 | 47 |
| 45 SkColor prominent_color() const { return prominent_color_; } | 48 std::vector<SkColor> prominent_colors() const { return prominent_colors_; } |
| 46 | 49 |
| 47 void set_prominent_color_for_test(SkColor prominent_color) { | 50 void set_prominent_colors_for_test( |
| 48 prominent_color_ = prominent_color; | 51 const std::vector<SkColor>& prominent_colors) { |
| 52 prominent_colors_ = prominent_colors; |
| 49 } | 53 } |
| 50 | 54 |
| 51 // Explicitly sets the |task_runner_| for testing. | 55 // Explicitly sets the |task_runner_| for testing. |
| 52 void SetTaskRunnerForTest(scoped_refptr<base::TaskRunner> task_runner); | 56 void SetTaskRunnerForTest(scoped_refptr<base::TaskRunner> task_runner); |
| 53 | 57 |
| 54 private: | 58 private: |
| 55 // Handles asynchronous calculation results. |async_start_time| is used to | 59 // Handles asynchronous calculation results. |async_start_time| is used to |
| 56 // record duration metrics. | 60 // record duration metrics. |
| 57 void OnAsyncCalculationComplete(base::TimeTicks async_start_time, | 61 void OnAsyncCalculationComplete(base::TimeTicks async_start_time, |
| 58 SkColor prominent_color); | 62 const std::vector<SkColor>& prominent_colors); |
| 59 | 63 |
| 60 // Notifies observers that a color calulation has completed. Called on the | 64 // Notifies observers that a color calulation has completed. Called on the |
| 61 // same thread that constructed |this|. | 65 // same thread that constructed |this|. |
| 62 void NotifyCalculationComplete(SkColor prominent_color); | 66 void NotifyCalculationComplete(const std::vector<SkColor>& prominent_colors); |
| 63 | 67 |
| 64 // The result of the color calculation. | 68 // The result of the color calculation. |
| 65 SkColor prominent_color_ = SK_ColorTRANSPARENT; | 69 std::vector<SkColor> prominent_colors_; |
| 66 | 70 |
| 67 // The image to calculate colors from. | 71 // The image to calculate colors from. |
| 68 gfx::ImageSkia image_; | 72 gfx::ImageSkia image_; |
| 69 | 73 |
| 70 // Input for the color calculation. | 74 // The color profiles used to calculate colors. |
| 71 color_utils::LumaRange luma_; | 75 std::vector<color_utils::ColorProfile> color_profiles_; |
| 72 | |
| 73 // Input for the color calculation. | |
| 74 color_utils::SaturationRange saturation_; | |
| 75 | 76 |
| 76 // The task runner to run the calculation on. | 77 // The task runner to run the calculation on. |
| 77 scoped_refptr<base::TaskRunner> task_runner_; | 78 scoped_refptr<base::TaskRunner> task_runner_; |
| 78 | 79 |
| 79 base::ObserverList<WallpaperColorCalculatorObserver> observers_; | 80 base::ObserverList<WallpaperColorCalculatorObserver> observers_; |
| 80 | 81 |
| 81 base::WeakPtrFactory<WallpaperColorCalculator> weak_ptr_factory_; | 82 base::WeakPtrFactory<WallpaperColorCalculator> weak_ptr_factory_; |
| 82 | 83 |
| 83 DISALLOW_COPY_AND_ASSIGN(WallpaperColorCalculator); | 84 DISALLOW_COPY_AND_ASSIGN(WallpaperColorCalculator); |
| 84 }; | 85 }; |
| 85 | 86 |
| 86 } // namespace wallpaper | 87 } // namespace wallpaper |
| 87 | 88 |
| 88 #endif // COMPONENTS_WALLPAPER_WALLPAPER_COLOR_CALCULATOR_H_ | 89 #endif // COMPONENTS_WALLPAPER_WALLPAPER_COLOR_CALCULATOR_H_ |
| OLD | NEW |