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

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

Issue 2943333003: Extracting more than one wallpaper prominent color (Closed)
Patch Set: Created 3 years, 6 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 { 18 namespace {
19 19
20 // The largest image size, in pixels, to synchronously calculate the prominent 20 // The largest image size, in pixels, to synchronously calculate the prominent
21 // color. This is a simple heuristic optimization because extraction on images 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 22 // smaller than this should run very quickly, and offloading the task to another
23 // thread would actually take longer. 23 // thread would actually take longer.
24 const int kMaxPixelsForSynchronousCalculation = 100; 24 const int kMaxPixelsForSynchronousCalculation = 100;
25 25
26 // Wrapper for color_utils::CalculateProminentColorOfBitmap() that records 26 // Wrapper for color_utils::CalculateProminentColorsOfBitmap() that records
27 // wallpaper specific metrics. 27 // wallpaper specific metrics.
28 // 28 //
29 // NOTE: |image| is intentionally a copy to ensure it exists for the duration of 29 // NOTE: |image| is intentionally a copy to ensure it exists for the duration of
30 // the calculation. 30 // the calculation.
31 SkColor CalculateWallpaperColor(const gfx::ImageSkia image, 31 std::vector<SkColor> CalculateWallpaperColor(
32 color_utils::LumaRange luma, 32 const gfx::ImageSkia image,
33 color_utils::SaturationRange saturation) { 33 const color_utils::ColorProfiles& color_profiles) {
34 base::TimeTicks start_time = base::TimeTicks::Now(); 34 base::TimeTicks start_time = base::TimeTicks::Now();
35 const SkColor prominent_color = color_utils::CalculateProminentColorOfBitmap( 35 const std::vector<SkColor> prominent_colors =
36 *image.bitmap(), luma, saturation); 36 color_utils::CalculateProminentColorsOfBitmap(*image.bitmap(),
37 color_profiles);
37 38
38 UMA_HISTOGRAM_TIMES("Ash.Wallpaper.ColorExtraction.Durations", 39 // TODO(warx): reconsider these UMAs for multiple colors calculation.
Qiang(Joe) Xu 2017/06/19 22:51:30 This is a workaround to please the wallpaper_color
39 base::TimeTicks::Now() - start_time); 40 if (1u == color_profiles.size()) {
40 UMA_HISTOGRAM_BOOLEAN("Ash.Wallpaper.ColorExtractionResult", 41 UMA_HISTOGRAM_TIMES("Ash.Wallpaper.ColorExtraction.Durations",
41 prominent_color != SK_ColorTRANSPARENT); 42 base::TimeTicks::Now() - start_time);
43 UMA_HISTOGRAM_BOOLEAN("Ash.Wallpaper.ColorExtractionResult",
44 prominent_colors[0] != SK_ColorTRANSPARENT);
45 }
42 46
43 return prominent_color; 47 return prominent_colors;
44 } 48 }
45 49
46 bool ShouldCalculateSync(const gfx::ImageSkia& image) { 50 bool ShouldCalculateSync(const gfx::ImageSkia& image) {
47 return image.width() * image.height() <= kMaxPixelsForSynchronousCalculation; 51 return image.width() * image.height() <= kMaxPixelsForSynchronousCalculation;
48 } 52 }
49 53
50 } // namespace 54 } // namespace
51 55
52 WallpaperColorCalculator::WallpaperColorCalculator( 56 WallpaperColorCalculator::WallpaperColorCalculator(
53 const gfx::ImageSkia& image, 57 const gfx::ImageSkia& image,
54 color_utils::LumaRange luma, 58 const color_utils::ColorProfiles& color_profiles,
55 color_utils::SaturationRange saturation,
56 scoped_refptr<base::TaskRunner> task_runner) 59 scoped_refptr<base::TaskRunner> task_runner)
57 : image_(image), 60 : image_(image),
58 luma_(luma), 61 color_profiles_(color_profiles),
59 saturation_(saturation),
60 task_runner_(std::move(task_runner)), 62 task_runner_(std::move(task_runner)),
61 weak_ptr_factory_(this) {} 63 weak_ptr_factory_(this) {
64 prominent_colors_ =
65 std::vector<SkColor>(color_profiles_.size(), SK_ColorTRANSPARENT);
66 }
62 67
63 WallpaperColorCalculator::~WallpaperColorCalculator() {} 68 WallpaperColorCalculator::~WallpaperColorCalculator() = default;
64 69
65 void WallpaperColorCalculator::AddObserver( 70 void WallpaperColorCalculator::AddObserver(
66 WallpaperColorCalculatorObserver* observer) { 71 WallpaperColorCalculatorObserver* observer) {
67 observers_.AddObserver(observer); 72 observers_.AddObserver(observer);
68 } 73 }
69 74
70 void WallpaperColorCalculator::RemoveObserver( 75 void WallpaperColorCalculator::RemoveObserver(
71 WallpaperColorCalculatorObserver* observer) { 76 WallpaperColorCalculatorObserver* observer) {
72 observers_.RemoveObserver(observer); 77 observers_.RemoveObserver(observer);
73 } 78 }
74 79
75 bool WallpaperColorCalculator::StartCalculation() { 80 bool WallpaperColorCalculator::StartCalculation() {
76 if (ShouldCalculateSync(image_)) { 81 if (ShouldCalculateSync(image_)) {
77 const SkColor prominent_color = 82 const std::vector<SkColor> prominent_colors =
78 CalculateWallpaperColor(image_, luma_, saturation_); 83 CalculateWallpaperColor(image_, color_profiles_);
79 NotifyCalculationComplete(prominent_color); 84 NotifyCalculationComplete(prominent_colors);
80 return true; 85 return true;
81 } 86 }
82 87
83 image_.MakeThreadSafe(); 88 image_.MakeThreadSafe();
84 if (base::PostTaskAndReplyWithResult( 89 if (base::PostTaskAndReplyWithResult(
85 task_runner_.get(), FROM_HERE, 90 task_runner_.get(), FROM_HERE,
86 base::Bind(&CalculateWallpaperColor, image_, luma_, saturation_), 91 base::Bind(&CalculateWallpaperColor, image_, color_profiles_),
87 base::Bind(&WallpaperColorCalculator::OnAsyncCalculationComplete, 92 base::Bind(&WallpaperColorCalculator::OnAsyncCalculationComplete,
88 weak_ptr_factory_.GetWeakPtr(), base::TimeTicks::Now()))) { 93 weak_ptr_factory_.GetWeakPtr(), base::TimeTicks::Now()))) {
89 return true; 94 return true;
90 } 95 }
91 96
92 LOG(WARNING) << "PostSequencedWorkerTask failed. " 97 LOG(WARNING) << "PostSequencedWorkerTask failed. "
93 << "Wallpaper promiment color may not be calculated."; 98 << "Wallpaper prominent colors may not be calculated.";
94 99
95 prominent_color_ = SK_ColorTRANSPARENT; 100 prominent_colors_ =
101 std::vector<SkColor>(color_profiles_.size(), SK_ColorTRANSPARENT);
96 return false; 102 return false;
97 } 103 }
98 104
99 void WallpaperColorCalculator::SetTaskRunnerForTest( 105 void WallpaperColorCalculator::SetTaskRunnerForTest(
100 scoped_refptr<base::TaskRunner> task_runner) { 106 scoped_refptr<base::TaskRunner> task_runner) {
101 task_runner_ = task_runner; 107 task_runner_ = task_runner;
102 } 108 }
103 109
104 void WallpaperColorCalculator::OnAsyncCalculationComplete( 110 void WallpaperColorCalculator::OnAsyncCalculationComplete(
105 base::TimeTicks async_start_time, 111 base::TimeTicks async_start_time,
106 SkColor prominent_color) { 112 const std::vector<SkColor>& prominent_colors) {
107 UMA_HISTOGRAM_TIMES("Ash.Wallpaper.ColorExtraction.UserDelay", 113 UMA_HISTOGRAM_TIMES("Ash.Wallpaper.ColorExtraction.UserDelay",
108 base::TimeTicks::Now() - async_start_time); 114 base::TimeTicks::Now() - async_start_time);
109 NotifyCalculationComplete(prominent_color); 115 NotifyCalculationComplete(prominent_colors);
110 } 116 }
111 117
112 void WallpaperColorCalculator::NotifyCalculationComplete( 118 void WallpaperColorCalculator::NotifyCalculationComplete(
113 SkColor prominent_color) { 119 const std::vector<SkColor>& prominent_colors) {
114 prominent_color_ = prominent_color; 120 prominent_colors_ = prominent_colors;
115 for (auto& observer : observers_) 121 for (auto& observer : observers_)
116 observer.OnColorCalculationComplete(); 122 observer.OnColorCalculationComplete();
117 123
118 // This could be deleted! 124 // This could be deleted!
119 } 125 }
120 126
121 } // namespace wallpaper 127 } // namespace wallpaper
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698