summaryrefslogtreecommitdiffstats
path: root/ui/gfx
diff options
context:
space:
mode:
authoroshima@chromium.org <oshima@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-07-10 22:53:09 +0000
committeroshima@chromium.org <oshima@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-07-10 22:53:09 +0000
commit831d9ba79c56fc142147b88287e787ed38395b76 (patch)
tree1e3f9534472a9b76d874ef3a889db481f6fec036 /ui/gfx
parentc192ea0ece21d496cf0caba87d09993eb1c326ce (diff)
downloadchromium_src-831d9ba79c56fc142147b88287e787ed38395b76.zip
chromium_src-831d9ba79c56fc142147b88287e787ed38395b76.tar.gz
chromium_src-831d9ba79c56fc142147b88287e787ed38395b76.tar.bz2
Make tab/tabstrip high density compatible
added ImageSkiaOperations and replaced Bitmap with ImageSkiaRep/ImagSkia. BUG=122992 TEST=manual Review URL: https://chromiumcodereview.appspot.com/10704113 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@145976 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'ui/gfx')
-rw-r--r--ui/gfx/image/image_skia_operations.cc167
-rw-r--r--ui/gfx/image/image_skia_operations.h43
2 files changed, 210 insertions, 0 deletions
diff --git a/ui/gfx/image/image_skia_operations.cc b/ui/gfx/image/image_skia_operations.cc
new file mode 100644
index 0000000..cbe2c8e
--- /dev/null
+++ b/ui/gfx/image/image_skia_operations.cc
@@ -0,0 +1,167 @@
+// Copyright (c) 2012 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "ui/gfx/image/image_skia_operations.h"
+
+#include "base/command_line.h"
+#include "base/logging.h"
+#include "skia/ext/platform_canvas.h"
+#include "ui/base/layout.h"
+#include "ui/base/ui_base_switches.h"
+#include "ui/gfx/image/image_skia.h"
+#include "ui/gfx/image/image_skia_rep.h"
+#include "ui/gfx/image/image_skia_source.h"
+#include "ui/gfx/rect.h"
+#include "ui/gfx/size.h"
+#include "ui/gfx/skbitmap_operations.h"
+#include "ui/gfx/skia_util.h"
+
+namespace gfx {
+namespace {
+
+bool ScalingEnabled() {
+ static bool scale_images = !CommandLine::ForCurrentProcess()->HasSwitch(
+ switches::kDisableScalingInImageSkiaOperations);
+ return scale_images;
+}
+
+// Creates 2x scaled image of the give |source|.
+ImageSkiaRep Create2XImageSkiaRep(const ImageSkiaRep& source) {
+ gfx::Size size(source.GetWidth() * 2.0f, source.GetHeight() * 2.0f);
+ skia::PlatformCanvas canvas(size.width(), size.height(), false);
+ SkRect resized_bounds = RectToSkRect(gfx::Rect(size));
+ canvas.drawBitmapRect(source.sk_bitmap(), NULL, resized_bounds);
+ SkBitmap resized_bitmap = canvas.getDevice()->accessBitmap(false);
+ return ImageSkiaRep(resized_bitmap, ui::SCALE_FACTOR_200P);
+}
+
+// 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;
+ } else {
+ target = second;
+ }
+ *target = Create2XImageSkiaRep(*target);
+ }
+}
+
+class BlendingImageSource : public gfx::ImageSkiaSource {
+ public:
+ BlendingImageSource(const ImageSkia& first,
+ const ImageSkia& second,
+ double alpha)
+ : first_(first),
+ second_(second),
+ alpha_(alpha) {
+ }
+
+ // 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);
+ 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);
+};
+
+class MaskedImageSource : public gfx::ImageSkiaSource {
+ public:
+ MaskedImageSource(const ImageSkia& rgb, const ImageSkia& alpha)
+ : rgb_(rgb),
+ alpha_(alpha) {
+ }
+
+ // 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);
+ return ImageSkiaRep(SkBitmapOperations::CreateMaskedBitmap(
+ rgb_rep.sk_bitmap(), alpha_rep.sk_bitmap()),
+ rgb_rep.scale_factor());
+ }
+
+ private:
+ const ImageSkia rgb_;
+ const ImageSkia alpha_;
+
+ DISALLOW_COPY_AND_ASSIGN(MaskedImageSource);
+};
+
+class TiledImageSource : public gfx::ImageSkiaSource {
+ public:
+ TiledImageSource(const ImageSkia& source,
+ int src_x, int src_y,
+ int dst_w, int dst_h)
+ : source_(source),
+ src_x_(src_x),
+ src_y_(src_y),
+ dst_w_(dst_w),
+ dst_h_(dst_h) {
+ }
+
+ // gfx::ImageSkiaSource overrides:
+ virtual ImageSkiaRep GetImageForScale(ui::ScaleFactor scale_factor) OVERRIDE {
+ ImageSkiaRep source_rep = source_.GetRepresentation(scale_factor);
+ float scale = ui::GetScaleFactorScale(source_rep.scale_factor());
+ return ImageSkiaRep(
+ SkBitmapOperations::CreateTiledBitmap(
+ source_rep.sk_bitmap(),
+ src_x_ * scale, src_y_ * scale, dst_w_ * scale, dst_h_ * scale),
+ source_rep.scale_factor());
+ }
+
+ private:
+ const ImageSkia& source_;
+ const int src_x_;
+ const int src_y_;
+ const int dst_w_;
+ const int dst_h_;
+
+ DISALLOW_COPY_AND_ASSIGN(TiledImageSource);
+};
+
+} // namespace;
+
+// static
+ImageSkia ImageSkiaOperations::CreateBlendedImage(const ImageSkia& first,
+ const ImageSkia& second,
+ double alpha) {
+ return ImageSkia(new BlendingImageSource(first, second, alpha), first.size());
+}
+
+// static
+ImageSkia ImageSkiaOperations::CreateMaskedImage(const ImageSkia& rgb,
+ const ImageSkia& alpha) {
+ return ImageSkia(new MaskedImageSource(rgb, alpha), rgb.size());
+}
+
+// static
+ImageSkia ImageSkiaOperations::CreateTiledImage(const ImageSkia& source,
+ int src_x, int src_y,
+ int dst_w, int dst_h) {
+ return ImageSkia(new TiledImageSource(source, src_x, src_y, dst_w, dst_h),
+ gfx::Size(dst_w, dst_h));
+}
+
+} // namespace gfx
diff --git a/ui/gfx/image/image_skia_operations.h b/ui/gfx/image/image_skia_operations.h
new file mode 100644
index 0000000..24a5b59
--- /dev/null
+++ b/ui/gfx/image/image_skia_operations.h
@@ -0,0 +1,43 @@
+// Copyright (c) 2012 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef UI_GFX_IMAGESKIA_OPERATIONS_H_
+#define UI_GFX_IMAGESKIA_OPERATIONS_H_
+#pragma once
+
+#include "base/gtest_prod_util.h"
+#include "ui/base/ui_export.h"
+
+namespace gfx {
+class ImageSkia;
+
+class UI_EXPORT ImageSkiaOperations {
+ public:
+ // Create an image that is a blend of two others. The alpha argument
+ // specifies the opacity of the second imag. The provided image must
+ // use the kARGB_8888_Config config and be of equal dimensions.
+ static ImageSkia CreateBlendedImage(const ImageSkia& first,
+ const ImageSkia& second,
+ double alpha);
+
+ // Create an image that is the original image masked out by the mask defined
+ // in the alpha image. The images must use the kARGB_8888_Config config and
+ // be of equal dimensions.
+ static ImageSkia CreateMaskedImage(const ImageSkia& first,
+ const ImageSkia& alpha);
+
+ // Create an image that is cropped from another image. This is special
+ // because it tiles the original image, so your coordinates can extend
+ // outside the bounds of the original image.
+ static ImageSkia CreateTiledImage(const ImageSkia& image,
+ int src_x, int src_y,
+ int dst_w, int dst_h);
+
+ private:
+ ImageSkiaOperations(); // Class for scoping only.
+};
+
+}
+
+#endif // UI_GFX_IMAGESKIA_OPERATIONS_H_