06ef9fdf7997759c9af1427d196b7f63ecc2e97f
[framework/graphics/cairo.git] / src / cairo-damage.c
1 /*
2  * Copyright © 2012 Intel Corporation
3  *
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.
11  *
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
17  *
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/
22  *
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.
26  *
27  * The Original Code is the cairo graphics library.
28  *
29  * The Initial Developer of the Original Code is Chris Wilson
30  *
31  * Contributor(s):
32  *      Chris Wilson <chris@chris-wilson.co.uk>
33  */
34
35 #include "cairoint.h"
36
37 #include "cairo-damage-private.h"
38 #include "cairo-region-private.h"
39
40 static const cairo_damage_t __cairo_damage__nil = { CAIRO_STATUS_NO_MEMORY };
41
42 cairo_damage_t *
43 _cairo_damage_create (void)
44 {
45     cairo_damage_t *damage;
46
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;
51     }
52
53     damage->status = CAIRO_STATUS_SUCCESS;
54     damage->region = NULL;
55     damage->dirty = 0;
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;
61
62     damage->remain = damage->chunks.size;
63
64     return damage;
65 }
66
67 void
68 _cairo_damage_destroy (cairo_damage_t *damage)
69 {
70     struct _cairo_damage_chunk *chunk, *next;
71
72     if (damage == (cairo_damage_t *) &__cairo_damage__nil)
73         return;
74
75     for (chunk = damage->chunks.next; chunk != NULL; chunk = next) {
76         next = chunk->next;
77         free (chunk);
78     }
79     cairo_region_destroy (damage->region);
80     free (damage);
81 }
82
83 static cairo_damage_t *
84 _cairo_damage_add_boxes(cairo_damage_t *damage,
85                         const cairo_box_t *boxes,
86                         int count)
87 {
88     struct _cairo_damage_chunk *chunk;
89     int n, size;
90
91     TRACE ((stderr, "%s x%d\n", __FUNCTION__, count));
92
93     if (damage == NULL)
94         damage = _cairo_damage_create ();
95     if (damage->status)
96         return damage;
97
98
99     damage->dirty += count;
100
101     n = count;
102     if (n > damage->remain)
103         n = damage->remain;
104
105     memcpy (damage->tail->base + damage->tail->count, boxes,
106             n * sizeof (cairo_box_t));
107
108     count -= n;
109     damage->tail->count += n;
110     damage->remain -= n;
111
112     if (count == 0)
113         return damage;
114
115     size = 2 * damage->tail->size;
116     if (size < count)
117         size = (count + 64) & ~63;
118
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;
123     }
124
125     chunk->next = NULL;
126     chunk->base = (cairo_box_t *) (chunk + 1);
127     chunk->size = size;
128     chunk->count = count;
129
130     damage->tail->next = chunk;
131     damage->remain = size - count;
132
133     memcpy (damage->tail->base, boxes + n,
134             count * sizeof (cairo_box_t));
135
136     return damage;
137 }
138
139 cairo_damage_t *
140 _cairo_damage_add_box(cairo_damage_t *damage,
141                       const cairo_box_t *box)
142 {
143     TRACE ((stderr, "%s: (%d, %d),(%d, %d)\n", __FUNCTION__,
144             box->p1.x, box->p1.y, box->p2.x, box->p2.y));
145
146     return _cairo_damage_add_boxes(damage, box, 1);
147 }
148
149 cairo_damage_t *
150 _cairo_damage_add_rectangle(cairo_damage_t *damage,
151                             const cairo_rectangle_int_t *r)
152 {
153     cairo_box_t box;
154
155     TRACE ((stderr, "%s: (%d, %d)x(%d, %d)\n", __FUNCTION__,
156             r->x, r->y, r->width, r->height));
157
158     box.p1.x = r->x;
159     box.p1.y = r->y;
160     box.p2.x = r->x + r->width;
161     box.p2.y = r->y + r->height;
162
163     return _cairo_damage_add_boxes(damage, &box, 1);
164 }
165
166 cairo_damage_t *
167 _cairo_damage_add_region (cairo_damage_t *damage,
168                           const cairo_region_t *region)
169 {
170     cairo_box_t *boxes;
171     int nbox;
172
173     TRACE ((stderr, "%s\n", __FUNCTION__));
174
175     boxes = _cairo_region_get_boxes (region, &nbox);
176     return _cairo_damage_add_boxes(damage, boxes, nbox);
177 }
178
179 cairo_damage_t *
180 _cairo_damage_reduce (cairo_damage_t *damage)
181 {
182     cairo_box_t *free_boxes = NULL;
183     cairo_box_t *boxes, *b;
184     struct _cairo_damage_chunk *chunk, *last;
185
186     TRACE ((stderr, "%s: dirty=%d\n", __FUNCTION__,
187             damage ? damage->dirty : -1));
188     if (damage == NULL || damage->status || !damage->dirty)
189         return damage;
190
191     if (damage->region) {
192         cairo_region_t *region;
193
194         region = damage->region;
195         damage->region = NULL;
196
197         damage = _cairo_damage_add_region (damage, region);
198         cairo_region_destroy (region);
199
200         if (unlikely (damage->status))
201             return damage;
202     }
203
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;
210         }
211
212         b = boxes;
213         last = NULL;
214     } else {
215         b = boxes + damage->tail->count;
216         last = damage->tail;
217     }
218
219     for (chunk = &damage->chunks; chunk != last; chunk = chunk->next) {
220         memcpy (b, chunk->base, chunk->count * sizeof (cairo_box_t));
221         b += chunk->count;
222     }
223
224     damage->region = _cairo_region_create_from_boxes (boxes, damage->dirty);
225     free (free_boxes);
226
227     if (unlikely (damage->region->status)) {
228         _cairo_damage_destroy (damage);
229         return (cairo_damage_t *) &__cairo_damage__nil;
230     }
231
232     damage->dirty = 0;
233     return damage;
234 }