tests: add a test causing SIGBUS to the compositor
[profile/ivi/weston-ivi-shell.git] / shared / cairo-util.c
1 /*
2  * Copyright © 2008 Kristian Høgsberg
3  * Copyright © 2012 Intel Corporation
4  *
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
24 #include "config.h"
25
26 #include <stdint.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <stdio.h>
30 #include <math.h>
31 #include <cairo.h>
32 #include "cairo-util.h"
33
34 #include "image-loader.h"
35 #include "config-parser.h"
36
37 #define ARRAY_LENGTH(a) (sizeof (a) / sizeof (a)[0])
38
39 void
40 surface_flush_device(cairo_surface_t *surface)
41 {
42         cairo_device_t *device;
43
44         device = cairo_surface_get_device(surface);
45         if (device)
46                 cairo_device_flush(device);
47 }
48
49 static int
50 blur_surface(cairo_surface_t *surface, int margin)
51 {
52         int32_t width, height, stride, x, y, z, w;
53         uint8_t *src, *dst;
54         uint32_t *s, *d, a, p;
55         int i, j, k, size, half;
56         uint32_t kernel[71];
57         double f;
58
59         size = ARRAY_LENGTH(kernel);
60         width = cairo_image_surface_get_width(surface);
61         height = cairo_image_surface_get_height(surface);
62         stride = cairo_image_surface_get_stride(surface);
63         src = cairo_image_surface_get_data(surface);
64
65         dst = malloc(height * stride);
66         if (dst == NULL)
67                 return -1;
68
69         half = size / 2;
70         a = 0;
71         for (i = 0; i < size; i++) {
72                 f = (i - half);
73                 kernel[i] = exp(- f * f / ARRAY_LENGTH(kernel)) * 10000;
74                 a += kernel[i];
75         }
76
77         for (i = 0; i < height; i++) {
78                 s = (uint32_t *) (src + i * stride);
79                 d = (uint32_t *) (dst + i * stride);
80                 for (j = 0; j < width; j++) {
81                         if (margin < j && j < width - margin) {
82                                 d[j] = s[j];
83                                 continue;
84                         }
85
86                         x = 0;
87                         y = 0;
88                         z = 0;
89                         w = 0;
90                         for (k = 0; k < size; k++) {
91                                 if (j - half + k < 0 || j - half + k >= width)
92                                         continue;
93                                 p = s[j - half + k];
94
95                                 x += (p >> 24) * kernel[k];
96                                 y += ((p >> 16) & 0xff) * kernel[k];
97                                 z += ((p >> 8) & 0xff) * kernel[k];
98                                 w += (p & 0xff) * kernel[k];
99                         }
100                         d[j] = (x / a << 24) | (y / a << 16) | (z / a << 8) | w / a;
101                 }
102         }
103
104         for (i = 0; i < height; i++) {
105                 s = (uint32_t *) (dst + i * stride);
106                 d = (uint32_t *) (src + i * stride);
107                 for (j = 0; j < width; j++) {
108                         if (margin <= i && i < height - margin) {
109                                 d[j] = s[j];
110                                 continue;
111                         }
112
113                         x = 0;
114                         y = 0;
115                         z = 0;
116                         w = 0;
117                         for (k = 0; k < size; k++) {
118                                 if (i - half + k < 0 || i - half + k >= height)
119                                         continue;
120                                 s = (uint32_t *) (dst + (i - half + k) * stride);
121                                 p = s[j];
122
123                                 x += (p >> 24) * kernel[k];
124                                 y += ((p >> 16) & 0xff) * kernel[k];
125                                 z += ((p >> 8) & 0xff) * kernel[k];
126                                 w += (p & 0xff) * kernel[k];
127                         }
128                         d[j] = (x / a << 24) | (y / a << 16) | (z / a << 8) | w / a;
129                 }
130         }
131
132         free(dst);
133         cairo_surface_mark_dirty(surface);
134
135         return 0;
136 }
137
138 void
139 tile_mask(cairo_t *cr, cairo_surface_t *surface,
140           int x, int y, int width, int height, int margin, int top_margin)
141 {
142         cairo_pattern_t *pattern;
143         cairo_matrix_t matrix;
144         int i, fx, fy, vmargin;
145
146         cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
147         pattern = cairo_pattern_create_for_surface (surface);
148         cairo_pattern_set_filter(pattern, CAIRO_FILTER_NEAREST);
149
150         for (i = 0; i < 4; i++) {
151                 fx = i & 1;
152                 fy = i >> 1;
153
154                 cairo_matrix_init_translate(&matrix,
155                                             -x + fx * (128 - width),
156                                             -y + fy * (128 - height));
157                 cairo_pattern_set_matrix(pattern, &matrix);
158
159                 if (fy)
160                         vmargin = margin;
161                 else
162                         vmargin = top_margin;
163
164                 cairo_reset_clip(cr);
165                 cairo_rectangle(cr,
166                                 x + fx * (width - margin),
167                                 y + fy * (height - vmargin),
168                                 margin, vmargin);
169                 cairo_clip (cr);
170                 cairo_mask(cr, pattern);
171         }
172
173         /* Top stretch */
174         cairo_matrix_init_translate(&matrix, 60, 0);
175         cairo_matrix_scale(&matrix, 8.0 / width, 1);
176         cairo_matrix_translate(&matrix, -x - width / 2, -y);
177         cairo_pattern_set_matrix(pattern, &matrix);
178         cairo_rectangle(cr, x + margin, y, width - 2 * margin, margin);
179
180         cairo_reset_clip(cr);
181         cairo_rectangle(cr,
182                         x + margin,
183                         y,
184                         width - 2 * margin, margin);
185         cairo_clip (cr);
186         cairo_mask(cr, pattern);
187
188         /* Bottom stretch */
189         cairo_matrix_translate(&matrix, 0, -height + 128);
190         cairo_pattern_set_matrix(pattern, &matrix);
191
192         cairo_reset_clip(cr);
193         cairo_rectangle(cr, x + margin, y + height - margin,
194                         width - 2 * margin, margin);
195         cairo_clip (cr);
196         cairo_mask(cr, pattern);
197
198         /* Left stretch */
199         cairo_matrix_init_translate(&matrix, 0, 60);
200         cairo_matrix_scale(&matrix, 1, 8.0 / height);
201         cairo_matrix_translate(&matrix, -x, -y - height / 2);
202         cairo_pattern_set_matrix(pattern, &matrix);
203         cairo_reset_clip(cr);
204         cairo_rectangle(cr, x, y + margin, margin, height - 2 * margin);
205         cairo_clip (cr);
206         cairo_mask(cr, pattern);
207
208         /* Right stretch */
209         cairo_matrix_translate(&matrix, -width + 128, 0);
210         cairo_pattern_set_matrix(pattern, &matrix);
211         cairo_rectangle(cr, x + width - margin, y + margin,
212                         margin, height - 2 * margin);
213         cairo_reset_clip(cr);
214         cairo_clip (cr);
215         cairo_mask(cr, pattern);
216
217         cairo_pattern_destroy(pattern);
218         cairo_reset_clip(cr);
219 }
220
221 void
222 tile_source(cairo_t *cr, cairo_surface_t *surface,
223             int x, int y, int width, int height, int margin, int top_margin)
224 {
225         cairo_pattern_t *pattern;
226         cairo_matrix_t matrix;
227         int i, fx, fy, vmargin;
228
229         cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
230         pattern = cairo_pattern_create_for_surface (surface);
231         cairo_pattern_set_filter(pattern, CAIRO_FILTER_NEAREST);
232         cairo_set_source(cr, pattern);
233         cairo_pattern_destroy(pattern);
234
235         for (i = 0; i < 4; i++) {
236                 fx = i & 1;
237                 fy = i >> 1;
238
239                 cairo_matrix_init_translate(&matrix,
240                                             -x + fx * (128 - width),
241                                             -y + fy * (128 - height));
242                 cairo_pattern_set_matrix(pattern, &matrix);
243
244                 if (fy)
245                         vmargin = margin;
246                 else
247                         vmargin = top_margin;
248
249                 cairo_rectangle(cr,
250                                 x + fx * (width - margin),
251                                 y + fy * (height - vmargin),
252                                 margin, vmargin);
253                 cairo_fill(cr);
254         }
255
256         /* Top stretch */
257         cairo_matrix_init_translate(&matrix, 60, 0);
258         cairo_matrix_scale(&matrix, 8.0 / (width - 2 * margin), 1);
259         cairo_matrix_translate(&matrix, -x - width / 2, -y);
260         cairo_pattern_set_matrix(pattern, &matrix);
261         cairo_rectangle(cr, x + margin, y, width - 2 * margin, top_margin);
262         cairo_fill(cr);
263
264         /* Bottom stretch */
265         cairo_matrix_translate(&matrix, 0, -height + 128);
266         cairo_pattern_set_matrix(pattern, &matrix);
267         cairo_rectangle(cr, x + margin, y + height - margin,
268                         width - 2 * margin, margin);
269         cairo_fill(cr);
270
271         /* Left stretch */
272         cairo_matrix_init_translate(&matrix, 0, 60);
273         cairo_matrix_scale(&matrix, 1, 8.0 / (height - margin - top_margin));
274         cairo_matrix_translate(&matrix, -x, -y - height / 2);
275         cairo_pattern_set_matrix(pattern, &matrix);
276         cairo_rectangle(cr, x, y + top_margin,
277                         margin, height - margin - top_margin);
278         cairo_fill(cr);
279
280         /* Right stretch */
281         cairo_matrix_translate(&matrix, -width + 128, 0);
282         cairo_pattern_set_matrix(pattern, &matrix);
283         cairo_rectangle(cr, x + width - margin, y + top_margin,
284                         margin, height - margin - top_margin);
285         cairo_fill(cr);
286 }
287
288 void
289 rounded_rect(cairo_t *cr, int x0, int y0, int x1, int y1, int radius)
290 {
291         cairo_move_to(cr, x0, y0 + radius);
292         cairo_arc(cr, x0 + radius, y0 + radius, radius, M_PI, 3 * M_PI / 2);
293         cairo_line_to(cr, x1 - radius, y0);
294         cairo_arc(cr, x1 - radius, y0 + radius, radius, 3 * M_PI / 2, 2 * M_PI);
295         cairo_line_to(cr, x1, y1 - radius);
296         cairo_arc(cr, x1 - radius, y1 - radius, radius, 0, M_PI / 2);
297         cairo_line_to(cr, x0 + radius, y1);
298         cairo_arc(cr, x0 + radius, y1 - radius, radius, M_PI / 2, M_PI);
299         cairo_close_path(cr);
300 }
301
302 cairo_surface_t *
303 load_cairo_surface(const char *filename)
304 {
305         pixman_image_t *image;
306         int width, height, stride;
307         void *data;
308
309         image = load_image(filename);
310         if (image == NULL) {
311                 return NULL;
312         }
313
314         data = pixman_image_get_data(image);
315         width = pixman_image_get_width(image);
316         height = pixman_image_get_height(image);
317         stride = pixman_image_get_stride(image);
318
319         return cairo_image_surface_create_for_data(data, CAIRO_FORMAT_ARGB32,
320                                                    width, height, stride);
321 }
322
323 void
324 theme_set_background_source(struct theme *t, cairo_t *cr, uint32_t flags)
325 {
326         cairo_pattern_t *pattern;
327
328         if (flags & THEME_FRAME_ACTIVE) {
329                 pattern = cairo_pattern_create_linear(16, 16, 16, 112);
330                 cairo_pattern_add_color_stop_rgb(pattern, 0.0, 1.0, 1.0, 1.0);
331                 cairo_pattern_add_color_stop_rgb(pattern, 0.2, 0.8, 0.8, 0.8);
332                 cairo_set_source(cr, pattern);
333                 cairo_pattern_destroy(pattern);
334         } else {
335                 cairo_set_source_rgba(cr, 0.75, 0.75, 0.75, 1);
336         }
337 }
338
339 struct theme *
340 theme_create(void)
341 {
342         struct theme *t;
343         cairo_t *cr;
344
345         t = malloc(sizeof *t);
346         if (t == NULL)
347                 return NULL;
348
349         t->margin = 32;
350         t->width = 6;
351         t->titlebar_height = 27;
352         t->frame_radius = 3;
353         t->shadow = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
354         cr = cairo_create(t->shadow);
355         cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
356         cairo_set_source_rgba(cr, 0, 0, 0, 1);
357         rounded_rect(cr, 32, 32, 96, 96, t->frame_radius);
358         cairo_fill(cr);
359         if (cairo_status (cr) != CAIRO_STATUS_SUCCESS)
360                 goto err_shadow;
361         cairo_destroy(cr);
362         if (blur_surface(t->shadow, 64) == -1)
363                 goto err_shadow;
364
365         t->active_frame =
366                 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
367         cr = cairo_create(t->active_frame);
368         cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
369
370         theme_set_background_source(t, cr, THEME_FRAME_ACTIVE);
371         rounded_rect(cr, 0, 0, 128, 128, t->frame_radius);
372         cairo_fill(cr);
373
374         if (cairo_status(cr) != CAIRO_STATUS_SUCCESS)
375                 goto err_active_frame;
376
377         cairo_destroy(cr);
378
379         t->inactive_frame =
380                 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
381         cr = cairo_create(t->inactive_frame);
382         cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
383         theme_set_background_source(t, cr, 0);
384         rounded_rect(cr, 0, 0, 128, 128, t->frame_radius);
385         cairo_fill(cr);
386
387         if (cairo_status (cr) != CAIRO_STATUS_SUCCESS)
388                 goto err_inactive_frame;
389
390         cairo_destroy(cr);
391
392         return t;
393
394  err_inactive_frame:
395         cairo_surface_destroy(t->inactive_frame);
396  err_active_frame:
397         cairo_surface_destroy(t->active_frame);
398  err_shadow:
399         cairo_surface_destroy(t->shadow);
400         free(t);
401         return NULL;
402 }
403
404 void
405 theme_destroy(struct theme *t)
406 {
407         cairo_surface_destroy(t->active_frame);
408         cairo_surface_destroy(t->inactive_frame);
409         cairo_surface_destroy(t->shadow);
410         free(t);
411 }
412
413 void
414 theme_render_frame(struct theme *t,
415                    cairo_t *cr, int width, int height,
416                    const char *title, uint32_t flags)
417 {
418         cairo_text_extents_t extents;
419         cairo_font_extents_t font_extents;
420         cairo_surface_t *source;
421         int x, y, margin, top_margin;
422
423         cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
424         cairo_set_source_rgba(cr, 0, 0, 0, 0);
425         cairo_paint(cr);
426
427         if (flags & THEME_FRAME_MAXIMIZED)
428                 margin = 0;
429         else {
430                 cairo_set_source_rgba(cr, 0, 0, 0, 0.45);
431                 tile_mask(cr, t->shadow,
432                           2, 2, width + 8, height + 8,
433                           64, 64);
434                 margin = t->margin;
435         }
436
437         if (flags & THEME_FRAME_ACTIVE)
438                 source = t->active_frame;
439         else
440                 source = t->inactive_frame;
441
442         if (title)
443                 top_margin = t->titlebar_height;
444         else
445                 top_margin = t->width;
446
447         tile_source(cr, source,
448                     margin, margin,
449                     width - margin * 2, height - margin * 2,
450                     t->width, top_margin);
451
452         if (title) {
453                 cairo_rectangle (cr, margin + t->width, margin,
454                                  width - (margin + t->width) * 2,
455                                  t->titlebar_height - t->width);
456                 cairo_clip(cr);
457
458                 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
459                 cairo_select_font_face(cr, "sans",
460                                        CAIRO_FONT_SLANT_NORMAL,
461                                        CAIRO_FONT_WEIGHT_BOLD);
462                 cairo_set_font_size(cr, 14);
463                 cairo_text_extents(cr, title, &extents);
464                 cairo_font_extents (cr, &font_extents);
465                 x = (width - extents.width) / 2;
466                 y = margin +
467                         (t->titlebar_height -
468                          font_extents.ascent - font_extents.descent) / 2 +
469                         font_extents.ascent;
470
471                 if (flags & THEME_FRAME_ACTIVE) {
472                         cairo_move_to(cr, x + 1, y  + 1);
473                         cairo_set_source_rgb(cr, 1, 1, 1);
474                         cairo_show_text(cr, title);
475                         cairo_move_to(cr, x, y);
476                         cairo_set_source_rgb(cr, 0, 0, 0);
477                         cairo_show_text(cr, title);
478                 } else {
479                         cairo_move_to(cr, x, y);
480                         cairo_set_source_rgb(cr, 0.4, 0.4, 0.4);
481                         cairo_show_text(cr, title);
482                 }
483         }
484 }
485
486 enum theme_location
487 theme_get_location(struct theme *t, int x, int y,
488                                 int width, int height, int flags)
489 {
490         int vlocation, hlocation, location;
491         const int grip_size = 8;
492         int margin, top_margin;
493
494         margin = (flags & THEME_FRAME_MAXIMIZED) ? 0 : t->margin;
495
496         if (flags & THEME_FRAME_NO_TITLE)
497                 top_margin = t->width;
498         else
499                 top_margin = t->titlebar_height;
500
501         if (x < margin)
502                 hlocation = THEME_LOCATION_EXTERIOR;
503         else if (margin <= x && x < margin + grip_size)
504                 hlocation = THEME_LOCATION_RESIZING_LEFT;
505         else if (x < width - margin - grip_size)
506                 hlocation = THEME_LOCATION_INTERIOR;
507         else if (x < width - margin)
508                 hlocation = THEME_LOCATION_RESIZING_RIGHT;
509         else
510                 hlocation = THEME_LOCATION_EXTERIOR;
511
512         if (y < margin)
513                 vlocation = THEME_LOCATION_EXTERIOR;
514         else if (margin <= y && y < margin + grip_size)
515                 vlocation = THEME_LOCATION_RESIZING_TOP;
516         else if (y < height - margin - grip_size)
517                 vlocation = THEME_LOCATION_INTERIOR;
518         else if (y < height - margin)
519                 vlocation = THEME_LOCATION_RESIZING_BOTTOM;
520         else
521                 vlocation = THEME_LOCATION_EXTERIOR;
522
523         location = vlocation | hlocation;
524         if (location & THEME_LOCATION_EXTERIOR)
525                 location = THEME_LOCATION_EXTERIOR;
526         if (location == THEME_LOCATION_INTERIOR &&
527             y < margin + top_margin)
528                 location = THEME_LOCATION_TITLEBAR;
529         else if (location == THEME_LOCATION_INTERIOR)
530                 location = THEME_LOCATION_CLIENT_AREA;
531
532         return location;
533 }