| 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); |
| 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 = | 238 internal::ImageRepSkia* rep = |
| 230 new internal::ImageRepSkia(new ImageSkia(new SkBitmap(bitmap))); | 239 new internal::ImageRepSkia(new ImageSkia(bitmap)); |
| 231 AddRepresentation(rep); | 240 AddRepresentation(rep); |
| 232 } | 241 } |
| 233 | 242 |
| 234 Image::Image(const std::vector<const SkBitmap*>& bitmaps) | |
| 235 : storage_(new internal::ImageStorage(Image::kImageRepSkia)) { | |
| 236 internal::ImageRepSkia* rep = new internal::ImageRepSkia( | |
| 237 new ImageSkia(bitmaps)); | |
| 238 AddRepresentation(rep); | |
| 239 } | |
| 240 | |
| 241 #if defined(TOOLKIT_GTK) | 243 #if defined(TOOLKIT_GTK) |
| 242 Image::Image(GdkPixbuf* pixbuf) | 244 Image::Image(GdkPixbuf* pixbuf) |
| 243 : storage_(new internal::ImageStorage(Image::kImageRepGdk)) { | 245 : storage_(new internal::ImageStorage(Image::kImageRepGdk)) { |
| 244 internal::ImageRepGdk* rep = new internal::ImageRepGdk(pixbuf); | 246 internal::ImageRepGdk* rep = new internal::ImageRepGdk(pixbuf); |
| 245 AddRepresentation(rep); | 247 AddRepresentation(rep); |
| 246 } | 248 } |
| 247 #endif | 249 #endif |
| 248 | 250 |
| 249 #if defined(OS_MACOSX) | 251 #if defined(OS_MACOSX) |
| 250 Image::Image(NSImage* image) | 252 Image::Image(NSImage* image) |
| 251 : storage_(new internal::ImageStorage(Image::kImageRepCocoa)) { | 253 : storage_(new internal::ImageStorage(Image::kImageRepCocoa)) { |
| 252 internal::ImageRepCocoa* rep = new internal::ImageRepCocoa(image); | 254 internal::ImageRepCocoa* rep = new internal::ImageRepCocoa(image); |
| 253 AddRepresentation(rep); | 255 AddRepresentation(rep); |
| 254 } | 256 } |
| 255 #endif | 257 #endif |
| 256 | 258 |
| 257 Image::Image(const Image& other) : storage_(other.storage_) { | 259 Image::Image(const Image& other) : storage_(other.storage_) { |
| 258 } | 260 } |
| 259 | 261 |
| 260 Image& Image::operator=(const Image& other) { | 262 Image& Image::operator=(const Image& other) { |
| 261 storage_ = other.storage_; | 263 storage_ = other.storage_; |
| 262 return *this; | 264 return *this; |
| 263 } | 265 } |
| 264 | 266 |
| 265 Image::~Image() { | 267 Image::~Image() { |
| 266 } | 268 } |
| 267 | 269 |
| 268 const SkBitmap* Image::ToSkBitmap() const { | 270 const SkBitmap* Image::ToSkBitmap() const { |
| 269 internal::ImageRep* rep = GetRepresentation(Image::kImageRepSkia); | 271 internal::ImageRep* rep = GetRepresentation(Image::kImageRepSkia); |
| 270 return rep->AsImageRepSkia()->image()->bitmaps()[0]; | 272 return rep->AsImageRepSkia()->image()->bitmap(); |
| 271 } | 273 } |
| 272 | 274 |
| 273 const ImageSkia* Image::ToImageSkia() const { | 275 const ImageSkia* Image::ToImageSkia() const { |
| 274 internal::ImageRep* rep = GetRepresentation(Image::kImageRepSkia); | 276 internal::ImageRep* rep = GetRepresentation(Image::kImageRepSkia); |
| 275 return rep->AsImageRepSkia()->image(); | 277 return rep->AsImageRepSkia()->image(); |
| 276 } | 278 } |
| 277 | 279 |
| 278 #if defined(TOOLKIT_GTK) | 280 #if defined(TOOLKIT_GTK) |
| 279 GdkPixbuf* Image::ToGdkPixbuf() const { | 281 GdkPixbuf* Image::ToGdkPixbuf() const { |
| 280 internal::ImageRep* rep = GetRepresentation(Image::kImageRepGdk); | 282 internal::ImageRep* rep = GetRepresentation(Image::kImageRepGdk); |
| 281 return rep->AsImageRepGdk()->pixbuf(); | 283 return rep->AsImageRepGdk()->pixbuf(); |
| 282 } | 284 } |
| 283 | 285 |
| 284 CairoCachedSurface* const Image::ToCairo() const { | 286 CairoCachedSurface* const Image::ToCairo() const { |
| 285 internal::ImageRep* rep = GetRepresentation(Image::kImageRepCairoCache); | 287 internal::ImageRep* rep = GetRepresentation(Image::kImageRepCairoCache); |
| 286 return rep->AsImageRepCairo()->surface(); | 288 return rep->AsImageRepCairo()->surface(); |
| 287 } | 289 } |
| 288 #endif | 290 #endif |
| 289 | 291 |
| 290 #if defined(OS_MACOSX) | 292 #if defined(OS_MACOSX) |
| 291 NSImage* Image::ToNSImage() const { | 293 NSImage* Image::ToNSImage() const { |
| 292 internal::ImageRep* rep = GetRepresentation(Image::kImageRepCocoa); | 294 internal::ImageRep* rep = GetRepresentation(Image::kImageRepCocoa); |
| 293 return rep->AsImageRepCocoa()->image(); | 295 return rep->AsImageRepCocoa()->image(); |
| 294 } | 296 } |
| 295 #endif | 297 #endif |
| 296 | 298 |
| 299 ImageSkia* Image::CopyImageSkia() const { |
| 300 return new ImageSkia(*ToImageSkia()); |
| 301 } |
| 302 |
| 297 SkBitmap* Image::CopySkBitmap() const { | 303 SkBitmap* Image::CopySkBitmap() const { |
| 298 return new SkBitmap(*ToSkBitmap()); | 304 return new SkBitmap(*ToSkBitmap()); |
| 299 } | 305 } |
| 300 | 306 |
| 301 #if defined(TOOLKIT_GTK) | 307 #if defined(TOOLKIT_GTK) |
| 302 GdkPixbuf* Image::CopyGdkPixbuf() const { | 308 GdkPixbuf* Image::CopyGdkPixbuf() const { |
| 303 GdkPixbuf* pixbuf = ToGdkPixbuf(); | 309 GdkPixbuf* pixbuf = ToGdkPixbuf(); |
| 304 g_object_ref(pixbuf); | 310 g_object_ref(pixbuf); |
| 305 return pixbuf; | 311 return pixbuf; |
| 306 } | 312 } |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 363 | 369 |
| 364 // At this point, the requested rep does not exist, so it must be converted | 370 // At this point, the requested rep does not exist, so it must be converted |
| 365 // from the default rep. | 371 // from the default rep. |
| 366 | 372 |
| 367 // Handle native-to-Skia conversion. | 373 // Handle native-to-Skia conversion. |
| 368 if (rep_type == Image::kImageRepSkia) { | 374 if (rep_type == Image::kImageRepSkia) { |
| 369 internal::ImageRepSkia* rep = NULL; | 375 internal::ImageRepSkia* rep = NULL; |
| 370 #if defined(TOOLKIT_GTK) | 376 #if defined(TOOLKIT_GTK) |
| 371 if (storage_->default_representation_type() == Image::kImageRepGdk) { | 377 if (storage_->default_representation_type() == Image::kImageRepGdk) { |
| 372 internal::ImageRepGdk* pixbuf_rep = default_rep->AsImageRepGdk(); | 378 internal::ImageRepGdk* pixbuf_rep = default_rep->AsImageRepGdk(); |
| 373 rep = new internal::ImageRepSkia(new ImageSkia( | 379 scoped_ptr<const SkBitmap> bitmap( |
| 374 internal::GdkPixbufToSkBitmap(pixbuf_rep->pixbuf()))); | 380 internal::GdkPixbufToSkBitmap(pixbuf_rep->pixbuf())); |
| 381 rep = new internal::ImageRepSkia(new ImageSkia(*bitmap)); |
| 375 } | 382 } |
| 376 // We don't do conversions from CairoCachedSurfaces to Skia because the | 383 // We don't do conversions from CairoCachedSurfaces to Skia because the |
| 377 // data lives on the display server and we'll always have a GdkPixbuf if we | 384 // data lives on the display server and we'll always have a GdkPixbuf if we |
| 378 // have a CairoCachedSurface. | 385 // have a CairoCachedSurface. |
| 379 #elif defined(OS_MACOSX) | 386 #elif defined(OS_MACOSX) |
| 380 if (storage_->default_representation_type() == Image::kImageRepCocoa) { | 387 if (storage_->default_representation_type() == Image::kImageRepCocoa) { |
| 381 internal::ImageRepCocoa* nsimage_rep = default_rep->AsImageRepCocoa(); | 388 internal::ImageRepCocoa* nsimage_rep = default_rep->AsImageRepCocoa(); |
| 382 std::vector<const SkBitmap*> bitmaps; | 389 ImageSkia image_skia = internal::NSImageToImageSkia(nsimage_rep->image()); |
| 383 CHECK(internal::NSImageToSkBitmaps(nsimage_rep->image(), &bitmaps)); | 390 rep = new internal::ImageRepSkia(new ImageSkia(image_skia)); |
| 384 rep = new internal::ImageRepSkia(new ImageSkia(bitmaps)); | |
| 385 } | 391 } |
| 386 #endif | 392 #endif |
| 387 CHECK(rep); | 393 CHECK(rep); |
| 388 AddRepresentation(rep); | 394 AddRepresentation(rep); |
| 389 return rep; | 395 return rep; |
| 390 } | 396 } |
| 391 #if defined(TOOLKIT_GTK) | 397 #if defined(TOOLKIT_GTK) |
| 392 else if (rep_type == Image::kImageRepCairoCache) { | 398 else if (rep_type == Image::kImageRepCairoCache) { |
| 393 // Handle any-to-Cairo conversion. This may recursively create an | 399 // Handle any-to-Cairo conversion. This may recursively create an |
| 394 // intermediate pixbuf before we send the data to the display server. | 400 // intermediate pixbuf before we send the data to the display server. |
| 395 internal::ImageRep* rep = GetRepresentation(Image::kImageRepGdk); | 401 internal::ImageRep* rep = GetRepresentation(Image::kImageRepGdk); |
| 396 internal::ImageRepCairoCached* native_rep = | 402 internal::ImageRepCairoCached* native_rep = |
| 397 new internal::ImageRepCairoCached(rep->AsImageRepGdk()->pixbuf()); | 403 new internal::ImageRepCairoCached(rep->AsImageRepGdk()->pixbuf()); |
| 398 | 404 |
| 399 CHECK(native_rep); | 405 CHECK(native_rep); |
| 400 AddRepresentation(native_rep); | 406 AddRepresentation(native_rep); |
| 401 return native_rep; | 407 return native_rep; |
| 402 } | 408 } |
| 403 #endif | 409 #endif |
| 404 | 410 |
| 405 // Handle Skia-to-native conversions. | 411 // Handle Skia-to-native conversions. |
| 406 if (default_rep->type() == Image::kImageRepSkia) { | 412 if (default_rep->type() == Image::kImageRepSkia) { |
| 407 internal::ImageRep* native_rep = NULL; | 413 internal::ImageRep* native_rep = NULL; |
| 408 #if defined(USE_AURA) | 414 #if defined(USE_AURA) |
| 409 NOTIMPLEMENTED(); | 415 NOTIMPLEMENTED(); |
| 410 #elif defined(TOOLKIT_GTK) | 416 #elif defined(TOOLKIT_GTK) |
| 411 if (rep_type == Image::kImageRepGdk) { | 417 if (rep_type == Image::kImageRepGdk) { |
| 412 GdkPixbuf* pixbuf = gfx::GdkPixbufFromSkBitmap( | 418 GdkPixbuf* pixbuf = gfx::GdkPixbufFromSkBitmap( |
| 413 default_rep->AsImageRepSkia()->image()->bitmaps()[0]); | 419 default_rep->AsImageRepSkia()->image()->bitmap()); |
| 414 native_rep = new internal::ImageRepGdk(pixbuf); | 420 native_rep = new internal::ImageRepGdk(pixbuf); |
| 415 } | 421 } |
| 416 #elif defined(OS_MACOSX) | 422 #elif defined(OS_MACOSX) |
| 417 if (rep_type == Image::kImageRepCocoa) { | 423 if (rep_type == Image::kImageRepCocoa) { |
| 418 NSImage* image = gfx::SkBitmapsToNSImage( | 424 NSImage* image = internal::ImageSkiaToNSImage( |
| 419 default_rep->AsImageRepSkia()->image()->bitmaps()); | 425 default_rep->AsImageRepSkia()->image()); |
| 420 base::mac::NSObjectRetain(image); | 426 base::mac::NSObjectRetain(image); |
| 421 native_rep = new internal::ImageRepCocoa(image); | 427 native_rep = new internal::ImageRepCocoa(image); |
| 422 } | 428 } |
| 423 #endif | 429 #endif |
| 424 CHECK(native_rep); | 430 CHECK(native_rep); |
| 425 AddRepresentation(native_rep); | 431 AddRepresentation(native_rep); |
| 426 return native_rep; | 432 return native_rep; |
| 427 } | 433 } |
| 428 | 434 |
| 429 // Something went seriously wrong... | 435 // Something went seriously wrong... |
| 430 return NULL; | 436 return NULL; |
| 431 } | 437 } |
| 432 | 438 |
| 433 void Image::AddRepresentation(internal::ImageRep* rep) const { | 439 void Image::AddRepresentation(internal::ImageRep* rep) const { |
| 434 CHECK(storage_.get()); | 440 CHECK(storage_.get()); |
| 435 storage_->representations().insert(std::make_pair(rep->type(), rep)); | 441 storage_->representations().insert(std::make_pair(rep->type(), rep)); |
| 436 } | 442 } |
| 437 | 443 |
| 438 } // namespace gfx | 444 } // namespace gfx |
| OLD | NEW |