OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/image.h" | 5 #include "ui/gfx/image/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/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
(...skipping 13 matching lines...) Expand all Loading... | |
24 #include "skia/ext/skia_utils_mac.h" | 24 #include "skia/ext/skia_utils_mac.h" |
25 #endif | 25 #endif |
26 | 26 |
27 namespace gfx { | 27 namespace gfx { |
28 | 28 |
29 namespace internal { | 29 namespace internal { |
30 | 30 |
31 #if defined(OS_MACOSX) | 31 #if defined(OS_MACOSX) |
32 // This is a wrapper around gfx::NSImageToSkBitmap() because this cross-platform | 32 // This is a wrapper around gfx::NSImageToSkBitmap() because this cross-platform |
33 // file cannot include the [square brackets] of ObjC. | 33 // file cannot include the [square brackets] of ObjC. |
34 bool NSImageToSkBitmaps(NSImage* image, std::vector<const SkBitmap*>* bitmaps); | 34 ImageSkia NSImageToImageSkia(NSImage* image); |
Robert Sesek
2012/05/08 23:30:47
What if this conversion fails?
| |
35 NSImage* ImageSkiaToNSImage(const ImageSkia* image); | |
35 #endif | 36 #endif |
36 | 37 |
37 #if defined(TOOLKIT_GTK) | 38 #if defined(TOOLKIT_GTK) |
38 const SkBitmap* GdkPixbufToSkBitmap(GdkPixbuf* pixbuf) { | 39 const SkBitmap* GdkPixbufToSkBitmap(GdkPixbuf* pixbuf) { |
39 CHECK(pixbuf); | 40 CHECK(pixbuf); |
40 gfx::Canvas canvas(gfx::Size(gdk_pixbuf_get_width(pixbuf), | 41 gfx::Canvas canvas(gfx::Size(gdk_pixbuf_get_width(pixbuf), |
41 gdk_pixbuf_get_height(pixbuf)), false); | 42 gdk_pixbuf_get_height(pixbuf)), false); |
42 skia::ScopedPlatformPaint scoped_platform_paint(canvas.sk_canvas()); | 43 skia::ScopedPlatformPaint scoped_platform_paint(canvas.sk_canvas()); |
43 cairo_t* cr = scoped_platform_paint.GetPlatformSurface(); | 44 cairo_t* cr = scoped_platform_paint.GetPlatformSurface(); |
44 gdk_cairo_set_source_pixbuf(cr, pixbuf, 0, 0); | 45 gdk_cairo_set_source_pixbuf(cr, pixbuf, 0, 0); |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
90 #endif | 91 #endif |
91 | 92 |
92 Image::RepresentationType type() const { return type_; } | 93 Image::RepresentationType type() const { return type_; } |
93 | 94 |
94 private: | 95 private: |
95 Image::RepresentationType type_; | 96 Image::RepresentationType type_; |
96 }; | 97 }; |
97 | 98 |
98 class ImageRepSkia : public ImageRep { | 99 class ImageRepSkia : public ImageRep { |
99 public: | 100 public: |
101 // Takes ownership of |image|. | |
100 explicit ImageRepSkia(ImageSkia* image) | 102 explicit ImageRepSkia(ImageSkia* image) |
101 : ImageRep(Image::kImageRepSkia), | 103 : ImageRep(Image::kImageRepSkia), |
102 image_(image) { | 104 image_(image) { |
103 } | 105 } |
104 | 106 |
105 virtual ~ImageRepSkia() { | 107 virtual ~ImageRepSkia() { |
106 } | 108 } |
107 | 109 |
108 ImageSkia* image() { return image_.get(); } | 110 ImageSkia* image() { return image_.get(); } |
109 | 111 |
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
217 | 219 |
218 friend class base::RefCounted<ImageStorage>; | 220 friend class base::RefCounted<ImageStorage>; |
219 }; | 221 }; |
220 | 222 |
221 } // namespace internal | 223 } // namespace internal |
222 | 224 |
223 Image::Image() { | 225 Image::Image() { |
224 // |storage_| is NULL for empty Images. | 226 // |storage_| is NULL for empty Images. |
225 } | 227 } |
226 | 228 |
229 Image::Image(const ImageSkia& image) | |
230 : storage_(new internal::ImageStorage(Image::kImageRepSkia)) { | |
231 internal::ImageRepSkia* rep = new internal::ImageRepSkia( | |
232 new ImageSkia(image)); | |
233 AddRepresentation(rep); | |
234 } | |
235 | |
227 Image::Image(const SkBitmap* bitmap) | 236 Image::Image(const SkBitmap* bitmap) |
228 : storage_(new internal::ImageStorage(Image::kImageRepSkia)) { | 237 : storage_(new internal::ImageStorage(Image::kImageRepSkia)) { |
229 internal::ImageRepSkia* rep = new internal::ImageRepSkia( | 238 internal::ImageRepSkia* rep = new internal::ImageRepSkia( |
230 new ImageSkia(bitmap)); | 239 new ImageSkia(bitmap)); |
231 AddRepresentation(rep); | 240 AddRepresentation(rep); |
232 } | 241 } |
233 | 242 |
234 Image::Image(const SkBitmap& bitmap) | 243 Image::Image(const SkBitmap& bitmap) |
235 : storage_(new internal::ImageStorage(Image::kImageRepSkia)) { | 244 : storage_(new internal::ImageStorage(Image::kImageRepSkia)) { |
236 internal::ImageRepSkia* rep = | 245 internal::ImageRepSkia* rep = |
237 new internal::ImageRepSkia(new ImageSkia(new SkBitmap(bitmap))); | 246 new internal::ImageRepSkia(new ImageSkia(new SkBitmap(bitmap))); |
238 AddRepresentation(rep); | 247 AddRepresentation(rep); |
239 } | 248 } |
240 | 249 |
241 Image::Image(const std::vector<const SkBitmap*>& bitmaps) | |
242 : storage_(new internal::ImageStorage(Image::kImageRepSkia)) { | |
243 internal::ImageRepSkia* rep = new internal::ImageRepSkia( | |
244 new ImageSkia(bitmaps)); | |
245 AddRepresentation(rep); | |
246 } | |
247 | |
248 #if defined(TOOLKIT_GTK) | 250 #if defined(TOOLKIT_GTK) |
249 Image::Image(GdkPixbuf* pixbuf) | 251 Image::Image(GdkPixbuf* pixbuf) |
250 : storage_(new internal::ImageStorage(Image::kImageRepGdk)) { | 252 : storage_(new internal::ImageStorage(Image::kImageRepGdk)) { |
251 internal::ImageRepGdk* rep = new internal::ImageRepGdk(pixbuf); | 253 internal::ImageRepGdk* rep = new internal::ImageRepGdk(pixbuf); |
252 AddRepresentation(rep); | 254 AddRepresentation(rep); |
253 } | 255 } |
254 #endif | 256 #endif |
255 | 257 |
256 #if defined(OS_MACOSX) | 258 #if defined(OS_MACOSX) |
257 Image::Image(NSImage* image) | 259 Image::Image(NSImage* image) |
258 : storage_(new internal::ImageStorage(Image::kImageRepCocoa)) { | 260 : storage_(new internal::ImageStorage(Image::kImageRepCocoa)) { |
259 internal::ImageRepCocoa* rep = new internal::ImageRepCocoa(image); | 261 internal::ImageRepCocoa* rep = new internal::ImageRepCocoa(image); |
260 AddRepresentation(rep); | 262 AddRepresentation(rep); |
261 } | 263 } |
262 #endif | 264 #endif |
263 | 265 |
264 Image::Image(const Image& other) : storage_(other.storage_) { | 266 Image::Image(const Image& other) : storage_(other.storage_) { |
265 } | 267 } |
266 | 268 |
267 Image& Image::operator=(const Image& other) { | 269 Image& Image::operator=(const Image& other) { |
268 storage_ = other.storage_; | 270 storage_ = other.storage_; |
269 return *this; | 271 return *this; |
270 } | 272 } |
271 | 273 |
272 Image::~Image() { | 274 Image::~Image() { |
273 } | 275 } |
274 | 276 |
275 const SkBitmap* Image::ToSkBitmap() const { | 277 const SkBitmap* Image::ToSkBitmap() const { |
276 internal::ImageRep* rep = GetRepresentation(Image::kImageRepSkia); | 278 internal::ImageRep* rep = GetRepresentation(Image::kImageRepSkia); |
277 return rep->AsImageRepSkia()->image()->bitmaps()[0]; | 279 return &rep->AsImageRepSkia()->image()->bitmaps()[0]; |
278 } | 280 } |
279 | 281 |
280 const ImageSkia* Image::ToImageSkia() const { | 282 const ImageSkia* Image::ToImageSkia() const { |
281 internal::ImageRep* rep = GetRepresentation(Image::kImageRepSkia); | 283 internal::ImageRep* rep = GetRepresentation(Image::kImageRepSkia); |
282 return rep->AsImageRepSkia()->image(); | 284 return rep->AsImageRepSkia()->image(); |
283 } | 285 } |
284 | 286 |
285 #if defined(TOOLKIT_GTK) | 287 #if defined(TOOLKIT_GTK) |
286 GdkPixbuf* Image::ToGdkPixbuf() const { | 288 GdkPixbuf* Image::ToGdkPixbuf() const { |
287 internal::ImageRep* rep = GetRepresentation(Image::kImageRepGdk); | 289 internal::ImageRep* rep = GetRepresentation(Image::kImageRepGdk); |
288 return rep->AsImageRepGdk()->pixbuf(); | 290 return rep->AsImageRepGdk()->pixbuf(); |
289 } | 291 } |
290 | 292 |
291 CairoCachedSurface* const Image::ToCairo() const { | 293 CairoCachedSurface* const Image::ToCairo() const { |
292 internal::ImageRep* rep = GetRepresentation(Image::kImageRepCairoCache); | 294 internal::ImageRep* rep = GetRepresentation(Image::kImageRepCairoCache); |
293 return rep->AsImageRepCairo()->surface(); | 295 return rep->AsImageRepCairo()->surface(); |
294 } | 296 } |
295 #endif | 297 #endif |
296 | 298 |
297 #if defined(OS_MACOSX) | 299 #if defined(OS_MACOSX) |
298 NSImage* Image::ToNSImage() const { | 300 NSImage* Image::ToNSImage() const { |
299 internal::ImageRep* rep = GetRepresentation(Image::kImageRepCocoa); | 301 internal::ImageRep* rep = GetRepresentation(Image::kImageRepCocoa); |
300 return rep->AsImageRepCocoa()->image(); | 302 return rep->AsImageRepCocoa()->image(); |
301 } | 303 } |
302 #endif | 304 #endif |
303 | 305 |
306 ImageSkia* Image::CopyImageSkia() const { | |
307 return new ImageSkia(*ToImageSkia()); | |
308 } | |
309 | |
304 SkBitmap* Image::CopySkBitmap() const { | 310 SkBitmap* Image::CopySkBitmap() const { |
305 return new SkBitmap(*ToSkBitmap()); | 311 return new SkBitmap(*ToSkBitmap()); |
306 } | 312 } |
307 | 313 |
308 #if defined(TOOLKIT_GTK) | 314 #if defined(TOOLKIT_GTK) |
309 GdkPixbuf* Image::CopyGdkPixbuf() const { | 315 GdkPixbuf* Image::CopyGdkPixbuf() const { |
310 GdkPixbuf* pixbuf = ToGdkPixbuf(); | 316 GdkPixbuf* pixbuf = ToGdkPixbuf(); |
311 g_object_ref(pixbuf); | 317 g_object_ref(pixbuf); |
312 return pixbuf; | 318 return pixbuf; |
313 } | 319 } |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
379 internal::ImageRepGdk* pixbuf_rep = default_rep->AsImageRepGdk(); | 385 internal::ImageRepGdk* pixbuf_rep = default_rep->AsImageRepGdk(); |
380 rep = new internal::ImageRepSkia(new ImageSkia( | 386 rep = new internal::ImageRepSkia(new ImageSkia( |
381 internal::GdkPixbufToSkBitmap(pixbuf_rep->pixbuf()))); | 387 internal::GdkPixbufToSkBitmap(pixbuf_rep->pixbuf()))); |
382 } | 388 } |
383 // We don't do conversions from CairoCachedSurfaces to Skia because the | 389 // We don't do conversions from CairoCachedSurfaces to Skia because the |
384 // data lives on the display server and we'll always have a GdkPixbuf if we | 390 // data lives on the display server and we'll always have a GdkPixbuf if we |
385 // have a CairoCachedSurface. | 391 // have a CairoCachedSurface. |
386 #elif defined(OS_MACOSX) | 392 #elif defined(OS_MACOSX) |
387 if (storage_->default_representation_type() == Image::kImageRepCocoa) { | 393 if (storage_->default_representation_type() == Image::kImageRepCocoa) { |
388 internal::ImageRepCocoa* nsimage_rep = default_rep->AsImageRepCocoa(); | 394 internal::ImageRepCocoa* nsimage_rep = default_rep->AsImageRepCocoa(); |
389 std::vector<const SkBitmap*> bitmaps; | 395 ImageSkia image_skia = internal::NSImageToImageSkia(nsimage_rep->image()); |
390 CHECK(internal::NSImageToSkBitmaps(nsimage_rep->image(), &bitmaps)); | 396 rep = new internal::ImageRepSkia(new ImageSkia(image_skia)); |
391 rep = new internal::ImageRepSkia(new ImageSkia(bitmaps)); | |
392 } | 397 } |
393 #endif | 398 #endif |
394 CHECK(rep); | 399 CHECK(rep); |
395 AddRepresentation(rep); | 400 AddRepresentation(rep); |
396 return rep; | 401 return rep; |
397 } | 402 } |
398 #if defined(TOOLKIT_GTK) | 403 #if defined(TOOLKIT_GTK) |
399 else if (rep_type == Image::kImageRepCairoCache) { | 404 else if (rep_type == Image::kImageRepCairoCache) { |
400 // Handle any-to-Cairo conversion. This may recursively create an | 405 // Handle any-to-Cairo conversion. This may recursively create an |
401 // intermediate pixbuf before we send the data to the display server. | 406 // intermediate pixbuf before we send the data to the display server. |
402 internal::ImageRep* rep = GetRepresentation(Image::kImageRepGdk); | 407 internal::ImageRep* rep = GetRepresentation(Image::kImageRepGdk); |
403 internal::ImageRepCairoCached* native_rep = | 408 internal::ImageRepCairoCached* native_rep = |
404 new internal::ImageRepCairoCached(rep->AsImageRepGdk()->pixbuf()); | 409 new internal::ImageRepCairoCached(rep->AsImageRepGdk()->pixbuf()); |
405 | 410 |
406 CHECK(native_rep); | 411 CHECK(native_rep); |
407 AddRepresentation(native_rep); | 412 AddRepresentation(native_rep); |
408 return native_rep; | 413 return native_rep; |
409 } | 414 } |
410 #endif | 415 #endif |
411 | 416 |
412 // Handle Skia-to-native conversions. | 417 // Handle Skia-to-native conversions. |
413 if (default_rep->type() == Image::kImageRepSkia) { | 418 if (default_rep->type() == Image::kImageRepSkia) { |
414 internal::ImageRep* native_rep = NULL; | 419 internal::ImageRep* native_rep = NULL; |
415 #if defined(USE_AURA) | 420 #if defined(USE_AURA) |
416 NOTIMPLEMENTED(); | 421 NOTIMPLEMENTED(); |
417 #elif defined(TOOLKIT_GTK) | 422 #elif defined(TOOLKIT_GTK) |
418 if (rep_type == Image::kImageRepGdk) { | 423 if (rep_type == Image::kImageRepGdk) { |
419 GdkPixbuf* pixbuf = gfx::GdkPixbufFromSkBitmap( | 424 GdkPixbuf* pixbuf = gfx::GdkPixbufFromSkBitmap( |
420 default_rep->AsImageRepSkia()->image()->bitmaps()[0]); | 425 &default_rep->AsImageRepSkia()->image()->bitmaps()[0]); |
421 native_rep = new internal::ImageRepGdk(pixbuf); | 426 native_rep = new internal::ImageRepGdk(pixbuf); |
422 } | 427 } |
423 #elif defined(OS_MACOSX) | 428 #elif defined(OS_MACOSX) |
424 if (rep_type == Image::kImageRepCocoa) { | 429 if (rep_type == Image::kImageRepCocoa) { |
425 NSImage* image = gfx::SkBitmapsToNSImage( | 430 NSImage* image = internal::ImageSkiaToNSImage( |
426 default_rep->AsImageRepSkia()->image()->bitmaps()); | 431 default_rep->AsImageRepSkia()->image()); |
427 base::mac::NSObjectRetain(image); | 432 base::mac::NSObjectRetain(image); |
428 native_rep = new internal::ImageRepCocoa(image); | 433 native_rep = new internal::ImageRepCocoa(image); |
429 } | 434 } |
430 #endif | 435 #endif |
431 CHECK(native_rep); | 436 CHECK(native_rep); |
432 AddRepresentation(native_rep); | 437 AddRepresentation(native_rep); |
433 return native_rep; | 438 return native_rep; |
434 } | 439 } |
435 | 440 |
436 // Something went seriously wrong... | 441 // Something went seriously wrong... |
437 return NULL; | 442 return NULL; |
438 } | 443 } |
439 | 444 |
440 void Image::AddRepresentation(internal::ImageRep* rep) const { | 445 void Image::AddRepresentation(internal::ImageRep* rep) const { |
441 CHECK(storage_.get()); | 446 CHECK(storage_.get()); |
442 storage_->representations().insert(std::make_pair(rep->type(), rep)); | 447 storage_->representations().insert(std::make_pair(rep->type(), rep)); |
443 } | 448 } |
444 | 449 |
445 } // namespace gfx | 450 } // namespace gfx |
OLD | NEW |