From abe29e2ae1ad6d9bd8a7bfa23b3316a203d39ef2 Mon Sep 17 00:00:00 2001 From: "oshima@chromium.org" Date: Fri, 20 Jun 2014 23:20:53 +0000 Subject: Convert ui::ScaleFactor -> float in favicon/history code First stop to fix favicon for fractional scale factor. I'll convert the rest of ui::ScaleFactor in 38 BUG=376367,381601 R=pkotwicz@chromium.org, sky@chromium.org Review URL: https://codereview.chromium.org/335233003 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@278853 0039d316-1c4b-4281-b951-d872f2087c98 --- components/favicon_base/favicon_util.cc | 82 +++++++++++------------- components/favicon_base/favicon_util.h | 12 ++-- components/favicon_base/select_favicon_frames.cc | 47 ++++++-------- components/favicon_base/select_favicon_frames.h | 26 ++++---- 4 files changed, 75 insertions(+), 92 deletions(-) (limited to 'components/favicon_base') diff --git a/components/favicon_base/favicon_util.cc b/components/favicon_base/favicon_util.cc index 0a3f0a2..bf3d31a 100644 --- a/components/favicon_base/favicon_util.cc +++ b/components/favicon_base/favicon_util.cc @@ -4,11 +4,14 @@ #include "components/favicon_base/favicon_util.h" +#include + #include "components/favicon_base/favicon_types.h" #include "components/favicon_base/select_favicon_frames.h" #include "skia/ext/image_operations.h" #include "third_party/skia/include/core/SkBitmap.h" #include "third_party/skia/include/core/SkCanvas.h" +#include "ui/base/layout.h" #include "ui/gfx/codec/png_codec.h" #include "ui/gfx/favicon_size.h" #include "ui/gfx/image/image_png_rep.h" @@ -23,11 +26,11 @@ namespace favicon_base { namespace { // Creates image reps of DIP size |favicon_size| for the subset of -// |scale_factors| for which the image reps can be created without resizing +// |favicon_scales| for which the image reps can be created without resizing // or decoding the bitmap data. std::vector SelectFaviconFramesFromPNGsWithoutResizing( const std::vector& png_data, - const std::vector& scale_factors, + const std::vector& favicon_scales, int favicon_size) { std::vector png_reps; if (png_data.empty()) @@ -48,14 +51,12 @@ std::vector SelectFaviconFramesFromPNGsWithoutResizing( return png_reps; } - // Cache the scale factor for each pixel size as |scale_factors| may contain - // any of GetFaviconScaleFactors() which may include scale factors not - // supported by the platform. (ui::GetSupportedScaleFactor() cannot be used.) - std::map desired_pixel_sizes; - for (size_t i = 0; i < scale_factors.size(); ++i) { - int pixel_size = - floor(favicon_size * ui::GetScaleForScaleFactor(scale_factors[i])); - desired_pixel_sizes[pixel_size] = scale_factors[i]; + // Build a map which will be used to determine the scale used to + // create a bitmap with given pixel size. + std::map desired_pixel_sizes; + for (size_t i = 0; i < favicon_scales.size(); ++i) { + int pixel_size = 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) { @@ -66,13 +67,12 @@ std::vector SelectFaviconFramesFromPNGsWithoutResizing( if (pixel_size.width() != pixel_size.height()) continue; - std::map::iterator it = + std::map::iterator it = desired_pixel_sizes.find(pixel_size.width()); if (it == desired_pixel_sizes.end()) continue; - png_reps.push_back(gfx::ImagePNGRep( - png_data[i].bitmap_data, ui::GetScaleForScaleFactor(it->second))); + png_reps.push_back(gfx::ImagePNGRep(png_data[i].bitmap_data, it->second)); } return png_reps; @@ -136,31 +136,22 @@ SkBitmap ResizeBitmapByDownsamplingIfPossible( } // namespace -std::vector GetFaviconScaleFactors() { +std::vector GetFaviconScales() { const float kScale1x = 1.0f; - std::vector favicon_scale_factors = + std::vector resource_scale_factors = ui::GetSupportedScaleFactors(); - // The scale factors returned from ui::GetSupportedScaleFactors() are sorted. - // Insert the 1x scale factor such that GetFaviconScaleFactors() is sorted as - // well. - size_t insert_index = favicon_scale_factors.size(); - for (size_t i = 0; i < favicon_scale_factors.size(); ++i) { - float scale = ui::GetScaleForScaleFactor(favicon_scale_factors[i]); - if (scale == kScale1x) { - return favicon_scale_factors; - } else if (scale > kScale1x) { - insert_index = i; - break; - } - } - // TODO(ios): 100p should not be necessary on iOS retina devices. However + // TODO(ios): 1.0f should not be necessary on iOS retina devices. However // the sync service only supports syncing 100p favicons. Until sync supports - // other scales 100p is needed in the list of scale factors to retrieve and + // other scales 100p is needed in the list of scales to retrieve and // store the favicons in both 100p for sync and 200p for display. cr/160503. - favicon_scale_factors.insert(favicon_scale_factors.begin() + insert_index, - ui::SCALE_FACTOR_100P); - return favicon_scale_factors; + std::vector favicon_scales(1, kScale1x); + for (size_t i = 0; i < resource_scale_factors.size(); ++i) { + if (resource_scale_factors[i] != ui::SCALE_FACTOR_100P) + favicon_scales.push_back( + ui::GetScaleForScaleFactor(resource_scale_factors[i])); + } + return favicon_scales; } void SetFaviconColorSpace(gfx::Image* image) { @@ -171,9 +162,9 @@ void SetFaviconColorSpace(gfx::Image* image) { gfx::Image SelectFaviconFramesFromPNGs( const std::vector& png_data, - const std::vector& scale_factors, + const std::vector& favicon_scales, int favicon_size) { - // Create image reps for as many scale factors as possible without resizing + // 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 // common case. @@ -187,25 +178,26 @@ gfx::Image SelectFaviconFramesFromPNGs( // TODO(pkotwicz): Move the decoding off the UI thread. std::vector png_reps = SelectFaviconFramesFromPNGsWithoutResizing( - png_data, scale_factors, favicon_size); + 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); - std::vector scale_factors_to_generate = scale_factors; + std::vector favicon_scales_to_generate = favicon_scales; for (size_t i = 0; i < png_reps.size(); ++i) { - for (int j = static_cast(scale_factors_to_generate.size()) - 1; j >= 0; + for (int j = static_cast(favicon_scales_to_generate.size()) - 1; + j >= 0; --j) { - if (png_reps[i].scale == - ui::GetScaleForScaleFactor(scale_factors_to_generate[j])) { - scale_factors_to_generate.erase(scale_factors_to_generate.begin() + j); + if (png_reps[i].scale == favicon_scales_to_generate[j]) { + favicon_scales_to_generate.erase(favicon_scales_to_generate.begin() + + j); } } } - if (scale_factors_to_generate.empty()) + if (favicon_scales_to_generate.empty()) return gfx::Image(png_reps); std::vector bitmaps; @@ -225,9 +217,9 @@ gfx::Image SelectFaviconFramesFromPNGs( return gfx::Image(); gfx::ImageSkia resized_image_skia; - for (size_t i = 0; i < scale_factors_to_generate.size(); ++i) { - float scale = ui::GetScaleForScaleFactor(scale_factors_to_generate[i]); - int desired_size_in_pixel = ceil(favicon_size * scale); + for (size_t i = 0; i < favicon_scales_to_generate.size(); ++i) { + float scale = favicon_scales_to_generate[i]; + int desired_size_in_pixel = std::ceil(favicon_size * scale); SkBitmap bitmap = ResizeBitmapByDownsamplingIfPossible(bitmaps, desired_size_in_pixel); resized_image_skia.AddRepresentation(gfx::ImageSkiaRep(bitmap, scale)); diff --git a/components/favicon_base/favicon_util.h b/components/favicon_base/favicon_util.h index ee59428..912f06f 100644 --- a/components/favicon_base/favicon_util.h +++ b/components/favicon_base/favicon_util.h @@ -8,7 +8,6 @@ #include #include "components/favicon_base/favicon_types.h" -#include "ui/base/layout.h" namespace gfx { class Image; @@ -16,24 +15,25 @@ class Image; namespace favicon_base { -// Returns the scale factors at which favicons should be fetched. This is +// Returns the scales at which favicons should be fetched. This is // different from ui::GetSupportedScaleFactors() because clients which do // not support 1x should still fetch a favicon for 1x to push to sync. This // guarantees that the clients receiving sync updates pushed by this client // receive a favicon (potentially of the wrong scale factor) and do not show // the default favicon. -std::vector GetFaviconScaleFactors(); +std::vector GetFaviconScales(); // Sets the color space used for converting |image| to an NSImage to the // system colorspace. This makes the favicon look the same in the browser UI // as it does in the renderer. void SetFaviconColorSpace(gfx::Image* image); -// Takes a vector of png-encoded frames, decodes them, and converts them to -// a favicon of size favicon_size (in DIPs) at the desired ui scale factors. +// Takes a vector of PNG-encoded frames, and converts it to a gfx::Image of +// size |favicon_size| in DIPS. The result gfx::Image has a gfx::ImageSkia with +// gfx::ImageSkiaReps for each |favicon_scales|. gfx::Image SelectFaviconFramesFromPNGs( const std::vector& png_data, - const std::vector& scale_factors, + const std::vector& favicon_scales, int favicon_size); } // namspace favicon_base diff --git a/components/favicon_base/select_favicon_frames.cc b/components/favicon_base/select_favicon_frames.cc index 2f67aaa..0315408 100644 --- a/components/favicon_base/select_favicon_frames.cc +++ b/components/favicon_base/select_favicon_frames.cc @@ -88,9 +88,7 @@ size_t GetCandidateIndexWithBestScore( candidate_sizes[i].height() >= desired_size) { score = desired_size / average_edge * 0.01f + 0.15f; } else { - score = std::min(1.0f, average_edge / desired_size) * - 0.01f + - 0.1f; + score = std::min(1.0f, average_edge / desired_size) * 0.01f + 0.1f; } if (candidate_index == std::numeric_limits::max() || @@ -140,8 +138,8 @@ void GetCandidateIndicesWithBestScores( return; } - std::vector::const_iterator zero_size_it = std::find( - desired_sizes.begin(), desired_sizes.end(), 0); + std::vector::const_iterator zero_size_it = + std::find(desired_sizes.begin(), desired_sizes.end(), 0); if (zero_size_it != desired_sizes.end()) { // Just return the biggest image available. SelectionResult result; @@ -159,10 +157,8 @@ void GetCandidateIndicesWithBestScores( float score; SelectionResult result; result.desired_size = desired_sizes[i]; - result.index = GetCandidateIndexWithBestScore(candidate_sizes, - result.desired_size, - &score, - &result.resize_method); + result.index = GetCandidateIndexWithBestScore( + candidate_sizes, result.desired_size, &score, &result.resize_method); results->push_back(result); total_score += score; } @@ -194,20 +190,19 @@ SkBitmap GetResizedBitmap(const SkBitmap& source_bitmap, const float kSelectFaviconFramesInvalidScore = -1.0f; -gfx::ImageSkia SelectFaviconFrames( - const std::vector& bitmaps, - const std::vector& original_sizes, - const std::vector& scale_factors, - int desired_size_in_dip, - float* match_score) { +gfx::ImageSkia SelectFaviconFrames(const std::vector& bitmaps, + const std::vector& original_sizes, + const std::vector& favicon_scales, + int desired_size_in_dip, + float* match_score) { std::vector desired_sizes; std::map scale_map; if (desired_size_in_dip == 0) { desired_sizes.push_back(0); scale_map[0] = 1.0f; } else { - for (size_t i = 0; i < scale_factors.size(); ++i) { - float scale = ui::GetScaleForScaleFactor(scale_factors[i]); + for (size_t i = 0; i < favicon_scales.size(); ++i) { + float scale = favicon_scales[i]; int desired_size = ceil(desired_size_in_dip * scale); desired_sizes.push_back(desired_size); scale_map[desired_size] = scale; @@ -221,12 +216,11 @@ gfx::ImageSkia SelectFaviconFrames( gfx::ImageSkia multi_image; for (size_t i = 0; i < results.size(); ++i) { const SelectionResult& result = results[i]; - SkBitmap resized_bitmap = GetResizedBitmap(bitmaps[result.index], - result.desired_size, - result.resize_method); + SkBitmap resized_bitmap = GetResizedBitmap( + bitmaps[result.index], result.desired_size, result.resize_method); - std::map::const_iterator it = scale_map.find( - result.desired_size); + std::map::const_iterator it = + scale_map.find(result.desired_size); DCHECK(it != scale_map.end()); float scale = it->second; multi_image.AddRepresentation(gfx::ImageSkiaRep(resized_bitmap, scale)); @@ -234,11 +228,10 @@ gfx::ImageSkia SelectFaviconFrames( return multi_image; } -void SelectFaviconFrameIndices( - const std::vector& frame_pixel_sizes, - const std::vector& desired_sizes, - std::vector* best_indices, - float* match_score) { +void SelectFaviconFrameIndices(const std::vector& frame_pixel_sizes, + const std::vector& desired_sizes, + std::vector* best_indices, + float* match_score) { std::vector results; GetCandidateIndicesWithBestScores( frame_pixel_sizes, desired_sizes, match_score, &results); diff --git a/components/favicon_base/select_favicon_frames.h b/components/favicon_base/select_favicon_frames.h index 34ffef3..9c1b80e 100644 --- a/components/favicon_base/select_favicon_frames.h +++ b/components/favicon_base/select_favicon_frames.h @@ -7,7 +7,7 @@ #include -#include "ui/base/layout.h" +#include "base/basictypes.h" class SkBitmap; @@ -22,24 +22,23 @@ extern const float kSelectFaviconFramesInvalidScore; // Takes a list of all bitmaps found in a .ico file, and creates an // ImageSkia that's |desired_size_in_dip| x |desired_size_in_dip| big. This -// function adds a representation at every desired scale factor. +// function adds a representation at every entry in |favicon_scales|. // If |desired_size_in_dip| is 0, the largest bitmap is returned unmodified. // |original_sizes| are the original sizes of the bitmaps. (For instance, // WebContents::DownloadImage() does resampling if it is passed a max size.) // If score is non-NULL, it receives a score between 0 (bad) and 1 (good) // that describes how well |bitmaps| were able to produce an image at -// |desired_size_in_dip| for |scale_factors|. +// |desired_size_in_dip| for |favicon_scales|. // The score is arbitrary, but it's best for exact size matches, // and gets worse the more resampling needs to happen. // If the resampling algorithm is modified, the resampling done in // FaviconUtil::SelectFaviconFramesFromPNGs() should probably be modified too as // it inspired by this method. -gfx::ImageSkia SelectFaviconFrames( - const std::vector& bitmaps, - const std::vector& original_sizes, - const std::vector& scale_factors, - int desired_size_in_dip, - float* score); +gfx::ImageSkia SelectFaviconFrames(const std::vector& bitmaps, + const std::vector& original_sizes, + const std::vector& favicon_scales, + int desired_size_in_dip, + float* score); // Takes a list of the pixel sizes of a favicon's favicon bitmaps and returns // the indices of the best sizes to use to create an ImageSkia with @@ -53,10 +52,9 @@ gfx::ImageSkia SelectFaviconFrames( // that the index of the largest size is requested. // TODO(pkotwicz): Remove callers of this method for which |frame_pixel_sizes| // are the sizes of the favicon bitmaps after they were resized. -void SelectFaviconFrameIndices( - const std::vector& frame_pixel_sizes, - const std::vector& desired_sizes, - std::vector* best_indices, - float* score); +void SelectFaviconFrameIndices(const std::vector& frame_pixel_sizes, + const std::vector& desired_sizes, + std::vector* best_indices, + float* score); #endif // COMPONENTS_FAVICON_BASE_SELECT_FAVICON_FRAMES_H_ -- cgit v1.1