Vasily Khoruzhick | 71c2dd3 | 2013-01-07 20:39:49 +0300 | [diff] [blame] | 1 | /* |
| 2 | * Copyright © 2012 Intel Corporation |
| 3 | * Copyright © 2013 Vasily Khoruzhick <anarsoul@gmail.com> |
| 4 | * |
| 5 | * Permission to use, copy, modify, distribute, and sell this software and |
| 6 | * its documentation for any purpose is hereby granted without fee, provided |
| 7 | * that the above copyright notice appear in all copies and that both that |
| 8 | * copyright notice and this permission notice appear in supporting |
| 9 | * documentation, and that the name of the copyright holders not be used in |
| 10 | * advertising or publicity pertaining to distribution of the software |
| 11 | * without specific, written prior permission. The copyright holders make |
| 12 | * no representations about the suitability of this software for any |
| 13 | * purpose. It is provided "as is" without express or implied warranty. |
| 14 | * |
| 15 | * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS |
| 16 | * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND |
| 17 | * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY |
| 18 | * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER |
| 19 | * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF |
| 20 | * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN |
| 21 | * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 22 | */ |
| 23 | |
| 24 | #define _GNU_SOURCE |
| 25 | |
| 26 | #include <errno.h> |
| 27 | #include <stdlib.h> |
| 28 | |
| 29 | #include "pixman-renderer.h" |
| 30 | |
Ander Conselvan de Oliveira | 8792ef1 | 2013-01-25 15:13:00 +0200 | [diff] [blame] | 31 | #include <linux/input.h> |
| 32 | |
Vasily Khoruzhick | 71c2dd3 | 2013-01-07 20:39:49 +0300 | [diff] [blame] | 33 | struct pixman_output_state { |
Ander Conselvan de Oliveira | 936effd | 2013-01-25 15:13:01 +0200 | [diff] [blame] | 34 | void *shadow_buffer; |
| 35 | pixman_image_t *shadow_image; |
Vasily Khoruzhick | 71c2dd3 | 2013-01-07 20:39:49 +0300 | [diff] [blame] | 36 | pixman_image_t *hw_buffer; |
| 37 | }; |
| 38 | |
| 39 | struct pixman_surface_state { |
| 40 | pixman_image_t *image; |
| 41 | struct weston_buffer_reference buffer_ref; |
| 42 | }; |
| 43 | |
| 44 | struct pixman_renderer { |
| 45 | struct weston_renderer base; |
Ander Conselvan de Oliveira | 8792ef1 | 2013-01-25 15:13:00 +0200 | [diff] [blame] | 46 | int repaint_debug; |
| 47 | pixman_image_t *debug_color; |
Vasily Khoruzhick | 71c2dd3 | 2013-01-07 20:39:49 +0300 | [diff] [blame] | 48 | }; |
| 49 | |
| 50 | static inline struct pixman_output_state * |
| 51 | get_output_state(struct weston_output *output) |
| 52 | { |
| 53 | return (struct pixman_output_state *)output->renderer_state; |
| 54 | } |
| 55 | |
| 56 | static inline struct pixman_surface_state * |
| 57 | get_surface_state(struct weston_surface *surface) |
| 58 | { |
| 59 | return (struct pixman_surface_state *)surface->renderer_state; |
| 60 | } |
| 61 | |
| 62 | static inline struct pixman_renderer * |
| 63 | get_renderer(struct weston_compositor *ec) |
| 64 | { |
| 65 | return (struct pixman_renderer *)ec->renderer; |
| 66 | } |
| 67 | |
| 68 | static int |
| 69 | pixman_renderer_read_pixels(struct weston_output *output, |
| 70 | pixman_format_code_t format, void *pixels, |
| 71 | uint32_t x, uint32_t y, |
| 72 | uint32_t width, uint32_t height) |
| 73 | { |
| 74 | struct pixman_output_state *po = get_output_state(output); |
| 75 | pixman_image_t *out_buf; |
| 76 | uint32_t cur_y; |
| 77 | |
| 78 | if (!po->hw_buffer) { |
| 79 | errno = ENODEV; |
| 80 | return -1; |
| 81 | } |
| 82 | |
| 83 | out_buf = pixman_image_create_bits(format, |
| 84 | width, |
| 85 | height, |
| 86 | pixels, |
| 87 | (PIXMAN_FORMAT_BPP(format) / 8) * width); |
| 88 | |
| 89 | /* Caller expects vflipped image */ |
| 90 | for (cur_y = y; cur_y < y + height; cur_y++) { |
| 91 | pixman_image_composite32(PIXMAN_OP_SRC, |
| 92 | po->hw_buffer, /* src */ |
| 93 | NULL /* mask */, |
| 94 | out_buf, /* dest */ |
| 95 | x, cur_y, /* src_x, src_y */ |
| 96 | 0, 0, /* mask_x, mask_y */ |
| 97 | 0, height - (cur_y - y), /* dest_x, dest_y */ |
| 98 | width, /* width */ |
| 99 | 1 /* height */); |
| 100 | } |
| 101 | |
| 102 | pixman_image_unref(out_buf); |
| 103 | |
| 104 | return 0; |
| 105 | } |
| 106 | |
Vasily Khoruzhick | 031fc87 | 2013-01-29 14:58:14 +0300 | [diff] [blame] | 107 | static void |
Alexander Larsson | 4ea9552 | 2013-05-22 14:41:37 +0200 | [diff] [blame] | 108 | box_scale(pixman_box32_t *dst, int scale) |
| 109 | { |
| 110 | dst->x1 *= scale; |
| 111 | dst->x2 *= scale; |
| 112 | dst->y1 *= scale; |
| 113 | dst->y2 *= scale; |
| 114 | } |
| 115 | |
| 116 | static void |
| 117 | scale_region (pixman_region32_t *region, int scale) |
| 118 | { |
| 119 | pixman_box32_t *rects, *scaled_rects; |
| 120 | int nrects, i; |
| 121 | |
| 122 | if (scale != 1) { |
| 123 | rects = pixman_region32_rectangles(region, &nrects); |
| 124 | scaled_rects = calloc(nrects, sizeof(pixman_box32_t)); |
| 125 | |
| 126 | for (i = 0; i < nrects; i++) { |
| 127 | scaled_rects[i] = rects[i]; |
| 128 | box_scale(&scaled_rects[i], scale); |
| 129 | } |
| 130 | pixman_region32_clear(region); |
| 131 | |
| 132 | pixman_region32_init_rects (region, scaled_rects, nrects); |
| 133 | free (scaled_rects); |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | static void |
Alexander Larsson | 1f206b4 | 2013-05-22 14:41:36 +0200 | [diff] [blame] | 138 | transform_region (pixman_region32_t *region, int width, int height, enum wl_output_transform transform) |
Vasily Khoruzhick | 031fc87 | 2013-01-29 14:58:14 +0300 | [diff] [blame] | 139 | { |
Alexander Larsson | 1f206b4 | 2013-05-22 14:41:36 +0200 | [diff] [blame] | 140 | pixman_box32_t *rects, *transformed_rects; |
| 141 | int nrects, i; |
| 142 | |
| 143 | if (transform == WL_OUTPUT_TRANSFORM_NORMAL) |
| 144 | return; |
| 145 | |
| 146 | rects = pixman_region32_rectangles(region, &nrects); |
| 147 | transformed_rects = calloc(nrects, sizeof(pixman_box32_t)); |
| 148 | |
| 149 | for (i = 0; i < nrects; i++) { |
| 150 | switch (transform) { |
| 151 | default: |
| 152 | case WL_OUTPUT_TRANSFORM_NORMAL: |
| 153 | transformed_rects[i].x1 = rects[i].x1; |
| 154 | transformed_rects[i].y1 = rects[i].y1; |
| 155 | transformed_rects[i].x2 = rects[i].x2; |
| 156 | transformed_rects[i].y2 = rects[i].y2; |
| 157 | break; |
| 158 | case WL_OUTPUT_TRANSFORM_90: |
| 159 | transformed_rects[i].x1 = height - rects[i].y2; |
| 160 | transformed_rects[i].y1 = rects[i].x1; |
| 161 | transformed_rects[i].x2 = height - rects[i].y1; |
| 162 | transformed_rects[i].y2 = rects[i].x2; |
| 163 | break; |
| 164 | case WL_OUTPUT_TRANSFORM_180: |
| 165 | transformed_rects[i].x1 = width - rects[i].x2; |
| 166 | transformed_rects[i].y1 = height - rects[i].y2; |
| 167 | transformed_rects[i].x2 = width - rects[i].x1; |
| 168 | transformed_rects[i].y2 = height - rects[i].y1; |
| 169 | break; |
| 170 | case WL_OUTPUT_TRANSFORM_270: |
| 171 | transformed_rects[i].x1 = rects[i].y1; |
| 172 | transformed_rects[i].y1 = width - rects[i].x2; |
| 173 | transformed_rects[i].x2 = rects[i].y2; |
| 174 | transformed_rects[i].y2 = width - rects[i].x1; |
| 175 | break; |
| 176 | case WL_OUTPUT_TRANSFORM_FLIPPED: |
| 177 | transformed_rects[i].x1 = width - rects[i].x2; |
| 178 | transformed_rects[i].y1 = rects[i].y1; |
| 179 | transformed_rects[i].x2 = width - rects[i].x1; |
| 180 | transformed_rects[i].y2 = rects[i].y2; |
| 181 | break; |
| 182 | case WL_OUTPUT_TRANSFORM_FLIPPED_90: |
| 183 | transformed_rects[i].x1 = height - rects[i].y2; |
| 184 | transformed_rects[i].y1 = width - rects[i].x2; |
| 185 | transformed_rects[i].x2 = height - rects[i].y1; |
| 186 | transformed_rects[i].y2 = width - rects[i].x1; |
| 187 | break; |
| 188 | case WL_OUTPUT_TRANSFORM_FLIPPED_180: |
| 189 | transformed_rects[i].x1 = rects[i].x1; |
| 190 | transformed_rects[i].y1 = height - rects[i].y2; |
| 191 | transformed_rects[i].x2 = rects[i].x2; |
| 192 | transformed_rects[i].y2 = height - rects[i].y1; |
| 193 | break; |
| 194 | case WL_OUTPUT_TRANSFORM_FLIPPED_270: |
| 195 | transformed_rects[i].x1 = rects[i].y1; |
| 196 | transformed_rects[i].y1 = rects[i].x1; |
| 197 | transformed_rects[i].x2 = rects[i].y2; |
| 198 | transformed_rects[i].y2 = rects[i].x2; |
| 199 | break; |
| 200 | } |
| 201 | } |
| 202 | pixman_region32_clear(region); |
| 203 | |
| 204 | pixman_region32_init_rects (region, transformed_rects, nrects); |
| 205 | free (transformed_rects); |
| 206 | } |
| 207 | |
| 208 | static void |
| 209 | region_global_to_output(struct weston_output *output, pixman_region32_t *region) |
| 210 | { |
| 211 | pixman_region32_translate(region, -output->x, -output->y); |
| 212 | transform_region (region, output->width, output->height, output->transform); |
Alexander Larsson | 4ea9552 | 2013-05-22 14:41:37 +0200 | [diff] [blame] | 213 | scale_region (region, output->scale); |
Vasily Khoruzhick | 031fc87 | 2013-01-29 14:58:14 +0300 | [diff] [blame] | 214 | } |
| 215 | |
Vasily Khoruzhick | f4a457f | 2013-01-28 22:40:29 +0300 | [diff] [blame] | 216 | #define D2F(v) pixman_double_to_fixed((double)v) |
| 217 | |
Vasily Khoruzhick | 71c2dd3 | 2013-01-07 20:39:49 +0300 | [diff] [blame] | 218 | static void |
Alexander Larsson | 1f206b4 | 2013-05-22 14:41:36 +0200 | [diff] [blame] | 219 | repaint_region(struct weston_surface *es, struct weston_output *output, |
| 220 | pixman_region32_t *region, pixman_region32_t *surf_region, |
| 221 | pixman_op_t pixman_op) |
Vasily Khoruzhick | 71c2dd3 | 2013-01-07 20:39:49 +0300 | [diff] [blame] | 222 | { |
Ander Conselvan de Oliveira | 8792ef1 | 2013-01-25 15:13:00 +0200 | [diff] [blame] | 223 | struct pixman_renderer *pr = |
| 224 | (struct pixman_renderer *) output->compositor->renderer; |
Vasily Khoruzhick | 71c2dd3 | 2013-01-07 20:39:49 +0300 | [diff] [blame] | 225 | struct pixman_surface_state *ps = get_surface_state(es); |
| 226 | struct pixman_output_state *po = get_output_state(output); |
| 227 | pixman_region32_t final_region; |
Vasily Khoruzhick | 71c2dd3 | 2013-01-07 20:39:49 +0300 | [diff] [blame] | 228 | float surface_x, surface_y; |
Alexander Larsson | 1f206b4 | 2013-05-22 14:41:36 +0200 | [diff] [blame] | 229 | pixman_transform_t transform; |
| 230 | pixman_fixed_t fw, fh; |
Vasily Khoruzhick | 71c2dd3 | 2013-01-07 20:39:49 +0300 | [diff] [blame] | 231 | |
| 232 | /* The final region to be painted is the intersection of |
| 233 | * 'region' and 'surf_region'. However, 'region' is in the global |
| 234 | * coordinates, and 'surf_region' is in the surface-local |
| 235 | * coordinates |
| 236 | */ |
| 237 | pixman_region32_init(&final_region); |
Alexander Larsson | 1f206b4 | 2013-05-22 14:41:36 +0200 | [diff] [blame] | 238 | if (surf_region) { |
| 239 | pixman_region32_copy(&final_region, surf_region); |
Vasily Khoruzhick | 71c2dd3 | 2013-01-07 20:39:49 +0300 | [diff] [blame] | 240 | |
Alexander Larsson | 1f206b4 | 2013-05-22 14:41:36 +0200 | [diff] [blame] | 241 | /* Convert from surface to global coordinates */ |
| 242 | if (!es->transform.enabled) { |
| 243 | pixman_region32_translate(&final_region, es->geometry.x, es->geometry.y); |
| 244 | } else { |
| 245 | weston_surface_to_global_float(es, 0, 0, &surface_x, &surface_y); |
| 246 | pixman_region32_translate(&final_region, (int)surface_x, (int)surface_y); |
| 247 | } |
Vasily Khoruzhick | f4a457f | 2013-01-28 22:40:29 +0300 | [diff] [blame] | 248 | |
Alexander Larsson | 1f206b4 | 2013-05-22 14:41:36 +0200 | [diff] [blame] | 249 | /* We need to paint the intersection */ |
| 250 | pixman_region32_intersect(&final_region, &final_region, region); |
Vasily Khoruzhick | f4a457f | 2013-01-28 22:40:29 +0300 | [diff] [blame] | 251 | } else { |
Alexander Larsson | 1f206b4 | 2013-05-22 14:41:36 +0200 | [diff] [blame] | 252 | /* If there is no surface region, just use the global region */ |
| 253 | pixman_region32_copy(&final_region, region); |
Vasily Khoruzhick | f4a457f | 2013-01-28 22:40:29 +0300 | [diff] [blame] | 254 | } |
Vasily Khoruzhick | 71c2dd3 | 2013-01-07 20:39:49 +0300 | [diff] [blame] | 255 | |
Alexander Larsson | 1f206b4 | 2013-05-22 14:41:36 +0200 | [diff] [blame] | 256 | /* Convert from global to output coord */ |
| 257 | region_global_to_output(output, &final_region); |
Vasily Khoruzhick | 71c2dd3 | 2013-01-07 20:39:49 +0300 | [diff] [blame] | 258 | |
Alexander Larsson | 1f206b4 | 2013-05-22 14:41:36 +0200 | [diff] [blame] | 259 | /* And clip to it */ |
| 260 | pixman_image_set_clip_region32 (po->shadow_image, &final_region); |
Ander Conselvan de Oliveira | 8792ef1 | 2013-01-25 15:13:00 +0200 | [diff] [blame] | 261 | |
Alexander Larsson | 1f206b4 | 2013-05-22 14:41:36 +0200 | [diff] [blame] | 262 | /* Set up the source transformation based on the surface |
Alexander Larsson | 4ea9552 | 2013-05-22 14:41:37 +0200 | [diff] [blame] | 263 | position, the output position/transform/scale and the client |
| 264 | specified buffer transform/scale */ |
Alexander Larsson | 1f206b4 | 2013-05-22 14:41:36 +0200 | [diff] [blame] | 265 | pixman_transform_init_identity(&transform); |
Alexander Larsson | 4ea9552 | 2013-05-22 14:41:37 +0200 | [diff] [blame] | 266 | pixman_transform_scale(&transform, NULL, |
| 267 | pixman_double_to_fixed ((double)1.0/output->scale), |
| 268 | pixman_double_to_fixed ((double)1.0/output->scale)); |
Ander Conselvan de Oliveira | 8792ef1 | 2013-01-25 15:13:00 +0200 | [diff] [blame] | 269 | |
Alexander Larsson | 1f206b4 | 2013-05-22 14:41:36 +0200 | [diff] [blame] | 270 | fw = pixman_int_to_fixed(output->width); |
| 271 | fh = pixman_int_to_fixed(output->height); |
| 272 | switch (output->transform) { |
| 273 | default: |
| 274 | case WL_OUTPUT_TRANSFORM_NORMAL: |
| 275 | case WL_OUTPUT_TRANSFORM_FLIPPED: |
| 276 | break; |
| 277 | case WL_OUTPUT_TRANSFORM_90: |
| 278 | case WL_OUTPUT_TRANSFORM_FLIPPED_90: |
| 279 | pixman_transform_rotate(&transform, NULL, 0, -pixman_fixed_1); |
| 280 | pixman_transform_translate(&transform, NULL, 0, fh); |
| 281 | break; |
| 282 | case WL_OUTPUT_TRANSFORM_180: |
| 283 | case WL_OUTPUT_TRANSFORM_FLIPPED_180: |
| 284 | pixman_transform_rotate(&transform, NULL, -pixman_fixed_1, 0); |
| 285 | pixman_transform_translate(&transform, NULL, fw, fh); |
| 286 | break; |
| 287 | case WL_OUTPUT_TRANSFORM_270: |
| 288 | case WL_OUTPUT_TRANSFORM_FLIPPED_270: |
| 289 | pixman_transform_rotate(&transform, NULL, 0, pixman_fixed_1); |
| 290 | pixman_transform_translate(&transform, NULL, fw, 0); |
| 291 | break; |
| 292 | } |
| 293 | |
| 294 | switch (output->transform) { |
| 295 | case WL_OUTPUT_TRANSFORM_FLIPPED: |
| 296 | case WL_OUTPUT_TRANSFORM_FLIPPED_90: |
| 297 | case WL_OUTPUT_TRANSFORM_FLIPPED_180: |
| 298 | case WL_OUTPUT_TRANSFORM_FLIPPED_270: |
| 299 | pixman_transform_scale(&transform, NULL, |
| 300 | pixman_int_to_fixed (-1), |
| 301 | pixman_int_to_fixed (1)); |
| 302 | pixman_transform_translate(&transform, NULL, fw, 0); |
| 303 | break; |
| 304 | } |
| 305 | |
| 306 | pixman_transform_translate(&transform, NULL, |
| 307 | pixman_double_to_fixed (output->x), |
| 308 | pixman_double_to_fixed (output->y)); |
| 309 | |
| 310 | if (es->transform.enabled) { |
| 311 | /* Pixman supports only 2D transform matrix, but Weston uses 3D, |
| 312 | * so we're omitting Z coordinate here |
| 313 | */ |
| 314 | pixman_transform_t surface_transform = {{ |
| 315 | { D2F(es->transform.matrix.d[0]), |
| 316 | D2F(es->transform.matrix.d[4]), |
| 317 | D2F(es->transform.matrix.d[12]), |
| 318 | }, |
| 319 | { D2F(es->transform.matrix.d[1]), |
| 320 | D2F(es->transform.matrix.d[5]), |
| 321 | D2F(es->transform.matrix.d[13]), |
| 322 | }, |
| 323 | { D2F(es->transform.matrix.d[3]), |
| 324 | D2F(es->transform.matrix.d[7]), |
| 325 | D2F(es->transform.matrix.d[15]), |
| 326 | } |
| 327 | }}; |
| 328 | |
| 329 | pixman_transform_invert(&surface_transform, &surface_transform); |
| 330 | pixman_transform_multiply (&transform, &surface_transform, &transform); |
| 331 | } else { |
| 332 | pixman_transform_translate(&transform, NULL, |
| 333 | pixman_double_to_fixed ((double)-es->geometry.x), |
| 334 | pixman_double_to_fixed ((double)-es->geometry.y)); |
| 335 | } |
| 336 | |
| 337 | |
| 338 | fw = pixman_int_to_fixed(es->geometry.width); |
| 339 | fh = pixman_int_to_fixed(es->geometry.height); |
| 340 | |
| 341 | switch (es->buffer_transform) { |
| 342 | case WL_OUTPUT_TRANSFORM_FLIPPED: |
| 343 | case WL_OUTPUT_TRANSFORM_FLIPPED_90: |
| 344 | case WL_OUTPUT_TRANSFORM_FLIPPED_180: |
| 345 | case WL_OUTPUT_TRANSFORM_FLIPPED_270: |
| 346 | pixman_transform_scale(&transform, NULL, |
| 347 | pixman_int_to_fixed (-1), |
| 348 | pixman_int_to_fixed (1)); |
| 349 | pixman_transform_translate(&transform, NULL, fw, 0); |
| 350 | break; |
| 351 | } |
| 352 | |
| 353 | switch (es->buffer_transform) { |
| 354 | default: |
| 355 | case WL_OUTPUT_TRANSFORM_NORMAL: |
| 356 | case WL_OUTPUT_TRANSFORM_FLIPPED: |
| 357 | break; |
| 358 | case WL_OUTPUT_TRANSFORM_90: |
| 359 | case WL_OUTPUT_TRANSFORM_FLIPPED_90: |
| 360 | pixman_transform_rotate(&transform, NULL, 0, pixman_fixed_1); |
| 361 | pixman_transform_translate(&transform, NULL, fh, 0); |
| 362 | break; |
| 363 | case WL_OUTPUT_TRANSFORM_180: |
| 364 | case WL_OUTPUT_TRANSFORM_FLIPPED_180: |
| 365 | pixman_transform_rotate(&transform, NULL, -pixman_fixed_1, 0); |
| 366 | pixman_transform_translate(&transform, NULL, fw, fh); |
| 367 | break; |
| 368 | case WL_OUTPUT_TRANSFORM_270: |
| 369 | case WL_OUTPUT_TRANSFORM_FLIPPED_270: |
| 370 | pixman_transform_rotate(&transform, NULL, 0, -pixman_fixed_1); |
| 371 | pixman_transform_translate(&transform, NULL, 0, fw); |
| 372 | break; |
| 373 | } |
| 374 | |
Alexander Larsson | 4ea9552 | 2013-05-22 14:41:37 +0200 | [diff] [blame] | 375 | pixman_transform_scale(&transform, NULL, |
| 376 | pixman_double_to_fixed ((double)es->buffer_scale), |
| 377 | pixman_double_to_fixed ((double)es->buffer_scale)); |
| 378 | |
Alexander Larsson | 1f206b4 | 2013-05-22 14:41:36 +0200 | [diff] [blame] | 379 | pixman_image_set_transform(ps->image, &transform); |
| 380 | |
Alexander Larsson | 4ea9552 | 2013-05-22 14:41:37 +0200 | [diff] [blame] | 381 | if (es->transform.enabled || output->scale != es->buffer_scale) |
Alexander Larsson | 1f206b4 | 2013-05-22 14:41:36 +0200 | [diff] [blame] | 382 | pixman_image_set_filter(ps->image, PIXMAN_FILTER_BILINEAR, NULL, 0); |
| 383 | else |
| 384 | pixman_image_set_filter(ps->image, PIXMAN_FILTER_NEAREST, NULL, 0); |
| 385 | |
| 386 | pixman_image_composite32(pixman_op, |
| 387 | ps->image, /* src */ |
| 388 | NULL /* mask */, |
| 389 | po->shadow_image, /* dest */ |
| 390 | 0, 0, /* src_x, src_y */ |
| 391 | 0, 0, /* mask_x, mask_y */ |
| 392 | 0, 0, /* dest_x, dest_y */ |
| 393 | pixman_image_get_width (po->shadow_image), /* width */ |
| 394 | pixman_image_get_height (po->shadow_image) /* height */); |
| 395 | |
| 396 | if (pr->repaint_debug) |
Ander Conselvan de Oliveira | 8792ef1 | 2013-01-25 15:13:00 +0200 | [diff] [blame] | 397 | pixman_image_composite32(PIXMAN_OP_OVER, |
Alexander Larsson | 1f206b4 | 2013-05-22 14:41:36 +0200 | [diff] [blame] | 398 | pr->debug_color, /* src */ |
| 399 | NULL /* mask */, |
| 400 | po->shadow_image, /* dest */ |
| 401 | 0, 0, /* src_x, src_y */ |
| 402 | 0, 0, /* mask_x, mask_y */ |
| 403 | 0, 0, /* dest_x, dest_y */ |
| 404 | pixman_image_get_width (po->shadow_image), /* width */ |
| 405 | pixman_image_get_height (po->shadow_image) /* height */); |
| 406 | |
| 407 | pixman_image_set_clip_region32 (po->shadow_image, NULL); |
| 408 | |
Vasily Khoruzhick | 71c2dd3 | 2013-01-07 20:39:49 +0300 | [diff] [blame] | 409 | pixman_region32_fini(&final_region); |
| 410 | } |
| 411 | |
| 412 | static void |
| 413 | draw_surface(struct weston_surface *es, struct weston_output *output, |
| 414 | pixman_region32_t *damage) /* in global coordinates */ |
| 415 | { |
| 416 | struct pixman_surface_state *ps = get_surface_state(es); |
| 417 | /* repaint bounding region in global coordinates: */ |
| 418 | pixman_region32_t repaint; |
| 419 | /* non-opaque region in surface coordinates: */ |
| 420 | pixman_region32_t surface_blend; |
| 421 | |
| 422 | /* No buffer attached */ |
| 423 | if (!ps->image) |
| 424 | return; |
| 425 | |
| 426 | pixman_region32_init(&repaint); |
| 427 | pixman_region32_intersect(&repaint, |
| 428 | &es->transform.boundingbox, damage); |
| 429 | pixman_region32_subtract(&repaint, &repaint, &es->clip); |
| 430 | |
| 431 | if (!pixman_region32_not_empty(&repaint)) |
| 432 | goto out; |
| 433 | |
| 434 | if (output->zoom.active) { |
| 435 | weston_log("pixman renderer does not support zoom\n"); |
| 436 | goto out; |
| 437 | } |
| 438 | |
Vasily Khoruzhick | f4a457f | 2013-01-28 22:40:29 +0300 | [diff] [blame] | 439 | /* TODO: Implement repaint_region_complex() using pixman_composite_trapezoids() */ |
| 440 | if (es->transform.enabled && |
| 441 | es->transform.matrix.type != WESTON_MATRIX_TRANSFORM_TRANSLATE) { |
Alexander Larsson | 1f206b4 | 2013-05-22 14:41:36 +0200 | [diff] [blame] | 442 | repaint_region(es, output, &repaint, NULL, PIXMAN_OP_OVER); |
Vasily Khoruzhick | f4a457f | 2013-01-28 22:40:29 +0300 | [diff] [blame] | 443 | } else { |
| 444 | /* blended region is whole surface minus opaque region: */ |
| 445 | pixman_region32_init_rect(&surface_blend, 0, 0, |
| 446 | es->geometry.width, es->geometry.height); |
| 447 | pixman_region32_subtract(&surface_blend, &surface_blend, &es->opaque); |
Vasily Khoruzhick | 71c2dd3 | 2013-01-07 20:39:49 +0300 | [diff] [blame] | 448 | |
Vasily Khoruzhick | f4a457f | 2013-01-28 22:40:29 +0300 | [diff] [blame] | 449 | if (pixman_region32_not_empty(&es->opaque)) { |
Alexander Larsson | 1f206b4 | 2013-05-22 14:41:36 +0200 | [diff] [blame] | 450 | repaint_region(es, output, &repaint, &es->opaque, PIXMAN_OP_SRC); |
Vasily Khoruzhick | f4a457f | 2013-01-28 22:40:29 +0300 | [diff] [blame] | 451 | } |
| 452 | |
| 453 | if (pixman_region32_not_empty(&surface_blend)) { |
Alexander Larsson | 1f206b4 | 2013-05-22 14:41:36 +0200 | [diff] [blame] | 454 | repaint_region(es, output, &repaint, &surface_blend, PIXMAN_OP_OVER); |
Vasily Khoruzhick | f4a457f | 2013-01-28 22:40:29 +0300 | [diff] [blame] | 455 | } |
| 456 | pixman_region32_fini(&surface_blend); |
Vasily Khoruzhick | 71c2dd3 | 2013-01-07 20:39:49 +0300 | [diff] [blame] | 457 | } |
| 458 | |
Vasily Khoruzhick | 71c2dd3 | 2013-01-07 20:39:49 +0300 | [diff] [blame] | 459 | |
| 460 | out: |
| 461 | pixman_region32_fini(&repaint); |
| 462 | } |
| 463 | static void |
| 464 | repaint_surfaces(struct weston_output *output, pixman_region32_t *damage) |
| 465 | { |
| 466 | struct weston_compositor *compositor = output->compositor; |
| 467 | struct weston_surface *surface; |
| 468 | |
| 469 | wl_list_for_each_reverse(surface, &compositor->surface_list, link) |
| 470 | if (surface->plane == &compositor->primary_plane) |
| 471 | draw_surface(surface, output, damage); |
| 472 | } |
| 473 | |
| 474 | static void |
Ander Conselvan de Oliveira | 936effd | 2013-01-25 15:13:01 +0200 | [diff] [blame] | 475 | copy_to_hw_buffer(struct weston_output *output, pixman_region32_t *region) |
| 476 | { |
| 477 | struct pixman_output_state *po = get_output_state(output); |
Alexander Larsson | 1f206b4 | 2013-05-22 14:41:36 +0200 | [diff] [blame] | 478 | pixman_region32_t output_region; |
Ander Conselvan de Oliveira | 936effd | 2013-01-25 15:13:01 +0200 | [diff] [blame] | 479 | |
Alexander Larsson | 1f206b4 | 2013-05-22 14:41:36 +0200 | [diff] [blame] | 480 | pixman_region32_init(&output_region); |
| 481 | pixman_region32_copy(&output_region, region); |
Ander Conselvan de Oliveira | 936effd | 2013-01-25 15:13:01 +0200 | [diff] [blame] | 482 | |
Alexander Larsson | 1f206b4 | 2013-05-22 14:41:36 +0200 | [diff] [blame] | 483 | region_global_to_output(output, &output_region); |
Ander Conselvan de Oliveira | 936effd | 2013-01-25 15:13:01 +0200 | [diff] [blame] | 484 | |
Alexander Larsson | 1f206b4 | 2013-05-22 14:41:36 +0200 | [diff] [blame] | 485 | pixman_image_set_clip_region32 (po->hw_buffer, &output_region); |
Ander Conselvan de Oliveira | 936effd | 2013-01-25 15:13:01 +0200 | [diff] [blame] | 486 | |
Alexander Larsson | 1f206b4 | 2013-05-22 14:41:36 +0200 | [diff] [blame] | 487 | pixman_image_composite32(PIXMAN_OP_SRC, |
| 488 | po->shadow_image, /* src */ |
| 489 | NULL /* mask */, |
| 490 | po->hw_buffer, /* dest */ |
| 491 | 0, 0, /* src_x, src_y */ |
| 492 | 0, 0, /* mask_x, mask_y */ |
| 493 | 0, 0, /* dest_x, dest_y */ |
| 494 | pixman_image_get_width (po->hw_buffer), /* width */ |
| 495 | pixman_image_get_height (po->hw_buffer) /* height */); |
| 496 | |
| 497 | pixman_image_set_clip_region32 (po->hw_buffer, NULL); |
Ander Conselvan de Oliveira | 936effd | 2013-01-25 15:13:01 +0200 | [diff] [blame] | 498 | } |
| 499 | |
| 500 | static void |
Vasily Khoruzhick | 71c2dd3 | 2013-01-07 20:39:49 +0300 | [diff] [blame] | 501 | pixman_renderer_repaint_output(struct weston_output *output, |
| 502 | pixman_region32_t *output_damage) |
| 503 | { |
| 504 | struct pixman_output_state *po = get_output_state(output); |
| 505 | |
| 506 | if (!po->hw_buffer) |
| 507 | return; |
| 508 | |
| 509 | repaint_surfaces(output, output_damage); |
Ander Conselvan de Oliveira | 936effd | 2013-01-25 15:13:01 +0200 | [diff] [blame] | 510 | copy_to_hw_buffer(output, output_damage); |
Vasily Khoruzhick | 71c2dd3 | 2013-01-07 20:39:49 +0300 | [diff] [blame] | 511 | |
| 512 | pixman_region32_copy(&output->previous_damage, output_damage); |
| 513 | wl_signal_emit(&output->frame_signal, output); |
| 514 | |
| 515 | /* Actual flip should be done by caller */ |
| 516 | } |
| 517 | |
| 518 | static void |
| 519 | pixman_renderer_flush_damage(struct weston_surface *surface) |
| 520 | { |
| 521 | /* No-op for pixman renderer */ |
| 522 | } |
| 523 | |
| 524 | static void |
| 525 | pixman_renderer_attach(struct weston_surface *es, struct wl_buffer *buffer) |
| 526 | { |
| 527 | struct pixman_surface_state *ps = get_surface_state(es); |
| 528 | pixman_format_code_t pixman_format; |
| 529 | |
| 530 | weston_buffer_reference(&ps->buffer_ref, buffer); |
| 531 | |
| 532 | if (ps->image) { |
| 533 | pixman_image_unref(ps->image); |
| 534 | ps->image = NULL; |
| 535 | } |
| 536 | |
| 537 | if (!buffer) |
| 538 | return; |
| 539 | |
| 540 | if (!wl_buffer_is_shm(buffer)) { |
| 541 | weston_log("Pixman renderer supports only SHM buffers\n"); |
| 542 | weston_buffer_reference(&ps->buffer_ref, NULL); |
| 543 | return; |
| 544 | } |
| 545 | |
| 546 | switch (wl_shm_buffer_get_format(buffer)) { |
| 547 | case WL_SHM_FORMAT_XRGB8888: |
| 548 | pixman_format = PIXMAN_x8r8g8b8; |
| 549 | break; |
| 550 | case WL_SHM_FORMAT_ARGB8888: |
| 551 | pixman_format = PIXMAN_a8r8g8b8; |
| 552 | break; |
| 553 | default: |
| 554 | weston_log("Unsupported SHM buffer format\n"); |
| 555 | weston_buffer_reference(&ps->buffer_ref, NULL); |
| 556 | return; |
| 557 | break; |
| 558 | } |
| 559 | ps->image = pixman_image_create_bits(pixman_format, |
| 560 | wl_shm_buffer_get_width(buffer), |
| 561 | wl_shm_buffer_get_height(buffer), |
| 562 | wl_shm_buffer_get_data(buffer), |
| 563 | wl_shm_buffer_get_stride(buffer)); |
| 564 | } |
| 565 | |
| 566 | static int |
| 567 | pixman_renderer_create_surface(struct weston_surface *surface) |
| 568 | { |
| 569 | struct pixman_surface_state *ps; |
| 570 | |
| 571 | ps = calloc(1, sizeof *ps); |
| 572 | if (!ps) |
| 573 | return -1; |
| 574 | |
| 575 | surface->renderer_state = ps; |
| 576 | |
| 577 | return 0; |
| 578 | } |
| 579 | |
| 580 | static void |
| 581 | pixman_renderer_surface_set_color(struct weston_surface *es, |
| 582 | float red, float green, float blue, float alpha) |
| 583 | { |
| 584 | struct pixman_surface_state *ps = get_surface_state(es); |
| 585 | pixman_color_t color; |
| 586 | |
| 587 | color.red = red * 0xffff; |
| 588 | color.green = green * 0xffff; |
| 589 | color.blue = blue * 0xffff; |
| 590 | color.alpha = alpha * 0xffff; |
| 591 | |
| 592 | if (ps->image) { |
| 593 | pixman_image_unref(ps->image); |
| 594 | ps->image = NULL; |
| 595 | } |
| 596 | |
| 597 | ps->image = pixman_image_create_solid_fill(&color); |
| 598 | } |
| 599 | |
| 600 | static void |
| 601 | pixman_renderer_destroy_surface(struct weston_surface *surface) |
| 602 | { |
| 603 | struct pixman_surface_state *ps = get_surface_state(surface); |
| 604 | |
| 605 | if (ps->image) { |
| 606 | pixman_image_unref(ps->image); |
| 607 | ps->image = NULL; |
| 608 | } |
| 609 | weston_buffer_reference(&ps->buffer_ref, NULL); |
| 610 | free(ps); |
| 611 | } |
| 612 | |
Vasily Khoruzhick | 52cfd61 | 2013-01-08 19:09:01 +0300 | [diff] [blame] | 613 | static void |
Vasily Khoruzhick | 71c2dd3 | 2013-01-07 20:39:49 +0300 | [diff] [blame] | 614 | pixman_renderer_destroy(struct weston_compositor *ec) |
| 615 | { |
| 616 | free(ec->renderer); |
| 617 | ec->renderer = NULL; |
| 618 | } |
| 619 | |
Ander Conselvan de Oliveira | 8792ef1 | 2013-01-25 15:13:00 +0200 | [diff] [blame] | 620 | static void |
Kristian Høgsberg | e314875 | 2013-05-06 23:19:49 -0400 | [diff] [blame] | 621 | debug_binding(struct weston_seat *seat, uint32_t time, uint32_t key, |
Ander Conselvan de Oliveira | 8792ef1 | 2013-01-25 15:13:00 +0200 | [diff] [blame] | 622 | void *data) |
| 623 | { |
| 624 | struct weston_compositor *ec = data; |
| 625 | struct pixman_renderer *pr = (struct pixman_renderer *) ec->renderer; |
| 626 | |
| 627 | pr->repaint_debug ^= 1; |
| 628 | |
| 629 | if (pr->repaint_debug) { |
| 630 | pixman_color_t red = { |
| 631 | 0x3fff, 0x0000, 0x0000, 0x3fff |
| 632 | }; |
| 633 | |
| 634 | pr->debug_color = pixman_image_create_solid_fill(&red); |
| 635 | } else { |
| 636 | pixman_image_unref(pr->debug_color); |
| 637 | weston_compositor_damage_all(ec); |
| 638 | } |
| 639 | } |
| 640 | |
Vasily Khoruzhick | 71c2dd3 | 2013-01-07 20:39:49 +0300 | [diff] [blame] | 641 | WL_EXPORT int |
| 642 | pixman_renderer_init(struct weston_compositor *ec) |
| 643 | { |
Ander Conselvan de Oliveira | 8792ef1 | 2013-01-25 15:13:00 +0200 | [diff] [blame] | 644 | struct pixman_renderer *renderer; |
Vasily Khoruzhick | 71c2dd3 | 2013-01-07 20:39:49 +0300 | [diff] [blame] | 645 | |
| 646 | renderer = malloc(sizeof *renderer); |
| 647 | if (renderer == NULL) |
| 648 | return -1; |
| 649 | |
Philipp Brüschweiler | f3e39f9 | 2013-03-08 20:35:39 +0100 | [diff] [blame] | 650 | renderer->repaint_debug = 0; |
| 651 | renderer->debug_color = NULL; |
Ander Conselvan de Oliveira | 8792ef1 | 2013-01-25 15:13:00 +0200 | [diff] [blame] | 652 | renderer->base.read_pixels = pixman_renderer_read_pixels; |
| 653 | renderer->base.repaint_output = pixman_renderer_repaint_output; |
| 654 | renderer->base.flush_damage = pixman_renderer_flush_damage; |
| 655 | renderer->base.attach = pixman_renderer_attach; |
| 656 | renderer->base.create_surface = pixman_renderer_create_surface; |
| 657 | renderer->base.surface_set_color = pixman_renderer_surface_set_color; |
| 658 | renderer->base.destroy_surface = pixman_renderer_destroy_surface; |
| 659 | renderer->base.destroy = pixman_renderer_destroy; |
| 660 | ec->renderer = &renderer->base; |
Pekka Paalanen | 7bb6510 | 2013-05-22 18:03:04 +0300 | [diff] [blame] | 661 | ec->capabilities |= WESTON_CAP_ROTATION_ANY; |
Pekka Paalanen | 4fc5dd0 | 2013-05-22 18:03:05 +0300 | [diff] [blame^] | 662 | ec->capabilities |= WESTON_CAP_CAPTURE_YFLIP; |
Vasily Khoruzhick | 71c2dd3 | 2013-01-07 20:39:49 +0300 | [diff] [blame] | 663 | |
Ander Conselvan de Oliveira | 8792ef1 | 2013-01-25 15:13:00 +0200 | [diff] [blame] | 664 | weston_compositor_add_debug_binding(ec, KEY_R, |
| 665 | debug_binding, ec); |
Vasily Khoruzhick | 71c2dd3 | 2013-01-07 20:39:49 +0300 | [diff] [blame] | 666 | return 0; |
| 667 | } |
| 668 | |
| 669 | WL_EXPORT void |
| 670 | pixman_renderer_output_set_buffer(struct weston_output *output, pixman_image_t *buffer) |
| 671 | { |
| 672 | struct pixman_output_state *po = get_output_state(output); |
| 673 | |
| 674 | if (po->hw_buffer) |
| 675 | pixman_image_unref(po->hw_buffer); |
| 676 | po->hw_buffer = buffer; |
| 677 | |
| 678 | if (po->hw_buffer) { |
| 679 | output->compositor->read_format = pixman_image_get_format(po->hw_buffer); |
| 680 | pixman_image_ref(po->hw_buffer); |
| 681 | } |
| 682 | } |
| 683 | |
| 684 | WL_EXPORT int |
| 685 | pixman_renderer_output_create(struct weston_output *output) |
| 686 | { |
| 687 | struct pixman_output_state *po = calloc(1, sizeof *po); |
Ander Conselvan de Oliveira | 936effd | 2013-01-25 15:13:01 +0200 | [diff] [blame] | 688 | int w, h; |
Vasily Khoruzhick | 71c2dd3 | 2013-01-07 20:39:49 +0300 | [diff] [blame] | 689 | |
| 690 | if (!po) |
| 691 | return -1; |
Ander Conselvan de Oliveira | 936effd | 2013-01-25 15:13:01 +0200 | [diff] [blame] | 692 | |
| 693 | /* set shadow image transformation */ |
Alexander Larsson | 4ea9552 | 2013-05-22 14:41:37 +0200 | [diff] [blame] | 694 | w = output->current->width * output->scale; |
| 695 | h = output->current->height * output->scale; |
Ander Conselvan de Oliveira | 936effd | 2013-01-25 15:13:01 +0200 | [diff] [blame] | 696 | |
Ander Conselvan de Oliveira | 936effd | 2013-01-25 15:13:01 +0200 | [diff] [blame] | 697 | po->shadow_buffer = malloc(w * h * 4); |
| 698 | |
| 699 | if (!po->shadow_buffer) { |
| 700 | free(po); |
| 701 | return -1; |
| 702 | } |
| 703 | |
| 704 | po->shadow_image = |
| 705 | pixman_image_create_bits(PIXMAN_x8r8g8b8, w, h, |
| 706 | po->shadow_buffer, w * 4); |
| 707 | |
| 708 | if (!po->shadow_image) { |
| 709 | free(po->shadow_buffer); |
| 710 | free(po); |
| 711 | return -1; |
| 712 | } |
| 713 | |
Vasily Khoruzhick | 71c2dd3 | 2013-01-07 20:39:49 +0300 | [diff] [blame] | 714 | output->renderer_state = po; |
| 715 | |
| 716 | return 0; |
| 717 | } |
| 718 | |
| 719 | WL_EXPORT void |
| 720 | pixman_renderer_output_destroy(struct weston_output *output) |
| 721 | { |
| 722 | struct pixman_output_state *po = get_output_state(output); |
| 723 | |
Ander Conselvan de Oliveira | 936effd | 2013-01-25 15:13:01 +0200 | [diff] [blame] | 724 | pixman_image_unref(po->shadow_image); |
Ander Conselvan de Oliveira | 23e72b8 | 2013-01-25 15:13:06 +0200 | [diff] [blame] | 725 | |
| 726 | if (po->hw_buffer) |
| 727 | pixman_image_unref(po->hw_buffer); |
Ander Conselvan de Oliveira | 936effd | 2013-01-25 15:13:01 +0200 | [diff] [blame] | 728 | |
| 729 | po->shadow_image = NULL; |
Vasily Khoruzhick | 71c2dd3 | 2013-01-07 20:39:49 +0300 | [diff] [blame] | 730 | po->hw_buffer = NULL; |
| 731 | |
| 732 | free(po); |
| 733 | } |