2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
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.
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.
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.
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/.
31 #include <cairo-xlib.h>
34 #include <string.h> /* for memcpy() */
36 #if defined (HAVE_IPC_H) && defined (HAVE_SHM_H) && defined (HAVE_XSHM_H)
41 #include <X11/extensions/XShm.h>
44 #include "gdkprivate-x11.h"
45 #include "gdkdrawable-x11.h"
46 #include "gdkpixmap-x11.h"
47 #include "gdkscreen-x11.h"
48 #include "gdkdisplay-x11.h"
51 static void gdk_x11_draw_rectangle (GdkDrawable *drawable,
58 static void gdk_x11_draw_arc (GdkDrawable *drawable,
67 static void gdk_x11_draw_polygon (GdkDrawable *drawable,
72 static void gdk_x11_draw_drawable (GdkDrawable *drawable,
81 GdkDrawable *original_src);
82 static void gdk_x11_draw_points (GdkDrawable *drawable,
86 static void gdk_x11_draw_segments (GdkDrawable *drawable,
90 static void gdk_x11_draw_lines (GdkDrawable *drawable,
95 static void gdk_x11_draw_image (GdkDrawable *drawable,
104 static void gdk_x11_draw_pixbuf (GdkDrawable *drawable,
117 static cairo_surface_t *gdk_x11_ref_cairo_surface (GdkDrawable *drawable);
119 static void gdk_x11_set_colormap (GdkDrawable *drawable,
120 GdkColormap *colormap);
122 static GdkColormap* gdk_x11_get_colormap (GdkDrawable *drawable);
123 static gint gdk_x11_get_depth (GdkDrawable *drawable);
124 static GdkScreen * gdk_x11_get_screen (GdkDrawable *drawable);
125 static GdkVisual* gdk_x11_get_visual (GdkDrawable *drawable);
127 static void gdk_drawable_impl_x11_finalize (GObject *object);
129 static const cairo_user_data_key_t gdk_x11_cairo_key;
131 G_DEFINE_TYPE (GdkDrawableImplX11, _gdk_drawable_impl_x11, GDK_TYPE_DRAWABLE)
134 _gdk_drawable_impl_x11_class_init (GdkDrawableImplX11Class *klass)
136 GdkDrawableClass *drawable_class = GDK_DRAWABLE_CLASS (klass);
137 GObjectClass *object_class = G_OBJECT_CLASS (klass);
139 object_class->finalize = gdk_drawable_impl_x11_finalize;
141 drawable_class->create_gc = _gdk_x11_gc_new;
142 drawable_class->draw_rectangle = gdk_x11_draw_rectangle;
143 drawable_class->draw_arc = gdk_x11_draw_arc;
144 drawable_class->draw_polygon = gdk_x11_draw_polygon;
145 drawable_class->draw_drawable_with_src = gdk_x11_draw_drawable;
146 drawable_class->draw_points = gdk_x11_draw_points;
147 drawable_class->draw_segments = gdk_x11_draw_segments;
148 drawable_class->draw_lines = gdk_x11_draw_lines;
149 drawable_class->draw_image = gdk_x11_draw_image;
150 drawable_class->draw_pixbuf = gdk_x11_draw_pixbuf;
152 drawable_class->ref_cairo_surface = gdk_x11_ref_cairo_surface;
154 drawable_class->set_colormap = gdk_x11_set_colormap;
155 drawable_class->get_colormap = gdk_x11_get_colormap;
157 drawable_class->get_depth = gdk_x11_get_depth;
158 drawable_class->get_screen = gdk_x11_get_screen;
159 drawable_class->get_visual = gdk_x11_get_visual;
161 drawable_class->_copy_to_image = _gdk_x11_copy_to_image;
165 _gdk_drawable_impl_x11_init (GdkDrawableImplX11 *impl)
170 gdk_drawable_impl_x11_finalize (GObject *object)
172 gdk_drawable_set_colormap (GDK_DRAWABLE (object), NULL);
174 G_OBJECT_CLASS (_gdk_drawable_impl_x11_parent_class)->finalize (object);
178 * _gdk_x11_drawable_finish:
179 * @drawable: a #GdkDrawableImplX11.
181 * Performs necessary cleanup prior to freeing a pixmap or
182 * destroying a window.
185 _gdk_x11_drawable_finish (GdkDrawable *drawable)
187 GdkDrawableImplX11 *impl = GDK_DRAWABLE_IMPL_X11 (drawable);
191 XRenderFreePicture (GDK_SCREEN_XDISPLAY (impl->screen),
193 impl->picture = None;
196 if (impl->cairo_surface)
198 cairo_surface_finish (impl->cairo_surface);
199 cairo_surface_set_user_data (impl->cairo_surface, &gdk_x11_cairo_key,
205 * _gdk_x11_drawable_update_size:
206 * @drawable: a #GdkDrawableImplX11.
208 * Updates the state of the drawable (in particular the drawable's
209 * cairo surface) when its size has changed.
212 _gdk_x11_drawable_update_size (GdkDrawable *drawable)
214 GdkDrawableImplX11 *impl = GDK_DRAWABLE_IMPL_X11 (drawable);
216 if (impl->cairo_surface)
220 gdk_drawable_get_size (drawable, &width, &height);
221 cairo_xlib_surface_set_size (impl->cairo_surface, width, height);
226 try_pixmap (Display *xdisplay,
230 Pixmap pixmap = XCreatePixmap (xdisplay,
231 RootWindow (xdisplay, screen),
233 XFreePixmap (xdisplay, pixmap);
237 _gdk_x11_have_render (GdkDisplay *display)
239 Display *xdisplay = GDK_DISPLAY_XDISPLAY (display);
240 GdkDisplayX11 *x11display = GDK_DISPLAY_X11 (display);
242 if (x11display->have_render == GDK_UNKNOWN)
244 int event_base, error_base;
245 x11display->have_render =
246 XRenderQueryExtension (xdisplay, &event_base, &error_base)
249 if (x11display->have_render == GDK_YES)
252 * Sun advertises RENDER, but fails to support 32-bit pixmaps.
253 * That is just no good. Therefore, we check all screens
254 * for proper support.
258 for (screen = 0; screen < ScreenCount (xdisplay); screen++)
261 int *depths = XListDepths (xdisplay, screen, &count);
262 gboolean has_8 = FALSE, has_32 = FALSE;
268 for (i = 0; i < count; i++)
272 else if (depths[i] == 32)
278 /* At this point, we might have a false positive;
279 * buggy versions of Xinerama only report depths for
280 * which there is an associated visual; so we actually
281 * go ahead and try create pixmaps.
283 if (!(has_8 && has_32))
285 gdk_error_trap_push ();
287 try_pixmap (xdisplay, screen, 8);
289 try_pixmap (xdisplay, screen, 32);
290 XSync (xdisplay, False);
291 if (gdk_error_trap_pop () == 0)
298 if (!(has_8 && has_32))
300 g_warning ("The X server advertises that RENDER support is present,\n"
301 "but fails to supply the necessary pixmap support. In\n"
302 "other words, it is buggy.");
303 x11display->have_render = GDK_NO;
310 return x11display->have_render == GDK_YES;
314 gdk_x11_drawable_get_picture (GdkDrawable *drawable)
316 GdkDrawableImplX11 *impl = GDK_DRAWABLE_IMPL_X11 (drawable);
320 Display *xdisplay = GDK_SCREEN_XDISPLAY (impl->screen);
321 XRenderPictFormat *format;
323 GdkVisual *visual = gdk_drawable_get_visual (GDK_DRAWABLE_IMPL_X11 (drawable)->wrapper);
327 format = XRenderFindVisualFormat (xdisplay, GDK_VISUAL_XVISUAL (visual));
330 XRenderPictureAttributes attributes;
331 attributes.graphics_exposures = False;
333 impl->picture = XRenderCreatePicture (xdisplay, impl->xid, format,
334 CPGraphicsExposure, &attributes);
338 return impl->picture;
342 gdk_x11_drawable_update_picture_clip (GdkDrawable *drawable,
345 GdkDrawableImplX11 *impl = GDK_DRAWABLE_IMPL_X11 (drawable);
346 Display *xdisplay = GDK_SCREEN_XDISPLAY (impl->screen);
347 Picture picture = gdk_x11_drawable_get_picture (drawable);
348 cairo_region_t *clip_region = gc ? _gdk_gc_get_clip_region (gc) : NULL;
355 _gdk_region_get_xrectangles (clip_region,
360 XRenderSetPictureClipRectangles (xdisplay, picture,
361 0, 0, rects, n_rects);
366 XRenderPictureAttributes pa;
370 pa_mask = CPClipMask;
371 if (gc && (mask = _gdk_gc_get_clip_mask (gc)))
373 pa.clip_mask = GDK_PIXMAP_XID (mask);
374 pa.clip_x_origin = gc->clip_x_origin;
375 pa.clip_y_origin = gc->clip_y_origin;
376 pa_mask |= CPClipXOrigin | CPClipYOrigin;
381 XRenderChangePicture (xdisplay, picture,
386 /*****************************************************
387 * X11 specific implementations of generic functions *
388 *****************************************************/
391 gdk_x11_get_colormap (GdkDrawable *drawable)
393 GdkDrawableImplX11 *impl;
395 impl = GDK_DRAWABLE_IMPL_X11 (drawable);
397 return impl->colormap;
401 gdk_x11_set_colormap (GdkDrawable *drawable,
402 GdkColormap *colormap)
404 GdkDrawableImplX11 *impl;
406 impl = GDK_DRAWABLE_IMPL_X11 (drawable);
408 if (impl->colormap == colormap)
412 g_object_unref (impl->colormap);
413 impl->colormap = colormap;
415 g_object_ref (impl->colormap);
422 gdk_x11_draw_rectangle (GdkDrawable *drawable,
430 GdkDrawableImplX11 *impl;
432 impl = GDK_DRAWABLE_IMPL_X11 (drawable);
435 XFillRectangle (GDK_SCREEN_XDISPLAY (impl->screen), impl->xid,
436 GDK_GC_GET_XGC (gc), x, y, width, height);
438 XDrawRectangle (GDK_SCREEN_XDISPLAY (impl->screen), impl->xid,
439 GDK_GC_GET_XGC (gc), x, y, width, height);
443 gdk_x11_draw_arc (GdkDrawable *drawable,
453 GdkDrawableImplX11 *impl;
455 impl = GDK_DRAWABLE_IMPL_X11 (drawable);
459 XFillArc (GDK_SCREEN_XDISPLAY (impl->screen), impl->xid,
460 GDK_GC_GET_XGC (gc), x, y, width, height, angle1, angle2);
462 XDrawArc (GDK_SCREEN_XDISPLAY (impl->screen), impl->xid,
463 GDK_GC_GET_XGC (gc), x, y, width, height, angle1, angle2);
467 gdk_x11_draw_polygon (GdkDrawable *drawable,
475 GdkDrawableImplX11 *impl;
477 impl = GDK_DRAWABLE_IMPL_X11 (drawable);
481 (points[0].x != points[npoints-1].x || points[0].y != points[npoints-1].y))
483 tmp_npoints = npoints + 1;
484 tmp_points = g_new (XPoint, tmp_npoints);
485 tmp_points[npoints].x = points[0].x;
486 tmp_points[npoints].y = points[0].y;
490 tmp_npoints = npoints;
491 tmp_points = g_new (XPoint, tmp_npoints);
494 for (i=0; i<npoints; i++)
496 tmp_points[i].x = points[i].x;
497 tmp_points[i].y = points[i].y;
501 XFillPolygon (GDK_SCREEN_XDISPLAY (impl->screen), impl->xid,
502 GDK_GC_GET_XGC (gc), tmp_points, tmp_npoints, Complex, CoordModeOrigin);
504 XDrawLines (GDK_SCREEN_XDISPLAY (impl->screen), impl->xid,
505 GDK_GC_GET_XGC (gc), tmp_points, tmp_npoints, CoordModeOrigin);
511 gdk_x11_draw_drawable (GdkDrawable *drawable,
520 GdkDrawable *original_src)
522 int src_depth = gdk_drawable_get_depth (src);
523 int dest_depth = gdk_drawable_get_depth (drawable);
524 GdkDrawableImplX11 *impl;
525 GdkDrawableImplX11 *src_impl;
527 impl = GDK_DRAWABLE_IMPL_X11 (drawable);
529 if (GDK_IS_DRAWABLE_IMPL_X11 (src))
530 src_impl = GDK_DRAWABLE_IMPL_X11 (src);
531 else if (GDK_IS_WINDOW (src))
532 src_impl = GDK_DRAWABLE_IMPL_X11(((GdkWindowObject *)src)->impl);
534 src_impl = GDK_DRAWABLE_IMPL_X11(((GdkPixmapObject *)src)->impl);
536 if (GDK_IS_WINDOW_IMPL_X11 (impl) &&
537 GDK_IS_PIXMAP_IMPL_X11 (src_impl))
539 GdkPixmapImplX11 *src_pixmap = GDK_PIXMAP_IMPL_X11 (src_impl);
540 /* Work around an Xserver bug where non-visible areas from
541 * a pixmap to a window will clear the window background
542 * in destination areas that are supposed to be clipped out.
543 * This is a problem with client side windows as this means
544 * things may draw outside the virtual windows. This could
545 * also happen for window to window copies, but I don't
546 * think we generate any calls like that.
549 * http://lists.freedesktop.org/archives/xorg/2009-February/043318.html
565 if (xsrc + width > src_pixmap->width)
566 width = src_pixmap->width - xsrc;
567 if (ysrc + height > src_pixmap->height)
568 height = src_pixmap->height - ysrc;
573 XCopyArea (GDK_SCREEN_XDISPLAY (impl->screen),
581 else if (dest_depth != 0 && src_depth == dest_depth)
583 XCopyArea (GDK_SCREEN_XDISPLAY (impl->screen),
592 g_warning ("Attempt to draw a drawable with depth %d to a drawable with depth %d",
593 src_depth, dest_depth);
597 gdk_x11_draw_points (GdkDrawable *drawable,
602 GdkDrawableImplX11 *impl;
604 impl = GDK_DRAWABLE_IMPL_X11 (drawable);
607 /* We special-case npoints == 1, because X will merge multiple
608 * consecutive XDrawPoint requests into a PolyPoint request
612 XDrawPoint (GDK_SCREEN_XDISPLAY (impl->screen),
615 points[0].x, points[0].y);
620 XPoint *tmp_points = g_new (XPoint, npoints);
622 for (i=0; i<npoints; i++)
624 tmp_points[i].x = points[i].x;
625 tmp_points[i].y = points[i].y;
628 XDrawPoints (GDK_SCREEN_XDISPLAY (impl->screen),
640 gdk_x11_draw_segments (GdkDrawable *drawable,
645 GdkDrawableImplX11 *impl;
647 impl = GDK_DRAWABLE_IMPL_X11 (drawable);
650 /* We special-case nsegs == 1, because X will merge multiple
651 * consecutive XDrawLine requests into a PolySegment request
655 XDrawLine (GDK_SCREEN_XDISPLAY (impl->screen), impl->xid,
656 GDK_GC_GET_XGC (gc), segs[0].x1, segs[0].y1,
657 segs[0].x2, segs[0].y2);
662 XSegment *tmp_segs = g_new (XSegment, nsegs);
664 for (i=0; i<nsegs; i++)
666 tmp_segs[i].x1 = segs[i].x1;
667 tmp_segs[i].x2 = segs[i].x2;
668 tmp_segs[i].y1 = segs[i].y1;
669 tmp_segs[i].y2 = segs[i].y2;
672 XDrawSegments (GDK_SCREEN_XDISPLAY (impl->screen),
682 gdk_x11_draw_lines (GdkDrawable *drawable,
688 XPoint *tmp_points = g_new (XPoint, npoints);
689 GdkDrawableImplX11 *impl;
691 impl = GDK_DRAWABLE_IMPL_X11 (drawable);
694 for (i=0; i<npoints; i++)
696 tmp_points[i].x = points[i].x;
697 tmp_points[i].y = points[i].y;
700 XDrawLines (GDK_SCREEN_XDISPLAY (impl->screen),
710 gdk_x11_draw_image (GdkDrawable *drawable,
720 GdkDrawableImplX11 *impl;
722 impl = GDK_DRAWABLE_IMPL_X11 (drawable);
725 if (image->type == GDK_IMAGE_SHARED)
726 XShmPutImage (GDK_SCREEN_XDISPLAY (impl->screen), impl->xid,
727 GDK_GC_GET_XGC (gc), GDK_IMAGE_XIMAGE (image),
728 xsrc, ysrc, xdest, ydest, width, height, False);
731 XPutImage (GDK_SCREEN_XDISPLAY (impl->screen), impl->xid,
732 GDK_GC_GET_XGC (gc), GDK_IMAGE_XIMAGE (image),
733 xsrc, ysrc, xdest, ydest, width, height);
737 gdk_x11_get_depth (GdkDrawable *drawable)
739 /* This is a bit bogus but I'm not sure the other way is better */
741 return gdk_drawable_get_depth (GDK_DRAWABLE_IMPL_X11 (drawable)->wrapper);
745 get_impl_drawable (GdkDrawable *drawable)
747 if (GDK_IS_WINDOW (drawable))
748 return ((GdkWindowObject *)drawable)->impl;
749 else if (GDK_IS_PIXMAP (drawable))
750 return ((GdkPixmapObject *)drawable)->impl;
753 g_warning (G_STRLOC " drawable is not a pixmap or window");
759 gdk_x11_get_screen (GdkDrawable *drawable)
761 if (GDK_IS_DRAWABLE_IMPL_X11 (drawable))
762 return GDK_DRAWABLE_IMPL_X11 (drawable)->screen;
764 return GDK_DRAWABLE_IMPL_X11 (get_impl_drawable (drawable))->screen;
768 gdk_x11_get_visual (GdkDrawable *drawable)
770 return gdk_drawable_get_visual (GDK_DRAWABLE_IMPL_X11 (drawable)->wrapper);
774 * gdk_x11_drawable_get_xdisplay:
775 * @drawable: a #GdkDrawable.
777 * Returns the display of a #GdkDrawable.
779 * Return value: an Xlib <type>Display*</type>.
782 gdk_x11_drawable_get_xdisplay (GdkDrawable *drawable)
784 if (GDK_IS_DRAWABLE_IMPL_X11 (drawable))
785 return GDK_SCREEN_XDISPLAY (GDK_DRAWABLE_IMPL_X11 (drawable)->screen);
787 return GDK_SCREEN_XDISPLAY (GDK_DRAWABLE_IMPL_X11 (get_impl_drawable (drawable))->screen);
791 * gdk_x11_drawable_get_xid:
792 * @drawable: a #GdkDrawable.
794 * Returns the X resource (window or pixmap) belonging to a #GdkDrawable.
796 * Return value: the ID of @drawable's X resource.
799 gdk_x11_drawable_get_xid (GdkDrawable *drawable)
803 if (GDK_IS_WINDOW (drawable))
805 GdkWindow *window = (GdkWindow *)drawable;
807 /* Try to ensure the window has a native window */
808 if (!_gdk_window_has_impl (window))
810 gdk_window_ensure_native (window);
812 /* We sync here to ensure the window is created in the Xserver when
813 * this function returns. This is required because the returned XID
814 * for this window must be valid immediately, even with another
815 * connection to the Xserver */
816 gdk_display_sync (gdk_drawable_get_display (window));
819 if (!GDK_WINDOW_IS_X11 (window))
821 g_warning (G_STRLOC " drawable is not a native X11 window");
825 impl = ((GdkWindowObject *)drawable)->impl;
827 else if (GDK_IS_PIXMAP (drawable))
828 impl = ((GdkPixmapObject *)drawable)->impl;
831 g_warning (G_STRLOC " drawable is not a pixmap or window");
835 return ((GdkDrawableImplX11 *)impl)->xid;
839 gdk_x11_window_get_drawable_impl (GdkWindow *window)
841 return ((GdkWindowObject *)window)->impl;
844 gdk_x11_pixmap_get_drawable_impl (GdkPixmap *pixmap)
846 return ((GdkPixmapObject *)pixmap)->impl;
849 /* Code for accelerated alpha compositing using the RENDER extension.
850 * It's a bit long because there are lots of possibilities for
851 * what's the fastest depending on the available picture formats,
852 * whether we can used shared pixmaps, etc.
855 static GdkX11FormatType
856 select_format (GdkDisplay *display,
857 XRenderPictFormat **format,
858 XRenderPictFormat **mask)
860 Display *xdisplay = GDK_DISPLAY_XDISPLAY (display);
861 XRenderPictFormat pf;
863 if (!_gdk_x11_have_render (display))
864 return GDK_X11_FORMAT_NONE;
866 /* Look for a 32-bit xRGB and Axxx formats that exactly match the
867 * in memory data format. We can use them as pixmap and mask
868 * to deal with non-premultiplied data.
871 pf.type = PictTypeDirect;
873 pf.direct.redMask = 0xff;
874 pf.direct.greenMask = 0xff;
875 pf.direct.blueMask = 0xff;
877 pf.direct.alphaMask = 0;
878 if (ImageByteOrder (xdisplay) == LSBFirst)
889 pf.direct.green = 16;
893 *format = XRenderFindFormat (xdisplay,
894 (PictFormatType | PictFormatDepth |
895 PictFormatRedMask | PictFormatRed |
896 PictFormatGreenMask | PictFormatGreen |
897 PictFormatBlueMask | PictFormatBlue |
898 PictFormatAlphaMask),
902 pf.direct.alphaMask = 0xff;
903 if (ImageByteOrder (xdisplay) == LSBFirst)
906 pf.direct.alpha = 24;
913 *mask = XRenderFindFormat (xdisplay,
914 (PictFormatType | PictFormatDepth |
915 PictFormatAlphaMask | PictFormatAlpha),
919 if (*format && *mask)
920 return GDK_X11_FORMAT_EXACT_MASK;
922 /* OK, that failed, now look for xRGB and Axxx formats in
923 * RENDER's preferred order
925 pf.direct.alphaMask = 0;
931 *format = XRenderFindFormat (xdisplay,
932 (PictFormatType | PictFormatDepth |
933 PictFormatRedMask | PictFormatRed |
934 PictFormatGreenMask | PictFormatGreen |
935 PictFormatBlueMask | PictFormatBlue |
936 PictFormatAlphaMask),
940 pf.direct.alphaMask = 0xff;
941 pf.direct.alpha = 24;
943 *mask = XRenderFindFormat (xdisplay,
944 (PictFormatType | PictFormatDepth |
945 PictFormatAlphaMask | PictFormatAlpha),
949 if (*format && *mask)
950 return GDK_X11_FORMAT_ARGB_MASK;
952 /* Finally, if neither of the above worked, fall back to
953 * looking for combined ARGB -- we'll premultiply ourselves.
956 pf.type = PictTypeDirect;
961 pf.direct.alphaMask = 0xff;
962 pf.direct.alpha = 24;
964 *format = XRenderFindFormat (xdisplay,
965 (PictFormatType | PictFormatDepth |
966 PictFormatRedMask | PictFormatRed |
967 PictFormatGreenMask | PictFormatGreen |
968 PictFormatBlueMask | PictFormatBlue |
969 PictFormatAlphaMask | PictFormatAlpha),
975 return GDK_X11_FORMAT_ARGB;
977 return GDK_X11_FORMAT_NONE;
982 list_formats (XRenderPictFormat *pf)
988 XRenderPictFormat *pf = XRenderFindFormat (impl->xdisplay, 0, NULL, i);
991 g_print ("%2d R-%#06x/%#06x G-%#06x/%#06x B-%#06x/%#06x A-%#06x/%#06x\n",
996 pf->direct.greenMask,
1000 pf->direct.alphaMask);
1009 _gdk_x11_convert_to_format (guchar *src_buf,
1012 gint dest_rowstride,
1013 GdkX11FormatType dest_format,
1014 GdkByteOrder dest_byteorder,
1020 for (i=0; i < height; i++)
1022 switch (dest_format)
1024 case GDK_X11_FORMAT_EXACT_MASK:
1026 memcpy (dest_buf + i * dest_rowstride,
1027 src_buf + i * src_rowstride,
1031 case GDK_X11_FORMAT_ARGB_MASK:
1033 guchar *row = src_buf + i * src_rowstride;
1034 if (((gsize)row & 3) != 0)
1037 guint32 *q = (guint32 *)(dest_buf + i * dest_rowstride);
1038 guchar *end = p + 4 * width;
1042 *q = (p[3] << 24) | (p[0] << 16) | (p[1] << 8) | p[2];
1049 guint32 *p = (guint32 *)row;
1050 guint32 *q = (guint32 *)(dest_buf + i * dest_rowstride);
1051 guint32 *end = p + width;
1053 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
1054 if (dest_byteorder == GDK_LSB_FIRST)
1060 *q = ( (*p & 0xff00ff00) |
1061 ((*p & 0x000000ff) << 16) |
1062 ((*p & 0x00ff0000) >> 16));
1073 *q = (((*p & 0xff000000) >> 24) |
1074 ((*p & 0x00ffffff) << 8));
1079 #else /* G_BYTE_ORDER == G_BIG_ENDIAN */
1080 if (dest_byteorder == GDK_LSB_FIRST)
1086 *q = ( (*p & 0x00ff00ff) |
1087 ((*p & 0x0000ff00) << 16) |
1088 ((*p & 0xff000000) >> 16));
1099 *q = (((*p & 0xffffff00) >> 8) |
1100 ((*p & 0x000000ff) << 24));
1105 #endif /* G_BYTE_ORDER*/
1109 case GDK_X11_FORMAT_ARGB:
1111 guchar *p = (src_buf + i * src_rowstride);
1112 guchar *q = (dest_buf + i * dest_rowstride);
1113 guchar *end = p + 4 * width;
1116 #define MULT(d,c,a,t) G_STMT_START { t = c * a; d = ((t >> 8) + t) >> 8; } G_STMT_END
1118 if (dest_byteorder == GDK_LSB_FIRST)
1122 MULT(q[0], p[2], p[3], t1);
1123 MULT(q[1], p[1], p[3], t2);
1124 MULT(q[2], p[0], p[3], t3);
1135 MULT(q[1], p[0], p[3], t1);
1136 MULT(q[2], p[1], p[3], t2);
1137 MULT(q[3], p[2], p[3], t3);
1145 case GDK_X11_FORMAT_NONE:
1146 g_assert_not_reached ();
1153 draw_with_images (GdkDrawable *drawable,
1155 GdkX11FormatType format_type,
1156 XRenderPictFormat *format,
1157 XRenderPictFormat *mask_format,
1165 GdkScreen *screen = GDK_DRAWABLE_IMPL_X11 (drawable)->screen;
1166 Display *xdisplay = GDK_SCREEN_XDISPLAY (screen);
1172 Picture mask = None;
1175 pix = gdk_pixmap_new (gdk_screen_get_root_window (screen), width, height, 32);
1177 pict = XRenderCreatePicture (xdisplay,
1178 GDK_PIXMAP_XID (pix),
1181 mask = XRenderCreatePicture (xdisplay,
1182 GDK_PIXMAP_XID (pix),
1183 mask_format, 0, NULL);
1185 dest_pict = gdk_x11_drawable_get_picture (drawable);
1187 pix_gc = _gdk_drawable_get_scratch_gc (pix, FALSE);
1189 for (y0 = 0; y0 < height; y0 += GDK_SCRATCH_IMAGE_HEIGHT)
1191 gint height1 = MIN (height - y0, GDK_SCRATCH_IMAGE_HEIGHT);
1192 for (x0 = 0; x0 < width; x0 += GDK_SCRATCH_IMAGE_WIDTH)
1196 gint width1 = MIN (width - x0, GDK_SCRATCH_IMAGE_WIDTH);
1198 image = _gdk_image_get_scratch (screen, width1, height1, 32, &xs0, &ys0);
1200 _gdk_x11_convert_to_format (src_rgb + y0 * src_rowstride + 4 * x0, src_rowstride,
1201 (guchar *)image->mem + ys0 * image->bpl + xs0 * image->bpp, image->bpl,
1202 format_type, image->byte_order,
1205 gdk_draw_image (pix, pix_gc,
1206 image, xs0, ys0, x0, y0, width1, height1);
1210 XRenderComposite (xdisplay, PictOpOver, pict, mask, dest_pict,
1211 0, 0, 0, 0, dest_x, dest_y, width, height);
1213 XRenderFreePicture (xdisplay, pict);
1215 XRenderFreePicture (xdisplay, mask);
1217 g_object_unref (pix);
1220 typedef struct _ShmPixmapInfo ShmPixmapInfo;
1222 struct _ShmPixmapInfo
1231 shm_pixmap_info_destroy (gpointer data)
1233 ShmPixmapInfo *info = data;
1235 if (info->pict != None)
1236 XRenderFreePicture (info->display, info->pict);
1237 if (info->mask != None)
1238 XRenderFreePicture (info->display, info->mask);
1244 /* Returns FALSE if we can't get a shm pixmap */
1246 get_shm_pixmap_for_image (Display *xdisplay,
1248 XRenderPictFormat *format,
1249 XRenderPictFormat *mask_format,
1254 ShmPixmapInfo *info;
1256 if (image->type != GDK_IMAGE_SHARED)
1259 info = g_object_get_data (G_OBJECT (image), "gdk-x11-shm-pixmap");
1262 *pix = _gdk_x11_image_get_shm_pixmap (image);
1267 info = g_new (ShmPixmapInfo, 1);
1268 info->display = xdisplay;
1271 info->pict = XRenderCreatePicture (xdisplay, info->pix,
1274 info->mask = XRenderCreatePicture (xdisplay, info->pix,
1275 mask_format, 0, NULL);
1279 g_object_set_data_full (G_OBJECT (image), "gdk-x11-shm-pixmap", info,
1280 shm_pixmap_info_destroy);
1291 /* Returns FALSE if drawing with ShmPixmaps is not possible */
1293 draw_with_pixmaps (GdkDrawable *drawable,
1295 GdkX11FormatType format_type,
1296 XRenderPictFormat *format,
1297 XRenderPictFormat *mask_format,
1305 Display *xdisplay = GDK_SCREEN_XDISPLAY (GDK_DRAWABLE_IMPL_X11 (drawable)->screen);
1310 Picture mask = None;
1313 dest_pict = gdk_x11_drawable_get_picture (drawable);
1315 for (y0 = 0; y0 < height; y0 += GDK_SCRATCH_IMAGE_HEIGHT)
1317 gint height1 = MIN (height - y0, GDK_SCRATCH_IMAGE_HEIGHT);
1318 for (x0 = 0; x0 < width; x0 += GDK_SCRATCH_IMAGE_WIDTH)
1322 gint width1 = MIN (width - x0, GDK_SCRATCH_IMAGE_WIDTH);
1324 image = _gdk_image_get_scratch (GDK_DRAWABLE_IMPL_X11 (drawable)->screen,
1325 width1, height1, 32, &xs0, &ys0);
1326 if (!get_shm_pixmap_for_image (xdisplay, image, format, mask_format, &pix, &pict, &mask))
1329 _gdk_x11_convert_to_format (src_rgb + y0 * src_rowstride + 4 * x0, src_rowstride,
1330 (guchar *)image->mem + ys0 * image->bpl + xs0 * image->bpp, image->bpl,
1331 format_type, image->byte_order,
1334 XRenderComposite (xdisplay, PictOpOver, pict, mask, dest_pict,
1335 xs0, ys0, xs0, ys0, x0 + dest_x, y0 + dest_y,
1345 gdk_x11_draw_pixbuf (GdkDrawable *drawable,
1354 GdkRgbDither dither,
1358 GdkX11FormatType format_type;
1359 XRenderPictFormat *format, *mask_format;
1362 gboolean use_pixmaps = TRUE;
1363 #endif /* USE_SHM */
1365 format_type = select_format (gdk_drawable_get_display (drawable),
1366 &format, &mask_format);
1368 if (format_type == GDK_X11_FORMAT_NONE ||
1369 !gdk_pixbuf_get_has_alpha (pixbuf) ||
1370 gdk_drawable_get_depth (drawable) == 1 ||
1371 (dither == GDK_RGB_DITHER_MAX && gdk_drawable_get_depth (drawable) != 24) ||
1372 gdk_x11_drawable_get_picture (drawable) == None)
1374 GdkDrawable *wrapper = GDK_DRAWABLE_IMPL_X11 (drawable)->wrapper;
1375 GDK_DRAWABLE_CLASS (_gdk_drawable_impl_x11_parent_class)->draw_pixbuf (wrapper, gc, pixbuf,
1376 src_x, src_y, dest_x, dest_y,
1378 dither, x_dither, y_dither);
1382 gdk_x11_drawable_update_picture_clip (drawable, gc);
1384 rowstride = gdk_pixbuf_get_rowstride (pixbuf);
1389 if (!draw_with_pixmaps (drawable, gc,
1390 format_type, format, mask_format,
1391 gdk_pixbuf_get_pixels (pixbuf) + src_y * rowstride + src_x * 4,
1393 dest_x, dest_y, width, height))
1394 use_pixmaps = FALSE;
1398 #endif /* USE_SHM */
1399 draw_with_images (drawable, gc,
1400 format_type, format, mask_format,
1401 gdk_pixbuf_get_pixels (pixbuf) + src_y * rowstride + src_x * 4,
1403 dest_x, dest_y, width, height);
1407 gdk_x11_cairo_surface_destroy (void *data)
1409 GdkDrawableImplX11 *impl = data;
1411 impl->cairo_surface = NULL;
1415 _gdk_windowing_set_cairo_surface_size (cairo_surface_t *surface,
1419 cairo_xlib_surface_set_size (surface, width, height);
1423 _gdk_windowing_create_cairo_surface (GdkDrawable *drawable,
1427 GdkDrawableImplX11 *impl = GDK_DRAWABLE_IMPL_X11 (drawable);
1430 visual = gdk_drawable_get_visual (drawable);
1432 return cairo_xlib_surface_create (GDK_SCREEN_XDISPLAY (impl->screen),
1434 GDK_VISUAL_XVISUAL (visual),
1436 else if (gdk_drawable_get_depth (drawable) == 1)
1437 return cairo_xlib_surface_create_for_bitmap (GDK_SCREEN_XDISPLAY (impl->screen),
1439 GDK_SCREEN_XSCREEN (impl->screen),
1443 g_warning ("Using Cairo rendering requires the drawable argument to\n"
1444 "have a specified colormap. All windows have a colormap,\n"
1445 "however, pixmaps only have colormap by default if they\n"
1446 "were created with a non-NULL window argument. Otherwise\n"
1447 "a colormap must be set on them with gdk_drawable_set_colormap");
1453 static cairo_surface_t *
1454 gdk_x11_ref_cairo_surface (GdkDrawable *drawable)
1456 GdkDrawableImplX11 *impl = GDK_DRAWABLE_IMPL_X11 (drawable);
1458 if (GDK_IS_WINDOW_IMPL_X11 (drawable) &&
1459 GDK_WINDOW_DESTROYED (impl->wrapper))
1462 if (!impl->cairo_surface)
1466 gdk_drawable_get_size (drawable, &width, &height);
1468 impl->cairo_surface = _gdk_windowing_create_cairo_surface (drawable, width, height);
1470 if (impl->cairo_surface)
1471 cairo_surface_set_user_data (impl->cairo_surface, &gdk_x11_cairo_key,
1472 drawable, gdk_x11_cairo_surface_destroy);
1475 cairo_surface_reference (impl->cairo_surface);
1477 return impl->cairo_surface;