Kristian Høgsberg | ffd710e | 2008-12-02 15:15:01 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright © 2008 Kristian Høgsberg |
Kristian Høgsberg | 902865c | 2012-02-08 10:11:42 -0500 | [diff] [blame] | 3 | * Copyright © 2012 Intel Corporation |
Kristian Høgsberg | ffd710e | 2008-12-02 15:15:01 -0500 | [diff] [blame] | 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 | |
Daniel Stone | c228e23 | 2013-05-22 18:03:19 +0300 | [diff] [blame] | 24 | #include "config.h" |
Kristian Høgsberg | 3d5437c | 2012-02-08 12:46:57 -0500 | [diff] [blame] | 25 | |
Kristian Høgsberg | 2f2cfae | 2008-11-08 22:46:30 -0500 | [diff] [blame] | 26 | #include <stdint.h> |
| 27 | #include <stdlib.h> |
| 28 | #include <string.h> |
| 29 | #include <stdio.h> |
Kristian Høgsberg | 2f2cfae | 2008-11-08 22:46:30 -0500 | [diff] [blame] | 30 | #include <math.h> |
Boyan Ding | 850a514 | 2014-08-05 15:22:04 +0800 | [diff] [blame] | 31 | #include <wayland-util.h> |
Kristian Høgsberg | 2f2cfae | 2008-11-08 22:46:30 -0500 | [diff] [blame] | 32 | #include <cairo.h> |
| 33 | #include "cairo-util.h" |
| 34 | |
Kristian Høgsberg | 3c2360f | 2013-01-28 16:01:22 -0500 | [diff] [blame] | 35 | #include "image-loader.h" |
| 36 | #include "config-parser.h" |
Kristian Høgsberg | 3d5437c | 2012-02-08 12:46:57 -0500 | [diff] [blame] | 37 | |
Kristian Høgsberg | 8733026 | 2008-11-17 22:23:55 -0500 | [diff] [blame] | 38 | #define ARRAY_LENGTH(a) (sizeof (a) / sizeof (a)[0]) |
| 39 | |
Kristian Høgsberg | 2f2cfae | 2008-11-08 22:46:30 -0500 | [diff] [blame] | 40 | void |
Benjamin Franzke | 47eb8f4 | 2011-10-07 09:08:56 +0200 | [diff] [blame] | 41 | surface_flush_device(cairo_surface_t *surface) |
| 42 | { |
| 43 | cairo_device_t *device; |
| 44 | |
| 45 | device = cairo_surface_get_device(surface); |
| 46 | if (device) |
| 47 | cairo_device_flush(device); |
| 48 | } |
| 49 | |
Kristian Høgsberg | c0bf817 | 2013-07-25 15:05:35 -0700 | [diff] [blame] | 50 | static int |
Kristian Høgsberg | 10bdd29 | 2008-11-08 23:27:27 -0500 | [diff] [blame] | 51 | blur_surface(cairo_surface_t *surface, int margin) |
Kristian Høgsberg | 2f2cfae | 2008-11-08 22:46:30 -0500 | [diff] [blame] | 52 | { |
Kristian Høgsberg | 2f2cfae | 2008-11-08 22:46:30 -0500 | [diff] [blame] | 53 | int32_t width, height, stride, x, y, z, w; |
| 54 | uint8_t *src, *dst; |
| 55 | uint32_t *s, *d, a, p; |
Kristian Høgsberg | 8733026 | 2008-11-17 22:23:55 -0500 | [diff] [blame] | 56 | int i, j, k, size, half; |
Kristian Høgsberg | ec323d2 | 2012-03-21 01:07:49 -0400 | [diff] [blame] | 57 | uint32_t kernel[71]; |
Kristian Høgsberg | 2f2cfae | 2008-11-08 22:46:30 -0500 | [diff] [blame] | 58 | double f; |
| 59 | |
Kristian Høgsberg | 8733026 | 2008-11-17 22:23:55 -0500 | [diff] [blame] | 60 | size = ARRAY_LENGTH(kernel); |
Kristian Høgsberg | 2f2cfae | 2008-11-08 22:46:30 -0500 | [diff] [blame] | 61 | width = cairo_image_surface_get_width(surface); |
| 62 | height = cairo_image_surface_get_height(surface); |
| 63 | stride = cairo_image_surface_get_stride(surface); |
| 64 | src = cairo_image_surface_get_data(surface); |
| 65 | |
Kristian Høgsberg | 5fc96ff | 2009-09-12 15:58:48 -0400 | [diff] [blame] | 66 | dst = malloc(height * stride); |
Kristian Høgsberg | c0bf817 | 2013-07-25 15:05:35 -0700 | [diff] [blame] | 67 | if (dst == NULL) |
| 68 | return -1; |
Kristian Høgsberg | 2f2cfae | 2008-11-08 22:46:30 -0500 | [diff] [blame] | 69 | |
| 70 | half = size / 2; |
Kristian Høgsberg | 0cd8f6e | 2011-01-21 22:19:40 -0500 | [diff] [blame] | 71 | a = 0; |
Kristian Høgsberg | 2f2cfae | 2008-11-08 22:46:30 -0500 | [diff] [blame] | 72 | for (i = 0; i < size; i++) { |
| 73 | f = (i - half); |
Kristian Høgsberg | 49e868c | 2010-06-15 16:18:58 -0400 | [diff] [blame] | 74 | kernel[i] = exp(- f * f / ARRAY_LENGTH(kernel)) * 10000; |
Kristian Høgsberg | 0cd8f6e | 2011-01-21 22:19:40 -0500 | [diff] [blame] | 75 | a += kernel[i]; |
Kristian Høgsberg | 2f2cfae | 2008-11-08 22:46:30 -0500 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | for (i = 0; i < height; i++) { |
| 79 | s = (uint32_t *) (src + i * stride); |
| 80 | d = (uint32_t *) (dst + i * stride); |
| 81 | for (j = 0; j < width; j++) { |
Kristian Høgsberg | 8733026 | 2008-11-17 22:23:55 -0500 | [diff] [blame] | 82 | if (margin < j && j < width - margin) { |
| 83 | d[j] = s[j]; |
Kristian Høgsberg | 10bdd29 | 2008-11-08 23:27:27 -0500 | [diff] [blame] | 84 | continue; |
Kristian Høgsberg | 8733026 | 2008-11-17 22:23:55 -0500 | [diff] [blame] | 85 | } |
| 86 | |
Kristian Høgsberg | 2f2cfae | 2008-11-08 22:46:30 -0500 | [diff] [blame] | 87 | x = 0; |
| 88 | y = 0; |
| 89 | z = 0; |
| 90 | w = 0; |
| 91 | for (k = 0; k < size; k++) { |
| 92 | if (j - half + k < 0 || j - half + k >= width) |
| 93 | continue; |
| 94 | p = s[j - half + k]; |
| 95 | |
| 96 | x += (p >> 24) * kernel[k]; |
| 97 | y += ((p >> 16) & 0xff) * kernel[k]; |
| 98 | z += ((p >> 8) & 0xff) * kernel[k]; |
| 99 | w += (p & 0xff) * kernel[k]; |
| 100 | } |
| 101 | d[j] = (x / a << 24) | (y / a << 16) | (z / a << 8) | w / a; |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | for (i = 0; i < height; i++) { |
| 106 | s = (uint32_t *) (dst + i * stride); |
| 107 | d = (uint32_t *) (src + i * stride); |
| 108 | for (j = 0; j < width; j++) { |
Kristian Høgsberg | 8733026 | 2008-11-17 22:23:55 -0500 | [diff] [blame] | 109 | if (margin <= i && i < height - margin) { |
| 110 | d[j] = s[j]; |
Kristian Høgsberg | 10bdd29 | 2008-11-08 23:27:27 -0500 | [diff] [blame] | 111 | continue; |
Kristian Høgsberg | 8733026 | 2008-11-17 22:23:55 -0500 | [diff] [blame] | 112 | } |
| 113 | |
Kristian Høgsberg | 2f2cfae | 2008-11-08 22:46:30 -0500 | [diff] [blame] | 114 | x = 0; |
| 115 | y = 0; |
| 116 | z = 0; |
| 117 | w = 0; |
| 118 | for (k = 0; k < size; k++) { |
| 119 | if (i - half + k < 0 || i - half + k >= height) |
| 120 | continue; |
| 121 | s = (uint32_t *) (dst + (i - half + k) * stride); |
| 122 | p = s[j]; |
| 123 | |
| 124 | x += (p >> 24) * kernel[k]; |
| 125 | y += ((p >> 16) & 0xff) * kernel[k]; |
| 126 | z += ((p >> 8) & 0xff) * kernel[k]; |
| 127 | w += (p & 0xff) * kernel[k]; |
| 128 | } |
| 129 | d[j] = (x / a << 24) | (y / a << 16) | (z / a << 8) | w / a; |
| 130 | } |
| 131 | } |
| 132 | |
Kristian Høgsberg | 5fc96ff | 2009-09-12 15:58:48 -0400 | [diff] [blame] | 133 | free(dst); |
Kristian Høgsberg | dcb71b6 | 2010-06-15 17:16:35 -0400 | [diff] [blame] | 134 | cairo_surface_mark_dirty(surface); |
Kristian Høgsberg | c0bf817 | 2013-07-25 15:05:35 -0700 | [diff] [blame] | 135 | |
| 136 | return 0; |
Kristian Høgsberg | dcb71b6 | 2010-06-15 17:16:35 -0400 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | void |
Marek Chalupa | 0d7fe8d | 2014-10-29 14:51:22 +0100 | [diff] [blame] | 140 | render_shadow(cairo_t *cr, cairo_surface_t *surface, |
| 141 | int x, int y, int width, int height, int margin, int top_margin) |
Kristian Høgsberg | dcb71b6 | 2010-06-15 17:16:35 -0400 | [diff] [blame] | 142 | { |
| 143 | cairo_pattern_t *pattern; |
| 144 | cairo_matrix_t matrix; |
Marek Chalupa | eaea470 | 2014-10-29 14:51:23 +0100 | [diff] [blame^] | 145 | int i, fx, fy, shadow_height, shadow_width; |
Kristian Høgsberg | dcb71b6 | 2010-06-15 17:16:35 -0400 | [diff] [blame] | 146 | |
Marek Chalupa | 0d7fe8d | 2014-10-29 14:51:22 +0100 | [diff] [blame] | 147 | cairo_set_source_rgba(cr, 0, 0, 0, 0.45); |
Kristian Høgsberg | dcb71b6 | 2010-06-15 17:16:35 -0400 | [diff] [blame] | 148 | cairo_set_operator(cr, CAIRO_OPERATOR_OVER); |
| 149 | pattern = cairo_pattern_create_for_surface (surface); |
Kristian Høgsberg | 919fbf0 | 2012-04-03 10:53:15 -0400 | [diff] [blame] | 150 | cairo_pattern_set_filter(pattern, CAIRO_FILTER_NEAREST); |
Kristian Høgsberg | dcb71b6 | 2010-06-15 17:16:35 -0400 | [diff] [blame] | 151 | |
| 152 | for (i = 0; i < 4; i++) { |
Marek Chalupa | eaea470 | 2014-10-29 14:51:23 +0100 | [diff] [blame^] | 153 | /* when fy is set, then we are working with lower corners, |
| 154 | * when fx is set, then we are working with right corners |
| 155 | * |
| 156 | * 00 ------- 01 |
| 157 | * | | |
| 158 | * | | |
| 159 | * 10 ------- 11 |
| 160 | */ |
Kristian Høgsberg | dcb71b6 | 2010-06-15 17:16:35 -0400 | [diff] [blame] | 161 | fx = i & 1; |
| 162 | fy = i >> 1; |
| 163 | |
| 164 | cairo_matrix_init_translate(&matrix, |
| 165 | -x + fx * (128 - width), |
| 166 | -y + fy * (128 - height)); |
| 167 | cairo_pattern_set_matrix(pattern, &matrix); |
| 168 | |
Marek Chalupa | eaea470 | 2014-10-29 14:51:23 +0100 | [diff] [blame^] | 169 | shadow_width = margin; |
| 170 | shadow_height = fy ? margin : top_margin; |
| 171 | |
| 172 | /* if the shadows together are greater than the surface, we need |
| 173 | * to fix it - set the shadow size to the half of |
| 174 | * the size of surface. Also handle the case when the size is |
| 175 | * not divisible by 2. In that case we need one part of the |
| 176 | * shadow to be one pixel greater. !fy or !fx, respectively, |
| 177 | * will do the work. |
| 178 | */ |
| 179 | if (height < 2 * shadow_height) |
| 180 | shadow_height = (height + !fy) / 2; |
| 181 | |
| 182 | if (width < 2 * shadow_width) |
| 183 | shadow_width = (width + !fx) / 2; |
Kristian Høgsberg | 9a68624 | 2010-08-18 15:28:04 -0400 | [diff] [blame] | 184 | |
Kristian Høgsberg | dcb71b6 | 2010-06-15 17:16:35 -0400 | [diff] [blame] | 185 | cairo_reset_clip(cr); |
| 186 | cairo_rectangle(cr, |
Marek Chalupa | eaea470 | 2014-10-29 14:51:23 +0100 | [diff] [blame^] | 187 | x + fx * (width - shadow_width), |
| 188 | y + fy * (height - shadow_height), |
| 189 | shadow_width, shadow_height); |
Kristian Høgsberg | dcb71b6 | 2010-06-15 17:16:35 -0400 | [diff] [blame] | 190 | cairo_clip (cr); |
| 191 | cairo_mask(cr, pattern); |
| 192 | } |
| 193 | |
Kristian Høgsberg | dcb71b6 | 2010-06-15 17:16:35 -0400 | [diff] [blame] | 194 | |
Marek Chalupa | eaea470 | 2014-10-29 14:51:23 +0100 | [diff] [blame^] | 195 | shadow_width = width - 2 * margin; |
| 196 | shadow_height = top_margin; |
| 197 | if (height < 2 * shadow_height) |
| 198 | shadow_height = height / 2; |
Kristian Høgsberg | dcb71b6 | 2010-06-15 17:16:35 -0400 | [diff] [blame] | 199 | |
Marek Chalupa | eaea470 | 2014-10-29 14:51:23 +0100 | [diff] [blame^] | 200 | if (shadow_width > 0 && shadow_height) { |
| 201 | /* Top stretch */ |
| 202 | cairo_matrix_init_translate(&matrix, 60, 0); |
| 203 | cairo_matrix_scale(&matrix, 8.0 / width, 1); |
| 204 | cairo_matrix_translate(&matrix, -x - width / 2, -y); |
| 205 | cairo_pattern_set_matrix(pattern, &matrix); |
| 206 | cairo_rectangle(cr, x + margin, y, shadow_width, shadow_height); |
Kristian Høgsberg | dcb71b6 | 2010-06-15 17:16:35 -0400 | [diff] [blame] | 207 | |
Marek Chalupa | eaea470 | 2014-10-29 14:51:23 +0100 | [diff] [blame^] | 208 | cairo_reset_clip(cr); |
| 209 | cairo_rectangle(cr, |
| 210 | x + margin, y, |
| 211 | shadow_width, shadow_height); |
| 212 | cairo_clip (cr); |
| 213 | cairo_mask(cr, pattern); |
Kristian Høgsberg | dcb71b6 | 2010-06-15 17:16:35 -0400 | [diff] [blame] | 214 | |
Marek Chalupa | eaea470 | 2014-10-29 14:51:23 +0100 | [diff] [blame^] | 215 | /* Bottom stretch */ |
| 216 | cairo_matrix_translate(&matrix, 0, -height + 128); |
| 217 | cairo_pattern_set_matrix(pattern, &matrix); |
Kristian Høgsberg | dcb71b6 | 2010-06-15 17:16:35 -0400 | [diff] [blame] | 218 | |
Marek Chalupa | eaea470 | 2014-10-29 14:51:23 +0100 | [diff] [blame^] | 219 | cairo_reset_clip(cr); |
| 220 | cairo_rectangle(cr, x + margin, y + height - margin, |
| 221 | shadow_width, margin); |
| 222 | cairo_clip (cr); |
| 223 | cairo_mask(cr, pattern); |
| 224 | } |
| 225 | |
| 226 | shadow_width = margin; |
| 227 | if (width < 2 * shadow_width) |
| 228 | shadow_width = width / 2; |
| 229 | |
| 230 | shadow_height = height - margin - top_margin; |
| 231 | |
| 232 | /* if height is smaller than sum of margins, |
| 233 | * then the shadow is already done by the corners */ |
| 234 | if (shadow_height > 0 && shadow_width) { |
| 235 | /* Left stretch */ |
| 236 | cairo_matrix_init_translate(&matrix, 0, 60); |
| 237 | cairo_matrix_scale(&matrix, 1, 8.0 / height); |
| 238 | cairo_matrix_translate(&matrix, -x, -y - height / 2); |
| 239 | cairo_pattern_set_matrix(pattern, &matrix); |
| 240 | cairo_reset_clip(cr); |
| 241 | cairo_rectangle(cr, x, y + top_margin, |
| 242 | shadow_width, shadow_height); |
| 243 | cairo_clip (cr); |
| 244 | cairo_mask(cr, pattern); |
| 245 | |
| 246 | /* Right stretch */ |
| 247 | cairo_matrix_translate(&matrix, -width + 128, 0); |
| 248 | cairo_pattern_set_matrix(pattern, &matrix); |
| 249 | cairo_rectangle(cr, x + width - shadow_width, y + top_margin, |
| 250 | shadow_width, shadow_height); |
| 251 | cairo_reset_clip(cr); |
| 252 | cairo_clip (cr); |
| 253 | cairo_mask(cr, pattern); |
| 254 | } |
Kristian Høgsberg | dcb71b6 | 2010-06-15 17:16:35 -0400 | [diff] [blame] | 255 | |
| 256 | cairo_pattern_destroy(pattern); |
| 257 | cairo_reset_clip(cr); |
| 258 | } |
| 259 | |
| 260 | void |
| 261 | tile_source(cairo_t *cr, cairo_surface_t *surface, |
Kristian Høgsberg | 9a68624 | 2010-08-18 15:28:04 -0400 | [diff] [blame] | 262 | int x, int y, int width, int height, int margin, int top_margin) |
Kristian Høgsberg | dcb71b6 | 2010-06-15 17:16:35 -0400 | [diff] [blame] | 263 | { |
| 264 | cairo_pattern_t *pattern; |
| 265 | cairo_matrix_t matrix; |
Kristian Høgsberg | 9a68624 | 2010-08-18 15:28:04 -0400 | [diff] [blame] | 266 | int i, fx, fy, vmargin; |
Kristian Høgsberg | dcb71b6 | 2010-06-15 17:16:35 -0400 | [diff] [blame] | 267 | |
| 268 | cairo_set_operator(cr, CAIRO_OPERATOR_OVER); |
| 269 | pattern = cairo_pattern_create_for_surface (surface); |
Kristian Høgsberg | 919fbf0 | 2012-04-03 10:53:15 -0400 | [diff] [blame] | 270 | cairo_pattern_set_filter(pattern, CAIRO_FILTER_NEAREST); |
Kristian Høgsberg | dcb71b6 | 2010-06-15 17:16:35 -0400 | [diff] [blame] | 271 | cairo_set_source(cr, pattern); |
| 272 | cairo_pattern_destroy(pattern); |
| 273 | |
| 274 | for (i = 0; i < 4; i++) { |
| 275 | fx = i & 1; |
| 276 | fy = i >> 1; |
| 277 | |
| 278 | cairo_matrix_init_translate(&matrix, |
| 279 | -x + fx * (128 - width), |
| 280 | -y + fy * (128 - height)); |
| 281 | cairo_pattern_set_matrix(pattern, &matrix); |
| 282 | |
Kristian Høgsberg | 9a68624 | 2010-08-18 15:28:04 -0400 | [diff] [blame] | 283 | if (fy) |
| 284 | vmargin = margin; |
| 285 | else |
| 286 | vmargin = top_margin; |
| 287 | |
Kristian Høgsberg | dcb71b6 | 2010-06-15 17:16:35 -0400 | [diff] [blame] | 288 | cairo_rectangle(cr, |
| 289 | x + fx * (width - margin), |
Kristian Høgsberg | 9a68624 | 2010-08-18 15:28:04 -0400 | [diff] [blame] | 290 | y + fy * (height - vmargin), |
| 291 | margin, vmargin); |
Kristian Høgsberg | dcb71b6 | 2010-06-15 17:16:35 -0400 | [diff] [blame] | 292 | cairo_fill(cr); |
| 293 | } |
| 294 | |
Tiago Vignatti | 0a26641 | 2012-02-09 19:06:56 +0200 | [diff] [blame] | 295 | /* Top stretch */ |
Kristian Høgsberg | 126f855 | 2012-03-21 12:37:04 -0400 | [diff] [blame] | 296 | cairo_matrix_init_translate(&matrix, 60, 0); |
| 297 | cairo_matrix_scale(&matrix, 8.0 / (width - 2 * margin), 1); |
Kristian Høgsberg | dcb71b6 | 2010-06-15 17:16:35 -0400 | [diff] [blame] | 298 | cairo_matrix_translate(&matrix, -x - width / 2, -y); |
| 299 | cairo_pattern_set_matrix(pattern, &matrix); |
Kristian Høgsberg | 9a68624 | 2010-08-18 15:28:04 -0400 | [diff] [blame] | 300 | cairo_rectangle(cr, x + margin, y, width - 2 * margin, top_margin); |
Kristian Høgsberg | dcb71b6 | 2010-06-15 17:16:35 -0400 | [diff] [blame] | 301 | cairo_fill(cr); |
| 302 | |
Tiago Vignatti | 0a26641 | 2012-02-09 19:06:56 +0200 | [diff] [blame] | 303 | /* Bottom stretch */ |
Kristian Høgsberg | dcb71b6 | 2010-06-15 17:16:35 -0400 | [diff] [blame] | 304 | cairo_matrix_translate(&matrix, 0, -height + 128); |
| 305 | cairo_pattern_set_matrix(pattern, &matrix); |
| 306 | cairo_rectangle(cr, x + margin, y + height - margin, |
| 307 | width - 2 * margin, margin); |
| 308 | cairo_fill(cr); |
| 309 | |
Tiago Vignatti | 0a26641 | 2012-02-09 19:06:56 +0200 | [diff] [blame] | 310 | /* Left stretch */ |
Kristian Høgsberg | 126f855 | 2012-03-21 12:37:04 -0400 | [diff] [blame] | 311 | cairo_matrix_init_translate(&matrix, 0, 60); |
| 312 | cairo_matrix_scale(&matrix, 1, 8.0 / (height - margin - top_margin)); |
Kristian Høgsberg | dcb71b6 | 2010-06-15 17:16:35 -0400 | [diff] [blame] | 313 | cairo_matrix_translate(&matrix, -x, -y - height / 2); |
| 314 | cairo_pattern_set_matrix(pattern, &matrix); |
Kristian Høgsberg | 9a68624 | 2010-08-18 15:28:04 -0400 | [diff] [blame] | 315 | cairo_rectangle(cr, x, y + top_margin, |
| 316 | margin, height - margin - top_margin); |
Kristian Høgsberg | dcb71b6 | 2010-06-15 17:16:35 -0400 | [diff] [blame] | 317 | cairo_fill(cr); |
| 318 | |
Tiago Vignatti | 0a26641 | 2012-02-09 19:06:56 +0200 | [diff] [blame] | 319 | /* Right stretch */ |
Kristian Høgsberg | dcb71b6 | 2010-06-15 17:16:35 -0400 | [diff] [blame] | 320 | cairo_matrix_translate(&matrix, -width + 128, 0); |
| 321 | cairo_pattern_set_matrix(pattern, &matrix); |
Kristian Høgsberg | 9a68624 | 2010-08-18 15:28:04 -0400 | [diff] [blame] | 322 | cairo_rectangle(cr, x + width - margin, y + top_margin, |
| 323 | margin, height - margin - top_margin); |
Kristian Høgsberg | dcb71b6 | 2010-06-15 17:16:35 -0400 | [diff] [blame] | 324 | cairo_fill(cr); |
Kristian Høgsberg | 2f2cfae | 2008-11-08 22:46:30 -0500 | [diff] [blame] | 325 | } |
Kristian Høgsberg | 1e164b9 | 2011-09-13 14:47:46 -0400 | [diff] [blame] | 326 | |
| 327 | void |
| 328 | rounded_rect(cairo_t *cr, int x0, int y0, int x1, int y1, int radius) |
| 329 | { |
| 330 | cairo_move_to(cr, x0, y0 + radius); |
| 331 | cairo_arc(cr, x0 + radius, y0 + radius, radius, M_PI, 3 * M_PI / 2); |
| 332 | cairo_line_to(cr, x1 - radius, y0); |
| 333 | cairo_arc(cr, x1 - radius, y0 + radius, radius, 3 * M_PI / 2, 2 * M_PI); |
| 334 | cairo_line_to(cr, x1, y1 - radius); |
| 335 | cairo_arc(cr, x1 - radius, y1 - radius, radius, 0, M_PI / 2); |
| 336 | cairo_line_to(cr, x0 + radius, y1); |
| 337 | cairo_arc(cr, x0 + radius, y1 - radius, radius, M_PI / 2, M_PI); |
| 338 | cairo_close_path(cr); |
| 339 | } |
Kristian Høgsberg | 27d3866 | 2011-10-20 13:11:12 -0400 | [diff] [blame] | 340 | |
Kristian Høgsberg | d654876 | 2012-01-25 15:43:48 -0500 | [diff] [blame] | 341 | cairo_surface_t * |
Kristian Høgsberg | f02a649 | 2012-03-12 01:05:25 -0400 | [diff] [blame] | 342 | load_cairo_surface(const char *filename) |
Kristian Høgsberg | d654876 | 2012-01-25 15:43:48 -0500 | [diff] [blame] | 343 | { |
Kristian Høgsberg | f02a649 | 2012-03-12 01:05:25 -0400 | [diff] [blame] | 344 | pixman_image_t *image; |
| 345 | int width, height, stride; |
| 346 | void *data; |
Kristian Høgsberg | d654876 | 2012-01-25 15:43:48 -0500 | [diff] [blame] | 347 | |
Kristian Høgsberg | f02a649 | 2012-03-12 01:05:25 -0400 | [diff] [blame] | 348 | image = load_image(filename); |
Ustun Ergenoglu | 6dc0f86 | 2012-03-14 22:07:58 +0200 | [diff] [blame] | 349 | if (image == NULL) { |
| 350 | return NULL; |
| 351 | } |
| 352 | |
Kristian Høgsberg | f02a649 | 2012-03-12 01:05:25 -0400 | [diff] [blame] | 353 | data = pixman_image_get_data(image); |
| 354 | width = pixman_image_get_width(image); |
| 355 | height = pixman_image_get_height(image); |
| 356 | stride = pixman_image_get_stride(image); |
Kristian Høgsberg | d654876 | 2012-01-25 15:43:48 -0500 | [diff] [blame] | 357 | |
Kristian Høgsberg | f02a649 | 2012-03-12 01:05:25 -0400 | [diff] [blame] | 358 | return cairo_image_surface_create_for_data(data, CAIRO_FORMAT_ARGB32, |
| 359 | width, height, stride); |
Kristian Høgsberg | d654876 | 2012-01-25 15:43:48 -0500 | [diff] [blame] | 360 | } |
Kristian Høgsberg | 42abdf5 | 2012-05-15 22:14:27 -0400 | [diff] [blame] | 361 | |
Kristian Høgsberg | c680e90 | 2013-10-23 21:49:30 -0700 | [diff] [blame] | 362 | void |
| 363 | theme_set_background_source(struct theme *t, cairo_t *cr, uint32_t flags) |
| 364 | { |
| 365 | cairo_pattern_t *pattern; |
| 366 | |
| 367 | if (flags & THEME_FRAME_ACTIVE) { |
| 368 | pattern = cairo_pattern_create_linear(16, 16, 16, 112); |
| 369 | cairo_pattern_add_color_stop_rgb(pattern, 0.0, 1.0, 1.0, 1.0); |
| 370 | cairo_pattern_add_color_stop_rgb(pattern, 0.2, 0.8, 0.8, 0.8); |
| 371 | cairo_set_source(cr, pattern); |
| 372 | cairo_pattern_destroy(pattern); |
| 373 | } else { |
| 374 | cairo_set_source_rgba(cr, 0.75, 0.75, 0.75, 1); |
| 375 | } |
| 376 | } |
| 377 | |
Kristian Høgsberg | 5adb480 | 2012-05-15 22:25:28 -0400 | [diff] [blame] | 378 | struct theme * |
| 379 | theme_create(void) |
Kristian Høgsberg | 42abdf5 | 2012-05-15 22:14:27 -0400 | [diff] [blame] | 380 | { |
Kristian Høgsberg | 5adb480 | 2012-05-15 22:25:28 -0400 | [diff] [blame] | 381 | struct theme *t; |
Kristian Høgsberg | 42abdf5 | 2012-05-15 22:14:27 -0400 | [diff] [blame] | 382 | cairo_t *cr; |
Kristian Høgsberg | 42abdf5 | 2012-05-15 22:14:27 -0400 | [diff] [blame] | 383 | |
Kristian Høgsberg | 5adb480 | 2012-05-15 22:25:28 -0400 | [diff] [blame] | 384 | t = malloc(sizeof *t); |
Kristian Høgsberg | c0bf817 | 2013-07-25 15:05:35 -0700 | [diff] [blame] | 385 | if (t == NULL) |
| 386 | return NULL; |
| 387 | |
Kristian Høgsberg | 42abdf5 | 2012-05-15 22:14:27 -0400 | [diff] [blame] | 388 | t->margin = 32; |
| 389 | t->width = 6; |
| 390 | t->titlebar_height = 27; |
| 391 | t->frame_radius = 3; |
| 392 | t->shadow = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128); |
| 393 | cr = cairo_create(t->shadow); |
| 394 | cairo_set_operator(cr, CAIRO_OPERATOR_OVER); |
| 395 | cairo_set_source_rgba(cr, 0, 0, 0, 1); |
| 396 | rounded_rect(cr, 32, 32, 96, 96, t->frame_radius); |
| 397 | cairo_fill(cr); |
Kristian Høgsberg | c0bf817 | 2013-07-25 15:05:35 -0700 | [diff] [blame] | 398 | if (cairo_status (cr) != CAIRO_STATUS_SUCCESS) |
| 399 | goto err_shadow; |
Kristian Høgsberg | 42abdf5 | 2012-05-15 22:14:27 -0400 | [diff] [blame] | 400 | cairo_destroy(cr); |
Kristian Høgsberg | c0bf817 | 2013-07-25 15:05:35 -0700 | [diff] [blame] | 401 | if (blur_surface(t->shadow, 64) == -1) |
| 402 | goto err_shadow; |
Kristian Høgsberg | 42abdf5 | 2012-05-15 22:14:27 -0400 | [diff] [blame] | 403 | |
| 404 | t->active_frame = |
| 405 | cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128); |
| 406 | cr = cairo_create(t->active_frame); |
| 407 | cairo_set_operator(cr, CAIRO_OPERATOR_OVER); |
| 408 | |
Kristian Høgsberg | c680e90 | 2013-10-23 21:49:30 -0700 | [diff] [blame] | 409 | theme_set_background_source(t, cr, THEME_FRAME_ACTIVE); |
Kristian Høgsberg | 42abdf5 | 2012-05-15 22:14:27 -0400 | [diff] [blame] | 410 | rounded_rect(cr, 0, 0, 128, 128, t->frame_radius); |
| 411 | cairo_fill(cr); |
Kristian Høgsberg | c0bf817 | 2013-07-25 15:05:35 -0700 | [diff] [blame] | 412 | |
| 413 | if (cairo_status(cr) != CAIRO_STATUS_SUCCESS) |
| 414 | goto err_active_frame; |
| 415 | |
Kristian Høgsberg | 42abdf5 | 2012-05-15 22:14:27 -0400 | [diff] [blame] | 416 | cairo_destroy(cr); |
| 417 | |
| 418 | t->inactive_frame = |
| 419 | cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128); |
| 420 | cr = cairo_create(t->inactive_frame); |
| 421 | cairo_set_operator(cr, CAIRO_OPERATOR_OVER); |
Kristian Høgsberg | c680e90 | 2013-10-23 21:49:30 -0700 | [diff] [blame] | 422 | theme_set_background_source(t, cr, 0); |
Kristian Høgsberg | 42abdf5 | 2012-05-15 22:14:27 -0400 | [diff] [blame] | 423 | rounded_rect(cr, 0, 0, 128, 128, t->frame_radius); |
| 424 | cairo_fill(cr); |
Kristian Høgsberg | c0bf817 | 2013-07-25 15:05:35 -0700 | [diff] [blame] | 425 | |
| 426 | if (cairo_status (cr) != CAIRO_STATUS_SUCCESS) |
| 427 | goto err_inactive_frame; |
| 428 | |
Kristian Høgsberg | 42abdf5 | 2012-05-15 22:14:27 -0400 | [diff] [blame] | 429 | cairo_destroy(cr); |
Kristian Høgsberg | 5adb480 | 2012-05-15 22:25:28 -0400 | [diff] [blame] | 430 | |
| 431 | return t; |
Kristian Høgsberg | c0bf817 | 2013-07-25 15:05:35 -0700 | [diff] [blame] | 432 | |
| 433 | err_inactive_frame: |
| 434 | cairo_surface_destroy(t->inactive_frame); |
| 435 | err_active_frame: |
| 436 | cairo_surface_destroy(t->active_frame); |
| 437 | err_shadow: |
| 438 | cairo_surface_destroy(t->shadow); |
| 439 | free(t); |
| 440 | return NULL; |
Kristian Høgsberg | 42abdf5 | 2012-05-15 22:14:27 -0400 | [diff] [blame] | 441 | } |
| 442 | |
| 443 | void |
Kristian Høgsberg | 5adb480 | 2012-05-15 22:25:28 -0400 | [diff] [blame] | 444 | theme_destroy(struct theme *t) |
Kristian Høgsberg | 42abdf5 | 2012-05-15 22:14:27 -0400 | [diff] [blame] | 445 | { |
| 446 | cairo_surface_destroy(t->active_frame); |
| 447 | cairo_surface_destroy(t->inactive_frame); |
| 448 | cairo_surface_destroy(t->shadow); |
Kristian Høgsberg | 5adb480 | 2012-05-15 22:25:28 -0400 | [diff] [blame] | 449 | free(t); |
| 450 | } |
| 451 | |
| 452 | void |
Scott Moreau | c6a7e4b | 2012-09-28 02:45:06 -0600 | [diff] [blame] | 453 | theme_render_frame(struct theme *t, |
Kristian Høgsberg | 5adb480 | 2012-05-15 22:25:28 -0400 | [diff] [blame] | 454 | cairo_t *cr, int width, int height, |
Boyan Ding | 850a514 | 2014-08-05 15:22:04 +0800 | [diff] [blame] | 455 | const char *title, struct wl_list *buttons, |
| 456 | uint32_t flags) |
Kristian Høgsberg | 5adb480 | 2012-05-15 22:25:28 -0400 | [diff] [blame] | 457 | { |
| 458 | cairo_text_extents_t extents; |
| 459 | cairo_font_extents_t font_extents; |
| 460 | cairo_surface_t *source; |
Kristian Høgsberg | 89f4bc4 | 2013-10-23 22:12:13 -0700 | [diff] [blame] | 461 | int x, y, margin, top_margin; |
Kristian Høgsberg | 5adb480 | 2012-05-15 22:25:28 -0400 | [diff] [blame] | 462 | |
| 463 | cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE); |
| 464 | cairo_set_source_rgba(cr, 0, 0, 0, 0); |
| 465 | cairo_paint(cr); |
| 466 | |
Scott Moreau | c6a7e4b | 2012-09-28 02:45:06 -0600 | [diff] [blame] | 467 | if (flags & THEME_FRAME_MAXIMIZED) |
| 468 | margin = 0; |
| 469 | else { |
Marek Chalupa | 0d7fe8d | 2014-10-29 14:51:22 +0100 | [diff] [blame] | 470 | render_shadow(cr, t->shadow, |
| 471 | 2, 2, width + 8, height + 8, |
| 472 | 64, 64); |
Scott Moreau | c6a7e4b | 2012-09-28 02:45:06 -0600 | [diff] [blame] | 473 | margin = t->margin; |
| 474 | } |
Kristian Høgsberg | 5adb480 | 2012-05-15 22:25:28 -0400 | [diff] [blame] | 475 | |
| 476 | if (flags & THEME_FRAME_ACTIVE) |
| 477 | source = t->active_frame; |
| 478 | else |
| 479 | source = t->inactive_frame; |
| 480 | |
Boyan Ding | 850a514 | 2014-08-05 15:22:04 +0800 | [diff] [blame] | 481 | if (title || !wl_list_empty(buttons)) |
Kristian Høgsberg | 89f4bc4 | 2013-10-23 22:12:13 -0700 | [diff] [blame] | 482 | top_margin = t->titlebar_height; |
| 483 | else |
| 484 | top_margin = t->width; |
| 485 | |
Kristian Høgsberg | 5adb480 | 2012-05-15 22:25:28 -0400 | [diff] [blame] | 486 | tile_source(cr, source, |
Scott Moreau | c6a7e4b | 2012-09-28 02:45:06 -0600 | [diff] [blame] | 487 | margin, margin, |
| 488 | width - margin * 2, height - margin * 2, |
Kristian Høgsberg | 89f4bc4 | 2013-10-23 22:12:13 -0700 | [diff] [blame] | 489 | t->width, top_margin); |
Kristian Høgsberg | 5adb480 | 2012-05-15 22:25:28 -0400 | [diff] [blame] | 490 | |
Boyan Ding | 850a514 | 2014-08-05 15:22:04 +0800 | [diff] [blame] | 491 | if (title || !wl_list_empty(buttons)) { |
Kristian Høgsberg | 89f4bc4 | 2013-10-23 22:12:13 -0700 | [diff] [blame] | 492 | cairo_rectangle (cr, margin + t->width, margin, |
| 493 | width - (margin + t->width) * 2, |
| 494 | t->titlebar_height - t->width); |
| 495 | cairo_clip(cr); |
Martin Minarik | 5f3eddc | 2012-07-02 23:05:50 +0200 | [diff] [blame] | 496 | |
Kristian Høgsberg | 89f4bc4 | 2013-10-23 22:12:13 -0700 | [diff] [blame] | 497 | cairo_set_operator(cr, CAIRO_OPERATOR_OVER); |
| 498 | cairo_select_font_face(cr, "sans", |
| 499 | CAIRO_FONT_SLANT_NORMAL, |
| 500 | CAIRO_FONT_WEIGHT_BOLD); |
| 501 | cairo_set_font_size(cr, 14); |
| 502 | cairo_text_extents(cr, title, &extents); |
| 503 | cairo_font_extents (cr, &font_extents); |
| 504 | x = (width - extents.width) / 2; |
| 505 | y = margin + |
| 506 | (t->titlebar_height - |
| 507 | font_extents.ascent - font_extents.descent) / 2 + |
| 508 | font_extents.ascent; |
Kristian Høgsberg | 5adb480 | 2012-05-15 22:25:28 -0400 | [diff] [blame] | 509 | |
Kristian Høgsberg | 89f4bc4 | 2013-10-23 22:12:13 -0700 | [diff] [blame] | 510 | if (flags & THEME_FRAME_ACTIVE) { |
| 511 | cairo_move_to(cr, x + 1, y + 1); |
| 512 | cairo_set_source_rgb(cr, 1, 1, 1); |
| 513 | cairo_show_text(cr, title); |
| 514 | cairo_move_to(cr, x, y); |
| 515 | cairo_set_source_rgb(cr, 0, 0, 0); |
| 516 | cairo_show_text(cr, title); |
| 517 | } else { |
| 518 | cairo_move_to(cr, x, y); |
| 519 | cairo_set_source_rgb(cr, 0.4, 0.4, 0.4); |
| 520 | cairo_show_text(cr, title); |
| 521 | } |
Kristian Høgsberg | 5adb480 | 2012-05-15 22:25:28 -0400 | [diff] [blame] | 522 | } |
Kristian Høgsberg | 42abdf5 | 2012-05-15 22:14:27 -0400 | [diff] [blame] | 523 | } |
Kristian Høgsberg | f96e6c0 | 2012-05-22 16:38:53 -0400 | [diff] [blame] | 524 | |
| 525 | enum theme_location |
Scott Moreau | c6a7e4b | 2012-09-28 02:45:06 -0600 | [diff] [blame] | 526 | theme_get_location(struct theme *t, int x, int y, |
| 527 | int width, int height, int flags) |
Kristian Høgsberg | f96e6c0 | 2012-05-22 16:38:53 -0400 | [diff] [blame] | 528 | { |
| 529 | int vlocation, hlocation, location; |
Jasper St. Pierre | f11ad43 | 2014-04-28 11:19:30 -0400 | [diff] [blame] | 530 | int margin, top_margin, grip_size; |
Kristian Høgsberg | f96e6c0 | 2012-05-22 16:38:53 -0400 | [diff] [blame] | 531 | |
Jasper St. Pierre | f11ad43 | 2014-04-28 11:19:30 -0400 | [diff] [blame] | 532 | if (flags & THEME_FRAME_MAXIMIZED) { |
| 533 | margin = 0; |
| 534 | grip_size = 0; |
| 535 | } else { |
| 536 | margin = t->margin; |
| 537 | grip_size = 8; |
| 538 | } |
Scott Moreau | c6a7e4b | 2012-09-28 02:45:06 -0600 | [diff] [blame] | 539 | |
Kristian Høgsberg | 89f4bc4 | 2013-10-23 22:12:13 -0700 | [diff] [blame] | 540 | if (flags & THEME_FRAME_NO_TITLE) |
| 541 | top_margin = t->width; |
| 542 | else |
| 543 | top_margin = t->titlebar_height; |
| 544 | |
Scott Moreau | c6a7e4b | 2012-09-28 02:45:06 -0600 | [diff] [blame] | 545 | if (x < margin) |
Kristian Høgsberg | f96e6c0 | 2012-05-22 16:38:53 -0400 | [diff] [blame] | 546 | hlocation = THEME_LOCATION_EXTERIOR; |
Jasper St. Pierre | a4d9723 | 2014-04-28 11:19:29 -0400 | [diff] [blame] | 547 | else if (x < margin + grip_size) |
Kristian Høgsberg | f96e6c0 | 2012-05-22 16:38:53 -0400 | [diff] [blame] | 548 | hlocation = THEME_LOCATION_RESIZING_LEFT; |
Scott Moreau | c6a7e4b | 2012-09-28 02:45:06 -0600 | [diff] [blame] | 549 | else if (x < width - margin - grip_size) |
Kristian Høgsberg | f96e6c0 | 2012-05-22 16:38:53 -0400 | [diff] [blame] | 550 | hlocation = THEME_LOCATION_INTERIOR; |
Scott Moreau | c6a7e4b | 2012-09-28 02:45:06 -0600 | [diff] [blame] | 551 | else if (x < width - margin) |
Kristian Høgsberg | f96e6c0 | 2012-05-22 16:38:53 -0400 | [diff] [blame] | 552 | hlocation = THEME_LOCATION_RESIZING_RIGHT; |
| 553 | else |
| 554 | hlocation = THEME_LOCATION_EXTERIOR; |
| 555 | |
Scott Moreau | c6a7e4b | 2012-09-28 02:45:06 -0600 | [diff] [blame] | 556 | if (y < margin) |
Kristian Høgsberg | f96e6c0 | 2012-05-22 16:38:53 -0400 | [diff] [blame] | 557 | vlocation = THEME_LOCATION_EXTERIOR; |
Jasper St. Pierre | a4d9723 | 2014-04-28 11:19:29 -0400 | [diff] [blame] | 558 | else if (y < margin + grip_size) |
Kristian Høgsberg | f96e6c0 | 2012-05-22 16:38:53 -0400 | [diff] [blame] | 559 | vlocation = THEME_LOCATION_RESIZING_TOP; |
Scott Moreau | c6a7e4b | 2012-09-28 02:45:06 -0600 | [diff] [blame] | 560 | else if (y < height - margin - grip_size) |
Kristian Høgsberg | f96e6c0 | 2012-05-22 16:38:53 -0400 | [diff] [blame] | 561 | vlocation = THEME_LOCATION_INTERIOR; |
Scott Moreau | c6a7e4b | 2012-09-28 02:45:06 -0600 | [diff] [blame] | 562 | else if (y < height - margin) |
Kristian Høgsberg | f96e6c0 | 2012-05-22 16:38:53 -0400 | [diff] [blame] | 563 | vlocation = THEME_LOCATION_RESIZING_BOTTOM; |
| 564 | else |
| 565 | vlocation = THEME_LOCATION_EXTERIOR; |
| 566 | |
| 567 | location = vlocation | hlocation; |
| 568 | if (location & THEME_LOCATION_EXTERIOR) |
| 569 | location = THEME_LOCATION_EXTERIOR; |
| 570 | if (location == THEME_LOCATION_INTERIOR && |
Kristian Høgsberg | 89f4bc4 | 2013-10-23 22:12:13 -0700 | [diff] [blame] | 571 | y < margin + top_margin) |
Kristian Høgsberg | f96e6c0 | 2012-05-22 16:38:53 -0400 | [diff] [blame] | 572 | location = THEME_LOCATION_TITLEBAR; |
| 573 | else if (location == THEME_LOCATION_INTERIOR) |
| 574 | location = THEME_LOCATION_CLIENT_AREA; |
| 575 | |
| 576 | return location; |
| 577 | } |