Commit fb3f6d7e authored by Qiang Xu's avatar Qiang Xu Committed by Commit Bot

cros: manual scale of ImageSkia for different representations

This CL supports manually scale of ImageSkia for different
representations, which could deliver good visual effect for reps other
than 1x and 2x.

Bug: 840641
Test: manual
Change-Id: I7c131a76297dd565065ff67213b90349ff00206d
Reviewed-on: https://chromium-review.googlesource.com/1081408Reviewed-by: default avatarScott Violet <sky@chromium.org>
Reviewed-by: default avatarXiyuan Xia <xiyuan@chromium.org>
Commit-Queue: Qiang Xu <warx@google.com>
Cr-Commit-Position: refs/heads/master@{#568192}
parent f2b9f600
......@@ -360,23 +360,10 @@ void SearchResultView::OnMetadataChanged() {
void SearchResultView::SetIconImage(const gfx::ImageSkia& source,
views::ImageView* const icon,
const int icon_dimension) {
// Copy.
gfx::ImageSkia image(source);
// Scales down big icons but leave small ones unchanged.
if (image.width() > icon_dimension || image.height() > icon_dimension) {
image = gfx::ImageSkiaOperations::CreateResizedImage(
image, skia::ImageOperations::RESIZE_BEST,
gfx::Size(icon_dimension, icon_dimension));
} else {
icon->ResetImageSize();
}
// Set the image to an empty image before we reset the image because
// since we're using the same backing store for our images, sometimes
// ImageView won't detect that we have a new image set due to the pixel
// buffer pointers remaining the same despite the image changing.
icon->SetImage(gfx::ImageSkia());
image = gfx::ImageSkiaOperations::CreateResizedImage(
source, skia::ImageOperations::RESIZE_BEST,
gfx::Size(icon_dimension, icon_dimension));
icon->SetImage(image);
}
......
......@@ -9,6 +9,7 @@
#include "base/logging.h"
#include "base/strings/utf_string_conversions.h"
#include "cc/paint/paint_flags.h"
#include "skia/ext/image_operations.h"
#include "ui/accessibility/ax_node_data.h"
#include "ui/gfx/canvas.h"
#include "ui/gfx/geometry/insets.h"
......@@ -43,6 +44,7 @@ void ImageView::SetImage(const gfx::ImageSkia& img) {
last_painted_bitmap_pixels_ = nullptr;
gfx::Size pref_size(GetPreferredSize());
image_ = img;
scaled_image_ = gfx::ImageSkia();
if (pref_size != GetPreferredSize())
PreferredSizeChanged();
SchedulePaint();
......@@ -207,25 +209,50 @@ void ImageView::OnPaintImage(gfx::Canvas* canvas) {
last_paint_scale_ = canvas->image_scale();
last_painted_bitmap_pixels_ = nullptr;
if (image_.isNull())
gfx::ImageSkia image = GetPaintImage(last_paint_scale_);
if (image.isNull())
return;
gfx::Rect image_bounds(GetImageBounds());
if (image_bounds.IsEmpty())
return;
if (image_bounds.size() != gfx::Size(image_.width(), image_.height())) {
if (image_bounds.size() != gfx::Size(image.width(), image.height())) {
// Resize case
cc::PaintFlags flags;
flags.setFilterQuality(kLow_SkFilterQuality);
canvas->DrawImageInt(image_, 0, 0, image_.width(), image_.height(),
canvas->DrawImageInt(image, 0, 0, image.width(), image.height(),
image_bounds.x(), image_bounds.y(),
image_bounds.width(), image_bounds.height(), true,
flags);
} else {
canvas->DrawImageInt(image_, image_bounds.x(), image_bounds.y());
canvas->DrawImageInt(image, image_bounds.x(), image_bounds.y());
}
last_painted_bitmap_pixels_ = GetBitmapPixels(image_, last_paint_scale_);
last_painted_bitmap_pixels_ = GetBitmapPixels(image, last_paint_scale_);
}
gfx::ImageSkia ImageView::GetPaintImage(float scale) {
if (image_.isNull())
return image_;
const gfx::ImageSkiaRep& rep = image_.GetRepresentation(scale);
if (rep.scale() == scale)
return image_;
if (scaled_image_.HasRepresentation(scale))
return scaled_image_;
// Only caches one image rep for the current scale.
scaled_image_ = gfx::ImageSkia();
gfx::Size scaled_size =
gfx::ScaleToCeiledSize(rep.pixel_size(), scale / rep.scale());
scaled_image_.AddRepresentation(gfx::ImageSkiaRep(
skia::ImageOperations::Resize(rep.sk_bitmap(),
skia::ImageOperations::RESIZE_BEST,
scaled_size.width(), scaled_size.height()),
scale));
return scaled_image_;
}
} // namespace views
......@@ -87,6 +87,9 @@ class VIEWS_EXPORT ImageView : public View {
void OnPaintImage(gfx::Canvas* canvas);
// Gets an ImageSkia to paint that has proper rep for |scale|.
gfx::ImageSkia GetPaintImage(float scale);
// Returns true if |img| is the same as the last image we painted. This is
// intended to be a quick check, not exhaustive. In other words it's possible
// for this to return false even though the images are in fact equal.
......@@ -105,6 +108,9 @@ class VIEWS_EXPORT ImageView : public View {
// The underlying image.
gfx::ImageSkia image_;
// Caches the scaled image reps.
gfx::ImageSkia scaled_image_;
// Horizontal alignment.
Alignment horizontal_alignment_;
......
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