2 * Copyright © 2012 Intel Corporation
3 * Copyright © 2013 Vasily Khoruzhick <anarsoul@gmail.com>
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.
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.
29 #include "pixman-renderer.h"
31 #include <linux/input.h>
33 struct pixman_output_state {
35 pixman_image_t *shadow_image;
36 pixman_image_t *hw_buffer;
39 struct pixman_surface_state {
40 pixman_image_t *image;
41 struct weston_buffer_reference buffer_ref;
44 struct pixman_renderer {
45 struct weston_renderer base;
47 pixman_image_t *debug_color;
50 static inline struct pixman_output_state *
51 get_output_state(struct weston_output *output)
53 return (struct pixman_output_state *)output->renderer_state;
56 static inline struct pixman_surface_state *
57 get_surface_state(struct weston_surface *surface)
59 return (struct pixman_surface_state *)surface->renderer_state;
62 static inline struct pixman_renderer *
63 get_renderer(struct weston_compositor *ec)
65 return (struct pixman_renderer *)ec->renderer;
69 pixman_renderer_read_pixels(struct weston_output *output,
70 pixman_format_code_t format, void *pixels,
71 uint32_t x, uint32_t y,
72 uint32_t width, uint32_t height)
74 struct pixman_output_state *po = get_output_state(output);
75 pixman_image_t *out_buf;
83 out_buf = pixman_image_create_bits(format,
87 (PIXMAN_FORMAT_BPP(format) / 8) * width);
89 /* Caller expects vflipped image */
90 for (cur_y = y; cur_y < y + height; cur_y++) {
91 pixman_image_composite32(PIXMAN_OP_SRC,
92 po->hw_buffer, /* src */
95 x, cur_y, /* src_x, src_y */
96 0, 0, /* mask_x, mask_y */
97 0, height - (cur_y - y), /* dest_x, dest_y */
102 pixman_image_unref(out_buf);
108 box_translate(pixman_box32_t *dst, const pixman_box32_t *src, int x, int y)
110 dst->x1 = src->x1 + x;
111 dst->x2 = src->x2 + x;
112 dst->y1 = src->y1 + y;
113 dst->y2 = src->y2 + y;
116 #define D2F(v) pixman_double_to_fixed((double)v)
119 repaint_region_complex(struct weston_surface *es, struct weston_output *output,
120 pixman_region32_t *region)
122 struct pixman_renderer *pr =
123 (struct pixman_renderer *) output->compositor->renderer;
124 struct pixman_surface_state *ps = get_surface_state(es);
125 struct pixman_output_state *po = get_output_state(output);
127 pixman_box32_t *rects, rect;
129 /* Pixman supports only 2D transform matrix, but Weston uses 3D,
130 * so we're omitting Z coordinate here
132 pixman_transform_t transform = {{
133 { D2F(es->transform.matrix.d[0]),
134 D2F(es->transform.matrix.d[4]),
135 D2F(es->transform.matrix.d[12]),
137 { D2F(es->transform.matrix.d[1]),
138 D2F(es->transform.matrix.d[5]),
139 D2F(es->transform.matrix.d[13]),
141 { D2F(es->transform.matrix.d[3]),
142 D2F(es->transform.matrix.d[7]),
143 D2F(es->transform.matrix.d[15]),
147 pixman_transform_invert(&transform, &transform);
149 pixman_image_set_transform(ps->image, &transform);
150 pixman_image_set_filter(ps->image, PIXMAN_FILTER_BILINEAR, NULL, 0);
152 rects = pixman_region32_rectangles(region, &nrects);
153 for (i = 0; i < nrects; i++) {
154 box_translate(&rect, &rects[i], -output->x, -output->y);
155 pixman_image_composite32(PIXMAN_OP_OVER,
158 po->shadow_image, /* dest */
159 rects[i].x1, rects[i].y1, /* src_x, src_y */
160 0, 0, /* mask_x, mask_y */
161 rect.x1, rect.y1, /* dst_x, dst_y */
162 rect.x2 - rect.x1, /* width */
163 rect.y2 - rect.y1 /* height */
166 if (!pr->repaint_debug)
169 pixman_image_composite32(PIXMAN_OP_OVER,
170 pr->debug_color, /* src */
172 po->shadow_image, /* dest */
173 0, 0, /* src_x, src_y */
174 0, 0, /* mask_x, mask_y */
175 rect.x1, rect.y1, /* dest_x, dest_y */
176 rect.x2 - rect.x1, /* width */
177 rect.y2 - rect.y1 /* height */);
182 repaint_region_simple(struct weston_surface *es, struct weston_output *output,
183 pixman_region32_t *region, pixman_region32_t *surf_region,
184 pixman_op_t pixman_op)
186 struct pixman_renderer *pr =
187 (struct pixman_renderer *) output->compositor->renderer;
188 struct pixman_surface_state *ps = get_surface_state(es);
189 struct pixman_output_state *po = get_output_state(output);
190 pixman_region32_t final_region;
191 pixman_box32_t *rects, rect;
192 int nrects, i, src_x, src_y;
193 float surface_x, surface_y;
195 /* The final region to be painted is the intersection of
196 * 'region' and 'surf_region'. However, 'region' is in the global
197 * coordinates, and 'surf_region' is in the surface-local
200 pixman_region32_init(&final_region);
201 pixman_region32_copy(&final_region, surf_region);
203 pixman_image_set_filter(ps->image, PIXMAN_FILTER_NEAREST, NULL, 0);
204 pixman_image_set_transform(ps->image, NULL);
206 if (!es->transform.enabled) {
207 pixman_region32_translate(&final_region, es->geometry.x, es->geometry.y);
209 weston_surface_to_global_float(es, 0, 0, &surface_x, &surface_y);
210 pixman_region32_translate(&final_region, (int)surface_x, (int)surface_y);
213 /* That's what we need to paint */
214 pixman_region32_intersect(&final_region, &final_region, region);
215 rects = pixman_region32_rectangles(&final_region, &nrects);
217 for (i = 0; i < nrects; i++) {
218 weston_surface_from_global(es, rects[i].x1, rects[i].y1, &src_x, &src_y);
219 box_translate(&rect, &rects[i], -output->x, -output->y);
220 pixman_image_composite32(pixman_op,
223 po->shadow_image, /* dest */
224 src_x, src_y, /* src_x, src_y */
225 0, 0, /* mask_x, mask_y */
226 rect.x1, rect.y1, /* dest_x, dest_y */
227 rect.x2 - rect.x1, /* width */
228 rect.y2 - rect.y1 /* height */);
230 if (!pr->repaint_debug)
233 pixman_image_composite32(PIXMAN_OP_OVER,
234 pr->debug_color, /* src */
236 po->shadow_image, /* dest */
237 src_x, src_y, /* src_x, src_y */
238 0, 0, /* mask_x, mask_y */
239 rect.x1, rect.y1, /* dest_x, dest_y */
240 rect.x2 - rect.x1, /* width */
241 rect.y2 - rect.y1 /* height */);
243 pixman_region32_fini(&final_region);
247 draw_surface(struct weston_surface *es, struct weston_output *output,
248 pixman_region32_t *damage) /* in global coordinates */
250 struct pixman_surface_state *ps = get_surface_state(es);
251 /* repaint bounding region in global coordinates: */
252 pixman_region32_t repaint;
253 /* non-opaque region in surface coordinates: */
254 pixman_region32_t surface_blend;
256 /* No buffer attached */
260 pixman_region32_init(&repaint);
261 pixman_region32_intersect(&repaint,
262 &es->transform.boundingbox, damage);
263 pixman_region32_subtract(&repaint, &repaint, &es->clip);
265 if (!pixman_region32_not_empty(&repaint))
268 if (output->zoom.active) {
269 weston_log("pixman renderer does not support zoom\n");
273 /* TODO: Implement repaint_region_complex() using pixman_composite_trapezoids() */
274 if (es->transform.enabled &&
275 es->transform.matrix.type != WESTON_MATRIX_TRANSFORM_TRANSLATE) {
276 repaint_region_complex(es, output, &repaint);
278 /* blended region is whole surface minus opaque region: */
279 pixman_region32_init_rect(&surface_blend, 0, 0,
280 es->geometry.width, es->geometry.height);
281 pixman_region32_subtract(&surface_blend, &surface_blend, &es->opaque);
283 if (pixman_region32_not_empty(&es->opaque)) {
284 repaint_region_simple(es, output, &repaint, &es->opaque, PIXMAN_OP_SRC);
287 if (pixman_region32_not_empty(&surface_blend)) {
288 repaint_region_simple(es, output, &repaint, &surface_blend, PIXMAN_OP_OVER);
290 pixman_region32_fini(&surface_blend);
295 pixman_region32_fini(&repaint);
298 repaint_surfaces(struct weston_output *output, pixman_region32_t *damage)
300 struct weston_compositor *compositor = output->compositor;
301 struct weston_surface *surface;
303 wl_list_for_each_reverse(surface, &compositor->surface_list, link)
304 if (surface->plane == &compositor->primary_plane)
305 draw_surface(surface, output, damage);
309 copy_to_hw_buffer(struct weston_output *output, pixman_region32_t *region)
311 struct pixman_output_state *po = get_output_state(output);
312 int nrects, i, width, height;
313 pixman_box32_t *rects;
314 pixman_box32_t b, rect;
316 width = pixman_image_get_width(po->shadow_image);
317 height = pixman_image_get_height(po->shadow_image);
319 rects = pixman_region32_rectangles(region, &nrects);
320 for (i = 0; i < nrects; i++) {
321 box_translate(&rect, &rects[i], -output->x, -output->y);
322 b = weston_transformed_rect(width, height,
323 output->transform, rect);
325 pixman_image_composite32(PIXMAN_OP_SRC,
326 po->shadow_image, /* src */
328 po->hw_buffer, /* dest */
329 b.x1, b.y1, /* src_x, src_y */
330 0, 0, /* mask_x, mask_y */
331 b.x1, b.y1, /* dest_x, dest_y */
332 b.x2 - b.x1, /* width */
333 b.y2 - b.y1 /* height */);
339 pixman_renderer_repaint_output(struct weston_output *output,
340 pixman_region32_t *output_damage)
342 struct pixman_output_state *po = get_output_state(output);
347 repaint_surfaces(output, output_damage);
348 copy_to_hw_buffer(output, output_damage);
350 pixman_region32_copy(&output->previous_damage, output_damage);
351 wl_signal_emit(&output->frame_signal, output);
353 /* Actual flip should be done by caller */
357 pixman_renderer_flush_damage(struct weston_surface *surface)
359 /* No-op for pixman renderer */
363 pixman_renderer_attach(struct weston_surface *es, struct wl_buffer *buffer)
365 struct pixman_surface_state *ps = get_surface_state(es);
366 pixman_format_code_t pixman_format;
368 weston_buffer_reference(&ps->buffer_ref, buffer);
371 pixman_image_unref(ps->image);
378 if (!wl_buffer_is_shm(buffer)) {
379 weston_log("Pixman renderer supports only SHM buffers\n");
380 weston_buffer_reference(&ps->buffer_ref, NULL);
384 switch (wl_shm_buffer_get_format(buffer)) {
385 case WL_SHM_FORMAT_XRGB8888:
386 pixman_format = PIXMAN_x8r8g8b8;
388 case WL_SHM_FORMAT_ARGB8888:
389 pixman_format = PIXMAN_a8r8g8b8;
392 weston_log("Unsupported SHM buffer format\n");
393 weston_buffer_reference(&ps->buffer_ref, NULL);
397 ps->image = pixman_image_create_bits(pixman_format,
398 wl_shm_buffer_get_width(buffer),
399 wl_shm_buffer_get_height(buffer),
400 wl_shm_buffer_get_data(buffer),
401 wl_shm_buffer_get_stride(buffer));
405 pixman_renderer_create_surface(struct weston_surface *surface)
407 struct pixman_surface_state *ps;
409 ps = calloc(1, sizeof *ps);
413 surface->renderer_state = ps;
419 pixman_renderer_surface_set_color(struct weston_surface *es,
420 float red, float green, float blue, float alpha)
422 struct pixman_surface_state *ps = get_surface_state(es);
423 pixman_color_t color;
425 color.red = red * 0xffff;
426 color.green = green * 0xffff;
427 color.blue = blue * 0xffff;
428 color.alpha = alpha * 0xffff;
431 pixman_image_unref(ps->image);
435 ps->image = pixman_image_create_solid_fill(&color);
439 pixman_renderer_destroy_surface(struct weston_surface *surface)
441 struct pixman_surface_state *ps = get_surface_state(surface);
444 pixman_image_unref(ps->image);
447 weston_buffer_reference(&ps->buffer_ref, NULL);
452 pixman_renderer_destroy(struct weston_compositor *ec)
459 debug_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
462 struct weston_compositor *ec = data;
463 struct pixman_renderer *pr = (struct pixman_renderer *) ec->renderer;
465 pr->repaint_debug ^= 1;
467 if (pr->repaint_debug) {
468 pixman_color_t red = {
469 0x3fff, 0x0000, 0x0000, 0x3fff
472 pr->debug_color = pixman_image_create_solid_fill(&red);
474 pixman_image_unref(pr->debug_color);
475 weston_compositor_damage_all(ec);
480 pixman_renderer_init(struct weston_compositor *ec)
482 struct pixman_renderer *renderer;
484 renderer = malloc(sizeof *renderer);
485 if (renderer == NULL)
488 renderer->repaint_debug = 0;
489 renderer->debug_color = NULL;
490 renderer->base.read_pixels = pixman_renderer_read_pixels;
491 renderer->base.repaint_output = pixman_renderer_repaint_output;
492 renderer->base.flush_damage = pixman_renderer_flush_damage;
493 renderer->base.attach = pixman_renderer_attach;
494 renderer->base.create_surface = pixman_renderer_create_surface;
495 renderer->base.surface_set_color = pixman_renderer_surface_set_color;
496 renderer->base.destroy_surface = pixman_renderer_destroy_surface;
497 renderer->base.destroy = pixman_renderer_destroy;
498 ec->renderer = &renderer->base;
500 weston_compositor_add_debug_binding(ec, KEY_R,
506 pixman_renderer_output_set_buffer(struct weston_output *output, pixman_image_t *buffer)
508 struct pixman_output_state *po = get_output_state(output);
511 pixman_image_unref(po->hw_buffer);
512 po->hw_buffer = buffer;
515 output->compositor->read_format = pixman_image_get_format(po->hw_buffer);
516 pixman_image_ref(po->hw_buffer);
521 pixman_renderer_output_create(struct weston_output *output)
523 struct pixman_output_state *po = calloc(1, sizeof *po);
524 pixman_transform_t transform;
525 pixman_fixed_t fw, fh;
532 /* set shadow image transformation */
533 w = output->current->width;
534 h = output->current->height;
536 fw = pixman_int_to_fixed(w);
537 fh = pixman_int_to_fixed(h);
539 pixman_transform_init_identity(&transform);
541 switch (output->transform) {
543 case WL_OUTPUT_TRANSFORM_NORMAL:
545 case WL_OUTPUT_TRANSFORM_180:
546 pixman_transform_rotate(&transform, NULL, -pixman_fixed_1, 0);
547 pixman_transform_translate(NULL, &transform, fw, fh);
549 case WL_OUTPUT_TRANSFORM_270:
551 pixman_transform_rotate(&transform, NULL, 0, pixman_fixed_1);
552 pixman_transform_translate(&transform, NULL, fh, 0);
554 case WL_OUTPUT_TRANSFORM_90:
556 pixman_transform_rotate(&transform, NULL, 0, -pixman_fixed_1);
557 pixman_transform_translate(&transform, NULL, 0, fw);
567 po->shadow_buffer = malloc(w * h * 4);
569 if (!po->shadow_buffer) {
575 pixman_image_create_bits(PIXMAN_x8r8g8b8, w, h,
576 po->shadow_buffer, w * 4);
578 if (!po->shadow_image) {
579 free(po->shadow_buffer);
584 pixman_image_set_transform(po->shadow_image, &transform);
586 output->renderer_state = po;
592 pixman_renderer_output_destroy(struct weston_output *output)
594 struct pixman_output_state *po = get_output_state(output);
596 pixman_image_unref(po->shadow_image);
599 pixman_image_unref(po->hw_buffer);
601 po->shadow_image = NULL;
602 po->hw_buffer = NULL;