blob: 1f8d30789a93cb530cb960b51c4d480a0e9b9e46 [file] [log] [blame]
Kristian Høgsbergffd710e2008-12-02 15:15:01 -05001/*
2 * Copyright © 2008 Kristian Høgsberg
Kristian Høgsberg902865c2012-02-08 10:11:42 -05003 * Copyright © 2012 Intel Corporation
Kristian Høgsbergffd710e2008-12-02 15:15:01 -05004 *
5 * Permission to use, copy, modify, distribute, and sell this software and its
6 * documentation for any purpose is hereby granted without fee, provided that
7 * the above copyright notice appear in all copies and that both that copyright
8 * notice and this permission notice appear in supporting documentation, and
9 * that the name of the copyright holders not be used in advertising or
10 * publicity pertaining to distribution of the software without specific,
11 * written prior permission. The copyright holders make no representations
12 * about the suitability of this software for any purpose. It is provided "as
13 * is" without express or implied warranty.
14 *
15 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
16 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
17 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
18 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
19 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
20 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
21 * OF THIS SOFTWARE.
22 */
23
Kristian Høgsberg3d5437c2012-02-08 12:46:57 -050024#include "../config.h"
25
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050026#include <stdint.h>
27#include <stdlib.h>
28#include <string.h>
29#include <stdio.h>
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050030#include <math.h>
31#include <cairo.h>
32#include "cairo-util.h"
33
Kristian Høgsbergf02a6492012-03-12 01:05:25 -040034#include "../shared/config-parser.h"
Kristian Høgsberg3d5437c2012-02-08 12:46:57 -050035
Kristian Høgsberg87330262008-11-17 22:23:55 -050036#define ARRAY_LENGTH(a) (sizeof (a) / sizeof (a)[0])
37
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050038void
Benjamin Franzke47eb8f42011-10-07 09:08:56 +020039surface_flush_device(cairo_surface_t *surface)
40{
41 cairo_device_t *device;
42
43 device = cairo_surface_get_device(surface);
44 if (device)
45 cairo_device_flush(device);
46}
47
48void
Kristian Høgsberg10bdd292008-11-08 23:27:27 -050049blur_surface(cairo_surface_t *surface, int margin)
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050050{
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050051 int32_t width, height, stride, x, y, z, w;
52 uint8_t *src, *dst;
53 uint32_t *s, *d, a, p;
Kristian Høgsberg87330262008-11-17 22:23:55 -050054 int i, j, k, size, half;
Kristian Høgsberg49e868c2010-06-15 16:18:58 -040055 uint32_t kernel[49];
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050056 double f;
57
Kristian Høgsberg87330262008-11-17 22:23:55 -050058 size = ARRAY_LENGTH(kernel);
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050059 width = cairo_image_surface_get_width(surface);
60 height = cairo_image_surface_get_height(surface);
61 stride = cairo_image_surface_get_stride(surface);
62 src = cairo_image_surface_get_data(surface);
63
Kristian Høgsberg5fc96ff2009-09-12 15:58:48 -040064 dst = malloc(height * stride);
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050065
66 half = size / 2;
Kristian Høgsberg0cd8f6e2011-01-21 22:19:40 -050067 a = 0;
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050068 for (i = 0; i < size; i++) {
69 f = (i - half);
Kristian Høgsberg49e868c2010-06-15 16:18:58 -040070 kernel[i] = exp(- f * f / ARRAY_LENGTH(kernel)) * 10000;
Kristian Høgsberg0cd8f6e2011-01-21 22:19:40 -050071 a += kernel[i];
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050072 }
73
74 for (i = 0; i < height; i++) {
75 s = (uint32_t *) (src + i * stride);
76 d = (uint32_t *) (dst + i * stride);
77 for (j = 0; j < width; j++) {
Kristian Høgsberg87330262008-11-17 22:23:55 -050078 if (margin < j && j < width - margin) {
79 d[j] = s[j];
Kristian Høgsberg10bdd292008-11-08 23:27:27 -050080 continue;
Kristian Høgsberg87330262008-11-17 22:23:55 -050081 }
82
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050083 x = 0;
84 y = 0;
85 z = 0;
86 w = 0;
87 for (k = 0; k < size; k++) {
88 if (j - half + k < 0 || j - half + k >= width)
89 continue;
90 p = s[j - half + k];
91
92 x += (p >> 24) * kernel[k];
93 y += ((p >> 16) & 0xff) * kernel[k];
94 z += ((p >> 8) & 0xff) * kernel[k];
95 w += (p & 0xff) * kernel[k];
96 }
97 d[j] = (x / a << 24) | (y / a << 16) | (z / a << 8) | w / a;
98 }
99 }
100
101 for (i = 0; i < height; i++) {
102 s = (uint32_t *) (dst + i * stride);
103 d = (uint32_t *) (src + i * stride);
104 for (j = 0; j < width; j++) {
Kristian Høgsberg87330262008-11-17 22:23:55 -0500105 if (margin <= i && i < height - margin) {
106 d[j] = s[j];
Kristian Høgsberg10bdd292008-11-08 23:27:27 -0500107 continue;
Kristian Høgsberg87330262008-11-17 22:23:55 -0500108 }
109
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -0500110 x = 0;
111 y = 0;
112 z = 0;
113 w = 0;
114 for (k = 0; k < size; k++) {
115 if (i - half + k < 0 || i - half + k >= height)
116 continue;
117 s = (uint32_t *) (dst + (i - half + k) * stride);
118 p = s[j];
119
120 x += (p >> 24) * kernel[k];
121 y += ((p >> 16) & 0xff) * kernel[k];
122 z += ((p >> 8) & 0xff) * kernel[k];
123 w += (p & 0xff) * kernel[k];
124 }
125 d[j] = (x / a << 24) | (y / a << 16) | (z / a << 8) | w / a;
126 }
127 }
128
Kristian Høgsberg5fc96ff2009-09-12 15:58:48 -0400129 free(dst);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400130 cairo_surface_mark_dirty(surface);
131}
132
133void
134tile_mask(cairo_t *cr, cairo_surface_t *surface,
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400135 int x, int y, int width, int height, int margin, int top_margin)
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400136{
137 cairo_pattern_t *pattern;
138 cairo_matrix_t matrix;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400139 int i, fx, fy, vmargin;
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400140
141 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
142 pattern = cairo_pattern_create_for_surface (surface);
143
144 for (i = 0; i < 4; i++) {
145 fx = i & 1;
146 fy = i >> 1;
147
148 cairo_matrix_init_translate(&matrix,
149 -x + fx * (128 - width),
150 -y + fy * (128 - height));
151 cairo_pattern_set_matrix(pattern, &matrix);
152
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400153 if (fy)
154 vmargin = margin;
155 else
156 vmargin = top_margin;
157
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400158 cairo_reset_clip(cr);
159 cairo_rectangle(cr,
160 x + fx * (width - margin),
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400161 y + fy * (height - vmargin),
162 margin, vmargin);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400163 cairo_clip (cr);
164 cairo_mask(cr, pattern);
165 }
166
Tiago Vignatti0a266412012-02-09 19:06:56 +0200167 /* Top stretch */
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400168 cairo_matrix_init_translate(&matrix, 64, 0);
169 cairo_matrix_scale(&matrix, 64.0 / (width - 2 * margin), 1);
170 cairo_matrix_translate(&matrix, -x - width / 2, -y);
171 cairo_pattern_set_matrix(pattern, &matrix);
172 cairo_rectangle(cr, x + margin, y, width - 2 * margin, margin);
173
174 cairo_reset_clip(cr);
175 cairo_rectangle(cr,
176 x + margin,
177 y,
178 width - 2 * margin, margin);
179 cairo_clip (cr);
180 cairo_mask(cr, pattern);
181
Tiago Vignatti0a266412012-02-09 19:06:56 +0200182 /* Bottom stretch */
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400183 cairo_matrix_translate(&matrix, 0, -height + 128);
184 cairo_pattern_set_matrix(pattern, &matrix);
185
186 cairo_reset_clip(cr);
187 cairo_rectangle(cr, x + margin, y + height - margin,
188 width - 2 * margin, margin);
189 cairo_clip (cr);
190 cairo_mask(cr, pattern);
191
Tiago Vignatti0a266412012-02-09 19:06:56 +0200192 /* Left stretch */
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400193 cairo_matrix_init_translate(&matrix, 0, 64);
194 cairo_matrix_scale(&matrix, 1, 64.0 / (height - 2 * margin));
195 cairo_matrix_translate(&matrix, -x, -y - height / 2);
196 cairo_pattern_set_matrix(pattern, &matrix);
197 cairo_reset_clip(cr);
198 cairo_rectangle(cr, x, y + margin, margin, height - 2 * margin);
199 cairo_clip (cr);
200 cairo_mask(cr, pattern);
201
Tiago Vignatti0a266412012-02-09 19:06:56 +0200202 /* Right stretch */
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400203 cairo_matrix_translate(&matrix, -width + 128, 0);
204 cairo_pattern_set_matrix(pattern, &matrix);
205 cairo_rectangle(cr, x + width - margin, y + margin,
206 margin, height - 2 * margin);
207 cairo_reset_clip(cr);
208 cairo_clip (cr);
209 cairo_mask(cr, pattern);
210
211 cairo_pattern_destroy(pattern);
212 cairo_reset_clip(cr);
213}
214
215void
216tile_source(cairo_t *cr, cairo_surface_t *surface,
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400217 int x, int y, int width, int height, int margin, int top_margin)
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400218{
219 cairo_pattern_t *pattern;
220 cairo_matrix_t matrix;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400221 int i, fx, fy, vmargin;
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400222
223 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
224 pattern = cairo_pattern_create_for_surface (surface);
225 cairo_set_source(cr, pattern);
226 cairo_pattern_destroy(pattern);
227
228 for (i = 0; i < 4; i++) {
229 fx = i & 1;
230 fy = i >> 1;
231
232 cairo_matrix_init_translate(&matrix,
233 -x + fx * (128 - width),
234 -y + fy * (128 - height));
235 cairo_pattern_set_matrix(pattern, &matrix);
236
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400237 if (fy)
238 vmargin = margin;
239 else
240 vmargin = top_margin;
241
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400242 cairo_rectangle(cr,
243 x + fx * (width - margin),
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400244 y + fy * (height - vmargin),
245 margin, vmargin);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400246 cairo_fill(cr);
247 }
248
Tiago Vignatti0a266412012-02-09 19:06:56 +0200249 /* Top stretch */
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400250 cairo_matrix_init_translate(&matrix, 64, 0);
251 cairo_matrix_scale(&matrix, 64.0 / (width - 2 * margin), 1);
252 cairo_matrix_translate(&matrix, -x - width / 2, -y);
253 cairo_pattern_set_matrix(pattern, &matrix);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400254 cairo_rectangle(cr, x + margin, y, width - 2 * margin, top_margin);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400255 cairo_fill(cr);
256
Tiago Vignatti0a266412012-02-09 19:06:56 +0200257 /* Bottom stretch */
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400258 cairo_matrix_translate(&matrix, 0, -height + 128);
259 cairo_pattern_set_matrix(pattern, &matrix);
260 cairo_rectangle(cr, x + margin, y + height - margin,
261 width - 2 * margin, margin);
262 cairo_fill(cr);
263
Tiago Vignatti0a266412012-02-09 19:06:56 +0200264 /* Left stretch */
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400265 cairo_matrix_init_translate(&matrix, 0, 64);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400266 cairo_matrix_scale(&matrix, 1, 64.0 / (height - margin - top_margin));
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400267 cairo_matrix_translate(&matrix, -x, -y - height / 2);
268 cairo_pattern_set_matrix(pattern, &matrix);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400269 cairo_rectangle(cr, x, y + top_margin,
270 margin, height - margin - top_margin);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400271 cairo_fill(cr);
272
Tiago Vignatti0a266412012-02-09 19:06:56 +0200273 /* Right stretch */
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400274 cairo_matrix_translate(&matrix, -width + 128, 0);
275 cairo_pattern_set_matrix(pattern, &matrix);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400276 cairo_rectangle(cr, x + width - margin, y + top_margin,
277 margin, height - margin - top_margin);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400278 cairo_fill(cr);
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -0500279}
Kristian Høgsberg1e164b92011-09-13 14:47:46 -0400280
281void
282rounded_rect(cairo_t *cr, int x0, int y0, int x1, int y1, int radius)
283{
284 cairo_move_to(cr, x0, y0 + radius);
285 cairo_arc(cr, x0 + radius, y0 + radius, radius, M_PI, 3 * M_PI / 2);
286 cairo_line_to(cr, x1 - radius, y0);
287 cairo_arc(cr, x1 - radius, y0 + radius, radius, 3 * M_PI / 2, 2 * M_PI);
288 cairo_line_to(cr, x1, y1 - radius);
289 cairo_arc(cr, x1 - radius, y1 - radius, radius, 0, M_PI / 2);
290 cairo_line_to(cr, x0 + radius, y1);
291 cairo_arc(cr, x0 + radius, y1 - radius, radius, M_PI / 2, M_PI);
292 cairo_close_path(cr);
293}
Kristian Høgsberg27d38662011-10-20 13:11:12 -0400294
Kristian Høgsbergd6548762012-01-25 15:43:48 -0500295cairo_surface_t *
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400296load_cairo_surface(const char *filename)
Kristian Høgsbergd6548762012-01-25 15:43:48 -0500297{
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400298 pixman_image_t *image;
299 int width, height, stride;
300 void *data;
Kristian Høgsbergd6548762012-01-25 15:43:48 -0500301
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400302 image = load_image(filename);
303 data = pixman_image_get_data(image);
304 width = pixman_image_get_width(image);
305 height = pixman_image_get_height(image);
306 stride = pixman_image_get_stride(image);
Kristian Høgsbergd6548762012-01-25 15:43:48 -0500307
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400308 return cairo_image_surface_create_for_data(data, CAIRO_FORMAT_ARGB32,
309 width, height, stride);
Kristian Høgsbergd6548762012-01-25 15:43:48 -0500310}