2 * Copyright © 2012 Intel Corporation
4 * This library is free software; you can redistribute it and/or
5 * modify it either under the terms of the GNU Lesser General Public
6 * License version 2.1 as published by the Free Software Foundation
7 * (the "LGPL") or, at your option, under the terms of the Mozilla
8 * Public License Version 1.1 (the "MPL"). If you do not alter this
9 * notice, a recipient may use your version of this file under either
10 * the MPL or the LGPL.
12 * You should have received a copy of the LGPL along with this library
13 * in the file COPYING-LGPL-2.1; if not, write to the Free Software
14 * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA
15 * You should have received a copy of the MPL along with this library
16 * in the file COPYING-MPL-1.1
18 * The contents of this file are subject to the Mozilla Public License
19 * Version 1.1 (the "License"); you may not use this file except in
20 * compliance with the License. You may obtain a copy of the License at
21 * http://www.mozilla.org/MPL/
23 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY
24 * OF ANY KIND, either express or implied. See the LGPL or the MPL for
25 * the specific language governing rights and limitations.
27 * The Original Code is the cairo graphics library.
29 * The Initial Developer of the Original Code is Chris Wilson
32 * Chris Wilson <chris@chris-wilson.co.uk>
37 #include "cairo-damage-private.h"
38 #include "cairo-region-private.h"
40 static const cairo_damage_t __cairo_damage__nil = { CAIRO_STATUS_NO_MEMORY };
43 _cairo_damage_create (void)
45 cairo_damage_t *damage;
47 damage = malloc (sizeof (*damage));
48 if (unlikely (damage == NULL)) {
49 _cairo_error_throw(CAIRO_STATUS_NO_MEMORY);
50 return (cairo_damage_t *) &__cairo_damage__nil;
53 damage->status = CAIRO_STATUS_SUCCESS;
54 damage->region = NULL;
56 damage->tail = &damage->chunks;
57 damage->chunks.base = damage->boxes;
58 damage->chunks.size = ARRAY_LENGTH(damage->boxes);
59 damage->chunks.count = 0;
60 damage->chunks.next = NULL;
62 damage->remain = damage->chunks.size;
68 _cairo_damage_destroy (cairo_damage_t *damage)
70 struct _cairo_damage_chunk *chunk, *next;
72 if (damage == (cairo_damage_t *) &__cairo_damage__nil)
75 for (chunk = damage->chunks.next; chunk != NULL; chunk = next) {
79 cairo_region_destroy (damage->region);
83 static cairo_damage_t *
84 _cairo_damage_add_boxes(cairo_damage_t *damage,
85 const cairo_box_t *boxes,
88 struct _cairo_damage_chunk *chunk;
91 TRACE ((stderr, "%s x%d\n", __FUNCTION__, count));
94 damage = _cairo_damage_create ();
99 damage->dirty += count;
102 if (n > damage->remain)
105 memcpy (damage->tail->base + damage->tail->count, boxes,
106 n * sizeof (cairo_box_t));
109 damage->tail->count += n;
115 size = 2 * damage->tail->size;
117 size = (count + 64) & ~63;
119 chunk = malloc (sizeof (*chunk) + sizeof (cairo_box_t) * size);
120 if (unlikely (chunk == NULL)) {
121 _cairo_damage_destroy (damage);
122 return (cairo_damage_t *) &__cairo_damage__nil;
126 chunk->base = (cairo_box_t *) (chunk + 1);
128 chunk->count = count;
130 damage->tail->next = chunk;
131 damage->remain = size - count;
133 memcpy (damage->tail->base, boxes + n,
134 count * sizeof (cairo_box_t));
140 _cairo_damage_add_box(cairo_damage_t *damage,
141 const cairo_box_t *box)
143 TRACE ((stderr, "%s: (%d, %d),(%d, %d)\n", __FUNCTION__,
144 box->p1.x, box->p1.y, box->p2.x, box->p2.y));
146 return _cairo_damage_add_boxes(damage, box, 1);
150 _cairo_damage_add_rectangle(cairo_damage_t *damage,
151 const cairo_rectangle_int_t *r)
155 TRACE ((stderr, "%s: (%d, %d)x(%d, %d)\n", __FUNCTION__,
156 r->x, r->y, r->width, r->height));
160 box.p2.x = r->x + r->width;
161 box.p2.y = r->y + r->height;
163 return _cairo_damage_add_boxes(damage, &box, 1);
167 _cairo_damage_add_region (cairo_damage_t *damage,
168 const cairo_region_t *region)
173 TRACE ((stderr, "%s\n", __FUNCTION__));
175 boxes = _cairo_region_get_boxes (region, &nbox);
176 return _cairo_damage_add_boxes(damage, boxes, nbox);
180 _cairo_damage_reduce (cairo_damage_t *damage)
182 cairo_box_t *free_boxes = NULL;
183 cairo_box_t *boxes, *b;
184 struct _cairo_damage_chunk *chunk, *last;
186 TRACE ((stderr, "%s: dirty=%d\n", __FUNCTION__,
187 damage ? damage->dirty : -1));
188 if (damage == NULL || damage->status || !damage->dirty)
191 if (damage->region) {
192 cairo_region_t *region;
194 region = damage->region;
195 damage->region = NULL;
197 damage = _cairo_damage_add_region (damage, region);
198 cairo_region_destroy (region);
200 if (unlikely (damage->status))
204 boxes = damage->tail->base;
205 if (damage->dirty > damage->tail->size) {
206 boxes = free_boxes = malloc (damage->dirty * sizeof (cairo_box_t));
207 if (unlikely (boxes == NULL)) {
208 _cairo_damage_destroy (damage);
209 return (cairo_damage_t *) &__cairo_damage__nil;
215 b = boxes + damage->tail->count;
219 for (chunk = &damage->chunks; chunk != last; chunk = chunk->next) {
220 memcpy (b, chunk->base, chunk->count * sizeof (cairo_box_t));
224 damage->region = _cairo_region_create_from_boxes (boxes, damage->dirty);
227 if (unlikely (damage->region->status)) {
228 _cairo_damage_destroy (damage);
229 return (cairo_damage_t *) &__cairo_damage__nil;