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