OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef GFX_SCOPED_IMAGE_H_ | 5 #ifndef GFX_SCOPED_IMAGE_H_ |
6 #define GFX_SCOPED_IMAGE_H_ | 6 #define GFX_SCOPED_IMAGE_H_ |
7 #pragma once | 7 #pragma once |
8 | 8 |
9 #include "base/basictypes.h" | 9 #include "ui/gfx/scoped_image.h" |
10 #include "build/build_config.h" | 10 // TODO(sail): remove this file once all includes have been updated. |
11 #include "gfx/native_widget_types.h" | |
12 #include "third_party/skia/include/core/SkBitmap.h" | |
13 | |
14 #if defined(OS_LINUX) | |
15 #include <glib-object.h> | |
16 #elif defined(OS_MACOSX) | |
17 #include "base/mac/mac_util.h" | |
18 #endif | |
19 | |
20 namespace gfx { | |
21 | |
22 namespace internal { | |
23 | |
24 // ScopedImage is class that encapsulates one of the three platform-specific | |
25 // images used: SkBitmap, NSImage, and GdkPixbuf. This is the abstract interface | |
26 // that all ScopedImages respond to. This wrapper expects to own the image it | |
27 // holds, unless it is Release()ed or Free()ed. | |
28 // | |
29 // This class is abstract and callers should use the specialized versions below, | |
30 // which are not in the internal namespace. | |
31 template <class ImageType> | |
32 class ScopedImage { | |
33 public: | |
34 virtual ~ScopedImage() {} | |
35 | |
36 // Frees the actual image that this boxes. | |
37 virtual void Free() = 0; | |
38 | |
39 // Returns the image that this boxes. | |
40 ImageType* Get() { | |
41 return image_; | |
42 } | |
43 | |
44 // Frees the current image and sets a new one. | |
45 void Set(ImageType* new_image) { | |
46 Free(); | |
47 image_ = new_image; | |
48 } | |
49 | |
50 // Returns the image this boxes and relinquishes ownership. | |
51 ImageType* Release() { | |
52 ImageType* tmp = image_; | |
53 image_ = NULL; | |
54 return tmp; | |
55 } | |
56 | |
57 protected: | |
58 explicit ScopedImage(ImageType* image) : image_(image) {} | |
59 ImageType* image_; | |
60 | |
61 private: | |
62 DISALLOW_COPY_AND_ASSIGN(ScopedImage); | |
63 }; | |
64 | |
65 } // namespace internal | |
66 | |
67 // Generic template. | |
68 template <class ImageType = gfx::NativeImageType> | |
69 class ScopedImage : public gfx::internal::ScopedImage<ImageType> { | |
70 public: | |
71 explicit ScopedImage(gfx::NativeImage image) | |
72 : gfx::internal::ScopedImage<ImageType>(image) {} | |
73 | |
74 private: | |
75 DISALLOW_COPY_AND_ASSIGN(ScopedImage<ImageType>); | |
76 }; | |
77 | |
78 // Specialization for SkBitmap on all platforms. | |
79 template <> | |
80 class ScopedImage<SkBitmap> : public gfx::internal::ScopedImage<SkBitmap> { | |
81 public: | |
82 explicit ScopedImage(SkBitmap* image) | |
83 : gfx::internal::ScopedImage<SkBitmap>(image) {} | |
84 virtual ~ScopedImage() { | |
85 Free(); | |
86 } | |
87 | |
88 virtual void Free() { | |
89 delete image_; | |
90 image_ = NULL; | |
91 } | |
92 | |
93 private: | |
94 DISALLOW_COPY_AND_ASSIGN(ScopedImage); | |
95 }; | |
96 | |
97 // Specialization for the NSImage type on Mac OS X. | |
98 #if defined(OS_MACOSX) | |
99 template <> | |
100 class ScopedImage<NSImage> : public gfx::internal::ScopedImage<NSImage> { | |
101 public: | |
102 explicit ScopedImage(NSImage* image) | |
103 : gfx::internal::ScopedImage<NSImage>(image) {} | |
104 virtual ~ScopedImage() { | |
105 Free(); | |
106 } | |
107 | |
108 virtual void Free() { | |
109 base::mac::NSObjectRelease(image_); | |
110 image_ = NULL; | |
111 } | |
112 | |
113 private: | |
114 DISALLOW_COPY_AND_ASSIGN(ScopedImage); | |
115 }; | |
116 #endif // defined(OS_MACOSX) | |
117 | |
118 // Specialization for the GdkPixbuf type on Linux. | |
119 #if defined(OS_LINUX) | |
120 template <> | |
121 class ScopedImage<GdkPixbuf> : public gfx::internal::ScopedImage<GdkPixbuf> { | |
122 public: | |
123 explicit ScopedImage(GdkPixbuf* image) | |
124 : gfx::internal::ScopedImage<GdkPixbuf>(image) {} | |
125 virtual ~ScopedImage() { | |
126 Free(); | |
127 } | |
128 | |
129 virtual void Free() { | |
130 if (image_) { | |
131 g_object_unref(image_); | |
132 image_ = NULL; | |
133 } | |
134 } | |
135 | |
136 private: | |
137 DISALLOW_COPY_AND_ASSIGN(ScopedImage); | |
138 }; | |
139 #endif // defined(OS_LINUX) | |
140 | |
141 // Typedef ScopedNativeImage to the default template argument. This allows for | |
142 // easy exchange between gfx::NativeImage and a gfx::ScopedNativeImage. | |
143 typedef ScopedImage<> ScopedNativeImage; | |
144 | |
145 } // namespace gfx | |
146 | 11 |
147 #endif // GFX_SCOPED_IMAGE_H_ | 12 #endif // GFX_SCOPED_IMAGE_H_ |
OLD | NEW |