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/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/stl_util.h" | 10 #include "base/stl_util.h" |
11 #include "third_party/skia/include/core/SkBitmap.h" | 11 #include "third_party/skia/include/core/SkBitmap.h" |
12 | 12 |
13 #if defined(TOOLKIT_USES_GTK) | 13 #if defined(TOOLKIT_USES_GTK) |
14 #include <gdk-pixbuf/gdk-pixbuf.h> | 14 #include <gdk-pixbuf/gdk-pixbuf.h> |
15 #include <glib-object.h> | 15 #include <glib-object.h> |
16 #include "ui/gfx/canvas_skia.h" | 16 #include "ui/gfx/canvas_skia.h" |
17 #include "ui/gfx/gtk_util.h" | 17 #include "ui/gfx/gtk_util.h" |
18 #include "ui/gfx/image/cairo_cached_surface.h" | |
18 #elif defined(OS_MACOSX) | 19 #elif defined(OS_MACOSX) |
19 #include "base/mac/mac_util.h" | 20 #include "base/mac/mac_util.h" |
20 #include "skia/ext/skia_utils_mac.h" | 21 #include "skia/ext/skia_utils_mac.h" |
21 #endif | 22 #endif |
22 | 23 |
23 namespace gfx { | 24 namespace gfx { |
24 | 25 |
25 namespace internal { | 26 namespace internal { |
26 | 27 |
27 #if defined(OS_MACOSX) | 28 #if defined(OS_MACOSX) |
28 // This is a wrapper around gfx::NSImageToSkBitmap() because this cross-platform | 29 // This is a wrapper around gfx::NSImageToSkBitmap() because this cross-platform |
29 // file cannot include the [square brackets] of ObjC. | 30 // file cannot include the [square brackets] of ObjC. |
30 bool NSImageToSkBitmaps(NSImage* image, std::vector<const SkBitmap*>* bitmaps); | 31 bool NSImageToSkBitmaps(NSImage* image, std::vector<const SkBitmap*>* bitmaps); |
31 #endif | 32 #endif |
32 | 33 |
33 #if defined(TOOLKIT_USES_GTK) | 34 #if defined(TOOLKIT_USES_GTK) |
34 const SkBitmap* GdkPixbufToSkBitmap(GdkPixbuf* pixbuf) { | 35 const SkBitmap* GdkPixbufToSkBitmap(GdkPixbuf* pixbuf) { |
35 gfx::CanvasSkia canvas(gdk_pixbuf_get_width(pixbuf), | 36 gfx::CanvasSkia canvas(gdk_pixbuf_get_width(pixbuf), |
36 gdk_pixbuf_get_height(pixbuf), | 37 gdk_pixbuf_get_height(pixbuf), |
37 /*is_opaque=*/false); | 38 /*is_opaque=*/false); |
38 canvas.DrawGdkPixbuf(pixbuf, 0, 0); | 39 canvas.DrawGdkPixbuf(pixbuf, 0, 0); |
39 return new SkBitmap(canvas.ExtractBitmap()); | 40 return new SkBitmap(canvas.ExtractBitmap()); |
40 } | 41 } |
41 #endif | 42 #endif |
42 | 43 |
43 class ImageRepSkia; | 44 class ImageRepSkia; |
44 class ImageRepGdk; | 45 class ImageRepGdk; |
45 class ImageRepCocoa; | 46 class ImageRepCocoa; |
47 class ImageRepCairoCached; | |
Robert Sesek
2011/12/01 19:30:35
nit: put below ImageRepGdk
| |
46 | 48 |
47 // An ImageRep is the object that holds the backing memory for an Image. Each | 49 // 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 | 50 // RepresentationType has an ImageRep subclass that is responsible for freeing |
49 // the memory that the ImageRep holds. When an ImageRep is created, it expects | 51 // 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 | 52 // to take ownership of the image, without having to retain it or increase its |
51 // reference count. | 53 // reference count. |
52 class ImageRep { | 54 class ImageRep { |
53 public: | 55 public: |
54 explicit ImageRep(Image::RepresentationType rep) : type_(rep) {} | 56 explicit ImageRep(Image::RepresentationType rep) : type_(rep) {} |
55 | 57 |
56 // Deletes the associated pixels of an ImageRep. | 58 // Deletes the associated pixels of an ImageRep. |
57 virtual ~ImageRep() {} | 59 virtual ~ImageRep() {} |
58 | 60 |
59 // Cast helpers ("fake RTTI"). | 61 // Cast helpers ("fake RTTI"). |
60 ImageRepSkia* AsImageRepSkia() { | 62 ImageRepSkia* AsImageRepSkia() { |
61 CHECK_EQ(type_, Image::kImageRepSkia); | 63 CHECK_EQ(type_, Image::kImageRepSkia); |
62 return reinterpret_cast<ImageRepSkia*>(this); | 64 return reinterpret_cast<ImageRepSkia*>(this); |
63 } | 65 } |
64 | 66 |
65 #if defined(TOOLKIT_USES_GTK) | 67 #if defined(TOOLKIT_USES_GTK) |
66 ImageRepGdk* AsImageRepGdk() { | 68 ImageRepGdk* AsImageRepGdk() { |
67 CHECK_EQ(type_, Image::kImageRepGdk); | 69 CHECK_EQ(type_, Image::kImageRepGdk); |
68 return reinterpret_cast<ImageRepGdk*>(this); | 70 return reinterpret_cast<ImageRepGdk*>(this); |
69 } | 71 } |
72 | |
73 ImageRepCairoCached* AsImageRepCairo() { | |
74 CHECK_EQ(type_, Image::kImageRepCairoCache); | |
75 return reinterpret_cast<ImageRepCairoCached*>(this); | |
76 } | |
70 #endif | 77 #endif |
71 | 78 |
72 #if defined(OS_MACOSX) | 79 #if defined(OS_MACOSX) |
73 ImageRepCocoa* AsImageRepCocoa() { | 80 ImageRepCocoa* AsImageRepCocoa() { |
74 CHECK_EQ(type_, Image::kImageRepCocoa); | 81 CHECK_EQ(type_, Image::kImageRepCocoa); |
75 return reinterpret_cast<ImageRepCocoa*>(this); | 82 return reinterpret_cast<ImageRepCocoa*>(this); |
76 } | 83 } |
77 #endif | 84 #endif |
78 | 85 |
79 Image::RepresentationType type() const { return type_; } | 86 Image::RepresentationType type() const { return type_; } |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
126 } | 133 } |
127 } | 134 } |
128 | 135 |
129 GdkPixbuf* pixbuf() const { return pixbuf_; } | 136 GdkPixbuf* pixbuf() const { return pixbuf_; } |
130 | 137 |
131 private: | 138 private: |
132 GdkPixbuf* pixbuf_; | 139 GdkPixbuf* pixbuf_; |
133 | 140 |
134 DISALLOW_COPY_AND_ASSIGN(ImageRepGdk); | 141 DISALLOW_COPY_AND_ASSIGN(ImageRepGdk); |
135 }; | 142 }; |
143 | |
144 // Represents data that lives on the display server instead of in the client. | |
145 class ImageRepCairoCached : public ImageRep { | |
146 public: | |
147 explicit ImageRepCairoCached(GdkPixbuf* pixbuf) | |
148 : ImageRep(Image::kImageRepCairoCache), | |
149 cairo_cache_(new CairoCachedSurface) { | |
150 CHECK(pixbuf); | |
151 cairo_cache_->UsePixbuf(pixbuf); | |
152 } | |
153 | |
154 virtual ~ImageRepCairoCached() { | |
155 delete cairo_cache_; | |
156 } | |
157 | |
158 CairoCachedSurface* surface() const { return cairo_cache_; } | |
159 | |
160 private: | |
161 CairoCachedSurface* cairo_cache_; | |
162 | |
163 DISALLOW_COPY_AND_ASSIGN(ImageRepCairoCached); | |
164 }; | |
165 | |
Robert Sesek
2011/12/01 19:30:35
nit: remove blank line
| |
136 #endif | 166 #endif |
Robert Sesek
2011/12/01 19:30:35
// defined(TOOLKIT_USES_GTK)
| |
137 | 167 |
138 #if defined(OS_MACOSX) | 168 #if defined(OS_MACOSX) |
139 class ImageRepCocoa : public ImageRep { | 169 class ImageRepCocoa : public ImageRep { |
140 public: | 170 public: |
141 explicit ImageRepCocoa(NSImage* image) | 171 explicit ImageRepCocoa(NSImage* image) |
142 : ImageRep(Image::kImageRepCocoa), | 172 : ImageRep(Image::kImageRepCocoa), |
143 image_(image) { | 173 image_(image) { |
144 CHECK(image); | 174 CHECK(image); |
145 } | 175 } |
146 | 176 |
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
237 const SkBitmap* Image::ToSkBitmap() const { | 267 const SkBitmap* Image::ToSkBitmap() const { |
238 internal::ImageRep* rep = GetRepresentation(Image::kImageRepSkia); | 268 internal::ImageRep* rep = GetRepresentation(Image::kImageRepSkia); |
239 return rep->AsImageRepSkia()->bitmap(); | 269 return rep->AsImageRepSkia()->bitmap(); |
240 } | 270 } |
241 | 271 |
242 #if defined(TOOLKIT_USES_GTK) | 272 #if defined(TOOLKIT_USES_GTK) |
243 GdkPixbuf* Image::ToGdkPixbuf() const { | 273 GdkPixbuf* Image::ToGdkPixbuf() const { |
244 internal::ImageRep* rep = GetRepresentation(Image::kImageRepGdk); | 274 internal::ImageRep* rep = GetRepresentation(Image::kImageRepGdk); |
245 return rep->AsImageRepGdk()->pixbuf(); | 275 return rep->AsImageRepGdk()->pixbuf(); |
246 } | 276 } |
277 | |
278 CairoCachedSurface* const Image::ToCairo() const { | |
279 internal::ImageRep* rep = GetRepresentation(Image::kImageRepCairoCache); | |
280 return rep->AsImageRepCairo()->surface(); | |
281 } | |
247 #endif | 282 #endif |
248 | 283 |
249 #if defined(OS_MACOSX) | 284 #if defined(OS_MACOSX) |
250 NSImage* Image::ToNSImage() const { | 285 NSImage* Image::ToNSImage() const { |
251 internal::ImageRep* rep = GetRepresentation(Image::kImageRepCocoa); | 286 internal::ImageRep* rep = GetRepresentation(Image::kImageRepCocoa); |
252 return rep->AsImageRepCocoa()->image(); | 287 return rep->AsImageRepCocoa()->image(); |
253 } | 288 } |
254 #endif | 289 #endif |
255 | 290 |
256 const SkBitmap* Image::CopySkBitmap() const { | 291 const SkBitmap* Image::CopySkBitmap() const { |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
329 // from the default rep. | 364 // from the default rep. |
330 | 365 |
331 // Handle native-to-Skia conversion. | 366 // Handle native-to-Skia conversion. |
332 if (rep_type == Image::kImageRepSkia) { | 367 if (rep_type == Image::kImageRepSkia) { |
333 internal::ImageRepSkia* rep = NULL; | 368 internal::ImageRepSkia* rep = NULL; |
334 #if defined(TOOLKIT_USES_GTK) | 369 #if defined(TOOLKIT_USES_GTK) |
335 if (storage_->default_representation_type() == Image::kImageRepGdk) { | 370 if (storage_->default_representation_type() == Image::kImageRepGdk) { |
336 internal::ImageRepGdk* pixbuf_rep = default_rep->AsImageRepGdk(); | 371 internal::ImageRepGdk* pixbuf_rep = default_rep->AsImageRepGdk(); |
337 rep = new internal::ImageRepSkia( | 372 rep = new internal::ImageRepSkia( |
338 internal::GdkPixbufToSkBitmap(pixbuf_rep->pixbuf())); | 373 internal::GdkPixbufToSkBitmap(pixbuf_rep->pixbuf())); |
339 } | 374 } |
Robert Sesek
2011/12/01 19:30:35
What about going from Cairo to Skia?
Elliot Glaysher
2011/12/01 20:30:11
We shouldn't allow this; it doesn't make sense.
Robert Sesek
2011/12/01 20:56:14
Can you add a comment here to that effect so that
| |
340 #elif defined(OS_MACOSX) | 375 #elif defined(OS_MACOSX) |
341 if (storage_->default_representation_type() == Image::kImageRepCocoa) { | 376 if (storage_->default_representation_type() == Image::kImageRepCocoa) { |
342 internal::ImageRepCocoa* nsimage_rep = default_rep->AsImageRepCocoa(); | 377 internal::ImageRepCocoa* nsimage_rep = default_rep->AsImageRepCocoa(); |
343 std::vector<const SkBitmap*> bitmaps; | 378 std::vector<const SkBitmap*> bitmaps; |
344 CHECK(internal::NSImageToSkBitmaps(nsimage_rep->image(), &bitmaps)); | 379 CHECK(internal::NSImageToSkBitmaps(nsimage_rep->image(), &bitmaps)); |
345 rep = new internal::ImageRepSkia(bitmaps); | 380 rep = new internal::ImageRepSkia(bitmaps); |
346 } | 381 } |
347 #endif | 382 #endif |
348 CHECK(rep); | 383 CHECK(rep); |
349 AddRepresentation(rep); | 384 AddRepresentation(rep); |
350 return rep; | 385 return rep; |
351 } | 386 } |
352 | 387 |
353 // Handle Skia-to-native conversions. | 388 // Handle Skia-to-native conversions. |
354 if (default_rep->type() == Image::kImageRepSkia) { | 389 if (default_rep->type() == Image::kImageRepSkia) { |
355 internal::ImageRepSkia* skia_rep = default_rep->AsImageRepSkia(); | 390 internal::ImageRepSkia* skia_rep = default_rep->AsImageRepSkia(); |
356 internal::ImageRep* native_rep = NULL; | 391 internal::ImageRep* native_rep = NULL; |
357 #if defined(USE_AURA) | 392 #if defined(USE_AURA) |
358 static_cast<void>(skia_rep); | 393 static_cast<void>(skia_rep); |
359 NOTIMPLEMENTED(); | 394 NOTIMPLEMENTED(); |
360 #elif defined(TOOLKIT_USES_GTK) | 395 #elif defined(TOOLKIT_USES_GTK) |
361 if (rep_type == Image::kImageRepGdk) { | 396 if (rep_type == Image::kImageRepGdk) { |
362 GdkPixbuf* pixbuf = gfx::GdkPixbufFromSkBitmap(skia_rep->bitmap()); | 397 GdkPixbuf* pixbuf = gfx::GdkPixbufFromSkBitmap(skia_rep->bitmap()); |
363 native_rep = new internal::ImageRepGdk(pixbuf); | 398 native_rep = new internal::ImageRepGdk(pixbuf); |
399 } else if (rep_type == Image::kImageRepCairoCache) { | |
400 // Send the data that we have in process right now to the display server. | |
401 internal::ImageRep* rep = GetRepresentation(Image::kImageRepGdk); | |
402 native_rep = new internal::ImageRepCairoCached( | |
403 rep->AsImageRepGdk()->pixbuf()); | |
364 } | 404 } |
365 #elif defined(OS_MACOSX) | 405 #elif defined(OS_MACOSX) |
366 if (rep_type == Image::kImageRepCocoa) { | 406 if (rep_type == Image::kImageRepCocoa) { |
367 NSImage* image = gfx::SkBitmapsToNSImage(skia_rep->bitmaps()); | 407 NSImage* image = gfx::SkBitmapsToNSImage(skia_rep->bitmaps()); |
368 base::mac::NSObjectRetain(image); | 408 base::mac::NSObjectRetain(image); |
369 native_rep = new internal::ImageRepCocoa(image); | 409 native_rep = new internal::ImageRepCocoa(image); |
370 } | 410 } |
371 #endif | 411 #endif |
372 CHECK(native_rep); | 412 CHECK(native_rep); |
373 AddRepresentation(native_rep); | 413 AddRepresentation(native_rep); |
(...skipping 12 matching lines...) Expand all Loading... | |
386 return GetRepresentation(Image::kImageRepSkia)->AsImageRepSkia()-> | 426 return GetRepresentation(Image::kImageRepSkia)->AsImageRepSkia()-> |
387 bitmaps().size(); | 427 bitmaps().size(); |
388 } | 428 } |
389 | 429 |
390 const SkBitmap* Image::GetSkBitmapAtIndex(size_t index) const { | 430 const SkBitmap* Image::GetSkBitmapAtIndex(size_t index) const { |
391 return GetRepresentation(Image::kImageRepSkia)->AsImageRepSkia()-> | 431 return GetRepresentation(Image::kImageRepSkia)->AsImageRepSkia()-> |
392 bitmaps()[index]; | 432 bitmaps()[index]; |
393 } | 433 } |
394 | 434 |
395 } // namespace gfx | 435 } // namespace gfx |
OLD | NEW |