Debian Codegraph @

gdk/x11/gdkdrawable-x11.c

raw
1 /* GIMP Drawing Kit
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
3 *
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.
8 *
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.
13 *
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.
18 */
19
20 /*
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/.
25 */
26
27 #include "config.h"
28
29 #include "gdkx.h"
30 #include "gdkregion-generic.h"
31
32 #include <cairo-xlib.h>
33
34 #include <stdlib.h>
35 #include <string.h> /* for memcpy() */
36
37 #if defined (HAVE_IPC_H) && defined (HAVE_SHM_H) && defined (HAVE_XSHM_H)
38 #define USE_SHM
39 #endif
40
41 #ifdef USE_SHM
42 #include <X11/extensions/XShm.h>
43 #endif /* USE_SHM */
44
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"
50
51 #include "gdkalias.h"
52
53 static void gdk_x11_draw_rectangle (GdkDrawable *drawable,
54 GdkGC *gc,
55 gboolean filled,
56 gint x,
57 gint y,
58 gint width,
59 gint height);
60 static void gdk_x11_draw_arc (GdkDrawable *drawable,
61 GdkGC *gc,
62 gboolean filled,
63 gint x,
64 gint y,
65 gint width,
66 gint height,
67 gint angle1,
68 gint angle2);
69 static void gdk_x11_draw_polygon (GdkDrawable *drawable,
70 GdkGC *gc,
71 gboolean filled,
72 GdkPoint *points,
73 gint npoints);
74 static void gdk_x11_draw_text (GdkDrawable *drawable,
75 GdkFont *font,
76 GdkGC *gc,
77 gint x,
78 gint y,
79 const gchar *text,
80 gint text_length);
81 static void gdk_x11_draw_text_wc (GdkDrawable *drawable,
82 GdkFont *font,
83 GdkGC *gc,
84 gint x,
85 gint y,
86 const GdkWChar *text,
87 gint text_length);
88 static void gdk_x11_draw_drawable (GdkDrawable *drawable,
89 GdkGC *gc,
90 GdkPixmap *src,
91 gint xsrc,
92 gint ysrc,
93 gint xdest,
94 gint ydest,
95 gint width,
96 gint height,
97 GdkDrawable *original_src);
98 static void gdk_x11_draw_points (GdkDrawable *drawable,
99 GdkGC *gc,
100 GdkPoint *points,
101 gint npoints);
102 static void gdk_x11_draw_segments (GdkDrawable *drawable,
103 GdkGC *gc,
104 GdkSegment *segs,
105 gint nsegs);
106 static void gdk_x11_draw_lines (GdkDrawable *drawable,
107 GdkGC *gc,
108 GdkPoint *points,
109 gint npoints);
110
111 static void gdk_x11_draw_image (GdkDrawable *drawable,
112 GdkGC *gc,
113 GdkImage *image,
114 gint xsrc,
115 gint ysrc,
116 gint xdest,
117 gint ydest,
118 gint width,
119 gint height);
120 static void gdk_x11_draw_pixbuf (GdkDrawable *drawable,
121 GdkGC *gc,
122 GdkPixbuf *pixbuf,
123 gint src_x,
124 gint src_y,
125 gint dest_x,
126 gint dest_y,
127 gint width,
128 gint height,
129 GdkRgbDither dither,
130 gint x_dither,
131 gint y_dither);
132
133 static cairo_surface_t *gdk_x11_ref_cairo_surface (GdkDrawable *drawable);
134
135 static void gdk_x11_set_colormap (GdkDrawable *drawable,
136 GdkColormap *colormap);
137
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);
142
143 static void gdk_drawable_impl_x11_finalize (GObject *object);
144
145 static const cairo_user_data_key_t gdk_x11_cairo_key;
146
147 G_DEFINE_TYPE (GdkDrawableImplX11, _gdk_drawable_impl_x11, GDK_TYPE_DRAWABLE)
148
149 static void
150 _gdk_drawable_impl_x11_class_init (GdkDrawableImplX11Class *klass)
151 {
152 GdkDrawableClass *drawable_class = GDK_DRAWABLE_CLASS (klass);
153 GObjectClass *object_class = G_OBJECT_CLASS (klass);
154
155 object_class->finalize = gdk_drawable_impl_x11_finalize;
156
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_with_src = 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;
169
170 drawable_class->ref_cairo_surface = gdk_x11_ref_cairo_surface;
171
172 drawable_class->set_colormap = gdk_x11_set_colormap;
173 drawable_class->get_colormap = gdk_x11_get_colormap;
174
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;
178
179 drawable_class->_copy_to_image = _gdk_x11_copy_to_image;
180 }
181
182 static void
183 _gdk_drawable_impl_x11_init (GdkDrawableImplX11 *impl)
184 {
185 }
186
187 static void
188 gdk_drawable_impl_x11_finalize (GObject *object)
189 {
190 gdk_drawable_set_colormap (GDK_DRAWABLE (object), NULL);
191
192 G_OBJECT_CLASS (_gdk_drawable_impl_x11_parent_class)->finalize (object);
193 }
194
195 /**
196 * _gdk_x11_drawable_finish:
197 * @drawable: a #GdkDrawableImplX11.
198 *
199 * Performs necessary cleanup prior to freeing a pixmap or
200 * destroying a window.
201 **/
202 void
203 _gdk_x11_drawable_finish (GdkDrawable *drawable)
204 {
205 GdkDrawableImplX11 *impl = GDK_DRAWABLE_IMPL_X11 (drawable);
206
207 if (impl->picture)
208 {
209 XRenderFreePicture (GDK_SCREEN_XDISPLAY (impl->screen),
210 impl->picture);
211 impl->picture = None;
212 }
213
214 if (impl->cairo_surface)
215 {
216 cairo_surface_finish (impl->cairo_surface);
217 cairo_surface_set_user_data (impl->cairo_surface, &gdk_x11_cairo_key,
218 NULL, NULL);
219 }
220 }
221
222 /**
223 * _gdk_x11_drawable_update_size:
224 * @drawable: a #GdkDrawableImplX11.
225 *
226 * Updates the state of the drawable (in particular the drawable's
227 * cairo surface) when its size has changed.
228 **/
229 void
230 _gdk_x11_drawable_update_size (GdkDrawable *drawable)
231 {
232 GdkDrawableImplX11 *impl = GDK_DRAWABLE_IMPL_X11 (drawable);
233
234 if (impl->cairo_surface)
235 {
236 int width, height;
237
238 gdk_drawable_get_size (drawable, &width, &height);
239 cairo_xlib_surface_set_size (impl->cairo_surface, width, height);
240 }
241 }
242
243 static void
244 try_pixmap (Display *xdisplay,
245 int screen,
246 int depth)
247 {
248 Pixmap pixmap = XCreatePixmap (xdisplay,
249 RootWindow (xdisplay, screen),
250 1, 1, depth);
251 XFreePixmap (xdisplay, pixmap);
252 }
253
254 gboolean
255 _gdk_x11_have_render (GdkDisplay *display)
256 {
257 Display *xdisplay = GDK_DISPLAY_XDISPLAY (display);
258 GdkDisplayX11 *x11display = GDK_DISPLAY_X11 (display);
259
260 if (x11display->have_render == GDK_UNKNOWN)
261 {
262 int event_base, error_base;
263 x11display->have_render =
264 XRenderQueryExtension (xdisplay, &event_base, &error_base)
265 ? GDK_YES : GDK_NO;
266
267 if (x11display->have_render == GDK_YES)
268 {
269 /*
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.
273 */
274
275 int screen;
276 for (screen = 0; screen < ScreenCount (xdisplay); screen++)
277 {
278 int count;
279 int *depths = XListDepths (xdisplay, screen, &count);
280 gboolean has_8 = FALSE, has_32 = FALSE;
281
282 if (depths)
283 {
284 int i;
285
286 for (i = 0; i < count; i++)
287 {
288 if (depths[i] == 8)
289 has_8 = TRUE;
290 else if (depths[i] == 32)
291 has_32 = TRUE;
292 }
293 XFree (depths);
294 }
295
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.
300 */
301 if (!(has_8 && has_32))
302 {
303 gdk_error_trap_push ();
304 if (!has_8)
305 try_pixmap (xdisplay, screen, 8);
306 if (!has_32)
307 try_pixmap (xdisplay, screen, 32);
308 XSync (xdisplay, False);
309 if (gdk_error_trap_pop () == 0)
310 {
311 has_8 = TRUE;
312 has_32 = TRUE;
313 }
314 }
315
316 if (!(has_8 && has_32))
317 {
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;
322 break;
323 }
324 }
325 }
326 }
327
328 return x11display->have_render == GDK_YES;
329 }
330
331 static Picture
332 gdk_x11_drawable_get_picture (GdkDrawable *drawable)
333 {
334 GdkDrawableImplX11 *impl = GDK_DRAWABLE_IMPL_X11 (drawable);
335
336 if (!impl->picture)
337 {
338 Display *xdisplay = GDK_SCREEN_XDISPLAY (impl->screen);
339 XRenderPictFormat *format;
340
341 GdkVisual *visual = gdk_drawable_get_visual (GDK_DRAWABLE_IMPL_X11 (drawable)->wrapper);
342 if (!visual)
343 return None;
344
345 format = XRenderFindVisualFormat (xdisplay, GDK_VISUAL_XVISUAL (visual));
346 if (format)
347 {
348 XRenderPictureAttributes attributes;
349 attributes.graphics_exposures = False;
350
351 impl->picture = XRenderCreatePicture (xdisplay, impl->xid, format,
352 CPGraphicsExposure, &attributes);
353 }
354 }
355
356 return impl->picture;
357 }
358
359 static void
360 gdk_x11_drawable_update_picture_clip (GdkDrawable *drawable,
361 GdkGC *gc)
362 {
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;
367
368 if (clip_region)
369 {
370 GdkRegionBox *boxes = clip_region->rects;
371 gint n_boxes = clip_region->numRects;
372 XRectangle *rects = g_new (XRectangle, n_boxes);
373 int i;
374
375 for (i=0; i < n_boxes; i++)
376 {
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;
381 }
382
383 XRenderSetPictureClipRectangles (xdisplay, picture,
384 0, 0, rects, n_boxes);
385
386 g_free (rects);
387 }
388 else
389 {
390 XRenderPictureAttributes pa;
391 GdkBitmap *mask;
392 gulong pa_mask;
393
394 pa_mask = CPClipMask;
395 if (gc && (mask = _gdk_gc_get_clip_mask (gc)))
396 {
397 pa.clip_mask = GDK_PIXMAP_XID (mask);
398 pa.clip_x_origin = gc->clip_x_origin;
399 pa.clip_y_origin = gc->clip_y_origin;
400 pa_mask |= CPClipXOrigin | CPClipYOrigin;
401 }
402 else
403 pa.clip_mask = None;
404
405 XRenderChangePicture (xdisplay, picture,
406 pa_mask, &pa);
407 }
408 }
409
410 /*****************************************************
411 * X11 specific implementations of generic functions *
412 *****************************************************/
413
414 static GdkColormap*
415 gdk_x11_get_colormap (GdkDrawable *drawable)
416 {
417 GdkDrawableImplX11 *impl;
418
419 impl = GDK_DRAWABLE_IMPL_X11 (drawable);
420
421 return impl->colormap;
422 }
423
424 static void
425 gdk_x11_set_colormap (GdkDrawable *drawable,
426 GdkColormap *colormap)
427 {
428 GdkDrawableImplX11 *impl;
429
430 impl = GDK_DRAWABLE_IMPL_X11 (drawable);
431
432 if (impl->colormap == colormap)
433 return;
434
435 if (impl->colormap)
436 g_object_unref (impl->colormap);
437 impl->colormap = colormap;
438 if (impl->colormap)
439 g_object_ref (impl->colormap);
440 }
441
442 /* Drawing
443 */
444
445 static void
446 gdk_x11_draw_rectangle (GdkDrawable *drawable,
447 GdkGC *gc,
448 gboolean filled,
449 gint x,
450 gint y,
451 gint width,
452 gint height)
453 {
454 GdkDrawableImplX11 *impl;
455
456 impl = GDK_DRAWABLE_IMPL_X11 (drawable);
457
458 if (filled)
459 XFillRectangle (GDK_SCREEN_XDISPLAY (impl->screen), impl->xid,
460 GDK_GC_GET_XGC (gc), x, y, width, height);
461 else
462 XDrawRectangle (GDK_SCREEN_XDISPLAY (impl->screen), impl->xid,
463 GDK_GC_GET_XGC (gc), x, y, width, height);
464 }
465
466 static void
467 gdk_x11_draw_arc (GdkDrawable *drawable,
468 GdkGC *gc,
469 gboolean filled,
470 gint x,
471 gint y,
472 gint width,
473 gint height,
474 gint angle1,
475 gint angle2)
476 {
477 GdkDrawableImplX11 *impl;
478
479 impl = GDK_DRAWABLE_IMPL_X11 (drawable);
480
481
482 if (filled)
483 XFillArc (GDK_SCREEN_XDISPLAY (impl->screen), impl->xid,
484 GDK_GC_GET_XGC (gc), x, y, width, height, angle1, angle2);
485 else
486 XDrawArc (GDK_SCREEN_XDISPLAY (impl->screen), impl->xid,
487 GDK_GC_GET_XGC (gc), x, y, width, height, angle1, angle2);
488 }
489
490 static void
491 gdk_x11_draw_polygon (GdkDrawable *drawable,
492 GdkGC *gc,
493 gboolean filled,
494 GdkPoint *points,
495 gint npoints)
496 {
497 XPoint *tmp_points;
498 gint tmp_npoints, i;
499 GdkDrawableImplX11 *impl;
500
501 impl = GDK_DRAWABLE_IMPL_X11 (drawable);
502
503
504 if (!filled &&
505 (points[0].x != points[npoints-1].x || points[0].y != points[npoints-1].y))
506 {
507 tmp_npoints = npoints + 1;
508 tmp_points = g_new (XPoint, tmp_npoints);
509 tmp_points[npoints].x = points[0].x;
510 tmp_points[npoints].y = points[0].y;
511 }
512 else
513 {
514 tmp_npoints = npoints;
515 tmp_points = g_new (XPoint, tmp_npoints);
516 }
517
518 for (i=0; i<npoints; i++)
519 {
520 tmp_points[i].x = points[i].x;
521 tmp_points[i].y = points[i].y;
522 }
523
524 if (filled)
525 XFillPolygon (GDK_SCREEN_XDISPLAY (impl->screen), impl->xid,
526 GDK_GC_GET_XGC (gc), tmp_points, tmp_npoints, Complex, CoordModeOrigin);
527 else
528 XDrawLines (GDK_SCREEN_XDISPLAY (impl->screen), impl->xid,
529 GDK_GC_GET_XGC (gc), tmp_points, tmp_npoints, CoordModeOrigin);
530
531 g_free (tmp_points);
532 }
533
534 /* gdk_x11_draw_text
535 *
536 * Modified by Li-Da Lho to draw 16 bits and Multibyte strings
537 *
538 * Interface changed: add "GdkFont *font" to specify font or fontset explicitely
539 */
540 static void
541 gdk_x11_draw_text (GdkDrawable *drawable,
542 GdkFont *font,
543 GdkGC *gc,
544 gint x,
545 gint y,
546 const gchar *text,
547 gint text_length)
548 {
549 GdkDrawableImplX11 *impl;
550 Display *xdisplay;
551
552 impl = GDK_DRAWABLE_IMPL_X11 (drawable);
553 xdisplay = GDK_SCREEN_XDISPLAY (impl->screen);
554
555 if (font->type == GDK_FONT_FONT)
556 {
557 XFontStruct *xfont = (XFontStruct *) GDK_FONT_XFONT (font);
558 XSetFont(xdisplay, GDK_GC_GET_XGC (gc), xfont->fid);
559 if ((xfont->min_byte1 == 0) && (xfont->max_byte1 == 0))
560 {
561 XDrawString (xdisplay, impl->xid,
562 GDK_GC_GET_XGC (gc), x, y, text, text_length);
563 }
564 else
565 {
566 XDrawString16 (xdisplay, impl->xid,
567 GDK_GC_GET_XGC (gc), x, y, (XChar2b *) text, text_length / 2);
568 }
569 }
570 else if (font->type == GDK_FONT_FONTSET)
571 {
572 XFontSet fontset = (XFontSet) GDK_FONT_XFONT (font);
573 XmbDrawString (xdisplay, impl->xid,
574 fontset, GDK_GC_GET_XGC (gc), x, y, text, text_length);
575 }
576 else
577 g_error("undefined font type\n");
578 }
579
580 static void
581 gdk_x11_draw_text_wc (GdkDrawable *drawable,
582 GdkFont *font,
583 GdkGC *gc,
584 gint x,
585 gint y,
586 const GdkWChar *text,
587 gint text_length)
588 {
589 GdkDrawableImplX11 *impl;
590 Display *xdisplay;
591
592 impl = GDK_DRAWABLE_IMPL_X11 (drawable);
593 xdisplay = GDK_SCREEN_XDISPLAY (impl->screen);
594
595 if (font->type == GDK_FONT_FONT)
596 {
597 XFontStruct *xfont = (XFontStruct *) GDK_FONT_XFONT (font);
598 gchar *text_8bit;
599 gint i;
600 XSetFont(xdisplay, GDK_GC_GET_XGC (gc), xfont->fid);
601 text_8bit = g_new (gchar, text_length);
602 for (i=0; i<text_length; i++) text_8bit[i] = text[i];
603 XDrawString (xdisplay, impl->xid,
604 GDK_GC_GET_XGC (gc), x, y, text_8bit, text_length);
605 g_free (text_8bit);
606 }
607 else if (font->type == GDK_FONT_FONTSET)
608 {
609 if (sizeof(GdkWChar) == sizeof(wchar_t))
610 {
611 XwcDrawString (xdisplay, impl->xid,
612 (XFontSet) GDK_FONT_XFONT (font),
613 GDK_GC_GET_XGC (gc), x, y, (wchar_t *)text, text_length);
614 }
615 else
616 {
617 wchar_t *text_wchar;
618 gint i;
619 text_wchar = g_new (wchar_t, text_length);
620 for (i=0; i<text_length; i++) text_wchar[i] = text[i];
621 XwcDrawString (xdisplay, impl->xid,
622 (XFontSet) GDK_FONT_XFONT (font),
623 GDK_GC_GET_XGC (gc), x, y, text_wchar, text_length);
624 g_free (text_wchar);
625 }
626 }
627 else
628 g_error("undefined font type\n");
629 }
630
631 static void
632 gdk_x11_draw_drawable (GdkDrawable *drawable,
633 GdkGC *gc,
634 GdkPixmap *src,
635 gint xsrc,
636 gint ysrc,
637 gint xdest,
638 gint ydest,
639 gint width,
640 gint height,
641 GdkDrawable *original_src)
642 {
643 int src_depth = gdk_drawable_get_depth (src);
644 int dest_depth = gdk_drawable_get_depth (drawable);
645 GdkDrawableImplX11 *impl;
646 GdkDrawableImplX11 *src_impl;
647
648 impl = GDK_DRAWABLE_IMPL_X11 (drawable);
649
650 if (GDK_IS_DRAWABLE_IMPL_X11 (src))
651 src_impl = GDK_DRAWABLE_IMPL_X11 (src);
652 else if (GDK_IS_WINDOW (src))
653 src_impl = GDK_DRAWABLE_IMPL_X11(((GdkWindowObject *)src)->impl);
654 else
655 src_impl = GDK_DRAWABLE_IMPL_X11(((GdkPixmapObject *)src)->impl);
656
657 if (GDK_IS_WINDOW_IMPL_X11 (impl) &&
658 GDK_IS_PIXMAP_IMPL_X11 (src_impl))
659 {
660 GdkPixmapImplX11 *src_pixmap = GDK_PIXMAP_IMPL_X11 (src_impl);
661 /* Work around an Xserver bug where non-visible areas from
662 * a pixmap to a window will clear the window background
663 * in destination areas that are supposed to be clipped out.
664 * This is a problem with client side windows as this means
665 * things may draw outside the virtual windows. This could
666 * also happen for window to window copies, but I don't
667 * think we generate any calls like that.
668 *
669 * See:
670 * http://lists.freedesktop.org/archives/xorg/2009-February/043318.html
671 */
672 if (xsrc < 0)
673 {
674 width += xsrc;
675 xdest -= xsrc;
676 xsrc = 0;
677 }
678
679 if (ysrc < 0)
680 {
681 height += ysrc;
682 ydest -= ysrc;
683 ysrc = 0;
684 }
685
686 if (xsrc + width > src_pixmap->width)
687 width = src_pixmap->width - xsrc;
688 if (ysrc + height > src_pixmap->height)
689 height = src_pixmap->height - ysrc;
690 }
691
692 if (src_depth == 1)
693 {
694 XCopyArea (GDK_SCREEN_XDISPLAY (impl->screen),
695 src_impl->xid,
696 impl->xid,
697 GDK_GC_GET_XGC (gc),
698 xsrc, ysrc,
699 width, height,
700 xdest, ydest);
701 }
702 else if (dest_depth != 0 && src_depth == dest_depth)
703 {
704 XCopyArea (GDK_SCREEN_XDISPLAY (impl->screen),
705 src_impl->xid,
706 impl->xid,
707 GDK_GC_GET_XGC (gc),
708 xsrc, ysrc,
709 width, height,
710 xdest, ydest);
711 }
712 else
713 g_warning ("Attempt to draw a drawable with depth %d to a drawable with depth %d",
714 src_depth, dest_depth);
715 }
716
717 static void
718 gdk_x11_draw_points (GdkDrawable *drawable,
719 GdkGC *gc,
720 GdkPoint *points,
721 gint npoints)
722 {
723 GdkDrawableImplX11 *impl;
724
725 impl = GDK_DRAWABLE_IMPL_X11 (drawable);
726
727
728 /* We special-case npoints == 1, because X will merge multiple
729 * consecutive XDrawPoint requests into a PolyPoint request
730 */
731 if (npoints == 1)
732 {
733 XDrawPoint (GDK_SCREEN_XDISPLAY (impl->screen),
734 impl->xid,
735 GDK_GC_GET_XGC (gc),
736 points[0].x, points[0].y);
737 }
738 else
739 {
740 gint i;
741 XPoint *tmp_points = g_new (XPoint, npoints);
742
743 for (i=0; i<npoints; i++)
744 {
745 tmp_points[i].x = points[i].x;
746 tmp_points[i].y = points[i].y;
747 }
748
749 XDrawPoints (GDK_SCREEN_XDISPLAY (impl->screen),
750 impl->xid,
751 GDK_GC_GET_XGC (gc),
752 tmp_points,
753 npoints,
754 CoordModeOrigin);
755
756 g_free (tmp_points);
757 }
758 }
759
760 static void
761 gdk_x11_draw_segments (GdkDrawable *drawable,
762 GdkGC *gc,
763 GdkSegment *segs,
764 gint nsegs)
765 {
766 GdkDrawableImplX11 *impl;
767
768 impl = GDK_DRAWABLE_IMPL_X11 (drawable);
769
770
771 /* We special-case nsegs == 1, because X will merge multiple
772 * consecutive XDrawLine requests into a PolySegment request
773 */
774 if (nsegs == 1)
775 {
776 XDrawLine (GDK_SCREEN_XDISPLAY (impl->screen), impl->xid,
777 GDK_GC_GET_XGC (gc), segs[0].x1, segs[0].y1,
778 segs[0].x2, segs[0].y2);
779 }
780 else
781 {
782 gint i;
783 XSegment *tmp_segs = g_new (XSegment, nsegs);
784
785 for (i=0; i<nsegs; i++)
786 {
787 tmp_segs[i].x1 = segs[i].x1;
788 tmp_segs[i].x2 = segs[i].x2;
789 tmp_segs[i].y1 = segs[i].y1;
790 tmp_segs[i].y2 = segs[i].y2;
791 }
792
793 XDrawSegments (GDK_SCREEN_XDISPLAY (impl->screen),
794 impl->xid,
795 GDK_GC_GET_XGC (gc),
796 tmp_segs, nsegs);
797
798 g_free (tmp_segs);
799 }
800 }
801
802 static void
803 gdk_x11_draw_lines (GdkDrawable *drawable,
804 GdkGC *gc,
805 GdkPoint *points,
806 gint npoints)
807 {
808 gint i;
809 XPoint *tmp_points = g_new (XPoint, npoints);
810 GdkDrawableImplX11 *impl;
811
812 impl = GDK_DRAWABLE_IMPL_X11 (drawable);
813
814
815 for (i=0; i<npoints; i++)
816 {
817 tmp_points[i].x = points[i].x;
818 tmp_points[i].y = points[i].y;
819 }
820
821 XDrawLines (GDK_SCREEN_XDISPLAY (impl->screen),
822 impl->xid,
823 GDK_GC_GET_XGC (gc),
824 tmp_points, npoints,
825 CoordModeOrigin);
826
827 g_free (tmp_points);
828 }
829
830 static void
831 gdk_x11_draw_image (GdkDrawable *drawable,
832 GdkGC *gc,
833 GdkImage *image,
834 gint xsrc,
835 gint ysrc,
836 gint xdest,
837 gint ydest,
838 gint width,
839 gint height)
840 {
841 GdkDrawableImplX11 *impl;
842
843 impl = GDK_DRAWABLE_IMPL_X11 (drawable);
844
845 #ifdef USE_SHM
846 if (image->type == GDK_IMAGE_SHARED)
847 XShmPutImage (GDK_SCREEN_XDISPLAY (impl->screen), impl->xid,
848 GDK_GC_GET_XGC (gc), GDK_IMAGE_XIMAGE (image),
849 xsrc, ysrc, xdest, ydest, width, height, False);
850 else
851 #endif
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);
855 }
856
857 static gint
858 gdk_x11_get_depth (GdkDrawable *drawable)
859 {
860 /* This is a bit bogus but I'm not sure the other way is better */
861
862 return gdk_drawable_get_depth (GDK_DRAWABLE_IMPL_X11 (drawable)->wrapper);
863 }
864
865 static GdkDrawable *
866 get_impl_drawable (GdkDrawable *drawable)
867 {
868 if (GDK_IS_WINDOW (drawable))
869 return ((GdkWindowObject *)drawable)->impl;
870 else if (GDK_IS_PIXMAP (drawable))
871 return ((GdkPixmapObject *)drawable)->impl;
872 else
873 {
874 g_warning (G_STRLOC " drawable is not a pixmap or window");
875 return NULL;
876 }
877 }
878
879 static GdkScreen*
880 gdk_x11_get_screen (GdkDrawable *drawable)
881 {
882 if (GDK_IS_DRAWABLE_IMPL_X11 (drawable))
883 return GDK_DRAWABLE_IMPL_X11 (drawable)->screen;
884 else
885 return GDK_DRAWABLE_IMPL_X11 (get_impl_drawable (drawable))->screen;
886 }
887
888 static GdkVisual*
889 gdk_x11_get_visual (GdkDrawable *drawable)
890 {
891 return gdk_drawable_get_visual (GDK_DRAWABLE_IMPL_X11 (drawable)->wrapper);
892 }
893
894 /**
895 * gdk_x11_drawable_get_xdisplay:
896 * @drawable: a #GdkDrawable.
897 *
898 * Returns the display of a #GdkDrawable.
899 *
900 * Return value: an Xlib <type>Display*</type>.
901 **/
902 Display *
903 gdk_x11_drawable_get_xdisplay (GdkDrawable *drawable)
904 {
905 if (GDK_IS_DRAWABLE_IMPL_X11 (drawable))
906 return GDK_SCREEN_XDISPLAY (GDK_DRAWABLE_IMPL_X11 (drawable)->screen);
907 else
908 return GDK_SCREEN_XDISPLAY (GDK_DRAWABLE_IMPL_X11 (get_impl_drawable (drawable))->screen);
909 }
910
911 /**
912 * gdk_x11_drawable_get_xid:
913 * @drawable: a #GdkDrawable.
914 *
915 * Returns the X resource (window or pixmap) belonging to a #GdkDrawable.
916 *
917 * Return value: the ID of @drawable's X resource.
918 **/
919 XID
920 gdk_x11_drawable_get_xid (GdkDrawable *drawable)
921 {
922 GdkDrawable *impl;
923
924 if (GDK_IS_WINDOW (drawable))
925 {
926 GdkWindow *window = (GdkWindow *)drawable;
927
928 /* Try to ensure the window has a native window */
929 if (!_gdk_window_has_impl (window))
930 {
931 gdk_window_ensure_native (window);
932
933 /* We sync here to ensure the window is created in the Xserver when
934 * this function returns. This is required because the returned XID
935 * for this window must be valid immediately, even with another
936 * connection to the Xserver */
937 gdk_display_sync (gdk_drawable_get_display (window));
938 }
939
940 if (!GDK_WINDOW_IS_X11 (window))
941 {
942 g_warning (G_STRLOC " drawable is not a native X11 window");
943 return None;
944 }
945
946 impl = ((GdkWindowObject *)drawable)->impl;
947 }
948 else if (GDK_IS_PIXMAP (drawable))
949 impl = ((GdkPixmapObject *)drawable)->impl;
950 else
951 {
952 g_warning (G_STRLOC " drawable is not a pixmap or window");
953 return None;
954 }
955
956 return ((GdkDrawableImplX11 *)impl)->xid;
957 }
958
959 GdkDrawable *
960 gdk_x11_window_get_drawable_impl (GdkWindow *window)
961 {
962 return ((GdkWindowObject *)window)->impl;
963 }
964 GdkDrawable *
965 gdk_x11_pixmap_get_drawable_impl (GdkPixmap *pixmap)
966 {
967 return ((GdkPixmapObject *)pixmap)->impl;
968 }
969
970 /* Code for accelerated alpha compositing using the RENDER extension.
971 * It's a bit long because there are lots of possibilities for
972 * what's the fastest depending on the available picture formats,
973 * whether we can used shared pixmaps, etc.
974 */
975
976 static GdkX11FormatType
977 select_format (GdkDisplay *display,
978 XRenderPictFormat **format,
979 XRenderPictFormat **mask)
980 {
981 Display *xdisplay = GDK_DISPLAY_XDISPLAY (display);
982 XRenderPictFormat pf;
983
984 if (!_gdk_x11_have_render (display))
985 return GDK_X11_FORMAT_NONE;
986
987 /* Look for a 32-bit xRGB and Axxx formats that exactly match the
988 * in memory data format. We can use them as pixmap and mask
989 * to deal with non-premultiplied data.
990 */
991
992 pf.type = PictTypeDirect;
993 pf.depth = 32;
994 pf.direct.redMask = 0xff;
995 pf.direct.greenMask = 0xff;
996 pf.direct.blueMask = 0xff;
997
998 pf.direct.alphaMask = 0;
999 if (ImageByteOrder (xdisplay) == LSBFirst)
1000 {
1001 /* ABGR */
1002 pf.direct.red = 0;
1003 pf.direct.green = 8;
1004 pf.direct.blue = 16;
1005 }
1006 else
1007 {
1008 /* RGBA */
1009 pf.direct.red = 24;
1010 pf.direct.green = 16;
1011 pf.direct.blue = 8;
1012 }
1013
1014 *format = XRenderFindFormat (xdisplay,
1015 (PictFormatType | PictFormatDepth |
1016 PictFormatRedMask | PictFormatRed |
1017 PictFormatGreenMask | PictFormatGreen |
1018 PictFormatBlueMask | PictFormatBlue |
1019 PictFormatAlphaMask),
1020 &pf,
1021 0);
1022
1023 pf.direct.alphaMask = 0xff;
1024 if (ImageByteOrder (xdisplay) == LSBFirst)
1025 {
1026 /* ABGR */
1027 pf.direct.alpha = 24;
1028 }
1029 else
1030 {
1031 pf.direct.alpha = 0;
1032 }
1033
1034 *mask = XRenderFindFormat (xdisplay,
1035 (PictFormatType | PictFormatDepth |
1036 PictFormatAlphaMask | PictFormatAlpha),
1037 &pf,
1038 0);
1039
1040 if (*format && *mask)
1041 return GDK_X11_FORMAT_EXACT_MASK;
1042
1043 /* OK, that failed, now look for xRGB and Axxx formats in
1044 * RENDER's preferred order
1045 */
1046 pf.direct.alphaMask = 0;
1047 /* ARGB */
1048 pf.direct.red = 16;
1049 pf.direct.green = 8;
1050 pf.direct.blue = 0;
1051
1052 *format = XRenderFindFormat (xdisplay,
1053 (PictFormatType | PictFormatDepth |
1054 PictFormatRedMask | PictFormatRed |
1055 PictFormatGreenMask | PictFormatGreen |
1056 PictFormatBlueMask | PictFormatBlue |
1057 PictFormatAlphaMask),
1058 &pf,
1059 0);
1060
1061 pf.direct.alphaMask = 0xff;
1062 pf.direct.alpha = 24;
1063
1064 *mask = XRenderFindFormat (xdisplay,
1065 (PictFormatType | PictFormatDepth |
1066 PictFormatAlphaMask | PictFormatAlpha),
1067 &pf,
1068 0);
1069
1070 if (*format && *mask)
1071 return GDK_X11_FORMAT_ARGB_MASK;
1072
1073 /* Finally, if neither of the above worked, fall back to
1074 * looking for combined ARGB -- we'll premultiply ourselves.
1075 */
1076
1077 pf.type = PictTypeDirect;
1078 pf.depth = 32;
1079 pf.direct.red = 16;
1080 pf.direct.green = 8;
1081 pf.direct.blue = 0;
1082 pf.direct.alphaMask = 0xff;
1083 pf.direct.alpha = 24;
1084
1085 *format = XRenderFindFormat (xdisplay,
1086 (PictFormatType | PictFormatDepth |
1087 PictFormatRedMask | PictFormatRed |
1088 PictFormatGreenMask | PictFormatGreen |
1089 PictFormatBlueMask | PictFormatBlue |
1090 PictFormatAlphaMask | PictFormatAlpha),
1091 &pf,
1092 0);
1093 *mask = NULL;
1094
1095 if (*format)
1096 return GDK_X11_FORMAT_ARGB;
1097
1098 return GDK_X11_FORMAT_NONE;
1099 }
1100
1101 #if 0
1102 static void
1103 list_formats (XRenderPictFormat *pf)
1104 {
1105 gint i;
1106
1107 for (i=0 ;; i++)
1108 {
1109 XRenderPictFormat *pf = XRenderFindFormat (impl->xdisplay, 0, NULL, i);
1110 if (pf)
1111 {
1112 g_print ("%2d R-%#06x/%#06x G-%#06x/%#06x B-%#06x/%#06x A-%#06x/%#06x\n",
1113 pf->depth,
1114 pf->direct.red,
1115 pf->direct.redMask,
1116 pf->direct.green,
1117 pf->direct.greenMask,
1118 pf->direct.blue,
1119 pf->direct.blueMask,
1120 pf->direct.alpha,
1121 pf->direct.alphaMask);
1122 }
1123 else
1124 break;
1125 }
1126 }
1127 #endif
1128
1129 void
1130 _gdk_x11_convert_to_format (guchar *src_buf,
1131 gint src_rowstride,
1132 guchar *dest_buf,
1133 gint dest_rowstride,
1134 GdkX11FormatType dest_format,
1135 GdkByteOrder dest_byteorder,
1136 gint width,
1137 gint height)
1138 {
1139 gint i;
1140
1141 for (i=0; i < height; i++)
1142 {
1143 switch (dest_format)
1144 {
1145 case GDK_X11_FORMAT_EXACT_MASK:
1146 {
1147 memcpy (dest_buf + i * dest_rowstride,
1148 src_buf + i * src_rowstride,
1149 width * 4);
1150 break;
1151 }
1152 case GDK_X11_FORMAT_ARGB_MASK:
1153 {
1154 guchar *row = src_buf + i * src_rowstride;
1155 if (((gsize)row & 3) != 0)
1156 {
1157 guchar *p = row;
1158 guint32 *q = (guint32 *)(dest_buf + i * dest_rowstride);
1159 guchar *end = p + 4 * width;
1160
1161 while (p < end)
1162 {
1163 *q = (p[3] << 24) | (p[0] << 16) | (p[1] << 8) | p[2];
1164 p += 4;
1165 q++;
1166 }
1167 }
1168 else
1169 {
1170 guint32 *p = (guint32 *)row;
1171 guint32 *q = (guint32 *)(dest_buf + i * dest_rowstride);
1172 guint32 *end = p + width;
1173
1174 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
1175 if (dest_byteorder == GDK_LSB_FIRST)
1176 {
1177 /* ABGR => ARGB */
1178
1179 while (p < end)
1180 {
1181 *q = ( (*p & 0xff00ff00) |
1182 ((*p & 0x000000ff) << 16) |
1183 ((*p & 0x00ff0000) >> 16));
1184 q++;
1185 p++;
1186 }
1187 }
1188 else
1189 {
1190 /* ABGR => BGRA */
1191
1192 while (p < end)
1193 {
1194 *q = (((*p & 0xff000000) >> 24) |
1195 ((*p & 0x00ffffff) << 8));
1196 q++;
1197 p++;
1198 }
1199 }
1200 #else /* G_BYTE_ORDER == G_BIG_ENDIAN */
1201 if (dest_byteorder == GDK_LSB_FIRST)
1202 {
1203 /* RGBA => BGRA */
1204
1205 while (p < end)
1206 {
1207 *q = ( (*p & 0x00ff00ff) |
1208 ((*p & 0x0000ff00) << 16) |
1209 ((*p & 0xff000000) >> 16));
1210 q++;
1211 p++;
1212 }
1213 }
1214 else
1215 {
1216 /* RGBA => ARGB */
1217
1218 while (p < end)
1219 {
1220 *q = (((*p & 0xffffff00) >> 8) |
1221 ((*p & 0x000000ff) << 24));
1222 q++;
1223 p++;
1224 }
1225 }
1226 #endif /* G_BYTE_ORDER*/
1227 }
1228 break;
1229 }
1230 case GDK_X11_FORMAT_ARGB:
1231 {
1232 guchar *p = (src_buf + i * src_rowstride);
1233 guchar *q = (dest_buf + i * dest_rowstride);
1234 guchar *end = p + 4 * width;
1235 guint t1,t2,t3;
1236
1237 #define MULT(d,c,a,t) G_STMT_START { t = c * a; d = ((t >> 8) + t) >> 8; } G_STMT_END
1238
1239 if (dest_byteorder == GDK_LSB_FIRST)
1240 {
1241 while (p < end)
1242 {
1243 MULT(q[0], p[2], p[3], t1);
1244 MULT(q[1], p[1], p[3], t2);
1245 MULT(q[2], p[0], p[3], t3);
1246 q[3] = p[3];
1247 p += 4;
1248 q += 4;
1249 }
1250 }
1251 else
1252 {
1253 while (p < end)
1254 {
1255 q[0] = p[3];
1256 MULT(q[1], p[0], p[3], t1);
1257 MULT(q[2], p[1], p[3], t2);
1258 MULT(q[3], p[2], p[3], t3);
1259 p += 4;
1260 q += 4;
1261 }
1262 }
1263 #undef MULT
1264 break;
1265 }
1266 case GDK_X11_FORMAT_NONE:
1267 g_assert_not_reached ();
1268 break;
1269 }
1270 }
1271 }
1272
1273 static void
1274 draw_with_images (GdkDrawable *drawable,
1275 GdkGC *gc,
1276 GdkX11FormatType format_type,
1277 XRenderPictFormat *format,
1278 XRenderPictFormat *mask_format,
1279 guchar *src_rgb,
1280 gint src_rowstride,
1281 gint dest_x,
1282 gint dest_y,
1283 gint width,
1284 gint height)
1285 {
1286 GdkScreen *screen = GDK_DRAWABLE_IMPL_X11 (drawable)->screen;
1287 Display *xdisplay = GDK_SCREEN_XDISPLAY (screen);
1288 GdkImage *image;
1289 GdkPixmap *pix;
1290 GdkGC *pix_gc;
1291 Picture pict;
1292 Picture dest_pict;
1293 Picture mask = None;
1294 gint x0, y0;
1295
1296 pix = gdk_pixmap_new (gdk_screen_get_root_window (screen), width, height, 32);
1297
1298 pict = XRenderCreatePicture (xdisplay,
1299 GDK_PIXMAP_XID (pix),
1300 format, 0, NULL);
1301 if (mask_format)
1302 mask = XRenderCreatePicture (xdisplay,
1303 GDK_PIXMAP_XID (pix),
1304 mask_format, 0, NULL);
1305
1306 dest_pict = gdk_x11_drawable_get_picture (drawable);
1307
1308 pix_gc = _gdk_drawable_get_scratch_gc (pix, FALSE);
1309
1310 for (y0 = 0; y0 < height; y0 += GDK_SCRATCH_IMAGE_HEIGHT)
1311 {
1312 gint height1 = MIN (height - y0, GDK_SCRATCH_IMAGE_HEIGHT);
1313 for (x0 = 0; x0 < width; x0 += GDK_SCRATCH_IMAGE_WIDTH)
1314 {
1315 gint xs0, ys0;
1316
1317 gint width1 = MIN (width - x0, GDK_SCRATCH_IMAGE_WIDTH);
1318
1319 image = _gdk_image_get_scratch (screen, width1, height1, 32, &xs0, &ys0);
1320
1321 _gdk_x11_convert_to_format (src_rgb + y0 * src_rowstride + 4 * x0, src_rowstride,
1322 (guchar *)image->mem + ys0 * image->bpl + xs0 * image->bpp, image->bpl,
1323 format_type, image->byte_order,
1324 width1, height1);
1325
1326 gdk_draw_image (pix, pix_gc,
1327 image, xs0, ys0, x0, y0, width1, height1);
1328 }
1329 }
1330
1331 XRenderComposite (xdisplay, PictOpOver, pict, mask, dest_pict,
1332 0, 0, 0, 0, dest_x, dest_y, width, height);
1333
1334 XRenderFreePicture (xdisplay, pict);
1335 if (mask)
1336 XRenderFreePicture (xdisplay, mask);
1337
1338 g_object_unref (pix);
1339 }
1340
1341 typedef struct _ShmPixmapInfo ShmPixmapInfo;
1342
1343 struct _ShmPixmapInfo
1344 {
1345 Display *display;
1346 Pixmap pix;
1347 Picture pict;
1348 Picture mask;
1349 };
1350
1351 static void
1352 shm_pixmap_info_destroy (gpointer data)
1353 {
1354 ShmPixmapInfo *info = data;
1355
1356 if (info->pict != None)
1357 XRenderFreePicture (info->display, info->pict);
1358 if (info->mask != None)
1359 XRenderFreePicture (info->display, info->mask);
1360
1361 g_free (data);
1362 }
1363
1364
1365 #ifdef USE_SHM
1366 /* Returns FALSE if we can't get a shm pixmap */
1367 static gboolean
1368 get_shm_pixmap_for_image (Display *xdisplay,
1369 GdkImage *image,
1370 XRenderPictFormat *format,
1371 XRenderPictFormat *mask_format,
1372 Pixmap *pix,
1373 Picture *pict,
1374 Picture *mask)
1375 {
1376 ShmPixmapInfo *info;
1377
1378 if (image->type != GDK_IMAGE_SHARED)
1379 return FALSE;
1380
1381 info = g_object_get_data (G_OBJECT (image), "gdk-x11-shm-pixmap");
1382 if (!info)
1383 {
1384 *pix = _gdk_x11_image_get_shm_pixmap (image);
1385
1386 if (!*pix)
1387 return FALSE;
1388
1389 info = g_new (ShmPixmapInfo, 1);
1390 info->display = xdisplay;
1391 info->pix = *pix;
1392
1393 info->pict = XRenderCreatePicture (xdisplay, info->pix,
1394 format, 0, NULL);
1395 if (mask_format)
1396 info->mask = XRenderCreatePicture (xdisplay, info->pix,
1397 mask_format, 0, NULL);
1398 else
1399 info->mask = None;
1400
1401 g_object_set_data_full (G_OBJECT (image), "gdk-x11-shm-pixmap", info,
1402 shm_pixmap_info_destroy);
1403 }
1404
1405 *pix = info->pix;
1406 *pict = info->pict;
1407 *mask = info->mask;
1408
1409 return TRUE;
1410 }
1411
1412 /* Returns FALSE if drawing with ShmPixmaps is not possible */
1413 static gboolean
1414 draw_with_pixmaps (GdkDrawable *drawable,
1415 GdkGC *gc,
1416 GdkX11FormatType format_type,
1417 XRenderPictFormat *format,
1418 XRenderPictFormat *mask_format,
1419 guchar *src_rgb,
1420 gint src_rowstride,
1421 gint dest_x,
1422 gint dest_y,
1423 gint width,
1424 gint height)
1425 {
1426 Display *xdisplay = GDK_SCREEN_XDISPLAY (GDK_DRAWABLE_IMPL_X11 (drawable)->screen);
1427 GdkImage *image;
1428 Pixmap pix;
1429 Picture pict;
1430 Picture dest_pict;
1431 Picture mask = None;
1432 gint x0, y0;
1433
1434 dest_pict = gdk_x11_drawable_get_picture (drawable);
1435
1436 for (y0 = 0; y0 < height; y0 += GDK_SCRATCH_IMAGE_HEIGHT)
1437 {
1438 gint height1 = MIN (height - y0, GDK_SCRATCH_IMAGE_HEIGHT);
1439 for (x0 = 0; x0 < width; x0 += GDK_SCRATCH_IMAGE_WIDTH)
1440 {
1441 gint xs0, ys0;
1442
1443 gint width1 = MIN (width - x0, GDK_SCRATCH_IMAGE_WIDTH);
1444
1445 image = _gdk_image_get_scratch (GDK_DRAWABLE_IMPL_X11 (drawable)->screen,
1446 width1, height1, 32, &xs0, &ys0);
1447 if (!get_shm_pixmap_for_image (xdisplay, image, format, mask_format, &pix, &pict, &mask))
1448 return FALSE;
1449
1450 _gdk_x11_convert_to_format (src_rgb + y0 * src_rowstride + 4 * x0, src_rowstride,
1451 (guchar *)image->mem + ys0 * image->bpl + xs0 * image->bpp, image->bpl,
1452 format_type, image->byte_order,
1453 width1, height1);
1454
1455 XRenderComposite (xdisplay, PictOpOver, pict, mask, dest_pict,
1456 xs0, ys0, xs0, ys0, x0 + dest_x, y0 + dest_y,
1457 width1, height1);
1458 }
1459 }
1460
1461 return TRUE;
1462 }
1463 #endif
1464
1465 static void
1466 gdk_x11_draw_pixbuf (GdkDrawable *drawable,
1467 GdkGC *gc,
1468 GdkPixbuf *pixbuf,
1469 gint src_x,
1470 gint src_y,
1471 gint dest_x,
1472 gint dest_y,
1473 gint width,
1474 gint height,
1475 GdkRgbDither dither,
1476 gint x_dither,
1477 gint y_dither)
1478 {
1479 GdkX11FormatType format_type;
1480 XRenderPictFormat *format, *mask_format;
1481 gint rowstride;
1482 #ifdef USE_SHM
1483 gboolean use_pixmaps = TRUE;
1484 #endif /* USE_SHM */
1485
1486 format_type = select_format (gdk_drawable_get_display (drawable),
1487 &format, &mask_format);
1488
1489 if (format_type == GDK_X11_FORMAT_NONE ||
1490 !gdk_pixbuf_get_has_alpha (pixbuf) ||
1491 gdk_drawable_get_depth (drawable) == 1 ||
1492 (dither == GDK_RGB_DITHER_MAX && gdk_drawable_get_depth (drawable) != 24) ||
1493 gdk_x11_drawable_get_picture (drawable) == None)
1494 {
1495 GdkDrawable *wrapper = GDK_DRAWABLE_IMPL_X11 (drawable)->wrapper;
1496 GDK_DRAWABLE_CLASS (_gdk_drawable_impl_x11_parent_class)->draw_pixbuf (wrapper, gc, pixbuf,
1497 src_x, src_y, dest_x, dest_y,
1498 width, height,
1499 dither, x_dither, y_dither);
1500 return;
1501 }
1502
1503 gdk_x11_drawable_update_picture_clip (drawable, gc);
1504
1505 rowstride = gdk_pixbuf_get_rowstride (pixbuf);
1506
1507 #ifdef USE_SHM
1508 if (use_pixmaps)
1509 {
1510 if (!draw_with_pixmaps (drawable, gc,
1511 format_type, format, mask_format,
1512 gdk_pixbuf_get_pixels (pixbuf) + src_y * rowstride + src_x * 4,
1513 rowstride,
1514 dest_x, dest_y, width, height))
1515 use_pixmaps = FALSE;
1516 }
1517
1518 if (!use_pixmaps)
1519 #endif /* USE_SHM */
1520 draw_with_images (drawable, gc,
1521 format_type, format, mask_format,
1522 gdk_pixbuf_get_pixels (pixbuf) + src_y * rowstride + src_x * 4,
1523 rowstride,
1524 dest_x, dest_y, width, height);
1525 }
1526
1527 static void
1528 gdk_x11_cairo_surface_destroy (void *data)
1529 {
1530 GdkDrawableImplX11 *impl = data;
1531
1532 impl->cairo_surface = NULL;
1533 }
1534
1535 void
1536 _gdk_windowing_set_cairo_surface_size (cairo_surface_t *surface,
1537 int width,
1538 int height)
1539 {
1540 cairo_xlib_surface_set_size (surface, width, height);
1541 }
1542
1543 cairo_surface_t *
1544 _gdk_windowing_create_cairo_surface (GdkDrawable *drawable,
1545 int width,
1546 int height)
1547 {
1548 GdkDrawableImplX11 *impl = GDK_DRAWABLE_IMPL_X11 (drawable);
1549 GdkVisual *visual;
1550
1551 visual = gdk_drawable_get_visual (drawable);
1552 if (visual)
1553 return cairo_xlib_surface_create (GDK_SCREEN_XDISPLAY (impl->screen),
1554 impl->xid,
1555 GDK_VISUAL_XVISUAL (visual),
1556 width, height);
1557 else if (gdk_drawable_get_depth (drawable) == 1)
1558 return cairo_xlib_surface_create_for_bitmap (GDK_SCREEN_XDISPLAY (impl->screen),
1559 impl->xid,
1560 GDK_SCREEN_XSCREEN (impl->screen),
1561 width, height);
1562 else
1563 {
1564 g_warning ("Using Cairo rendering requires the drawable argument to\n"
1565 "have a specified colormap. All windows have a colormap,\n"
1566 "however, pixmaps only have colormap by default if they\n"
1567 "were created with a non-NULL window argument. Otherwise\n"
1568 "a colormap must be set on them with gdk_drawable_set_colormap");
1569 return NULL;
1570 }
1571
1572 }
1573
1574 static cairo_surface_t *
1575 gdk_x11_ref_cairo_surface (GdkDrawable *drawable)
1576 {
1577 GdkDrawableImplX11 *impl = GDK_DRAWABLE_IMPL_X11 (drawable);
1578
1579 if (GDK_IS_WINDOW_IMPL_X11 (drawable) &&
1580 GDK_WINDOW_DESTROYED (impl->wrapper))
1581 return NULL;
1582
1583 if (!impl->cairo_surface)
1584 {
1585 int width, height;
1586
1587 gdk_drawable_get_size (impl->wrapper, &width, &height);
1588
1589 impl->cairo_surface = _gdk_windowing_create_cairo_surface (drawable, width, height);
1590
1591 if (impl->cairo_surface)
1592 cairo_surface_set_user_data (impl->cairo_surface, &gdk_x11_cairo_key,
1593 drawable, gdk_x11_cairo_surface_destroy);
1594 }
1595 else
1596 cairo_surface_reference (impl->cairo_surface);
1597
1598 return impl->cairo_surface;
1599 }
1600
1601 #define __GDK_DRAWABLE_X11_C__
1602 #include "gdkaliasdef.c"