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 | 1f206b4 | 2013-05-22 14:41:36 +0200 | [diff] [blame^] | 108 | 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] | 109 | { |
Alexander Larsson | 1f206b4 | 2013-05-22 14:41:36 +0200 | [diff] [blame^] | 110 | pixman_box32_t *rects, *transformed_rects; |
| 111 | int nrects, i; |
| 112 | |
| 113 | if (transform == WL_OUTPUT_TRANSFORM_NORMAL) |
| 114 | return; |
| 115 | |
| 116 | rects = pixman_region32_rectangles(region, &nrects); |
| 117 | transformed_rects = calloc(nrects, sizeof(pixman_box32_t)); |
| 118 | |
| 119 | for (i = 0; i < nrects; i++) { |
| 120 | switch (transform) { |
| 121 | default: |
| 122 | case WL_OUTPUT_TRANSFORM_NORMAL: |
| 123 | transformed_rects[i].x1 = rects[i].x1; |
| 124 | transformed_rects[i].y1 = rects[i].y1; |
| 125 | transformed_rects[i].x2 = rects[i].x2; |
| 126 | transformed_rects[i].y2 = rects[i].y2; |
| 127 | break; |
| 128 | case WL_OUTPUT_TRANSFORM_90: |
| 129 | transformed_rects[i].x1 = height - rects[i].y2; |
| 130 | transformed_rects[i].y1 = rects[i].x1; |
| 131 | transformed_rects[i].x2 = height - rects[i].y1; |
| 132 | transformed_rects[i].y2 = rects[i].x2; |
| 133 | break; |
| 134 | case WL_OUTPUT_TRANSFORM_180: |
| 135 | transformed_rects[i].x1 = width - rects[i].x2; |
| 136 | transformed_rects[i].y1 = height - rects[i].y2; |
| 137 | transformed_rects[i].x2 = width - rects[i].x1; |
| 138 | transformed_rects[i].y2 = height - rects[i].y1; |
| 139 | break; |
| 140 | case WL_OUTPUT_TRANSFORM_270: |
| 141 | transformed_rects[i].x1 = rects[i].y1; |
| 142 | transformed_rects[i].y1 = width - rects[i].x2; |
| 143 | transformed_rects[i].x2 = rects[i].y2; |
| 144 | transformed_rects[i].y2 = width - rects[i].x1; |
| 145 | break; |
| 146 | case WL_OUTPUT_TRANSFORM_FLIPPED: |
| 147 | transformed_rects[i].x1 = width - rects[i].x2; |
| 148 | transformed_rects[i].y1 = rects[i].y1; |
| 149 | transformed_rects[i].x2 = width - rects[i].x1; |
| 150 | transformed_rects[i].y2 = rects[i].y2; |
| 151 | break; |
| 152 | case WL_OUTPUT_TRANSFORM_FLIPPED_90: |
| 153 | transformed_rects[i].x1 = height - rects[i].y2; |
| 154 | transformed_rects[i].y1 = width - rects[i].x2; |
| 155 | transformed_rects[i].x2 = height - rects[i].y1; |
| 156 | transformed_rects[i].y2 = width - rects[i].x1; |
| 157 | break; |
| 158 | case WL_OUTPUT_TRANSFORM_FLIPPED_180: |
| 159 | transformed_rects[i].x1 = rects[i].x1; |
| 160 | transformed_rects[i].y1 = height - rects[i].y2; |
| 161 | transformed_rects[i].x2 = rects[i].x2; |
| 162 | transformed_rects[i].y2 = height - rects[i].y1; |
| 163 | break; |
| 164 | case WL_OUTPUT_TRANSFORM_FLIPPED_270: |
| 165 | transformed_rects[i].x1 = rects[i].y1; |
| 166 | transformed_rects[i].y1 = rects[i].x1; |
| 167 | transformed_rects[i].x2 = rects[i].y2; |
| 168 | transformed_rects[i].y2 = rects[i].x2; |
| 169 | break; |
| 170 | } |
| 171 | } |
| 172 | pixman_region32_clear(region); |
| 173 | |
| 174 | pixman_region32_init_rects (region, transformed_rects, nrects); |
| 175 | free (transformed_rects); |
| 176 | } |
| 177 | |
| 178 | static void |
| 179 | region_global_to_output(struct weston_output *output, pixman_region32_t *region) |
| 180 | { |
| 181 | pixman_region32_translate(region, -output->x, -output->y); |
| 182 | transform_region (region, output->width, output->height, output->transform); |
Vasily Khoruzhick | 031fc87 | 2013-01-29 14:58:14 +0300 | [diff] [blame] | 183 | } |
| 184 | |
Vasily Khoruzhick | f4a457f | 2013-01-28 22:40:29 +0300 | [diff] [blame] | 185 | #define D2F(v) pixman_double_to_fixed((double)v) |
| 186 | |
Vasily Khoruzhick | 71c2dd3 | 2013-01-07 20:39:49 +0300 | [diff] [blame] | 187 | static void |
Alexander Larsson | 1f206b4 | 2013-05-22 14:41:36 +0200 | [diff] [blame^] | 188 | repaint_region(struct weston_surface *es, struct weston_output *output, |
| 189 | pixman_region32_t *region, pixman_region32_t *surf_region, |
| 190 | pixman_op_t pixman_op) |
Vasily Khoruzhick | 71c2dd3 | 2013-01-07 20:39:49 +0300 | [diff] [blame] | 191 | { |
Ander Conselvan de Oliveira | 8792ef1 | 2013-01-25 15:13:00 +0200 | [diff] [blame] | 192 | struct pixman_renderer *pr = |
| 193 | (struct pixman_renderer *) output->compositor->renderer; |
Vasily Khoruzhick | 71c2dd3 | 2013-01-07 20:39:49 +0300 | [diff] [blame] | 194 | struct pixman_surface_state *ps = get_surface_state(es); |
| 195 | struct pixman_output_state *po = get_output_state(output); |
| 196 | pixman_region32_t final_region; |
Vasily Khoruzhick | 71c2dd3 | 2013-01-07 20:39:49 +0300 | [diff] [blame] | 197 | float surface_x, surface_y; |
Alexander Larsson | 1f206b4 | 2013-05-22 14:41:36 +0200 | [diff] [blame^] | 198 | pixman_transform_t transform; |
| 199 | pixman_fixed_t fw, fh; |
Vasily Khoruzhick | 71c2dd3 | 2013-01-07 20:39:49 +0300 | [diff] [blame] | 200 | |
| 201 | /* The final region to be painted is the intersection of |
| 202 | * 'region' and 'surf_region'. However, 'region' is in the global |
| 203 | * coordinates, and 'surf_region' is in the surface-local |
| 204 | * coordinates |
| 205 | */ |
| 206 | pixman_region32_init(&final_region); |
Alexander Larsson | 1f206b4 | 2013-05-22 14:41:36 +0200 | [diff] [blame^] | 207 | if (surf_region) { |
| 208 | pixman_region32_copy(&final_region, surf_region); |
Vasily Khoruzhick | 71c2dd3 | 2013-01-07 20:39:49 +0300 | [diff] [blame] | 209 | |
Alexander Larsson | 1f206b4 | 2013-05-22 14:41:36 +0200 | [diff] [blame^] | 210 | /* Convert from surface to global coordinates */ |
| 211 | if (!es->transform.enabled) { |
| 212 | pixman_region32_translate(&final_region, es->geometry.x, es->geometry.y); |
| 213 | } else { |
| 214 | weston_surface_to_global_float(es, 0, 0, &surface_x, &surface_y); |
| 215 | pixman_region32_translate(&final_region, (int)surface_x, (int)surface_y); |
| 216 | } |
Vasily Khoruzhick | f4a457f | 2013-01-28 22:40:29 +0300 | [diff] [blame] | 217 | |
Alexander Larsson | 1f206b4 | 2013-05-22 14:41:36 +0200 | [diff] [blame^] | 218 | /* We need to paint the intersection */ |
| 219 | pixman_region32_intersect(&final_region, &final_region, region); |
Vasily Khoruzhick | f4a457f | 2013-01-28 22:40:29 +0300 | [diff] [blame] | 220 | } else { |
Alexander Larsson | 1f206b4 | 2013-05-22 14:41:36 +0200 | [diff] [blame^] | 221 | /* If there is no surface region, just use the global region */ |
| 222 | pixman_region32_copy(&final_region, region); |
Vasily Khoruzhick | f4a457f | 2013-01-28 22:40:29 +0300 | [diff] [blame] | 223 | } |
Vasily Khoruzhick | 71c2dd3 | 2013-01-07 20:39:49 +0300 | [diff] [blame] | 224 | |
Alexander Larsson | 1f206b4 | 2013-05-22 14:41:36 +0200 | [diff] [blame^] | 225 | /* Convert from global to output coord */ |
| 226 | region_global_to_output(output, &final_region); |
Vasily Khoruzhick | 71c2dd3 | 2013-01-07 20:39:49 +0300 | [diff] [blame] | 227 | |
Alexander Larsson | 1f206b4 | 2013-05-22 14:41:36 +0200 | [diff] [blame^] | 228 | /* And clip to it */ |
| 229 | pixman_image_set_clip_region32 (po->shadow_image, &final_region); |
Ander Conselvan de Oliveira | 8792ef1 | 2013-01-25 15:13:00 +0200 | [diff] [blame] | 230 | |
Alexander Larsson | 1f206b4 | 2013-05-22 14:41:36 +0200 | [diff] [blame^] | 231 | /* Set up the source transformation based on the surface |
| 232 | position, the output position/transform and the client |
| 233 | specified buffer transform */ |
| 234 | pixman_transform_init_identity(&transform); |
Ander Conselvan de Oliveira | 8792ef1 | 2013-01-25 15:13:00 +0200 | [diff] [blame] | 235 | |
Alexander Larsson | 1f206b4 | 2013-05-22 14:41:36 +0200 | [diff] [blame^] | 236 | fw = pixman_int_to_fixed(output->width); |
| 237 | fh = pixman_int_to_fixed(output->height); |
| 238 | switch (output->transform) { |
| 239 | default: |
| 240 | case WL_OUTPUT_TRANSFORM_NORMAL: |
| 241 | case WL_OUTPUT_TRANSFORM_FLIPPED: |
| 242 | break; |
| 243 | case WL_OUTPUT_TRANSFORM_90: |
| 244 | case WL_OUTPUT_TRANSFORM_FLIPPED_90: |
| 245 | pixman_transform_rotate(&transform, NULL, 0, -pixman_fixed_1); |
| 246 | pixman_transform_translate(&transform, NULL, 0, fh); |
| 247 | break; |
| 248 | case WL_OUTPUT_TRANSFORM_180: |
| 249 | case WL_OUTPUT_TRANSFORM_FLIPPED_180: |
| 250 | pixman_transform_rotate(&transform, NULL, -pixman_fixed_1, 0); |
| 251 | pixman_transform_translate(&transform, NULL, fw, fh); |
| 252 | break; |
| 253 | case WL_OUTPUT_TRANSFORM_270: |
| 254 | case WL_OUTPUT_TRANSFORM_FLIPPED_270: |
| 255 | pixman_transform_rotate(&transform, NULL, 0, pixman_fixed_1); |
| 256 | pixman_transform_translate(&transform, NULL, fw, 0); |
| 257 | break; |
| 258 | } |
| 259 | |
| 260 | switch (output->transform) { |
| 261 | case WL_OUTPUT_TRANSFORM_FLIPPED: |
| 262 | case WL_OUTPUT_TRANSFORM_FLIPPED_90: |
| 263 | case WL_OUTPUT_TRANSFORM_FLIPPED_180: |
| 264 | case WL_OUTPUT_TRANSFORM_FLIPPED_270: |
| 265 | pixman_transform_scale(&transform, NULL, |
| 266 | pixman_int_to_fixed (-1), |
| 267 | pixman_int_to_fixed (1)); |
| 268 | pixman_transform_translate(&transform, NULL, fw, 0); |
| 269 | break; |
| 270 | } |
| 271 | |
| 272 | pixman_transform_translate(&transform, NULL, |
| 273 | pixman_double_to_fixed (output->x), |
| 274 | pixman_double_to_fixed (output->y)); |
| 275 | |
| 276 | if (es->transform.enabled) { |
| 277 | /* Pixman supports only 2D transform matrix, but Weston uses 3D, |
| 278 | * so we're omitting Z coordinate here |
| 279 | */ |
| 280 | pixman_transform_t surface_transform = {{ |
| 281 | { D2F(es->transform.matrix.d[0]), |
| 282 | D2F(es->transform.matrix.d[4]), |
| 283 | D2F(es->transform.matrix.d[12]), |
| 284 | }, |
| 285 | { D2F(es->transform.matrix.d[1]), |
| 286 | D2F(es->transform.matrix.d[5]), |
| 287 | D2F(es->transform.matrix.d[13]), |
| 288 | }, |
| 289 | { D2F(es->transform.matrix.d[3]), |
| 290 | D2F(es->transform.matrix.d[7]), |
| 291 | D2F(es->transform.matrix.d[15]), |
| 292 | } |
| 293 | }}; |
| 294 | |
| 295 | pixman_transform_invert(&surface_transform, &surface_transform); |
| 296 | pixman_transform_multiply (&transform, &surface_transform, &transform); |
| 297 | } else { |
| 298 | pixman_transform_translate(&transform, NULL, |
| 299 | pixman_double_to_fixed ((double)-es->geometry.x), |
| 300 | pixman_double_to_fixed ((double)-es->geometry.y)); |
| 301 | } |
| 302 | |
| 303 | |
| 304 | fw = pixman_int_to_fixed(es->geometry.width); |
| 305 | fh = pixman_int_to_fixed(es->geometry.height); |
| 306 | |
| 307 | switch (es->buffer_transform) { |
| 308 | case WL_OUTPUT_TRANSFORM_FLIPPED: |
| 309 | case WL_OUTPUT_TRANSFORM_FLIPPED_90: |
| 310 | case WL_OUTPUT_TRANSFORM_FLIPPED_180: |
| 311 | case WL_OUTPUT_TRANSFORM_FLIPPED_270: |
| 312 | pixman_transform_scale(&transform, NULL, |
| 313 | pixman_int_to_fixed (-1), |
| 314 | pixman_int_to_fixed (1)); |
| 315 | pixman_transform_translate(&transform, NULL, fw, 0); |
| 316 | break; |
| 317 | } |
| 318 | |
| 319 | switch (es->buffer_transform) { |
| 320 | default: |
| 321 | case WL_OUTPUT_TRANSFORM_NORMAL: |
| 322 | case WL_OUTPUT_TRANSFORM_FLIPPED: |
| 323 | break; |
| 324 | case WL_OUTPUT_TRANSFORM_90: |
| 325 | case WL_OUTPUT_TRANSFORM_FLIPPED_90: |
| 326 | pixman_transform_rotate(&transform, NULL, 0, pixman_fixed_1); |
| 327 | pixman_transform_translate(&transform, NULL, fh, 0); |
| 328 | break; |
| 329 | case WL_OUTPUT_TRANSFORM_180: |
| 330 | case WL_OUTPUT_TRANSFORM_FLIPPED_180: |
| 331 | pixman_transform_rotate(&transform, NULL, -pixman_fixed_1, 0); |
| 332 | pixman_transform_translate(&transform, NULL, fw, fh); |
| 333 | break; |
| 334 | case WL_OUTPUT_TRANSFORM_270: |
| 335 | case WL_OUTPUT_TRANSFORM_FLIPPED_270: |
| 336 | pixman_transform_rotate(&transform, NULL, 0, -pixman_fixed_1); |
| 337 | pixman_transform_translate(&transform, NULL, 0, fw); |
| 338 | break; |
| 339 | } |
| 340 | |
| 341 | pixman_image_set_transform(ps->image, &transform); |
| 342 | |
| 343 | if (es->transform.enabled) |
| 344 | pixman_image_set_filter(ps->image, PIXMAN_FILTER_BILINEAR, NULL, 0); |
| 345 | else |
| 346 | pixman_image_set_filter(ps->image, PIXMAN_FILTER_NEAREST, NULL, 0); |
| 347 | |
| 348 | pixman_image_composite32(pixman_op, |
| 349 | ps->image, /* src */ |
| 350 | NULL /* mask */, |
| 351 | po->shadow_image, /* dest */ |
| 352 | 0, 0, /* src_x, src_y */ |
| 353 | 0, 0, /* mask_x, mask_y */ |
| 354 | 0, 0, /* dest_x, dest_y */ |
| 355 | pixman_image_get_width (po->shadow_image), /* width */ |
| 356 | pixman_image_get_height (po->shadow_image) /* height */); |
| 357 | |
| 358 | if (pr->repaint_debug) |
Ander Conselvan de Oliveira | 8792ef1 | 2013-01-25 15:13:00 +0200 | [diff] [blame] | 359 | pixman_image_composite32(PIXMAN_OP_OVER, |
Alexander Larsson | 1f206b4 | 2013-05-22 14:41:36 +0200 | [diff] [blame^] | 360 | pr->debug_color, /* src */ |
| 361 | NULL /* mask */, |
| 362 | po->shadow_image, /* dest */ |
| 363 | 0, 0, /* src_x, src_y */ |
| 364 | 0, 0, /* mask_x, mask_y */ |
| 365 | 0, 0, /* dest_x, dest_y */ |
| 366 | pixman_image_get_width (po->shadow_image), /* width */ |
| 367 | pixman_image_get_height (po->shadow_image) /* height */); |
| 368 | |
| 369 | pixman_image_set_clip_region32 (po->shadow_image, NULL); |
| 370 | |
Vasily Khoruzhick | 71c2dd3 | 2013-01-07 20:39:49 +0300 | [diff] [blame] | 371 | pixman_region32_fini(&final_region); |
| 372 | } |
| 373 | |
| 374 | static void |
| 375 | draw_surface(struct weston_surface *es, struct weston_output *output, |
| 376 | pixman_region32_t *damage) /* in global coordinates */ |
| 377 | { |
| 378 | struct pixman_surface_state *ps = get_surface_state(es); |
| 379 | /* repaint bounding region in global coordinates: */ |
| 380 | pixman_region32_t repaint; |
| 381 | /* non-opaque region in surface coordinates: */ |
| 382 | pixman_region32_t surface_blend; |
| 383 | |
| 384 | /* No buffer attached */ |
| 385 | if (!ps->image) |
| 386 | return; |
| 387 | |
| 388 | pixman_region32_init(&repaint); |
| 389 | pixman_region32_intersect(&repaint, |
| 390 | &es->transform.boundingbox, damage); |
| 391 | pixman_region32_subtract(&repaint, &repaint, &es->clip); |
| 392 | |
| 393 | if (!pixman_region32_not_empty(&repaint)) |
| 394 | goto out; |
| 395 | |
| 396 | if (output->zoom.active) { |
| 397 | weston_log("pixman renderer does not support zoom\n"); |
| 398 | goto out; |
| 399 | } |
| 400 | |
Vasily Khoruzhick | f4a457f | 2013-01-28 22:40:29 +0300 | [diff] [blame] | 401 | /* TODO: Implement repaint_region_complex() using pixman_composite_trapezoids() */ |
| 402 | if (es->transform.enabled && |
| 403 | es->transform.matrix.type != WESTON_MATRIX_TRANSFORM_TRANSLATE) { |
Alexander Larsson | 1f206b4 | 2013-05-22 14:41:36 +0200 | [diff] [blame^] | 404 | repaint_region(es, output, &repaint, NULL, PIXMAN_OP_OVER); |
Vasily Khoruzhick | f4a457f | 2013-01-28 22:40:29 +0300 | [diff] [blame] | 405 | } else { |
| 406 | /* blended region is whole surface minus opaque region: */ |
| 407 | pixman_region32_init_rect(&surface_blend, 0, 0, |
| 408 | es->geometry.width, es->geometry.height); |
| 409 | pixman_region32_subtract(&surface_blend, &surface_blend, &es->opaque); |
Vasily Khoruzhick | 71c2dd3 | 2013-01-07 20:39:49 +0300 | [diff] [blame] | 410 | |
Vasily Khoruzhick | f4a457f | 2013-01-28 22:40:29 +0300 | [diff] [blame] | 411 | if (pixman_region32_not_empty(&es->opaque)) { |
Alexander Larsson | 1f206b4 | 2013-05-22 14:41:36 +0200 | [diff] [blame^] | 412 | repaint_region(es, output, &repaint, &es->opaque, PIXMAN_OP_SRC); |
Vasily Khoruzhick | f4a457f | 2013-01-28 22:40:29 +0300 | [diff] [blame] | 413 | } |
| 414 | |
| 415 | if (pixman_region32_not_empty(&surface_blend)) { |
Alexander Larsson | 1f206b4 | 2013-05-22 14:41:36 +0200 | [diff] [blame^] | 416 | repaint_region(es, output, &repaint, &surface_blend, PIXMAN_OP_OVER); |
Vasily Khoruzhick | f4a457f | 2013-01-28 22:40:29 +0300 | [diff] [blame] | 417 | } |
| 418 | pixman_region32_fini(&surface_blend); |
Vasily Khoruzhick | 71c2dd3 | 2013-01-07 20:39:49 +0300 | [diff] [blame] | 419 | } |
| 420 | |
Vasily Khoruzhick | 71c2dd3 | 2013-01-07 20:39:49 +0300 | [diff] [blame] | 421 | |
| 422 | out: |
| 423 | pixman_region32_fini(&repaint); |
| 424 | } |
| 425 | static void |
| 426 | repaint_surfaces(struct weston_output *output, pixman_region32_t *damage) |
| 427 | { |
| 428 | struct weston_compositor *compositor = output->compositor; |
| 429 | struct weston_surface *surface; |
| 430 | |
| 431 | wl_list_for_each_reverse(surface, &compositor->surface_list, link) |
| 432 | if (surface->plane == &compositor->primary_plane) |
| 433 | draw_surface(surface, output, damage); |
| 434 | } |
| 435 | |
| 436 | static void |
Ander Conselvan de Oliveira | 936effd | 2013-01-25 15:13:01 +0200 | [diff] [blame] | 437 | copy_to_hw_buffer(struct weston_output *output, pixman_region32_t *region) |
| 438 | { |
| 439 | struct pixman_output_state *po = get_output_state(output); |
Alexander Larsson | 1f206b4 | 2013-05-22 14:41:36 +0200 | [diff] [blame^] | 440 | pixman_region32_t output_region; |
Ander Conselvan de Oliveira | 936effd | 2013-01-25 15:13:01 +0200 | [diff] [blame] | 441 | |
Alexander Larsson | 1f206b4 | 2013-05-22 14:41:36 +0200 | [diff] [blame^] | 442 | pixman_region32_init(&output_region); |
| 443 | pixman_region32_copy(&output_region, region); |
Ander Conselvan de Oliveira | 936effd | 2013-01-25 15:13:01 +0200 | [diff] [blame] | 444 | |
Alexander Larsson | 1f206b4 | 2013-05-22 14:41:36 +0200 | [diff] [blame^] | 445 | region_global_to_output(output, &output_region); |
Ander Conselvan de Oliveira | 936effd | 2013-01-25 15:13:01 +0200 | [diff] [blame] | 446 | |
Alexander Larsson | 1f206b4 | 2013-05-22 14:41:36 +0200 | [diff] [blame^] | 447 | pixman_image_set_clip_region32 (po->hw_buffer, &output_region); |
Ander Conselvan de Oliveira | 936effd | 2013-01-25 15:13:01 +0200 | [diff] [blame] | 448 | |
Alexander Larsson | 1f206b4 | 2013-05-22 14:41:36 +0200 | [diff] [blame^] | 449 | pixman_image_composite32(PIXMAN_OP_SRC, |
| 450 | po->shadow_image, /* src */ |
| 451 | NULL /* mask */, |
| 452 | po->hw_buffer, /* dest */ |
| 453 | 0, 0, /* src_x, src_y */ |
| 454 | 0, 0, /* mask_x, mask_y */ |
| 455 | 0, 0, /* dest_x, dest_y */ |
| 456 | pixman_image_get_width (po->hw_buffer), /* width */ |
| 457 | pixman_image_get_height (po->hw_buffer) /* height */); |
| 458 | |
| 459 | pixman_image_set_clip_region32 (po->hw_buffer, NULL); |
Ander Conselvan de Oliveira | 936effd | 2013-01-25 15:13:01 +0200 | [diff] [blame] | 460 | } |
| 461 | |
| 462 | static void |
Vasily Khoruzhick | 71c2dd3 | 2013-01-07 20:39:49 +0300 | [diff] [blame] | 463 | pixman_renderer_repaint_output(struct weston_output *output, |
| 464 | pixman_region32_t *output_damage) |
| 465 | { |
| 466 | struct pixman_output_state *po = get_output_state(output); |
| 467 | |
| 468 | if (!po->hw_buffer) |
| 469 | return; |
| 470 | |
| 471 | repaint_surfaces(output, output_damage); |
Ander Conselvan de Oliveira | 936effd | 2013-01-25 15:13:01 +0200 | [diff] [blame] | 472 | copy_to_hw_buffer(output, output_damage); |
Vasily Khoruzhick | 71c2dd3 | 2013-01-07 20:39:49 +0300 | [diff] [blame] | 473 | |
| 474 | pixman_region32_copy(&output->previous_damage, output_damage); |
| 475 | wl_signal_emit(&output->frame_signal, output); |
| 476 | |
| 477 | /* Actual flip should be done by caller */ |
| 478 | } |
| 479 | |
| 480 | static void |
| 481 | pixman_renderer_flush_damage(struct weston_surface *surface) |
| 482 | { |
| 483 | /* No-op for pixman renderer */ |
| 484 | } |
| 485 | |
| 486 | static void |
| 487 | pixman_renderer_attach(struct weston_surface *es, struct wl_buffer *buffer) |
| 488 | { |
| 489 | struct pixman_surface_state *ps = get_surface_state(es); |
| 490 | pixman_format_code_t pixman_format; |
| 491 | |
| 492 | weston_buffer_reference(&ps->buffer_ref, buffer); |
| 493 | |
| 494 | if (ps->image) { |
| 495 | pixman_image_unref(ps->image); |
| 496 | ps->image = NULL; |
| 497 | } |
| 498 | |
| 499 | if (!buffer) |
| 500 | return; |
| 501 | |
| 502 | if (!wl_buffer_is_shm(buffer)) { |
| 503 | weston_log("Pixman renderer supports only SHM buffers\n"); |
| 504 | weston_buffer_reference(&ps->buffer_ref, NULL); |
| 505 | return; |
| 506 | } |
| 507 | |
| 508 | switch (wl_shm_buffer_get_format(buffer)) { |
| 509 | case WL_SHM_FORMAT_XRGB8888: |
| 510 | pixman_format = PIXMAN_x8r8g8b8; |
| 511 | break; |
| 512 | case WL_SHM_FORMAT_ARGB8888: |
| 513 | pixman_format = PIXMAN_a8r8g8b8; |
| 514 | break; |
| 515 | default: |
| 516 | weston_log("Unsupported SHM buffer format\n"); |
| 517 | weston_buffer_reference(&ps->buffer_ref, NULL); |
| 518 | return; |
| 519 | break; |
| 520 | } |
| 521 | ps->image = pixman_image_create_bits(pixman_format, |
| 522 | wl_shm_buffer_get_width(buffer), |
| 523 | wl_shm_buffer_get_height(buffer), |
| 524 | wl_shm_buffer_get_data(buffer), |
| 525 | wl_shm_buffer_get_stride(buffer)); |
| 526 | } |
| 527 | |
| 528 | static int |
| 529 | pixman_renderer_create_surface(struct weston_surface *surface) |
| 530 | { |
| 531 | struct pixman_surface_state *ps; |
| 532 | |
| 533 | ps = calloc(1, sizeof *ps); |
| 534 | if (!ps) |
| 535 | return -1; |
| 536 | |
| 537 | surface->renderer_state = ps; |
| 538 | |
| 539 | return 0; |
| 540 | } |
| 541 | |
| 542 | static void |
| 543 | pixman_renderer_surface_set_color(struct weston_surface *es, |
| 544 | float red, float green, float blue, float alpha) |
| 545 | { |
| 546 | struct pixman_surface_state *ps = get_surface_state(es); |
| 547 | pixman_color_t color; |
| 548 | |
| 549 | color.red = red * 0xffff; |
| 550 | color.green = green * 0xffff; |
| 551 | color.blue = blue * 0xffff; |
| 552 | color.alpha = alpha * 0xffff; |
| 553 | |
| 554 | if (ps->image) { |
| 555 | pixman_image_unref(ps->image); |
| 556 | ps->image = NULL; |
| 557 | } |
| 558 | |
| 559 | ps->image = pixman_image_create_solid_fill(&color); |
| 560 | } |
| 561 | |
| 562 | static void |
| 563 | pixman_renderer_destroy_surface(struct weston_surface *surface) |
| 564 | { |
| 565 | struct pixman_surface_state *ps = get_surface_state(surface); |
| 566 | |
| 567 | if (ps->image) { |
| 568 | pixman_image_unref(ps->image); |
| 569 | ps->image = NULL; |
| 570 | } |
| 571 | weston_buffer_reference(&ps->buffer_ref, NULL); |
| 572 | free(ps); |
| 573 | } |
| 574 | |
Vasily Khoruzhick | 52cfd61 | 2013-01-08 19:09:01 +0300 | [diff] [blame] | 575 | static void |
Vasily Khoruzhick | 71c2dd3 | 2013-01-07 20:39:49 +0300 | [diff] [blame] | 576 | pixman_renderer_destroy(struct weston_compositor *ec) |
| 577 | { |
| 578 | free(ec->renderer); |
| 579 | ec->renderer = NULL; |
| 580 | } |
| 581 | |
Ander Conselvan de Oliveira | 8792ef1 | 2013-01-25 15:13:00 +0200 | [diff] [blame] | 582 | static void |
Kristian Høgsberg | e314875 | 2013-05-06 23:19:49 -0400 | [diff] [blame] | 583 | 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] | 584 | void *data) |
| 585 | { |
| 586 | struct weston_compositor *ec = data; |
| 587 | struct pixman_renderer *pr = (struct pixman_renderer *) ec->renderer; |
| 588 | |
| 589 | pr->repaint_debug ^= 1; |
| 590 | |
| 591 | if (pr->repaint_debug) { |
| 592 | pixman_color_t red = { |
| 593 | 0x3fff, 0x0000, 0x0000, 0x3fff |
| 594 | }; |
| 595 | |
| 596 | pr->debug_color = pixman_image_create_solid_fill(&red); |
| 597 | } else { |
| 598 | pixman_image_unref(pr->debug_color); |
| 599 | weston_compositor_damage_all(ec); |
| 600 | } |
| 601 | } |
| 602 | |
Vasily Khoruzhick | 71c2dd3 | 2013-01-07 20:39:49 +0300 | [diff] [blame] | 603 | WL_EXPORT int |
| 604 | pixman_renderer_init(struct weston_compositor *ec) |
| 605 | { |
Ander Conselvan de Oliveira | 8792ef1 | 2013-01-25 15:13:00 +0200 | [diff] [blame] | 606 | struct pixman_renderer *renderer; |
Vasily Khoruzhick | 71c2dd3 | 2013-01-07 20:39:49 +0300 | [diff] [blame] | 607 | |
| 608 | renderer = malloc(sizeof *renderer); |
| 609 | if (renderer == NULL) |
| 610 | return -1; |
| 611 | |
Philipp Brüschweiler | f3e39f9 | 2013-03-08 20:35:39 +0100 | [diff] [blame] | 612 | renderer->repaint_debug = 0; |
| 613 | renderer->debug_color = NULL; |
Ander Conselvan de Oliveira | 8792ef1 | 2013-01-25 15:13:00 +0200 | [diff] [blame] | 614 | renderer->base.read_pixels = pixman_renderer_read_pixels; |
| 615 | renderer->base.repaint_output = pixman_renderer_repaint_output; |
| 616 | renderer->base.flush_damage = pixman_renderer_flush_damage; |
| 617 | renderer->base.attach = pixman_renderer_attach; |
| 618 | renderer->base.create_surface = pixman_renderer_create_surface; |
| 619 | renderer->base.surface_set_color = pixman_renderer_surface_set_color; |
| 620 | renderer->base.destroy_surface = pixman_renderer_destroy_surface; |
| 621 | renderer->base.destroy = pixman_renderer_destroy; |
| 622 | ec->renderer = &renderer->base; |
Vasily Khoruzhick | 71c2dd3 | 2013-01-07 20:39:49 +0300 | [diff] [blame] | 623 | |
Ander Conselvan de Oliveira | 8792ef1 | 2013-01-25 15:13:00 +0200 | [diff] [blame] | 624 | weston_compositor_add_debug_binding(ec, KEY_R, |
| 625 | debug_binding, ec); |
Vasily Khoruzhick | 71c2dd3 | 2013-01-07 20:39:49 +0300 | [diff] [blame] | 626 | return 0; |
| 627 | } |
| 628 | |
| 629 | WL_EXPORT void |
| 630 | pixman_renderer_output_set_buffer(struct weston_output *output, pixman_image_t *buffer) |
| 631 | { |
| 632 | struct pixman_output_state *po = get_output_state(output); |
| 633 | |
| 634 | if (po->hw_buffer) |
| 635 | pixman_image_unref(po->hw_buffer); |
| 636 | po->hw_buffer = buffer; |
| 637 | |
| 638 | if (po->hw_buffer) { |
| 639 | output->compositor->read_format = pixman_image_get_format(po->hw_buffer); |
| 640 | pixman_image_ref(po->hw_buffer); |
| 641 | } |
| 642 | } |
| 643 | |
| 644 | WL_EXPORT int |
| 645 | pixman_renderer_output_create(struct weston_output *output) |
| 646 | { |
| 647 | struct pixman_output_state *po = calloc(1, sizeof *po); |
Ander Conselvan de Oliveira | 936effd | 2013-01-25 15:13:01 +0200 | [diff] [blame] | 648 | int w, h; |
Vasily Khoruzhick | 71c2dd3 | 2013-01-07 20:39:49 +0300 | [diff] [blame] | 649 | |
| 650 | if (!po) |
| 651 | return -1; |
Ander Conselvan de Oliveira | 936effd | 2013-01-25 15:13:01 +0200 | [diff] [blame] | 652 | |
| 653 | /* set shadow image transformation */ |
| 654 | w = output->current->width; |
| 655 | h = output->current->height; |
| 656 | |
Ander Conselvan de Oliveira | 936effd | 2013-01-25 15:13:01 +0200 | [diff] [blame] | 657 | po->shadow_buffer = malloc(w * h * 4); |
| 658 | |
| 659 | if (!po->shadow_buffer) { |
| 660 | free(po); |
| 661 | return -1; |
| 662 | } |
| 663 | |
| 664 | po->shadow_image = |
| 665 | pixman_image_create_bits(PIXMAN_x8r8g8b8, w, h, |
| 666 | po->shadow_buffer, w * 4); |
| 667 | |
| 668 | if (!po->shadow_image) { |
| 669 | free(po->shadow_buffer); |
| 670 | free(po); |
| 671 | return -1; |
| 672 | } |
| 673 | |
Vasily Khoruzhick | 71c2dd3 | 2013-01-07 20:39:49 +0300 | [diff] [blame] | 674 | output->renderer_state = po; |
| 675 | |
| 676 | return 0; |
| 677 | } |
| 678 | |
| 679 | WL_EXPORT void |
| 680 | pixman_renderer_output_destroy(struct weston_output *output) |
| 681 | { |
| 682 | struct pixman_output_state *po = get_output_state(output); |
| 683 | |
Ander Conselvan de Oliveira | 936effd | 2013-01-25 15:13:01 +0200 | [diff] [blame] | 684 | pixman_image_unref(po->shadow_image); |
Ander Conselvan de Oliveira | 23e72b8 | 2013-01-25 15:13:06 +0200 | [diff] [blame] | 685 | |
| 686 | if (po->hw_buffer) |
| 687 | pixman_image_unref(po->hw_buffer); |
Ander Conselvan de Oliveira | 936effd | 2013-01-25 15:13:01 +0200 | [diff] [blame] | 688 | |
| 689 | po->shadow_image = NULL; |
Vasily Khoruzhick | 71c2dd3 | 2013-01-07 20:39:49 +0300 | [diff] [blame] | 690 | po->hw_buffer = NULL; |
| 691 | |
| 692 | free(po); |
| 693 | } |