diff options
Diffstat (limited to 'ash/desktop_background/wallpaper_resizer.cc')
-rw-r--r-- | ash/desktop_background/wallpaper_resizer.cc | 122 |
1 files changed, 63 insertions, 59 deletions
diff --git a/ash/desktop_background/wallpaper_resizer.cc b/ash/desktop_background/wallpaper_resizer.cc index c7e1dda..b75e262 100644 --- a/ash/desktop_background/wallpaper_resizer.cc +++ b/ash/desktop_background/wallpaper_resizer.cc @@ -18,91 +18,95 @@ using content::BrowserThread; namespace ash { namespace { -// Callback used to indicate that wallpaper has been resized. -typedef base::Callback<void(const SkBitmap&)> ResizedCallback; - // For our scaling ratios we need to round positive numbers. int RoundPositive(double x) { return static_cast<int>(floor(x + 0.5)); } -// Resizes |wallpaper| to |target_size| and calls the callback. -void Resize(const SkBitmap& wallpaper, - WallpaperLayout layout, +// Resizes |orig_bitmap| to |target_size| using |layout| and stores the +// resulting bitmap at |resized_bitmap_out|. +void Resize(SkBitmap orig_bitmap, const gfx::Size& target_size, - base::MessageLoop* origin_loop, - const ResizedCallback& callback) { - SkBitmap resized_wallpaper = wallpaper; - int width = target_size.width(); - int height = target_size.height(); - if (wallpaper.width() > width || wallpaper.height() > height) { - gfx::Rect wallpaper_rect(0, 0, wallpaper.width(), wallpaper.height()); - gfx::Size cropped_size = gfx::Size(std::min(width, wallpaper.width()), - std::min(height, wallpaper.height())); + WallpaperLayout layout, + SkBitmap* resized_bitmap_out) { + DCHECK(BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread()); + SkBitmap new_bitmap = orig_bitmap; + + const int orig_width = orig_bitmap.width(); + const int orig_height = orig_bitmap.height(); + const int new_width = target_size.width(); + const int new_height = target_size.height(); + + if (orig_width > new_width || orig_height > new_height) { + gfx::Rect wallpaper_rect(0, 0, orig_width, orig_height); + gfx::Size cropped_size = gfx::Size(std::min(new_width, orig_width), + std::min(new_height, orig_height)); switch (layout) { case WALLPAPER_LAYOUT_CENTER: wallpaper_rect.ClampToCenteredSize(cropped_size); - wallpaper.extractSubset(&resized_wallpaper, - gfx::RectToSkIRect(wallpaper_rect)); + orig_bitmap.extractSubset(&new_bitmap, + gfx::RectToSkIRect(wallpaper_rect)); break; case WALLPAPER_LAYOUT_TILE: wallpaper_rect.set_size(cropped_size); - wallpaper.extractSubset(&resized_wallpaper, - gfx::RectToSkIRect(wallpaper_rect)); + orig_bitmap.extractSubset(&new_bitmap, + gfx::RectToSkIRect(wallpaper_rect)); break; case WALLPAPER_LAYOUT_STRETCH: - resized_wallpaper = skia::ImageOperations::Resize( - wallpaper, skia::ImageOperations::RESIZE_LANCZOS3, - width, height); + new_bitmap = skia::ImageOperations::Resize( + orig_bitmap, skia::ImageOperations::RESIZE_LANCZOS3, + new_width, new_height); break; case WALLPAPER_LAYOUT_CENTER_CROPPED: - if (wallpaper.width() > width && wallpaper.height() > height) { + if (orig_width > new_width && orig_height > new_height) { // The dimension with the smallest ratio must be cropped, the other // one is preserved. Both are set in gfx::Size cropped_size. - double horizontal_ratio = static_cast<double>(width) / - static_cast<double>(wallpaper.width()); - double vertical_ratio = static_cast<double>(height) / - static_cast<double>(wallpaper.height()); + double horizontal_ratio = static_cast<double>(new_width) / + static_cast<double>(orig_width); + double vertical_ratio = static_cast<double>(new_height) / + static_cast<double>(orig_height); if (vertical_ratio > horizontal_ratio) { cropped_size = gfx::Size( - RoundPositive(static_cast<double>(width) / vertical_ratio), - wallpaper.height()); + RoundPositive(static_cast<double>(new_width) / vertical_ratio), + orig_height); } else { - cropped_size = gfx::Size(wallpaper.width(), - RoundPositive(static_cast<double>(height) / horizontal_ratio)); + cropped_size = gfx::Size(orig_width, RoundPositive( + static_cast<double>(new_height) / horizontal_ratio)); } wallpaper_rect.ClampToCenteredSize(cropped_size); SkBitmap sub_image; - wallpaper.extractSubset(&sub_image, - gfx::RectToSkIRect(wallpaper_rect)); - resized_wallpaper = skia::ImageOperations::Resize( + orig_bitmap.extractSubset(&sub_image, + gfx::RectToSkIRect(wallpaper_rect)); + new_bitmap = skia::ImageOperations::Resize( sub_image, skia::ImageOperations::RESIZE_LANCZOS3, - width, height); + new_width, new_height); } } } - resized_wallpaper.setImmutable(); - origin_loop->PostTask(FROM_HERE, base::Bind(callback, resized_wallpaper)); + + *resized_bitmap_out = new_bitmap; + resized_bitmap_out->setImmutable(); } } // namespace -WallpaperResizer::WallpaperResizer(const WallpaperInfo& info, - const gfx::Size& target_size) - : wallpaper_info_(info), +WallpaperResizer::WallpaperResizer(int image_resource_id, + const gfx::Size& target_size, + WallpaperLayout layout) + : wallpaper_image_(*(ui::ResourceBundle::GetSharedInstance(). + GetImageNamed(image_resource_id).ToImageSkia())), target_size_(target_size), - wallpaper_image_(*(ui::ResourceBundle::GetSharedInstance(). - GetImageNamed(info.idr).ToImageSkia())), + layout_(layout), weak_ptr_factory_(this) { } -WallpaperResizer::WallpaperResizer(const WallpaperInfo& info, +WallpaperResizer::WallpaperResizer(const gfx::ImageSkia& image, const gfx::Size& target_size, - const gfx::ImageSkia& image) - : wallpaper_info_(info), + WallpaperLayout layout) + : wallpaper_image_(image), target_size_(target_size), - wallpaper_image_(image), + layout_(layout), weak_ptr_factory_(this) { } @@ -110,18 +114,17 @@ WallpaperResizer::~WallpaperResizer() { } void WallpaperResizer::StartResize() { - if (!BrowserThread::GetBlockingPool()->PostWorkerTaskWithShutdownBehavior( + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); + SkBitmap* resized_bitmap = new SkBitmap; + if (!content::BrowserThread::PostBlockingPoolTaskAndReply( FROM_HERE, - base::Bind(&Resize, - *wallpaper_image_.bitmap(), - wallpaper_info_.layout, - target_size_, - base::MessageLoop::current(), - base::Bind(&WallpaperResizer::OnResizeFinished, - weak_ptr_factory_.GetWeakPtr())), - base::SequencedWorkerPool::CONTINUE_ON_SHUTDOWN)) { - LOG(WARNING) << "PostSequencedWorkerTask failed. " << - "Wallpaper may not be resized."; + base::Bind(&Resize, *wallpaper_image_.bitmap(), target_size_, + layout_, resized_bitmap), + base::Bind(&WallpaperResizer::OnResizeFinished, + weak_ptr_factory_.GetWeakPtr(), + base::Owned(resized_bitmap)))) { + LOG(WARNING) << "PostSequencedWorkerTask failed. " + << "Wallpaper may not be resized."; } } @@ -133,8 +136,9 @@ void WallpaperResizer::RemoveObserver(WallpaperResizerObserver* observer) { observers_.RemoveObserver(observer); } -void WallpaperResizer::OnResizeFinished(const SkBitmap& resized_wallpaper) { - wallpaper_image_ = gfx::ImageSkia::CreateFrom1xBitmap(resized_wallpaper); +void WallpaperResizer::OnResizeFinished(SkBitmap* resized_bitmap) { + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); + wallpaper_image_ = gfx::ImageSkia::CreateFrom1xBitmap(*resized_bitmap); FOR_EACH_OBSERVER(WallpaperResizerObserver, observers_, OnWallpaperResized()); } |