OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "chrome/browser/icon_loader.h" | 5 #include "chrome/browser/icon_loader.h" |
6 | 6 |
7 #include <map> | 7 #include <map> |
8 #include <string> | 8 #include <string> |
9 #include <utility> | 9 #include <utility> |
10 | 10 |
11 #include "base/bind.h" | 11 #include "base/bind.h" |
12 #include "base/lazy_instance.h" | 12 #include "base/lazy_instance.h" |
13 #include "base/memory/ref_counted_memory.h" | 13 #include "base/memory/ref_counted_memory.h" |
14 #include "base/message_loop.h" | 14 #include "base/message_loop.h" |
15 #include "chrome/browser/icon_loader.h" | 15 #include "chrome/browser/icon_loader.h" |
16 #include "grit/component_extension_resources.h" | 16 #include "grit/component_extension_resources.h" |
17 #include "skia/ext/image_operations.h" | |
18 #include "third_party/skia/include/core/SkBitmap.h" | 17 #include "third_party/skia/include/core/SkBitmap.h" |
19 #include "ui/base/layout.h" | 18 #include "ui/base/layout.h" |
20 #include "ui/base/resource/resource_bundle.h" | 19 #include "ui/base/resource/resource_bundle.h" |
21 #include "ui/gfx/canvas.h" | 20 #include "ui/gfx/canvas.h" |
22 #include "ui/gfx/codec/png_codec.h" | 21 #include "ui/gfx/codec/png_codec.h" |
23 #include "ui/gfx/image/image.h" | 22 #include "ui/gfx/image/image.h" |
| 23 #include "ui/gfx/image/image_skia_operations.h" |
24 #include "webkit/glue/image_decoder.h" | 24 #include "webkit/glue/image_decoder.h" |
25 | 25 |
26 namespace { | 26 namespace { |
27 | 27 |
28 // Used with GenerateBitmapWithSize() to indicate that the image shouldn't be | 28 // Used with GenerateImageWithSize() to indicate that the image shouldn't be |
29 // resized. | 29 // resized. |
30 const int kDoNotResize = -1; | 30 const int kDoNotResize = -1; |
31 | 31 |
32 struct IdrBySize { | 32 struct IdrBySize { |
33 int idr_small; | 33 int idr_small; |
34 int idr_normal; | 34 int idr_normal; |
35 int idr_large; | 35 int idr_large; |
36 }; | 36 }; |
37 | 37 |
38 // Performs mapping of <file extension, icon size> to icon resource IDs. | 38 // Performs mapping of <file extension, icon size> to icon resource IDs. |
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
155 case IconLoader::SMALL: idr = idrbysize.idr_small; break; | 155 case IconLoader::SMALL: idr = idrbysize.idr_small; break; |
156 case IconLoader::NORMAL: idr = idrbysize.idr_normal; break; | 156 case IconLoader::NORMAL: idr = idrbysize.idr_normal; break; |
157 case IconLoader::LARGE: idr = idrbysize.idr_large; break; | 157 case IconLoader::LARGE: idr = idrbysize.idr_large; break; |
158 case IconLoader::ALL: | 158 case IconLoader::ALL: |
159 default: | 159 default: |
160 NOTREACHED(); | 160 NOTREACHED(); |
161 } | 161 } |
162 return idr; | 162 return idr; |
163 } | 163 } |
164 | 164 |
165 // Returns a copy of |source| that is |pixel_size| in width and height. If | 165 // Returns a copy of |source| that is |dip_size| in width and height. If |
166 // |pixel_size| is |kDoNotResize|, returns an unmodified copy of |source|. | 166 // |dip_size| is |kDoNotResize|, returns an unmodified copy of |source|. |
167 // |source| must be a square image (width == height). | 167 // |source| must be a square image (width == height). |
168 SkBitmap GenerateBitmapWithSize(const SkBitmap& source, int pixel_size) { | 168 gfx::ImageSkia ResizeImage(const gfx::ImageSkia& source, int dip_size) { |
169 DCHECK(!source.isNull()); | 169 DCHECK(!source.isNull()); |
170 DCHECK(source.width() == source.height()); | 170 DCHECK(source.width() == source.height()); |
171 | 171 |
172 if (pixel_size == kDoNotResize || source.width() == pixel_size) | 172 if (dip_size == kDoNotResize || source.width() == dip_size) |
173 return source; | 173 return source; |
174 | 174 |
175 return skia::ImageOperations::Resize( | 175 return gfx::ImageSkiaOperations::CreateResizedImage(source, |
176 source, skia::ImageOperations::RESIZE_BEST, pixel_size, pixel_size); | 176 gfx::Size(dip_size, dip_size)); |
177 } | 177 } |
178 | 178 |
179 int IconSizeToPixelSize(IconLoader::IconSize size) { | 179 int IconSizeToDIPSize(IconLoader::IconSize size) { |
180 switch (size) { | 180 switch (size) { |
181 case IconLoader::SMALL: return 16; | 181 case IconLoader::SMALL: return 16; |
182 case IconLoader::NORMAL: return 32; | 182 case IconLoader::NORMAL: return 32; |
183 case IconLoader::LARGE: // fallthrough | 183 case IconLoader::LARGE: // fallthrough |
184 // On ChromeOS, we consider LARGE to mean "the largest image we have." | 184 // On ChromeOS, we consider LARGE to mean "the largest image we have." |
185 // Since we have already chosen the largest applicable image resource, we | 185 // Since we have already chosen the largest applicable image resource, we |
186 // return the image as-is. | 186 // return the image as-is. |
187 case IconLoader::ALL: // fallthrough | 187 case IconLoader::ALL: // fallthrough |
188 default: | 188 default: |
189 return kDoNotResize; | 189 return kDoNotResize; |
190 } | 190 } |
191 } | 191 } |
192 | 192 |
193 } // namespace | 193 } // namespace |
194 | 194 |
195 void IconLoader::ReadIcon() { | 195 void IconLoader::ReadIcon() { |
196 static base::LazyInstance<IconMapper>::Leaky icon_mapper = | 196 static base::LazyInstance<IconMapper>::Leaky icon_mapper = |
197 LAZY_INSTANCE_INITIALIZER; | 197 LAZY_INSTANCE_INITIALIZER; |
198 int idr = icon_mapper.Get().Lookup(group_, icon_size_); | 198 int idr = icon_mapper.Get().Lookup(group_, icon_size_); |
199 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); | 199 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); |
200 scoped_refptr<base::RefCountedStaticMemory> bytes( | 200 const gfx::ImageSkia* image_skia = rb.GetImageNamed(idr).ToImageSkia(); |
201 rb.LoadDataResourceBytes(idr, ui::SCALE_FACTOR_100P)); | |
202 DCHECK(bytes.get()); | |
203 SkBitmap bitmap; | |
204 if (!gfx::PNGCodec::Decode(bytes->front(), bytes->size(), &bitmap)) | |
205 NOTREACHED(); | |
206 image_.reset(new gfx::Image( | 201 image_.reset(new gfx::Image( |
207 GenerateBitmapWithSize(bitmap, IconSizeToPixelSize(icon_size_)))); | 202 ResizeImage(*image_skia, IconSizeToDIPSize(icon_size_)))); |
208 target_message_loop_->PostTask( | 203 target_message_loop_->PostTask( |
209 FROM_HERE, base::Bind(&IconLoader::NotifyDelegate, this)); | 204 FROM_HERE, base::Bind(&IconLoader::NotifyDelegate, this)); |
210 } | 205 } |
OLD | NEW |