From 49a7e41d8b10fb00e790bf851653fcb36f63875f Mon Sep 17 00:00:00 2001 From: "oshima@chromium.org" Date: Sat, 19 Jan 2013 01:11:52 +0000 Subject: Merge 177692 > Fallback to 1x scale factor if the images doesn't have the target > scale factor. > > Remove MatchScale and its flag as it's no longer necessary. > > BUG=170415 > TEST=see the bug > > > Review URL: https://chromiumcodereview.appspot.com/11946032 TBR=oshima@chromium.org Review URL: https://codereview.chromium.org/11938038 git-svn-id: svn://svn.chromium.org/chrome/branches/1364/src@177804 0039d316-1c4b-4281-b951-d872f2087c98 --- ui/gfx/image/image_skia_operations.cc | 160 +++++++++++++++++++--------------- 1 file changed, 88 insertions(+), 72 deletions(-) diff --git a/ui/gfx/image/image_skia_operations.cc b/ui/gfx/image/image_skia_operations.cc index b07993d..ea5127c 100644 --- a/ui/gfx/image/image_skia_operations.cc +++ b/ui/gfx/image/image_skia_operations.cc @@ -20,78 +20,104 @@ #include "ui/gfx/size_conversions.h" #include "ui/gfx/skbitmap_operations.h" #include "ui/gfx/skia_util.h" -#include "ui/gfx/switches.h" namespace gfx { namespace { -bool ScalingEnabled() { - static bool scale_images = !CommandLine::ForCurrentProcess()->HasSwitch( - switches::kDisableScalingInImageSkiaOperations); - return scale_images; +// Returns an image rep for the ImageSkiaSource to return to visually indicate +// an error. +ImageSkiaRep GetErrorImageRep(ui::ScaleFactor scale_factor, + const gfx::Size& pixel_size) { + SkBitmap bitmap; + bitmap.setConfig( + SkBitmap::kARGB_8888_Config, pixel_size.width(), pixel_size.height()); + bitmap.allocPixels(); + bitmap.eraseColor(SK_ColorRED); + return gfx::ImageSkiaRep(bitmap, scale_factor); } -// Creates 2x scaled image of the give |source|. -ImageSkiaRep Create2XImageSkiaRep(const ImageSkiaRep& source) { - gfx::Size size(source.GetWidth() * 2.0f, source.GetHeight() * 2.0f); - - SkBitmap resized_bitmap; - resized_bitmap.setConfig(SkBitmap::kARGB_8888_Config, size.width(), - size.height()); - if (!resized_bitmap.allocPixels()) - SK_CRASH(); - SkCanvas canvas(resized_bitmap); - SkRect resized_bounds = RectToSkRect(gfx::Rect(size)); - canvas.drawBitmapRect(source.sk_bitmap(), NULL, resized_bounds); - return ImageSkiaRep(resized_bitmap, ui::SCALE_FACTOR_200P); -} +// A base image source class that creates an image from two source images. +// This class guarantees that two ImageSkiaReps have have the same pixel size. +class BinaryImageSource : public gfx::ImageSkiaSource { + protected: + BinaryImageSource(const ImageSkia& first, + const ImageSkia& second, + const char* source_name) + : first_(first), + second_(second), + source_name_(source_name) { + } + virtual ~BinaryImageSource() { + } -// A utility function to synchronize the scale factor of the two images. -// When the command line option "--disable-scaling-in-image-skia-operation" -// is provided, this function will fail if the scale factors of the two images -// are different. This assumes that the platform only supports -// 1x and 2x scale factors. -// TODO(oshima): Remove and replace this with plain CHECK once -// 2x images for all resources are provided. -void MatchScale(ImageSkiaRep* first, ImageSkiaRep* second) { - if (first->scale_factor() != second->scale_factor()) { - CHECK(ScalingEnabled()); - ImageSkiaRep* target = NULL; - if (first->scale_factor() == ui::SCALE_FACTOR_100P) { - target = first; + // gfx::ImageSkiaSource overrides: + virtual ImageSkiaRep GetImageForScale(ui::ScaleFactor scale_factor) OVERRIDE { + ImageSkiaRep first_rep = first_.GetRepresentation(scale_factor); + ImageSkiaRep second_rep = second_.GetRepresentation(scale_factor); + if (first_rep.pixel_width() != second_rep.pixel_width() || + first_rep.pixel_height() != second_rep.pixel_height()) { + DCHECK_NE(first_rep.scale_factor(), second_rep.scale_factor()); + if (first_rep.scale_factor() == second_rep.scale_factor()) { + LOG(ERROR) << "ImageSkiaRep size mismatch in " << source_name_; + return GetErrorImageRep(first_rep.scale_factor(), + gfx::Size(first_rep.pixel_width(), + first_rep.pixel_height())); + } + first_rep = first_.GetRepresentation(ui::SCALE_FACTOR_100P); + second_rep = second_.GetRepresentation(ui::SCALE_FACTOR_100P); + DCHECK_EQ(first_rep.pixel_width(), second_rep.pixel_width()); + DCHECK_EQ(first_rep.pixel_height(), second_rep.pixel_height()); + if (first_rep.pixel_width() != second_rep.pixel_width() || + first_rep.pixel_height() != second_rep.pixel_height()) { + LOG(ERROR) << "ImageSkiaRep size mismatch in " << source_name_; + return GetErrorImageRep(first_rep.scale_factor(), + gfx::Size(first_rep.pixel_width(), + first_rep.pixel_height())); + } } else { - target = second; + DCHECK_EQ(first_rep.scale_factor(), second_rep.scale_factor()); } - *target = Create2XImageSkiaRep(*target); + return CreateImageSkiaRep(first_rep, second_rep); } -} -class BlendingImageSource : public gfx::ImageSkiaSource { + // Creates a final image from two ImageSkiaReps. The pixel size of + // the two images are guaranteed to be the same. + virtual ImageSkiaRep CreateImageSkiaRep( + const ImageSkiaRep& first_rep, + const ImageSkiaRep& second_rep) const = 0; + + private: + const ImageSkia first_; + const ImageSkia second_; + // The name of a class that implements the BinaryImageSource. + // The subclass is responsible for managing the memory. + const char* source_name_; + + DISALLOW_COPY_AND_ASSIGN(BinaryImageSource); +}; + +class BlendingImageSource : public BinaryImageSource { public: BlendingImageSource(const ImageSkia& first, const ImageSkia& second, double alpha) - : first_(first), - second_(second), + : BinaryImageSource(first, second, "BlendingImageSource"), alpha_(alpha) { } virtual ~BlendingImageSource() { } - // gfx::ImageSkiaSource overrides: - virtual ImageSkiaRep GetImageForScale(ui::ScaleFactor scale_factor) OVERRIDE { - ImageSkiaRep first_rep = first_.GetRepresentation(scale_factor); - ImageSkiaRep second_rep = second_.GetRepresentation(scale_factor); - MatchScale(&first_rep, &second_rep); + // BinaryImageSource overrides: + virtual ImageSkiaRep CreateImageSkiaRep( + const ImageSkiaRep& first_rep, + const ImageSkiaRep& second_rep) const OVERRIDE { SkBitmap blended = SkBitmapOperations::CreateBlendedBitmap( first_rep.sk_bitmap(), second_rep.sk_bitmap(), alpha_); return ImageSkiaRep(blended, first_rep.scale_factor()); } private: - const ImageSkia first_; - const ImageSkia second_; double alpha_; DISALLOW_COPY_AND_ASSIGN(BlendingImageSource); @@ -153,30 +179,25 @@ class TransparentImageSource : public gfx::ImageSkiaSource { DISALLOW_COPY_AND_ASSIGN(TransparentImageSource); }; -class MaskedImageSource : public gfx::ImageSkiaSource { +class MaskedImageSource : public BinaryImageSource { public: MaskedImageSource(const ImageSkia& rgb, const ImageSkia& alpha) - : rgb_(rgb), - alpha_(alpha) { + : BinaryImageSource(rgb, alpha, "MaskedImageSource") { } virtual ~MaskedImageSource() { } - // gfx::ImageSkiaSource overrides: - virtual ImageSkiaRep GetImageForScale(ui::ScaleFactor scale_factor) OVERRIDE { - ImageSkiaRep rgb_rep = rgb_.GetRepresentation(scale_factor); - ImageSkiaRep alpha_rep = alpha_.GetRepresentation(scale_factor); - MatchScale(&rgb_rep, &alpha_rep); + // BinaryImageSource overrides: + virtual ImageSkiaRep CreateImageSkiaRep( + const ImageSkiaRep& first_rep, + const ImageSkiaRep& second_rep) const OVERRIDE { return ImageSkiaRep(SkBitmapOperations::CreateMaskedBitmap( - rgb_rep.sk_bitmap(), alpha_rep.sk_bitmap()), - rgb_rep.scale_factor()); + first_rep.sk_bitmap(), second_rep.sk_bitmap()), + first_rep.scale_factor()); } private: - const ImageSkia rgb_; - const ImageSkia alpha_; - DISALLOW_COPY_AND_ASSIGN(MaskedImageSource); }; @@ -238,40 +259,35 @@ class HSLImageSource : public gfx::ImageSkiaSource { private: const gfx::ImageSkia image_; const color_utils::HSL hsl_shift_; - DISALLOW_COPY_AND_ASSIGN(HSLImageSource); }; // ImageSkiaSource which uses SkBitmapOperations::CreateButtonBackground // to generate image reps for the target image. -class ButtonImageSource: public gfx::ImageSkiaSource { +class ButtonImageSource: public BinaryImageSource { public: ButtonImageSource(SkColor color, const ImageSkia& image, const ImageSkia& mask) - : color_(color), - image_(image), - mask_(mask) { + : BinaryImageSource(image, mask, "ButtonImageSource"), + color_(color) { } virtual ~ButtonImageSource() { } - // gfx::ImageSkiaSource overrides: - virtual ImageSkiaRep GetImageForScale(ui::ScaleFactor scale_factor) OVERRIDE { - ImageSkiaRep image_rep = image_.GetRepresentation(scale_factor); - ImageSkiaRep mask_rep = mask_.GetRepresentation(scale_factor); - MatchScale(&image_rep, &mask_rep); + // BinaryImageSource overrides: + virtual ImageSkiaRep CreateImageSkiaRep( + const ImageSkiaRep& first_rep, + const ImageSkiaRep& second_rep) const OVERRIDE { return ImageSkiaRep( SkBitmapOperations::CreateButtonBackground(color_, - image_rep.sk_bitmap(), mask_rep.sk_bitmap()), - image_rep.scale_factor()); + first_rep.sk_bitmap(), second_rep.sk_bitmap()), + first_rep.scale_factor()); } private: const SkColor color_; - const ImageSkia image_; - const ImageSkia mask_; DISALLOW_COPY_AND_ASSIGN(ButtonImageSource); }; -- cgit v1.1