OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 #include "ui/gfx/image.h" | 5 #include "ui/gfx/image.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/stl_util-inl.h" | 10 #include "base/stl_util-inl.h" |
(...skipping 22 matching lines...) Expand all Loading... |
33 #if defined(OS_LINUX) | 33 #if defined(OS_LINUX) |
34 const SkBitmap* GdkPixbufToSkBitmap(GdkPixbuf* pixbuf) { | 34 const SkBitmap* GdkPixbufToSkBitmap(GdkPixbuf* pixbuf) { |
35 gfx::CanvasSkia canvas(gdk_pixbuf_get_width(pixbuf), | 35 gfx::CanvasSkia canvas(gdk_pixbuf_get_width(pixbuf), |
36 gdk_pixbuf_get_height(pixbuf), | 36 gdk_pixbuf_get_height(pixbuf), |
37 /*is_opaque=*/false); | 37 /*is_opaque=*/false); |
38 canvas.DrawGdkPixbuf(pixbuf, 0, 0); | 38 canvas.DrawGdkPixbuf(pixbuf, 0, 0); |
39 return new SkBitmap(canvas.ExtractBitmap()); | 39 return new SkBitmap(canvas.ExtractBitmap()); |
40 } | 40 } |
41 #endif | 41 #endif |
42 | 42 |
43 class SkBitmapRep; | 43 class ImageRepSkia; |
44 class GdkPixbufRep; | 44 class ImageRepGdk; |
45 class NSImageRep; | 45 class ImageRepCocoa; |
46 | 46 |
47 // An ImageRep is the object that holds the backing memory for an Image. Each | 47 // An ImageRep is the object that holds the backing memory for an Image. Each |
48 // RepresentationType has an ImageRep subclass that is responsible for freeing | 48 // RepresentationType has an ImageRep subclass that is responsible for freeing |
49 // the memory that the ImageRep holds. When an ImageRep is created, it expects | 49 // the memory that the ImageRep holds. When an ImageRep is created, it expects |
50 // to take ownership of the image, without having to retain it or increase its | 50 // to take ownership of the image, without having to retain it or increase its |
51 // reference count. | 51 // reference count. |
52 class ImageRep { | 52 class ImageRep { |
53 public: | 53 public: |
54 explicit ImageRep(Image::RepresentationType rep) : type_(rep) {} | 54 explicit ImageRep(Image::RepresentationType rep) : type_(rep) {} |
55 | 55 |
56 // Deletes the associated pixels of an ImageRep. | 56 // Deletes the associated pixels of an ImageRep. |
57 virtual ~ImageRep() {} | 57 virtual ~ImageRep() {} |
58 | 58 |
59 // Cast helpers ("fake RTTI"). | 59 // Cast helpers ("fake RTTI"). |
60 SkBitmapRep* AsSkBitmapRep() { | 60 ImageRepSkia* AsImageRepSkia() { |
61 CHECK_EQ(type_, Image::kSkBitmapRep); | 61 CHECK_EQ(type_, Image::kImageRepSkia); |
62 return reinterpret_cast<SkBitmapRep*>(this); | 62 return reinterpret_cast<ImageRepSkia*>(this); |
63 } | 63 } |
64 | 64 |
65 #if defined(OS_LINUX) | 65 #if defined(OS_LINUX) |
66 GdkPixbufRep* AsGdkPixbufRep() { | 66 ImageRepGdk* AsImageRepGdk() { |
67 CHECK_EQ(type_, Image::kGdkPixbufRep); | 67 CHECK_EQ(type_, Image::kImageRepGdk); |
68 return reinterpret_cast<GdkPixbufRep*>(this); | 68 return reinterpret_cast<ImageRepGdk*>(this); |
69 } | 69 } |
70 #endif | 70 #endif |
71 | 71 |
72 #if defined(OS_MACOSX) | 72 #if defined(OS_MACOSX) |
73 NSImageRep* AsNSImageRep() { | 73 ImageRepCocoa* AsImageRepCocoa() { |
74 CHECK_EQ(type_, Image::kNSImageRep); | 74 CHECK_EQ(type_, Image::kImageRepCocoa); |
75 return reinterpret_cast<NSImageRep*>(this); | 75 return reinterpret_cast<ImageRepCocoa*>(this); |
76 } | 76 } |
77 #endif | 77 #endif |
78 | 78 |
79 Image::RepresentationType type() const { return type_; } | 79 Image::RepresentationType type() const { return type_; } |
80 | 80 |
81 private: | 81 private: |
82 Image::RepresentationType type_; | 82 Image::RepresentationType type_; |
83 }; | 83 }; |
84 | 84 |
85 class SkBitmapRep : public ImageRep { | 85 class ImageRepSkia : public ImageRep { |
86 public: | 86 public: |
87 explicit SkBitmapRep(const SkBitmap* bitmap) | 87 explicit ImageRepSkia(const SkBitmap* bitmap) |
88 : ImageRep(Image::kSkBitmapRep) { | 88 : ImageRep(Image::kImageRepSkia) { |
89 CHECK(bitmap); | 89 CHECK(bitmap); |
90 bitmaps_.push_back(bitmap); | 90 bitmaps_.push_back(bitmap); |
91 } | 91 } |
92 | 92 |
93 explicit SkBitmapRep(const std::vector<const SkBitmap*>& bitmaps) | 93 explicit ImageRepSkia(const std::vector<const SkBitmap*>& bitmaps) |
94 : ImageRep(Image::kSkBitmapRep), | 94 : ImageRep(Image::kImageRepSkia), |
95 bitmaps_(bitmaps) { | 95 bitmaps_(bitmaps) { |
96 CHECK(!bitmaps_.empty()); | 96 CHECK(!bitmaps_.empty()); |
97 } | 97 } |
98 | 98 |
99 virtual ~SkBitmapRep() { | 99 virtual ~ImageRepSkia() { |
100 STLDeleteElements(&bitmaps_); | 100 STLDeleteElements(&bitmaps_); |
101 } | 101 } |
102 | 102 |
103 const SkBitmap* bitmap() const { return bitmaps_[0]; } | 103 const SkBitmap* bitmap() const { return bitmaps_[0]; } |
104 | 104 |
105 const std::vector<const SkBitmap*>& bitmaps() const { return bitmaps_; } | 105 const std::vector<const SkBitmap*>& bitmaps() const { return bitmaps_; } |
106 | 106 |
107 private: | 107 private: |
108 std::vector<const SkBitmap*> bitmaps_; | 108 std::vector<const SkBitmap*> bitmaps_; |
109 | 109 |
110 DISALLOW_COPY_AND_ASSIGN(SkBitmapRep); | 110 DISALLOW_COPY_AND_ASSIGN(ImageRepSkia); |
111 }; | 111 }; |
112 | 112 |
113 #if defined(OS_LINUX) | 113 #if defined(OS_LINUX) |
114 class GdkPixbufRep : public ImageRep { | 114 class ImageRepGdk : public ImageRep { |
115 public: | 115 public: |
116 explicit GdkPixbufRep(GdkPixbuf* pixbuf) | 116 explicit ImageRepGdk(GdkPixbuf* pixbuf) |
117 : ImageRep(Image::kGdkPixbufRep), | 117 : ImageRep(Image::kImageRepGdk), |
118 pixbuf_(pixbuf) { | 118 pixbuf_(pixbuf) { |
119 CHECK(pixbuf); | 119 CHECK(pixbuf); |
120 } | 120 } |
121 | 121 |
122 virtual ~GdkPixbufRep() { | 122 virtual ~ImageRepGdk() { |
123 if (pixbuf_) { | 123 if (pixbuf_) { |
124 g_object_unref(pixbuf_); | 124 g_object_unref(pixbuf_); |
125 pixbuf_ = NULL; | 125 pixbuf_ = NULL; |
126 } | 126 } |
127 } | 127 } |
128 | 128 |
129 GdkPixbuf* pixbuf() const { return pixbuf_; } | 129 GdkPixbuf* pixbuf() const { return pixbuf_; } |
130 | 130 |
131 private: | 131 private: |
132 GdkPixbuf* pixbuf_; | 132 GdkPixbuf* pixbuf_; |
133 | 133 |
134 DISALLOW_COPY_AND_ASSIGN(GdkPixbufRep); | 134 DISALLOW_COPY_AND_ASSIGN(ImageRepGdk); |
135 }; | 135 }; |
136 #endif | 136 #endif |
137 | 137 |
138 #if defined(OS_MACOSX) | 138 #if defined(OS_MACOSX) |
139 class NSImageRep : public ImageRep { | 139 class ImageRepCocoa : public ImageRep { |
140 public: | 140 public: |
141 explicit NSImageRep(NSImage* image) | 141 explicit ImageRepCocoa(NSImage* image) |
142 : ImageRep(Image::kNSImageRep), | 142 : ImageRep(Image::kImageRepCocoa), |
143 image_(image) { | 143 image_(image) { |
144 CHECK(image); | 144 CHECK(image); |
145 } | 145 } |
146 | 146 |
147 virtual ~NSImageRep() { | 147 virtual ~ImageRepCocoa() { |
148 base::mac::NSObjectRelease(image_); | 148 base::mac::NSObjectRelease(image_); |
149 image_ = nil; | 149 image_ = nil; |
150 } | 150 } |
151 | 151 |
152 NSImage* image() const { return image_; } | 152 NSImage* image() const { return image_; } |
153 | 153 |
154 private: | 154 private: |
155 NSImage* image_; | 155 NSImage* image_; |
156 | 156 |
157 DISALLOW_COPY_AND_ASSIGN(NSImageRep); | 157 DISALLOW_COPY_AND_ASSIGN(ImageRepCocoa); |
158 }; | 158 }; |
159 #endif | 159 #endif |
160 | 160 |
161 // The Storage class acts similarly to the pixels in a SkBitmap: the Image | 161 // The Storage class acts similarly to the pixels in a SkBitmap: the Image |
162 // class holds a refptr instance of Storage, which in turn holds all the | 162 // class holds a refptr instance of Storage, which in turn holds all the |
163 // ImageReps. This way, the Image can be cheaply copied. | 163 // ImageReps. This way, the Image can be cheaply copied. |
164 class ImageStorage : public base::RefCounted<ImageStorage> { | 164 class ImageStorage : public base::RefCounted<ImageStorage> { |
165 public: | 165 public: |
166 ImageStorage(gfx::Image::RepresentationType default_type) | 166 ImageStorage(gfx::Image::RepresentationType default_type) |
167 : default_representation_type_(default_type) { | 167 : default_representation_type_(default_type) { |
(...skipping 21 matching lines...) Expand all Loading... |
189 // All the representations of an Image. Size will always be at least one, with | 189 // All the representations of an Image. Size will always be at least one, with |
190 // more for any converted representations. | 190 // more for any converted representations. |
191 gfx::Image::RepresentationMap representations_; | 191 gfx::Image::RepresentationMap representations_; |
192 | 192 |
193 friend class base::RefCounted<ImageStorage>; | 193 friend class base::RefCounted<ImageStorage>; |
194 }; | 194 }; |
195 | 195 |
196 } // namespace internal | 196 } // namespace internal |
197 | 197 |
198 Image::Image(const SkBitmap* bitmap) | 198 Image::Image(const SkBitmap* bitmap) |
199 : storage_(new internal::ImageStorage(Image::kSkBitmapRep)) { | 199 : storage_(new internal::ImageStorage(Image::kImageRepSkia)) { |
200 internal::SkBitmapRep* rep = new internal::SkBitmapRep(bitmap); | 200 internal::ImageRepSkia* rep = new internal::ImageRepSkia(bitmap); |
201 AddRepresentation(rep); | 201 AddRepresentation(rep); |
202 } | 202 } |
203 | 203 |
204 Image::Image(const std::vector<const SkBitmap*>& bitmaps) | 204 Image::Image(const std::vector<const SkBitmap*>& bitmaps) |
205 : storage_(new internal::ImageStorage(Image::kSkBitmapRep)) { | 205 : storage_(new internal::ImageStorage(Image::kImageRepSkia)) { |
206 internal::SkBitmapRep* rep = new internal::SkBitmapRep(bitmaps); | 206 internal::ImageRepSkia* rep = new internal::ImageRepSkia(bitmaps); |
207 AddRepresentation(rep); | 207 AddRepresentation(rep); |
208 } | 208 } |
209 | 209 |
210 #if defined(OS_LINUX) | 210 #if defined(OS_LINUX) |
211 Image::Image(GdkPixbuf* pixbuf) | 211 Image::Image(GdkPixbuf* pixbuf) |
212 : storage_(new internal::ImageStorage(Image::kGdkPixbufRep)) { | 212 : storage_(new internal::ImageStorage(Image::kImageRepGdk)) { |
213 internal::GdkPixbufRep* rep = new internal::GdkPixbufRep(pixbuf); | 213 internal::ImageRepGdk* rep = new internal::ImageRepGdk(pixbuf); |
214 AddRepresentation(rep); | 214 AddRepresentation(rep); |
215 } | 215 } |
216 #endif | 216 #endif |
217 | 217 |
218 #if defined(OS_MACOSX) | 218 #if defined(OS_MACOSX) |
219 Image::Image(NSImage* image) | 219 Image::Image(NSImage* image) |
220 : storage_(new internal::ImageStorage(Image::kNSImageRep)) { | 220 : storage_(new internal::ImageStorage(Image::kImageRepCocoa)) { |
221 internal::NSImageRep* rep = new internal::NSImageRep(image); | 221 internal::ImageRepCocoa* rep = new internal::ImageRepCocoa(image); |
222 AddRepresentation(rep); | 222 AddRepresentation(rep); |
223 } | 223 } |
224 #endif | 224 #endif |
225 | 225 |
226 Image::Image(const Image& other) : storage_(other.storage_) { | 226 Image::Image(const Image& other) : storage_(other.storage_) { |
227 } | 227 } |
228 | 228 |
229 Image& Image::operator=(const Image& other) { | 229 Image& Image::operator=(const Image& other) { |
230 storage_ = other.storage_; | 230 storage_ = other.storage_; |
231 return *this; | 231 return *this; |
232 } | 232 } |
233 | 233 |
234 Image::~Image() { | 234 Image::~Image() { |
235 } | 235 } |
236 | 236 |
237 Image::operator const SkBitmap*() const { | 237 Image::operator const SkBitmap*() const { |
238 internal::ImageRep* rep = GetRepresentation(Image::kSkBitmapRep); | 238 internal::ImageRep* rep = GetRepresentation(Image::kImageRepSkia); |
239 return rep->AsSkBitmapRep()->bitmap(); | 239 return rep->AsImageRepSkia()->bitmap(); |
240 } | 240 } |
241 | 241 |
242 Image::operator const SkBitmap&() const { | 242 Image::operator const SkBitmap&() const { |
243 return *(this->operator const SkBitmap*()); | 243 return *(this->operator const SkBitmap*()); |
244 } | 244 } |
245 | 245 |
246 #if defined(OS_LINUX) | 246 #if defined(OS_LINUX) |
247 Image::operator GdkPixbuf*() const { | 247 Image::operator GdkPixbuf*() const { |
248 internal::ImageRep* rep = GetRepresentation(Image::kGdkPixbufRep); | 248 internal::ImageRep* rep = GetRepresentation(Image::kImageRepGdk); |
249 return rep->AsGdkPixbufRep()->pixbuf(); | 249 return rep->AsImageRepGdk()->pixbuf(); |
250 } | 250 } |
251 #endif | 251 #endif |
252 | 252 |
253 #if defined(OS_MACOSX) | 253 #if defined(OS_MACOSX) |
254 Image::operator NSImage*() const { | 254 Image::operator NSImage*() const { |
255 internal::ImageRep* rep = GetRepresentation(Image::kNSImageRep); | 255 internal::ImageRep* rep = GetRepresentation(Image::kImageRepCocoa); |
256 return rep->AsNSImageRep()->image(); | 256 return rep->AsImageRepCocoa()->image(); |
257 } | 257 } |
258 #endif | 258 #endif |
259 | 259 |
260 bool Image::HasRepresentation(RepresentationType type) const { | 260 bool Image::HasRepresentation(RepresentationType type) const { |
261 return storage_->representations().count(type) != 0; | 261 return storage_->representations().count(type) != 0; |
262 } | 262 } |
263 | 263 |
264 size_t Image::RepresentationCount() const { | 264 size_t Image::RepresentationCount() const { |
265 return storage_->representations().size(); | 265 return storage_->representations().size(); |
266 } | 266 } |
(...skipping 19 matching lines...) Expand all Loading... |
286 | 286 |
287 // Check to see if the representation already exists. | 287 // Check to see if the representation already exists. |
288 RepresentationMap::iterator it = storage_->representations().find(rep_type); | 288 RepresentationMap::iterator it = storage_->representations().find(rep_type); |
289 if (it != storage_->representations().end()) | 289 if (it != storage_->representations().end()) |
290 return it->second; | 290 return it->second; |
291 | 291 |
292 // At this point, the requested rep does not exist, so it must be converted | 292 // At this point, the requested rep does not exist, so it must be converted |
293 // from the default rep. | 293 // from the default rep. |
294 | 294 |
295 // Handle native-to-Skia conversion. | 295 // Handle native-to-Skia conversion. |
296 if (rep_type == Image::kSkBitmapRep) { | 296 if (rep_type == Image::kImageRepSkia) { |
297 internal::SkBitmapRep* rep = NULL; | 297 internal::ImageRepSkia* rep = NULL; |
298 #if defined(OS_LINUX) | 298 #if defined(OS_LINUX) |
299 if (storage_->default_representation_type() == Image::kGdkPixbufRep) { | 299 if (storage_->default_representation_type() == Image::kImageRepGdk) { |
300 internal::GdkPixbufRep* pixbuf_rep = default_rep->AsGdkPixbufRep(); | 300 internal::ImageRepGdk* pixbuf_rep = default_rep->AsImageRepGdk(); |
301 rep = new internal::SkBitmapRep( | 301 rep = new internal::ImageRepSkia( |
302 internal::GdkPixbufToSkBitmap(pixbuf_rep->pixbuf())); | 302 internal::GdkPixbufToSkBitmap(pixbuf_rep->pixbuf())); |
303 } | 303 } |
304 #elif defined(OS_MACOSX) | 304 #elif defined(OS_MACOSX) |
305 if (storage_->default_representation_type() == Image::kNSImageRep) { | 305 if (storage_->default_representation_type() == Image::kImageRepCocoa) { |
306 internal::NSImageRep* nsimage_rep = default_rep->AsNSImageRep(); | 306 internal::ImageRepCocoa* nsimage_rep = default_rep->AsImageRepCocoa(); |
307 std::vector<const SkBitmap*> bitmaps; | 307 std::vector<const SkBitmap*> bitmaps; |
308 CHECK(internal::NSImageToSkBitmaps(nsimage_rep->image(), &bitmaps)); | 308 CHECK(internal::NSImageToSkBitmaps(nsimage_rep->image(), &bitmaps)); |
309 rep = new internal::SkBitmapRep(bitmaps); | 309 rep = new internal::ImageRepSkia(bitmaps); |
310 } | 310 } |
311 #endif | 311 #endif |
312 CHECK(rep); | 312 CHECK(rep); |
313 AddRepresentation(rep); | 313 AddRepresentation(rep); |
314 return rep; | 314 return rep; |
315 } | 315 } |
316 | 316 |
317 // Handle Skia-to-native conversions. | 317 // Handle Skia-to-native conversions. |
318 if (default_rep->type() == Image::kSkBitmapRep) { | 318 if (default_rep->type() == Image::kImageRepSkia) { |
319 internal::SkBitmapRep* skia_rep = default_rep->AsSkBitmapRep(); | 319 internal::ImageRepSkia* skia_rep = default_rep->AsImageRepSkia(); |
320 internal::ImageRep* native_rep = NULL; | 320 internal::ImageRep* native_rep = NULL; |
321 #if defined(OS_LINUX) | 321 #if defined(OS_LINUX) |
322 if (rep_type == Image::kGdkPixbufRep) { | 322 if (rep_type == Image::kImageRepGdk) { |
323 GdkPixbuf* pixbuf = gfx::GdkPixbufFromSkBitmap(skia_rep->bitmap()); | 323 GdkPixbuf* pixbuf = gfx::GdkPixbufFromSkBitmap(skia_rep->bitmap()); |
324 native_rep = new internal::GdkPixbufRep(pixbuf); | 324 native_rep = new internal::ImageRepGdk(pixbuf); |
325 } | 325 } |
326 #elif defined(OS_MACOSX) | 326 #elif defined(OS_MACOSX) |
327 if (rep_type == Image::kNSImageRep) { | 327 if (rep_type == Image::kImageRepCocoa) { |
328 NSImage* image = gfx::SkBitmapsToNSImage(skia_rep->bitmaps()); | 328 NSImage* image = gfx::SkBitmapsToNSImage(skia_rep->bitmaps()); |
329 base::mac::NSObjectRetain(image); | 329 base::mac::NSObjectRetain(image); |
330 native_rep = new internal::NSImageRep(image); | 330 native_rep = new internal::ImageRepCocoa(image); |
331 } | 331 } |
332 #endif | 332 #endif |
333 CHECK(native_rep); | 333 CHECK(native_rep); |
334 AddRepresentation(native_rep); | 334 AddRepresentation(native_rep); |
335 return native_rep; | 335 return native_rep; |
336 } | 336 } |
337 | 337 |
338 // Something went seriously wrong... | 338 // Something went seriously wrong... |
339 return NULL; | 339 return NULL; |
340 } | 340 } |
341 | 341 |
342 void Image::AddRepresentation(internal::ImageRep* rep) const { | 342 void Image::AddRepresentation(internal::ImageRep* rep) const { |
343 storage_->representations().insert(std::make_pair(rep->type(), rep)); | 343 storage_->representations().insert(std::make_pair(rep->type(), rep)); |
344 } | 344 } |
345 | 345 |
346 size_t Image::GetNumberOfSkBitmaps() const { | 346 size_t Image::GetNumberOfSkBitmaps() const { |
347 return GetRepresentation(Image::kSkBitmapRep)->AsSkBitmapRep()-> | 347 return GetRepresentation(Image::kImageRepSkia)->AsImageRepSkia()-> |
348 bitmaps().size(); | 348 bitmaps().size(); |
349 } | 349 } |
350 | 350 |
351 const SkBitmap* Image::GetSkBitmapAtIndex(size_t index) const { | 351 const SkBitmap* Image::GetSkBitmapAtIndex(size_t index) const { |
352 return GetRepresentation(Image::kSkBitmapRep)->AsSkBitmapRep()-> | 352 return GetRepresentation(Image::kImageRepSkia)->AsImageRepSkia()-> |
353 bitmaps()[index]; | 353 bitmaps()[index]; |
354 } | 354 } |
355 | 355 |
356 } // namespace gfx | 356 } // namespace gfx |
OLD | NEW |