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/.
28 #include "gdkregion-generic.h"
30 #include <pango/pangox.h>
34 #include <pango/pangoxft.h>
38 #include <string.h> /* for memcpy() */
40 #if defined (HAVE_IPC_H) && defined (HAVE_SHM_H) && defined (HAVE_XSHM_H)
45 #include <X11/extensions/XShm.h>
48 #include "gdkprivate-x11.h"
49 #include "gdkdrawable-x11.h"
50 #include "gdkpixmap-x11.h"
51 #include "gdkscreen-x11.h"
52 #include "gdkdisplay-x11.h"
54 static void gdk_x11_draw_rectangle (GdkDrawable *drawable,
61 static void gdk_x11_draw_arc (GdkDrawable *drawable,
70 static void gdk_x11_draw_polygon (GdkDrawable *drawable,
75 static void gdk_x11_draw_text (GdkDrawable *drawable,
82 static void gdk_x11_draw_text_wc (GdkDrawable *drawable,
89 static void gdk_x11_draw_drawable (GdkDrawable *drawable,
98 static void gdk_x11_draw_points (GdkDrawable *drawable,
102 static void gdk_x11_draw_segments (GdkDrawable *drawable,
106 static void gdk_x11_draw_lines (GdkDrawable *drawable,
110 static void gdk_x11_draw_glyphs (GdkDrawable *drawable,
115 PangoGlyphString *glyphs);
116 static void gdk_x11_draw_image (GdkDrawable *drawable,
126 static void gdk_x11_draw_pixbuf (GdkDrawable *drawable,
138 #endif /* HAVE_XFT */
140 static void gdk_x11_set_colormap (GdkDrawable *drawable,
141 GdkColormap *colormap);
143 static GdkColormap* gdk_x11_get_colormap (GdkDrawable *drawable);
144 static gint gdk_x11_get_depth (GdkDrawable *drawable);
145 static GdkScreen * gdk_x11_get_screen (GdkDrawable *drawable);
146 static GdkVisual* gdk_x11_get_visual (GdkDrawable *drawable);
148 static void gdk_drawable_impl_x11_class_init (GdkDrawableImplX11Class *klass);
150 static void gdk_drawable_impl_x11_finalize (GObject *object);
152 static gpointer parent_class = NULL;
155 _gdk_drawable_impl_x11_get_type (void)
157 static GType object_type = 0;
161 static const GTypeInfo object_info =
163 sizeof (GdkDrawableImplX11Class),
164 (GBaseInitFunc) NULL,
165 (GBaseFinalizeFunc) NULL,
166 (GClassInitFunc) gdk_drawable_impl_x11_class_init,
167 NULL, /* class_finalize */
168 NULL, /* class_data */
169 sizeof (GdkDrawableImplX11),
171 (GInstanceInitFunc) NULL,
174 object_type = g_type_register_static (GDK_TYPE_DRAWABLE,
175 "GdkDrawableImplX11",
183 gdk_drawable_impl_x11_class_init (GdkDrawableImplX11Class *klass)
185 GdkDrawableClass *drawable_class = GDK_DRAWABLE_CLASS (klass);
186 GObjectClass *object_class = G_OBJECT_CLASS (klass);
188 parent_class = g_type_class_peek_parent (klass);
190 object_class->finalize = gdk_drawable_impl_x11_finalize;
192 drawable_class->create_gc = _gdk_x11_gc_new;
193 drawable_class->draw_rectangle = gdk_x11_draw_rectangle;
194 drawable_class->draw_arc = gdk_x11_draw_arc;
195 drawable_class->draw_polygon = gdk_x11_draw_polygon;
196 drawable_class->draw_text = gdk_x11_draw_text;
197 drawable_class->draw_text_wc = gdk_x11_draw_text_wc;
198 drawable_class->draw_drawable = gdk_x11_draw_drawable;
199 drawable_class->draw_points = gdk_x11_draw_points;
200 drawable_class->draw_segments = gdk_x11_draw_segments;
201 drawable_class->draw_lines = gdk_x11_draw_lines;
202 drawable_class->draw_glyphs = gdk_x11_draw_glyphs;
203 drawable_class->draw_image = gdk_x11_draw_image;
205 drawable_class->draw_pixbuf = gdk_x11_draw_pixbuf;
206 #endif /* HAVE_XFT */
208 drawable_class->set_colormap = gdk_x11_set_colormap;
209 drawable_class->get_colormap = gdk_x11_get_colormap;
211 drawable_class->get_depth = gdk_x11_get_depth;
212 drawable_class->get_screen = gdk_x11_get_screen;
213 drawable_class->get_visual = gdk_x11_get_visual;
215 drawable_class->_copy_to_image = _gdk_x11_copy_to_image;
219 gdk_drawable_impl_x11_finalize (GObject *object)
221 gdk_drawable_set_colormap (GDK_DRAWABLE (object), NULL);
223 G_OBJECT_CLASS (parent_class)->finalize (object);
228 _gdk_x11_have_render (GdkDisplay *display)
230 /* This check is cheap, but if we have to do version checks, we will
231 * need to cache the result since version checks are round-trip
233 int event_base, error_base;
235 return XRenderQueryExtension (GDK_DISPLAY_XDISPLAY (display),
236 &event_base, &error_base);
241 gdk_x11_drawable_get_xft_draw (GdkDrawable *drawable)
243 GdkDrawableImplX11 *impl = GDK_DRAWABLE_IMPL_X11 (drawable);
245 if (impl->xft_draw == NULL)
247 GdkColormap *colormap = gdk_drawable_get_colormap (drawable);
252 g_warning ("Using Xft rendering requires the drawable argument to\n"
253 "have a specified colormap. All windows have a colormap,\n"
254 "however, pixmaps only have colormap by default if they\n"
255 "were created with a non-NULL window argument. Otherwise\n"
256 "a colormap must be set on them with gdk_drawable_set_colormap");
260 visual = gdk_colormap_get_visual (colormap);
262 impl->xft_draw = XftDrawCreate (GDK_SCREEN_XDISPLAY (impl->screen), impl->xid,
263 GDK_VISUAL_XVISUAL (visual), GDK_COLORMAP_XCOLORMAP (colormap));
266 return impl->xft_draw;
270 gdk_x11_drawable_get_picture (GdkDrawable *drawable)
272 XftDraw *draw = gdk_x11_drawable_get_xft_draw (drawable);
274 return draw ? XftDrawPicture (draw) : None;
278 gdk_x11_drawable_update_xft_clip (GdkDrawable *drawable,
281 GdkGCX11 *gc_private = gc ? GDK_GC_X11 (gc) : NULL;
282 XftDraw *xft_draw = gdk_x11_drawable_get_xft_draw (drawable);
284 if (gc && gc_private->clip_region)
286 GdkRegionBox *boxes = gc_private->clip_region->rects;
287 gint n_boxes = gc_private->clip_region->numRects;
288 #if 0 /* Until XftDrawSetClipRectangles is there */
289 XRectangle *rects = g_new (XRectangle, n_boxes);
292 for (i=0; i < n_boxes; i++)
294 rects[i].x = CLAMP (boxes[i].x1 + gc->clip_x_origin, G_MINSHORT, G_MAXSHORT);
295 rects[i].y = CLAMP (boxes[i].y1 + gc->clip_y_origin, G_MINSHORT, G_MAXSHORT);
296 rects[i].width = CLAMP (boxes[i].x2 + gc->clip_x_origin, G_MINSHORT, G_MAXSHORT) - rects[i].x;
297 rects[i].height = CLAMP (boxes[i].y2 + gc->clip_y_origin, G_MINSHORT, G_MAXSHORT) - rects[i].y;
299 XftDrawSetClipRectangles (xft_draw, 0, 0, rects, n_boxes);
303 Region xregion = XCreateRegion ();
306 for (i=0; i < n_boxes; i++)
310 rect.x = CLAMP (boxes[i].x1 + gc->clip_x_origin, G_MINSHORT, G_MAXSHORT);
311 rect.y = CLAMP (boxes[i].y1 + gc->clip_y_origin, G_MINSHORT, G_MAXSHORT);
312 rect.width = CLAMP (boxes[i].x2 + gc->clip_x_origin, G_MINSHORT, G_MAXSHORT) - rect.x;
313 rect.height = CLAMP (boxes[i].y2 + gc->clip_y_origin, G_MINSHORT, G_MAXSHORT) - rect.y;
315 XUnionRectWithRegion (&rect, xregion, xregion);
318 XftDrawSetClip (xft_draw, xregion);
319 XDestroyRegion (xregion);
324 XftDrawSetClip (xft_draw, NULL);
328 #else /* !HAVE_XFT2 */
331 gdk_x11_drawable_get_picture (GdkDrawable *drawable)
333 GdkDrawableImplX11 *impl = GDK_DRAWABLE_IMPL_X11 (drawable);
335 if (!_gdk_x11_have_render (gdk_drawable_get_display (drawable)))
338 if (impl->picture == None)
340 GdkVisual *visual = gdk_drawable_get_visual (drawable);
341 XRenderPictFormat *format;
345 g_warning ("Using Xft rendering requires the drawable argument to\n"
346 "have a specified colormap. All windows have a colormap,\n"
347 "however, pixmaps only have colormap by default if they\n"
348 "were created with a non-NULL window argument. Otherwise\n"
349 "a colormap must be set on them with gdk_drawable_set_colormap");
353 format = XRenderFindVisualFormat (GDK_SCREEN_XDISPLAY (impl->screen),
354 gdk_x11_visual_get_xvisual(visual));
356 impl->picture = XRenderCreatePicture (GDK_SCREEN_XDISPLAY (impl->screen),
357 impl->xid, format, 0, NULL);
360 return impl->picture;
364 gdk_x11_drawable_update_xft_clip (GdkDrawable *drawable,
367 GdkGCX11 *gc_private = gc ? GDK_GC_X11 (gc) : NULL;
368 GdkDrawableImplX11 *impl = GDK_DRAWABLE_IMPL_X11 (drawable);
369 Picture picture = gdk_x11_drawable_get_picture (drawable);
371 if (gc && gc_private->clip_region)
373 GdkRegionBox *boxes = gc_private->clip_region->rects;
374 gint n_boxes = gc_private->clip_region->numRects;
375 XRectangle *rects = g_new (XRectangle, n_boxes);
378 for (i=0; i < n_boxes; i++)
380 rects[i].x = CLAMP (boxes[i].x1 + gc->clip_x_origin, G_MINSHORT, G_MAXSHORT);
381 rects[i].y = CLAMP (boxes[i].y1 + gc->clip_y_origin, G_MINSHORT, G_MAXSHORT);
382 rects[i].width = CLAMP (boxes[i].x2 + gc->clip_x_origin, G_MINSHORT, G_MAXSHORT) - rects[i].x;
383 rects[i].height = CLAMP (boxes[i].y2 + gc->clip_y_origin, G_MINSHORT, G_MAXSHORT) - rects[i].y;
386 XRenderSetPictureClipRectangles (GDK_SCREEN_XDISPLAY (impl->screen),
387 picture, 0, 0, rects, n_boxes);
393 XRenderPictureAttributes pa;
395 XRenderChangePicture (GDK_SCREEN_XDISPLAY (impl->screen),
396 picture, CPClipMask, &pa);
399 #endif /* HAVE_XFT2 */
401 #endif /* HAVE_XFT */
403 /*****************************************************
404 * X11 specific implementations of generic functions *
405 *****************************************************/
408 gdk_x11_get_colormap (GdkDrawable *drawable)
410 GdkDrawableImplX11 *impl;
412 impl = GDK_DRAWABLE_IMPL_X11 (drawable);
414 return impl->colormap;
418 gdk_x11_set_colormap (GdkDrawable *drawable,
419 GdkColormap *colormap)
421 GdkDrawableImplX11 *impl;
423 impl = GDK_DRAWABLE_IMPL_X11 (drawable);
425 if (impl->colormap == colormap)
429 g_object_unref (impl->colormap);
430 impl->colormap = colormap;
432 g_object_ref (impl->colormap);
439 gdk_x11_draw_rectangle (GdkDrawable *drawable,
447 GdkDrawableImplX11 *impl;
449 impl = GDK_DRAWABLE_IMPL_X11 (drawable);
452 XFillRectangle (GDK_SCREEN_XDISPLAY (impl->screen), impl->xid,
453 GDK_GC_GET_XGC (gc), x, y, width, height);
455 XDrawRectangle (GDK_SCREEN_XDISPLAY (impl->screen), impl->xid,
456 GDK_GC_GET_XGC (gc), x, y, width, height);
460 gdk_x11_draw_arc (GdkDrawable *drawable,
470 GdkDrawableImplX11 *impl;
472 impl = GDK_DRAWABLE_IMPL_X11 (drawable);
476 XFillArc (GDK_SCREEN_XDISPLAY (impl->screen), impl->xid,
477 GDK_GC_GET_XGC (gc), x, y, width, height, angle1, angle2);
479 XDrawArc (GDK_SCREEN_XDISPLAY (impl->screen), impl->xid,
480 GDK_GC_GET_XGC (gc), x, y, width, height, angle1, angle2);
484 gdk_x11_draw_polygon (GdkDrawable *drawable,
492 GdkDrawableImplX11 *impl;
494 impl = GDK_DRAWABLE_IMPL_X11 (drawable);
498 (points[0].x != points[npoints-1].x || points[0].y != points[npoints-1].y))
500 tmp_npoints = npoints + 1;
501 tmp_points = g_new (XPoint, tmp_npoints);
502 tmp_points[npoints].x = points[0].x;
503 tmp_points[npoints].y = points[0].y;
507 tmp_npoints = npoints;
508 tmp_points = g_new (XPoint, tmp_npoints);
511 for (i=0; i<npoints; i++)
513 tmp_points[i].x = points[i].x;
514 tmp_points[i].y = points[i].y;
518 XFillPolygon (GDK_SCREEN_XDISPLAY (impl->screen), impl->xid,
519 GDK_GC_GET_XGC (gc), tmp_points, tmp_npoints, Complex, CoordModeOrigin);
521 XDrawLines (GDK_SCREEN_XDISPLAY (impl->screen), impl->xid,
522 GDK_GC_GET_XGC (gc), tmp_points, tmp_npoints, CoordModeOrigin);
529 * Modified by Li-Da Lho to draw 16 bits and Multibyte strings
531 * Interface changed: add "GdkFont *font" to specify font or fontset explicitely
534 gdk_x11_draw_text (GdkDrawable *drawable,
542 GdkDrawableImplX11 *impl;
545 impl = GDK_DRAWABLE_IMPL_X11 (drawable);
546 xdisplay = GDK_SCREEN_XDISPLAY (impl->screen);
548 if (font->type == GDK_FONT_FONT)
550 XFontStruct *xfont = (XFontStruct *) GDK_FONT_XFONT (font);
551 XSetFont(xdisplay, GDK_GC_GET_XGC (gc), xfont->fid);
552 if ((xfont->min_byte1 == 0) && (xfont->max_byte1 == 0))
554 XDrawString (xdisplay, impl->xid,
555 GDK_GC_GET_XGC (gc), x, y, text, text_length);
559 XDrawString16 (xdisplay, impl->xid,
560 GDK_GC_GET_XGC (gc), x, y, (XChar2b *) text, text_length / 2);
563 else if (font->type == GDK_FONT_FONTSET)
565 XFontSet fontset = (XFontSet) GDK_FONT_XFONT (font);
566 XmbDrawString (xdisplay, impl->xid,
567 fontset, GDK_GC_GET_XGC (gc), x, y, text, text_length);
570 g_error("undefined font type\n");
574 gdk_x11_draw_text_wc (GdkDrawable *drawable,
579 const GdkWChar *text,
582 GdkDrawableImplX11 *impl;
585 impl = GDK_DRAWABLE_IMPL_X11 (drawable);
586 xdisplay = GDK_SCREEN_XDISPLAY (impl->screen);
588 if (font->type == GDK_FONT_FONT)
590 XFontStruct *xfont = (XFontStruct *) GDK_FONT_XFONT (font);
593 XSetFont(xdisplay, GDK_GC_GET_XGC (gc), xfont->fid);
594 text_8bit = g_new (gchar, text_length);
595 for (i=0; i<text_length; i++) text_8bit[i] = text[i];
596 XDrawString (xdisplay, impl->xid,
597 GDK_GC_GET_XGC (gc), x, y, text_8bit, text_length);
600 else if (font->type == GDK_FONT_FONTSET)
602 if (sizeof(GdkWChar) == sizeof(wchar_t))
604 XwcDrawString (xdisplay, impl->xid,
605 (XFontSet) GDK_FONT_XFONT (font),
606 GDK_GC_GET_XGC (gc), x, y, (wchar_t *)text, text_length);
612 text_wchar = g_new (wchar_t, text_length);
613 for (i=0; i<text_length; i++) text_wchar[i] = text[i];
614 XwcDrawString (xdisplay, impl->xid,
615 (XFontSet) GDK_FONT_XFONT (font),
616 GDK_GC_GET_XGC (gc), x, y, text_wchar, text_length);
621 g_error("undefined font type\n");
625 gdk_x11_draw_drawable (GdkDrawable *drawable,
635 int src_depth = gdk_drawable_get_depth (src);
636 int dest_depth = gdk_drawable_get_depth (drawable);
637 GdkDrawableImplX11 *impl;
638 GdkDrawableImplX11 *src_impl;
640 impl = GDK_DRAWABLE_IMPL_X11 (drawable);
642 if (GDK_IS_DRAWABLE_IMPL_X11 (src))
643 src_impl = GDK_DRAWABLE_IMPL_X11 (src);
649 XCopyArea (GDK_SCREEN_XDISPLAY (impl->screen),
650 src_impl ? src_impl->xid : GDK_DRAWABLE_XID (src),
657 else if (dest_depth != 0 && src_depth == dest_depth)
659 XCopyArea (GDK_SCREEN_XDISPLAY (impl->screen),
660 src_impl ? src_impl->xid : GDK_DRAWABLE_XID (src),
668 g_warning ("Attempt to draw a drawable with depth %d to a drawable with depth %d",
669 src_depth, dest_depth);
673 gdk_x11_draw_points (GdkDrawable *drawable,
678 GdkDrawableImplX11 *impl;
680 impl = GDK_DRAWABLE_IMPL_X11 (drawable);
683 /* We special-case npoints == 1, because X will merge multiple
684 * consecutive XDrawPoint requests into a PolyPoint request
688 XDrawPoint (GDK_SCREEN_XDISPLAY (impl->screen),
691 points[0].x, points[0].y);
696 XPoint *tmp_points = g_new (XPoint, npoints);
698 for (i=0; i<npoints; i++)
700 tmp_points[i].x = points[i].x;
701 tmp_points[i].y = points[i].y;
704 XDrawPoints (GDK_SCREEN_XDISPLAY (impl->screen),
716 gdk_x11_draw_segments (GdkDrawable *drawable,
721 GdkDrawableImplX11 *impl;
723 impl = GDK_DRAWABLE_IMPL_X11 (drawable);
726 /* We special-case nsegs == 1, because X will merge multiple
727 * consecutive XDrawLine requests into a PolySegment request
731 XDrawLine (GDK_SCREEN_XDISPLAY (impl->screen), impl->xid,
732 GDK_GC_GET_XGC (gc), segs[0].x1, segs[0].y1,
733 segs[0].x2, segs[0].y2);
738 XSegment *tmp_segs = g_new (XSegment, nsegs);
740 for (i=0; i<nsegs; i++)
742 tmp_segs[i].x1 = segs[i].x1;
743 tmp_segs[i].x2 = segs[i].x2;
744 tmp_segs[i].y1 = segs[i].y1;
745 tmp_segs[i].y2 = segs[i].y2;
748 XDrawSegments (GDK_SCREEN_XDISPLAY (impl->screen),
758 gdk_x11_draw_lines (GdkDrawable *drawable,
764 XPoint *tmp_points = g_new (XPoint, npoints);
765 GdkDrawableImplX11 *impl;
767 impl = GDK_DRAWABLE_IMPL_X11 (drawable);
770 for (i=0; i<npoints; i++)
772 tmp_points[i].x = points[i].x;
773 tmp_points[i].y = points[i].y;
776 XDrawLines (GDK_SCREEN_XDISPLAY (impl->screen),
786 gdk_x11_draw_glyphs (GdkDrawable *drawable,
791 PangoGlyphString *glyphs)
793 GdkDrawableImplX11 *impl;
795 impl = GDK_DRAWABLE_IMPL_X11 (drawable);
798 if (PANGO_XFT_IS_FONT (font))
804 _gdk_gc_x11_get_fg_xft_color (gc, &color);
806 gdk_x11_drawable_update_xft_clip (drawable, gc);
807 draw = gdk_x11_drawable_get_xft_draw (drawable);
809 pango_xft_render (draw, &color, font, glyphs, x, y);
810 #else /* !HAVE_XFT2 */
812 Picture dest_picture;
814 src_picture = _gdk_x11_gc_get_fg_picture (gc);
816 gdk_x11_drawable_update_xft_clip (drawable, gc);
817 dest_picture = gdk_x11_drawable_get_picture (drawable);
819 pango_xft_picture_render (GDK_SCREEN_XDISPLAY (impl->screen),
820 src_picture, dest_picture,
822 #endif /* HAVE_XFT2 */
825 #endif /* HAVE_XFT */
826 pango_x_render (GDK_SCREEN_XDISPLAY (impl->screen),
833 gdk_x11_draw_image (GdkDrawable *drawable,
843 GdkDrawableImplX11 *impl;
845 impl = GDK_DRAWABLE_IMPL_X11 (drawable);
847 if (image->type == GDK_IMAGE_SHARED)
848 XShmPutImage (GDK_SCREEN_XDISPLAY (impl->screen), impl->xid,
849 GDK_GC_GET_XGC (gc), GDK_IMAGE_XIMAGE (image),
850 xsrc, ysrc, xdest, ydest, width, height, False);
852 XPutImage (GDK_SCREEN_XDISPLAY (impl->screen), impl->xid,
853 GDK_GC_GET_XGC (gc), GDK_IMAGE_XIMAGE (image),
854 xsrc, ysrc, xdest, ydest, width, height);
858 gdk_x11_get_depth (GdkDrawable *drawable)
860 /* This is a bit bogus but I'm not sure the other way is better */
862 return gdk_drawable_get_depth (GDK_DRAWABLE_IMPL_X11 (drawable)->wrapper);
866 static GdkDrawable * get_impl_drawable (GdkDrawable *drawable)
870 if (GDK_IS_WINDOW (drawable))
871 impl = ((GdkPixmapObject *)drawable)->impl;
872 else if (GDK_IS_PIXMAP (drawable))
873 impl = ((GdkPixmapObject *)drawable)->impl;
876 g_warning (G_STRLOC " drawable is not a pixmap or window");
884 gdk_x11_get_screen (GdkDrawable *drawable)
886 if (GDK_IS_DRAWABLE_IMPL_X11 (drawable))
887 return GDK_DRAWABLE_IMPL_X11 (drawable)->screen;
889 return GDK_DRAWABLE_IMPL_X11 (get_impl_drawable (drawable))->screen;
893 gdk_x11_get_visual (GdkDrawable *drawable)
895 return gdk_drawable_get_visual (GDK_DRAWABLE_IMPL_X11 (drawable)->wrapper);
899 * gdk_x11_drawable_get_xdisplay:
900 * @drawable: a #GdkDrawable.
902 * Returns the display of a #GdkDrawable.
904 * Return value: an Xlib <type>Display*</type>.
907 gdk_x11_drawable_get_xdisplay (GdkDrawable *drawable)
909 if (GDK_IS_DRAWABLE_IMPL_X11 (drawable))
910 return GDK_SCREEN_XDISPLAY (GDK_DRAWABLE_IMPL_X11 (drawable)->screen);
912 return GDK_SCREEN_XDISPLAY (GDK_DRAWABLE_IMPL_X11 (get_impl_drawable (drawable))->screen);
916 * gdk_x11_drawable_get_xid:
917 * @drawable: a #GdkDrawable.
919 * Returns the X resource (window or pixmap) belonging to a #GdkDrawable.
921 * Return value: the ID of @drawable's X resource.
924 gdk_x11_drawable_get_xid (GdkDrawable *drawable)
928 if (GDK_IS_WINDOW (drawable))
929 impl = ((GdkPixmapObject *)drawable)->impl;
930 else if (GDK_IS_PIXMAP (drawable))
931 impl = ((GdkPixmapObject *)drawable)->impl;
934 g_warning (G_STRLOC " drawable is not a pixmap or window");
938 return ((GdkDrawableImplX11 *)impl)->xid;
941 /* Code for accelerated alpha compositing using the RENDER extension.
942 * It's a bit long because there are lots of possibilities for
943 * what's the fastest depending on the available picture formats,
944 * whether we can used shared pixmaps, etc.
955 select_format (GdkDisplay *display,
956 XRenderPictFormat **format,
957 XRenderPictFormat **mask)
959 Display *xdisplay = GDK_DISPLAY_XDISPLAY (display);
960 XRenderPictFormat pf;
962 if (!_gdk_x11_have_render (display))
965 /* Look for a 32-bit xRGB and Axxx formats that exactly match the
966 * in memory data format. We can use them as pixmap and mask
967 * to deal with non-premultiplied data.
970 pf.type = PictTypeDirect;
972 pf.direct.redMask = 0xff;
973 pf.direct.greenMask = 0xff;
974 pf.direct.blueMask = 0xff;
976 pf.direct.alphaMask = 0;
977 if (ImageByteOrder (xdisplay) == LSBFirst)
988 pf.direct.green = 16;
992 *format = XRenderFindFormat (xdisplay,
993 (PictFormatType | PictFormatDepth |
994 PictFormatRedMask | PictFormatRed |
995 PictFormatGreenMask | PictFormatGreen |
996 PictFormatBlueMask | PictFormatBlue |
997 PictFormatAlphaMask),
1001 pf.direct.alphaMask = 0xff;
1002 if (ImageByteOrder (xdisplay) == LSBFirst)
1005 pf.direct.alpha = 24;
1009 pf.direct.alpha = 0;
1012 *mask = XRenderFindFormat (xdisplay,
1013 (PictFormatType | PictFormatDepth |
1014 PictFormatAlphaMask | PictFormatAlpha),
1018 if (*format && *mask)
1019 return FORMAT_EXACT_MASK;
1021 /* OK, that failed, now look for xRGB and Axxx formats in
1022 * RENDER's preferred order
1024 pf.direct.alphaMask = 0;
1027 pf.direct.green = 8;
1030 *format = XRenderFindFormat (xdisplay,
1031 (PictFormatType | PictFormatDepth |
1032 PictFormatRedMask | PictFormatRed |
1033 PictFormatGreenMask | PictFormatGreen |
1034 PictFormatBlueMask | PictFormatBlue |
1035 PictFormatAlphaMask),
1039 pf.direct.alphaMask = 0xff;
1040 pf.direct.alpha = 24;
1042 *mask = XRenderFindFormat (xdisplay,
1043 (PictFormatType | PictFormatDepth |
1044 PictFormatAlphaMask | PictFormatAlpha),
1048 if (*format && *mask)
1049 return FORMAT_ARGB_MASK;
1051 /* Finally, if neither of the above worked, fall back to
1052 * looking for combined ARGB -- we'll premultiply ourselves.
1055 pf.type = PictTypeDirect;
1058 pf.direct.green = 8;
1060 pf.direct.alphaMask = 0xff;
1061 pf.direct.alpha = 24;
1063 *format = XRenderFindFormat (xdisplay,
1064 (PictFormatType | PictFormatDepth |
1065 PictFormatRedMask | PictFormatRed |
1066 PictFormatGreenMask | PictFormatGreen |
1067 PictFormatBlueMask | PictFormatBlue |
1068 PictFormatAlphaMask | PictFormatAlpha),
1081 list_formats (XRenderPictFormat *pf)
1087 XRenderPictFormat *pf = XRenderFindFormat (impl->xdisplay, 0, NULL, i);
1090 g_print ("%2d R-%#06x/%#06x G-%#06x/%#06x B-%#06x/%#06x A-%#06x/%#06x\n",
1095 pf->direct.greenMask,
1097 pf->direct.blueMask,
1099 pf->direct.alphaMask);
1108 convert_to_format (guchar *src_buf,
1111 gint dest_rowstride,
1112 FormatType dest_format,
1113 GdkByteOrder dest_byteorder,
1119 if (dest_format == FORMAT_EXACT_MASK &&
1120 src_rowstride == dest_rowstride)
1122 memcpy (dest_buf, src_buf, height * src_rowstride);
1126 for (i=0; i < height; i++)
1128 switch (dest_format)
1130 case FORMAT_EXACT_MASK:
1132 memcpy (dest_buf + i * dest_rowstride,
1133 src_buf + i * src_rowstride,
1137 case FORMAT_ARGB_MASK:
1139 guint *p = (guint *)(src_buf + i * src_rowstride);
1140 guint *q = (guint *)(dest_buf + i * dest_rowstride);
1141 guint *end = p + width;
1143 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
1144 if (dest_byteorder == GDK_LSB_FIRST)
1150 *q = ( (*p & 0xff00ff00) |
1151 ((*p & 0x000000ff) << 16) |
1152 ((*p & 0x00ff0000) >> 16));
1163 *q = (((*p & 0xff000000) >> 24) |
1164 ((*p & 0x00ffffff) << 8));
1169 #else /* G_BYTE_ORDER == G_BIG_ENDIAN */
1170 if (dest_byteorder == GDK_LSB_FIRST)
1176 *q = ( (*p & 0x00ff00ff) |
1177 ((*p & 0x0000ff00) << 16) |
1178 ((*p & 0xff000000) >> 16));
1189 *q = (((*p & 0xffffff00) >> 8) |
1190 ((*p & 0x000000ff) << 24));
1195 #endif /* G_BYTE_ORDER*/
1200 guchar *p = (src_buf + i * src_rowstride);
1201 guchar *q = (dest_buf + i * dest_rowstride);
1202 guchar *end = p + 4 * width;
1205 #define MULT(d,c,a,t) G_STMT_START { t = c * a; d = ((t >> 8) + t) >> 8; } G_STMT_END
1207 if (dest_byteorder == GDK_LSB_FIRST)
1211 MULT(q[0], p[2], p[3], t1);
1212 MULT(q[1], p[1], p[3], t2);
1213 MULT(q[2], p[0], p[3], t3);
1224 MULT(q[1], p[0], p[3], t1);
1225 MULT(q[2], p[1], p[3], t2);
1226 MULT(q[3], p[2], p[3], t3);
1235 g_assert_not_reached ();
1242 draw_with_images (GdkDrawable *drawable,
1244 FormatType format_type,
1245 XRenderPictFormat *format,
1246 XRenderPictFormat *mask_format,
1254 GdkScreen *screen = GDK_DRAWABLE_IMPL_X11 (drawable)->screen;
1255 Display *xdisplay = GDK_SCREEN_XDISPLAY (screen);
1261 Picture mask = None;
1264 pix = gdk_pixmap_new (gdk_screen_get_root_window (screen), width, height, 32);
1266 pict = XRenderCreatePicture (xdisplay,
1267 GDK_PIXMAP_XID (pix),
1270 mask = XRenderCreatePicture (xdisplay,
1271 GDK_PIXMAP_XID (pix),
1272 mask_format, 0, NULL);
1274 dest_pict = gdk_x11_drawable_get_picture (drawable);
1276 pix_gc = gdk_gc_new (pix);
1278 for (y0 = 0; y0 < height; y0 += GDK_SCRATCH_IMAGE_HEIGHT)
1280 gint height1 = MIN (height - y0, GDK_SCRATCH_IMAGE_HEIGHT);
1281 for (x0 = 0; x0 < width; x0 += GDK_SCRATCH_IMAGE_WIDTH)
1285 gint width1 = MIN (width - x0, GDK_SCRATCH_IMAGE_WIDTH);
1287 image = _gdk_image_get_scratch (screen, width1, height1, 32, &xs0, &ys0);
1289 convert_to_format (src_rgb + y0 * src_rowstride + 4 * x0, src_rowstride,
1290 image->mem + ys0 * image->bpl + xs0 * image->bpp, image->bpl,
1291 format_type, image->byte_order,
1294 gdk_draw_image (pix, pix_gc,
1295 image, xs0, ys0, x0, y0, width1, height1);
1299 XRenderComposite (xdisplay, PictOpOver, pict, mask, dest_pict,
1300 0, 0, 0, 0, dest_x, dest_y, width, height);
1302 XRenderFreePicture (xdisplay, pict);
1304 XRenderFreePicture (xdisplay, mask);
1306 g_object_unref (pix);
1307 g_object_unref (pix_gc);
1310 typedef struct _ShmPixmapInfo ShmPixmapInfo;
1312 struct _ShmPixmapInfo
1320 /* Returns FALSE if we can't get a shm pixmap */
1322 get_shm_pixmap_for_image (Display *xdisplay,
1324 XRenderPictFormat *format,
1325 XRenderPictFormat *mask_format,
1330 ShmPixmapInfo *info;
1332 if (image->type != GDK_IMAGE_SHARED)
1335 info = g_object_get_data (G_OBJECT (image), "gdk-x11-shm-pixmap");
1338 *pix = _gdk_x11_image_get_shm_pixmap (image);
1343 info = g_new (ShmPixmapInfo, 1);
1346 info->pict = XRenderCreatePicture (xdisplay, info->pix,
1349 info->mask = XRenderCreatePicture (xdisplay, info->pix,
1350 mask_format, 0, NULL);
1354 g_object_set_data (G_OBJECT (image), "gdk-x11-shm-pixmap", info);
1365 /* Returns FALSE if drawing with ShmPixmaps is not possible */
1367 draw_with_pixmaps (GdkDrawable *drawable,
1369 FormatType format_type,
1370 XRenderPictFormat *format,
1371 XRenderPictFormat *mask_format,
1379 Display *xdisplay = GDK_SCREEN_XDISPLAY (GDK_DRAWABLE_IMPL_X11 (drawable)->screen);
1384 Picture mask = None;
1387 dest_pict = gdk_x11_drawable_get_picture (drawable);
1389 for (y0 = 0; y0 < height; y0 += GDK_SCRATCH_IMAGE_HEIGHT)
1391 gint height1 = MIN (height - y0, GDK_SCRATCH_IMAGE_HEIGHT);
1392 for (x0 = 0; x0 < width; x0 += GDK_SCRATCH_IMAGE_WIDTH)
1396 gint width1 = MIN (width - x0, GDK_SCRATCH_IMAGE_WIDTH);
1398 image = _gdk_image_get_scratch (GDK_DRAWABLE_IMPL_X11 (drawable)->screen,
1399 width1, height1, 32, &xs0, &ys0);
1400 if (!get_shm_pixmap_for_image (xdisplay, image, format, mask_format, &pix, &pict, &mask))
1403 convert_to_format (src_rgb + y0 * src_rowstride + 4 * x0, src_rowstride,
1404 image->mem + ys0 * image->bpl + xs0 * image->bpp, image->bpl,
1405 format_type, image->byte_order,
1408 XRenderComposite (xdisplay, PictOpOver, pict, mask, dest_pict,
1409 xs0, ys0, xs0, ys0, x0 + dest_x, y0 + dest_y,
1419 gdk_x11_draw_pixbuf (GdkDrawable *drawable,
1428 GdkRgbDither dither,
1432 FormatType format_type;
1433 XRenderPictFormat *format, *mask_format;
1436 gboolean use_pixmaps = TRUE;
1437 #endif /* USE_SHM */
1439 format_type = select_format (gdk_drawable_get_display (drawable),
1440 &format, &mask_format);
1442 if (format_type == FORMAT_NONE ||
1443 !gdk_pixbuf_get_has_alpha (pixbuf) ||
1444 (dither == GDK_RGB_DITHER_MAX && gdk_drawable_get_depth (drawable) != 24) ||
1445 gdk_x11_drawable_get_picture (drawable) == None)
1447 GdkDrawable *wrapper = GDK_DRAWABLE_IMPL_X11 (drawable)->wrapper;
1448 GDK_DRAWABLE_CLASS (parent_class)->draw_pixbuf (wrapper, gc, pixbuf,
1449 src_x, src_y, dest_x, dest_y,
1451 dither, x_dither, y_dither);
1455 gdk_x11_drawable_update_xft_clip (drawable, gc);
1457 rowstride = gdk_pixbuf_get_rowstride (pixbuf);
1462 if (!draw_with_pixmaps (drawable, gc,
1463 format_type, format, mask_format,
1464 gdk_pixbuf_get_pixels (pixbuf) + src_y * rowstride + src_x * 4,
1466 dest_x, dest_y, width, height))
1467 use_pixmaps = FALSE;
1471 #endif /* USE_SHM */
1472 draw_with_images (drawable, gc,
1473 format_type, format, mask_format,
1474 gdk_pixbuf_get_pixels (pixbuf) + src_y * rowstride + src_x * 4,
1476 dest_x, dest_y, width, height);
1478 #endif /* HAVE_XFT */