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