1 /* cairo - a vector graphics library with display and print output
3 * Copyright © 2009 Intel Corporation
5 * This library is free software; you can redistribute it and/or
6 * modify it either under the terms of the GNU Lesser General Public
7 * License version 2.1 as published by the Free Software Foundation
8 * (the "LGPL") or, at your option, under the terms of the Mozilla
9 * Public License Version 1.1 (the "MPL"). If you do not alter this
10 * notice, a recipient may use your version of this file under either
11 * the MPL or the LGPL.
13 * You should have received a copy of the LGPL along with this library
14 * in the file COPYING-LGPL-2.1; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA
16 * You should have received a copy of the MPL along with this library
17 * in the file COPYING-MPL-1.1
19 * The contents of this file are subject to the Mozilla Public License
20 * Version 1.1 (the "License"); you may not use this file except in
21 * compliance with the License. You may obtain a copy of the License at
22 * http://www.mozilla.org/MPL/
24 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY
25 * OF ANY KIND, either express or implied. See the LGPL or the MPL for
26 * the specific language governing rights and limitations.
29 * Chris Wilson <chris@chris-wilson.co.uk>
34 #include "cairo-xcb-private.h"
36 #include "cairo-boxes-private.h"
37 #include "cairo-clip-inline.h"
38 #include "cairo-clip-private.h"
39 #include "cairo-composite-rectangles-private.h"
40 #include "cairo-image-surface-private.h"
41 #include "cairo-list-inline.h"
42 #include "cairo-region-private.h"
43 #include "cairo-surface-offset-private.h"
44 #include "cairo-surface-snapshot-inline.h"
45 #include "cairo-surface-subsurface-private.h"
46 #include "cairo-traps-private.h"
47 #include "cairo-recording-surface-inline.h"
48 #include "cairo-paginated-private.h"
49 #include "cairo-pattern-inline.h"
51 #define PIXMAN_MAX_INT ((pixman_fixed_1 >> 1) - pixman_fixed_e) /* need to ensure deltas also fit */
54 _clip_and_composite_boxes (cairo_xcb_surface_t *dst,
56 const cairo_pattern_t *src,
58 cairo_composite_rectangles_t *extents);
60 static inline cairo_xcb_connection_t *
61 _picture_to_connection (cairo_xcb_picture_t *picture)
63 return (cairo_xcb_connection_t *) picture->base.device;
67 _cairo_xcb_surface_ensure_picture (cairo_xcb_surface_t *surface);
70 hars_petruska_f54_1_random (void)
72 #define rol(x,k) ((x << k) | (x >> (32-k)))
74 return x = (x ^ rol (x, 5) ^ rol (x, 24)) + 0x37798849;
79 _cairo_xcb_picture_finish (void *abstract_surface)
81 cairo_xcb_picture_t *surface = abstract_surface;
82 cairo_xcb_connection_t *connection = _picture_to_connection (surface);
83 cairo_status_t status;
85 status = _cairo_xcb_connection_acquire (connection);
86 cairo_list_del (&surface->link);
87 if (unlikely (status))
90 _cairo_xcb_connection_render_free_picture (connection, surface->picture);
92 _cairo_xcb_connection_release (connection);
94 return CAIRO_STATUS_SUCCESS;
97 static const cairo_surface_backend_t _cairo_xcb_picture_backend = {
98 CAIRO_SURFACE_TYPE_XCB,
99 _cairo_xcb_picture_finish,
102 static const struct xcb_render_transform_t identity_transform = {
108 static cairo_xcb_picture_t *
109 _cairo_xcb_picture_create (cairo_xcb_screen_t *screen,
110 pixman_format_code_t pixman_format,
111 xcb_render_pictformat_t xrender_format,
112 int width, int height)
114 cairo_xcb_picture_t *surface;
116 surface = malloc (sizeof (cairo_xcb_picture_t));
117 if (unlikely (surface == NULL))
118 return (cairo_xcb_picture_t *)
119 _cairo_surface_create_in_error (_cairo_error (CAIRO_STATUS_NO_MEMORY));
121 _cairo_surface_init (&surface->base,
122 &_cairo_xcb_picture_backend,
123 &screen->connection->device,
124 _cairo_content_from_pixman_format (pixman_format));
126 cairo_list_add (&surface->link, &screen->pictures);
128 surface->screen = screen;
129 surface->picture = _cairo_xcb_connection_get_xid (screen->connection);
130 surface->pixman_format = pixman_format;
131 surface->xrender_format = xrender_format;
133 surface->x0 = surface->y0 = 0;
134 surface->x = surface->y = 0;
135 surface->width = width;
136 surface->height = height;
138 surface->transform = identity_transform;
139 surface->extend = CAIRO_EXTEND_NONE;
140 surface->filter = CAIRO_FILTER_NEAREST;
141 surface->has_component_alpha = FALSE;
146 static inline cairo_bool_t
147 _operator_is_supported (uint32_t flags, cairo_operator_t op)
149 if (op <= CAIRO_OPERATOR_SATURATE)
152 /* Can we use PDF operators? */
153 #if CAIRO_XCB_RENDER_AT_LEAST(0, 11)
154 if (op <= CAIRO_OPERATOR_HSL_LUMINOSITY)
155 return flags & CAIRO_XCB_RENDER_HAS_PDF_OPERATORS;
162 _render_operator (cairo_operator_t op)
164 #define C(x,y) case CAIRO_OPERATOR_##x: return XCB_RENDER_PICT_OP_##y
175 C(DEST_OVER, OVER_REVERSE);
176 C(DEST_IN, IN_REVERSE);
177 C(DEST_OUT, OUT_REVERSE);
178 C(DEST_ATOP, ATOP_REVERSE);
182 C(SATURATE, SATURATE);
184 /* PDF operators were added in RENDER 0.11, check if the xcb headers have
185 * the defines, else fall through to the default case. */
186 #if CAIRO_XCB_RENDER_AT_LEAST(0, 11)
187 #define BLEND(x,y) C(x,y)
189 #define BLEND(x,y) case CAIRO_OPERATOR_##x:
191 BLEND(MULTIPLY, MULTIPLY);
192 BLEND(SCREEN, SCREEN);
193 BLEND(OVERLAY, OVERLAY);
194 BLEND(DARKEN, DARKEN);
195 BLEND(LIGHTEN, LIGHTEN);
196 BLEND(COLOR_DODGE, COLOR_DODGE);
197 BLEND(COLOR_BURN, COLOR_BURN);
198 BLEND(HARD_LIGHT, HARD_LIGHT);
199 BLEND(SOFT_LIGHT, SOFT_LIGHT);
200 BLEND(DIFFERENCE, DIFFERENCE);
201 BLEND(EXCLUSION, EXCLUSION);
202 BLEND(HSL_HUE, HSL_HUE);
203 BLEND(HSL_SATURATION, HSL_SATURATION);
204 BLEND(HSL_COLOR, HSL_COLOR);
205 BLEND(HSL_LUMINOSITY, HSL_LUMINOSITY);
209 return XCB_RENDER_PICT_OP_OVER;
213 static cairo_status_t
214 _cairo_xcb_surface_set_clip_region (cairo_xcb_surface_t *surface,
215 cairo_region_t *region)
217 xcb_rectangle_t stack_rects[CAIRO_STACK_ARRAY_LENGTH (xcb_rectangle_t)];
218 xcb_rectangle_t *rects = stack_rects;
221 num_rects = cairo_region_num_rectangles (region);
223 if (num_rects > ARRAY_LENGTH (stack_rects)) {
224 rects = _cairo_malloc_ab (num_rects, sizeof (xcb_rectangle_t));
225 if (unlikely (rects == NULL)) {
226 return _cairo_error (CAIRO_STATUS_NO_MEMORY);
230 for (i = 0; i < num_rects; i++) {
231 cairo_rectangle_int_t rect;
233 cairo_region_get_rectangle (region, i, &rect);
237 rects[i].width = rect.width;
238 rects[i].height = rect.height;
241 _cairo_xcb_connection_render_set_picture_clip_rectangles (surface->connection,
246 if (rects != stack_rects)
249 return CAIRO_STATUS_SUCCESS;
253 _cairo_xcb_surface_clear_clip_region (cairo_xcb_surface_t *surface)
255 uint32_t values[] = { XCB_NONE };
256 _cairo_xcb_connection_render_change_picture (surface->connection,
258 XCB_RENDER_CP_CLIP_MASK,
263 _cairo_xcb_surface_set_precision (cairo_xcb_surface_t *surface,
264 cairo_antialias_t antialias)
266 cairo_xcb_connection_t *connection = surface->connection;
269 if (connection->force_precision != -1)
270 precision = connection->force_precision;
271 else switch (antialias) {
273 case CAIRO_ANTIALIAS_DEFAULT:
274 case CAIRO_ANTIALIAS_GRAY:
275 case CAIRO_ANTIALIAS_NONE:
276 case CAIRO_ANTIALIAS_FAST:
277 case CAIRO_ANTIALIAS_GOOD:
278 precision = XCB_RENDER_POLY_MODE_IMPRECISE;
280 case CAIRO_ANTIALIAS_SUBPIXEL:
281 case CAIRO_ANTIALIAS_BEST:
282 precision = XCB_RENDER_POLY_MODE_PRECISE;
286 if (surface->precision != precision) {
287 _cairo_xcb_connection_render_change_picture (connection,
289 XCB_RENDER_CP_POLY_MODE,
291 surface->precision = precision;
297 _cairo_xcb_surface_ensure_picture (cairo_xcb_surface_t *surface)
299 assert (surface->fallback == NULL);
300 if (surface->picture == XCB_NONE) {
304 if (surface->precision != XCB_RENDER_POLY_MODE_PRECISE) {
305 flags |= XCB_RENDER_CP_POLY_MODE;
306 values[0] = surface->precision;
309 surface->picture = _cairo_xcb_connection_get_xid (surface->connection);
310 _cairo_xcb_connection_render_create_picture (surface->connection,
313 surface->xrender_format,
318 static cairo_xcb_picture_t *
319 _picture_from_image (cairo_xcb_surface_t *target,
320 xcb_render_pictformat_t format,
321 cairo_image_surface_t *image,
322 cairo_xcb_shm_info_t *shm_info)
326 cairo_xcb_picture_t *picture;
328 pixmap = _cairo_xcb_connection_create_pixmap (target->connection,
331 image->width, image->height);
333 gc = _cairo_xcb_screen_get_gc (target->screen, pixmap, image->depth);
335 if (shm_info != NULL) {
336 _cairo_xcb_connection_shm_put_image (target->connection,
338 image->width, image->height,
340 image->width, image->height,
348 /* Do we need to trim the image? */
349 len = CAIRO_STRIDE_FOR_WIDTH_BPP (image->width, PIXMAN_FORMAT_BPP (image->pixman_format));
350 if (len == image->stride) {
351 _cairo_xcb_connection_put_image (target->connection,
353 image->width, image->height,
359 _cairo_xcb_connection_put_subimage (target->connection,
362 image->width, image->height,
363 PIXMAN_FORMAT_BPP (image->pixman_format) / 8,
372 _cairo_xcb_screen_put_gc (target->screen, image->depth, gc);
374 picture = _cairo_xcb_picture_create (target->screen,
375 image->pixman_format, format,
376 image->width, image->height);
377 if (likely (picture->base.status == CAIRO_STATUS_SUCCESS)) {
378 _cairo_xcb_connection_render_create_picture (target->connection,
379 picture->picture, pixmap, format,
383 _cairo_xcb_connection_free_pixmap (target->connection, pixmap);
389 _pattern_is_supported (uint32_t flags,
390 const cairo_pattern_t *pattern)
393 if (pattern->type == CAIRO_PATTERN_TYPE_SOLID)
396 if (! _cairo_matrix_is_integer_translation (&pattern->matrix, NULL, NULL)) {
397 if ((flags & CAIRO_XCB_RENDER_HAS_PICTURE_TRANSFORM) == 0)
401 switch (pattern->extend) {
404 case CAIRO_EXTEND_NONE:
405 case CAIRO_EXTEND_REPEAT:
407 case CAIRO_EXTEND_PAD:
408 case CAIRO_EXTEND_REFLECT:
409 if ((flags & CAIRO_XCB_RENDER_HAS_EXTENDED_REPEAT) == 0)
413 if (pattern->type == CAIRO_PATTERN_TYPE_SURFACE) {
414 cairo_filter_t filter;
416 filter = pattern->filter;
417 if (_cairo_matrix_has_unity_scale (&pattern->matrix) &&
418 _cairo_matrix_is_integer_translation (&pattern->matrix, NULL, NULL))
420 filter = CAIRO_FILTER_NEAREST;
423 if (! (filter == CAIRO_FILTER_NEAREST || filter == CAIRO_FILTER_FAST)) {
424 if ((flags & CAIRO_XCB_RENDER_HAS_FILTERS) == 0)
427 } else { /* gradient */
428 if ((flags & CAIRO_XCB_RENDER_HAS_GRADIENTS) == 0)
431 /* The RENDER specification says that the inner circle has to be
432 * completely contained inside the outer one. */
433 if (pattern->type == CAIRO_PATTERN_TYPE_RADIAL &&
434 ! _cairo_radial_pattern_focus_is_inside ((cairo_radial_pattern_t *) pattern))
440 return pattern->type != CAIRO_PATTERN_TYPE_MESH;
444 _cairo_xcb_picture_set_matrix (cairo_xcb_picture_t *picture,
445 const cairo_matrix_t *matrix,
446 cairo_filter_t filter,
447 double xc, double yc)
449 xcb_render_transform_t transform;
450 pixman_transform_t *pixman_transform;
451 cairo_int_status_t ignored;
453 /* Casting between pixman_transform_t and xcb_render_transform_t is safe
454 * because they happen to be the exact same type.
456 pixman_transform = (pixman_transform_t *) &transform;
458 picture->x = picture->x0;
459 picture->y = picture->y0;
460 ignored = _cairo_matrix_to_pixman_matrix_offset (matrix, filter, xc, yc,
462 &picture->x, &picture->y);
465 if (memcmp (&picture->transform, &transform, sizeof (xcb_render_transform_t))) {
466 _cairo_xcb_connection_render_set_picture_transform (_picture_to_connection (picture),
470 picture->transform = transform;
475 _cairo_xcb_picture_set_filter (cairo_xcb_picture_t *picture,
476 cairo_filter_t filter)
478 const char *render_filter;
481 if (picture->filter == filter)
485 case CAIRO_FILTER_FAST:
486 render_filter = "fast";
487 len = strlen ("fast");
490 case CAIRO_FILTER_GOOD:
491 render_filter = "good";
492 len = strlen ("good");
495 case CAIRO_FILTER_BEST:
496 render_filter = "best";
497 len = strlen ("best");
500 case CAIRO_FILTER_NEAREST:
501 render_filter = "nearest";
502 len = strlen ("nearest");
505 case CAIRO_FILTER_BILINEAR:
506 render_filter = "bilinear";
507 len = strlen ("bilinear");
512 case CAIRO_FILTER_GAUSSIAN:
513 render_filter = "best";
514 len = strlen ("best");
518 _cairo_xcb_connection_render_set_picture_filter (_picture_to_connection (picture),
520 len, (char *) render_filter);
521 picture->filter = filter;
525 _cairo_xcb_picture_set_extend (cairo_xcb_picture_t *picture,
526 cairo_extend_t extend)
530 if (picture->extend == extend)
536 case CAIRO_EXTEND_NONE:
537 pa[0] = XCB_RENDER_REPEAT_NONE;
540 case CAIRO_EXTEND_REPEAT:
541 pa[0] = XCB_RENDER_REPEAT_NORMAL;
544 case CAIRO_EXTEND_REFLECT:
545 pa[0] = XCB_RENDER_REPEAT_REFLECT;
548 case CAIRO_EXTEND_PAD:
549 pa[0] = XCB_RENDER_REPEAT_PAD;
553 _cairo_xcb_connection_render_change_picture (_picture_to_connection (picture),
555 XCB_RENDER_CP_REPEAT, pa);
556 picture->extend = extend;
560 _cairo_xcb_picture_set_component_alpha (cairo_xcb_picture_t *picture,
565 if (picture->has_component_alpha == ca)
570 _cairo_xcb_connection_render_change_picture (_picture_to_connection (picture),
572 XCB_RENDER_CP_COMPONENT_ALPHA,
574 picture->has_component_alpha = ca;
577 static cairo_xcb_picture_t *
578 _solid_picture (cairo_xcb_surface_t *target,
579 const cairo_color_t *color)
581 xcb_render_color_t xcb_color;
582 xcb_render_pictformat_t xrender_format;
583 cairo_xcb_picture_t *picture;
585 xcb_color.red = color->red_short;
586 xcb_color.green = color->green_short;
587 xcb_color.blue = color->blue_short;
588 xcb_color.alpha = color->alpha_short;
590 xrender_format = target->screen->connection->standard_formats[CAIRO_FORMAT_ARGB32];
591 picture = _cairo_xcb_picture_create (target->screen,
595 if (unlikely (picture->base.status))
598 if (target->connection->flags & CAIRO_XCB_RENDER_HAS_GRADIENTS) {
599 _cairo_xcb_connection_render_create_solid_fill (target->connection,
604 uint32_t values[] = { XCB_RENDER_REPEAT_NORMAL };
606 pixmap = _cairo_xcb_connection_create_pixmap (target->connection,
607 32, target->drawable, 1, 1);
608 _cairo_xcb_connection_render_create_picture (target->connection,
612 XCB_RENDER_CP_REPEAT,
614 if (target->connection->flags & CAIRO_XCB_RENDER_HAS_FILL_RECTANGLES) {
615 xcb_rectangle_t rect;
618 rect.width = rect.height = 1;
620 _cairo_xcb_connection_render_fill_rectangles (_picture_to_connection (picture),
621 XCB_RENDER_PICT_OP_SRC,
623 xcb_color, 1, &rect);
628 gc = _cairo_xcb_screen_get_gc (target->screen, pixmap, 32);
630 /* XXX byte ordering? */
631 pixel = ((color->alpha_short >> 8) << 24) |
632 ((color->red_short >> 8) << 16) |
633 ((color->green_short >> 8) << 8) |
634 ((color->blue_short >> 8) << 0);
636 _cairo_xcb_connection_put_image (target->connection,
641 _cairo_xcb_screen_put_gc (target->screen, 32, gc);
644 _cairo_xcb_connection_free_pixmap (target->connection, pixmap);
650 static cairo_xcb_picture_t *
651 _cairo_xcb_transparent_picture (cairo_xcb_surface_t *target)
653 cairo_xcb_picture_t *picture;
655 picture = (cairo_xcb_picture_t *) target->screen->stock_colors[CAIRO_STOCK_TRANSPARENT];
656 if (picture == NULL) {
657 picture = _solid_picture (target, CAIRO_COLOR_TRANSPARENT);
658 target->screen->stock_colors[CAIRO_STOCK_TRANSPARENT] = &picture->base;
661 return (cairo_xcb_picture_t *) cairo_surface_reference (&picture->base);
664 static cairo_xcb_picture_t *
665 _cairo_xcb_black_picture (cairo_xcb_surface_t *target)
667 cairo_xcb_picture_t *picture;
669 picture = (cairo_xcb_picture_t *) target->screen->stock_colors[CAIRO_STOCK_BLACK];
670 if (picture == NULL) {
671 picture = _solid_picture (target, CAIRO_COLOR_BLACK);
672 target->screen->stock_colors[CAIRO_STOCK_BLACK] = &picture->base;
675 return (cairo_xcb_picture_t *) cairo_surface_reference (&picture->base);
678 static cairo_xcb_picture_t *
679 _cairo_xcb_white_picture (cairo_xcb_surface_t *target)
681 cairo_xcb_picture_t *picture;
683 picture = (cairo_xcb_picture_t *) target->screen->stock_colors[CAIRO_STOCK_WHITE];
684 if (picture == NULL) {
685 picture = _solid_picture (target, CAIRO_COLOR_WHITE);
686 target->screen->stock_colors[CAIRO_STOCK_WHITE] = &picture->base;
689 return (cairo_xcb_picture_t *) cairo_surface_reference (&picture->base);
692 static cairo_xcb_picture_t *
693 _cairo_xcb_solid_picture (cairo_xcb_surface_t *target,
694 const cairo_solid_pattern_t *pattern)
696 cairo_xcb_picture_t *picture;
697 cairo_xcb_screen_t *screen;
700 if (pattern->color.alpha_short <= 0x00ff)
701 return _cairo_xcb_transparent_picture (target);
703 if (pattern->color.alpha_short >= 0xff00) {
704 if (pattern->color.red_short <= 0x00ff &&
705 pattern->color.green_short <= 0x00ff &&
706 pattern->color.blue_short <= 0x00ff)
708 return _cairo_xcb_black_picture (target);
711 if (pattern->color.red_short >= 0xff00 &&
712 pattern->color.green_short >= 0xff00 &&
713 pattern->color.blue_short >= 0xff00)
715 return _cairo_xcb_white_picture (target);
719 screen = target->screen;
720 n_cached = screen->solid_cache_size;
721 for (i = 0; i < n_cached; i++) {
722 if (_cairo_color_equal (&screen->solid_cache[i].color, &pattern->color)) {
723 return (cairo_xcb_picture_t *) cairo_surface_reference (screen->solid_cache[i].picture);
727 picture = _solid_picture (target, &pattern->color);
728 if (unlikely (picture->base.status))
731 if (screen->solid_cache_size < ARRAY_LENGTH (screen->solid_cache)) {
732 i = screen->solid_cache_size++;
734 i = hars_petruska_f54_1_random () % ARRAY_LENGTH (screen->solid_cache);
735 cairo_surface_destroy (screen->solid_cache[i].picture);
737 screen->solid_cache[i].picture = cairo_surface_reference (&picture->base);
738 screen->solid_cache[i].color = pattern->color;
743 static cairo_xcb_picture_t *
744 _render_to_picture (cairo_xcb_surface_t *target,
745 const cairo_pattern_t *pattern,
746 const cairo_rectangle_int_t *extents)
748 cairo_image_surface_t *image;
749 cairo_xcb_shm_info_t *shm_info;
750 cairo_pattern_union_t copy;
751 cairo_status_t status;
752 cairo_xcb_picture_t *picture;
753 pixman_format_code_t pixman_format;
754 xcb_render_pictformat_t xrender_format;
756 /* XXX handle extend modes via tiling? */
757 /* XXX alpha-only masks? */
759 pixman_format = PIXMAN_a8r8g8b8;
760 xrender_format = target->screen->connection->standard_formats[CAIRO_FORMAT_ARGB32];
762 status = _cairo_xcb_shm_image_create (target->screen->connection,
764 extents->width, extents->height,
766 if (unlikely (status))
767 return (cairo_xcb_picture_t *) _cairo_surface_create_in_error (status);
769 _cairo_pattern_init_static_copy (©.base, pattern);
770 cairo_matrix_translate (©.base.matrix, extents->x, extents->y);
771 status = _cairo_surface_paint (&image->base,
772 CAIRO_OPERATOR_SOURCE,
775 if (unlikely (status)) {
776 cairo_surface_destroy (&image->base);
777 return (cairo_xcb_picture_t *) _cairo_surface_create_in_error (status);
780 picture = _picture_from_image (target, xrender_format, image, shm_info);
781 cairo_surface_destroy (&image->base);
783 if (unlikely (picture->base.status))
786 _cairo_xcb_picture_set_component_alpha (picture, pattern->has_component_alpha);
787 picture->x = -extents->x;
788 picture->y = -extents->y;
793 static xcb_render_fixed_t *
794 _gradient_to_xcb (const cairo_gradient_pattern_t *gradient,
795 unsigned int *n_stops,
796 char *buf, unsigned int buflen)
798 xcb_render_fixed_t *stops;
799 xcb_render_color_t *colors;
802 assert (gradient->n_stops > 0);
803 *n_stops = MAX (gradient->n_stops, 2);
805 if (*n_stops * (sizeof (xcb_render_fixed_t) + sizeof (xcb_render_color_t)) < buflen)
807 stops = (xcb_render_fixed_t *) buf;
812 _cairo_malloc_ab (*n_stops,
813 sizeof (xcb_render_fixed_t) + sizeof (xcb_render_color_t));
814 if (unlikely (stops == NULL))
818 colors = (xcb_render_color_t *) (stops + *n_stops);
819 for (i = 0; i < gradient->n_stops; i++) {
821 _cairo_fixed_16_16_from_double (gradient->stops[i].offset);
823 colors[i].red = gradient->stops[i].color.red_short;
824 colors[i].green = gradient->stops[i].color.green_short;
825 colors[i].blue = gradient->stops[i].color.blue_short;
826 colors[i].alpha = gradient->stops[i].color.alpha_short;
829 /* RENDER does not support gradients with less than 2 stops. If a
830 * gradient has only a single stop, duplicate it to make RENDER
832 if (gradient->n_stops == 1) {
833 stops[1] = _cairo_fixed_16_16_from_double (gradient->stops[0].offset);
835 colors[1].red = gradient->stops[0].color.red_short;
836 colors[1].green = gradient->stops[0].color.green_short;
837 colors[1].blue = gradient->stops[0].color.blue_short;
838 colors[1].alpha = gradient->stops[0].color.alpha_short;
844 static cairo_xcb_picture_t *
845 _cairo_xcb_linear_picture (cairo_xcb_surface_t *target,
846 const cairo_linear_pattern_t *pattern,
847 const cairo_rectangle_int_t *extents)
849 char buf[CAIRO_STACK_BUFFER_SIZE];
850 xcb_render_fixed_t *stops;
851 xcb_render_color_t *colors;
852 xcb_render_pointfix_t p1, p2;
853 cairo_matrix_t matrix;
854 cairo_circle_double_t extremes[2];
855 cairo_xcb_picture_t *picture;
856 cairo_status_t status;
857 unsigned int n_stops;
859 _cairo_gradient_pattern_fit_to_range (&pattern->base, PIXMAN_MAX_INT >> 1, &matrix, extremes);
861 picture = (cairo_xcb_picture_t *)
862 _cairo_xcb_screen_lookup_linear_picture (target->screen, pattern);
866 stops = _gradient_to_xcb (&pattern->base, &n_stops, buf, sizeof (buf));
867 if (unlikely (stops == NULL))
868 return (cairo_xcb_picture_t *) _cairo_surface_create_in_error (CAIRO_STATUS_NO_MEMORY);
870 picture = _cairo_xcb_picture_create (target->screen,
871 target->screen->connection->standard_formats[CAIRO_FORMAT_ARGB32],
874 if (unlikely (picture->base.status)) {
875 if (stops != (xcb_render_fixed_t *) buf)
879 picture->filter = CAIRO_FILTER_DEFAULT;
881 colors = (xcb_render_color_t *) (stops + n_stops);
883 p1.x = _cairo_fixed_16_16_from_double (extremes[0].center.x);
884 p1.y = _cairo_fixed_16_16_from_double (extremes[0].center.y);
885 p2.x = _cairo_fixed_16_16_from_double (extremes[1].center.x);
886 p2.y = _cairo_fixed_16_16_from_double (extremes[1].center.y);
888 _cairo_xcb_connection_render_create_linear_gradient (target->connection,
894 if (stops != (xcb_render_fixed_t *) buf)
897 status = _cairo_xcb_screen_store_linear_picture (target->screen,
900 if (unlikely (status)) {
901 cairo_surface_destroy (&picture->base);
902 return (cairo_xcb_picture_t *) _cairo_surface_create_in_error (status);
906 _cairo_xcb_picture_set_matrix (picture, &matrix,
907 pattern->base.base.filter,
908 extents->x + extents->width/2.,
909 extents->y + extents->height/2.);
910 _cairo_xcb_picture_set_filter (picture, pattern->base.base.filter);
911 _cairo_xcb_picture_set_extend (picture, pattern->base.base.extend);
912 _cairo_xcb_picture_set_component_alpha (picture,
913 pattern->base.base.has_component_alpha);
918 static cairo_xcb_picture_t *
919 _cairo_xcb_radial_picture (cairo_xcb_surface_t *target,
920 const cairo_radial_pattern_t *pattern,
921 const cairo_rectangle_int_t *extents)
923 char buf[CAIRO_STACK_BUFFER_SIZE];
924 xcb_render_fixed_t *stops;
925 xcb_render_color_t *colors;
926 xcb_render_pointfix_t p1, p2;
927 xcb_render_fixed_t r1, r2;
928 cairo_matrix_t matrix;
929 cairo_circle_double_t extremes[2];
930 cairo_xcb_picture_t *picture;
931 cairo_status_t status;
932 unsigned int n_stops;
934 _cairo_gradient_pattern_fit_to_range (&pattern->base, PIXMAN_MAX_INT >> 1, &matrix, extremes);
936 picture = (cairo_xcb_picture_t *)
937 _cairo_xcb_screen_lookup_radial_picture (target->screen, pattern);
941 stops = _gradient_to_xcb (&pattern->base, &n_stops, buf, sizeof (buf));
942 if (unlikely (stops == NULL))
943 return (cairo_xcb_picture_t *) _cairo_surface_create_in_error (CAIRO_STATUS_NO_MEMORY);
945 picture = _cairo_xcb_picture_create (target->screen,
946 target->screen->connection->standard_formats[CAIRO_FORMAT_ARGB32],
949 if (unlikely (picture->base.status)) {
950 if (stops != (xcb_render_fixed_t *) buf)
954 picture->filter = CAIRO_FILTER_DEFAULT;
956 colors = (xcb_render_color_t *) (stops + n_stops);
958 p1.x = _cairo_fixed_16_16_from_double (extremes[0].center.x);
959 p1.y = _cairo_fixed_16_16_from_double (extremes[0].center.y);
960 p2.x = _cairo_fixed_16_16_from_double (extremes[1].center.x);
961 p2.y = _cairo_fixed_16_16_from_double (extremes[1].center.y);
963 r1 = _cairo_fixed_16_16_from_double (extremes[0].radius);
964 r2 = _cairo_fixed_16_16_from_double (extremes[1].radius);
966 _cairo_xcb_connection_render_create_radial_gradient (target->connection,
972 if (stops != (xcb_render_fixed_t *) buf)
975 status = _cairo_xcb_screen_store_radial_picture (target->screen,
978 if (unlikely (status)) {
979 cairo_surface_destroy (&picture->base);
980 return (cairo_xcb_picture_t *) _cairo_surface_create_in_error (status);
984 _cairo_xcb_picture_set_matrix (picture, &matrix,
985 pattern->base.base.filter,
986 extents->x + extents->width/2.,
987 extents->y + extents->height/2.);
988 _cairo_xcb_picture_set_filter (picture, pattern->base.base.filter);
989 _cairo_xcb_picture_set_extend (picture, pattern->base.base.extend);
990 _cairo_xcb_picture_set_component_alpha (picture,
991 pattern->base.base.has_component_alpha);
996 static cairo_xcb_picture_t *
997 _copy_to_picture (cairo_xcb_surface_t *source)
999 cairo_xcb_picture_t *picture;
1000 uint32_t values[] = { 0, 1 };
1002 if (source->deferred_clear) {
1003 cairo_status_t status = _cairo_xcb_surface_clear (source);
1004 if (unlikely (status))
1005 return (cairo_xcb_picture_t *) _cairo_surface_create_in_error (status);
1008 picture = _cairo_xcb_picture_create (source->screen,
1009 source->xrender_format,
1010 source->pixman_format,
1013 if (unlikely (picture->base.status))
1016 _cairo_xcb_connection_render_create_picture (source->connection,
1019 source->xrender_format,
1020 XCB_RENDER_CP_GRAPHICS_EXPOSURE |
1021 XCB_RENDER_CP_SUBWINDOW_MODE,
1028 _cairo_xcb_surface_setup_surface_picture(cairo_xcb_picture_t *picture,
1029 const cairo_surface_pattern_t *pattern,
1030 const cairo_rectangle_int_t *extents)
1032 cairo_filter_t filter;
1034 filter = pattern->base.filter;
1035 if (filter != CAIRO_FILTER_NEAREST &&
1036 _cairo_matrix_has_unity_scale (&pattern->base.matrix) &&
1037 _cairo_fixed_is_integer (_cairo_fixed_from_double (pattern->base.matrix.x0)) &&
1038 _cairo_fixed_is_integer (_cairo_fixed_from_double (pattern->base.matrix.y0)))
1040 filter = CAIRO_FILTER_NEAREST;
1042 _cairo_xcb_picture_set_filter (picture, filter);
1044 _cairo_xcb_picture_set_matrix (picture,
1045 &pattern->base.matrix, filter,
1046 extents->x + extents->width/2.,
1047 extents->y + extents->height/2.);
1050 _cairo_xcb_picture_set_extend (picture, pattern->base.extend);
1051 _cairo_xcb_picture_set_component_alpha (picture, pattern->base.has_component_alpha);
1054 static cairo_xcb_picture_t *
1055 record_to_picture (cairo_surface_t *target,
1056 const cairo_surface_pattern_t *pattern,
1057 const cairo_rectangle_int_t *extents)
1059 cairo_surface_pattern_t tmp_pattern;
1060 cairo_xcb_picture_t *picture;
1061 cairo_status_t status;
1062 cairo_matrix_t matrix;
1063 cairo_surface_t *tmp;
1064 cairo_surface_t *source;
1065 cairo_rectangle_int_t limit;
1066 cairo_extend_t extend;
1068 /* XXX: The following was once more or less copied from cairo-xlibs-ource.c,
1069 * record_source() and recording_pattern_get_surface(), can we share a
1073 /* First get the 'real' recording surface and figure out the size for tmp */
1074 source = _cairo_pattern_get_source (pattern, &limit);
1075 assert (_cairo_surface_is_recording (source));
1077 if (! _cairo_matrix_is_identity (&pattern->base.matrix)) {
1078 double x1, y1, x2, y2;
1080 matrix = pattern->base.matrix;
1081 status = cairo_matrix_invert (&matrix);
1082 assert (status == CAIRO_STATUS_SUCCESS);
1086 x2 = limit.x + limit.width;
1087 y2 = limit.y + limit.height;
1089 _cairo_matrix_transform_bounding_box (&matrix,
1090 &x1, &y1, &x2, &y2, NULL);
1092 limit.x = floor (x1);
1093 limit.y = floor (y1);
1094 limit.width = ceil (x2) - limit.x;
1095 limit.height = ceil (y2) - limit.y;
1097 extend = pattern->base.extend;
1098 if (_cairo_rectangle_contains_rectangle (&limit, extents))
1099 extend = CAIRO_EXTEND_NONE;
1100 if (extend == CAIRO_EXTEND_NONE && ! _cairo_rectangle_intersect (&limit, extents))
1101 return _cairo_xcb_transparent_picture ((cairo_xcb_surface_t *) target);
1103 /* Now draw the recording surface to an xcb surface */
1104 tmp = _cairo_surface_create_similar_scratch (target,
1108 if (tmp->status != CAIRO_STATUS_SUCCESS) {
1109 return (cairo_xcb_picture_t *) tmp;
1112 cairo_matrix_init_translate (&matrix, limit.x, limit.y);
1113 cairo_matrix_multiply (&matrix, &matrix, &pattern->base.matrix);
1115 status = _cairo_recording_surface_replay_with_clip (source,
1118 if (unlikely (status)) {
1119 cairo_surface_destroy (tmp);
1120 return (cairo_xcb_picture_t *) _cairo_surface_create_in_error (status);
1123 /* Now that we have drawn this to an xcb surface, try again with that */
1124 _cairo_pattern_init_static_copy (&tmp_pattern.base, &pattern->base);
1125 tmp_pattern.surface = tmp;
1126 cairo_matrix_init_translate (&tmp_pattern.base.matrix, -limit.x, -limit.y);
1128 picture = _copy_to_picture ((cairo_xcb_surface_t *) tmp);
1129 if (picture->base.status == CAIRO_STATUS_SUCCESS)
1130 _cairo_xcb_surface_setup_surface_picture (picture, &tmp_pattern, extents);
1131 cairo_surface_destroy (tmp);
1135 static cairo_xcb_picture_t *
1136 _cairo_xcb_surface_picture (cairo_xcb_surface_t *target,
1137 const cairo_surface_pattern_t *pattern,
1138 const cairo_rectangle_int_t *extents)
1140 cairo_surface_t *source = pattern->surface;
1141 cairo_xcb_picture_t *picture;
1143 picture = (cairo_xcb_picture_t *)
1144 _cairo_surface_has_snapshot (source, &_cairo_xcb_picture_backend);
1145 if (picture != NULL) {
1146 if (picture->screen == target->screen) {
1147 picture = (cairo_xcb_picture_t *) cairo_surface_reference (&picture->base);
1148 _cairo_xcb_surface_setup_surface_picture (picture, pattern, extents);
1154 if (source->type == CAIRO_SURFACE_TYPE_XCB)
1156 if (source->backend->type == CAIRO_SURFACE_TYPE_XCB) {
1157 cairo_xcb_surface_t *xcb = (cairo_xcb_surface_t *) source;
1158 if (xcb->screen == target->screen && xcb->fallback == NULL) {
1159 picture = _copy_to_picture ((cairo_xcb_surface_t *) source);
1160 if (unlikely (picture->base.status))
1163 } else if (source->backend->type == CAIRO_SURFACE_TYPE_SUBSURFACE) {
1164 cairo_surface_subsurface_t *sub = (cairo_surface_subsurface_t *) source;
1165 cairo_xcb_surface_t *xcb = (cairo_xcb_surface_t *) sub->target;
1167 /* XXX repeat interval with source clipping? */
1168 if (FALSE && xcb->screen == target->screen && xcb->fallback == NULL) {
1169 xcb_rectangle_t rect;
1171 picture = _copy_to_picture (xcb);
1172 if (unlikely (picture->base.status))
1175 rect.x = sub->extents.x;
1176 rect.y = sub->extents.y;
1177 rect.width = sub->extents.width;
1178 rect.height = sub->extents.height;
1180 _cairo_xcb_connection_render_set_picture_clip_rectangles (xcb->connection,
1184 picture->x0 = rect.x;
1185 picture->y0 = rect.y;
1186 picture->width = rect.width;
1187 picture->height = rect.height;
1189 } else if (_cairo_surface_is_snapshot (source)) {
1190 cairo_surface_snapshot_t *snap = (cairo_surface_snapshot_t *) source;
1191 cairo_xcb_surface_t *xcb = (cairo_xcb_surface_t *) snap->target;
1193 if (xcb->screen == target->screen && xcb->fallback == NULL) {
1194 picture = _copy_to_picture (xcb);
1195 if (unlikely (picture->base.status))
1200 #if CAIRO_HAS_XLIB_XCB_FUNCTIONS
1201 else if (source->type == CAIRO_SURFACE_TYPE_XLIB)
1203 if (source->backend->type == CAIRO_SURFACE_TYPE_XLIB) {
1204 cairo_xcb_surface_t *xcb = ((cairo_xlib_xcb_surface_t *) source)->xcb;
1205 if (xcb->screen == target->screen && xcb->fallback == NULL) {
1206 picture = _copy_to_picture (xcb);
1207 if (unlikely (picture->base.status))
1210 } else if (source->backend->type == CAIRO_SURFACE_TYPE_SUBSURFACE) {
1211 cairo_surface_subsurface_t *sub = (cairo_surface_subsurface_t *) source;
1212 cairo_xcb_surface_t *xcb = ((cairo_xlib_xcb_surface_t *) sub->target)->xcb;
1214 if (FALSE && xcb->screen == target->screen && xcb->fallback == NULL) {
1215 xcb_rectangle_t rect;
1217 picture = _copy_to_picture (xcb);
1218 if (unlikely (picture->base.status))
1221 rect.x = sub->extents.x;
1222 rect.y = sub->extents.y;
1223 rect.width = sub->extents.width;
1224 rect.height = sub->extents.height;
1226 _cairo_xcb_connection_render_set_picture_clip_rectangles (xcb->connection,
1230 picture->x0 = rect.x;
1231 picture->y0 = rect.y;
1232 picture->width = rect.width;
1233 picture->height = rect.height;
1235 } else if (_cairo_surface_is_snapshot (source)) {
1236 cairo_surface_snapshot_t *snap = (cairo_surface_snapshot_t *) source;
1237 cairo_xcb_surface_t *xcb = ((cairo_xlib_xcb_surface_t *) snap->target)->xcb;
1239 if (xcb->screen == target->screen && xcb->fallback == NULL) {
1240 picture = _copy_to_picture (xcb);
1241 if (unlikely (picture->base.status))
1247 #if CAIRO_HAS_GL_FUNCTIONS
1248 else if (source->type == CAIRO_SURFACE_TYPE_GL)
1250 /* pixmap from texture */
1253 else if (source->type == CAIRO_SURFACE_TYPE_RECORDING)
1255 /* We have to skip the call to attach_snapshot() because we possibly
1256 * only drew part of the recording surface.
1257 * TODO: When can we safely attach a snapshot?
1259 return record_to_picture(&target->base, pattern, extents);
1262 if (picture == NULL) {
1263 cairo_image_surface_t *image;
1265 cairo_status_t status;
1267 status = _cairo_surface_acquire_source_image (source, &image, &image_extra);
1268 if (unlikely (status))
1269 return (cairo_xcb_picture_t *) _cairo_surface_create_in_error (status);
1271 if (image->format != CAIRO_FORMAT_INVALID) {
1272 xcb_render_pictformat_t format;
1274 format = target->screen->connection->standard_formats[image->format];
1276 picture = _picture_from_image (target, format, image, NULL);
1277 _cairo_surface_release_source_image (source, image, image_extra);
1279 cairo_image_surface_t *conv;
1280 xcb_render_pictformat_t render_format;
1282 /* XXX XRenderPutImage! */
1284 conv = _cairo_image_surface_coerce (image);
1285 _cairo_surface_release_source_image (source, image, image_extra);
1286 if (unlikely (conv->base.status))
1287 return (cairo_xcb_picture_t *) conv;
1289 render_format = target->screen->connection->standard_formats[conv->format];
1290 picture = _picture_from_image (target, render_format, conv, NULL);
1291 cairo_surface_destroy (&conv->base);
1294 if (unlikely (picture->base.status))
1298 _cairo_surface_attach_snapshot (source,
1302 _cairo_xcb_surface_setup_surface_picture (picture, pattern, extents);
1306 static cairo_xcb_picture_t *
1307 _cairo_xcb_picture_for_pattern (cairo_xcb_surface_t *target,
1308 const cairo_pattern_t *pattern,
1309 const cairo_rectangle_int_t *extents)
1311 if (pattern == NULL)
1312 return _cairo_xcb_white_picture (target);
1314 if (! _pattern_is_supported (target->connection->flags, pattern))
1315 return _render_to_picture (target, pattern, extents);
1317 switch (pattern->type) {
1318 case CAIRO_PATTERN_TYPE_SOLID:
1319 return _cairo_xcb_solid_picture (target, (cairo_solid_pattern_t *) pattern);
1321 case CAIRO_PATTERN_TYPE_LINEAR:
1322 return _cairo_xcb_linear_picture (target,
1323 (cairo_linear_pattern_t *) pattern,
1326 case CAIRO_PATTERN_TYPE_RADIAL:
1327 return _cairo_xcb_radial_picture (target,
1328 (cairo_radial_pattern_t *) pattern,
1331 case CAIRO_PATTERN_TYPE_SURFACE:
1332 return _cairo_xcb_surface_picture (target,
1333 (cairo_surface_pattern_t *) pattern,
1337 case CAIRO_PATTERN_TYPE_MESH:
1338 case CAIRO_PATTERN_TYPE_RASTER_SOURCE:
1339 return _render_to_picture (target, pattern, extents);
1343 COMPILE_TIME_ASSERT (sizeof (xcb_rectangle_t) <= sizeof (cairo_box_t));
1345 static cairo_status_t
1346 _render_fill_boxes (void *abstract_dst,
1347 cairo_operator_t op,
1348 const cairo_color_t *color,
1349 cairo_boxes_t *boxes)
1351 cairo_xcb_surface_t *dst = abstract_dst;
1352 xcb_rectangle_t stack_xrects[CAIRO_STACK_ARRAY_LENGTH (sizeof (xcb_rectangle_t))];
1353 xcb_rectangle_t *xrects = stack_xrects;
1354 xcb_render_color_t render_color;
1355 int render_op = _render_operator (op);
1356 struct _cairo_boxes_chunk *chunk;
1359 render_color.red = color->red_short;
1360 render_color.green = color->green_short;
1361 render_color.blue = color->blue_short;
1362 render_color.alpha = color->alpha_short;
1365 for (chunk = &boxes->chunks; chunk != NULL; chunk = chunk->next) {
1366 if (chunk->count > max_count)
1367 max_count = chunk->count;
1369 if (max_count > ARRAY_LENGTH (stack_xrects)) {
1370 xrects = _cairo_malloc_ab (max_count, sizeof (xcb_rectangle_t));
1371 if (unlikely (xrects == NULL))
1372 return _cairo_error (CAIRO_STATUS_NO_MEMORY);
1375 for (chunk = &boxes->chunks; chunk != NULL; chunk = chunk->next) {
1378 for (i = j = 0; i < chunk->count; i++) {
1379 int x1 = _cairo_fixed_integer_round_down (chunk->base[i].p1.x);
1380 int y1 = _cairo_fixed_integer_round_down (chunk->base[i].p1.y);
1381 int x2 = _cairo_fixed_integer_round_down (chunk->base[i].p2.x);
1382 int y2 = _cairo_fixed_integer_round_down (chunk->base[i].p2.y);
1384 if (x2 > x1 && y2 > y1) {
1387 xrects[j].width = x2 - x1;
1388 xrects[j].height = y2 - y1;
1394 _cairo_xcb_connection_render_fill_rectangles
1396 render_op, dst->picture,
1397 render_color, j, xrects);
1401 if (xrects != stack_xrects)
1404 return CAIRO_STATUS_SUCCESS;
1407 /* pixel aligned, non-overlapping boxes */
1408 static cairo_int_status_t
1409 _render_composite_boxes (cairo_xcb_surface_t *dst,
1410 cairo_operator_t op,
1411 const cairo_pattern_t *src_pattern,
1412 const cairo_pattern_t *mask_pattern,
1413 const cairo_rectangle_int_t *extents,
1414 const cairo_boxes_t *boxes)
1416 cairo_xcb_picture_t *src, *mask;
1417 const struct _cairo_boxes_chunk *chunk;
1418 xcb_rectangle_t stack_boxes[CAIRO_STACK_ARRAY_LENGTH (xcb_rectangle_t)];
1419 xcb_rectangle_t *clip_boxes;
1420 cairo_rectangle_int_t stack_extents;
1421 cairo_status_t status;
1425 render_op = _render_operator (op);
1427 if (src_pattern == NULL) {
1428 src_pattern = mask_pattern;
1429 mask_pattern = NULL;
1432 /* amalgamate into a single Composite call by setting a clip region */
1433 clip_boxes = stack_boxes;
1434 if (boxes->num_boxes > ARRAY_LENGTH (stack_boxes)) {
1435 clip_boxes = _cairo_malloc_ab (boxes->num_boxes, sizeof (xcb_rectangle_t));
1436 if (unlikely (clip_boxes == NULL))
1437 return _cairo_error (CAIRO_STATUS_NO_MEMORY);
1440 src = _cairo_xcb_picture_for_pattern (dst, src_pattern, extents);
1441 status = src->base.status;
1442 if (unlikely (status))
1446 for (chunk = &boxes->chunks; chunk != NULL; chunk = chunk->next) {
1447 const cairo_box_t *box = chunk->base;
1450 for (i = 0; i < chunk->count; i++) {
1451 int x = _cairo_fixed_integer_round_down (box[i].p1.x);
1452 int y = _cairo_fixed_integer_round_down (box[i].p1.y);
1453 int width = _cairo_fixed_integer_round_down (box[i].p2.x) - x;
1454 int height = _cairo_fixed_integer_round_down (box[i].p2.y) - y;
1456 if (width && height) {
1457 clip_boxes[num_boxes].x = x;
1458 clip_boxes[num_boxes].y = y;
1459 clip_boxes[num_boxes].width = width;
1460 clip_boxes[num_boxes].height = height;
1467 if (num_boxes > 1) {
1468 _cairo_xcb_connection_render_set_picture_clip_rectangles (dst->connection,
1474 stack_extents.x = clip_boxes[0].x;
1475 stack_extents.y = clip_boxes[0].y;
1476 stack_extents.width = clip_boxes[0].width;
1477 stack_extents.height = clip_boxes[0].height;
1478 extents = &stack_extents;
1481 if (mask_pattern != NULL) {
1482 mask = _cairo_xcb_picture_for_pattern (dst, mask_pattern, extents);
1483 status = mask->base.status;
1484 if (unlikely (status))
1487 _cairo_xcb_connection_render_composite (dst->connection,
1492 src->x + extents->x, src->y + extents->y,
1493 mask->x + extents->x, mask->y + extents->y,
1494 extents->x, extents->y,
1495 extents->width, extents->height);
1497 cairo_surface_destroy (&mask->base);
1499 _cairo_xcb_connection_render_composite (dst->connection,
1504 src->x + extents->x, src->y + extents->y,
1506 extents->x, extents->y,
1507 extents->width, extents->height);
1513 _cairo_xcb_surface_clear_clip_region (dst);
1516 cairo_surface_destroy (&src->base);
1520 if (clip_boxes != stack_boxes)
1527 #define CAIRO_FIXED_16_16_MIN _cairo_fixed_from_int (-32768)
1528 #define CAIRO_FIXED_16_16_MAX _cairo_fixed_from_int (32767)
1531 _line_exceeds_16_16 (const cairo_line_t *line)
1534 line->p1.x <= CAIRO_FIXED_16_16_MIN ||
1535 line->p1.x >= CAIRO_FIXED_16_16_MAX ||
1537 line->p2.x <= CAIRO_FIXED_16_16_MIN ||
1538 line->p2.x >= CAIRO_FIXED_16_16_MAX ||
1540 line->p1.y <= CAIRO_FIXED_16_16_MIN ||
1541 line->p1.y >= CAIRO_FIXED_16_16_MAX ||
1543 line->p2.y <= CAIRO_FIXED_16_16_MIN ||
1544 line->p2.y >= CAIRO_FIXED_16_16_MAX;
1548 _project_line_x_onto_16_16 (const cairo_line_t *line,
1550 cairo_fixed_t bottom,
1551 xcb_render_linefix_t *out)
1553 cairo_point_double_t p1, p2;
1556 p1.x = _cairo_fixed_to_double (line->p1.x);
1557 p1.y = _cairo_fixed_to_double (line->p1.y);
1559 p2.x = _cairo_fixed_to_double (line->p2.x);
1560 p2.y = _cairo_fixed_to_double (line->p2.y);
1562 m = (p2.x - p1.x) / (p2.y - p1.y);
1563 out->p1.x = _cairo_fixed_16_16_from_double (p1.x + m * _cairo_fixed_to_double (top - line->p1.y));
1564 out->p2.x = _cairo_fixed_16_16_from_double (p1.x + m * _cairo_fixed_to_double (bottom - line->p1.y));
1568 cairo_traps_t traps;
1569 cairo_antialias_t antialias;
1570 } composite_traps_info_t;
1572 COMPILE_TIME_ASSERT (sizeof (xcb_render_trapezoid_t) <= sizeof (cairo_trapezoid_t));
1574 static cairo_int_status_t
1575 _composite_traps (void *closure,
1576 cairo_xcb_surface_t *dst,
1577 cairo_operator_t op,
1578 const cairo_pattern_t *pattern,
1579 int dst_x, int dst_y,
1580 const cairo_rectangle_int_t *extents,
1583 composite_traps_info_t *info = closure;
1584 const cairo_traps_t *traps = &info->traps;
1585 cairo_xcb_picture_t *src;
1586 cairo_format_t format;
1587 xcb_render_pictformat_t xrender_format;
1588 xcb_render_trapezoid_t *xtraps;
1589 int render_reference_x, render_reference_y;
1590 cairo_status_t status;
1593 if (dst->deferred_clear) {
1594 status = _cairo_xcb_surface_clear (dst);
1595 if (unlikely (status))
1599 src = _cairo_xcb_picture_for_pattern (dst, pattern, extents);
1600 if (unlikely (src->base.status))
1601 return src->base.status;
1603 if (info->antialias == CAIRO_ANTIALIAS_NONE)
1604 format = CAIRO_FORMAT_A1;
1606 format = CAIRO_FORMAT_A8;
1607 xrender_format = dst->screen->connection->standard_formats[format];
1609 xtraps = (xcb_render_trapezoid_t *) traps->traps;
1610 for (i = 0; i < traps->num_traps; i++) {
1611 cairo_trapezoid_t t = traps->traps[i];
1613 /* top/bottom will be clamped to surface bounds */
1614 xtraps[i].top = _cairo_fixed_to_16_16 (t.top);
1615 xtraps[i].top -= dst_y << 16;
1616 xtraps[i].bottom = _cairo_fixed_to_16_16 (t.bottom);
1617 xtraps[i].bottom -= dst_y << 16;
1619 /* However, all the other coordinates will have been left untouched so
1620 * as not to introduce numerical error. Recompute them if they
1621 * exceed the 16.16 limits.
1623 if (unlikely (_line_exceeds_16_16 (&t.left))) {
1624 _project_line_x_onto_16_16 (&t.left,
1628 xtraps[i].left.p1.y = xtraps[i].top;
1629 xtraps[i].left.p2.y = xtraps[i].bottom;
1631 xtraps[i].left.p1.x = _cairo_fixed_to_16_16 (t.left.p1.x);
1632 xtraps[i].left.p1.y = _cairo_fixed_to_16_16 (t.left.p1.y);
1633 xtraps[i].left.p2.x = _cairo_fixed_to_16_16 (t.left.p2.x);
1634 xtraps[i].left.p2.y = _cairo_fixed_to_16_16 (t.left.p2.y);
1636 xtraps[i].left.p1.x -= dst_x << 16;
1637 xtraps[i].left.p1.y -= dst_y << 16;
1638 xtraps[i].left.p2.x -= dst_x << 16;
1639 xtraps[i].left.p2.y -= dst_y << 16;
1641 if (unlikely (_line_exceeds_16_16 (&t.right))) {
1642 _project_line_x_onto_16_16 (&t.right,
1646 xtraps[i].right.p1.y = xtraps[i].top;
1647 xtraps[i].right.p2.y = xtraps[i].bottom;
1649 xtraps[i].right.p1.x = _cairo_fixed_to_16_16 (t.right.p1.x);
1650 xtraps[i].right.p1.y = _cairo_fixed_to_16_16 (t.right.p1.y);
1651 xtraps[i].right.p2.x = _cairo_fixed_to_16_16 (t.right.p2.x);
1652 xtraps[i].right.p2.y = _cairo_fixed_to_16_16 (t.right.p2.y);
1654 xtraps[i].right.p1.x -= dst_x << 16;
1655 xtraps[i].right.p1.y -= dst_y << 16;
1656 xtraps[i].right.p2.x -= dst_x << 16;
1657 xtraps[i].right.p2.y -= dst_y << 16;
1660 if (xtraps[0].left.p1.y < xtraps[0].left.p2.y) {
1661 render_reference_x = xtraps[0].left.p1.x >> 16;
1662 render_reference_y = xtraps[0].left.p1.y >> 16;
1664 render_reference_x = xtraps[0].left.p2.x >> 16;
1665 render_reference_y = xtraps[0].left.p2.y >> 16;
1667 render_reference_x += src->x + dst_x;
1668 render_reference_y += src->y + dst_y;
1670 _cairo_xcb_surface_set_precision (dst, info->antialias);
1671 _cairo_xcb_connection_render_trapezoids (dst->connection,
1672 _render_operator (op),
1678 traps->num_traps, xtraps);
1680 cairo_surface_destroy (&src->base);
1682 return CAIRO_STATUS_SUCCESS;
1685 /* low-level composite driver */
1687 static cairo_xcb_surface_t *
1688 get_clip_surface (const cairo_clip_t *clip,
1689 cairo_xcb_surface_t *target,
1692 cairo_surface_t *surface;
1693 cairo_status_t status;
1695 surface = _cairo_surface_create_similar_solid (&target->base,
1696 CAIRO_CONTENT_ALPHA,
1697 clip->extents.width,
1698 clip->extents.height,
1700 if (unlikely (surface->status))
1701 return (cairo_xcb_surface_t *) surface;
1703 assert (surface->backend == &_cairo_xcb_surface_backend);
1704 status = _cairo_clip_combine_with_surface (clip, surface,
1705 clip->extents.x, clip->extents.y);
1706 if (unlikely (status)) {
1707 cairo_surface_destroy (surface);
1708 surface = _cairo_surface_create_in_error (status);
1711 *tx = clip->extents.x;
1712 *ty = clip->extents.y;
1714 return (cairo_xcb_surface_t *) surface;
1717 typedef cairo_int_status_t
1718 (*xcb_draw_func_t) (void *closure,
1719 cairo_xcb_surface_t *dst,
1720 cairo_operator_t op,
1721 const cairo_pattern_t *src,
1724 const cairo_rectangle_int_t *extents,
1725 cairo_clip_t *clip);
1727 static void do_unaligned_row(void (*blt)(void *closure,
1728 int16_t x, int16_t y,
1729 int16_t w, int16_t h,
1732 const cairo_box_t *b,
1733 int tx, int y, int h,
1736 int x1 = _cairo_fixed_integer_part (b->p1.x) - tx;
1737 int x2 = _cairo_fixed_integer_part (b->p2.x) - tx;
1739 if (! _cairo_fixed_is_integer (b->p1.x)) {
1740 blt(closure, x1, y, 1, h,
1741 coverage * (256 - _cairo_fixed_fractional_part (b->p1.x)));
1746 blt(closure, x1, y, x2-x1, h, (coverage << 8) - (coverage >> 8));
1748 if (! _cairo_fixed_is_integer (b->p2.x))
1749 blt(closure, x2, y, 1, h,
1750 coverage * _cairo_fixed_fractional_part (b->p2.x));
1752 blt(closure, x1, y, 1, h,
1753 coverage * (b->p2.x - b->p1.x));
1756 static void do_unaligned_box(void (*blt)(void *closure,
1757 int16_t x, int16_t y,
1758 int16_t w, int16_t h,
1761 const cairo_box_t *b, int tx, int ty)
1763 int y1 = _cairo_fixed_integer_part (b->p1.y) - ty;
1764 int y2 = _cairo_fixed_integer_part (b->p2.y) - ty;
1766 if (! _cairo_fixed_is_integer (b->p1.y)) {
1767 do_unaligned_row(blt, closure, b, tx, y1, 1,
1768 256 - _cairo_fixed_fractional_part (b->p1.y));
1773 do_unaligned_row(blt, closure, b, tx, y1, y2-y1, 256);
1775 if (! _cairo_fixed_is_integer (b->p2.y))
1776 do_unaligned_row(blt, closure, b, tx, y2, 1,
1777 _cairo_fixed_fractional_part (b->p2.y));
1779 do_unaligned_row(blt, closure, b, tx, y1, 1,
1784 static void blt_in(void *closure,
1785 int16_t x, int16_t y,
1786 int16_t w, int16_t h,
1789 cairo_xcb_surface_t *mask = closure;
1790 xcb_render_color_t color;
1791 xcb_rectangle_t rect;
1793 if (coverage == 0xffff)
1796 color.red = color.green = color.blue = 0;
1797 color.alpha = coverage;
1804 _cairo_xcb_connection_render_fill_rectangles (mask->connection,
1805 XCB_RENDER_PICT_OP_IN,
1810 static cairo_xcb_surface_t *
1811 _create_composite_mask (cairo_clip_t *clip,
1812 xcb_draw_func_t draw_func,
1813 xcb_draw_func_t mask_func,
1815 cairo_xcb_surface_t *dst,
1816 const cairo_rectangle_int_t*extents)
1818 cairo_xcb_surface_t *surface;
1819 cairo_bool_t need_clip_combine;
1820 cairo_int_status_t status;
1822 surface = (cairo_xcb_surface_t *)
1823 _cairo_xcb_surface_create_similar (dst, CAIRO_CONTENT_ALPHA,
1824 extents->width, extents->height);
1825 if (unlikely (surface->base.status))
1828 _cairo_xcb_surface_ensure_picture (surface);
1830 surface->deferred_clear_color = *CAIRO_COLOR_TRANSPARENT;
1831 surface->deferred_clear = TRUE;
1832 surface->base.is_clear = TRUE;
1835 status = mask_func (draw_closure, surface,
1836 CAIRO_OPERATOR_ADD, NULL,
1837 extents->x, extents->y,
1839 if (likely (status != CAIRO_INT_STATUS_UNSUPPORTED))
1843 /* Is it worth setting the clip region here? */
1844 status = draw_func (draw_closure, surface,
1845 CAIRO_OPERATOR_ADD, NULL,
1846 extents->x, extents->y,
1848 if (unlikely (status)) {
1849 cairo_surface_destroy (&surface->base);
1850 return (cairo_xcb_surface_t *) _cairo_surface_create_in_error (status);
1853 if (surface->connection->flags & CAIRO_XCB_RENDER_HAS_FILL_RECTANGLES) {
1856 for (i = 0; i < clip->num_boxes; i++) {
1857 cairo_box_t *b = &clip->boxes[i];
1859 if (! _cairo_fixed_is_integer (b->p1.x) ||
1860 ! _cairo_fixed_is_integer (b->p1.y) ||
1861 ! _cairo_fixed_is_integer (b->p2.x) ||
1862 ! _cairo_fixed_is_integer (b->p2.y))
1864 do_unaligned_box(blt_in, surface, b, extents->x, extents->y);
1868 need_clip_combine = clip->path != NULL;
1870 need_clip_combine = ! _cairo_clip_is_region (clip);
1872 if (need_clip_combine) {
1873 status = _cairo_clip_combine_with_surface (clip, &surface->base,
1874 extents->x, extents->y);
1875 if (unlikely (status)) {
1876 cairo_surface_destroy (&surface->base);
1877 return (cairo_xcb_surface_t *) _cairo_surface_create_in_error (status);
1884 /* Handles compositing with a clip surface when the operator allows
1885 * us to combine the clip with the mask
1887 static cairo_status_t
1888 _clip_and_composite_with_mask (cairo_clip_t *clip,
1889 cairo_operator_t op,
1890 const cairo_pattern_t *pattern,
1891 xcb_draw_func_t draw_func,
1892 xcb_draw_func_t mask_func,
1894 cairo_xcb_surface_t *dst,
1895 const cairo_rectangle_int_t*extents)
1897 cairo_xcb_surface_t *mask;
1898 cairo_xcb_picture_t *src;
1900 mask = _create_composite_mask (clip,
1901 draw_func, mask_func, draw_closure,
1903 if (unlikely (mask->base.status))
1904 return mask->base.status;
1906 if (pattern != NULL || dst->base.content != CAIRO_CONTENT_ALPHA) {
1907 src = _cairo_xcb_picture_for_pattern (dst, pattern, extents);
1908 if (unlikely (src->base.status)) {
1909 cairo_surface_destroy (&mask->base);
1910 return src->base.status;
1913 _cairo_xcb_connection_render_composite (dst->connection,
1914 _render_operator (op),
1918 extents->x + src->x, extents->y + src->y,
1920 extents->x, extents->y,
1921 extents->width, extents->height);
1923 cairo_surface_destroy (&src->base);
1925 _cairo_xcb_connection_render_composite (dst->connection,
1926 _render_operator (op),
1932 extents->x, extents->y,
1933 extents->width, extents->height);
1935 cairo_surface_destroy (&mask->base);
1937 return CAIRO_STATUS_SUCCESS;
1940 /* Handles compositing with a clip surface when we have to do the operation
1941 * in two pieces and combine them together.
1943 static cairo_status_t
1944 _clip_and_composite_combine (cairo_clip_t *clip,
1945 cairo_operator_t op,
1946 const cairo_pattern_t *pattern,
1947 xcb_draw_func_t draw_func,
1949 cairo_xcb_surface_t *dst,
1950 const cairo_rectangle_int_t*extents)
1952 cairo_xcb_surface_t *tmp;
1953 cairo_xcb_surface_t *clip_surface;
1955 xcb_render_picture_t clip_picture;
1956 cairo_status_t status;
1958 tmp = (cairo_xcb_surface_t *)
1959 _cairo_xcb_surface_create_similar (dst, dst->base.content,
1960 extents->width, extents->height);
1961 if (unlikely (tmp->base.status))
1962 return tmp->base.status;
1964 /* create_similar() could have done a fallback to an image surface */
1965 assert (tmp->base.backend == &_cairo_xcb_surface_backend);
1967 _cairo_xcb_surface_ensure_picture (tmp);
1969 if (pattern == NULL) {
1970 status = (*draw_func) (draw_closure, tmp,
1971 CAIRO_OPERATOR_ADD, NULL,
1972 extents->x, extents->y,
1975 /* Initialize the temporary surface from the destination surface */
1976 if (! dst->base.is_clear ||
1977 (dst->connection->flags & CAIRO_XCB_RENDER_HAS_FILL_RECTANGLES) == 0)
1979 /* XCopyArea may actually be quicker here.
1980 * A good driver should translate if appropriate.
1982 _cairo_xcb_connection_render_composite (dst->connection,
1983 XCB_RENDER_PICT_OP_SRC,
1987 extents->x, extents->y,
1990 extents->width, extents->height);
1994 xcb_render_color_t clear;
1995 xcb_rectangle_t xrect;
1997 clear.red = clear.green = clear.blue = clear.alpha = 0;
1999 xrect.x = xrect.y = 0;
2000 xrect.width = extents->width;
2001 xrect.height = extents->height;
2003 _cairo_xcb_connection_render_fill_rectangles (dst->connection,
2004 XCB_RENDER_PICT_OP_CLEAR,
2009 status = (*draw_func) (draw_closure, tmp, op, pattern,
2010 extents->x, extents->y,
2013 if (unlikely (status))
2014 goto CLEANUP_SURFACE;
2016 clip_surface = get_clip_surface (clip, dst, &clip_x, &clip_y);
2017 status = clip_surface->base.status;
2018 if (unlikely (status))
2019 goto CLEANUP_SURFACE;
2021 assert (clip_surface->base.backend == &_cairo_xcb_surface_backend);
2022 clip_picture = clip_surface->picture;
2023 assert (clip_picture != XCB_NONE);
2025 if (dst->base.is_clear) {
2026 _cairo_xcb_connection_render_composite (dst->connection,
2027 XCB_RENDER_PICT_OP_SRC,
2028 tmp->picture, clip_picture, dst->picture,
2031 extents->x, extents->y,
2032 extents->width, extents->height);
2034 /* Punch the clip out of the destination */
2035 _cairo_xcb_connection_render_composite (dst->connection,
2036 XCB_RENDER_PICT_OP_OUT_REVERSE,
2037 clip_picture, XCB_NONE, dst->picture,
2038 extents->x - clip_x,
2039 extents->y - clip_y,
2041 extents->x, extents->y,
2042 extents->width, extents->height);
2044 /* Now add the two results together */
2045 _cairo_xcb_connection_render_composite (dst->connection,
2046 XCB_RENDER_PICT_OP_ADD,
2047 tmp->picture, clip_picture, dst->picture,
2049 extents->x - clip_x,
2050 extents->y - clip_y,
2051 extents->x, extents->y,
2052 extents->width, extents->height);
2054 cairo_surface_destroy (&clip_surface->base);
2057 cairo_surface_destroy (&tmp->base);
2062 /* Handles compositing for %CAIRO_OPERATOR_SOURCE, which is special; it's
2063 * defined as (src IN mask IN clip) ADD (dst OUT (mask IN clip))
2065 static cairo_status_t
2066 _clip_and_composite_source (cairo_clip_t *clip,
2067 const cairo_pattern_t *pattern,
2068 xcb_draw_func_t draw_func,
2069 xcb_draw_func_t mask_func,
2071 cairo_xcb_surface_t *dst,
2072 const cairo_rectangle_int_t *extents)
2074 cairo_xcb_surface_t *mask;
2075 cairo_xcb_picture_t *src;
2077 /* Create a surface that is mask IN clip */
2078 mask = _create_composite_mask (clip,
2079 draw_func, mask_func, draw_closure,
2081 if (unlikely (mask->base.status))
2082 return mask->base.status;
2084 src = _cairo_xcb_picture_for_pattern (dst, pattern, extents);
2085 if (unlikely (src->base.status)) {
2086 cairo_surface_destroy (&mask->base);
2087 return src->base.status;
2090 if (dst->base.is_clear) {
2091 _cairo_xcb_connection_render_composite (dst->connection,
2092 XCB_RENDER_PICT_OP_SRC,
2096 extents->x + src->x, extents->y + src->y,
2098 extents->x, extents->y,
2099 extents->width, extents->height);
2101 /* Compute dest' = dest OUT (mask IN clip) */
2102 _cairo_xcb_connection_render_composite (dst->connection,
2103 XCB_RENDER_PICT_OP_OUT_REVERSE,
2108 extents->x, extents->y,
2109 extents->width, extents->height);
2111 /* Now compute (src IN (mask IN clip)) ADD dest' */
2112 _cairo_xcb_connection_render_composite (dst->connection,
2113 XCB_RENDER_PICT_OP_ADD,
2117 extents->x + src->x, extents->y + src->y,
2119 extents->x, extents->y,
2120 extents->width, extents->height);
2123 cairo_surface_destroy (&src->base);
2124 cairo_surface_destroy (&mask->base);
2126 return CAIRO_STATUS_SUCCESS;
2130 can_reduce_alpha_op (cairo_operator_t op)
2134 case CAIRO_OPERATOR_OVER:
2135 case CAIRO_OPERATOR_SOURCE:
2136 case CAIRO_OPERATOR_ADD:
2144 reduce_alpha_op (cairo_surface_t *dst,
2145 cairo_operator_t op,
2146 const cairo_pattern_t *pattern)
2148 return dst->is_clear &&
2149 dst->content == CAIRO_CONTENT_ALPHA &&
2150 _cairo_pattern_is_opaque_solid (pattern) &&
2151 can_reduce_alpha_op (op);
2154 static cairo_status_t
2155 _cairo_xcb_surface_fixup_unbounded (cairo_xcb_surface_t *dst,
2156 const cairo_composite_rectangles_t *rects)
2158 xcb_rectangle_t xrects[4];
2161 if (rects->bounded.width == rects->unbounded.width &&
2162 rects->bounded.height == rects->unbounded.height)
2164 return CAIRO_STATUS_SUCCESS;
2168 if (rects->bounded.width == 0 || rects->bounded.height == 0) {
2169 xrects[n].x = rects->unbounded.x;
2170 xrects[n].width = rects->unbounded.width;
2171 xrects[n].y = rects->unbounded.y;
2172 xrects[n].height = rects->unbounded.height;
2176 if (rects->bounded.y != rects->unbounded.y) {
2177 xrects[n].x = rects->unbounded.x;
2178 xrects[n].width = rects->unbounded.width;
2179 xrects[n].y = rects->unbounded.y;
2180 xrects[n].height = rects->bounded.y - rects->unbounded.y;
2184 if (rects->bounded.x != rects->unbounded.x) {
2185 xrects[n].x = rects->unbounded.x;
2186 xrects[n].width = rects->bounded.x - rects->unbounded.x;
2187 xrects[n].y = rects->bounded.y;
2188 xrects[n].height = rects->bounded.height;
2192 if (rects->bounded.x + rects->bounded.width != rects->unbounded.x + rects->unbounded.width) {
2193 xrects[n].x = rects->bounded.x + rects->bounded.width;
2194 xrects[n].width = rects->unbounded.x + rects->unbounded.width - xrects[n].x;
2195 xrects[n].y = rects->bounded.y;
2196 xrects[n].height = rects->bounded.height;
2200 if (rects->bounded.y + rects->bounded.height != rects->unbounded.y + rects->unbounded.height) {
2201 xrects[n].x = rects->unbounded.x;
2202 xrects[n].width = rects->unbounded.width;
2203 xrects[n].y = rects->bounded.y + rects->bounded.height;
2204 xrects[n].height = rects->unbounded.y + rects->unbounded.height - xrects[n].y;
2209 if (dst->connection->flags & CAIRO_XCB_RENDER_HAS_FILL_RECTANGLES) {
2210 xcb_render_color_t color;
2217 _cairo_xcb_connection_render_fill_rectangles (dst->connection,
2218 XCB_RENDER_PICT_OP_CLEAR,
2223 cairo_xcb_picture_t *src;
2225 src = _cairo_xcb_transparent_picture (dst);
2226 if (unlikely (src->base.status))
2227 return src->base.status;
2229 for (i = 0; i < n; i++) {
2230 _cairo_xcb_connection_render_composite (dst->connection,
2231 XCB_RENDER_PICT_OP_CLEAR,
2232 src->picture, XCB_NONE, dst->picture,
2235 xrects[i].x, xrects[i].y,
2236 xrects[i].width, xrects[i].height);
2238 cairo_surface_destroy (&src->base);
2241 return CAIRO_STATUS_SUCCESS;
2244 static cairo_status_t
2245 _cairo_xcb_surface_fixup_unbounded_with_mask (cairo_xcb_surface_t *dst,
2246 const cairo_composite_rectangles_t *rects,
2249 cairo_xcb_surface_t *mask;
2252 mask = get_clip_surface (clip, dst, &mask_x, &mask_y);
2253 if (unlikely (mask->base.status))
2254 return mask->base.status;
2257 if (rects->bounded.y != rects->unbounded.y) {
2258 int x = rects->unbounded.x;
2259 int y = rects->unbounded.y;
2260 int width = rects->unbounded.width;
2261 int height = rects->bounded.y - y;
2263 _cairo_xcb_connection_render_composite (dst->connection,
2264 XCB_RENDER_PICT_OP_OUT_REVERSE,
2265 mask->picture, XCB_NONE, dst->picture,
2266 x - mask_x, y - mask_y,
2273 if (rects->bounded.x != rects->unbounded.x) {
2274 int x = rects->unbounded.x;
2275 int y = rects->bounded.y;
2276 int width = rects->bounded.x - x;
2277 int height = rects->bounded.height;
2279 _cairo_xcb_connection_render_composite (dst->connection,
2280 XCB_RENDER_PICT_OP_OUT_REVERSE,
2281 mask->picture, XCB_NONE, dst->picture,
2282 x - mask_x, y - mask_y,
2289 if (rects->bounded.x + rects->bounded.width != rects->unbounded.x + rects->unbounded.width) {
2290 int x = rects->bounded.x + rects->bounded.width;
2291 int y = rects->bounded.y;
2292 int width = rects->unbounded.x + rects->unbounded.width - x;
2293 int height = rects->bounded.height;
2295 _cairo_xcb_connection_render_composite (dst->connection,
2296 XCB_RENDER_PICT_OP_OUT_REVERSE,
2297 mask->picture, XCB_NONE, dst->picture,
2298 x - mask_x, y - mask_y,
2305 if (rects->bounded.y + rects->bounded.height != rects->unbounded.y + rects->unbounded.height) {
2306 int x = rects->unbounded.x;
2307 int y = rects->bounded.y + rects->bounded.height;
2308 int width = rects->unbounded.width;
2309 int height = rects->unbounded.y + rects->unbounded.height - y;
2311 _cairo_xcb_connection_render_composite (dst->connection,
2312 XCB_RENDER_PICT_OP_OUT_REVERSE,
2313 mask->picture, XCB_NONE, dst->picture,
2314 x - mask_x, y - mask_y,
2320 cairo_surface_destroy (&mask->base);
2322 return CAIRO_STATUS_SUCCESS;
2325 static cairo_status_t
2326 _cairo_xcb_surface_fixup_unbounded_boxes (cairo_xcb_surface_t *dst,
2327 const cairo_composite_rectangles_t *extents,
2329 cairo_boxes_t *boxes)
2331 cairo_boxes_t clear;
2333 cairo_status_t status;
2334 struct _cairo_boxes_chunk *chunk;
2337 if (boxes->num_boxes <= 1 && clip == NULL)
2338 return _cairo_xcb_surface_fixup_unbounded (dst, extents);
2340 _cairo_boxes_init (&clear);
2342 box.p1.x = _cairo_fixed_from_int (extents->unbounded.x + extents->unbounded.width);
2343 box.p1.y = _cairo_fixed_from_int (extents->unbounded.y);
2344 box.p2.x = _cairo_fixed_from_int (extents->unbounded.x);
2345 box.p2.y = _cairo_fixed_from_int (extents->unbounded.y + extents->unbounded.height);
2350 _cairo_boxes_init (&tmp);
2352 status = _cairo_boxes_add (&tmp, CAIRO_ANTIALIAS_DEFAULT, &box);
2353 assert (status == CAIRO_STATUS_SUCCESS);
2355 tmp.chunks.next = &boxes->chunks;
2356 tmp.num_boxes += boxes->num_boxes;
2358 status = _cairo_bentley_ottmann_tessellate_boxes (&tmp,
2359 CAIRO_FILL_RULE_WINDING,
2362 tmp.chunks.next = NULL;
2364 _cairo_boxes_init_with_clip (&clear, clip);
2366 status = _cairo_boxes_add (&clear, CAIRO_ANTIALIAS_DEFAULT, &box);
2367 assert (status == CAIRO_STATUS_SUCCESS);
2369 for (chunk = &boxes->chunks; chunk != NULL; chunk = chunk->next) {
2370 for (i = 0; i < chunk->count; i++) {
2371 status = _cairo_boxes_add (&clear,
2372 CAIRO_ANTIALIAS_DEFAULT,
2374 if (unlikely (status)) {
2375 _cairo_boxes_fini (&clear);
2381 status = _cairo_bentley_ottmann_tessellate_boxes (&clear,
2382 CAIRO_FILL_RULE_WINDING,
2386 if (likely (status == CAIRO_STATUS_SUCCESS)) {
2387 if (dst->connection->flags & CAIRO_XCB_RENDER_HAS_FILL_RECTANGLES)
2388 status = _render_fill_boxes (dst,
2389 CAIRO_OPERATOR_CLEAR,
2390 CAIRO_COLOR_TRANSPARENT,
2393 status = _cairo_xcb_surface_core_fill_boxes (dst,
2394 CAIRO_COLOR_TRANSPARENT,
2398 _cairo_boxes_fini (&clear);
2404 _cairo_xcb_surface_clear (cairo_xcb_surface_t *dst)
2407 xcb_rectangle_t rect;
2408 cairo_status_t status;
2410 status = _cairo_xcb_connection_acquire (dst->connection);
2411 if (unlikely (status))
2414 rect.x = rect.y = 0;
2415 rect.width = dst->width;
2416 rect.height = dst->height;
2418 if (dst->connection->flags & CAIRO_XCB_RENDER_HAS_FILL_RECTANGLES) {
2419 xcb_render_color_t color;
2422 color.red = dst->deferred_clear_color.red_short;
2423 color.green = dst->deferred_clear_color.green_short;
2424 color.blue = dst->deferred_clear_color.blue_short;
2425 color.alpha = dst->deferred_clear_color.alpha_short;
2427 if (color.alpha == 0)
2428 op = XCB_RENDER_PICT_OP_CLEAR;
2430 op = XCB_RENDER_PICT_OP_SRC;
2432 _cairo_xcb_surface_ensure_picture (dst);
2433 _cairo_xcb_connection_render_fill_rectangles (dst->connection,
2434 op, dst->picture, color,
2437 gc = _cairo_xcb_screen_get_gc (dst->screen, dst->drawable, dst->depth);
2440 _cairo_xcb_connection_poly_fill_rectangle (dst->connection,
2444 _cairo_xcb_screen_put_gc (dst->screen, dst->depth, gc);
2447 _cairo_xcb_connection_release (dst->connection);
2449 dst->deferred_clear = FALSE;
2450 return CAIRO_STATUS_SUCCESS;
2454 NEED_CLIP_REGION = 0x1,
2455 NEED_CLIP_SURFACE = 0x2,
2456 FORCE_CLIP_REGION = 0x4,
2460 need_bounded_clip (cairo_composite_rectangles_t *extents)
2462 unsigned int flags = NEED_CLIP_REGION;
2463 if (! _cairo_clip_is_region (extents->clip))
2464 flags |= NEED_CLIP_SURFACE;
2469 need_unbounded_clip (cairo_composite_rectangles_t *extents)
2471 unsigned int flags = 0;
2472 if (! extents->is_bounded) {
2473 flags |= NEED_CLIP_REGION;
2474 if (! _cairo_clip_is_region (extents->clip))
2475 flags |= NEED_CLIP_SURFACE;
2477 if (extents->clip->path != NULL)
2478 flags |= NEED_CLIP_SURFACE;
2482 static cairo_status_t
2483 _clip_and_composite (cairo_xcb_surface_t *dst,
2484 cairo_operator_t op,
2485 const cairo_pattern_t *src,
2486 xcb_draw_func_t draw_func,
2487 xcb_draw_func_t mask_func,
2489 cairo_composite_rectangles_t*extents,
2490 unsigned int need_clip)
2492 cairo_region_t *clip_region = NULL;
2493 cairo_status_t status;
2495 status = _cairo_xcb_connection_acquire (dst->connection);
2496 if (unlikely (status))
2499 if (dst->deferred_clear) {
2500 status = _cairo_xcb_surface_clear (dst);
2501 if (unlikely (status)) {
2502 _cairo_xcb_connection_release (dst->connection);
2507 _cairo_xcb_surface_ensure_picture (dst);
2509 if (need_clip & NEED_CLIP_REGION) {
2510 clip_region = _cairo_clip_get_region (extents->clip);
2511 if ((need_clip & FORCE_CLIP_REGION) == 0 && clip_region != NULL &&
2512 cairo_region_contains_rectangle (clip_region,
2513 &extents->unbounded) == CAIRO_REGION_OVERLAP_IN)
2515 if (clip_region != NULL) {
2516 status = _cairo_xcb_surface_set_clip_region (dst, clip_region);
2517 if (unlikely (status)) {
2518 _cairo_xcb_connection_release (dst->connection);
2524 if (reduce_alpha_op (&dst->base, op, src)) {
2525 op = CAIRO_OPERATOR_ADD;
2529 if (extents->bounded.width != 0 && extents->bounded.height != 0) {
2530 if (op == CAIRO_OPERATOR_SOURCE) {
2531 status = _clip_and_composite_source (extents->clip, src,
2532 draw_func, mask_func, draw_closure,
2533 dst, &extents->bounded);
2535 if (op == CAIRO_OPERATOR_CLEAR) {
2536 op = CAIRO_OPERATOR_DEST_OUT;
2540 if (need_clip & NEED_CLIP_SURFACE) {
2541 if (extents->is_bounded) {
2542 status = _clip_and_composite_with_mask (extents->clip, op, src,
2546 dst, &extents->bounded);
2548 status = _clip_and_composite_combine (extents->clip, op, src,
2549 draw_func, draw_closure,
2550 dst, &extents->bounded);
2553 status = draw_func (draw_closure,
2562 if (status == CAIRO_STATUS_SUCCESS && ! extents->is_bounded) {
2563 if (need_clip & NEED_CLIP_SURFACE)
2564 status = _cairo_xcb_surface_fixup_unbounded_with_mask (dst, extents, extents->clip);
2566 status = _cairo_xcb_surface_fixup_unbounded (dst, extents);
2570 _cairo_xcb_surface_clear_clip_region (dst);
2572 _cairo_xcb_connection_release (dst->connection);
2577 static cairo_status_t
2578 _core_boxes (cairo_xcb_surface_t *dst,
2579 cairo_operator_t op,
2580 const cairo_pattern_t *src,
2581 cairo_boxes_t *boxes,
2582 const cairo_composite_rectangles_t *extents)
2584 if (! boxes->is_pixel_aligned)
2585 return CAIRO_INT_STATUS_UNSUPPORTED;
2587 if (! _cairo_clip_is_region (extents->clip))
2588 return CAIRO_INT_STATUS_UNSUPPORTED;
2590 if (op == CAIRO_OPERATOR_CLEAR)
2591 return _cairo_xcb_surface_core_fill_boxes (dst, CAIRO_COLOR_TRANSPARENT, boxes);
2593 if (op == CAIRO_OPERATOR_OVER) {
2594 if (dst->base.is_clear || _cairo_pattern_is_opaque (src, &extents->bounded))
2595 op = CAIRO_OPERATOR_SOURCE;
2597 if (op != CAIRO_OPERATOR_SOURCE)
2598 return CAIRO_INT_STATUS_UNSUPPORTED;
2600 if (src->type == CAIRO_PATTERN_TYPE_SOLID) {
2601 return _cairo_xcb_surface_core_fill_boxes (dst,
2602 &((cairo_solid_pattern_t *) src)->color,
2606 return _cairo_xcb_surface_core_copy_boxes (dst, src, &extents->bounded, boxes);
2609 static cairo_status_t
2610 _composite_boxes (cairo_xcb_surface_t *dst,
2611 cairo_operator_t op,
2612 const cairo_pattern_t *src,
2613 cairo_boxes_t *boxes,
2614 const cairo_composite_rectangles_t *extents)
2616 cairo_clip_t *clip = extents->clip;
2617 cairo_bool_t need_clip_mask = ! _cairo_clip_is_region (clip);
2618 cairo_status_t status;
2620 /* If the boxes are not pixel-aligned, we will need to compute a real mask */
2621 if (! boxes->is_pixel_aligned)
2622 return CAIRO_INT_STATUS_UNSUPPORTED;
2624 if (need_clip_mask &&
2625 (! extents->is_bounded || op == CAIRO_OPERATOR_SOURCE))
2627 return CAIRO_INT_STATUS_UNSUPPORTED;
2630 status = _cairo_xcb_connection_acquire (dst->connection);
2631 if (unlikely (status))
2634 _cairo_xcb_surface_ensure_picture (dst);
2635 if (dst->connection->flags & CAIRO_XCB_RENDER_HAS_FILL_RECTANGLES && ! need_clip_mask &&
2636 (op == CAIRO_OPERATOR_CLEAR || src->type == CAIRO_PATTERN_TYPE_SOLID))
2638 const cairo_color_t *color;
2640 if (op == CAIRO_OPERATOR_CLEAR)
2641 color = CAIRO_COLOR_TRANSPARENT;
2643 color = &((cairo_solid_pattern_t *) src)->color;
2645 status = _render_fill_boxes (dst, op, color, boxes);
2649 cairo_surface_pattern_t mask;
2651 if (need_clip_mask) {
2652 cairo_xcb_surface_t *clip_surface;
2655 clip_surface = get_clip_surface (extents->clip, dst,
2657 if (unlikely (clip_surface->base.status))
2658 return clip_surface->base.status;
2660 _cairo_pattern_init_for_surface (&mask, &clip_surface->base);
2661 mask.base.filter = CAIRO_FILTER_NEAREST;
2662 cairo_matrix_init_translate (&mask.base.matrix,
2665 cairo_surface_destroy (&clip_surface->base);
2667 if (op == CAIRO_OPERATOR_CLEAR) {
2669 op = CAIRO_OPERATOR_DEST_OUT;
2673 status = _render_composite_boxes (dst, op, src,
2674 need_clip_mask ? &mask.base : NULL,
2675 &extents->bounded, boxes);
2678 _cairo_pattern_fini (&mask.base);
2681 if (status == CAIRO_STATUS_SUCCESS && ! extents->is_bounded) {
2683 _cairo_xcb_surface_fixup_unbounded_boxes (dst, extents,
2687 _cairo_xcb_connection_release (dst->connection);
2693 cairo_boxes_for_each_box (cairo_boxes_t *boxes,
2694 cairo_bool_t (*func) (cairo_box_t *box,
2698 struct _cairo_boxes_chunk *chunk;
2701 for (chunk = &boxes->chunks; chunk != NULL; chunk = chunk->next) {
2702 for (i = 0; i < chunk->count; i++)
2703 if (! func (&chunk->base[i], data))
2710 struct _image_contains_box {
2715 static cairo_bool_t image_contains_box (cairo_box_t *box, void *closure)
2717 struct _image_contains_box *data = closure;
2719 /* The box is pixel-aligned so the truncation is safe. */
2721 _cairo_fixed_integer_part (box->p1.x) + data->tx >= 0 &&
2722 _cairo_fixed_integer_part (box->p1.y) + data->ty >= 0 &&
2723 _cairo_fixed_integer_part (box->p2.x) + data->tx <= data->width &&
2724 _cairo_fixed_integer_part (box->p2.y) + data->ty <= data->height;
2727 struct _image_upload_box {
2728 cairo_xcb_surface_t *surface;
2729 cairo_image_surface_t *image;
2734 static cairo_bool_t image_upload_box (cairo_box_t *box, void *closure)
2736 const struct _image_upload_box *iub = closure;
2737 /* The box is pixel-aligned so the truncation is safe. */
2738 int x = _cairo_fixed_integer_part (box->p1.x);
2739 int y = _cairo_fixed_integer_part (box->p1.y);
2740 int width = _cairo_fixed_integer_part (box->p2.x - box->p1.x);
2741 int height = _cairo_fixed_integer_part (box->p2.y - box->p1.y);
2742 int bpp = PIXMAN_FORMAT_BPP (iub->image->pixman_format);
2743 int len = CAIRO_STRIDE_FOR_WIDTH_BPP (width, bpp);
2744 if (len == iub->image->stride) {
2745 _cairo_xcb_connection_put_image (iub->surface->connection,
2746 iub->surface->drawable,
2753 (y + iub->ty) * iub->image->stride +
2754 (x + iub->tx) * bpp/8);
2756 _cairo_xcb_connection_put_subimage (iub->surface->connection,
2757 iub->surface->drawable,
2772 static cairo_status_t
2773 _upload_image_inplace (cairo_xcb_surface_t *surface,
2774 const cairo_pattern_t *source,
2775 cairo_boxes_t *boxes)
2777 const cairo_surface_pattern_t *pattern;
2778 struct _image_contains_box icb;
2779 struct _image_upload_box iub;
2780 cairo_image_surface_t *image;
2781 cairo_status_t status;
2784 if (! boxes->is_pixel_aligned)
2785 return CAIRO_INT_STATUS_UNSUPPORTED;
2787 if (source->type != CAIRO_PATTERN_TYPE_SURFACE)
2788 return CAIRO_INT_STATUS_UNSUPPORTED;
2790 pattern = (const cairo_surface_pattern_t *) source;
2791 if (pattern->surface->type != CAIRO_SURFACE_TYPE_IMAGE)
2792 return CAIRO_INT_STATUS_UNSUPPORTED;
2794 /* Have we already upload this image to a pixmap? */
2796 cairo_xcb_picture_t *snapshot;
2798 snapshot = (cairo_xcb_picture_t *)
2799 _cairo_surface_has_snapshot (pattern->surface, &_cairo_xcb_picture_backend);
2800 if (snapshot != NULL) {
2801 if (snapshot->screen == surface->screen)
2802 return CAIRO_INT_STATUS_UNSUPPORTED;
2806 image = (cairo_image_surface_t *) pattern->surface;
2807 if (image->format == CAIRO_FORMAT_INVALID)
2808 return CAIRO_INT_STATUS_UNSUPPORTED;
2810 if (image->depth != surface->depth)
2811 return CAIRO_INT_STATUS_UNSUPPORTED;
2813 if (! _cairo_matrix_is_integer_translation (&source->matrix, &tx, &ty))
2814 return CAIRO_INT_STATUS_UNSUPPORTED;
2816 /* Check that the data is entirely within the image */
2817 icb.width = image->width;
2818 icb.height = image->height;
2821 if (! cairo_boxes_for_each_box (boxes, image_contains_box, &icb))
2822 return CAIRO_INT_STATUS_UNSUPPORTED;
2824 if (surface->deferred_clear) {
2825 status = _cairo_xcb_surface_clear (surface);
2826 if (unlikely (status))
2830 status = _cairo_xcb_connection_acquire (surface->connection);
2831 if (unlikely (status))
2834 iub.surface = surface;
2836 iub.gc = _cairo_xcb_screen_get_gc (surface->screen,
2841 cairo_boxes_for_each_box (boxes, image_upload_box, &iub);
2843 _cairo_xcb_screen_put_gc (surface->screen, image->depth, iub.gc);
2844 _cairo_xcb_connection_release (surface->connection);
2846 return CAIRO_STATUS_SUCCESS;
2849 static cairo_int_status_t
2850 trim_extents_to_traps (cairo_composite_rectangles_t *extents,
2851 cairo_traps_t *traps)
2855 /* X trims the affected area to the extents of the trapezoids, so
2856 * we need to compensate when fixing up the unbounded area.
2858 _cairo_traps_extents (traps, &box);
2859 return _cairo_composite_rectangles_intersect_mask_extents (extents, &box);
2863 _mono_edge_is_vertical (const cairo_line_t *line)
2865 return _cairo_fixed_integer_round_down (line->p1.x) == _cairo_fixed_integer_round_down (line->p2.x);
2869 _traps_are_pixel_aligned (cairo_traps_t *traps,
2870 cairo_antialias_t antialias)
2874 if (antialias == CAIRO_ANTIALIAS_NONE) {
2875 for (i = 0; i < traps->num_traps; i++) {
2876 if (! _mono_edge_is_vertical (&traps->traps[i].left) ||
2877 ! _mono_edge_is_vertical (&traps->traps[i].right))
2879 traps->maybe_region = FALSE;
2884 for (i = 0; i < traps->num_traps; i++) {
2885 if (traps->traps[i].left.p1.x != traps->traps[i].left.p2.x ||
2886 traps->traps[i].right.p1.x != traps->traps[i].right.p2.x ||
2887 ! _cairo_fixed_is_integer (traps->traps[i].top) ||
2888 ! _cairo_fixed_is_integer (traps->traps[i].bottom) ||
2889 ! _cairo_fixed_is_integer (traps->traps[i].left.p1.x) ||
2890 ! _cairo_fixed_is_integer (traps->traps[i].right.p1.x))
2892 traps->maybe_region = FALSE;
2902 _boxes_for_traps (cairo_boxes_t *boxes,
2903 cairo_traps_t *traps,
2904 cairo_antialias_t antialias)
2908 _cairo_boxes_init (boxes);
2910 boxes->num_boxes = traps->num_traps;
2911 boxes->chunks.base = (cairo_box_t *) traps->traps;
2912 boxes->chunks.count = traps->num_traps;
2913 boxes->chunks.size = traps->num_traps;
2915 if (antialias != CAIRO_ANTIALIAS_NONE) {
2916 for (i = 0; i < traps->num_traps; i++) {
2917 /* Note the traps and boxes alias so we need to take the local copies first. */
2918 cairo_fixed_t x1 = traps->traps[i].left.p1.x;
2919 cairo_fixed_t x2 = traps->traps[i].right.p1.x;
2920 cairo_fixed_t y1 = traps->traps[i].top;
2921 cairo_fixed_t y2 = traps->traps[i].bottom;
2923 boxes->chunks.base[i].p1.x = x1;
2924 boxes->chunks.base[i].p1.y = y1;
2925 boxes->chunks.base[i].p2.x = x2;
2926 boxes->chunks.base[i].p2.y = y2;
2928 if (boxes->is_pixel_aligned) {
2929 boxes->is_pixel_aligned =
2930 _cairo_fixed_is_integer (x1) && _cairo_fixed_is_integer (y1) &&
2931 _cairo_fixed_is_integer (x2) && _cairo_fixed_is_integer (y2);
2935 boxes->is_pixel_aligned = TRUE;
2937 for (i = 0; i < traps->num_traps; i++) {
2938 /* Note the traps and boxes alias so we need to take the local copies first. */
2939 cairo_fixed_t x1 = traps->traps[i].left.p1.x;
2940 cairo_fixed_t x2 = traps->traps[i].right.p1.x;
2941 cairo_fixed_t y1 = traps->traps[i].top;
2942 cairo_fixed_t y2 = traps->traps[i].bottom;
2944 /* round down here to match Pixman's behavior when using traps. */
2945 boxes->chunks.base[i].p1.x = _cairo_fixed_round_down (x1);
2946 boxes->chunks.base[i].p1.y = _cairo_fixed_round_down (y1);
2947 boxes->chunks.base[i].p2.x = _cairo_fixed_round_down (x2);
2948 boxes->chunks.base[i].p2.y = _cairo_fixed_round_down (y2);
2953 static cairo_status_t
2954 _composite_polygon (cairo_xcb_surface_t *dst,
2955 cairo_operator_t op,
2956 const cairo_pattern_t *source,
2957 cairo_polygon_t *polygon,
2958 cairo_antialias_t antialias,
2959 cairo_fill_rule_t fill_rule,
2960 cairo_composite_rectangles_t *extents)
2962 composite_traps_info_t traps;
2963 cairo_bool_t clip_surface = ! _cairo_clip_is_region (extents->clip);
2964 cairo_region_t *clip_region = _cairo_clip_get_region (extents->clip);
2965 cairo_status_t status;
2967 if (polygon->num_edges == 0) {
2968 status = CAIRO_STATUS_SUCCESS;
2970 if (! extents->is_bounded) {
2971 if (cairo_region_contains_rectangle (clip_region, &extents->unbounded) == CAIRO_REGION_OVERLAP_IN)
2974 if (clip_surface == FALSE) {
2975 if (clip_region != NULL) {
2976 status = _cairo_xcb_surface_set_clip_region (dst, clip_region);
2977 if (unlikely (status))
2981 status = _cairo_xcb_surface_fixup_unbounded (dst, extents);
2983 if (clip_region != NULL)
2984 _cairo_xcb_surface_clear_clip_region (dst);
2986 status = _cairo_xcb_surface_fixup_unbounded_with_mask (dst,
2995 if (extents->clip->path != NULL && extents->is_bounded) {
2996 cairo_polygon_t clipper;
2997 cairo_fill_rule_t clipper_fill_rule;
2998 cairo_antialias_t clipper_antialias;
3000 status = _cairo_clip_get_polygon (extents->clip,
3003 &clipper_antialias);
3004 if (likely (status == CAIRO_STATUS_SUCCESS)) {
3005 if (clipper_antialias == antialias) {
3006 status = _cairo_polygon_intersect (polygon, fill_rule,
3007 &clipper, clipper_fill_rule);
3008 if (likely (status == CAIRO_STATUS_SUCCESS)) {
3009 cairo_clip_t * clip = _cairo_clip_copy_region (extents->clip);
3010 _cairo_clip_destroy (extents->clip);
3011 extents->clip = clip;
3013 fill_rule = CAIRO_FILL_RULE_WINDING;
3015 _cairo_polygon_fini (&clipper);
3020 _cairo_traps_init (&traps.traps);
3022 status = _cairo_bentley_ottmann_tessellate_polygon (&traps.traps, polygon, fill_rule);
3023 if (unlikely (status))
3026 if (traps.traps.has_intersections) {
3027 if (traps.traps.is_rectangular)
3028 status = _cairo_bentley_ottmann_tessellate_rectangular_traps (&traps.traps, CAIRO_FILL_RULE_WINDING);
3029 else if (traps.traps.is_rectilinear)
3030 status = _cairo_bentley_ottmann_tessellate_rectilinear_traps (&traps.traps, CAIRO_FILL_RULE_WINDING);
3032 status = _cairo_bentley_ottmann_tessellate_traps (&traps.traps, CAIRO_FILL_RULE_WINDING);
3033 if (unlikely (status))
3037 /* Use a fast path if the trapezoids consist of a simple region,
3038 * but we can only do this if we do not have a clip surface, or can
3039 * substitute the mask with the clip.
3041 if (traps.traps.maybe_region &&
3042 _traps_are_pixel_aligned (&traps.traps, antialias) &&
3044 (extents->is_bounded && op != CAIRO_OPERATOR_SOURCE)))
3046 cairo_boxes_t boxes;
3048 _boxes_for_traps (&boxes, &traps.traps, antialias);
3049 status = _clip_and_composite_boxes (dst, op, source, &boxes, extents);
3053 /* Otherwise render the trapezoids to a mask and composite in the usual
3056 traps.antialias = antialias;
3057 status = trim_extents_to_traps (extents, &traps.traps);
3058 if (likely (status == CAIRO_STATUS_SUCCESS)) {
3059 unsigned int flags = 0;
3061 /* For unbounded operations, the X11 server will estimate the
3062 * affected rectangle and apply the operation to that. However,
3063 * there are cases where this is an overestimate (e.g. the
3064 * clip-fill-{eo,nz}-unbounded test).
3066 * The clip will trim that overestimate to our expectations.
3068 if (! extents->is_bounded)
3069 flags |= FORCE_CLIP_REGION;
3071 status = _clip_and_composite (dst, op, source, _composite_traps,
3072 NULL, &traps, extents,
3073 need_unbounded_clip (extents) | flags);
3078 _cairo_traps_fini (&traps.traps);
3083 static cairo_status_t
3084 _clip_and_composite_boxes (cairo_xcb_surface_t *dst,
3085 cairo_operator_t op,
3086 const cairo_pattern_t *src,
3087 cairo_boxes_t *boxes,
3088 cairo_composite_rectangles_t *extents)
3090 composite_traps_info_t info;
3091 cairo_int_status_t status;
3093 if (boxes->num_boxes == 0 && extents->is_bounded)
3094 return CAIRO_STATUS_SUCCESS;
3096 if (boxes->is_pixel_aligned && _cairo_clip_is_region (extents->clip) &&
3097 (op == CAIRO_OPERATOR_SOURCE ||
3098 (dst->base.is_clear && (op == CAIRO_OPERATOR_OVER || op == CAIRO_OPERATOR_ADD))))
3100 if (boxes->num_boxes == 1 &&
3101 extents->bounded.width == dst->width &&
3102 extents->bounded.height == dst->height)
3104 op = CAIRO_OPERATOR_SOURCE;
3105 dst->deferred_clear = FALSE;
3108 status = _upload_image_inplace (dst, src, boxes);
3109 if (status != CAIRO_INT_STATUS_UNSUPPORTED)
3113 /* Can we reduce drawing through a clip-mask to simply drawing the clip? */
3114 if (dst->connection->flags & CAIRO_XCB_RENDER_HAS_COMPOSITE_TRAPEZOIDS &&
3115 extents->clip->path != NULL && extents->is_bounded) {
3116 cairo_polygon_t polygon;
3117 cairo_fill_rule_t fill_rule;
3118 cairo_antialias_t antialias;
3121 clip = _cairo_clip_copy (extents->clip);
3122 clip = _cairo_clip_intersect_boxes (clip, boxes);
3123 status = _cairo_clip_get_polygon (clip, &polygon,
3124 &fill_rule, &antialias);
3125 _cairo_clip_path_destroy (clip->path);
3127 if (likely (status == CAIRO_INT_STATUS_SUCCESS)) {
3128 cairo_clip_t *saved_clip = extents->clip;
3129 extents->clip = clip;
3130 status = _composite_polygon (dst, op, src,
3135 if (extents->clip != clip)
3137 extents->clip = saved_clip;
3138 _cairo_polygon_fini (&polygon);
3141 _cairo_clip_destroy (clip);
3143 if (status != CAIRO_INT_STATUS_UNSUPPORTED)
3147 if (dst->deferred_clear) {
3148 status = _cairo_xcb_surface_clear (dst);
3149 if (unlikely (status))
3153 if (boxes->is_pixel_aligned &&
3154 _cairo_clip_is_region (extents->clip) &&
3155 op == CAIRO_OPERATOR_SOURCE) {
3156 status = _upload_image_inplace (dst, src, boxes);
3157 if (status != CAIRO_INT_STATUS_UNSUPPORTED)
3161 if ((dst->connection->flags & CAIRO_XCB_RENDER_HAS_COMPOSITE) == 0)
3162 return _core_boxes (dst, op, src, boxes, extents);
3164 /* Use a fast path if the boxes are pixel aligned */
3165 status = _composite_boxes (dst, op, src, boxes, extents);
3166 if (status != CAIRO_INT_STATUS_UNSUPPORTED)
3169 if ((dst->connection->flags & CAIRO_XCB_RENDER_HAS_COMPOSITE_TRAPEZOIDS) == 0)
3170 return CAIRO_INT_STATUS_UNSUPPORTED;
3172 /* Otherwise render via a mask and composite in the usual fashion. */
3173 status = _cairo_traps_init_boxes (&info.traps, boxes);
3174 if (unlikely (status))
3177 info.antialias = CAIRO_ANTIALIAS_DEFAULT;
3178 status = trim_extents_to_traps (extents, &info.traps);
3179 if (status == CAIRO_INT_STATUS_SUCCESS) {
3180 status = _clip_and_composite (dst, op, src,
3181 _composite_traps, NULL, &info,
3182 extents, need_unbounded_clip (extents));
3185 _cairo_traps_fini (&info.traps);
3189 static cairo_int_status_t
3190 _composite_mask (void *closure,
3191 cairo_xcb_surface_t *dst,
3192 cairo_operator_t op,
3193 const cairo_pattern_t *src_pattern,
3196 const cairo_rectangle_int_t *extents,
3199 const cairo_pattern_t *mask_pattern = closure;
3200 cairo_xcb_picture_t *src, *mask = NULL;
3201 cairo_status_t status;
3203 if (dst->base.is_clear) {
3204 if (op == CAIRO_OPERATOR_OVER || op == CAIRO_OPERATOR_ADD)
3205 op = CAIRO_OPERATOR_SOURCE;
3208 if (op == CAIRO_OPERATOR_SOURCE && clip == NULL)
3209 dst->deferred_clear = FALSE;
3211 if (dst->deferred_clear) {
3212 status = _cairo_xcb_surface_clear (dst);
3213 if (unlikely (status))
3217 if (src_pattern != NULL) {
3218 src = _cairo_xcb_picture_for_pattern (dst, src_pattern, extents);
3219 if (unlikely (src->base.status))
3220 return src->base.status;
3222 mask = _cairo_xcb_picture_for_pattern (dst, mask_pattern, extents);
3223 if (unlikely (mask->base.status)) {
3224 cairo_surface_destroy (&src->base);
3225 return mask->base.status;
3228 _cairo_xcb_connection_render_composite (dst->connection,
3229 _render_operator (op),
3233 extents->x + src->x, extents->y + src->y,
3234 extents->x + mask->x, extents->y + mask->y,
3235 extents->x - dst_x, extents->y - dst_y,
3236 extents->width, extents->height);
3237 cairo_surface_destroy (&mask->base);
3238 cairo_surface_destroy (&src->base);
3240 src = _cairo_xcb_picture_for_pattern (dst, mask_pattern, extents);
3241 if (unlikely (src->base.status))
3242 return src->base.status;
3244 _cairo_xcb_connection_render_composite (dst->connection,
3245 _render_operator (op),
3249 extents->x + src->x, extents->y + src->y,
3251 extents->x - dst_x, extents->y - dst_y,
3252 extents->width, extents->height);
3253 cairo_surface_destroy (&src->base);
3256 return CAIRO_STATUS_SUCCESS;
3259 struct composite_box_info {
3260 cairo_xcb_surface_t *dst;
3261 cairo_xcb_picture_t *src;
3265 static void composite_box(void *closure,
3266 int16_t x, int16_t y,
3267 int16_t w, int16_t h,
3270 struct composite_box_info *info = closure;
3272 if (coverage < 0xff00) {
3273 cairo_xcb_picture_t *mask;
3274 cairo_color_t color;
3276 color.red_short = color.green_short = color.blue_short = 0;
3277 color.alpha_short = coverage;
3279 mask = _solid_picture (info->dst, &color);
3280 if (likely (mask->base.status == CAIRO_STATUS_SUCCESS)) {
3281 _cairo_xcb_connection_render_composite (info->dst->connection,
3286 x + info->src->x, y + info->src->y,
3291 cairo_surface_destroy (&mask->base);
3293 _cairo_xcb_connection_render_composite (info->dst->connection,
3298 x + info->src->x, y + info->src->y,
3305 static cairo_int_status_t
3306 _composite_mask_clip_boxes (void *closure,
3307 cairo_xcb_surface_t *dst,
3308 cairo_operator_t op,
3309 const cairo_pattern_t *src_pattern,
3312 const cairo_rectangle_int_t *extents,
3315 struct composite_box_info info;
3316 cairo_status_t status;
3319 assert (src_pattern == NULL);
3320 assert (op == CAIRO_OPERATOR_ADD);
3321 assert (dst->base.is_clear);
3323 if (clip->num_boxes > 1) {
3324 status = _cairo_xcb_surface_clear (dst);
3325 if (unlikely (status))
3329 info.op = XCB_RENDER_PICT_OP_SRC;
3331 info.src = _cairo_xcb_picture_for_pattern (dst, closure, extents);
3332 if (unlikely (info.src->base.status))
3333 return info.src->base.status;
3335 info.src->x += dst_x;
3336 info.src->y += dst_y;
3338 for (i = 0; i < clip->num_boxes; i++)
3339 do_unaligned_box(composite_box, &info, &clip->boxes[i], dst_x, dst_y);
3340 cairo_surface_destroy (&info.src->base);
3342 return CAIRO_STATUS_SUCCESS;
3345 static cairo_int_status_t
3346 _composite_mask_clip (void *closure,
3347 cairo_xcb_surface_t *dst,
3348 cairo_operator_t op,
3349 const cairo_pattern_t *src_pattern,
3352 const cairo_rectangle_int_t *extents,
3355 const cairo_pattern_t *mask_pattern = closure;
3356 cairo_polygon_t polygon;
3357 cairo_fill_rule_t fill_rule;
3358 composite_traps_info_t info;
3359 cairo_status_t status;
3361 assert (src_pattern == NULL);
3362 assert (op == CAIRO_OPERATOR_ADD);
3363 assert (dst->base.is_clear);
3365 status = _cairo_clip_get_polygon (clip, &polygon,
3366 &fill_rule, &info.antialias);
3367 if (unlikely (status))
3370 _cairo_traps_init (&info.traps);
3371 status = _cairo_bentley_ottmann_tessellate_polygon (&info.traps,
3374 _cairo_polygon_fini (&polygon);
3375 if (unlikely (status))
3378 if (info.traps.has_intersections) {
3379 if (info.traps.is_rectangular)
3380 status = _cairo_bentley_ottmann_tessellate_rectangular_traps (&info.traps, CAIRO_FILL_RULE_WINDING);
3381 else if (info.traps.is_rectilinear)
3382 status = _cairo_bentley_ottmann_tessellate_rectilinear_traps (&info.traps, CAIRO_FILL_RULE_WINDING);
3384 status = _cairo_bentley_ottmann_tessellate_traps (&info.traps, CAIRO_FILL_RULE_WINDING);
3385 if (unlikely (status)) {
3386 _cairo_traps_fini (&info.traps);
3391 dst->deferred_clear = FALSE; /* assert(trap extents == extents); */
3393 status = _composite_traps (&info,
3394 dst, CAIRO_OPERATOR_SOURCE, mask_pattern,
3397 _cairo_traps_fini (&info.traps);
3402 struct composite_opacity_info {
3404 cairo_xcb_surface_t *dst;
3405 cairo_xcb_picture_t *src;
3409 static void composite_opacity(void *closure,
3410 int16_t x, int16_t y,
3411 int16_t w, int16_t h,
3414 struct composite_opacity_info *info = closure;
3415 cairo_xcb_picture_t *mask;
3416 cairo_color_t color;
3418 color.red_short = color.green_short = color.blue_short = 0;
3419 color.alpha_short = info->opacity * coverage;
3421 mask = _solid_picture (info->dst, &color);
3422 if (likely (mask->base.status == CAIRO_STATUS_SUCCESS)) {
3424 _cairo_xcb_connection_render_composite (info->dst->connection,
3429 x + info->src->x, y + info->src->y,
3434 _cairo_xcb_connection_render_composite (info->dst->connection,
3446 cairo_surface_destroy (&mask->base);
3449 static cairo_int_status_t
3450 _composite_opacity_boxes (void *closure,
3451 cairo_xcb_surface_t *dst,
3452 cairo_operator_t op,
3453 const cairo_pattern_t *src_pattern,
3456 const cairo_rectangle_int_t *extents,
3459 const cairo_solid_pattern_t *mask_pattern = closure;
3460 struct composite_opacity_info info;
3461 cairo_status_t status;
3464 if (dst->base.is_clear) {
3465 if (op == CAIRO_OPERATOR_OVER || op == CAIRO_OPERATOR_ADD)
3466 op = CAIRO_OPERATOR_SOURCE;
3469 if (op == CAIRO_OPERATOR_SOURCE &&
3471 (clip->extents.width >= extents->width &&
3472 clip->extents.height >= extents->height)))
3473 dst->deferred_clear = FALSE;
3475 if (dst->deferred_clear) {
3476 status = _cairo_xcb_surface_clear (dst);
3477 if (unlikely (status))
3481 info.op = _render_operator (op);
3484 if (src_pattern != NULL) {
3485 info.src = _cairo_xcb_picture_for_pattern (dst, src_pattern, extents);
3486 if (unlikely (info.src->base.status))
3487 return info.src->base.status;
3491 info.opacity = mask_pattern->color.alpha;
3493 /* XXX for lots of boxes create a clip region for the fully opaque areas */
3495 for (i = 0; i < clip->num_boxes; i++)
3496 do_unaligned_box(composite_opacity, &info,
3497 &clip->boxes[i], dst_x, dst_y);
3499 composite_opacity(&info,
3506 cairo_surface_destroy (&info.src->base);
3508 return CAIRO_STATUS_SUCCESS;
3511 /* high level rasteriser -> compositor */
3514 _cairo_xcb_render_compositor_paint (const cairo_compositor_t *compositor,
3515 cairo_composite_rectangles_t *composite)
3517 cairo_xcb_surface_t *surface = (cairo_xcb_surface_t *) composite->surface;
3518 cairo_operator_t op = composite->op;
3519 cairo_pattern_t *source = &composite->source_pattern.base;
3520 cairo_boxes_t boxes;
3521 cairo_status_t status;
3523 if (unlikely (! _operator_is_supported (surface->connection->flags, op)))
3524 return CAIRO_INT_STATUS_UNSUPPORTED;
3526 if ((surface->connection->flags & (CAIRO_XCB_RENDER_HAS_COMPOSITE_TRAPEZOIDS |
3527 CAIRO_XCB_RENDER_HAS_COMPOSITE)) == 0)
3529 return CAIRO_INT_STATUS_UNSUPPORTED;
3532 if (composite->clip == NULL &&
3533 source->type == CAIRO_PATTERN_TYPE_SOLID &&
3534 (op == CAIRO_OPERATOR_SOURCE ||
3535 op == CAIRO_OPERATOR_CLEAR ||
3536 (surface->base.is_clear &&
3537 (op == CAIRO_OPERATOR_ADD || op == CAIRO_OPERATOR_OVER))))
3539 surface->deferred_clear = TRUE;
3540 surface->deferred_clear_color = composite->source_pattern.solid.color;
3541 return CAIRO_STATUS_SUCCESS;
3544 _cairo_clip_steal_boxes(composite->clip, &boxes);
3545 status = _clip_and_composite_boxes (surface, op, source, &boxes, composite);
3546 _cairo_clip_unsteal_boxes (composite->clip, &boxes);
3552 _cairo_xcb_render_compositor_mask (const cairo_compositor_t *compositor,
3553 cairo_composite_rectangles_t *composite)
3555 cairo_xcb_surface_t *surface = (cairo_xcb_surface_t *) composite->surface;
3556 cairo_operator_t op = composite->op;
3557 cairo_pattern_t *source = &composite->source_pattern.base;
3558 cairo_pattern_t *mask = &composite->mask_pattern.base;
3559 cairo_status_t status;
3561 if (unlikely (! _operator_is_supported (surface->connection->flags, op)))
3562 return CAIRO_INT_STATUS_UNSUPPORTED;
3564 if ((surface->connection->flags & CAIRO_XCB_RENDER_HAS_COMPOSITE) == 0)
3565 return CAIRO_INT_STATUS_UNSUPPORTED;
3567 if (mask->type == CAIRO_PATTERN_TYPE_SOLID &&
3568 composite->clip->path == NULL &&
3569 ! _cairo_clip_is_region (composite->clip)) {
3570 status = _clip_and_composite (surface, op, source,
3571 _composite_opacity_boxes,
3572 _composite_opacity_boxes,
3574 composite, need_unbounded_clip (composite));
3576 xcb_draw_func_t mask_func = NULL;
3577 if (surface->connection->flags & CAIRO_XCB_RENDER_HAS_COMPOSITE_TRAPEZOIDS)
3578 mask_func = composite->clip->path ? _composite_mask_clip : _composite_mask_clip_boxes;
3579 status = _clip_and_composite (surface, op, source,
3580 _composite_mask, mask_func,
3582 composite, need_bounded_clip (composite));
3588 static cairo_int_status_t
3589 _cairo_xcb_surface_render_stroke_as_polygon (cairo_xcb_surface_t *dst,
3590 cairo_operator_t op,
3591 const cairo_pattern_t *source,
3592 const cairo_path_fixed_t *path,
3593 const cairo_stroke_style_t *stroke_style,
3594 const cairo_matrix_t *ctm,
3595 const cairo_matrix_t *ctm_inverse,
3597 cairo_antialias_t antialias,
3598 cairo_composite_rectangles_t *extents)
3600 cairo_polygon_t polygon;
3601 cairo_status_t status;
3603 _cairo_polygon_init_with_clip (&polygon, extents->clip);
3604 status = _cairo_path_fixed_stroke_to_polygon (path,
3609 if (likely (status == CAIRO_STATUS_SUCCESS)) {
3610 status = _composite_polygon (dst, op, source,
3611 &polygon, antialias,
3612 CAIRO_FILL_RULE_WINDING,
3615 _cairo_polygon_fini (&polygon);
3620 static cairo_status_t
3621 _cairo_xcb_surface_render_stroke_via_mask (cairo_xcb_surface_t *dst,
3622 cairo_operator_t op,
3623 const cairo_pattern_t *source,
3624 const cairo_path_fixed_t *path,
3625 const cairo_stroke_style_t *stroke_style,
3626 const cairo_matrix_t *ctm,
3627 const cairo_matrix_t *ctm_inverse,
3629 cairo_antialias_t antialias,
3630 cairo_composite_rectangles_t *extents)
3632 cairo_surface_t *image;
3633 cairo_status_t status;
3637 x = extents->bounded.x;
3638 y = extents->bounded.y;
3639 image = _cairo_xcb_surface_create_similar_image (dst, CAIRO_FORMAT_A8,
3640 extents->bounded.width,
3641 extents->bounded.height);
3642 if (unlikely (image->status))
3643 return image->status;
3645 clip = _cairo_clip_copy_region (extents->clip);
3646 status = _cairo_surface_offset_stroke (image, x, y,
3648 &_cairo_pattern_white.base,
3651 tolerance, antialias,
3653 _cairo_clip_destroy (clip);
3654 if (likely (status == CAIRO_STATUS_SUCCESS)) {
3655 cairo_surface_pattern_t mask;
3657 _cairo_pattern_init_for_surface (&mask, image);
3658 mask.base.filter = CAIRO_FILTER_NEAREST;
3660 cairo_matrix_init_translate (&mask.base.matrix, -x, -y);
3661 status = _clip_and_composite (dst, op, source,
3662 _composite_mask, NULL, &mask.base,
3663 extents, need_bounded_clip (extents));
3664 _cairo_pattern_fini (&mask.base);
3667 cairo_surface_finish (image);
3668 cairo_surface_destroy (image);
3674 _cairo_xcb_render_compositor_stroke (const cairo_compositor_t *compositor,
3675 cairo_composite_rectangles_t *composite,
3676 const cairo_path_fixed_t *path,
3677 const cairo_stroke_style_t *style,
3678 const cairo_matrix_t *ctm,
3679 const cairo_matrix_t *ctm_inverse,
3681 cairo_antialias_t antialias)
3683 cairo_xcb_surface_t *surface = (cairo_xcb_surface_t *) composite->surface;
3684 cairo_operator_t op = composite->op;
3685 cairo_pattern_t *source = &composite->source_pattern.base;
3686 cairo_int_status_t status;
3688 if (unlikely (! _operator_is_supported (surface->connection->flags, op)))
3689 return CAIRO_INT_STATUS_UNSUPPORTED;
3691 if ((surface->connection->flags & (CAIRO_XCB_RENDER_HAS_COMPOSITE_TRAPEZOIDS |
3692 CAIRO_XCB_RENDER_HAS_COMPOSITE)) == 0)
3694 return CAIRO_INT_STATUS_UNSUPPORTED;
3697 status = CAIRO_INT_STATUS_UNSUPPORTED;
3698 if (_cairo_path_fixed_stroke_is_rectilinear (path)) {
3699 cairo_boxes_t boxes;
3701 _cairo_boxes_init_with_clip (&boxes, composite->clip);
3702 status = _cairo_path_fixed_stroke_rectilinear_to_boxes (path,
3707 if (likely (status == CAIRO_INT_STATUS_SUCCESS)) {
3708 status = _clip_and_composite_boxes (surface, op, source,
3711 _cairo_boxes_fini (&boxes);
3714 if (status == CAIRO_INT_STATUS_UNSUPPORTED) {
3715 if (surface->connection->flags & CAIRO_XCB_RENDER_HAS_COMPOSITE_TRAPEZOIDS) {
3716 status = _cairo_xcb_surface_render_stroke_as_polygon (surface, op, source,
3719 tolerance, antialias,
3721 } else if (surface->connection->flags & CAIRO_XCB_RENDER_HAS_COMPOSITE) {
3722 status = _cairo_xcb_surface_render_stroke_via_mask (surface, op, source,
3725 tolerance, antialias,
3735 static cairo_status_t
3736 _cairo_xcb_surface_render_fill_as_polygon (cairo_xcb_surface_t *dst,
3737 cairo_operator_t op,
3738 const cairo_pattern_t*source,
3739 const cairo_path_fixed_t *path,
3740 cairo_fill_rule_t fill_rule,
3742 cairo_antialias_t antialias,
3743 cairo_composite_rectangles_t *extents)
3745 cairo_polygon_t polygon;
3746 cairo_status_t status;
3748 _cairo_polygon_init_with_clip (&polygon, extents->clip);
3749 status = _cairo_path_fixed_fill_to_polygon (path, tolerance, &polygon);
3750 if (likely (status == CAIRO_STATUS_SUCCESS)) {
3751 status = _composite_polygon (dst, op, source,
3757 _cairo_polygon_fini (&polygon);
3762 static cairo_status_t
3763 _cairo_xcb_surface_render_fill_via_mask (cairo_xcb_surface_t *dst,
3764 cairo_operator_t op,
3765 const cairo_pattern_t *source,
3766 const cairo_path_fixed_t *path,
3767 cairo_fill_rule_t fill_rule,
3769 cairo_antialias_t antialias,
3770 cairo_composite_rectangles_t *extents)
3772 cairo_surface_t *image;
3773 cairo_status_t status;
3777 x = extents->bounded.x;
3778 y = extents->bounded.y;
3779 image = _cairo_xcb_surface_create_similar_image (dst, CAIRO_FORMAT_A8,
3780 extents->bounded.width,
3781 extents->bounded.height);
3782 if (unlikely (image->status))
3783 return image->status;
3785 clip = _cairo_clip_copy_region (extents->clip);
3786 status = _cairo_surface_offset_fill (image, x, y,
3788 &_cairo_pattern_white.base,
3789 path, fill_rule, tolerance, antialias,
3791 _cairo_clip_destroy (clip);
3792 if (likely (status == CAIRO_STATUS_SUCCESS)) {
3793 cairo_surface_pattern_t mask;
3795 _cairo_pattern_init_for_surface (&mask, image);
3796 mask.base.filter = CAIRO_FILTER_NEAREST;
3798 cairo_matrix_init_translate (&mask.base.matrix, -x, -y);
3799 status = _clip_and_composite (dst, op, source,
3800 _composite_mask, NULL, &mask.base,
3801 extents, need_bounded_clip (extents));
3803 _cairo_pattern_fini (&mask.base);
3806 cairo_surface_finish (image);
3807 cairo_surface_destroy (image);
3813 _cairo_xcb_render_compositor_fill (const cairo_compositor_t *compositor,
3814 cairo_composite_rectangles_t *composite,
3815 const cairo_path_fixed_t *path,
3816 cairo_fill_rule_t fill_rule,
3818 cairo_antialias_t antialias)
3820 cairo_xcb_surface_t *surface = (cairo_xcb_surface_t *) composite->surface;
3821 cairo_operator_t op = composite->op;
3822 cairo_pattern_t *source = &composite->source_pattern.base;
3823 cairo_int_status_t status;
3825 if (unlikely (! _operator_is_supported (surface->connection->flags, op)))
3826 return CAIRO_INT_STATUS_UNSUPPORTED;
3828 if ((surface->connection->flags & (CAIRO_XCB_RENDER_HAS_COMPOSITE_TRAPEZOIDS |
3829 CAIRO_XCB_RENDER_HAS_COMPOSITE)) == 0)
3831 return CAIRO_INT_STATUS_UNSUPPORTED;
3834 status = CAIRO_INT_STATUS_UNSUPPORTED;
3835 if (_cairo_path_fixed_fill_is_rectilinear (path)) {
3836 cairo_boxes_t boxes;
3838 _cairo_boxes_init_with_clip (&boxes, composite->clip);
3839 status = _cairo_path_fixed_fill_rectilinear_to_boxes (path,
3843 if (likely (status == CAIRO_INT_STATUS_SUCCESS)) {
3844 status = _clip_and_composite_boxes (surface, op, source,
3847 _cairo_boxes_fini (&boxes);
3850 if (status == CAIRO_INT_STATUS_UNSUPPORTED) {
3851 if (surface->connection->flags & CAIRO_XCB_RENDER_HAS_COMPOSITE_TRAPEZOIDS) {
3852 status = _cairo_xcb_surface_render_fill_as_polygon (surface, op, source, path,
3853 fill_rule, tolerance, antialias,
3855 } else if (surface->connection->flags & CAIRO_XCB_RENDER_HAS_COMPOSITE) {
3856 status = _cairo_xcb_surface_render_fill_via_mask (surface, op, source, path,
3857 fill_rule, tolerance, antialias,
3867 static cairo_status_t
3868 _cairo_xcb_surface_render_glyphs_via_mask (cairo_xcb_surface_t *dst,
3869 cairo_operator_t op,
3870 const cairo_pattern_t *source,
3871 cairo_scaled_font_t *scaled_font,
3872 cairo_glyph_t *glyphs,
3874 cairo_composite_rectangles_t *extents)
3876 cairo_surface_t *image;
3877 cairo_content_t content;
3878 cairo_status_t status;
3882 content = CAIRO_CONTENT_ALPHA;
3883 if (scaled_font->options.antialias == CAIRO_ANTIALIAS_SUBPIXEL)
3884 content = CAIRO_CONTENT_COLOR_ALPHA;
3886 x = extents->bounded.x;
3887 y = extents->bounded.y;
3888 image = _cairo_xcb_surface_create_similar_image (dst,
3889 _cairo_format_from_content (content),
3890 extents->bounded.width,
3891 extents->bounded.height);
3892 if (unlikely (image->status))
3893 return image->status;
3895 clip = _cairo_clip_copy_region (extents->clip);
3896 status = _cairo_surface_offset_glyphs (image, x, y,
3898 &_cairo_pattern_white.base,
3899 scaled_font, glyphs, num_glyphs,
3901 _cairo_clip_destroy (clip);
3902 if (likely (status == CAIRO_STATUS_SUCCESS)) {
3903 cairo_surface_pattern_t mask;
3905 _cairo_pattern_init_for_surface (&mask, image);
3906 mask.base.filter = CAIRO_FILTER_NEAREST;
3907 if (content & CAIRO_CONTENT_COLOR)
3908 mask.base.has_component_alpha = TRUE;
3910 cairo_matrix_init_translate (&mask.base.matrix, -x, -y);
3911 status = _clip_and_composite (dst, op, source,
3912 _composite_mask, NULL, &mask.base,
3913 extents, need_bounded_clip (extents));
3915 _cairo_pattern_fini (&mask.base);
3918 cairo_surface_finish (image);
3919 cairo_surface_destroy (image);
3924 /* Build a struct of the same size of #cairo_glyph_t that can be used both as
3925 * an input glyph with double coordinates, and as "working" glyph with
3926 * integer from-current-point offsets. */
3929 unsigned long index;
3931 unsigned long index;
3935 } cairo_xcb_glyph_t;
3937 /* compile-time assert that #cairo_xcb_glyph_t is the same size as #cairo_glyph_t */
3938 COMPILE_TIME_ASSERT (sizeof (cairo_xcb_glyph_t) == sizeof (cairo_glyph_t));
3941 cairo_scaled_font_t *font;
3942 cairo_xcb_glyph_t *glyphs;
3944 cairo_bool_t use_mask;
3945 } composite_glyphs_info_t;
3947 static cairo_status_t
3948 _can_composite_glyphs (cairo_xcb_surface_t *dst,
3949 cairo_rectangle_int_t *extents,
3950 cairo_scaled_font_t *scaled_font,
3951 cairo_glyph_t *glyphs,
3954 #define GLYPH_CACHE_SIZE 64
3955 cairo_box_t bbox_cache[GLYPH_CACHE_SIZE];
3956 unsigned long glyph_cache[GLYPH_CACHE_SIZE];
3957 #undef GLYPH_CACHE_SIZE
3958 cairo_status_t status = CAIRO_STATUS_SUCCESS;
3959 cairo_glyph_t *glyphs_end, *valid_glyphs;
3960 const int max_glyph_size = dst->connection->maximum_request_length - 64;
3962 /* We must initialize the cache with values that cannot match the
3963 * "hash" to guarantee that when compared for the first time they
3964 * will result in a mismatch. The hash function is simply modulus,
3965 * so we cannot use 0 in glyph_cache[0], but we can use it in all
3966 * other array cells.
3968 memset (glyph_cache, 0, sizeof (glyph_cache));
3971 /* Scan for oversized glyphs or glyphs outside the representable
3972 * range and fallback in that case, discard glyphs outside of the
3975 valid_glyphs = glyphs;
3976 for (glyphs_end = glyphs + *num_glyphs; glyphs != glyphs_end; glyphs++) {
3977 double x1, y1, x2, y2;
3978 cairo_scaled_glyph_t *glyph;
3980 int width, height, len;
3983 g = glyphs->index % ARRAY_LENGTH (glyph_cache);
3984 if (glyph_cache[g] != glyphs->index) {
3985 status = _cairo_scaled_glyph_lookup (scaled_font,
3987 CAIRO_SCALED_GLYPH_INFO_METRICS,
3989 if (unlikely (status))
3992 glyph_cache[g] = glyphs->index;
3993 bbox_cache[g] = glyph->bbox;
3995 bbox = &bbox_cache[g];
3997 /* Drop glyphs outside the clipping */
3998 x1 = _cairo_fixed_to_double (bbox->p1.x);
3999 y1 = _cairo_fixed_to_double (bbox->p1.y);
4000 y2 = _cairo_fixed_to_double (bbox->p2.y);
4001 x2 = _cairo_fixed_to_double (bbox->p2.x);
4002 if (unlikely (glyphs->x + x2 <= extents->x ||
4003 glyphs->y + y2 <= extents->y ||
4004 glyphs->x + x1 >= extents->x + extents->width ||
4005 glyphs->y + y1 >= extents->y + extents->height))
4011 /* XRenderAddGlyph does not handle a glyph surface larger than
4012 * the extended maximum XRequest size.
4014 width = _cairo_fixed_integer_ceil (bbox->p2.x - bbox->p1.x);
4015 height = _cairo_fixed_integer_ceil (bbox->p2.y - bbox->p1.y);
4016 len = CAIRO_STRIDE_FOR_WIDTH_BPP (width, 32) * height;
4017 if (unlikely (len >= max_glyph_size)) {
4018 status = CAIRO_INT_STATUS_UNSUPPORTED;
4022 /* The glyph coordinates must be representable in an int16_t.
4023 * When possible, they will be expressed as an offset from the
4024 * previous glyph, otherwise they will be an offset from the
4025 * operation extents or from the surface origin. If the last
4026 * two options are not valid, fallback.
4028 if (unlikely (glyphs->x > INT16_MAX ||
4029 glyphs->y > INT16_MAX ||
4030 glyphs->x - extents->x < INT16_MIN ||
4031 glyphs->y - extents->y < INT16_MIN))
4033 status = CAIRO_INT_STATUS_UNSUPPORTED;
4038 if (unlikely (valid_glyphs != glyphs))
4039 *valid_glyphs = *glyphs;
4043 if (unlikely (valid_glyphs != glyphs)) {
4044 for (; glyphs != glyphs_end; glyphs++) {
4045 *valid_glyphs = *glyphs;
4053 /* Start a new element for the first glyph,
4054 * or for any glyph that has unexpected position,
4055 * or if current element has too many glyphs
4056 * (Xrender limits each element to 252 glyphs, we limit them to 128)
4058 * These same conditions need to be mirrored between
4059 * _cairo_xcb_surface_emit_glyphs and _emit_glyph_chunks
4061 #define _start_new_glyph_elt(count, glyph) \
4062 (((count) & 127) == 0 || (glyph)->i.x || (glyph)->i.y)
4064 /* sz_xGlyphtElt required alignment to a 32-bit boundary, so ensure we have
4065 * enough room for padding */
4073 #define _cairo_sz_x_glyph_elt_t (sizeof (x_glyph_elt_t) + 4)
4076 _cairo_xcb_font_destroy (cairo_xcb_font_t *font)
4080 for (i = 0; i < NUM_GLYPHSETS; i++) {
4081 cairo_xcb_font_glyphset_info_t *info;
4083 info = &font->glyphset_info[i];
4084 free (info->pending_free_glyphs);
4087 cairo_list_del (&font->base.link);
4088 cairo_list_del (&font->link);
4090 _cairo_xcb_connection_destroy (font->connection);
4096 _cairo_xcb_font_fini (cairo_scaled_font_private_t *abstract_private,
4097 cairo_scaled_font_t *scaled_font)
4099 cairo_xcb_font_t *font_private = (cairo_xcb_font_t *)abstract_private;
4100 cairo_xcb_connection_t *connection;
4101 cairo_bool_t have_connection;
4102 cairo_status_t status;
4105 connection = font_private->connection;
4107 status = _cairo_xcb_connection_acquire (connection);
4108 have_connection = status == CAIRO_STATUS_SUCCESS;
4110 for (i = 0; i < NUM_GLYPHSETS; i++) {
4111 cairo_xcb_font_glyphset_info_t *info;
4113 info = &font_private->glyphset_info[i];
4114 if (info->glyphset && status == CAIRO_STATUS_SUCCESS) {
4115 _cairo_xcb_connection_render_free_glyph_set (connection,
4120 if (have_connection)
4121 _cairo_xcb_connection_release (connection);
4123 _cairo_xcb_font_destroy (font_private);
4127 static cairo_xcb_font_t *
4128 _cairo_xcb_font_create (cairo_xcb_connection_t *connection,
4129 cairo_scaled_font_t *font)
4131 cairo_xcb_font_t *priv;
4134 priv = malloc (sizeof (cairo_xcb_font_t));
4135 if (unlikely (priv == NULL))
4138 _cairo_scaled_font_attach_private (font, &priv->base, connection,
4139 _cairo_xcb_font_fini);
4141 priv->scaled_font = font;
4142 priv->connection = _cairo_xcb_connection_reference (connection);
4143 cairo_list_add (&priv->link, &connection->fonts);
4145 for (i = 0; i < NUM_GLYPHSETS; i++) {
4146 cairo_xcb_font_glyphset_info_t *info = &priv->glyphset_info[i];
4148 case GLYPHSET_INDEX_ARGB32: info->format = CAIRO_FORMAT_ARGB32; break;
4149 case GLYPHSET_INDEX_A8: info->format = CAIRO_FORMAT_A8; break;
4150 case GLYPHSET_INDEX_A1: info->format = CAIRO_FORMAT_A1; break;
4151 default: ASSERT_NOT_REACHED; break;
4153 info->xrender_format = 0;
4154 info->glyphset = XCB_NONE;
4155 info->pending_free_glyphs = NULL;
4162 _cairo_xcb_font_close (cairo_xcb_font_t *font)
4164 cairo_scaled_font_t *scaled_font;
4166 scaled_font = font->scaled_font;
4168 CAIRO_MUTEX_LOCK (scaled_font->mutex);
4169 //scaled_font->surface_private = NULL;
4170 _cairo_scaled_font_reset_cache (scaled_font);
4171 CAIRO_MUTEX_UNLOCK (scaled_font->mutex);
4173 _cairo_xcb_font_destroy (font);
4177 _cairo_xcb_render_free_glyphs (cairo_xcb_connection_t *connection,
4178 cairo_xcb_font_glyphset_free_glyphs_t *to_free)
4180 _cairo_xcb_connection_render_free_glyphs (connection,
4182 to_free->glyph_count,
4183 to_free->glyph_indices);
4187 _cairo_xcb_get_glyphset_index_for_format (cairo_format_t format)
4189 if (format == CAIRO_FORMAT_A8)
4190 return GLYPHSET_INDEX_A8;
4191 if (format == CAIRO_FORMAT_A1)
4192 return GLYPHSET_INDEX_A1;
4194 assert (format == CAIRO_FORMAT_ARGB32);
4195 return GLYPHSET_INDEX_ARGB32;
4200 static inline cairo_xcb_font_t *
4201 _cairo_xcb_font_get (const cairo_xcb_connection_t *c,
4202 cairo_scaled_font_t *font)
4204 return (cairo_xcb_font_t *)_cairo_scaled_font_find_private (font, c);
4208 static cairo_xcb_font_glyphset_info_t *
4209 _cairo_xcb_scaled_font_get_glyphset_info_for_format (cairo_xcb_connection_t *c,
4210 cairo_scaled_font_t *font,
4211 cairo_format_t format)
4213 cairo_xcb_font_t *priv;
4214 cairo_xcb_font_glyphset_info_t *info;
4217 glyphset_index = _cairo_xcb_get_glyphset_index_for_format (format);
4219 priv = _cairo_xcb_font_get (c, font);
4221 priv = _cairo_xcb_font_create (c, font);
4226 info = &priv->glyphset_info[glyphset_index];
4227 if (info->glyphset == XCB_NONE) {
4228 info->glyphset = _cairo_xcb_connection_get_xid (c);
4229 info->xrender_format = c->standard_formats[info->format];
4231 _cairo_xcb_connection_render_create_glyph_set (c,
4233 info->xrender_format);
4240 _cairo_xcb_glyphset_info_has_pending_free_glyph (
4241 cairo_xcb_font_glyphset_info_t *info,
4242 unsigned long glyph_index)
4244 if (info->pending_free_glyphs != NULL) {
4245 cairo_xcb_font_glyphset_free_glyphs_t *to_free;
4248 to_free = info->pending_free_glyphs;
4249 for (i = 0; i < to_free->glyph_count; i++) {
4250 if (to_free->glyph_indices[i] == glyph_index) {
4251 to_free->glyph_count--;
4252 memmove (&to_free->glyph_indices[i],
4253 &to_free->glyph_indices[i+1],
4254 (to_free->glyph_count - i) * sizeof (to_free->glyph_indices[0]));
4264 cairo_scaled_glyph_private_t base;
4266 cairo_xcb_font_glyphset_info_t *glyphset;
4267 } cairo_xcb_glyph_private_t;
4269 static cairo_xcb_font_glyphset_info_t *
4270 _cairo_xcb_scaled_font_get_glyphset_info_for_pending_free_glyph (cairo_xcb_connection_t *c,
4271 cairo_scaled_font_t *font,
4272 unsigned long glyph_index,
4273 cairo_image_surface_t *surface)
4275 cairo_xcb_font_t *priv;
4278 priv = _cairo_xcb_font_get (c, font);
4282 if (surface != NULL) {
4283 i = _cairo_xcb_get_glyphset_index_for_format (surface->format);
4285 if (_cairo_xcb_glyphset_info_has_pending_free_glyph (
4286 &priv->glyphset_info[i],
4289 return &priv->glyphset_info[i];
4292 for (i = 0; i < NUM_GLYPHSETS; i++) {
4293 if (_cairo_xcb_glyphset_info_has_pending_free_glyph (
4294 &priv->glyphset_info[i],
4297 return &priv->glyphset_info[i];
4306 _cairo_xcb_glyph_fini (cairo_scaled_glyph_private_t *glyph_private,
4307 cairo_scaled_glyph_t *glyph,
4308 cairo_scaled_font_t *font)
4310 cairo_xcb_glyph_private_t *priv = (cairo_xcb_glyph_private_t *)glyph_private;
4312 if (! font->finished) {
4313 cairo_xcb_font_glyphset_info_t *info = priv->glyphset;
4314 cairo_xcb_font_glyphset_free_glyphs_t *to_free;
4315 cairo_xcb_font_t *font_private;
4317 font_private = _cairo_xcb_font_get (glyph_private->key, font);
4318 assert (font_private);
4320 to_free = info->pending_free_glyphs;
4321 if (to_free != NULL &&
4322 to_free->glyph_count == ARRAY_LENGTH (to_free->glyph_indices))
4324 _cairo_xcb_render_free_glyphs (font_private->connection, to_free);
4325 to_free = info->pending_free_glyphs = NULL;
4328 if (to_free == NULL) {
4329 to_free = malloc (sizeof (cairo_xcb_font_glyphset_free_glyphs_t));
4330 if (unlikely (to_free == NULL)) {
4331 _cairo_error_throw (CAIRO_STATUS_NO_MEMORY);
4332 return; /* XXX cannot propagate failure */
4335 to_free->glyphset = info->glyphset;
4336 to_free->glyph_count = 0;
4337 info->pending_free_glyphs = to_free;
4340 to_free->glyph_indices[to_free->glyph_count++] =
4341 _cairo_scaled_glyph_index (glyph);
4344 cairo_list_del (&glyph_private->link);
4345 free (glyph_private);
4349 static cairo_status_t
4350 _cairo_xcb_glyph_attach (cairo_xcb_connection_t *c,
4351 cairo_scaled_glyph_t *glyph,
4352 cairo_xcb_font_glyphset_info_t *info)
4354 cairo_xcb_glyph_private_t *priv;
4356 priv = malloc (sizeof (*priv));
4357 if (unlikely (priv == NULL))
4358 return _cairo_error (CAIRO_STATUS_NO_MEMORY);
4360 _cairo_scaled_glyph_attach_private (glyph, &priv->base, c,
4361 _cairo_xcb_glyph_fini);
4362 priv->glyphset = info;
4364 glyph->dev_private = info;
4365 glyph->dev_private_key = c;
4366 return CAIRO_STATUS_SUCCESS;
4369 static cairo_status_t
4370 _cairo_xcb_surface_add_glyph (cairo_xcb_connection_t *connection,
4371 cairo_scaled_font_t *font,
4372 cairo_scaled_glyph_t **scaled_glyph_out)
4374 xcb_render_glyphinfo_t glyph_info;
4375 uint32_t glyph_index;
4377 cairo_status_t status = CAIRO_STATUS_SUCCESS;
4378 cairo_scaled_glyph_t *scaled_glyph = *scaled_glyph_out;
4379 cairo_image_surface_t *glyph_surface = scaled_glyph->surface;
4380 cairo_bool_t already_had_glyph_surface;
4381 cairo_xcb_font_glyphset_info_t *info;
4383 glyph_index = _cairo_scaled_glyph_index (scaled_glyph);
4385 /* check to see if we have a pending XRenderFreeGlyph for this glyph */
4386 info = _cairo_xcb_scaled_font_get_glyphset_info_for_pending_free_glyph (connection, font, glyph_index, glyph_surface);
4388 return _cairo_xcb_glyph_attach (connection, scaled_glyph, info);
4390 if (glyph_surface == NULL) {
4391 status = _cairo_scaled_glyph_lookup (font,
4393 CAIRO_SCALED_GLYPH_INFO_METRICS |
4394 CAIRO_SCALED_GLYPH_INFO_SURFACE,
4396 if (unlikely (status))
4399 scaled_glyph = *scaled_glyph_out;
4400 glyph_surface = scaled_glyph->surface;
4401 already_had_glyph_surface = FALSE;
4403 already_had_glyph_surface = TRUE;
4406 info = _cairo_xcb_scaled_font_get_glyphset_info_for_format (connection,
4408 glyph_surface->format);
4409 if (unlikely (info == NULL)) {
4410 status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
4415 /* If the glyph surface has zero height or width, we create
4416 * a clear 1x1 surface, to avoid various X server bugs.
4418 if (glyph_surface->width == 0 || glyph_surface->height == 0) {
4419 cairo_surface_t *tmp_surface;
4421 tmp_surface = cairo_image_surface_create (info->format, 1, 1);
4422 status = tmp_surface->status;
4423 if (unlikely (status))
4426 tmp_surface->device_transform = glyph_surface->base.device_transform;
4427 tmp_surface->device_transform_inverse = glyph_surface->base.device_transform_inverse;
4429 glyph_surface = (cairo_image_surface_t *) tmp_surface;
4433 /* If the glyph format does not match the font format, then we
4434 * create a temporary surface for the glyph image with the font's
4437 if (glyph_surface->format != info->format) {
4438 glyph_surface = _cairo_image_surface_coerce_to_format (glyph_surface,
4440 status = glyph_surface->base.status;
4441 if (unlikely (status))
4445 /* XXX: FRAGILE: We're ignore device_transform scaling here. A bug? */
4446 glyph_info.x = _cairo_lround (glyph_surface->base.device_transform.x0);
4447 glyph_info.y = _cairo_lround (glyph_surface->base.device_transform.y0);
4448 glyph_info.width = glyph_surface->width;
4449 glyph_info.height = glyph_surface->height;
4450 glyph_info.x_off = scaled_glyph->x_advance;
4451 glyph_info.y_off = scaled_glyph->y_advance;
4453 data = glyph_surface->data;
4455 /* flip formats around */
4456 switch (_cairo_xcb_get_glyphset_index_for_format (scaled_glyph->surface->format)) {
4457 case GLYPHSET_INDEX_A1:
4458 /* local bitmaps are always stored with bit == byte */
4459 if (_cairo_is_little_endian() != (connection->root->bitmap_format_bit_order == XCB_IMAGE_ORDER_LSB_FIRST)) {
4460 int c = glyph_surface->stride * glyph_surface->height;
4465 if (unlikely (new == NULL)) {
4466 status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
4474 b = ((b << 1) & 0xaa) | ((b >> 1) & 0x55);
4475 b = ((b << 2) & 0xcc) | ((b >> 2) & 0x33);
4476 b = ((b << 4) & 0xf0) | ((b >> 4) & 0x0f);
4483 case GLYPHSET_INDEX_A8:
4486 case GLYPHSET_INDEX_ARGB32:
4487 if (_cairo_is_little_endian() != (connection->root->image_byte_order == XCB_IMAGE_ORDER_LSB_FIRST)) {
4488 unsigned int c = glyph_surface->stride * glyph_surface->height / 4;
4492 new = malloc (4 * c);
4493 if (unlikely (new == NULL)) {
4494 status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
4499 d = (uint32_t *) data;
4501 *n++ = bswap_32 (*d);
4504 data = (uint8_t *) new;
4512 /* XXX assume X server wants pixman padding. Xft assumes this as well */
4514 _cairo_xcb_connection_render_add_glyphs (connection,
4516 1, &glyph_index, &glyph_info,
4517 glyph_surface->stride * glyph_surface->height,
4520 if (data != glyph_surface->data)
4523 status = _cairo_xcb_glyph_attach (connection, scaled_glyph, info);
4526 if (glyph_surface != scaled_glyph->surface)
4527 cairo_surface_destroy (&glyph_surface->base);
4529 /* If the scaled glyph didn't already have a surface attached
4530 * to it, release the created surface now that we have it
4531 * uploaded to the X server. If the surface has already been
4532 * there (e.g. because image backend requested it), leave it in
4535 if (! already_had_glyph_surface)
4536 _cairo_scaled_glyph_set_surface (scaled_glyph, font, NULL);
4541 typedef void (*cairo_xcb_render_composite_text_func_t)
4542 (cairo_xcb_connection_t *connection,
4544 xcb_render_picture_t src,
4545 xcb_render_picture_t dst,
4546 xcb_render_pictformat_t mask_format,
4547 xcb_render_glyphset_t glyphset,
4554 static cairo_status_t
4555 _emit_glyphs_chunk (cairo_xcb_surface_t *dst,
4556 cairo_operator_t op,
4557 cairo_xcb_picture_t *src,
4558 /* info for this chunk */
4559 cairo_xcb_glyph_t *glyphs,
4562 int estimated_req_size,
4563 cairo_xcb_font_glyphset_info_t *info,
4564 xcb_render_pictformat_t mask_format)
4566 cairo_xcb_render_composite_text_func_t composite_text_func;
4567 uint8_t stack_buf[CAIRO_STACK_BUFFER_SIZE];
4568 uint8_t *buf = stack_buf;
4569 x_glyph_elt_t *elt = NULL; /* silence compiler */
4573 if (estimated_req_size > ARRAY_LENGTH (stack_buf)) {
4574 buf = malloc (estimated_req_size);
4575 if (unlikely (buf == NULL))
4576 return _cairo_error (CAIRO_STATUS_NO_MEMORY);
4580 for (i = 0; i < num_glyphs; i++) {
4581 if (_start_new_glyph_elt (i, &glyphs[i])) {
4583 len += 4 - (len & 3);
4585 elt = (x_glyph_elt_t *) (buf + len);
4587 elt->deltax = glyphs[i].i.x;
4588 elt->deltay = glyphs[i].i.y;
4589 len += sizeof (x_glyph_elt_t);
4593 case 1: *(uint8_t *) (buf + len) = glyphs[i].index; break;
4594 case 2: *(uint16_t *) (buf + len) = glyphs[i].index; break;
4596 case 4: *(uint32_t *) (buf + len) = glyphs[i].index; break;
4602 len += 4 - (len & 3);
4606 composite_text_func = _cairo_xcb_connection_render_composite_glyphs_8;
4609 composite_text_func = _cairo_xcb_connection_render_composite_glyphs_16;
4613 composite_text_func = _cairo_xcb_connection_render_composite_glyphs_32;
4616 composite_text_func (dst->connection,
4617 _render_operator (op),
4622 src->x + glyphs[0].i.x,
4623 src->y + glyphs[0].i.y,
4626 if (buf != stack_buf)
4629 return CAIRO_STATUS_SUCCESS;
4632 static cairo_int_status_t
4633 _composite_glyphs (void *closure,
4634 cairo_xcb_surface_t *dst,
4635 cairo_operator_t op,
4636 const cairo_pattern_t *pattern,
4639 const cairo_rectangle_int_t *extents,
4642 composite_glyphs_info_t *info = closure;
4643 cairo_scaled_glyph_t *glyph_cache[64];
4644 cairo_status_t status = CAIRO_STATUS_SUCCESS;
4645 cairo_fixed_t x = 0, y = 0;
4646 cairo_xcb_font_glyphset_info_t *glyphset_info = NULL, *this_glyphset_info;
4647 const unsigned int max_request_size = dst->connection->maximum_request_length - 64;
4648 cairo_xcb_picture_t *src;
4650 unsigned long max_index = 0;
4653 unsigned int request_size = 0;
4656 if (dst->deferred_clear) {
4657 status = _cairo_xcb_surface_clear (dst);
4658 if (unlikely (status))
4662 src = _cairo_xcb_picture_for_pattern (dst, pattern, extents);
4663 if (unlikely (src->base.status))
4664 return src->base.status;
4666 memset (glyph_cache, 0, sizeof (glyph_cache));
4668 for (i = 0; i < info->num_glyphs; i++) {
4669 cairo_scaled_glyph_t *glyph;
4670 unsigned long glyph_index = info->glyphs[i].index;
4671 int cache_index = glyph_index % ARRAY_LENGTH (glyph_cache);
4672 int old_width = width;
4675 glyph = glyph_cache[cache_index];
4676 if (glyph == NULL ||
4677 _cairo_scaled_glyph_index (glyph) != glyph_index)
4679 status = _cairo_scaled_glyph_lookup (info->font,
4681 CAIRO_SCALED_GLYPH_INFO_METRICS,
4683 if (unlikely (status)) {
4684 cairo_surface_destroy (&src->base);
4688 /* Send unseen glyphs to the server */
4689 if (glyph->dev_private_key != dst->connection) {
4690 status = _cairo_xcb_surface_add_glyph (dst->connection,
4693 if (unlikely (status)) {
4694 cairo_surface_destroy (&src->base);
4699 glyph_cache[cache_index] = glyph;
4702 this_x = _cairo_lround (info->glyphs[i].d.x) - dst_x;
4703 this_y = _cairo_lround (info->glyphs[i].d.y) - dst_y;
4705 this_glyphset_info = glyph->dev_private;
4706 if (glyphset_info == NULL)
4707 glyphset_info = this_glyphset_info;
4709 /* Update max glyph index */
4710 if (glyph_index > max_index) {
4711 max_index = glyph_index;
4712 if (max_index >= 65536)
4714 else if (max_index >= 256)
4716 if (width != old_width)
4717 request_size += (width - old_width) * i;
4720 /* If we will pass the max request size by adding this glyph,
4721 * flush current glyphs. Note that we account for a
4722 * possible element being added below.
4724 * Also flush if changing glyphsets, as Xrender limits one mask
4725 * format per request, so we can either break up, or use a
4726 * wide-enough mask format. We do the former. One reason to
4727 * prefer the latter is the fact that Xserver ADDs all glyphs
4728 * to the mask first, and then composes that to final surface,
4729 * though it's not a big deal.
4731 * If the glyph has a coordinate which cannot be represented
4732 * as a 16-bit offset from the previous glyph, flush the
4733 * current chunk. The current glyph will be the first one in
4734 * the next chunk, thus its coordinates will be an offset from
4735 * the destination origin. This offset is guaranteed to be
4736 * representable as 16-bit offset in _can_composite_glyphs().
4738 if (request_size + width > max_request_size - _cairo_sz_x_glyph_elt_t ||
4739 this_x - x > INT16_MAX || this_x - x < INT16_MIN ||
4740 this_y - y > INT16_MAX || this_y - y < INT16_MIN ||
4741 this_glyphset_info != glyphset_info)
4743 status = _emit_glyphs_chunk (dst, op, src,
4745 old_width, request_size,
4747 info->use_mask ? glyphset_info->xrender_format : 0);
4748 if (unlikely (status)) {
4749 cairo_surface_destroy (&src->base);
4754 info->num_glyphs -= i;
4757 max_index = info->glyphs[0].index;
4758 width = max_index < 256 ? 1 : max_index < 65536 ? 2 : 4;
4763 glyphset_info = this_glyphset_info;
4766 /* Convert absolute glyph position to relative-to-current-point
4768 info->glyphs[i].i.x = this_x - x;
4769 info->glyphs[i].i.y = this_y - y;
4771 /* Start a new element for the first glyph,
4772 * or for any glyph that has unexpected position,
4773 * or if current element has too many glyphs.
4775 * These same conditions are mirrored in _emit_glyphs_chunk().
4777 if (_start_new_glyph_elt (i, &info->glyphs[i]))
4778 request_size += _cairo_sz_x_glyph_elt_t;
4780 /* adjust current-position */
4781 x = this_x + glyph->x_advance;
4782 y = this_y + glyph->y_advance;
4784 request_size += width;
4788 status = _emit_glyphs_chunk (dst, op, src,
4790 width, request_size,
4792 info->use_mask ? glyphset_info->xrender_format : 0);
4795 cairo_surface_destroy (&src->base);
4801 _cairo_xcb_render_compositor_glyphs (const cairo_compositor_t *compositor,
4802 cairo_composite_rectangles_t *composite,
4803 cairo_scaled_font_t *scaled_font,
4804 cairo_glyph_t *glyphs,
4806 cairo_bool_t overlap)
4808 cairo_xcb_surface_t *surface = (cairo_xcb_surface_t *) composite->surface;
4809 cairo_operator_t op = composite->op;
4810 cairo_pattern_t *source = &composite->source_pattern.base;
4811 cairo_int_status_t status;
4813 if (unlikely (! _operator_is_supported (surface->connection->flags, op)))
4814 return CAIRO_INT_STATUS_UNSUPPORTED;
4816 if ((surface->connection->flags & (CAIRO_XCB_RENDER_HAS_COMPOSITE_GLYPHS | CAIRO_XCB_RENDER_HAS_COMPOSITE)) == 0)
4817 return CAIRO_INT_STATUS_UNSUPPORTED;
4819 status = CAIRO_INT_STATUS_UNSUPPORTED;
4820 if (surface->connection->flags & CAIRO_XCB_RENDER_HAS_COMPOSITE_GLYPHS) {
4821 _cairo_scaled_font_freeze_cache (scaled_font);
4823 status = _can_composite_glyphs (surface, &composite->bounded,
4824 scaled_font, glyphs, &num_glyphs);
4825 if (likely (status == CAIRO_INT_STATUS_SUCCESS)) {
4826 composite_glyphs_info_t info;
4829 info.font = scaled_font;
4830 info.glyphs = (cairo_xcb_glyph_t *) glyphs;
4831 info.num_glyphs = num_glyphs;
4834 ! composite->is_bounded ||
4835 ! _cairo_clip_is_region(composite->clip);
4837 if (composite->mask.width > composite->unbounded.width ||
4838 composite->mask.height > composite->unbounded.height)
4840 /* Glyphs are tricky since we do not directly control the
4841 * geometry and their inked extents depend on the
4842 * individual glyph-surface size. We must set a clip region
4843 * so that the X server can trim the glyphs appropriately.
4845 flags |= FORCE_CLIP_REGION;
4847 status = _clip_and_composite (surface, op, source,
4848 _composite_glyphs, NULL,
4850 need_bounded_clip (composite) |
4854 _cairo_scaled_font_thaw_cache (scaled_font);
4857 if (status == CAIRO_INT_STATUS_UNSUPPORTED) {
4858 assert (surface->connection->flags & CAIRO_XCB_RENDER_HAS_COMPOSITE);
4860 _cairo_xcb_surface_render_glyphs_via_mask (surface, op, source,
4861 scaled_font, glyphs, num_glyphs,