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 {
public:
explicit GeneratedIconImageSource(base::char16 letter,
SkColor color,
int output_size)
SquareSizePx output_size)
: gfx::CanvasImageSource(gfx::Size(output_size, output_size)),
letter_(letter),
color_(color),
......@@ -46,7 +46,7 @@ class GeneratedIconImageSource : public gfx::CanvasImageSource {
private:
// gfx::CanvasImageSource overrides:
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 size_t border_radius = output_size_ / 16;
const size_t font_size = output_size_ * 7 / 16;
......@@ -88,21 +88,21 @@ class GeneratedIconImageSource : public gfx::CanvasImageSource {
// 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
// |bitmaps|, nothing will happen.
void GenerateIcon(std::map<int, BitmapAndSource>* bitmaps,
int output_size,
void GenerateIcon(std::map<SquareSizePx, SkBitmap>* bitmaps,
SquareSizePx output_size,
SkColor color,
base::char16 letter) {
// Do nothing if there is already an icon of |output_size|.
if (bitmaps->count(output_size))
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,
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
// black shelf.
if (generated_icon_color == SK_ColorTRANSPARENT)
......@@ -110,14 +110,14 @@ void GenerateIcons(std::set<int> generate_sizes,
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);
}
} // namespace
std::set<int> SizesToGenerate() {
return std::set<int>({
std::set<SquareSizePx> SizesToGenerate() {
return std::set<SquareSizePx>({
icon_size::k32,
icon_size::k64,
icon_size::k48,
......@@ -127,23 +127,14 @@ std::set<int> SizesToGenerate() {
});
}
BitmapAndSource::BitmapAndSource() {}
BitmapAndSource::BitmapAndSource(const GURL& source_url_p,
const SkBitmap& bitmap_p)
: source_url(source_url_p), bitmap(bitmap_p) {}
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;
std::map<SquareSizePx, SkBitmap> ConstrainBitmapsToSizes(
const std::vector<SkBitmap>& bitmaps,
const std::set<SquareSizePx>& sizes) {
std::map<SquareSizePx, SkBitmap> output_bitmaps;
std::map<SquareSizePx, SkBitmap> ordered_bitmaps;
for (const SkBitmap& bitmap : bitmaps) {
DCHECK(bitmap.width() == bitmap.height());
ordered_bitmaps[bitmap.width()] = bitmap_and_source;
ordered_bitmaps[bitmap.width()] = bitmap;
}
if (!ordered_bitmaps.empty()) {
......@@ -157,10 +148,10 @@ std::map<int, BitmapAndSource> ConstrainBitmapsToSizes(
output_bitmaps[size] = ordered_bitmaps.rbegin()->second;
// Resize the bitmap if it does not exactly match the desired size.
if (output_bitmaps[size].bitmap.width() != size) {
output_bitmaps[size].bitmap = skia::ImageOperations::Resize(
output_bitmaps[size].bitmap, skia::ImageOperations::RESIZE_LANCZOS3,
size, size);
if (output_bitmaps[size].width() != size) {
output_bitmaps[size] = skia::ImageOperations::Resize(
output_bitmaps[size], skia::ImageOperations::RESIZE_LANCZOS3, size,
size);
}
}
}
......@@ -168,7 +159,9 @@ std::map<int, BitmapAndSource> ConstrainBitmapsToSizes(
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(
std::make_unique<GeneratedIconImageSource>(letter, color, output_size),
gfx::Size(output_size, output_size));
......@@ -202,22 +195,22 @@ base::char16 GenerateIconLetterFromUrl(const GURL& app_url) {
return icon_letter;
}
std::map<int, BitmapAndSource> ResizeIconsAndGenerateMissing(
const std::vector<BitmapAndSource>& icons,
const std::set<int>& sizes_to_generate,
std::map<SquareSizePx, SkBitmap> ResizeIconsAndGenerateMissing(
const std::vector<SkBitmap>& icons,
const std::set<SquareSizePx>& sizes_to_generate,
const GURL& app_url,
SkColor* generated_icon_color) {
DCHECK(generated_icon_color);
// Resize provided icons to make sure we have versions for each size in
// |sizes_to_generate|.
std::map<int, BitmapAndSource> resized_bitmaps(
std::map<SquareSizePx, SkBitmap> resized_bitmaps(
ConstrainBitmapsToSizes(icons, sizes_to_generate));
// Also add all provided icon sizes.
for (const BitmapAndSource& icon : icons) {
if (resized_bitmaps.find(icon.bitmap.width()) == resized_bitmaps.end())
resized_bitmaps.insert(std::make_pair(icon.bitmap.width(), icon));
for (const SkBitmap& icon : icons) {
if (resized_bitmaps.find(icon.width()) == resized_bitmaps.end())
resized_bitmaps.insert(std::make_pair(icon.width(), icon));
}
// Determine the color that will be used for the icon's background. For this
......@@ -225,13 +218,13 @@ std::map<int, BitmapAndSource> ResizeIconsAndGenerateMissing(
if (!resized_bitmaps.empty()) {
color_utils::GridSampler sampler;
*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
// there is no icon in the required size.
std::set<int> generate_sizes;
for (int size : sizes_to_generate) {
std::set<SquareSizePx> generate_sizes;
for (SquareSizePx size : sizes_to_generate) {
if (resized_bitmaps.find(size) == resized_bitmaps.end())
generate_sizes.insert(size);
}
......
......@@ -38,30 +38,21 @@ enum {
} // namespace icon_size
// Returns icon sizes to be generated from downloaded icons.
std::set<int> 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;
};
std::set<SquareSizePx> SizesToGenerate();
// 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
// which contains only bitmaps of a size in |sizes| and at most one bitmap of
// each size.
std::map<int, BitmapAndSource> ConstrainBitmapsToSizes(
const std::vector<BitmapAndSource>& bitmaps,
const std::set<int>& sizes);
std::map<SquareSizePx, SkBitmap> ConstrainBitmapsToSizes(
const std::vector<SkBitmap>& bitmaps,
const std::set<SquareSizePx>& sizes);
// Generates a square container icon of |output_size| by drawing the given
// |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.
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.
// Output: |generated_icon_color| is the color to use if an icon needs to be
// generated for the web app.
std::map<int, BitmapAndSource> ResizeIconsAndGenerateMissing(
const std::vector<BitmapAndSource>& icons,
const std::set<int>& sizes_to_generate,
std::map<SquareSizePx, SkBitmap> ResizeIconsAndGenerateMissing(
const std::vector<SkBitmap>& icons,
const std::set<SquareSizePx>& sizes_to_generate,
const GURL& app_url,
SkColor* generated_icon_color);
......
......@@ -25,15 +25,15 @@ namespace web_app {
namespace {
constexpr int kMaxIcons = 20;
constexpr int kMaxIconSize = 1024;
constexpr SquareSizePx kMaxIconSize = 1024;
// Get a list of non-empty square icons from |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 SkBitmap& icon : url_icon.second) {
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,
// Get all non-empty square icons from |icons_map|.
void FilterSquareIconsFromBitmaps(
const std::map<SquareSizePx, SkBitmap> bitmaps,
std::vector<BitmapAndSource>* square_icons) {
std::vector<SkBitmap>* square_icons) {
for (const std::pair<SquareSizePx, SkBitmap>& icon : bitmaps) {
DCHECK_EQ(icon.first, icon.second.width());
DCHECK_EQ(icon.first, icon.second.height());
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,
// 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
// which are not actually created and linked on disk.
std::vector<BitmapAndSource> square_icons;
std::vector<SkBitmap> square_icons;
if (icons_map)
FilterSquareIconsFromMap(*icons_map, &square_icons);
if (!is_for_sync)
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) {
// 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
......@@ -161,14 +161,14 @@ void FilterAndResizeIconsGenerateMissing(WebApplicationInfo* web_app_info,
web_app_info->generated_icon_color = SK_ColorTRANSPARENT;
// TODO(https://crbug.com/1029223): Don't resize before writing to disk, it's
// 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,
&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.
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