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 "gdkregion-generic.h"
33 #include <pango/pangoxft.h>
36 #include <string.h> /* for memcpy() */
38 #if defined (HAVE_IPC_H) && defined (HAVE_SHM_H) && defined (HAVE_XSHM_H)
43 #include <X11/extensions/XShm.h>
46 #include "gdkprivate-x11.h"
47 #include "gdkdrawable-x11.h"
48 #include "gdkpixmap-x11.h"
49 #include "gdkscreen-x11.h"
50 #include "gdkdisplay-x11.h"
52 static void gdk_x11_draw_rectangle (GdkDrawable *drawable,
59 static void gdk_x11_draw_arc (GdkDrawable *drawable,
68 static void gdk_x11_draw_polygon (GdkDrawable *drawable,
73 static void gdk_x11_draw_text (GdkDrawable *drawable,
80 static void gdk_x11_draw_text_wc (GdkDrawable *drawable,
87 static void gdk_x11_draw_drawable (GdkDrawable *drawable,
96 static void gdk_x11_draw_points (GdkDrawable *drawable,
100 static void gdk_x11_draw_segments (GdkDrawable *drawable,
104 static void gdk_x11_draw_lines (GdkDrawable *drawable,
109 static void gdk_x11_draw_glyphs (GdkDrawable *drawable,
114 PangoGlyphString *glyphs);
115 static void gdk_x11_draw_glyphs_transformed (GdkDrawable *drawable,
121 PangoGlyphString *glyphs);
123 static void gdk_x11_draw_image (GdkDrawable *drawable,
132 static void gdk_x11_draw_pixbuf (GdkDrawable *drawable,
145 static void gdk_x11_draw_trapezoids (GdkDrawable *drawable,
147 GdkTrapezoid *trapezoids,
150 static void gdk_x11_set_colormap (GdkDrawable *drawable,
151 GdkColormap *colormap);
153 static GdkColormap* gdk_x11_get_colormap (GdkDrawable *drawable);
154 static gint gdk_x11_get_depth (GdkDrawable *drawable);
155 static GdkScreen * gdk_x11_get_screen (GdkDrawable *drawable);
156 static GdkVisual* gdk_x11_get_visual (GdkDrawable *drawable);
158 static void gdk_drawable_impl_x11_class_init (GdkDrawableImplX11Class *klass);
160 static void gdk_drawable_impl_x11_finalize (GObject *object);
162 static gpointer parent_class = NULL;
165 _gdk_drawable_impl_x11_get_type (void)
167 static GType object_type = 0;
171 static const GTypeInfo object_info =
173 sizeof (GdkDrawableImplX11Class),
174 (GBaseInitFunc) NULL,
175 (GBaseFinalizeFunc) NULL,
176 (GClassInitFunc) gdk_drawable_impl_x11_class_init,
177 NULL, /* class_finalize */
178 NULL, /* class_data */
179 sizeof (GdkDrawableImplX11),
181 (GInstanceInitFunc) NULL,
184 object_type = g_type_register_static (GDK_TYPE_DRAWABLE,
185 "GdkDrawableImplX11",
193 gdk_drawable_impl_x11_class_init (GdkDrawableImplX11Class *klass)
195 GdkDrawableClass *drawable_class = GDK_DRAWABLE_CLASS (klass);
196 GObjectClass *object_class = G_OBJECT_CLASS (klass);
198 parent_class = g_type_class_peek_parent (klass);
200 object_class->finalize = gdk_drawable_impl_x11_finalize;
202 drawable_class->create_gc = _gdk_x11_gc_new;
203 drawable_class->draw_rectangle = gdk_x11_draw_rectangle;
204 drawable_class->draw_arc = gdk_x11_draw_arc;
205 drawable_class->draw_polygon = gdk_x11_draw_polygon;
206 drawable_class->draw_text = gdk_x11_draw_text;
207 drawable_class->draw_text_wc = gdk_x11_draw_text_wc;
208 drawable_class->draw_drawable = gdk_x11_draw_drawable;
209 drawable_class->draw_points = gdk_x11_draw_points;
210 drawable_class->draw_segments = gdk_x11_draw_segments;
211 drawable_class->draw_lines = gdk_x11_draw_lines;
212 drawable_class->draw_glyphs = gdk_x11_draw_glyphs;
213 drawable_class->draw_glyphs_transformed = gdk_x11_draw_glyphs_transformed;
214 drawable_class->draw_image = gdk_x11_draw_image;
215 drawable_class->draw_pixbuf = gdk_x11_draw_pixbuf;
216 drawable_class->draw_trapezoids = gdk_x11_draw_trapezoids;
218 drawable_class->set_colormap = gdk_x11_set_colormap;
219 drawable_class->get_colormap = gdk_x11_get_colormap;
221 drawable_class->get_depth = gdk_x11_get_depth;
222 drawable_class->get_screen = gdk_x11_get_screen;
223 drawable_class->get_visual = gdk_x11_get_visual;
225 drawable_class->_copy_to_image = _gdk_x11_copy_to_image;
229 gdk_drawable_impl_x11_finalize (GObject *object)
231 gdk_drawable_set_colormap (GDK_DRAWABLE (object), NULL);
233 G_OBJECT_CLASS (parent_class)->finalize (object);
237 try_pixmap (Display *xdisplay,
241 Pixmap pixmap = XCreatePixmap (xdisplay,
242 RootWindow (xdisplay, screen),
244 XFreePixmap (xdisplay, pixmap);
248 _gdk_x11_have_render (GdkDisplay *display)
250 Display *xdisplay = GDK_DISPLAY_XDISPLAY (display);
251 GdkDisplayX11 *x11display = GDK_DISPLAY_X11 (display);
253 if (x11display->have_render == GDK_UNKNOWN)
255 int event_base, error_base;
256 x11display->have_render =
257 XRenderQueryExtension (xdisplay, &event_base, &error_base)
260 if (x11display->have_render == GDK_YES)
263 * Sun advertises RENDER, but fails to support 32-bit pixmaps.
264 * That is just no good. Therefore, we check all screens
265 * for proper support.
269 for (screen = 0; screen < ScreenCount (xdisplay); screen++)
272 int *depths = XListDepths (xdisplay, screen, &count);
273 gboolean has_8 = FALSE, has_32 = FALSE;
279 for (i = 0; i < count; i++)
283 else if (depths[i] == 32)
289 /* At this point, we might have a false positive;
290 * buggy versions of Xinerama only report depths for
291 * which there is an associated visual; so we actually
292 * go ahead and try create pixmaps.
294 if (!(has_8 && has_32))
296 gdk_error_trap_push ();
298 try_pixmap (xdisplay, screen, 8);
300 try_pixmap (xdisplay, screen, 32);
301 XSync (xdisplay, False);
302 if (gdk_error_trap_pop () == 0)
309 if (!(has_8 && has_32))
311 g_warning ("The X server advertises that RENDER support is present,\n"
312 "but fails to supply the necessary pixmap support. In\n"
313 "other words, it is buggy.");
314 x11display->have_render = GDK_NO;
321 return x11display->have_render == GDK_YES;
325 gdk_x11_drawable_get_xft_draw (GdkDrawable *drawable)
327 GdkDrawableImplX11 *impl = GDK_DRAWABLE_IMPL_X11 (drawable);
329 if (impl->xft_draw == NULL)
331 GdkColormap *colormap = gdk_drawable_get_colormap (drawable);
337 visual = gdk_colormap_get_visual (colormap);
339 impl->xft_draw = XftDrawCreate (GDK_SCREEN_XDISPLAY (impl->screen), impl->xid,
340 GDK_VISUAL_XVISUAL (visual), GDK_COLORMAP_XCOLORMAP (colormap));
342 else if (gdk_drawable_get_depth (drawable) == 1)
344 impl->xft_draw = XftDrawCreateBitmap (GDK_SCREEN_XDISPLAY (impl->screen), impl->xid);
348 g_warning ("Using Xft rendering requires the drawable argument to\n"
349 "have a specified colormap. All windows have a colormap,\n"
350 "however, pixmaps only have colormap by default if they\n"
351 "were created with a non-NULL window argument. Otherwise\n"
352 "a colormap must be set on them with gdk_drawable_set_colormap");
357 return impl->xft_draw;
361 gdk_x11_drawable_get_picture (GdkDrawable *drawable)
363 XftDraw *draw = gdk_x11_drawable_get_xft_draw (drawable);
365 return draw ? XftDrawPicture (draw) : None;
369 gdk_x11_drawable_update_xft_clip (GdkDrawable *drawable,
372 GdkGCX11 *gc_private = gc ? GDK_GC_X11 (gc) : NULL;
373 XftDraw *xft_draw = gdk_x11_drawable_get_xft_draw (drawable);
375 if (gc && gc_private->clip_region)
377 GdkRegionBox *boxes = gc_private->clip_region->rects;
378 gint n_boxes = gc_private->clip_region->numRects;
379 #if 0 /* Until XftDrawSetClipRectangles is there */
380 XRectangle *rects = g_new (XRectangle, n_boxes);
383 for (i=0; i < n_boxes; i++)
385 rects[i].x = CLAMP (boxes[i].x1 + gc->clip_x_origin, G_MINSHORT, G_MAXSHORT);
386 rects[i].y = CLAMP (boxes[i].y1 + gc->clip_y_origin, G_MINSHORT, G_MAXSHORT);
387 rects[i].width = CLAMP (boxes[i].x2 + gc->clip_x_origin, G_MINSHORT, G_MAXSHORT) - rects[i].x;
388 rects[i].height = CLAMP (boxes[i].y2 + gc->clip_y_origin, G_MINSHORT, G_MAXSHORT) - rects[i].y;
390 XftDrawSetClipRectangles (xft_draw, 0, 0, rects, n_boxes);
394 Region xregion = XCreateRegion ();
397 for (i=0; i < n_boxes; i++)
401 rect.x = CLAMP (boxes[i].x1 + gc->clip_x_origin, G_MINSHORT, G_MAXSHORT);
402 rect.y = CLAMP (boxes[i].y1 + gc->clip_y_origin, G_MINSHORT, G_MAXSHORT);
403 rect.width = CLAMP (boxes[i].x2 + gc->clip_x_origin, G_MINSHORT, G_MAXSHORT) - rect.x;
404 rect.height = CLAMP (boxes[i].y2 + gc->clip_y_origin, G_MINSHORT, G_MAXSHORT) - rect.y;
406 XUnionRectWithRegion (&rect, xregion, xregion);
409 XftDrawSetClip (xft_draw, xregion);
410 XDestroyRegion (xregion);
415 XftDrawSetClip (xft_draw, NULL);
419 /*****************************************************
420 * X11 specific implementations of generic functions *
421 *****************************************************/
424 gdk_x11_get_colormap (GdkDrawable *drawable)
426 GdkDrawableImplX11 *impl;
428 impl = GDK_DRAWABLE_IMPL_X11 (drawable);
430 return impl->colormap;
434 gdk_x11_set_colormap (GdkDrawable *drawable,
435 GdkColormap *colormap)
437 GdkDrawableImplX11 *impl;
439 impl = GDK_DRAWABLE_IMPL_X11 (drawable);
441 if (impl->colormap == colormap)
445 g_object_unref (impl->colormap);
446 impl->colormap = colormap;
448 g_object_ref (impl->colormap);
455 gdk_x11_draw_rectangle (GdkDrawable *drawable,
463 GdkDrawableImplX11 *impl;
465 impl = GDK_DRAWABLE_IMPL_X11 (drawable);
468 XFillRectangle (GDK_SCREEN_XDISPLAY (impl->screen), impl->xid,
469 GDK_GC_GET_XGC (gc), x, y, width, height);
471 XDrawRectangle (GDK_SCREEN_XDISPLAY (impl->screen), impl->xid,
472 GDK_GC_GET_XGC (gc), x, y, width, height);
476 gdk_x11_draw_arc (GdkDrawable *drawable,
486 GdkDrawableImplX11 *impl;
488 impl = GDK_DRAWABLE_IMPL_X11 (drawable);
492 XFillArc (GDK_SCREEN_XDISPLAY (impl->screen), impl->xid,
493 GDK_GC_GET_XGC (gc), x, y, width, height, angle1, angle2);
495 XDrawArc (GDK_SCREEN_XDISPLAY (impl->screen), impl->xid,
496 GDK_GC_GET_XGC (gc), x, y, width, height, angle1, angle2);
500 gdk_x11_draw_polygon (GdkDrawable *drawable,
508 GdkDrawableImplX11 *impl;
510 impl = GDK_DRAWABLE_IMPL_X11 (drawable);
514 (points[0].x != points[npoints-1].x || points[0].y != points[npoints-1].y))
516 tmp_npoints = npoints + 1;
517 tmp_points = g_new (XPoint, tmp_npoints);
518 tmp_points[npoints].x = points[0].x;
519 tmp_points[npoints].y = points[0].y;
523 tmp_npoints = npoints;
524 tmp_points = g_new (XPoint, tmp_npoints);
527 for (i=0; i<npoints; i++)
529 tmp_points[i].x = points[i].x;
530 tmp_points[i].y = points[i].y;
534 XFillPolygon (GDK_SCREEN_XDISPLAY (impl->screen), impl->xid,
535 GDK_GC_GET_XGC (gc), tmp_points, tmp_npoints, Complex, CoordModeOrigin);
537 XDrawLines (GDK_SCREEN_XDISPLAY (impl->screen), impl->xid,
538 GDK_GC_GET_XGC (gc), tmp_points, tmp_npoints, CoordModeOrigin);
545 * Modified by Li-Da Lho to draw 16 bits and Multibyte strings
547 * Interface changed: add "GdkFont *font" to specify font or fontset explicitely
550 gdk_x11_draw_text (GdkDrawable *drawable,
558 GdkDrawableImplX11 *impl;
561 impl = GDK_DRAWABLE_IMPL_X11 (drawable);
562 xdisplay = GDK_SCREEN_XDISPLAY (impl->screen);
564 if (font->type == GDK_FONT_FONT)
566 XFontStruct *xfont = (XFontStruct *) GDK_FONT_XFONT (font);
567 XSetFont(xdisplay, GDK_GC_GET_XGC (gc), xfont->fid);
568 if ((xfont->min_byte1 == 0) && (xfont->max_byte1 == 0))
570 XDrawString (xdisplay, impl->xid,
571 GDK_GC_GET_XGC (gc), x, y, text, text_length);
575 XDrawString16 (xdisplay, impl->xid,
576 GDK_GC_GET_XGC (gc), x, y, (XChar2b *) text, text_length / 2);
579 else if (font->type == GDK_FONT_FONTSET)
581 XFontSet fontset = (XFontSet) GDK_FONT_XFONT (font);
582 XmbDrawString (xdisplay, impl->xid,
583 fontset, GDK_GC_GET_XGC (gc), x, y, text, text_length);
586 g_error("undefined font type\n");
590 gdk_x11_draw_text_wc (GdkDrawable *drawable,
595 const GdkWChar *text,
598 GdkDrawableImplX11 *impl;
601 impl = GDK_DRAWABLE_IMPL_X11 (drawable);
602 xdisplay = GDK_SCREEN_XDISPLAY (impl->screen);
604 if (font->type == GDK_FONT_FONT)
606 XFontStruct *xfont = (XFontStruct *) GDK_FONT_XFONT (font);
609 XSetFont(xdisplay, GDK_GC_GET_XGC (gc), xfont->fid);
610 text_8bit = g_new (gchar, text_length);
611 for (i=0; i<text_length; i++) text_8bit[i] = text[i];
612 XDrawString (xdisplay, impl->xid,
613 GDK_GC_GET_XGC (gc), x, y, text_8bit, text_length);
616 else if (font->type == GDK_FONT_FONTSET)
618 if (sizeof(GdkWChar) == sizeof(wchar_t))
620 XwcDrawString (xdisplay, impl->xid,
621 (XFontSet) GDK_FONT_XFONT (font),
622 GDK_GC_GET_XGC (gc), x, y, (wchar_t *)text, text_length);
628 text_wchar = g_new (wchar_t, text_length);
629 for (i=0; i<text_length; i++) text_wchar[i] = text[i];
630 XwcDrawString (xdisplay, impl->xid,
631 (XFontSet) GDK_FONT_XFONT (font),
632 GDK_GC_GET_XGC (gc), x, y, text_wchar, text_length);
637 g_error("undefined font type\n");
641 gdk_x11_draw_drawable (GdkDrawable *drawable,
651 int src_depth = gdk_drawable_get_depth (src);
652 int dest_depth = gdk_drawable_get_depth (drawable);
653 GdkDrawableImplX11 *impl;
654 GdkDrawableImplX11 *src_impl;
656 impl = GDK_DRAWABLE_IMPL_X11 (drawable);
658 if (GDK_IS_DRAWABLE_IMPL_X11 (src))
659 src_impl = GDK_DRAWABLE_IMPL_X11 (src);
665 XCopyArea (GDK_SCREEN_XDISPLAY (impl->screen),
666 src_impl ? src_impl->xid : GDK_DRAWABLE_XID (src),
673 else if (dest_depth != 0 && src_depth == dest_depth)
675 XCopyArea (GDK_SCREEN_XDISPLAY (impl->screen),
676 src_impl ? src_impl->xid : GDK_DRAWABLE_XID (src),
684 g_warning ("Attempt to draw a drawable with depth %d to a drawable with depth %d",
685 src_depth, dest_depth);
689 gdk_x11_draw_points (GdkDrawable *drawable,
694 GdkDrawableImplX11 *impl;
696 impl = GDK_DRAWABLE_IMPL_X11 (drawable);
699 /* We special-case npoints == 1, because X will merge multiple
700 * consecutive XDrawPoint requests into a PolyPoint request
704 XDrawPoint (GDK_SCREEN_XDISPLAY (impl->screen),
707 points[0].x, points[0].y);
712 XPoint *tmp_points = g_new (XPoint, npoints);
714 for (i=0; i<npoints; i++)
716 tmp_points[i].x = points[i].x;
717 tmp_points[i].y = points[i].y;
720 XDrawPoints (GDK_SCREEN_XDISPLAY (impl->screen),
732 gdk_x11_draw_segments (GdkDrawable *drawable,
737 GdkDrawableImplX11 *impl;
739 impl = GDK_DRAWABLE_IMPL_X11 (drawable);
742 /* We special-case nsegs == 1, because X will merge multiple
743 * consecutive XDrawLine requests into a PolySegment request
747 XDrawLine (GDK_SCREEN_XDISPLAY (impl->screen), impl->xid,
748 GDK_GC_GET_XGC (gc), segs[0].x1, segs[0].y1,
749 segs[0].x2, segs[0].y2);
754 XSegment *tmp_segs = g_new (XSegment, nsegs);
756 for (i=0; i<nsegs; i++)
758 tmp_segs[i].x1 = segs[i].x1;
759 tmp_segs[i].x2 = segs[i].x2;
760 tmp_segs[i].y1 = segs[i].y1;
761 tmp_segs[i].y2 = segs[i].y2;
764 XDrawSegments (GDK_SCREEN_XDISPLAY (impl->screen),
774 gdk_x11_draw_lines (GdkDrawable *drawable,
780 XPoint *tmp_points = g_new (XPoint, npoints);
781 GdkDrawableImplX11 *impl;
783 impl = GDK_DRAWABLE_IMPL_X11 (drawable);
786 for (i=0; i<npoints; i++)
788 tmp_points[i].x = points[i].x;
789 tmp_points[i].y = points[i].y;
792 XDrawLines (GDK_SCREEN_XDISPLAY (impl->screen),
802 gdk_x11_draw_glyphs (GdkDrawable *drawable,
807 PangoGlyphString *glyphs)
809 gdk_x11_draw_glyphs_transformed (drawable, gc, NULL,
817 gdk_x11_draw_glyphs_transformed (GdkDrawable *drawable,
823 PangoGlyphString *glyphs)
825 GdkDrawableImplX11 *impl;
826 PangoRenderer *renderer;
830 impl = GDK_DRAWABLE_IMPL_X11 (drawable);
832 g_return_if_fail (PANGO_XFT_IS_FONT (font));
834 _gdk_gc_x11_get_fg_xft_color (gc, &color);
836 gdk_x11_drawable_update_xft_clip (drawable, gc);
837 draw = gdk_x11_drawable_get_xft_draw (drawable);
839 renderer = _gdk_x11_renderer_get (drawable, gc);
841 pango_renderer_set_matrix (renderer, matrix);
842 pango_renderer_draw_glyphs (renderer, font, glyphs, x, y);
844 pango_renderer_set_matrix (renderer, NULL);
848 gdk_x11_draw_image (GdkDrawable *drawable,
858 GdkDrawableImplX11 *impl;
860 impl = GDK_DRAWABLE_IMPL_X11 (drawable);
863 if (image->type == GDK_IMAGE_SHARED)
864 XShmPutImage (GDK_SCREEN_XDISPLAY (impl->screen), impl->xid,
865 GDK_GC_GET_XGC (gc), GDK_IMAGE_XIMAGE (image),
866 xsrc, ysrc, xdest, ydest, width, height, False);
869 XPutImage (GDK_SCREEN_XDISPLAY (impl->screen), impl->xid,
870 GDK_GC_GET_XGC (gc), GDK_IMAGE_XIMAGE (image),
871 xsrc, ysrc, xdest, ydest, width, height);
875 gdk_x11_get_depth (GdkDrawable *drawable)
877 /* This is a bit bogus but I'm not sure the other way is better */
879 return gdk_drawable_get_depth (GDK_DRAWABLE_IMPL_X11 (drawable)->wrapper);
883 get_impl_drawable (GdkDrawable *drawable)
885 if (GDK_IS_WINDOW (drawable))
886 return ((GdkWindowObject *)drawable)->impl;
887 else if (GDK_IS_PIXMAP (drawable))
888 return ((GdkPixmapObject *)drawable)->impl;
891 g_warning (G_STRLOC " drawable is not a pixmap or window");
897 gdk_x11_get_screen (GdkDrawable *drawable)
899 if (GDK_IS_DRAWABLE_IMPL_X11 (drawable))
900 return GDK_DRAWABLE_IMPL_X11 (drawable)->screen;
902 return GDK_DRAWABLE_IMPL_X11 (get_impl_drawable (drawable))->screen;
906 gdk_x11_get_visual (GdkDrawable *drawable)
908 return gdk_drawable_get_visual (GDK_DRAWABLE_IMPL_X11 (drawable)->wrapper);
912 * gdk_x11_drawable_get_xdisplay:
913 * @drawable: a #GdkDrawable.
915 * Returns the display of a #GdkDrawable.
917 * Return value: an Xlib <type>Display*</type>.
920 gdk_x11_drawable_get_xdisplay (GdkDrawable *drawable)
922 if (GDK_IS_DRAWABLE_IMPL_X11 (drawable))
923 return GDK_SCREEN_XDISPLAY (GDK_DRAWABLE_IMPL_X11 (drawable)->screen);
925 return GDK_SCREEN_XDISPLAY (GDK_DRAWABLE_IMPL_X11 (get_impl_drawable (drawable))->screen);
929 * gdk_x11_drawable_get_xid:
930 * @drawable: a #GdkDrawable.
932 * Returns the X resource (window or pixmap) belonging to a #GdkDrawable.
934 * Return value: the ID of @drawable's X resource.
937 gdk_x11_drawable_get_xid (GdkDrawable *drawable)
941 if (GDK_IS_WINDOW (drawable))
942 impl = ((GdkPixmapObject *)drawable)->impl;
943 else if (GDK_IS_PIXMAP (drawable))
944 impl = ((GdkPixmapObject *)drawable)->impl;
947 g_warning (G_STRLOC " drawable is not a pixmap or window");
951 return ((GdkDrawableImplX11 *)impl)->xid;
954 /* Code for accelerated alpha compositing using the RENDER extension.
955 * It's a bit long because there are lots of possibilities for
956 * what's the fastest depending on the available picture formats,
957 * whether we can used shared pixmaps, etc.
960 static GdkX11FormatType
961 select_format (GdkDisplay *display,
962 XRenderPictFormat **format,
963 XRenderPictFormat **mask)
965 Display *xdisplay = GDK_DISPLAY_XDISPLAY (display);
966 XRenderPictFormat pf;
968 if (!_gdk_x11_have_render (display))
969 return GDK_X11_FORMAT_NONE;
971 /* Look for a 32-bit xRGB and Axxx formats that exactly match the
972 * in memory data format. We can use them as pixmap and mask
973 * to deal with non-premultiplied data.
976 pf.type = PictTypeDirect;
978 pf.direct.redMask = 0xff;
979 pf.direct.greenMask = 0xff;
980 pf.direct.blueMask = 0xff;
982 pf.direct.alphaMask = 0;
983 if (ImageByteOrder (xdisplay) == LSBFirst)
994 pf.direct.green = 16;
998 *format = XRenderFindFormat (xdisplay,
999 (PictFormatType | PictFormatDepth |
1000 PictFormatRedMask | PictFormatRed |
1001 PictFormatGreenMask | PictFormatGreen |
1002 PictFormatBlueMask | PictFormatBlue |
1003 PictFormatAlphaMask),
1007 pf.direct.alphaMask = 0xff;
1008 if (ImageByteOrder (xdisplay) == LSBFirst)
1011 pf.direct.alpha = 24;
1015 pf.direct.alpha = 0;
1018 *mask = XRenderFindFormat (xdisplay,
1019 (PictFormatType | PictFormatDepth |
1020 PictFormatAlphaMask | PictFormatAlpha),
1024 if (*format && *mask)
1025 return GDK_X11_FORMAT_EXACT_MASK;
1027 /* OK, that failed, now look for xRGB and Axxx formats in
1028 * RENDER's preferred order
1030 pf.direct.alphaMask = 0;
1033 pf.direct.green = 8;
1036 *format = XRenderFindFormat (xdisplay,
1037 (PictFormatType | PictFormatDepth |
1038 PictFormatRedMask | PictFormatRed |
1039 PictFormatGreenMask | PictFormatGreen |
1040 PictFormatBlueMask | PictFormatBlue |
1041 PictFormatAlphaMask),
1045 pf.direct.alphaMask = 0xff;
1046 pf.direct.alpha = 24;
1048 *mask = XRenderFindFormat (xdisplay,
1049 (PictFormatType | PictFormatDepth |
1050 PictFormatAlphaMask | PictFormatAlpha),
1054 if (*format && *mask)
1055 return GDK_X11_FORMAT_ARGB_MASK;
1057 /* Finally, if neither of the above worked, fall back to
1058 * looking for combined ARGB -- we'll premultiply ourselves.
1061 pf.type = PictTypeDirect;
1064 pf.direct.green = 8;
1066 pf.direct.alphaMask = 0xff;
1067 pf.direct.alpha = 24;
1069 *format = XRenderFindFormat (xdisplay,
1070 (PictFormatType | PictFormatDepth |
1071 PictFormatRedMask | PictFormatRed |
1072 PictFormatGreenMask | PictFormatGreen |
1073 PictFormatBlueMask | PictFormatBlue |
1074 PictFormatAlphaMask | PictFormatAlpha),
1080 return GDK_X11_FORMAT_ARGB;
1082 return GDK_X11_FORMAT_NONE;
1087 list_formats (XRenderPictFormat *pf)
1093 XRenderPictFormat *pf = XRenderFindFormat (impl->xdisplay, 0, NULL, i);
1096 g_print ("%2d R-%#06x/%#06x G-%#06x/%#06x B-%#06x/%#06x A-%#06x/%#06x\n",
1101 pf->direct.greenMask,
1103 pf->direct.blueMask,
1105 pf->direct.alphaMask);
1114 _gdk_x11_convert_to_format (guchar *src_buf,
1117 gint dest_rowstride,
1118 GdkX11FormatType dest_format,
1119 GdkByteOrder dest_byteorder,
1125 for (i=0; i < height; i++)
1127 switch (dest_format)
1129 case GDK_X11_FORMAT_EXACT_MASK:
1131 memcpy (dest_buf + i * dest_rowstride,
1132 src_buf + i * src_rowstride,
1136 case GDK_X11_FORMAT_ARGB_MASK:
1138 guchar *row = src_buf + i * src_rowstride;
1139 if (((gsize)row & 3) != 0)
1142 guint32 *q = (guint32 *)(dest_buf + i * dest_rowstride);
1143 guchar *end = p + 4 * width;
1147 *q = (p[3] << 24) | (p[0] << 16) | (p[1] << 8) | p[2];
1154 guint32 *p = (guint32 *)row;
1155 guint32 *q = (guint32 *)(dest_buf + i * dest_rowstride);
1156 guint32 *end = p + width;
1158 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
1159 if (dest_byteorder == GDK_LSB_FIRST)
1165 *q = ( (*p & 0xff00ff00) |
1166 ((*p & 0x000000ff) << 16) |
1167 ((*p & 0x00ff0000) >> 16));
1178 *q = (((*p & 0xff000000) >> 24) |
1179 ((*p & 0x00ffffff) << 8));
1184 #else /* G_BYTE_ORDER == G_BIG_ENDIAN */
1185 if (dest_byteorder == GDK_LSB_FIRST)
1191 *q = ( (*p & 0x00ff00ff) |
1192 ((*p & 0x0000ff00) << 16) |
1193 ((*p & 0xff000000) >> 16));
1204 *q = (((*p & 0xffffff00) >> 8) |
1205 ((*p & 0x000000ff) << 24));
1210 #endif /* G_BYTE_ORDER*/
1214 case GDK_X11_FORMAT_ARGB:
1216 guchar *p = (src_buf + i * src_rowstride);
1217 guchar *q = (dest_buf + i * dest_rowstride);
1218 guchar *end = p + 4 * width;
1221 #define MULT(d,c,a,t) G_STMT_START { t = c * a; d = ((t >> 8) + t) >> 8; } G_STMT_END
1223 if (dest_byteorder == GDK_LSB_FIRST)
1227 MULT(q[0], p[2], p[3], t1);
1228 MULT(q[1], p[1], p[3], t2);
1229 MULT(q[2], p[0], p[3], t3);
1240 MULT(q[1], p[0], p[3], t1);
1241 MULT(q[2], p[1], p[3], t2);
1242 MULT(q[3], p[2], p[3], t3);
1250 case GDK_X11_FORMAT_NONE:
1251 g_assert_not_reached ();
1258 draw_with_images (GdkDrawable *drawable,
1260 GdkX11FormatType format_type,
1261 XRenderPictFormat *format,
1262 XRenderPictFormat *mask_format,
1270 GdkScreen *screen = GDK_DRAWABLE_IMPL_X11 (drawable)->screen;
1271 Display *xdisplay = GDK_SCREEN_XDISPLAY (screen);
1277 Picture mask = None;
1280 pix = gdk_pixmap_new (gdk_screen_get_root_window (screen), width, height, 32);
1282 pict = XRenderCreatePicture (xdisplay,
1283 GDK_PIXMAP_XID (pix),
1286 mask = XRenderCreatePicture (xdisplay,
1287 GDK_PIXMAP_XID (pix),
1288 mask_format, 0, NULL);
1290 dest_pict = gdk_x11_drawable_get_picture (drawable);
1292 pix_gc = _gdk_drawable_get_scratch_gc (pix, FALSE);
1294 for (y0 = 0; y0 < height; y0 += GDK_SCRATCH_IMAGE_HEIGHT)
1296 gint height1 = MIN (height - y0, GDK_SCRATCH_IMAGE_HEIGHT);
1297 for (x0 = 0; x0 < width; x0 += GDK_SCRATCH_IMAGE_WIDTH)
1301 gint width1 = MIN (width - x0, GDK_SCRATCH_IMAGE_WIDTH);
1303 image = _gdk_image_get_scratch (screen, width1, height1, 32, &xs0, &ys0);
1305 _gdk_x11_convert_to_format (src_rgb + y0 * src_rowstride + 4 * x0, src_rowstride,
1306 (guchar *)image->mem + ys0 * image->bpl + xs0 * image->bpp, image->bpl,
1307 format_type, image->byte_order,
1310 gdk_draw_image (pix, pix_gc,
1311 image, xs0, ys0, x0, y0, width1, height1);
1315 XRenderComposite (xdisplay, PictOpOver, pict, mask, dest_pict,
1316 0, 0, 0, 0, dest_x, dest_y, width, height);
1318 XRenderFreePicture (xdisplay, pict);
1320 XRenderFreePicture (xdisplay, mask);
1322 g_object_unref (pix);
1325 typedef struct _ShmPixmapInfo ShmPixmapInfo;
1327 struct _ShmPixmapInfo
1335 /* Returns FALSE if we can't get a shm pixmap */
1337 get_shm_pixmap_for_image (Display *xdisplay,
1339 XRenderPictFormat *format,
1340 XRenderPictFormat *mask_format,
1345 ShmPixmapInfo *info;
1347 if (image->type != GDK_IMAGE_SHARED)
1350 info = g_object_get_data (G_OBJECT (image), "gdk-x11-shm-pixmap");
1353 *pix = _gdk_x11_image_get_shm_pixmap (image);
1358 info = g_new (ShmPixmapInfo, 1);
1361 info->pict = XRenderCreatePicture (xdisplay, info->pix,
1364 info->mask = XRenderCreatePicture (xdisplay, info->pix,
1365 mask_format, 0, NULL);
1369 g_object_set_data (G_OBJECT (image), "gdk-x11-shm-pixmap", info);
1380 /* Returns FALSE if drawing with ShmPixmaps is not possible */
1382 draw_with_pixmaps (GdkDrawable *drawable,
1384 GdkX11FormatType format_type,
1385 XRenderPictFormat *format,
1386 XRenderPictFormat *mask_format,
1394 Display *xdisplay = GDK_SCREEN_XDISPLAY (GDK_DRAWABLE_IMPL_X11 (drawable)->screen);
1399 Picture mask = None;
1402 dest_pict = gdk_x11_drawable_get_picture (drawable);
1404 for (y0 = 0; y0 < height; y0 += GDK_SCRATCH_IMAGE_HEIGHT)
1406 gint height1 = MIN (height - y0, GDK_SCRATCH_IMAGE_HEIGHT);
1407 for (x0 = 0; x0 < width; x0 += GDK_SCRATCH_IMAGE_WIDTH)
1411 gint width1 = MIN (width - x0, GDK_SCRATCH_IMAGE_WIDTH);
1413 image = _gdk_image_get_scratch (GDK_DRAWABLE_IMPL_X11 (drawable)->screen,
1414 width1, height1, 32, &xs0, &ys0);
1415 if (!get_shm_pixmap_for_image (xdisplay, image, format, mask_format, &pix, &pict, &mask))
1418 _gdk_x11_convert_to_format (src_rgb + y0 * src_rowstride + 4 * x0, src_rowstride,
1419 (guchar *)image->mem + ys0 * image->bpl + xs0 * image->bpp, image->bpl,
1420 format_type, image->byte_order,
1423 XRenderComposite (xdisplay, PictOpOver, pict, mask, dest_pict,
1424 xs0, ys0, xs0, ys0, x0 + dest_x, y0 + dest_y,
1434 gdk_x11_draw_pixbuf (GdkDrawable *drawable,
1443 GdkRgbDither dither,
1447 GdkX11FormatType format_type;
1448 XRenderPictFormat *format, *mask_format;
1451 gboolean use_pixmaps = TRUE;
1452 #endif /* USE_SHM */
1454 format_type = select_format (gdk_drawable_get_display (drawable),
1455 &format, &mask_format);
1457 if (format_type == GDK_X11_FORMAT_NONE ||
1458 !gdk_pixbuf_get_has_alpha (pixbuf) ||
1459 gdk_drawable_get_depth (drawable) == 1 ||
1460 (dither == GDK_RGB_DITHER_MAX && gdk_drawable_get_depth (drawable) != 24) ||
1461 gdk_x11_drawable_get_picture (drawable) == None)
1463 GdkDrawable *wrapper = GDK_DRAWABLE_IMPL_X11 (drawable)->wrapper;
1464 GDK_DRAWABLE_CLASS (parent_class)->draw_pixbuf (wrapper, gc, pixbuf,
1465 src_x, src_y, dest_x, dest_y,
1467 dither, x_dither, y_dither);
1471 gdk_x11_drawable_update_xft_clip (drawable, gc);
1473 rowstride = gdk_pixbuf_get_rowstride (pixbuf);
1478 if (!draw_with_pixmaps (drawable, gc,
1479 format_type, format, mask_format,
1480 gdk_pixbuf_get_pixels (pixbuf) + src_y * rowstride + src_x * 4,
1482 dest_x, dest_y, width, height))
1483 use_pixmaps = FALSE;
1487 #endif /* USE_SHM */
1488 draw_with_images (drawable, gc,
1489 format_type, format, mask_format,
1490 gdk_pixbuf_get_pixels (pixbuf) + src_y * rowstride + src_x * 4,
1492 dest_x, dest_y, width, height);
1496 gdk_x11_draw_trapezoids (GdkDrawable *drawable,
1498 GdkTrapezoid *trapezoids,
1501 GdkScreen *screen = GDK_DRAWABLE_IMPL_X11 (drawable)->screen;
1502 GdkDisplay *display = gdk_screen_get_display (screen);
1503 XTrapezoid *xtrapezoids;
1506 if (!_gdk_x11_have_render (display))
1508 GdkDrawable *wrapper = GDK_DRAWABLE_IMPL_X11 (drawable)->wrapper;
1509 GDK_DRAWABLE_CLASS (parent_class)->draw_trapezoids (wrapper, gc,
1510 trapezoids, n_trapezoids);
1514 xtrapezoids = g_new (XTrapezoid, n_trapezoids);
1516 for (i = 0; i < n_trapezoids; i++)
1518 xtrapezoids[i].top = XDoubleToFixed (trapezoids[i].y1);
1519 xtrapezoids[i].bottom = XDoubleToFixed (trapezoids[i].y2);
1520 xtrapezoids[i].left.p1.x = XDoubleToFixed (trapezoids[i].x11);
1521 xtrapezoids[i].left.p1.y = XDoubleToFixed (trapezoids[i].y1);
1522 xtrapezoids[i].left.p2.x = XDoubleToFixed (trapezoids[i].x12);
1523 xtrapezoids[i].left.p2.y = XDoubleToFixed (trapezoids[i].y2);
1524 xtrapezoids[i].right.p1.x = XDoubleToFixed (trapezoids[i].x21);
1525 xtrapezoids[i].right.p1.y = XDoubleToFixed (trapezoids[i].y1);
1526 xtrapezoids[i].right.p2.x = XDoubleToFixed (trapezoids[i].x22);
1527 xtrapezoids[i].right.p2.y = XDoubleToFixed (trapezoids[i].y2);
1530 _gdk_x11_drawable_draw_xtrapezoids (drawable, gc,
1531 xtrapezoids, n_trapezoids);
1533 g_free (xtrapezoids);
1537 * gdk_draw_rectangle_alpha_libgtk_only:
1538 * @drawable: The #GdkDrawable to draw on
1539 * @x: the x coordinate of the left edge of the rectangle.
1540 * @y: the y coordinate of the top edge of the rectangle.
1541 * @width: the width of the rectangle.
1542 * @height: the height of the rectangle.
1544 * @alpha: The alpha value.
1546 * Tries to draw a filled alpha blended rectangle using the window
1547 * system's native routines. This is not public API and must not be
1548 * used by applications.
1550 * Return value: TRUE if the rectangle could be drawn, FALSE
1554 gdk_draw_rectangle_alpha_libgtk_only (GdkDrawable *drawable,
1563 XRenderColor render_color;
1565 int x_offset, y_offset;
1566 GdkDrawable *real_drawable, *impl;
1568 g_return_val_if_fail (color != NULL, FALSE);
1570 if (!GDK_IS_WINDOW (drawable))
1573 if (!_gdk_x11_have_render (gdk_drawable_get_display (drawable)))
1576 gdk_window_get_internal_paint_info (GDK_WINDOW (drawable),
1578 &x_offset, &y_offset);
1580 impl = ((GdkWindowObject *)real_drawable)->impl;
1582 pict = gdk_x11_drawable_get_picture (impl);
1587 xdisplay = GDK_DISPLAY_XDISPLAY (gdk_drawable_get_display (drawable));
1589 render_color.alpha = alpha;
1590 render_color.red = (guint32)color->red * render_color.alpha / 0xffff;
1591 render_color.green = (guint32)color->green * render_color.alpha / 0xffff;
1592 render_color.blue = (guint32)color->blue * render_color.alpha / 0xffff;
1594 XRenderFillRectangle (xdisplay,
1595 PictOpOver, pict, &render_color,
1596 x - x_offset, y - y_offset,
1602 _gdk_x11_drawable_draw_xtrapezoids (GdkDrawable *drawable,
1604 XTrapezoid *xtrapezoids,
1607 GdkScreen *screen = GDK_DRAWABLE_IMPL_X11 (drawable)->screen;
1608 GdkDisplay *display = gdk_screen_get_display (screen);
1609 GdkDisplayX11 *x11display = GDK_DISPLAY_X11 (display);
1613 if (!_gdk_x11_have_render (display))
1615 /* This is the case of drawing the borders of the unknown glyph box
1616 * without render on the display, we need to feed it back to
1617 * fallback code. Not efficient, but doesn't matter.
1619 GdkTrapezoid *trapezoids = g_new (GdkTrapezoid, n_trapezoids);
1622 for (i = 0; i < n_trapezoids; i++)
1624 trapezoids[i].y1 = XFixedToDouble (xtrapezoids[i].top);
1625 trapezoids[i].y2 = XFixedToDouble (xtrapezoids[i].bottom);
1626 trapezoids[i].x11 = XFixedToDouble (xtrapezoids[i].left.p1.x);
1627 trapezoids[i].x12 = XFixedToDouble (xtrapezoids[i].left.p2.x);
1628 trapezoids[i].x21 = XFixedToDouble (xtrapezoids[i].right.p1.x);
1629 trapezoids[i].x22 = XFixedToDouble (xtrapezoids[i].right.p2.x);
1632 gdk_x11_draw_trapezoids (drawable, gc, trapezoids, n_trapezoids);
1633 g_free (trapezoids);
1638 draw = gdk_x11_drawable_get_xft_draw (drawable);
1640 if (!x11display->mask_format)
1641 x11display->mask_format = XRenderFindStandardFormat (x11display->xdisplay,
1644 XRenderCompositeTrapezoids (x11display->xdisplay, PictOpOver,
1645 _gdk_x11_gc_get_fg_picture (gc),
1646 XftDrawPicture (draw),
1647 x11display->mask_format,
1648 - gc->ts_x_origin, - gc->ts_y_origin,
1649 xtrapezoids, n_trapezoids);
1653 _gdk_x11_drawable_draw_xft_glyphs (GdkDrawable *drawable,
1656 XftGlyphSpec *glyphs,
1659 GdkScreen *screen = GDK_DRAWABLE_IMPL_X11 (drawable)->screen;
1660 GdkDisplay *display = gdk_screen_get_display (screen);
1661 GdkDisplayX11 *x11display = GDK_DISPLAY_X11 (display);
1663 XftDraw *draw = gdk_x11_drawable_get_xft_draw (drawable);
1665 if (_gdk_x11_have_render (display))
1667 XftGlyphSpecRender (x11display->xdisplay, PictOpOver,
1668 _gdk_x11_gc_get_fg_picture (gc),
1670 XftDrawPicture (draw),
1671 - gc->ts_x_origin, - gc->ts_y_origin,
1678 _gdk_gc_x11_get_fg_xft_color (gc, &color);
1679 XftDrawGlyphSpec (draw, &color, xft_font, glyphs, n_glyphs);