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 12CairoCachedSurface::CairoCachedSurface() : pixbuf_(NULL), surface_(NULL) { 13} 14 15CairoCachedSurface::~CairoCachedSurface() { 16 if (surface_) 17 cairo_surface_destroy(surface_); 18 19 if (pixbuf_) 20 g_object_unref(pixbuf_); 21} 22 23int CairoCachedSurface::Width() const { 24 return pixbuf_ ? gdk_pixbuf_get_width(pixbuf_) : -1; 25} 26 27int CairoCachedSurface::Height() const { 28 return pixbuf_ ? gdk_pixbuf_get_height(pixbuf_) : -1; 29} 30 31void CairoCachedSurface::UsePixbuf(GdkPixbuf* pixbuf) { 32 if (surface_) { 33 cairo_surface_destroy(surface_); 34 surface_ = NULL; 35 } 36 37 if (pixbuf) 38 g_object_ref(pixbuf); 39 40 if (pixbuf_) 41 g_object_unref(pixbuf_); 42 43 pixbuf_ = pixbuf; 44} 45 46void CairoCachedSurface::SetSource(cairo_t* cr, int x, int y) { 47 DCHECK(pixbuf_); 48 DCHECK(cr); 49 50 if (!surface_) { 51 // First time here since last UsePixbuf call. Generate the surface. 52 cairo_surface_t* target = cairo_get_target(cr); 53 surface_ = cairo_surface_create_similar( 54 target, 55 CAIRO_CONTENT_COLOR_ALPHA, 56 gdk_pixbuf_get_width(pixbuf_), 57 gdk_pixbuf_get_height(pixbuf_)); 58 59 DCHECK(surface_); 60#if !defined(NDEBUG) 61 int surface_type = cairo_surface_get_type(surface_); 62 DCHECK(surface_type == CAIRO_SURFACE_TYPE_XLIB || 63 surface_type == CAIRO_SURFACE_TYPE_XCB || 64 surface_type == CAIRO_SURFACE_TYPE_IMAGE); 65#endif 66 67 cairo_t* copy_cr = cairo_create(surface_); 68 gdk_cairo_set_source_pixbuf(copy_cr, pixbuf_, 0, 0); 69 cairo_paint(copy_cr); 70 cairo_destroy(copy_cr); 71 } 72 73 cairo_set_source_surface(cr, surface_, x, y); 74} 75