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