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; |
| 46 class ImageRepCairoCached; |
45 class ImageRepCocoa; | 47 class ImageRepCocoa; |
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 }; |
136 #endif | 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 #endif // defined(TOOLKIT_USES_GTK) |
137 | 166 |
138 #if defined(OS_MACOSX) | 167 #if defined(OS_MACOSX) |
139 class ImageRepCocoa : public ImageRep { | 168 class ImageRepCocoa : public ImageRep { |
140 public: | 169 public: |
141 explicit ImageRepCocoa(NSImage* image) | 170 explicit ImageRepCocoa(NSImage* image) |
142 : ImageRep(Image::kImageRepCocoa), | 171 : ImageRep(Image::kImageRepCocoa), |
143 image_(image) { | 172 image_(image) { |
144 CHECK(image); | 173 CHECK(image); |
145 } | 174 } |
146 | 175 |
147 virtual ~ImageRepCocoa() { | 176 virtual ~ImageRepCocoa() { |
148 base::mac::NSObjectRelease(image_); | 177 base::mac::NSObjectRelease(image_); |
149 image_ = nil; | 178 image_ = nil; |
150 } | 179 } |
151 | 180 |
152 NSImage* image() const { return image_; } | 181 NSImage* image() const { return image_; } |
153 | 182 |
154 private: | 183 private: |
155 NSImage* image_; | 184 NSImage* image_; |
156 | 185 |
157 DISALLOW_COPY_AND_ASSIGN(ImageRepCocoa); | 186 DISALLOW_COPY_AND_ASSIGN(ImageRepCocoa); |
158 }; | 187 }; |
159 #endif | 188 #endif // defined(OS_MACOSX) |
160 | 189 |
161 // The Storage class acts similarly to the pixels in a SkBitmap: the Image | 190 // The Storage class acts similarly to the pixels in a SkBitmap: the Image |
162 // class holds a refptr instance of Storage, which in turn holds all the | 191 // class holds a refptr instance of Storage, which in turn holds all the |
163 // ImageReps. This way, the Image can be cheaply copied. | 192 // ImageReps. This way, the Image can be cheaply copied. |
164 class ImageStorage : public base::RefCounted<ImageStorage> { | 193 class ImageStorage : public base::RefCounted<ImageStorage> { |
165 public: | 194 public: |
166 ImageStorage(gfx::Image::RepresentationType default_type) | 195 ImageStorage(gfx::Image::RepresentationType default_type) |
167 : default_representation_type_(default_type) { | 196 : default_representation_type_(default_type) { |
168 } | 197 } |
169 | 198 |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
237 const SkBitmap* Image::ToSkBitmap() const { | 266 const SkBitmap* Image::ToSkBitmap() const { |
238 internal::ImageRep* rep = GetRepresentation(Image::kImageRepSkia); | 267 internal::ImageRep* rep = GetRepresentation(Image::kImageRepSkia); |
239 return rep->AsImageRepSkia()->bitmap(); | 268 return rep->AsImageRepSkia()->bitmap(); |
240 } | 269 } |
241 | 270 |
242 #if defined(TOOLKIT_USES_GTK) | 271 #if defined(TOOLKIT_USES_GTK) |
243 GdkPixbuf* Image::ToGdkPixbuf() const { | 272 GdkPixbuf* Image::ToGdkPixbuf() const { |
244 internal::ImageRep* rep = GetRepresentation(Image::kImageRepGdk); | 273 internal::ImageRep* rep = GetRepresentation(Image::kImageRepGdk); |
245 return rep->AsImageRepGdk()->pixbuf(); | 274 return rep->AsImageRepGdk()->pixbuf(); |
246 } | 275 } |
| 276 |
| 277 CairoCachedSurface* const Image::ToCairo() const { |
| 278 internal::ImageRep* rep = GetRepresentation(Image::kImageRepCairoCache); |
| 279 return rep->AsImageRepCairo()->surface(); |
| 280 } |
247 #endif | 281 #endif |
248 | 282 |
249 #if defined(OS_MACOSX) | 283 #if defined(OS_MACOSX) |
250 NSImage* Image::ToNSImage() const { | 284 NSImage* Image::ToNSImage() const { |
251 internal::ImageRep* rep = GetRepresentation(Image::kImageRepCocoa); | 285 internal::ImageRep* rep = GetRepresentation(Image::kImageRepCocoa); |
252 return rep->AsImageRepCocoa()->image(); | 286 return rep->AsImageRepCocoa()->image(); |
253 } | 287 } |
254 #endif | 288 #endif |
255 | 289 |
256 const SkBitmap* Image::CopySkBitmap() const { | 290 const SkBitmap* Image::CopySkBitmap() const { |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
330 | 364 |
331 // Handle native-to-Skia conversion. | 365 // Handle native-to-Skia conversion. |
332 if (rep_type == Image::kImageRepSkia) { | 366 if (rep_type == Image::kImageRepSkia) { |
333 internal::ImageRepSkia* rep = NULL; | 367 internal::ImageRepSkia* rep = NULL; |
334 #if defined(TOOLKIT_USES_GTK) | 368 #if defined(TOOLKIT_USES_GTK) |
335 if (storage_->default_representation_type() == Image::kImageRepGdk) { | 369 if (storage_->default_representation_type() == Image::kImageRepGdk) { |
336 internal::ImageRepGdk* pixbuf_rep = default_rep->AsImageRepGdk(); | 370 internal::ImageRepGdk* pixbuf_rep = default_rep->AsImageRepGdk(); |
337 rep = new internal::ImageRepSkia( | 371 rep = new internal::ImageRepSkia( |
338 internal::GdkPixbufToSkBitmap(pixbuf_rep->pixbuf())); | 372 internal::GdkPixbufToSkBitmap(pixbuf_rep->pixbuf())); |
339 } | 373 } |
| 374 // We don't do conversions from CairoCachedSurfaces to Skia because the |
| 375 // data lives on the display server and we'll always have a GdkPixbuf if we |
| 376 // have a CairoCachedSurface. |
340 #elif defined(OS_MACOSX) | 377 #elif defined(OS_MACOSX) |
341 if (storage_->default_representation_type() == Image::kImageRepCocoa) { | 378 if (storage_->default_representation_type() == Image::kImageRepCocoa) { |
342 internal::ImageRepCocoa* nsimage_rep = default_rep->AsImageRepCocoa(); | 379 internal::ImageRepCocoa* nsimage_rep = default_rep->AsImageRepCocoa(); |
343 std::vector<const SkBitmap*> bitmaps; | 380 std::vector<const SkBitmap*> bitmaps; |
344 CHECK(internal::NSImageToSkBitmaps(nsimage_rep->image(), &bitmaps)); | 381 CHECK(internal::NSImageToSkBitmaps(nsimage_rep->image(), &bitmaps)); |
345 rep = new internal::ImageRepSkia(bitmaps); | 382 rep = new internal::ImageRepSkia(bitmaps); |
346 } | 383 } |
347 #endif | 384 #endif |
348 CHECK(rep); | 385 CHECK(rep); |
349 AddRepresentation(rep); | 386 AddRepresentation(rep); |
350 return rep; | 387 return rep; |
351 } | 388 } |
| 389 #if defined(TOOLKIT_USES_GTK) |
| 390 else if (rep_type == Image::kImageRepCairoCache) { |
| 391 // Handle any-to-Cairo conversion. This may recursively create an |
| 392 // intermediate pixbuf before we send the data to the display server. |
| 393 internal::ImageRep* rep = GetRepresentation(Image::kImageRepGdk); |
| 394 internal::ImageRepCairoCached* native_rep = |
| 395 new internal::ImageRepCairoCached(rep->AsImageRepGdk()->pixbuf()); |
| 396 |
| 397 CHECK(native_rep); |
| 398 AddRepresentation(native_rep); |
| 399 return native_rep; |
| 400 } |
| 401 #endif |
352 | 402 |
353 // Handle Skia-to-native conversions. | 403 // Handle Skia-to-native conversions. |
354 if (default_rep->type() == Image::kImageRepSkia) { | 404 if (default_rep->type() == Image::kImageRepSkia) { |
355 internal::ImageRepSkia* skia_rep = default_rep->AsImageRepSkia(); | 405 internal::ImageRepSkia* skia_rep = default_rep->AsImageRepSkia(); |
356 internal::ImageRep* native_rep = NULL; | 406 internal::ImageRep* native_rep = NULL; |
357 #if defined(USE_AURA) | 407 #if defined(USE_AURA) |
358 static_cast<void>(skia_rep); | 408 static_cast<void>(skia_rep); |
359 NOTIMPLEMENTED(); | 409 NOTIMPLEMENTED(); |
360 #elif defined(TOOLKIT_USES_GTK) | 410 #elif defined(TOOLKIT_USES_GTK) |
361 if (rep_type == Image::kImageRepGdk) { | 411 if (rep_type == Image::kImageRepGdk) { |
(...skipping 24 matching lines...) Expand all Loading... |
386 return GetRepresentation(Image::kImageRepSkia)->AsImageRepSkia()-> | 436 return GetRepresentation(Image::kImageRepSkia)->AsImageRepSkia()-> |
387 bitmaps().size(); | 437 bitmaps().size(); |
388 } | 438 } |
389 | 439 |
390 const SkBitmap* Image::GetSkBitmapAtIndex(size_t index) const { | 440 const SkBitmap* Image::GetSkBitmapAtIndex(size_t index) const { |
391 return GetRepresentation(Image::kImageRepSkia)->AsImageRepSkia()-> | 441 return GetRepresentation(Image::kImageRepSkia)->AsImageRepSkia()-> |
392 bitmaps()[index]; | 442 bitmaps()[index]; |
393 } | 443 } |
394 | 444 |
395 } // namespace gfx | 445 } // namespace gfx |
OLD | NEW |