blob: 8fd50e3d13fab4f3ed7ca190d05868febe374f31 [file] [log] [blame]
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +03001/*
2 * Copyright © 2012 Intel Corporation
3 * Copyright © 2013 Vasily Khoruzhick <anarsoul@gmail.com>
Pekka Paalanen6764bbe2015-02-11 12:29:56 +02004 * Copyright © 2015 Collabora, Ltd.
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +03005 *
6 * Permission to use, copy, modify, distribute, and sell this software and
7 * its documentation for any purpose is hereby granted without fee, provided
8 * that the above copyright notice appear in all copies and that both that
9 * copyright notice and this permission notice appear in supporting
10 * documentation, and that the name of the copyright holders not be used in
11 * advertising or publicity pertaining to distribution of the software
12 * without specific, written prior permission. The copyright holders make
13 * no representations about the suitability of this software for any
14 * purpose. It is provided "as is" without express or implied warranty.
15 *
16 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
17 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
18 * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
19 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
20 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
21 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
22 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23 */
24
Daniel Stonec228e232013-05-22 18:03:19 +030025#include "config.h"
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +030026
27#include <errno.h>
28#include <stdlib.h>
29
30#include "pixman-renderer.h"
31
Ander Conselvan de Oliveira8792ef12013-01-25 15:13:00 +020032#include <linux/input.h>
33
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +030034struct pixman_output_state {
Ander Conselvan de Oliveira936effd2013-01-25 15:13:01 +020035 void *shadow_buffer;
36 pixman_image_t *shadow_image;
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +030037 pixman_image_t *hw_buffer;
38};
39
40struct pixman_surface_state {
Ander Conselvan de Oliveiraaa398ae2013-10-25 16:26:33 +030041 struct weston_surface *surface;
42
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +030043 pixman_image_t *image;
44 struct weston_buffer_reference buffer_ref;
Ander Conselvan de Oliveiraaa398ae2013-10-25 16:26:33 +030045
Lubomir Rintelddc2b1e2013-12-12 12:57:56 +010046 struct wl_listener buffer_destroy_listener;
Ander Conselvan de Oliveiraaa398ae2013-10-25 16:26:33 +030047 struct wl_listener surface_destroy_listener;
Ander Conselvan de Oliveiraadda00e2013-10-25 16:26:34 +030048 struct wl_listener renderer_destroy_listener;
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +030049};
50
51struct pixman_renderer {
52 struct weston_renderer base;
Ander Conselvan de Oliveira6b162142013-10-25 16:26:32 +030053
Ander Conselvan de Oliveira8792ef12013-01-25 15:13:00 +020054 int repaint_debug;
55 pixman_image_t *debug_color;
Ander Conselvan de Oliveira6b162142013-10-25 16:26:32 +030056 struct weston_binding *debug_binding;
Ander Conselvan de Oliveiraadda00e2013-10-25 16:26:34 +030057
58 struct wl_signal destroy_signal;
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +030059};
60
61static inline struct pixman_output_state *
62get_output_state(struct weston_output *output)
63{
64 return (struct pixman_output_state *)output->renderer_state;
65}
66
Ander Conselvan de Oliveiraaa398ae2013-10-25 16:26:33 +030067static int
68pixman_renderer_create_surface(struct weston_surface *surface);
69
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +030070static inline struct pixman_surface_state *
71get_surface_state(struct weston_surface *surface)
72{
Ander Conselvan de Oliveiraaa398ae2013-10-25 16:26:33 +030073 if (!surface->renderer_state)
74 pixman_renderer_create_surface(surface);
75
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +030076 return (struct pixman_surface_state *)surface->renderer_state;
77}
78
79static inline struct pixman_renderer *
80get_renderer(struct weston_compositor *ec)
81{
82 return (struct pixman_renderer *)ec->renderer;
83}
84
85static int
86pixman_renderer_read_pixels(struct weston_output *output,
87 pixman_format_code_t format, void *pixels,
88 uint32_t x, uint32_t y,
89 uint32_t width, uint32_t height)
90{
91 struct pixman_output_state *po = get_output_state(output);
Alexander Larsson97af7922013-05-29 12:01:33 +020092 pixman_transform_t transform;
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +030093 pixman_image_t *out_buf;
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +030094
95 if (!po->hw_buffer) {
96 errno = ENODEV;
97 return -1;
98 }
99
100 out_buf = pixman_image_create_bits(format,
101 width,
102 height,
103 pixels,
104 (PIXMAN_FORMAT_BPP(format) / 8) * width);
105
Alexander Larsson97af7922013-05-29 12:01:33 +0200106 /* Caller expects vflipped source image */
107 pixman_transform_init_translate(&transform,
108 pixman_int_to_fixed (x),
109 pixman_int_to_fixed (y - pixman_image_get_height (po->hw_buffer)));
110 pixman_transform_scale(&transform, NULL,
111 pixman_fixed_1,
112 pixman_fixed_minus_1);
113 pixman_image_set_transform(po->hw_buffer, &transform);
114
115 pixman_image_composite32(PIXMAN_OP_SRC,
116 po->hw_buffer, /* src */
117 NULL /* mask */,
118 out_buf, /* dest */
119 0, 0, /* src_x, src_y */
120 0, 0, /* mask_x, mask_y */
121 0, 0, /* dest_x, dest_y */
122 pixman_image_get_width (po->hw_buffer), /* width */
123 pixman_image_get_height (po->hw_buffer) /* height */);
124 pixman_image_set_transform(po->hw_buffer, NULL);
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +0300125
126 pixman_image_unref(out_buf);
127
128 return 0;
129}
130
Vasily Khoruzhick031fc872013-01-29 14:58:14 +0300131static void
Alexander Larsson1f206b42013-05-22 14:41:36 +0200132region_global_to_output(struct weston_output *output, pixman_region32_t *region)
133{
134 pixman_region32_translate(region, -output->x, -output->y);
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500135 weston_transformed_region(output->width, output->height,
136 output->transform, output->current_scale,
137 region, region);
Vasily Khoruzhick031fc872013-01-29 14:58:14 +0300138}
139
Vasily Khoruzhickf4a457f2013-01-28 22:40:29 +0300140#define D2F(v) pixman_double_to_fixed((double)v)
141
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +0300142static void
Jason Ekstrand8870a232014-05-20 15:53:19 -0500143weston_matrix_to_pixman_transform(pixman_transform_t *pt,
144 const struct weston_matrix *wm)
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200145{
Jason Ekstrand8870a232014-05-20 15:53:19 -0500146 /* Pixman supports only 2D transform matrix, but Weston uses 3D, *
147 * so we're omitting Z coordinate here. */
148 pt->matrix[0][0] = pixman_double_to_fixed(wm->d[0]);
149 pt->matrix[0][1] = pixman_double_to_fixed(wm->d[4]);
150 pt->matrix[0][2] = pixman_double_to_fixed(wm->d[12]);
151 pt->matrix[1][0] = pixman_double_to_fixed(wm->d[1]);
152 pt->matrix[1][1] = pixman_double_to_fixed(wm->d[5]);
153 pt->matrix[1][2] = pixman_double_to_fixed(wm->d[13]);
154 pt->matrix[2][0] = pixman_double_to_fixed(wm->d[3]);
155 pt->matrix[2][1] = pixman_double_to_fixed(wm->d[7]);
156 pt->matrix[2][2] = pixman_double_to_fixed(wm->d[15]);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200157}
158
159static void
Pekka Paalanen6ea523b2015-03-04 14:18:39 +0200160pixman_renderer_compute_transform(pixman_transform_t *transform_out,
161 struct weston_view *ev,
162 struct weston_output *output)
163{
164 struct weston_matrix matrix;
165
166 /* Set up the source transformation based on the surface
167 position, the output position/transform/scale and the client
168 specified buffer transform/scale */
169 weston_matrix_invert(&matrix, &output->matrix);
170
171 if (ev->transform.enabled) {
172 weston_matrix_multiply(&matrix, &ev->transform.inverse);
173 } else {
174 weston_matrix_translate(&matrix,
175 -ev->geometry.x, -ev->geometry.y, 0);
176 }
177
178 weston_matrix_multiply(&matrix, &ev->surface->surface_to_buffer_matrix);
179
180 weston_matrix_to_pixman_transform(transform_out, &matrix);
181}
182
183static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500184repaint_region(struct weston_view *ev, struct weston_output *output,
Alexander Larsson1f206b42013-05-22 14:41:36 +0200185 pixman_region32_t *region, pixman_region32_t *surf_region,
186 pixman_op_t pixman_op)
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +0300187{
Ander Conselvan de Oliveira8792ef12013-01-25 15:13:00 +0200188 struct pixman_renderer *pr =
189 (struct pixman_renderer *) output->compositor->renderer;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500190 struct pixman_surface_state *ps = get_surface_state(ev->surface);
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +0300191 struct pixman_output_state *po = get_output_state(output);
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200192 struct weston_buffer_viewport *vp = &ev->surface->buffer_viewport;
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +0300193 pixman_region32_t final_region;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500194 float view_x, view_y;
Alexander Larsson1f206b42013-05-22 14:41:36 +0200195 pixman_transform_t transform;
Manuel Bachmann9c4ab662014-04-07 11:58:45 +0200196 pixman_image_t *mask_image;
197 pixman_color_t mask = { 0, };
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +0300198
199 /* The final region to be painted is the intersection of
200 * 'region' and 'surf_region'. However, 'region' is in the global
201 * coordinates, and 'surf_region' is in the surface-local
202 * coordinates
203 */
204 pixman_region32_init(&final_region);
Alexander Larsson1f206b42013-05-22 14:41:36 +0200205 if (surf_region) {
206 pixman_region32_copy(&final_region, surf_region);
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +0300207
Alexander Larsson1f206b42013-05-22 14:41:36 +0200208 /* Convert from surface to global coordinates */
Jason Ekstranda7af7042013-10-12 22:38:11 -0500209 if (!ev->transform.enabled) {
210 pixman_region32_translate(&final_region, ev->geometry.x, ev->geometry.y);
Alexander Larsson1f206b42013-05-22 14:41:36 +0200211 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500212 weston_view_to_global_float(ev, 0, 0, &view_x, &view_y);
213 pixman_region32_translate(&final_region, (int)view_x, (int)view_y);
Alexander Larsson1f206b42013-05-22 14:41:36 +0200214 }
Vasily Khoruzhickf4a457f2013-01-28 22:40:29 +0300215
Alexander Larsson1f206b42013-05-22 14:41:36 +0200216 /* We need to paint the intersection */
217 pixman_region32_intersect(&final_region, &final_region, region);
Vasily Khoruzhickf4a457f2013-01-28 22:40:29 +0300218 } else {
Alexander Larsson1f206b42013-05-22 14:41:36 +0200219 /* If there is no surface region, just use the global region */
220 pixman_region32_copy(&final_region, region);
Vasily Khoruzhickf4a457f2013-01-28 22:40:29 +0300221 }
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +0300222
Alexander Larsson1f206b42013-05-22 14:41:36 +0200223 /* Convert from global to output coord */
224 region_global_to_output(output, &final_region);
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +0300225
Alexander Larsson1f206b42013-05-22 14:41:36 +0200226 /* And clip to it */
227 pixman_image_set_clip_region32 (po->shadow_image, &final_region);
Ander Conselvan de Oliveira8792ef12013-01-25 15:13:00 +0200228
Pekka Paalanen6ea523b2015-03-04 14:18:39 +0200229 pixman_renderer_compute_transform(&transform, ev, output);
Alexander Larsson1f206b42013-05-22 14:41:36 +0200230 pixman_image_set_transform(ps->image, &transform);
231
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200232 if (ev->transform.enabled || output->current_scale != vp->buffer.scale)
Alexander Larsson1f206b42013-05-22 14:41:36 +0200233 pixman_image_set_filter(ps->image, PIXMAN_FILTER_BILINEAR, NULL, 0);
234 else
235 pixman_image_set_filter(ps->image, PIXMAN_FILTER_NEAREST, NULL, 0);
236
Neil Robertse5051712013-11-13 15:44:06 +0000237 if (ps->buffer_ref.buffer)
238 wl_shm_buffer_begin_access(ps->buffer_ref.buffer->shm_buffer);
239
Manuel Bachmannb1fff412014-04-05 05:31:37 +0200240 if (ev->alpha < 1.0) {
Manuel Bachmann9c4ab662014-04-07 11:58:45 +0200241 mask.alpha = 0xffff * ev->alpha;
Manuel Bachmannb1fff412014-04-05 05:31:37 +0200242 mask_image = pixman_image_create_solid_fill(&mask);
Manuel Bachmann9c4ab662014-04-07 11:58:45 +0200243 } else {
Manuel Bachmannb1fff412014-04-05 05:31:37 +0200244 mask_image = NULL;
Manuel Bachmann9c4ab662014-04-07 11:58:45 +0200245 }
Manuel Bachmannb1fff412014-04-05 05:31:37 +0200246
Alexander Larsson1f206b42013-05-22 14:41:36 +0200247 pixman_image_composite32(pixman_op,
248 ps->image, /* src */
Manuel Bachmannb1fff412014-04-05 05:31:37 +0200249 mask_image, /* mask */
Alexander Larsson1f206b42013-05-22 14:41:36 +0200250 po->shadow_image, /* dest */
251 0, 0, /* src_x, src_y */
252 0, 0, /* mask_x, mask_y */
253 0, 0, /* dest_x, dest_y */
254 pixman_image_get_width (po->shadow_image), /* width */
255 pixman_image_get_height (po->shadow_image) /* height */);
256
Manuel Bachmann9c4ab662014-04-07 11:58:45 +0200257 if (mask_image)
258 pixman_image_unref(mask_image);
259
Neil Robertse5051712013-11-13 15:44:06 +0000260 if (ps->buffer_ref.buffer)
261 wl_shm_buffer_end_access(ps->buffer_ref.buffer->shm_buffer);
262
Alexander Larsson1f206b42013-05-22 14:41:36 +0200263 if (pr->repaint_debug)
Ander Conselvan de Oliveira8792ef12013-01-25 15:13:00 +0200264 pixman_image_composite32(PIXMAN_OP_OVER,
Alexander Larsson1f206b42013-05-22 14:41:36 +0200265 pr->debug_color, /* src */
266 NULL /* mask */,
267 po->shadow_image, /* dest */
268 0, 0, /* src_x, src_y */
269 0, 0, /* mask_x, mask_y */
270 0, 0, /* dest_x, dest_y */
271 pixman_image_get_width (po->shadow_image), /* width */
272 pixman_image_get_height (po->shadow_image) /* height */);
273
274 pixman_image_set_clip_region32 (po->shadow_image, NULL);
275
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +0300276 pixman_region32_fini(&final_region);
277}
278
279static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500280draw_view(struct weston_view *ev, struct weston_output *output,
281 pixman_region32_t *damage) /* in global coordinates */
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +0300282{
Derek Foreman66951b72014-09-01 10:33:28 -0500283 static int zoom_logged = 0;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500284 struct pixman_surface_state *ps = get_surface_state(ev->surface);
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +0300285 /* repaint bounding region in global coordinates: */
286 pixman_region32_t repaint;
287 /* non-opaque region in surface coordinates: */
288 pixman_region32_t surface_blend;
289
290 /* No buffer attached */
291 if (!ps->image)
292 return;
293
294 pixman_region32_init(&repaint);
295 pixman_region32_intersect(&repaint,
Pekka Paalanen25c0ca52015-02-19 11:15:33 +0200296 &ev->transform.boundingbox, damage);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500297 pixman_region32_subtract(&repaint, &repaint, &ev->clip);
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +0300298
299 if (!pixman_region32_not_empty(&repaint))
300 goto out;
301
Derek Foreman66951b72014-09-01 10:33:28 -0500302 if (output->zoom.active && !zoom_logged) {
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +0300303 weston_log("pixman renderer does not support zoom\n");
Derek Foreman66951b72014-09-01 10:33:28 -0500304 zoom_logged = 1;
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +0300305 }
306
Vasily Khoruzhickf4a457f2013-01-28 22:40:29 +0300307 /* TODO: Implement repaint_region_complex() using pixman_composite_trapezoids() */
Manuel Bachmann9c4ab662014-04-07 11:58:45 +0200308 if (ev->alpha != 1.0 ||
309 (ev->transform.enabled &&
310 ev->transform.matrix.type != WESTON_MATRIX_TRANSFORM_TRANSLATE)) {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500311 repaint_region(ev, output, &repaint, NULL, PIXMAN_OP_OVER);
Vasily Khoruzhickf4a457f2013-01-28 22:40:29 +0300312 } else {
313 /* blended region is whole surface minus opaque region: */
314 pixman_region32_init_rect(&surface_blend, 0, 0,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600315 ev->surface->width, ev->surface->height);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500316 pixman_region32_subtract(&surface_blend, &surface_blend, &ev->surface->opaque);
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +0300317
Jason Ekstranda7af7042013-10-12 22:38:11 -0500318 if (pixman_region32_not_empty(&ev->surface->opaque)) {
319 repaint_region(ev, output, &repaint, &ev->surface->opaque, PIXMAN_OP_SRC);
Vasily Khoruzhickf4a457f2013-01-28 22:40:29 +0300320 }
321
322 if (pixman_region32_not_empty(&surface_blend)) {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500323 repaint_region(ev, output, &repaint, &surface_blend, PIXMAN_OP_OVER);
Vasily Khoruzhickf4a457f2013-01-28 22:40:29 +0300324 }
325 pixman_region32_fini(&surface_blend);
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +0300326 }
327
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +0300328
329out:
330 pixman_region32_fini(&repaint);
331}
332static void
333repaint_surfaces(struct weston_output *output, pixman_region32_t *damage)
334{
335 struct weston_compositor *compositor = output->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500336 struct weston_view *view;
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +0300337
Jason Ekstranda7af7042013-10-12 22:38:11 -0500338 wl_list_for_each_reverse(view, &compositor->view_list, link)
339 if (view->plane == &compositor->primary_plane)
340 draw_view(view, output, damage);
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +0300341}
342
343static void
Ander Conselvan de Oliveira936effd2013-01-25 15:13:01 +0200344copy_to_hw_buffer(struct weston_output *output, pixman_region32_t *region)
345{
346 struct pixman_output_state *po = get_output_state(output);
Alexander Larsson1f206b42013-05-22 14:41:36 +0200347 pixman_region32_t output_region;
Ander Conselvan de Oliveira936effd2013-01-25 15:13:01 +0200348
Alexander Larsson1f206b42013-05-22 14:41:36 +0200349 pixman_region32_init(&output_region);
350 pixman_region32_copy(&output_region, region);
Ander Conselvan de Oliveira936effd2013-01-25 15:13:01 +0200351
Alexander Larsson1f206b42013-05-22 14:41:36 +0200352 region_global_to_output(output, &output_region);
Ander Conselvan de Oliveira936effd2013-01-25 15:13:01 +0200353
Alexander Larsson1f206b42013-05-22 14:41:36 +0200354 pixman_image_set_clip_region32 (po->hw_buffer, &output_region);
Ryo Munakata389d2052014-09-04 01:56:53 +0900355 pixman_region32_fini(&output_region);
Ander Conselvan de Oliveira936effd2013-01-25 15:13:01 +0200356
Alexander Larsson1f206b42013-05-22 14:41:36 +0200357 pixman_image_composite32(PIXMAN_OP_SRC,
358 po->shadow_image, /* src */
359 NULL /* mask */,
360 po->hw_buffer, /* dest */
361 0, 0, /* src_x, src_y */
362 0, 0, /* mask_x, mask_y */
363 0, 0, /* dest_x, dest_y */
364 pixman_image_get_width (po->hw_buffer), /* width */
365 pixman_image_get_height (po->hw_buffer) /* height */);
366
367 pixman_image_set_clip_region32 (po->hw_buffer, NULL);
Ander Conselvan de Oliveira936effd2013-01-25 15:13:01 +0200368}
369
370static void
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +0300371pixman_renderer_repaint_output(struct weston_output *output,
372 pixman_region32_t *output_damage)
373{
374 struct pixman_output_state *po = get_output_state(output);
375
376 if (!po->hw_buffer)
377 return;
378
379 repaint_surfaces(output, output_damage);
Ander Conselvan de Oliveira936effd2013-01-25 15:13:01 +0200380 copy_to_hw_buffer(output, output_damage);
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +0300381
382 pixman_region32_copy(&output->previous_damage, output_damage);
383 wl_signal_emit(&output->frame_signal, output);
384
385 /* Actual flip should be done by caller */
386}
387
388static void
389pixman_renderer_flush_damage(struct weston_surface *surface)
390{
391 /* No-op for pixman renderer */
392}
393
394static void
Lubomir Rintelddc2b1e2013-12-12 12:57:56 +0100395buffer_state_handle_buffer_destroy(struct wl_listener *listener, void *data)
396{
397 struct pixman_surface_state *ps;
398
399 ps = container_of(listener, struct pixman_surface_state,
400 buffer_destroy_listener);
401
402 if (ps->image) {
403 pixman_image_unref(ps->image);
404 ps->image = NULL;
405 }
406
407 ps->buffer_destroy_listener.notify = NULL;
408}
409
410static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -0500411pixman_renderer_attach(struct weston_surface *es, struct weston_buffer *buffer)
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +0300412{
413 struct pixman_surface_state *ps = get_surface_state(es);
Jason Ekstrand6bd62942013-06-20 20:38:23 -0500414 struct wl_shm_buffer *shm_buffer;
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +0300415 pixman_format_code_t pixman_format;
416
417 weston_buffer_reference(&ps->buffer_ref, buffer);
418
Lubomir Rintelddc2b1e2013-12-12 12:57:56 +0100419 if (ps->buffer_destroy_listener.notify) {
420 wl_list_remove(&ps->buffer_destroy_listener.link);
421 ps->buffer_destroy_listener.notify = NULL;
422 }
423
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +0300424 if (ps->image) {
425 pixman_image_unref(ps->image);
426 ps->image = NULL;
427 }
428
429 if (!buffer)
430 return;
Jason Ekstrand6bd62942013-06-20 20:38:23 -0500431
432 shm_buffer = wl_shm_buffer_get(buffer->resource);
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +0300433
Jason Ekstrand6bd62942013-06-20 20:38:23 -0500434 if (! shm_buffer) {
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +0300435 weston_log("Pixman renderer supports only SHM buffers\n");
436 weston_buffer_reference(&ps->buffer_ref, NULL);
437 return;
438 }
439
Jason Ekstrand6bd62942013-06-20 20:38:23 -0500440 switch (wl_shm_buffer_get_format(shm_buffer)) {
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +0300441 case WL_SHM_FORMAT_XRGB8888:
442 pixman_format = PIXMAN_x8r8g8b8;
443 break;
444 case WL_SHM_FORMAT_ARGB8888:
445 pixman_format = PIXMAN_a8r8g8b8;
446 break;
Tomeu Vizoso1c1fc292013-08-06 20:05:56 +0200447 case WL_SHM_FORMAT_RGB565:
448 pixman_format = PIXMAN_r5g6b5;
449 break;
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +0300450 default:
451 weston_log("Unsupported SHM buffer format\n");
452 weston_buffer_reference(&ps->buffer_ref, NULL);
453 return;
454 break;
455 }
Jason Ekstrand6bd62942013-06-20 20:38:23 -0500456
457 buffer->shm_buffer = shm_buffer;
458 buffer->width = wl_shm_buffer_get_width(shm_buffer);
459 buffer->height = wl_shm_buffer_get_height(shm_buffer);
460
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +0300461 ps->image = pixman_image_create_bits(pixman_format,
Jason Ekstrand6bd62942013-06-20 20:38:23 -0500462 buffer->width, buffer->height,
463 wl_shm_buffer_get_data(shm_buffer),
464 wl_shm_buffer_get_stride(shm_buffer));
Lubomir Rintelddc2b1e2013-12-12 12:57:56 +0100465
466 ps->buffer_destroy_listener.notify =
467 buffer_state_handle_buffer_destroy;
468 wl_signal_add(&buffer->destroy_signal,
469 &ps->buffer_destroy_listener);
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +0300470}
471
Ander Conselvan de Oliveiraaa398ae2013-10-25 16:26:33 +0300472static void
Ander Conselvan de Oliveiraadda00e2013-10-25 16:26:34 +0300473pixman_renderer_surface_state_destroy(struct pixman_surface_state *ps)
Ander Conselvan de Oliveiraaa398ae2013-10-25 16:26:33 +0300474{
Ander Conselvan de Oliveiraadda00e2013-10-25 16:26:34 +0300475 wl_list_remove(&ps->surface_destroy_listener.link);
476 wl_list_remove(&ps->renderer_destroy_listener.link);
Lubomir Rintelddc2b1e2013-12-12 12:57:56 +0100477 if (ps->buffer_destroy_listener.notify) {
478 wl_list_remove(&ps->buffer_destroy_listener.link);
479 ps->buffer_destroy_listener.notify = NULL;
480 }
Ander Conselvan de Oliveiraaa398ae2013-10-25 16:26:33 +0300481
482 ps->surface->renderer_state = NULL;
483
484 if (ps->image) {
485 pixman_image_unref(ps->image);
486 ps->image = NULL;
487 }
488 weston_buffer_reference(&ps->buffer_ref, NULL);
489 free(ps);
490}
491
Ander Conselvan de Oliveiraadda00e2013-10-25 16:26:34 +0300492static void
493surface_state_handle_surface_destroy(struct wl_listener *listener, void *data)
494{
495 struct pixman_surface_state *ps;
496
497 ps = container_of(listener, struct pixman_surface_state,
498 surface_destroy_listener);
499
500 pixman_renderer_surface_state_destroy(ps);
501}
502
503static void
504surface_state_handle_renderer_destroy(struct wl_listener *listener, void *data)
505{
506 struct pixman_surface_state *ps;
507
508 ps = container_of(listener, struct pixman_surface_state,
509 renderer_destroy_listener);
510
511 pixman_renderer_surface_state_destroy(ps);
512}
513
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +0300514static int
515pixman_renderer_create_surface(struct weston_surface *surface)
516{
517 struct pixman_surface_state *ps;
Ander Conselvan de Oliveiraadda00e2013-10-25 16:26:34 +0300518 struct pixman_renderer *pr = get_renderer(surface->compositor);
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +0300519
Bryce Harringtonde16d892014-11-20 22:21:57 -0800520 ps = zalloc(sizeof *ps);
521 if (ps == NULL)
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +0300522 return -1;
523
524 surface->renderer_state = ps;
525
Ander Conselvan de Oliveiraaa398ae2013-10-25 16:26:33 +0300526 ps->surface = surface;
527
528 ps->surface_destroy_listener.notify =
529 surface_state_handle_surface_destroy;
530 wl_signal_add(&surface->destroy_signal,
531 &ps->surface_destroy_listener);
532
Ander Conselvan de Oliveiraadda00e2013-10-25 16:26:34 +0300533 ps->renderer_destroy_listener.notify =
534 surface_state_handle_renderer_destroy;
535 wl_signal_add(&pr->destroy_signal,
536 &ps->renderer_destroy_listener);
537
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +0300538 return 0;
539}
540
541static void
542pixman_renderer_surface_set_color(struct weston_surface *es,
543 float red, float green, float blue, float alpha)
544{
545 struct pixman_surface_state *ps = get_surface_state(es);
546 pixman_color_t color;
547
548 color.red = red * 0xffff;
549 color.green = green * 0xffff;
550 color.blue = blue * 0xffff;
551 color.alpha = alpha * 0xffff;
552
553 if (ps->image) {
554 pixman_image_unref(ps->image);
555 ps->image = NULL;
556 }
557
558 ps->image = pixman_image_create_solid_fill(&color);
559}
560
561static void
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +0300562pixman_renderer_destroy(struct weston_compositor *ec)
563{
Ander Conselvan de Oliveira6b162142013-10-25 16:26:32 +0300564 struct pixman_renderer *pr = get_renderer(ec);
565
Ander Conselvan de Oliveiraadda00e2013-10-25 16:26:34 +0300566 wl_signal_emit(&pr->destroy_signal, pr);
Ander Conselvan de Oliveira6b162142013-10-25 16:26:32 +0300567 weston_binding_destroy(pr->debug_binding);
568 free(pr);
569
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +0300570 ec->renderer = NULL;
571}
572
Ander Conselvan de Oliveira8792ef12013-01-25 15:13:00 +0200573static void
Pekka Paalanen6764bbe2015-02-11 12:29:56 +0200574pixman_renderer_surface_get_content_size(struct weston_surface *surface,
575 int *width, int *height)
576{
577 struct pixman_surface_state *ps = get_surface_state(surface);
578
579 if (ps->image) {
580 *width = pixman_image_get_width(ps->image);
581 *height = pixman_image_get_height(ps->image);
582 } else {
583 *width = 0;
584 *height = 0;
585 }
586}
587
588static int
589pixman_renderer_surface_copy_content(struct weston_surface *surface,
590 void *target, size_t size,
591 int src_x, int src_y,
592 int width, int height)
593{
594 const pixman_format_code_t format = PIXMAN_a8b8g8r8;
595 const size_t bytespp = 4; /* PIXMAN_a8b8g8r8 */
596 struct pixman_surface_state *ps = get_surface_state(surface);
597 pixman_image_t *out_buf;
598
599 if (!ps->image)
600 return -1;
601
602 out_buf = pixman_image_create_bits(format, width, height,
603 target, width * bytespp);
604
605 pixman_image_set_transform(ps->image, NULL);
606 pixman_image_composite32(PIXMAN_OP_SRC,
607 ps->image, /* src */
608 NULL, /* mask */
609 out_buf, /* dest */
610 src_x, src_y, /* src_x, src_y */
611 0, 0, /* mask_x, mask_y */
612 0, 0, /* dest_x, dest_y */
613 width, height);
614
615 pixman_image_unref(out_buf);
616
617 return 0;
618}
619
620static void
Kristian Høgsberge3148752013-05-06 23:19:49 -0400621debug_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Ander Conselvan de Oliveira8792ef12013-01-25 15:13:00 +0200622 void *data)
623{
624 struct weston_compositor *ec = data;
625 struct pixman_renderer *pr = (struct pixman_renderer *) ec->renderer;
626
627 pr->repaint_debug ^= 1;
628
629 if (pr->repaint_debug) {
630 pixman_color_t red = {
631 0x3fff, 0x0000, 0x0000, 0x3fff
632 };
633
634 pr->debug_color = pixman_image_create_solid_fill(&red);
635 } else {
636 pixman_image_unref(pr->debug_color);
637 weston_compositor_damage_all(ec);
638 }
639}
640
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +0300641WL_EXPORT int
642pixman_renderer_init(struct weston_compositor *ec)
643{
Ander Conselvan de Oliveira8792ef12013-01-25 15:13:00 +0200644 struct pixman_renderer *renderer;
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +0300645
Bryce Harringtonde16d892014-11-20 22:21:57 -0800646 renderer = zalloc(sizeof *renderer);
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +0300647 if (renderer == NULL)
648 return -1;
649
Philipp Brüschweilerf3e39f92013-03-08 20:35:39 +0100650 renderer->repaint_debug = 0;
651 renderer->debug_color = NULL;
Ander Conselvan de Oliveira8792ef12013-01-25 15:13:00 +0200652 renderer->base.read_pixels = pixman_renderer_read_pixels;
653 renderer->base.repaint_output = pixman_renderer_repaint_output;
654 renderer->base.flush_damage = pixman_renderer_flush_damage;
655 renderer->base.attach = pixman_renderer_attach;
Ander Conselvan de Oliveira8792ef12013-01-25 15:13:00 +0200656 renderer->base.surface_set_color = pixman_renderer_surface_set_color;
Ander Conselvan de Oliveira8792ef12013-01-25 15:13:00 +0200657 renderer->base.destroy = pixman_renderer_destroy;
Pekka Paalanen6764bbe2015-02-11 12:29:56 +0200658 renderer->base.surface_get_content_size =
659 pixman_renderer_surface_get_content_size;
660 renderer->base.surface_copy_content =
661 pixman_renderer_surface_copy_content;
Ander Conselvan de Oliveira8792ef12013-01-25 15:13:00 +0200662 ec->renderer = &renderer->base;
Pekka Paalanen7bb65102013-05-22 18:03:04 +0300663 ec->capabilities |= WESTON_CAP_ROTATION_ANY;
Pekka Paalanen4fc5dd02013-05-22 18:03:05 +0300664 ec->capabilities |= WESTON_CAP_CAPTURE_YFLIP;
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +0300665
Ander Conselvan de Oliveira6b162142013-10-25 16:26:32 +0300666 renderer->debug_binding =
667 weston_compositor_add_debug_binding(ec, KEY_R,
668 debug_binding, ec);
Tomeu Vizoso1c1fc292013-08-06 20:05:56 +0200669
670 wl_display_add_shm_format(ec->wl_display, WL_SHM_FORMAT_RGB565);
671
Ander Conselvan de Oliveiraadda00e2013-10-25 16:26:34 +0300672 wl_signal_init(&renderer->destroy_signal);
673
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +0300674 return 0;
675}
676
677WL_EXPORT void
678pixman_renderer_output_set_buffer(struct weston_output *output, pixman_image_t *buffer)
679{
680 struct pixman_output_state *po = get_output_state(output);
681
682 if (po->hw_buffer)
683 pixman_image_unref(po->hw_buffer);
684 po->hw_buffer = buffer;
685
686 if (po->hw_buffer) {
687 output->compositor->read_format = pixman_image_get_format(po->hw_buffer);
688 pixman_image_ref(po->hw_buffer);
689 }
690}
691
692WL_EXPORT int
693pixman_renderer_output_create(struct weston_output *output)
694{
Bryce Harringtonde16d892014-11-20 22:21:57 -0800695 struct pixman_output_state *po;
Ander Conselvan de Oliveira936effd2013-01-25 15:13:01 +0200696 int w, h;
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +0300697
Bryce Harringtonde16d892014-11-20 22:21:57 -0800698 po = zalloc(sizeof *po);
699 if (po == NULL)
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +0300700 return -1;
Ander Conselvan de Oliveira936effd2013-01-25 15:13:01 +0200701
702 /* set shadow image transformation */
Hardeningff39efa2013-09-18 23:56:35 +0200703 w = output->current_mode->width;
704 h = output->current_mode->height;
Ander Conselvan de Oliveira936effd2013-01-25 15:13:01 +0200705
Ander Conselvan de Oliveira936effd2013-01-25 15:13:01 +0200706 po->shadow_buffer = malloc(w * h * 4);
707
708 if (!po->shadow_buffer) {
709 free(po);
710 return -1;
711 }
712
713 po->shadow_image =
714 pixman_image_create_bits(PIXMAN_x8r8g8b8, w, h,
715 po->shadow_buffer, w * 4);
716
717 if (!po->shadow_image) {
718 free(po->shadow_buffer);
719 free(po);
720 return -1;
721 }
722
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +0300723 output->renderer_state = po;
724
725 return 0;
726}
727
728WL_EXPORT void
729pixman_renderer_output_destroy(struct weston_output *output)
730{
731 struct pixman_output_state *po = get_output_state(output);
732
Ander Conselvan de Oliveira936effd2013-01-25 15:13:01 +0200733 pixman_image_unref(po->shadow_image);
Ander Conselvan de Oliveira23e72b82013-01-25 15:13:06 +0200734
735 if (po->hw_buffer)
736 pixman_image_unref(po->hw_buffer);
Ander Conselvan de Oliveira936effd2013-01-25 15:13:01 +0200737
Arnaud Vrac8e3fe082014-08-25 20:56:52 +0200738 free(po->shadow_buffer);
739
740 po->shadow_buffer = NULL;
Ander Conselvan de Oliveira936effd2013-01-25 15:13:01 +0200741 po->shadow_image = NULL;
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +0300742 po->hw_buffer = NULL;
743
744 free(po);
745}