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 3 // found in the LICENSE file. 4 5 #ifndef UI_GFX_IMAGE_CAIRO_CACHED_SURFACE_H_ 6 #define UI_GFX_IMAGE_CAIRO_CACHED_SURFACE_H_ 7 8 #include <vector> 9 10 #include "ui/gfx/gfx_export.h" 11 12 typedef struct _GdkDisplay GdkDisplay; 13 typedef struct _GdkPixbuf GdkPixbuf; 14 typedef struct _GtkWidget GtkWidget; 15 typedef struct _cairo cairo_t; 16 typedef struct _cairo_surface cairo_surface_t; 17 18 namespace gfx { 19 20 // A helper class that takes a GdkPixbuf* and renders it to the screen. Unlike 21 // gdk_cairo_set_source_pixbuf(), CairoCachedSurface assumes that the pixbuf is 22 // immutable after UsePixbuf() is called and can be sent to the display server 23 // once. From then on, that cached version is used so we don't upload the same 24 // image each and every time we expose. 25 // 26 // Most cached surfaces are owned by the GtkThemeService, which associates 27 // them with a certain XDisplay. Some users of surfaces (CustomDrawButtonBase, 28 // for example) own their surfaces instead since they interact with the 29 // ResourceBundle instead of the GtkThemeService. 30 class GFX_EXPORT CairoCachedSurface { 31 public: 32 CairoCachedSurface(); 33 ~CairoCachedSurface(); 34 35 // Whether this CairoCachedSurface owns a GdkPixbuf. 36 bool valid() const { 37 return pixbuf_; 38 } 39 40 // Delete all our data. 41 void Reset(); 42 43 // The dimensions of the underlying pixbuf/surface. (or -1 if invalid.) 44 int Width() const; 45 int Height() const; 46 47 // Sets the pixbuf that we pass to cairo. Calling UsePixbuf() only derefs the 48 // current pixbuf and surface (if they exist). Actually transfering data to 49 // the X server occurs at SetSource() time. Calling UsePixbuf() should only 50 // be done once as it clears cached data from the X server. 51 void UsePixbuf(GdkPixbuf* pixbuf); 52 53 // Sets our pixbuf as the active surface starting at (x, y), uploading it in 54 // case we don't have an X backed surface cached. 55 void SetSource(cairo_t* cr, GtkWidget* widget, int x, int y) const; 56 void SetSource(cairo_t* cr, GdkDisplay* display, int x, int y) const; 57 58 // Performs a mask operation, using this surface as the alpha channel. 59 void MaskSource(cairo_t* cr, GtkWidget* widget, int x, int y) const; 60 void MaskSource(cairo_t* cr, GdkDisplay* display, int x, int y) const; 61 62 // Raw access to the pixbuf. May be NULL. Used for a few gdk operations 63 // regarding window shaping. 64 GdkPixbuf* pixbuf() { return pixbuf_; } 65 66 private: 67 typedef std::vector<std::pair<GdkDisplay*, cairo_surface_t*> > SurfaceVector; 68 69 // Returns a surface . Caches results so only one copy of the image data 70 // lives on the display server. 71 cairo_surface_t* GetSurfaceFor(cairo_t* cr, GdkDisplay* display) const; 72 73 // The source pixbuf. 74 GdkPixbuf* pixbuf_; 75 76 // Our list of cached surfaces. 99% of the time, this will only contain a 77 // single entry. At most two. We need to get this right for multiple displays 78 // to work correct, since each GdkDisplay is a different display server. 79 mutable SurfaceVector surface_map_; 80 }; 81 82 } // namespace gfx 83 84 #endif // UI_GFX_IMAGE_CAIRO_CACHED_SURFACE_H_ 85