OLD | NEW |
---|---|
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 "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/location.h" | 8 #include "base/location.h" |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/threading/sequenced_worker_pool.h" | 10 #include "base/task_runner.h" |
11 #include "base/threading/worker_pool.h" | |
12 #include "components/wallpaper/wallpaper_resizer_observer.h" | 11 #include "components/wallpaper/wallpaper_resizer_observer.h" |
13 #include "third_party/skia/include/core/SkImage.h" | 12 #include "third_party/skia/include/core/SkImage.h" |
14 #include "ui/gfx/image/image_skia_rep.h" | 13 #include "ui/gfx/image/image_skia_rep.h" |
15 #include "ui/gfx/skia_util.h" | 14 #include "ui/gfx/skia_util.h" |
16 | 15 |
17 using base::SequencedWorkerPool; | |
18 | |
19 namespace wallpaper { | 16 namespace wallpaper { |
20 namespace { | 17 namespace { |
21 | 18 |
22 // For our scaling ratios we need to round positive numbers. | 19 // For our scaling ratios we need to round positive numbers. |
23 int RoundPositive(double x) { | 20 int RoundPositive(double x) { |
24 return static_cast<int>(floor(x + 0.5)); | 21 return static_cast<int>(floor(x + 0.5)); |
25 } | 22 } |
26 | 23 |
27 // Resizes |orig_bitmap| to |target_size| using |layout| and stores the | 24 // Resizes |orig_bitmap| to |target_size| using |layout| and stores the |
28 // resulting bitmap at |resized_bitmap_out|. | 25 // resulting bitmap at |resized_bitmap_out|. |
29 void Resize(SkBitmap orig_bitmap, | 26 void Resize(SkBitmap orig_bitmap, |
30 const gfx::Size& target_size, | 27 const gfx::Size& target_size, |
31 WallpaperLayout layout, | 28 WallpaperLayout layout, |
32 SkBitmap* resized_bitmap_out, | 29 SkBitmap* resized_bitmap_out, |
33 SequencedWorkerPool* worker_pool) { | 30 base::TaskRunner* task_runner) { |
34 DCHECK(worker_pool->RunsTasksOnCurrentThread()); | 31 DCHECK(task_runner->RunsTasksOnCurrentThread()); |
35 SkBitmap new_bitmap = orig_bitmap; | 32 SkBitmap new_bitmap = orig_bitmap; |
36 | 33 |
37 const int orig_width = orig_bitmap.width(); | 34 const int orig_width = orig_bitmap.width(); |
38 const int orig_height = orig_bitmap.height(); | 35 const int orig_height = orig_bitmap.height(); |
39 const int new_width = target_size.width(); | 36 const int new_width = target_size.width(); |
40 const int new_height = target_size.height(); | 37 const int new_height = target_size.height(); |
41 | 38 |
42 if (orig_width > new_width || orig_height > new_height) { | 39 if (orig_width > new_width || orig_height > new_height) { |
43 gfx::Rect wallpaper_rect(0, 0, orig_width, orig_height); | 40 gfx::Rect wallpaper_rect(0, 0, orig_width, orig_height); |
44 gfx::Size cropped_size = gfx::Size(std::min(new_width, orig_width), | 41 gfx::Size cropped_size = gfx::Size(std::min(new_width, orig_width), |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
100 | 97 |
101 // static | 98 // static |
102 uint32_t WallpaperResizer::GetImageId(const gfx::ImageSkia& image) { | 99 uint32_t WallpaperResizer::GetImageId(const gfx::ImageSkia& image) { |
103 const gfx::ImageSkiaRep& image_rep = image.GetRepresentation(1.0f); | 100 const gfx::ImageSkiaRep& image_rep = image.GetRepresentation(1.0f); |
104 return image_rep.is_null() ? 0 : image_rep.sk_bitmap().getGenerationID(); | 101 return image_rep.is_null() ? 0 : image_rep.sk_bitmap().getGenerationID(); |
105 } | 102 } |
106 | 103 |
107 WallpaperResizer::WallpaperResizer(const gfx::ImageSkia& image, | 104 WallpaperResizer::WallpaperResizer(const gfx::ImageSkia& image, |
108 const gfx::Size& target_size, | 105 const gfx::Size& target_size, |
109 WallpaperLayout layout, | 106 WallpaperLayout layout, |
110 base::SequencedWorkerPool* worker_pool_ptr) | 107 base::TaskRunner* task_runner) |
111 : image_(image), | 108 : image_(image), |
112 original_image_id_(GetImageId(image_)), | 109 original_image_id_(GetImageId(image_)), |
113 target_size_(target_size), | 110 target_size_(target_size), |
114 layout_(layout), | 111 layout_(layout), |
115 worker_pool_(worker_pool_ptr), | 112 task_runner_(task_runner), |
116 weak_ptr_factory_(this) { | 113 weak_ptr_factory_(this) { |
117 image_.MakeThreadSafe(); | 114 image_.MakeThreadSafe(); |
118 } | 115 } |
119 | 116 |
120 WallpaperResizer::~WallpaperResizer() { | 117 WallpaperResizer::~WallpaperResizer() { |
121 } | 118 } |
122 | 119 |
123 void WallpaperResizer::StartResize() { | 120 void WallpaperResizer::StartResize() { |
124 SkBitmap* resized_bitmap = new SkBitmap; | 121 SkBitmap* resized_bitmap = new SkBitmap; |
125 scoped_refptr<SequencedWorkerPool> worker_pool_refptr(worker_pool_); | 122 scoped_refptr<base::TaskRunner> task_runner_refptr(task_runner_); |
James Cook
2016/09/07 21:27:05
I see you haven't changed this, but just for my kn
msw
2016/09/07 22:28:24
I'm also not too familiar with this pattern or the
| |
126 if (!worker_pool_->PostTaskAndReply( | 123 if (!task_runner_->PostTaskAndReply( |
127 FROM_HERE, | 124 FROM_HERE, |
128 base::Bind(&Resize, *image_.bitmap(), target_size_, layout_, | 125 base::Bind(&Resize, *image_.bitmap(), target_size_, layout_, |
129 resized_bitmap, base::RetainedRef(worker_pool_refptr)), | 126 resized_bitmap, base::RetainedRef(task_runner_refptr)), |
130 base::Bind(&WallpaperResizer::OnResizeFinished, | 127 base::Bind(&WallpaperResizer::OnResizeFinished, |
131 weak_ptr_factory_.GetWeakPtr(), | 128 weak_ptr_factory_.GetWeakPtr(), |
132 base::Owned(resized_bitmap)))) { | 129 base::Owned(resized_bitmap)))) { |
133 LOG(WARNING) << "PostSequencedWorkerTask failed. " | 130 LOG(WARNING) << "PostSequencedWorkerTask failed. " |
134 << "Wallpaper may not be resized."; | 131 << "Wallpaper may not be resized."; |
135 } | 132 } |
136 } | 133 } |
137 | 134 |
138 void WallpaperResizer::AddObserver(WallpaperResizerObserver* observer) { | 135 void WallpaperResizer::AddObserver(WallpaperResizerObserver* observer) { |
139 observers_.AddObserver(observer); | 136 observers_.AddObserver(observer); |
140 } | 137 } |
141 | 138 |
142 void WallpaperResizer::RemoveObserver(WallpaperResizerObserver* observer) { | 139 void WallpaperResizer::RemoveObserver(WallpaperResizerObserver* observer) { |
143 observers_.RemoveObserver(observer); | 140 observers_.RemoveObserver(observer); |
144 } | 141 } |
145 | 142 |
146 void WallpaperResizer::OnResizeFinished(SkBitmap* resized_bitmap) { | 143 void WallpaperResizer::OnResizeFinished(SkBitmap* resized_bitmap) { |
147 image_ = gfx::ImageSkia::CreateFrom1xBitmap(*resized_bitmap); | 144 image_ = gfx::ImageSkia::CreateFrom1xBitmap(*resized_bitmap); |
148 FOR_EACH_OBSERVER(WallpaperResizerObserver, observers_, OnWallpaperResized()); | 145 FOR_EACH_OBSERVER(WallpaperResizerObserver, observers_, OnWallpaperResized()); |
149 } | 146 } |
150 | 147 |
151 } // namespace wallpaper | 148 } // namespace wallpaper |
OLD | NEW |