blob: 58cb900ffb6b03505e8428df044cc25be2b68fa3 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
|
// 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 UI_GFX_SCOPED_IMAGE_H_
#define UI_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 <glib-object.h>
#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 ImageType>
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 ImageType = gfx::NativeImageType>
class ScopedImage : public gfx::internal::ScopedImage<ImageType> {
public:
explicit ScopedImage(gfx::NativeImage image)
: gfx::internal::ScopedImage<ImageType>(image) {}
private:
DISALLOW_COPY_AND_ASSIGN(ScopedImage<ImageType>);
};
// Specialization for SkBitmap on all platforms.
template <>
class ScopedImage<SkBitmap> : public gfx::internal::ScopedImage<SkBitmap> {
public:
explicit ScopedImage(SkBitmap* image)
: gfx::internal::ScopedImage<SkBitmap>(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<NSImage> : public gfx::internal::ScopedImage<NSImage> {
public:
explicit ScopedImage(NSImage* image)
: gfx::internal::ScopedImage<NSImage>(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<GdkPixbuf> : public gfx::internal::ScopedImage<GdkPixbuf> {
public:
explicit ScopedImage(GdkPixbuf* image)
: gfx::internal::ScopedImage<GdkPixbuf>(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 // UI_GFX_SCOPED_IMAGE_H_
|