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