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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
|
// Copyright (c) 2011 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.
#include "ui/gfx/image.h"
#include <algorithm>
#include "base/logging.h"
#include "base/stl_util-inl.h"
#include "third_party/skia/include/core/SkBitmap.h"
#if defined(TOOLKIT_USES_GTK)
#include <gdk-pixbuf/gdk-pixbuf.h>
#include <glib-object.h>
#include "ui/gfx/canvas_skia.h"
#include "ui/gfx/gtk_util.h"
#elif defined(OS_MACOSX)
#include "base/mac/mac_util.h"
#include "skia/ext/skia_utils_mac.h"
#endif
namespace gfx {
namespace internal {
#if defined(OS_MACOSX)
// This is a wrapper around gfx::NSImageToSkBitmap() because this cross-platform
// file cannot include the [square brackets] of ObjC.
bool NSImageToSkBitmaps(NSImage* image, std::vector<const SkBitmap*>* bitmaps);
#endif
#if defined(TOOLKIT_USES_GTK)
const SkBitmap* GdkPixbufToSkBitmap(GdkPixbuf* pixbuf) {
gfx::CanvasSkia canvas(gdk_pixbuf_get_width(pixbuf),
gdk_pixbuf_get_height(pixbuf),
/*is_opaque=*/false);
canvas.DrawGdkPixbuf(pixbuf, 0, 0);
return new SkBitmap(canvas.ExtractBitmap());
}
#endif
class ImageRepSkia;
class ImageRepGdk;
class ImageRepCocoa;
// An ImageRep is the object that holds the backing memory for an Image. Each
// RepresentationType has an ImageRep subclass that is responsible for freeing
// the memory that the ImageRep holds. When an ImageRep is created, it expects
// to take ownership of the image, without having to retain it or increase its
// reference count.
class ImageRep {
public:
explicit ImageRep(Image::RepresentationType rep) : type_(rep) {}
// Deletes the associated pixels of an ImageRep.
virtual ~ImageRep() {}
// Cast helpers ("fake RTTI").
ImageRepSkia* AsImageRepSkia() {
CHECK_EQ(type_, Image::kImageRepSkia);
return reinterpret_cast<ImageRepSkia*>(this);
}
#if defined(TOOLKIT_USES_GTK)
ImageRepGdk* AsImageRepGdk() {
CHECK_EQ(type_, Image::kImageRepGdk);
return reinterpret_cast<ImageRepGdk*>(this);
}
#endif
#if defined(OS_MACOSX)
ImageRepCocoa* AsImageRepCocoa() {
CHECK_EQ(type_, Image::kImageRepCocoa);
return reinterpret_cast<ImageRepCocoa*>(this);
}
#endif
Image::RepresentationType type() const { return type_; }
private:
Image::RepresentationType type_;
};
class ImageRepSkia : public ImageRep {
public:
explicit ImageRepSkia(const SkBitmap* bitmap)
: ImageRep(Image::kImageRepSkia) {
CHECK(bitmap);
bitmaps_.push_back(bitmap);
}
explicit ImageRepSkia(const std::vector<const SkBitmap*>& bitmaps)
: ImageRep(Image::kImageRepSkia),
bitmaps_(bitmaps) {
CHECK(!bitmaps_.empty());
}
virtual ~ImageRepSkia() {
STLDeleteElements(&bitmaps_);
}
const SkBitmap* bitmap() const { return bitmaps_[0]; }
const std::vector<const SkBitmap*>& bitmaps() const { return bitmaps_; }
private:
std::vector<const SkBitmap*> bitmaps_;
DISALLOW_COPY_AND_ASSIGN(ImageRepSkia);
};
#if defined(TOOLKIT_USES_GTK)
class ImageRepGdk : public ImageRep {
public:
explicit ImageRepGdk(GdkPixbuf* pixbuf)
: ImageRep(Image::kImageRepGdk),
pixbuf_(pixbuf) {
CHECK(pixbuf);
}
virtual ~ImageRepGdk() {
if (pixbuf_) {
g_object_unref(pixbuf_);
pixbuf_ = NULL;
}
}
GdkPixbuf* pixbuf() const { return pixbuf_; }
private:
GdkPixbuf* pixbuf_;
DISALLOW_COPY_AND_ASSIGN(ImageRepGdk);
};
#endif
#if defined(OS_MACOSX)
class ImageRepCocoa : public ImageRep {
public:
explicit ImageRepCocoa(NSImage* image)
: ImageRep(Image::kImageRepCocoa),
image_(image) {
CHECK(image);
}
virtual ~ImageRepCocoa() {
base::mac::NSObjectRelease(image_);
image_ = nil;
}
NSImage* image() const { return image_; }
private:
NSImage* image_;
DISALLOW_COPY_AND_ASSIGN(ImageRepCocoa);
};
#endif
// The Storage class acts similarly to the pixels in a SkBitmap: the Image
// class holds a refptr instance of Storage, which in turn holds all the
// ImageReps. This way, the Image can be cheaply copied.
class ImageStorage : public base::RefCounted<ImageStorage> {
public:
ImageStorage(gfx::Image::RepresentationType default_type)
: default_representation_type_(default_type) {
}
gfx::Image::RepresentationType default_representation_type() {
return default_representation_type_;
}
gfx::Image::RepresentationMap& representations() { return representations_; }
private:
~ImageStorage() {
for (gfx::Image::RepresentationMap::iterator it = representations_.begin();
it != representations_.end();
++it) {
delete it->second;
}
representations_.clear();
}
// The type of image that was passed to the constructor. This key will always
// exist in the |representations_| map.
gfx::Image::RepresentationType default_representation_type_;
// All the representations of an Image. Size will always be at least one, with
// more for any converted representations.
gfx::Image::RepresentationMap representations_;
friend class base::RefCounted<ImageStorage>;
};
} // namespace internal
Image::Image(const SkBitmap* bitmap)
: storage_(new internal::ImageStorage(Image::kImageRepSkia)) {
internal::ImageRepSkia* rep = new internal::ImageRepSkia(bitmap);
AddRepresentation(rep);
}
Image::Image(const std::vector<const SkBitmap*>& bitmaps)
: storage_(new internal::ImageStorage(Image::kImageRepSkia)) {
internal::ImageRepSkia* rep = new internal::ImageRepSkia(bitmaps);
AddRepresentation(rep);
}
#if defined(TOOLKIT_USES_GTK)
Image::Image(GdkPixbuf* pixbuf)
: storage_(new internal::ImageStorage(Image::kImageRepGdk)) {
internal::ImageRepGdk* rep = new internal::ImageRepGdk(pixbuf);
AddRepresentation(rep);
}
#endif
#if defined(OS_MACOSX)
Image::Image(NSImage* image)
: storage_(new internal::ImageStorage(Image::kImageRepCocoa)) {
internal::ImageRepCocoa* rep = new internal::ImageRepCocoa(image);
AddRepresentation(rep);
}
#endif
Image::Image(const Image& other) : storage_(other.storage_) {
}
Image& Image::operator=(const Image& other) {
storage_ = other.storage_;
return *this;
}
Image::~Image() {
}
Image::operator const SkBitmap*() const {
internal::ImageRep* rep = GetRepresentation(Image::kImageRepSkia);
return rep->AsImageRepSkia()->bitmap();
}
Image::operator const SkBitmap&() const {
return *(this->operator const SkBitmap*());
}
#if defined(TOOLKIT_USES_GTK)
Image::operator GdkPixbuf*() const {
internal::ImageRep* rep = GetRepresentation(Image::kImageRepGdk);
return rep->AsImageRepGdk()->pixbuf();
}
#endif
#if defined(OS_MACOSX)
Image::operator NSImage*() const {
internal::ImageRep* rep = GetRepresentation(Image::kImageRepCocoa);
return rep->AsImageRepCocoa()->image();
}
#endif
bool Image::HasRepresentation(RepresentationType type) const {
return storage_->representations().count(type) != 0;
}
size_t Image::RepresentationCount() const {
return storage_->representations().size();
}
void Image::SwapRepresentations(gfx::Image* other) {
storage_.swap(other->storage_);
}
internal::ImageRep* Image::DefaultRepresentation() const {
RepresentationMap& representations = storage_->representations();
RepresentationMap::iterator it =
representations.find(storage_->default_representation_type());
DCHECK(it != representations.end());
return it->second;
}
internal::ImageRep* Image::GetRepresentation(
RepresentationType rep_type) const {
// If the requested rep is the default, return it.
internal::ImageRep* default_rep = DefaultRepresentation();
if (rep_type == storage_->default_representation_type())
return default_rep;
// Check to see if the representation already exists.
RepresentationMap::iterator it = storage_->representations().find(rep_type);
if (it != storage_->representations().end())
return it->second;
// At this point, the requested rep does not exist, so it must be converted
// from the default rep.
// Handle native-to-Skia conversion.
if (rep_type == Image::kImageRepSkia) {
internal::ImageRepSkia* rep = NULL;
#if defined(TOOLKIT_USES_GTK)
if (storage_->default_representation_type() == Image::kImageRepGdk) {
internal::ImageRepGdk* pixbuf_rep = default_rep->AsImageRepGdk();
rep = new internal::ImageRepSkia(
internal::GdkPixbufToSkBitmap(pixbuf_rep->pixbuf()));
}
#elif defined(OS_MACOSX)
if (storage_->default_representation_type() == Image::kImageRepCocoa) {
internal::ImageRepCocoa* nsimage_rep = default_rep->AsImageRepCocoa();
std::vector<const SkBitmap*> bitmaps;
CHECK(internal::NSImageToSkBitmaps(nsimage_rep->image(), &bitmaps));
rep = new internal::ImageRepSkia(bitmaps);
}
#endif
CHECK(rep);
AddRepresentation(rep);
return rep;
}
// Handle Skia-to-native conversions.
if (default_rep->type() == Image::kImageRepSkia) {
internal::ImageRepSkia* skia_rep = default_rep->AsImageRepSkia();
internal::ImageRep* native_rep = NULL;
#if defined(TOOLKIT_USES_GTK)
if (rep_type == Image::kImageRepGdk) {
GdkPixbuf* pixbuf = gfx::GdkPixbufFromSkBitmap(skia_rep->bitmap());
native_rep = new internal::ImageRepGdk(pixbuf);
}
#elif defined(OS_MACOSX)
if (rep_type == Image::kImageRepCocoa) {
NSImage* image = gfx::SkBitmapsToNSImage(skia_rep->bitmaps());
base::mac::NSObjectRetain(image);
native_rep = new internal::ImageRepCocoa(image);
}
#endif
CHECK(native_rep);
AddRepresentation(native_rep);
return native_rep;
}
// Something went seriously wrong...
return NULL;
}
void Image::AddRepresentation(internal::ImageRep* rep) const {
storage_->representations().insert(std::make_pair(rep->type(), rep));
}
size_t Image::GetNumberOfSkBitmaps() const {
return GetRepresentation(Image::kImageRepSkia)->AsImageRepSkia()->
bitmaps().size();
}
const SkBitmap* Image::GetSkBitmapAtIndex(size_t index) const {
return GetRepresentation(Image::kImageRepSkia)->AsImageRepSkia()->
bitmaps()[index];
}
} // namespace gfx
|