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/.
30 #include "gdkregion-generic.h"
32 #include <cairo-xlib.h>
35 #include <string.h> /* for memcpy() */
37 #if defined (HAVE_IPC_H) && defined (HAVE_SHM_H) && defined (HAVE_XSHM_H)
42 #include <X11/extensions/XShm.h>
45 #include "gdkprivate-x11.h"
46 #include "gdkdrawable-x11.h"
47 #include "gdkpixmap-x11.h"
48 #include "gdkscreen-x11.h"
49 #include "gdkdisplay-x11.h"
53 static void gdk_x11_draw_rectangle (GdkDrawable *drawable,
60 static void gdk_x11_draw_arc (GdkDrawable *drawable,
69 static void gdk_x11_draw_polygon (GdkDrawable *drawable,
74 static void gdk_x11_draw_text (GdkDrawable *drawable,
81 static void gdk_x11_draw_text_wc (GdkDrawable *drawable,
88 static void gdk_x11_draw_drawable (GdkDrawable *drawable,
97 static void gdk_x11_draw_points (GdkDrawable *drawable,
101 static void gdk_x11_draw_segments (GdkDrawable *drawable,
105 static void gdk_x11_draw_lines (GdkDrawable *drawable,
110 static void gdk_x11_draw_image (GdkDrawable *drawable,
119 static void gdk_x11_draw_pixbuf (GdkDrawable *drawable,
132 static cairo_surface_t *gdk_x11_ref_cairo_surface (GdkDrawable *drawable);
134 static void gdk_x11_set_colormap (GdkDrawable *drawable,
135 GdkColormap *colormap);
137 static GdkColormap* gdk_x11_get_colormap (GdkDrawable *drawable);
138 static gint gdk_x11_get_depth (GdkDrawable *drawable);
139 static GdkScreen * gdk_x11_get_screen (GdkDrawable *drawable);
140 static GdkVisual* gdk_x11_get_visual (GdkDrawable *drawable);
142 static void gdk_drawable_impl_x11_finalize (GObject *object);
144 static const cairo_user_data_key_t gdk_x11_cairo_key;
146 G_DEFINE_TYPE (GdkDrawableImplX11, _gdk_drawable_impl_x11, GDK_TYPE_DRAWABLE)
149 _gdk_drawable_impl_x11_class_init (GdkDrawableImplX11Class *klass)
151 GdkDrawableClass *drawable_class = GDK_DRAWABLE_CLASS (klass);
152 GObjectClass *object_class = G_OBJECT_CLASS (klass);
154 object_class->finalize = gdk_drawable_impl_x11_finalize;
156 drawable_class->create_gc = _gdk_x11_gc_new;
157 drawable_class->draw_rectangle = gdk_x11_draw_rectangle;
158 drawable_class->draw_arc = gdk_x11_draw_arc;
159 drawable_class->draw_polygon = gdk_x11_draw_polygon;
160 drawable_class->draw_text = gdk_x11_draw_text;
161 drawable_class->draw_text_wc = gdk_x11_draw_text_wc;
162 drawable_class->draw_drawable = gdk_x11_draw_drawable;
163 drawable_class->draw_points = gdk_x11_draw_points;
164 drawable_class->draw_segments = gdk_x11_draw_segments;
165 drawable_class->draw_lines = gdk_x11_draw_lines;
166 drawable_class->draw_image = gdk_x11_draw_image;
167 drawable_class->draw_pixbuf = gdk_x11_draw_pixbuf;
169 drawable_class->ref_cairo_surface = gdk_x11_ref_cairo_surface;
171 drawable_class->set_colormap = gdk_x11_set_colormap;
172 drawable_class->get_colormap = gdk_x11_get_colormap;
174 drawable_class->get_depth = gdk_x11_get_depth;
175 drawable_class->get_screen = gdk_x11_get_screen;
176 drawable_class->get_visual = gdk_x11_get_visual;
178 drawable_class->_copy_to_image = _gdk_x11_copy_to_image;
182 _gdk_drawable_impl_x11_init (GdkDrawableImplX11 *impl)
187 gdk_drawable_impl_x11_finalize (GObject *object)
189 gdk_drawable_set_colormap (GDK_DRAWABLE (object), NULL);
191 G_OBJECT_CLASS (_gdk_drawable_impl_x11_parent_class)->finalize (object);
195 * _gdk_x11_drawable_finish:
196 * @drawable: a #GdkDrawableImplX11.
198 * Performs necessary cleanup prior to freeing a pixmap or
199 * destroying a window.
202 _gdk_x11_drawable_finish (GdkDrawable *drawable)
204 GdkDrawableImplX11 *impl = GDK_DRAWABLE_IMPL_X11 (drawable);
208 XRenderFreePicture (GDK_SCREEN_XDISPLAY (impl->screen),
210 impl->picture = None;
213 if (impl->cairo_surface)
215 cairo_surface_finish (impl->cairo_surface);
216 cairo_surface_set_user_data (impl->cairo_surface, &gdk_x11_cairo_key,
222 * _gdk_x11_drawable_update_size:
223 * @drawable: a #GdkDrawableImplX11.
225 * Updates the state of the drawable (in particular the drawable's
226 * cairo surface) when its size has changed.
229 _gdk_x11_drawable_update_size (GdkDrawable *drawable)
231 GdkDrawableImplX11 *impl = GDK_DRAWABLE_IMPL_X11 (drawable);
233 if (impl->cairo_surface)
237 gdk_drawable_get_size (drawable, &width, &height);
238 cairo_xlib_surface_set_size (impl->cairo_surface, width, height);
243 try_pixmap (Display *xdisplay,
247 Pixmap pixmap = XCreatePixmap (xdisplay,
248 RootWindow (xdisplay, screen),
250 XFreePixmap (xdisplay, pixmap);
254 _gdk_x11_have_render (GdkDisplay *display)
256 Display *xdisplay = GDK_DISPLAY_XDISPLAY (display);
257 GdkDisplayX11 *x11display = GDK_DISPLAY_X11 (display);
259 if (x11display->have_render == GDK_UNKNOWN)
261 int event_base, error_base;
262 x11display->have_render =
263 XRenderQueryExtension (xdisplay, &event_base, &error_base)
266 if (x11display->have_render == GDK_YES)
269 * Sun advertises RENDER, but fails to support 32-bit pixmaps.
270 * That is just no good. Therefore, we check all screens
271 * for proper support.
275 for (screen = 0; screen < ScreenCount (xdisplay); screen++)
278 int *depths = XListDepths (xdisplay, screen, &count);
279 gboolean has_8 = FALSE, has_32 = FALSE;
285 for (i = 0; i < count; i++)
289 else if (depths[i] == 32)
295 /* At this point, we might have a false positive;
296 * buggy versions of Xinerama only report depths for
297 * which there is an associated visual; so we actually
298 * go ahead and try create pixmaps.
300 if (!(has_8 && has_32))
302 gdk_error_trap_push ();
304 try_pixmap (xdisplay, screen, 8);
306 try_pixmap (xdisplay, screen, 32);
307 XSync (xdisplay, False);
308 if (gdk_error_trap_pop () == 0)
315 if (!(has_8 && has_32))
317 g_warning ("The X server advertises that RENDER support is present,\n"
318 "but fails to supply the necessary pixmap support. In\n"
319 "other words, it is buggy.");
320 x11display->have_render = GDK_NO;
327 return x11display->have_render == GDK_YES;
331 gdk_x11_drawable_get_picture (GdkDrawable *drawable)
333 GdkDrawableImplX11 *impl = GDK_DRAWABLE_IMPL_X11 (drawable);
337 Display *xdisplay = GDK_SCREEN_XDISPLAY (impl->screen);
338 XRenderPictFormat *format;
340 GdkVisual *visual = gdk_drawable_get_visual (GDK_DRAWABLE_IMPL_X11 (drawable)->wrapper);
344 format = XRenderFindVisualFormat (xdisplay, GDK_VISUAL_XVISUAL (visual));
347 XRenderPictureAttributes attributes;
348 attributes.graphics_exposures = False;
350 impl->picture = XRenderCreatePicture (xdisplay, impl->xid, format,
351 CPGraphicsExposure, &attributes);
355 return impl->picture;
359 gdk_x11_drawable_update_picture_clip (GdkDrawable *drawable,
362 GdkDrawableImplX11 *impl = GDK_DRAWABLE_IMPL_X11 (drawable);
363 Display *xdisplay = GDK_SCREEN_XDISPLAY (impl->screen);
364 Picture picture = gdk_x11_drawable_get_picture (drawable);
365 GdkRegion *clip_region = gc ? _gdk_gc_get_clip_region (gc) : NULL;
369 GdkRegionBox *boxes = clip_region->rects;
370 gint n_boxes = clip_region->numRects;
371 XRectangle *rects = g_new (XRectangle, n_boxes);
374 for (i=0; i < n_boxes; i++)
376 rects[i].x = CLAMP (boxes[i].x1 + gc->clip_x_origin, G_MINSHORT, G_MAXSHORT);
377 rects[i].y = CLAMP (boxes[i].y1 + gc->clip_y_origin, G_MINSHORT, G_MAXSHORT);
378 rects[i].width = CLAMP (boxes[i].x2 + gc->clip_x_origin, G_MINSHORT, G_MAXSHORT) - rects[i].x;
379 rects[i].height = CLAMP (boxes[i].y2 + gc->clip_y_origin, G_MINSHORT, G_MAXSHORT) - rects[i].y;
382 XRenderSetPictureClipRectangles (xdisplay, picture,
383 0, 0, rects, n_boxes);
389 XRenderPictureAttributes pa;
391 XRenderChangePicture (xdisplay, picture,
396 /*****************************************************
397 * X11 specific implementations of generic functions *
398 *****************************************************/
401 gdk_x11_get_colormap (GdkDrawable *drawable)
403 GdkDrawableImplX11 *impl;
405 impl = GDK_DRAWABLE_IMPL_X11 (drawable);
407 return impl->colormap;
411 gdk_x11_set_colormap (GdkDrawable *drawable,
412 GdkColormap *colormap)
414 GdkDrawableImplX11 *impl;
416 impl = GDK_DRAWABLE_IMPL_X11 (drawable);
418 if (impl->colormap == colormap)
422 g_object_unref (impl->colormap);
423 impl->colormap = colormap;
425 g_object_ref (impl->colormap);
432 gdk_x11_draw_rectangle (GdkDrawable *drawable,
440 GdkDrawableImplX11 *impl;
442 impl = GDK_DRAWABLE_IMPL_X11 (drawable);
445 XFillRectangle (GDK_SCREEN_XDISPLAY (impl->screen), impl->xid,
446 GDK_GC_GET_XGC (gc), x, y, width, height);
448 XDrawRectangle (GDK_SCREEN_XDISPLAY (impl->screen), impl->xid,
449 GDK_GC_GET_XGC (gc), x, y, width, height);
453 gdk_x11_draw_arc (GdkDrawable *drawable,
463 GdkDrawableImplX11 *impl;
465 impl = GDK_DRAWABLE_IMPL_X11 (drawable);
469 XFillArc (GDK_SCREEN_XDISPLAY (impl->screen), impl->xid,
470 GDK_GC_GET_XGC (gc), x, y, width, height, angle1, angle2);
472 XDrawArc (GDK_SCREEN_XDISPLAY (impl->screen), impl->xid,
473 GDK_GC_GET_XGC (gc), x, y, width, height, angle1, angle2);
477 gdk_x11_draw_polygon (GdkDrawable *drawable,
485 GdkDrawableImplX11 *impl;
487 impl = GDK_DRAWABLE_IMPL_X11 (drawable);
491 (points[0].x != points[npoints-1].x || points[0].y != points[npoints-1].y))
493 tmp_npoints = npoints + 1;
494 tmp_points = g_new (XPoint, tmp_npoints);
495 tmp_points[npoints].x = points[0].x;
496 tmp_points[npoints].y = points[0].y;
500 tmp_npoints = npoints;
501 tmp_points = g_new (XPoint, tmp_npoints);
504 for (i=0; i<npoints; i++)
506 tmp_points[i].x = points[i].x;
507 tmp_points[i].y = points[i].y;
511 XFillPolygon (GDK_SCREEN_XDISPLAY (impl->screen), impl->xid,
512 GDK_GC_GET_XGC (gc), tmp_points, tmp_npoints, Complex, CoordModeOrigin);
514 XDrawLines (GDK_SCREEN_XDISPLAY (impl->screen), impl->xid,
515 GDK_GC_GET_XGC (gc), tmp_points, tmp_npoints, CoordModeOrigin);
522 * Modified by Li-Da Lho to draw 16 bits and Multibyte strings
524 * Interface changed: add "GdkFont *font" to specify font or fontset explicitely
527 gdk_x11_draw_text (GdkDrawable *drawable,
535 GdkDrawableImplX11 *impl;
538 impl = GDK_DRAWABLE_IMPL_X11 (drawable);
539 xdisplay = GDK_SCREEN_XDISPLAY (impl->screen);
541 if (font->type == GDK_FONT_FONT)
543 XFontStruct *xfont = (XFontStruct *) GDK_FONT_XFONT (font);
544 XSetFont(xdisplay, GDK_GC_GET_XGC (gc), xfont->fid);
545 if ((xfont->min_byte1 == 0) && (xfont->max_byte1 == 0))
547 XDrawString (xdisplay, impl->xid,
548 GDK_GC_GET_XGC (gc), x, y, text, text_length);
552 XDrawString16 (xdisplay, impl->xid,
553 GDK_GC_GET_XGC (gc), x, y, (XChar2b *) text, text_length / 2);
556 else if (font->type == GDK_FONT_FONTSET)
558 XFontSet fontset = (XFontSet) GDK_FONT_XFONT (font);
559 XmbDrawString (xdisplay, impl->xid,
560 fontset, GDK_GC_GET_XGC (gc), x, y, text, text_length);
563 g_error("undefined font type\n");
567 gdk_x11_draw_text_wc (GdkDrawable *drawable,
572 const GdkWChar *text,
575 GdkDrawableImplX11 *impl;
578 impl = GDK_DRAWABLE_IMPL_X11 (drawable);
579 xdisplay = GDK_SCREEN_XDISPLAY (impl->screen);
581 if (font->type == GDK_FONT_FONT)
583 XFontStruct *xfont = (XFontStruct *) GDK_FONT_XFONT (font);
586 XSetFont(xdisplay, GDK_GC_GET_XGC (gc), xfont->fid);
587 text_8bit = g_new (gchar, text_length);
588 for (i=0; i<text_length; i++) text_8bit[i] = text[i];
589 XDrawString (xdisplay, impl->xid,
590 GDK_GC_GET_XGC (gc), x, y, text_8bit, text_length);
593 else if (font->type == GDK_FONT_FONTSET)
595 if (sizeof(GdkWChar) == sizeof(wchar_t))
597 XwcDrawString (xdisplay, impl->xid,
598 (XFontSet) GDK_FONT_XFONT (font),
599 GDK_GC_GET_XGC (gc), x, y, (wchar_t *)text, text_length);
605 text_wchar = g_new (wchar_t, text_length);
606 for (i=0; i<text_length; i++) text_wchar[i] = text[i];
607 XwcDrawString (xdisplay, impl->xid,
608 (XFontSet) GDK_FONT_XFONT (font),
609 GDK_GC_GET_XGC (gc), x, y, text_wchar, text_length);
614 g_error("undefined font type\n");
618 gdk_x11_draw_drawable (GdkDrawable *drawable,
628 int src_depth = gdk_drawable_get_depth (src);
629 int dest_depth = gdk_drawable_get_depth (drawable);
630 GdkDrawableImplX11 *impl;
631 GdkDrawableImplX11 *src_impl;
633 impl = GDK_DRAWABLE_IMPL_X11 (drawable);
635 if (GDK_IS_DRAWABLE_IMPL_X11 (src))
636 src_impl = GDK_DRAWABLE_IMPL_X11 (src);
637 else if (GDK_IS_WINDOW (src))
638 src_impl = GDK_DRAWABLE_IMPL_X11(((GdkWindowObject *)src)->impl);
640 src_impl = GDK_DRAWABLE_IMPL_X11(((GdkPixmapObject *)src)->impl);
642 if (GDK_IS_WINDOW_IMPL_X11 (impl) &&
643 GDK_IS_PIXMAP_IMPL_X11 (src_impl))
645 GdkPixmapImplX11 *src_pixmap = GDK_PIXMAP_IMPL_X11 (src_impl);
646 /* Work around an Xserver bug where non-visible areas from
647 * a pixmap to a window will clear the window background
648 * in destination areas that are supposed to be clipped out.
649 * This is a problem with client side windows as this means
650 * things may draw outside the virtual windows. This could
651 * also happen for window to window copies, but I don't
652 * think we generate any calls like that.
655 * http://lists.freedesktop.org/archives/xorg/2009-February/043318.html
671 if (xsrc + width > src_pixmap->width)
672 width = src_pixmap->width - xsrc;
673 if (ysrc + height > src_pixmap->height)
674 height = src_pixmap->height - ysrc;
679 XCopyArea (GDK_SCREEN_XDISPLAY (impl->screen),
687 else if (dest_depth != 0 && src_depth == dest_depth)
689 XCopyArea (GDK_SCREEN_XDISPLAY (impl->screen),
698 g_warning ("Attempt to draw a drawable with depth %d to a drawable with depth %d",
699 src_depth, dest_depth);
703 gdk_x11_draw_points (GdkDrawable *drawable,
708 GdkDrawableImplX11 *impl;
710 impl = GDK_DRAWABLE_IMPL_X11 (drawable);
713 /* We special-case npoints == 1, because X will merge multiple
714 * consecutive XDrawPoint requests into a PolyPoint request
718 XDrawPoint (GDK_SCREEN_XDISPLAY (impl->screen),
721 points[0].x, points[0].y);
726 XPoint *tmp_points = g_new (XPoint, npoints);
728 for (i=0; i<npoints; i++)
730 tmp_points[i].x = points[i].x;
731 tmp_points[i].y = points[i].y;
734 XDrawPoints (GDK_SCREEN_XDISPLAY (impl->screen),
746 gdk_x11_draw_segments (GdkDrawable *drawable,
751 GdkDrawableImplX11 *impl;
753 impl = GDK_DRAWABLE_IMPL_X11 (drawable);
756 /* We special-case nsegs == 1, because X will merge multiple
757 * consecutive XDrawLine requests into a PolySegment request
761 XDrawLine (GDK_SCREEN_XDISPLAY (impl->screen), impl->xid,
762 GDK_GC_GET_XGC (gc), segs[0].x1, segs[0].y1,
763 segs[0].x2, segs[0].y2);
768 XSegment *tmp_segs = g_new (XSegment, nsegs);
770 for (i=0; i<nsegs; i++)
772 tmp_segs[i].x1 = segs[i].x1;
773 tmp_segs[i].x2 = segs[i].x2;
774 tmp_segs[i].y1 = segs[i].y1;
775 tmp_segs[i].y2 = segs[i].y2;
778 XDrawSegments (GDK_SCREEN_XDISPLAY (impl->screen),
788 gdk_x11_draw_lines (GdkDrawable *drawable,
794 XPoint *tmp_points = g_new (XPoint, npoints);
795 GdkDrawableImplX11 *impl;
797 impl = GDK_DRAWABLE_IMPL_X11 (drawable);
800 for (i=0; i<npoints; i++)
802 tmp_points[i].x = points[i].x;
803 tmp_points[i].y = points[i].y;
806 XDrawLines (GDK_SCREEN_XDISPLAY (impl->screen),
816 gdk_x11_draw_image (GdkDrawable *drawable,
826 GdkDrawableImplX11 *impl;
828 impl = GDK_DRAWABLE_IMPL_X11 (drawable);
831 if (image->type == GDK_IMAGE_SHARED)
832 XShmPutImage (GDK_SCREEN_XDISPLAY (impl->screen), impl->xid,
833 GDK_GC_GET_XGC (gc), GDK_IMAGE_XIMAGE (image),
834 xsrc, ysrc, xdest, ydest, width, height, False);
837 XPutImage (GDK_SCREEN_XDISPLAY (impl->screen), impl->xid,
838 GDK_GC_GET_XGC (gc), GDK_IMAGE_XIMAGE (image),
839 xsrc, ysrc, xdest, ydest, width, height);
843 gdk_x11_get_depth (GdkDrawable *drawable)
845 /* This is a bit bogus but I'm not sure the other way is better */
847 return gdk_drawable_get_depth (GDK_DRAWABLE_IMPL_X11 (drawable)->wrapper);
851 get_impl_drawable (GdkDrawable *drawable)
853 if (GDK_IS_WINDOW (drawable))
854 return ((GdkWindowObject *)drawable)->impl;
855 else if (GDK_IS_PIXMAP (drawable))
856 return ((GdkPixmapObject *)drawable)->impl;
859 g_warning (G_STRLOC " drawable is not a pixmap or window");
865 gdk_x11_get_screen (GdkDrawable *drawable)
867 if (GDK_IS_DRAWABLE_IMPL_X11 (drawable))
868 return GDK_DRAWABLE_IMPL_X11 (drawable)->screen;
870 return GDK_DRAWABLE_IMPL_X11 (get_impl_drawable (drawable))->screen;
874 gdk_x11_get_visual (GdkDrawable *drawable)
876 return gdk_drawable_get_visual (GDK_DRAWABLE_IMPL_X11 (drawable)->wrapper);
880 * gdk_x11_drawable_get_xdisplay:
881 * @drawable: a #GdkDrawable.
883 * Returns the display of a #GdkDrawable.
885 * Return value: an Xlib <type>Display*</type>.
888 gdk_x11_drawable_get_xdisplay (GdkDrawable *drawable)
890 if (GDK_IS_DRAWABLE_IMPL_X11 (drawable))
891 return GDK_SCREEN_XDISPLAY (GDK_DRAWABLE_IMPL_X11 (drawable)->screen);
893 return GDK_SCREEN_XDISPLAY (GDK_DRAWABLE_IMPL_X11 (get_impl_drawable (drawable))->screen);
897 * gdk_x11_drawable_get_xid:
898 * @drawable: a #GdkDrawable.
900 * Returns the X resource (window or pixmap) belonging to a #GdkDrawable.
902 * Return value: the ID of @drawable's X resource.
905 gdk_x11_drawable_get_xid (GdkDrawable *drawable)
909 if (GDK_IS_WINDOW (drawable))
911 GdkWindow *window = (GdkWindow *)drawable;
913 /* Try to ensure the window has a native window */
914 if (!_gdk_window_has_impl (window))
916 gdk_window_set_has_native (window, TRUE);
918 /* We sync here to ensure the window is created in the Xserver when
919 * this function returns. This is required because the returned XID
920 * for this window must be valid immediately, even with another
921 * connection to the Xserver */
922 gdk_display_sync (gdk_drawable_get_display (window));
925 if (!GDK_WINDOW_IS_X11 (window))
927 g_warning (G_STRLOC " drawable is not a native X11 window");
931 impl = ((GdkWindowObject *)drawable)->impl;
933 else if (GDK_IS_PIXMAP (drawable))
934 impl = ((GdkPixmapObject *)drawable)->impl;
937 g_warning (G_STRLOC " drawable is not a pixmap or window");
941 return ((GdkDrawableImplX11 *)impl)->xid;
944 /* Code for accelerated alpha compositing using the RENDER extension.
945 * It's a bit long because there are lots of possibilities for
946 * what's the fastest depending on the available picture formats,
947 * whether we can used shared pixmaps, etc.
950 static GdkX11FormatType
951 select_format (GdkDisplay *display,
952 XRenderPictFormat **format,
953 XRenderPictFormat **mask)
955 Display *xdisplay = GDK_DISPLAY_XDISPLAY (display);
956 XRenderPictFormat pf;
958 if (!_gdk_x11_have_render (display))
959 return GDK_X11_FORMAT_NONE;
961 /* Look for a 32-bit xRGB and Axxx formats that exactly match the
962 * in memory data format. We can use them as pixmap and mask
963 * to deal with non-premultiplied data.
966 pf.type = PictTypeDirect;
968 pf.direct.redMask = 0xff;
969 pf.direct.greenMask = 0xff;
970 pf.direct.blueMask = 0xff;
972 pf.direct.alphaMask = 0;
973 if (ImageByteOrder (xdisplay) == LSBFirst)
984 pf.direct.green = 16;
988 *format = XRenderFindFormat (xdisplay,
989 (PictFormatType | PictFormatDepth |
990 PictFormatRedMask | PictFormatRed |
991 PictFormatGreenMask | PictFormatGreen |
992 PictFormatBlueMask | PictFormatBlue |
993 PictFormatAlphaMask),
997 pf.direct.alphaMask = 0xff;
998 if (ImageByteOrder (xdisplay) == LSBFirst)
1001 pf.direct.alpha = 24;
1005 pf.direct.alpha = 0;
1008 *mask = XRenderFindFormat (xdisplay,
1009 (PictFormatType | PictFormatDepth |
1010 PictFormatAlphaMask | PictFormatAlpha),
1014 if (*format && *mask)
1015 return GDK_X11_FORMAT_EXACT_MASK;
1017 /* OK, that failed, now look for xRGB and Axxx formats in
1018 * RENDER's preferred order
1020 pf.direct.alphaMask = 0;
1023 pf.direct.green = 8;
1026 *format = XRenderFindFormat (xdisplay,
1027 (PictFormatType | PictFormatDepth |
1028 PictFormatRedMask | PictFormatRed |
1029 PictFormatGreenMask | PictFormatGreen |
1030 PictFormatBlueMask | PictFormatBlue |
1031 PictFormatAlphaMask),
1035 pf.direct.alphaMask = 0xff;
1036 pf.direct.alpha = 24;
1038 *mask = XRenderFindFormat (xdisplay,
1039 (PictFormatType | PictFormatDepth |
1040 PictFormatAlphaMask | PictFormatAlpha),
1044 if (*format && *mask)
1045 return GDK_X11_FORMAT_ARGB_MASK;
1047 /* Finally, if neither of the above worked, fall back to
1048 * looking for combined ARGB -- we'll premultiply ourselves.
1051 pf.type = PictTypeDirect;
1054 pf.direct.green = 8;
1056 pf.direct.alphaMask = 0xff;
1057 pf.direct.alpha = 24;
1059 *format = XRenderFindFormat (xdisplay,
1060 (PictFormatType | PictFormatDepth |
1061 PictFormatRedMask | PictFormatRed |
1062 PictFormatGreenMask | PictFormatGreen |
1063 PictFormatBlueMask | PictFormatBlue |
1064 PictFormatAlphaMask | PictFormatAlpha),
1070 return GDK_X11_FORMAT_ARGB;
1072 return GDK_X11_FORMAT_NONE;
1077 list_formats (XRenderPictFormat *pf)
1083 XRenderPictFormat *pf = XRenderFindFormat (impl->xdisplay, 0, NULL, i);
1086 g_print ("%2d R-%#06x/%#06x G-%#06x/%#06x B-%#06x/%#06x A-%#06x/%#06x\n",
1091 pf->direct.greenMask,
1093 pf->direct.blueMask,
1095 pf->direct.alphaMask);
1104 _gdk_x11_convert_to_format (guchar *src_buf,
1107 gint dest_rowstride,
1108 GdkX11FormatType dest_format,
1109 GdkByteOrder dest_byteorder,
1115 for (i=0; i < height; i++)
1117 switch (dest_format)
1119 case GDK_X11_FORMAT_EXACT_MASK:
1121 memcpy (dest_buf + i * dest_rowstride,
1122 src_buf + i * src_rowstride,
1126 case GDK_X11_FORMAT_ARGB_MASK:
1128 guchar *row = src_buf + i * src_rowstride;
1129 if (((gsize)row & 3) != 0)
1132 guint32 *q = (guint32 *)(dest_buf + i * dest_rowstride);
1133 guchar *end = p + 4 * width;
1137 *q = (p[3] << 24) | (p[0] << 16) | (p[1] << 8) | p[2];
1144 guint32 *p = (guint32 *)row;
1145 guint32 *q = (guint32 *)(dest_buf + i * dest_rowstride);
1146 guint32 *end = p + width;
1148 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
1149 if (dest_byteorder == GDK_LSB_FIRST)
1155 *q = ( (*p & 0xff00ff00) |
1156 ((*p & 0x000000ff) << 16) |
1157 ((*p & 0x00ff0000) >> 16));
1168 *q = (((*p & 0xff000000) >> 24) |
1169 ((*p & 0x00ffffff) << 8));
1174 #else /* G_BYTE_ORDER == G_BIG_ENDIAN */
1175 if (dest_byteorder == GDK_LSB_FIRST)
1181 *q = ( (*p & 0x00ff00ff) |
1182 ((*p & 0x0000ff00) << 16) |
1183 ((*p & 0xff000000) >> 16));
1194 *q = (((*p & 0xffffff00) >> 8) |
1195 ((*p & 0x000000ff) << 24));
1200 #endif /* G_BYTE_ORDER*/
1204 case GDK_X11_FORMAT_ARGB:
1206 guchar *p = (src_buf + i * src_rowstride);
1207 guchar *q = (dest_buf + i * dest_rowstride);
1208 guchar *end = p + 4 * width;
1211 #define MULT(d,c,a,t) G_STMT_START { t = c * a; d = ((t >> 8) + t) >> 8; } G_STMT_END
1213 if (dest_byteorder == GDK_LSB_FIRST)
1217 MULT(q[0], p[2], p[3], t1);
1218 MULT(q[1], p[1], p[3], t2);
1219 MULT(q[2], p[0], p[3], t3);
1230 MULT(q[1], p[0], p[3], t1);
1231 MULT(q[2], p[1], p[3], t2);
1232 MULT(q[3], p[2], p[3], t3);
1240 case GDK_X11_FORMAT_NONE:
1241 g_assert_not_reached ();
1248 draw_with_images (GdkDrawable *drawable,
1250 GdkX11FormatType format_type,
1251 XRenderPictFormat *format,
1252 XRenderPictFormat *mask_format,
1260 GdkScreen *screen = GDK_DRAWABLE_IMPL_X11 (drawable)->screen;
1261 Display *xdisplay = GDK_SCREEN_XDISPLAY (screen);
1267 Picture mask = None;
1270 pix = gdk_pixmap_new (gdk_screen_get_root_window (screen), width, height, 32);
1272 pict = XRenderCreatePicture (xdisplay,
1273 GDK_PIXMAP_XID (pix),
1276 mask = XRenderCreatePicture (xdisplay,
1277 GDK_PIXMAP_XID (pix),
1278 mask_format, 0, NULL);
1280 dest_pict = gdk_x11_drawable_get_picture (drawable);
1282 pix_gc = _gdk_drawable_get_scratch_gc (pix, FALSE);
1284 for (y0 = 0; y0 < height; y0 += GDK_SCRATCH_IMAGE_HEIGHT)
1286 gint height1 = MIN (height - y0, GDK_SCRATCH_IMAGE_HEIGHT);
1287 for (x0 = 0; x0 < width; x0 += GDK_SCRATCH_IMAGE_WIDTH)
1291 gint width1 = MIN (width - x0, GDK_SCRATCH_IMAGE_WIDTH);
1293 image = _gdk_image_get_scratch (screen, width1, height1, 32, &xs0, &ys0);
1295 _gdk_x11_convert_to_format (src_rgb + y0 * src_rowstride + 4 * x0, src_rowstride,
1296 (guchar *)image->mem + ys0 * image->bpl + xs0 * image->bpp, image->bpl,
1297 format_type, image->byte_order,
1300 gdk_draw_image (pix, pix_gc,
1301 image, xs0, ys0, x0, y0, width1, height1);
1305 XRenderComposite (xdisplay, PictOpOver, pict, mask, dest_pict,
1306 0, 0, 0, 0, dest_x, dest_y, width, height);
1308 XRenderFreePicture (xdisplay, pict);
1310 XRenderFreePicture (xdisplay, mask);
1312 g_object_unref (pix);
1315 typedef struct _ShmPixmapInfo ShmPixmapInfo;
1317 struct _ShmPixmapInfo
1326 shm_pixmap_info_destroy (gpointer data)
1328 ShmPixmapInfo *info = data;
1330 if (info->pict != None)
1331 XRenderFreePicture (info->display, info->pict);
1332 if (info->mask != None)
1333 XRenderFreePicture (info->display, info->mask);
1339 /* Returns FALSE if we can't get a shm pixmap */
1341 get_shm_pixmap_for_image (Display *xdisplay,
1343 XRenderPictFormat *format,
1344 XRenderPictFormat *mask_format,
1349 ShmPixmapInfo *info;
1351 if (image->type != GDK_IMAGE_SHARED)
1354 info = g_object_get_data (G_OBJECT (image), "gdk-x11-shm-pixmap");
1357 *pix = _gdk_x11_image_get_shm_pixmap (image);
1362 info = g_new (ShmPixmapInfo, 1);
1363 info->display = xdisplay;
1366 info->pict = XRenderCreatePicture (xdisplay, info->pix,
1369 info->mask = XRenderCreatePicture (xdisplay, info->pix,
1370 mask_format, 0, NULL);
1374 g_object_set_data_full (G_OBJECT (image), "gdk-x11-shm-pixmap", info,
1375 shm_pixmap_info_destroy);
1386 /* Returns FALSE if drawing with ShmPixmaps is not possible */
1388 draw_with_pixmaps (GdkDrawable *drawable,
1390 GdkX11FormatType format_type,
1391 XRenderPictFormat *format,
1392 XRenderPictFormat *mask_format,
1400 Display *xdisplay = GDK_SCREEN_XDISPLAY (GDK_DRAWABLE_IMPL_X11 (drawable)->screen);
1405 Picture mask = None;
1408 dest_pict = gdk_x11_drawable_get_picture (drawable);
1410 for (y0 = 0; y0 < height; y0 += GDK_SCRATCH_IMAGE_HEIGHT)
1412 gint height1 = MIN (height - y0, GDK_SCRATCH_IMAGE_HEIGHT);
1413 for (x0 = 0; x0 < width; x0 += GDK_SCRATCH_IMAGE_WIDTH)
1417 gint width1 = MIN (width - x0, GDK_SCRATCH_IMAGE_WIDTH);
1419 image = _gdk_image_get_scratch (GDK_DRAWABLE_IMPL_X11 (drawable)->screen,
1420 width1, height1, 32, &xs0, &ys0);
1421 if (!get_shm_pixmap_for_image (xdisplay, image, format, mask_format, &pix, &pict, &mask))
1424 _gdk_x11_convert_to_format (src_rgb + y0 * src_rowstride + 4 * x0, src_rowstride,
1425 (guchar *)image->mem + ys0 * image->bpl + xs0 * image->bpp, image->bpl,
1426 format_type, image->byte_order,
1429 XRenderComposite (xdisplay, PictOpOver, pict, mask, dest_pict,
1430 xs0, ys0, xs0, ys0, x0 + dest_x, y0 + dest_y,
1440 gdk_x11_draw_pixbuf (GdkDrawable *drawable,
1449 GdkRgbDither dither,
1453 GdkX11FormatType format_type;
1454 XRenderPictFormat *format, *mask_format;
1457 gboolean use_pixmaps = TRUE;
1458 #endif /* USE_SHM */
1460 format_type = select_format (gdk_drawable_get_display (drawable),
1461 &format, &mask_format);
1463 if (format_type == GDK_X11_FORMAT_NONE ||
1464 !gdk_pixbuf_get_has_alpha (pixbuf) ||
1465 gdk_drawable_get_depth (drawable) == 1 ||
1466 (dither == GDK_RGB_DITHER_MAX && gdk_drawable_get_depth (drawable) != 24) ||
1467 gdk_x11_drawable_get_picture (drawable) == None)
1469 GdkDrawable *wrapper = GDK_DRAWABLE_IMPL_X11 (drawable)->wrapper;
1470 GDK_DRAWABLE_CLASS (_gdk_drawable_impl_x11_parent_class)->draw_pixbuf (wrapper, gc, pixbuf,
1471 src_x, src_y, dest_x, dest_y,
1473 dither, x_dither, y_dither);
1477 gdk_x11_drawable_update_picture_clip (drawable, gc);
1479 rowstride = gdk_pixbuf_get_rowstride (pixbuf);
1484 if (!draw_with_pixmaps (drawable, gc,
1485 format_type, format, mask_format,
1486 gdk_pixbuf_get_pixels (pixbuf) + src_y * rowstride + src_x * 4,
1488 dest_x, dest_y, width, height))
1489 use_pixmaps = FALSE;
1493 #endif /* USE_SHM */
1494 draw_with_images (drawable, gc,
1495 format_type, format, mask_format,
1496 gdk_pixbuf_get_pixels (pixbuf) + src_y * rowstride + src_x * 4,
1498 dest_x, dest_y, width, height);
1502 gdk_x11_cairo_surface_destroy (void *data)
1504 GdkDrawableImplX11 *impl = data;
1506 impl->cairo_surface = NULL;
1510 _gdk_windowing_set_cairo_surface_size (cairo_surface_t *surface,
1514 cairo_xlib_surface_set_size (surface, width, height);
1518 _gdk_windowing_create_cairo_surface (GdkDrawable *drawable,
1522 GdkDrawableImplX11 *impl = GDK_DRAWABLE_IMPL_X11 (drawable);
1525 visual = gdk_drawable_get_visual (drawable);
1527 return cairo_xlib_surface_create (GDK_SCREEN_XDISPLAY (impl->screen),
1529 GDK_VISUAL_XVISUAL (visual),
1531 else if (gdk_drawable_get_depth (drawable) == 1)
1532 return cairo_xlib_surface_create_for_bitmap (GDK_SCREEN_XDISPLAY (impl->screen),
1534 GDK_SCREEN_XSCREEN (impl->screen),
1538 g_warning ("Using Cairo rendering requires the drawable argument to\n"
1539 "have a specified colormap. All windows have a colormap,\n"
1540 "however, pixmaps only have colormap by default if they\n"
1541 "were created with a non-NULL window argument. Otherwise\n"
1542 "a colormap must be set on them with gdk_drawable_set_colormap");
1548 static cairo_surface_t *
1549 gdk_x11_ref_cairo_surface (GdkDrawable *drawable)
1551 GdkDrawableImplX11 *impl = GDK_DRAWABLE_IMPL_X11 (drawable);
1553 if (GDK_IS_WINDOW_IMPL_X11 (drawable) &&
1554 GDK_WINDOW_DESTROYED (impl->wrapper))
1557 if (!impl->cairo_surface)
1561 gdk_drawable_get_size (drawable, &width, &height);
1563 impl->cairo_surface = _gdk_windowing_create_cairo_surface (drawable, width, height);
1565 if (impl->cairo_surface)
1566 cairo_surface_set_user_data (impl->cairo_surface, &gdk_x11_cairo_key,
1567 drawable, gdk_x11_cairo_surface_destroy);
1570 cairo_surface_reference (impl->cairo_surface);
1572 return impl->cairo_surface;
1575 #define __GDK_DRAWABLE_X11_C__
1576 #include "gdkaliasdef.c"