diff options
author | oshima@chromium.org <oshima@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-06-20 23:20:53 +0000 |
---|---|---|
committer | oshima@chromium.org <oshima@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-06-20 23:20:53 +0000 |
commit | abe29e2ae1ad6d9bd8a7bfa23b3316a203d39ef2 (patch) | |
tree | a86028978d5e3258dfbf7040de93a4e1fb4be54b | |
parent | 87ab8a7db8754aa193cc3b5aa4f7a77b439f2141 (diff) | |
download | chromium_src-abe29e2ae1ad6d9bd8a7bfa23b3316a203d39ef2.zip chromium_src-abe29e2ae1ad6d9bd8a7bfa23b3316a203d39ef2.tar.gz chromium_src-abe29e2ae1ad6d9bd8a7bfa23b3316a203d39ef2.tar.bz2 |
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
17 files changed, 219 insertions, 241 deletions
diff --git a/chrome/browser/extensions/extension_web_ui.cc b/chrome/browser/extensions/extension_web_ui.cc index 21b2b10..4b395dc 100644 --- a/chrome/browser/extensions/extension_web_ui.cc +++ b/chrome/browser/extensions/extension_web_ui.cc @@ -424,23 +424,22 @@ void ExtensionWebUI::GetFaviconForURL( // resources. Load image reps for all supported scale factors (in addition to // 1x) immediately instead of in an as needed fashion to be consistent with // how favicons are requested for chrome:// and page URLs. - const std::vector<ui::ScaleFactor>& scale_factors = - favicon_base::GetFaviconScaleFactors(); + const std::vector<float>& favicon_scales = favicon_base::GetFaviconScales(); std::vector<extensions::ImageLoader::ImageRepresentation> info_list; - 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 pixel_size = static_cast<int>(gfx::kFaviconSize * scale); extensions::ExtensionResource icon_resource = extensions::IconsInfo::GetIconResource(extension, pixel_size, ExtensionIconSet::MATCH_BIGGER); - info_list.push_back( - extensions::ImageLoader::ImageRepresentation( - icon_resource, - extensions::ImageLoader::ImageRepresentation::ALWAYS_RESIZE, - gfx::Size(pixel_size, pixel_size), - scale_factors[i])); + ui::ScaleFactor resource_scale_factor = ui::GetSupportedScaleFactor(scale); + info_list.push_back(extensions::ImageLoader::ImageRepresentation( + icon_resource, + extensions::ImageLoader::ImageRepresentation::ALWAYS_RESIZE, + gfx::Size(pixel_size, pixel_size), + resource_scale_factor)); } // LoadImagesAsync actually can run callback synchronously. We want to force diff --git a/chrome/browser/favicon/favicon_handler.cc b/chrome/browser/favicon/favicon_handler.cc index be6d9af..dade696 100644 --- a/chrome/browser/favicon/favicon_handler.cc +++ b/chrome/browser/favicon/favicon_handler.cc @@ -7,6 +7,7 @@ #include "build/build_config.h" #include <algorithm> +#include <cmath> #include <vector> #include "base/bind.h" @@ -98,7 +99,7 @@ bool IsValid(const favicon_base::FaviconRawBitmapResult& bitmap_result) { // Returns true if at least one of the bitmaps in |bitmap_results| is expired or // if |bitmap_results| is missing favicons for |desired_size_in_dip| and one of -// the scale factors in favicon_base::GetFaviconScaleFactors(). +// the scale factors in favicon_base::GetFaviconScales(). bool HasExpiredOrIncompleteResult( int desired_size_in_dip, const std::vector<favicon_base::FaviconRawBitmapResult>& bitmap_results) { @@ -115,7 +116,7 @@ bool HasExpiredOrIncompleteResult( // Check if the favicon for at least one of the scale factors is missing. // |bitmap_results| should always be complete for data inserted by // FaviconHandler as the FaviconHandler stores favicons resized to all - // of favicon_base::GetFaviconScaleFactors() into the history backend. + // of favicon_base::GetFaviconScales() into the history backend. // Examples of when |bitmap_results| can be incomplete: // - Favicons inserted into the history backend by sync. // - Favicons for imported bookmarks. @@ -123,11 +124,9 @@ bool HasExpiredOrIncompleteResult( for (size_t i = 0; i < bitmap_results.size(); ++i) favicon_sizes.push_back(bitmap_results[i].pixel_size); - std::vector<ui::ScaleFactor> scale_factors = - favicon_base::GetFaviconScaleFactors(); - for (size_t i = 0; i < scale_factors.size(); ++i) { - int edge_size_in_pixel = floor( - desired_size_in_dip * ui::GetScaleForScaleFactor(scale_factors[i])); + std::vector<float> favicon_scales = favicon_base::GetFaviconScales(); + for (size_t i = 0; i < favicon_scales.size(); ++i) { + int edge_size_in_pixel = std::ceil(desired_size_in_dip * favicon_scales[i]); std::vector<gfx::Size>::iterator it = std::find(favicon_sizes.begin(), favicon_sizes.end(), gfx::Size(edge_size_in_pixel, edge_size_in_pixel)); if (it == favicon_sizes.end()) @@ -317,7 +316,7 @@ void FaviconHandler::SetFaviconOnActivePage(const std::vector< favicon_base::FaviconRawBitmapResult>& favicon_bitmap_results) { gfx::Image resized_image = favicon_base::SelectFaviconFramesFromPNGs( favicon_bitmap_results, - favicon_base::GetFaviconScaleFactors(), + favicon_base::GetFaviconScales(), preferred_icon_size()); // The history service sends back results for a single icon URL, so it does // not matter which result we get the |icon_url| from. @@ -428,11 +427,9 @@ void FaviconHandler::OnDidDownloadFavicon( if (index != -1) image_skia = gfx::ImageSkia(gfx::ImageSkiaRep(bitmaps[index], 1)); } else { - std::vector<ui::ScaleFactor> scale_factors = - favicon_base::GetFaviconScaleFactors(); image_skia = SelectFaviconFrames(bitmaps, original_bitmap_sizes, - scale_factors, + favicon_base::GetFaviconScales(), preferred_icon_size(), &score); } diff --git a/chrome/browser/favicon/favicon_service.cc b/chrome/browser/favicon/favicon_service.cc index 77c5f99..4df4387 100644 --- a/chrome/browser/favicon/favicon_service.cc +++ b/chrome/browser/favicon/favicon_service.cc @@ -59,25 +59,18 @@ base::CancelableTaskTracker::TaskId GetFaviconForChromeURL( tracker->NewTrackedTaskId(&is_canceled_cb); favicon_base::FaviconResultsCallback cancelable_cb = Bind(&CancelOrRunFaviconResultsCallback, is_canceled_cb, callback); - ChromeWebUIControllerFactory::GetInstance()->GetFaviconForURL(profile, - page_url, desired_sizes_in_pixel, cancelable_cb); + ChromeWebUIControllerFactory::GetInstance()->GetFaviconForURL( + profile, page_url, desired_sizes_in_pixel, cancelable_cb); return id; } -// Returns the size in pixel that a favicon should be based on |size_in_dip| -// and |scale_factor|. -int GetSizeInPixel(int size_in_dip, ui::ScaleFactor scale_factor) { - return ceil(size_in_dip * ui::GetScaleForScaleFactor(scale_factor)); -} - // Returns a vector of pixel edge sizes from |size_in_dip| and -// favicon_base::GetFaviconScaleFactors(). +// favicon_base::GetFaviconScales(). std::vector<int> GetPixelSizesForFaviconScales(int size_in_dip) { - std::vector<ui::ScaleFactor> scale_factors = - favicon_base::GetFaviconScaleFactors(); + std::vector<float> scales = favicon_base::GetFaviconScales(); std::vector<int> sizes_in_pixel; - for (size_t i = 0; i < scale_factors.size(); ++i) { - sizes_in_pixel.push_back(GetSizeInPixel(size_in_dip, scale_factors[i])); + for (size_t i = 0; i < scales.size(); ++i) { + sizes_in_pixel.push_back(std::ceil(size_in_dip * scales[i])); } return sizes_in_pixel; } @@ -123,11 +116,11 @@ base::CancelableTaskTracker::TaskId FaviconService::GetRawFavicon( const GURL& icon_url, favicon_base::IconType icon_type, int desired_size_in_dip, - ui::ScaleFactor desired_scale_factor, + float desired_favicon_scale, const favicon_base::FaviconRawBitmapCallback& callback, base::CancelableTaskTracker* tracker) { int desired_size_in_pixel = - GetSizeInPixel(desired_size_in_dip, desired_scale_factor); + std::ceil(desired_size_in_dip * desired_favicon_scale); favicon_base::FaviconResultsCallback callback_runner = Bind(&FaviconService::RunFaviconRawBitmapCallbackWithBitmapResults, base::Unretained(this), @@ -201,11 +194,11 @@ base::CancelableTaskTracker::TaskId FaviconService::GetFaviconImageForPageURL( base::CancelableTaskTracker::TaskId FaviconService::GetRawFaviconForPageURL( const FaviconForPageURLParams& params, - ui::ScaleFactor desired_scale_factor, + float desired_favicon_scale, const favicon_base::FaviconRawBitmapCallback& callback, base::CancelableTaskTracker* tracker) { int desired_size_in_pixel = - GetSizeInPixel(params.desired_size_in_dip, desired_scale_factor); + std::ceil(params.desired_size_in_dip * desired_favicon_scale); std::vector<int> desired_sizes_in_pixel; desired_sizes_in_pixel.push_back(desired_size_in_pixel); return GetFaviconForPageURLImpl( @@ -235,8 +228,11 @@ FaviconService::GetLargestRawFaviconForPageURL( page_url.SchemeIs(extensions::kExtensionScheme)) { std::vector<int> desired_sizes_in_pixel; desired_sizes_in_pixel.push_back(0); - return GetFaviconForChromeURL(profile, page_url, desired_sizes_in_pixel, - favicon_results_callback, tracker); + return GetFaviconForChromeURL(profile, + page_url, + desired_sizes_in_pixel, + favicon_results_callback, + tracker); } if (history_service_) { return history_service_->GetLargestFaviconForURL(page_url, icon_types, @@ -359,8 +355,8 @@ base::CancelableTaskTracker::TaskId FaviconService::GetFaviconForPageURLImpl( base::CancelableTaskTracker* tracker) { if (params.page_url.SchemeIs(content::kChromeUIScheme) || params.page_url.SchemeIs(extensions::kExtensionScheme)) { - return GetFaviconForChromeURL(profile_, params.page_url, - desired_sizes_in_pixel, callback, tracker); + return GetFaviconForChromeURL( + profile_, params.page_url, desired_sizes_in_pixel, callback, tracker); } if (history_service_) { return history_service_->GetFaviconsForURL(params.page_url, @@ -380,7 +376,7 @@ void FaviconService::RunFaviconImageCallbackWithBitmapResults( favicon_base::FaviconImageResult image_result; image_result.image = favicon_base::SelectFaviconFramesFromPNGs( favicon_bitmap_results, - favicon_base::GetFaviconScaleFactors(), + favicon_base::GetFaviconScales(), desired_size_in_dip); favicon_base::SetFaviconColorSpace(&image_result.image); @@ -420,10 +416,10 @@ void FaviconService::RunFaviconRawBitmapCallbackWithBitmapResults( // Convert raw bytes to SkBitmap, resize via SelectFaviconFrames(), then // convert back. - std::vector<ui::ScaleFactor> desired_scale_factors; - desired_scale_factors.push_back(ui::SCALE_FACTOR_100P); + std::vector<float> desired_favicon_scales; + desired_favicon_scales.push_back(1.0f); gfx::Image resized_image = favicon_base::SelectFaviconFramesFromPNGs( - favicon_bitmap_results, desired_scale_factors, desired_size_in_pixel); + favicon_bitmap_results, desired_favicon_scales, desired_size_in_pixel); std::vector<unsigned char> resized_bitmap_data; if (!gfx::PNGCodec::EncodeBGRASkBitmap(resized_image.AsBitmap(), false, diff --git a/chrome/browser/favicon/favicon_service.h b/chrome/browser/favicon/favicon_service.h index 5387b5e..555e042 100644 --- a/chrome/browser/favicon/favicon_service.h +++ b/chrome/browser/favicon/favicon_service.h @@ -14,7 +14,6 @@ #include "components/favicon_base/favicon_callback.h" #include "components/favicon_base/favicon_types.h" #include "components/keyed_service/core/keyed_service.h" -#include "ui/base/layout.h" class GURL; class HistoryService; @@ -51,9 +50,9 @@ class FaviconService : public KeyedService { // The first argument of |callback| is a |const FaviconImageResult&|. Of which // |FaviconImageResult::image| is constructed from the bitmaps for the - // passed in URL and icon types which most which closely match the passed in - // |desired_size_in_dip| at the scale factors supported by the current - // platform (eg MacOS) in addition to 1x. + // passed in URL and icon types which most wich closely match the passed in + // |desired_size_in_dip| at the resource scale factors supported by the + // current platform (eg MacOS) in addition to 1x. // |FaviconImageResult::icon_url| is the favicon that the favicon bitmaps in // |image| originate from. // TODO(pkotwicz): Enable constructing |image| from bitmaps from several @@ -65,7 +64,7 @@ class FaviconService : public KeyedService { // bits have been fetched. |icon_url| is the URL of the icon itself, e.g. // <http://www.google.com/favicon.ico>. // Each of the three methods below differs in the format of the callback and - // the requested scale factors. All of the scale factors supported by the + // the requested scales. All of the resource scale factors supported by the // current platform (eg MacOS) are requested for GetFaviconImage(). base::CancelableTaskTracker::TaskId GetFaviconImage( const GURL& icon_url, @@ -78,16 +77,17 @@ class FaviconService : public KeyedService { const GURL& icon_url, favicon_base::IconType icon_type, int desired_size_in_dip, - ui::ScaleFactor desired_scale_factor, + float desired_favicon_scale, const favicon_base::FaviconRawBitmapCallback& callback, base::CancelableTaskTracker* tracker); // The first argument for |callback| is the set of bitmaps for the passed in // URL and icon types whose pixel sizes best match the passed in - // |desired_size_in_dip| at the scale factors supported by the current - // platform (eg MacOS) in addition to 1x. The vector has at most one result - // for each of the scale factors. There are less entries if a single result - // is the best bitmap to use for several scale factors. + // |desired_size_in_dip| at the resource scale factors supported by the + // current platform (eg MacOS) in addition to 1x. The vector has at most one + // result for each of the resource scale factors. There are less entries if a + // single/ result is the best bitmap to use for several resource scale + // factors. base::CancelableTaskTracker::TaskId GetFavicon( const GURL& icon_url, favicon_base::IconType icon_type, @@ -111,9 +111,9 @@ class FaviconService : public KeyedService { // |icon_types| can only have multiple IconTypes if // |icon_types| == TOUCH_ICON | TOUCH_PRECOMPOSED_ICON. // The favicon bitmaps which most closely match |desired_size_in_dip| - // at the scale factors supported by the current platform (eg MacOS) in - // addition to 1x from the favicons which were just mapped to |page_url| are - // returned. If |desired_size_in_dip| is 0, the largest favicon bitmap is + // at the reosurce scale factors supported by the current platform (eg MacOS) + // in addition to 1x from the favicons which were just mapped to |page_url| + // are returned. If |desired_size_in_dip| is 0, the largest favicon bitmap is // returned. base::CancelableTaskTracker::TaskId UpdateFaviconMappingsAndFetch( const GURL& page_url, @@ -124,13 +124,13 @@ class FaviconService : public KeyedService { base::CancelableTaskTracker* tracker); // Requests the favicons of any of |icon_types| whose pixel sizes most - // closely match |desired_size_in_dip| and desired scale factors for a web + // closely match |desired_size_in_dip| and desired scale for a web // page URL. If |desired_size_in_dip| is 0, the largest favicon for the web // page URL is returned. |callback| is run when the bits have been fetched. // |icon_types| can be any combination of IconType value, but only one icon // will be returned in the priority of TOUCH_PRECOMPOSED_ICON, TOUCH_ICON and // FAVICON. Each of the three methods below differs in the format of the - // callback and the requested scale factors. All of the scale factors + // callback and the requested scales. All of the resource scale factors // supported by the current platform (eg MacOS) are requested for // GetFaviconImageForPageURL(). // Note. |callback| is always run asynchronously. @@ -141,7 +141,7 @@ class FaviconService : public KeyedService { base::CancelableTaskTracker::TaskId GetRawFaviconForPageURL( const FaviconForPageURLParams& params, - ui::ScaleFactor desired_scale_factor, + float desired_favicon_scale, const favicon_base::FaviconRawBitmapCallback& callback, base::CancelableTaskTracker* tracker); diff --git a/chrome/browser/history/select_favicon_frames_unittest.cc b/chrome/browser/history/select_favicon_frames_unittest.cc index f82b17a..543dc21 100644 --- a/chrome/browser/history/select_favicon_frames_unittest.cc +++ b/chrome/browser/history/select_favicon_frames_unittest.cc @@ -7,21 +7,20 @@ #include "testing/gtest/include/gtest/gtest.h" #include "third_party/skia/include/core/SkBitmap.h" #include "third_party/skia/include/core/SkColor.h" -#include "ui/base/layout.h" #include "ui/gfx/image/image_skia.h" using std::vector; namespace { -vector<ui::ScaleFactor> Scale1x() { - return vector<ui::ScaleFactor>(1, ui::SCALE_FACTOR_100P); +vector<float> FaviconScale1x() { + return vector<float>(1, 1.0f); } -vector<ui::ScaleFactor> Scale1x2x() { - vector<ui::ScaleFactor> scales; - scales.push_back(ui::SCALE_FACTOR_100P); - scales.push_back(ui::SCALE_FACTOR_200P); +vector<float> FaviconScale1x2x() { + vector<float> scales; + scales.push_back(1.0f); + scales.push_back(2.0f); return scales; } @@ -41,10 +40,10 @@ SkBitmap MakeBitmap(SkColor color, int w, int h) { return bitmap; } -SkColor GetColor(const gfx::ImageSkia& image, ui::ScaleFactor factor, +SkColor GetColor(const gfx::ImageSkia& image, float scale, int x = -1, int y = -1) { const SkBitmap& bitmap = - image.GetRepresentation(ui::GetScaleForScaleFactor(factor)).sk_bitmap(); + image.GetRepresentation(scale).sk_bitmap(); if (x == -1) x = bitmap.width() / 2; if (y == -1) @@ -56,11 +55,11 @@ SkColor GetColor(const gfx::ImageSkia& image, ui::ScaleFactor factor, } SkColor GetColor1x(const gfx::ImageSkia& image) { - return GetColor(image, ui::SCALE_FACTOR_100P); + return GetColor(image, 1.0f); } SkColor GetColor2x(const gfx::ImageSkia& image) { - return GetColor(image, ui::SCALE_FACTOR_200P); + return GetColor(image, 2.0f); } TEST(SelectFaviconFramesTest, ZeroSizePicksLargest) { @@ -70,7 +69,7 @@ TEST(SelectFaviconFramesTest, ZeroSizePicksLargest) { bitmaps.push_back(MakeBitmap(SK_ColorBLUE, 32, 32)); gfx::ImageSkia image = SelectFaviconFrames(bitmaps, - SizesFromBitmaps(bitmaps), Scale1x(), 0, NULL); + SizesFromBitmaps(bitmaps), FaviconScale1x(), 0, NULL); EXPECT_EQ(1u, image.image_reps().size()); ASSERT_TRUE(image.HasRepresentation(1.0f)); EXPECT_EQ(48, image.width()); @@ -86,7 +85,7 @@ TEST(SelectFaviconFramesTest, _16From16) { bitmaps.push_back(MakeBitmap(SK_ColorBLUE, 17, 17)); gfx::ImageSkia image = SelectFaviconFrames(bitmaps, - SizesFromBitmaps(bitmaps), Scale1x(), 16, NULL); + SizesFromBitmaps(bitmaps), FaviconScale1x(), 16, NULL); EXPECT_EQ(1u, image.image_reps().size()); ASSERT_TRUE(image.HasRepresentation(1.0f)); EXPECT_EQ(16, image.width()); @@ -101,7 +100,7 @@ TEST(SelectFaviconFramesTest, _16From17) { // Should resample from the bigger candidate. gfx::ImageSkia image = SelectFaviconFrames(bitmaps, - SizesFromBitmaps(bitmaps), Scale1x(), 16, NULL); + SizesFromBitmaps(bitmaps), FaviconScale1x(), 16, NULL); EXPECT_EQ(1u, image.image_reps().size()); ASSERT_TRUE(image.HasRepresentation(1.0f)); EXPECT_EQ(16, image.width()); @@ -117,7 +116,7 @@ TEST(SelectFaviconFramesTest, _16From15) { // If nothing else is available, should resample from the next smaller // candidate. gfx::ImageSkia image = SelectFaviconFrames(bitmaps, - SizesFromBitmaps(bitmaps), Scale1x(), 16, NULL); + SizesFromBitmaps(bitmaps), FaviconScale1x(), 16, NULL); EXPECT_EQ(1u, image.image_reps().size()); ASSERT_TRUE(image.HasRepresentation(1.0f)); EXPECT_EQ(16, image.width()); @@ -130,7 +129,7 @@ TEST(SelectFaviconFramesTest, _16From16_Scale2x_32_From_16) { bitmaps.push_back(MakeBitmap(SK_ColorGREEN, 16, 16)); gfx::ImageSkia image = SelectFaviconFrames(bitmaps, - SizesFromBitmaps(bitmaps), Scale1x2x(), 16, NULL); + SizesFromBitmaps(bitmaps), FaviconScale1x2x(), 16, NULL); EXPECT_EQ(2u, image.image_reps().size()); ASSERT_TRUE(image.HasRepresentation(1.0f)); ASSERT_TRUE(image.HasRepresentation(2.0f)); @@ -146,7 +145,7 @@ TEST(SelectFaviconFramesTest, _16From16_Scale2x_32_From_32) { bitmaps.push_back(MakeBitmap(SK_ColorBLUE, 32, 32)); gfx::ImageSkia image = SelectFaviconFrames(bitmaps, - SizesFromBitmaps(bitmaps), Scale1x2x(), 16, NULL); + SizesFromBitmaps(bitmaps), FaviconScale1x2x(), 16, NULL); EXPECT_EQ(2u, image.image_reps().size()); ASSERT_TRUE(image.HasRepresentation(1.0f)); ASSERT_TRUE(image.HasRepresentation(2.0f)); @@ -161,14 +160,14 @@ TEST(SelectFaviconFramesTest, ExactMatchBetterThanLargeBitmap) { vector<SkBitmap> bitmaps1; bitmaps1.push_back(MakeBitmap(SK_ColorGREEN, 48, 48)); SelectFaviconFrames(bitmaps1, - SizesFromBitmaps(bitmaps1), Scale1x2x(), 16, &score1); + SizesFromBitmaps(bitmaps1), FaviconScale1x2x(), 16, &score1); float score2; vector<SkBitmap> bitmaps2; bitmaps2.push_back(MakeBitmap(SK_ColorGREEN, 16, 16)); bitmaps2.push_back(MakeBitmap(SK_ColorGREEN, 32, 32)); SelectFaviconFrames(bitmaps2, - SizesFromBitmaps(bitmaps2), Scale1x2x(), 16, &score2); + SizesFromBitmaps(bitmaps2), FaviconScale1x2x(), 16, &score2); EXPECT_GT(score2, score1); } @@ -178,25 +177,25 @@ TEST(SelectFaviconFramesTest, UpsampleABitBetterThanHugeBitmap) { vector<SkBitmap> bitmaps1; bitmaps1.push_back(MakeBitmap(SK_ColorGREEN, 128, 128)); SelectFaviconFrames(bitmaps1, - SizesFromBitmaps(bitmaps1), Scale1x2x(), 16, &score1); + SizesFromBitmaps(bitmaps1), FaviconScale1x2x(), 16, &score1); float score2; vector<SkBitmap> bitmaps2; bitmaps2.push_back(MakeBitmap(SK_ColorGREEN, 24, 24)); SelectFaviconFrames(bitmaps2, - SizesFromBitmaps(bitmaps2), Scale1x2x(), 16, &score2); + SizesFromBitmaps(bitmaps2), FaviconScale1x2x(), 16, &score2); float score3; vector<SkBitmap> bitmaps3; bitmaps3.push_back(MakeBitmap(SK_ColorGREEN, 16, 16)); SelectFaviconFrames(bitmaps3, - SizesFromBitmaps(bitmaps3), Scale1x2x(), 16, &score3); + SizesFromBitmaps(bitmaps3), FaviconScale1x2x(), 16, &score3); float score4; vector<SkBitmap> bitmaps4; bitmaps4.push_back(MakeBitmap(SK_ColorGREEN, 15, 15)); SelectFaviconFrames(bitmaps4, - SizesFromBitmaps(bitmaps4), Scale1x2x(), 16, &score4); + SizesFromBitmaps(bitmaps4), FaviconScale1x2x(), 16, &score4); EXPECT_GT(score2, score1); EXPECT_GT(score3, score1); @@ -208,13 +207,13 @@ TEST(SelectFaviconFramesTest, DownsamplingBetterThanUpsampling) { vector<SkBitmap> bitmaps1; bitmaps1.push_back(MakeBitmap(SK_ColorGREEN, 8, 8)); SelectFaviconFrames(bitmaps1, - SizesFromBitmaps(bitmaps1), Scale1x(), 16, &score1); + SizesFromBitmaps(bitmaps1), FaviconScale1x(), 16, &score1); float score2; vector<SkBitmap> bitmaps2; bitmaps2.push_back(MakeBitmap(SK_ColorGREEN, 24, 24)); SelectFaviconFrames(bitmaps2, - SizesFromBitmaps(bitmaps2), Scale1x(), 16, &score2); + SizesFromBitmaps(bitmaps2), FaviconScale1x(), 16, &score2); EXPECT_GT(score2, score1); } @@ -224,13 +223,13 @@ TEST(SelectFaviconFramesTest, DownsamplingLessIsBetter) { vector<SkBitmap> bitmaps1; bitmaps1.push_back(MakeBitmap(SK_ColorGREEN, 34, 34)); SelectFaviconFrames(bitmaps1, - SizesFromBitmaps(bitmaps1), Scale1x2x(), 16, &score1); + SizesFromBitmaps(bitmaps1), FaviconScale1x2x(), 16, &score1); float score2; vector<SkBitmap> bitmaps2; bitmaps2.push_back(MakeBitmap(SK_ColorGREEN, 33, 33)); SelectFaviconFrames(bitmaps2, - SizesFromBitmaps(bitmaps2), Scale1x2x(), 16, &score2); + SizesFromBitmaps(bitmaps2), FaviconScale1x2x(), 16, &score2); EXPECT_GT(score2, score1); } @@ -240,13 +239,13 @@ TEST(SelectFaviconFramesTest, UpsamplingLessIsBetter) { vector<SkBitmap> bitmaps1; bitmaps1.push_back(MakeBitmap(SK_ColorGREEN, 8, 8)); SelectFaviconFrames(bitmaps1, - SizesFromBitmaps(bitmaps1), Scale1x2x(), 16, &score1); + SizesFromBitmaps(bitmaps1), FaviconScale1x2x(), 16, &score1); float score2; vector<SkBitmap> bitmaps2; bitmaps2.push_back(MakeBitmap(SK_ColorGREEN, 9, 9)); SelectFaviconFrames(bitmaps2, - SizesFromBitmaps(bitmaps2), Scale1x2x(), 16, &score2); + SizesFromBitmaps(bitmaps2), FaviconScale1x2x(), 16, &score2); EXPECT_GT(score2, score1); } @@ -259,14 +258,14 @@ TEST(SelectFaviconFramesTest, ScoreDeterminedByOriginalSizes) { vector<gfx::Size> sizes1; sizes1.push_back(gfx::Size(256, 256)); float score1; - SelectFaviconFrames(bitmaps1, sizes1, Scale1x(), 16, &score1); + SelectFaviconFrames(bitmaps1, sizes1, FaviconScale1x(), 16, &score1); vector<SkBitmap> bitmaps2; bitmaps2.push_back(MakeBitmap(SK_ColorGREEN, 15, 15)); vector<gfx::Size> sizes2; sizes2.push_back(gfx::Size(15, 15)); float score2; - SelectFaviconFrames(bitmaps2, sizes2, Scale1x(), 16, &score2); + SelectFaviconFrames(bitmaps2, sizes2, FaviconScale1x(), 16, &score2); EXPECT_GT(score2, score1); } diff --git a/chrome/browser/sync/test/integration/bookmarks_helper.cc b/chrome/browser/sync/test/integration/bookmarks_helper.cc index a05d296..efac13f 100644 --- a/chrome/browser/sync/test/integration/bookmarks_helper.cc +++ b/chrome/browser/sync/test/integration/bookmarks_helper.cc @@ -768,11 +768,10 @@ int CountFoldersWithTitlesMatching(int profile, const std::string& title) { gfx::Image CreateFavicon(SkColor color) { const int dip_width = 16; const int dip_height = 16; - std::vector<ui::ScaleFactor> favicon_scale_factors = - favicon_base::GetFaviconScaleFactors(); + std::vector<float> favicon_scales = favicon_base::GetFaviconScales(); gfx::ImageSkia favicon; - for (size_t i = 0; i < favicon_scale_factors.size(); ++i) { - float scale = ui::GetScaleForScaleFactor(favicon_scale_factors[i]); + for (size_t i = 0; i < favicon_scales.size(); ++i) { + float scale = favicon_scales[i]; int pixel_width = dip_width * scale; int pixel_height = dip_height * scale; SkBitmap bmp; diff --git a/chrome/browser/ui/webui/chrome_web_ui_controller_factory.cc b/chrome/browser/ui/webui/chrome_web_ui_controller_factory.cc index 4c0ca3b..4eb470f 100644 --- a/chrome/browser/ui/webui/chrome_web_ui_controller_factory.cc +++ b/chrome/browser/ui/webui/chrome_web_ui_controller_factory.cc @@ -596,27 +596,32 @@ void ChromeWebUIControllerFactory::GetFaviconForURL( std::vector<favicon_base::FaviconRawBitmapResult>* favicon_bitmap_results = new std::vector<favicon_base::FaviconRawBitmapResult>(); - // Assume that GetFaviconResourceBytes() returns favicons which are - // |gfx::kFaviconSize| x |gfx::kFaviconSize| DIP. - std::vector<ui::ScaleFactor> scale_factors = - favicon_base::GetFaviconScaleFactors(); + // Use ui::GetSupportedScaleFactors instead of + // favicon_base::GetFaviconScales() because chrome favicons comes from + // resources. + std::vector<ui::ScaleFactor> resource_scale_factors = + ui::GetSupportedScaleFactors(); + std::vector<gfx::Size> candidate_sizes; - for (size_t i = 0; i < scale_factors.size(); ++i) { - float scale = ui::GetScaleForScaleFactor(scale_factors[i]); + for (size_t i = 0; i < resource_scale_factors.size(); ++i) { + float scale = ui::GetScaleForScaleFactor(resource_scale_factors[i]); + // Assume that GetFaviconResourceBytes() returns favicons which are + // |gfx::kFaviconSize| x |gfx::kFaviconSize| DIP. int candidate_edge_size = - static_cast<int>(gfx::kFaviconSize * scale + 0.5f); + static_cast<int>(gfx::kFaviconSize * scale + 0.5f); candidate_sizes.push_back( gfx::Size(candidate_edge_size, candidate_edge_size)); } std::vector<size_t> selected_indices; - SelectFaviconFrameIndices(candidate_sizes, - desired_sizes_in_pixel, - &selected_indices, - NULL); + SelectFaviconFrameIndices( + candidate_sizes, desired_sizes_in_pixel, &selected_indices, NULL); for (size_t i = 0; i < selected_indices.size(); ++i) { size_t selected_index = selected_indices[i]; - scoped_refptr<base::RefCountedMemory> bitmap(GetFaviconResourceBytes( - url, scale_factors[selected_index])); + ui::ScaleFactor selected_resource_scale = + resource_scale_factors[selected_index]; + + scoped_refptr<base::RefCountedMemory> bitmap( + GetFaviconResourceBytes(url, selected_resource_scale)); if (bitmap.get() && bitmap->size()) { favicon_base::FaviconRawBitmapResult bitmap_result; bitmap_result.bitmap_data = bitmap; diff --git a/chrome/browser/ui/webui/favicon_source.cc b/chrome/browser/ui/webui/favicon_source.cc index c56e7e1..2b322eb 100644 --- a/chrome/browser/ui/webui/favicon_source.cc +++ b/chrome/browser/ui/webui/favicon_source.cc @@ -25,19 +25,18 @@ #include "ui/base/webui/web_ui_util.h" FaviconSource::IconRequest::IconRequest() - : size_in_dip(gfx::kFaviconSize), - scale_factor(ui::SCALE_FACTOR_NONE) { + : size_in_dip(gfx::kFaviconSize), device_scale_factor(1.0f) { } FaviconSource::IconRequest::IconRequest( const content::URLDataSource::GotDataCallback& cb, const GURL& path, int size, - ui::ScaleFactor scale) + float scale) : callback(cb), request_path(path), size_in_dip(size), - scale_factor(scale) { + device_scale_factor(scale) { } FaviconSource::IconRequest::~IconRequest() { @@ -79,8 +78,6 @@ void FaviconSource::StartDataRequest( GURL url(parsed.url); - ui::ScaleFactor scale_factor = - ui::GetSupportedScaleFactor(parsed.scale_factor); if (parsed.is_icon_url) { // TODO(michaelbai): Change GetRawFavicon to support combination of // IconType. @@ -88,21 +85,24 @@ void FaviconSource::StartDataRequest( url, favicon_base::FAVICON, parsed.size_in_dip, - scale_factor, - base::Bind(&FaviconSource::OnFaviconDataAvailable, - base::Unretained(this), - IconRequest( - callback, url, parsed.size_in_dip, scale_factor)), + parsed.device_scale_factor, + base::Bind( + &FaviconSource::OnFaviconDataAvailable, + base::Unretained(this), + IconRequest( + callback, url, parsed.size_in_dip, parsed.device_scale_factor)), &cancelable_task_tracker_); } else { // Intercept requests for prepopulated pages. for (size_t i = 0; i < arraysize(history::kPrepopulatedPages); i++) { if (url.spec() == l10n_util::GetStringUTF8(history::kPrepopulatedPages[i].url_id)) { + ui::ScaleFactor resource_scale_factor = + ui::GetSupportedScaleFactor(parsed.device_scale_factor); callback.Run( ResourceBundle::GetSharedInstance().LoadDataResourceBytesForScale( history::kPrepopulatedPages[i].favicon_id, - scale_factor)); + resource_scale_factor)); return; } } @@ -110,11 +110,12 @@ void FaviconSource::StartDataRequest( favicon_service->GetRawFaviconForPageURL( FaviconService::FaviconForPageURLParams( url, icon_types_, parsed.size_in_dip), - scale_factor, + parsed.device_scale_factor, base::Bind( &FaviconSource::OnFaviconDataAvailable, base::Unretained(this), - IconRequest(callback, url, parsed.size_in_dip, scale_factor)), + IconRequest( + callback, url, parsed.size_in_dip, parsed.device_scale_factor)), &cancelable_task_tracker_); } } @@ -167,8 +168,7 @@ void FaviconSource::OnFaviconDataAvailable( void FaviconSource::SendDefaultResponse( const content::URLDataSource::GotDataCallback& callback) { - SendDefaultResponse( - IconRequest(callback, GURL(), 16, ui::SCALE_FACTOR_100P)); + SendDefaultResponse(IconRequest(callback, GURL(), 16, 1.0f)); } void FaviconSource::SendDefaultResponse(const IconRequest& icon_request) { @@ -192,9 +192,11 @@ void FaviconSource::SendDefaultResponse(const IconRequest& icon_request) { default_favicons_[favicon_index].get(); if (!default_favicon) { - default_favicon = ResourceBundle::GetSharedInstance() - .LoadDataResourceBytesForScale(resource_id, - icon_request.scale_factor); + ui::ScaleFactor resource_scale_factor = + ui::GetSupportedScaleFactor(icon_request.device_scale_factor); + default_favicon = + ResourceBundle::GetSharedInstance().LoadDataResourceBytesForScale( + resource_id, resource_scale_factor); default_favicons_[favicon_index] = default_favicon; } diff --git a/chrome/browser/ui/webui/favicon_source.h b/chrome/browser/ui/webui/favicon_source.h index a4014b7..829ac01 100644 --- a/chrome/browser/ui/webui/favicon_source.h +++ b/chrome/browser/ui/webui/favicon_source.h @@ -88,13 +88,13 @@ class FaviconSource : public content::URLDataSource { IconRequest(const content::URLDataSource::GotDataCallback& cb, const GURL& path, int size, - ui::ScaleFactor scale); + float scale); ~IconRequest(); content::URLDataSource::GotDataCallback callback; GURL request_path; int size_in_dip; - ui::ScaleFactor scale_factor; + float device_scale_factor; }; // Called when the favicon data is missing to perform additional checks to diff --git a/chrome/browser/web_applications/update_shortcut_worker_win.cc b/chrome/browser/web_applications/update_shortcut_worker_win.cc index e447f4b..995ed66 100644 --- a/chrome/browser/web_applications/update_shortcut_worker_win.cc +++ b/chrome/browser/web_applications/update_shortcut_worker_win.cc @@ -106,10 +106,8 @@ void UpdateShortcutWorker::DidDownloadFavicon( requested_sizes_in_pixel.push_back(requested_size); std::vector<size_t> closest_indices; - SelectFaviconFrameIndices(original_sizes, - requested_sizes_in_pixel, - &closest_indices, - NULL); + SelectFaviconFrameIndices( + original_sizes, requested_sizes_in_pixel, &closest_indices, NULL); SkBitmap bitmap; if (!bitmaps.empty()) { diff --git a/chrome/common/favicon/favicon_url_parser.cc b/chrome/common/favicon/favicon_url_parser.cc index 954bad8..434642d7 100644 --- a/chrome/common/favicon/favicon_url_parser.cc +++ b/chrome/common/favicon/favicon_url_parser.cc @@ -38,7 +38,7 @@ bool ParseFaviconPath(const std::string& path, parsed->is_icon_url = false; parsed->url = ""; parsed->size_in_dip = gfx::kFaviconSize; - parsed->scale_factor = 1.0f; + parsed->device_scale_factor = 1.0f; parsed->path_index = -1; if (path.empty()) @@ -77,13 +77,13 @@ bool ParseFaviconPath(const std::string& path, parsed->size_in_dip = gfx::kFaviconSize; } if (!scale_str.empty()) - webui::ParseScaleFactor(scale_str, &parsed->scale_factor); + webui::ParseScaleFactor(scale_str, &parsed->device_scale_factor); // Return the default favicon (as opposed to a resized favicon) for // favicon sizes which are not cached by the favicon service. // Currently the favicon service caches: // - favicons of sizes "gfx::kFaviconSize * scale factor" px of type FAVICON - // where scale factor is one of FaviconUtil::GetFaviconScaleFactors(). + // where scale factor is one of FaviconUtil::GetFaviconScales(). // - the largest TOUCH_ICON / TOUCH_PRECOMPOSED_ICON if (parsed->size_in_dip != gfx::kFaviconSize && icon_types == favicon_base::FAVICON) diff --git a/chrome/common/favicon/favicon_url_parser.h b/chrome/common/favicon/favicon_url_parser.h index 46eed9b..d7de770 100644 --- a/chrome/common/favicon/favicon_url_parser.h +++ b/chrome/common/favicon/favicon_url_parser.h @@ -19,8 +19,8 @@ struct ParsedFaviconPath { // The size of the requested favicon in dip. int size_in_dip; - // The scale factor of the requested favicon. - float scale_factor; + // The device scale factor of the requested favicon. + float device_scale_factor; // The index of the first character (relative to the path) where the the URL // from which the favicon is being requested is located. diff --git a/chrome/common/favicon/favicon_url_parser_unittest.cc b/chrome/common/favicon/favicon_url_parser_unittest.cc index 0e47a48..2c2fedc 100644 --- a/chrome/common/favicon/favicon_url_parser_unittest.cc +++ b/chrome/common/favicon/favicon_url_parser_unittest.cc @@ -43,7 +43,7 @@ TEST_F(FaviconUrlParserTest, ParsingNoExtraParams) { EXPECT_FALSE(parsed.is_icon_url); EXPECT_EQ(url, parsed.url); EXPECT_EQ(16, parsed.size_in_dip); - EXPECT_EQ(ui::SCALE_FACTOR_100P, parsed.scale_factor); + EXPECT_EQ(ui::SCALE_FACTOR_100P, parsed.device_scale_factor); } // Test parsing path with a 'size' parameter. @@ -58,7 +58,7 @@ TEST_F(FaviconUrlParserTest, ParsingSizeParam) { EXPECT_FALSE(parsed.is_icon_url); EXPECT_EQ(url, parsed.url); EXPECT_EQ(32, parsed.size_in_dip); - EXPECT_EQ(1.0f, parsed.scale_factor); + EXPECT_EQ(1.0f, parsed.device_scale_factor); // Test parsing current 'size' parameter format. const std::string path3 = "size/32@1.4x/" + url; @@ -66,7 +66,7 @@ TEST_F(FaviconUrlParserTest, ParsingSizeParam) { EXPECT_FALSE(parsed.is_icon_url); EXPECT_EQ(url, parsed.url); EXPECT_EQ(32, parsed.size_in_dip); - EXPECT_EQ(1.4f, parsed.scale_factor); + EXPECT_EQ(1.4f, parsed.device_scale_factor); // Test that we pick the ui::ScaleFactor which is closest to the passed in // scale factor. @@ -75,7 +75,7 @@ TEST_F(FaviconUrlParserTest, ParsingSizeParam) { EXPECT_FALSE(parsed.is_icon_url); EXPECT_EQ(url, parsed.url); EXPECT_EQ(16, parsed.size_in_dip); - EXPECT_EQ(1.41f, parsed.scale_factor); + EXPECT_EQ(1.41f, parsed.device_scale_factor); // Invalid cases. const std::string path5 = "size/" + url; @@ -91,7 +91,7 @@ TEST_F(FaviconUrlParserTest, ParsingSizeParam) { EXPECT_FALSE(parsed.is_icon_url); EXPECT_EQ(path8, parsed.url); EXPECT_EQ(16, parsed.size_in_dip); - EXPECT_EQ(1.0f, parsed.scale_factor); + EXPECT_EQ(1.0f, parsed.device_scale_factor); } // Test parsing path with the 'largest' parameter. @@ -119,7 +119,7 @@ TEST_F(FaviconUrlParserTest, ParsingIconUrlParam) { EXPECT_TRUE(parsed.is_icon_url); EXPECT_EQ("http://www.google.com/favicon.ico", parsed.url); EXPECT_EQ(16, parsed.size_in_dip); - EXPECT_EQ(ui::SCALE_FACTOR_100P, parsed.scale_factor); + EXPECT_EQ(ui::SCALE_FACTOR_100P, parsed.device_scale_factor); } // Test parsing path with 'origin' parameter. @@ -133,14 +133,14 @@ TEST_F(FaviconUrlParserTest, ParsingOriginParam) { EXPECT_FALSE(parsed.is_icon_url); EXPECT_EQ("https://www.google.ca/", parsed.url); EXPECT_EQ(16, parsed.size_in_dip); - EXPECT_EQ(ui::SCALE_FACTOR_100P, parsed.scale_factor); + EXPECT_EQ(ui::SCALE_FACTOR_100P, parsed.device_scale_factor); const std::string path12 = "origin/google.com"; EXPECT_TRUE(chrome::ParseFaviconPath(path12, icon_types, &parsed)); EXPECT_FALSE(parsed.is_icon_url); EXPECT_EQ("http://google.com/", parsed.url); EXPECT_EQ(16, parsed.size_in_dip); - EXPECT_EQ(ui::SCALE_FACTOR_100P, parsed.scale_factor); + EXPECT_EQ(ui::SCALE_FACTOR_100P, parsed.device_scale_factor); } // Test parsing paths with both a 'size' parameter and a 'url modifier' @@ -155,7 +155,7 @@ TEST_F(FaviconUrlParserTest, ParsingSizeParamAndUrlModifier) { EXPECT_FALSE(parsed.is_icon_url); EXPECT_EQ("https://www.google.ca/", parsed.url); EXPECT_EQ(32, parsed.size_in_dip); - EXPECT_EQ(1.4f, parsed.scale_factor); + EXPECT_EQ(1.4f, parsed.device_scale_factor); const std::string path14 = "largest/iconurl/http://www.google.com/favicon.ico"; 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 <cmath> + #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<gfx::ImagePNGRep> SelectFaviconFramesFromPNGsWithoutResizing( const std::vector<favicon_base::FaviconRawBitmapResult>& png_data, - const std::vector<ui::ScaleFactor>& scale_factors, + const std::vector<float>& favicon_scales, int favicon_size) { std::vector<gfx::ImagePNGRep> png_reps; if (png_data.empty()) @@ -48,14 +51,12 @@ std::vector<gfx::ImagePNGRep> 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<int, ui::ScaleFactor> 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<int, float> 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<gfx::ImagePNGRep> SelectFaviconFramesFromPNGsWithoutResizing( if (pixel_size.width() != pixel_size.height()) continue; - std::map<int, ui::ScaleFactor>::iterator it = + std::map<int, float>::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<ui::ScaleFactor> GetFaviconScaleFactors() { +std::vector<float> GetFaviconScales() { const float kScale1x = 1.0f; - std::vector<ui::ScaleFactor> favicon_scale_factors = + std::vector<ui::ScaleFactor> 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<float> 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<favicon_base::FaviconRawBitmapResult>& png_data, - const std::vector<ui::ScaleFactor>& scale_factors, + const std::vector<float>& 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<gfx::ImagePNGRep> 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<ui::ScaleFactor> scale_factors_to_generate = scale_factors; + std::vector<float> favicon_scales_to_generate = favicon_scales; for (size_t i = 0; i < png_reps.size(); ++i) { - for (int j = static_cast<int>(scale_factors_to_generate.size()) - 1; j >= 0; + for (int j = static_cast<int>(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<SkBitmap> 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 <vector> #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<ui::ScaleFactor> GetFaviconScaleFactors(); +std::vector<float> 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<favicon_base::FaviconRawBitmapResult>& png_data, - const std::vector<ui::ScaleFactor>& scale_factors, + const std::vector<float>& 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<size_t>::max() || @@ -140,8 +138,8 @@ void GetCandidateIndicesWithBestScores( return; } - std::vector<int>::const_iterator zero_size_it = std::find( - desired_sizes.begin(), desired_sizes.end(), 0); + std::vector<int>::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<SkBitmap>& bitmaps, - const std::vector<gfx::Size>& original_sizes, - const std::vector<ui::ScaleFactor>& scale_factors, - int desired_size_in_dip, - float* match_score) { +gfx::ImageSkia SelectFaviconFrames(const std::vector<SkBitmap>& bitmaps, + const std::vector<gfx::Size>& original_sizes, + const std::vector<float>& favicon_scales, + int desired_size_in_dip, + float* match_score) { std::vector<int> desired_sizes; std::map<int, float> 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<int, float>::const_iterator it = scale_map.find( - result.desired_size); + std::map<int, float>::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<gfx::Size>& frame_pixel_sizes, - const std::vector<int>& desired_sizes, - std::vector<size_t>* best_indices, - float* match_score) { +void SelectFaviconFrameIndices(const std::vector<gfx::Size>& frame_pixel_sizes, + const std::vector<int>& desired_sizes, + std::vector<size_t>* best_indices, + float* match_score) { std::vector<SelectionResult> 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 <vector> -#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<SkBitmap>& bitmaps, - const std::vector<gfx::Size>& original_sizes, - const std::vector<ui::ScaleFactor>& scale_factors, - int desired_size_in_dip, - float* score); +gfx::ImageSkia SelectFaviconFrames(const std::vector<SkBitmap>& bitmaps, + const std::vector<gfx::Size>& original_sizes, + const std::vector<float>& 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<gfx::Size>& frame_pixel_sizes, - const std::vector<int>& desired_sizes, - std::vector<size_t>* best_indices, - float* score); +void SelectFaviconFrameIndices(const std::vector<gfx::Size>& frame_pixel_sizes, + const std::vector<int>& desired_sizes, + std::vector<size_t>* best_indices, + float* score); #endif // COMPONENTS_FAVICON_BASE_SELECT_FAVICON_FRAMES_H_ |