blob: 0b33a2b7f1e743d76d16f3085db86802ed04094c [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
Pekka Paalanen23d4af52015-03-04 16:18:26 +0200183static bool
184view_transformation_is_translation(struct weston_view *view)
185{
186 if (!view->transform.enabled)
187 return true;
188
189 if (view->transform.matrix.type <= WESTON_MATRIX_TRANSFORM_TRANSLATE)
190 return true;
191
192 return false;
193}
194
Pekka Paalanen6ea523b2015-03-04 14:18:39 +0200195static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500196repaint_region(struct weston_view *ev, struct weston_output *output,
Alexander Larsson1f206b42013-05-22 14:41:36 +0200197 pixman_region32_t *region, pixman_region32_t *surf_region,
198 pixman_op_t pixman_op)
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +0300199{
Ander Conselvan de Oliveira8792ef12013-01-25 15:13:00 +0200200 struct pixman_renderer *pr =
201 (struct pixman_renderer *) output->compositor->renderer;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500202 struct pixman_surface_state *ps = get_surface_state(ev->surface);
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +0300203 struct pixman_output_state *po = get_output_state(output);
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200204 struct weston_buffer_viewport *vp = &ev->surface->buffer_viewport;
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +0300205 pixman_region32_t final_region;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500206 float view_x, view_y;
Alexander Larsson1f206b42013-05-22 14:41:36 +0200207 pixman_transform_t transform;
Manuel Bachmann9c4ab662014-04-07 11:58:45 +0200208 pixman_image_t *mask_image;
209 pixman_color_t mask = { 0, };
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +0300210
211 /* The final region to be painted is the intersection of
212 * 'region' and 'surf_region'. However, 'region' is in the global
213 * coordinates, and 'surf_region' is in the surface-local
214 * coordinates
215 */
216 pixman_region32_init(&final_region);
Alexander Larsson1f206b42013-05-22 14:41:36 +0200217 if (surf_region) {
218 pixman_region32_copy(&final_region, surf_region);
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +0300219
Alexander Larsson1f206b42013-05-22 14:41:36 +0200220 /* Convert from surface to global coordinates */
Jason Ekstranda7af7042013-10-12 22:38:11 -0500221 if (!ev->transform.enabled) {
222 pixman_region32_translate(&final_region, ev->geometry.x, ev->geometry.y);
Alexander Larsson1f206b42013-05-22 14:41:36 +0200223 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500224 weston_view_to_global_float(ev, 0, 0, &view_x, &view_y);
225 pixman_region32_translate(&final_region, (int)view_x, (int)view_y);
Alexander Larsson1f206b42013-05-22 14:41:36 +0200226 }
Vasily Khoruzhickf4a457f2013-01-28 22:40:29 +0300227
Alexander Larsson1f206b42013-05-22 14:41:36 +0200228 /* We need to paint the intersection */
229 pixman_region32_intersect(&final_region, &final_region, region);
Vasily Khoruzhickf4a457f2013-01-28 22:40:29 +0300230 } else {
Alexander Larsson1f206b42013-05-22 14:41:36 +0200231 /* If there is no surface region, just use the global region */
232 pixman_region32_copy(&final_region, region);
Vasily Khoruzhickf4a457f2013-01-28 22:40:29 +0300233 }
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +0300234
Alexander Larsson1f206b42013-05-22 14:41:36 +0200235 /* Convert from global to output coord */
236 region_global_to_output(output, &final_region);
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +0300237
Alexander Larsson1f206b42013-05-22 14:41:36 +0200238 /* And clip to it */
239 pixman_image_set_clip_region32 (po->shadow_image, &final_region);
Ander Conselvan de Oliveira8792ef12013-01-25 15:13:00 +0200240
Pekka Paalanen6ea523b2015-03-04 14:18:39 +0200241 pixman_renderer_compute_transform(&transform, ev, output);
Alexander Larsson1f206b42013-05-22 14:41:36 +0200242 pixman_image_set_transform(ps->image, &transform);
243
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200244 if (ev->transform.enabled || output->current_scale != vp->buffer.scale)
Alexander Larsson1f206b42013-05-22 14:41:36 +0200245 pixman_image_set_filter(ps->image, PIXMAN_FILTER_BILINEAR, NULL, 0);
246 else
247 pixman_image_set_filter(ps->image, PIXMAN_FILTER_NEAREST, NULL, 0);
248
Neil Robertse5051712013-11-13 15:44:06 +0000249 if (ps->buffer_ref.buffer)
250 wl_shm_buffer_begin_access(ps->buffer_ref.buffer->shm_buffer);
251
Manuel Bachmannb1fff412014-04-05 05:31:37 +0200252 if (ev->alpha < 1.0) {
Manuel Bachmann9c4ab662014-04-07 11:58:45 +0200253 mask.alpha = 0xffff * ev->alpha;
Manuel Bachmannb1fff412014-04-05 05:31:37 +0200254 mask_image = pixman_image_create_solid_fill(&mask);
Manuel Bachmann9c4ab662014-04-07 11:58:45 +0200255 } else {
Manuel Bachmannb1fff412014-04-05 05:31:37 +0200256 mask_image = NULL;
Manuel Bachmann9c4ab662014-04-07 11:58:45 +0200257 }
Manuel Bachmannb1fff412014-04-05 05:31:37 +0200258
Alexander Larsson1f206b42013-05-22 14:41:36 +0200259 pixman_image_composite32(pixman_op,
260 ps->image, /* src */
Manuel Bachmannb1fff412014-04-05 05:31:37 +0200261 mask_image, /* mask */
Alexander Larsson1f206b42013-05-22 14:41:36 +0200262 po->shadow_image, /* dest */
263 0, 0, /* src_x, src_y */
264 0, 0, /* mask_x, mask_y */
265 0, 0, /* dest_x, dest_y */
266 pixman_image_get_width (po->shadow_image), /* width */
267 pixman_image_get_height (po->shadow_image) /* height */);
268
Manuel Bachmann9c4ab662014-04-07 11:58:45 +0200269 if (mask_image)
270 pixman_image_unref(mask_image);
271
Neil Robertse5051712013-11-13 15:44:06 +0000272 if (ps->buffer_ref.buffer)
273 wl_shm_buffer_end_access(ps->buffer_ref.buffer->shm_buffer);
274
Alexander Larsson1f206b42013-05-22 14:41:36 +0200275 if (pr->repaint_debug)
Ander Conselvan de Oliveira8792ef12013-01-25 15:13:00 +0200276 pixman_image_composite32(PIXMAN_OP_OVER,
Alexander Larsson1f206b42013-05-22 14:41:36 +0200277 pr->debug_color, /* src */
278 NULL /* mask */,
279 po->shadow_image, /* dest */
280 0, 0, /* src_x, src_y */
281 0, 0, /* mask_x, mask_y */
282 0, 0, /* dest_x, dest_y */
283 pixman_image_get_width (po->shadow_image), /* width */
284 pixman_image_get_height (po->shadow_image) /* height */);
285
286 pixman_image_set_clip_region32 (po->shadow_image, NULL);
287
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +0300288 pixman_region32_fini(&final_region);
289}
290
291static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500292draw_view(struct weston_view *ev, struct weston_output *output,
293 pixman_region32_t *damage) /* in global coordinates */
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +0300294{
Derek Foreman66951b72014-09-01 10:33:28 -0500295 static int zoom_logged = 0;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500296 struct pixman_surface_state *ps = get_surface_state(ev->surface);
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +0300297 /* repaint bounding region in global coordinates: */
298 pixman_region32_t repaint;
299 /* non-opaque region in surface coordinates: */
300 pixman_region32_t surface_blend;
301
302 /* No buffer attached */
303 if (!ps->image)
304 return;
305
306 pixman_region32_init(&repaint);
307 pixman_region32_intersect(&repaint,
Pekka Paalanen25c0ca52015-02-19 11:15:33 +0200308 &ev->transform.boundingbox, damage);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500309 pixman_region32_subtract(&repaint, &repaint, &ev->clip);
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +0300310
311 if (!pixman_region32_not_empty(&repaint))
312 goto out;
313
Derek Foreman66951b72014-09-01 10:33:28 -0500314 if (output->zoom.active && !zoom_logged) {
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +0300315 weston_log("pixman renderer does not support zoom\n");
Derek Foreman66951b72014-09-01 10:33:28 -0500316 zoom_logged = 1;
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +0300317 }
318
Vasily Khoruzhickf4a457f2013-01-28 22:40:29 +0300319 /* TODO: Implement repaint_region_complex() using pixman_composite_trapezoids() */
Pekka Paalanen23d4af52015-03-04 16:18:26 +0200320 if (ev->alpha != 1.0 || !view_transformation_is_translation(ev)) {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500321 repaint_region(ev, output, &repaint, NULL, PIXMAN_OP_OVER);
Vasily Khoruzhickf4a457f2013-01-28 22:40:29 +0300322 } else {
323 /* blended region is whole surface minus opaque region: */
324 pixman_region32_init_rect(&surface_blend, 0, 0,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600325 ev->surface->width, ev->surface->height);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500326 pixman_region32_subtract(&surface_blend, &surface_blend, &ev->surface->opaque);
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +0300327
Jason Ekstranda7af7042013-10-12 22:38:11 -0500328 if (pixman_region32_not_empty(&ev->surface->opaque)) {
329 repaint_region(ev, output, &repaint, &ev->surface->opaque, PIXMAN_OP_SRC);
Vasily Khoruzhickf4a457f2013-01-28 22:40:29 +0300330 }
331
332 if (pixman_region32_not_empty(&surface_blend)) {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500333 repaint_region(ev, output, &repaint, &surface_blend, PIXMAN_OP_OVER);
Vasily Khoruzhickf4a457f2013-01-28 22:40:29 +0300334 }
335 pixman_region32_fini(&surface_blend);
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +0300336 }
337
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +0300338
339out:
340 pixman_region32_fini(&repaint);
341}
342static void
343repaint_surfaces(struct weston_output *output, pixman_region32_t *damage)
344{
345 struct weston_compositor *compositor = output->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500346 struct weston_view *view;
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +0300347
Jason Ekstranda7af7042013-10-12 22:38:11 -0500348 wl_list_for_each_reverse(view, &compositor->view_list, link)
349 if (view->plane == &compositor->primary_plane)
350 draw_view(view, output, damage);
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +0300351}
352
353static void
Ander Conselvan de Oliveira936effd2013-01-25 15:13:01 +0200354copy_to_hw_buffer(struct weston_output *output, pixman_region32_t *region)
355{
356 struct pixman_output_state *po = get_output_state(output);
Alexander Larsson1f206b42013-05-22 14:41:36 +0200357 pixman_region32_t output_region;
Ander Conselvan de Oliveira936effd2013-01-25 15:13:01 +0200358
Alexander Larsson1f206b42013-05-22 14:41:36 +0200359 pixman_region32_init(&output_region);
360 pixman_region32_copy(&output_region, region);
Ander Conselvan de Oliveira936effd2013-01-25 15:13:01 +0200361
Alexander Larsson1f206b42013-05-22 14:41:36 +0200362 region_global_to_output(output, &output_region);
Ander Conselvan de Oliveira936effd2013-01-25 15:13:01 +0200363
Alexander Larsson1f206b42013-05-22 14:41:36 +0200364 pixman_image_set_clip_region32 (po->hw_buffer, &output_region);
Ryo Munakata389d2052014-09-04 01:56:53 +0900365 pixman_region32_fini(&output_region);
Ander Conselvan de Oliveira936effd2013-01-25 15:13:01 +0200366
Alexander Larsson1f206b42013-05-22 14:41:36 +0200367 pixman_image_composite32(PIXMAN_OP_SRC,
368 po->shadow_image, /* src */
369 NULL /* mask */,
370 po->hw_buffer, /* dest */
371 0, 0, /* src_x, src_y */
372 0, 0, /* mask_x, mask_y */
373 0, 0, /* dest_x, dest_y */
374 pixman_image_get_width (po->hw_buffer), /* width */
375 pixman_image_get_height (po->hw_buffer) /* height */);
376
377 pixman_image_set_clip_region32 (po->hw_buffer, NULL);
Ander Conselvan de Oliveira936effd2013-01-25 15:13:01 +0200378}
379
380static void
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +0300381pixman_renderer_repaint_output(struct weston_output *output,
382 pixman_region32_t *output_damage)
383{
384 struct pixman_output_state *po = get_output_state(output);
385
386 if (!po->hw_buffer)
387 return;
388
389 repaint_surfaces(output, output_damage);
Ander Conselvan de Oliveira936effd2013-01-25 15:13:01 +0200390 copy_to_hw_buffer(output, output_damage);
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +0300391
392 pixman_region32_copy(&output->previous_damage, output_damage);
393 wl_signal_emit(&output->frame_signal, output);
394
395 /* Actual flip should be done by caller */
396}
397
398static void
399pixman_renderer_flush_damage(struct weston_surface *surface)
400{
401 /* No-op for pixman renderer */
402}
403
404static void
Lubomir Rintelddc2b1e2013-12-12 12:57:56 +0100405buffer_state_handle_buffer_destroy(struct wl_listener *listener, void *data)
406{
407 struct pixman_surface_state *ps;
408
409 ps = container_of(listener, struct pixman_surface_state,
410 buffer_destroy_listener);
411
412 if (ps->image) {
413 pixman_image_unref(ps->image);
414 ps->image = NULL;
415 }
416
417 ps->buffer_destroy_listener.notify = NULL;
418}
419
420static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -0500421pixman_renderer_attach(struct weston_surface *es, struct weston_buffer *buffer)
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +0300422{
423 struct pixman_surface_state *ps = get_surface_state(es);
Jason Ekstrand6bd62942013-06-20 20:38:23 -0500424 struct wl_shm_buffer *shm_buffer;
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +0300425 pixman_format_code_t pixman_format;
426
427 weston_buffer_reference(&ps->buffer_ref, buffer);
428
Lubomir Rintelddc2b1e2013-12-12 12:57:56 +0100429 if (ps->buffer_destroy_listener.notify) {
430 wl_list_remove(&ps->buffer_destroy_listener.link);
431 ps->buffer_destroy_listener.notify = NULL;
432 }
433
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +0300434 if (ps->image) {
435 pixman_image_unref(ps->image);
436 ps->image = NULL;
437 }
438
439 if (!buffer)
440 return;
Jason Ekstrand6bd62942013-06-20 20:38:23 -0500441
442 shm_buffer = wl_shm_buffer_get(buffer->resource);
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +0300443
Jason Ekstrand6bd62942013-06-20 20:38:23 -0500444 if (! shm_buffer) {
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +0300445 weston_log("Pixman renderer supports only SHM buffers\n");
446 weston_buffer_reference(&ps->buffer_ref, NULL);
447 return;
448 }
449
Jason Ekstrand6bd62942013-06-20 20:38:23 -0500450 switch (wl_shm_buffer_get_format(shm_buffer)) {
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +0300451 case WL_SHM_FORMAT_XRGB8888:
452 pixman_format = PIXMAN_x8r8g8b8;
453 break;
454 case WL_SHM_FORMAT_ARGB8888:
455 pixman_format = PIXMAN_a8r8g8b8;
456 break;
Tomeu Vizoso1c1fc292013-08-06 20:05:56 +0200457 case WL_SHM_FORMAT_RGB565:
458 pixman_format = PIXMAN_r5g6b5;
459 break;
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +0300460 default:
461 weston_log("Unsupported SHM buffer format\n");
462 weston_buffer_reference(&ps->buffer_ref, NULL);
463 return;
464 break;
465 }
Jason Ekstrand6bd62942013-06-20 20:38:23 -0500466
467 buffer->shm_buffer = shm_buffer;
468 buffer->width = wl_shm_buffer_get_width(shm_buffer);
469 buffer->height = wl_shm_buffer_get_height(shm_buffer);
470
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +0300471 ps->image = pixman_image_create_bits(pixman_format,
Jason Ekstrand6bd62942013-06-20 20:38:23 -0500472 buffer->width, buffer->height,
473 wl_shm_buffer_get_data(shm_buffer),
474 wl_shm_buffer_get_stride(shm_buffer));
Lubomir Rintelddc2b1e2013-12-12 12:57:56 +0100475
476 ps->buffer_destroy_listener.notify =
477 buffer_state_handle_buffer_destroy;
478 wl_signal_add(&buffer->destroy_signal,
479 &ps->buffer_destroy_listener);
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +0300480}
481
Ander Conselvan de Oliveiraaa398ae2013-10-25 16:26:33 +0300482static void
Ander Conselvan de Oliveiraadda00e2013-10-25 16:26:34 +0300483pixman_renderer_surface_state_destroy(struct pixman_surface_state *ps)
Ander Conselvan de Oliveiraaa398ae2013-10-25 16:26:33 +0300484{
Ander Conselvan de Oliveiraadda00e2013-10-25 16:26:34 +0300485 wl_list_remove(&ps->surface_destroy_listener.link);
486 wl_list_remove(&ps->renderer_destroy_listener.link);
Lubomir Rintelddc2b1e2013-12-12 12:57:56 +0100487 if (ps->buffer_destroy_listener.notify) {
488 wl_list_remove(&ps->buffer_destroy_listener.link);
489 ps->buffer_destroy_listener.notify = NULL;
490 }
Ander Conselvan de Oliveiraaa398ae2013-10-25 16:26:33 +0300491
492 ps->surface->renderer_state = NULL;
493
494 if (ps->image) {
495 pixman_image_unref(ps->image);
496 ps->image = NULL;
497 }
498 weston_buffer_reference(&ps->buffer_ref, NULL);
499 free(ps);
500}
501
Ander Conselvan de Oliveiraadda00e2013-10-25 16:26:34 +0300502static void
503surface_state_handle_surface_destroy(struct wl_listener *listener, void *data)
504{
505 struct pixman_surface_state *ps;
506
507 ps = container_of(listener, struct pixman_surface_state,
508 surface_destroy_listener);
509
510 pixman_renderer_surface_state_destroy(ps);
511}
512
513static void
514surface_state_handle_renderer_destroy(struct wl_listener *listener, void *data)
515{
516 struct pixman_surface_state *ps;
517
518 ps = container_of(listener, struct pixman_surface_state,
519 renderer_destroy_listener);
520
521 pixman_renderer_surface_state_destroy(ps);
522}
523
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +0300524static int
525pixman_renderer_create_surface(struct weston_surface *surface)
526{
527 struct pixman_surface_state *ps;
Ander Conselvan de Oliveiraadda00e2013-10-25 16:26:34 +0300528 struct pixman_renderer *pr = get_renderer(surface->compositor);
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +0300529
Bryce Harringtonde16d892014-11-20 22:21:57 -0800530 ps = zalloc(sizeof *ps);
531 if (ps == NULL)
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +0300532 return -1;
533
534 surface->renderer_state = ps;
535
Ander Conselvan de Oliveiraaa398ae2013-10-25 16:26:33 +0300536 ps->surface = surface;
537
538 ps->surface_destroy_listener.notify =
539 surface_state_handle_surface_destroy;
540 wl_signal_add(&surface->destroy_signal,
541 &ps->surface_destroy_listener);
542
Ander Conselvan de Oliveiraadda00e2013-10-25 16:26:34 +0300543 ps->renderer_destroy_listener.notify =
544 surface_state_handle_renderer_destroy;
545 wl_signal_add(&pr->destroy_signal,
546 &ps->renderer_destroy_listener);
547
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +0300548 return 0;
549}
550
551static void
552pixman_renderer_surface_set_color(struct weston_surface *es,
553 float red, float green, float blue, float alpha)
554{
555 struct pixman_surface_state *ps = get_surface_state(es);
556 pixman_color_t color;
557
558 color.red = red * 0xffff;
559 color.green = green * 0xffff;
560 color.blue = blue * 0xffff;
561 color.alpha = alpha * 0xffff;
562
563 if (ps->image) {
564 pixman_image_unref(ps->image);
565 ps->image = NULL;
566 }
567
568 ps->image = pixman_image_create_solid_fill(&color);
569}
570
571static void
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +0300572pixman_renderer_destroy(struct weston_compositor *ec)
573{
Ander Conselvan de Oliveira6b162142013-10-25 16:26:32 +0300574 struct pixman_renderer *pr = get_renderer(ec);
575
Ander Conselvan de Oliveiraadda00e2013-10-25 16:26:34 +0300576 wl_signal_emit(&pr->destroy_signal, pr);
Ander Conselvan de Oliveira6b162142013-10-25 16:26:32 +0300577 weston_binding_destroy(pr->debug_binding);
578 free(pr);
579
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +0300580 ec->renderer = NULL;
581}
582
Ander Conselvan de Oliveira8792ef12013-01-25 15:13:00 +0200583static void
Pekka Paalanen6764bbe2015-02-11 12:29:56 +0200584pixman_renderer_surface_get_content_size(struct weston_surface *surface,
585 int *width, int *height)
586{
587 struct pixman_surface_state *ps = get_surface_state(surface);
588
589 if (ps->image) {
590 *width = pixman_image_get_width(ps->image);
591 *height = pixman_image_get_height(ps->image);
592 } else {
593 *width = 0;
594 *height = 0;
595 }
596}
597
598static int
599pixman_renderer_surface_copy_content(struct weston_surface *surface,
600 void *target, size_t size,
601 int src_x, int src_y,
602 int width, int height)
603{
604 const pixman_format_code_t format = PIXMAN_a8b8g8r8;
605 const size_t bytespp = 4; /* PIXMAN_a8b8g8r8 */
606 struct pixman_surface_state *ps = get_surface_state(surface);
607 pixman_image_t *out_buf;
608
609 if (!ps->image)
610 return -1;
611
612 out_buf = pixman_image_create_bits(format, width, height,
613 target, width * bytespp);
614
615 pixman_image_set_transform(ps->image, NULL);
616 pixman_image_composite32(PIXMAN_OP_SRC,
617 ps->image, /* src */
618 NULL, /* mask */
619 out_buf, /* dest */
620 src_x, src_y, /* src_x, src_y */
621 0, 0, /* mask_x, mask_y */
622 0, 0, /* dest_x, dest_y */
623 width, height);
624
625 pixman_image_unref(out_buf);
626
627 return 0;
628}
629
630static void
Kristian Høgsberge3148752013-05-06 23:19:49 -0400631debug_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Ander Conselvan de Oliveira8792ef12013-01-25 15:13:00 +0200632 void *data)
633{
634 struct weston_compositor *ec = data;
635 struct pixman_renderer *pr = (struct pixman_renderer *) ec->renderer;
636
637 pr->repaint_debug ^= 1;
638
639 if (pr->repaint_debug) {
640 pixman_color_t red = {
641 0x3fff, 0x0000, 0x0000, 0x3fff
642 };
643
644 pr->debug_color = pixman_image_create_solid_fill(&red);
645 } else {
646 pixman_image_unref(pr->debug_color);
647 weston_compositor_damage_all(ec);
648 }
649}
650
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +0300651WL_EXPORT int
652pixman_renderer_init(struct weston_compositor *ec)
653{
Ander Conselvan de Oliveira8792ef12013-01-25 15:13:00 +0200654 struct pixman_renderer *renderer;
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +0300655
Bryce Harringtonde16d892014-11-20 22:21:57 -0800656 renderer = zalloc(sizeof *renderer);
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +0300657 if (renderer == NULL)
658 return -1;
659
Philipp Brüschweilerf3e39f92013-03-08 20:35:39 +0100660 renderer->repaint_debug = 0;
661 renderer->debug_color = NULL;
Ander Conselvan de Oliveira8792ef12013-01-25 15:13:00 +0200662 renderer->base.read_pixels = pixman_renderer_read_pixels;
663 renderer->base.repaint_output = pixman_renderer_repaint_output;
664 renderer->base.flush_damage = pixman_renderer_flush_damage;
665 renderer->base.attach = pixman_renderer_attach;
Ander Conselvan de Oliveira8792ef12013-01-25 15:13:00 +0200666 renderer->base.surface_set_color = pixman_renderer_surface_set_color;
Ander Conselvan de Oliveira8792ef12013-01-25 15:13:00 +0200667 renderer->base.destroy = pixman_renderer_destroy;
Pekka Paalanen6764bbe2015-02-11 12:29:56 +0200668 renderer->base.surface_get_content_size =
669 pixman_renderer_surface_get_content_size;
670 renderer->base.surface_copy_content =
671 pixman_renderer_surface_copy_content;
Ander Conselvan de Oliveira8792ef12013-01-25 15:13:00 +0200672 ec->renderer = &renderer->base;
Pekka Paalanen7bb65102013-05-22 18:03:04 +0300673 ec->capabilities |= WESTON_CAP_ROTATION_ANY;
Pekka Paalanen4fc5dd02013-05-22 18:03:05 +0300674 ec->capabilities |= WESTON_CAP_CAPTURE_YFLIP;
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +0300675
Ander Conselvan de Oliveira6b162142013-10-25 16:26:32 +0300676 renderer->debug_binding =
677 weston_compositor_add_debug_binding(ec, KEY_R,
678 debug_binding, ec);
Tomeu Vizoso1c1fc292013-08-06 20:05:56 +0200679
680 wl_display_add_shm_format(ec->wl_display, WL_SHM_FORMAT_RGB565);
681
Ander Conselvan de Oliveiraadda00e2013-10-25 16:26:34 +0300682 wl_signal_init(&renderer->destroy_signal);
683
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +0300684 return 0;
685}
686
687WL_EXPORT void
688pixman_renderer_output_set_buffer(struct weston_output *output, pixman_image_t *buffer)
689{
690 struct pixman_output_state *po = get_output_state(output);
691
692 if (po->hw_buffer)
693 pixman_image_unref(po->hw_buffer);
694 po->hw_buffer = buffer;
695
696 if (po->hw_buffer) {
697 output->compositor->read_format = pixman_image_get_format(po->hw_buffer);
698 pixman_image_ref(po->hw_buffer);
699 }
700}
701
702WL_EXPORT int
703pixman_renderer_output_create(struct weston_output *output)
704{
Bryce Harringtonde16d892014-11-20 22:21:57 -0800705 struct pixman_output_state *po;
Ander Conselvan de Oliveira936effd2013-01-25 15:13:01 +0200706 int w, h;
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +0300707
Bryce Harringtonde16d892014-11-20 22:21:57 -0800708 po = zalloc(sizeof *po);
709 if (po == NULL)
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +0300710 return -1;
Ander Conselvan de Oliveira936effd2013-01-25 15:13:01 +0200711
712 /* set shadow image transformation */
Hardeningff39efa2013-09-18 23:56:35 +0200713 w = output->current_mode->width;
714 h = output->current_mode->height;
Ander Conselvan de Oliveira936effd2013-01-25 15:13:01 +0200715
Ander Conselvan de Oliveira936effd2013-01-25 15:13:01 +0200716 po->shadow_buffer = malloc(w * h * 4);
717
718 if (!po->shadow_buffer) {
719 free(po);
720 return -1;
721 }
722
723 po->shadow_image =
724 pixman_image_create_bits(PIXMAN_x8r8g8b8, w, h,
725 po->shadow_buffer, w * 4);
726
727 if (!po->shadow_image) {
728 free(po->shadow_buffer);
729 free(po);
730 return -1;
731 }
732
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +0300733 output->renderer_state = po;
734
735 return 0;
736}
737
738WL_EXPORT void
739pixman_renderer_output_destroy(struct weston_output *output)
740{
741 struct pixman_output_state *po = get_output_state(output);
742
Ander Conselvan de Oliveira936effd2013-01-25 15:13:01 +0200743 pixman_image_unref(po->shadow_image);
Ander Conselvan de Oliveira23e72b82013-01-25 15:13:06 +0200744
745 if (po->hw_buffer)
746 pixman_image_unref(po->hw_buffer);
Ander Conselvan de Oliveira936effd2013-01-25 15:13:01 +0200747
Arnaud Vrac8e3fe082014-08-25 20:56:52 +0200748 free(po->shadow_buffer);
749
750 po->shadow_buffer = NULL;
Ander Conselvan de Oliveira936effd2013-01-25 15:13:01 +0200751 po->shadow_image = NULL;
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +0300752 po->hw_buffer = NULL;
753
754 free(po);
755}