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);
642 XCopyArea (GDK_SCREEN_XDISPLAY (impl->screen),
643 src_impl ? src_impl->xid : GDK_DRAWABLE_XID (src),
650 else if (dest_depth != 0 && src_depth == dest_depth)
652 XCopyArea (GDK_SCREEN_XDISPLAY (impl->screen),
653 src_impl ? src_impl->xid : GDK_DRAWABLE_XID (src),
661 g_warning ("Attempt to draw a drawable with depth %d to a drawable with depth %d",
662 src_depth, dest_depth);
666 gdk_x11_draw_points (GdkDrawable *drawable,
671 GdkDrawableImplX11 *impl;
673 impl = GDK_DRAWABLE_IMPL_X11 (drawable);
676 /* We special-case npoints == 1, because X will merge multiple
677 * consecutive XDrawPoint requests into a PolyPoint request
681 XDrawPoint (GDK_SCREEN_XDISPLAY (impl->screen),
684 points[0].x, points[0].y);
689 XPoint *tmp_points = g_new (XPoint, npoints);
691 for (i=0; i<npoints; i++)
693 tmp_points[i].x = points[i].x;
694 tmp_points[i].y = points[i].y;
697 XDrawPoints (GDK_SCREEN_XDISPLAY (impl->screen),
709 gdk_x11_draw_segments (GdkDrawable *drawable,
714 GdkDrawableImplX11 *impl;
716 impl = GDK_DRAWABLE_IMPL_X11 (drawable);
719 /* We special-case nsegs == 1, because X will merge multiple
720 * consecutive XDrawLine requests into a PolySegment request
724 XDrawLine (GDK_SCREEN_XDISPLAY (impl->screen), impl->xid,
725 GDK_GC_GET_XGC (gc), segs[0].x1, segs[0].y1,
726 segs[0].x2, segs[0].y2);
731 XSegment *tmp_segs = g_new (XSegment, nsegs);
733 for (i=0; i<nsegs; i++)
735 tmp_segs[i].x1 = segs[i].x1;
736 tmp_segs[i].x2 = segs[i].x2;
737 tmp_segs[i].y1 = segs[i].y1;
738 tmp_segs[i].y2 = segs[i].y2;
741 XDrawSegments (GDK_SCREEN_XDISPLAY (impl->screen),
751 gdk_x11_draw_lines (GdkDrawable *drawable,
757 XPoint *tmp_points = g_new (XPoint, npoints);
758 GdkDrawableImplX11 *impl;
760 impl = GDK_DRAWABLE_IMPL_X11 (drawable);
763 for (i=0; i<npoints; i++)
765 tmp_points[i].x = points[i].x;
766 tmp_points[i].y = points[i].y;
769 XDrawLines (GDK_SCREEN_XDISPLAY (impl->screen),
779 gdk_x11_draw_image (GdkDrawable *drawable,
789 GdkDrawableImplX11 *impl;
791 impl = GDK_DRAWABLE_IMPL_X11 (drawable);
794 if (image->type == GDK_IMAGE_SHARED)
795 XShmPutImage (GDK_SCREEN_XDISPLAY (impl->screen), impl->xid,
796 GDK_GC_GET_XGC (gc), GDK_IMAGE_XIMAGE (image),
797 xsrc, ysrc, xdest, ydest, width, height, False);
800 XPutImage (GDK_SCREEN_XDISPLAY (impl->screen), impl->xid,
801 GDK_GC_GET_XGC (gc), GDK_IMAGE_XIMAGE (image),
802 xsrc, ysrc, xdest, ydest, width, height);
806 gdk_x11_get_depth (GdkDrawable *drawable)
808 /* This is a bit bogus but I'm not sure the other way is better */
810 return gdk_drawable_get_depth (GDK_DRAWABLE_IMPL_X11 (drawable)->wrapper);
814 get_impl_drawable (GdkDrawable *drawable)
816 if (GDK_IS_WINDOW (drawable))
817 return ((GdkWindowObject *)drawable)->impl;
818 else if (GDK_IS_PIXMAP (drawable))
819 return ((GdkPixmapObject *)drawable)->impl;
822 g_warning (G_STRLOC " drawable is not a pixmap or window");
828 gdk_x11_get_screen (GdkDrawable *drawable)
830 if (GDK_IS_DRAWABLE_IMPL_X11 (drawable))
831 return GDK_DRAWABLE_IMPL_X11 (drawable)->screen;
833 return GDK_DRAWABLE_IMPL_X11 (get_impl_drawable (drawable))->screen;
837 gdk_x11_get_visual (GdkDrawable *drawable)
839 return gdk_drawable_get_visual (GDK_DRAWABLE_IMPL_X11 (drawable)->wrapper);
843 * gdk_x11_drawable_get_xdisplay:
844 * @drawable: a #GdkDrawable.
846 * Returns the display of a #GdkDrawable.
848 * Return value: an Xlib <type>Display*</type>.
851 gdk_x11_drawable_get_xdisplay (GdkDrawable *drawable)
853 if (GDK_IS_DRAWABLE_IMPL_X11 (drawable))
854 return GDK_SCREEN_XDISPLAY (GDK_DRAWABLE_IMPL_X11 (drawable)->screen);
856 return GDK_SCREEN_XDISPLAY (GDK_DRAWABLE_IMPL_X11 (get_impl_drawable (drawable))->screen);
860 * gdk_x11_drawable_get_xid:
861 * @drawable: a #GdkDrawable.
863 * Returns the X resource (window or pixmap) belonging to a #GdkDrawable.
865 * Return value: the ID of @drawable's X resource.
868 gdk_x11_drawable_get_xid (GdkDrawable *drawable)
872 if (GDK_IS_WINDOW (drawable))
873 impl = ((GdkPixmapObject *)drawable)->impl;
874 else if (GDK_IS_PIXMAP (drawable))
875 impl = ((GdkPixmapObject *)drawable)->impl;
878 g_warning (G_STRLOC " drawable is not a pixmap or window");
882 return ((GdkDrawableImplX11 *)impl)->xid;
885 /* Code for accelerated alpha compositing using the RENDER extension.
886 * It's a bit long because there are lots of possibilities for
887 * what's the fastest depending on the available picture formats,
888 * whether we can used shared pixmaps, etc.
891 static GdkX11FormatType
892 select_format (GdkDisplay *display,
893 XRenderPictFormat **format,
894 XRenderPictFormat **mask)
896 Display *xdisplay = GDK_DISPLAY_XDISPLAY (display);
897 XRenderPictFormat pf;
899 if (!_gdk_x11_have_render (display))
900 return GDK_X11_FORMAT_NONE;
902 /* Look for a 32-bit xRGB and Axxx formats that exactly match the
903 * in memory data format. We can use them as pixmap and mask
904 * to deal with non-premultiplied data.
907 pf.type = PictTypeDirect;
909 pf.direct.redMask = 0xff;
910 pf.direct.greenMask = 0xff;
911 pf.direct.blueMask = 0xff;
913 pf.direct.alphaMask = 0;
914 if (ImageByteOrder (xdisplay) == LSBFirst)
925 pf.direct.green = 16;
929 *format = XRenderFindFormat (xdisplay,
930 (PictFormatType | PictFormatDepth |
931 PictFormatRedMask | PictFormatRed |
932 PictFormatGreenMask | PictFormatGreen |
933 PictFormatBlueMask | PictFormatBlue |
934 PictFormatAlphaMask),
938 pf.direct.alphaMask = 0xff;
939 if (ImageByteOrder (xdisplay) == LSBFirst)
942 pf.direct.alpha = 24;
949 *mask = XRenderFindFormat (xdisplay,
950 (PictFormatType | PictFormatDepth |
951 PictFormatAlphaMask | PictFormatAlpha),
955 if (*format && *mask)
956 return GDK_X11_FORMAT_EXACT_MASK;
958 /* OK, that failed, now look for xRGB and Axxx formats in
959 * RENDER's preferred order
961 pf.direct.alphaMask = 0;
967 *format = XRenderFindFormat (xdisplay,
968 (PictFormatType | PictFormatDepth |
969 PictFormatRedMask | PictFormatRed |
970 PictFormatGreenMask | PictFormatGreen |
971 PictFormatBlueMask | PictFormatBlue |
972 PictFormatAlphaMask),
976 pf.direct.alphaMask = 0xff;
977 pf.direct.alpha = 24;
979 *mask = XRenderFindFormat (xdisplay,
980 (PictFormatType | PictFormatDepth |
981 PictFormatAlphaMask | PictFormatAlpha),
985 if (*format && *mask)
986 return GDK_X11_FORMAT_ARGB_MASK;
988 /* Finally, if neither of the above worked, fall back to
989 * looking for combined ARGB -- we'll premultiply ourselves.
992 pf.type = PictTypeDirect;
997 pf.direct.alphaMask = 0xff;
998 pf.direct.alpha = 24;
1000 *format = XRenderFindFormat (xdisplay,
1001 (PictFormatType | PictFormatDepth |
1002 PictFormatRedMask | PictFormatRed |
1003 PictFormatGreenMask | PictFormatGreen |
1004 PictFormatBlueMask | PictFormatBlue |
1005 PictFormatAlphaMask | PictFormatAlpha),
1011 return GDK_X11_FORMAT_ARGB;
1013 return GDK_X11_FORMAT_NONE;
1018 list_formats (XRenderPictFormat *pf)
1024 XRenderPictFormat *pf = XRenderFindFormat (impl->xdisplay, 0, NULL, i);
1027 g_print ("%2d R-%#06x/%#06x G-%#06x/%#06x B-%#06x/%#06x A-%#06x/%#06x\n",
1032 pf->direct.greenMask,
1034 pf->direct.blueMask,
1036 pf->direct.alphaMask);
1045 _gdk_x11_convert_to_format (guchar *src_buf,
1048 gint dest_rowstride,
1049 GdkX11FormatType dest_format,
1050 GdkByteOrder dest_byteorder,
1056 for (i=0; i < height; i++)
1058 switch (dest_format)
1060 case GDK_X11_FORMAT_EXACT_MASK:
1062 memcpy (dest_buf + i * dest_rowstride,
1063 src_buf + i * src_rowstride,
1067 case GDK_X11_FORMAT_ARGB_MASK:
1069 guchar *row = src_buf + i * src_rowstride;
1070 if (((gsize)row & 3) != 0)
1073 guint32 *q = (guint32 *)(dest_buf + i * dest_rowstride);
1074 guchar *end = p + 4 * width;
1078 *q = (p[3] << 24) | (p[0] << 16) | (p[1] << 8) | p[2];
1085 guint32 *p = (guint32 *)row;
1086 guint32 *q = (guint32 *)(dest_buf + i * dest_rowstride);
1087 guint32 *end = p + width;
1089 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
1090 if (dest_byteorder == GDK_LSB_FIRST)
1096 *q = ( (*p & 0xff00ff00) |
1097 ((*p & 0x000000ff) << 16) |
1098 ((*p & 0x00ff0000) >> 16));
1109 *q = (((*p & 0xff000000) >> 24) |
1110 ((*p & 0x00ffffff) << 8));
1115 #else /* G_BYTE_ORDER == G_BIG_ENDIAN */
1116 if (dest_byteorder == GDK_LSB_FIRST)
1122 *q = ( (*p & 0x00ff00ff) |
1123 ((*p & 0x0000ff00) << 16) |
1124 ((*p & 0xff000000) >> 16));
1135 *q = (((*p & 0xffffff00) >> 8) |
1136 ((*p & 0x000000ff) << 24));
1141 #endif /* G_BYTE_ORDER*/
1145 case GDK_X11_FORMAT_ARGB:
1147 guchar *p = (src_buf + i * src_rowstride);
1148 guchar *q = (dest_buf + i * dest_rowstride);
1149 guchar *end = p + 4 * width;
1152 #define MULT(d,c,a,t) G_STMT_START { t = c * a; d = ((t >> 8) + t) >> 8; } G_STMT_END
1154 if (dest_byteorder == GDK_LSB_FIRST)
1158 MULT(q[0], p[2], p[3], t1);
1159 MULT(q[1], p[1], p[3], t2);
1160 MULT(q[2], p[0], p[3], t3);
1171 MULT(q[1], p[0], p[3], t1);
1172 MULT(q[2], p[1], p[3], t2);
1173 MULT(q[3], p[2], p[3], t3);
1181 case GDK_X11_FORMAT_NONE:
1182 g_assert_not_reached ();
1189 draw_with_images (GdkDrawable *drawable,
1191 GdkX11FormatType format_type,
1192 XRenderPictFormat *format,
1193 XRenderPictFormat *mask_format,
1201 GdkScreen *screen = GDK_DRAWABLE_IMPL_X11 (drawable)->screen;
1202 Display *xdisplay = GDK_SCREEN_XDISPLAY (screen);
1208 Picture mask = None;
1211 pix = gdk_pixmap_new (gdk_screen_get_root_window (screen), width, height, 32);
1213 pict = XRenderCreatePicture (xdisplay,
1214 GDK_PIXMAP_XID (pix),
1217 mask = XRenderCreatePicture (xdisplay,
1218 GDK_PIXMAP_XID (pix),
1219 mask_format, 0, NULL);
1221 dest_pict = gdk_x11_drawable_get_picture (drawable);
1223 pix_gc = _gdk_drawable_get_scratch_gc (pix, FALSE);
1225 for (y0 = 0; y0 < height; y0 += GDK_SCRATCH_IMAGE_HEIGHT)
1227 gint height1 = MIN (height - y0, GDK_SCRATCH_IMAGE_HEIGHT);
1228 for (x0 = 0; x0 < width; x0 += GDK_SCRATCH_IMAGE_WIDTH)
1232 gint width1 = MIN (width - x0, GDK_SCRATCH_IMAGE_WIDTH);
1234 image = _gdk_image_get_scratch (screen, width1, height1, 32, &xs0, &ys0);
1236 _gdk_x11_convert_to_format (src_rgb + y0 * src_rowstride + 4 * x0, src_rowstride,
1237 (guchar *)image->mem + ys0 * image->bpl + xs0 * image->bpp, image->bpl,
1238 format_type, image->byte_order,
1241 gdk_draw_image (pix, pix_gc,
1242 image, xs0, ys0, x0, y0, width1, height1);
1246 XRenderComposite (xdisplay, PictOpOver, pict, mask, dest_pict,
1247 0, 0, 0, 0, dest_x, dest_y, width, height);
1249 XRenderFreePicture (xdisplay, pict);
1251 XRenderFreePicture (xdisplay, mask);
1253 g_object_unref (pix);
1256 typedef struct _ShmPixmapInfo ShmPixmapInfo;
1258 struct _ShmPixmapInfo
1267 shm_pixmap_info_destroy (gpointer data)
1269 ShmPixmapInfo *info = data;
1271 if (info->pict != None)
1272 XRenderFreePicture (info->display, info->pict);
1273 if (info->mask != None)
1274 XRenderFreePicture (info->display, info->mask);
1280 /* Returns FALSE if we can't get a shm pixmap */
1282 get_shm_pixmap_for_image (Display *xdisplay,
1284 XRenderPictFormat *format,
1285 XRenderPictFormat *mask_format,
1290 ShmPixmapInfo *info;
1292 if (image->type != GDK_IMAGE_SHARED)
1295 info = g_object_get_data (G_OBJECT (image), "gdk-x11-shm-pixmap");
1298 *pix = _gdk_x11_image_get_shm_pixmap (image);
1303 info = g_new (ShmPixmapInfo, 1);
1304 info->display = xdisplay;
1307 info->pict = XRenderCreatePicture (xdisplay, info->pix,
1310 info->mask = XRenderCreatePicture (xdisplay, info->pix,
1311 mask_format, 0, NULL);
1315 g_object_set_data_full (G_OBJECT (image), "gdk-x11-shm-pixmap", info,
1316 shm_pixmap_info_destroy);
1327 /* Returns FALSE if drawing with ShmPixmaps is not possible */
1329 draw_with_pixmaps (GdkDrawable *drawable,
1331 GdkX11FormatType format_type,
1332 XRenderPictFormat *format,
1333 XRenderPictFormat *mask_format,
1341 Display *xdisplay = GDK_SCREEN_XDISPLAY (GDK_DRAWABLE_IMPL_X11 (drawable)->screen);
1346 Picture mask = None;
1349 dest_pict = gdk_x11_drawable_get_picture (drawable);
1351 for (y0 = 0; y0 < height; y0 += GDK_SCRATCH_IMAGE_HEIGHT)
1353 gint height1 = MIN (height - y0, GDK_SCRATCH_IMAGE_HEIGHT);
1354 for (x0 = 0; x0 < width; x0 += GDK_SCRATCH_IMAGE_WIDTH)
1358 gint width1 = MIN (width - x0, GDK_SCRATCH_IMAGE_WIDTH);
1360 image = _gdk_image_get_scratch (GDK_DRAWABLE_IMPL_X11 (drawable)->screen,
1361 width1, height1, 32, &xs0, &ys0);
1362 if (!get_shm_pixmap_for_image (xdisplay, image, format, mask_format, &pix, &pict, &mask))
1365 _gdk_x11_convert_to_format (src_rgb + y0 * src_rowstride + 4 * x0, src_rowstride,
1366 (guchar *)image->mem + ys0 * image->bpl + xs0 * image->bpp, image->bpl,
1367 format_type, image->byte_order,
1370 XRenderComposite (xdisplay, PictOpOver, pict, mask, dest_pict,
1371 xs0, ys0, xs0, ys0, x0 + dest_x, y0 + dest_y,
1381 gdk_x11_draw_pixbuf (GdkDrawable *drawable,
1390 GdkRgbDither dither,
1394 GdkX11FormatType format_type;
1395 XRenderPictFormat *format, *mask_format;
1398 gboolean use_pixmaps = TRUE;
1399 #endif /* USE_SHM */
1401 format_type = select_format (gdk_drawable_get_display (drawable),
1402 &format, &mask_format);
1404 if (format_type == GDK_X11_FORMAT_NONE ||
1405 !gdk_pixbuf_get_has_alpha (pixbuf) ||
1406 gdk_drawable_get_depth (drawable) == 1 ||
1407 (dither == GDK_RGB_DITHER_MAX && gdk_drawable_get_depth (drawable) != 24) ||
1408 gdk_x11_drawable_get_picture (drawable) == None)
1410 GdkDrawable *wrapper = GDK_DRAWABLE_IMPL_X11 (drawable)->wrapper;
1411 GDK_DRAWABLE_CLASS (_gdk_drawable_impl_x11_parent_class)->draw_pixbuf (wrapper, gc, pixbuf,
1412 src_x, src_y, dest_x, dest_y,
1414 dither, x_dither, y_dither);
1418 gdk_x11_drawable_update_picture_clip (drawable, gc);
1420 rowstride = gdk_pixbuf_get_rowstride (pixbuf);
1425 if (!draw_with_pixmaps (drawable, gc,
1426 format_type, format, mask_format,
1427 gdk_pixbuf_get_pixels (pixbuf) + src_y * rowstride + src_x * 4,
1429 dest_x, dest_y, width, height))
1430 use_pixmaps = FALSE;
1434 #endif /* USE_SHM */
1435 draw_with_images (drawable, gc,
1436 format_type, format, mask_format,
1437 gdk_pixbuf_get_pixels (pixbuf) + src_y * rowstride + src_x * 4,
1439 dest_x, dest_y, width, height);
1443 gdk_x11_cairo_surface_destroy (void *data)
1445 GdkDrawableImplX11 *impl = data;
1447 impl->cairo_surface = NULL;
1450 static cairo_surface_t *
1451 gdk_x11_ref_cairo_surface (GdkDrawable *drawable)
1453 GdkDrawableImplX11 *impl = GDK_DRAWABLE_IMPL_X11 (drawable);
1455 if (GDK_IS_WINDOW_IMPL_X11 (drawable) &&
1456 GDK_WINDOW_DESTROYED (impl->wrapper))
1459 if (!impl->cairo_surface)
1461 GdkVisual *visual = NULL;
1464 visual = gdk_drawable_get_visual (drawable);
1466 gdk_drawable_get_size (drawable, &width, &height);
1469 impl->cairo_surface = cairo_xlib_surface_create (GDK_SCREEN_XDISPLAY (impl->screen),
1471 GDK_VISUAL_XVISUAL (visual),
1473 else if (gdk_drawable_get_depth (drawable) == 1)
1474 impl->cairo_surface = cairo_xlib_surface_create_for_bitmap (GDK_SCREEN_XDISPLAY (impl->screen),
1476 GDK_SCREEN_XSCREEN (impl->screen),
1480 g_warning ("Using Cairo rendering requires the drawable argument to\n"
1481 "have a specified colormap. All windows have a colormap,\n"
1482 "however, pixmaps only have colormap by default if they\n"
1483 "were created with a non-NULL window argument. Otherwise\n"
1484 "a colormap must be set on them with gdk_drawable_set_colormap");
1488 cairo_surface_set_user_data (impl->cairo_surface, &gdk_x11_cairo_key,
1489 drawable, gdk_x11_cairo_surface_destroy);
1492 cairo_surface_reference (impl->cairo_surface);
1494 return impl->cairo_surface;
1497 #define __GDK_DRAWABLE_X11_C__
1498 #include "gdkaliasdef.c"