1 /* -*- Mode: c; c-basic-offset: 4; indent-tabs-mode: t; tab-width: 8; -*- */
2 /* cairo - a vector graphics library with display and print output
4 * Copyright © 2002 University of Southern California
5 * Copyright © 2005 Red Hat, Inc.
7 * This library is free software; you can redistribute it and/or
8 * modify it either under the terms of the GNU Lesser General Public
9 * License version 2.1 as published by the Free Software Foundation
10 * (the "LGPL") or, at your option, under the terms of the Mozilla
11 * Public License Version 1.1 (the "MPL"). If you do not alter this
12 * notice, a recipient may use your version of this file under either
13 * the MPL or the LGPL.
15 * You should have received a copy of the LGPL along with this library
16 * in the file COPYING-LGPL-2.1; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA
18 * You should have received a copy of the MPL along with this library
19 * in the file COPYING-MPL-1.1
21 * The contents of this file are subject to the Mozilla Public License
22 * Version 1.1 (the "License"); you may not use this file except in
23 * compliance with the License. You may obtain a copy of the License at
24 * http://www.mozilla.org/MPL/
26 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY
27 * OF ANY KIND, either express or implied. See the LGPL or the MPL for
28 * the specific language governing rights and limitations.
30 * The Original Code is the cairo graphics library.
32 * The Initial Developer of the Original Code is University of Southern
36 * Carl D. Worth <cworth@cworth.org>
37 * Behdad Esfahbod <behdad@behdad.org>
38 * Chris Wilson <chris@chris-wilson.co.uk>
39 * Karl Tomlinson <karlt+@karlt.net>, Mozilla Corporation
42 /* Heed well the words of Owen Taylor:
43 * "Any patch that works around a render bug, or claims to, without a
44 * specific reference to the bug filed in bugzilla.freedesktop.org will
45 * never pass approval."
50 #if !CAIRO_HAS_XLIB_XCB_FUNCTIONS
52 #include "cairo-xlib-private.h"
53 #include "cairo-xlib-surface-private.h"
55 #include "cairo-compositor-private.h"
56 #include "cairo-clip-private.h"
57 #include "cairo-default-context-private.h"
58 #include "cairo-error-private.h"
59 #include "cairo-image-surface-private.h"
60 #include "cairo-pattern-private.h"
61 #include "cairo-region-private.h"
62 #include "cairo-scaled-font-private.h"
63 #include "cairo-surface-snapshot-private.h"
64 #include "cairo-surface-subsurface-private.h"
66 #include <X11/Xutil.h> /* for XDestroyImage */
68 #define XLIB_COORD_MAX 32767
73 #define UNSUPPORTED(reason) \
75 "cairo-xlib: hit unsupported operation %s(), line %d: %s\n", \
76 __FUNCTION__, __LINE__, reason), \
77 CAIRO_INT_STATUS_UNSUPPORTED
79 #define UNSUPPORTED(reason) CAIRO_INT_STATUS_UNSUPPORTED
83 #include <X11/Xlibint.h>
84 static void CAIRO_PRINTF_FORMAT (2, 3)
85 _x_bread_crumb (Display *dpy,
91 unsigned int len, len_dwords;
95 len = vsnprintf (buf, sizeof (buf), fmt, ap);
103 GetEmptyReq (NoOperation, req);
105 len_dwords = len >> 2;
106 SetReqLen (req, len_dwords, len_dwords);
107 Data (dpy, buf, len);
112 #define X_DEBUG(x) _x_bread_crumb x
119 * @Title: XLib Surfaces
120 * @Short_Description: X Window System rendering using XLib
121 * @See_Also: #cairo_surface_t
123 * The XLib surface is used to render cairo graphics to X Window System
124 * windows and pixmaps using the XLib library.
126 * Note that the XLib surface automatically takes advantage of X render extension
127 * if it is available.
131 * CAIRO_HAS_XLIB_SURFACE:
133 * Defined if the Xlib surface backend is available.
134 * This macro can be used to conditionally compile backend-specific code.
138 * SECTION:cairo-xlib-xrender
139 * @Title: XLib/XRender Backend
140 * @Short_Description: X Window System rendering using XLib and the X Render extension
141 * @See_Also: #cairo_surface_t
143 * The XLib surface is used to render cairo graphics to X Window System
144 * windows and pixmaps using the XLib and Xrender libraries.
146 * Note that the XLib surface automatically takes advantage of X Render extension
147 * if it is available.
151 * CAIRO_HAS_XLIB_XRENDER_SURFACE:
153 * Defined if the XLib/XRender surface functions are available.
154 * This macro can be used to conditionally compile backend-specific code.
157 /* Xlib doesn't define a typedef, so define one ourselves */
158 typedef int (*cairo_xlib_error_func_t) (Display *display,
161 static cairo_surface_t *
162 _cairo_xlib_surface_create_internal (cairo_xlib_screen_t *screen,
165 XRenderPictFormat *xrender_format,
171 _cairo_surface_is_xlib (cairo_surface_t *surface);
174 * Instead of taking two round trips for each blending request,
175 * assume that if a particular drawable fails GetImage that it will
176 * fail for a "while"; use temporary pixmaps to avoid the errors
179 #define CAIRO_ASSUME_PIXMAP 20
181 static const XTransform identity = { {
182 { 1 << 16, 0x00000, 0x00000 },
183 { 0x00000, 1 << 16, 0x00000 },
184 { 0x00000, 0x00000, 1 << 16 },
188 _visual_for_xrender_format(Screen *screen,
189 XRenderPictFormat *xrender_format)
193 /* XXX Consider searching through the list of known cairo_visual_t for
194 * the reverse mapping.
197 for (d = 0; d < screen->ndepths; d++) {
198 Depth *d_info = &screen->depths[d];
200 if (d_info->depth != xrender_format->depth)
203 for (v = 0; v < d_info->nvisuals; v++) {
204 Visual *visual = &d_info->visuals[v];
206 switch (visual->class) {
208 if (xrender_format->type != PictTypeDirect)
213 /* Prefer TrueColor to DirectColor.
214 * (XRenderFindVisualFormat considers both TrueColor and DirectColor
215 * Visuals to match the same PictFormat.)
223 if (xrender_format->type != PictTypeIndexed)
228 if (xrender_format ==
229 XRenderFindVisualFormat (DisplayOfScreen(screen), visual))
237 static cairo_content_t
238 _xrender_format_to_content (XRenderPictFormat *xrender_format)
240 cairo_bool_t xrender_format_has_alpha;
241 cairo_bool_t xrender_format_has_color;
243 /* This only happens when using a non-Render server. Let's punt
244 * and say there's no alpha here. */
245 if (xrender_format == NULL)
246 return CAIRO_CONTENT_COLOR;
248 xrender_format_has_alpha = (xrender_format->direct.alphaMask != 0);
249 xrender_format_has_color = (xrender_format->direct.redMask != 0 ||
250 xrender_format->direct.greenMask != 0 ||
251 xrender_format->direct.blueMask != 0);
253 if (xrender_format_has_alpha)
254 if (xrender_format_has_color)
255 return CAIRO_CONTENT_COLOR_ALPHA;
257 return CAIRO_CONTENT_ALPHA;
259 return CAIRO_CONTENT_COLOR;
262 static cairo_surface_t *
263 _cairo_xlib_surface_create_similar (void *abstract_src,
264 cairo_content_t content,
268 cairo_xlib_surface_t *src = abstract_src;
269 XRenderPictFormat *xrender_format;
270 cairo_xlib_surface_t *surface;
271 cairo_xlib_display_t *display;
274 if (width > XLIB_COORD_MAX || height > XLIB_COORD_MAX)
277 if (width == 0 || height == 0)
280 if (_cairo_xlib_display_acquire (src->base.device, &display))
283 /* If we never found an XRenderFormat or if it isn't compatible
284 * with the content being requested, then we fallback to just
285 * constructing a cairo_format_t instead, (which will fairly
286 * arbitrarily pick a visual/depth for the similar surface.
288 xrender_format = NULL;
289 if (src->xrender_format &&
290 _xrender_format_to_content (src->xrender_format) == content)
292 xrender_format = src->xrender_format;
294 if (xrender_format == NULL) {
296 _cairo_xlib_display_get_xrender_format (display,
297 _cairo_format_from_content (content));
299 if (xrender_format) {
302 /* We've got a compatible XRenderFormat now, which means the
303 * similar surface will match the existing surface as closely in
304 * visual/depth etc. as possible. */
305 pix = XCreatePixmap (display->display, src->drawable,
306 width, height, xrender_format->depth);
308 if (xrender_format == src->xrender_format)
309 visual = src->visual;
311 visual = _visual_for_xrender_format(src->screen->screen,
314 surface = (cairo_xlib_surface_t *)
315 _cairo_xlib_surface_create_internal (src->screen, pix, visual,
318 xrender_format->depth);
322 Screen *screen = src->screen->screen;
325 /* No compatabile XRenderFormat, see if we can make an ordinary pixmap,
326 * so that we can still accelerate blits with XCopyArea(). */
327 if (content != CAIRO_CONTENT_COLOR) {
328 cairo_device_release (&display->base);
332 depth = DefaultDepthOfScreen (screen);
334 pix = XCreatePixmap (display->display, RootWindowOfScreen (screen),
335 width <= 0 ? 1 : width, height <= 0 ? 1 : height,
338 surface = (cairo_xlib_surface_t *)
339 _cairo_xlib_surface_create_internal (src->screen, pix,
340 DefaultVisualOfScreen (screen),
342 width, height, depth);
345 if (likely (surface->base.status == CAIRO_STATUS_SUCCESS))
346 surface->owns_pixmap = TRUE;
348 XFreePixmap (display->display, pix);
350 cairo_device_release (&display->base);
352 return &surface->base;
355 static cairo_status_t
356 _cairo_xlib_surface_finish (void *abstract_surface)
358 cairo_xlib_surface_t *surface = abstract_surface;
359 cairo_status_t status;
360 cairo_xlib_display_t *display;
362 X_DEBUG ((display->display, "finish (drawable=%x)", (unsigned int) surface->drawable));
364 cairo_list_del (&surface->link);
366 status = _cairo_xlib_display_acquire (surface->base.device, &display);
367 if (unlikely (status))
370 if (surface->embedded_source.picture)
371 XRenderFreePicture (display->display, surface->embedded_source.picture);
372 if (surface->picture)
373 XRenderFreePicture (display->display, surface->picture);
374 if (surface->owns_pixmap)
375 XFreePixmap (display->display, surface->drawable);
377 cairo_device_release (&display->base);
383 _cairo_xlib_surface_get_gc (cairo_xlib_display_t *display,
384 cairo_xlib_surface_t *surface,
387 *gc = _cairo_xlib_screen_get_gc (display,
391 if (unlikely (*gc == NULL))
392 return _cairo_error (CAIRO_STATUS_NO_MEMORY);
394 return CAIRO_STATUS_SUCCESS;
398 _noop_error_handler (Display *display,
401 return False; /* return value is ignored */
405 _swap_ximage_2bytes (XImage *ximage)
408 char *line = ximage->data;
410 for (j = ximage->height; j; j--) {
411 uint16_t *p = (uint16_t *) line;
412 for (i = ximage->width; i; i--) {
417 line += ximage->bytes_per_line;
422 _swap_ximage_3bytes (XImage *ximage)
425 char *line = ximage->data;
427 for (j = ximage->height; j; j--) {
428 uint8_t *p = (uint8_t *) line;
429 for (i = ximage->width; i; i--) {
437 line += ximage->bytes_per_line;
442 _swap_ximage_4bytes (XImage *ximage)
445 char *line = ximage->data;
447 for (j = ximage->height; j; j--) {
448 uint32_t *p = (uint32_t *) line;
449 for (i = ximage->width; i; i--) {
454 line += ximage->bytes_per_line;
459 _swap_ximage_nibbles (XImage *ximage)
462 char *line = ximage->data;
464 for (j = ximage->height; j; j--) {
465 uint8_t *p = (uint8_t *) line;
466 for (i = (ximage->width + 1) / 2; i; i--) {
467 *p = ((*p >> 4) & 0xf) | ((*p << 4) & ~0xf);
471 line += ximage->bytes_per_line;
476 _swap_ximage_bits (XImage *ximage)
479 char *line = ximage->data;
480 int unit = ximage->bitmap_unit;
481 int line_bytes = ((ximage->width + unit - 1) & ~(unit - 1)) / 8;
483 for (j = ximage->height; j; j--) {
486 for (i = line_bytes; i; i--) {
488 b = ((b << 1) & 0xaa) | ((b >> 1) & 0x55);
489 b = ((b << 2) & 0xcc) | ((b >> 2) & 0x33);
490 b = ((b << 4) & 0xf0) | ((b >> 4) & 0x0f);
496 line += ximage->bytes_per_line;
501 _swap_ximage_to_native (XImage *ximage)
504 int native_byte_order = _cairo_is_little_endian () ? LSBFirst : MSBFirst;
506 if (ximage->bits_per_pixel == 1 &&
507 ximage->bitmap_bit_order != native_byte_order)
509 _swap_ximage_bits (ximage);
510 if (ximage->bitmap_bit_order == ximage->byte_order)
514 if (ximage->byte_order == native_byte_order)
517 switch (ximage->bits_per_pixel) {
519 unit_bytes = ximage->bitmap_unit / 8;
522 _swap_ximage_nibbles (ximage);
531 unit_bytes = (ximage->bits_per_pixel + 7) / 8;
534 /* This could be hit on some rare but possible cases. */
538 switch (unit_bytes) {
542 _swap_ximage_2bytes (ximage);
545 _swap_ximage_3bytes (ximage);
548 _swap_ximage_4bytes (ximage);
556 /* Given a mask, (with a single sequence of contiguous 1 bits), return
557 * the number of 1 bits in 'width' and the number of 0 bits to its
558 * right in 'shift'. */
560 _characterize_field (uint32_t mask, int *width, int *shift)
562 *width = _cairo_popcount (mask);
563 /* The final '& 31' is to force a 0 mask to result in 0 shift. */
564 *shift = _cairo_popcount ((mask - 1) & ~mask) & 31;
567 /* Convert a field of 'width' bits to 'new_width' bits with correct
569 static inline uint32_t
570 _resize_field (uint32_t field, int width, int new_width)
575 if (width >= new_width) {
576 return field >> (width - new_width);
578 uint32_t result = field << (new_width - width);
580 while (width < new_width) {
581 result |= result >> width;
588 static inline uint32_t
589 _adjust_field (uint32_t field, int adjustment)
591 return MIN (255, MAX(0, (int)field + adjustment));
594 /* Given a shifted field value, (described by 'width' and 'shift),
595 * resize it 8-bits and return that value.
597 * Note that the original field value must not have any non-field bits
600 static inline uint32_t
601 _field_to_8 (uint32_t field, int width, int shift)
603 return _resize_field (field >> shift, width, 8);
606 static inline uint32_t
607 _field_to_8_undither (uint32_t field, int width, int shift,
608 int dither_adjustment)
610 return _adjust_field (_field_to_8 (field, width, shift), - dither_adjustment>>width);
613 /* Given an 8-bit value, convert it to a field of 'width', shift it up
614 * to 'shift, and return it. */
615 static inline uint32_t
616 _field_from_8 (uint32_t field, int width, int shift)
618 return _resize_field (field, 8, width) << shift;
621 static inline uint32_t
622 _field_from_8_dither (uint32_t field, int width, int shift,
623 int8_t dither_adjustment)
625 return _field_from_8 (_adjust_field (field, dither_adjustment>>width), width, shift);
628 static inline uint32_t
629 _pseudocolor_from_rgb888_dither (cairo_xlib_visual_info_t *visual_info,
630 uint32_t r, uint32_t g, uint32_t b,
631 int8_t dither_adjustment)
633 if (r == g && g == b) {
634 dither_adjustment /= RAMP_SIZE;
635 return visual_info->gray8_to_pseudocolor[_adjust_field (r, dither_adjustment)];
637 dither_adjustment = visual_info->dither8_to_cube[dither_adjustment+128];
638 return visual_info->cube_to_pseudocolor[visual_info->field8_to_cube[_adjust_field (r, dither_adjustment)]]
639 [visual_info->field8_to_cube[_adjust_field (g, dither_adjustment)]]
640 [visual_info->field8_to_cube[_adjust_field (b, dither_adjustment)]];
644 static inline uint32_t
645 _pseudocolor_to_rgb888 (cairo_xlib_visual_info_t *visual_info,
650 r = visual_info->colors[pixel].r;
651 g = visual_info->colors[pixel].g;
652 b = visual_info->colors[pixel].b;
658 /* should range from -128 to 127 */
660 static const int8_t dither_pattern[4][4] = {
661 {-8*X, +0*X, -6*X, +2*X},
662 {+4*X, -4*X, +6*X, -2*X},
663 {-5*X, +4*X, -7*X, +1*X},
664 {+7*X, -1*X, +5*X, -3*X}
668 static cairo_surface_t *
669 _get_image_surface (cairo_xlib_surface_t *surface,
670 const cairo_rectangle_int_t *extents)
672 cairo_int_status_t status;
673 cairo_image_surface_t *image = NULL;
675 pixman_format_code_t pixman_format;
676 cairo_format_masks_t xlib_masks;
677 cairo_xlib_display_t *display;
679 assert (extents->x >= 0);
680 assert (extents->y >= 0);
681 assert (extents->x + extents->width <= surface->width);
682 assert (extents->y + extents->height <= surface->height);
684 if (surface->base.serial == 0) {
685 xlib_masks.bpp = (surface->depth + 7) & ~7;
686 xlib_masks.alpha_mask = surface->a_mask;
687 xlib_masks.red_mask = surface->r_mask;
688 xlib_masks.green_mask = surface->g_mask;
689 xlib_masks.blue_mask = surface->b_mask;
690 _pixman_format_from_masks (&xlib_masks, &pixman_format);
691 return _cairo_image_surface_create_with_pixman_format (NULL,
698 status = _cairo_xlib_display_acquire (surface->base.device, &display);
700 return _cairo_surface_create_in_error (status);
702 /* XXX: This should try to use the XShm extension if available */
704 if (surface->use_pixmap == 0) {
705 cairo_xlib_error_func_t old_handler;
707 old_handler = XSetErrorHandler (_noop_error_handler);
709 ximage = XGetImage (display->display,
711 extents->x, extents->y,
712 extents->width, extents->height,
715 XSetErrorHandler (old_handler);
717 /* If we get an error, the surface must have been a window,
718 * so retry with the safe code path.
721 surface->use_pixmap = CAIRO_ASSUME_PIXMAP;
723 surface->use_pixmap--;
727 if (ximage == NULL) {
728 /* XGetImage from a window is dangerous because it can
729 * produce errors if the window is unmapped or partially
730 * outside the screen. We could check for errors and
731 * retry, but to keep things simple, we just create a
737 status = _cairo_xlib_surface_get_gc (display, surface, &gc);
738 if (unlikely (status))
741 pixmap = XCreatePixmap (display->display,
743 extents->width, extents->height,
746 XCopyArea (display->display, surface->drawable, pixmap, gc,
747 extents->x, extents->y,
748 extents->width, extents->height,
751 ximage = XGetImage (display->display,
754 extents->width, extents->height,
757 XFreePixmap (display->display, pixmap);
760 _cairo_xlib_surface_put_gc (display, surface, gc);
762 if (ximage == NULL) {
763 status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
768 _swap_ximage_to_native (ximage);
770 xlib_masks.bpp = ximage->bits_per_pixel;
771 xlib_masks.alpha_mask = surface->a_mask;
772 xlib_masks.red_mask = surface->r_mask;
773 xlib_masks.green_mask = surface->g_mask;
774 xlib_masks.blue_mask = surface->b_mask;
776 /* We can't use pixman to simply write to image if:
777 * (a) the pixels are not appropriately aligned,
778 * (b) pixman does not the pixel format, or
779 * (c) if the image is palettized and we need to convert.
781 if (ximage->bitmap_unit == 32 && ximage->bitmap_pad == 32 &&
782 _pixman_format_from_masks (&xlib_masks, &pixman_format) &&
783 (surface->visual == NULL || surface->visual->class == TrueColor))
785 image = (cairo_image_surface_t*)
786 _cairo_image_surface_create_with_pixman_format ((unsigned char *) ximage->data,
790 ximage->bytes_per_line);
791 status = image->base.status;
792 if (unlikely (status))
795 /* Let the surface take ownership of the data */
796 _cairo_image_surface_assume_ownership_of_data (image);
799 /* The visual we are dealing with is not supported by the
800 * standard pixman formats. So we must first convert the data
801 * to a supported format. */
803 cairo_format_t format;
806 uint32_t in_pixel, out_pixel;
807 unsigned int rowstride;
808 uint32_t a_mask=0, r_mask=0, g_mask=0, b_mask=0;
809 int a_width=0, r_width=0, g_width=0, b_width=0;
810 int a_shift=0, r_shift=0, g_shift=0, b_shift=0;
811 int x, y, x0, y0, x_off, y_off;
812 cairo_xlib_visual_info_t *visual_info = NULL;
814 if (surface->visual == NULL || surface->visual->class == TrueColor) {
815 cairo_bool_t has_alpha;
816 cairo_bool_t has_color;
818 has_alpha = surface->a_mask;
819 has_color = (surface->r_mask ||
825 format = CAIRO_FORMAT_ARGB32;
827 format = CAIRO_FORMAT_RGB24;
830 /* XXX: Using CAIRO_FORMAT_A8 here would be more
831 * efficient, but would require slightly different code in
832 * the image conversion to put the alpha channel values
833 * into the right place. */
834 format = CAIRO_FORMAT_ARGB32;
837 a_mask = surface->a_mask;
838 r_mask = surface->r_mask;
839 g_mask = surface->g_mask;
840 b_mask = surface->b_mask;
842 _characterize_field (a_mask, &a_width, &a_shift);
843 _characterize_field (r_mask, &r_width, &r_shift);
844 _characterize_field (g_mask, &g_width, &g_shift);
845 _characterize_field (b_mask, &b_width, &b_shift);
848 format = CAIRO_FORMAT_RGB24;
850 status = _cairo_xlib_screen_get_visual_info (display,
854 if (unlikely (status))
858 image = (cairo_image_surface_t *) cairo_image_surface_create
859 (format, ximage->width, ximage->height);
860 status = image->base.status;
861 if (unlikely (status))
864 data = cairo_image_surface_get_data (&image->base);
865 rowstride = cairo_image_surface_get_stride (&image->base) >> 2;
866 row = (uint32_t *) data;
867 x0 = extents->x + surface->base.device_transform.x0;
868 y0 = extents->y + surface->base.device_transform.y0;
869 for (y = 0, y_off = y0 % ARRAY_LENGTH (dither_pattern);
871 y++, y_off = (y_off+1) % ARRAY_LENGTH (dither_pattern)) {
872 const int8_t *dither_row = dither_pattern[y_off];
873 for (x = 0, x_off = x0 % ARRAY_LENGTH (dither_pattern[0]);
875 x++, x_off = (x_off+1) % ARRAY_LENGTH (dither_pattern[0])) {
876 int dither_adjustment = dither_row[x_off];
878 in_pixel = XGetPixel (ximage, x, y);
879 if (visual_info == NULL) {
881 _field_to_8 (in_pixel & a_mask, a_width, a_shift) << 24 |
882 _field_to_8_undither (in_pixel & r_mask, r_width, r_shift, dither_adjustment) << 16 |
883 _field_to_8_undither (in_pixel & g_mask, g_width, g_shift, dither_adjustment) << 8 |
884 _field_to_8_undither (in_pixel & b_mask, b_width, b_shift, dither_adjustment));
886 /* Undithering pseudocolor does not look better */
887 out_pixel = _pseudocolor_to_rgb888 (visual_info, in_pixel);
893 cairo_surface_mark_dirty (&image->base);
898 XDestroyImage (ximage);
900 cairo_device_release (&display->base);
902 if (unlikely (status)) {
903 cairo_surface_destroy (&image->base);
904 return _cairo_surface_create_in_error (status);
911 _cairo_xlib_surface_set_precision (cairo_xlib_surface_t *surface,
912 cairo_antialias_t antialias)
914 cairo_xlib_display_t *display = surface->display;
917 if (display->force_precision != -1)
918 precision = display->force_precision;
919 else switch (antialias) {
921 case CAIRO_ANTIALIAS_DEFAULT:
922 case CAIRO_ANTIALIAS_GRAY:
923 case CAIRO_ANTIALIAS_NONE:
924 case CAIRO_ANTIALIAS_FAST:
925 case CAIRO_ANTIALIAS_GOOD:
926 precision = PolyModeImprecise;
928 case CAIRO_ANTIALIAS_BEST:
929 case CAIRO_ANTIALIAS_SUBPIXEL:
930 precision = PolyModePrecise;
934 if (surface->precision != precision) {
935 XRenderPictureAttributes pa;
937 pa.poly_mode = precision;
938 XRenderChangePicture (display->display, surface->picture,
941 surface->precision = precision;
946 _cairo_xlib_surface_ensure_picture (cairo_xlib_surface_t *surface)
948 cairo_xlib_display_t *display = surface->display;
949 XRenderPictureAttributes pa;
952 if (surface->picture)
955 if (display->force_precision != -1)
956 pa.poly_mode = display->force_precision;
958 pa.poly_mode = PolyModeImprecise;
962 surface->precision = pa.poly_mode;
963 surface->picture = XRenderCreatePicture (display->display,
965 surface->xrender_format,
970 _cairo_xlib_surface_draw_image (cairo_xlib_surface_t *surface,
971 cairo_image_surface_t *image,
979 cairo_xlib_display_t *display;
981 cairo_format_masks_t image_masks;
982 int native_byte_order = _cairo_is_little_endian () ? LSBFirst : MSBFirst;
983 pixman_image_t *pixman_image = NULL;
984 cairo_status_t status;
985 cairo_bool_t own_data;
986 cairo_bool_t is_rgb_image;
989 ximage.width = image->width;
990 ximage.height = image->height;
991 ximage.format = ZPixmap;
992 ximage.byte_order = native_byte_order;
993 ximage.bitmap_unit = 32; /* always for libpixman */
994 ximage.bitmap_bit_order = native_byte_order;
995 ximage.bitmap_pad = 32; /* always for libpixman */
996 ximage.depth = surface->depth;
997 ximage.red_mask = surface->r_mask;
998 ximage.green_mask = surface->g_mask;
999 ximage.blue_mask = surface->b_mask;
1002 status = _cairo_xlib_display_acquire (surface->base.device, &display);
1003 if (unlikely (status))
1006 is_rgb_image = _pixman_format_to_masks (image->pixman_format, &image_masks);
1009 (image_masks.alpha_mask == surface->a_mask || surface->a_mask == 0) &&
1010 (image_masks.red_mask == surface->r_mask || surface->r_mask == 0) &&
1011 (image_masks.green_mask == surface->g_mask || surface->g_mask == 0) &&
1012 (image_masks.blue_mask == surface->b_mask || surface->b_mask == 0))
1016 ximage.bits_per_pixel = image_masks.bpp;
1017 ximage.bytes_per_line = image->stride;
1018 ximage.data = (char *)image->data;
1021 ret = XInitImage (&ximage);
1024 else if (!is_rgb_image ||
1025 (surface->visual == NULL || surface->visual->class == TrueColor))
1027 pixman_format_code_t intermediate_format;
1030 image_masks.alpha_mask = surface->a_mask;
1031 image_masks.red_mask = surface->r_mask;
1032 image_masks.green_mask = surface->g_mask;
1033 image_masks.blue_mask = surface->b_mask;
1034 image_masks.bpp = surface->depth;
1035 ret = _pixman_format_from_masks (&image_masks, &intermediate_format);
1040 pixman_image = pixman_image_create_bits (intermediate_format,
1045 if (pixman_image == NULL) {
1046 status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
1050 pixman_image_composite32 (PIXMAN_OP_SRC,
1051 image->pixman_image,
1057 image->width, image->height);
1059 ximage.bits_per_pixel = image_masks.bpp;
1060 ximage.data = (char *) pixman_image_get_data (pixman_image);
1061 ximage.bytes_per_line = pixman_image_get_stride (pixman_image);
1063 ret = XInitImage (&ximage);
1068 unsigned int stride, rowstride;
1069 int x, y, x0, y0, x_off, y_off;
1070 uint32_t in_pixel, out_pixel, *row;
1071 int i_a_width=0, i_r_width=0, i_g_width=0, i_b_width=0;
1072 int i_a_shift=0, i_r_shift=0, i_g_shift=0, i_b_shift=0;
1073 int o_a_width=0, o_r_width=0, o_g_width=0, o_b_width=0;
1074 int o_a_shift=0, o_r_shift=0, o_g_shift=0, o_b_shift=0;
1075 cairo_xlib_visual_info_t *visual_info = NULL;
1076 cairo_bool_t true_color;
1079 if (surface->depth > 16)
1080 ximage.bits_per_pixel = 32;
1081 else if (surface->depth > 8)
1082 ximage.bits_per_pixel = 16;
1083 else if (surface->depth > 1)
1084 ximage.bits_per_pixel = 8;
1086 ximage.bits_per_pixel = 1;
1087 stride = CAIRO_STRIDE_FOR_WIDTH_BPP (ximage.width,
1088 ximage.bits_per_pixel);
1089 ximage.bytes_per_line = stride;
1090 ximage.data = _cairo_malloc_ab (stride, ximage.height);
1091 if (unlikely (ximage.data == NULL)) {
1093 status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
1099 ret = XInitImage (&ximage);
1102 _characterize_field (image_masks.alpha_mask, &i_a_width, &i_a_shift);
1103 _characterize_field (image_masks.red_mask , &i_r_width, &i_r_shift);
1104 _characterize_field (image_masks.green_mask, &i_g_width, &i_g_shift);
1105 _characterize_field (image_masks.blue_mask , &i_b_width, &i_b_shift);
1107 true_color = surface->visual == NULL ||
1108 surface->visual->class == TrueColor;
1110 _characterize_field (surface->a_mask, &o_a_width, &o_a_shift);
1111 _characterize_field (surface->r_mask, &o_r_width, &o_r_shift);
1112 _characterize_field (surface->g_mask, &o_g_width, &o_g_shift);
1113 _characterize_field (surface->b_mask, &o_b_width, &o_b_shift);
1115 status = _cairo_xlib_screen_get_visual_info (display,
1119 if (unlikely (status))
1123 rowstride = image->stride >> 2;
1124 row = (uint32_t *) image->data;
1125 x0 = dst_x + surface->base.device_transform.x0;
1126 y0 = dst_y + surface->base.device_transform.y0;
1127 for (y = 0, y_off = y0 % ARRAY_LENGTH (dither_pattern);
1129 y++, y_off = (y_off+1) % ARRAY_LENGTH (dither_pattern))
1131 const int8_t *dither_row = dither_pattern[y_off];
1133 for (x = 0, x_off = x0 % ARRAY_LENGTH (dither_pattern[0]);
1135 x++, x_off = (x_off+1) % ARRAY_LENGTH (dither_pattern[0]))
1137 int dither_adjustment = dither_row[x_off];
1140 if (image_masks.bpp == 1)
1141 in_pixel = !! (((uint8_t*)row)[x/8] & (1 << (x & 7)));
1142 else if (image_masks.bpp <= 8)
1143 in_pixel = ((uint8_t*)row)[x];
1144 else if (image_masks.bpp <= 16)
1145 in_pixel = ((uint16_t*)row)[x];
1146 else if (image_masks.bpp <= 24)
1147 #ifdef WORDS_BIGENDIAN
1148 in_pixel = ((uint8_t*)row)[3 * x] << 16 |
1149 ((uint8_t*)row)[3 * x + 1] << 8 |
1150 ((uint8_t*)row)[3 * x + 2];
1152 in_pixel = ((uint8_t*)row)[3 * x] |
1153 ((uint8_t*)row)[3 * x + 1] << 8 |
1154 ((uint8_t*)row)[3 * x + 2] << 16;
1159 /* If the incoming image has no alpha channel, then the input
1160 * is opaque and the output should have the maximum alpha value.
1161 * For all other channels, their absence implies 0.
1163 if (image_masks.alpha_mask == 0x0)
1166 a = _field_to_8 (in_pixel & image_masks.alpha_mask, i_a_width, i_a_shift);
1167 r = _field_to_8 (in_pixel & image_masks.red_mask , i_r_width, i_r_shift);
1168 g = _field_to_8 (in_pixel & image_masks.green_mask, i_g_width, i_g_shift);
1169 b = _field_to_8 (in_pixel & image_masks.blue_mask , i_b_width, i_b_shift);
1172 out_pixel = _field_from_8 (a, o_a_width, o_a_shift) |
1173 _field_from_8_dither (r, o_r_width, o_r_shift, dither_adjustment) |
1174 _field_from_8_dither (g, o_g_width, o_g_shift, dither_adjustment) |
1175 _field_from_8_dither (b, o_b_width, o_b_shift, dither_adjustment);
1177 out_pixel = _pseudocolor_from_rgb888_dither (visual_info, r, g, b, dither_adjustment);
1180 XPutPixel (&ximage, x, y, out_pixel);
1187 status = _cairo_xlib_surface_get_gc (display, surface, &gc);
1188 if (unlikely (status))
1191 XPutImage (display->display, surface->drawable, gc,
1192 &ximage, src_x, src_y, dst_x, dst_y,
1195 _cairo_xlib_surface_put_gc (display, surface, gc);
1199 cairo_device_release (&display->base);
1204 pixman_image_unref (pixman_image);
1206 return CAIRO_STATUS_SUCCESS;
1209 static cairo_status_t
1210 _cairo_xlib_surface_acquire_source_image (void *abstract_surface,
1211 cairo_image_surface_t **image_out,
1214 cairo_xlib_surface_t *surface = abstract_surface;
1215 cairo_rectangle_int_t extents;
1217 extents.x = extents.y = 0;
1218 extents.width = surface->width;
1219 extents.height = surface->height;
1221 *image_extra = NULL;
1222 *image_out = (cairo_image_surface_t*)_get_image_surface (surface, &extents);
1223 return (*image_out)->base.status;
1226 static cairo_surface_t *
1227 _cairo_xlib_surface_snapshot (void *abstract_surface)
1229 cairo_xlib_surface_t *surface = abstract_surface;
1230 cairo_rectangle_int_t extents;
1232 extents.x = extents.y = 0;
1233 extents.width = surface->width;
1234 extents.height = surface->height;
1236 /* XXX notice the duplication with acquire source */
1237 return _get_image_surface (surface, &extents);
1241 _cairo_xlib_surface_release_source_image (void *abstract_surface,
1242 cairo_image_surface_t *image,
1245 cairo_surface_destroy (&image->base);
1248 static cairo_surface_t *
1249 _cairo_xlib_surface_map_to_image (void *abstract_surface,
1250 const cairo_rectangle_int_t *extents)
1252 cairo_surface_t *image;
1254 image = _get_image_surface (abstract_surface, extents);
1255 cairo_surface_set_device_offset (image, -extents->y, -extents->y);
1260 static cairo_int_status_t
1261 _cairo_xlib_surface_unmap_image (void *abstract_surface,
1262 cairo_image_surface_t *image)
1264 return _cairo_xlib_surface_draw_image (abstract_surface, image,
1266 image->width, image->height,
1267 image->base.device_transform_inverse.x0,
1268 image->base.device_transform_inverse.y0);
1272 _cairo_xlib_surface_get_extents (void *abstract_surface,
1273 cairo_rectangle_int_t *rectangle)
1275 cairo_xlib_surface_t *surface = abstract_surface;
1280 rectangle->width = surface->width;
1281 rectangle->height = surface->height;
1287 _cairo_xlib_surface_get_font_options (void *abstract_surface,
1288 cairo_font_options_t *options)
1290 cairo_xlib_surface_t *surface = abstract_surface;
1292 *options = *_cairo_xlib_screen_get_font_options (surface->screen);
1296 static cairo_int_status_t
1297 _cairo_xlib_surface_paint (void *_surface,
1298 cairo_operator_t op,
1299 const cairo_pattern_t *source,
1300 const cairo_clip_t *clip)
1302 cairo_xlib_surface_t *surface = _surface;
1303 return _cairo_compositor_paint (surface->compositor,
1304 &surface->base, op, source,
1308 static cairo_int_status_t
1309 _cairo_xlib_surface_mask (void *_surface,
1310 cairo_operator_t op,
1311 const cairo_pattern_t *source,
1312 const cairo_pattern_t *mask,
1313 const cairo_clip_t *clip)
1315 cairo_xlib_surface_t *surface = _surface;
1316 return _cairo_compositor_mask (surface->compositor,
1317 &surface->base, op, source, mask,
1321 static cairo_int_status_t
1322 _cairo_xlib_surface_stroke (void *_surface,
1323 cairo_operator_t op,
1324 const cairo_pattern_t *source,
1325 const cairo_path_fixed_t *path,
1326 const cairo_stroke_style_t *style,
1327 const cairo_matrix_t *ctm,
1328 const cairo_matrix_t *ctm_inverse,
1330 cairo_antialias_t antialias,
1331 const cairo_clip_t *clip)
1333 cairo_xlib_surface_t *surface = _surface;
1334 return _cairo_compositor_stroke (surface->compositor,
1335 &surface->base, op, source,
1336 path, style, ctm, ctm_inverse,
1337 tolerance, antialias,
1341 static cairo_int_status_t
1342 _cairo_xlib_surface_fill (void *_surface,
1343 cairo_operator_t op,
1344 const cairo_pattern_t *source,
1345 const cairo_path_fixed_t *path,
1346 cairo_fill_rule_t fill_rule,
1348 cairo_antialias_t antialias,
1349 const cairo_clip_t *clip)
1351 cairo_xlib_surface_t *surface = _surface;
1352 return _cairo_compositor_fill (surface->compositor,
1353 &surface->base, op, source,
1354 path, fill_rule, tolerance, antialias,
1358 static cairo_int_status_t
1359 _cairo_xlib_surface_glyphs (void *_surface,
1360 cairo_operator_t op,
1361 const cairo_pattern_t *source,
1362 cairo_glyph_t *glyphs,
1364 cairo_scaled_font_t *scaled_font,
1365 const cairo_clip_t *clip)
1367 cairo_xlib_surface_t *surface = _surface;
1368 return _cairo_compositor_glyphs (surface->compositor,
1369 &surface->base, op, source,
1370 glyphs, num_glyphs, scaled_font,
1374 static const cairo_surface_backend_t cairo_xlib_surface_backend = {
1375 CAIRO_SURFACE_TYPE_XLIB,
1376 _cairo_xlib_surface_finish,
1378 _cairo_default_context_create,
1380 _cairo_xlib_surface_create_similar,
1381 NULL, //_cairo_xlib_surface_create_similar_image, /* XXX shm */
1382 _cairo_xlib_surface_map_to_image,
1383 _cairo_xlib_surface_unmap_image,
1385 _cairo_xlib_surface_acquire_source_image,
1386 _cairo_xlib_surface_release_source_image,
1387 _cairo_xlib_surface_snapshot,
1389 NULL, /* copy_page */
1390 NULL, /* show_page */
1392 _cairo_xlib_surface_get_extents,
1393 _cairo_xlib_surface_get_font_options,
1396 NULL, /* mark_dirty_rectangle */
1398 _cairo_xlib_surface_paint,
1399 _cairo_xlib_surface_mask,
1400 _cairo_xlib_surface_stroke,
1401 _cairo_xlib_surface_fill,
1402 NULL, /* fill-stroke */
1403 _cairo_xlib_surface_glyphs,
1407 * _cairo_surface_is_xlib:
1408 * @surface: a #cairo_surface_t
1410 * Checks if a surface is a #cairo_xlib_surface_t
1412 * Return value: True if the surface is an xlib surface
1415 _cairo_surface_is_xlib (cairo_surface_t *surface)
1417 return surface->backend == &cairo_xlib_surface_backend;
1420 static cairo_surface_t *
1421 _cairo_xlib_surface_create_internal (cairo_xlib_screen_t *screen,
1424 XRenderPictFormat *xrender_format,
1429 cairo_xlib_surface_t *surface;
1430 cairo_xlib_display_t *display;
1431 cairo_status_t status;
1434 if (xrender_format) {
1435 depth = xrender_format->depth;
1437 /* XXX find matching visual for core/dithering fallbacks? */
1438 } else if (visual) {
1439 Screen *scr = screen->screen;
1441 if (visual == DefaultVisualOfScreen (scr)) {
1442 depth = DefaultDepthOfScreen (scr);
1446 /* This is ugly, but we have to walk over all visuals
1447 * for the display to find the correct depth.
1450 for (j = 0; j < scr->ndepths; j++) {
1451 Depth *d = &scr->depths[j];
1452 for (k = 0; k < d->nvisuals; k++) {
1453 if (&d->visuals[k] == visual) {
1463 return _cairo_surface_create_in_error (_cairo_error (CAIRO_STATUS_INVALID_VISUAL));
1469 surface = malloc (sizeof (cairo_xlib_surface_t));
1470 if (unlikely (surface == NULL))
1471 return _cairo_surface_create_in_error (_cairo_error (CAIRO_STATUS_NO_MEMORY));
1473 status = _cairo_xlib_display_acquire (screen->device, &display);
1474 if (unlikely (status)) {
1476 return _cairo_surface_create_in_error (_cairo_error (status));
1479 surface->display = display;
1480 if (CAIRO_RENDER_HAS_CREATE_PICTURE (display)) {
1481 if (!xrender_format) {
1483 xrender_format = XRenderFindVisualFormat (display->display, visual);
1484 } else if (depth == 1) {
1486 _cairo_xlib_display_get_xrender_format (display,
1492 cairo_device_release (&display->base);
1494 _cairo_surface_init (&surface->base,
1495 &cairo_xlib_surface_backend,
1497 _xrender_format_to_content (xrender_format));
1499 surface->screen = screen;
1500 surface->compositor = display->compositor;
1502 surface->drawable = drawable;
1503 surface->owns_pixmap = FALSE;
1504 surface->use_pixmap = 0;
1505 surface->width = width;
1506 surface->height = height;
1508 surface->picture = None;
1509 surface->precision = PolyModePrecise;
1511 surface->embedded_source.picture = None;
1513 surface->visual = visual;
1514 surface->xrender_format = xrender_format;
1515 surface->depth = depth;
1518 * Compute the pixel format masks from either a XrenderFormat or
1519 * else from a visual; failing that we assume the drawable is an
1520 * alpha-only pixmap as it could only have been created that way
1521 * through the cairo_xlib_surface_create_for_bitmap function.
1523 if (xrender_format) {
1524 surface->a_mask = (unsigned long)
1525 surface->xrender_format->direct.alphaMask
1526 << surface->xrender_format->direct.alpha;
1527 surface->r_mask = (unsigned long)
1528 surface->xrender_format->direct.redMask
1529 << surface->xrender_format->direct.red;
1530 surface->g_mask = (unsigned long)
1531 surface->xrender_format->direct.greenMask
1532 << surface->xrender_format->direct.green;
1533 surface->b_mask = (unsigned long)
1534 surface->xrender_format->direct.blueMask
1535 << surface->xrender_format->direct.blue;
1536 } else if (visual) {
1537 surface->a_mask = 0;
1538 surface->r_mask = visual->red_mask;
1539 surface->g_mask = visual->green_mask;
1540 surface->b_mask = visual->blue_mask;
1543 surface->a_mask = (1 << depth) - 1;
1545 surface->a_mask = 0xffffffff;
1546 surface->r_mask = 0;
1547 surface->g_mask = 0;
1548 surface->b_mask = 0;
1551 cairo_list_add (&surface->link, &screen->surfaces);
1553 return &surface->base;
1557 _cairo_xlib_screen_from_visual (Display *dpy, Visual *visual)
1561 for (s = 0; s < ScreenCount (dpy); s++) {
1564 screen = ScreenOfDisplay (dpy, s);
1565 if (visual == DefaultVisualOfScreen (screen))
1568 for (d = 0; d < screen->ndepths; d++) {
1571 depth = &screen->depths[d];
1572 for (v = 0; v < depth->nvisuals; v++)
1573 if (visual == &depth->visuals[v])
1581 static cairo_bool_t valid_size (int width, int height)
1583 return width > 0 && width <= XLIB_COORD_MAX && height > 0 && height <= XLIB_COORD_MAX;
1587 * cairo_xlib_surface_create:
1588 * @dpy: an X Display
1589 * @drawable: an X Drawable, (a Pixmap or a Window)
1590 * @visual: the visual to use for drawing to @drawable. The depth
1591 * of the visual must match the depth of the drawable.
1592 * Currently, only TrueColor visuals are fully supported.
1593 * @width: the current width of @drawable.
1594 * @height: the current height of @drawable.
1596 * Creates an Xlib surface that draws to the given drawable.
1597 * The way that colors are represented in the drawable is specified
1598 * by the provided visual.
1600 * Note: If @drawable is a Window, then the function
1601 * cairo_xlib_surface_set_size() must be called whenever the size of the
1604 * When @drawable is a Window containing child windows then drawing to
1605 * the created surface will be clipped by those child windows. When
1606 * the created surface is used as a source, the contents of the
1607 * children will be included.
1609 * Return value: the newly created surface
1612 cairo_xlib_surface_create (Display *dpy,
1619 cairo_xlib_screen_t *screen;
1620 cairo_status_t status;
1622 if (! valid_size (width, height)) {
1623 /* you're lying, and you know it! */
1624 return _cairo_surface_create_in_error (_cairo_error (CAIRO_STATUS_INVALID_SIZE));
1627 scr = _cairo_xlib_screen_from_visual (dpy, visual);
1629 return _cairo_surface_create_in_error (_cairo_error (CAIRO_STATUS_INVALID_VISUAL));
1631 status = _cairo_xlib_screen_get (dpy, scr, &screen);
1632 if (unlikely (status))
1633 return _cairo_surface_create_in_error (status);
1635 X_DEBUG ((dpy, "create (drawable=%x)", (unsigned int) drawable));
1637 return _cairo_xlib_surface_create_internal (screen, drawable,
1643 * cairo_xlib_surface_create_for_bitmap:
1644 * @dpy: an X Display
1645 * @bitmap: an X Drawable, (a depth-1 Pixmap)
1646 * @screen: the X Screen associated with @bitmap
1647 * @width: the current width of @bitmap.
1648 * @height: the current height of @bitmap.
1650 * Creates an Xlib surface that draws to the given bitmap.
1651 * This will be drawn to as a %CAIRO_FORMAT_A1 object.
1653 * Return value: the newly created surface
1656 cairo_xlib_surface_create_for_bitmap (Display *dpy,
1662 cairo_xlib_screen_t *screen;
1663 cairo_status_t status;
1665 if (! valid_size (width, height))
1666 return _cairo_surface_create_in_error (_cairo_error (CAIRO_STATUS_INVALID_SIZE));
1668 status = _cairo_xlib_screen_get (dpy, scr, &screen);
1669 if (unlikely (status))
1670 return _cairo_surface_create_in_error (status);
1672 X_DEBUG ((dpy, "create_for_bitmap (drawable=%x)", (unsigned int) bitmap));
1674 return _cairo_xlib_surface_create_internal (screen, bitmap,
1679 #if CAIRO_HAS_XLIB_XRENDER_SURFACE
1681 * cairo_xlib_surface_create_with_xrender_format:
1682 * @dpy: an X Display
1683 * @drawable: an X Drawable, (a Pixmap or a Window)
1684 * @screen: the X Screen associated with @drawable
1685 * @format: the picture format to use for drawing to @drawable. The depth
1686 * of @format must match the depth of the drawable.
1687 * @width: the current width of @drawable.
1688 * @height: the current height of @drawable.
1690 * Creates an Xlib surface that draws to the given drawable.
1691 * The way that colors are represented in the drawable is specified
1692 * by the provided picture format.
1694 * Note: If @drawable is a Window, then the function
1695 * cairo_xlib_surface_set_size() must be called whenever the size of the
1698 * Return value: the newly created surface
1701 cairo_xlib_surface_create_with_xrender_format (Display *dpy,
1704 XRenderPictFormat *format,
1708 cairo_xlib_screen_t *screen;
1709 cairo_status_t status;
1711 if (! valid_size (width, height))
1712 return _cairo_surface_create_in_error (_cairo_error (CAIRO_STATUS_INVALID_SIZE));
1714 status = _cairo_xlib_screen_get (dpy, scr, &screen);
1715 if (unlikely (status))
1716 return _cairo_surface_create_in_error (status);
1718 X_DEBUG ((dpy, "create_with_xrender_format (drawable=%x)", (unsigned int) drawable));
1720 return _cairo_xlib_surface_create_internal (screen, drawable,
1721 _visual_for_xrender_format (scr, format),
1722 format, width, height, 0);
1726 * cairo_xlib_surface_get_xrender_format:
1727 * @surface: an xlib surface
1729 * Gets the X Render picture format that @surface uses for rendering with the
1730 * X Render extension. If the surface was created by
1731 * cairo_xlib_surface_create_with_xrender_format() originally, the return
1732 * value is the format passed to that constructor.
1734 * Return value: the XRenderPictFormat* associated with @surface,
1735 * or %NULL if the surface is not an xlib surface
1736 * or if the X Render extension is not available.
1741 cairo_xlib_surface_get_xrender_format (cairo_surface_t *surface)
1743 cairo_xlib_surface_t *xlib_surface = (cairo_xlib_surface_t *) surface;
1745 /* Throw an error for a non-xlib surface */
1746 if (! _cairo_surface_is_xlib (surface)) {
1747 _cairo_error_throw (CAIRO_STATUS_SURFACE_TYPE_MISMATCH);
1751 return xlib_surface->xrender_format;
1756 * cairo_xlib_surface_set_size:
1757 * @surface: a #cairo_surface_t for the XLib backend
1758 * @width: the new width of the surface
1759 * @height: the new height of the surface
1761 * Informs cairo of the new size of the X Drawable underlying the
1762 * surface. For a surface created for a Window (rather than a Pixmap),
1763 * this function must be called each time the size of the window
1764 * changes. (For a subwindow, you are normally resizing the window
1765 * yourself, but for a toplevel window, it is necessary to listen for
1766 * ConfigureNotify events.)
1768 * A Pixmap can never change size, so it is never necessary to call
1769 * this function on a surface created for a Pixmap.
1772 cairo_xlib_surface_set_size (cairo_surface_t *abstract_surface,
1776 cairo_xlib_surface_t *surface = (cairo_xlib_surface_t *) abstract_surface;
1778 if (unlikely (abstract_surface->status))
1780 if (unlikely (abstract_surface->finished)) {
1781 _cairo_surface_set_error (abstract_surface,
1782 _cairo_error (CAIRO_STATUS_SURFACE_FINISHED));
1786 if (! _cairo_surface_is_xlib (abstract_surface)) {
1787 _cairo_surface_set_error (abstract_surface,
1788 _cairo_error (CAIRO_STATUS_SURFACE_TYPE_MISMATCH));
1792 if (! valid_size (width, height)) {
1793 _cairo_surface_set_error (abstract_surface,
1794 _cairo_error (CAIRO_STATUS_INVALID_SIZE));
1798 surface->width = width;
1799 surface->height = height;
1802 * cairo_xlib_surface_set_drawable:
1803 * @surface: a #cairo_surface_t for the XLib backend
1804 * @drawable: the new drawable for the surface
1805 * @width: the width of the new drawable
1806 * @height: the height of the new drawable
1808 * Informs cairo of a new X Drawable underlying the
1809 * surface. The drawable must match the display, screen
1810 * and format of the existing drawable or the application
1811 * will get X protocol errors and will probably terminate.
1812 * No checks are done by this function to ensure this
1816 cairo_xlib_surface_set_drawable (cairo_surface_t *abstract_surface,
1821 cairo_xlib_surface_t *surface = (cairo_xlib_surface_t *)abstract_surface;
1822 cairo_status_t status;
1824 if (unlikely (abstract_surface->status))
1826 if (unlikely (abstract_surface->finished)) {
1827 status = _cairo_surface_set_error (abstract_surface,
1828 _cairo_error (CAIRO_STATUS_SURFACE_FINISHED));
1832 if (! _cairo_surface_is_xlib (abstract_surface)) {
1833 status = _cairo_surface_set_error (abstract_surface,
1834 _cairo_error (CAIRO_STATUS_SURFACE_TYPE_MISMATCH));
1838 if (! valid_size (width, height)) {
1839 status = _cairo_surface_set_error (abstract_surface,
1840 _cairo_error (CAIRO_STATUS_INVALID_SIZE));
1844 /* XXX: and what about this case? */
1845 if (surface->owns_pixmap)
1848 if (surface->drawable != drawable) {
1849 cairo_xlib_display_t *display;
1851 status = _cairo_xlib_display_acquire (surface->base.device, &display);
1852 if (unlikely (status))
1855 X_DEBUG ((display->display, "set_drawable (drawable=%x)", (unsigned int) drawable));
1857 if (surface->picture != None) {
1858 XRenderFreePicture (display->display, surface->picture);
1859 if (unlikely (status)) {
1860 status = _cairo_surface_set_error (&surface->base, status);
1864 surface->picture = None;
1867 cairo_device_release (&display->base);
1869 surface->drawable = drawable;
1871 surface->width = width;
1872 surface->height = height;
1876 * cairo_xlib_surface_get_display:
1877 * @surface: a #cairo_xlib_surface_t
1879 * Get the X Display for the underlying X Drawable.
1881 * Return value: the display.
1886 cairo_xlib_surface_get_display (cairo_surface_t *abstract_surface)
1888 if (! _cairo_surface_is_xlib (abstract_surface)) {
1889 _cairo_error_throw (CAIRO_STATUS_SURFACE_TYPE_MISMATCH);
1893 return ((cairo_xlib_display_t *) abstract_surface->device)->display;
1897 * cairo_xlib_surface_get_drawable:
1898 * @surface: a #cairo_xlib_surface_t
1900 * Get the underlying X Drawable used for the surface.
1902 * Return value: the drawable.
1907 cairo_xlib_surface_get_drawable (cairo_surface_t *abstract_surface)
1909 cairo_xlib_surface_t *surface = (cairo_xlib_surface_t *) abstract_surface;
1911 if (! _cairo_surface_is_xlib (abstract_surface)) {
1912 _cairo_error_throw (CAIRO_STATUS_SURFACE_TYPE_MISMATCH);
1916 return surface->drawable;
1920 * cairo_xlib_surface_get_screen:
1921 * @surface: a #cairo_xlib_surface_t
1923 * Get the X Screen for the underlying X Drawable.
1925 * Return value: the screen.
1930 cairo_xlib_surface_get_screen (cairo_surface_t *abstract_surface)
1932 cairo_xlib_surface_t *surface = (cairo_xlib_surface_t *) abstract_surface;
1934 if (! _cairo_surface_is_xlib (abstract_surface)) {
1935 _cairo_error_throw (CAIRO_STATUS_SURFACE_TYPE_MISMATCH);
1939 return surface->screen->screen;
1943 * cairo_xlib_surface_get_visual:
1944 * @surface: a #cairo_xlib_surface_t
1946 * Gets the X Visual associated with @surface, suitable for use with the
1947 * underlying X Drawable. If @surface was created by
1948 * cairo_xlib_surface_create(), the return value is the Visual passed to that
1951 * Return value: the Visual or %NULL if there is no appropriate Visual for
1957 cairo_xlib_surface_get_visual (cairo_surface_t *surface)
1959 cairo_xlib_surface_t *xlib_surface = (cairo_xlib_surface_t *) surface;
1961 if (! _cairo_surface_is_xlib (surface)) {
1962 _cairo_error_throw (CAIRO_STATUS_SURFACE_TYPE_MISMATCH);
1966 return xlib_surface->visual;
1970 * cairo_xlib_surface_get_depth:
1971 * @surface: a #cairo_xlib_surface_t
1973 * Get the number of bits used to represent each pixel value.
1975 * Return value: the depth of the surface in bits.
1980 cairo_xlib_surface_get_depth (cairo_surface_t *abstract_surface)
1982 cairo_xlib_surface_t *surface = (cairo_xlib_surface_t *) abstract_surface;
1984 if (! _cairo_surface_is_xlib (abstract_surface)) {
1985 _cairo_error_throw (CAIRO_STATUS_SURFACE_TYPE_MISMATCH);
1989 return surface->depth;
1993 * cairo_xlib_surface_get_width:
1994 * @surface: a #cairo_xlib_surface_t
1996 * Get the width of the X Drawable underlying the surface in pixels.
1998 * Return value: the width of the surface in pixels.
2003 cairo_xlib_surface_get_width (cairo_surface_t *abstract_surface)
2005 cairo_xlib_surface_t *surface = (cairo_xlib_surface_t *) abstract_surface;
2007 if (! _cairo_surface_is_xlib (abstract_surface)) {
2008 _cairo_error_throw (CAIRO_STATUS_SURFACE_TYPE_MISMATCH);
2012 return surface->width;
2016 * cairo_xlib_surface_get_height:
2017 * @surface: a #cairo_xlib_surface_t
2019 * Get the height of the X Drawable underlying the surface in pixels.
2021 * Return value: the height of the surface in pixels.
2026 cairo_xlib_surface_get_height (cairo_surface_t *abstract_surface)
2028 cairo_xlib_surface_t *surface = (cairo_xlib_surface_t *) abstract_surface;
2030 if (! _cairo_surface_is_xlib (abstract_surface)) {
2031 _cairo_error_throw (CAIRO_STATUS_SURFACE_TYPE_MISMATCH);
2035 return surface->height;
2038 #endif /* !CAIRO_HAS_XLIB_XCB_FUNCTIONS */