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> |
Pekka Paalanen | 6764bbe | 2015-02-11 12:29:56 +0200 | [diff] [blame] | 4 | * Copyright © 2015 Collabora, Ltd. |
Vasily Khoruzhick | 71c2dd3 | 2013-01-07 20:39:49 +0300 | [diff] [blame] | 5 | * |
Bryce Harrington | a0bbfea | 2015-06-11 15:35:43 -0700 | [diff] [blame] | 6 | * Permission is hereby granted, free of charge, to any person obtaining |
| 7 | * a copy of this software and associated documentation files (the |
| 8 | * "Software"), to deal in the Software without restriction, including |
| 9 | * without limitation the rights to use, copy, modify, merge, publish, |
| 10 | * distribute, sublicense, and/or sell copies of the Software, and to |
| 11 | * permit persons to whom the Software is furnished to do so, subject to |
| 12 | * the following conditions: |
Vasily Khoruzhick | 71c2dd3 | 2013-01-07 20:39:49 +0300 | [diff] [blame] | 13 | * |
Bryce Harrington | a0bbfea | 2015-06-11 15:35:43 -0700 | [diff] [blame] | 14 | * The above copyright notice and this permission notice (including the |
| 15 | * next paragraph) shall be included in all copies or substantial |
| 16 | * portions of the Software. |
| 17 | * |
| 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 19 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 20 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 21 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS |
| 22 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN |
| 23 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 24 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 25 | * SOFTWARE. |
Vasily Khoruzhick | 71c2dd3 | 2013-01-07 20:39:49 +0300 | [diff] [blame] | 26 | */ |
| 27 | |
Daniel Stone | c228e23 | 2013-05-22 18:03:19 +0300 | [diff] [blame] | 28 | #include "config.h" |
Vasily Khoruzhick | 71c2dd3 | 2013-01-07 20:39:49 +0300 | [diff] [blame] | 29 | |
| 30 | #include <errno.h> |
| 31 | #include <stdlib.h> |
Pekka Paalanen | f75b6bb | 2015-03-04 16:12:04 +0200 | [diff] [blame] | 32 | #include <assert.h> |
Vasily Khoruzhick | 71c2dd3 | 2013-01-07 20:39:49 +0300 | [diff] [blame] | 33 | |
| 34 | #include "pixman-renderer.h" |
Jon Cruz | 867d50e | 2015-06-15 15:37:10 -0700 | [diff] [blame] | 35 | #include "shared/helpers.h" |
Vasily Khoruzhick | 71c2dd3 | 2013-01-07 20:39:49 +0300 | [diff] [blame] | 36 | |
Ander Conselvan de Oliveira | 8792ef1 | 2013-01-25 15:13:00 +0200 | [diff] [blame] | 37 | #include <linux/input.h> |
| 38 | |
Vasily Khoruzhick | 71c2dd3 | 2013-01-07 20:39:49 +0300 | [diff] [blame] | 39 | struct pixman_output_state { |
Ander Conselvan de Oliveira | 936effd | 2013-01-25 15:13:01 +0200 | [diff] [blame] | 40 | void *shadow_buffer; |
| 41 | pixman_image_t *shadow_image; |
Vasily Khoruzhick | 71c2dd3 | 2013-01-07 20:39:49 +0300 | [diff] [blame] | 42 | pixman_image_t *hw_buffer; |
| 43 | }; |
| 44 | |
| 45 | struct pixman_surface_state { |
Ander Conselvan de Oliveira | aa398ae | 2013-10-25 16:26:33 +0300 | [diff] [blame] | 46 | struct weston_surface *surface; |
| 47 | |
Vasily Khoruzhick | 71c2dd3 | 2013-01-07 20:39:49 +0300 | [diff] [blame] | 48 | pixman_image_t *image; |
| 49 | struct weston_buffer_reference buffer_ref; |
Ander Conselvan de Oliveira | aa398ae | 2013-10-25 16:26:33 +0300 | [diff] [blame] | 50 | |
Lubomir Rintel | ddc2b1e | 2013-12-12 12:57:56 +0100 | [diff] [blame] | 51 | struct wl_listener buffer_destroy_listener; |
Ander Conselvan de Oliveira | aa398ae | 2013-10-25 16:26:33 +0300 | [diff] [blame] | 52 | struct wl_listener surface_destroy_listener; |
Ander Conselvan de Oliveira | adda00e | 2013-10-25 16:26:34 +0300 | [diff] [blame] | 53 | struct wl_listener renderer_destroy_listener; |
Vasily Khoruzhick | 71c2dd3 | 2013-01-07 20:39:49 +0300 | [diff] [blame] | 54 | }; |
| 55 | |
| 56 | struct pixman_renderer { |
| 57 | struct weston_renderer base; |
Ander Conselvan de Oliveira | 6b16214 | 2013-10-25 16:26:32 +0300 | [diff] [blame] | 58 | |
Ander Conselvan de Oliveira | 8792ef1 | 2013-01-25 15:13:00 +0200 | [diff] [blame] | 59 | int repaint_debug; |
| 60 | pixman_image_t *debug_color; |
Ander Conselvan de Oliveira | 6b16214 | 2013-10-25 16:26:32 +0300 | [diff] [blame] | 61 | struct weston_binding *debug_binding; |
Ander Conselvan de Oliveira | adda00e | 2013-10-25 16:26:34 +0300 | [diff] [blame] | 62 | |
| 63 | struct wl_signal destroy_signal; |
Vasily Khoruzhick | 71c2dd3 | 2013-01-07 20:39:49 +0300 | [diff] [blame] | 64 | }; |
| 65 | |
| 66 | static inline struct pixman_output_state * |
| 67 | get_output_state(struct weston_output *output) |
| 68 | { |
| 69 | return (struct pixman_output_state *)output->renderer_state; |
| 70 | } |
| 71 | |
Ander Conselvan de Oliveira | aa398ae | 2013-10-25 16:26:33 +0300 | [diff] [blame] | 72 | static int |
| 73 | pixman_renderer_create_surface(struct weston_surface *surface); |
| 74 | |
Vasily Khoruzhick | 71c2dd3 | 2013-01-07 20:39:49 +0300 | [diff] [blame] | 75 | static inline struct pixman_surface_state * |
| 76 | get_surface_state(struct weston_surface *surface) |
| 77 | { |
Ander Conselvan de Oliveira | aa398ae | 2013-10-25 16:26:33 +0300 | [diff] [blame] | 78 | if (!surface->renderer_state) |
| 79 | pixman_renderer_create_surface(surface); |
| 80 | |
Vasily Khoruzhick | 71c2dd3 | 2013-01-07 20:39:49 +0300 | [diff] [blame] | 81 | return (struct pixman_surface_state *)surface->renderer_state; |
| 82 | } |
| 83 | |
| 84 | static inline struct pixman_renderer * |
| 85 | get_renderer(struct weston_compositor *ec) |
| 86 | { |
| 87 | return (struct pixman_renderer *)ec->renderer; |
| 88 | } |
| 89 | |
| 90 | static int |
| 91 | pixman_renderer_read_pixels(struct weston_output *output, |
| 92 | pixman_format_code_t format, void *pixels, |
| 93 | uint32_t x, uint32_t y, |
| 94 | uint32_t width, uint32_t height) |
| 95 | { |
| 96 | struct pixman_output_state *po = get_output_state(output); |
Alexander Larsson | 97af792 | 2013-05-29 12:01:33 +0200 | [diff] [blame] | 97 | pixman_transform_t transform; |
Vasily Khoruzhick | 71c2dd3 | 2013-01-07 20:39:49 +0300 | [diff] [blame] | 98 | pixman_image_t *out_buf; |
Vasily Khoruzhick | 71c2dd3 | 2013-01-07 20:39:49 +0300 | [diff] [blame] | 99 | |
| 100 | if (!po->hw_buffer) { |
| 101 | errno = ENODEV; |
| 102 | return -1; |
| 103 | } |
| 104 | |
| 105 | out_buf = pixman_image_create_bits(format, |
| 106 | width, |
| 107 | height, |
| 108 | pixels, |
| 109 | (PIXMAN_FORMAT_BPP(format) / 8) * width); |
| 110 | |
Alexander Larsson | 97af792 | 2013-05-29 12:01:33 +0200 | [diff] [blame] | 111 | /* Caller expects vflipped source image */ |
| 112 | pixman_transform_init_translate(&transform, |
| 113 | pixman_int_to_fixed (x), |
| 114 | pixman_int_to_fixed (y - pixman_image_get_height (po->hw_buffer))); |
| 115 | pixman_transform_scale(&transform, NULL, |
| 116 | pixman_fixed_1, |
| 117 | pixman_fixed_minus_1); |
| 118 | pixman_image_set_transform(po->hw_buffer, &transform); |
| 119 | |
| 120 | pixman_image_composite32(PIXMAN_OP_SRC, |
| 121 | po->hw_buffer, /* src */ |
| 122 | NULL /* mask */, |
| 123 | out_buf, /* dest */ |
| 124 | 0, 0, /* src_x, src_y */ |
| 125 | 0, 0, /* mask_x, mask_y */ |
| 126 | 0, 0, /* dest_x, dest_y */ |
| 127 | pixman_image_get_width (po->hw_buffer), /* width */ |
| 128 | pixman_image_get_height (po->hw_buffer) /* height */); |
| 129 | pixman_image_set_transform(po->hw_buffer, NULL); |
Vasily Khoruzhick | 71c2dd3 | 2013-01-07 20:39:49 +0300 | [diff] [blame] | 130 | |
| 131 | pixman_image_unref(out_buf); |
| 132 | |
| 133 | return 0; |
| 134 | } |
| 135 | |
Jason Ekstrand | 52c476a | 2015-11-18 16:32:29 -0600 | [diff] [blame^] | 136 | /* |
| 137 | * Warning: This function does not work for projective, affine, or matrices |
| 138 | * that encode arbitrary rotations. Only 90-degree step rotations are |
| 139 | * supported. |
| 140 | * |
| 141 | * Luckily it is only used for output matrices, so it is fine here. |
| 142 | */ |
| 143 | static void |
| 144 | weston_matrix_transform_region(pixman_region32_t *dest, |
| 145 | struct weston_matrix *matrix, |
| 146 | pixman_region32_t *src) |
| 147 | { |
| 148 | pixman_box32_t *src_rects, *dest_rects; |
| 149 | int nrects, i; |
| 150 | |
| 151 | src_rects = pixman_region32_rectangles(src, &nrects); |
| 152 | dest_rects = malloc(nrects * sizeof(*dest_rects)); |
| 153 | if (!dest_rects) |
| 154 | return; |
| 155 | |
| 156 | for (i = 0; i < nrects; i++) { |
| 157 | struct weston_vector vec1 = {{ |
| 158 | src_rects[i].x1, src_rects[i].y1, 0, 1 |
| 159 | }}; |
| 160 | weston_matrix_transform(matrix, &vec1); |
| 161 | vec1.f[0] /= vec1.f[3]; |
| 162 | vec1.f[1] /= vec1.f[3]; |
| 163 | |
| 164 | struct weston_vector vec2 = {{ |
| 165 | src_rects[i].x2, src_rects[i].y2, 0, 1 |
| 166 | }}; |
| 167 | weston_matrix_transform(matrix, &vec2); |
| 168 | vec2.f[0] /= vec2.f[3]; |
| 169 | vec2.f[1] /= vec2.f[3]; |
| 170 | |
| 171 | if (vec1.f[0] < vec2.f[0]) { |
| 172 | dest_rects[i].x1 = floor(vec1.f[0]); |
| 173 | dest_rects[i].x2 = ceil(vec2.f[0]); |
| 174 | } else { |
| 175 | dest_rects[i].x1 = floor(vec2.f[0]); |
| 176 | dest_rects[i].x2 = ceil(vec1.f[0]); |
| 177 | } |
| 178 | |
| 179 | |
| 180 | if (vec1.f[1] < vec2.f[1]) { |
| 181 | dest_rects[i].y1 = floor(vec1.f[1]); |
| 182 | dest_rects[i].y2 = ceil(vec2.f[1]); |
| 183 | } else { |
| 184 | dest_rects[i].y1 = floor(vec2.f[1]); |
| 185 | dest_rects[i].y2 = ceil(vec1.f[1]); |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | pixman_region32_clear(dest); |
| 190 | pixman_region32_init_rects(dest, dest_rects, nrects); |
| 191 | free(dest_rects); |
| 192 | } |
| 193 | |
Vasily Khoruzhick | 031fc87 | 2013-01-29 14:58:14 +0300 | [diff] [blame] | 194 | static void |
Alexander Larsson | 1f206b4 | 2013-05-22 14:41:36 +0200 | [diff] [blame] | 195 | region_global_to_output(struct weston_output *output, pixman_region32_t *region) |
| 196 | { |
Jason Ekstrand | 52c476a | 2015-11-18 16:32:29 -0600 | [diff] [blame^] | 197 | if (output->zoom.active) { |
| 198 | weston_matrix_transform_region(region, &output->matrix, region); |
| 199 | } else { |
| 200 | pixman_region32_translate(region, -output->x, -output->y); |
| 201 | weston_transformed_region(output->width, output->height, |
| 202 | output->transform, |
| 203 | output->current_scale, |
| 204 | region, region); |
| 205 | } |
Vasily Khoruzhick | 031fc87 | 2013-01-29 14:58:14 +0300 | [diff] [blame] | 206 | } |
| 207 | |
Vasily Khoruzhick | f4a457f | 2013-01-28 22:40:29 +0300 | [diff] [blame] | 208 | #define D2F(v) pixman_double_to_fixed((double)v) |
| 209 | |
Vasily Khoruzhick | 71c2dd3 | 2013-01-07 20:39:49 +0300 | [diff] [blame] | 210 | static void |
Jason Ekstrand | 8870a23 | 2014-05-20 15:53:19 -0500 | [diff] [blame] | 211 | weston_matrix_to_pixman_transform(pixman_transform_t *pt, |
| 212 | const struct weston_matrix *wm) |
Pekka Paalanen | 0b4c535 | 2014-03-14 14:38:17 +0200 | [diff] [blame] | 213 | { |
Jason Ekstrand | 8870a23 | 2014-05-20 15:53:19 -0500 | [diff] [blame] | 214 | /* Pixman supports only 2D transform matrix, but Weston uses 3D, * |
| 215 | * so we're omitting Z coordinate here. */ |
| 216 | pt->matrix[0][0] = pixman_double_to_fixed(wm->d[0]); |
| 217 | pt->matrix[0][1] = pixman_double_to_fixed(wm->d[4]); |
| 218 | pt->matrix[0][2] = pixman_double_to_fixed(wm->d[12]); |
| 219 | pt->matrix[1][0] = pixman_double_to_fixed(wm->d[1]); |
| 220 | pt->matrix[1][1] = pixman_double_to_fixed(wm->d[5]); |
| 221 | pt->matrix[1][2] = pixman_double_to_fixed(wm->d[13]); |
| 222 | pt->matrix[2][0] = pixman_double_to_fixed(wm->d[3]); |
| 223 | pt->matrix[2][1] = pixman_double_to_fixed(wm->d[7]); |
| 224 | pt->matrix[2][2] = pixman_double_to_fixed(wm->d[15]); |
Pekka Paalanen | 0b4c535 | 2014-03-14 14:38:17 +0200 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | static void |
Pekka Paalanen | 6ea523b | 2015-03-04 14:18:39 +0200 | [diff] [blame] | 228 | pixman_renderer_compute_transform(pixman_transform_t *transform_out, |
| 229 | struct weston_view *ev, |
| 230 | struct weston_output *output) |
| 231 | { |
| 232 | struct weston_matrix matrix; |
| 233 | |
| 234 | /* Set up the source transformation based on the surface |
| 235 | position, the output position/transform/scale and the client |
| 236 | specified buffer transform/scale */ |
Derek Foreman | a5855ad | 2015-03-24 11:36:16 -0500 | [diff] [blame] | 237 | matrix = output->inverse_matrix; |
Pekka Paalanen | 6ea523b | 2015-03-04 14:18:39 +0200 | [diff] [blame] | 238 | |
| 239 | if (ev->transform.enabled) { |
| 240 | weston_matrix_multiply(&matrix, &ev->transform.inverse); |
| 241 | } else { |
| 242 | weston_matrix_translate(&matrix, |
| 243 | -ev->geometry.x, -ev->geometry.y, 0); |
| 244 | } |
| 245 | |
| 246 | weston_matrix_multiply(&matrix, &ev->surface->surface_to_buffer_matrix); |
| 247 | |
| 248 | weston_matrix_to_pixman_transform(transform_out, &matrix); |
| 249 | } |
| 250 | |
Pekka Paalanen | 23d4af5 | 2015-03-04 16:18:26 +0200 | [diff] [blame] | 251 | static bool |
| 252 | view_transformation_is_translation(struct weston_view *view) |
| 253 | { |
| 254 | if (!view->transform.enabled) |
| 255 | return true; |
| 256 | |
| 257 | if (view->transform.matrix.type <= WESTON_MATRIX_TRANSFORM_TRANSLATE) |
| 258 | return true; |
| 259 | |
| 260 | return false; |
| 261 | } |
| 262 | |
Pekka Paalanen | 6ea523b | 2015-03-04 14:18:39 +0200 | [diff] [blame] | 263 | static void |
Pekka Paalanen | f75b6bb | 2015-03-04 16:12:04 +0200 | [diff] [blame] | 264 | region_intersect_only_translation(pixman_region32_t *result_global, |
| 265 | pixman_region32_t *global, |
| 266 | pixman_region32_t *surf, |
| 267 | struct weston_view *view) |
| 268 | { |
| 269 | float view_x, view_y; |
| 270 | |
| 271 | assert(view_transformation_is_translation(view)); |
| 272 | |
| 273 | /* Convert from surface to global coordinates */ |
| 274 | pixman_region32_copy(result_global, surf); |
| 275 | weston_view_to_global_float(view, 0, 0, &view_x, &view_y); |
| 276 | pixman_region32_translate(result_global, (int)view_x, (int)view_y); |
| 277 | |
| 278 | pixman_region32_intersect(result_global, result_global, global); |
| 279 | } |
| 280 | |
Pekka Paalanen | d7ca6b0 | 2015-03-05 13:25:19 +0200 | [diff] [blame] | 281 | static void |
| 282 | composite_whole(pixman_op_t op, |
| 283 | pixman_image_t *src, |
| 284 | pixman_image_t *mask, |
| 285 | pixman_image_t *dest, |
| 286 | const pixman_transform_t *transform, |
| 287 | pixman_filter_t filter) |
| 288 | { |
| 289 | int32_t dest_width; |
| 290 | int32_t dest_height; |
| 291 | |
| 292 | dest_width = pixman_image_get_width(dest); |
| 293 | dest_height = pixman_image_get_height(dest); |
| 294 | |
| 295 | pixman_image_set_transform(src, transform); |
| 296 | pixman_image_set_filter(src, filter, NULL, 0); |
| 297 | |
| 298 | pixman_image_composite32(op, src, mask, dest, |
| 299 | 0, 0, /* src_x, src_y */ |
| 300 | 0, 0, /* mask_x, mask_y */ |
| 301 | 0, 0, /* dest_x, dest_y */ |
| 302 | dest_width, dest_height); |
| 303 | } |
| 304 | |
| 305 | static void |
| 306 | composite_clipped(pixman_image_t *src, |
| 307 | pixman_image_t *mask, |
| 308 | pixman_image_t *dest, |
| 309 | const pixman_transform_t *transform, |
| 310 | pixman_filter_t filter, |
| 311 | pixman_region32_t *src_clip) |
| 312 | { |
| 313 | int n_box; |
| 314 | pixman_box32_t *boxes; |
| 315 | int32_t dest_width; |
| 316 | int32_t dest_height; |
| 317 | int src_stride; |
| 318 | int bitspp; |
| 319 | pixman_format_code_t src_format; |
| 320 | void *src_data; |
| 321 | int i; |
| 322 | |
| 323 | /* Hardcoded to use PIXMAN_OP_OVER, because sampling outside of |
| 324 | * a Pixman image produces (0,0,0,0) instead of discarding the |
| 325 | * fragment. |
| 326 | */ |
| 327 | |
| 328 | dest_width = pixman_image_get_width(dest); |
| 329 | dest_height = pixman_image_get_height(dest); |
| 330 | src_format = pixman_image_get_format(src); |
| 331 | src_stride = pixman_image_get_stride(src); |
| 332 | bitspp = PIXMAN_FORMAT_BPP(src_format); |
| 333 | src_data = pixman_image_get_data(src); |
| 334 | |
| 335 | assert(src_format); |
| 336 | |
| 337 | /* This would be massive overdraw, except when n_box is 1. */ |
| 338 | boxes = pixman_region32_rectangles(src_clip, &n_box); |
| 339 | for (i = 0; i < n_box; i++) { |
| 340 | uint8_t *ptr = src_data; |
| 341 | pixman_image_t *boximg; |
| 342 | pixman_transform_t adj = *transform; |
| 343 | |
| 344 | ptr += boxes[i].y1 * src_stride; |
| 345 | ptr += boxes[i].x1 * bitspp / 8; |
| 346 | boximg = pixman_image_create_bits_no_clear(src_format, |
| 347 | boxes[i].x2 - boxes[i].x1, |
| 348 | boxes[i].y2 - boxes[i].y1, |
| 349 | (uint32_t *)ptr, src_stride); |
| 350 | |
| 351 | pixman_transform_translate(&adj, NULL, |
| 352 | pixman_int_to_fixed(-boxes[i].x1), |
| 353 | pixman_int_to_fixed(-boxes[i].y1)); |
| 354 | pixman_image_set_transform(boximg, &adj); |
| 355 | |
| 356 | pixman_image_set_filter(boximg, filter, NULL, 0); |
| 357 | pixman_image_composite32(PIXMAN_OP_OVER, boximg, mask, dest, |
| 358 | 0, 0, /* src_x, src_y */ |
| 359 | 0, 0, /* mask_x, mask_y */ |
| 360 | 0, 0, /* dest_x, dest_y */ |
| 361 | dest_width, dest_height); |
| 362 | |
| 363 | pixman_image_unref(boximg); |
| 364 | } |
| 365 | |
| 366 | if (n_box > 1) { |
| 367 | static bool warned = false; |
| 368 | |
| 369 | if (!warned) |
| 370 | weston_log("Pixman-renderer warning: %dx overdraw\n", |
| 371 | n_box); |
| 372 | warned = true; |
| 373 | } |
| 374 | } |
| 375 | |
Pekka Paalanen | 11af0d9 | 2015-03-05 11:56:29 +0200 | [diff] [blame] | 376 | /** Paint an intersected region |
| 377 | * |
| 378 | * \param ev The view to be painted. |
| 379 | * \param output The output being painted. |
| 380 | * \param repaint_output The region to be painted in output coordinates. |
Pekka Paalanen | d7ca6b0 | 2015-03-05 13:25:19 +0200 | [diff] [blame] | 381 | * \param source_clip The region of the source image to use, in source image |
| 382 | * coordinates. If NULL, use the whole source image. |
Pekka Paalanen | 11af0d9 | 2015-03-05 11:56:29 +0200 | [diff] [blame] | 383 | * \param pixman_op Compositing operator, either SRC or OVER. |
| 384 | */ |
Pekka Paalanen | f75b6bb | 2015-03-04 16:12:04 +0200 | [diff] [blame] | 385 | static void |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 386 | repaint_region(struct weston_view *ev, struct weston_output *output, |
Pekka Paalanen | 11af0d9 | 2015-03-05 11:56:29 +0200 | [diff] [blame] | 387 | pixman_region32_t *repaint_output, |
Pekka Paalanen | d7ca6b0 | 2015-03-05 13:25:19 +0200 | [diff] [blame] | 388 | pixman_region32_t *source_clip, |
Alexander Larsson | 1f206b4 | 2013-05-22 14:41:36 +0200 | [diff] [blame] | 389 | pixman_op_t pixman_op) |
Vasily Khoruzhick | 71c2dd3 | 2013-01-07 20:39:49 +0300 | [diff] [blame] | 390 | { |
Ander Conselvan de Oliveira | 8792ef1 | 2013-01-25 15:13:00 +0200 | [diff] [blame] | 391 | struct pixman_renderer *pr = |
| 392 | (struct pixman_renderer *) output->compositor->renderer; |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 393 | struct pixman_surface_state *ps = get_surface_state(ev->surface); |
Vasily Khoruzhick | 71c2dd3 | 2013-01-07 20:39:49 +0300 | [diff] [blame] | 394 | struct pixman_output_state *po = get_output_state(output); |
Pekka Paalanen | 952b6c8 | 2014-03-14 14:38:15 +0200 | [diff] [blame] | 395 | struct weston_buffer_viewport *vp = &ev->surface->buffer_viewport; |
Alexander Larsson | 1f206b4 | 2013-05-22 14:41:36 +0200 | [diff] [blame] | 396 | pixman_transform_t transform; |
Pekka Paalanen | d7ca6b0 | 2015-03-05 13:25:19 +0200 | [diff] [blame] | 397 | pixman_filter_t filter; |
Manuel Bachmann | 9c4ab66 | 2014-04-07 11:58:45 +0200 | [diff] [blame] | 398 | pixman_image_t *mask_image; |
| 399 | pixman_color_t mask = { 0, }; |
Vasily Khoruzhick | 71c2dd3 | 2013-01-07 20:39:49 +0300 | [diff] [blame] | 400 | |
Pekka Paalanen | 11af0d9 | 2015-03-05 11:56:29 +0200 | [diff] [blame] | 401 | /* Clip rendering to the damaged output region */ |
| 402 | pixman_image_set_clip_region32(po->shadow_image, repaint_output); |
Ander Conselvan de Oliveira | 8792ef1 | 2013-01-25 15:13:00 +0200 | [diff] [blame] | 403 | |
Pekka Paalanen | 6ea523b | 2015-03-04 14:18:39 +0200 | [diff] [blame] | 404 | pixman_renderer_compute_transform(&transform, ev, output); |
Alexander Larsson | 1f206b4 | 2013-05-22 14:41:36 +0200 | [diff] [blame] | 405 | |
Pekka Paalanen | 952b6c8 | 2014-03-14 14:38:15 +0200 | [diff] [blame] | 406 | if (ev->transform.enabled || output->current_scale != vp->buffer.scale) |
Pekka Paalanen | d7ca6b0 | 2015-03-05 13:25:19 +0200 | [diff] [blame] | 407 | filter = PIXMAN_FILTER_BILINEAR; |
Alexander Larsson | 1f206b4 | 2013-05-22 14:41:36 +0200 | [diff] [blame] | 408 | else |
Pekka Paalanen | d7ca6b0 | 2015-03-05 13:25:19 +0200 | [diff] [blame] | 409 | filter = PIXMAN_FILTER_NEAREST; |
Alexander Larsson | 1f206b4 | 2013-05-22 14:41:36 +0200 | [diff] [blame] | 410 | |
Neil Roberts | e505171 | 2013-11-13 15:44:06 +0000 | [diff] [blame] | 411 | if (ps->buffer_ref.buffer) |
| 412 | wl_shm_buffer_begin_access(ps->buffer_ref.buffer->shm_buffer); |
| 413 | |
Manuel Bachmann | b1fff41 | 2014-04-05 05:31:37 +0200 | [diff] [blame] | 414 | if (ev->alpha < 1.0) { |
Manuel Bachmann | 9c4ab66 | 2014-04-07 11:58:45 +0200 | [diff] [blame] | 415 | mask.alpha = 0xffff * ev->alpha; |
Manuel Bachmann | b1fff41 | 2014-04-05 05:31:37 +0200 | [diff] [blame] | 416 | mask_image = pixman_image_create_solid_fill(&mask); |
Manuel Bachmann | 9c4ab66 | 2014-04-07 11:58:45 +0200 | [diff] [blame] | 417 | } else { |
Manuel Bachmann | b1fff41 | 2014-04-05 05:31:37 +0200 | [diff] [blame] | 418 | mask_image = NULL; |
Manuel Bachmann | 9c4ab66 | 2014-04-07 11:58:45 +0200 | [diff] [blame] | 419 | } |
Manuel Bachmann | b1fff41 | 2014-04-05 05:31:37 +0200 | [diff] [blame] | 420 | |
Pekka Paalanen | d7ca6b0 | 2015-03-05 13:25:19 +0200 | [diff] [blame] | 421 | if (source_clip) |
| 422 | composite_clipped(ps->image, mask_image, po->shadow_image, |
| 423 | &transform, filter, source_clip); |
| 424 | else |
| 425 | composite_whole(pixman_op, ps->image, mask_image, |
| 426 | po->shadow_image, &transform, filter); |
Alexander Larsson | 1f206b4 | 2013-05-22 14:41:36 +0200 | [diff] [blame] | 427 | |
Manuel Bachmann | 9c4ab66 | 2014-04-07 11:58:45 +0200 | [diff] [blame] | 428 | if (mask_image) |
| 429 | pixman_image_unref(mask_image); |
| 430 | |
Neil Roberts | e505171 | 2013-11-13 15:44:06 +0000 | [diff] [blame] | 431 | if (ps->buffer_ref.buffer) |
| 432 | wl_shm_buffer_end_access(ps->buffer_ref.buffer->shm_buffer); |
| 433 | |
Alexander Larsson | 1f206b4 | 2013-05-22 14:41:36 +0200 | [diff] [blame] | 434 | if (pr->repaint_debug) |
Ander Conselvan de Oliveira | 8792ef1 | 2013-01-25 15:13:00 +0200 | [diff] [blame] | 435 | pixman_image_composite32(PIXMAN_OP_OVER, |
Alexander Larsson | 1f206b4 | 2013-05-22 14:41:36 +0200 | [diff] [blame] | 436 | pr->debug_color, /* src */ |
| 437 | NULL /* mask */, |
| 438 | po->shadow_image, /* dest */ |
| 439 | 0, 0, /* src_x, src_y */ |
| 440 | 0, 0, /* mask_x, mask_y */ |
| 441 | 0, 0, /* dest_x, dest_y */ |
| 442 | pixman_image_get_width (po->shadow_image), /* width */ |
| 443 | pixman_image_get_height (po->shadow_image) /* height */); |
| 444 | |
| 445 | pixman_image_set_clip_region32 (po->shadow_image, NULL); |
Vasily Khoruzhick | 71c2dd3 | 2013-01-07 20:39:49 +0300 | [diff] [blame] | 446 | } |
| 447 | |
| 448 | static void |
Pekka Paalanen | ccf99ce | 2015-03-05 12:57:35 +0200 | [diff] [blame] | 449 | draw_view_translated(struct weston_view *view, struct weston_output *output, |
| 450 | pixman_region32_t *repaint_global) |
| 451 | { |
| 452 | struct weston_surface *surface = view->surface; |
| 453 | /* non-opaque region in surface coordinates: */ |
| 454 | pixman_region32_t surface_blend; |
| 455 | /* region to be painted in output coordinates: */ |
| 456 | pixman_region32_t repaint_output; |
| 457 | |
| 458 | pixman_region32_init(&repaint_output); |
| 459 | |
| 460 | /* Blended region is whole surface minus opaque region, |
| 461 | * unless surface alpha forces us to blend all. |
| 462 | */ |
| 463 | pixman_region32_init_rect(&surface_blend, 0, 0, |
| 464 | surface->width, surface->height); |
| 465 | |
| 466 | if (!(view->alpha < 1.0)) { |
| 467 | pixman_region32_subtract(&surface_blend, &surface_blend, |
| 468 | &surface->opaque); |
| 469 | |
| 470 | if (pixman_region32_not_empty(&surface->opaque)) { |
| 471 | region_intersect_only_translation(&repaint_output, |
| 472 | repaint_global, |
| 473 | &surface->opaque, |
| 474 | view); |
| 475 | region_global_to_output(output, &repaint_output); |
| 476 | |
Pekka Paalanen | d7ca6b0 | 2015-03-05 13:25:19 +0200 | [diff] [blame] | 477 | repaint_region(view, output, &repaint_output, NULL, |
Pekka Paalanen | ccf99ce | 2015-03-05 12:57:35 +0200 | [diff] [blame] | 478 | PIXMAN_OP_SRC); |
| 479 | } |
| 480 | } |
| 481 | |
| 482 | if (pixman_region32_not_empty(&surface_blend)) { |
| 483 | region_intersect_only_translation(&repaint_output, |
| 484 | repaint_global, |
| 485 | &surface_blend, view); |
| 486 | region_global_to_output(output, &repaint_output); |
| 487 | |
Pekka Paalanen | d7ca6b0 | 2015-03-05 13:25:19 +0200 | [diff] [blame] | 488 | repaint_region(view, output, &repaint_output, NULL, |
Pekka Paalanen | ccf99ce | 2015-03-05 12:57:35 +0200 | [diff] [blame] | 489 | PIXMAN_OP_OVER); |
| 490 | } |
| 491 | |
| 492 | pixman_region32_fini(&surface_blend); |
| 493 | pixman_region32_fini(&repaint_output); |
| 494 | } |
| 495 | |
| 496 | static void |
Pekka Paalanen | d7ca6b0 | 2015-03-05 13:25:19 +0200 | [diff] [blame] | 497 | draw_view_source_clipped(struct weston_view *view, |
| 498 | struct weston_output *output, |
| 499 | pixman_region32_t *repaint_global) |
| 500 | { |
| 501 | struct weston_surface *surface = view->surface; |
| 502 | pixman_region32_t surf_region; |
| 503 | pixman_region32_t buffer_region; |
| 504 | pixman_region32_t repaint_output; |
| 505 | |
| 506 | /* Do not bother separating the opaque region from non-opaque. |
| 507 | * Source clipping requires PIXMAN_OP_OVER in all cases, so painting |
| 508 | * opaque separately has no benefit. |
| 509 | */ |
| 510 | |
| 511 | pixman_region32_init_rect(&surf_region, 0, 0, |
| 512 | surface->width, surface->height); |
Pekka Paalanen | c93782a | 2015-03-06 10:33:50 +0200 | [diff] [blame] | 513 | if (view->geometry.scissor_enabled) |
| 514 | pixman_region32_intersect(&surf_region, &surf_region, |
| 515 | &view->geometry.scissor); |
| 516 | |
Pekka Paalanen | d7ca6b0 | 2015-03-05 13:25:19 +0200 | [diff] [blame] | 517 | pixman_region32_init(&buffer_region); |
| 518 | weston_surface_to_buffer_region(surface, &surf_region, &buffer_region); |
| 519 | |
| 520 | pixman_region32_init(&repaint_output); |
| 521 | pixman_region32_copy(&repaint_output, repaint_global); |
| 522 | region_global_to_output(output, &repaint_output); |
| 523 | |
| 524 | repaint_region(view, output, &repaint_output, &buffer_region, |
| 525 | PIXMAN_OP_OVER); |
| 526 | |
| 527 | pixman_region32_fini(&repaint_output); |
| 528 | pixman_region32_fini(&buffer_region); |
| 529 | pixman_region32_fini(&surf_region); |
| 530 | } |
| 531 | |
| 532 | static void |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 533 | draw_view(struct weston_view *ev, struct weston_output *output, |
| 534 | pixman_region32_t *damage) /* in global coordinates */ |
Vasily Khoruzhick | 71c2dd3 | 2013-01-07 20:39:49 +0300 | [diff] [blame] | 535 | { |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 536 | struct pixman_surface_state *ps = get_surface_state(ev->surface); |
Vasily Khoruzhick | 71c2dd3 | 2013-01-07 20:39:49 +0300 | [diff] [blame] | 537 | /* repaint bounding region in global coordinates: */ |
| 538 | pixman_region32_t repaint; |
Vasily Khoruzhick | 71c2dd3 | 2013-01-07 20:39:49 +0300 | [diff] [blame] | 539 | |
| 540 | /* No buffer attached */ |
| 541 | if (!ps->image) |
| 542 | return; |
| 543 | |
| 544 | pixman_region32_init(&repaint); |
| 545 | pixman_region32_intersect(&repaint, |
Pekka Paalanen | 25c0ca5 | 2015-02-19 11:15:33 +0200 | [diff] [blame] | 546 | &ev->transform.boundingbox, damage); |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 547 | pixman_region32_subtract(&repaint, &repaint, &ev->clip); |
Vasily Khoruzhick | 71c2dd3 | 2013-01-07 20:39:49 +0300 | [diff] [blame] | 548 | |
| 549 | if (!pixman_region32_not_empty(&repaint)) |
| 550 | goto out; |
| 551 | |
Pekka Paalanen | ccf99ce | 2015-03-05 12:57:35 +0200 | [diff] [blame] | 552 | if (view_transformation_is_translation(ev)) { |
Pekka Paalanen | d7ca6b0 | 2015-03-05 13:25:19 +0200 | [diff] [blame] | 553 | /* The simple case: The surface regions opaque, non-opaque, |
| 554 | * etc. are convertible to global coordinate space. |
| 555 | * There is no need to use a source clip region. |
| 556 | * It is possible to paint opaque region as PIXMAN_OP_SRC. |
Pekka Paalanen | c93782a | 2015-03-06 10:33:50 +0200 | [diff] [blame] | 557 | * Also the boundingbox is accurate rather than an |
| 558 | * approximation. |
Pekka Paalanen | d7ca6b0 | 2015-03-05 13:25:19 +0200 | [diff] [blame] | 559 | */ |
Pekka Paalanen | ccf99ce | 2015-03-05 12:57:35 +0200 | [diff] [blame] | 560 | draw_view_translated(ev, output, &repaint); |
| 561 | } else { |
Pekka Paalanen | d7ca6b0 | 2015-03-05 13:25:19 +0200 | [diff] [blame] | 562 | /* The complex case: the view transformation does not allow |
| 563 | * converting opaque etc. regions into global coordinate space. |
| 564 | * Therefore we need source clipping to avoid sampling from |
| 565 | * unwanted source image areas, unless the source image is |
| 566 | * to be used whole. Source clipping does not work with |
| 567 | * PIXMAN_OP_SRC. |
| 568 | */ |
| 569 | draw_view_source_clipped(ev, output, &repaint); |
Vasily Khoruzhick | 71c2dd3 | 2013-01-07 20:39:49 +0300 | [diff] [blame] | 570 | } |
| 571 | |
Vasily Khoruzhick | 71c2dd3 | 2013-01-07 20:39:49 +0300 | [diff] [blame] | 572 | out: |
| 573 | pixman_region32_fini(&repaint); |
| 574 | } |
| 575 | static void |
| 576 | repaint_surfaces(struct weston_output *output, pixman_region32_t *damage) |
| 577 | { |
| 578 | struct weston_compositor *compositor = output->compositor; |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 579 | struct weston_view *view; |
Vasily Khoruzhick | 71c2dd3 | 2013-01-07 20:39:49 +0300 | [diff] [blame] | 580 | |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 581 | wl_list_for_each_reverse(view, &compositor->view_list, link) |
| 582 | if (view->plane == &compositor->primary_plane) |
| 583 | draw_view(view, output, damage); |
Vasily Khoruzhick | 71c2dd3 | 2013-01-07 20:39:49 +0300 | [diff] [blame] | 584 | } |
| 585 | |
| 586 | static void |
Ander Conselvan de Oliveira | 936effd | 2013-01-25 15:13:01 +0200 | [diff] [blame] | 587 | copy_to_hw_buffer(struct weston_output *output, pixman_region32_t *region) |
| 588 | { |
| 589 | struct pixman_output_state *po = get_output_state(output); |
Alexander Larsson | 1f206b4 | 2013-05-22 14:41:36 +0200 | [diff] [blame] | 590 | pixman_region32_t output_region; |
Ander Conselvan de Oliveira | 936effd | 2013-01-25 15:13:01 +0200 | [diff] [blame] | 591 | |
Alexander Larsson | 1f206b4 | 2013-05-22 14:41:36 +0200 | [diff] [blame] | 592 | pixman_region32_init(&output_region); |
| 593 | pixman_region32_copy(&output_region, region); |
Ander Conselvan de Oliveira | 936effd | 2013-01-25 15:13:01 +0200 | [diff] [blame] | 594 | |
Alexander Larsson | 1f206b4 | 2013-05-22 14:41:36 +0200 | [diff] [blame] | 595 | region_global_to_output(output, &output_region); |
Ander Conselvan de Oliveira | 936effd | 2013-01-25 15:13:01 +0200 | [diff] [blame] | 596 | |
Alexander Larsson | 1f206b4 | 2013-05-22 14:41:36 +0200 | [diff] [blame] | 597 | pixman_image_set_clip_region32 (po->hw_buffer, &output_region); |
Ryo Munakata | 389d205 | 2014-09-04 01:56:53 +0900 | [diff] [blame] | 598 | pixman_region32_fini(&output_region); |
Ander Conselvan de Oliveira | 936effd | 2013-01-25 15:13:01 +0200 | [diff] [blame] | 599 | |
Alexander Larsson | 1f206b4 | 2013-05-22 14:41:36 +0200 | [diff] [blame] | 600 | pixman_image_composite32(PIXMAN_OP_SRC, |
| 601 | po->shadow_image, /* src */ |
| 602 | NULL /* mask */, |
| 603 | po->hw_buffer, /* dest */ |
| 604 | 0, 0, /* src_x, src_y */ |
| 605 | 0, 0, /* mask_x, mask_y */ |
| 606 | 0, 0, /* dest_x, dest_y */ |
| 607 | pixman_image_get_width (po->hw_buffer), /* width */ |
| 608 | pixman_image_get_height (po->hw_buffer) /* height */); |
| 609 | |
| 610 | pixman_image_set_clip_region32 (po->hw_buffer, NULL); |
Ander Conselvan de Oliveira | 936effd | 2013-01-25 15:13:01 +0200 | [diff] [blame] | 611 | } |
| 612 | |
| 613 | static void |
Vasily Khoruzhick | 71c2dd3 | 2013-01-07 20:39:49 +0300 | [diff] [blame] | 614 | pixman_renderer_repaint_output(struct weston_output *output, |
| 615 | pixman_region32_t *output_damage) |
| 616 | { |
| 617 | struct pixman_output_state *po = get_output_state(output); |
| 618 | |
| 619 | if (!po->hw_buffer) |
| 620 | return; |
| 621 | |
| 622 | repaint_surfaces(output, output_damage); |
Ander Conselvan de Oliveira | 936effd | 2013-01-25 15:13:01 +0200 | [diff] [blame] | 623 | copy_to_hw_buffer(output, output_damage); |
Vasily Khoruzhick | 71c2dd3 | 2013-01-07 20:39:49 +0300 | [diff] [blame] | 624 | |
| 625 | pixman_region32_copy(&output->previous_damage, output_damage); |
| 626 | wl_signal_emit(&output->frame_signal, output); |
| 627 | |
| 628 | /* Actual flip should be done by caller */ |
| 629 | } |
| 630 | |
| 631 | static void |
| 632 | pixman_renderer_flush_damage(struct weston_surface *surface) |
| 633 | { |
| 634 | /* No-op for pixman renderer */ |
| 635 | } |
| 636 | |
| 637 | static void |
Lubomir Rintel | ddc2b1e | 2013-12-12 12:57:56 +0100 | [diff] [blame] | 638 | buffer_state_handle_buffer_destroy(struct wl_listener *listener, void *data) |
| 639 | { |
| 640 | struct pixman_surface_state *ps; |
| 641 | |
| 642 | ps = container_of(listener, struct pixman_surface_state, |
| 643 | buffer_destroy_listener); |
| 644 | |
| 645 | if (ps->image) { |
| 646 | pixman_image_unref(ps->image); |
| 647 | ps->image = NULL; |
| 648 | } |
| 649 | |
| 650 | ps->buffer_destroy_listener.notify = NULL; |
| 651 | } |
| 652 | |
| 653 | static void |
Jason Ekstrand | 6bd6294 | 2013-06-20 20:38:23 -0500 | [diff] [blame] | 654 | pixman_renderer_attach(struct weston_surface *es, struct weston_buffer *buffer) |
Vasily Khoruzhick | 71c2dd3 | 2013-01-07 20:39:49 +0300 | [diff] [blame] | 655 | { |
| 656 | struct pixman_surface_state *ps = get_surface_state(es); |
Jason Ekstrand | 6bd6294 | 2013-06-20 20:38:23 -0500 | [diff] [blame] | 657 | struct wl_shm_buffer *shm_buffer; |
Vasily Khoruzhick | 71c2dd3 | 2013-01-07 20:39:49 +0300 | [diff] [blame] | 658 | pixman_format_code_t pixman_format; |
| 659 | |
| 660 | weston_buffer_reference(&ps->buffer_ref, buffer); |
| 661 | |
Lubomir Rintel | ddc2b1e | 2013-12-12 12:57:56 +0100 | [diff] [blame] | 662 | if (ps->buffer_destroy_listener.notify) { |
| 663 | wl_list_remove(&ps->buffer_destroy_listener.link); |
| 664 | ps->buffer_destroy_listener.notify = NULL; |
| 665 | } |
| 666 | |
Vasily Khoruzhick | 71c2dd3 | 2013-01-07 20:39:49 +0300 | [diff] [blame] | 667 | if (ps->image) { |
| 668 | pixman_image_unref(ps->image); |
| 669 | ps->image = NULL; |
| 670 | } |
| 671 | |
| 672 | if (!buffer) |
| 673 | return; |
Jason Ekstrand | 52c476a | 2015-11-18 16:32:29 -0600 | [diff] [blame^] | 674 | |
Jason Ekstrand | 6bd6294 | 2013-06-20 20:38:23 -0500 | [diff] [blame] | 675 | shm_buffer = wl_shm_buffer_get(buffer->resource); |
Vasily Khoruzhick | 71c2dd3 | 2013-01-07 20:39:49 +0300 | [diff] [blame] | 676 | |
Jason Ekstrand | 6bd6294 | 2013-06-20 20:38:23 -0500 | [diff] [blame] | 677 | if (! shm_buffer) { |
Vasily Khoruzhick | 71c2dd3 | 2013-01-07 20:39:49 +0300 | [diff] [blame] | 678 | weston_log("Pixman renderer supports only SHM buffers\n"); |
| 679 | weston_buffer_reference(&ps->buffer_ref, NULL); |
| 680 | return; |
| 681 | } |
| 682 | |
Jason Ekstrand | 6bd6294 | 2013-06-20 20:38:23 -0500 | [diff] [blame] | 683 | switch (wl_shm_buffer_get_format(shm_buffer)) { |
Vasily Khoruzhick | 71c2dd3 | 2013-01-07 20:39:49 +0300 | [diff] [blame] | 684 | case WL_SHM_FORMAT_XRGB8888: |
| 685 | pixman_format = PIXMAN_x8r8g8b8; |
| 686 | break; |
| 687 | case WL_SHM_FORMAT_ARGB8888: |
| 688 | pixman_format = PIXMAN_a8r8g8b8; |
| 689 | break; |
Tomeu Vizoso | 1c1fc29 | 2013-08-06 20:05:56 +0200 | [diff] [blame] | 690 | case WL_SHM_FORMAT_RGB565: |
| 691 | pixman_format = PIXMAN_r5g6b5; |
| 692 | break; |
Vasily Khoruzhick | 71c2dd3 | 2013-01-07 20:39:49 +0300 | [diff] [blame] | 693 | default: |
| 694 | weston_log("Unsupported SHM buffer format\n"); |
| 695 | weston_buffer_reference(&ps->buffer_ref, NULL); |
| 696 | return; |
| 697 | break; |
| 698 | } |
Jason Ekstrand | 6bd6294 | 2013-06-20 20:38:23 -0500 | [diff] [blame] | 699 | |
| 700 | buffer->shm_buffer = shm_buffer; |
| 701 | buffer->width = wl_shm_buffer_get_width(shm_buffer); |
| 702 | buffer->height = wl_shm_buffer_get_height(shm_buffer); |
| 703 | |
Vasily Khoruzhick | 71c2dd3 | 2013-01-07 20:39:49 +0300 | [diff] [blame] | 704 | ps->image = pixman_image_create_bits(pixman_format, |
Jason Ekstrand | 6bd6294 | 2013-06-20 20:38:23 -0500 | [diff] [blame] | 705 | buffer->width, buffer->height, |
| 706 | wl_shm_buffer_get_data(shm_buffer), |
| 707 | wl_shm_buffer_get_stride(shm_buffer)); |
Lubomir Rintel | ddc2b1e | 2013-12-12 12:57:56 +0100 | [diff] [blame] | 708 | |
| 709 | ps->buffer_destroy_listener.notify = |
| 710 | buffer_state_handle_buffer_destroy; |
| 711 | wl_signal_add(&buffer->destroy_signal, |
| 712 | &ps->buffer_destroy_listener); |
Vasily Khoruzhick | 71c2dd3 | 2013-01-07 20:39:49 +0300 | [diff] [blame] | 713 | } |
| 714 | |
Ander Conselvan de Oliveira | aa398ae | 2013-10-25 16:26:33 +0300 | [diff] [blame] | 715 | static void |
Ander Conselvan de Oliveira | adda00e | 2013-10-25 16:26:34 +0300 | [diff] [blame] | 716 | pixman_renderer_surface_state_destroy(struct pixman_surface_state *ps) |
Ander Conselvan de Oliveira | aa398ae | 2013-10-25 16:26:33 +0300 | [diff] [blame] | 717 | { |
Ander Conselvan de Oliveira | adda00e | 2013-10-25 16:26:34 +0300 | [diff] [blame] | 718 | wl_list_remove(&ps->surface_destroy_listener.link); |
| 719 | wl_list_remove(&ps->renderer_destroy_listener.link); |
Lubomir Rintel | ddc2b1e | 2013-12-12 12:57:56 +0100 | [diff] [blame] | 720 | if (ps->buffer_destroy_listener.notify) { |
| 721 | wl_list_remove(&ps->buffer_destroy_listener.link); |
| 722 | ps->buffer_destroy_listener.notify = NULL; |
| 723 | } |
Ander Conselvan de Oliveira | aa398ae | 2013-10-25 16:26:33 +0300 | [diff] [blame] | 724 | |
| 725 | ps->surface->renderer_state = NULL; |
| 726 | |
| 727 | if (ps->image) { |
| 728 | pixman_image_unref(ps->image); |
| 729 | ps->image = NULL; |
| 730 | } |
| 731 | weston_buffer_reference(&ps->buffer_ref, NULL); |
| 732 | free(ps); |
| 733 | } |
| 734 | |
Ander Conselvan de Oliveira | adda00e | 2013-10-25 16:26:34 +0300 | [diff] [blame] | 735 | static void |
| 736 | surface_state_handle_surface_destroy(struct wl_listener *listener, void *data) |
| 737 | { |
| 738 | struct pixman_surface_state *ps; |
| 739 | |
| 740 | ps = container_of(listener, struct pixman_surface_state, |
| 741 | surface_destroy_listener); |
| 742 | |
| 743 | pixman_renderer_surface_state_destroy(ps); |
| 744 | } |
| 745 | |
| 746 | static void |
| 747 | surface_state_handle_renderer_destroy(struct wl_listener *listener, void *data) |
| 748 | { |
| 749 | struct pixman_surface_state *ps; |
| 750 | |
| 751 | ps = container_of(listener, struct pixman_surface_state, |
| 752 | renderer_destroy_listener); |
| 753 | |
| 754 | pixman_renderer_surface_state_destroy(ps); |
| 755 | } |
| 756 | |
Vasily Khoruzhick | 71c2dd3 | 2013-01-07 20:39:49 +0300 | [diff] [blame] | 757 | static int |
| 758 | pixman_renderer_create_surface(struct weston_surface *surface) |
| 759 | { |
| 760 | struct pixman_surface_state *ps; |
Ander Conselvan de Oliveira | adda00e | 2013-10-25 16:26:34 +0300 | [diff] [blame] | 761 | struct pixman_renderer *pr = get_renderer(surface->compositor); |
Vasily Khoruzhick | 71c2dd3 | 2013-01-07 20:39:49 +0300 | [diff] [blame] | 762 | |
Bryce Harrington | de16d89 | 2014-11-20 22:21:57 -0800 | [diff] [blame] | 763 | ps = zalloc(sizeof *ps); |
| 764 | if (ps == NULL) |
Vasily Khoruzhick | 71c2dd3 | 2013-01-07 20:39:49 +0300 | [diff] [blame] | 765 | return -1; |
| 766 | |
| 767 | surface->renderer_state = ps; |
| 768 | |
Ander Conselvan de Oliveira | aa398ae | 2013-10-25 16:26:33 +0300 | [diff] [blame] | 769 | ps->surface = surface; |
| 770 | |
| 771 | ps->surface_destroy_listener.notify = |
| 772 | surface_state_handle_surface_destroy; |
| 773 | wl_signal_add(&surface->destroy_signal, |
| 774 | &ps->surface_destroy_listener); |
| 775 | |
Ander Conselvan de Oliveira | adda00e | 2013-10-25 16:26:34 +0300 | [diff] [blame] | 776 | ps->renderer_destroy_listener.notify = |
| 777 | surface_state_handle_renderer_destroy; |
| 778 | wl_signal_add(&pr->destroy_signal, |
| 779 | &ps->renderer_destroy_listener); |
| 780 | |
Vasily Khoruzhick | 71c2dd3 | 2013-01-07 20:39:49 +0300 | [diff] [blame] | 781 | return 0; |
| 782 | } |
| 783 | |
| 784 | static void |
| 785 | pixman_renderer_surface_set_color(struct weston_surface *es, |
| 786 | float red, float green, float blue, float alpha) |
| 787 | { |
| 788 | struct pixman_surface_state *ps = get_surface_state(es); |
| 789 | pixman_color_t color; |
| 790 | |
| 791 | color.red = red * 0xffff; |
| 792 | color.green = green * 0xffff; |
| 793 | color.blue = blue * 0xffff; |
| 794 | color.alpha = alpha * 0xffff; |
Jason Ekstrand | 52c476a | 2015-11-18 16:32:29 -0600 | [diff] [blame^] | 795 | |
Vasily Khoruzhick | 71c2dd3 | 2013-01-07 20:39:49 +0300 | [diff] [blame] | 796 | if (ps->image) { |
| 797 | pixman_image_unref(ps->image); |
| 798 | ps->image = NULL; |
| 799 | } |
| 800 | |
| 801 | ps->image = pixman_image_create_solid_fill(&color); |
| 802 | } |
| 803 | |
| 804 | static void |
Vasily Khoruzhick | 71c2dd3 | 2013-01-07 20:39:49 +0300 | [diff] [blame] | 805 | pixman_renderer_destroy(struct weston_compositor *ec) |
| 806 | { |
Ander Conselvan de Oliveira | 6b16214 | 2013-10-25 16:26:32 +0300 | [diff] [blame] | 807 | struct pixman_renderer *pr = get_renderer(ec); |
| 808 | |
Ander Conselvan de Oliveira | adda00e | 2013-10-25 16:26:34 +0300 | [diff] [blame] | 809 | wl_signal_emit(&pr->destroy_signal, pr); |
Ander Conselvan de Oliveira | 6b16214 | 2013-10-25 16:26:32 +0300 | [diff] [blame] | 810 | weston_binding_destroy(pr->debug_binding); |
| 811 | free(pr); |
| 812 | |
Vasily Khoruzhick | 71c2dd3 | 2013-01-07 20:39:49 +0300 | [diff] [blame] | 813 | ec->renderer = NULL; |
| 814 | } |
| 815 | |
Ander Conselvan de Oliveira | 8792ef1 | 2013-01-25 15:13:00 +0200 | [diff] [blame] | 816 | static void |
Pekka Paalanen | 6764bbe | 2015-02-11 12:29:56 +0200 | [diff] [blame] | 817 | pixman_renderer_surface_get_content_size(struct weston_surface *surface, |
| 818 | int *width, int *height) |
| 819 | { |
| 820 | struct pixman_surface_state *ps = get_surface_state(surface); |
| 821 | |
| 822 | if (ps->image) { |
| 823 | *width = pixman_image_get_width(ps->image); |
| 824 | *height = pixman_image_get_height(ps->image); |
| 825 | } else { |
| 826 | *width = 0; |
| 827 | *height = 0; |
| 828 | } |
| 829 | } |
| 830 | |
| 831 | static int |
| 832 | pixman_renderer_surface_copy_content(struct weston_surface *surface, |
| 833 | void *target, size_t size, |
| 834 | int src_x, int src_y, |
| 835 | int width, int height) |
| 836 | { |
| 837 | const pixman_format_code_t format = PIXMAN_a8b8g8r8; |
| 838 | const size_t bytespp = 4; /* PIXMAN_a8b8g8r8 */ |
| 839 | struct pixman_surface_state *ps = get_surface_state(surface); |
| 840 | pixman_image_t *out_buf; |
| 841 | |
| 842 | if (!ps->image) |
| 843 | return -1; |
| 844 | |
| 845 | out_buf = pixman_image_create_bits(format, width, height, |
| 846 | target, width * bytespp); |
| 847 | |
| 848 | pixman_image_set_transform(ps->image, NULL); |
| 849 | pixman_image_composite32(PIXMAN_OP_SRC, |
| 850 | ps->image, /* src */ |
| 851 | NULL, /* mask */ |
| 852 | out_buf, /* dest */ |
| 853 | src_x, src_y, /* src_x, src_y */ |
| 854 | 0, 0, /* mask_x, mask_y */ |
| 855 | 0, 0, /* dest_x, dest_y */ |
| 856 | width, height); |
| 857 | |
| 858 | pixman_image_unref(out_buf); |
| 859 | |
| 860 | return 0; |
| 861 | } |
| 862 | |
| 863 | static void |
Derek Foreman | 8ae2db5 | 2015-07-15 13:00:36 -0500 | [diff] [blame] | 864 | debug_binding(struct weston_keyboard *keyboard, uint32_t time, uint32_t key, |
Ander Conselvan de Oliveira | 8792ef1 | 2013-01-25 15:13:00 +0200 | [diff] [blame] | 865 | void *data) |
| 866 | { |
| 867 | struct weston_compositor *ec = data; |
| 868 | struct pixman_renderer *pr = (struct pixman_renderer *) ec->renderer; |
| 869 | |
| 870 | pr->repaint_debug ^= 1; |
| 871 | |
| 872 | if (pr->repaint_debug) { |
| 873 | pixman_color_t red = { |
| 874 | 0x3fff, 0x0000, 0x0000, 0x3fff |
| 875 | }; |
| 876 | |
| 877 | pr->debug_color = pixman_image_create_solid_fill(&red); |
| 878 | } else { |
| 879 | pixman_image_unref(pr->debug_color); |
| 880 | weston_compositor_damage_all(ec); |
| 881 | } |
| 882 | } |
| 883 | |
Vasily Khoruzhick | 71c2dd3 | 2013-01-07 20:39:49 +0300 | [diff] [blame] | 884 | WL_EXPORT int |
| 885 | pixman_renderer_init(struct weston_compositor *ec) |
| 886 | { |
Ander Conselvan de Oliveira | 8792ef1 | 2013-01-25 15:13:00 +0200 | [diff] [blame] | 887 | struct pixman_renderer *renderer; |
Vasily Khoruzhick | 71c2dd3 | 2013-01-07 20:39:49 +0300 | [diff] [blame] | 888 | |
Bryce Harrington | de16d89 | 2014-11-20 22:21:57 -0800 | [diff] [blame] | 889 | renderer = zalloc(sizeof *renderer); |
Vasily Khoruzhick | 71c2dd3 | 2013-01-07 20:39:49 +0300 | [diff] [blame] | 890 | if (renderer == NULL) |
| 891 | return -1; |
| 892 | |
Philipp Brüschweiler | f3e39f9 | 2013-03-08 20:35:39 +0100 | [diff] [blame] | 893 | renderer->repaint_debug = 0; |
| 894 | renderer->debug_color = NULL; |
Ander Conselvan de Oliveira | 8792ef1 | 2013-01-25 15:13:00 +0200 | [diff] [blame] | 895 | renderer->base.read_pixels = pixman_renderer_read_pixels; |
| 896 | renderer->base.repaint_output = pixman_renderer_repaint_output; |
| 897 | renderer->base.flush_damage = pixman_renderer_flush_damage; |
| 898 | renderer->base.attach = pixman_renderer_attach; |
Ander Conselvan de Oliveira | 8792ef1 | 2013-01-25 15:13:00 +0200 | [diff] [blame] | 899 | renderer->base.surface_set_color = pixman_renderer_surface_set_color; |
Ander Conselvan de Oliveira | 8792ef1 | 2013-01-25 15:13:00 +0200 | [diff] [blame] | 900 | renderer->base.destroy = pixman_renderer_destroy; |
Pekka Paalanen | 6764bbe | 2015-02-11 12:29:56 +0200 | [diff] [blame] | 901 | renderer->base.surface_get_content_size = |
| 902 | pixman_renderer_surface_get_content_size; |
| 903 | renderer->base.surface_copy_content = |
| 904 | pixman_renderer_surface_copy_content; |
Ander Conselvan de Oliveira | 8792ef1 | 2013-01-25 15:13:00 +0200 | [diff] [blame] | 905 | ec->renderer = &renderer->base; |
Pekka Paalanen | 7bb6510 | 2013-05-22 18:03:04 +0300 | [diff] [blame] | 906 | ec->capabilities |= WESTON_CAP_ROTATION_ANY; |
Pekka Paalanen | 4fc5dd0 | 2013-05-22 18:03:05 +0300 | [diff] [blame] | 907 | ec->capabilities |= WESTON_CAP_CAPTURE_YFLIP; |
Pekka Paalanen | c93782a | 2015-03-06 10:33:50 +0200 | [diff] [blame] | 908 | ec->capabilities |= WESTON_CAP_VIEW_CLIP_MASK; |
Vasily Khoruzhick | 71c2dd3 | 2013-01-07 20:39:49 +0300 | [diff] [blame] | 909 | |
Ander Conselvan de Oliveira | 6b16214 | 2013-10-25 16:26:32 +0300 | [diff] [blame] | 910 | renderer->debug_binding = |
| 911 | weston_compositor_add_debug_binding(ec, KEY_R, |
| 912 | debug_binding, ec); |
Tomeu Vizoso | 1c1fc29 | 2013-08-06 20:05:56 +0200 | [diff] [blame] | 913 | |
| 914 | wl_display_add_shm_format(ec->wl_display, WL_SHM_FORMAT_RGB565); |
| 915 | |
Ander Conselvan de Oliveira | adda00e | 2013-10-25 16:26:34 +0300 | [diff] [blame] | 916 | wl_signal_init(&renderer->destroy_signal); |
| 917 | |
Vasily Khoruzhick | 71c2dd3 | 2013-01-07 20:39:49 +0300 | [diff] [blame] | 918 | return 0; |
| 919 | } |
| 920 | |
| 921 | WL_EXPORT void |
| 922 | pixman_renderer_output_set_buffer(struct weston_output *output, pixman_image_t *buffer) |
| 923 | { |
| 924 | struct pixman_output_state *po = get_output_state(output); |
| 925 | |
| 926 | if (po->hw_buffer) |
| 927 | pixman_image_unref(po->hw_buffer); |
| 928 | po->hw_buffer = buffer; |
| 929 | |
| 930 | if (po->hw_buffer) { |
| 931 | output->compositor->read_format = pixman_image_get_format(po->hw_buffer); |
| 932 | pixman_image_ref(po->hw_buffer); |
| 933 | } |
| 934 | } |
| 935 | |
| 936 | WL_EXPORT int |
| 937 | pixman_renderer_output_create(struct weston_output *output) |
| 938 | { |
Bryce Harrington | de16d89 | 2014-11-20 22:21:57 -0800 | [diff] [blame] | 939 | struct pixman_output_state *po; |
Ander Conselvan de Oliveira | 936effd | 2013-01-25 15:13:01 +0200 | [diff] [blame] | 940 | int w, h; |
Vasily Khoruzhick | 71c2dd3 | 2013-01-07 20:39:49 +0300 | [diff] [blame] | 941 | |
Bryce Harrington | de16d89 | 2014-11-20 22:21:57 -0800 | [diff] [blame] | 942 | po = zalloc(sizeof *po); |
| 943 | if (po == NULL) |
Vasily Khoruzhick | 71c2dd3 | 2013-01-07 20:39:49 +0300 | [diff] [blame] | 944 | return -1; |
Ander Conselvan de Oliveira | 936effd | 2013-01-25 15:13:01 +0200 | [diff] [blame] | 945 | |
| 946 | /* set shadow image transformation */ |
Hardening | ff39efa | 2013-09-18 23:56:35 +0200 | [diff] [blame] | 947 | w = output->current_mode->width; |
| 948 | h = output->current_mode->height; |
Ander Conselvan de Oliveira | 936effd | 2013-01-25 15:13:01 +0200 | [diff] [blame] | 949 | |
Ander Conselvan de Oliveira | 936effd | 2013-01-25 15:13:01 +0200 | [diff] [blame] | 950 | po->shadow_buffer = malloc(w * h * 4); |
| 951 | |
| 952 | if (!po->shadow_buffer) { |
| 953 | free(po); |
| 954 | return -1; |
| 955 | } |
| 956 | |
| 957 | po->shadow_image = |
| 958 | pixman_image_create_bits(PIXMAN_x8r8g8b8, w, h, |
| 959 | po->shadow_buffer, w * 4); |
| 960 | |
| 961 | if (!po->shadow_image) { |
| 962 | free(po->shadow_buffer); |
| 963 | free(po); |
| 964 | return -1; |
| 965 | } |
| 966 | |
Vasily Khoruzhick | 71c2dd3 | 2013-01-07 20:39:49 +0300 | [diff] [blame] | 967 | output->renderer_state = po; |
| 968 | |
| 969 | return 0; |
| 970 | } |
| 971 | |
| 972 | WL_EXPORT void |
| 973 | pixman_renderer_output_destroy(struct weston_output *output) |
| 974 | { |
| 975 | struct pixman_output_state *po = get_output_state(output); |
| 976 | |
Ander Conselvan de Oliveira | 936effd | 2013-01-25 15:13:01 +0200 | [diff] [blame] | 977 | pixman_image_unref(po->shadow_image); |
Ander Conselvan de Oliveira | 23e72b8 | 2013-01-25 15:13:06 +0200 | [diff] [blame] | 978 | |
| 979 | if (po->hw_buffer) |
| 980 | pixman_image_unref(po->hw_buffer); |
Ander Conselvan de Oliveira | 936effd | 2013-01-25 15:13:01 +0200 | [diff] [blame] | 981 | |
Arnaud Vrac | 8e3fe08 | 2014-08-25 20:56:52 +0200 | [diff] [blame] | 982 | free(po->shadow_buffer); |
| 983 | |
| 984 | po->shadow_buffer = NULL; |
Ander Conselvan de Oliveira | 936effd | 2013-01-25 15:13:01 +0200 | [diff] [blame] | 985 | po->shadow_image = NULL; |
Vasily Khoruzhick | 71c2dd3 | 2013-01-07 20:39:49 +0300 | [diff] [blame] | 986 | po->hw_buffer = NULL; |
| 987 | |
| 988 | free(po); |
| 989 | } |