OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #ifndef COMPONENTS_WALLPAPER_WALLPAPER_COLOR_CALCULATOR_H_ |
| 6 #define COMPONENTS_WALLPAPER_WALLPAPER_COLOR_CALCULATOR_H_ |
| 7 |
| 8 #include "base/macros.h" |
| 9 #include "base/memory/ref_counted.h" |
| 10 #include "base/memory/weak_ptr.h" |
| 11 #include "base/observer_list.h" |
| 12 #include "base/time/time.h" |
| 13 #include "components/wallpaper/wallpaper_color_calculator_observer.h" |
| 14 #include "components/wallpaper/wallpaper_export.h" |
| 15 #include "third_party/skia/include/core/SkColor.h" |
| 16 #include "ui/gfx/color_analysis.h" |
| 17 #include "ui/gfx/image/image_skia.h" |
| 18 |
| 19 namespace base { |
| 20 class TaskRunner; |
| 21 } |
| 22 |
| 23 namespace wallpaper { |
| 24 class WallpaperColorCalculatorObserver; |
| 25 |
| 26 // Asynchronously calculates colors based on a wallpaper image. |
| 27 class WALLPAPER_EXPORT WallpaperColorCalculator { |
| 28 public: |
| 29 // |image|, |luma| and |saturation| are the input parameters to the color |
| 30 // calculation that is executed on the |task_runner|. |
| 31 WallpaperColorCalculator(const gfx::ImageSkia& image, |
| 32 color_utils::LumaRange luma, |
| 33 color_utils::SaturationRange saturation, |
| 34 scoped_refptr<base::TaskRunner> task_runner); |
| 35 ~WallpaperColorCalculator(); |
| 36 |
| 37 void AddObserver(WallpaperColorCalculatorObserver* observer); |
| 38 |
| 39 void RemoveObserver(WallpaperColorCalculatorObserver* observer); |
| 40 |
| 41 // Initiates the calculation and returns false if the calculation fails to be |
| 42 // initiated. Callers should be aware that this will make |image_| read-only. |
| 43 bool StartCalculation() WARN_UNUSED_RESULT; |
| 44 |
| 45 SkColor prominent_color() const { return prominent_color_; } |
| 46 |
| 47 void set_prominent_color_for_test(SkColor prominent_color) { |
| 48 prominent_color_ = prominent_color; |
| 49 } |
| 50 |
| 51 // Explicitly sets the |task_runner_| for testing. |
| 52 void SetTaskRunnerForTest(scoped_refptr<base::TaskRunner> task_runner); |
| 53 |
| 54 private: |
| 55 // Notifies observers that a color calulation has completed. Called on the |
| 56 // same thread that constructed |this|. |
| 57 void NotifyCalculationComplete(SkColor prominent_color); |
| 58 |
| 59 // The result of the color calculation. |
| 60 SkColor prominent_color_ = SK_ColorTRANSPARENT; |
| 61 |
| 62 // The image to calculate colors from. |
| 63 gfx::ImageSkia image_; |
| 64 |
| 65 // Input for the color calculation. |
| 66 color_utils::LumaRange luma_; |
| 67 |
| 68 // Input for the color calculation. |
| 69 color_utils::SaturationRange saturation_; |
| 70 |
| 71 // The task runner to run the calculation on. |
| 72 scoped_refptr<base::TaskRunner> task_runner_; |
| 73 |
| 74 // The time that StartCalculation() was last called. Used for recording |
| 75 // timing metrics. |
| 76 base::Time start_calculation_time_; |
| 77 |
| 78 base::ObserverList<WallpaperColorCalculatorObserver> observers_; |
| 79 |
| 80 base::WeakPtrFactory<WallpaperColorCalculator> weak_ptr_factory_; |
| 81 |
| 82 DISALLOW_COPY_AND_ASSIGN(WallpaperColorCalculator); |
| 83 }; |
| 84 |
| 85 } // namespace wallpaper |
| 86 |
| 87 #endif // COMPONENTS_WALLPAPER_WALLPAPER_COLOR_CALCULATOR_H_ |
OLD | NEW |