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 #include "components/wallpaper/wallpaper_color_calculator.h" |
| 6 |
| 7 #include <string> |
| 8 #include <utility> |
| 9 |
| 10 #include "base/bind.h" |
| 11 #include "base/metrics/histogram_macros.h" |
| 12 #include "base/task_runner.h" |
| 13 #include "base/task_runner_util.h" |
| 14 |
| 15 namespace wallpaper { |
| 16 |
| 17 WallpaperColorCalculator::WallpaperColorCalculator( |
| 18 const gfx::ImageSkia& image, |
| 19 color_utils::LumaRange luma, |
| 20 color_utils::SaturationRange saturation, |
| 21 scoped_refptr<base::TaskRunner> task_runner) |
| 22 : image_(image), |
| 23 luma_(luma), |
| 24 saturation_(saturation), |
| 25 task_runner_(std::move(task_runner)), |
| 26 weak_ptr_factory_(this) {} |
| 27 |
| 28 WallpaperColorCalculator::~WallpaperColorCalculator() {} |
| 29 |
| 30 void WallpaperColorCalculator::AddObserver( |
| 31 WallpaperColorCalculatorObserver* observer) { |
| 32 observers_.AddObserver(observer); |
| 33 } |
| 34 |
| 35 void WallpaperColorCalculator::RemoveObserver( |
| 36 WallpaperColorCalculatorObserver* observer) { |
| 37 observers_.RemoveObserver(observer); |
| 38 } |
| 39 |
| 40 bool WallpaperColorCalculator::StartCalculation() { |
| 41 start_calculation_time_ = base::Time::Now(); |
| 42 |
| 43 image_.MakeThreadSafe(); |
| 44 if (base::PostTaskAndReplyWithResult( |
| 45 task_runner_.get(), FROM_HERE, |
| 46 base::Bind(&color_utils::CalculateProminentColorOfBitmap, |
| 47 *image_.bitmap(), luma_, saturation_), |
| 48 base::Bind(&WallpaperColorCalculator::NotifyCalculationComplete, |
| 49 weak_ptr_factory_.GetWeakPtr()))) { |
| 50 return true; |
| 51 } |
| 52 |
| 53 LOG(WARNING) << "PostSequencedWorkerTask failed. " |
| 54 << "Wallpaper promiment color may not be calculated."; |
| 55 |
| 56 prominent_color_ = SK_ColorTRANSPARENT; |
| 57 return false; |
| 58 } |
| 59 |
| 60 void WallpaperColorCalculator::SetTaskRunnerForTest( |
| 61 scoped_refptr<base::TaskRunner> task_runner) { |
| 62 task_runner_ = task_runner; |
| 63 } |
| 64 |
| 65 void WallpaperColorCalculator::NotifyCalculationComplete( |
| 66 SkColor prominent_color) { |
| 67 UMA_HISTOGRAM_MEDIUM_TIMES("Ash.Wallpaper.TimeSpentExtractingColors", |
| 68 base::Time::Now() - start_calculation_time_); |
| 69 |
| 70 prominent_color_ = prominent_color; |
| 71 for (auto& observer : observers_) |
| 72 observer.OnColorCalculationComplete(); |
| 73 |
| 74 // This could be deleted! |
| 75 } |
| 76 |
| 77 } // namespace wallpaper |
OLD | NEW |