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,
91 GdkDrawable *original_src,
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,
111 static void gdk_x11_draw_image (GdkDrawable *drawable,
120 static void gdk_x11_draw_pixbuf (GdkDrawable *drawable,
133 static cairo_surface_t *gdk_x11_ref_cairo_surface (GdkDrawable *drawable);
135 static void gdk_x11_set_colormap (GdkDrawable *drawable,
136 GdkColormap *colormap);
138 static GdkColormap* gdk_x11_get_colormap (GdkDrawable *drawable);
139 static gint gdk_x11_get_depth (GdkDrawable *drawable);
140 static GdkScreen * gdk_x11_get_screen (GdkDrawable *drawable);
141 static GdkVisual* gdk_x11_get_visual (GdkDrawable *drawable);
143 static void gdk_drawable_impl_x11_finalize (GObject *object);
145 static const cairo_user_data_key_t gdk_x11_cairo_key;
147 G_DEFINE_TYPE (GdkDrawableImplX11, _gdk_drawable_impl_x11, GDK_TYPE_DRAWABLE)
150 _gdk_drawable_impl_x11_class_init (GdkDrawableImplX11Class *klass)
152 GdkDrawableClass *drawable_class = GDK_DRAWABLE_CLASS (klass);
153 GObjectClass *object_class = G_OBJECT_CLASS (klass);
155 object_class->finalize = gdk_drawable_impl_x11_finalize;
157 drawable_class->create_gc = _gdk_x11_gc_new;
158 drawable_class->draw_rectangle = gdk_x11_draw_rectangle;
159 drawable_class->draw_arc = gdk_x11_draw_arc;
160 drawable_class->draw_polygon = gdk_x11_draw_polygon;
161 drawable_class->draw_text = gdk_x11_draw_text;
162 drawable_class->draw_text_wc = gdk_x11_draw_text_wc;
163 drawable_class->draw_drawable = gdk_x11_draw_drawable;
164 drawable_class->draw_points = gdk_x11_draw_points;
165 drawable_class->draw_segments = gdk_x11_draw_segments;
166 drawable_class->draw_lines = gdk_x11_draw_lines;
167 drawable_class->draw_image = gdk_x11_draw_image;
168 drawable_class->draw_pixbuf = gdk_x11_draw_pixbuf;
170 drawable_class->ref_cairo_surface = gdk_x11_ref_cairo_surface;
172 drawable_class->set_colormap = gdk_x11_set_colormap;
173 drawable_class->get_colormap = gdk_x11_get_colormap;
175 drawable_class->get_depth = gdk_x11_get_depth;
176 drawable_class->get_screen = gdk_x11_get_screen;
177 drawable_class->get_visual = gdk_x11_get_visual;
179 drawable_class->_copy_to_image = _gdk_x11_copy_to_image;
183 _gdk_drawable_impl_x11_init (GdkDrawableImplX11 *impl)
188 gdk_drawable_impl_x11_finalize (GObject *object)
190 gdk_drawable_set_colormap (GDK_DRAWABLE (object), NULL);
192 G_OBJECT_CLASS (_gdk_drawable_impl_x11_parent_class)->finalize (object);
196 * _gdk_x11_drawable_finish:
197 * @drawable: a #GdkDrawableImplX11.
199 * Performs necessary cleanup prior to freeing a pixmap or
200 * destroying a window.
203 _gdk_x11_drawable_finish (GdkDrawable *drawable)
205 GdkDrawableImplX11 *impl = GDK_DRAWABLE_IMPL_X11 (drawable);
209 XRenderFreePicture (GDK_SCREEN_XDISPLAY (impl->screen),
211 impl->picture = None;
214 if (impl->cairo_surface)
216 cairo_surface_finish (impl->cairo_surface);
217 cairo_surface_set_user_data (impl->cairo_surface, &gdk_x11_cairo_key,
223 * _gdk_x11_drawable_update_size:
224 * @drawable: a #GdkDrawableImplX11.
226 * Updates the state of the drawable (in particular the drawable's
227 * cairo surface) when its size has changed.
230 _gdk_x11_drawable_update_size (GdkDrawable *drawable)
232 GdkDrawableImplX11 *impl = GDK_DRAWABLE_IMPL_X11 (drawable);
234 if (impl->cairo_surface)
238 gdk_drawable_get_size (drawable, &width, &height);
239 cairo_xlib_surface_set_size (impl->cairo_surface, width, height);
244 try_pixmap (Display *xdisplay,
248 Pixmap pixmap = XCreatePixmap (xdisplay,
249 RootWindow (xdisplay, screen),
251 XFreePixmap (xdisplay, pixmap);
255 _gdk_x11_have_render (GdkDisplay *display)
257 Display *xdisplay = GDK_DISPLAY_XDISPLAY (display);
258 GdkDisplayX11 *x11display = GDK_DISPLAY_X11 (display);
260 if (x11display->have_render == GDK_UNKNOWN)
262 int event_base, error_base;
263 x11display->have_render =
264 XRenderQueryExtension (xdisplay, &event_base, &error_base)
267 if (x11display->have_render == GDK_YES)
270 * Sun advertises RENDER, but fails to support 32-bit pixmaps.
271 * That is just no good. Therefore, we check all screens
272 * for proper support.
276 for (screen = 0; screen < ScreenCount (xdisplay); screen++)
279 int *depths = XListDepths (xdisplay, screen, &count);
280 gboolean has_8 = FALSE, has_32 = FALSE;
286 for (i = 0; i < count; i++)
290 else if (depths[i] == 32)
296 /* At this point, we might have a false positive;
297 * buggy versions of Xinerama only report depths for
298 * which there is an associated visual; so we actually
299 * go ahead and try create pixmaps.
301 if (!(has_8 && has_32))
303 gdk_error_trap_push ();
305 try_pixmap (xdisplay, screen, 8);
307 try_pixmap (xdisplay, screen, 32);
308 XSync (xdisplay, False);
309 if (gdk_error_trap_pop () == 0)
316 if (!(has_8 && has_32))
318 g_warning ("The X server advertises that RENDER support is present,\n"
319 "but fails to supply the necessary pixmap support. In\n"
320 "other words, it is buggy.");
321 x11display->have_render = GDK_NO;
328 return x11display->have_render == GDK_YES;
332 gdk_x11_drawable_get_picture (GdkDrawable *drawable)
334 GdkDrawableImplX11 *impl = GDK_DRAWABLE_IMPL_X11 (drawable);
338 Display *xdisplay = GDK_SCREEN_XDISPLAY (impl->screen);
339 XRenderPictFormat *format;
341 GdkVisual *visual = gdk_drawable_get_visual (GDK_DRAWABLE_IMPL_X11 (drawable)->wrapper);
345 format = XRenderFindVisualFormat (xdisplay, GDK_VISUAL_XVISUAL (visual));
348 XRenderPictureAttributes attributes;
349 attributes.graphics_exposures = False;
351 impl->picture = XRenderCreatePicture (xdisplay, impl->xid, format,
352 CPGraphicsExposure, &attributes);
356 return impl->picture;
360 gdk_x11_drawable_update_picture_clip (GdkDrawable *drawable,
363 GdkDrawableImplX11 *impl = GDK_DRAWABLE_IMPL_X11 (drawable);
364 Display *xdisplay = GDK_SCREEN_XDISPLAY (impl->screen);
365 Picture picture = gdk_x11_drawable_get_picture (drawable);
366 GdkRegion *clip_region = gc ? _gdk_gc_get_clip_region (gc) : NULL;
370 GdkRegionBox *boxes = clip_region->rects;
371 gint n_boxes = clip_region->numRects;
372 XRectangle *rects = g_new (XRectangle, n_boxes);
375 for (i=0; i < n_boxes; i++)
377 rects[i].x = CLAMP (boxes[i].x1 + gc->clip_x_origin, G_MINSHORT, G_MAXSHORT);
378 rects[i].y = CLAMP (boxes[i].y1 + gc->clip_y_origin, G_MINSHORT, G_MAXSHORT);
379 rects[i].width = CLAMP (boxes[i].x2 + gc->clip_x_origin, G_MINSHORT, G_MAXSHORT) - rects[i].x;
380 rects[i].height = CLAMP (boxes[i].y2 + gc->clip_y_origin, G_MINSHORT, G_MAXSHORT) - rects[i].y;
383 XRenderSetPictureClipRectangles (xdisplay, picture,
384 0, 0, rects, n_boxes);
390 XRenderPictureAttributes pa;
392 XRenderChangePicture (xdisplay, picture,
397 /*****************************************************
398 * X11 specific implementations of generic functions *
399 *****************************************************/
402 gdk_x11_get_colormap (GdkDrawable *drawable)
404 GdkDrawableImplX11 *impl;
406 impl = GDK_DRAWABLE_IMPL_X11 (drawable);
408 return impl->colormap;
412 gdk_x11_set_colormap (GdkDrawable *drawable,
413 GdkColormap *colormap)
415 GdkDrawableImplX11 *impl;
417 impl = GDK_DRAWABLE_IMPL_X11 (drawable);
419 if (impl->colormap == colormap)
423 g_object_unref (impl->colormap);
424 impl->colormap = colormap;
426 g_object_ref (impl->colormap);
433 gdk_x11_draw_rectangle (GdkDrawable *drawable,
441 GdkDrawableImplX11 *impl;
443 impl = GDK_DRAWABLE_IMPL_X11 (drawable);
446 XFillRectangle (GDK_SCREEN_XDISPLAY (impl->screen), impl->xid,
447 GDK_GC_GET_XGC (gc), x, y, width, height);
449 XDrawRectangle (GDK_SCREEN_XDISPLAY (impl->screen), impl->xid,
450 GDK_GC_GET_XGC (gc), x, y, width, height);
454 gdk_x11_draw_arc (GdkDrawable *drawable,
464 GdkDrawableImplX11 *impl;
466 impl = GDK_DRAWABLE_IMPL_X11 (drawable);
470 XFillArc (GDK_SCREEN_XDISPLAY (impl->screen), impl->xid,
471 GDK_GC_GET_XGC (gc), x, y, width, height, angle1, angle2);
473 XDrawArc (GDK_SCREEN_XDISPLAY (impl->screen), impl->xid,
474 GDK_GC_GET_XGC (gc), x, y, width, height, angle1, angle2);
478 gdk_x11_draw_polygon (GdkDrawable *drawable,
486 GdkDrawableImplX11 *impl;
488 impl = GDK_DRAWABLE_IMPL_X11 (drawable);
492 (points[0].x != points[npoints-1].x || points[0].y != points[npoints-1].y))
494 tmp_npoints = npoints + 1;
495 tmp_points = g_new (XPoint, tmp_npoints);
496 tmp_points[npoints].x = points[0].x;
497 tmp_points[npoints].y = points[0].y;
501 tmp_npoints = npoints;
502 tmp_points = g_new (XPoint, tmp_npoints);
505 for (i=0; i<npoints; i++)
507 tmp_points[i].x = points[i].x;
508 tmp_points[i].y = points[i].y;
512 XFillPolygon (GDK_SCREEN_XDISPLAY (impl->screen), impl->xid,
513 GDK_GC_GET_XGC (gc), tmp_points, tmp_npoints, Complex, CoordModeOrigin);
515 XDrawLines (GDK_SCREEN_XDISPLAY (impl->screen), impl->xid,
516 GDK_GC_GET_XGC (gc), tmp_points, tmp_npoints, CoordModeOrigin);
523 * Modified by Li-Da Lho to draw 16 bits and Multibyte strings
525 * Interface changed: add "GdkFont *font" to specify font or fontset explicitely
528 gdk_x11_draw_text (GdkDrawable *drawable,
536 GdkDrawableImplX11 *impl;
539 impl = GDK_DRAWABLE_IMPL_X11 (drawable);
540 xdisplay = GDK_SCREEN_XDISPLAY (impl->screen);
542 if (font->type == GDK_FONT_FONT)
544 XFontStruct *xfont = (XFontStruct *) GDK_FONT_XFONT (font);
545 XSetFont(xdisplay, GDK_GC_GET_XGC (gc), xfont->fid);
546 if ((xfont->min_byte1 == 0) && (xfont->max_byte1 == 0))
548 XDrawString (xdisplay, impl->xid,
549 GDK_GC_GET_XGC (gc), x, y, text, text_length);
553 XDrawString16 (xdisplay, impl->xid,
554 GDK_GC_GET_XGC (gc), x, y, (XChar2b *) text, text_length / 2);
557 else if (font->type == GDK_FONT_FONTSET)
559 XFontSet fontset = (XFontSet) GDK_FONT_XFONT (font);
560 XmbDrawString (xdisplay, impl->xid,
561 fontset, GDK_GC_GET_XGC (gc), x, y, text, text_length);
564 g_error("undefined font type\n");
568 gdk_x11_draw_text_wc (GdkDrawable *drawable,
573 const GdkWChar *text,
576 GdkDrawableImplX11 *impl;
579 impl = GDK_DRAWABLE_IMPL_X11 (drawable);
580 xdisplay = GDK_SCREEN_XDISPLAY (impl->screen);
582 if (font->type == GDK_FONT_FONT)
584 XFontStruct *xfont = (XFontStruct *) GDK_FONT_XFONT (font);
587 XSetFont(xdisplay, GDK_GC_GET_XGC (gc), xfont->fid);
588 text_8bit = g_new (gchar, text_length);
589 for (i=0; i<text_length; i++) text_8bit[i] = text[i];
590 XDrawString (xdisplay, impl->xid,
591 GDK_GC_GET_XGC (gc), x, y, text_8bit, text_length);
594 else if (font->type == GDK_FONT_FONTSET)
596 if (sizeof(GdkWChar) == sizeof(wchar_t))
598 XwcDrawString (xdisplay, impl->xid,
599 (XFontSet) GDK_FONT_XFONT (font),
600 GDK_GC_GET_XGC (gc), x, y, (wchar_t *)text, text_length);
606 text_wchar = g_new (wchar_t, text_length);
607 for (i=0; i<text_length; i++) text_wchar[i] = text[i];
608 XwcDrawString (xdisplay, impl->xid,
609 (XFontSet) GDK_FONT_XFONT (font),
610 GDK_GC_GET_XGC (gc), x, y, text_wchar, text_length);
615 g_error("undefined font type\n");
619 gdk_x11_draw_drawable (GdkDrawable *drawable,
622 GdkDrawable *original_src,
630 int src_depth = gdk_drawable_get_depth (src);
631 int dest_depth = gdk_drawable_get_depth (drawable);
632 GdkDrawableImplX11 *impl;
633 GdkDrawableImplX11 *src_impl;
635 impl = GDK_DRAWABLE_IMPL_X11 (drawable);
637 if (GDK_IS_DRAWABLE_IMPL_X11 (src))
638 src_impl = GDK_DRAWABLE_IMPL_X11 (src);
639 else if (GDK_IS_WINDOW (src))
640 src_impl = GDK_DRAWABLE_IMPL_X11(((GdkWindowObject *)src)->impl);
642 src_impl = GDK_DRAWABLE_IMPL_X11(((GdkPixmapObject *)src)->impl);
644 if (GDK_IS_WINDOW_IMPL_X11 (impl) &&
645 GDK_IS_PIXMAP_IMPL_X11 (src_impl))
647 GdkPixmapImplX11 *src_pixmap = GDK_PIXMAP_IMPL_X11 (src_impl);
648 /* Work around an Xserver bug where non-visible areas from
649 * a pixmap to a window will clear the window background
650 * in destination areas that are supposed to be clipped out.
651 * This is a problem with client side windows as this means
652 * things may draw outside the virtual windows. This could
653 * also happen for window to window copies, but I don't
654 * think we generate any calls like that.
657 * http://lists.freedesktop.org/archives/xorg/2009-February/043318.html
673 if (xsrc + width > src_pixmap->width)
674 width = src_pixmap->width - xsrc;
675 if (ysrc + height > src_pixmap->height)
676 height = src_pixmap->height - ysrc;
681 XCopyArea (GDK_SCREEN_XDISPLAY (impl->screen),
689 else if (dest_depth != 0 && src_depth == dest_depth)
691 XCopyArea (GDK_SCREEN_XDISPLAY (impl->screen),
700 g_warning ("Attempt to draw a drawable with depth %d to a drawable with depth %d",
701 src_depth, dest_depth);
705 gdk_x11_draw_points (GdkDrawable *drawable,
710 GdkDrawableImplX11 *impl;
712 impl = GDK_DRAWABLE_IMPL_X11 (drawable);
715 /* We special-case npoints == 1, because X will merge multiple
716 * consecutive XDrawPoint requests into a PolyPoint request
720 XDrawPoint (GDK_SCREEN_XDISPLAY (impl->screen),
723 points[0].x, points[0].y);
728 XPoint *tmp_points = g_new (XPoint, npoints);
730 for (i=0; i<npoints; i++)
732 tmp_points[i].x = points[i].x;
733 tmp_points[i].y = points[i].y;
736 XDrawPoints (GDK_SCREEN_XDISPLAY (impl->screen),
748 gdk_x11_draw_segments (GdkDrawable *drawable,
753 GdkDrawableImplX11 *impl;
755 impl = GDK_DRAWABLE_IMPL_X11 (drawable);
758 /* We special-case nsegs == 1, because X will merge multiple
759 * consecutive XDrawLine requests into a PolySegment request
763 XDrawLine (GDK_SCREEN_XDISPLAY (impl->screen), impl->xid,
764 GDK_GC_GET_XGC (gc), segs[0].x1, segs[0].y1,
765 segs[0].x2, segs[0].y2);
770 XSegment *tmp_segs = g_new (XSegment, nsegs);
772 for (i=0; i<nsegs; i++)
774 tmp_segs[i].x1 = segs[i].x1;
775 tmp_segs[i].x2 = segs[i].x2;
776 tmp_segs[i].y1 = segs[i].y1;
777 tmp_segs[i].y2 = segs[i].y2;
780 XDrawSegments (GDK_SCREEN_XDISPLAY (impl->screen),
790 gdk_x11_draw_lines (GdkDrawable *drawable,
796 XPoint *tmp_points = g_new (XPoint, npoints);
797 GdkDrawableImplX11 *impl;
799 impl = GDK_DRAWABLE_IMPL_X11 (drawable);
802 for (i=0; i<npoints; i++)
804 tmp_points[i].x = points[i].x;
805 tmp_points[i].y = points[i].y;
808 XDrawLines (GDK_SCREEN_XDISPLAY (impl->screen),
818 gdk_x11_draw_image (GdkDrawable *drawable,
828 GdkDrawableImplX11 *impl;
830 impl = GDK_DRAWABLE_IMPL_X11 (drawable);
833 if (image->type == GDK_IMAGE_SHARED)
834 XShmPutImage (GDK_SCREEN_XDISPLAY (impl->screen), impl->xid,
835 GDK_GC_GET_XGC (gc), GDK_IMAGE_XIMAGE (image),
836 xsrc, ysrc, xdest, ydest, width, height, False);
839 XPutImage (GDK_SCREEN_XDISPLAY (impl->screen), impl->xid,
840 GDK_GC_GET_XGC (gc), GDK_IMAGE_XIMAGE (image),
841 xsrc, ysrc, xdest, ydest, width, height);
845 gdk_x11_get_depth (GdkDrawable *drawable)
847 /* This is a bit bogus but I'm not sure the other way is better */
849 return gdk_drawable_get_depth (GDK_DRAWABLE_IMPL_X11 (drawable)->wrapper);
853 get_impl_drawable (GdkDrawable *drawable)
855 if (GDK_IS_WINDOW (drawable))
856 return ((GdkWindowObject *)drawable)->impl;
857 else if (GDK_IS_PIXMAP (drawable))
858 return ((GdkPixmapObject *)drawable)->impl;
861 g_warning (G_STRLOC " drawable is not a pixmap or window");
867 gdk_x11_get_screen (GdkDrawable *drawable)
869 if (GDK_IS_DRAWABLE_IMPL_X11 (drawable))
870 return GDK_DRAWABLE_IMPL_X11 (drawable)->screen;
872 return GDK_DRAWABLE_IMPL_X11 (get_impl_drawable (drawable))->screen;
876 gdk_x11_get_visual (GdkDrawable *drawable)
878 return gdk_drawable_get_visual (GDK_DRAWABLE_IMPL_X11 (drawable)->wrapper);
882 * gdk_x11_drawable_get_xdisplay:
883 * @drawable: a #GdkDrawable.
885 * Returns the display of a #GdkDrawable.
887 * Return value: an Xlib <type>Display*</type>.
890 gdk_x11_drawable_get_xdisplay (GdkDrawable *drawable)
892 if (GDK_IS_DRAWABLE_IMPL_X11 (drawable))
893 return GDK_SCREEN_XDISPLAY (GDK_DRAWABLE_IMPL_X11 (drawable)->screen);
895 return GDK_SCREEN_XDISPLAY (GDK_DRAWABLE_IMPL_X11 (get_impl_drawable (drawable))->screen);
899 * gdk_x11_drawable_get_xid:
900 * @drawable: a #GdkDrawable.
902 * Returns the X resource (window or pixmap) belonging to a #GdkDrawable.
904 * Return value: the ID of @drawable's X resource.
907 gdk_x11_drawable_get_xid (GdkDrawable *drawable)
911 if (GDK_IS_WINDOW (drawable))
913 GdkWindow *window = (GdkWindow *)drawable;
915 /* Try to ensure the window has a native window */
916 if (!_gdk_window_has_impl (window))
918 gdk_window_set_has_native (window, TRUE);
920 /* We sync here to ensure the window is created in the Xserver when
921 * this function returns. This is required because the returned XID
922 * for this window must be valid immediately, even with another
923 * connection to the Xserver */
924 gdk_display_sync (gdk_drawable_get_display (window));
927 if (!GDK_WINDOW_IS_X11 (window))
929 g_warning (G_STRLOC " drawable is not a native X11 window");
933 impl = ((GdkWindowObject *)drawable)->impl;
935 else if (GDK_IS_PIXMAP (drawable))
936 impl = ((GdkPixmapObject *)drawable)->impl;
939 g_warning (G_STRLOC " drawable is not a pixmap or window");
943 return ((GdkDrawableImplX11 *)impl)->xid;
946 /* Code for accelerated alpha compositing using the RENDER extension.
947 * It's a bit long because there are lots of possibilities for
948 * what's the fastest depending on the available picture formats,
949 * whether we can used shared pixmaps, etc.
952 static GdkX11FormatType
953 select_format (GdkDisplay *display,
954 XRenderPictFormat **format,
955 XRenderPictFormat **mask)
957 Display *xdisplay = GDK_DISPLAY_XDISPLAY (display);
958 XRenderPictFormat pf;
960 if (!_gdk_x11_have_render (display))
961 return GDK_X11_FORMAT_NONE;
963 /* Look for a 32-bit xRGB and Axxx formats that exactly match the
964 * in memory data format. We can use them as pixmap and mask
965 * to deal with non-premultiplied data.
968 pf.type = PictTypeDirect;
970 pf.direct.redMask = 0xff;
971 pf.direct.greenMask = 0xff;
972 pf.direct.blueMask = 0xff;
974 pf.direct.alphaMask = 0;
975 if (ImageByteOrder (xdisplay) == LSBFirst)
986 pf.direct.green = 16;
990 *format = XRenderFindFormat (xdisplay,
991 (PictFormatType | PictFormatDepth |
992 PictFormatRedMask | PictFormatRed |
993 PictFormatGreenMask | PictFormatGreen |
994 PictFormatBlueMask | PictFormatBlue |
995 PictFormatAlphaMask),
999 pf.direct.alphaMask = 0xff;
1000 if (ImageByteOrder (xdisplay) == LSBFirst)
1003 pf.direct.alpha = 24;
1007 pf.direct.alpha = 0;
1010 *mask = XRenderFindFormat (xdisplay,
1011 (PictFormatType | PictFormatDepth |
1012 PictFormatAlphaMask | PictFormatAlpha),
1016 if (*format && *mask)
1017 return GDK_X11_FORMAT_EXACT_MASK;
1019 /* OK, that failed, now look for xRGB and Axxx formats in
1020 * RENDER's preferred order
1022 pf.direct.alphaMask = 0;
1025 pf.direct.green = 8;
1028 *format = XRenderFindFormat (xdisplay,
1029 (PictFormatType | PictFormatDepth |
1030 PictFormatRedMask | PictFormatRed |
1031 PictFormatGreenMask | PictFormatGreen |
1032 PictFormatBlueMask | PictFormatBlue |
1033 PictFormatAlphaMask),
1037 pf.direct.alphaMask = 0xff;
1038 pf.direct.alpha = 24;
1040 *mask = XRenderFindFormat (xdisplay,
1041 (PictFormatType | PictFormatDepth |
1042 PictFormatAlphaMask | PictFormatAlpha),
1046 if (*format && *mask)
1047 return GDK_X11_FORMAT_ARGB_MASK;
1049 /* Finally, if neither of the above worked, fall back to
1050 * looking for combined ARGB -- we'll premultiply ourselves.
1053 pf.type = PictTypeDirect;
1056 pf.direct.green = 8;
1058 pf.direct.alphaMask = 0xff;
1059 pf.direct.alpha = 24;
1061 *format = XRenderFindFormat (xdisplay,
1062 (PictFormatType | PictFormatDepth |
1063 PictFormatRedMask | PictFormatRed |
1064 PictFormatGreenMask | PictFormatGreen |
1065 PictFormatBlueMask | PictFormatBlue |
1066 PictFormatAlphaMask | PictFormatAlpha),
1072 return GDK_X11_FORMAT_ARGB;
1074 return GDK_X11_FORMAT_NONE;
1079 list_formats (XRenderPictFormat *pf)
1085 XRenderPictFormat *pf = XRenderFindFormat (impl->xdisplay, 0, NULL, i);
1088 g_print ("%2d R-%#06x/%#06x G-%#06x/%#06x B-%#06x/%#06x A-%#06x/%#06x\n",
1093 pf->direct.greenMask,
1095 pf->direct.blueMask,
1097 pf->direct.alphaMask);
1106 _gdk_x11_convert_to_format (guchar *src_buf,
1109 gint dest_rowstride,
1110 GdkX11FormatType dest_format,
1111 GdkByteOrder dest_byteorder,
1117 for (i=0; i < height; i++)
1119 switch (dest_format)
1121 case GDK_X11_FORMAT_EXACT_MASK:
1123 memcpy (dest_buf + i * dest_rowstride,
1124 src_buf + i * src_rowstride,
1128 case GDK_X11_FORMAT_ARGB_MASK:
1130 guchar *row = src_buf + i * src_rowstride;
1131 if (((gsize)row & 3) != 0)
1134 guint32 *q = (guint32 *)(dest_buf + i * dest_rowstride);
1135 guchar *end = p + 4 * width;
1139 *q = (p[3] << 24) | (p[0] << 16) | (p[1] << 8) | p[2];
1146 guint32 *p = (guint32 *)row;
1147 guint32 *q = (guint32 *)(dest_buf + i * dest_rowstride);
1148 guint32 *end = p + width;
1150 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
1151 if (dest_byteorder == GDK_LSB_FIRST)
1157 *q = ( (*p & 0xff00ff00) |
1158 ((*p & 0x000000ff) << 16) |
1159 ((*p & 0x00ff0000) >> 16));
1170 *q = (((*p & 0xff000000) >> 24) |
1171 ((*p & 0x00ffffff) << 8));
1176 #else /* G_BYTE_ORDER == G_BIG_ENDIAN */
1177 if (dest_byteorder == GDK_LSB_FIRST)
1183 *q = ( (*p & 0x00ff00ff) |
1184 ((*p & 0x0000ff00) << 16) |
1185 ((*p & 0xff000000) >> 16));
1196 *q = (((*p & 0xffffff00) >> 8) |
1197 ((*p & 0x000000ff) << 24));
1202 #endif /* G_BYTE_ORDER*/
1206 case GDK_X11_FORMAT_ARGB:
1208 guchar *p = (src_buf + i * src_rowstride);
1209 guchar *q = (dest_buf + i * dest_rowstride);
1210 guchar *end = p + 4 * width;
1213 #define MULT(d,c,a,t) G_STMT_START { t = c * a; d = ((t >> 8) + t) >> 8; } G_STMT_END
1215 if (dest_byteorder == GDK_LSB_FIRST)
1219 MULT(q[0], p[2], p[3], t1);
1220 MULT(q[1], p[1], p[3], t2);
1221 MULT(q[2], p[0], p[3], t3);
1232 MULT(q[1], p[0], p[3], t1);
1233 MULT(q[2], p[1], p[3], t2);
1234 MULT(q[3], p[2], p[3], t3);
1242 case GDK_X11_FORMAT_NONE:
1243 g_assert_not_reached ();
1250 draw_with_images (GdkDrawable *drawable,
1252 GdkX11FormatType format_type,
1253 XRenderPictFormat *format,
1254 XRenderPictFormat *mask_format,
1262 GdkScreen *screen = GDK_DRAWABLE_IMPL_X11 (drawable)->screen;
1263 Display *xdisplay = GDK_SCREEN_XDISPLAY (screen);
1269 Picture mask = None;
1272 pix = gdk_pixmap_new (gdk_screen_get_root_window (screen), width, height, 32);
1274 pict = XRenderCreatePicture (xdisplay,
1275 GDK_PIXMAP_XID (pix),
1278 mask = XRenderCreatePicture (xdisplay,
1279 GDK_PIXMAP_XID (pix),
1280 mask_format, 0, NULL);
1282 dest_pict = gdk_x11_drawable_get_picture (drawable);
1284 pix_gc = _gdk_drawable_get_scratch_gc (pix, FALSE);
1286 for (y0 = 0; y0 < height; y0 += GDK_SCRATCH_IMAGE_HEIGHT)
1288 gint height1 = MIN (height - y0, GDK_SCRATCH_IMAGE_HEIGHT);
1289 for (x0 = 0; x0 < width; x0 += GDK_SCRATCH_IMAGE_WIDTH)
1293 gint width1 = MIN (width - x0, GDK_SCRATCH_IMAGE_WIDTH);
1295 image = _gdk_image_get_scratch (screen, width1, height1, 32, &xs0, &ys0);
1297 _gdk_x11_convert_to_format (src_rgb + y0 * src_rowstride + 4 * x0, src_rowstride,
1298 (guchar *)image->mem + ys0 * image->bpl + xs0 * image->bpp, image->bpl,
1299 format_type, image->byte_order,
1302 gdk_draw_image (pix, pix_gc,
1303 image, xs0, ys0, x0, y0, width1, height1);
1307 XRenderComposite (xdisplay, PictOpOver, pict, mask, dest_pict,
1308 0, 0, 0, 0, dest_x, dest_y, width, height);
1310 XRenderFreePicture (xdisplay, pict);
1312 XRenderFreePicture (xdisplay, mask);
1314 g_object_unref (pix);
1317 typedef struct _ShmPixmapInfo ShmPixmapInfo;
1319 struct _ShmPixmapInfo
1328 shm_pixmap_info_destroy (gpointer data)
1330 ShmPixmapInfo *info = data;
1332 if (info->pict != None)
1333 XRenderFreePicture (info->display, info->pict);
1334 if (info->mask != None)
1335 XRenderFreePicture (info->display, info->mask);
1341 /* Returns FALSE if we can't get a shm pixmap */
1343 get_shm_pixmap_for_image (Display *xdisplay,
1345 XRenderPictFormat *format,
1346 XRenderPictFormat *mask_format,
1351 ShmPixmapInfo *info;
1353 if (image->type != GDK_IMAGE_SHARED)
1356 info = g_object_get_data (G_OBJECT (image), "gdk-x11-shm-pixmap");
1359 *pix = _gdk_x11_image_get_shm_pixmap (image);
1364 info = g_new (ShmPixmapInfo, 1);
1365 info->display = xdisplay;
1368 info->pict = XRenderCreatePicture (xdisplay, info->pix,
1371 info->mask = XRenderCreatePicture (xdisplay, info->pix,
1372 mask_format, 0, NULL);
1376 g_object_set_data_full (G_OBJECT (image), "gdk-x11-shm-pixmap", info,
1377 shm_pixmap_info_destroy);
1388 /* Returns FALSE if drawing with ShmPixmaps is not possible */
1390 draw_with_pixmaps (GdkDrawable *drawable,
1392 GdkX11FormatType format_type,
1393 XRenderPictFormat *format,
1394 XRenderPictFormat *mask_format,
1402 Display *xdisplay = GDK_SCREEN_XDISPLAY (GDK_DRAWABLE_IMPL_X11 (drawable)->screen);
1407 Picture mask = None;
1410 dest_pict = gdk_x11_drawable_get_picture (drawable);
1412 for (y0 = 0; y0 < height; y0 += GDK_SCRATCH_IMAGE_HEIGHT)
1414 gint height1 = MIN (height - y0, GDK_SCRATCH_IMAGE_HEIGHT);
1415 for (x0 = 0; x0 < width; x0 += GDK_SCRATCH_IMAGE_WIDTH)
1419 gint width1 = MIN (width - x0, GDK_SCRATCH_IMAGE_WIDTH);
1421 image = _gdk_image_get_scratch (GDK_DRAWABLE_IMPL_X11 (drawable)->screen,
1422 width1, height1, 32, &xs0, &ys0);
1423 if (!get_shm_pixmap_for_image (xdisplay, image, format, mask_format, &pix, &pict, &mask))
1426 _gdk_x11_convert_to_format (src_rgb + y0 * src_rowstride + 4 * x0, src_rowstride,
1427 (guchar *)image->mem + ys0 * image->bpl + xs0 * image->bpp, image->bpl,
1428 format_type, image->byte_order,
1431 XRenderComposite (xdisplay, PictOpOver, pict, mask, dest_pict,
1432 xs0, ys0, xs0, ys0, x0 + dest_x, y0 + dest_y,
1442 gdk_x11_draw_pixbuf (GdkDrawable *drawable,
1451 GdkRgbDither dither,
1455 GdkX11FormatType format_type;
1456 XRenderPictFormat *format, *mask_format;
1459 gboolean use_pixmaps = TRUE;
1460 #endif /* USE_SHM */
1462 format_type = select_format (gdk_drawable_get_display (drawable),
1463 &format, &mask_format);
1465 if (format_type == GDK_X11_FORMAT_NONE ||
1466 !gdk_pixbuf_get_has_alpha (pixbuf) ||
1467 gdk_drawable_get_depth (drawable) == 1 ||
1468 (dither == GDK_RGB_DITHER_MAX && gdk_drawable_get_depth (drawable) != 24) ||
1469 gdk_x11_drawable_get_picture (drawable) == None)
1471 GdkDrawable *wrapper = GDK_DRAWABLE_IMPL_X11 (drawable)->wrapper;
1472 GDK_DRAWABLE_CLASS (_gdk_drawable_impl_x11_parent_class)->draw_pixbuf (wrapper, gc, pixbuf,
1473 src_x, src_y, dest_x, dest_y,
1475 dither, x_dither, y_dither);
1479 gdk_x11_drawable_update_picture_clip (drawable, gc);
1481 rowstride = gdk_pixbuf_get_rowstride (pixbuf);
1486 if (!draw_with_pixmaps (drawable, gc,
1487 format_type, format, mask_format,
1488 gdk_pixbuf_get_pixels (pixbuf) + src_y * rowstride + src_x * 4,
1490 dest_x, dest_y, width, height))
1491 use_pixmaps = FALSE;
1495 #endif /* USE_SHM */
1496 draw_with_images (drawable, gc,
1497 format_type, format, mask_format,
1498 gdk_pixbuf_get_pixels (pixbuf) + src_y * rowstride + src_x * 4,
1500 dest_x, dest_y, width, height);
1504 gdk_x11_cairo_surface_destroy (void *data)
1506 GdkDrawableImplX11 *impl = data;
1508 impl->cairo_surface = NULL;
1512 _gdk_windowing_set_cairo_surface_size (cairo_surface_t *surface,
1516 cairo_xlib_surface_set_size (surface, width, height);
1520 _gdk_windowing_create_cairo_surface (GdkDrawable *drawable,
1524 GdkDrawableImplX11 *impl = GDK_DRAWABLE_IMPL_X11 (drawable);
1527 visual = gdk_drawable_get_visual (drawable);
1529 return cairo_xlib_surface_create (GDK_SCREEN_XDISPLAY (impl->screen),
1531 GDK_VISUAL_XVISUAL (visual),
1533 else if (gdk_drawable_get_depth (drawable) == 1)
1534 return cairo_xlib_surface_create_for_bitmap (GDK_SCREEN_XDISPLAY (impl->screen),
1536 GDK_SCREEN_XSCREEN (impl->screen),
1540 g_warning ("Using Cairo rendering requires the drawable argument to\n"
1541 "have a specified colormap. All windows have a colormap,\n"
1542 "however, pixmaps only have colormap by default if they\n"
1543 "were created with a non-NULL window argument. Otherwise\n"
1544 "a colormap must be set on them with gdk_drawable_set_colormap");
1550 static cairo_surface_t *
1551 gdk_x11_ref_cairo_surface (GdkDrawable *drawable)
1553 GdkDrawableImplX11 *impl = GDK_DRAWABLE_IMPL_X11 (drawable);
1555 if (GDK_IS_WINDOW_IMPL_X11 (drawable) &&
1556 GDK_WINDOW_DESTROYED (impl->wrapper))
1559 if (!impl->cairo_surface)
1563 gdk_drawable_get_size (drawable, &width, &height);
1565 impl->cairo_surface = _gdk_windowing_create_cairo_surface (drawable, width, height);
1567 if (impl->cairo_surface)
1568 cairo_surface_set_user_data (impl->cairo_surface, &gdk_x11_cairo_key,
1569 drawable, gdk_x11_cairo_surface_destroy);
1572 cairo_surface_reference (impl->cairo_surface);
1574 return impl->cairo_surface;
1577 #define __GDK_DRAWABLE_X11_C__
1578 #include "gdkaliasdef.c"