Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1326)

Side by Side Diff: components/wallpaper/wallpaper_color_calculator.cc

Issue 2824883006: [ash-md] Reduce thread hopping for cheap wallpaper color extraction. (Closed)
Patch Set: Addressed patch set 4 comments. Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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. This is a simple heuristic optimization because extraction on images
22 // smaller than this should run very quickly, and offloading the task to another
23 // thread would actually take longer.
24 const int kMaxPixelsForSynchronousCalculation = 100;
25
26 // Wrapper for color_utils::CalculateProminentColorOfBitmap() that records
27 // wallpaper specific metrics.
28 //
29 // NOTE: |image| is intentionally a copy to ensure it exists for the duration of
Evan Stade 2017/04/25 16:24:39 thumbs up
30 // the calculation.
31 SkColor CalculateWallpaperColor(const gfx::ImageSkia image,
32 color_utils::LumaRange luma,
33 color_utils::SaturationRange saturation) {
34 base::TimeTicks start_time = base::TimeTicks::Now();
35 const SkColor prominent_color = color_utils::CalculateProminentColorOfBitmap(
36 *image.bitmap(), luma, saturation);
37
38 UMA_HISTOGRAM_TIMES("Ash.Wallpaper.ColorExtraction.Durations",
39 base::TimeTicks::Now() - start_time);
40 UMA_HISTOGRAM_BOOLEAN("Ash.Wallpaper.ColorExtractionResult",
41 prominent_color != SK_ColorTRANSPARENT);
42
43 return prominent_color;
44 }
45
46 bool ShouldCalculateSync(const gfx::ImageSkia& image) {
47 return image.width() * image.height() <= kMaxPixelsForSynchronousCalculation;
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 (ShouldCalculateSync(image_)) {
77 const SkColor prominent_color =
78 CalculateWallpaperColor(image_, luma_, saturation_);
79 NotifyCalculationComplete(prominent_color);
80 return true;
81 }
43 82
44 image_.MakeThreadSafe(); 83 image_.MakeThreadSafe();
45 if (base::PostTaskAndReplyWithResult( 84 if (base::PostTaskAndReplyWithResult(
46 task_runner_.get(), FROM_HERE, 85 task_runner_.get(), FROM_HERE,
47 base::Bind(&color_utils::CalculateProminentColorOfBitmap, 86 base::Bind(&CalculateWallpaperColor, image_, luma_, saturation_),
48 *image_.bitmap(), luma_, saturation_), 87 base::Bind(&WallpaperColorCalculator::OnAsyncCalculationComplete,
49 base::Bind(&WallpaperColorCalculator::NotifyCalculationComplete, 88 weak_ptr_factory_.GetWeakPtr(), base::TimeTicks::Now()))) {
50 weak_ptr_factory_.GetWeakPtr()))) {
51 return true; 89 return true;
52 } 90 }
53 91
54 LOG(WARNING) << "PostSequencedWorkerTask failed. " 92 LOG(WARNING) << "PostSequencedWorkerTask failed. "
55 << "Wallpaper promiment color may not be calculated."; 93 << "Wallpaper promiment color may not be calculated.";
56 94
57 prominent_color_ = SK_ColorTRANSPARENT; 95 prominent_color_ = SK_ColorTRANSPARENT;
58 return false; 96 return false;
59 } 97 }
60 98
61 void WallpaperColorCalculator::SetTaskRunnerForTest( 99 void WallpaperColorCalculator::SetTaskRunnerForTest(
62 scoped_refptr<base::TaskRunner> task_runner) { 100 scoped_refptr<base::TaskRunner> task_runner) {
63 task_runner_ = task_runner; 101 task_runner_ = task_runner;
64 } 102 }
65 103
104 void WallpaperColorCalculator::OnAsyncCalculationComplete(
105 base::TimeTicks async_start_time,
106 SkColor prominent_color) {
107 UMA_HISTOGRAM_TIMES("Ash.Wallpaper.ColorExtraction.UserDelay",
108 base::TimeTicks::Now() - async_start_time);
109 NotifyCalculationComplete(prominent_color);
110 }
111
66 void WallpaperColorCalculator::NotifyCalculationComplete( 112 void WallpaperColorCalculator::NotifyCalculationComplete(
67 SkColor prominent_color) { 113 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; 114 prominent_color_ = prominent_color;
75 for (auto& observer : observers_) 115 for (auto& observer : observers_)
76 observer.OnColorCalculationComplete(); 116 observer.OnColorCalculationComplete();
77 117
78 // This could be deleted! 118 // This could be deleted!
79 } 119 }
80 120
81 } // namespace wallpaper 121 } // namespace wallpaper
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698