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