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 "ash/desktop_background/wallpaper_resizer.h" | 5 #include "ash/desktop_background/wallpaper_resizer.h" |
6 | 6 |
7 #include "ash/desktop_background/wallpaper_resizer_observer.h" | 7 #include "ash/desktop_background/wallpaper_resizer_observer.h" |
8 #include "base/bind.h" | 8 #include "base/bind.h" |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/threading/sequenced_worker_pool.h" | 10 #include "base/threading/sequenced_worker_pool.h" |
11 #include "base/threading/worker_pool.h" | 11 #include "base/threading/worker_pool.h" |
12 #include "content/public/browser/browser_thread.h" | 12 #include "content/public/browser/browser_thread.h" |
13 #include "ui/base/resource/resource_bundle.h" | 13 #include "ui/base/resource/resource_bundle.h" |
14 #include "ui/gfx/skia_util.h" | 14 #include "ui/gfx/skia_util.h" |
15 | 15 |
16 using content::BrowserThread; | 16 using content::BrowserThread; |
17 | 17 |
18 namespace ash { | 18 namespace ash { |
19 namespace { | 19 namespace { |
20 | 20 |
21 // Callback used to indicate that wallpaper has been resized. | |
22 typedef base::Callback<void(const SkBitmap&)> ResizedCallback; | |
23 | |
24 // For our scaling ratios we need to round positive numbers. | 21 // For our scaling ratios we need to round positive numbers. |
25 int RoundPositive(double x) { | 22 int RoundPositive(double x) { |
26 return static_cast<int>(floor(x + 0.5)); | 23 return static_cast<int>(floor(x + 0.5)); |
27 } | 24 } |
28 | 25 |
29 // Resizes |wallpaper| to |target_size| and calls the callback. | 26 // Resizes |orig_bitmap| to |target_size| using |layout| and stores the |
30 void Resize(const SkBitmap& wallpaper, | 27 // resulting bitmap at |resized_bitmap_out|. |
| 28 void Resize(SkBitmap orig_bitmap, |
| 29 const gfx::Size& target_size, |
31 WallpaperLayout layout, | 30 WallpaperLayout layout, |
32 const gfx::Size& target_size, | 31 SkBitmap* resized_bitmap_out) { |
33 base::MessageLoop* origin_loop, | 32 DCHECK(BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread()); |
34 const ResizedCallback& callback) { | 33 SkBitmap new_bitmap = orig_bitmap; |
35 SkBitmap resized_wallpaper = wallpaper; | 34 |
36 int width = target_size.width(); | 35 const int orig_width = orig_bitmap.width(); |
37 int height = target_size.height(); | 36 const int orig_height = orig_bitmap.height(); |
38 if (wallpaper.width() > width || wallpaper.height() > height) { | 37 const int new_width = target_size.width(); |
39 gfx::Rect wallpaper_rect(0, 0, wallpaper.width(), wallpaper.height()); | 38 const int new_height = target_size.height(); |
40 gfx::Size cropped_size = gfx::Size(std::min(width, wallpaper.width()), | 39 |
41 std::min(height, wallpaper.height())); | 40 if (orig_width > new_width || orig_height > new_height) { |
| 41 gfx::Rect wallpaper_rect(0, 0, orig_width, orig_height); |
| 42 gfx::Size cropped_size = gfx::Size(std::min(new_width, orig_width), |
| 43 std::min(new_height, orig_height)); |
42 switch (layout) { | 44 switch (layout) { |
43 case WALLPAPER_LAYOUT_CENTER: | 45 case WALLPAPER_LAYOUT_CENTER: |
44 wallpaper_rect.ClampToCenteredSize(cropped_size); | 46 wallpaper_rect.ClampToCenteredSize(cropped_size); |
45 wallpaper.extractSubset(&resized_wallpaper, | 47 orig_bitmap.extractSubset(&new_bitmap, |
46 gfx::RectToSkIRect(wallpaper_rect)); | 48 gfx::RectToSkIRect(wallpaper_rect)); |
47 break; | 49 break; |
48 case WALLPAPER_LAYOUT_TILE: | 50 case WALLPAPER_LAYOUT_TILE: |
49 wallpaper_rect.set_size(cropped_size); | 51 wallpaper_rect.set_size(cropped_size); |
50 wallpaper.extractSubset(&resized_wallpaper, | 52 orig_bitmap.extractSubset(&new_bitmap, |
51 gfx::RectToSkIRect(wallpaper_rect)); | 53 gfx::RectToSkIRect(wallpaper_rect)); |
52 break; | 54 break; |
53 case WALLPAPER_LAYOUT_STRETCH: | 55 case WALLPAPER_LAYOUT_STRETCH: |
54 resized_wallpaper = skia::ImageOperations::Resize( | 56 new_bitmap = skia::ImageOperations::Resize( |
55 wallpaper, skia::ImageOperations::RESIZE_LANCZOS3, | 57 orig_bitmap, skia::ImageOperations::RESIZE_LANCZOS3, |
56 width, height); | 58 new_width, new_height); |
57 break; | 59 break; |
58 case WALLPAPER_LAYOUT_CENTER_CROPPED: | 60 case WALLPAPER_LAYOUT_CENTER_CROPPED: |
59 if (wallpaper.width() > width && wallpaper.height() > height) { | 61 if (orig_width > new_width && orig_height > new_height) { |
60 // The dimension with the smallest ratio must be cropped, the other | 62 // The dimension with the smallest ratio must be cropped, the other |
61 // one is preserved. Both are set in gfx::Size cropped_size. | 63 // one is preserved. Both are set in gfx::Size cropped_size. |
62 double horizontal_ratio = static_cast<double>(width) / | 64 double horizontal_ratio = static_cast<double>(new_width) / |
63 static_cast<double>(wallpaper.width()); | 65 static_cast<double>(orig_width); |
64 double vertical_ratio = static_cast<double>(height) / | 66 double vertical_ratio = static_cast<double>(new_height) / |
65 static_cast<double>(wallpaper.height()); | 67 static_cast<double>(orig_height); |
66 | 68 |
67 if (vertical_ratio > horizontal_ratio) { | 69 if (vertical_ratio > horizontal_ratio) { |
68 cropped_size = gfx::Size( | 70 cropped_size = gfx::Size( |
69 RoundPositive(static_cast<double>(width) / vertical_ratio), | 71 RoundPositive(static_cast<double>(new_width) / vertical_ratio), |
70 wallpaper.height()); | 72 orig_height); |
71 } else { | 73 } else { |
72 cropped_size = gfx::Size(wallpaper.width(), | 74 cropped_size = gfx::Size(orig_width, RoundPositive( |
73 RoundPositive(static_cast<double>(height) / horizontal_ratio)); | 75 static_cast<double>(new_height) / horizontal_ratio)); |
74 } | 76 } |
75 wallpaper_rect.ClampToCenteredSize(cropped_size); | 77 wallpaper_rect.ClampToCenteredSize(cropped_size); |
76 SkBitmap sub_image; | 78 SkBitmap sub_image; |
77 wallpaper.extractSubset(&sub_image, | 79 orig_bitmap.extractSubset(&sub_image, |
78 gfx::RectToSkIRect(wallpaper_rect)); | 80 gfx::RectToSkIRect(wallpaper_rect)); |
79 resized_wallpaper = skia::ImageOperations::Resize( | 81 new_bitmap = skia::ImageOperations::Resize( |
80 sub_image, skia::ImageOperations::RESIZE_LANCZOS3, | 82 sub_image, skia::ImageOperations::RESIZE_LANCZOS3, |
81 width, height); | 83 new_width, new_height); |
82 } | 84 } |
83 } | 85 } |
84 } | 86 } |
85 resized_wallpaper.setImmutable(); | 87 |
86 origin_loop->PostTask(FROM_HERE, base::Bind(callback, resized_wallpaper)); | 88 *resized_bitmap_out = new_bitmap; |
| 89 resized_bitmap_out->setImmutable(); |
87 } | 90 } |
88 | 91 |
89 } // namespace | 92 } // namespace |
90 | 93 |
91 WallpaperResizer::WallpaperResizer(const WallpaperInfo& info, | 94 WallpaperResizer::WallpaperResizer(int image_resource_id, |
92 const gfx::Size& target_size) | 95 const gfx::Size& target_size, |
93 : wallpaper_info_(info), | 96 WallpaperLayout layout) |
| 97 : wallpaper_image_(*(ui::ResourceBundle::GetSharedInstance(). |
| 98 GetImageNamed(image_resource_id).ToImageSkia())), |
94 target_size_(target_size), | 99 target_size_(target_size), |
95 wallpaper_image_(*(ui::ResourceBundle::GetSharedInstance(). | 100 layout_(layout), |
96 GetImageNamed(info.idr).ToImageSkia())), | |
97 weak_ptr_factory_(this) { | 101 weak_ptr_factory_(this) { |
98 } | 102 } |
99 | 103 |
100 WallpaperResizer::WallpaperResizer(const WallpaperInfo& info, | 104 WallpaperResizer::WallpaperResizer(const gfx::ImageSkia& image, |
101 const gfx::Size& target_size, | 105 const gfx::Size& target_size, |
102 const gfx::ImageSkia& image) | 106 WallpaperLayout layout) |
103 : wallpaper_info_(info), | 107 : wallpaper_image_(image), |
104 target_size_(target_size), | 108 target_size_(target_size), |
105 wallpaper_image_(image), | 109 layout_(layout), |
106 weak_ptr_factory_(this) { | 110 weak_ptr_factory_(this) { |
107 } | 111 } |
108 | 112 |
109 WallpaperResizer::~WallpaperResizer() { | 113 WallpaperResizer::~WallpaperResizer() { |
110 } | 114 } |
111 | 115 |
112 void WallpaperResizer::StartResize() { | 116 void WallpaperResizer::StartResize() { |
113 if (!BrowserThread::GetBlockingPool()->PostWorkerTaskWithShutdownBehavior( | 117 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 118 SkBitmap* resized_bitmap = new SkBitmap; |
| 119 if (!content::BrowserThread::PostBlockingPoolTaskAndReply( |
114 FROM_HERE, | 120 FROM_HERE, |
115 base::Bind(&Resize, | 121 base::Bind(&Resize, *wallpaper_image_.bitmap(), target_size_, |
116 *wallpaper_image_.bitmap(), | 122 layout_, resized_bitmap), |
117 wallpaper_info_.layout, | 123 base::Bind(&WallpaperResizer::OnResizeFinished, |
118 target_size_, | 124 weak_ptr_factory_.GetWeakPtr(), |
119 base::MessageLoop::current(), | 125 base::Owned(resized_bitmap)))) { |
120 base::Bind(&WallpaperResizer::OnResizeFinished, | 126 LOG(WARNING) << "PostSequencedWorkerTask failed. " |
121 weak_ptr_factory_.GetWeakPtr())), | 127 << "Wallpaper may not be resized."; |
122 base::SequencedWorkerPool::CONTINUE_ON_SHUTDOWN)) { | |
123 LOG(WARNING) << "PostSequencedWorkerTask failed. " << | |
124 "Wallpaper may not be resized."; | |
125 } | 128 } |
126 } | 129 } |
127 | 130 |
128 void WallpaperResizer::AddObserver(WallpaperResizerObserver* observer) { | 131 void WallpaperResizer::AddObserver(WallpaperResizerObserver* observer) { |
129 observers_.AddObserver(observer); | 132 observers_.AddObserver(observer); |
130 } | 133 } |
131 | 134 |
132 void WallpaperResizer::RemoveObserver(WallpaperResizerObserver* observer) { | 135 void WallpaperResizer::RemoveObserver(WallpaperResizerObserver* observer) { |
133 observers_.RemoveObserver(observer); | 136 observers_.RemoveObserver(observer); |
134 } | 137 } |
135 | 138 |
136 void WallpaperResizer::OnResizeFinished(const SkBitmap& resized_wallpaper) { | 139 void WallpaperResizer::OnResizeFinished(SkBitmap* resized_bitmap) { |
137 wallpaper_image_ = gfx::ImageSkia::CreateFrom1xBitmap(resized_wallpaper); | 140 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 141 wallpaper_image_ = gfx::ImageSkia::CreateFrom1xBitmap(*resized_bitmap); |
138 FOR_EACH_OBSERVER(WallpaperResizerObserver, observers_, | 142 FOR_EACH_OBSERVER(WallpaperResizerObserver, observers_, |
139 OnWallpaperResized()); | 143 OnWallpaperResized()); |
140 } | 144 } |
141 | 145 |
142 } // namespace ash | 146 } // namespace ash |
OLD | NEW |