summaryrefslogtreecommitdiffstats
path: root/chrome/browser/gtk/cairo_cached_surface.h
blob: 0a2c256ce23c27dd75a08eed7b6bce9a636671b5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// Copyright (c) 2009 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_BROWSER_GTK_CAIRO_CACHED_SURFACE_H_
#define CHROME_BROWSER_GTK_CAIRO_CACHED_SURFACE_H_

typedef struct _GdkPixbuf GdkPixbuf;
typedef struct _cairo cairo_t;
typedef struct _cairo_surface cairo_surface_t;

// A helper class that takes a GdkPixbuf* and renders it to the screen. Unlike
// gdk_cairo_set_source_pixbuf(), CairoCachedSurface assumes that the pixbuf is
// immutable after UsePixbuf() is called and can be sent to the X server
// once. From then on, that cached version is used so we don't upload the same
// image each and every time we expose.
//
// Most cached surfaces are owned by the GtkThemeProvider, which associates
// them with a certain XDisplay. Some users of surfaces (CustomDrawButtonBase,
// for example) own their surfaces instead since they interact with the
// ResourceBundle instead of the GtkThemeProvider.
class CairoCachedSurface {
 public:
  CairoCachedSurface();
  ~CairoCachedSurface();

  // Whether this CairoCachedSurface owns a GdkPixbuf.
  bool valid() const {
    return pixbuf_;
  }

  // The dimensions of the underlying pixbuf/surface. (or -1 if invalid.)
  int Width() const;
  int Height() const;

  // Sets the pixbuf that we pass to cairo. Calling UsePixbuf() only derefs the
  // current pixbuf and surface (if they exist). Actually transfering data to
  // the X server occurs at SetSource() time. Calling UsePixbuf() should only
  // be done once as it clears cached data from the X server.
  void UsePixbuf(GdkPixbuf* pixbuf);

  // Sets our pixbuf as the active surface starting at (x, y), uploading it in
  // case we don't have an X backed surface cached.
  void SetSource(cairo_t* cr, int x, int y);

  // Raw access to the pixbuf. May be NULL. Used for a few gdk operations
  // regarding window shaping.
  GdkPixbuf* pixbuf() { return pixbuf_; }

 private:
  // The source pixbuf.
  GdkPixbuf* pixbuf_;

  // Our cached surface. This should be a xlib surface so the data lives on the
  // server instead of on the client.
  cairo_surface_t* surface_;
};

#endif  // CHROME_BROWSER_GTK_CAIRO_CACHED_SURFACE_H_