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

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

Issue 2868943003: Fixed the WallpaperResizer to ensure the image isn't destroyed prematurely. (Closed)
Patch Set: Created 3 years, 7 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
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2013 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_resizer.h" 5 #include "components/wallpaper/wallpaper_resizer.h"
6 6
7 #include <utility> 7 #include <utility>
8 8
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/location.h" 10 #include "base/location.h"
11 #include "base/logging.h" 11 #include "base/logging.h"
12 #include "base/metrics/histogram_macros.h" 12 #include "base/metrics/histogram_macros.h"
13 #include "base/task_runner.h" 13 #include "base/task_runner.h"
14 #include "components/wallpaper/wallpaper_resizer_observer.h" 14 #include "components/wallpaper/wallpaper_resizer_observer.h"
15 #include "third_party/skia/include/core/SkImage.h" 15 #include "third_party/skia/include/core/SkImage.h"
16 #include "ui/gfx/image/image_skia_rep.h" 16 #include "ui/gfx/image/image_skia_rep.h"
17 #include "ui/gfx/skia_util.h" 17 #include "ui/gfx/skia_util.h"
18 18
19 namespace wallpaper { 19 namespace wallpaper {
20 namespace { 20 namespace {
21 21
22 // For our scaling ratios we need to round positive numbers. 22 // For our scaling ratios we need to round positive numbers.
23 int RoundPositive(double x) { 23 int RoundPositive(double x) {
24 return static_cast<int>(floor(x + 0.5)); 24 return static_cast<int>(floor(x + 0.5));
25 } 25 }
26 26
27 // Resizes |orig_bitmap| to |target_size| using |layout| and stores the 27 // Resizes |image| to |target_size| using |layout| and stores the
28 // resulting bitmap at |resized_bitmap_out|. 28 // resulting bitmap at |resized_bitmap_out|.
29 void Resize(SkBitmap orig_bitmap, 29 //
30 // NOTE: |image| is intentionally a copy to ensure it exists for the duration of
31 // the function.
32 void Resize(const gfx::ImageSkia image,
30 const gfx::Size& target_size, 33 const gfx::Size& target_size,
31 WallpaperLayout layout, 34 WallpaperLayout layout,
32 SkBitmap* resized_bitmap_out, 35 SkBitmap* resized_bitmap_out,
33 base::TaskRunner* task_runner) { 36 base::TaskRunner* task_runner) {
34 DCHECK(task_runner->RunsTasksOnCurrentThread()); 37 DCHECK(task_runner->RunsTasksOnCurrentThread());
38
39 SkBitmap orig_bitmap = *image.bitmap();
35 SkBitmap new_bitmap = orig_bitmap; 40 SkBitmap new_bitmap = orig_bitmap;
36 41
37 const int orig_width = orig_bitmap.width(); 42 const int orig_width = orig_bitmap.width();
38 const int orig_height = orig_bitmap.height(); 43 const int orig_height = orig_bitmap.height();
39 const int new_width = target_size.width(); 44 const int new_width = target_size.width();
40 const int new_height = target_size.height(); 45 const int new_height = target_size.height();
41 46
42 if (orig_width > new_width || orig_height > new_height) { 47 if (orig_width > new_width || orig_height > new_height) {
43 gfx::Rect wallpaper_rect(0, 0, orig_width, orig_height); 48 gfx::Rect wallpaper_rect(0, 0, orig_width, orig_height);
44 gfx::Size cropped_size = gfx::Size(std::min(new_width, orig_width), 49 gfx::Size cropped_size = gfx::Size(std::min(new_width, orig_width),
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after
120 125
121 WallpaperResizer::~WallpaperResizer() { 126 WallpaperResizer::~WallpaperResizer() {
122 } 127 }
123 128
124 void WallpaperResizer::StartResize() { 129 void WallpaperResizer::StartResize() {
125 start_calculation_time_ = base::TimeTicks::Now(); 130 start_calculation_time_ = base::TimeTicks::Now();
126 131
127 SkBitmap* resized_bitmap = new SkBitmap; 132 SkBitmap* resized_bitmap = new SkBitmap;
128 if (!task_runner_->PostTaskAndReply( 133 if (!task_runner_->PostTaskAndReply(
129 FROM_HERE, 134 FROM_HERE,
130 base::Bind(&Resize, *image_.bitmap(), target_size_, layout_, 135 base::Bind(&Resize, image_, target_size_, layout_, resized_bitmap,
131 resized_bitmap, base::RetainedRef(task_runner_)), 136 base::RetainedRef(task_runner_)),
132 base::Bind(&WallpaperResizer::OnResizeFinished, 137 base::Bind(&WallpaperResizer::OnResizeFinished,
133 weak_ptr_factory_.GetWeakPtr(), 138 weak_ptr_factory_.GetWeakPtr(),
134 base::Owned(resized_bitmap)))) { 139 base::Owned(resized_bitmap)))) {
135 LOG(WARNING) << "PostSequencedWorkerTask failed. " 140 LOG(WARNING) << "PostSequencedWorkerTask failed. "
136 << "Wallpaper may not be resized."; 141 << "Wallpaper may not be resized.";
137 } 142 }
138 } 143 }
139 144
140 void WallpaperResizer::AddObserver(WallpaperResizerObserver* observer) { 145 void WallpaperResizer::AddObserver(WallpaperResizerObserver* observer) {
141 observers_.AddObserver(observer); 146 observers_.AddObserver(observer);
142 } 147 }
143 148
144 void WallpaperResizer::RemoveObserver(WallpaperResizerObserver* observer) { 149 void WallpaperResizer::RemoveObserver(WallpaperResizerObserver* observer) {
145 observers_.RemoveObserver(observer); 150 observers_.RemoveObserver(observer);
146 } 151 }
147 152
148 void WallpaperResizer::OnResizeFinished(SkBitmap* resized_bitmap) { 153 void WallpaperResizer::OnResizeFinished(SkBitmap* resized_bitmap) {
149 image_ = gfx::ImageSkia::CreateFrom1xBitmap(*resized_bitmap); 154 image_ = gfx::ImageSkia::CreateFrom1xBitmap(*resized_bitmap);
150 UMA_HISTOGRAM_TIMES("Ash.Wallpaper.TimeSpentResizing", 155 UMA_HISTOGRAM_TIMES("Ash.Wallpaper.TimeSpentResizing",
151 base::TimeTicks::Now() - start_calculation_time_); 156 base::TimeTicks::Now() - start_calculation_time_);
152 157
153 for (auto& observer : observers_) 158 for (auto& observer : observers_)
154 observer.OnWallpaperResized(); 159 observer.OnWallpaperResized();
155 } 160 }
156 161
157 } // namespace wallpaper 162 } // namespace wallpaper
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698