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

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

Issue 2943333003: Extracting more than one wallpaper prominent color (Closed)
Patch Set: feedback from ps4 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 #include "components/wallpaper/wallpaper_color_extraction_result.h"
16 #include "ui/gfx/color_analysis.h"
17 #include "ui/gfx/image/image_skia.h"
18
19 using LumaRange = color_utils::LumaRange;
20 using SaturationRange = color_utils::SaturationRange;
15 21
16 namespace wallpaper { 22 namespace wallpaper {
17 23
18 namespace { 24 namespace {
19 25
20 // The largest image size, in pixels, to synchronously calculate the prominent 26 // The largest image size, in pixels, to synchronously calculate the prominent
21 // color. This is a simple heuristic optimization because extraction on images 27 // 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 28 // smaller than this should run very quickly, and offloading the task to another
23 // thread would actually take longer. 29 // thread would actually take longer.
24 const int kMaxPixelsForSynchronousCalculation = 100; 30 const int kMaxPixelsForSynchronousCalculation = 100;
25 31
26 // Wrapper for color_utils::CalculateProminentColorOfBitmap() that records 32 // Wrapper for color_utils::CalculateProminentColorsOfBitmap() that records
27 // wallpaper specific metrics. 33 // wallpaper specific metrics.
28 // 34 //
29 // NOTE: |image| is intentionally a copy to ensure it exists for the duration of 35 // NOTE: |image| is intentionally a copy to ensure it exists for the duration of
30 // the calculation. 36 // the calculation.
31 SkColor CalculateWallpaperColor(const gfx::ImageSkia image, 37 std::vector<SkColor> CalculateWallpaperColor(
32 color_utils::LumaRange luma, 38 const gfx::ImageSkia image,
33 color_utils::SaturationRange saturation) { 39 const std::vector<color_utils::ColorProfile> color_profiles) {
34 base::TimeTicks start_time = base::TimeTicks::Now(); 40 base::TimeTicks start_time = base::TimeTicks::Now();
35 const SkColor prominent_color = color_utils::CalculateProminentColorOfBitmap( 41 const std::vector<SkColor> prominent_colors =
36 *image.bitmap(), luma, saturation); 42 color_utils::CalculateProminentColorsOfBitmap(*image.bitmap(),
43 color_profiles);
37 44
38 UMA_HISTOGRAM_TIMES("Ash.Wallpaper.ColorExtraction.Durations", 45 UMA_HISTOGRAM_TIMES("Ash.Wallpaper.ColorExtraction.Durations",
39 base::TimeTicks::Now() - start_time); 46 base::TimeTicks::Now() - start_time);
40 UMA_HISTOGRAM_BOOLEAN("Ash.Wallpaper.ColorExtractionResult", 47 WallpaperColorExtractionResult result;
41 prominent_color != SK_ColorTRANSPARENT); 48 for (size_t i = 0; i < color_profiles.size(); ++i) {
49 bool is_result_transparent = prominent_colors[i] == SK_ColorTRANSPARENT;
50 if (color_profiles[i].saturation == SaturationRange::VIBRANT) {
51 switch (color_profiles[i].luma) {
52 case LumaRange::DARK:
53 result = is_result_transparent ? RESULT_DARK_VIBRANT_TRANSPARENT
54 : RESULT_DARK_VIBRANT_OPAQUE;
55 break;
56 case LumaRange::NORMAL:
57 result = is_result_transparent ? RESULT_NORMAL_VIBRANT_TRANSPARENT
58 : RESULT_NORMAL_VIBRANT_OPAQUE;
59 break;
60 case LumaRange::LIGHT:
61 result = is_result_transparent ? RESULT_LIGHT_VIBRANT_TRANSPARENT
62 : RESULT_LIGHT_VIBRANT_OPAQUE;
63 break;
64 }
65 } else {
66 switch (color_profiles[i].luma) {
67 case LumaRange::DARK:
68 result = is_result_transparent ? RESULT_DARK_MUTED_TRANSPARENT
69 : RESULT_DARK_MUTED_OPAQUE;
70 break;
71 case LumaRange::NORMAL:
72 result = is_result_transparent ? RESULT_NORMAL_MUTED_TRANSPARENT
73 : RESULT_NORMAL_MUTED_OPAQUE;
74 break;
75 case LumaRange::LIGHT:
76 result = is_result_transparent ? RESULT_LIGHT_MUTED_TRANSPARENT
77 : RESULT_LIGHT_MUTED_OPAQUE;
78 break;
79 }
80 }
81 }
82 UMA_HISTOGRAM_ENUMERATION("Ash.Wallpaper.ColorExtractionResultEnum", result,
83 NUM_COLOR_EXTRACTION_RESULTS);
42 84
43 return prominent_color; 85 return prominent_colors;
44 } 86 }
45 87
46 bool ShouldCalculateSync(const gfx::ImageSkia& image) { 88 bool ShouldCalculateSync(const gfx::ImageSkia& image) {
47 return image.width() * image.height() <= kMaxPixelsForSynchronousCalculation; 89 return image.width() * image.height() <= kMaxPixelsForSynchronousCalculation;
48 } 90 }
49 91
50 } // namespace 92 } // namespace
51 93
52 WallpaperColorCalculator::WallpaperColorCalculator( 94 WallpaperColorCalculator::WallpaperColorCalculator(
53 const gfx::ImageSkia& image, 95 const gfx::ImageSkia& image,
54 color_utils::LumaRange luma, 96 const std::vector<color_utils::ColorProfile>& color_profiles,
55 color_utils::SaturationRange saturation,
56 scoped_refptr<base::TaskRunner> task_runner) 97 scoped_refptr<base::TaskRunner> task_runner)
57 : image_(image), 98 : image_(image),
58 luma_(luma), 99 color_profiles_(color_profiles),
59 saturation_(saturation),
60 task_runner_(std::move(task_runner)), 100 task_runner_(std::move(task_runner)),
61 weak_ptr_factory_(this) {} 101 weak_ptr_factory_(this) {
102 prominent_colors_ =
103 std::vector<SkColor>(color_profiles_.size(), SK_ColorTRANSPARENT);
104 }
62 105
63 WallpaperColorCalculator::~WallpaperColorCalculator() {} 106 WallpaperColorCalculator::~WallpaperColorCalculator() = default;
64 107
65 void WallpaperColorCalculator::AddObserver( 108 void WallpaperColorCalculator::AddObserver(
66 WallpaperColorCalculatorObserver* observer) { 109 WallpaperColorCalculatorObserver* observer) {
67 observers_.AddObserver(observer); 110 observers_.AddObserver(observer);
68 } 111 }
69 112
70 void WallpaperColorCalculator::RemoveObserver( 113 void WallpaperColorCalculator::RemoveObserver(
71 WallpaperColorCalculatorObserver* observer) { 114 WallpaperColorCalculatorObserver* observer) {
72 observers_.RemoveObserver(observer); 115 observers_.RemoveObserver(observer);
73 } 116 }
74 117
75 bool WallpaperColorCalculator::StartCalculation() { 118 bool WallpaperColorCalculator::StartCalculation() {
76 if (ShouldCalculateSync(image_)) { 119 if (ShouldCalculateSync(image_)) {
77 const SkColor prominent_color = 120 const std::vector<SkColor> prominent_colors =
78 CalculateWallpaperColor(image_, luma_, saturation_); 121 CalculateWallpaperColor(image_, color_profiles_);
79 NotifyCalculationComplete(prominent_color); 122 NotifyCalculationComplete(prominent_colors);
80 return true; 123 return true;
81 } 124 }
82 125
83 image_.MakeThreadSafe(); 126 image_.MakeThreadSafe();
84 if (base::PostTaskAndReplyWithResult( 127 if (base::PostTaskAndReplyWithResult(
85 task_runner_.get(), FROM_HERE, 128 task_runner_.get(), FROM_HERE,
86 base::Bind(&CalculateWallpaperColor, image_, luma_, saturation_), 129 base::Bind(&CalculateWallpaperColor, image_, color_profiles_),
87 base::Bind(&WallpaperColorCalculator::OnAsyncCalculationComplete, 130 base::Bind(&WallpaperColorCalculator::OnAsyncCalculationComplete,
88 weak_ptr_factory_.GetWeakPtr(), base::TimeTicks::Now()))) { 131 weak_ptr_factory_.GetWeakPtr(), base::TimeTicks::Now()))) {
89 return true; 132 return true;
90 } 133 }
91 134
92 LOG(WARNING) << "PostSequencedWorkerTask failed. " 135 LOG(WARNING) << "PostSequencedWorkerTask failed. "
93 << "Wallpaper promiment color may not be calculated."; 136 << "Wallpaper prominent colors may not be calculated.";
94 137
95 prominent_color_ = SK_ColorTRANSPARENT; 138 prominent_colors_ =
139 std::vector<SkColor>(color_profiles_.size(), SK_ColorTRANSPARENT);
96 return false; 140 return false;
97 } 141 }
98 142
99 void WallpaperColorCalculator::SetTaskRunnerForTest( 143 void WallpaperColorCalculator::SetTaskRunnerForTest(
100 scoped_refptr<base::TaskRunner> task_runner) { 144 scoped_refptr<base::TaskRunner> task_runner) {
101 task_runner_ = task_runner; 145 task_runner_ = task_runner;
102 } 146 }
103 147
104 void WallpaperColorCalculator::OnAsyncCalculationComplete( 148 void WallpaperColorCalculator::OnAsyncCalculationComplete(
105 base::TimeTicks async_start_time, 149 base::TimeTicks async_start_time,
106 SkColor prominent_color) { 150 const std::vector<SkColor>& prominent_colors) {
107 UMA_HISTOGRAM_TIMES("Ash.Wallpaper.ColorExtraction.UserDelay", 151 UMA_HISTOGRAM_TIMES("Ash.Wallpaper.ColorExtraction.UserDelay",
108 base::TimeTicks::Now() - async_start_time); 152 base::TimeTicks::Now() - async_start_time);
109 NotifyCalculationComplete(prominent_color); 153 NotifyCalculationComplete(prominent_colors);
110 } 154 }
111 155
112 void WallpaperColorCalculator::NotifyCalculationComplete( 156 void WallpaperColorCalculator::NotifyCalculationComplete(
113 SkColor prominent_color) { 157 const std::vector<SkColor>& prominent_colors) {
114 prominent_color_ = prominent_color; 158 prominent_colors_ = prominent_colors;
115 for (auto& observer : observers_) 159 for (auto& observer : observers_)
116 observer.OnColorCalculationComplete(); 160 observer.OnColorCalculationComplete();
117 161
118 // This could be deleted! 162 // This could be deleted!
119 } 163 }
120 164
121 } // namespace wallpaper 165 } // namespace wallpaper
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698