diff options
author | pkotwicz@chromium.org <pkotwicz@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-07-23 20:35:07 +0000 |
---|---|---|
committer | pkotwicz@chromium.org <pkotwicz@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-07-23 20:35:07 +0000 |
commit | 474a0984f645ad944d37e3dc14c52b72fb6a74ad (patch) | |
tree | 07ccbe68dd58150fe2876820654284421cd592fd /ui/views/controls | |
parent | 8fedfee482c62619a504a59316fb07a3b0f1995a (diff) | |
download | chromium_src-474a0984f645ad944d37e3dc14c52b72fb6a74ad.zip chromium_src-474a0984f645ad944d37e3dc14c52b72fb6a74ad.tar.gz chromium_src-474a0984f645ad944d37e3dc14c52b72fb6a74ad.tar.bz2 |
Implement remaining SkBitmapOperations as ImageSkiaOperations
Bug=None
Test=Compiles, code looks nicer
R=oshima,sky
TBR=sadrul
Review URL: https://chromiumcodereview.appspot.com/10704199
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@147925 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'ui/views/controls')
-rw-r--r-- | ui/views/controls/button/image_button.cc | 67 | ||||
-rw-r--r-- | ui/views/controls/button/image_button.h | 16 | ||||
-rw-r--r-- | ui/views/controls/button/image_button_unittest.cc | 22 |
3 files changed, 26 insertions, 79 deletions
diff --git a/ui/views/controls/button/image_button.cc b/ui/views/controls/button/image_button.cc index a1b05c8..1640f99 100644 --- a/ui/views/controls/button/image_button.cc +++ b/ui/views/controls/button/image_button.cc @@ -7,8 +7,7 @@ #include "base/utf_string_conversions.h" #include "ui/base/animation/throb_animation.h" #include "ui/gfx/canvas.h" -#include "ui/gfx/screen.h" -#include "ui/gfx/skbitmap_operations.h" +#include "ui/gfx/image/image_skia_operations.h" #include "ui/views/widget/widget.h" namespace views { @@ -41,10 +40,13 @@ void ImageButton::SetImage(ButtonState state, const gfx::ImageSkia* image) { void ImageButton::SetBackground(SkColor color, const gfx::ImageSkia* image, const gfx::ImageSkia* mask) { - background_image_.src_color_ = color; - background_image_.src_image_ = image ? *image : gfx::ImageSkia(); - background_image_.src_mask_ = mask ? *mask : gfx::ImageSkia(); - background_image_.result_ = gfx::ImageSkia(); + if (image == NULL || mask == NULL) { + background_image_ = gfx::ImageSkia(); + return; + } + + background_image_ = gfx::ImageSkiaOperations::CreateButtonBackground(color, + *image, *mask); } void ImageButton::SetOverlayImage(const gfx::ImageSkia* image) { @@ -75,8 +77,7 @@ void ImageButton::OnPaint(gfx::Canvas* canvas) { // Call the base class first to paint any background/borders. View::OnPaint(canvas); - ui::ScaleFactor current_device_scale_factor = canvas->scale_factor(); - gfx::ImageSkia img = GetImageToPaint(current_device_scale_factor); + gfx::ImageSkia img = GetImageToPaint(); if (!img.isNull()) { int x = 0, y = 0; @@ -91,12 +92,8 @@ void ImageButton::OnPaint(gfx::Canvas* canvas) { else if (v_alignment_ == ALIGN_BOTTOM) y = height() - img.height(); - if (!background_image_.result_.HasRepresentation( - current_device_scale_factor)) { - UpdateButtonBackground(current_device_scale_factor); - } - if (!background_image_.result_.empty()) - canvas->DrawImageInt(background_image_.result_, x, y); + if (!background_image_.isNull()) + canvas->DrawImageInt(background_image_, x, y); canvas->DrawImageInt(img, x, y); @@ -109,21 +106,12 @@ void ImageButton::OnPaint(gfx::Canvas* canvas) { //////////////////////////////////////////////////////////////////////////////// // ImageButton, protected: -gfx::ImageSkia ImageButton::GetImageToPaint(ui::ScaleFactor scale_factor) { +gfx::ImageSkia ImageButton::GetImageToPaint() { gfx::ImageSkia img; if (!images_[BS_HOT].isNull() && hover_animation_->is_animating()) { - gfx::ImageSkiaRep normal_image_rep = images_[BS_NORMAL].GetRepresentation( - scale_factor); - gfx::ImageSkiaRep hot_image_rep = images_[BS_HOT].GetRepresentation( - scale_factor); - DCHECK_EQ(normal_image_rep.scale_factor(), hot_image_rep.scale_factor()); - SkBitmap blended_bitmap = SkBitmapOperations::CreateBlendedBitmap( - normal_image_rep.sk_bitmap(), - hot_image_rep.sk_bitmap(), - hover_animation_->GetCurrentValue()); - img = gfx::ImageSkia(gfx::ImageSkiaRep(blended_bitmap, - normal_image_rep.scale_factor())); + img = gfx::ImageSkiaOperations::CreateBlendedImage(images_[BS_NORMAL], + images_[BS_HOT], hover_animation_->GetCurrentValue()); } else { img = images_[state_]; } @@ -131,24 +119,6 @@ gfx::ImageSkia ImageButton::GetImageToPaint(ui::ScaleFactor scale_factor) { return !img.isNull() ? img : images_[BS_NORMAL]; } -void ImageButton::UpdateButtonBackground(ui::ScaleFactor scale_factor) { - gfx::ImageSkiaRep image_rep = - background_image_.src_image_.GetRepresentation(scale_factor); - gfx::ImageSkiaRep mask_image_rep = - background_image_.src_mask_.GetRepresentation(scale_factor); - if (image_rep.is_null() || mask_image_rep.is_null() || - background_image_.result_.HasRepresentation(image_rep.scale_factor())) { - return; - } - DCHECK_EQ(image_rep.scale_factor(), mask_image_rep.scale_factor()); - SkBitmap result = SkBitmapOperations::CreateButtonBackground( - background_image_.src_color_, - image_rep.sk_bitmap(), - mask_image_rep.sk_bitmap()); - background_image_.result_.AddRepresentation(gfx::ImageSkiaRep( - result, image_rep.scale_factor())); -} - //////////////////////////////////////////////////////////////////////////////// // ToggleImageButton, public: @@ -215,13 +185,4 @@ bool ToggleImageButton::GetTooltipText(const gfx::Point& p, return true; } -/////////////////////////////////////////////////////////////////////////////// -// struct BackgroundImageGenerationInfo -ImageButton::BackgroundImageGenerationInfo::BackgroundImageGenerationInfo() - : src_color_(0) { -} - -ImageButton::BackgroundImageGenerationInfo::~BackgroundImageGenerationInfo() { -} - } // namespace views diff --git a/ui/views/controls/button/image_button.h b/ui/views/controls/button/image_button.h index 4ec203b..215a9ac 100644 --- a/ui/views/controls/button/image_button.h +++ b/ui/views/controls/button/image_button.h @@ -64,9 +64,7 @@ class VIEWS_EXPORT ImageButton : public CustomButton { protected: // Returns the image to paint. This is invoked from paint and returns a value // from images. - // |scale_factor| is the scale factor at which the view is painted and the - // scale factor which should be used when mutating ImageSkias. - virtual gfx::ImageSkia GetImageToPaint(ui::ScaleFactor scale_factor); + virtual gfx::ImageSkia GetImageToPaint(); // Updates button background for |scale_factor|. void UpdateButtonBackground(ui::ScaleFactor scale_factor); @@ -74,17 +72,7 @@ class VIEWS_EXPORT ImageButton : public CustomButton { // The images used to render the different states of this button. gfx::ImageSkia images_[BS_COUNT]; - // Information necessary to generate background image. - struct BackgroundImageGenerationInfo { - BackgroundImageGenerationInfo(); - ~BackgroundImageGenerationInfo(); - - SkColor src_color_; - gfx::ImageSkia src_image_; - gfx::ImageSkia src_mask_; - gfx::ImageSkia result_; - }; - BackgroundImageGenerationInfo background_image_; + gfx::ImageSkia background_image_; // Image to draw on top of normal / hot / pushed image. Usually empty. gfx::ImageSkia overlay_image_; diff --git a/ui/views/controls/button/image_button_unittest.cc b/ui/views/controls/button/image_button_unittest.cc index 617a855..d95d79b 100644 --- a/ui/views/controls/button/image_button_unittest.cc +++ b/ui/views/controls/button/image_button_unittest.cc @@ -25,10 +25,8 @@ typedef ViewsTestBase ImageButtonTest; TEST_F(ImageButtonTest, Basics) { ImageButton button(NULL); - ui::ScaleFactor kRequestedScaleFactor = ui::SCALE_FACTOR_100P; - // Our image to paint starts empty. - EXPECT_TRUE(button.GetImageToPaint(kRequestedScaleFactor).empty()); + EXPECT_TRUE(button.GetImageToPaint().empty()); // Without a theme, buttons are 16x14 by default. EXPECT_EQ("16x14", button.GetPreferredSize().ToString()); @@ -42,9 +40,9 @@ TEST_F(ImageButtonTest, Basics) { button.SetImage(CustomButton::BS_NORMAL, &normal_image); // Image uses normal image for painting. - EXPECT_FALSE(button.GetImageToPaint(kRequestedScaleFactor).empty()); - EXPECT_EQ(10, button.GetImageToPaint(kRequestedScaleFactor).width()); - EXPECT_EQ(20, button.GetImageToPaint(kRequestedScaleFactor).height()); + EXPECT_FALSE(button.GetImageToPaint().empty()); + EXPECT_EQ(10, button.GetImageToPaint().width()); + EXPECT_EQ(20, button.GetImageToPaint().height()); // Preferred size is the normal button size. EXPECT_EQ("10x20", button.GetPreferredSize().ToString()); @@ -58,9 +56,9 @@ TEST_F(ImageButtonTest, Basics) { EXPECT_EQ("10x20", button.GetPreferredSize().ToString()); // We're still painting the normal image. - EXPECT_FALSE(button.GetImageToPaint(kRequestedScaleFactor).empty()); - EXPECT_EQ(10, button.GetImageToPaint(kRequestedScaleFactor).width()); - EXPECT_EQ(20, button.GetImageToPaint(kRequestedScaleFactor).height()); + EXPECT_FALSE(button.GetImageToPaint().empty()); + EXPECT_EQ(10, button.GetImageToPaint().width()); + EXPECT_EQ(20, button.GetImageToPaint().height()); // Set an overlay image. gfx::ImageSkia overlay_image = CreateTestImage(12, 22); @@ -73,9 +71,9 @@ TEST_F(ImageButtonTest, Basics) { EXPECT_EQ("10x20", button.GetPreferredSize().ToString()); // We're still painting the normal image. - EXPECT_FALSE(button.GetImageToPaint(kRequestedScaleFactor).empty()); - EXPECT_EQ(10, button.GetImageToPaint(kRequestedScaleFactor).width()); - EXPECT_EQ(20, button.GetImageToPaint(kRequestedScaleFactor).height()); + EXPECT_FALSE(button.GetImageToPaint().empty()); + EXPECT_EQ(10, button.GetImageToPaint().width()); + EXPECT_EQ(20, button.GetImageToPaint().height()); // Reset the overlay image. button.SetOverlayImage(NULL); |