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/favicon/favicon_util.h" | 5 #include "chrome/browser/favicon/favicon_util.h" |
6 | 6 |
7 #include "chrome/browser/history/select_favicon_frames.h" | 7 #include "chrome/browser/history/select_favicon_frames.h" |
8 #include "chrome/common/favicon/favicon_types.h" | 8 #include "chrome/common/favicon/favicon_types.h" |
9 #include "skia/ext/image_operations.h" | 9 #include "skia/ext/image_operations.h" |
10 #include "third_party/skia/include/core/SkBitmap.h" | 10 #include "third_party/skia/include/core/SkBitmap.h" |
(...skipping 25 matching lines...) Expand all Loading... |
36 if (favicon_size == 0) { | 36 if (favicon_size == 0) { |
37 int maximum_area = 0; | 37 int maximum_area = 0; |
38 scoped_refptr<base::RefCountedMemory> best_candidate; | 38 scoped_refptr<base::RefCountedMemory> best_candidate; |
39 for (size_t i = 0; i < png_data.size(); ++i) { | 39 for (size_t i = 0; i < png_data.size(); ++i) { |
40 int area = png_data[i].pixel_size.GetArea(); | 40 int area = png_data[i].pixel_size.GetArea(); |
41 if (area > maximum_area) { | 41 if (area > maximum_area) { |
42 maximum_area = area; | 42 maximum_area = area; |
43 best_candidate = png_data[i].bitmap_data; | 43 best_candidate = png_data[i].bitmap_data; |
44 } | 44 } |
45 } | 45 } |
46 png_reps.push_back(gfx::ImagePNGRep(best_candidate, | 46 png_reps.push_back(gfx::ImagePNGRep(best_candidate, 1.0f)); |
47 ui::SCALE_FACTOR_100P)); | |
48 return png_reps; | 47 return png_reps; |
49 } | 48 } |
50 | 49 |
51 // Cache the scale factor for each pixel size as |scale_factors| may contain | 50 // Cache the scale factor for each pixel size as |scale_factors| may contain |
52 // any of GetFaviconScaleFactors() which may include scale factors not | 51 // any of GetFaviconScaleFactors() which may include scale factors not |
53 // supported by the platform. (ui::GetScaleFactorFromScale() cannot be used.) | 52 // supported by the platform. (ui::GetSupportedScaleFactor() cannot be used.) |
54 std::map<int, ui::ScaleFactor> desired_pixel_sizes; | 53 std::map<int, ui::ScaleFactor> desired_pixel_sizes; |
55 for (size_t i = 0; i < scale_factors.size(); ++i) { | 54 for (size_t i = 0; i < scale_factors.size(); ++i) { |
56 int pixel_size = floor(favicon_size * | 55 int pixel_size = floor(favicon_size * |
57 ui::GetScaleFactorScale(scale_factors[i])); | 56 ui::GetImageScale(scale_factors[i])); |
58 desired_pixel_sizes[pixel_size] = scale_factors[i]; | 57 desired_pixel_sizes[pixel_size] = scale_factors[i]; |
59 } | 58 } |
60 | 59 |
61 for (size_t i = 0; i < png_data.size(); ++i) { | 60 for (size_t i = 0; i < png_data.size(); ++i) { |
62 if (!png_data[i].is_valid()) | 61 if (!png_data[i].is_valid()) |
63 continue; | 62 continue; |
64 | 63 |
65 const gfx::Size& pixel_size = png_data[i].pixel_size; | 64 const gfx::Size& pixel_size = png_data[i].pixel_size; |
66 if (pixel_size.width() != pixel_size.height()) | 65 if (pixel_size.width() != pixel_size.height()) |
67 continue; | 66 continue; |
68 | 67 |
69 std::map<int, ui::ScaleFactor>::iterator it = desired_pixel_sizes.find( | 68 std::map<int, ui::ScaleFactor>::iterator it = desired_pixel_sizes.find( |
70 pixel_size.width()); | 69 pixel_size.width()); |
71 if (it == desired_pixel_sizes.end()) | 70 if (it == desired_pixel_sizes.end()) |
72 continue; | 71 continue; |
73 | 72 |
74 png_reps.push_back(gfx::ImagePNGRep(png_data[i].bitmap_data, it->second)); | 73 png_reps.push_back( |
| 74 gfx::ImagePNGRep(png_data[i].bitmap_data, |
| 75 ui::GetImageScale(it->second))); |
75 } | 76 } |
76 | 77 |
77 return png_reps; | 78 return png_reps; |
78 } | 79 } |
79 | 80 |
80 // Returns a resampled bitmap of | 81 // Returns a resampled bitmap of |
81 // |desired_size_in_pixel| x |desired_size_in_pixel| by resampling the best | 82 // |desired_size_in_pixel| x |desired_size_in_pixel| by resampling the best |
82 // bitmap out of |input_bitmaps|. ResizeBitmapByDownsamplingIfPossible() is | 83 // bitmap out of |input_bitmaps|. ResizeBitmapByDownsamplingIfPossible() is |
83 // similar to SelectFaviconFrames() but it operates on bitmaps which have | 84 // similar to SelectFaviconFrames() but it operates on bitmaps which have |
84 // already been resampled via SelectFaviconFrames(). | 85 // already been resampled via SelectFaviconFrames(). |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
130 return skia::ImageOperations::Resize(best_bitmap, | 131 return skia::ImageOperations::Resize(best_bitmap, |
131 skia::ImageOperations::RESIZE_LANCZOS3, | 132 skia::ImageOperations::RESIZE_LANCZOS3, |
132 desired_size_in_pixel, | 133 desired_size_in_pixel, |
133 desired_size_in_pixel); | 134 desired_size_in_pixel); |
134 } | 135 } |
135 | 136 |
136 } // namespace | 137 } // namespace |
137 | 138 |
138 // static | 139 // static |
139 std::vector<ui::ScaleFactor> FaviconUtil::GetFaviconScaleFactors() { | 140 std::vector<ui::ScaleFactor> FaviconUtil::GetFaviconScaleFactors() { |
140 const float kScale1x = ui::GetScaleFactorScale(ui::SCALE_FACTOR_100P); | 141 const float kScale1x = ui::GetImageScale(ui::SCALE_FACTOR_100P); |
141 std::vector<ui::ScaleFactor> favicon_scale_factors = | 142 std::vector<ui::ScaleFactor> favicon_scale_factors = |
142 ui::GetSupportedScaleFactors(); | 143 ui::GetSupportedScaleFactors(); |
143 | 144 |
144 // The scale factors returned from ui::GetSupportedScaleFactors() are sorted. | 145 // The scale factors returned from ui::GetSupportedScaleFactors() are sorted. |
145 // Insert the 1x scale factor such that GetFaviconScaleFactors() is sorted as | 146 // Insert the 1x scale factor such that GetFaviconScaleFactors() is sorted as |
146 // well. | 147 // well. |
147 size_t insert_index = favicon_scale_factors.size(); | 148 size_t insert_index = favicon_scale_factors.size(); |
148 for (size_t i = 0; i < favicon_scale_factors.size(); ++i) { | 149 for (size_t i = 0; i < favicon_scale_factors.size(); ++i) { |
149 float scale = ui::GetScaleFactorScale(favicon_scale_factors[i]); | 150 float scale = ui::GetImageScale(favicon_scale_factors[i]); |
150 if (scale == kScale1x) { | 151 if (scale == kScale1x) { |
151 return favicon_scale_factors; | 152 return favicon_scale_factors; |
152 } else if (scale > kScale1x) { | 153 } else if (scale > kScale1x) { |
153 insert_index = i; | 154 insert_index = i; |
154 break; | 155 break; |
155 } | 156 } |
156 } | 157 } |
157 // TODO(ios): 100p should not be necessary on iOS retina devices. However | 158 // TODO(ios): 100p should not be necessary on iOS retina devices. However |
158 // the sync service only supports syncing 100p favicons. Until sync supports | 159 // the sync service only supports syncing 100p favicons. Until sync supports |
159 // other scales 100p is needed in the list of scale factors to retrieve and | 160 // other scales 100p is needed in the list of scale factors to retrieve and |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
194 // SelectFaviconFramesFromPNGsWithoutResizing() should have selected the | 195 // SelectFaviconFramesFromPNGsWithoutResizing() should have selected the |
195 // largest favicon if |favicon_size| == 0. | 196 // largest favicon if |favicon_size| == 0. |
196 if (favicon_size == 0) | 197 if (favicon_size == 0) |
197 return gfx::Image(png_reps); | 198 return gfx::Image(png_reps); |
198 | 199 |
199 std::vector<ui::ScaleFactor> scale_factors_to_generate = scale_factors; | 200 std::vector<ui::ScaleFactor> scale_factors_to_generate = scale_factors; |
200 for (size_t i = 0; i < png_reps.size(); ++i) { | 201 for (size_t i = 0; i < png_reps.size(); ++i) { |
201 std::vector<ui::ScaleFactor>::iterator it = std::find( | 202 std::vector<ui::ScaleFactor>::iterator it = std::find( |
202 scale_factors_to_generate.begin(), | 203 scale_factors_to_generate.begin(), |
203 scale_factors_to_generate.end(), | 204 scale_factors_to_generate.end(), |
204 png_reps[i].scale_factor); | 205 ui::GetSupportedScaleFactor(png_reps[i].scale)); |
205 CHECK(it != scale_factors_to_generate.end()); | 206 CHECK(it != scale_factors_to_generate.end()); |
206 scale_factors_to_generate.erase(it); | 207 scale_factors_to_generate.erase(it); |
207 } | 208 } |
208 | 209 |
209 if (scale_factors_to_generate.empty()) | 210 if (scale_factors_to_generate.empty()) |
210 return gfx::Image(png_reps); | 211 return gfx::Image(png_reps); |
211 | 212 |
212 std::vector<SkBitmap> bitmaps; | 213 std::vector<SkBitmap> bitmaps; |
213 for (size_t i = 0; i < png_data.size(); ++i) { | 214 for (size_t i = 0; i < png_data.size(); ++i) { |
214 if (!png_data[i].is_valid()) | 215 if (!png_data[i].is_valid()) |
215 continue; | 216 continue; |
216 | 217 |
217 SkBitmap bitmap; | 218 SkBitmap bitmap; |
218 if (gfx::PNGCodec::Decode(png_data[i].bitmap_data->front(), | 219 if (gfx::PNGCodec::Decode(png_data[i].bitmap_data->front(), |
219 png_data[i].bitmap_data->size(), | 220 png_data[i].bitmap_data->size(), |
220 &bitmap)) { | 221 &bitmap)) { |
221 bitmaps.push_back(bitmap); | 222 bitmaps.push_back(bitmap); |
222 } | 223 } |
223 } | 224 } |
224 | 225 |
225 if (bitmaps.empty()) | 226 if (bitmaps.empty()) |
226 return gfx::Image(); | 227 return gfx::Image(); |
227 | 228 |
228 gfx::ImageSkia resized_image_skia; | 229 gfx::ImageSkia resized_image_skia; |
229 for (size_t i = 0; i < scale_factors_to_generate.size(); ++i) { | 230 for (size_t i = 0; i < scale_factors_to_generate.size(); ++i) { |
230 ui::ScaleFactor scale_factor = scale_factors_to_generate[i]; | 231 ui::ScaleFactor scale_factor = scale_factors_to_generate[i]; |
231 int desired_size_in_pixel = | 232 int desired_size_in_pixel = |
232 ceil(favicon_size * ui::GetScaleFactorScale(scale_factor)); | 233 ceil(favicon_size * ui::GetImageScale(scale_factor)); |
233 SkBitmap bitmap = ResizeBitmapByDownsamplingIfPossible( | 234 SkBitmap bitmap = ResizeBitmapByDownsamplingIfPossible( |
234 bitmaps, desired_size_in_pixel); | 235 bitmaps, desired_size_in_pixel); |
235 resized_image_skia.AddRepresentation( | 236 resized_image_skia.AddRepresentation( |
236 gfx::ImageSkiaRep(bitmap, scale_factor)); | 237 gfx::ImageSkiaRep(bitmap, scale_factor)); |
237 } | 238 } |
238 | 239 |
239 if (png_reps.empty()) | 240 if (png_reps.empty()) |
240 return gfx::Image(resized_image_skia); | 241 return gfx::Image(resized_image_skia); |
241 | 242 |
242 std::vector<gfx::ImageSkiaRep> resized_image_skia_reps = | 243 std::vector<gfx::ImageSkiaRep> resized_image_skia_reps = |
243 resized_image_skia.image_reps(); | 244 resized_image_skia.image_reps(); |
244 for (size_t i = 0; i < resized_image_skia_reps.size(); ++i) { | 245 for (size_t i = 0; i < resized_image_skia_reps.size(); ++i) { |
245 scoped_refptr<base::RefCountedBytes> png_bytes(new base::RefCountedBytes()); | 246 scoped_refptr<base::RefCountedBytes> png_bytes(new base::RefCountedBytes()); |
246 if (gfx::PNGCodec::EncodeBGRASkBitmap( | 247 if (gfx::PNGCodec::EncodeBGRASkBitmap( |
247 resized_image_skia_reps[i].sk_bitmap(), false, &png_bytes->data())) { | 248 resized_image_skia_reps[i].sk_bitmap(), false, &png_bytes->data())) { |
248 png_reps.push_back(gfx::ImagePNGRep(png_bytes, | 249 png_reps.push_back(gfx::ImagePNGRep(png_bytes, |
249 resized_image_skia_reps[i].scale_factor())); | 250 resized_image_skia_reps[i].scale())); |
250 } | 251 } |
251 } | 252 } |
252 | 253 |
253 return gfx::Image(png_reps); | 254 return gfx::Image(png_reps); |
254 } | 255 } |
OLD | NEW |