From 4f4bf0f30416855af06e72eeb2484be01e97927c Mon Sep 17 00:00:00 2001
From: Benjamin Otte <otte@gnome.org>
Date: Tue, 13 Oct 2009 22:16:10 +0200
Subject: [PATCH 05/12] [cairo] Allow any cairo surface type in the constructor

Due to the limited API for querying size, a workaround using clip
extents is needed (see inline comments). This allows creating Images
from native surfaces.
---
 WebCore/platform/graphics/cairo/ImageCairo.cpp |   14 ++++++++++----
 1 files changed, 10 insertions(+), 4 deletions(-)

diff --git a/WebCore/platform/graphics/cairo/ImageCairo.cpp b/WebCore/platform/graphics/cairo/ImageCairo.cpp
index bfc75df..0ade6d7 100644
--- a/WebCore/platform/graphics/cairo/ImageCairo.cpp
+++ b/WebCore/platform/graphics/cairo/ImageCairo.cpp
@@ -75,10 +75,16 @@ BitmapImage::BitmapImage(cairo_surface_t* surface, ImageObserver* observer)
 {
     initPlatformData();
 
-    // TODO: check to be sure this is an image surface
-
-    int width = cairo_image_surface_get_width(surface);
-    int height = cairo_image_surface_get_height(surface);
+    // FIXME: Make cairo implement surface extents API, so this trick isn't necessary.
+    // What we do here is looking at the clip extents without any clip set, this is
+    // defined as the extents of the surface.
+    // We don't ignore x1 and y1, because device offsets might be set on the surface.
+    double x1, y1, x2, y2;
+    cairo_t *cr = cairo_create(surface);
+    cairo_clip_extents(cr, &x1, &y1, &x2, &y2);
+    cairo_destroy(cr);
+    int width = ceil(x2) - floor(x1);
+    int height = ceil(y2) - floor(y1);
     m_decodedSize = width * height * 4;
     m_size = IntSize(width, height);
 
-- 
1.6.3.3

