[cairo-commit] src/cairo-directfb-surface.c

Michael Emmel memmel at kemper.freedesktop.org
Mon Jun 26 22:27:42 PDT 2006


 src/cairo-directfb-surface.c | 2219 +++++++++++++++++++++++++++++--------------
 1 files changed, 1545 insertions(+), 674 deletions(-)

New commits:
diff-tree 5d60ceb9b85a6b39865bc760ef7467c10f2ec7b6 (from 289e8b8af30ae3f00e7649109a933a673f346b37)
Author: Michael Emmel <memmel at debian.localdomain>
Date:   Mon Jun 26 22:27:14 2006 -0700

    Added major updates fixes and enhancements by
    Claudio Ciccani klan at directfb.org
    Almost a full rewrite

diff --git a/src/cairo-directfb-surface.c b/src/cairo-directfb-surface.c
index 5ff117b..cb9a667 100644
--- a/src/cairo-directfb-surface.c
+++ b/src/cairo-directfb-surface.c
@@ -31,767 +31,1638 @@
  * California.
  *
  * Contributor(s):
- *	Michael Emmel <mike.emmel at gmail.com>
+ *    Michael Emmel <mike.emmel at gmail.com>
+ *    Claudio Ciccani <klan at users.sf.net>
  */
+
 #include <stdio.h>
+#include <stdlib.h>
+#include <math.h>
+
 #include <directfb.h>
+
+#include <direct/types.h>
+#include <direct/debug.h>
+#include <direct/memcpy.h>
+#include <direct/util.h>
+
 #include "cairo-directfb.h"
 #include "cairoint.h"
 
-#define DFB_UNSUPPORTED -1
-#define DFB_SUPPORTED 1
 
 /*
-Glyph support not working
-*/
-#define DFB_SHOW_GLYPHS 0
+ * Composite works fine.
+ */
+#define DFB_COMPOSITE 1
+
 /*
-Composite support not working
-*/
-#define DFB_COMPOSITE 0
+ * CompositeTrapezoids works (without antialiasing).
+ */
+#define DFB_COMPOSITE_TRAPEZOIDS 1
+
+/*
+ * ShowGlyphs works fine.
+ */
+#define DFB_SHOW_GLYPHS 1
+
+
+D_DEBUG_DOMAIN (Cairo_DirectFB, "Cairo/DirectFB", "Cairo DirectFB backend");
+
+/*****************************************************************************/
 
-#if DFB_SHOW_GLYPHS
-static cairo_int_status_t
-_cairo_directfb_surface_show_glyphs (cairo_scaled_font_t    *scaled_font,
-                 cairo_operator_t       operator,
-                 cairo_pattern_t        *pattern,
-                 void               *abstract_surface,
-                 int                    source_x,
-                 int                    source_y,
-                 int            dest_x,
-                 int            dest_y,
-                 unsigned int       width,
-                 unsigned int       height,
-                 const cairo_glyph_t    *glyphs,
-                 int                    num_glyphs);
-#endif
 
 typedef struct _cairo_directfb_surface {
-		cairo_surface_t   base;
-		cairo_format_t format;
-		IDirectFB *dfb;
-		IDirectFBSurface *main_surface;
-		IDirectFBSurface *buffer;
-		pixman_region16_t *clip;
-		cairo_surface_t *buffer_image;
-		void *buffer_data;
-		int width;
-		int height;
+    cairo_surface_t      base;
+        
+    cairo_format_t       format;
+        
+    IDirectFB           *dfb;
+    IDirectFBSurface    *surface;
+    IDirectFBSurface    *buffer;
+    
+    /* color buffer */
+    cairo_surface_t     *color;
+        
+    DFBRegion           *clips;
+    int                  n_clips;
+        
+    cairo_surface_t     *buffer_image;
+        
+    int                  width;
+    int                  height;
+    
+    bool                 local;
 } cairo_directfb_surface_t;
 
-static int
-_dfb_set_operator (cairo_operator_t operator,IDirectFBSurface *dest)
+
+typedef struct _cairo_directfb_font_cache {
+    IDirectFB           *dfb; 
+    IDirectFBSurface    *buffer;
+
+    int                  width;
+    int                  height;
+
+    /* coordinates within the surface 
+     * of the last loaded glyph */
+    int                  x;
+    int                  y;
+} cairo_directfb_font_cache_t;
+    
+
+static cairo_surface_backend_t cairo_directfb_surface_backend;
+
+/*****************************************************************************/
+
+
+#define RUN_CLIPPED( surface, clip, func ) {\
+    if ((surface)->clips) {\
+        int k;\
+        for (k = 0; k < (surface)->n_clips; k++) {\
+            if (clip) {\
+                DFBRegion  reg = (surface)->clips[k];\
+                DFBRegion *cli = (clip);\
+                if (reg.x2 < cli->x1 || reg.y2 < cli->y1 ||\
+                    reg.x1 > cli->x2 || reg.y1 > cli->y2)\
+                    continue;\
+                if (reg.x1 < cli->x1)\
+                    reg.x1 = cli->x1;\
+                if (reg.y1 < cli->y1)\
+                    reg.y1 = cli->y1;\
+                if (reg.x2 > cli->x2)\
+                    reg.x2 = cli->x2;\
+                if (reg.y2 > cli->y2)\
+                    reg.y2 = cli->y2;\
+                (surface)->buffer->SetClip ((surface)->buffer, &reg);\
+            }\
+            else {\
+                (surface)->buffer->SetClip ((surface)->buffer,\
+                                           &(surface)->clips[k]);\
+            }\
+            func;\
+        }\
+    }\
+    else {\
+        (surface)->buffer->SetClip ((surface)->buffer, clip);\
+        func;\
+    }\
+}
+
+#define TRANSFORM_POINT( m, x, y, ret_x, ret_y ) {\
+    double _x = (x);\
+    double _y = (y);\
+    (ret_x) = (_x * (m)->xx + _y * (m)->xy + (m)->x0);\
+    (ret_y) = (_x * (m)->yx + _y * (m)->yy + (m)->y0);\
+}
+
+/* XXX: A1 has a different bits ordering in cairo.
+ *      Probably we should drop it.
+ */
+
+static cairo_content_t 
+_directfb_format_to_content ( DFBSurfacePixelFormat format )
+{
+    switch (format) {
+        case DSPF_ARGB:
+            return CAIRO_CONTENT_COLOR_ALPHA;
+        case DSPF_A8: 
+            return CAIRO_CONTENT_ALPHA;
+        case DSPF_RGB32:
+        case DSPF_A1: 
+        default:
+            return CAIRO_CONTENT_COLOR;
+         break;
+    }
+}
+
+                                                           
+static inline DFBSurfacePixelFormat 
+cairo_to_directfb_format (cairo_format_t format)
 {
-	dest->SetDrawingFlags(dest,DSDRAW_BLEND);
-	dest->SetPorterDuff(dest,DSPD_NONE);
+    switch (format) {
+        case CAIRO_FORMAT_RGB24:
+            return DSPF_RGB32;
+        case CAIRO_FORMAT_ARGB32:
+            return DSPF_ARGB; 
+        case CAIRO_FORMAT_A8:
+            return DSPF_A8; 
+        case CAIRO_FORMAT_A1:
+            return DSPF_A1; 
+        default:
+            break;
+    }
+    
+    return -1;
+}
+
+static inline cairo_format_t
+directfb_to_cairo_format (DFBSurfacePixelFormat format)
+{
+    switch (format) {
+        case DSPF_RGB32:
+            return CAIRO_FORMAT_RGB24;
+        case DSPF_ARGB:
+            return CAIRO_FORMAT_ARGB32;
+        case DSPF_A8 :
+            return CAIRO_FORMAT_A8;
+        case DSPF_A1 :
+            return CAIRO_FORMAT_A1;
+        default:
+            break;
+    }
+    
+    return -1;
+}
+
+
+static cairo_status_t
+_directfb_get_operator (cairo_operator_t         operator,
+                        DFBSurfaceDrawingFlags  *ret_drawing,
+                        DFBSurfaceBlittingFlags *ret_blitting,
+                        DFBSurfaceBlendFunction *ret_srcblend,
+                        DFBSurfaceBlendFunction *ret_dstblend )
+{ 
+    DFBSurfaceDrawingFlags  drawing  = DSDRAW_BLEND;
+    DFBSurfaceBlittingFlags blitting = DSBLIT_BLEND_ALPHACHANNEL;
+    DFBSurfaceBlendFunction srcblend = DSBF_ONE;
+    DFBSurfaceBlendFunction dstblend = DSBF_ZERO;
+    
     switch (operator) {
-    case CAIRO_OPERATOR_CLEAR:
-	dest->SetPorterDuff(dest,DSPD_CLEAR);
-	break;
-    case CAIRO_OPERATOR_SOURCE:
-	dest->SetPorterDuff(dest,DSPD_SRC);
-	break;
-    case CAIRO_OPERATOR_OVER:
-	dest->SetPorterDuff(dest,DSPD_SRC_OVER);
-	break;
-    case CAIRO_OPERATOR_IN:
-	dest->SetPorterDuff(dest,DSPD_SRC_IN);
-	break;
-    case CAIRO_OPERATOR_OUT:
-	dest->SetPorterDuff(dest,DSPD_SRC_OUT);
-	break;
-    case CAIRO_OPERATOR_DEST_OVER:
-	dest->SetPorterDuff(dest,DSPD_DST_OVER);
-	break;
-    case CAIRO_OPERATOR_DEST_IN:
-	dest->SetPorterDuff(dest,DSPD_DST_IN);
-	break;
-    case CAIRO_OPERATOR_DEST_OUT:
-	dest->SetPorterDuff(dest,DSPD_DST_OUT);
-	break;
-
-	/*not sure about these yet */
-    case CAIRO_OPERATOR_ATOP:
-    	return DFB_UNSUPPORTED;
-	break;
-    case CAIRO_OPERATOR_DEST:
-    	return DFB_UNSUPPORTED;
-	break;
-    case CAIRO_OPERATOR_DEST_ATOP:
-    	return DFB_UNSUPPORTED;
-	break;
-    case CAIRO_OPERATOR_XOR:
-		dest->SetDrawingFlags(dest,DSDRAW_XOR);
-	break;
-    case CAIRO_OPERATOR_ADD:
-    	return DFB_UNSUPPORTED;
-	break;
-    case CAIRO_OPERATOR_SATURATE:
-    	return DFB_UNSUPPORTED;
-	break;
-    default:
-    	return DFB_UNSUPPORTED;
-    }
-    return DFB_SUPPORTED;
-}
-
-static inline int cairo_to_directfb_format(cairo_format_t format ) {
-		switch( format ) {
-				case CAIRO_FORMAT_RGB24:
-						return DSPF_RGB24;
-				case CAIRO_FORMAT_ARGB32:
-						return DSPF_ARGB;
-				case CAIRO_FORMAT_A8:
-						return DSPF_A8;
-				case CAIRO_FORMAT_A1:
-						return DSPF_A1;
-				default:
-				{
-						/*assert(0);*/
-						return DSPF_UNKNOWN;
-				}
-		}
-}
-
-static inline int directfb_to_cairo_format(DFBSurfacePixelFormat dfbformat ) {
-        switch( dfbformat ) {
-                case DSPF_RGB24 :
-                case DSPF_RGB32 :
-                        return CAIRO_FORMAT_RGB24;
-                        break;
-                case DSPF_AiRGB :
-                case DSPF_ARGB :
-                        return CAIRO_FORMAT_ARGB32;
-                        break;
-                case DSPF_A8 :
-                        return CAIRO_FORMAT_A8;
-                        break;
-                case DSPF_A1 :
-                        return CAIRO_FORMAT_A1;
-                        break;
-                case DSPF_UNKNOWN :
-                case DSPF_ARGB1555 :
-                case DSPF_RGB16 :
-                case DSPF_YUY2 :
-                case DSPF_RGB332 :
-                case DSPF_UYVY :
-                case DSPF_I420 :
-                case DSPF_ALUT44 :
-                case DSPF_NV12 :
-                case DSPF_NV16 :
-                default :
-                	return DFB_UNSUPPORTED;
-	}
-}
-
-static IDirectFBSurface *cairo_directfb_buffer_surface_create(IDirectFB *dfb,void *data,int pitch, int format,
-				int width, int height) {
-
-		DFBResult  ret;
-		IDirectFBSurface      *buffer;
-		DFBSurfaceDescription  dsc;
-
-		dsc.flags = DSDESC_WIDTH | DSDESC_HEIGHT | DSDESC_PIXELFORMAT;
-		dsc.caps = DSCAPS_NONE;
-		dsc.width       = width;
-		dsc.height      = height;
-		dsc.pixelformat = format;
-		if( data ) {
-				dsc.flags |= DSDESC_CAPS;
-				dsc.flags |= DSDESC_PREALLOCATED;
-				dsc.caps = DSCAPS_NONE;
-				dsc.preallocated[0].data = data;
-				dsc.preallocated[0].pitch = pitch;
-				dsc.preallocated[1].data = NULL;
-				dsc.preallocated[1].pitch = 0;
-		}
-
-		ret =dfb->CreateSurface (dfb, &dsc, &buffer);
-		if (ret) {
-				DirectFBError ("cairo_directfb_buffer_surface_create failed ", ret);
-				assert(0);
-				return NULL;
-		}
-#if 0
-		{
-				DFBSurfacePixelFormat dfbformat;
-				int nwidth=0;
-				int nheight=0;
-				int npitch;
-				void *ndata;
-				buffer->GetSize(buffer,&nwidth,&nheight);
-				buffer->GetPixelFormat(buffer,&dfbformat);
-				buffer->Lock(buffer,DSLF_READ,&ndata,&npitch);
-				buffer->Unlock(buffer);
-				assert( ( nwidth == width) && (nheight == height));
-		}
-#endif
-		return buffer;
+        case CAIRO_OPERATOR_CLEAR:
+            srcblend = DSBF_ZERO;
+            dstblend = DSBF_ZERO;
+            break;
+        case CAIRO_OPERATOR_SOURCE:
+            drawing  = DSDRAW_NOFX;
+            blitting = DSBLIT_NOFX;
+            break;
+        case CAIRO_OPERATOR_OVER:
+            srcblend = DSBF_ONE;
+            dstblend = DSBF_INVSRCALPHA;
+            break;
+        case CAIRO_OPERATOR_IN:
+            srcblend = DSBF_DESTALPHA;
+            dstblend = DSBF_ZERO;
+            break;
+        case CAIRO_OPERATOR_OUT:
+            srcblend = DSBF_INVDESTALPHA;
+            dstblend = DSBF_ZERO;
+            break;
+        case CAIRO_OPERATOR_ATOP:
+            srcblend = DSBF_DESTALPHA;
+            dstblend = DSBF_INVSRCALPHA;
+            break;
+        case CAIRO_OPERATOR_DEST:
+            srcblend = DSBF_ZERO;
+            dstblend = DSBF_ONE;
+            break;
+        case CAIRO_OPERATOR_DEST_OVER:
+            srcblend = DSBF_INVDESTALPHA;
+            dstblend = DSBF_ONE;
+            break;
+        case CAIRO_OPERATOR_DEST_IN:
+            srcblend = DSBF_ZERO;
+            dstblend = DSBF_SRCALPHA;
+            break;
+        case CAIRO_OPERATOR_DEST_OUT:
+            srcblend = DSBF_ZERO;
+            dstblend = DSBF_INVSRCALPHA;
+            break;
+        case CAIRO_OPERATOR_DEST_ATOP:
+            srcblend = DSBF_INVDESTALPHA;
+            dstblend = DSBF_SRCALPHA;
+            break;                        
+        case CAIRO_OPERATOR_XOR:
+            srcblend = DSBF_INVDESTALPHA;
+            dstblend = DSBF_INVSRCALPHA;
+            break;
+        case CAIRO_OPERATOR_ADD:
+            srcblend = DSBF_ONE;
+            dstblend = DSBF_ONE;
+            break;
+        case CAIRO_OPERATOR_SATURATE:
+            srcblend = DSBF_SRCALPHASAT;
+            dstblend = DSBF_ONE;
+            break;
+        default:
+            return CAIRO_INT_STATUS_UNSUPPORTED;
+    }
+    
+    if (ret_drawing)
+        *ret_drawing = drawing;
+    if (ret_blitting)
+        *ret_blitting = blitting;
+    if (ret_srcblend)
+        *ret_srcblend = srcblend;
+    if (ret_dstblend)
+        *ret_dstblend = dstblend;
+    
+    return CAIRO_STATUS_SUCCESS;
+}
+
+static IDirectFBSurface*
+_directfb_buffer_surface_create (IDirectFB             *dfb, 
+                                 DFBSurfacePixelFormat  format,
+                                 int                    width,
+                                 int                    height)
+{
+    IDirectFBSurface      *buffer;
+    DFBSurfaceDescription  dsc;
+    DFBResult              ret;
+    
+    dsc.flags       = DSDESC_WIDTH | DSDESC_HEIGHT | DSDESC_PIXELFORMAT;
+    dsc.width       = width;
+    dsc.height      = height;
+    dsc.pixelformat = format;
+
+    ret = dfb->CreateSurface (dfb, &dsc, &buffer);
+    if (ret) {
+        DirectFBError ("IDirectFB::CreateSurface()", ret);
+        return NULL;
+    }
+    
+    return buffer;
 }
 
 static cairo_status_t
-_cairo_directfb_surface_get_image (cairo_directfb_surface_t *surface,
-				   cairo_rectangle_int16_t  *interest,
-				   cairo_image_surface_t   **image_out,
-				   cairo_rectangle_int16_t  *rect_out,
-				   DFBSurfaceLockFlags       flags)
-{
-		int pitch;
-		void *data;
-
-		(void)interest;
-		if( surface->buffer->Lock(surface->buffer,flags,&data,&pitch) != DFB_OK )
-		return CAIRO_STATUS_NO_MEMORY;
-
-		/*lock the dest agianst other changes*/
-		if( surface->buffer_image ) {
-			if( surface->buffer_data == data ) {
-				cairo_surface_reference(surface->buffer_image);
-			}
-		}
-
-		if( surface->buffer_data != data ){
-			/* new off screen buffer */
-			int width;
-			int height;
-			DFBSurfacePixelFormat dfbformat;
-			/*surface moved free image if allocated */
-			if( surface->buffer_image ) {
-			cairo_surface_destroy (surface->buffer_image);
-			surface->buffer_image=NULL;
-			surface->buffer_data=NULL;
-			}
-			surface->buffer->Unlock(surface->buffer);
-
-			surface->main_surface->GetSize(surface->main_surface,&width,&height);
-			surface->main_surface->GetPixelFormat(surface->main_surface,&dfbformat);
-			surface->format = directfb_to_cairo_format(dfbformat);
-
-			if( surface->format == DFB_UNSUPPORTED ) {
-				surface->format = CAIRO_FORMAT_ARGB32;
-				surface->buffer = cairo_directfb_buffer_surface_create(surface->dfb,NULL,
-											0,DSPF_ARGB,width,height);
-				if( !surface->buffer )
-				return CAIRO_STATUS_NO_MEMORY;
-				/*Have to flip the main surface if its double buffered to blit the buffer*/
-				surface->main_surface->Flip(surface->main_surface,NULL,0);
-				surface->buffer->Blit(surface->buffer,surface->main_surface,NULL,0,0);
-			}else {
-				surface->buffer = surface->main_surface;
-			}
-
-			surface->width=width;
-			surface->height=height;
-
-			if( surface->buffer->Lock(surface->buffer,flags,&data,&pitch) != DFB_OK )
-			return CAIRO_STATUS_NO_MEMORY;
-
-			surface->buffer_data = data;
-			surface->buffer_image = cairo_image_surface_create_for_data (
-				(unsigned char *)data,surface->format,width, height, pitch);
-			if( surface->buffer_image == NULL ) {
-			surface->buffer->Release(surface->buffer);
-			surface->buffer = NULL;
-			surface->buffer_data = NULL;
-			return CAIRO_STATUS_NO_MEMORY;
-			}
-			#if 0
-			if( surface->clip)
-				_cairo_image_surface_set_clip_region (
-					(cairo_image_surface_t *)surface->buffer_image,surface->clip);
-			#endif
-		}
-
-		if (rect_out) {
-				rect_out->x = 0;
-				rect_out->y = 0;
-				rect_out->width = surface->width;
-				rect_out->height = surface->height;
-		}
-		if( image_out )
-		*image_out=(cairo_image_surface_t *)surface->buffer_image;
-		return CAIRO_STATUS_SUCCESS;
+_directfb_acquire_surface (cairo_directfb_surface_t *surface, 
+                           DFBSurfaceLockFlags       lock_flags)
+{   
+    if (!surface->local) {
+        int width, height;
+    
+        surface->surface->GetSize (surface->surface, &width, &height);   
+        
+        if (surface->width != width || surface->height != height) {
+            DFBSurfacePixelFormat format;
+        
+            if (surface->buffer_image) {
+                cairo_surface_destroy (surface->buffer_image);
+                surface->buffer_image = NULL;
+            }
+        
+            if (surface->buffer)
+                surface->buffer->Release (surface->buffer);
+        
+            surface->surface->GetPixelFormat (surface->surface, &format);
+            surface->format = directfb_to_cairo_format (format);
+        
+            if (surface->format == -1) {
+                D_DEBUG_AT (Cairo_DirectFB, "%s buffer for surface.\n",
+                            surface->buffer ? "Reallocating" : "Allocating");
+            
+                surface->buffer = _directfb_buffer_surface_create (surface->dfb,
+                                                        DSPF_ARGB, width, height);
+                if (!surface->buffer)
+                    return CAIRO_STATUS_NO_MEMORY;
+                
+                surface->buffer->Blit (surface->buffer, 
+                                       surface->surface, NULL, 0, 0);
+            
+                surface->format = CAIRO_FORMAT_ARGB32;
+            }
+            else {
+                surface->surface->AddRef (surface->surface);
+                surface->buffer = surface->surface;
+            }
+        
+            surface->width  = width;
+            surface->height = height;
+        }
+    }
+    
+    if (lock_flags) {
+        void      *data;
+        int        pitch;
+        DFBResult  ret;
+        
+        ret = surface->buffer->Lock (surface->buffer, 
+                                     lock_flags, &data, &pitch);
+        if (ret) {
+            DirectFBError ("IDirectFBSurface::Lock()", ret);
+            return CAIRO_STATUS_NO_MEMORY;
+        }
+            
+        if (!surface->buffer_image) {            
+            surface->buffer_image = cairo_image_surface_create_for_data (data,
+                                                    surface->format, surface->width,
+                                                    surface->height, pitch);
+            if (!surface->buffer_image) {
+                surface->buffer->Unlock (surface->buffer);
+                return CAIRO_STATUS_NO_MEMORY;
+            }
+        }
+        else {
+            cairo_surface_reference (surface->buffer_image);
+        }
+    }
+    
+    return CAIRO_STATUS_SUCCESS;
 }
 
+
+
 static cairo_surface_t *
-_cairo_directfb_surface_create_similar (void *abstract_src,
-				cairo_content_t content,
-				int        width,
-				int        height)
-{
-   	cairo_format_t format = _cairo_format_from_content (content);
-	int dfbformat;
-	switch( content ) {
-		case CAIRO_CONTENT_COLOR:
-			dfbformat=DSPF_ARGB;
-		break;
-		case CAIRO_CONTENT_ALPHA:
-			dfbformat=DSPF_A8;
-		break;
-		case CAIRO_CONTENT_COLOR_ALPHA:
-			dfbformat= DSPF_ARGB;
-		break;
-		default:
-		{
-			return cairo_image_surface_create (format, width, height);
-		}
-	}
-	cairo_directfb_surface_t *src = abstract_src;
-	IDirectFBSurface *buffer =cairo_directfb_buffer_surface_create(src->dfb,
-					NULL,0,dfbformat,width,height);
-	cairo_surface_t *sur =cairo_directfb_surface_create (src->dfb,buffer);
-	((cairo_directfb_surface_t *)sur)->format = format;
-	return sur;
+_cairo_directfb_surface_create_similar (void            *abstract_src,
+                                        cairo_content_t  content,
+                                        int              width,
+                                        int              height)
+{
+    cairo_directfb_surface_t *source  = abstract_src;
+    cairo_directfb_surface_t *surface;
+    cairo_format_t            format;
+    
+    D_DEBUG_AT (Cairo_DirectFB, 
+                "%s( src=%p, content=0x%x, width=%d, height=%d).\n",
+                __FUNCTION__, source, content, width, height);
+    
+    format = _cairo_format_from_content (content);             
+    
+    surface = calloc (1, sizeof(cairo_directfb_surface_t));
+    if (!surface) 
+        return NULL;
+   
+    surface->surface = _directfb_buffer_surface_create (source->dfb,
+                                              cairo_to_directfb_format (format),
+                                              MAX (width, 8), MAX (height, 8) );
+    if (!surface->surface) {
+        free (surface);
+        return NULL;
+    }
+    _cairo_surface_init (&surface->base, &cairo_directfb_surface_backend,content);
+                    
+        
+    source->dfb->AddRef (source->dfb);
+    surface->dfb = source->dfb;
+    
+    surface->surface->AddRef (surface->surface);
+    surface->buffer = surface->surface;
+    
+    surface->format = format;
+    surface->width  = width;
+    surface->height = height;
+    surface->local  = true;
+    
+    return &surface->base;
 }
 
 static cairo_status_t
-_cairo_directfb_surface_finish (void *data ) {
-		cairo_directfb_surface_t *surface=(cairo_directfb_surface_t *)data;
-		if( surface->buffer_image )
-		cairo_surface_destroy (surface->buffer_image);
-		if (surface->clip)
-	    pixman_region_destroy (surface->clip);
-
-		if( surface->main_surface != surface->buffer ) {
-				surface->main_surface->SetClip (surface->main_surface,NULL);
-				surface->main_surface->Blit(surface->main_surface,surface->buffer,NULL,0,0);
-				surface->buffer->Release (surface->buffer);
-				surface->buffer=NULL;
-		}
-#if 0 /* No don't do this */
-		surface->main_surface->Flip(surface->main_surface,NULL,0);
-#endif
-		surface->main_surface->Release (surface->main_surface);
-		surface->main_surface=NULL;
-		surface->dfb->Release(surface->dfb);
-		surface->dfb=NULL;
-		return CAIRO_STATUS_SUCCESS;
+_cairo_directfb_surface_finish (void *data)
+{
+    cairo_directfb_surface_t *surface = (cairo_directfb_surface_t *)data;
+    
+    D_DEBUG_AT (Cairo_DirectFB, 
+                "%s( surface=%p ).\n", __FUNCTION__, surface);
+        
+    if (surface->buffer_image) {
+        cairo_surface_destroy (surface->buffer_image);
+        surface->buffer_image = NULL;
+    }
+    
+    if (surface->clips) {
+        free (surface->clips);
+        surface->clips   = NULL;
+        surface->n_clips = 0;
+    }
+    
+    if (surface->color) {
+        cairo_surface_destroy (surface->color);
+        surface->color = NULL;
+    }
+
+    if (surface->buffer) {
+        surface->buffer->Release (surface->buffer);
+        surface->buffer = NULL;
+    }
+
+    if (surface->surface) {
+        surface->surface->Release (surface->surface);
+        surface->surface = NULL;
+    }
+    
+    if (surface->dfb) {
+        surface->dfb->Release (surface->dfb); 
+        surface->dfb     = NULL;
+    }
+        
+    return CAIRO_STATUS_SUCCESS;
 }
 
 static cairo_status_t
-_cairo_directfb_surface_acquire_source_image (void  *abstract_surface,
-				cairo_image_surface_t **image_out,
-				void                  **image_extra)
-{
-		cairo_directfb_surface_t *surface = abstract_surface;
-		*image_extra = surface;
-		return _cairo_directfb_surface_get_image (surface, NULL,image_out,NULL,DSLF_READ);
-}
-
-		static void
-_cairo_directfb_surface_release_source_image (void  *abstract_surface,
-				cairo_image_surface_t *image,
-				void                  *image_extra)
-{
-		cairo_directfb_surface_t *surface = abstract_surface;
-		surface->buffer->Unlock(surface->buffer);
-}
-
-		static cairo_status_t
-_cairo_directfb_surface_acquire_dest_image (void                    *abstract_surface,
-				            cairo_rectangle_int16_t *interest_rect,
-				            cairo_image_surface_t  **image_out,
-				            cairo_rectangle_int16_t *image_rect_out,
-				            void                   **image_extra)
-{
-		cairo_directfb_surface_t *surface = abstract_surface;
-		*image_extra = interest_rect;
-		return  _cairo_directfb_surface_get_image (surface, interest_rect,image_out,
-						image_rect_out,DSLF_READ|DSLF_WRITE);
+_cairo_directfb_surface_acquire_source_image (void                   *abstract_surface,
+                                              cairo_image_surface_t **image_out,
+                                              void                  **image_extra)
+{
+    cairo_directfb_surface_t *surface = abstract_surface;
+    cairo_status_t            ret;
+    
+    D_DEBUG_AT (Cairo_DirectFB, 
+                "%s( surface=%p ).\n", __FUNCTION__, surface);
+    
+    ret = _directfb_acquire_surface (surface, DSLF_READ);
+    if (ret)
+        return ret;
+    
+    if (image_out)
+        *image_out = (cairo_image_surface_t *)surface->buffer_image;
+    
+    if (image_extra)
+        *image_extra = surface;
+    
+    return CAIRO_STATUS_SUCCESS;
 }
 
 static void
-_cairo_directfb_surface_release_dest_image (void        *abstract_surface,
-				cairo_rectangle_int16_t *interest_rect,
-				cairo_image_surface_t   *image,
-				cairo_rectangle_int16_t *image_rect,
-				void                    *image_extra)
-{
-		cairo_directfb_surface_t *surface = abstract_surface;
-		IDirectFBSurface *buffer=surface->buffer;
-		buffer->Unlock(buffer);
+_cairo_directfb_surface_release_source_image (void                  *abstract_surface,
+                                              cairo_image_surface_t *image,
+                                              void                  *image_extra)
+{
+    cairo_directfb_surface_t *surface = abstract_surface;
+    
+    D_DEBUG_AT (Cairo_DirectFB, 
+                "%s( surface=%p ).\n", __FUNCTION__, surface);
+    
+    surface->buffer->Unlock (surface->buffer);
 }
 
 static cairo_status_t
-_cairo_directfb_surface_clone_similar (void	    *abstract_surface,
-				cairo_surface_t *src,
-				cairo_surface_t **clone_out)
-{
-		cairo_directfb_surface_t *surface = abstract_surface;
-		cairo_directfb_surface_t *clone;
-
-		if (src->backend == surface->base.backend) {
-				*clone_out = src;
-				cairo_surface_reference (src);
-				return CAIRO_STATUS_SUCCESS;
-		} else if (_cairo_surface_is_image (src)) {
-				cairo_image_surface_t *image_src = (cairo_image_surface_t *) src;
-				clone = (cairo_directfb_surface_t *)
-						_cairo_directfb_surface_create_similar (surface, image_src->format,
-										image_src->width,
-										image_src->height);
-				if (!clone)
-						return CAIRO_STATUS_NO_MEMORY;
-				IDirectFBSurface *tmpbuffer;
-				int format = cairo_to_directfb_format(image_src->format);
-				tmpbuffer =cairo_directfb_buffer_surface_create(surface->dfb,
-								(void *)image_src->data,image_src->stride,format,
-								image_src->width,image_src->height);
-
-				if (!tmpbuffer){
-						cairo_surface_destroy((cairo_surface_t *)surface);
-						return CAIRO_STATUS_NO_MEMORY;
-				}
-				clone->buffer->Blit(clone->buffer,tmpbuffer,NULL,0,0);
-				tmpbuffer->Release(tmpbuffer);
-				*clone_out = &clone->base;
-				return CAIRO_STATUS_SUCCESS;
-		}
-		return CAIRO_INT_STATUS_UNSUPPORTED;
+_cairo_directfb_surface_acquire_dest_image (void                   *abstract_surface,
+                                            cairo_rectangle_int16_t      *interest_rect,
+                                            cairo_image_surface_t **image_out,
+                                            cairo_rectangle_int16_t      *image_rect_out,
+                                            void                  **image_extra)
+{
+    cairo_directfb_surface_t *surface = abstract_surface;
+    cairo_status_t            ret;
+    
+    D_DEBUG_AT (Cairo_DirectFB, 
+                "%s( surface=%p ).\n", __FUNCTION__, surface);
+    
+    ret = _directfb_acquire_surface (surface, DSLF_READ | DSLF_WRITE);
+    if (ret)
+        return ret;
+        
+    if (image_out)
+        *image_out = (cairo_image_surface_t *)surface->buffer_image;
+        
+    if (image_rect_out) {
+        image_rect_out->x = 0;
+        image_rect_out->y = 0;
+        image_rect_out->width = surface->width;
+        image_rect_out->height = surface->height;
+    }
+        
+    if (image_extra)
+        *image_extra = interest_rect;
+    
+    return CAIRO_STATUS_SUCCESS;
+}
+
+static void
+_cairo_directfb_surface_release_dest_image (void                  *abstract_surface,
+                                            cairo_rectangle_int16_t     *interest_rect,
+                                            cairo_image_surface_t *image,
+                                            cairo_rectangle_int16_t     *image_rect,
+                                            void                  *image_extra)
+{
+    cairo_directfb_surface_t *surface = abstract_surface;
+    
+    D_DEBUG_AT (Cairo_DirectFB, 
+                "%s( surface=%p ).\n", __FUNCTION__, surface);
+    
+    surface->buffer->Unlock (surface->buffer);
+}
+
+static cairo_status_t
+_cairo_directfb_surface_clone_similar (void             *abstract_surface,
+                                       cairo_surface_t  *src,
+                                       cairo_surface_t **clone_out)
+{
+    cairo_directfb_surface_t *surface = abstract_surface;
+    cairo_directfb_surface_t *clone;
+    
+    D_DEBUG_AT (Cairo_DirectFB, 
+                "%s( surface=%p, src=%p ).\n", __FUNCTION__, surface, src);
+
+    if (src->backend == surface->base.backend) {
+        cairo_surface_reference (src);
+        *clone_out = src;
+         
+        return CAIRO_STATUS_SUCCESS;
+    }
+    else if (_cairo_surface_is_image (src)) {
+        cairo_image_surface_t *image_src = (cairo_image_surface_t *) src;
+        unsigned char         *dst, *src = image_src->data;
+        int                    pitch;
+        int                    i, j;
+        DFBResult              ret;
+        
+        clone = (cairo_directfb_surface_t *)
+                _cairo_directfb_surface_create_similar (surface, 
+                            _cairo_content_from_format (image_src->format),
+                            image_src->width, image_src->height);
+        if (!clone)
+            return CAIRO_STATUS_NO_MEMORY;
+            
+        ret = clone->buffer->Lock (clone->buffer, 
+                                   DSLF_WRITE, (void *)&dst, &pitch);
+        if (ret) {
+            DirectFBError ("IDirectFBSurface::Lock()", ret);
+            cairo_surface_destroy ((cairo_surface_t *)clone);
+            return CAIRO_STATUS_NO_MEMORY;
+        }
+        
+        if (image_src->format == CAIRO_FORMAT_A1) {
+            /* A1 -> A8 */
+            for (i = 0; i < image_src->height; i++) {
+                for (j = 0; j < image_src->width; j++)
+                    dst[j] = (src[j>>3] & (1 << (j&7))) ? 0xff : 0x00;
+                dst += pitch;
+                src += image_src->stride;
+            }
+        }
+        else {     
+            for (i = 0; i < image_src->height; i++) {
+                direct_memcpy( dst, src, image_src->stride );
+                dst += pitch;
+                src += image_src->stride;
+            }
+        }
+        
+        clone->buffer->Unlock (clone->buffer);
+        
+        *clone_out = &clone->base;
+                
+        return CAIRO_STATUS_SUCCESS;
+    }
+    
+    return CAIRO_INT_STATUS_UNSUPPORTED;
+}
+
+#if DFB_COMPOSITE || DFB_COMPOSITE_TRAPEZOIDS
+static cairo_int_status_t
+_directfb_prepare_composite (cairo_directfb_surface_t   *dst,
+                             cairo_pattern_t             *src_pattern,
+                             cairo_pattern_t             *mask_pattern,
+                             cairo_operator_t             op,
+                             int *src_x,             int *src_y,
+                             int *mask_x,            int *mask_y,
+                             unsigned int                 width,
+                             unsigned int                 height,                        
+                             cairo_directfb_surface_t   **ret_src,
+                             cairo_surface_attributes_t  *ret_src_attr)
+{
+    cairo_directfb_surface_t   *src;
+    cairo_surface_attributes_t	src_attr;
+    cairo_status_t              ret;
+    DFBSurfaceBlittingFlags     flags;
+    DFBSurfaceBlendFunction     sblend;
+    DFBSurfaceBlendFunction     dblend;
+    DFBColor                    color;
+    
+    if (_directfb_get_operator (op, NULL, &flags, &sblend, &dblend))
+        return CAIRO_INT_STATUS_UNSUPPORTED;
+     
+    if (mask_pattern) {
+        cairo_solid_pattern_t *pattern;
+        
+        if (mask_pattern->type != CAIRO_PATTERN_TYPE_SOLID) {
+            cairo_pattern_t *tmp;
+            int              tmp_x, tmp_y;
+            
+            if (src_pattern->type != CAIRO_PATTERN_TYPE_SOLID ||
+                sblend == DSBF_INVDESTALPHA) /* Doesn't work correctly */
+                return CAIRO_INT_STATUS_UNSUPPORTED;
+                
+            D_DEBUG_AT (Cairo_DirectFB, "Replacing src pattern by mask pattern.\n");
+                
+            tmp = src_pattern;
+            tmp_x = *src_x; tmp_y = *src_y;
+            
+            src_pattern = mask_pattern;
+            *src_x = *mask_x; *src_y = *mask_y;
+            
+            mask_pattern = tmp;
+            *mask_x = tmp_x; *mask_y = tmp_y;
+            
+            if (sblend == DSBF_ONE) {
+                flags |= DSBLIT_BLEND_ALPHACHANNEL;                   
+                sblend = DSBF_SRCALPHA;
+                //dblend = DSBF_INVSRCALPHA;
+            }
+        }
+            
+        pattern = (cairo_solid_pattern_t *)mask_pattern;
+        color.a = pattern->color.alpha_short >> 8;
+        color.r = pattern->color.red_short   >> 8;
+        color.g = pattern->color.green_short >> 8;
+        color.b = pattern->color.blue_short  >> 8;
+    } 
+    else {
+        color.a = color.r = color.g = color.b = 0xff;
+    }
+        
+    ret = _directfb_acquire_surface (dst, 0);
+    if (ret)
+        return ret;
+        
+    if (src_pattern->type == CAIRO_PATTERN_TYPE_SOLID) {
+        cairo_solid_pattern_t *pattern = (cairo_solid_pattern_t *)src_pattern;
+         
+        if (!dst->color) {
+            dst->color = _cairo_directfb_surface_create_similar (dst,
+                                                CAIRO_CONTENT_COLOR_ALPHA, 1, 1);
+            if (!dst->color)
+                return CAIRO_STATUS_NO_MEMORY;
+        }
+        
+        src = (cairo_directfb_surface_t *)dst->color;
+        src->buffer->SetColor (src->buffer,
+                               pattern->color.red_short   >> 8,
+                               pattern->color.green_short >> 8,
+                               pattern->color.blue_short  >> 8,
+                               pattern->color.alpha_short >> 8);
+        src->buffer->FillRectangle (src->buffer, 0, 0, 1, 1);
+        
+        cairo_matrix_init_identity (&src_attr.matrix);                           
+        src_attr.matrix   = src_pattern->matrix;
+        src_attr.extend   = CAIRO_EXTEND_NONE;
+        src_attr.filter   = CAIRO_FILTER_NEAREST;
+        src_attr.x_offset =
+        src_attr.y_offset = 0;
+    }
+    else {
+        ret = _cairo_pattern_acquire_surface (src_pattern, &dst->base,
+                                              *src_x, *src_y, width, height,
+                                              (cairo_surface_t **)&src, &src_attr);
+        if (ret)
+            return ret;
+        
+        ret = _directfb_acquire_surface (src, 0);
+        if (ret) {
+            _cairo_pattern_release_surface (src_pattern, &src->base, &src_attr);
+            return ret;
+        }
+    }
+        
+    if (color.a != 0xff)
+        flags |= DSBLIT_BLEND_COLORALPHA;
+    if (color.r != 0xff || color.g != 0xff || color.b != 0xff)
+        flags |= DSBLIT_COLORIZE;
+            
+    dst->buffer->SetBlittingFlags (dst->buffer, flags);
+    
+    if (flags & (DSBLIT_BLEND_COLORALPHA | DSBLIT_BLEND_ALPHACHANNEL)) {
+        dst->buffer->SetSrcBlendFunction (dst->buffer, sblend);
+        dst->buffer->SetDstBlendFunction (dst->buffer, dblend);
+    }
+    
+    if (flags & (DSBLIT_BLEND_COLORALPHA | DSBLIT_COLORIZE))    
+        dst->buffer->SetColor (dst->buffer, color.r, color.g, color.b, color.a); 
+        
+    *ret_src = src;
+    *ret_src_attr = src_attr;
+    
+    return CAIRO_STATUS_SUCCESS;
+}
+
+static void
+_directfb_finish_composite (cairo_directfb_surface_t   *dst,
+                            cairo_pattern_t            *src_pattern,
+                            cairo_surface_t            *src,
+                            cairo_surface_attributes_t *src_attr)
+{
+    if (src != dst->color)
+         _cairo_pattern_release_surface (src_pattern, src, src_attr);
 }
+#endif /* DFB_COMPOSITE || DFB_COMPOSITE_TRAPEZOIDS */        
 
 #if DFB_COMPOSITE
 static cairo_int_status_t
-_cairo_directfb_surface_composite (cairo_operator_t op,
-				cairo_pattern_t  *src_pattern,
-				cairo_pattern_t  *mask_pattern,
-				void         *abstract_dst,
-				int      src_x,
-				int      src_y,
-				int      mask_x,
-				int      mask_y,
-				int      dst_x,
-				int      dst_y,
-				unsigned int     width,
-				unsigned int     height)
-{
-    cairo_directfb_surface_t *dst = abstract_dst;
-    cairo_directfb_surface_t *src;
-    cairo_surface_pattern_t *src_surface_pattern;
-    int alpha;
-    int integer_transform;
-    int itx, ity;
-
-	cairo_directfb_surface_t *surface = abstract_dst;
-	if( _dfb_set_operator(op,surface->buffer) == DFB_UNSUPPORTED )
-		return CAIRO_INT_STATUS_UNSUPPORTED;
-
-    if (src_pattern->type == CAIRO_PATTERN_TYPE_SOLID ) {
-
-    } else if (src_pattern->type != CAIRO_PATTERN_TYPE_SURFACE ||
-			src_pattern->extend != CAIRO_EXTEND_NONE) {
-		return CAIRO_INT_STATUS_UNSUPPORTED;
-	}
+_cairo_directfb_surface_composite (cairo_operator_t  op,
+                                   cairo_pattern_t  *src_pattern,
+                                   cairo_pattern_t  *mask_pattern,
+                                   void             *abstract_dst,
+                                   int  src_x,  int  src_y,
+                                   int  mask_x, int  mask_y,
+                                   int  dst_x,  int  dst_y,
+                                   unsigned int      width,
+                                   unsigned int      height)
+{
+    cairo_directfb_surface_t   *dst = abstract_dst;
+    cairo_directfb_surface_t   *src;
+    cairo_surface_attributes_t  src_attr;
+    cairo_matrix_t             *m;
+    cairo_status_t              ret;
+    
+    D_DEBUG_AT (Cairo_DirectFB,
+                "%s( op=%d, src_pattern=%p, mask_pattern=%p, dst=%p,"
+                   " src_x=%d, src_y=%d, mask_x=%d, mask_y=%d, dst_x=%d,"
+                   " dst_y=%d, width=%u, height=%u ).\n",
+                __FUNCTION__, op, src_pattern, mask_pattern, dst, 
+                src_x, src_y, mask_x, mask_y, dst_x, dst_y, width, height);
+    
+    ret = _directfb_prepare_composite (dst, src_pattern, mask_pattern, op,
+                                       &src_x, &src_y, &mask_x, &mask_y, 
+                                       width, height, &src, &src_attr);
+    if (ret)
+        return ret;
+    
+    ret = CAIRO_INT_STATUS_UNSUPPORTED;
+    
+    m = &src_attr.matrix;
+    if (_cairo_matrix_is_integer_translation (m, NULL, NULL)) {
+        DFBRectangle sr;
+        
+        sr.x = src_x + src_attr.x_offset;
+        sr.y = src_y + src_attr.y_offset;
+        sr.w = src->width  - sr.x;
+        sr.h = src->height - sr.y;
+        
+        if (src_attr.extend == CAIRO_EXTEND_NONE) {
+            D_DEBUG_AT (Cairo_DirectFB, "Running Blit().\n");
+        
+            RUN_CLIPPED( dst, NULL,
+                         dst->buffer->Blit (dst->buffer,
+                                            src->buffer, &sr, dst_x, dst_y));
+            ret = CAIRO_STATUS_SUCCESS;
+        }
+        else if (src_attr.extend == CAIRO_EXTEND_REPEAT) {
+            DFBRegion clip;
+            
+            clip.x1 = dst_x;
+            clip.y1 = dst_y;
+            clip.x2 = dst_x + width  - 1;
+            clip.y2 = dst_y + height - 1;
+            
+            D_DEBUG_AT (Cairo_DirectFB, "Running TileBlit().\n");
+            
+            RUN_CLIPPED( dst, &clip,
+                         dst->buffer->TileBlit (dst->buffer, 
+                                                src->buffer, &sr, dst_x, dst_y));
+            ret = CAIRO_STATUS_SUCCESS;
+        }
+    }
+    else if (src_attr.extend == CAIRO_EXTEND_NONE &&
+             cairo_matrix_invert (m) == CAIRO_STATUS_SUCCESS)
+    {
+        DFBAccelerationMask accel;
+        
+        /* Yet I don't fully understand what these src_x/src_y mean.
+         * It seems they are X11 specific, so I ignore them for now.
+         */
+        src_x = src_y = 0;
+        
+        dst->buffer->GetAccelerationMask (dst->buffer, src->buffer, &accel);
+        
+        if (m->xy != 0.0 || m->yx != 0.0) {
+            if (accel & DFXL_TEXTRIANGLES) {
+                DFBVertex v[4];
+                float     w, h;
+                float     x1, y1, x2, y2;
+                int       i;
+            
+                w = MAX (src->width,  8);
+                h = MAX (src->height, 8);
+            
+                x1 = src_x + src_attr.x_offset;
+                y1 = src_y + src_attr.y_offset;
+                x2 = src->width  - x1;
+                y2 = src->height - y1;
+        
+                v[0].x = x1;
+                v[0].y = y1;
+                v[0].s = x1/w;
+                v[0].t = y1/h;
+        
+                v[1].x = x2;
+                v[1].y = y1;
+                v[1].s = x2/w;
+                v[1].t = y1/h;
+        
+                v[2].x = x2;
+                v[2].y = y2;
+                v[2].s = x2/w;
+                v[2].t = y2/h;
+        
+                v[3].x = x1;
+                v[3].y = y2;
+                v[3].s = x1/w;
+                v[3].t = y2/h;
+        
+                for (i = 0; i < 4; i++) {
+                    TRANSFORM_POINT (m, v[i].x, v[i].y, v[i].x, v[i].y);
+                    v[i].z  = 0;
+                    v[i].w  = 1;
+                }
+        
+                D_DEBUG_AT (Cairo_DirectFB, "Running TextureTriangles().\n");
+            
+                RUN_CLIPPED (dst, NULL,
+                             dst->buffer->TextureTriangles (dst->buffer, 
+                                            src->buffer, v, NULL, 4, DTTF_FAN));
+                ret = CAIRO_STATUS_SUCCESS;
+            }
+        }
+        else {
+            if (accel & DFXL_STRETCHBLIT ||
+                src_attr.filter == CAIRO_FILTER_NEAREST)
+            {
+                DFBRectangle sr, dr;
+                double       x1, y1, x2, y2;
+            
+                sr.x = src_x + src_attr.x_offset;
+                sr.y = src_y + src_attr.y_offset;
+                sr.w = src->width  - sr.x;
+                sr.h = src->height - sr.y;
+            
+                TRANSFORM_POINT (m, sr.x, sr.y, x1, y1);
+                TRANSFORM_POINT (m, sr.x+sr.w, sr.y+sr.h, x2, y2);
+                
+                dr.x = floor (x1+.5);
+                dr.y = floor (y1+.5);
+                dr.w = floor (x2-x1+.5);
+                dr.h = floor (y2-y1+.5);
+        
+                D_DEBUG_AT (Cairo_DirectFB, "Running StretchBlit().\n");
+
+                RUN_CLIPPED (dst, NULL,
+                             dst->buffer->StretchBlit (dst->buffer, 
+                                                       src->buffer, &sr, &dr));
+                ret = CAIRO_STATUS_SUCCESS;
+            }
+        }
+    }  
+   
+    _directfb_finish_composite (dst, src_pattern, &src->base, &src_attr);
+    
+    return ret;
+}
+#endif /* DFB_COMPOSITE */
 
-    if (mask_pattern) {
-	/* FIXME: When we fully support RENDER style 4-channel
-	 * masks we need to check r/g/b != 1.0.
-	 */
-	if (mask_pattern->type != CAIRO_PATTERN_TYPE_SOLID)
-	    return CAIRO_INT_STATUS_UNSUPPORTED;
-
-		alpha = ((cairo_solid_pattern_t *)mask_pattern)->color.alpha_short >> 8;
-    } else {
-    	alpha = 255;
-    }
-
-    src_surface_pattern = (cairo_surface_pattern_t *)src_pattern;
-    src = (cairo_directfb_surface_t *)src_surface_pattern->surface;
-
-    if (src->base.backend != dst->base.backend){
-	return CAIRO_INT_STATUS_UNSUPPORTED;
-	}
-
-    integer_transform = _cairo_matrix_is_integer_translation (&src_pattern->matrix, &itx, &ity);
-    if (!integer_transform) {
-	return CAIRO_INT_STATUS_UNSUPPORTED;
-	}
-	surface->buffer->SetPorterDuff(surface->buffer,DSPD_NONE);
-	return CAIRO_INT_STATUS_UNSUPPORTED;
+static cairo_int_status_t
+_cairo_directfb_surface_fill_rectangles (void                *abstract_surface,
+                                         cairo_operator_t     op,
+                                         const cairo_color_t *color,
+                                         cairo_rectangle_int16_t   *rects,
+                                         int                  n_rects)
+{
+    cairo_directfb_surface_t *dst = abstract_surface;
+    cairo_status_t            ret;
+    DFBSurfaceDrawingFlags    flags;
+    DFBSurfaceBlendFunction   sblend;
+    DFBSurfaceBlendFunction   dblend;
+    DFBRectangle              r[n_rects];
+    int                       i;
+    
+    D_DEBUG_AT (Cairo_DirectFB, 
+                "%s( dst=%p, op=%d, color=%p, rects=%p, n_rects=%d ).\n",
+                __FUNCTION__, dst, op, color, rects, n_rects);
+
+    if (_directfb_get_operator (op, &flags, NULL, &sblend, &dblend))
+        return CAIRO_INT_STATUS_UNSUPPORTED;
+    
+    ret = _directfb_acquire_surface (dst, 0);
+    if (ret)
+        return ret;
+    
+    dst->buffer->SetDrawingFlags (dst->buffer, flags);
+    if (flags & DSDRAW_BLEND) {
+        dst->buffer->SetSrcBlendFunction (dst->buffer, sblend);
+        dst->buffer->SetDstBlendFunction (dst->buffer, dblend);
+    }    
+    
+    dst->buffer->SetColor (dst->buffer, color->red_short   >> 8,
+                                        color->green_short >> 8, 
+                                        color->blue_short  >> 8, 
+                                        color->alpha_short >> 8 );
+       
+    for (i = 0; i < n_rects; i++) {
+        r[i].x = rects[i].x;
+        r[i].y = rects[i].y;
+        r[i].w = rects[i].width;
+        r[i].h = rects[i].height;
+    }
+     
+    RUN_CLIPPED (dst, NULL,
+                 dst->buffer->FillRectangles (dst->buffer, r, n_rects));
+    
+    return CAIRO_STATUS_SUCCESS;
 }
-#endif
 
+#if DFB_COMPOSITE_TRAPEZOIDS
 static cairo_int_status_t
-_cairo_directfb_surface_fill_rectangles (void		         *abstract_surface,
-				         cairo_operator_t	  op,
-				         const cairo_color_t     *color,
-				         cairo_rectangle_int16_t	 *rects,
-				         int		          n_rects)
-{
-	int i,k;
-	cairo_directfb_surface_t *surface = abstract_surface;
-	IDirectFBSurface *buffer = surface->buffer;
-
-	if( _dfb_set_operator(op,buffer) == DFB_UNSUPPORTED ) {
-		return CAIRO_INT_STATUS_UNSUPPORTED;
-	}
-
-	buffer->SetColor(buffer,color->red_short >> 8,
-				  color->green_short >> 8,
-				  color->blue_short >> 8,
-				  color->alpha_short >> 8 );
-	/*Not optimized not sure of the sorting on region*/
-	if( surface->clip ) {
-		DFBRegion region;
-    	int n_boxes = pixman_region_num_rects (surface->clip);
-    	pixman_box16_t *boxes = pixman_region_rects (surface->clip);
-		for( k = 0; k < n_boxes; k++ ) {
-        	region.x1 = boxes[k].x1;
-        	region.y1 = boxes[k].y1;
-        	region.x2 = boxes[k].x2;
-        	region.y2 = boxes[k].y2;
-			buffer->SetClip (buffer,&region);
-    		for (i = 0; i < n_rects; i++) {
-				buffer->FillRectangle(buffer,rects[i].x,rects[i].y,
-					rects[i].width,rects[i].height);
-			}
-		}
-		buffer->SetClip (buffer, NULL);
-	}else {
-		buffer->SetClip (buffer, NULL);
-    	for (i = 0; i < n_rects; i++) {
-			buffer->FillRectangle(buffer,rects[i].x,rects[i].y,
-				rects[i].width,rects[i].height);
-		}
-	}
-	return CAIRO_STATUS_SUCCESS;
+_cairo_directfb_surface_composite_trapezoids (cairo_operator_t   op,
+                                              cairo_pattern_t   *pattern,
+                                              void              *abstract_dst,
+                                              cairo_antialias_t  antialias,
+                                              int  src_x,   int  src_y,
+                                              int  dst_x,   int  dst_y,
+                                              unsigned int       width,
+                                              unsigned int       height,
+                                              cairo_trapezoid_t *traps,
+                                              int                num_traps )
+{
+    cairo_directfb_surface_t   *dst = abstract_dst;
+    cairo_directfb_surface_t   *src;
+    cairo_surface_attributes_t  src_attr;
+    cairo_status_t              ret;
+    DFBAccelerationMask         accel;
+    
+    D_DEBUG_AT (Cairo_DirectFB,
+                "%s( op=%d, pattern=%p, dst=%p, antialias=%d,"
+                   " src_x=%d, src_y=%d, dst_x=%d, dst_y=%d,"
+                   " width=%u, height=%u, traps=%p, num_traps=%d ).\n",
+                __FUNCTION__, op, pattern, dst, antialias,
+                src_x, src_y, dst_x, dst_y, width, height, traps, num_traps);
+
+    if (antialias != CAIRO_ANTIALIAS_NONE)
+        return CAIRO_INT_STATUS_UNSUPPORTED;
+        
+    /* Textures are not supported yet. */
+    if (pattern->type != CAIRO_PATTERN_TYPE_SOLID)
+        return CAIRO_INT_STATUS_UNSUPPORTED;
+    
+    ret = _directfb_prepare_composite (dst, pattern, NULL, op,
+                                       &src_x, &src_y, NULL, NULL, 
+                                       width, height, &src, &src_attr);
+    if (ret)
+        return ret;
+   
+    dst->buffer->GetAccelerationMask (dst->buffer, src->buffer, &accel);
+    
+    ret = CAIRO_INT_STATUS_UNSUPPORTED;
+    
+    if (accel & DFXL_TEXTRIANGLES) {
+        DFBVertex  vertex[6*num_traps];
+        DFBVertex *v = &vertex[0];
+        int        n;
+      
+#define ADD_TRI(id, x1, y1, s1, t1, x2, y2, s2, t2, x3, y3, s3, t3) {\
+    const int p = (id)*3;\
+    v[p+0].x=(x1); v[p+0].y=(y1); v[p+0].z=0; v[p+0].w=1; v[p+0].s=(s1); v[p+0].t=(t1);\
+    v[p+1].x=(x2); v[p+1].y=(y2); v[p+1].z=0; v[p+1].w=1; v[p+1].s=(s2); v[p+1].t=(t2);\
+    v[p+2].x=(x3); v[p+2].y=(y3); v[p+2].z=0; v[p+2].w=1; v[p+2].s=(s3); v[p+2].t=(t3);\
+}         
+       
+        for (n = 0; num_traps; num_traps--) {
+            float lx1, ly1, lx2, ly2;
+            float rx1, ry1, rx2, ry2;
+            
+            /* XXX: Do we need to validate the trapezoid? */
+ 
+            lx1 = traps->left.p1.x/65536.0;
+            ly1 = traps->left.p1.y/65536.0;
+            lx2 = traps->left.p2.x/65536.0;
+            ly2 = traps->left.p2.y/65536.0;
+            rx1 = traps->right.p1.x/65536.0;
+            ry1 = traps->right.p1.y/65536.0;
+            rx2 = traps->right.p2.x/65536.0;
+            ry2 = traps->right.p2.y/65536.0;
+            
+            if (traps->left.p1.y < traps->top) {
+                float y = traps->top/65536.0;
+                if (lx2 != lx1)
+                    lx1 = (y - ly1) * (lx2 - lx1) / (ly2 - ly1) + lx1;
+                ly1 = y;
+            } 
+            if (traps->left.p2.y > traps->bottom) {
+                float y = traps->bottom/65536.0;
+                if (lx2 != lx1)
+                    lx2 = (y - ly1) * (lx2 - lx1) / (ly2 - ly1) + lx1;
+                ly2 = y;
+            }
+            
+            if (traps->right.p1.y < traps->top) {
+                float y = traps->top/65536.0;
+                if (rx2 != rx1)
+                    rx1 = (y - ry1) * (rx2 - rx1) / (ry2 - ry1) + rx1;
+                ry1 = y;
+            }
+            if (traps->right.p2.y > traps->bottom) {
+                float y = traps->bottom/65536.0;
+                if (rx2 != rx1)
+                    rx2 = (y - ry1) * (rx2 - rx1) / (ry2 - ry1) + rx1;
+                ry2 = y;
+            }
+             
+            if (lx1 == rx1 && ly1 == ry1) {
+                ADD_TRI (0, lx2, ly2, 0, 0,
+                            lx1, ly1, 0, 0,
+                            rx2, ry2, 0, 0 );
+                v += 3;
+                n += 3;
+            }
+            else if (lx2 == rx2 && ly2 == ry2) {
+                ADD_TRI (0, lx1, ly1, 0, 0,
+                            lx2, ly2, 0, 0,
+                            rx1, ry1, 0, 0 );
+                v += 3;
+                n += 3;
+            }
+            else {
+                ADD_TRI (0, lx1, ly1, 0, 0, 
+                            rx1, ry1, 0, 0,
+                            lx2, ly2, 0, 0);
+                ADD_TRI (1, lx2, ly2, 0, 0,
+                            rx1, ry1, 0, 0,
+                            rx2, ry2, 0, 0);            
+                v += 6;
+                n += 6;
+            }
+            
+            traps++;
+        }
+        
+#undef ADD_TRI
+              
+        D_DEBUG_AT (Cairo_DirectFB, "Running TextureTriangles().\n");
+            
+        RUN_CLIPPED (dst, NULL,
+                     dst->buffer->TextureTriangles (dst->buffer, src->buffer, 
+                                                    vertex, NULL, n, DTTF_LIST));
+                                            
+        ret = CAIRO_STATUS_SUCCESS;
+    }
+    
+    _directfb_finish_composite (dst, pattern, &src->base, &src_attr);
+
+    return ret;
 }
+#endif /* DFB_COMPOSITE_TRAPEZOIDS */
 
 static cairo_int_status_t
-_cairo_directfb_surface_set_clip_region (void		*abstract_surface,
-				pixman_region16_t *region)
+_cairo_directfb_surface_set_clip_region (void              *abstract_surface,
+                                         pixman_region16_t *region)
 {
-	cairo_directfb_surface_t *surface = abstract_surface;
-	if( region == NULL ) {
-		pixman_region_destroy (surface->clip);
-		surface->clip = NULL;
-	}else {
-		if (!surface->clip) {
-	    	surface->clip = pixman_region_create ();
-	   		 if (!surface->clip)
-			return CAIRO_STATUS_NO_MEMORY;
-		}
-		pixman_region_copy (surface->clip, region);
-	}
-#if 0
-	if( surface->buffer_image )
-		_cairo_image_surface_set_clip_region (
-			(cairo_image_surface_t *)surface->buffer_image,region);
-#endif
+    cairo_directfb_surface_t *surface = abstract_surface;
+    
+    D_DEBUG_AT (Cairo_DirectFB, 
+                "%s( surface=%p, region=%p ).\n",
+                __FUNCTION__, surface, region);
+    
+    if (region) {
+        pixman_box16_t *boxes   = pixman_region_rects (region);
+        int             n_boxes = pixman_region_num_rects (region);
+        int             i;
+        
+        if (surface->n_clips != n_boxes) {
+            free (surface->clips);
+            
+            surface->clips = malloc (n_boxes * sizeof(DFBRegion));
+            if (!surface->clips) {
+                surface->n_clips = 0;
+                return CAIRO_STATUS_NO_MEMORY;
+            }
+        
+            surface->n_clips = n_boxes;
+        }
+        
+        for (i = 0; i < n_boxes; i++) {
+            surface->clips[i].x1 = boxes[i].x1;
+            surface->clips[i].y1 = boxes[i].y1;
+            surface->clips[i].x2 = boxes[i].x2;
+            surface->clips[i].y2 = boxes[i].y2;
+        }
+    }
+    else {
+        if (surface->clips) {
+            free (surface->clips);
+            surface->clips = NULL;
+            surface->n_clips = 0;
+        }
+    }
 
-	return CAIRO_STATUS_SUCCESS;
+    return CAIRO_STATUS_SUCCESS;
 }
 
 static cairo_int_status_t
-_cairo_directfb_abstract_surface_get_extents (void	              *abstract_surface,
-				              cairo_rectangle_int16_t *rectangle)
+_cairo_directfb_abstract_surface_get_extents (void              *abstract_surface,
+                                              cairo_rectangle_int16_t *rectangle)
 {
-		if( rectangle ) {
-				cairo_directfb_surface_t *surface = abstract_surface;
-				rectangle->x = 0;
-				rectangle->y = 0;
-				rectangle->width = surface->width;
-				rectangle->height = surface->height;
-		}
-		return CAIRO_STATUS_SUCCESS;
+    cairo_directfb_surface_t *surface = abstract_surface;
+    
+    D_DEBUG_AT (Cairo_DirectFB,
+                "%s( surface=%p, rectangle=%p ).\n",
+                __FUNCTION__, surface, rectangle);
+    
+    if (rectangle) {           
+        rectangle->x = 0;
+        rectangle->y = 0;
+        rectangle->width = surface->width;
+        rectangle->height = surface->height;
+    }
+    
+    return CAIRO_STATUS_SUCCESS;
 }
+
 static cairo_status_t
-_cairo_directfb_surface_mark_dirty_rectangle (void * abstract_surface,
-				int                    x,
-				int                    y,
-				int                    width,
-				int                    height)
-{
-	cairo_directfb_surface_t *surface = abstract_surface;
-	if( surface->main_surface != surface->buffer) {
-		DFBRegion region;
-		region.x1=x;
-		region.y1=y;
-		region.x2=x+width;
-		region.y2=y+height;
-		surface->buffer->SetClip (surface->buffer,&region);
-		surface->buffer->Blit(surface->buffer,surface->main_surface,NULL,0,0);
-		surface->buffer->SetClip (surface->buffer,NULL);
-	}
-	return CAIRO_STATUS_SUCCESS;
+_cairo_directfb_surface_mark_dirty_rectangle (void *abstract_surface,
+                                              int   x,
+                                              int   y,
+                                              int   width,
+                                              int   height)
+{
+    cairo_directfb_surface_t *surface = abstract_surface;
+    
+    D_DEBUG_AT (Cairo_DirectFB,
+                "%s( surface=%p, x=%d, y=%d, width=%d, height=%d ).\n",
+                __FUNCTION__, surface, x, y, width, height);
+    
+    if (surface->surface != surface->buffer) {
+        DFBRegion region = { x1:x, y1:y, x2:x+width-1, y2:y+height-1 };
+        surface->buffer->SetClip (surface->buffer, &region);
+        surface->buffer->SetBlittingFlags (surface->buffer, DSBLIT_NOFX);
+        surface->buffer->Blit (surface->buffer, surface->surface, NULL, 0, 0);
+    }
+    
+    return CAIRO_STATUS_SUCCESS;
 }
-static cairo_status_t
-_cairo_directfb_surface_flush (void  *abstract_surface)
+
+static cairo_status_t 
+_cairo_directfb_surface_flush (void *abstract_surface)
 {
-		cairo_directfb_surface_t *surface = abstract_surface;
-		if( surface->main_surface != surface->buffer)
-		surface->main_surface->Blit(surface->main_surface,surface->buffer,NULL,0,0);
-		return CAIRO_STATUS_SUCCESS;
+    cairo_directfb_surface_t *surface = abstract_surface;
+    
+    D_DEBUG_AT (Cairo_DirectFB, 
+                "%s( surface=%p ).\n", __FUNCTION__, surface);
+    
+    if (surface->surface != surface->buffer) {
+        surface->surface->SetClip (surface->surface, NULL);
+        surface->surface->SetBlittingFlags (surface->surface, DSBLIT_NOFX);
+        surface->surface->Blit (surface->surface, surface->buffer, NULL, 0, 0);
+    }
+        
+    return CAIRO_STATUS_SUCCESS;
 }
 
 #if DFB_SHOW_GLYPHS
-static void
-_cairo_directfb_surface_scaled_font_fini (cairo_scaled_font_t *scaled_font)
+static cairo_directfb_font_cache_t*
+_directfb_allocate_font_cache (IDirectFB *dfb, int width, int height)
 {
-    cairo_directfb_surface_font_private_t *font_private;
+    cairo_directfb_font_cache_t *cache;
 
-    font_private = scaled_font->surface_private;
-    if (font_private)
-    {
-    xxx_destroy (font_private);
+    cache = calloc (1, sizeof(cairo_directfb_font_cache_t));
+    if (!cache)
+        return NULL;
+
+    cache->buffer = _directfb_buffer_surface_create( dfb, DSPF_A8, width, height);
+    if (!cache->buffer) {
+        free (cache);
+        return NULL;
     }
+
+    dfb->AddRef (dfb);
+    cache->dfb = dfb;
+
+    cache->width  = width;
+    cache->height = height;
+
+    return cache;
 }
-#endif
 
-#if DFB_SHOW_GLYPHS
 static void
-_cairo_directfb_surface_scaled_glyph_fini (cairo_scaled_glyph_t *scaled_glyph,
-                    cairo_scaled_font_t  *scaled_font)
+_directfb_destroy_font_cache (cairo_directfb_font_cache_t *cache)
 {
-    cairo_directfb_surface_glyph_private_t *glyph_private;
+    cache->buffer->Release (cache->buffer);
+    cache->dfb->Release (cache->dfb);
+    free (cache);
+}
 
-    glyph_private = scaled_glyph->surface_private;
-    if (glyph_private)
-    {
-        xxx_release(glyph_private);
+static cairo_status_t
+_directfb_acquire_font_cache (cairo_directfb_surface_t     *surface,
+                              cairo_scaled_font_t          *scaled_font,
+                              const cairo_glyph_t          *glyphs,
+                              int                           num_glyphs,
+                              cairo_directfb_font_cache_t **ret_cache,
+                              DFBRectangle                 *rects,
+                              DFBPoint                     *points,
+                              int                          *ret_num )
+{
+    cairo_status_t               ret;
+    cairo_scaled_glyph_t        *chars[num_glyphs];
+    int                          num_chars = 0;
+    cairo_directfb_font_cache_t *cache     = NULL; 
+    int                          n         = 0;
+    int                          x         = 0;
+    int                          y         = 0;
+    int                          w         = 8;
+    int                          h         = 8;
+    int                          i;
+    
+    if (scaled_font->surface_private) {
+        cache = scaled_font->surface_private;
+        x = cache->x;
+        y = cache->y;
+    }
+    
+    for (i = 0; i < num_glyphs; i++) {
+        cairo_scaled_glyph_t  *scaled_glyph;
+        cairo_image_surface_t *img;
+        
+        ret = _cairo_scaled_glyph_lookup (scaled_font, glyphs[i].index,
+                                          CAIRO_SCALED_GLYPH_INFO_SURFACE,
+                                          &scaled_glyph);
+        if (ret)
+            return ret;
+        
+        img = scaled_glyph->surface;
+        switch (img->format) {
+            case CAIRO_FORMAT_A1:
+            case CAIRO_FORMAT_A8:
+                break;
+            default:
+                D_DEBUG_AT (Cairo_DirectFB,
+                            "Unsupported font format %d!\n", img->format);
+                return CAIRO_INT_STATUS_UNSUPPORTED;
+        }
+        
+        points[n].x = floor (glyphs[i].x + img->base.device_transform.x0 + .5);
+        points[n].y = floor (glyphs[i].y + img->base.device_transform.y0 + .5);
+        
+        if (points[n].x >= surface->width  ||
+            points[n].y >= surface->height ||
+            points[n].x+img->width  <= 0   ||
+            points[n].y+img->height <= 0)
+            continue;
+        
+        if (!scaled_glyph->surface_private) {
+            DFBRectangle *rect;
+            
+            if (x+img->width > 2048) {
+                x = 0;
+                y = h;
+                h = 8;
+            }
+        
+            rects[n].x = x;
+            rects[n].y = y;
+            rects[n].w = img->width;
+            rects[n].h = img->height;
+ 
+            x += img->width;
+            h  = MAX (h, img->height);
+            w  = MAX (w, x);
+            
+            /* Remember glyph location */ 
+            rect = malloc (sizeof(DFBRectangle));
+            if (!rect)
+                return CAIRO_STATUS_NO_MEMORY;
+            *rect = rects[n];
+            
+            scaled_glyph->surface_private = rect;
+            chars[num_chars++] = scaled_glyph;
+            
+            /*D_DEBUG_AT (Cairo_DirectFB, 
+                        "Glyph %lu will be loaded at (%d,%d).\n",
+                        glyphs[i].index, rects[n].x, rects[n].y);*/
+        }
+        else {
+            rects[n] = *((DFBRectangle *)scaled_glyph->surface_private);
+            
+            /*D_DEBUG_AT (Cairo_DirectFB, 
+                        "Glyph %lu already loaded at (%d,%d).\n",
+                        glyphs[i].index, rects[n].x, rects[n].y);*/
+        }
+            
+        n++;
     }
+    
+    if (!n)
+        return CAIRO_INT_STATUS_NOTHING_TO_DO;
+    
+    h += y;
+     
+    if (cache) {
+        if (cache->width < w || cache->height < h) {
+            cairo_directfb_font_cache_t *new_cache;
+            
+            w = MAX (w, cache->width);
+            h = MAX (h, cache->height);
+            
+            D_DEBUG_AT (Cairo_DirectFB,
+                        "Reallocating font cache (%dx%d).\n", w, h);
+            
+            new_cache = _directfb_allocate_font_cache (surface->dfb, w, h);
+            if (!new_cache)
+                return CAIRO_STATUS_NO_MEMORY;
+            
+            new_cache->buffer->Blit (new_cache->buffer,
+                                     cache->buffer, NULL, 0, 0);
+            
+            _directfb_destroy_font_cache (cache);
+            scaled_font->surface_private = cache = new_cache;
+        }
+    }
+    else {
+        D_DEBUG_AT (Cairo_DirectFB,
+                    "Allocating font cache (%dx%d).\n", w, h);
+        
+        cache = _directfb_allocate_font_cache (surface->dfb, w, h);
+        if (!cache)
+            return CAIRO_STATUS_NO_MEMORY;
+            
+        scaled_font->surface_backend = &cairo_directfb_surface_backend;
+        scaled_font->surface_private = cache;
+    }
+    
+    if (num_chars) {
+        unsigned char *data;
+        int            pitch;
+    
+        if (cache->buffer->Lock (cache->buffer, 
+                                 DSLF_WRITE, (void *)&data, &pitch))
+            return CAIRO_STATUS_NO_MEMORY;
+    
+        for (i = 0; i < num_chars; i++) {
+            cairo_image_surface_t *img  = chars[i]->surface;
+            DFBRectangle          *rect = chars[i]->surface_private;
+            unsigned char         *dst  = data + rect->y*pitch + rect->x;
+            unsigned char         *src  = img->data;
+            
+            if (img->format == CAIRO_FORMAT_A1) {
+                int j;
+                for (h = rect->h; h; h--) {
+                    for (j = 0; j < rect->w; j++)
+                        dst[j] = (src[j>>3] & (1 << (j&7))) ? 0xff : 0x00;
+                    dst += pitch;
+                    src += img->stride;
+                }
+            }
+            else {
+                for (h = rect->h; h; h--) {
+                    direct_memcpy (dst, src, rect->w);
+                    dst += pitch;
+                    src += img->stride;
+                }
+            }
+        }
+        
+        cache->buffer->Unlock (cache->buffer);
+    }
+
+    cache->x = x;
+    cache->y = y;
+    
+    *ret_cache = cache;
+    *ret_num   = n;
+
+    return CAIRO_STATUS_SUCCESS;
 }
-#endif
 
-static const cairo_surface_backend_t cairo_directfb_surface_backend = {
-		CAIRO_SURFACE_TYPE_DIRECTFB,
-		_cairo_directfb_surface_create_similar,
-		_cairo_directfb_surface_finish,
-		_cairo_directfb_surface_acquire_source_image,
-		_cairo_directfb_surface_release_source_image,
-		_cairo_directfb_surface_acquire_dest_image,
-		_cairo_directfb_surface_release_dest_image,
-		_cairo_directfb_surface_clone_similar,
+static void
+_cairo_directfb_surface_scaled_font_fini (cairo_scaled_font_t *scaled_font)
+{
+    cairo_directfb_font_cache_t *cache = scaled_font->surface_private;
+    
+    D_DEBUG_AT (Cairo_DirectFB,
+                "%s( scaled_font=%p ).\n", __FUNCTION__, scaled_font);
+    
+    if (cache) {
+        _directfb_destroy_font_cache (cache);
+        scaled_font->surface_private = NULL;
+    }
+}
+
+static void
+_cairo_directfb_surface_scaled_glyph_fini (cairo_scaled_glyph_t *scaled_glyph,
+                                           cairo_scaled_font_t  *scaled_font)
+{
+     D_DEBUG_AT (Cairo_DirectFB,
+                 "%s( scaled_glyph=%p, scaled_font=%p ).\n",
+                 __FUNCTION__, scaled_glyph, scaled_font);
+
+     if (scaled_glyph->surface_private) {
+          free (scaled_glyph->surface_private);
+          scaled_glyph->surface_private = NULL;
+     }
+}
+
+
+
+static cairo_int_status_t
+_cairo_directfb_surface_show_glyphs ( void                 *abstract_dst,
+                                     cairo_operator_t      op,
+                                     cairo_pattern_t      *pattern,
+                                     const cairo_glyph_t  *glyphs,
+                                     int                   num_glyphs,
+                                     cairo_scaled_font_t  *scaled_font)
+{
+    cairo_directfb_surface_t    *dst = abstract_dst;
+    cairo_directfb_font_cache_t *cache;
+    cairo_status_t               ret;
+    DFBSurfaceBlittingFlags      flags;
+    DFBSurfaceBlendFunction      sblend;
+    DFBSurfaceBlendFunction      dblend;
+    DFBColor                     color;
+    DFBRectangle                 rects[num_glyphs];
+    DFBPoint                     points[num_glyphs];
+    int                          num;
+    
+    D_DEBUG_AT (Cairo_DirectFB,
+                "%s( dst=%p, op=%d, pattern=%p, glyphs=%p, num_glyphs=%d, scaled_font=%p ).\n",
+                __FUNCTION__, dst, op, pattern, glyphs, num_glyphs, scaled_font);
+                            
+    if (pattern->type != CAIRO_PATTERN_TYPE_SOLID)
+        return CAIRO_INT_STATUS_UNSUPPORTED;
+        
+    if (_directfb_get_operator (op, NULL, &flags, &sblend, &dblend) ||
+        sblend == DSBF_DESTALPHA || sblend == DSBF_INVDESTALPHA) 
+        return CAIRO_INT_STATUS_UNSUPPORTED;
+        
+    ret = _directfb_acquire_surface (dst, 0);
+    if (ret)
+        return ret;
+        
+    ret = _directfb_acquire_font_cache (dst, scaled_font, glyphs, num_glyphs,
+                                        &cache, &rects[0], &points[0], &num);
+    if (ret) {
+        if (ret == CAIRO_INT_STATUS_NOTHING_TO_DO)
+            ret = CAIRO_STATUS_SUCCESS;
+        return ret;
+    }
+        
+    color.a = ((cairo_solid_pattern_t *)pattern)->color.alpha_short >> 8;
+    color.r = ((cairo_solid_pattern_t *)pattern)->color.red_short >> 8;
+    color.g = ((cairo_solid_pattern_t *)pattern)->color.green_short >> 8;
+    color.b = ((cairo_solid_pattern_t *)pattern)->color.blue_short >> 8;
+    
+    flags |= DSBLIT_BLEND_ALPHACHANNEL | DSBLIT_COLORIZE;
+    if (color.a != 0xff)
+        flags |= DSBLIT_BLEND_COLORALPHA;
+        
+    if (sblend == DSBF_ONE) {
+        sblend = DSBF_SRCALPHA;
+        if (dblend == DSBF_ZERO)
+            dblend = DSBF_INVSRCALPHA;
+    } 
+        
+    dst->buffer->SetBlittingFlags (dst->buffer, flags);
+    dst->buffer->SetSrcBlendFunction (dst->buffer, sblend);
+    dst->buffer->SetDstBlendFunction (dst->buffer, dblend);
+    dst->buffer->SetColor (dst->buffer, color.r, color.g, color.b, color.a);
+    
+    D_DEBUG_AT (Cairo_DirectFB, "Running BatchBlit().\n");
+        
+    RUN_CLIPPED (dst, NULL,
+                 dst->buffer->BatchBlit (dst->buffer,
+                                         cache->buffer, rects, points, num));
+        
+    return CAIRO_STATUS_SUCCESS;
+}
+#endif /* DFB_SHOW_GLYPHS */
+
+
+static cairo_surface_backend_t cairo_directfb_surface_backend = {
+         CAIRO_SURFACE_TYPE_DIRECTFB, /*type*/
+        _cairo_directfb_surface_create_similar,/*create_similar*/
+        _cairo_directfb_surface_finish, /*finish*/
+        _cairo_directfb_surface_acquire_source_image,/*acquire_source_image*/
+        _cairo_directfb_surface_release_source_image,/*release_source_image*/
+        _cairo_directfb_surface_acquire_dest_image,/*acquire_dest_image*/
+        _cairo_directfb_surface_release_dest_image,/*release_dest_image*/ 
+        _cairo_directfb_surface_clone_similar,/*clone_similar*/
 #if DFB_COMPOSITE
-		_cairo_directfb_surface_composite,
+        _cairo_directfb_surface_composite,/*composite*/
 #else
-		NULL,
+        NULL,/*composite*/
 #endif
-		_cairo_directfb_surface_fill_rectangles,
-		NULL,/*composite_trapezoids*/
-		NULL, /* copy_page */
-		NULL, /* show_page */
-		_cairo_directfb_surface_set_clip_region,
-		NULL, /* intersect_clip_path */
-		_cairo_directfb_abstract_surface_get_extents,
+        _cairo_directfb_surface_fill_rectangles,/*fill_rectangles*/
+#if DFB_COMPOSITE_TRAPEZOIDS
+        _cairo_directfb_surface_composite_trapezoids,/*composite_trapezoids*/
+#else
+        NULL,/*composite_trapezoids*/
+#endif
+        NULL, /* copy_page */
+        NULL, /* show_page */
+        _cairo_directfb_surface_set_clip_region,/*set_clip_region*/
+        NULL, /* intersect_clip_path */
+        _cairo_directfb_abstract_surface_get_extents,/*get_extents*/
+        NULL, /* old_show_glyphs */
+        NULL, /* get_font_options */
+        _cairo_directfb_surface_flush,/*flush*/
+        _cairo_directfb_surface_mark_dirty_rectangle,/*mark_dirty_rectangle*/
 #if DFB_SHOW_GLYPHS
-		_cairo_directfb_surface_show_glyphs,
+        _cairo_directfb_surface_scaled_font_fini,/*scaled_font_fini*/
+        _cairo_directfb_surface_scaled_glyph_fini,/*scaled_glyph_fini*/
 #else
-		NULL,
+        NULL,
+        NULL,
 #endif
-		NULL, /* get_font_options */
-		_cairo_directfb_surface_flush,
-		_cairo_directfb_surface_mark_dirty_rectangle,
+        NULL, /* paint */
+        NULL, /* mask */
+        NULL, /* stroke */
+        NULL, /* fill */
 #if DFB_SHOW_GLYPHS
-		_cairo_directfb_surface_scaled_font_fini,
-		_cairo_directfb_surface_scaled_glyph_fini
+        _cairo_directfb_surface_show_glyphs,/*show_glyphs*/
 #else
-		NULL,
-		NULL
+        NULL, /* show_glyphs */
 #endif
+        NULL /* snapshot */
 };
 
-#if DFB_SHOW_GLYPHS
-static cairo_int_status_t
-_cairo_directfb_surface_show_glyphs (cairo_scaled_font_t    *scaled_font,
-                 cairo_operator_t       operator,
-                 cairo_pattern_t        *pattern,
-                 void               *abstract_surface,
-                 int                    source_x,
-                 int                    source_y,
-                 int            dest_x,
-                 int            dest_y,
-                 unsigned int       width,
-                 unsigned int       height,
-                 const cairo_glyph_t    *glyphs,
-                 int                    num_glyphs)
-{
-	int i;
-	cairo_int_status_t status;
-	cairo_directfb_surface_t *surface = abstract_surface;
-	cairo_scaled_glyph_t *scaled_glyph;
 
-    if ((scaled_font->surface_backend != NULL &&
-     scaled_font->surface_backend != &cairo_directfb_surface_backend) )
-    return CAIRO_INT_STATUS_UNSUPPORTED;
+static void
+cairo_directfb_surface_backend_init (IDirectFB *dfb)
+{
+    DFBGraphicsDeviceDescription dsc;
+    static int                   done = 0;
+    
+    if (done)
+        return;
+        
+    dfb->GetDeviceDescription (dfb, &dsc);
+    
+#if DFB_COMPOSITE
+    if (!(dsc.acceleration_mask & DFXL_BLIT))
+        cairo_directfb_surface_backend.composite = NULL;
+#endif
 
-    if (scaled_font->surface_backend == NULL) {
-		scaled_font->surface_backend = &cairo_directfb_surface_backend;
-	}
-   /* Send all unsent glyphs to the server */
-    for (i = 0; i < num_glyphs; i++) {
-        IDirectFBSurface *tmpbuffer;
-		int x;
-		int y;
-		cairo_image_surface_t *glyph_img;
-    	status = _cairo_scaled_glyph_lookup (scaled_font,
-                         glyphs[i].index,
-                         CAIRO_SCALED_GLYPH_INFO_SURFACE,
-                         &scaled_glyph);
-		glyph_img = scaled_glyph->surface;
-    	if (status != CAIRO_STATUS_SUCCESS)
-        return status;
-		/* its a space or something*/
-		if( !glyph_img->data ) {
-			continue;
-		}
-    	if (scaled_glyph->surface_private == NULL  ) {
-        int dfbformat = cairo_to_directfb_format(glyph_img->format);
-		if( dfbformat == DSPF_UNKNOWN ) {
-					printf(" BAD IMAGE FORMAT[%d] cf=%d dfbf=%d data=%p \n",i,glyph_img->format,dfbformat,glyph_img->data);
-			continue;
-		}
-        	tmpbuffer = cairo_directfb_buffer_surface_create(surface->dfb,
-        	(void *)glyph_img->data,glyph_img->stride,dfbformat,glyph_img->width,glyph_img->height);
-			/*scaled_glyph->surface_private = tmpbuffer;*/
-    	}else {
-			tmpbuffer = (IDirectFBSurface *)scaled_glyph->surface_private;
-		}
-		if( !tmpbuffer ) {
-			assert(0); /*something really bad happend*/
-    		return CAIRO_INT_STATUS_UNSUPPORTED;
-		}
-   /* round glyph locations to the nearest pixel */
-   /* XXX: FRAGILE: We're ignoring device_transform scaling here. A bug? */
-    x = (int) floor (glyphs[i].x +
-             glyph_img->base.device_transform.x0 +
-             0.5);
-    y = (int) floor (glyphs[i].y +
-             glyph_img->base.device_transform.y0 +
-             0.5);
-	x +=dest_x;
-	y +=dest_y;
-
-		printf(" IMAGE FORMAT[%d] src_x=%d src_y=%d cf=%d data=%p x=%d y=%d w=%d h=%d\n",i,source_x,source_y,glyph_img->format,glyph_img->data,x,y,glyph_img->width,glyph_img->height);
-#if 0
-		surface->buffer->SetColor(surface->buffer,0,0xff,0,0xff);
-		surface->buffer->FillRectangle(surface->buffer,x,y,glyph_img->width,glyph_img->height);
-		surface->buffer->SetColor(surface->buffer,0,0xff,0xff,0xff);
+#if DFB_COMPOSITE_TRAPEZOIDS
+    if (!(dsc.acceleration_mask & DFXL_TEXTRIANGLES))
+        cairo_directfb_surface_backend.composite_trapezoids = NULL;
 #endif
-		surface->buffer->Blit(surface->buffer,tmpbuffer,NULL,x,y);
 
+#if DFB_SHOW_GLYPHS
+    if (!(dsc.acceleration_mask & DFXL_BLIT)            ||
+        !(dsc.blitting_flags & DSBLIT_COLORIZE)         ||
+        !(dsc.blitting_flags & DSBLIT_BLEND_ALPHACHANNEL))
+    {
+        cairo_directfb_surface_backend.scaled_font_fini = NULL;
+        cairo_directfb_surface_backend.scaled_glyph_fini = NULL;
+        cairo_directfb_surface_backend.show_glyphs = NULL;
     }
-	return CAIRO_INT_STATUS_UNSUPPORTED;
-}
 #endif
 
+    done = 1;
+}   
+
+
 cairo_surface_t *
-cairo_directfb_surface_create (IDirectFB *dfb,IDirectFBSurface *dfbsurface)
+cairo_directfb_surface_create (IDirectFB *dfb, IDirectFBSurface *dfbsurface) 
 {
-		cairo_directfb_surface_t *surface = calloc(1,sizeof(cairo_directfb_surface_t));
-		if( surface == NULL )
-		return NULL;
-		/* XXX: The content value here might be totally wrong. */
-		_cairo_surface_init (&surface->base, &cairo_directfb_surface_backend,
-				     CAIRO_CONTENT_COLOR_ALPHA);
-		/*Reference the surface */
-		dfb->AddRef(dfb);
-		dfbsurface->AddRef(dfbsurface);
-		surface->dfb=dfb;
-		surface->main_surface = dfbsurface;
-		dfbsurface->GetSize(dfbsurface,&surface->width,&surface->height);
-		surface->buffer = surface->main_surface;
-		surface->format = DFB_UNSUPPORTED;
-		surface->clip=NULL;
-		return ((cairo_surface_t *)surface);
+   DFBSurfacePixelFormat format;
+    cairo_directfb_surface_t *surface;
+    
+    cairo_directfb_surface_backend_init (dfb);
+        
+    surface = calloc (1, sizeof(cairo_directfb_surface_t));
+    if (!surface) 
+        return NULL;
+        
+    dfbsurface->GetPixelFormat (dfbsurface, &format);
+    _cairo_surface_init (&surface->base, &cairo_directfb_surface_backend,
+                _directfb_format_to_content(format));
+
+    dfb->AddRef (dfb);
+    surface->dfb = dfb;
+    dfbsurface->AddRef (dfbsurface); 
+    surface->surface = dfbsurface;
+    
+    if (_directfb_acquire_surface (surface, 0)) {
+        cairo_surface_destroy ((cairo_surface_t *)surface);
+        return NULL;
+    }
+        
+    return &surface->base;
 }
+


More information about the cairo-commit mailing list