// Copyright (c) 2010 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 GFX_SCOPED_IMAGE_H_ #define GFX_SCOPED_IMAGE_H_ #pragma once #include "base/basictypes.h" #include "build/build_config.h" #include "gfx/native_widget_types.h" #include "third_party/skia/include/core/SkBitmap.h" #if defined(OS_LINUX) #include #elif defined(OS_MACOSX) #include "base/mac/mac_util.h" #endif namespace gfx { namespace internal { // ScopedImage is class that encapsulates one of the three platform-specific // images used: SkBitmap, NSImage, and GdkPixbuf. This is the abstract interface // that all ScopedImages respond to. This wrapper expects to own the image it // holds, unless it is Release()ed or Free()ed. // // This class is abstract and callers should use the specialized versions below, // which are not in the internal namespace. template class ScopedImage { public: virtual ~ScopedImage() {} // Frees the actual image that this boxes. virtual void Free() = 0; // Returns the image that this boxes. ImageType* Get() { return image_; } // Frees the current image and sets a new one. void Set(ImageType* new_image) { Free(); image_ = new_image; } // Returns the image this boxes and relinquishes ownership. ImageType* Release() { ImageType* tmp = image_; image_ = NULL; return tmp; } protected: explicit ScopedImage(ImageType* image) : image_(image) {} ImageType* image_; private: DISALLOW_COPY_AND_ASSIGN(ScopedImage); }; } // namespace internal // Generic template. template class ScopedImage : public gfx::internal::ScopedImage { public: explicit ScopedImage(gfx::NativeImage image) : gfx::internal::ScopedImage(image) {} private: DISALLOW_COPY_AND_ASSIGN(ScopedImage); }; // Specialization for SkBitmap on all platforms. template <> class ScopedImage : public gfx::internal::ScopedImage { public: explicit ScopedImage(SkBitmap* image) : gfx::internal::ScopedImage(image) {} virtual ~ScopedImage() { Free(); } virtual void Free() { delete image_; image_ = NULL; } private: DISALLOW_COPY_AND_ASSIGN(ScopedImage); }; // Specialization for the NSImage type on Mac OS X. #if defined(OS_MACOSX) template <> class ScopedImage : public gfx::internal::ScopedImage { public: explicit ScopedImage(NSImage* image) : gfx::internal::ScopedImage(image) {} virtual ~ScopedImage() { Free(); } virtual void Free() { base::mac::NSObjectRelease(image_); image_ = NULL; } private: DISALLOW_COPY_AND_ASSIGN(ScopedImage); }; #endif // defined(OS_MACOSX) // Specialization for the GdkPixbuf type on Linux. #if defined(OS_LINUX) template <> class ScopedImage : public gfx::internal::ScopedImage { public: explicit ScopedImage(GdkPixbuf* image) : gfx::internal::ScopedImage(image) {} virtual ~ScopedImage() { Free(); } virtual void Free() { if (image_) { g_object_unref(image_); image_ = NULL; } } private: DISALLOW_COPY_AND_ASSIGN(ScopedImage); }; #endif // defined(OS_LINUX) // Typedef ScopedNativeImage to the default template argument. This allows for // easy exchange between gfx::NativeImage and a gfx::ScopedNativeImage. typedef ScopedImage<> ScopedNativeImage; } // namespace gfx #endif // GFX_SCOPED_IMAGE_H_