diff options
author | rsesek@chromium.org <rsesek@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-06-21 14:38:15 +0000 |
---|---|---|
committer | rsesek@chromium.org <rsesek@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-06-21 14:38:15 +0000 |
commit | bb48d6567151de71d74a1fe373787881fba1c28c (patch) | |
tree | 5f199c41d3eaefe3837620dee1ead721f1296be6 /ui | |
parent | 356df06e275399aa122e57388ba946a477611449 (diff) | |
download | chromium_src-bb48d6567151de71d74a1fe373787881fba1c28c.zip chromium_src-bb48d6567151de71d74a1fe373787881fba1c28c.tar.gz chromium_src-bb48d6567151de71d74a1fe373787881fba1c28c.tar.bz2 |
Augment the gfx::Image conversion methods to let callers choose between weak and strong refs.
This deprecates the operator-based conversion methods in favor of To/CopyType().
New callsites should use these methods. Other uses will be converted in a later
CL.
BUG=58030
TEST=ui_unittests
Review URL: http://codereview.chromium.org/7205031
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@89826 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'ui')
-rw-r--r-- | ui/gfx/image/image.cc | 48 | ||||
-rw-r--r-- | ui/gfx/image/image.h | 29 | ||||
-rw-r--r-- | ui/gfx/image/image_unittest.cc | 74 | ||||
-rw-r--r-- | ui/gfx/image/image_unittest_util.cc | 10 | ||||
-rw-r--r-- | ui/gfx/image/image_unittest_util.h | 2 |
5 files changed, 138 insertions, 25 deletions
diff --git a/ui/gfx/image/image.cc b/ui/gfx/image/image.cc index 41400c7..4d87944 100644 --- a/ui/gfx/image/image.cc +++ b/ui/gfx/image/image.cc @@ -234,26 +234,62 @@ Image& Image::operator=(const Image& other) { Image::~Image() { } -Image::operator const SkBitmap*() const { +const SkBitmap* Image::ToSkBitmap() const { internal::ImageRep* rep = GetRepresentation(Image::kImageRepSkia); return rep->AsImageRepSkia()->bitmap(); } +#if defined(TOOLKIT_USES_GTK) +GdkPixbuf* Image::ToGdkPixbuf() const { + internal::ImageRep* rep = GetRepresentation(Image::kImageRepGdk); + return rep->AsImageRepGdk()->pixbuf(); +} +#endif + +#if defined(OS_MACOSX) +NSImage* Image::ToNSImage() const { + internal::ImageRep* rep = GetRepresentation(Image::kImageRepCocoa); + return rep->AsImageRepCocoa()->image(); +} +#endif + +const SkBitmap* Image::CopySkBitmap() const { + return new SkBitmap(*ToSkBitmap()); +} + +#if defined(TOOLKIT_USES_GTK) +GdkPixbuf* Image::CopyGdkPixbuf() const { + GdkPixbuf* pixbuf = ToGdkPixbuf(); + g_object_ref(pixbuf); + return pixbuf; +} +#endif + +#if defined(OS_MACOSX) +NSImage* Image::CopyNSImage() const { + NSImage* image = ToNSImage(); + base::mac::NSObjectRetain(image); + return image; +} +#endif + +Image::operator const SkBitmap*() const { + return ToSkBitmap(); +} + Image::operator const SkBitmap&() const { - return *(this->operator const SkBitmap*()); + return *ToSkBitmap(); } #if defined(TOOLKIT_USES_GTK) Image::operator GdkPixbuf*() const { - internal::ImageRep* rep = GetRepresentation(Image::kImageRepGdk); - return rep->AsImageRepGdk()->pixbuf(); + return ToGdkPixbuf(); } #endif #if defined(OS_MACOSX) Image::operator NSImage*() const { - internal::ImageRep* rep = GetRepresentation(Image::kImageRepCocoa); - return rep->AsImageRepCocoa()->image(); + return ToNSImage(); } #endif diff --git a/ui/gfx/image/image.h b/ui/gfx/image/image.h index d08632d..a57b519 100644 --- a/ui/gfx/image/image.h +++ b/ui/gfx/image/image.h @@ -78,14 +78,39 @@ class Image { // representations. ~Image(); - // Conversion handlers. - operator const SkBitmap*() const ; + // Converts the Image to the desired representation and stores it internally. + // The returned result is a weak pointer owned by and scoped to the life of + // the Image. + const SkBitmap* ToSkBitmap() const; +#if defined(TOOLKIT_USES_GTK) + GdkPixbuf* ToGdkPixbuf() const; +#elif defined(OS_MACOSX) + NSImage* ToNSImage() const; +#endif + + // Performs a conversion, like above, but returns a copy of the result rather + // than a weak pointer. The caller is responsible for deleting the result. + // Note that the result is only a copy in terms of memory management; the + // backing pixels are shared amongst all copies (a fact of each of the + // converted representations, rather than a limitation imposed by Image) and + // so the result should be considered immutable. + const SkBitmap* CopySkBitmap() const; +#if defined(TOOLKIT_USES_GTK) + GdkPixbuf* CopyGdkPixbuf() const; +#elif defined(OS_MACOSX) + NSImage* CopyNSImage() const; +#endif + + // DEPRECATED ---------------------------------------------------------------- + // Conversion handlers. These wrap the ToType() variants. + operator const SkBitmap*() const; operator const SkBitmap&() const; #if defined(TOOLKIT_USES_GTK) operator GdkPixbuf*() const; #elif defined(OS_MACOSX) operator NSImage*() const; #endif + // --------------------------------------------------------------------------- // Gets the number of bitmaps in this image. This may cause a conversion // to a bitmap representation. Note, this function and GetSkBitmapAtIndex() diff --git a/ui/gfx/image/image_unittest.cc b/ui/gfx/image/image_unittest.cc index e3af1ee..2df0715 100644 --- a/ui/gfx/image/image_unittest.cc +++ b/ui/gfx/image/image_unittest.cc @@ -31,13 +31,13 @@ namespace gt = gfx::test; TEST_F(ImageTest, SkiaToSkia) { gfx::Image image(gt::CreateBitmap(25, 25)); - const SkBitmap* bitmap = static_cast<const SkBitmap*>(image); + const SkBitmap* bitmap = image.ToSkBitmap(); EXPECT_TRUE(bitmap); EXPECT_FALSE(bitmap->isNull()); EXPECT_EQ(1U, image.RepresentationCount()); // Make sure double conversion doesn't happen. - bitmap = static_cast<const SkBitmap*>(image); + bitmap = image.ToSkBitmap(); EXPECT_TRUE(bitmap); EXPECT_FALSE(bitmap->isNull()); EXPECT_EQ(1U, image.RepresentationCount()); @@ -54,7 +54,7 @@ TEST_F(ImageTest, SkiaToSkiaRef) { EXPECT_FALSE(bitmap.isNull()); EXPECT_EQ(1U, image.RepresentationCount()); - const SkBitmap* bitmap1 = static_cast<const SkBitmap*>(image); + const SkBitmap* bitmap1 = image.ToSkBitmap(); EXPECT_FALSE(bitmap1->isNull()); EXPECT_EQ(1U, image.RepresentationCount()); @@ -71,7 +71,7 @@ TEST_F(ImageTest, SkiaToPlatform) { if (!kUsesSkiaNatively) EXPECT_FALSE(image.HasRepresentation(gt::GetPlatformRepresentationType())); - EXPECT_TRUE(static_cast<gt::PlatformImage>(image)); + EXPECT_TRUE(gt::ToPlatformType(image)); EXPECT_EQ(kRepCount, image.RepresentationCount()); const SkBitmap& bitmap = static_cast<const SkBitmap&>(image); @@ -90,12 +90,12 @@ TEST_F(ImageTest, PlatformToSkia) { if (!kUsesSkiaNatively) EXPECT_FALSE(image.HasRepresentation(gfx::Image::kImageRepSkia)); - const SkBitmap* bitmap = static_cast<const SkBitmap*>(image); + const SkBitmap* bitmap = image.ToSkBitmap(); EXPECT_TRUE(bitmap); EXPECT_FALSE(bitmap->isNull()); EXPECT_EQ(kRepCount, image.RepresentationCount()); - EXPECT_TRUE(static_cast<gt::PlatformImage>(image)); + EXPECT_TRUE(gt::ToPlatformType(image)); EXPECT_EQ(kRepCount, image.RepresentationCount()); EXPECT_TRUE(image.HasRepresentation(gfx::Image::kImageRepSkia)); @@ -103,11 +103,11 @@ TEST_F(ImageTest, PlatformToSkia) { TEST_F(ImageTest, PlatformToPlatform) { gfx::Image image(gt::CreatePlatformImage()); - EXPECT_TRUE(static_cast<gt::PlatformImage>(image)); + EXPECT_TRUE(gt::ToPlatformType(image)); EXPECT_EQ(1U, image.RepresentationCount()); // Make sure double conversion doesn't happen. - EXPECT_TRUE(static_cast<gt::PlatformImage>(image)); + EXPECT_TRUE(gt::ToPlatformType(image)); EXPECT_EQ(1U, image.RepresentationCount()); EXPECT_TRUE(image.HasRepresentation(gt::GetPlatformRepresentationType())); @@ -115,6 +115,48 @@ TEST_F(ImageTest, PlatformToPlatform) { EXPECT_FALSE(image.HasRepresentation(gfx::Image::kImageRepSkia)); } +TEST_F(ImageTest, PlatformToSkiaToCopy) { + const SkBitmap* bitmap; + + { + gfx::Image image(gt::CreatePlatformImage()); + bitmap = image.CopySkBitmap(); + } + + EXPECT_TRUE(bitmap); + EXPECT_FALSE(bitmap->isNull()); + + delete bitmap; +} + +#if defined(TOOLKIT_USES_GTK) +TEST_F(ImageTest, SkiaToGdkCopy) { + GdkPixbuf* pixbuf; + + { + gfx::Image image(gt::CreateBitmap(25, 25)); + pixbuf = image.CopyGdkPixbuf(); + } + + EXPECT_TRUE(pixbuf); + g_object_unref(pixbuf); +} +#endif + +#if defined(OS_MACOSX) +TEST_F(ImageTest, SkiaToCocoaCopy) { + NSImage* ns_image; + + { + gfx::Image image(gt::CreateBitmap(25, 25)); + ns_image = image.CopyNSImage(); + } + + EXPECT_TRUE(ns_image); + base::mac::NSObjectRelease(ns_image); +} +#endif + TEST_F(ImageTest, CheckSkiaColor) { gfx::Image image(gt::CreatePlatformImage()); const SkBitmap& bitmap(image); @@ -133,14 +175,14 @@ TEST_F(ImageTest, SwapRepresentations) { gfx::Image image2(gt::CreatePlatformImage()); const SkBitmap* bitmap2 = image2; - gt::PlatformImage platform_image = static_cast<gt::PlatformImage>(image2); + gt::PlatformImage platform_image = gt::ToPlatformType(image2); EXPECT_EQ(kRepCount, image2.RepresentationCount()); image1.SwapRepresentations(&image2); - EXPECT_EQ(bitmap2, static_cast<const SkBitmap*>(image1)); - EXPECT_EQ(platform_image, static_cast<gt::PlatformImage>(image1)); - EXPECT_EQ(bitmap1, static_cast<const SkBitmap*>(image2)); + EXPECT_EQ(bitmap2, image1.ToSkBitmap()); + EXPECT_EQ(platform_image, gt::ToPlatformType(image1)); + EXPECT_EQ(bitmap1, image2.ToSkBitmap()); EXPECT_EQ(kRepCount, image1.RepresentationCount()); EXPECT_EQ(1U, image2.RepresentationCount()); } @@ -153,10 +195,9 @@ TEST_F(ImageTest, Copy) { EXPECT_EQ(1U, image1.RepresentationCount()); EXPECT_EQ(1U, image2.RepresentationCount()); - EXPECT_EQ(static_cast<const SkBitmap*>(image1), - static_cast<const SkBitmap*>(image2)); + EXPECT_EQ(image1.ToSkBitmap(), image2.ToSkBitmap()); - EXPECT_TRUE(static_cast<gt::PlatformImage>(image2)); + EXPECT_TRUE(gt::ToPlatformType(image2)); EXPECT_EQ(kRepCount, image2.RepresentationCount()); EXPECT_EQ(kRepCount, image1.RepresentationCount()); } @@ -167,8 +208,7 @@ TEST_F(ImageTest, Assign) { EXPECT_EQ(1U, image1.RepresentationCount()); EXPECT_EQ(1U, image2.RepresentationCount()); - EXPECT_EQ(static_cast<const SkBitmap*>(image1), - static_cast<const SkBitmap*>(image2)); + EXPECT_EQ(image1.ToSkBitmap(), image2.ToSkBitmap()); } TEST_F(ImageTest, MultiResolutionSkBitmap) { diff --git a/ui/gfx/image/image_unittest_util.cc b/ui/gfx/image/image_unittest_util.cc index 065587e..8690542 100644 --- a/ui/gfx/image/image_unittest_util.cc +++ b/ui/gfx/image/image_unittest_util.cc @@ -51,5 +51,15 @@ gfx::Image::RepresentationType GetPlatformRepresentationType() { #endif } +PlatformImage ToPlatformType(const gfx::Image& image) { +#if defined(OS_MACOSX) + return image.ToNSImage(); +#elif defined(TOOLKIT_GTK) + return image.ToGdkPixbuf(); +#else + return image.ToSkBitmap(); +#endif +} + } // namespace test } // namespace gfx diff --git a/ui/gfx/image/image_unittest_util.h b/ui/gfx/image/image_unittest_util.h index dcf6bf3..e46c412 100644 --- a/ui/gfx/image/image_unittest_util.h +++ b/ui/gfx/image/image_unittest_util.h @@ -27,6 +27,8 @@ PlatformImage CreatePlatformImage(); gfx::Image::RepresentationType GetPlatformRepresentationType(); +PlatformImage ToPlatformType(const gfx::Image& image); + } // namespace test } // namespace gfx |