| 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 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 97 } | 94 } |
| 98 | 95 |
| 99 } // namespace | 96 } // namespace |
| 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( |
| 108 const gfx::Size& target_size, | 105 const gfx::ImageSkia& image, |
| 109 WallpaperLayout layout, | 106 const gfx::Size& target_size, |
| 110 base::SequencedWorkerPool* worker_pool_ptr) | 107 WallpaperLayout layout, |
| 108 const scoped_refptr<base::TaskRunner>& task_runner) |
| 111 : image_(image), | 109 : image_(image), |
| 112 original_image_id_(GetImageId(image_)), | 110 original_image_id_(GetImageId(image_)), |
| 113 target_size_(target_size), | 111 target_size_(target_size), |
| 114 layout_(layout), | 112 layout_(layout), |
| 115 worker_pool_(worker_pool_ptr), | 113 task_runner_(task_runner), |
| 116 weak_ptr_factory_(this) { | 114 weak_ptr_factory_(this) { |
| 117 image_.MakeThreadSafe(); | 115 image_.MakeThreadSafe(); |
| 118 } | 116 } |
| 119 | 117 |
| 120 WallpaperResizer::~WallpaperResizer() { | 118 WallpaperResizer::~WallpaperResizer() { |
| 121 } | 119 } |
| 122 | 120 |
| 123 void WallpaperResizer::StartResize() { | 121 void WallpaperResizer::StartResize() { |
| 124 SkBitmap* resized_bitmap = new SkBitmap; | 122 SkBitmap* resized_bitmap = new SkBitmap; |
| 125 scoped_refptr<SequencedWorkerPool> worker_pool_refptr(worker_pool_); | 123 if (!task_runner_->PostTaskAndReply( |
| 126 if (!worker_pool_->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_)), |
| 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 |