Commit 52969d73 authored by estade's avatar estade Committed by Commit bot

Fix bug in browser action icon scaling for non-1x scale factors.

skia::ImageOperations::Resize needs pixels, and we were passing dp.

Also, ignore the icon size key and just use the icon size itself. This fixes a bug (evidenced again by Shoptagr) where an icon size such as

"19": "icons/19.png"  // 19.png is actually 50x50

would be scaled incorrectly. At this point, the API should take a list rather than a dict, but it's probably not possible to change that and maintain backcompat.

BUG=581232
TBR=benwells@chromium.org

Review URL: https://codereview.chromium.org/1637763002

Cr-Commit-Position: refs/heads/master@{#371904}
parent 44f9260b
......@@ -129,14 +129,6 @@ bool ExtensionAction::ParseIconFromCanvasDictionary(
gfx::ImageSkia* icon) {
for (base::DictionaryValue::Iterator iter(dict); !iter.IsAtEnd();
iter.Advance()) {
int icon_size = 0;
// Chrome helpfully scales the provided icon(s), but let's not go overboard.
const int kActionIconMaxSize = 10 * extension_misc::EXTENSION_ICON_ACTION;
if (!base::StringToInt(iter.key(), &icon_size) || icon_size <= 0 ||
icon_size > kActionIconMaxSize) {
continue;
}
const base::BinaryValue* image_data;
std::string binary_string64;
IPC::Message pickle;
......@@ -155,8 +147,16 @@ bool ExtensionAction::ParseIconFromCanvasDictionary(
if (!IPC::ReadParam(&pickle, &pickle_iter, &bitmap))
return false;
CHECK(!bitmap.isNull());
// Chrome helpfully scales the provided icon(s), but let's not go overboard.
const int kActionIconMaxSize = 10 * extension_misc::EXTENSION_ICON_ACTION;
if (bitmap.drawsNothing() || bitmap.width() != bitmap.height() ||
bitmap.width() > kActionIconMaxSize) {
continue;
}
float scale =
static_cast<float>(icon_size) / ExtensionAction::ActionIconSize();
static_cast<float>(bitmap.width()) / ExtensionAction::ActionIconSize();
icon->AddRepresentation(gfx::ImageSkiaRep(bitmap, scale));
}
return true;
......
......@@ -11,6 +11,7 @@
#include "base/logging.h"
#include "base/strings/utf_string_conversions.h"
#include "build/build_config.h"
#include "chrome/browser/extensions/extension_action.h"
#include "third_party/skia/include/core/SkPaint.h"
#include "third_party/skia/include/core/SkTypeface.h"
#include "ui/base/material_design/material_design_controller.h"
......@@ -90,12 +91,13 @@ SkPaint* GetBadgeTextPaintSingleton() {
}
gfx::ImageSkiaRep ScaleImageSkiaRep(const gfx::ImageSkiaRep& rep,
int target_width,
int target_width_dp,
float target_scale) {
int width_px = target_width_dp * target_scale;
return gfx::ImageSkiaRep(
skia::ImageOperations::Resize(rep.sk_bitmap(),
skia::ImageOperations::RESIZE_BEST,
target_width, target_width),
width_px, width_px),
target_scale);
}
......@@ -130,14 +132,16 @@ void IconWithBadgeImageSource::Draw(gfx::Canvas* canvas) {
gfx::ImageSkia skia = icon_.AsImageSkia();
gfx::ImageSkiaRep rep = skia.GetRepresentation(canvas->image_scale());
if (rep.scale() != canvas->image_scale()) {
skia.AddRepresentation(
ScaleImageSkiaRep(rep, skia.width(), canvas->image_scale()));
skia.AddRepresentation(ScaleImageSkiaRep(
rep, ExtensionAction::ActionIconSize(), canvas->image_scale()));
}
if (grayscale_)
skia = gfx::ImageSkiaOperations::CreateHSLShiftedImage(skia, {-1, 0, 0.6});
int x_offset = std::floor((size().width() - icon_.Width()) / 2.0);
int y_offset = std::floor((size().height() - icon_.Height()) / 2.0);
int x_offset =
std::floor((size().width() - ExtensionAction::ActionIconSize()) / 2.0);
int y_offset =
std::floor((size().height() - ExtensionAction::ActionIconSize()) / 2.0);
canvas->DrawImageInt(skia, x_offset, y_offset);
// Draw a badge on the provided browser action icon's canvas.
......
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