diff options
author | erg@chromium.org <erg@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-12-02 20:54:19 +0000 |
---|---|---|
committer | erg@chromium.org <erg@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-12-02 20:54:19 +0000 |
commit | 61e3009b9f5b3134fa3697befba4bc69c6be1e35 (patch) | |
tree | 81e35d5ca6efe019c1c888b7d7edbcdd0cf4fc7d /ui/gfx/image/cairo_cached_surface.cc | |
parent | 716b7785e16a2049525e406b17d75776cc1911ee (diff) | |
download | chromium_src-61e3009b9f5b3134fa3697befba4bc69c6be1e35.zip chromium_src-61e3009b9f5b3134fa3697befba4bc69c6be1e35.tar.gz chromium_src-61e3009b9f5b3134fa3697befba4bc69c6be1e35.tar.bz2 |
GTK: Move CairoCachedSurface from being owned by GtkThemeService to gfx::Image.
CairoCachedSurfaces are representations of a GdkPixbuf that live on the display
server instead of in process. GtkThemeService currently returns a
CairoCachedSurface for a (IDR#, Display). Instead, make gfx::Image keep
CairoCachedSurfaces as a representation and make CairoCachedSurface work on any
display, keeping the mapping to display server resources as an implementation
detail.
This has the benefit of:
- You don't need to go through GtkThemeService to access all resources,
simplifying the code in some places and allowing sharing of static images
across profiles.
- This will let us (in the future) remove several image conversions in the GTK
port.
This is part 1. Part 2 will remove the custom CairoCachedSurface calls from
GtkThemeService.
BUG=106060
TEST=none
R=rsesek@chromium.org
TBR=ben@chromium.org
Review URL: http://codereview.chromium.org/8769017
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@112778 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'ui/gfx/image/cairo_cached_surface.cc')
-rw-r--r-- | ui/gfx/image/cairo_cached_surface.cc | 109 |
1 files changed, 109 insertions, 0 deletions
diff --git a/ui/gfx/image/cairo_cached_surface.cc b/ui/gfx/image/cairo_cached_surface.cc new file mode 100644 index 0000000..2de4a32 --- /dev/null +++ b/ui/gfx/image/cairo_cached_surface.cc @@ -0,0 +1,109 @@ +// Copyright (c) 2011 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. + +#include "ui/gfx/image/cairo_cached_surface.h" + +#include <gtk/gtk.h> + +#include "base/basictypes.h" +#include "base/logging.h" + +namespace gfx { + +CairoCachedSurface::CairoCachedSurface() : pixbuf_(NULL) { +} + +CairoCachedSurface::~CairoCachedSurface() { + Reset(); +} + +void CairoCachedSurface::Reset() { + for (SurfaceVector::iterator it = surface_map_.begin(); + it != surface_map_.end(); ++it) { + cairo_surface_destroy(it->second); + } + surface_map_.clear(); + + if (pixbuf_) { + g_object_unref(pixbuf_); + pixbuf_ = NULL; + } +} + +int CairoCachedSurface::Width() const { + return pixbuf_ ? gdk_pixbuf_get_width(pixbuf_) : -1; +} + +int CairoCachedSurface::Height() const { + return pixbuf_ ? gdk_pixbuf_get_height(pixbuf_) : -1; +} + +void CairoCachedSurface::UsePixbuf(GdkPixbuf* pixbuf) { + if (pixbuf) + g_object_ref(pixbuf); + + Reset(); + + pixbuf_ = pixbuf; +} + +void CairoCachedSurface::SetSource(cairo_t* cr, GtkWidget* widget, + int x, int y) const { + SetSource(cr, gtk_widget_get_display(widget), x, y); +} + +void CairoCachedSurface::SetSource(cairo_t* cr, GdkDisplay* display, + int x, int y) const { + DCHECK(pixbuf_); + DCHECK(cr); + DCHECK(display); + + cairo_surface_t* surface = GetSurfaceFor(cr, display); + cairo_set_source_surface(cr, surface, x, y); +} + +void CairoCachedSurface::MaskSource(cairo_t* cr, GtkWidget* widget, + int x, int y) const { + MaskSource(cr, gtk_widget_get_display(widget), x, y); +} + +void CairoCachedSurface::MaskSource(cairo_t* cr, GdkDisplay* display, + int x, int y) const { + DCHECK(pixbuf_); + DCHECK(cr); + DCHECK(display); + + cairo_surface_t* surface = GetSurfaceFor(cr, display); + cairo_mask_surface(cr, surface, x, y); +} + +cairo_surface_t* CairoCachedSurface::GetSurfaceFor(cairo_t* cr, + GdkDisplay* display) const { + for (SurfaceVector::const_iterator it = surface_map_.begin(); + it != surface_map_.end(); ++it) { + if (display == it->first) { + return it->second; + } + } + + // First time here since last UsePixbuf call. Generate the surface. + cairo_surface_t* target = cairo_get_target(cr); + cairo_surface_t* out = cairo_surface_create_similar( + target, + CAIRO_CONTENT_COLOR_ALPHA, + gdk_pixbuf_get_width(pixbuf_), + gdk_pixbuf_get_height(pixbuf_)); + + DCHECK(out); + + cairo_t* copy_cr = cairo_create(out); + gdk_cairo_set_source_pixbuf(copy_cr, pixbuf_, 0, 0); + cairo_paint(copy_cr); + cairo_destroy(copy_cr); + + surface_map_.push_back(std::make_pair(display, out)); + return out; +} + +} // namespace gfx |