~ubuntu-branches/ubuntu/wily/mutter/wily

« back to all changes in this revision

Viewing changes to src/compositor/region-utils.c

  • Committer: Bazaar Package Importer
  • Author(s): Robert Ancell
  • Date: 2010-11-30 10:56:20 UTC
  • Revision ID: james.westby@ubuntu.com-20101130105620-5pg8qjx4fn4nt00b
Tags: 2.91.3-0ubuntu1
New upstream release

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
 
2
/*
 
3
 * Utilities for region manipulation
 
4
 *
 
5
 * Copyright (C) 2010 Red Hat, Inc.
 
6
 *
 
7
 * This program is free software; you can redistribute it and/or
 
8
 * modify it under the terms of the GNU General Public License as
 
9
 * published by the Free Software Foundation; either version 2 of the
 
10
 * License, or (at your option) any later version.
 
11
 *
 
12
 * This program is distributed in the hope that it will be useful, but
 
13
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
15
 * General Public License for more details.
 
16
 *
 
17
 * You should have received a copy of the GNU General Public License
 
18
 * along with this program; if not, write to the Free Software
 
19
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 
20
 * 02111-1307, USA.
 
21
 */
 
22
 
 
23
#include "region-utils.h"
 
24
 
 
25
#include <math.h>
 
26
 
 
27
/* MetaRegionBuilder */
 
28
 
 
29
/* Various algorithms in this file require unioning together a set of rectangles
 
30
 * that are unsorted or overlap; unioning such a set of rectangles 1-by-1
 
31
 * using cairo_region_union_rectangle() produces O(N^2) behavior (if the union
 
32
 * adds or removes rectangles in the middle of the region, then it has to
 
33
 * move all the rectangles after that.) To avoid this behavior, MetaRegionBuilder
 
34
 * creates regions for small groups of rectangles and merges them together in
 
35
 * a binary tree.
 
36
 *
 
37
 * Possible improvement: From a glance at the code, accumulating all the rectangles
 
38
 *  into a flat array and then calling the (not usefully documented)
 
39
 *  cairo_region_create_rectangles() would have the same behavior and would be
 
40
 *  simpler and a bit more efficient.
 
41
 */
 
42
 
 
43
/* Optimium performance seems to be with MAX_CHUNK_RECTANGLES=4; 8 is about 10% slower.
 
44
 * But using 8 may be more robust to systems with slow malloc(). */
 
45
#define MAX_CHUNK_RECTANGLES 8
 
46
#define MAX_LEVELS 16
 
47
 
 
48
typedef struct
 
49
{
 
50
  /* To merge regions in binary tree order, we need to keep track of
 
51
   * the regions that we've already merged together at different
 
52
   * levels of the tree. We fill in an array in the pattern:
 
53
   *
 
54
   * |a  |
 
55
   * |b  |a  |
 
56
   * |c  |   |ab |
 
57
   * |d  |c  |ab |
 
58
   * |e  |   |   |abcd|
 
59
   */
 
60
  cairo_region_t *levels[MAX_LEVELS];
 
61
  int n_levels;
 
62
} MetaRegionBuilder;
 
63
 
 
64
static void
 
65
meta_region_builder_init (MetaRegionBuilder *builder)
 
66
{
 
67
  int i;
 
68
  for (i = 0; i < MAX_LEVELS; i++)
 
69
    builder->levels[i] = NULL;
 
70
  builder->n_levels = 1;
 
71
}
 
72
 
 
73
static void
 
74
meta_region_builder_add_rectangle (MetaRegionBuilder *builder,
 
75
                                   int                x,
 
76
                                   int                y,
 
77
                                   int                width,
 
78
                                   int                height)
 
79
{
 
80
  cairo_rectangle_int_t rect;
 
81
  int i;
 
82
 
 
83
  if (builder->levels[0] == NULL)
 
84
    builder->levels[0] = cairo_region_create ();
 
85
 
 
86
  rect.x = x;
 
87
  rect.y = y;
 
88
  rect.width = width;
 
89
  rect.height = height;
 
90
 
 
91
  cairo_region_union_rectangle (builder->levels[0], &rect);
 
92
  if (cairo_region_num_rectangles (builder->levels[0]) >= MAX_CHUNK_RECTANGLES)
 
93
    {
 
94
      for (i = 1; i < builder->n_levels + 1; i++)
 
95
        {
 
96
          if (builder->levels[i] == NULL)
 
97
            {
 
98
              if (i < MAX_LEVELS)
 
99
                {
 
100
                  builder->levels[i] = builder->levels[i - 1];
 
101
                  builder->levels[i - 1] = NULL;
 
102
                  if (i == builder->n_levels)
 
103
                    builder->n_levels++;
 
104
                }
 
105
 
 
106
              break;
 
107
            }
 
108
          else
 
109
            {
 
110
              cairo_region_union (builder->levels[i], builder->levels[i - 1]);
 
111
              cairo_region_destroy (builder->levels[i - 1]);
 
112
              builder->levels[i - 1] = NULL;
 
113
            }
 
114
        }
 
115
    }
 
116
}
 
117
 
 
118
static cairo_region_t *
 
119
meta_region_builder_finish (MetaRegionBuilder *builder)
 
120
{
 
121
  cairo_region_t *result = NULL;
 
122
  int i;
 
123
 
 
124
  for (i = 0; i < builder->n_levels; i++)
 
125
    {
 
126
      if (builder->levels[i])
 
127
        {
 
128
          if (result == NULL)
 
129
            result = builder->levels[i];
 
130
          else
 
131
            {
 
132
              cairo_region_union(result, builder->levels[i]);
 
133
              cairo_region_destroy (builder->levels[i]);
 
134
            }
 
135
        }
 
136
    }
 
137
 
 
138
  if (result == NULL)
 
139
    result = cairo_region_create ();
 
140
 
 
141
  return result;
 
142
}
 
143
 
 
144
 
 
145
/* MetaRegionIterator */
 
146
 
 
147
void
 
148
meta_region_iterator_init (MetaRegionIterator *iter,
 
149
                           cairo_region_t     *region)
 
150
{
 
151
  iter->region = region;
 
152
  iter->i = 0;
 
153
  iter->n_rectangles = cairo_region_num_rectangles (region);
 
154
  iter->line_start = TRUE;
 
155
 
 
156
  if (iter->n_rectangles > 1)
 
157
    {
 
158
      cairo_region_get_rectangle (region, 0, &iter->rectangle);
 
159
      cairo_region_get_rectangle (region, 1, &iter->next_rectangle);
 
160
 
 
161
      iter->line_end = iter->next_rectangle.y != iter->rectangle.y;
 
162
    }
 
163
  else if (iter->n_rectangles > 0)
 
164
    {
 
165
      cairo_region_get_rectangle (region, 0, &iter->rectangle);
 
166
      iter->line_end = TRUE;
 
167
    }
 
168
}
 
169
 
 
170
gboolean
 
171
meta_region_iterator_at_end (MetaRegionIterator *iter)
 
172
{
 
173
  return iter->i >= iter->n_rectangles;
 
174
}
 
175
 
 
176
void
 
177
meta_region_iterator_next (MetaRegionIterator *iter)
 
178
{
 
179
  iter->i++;
 
180
  iter->rectangle = iter->next_rectangle;
 
181
  iter->line_start = iter->line_end;
 
182
 
 
183
  if (iter->i < iter->n_rectangles)
 
184
    {
 
185
      cairo_region_get_rectangle (iter->region, iter->i + 1, &iter->next_rectangle);
 
186
      iter->line_end = iter->next_rectangle.y != iter->rectangle.y;
 
187
    }
 
188
  else
 
189
    {
 
190
      iter->line_end = TRUE;
 
191
    }
 
192
}
 
193
 
 
194
static void
 
195
add_expanded_rect (MetaRegionBuilder  *builder,
 
196
                   int                 x,
 
197
                   int                 y,
 
198
                   int                 width,
 
199
                   int                 height,
 
200
                   int                 x_amount,
 
201
                   int                 y_amount,
 
202
                   gboolean            flip)
 
203
{
 
204
  if (flip)
 
205
    meta_region_builder_add_rectangle (builder,
 
206
                                       y - y_amount, x - x_amount,
 
207
                                       height + 2 * y_amount, width + 2 * x_amount);
 
208
  else
 
209
    meta_region_builder_add_rectangle (builder,
 
210
                                       x - x_amount, y - y_amount,
 
211
                                       width + 2 * x_amount, height + 2 * y_amount);
 
212
}
 
213
 
 
214
static cairo_region_t *
 
215
expand_region (cairo_region_t *region,
 
216
               int             x_amount,
 
217
               int             y_amount,
 
218
               gboolean        flip)
 
219
{
 
220
  MetaRegionBuilder builder;
 
221
  int n;
 
222
  int i;
 
223
 
 
224
  meta_region_builder_init (&builder);
 
225
 
 
226
  n = cairo_region_num_rectangles (region);
 
227
  for (i = 0; i < n; i++)
 
228
    {
 
229
      cairo_rectangle_int_t rect;
 
230
 
 
231
      cairo_region_get_rectangle (region, i, &rect);
 
232
      add_expanded_rect (&builder,
 
233
                         rect.x, rect.y, rect.width, rect.height,
 
234
                         x_amount, y_amount, flip);
 
235
    }
 
236
 
 
237
  return meta_region_builder_finish (&builder);
 
238
}
 
239
 
 
240
/* This computes a (clipped version) of the inverse of the region
 
241
 * and expands it by the given amount */
 
242
static cairo_region_t *
 
243
expand_region_inverse (cairo_region_t *region,
 
244
                       int             x_amount,
 
245
                       int             y_amount,
 
246
                       gboolean        flip)
 
247
{
 
248
  MetaRegionBuilder builder;
 
249
  MetaRegionIterator iter;
 
250
  cairo_rectangle_int_t extents;
 
251
  cairo_region_t *chunk;
 
252
 
 
253
  int last_x;
 
254
 
 
255
  meta_region_builder_init (&builder);
 
256
 
 
257
  cairo_region_get_extents (region, &extents);
 
258
  add_expanded_rect (&builder,
 
259
                     extents.x, extents.y - 1, extents.width, 1,
 
260
                     x_amount, y_amount, flip);
 
261
  add_expanded_rect (&builder,
 
262
                     extents.x - 1, extents.y, 1, extents.height,
 
263
                     x_amount, y_amount, flip);
 
264
  add_expanded_rect (&builder,
 
265
                     extents.x + extents.width, extents.y, 1, extents.height,
 
266
                     x_amount, y_amount, flip);
 
267
  add_expanded_rect (&builder,
 
268
                     extents.x, extents.y + extents.height, extents.width, 1,
 
269
                     x_amount, y_amount, flip);
 
270
 
 
271
  chunk = NULL;
 
272
 
 
273
  last_x = extents.x;
 
274
  for (meta_region_iterator_init (&iter, region);
 
275
       !meta_region_iterator_at_end (&iter);
 
276
       meta_region_iterator_next (&iter))
 
277
    {
 
278
      if (chunk == NULL)
 
279
        chunk = cairo_region_create ();
 
280
 
 
281
      if (iter.rectangle.x > last_x)
 
282
        add_expanded_rect (&builder,
 
283
                           last_x, iter.rectangle.y,
 
284
                           iter.rectangle.x - last_x, iter.rectangle.height,
 
285
                           x_amount, y_amount, flip);
 
286
 
 
287
      if (iter.line_end)
 
288
        {
 
289
          if (extents.x + extents.width > iter.rectangle.x + iter.rectangle.width)
 
290
            add_expanded_rect (&builder,
 
291
                               iter.rectangle.x + iter.rectangle.width, iter.rectangle.y,
 
292
                               (extents.x + extents.width) - (iter.rectangle.x + iter.rectangle.width), iter.rectangle.height,
 
293
                               x_amount, y_amount, flip);
 
294
          last_x = extents.x;
 
295
        }
 
296
      else
 
297
        last_x = iter.rectangle.x + iter.rectangle.width;
 
298
    }
 
299
 
 
300
  return meta_region_builder_finish (&builder);
 
301
}
 
302
 
 
303
/**
 
304
 * meta_make_border_region:
 
305
 * @region: a #cairo_region_t
 
306
 * @x_amount: distance from the border to extend horizontally
 
307
 * @y_amount: distance from the border to extend vertically
 
308
 * @flip: if true, the result is computed with x and y interchanged
 
309
 *
 
310
 * Computes the "border region" of a given region, which is roughly
 
311
 * speaking the set of points near the boundary of the region.  If we
 
312
 * define the operation of growing a region as computing the set of
 
313
 * points within a given manhattan distance of the region, then the
 
314
 * border is 'grow(region) intersect grow(inverse(region))'.
 
315
 *
 
316
 * If we create an image by filling the region with a solid color,
 
317
 * the border is the region affected by blurring the region.
 
318
 *
 
319
 * Return value: a new region which is the border of the given region
 
320
 */
 
321
cairo_region_t *
 
322
meta_make_border_region (cairo_region_t *region,
 
323
                         int             x_amount,
 
324
                         int             y_amount,
 
325
                         gboolean        flip)
 
326
{
 
327
  cairo_region_t *border_region;
 
328
  cairo_region_t *inverse_region;
 
329
 
 
330
  border_region = expand_region (region, x_amount, y_amount, flip);
 
331
  inverse_region = expand_region_inverse (region, x_amount, y_amount, flip);
 
332
  cairo_region_intersect (border_region, inverse_region);
 
333
  cairo_region_destroy (inverse_region);
 
334
 
 
335
  return border_region;
 
336
}