blob: 129affcdf8d254aedf0f702e0526bed74bb2ad98 [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 {
Ander Conselvan de Oliveiraaa398ae2013-10-25 16:26:33 +030040 struct weston_surface *surface;
41
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +030042 pixman_image_t *image;
43 struct weston_buffer_reference buffer_ref;
Ander Conselvan de Oliveiraaa398ae2013-10-25 16:26:33 +030044
Lubomir Rintelddc2b1e2013-12-12 12:57:56 +010045 struct wl_listener buffer_destroy_listener;
Ander Conselvan de Oliveiraaa398ae2013-10-25 16:26:33 +030046 struct wl_listener surface_destroy_listener;
Ander Conselvan de Oliveiraadda00e2013-10-25 16:26:34 +030047 struct wl_listener renderer_destroy_listener;
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +030048};
49
50struct pixman_renderer {
51 struct weston_renderer base;
Ander Conselvan de Oliveira6b162142013-10-25 16:26:32 +030052
Ander Conselvan de Oliveira8792ef12013-01-25 15:13:00 +020053 int repaint_debug;
54 pixman_image_t *debug_color;
Ander Conselvan de Oliveira6b162142013-10-25 16:26:32 +030055 struct weston_binding *debug_binding;
Ander Conselvan de Oliveiraadda00e2013-10-25 16:26:34 +030056
57 struct wl_signal destroy_signal;
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +030058};
59
60static inline struct pixman_output_state *
61get_output_state(struct weston_output *output)
62{
63 return (struct pixman_output_state *)output->renderer_state;
64}
65
Ander Conselvan de Oliveiraaa398ae2013-10-25 16:26:33 +030066static int
67pixman_renderer_create_surface(struct weston_surface *surface);
68
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +030069static inline struct pixman_surface_state *
70get_surface_state(struct weston_surface *surface)
71{
Ander Conselvan de Oliveiraaa398ae2013-10-25 16:26:33 +030072 if (!surface->renderer_state)
73 pixman_renderer_create_surface(surface);
74
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +030075 return (struct pixman_surface_state *)surface->renderer_state;
76}
77
78static inline struct pixman_renderer *
79get_renderer(struct weston_compositor *ec)
80{
81 return (struct pixman_renderer *)ec->renderer;
82}
83
84static int
85pixman_renderer_read_pixels(struct weston_output *output,
86 pixman_format_code_t format, void *pixels,
87 uint32_t x, uint32_t y,
88 uint32_t width, uint32_t height)
89{
90 struct pixman_output_state *po = get_output_state(output);
Alexander Larsson97af7922013-05-29 12:01:33 +020091 pixman_transform_t transform;
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +030092 pixman_image_t *out_buf;
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +030093
94 if (!po->hw_buffer) {
95 errno = ENODEV;
96 return -1;
97 }
98
99 out_buf = pixman_image_create_bits(format,
100 width,
101 height,
102 pixels,
103 (PIXMAN_FORMAT_BPP(format) / 8) * width);
104
Alexander Larsson97af7922013-05-29 12:01:33 +0200105 /* Caller expects vflipped source image */
106 pixman_transform_init_translate(&transform,
107 pixman_int_to_fixed (x),
108 pixman_int_to_fixed (y - pixman_image_get_height (po->hw_buffer)));
109 pixman_transform_scale(&transform, NULL,
110 pixman_fixed_1,
111 pixman_fixed_minus_1);
112 pixman_image_set_transform(po->hw_buffer, &transform);
113
114 pixman_image_composite32(PIXMAN_OP_SRC,
115 po->hw_buffer, /* src */
116 NULL /* mask */,
117 out_buf, /* dest */
118 0, 0, /* src_x, src_y */
119 0, 0, /* mask_x, mask_y */
120 0, 0, /* dest_x, dest_y */
121 pixman_image_get_width (po->hw_buffer), /* width */
122 pixman_image_get_height (po->hw_buffer) /* height */);
123 pixman_image_set_transform(po->hw_buffer, NULL);
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +0300124
125 pixman_image_unref(out_buf);
126
127 return 0;
128}
129
Vasily Khoruzhick031fc872013-01-29 14:58:14 +0300130static void
Alexander Larsson1f206b42013-05-22 14:41:36 +0200131region_global_to_output(struct weston_output *output, pixman_region32_t *region)
132{
133 pixman_region32_translate(region, -output->x, -output->y);
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500134 weston_transformed_region(output->width, output->height,
135 output->transform, output->current_scale,
136 region, region);
Vasily Khoruzhick031fc872013-01-29 14:58:14 +0300137}
138
Vasily Khoruzhickf4a457f2013-01-28 22:40:29 +0300139#define D2F(v) pixman_double_to_fixed((double)v)
140
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +0300141static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500142repaint_region(struct weston_view *ev, struct weston_output *output,
Alexander Larsson1f206b42013-05-22 14:41:36 +0200143 pixman_region32_t *region, pixman_region32_t *surf_region,
144 pixman_op_t pixman_op)
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +0300145{
Ander Conselvan de Oliveira8792ef12013-01-25 15:13:00 +0200146 struct pixman_renderer *pr =
147 (struct pixman_renderer *) output->compositor->renderer;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500148 struct pixman_surface_state *ps = get_surface_state(ev->surface);
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +0300149 struct pixman_output_state *po = get_output_state(output);
150 pixman_region32_t final_region;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500151 float view_x, view_y;
Alexander Larsson1f206b42013-05-22 14:41:36 +0200152 pixman_transform_t transform;
153 pixman_fixed_t fw, fh;
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +0300154
155 /* The final region to be painted is the intersection of
156 * 'region' and 'surf_region'. However, 'region' is in the global
157 * coordinates, and 'surf_region' is in the surface-local
158 * coordinates
159 */
160 pixman_region32_init(&final_region);
Alexander Larsson1f206b42013-05-22 14:41:36 +0200161 if (surf_region) {
162 pixman_region32_copy(&final_region, surf_region);
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +0300163
Alexander Larsson1f206b42013-05-22 14:41:36 +0200164 /* Convert from surface to global coordinates */
Jason Ekstranda7af7042013-10-12 22:38:11 -0500165 if (!ev->transform.enabled) {
166 pixman_region32_translate(&final_region, ev->geometry.x, ev->geometry.y);
Alexander Larsson1f206b42013-05-22 14:41:36 +0200167 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500168 weston_view_to_global_float(ev, 0, 0, &view_x, &view_y);
169 pixman_region32_translate(&final_region, (int)view_x, (int)view_y);
Alexander Larsson1f206b42013-05-22 14:41:36 +0200170 }
Vasily Khoruzhickf4a457f2013-01-28 22:40:29 +0300171
Alexander Larsson1f206b42013-05-22 14:41:36 +0200172 /* We need to paint the intersection */
173 pixman_region32_intersect(&final_region, &final_region, region);
Vasily Khoruzhickf4a457f2013-01-28 22:40:29 +0300174 } else {
Alexander Larsson1f206b42013-05-22 14:41:36 +0200175 /* If there is no surface region, just use the global region */
176 pixman_region32_copy(&final_region, region);
Vasily Khoruzhickf4a457f2013-01-28 22:40:29 +0300177 }
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +0300178
Alexander Larsson1f206b42013-05-22 14:41:36 +0200179 /* Convert from global to output coord */
180 region_global_to_output(output, &final_region);
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +0300181
Alexander Larsson1f206b42013-05-22 14:41:36 +0200182 /* And clip to it */
183 pixman_image_set_clip_region32 (po->shadow_image, &final_region);
Ander Conselvan de Oliveira8792ef12013-01-25 15:13:00 +0200184
Alexander Larsson1f206b42013-05-22 14:41:36 +0200185 /* Set up the source transformation based on the surface
Alexander Larsson4ea95522013-05-22 14:41:37 +0200186 position, the output position/transform/scale and the client
187 specified buffer transform/scale */
Alexander Larsson1f206b42013-05-22 14:41:36 +0200188 pixman_transform_init_identity(&transform);
Alexander Larsson4ea95522013-05-22 14:41:37 +0200189 pixman_transform_scale(&transform, NULL,
Hardeningff39efa2013-09-18 23:56:35 +0200190 pixman_double_to_fixed ((double)1.0/output->current_scale),
191 pixman_double_to_fixed ((double)1.0/output->current_scale));
Ander Conselvan de Oliveira8792ef12013-01-25 15:13:00 +0200192
Alexander Larsson1f206b42013-05-22 14:41:36 +0200193 fw = pixman_int_to_fixed(output->width);
194 fh = pixman_int_to_fixed(output->height);
195 switch (output->transform) {
196 default:
197 case WL_OUTPUT_TRANSFORM_NORMAL:
198 case WL_OUTPUT_TRANSFORM_FLIPPED:
199 break;
200 case WL_OUTPUT_TRANSFORM_90:
201 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
202 pixman_transform_rotate(&transform, NULL, 0, -pixman_fixed_1);
203 pixman_transform_translate(&transform, NULL, 0, fh);
204 break;
205 case WL_OUTPUT_TRANSFORM_180:
206 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
207 pixman_transform_rotate(&transform, NULL, -pixman_fixed_1, 0);
208 pixman_transform_translate(&transform, NULL, fw, fh);
209 break;
210 case WL_OUTPUT_TRANSFORM_270:
211 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
212 pixman_transform_rotate(&transform, NULL, 0, pixman_fixed_1);
213 pixman_transform_translate(&transform, NULL, fw, 0);
214 break;
215 }
216
217 switch (output->transform) {
218 case WL_OUTPUT_TRANSFORM_FLIPPED:
219 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
220 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
221 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
222 pixman_transform_scale(&transform, NULL,
223 pixman_int_to_fixed (-1),
224 pixman_int_to_fixed (1));
225 pixman_transform_translate(&transform, NULL, fw, 0);
226 break;
227 }
228
229 pixman_transform_translate(&transform, NULL,
230 pixman_double_to_fixed (output->x),
231 pixman_double_to_fixed (output->y));
232
Jason Ekstranda7af7042013-10-12 22:38:11 -0500233 if (ev->transform.enabled) {
Alexander Larsson1f206b42013-05-22 14:41:36 +0200234 /* Pixman supports only 2D transform matrix, but Weston uses 3D,
235 * so we're omitting Z coordinate here
236 */
237 pixman_transform_t surface_transform = {{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500238 { D2F(ev->transform.matrix.d[0]),
239 D2F(ev->transform.matrix.d[4]),
240 D2F(ev->transform.matrix.d[12]),
Alexander Larsson1f206b42013-05-22 14:41:36 +0200241 },
Jason Ekstranda7af7042013-10-12 22:38:11 -0500242 { D2F(ev->transform.matrix.d[1]),
243 D2F(ev->transform.matrix.d[5]),
244 D2F(ev->transform.matrix.d[13]),
Alexander Larsson1f206b42013-05-22 14:41:36 +0200245 },
Jason Ekstranda7af7042013-10-12 22:38:11 -0500246 { D2F(ev->transform.matrix.d[3]),
247 D2F(ev->transform.matrix.d[7]),
248 D2F(ev->transform.matrix.d[15]),
Alexander Larsson1f206b42013-05-22 14:41:36 +0200249 }
250 }};
251
252 pixman_transform_invert(&surface_transform, &surface_transform);
253 pixman_transform_multiply (&transform, &surface_transform, &transform);
254 } else {
255 pixman_transform_translate(&transform, NULL,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500256 pixman_double_to_fixed ((double)-ev->geometry.x),
257 pixman_double_to_fixed ((double)-ev->geometry.y));
Alexander Larsson1f206b42013-05-22 14:41:36 +0200258 }
259
Jonny Lambfa1b3052013-11-26 18:19:47 +0100260 if (ev->surface->buffer_viewport.scaler_set) {
261 double scaler_x, scaler_y, scaler_width, scaler_height;
262 double ratio_x, ratio_y;
Alexander Larsson1f206b42013-05-22 14:41:36 +0200263
Jonny Lambfa1b3052013-11-26 18:19:47 +0100264 scaler_x = wl_fixed_to_double(ev->surface->buffer_viewport.src_x);
265 scaler_y = wl_fixed_to_double(ev->surface->buffer_viewport.src_y);
266 scaler_width = wl_fixed_to_double(ev->surface->buffer_viewport.src_width);
267 scaler_height = wl_fixed_to_double(ev->surface->buffer_viewport.src_height);
268
269 ratio_x = scaler_width / ev->surface->buffer_viewport.dst_width;
270 ratio_y = scaler_height / ev->surface->buffer_viewport.dst_height;
271
272 pixman_transform_scale(&transform, NULL,
273 pixman_double_to_fixed(ratio_x),
274 pixman_double_to_fixed(ratio_y));
275 pixman_transform_translate(&transform, NULL, pixman_double_to_fixed(scaler_x),
276 pixman_double_to_fixed(scaler_y));
277 }
278
279 pixman_transform_scale(&transform, NULL,
280 pixman_double_to_fixed(ev->surface->buffer_viewport.scale),
281 pixman_double_to_fixed(ev->surface->buffer_viewport.scale));
282
283 fw = pixman_int_to_fixed(pixman_image_get_width(ps->image));
284 fh = pixman_int_to_fixed(pixman_image_get_height(ps->image));
Alexander Larsson1f206b42013-05-22 14:41:36 +0200285
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +0100286 switch (ev->surface->buffer_viewport.transform) {
Alexander Larsson1f206b42013-05-22 14:41:36 +0200287 case WL_OUTPUT_TRANSFORM_FLIPPED:
288 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
289 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
290 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
291 pixman_transform_scale(&transform, NULL,
292 pixman_int_to_fixed (-1),
293 pixman_int_to_fixed (1));
294 pixman_transform_translate(&transform, NULL, fw, 0);
295 break;
296 }
297
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +0100298 switch (ev->surface->buffer_viewport.transform) {
Alexander Larsson1f206b42013-05-22 14:41:36 +0200299 default:
300 case WL_OUTPUT_TRANSFORM_NORMAL:
301 case WL_OUTPUT_TRANSFORM_FLIPPED:
302 break;
303 case WL_OUTPUT_TRANSFORM_90:
304 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
305 pixman_transform_rotate(&transform, NULL, 0, pixman_fixed_1);
306 pixman_transform_translate(&transform, NULL, fh, 0);
307 break;
308 case WL_OUTPUT_TRANSFORM_180:
309 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
310 pixman_transform_rotate(&transform, NULL, -pixman_fixed_1, 0);
311 pixman_transform_translate(&transform, NULL, fw, fh);
312 break;
313 case WL_OUTPUT_TRANSFORM_270:
314 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
315 pixman_transform_rotate(&transform, NULL, 0, -pixman_fixed_1);
316 pixman_transform_translate(&transform, NULL, 0, fw);
317 break;
318 }
319
320 pixman_image_set_transform(ps->image, &transform);
321
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +0100322 if (ev->transform.enabled || output->current_scale != ev->surface->buffer_viewport.scale)
Alexander Larsson1f206b42013-05-22 14:41:36 +0200323 pixman_image_set_filter(ps->image, PIXMAN_FILTER_BILINEAR, NULL, 0);
324 else
325 pixman_image_set_filter(ps->image, PIXMAN_FILTER_NEAREST, NULL, 0);
326
Neil Robertse5051712013-11-13 15:44:06 +0000327 if (ps->buffer_ref.buffer)
328 wl_shm_buffer_begin_access(ps->buffer_ref.buffer->shm_buffer);
329
Alexander Larsson1f206b42013-05-22 14:41:36 +0200330 pixman_image_composite32(pixman_op,
331 ps->image, /* src */
332 NULL /* mask */,
333 po->shadow_image, /* dest */
334 0, 0, /* src_x, src_y */
335 0, 0, /* mask_x, mask_y */
336 0, 0, /* dest_x, dest_y */
337 pixman_image_get_width (po->shadow_image), /* width */
338 pixman_image_get_height (po->shadow_image) /* height */);
339
Neil Robertse5051712013-11-13 15:44:06 +0000340 if (ps->buffer_ref.buffer)
341 wl_shm_buffer_end_access(ps->buffer_ref.buffer->shm_buffer);
342
Alexander Larsson1f206b42013-05-22 14:41:36 +0200343 if (pr->repaint_debug)
Ander Conselvan de Oliveira8792ef12013-01-25 15:13:00 +0200344 pixman_image_composite32(PIXMAN_OP_OVER,
Alexander Larsson1f206b42013-05-22 14:41:36 +0200345 pr->debug_color, /* src */
346 NULL /* mask */,
347 po->shadow_image, /* dest */
348 0, 0, /* src_x, src_y */
349 0, 0, /* mask_x, mask_y */
350 0, 0, /* dest_x, dest_y */
351 pixman_image_get_width (po->shadow_image), /* width */
352 pixman_image_get_height (po->shadow_image) /* height */);
353
354 pixman_image_set_clip_region32 (po->shadow_image, NULL);
355
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +0300356 pixman_region32_fini(&final_region);
357}
358
359static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500360draw_view(struct weston_view *ev, struct weston_output *output,
361 pixman_region32_t *damage) /* in global coordinates */
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +0300362{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500363 struct pixman_surface_state *ps = get_surface_state(ev->surface);
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +0300364 /* repaint bounding region in global coordinates: */
365 pixman_region32_t repaint;
366 /* non-opaque region in surface coordinates: */
367 pixman_region32_t surface_blend;
368
369 /* No buffer attached */
370 if (!ps->image)
371 return;
372
373 pixman_region32_init(&repaint);
374 pixman_region32_intersect(&repaint,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500375 &ev->transform.boundingbox, damage);
376 pixman_region32_subtract(&repaint, &repaint, &ev->clip);
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +0300377
378 if (!pixman_region32_not_empty(&repaint))
379 goto out;
380
381 if (output->zoom.active) {
382 weston_log("pixman renderer does not support zoom\n");
383 goto out;
384 }
385
Vasily Khoruzhickf4a457f2013-01-28 22:40:29 +0300386 /* TODO: Implement repaint_region_complex() using pixman_composite_trapezoids() */
Jason Ekstranda7af7042013-10-12 22:38:11 -0500387 if (ev->transform.enabled &&
388 ev->transform.matrix.type != WESTON_MATRIX_TRANSFORM_TRANSLATE) {
389 repaint_region(ev, output, &repaint, NULL, PIXMAN_OP_OVER);
Vasily Khoruzhickf4a457f2013-01-28 22:40:29 +0300390 } else {
391 /* blended region is whole surface minus opaque region: */
392 pixman_region32_init_rect(&surface_blend, 0, 0,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600393 ev->surface->width, ev->surface->height);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500394 pixman_region32_subtract(&surface_blend, &surface_blend, &ev->surface->opaque);
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +0300395
Jason Ekstranda7af7042013-10-12 22:38:11 -0500396 if (pixman_region32_not_empty(&ev->surface->opaque)) {
397 repaint_region(ev, output, &repaint, &ev->surface->opaque, PIXMAN_OP_SRC);
Vasily Khoruzhickf4a457f2013-01-28 22:40:29 +0300398 }
399
400 if (pixman_region32_not_empty(&surface_blend)) {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500401 repaint_region(ev, output, &repaint, &surface_blend, PIXMAN_OP_OVER);
Vasily Khoruzhickf4a457f2013-01-28 22:40:29 +0300402 }
403 pixman_region32_fini(&surface_blend);
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +0300404 }
405
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +0300406
407out:
408 pixman_region32_fini(&repaint);
409}
410static void
411repaint_surfaces(struct weston_output *output, pixman_region32_t *damage)
412{
413 struct weston_compositor *compositor = output->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500414 struct weston_view *view;
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +0300415
Jason Ekstranda7af7042013-10-12 22:38:11 -0500416 wl_list_for_each_reverse(view, &compositor->view_list, link)
417 if (view->plane == &compositor->primary_plane)
418 draw_view(view, output, damage);
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +0300419}
420
421static void
Ander Conselvan de Oliveira936effd2013-01-25 15:13:01 +0200422copy_to_hw_buffer(struct weston_output *output, pixman_region32_t *region)
423{
424 struct pixman_output_state *po = get_output_state(output);
Alexander Larsson1f206b42013-05-22 14:41:36 +0200425 pixman_region32_t output_region;
Ander Conselvan de Oliveira936effd2013-01-25 15:13:01 +0200426
Alexander Larsson1f206b42013-05-22 14:41:36 +0200427 pixman_region32_init(&output_region);
428 pixman_region32_copy(&output_region, region);
Ander Conselvan de Oliveira936effd2013-01-25 15:13:01 +0200429
Alexander Larsson1f206b42013-05-22 14:41:36 +0200430 region_global_to_output(output, &output_region);
Ander Conselvan de Oliveira936effd2013-01-25 15:13:01 +0200431
Alexander Larsson1f206b42013-05-22 14:41:36 +0200432 pixman_image_set_clip_region32 (po->hw_buffer, &output_region);
Ander Conselvan de Oliveira936effd2013-01-25 15:13:01 +0200433
Alexander Larsson1f206b42013-05-22 14:41:36 +0200434 pixman_image_composite32(PIXMAN_OP_SRC,
435 po->shadow_image, /* src */
436 NULL /* mask */,
437 po->hw_buffer, /* dest */
438 0, 0, /* src_x, src_y */
439 0, 0, /* mask_x, mask_y */
440 0, 0, /* dest_x, dest_y */
441 pixman_image_get_width (po->hw_buffer), /* width */
442 pixman_image_get_height (po->hw_buffer) /* height */);
443
444 pixman_image_set_clip_region32 (po->hw_buffer, NULL);
Ander Conselvan de Oliveira936effd2013-01-25 15:13:01 +0200445}
446
447static void
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +0300448pixman_renderer_repaint_output(struct weston_output *output,
449 pixman_region32_t *output_damage)
450{
451 struct pixman_output_state *po = get_output_state(output);
452
453 if (!po->hw_buffer)
454 return;
455
456 repaint_surfaces(output, output_damage);
Ander Conselvan de Oliveira936effd2013-01-25 15:13:01 +0200457 copy_to_hw_buffer(output, output_damage);
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +0300458
459 pixman_region32_copy(&output->previous_damage, output_damage);
460 wl_signal_emit(&output->frame_signal, output);
461
462 /* Actual flip should be done by caller */
463}
464
465static void
466pixman_renderer_flush_damage(struct weston_surface *surface)
467{
468 /* No-op for pixman renderer */
469}
470
471static void
Lubomir Rintelddc2b1e2013-12-12 12:57:56 +0100472buffer_state_handle_buffer_destroy(struct wl_listener *listener, void *data)
473{
474 struct pixman_surface_state *ps;
475
476 ps = container_of(listener, struct pixman_surface_state,
477 buffer_destroy_listener);
478
479 if (ps->image) {
480 pixman_image_unref(ps->image);
481 ps->image = NULL;
482 }
483
484 ps->buffer_destroy_listener.notify = NULL;
485}
486
487static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -0500488pixman_renderer_attach(struct weston_surface *es, struct weston_buffer *buffer)
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +0300489{
490 struct pixman_surface_state *ps = get_surface_state(es);
Jason Ekstrand6bd62942013-06-20 20:38:23 -0500491 struct wl_shm_buffer *shm_buffer;
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +0300492 pixman_format_code_t pixman_format;
493
494 weston_buffer_reference(&ps->buffer_ref, buffer);
495
Lubomir Rintelddc2b1e2013-12-12 12:57:56 +0100496 if (ps->buffer_destroy_listener.notify) {
497 wl_list_remove(&ps->buffer_destroy_listener.link);
498 ps->buffer_destroy_listener.notify = NULL;
499 }
500
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +0300501 if (ps->image) {
502 pixman_image_unref(ps->image);
503 ps->image = NULL;
504 }
505
506 if (!buffer)
507 return;
Jason Ekstrand6bd62942013-06-20 20:38:23 -0500508
509 shm_buffer = wl_shm_buffer_get(buffer->resource);
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +0300510
Jason Ekstrand6bd62942013-06-20 20:38:23 -0500511 if (! shm_buffer) {
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +0300512 weston_log("Pixman renderer supports only SHM buffers\n");
513 weston_buffer_reference(&ps->buffer_ref, NULL);
514 return;
515 }
516
Jason Ekstrand6bd62942013-06-20 20:38:23 -0500517 switch (wl_shm_buffer_get_format(shm_buffer)) {
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +0300518 case WL_SHM_FORMAT_XRGB8888:
519 pixman_format = PIXMAN_x8r8g8b8;
520 break;
521 case WL_SHM_FORMAT_ARGB8888:
522 pixman_format = PIXMAN_a8r8g8b8;
523 break;
Tomeu Vizoso1c1fc292013-08-06 20:05:56 +0200524 case WL_SHM_FORMAT_RGB565:
525 pixman_format = PIXMAN_r5g6b5;
526 break;
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +0300527 default:
528 weston_log("Unsupported SHM buffer format\n");
529 weston_buffer_reference(&ps->buffer_ref, NULL);
530 return;
531 break;
532 }
Jason Ekstrand6bd62942013-06-20 20:38:23 -0500533
534 buffer->shm_buffer = shm_buffer;
535 buffer->width = wl_shm_buffer_get_width(shm_buffer);
536 buffer->height = wl_shm_buffer_get_height(shm_buffer);
537
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +0300538 ps->image = pixman_image_create_bits(pixman_format,
Jason Ekstrand6bd62942013-06-20 20:38:23 -0500539 buffer->width, buffer->height,
540 wl_shm_buffer_get_data(shm_buffer),
541 wl_shm_buffer_get_stride(shm_buffer));
Lubomir Rintelddc2b1e2013-12-12 12:57:56 +0100542
543 ps->buffer_destroy_listener.notify =
544 buffer_state_handle_buffer_destroy;
545 wl_signal_add(&buffer->destroy_signal,
546 &ps->buffer_destroy_listener);
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +0300547}
548
Ander Conselvan de Oliveiraaa398ae2013-10-25 16:26:33 +0300549static void
Ander Conselvan de Oliveiraadda00e2013-10-25 16:26:34 +0300550pixman_renderer_surface_state_destroy(struct pixman_surface_state *ps)
Ander Conselvan de Oliveiraaa398ae2013-10-25 16:26:33 +0300551{
Ander Conselvan de Oliveiraadda00e2013-10-25 16:26:34 +0300552 wl_list_remove(&ps->surface_destroy_listener.link);
553 wl_list_remove(&ps->renderer_destroy_listener.link);
Lubomir Rintelddc2b1e2013-12-12 12:57:56 +0100554 if (ps->buffer_destroy_listener.notify) {
555 wl_list_remove(&ps->buffer_destroy_listener.link);
556 ps->buffer_destroy_listener.notify = NULL;
557 }
Ander Conselvan de Oliveiraaa398ae2013-10-25 16:26:33 +0300558
559 ps->surface->renderer_state = NULL;
560
561 if (ps->image) {
562 pixman_image_unref(ps->image);
563 ps->image = NULL;
564 }
565 weston_buffer_reference(&ps->buffer_ref, NULL);
566 free(ps);
567}
568
Ander Conselvan de Oliveiraadda00e2013-10-25 16:26:34 +0300569static void
570surface_state_handle_surface_destroy(struct wl_listener *listener, void *data)
571{
572 struct pixman_surface_state *ps;
573
574 ps = container_of(listener, struct pixman_surface_state,
575 surface_destroy_listener);
576
577 pixman_renderer_surface_state_destroy(ps);
578}
579
580static void
581surface_state_handle_renderer_destroy(struct wl_listener *listener, void *data)
582{
583 struct pixman_surface_state *ps;
584
585 ps = container_of(listener, struct pixman_surface_state,
586 renderer_destroy_listener);
587
588 pixman_renderer_surface_state_destroy(ps);
589}
590
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +0300591static int
592pixman_renderer_create_surface(struct weston_surface *surface)
593{
594 struct pixman_surface_state *ps;
Ander Conselvan de Oliveiraadda00e2013-10-25 16:26:34 +0300595 struct pixman_renderer *pr = get_renderer(surface->compositor);
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +0300596
597 ps = calloc(1, sizeof *ps);
598 if (!ps)
599 return -1;
600
601 surface->renderer_state = ps;
602
Ander Conselvan de Oliveiraaa398ae2013-10-25 16:26:33 +0300603 ps->surface = surface;
604
605 ps->surface_destroy_listener.notify =
606 surface_state_handle_surface_destroy;
607 wl_signal_add(&surface->destroy_signal,
608 &ps->surface_destroy_listener);
609
Ander Conselvan de Oliveiraadda00e2013-10-25 16:26:34 +0300610 ps->renderer_destroy_listener.notify =
611 surface_state_handle_renderer_destroy;
612 wl_signal_add(&pr->destroy_signal,
613 &ps->renderer_destroy_listener);
614
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +0300615 return 0;
616}
617
618static void
619pixman_renderer_surface_set_color(struct weston_surface *es,
620 float red, float green, float blue, float alpha)
621{
622 struct pixman_surface_state *ps = get_surface_state(es);
623 pixman_color_t color;
624
625 color.red = red * 0xffff;
626 color.green = green * 0xffff;
627 color.blue = blue * 0xffff;
628 color.alpha = alpha * 0xffff;
629
630 if (ps->image) {
631 pixman_image_unref(ps->image);
632 ps->image = NULL;
633 }
634
635 ps->image = pixman_image_create_solid_fill(&color);
636}
637
638static void
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +0300639pixman_renderer_destroy(struct weston_compositor *ec)
640{
Ander Conselvan de Oliveira6b162142013-10-25 16:26:32 +0300641 struct pixman_renderer *pr = get_renderer(ec);
642
Ander Conselvan de Oliveiraadda00e2013-10-25 16:26:34 +0300643 wl_signal_emit(&pr->destroy_signal, pr);
Ander Conselvan de Oliveira6b162142013-10-25 16:26:32 +0300644 weston_binding_destroy(pr->debug_binding);
645 free(pr);
646
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +0300647 ec->renderer = NULL;
648}
649
Ander Conselvan de Oliveira8792ef12013-01-25 15:13:00 +0200650static void
Kristian Høgsberge3148752013-05-06 23:19:49 -0400651debug_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Ander Conselvan de Oliveira8792ef12013-01-25 15:13:00 +0200652 void *data)
653{
654 struct weston_compositor *ec = data;
655 struct pixman_renderer *pr = (struct pixman_renderer *) ec->renderer;
656
657 pr->repaint_debug ^= 1;
658
659 if (pr->repaint_debug) {
660 pixman_color_t red = {
661 0x3fff, 0x0000, 0x0000, 0x3fff
662 };
663
664 pr->debug_color = pixman_image_create_solid_fill(&red);
665 } else {
666 pixman_image_unref(pr->debug_color);
667 weston_compositor_damage_all(ec);
668 }
669}
670
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +0300671WL_EXPORT int
672pixman_renderer_init(struct weston_compositor *ec)
673{
Ander Conselvan de Oliveira8792ef12013-01-25 15:13:00 +0200674 struct pixman_renderer *renderer;
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +0300675
Ander Conselvan de Oliveirac68b1082013-10-25 16:26:31 +0300676 renderer = calloc(1, sizeof *renderer);
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +0300677 if (renderer == NULL)
678 return -1;
679
Philipp Brüschweilerf3e39f92013-03-08 20:35:39 +0100680 renderer->repaint_debug = 0;
681 renderer->debug_color = NULL;
Ander Conselvan de Oliveira8792ef12013-01-25 15:13:00 +0200682 renderer->base.read_pixels = pixman_renderer_read_pixels;
683 renderer->base.repaint_output = pixman_renderer_repaint_output;
684 renderer->base.flush_damage = pixman_renderer_flush_damage;
685 renderer->base.attach = pixman_renderer_attach;
Ander Conselvan de Oliveira8792ef12013-01-25 15:13:00 +0200686 renderer->base.surface_set_color = pixman_renderer_surface_set_color;
Ander Conselvan de Oliveira8792ef12013-01-25 15:13:00 +0200687 renderer->base.destroy = pixman_renderer_destroy;
688 ec->renderer = &renderer->base;
Pekka Paalanen7bb65102013-05-22 18:03:04 +0300689 ec->capabilities |= WESTON_CAP_ROTATION_ANY;
Pekka Paalanen4fc5dd02013-05-22 18:03:05 +0300690 ec->capabilities |= WESTON_CAP_CAPTURE_YFLIP;
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +0300691
Ander Conselvan de Oliveira6b162142013-10-25 16:26:32 +0300692 renderer->debug_binding =
693 weston_compositor_add_debug_binding(ec, KEY_R,
694 debug_binding, ec);
Tomeu Vizoso1c1fc292013-08-06 20:05:56 +0200695
696 wl_display_add_shm_format(ec->wl_display, WL_SHM_FORMAT_RGB565);
697
Ander Conselvan de Oliveiraadda00e2013-10-25 16:26:34 +0300698 wl_signal_init(&renderer->destroy_signal);
699
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +0300700 return 0;
701}
702
703WL_EXPORT void
704pixman_renderer_output_set_buffer(struct weston_output *output, pixman_image_t *buffer)
705{
706 struct pixman_output_state *po = get_output_state(output);
707
708 if (po->hw_buffer)
709 pixman_image_unref(po->hw_buffer);
710 po->hw_buffer = buffer;
711
712 if (po->hw_buffer) {
713 output->compositor->read_format = pixman_image_get_format(po->hw_buffer);
714 pixman_image_ref(po->hw_buffer);
715 }
716}
717
718WL_EXPORT int
719pixman_renderer_output_create(struct weston_output *output)
720{
721 struct pixman_output_state *po = calloc(1, sizeof *po);
Ander Conselvan de Oliveira936effd2013-01-25 15:13:01 +0200722 int w, h;
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +0300723
724 if (!po)
725 return -1;
Ander Conselvan de Oliveira936effd2013-01-25 15:13:01 +0200726
727 /* set shadow image transformation */
Hardeningff39efa2013-09-18 23:56:35 +0200728 w = output->current_mode->width;
729 h = output->current_mode->height;
Ander Conselvan de Oliveira936effd2013-01-25 15:13:01 +0200730
Ander Conselvan de Oliveira936effd2013-01-25 15:13:01 +0200731 po->shadow_buffer = malloc(w * h * 4);
732
733 if (!po->shadow_buffer) {
734 free(po);
735 return -1;
736 }
737
738 po->shadow_image =
739 pixman_image_create_bits(PIXMAN_x8r8g8b8, w, h,
740 po->shadow_buffer, w * 4);
741
742 if (!po->shadow_image) {
743 free(po->shadow_buffer);
744 free(po);
745 return -1;
746 }
747
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +0300748 output->renderer_state = po;
749
750 return 0;
751}
752
753WL_EXPORT void
754pixman_renderer_output_destroy(struct weston_output *output)
755{
756 struct pixman_output_state *po = get_output_state(output);
757
Ander Conselvan de Oliveira936effd2013-01-25 15:13:01 +0200758 pixman_image_unref(po->shadow_image);
Ander Conselvan de Oliveira23e72b82013-01-25 15:13:06 +0200759
760 if (po->hw_buffer)
761 pixman_image_unref(po->hw_buffer);
Ander Conselvan de Oliveira936effd2013-01-25 15:13:01 +0200762
763 po->shadow_image = NULL;
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +0300764 po->hw_buffer = NULL;
765
766 free(po);
767}