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

Unified Diff: components/wallpaper/wallpaper_color_calculator.cc

Issue 2824883006: [ash-md] Reduce thread hopping for cheap wallpaper color extraction. (Closed)
Patch Set: Removed 'BooleanSynchronous' from histograms.xml. 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 side-by-side diff with in-line comments
Download patch
Index: components/wallpaper/wallpaper_color_calculator.cc
diff --git a/components/wallpaper/wallpaper_color_calculator.cc b/components/wallpaper/wallpaper_color_calculator.cc
index fdab3d8cd6d1a568352457a57ee7e16888130ecb..51eeaae764cc976ed20d7241eb811827e1734f91 100644
--- a/components/wallpaper/wallpaper_color_calculator.cc
+++ b/components/wallpaper/wallpaper_color_calculator.cc
@@ -15,6 +15,35 @@
namespace wallpaper {
+namespace {
+
+// The largest image size, in pixels, to synchronously calculate the prominent
+// color.
Evan Stade 2017/04/20 21:37:15 nit: perhaps explain the motivation briefly, e.g.
bruthig 2017/04/21 15:42:08 Done.
+const int kMaxPixelsForSynchronousCalculation = 100;
+
+// Wrapper for color_utils::CalculateProminentColorOfBitmap() that records
+// wallpaper specific metrics.
+SkColor CalculateWallpaperColor(const SkBitmap& bitmap,
+ color_utils::LumaRange luma,
+ color_utils::SaturationRange saturation) {
+ base::TimeTicks start_time = base::TimeTicks::Now();
+ const SkColor prominent_color =
+ color_utils::CalculateProminentColorOfBitmap(bitmap, luma, saturation);
+
+ UMA_HISTOGRAM_TIMES("Ash.Wallpaper.ColorExtraction.Durations",
+ base::TimeTicks::Now() - start_time);
+ UMA_HISTOGRAM_BOOLEAN("Ash.Wallpaper.ColorExtractionResult",
+ prominent_color != SK_ColorTRANSPARENT);
+
+ return prominent_color;
+}
+
+bool ShouldCalculateSync(const gfx::ImageSkia& image) {
+ return image.width() * image.height() <= kMaxPixelsForSynchronousCalculation;
+}
+
+} // namespace
+
WallpaperColorCalculator::WallpaperColorCalculator(
const gfx::ImageSkia& image,
color_utils::LumaRange luma,
@@ -39,15 +68,20 @@ void WallpaperColorCalculator::RemoveObserver(
}
bool WallpaperColorCalculator::StartCalculation() {
- start_calculation_time_ = base::Time::Now();
+ if (ShouldCalculateSync(image_)) {
+ const SkColor prominent_color =
+ CalculateWallpaperColor(*image_.bitmap(), luma_, saturation_);
+ WallpaperColorCalculator::NotifyCalculationComplete(prominent_color);
Evan Stade 2017/04/20 21:37:15 nit: WallpaperColorCalculator:: isn't necessary is
bruthig 2017/04/21 15:42:08 Whoops, removed.
+ return true;
+ }
image_.MakeThreadSafe();
if (base::PostTaskAndReplyWithResult(
task_runner_.get(), FROM_HERE,
- base::Bind(&color_utils::CalculateProminentColorOfBitmap,
- *image_.bitmap(), luma_, saturation_),
- base::Bind(&WallpaperColorCalculator::NotifyCalculationComplete,
- weak_ptr_factory_.GetWeakPtr()))) {
+ base::Bind(&CalculateWallpaperColor, *image_.bitmap(), luma_,
+ saturation_),
+ base::Bind(&WallpaperColorCalculator::OnAsyncCalculationComplete,
+ weak_ptr_factory_.GetWeakPtr(), base::TimeTicks::Now()))) {
return true;
}
@@ -63,14 +97,16 @@ void WallpaperColorCalculator::SetTaskRunnerForTest(
task_runner_ = task_runner;
}
-void WallpaperColorCalculator::NotifyCalculationComplete(
+void WallpaperColorCalculator::OnAsyncCalculationComplete(
+ base::TimeTicks async_start_time,
SkColor prominent_color) {
- UMA_HISTOGRAM_MEDIUM_TIMES("Ash.Wallpaper.TimeSpentExtractingColors",
- base::Time::Now() - start_calculation_time_);
-
- UMA_HISTOGRAM_BOOLEAN("Ash.Wallpaper.ColorExtractionResult",
- prominent_color != SK_ColorTRANSPARENT);
+ UMA_HISTOGRAM_TIMES("Ash.Wallpaper.ColorExtraction.UserDelay",
+ base::TimeTicks::Now() - async_start_time);
+ NotifyCalculationComplete(prominent_color);
+}
+void WallpaperColorCalculator::NotifyCalculationComplete(
+ SkColor prominent_color) {
prominent_color_ = prominent_color;
for (auto& observer : observers_)
observer.OnColorCalculationComplete();

Powered by Google App Engine
This is Rietveld 408576698