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 "chrome/browser/ui/gtk/cairo_cached_surface.h" | 5 #include "chrome/browser/ui/gtk/cairo_cached_surface.h" |
6 | 6 |
7 #include <gtk/gtk.h> | 7 #include <gtk/gtk.h> |
8 | 8 |
9 #include "base/basictypes.h" | 9 #include "base/basictypes.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
11 | 11 |
12 CairoCachedSurface::CairoCachedSurface() : pixbuf_(NULL), surface_(NULL) { | 12 CairoCachedSurface::CairoCachedSurface() : pixbuf_(NULL), surface_(NULL) { |
13 } | 13 } |
14 | 14 |
15 CairoCachedSurface::~CairoCachedSurface() { | 15 CairoCachedSurface::~CairoCachedSurface() { |
16 if (surface_) | 16 Reset(); |
| 17 } |
| 18 |
| 19 void CairoCachedSurface::Reset() { |
| 20 if (surface_) { |
17 cairo_surface_destroy(surface_); | 21 cairo_surface_destroy(surface_); |
| 22 surface_ = NULL; |
| 23 } |
18 | 24 |
19 if (pixbuf_) | 25 if (pixbuf_) { |
20 g_object_unref(pixbuf_); | 26 g_object_unref(pixbuf_); |
| 27 pixbuf_ = NULL; |
| 28 } |
21 } | 29 } |
22 | 30 |
23 int CairoCachedSurface::Width() const { | 31 int CairoCachedSurface::Width() const { |
24 return pixbuf_ ? gdk_pixbuf_get_width(pixbuf_) : -1; | 32 return pixbuf_ ? gdk_pixbuf_get_width(pixbuf_) : -1; |
25 } | 33 } |
26 | 34 |
27 int CairoCachedSurface::Height() const { | 35 int CairoCachedSurface::Height() const { |
28 return pixbuf_ ? gdk_pixbuf_get_height(pixbuf_) : -1; | 36 return pixbuf_ ? gdk_pixbuf_get_height(pixbuf_) : -1; |
29 } | 37 } |
30 | 38 |
31 void CairoCachedSurface::UsePixbuf(GdkPixbuf* pixbuf) { | 39 void CairoCachedSurface::UsePixbuf(GdkPixbuf* pixbuf) { |
32 if (surface_) { | 40 if (surface_) { |
33 cairo_surface_destroy(surface_); | 41 cairo_surface_destroy(surface_); |
34 surface_ = NULL; | 42 surface_ = NULL; |
35 } | 43 } |
36 | 44 |
37 if (pixbuf) | 45 if (pixbuf) |
38 g_object_ref(pixbuf); | 46 g_object_ref(pixbuf); |
39 | 47 |
40 if (pixbuf_) | 48 if (pixbuf_) |
41 g_object_unref(pixbuf_); | 49 g_object_unref(pixbuf_); |
42 | 50 |
43 pixbuf_ = pixbuf; | 51 pixbuf_ = pixbuf; |
44 } | 52 } |
45 | 53 |
46 void CairoCachedSurface::SetSource(cairo_t* cr, int x, int y) { | 54 void CairoCachedSurface::SetSource(cairo_t* cr, int x, int y) { |
47 DCHECK(pixbuf_); | 55 DCHECK(pixbuf_); |
48 DCHECK(cr); | 56 DCHECK(cr); |
49 | 57 |
| 58 EnsureSurfaceValid(cr); |
| 59 cairo_set_source_surface(cr, surface_, x, y); |
| 60 } |
| 61 |
| 62 void CairoCachedSurface::MaskSource(cairo_t* cr, int x, int y) { |
| 63 DCHECK(pixbuf_); |
| 64 DCHECK(cr); |
| 65 |
| 66 EnsureSurfaceValid(cr); |
| 67 cairo_mask_surface(cr, surface_, x, y); |
| 68 } |
| 69 |
| 70 void CairoCachedSurface::EnsureSurfaceValid(cairo_t* cr) { |
50 if (!surface_) { | 71 if (!surface_) { |
51 // First time here since last UsePixbuf call. Generate the surface. | 72 // First time here since last UsePixbuf call. Generate the surface. |
52 cairo_surface_t* target = cairo_get_target(cr); | 73 cairo_surface_t* target = cairo_get_target(cr); |
53 surface_ = cairo_surface_create_similar( | 74 surface_ = cairo_surface_create_similar( |
54 target, | 75 target, |
55 CAIRO_CONTENT_COLOR_ALPHA, | 76 CAIRO_CONTENT_COLOR_ALPHA, |
56 gdk_pixbuf_get_width(pixbuf_), | 77 gdk_pixbuf_get_width(pixbuf_), |
57 gdk_pixbuf_get_height(pixbuf_)); | 78 gdk_pixbuf_get_height(pixbuf_)); |
58 | 79 |
59 DCHECK(surface_); | 80 DCHECK(surface_); |
60 #if !defined(NDEBUG) | 81 #if !defined(NDEBUG) |
61 int surface_type = cairo_surface_get_type(surface_); | 82 int surface_type = cairo_surface_get_type(surface_); |
62 DCHECK(surface_type == CAIRO_SURFACE_TYPE_XLIB || | 83 DCHECK(surface_type == CAIRO_SURFACE_TYPE_XLIB || |
63 surface_type == CAIRO_SURFACE_TYPE_XCB || | 84 surface_type == CAIRO_SURFACE_TYPE_XCB || |
64 surface_type == CAIRO_SURFACE_TYPE_IMAGE); | 85 surface_type == CAIRO_SURFACE_TYPE_IMAGE); |
65 #endif | 86 #endif |
66 | 87 |
67 cairo_t* copy_cr = cairo_create(surface_); | 88 cairo_t* copy_cr = cairo_create(surface_); |
68 gdk_cairo_set_source_pixbuf(copy_cr, pixbuf_, 0, 0); | 89 gdk_cairo_set_source_pixbuf(copy_cr, pixbuf_, 0, 0); |
69 cairo_paint(copy_cr); | 90 cairo_paint(copy_cr); |
70 cairo_destroy(copy_cr); | 91 cairo_destroy(copy_cr); |
71 } | 92 } |
72 | |
73 cairo_set_source_surface(cr, surface_, x, y); | |
74 } | 93 } |
OLD | NEW |