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 #include "components/wallpaper/wallpaper_color_calculator.h" | 5 #include "components/wallpaper/wallpaper_color_calculator.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 #include <utility> | 8 #include <utility> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
11 #include "base/metrics/histogram_macros.h" | 11 #include "base/metrics/histogram_macros.h" |
12 #include "base/task_runner.h" | 12 #include "base/task_runner.h" |
13 #include "base/task_runner_util.h" | 13 #include "base/task_runner_util.h" |
14 #include "components/wallpaper/wallpaper_color_calculator_observer.h" | 14 #include "components/wallpaper/wallpaper_color_calculator_observer.h" |
15 | 15 |
16 namespace wallpaper { | 16 namespace wallpaper { |
17 | 17 |
18 namespace { | |
19 | |
20 // The largest image size, in pixels, to synchronously calculate the prominent | |
21 // color. | |
Evan Stade
2017/04/20 21:37:15
nit: perhaps explain the motivation briefly, e.g.
bruthig
2017/04/21 15:42:08
Done.
| |
22 const int kMaxPixelsForSynchronousCalculation = 100; | |
23 | |
24 // Wrapper for color_utils::CalculateProminentColorOfBitmap() that records | |
25 // wallpaper specific metrics. | |
26 SkColor CalculateWallpaperColor(const SkBitmap& bitmap, | |
27 color_utils::LumaRange luma, | |
28 color_utils::SaturationRange saturation) { | |
29 base::TimeTicks start_time = base::TimeTicks::Now(); | |
30 const SkColor prominent_color = | |
31 color_utils::CalculateProminentColorOfBitmap(bitmap, luma, saturation); | |
32 | |
33 UMA_HISTOGRAM_TIMES("Ash.Wallpaper.ColorExtraction.Durations", | |
34 base::TimeTicks::Now() - start_time); | |
35 UMA_HISTOGRAM_BOOLEAN("Ash.Wallpaper.ColorExtractionResult", | |
36 prominent_color != SK_ColorTRANSPARENT); | |
37 | |
38 return prominent_color; | |
39 } | |
40 | |
41 bool ShouldCalculateSync(const gfx::ImageSkia& image) { | |
42 return image.width() * image.height() <= kMaxPixelsForSynchronousCalculation; | |
43 } | |
44 | |
45 } // namespace | |
46 | |
18 WallpaperColorCalculator::WallpaperColorCalculator( | 47 WallpaperColorCalculator::WallpaperColorCalculator( |
19 const gfx::ImageSkia& image, | 48 const gfx::ImageSkia& image, |
20 color_utils::LumaRange luma, | 49 color_utils::LumaRange luma, |
21 color_utils::SaturationRange saturation, | 50 color_utils::SaturationRange saturation, |
22 scoped_refptr<base::TaskRunner> task_runner) | 51 scoped_refptr<base::TaskRunner> task_runner) |
23 : image_(image), | 52 : image_(image), |
24 luma_(luma), | 53 luma_(luma), |
25 saturation_(saturation), | 54 saturation_(saturation), |
26 task_runner_(std::move(task_runner)), | 55 task_runner_(std::move(task_runner)), |
27 weak_ptr_factory_(this) {} | 56 weak_ptr_factory_(this) {} |
28 | 57 |
29 WallpaperColorCalculator::~WallpaperColorCalculator() {} | 58 WallpaperColorCalculator::~WallpaperColorCalculator() {} |
30 | 59 |
31 void WallpaperColorCalculator::AddObserver( | 60 void WallpaperColorCalculator::AddObserver( |
32 WallpaperColorCalculatorObserver* observer) { | 61 WallpaperColorCalculatorObserver* observer) { |
33 observers_.AddObserver(observer); | 62 observers_.AddObserver(observer); |
34 } | 63 } |
35 | 64 |
36 void WallpaperColorCalculator::RemoveObserver( | 65 void WallpaperColorCalculator::RemoveObserver( |
37 WallpaperColorCalculatorObserver* observer) { | 66 WallpaperColorCalculatorObserver* observer) { |
38 observers_.RemoveObserver(observer); | 67 observers_.RemoveObserver(observer); |
39 } | 68 } |
40 | 69 |
41 bool WallpaperColorCalculator::StartCalculation() { | 70 bool WallpaperColorCalculator::StartCalculation() { |
42 start_calculation_time_ = base::Time::Now(); | 71 if (ShouldCalculateSync(image_)) { |
72 const SkColor prominent_color = | |
73 CalculateWallpaperColor(*image_.bitmap(), luma_, saturation_); | |
74 WallpaperColorCalculator::NotifyCalculationComplete(prominent_color); | |
Evan Stade
2017/04/20 21:37:15
nit: WallpaperColorCalculator:: isn't necessary is
bruthig
2017/04/21 15:42:08
Whoops, removed.
| |
75 return true; | |
76 } | |
43 | 77 |
44 image_.MakeThreadSafe(); | 78 image_.MakeThreadSafe(); |
45 if (base::PostTaskAndReplyWithResult( | 79 if (base::PostTaskAndReplyWithResult( |
46 task_runner_.get(), FROM_HERE, | 80 task_runner_.get(), FROM_HERE, |
47 base::Bind(&color_utils::CalculateProminentColorOfBitmap, | 81 base::Bind(&CalculateWallpaperColor, *image_.bitmap(), luma_, |
48 *image_.bitmap(), luma_, saturation_), | 82 saturation_), |
49 base::Bind(&WallpaperColorCalculator::NotifyCalculationComplete, | 83 base::Bind(&WallpaperColorCalculator::OnAsyncCalculationComplete, |
50 weak_ptr_factory_.GetWeakPtr()))) { | 84 weak_ptr_factory_.GetWeakPtr(), base::TimeTicks::Now()))) { |
51 return true; | 85 return true; |
52 } | 86 } |
53 | 87 |
54 LOG(WARNING) << "PostSequencedWorkerTask failed. " | 88 LOG(WARNING) << "PostSequencedWorkerTask failed. " |
55 << "Wallpaper promiment color may not be calculated."; | 89 << "Wallpaper promiment color may not be calculated."; |
56 | 90 |
57 prominent_color_ = SK_ColorTRANSPARENT; | 91 prominent_color_ = SK_ColorTRANSPARENT; |
58 return false; | 92 return false; |
59 } | 93 } |
60 | 94 |
61 void WallpaperColorCalculator::SetTaskRunnerForTest( | 95 void WallpaperColorCalculator::SetTaskRunnerForTest( |
62 scoped_refptr<base::TaskRunner> task_runner) { | 96 scoped_refptr<base::TaskRunner> task_runner) { |
63 task_runner_ = task_runner; | 97 task_runner_ = task_runner; |
64 } | 98 } |
65 | 99 |
100 void WallpaperColorCalculator::OnAsyncCalculationComplete( | |
101 base::TimeTicks async_start_time, | |
102 SkColor prominent_color) { | |
103 UMA_HISTOGRAM_TIMES("Ash.Wallpaper.ColorExtraction.UserDelay", | |
104 base::TimeTicks::Now() - async_start_time); | |
105 NotifyCalculationComplete(prominent_color); | |
106 } | |
107 | |
66 void WallpaperColorCalculator::NotifyCalculationComplete( | 108 void WallpaperColorCalculator::NotifyCalculationComplete( |
67 SkColor prominent_color) { | 109 SkColor prominent_color) { |
68 UMA_HISTOGRAM_MEDIUM_TIMES("Ash.Wallpaper.TimeSpentExtractingColors", | |
69 base::Time::Now() - start_calculation_time_); | |
70 | |
71 UMA_HISTOGRAM_BOOLEAN("Ash.Wallpaper.ColorExtractionResult", | |
72 prominent_color != SK_ColorTRANSPARENT); | |
73 | |
74 prominent_color_ = prominent_color; | 110 prominent_color_ = prominent_color; |
75 for (auto& observer : observers_) | 111 for (auto& observer : observers_) |
76 observer.OnColorCalculationComplete(); | 112 observer.OnColorCalculationComplete(); |
77 | 113 |
78 // This could be deleted! | 114 // This could be deleted! |
79 } | 115 } |
80 | 116 |
81 } // namespace wallpaper | 117 } // namespace wallpaper |
OLD | NEW |