| 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 24 matching lines...) Expand all Loading... |
| 35 if (favicon_size == 0) { | 35 if (favicon_size == 0) { |
| 36 int maximum_area = 0; | 36 int maximum_area = 0; |
| 37 scoped_refptr<base::RefCountedMemory> best_candidate; | 37 scoped_refptr<base::RefCountedMemory> best_candidate; |
| 38 for (size_t i = 0; i < png_data.size(); ++i) { | 38 for (size_t i = 0; i < png_data.size(); ++i) { |
| 39 int area = png_data[i].pixel_size.GetArea(); | 39 int area = png_data[i].pixel_size.GetArea(); |
| 40 if (area > maximum_area) { | 40 if (area > maximum_area) { |
| 41 maximum_area = area; | 41 maximum_area = area; |
| 42 best_candidate = png_data[i].bitmap_data; | 42 best_candidate = png_data[i].bitmap_data; |
| 43 } | 43 } |
| 44 } | 44 } |
| 45 png_reps.push_back(gfx::ImagePNGRep(best_candidate, | 45 png_reps.push_back(gfx::ImagePNGRep(best_candidate, 1.0f)); |
| 46 ui::SCALE_FACTOR_100P)); | |
| 47 return png_reps; | 46 return png_reps; |
| 48 } | 47 } |
| 49 | 48 |
| 50 // Cache the scale factor for each pixel size as |scale_factors| may contain | 49 // Cache the scale factor for each pixel size as |scale_factors| may contain |
| 51 // any of GetFaviconScaleFactors() which may include scale factors not | 50 // any of GetFaviconScaleFactors() which may include scale factors not |
| 52 // supported by the platform. (ui::GetScaleFactorFromScale() cannot be used.) | 51 // supported by the platform. (ui::GetScaleFactorFromScale() cannot be used.) |
| 53 std::map<int, ui::ScaleFactor> desired_pixel_sizes; | 52 std::map<int, ui::ScaleFactor> desired_pixel_sizes; |
| 54 for (size_t i = 0; i < scale_factors.size(); ++i) { | 53 for (size_t i = 0; i < scale_factors.size(); ++i) { |
| 55 int pixel_size = floor(favicon_size * | 54 int pixel_size = floor(favicon_size * |
| 56 ui::GetScaleFactorScale(scale_factors[i])); | 55 ui::GetScaleFactorScale(scale_factors[i])); |
| 57 desired_pixel_sizes[pixel_size] = scale_factors[i]; | 56 desired_pixel_sizes[pixel_size] = scale_factors[i]; |
| 58 } | 57 } |
| 59 | 58 |
| 60 for (size_t i = 0; i < png_data.size(); ++i) { | 59 for (size_t i = 0; i < png_data.size(); ++i) { |
| 61 if (!png_data[i].is_valid()) | 60 if (!png_data[i].is_valid()) |
| 62 continue; | 61 continue; |
| 63 | 62 |
| 64 const gfx::Size& pixel_size = png_data[i].pixel_size; | 63 const gfx::Size& pixel_size = png_data[i].pixel_size; |
| 65 if (pixel_size.width() != pixel_size.height()) | 64 if (pixel_size.width() != pixel_size.height()) |
| 66 continue; | 65 continue; |
| 67 | 66 |
| 68 std::map<int, ui::ScaleFactor>::iterator it = desired_pixel_sizes.find( | 67 std::map<int, ui::ScaleFactor>::iterator it = desired_pixel_sizes.find( |
| 69 pixel_size.width()); | 68 pixel_size.width()); |
| 70 if (it == desired_pixel_sizes.end()) | 69 if (it == desired_pixel_sizes.end()) |
| 71 continue; | 70 continue; |
| 72 | 71 |
| 73 png_reps.push_back(gfx::ImagePNGRep(png_data[i].bitmap_data, it->second)); | 72 png_reps.push_back( |
| 73 gfx::ImagePNGRep(png_data[i].bitmap_data, |
| 74 ui::GetScaleFactorScale(it->second))); |
| 74 } | 75 } |
| 75 | 76 |
| 76 return png_reps; | 77 return png_reps; |
| 77 } | 78 } |
| 78 | 79 |
| 79 } // namespace | 80 } // namespace |
| 80 | 81 |
| 81 // static | 82 // static |
| 82 std::vector<ui::ScaleFactor> FaviconUtil::GetFaviconScaleFactors() { | 83 std::vector<ui::ScaleFactor> FaviconUtil::GetFaviconScaleFactors() { |
| 83 const float kScale1x = ui::GetScaleFactorScale(ui::SCALE_FACTOR_100P); | 84 const float kScale1x = ui::GetScaleFactorScale(ui::SCALE_FACTOR_100P); |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 137 // SelectFaviconFramesFromPNGsWithoutResizing() should have selected the | 138 // SelectFaviconFramesFromPNGsWithoutResizing() should have selected the |
| 138 // largest favicon if |favicon_size| == 0. | 139 // largest favicon if |favicon_size| == 0. |
| 139 if (favicon_size == 0) | 140 if (favicon_size == 0) |
| 140 return gfx::Image(png_reps); | 141 return gfx::Image(png_reps); |
| 141 | 142 |
| 142 std::vector<ui::ScaleFactor> scale_factors_to_generate = scale_factors; | 143 std::vector<ui::ScaleFactor> scale_factors_to_generate = scale_factors; |
| 143 for (size_t i = 0; i < png_reps.size(); ++i) { | 144 for (size_t i = 0; i < png_reps.size(); ++i) { |
| 144 std::vector<ui::ScaleFactor>::iterator it = std::find( | 145 std::vector<ui::ScaleFactor>::iterator it = std::find( |
| 145 scale_factors_to_generate.begin(), | 146 scale_factors_to_generate.begin(), |
| 146 scale_factors_to_generate.end(), | 147 scale_factors_to_generate.end(), |
| 147 png_reps[i].scale_factor); | 148 ui::GetScaleFactorFromScale(png_reps[i].scale)); |
| 148 CHECK(it != scale_factors_to_generate.end()); | 149 CHECK(it != scale_factors_to_generate.end()); |
| 149 scale_factors_to_generate.erase(it); | 150 scale_factors_to_generate.erase(it); |
| 150 } | 151 } |
| 151 | 152 |
| 152 if (scale_factors_to_generate.empty()) | 153 if (scale_factors_to_generate.empty()) |
| 153 return gfx::Image(png_reps); | 154 return gfx::Image(png_reps); |
| 154 | 155 |
| 155 std::vector<SkBitmap> bitmaps; | 156 std::vector<SkBitmap> bitmaps; |
| 156 for (size_t i = 0; i < png_data.size(); ++i) { | 157 for (size_t i = 0; i < png_data.size(); ++i) { |
| 157 if (!png_data[i].is_valid()) | 158 if (!png_data[i].is_valid()) |
| (...skipping 16 matching lines...) Expand all Loading... |
| 174 if (png_reps.empty()) | 175 if (png_reps.empty()) |
| 175 return gfx::Image(resized_image_skia); | 176 return gfx::Image(resized_image_skia); |
| 176 | 177 |
| 177 std::vector<gfx::ImageSkiaRep> resized_image_skia_reps = | 178 std::vector<gfx::ImageSkiaRep> resized_image_skia_reps = |
| 178 resized_image_skia.image_reps(); | 179 resized_image_skia.image_reps(); |
| 179 for (size_t i = 0; i < resized_image_skia_reps.size(); ++i) { | 180 for (size_t i = 0; i < resized_image_skia_reps.size(); ++i) { |
| 180 scoped_refptr<base::RefCountedBytes> png_bytes(new base::RefCountedBytes()); | 181 scoped_refptr<base::RefCountedBytes> png_bytes(new base::RefCountedBytes()); |
| 181 if (gfx::PNGCodec::EncodeBGRASkBitmap( | 182 if (gfx::PNGCodec::EncodeBGRASkBitmap( |
| 182 resized_image_skia_reps[i].sk_bitmap(), false, &png_bytes->data())) { | 183 resized_image_skia_reps[i].sk_bitmap(), false, &png_bytes->data())) { |
| 183 png_reps.push_back(gfx::ImagePNGRep(png_bytes, | 184 png_reps.push_back(gfx::ImagePNGRep(png_bytes, |
| 184 resized_image_skia_reps[i].scale_factor())); | 185 resized_image_skia_reps[i].scale())); |
| 185 } | 186 } |
| 186 } | 187 } |
| 187 | 188 |
| 188 return gfx::Image(png_reps); | 189 return gfx::Image(png_reps); |
| 189 } | 190 } |
| 190 | 191 |
| 191 // static | 192 // static |
| 192 size_t FaviconUtil::SelectBestFaviconFromBitmaps( | 193 size_t FaviconUtil::SelectBestFaviconFromBitmaps( |
| 193 const std::vector<SkBitmap>& bitmaps, | 194 const std::vector<SkBitmap>& bitmaps, |
| 194 const std::vector<ui::ScaleFactor>& scale_factors, | 195 const std::vector<ui::ScaleFactor>& scale_factors, |
| 195 int desired_size) { | 196 int desired_size) { |
| 196 std::vector<gfx::Size> sizes; | 197 std::vector<gfx::Size> sizes; |
| 197 for (size_t i = 0; i < bitmaps.size(); ++i) | 198 for (size_t i = 0; i < bitmaps.size(); ++i) |
| 198 sizes.push_back(gfx::Size(bitmaps[i].width(), bitmaps[i].height())); | 199 sizes.push_back(gfx::Size(bitmaps[i].width(), bitmaps[i].height())); |
| 199 std::vector<size_t> selected_bitmap_indices; | 200 std::vector<size_t> selected_bitmap_indices; |
| 200 SelectFaviconFrameIndices(sizes, scale_factors, desired_size, | 201 SelectFaviconFrameIndices(sizes, scale_factors, desired_size, |
| 201 &selected_bitmap_indices, NULL); | 202 &selected_bitmap_indices, NULL); |
| 202 DCHECK_EQ(1u, selected_bitmap_indices.size()); | 203 DCHECK_EQ(1u, selected_bitmap_indices.size()); |
| 203 return selected_bitmap_indices[0]; | 204 return selected_bitmap_indices[0]; |
| 204 } | 205 } |
| OLD | NEW |