blob: 0d85e07ff878a5435cbe77b39a92b174e2ba7c0e [file] [log] [blame]
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +03001/*
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
Daniel Stonec228e232013-05-22 18:03:19 +030024#include "config.h"
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +030025
26#include <errno.h>
27#include <stdlib.h>
28
29#include "pixman-renderer.h"
30
Ander Conselvan de Oliveira8792ef12013-01-25 15:13:00 +020031#include <linux/input.h>
32
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +030033struct pixman_output_state {
Ander Conselvan de Oliveira936effd2013-01-25 15:13:01 +020034 void *shadow_buffer;
35 pixman_image_t *shadow_image;
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +030036 pixman_image_t *hw_buffer;
37};
38
39struct pixman_surface_state {
40 pixman_image_t *image;
41 struct weston_buffer_reference buffer_ref;
42};
43
44struct pixman_renderer {
45 struct weston_renderer base;
Ander Conselvan de Oliveira6b162142013-10-25 16:26:32 +030046
Ander Conselvan de Oliveira8792ef12013-01-25 15:13:00 +020047 int repaint_debug;
48 pixman_image_t *debug_color;
Ander Conselvan de Oliveira6b162142013-10-25 16:26:32 +030049 struct weston_binding *debug_binding;
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +030050};
51
52static inline struct pixman_output_state *
53get_output_state(struct weston_output *output)
54{
55 return (struct pixman_output_state *)output->renderer_state;
56}
57
58static inline struct pixman_surface_state *
59get_surface_state(struct weston_surface *surface)
60{
61 return (struct pixman_surface_state *)surface->renderer_state;
62}
63
64static inline struct pixman_renderer *
65get_renderer(struct weston_compositor *ec)
66{
67 return (struct pixman_renderer *)ec->renderer;
68}
69
70static int
71pixman_renderer_read_pixels(struct weston_output *output,
72 pixman_format_code_t format, void *pixels,
73 uint32_t x, uint32_t y,
74 uint32_t width, uint32_t height)
75{
76 struct pixman_output_state *po = get_output_state(output);
Alexander Larsson97af7922013-05-29 12:01:33 +020077 pixman_transform_t transform;
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +030078 pixman_image_t *out_buf;
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +030079
80 if (!po->hw_buffer) {
81 errno = ENODEV;
82 return -1;
83 }
84
85 out_buf = pixman_image_create_bits(format,
86 width,
87 height,
88 pixels,
89 (PIXMAN_FORMAT_BPP(format) / 8) * width);
90
Alexander Larsson97af7922013-05-29 12:01:33 +020091 /* Caller expects vflipped source image */
92 pixman_transform_init_translate(&transform,
93 pixman_int_to_fixed (x),
94 pixman_int_to_fixed (y - pixman_image_get_height (po->hw_buffer)));
95 pixman_transform_scale(&transform, NULL,
96 pixman_fixed_1,
97 pixman_fixed_minus_1);
98 pixman_image_set_transform(po->hw_buffer, &transform);
99
100 pixman_image_composite32(PIXMAN_OP_SRC,
101 po->hw_buffer, /* src */
102 NULL /* mask */,
103 out_buf, /* dest */
104 0, 0, /* src_x, src_y */
105 0, 0, /* mask_x, mask_y */
106 0, 0, /* dest_x, dest_y */
107 pixman_image_get_width (po->hw_buffer), /* width */
108 pixman_image_get_height (po->hw_buffer) /* height */);
109 pixman_image_set_transform(po->hw_buffer, NULL);
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +0300110
111 pixman_image_unref(out_buf);
112
113 return 0;
114}
115
Vasily Khoruzhick031fc872013-01-29 14:58:14 +0300116static void
Alexander Larsson4ea95522013-05-22 14:41:37 +0200117box_scale(pixman_box32_t *dst, int scale)
118{
119 dst->x1 *= scale;
120 dst->x2 *= scale;
121 dst->y1 *= scale;
122 dst->y2 *= scale;
123}
124
125static void
126scale_region (pixman_region32_t *region, int scale)
127{
128 pixman_box32_t *rects, *scaled_rects;
129 int nrects, i;
130
131 if (scale != 1) {
132 rects = pixman_region32_rectangles(region, &nrects);
133 scaled_rects = calloc(nrects, sizeof(pixman_box32_t));
134
135 for (i = 0; i < nrects; i++) {
136 scaled_rects[i] = rects[i];
137 box_scale(&scaled_rects[i], scale);
138 }
139 pixman_region32_clear(region);
140
141 pixman_region32_init_rects (region, scaled_rects, nrects);
142 free (scaled_rects);
143 }
144}
145
146static void
Alexander Larsson1f206b42013-05-22 14:41:36 +0200147transform_region (pixman_region32_t *region, int width, int height, enum wl_output_transform transform)
Vasily Khoruzhick031fc872013-01-29 14:58:14 +0300148{
Alexander Larsson1f206b42013-05-22 14:41:36 +0200149 pixman_box32_t *rects, *transformed_rects;
150 int nrects, i;
151
152 if (transform == WL_OUTPUT_TRANSFORM_NORMAL)
153 return;
154
155 rects = pixman_region32_rectangles(region, &nrects);
156 transformed_rects = calloc(nrects, sizeof(pixman_box32_t));
157
158 for (i = 0; i < nrects; i++) {
159 switch (transform) {
160 default:
161 case WL_OUTPUT_TRANSFORM_NORMAL:
162 transformed_rects[i].x1 = rects[i].x1;
163 transformed_rects[i].y1 = rects[i].y1;
164 transformed_rects[i].x2 = rects[i].x2;
165 transformed_rects[i].y2 = rects[i].y2;
166 break;
167 case WL_OUTPUT_TRANSFORM_90:
168 transformed_rects[i].x1 = height - rects[i].y2;
169 transformed_rects[i].y1 = rects[i].x1;
170 transformed_rects[i].x2 = height - rects[i].y1;
171 transformed_rects[i].y2 = rects[i].x2;
172 break;
173 case WL_OUTPUT_TRANSFORM_180:
174 transformed_rects[i].x1 = width - rects[i].x2;
175 transformed_rects[i].y1 = height - rects[i].y2;
176 transformed_rects[i].x2 = width - rects[i].x1;
177 transformed_rects[i].y2 = height - rects[i].y1;
178 break;
179 case WL_OUTPUT_TRANSFORM_270:
180 transformed_rects[i].x1 = rects[i].y1;
181 transformed_rects[i].y1 = width - rects[i].x2;
182 transformed_rects[i].x2 = rects[i].y2;
183 transformed_rects[i].y2 = width - rects[i].x1;
184 break;
185 case WL_OUTPUT_TRANSFORM_FLIPPED:
186 transformed_rects[i].x1 = width - rects[i].x2;
187 transformed_rects[i].y1 = rects[i].y1;
188 transformed_rects[i].x2 = width - rects[i].x1;
189 transformed_rects[i].y2 = rects[i].y2;
190 break;
191 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
192 transformed_rects[i].x1 = height - rects[i].y2;
193 transformed_rects[i].y1 = width - rects[i].x2;
194 transformed_rects[i].x2 = height - rects[i].y1;
195 transformed_rects[i].y2 = width - rects[i].x1;
196 break;
197 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
198 transformed_rects[i].x1 = rects[i].x1;
199 transformed_rects[i].y1 = height - rects[i].y2;
200 transformed_rects[i].x2 = rects[i].x2;
201 transformed_rects[i].y2 = height - rects[i].y1;
202 break;
203 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
204 transformed_rects[i].x1 = rects[i].y1;
205 transformed_rects[i].y1 = rects[i].x1;
206 transformed_rects[i].x2 = rects[i].y2;
207 transformed_rects[i].y2 = rects[i].x2;
208 break;
209 }
210 }
211 pixman_region32_clear(region);
212
213 pixman_region32_init_rects (region, transformed_rects, nrects);
214 free (transformed_rects);
215}
216
217static void
218region_global_to_output(struct weston_output *output, pixman_region32_t *region)
219{
220 pixman_region32_translate(region, -output->x, -output->y);
221 transform_region (region, output->width, output->height, output->transform);
Hardeningff39efa2013-09-18 23:56:35 +0200222 scale_region (region, output->current_scale);
Vasily Khoruzhick031fc872013-01-29 14:58:14 +0300223}
224
Vasily Khoruzhickf4a457f2013-01-28 22:40:29 +0300225#define D2F(v) pixman_double_to_fixed((double)v)
226
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +0300227static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500228repaint_region(struct weston_view *ev, struct weston_output *output,
Alexander Larsson1f206b42013-05-22 14:41:36 +0200229 pixman_region32_t *region, pixman_region32_t *surf_region,
230 pixman_op_t pixman_op)
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +0300231{
Ander Conselvan de Oliveira8792ef12013-01-25 15:13:00 +0200232 struct pixman_renderer *pr =
233 (struct pixman_renderer *) output->compositor->renderer;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500234 struct pixman_surface_state *ps = get_surface_state(ev->surface);
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +0300235 struct pixman_output_state *po = get_output_state(output);
236 pixman_region32_t final_region;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500237 float view_x, view_y;
Alexander Larsson1f206b42013-05-22 14:41:36 +0200238 pixman_transform_t transform;
239 pixman_fixed_t fw, fh;
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +0300240
241 /* The final region to be painted is the intersection of
242 * 'region' and 'surf_region'. However, 'region' is in the global
243 * coordinates, and 'surf_region' is in the surface-local
244 * coordinates
245 */
246 pixman_region32_init(&final_region);
Alexander Larsson1f206b42013-05-22 14:41:36 +0200247 if (surf_region) {
248 pixman_region32_copy(&final_region, surf_region);
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +0300249
Alexander Larsson1f206b42013-05-22 14:41:36 +0200250 /* Convert from surface to global coordinates */
Jason Ekstranda7af7042013-10-12 22:38:11 -0500251 if (!ev->transform.enabled) {
252 pixman_region32_translate(&final_region, ev->geometry.x, ev->geometry.y);
Alexander Larsson1f206b42013-05-22 14:41:36 +0200253 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500254 weston_view_to_global_float(ev, 0, 0, &view_x, &view_y);
255 pixman_region32_translate(&final_region, (int)view_x, (int)view_y);
Alexander Larsson1f206b42013-05-22 14:41:36 +0200256 }
Vasily Khoruzhickf4a457f2013-01-28 22:40:29 +0300257
Alexander Larsson1f206b42013-05-22 14:41:36 +0200258 /* We need to paint the intersection */
259 pixman_region32_intersect(&final_region, &final_region, region);
Vasily Khoruzhickf4a457f2013-01-28 22:40:29 +0300260 } else {
Alexander Larsson1f206b42013-05-22 14:41:36 +0200261 /* If there is no surface region, just use the global region */
262 pixman_region32_copy(&final_region, region);
Vasily Khoruzhickf4a457f2013-01-28 22:40:29 +0300263 }
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +0300264
Alexander Larsson1f206b42013-05-22 14:41:36 +0200265 /* Convert from global to output coord */
266 region_global_to_output(output, &final_region);
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +0300267
Alexander Larsson1f206b42013-05-22 14:41:36 +0200268 /* And clip to it */
269 pixman_image_set_clip_region32 (po->shadow_image, &final_region);
Ander Conselvan de Oliveira8792ef12013-01-25 15:13:00 +0200270
Alexander Larsson1f206b42013-05-22 14:41:36 +0200271 /* Set up the source transformation based on the surface
Alexander Larsson4ea95522013-05-22 14:41:37 +0200272 position, the output position/transform/scale and the client
273 specified buffer transform/scale */
Alexander Larsson1f206b42013-05-22 14:41:36 +0200274 pixman_transform_init_identity(&transform);
Alexander Larsson4ea95522013-05-22 14:41:37 +0200275 pixman_transform_scale(&transform, NULL,
Hardeningff39efa2013-09-18 23:56:35 +0200276 pixman_double_to_fixed ((double)1.0/output->current_scale),
277 pixman_double_to_fixed ((double)1.0/output->current_scale));
Ander Conselvan de Oliveira8792ef12013-01-25 15:13:00 +0200278
Alexander Larsson1f206b42013-05-22 14:41:36 +0200279 fw = pixman_int_to_fixed(output->width);
280 fh = pixman_int_to_fixed(output->height);
281 switch (output->transform) {
282 default:
283 case WL_OUTPUT_TRANSFORM_NORMAL:
284 case WL_OUTPUT_TRANSFORM_FLIPPED:
285 break;
286 case WL_OUTPUT_TRANSFORM_90:
287 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
288 pixman_transform_rotate(&transform, NULL, 0, -pixman_fixed_1);
289 pixman_transform_translate(&transform, NULL, 0, fh);
290 break;
291 case WL_OUTPUT_TRANSFORM_180:
292 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
293 pixman_transform_rotate(&transform, NULL, -pixman_fixed_1, 0);
294 pixman_transform_translate(&transform, NULL, fw, fh);
295 break;
296 case WL_OUTPUT_TRANSFORM_270:
297 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
298 pixman_transform_rotate(&transform, NULL, 0, pixman_fixed_1);
299 pixman_transform_translate(&transform, NULL, fw, 0);
300 break;
301 }
302
303 switch (output->transform) {
304 case WL_OUTPUT_TRANSFORM_FLIPPED:
305 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
306 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
307 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
308 pixman_transform_scale(&transform, NULL,
309 pixman_int_to_fixed (-1),
310 pixman_int_to_fixed (1));
311 pixman_transform_translate(&transform, NULL, fw, 0);
312 break;
313 }
314
315 pixman_transform_translate(&transform, NULL,
316 pixman_double_to_fixed (output->x),
317 pixman_double_to_fixed (output->y));
318
Jason Ekstranda7af7042013-10-12 22:38:11 -0500319 if (ev->transform.enabled) {
Alexander Larsson1f206b42013-05-22 14:41:36 +0200320 /* Pixman supports only 2D transform matrix, but Weston uses 3D,
321 * so we're omitting Z coordinate here
322 */
323 pixman_transform_t surface_transform = {{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500324 { D2F(ev->transform.matrix.d[0]),
325 D2F(ev->transform.matrix.d[4]),
326 D2F(ev->transform.matrix.d[12]),
Alexander Larsson1f206b42013-05-22 14:41:36 +0200327 },
Jason Ekstranda7af7042013-10-12 22:38:11 -0500328 { D2F(ev->transform.matrix.d[1]),
329 D2F(ev->transform.matrix.d[5]),
330 D2F(ev->transform.matrix.d[13]),
Alexander Larsson1f206b42013-05-22 14:41:36 +0200331 },
Jason Ekstranda7af7042013-10-12 22:38:11 -0500332 { D2F(ev->transform.matrix.d[3]),
333 D2F(ev->transform.matrix.d[7]),
334 D2F(ev->transform.matrix.d[15]),
Alexander Larsson1f206b42013-05-22 14:41:36 +0200335 }
336 }};
337
338 pixman_transform_invert(&surface_transform, &surface_transform);
339 pixman_transform_multiply (&transform, &surface_transform, &transform);
340 } else {
341 pixman_transform_translate(&transform, NULL,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500342 pixman_double_to_fixed ((double)-ev->geometry.x),
343 pixman_double_to_fixed ((double)-ev->geometry.y));
Alexander Larsson1f206b42013-05-22 14:41:36 +0200344 }
345
346
Jason Ekstranda7af7042013-10-12 22:38:11 -0500347 fw = pixman_int_to_fixed(ev->geometry.width);
348 fh = pixman_int_to_fixed(ev->geometry.height);
Alexander Larsson1f206b42013-05-22 14:41:36 +0200349
Jason Ekstranda7af7042013-10-12 22:38:11 -0500350 switch (ev->surface->buffer_transform) {
Alexander Larsson1f206b42013-05-22 14:41:36 +0200351 case WL_OUTPUT_TRANSFORM_FLIPPED:
352 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
353 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
354 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
355 pixman_transform_scale(&transform, NULL,
356 pixman_int_to_fixed (-1),
357 pixman_int_to_fixed (1));
358 pixman_transform_translate(&transform, NULL, fw, 0);
359 break;
360 }
361
Jason Ekstranda7af7042013-10-12 22:38:11 -0500362 switch (ev->surface->buffer_transform) {
Alexander Larsson1f206b42013-05-22 14:41:36 +0200363 default:
364 case WL_OUTPUT_TRANSFORM_NORMAL:
365 case WL_OUTPUT_TRANSFORM_FLIPPED:
366 break;
367 case WL_OUTPUT_TRANSFORM_90:
368 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
369 pixman_transform_rotate(&transform, NULL, 0, pixman_fixed_1);
370 pixman_transform_translate(&transform, NULL, fh, 0);
371 break;
372 case WL_OUTPUT_TRANSFORM_180:
373 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
374 pixman_transform_rotate(&transform, NULL, -pixman_fixed_1, 0);
375 pixman_transform_translate(&transform, NULL, fw, fh);
376 break;
377 case WL_OUTPUT_TRANSFORM_270:
378 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
379 pixman_transform_rotate(&transform, NULL, 0, -pixman_fixed_1);
380 pixman_transform_translate(&transform, NULL, 0, fw);
381 break;
382 }
383
Alexander Larsson4ea95522013-05-22 14:41:37 +0200384 pixman_transform_scale(&transform, NULL,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500385 pixman_double_to_fixed ((double)ev->surface->buffer_scale),
386 pixman_double_to_fixed ((double)ev->surface->buffer_scale));
Alexander Larsson4ea95522013-05-22 14:41:37 +0200387
Alexander Larsson1f206b42013-05-22 14:41:36 +0200388 pixman_image_set_transform(ps->image, &transform);
389
Jason Ekstranda7af7042013-10-12 22:38:11 -0500390 if (ev->transform.enabled || output->current_scale != ev->surface->buffer_scale)
Alexander Larsson1f206b42013-05-22 14:41:36 +0200391 pixman_image_set_filter(ps->image, PIXMAN_FILTER_BILINEAR, NULL, 0);
392 else
393 pixman_image_set_filter(ps->image, PIXMAN_FILTER_NEAREST, NULL, 0);
394
395 pixman_image_composite32(pixman_op,
396 ps->image, /* src */
397 NULL /* mask */,
398 po->shadow_image, /* dest */
399 0, 0, /* src_x, src_y */
400 0, 0, /* mask_x, mask_y */
401 0, 0, /* dest_x, dest_y */
402 pixman_image_get_width (po->shadow_image), /* width */
403 pixman_image_get_height (po->shadow_image) /* height */);
404
405 if (pr->repaint_debug)
Ander Conselvan de Oliveira8792ef12013-01-25 15:13:00 +0200406 pixman_image_composite32(PIXMAN_OP_OVER,
Alexander Larsson1f206b42013-05-22 14:41:36 +0200407 pr->debug_color, /* src */
408 NULL /* mask */,
409 po->shadow_image, /* dest */
410 0, 0, /* src_x, src_y */
411 0, 0, /* mask_x, mask_y */
412 0, 0, /* dest_x, dest_y */
413 pixman_image_get_width (po->shadow_image), /* width */
414 pixman_image_get_height (po->shadow_image) /* height */);
415
416 pixman_image_set_clip_region32 (po->shadow_image, NULL);
417
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +0300418 pixman_region32_fini(&final_region);
419}
420
421static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500422draw_view(struct weston_view *ev, struct weston_output *output,
423 pixman_region32_t *damage) /* in global coordinates */
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +0300424{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500425 struct pixman_surface_state *ps = get_surface_state(ev->surface);
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +0300426 /* repaint bounding region in global coordinates: */
427 pixman_region32_t repaint;
428 /* non-opaque region in surface coordinates: */
429 pixman_region32_t surface_blend;
430
431 /* No buffer attached */
432 if (!ps->image)
433 return;
434
435 pixman_region32_init(&repaint);
436 pixman_region32_intersect(&repaint,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500437 &ev->transform.boundingbox, damage);
438 pixman_region32_subtract(&repaint, &repaint, &ev->clip);
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +0300439
440 if (!pixman_region32_not_empty(&repaint))
441 goto out;
442
443 if (output->zoom.active) {
444 weston_log("pixman renderer does not support zoom\n");
445 goto out;
446 }
447
Vasily Khoruzhickf4a457f2013-01-28 22:40:29 +0300448 /* TODO: Implement repaint_region_complex() using pixman_composite_trapezoids() */
Jason Ekstranda7af7042013-10-12 22:38:11 -0500449 if (ev->transform.enabled &&
450 ev->transform.matrix.type != WESTON_MATRIX_TRANSFORM_TRANSLATE) {
451 repaint_region(ev, output, &repaint, NULL, PIXMAN_OP_OVER);
Vasily Khoruzhickf4a457f2013-01-28 22:40:29 +0300452 } else {
453 /* blended region is whole surface minus opaque region: */
454 pixman_region32_init_rect(&surface_blend, 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500455 ev->geometry.width, ev->geometry.height);
456 pixman_region32_subtract(&surface_blend, &surface_blend, &ev->surface->opaque);
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +0300457
Jason Ekstranda7af7042013-10-12 22:38:11 -0500458 if (pixman_region32_not_empty(&ev->surface->opaque)) {
459 repaint_region(ev, output, &repaint, &ev->surface->opaque, PIXMAN_OP_SRC);
Vasily Khoruzhickf4a457f2013-01-28 22:40:29 +0300460 }
461
462 if (pixman_region32_not_empty(&surface_blend)) {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500463 repaint_region(ev, output, &repaint, &surface_blend, PIXMAN_OP_OVER);
Vasily Khoruzhickf4a457f2013-01-28 22:40:29 +0300464 }
465 pixman_region32_fini(&surface_blend);
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +0300466 }
467
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +0300468
469out:
470 pixman_region32_fini(&repaint);
471}
472static void
473repaint_surfaces(struct weston_output *output, pixman_region32_t *damage)
474{
475 struct weston_compositor *compositor = output->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500476 struct weston_view *view;
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +0300477
Jason Ekstranda7af7042013-10-12 22:38:11 -0500478 wl_list_for_each_reverse(view, &compositor->view_list, link)
479 if (view->plane == &compositor->primary_plane)
480 draw_view(view, output, damage);
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +0300481}
482
483static void
Ander Conselvan de Oliveira936effd2013-01-25 15:13:01 +0200484copy_to_hw_buffer(struct weston_output *output, pixman_region32_t *region)
485{
486 struct pixman_output_state *po = get_output_state(output);
Alexander Larsson1f206b42013-05-22 14:41:36 +0200487 pixman_region32_t output_region;
Ander Conselvan de Oliveira936effd2013-01-25 15:13:01 +0200488
Alexander Larsson1f206b42013-05-22 14:41:36 +0200489 pixman_region32_init(&output_region);
490 pixman_region32_copy(&output_region, region);
Ander Conselvan de Oliveira936effd2013-01-25 15:13:01 +0200491
Alexander Larsson1f206b42013-05-22 14:41:36 +0200492 region_global_to_output(output, &output_region);
Ander Conselvan de Oliveira936effd2013-01-25 15:13:01 +0200493
Alexander Larsson1f206b42013-05-22 14:41:36 +0200494 pixman_image_set_clip_region32 (po->hw_buffer, &output_region);
Ander Conselvan de Oliveira936effd2013-01-25 15:13:01 +0200495
Alexander Larsson1f206b42013-05-22 14:41:36 +0200496 pixman_image_composite32(PIXMAN_OP_SRC,
497 po->shadow_image, /* src */
498 NULL /* mask */,
499 po->hw_buffer, /* dest */
500 0, 0, /* src_x, src_y */
501 0, 0, /* mask_x, mask_y */
502 0, 0, /* dest_x, dest_y */
503 pixman_image_get_width (po->hw_buffer), /* width */
504 pixman_image_get_height (po->hw_buffer) /* height */);
505
506 pixman_image_set_clip_region32 (po->hw_buffer, NULL);
Ander Conselvan de Oliveira936effd2013-01-25 15:13:01 +0200507}
508
509static void
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +0300510pixman_renderer_repaint_output(struct weston_output *output,
511 pixman_region32_t *output_damage)
512{
513 struct pixman_output_state *po = get_output_state(output);
514
515 if (!po->hw_buffer)
516 return;
517
518 repaint_surfaces(output, output_damage);
Ander Conselvan de Oliveira936effd2013-01-25 15:13:01 +0200519 copy_to_hw_buffer(output, output_damage);
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +0300520
521 pixman_region32_copy(&output->previous_damage, output_damage);
522 wl_signal_emit(&output->frame_signal, output);
523
524 /* Actual flip should be done by caller */
525}
526
527static void
528pixman_renderer_flush_damage(struct weston_surface *surface)
529{
530 /* No-op for pixman renderer */
531}
532
533static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -0500534pixman_renderer_attach(struct weston_surface *es, struct weston_buffer *buffer)
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +0300535{
536 struct pixman_surface_state *ps = get_surface_state(es);
Jason Ekstrand6bd62942013-06-20 20:38:23 -0500537 struct wl_shm_buffer *shm_buffer;
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +0300538 pixman_format_code_t pixman_format;
539
540 weston_buffer_reference(&ps->buffer_ref, buffer);
541
542 if (ps->image) {
543 pixman_image_unref(ps->image);
544 ps->image = NULL;
545 }
546
547 if (!buffer)
548 return;
Jason Ekstrand6bd62942013-06-20 20:38:23 -0500549
550 shm_buffer = wl_shm_buffer_get(buffer->resource);
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +0300551
Jason Ekstrand6bd62942013-06-20 20:38:23 -0500552 if (! shm_buffer) {
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +0300553 weston_log("Pixman renderer supports only SHM buffers\n");
554 weston_buffer_reference(&ps->buffer_ref, NULL);
555 return;
556 }
557
Jason Ekstrand6bd62942013-06-20 20:38:23 -0500558 switch (wl_shm_buffer_get_format(shm_buffer)) {
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +0300559 case WL_SHM_FORMAT_XRGB8888:
560 pixman_format = PIXMAN_x8r8g8b8;
561 break;
562 case WL_SHM_FORMAT_ARGB8888:
563 pixman_format = PIXMAN_a8r8g8b8;
564 break;
Tomeu Vizoso1c1fc292013-08-06 20:05:56 +0200565 case WL_SHM_FORMAT_RGB565:
566 pixman_format = PIXMAN_r5g6b5;
567 break;
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +0300568 default:
569 weston_log("Unsupported SHM buffer format\n");
570 weston_buffer_reference(&ps->buffer_ref, NULL);
571 return;
572 break;
573 }
Jason Ekstrand6bd62942013-06-20 20:38:23 -0500574
575 buffer->shm_buffer = shm_buffer;
576 buffer->width = wl_shm_buffer_get_width(shm_buffer);
577 buffer->height = wl_shm_buffer_get_height(shm_buffer);
578
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +0300579 ps->image = pixman_image_create_bits(pixman_format,
Jason Ekstrand6bd62942013-06-20 20:38:23 -0500580 buffer->width, buffer->height,
581 wl_shm_buffer_get_data(shm_buffer),
582 wl_shm_buffer_get_stride(shm_buffer));
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +0300583}
584
585static int
586pixman_renderer_create_surface(struct weston_surface *surface)
587{
588 struct pixman_surface_state *ps;
589
590 ps = calloc(1, sizeof *ps);
591 if (!ps)
592 return -1;
593
594 surface->renderer_state = ps;
595
596 return 0;
597}
598
599static void
600pixman_renderer_surface_set_color(struct weston_surface *es,
601 float red, float green, float blue, float alpha)
602{
603 struct pixman_surface_state *ps = get_surface_state(es);
604 pixman_color_t color;
605
606 color.red = red * 0xffff;
607 color.green = green * 0xffff;
608 color.blue = blue * 0xffff;
609 color.alpha = alpha * 0xffff;
610
611 if (ps->image) {
612 pixman_image_unref(ps->image);
613 ps->image = NULL;
614 }
615
616 ps->image = pixman_image_create_solid_fill(&color);
617}
618
619static void
620pixman_renderer_destroy_surface(struct weston_surface *surface)
621{
622 struct pixman_surface_state *ps = get_surface_state(surface);
623
624 if (ps->image) {
625 pixman_image_unref(ps->image);
626 ps->image = NULL;
627 }
628 weston_buffer_reference(&ps->buffer_ref, NULL);
629 free(ps);
630}
631
Vasily Khoruzhick52cfd612013-01-08 19:09:01 +0300632static void
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +0300633pixman_renderer_destroy(struct weston_compositor *ec)
634{
Ander Conselvan de Oliveira6b162142013-10-25 16:26:32 +0300635 struct pixman_renderer *pr = get_renderer(ec);
636
637 weston_binding_destroy(pr->debug_binding);
638 free(pr);
639
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +0300640 ec->renderer = NULL;
641}
642
Ander Conselvan de Oliveira8792ef12013-01-25 15:13:00 +0200643static void
Kristian Høgsberge3148752013-05-06 23:19:49 -0400644debug_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Ander Conselvan de Oliveira8792ef12013-01-25 15:13:00 +0200645 void *data)
646{
647 struct weston_compositor *ec = data;
648 struct pixman_renderer *pr = (struct pixman_renderer *) ec->renderer;
649
650 pr->repaint_debug ^= 1;
651
652 if (pr->repaint_debug) {
653 pixman_color_t red = {
654 0x3fff, 0x0000, 0x0000, 0x3fff
655 };
656
657 pr->debug_color = pixman_image_create_solid_fill(&red);
658 } else {
659 pixman_image_unref(pr->debug_color);
660 weston_compositor_damage_all(ec);
661 }
662}
663
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +0300664WL_EXPORT int
665pixman_renderer_init(struct weston_compositor *ec)
666{
Ander Conselvan de Oliveira8792ef12013-01-25 15:13:00 +0200667 struct pixman_renderer *renderer;
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +0300668
Ander Conselvan de Oliveirac68b1082013-10-25 16:26:31 +0300669 renderer = calloc(1, sizeof *renderer);
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +0300670 if (renderer == NULL)
671 return -1;
672
Philipp Brüschweilerf3e39f92013-03-08 20:35:39 +0100673 renderer->repaint_debug = 0;
674 renderer->debug_color = NULL;
Ander Conselvan de Oliveira8792ef12013-01-25 15:13:00 +0200675 renderer->base.read_pixels = pixman_renderer_read_pixels;
676 renderer->base.repaint_output = pixman_renderer_repaint_output;
677 renderer->base.flush_damage = pixman_renderer_flush_damage;
678 renderer->base.attach = pixman_renderer_attach;
679 renderer->base.create_surface = pixman_renderer_create_surface;
680 renderer->base.surface_set_color = pixman_renderer_surface_set_color;
681 renderer->base.destroy_surface = pixman_renderer_destroy_surface;
682 renderer->base.destroy = pixman_renderer_destroy;
683 ec->renderer = &renderer->base;
Pekka Paalanen7bb65102013-05-22 18:03:04 +0300684 ec->capabilities |= WESTON_CAP_ROTATION_ANY;
Pekka Paalanen4fc5dd02013-05-22 18:03:05 +0300685 ec->capabilities |= WESTON_CAP_CAPTURE_YFLIP;
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +0300686
Ander Conselvan de Oliveira6b162142013-10-25 16:26:32 +0300687 renderer->debug_binding =
688 weston_compositor_add_debug_binding(ec, KEY_R,
689 debug_binding, ec);
Tomeu Vizoso1c1fc292013-08-06 20:05:56 +0200690
691 wl_display_add_shm_format(ec->wl_display, WL_SHM_FORMAT_RGB565);
692
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +0300693 return 0;
694}
695
696WL_EXPORT void
697pixman_renderer_output_set_buffer(struct weston_output *output, pixman_image_t *buffer)
698{
699 struct pixman_output_state *po = get_output_state(output);
700
701 if (po->hw_buffer)
702 pixman_image_unref(po->hw_buffer);
703 po->hw_buffer = buffer;
704
705 if (po->hw_buffer) {
706 output->compositor->read_format = pixman_image_get_format(po->hw_buffer);
707 pixman_image_ref(po->hw_buffer);
708 }
709}
710
711WL_EXPORT int
712pixman_renderer_output_create(struct weston_output *output)
713{
714 struct pixman_output_state *po = calloc(1, sizeof *po);
Ander Conselvan de Oliveira936effd2013-01-25 15:13:01 +0200715 int w, h;
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +0300716
717 if (!po)
718 return -1;
Ander Conselvan de Oliveira936effd2013-01-25 15:13:01 +0200719
720 /* set shadow image transformation */
Hardeningff39efa2013-09-18 23:56:35 +0200721 w = output->current_mode->width;
722 h = output->current_mode->height;
Ander Conselvan de Oliveira936effd2013-01-25 15:13:01 +0200723
Ander Conselvan de Oliveira936effd2013-01-25 15:13:01 +0200724 po->shadow_buffer = malloc(w * h * 4);
725
726 if (!po->shadow_buffer) {
727 free(po);
728 return -1;
729 }
730
731 po->shadow_image =
732 pixman_image_create_bits(PIXMAN_x8r8g8b8, w, h,
733 po->shadow_buffer, w * 4);
734
735 if (!po->shadow_image) {
736 free(po->shadow_buffer);
737 free(po);
738 return -1;
739 }
740
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +0300741 output->renderer_state = po;
742
743 return 0;
744}
745
746WL_EXPORT void
747pixman_renderer_output_destroy(struct weston_output *output)
748{
749 struct pixman_output_state *po = get_output_state(output);
750
Ander Conselvan de Oliveira936effd2013-01-25 15:13:01 +0200751 pixman_image_unref(po->shadow_image);
Ander Conselvan de Oliveira23e72b82013-01-25 15:13:06 +0200752
753 if (po->hw_buffer)
754 pixman_image_unref(po->hw_buffer);
Ander Conselvan de Oliveira936effd2013-01-25 15:13:01 +0200755
756 po->shadow_image = NULL;
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +0300757 po->hw_buffer = NULL;
758
759 free(po);
760}