Chromium Code Reviews| 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. | |
| 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.TimeSpentExtractingColors_v2", | |
| 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 ShouldCalculateAsync(const gfx::ImageSkia& image) { | |
|
Evan Stade
2017/04/19 13:44:31
I think the name of this is backwards?
bruthig
2017/04/19 15:15:52
Nice catch, thx!
| |
| 42 return kMaxPixelsForSynchronousCalculation >= image.width() * image.height(); | |
|
Evan Stade
2017/04/19 13:44:31
nit: do you think this reads more clearly as width
bruthig
2017/04/19 15:15:52
Done.
| |
| 43 } | |
| 44 | |
| 45 void RecordCalculationExecutionType(bool was_synchronous) { | |
| 46 UMA_HISTOGRAM_BOOLEAN("Ash.Wallpaper.ColorExtraction.ExecutionType", | |
| 47 was_synchronous); | |
| 48 } | |
| 49 | |
| 50 } // namespace | |
| 51 | |
| 18 WallpaperColorCalculator::WallpaperColorCalculator( | 52 WallpaperColorCalculator::WallpaperColorCalculator( |
| 19 const gfx::ImageSkia& image, | 53 const gfx::ImageSkia& image, |
| 20 color_utils::LumaRange luma, | 54 color_utils::LumaRange luma, |
| 21 color_utils::SaturationRange saturation, | 55 color_utils::SaturationRange saturation, |
| 22 scoped_refptr<base::TaskRunner> task_runner) | 56 scoped_refptr<base::TaskRunner> task_runner) |
| 23 : image_(image), | 57 : image_(image), |
| 24 luma_(luma), | 58 luma_(luma), |
| 25 saturation_(saturation), | 59 saturation_(saturation), |
| 26 task_runner_(std::move(task_runner)), | 60 task_runner_(std::move(task_runner)), |
| 27 weak_ptr_factory_(this) {} | 61 weak_ptr_factory_(this) {} |
| 28 | 62 |
| 29 WallpaperColorCalculator::~WallpaperColorCalculator() {} | 63 WallpaperColorCalculator::~WallpaperColorCalculator() {} |
| 30 | 64 |
| 31 void WallpaperColorCalculator::AddObserver( | 65 void WallpaperColorCalculator::AddObserver( |
| 32 WallpaperColorCalculatorObserver* observer) { | 66 WallpaperColorCalculatorObserver* observer) { |
| 33 observers_.AddObserver(observer); | 67 observers_.AddObserver(observer); |
| 34 } | 68 } |
| 35 | 69 |
| 36 void WallpaperColorCalculator::RemoveObserver( | 70 void WallpaperColorCalculator::RemoveObserver( |
| 37 WallpaperColorCalculatorObserver* observer) { | 71 WallpaperColorCalculatorObserver* observer) { |
| 38 observers_.RemoveObserver(observer); | 72 observers_.RemoveObserver(observer); |
| 39 } | 73 } |
| 40 | 74 |
| 41 bool WallpaperColorCalculator::StartCalculation() { | 75 bool WallpaperColorCalculator::StartCalculation() { |
| 42 start_calculation_time_ = base::Time::Now(); | 76 if (ShouldCalculateAsync(image_)) { |
| 77 RecordCalculationExecutionType(true); | |
| 43 | 78 |
| 44 image_.MakeThreadSafe(); | 79 const SkColor prominent_color = |
| 45 if (base::PostTaskAndReplyWithResult( | 80 CalculateWallpaperColor(*image_.bitmap(), luma_, saturation_); |
| 46 task_runner_.get(), FROM_HERE, | 81 WallpaperColorCalculator::NotifyCalculationComplete(prominent_color); |
| 47 base::Bind(&color_utils::CalculateProminentColorOfBitmap, | |
| 48 *image_.bitmap(), luma_, saturation_), | |
| 49 base::Bind(&WallpaperColorCalculator::NotifyCalculationComplete, | |
| 50 weak_ptr_factory_.GetWeakPtr()))) { | |
| 51 return true; | 82 return true; |
| 83 } else { | |
|
Evan Stade
2017/04/19 13:44:31
nit: no else after return.
bruthig
2017/04/19 15:15:52
Done.
| |
| 84 RecordCalculationExecutionType(false); | |
| 85 | |
| 86 image_.MakeThreadSafe(); | |
| 87 if (base::PostTaskAndReplyWithResult( | |
| 88 task_runner_.get(), FROM_HERE, | |
| 89 base::Bind(&CalculateWallpaperColor, *image_.bitmap(), luma_, | |
| 90 saturation_), | |
| 91 base::Bind(&WallpaperColorCalculator::OnAsyncCalculationComplete, | |
| 92 weak_ptr_factory_.GetWeakPtr(), | |
| 93 base::TimeTicks::Now()))) { | |
| 94 return true; | |
| 95 } | |
| 96 | |
| 97 LOG(WARNING) << "PostSequencedWorkerTask failed. " | |
| 98 << "Wallpaper promiment color may not be calculated."; | |
| 99 | |
| 100 prominent_color_ = SK_ColorTRANSPARENT; | |
| 101 return false; | |
| 52 } | 102 } |
| 53 | |
| 54 LOG(WARNING) << "PostSequencedWorkerTask failed. " | |
| 55 << "Wallpaper promiment color may not be calculated."; | |
| 56 | |
| 57 prominent_color_ = SK_ColorTRANSPARENT; | |
| 58 return false; | |
| 59 } | 103 } |
| 60 | 104 |
| 61 void WallpaperColorCalculator::SetTaskRunnerForTest( | 105 void WallpaperColorCalculator::SetTaskRunnerForTest( |
| 62 scoped_refptr<base::TaskRunner> task_runner) { | 106 scoped_refptr<base::TaskRunner> task_runner) { |
| 63 task_runner_ = task_runner; | 107 task_runner_ = task_runner; |
| 64 } | 108 } |
| 65 | 109 |
| 110 void WallpaperColorCalculator::OnAsyncCalculationComplete( | |
| 111 base::TimeTicks async_start_time, | |
| 112 SkColor prominent_color) { | |
| 113 UMA_HISTOGRAM_TIMES("Ash.Wallpaper.AsyncTimeSpentExtractingColors", | |
| 114 base::TimeTicks::Now() - async_start_time); | |
| 115 NotifyCalculationComplete(prominent_color); | |
| 116 } | |
| 117 | |
| 66 void WallpaperColorCalculator::NotifyCalculationComplete( | 118 void WallpaperColorCalculator::NotifyCalculationComplete( |
| 67 SkColor prominent_color) { | 119 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; | 120 prominent_color_ = prominent_color; |
| 75 for (auto& observer : observers_) | 121 for (auto& observer : observers_) |
| 76 observer.OnColorCalculationComplete(); | 122 observer.OnColorCalculationComplete(); |
| 77 | 123 |
| 78 // This could be deleted! | 124 // This could be deleted! |
| 79 } | 125 } |
| 80 | 126 |
| 81 } // namespace wallpaper | 127 } // namespace wallpaper |
| OLD | NEW |