| Index: components/favicon_base/favicon_util.cc
|
| diff --git a/components/favicon_base/favicon_util.cc b/components/favicon_base/favicon_util.cc
|
| index d45196e2a707ae48f5fb4dfa88516966025b2505..31f1da94aa3719f0240a390a5f45823e98f31e5a 100644
|
| --- a/components/favicon_base/favicon_util.cc
|
| +++ b/components/favicon_base/favicon_util.cc
|
| @@ -30,43 +30,35 @@
|
| namespace favicon_base {
|
| namespace {
|
|
|
| +gfx::Image SelectLargestFrame(
|
| + const std::vector<favicon_base::FaviconRawBitmapResult>& png_data) {
|
| + if (png_data.empty())
|
| + return gfx::Image();
|
| +
|
| + int maximum_area = 0;
|
| + scoped_refptr<base::RefCountedMemory> best_candidate;
|
| + for (size_t i = 0; i < png_data.size(); ++i) {
|
| + int area = png_data[i].pixel_size.GetArea();
|
| + if (area > maximum_area) {
|
| + maximum_area = area;
|
| + best_candidate = png_data[i].bitmap_data;
|
| + }
|
| + }
|
| + return gfx::Image({gfx::ImagePNGRep(best_candidate, 1.0f)});
|
| +}
|
| +
|
| // Creates image reps of DIP size |favicon_size| for the subset of
|
| // |favicon_scales| for which the image reps can be created without resizing
|
| // or decoding the bitmap data.
|
| std::vector<gfx::ImagePNGRep> SelectFaviconFramesFromPNGsWithoutResizing(
|
| const std::vector<favicon_base::FaviconRawBitmapResult>& png_data,
|
| - const std::vector<float>& favicon_scales,
|
| - int favicon_size) {
|
| + std::map<int, float> desired_pixel_sizes) {
|
| TRACE_EVENT0("browser",
|
| "FaviconUtil::SelectFaviconFramesFromPNGsWithoutResizing");
|
| std::vector<gfx::ImagePNGRep> png_reps;
|
| if (png_data.empty())
|
| return png_reps;
|
|
|
| - // A |favicon_size| of 0 indicates that the largest frame is desired.
|
| - if (favicon_size == 0) {
|
| - int maximum_area = 0;
|
| - scoped_refptr<base::RefCountedMemory> best_candidate;
|
| - for (size_t i = 0; i < png_data.size(); ++i) {
|
| - int area = png_data[i].pixel_size.GetArea();
|
| - if (area > maximum_area) {
|
| - maximum_area = area;
|
| - best_candidate = png_data[i].bitmap_data;
|
| - }
|
| - }
|
| - png_reps.push_back(gfx::ImagePNGRep(best_candidate, 1.0f));
|
| - return png_reps;
|
| - }
|
| -
|
| - // Build a map which will be used to determine the scale used to
|
| - // create a bitmap with given pixel size.
|
| - std::map<int, float> desired_pixel_sizes;
|
| - for (size_t i = 0; i < favicon_scales.size(); ++i) {
|
| - int pixel_size =
|
| - static_cast<int>(std::ceil(favicon_size * favicon_scales[i]));
|
| - desired_pixel_sizes[pixel_size] = favicon_scales[i];
|
| - }
|
| -
|
| for (size_t i = 0; i < png_data.size(); ++i) {
|
| if (!png_data[i].is_valid())
|
| continue;
|
| @@ -171,6 +163,27 @@ gfx::Image SelectFaviconFramesFromPNGs(
|
| int favicon_size) {
|
| TRACE_EVENT0("browser", "FaviconUtil::SelectFaviconFramesFromPNGs");
|
|
|
| + // A |favicon_size| of 0 indicates that the largest frame is desired.
|
| + if (favicon_size == 0) {
|
| + return SelectLargestFrame(png_data);
|
| + }
|
| +
|
| + // Build a map which will be used to determine the scale used to
|
| + // create a bitmap with given pixel size.
|
| + std::map<int, float> desired_pixel_sizes;
|
| + for (size_t i = 0; i < favicon_scales.size(); ++i) {
|
| + int pixel_size =
|
| + static_cast<int>(std::ceil(favicon_size * favicon_scales[i]));
|
| + desired_pixel_sizes[pixel_size] = favicon_scales[i];
|
| + }
|
| +
|
| + return SelectFaviconFramesForPixelSizesFromPNGs(png_data,
|
| + desired_pixel_sizes);
|
| +}
|
| +
|
| +gfx::Image SelectFaviconFramesForPixelSizesFromPNGs(
|
| + const std::vector<favicon_base::FaviconRawBitmapResult>& png_data,
|
| + const std::map<int, float>& desired_pixel_sizes) {
|
| // Create image reps for as many scales as possible without resizing
|
| // the bitmap data or decoding it. FaviconHandler stores already resized
|
| // favicons into history so no additional resizing should be needed in the
|
| @@ -184,22 +197,12 @@ gfx::Image SelectFaviconFramesFromPNGs(
|
| // significant performance hit if a user has many bookmarks.
|
| // TODO(pkotwicz): Move the decoding off the UI thread.
|
| std::vector<gfx::ImagePNGRep> png_reps =
|
| - SelectFaviconFramesFromPNGsWithoutResizing(
|
| - png_data, favicon_scales, favicon_size);
|
| -
|
| - // SelectFaviconFramesFromPNGsWithoutResizing() should have selected the
|
| - // largest favicon if |favicon_size| == 0.
|
| - if (favicon_size == 0)
|
| - return gfx::Image(png_reps);
|
| + SelectFaviconFramesFromPNGsWithoutResizing(png_data, desired_pixel_sizes);
|
|
|
| - std::vector<float> favicon_scales_to_generate = favicon_scales;
|
| + std::map<int, float> favicon_scales_to_generate = desired_pixel_sizes;
|
| for (size_t i = 0; i < png_reps.size(); ++i) {
|
| - std::vector<float>::iterator iter = std::find(
|
| - favicon_scales_to_generate.begin(),
|
| - favicon_scales_to_generate.end(),
|
| - png_reps[i].scale);
|
| - if (iter != favicon_scales_to_generate.end())
|
| - favicon_scales_to_generate.erase(iter);
|
| + favicon_scales_to_generate.erase(
|
| + favicon_scales_to_generate.find(png_reps[i].Size().width()));
|
| }
|
|
|
| if (favicon_scales_to_generate.empty())
|
| @@ -222,10 +225,9 @@ gfx::Image SelectFaviconFramesFromPNGs(
|
| return gfx::Image();
|
|
|
| gfx::ImageSkia resized_image_skia;
|
| - for (size_t i = 0; i < favicon_scales_to_generate.size(); ++i) {
|
| - float scale = favicon_scales_to_generate[i];
|
| - int desired_size_in_pixel =
|
| - static_cast<int>(std::ceil(favicon_size * scale));
|
| + for (const auto& scale_to_generate : favicon_scales_to_generate) {
|
| + float scale = scale_to_generate.second;
|
| + int desired_size_in_pixel = scale_to_generate.first;
|
| SkBitmap bitmap =
|
| ResizeBitmapByDownsamplingIfPossible(bitmaps, desired_size_in_pixel);
|
| resized_image_skia.AddRepresentation(gfx::ImageSkiaRep(bitmap, scale));
|
|
|