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 <pango/pangoxft.h>
34 #include <cairo-xlib.h>
37 #include <string.h> /* for memcpy() */
39 #if defined (HAVE_IPC_H) && defined (HAVE_SHM_H) && defined (HAVE_XSHM_H)
44 #include <X11/extensions/XShm.h>
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"
55 static void gdk_x11_draw_rectangle (GdkDrawable *drawable,
62 static void gdk_x11_draw_arc (GdkDrawable *drawable,
71 static void gdk_x11_draw_polygon (GdkDrawable *drawable,
76 static void gdk_x11_draw_text (GdkDrawable *drawable,
83 static void gdk_x11_draw_text_wc (GdkDrawable *drawable,
90 static void gdk_x11_draw_drawable (GdkDrawable *drawable,
99 static void gdk_x11_draw_points (GdkDrawable *drawable,
103 static void gdk_x11_draw_segments (GdkDrawable *drawable,
107 static void gdk_x11_draw_lines (GdkDrawable *drawable,
112 static void gdk_x11_draw_image (GdkDrawable *drawable,
121 static void gdk_x11_draw_pixbuf (GdkDrawable *drawable,
134 static cairo_surface_t *gdk_x11_ref_cairo_surface (GdkDrawable *drawable);
136 static void gdk_x11_set_colormap (GdkDrawable *drawable,
137 GdkColormap *colormap);
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);
144 static void gdk_drawable_impl_x11_class_init (GdkDrawableImplX11Class *klass);
146 static void gdk_drawable_impl_x11_finalize (GObject *object);
148 static gpointer parent_class = NULL;
149 static const cairo_user_data_key_t gdk_x11_cairo_key;
152 _gdk_drawable_impl_x11_get_type (void)
154 static GType object_type = 0;
158 static const GTypeInfo object_info =
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),
168 (GInstanceInitFunc) NULL,
171 object_type = g_type_register_static (GDK_TYPE_DRAWABLE,
172 "GdkDrawableImplX11",
180 gdk_drawable_impl_x11_class_init (GdkDrawableImplX11Class *klass)
182 GdkDrawableClass *drawable_class = GDK_DRAWABLE_CLASS (klass);
183 GObjectClass *object_class = G_OBJECT_CLASS (klass);
185 parent_class = g_type_class_peek_parent (klass);
187 object_class->finalize = gdk_drawable_impl_x11_finalize;
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;
202 drawable_class->ref_cairo_surface = gdk_x11_ref_cairo_surface;
204 drawable_class->set_colormap = gdk_x11_set_colormap;
205 drawable_class->get_colormap = gdk_x11_get_colormap;
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;
211 drawable_class->_copy_to_image = _gdk_x11_copy_to_image;
215 gdk_drawable_impl_x11_finalize (GObject *object)
217 gdk_drawable_set_colormap (GDK_DRAWABLE (object), NULL);
219 G_OBJECT_CLASS (parent_class)->finalize (object);
223 * _gdk_x11_drawable_finish:
224 * @drawable: a #GdkDrawableImplX11.
226 * Performs necessary cleanup prior to freeing a pixmap or
227 * destroying a window.
230 _gdk_x11_drawable_finish (GdkDrawable *drawable)
232 GdkDrawableImplX11 *impl = GDK_DRAWABLE_IMPL_X11 (drawable);
236 XRenderFreePicture (GDK_SCREEN_XDISPLAY (impl->screen),
238 impl->picture = None;
241 if (impl->cairo_surface)
243 cairo_surface_finish (impl->cairo_surface);
244 cairo_surface_set_user_data (impl->cairo_surface, &gdk_x11_cairo_key,
250 * _gdk_x11_drawable_update_size:
251 * @drawable: a #GdkDrawableImplX11.
253 * Updates the state of the drawable (in particular the drawable's
254 * cairo surface) when its size has changed.
257 _gdk_x11_drawable_update_size (GdkDrawable *drawable)
259 GdkDrawableImplX11 *impl = GDK_DRAWABLE_IMPL_X11 (drawable);
261 if (impl->cairo_surface)
265 gdk_drawable_get_size (drawable, &width, &height);
266 cairo_xlib_surface_set_size (impl->cairo_surface, width, height);
271 try_pixmap (Display *xdisplay,
275 Pixmap pixmap = XCreatePixmap (xdisplay,
276 RootWindow (xdisplay, screen),
278 XFreePixmap (xdisplay, pixmap);
282 _gdk_x11_have_render (GdkDisplay *display)
284 Display *xdisplay = GDK_DISPLAY_XDISPLAY (display);
285 GdkDisplayX11 *x11display = GDK_DISPLAY_X11 (display);
287 if (x11display->have_render == GDK_UNKNOWN)
289 int event_base, error_base;
290 x11display->have_render =
291 XRenderQueryExtension (xdisplay, &event_base, &error_base)
294 if (x11display->have_render == GDK_YES)
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.
303 for (screen = 0; screen < ScreenCount (xdisplay); screen++)
306 int *depths = XListDepths (xdisplay, screen, &count);
307 gboolean has_8 = FALSE, has_32 = FALSE;
313 for (i = 0; i < count; i++)
317 else if (depths[i] == 32)
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.
328 if (!(has_8 && has_32))
330 gdk_error_trap_push ();
332 try_pixmap (xdisplay, screen, 8);
334 try_pixmap (xdisplay, screen, 32);
335 XSync (xdisplay, False);
336 if (gdk_error_trap_pop () == 0)
343 if (!(has_8 && has_32))
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;
355 return x11display->have_render == GDK_YES;
359 gdk_x11_drawable_get_picture (GdkDrawable *drawable)
361 GdkDrawableImplX11 *impl = GDK_DRAWABLE_IMPL_X11 (drawable);
365 Display *xdisplay = GDK_SCREEN_XDISPLAY (impl->screen);
366 XRenderPictFormat *format;
368 GdkVisual *visual = gdk_drawable_get_visual (GDK_DRAWABLE_IMPL_X11 (drawable)->wrapper);
372 format = XRenderFindVisualFormat (xdisplay, GDK_VISUAL_XVISUAL (visual));
375 XRenderPictureAttributes attributes;
376 attributes.graphics_exposures = False;
378 impl->picture = XRenderCreatePicture (xdisplay, impl->xid, format,
379 CPGraphicsExposure, &attributes);
383 return impl->picture;
387 gdk_x11_drawable_update_picture_clip (GdkDrawable *drawable,
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;
397 GdkRegionBox *boxes = clip_region->rects;
398 gint n_boxes = clip_region->numRects;
399 XRectangle *rects = g_new (XRectangle, n_boxes);
402 for (i=0; i < n_boxes; i++)
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;
410 XRenderSetPictureClipRectangles (xdisplay, picture,
411 0, 0, rects, n_boxes);
417 XRenderPictureAttributes pa;
419 XRenderChangePicture (xdisplay, picture,
424 /*****************************************************
425 * X11 specific implementations of generic functions *
426 *****************************************************/
429 gdk_x11_get_colormap (GdkDrawable *drawable)
431 GdkDrawableImplX11 *impl;
433 impl = GDK_DRAWABLE_IMPL_X11 (drawable);
435 return impl->colormap;
439 gdk_x11_set_colormap (GdkDrawable *drawable,
440 GdkColormap *colormap)
442 GdkDrawableImplX11 *impl;
444 impl = GDK_DRAWABLE_IMPL_X11 (drawable);
446 if (impl->colormap == colormap)
450 g_object_unref (impl->colormap);
451 impl->colormap = colormap;
453 g_object_ref (impl->colormap);
460 gdk_x11_draw_rectangle (GdkDrawable *drawable,
468 GdkDrawableImplX11 *impl;
470 impl = GDK_DRAWABLE_IMPL_X11 (drawable);
473 XFillRectangle (GDK_SCREEN_XDISPLAY (impl->screen), impl->xid,
474 GDK_GC_GET_XGC (gc), x, y, width, height);
476 XDrawRectangle (GDK_SCREEN_XDISPLAY (impl->screen), impl->xid,
477 GDK_GC_GET_XGC (gc), x, y, width, height);
481 gdk_x11_draw_arc (GdkDrawable *drawable,
491 GdkDrawableImplX11 *impl;
493 impl = GDK_DRAWABLE_IMPL_X11 (drawable);
497 XFillArc (GDK_SCREEN_XDISPLAY (impl->screen), impl->xid,
498 GDK_GC_GET_XGC (gc), x, y, width, height, angle1, angle2);
500 XDrawArc (GDK_SCREEN_XDISPLAY (impl->screen), impl->xid,
501 GDK_GC_GET_XGC (gc), x, y, width, height, angle1, angle2);
505 gdk_x11_draw_polygon (GdkDrawable *drawable,
513 GdkDrawableImplX11 *impl;
515 impl = GDK_DRAWABLE_IMPL_X11 (drawable);
519 (points[0].x != points[npoints-1].x || points[0].y != points[npoints-1].y))
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;
528 tmp_npoints = npoints;
529 tmp_points = g_new (XPoint, tmp_npoints);
532 for (i=0; i<npoints; i++)
534 tmp_points[i].x = points[i].x;
535 tmp_points[i].y = points[i].y;
539 XFillPolygon (GDK_SCREEN_XDISPLAY (impl->screen), impl->xid,
540 GDK_GC_GET_XGC (gc), tmp_points, tmp_npoints, Complex, CoordModeOrigin);
542 XDrawLines (GDK_SCREEN_XDISPLAY (impl->screen), impl->xid,
543 GDK_GC_GET_XGC (gc), tmp_points, tmp_npoints, CoordModeOrigin);
550 * Modified by Li-Da Lho to draw 16 bits and Multibyte strings
552 * Interface changed: add "GdkFont *font" to specify font or fontset explicitely
555 gdk_x11_draw_text (GdkDrawable *drawable,
563 GdkDrawableImplX11 *impl;
566 impl = GDK_DRAWABLE_IMPL_X11 (drawable);
567 xdisplay = GDK_SCREEN_XDISPLAY (impl->screen);
569 if (font->type == GDK_FONT_FONT)
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))
575 XDrawString (xdisplay, impl->xid,
576 GDK_GC_GET_XGC (gc), x, y, text, text_length);
580 XDrawString16 (xdisplay, impl->xid,
581 GDK_GC_GET_XGC (gc), x, y, (XChar2b *) text, text_length / 2);
584 else if (font->type == GDK_FONT_FONTSET)
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);
591 g_error("undefined font type\n");
595 gdk_x11_draw_text_wc (GdkDrawable *drawable,
600 const GdkWChar *text,
603 GdkDrawableImplX11 *impl;
606 impl = GDK_DRAWABLE_IMPL_X11 (drawable);
607 xdisplay = GDK_SCREEN_XDISPLAY (impl->screen);
609 if (font->type == GDK_FONT_FONT)
611 XFontStruct *xfont = (XFontStruct *) GDK_FONT_XFONT (font);
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);
621 else if (font->type == GDK_FONT_FONTSET)
623 if (sizeof(GdkWChar) == sizeof(wchar_t))
625 XwcDrawString (xdisplay, impl->xid,
626 (XFontSet) GDK_FONT_XFONT (font),
627 GDK_GC_GET_XGC (gc), x, y, (wchar_t *)text, text_length);
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);
642 g_error("undefined font type\n");
646 gdk_x11_draw_drawable (GdkDrawable *drawable,
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;
661 impl = GDK_DRAWABLE_IMPL_X11 (drawable);
663 if (GDK_IS_DRAWABLE_IMPL_X11 (src))
664 src_impl = GDK_DRAWABLE_IMPL_X11 (src);
670 XCopyArea (GDK_SCREEN_XDISPLAY (impl->screen),
671 src_impl ? src_impl->xid : GDK_DRAWABLE_XID (src),
678 else if (dest_depth != 0 && src_depth == dest_depth)
680 XCopyArea (GDK_SCREEN_XDISPLAY (impl->screen),
681 src_impl ? src_impl->xid : GDK_DRAWABLE_XID (src),
689 g_warning ("Attempt to draw a drawable with depth %d to a drawable with depth %d",
690 src_depth, dest_depth);
694 gdk_x11_draw_points (GdkDrawable *drawable,
699 GdkDrawableImplX11 *impl;
701 impl = GDK_DRAWABLE_IMPL_X11 (drawable);
704 /* We special-case npoints == 1, because X will merge multiple
705 * consecutive XDrawPoint requests into a PolyPoint request
709 XDrawPoint (GDK_SCREEN_XDISPLAY (impl->screen),
712 points[0].x, points[0].y);
717 XPoint *tmp_points = g_new (XPoint, npoints);
719 for (i=0; i<npoints; i++)
721 tmp_points[i].x = points[i].x;
722 tmp_points[i].y = points[i].y;
725 XDrawPoints (GDK_SCREEN_XDISPLAY (impl->screen),
737 gdk_x11_draw_segments (GdkDrawable *drawable,
742 GdkDrawableImplX11 *impl;
744 impl = GDK_DRAWABLE_IMPL_X11 (drawable);
747 /* We special-case nsegs == 1, because X will merge multiple
748 * consecutive XDrawLine requests into a PolySegment request
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);
759 XSegment *tmp_segs = g_new (XSegment, nsegs);
761 for (i=0; i<nsegs; i++)
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;
769 XDrawSegments (GDK_SCREEN_XDISPLAY (impl->screen),
779 gdk_x11_draw_lines (GdkDrawable *drawable,
785 XPoint *tmp_points = g_new (XPoint, npoints);
786 GdkDrawableImplX11 *impl;
788 impl = GDK_DRAWABLE_IMPL_X11 (drawable);
791 for (i=0; i<npoints; i++)
793 tmp_points[i].x = points[i].x;
794 tmp_points[i].y = points[i].y;
797 XDrawLines (GDK_SCREEN_XDISPLAY (impl->screen),
807 gdk_x11_draw_image (GdkDrawable *drawable,
817 GdkDrawableImplX11 *impl;
819 impl = GDK_DRAWABLE_IMPL_X11 (drawable);
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);
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);
834 gdk_x11_get_depth (GdkDrawable *drawable)
836 /* This is a bit bogus but I'm not sure the other way is better */
838 return gdk_drawable_get_depth (GDK_DRAWABLE_IMPL_X11 (drawable)->wrapper);
842 get_impl_drawable (GdkDrawable *drawable)
844 if (GDK_IS_WINDOW (drawable))
845 return ((GdkWindowObject *)drawable)->impl;
846 else if (GDK_IS_PIXMAP (drawable))
847 return ((GdkPixmapObject *)drawable)->impl;
850 g_warning (G_STRLOC " drawable is not a pixmap or window");
856 gdk_x11_get_screen (GdkDrawable *drawable)
858 if (GDK_IS_DRAWABLE_IMPL_X11 (drawable))
859 return GDK_DRAWABLE_IMPL_X11 (drawable)->screen;
861 return GDK_DRAWABLE_IMPL_X11 (get_impl_drawable (drawable))->screen;
865 gdk_x11_get_visual (GdkDrawable *drawable)
867 return gdk_drawable_get_visual (GDK_DRAWABLE_IMPL_X11 (drawable)->wrapper);
871 * gdk_x11_drawable_get_xdisplay:
872 * @drawable: a #GdkDrawable.
874 * Returns the display of a #GdkDrawable.
876 * Return value: an Xlib <type>Display*</type>.
879 gdk_x11_drawable_get_xdisplay (GdkDrawable *drawable)
881 if (GDK_IS_DRAWABLE_IMPL_X11 (drawable))
882 return GDK_SCREEN_XDISPLAY (GDK_DRAWABLE_IMPL_X11 (drawable)->screen);
884 return GDK_SCREEN_XDISPLAY (GDK_DRAWABLE_IMPL_X11 (get_impl_drawable (drawable))->screen);
888 * gdk_x11_drawable_get_xid:
889 * @drawable: a #GdkDrawable.
891 * Returns the X resource (window or pixmap) belonging to a #GdkDrawable.
893 * Return value: the ID of @drawable's X resource.
896 gdk_x11_drawable_get_xid (GdkDrawable *drawable)
900 if (GDK_IS_WINDOW (drawable))
901 impl = ((GdkPixmapObject *)drawable)->impl;
902 else if (GDK_IS_PIXMAP (drawable))
903 impl = ((GdkPixmapObject *)drawable)->impl;
906 g_warning (G_STRLOC " drawable is not a pixmap or window");
910 return ((GdkDrawableImplX11 *)impl)->xid;
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.
919 static GdkX11FormatType
920 select_format (GdkDisplay *display,
921 XRenderPictFormat **format,
922 XRenderPictFormat **mask)
924 Display *xdisplay = GDK_DISPLAY_XDISPLAY (display);
925 XRenderPictFormat pf;
927 if (!_gdk_x11_have_render (display))
928 return GDK_X11_FORMAT_NONE;
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.
935 pf.type = PictTypeDirect;
937 pf.direct.redMask = 0xff;
938 pf.direct.greenMask = 0xff;
939 pf.direct.blueMask = 0xff;
941 pf.direct.alphaMask = 0;
942 if (ImageByteOrder (xdisplay) == LSBFirst)
953 pf.direct.green = 16;
957 *format = XRenderFindFormat (xdisplay,
958 (PictFormatType | PictFormatDepth |
959 PictFormatRedMask | PictFormatRed |
960 PictFormatGreenMask | PictFormatGreen |
961 PictFormatBlueMask | PictFormatBlue |
962 PictFormatAlphaMask),
966 pf.direct.alphaMask = 0xff;
967 if (ImageByteOrder (xdisplay) == LSBFirst)
970 pf.direct.alpha = 24;
977 *mask = XRenderFindFormat (xdisplay,
978 (PictFormatType | PictFormatDepth |
979 PictFormatAlphaMask | PictFormatAlpha),
983 if (*format && *mask)
984 return GDK_X11_FORMAT_EXACT_MASK;
986 /* OK, that failed, now look for xRGB and Axxx formats in
987 * RENDER's preferred order
989 pf.direct.alphaMask = 0;
995 *format = XRenderFindFormat (xdisplay,
996 (PictFormatType | PictFormatDepth |
997 PictFormatRedMask | PictFormatRed |
998 PictFormatGreenMask | PictFormatGreen |
999 PictFormatBlueMask | PictFormatBlue |
1000 PictFormatAlphaMask),
1004 pf.direct.alphaMask = 0xff;
1005 pf.direct.alpha = 24;
1007 *mask = XRenderFindFormat (xdisplay,
1008 (PictFormatType | PictFormatDepth |
1009 PictFormatAlphaMask | PictFormatAlpha),
1013 if (*format && *mask)
1014 return GDK_X11_FORMAT_ARGB_MASK;
1016 /* Finally, if neither of the above worked, fall back to
1017 * looking for combined ARGB -- we'll premultiply ourselves.
1020 pf.type = PictTypeDirect;
1023 pf.direct.green = 8;
1025 pf.direct.alphaMask = 0xff;
1026 pf.direct.alpha = 24;
1028 *format = XRenderFindFormat (xdisplay,
1029 (PictFormatType | PictFormatDepth |
1030 PictFormatRedMask | PictFormatRed |
1031 PictFormatGreenMask | PictFormatGreen |
1032 PictFormatBlueMask | PictFormatBlue |
1033 PictFormatAlphaMask | PictFormatAlpha),
1039 return GDK_X11_FORMAT_ARGB;
1041 return GDK_X11_FORMAT_NONE;
1046 list_formats (XRenderPictFormat *pf)
1052 XRenderPictFormat *pf = XRenderFindFormat (impl->xdisplay, 0, NULL, i);
1055 g_print ("%2d R-%#06x/%#06x G-%#06x/%#06x B-%#06x/%#06x A-%#06x/%#06x\n",
1060 pf->direct.greenMask,
1062 pf->direct.blueMask,
1064 pf->direct.alphaMask);
1073 _gdk_x11_convert_to_format (guchar *src_buf,
1076 gint dest_rowstride,
1077 GdkX11FormatType dest_format,
1078 GdkByteOrder dest_byteorder,
1084 for (i=0; i < height; i++)
1086 switch (dest_format)
1088 case GDK_X11_FORMAT_EXACT_MASK:
1090 memcpy (dest_buf + i * dest_rowstride,
1091 src_buf + i * src_rowstride,
1095 case GDK_X11_FORMAT_ARGB_MASK:
1097 guchar *row = src_buf + i * src_rowstride;
1098 if (((gsize)row & 3) != 0)
1101 guint32 *q = (guint32 *)(dest_buf + i * dest_rowstride);
1102 guchar *end = p + 4 * width;
1106 *q = (p[3] << 24) | (p[0] << 16) | (p[1] << 8) | p[2];
1113 guint32 *p = (guint32 *)row;
1114 guint32 *q = (guint32 *)(dest_buf + i * dest_rowstride);
1115 guint32 *end = p + width;
1117 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
1118 if (dest_byteorder == GDK_LSB_FIRST)
1124 *q = ( (*p & 0xff00ff00) |
1125 ((*p & 0x000000ff) << 16) |
1126 ((*p & 0x00ff0000) >> 16));
1137 *q = (((*p & 0xff000000) >> 24) |
1138 ((*p & 0x00ffffff) << 8));
1143 #else /* G_BYTE_ORDER == G_BIG_ENDIAN */
1144 if (dest_byteorder == GDK_LSB_FIRST)
1150 *q = ( (*p & 0x00ff00ff) |
1151 ((*p & 0x0000ff00) << 16) |
1152 ((*p & 0xff000000) >> 16));
1163 *q = (((*p & 0xffffff00) >> 8) |
1164 ((*p & 0x000000ff) << 24));
1169 #endif /* G_BYTE_ORDER*/
1173 case GDK_X11_FORMAT_ARGB:
1175 guchar *p = (src_buf + i * src_rowstride);
1176 guchar *q = (dest_buf + i * dest_rowstride);
1177 guchar *end = p + 4 * width;
1180 #define MULT(d,c,a,t) G_STMT_START { t = c * a; d = ((t >> 8) + t) >> 8; } G_STMT_END
1182 if (dest_byteorder == GDK_LSB_FIRST)
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);
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);
1209 case GDK_X11_FORMAT_NONE:
1210 g_assert_not_reached ();
1217 draw_with_images (GdkDrawable *drawable,
1219 GdkX11FormatType format_type,
1220 XRenderPictFormat *format,
1221 XRenderPictFormat *mask_format,
1229 GdkScreen *screen = GDK_DRAWABLE_IMPL_X11 (drawable)->screen;
1230 Display *xdisplay = GDK_SCREEN_XDISPLAY (screen);
1236 Picture mask = None;
1239 pix = gdk_pixmap_new (gdk_screen_get_root_window (screen), width, height, 32);
1241 pict = XRenderCreatePicture (xdisplay,
1242 GDK_PIXMAP_XID (pix),
1245 mask = XRenderCreatePicture (xdisplay,
1246 GDK_PIXMAP_XID (pix),
1247 mask_format, 0, NULL);
1249 dest_pict = gdk_x11_drawable_get_picture (drawable);
1251 pix_gc = _gdk_drawable_get_scratch_gc (pix, FALSE);
1253 for (y0 = 0; y0 < height; y0 += GDK_SCRATCH_IMAGE_HEIGHT)
1255 gint height1 = MIN (height - y0, GDK_SCRATCH_IMAGE_HEIGHT);
1256 for (x0 = 0; x0 < width; x0 += GDK_SCRATCH_IMAGE_WIDTH)
1260 gint width1 = MIN (width - x0, GDK_SCRATCH_IMAGE_WIDTH);
1262 image = _gdk_image_get_scratch (screen, width1, height1, 32, &xs0, &ys0);
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,
1269 gdk_draw_image (pix, pix_gc,
1270 image, xs0, ys0, x0, y0, width1, height1);
1274 XRenderComposite (xdisplay, PictOpOver, pict, mask, dest_pict,
1275 0, 0, 0, 0, dest_x, dest_y, width, height);
1277 XRenderFreePicture (xdisplay, pict);
1279 XRenderFreePicture (xdisplay, mask);
1281 g_object_unref (pix);
1284 typedef struct _ShmPixmapInfo ShmPixmapInfo;
1286 struct _ShmPixmapInfo
1294 /* Returns FALSE if we can't get a shm pixmap */
1296 get_shm_pixmap_for_image (Display *xdisplay,
1298 XRenderPictFormat *format,
1299 XRenderPictFormat *mask_format,
1304 ShmPixmapInfo *info;
1306 if (image->type != GDK_IMAGE_SHARED)
1309 info = g_object_get_data (G_OBJECT (image), "gdk-x11-shm-pixmap");
1312 *pix = _gdk_x11_image_get_shm_pixmap (image);
1317 info = g_new (ShmPixmapInfo, 1);
1320 info->pict = XRenderCreatePicture (xdisplay, info->pix,
1323 info->mask = XRenderCreatePicture (xdisplay, info->pix,
1324 mask_format, 0, NULL);
1328 g_object_set_data (G_OBJECT (image), "gdk-x11-shm-pixmap", info);
1339 /* Returns FALSE if drawing with ShmPixmaps is not possible */
1341 draw_with_pixmaps (GdkDrawable *drawable,
1343 GdkX11FormatType format_type,
1344 XRenderPictFormat *format,
1345 XRenderPictFormat *mask_format,
1353 Display *xdisplay = GDK_SCREEN_XDISPLAY (GDK_DRAWABLE_IMPL_X11 (drawable)->screen);
1358 Picture mask = None;
1361 dest_pict = gdk_x11_drawable_get_picture (drawable);
1363 for (y0 = 0; y0 < height; y0 += GDK_SCRATCH_IMAGE_HEIGHT)
1365 gint height1 = MIN (height - y0, GDK_SCRATCH_IMAGE_HEIGHT);
1366 for (x0 = 0; x0 < width; x0 += GDK_SCRATCH_IMAGE_WIDTH)
1370 gint width1 = MIN (width - x0, GDK_SCRATCH_IMAGE_WIDTH);
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))
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,
1382 XRenderComposite (xdisplay, PictOpOver, pict, mask, dest_pict,
1383 xs0, ys0, xs0, ys0, x0 + dest_x, y0 + dest_y,
1393 gdk_x11_draw_pixbuf (GdkDrawable *drawable,
1402 GdkRgbDither dither,
1406 GdkX11FormatType format_type;
1407 XRenderPictFormat *format, *mask_format;
1410 gboolean use_pixmaps = TRUE;
1411 #endif /* USE_SHM */
1413 format_type = select_format (gdk_drawable_get_display (drawable),
1414 &format, &mask_format);
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)
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,
1426 dither, x_dither, y_dither);
1430 gdk_x11_drawable_update_picture_clip (drawable, gc);
1432 rowstride = gdk_pixbuf_get_rowstride (pixbuf);
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,
1441 dest_x, dest_y, width, height))
1442 use_pixmaps = FALSE;
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,
1451 dest_x, dest_y, width, height);
1455 gdk_x11_cairo_surface_destroy (void *data)
1457 GdkDrawableImplX11 *impl = data;
1459 impl->cairo_surface = NULL;
1462 static cairo_surface_t *
1463 gdk_x11_ref_cairo_surface (GdkDrawable *drawable)
1465 GdkDrawableImplX11 *impl = GDK_DRAWABLE_IMPL_X11 (drawable);
1467 if (GDK_IS_WINDOW_IMPL_X11 (drawable) &&
1468 GDK_WINDOW_DESTROYED (impl->wrapper))
1471 if (!impl->cairo_surface)
1473 GdkVisual *visual = NULL;
1476 visual = gdk_drawable_get_visual (drawable);
1478 gdk_drawable_get_size (drawable, &width, &height);
1481 impl->cairo_surface = cairo_xlib_surface_create (GDK_SCREEN_XDISPLAY (impl->screen),
1483 GDK_VISUAL_XVISUAL (visual),
1485 else if (gdk_drawable_get_depth (drawable) == 1)
1486 impl->cairo_surface = cairo_xlib_surface_create_for_bitmap (GDK_SCREEN_XDISPLAY (impl->screen),
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");
1499 cairo_surface_set_user_data (impl->cairo_surface, &gdk_x11_cairo_key,
1500 drawable, gdk_x11_cairo_surface_destroy);
1503 cairo_surface_reference (impl->cairo_surface);
1505 return impl->cairo_surface;
1508 #define __GDK_DRAWABLE_X11_C__
1509 #include "gdkaliasdef.c"