]> Pileus Git - ~andy/gtk/blob - gdk/x11/gdkdrawable-x11.c
Update to changed cairo interface.
[~andy/gtk] / gdk / x11 / gdkdrawable-x11.c
1 /* GIMP Drawing Kit
2  * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 /*
21  * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
22  * file for a list of people on the GTK+ Team.  See the ChangeLog
23  * files for a list of changes.  These files are distributed with
24  * GTK+ at ftp://ftp.gtk.org/pub/gtk/. 
25  */
26
27 #include <config.h>
28
29 #include "gdkx.h"
30 #include "gdkregion-generic.h"
31
32 #include <pango/pangoxft.h>
33
34 #include <cairo-xlib.h>
35
36 #include <stdlib.h>
37 #include <string.h>             /* for memcpy() */
38
39 #if defined (HAVE_IPC_H) && defined (HAVE_SHM_H) && defined (HAVE_XSHM_H)
40 #define USE_SHM
41 #endif
42
43 #ifdef USE_SHM
44 #include <X11/extensions/XShm.h>
45 #endif /* USE_SHM */
46
47 #include "gdkprivate-x11.h"
48 #include "gdkdrawable-x11.h"
49 #include "gdkpixmap-x11.h"
50 #include "gdkscreen-x11.h"
51 #include "gdkdisplay-x11.h"
52
53 #include "gdkalias.h"
54
55 static void gdk_x11_draw_rectangle (GdkDrawable    *drawable,
56                                     GdkGC          *gc,
57                                     gboolean        filled,
58                                     gint            x,
59                                     gint            y,
60                                     gint            width,
61                                     gint            height);
62 static void gdk_x11_draw_arc       (GdkDrawable    *drawable,
63                                     GdkGC          *gc,
64                                     gboolean        filled,
65                                     gint            x,
66                                     gint            y,
67                                     gint            width,
68                                     gint            height,
69                                     gint            angle1,
70                                     gint            angle2);
71 static void gdk_x11_draw_polygon   (GdkDrawable    *drawable,
72                                     GdkGC          *gc,
73                                     gboolean        filled,
74                                     GdkPoint       *points,
75                                     gint            npoints);
76 static void gdk_x11_draw_text      (GdkDrawable    *drawable,
77                                     GdkFont        *font,
78                                     GdkGC          *gc,
79                                     gint            x,
80                                     gint            y,
81                                     const gchar    *text,
82                                     gint            text_length);
83 static void gdk_x11_draw_text_wc   (GdkDrawable    *drawable,
84                                     GdkFont        *font,
85                                     GdkGC          *gc,
86                                     gint            x,
87                                     gint            y,
88                                     const GdkWChar *text,
89                                     gint            text_length);
90 static void gdk_x11_draw_drawable  (GdkDrawable    *drawable,
91                                     GdkGC          *gc,
92                                     GdkPixmap      *src,
93                                     gint            xsrc,
94                                     gint            ysrc,
95                                     gint            xdest,
96                                     gint            ydest,
97                                     gint            width,
98                                     gint            height);
99 static void gdk_x11_draw_points    (GdkDrawable    *drawable,
100                                     GdkGC          *gc,
101                                     GdkPoint       *points,
102                                     gint            npoints);
103 static void gdk_x11_draw_segments  (GdkDrawable    *drawable,
104                                     GdkGC          *gc,
105                                     GdkSegment     *segs,
106                                     gint            nsegs);
107 static void gdk_x11_draw_lines     (GdkDrawable    *drawable,
108                                     GdkGC          *gc,
109                                     GdkPoint       *points,
110                                     gint            npoints);
111
112 static void gdk_x11_draw_image     (GdkDrawable     *drawable,
113                                     GdkGC           *gc,
114                                     GdkImage        *image,
115                                     gint             xsrc,
116                                     gint             ysrc,
117                                     gint             xdest,
118                                     gint             ydest,
119                                     gint             width,
120                                     gint             height);
121 static void gdk_x11_draw_pixbuf    (GdkDrawable     *drawable,
122                                     GdkGC           *gc,
123                                     GdkPixbuf       *pixbuf,
124                                     gint             src_x,
125                                     gint             src_y,
126                                     gint             dest_x,
127                                     gint             dest_y,
128                                     gint             width,
129                                     gint             height,
130                                     GdkRgbDither     dither,
131                                     gint             x_dither,
132                                     gint             y_dither);
133
134 static cairo_surface_t *gdk_x11_ref_cairo_surface (GdkDrawable *drawable);
135      
136 static void gdk_x11_set_colormap   (GdkDrawable    *drawable,
137                                     GdkColormap    *colormap);
138
139 static GdkColormap* gdk_x11_get_colormap   (GdkDrawable    *drawable);
140 static gint         gdk_x11_get_depth      (GdkDrawable    *drawable);
141 static GdkScreen *  gdk_x11_get_screen     (GdkDrawable    *drawable);
142 static GdkVisual*   gdk_x11_get_visual     (GdkDrawable    *drawable);
143
144 static void gdk_drawable_impl_x11_class_init (GdkDrawableImplX11Class *klass);
145
146 static void gdk_drawable_impl_x11_finalize   (GObject *object);
147
148 static gpointer parent_class = NULL;
149 static const cairo_user_data_key_t gdk_x11_cairo_key;
150
151 GType
152 _gdk_drawable_impl_x11_get_type (void)
153 {
154   static GType object_type = 0;
155
156   if (!object_type)
157     {
158       static const GTypeInfo object_info =
159       {
160         sizeof (GdkDrawableImplX11Class),
161         (GBaseInitFunc) NULL,
162         (GBaseFinalizeFunc) NULL,
163         (GClassInitFunc) gdk_drawable_impl_x11_class_init,
164         NULL,           /* class_finalize */
165         NULL,           /* class_data */
166         sizeof (GdkDrawableImplX11),
167         0,              /* n_preallocs */
168         (GInstanceInitFunc) NULL,
169       };
170       
171       object_type = g_type_register_static (GDK_TYPE_DRAWABLE,
172                                             "GdkDrawableImplX11",
173                                             &object_info, 0);
174     }
175   
176   return object_type;
177 }
178
179 static void
180 gdk_drawable_impl_x11_class_init (GdkDrawableImplX11Class *klass)
181 {
182   GdkDrawableClass *drawable_class = GDK_DRAWABLE_CLASS (klass);
183   GObjectClass *object_class = G_OBJECT_CLASS (klass);
184   
185   parent_class = g_type_class_peek_parent (klass);
186
187   object_class->finalize = gdk_drawable_impl_x11_finalize;
188   
189   drawable_class->create_gc = _gdk_x11_gc_new;
190   drawable_class->draw_rectangle = gdk_x11_draw_rectangle;
191   drawable_class->draw_arc = gdk_x11_draw_arc;
192   drawable_class->draw_polygon = gdk_x11_draw_polygon;
193   drawable_class->draw_text = gdk_x11_draw_text;
194   drawable_class->draw_text_wc = gdk_x11_draw_text_wc;
195   drawable_class->draw_drawable = gdk_x11_draw_drawable;
196   drawable_class->draw_points = gdk_x11_draw_points;
197   drawable_class->draw_segments = gdk_x11_draw_segments;
198   drawable_class->draw_lines = gdk_x11_draw_lines;
199   drawable_class->draw_image = gdk_x11_draw_image;
200   drawable_class->draw_pixbuf = gdk_x11_draw_pixbuf;
201   
202   drawable_class->ref_cairo_surface = gdk_x11_ref_cairo_surface;
203
204   drawable_class->set_colormap = gdk_x11_set_colormap;
205   drawable_class->get_colormap = gdk_x11_get_colormap;
206
207   drawable_class->get_depth = gdk_x11_get_depth;
208   drawable_class->get_screen = gdk_x11_get_screen;
209   drawable_class->get_visual = gdk_x11_get_visual;
210   
211   drawable_class->_copy_to_image = _gdk_x11_copy_to_image;
212 }
213
214 static void
215 gdk_drawable_impl_x11_finalize (GObject *object)
216 {
217   gdk_drawable_set_colormap (GDK_DRAWABLE (object), NULL);
218
219   G_OBJECT_CLASS (parent_class)->finalize (object);
220 }
221
222 /**
223  * _gdk_x11_drawable_finish:
224  * @drawable: a #GdkDrawableImplX11.
225  * 
226  * Performs necessary cleanup prior to freeing a pixmap or
227  * destroying a window.
228  **/
229 void
230 _gdk_x11_drawable_finish (GdkDrawable *drawable)
231 {
232   GdkDrawableImplX11 *impl = GDK_DRAWABLE_IMPL_X11 (drawable);
233   
234   if (impl->picture)
235     {
236       XRenderFreePicture (GDK_SCREEN_XDISPLAY (impl->screen),
237                           impl->picture);
238       impl->picture = None;
239     }
240   
241   if (impl->cairo_surface)
242     {
243       cairo_surface_finish (impl->cairo_surface);
244       cairo_surface_set_user_data (impl->cairo_surface, &gdk_x11_cairo_key,
245                                    NULL, NULL);
246     }
247 }
248
249 /**
250  * _gdk_x11_drawable_update_size:
251  * @drawable: a #GdkDrawableImplX11.
252  * 
253  * Updates the state of the drawable (in particular the drawable's
254  * cairo surface) when its size has changed.
255  **/
256 void
257 _gdk_x11_drawable_update_size (GdkDrawable *drawable)
258 {
259   GdkDrawableImplX11 *impl = GDK_DRAWABLE_IMPL_X11 (drawable);
260   
261   if (impl->cairo_surface)
262     {
263       int width, height;
264       
265       gdk_drawable_get_size (drawable, &width, &height);
266       cairo_xlib_surface_set_size (impl->cairo_surface, width, height);
267     }
268 }
269
270 static void
271 try_pixmap (Display *xdisplay,
272             int      screen,
273             int      depth)
274 {
275   Pixmap pixmap = XCreatePixmap (xdisplay,
276                                  RootWindow (xdisplay, screen),
277                                  1, 1, depth);
278   XFreePixmap (xdisplay, pixmap);
279 }
280
281 gboolean
282 _gdk_x11_have_render (GdkDisplay *display)
283 {
284   Display *xdisplay = GDK_DISPLAY_XDISPLAY (display);
285   GdkDisplayX11 *x11display = GDK_DISPLAY_X11 (display);
286
287   if (x11display->have_render == GDK_UNKNOWN)
288     {
289       int event_base, error_base;
290       x11display->have_render =
291         XRenderQueryExtension (xdisplay, &event_base, &error_base)
292         ? GDK_YES : GDK_NO;
293
294       if (x11display->have_render == GDK_YES)
295         {
296           /*
297            * Sun advertises RENDER, but fails to support 32-bit pixmaps.
298            * That is just no good.  Therefore, we check all screens
299            * for proper support.
300            */
301
302           int screen;
303           for (screen = 0; screen < ScreenCount (xdisplay); screen++)
304             {
305               int count;
306               int *depths = XListDepths (xdisplay, screen, &count);
307               gboolean has_8 = FALSE, has_32 = FALSE;
308
309               if (depths)
310                 {
311                   int i;
312
313                   for (i = 0; i < count; i++)
314                     {
315                       if (depths[i] == 8)
316                         has_8 = TRUE;
317                       else if (depths[i] == 32)
318                         has_32 = TRUE;
319                     }
320                   XFree (depths);
321                 }
322
323               /* At this point, we might have a false positive;
324                * buggy versions of Xinerama only report depths for
325                * which there is an associated visual; so we actually
326                * go ahead and try create pixmaps.
327                */
328               if (!(has_8 && has_32))
329                 {
330                   gdk_error_trap_push ();
331                   if (!has_8)
332                     try_pixmap (xdisplay, screen, 8);
333                   if (!has_32)
334                     try_pixmap (xdisplay, screen, 32);
335                   XSync (xdisplay, False);
336                   if (gdk_error_trap_pop () == 0)
337                     {
338                       has_8 = TRUE;
339                       has_32 = TRUE;
340                     }
341                 }
342               
343               if (!(has_8 && has_32))
344                 {
345                   g_warning ("The X server advertises that RENDER support is present,\n"
346                              "but fails to supply the necessary pixmap support.  In\n"
347                              "other words, it is buggy.");
348                   x11display->have_render = GDK_NO;
349                   break;
350                 }
351             }
352         }
353     }
354
355   return x11display->have_render == GDK_YES;
356 }
357
358 static Picture
359 gdk_x11_drawable_get_picture (GdkDrawable *drawable)
360 {
361   GdkDrawableImplX11 *impl = GDK_DRAWABLE_IMPL_X11 (drawable);
362   
363   if (!impl->picture)
364     {
365       Display *xdisplay = GDK_SCREEN_XDISPLAY (impl->screen);
366       XRenderPictFormat *format;
367       
368       GdkVisual *visual = gdk_drawable_get_visual (GDK_DRAWABLE_IMPL_X11 (drawable)->wrapper);
369       if (!visual)
370         return None;
371
372       format = XRenderFindVisualFormat (xdisplay, GDK_VISUAL_XVISUAL (visual));
373       if (format)
374         {
375           XRenderPictureAttributes attributes;
376           attributes.graphics_exposures = False;
377           
378           impl->picture = XRenderCreatePicture (xdisplay, impl->xid, format,
379                                                 CPGraphicsExposure, &attributes);
380         }
381     }
382   
383   return impl->picture;
384 }
385
386 static void
387 gdk_x11_drawable_update_picture_clip (GdkDrawable *drawable,
388                                       GdkGC       *gc)
389 {
390   GdkDrawableImplX11 *impl = GDK_DRAWABLE_IMPL_X11 (drawable);
391   Display *xdisplay = GDK_SCREEN_XDISPLAY (impl->screen);
392   Picture picture = gdk_x11_drawable_get_picture (drawable);
393   GdkRegion *clip_region = gc ? _gdk_gc_get_clip_region (gc) : NULL;
394
395   if (clip_region)
396     {
397       GdkRegionBox *boxes = clip_region->rects;
398       gint n_boxes = clip_region->numRects;
399       XRectangle *rects = g_new (XRectangle, n_boxes);
400       int i;
401
402       for (i=0; i < n_boxes; i++)
403         {
404           rects[i].x = CLAMP (boxes[i].x1 + gc->clip_x_origin, G_MINSHORT, G_MAXSHORT);
405           rects[i].y = CLAMP (boxes[i].y1 + gc->clip_y_origin, G_MINSHORT, G_MAXSHORT);
406           rects[i].width = CLAMP (boxes[i].x2 + gc->clip_x_origin, G_MINSHORT, G_MAXSHORT) - rects[i].x;
407           rects[i].height = CLAMP (boxes[i].y2 + gc->clip_y_origin, G_MINSHORT, G_MAXSHORT) - rects[i].y;
408         }
409       
410       XRenderSetPictureClipRectangles (xdisplay, picture,
411                                        0, 0, rects, n_boxes);
412       
413       g_free (rects);
414     }
415   else
416     {
417       XRenderPictureAttributes pa;
418       pa.clip_mask = None;
419       XRenderChangePicture (xdisplay, picture,
420                             CPClipMask, &pa);
421     }
422 }
423
424 /*****************************************************
425  * X11 specific implementations of generic functions *
426  *****************************************************/
427
428 static GdkColormap*
429 gdk_x11_get_colormap (GdkDrawable *drawable)
430 {
431   GdkDrawableImplX11 *impl;
432
433   impl = GDK_DRAWABLE_IMPL_X11 (drawable);
434
435   return impl->colormap;
436 }
437
438 static void
439 gdk_x11_set_colormap (GdkDrawable *drawable,
440                       GdkColormap *colormap)
441 {
442   GdkDrawableImplX11 *impl;
443
444   impl = GDK_DRAWABLE_IMPL_X11 (drawable);
445
446   if (impl->colormap == colormap)
447     return;
448   
449   if (impl->colormap)
450     g_object_unref (impl->colormap);
451   impl->colormap = colormap;
452   if (impl->colormap)
453     g_object_ref (impl->colormap);
454 }
455
456 /* Drawing
457  */
458
459 static void
460 gdk_x11_draw_rectangle (GdkDrawable *drawable,
461                         GdkGC       *gc,
462                         gboolean     filled,
463                         gint         x,
464                         gint         y,
465                         gint         width,
466                         gint         height)
467 {
468   GdkDrawableImplX11 *impl;
469
470   impl = GDK_DRAWABLE_IMPL_X11 (drawable);
471   
472   if (filled)
473     XFillRectangle (GDK_SCREEN_XDISPLAY (impl->screen), impl->xid,
474                     GDK_GC_GET_XGC (gc), x, y, width, height);
475   else
476     XDrawRectangle (GDK_SCREEN_XDISPLAY (impl->screen), impl->xid,
477                     GDK_GC_GET_XGC (gc), x, y, width, height);
478 }
479
480 static void
481 gdk_x11_draw_arc (GdkDrawable *drawable,
482                   GdkGC       *gc,
483                   gboolean     filled,
484                   gint         x,
485                   gint         y,
486                   gint         width,
487                   gint         height,
488                   gint         angle1,
489                   gint         angle2)
490 {
491   GdkDrawableImplX11 *impl;
492
493   impl = GDK_DRAWABLE_IMPL_X11 (drawable);
494
495   
496   if (filled)
497     XFillArc (GDK_SCREEN_XDISPLAY (impl->screen), impl->xid,
498               GDK_GC_GET_XGC (gc), x, y, width, height, angle1, angle2);
499   else
500     XDrawArc (GDK_SCREEN_XDISPLAY (impl->screen), impl->xid,
501               GDK_GC_GET_XGC (gc), x, y, width, height, angle1, angle2);
502 }
503
504 static void
505 gdk_x11_draw_polygon (GdkDrawable *drawable,
506                       GdkGC       *gc,
507                       gboolean     filled,
508                       GdkPoint    *points,
509                       gint         npoints)
510 {
511   XPoint *tmp_points;
512   gint tmp_npoints, i;
513   GdkDrawableImplX11 *impl;
514
515   impl = GDK_DRAWABLE_IMPL_X11 (drawable);
516
517   
518   if (!filled &&
519       (points[0].x != points[npoints-1].x || points[0].y != points[npoints-1].y))
520     {
521       tmp_npoints = npoints + 1;
522       tmp_points = g_new (XPoint, tmp_npoints);
523       tmp_points[npoints].x = points[0].x;
524       tmp_points[npoints].y = points[0].y;
525     }
526   else
527     {
528       tmp_npoints = npoints;
529       tmp_points = g_new (XPoint, tmp_npoints);
530     }
531
532   for (i=0; i<npoints; i++)
533     {
534       tmp_points[i].x = points[i].x;
535       tmp_points[i].y = points[i].y;
536     }
537   
538   if (filled)
539     XFillPolygon (GDK_SCREEN_XDISPLAY (impl->screen), impl->xid,
540                   GDK_GC_GET_XGC (gc), tmp_points, tmp_npoints, Complex, CoordModeOrigin);
541   else
542     XDrawLines (GDK_SCREEN_XDISPLAY (impl->screen), impl->xid,
543                 GDK_GC_GET_XGC (gc), tmp_points, tmp_npoints, CoordModeOrigin);
544
545   g_free (tmp_points);
546 }
547
548 /* gdk_x11_draw_text
549  *
550  * Modified by Li-Da Lho to draw 16 bits and Multibyte strings
551  *
552  * Interface changed: add "GdkFont *font" to specify font or fontset explicitely
553  */
554 static void
555 gdk_x11_draw_text (GdkDrawable *drawable,
556                    GdkFont     *font,
557                    GdkGC       *gc,
558                    gint         x,
559                    gint         y,
560                    const gchar *text,
561                    gint         text_length)
562 {
563   GdkDrawableImplX11 *impl;
564   Display *xdisplay;
565
566   impl = GDK_DRAWABLE_IMPL_X11 (drawable);
567   xdisplay = GDK_SCREEN_XDISPLAY (impl->screen);
568   
569   if (font->type == GDK_FONT_FONT)
570     {
571       XFontStruct *xfont = (XFontStruct *) GDK_FONT_XFONT (font);
572       XSetFont(xdisplay, GDK_GC_GET_XGC (gc), xfont->fid);
573       if ((xfont->min_byte1 == 0) && (xfont->max_byte1 == 0))
574         {
575           XDrawString (xdisplay, impl->xid,
576                        GDK_GC_GET_XGC (gc), x, y, text, text_length);
577         }
578       else
579         {
580           XDrawString16 (xdisplay, impl->xid,
581                          GDK_GC_GET_XGC (gc), x, y, (XChar2b *) text, text_length / 2);
582         }
583     }
584   else if (font->type == GDK_FONT_FONTSET)
585     {
586       XFontSet fontset = (XFontSet) GDK_FONT_XFONT (font);
587       XmbDrawString (xdisplay, impl->xid,
588                      fontset, GDK_GC_GET_XGC (gc), x, y, text, text_length);
589     }
590   else
591     g_error("undefined font type\n");
592 }
593
594 static void
595 gdk_x11_draw_text_wc (GdkDrawable    *drawable,
596                       GdkFont        *font,
597                       GdkGC          *gc,
598                       gint            x,
599                       gint            y,
600                       const GdkWChar *text,
601                       gint            text_length)
602 {
603   GdkDrawableImplX11 *impl;
604   Display *xdisplay;
605
606   impl = GDK_DRAWABLE_IMPL_X11 (drawable);
607   xdisplay = GDK_SCREEN_XDISPLAY (impl->screen);
608   
609   if (font->type == GDK_FONT_FONT)
610     {
611       XFontStruct *xfont = (XFontStruct *) GDK_FONT_XFONT (font);
612       gchar *text_8bit;
613       gint i;
614       XSetFont(xdisplay, GDK_GC_GET_XGC (gc), xfont->fid);
615       text_8bit = g_new (gchar, text_length);
616       for (i=0; i<text_length; i++) text_8bit[i] = text[i];
617       XDrawString (xdisplay, impl->xid,
618                    GDK_GC_GET_XGC (gc), x, y, text_8bit, text_length);
619       g_free (text_8bit);
620     }
621   else if (font->type == GDK_FONT_FONTSET)
622     {
623       if (sizeof(GdkWChar) == sizeof(wchar_t))
624         {
625           XwcDrawString (xdisplay, impl->xid,
626                          (XFontSet) GDK_FONT_XFONT (font),
627                          GDK_GC_GET_XGC (gc), x, y, (wchar_t *)text, text_length);
628         }
629       else
630         {
631           wchar_t *text_wchar;
632           gint i;
633           text_wchar = g_new (wchar_t, text_length);
634           for (i=0; i<text_length; i++) text_wchar[i] = text[i];
635           XwcDrawString (xdisplay, impl->xid,
636                          (XFontSet) GDK_FONT_XFONT (font),
637                          GDK_GC_GET_XGC (gc), x, y, text_wchar, text_length);
638           g_free (text_wchar);
639         }
640     }
641   else
642     g_error("undefined font type\n");
643 }
644
645 static void
646 gdk_x11_draw_drawable (GdkDrawable *drawable,
647                        GdkGC       *gc,
648                        GdkPixmap   *src,
649                        gint         xsrc,
650                        gint         ysrc,
651                        gint         xdest,
652                        gint         ydest,
653                        gint         width,
654                        gint         height)
655 {
656   int src_depth = gdk_drawable_get_depth (src);
657   int dest_depth = gdk_drawable_get_depth (drawable);
658   GdkDrawableImplX11 *impl;
659   GdkDrawableImplX11 *src_impl;
660   
661   impl = GDK_DRAWABLE_IMPL_X11 (drawable);
662
663   if (GDK_IS_DRAWABLE_IMPL_X11 (src))
664     src_impl = GDK_DRAWABLE_IMPL_X11 (src);
665   else
666     src_impl = NULL;
667   
668   if (src_depth == 1)
669     {
670       XCopyArea (GDK_SCREEN_XDISPLAY (impl->screen),
671                  src_impl ? src_impl->xid : GDK_DRAWABLE_XID (src),
672                  impl->xid,
673                  GDK_GC_GET_XGC (gc),
674                  xsrc, ysrc,
675                  width, height,
676                  xdest, ydest);
677     }
678   else if (dest_depth != 0 && src_depth == dest_depth)
679     {
680       XCopyArea (GDK_SCREEN_XDISPLAY (impl->screen),
681                  src_impl ? src_impl->xid : GDK_DRAWABLE_XID (src),
682                  impl->xid,
683                  GDK_GC_GET_XGC (gc),
684                  xsrc, ysrc,
685                  width, height,
686                  xdest, ydest);
687     }
688   else
689     g_warning ("Attempt to draw a drawable with depth %d to a drawable with depth %d",
690                src_depth, dest_depth);
691 }
692
693 static void
694 gdk_x11_draw_points (GdkDrawable *drawable,
695                      GdkGC       *gc,
696                      GdkPoint    *points,
697                      gint         npoints)
698 {
699   GdkDrawableImplX11 *impl;
700
701   impl = GDK_DRAWABLE_IMPL_X11 (drawable);
702
703   
704   /* We special-case npoints == 1, because X will merge multiple
705    * consecutive XDrawPoint requests into a PolyPoint request
706    */
707   if (npoints == 1)
708     {
709       XDrawPoint (GDK_SCREEN_XDISPLAY (impl->screen),
710                   impl->xid,
711                   GDK_GC_GET_XGC (gc),
712                   points[0].x, points[0].y);
713     }
714   else
715     {
716       gint i;
717       XPoint *tmp_points = g_new (XPoint, npoints);
718
719       for (i=0; i<npoints; i++)
720         {
721           tmp_points[i].x = points[i].x;
722           tmp_points[i].y = points[i].y;
723         }
724       
725       XDrawPoints (GDK_SCREEN_XDISPLAY (impl->screen),
726                    impl->xid,
727                    GDK_GC_GET_XGC (gc),
728                    tmp_points,
729                    npoints,
730                    CoordModeOrigin);
731
732       g_free (tmp_points);
733     }
734 }
735
736 static void
737 gdk_x11_draw_segments (GdkDrawable *drawable,
738                        GdkGC       *gc,
739                        GdkSegment  *segs,
740                        gint         nsegs)
741 {
742   GdkDrawableImplX11 *impl;
743
744   impl = GDK_DRAWABLE_IMPL_X11 (drawable);
745
746   
747   /* We special-case nsegs == 1, because X will merge multiple
748    * consecutive XDrawLine requests into a PolySegment request
749    */
750   if (nsegs == 1)
751     {
752       XDrawLine (GDK_SCREEN_XDISPLAY (impl->screen), impl->xid,
753                  GDK_GC_GET_XGC (gc), segs[0].x1, segs[0].y1,
754                  segs[0].x2, segs[0].y2);
755     }
756   else
757     {
758       gint i;
759       XSegment *tmp_segs = g_new (XSegment, nsegs);
760
761       for (i=0; i<nsegs; i++)
762         {
763           tmp_segs[i].x1 = segs[i].x1;
764           tmp_segs[i].x2 = segs[i].x2;
765           tmp_segs[i].y1 = segs[i].y1;
766           tmp_segs[i].y2 = segs[i].y2;
767         }
768       
769       XDrawSegments (GDK_SCREEN_XDISPLAY (impl->screen),
770                      impl->xid,
771                      GDK_GC_GET_XGC (gc),
772                      tmp_segs, nsegs);
773
774       g_free (tmp_segs);
775     }
776 }
777
778 static void
779 gdk_x11_draw_lines (GdkDrawable *drawable,
780                     GdkGC       *gc,
781                     GdkPoint    *points,
782                     gint         npoints)
783 {
784   gint i;
785   XPoint *tmp_points = g_new (XPoint, npoints);
786   GdkDrawableImplX11 *impl;
787
788   impl = GDK_DRAWABLE_IMPL_X11 (drawable);
789
790   
791   for (i=0; i<npoints; i++)
792     {
793       tmp_points[i].x = points[i].x;
794       tmp_points[i].y = points[i].y;
795     }
796       
797   XDrawLines (GDK_SCREEN_XDISPLAY (impl->screen),
798               impl->xid,
799               GDK_GC_GET_XGC (gc),
800               tmp_points, npoints,
801               CoordModeOrigin);
802
803   g_free (tmp_points);
804 }
805
806 static void
807 gdk_x11_draw_image     (GdkDrawable     *drawable,
808                         GdkGC           *gc,
809                         GdkImage        *image,
810                         gint             xsrc,
811                         gint             ysrc,
812                         gint             xdest,
813                         gint             ydest,
814                         gint             width,
815                         gint             height)
816 {
817   GdkDrawableImplX11 *impl;
818
819   impl = GDK_DRAWABLE_IMPL_X11 (drawable);
820
821 #ifdef USE_SHM  
822   if (image->type == GDK_IMAGE_SHARED)
823     XShmPutImage (GDK_SCREEN_XDISPLAY (impl->screen), impl->xid,
824                   GDK_GC_GET_XGC (gc), GDK_IMAGE_XIMAGE (image),
825                   xsrc, ysrc, xdest, ydest, width, height, False);
826   else
827 #endif
828     XPutImage (GDK_SCREEN_XDISPLAY (impl->screen), impl->xid,
829                GDK_GC_GET_XGC (gc), GDK_IMAGE_XIMAGE (image),
830                xsrc, ysrc, xdest, ydest, width, height);
831 }
832
833 static gint
834 gdk_x11_get_depth (GdkDrawable *drawable)
835 {
836   /* This is a bit bogus but I'm not sure the other way is better */
837
838   return gdk_drawable_get_depth (GDK_DRAWABLE_IMPL_X11 (drawable)->wrapper);
839 }
840
841 static GdkDrawable *
842 get_impl_drawable (GdkDrawable *drawable)
843 {
844   if (GDK_IS_WINDOW (drawable))
845     return ((GdkWindowObject *)drawable)->impl;
846   else if (GDK_IS_PIXMAP (drawable))
847     return ((GdkPixmapObject *)drawable)->impl;
848   else
849     {
850       g_warning (G_STRLOC " drawable is not a pixmap or window");
851       return NULL;
852     }
853 }
854
855 static GdkScreen*
856 gdk_x11_get_screen (GdkDrawable *drawable)
857 {
858   if (GDK_IS_DRAWABLE_IMPL_X11 (drawable))
859     return GDK_DRAWABLE_IMPL_X11 (drawable)->screen;
860   else
861     return GDK_DRAWABLE_IMPL_X11 (get_impl_drawable (drawable))->screen;
862 }
863
864 static GdkVisual*
865 gdk_x11_get_visual (GdkDrawable    *drawable)
866 {
867   return gdk_drawable_get_visual (GDK_DRAWABLE_IMPL_X11 (drawable)->wrapper);
868 }
869
870 /**
871  * gdk_x11_drawable_get_xdisplay:
872  * @drawable: a #GdkDrawable.
873  * 
874  * Returns the display of a #GdkDrawable.
875  * 
876  * Return value: an Xlib <type>Display*</type>.
877  **/
878 Display *
879 gdk_x11_drawable_get_xdisplay (GdkDrawable *drawable)
880 {
881   if (GDK_IS_DRAWABLE_IMPL_X11 (drawable))
882     return GDK_SCREEN_XDISPLAY (GDK_DRAWABLE_IMPL_X11 (drawable)->screen);
883   else
884     return GDK_SCREEN_XDISPLAY (GDK_DRAWABLE_IMPL_X11 (get_impl_drawable (drawable))->screen);
885 }
886
887 /**
888  * gdk_x11_drawable_get_xid:
889  * @drawable: a #GdkDrawable.
890  * 
891  * Returns the X resource (window or pixmap) belonging to a #GdkDrawable.
892  * 
893  * Return value: the ID of @drawable's X resource.
894  **/
895 XID
896 gdk_x11_drawable_get_xid (GdkDrawable *drawable)
897 {
898   GdkDrawable *impl;
899   
900   if (GDK_IS_WINDOW (drawable))
901     impl = ((GdkPixmapObject *)drawable)->impl;
902   else if (GDK_IS_PIXMAP (drawable))
903     impl = ((GdkPixmapObject *)drawable)->impl;
904   else
905     {
906       g_warning (G_STRLOC " drawable is not a pixmap or window");
907       return None;
908     }
909
910   return ((GdkDrawableImplX11 *)impl)->xid;
911 }
912
913 /* Code for accelerated alpha compositing using the RENDER extension.
914  * It's a bit long because there are lots of possibilities for
915  * what's the fastest depending on the available picture formats,
916  * whether we can used shared pixmaps, etc.
917  */
918
919 static GdkX11FormatType
920 select_format (GdkDisplay         *display,
921                XRenderPictFormat **format,
922                XRenderPictFormat **mask)
923 {
924   Display *xdisplay = GDK_DISPLAY_XDISPLAY (display);
925   XRenderPictFormat pf;
926
927   if (!_gdk_x11_have_render (display))
928     return GDK_X11_FORMAT_NONE;
929   
930   /* Look for a 32-bit xRGB and Axxx formats that exactly match the
931    * in memory data format. We can use them as pixmap and mask
932    * to deal with non-premultiplied data.
933    */
934
935   pf.type = PictTypeDirect;
936   pf.depth = 32;
937   pf.direct.redMask = 0xff;
938   pf.direct.greenMask = 0xff;
939   pf.direct.blueMask = 0xff;
940   
941   pf.direct.alphaMask = 0;
942   if (ImageByteOrder (xdisplay) == LSBFirst)
943     {
944       /* ABGR */
945       pf.direct.red = 0;
946       pf.direct.green = 8;
947       pf.direct.blue = 16;
948     }
949   else
950     {
951       /* RGBA */
952       pf.direct.red = 24;
953       pf.direct.green = 16;
954       pf.direct.blue = 8;
955     }
956   
957   *format = XRenderFindFormat (xdisplay,
958                                (PictFormatType | PictFormatDepth |
959                                 PictFormatRedMask | PictFormatRed |
960                                 PictFormatGreenMask | PictFormatGreen |
961                                 PictFormatBlueMask | PictFormatBlue |
962                                 PictFormatAlphaMask),
963                                &pf,
964                                0);
965
966   pf.direct.alphaMask = 0xff;
967   if (ImageByteOrder (xdisplay) == LSBFirst)
968     {
969       /* ABGR */
970       pf.direct.alpha = 24;
971     }
972   else
973     {
974       pf.direct.alpha = 0;
975     }
976   
977   *mask = XRenderFindFormat (xdisplay,
978                              (PictFormatType | PictFormatDepth |
979                               PictFormatAlphaMask | PictFormatAlpha),
980                              &pf,
981                              0);
982
983   if (*format && *mask)
984     return GDK_X11_FORMAT_EXACT_MASK;
985
986   /* OK, that failed, now look for xRGB and Axxx formats in
987    * RENDER's preferred order
988    */
989   pf.direct.alphaMask = 0;
990   /* ARGB */
991   pf.direct.red = 16;
992   pf.direct.green = 8;
993   pf.direct.blue = 0;
994   
995   *format = XRenderFindFormat (xdisplay,
996                                (PictFormatType | PictFormatDepth |
997                                 PictFormatRedMask | PictFormatRed |
998                                 PictFormatGreenMask | PictFormatGreen |
999                                 PictFormatBlueMask | PictFormatBlue |
1000                                 PictFormatAlphaMask),
1001                                &pf,
1002                                0);
1003
1004   pf.direct.alphaMask = 0xff;
1005   pf.direct.alpha = 24;
1006   
1007   *mask = XRenderFindFormat (xdisplay,
1008                              (PictFormatType | PictFormatDepth |
1009                               PictFormatAlphaMask | PictFormatAlpha),
1010                              &pf,
1011                              0);
1012
1013   if (*format && *mask)
1014     return GDK_X11_FORMAT_ARGB_MASK;
1015
1016   /* Finally, if neither of the above worked, fall back to
1017    * looking for combined ARGB -- we'll premultiply ourselves.
1018    */
1019
1020   pf.type = PictTypeDirect;
1021   pf.depth = 32;
1022   pf.direct.red = 16;
1023   pf.direct.green = 8;
1024   pf.direct.blue = 0;
1025   pf.direct.alphaMask = 0xff;
1026   pf.direct.alpha = 24;
1027
1028   *format = XRenderFindFormat (xdisplay,
1029                                (PictFormatType | PictFormatDepth |
1030                                 PictFormatRedMask | PictFormatRed |
1031                                 PictFormatGreenMask | PictFormatGreen |
1032                                 PictFormatBlueMask | PictFormatBlue |
1033                                 PictFormatAlphaMask | PictFormatAlpha),
1034                                &pf,
1035                                0);
1036   *mask = NULL;
1037
1038   if (*format)
1039     return GDK_X11_FORMAT_ARGB;
1040
1041   return GDK_X11_FORMAT_NONE;
1042 }
1043
1044 #if 0
1045 static void
1046 list_formats (XRenderPictFormat *pf)
1047 {
1048   gint i;
1049   
1050   for (i=0 ;; i++)
1051     {
1052       XRenderPictFormat *pf = XRenderFindFormat (impl->xdisplay, 0, NULL, i);
1053       if (pf)
1054         {
1055           g_print ("%2d R-%#06x/%#06x G-%#06x/%#06x B-%#06x/%#06x A-%#06x/%#06x\n",
1056                    pf->depth,
1057                    pf->direct.red,
1058                    pf->direct.redMask,
1059                    pf->direct.green,
1060                    pf->direct.greenMask,
1061                    pf->direct.blue,
1062                    pf->direct.blueMask,
1063                    pf->direct.alpha,
1064                    pf->direct.alphaMask);
1065         }
1066       else
1067         break;
1068     }
1069 }
1070 #endif  
1071
1072 void
1073 _gdk_x11_convert_to_format (guchar           *src_buf,
1074                             gint              src_rowstride,
1075                             guchar           *dest_buf,
1076                             gint              dest_rowstride,
1077                             GdkX11FormatType  dest_format,
1078                             GdkByteOrder      dest_byteorder,
1079                             gint              width,
1080                             gint              height)
1081 {
1082   gint i;
1083
1084   for (i=0; i < height; i++)
1085     {
1086       switch (dest_format)
1087         {
1088         case GDK_X11_FORMAT_EXACT_MASK:
1089           {
1090             memcpy (dest_buf + i * dest_rowstride,
1091                     src_buf + i * src_rowstride,
1092                     width * 4);
1093             break;
1094           }
1095         case GDK_X11_FORMAT_ARGB_MASK:
1096           {
1097             guchar *row = src_buf + i * src_rowstride;
1098             if (((gsize)row & 3) != 0)
1099               {
1100                 guchar *p = row;
1101                 guint32 *q = (guint32 *)(dest_buf + i * dest_rowstride);
1102                 guchar *end = p + 4 * width;
1103
1104                 while (p < end)
1105                   {
1106                     *q = (p[3] << 24) | (p[0] << 16) | (p[1] << 8) | p[2];
1107                     p += 4;
1108                     q++;
1109                   }
1110               }
1111             else
1112               {
1113                 guint32 *p = (guint32 *)row;
1114                 guint32 *q = (guint32 *)(dest_buf + i * dest_rowstride);
1115                 guint32 *end = p + width;
1116
1117 #if G_BYTE_ORDER == G_LITTLE_ENDIAN         
1118                 if (dest_byteorder == GDK_LSB_FIRST)
1119                   {
1120                     /* ABGR => ARGB */
1121                 
1122                     while (p < end)
1123                       {
1124                         *q = ( (*p & 0xff00ff00) |
1125                                ((*p & 0x000000ff) << 16) |
1126                                ((*p & 0x00ff0000) >> 16));
1127                         q++;
1128                         p++;
1129                       }
1130                   }
1131                 else
1132                   {
1133                     /* ABGR => BGRA */
1134                 
1135                     while (p < end)
1136                       {
1137                         *q = (((*p & 0xff000000) >> 24) |
1138                               ((*p & 0x00ffffff) << 8));
1139                         q++;
1140                         p++;
1141                       }
1142                   }
1143 #else /* G_BYTE_ORDER == G_BIG_ENDIAN */
1144                 if (dest_byteorder == GDK_LSB_FIRST)
1145                   {
1146                     /* RGBA => BGRA */
1147                 
1148                     while (p < end)
1149                       {
1150                         *q = ( (*p & 0x00ff00ff) |
1151                                ((*p & 0x0000ff00) << 16) |
1152                                ((*p & 0xff000000) >> 16));
1153                         q++;
1154                         p++;
1155                       }
1156                   }
1157                 else
1158                   {
1159                     /* RGBA => ARGB */
1160                 
1161                     while (p < end)
1162                       {
1163                         *q = (((*p & 0xffffff00) >> 8) |
1164                               ((*p & 0x000000ff) << 24));
1165                         q++;
1166                         p++;
1167                       }
1168                   }
1169 #endif /* G_BYTE_ORDER*/            
1170               }
1171             break;
1172           }
1173         case GDK_X11_FORMAT_ARGB:
1174           {
1175             guchar *p = (src_buf + i * src_rowstride);
1176             guchar *q = (dest_buf + i * dest_rowstride);
1177             guchar *end = p + 4 * width;
1178             guint t1,t2,t3;
1179             
1180 #define MULT(d,c,a,t) G_STMT_START { t = c * a; d = ((t >> 8) + t) >> 8; } G_STMT_END
1181             
1182             if (dest_byteorder == GDK_LSB_FIRST)
1183               {
1184                 while (p < end)
1185                   {
1186                     MULT(q[0], p[2], p[3], t1);
1187                     MULT(q[1], p[1], p[3], t2);
1188                     MULT(q[2], p[0], p[3], t3);
1189                     q[3] = p[3];
1190                     p += 4;
1191                     q += 4;
1192                   }
1193               }
1194             else
1195               {
1196                 while (p < end)
1197                   {
1198                     q[0] = p[3];
1199                     MULT(q[1], p[0], p[3], t1);
1200                     MULT(q[2], p[1], p[3], t2);
1201                     MULT(q[3], p[2], p[3], t3);
1202                     p += 4;
1203                     q += 4;
1204                   }
1205               }
1206 #undef MULT
1207             break;
1208           }
1209         case GDK_X11_FORMAT_NONE:
1210           g_assert_not_reached ();
1211           break;
1212         }
1213     }
1214 }
1215
1216 static void
1217 draw_with_images (GdkDrawable       *drawable,
1218                   GdkGC             *gc,
1219                   GdkX11FormatType   format_type,
1220                   XRenderPictFormat *format,
1221                   XRenderPictFormat *mask_format,
1222                   guchar            *src_rgb,
1223                   gint               src_rowstride,
1224                   gint               dest_x,
1225                   gint               dest_y,
1226                   gint               width,
1227                   gint               height)
1228 {
1229   GdkScreen *screen = GDK_DRAWABLE_IMPL_X11 (drawable)->screen;
1230   Display *xdisplay = GDK_SCREEN_XDISPLAY (screen);
1231   GdkImage *image;
1232   GdkPixmap *pix;
1233   GdkGC *pix_gc;
1234   Picture pict;
1235   Picture dest_pict;
1236   Picture mask = None;
1237   gint x0, y0;
1238
1239   pix = gdk_pixmap_new (gdk_screen_get_root_window (screen), width, height, 32);
1240                                                   
1241   pict = XRenderCreatePicture (xdisplay, 
1242                                GDK_PIXMAP_XID (pix),
1243                                format, 0, NULL);
1244   if (mask_format)
1245     mask = XRenderCreatePicture (xdisplay, 
1246                                  GDK_PIXMAP_XID (pix),
1247                                  mask_format, 0, NULL);
1248
1249   dest_pict = gdk_x11_drawable_get_picture (drawable);  
1250   
1251   pix_gc = _gdk_drawable_get_scratch_gc (pix, FALSE);
1252
1253   for (y0 = 0; y0 < height; y0 += GDK_SCRATCH_IMAGE_HEIGHT)
1254     {
1255       gint height1 = MIN (height - y0, GDK_SCRATCH_IMAGE_HEIGHT);
1256       for (x0 = 0; x0 < width; x0 += GDK_SCRATCH_IMAGE_WIDTH)
1257         {
1258           gint xs0, ys0;
1259           
1260           gint width1 = MIN (width - x0, GDK_SCRATCH_IMAGE_WIDTH);
1261           
1262           image = _gdk_image_get_scratch (screen, width1, height1, 32, &xs0, &ys0);
1263           
1264           _gdk_x11_convert_to_format (src_rgb + y0 * src_rowstride + 4 * x0, src_rowstride,
1265                                       (guchar *)image->mem + ys0 * image->bpl + xs0 * image->bpp, image->bpl,
1266                                       format_type, image->byte_order, 
1267                                       width1, height1);
1268
1269           gdk_draw_image (pix, pix_gc,
1270                           image, xs0, ys0, x0, y0, width1, height1);
1271         }
1272     }
1273   
1274   XRenderComposite (xdisplay, PictOpOver, pict, mask, dest_pict, 
1275                     0, 0, 0, 0, dest_x, dest_y, width, height);
1276
1277   XRenderFreePicture (xdisplay, pict);
1278   if (mask)
1279     XRenderFreePicture (xdisplay, mask);
1280   
1281   g_object_unref (pix);
1282 }
1283
1284 typedef struct _ShmPixmapInfo ShmPixmapInfo;
1285
1286 struct _ShmPixmapInfo
1287 {
1288   GdkImage *image;
1289   Pixmap    pix;
1290   Picture   pict;
1291   Picture   mask;
1292 };
1293
1294 /* Returns FALSE if we can't get a shm pixmap */
1295 static gboolean
1296 get_shm_pixmap_for_image (Display           *xdisplay,
1297                           GdkImage          *image,
1298                           XRenderPictFormat *format,
1299                           XRenderPictFormat *mask_format,
1300                           Pixmap            *pix,
1301                           Picture           *pict,
1302                           Picture           *mask)
1303 {
1304   ShmPixmapInfo *info;
1305   
1306   if (image->type != GDK_IMAGE_SHARED)
1307     return FALSE;
1308   
1309   info = g_object_get_data (G_OBJECT (image), "gdk-x11-shm-pixmap");
1310   if (!info)
1311     {
1312       *pix = _gdk_x11_image_get_shm_pixmap (image);
1313       
1314       if (!*pix)
1315         return FALSE;
1316       
1317       info = g_new (ShmPixmapInfo, 1);
1318       info->pix = *pix;
1319       
1320       info->pict = XRenderCreatePicture (xdisplay, info->pix,
1321                                          format, 0, NULL);
1322       if (mask_format)
1323         info->mask = XRenderCreatePicture (xdisplay, info->pix,
1324                                            mask_format, 0, NULL);
1325       else
1326         info->mask = None;
1327
1328       g_object_set_data (G_OBJECT (image), "gdk-x11-shm-pixmap", info);
1329     }
1330
1331   *pix = info->pix;
1332   *pict = info->pict;
1333   *mask = info->mask;
1334
1335   return TRUE;
1336 }
1337
1338 #ifdef USE_SHM
1339 /* Returns FALSE if drawing with ShmPixmaps is not possible */
1340 static gboolean
1341 draw_with_pixmaps (GdkDrawable       *drawable,
1342                    GdkGC             *gc,
1343                    GdkX11FormatType   format_type,
1344                    XRenderPictFormat *format,
1345                    XRenderPictFormat *mask_format,
1346                    guchar            *src_rgb,
1347                    gint               src_rowstride,
1348                    gint               dest_x,
1349                    gint               dest_y,
1350                    gint               width,
1351                    gint               height)
1352 {
1353   Display *xdisplay = GDK_SCREEN_XDISPLAY (GDK_DRAWABLE_IMPL_X11 (drawable)->screen);
1354   GdkImage *image;
1355   Pixmap pix;
1356   Picture pict;
1357   Picture dest_pict;
1358   Picture mask = None;
1359   gint x0, y0;
1360
1361   dest_pict = gdk_x11_drawable_get_picture (drawable);
1362   
1363   for (y0 = 0; y0 < height; y0 += GDK_SCRATCH_IMAGE_HEIGHT)
1364     {
1365       gint height1 = MIN (height - y0, GDK_SCRATCH_IMAGE_HEIGHT);
1366       for (x0 = 0; x0 < width; x0 += GDK_SCRATCH_IMAGE_WIDTH)
1367         {
1368           gint xs0, ys0;
1369           
1370           gint width1 = MIN (width - x0, GDK_SCRATCH_IMAGE_WIDTH);
1371           
1372           image = _gdk_image_get_scratch (GDK_DRAWABLE_IMPL_X11 (drawable)->screen,
1373                                           width1, height1, 32, &xs0, &ys0);
1374           if (!get_shm_pixmap_for_image (xdisplay, image, format, mask_format, &pix, &pict, &mask))
1375             return FALSE;
1376
1377           _gdk_x11_convert_to_format (src_rgb + y0 * src_rowstride + 4 * x0, src_rowstride,
1378                                       (guchar *)image->mem + ys0 * image->bpl + xs0 * image->bpp, image->bpl,
1379                                       format_type, image->byte_order, 
1380                                       width1, height1);
1381
1382           XRenderComposite (xdisplay, PictOpOver, pict, mask, dest_pict, 
1383                             xs0, ys0, xs0, ys0, x0 + dest_x, y0 + dest_y,
1384                             width1, height1);
1385         }
1386     }
1387
1388   return TRUE;
1389 }
1390 #endif
1391
1392 static void
1393 gdk_x11_draw_pixbuf (GdkDrawable     *drawable,
1394                      GdkGC           *gc,
1395                      GdkPixbuf       *pixbuf,
1396                      gint             src_x,
1397                      gint             src_y,
1398                      gint             dest_x,
1399                      gint             dest_y,
1400                      gint             width,
1401                      gint             height,
1402                      GdkRgbDither     dither,
1403                      gint             x_dither,
1404                      gint             y_dither)
1405 {
1406   GdkX11FormatType format_type;
1407   XRenderPictFormat *format, *mask_format;
1408   gint rowstride;
1409 #ifdef USE_SHM  
1410   gboolean use_pixmaps = TRUE;
1411 #endif /* USE_SHM */
1412     
1413   format_type = select_format (gdk_drawable_get_display (drawable),
1414                                &format, &mask_format);
1415
1416   if (format_type == GDK_X11_FORMAT_NONE ||
1417       !gdk_pixbuf_get_has_alpha (pixbuf) ||
1418       gdk_drawable_get_depth (drawable) == 1 ||
1419       (dither == GDK_RGB_DITHER_MAX && gdk_drawable_get_depth (drawable) != 24) ||
1420       gdk_x11_drawable_get_picture (drawable) == None)
1421     {
1422       GdkDrawable *wrapper = GDK_DRAWABLE_IMPL_X11 (drawable)->wrapper;
1423       GDK_DRAWABLE_CLASS (parent_class)->draw_pixbuf (wrapper, gc, pixbuf,
1424                                                       src_x, src_y, dest_x, dest_y,
1425                                                       width, height,
1426                                                       dither, x_dither, y_dither);
1427       return;
1428     }
1429
1430   gdk_x11_drawable_update_picture_clip (drawable, gc);
1431
1432   rowstride = gdk_pixbuf_get_rowstride (pixbuf);
1433
1434 #ifdef USE_SHM
1435   if (use_pixmaps)
1436     {
1437       if (!draw_with_pixmaps (drawable, gc,
1438                               format_type, format, mask_format,
1439                               gdk_pixbuf_get_pixels (pixbuf) + src_y * rowstride + src_x * 4,
1440                               rowstride,
1441                               dest_x, dest_y, width, height))
1442         use_pixmaps = FALSE;
1443     }
1444
1445   if (!use_pixmaps)
1446 #endif /* USE_SHM */
1447     draw_with_images (drawable, gc,
1448                       format_type, format, mask_format,
1449                       gdk_pixbuf_get_pixels (pixbuf) + src_y * rowstride + src_x * 4,
1450                       rowstride,
1451                       dest_x, dest_y, width, height);
1452 }
1453
1454 static void
1455 gdk_x11_cairo_surface_destroy (void *data)
1456 {
1457   GdkDrawableImplX11 *impl = data;
1458
1459   impl->cairo_surface = NULL;
1460 }
1461
1462 static cairo_surface_t *
1463 gdk_x11_ref_cairo_surface (GdkDrawable *drawable)
1464 {
1465   GdkDrawableImplX11 *impl = GDK_DRAWABLE_IMPL_X11 (drawable);
1466
1467   if (GDK_IS_WINDOW_IMPL_X11 (drawable) &&
1468       GDK_WINDOW_DESTROYED (impl->wrapper))
1469     return NULL;
1470
1471   if (!impl->cairo_surface)
1472     {
1473       GdkVisual *visual = NULL;
1474       int width, height;
1475   
1476       visual = gdk_drawable_get_visual (drawable);
1477
1478       gdk_drawable_get_size (drawable, &width, &height);
1479
1480       if (visual) 
1481         impl->cairo_surface = cairo_xlib_surface_create (GDK_SCREEN_XDISPLAY (impl->screen),
1482                                                          impl->xid,
1483                                                          GDK_VISUAL_XVISUAL (visual),
1484                                                          width, height);
1485       else if (gdk_drawable_get_depth (drawable) == 1)
1486         impl->cairo_surface = cairo_xlib_surface_create_for_bitmap (GDK_SCREEN_XDISPLAY (impl->screen),
1487                                                                     impl->xid,
1488                                                                     width, height);
1489       else
1490         {
1491           g_warning ("Using Cairo rendering requires the drawable argument to\n"
1492                      "have a specified colormap. All windows have a colormap,\n"
1493                      "however, pixmaps only have colormap by default if they\n"
1494                      "were created with a non-NULL window argument. Otherwise\n"
1495                      "a colormap must be set on them with gdk_drawable_set_colormap");
1496           return NULL;
1497         }
1498
1499       cairo_surface_set_user_data (impl->cairo_surface, &gdk_x11_cairo_key,
1500                                    drawable, gdk_x11_cairo_surface_destroy);
1501     }
1502   else
1503     cairo_surface_reference (impl->cairo_surface);
1504
1505   return impl->cairo_surface;
1506 }
1507
1508 #define __GDK_DRAWABLE_X11_C__
1509 #include "gdkaliasdef.c"