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 18 matching lines...) Expand all Loading... |
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 ImageSkia NSImageToImageSkia(NSImage* image); | 34 ImageSkia NSImageToImageSkia(NSImage* image); |
35 NSImage* ImageSkiaToNSImage(const ImageSkia* image); | 35 NSImage* ImageSkiaToNSImage(const ImageSkia* image); |
36 #endif | 36 #endif |
37 | 37 |
38 #if defined(TOOLKIT_GTK) | 38 #if defined(TOOLKIT_GTK) |
39 const SkBitmap* GdkPixbufToSkBitmap(GdkPixbuf* pixbuf) { | 39 const ImageSkia GdkPixbufToImageSkia(GdkPixbuf* pixbuf) { |
40 CHECK(pixbuf); | 40 CHECK(pixbuf); |
41 gfx::Canvas canvas(gfx::Size(gdk_pixbuf_get_width(pixbuf), | 41 gfx::Canvas canvas(gfx::Size(gdk_pixbuf_get_width(pixbuf), |
42 gdk_pixbuf_get_height(pixbuf)), false); | 42 gdk_pixbuf_get_height(pixbuf)), false); |
43 skia::ScopedPlatformPaint scoped_platform_paint(canvas.sk_canvas()); | 43 skia::ScopedPlatformPaint scoped_platform_paint(canvas.sk_canvas()); |
44 cairo_t* cr = scoped_platform_paint.GetPlatformSurface(); | 44 cairo_t* cr = scoped_platform_paint.GetPlatformSurface(); |
45 gdk_cairo_set_source_pixbuf(cr, pixbuf, 0, 0); | 45 gdk_cairo_set_source_pixbuf(cr, pixbuf, 0, 0); |
46 cairo_paint(cr); | 46 cairo_paint(cr); |
47 return new SkBitmap(canvas.ExtractBitmap()); | 47 return ImageSkia(SkBitmap(canvas.ExtractBitmap())); |
48 } | 48 } |
49 #endif | 49 #endif |
50 | 50 |
51 class ImageRepSkia; | 51 class ImageRepSkia; |
52 class ImageRepGdk; | 52 class ImageRepGdk; |
53 class ImageRepCairoCached; | 53 class ImageRepCairoCached; |
54 class ImageRepCocoa; | 54 class ImageRepCocoa; |
55 | 55 |
56 // An ImageRep is the object that holds the backing memory for an Image. Each | 56 // An ImageRep is the object that holds the backing memory for an Image. Each |
57 // RepresentationType has an ImageRep subclass that is responsible for freeing | 57 // RepresentationType has an ImageRep subclass that is responsible for freeing |
(...skipping 319 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
377 // At this point, the requested rep does not exist, so it must be converted | 377 // At this point, the requested rep does not exist, so it must be converted |
378 // from the default rep. | 378 // from the default rep. |
379 | 379 |
380 // Handle native-to-Skia conversion. | 380 // Handle native-to-Skia conversion. |
381 if (rep_type == Image::kImageRepSkia) { | 381 if (rep_type == Image::kImageRepSkia) { |
382 internal::ImageRepSkia* rep = NULL; | 382 internal::ImageRepSkia* rep = NULL; |
383 #if defined(TOOLKIT_GTK) | 383 #if defined(TOOLKIT_GTK) |
384 if (storage_->default_representation_type() == Image::kImageRepGdk) { | 384 if (storage_->default_representation_type() == Image::kImageRepGdk) { |
385 internal::ImageRepGdk* pixbuf_rep = default_rep->AsImageRepGdk(); | 385 internal::ImageRepGdk* pixbuf_rep = default_rep->AsImageRepGdk(); |
386 rep = new internal::ImageRepSkia(new ImageSkia( | 386 rep = new internal::ImageRepSkia(new ImageSkia( |
387 internal::GdkPixbufToSkBitmap(pixbuf_rep->pixbuf()))); | 387 internal::GdkPixbufToImageSkia(pixbuf_rep->pixbuf()))); |
388 } | 388 } |
389 // We don't do conversions from CairoCachedSurfaces to Skia because the | 389 // We don't do conversions from CairoCachedSurfaces to Skia because the |
390 // 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 |
391 // have a CairoCachedSurface. | 391 // have a CairoCachedSurface. |
392 #elif defined(OS_MACOSX) | 392 #elif defined(OS_MACOSX) |
393 if (storage_->default_representation_type() == Image::kImageRepCocoa) { | 393 if (storage_->default_representation_type() == Image::kImageRepCocoa) { |
394 internal::ImageRepCocoa* nsimage_rep = default_rep->AsImageRepCocoa(); | 394 internal::ImageRepCocoa* nsimage_rep = default_rep->AsImageRepCocoa(); |
395 ImageSkia image_skia = internal::NSImageToImageSkia(nsimage_rep->image()); | 395 ImageSkia image_skia = internal::NSImageToImageSkia(nsimage_rep->image()); |
396 rep = new internal::ImageRepSkia(new ImageSkia(image_skia)); | 396 rep = new internal::ImageRepSkia(new ImageSkia(image_skia)); |
397 } | 397 } |
(...skipping 17 matching lines...) Expand all Loading... |
415 #endif | 415 #endif |
416 | 416 |
417 // Handle Skia-to-native conversions. | 417 // Handle Skia-to-native conversions. |
418 if (default_rep->type() == Image::kImageRepSkia) { | 418 if (default_rep->type() == Image::kImageRepSkia) { |
419 internal::ImageRep* native_rep = NULL; | 419 internal::ImageRep* native_rep = NULL; |
420 #if defined(USE_AURA) | 420 #if defined(USE_AURA) |
421 NOTIMPLEMENTED(); | 421 NOTIMPLEMENTED(); |
422 #elif defined(TOOLKIT_GTK) | 422 #elif defined(TOOLKIT_GTK) |
423 if (rep_type == Image::kImageRepGdk) { | 423 if (rep_type == Image::kImageRepGdk) { |
424 GdkPixbuf* pixbuf = gfx::GdkPixbufFromSkBitmap( | 424 GdkPixbuf* pixbuf = gfx::GdkPixbufFromSkBitmap( |
425 default_rep->AsImageRepSkia()->image()->bitmap()); | 425 *default_rep->AsImageRepSkia()->image()->bitmap()); |
426 native_rep = new internal::ImageRepGdk(pixbuf); | 426 native_rep = new internal::ImageRepGdk(pixbuf); |
427 } | 427 } |
428 #elif defined(OS_MACOSX) | 428 #elif defined(OS_MACOSX) |
429 if (rep_type == Image::kImageRepCocoa) { | 429 if (rep_type == Image::kImageRepCocoa) { |
430 NSImage* image = internal::ImageSkiaToNSImage( | 430 NSImage* image = internal::ImageSkiaToNSImage( |
431 default_rep->AsImageRepSkia()->image()); | 431 default_rep->AsImageRepSkia()->image()); |
432 base::mac::NSObjectRetain(image); | 432 base::mac::NSObjectRetain(image); |
433 native_rep = new internal::ImageRepCocoa(image); | 433 native_rep = new internal::ImageRepCocoa(image); |
434 } | 434 } |
435 #endif | 435 #endif |
436 CHECK(native_rep); | 436 CHECK(native_rep); |
437 AddRepresentation(native_rep); | 437 AddRepresentation(native_rep); |
438 return native_rep; | 438 return native_rep; |
439 } | 439 } |
440 | 440 |
441 // Something went seriously wrong... | 441 // Something went seriously wrong... |
442 return NULL; | 442 return NULL; |
443 } | 443 } |
444 | 444 |
445 void Image::AddRepresentation(internal::ImageRep* rep) const { | 445 void Image::AddRepresentation(internal::ImageRep* rep) const { |
446 CHECK(storage_.get()); | 446 CHECK(storage_.get()); |
447 storage_->representations().insert(std::make_pair(rep->type(), rep)); | 447 storage_->representations().insert(std::make_pair(rep->type(), rep)); |
448 } | 448 } |
449 | 449 |
450 } // namespace gfx | 450 } // namespace gfx |
OLD | NEW |