1 /* -*- Mode: c; tab-width: 8; c-basic-offset: 4; indent-tabs-mode: t; -*- */
3 * Copyright © 2002 Keith Packard
4 * Copyright © 2007 Red Hat, Inc.
6 * This library is free software; you can redistribute it and/or
7 * modify it either under the terms of the GNU Lesser General Public
8 * License version 2.1 as published by the Free Software Foundation
9 * (the "LGPL") or, at your option, under the terms of the Mozilla
10 * Public License Version 1.1 (the "MPL"). If you do not alter this
11 * notice, a recipient may use your version of this file under either
12 * the MPL or the LGPL.
14 * You should have received a copy of the LGPL along with this library
15 * in the file COPYING-LGPL-2.1; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA
17 * You should have received a copy of the MPL along with this library
18 * in the file COPYING-MPL-1.1
20 * The contents of this file are subject to the Mozilla Public License
21 * Version 1.1 (the "License"); you may not use this file except in
22 * compliance with the License. You may obtain a copy of the License at
23 * http://www.mozilla.org/MPL/
25 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY
26 * OF ANY KIND, either express or implied. See the LGPL or the MPL for
27 * the specific language governing rights and limitations.
29 * The Original Code is the cairo graphics library.
31 * The Initial Developer of the Original Code is Keith Packard
34 * Keith R. Packard <keithp@keithp.com>
35 * Carl D. Worth <cworth@cworth.org>
37 * 2002-07-15: Converted from XRenderCompositeDoublePoly to #cairo_trap_t. Carl D. Worth
42 #include "cairo-boxes-private.h"
43 #include "cairo-error-private.h"
44 #include "cairo-region-private.h"
45 #include "cairo-slope-private.h"
46 #include "cairo-traps-private.h"
48 /* private functions */
51 _cairo_traps_init (cairo_traps_t *traps)
53 VG (VALGRIND_MAKE_MEM_UNDEFINED (traps, sizeof (cairo_traps_t)));
55 traps->status = CAIRO_STATUS_SUCCESS;
57 traps->maybe_region = 1;
58 traps->is_rectilinear = 0;
59 traps->is_rectangular = 0;
63 traps->traps_size = ARRAY_LENGTH (traps->traps_embedded);
64 traps->traps = traps->traps_embedded;
66 traps->num_limits = 0;
67 traps->has_intersections = FALSE;
71 _cairo_traps_limit (cairo_traps_t *traps,
72 const cairo_box_t *limits,
75 traps->limits = limits;
76 traps->num_limits = num_limits;
80 _cairo_traps_init_with_clip (cairo_traps_t *traps,
81 const cairo_clip_t *clip)
83 _cairo_traps_init (traps);
85 _cairo_traps_limit (traps, clip->boxes, clip->num_boxes);
89 _cairo_traps_clear (cairo_traps_t *traps)
91 traps->status = CAIRO_STATUS_SUCCESS;
93 traps->maybe_region = 1;
94 traps->is_rectilinear = 0;
95 traps->is_rectangular = 0;
98 traps->has_intersections = FALSE;
102 _cairo_traps_fini (cairo_traps_t *traps)
104 if (traps->traps != traps->traps_embedded)
107 VG (VALGRIND_MAKE_MEM_NOACCESS (traps, sizeof (cairo_traps_t)));
110 /* make room for at least one more trap */
112 _cairo_traps_grow (cairo_traps_t *traps)
114 cairo_trapezoid_t *new_traps;
115 int new_size = 4 * traps->traps_size;
117 if (CAIRO_INJECT_FAULT ()) {
118 traps->status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
122 if (traps->traps == traps->traps_embedded) {
123 new_traps = _cairo_malloc_ab (new_size, sizeof (cairo_trapezoid_t));
124 if (new_traps != NULL)
125 memcpy (new_traps, traps->traps, sizeof (traps->traps_embedded));
127 new_traps = _cairo_realloc_ab (traps->traps,
128 new_size, sizeof (cairo_trapezoid_t));
131 if (unlikely (new_traps == NULL)) {
132 traps->status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
136 traps->traps = new_traps;
137 traps->traps_size = new_size;
142 _cairo_traps_add_trap (cairo_traps_t *traps,
143 cairo_fixed_t top, cairo_fixed_t bottom,
144 cairo_line_t *left, cairo_line_t *right)
146 cairo_trapezoid_t *trap;
148 if (unlikely (traps->num_traps == traps->traps_size)) {
149 if (unlikely (! _cairo_traps_grow (traps)))
153 trap = &traps->traps[traps->num_traps++];
155 trap->bottom = bottom;
157 trap->right = *right;
161 * _cairo_traps_init_box:
162 * @traps: a #cairo_traps_t
163 * @box: an array box that will each be converted to a single trapezoid
164 * to store in @traps.
166 * Initializes a #cairo_traps_t to contain an array of rectangular
170 _cairo_traps_init_boxes (cairo_traps_t *traps,
171 const cairo_boxes_t *boxes)
173 cairo_trapezoid_t *trap;
174 const struct _cairo_boxes_chunk *chunk;
176 _cairo_traps_init (traps);
178 while (traps->traps_size < boxes->num_boxes) {
179 if (unlikely (! _cairo_traps_grow (traps))) {
180 _cairo_traps_fini (traps);
181 return _cairo_error (CAIRO_STATUS_NO_MEMORY);
185 traps->num_traps = boxes->num_boxes;
186 traps->is_rectilinear = TRUE;
187 traps->is_rectangular = TRUE;
188 traps->maybe_region = boxes->is_pixel_aligned;
190 trap = &traps->traps[0];
191 for (chunk = &boxes->chunks; chunk != NULL; chunk = chunk->next) {
192 const cairo_box_t *box;
196 for (i = 0; i < chunk->count; i++) {
197 trap->top = box->p1.y;
198 trap->bottom = box->p2.y;
200 trap->left.p1 = box->p1;
201 trap->left.p2.x = box->p1.x;
202 trap->left.p2.y = box->p2.y;
204 trap->right.p1.x = box->p2.x;
205 trap->right.p1.y = box->p1.y;
206 trap->right.p2 = box->p2;
212 return CAIRO_STATUS_SUCCESS;
216 _cairo_traps_tessellate_rectangle (cairo_traps_t *traps,
217 const cairo_point_t *top_left,
218 const cairo_point_t *bottom_right)
222 cairo_fixed_t top, bottom;
224 if (top_left->y == bottom_right->y)
225 return CAIRO_STATUS_SUCCESS;
227 if (top_left->x == bottom_right->x)
228 return CAIRO_STATUS_SUCCESS;
230 left.p1.x = left.p2.x = top_left->x;
231 left.p1.y = right.p1.y = top_left->y;
232 right.p1.x = right.p2.x = bottom_right->x;
233 left.p2.y = right.p2.y = bottom_right->y;
236 bottom = bottom_right->y;
238 if (traps->num_limits) {
239 cairo_bool_t reversed;
242 /* support counter-clockwise winding for rectangular tessellation */
243 reversed = top_left->x > bottom_right->x;
245 right.p1.x = right.p2.x = top_left->x;
246 left.p1.x = left.p2.x = bottom_right->x;
249 for (n = 0; n < traps->num_limits; n++) {
250 const cairo_box_t *limits = &traps->limits[n];
251 cairo_line_t _left, _right;
252 cairo_fixed_t _top, _bottom;
254 if (top >= limits->p2.y)
256 if (bottom <= limits->p1.y)
259 /* Trivially reject if trapezoid is entirely to the right or
260 * to the left of the limits. */
261 if (left.p1.x >= limits->p2.x)
263 if (right.p1.x <= limits->p1.x)
266 /* Otherwise, clip the trapezoid to the limits. */
268 if (_top < limits->p1.y)
272 if (_bottom > limits->p2.y)
273 _bottom = limits->p2.y;
279 if (_left.p1.x < limits->p1.x) {
280 _left.p1.x = limits->p1.x;
281 _left.p1.y = limits->p1.y;
282 _left.p2.x = limits->p1.x;
283 _left.p2.y = limits->p2.y;
287 if (_right.p1.x > limits->p2.x) {
288 _right.p1.x = limits->p2.x;
289 _right.p1.y = limits->p1.y;
290 _right.p2.x = limits->p2.x;
291 _right.p2.y = limits->p2.y;
294 if (left.p1.x >= right.p1.x)
298 _cairo_traps_add_trap (traps, _top, _bottom, &_right, &_left);
300 _cairo_traps_add_trap (traps, _top, _bottom, &_left, &_right);
303 _cairo_traps_add_trap (traps, top, bottom, &left, &right);
306 return traps->status;
310 _cairo_traps_translate (cairo_traps_t *traps, int x, int y)
312 cairo_fixed_t xoff, yoff;
313 cairo_trapezoid_t *t;
316 /* Ugh. The cairo_composite/(Render) interface doesn't allow
317 an offset for the trapezoids. Need to manually shift all
318 the coordinates to align with the offset origin of the
319 intermediate surface. */
321 xoff = _cairo_fixed_from_int (x);
322 yoff = _cairo_fixed_from_int (y);
324 for (i = 0, t = traps->traps; i < traps->num_traps; i++, t++) {
327 t->left.p1.x += xoff;
328 t->left.p1.y += yoff;
329 t->left.p2.x += xoff;
330 t->left.p2.y += yoff;
331 t->right.p1.x += xoff;
332 t->right.p1.y += yoff;
333 t->right.p2.x += xoff;
334 t->right.p2.y += yoff;
339 _cairo_trapezoid_array_translate_and_scale (cairo_trapezoid_t *offset_traps,
340 cairo_trapezoid_t *src_traps,
342 double tx, double ty,
343 double sx, double sy)
346 cairo_fixed_t xoff = _cairo_fixed_from_double (tx);
347 cairo_fixed_t yoff = _cairo_fixed_from_double (ty);
349 if (sx == 1.0 && sy == 1.0) {
350 for (i = 0; i < num_traps; i++) {
351 offset_traps[i].top = src_traps[i].top + yoff;
352 offset_traps[i].bottom = src_traps[i].bottom + yoff;
353 offset_traps[i].left.p1.x = src_traps[i].left.p1.x + xoff;
354 offset_traps[i].left.p1.y = src_traps[i].left.p1.y + yoff;
355 offset_traps[i].left.p2.x = src_traps[i].left.p2.x + xoff;
356 offset_traps[i].left.p2.y = src_traps[i].left.p2.y + yoff;
357 offset_traps[i].right.p1.x = src_traps[i].right.p1.x + xoff;
358 offset_traps[i].right.p1.y = src_traps[i].right.p1.y + yoff;
359 offset_traps[i].right.p2.x = src_traps[i].right.p2.x + xoff;
360 offset_traps[i].right.p2.y = src_traps[i].right.p2.y + yoff;
363 cairo_fixed_t xsc = _cairo_fixed_from_double (sx);
364 cairo_fixed_t ysc = _cairo_fixed_from_double (sy);
366 for (i = 0; i < num_traps; i++) {
367 offset_traps[i].top = _cairo_fixed_mul (src_traps[i].top + yoff, ysc);
368 offset_traps[i].bottom = _cairo_fixed_mul (src_traps[i].bottom + yoff, ysc);
369 offset_traps[i].left.p1.x = _cairo_fixed_mul (src_traps[i].left.p1.x + xoff, xsc);
370 offset_traps[i].left.p1.y = _cairo_fixed_mul (src_traps[i].left.p1.y + yoff, ysc);
371 offset_traps[i].left.p2.x = _cairo_fixed_mul (src_traps[i].left.p2.x + xoff, xsc);
372 offset_traps[i].left.p2.y = _cairo_fixed_mul (src_traps[i].left.p2.y + yoff, ysc);
373 offset_traps[i].right.p1.x = _cairo_fixed_mul (src_traps[i].right.p1.x + xoff, xsc);
374 offset_traps[i].right.p1.y = _cairo_fixed_mul (src_traps[i].right.p1.y + yoff, ysc);
375 offset_traps[i].right.p2.x = _cairo_fixed_mul (src_traps[i].right.p2.x + xoff, xsc);
376 offset_traps[i].right.p2.y = _cairo_fixed_mul (src_traps[i].right.p2.y + yoff, ysc);
382 _cairo_trap_contains (cairo_trapezoid_t *t, cairo_point_t *pt)
384 cairo_slope_t slope_left, slope_pt, slope_right;
388 if (t->bottom < pt->y)
391 _cairo_slope_init (&slope_left, &t->left.p1, &t->left.p2);
392 _cairo_slope_init (&slope_pt, &t->left.p1, pt);
394 if (_cairo_slope_compare (&slope_left, &slope_pt) < 0)
397 _cairo_slope_init (&slope_right, &t->right.p1, &t->right.p2);
398 _cairo_slope_init (&slope_pt, &t->right.p1, pt);
400 if (_cairo_slope_compare (&slope_pt, &slope_right) < 0)
407 _cairo_traps_contain (const cairo_traps_t *traps,
413 point.x = _cairo_fixed_from_double (x);
414 point.y = _cairo_fixed_from_double (y);
416 for (i = 0; i < traps->num_traps; i++) {
417 if (_cairo_trap_contains (&traps->traps[i], &point))
425 _line_compute_intersection_x_for_y (const cairo_line_t *line,
428 return _cairo_edge_compute_intersection_x_for_y (&line->p1, &line->p2, y);
432 _cairo_traps_extents (const cairo_traps_t *traps,
433 cairo_box_t *extents)
437 if (traps->num_traps == 0) {
438 extents->p1.x = extents->p1.y = 0;
439 extents->p2.x = extents->p2.y = 0;
443 extents->p1.x = extents->p1.y = INT32_MAX;
444 extents->p2.x = extents->p2.y = INT32_MIN;
446 for (i = 0; i < traps->num_traps; i++) {
447 const cairo_trapezoid_t *trap = &traps->traps[i];
449 if (trap->top < extents->p1.y)
450 extents->p1.y = trap->top;
451 if (trap->bottom > extents->p2.y)
452 extents->p2.y = trap->bottom;
454 if (trap->left.p1.x < extents->p1.x) {
455 cairo_fixed_t x = trap->left.p1.x;
456 if (trap->top != trap->left.p1.y) {
457 x = _line_compute_intersection_x_for_y (&trap->left,
459 if (x < extents->p1.x)
464 if (trap->left.p2.x < extents->p1.x) {
465 cairo_fixed_t x = trap->left.p2.x;
466 if (trap->bottom != trap->left.p2.y) {
467 x = _line_compute_intersection_x_for_y (&trap->left,
469 if (x < extents->p1.x)
475 if (trap->right.p1.x > extents->p2.x) {
476 cairo_fixed_t x = trap->right.p1.x;
477 if (trap->top != trap->right.p1.y) {
478 x = _line_compute_intersection_x_for_y (&trap->right,
480 if (x > extents->p2.x)
485 if (trap->right.p2.x > extents->p2.x) {
486 cairo_fixed_t x = trap->right.p2.x;
487 if (trap->bottom != trap->right.p2.y) {
488 x = _line_compute_intersection_x_for_y (&trap->right,
490 if (x > extents->p2.x)
499 _mono_edge_is_vertical (const cairo_line_t *line)
501 return _cairo_fixed_integer_round_down (line->p1.x) == _cairo_fixed_integer_round_down (line->p2.x);
505 _traps_are_pixel_aligned (cairo_traps_t *traps,
506 cairo_antialias_t antialias)
510 if (antialias == CAIRO_ANTIALIAS_NONE) {
511 for (i = 0; i < traps->num_traps; i++) {
512 if (! _mono_edge_is_vertical (&traps->traps[i].left) ||
513 ! _mono_edge_is_vertical (&traps->traps[i].right))
515 traps->maybe_region = FALSE;
520 for (i = 0; i < traps->num_traps; i++) {
521 if (traps->traps[i].left.p1.x != traps->traps[i].left.p2.x ||
522 traps->traps[i].right.p1.x != traps->traps[i].right.p2.x ||
523 ! _cairo_fixed_is_integer (traps->traps[i].top) ||
524 ! _cairo_fixed_is_integer (traps->traps[i].bottom) ||
525 ! _cairo_fixed_is_integer (traps->traps[i].left.p1.x) ||
526 ! _cairo_fixed_is_integer (traps->traps[i].right.p1.x))
528 traps->maybe_region = FALSE;
538 * _cairo_traps_extract_region:
539 * @traps: a #cairo_traps_t
540 * @region: a #cairo_region_t
542 * Determines if a set of trapezoids are exactly representable as a
543 * cairo region. If so, the passed-in region is initialized to
544 * the area representing the given traps. It should be finalized
545 * with cairo_region_fini(). If not, %CAIRO_INT_STATUS_UNSUPPORTED
548 * Return value: %CAIRO_STATUS_SUCCESS, %CAIRO_INT_STATUS_UNSUPPORTED
549 * or %CAIRO_STATUS_NO_MEMORY
552 _cairo_traps_extract_region (cairo_traps_t *traps,
553 cairo_antialias_t antialias,
554 cairo_region_t **region)
556 cairo_rectangle_int_t stack_rects[CAIRO_STACK_ARRAY_LENGTH (cairo_rectangle_int_t)];
557 cairo_rectangle_int_t *rects = stack_rects;
558 cairo_int_status_t status;
561 /* we only treat this a hint... */
562 if (antialias != CAIRO_ANTIALIAS_NONE && ! traps->maybe_region)
563 return CAIRO_INT_STATUS_UNSUPPORTED;
565 if (! _traps_are_pixel_aligned (traps, antialias)) {
566 traps->maybe_region = FALSE;
567 return CAIRO_INT_STATUS_UNSUPPORTED;
570 if (traps->num_traps > ARRAY_LENGTH (stack_rects)) {
571 rects = _cairo_malloc_ab (traps->num_traps, sizeof (cairo_rectangle_int_t));
573 if (unlikely (rects == NULL))
574 return _cairo_error (CAIRO_STATUS_NO_MEMORY);
578 for (i = 0; i < traps->num_traps; i++) {
581 if (antialias == CAIRO_ANTIALIAS_NONE) {
582 x1 = _cairo_fixed_integer_round_down (traps->traps[i].left.p1.x);
583 y1 = _cairo_fixed_integer_round_down (traps->traps[i].top);
584 x2 = _cairo_fixed_integer_round_down (traps->traps[i].right.p1.x);
585 y2 = _cairo_fixed_integer_round_down (traps->traps[i].bottom);
587 x1 = _cairo_fixed_integer_part (traps->traps[i].left.p1.x);
588 y1 = _cairo_fixed_integer_part (traps->traps[i].top);
589 x2 = _cairo_fixed_integer_part (traps->traps[i].right.p1.x);
590 y2 = _cairo_fixed_integer_part (traps->traps[i].bottom);
593 if (x2 > x1 && y2 > y1) {
594 rects[rect_count].x = x1;
595 rects[rect_count].y = y1;
596 rects[rect_count].width = x2 - x1;
597 rects[rect_count].height = y2 - y1;
603 *region = cairo_region_create_rectangles (rects, rect_count);
604 status = (*region)->status;
606 if (rects != stack_rects)
613 _cairo_traps_to_boxes (cairo_traps_t *traps,
614 cairo_antialias_t antialias,
615 cairo_boxes_t *boxes)
619 for (i = 0; i < traps->num_traps; i++) {
620 if (traps->traps[i].left.p1.x != traps->traps[i].left.p2.x ||
621 traps->traps[i].right.p1.x != traps->traps[i].right.p2.x)
625 _cairo_boxes_init (boxes);
627 boxes->num_boxes = traps->num_traps;
628 boxes->chunks.base = (cairo_box_t *) traps->traps;
629 boxes->chunks.count = traps->num_traps;
630 boxes->chunks.size = traps->num_traps;
632 if (antialias != CAIRO_ANTIALIAS_NONE) {
633 for (i = 0; i < traps->num_traps; i++) {
634 /* Note the traps and boxes alias so we need to take the local copies first. */
635 cairo_fixed_t x1 = traps->traps[i].left.p1.x;
636 cairo_fixed_t x2 = traps->traps[i].right.p1.x;
637 cairo_fixed_t y1 = traps->traps[i].top;
638 cairo_fixed_t y2 = traps->traps[i].bottom;
640 boxes->chunks.base[i].p1.x = x1;
641 boxes->chunks.base[i].p1.y = y1;
642 boxes->chunks.base[i].p2.x = x2;
643 boxes->chunks.base[i].p2.y = y2;
645 if (boxes->is_pixel_aligned) {
646 boxes->is_pixel_aligned =
647 _cairo_fixed_is_integer (x1) && _cairo_fixed_is_integer (y1) &&
648 _cairo_fixed_is_integer (x2) && _cairo_fixed_is_integer (y2);
652 boxes->is_pixel_aligned = TRUE;
654 for (i = 0; i < traps->num_traps; i++) {
655 /* Note the traps and boxes alias so we need to take the local copies first. */
656 cairo_fixed_t x1 = traps->traps[i].left.p1.x;
657 cairo_fixed_t x2 = traps->traps[i].right.p1.x;
658 cairo_fixed_t y1 = traps->traps[i].top;
659 cairo_fixed_t y2 = traps->traps[i].bottom;
661 /* round down here to match Pixman's behavior when using traps. */
662 boxes->chunks.base[i].p1.x = _cairo_fixed_round_down (x1);
663 boxes->chunks.base[i].p1.y = _cairo_fixed_round_down (y1);
664 boxes->chunks.base[i].p2.x = _cairo_fixed_round_down (x2);
665 boxes->chunks.base[i].p2.y = _cairo_fixed_round_down (y2);
672 /* moves trap points such that they become the actual corners of the trapezoid */
674 _sanitize_trap (cairo_trapezoid_t *t)
676 cairo_trapezoid_t s = *t;
678 #define FIX(lr, tb, p) \
679 if (t->lr.p.y != t->tb) { \
680 t->lr.p.x = s.lr.p2.x + _cairo_fixed_mul_div_floor (s.lr.p1.x - s.lr.p2.x, s.tb - s.lr.p2.y, s.lr.p1.y - s.lr.p2.y); \
684 FIX (left, bottom, p2);
685 FIX (right, top, p1);
686 FIX (right, bottom, p2);
689 cairo_private cairo_status_t
690 _cairo_traps_path (const cairo_traps_t *traps,
691 cairo_path_fixed_t *path)
695 for (i = 0; i < traps->num_traps; i++) {
696 cairo_status_t status;
697 cairo_trapezoid_t trap = traps->traps[i];
699 if (trap.top == trap.bottom)
702 _sanitize_trap (&trap);
704 status = _cairo_path_fixed_move_to (path, trap.left.p1.x, trap.top);
705 if (unlikely (status)) return status;
706 status = _cairo_path_fixed_line_to (path, trap.right.p1.x, trap.top);
707 if (unlikely (status)) return status;
708 status = _cairo_path_fixed_line_to (path, trap.right.p2.x, trap.bottom);
709 if (unlikely (status)) return status;
710 status = _cairo_path_fixed_line_to (path, trap.left.p2.x, trap.bottom);
711 if (unlikely (status)) return status;
712 status = _cairo_path_fixed_close_path (path);
713 if (unlikely (status)) return status;
716 return CAIRO_STATUS_SUCCESS;