Commit e59d30f9 authored by Alan Cutter's avatar Alan Cutter Committed by Commit Bot

WebApps: Remove unused BitmapAndSource struct

With the change to split WebApplicationInfo::icons into icon_infos and
icon_bitmaps [1] we don't need to preserve URLs when downloading icons.
This means we can simplfy the BitmapAndSource struct into just SkBitmap.

[1] https://chromium-review.googlesource.com/c/chromium/src/+/1928600/10

Bug: 926083
Change-Id: I9c36d1bf1e726aeb6d1f79f1bdab005b8032265a
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1936441
Commit-Queue: Alan Cutter <alancutter@chromium.org>
Reviewed-by: default avatarAlexey Baskakov <loyso@chromium.org>
Cr-Commit-Position: refs/heads/master@{#720830}
parent 59630335
...@@ -36,7 +36,7 @@ class GeneratedIconImageSource : public gfx::CanvasImageSource { ...@@ -36,7 +36,7 @@ class GeneratedIconImageSource : public gfx::CanvasImageSource {
public: public:
explicit GeneratedIconImageSource(base::char16 letter, explicit GeneratedIconImageSource(base::char16 letter,
SkColor color, SkColor color,
int output_size) SquareSizePx output_size)
: gfx::CanvasImageSource(gfx::Size(output_size, output_size)), : gfx::CanvasImageSource(gfx::Size(output_size, output_size)),
letter_(letter), letter_(letter),
color_(color), color_(color),
...@@ -46,7 +46,7 @@ class GeneratedIconImageSource : public gfx::CanvasImageSource { ...@@ -46,7 +46,7 @@ class GeneratedIconImageSource : public gfx::CanvasImageSource {
private: private:
// gfx::CanvasImageSource overrides: // gfx::CanvasImageSource overrides:
void Draw(gfx::Canvas* canvas) override { void Draw(gfx::Canvas* canvas) override {
const int icon_size = output_size_ * 3 / 4; const SquareSizePx icon_size = output_size_ * 3 / 4;
const int icon_inset = output_size_ / 8; const int icon_inset = output_size_ / 8;
const size_t border_radius = output_size_ / 16; const size_t border_radius = output_size_ / 16;
const size_t font_size = output_size_ * 7 / 16; const size_t font_size = output_size_ * 7 / 16;
...@@ -88,21 +88,21 @@ class GeneratedIconImageSource : public gfx::CanvasImageSource { ...@@ -88,21 +88,21 @@ class GeneratedIconImageSource : public gfx::CanvasImageSource {
// to |bitmaps| by drawing the given |letter| into a rounded background of // to |bitmaps| by drawing the given |letter| into a rounded background of
// |color|. For each size, if an icon of the requested size already exists in // |color|. For each size, if an icon of the requested size already exists in
// |bitmaps|, nothing will happen. // |bitmaps|, nothing will happen.
void GenerateIcon(std::map<int, BitmapAndSource>* bitmaps, void GenerateIcon(std::map<SquareSizePx, SkBitmap>* bitmaps,
int output_size, SquareSizePx output_size,
SkColor color, SkColor color,
base::char16 letter) { base::char16 letter) {
// Do nothing if there is already an icon of |output_size|. // Do nothing if there is already an icon of |output_size|.
if (bitmaps->count(output_size)) if (bitmaps->count(output_size))
return; return;
(*bitmaps)[output_size].bitmap = GenerateBitmap(output_size, color, letter); (*bitmaps)[output_size] = GenerateBitmap(output_size, color, letter);
} }
void GenerateIcons(std::set<int> generate_sizes, void GenerateIcons(std::set<SquareSizePx> generate_sizes,
const GURL& app_url, const GURL& app_url,
SkColor generated_icon_color, SkColor generated_icon_color,
std::map<int, BitmapAndSource>* bitmap_map) { std::map<SquareSizePx, SkBitmap>* bitmap_map) {
// If no color has been specified, use a dark gray so it will stand out on the // If no color has been specified, use a dark gray so it will stand out on the
// black shelf. // black shelf.
if (generated_icon_color == SK_ColorTRANSPARENT) if (generated_icon_color == SK_ColorTRANSPARENT)
...@@ -110,14 +110,14 @@ void GenerateIcons(std::set<int> generate_sizes, ...@@ -110,14 +110,14 @@ void GenerateIcons(std::set<int> generate_sizes,
const base::char16 icon_letter = GenerateIconLetterFromUrl(app_url); const base::char16 icon_letter = GenerateIconLetterFromUrl(app_url);
for (int size : generate_sizes) for (SquareSizePx size : generate_sizes)
GenerateIcon(bitmap_map, size, generated_icon_color, icon_letter); GenerateIcon(bitmap_map, size, generated_icon_color, icon_letter);
} }
} // namespace } // namespace
std::set<int> SizesToGenerate() { std::set<SquareSizePx> SizesToGenerate() {
return std::set<int>({ return std::set<SquareSizePx>({
icon_size::k32, icon_size::k32,
icon_size::k64, icon_size::k64,
icon_size::k48, icon_size::k48,
...@@ -127,23 +127,14 @@ std::set<int> SizesToGenerate() { ...@@ -127,23 +127,14 @@ std::set<int> SizesToGenerate() {
}); });
} }
BitmapAndSource::BitmapAndSource() {} std::map<SquareSizePx, SkBitmap> ConstrainBitmapsToSizes(
const std::vector<SkBitmap>& bitmaps,
BitmapAndSource::BitmapAndSource(const GURL& source_url_p, const std::set<SquareSizePx>& sizes) {
const SkBitmap& bitmap_p) std::map<SquareSizePx, SkBitmap> output_bitmaps;
: source_url(source_url_p), bitmap(bitmap_p) {} std::map<SquareSizePx, SkBitmap> ordered_bitmaps;
for (const SkBitmap& bitmap : bitmaps) {
BitmapAndSource::~BitmapAndSource() {}
std::map<int, BitmapAndSource> ConstrainBitmapsToSizes(
const std::vector<BitmapAndSource>& bitmaps,
const std::set<int>& sizes) {
std::map<int, BitmapAndSource> output_bitmaps;
std::map<int, BitmapAndSource> ordered_bitmaps;
for (const BitmapAndSource& bitmap_and_source : bitmaps) {
const SkBitmap& bitmap = bitmap_and_source.bitmap;
DCHECK(bitmap.width() == bitmap.height()); DCHECK(bitmap.width() == bitmap.height());
ordered_bitmaps[bitmap.width()] = bitmap_and_source; ordered_bitmaps[bitmap.width()] = bitmap;
} }
if (!ordered_bitmaps.empty()) { if (!ordered_bitmaps.empty()) {
...@@ -157,10 +148,10 @@ std::map<int, BitmapAndSource> ConstrainBitmapsToSizes( ...@@ -157,10 +148,10 @@ std::map<int, BitmapAndSource> ConstrainBitmapsToSizes(
output_bitmaps[size] = ordered_bitmaps.rbegin()->second; output_bitmaps[size] = ordered_bitmaps.rbegin()->second;
// Resize the bitmap if it does not exactly match the desired size. // Resize the bitmap if it does not exactly match the desired size.
if (output_bitmaps[size].bitmap.width() != size) { if (output_bitmaps[size].width() != size) {
output_bitmaps[size].bitmap = skia::ImageOperations::Resize( output_bitmaps[size] = skia::ImageOperations::Resize(
output_bitmaps[size].bitmap, skia::ImageOperations::RESIZE_LANCZOS3, output_bitmaps[size], skia::ImageOperations::RESIZE_LANCZOS3, size,
size, size); size);
} }
} }
} }
...@@ -168,7 +159,9 @@ std::map<int, BitmapAndSource> ConstrainBitmapsToSizes( ...@@ -168,7 +159,9 @@ std::map<int, BitmapAndSource> ConstrainBitmapsToSizes(
return output_bitmaps; return output_bitmaps;
} }
SkBitmap GenerateBitmap(int output_size, SkColor color, base::char16 letter) { SkBitmap GenerateBitmap(SquareSizePx output_size,
SkColor color,
base::char16 letter) {
gfx::ImageSkia icon_image( gfx::ImageSkia icon_image(
std::make_unique<GeneratedIconImageSource>(letter, color, output_size), std::make_unique<GeneratedIconImageSource>(letter, color, output_size),
gfx::Size(output_size, output_size)); gfx::Size(output_size, output_size));
...@@ -202,22 +195,22 @@ base::char16 GenerateIconLetterFromUrl(const GURL& app_url) { ...@@ -202,22 +195,22 @@ base::char16 GenerateIconLetterFromUrl(const GURL& app_url) {
return icon_letter; return icon_letter;
} }
std::map<int, BitmapAndSource> ResizeIconsAndGenerateMissing( std::map<SquareSizePx, SkBitmap> ResizeIconsAndGenerateMissing(
const std::vector<BitmapAndSource>& icons, const std::vector<SkBitmap>& icons,
const std::set<int>& sizes_to_generate, const std::set<SquareSizePx>& sizes_to_generate,
const GURL& app_url, const GURL& app_url,
SkColor* generated_icon_color) { SkColor* generated_icon_color) {
DCHECK(generated_icon_color); DCHECK(generated_icon_color);
// Resize provided icons to make sure we have versions for each size in // Resize provided icons to make sure we have versions for each size in
// |sizes_to_generate|. // |sizes_to_generate|.
std::map<int, BitmapAndSource> resized_bitmaps( std::map<SquareSizePx, SkBitmap> resized_bitmaps(
ConstrainBitmapsToSizes(icons, sizes_to_generate)); ConstrainBitmapsToSizes(icons, sizes_to_generate));
// Also add all provided icon sizes. // Also add all provided icon sizes.
for (const BitmapAndSource& icon : icons) { for (const SkBitmap& icon : icons) {
if (resized_bitmaps.find(icon.bitmap.width()) == resized_bitmaps.end()) if (resized_bitmaps.find(icon.width()) == resized_bitmaps.end())
resized_bitmaps.insert(std::make_pair(icon.bitmap.width(), icon)); resized_bitmaps.insert(std::make_pair(icon.width(), icon));
} }
// Determine the color that will be used for the icon's background. For this // Determine the color that will be used for the icon's background. For this
...@@ -225,13 +218,13 @@ std::map<int, BitmapAndSource> ResizeIconsAndGenerateMissing( ...@@ -225,13 +218,13 @@ std::map<int, BitmapAndSource> ResizeIconsAndGenerateMissing(
if (!resized_bitmaps.empty()) { if (!resized_bitmaps.empty()) {
color_utils::GridSampler sampler; color_utils::GridSampler sampler;
*generated_icon_color = color_utils::CalculateKMeanColorOfBitmap( *generated_icon_color = color_utils::CalculateKMeanColorOfBitmap(
resized_bitmaps.begin()->second.bitmap); resized_bitmaps.begin()->second);
} }
// Work out what icons we need to generate here. Icons are only generated if // Work out what icons we need to generate here. Icons are only generated if
// there is no icon in the required size. // there is no icon in the required size.
std::set<int> generate_sizes; std::set<SquareSizePx> generate_sizes;
for (int size : sizes_to_generate) { for (SquareSizePx size : sizes_to_generate) {
if (resized_bitmaps.find(size) == resized_bitmaps.end()) if (resized_bitmaps.find(size) == resized_bitmaps.end())
generate_sizes.insert(size); generate_sizes.insert(size);
} }
......
...@@ -38,30 +38,21 @@ enum { ...@@ -38,30 +38,21 @@ enum {
} // namespace icon_size } // namespace icon_size
// Returns icon sizes to be generated from downloaded icons. // Returns icon sizes to be generated from downloaded icons.
std::set<int> SizesToGenerate(); std::set<SquareSizePx> SizesToGenerate();
// TODO(https://crbug.com/926083): Replace all occurrences of this with
// SkBitmap. We now record the URL in WebApplicationIconInfo instead.
struct BitmapAndSource {
BitmapAndSource();
BitmapAndSource(const GURL& source_url_p, const SkBitmap& bitmap_p);
~BitmapAndSource();
GURL source_url;
SkBitmap bitmap;
};
// This finds the closest not-smaller bitmap in |bitmaps| for each size in // This finds the closest not-smaller bitmap in |bitmaps| for each size in
// |sizes| and resizes it to that size. This returns a map of sizes to bitmaps // |sizes| and resizes it to that size. This returns a map of sizes to bitmaps
// which contains only bitmaps of a size in |sizes| and at most one bitmap of // which contains only bitmaps of a size in |sizes| and at most one bitmap of
// each size. // each size.
std::map<int, BitmapAndSource> ConstrainBitmapsToSizes( std::map<SquareSizePx, SkBitmap> ConstrainBitmapsToSizes(
const std::vector<BitmapAndSource>& bitmaps, const std::vector<SkBitmap>& bitmaps,
const std::set<int>& sizes); const std::set<SquareSizePx>& sizes);
// Generates a square container icon of |output_size| by drawing the given // Generates a square container icon of |output_size| by drawing the given
// |letter| into a rounded background of |color|. // |letter| into a rounded background of |color|.
SkBitmap GenerateBitmap(int output_size, SkColor color, base::char16 letter); SkBitmap GenerateBitmap(SquareSizePx output_size,
SkColor color,
base::char16 letter);
// Returns the letter that will be painted on the generated icon. // Returns the letter that will be painted on the generated icon.
base::char16 GenerateIconLetterFromUrl(const GURL& app_url); base::char16 GenerateIconLetterFromUrl(const GURL& app_url);
...@@ -70,9 +61,9 @@ base::char16 GenerateIconLetterFromUrl(const GURL& app_url); ...@@ -70,9 +61,9 @@ base::char16 GenerateIconLetterFromUrl(const GURL& app_url);
// Note that |app_url| is the launch URL for the app. // Note that |app_url| is the launch URL for the app.
// Output: |generated_icon_color| is the color to use if an icon needs to be // Output: |generated_icon_color| is the color to use if an icon needs to be
// generated for the web app. // generated for the web app.
std::map<int, BitmapAndSource> ResizeIconsAndGenerateMissing( std::map<SquareSizePx, SkBitmap> ResizeIconsAndGenerateMissing(
const std::vector<BitmapAndSource>& icons, const std::vector<SkBitmap>& icons,
const std::set<int>& sizes_to_generate, const std::set<SquareSizePx>& sizes_to_generate,
const GURL& app_url, const GURL& app_url,
SkColor* generated_icon_color); SkColor* generated_icon_color);
......
...@@ -25,15 +25,15 @@ namespace web_app { ...@@ -25,15 +25,15 @@ namespace web_app {
namespace { namespace {
constexpr int kMaxIcons = 20; constexpr int kMaxIcons = 20;
constexpr int kMaxIconSize = 1024; constexpr SquareSizePx kMaxIconSize = 1024;
// Get a list of non-empty square icons from |icons_map|. // Get a list of non-empty square icons from |icons_map|.
void FilterSquareIconsFromMap(const IconsMap& icons_map, void FilterSquareIconsFromMap(const IconsMap& icons_map,
std::vector<BitmapAndSource>* square_icons) { std::vector<SkBitmap>* square_icons) {
for (const std::pair<GURL, std::vector<SkBitmap>>& url_icon : icons_map) { for (const std::pair<GURL, std::vector<SkBitmap>>& url_icon : icons_map) {
for (const SkBitmap& icon : url_icon.second) { for (const SkBitmap& icon : url_icon.second) {
if (!icon.empty() && icon.width() == icon.height()) if (!icon.empty() && icon.width() == icon.height())
square_icons->push_back(BitmapAndSource(url_icon.first, icon)); square_icons->push_back(icon);
} }
} }
} }
...@@ -41,12 +41,12 @@ void FilterSquareIconsFromMap(const IconsMap& icons_map, ...@@ -41,12 +41,12 @@ void FilterSquareIconsFromMap(const IconsMap& icons_map,
// Get all non-empty square icons from |icons_map|. // Get all non-empty square icons from |icons_map|.
void FilterSquareIconsFromBitmaps( void FilterSquareIconsFromBitmaps(
const std::map<SquareSizePx, SkBitmap> bitmaps, const std::map<SquareSizePx, SkBitmap> bitmaps,
std::vector<BitmapAndSource>* square_icons) { std::vector<SkBitmap>* square_icons) {
for (const std::pair<SquareSizePx, SkBitmap>& icon : bitmaps) { for (const std::pair<SquareSizePx, SkBitmap>& icon : bitmaps) {
DCHECK_EQ(icon.first, icon.second.width()); DCHECK_EQ(icon.first, icon.second.width());
DCHECK_EQ(icon.first, icon.second.height()); DCHECK_EQ(icon.first, icon.second.height());
if (!icon.second.empty()) if (!icon.second.empty())
square_icons->push_back(BitmapAndSource(GURL(), icon.second)); square_icons->push_back(icon.second);
} }
} }
...@@ -142,13 +142,13 @@ void FilterAndResizeIconsGenerateMissing(WebApplicationInfo* web_app_info, ...@@ -142,13 +142,13 @@ void FilterAndResizeIconsGenerateMissing(WebApplicationInfo* web_app_info,
// icons for any sizes which have failed to download. This ensures that the // icons for any sizes which have failed to download. This ensures that the
// created manifest for the web app does not contain links to icons // created manifest for the web app does not contain links to icons
// which are not actually created and linked on disk. // which are not actually created and linked on disk.
std::vector<BitmapAndSource> square_icons; std::vector<SkBitmap> square_icons;
if (icons_map) if (icons_map)
FilterSquareIconsFromMap(*icons_map, &square_icons); FilterSquareIconsFromMap(*icons_map, &square_icons);
if (!is_for_sync) if (!is_for_sync)
FilterSquareIconsFromBitmaps(web_app_info->icon_bitmaps, &square_icons); FilterSquareIconsFromBitmaps(web_app_info->icon_bitmaps, &square_icons);
std::set<int> sizes_to_generate = SizesToGenerate(); std::set<SquareSizePx> sizes_to_generate = SizesToGenerate();
if (is_for_sync) { if (is_for_sync) {
// Ensure that all icon widths in the web app info icon array are present in // Ensure that all icon widths in the web app info icon array are present in
// the sizes to generate set. This ensures that we will have all of the // the sizes to generate set. This ensures that we will have all of the
...@@ -161,14 +161,14 @@ void FilterAndResizeIconsGenerateMissing(WebApplicationInfo* web_app_info, ...@@ -161,14 +161,14 @@ void FilterAndResizeIconsGenerateMissing(WebApplicationInfo* web_app_info,
web_app_info->generated_icon_color = SK_ColorTRANSPARENT; web_app_info->generated_icon_color = SK_ColorTRANSPARENT;
// TODO(https://crbug.com/1029223): Don't resize before writing to disk, it's // TODO(https://crbug.com/1029223): Don't resize before writing to disk, it's
// not necessary and would simplify this code path to remove. // not necessary and would simplify this code path to remove.
std::map<int, BitmapAndSource> size_to_icon = ResizeIconsAndGenerateMissing( std::map<SquareSizePx, SkBitmap> size_to_icon = ResizeIconsAndGenerateMissing(
square_icons, sizes_to_generate, web_app_info->app_url, square_icons, sizes_to_generate, web_app_info->app_url,
&web_app_info->generated_icon_color); &web_app_info->generated_icon_color);
for (std::pair<const int, BitmapAndSource>& item : size_to_icon) { for (std::pair<const SquareSizePx, SkBitmap>& item : size_to_icon) {
// Retain any bitmaps provided as input to the installation. // Retain any bitmaps provided as input to the installation.
if (web_app_info->icon_bitmaps.count(item.first) == 0) { if (web_app_info->icon_bitmaps.count(item.first) == 0) {
web_app_info->icon_bitmaps[item.first] = std::move(item.second.bitmap); web_app_info->icon_bitmaps[item.first] = std::move(item.second);
} }
} }
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment