• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef CHROME_BROWSER_UI_GTK_CAIRO_CACHED_SURFACE_H_
6 #define CHROME_BROWSER_UI_GTK_CAIRO_CACHED_SURFACE_H_
7 #pragma once
8 
9 typedef struct _GdkPixbuf GdkPixbuf;
10 typedef struct _cairo cairo_t;
11 typedef struct _cairo_surface cairo_surface_t;
12 
13 // A helper class that takes a GdkPixbuf* and renders it to the screen. Unlike
14 // gdk_cairo_set_source_pixbuf(), CairoCachedSurface assumes that the pixbuf is
15 // immutable after UsePixbuf() is called and can be sent to the X server
16 // once. From then on, that cached version is used so we don't upload the same
17 // image each and every time we expose.
18 //
19 // Most cached surfaces are owned by the GtkThemeService, which associates
20 // them with a certain XDisplay. Some users of surfaces (CustomDrawButtonBase,
21 // for example) own their surfaces instead since they interact with the
22 // ResourceBundle instead of the GtkThemeService.
23 class CairoCachedSurface {
24  public:
25   CairoCachedSurface();
26   ~CairoCachedSurface();
27 
28   // Whether this CairoCachedSurface owns a GdkPixbuf.
valid()29   bool valid() const {
30     return pixbuf_;
31   }
32 
33   // The dimensions of the underlying pixbuf/surface. (or -1 if invalid.)
34   int Width() const;
35   int Height() const;
36 
37   // Sets the pixbuf that we pass to cairo. Calling UsePixbuf() only derefs the
38   // current pixbuf and surface (if they exist). Actually transfering data to
39   // the X server occurs at SetSource() time. Calling UsePixbuf() should only
40   // be done once as it clears cached data from the X server.
41   void UsePixbuf(GdkPixbuf* pixbuf);
42 
43   // Sets our pixbuf as the active surface starting at (x, y), uploading it in
44   // case we don't have an X backed surface cached.
45   void SetSource(cairo_t* cr, int x, int y);
46 
47   // Raw access to the pixbuf. May be NULL. Used for a few gdk operations
48   // regarding window shaping.
pixbuf()49   GdkPixbuf* pixbuf() { return pixbuf_; }
50 
51  private:
52   // The source pixbuf.
53   GdkPixbuf* pixbuf_;
54 
55   // Our cached surface. This should be a xlib surface so the data lives on the
56   // server instead of on the client.
57   cairo_surface_t* surface_;
58 };
59 
60 #endif  // CHROME_BROWSER_UI_GTK_CAIRO_CACHED_SURFACE_H_
61