blob: 908824c43e8a28cfd0da34596a62cdecce3cf9b5 [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
24#define _GNU_SOURCE
25
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 Oliveira8792ef12013-01-25 15:13:00 +020046 int repaint_debug;
47 pixman_image_t *debug_color;
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +030048};
49
50static inline struct pixman_output_state *
51get_output_state(struct weston_output *output)
52{
53 return (struct pixman_output_state *)output->renderer_state;
54}
55
56static inline struct pixman_surface_state *
57get_surface_state(struct weston_surface *surface)
58{
59 return (struct pixman_surface_state *)surface->renderer_state;
60}
61
62static inline struct pixman_renderer *
63get_renderer(struct weston_compositor *ec)
64{
65 return (struct pixman_renderer *)ec->renderer;
66}
67
68static int
69pixman_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 Khoruzhickf4a457f2013-01-28 22:40:29 +0300107#define D2F(v) pixman_double_to_fixed((double)v)
108
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +0300109static void
Vasily Khoruzhickf4a457f2013-01-28 22:40:29 +0300110repaint_region_complex(struct weston_surface *es, struct weston_output *output,
111 pixman_region32_t *region)
112{
113 struct pixman_renderer *pr =
114 (struct pixman_renderer *) output->compositor->renderer;
115 struct pixman_surface_state *ps = get_surface_state(es);
116 struct pixman_output_state *po = get_output_state(output);
117 int nrects, i;
118 pixman_box32_t *rects;
119
120 /* Pixman supports only 2D transform matrix, but Weston uses 3D,
121 * so we're omitting Z coordinate here
122 */
123 pixman_transform_t transform = {{
124 { D2F(es->transform.matrix.d[0]),
125 D2F(es->transform.matrix.d[4]),
126 D2F(es->transform.matrix.d[12]),
127 },
128 { D2F(es->transform.matrix.d[1]),
129 D2F(es->transform.matrix.d[5]),
130 D2F(es->transform.matrix.d[13]),
131 },
132 { D2F(es->transform.matrix.d[3]),
133 D2F(es->transform.matrix.d[7]),
134 D2F(es->transform.matrix.d[15]),
135 }
136 }};
137
138 pixman_transform_invert(&transform, &transform);
139
140 pixman_image_set_transform(ps->image, &transform);
141 pixman_image_set_filter(ps->image, PIXMAN_FILTER_BILINEAR, NULL, 0);
142
143 rects = pixman_region32_rectangles(region, &nrects);
144 for (i = 0; i < nrects; i++) {
145 pixman_image_composite32(PIXMAN_OP_OVER,
146 ps->image, /* src */
147 NULL /* mask */,
148 po->shadow_image, /* dest */
149 rects[i].x1, rects[i].y1, /* src_x, src_y */
150 0, 0, /* mask_x, mask_y */
151 rects[i].x1, rects[i].y1, /* dst_x, dst_y */
152 rects[i].x2 - rects[i].x1, /* width */
153 rects[i].y2 - rects[i].y1 /* height */
154 );
155
156 if (!pr->repaint_debug)
157 continue;
158
159 pixman_image_composite32(PIXMAN_OP_OVER,
160 pr->debug_color, /* src */
161 NULL /* mask */,
162 po->shadow_image, /* dest */
163 0, 0, /* src_x, src_y */
164 0, 0, /* mask_x, mask_y */
165 rects[i].x1, rects[i].y1, /* dest_x, dest_y */
166 rects[i].x2 - rects[i].x1, /* width */
167 rects[i].y2 - rects[i].y1 /* height */);
168 }
169}
170
171static void
172repaint_region_simple(struct weston_surface *es, struct weston_output *output,
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +0300173 pixman_region32_t *region, pixman_region32_t *surf_region,
174 pixman_op_t pixman_op)
175{
Ander Conselvan de Oliveira8792ef12013-01-25 15:13:00 +0200176 struct pixman_renderer *pr =
177 (struct pixman_renderer *) output->compositor->renderer;
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +0300178 struct pixman_surface_state *ps = get_surface_state(es);
179 struct pixman_output_state *po = get_output_state(output);
180 pixman_region32_t final_region;
181 pixman_box32_t *rects;
182 int nrects, i, src_x, src_y;
183 float surface_x, surface_y;
184
185 /* The final region to be painted is the intersection of
186 * 'region' and 'surf_region'. However, 'region' is in the global
187 * coordinates, and 'surf_region' is in the surface-local
188 * coordinates
189 */
190 pixman_region32_init(&final_region);
191 pixman_region32_copy(&final_region, surf_region);
192
Vasily Khoruzhickf4a457f2013-01-28 22:40:29 +0300193 pixman_image_set_filter(ps->image, PIXMAN_FILTER_NEAREST, NULL, 0);
194 pixman_image_set_transform(ps->image, NULL);
195
196 if (!es->transform.enabled) {
197 pixman_region32_translate(&final_region, es->geometry.x, es->geometry.y);
198 } else {
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +0300199 weston_surface_to_global_float(es, 0, 0, &surface_x, &surface_y);
200 pixman_region32_translate(&final_region, (int)surface_x, (int)surface_y);
Vasily Khoruzhickf4a457f2013-01-28 22:40:29 +0300201 }
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +0300202
203 /* That's what we need to paint */
204 pixman_region32_intersect(&final_region, &final_region, region);
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +0300205 rects = pixman_region32_rectangles(&final_region, &nrects);
206
207 for (i = 0; i < nrects; i++) {
208 weston_surface_from_global(es, rects[i].x1, rects[i].y1, &src_x, &src_y);
209 pixman_image_composite32(pixman_op,
210 ps->image, /* src */
211 NULL /* mask */,
Ander Conselvan de Oliveira936effd2013-01-25 15:13:01 +0200212 po->shadow_image, /* dest */
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +0300213 src_x, src_y, /* src_x, src_y */
214 0, 0, /* mask_x, mask_y */
215 rects[i].x1, rects[i].y1, /* dest_x, dest_y */
216 rects[i].x2 - rects[i].x1, /* width */
217 rects[i].y2 - rects[i].y1 /* height */);
Ander Conselvan de Oliveira8792ef12013-01-25 15:13:00 +0200218
219 if (!pr->repaint_debug)
220 continue;
221
222 pixman_image_composite32(PIXMAN_OP_OVER,
223 pr->debug_color, /* src */
224 NULL /* mask */,
Ander Conselvan de Oliveira936effd2013-01-25 15:13:01 +0200225 po->shadow_image, /* dest */
Ander Conselvan de Oliveira8792ef12013-01-25 15:13:00 +0200226 src_x, src_y, /* src_x, src_y */
227 0, 0, /* mask_x, mask_y */
228 rects[i].x1, rects[i].y1, /* dest_x, dest_y */
229 rects[i].x2 - rects[i].x1, /* width */
230 rects[i].y2 - rects[i].y1 /* height */);
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +0300231 }
232 pixman_region32_fini(&final_region);
233}
234
235static void
236draw_surface(struct weston_surface *es, struct weston_output *output,
237 pixman_region32_t *damage) /* in global coordinates */
238{
239 struct pixman_surface_state *ps = get_surface_state(es);
240 /* repaint bounding region in global coordinates: */
241 pixman_region32_t repaint;
242 /* non-opaque region in surface coordinates: */
243 pixman_region32_t surface_blend;
244
245 /* No buffer attached */
246 if (!ps->image)
247 return;
248
249 pixman_region32_init(&repaint);
250 pixman_region32_intersect(&repaint,
251 &es->transform.boundingbox, damage);
252 pixman_region32_subtract(&repaint, &repaint, &es->clip);
253
254 if (!pixman_region32_not_empty(&repaint))
255 goto out;
256
257 if (output->zoom.active) {
258 weston_log("pixman renderer does not support zoom\n");
259 goto out;
260 }
261
Vasily Khoruzhickf4a457f2013-01-28 22:40:29 +0300262 /* TODO: Implement repaint_region_complex() using pixman_composite_trapezoids() */
263 if (es->transform.enabled &&
264 es->transform.matrix.type != WESTON_MATRIX_TRANSFORM_TRANSLATE) {
265 repaint_region_complex(es, output, &repaint);
266 } else {
267 /* blended region is whole surface minus opaque region: */
268 pixman_region32_init_rect(&surface_blend, 0, 0,
269 es->geometry.width, es->geometry.height);
270 pixman_region32_subtract(&surface_blend, &surface_blend, &es->opaque);
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +0300271
Vasily Khoruzhickf4a457f2013-01-28 22:40:29 +0300272 if (pixman_region32_not_empty(&es->opaque)) {
273 repaint_region_simple(es, output, &repaint, &es->opaque, PIXMAN_OP_SRC);
274 }
275
276 if (pixman_region32_not_empty(&surface_blend)) {
277 repaint_region_simple(es, output, &repaint, &surface_blend, PIXMAN_OP_OVER);
278 }
279 pixman_region32_fini(&surface_blend);
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +0300280 }
281
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +0300282
283out:
284 pixman_region32_fini(&repaint);
285}
286static void
287repaint_surfaces(struct weston_output *output, pixman_region32_t *damage)
288{
289 struct weston_compositor *compositor = output->compositor;
290 struct weston_surface *surface;
291
292 wl_list_for_each_reverse(surface, &compositor->surface_list, link)
293 if (surface->plane == &compositor->primary_plane)
294 draw_surface(surface, output, damage);
295}
296
297static void
Ander Conselvan de Oliveira936effd2013-01-25 15:13:01 +0200298copy_to_hw_buffer(struct weston_output *output, pixman_region32_t *region)
299{
300 struct pixman_output_state *po = get_output_state(output);
301 int nrects, i, width, height;
302 pixman_box32_t *rects;
303 pixman_box32_t b;
304
305 width = pixman_image_get_width(po->shadow_image);
306 height = pixman_image_get_height(po->shadow_image);
307
308 rects = pixman_region32_rectangles(region, &nrects);
309 for (i = 0; i < nrects; i++) {
310 b = weston_transformed_rect(width, height,
311 output->transform, rects[i]);
312
313 pixman_image_composite32(PIXMAN_OP_SRC,
314 po->shadow_image, /* src */
315 NULL /* mask */,
316 po->hw_buffer, /* dest */
317 b.x1, b.y1, /* src_x, src_y */
318 0, 0, /* mask_x, mask_y */
319 b.x1, b.y1, /* dest_x, dest_y */
320 b.x2 - b.x1, /* width */
321 b.y2 - b.y1 /* height */);
322 }
323
324}
325
326static void
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +0300327pixman_renderer_repaint_output(struct weston_output *output,
328 pixman_region32_t *output_damage)
329{
330 struct pixman_output_state *po = get_output_state(output);
331
332 if (!po->hw_buffer)
333 return;
334
335 repaint_surfaces(output, output_damage);
Ander Conselvan de Oliveira936effd2013-01-25 15:13:01 +0200336 copy_to_hw_buffer(output, output_damage);
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +0300337
338 pixman_region32_copy(&output->previous_damage, output_damage);
339 wl_signal_emit(&output->frame_signal, output);
340
341 /* Actual flip should be done by caller */
342}
343
344static void
345pixman_renderer_flush_damage(struct weston_surface *surface)
346{
347 /* No-op for pixman renderer */
348}
349
350static void
351pixman_renderer_attach(struct weston_surface *es, struct wl_buffer *buffer)
352{
353 struct pixman_surface_state *ps = get_surface_state(es);
354 pixman_format_code_t pixman_format;
355
356 weston_buffer_reference(&ps->buffer_ref, buffer);
357
358 if (ps->image) {
359 pixman_image_unref(ps->image);
360 ps->image = NULL;
361 }
362
363 if (!buffer)
364 return;
365
366 if (!wl_buffer_is_shm(buffer)) {
367 weston_log("Pixman renderer supports only SHM buffers\n");
368 weston_buffer_reference(&ps->buffer_ref, NULL);
369 return;
370 }
371
372 switch (wl_shm_buffer_get_format(buffer)) {
373 case WL_SHM_FORMAT_XRGB8888:
374 pixman_format = PIXMAN_x8r8g8b8;
375 break;
376 case WL_SHM_FORMAT_ARGB8888:
377 pixman_format = PIXMAN_a8r8g8b8;
378 break;
379 default:
380 weston_log("Unsupported SHM buffer format\n");
381 weston_buffer_reference(&ps->buffer_ref, NULL);
382 return;
383 break;
384 }
385 ps->image = pixman_image_create_bits(pixman_format,
386 wl_shm_buffer_get_width(buffer),
387 wl_shm_buffer_get_height(buffer),
388 wl_shm_buffer_get_data(buffer),
389 wl_shm_buffer_get_stride(buffer));
390}
391
392static int
393pixman_renderer_create_surface(struct weston_surface *surface)
394{
395 struct pixman_surface_state *ps;
396
397 ps = calloc(1, sizeof *ps);
398 if (!ps)
399 return -1;
400
401 surface->renderer_state = ps;
402
403 return 0;
404}
405
406static void
407pixman_renderer_surface_set_color(struct weston_surface *es,
408 float red, float green, float blue, float alpha)
409{
410 struct pixman_surface_state *ps = get_surface_state(es);
411 pixman_color_t color;
412
413 color.red = red * 0xffff;
414 color.green = green * 0xffff;
415 color.blue = blue * 0xffff;
416 color.alpha = alpha * 0xffff;
417
418 if (ps->image) {
419 pixman_image_unref(ps->image);
420 ps->image = NULL;
421 }
422
423 ps->image = pixman_image_create_solid_fill(&color);
424}
425
426static void
427pixman_renderer_destroy_surface(struct weston_surface *surface)
428{
429 struct pixman_surface_state *ps = get_surface_state(surface);
430
431 if (ps->image) {
432 pixman_image_unref(ps->image);
433 ps->image = NULL;
434 }
435 weston_buffer_reference(&ps->buffer_ref, NULL);
436 free(ps);
437}
438
Vasily Khoruzhick52cfd612013-01-08 19:09:01 +0300439static void
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +0300440pixman_renderer_destroy(struct weston_compositor *ec)
441{
442 free(ec->renderer);
443 ec->renderer = NULL;
444}
445
Ander Conselvan de Oliveira8792ef12013-01-25 15:13:00 +0200446static void
447debug_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
448 void *data)
449{
450 struct weston_compositor *ec = data;
451 struct pixman_renderer *pr = (struct pixman_renderer *) ec->renderer;
452
453 pr->repaint_debug ^= 1;
454
455 if (pr->repaint_debug) {
456 pixman_color_t red = {
457 0x3fff, 0x0000, 0x0000, 0x3fff
458 };
459
460 pr->debug_color = pixman_image_create_solid_fill(&red);
461 } else {
462 pixman_image_unref(pr->debug_color);
463 weston_compositor_damage_all(ec);
464 }
465}
466
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +0300467WL_EXPORT int
468pixman_renderer_init(struct weston_compositor *ec)
469{
Ander Conselvan de Oliveira8792ef12013-01-25 15:13:00 +0200470 struct pixman_renderer *renderer;
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +0300471
472 renderer = malloc(sizeof *renderer);
473 if (renderer == NULL)
474 return -1;
475
Ander Conselvan de Oliveira8792ef12013-01-25 15:13:00 +0200476 renderer->base.read_pixels = pixman_renderer_read_pixels;
477 renderer->base.repaint_output = pixman_renderer_repaint_output;
478 renderer->base.flush_damage = pixman_renderer_flush_damage;
479 renderer->base.attach = pixman_renderer_attach;
480 renderer->base.create_surface = pixman_renderer_create_surface;
481 renderer->base.surface_set_color = pixman_renderer_surface_set_color;
482 renderer->base.destroy_surface = pixman_renderer_destroy_surface;
483 renderer->base.destroy = pixman_renderer_destroy;
484 ec->renderer = &renderer->base;
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +0300485
Ander Conselvan de Oliveira8792ef12013-01-25 15:13:00 +0200486 weston_compositor_add_debug_binding(ec, KEY_R,
487 debug_binding, ec);
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +0300488 return 0;
489}
490
491WL_EXPORT void
492pixman_renderer_output_set_buffer(struct weston_output *output, pixman_image_t *buffer)
493{
494 struct pixman_output_state *po = get_output_state(output);
495
496 if (po->hw_buffer)
497 pixman_image_unref(po->hw_buffer);
498 po->hw_buffer = buffer;
499
500 if (po->hw_buffer) {
501 output->compositor->read_format = pixman_image_get_format(po->hw_buffer);
502 pixman_image_ref(po->hw_buffer);
503 }
504}
505
506WL_EXPORT int
507pixman_renderer_output_create(struct weston_output *output)
508{
509 struct pixman_output_state *po = calloc(1, sizeof *po);
Ander Conselvan de Oliveira936effd2013-01-25 15:13:01 +0200510 pixman_transform_t transform;
511 pixman_fixed_t fw, fh;
512 int w, h;
513 int rotated = 0;
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +0300514
515 if (!po)
516 return -1;
Ander Conselvan de Oliveira936effd2013-01-25 15:13:01 +0200517
518 /* set shadow image transformation */
519 w = output->current->width;
520 h = output->current->height;
521
522 fw = pixman_int_to_fixed(w);
523 fh = pixman_int_to_fixed(h);
524
525 pixman_transform_init_identity(&transform);
526
527 switch (output->transform) {
528 default:
529 case WL_OUTPUT_TRANSFORM_NORMAL:
530 break;
531 case WL_OUTPUT_TRANSFORM_180:
532 pixman_transform_rotate(&transform, NULL, -pixman_fixed_1, 0);
533 pixman_transform_translate(NULL, &transform, fw, fh);
534 break;
535 case WL_OUTPUT_TRANSFORM_270:
536 rotated = 1;
537 pixman_transform_rotate(&transform, NULL, 0, pixman_fixed_1);
538 pixman_transform_translate(&transform, NULL, fh, 0);
539 break;
540 case WL_OUTPUT_TRANSFORM_90:
541 rotated = 1;
542 pixman_transform_rotate(&transform, NULL, 0, -pixman_fixed_1);
543 pixman_transform_translate(&transform, NULL, 0, fw);
544 break;
545 }
546
547 if (rotated) {
548 int tmp = w;
549 w = h;
550 h = tmp;
551 }
552
553 po->shadow_buffer = malloc(w * h * 4);
554
555 if (!po->shadow_buffer) {
556 free(po);
557 return -1;
558 }
559
560 po->shadow_image =
561 pixman_image_create_bits(PIXMAN_x8r8g8b8, w, h,
562 po->shadow_buffer, w * 4);
563
564 if (!po->shadow_image) {
565 free(po->shadow_buffer);
566 free(po);
567 return -1;
568 }
569
570 pixman_image_set_transform(po->shadow_image, &transform);
571
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +0300572 output->renderer_state = po;
573
574 return 0;
575}
576
577WL_EXPORT void
578pixman_renderer_output_destroy(struct weston_output *output)
579{
580 struct pixman_output_state *po = get_output_state(output);
581
Ander Conselvan de Oliveira936effd2013-01-25 15:13:01 +0200582 pixman_image_unref(po->shadow_image);
Ander Conselvan de Oliveira23e72b82013-01-25 15:13:06 +0200583
584 if (po->hw_buffer)
585 pixman_image_unref(po->hw_buffer);
Ander Conselvan de Oliveira936effd2013-01-25 15:13:01 +0200586
587 po->shadow_image = NULL;
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +0300588 po->hw_buffer = NULL;
589
590 free(po);
591}