blob: 8d95197ea7316789cb59001caf94ae5fd71617e8 [file] [log] [blame]
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +03001/*
2 * Copyright © 2012 Intel Corporation
3 * Copyright © 2013 Vasily Khoruzhick <anarsoul@gmail.com>
4 *
5 * Permission to use, copy, modify, distribute, and sell this software and
6 * its documentation for any purpose is hereby granted without fee, provided
7 * that the above copyright notice appear in all copies and that both that
8 * copyright notice and this permission notice appear in supporting
9 * documentation, and that the name of the copyright holders not be used in
10 * advertising or publicity pertaining to distribution of the software
11 * without specific, written prior permission. The copyright holders make
12 * no representations about the suitability of this software for any
13 * purpose. It is provided "as is" without express or implied warranty.
14 *
15 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
16 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17 * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
18 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
19 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
20 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
21 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22 */
23
24#define _GNU_SOURCE
25
26#include <errno.h>
27#include <stdlib.h>
28
29#include "pixman-renderer.h"
30
Ander Conselvan de Oliveira8792ef12013-01-25 15:13:00 +020031#include <linux/input.h>
32
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +030033struct pixman_output_state {
Ander Conselvan de Oliveira936effd2013-01-25 15:13:01 +020034 void *shadow_buffer;
35 pixman_image_t *shadow_image;
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +030036 pixman_image_t *hw_buffer;
37};
38
39struct pixman_surface_state {
40 pixman_image_t *image;
41 struct weston_buffer_reference buffer_ref;
42};
43
44struct pixman_renderer {
45 struct weston_renderer base;
Ander Conselvan de Oliveira8792ef12013-01-25 15:13:00 +020046 int repaint_debug;
47 pixman_image_t *debug_color;
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +030048};
49
50static inline struct pixman_output_state *
51get_output_state(struct weston_output *output)
52{
53 return (struct pixman_output_state *)output->renderer_state;
54}
55
56static inline struct pixman_surface_state *
57get_surface_state(struct weston_surface *surface)
58{
59 return (struct pixman_surface_state *)surface->renderer_state;
60}
61
62static inline struct pixman_renderer *
63get_renderer(struct weston_compositor *ec)
64{
65 return (struct pixman_renderer *)ec->renderer;
66}
67
68static int
69pixman_renderer_read_pixels(struct weston_output *output,
70 pixman_format_code_t format, void *pixels,
71 uint32_t x, uint32_t y,
72 uint32_t width, uint32_t height)
73{
74 struct pixman_output_state *po = get_output_state(output);
75 pixman_image_t *out_buf;
76 uint32_t cur_y;
77
78 if (!po->hw_buffer) {
79 errno = ENODEV;
80 return -1;
81 }
82
83 out_buf = pixman_image_create_bits(format,
84 width,
85 height,
86 pixels,
87 (PIXMAN_FORMAT_BPP(format) / 8) * width);
88
89 /* Caller expects vflipped image */
90 for (cur_y = y; cur_y < y + height; cur_y++) {
91 pixman_image_composite32(PIXMAN_OP_SRC,
92 po->hw_buffer, /* src */
93 NULL /* mask */,
94 out_buf, /* dest */
95 x, cur_y, /* src_x, src_y */
96 0, 0, /* mask_x, mask_y */
97 0, height - (cur_y - y), /* dest_x, dest_y */
98 width, /* width */
99 1 /* height */);
100 }
101
102 pixman_image_unref(out_buf);
103
104 return 0;
105}
106
107static void
108repaint_region(struct weston_surface *es, struct weston_output *output,
109 pixman_region32_t *region, pixman_region32_t *surf_region,
110 pixman_op_t pixman_op)
111{
Ander Conselvan de Oliveira8792ef12013-01-25 15:13:00 +0200112 struct pixman_renderer *pr =
113 (struct pixman_renderer *) output->compositor->renderer;
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +0300114 struct pixman_surface_state *ps = get_surface_state(es);
115 struct pixman_output_state *po = get_output_state(output);
116 pixman_region32_t final_region;
117 pixman_box32_t *rects;
118 int nrects, i, src_x, src_y;
119 float surface_x, surface_y;
120
121 /* The final region to be painted is the intersection of
122 * 'region' and 'surf_region'. However, 'region' is in the global
123 * coordinates, and 'surf_region' is in the surface-local
124 * coordinates
125 */
126 pixman_region32_init(&final_region);
127 pixman_region32_copy(&final_region, surf_region);
128
129 if (es->transform.enabled) {
130 weston_surface_to_global_float(es, 0, 0, &surface_x, &surface_y);
131 pixman_region32_translate(&final_region, (int)surface_x, (int)surface_y);
132 } else
133 pixman_region32_translate(&final_region, es->geometry.x, es->geometry.y);
134
135 /* That's what we need to paint */
136 pixman_region32_intersect(&final_region, &final_region, region);
137
138 rects = pixman_region32_rectangles(&final_region, &nrects);
139
140 for (i = 0; i < nrects; i++) {
141 weston_surface_from_global(es, rects[i].x1, rects[i].y1, &src_x, &src_y);
142 pixman_image_composite32(pixman_op,
143 ps->image, /* src */
144 NULL /* mask */,
Ander Conselvan de Oliveira936effd2013-01-25 15:13:01 +0200145 po->shadow_image, /* dest */
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +0300146 src_x, src_y, /* src_x, src_y */
147 0, 0, /* mask_x, mask_y */
148 rects[i].x1, rects[i].y1, /* dest_x, dest_y */
149 rects[i].x2 - rects[i].x1, /* width */
150 rects[i].y2 - rects[i].y1 /* height */);
Ander Conselvan de Oliveira8792ef12013-01-25 15:13:00 +0200151
152 if (!pr->repaint_debug)
153 continue;
154
155 pixman_image_composite32(PIXMAN_OP_OVER,
156 pr->debug_color, /* src */
157 NULL /* mask */,
Ander Conselvan de Oliveira936effd2013-01-25 15:13:01 +0200158 po->shadow_image, /* dest */
Ander Conselvan de Oliveira8792ef12013-01-25 15:13:00 +0200159 src_x, src_y, /* src_x, src_y */
160 0, 0, /* mask_x, mask_y */
161 rects[i].x1, rects[i].y1, /* dest_x, dest_y */
162 rects[i].x2 - rects[i].x1, /* width */
163 rects[i].y2 - rects[i].y1 /* height */);
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +0300164 }
165 pixman_region32_fini(&final_region);
166}
167
168static void
169draw_surface(struct weston_surface *es, struct weston_output *output,
170 pixman_region32_t *damage) /* in global coordinates */
171{
172 struct pixman_surface_state *ps = get_surface_state(es);
173 /* repaint bounding region in global coordinates: */
174 pixman_region32_t repaint;
175 /* non-opaque region in surface coordinates: */
176 pixman_region32_t surface_blend;
177
178 /* No buffer attached */
179 if (!ps->image)
180 return;
181
182 pixman_region32_init(&repaint);
183 pixman_region32_intersect(&repaint,
184 &es->transform.boundingbox, damage);
185 pixman_region32_subtract(&repaint, &repaint, &es->clip);
186
187 if (!pixman_region32_not_empty(&repaint))
188 goto out;
189
190 if (output->zoom.active) {
191 weston_log("pixman renderer does not support zoom\n");
192 goto out;
193 }
194
195 /* blended region is whole surface minus opaque region: */
196 pixman_region32_init_rect(&surface_blend, 0, 0,
197 es->geometry.width, es->geometry.height);
198 pixman_region32_subtract(&surface_blend, &surface_blend, &es->opaque);
199
200 if (pixman_region32_not_empty(&es->opaque)) {
201 repaint_region(es, output, &repaint, &es->opaque, PIXMAN_OP_SRC);
202 }
203
204 if (pixman_region32_not_empty(&surface_blend)) {
205 repaint_region(es, output, &repaint, &surface_blend, PIXMAN_OP_OVER);
206 }
207
208 pixman_region32_fini(&surface_blend);
209
210out:
211 pixman_region32_fini(&repaint);
212}
213static void
214repaint_surfaces(struct weston_output *output, pixman_region32_t *damage)
215{
216 struct weston_compositor *compositor = output->compositor;
217 struct weston_surface *surface;
218
219 wl_list_for_each_reverse(surface, &compositor->surface_list, link)
220 if (surface->plane == &compositor->primary_plane)
221 draw_surface(surface, output, damage);
222}
223
224static void
Ander Conselvan de Oliveira936effd2013-01-25 15:13:01 +0200225copy_to_hw_buffer(struct weston_output *output, pixman_region32_t *region)
226{
227 struct pixman_output_state *po = get_output_state(output);
228 int nrects, i, width, height;
229 pixman_box32_t *rects;
230 pixman_box32_t b;
231
232 width = pixman_image_get_width(po->shadow_image);
233 height = pixman_image_get_height(po->shadow_image);
234
235 rects = pixman_region32_rectangles(region, &nrects);
236 for (i = 0; i < nrects; i++) {
237 b = weston_transformed_rect(width, height,
238 output->transform, rects[i]);
239
240 pixman_image_composite32(PIXMAN_OP_SRC,
241 po->shadow_image, /* src */
242 NULL /* mask */,
243 po->hw_buffer, /* dest */
244 b.x1, b.y1, /* src_x, src_y */
245 0, 0, /* mask_x, mask_y */
246 b.x1, b.y1, /* dest_x, dest_y */
247 b.x2 - b.x1, /* width */
248 b.y2 - b.y1 /* height */);
249 }
250
251}
252
253static void
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +0300254pixman_renderer_repaint_output(struct weston_output *output,
255 pixman_region32_t *output_damage)
256{
257 struct pixman_output_state *po = get_output_state(output);
258
259 if (!po->hw_buffer)
260 return;
261
262 repaint_surfaces(output, output_damage);
Ander Conselvan de Oliveira936effd2013-01-25 15:13:01 +0200263 copy_to_hw_buffer(output, output_damage);
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +0300264
265 pixman_region32_copy(&output->previous_damage, output_damage);
266 wl_signal_emit(&output->frame_signal, output);
267
268 /* Actual flip should be done by caller */
269}
270
271static void
272pixman_renderer_flush_damage(struct weston_surface *surface)
273{
274 /* No-op for pixman renderer */
275}
276
277static void
278pixman_renderer_attach(struct weston_surface *es, struct wl_buffer *buffer)
279{
280 struct pixman_surface_state *ps = get_surface_state(es);
281 pixman_format_code_t pixman_format;
282
283 weston_buffer_reference(&ps->buffer_ref, buffer);
284
285 if (ps->image) {
286 pixman_image_unref(ps->image);
287 ps->image = NULL;
288 }
289
290 if (!buffer)
291 return;
292
293 if (!wl_buffer_is_shm(buffer)) {
294 weston_log("Pixman renderer supports only SHM buffers\n");
295 weston_buffer_reference(&ps->buffer_ref, NULL);
296 return;
297 }
298
299 switch (wl_shm_buffer_get_format(buffer)) {
300 case WL_SHM_FORMAT_XRGB8888:
301 pixman_format = PIXMAN_x8r8g8b8;
302 break;
303 case WL_SHM_FORMAT_ARGB8888:
304 pixman_format = PIXMAN_a8r8g8b8;
305 break;
306 default:
307 weston_log("Unsupported SHM buffer format\n");
308 weston_buffer_reference(&ps->buffer_ref, NULL);
309 return;
310 break;
311 }
312 ps->image = pixman_image_create_bits(pixman_format,
313 wl_shm_buffer_get_width(buffer),
314 wl_shm_buffer_get_height(buffer),
315 wl_shm_buffer_get_data(buffer),
316 wl_shm_buffer_get_stride(buffer));
317}
318
319static int
320pixman_renderer_create_surface(struct weston_surface *surface)
321{
322 struct pixman_surface_state *ps;
323
324 ps = calloc(1, sizeof *ps);
325 if (!ps)
326 return -1;
327
328 surface->renderer_state = ps;
329
330 return 0;
331}
332
333static void
334pixman_renderer_surface_set_color(struct weston_surface *es,
335 float red, float green, float blue, float alpha)
336{
337 struct pixman_surface_state *ps = get_surface_state(es);
338 pixman_color_t color;
339
340 color.red = red * 0xffff;
341 color.green = green * 0xffff;
342 color.blue = blue * 0xffff;
343 color.alpha = alpha * 0xffff;
344
345 if (ps->image) {
346 pixman_image_unref(ps->image);
347 ps->image = NULL;
348 }
349
350 ps->image = pixman_image_create_solid_fill(&color);
351}
352
353static void
354pixman_renderer_destroy_surface(struct weston_surface *surface)
355{
356 struct pixman_surface_state *ps = get_surface_state(surface);
357
358 if (ps->image) {
359 pixman_image_unref(ps->image);
360 ps->image = NULL;
361 }
362 weston_buffer_reference(&ps->buffer_ref, NULL);
363 free(ps);
364}
365
Vasily Khoruzhick52cfd612013-01-08 19:09:01 +0300366static void
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +0300367pixman_renderer_destroy(struct weston_compositor *ec)
368{
369 free(ec->renderer);
370 ec->renderer = NULL;
371}
372
Ander Conselvan de Oliveira8792ef12013-01-25 15:13:00 +0200373static void
374debug_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
375 void *data)
376{
377 struct weston_compositor *ec = data;
378 struct pixman_renderer *pr = (struct pixman_renderer *) ec->renderer;
379
380 pr->repaint_debug ^= 1;
381
382 if (pr->repaint_debug) {
383 pixman_color_t red = {
384 0x3fff, 0x0000, 0x0000, 0x3fff
385 };
386
387 pr->debug_color = pixman_image_create_solid_fill(&red);
388 } else {
389 pixman_image_unref(pr->debug_color);
390 weston_compositor_damage_all(ec);
391 }
392}
393
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +0300394WL_EXPORT int
395pixman_renderer_init(struct weston_compositor *ec)
396{
Ander Conselvan de Oliveira8792ef12013-01-25 15:13:00 +0200397 struct pixman_renderer *renderer;
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +0300398
399 renderer = malloc(sizeof *renderer);
400 if (renderer == NULL)
401 return -1;
402
Ander Conselvan de Oliveira8792ef12013-01-25 15:13:00 +0200403 renderer->base.read_pixels = pixman_renderer_read_pixels;
404 renderer->base.repaint_output = pixman_renderer_repaint_output;
405 renderer->base.flush_damage = pixman_renderer_flush_damage;
406 renderer->base.attach = pixman_renderer_attach;
407 renderer->base.create_surface = pixman_renderer_create_surface;
408 renderer->base.surface_set_color = pixman_renderer_surface_set_color;
409 renderer->base.destroy_surface = pixman_renderer_destroy_surface;
410 renderer->base.destroy = pixman_renderer_destroy;
411 ec->renderer = &renderer->base;
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +0300412
Ander Conselvan de Oliveira8792ef12013-01-25 15:13:00 +0200413 weston_compositor_add_debug_binding(ec, KEY_R,
414 debug_binding, ec);
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +0300415 return 0;
416}
417
418WL_EXPORT void
419pixman_renderer_output_set_buffer(struct weston_output *output, pixman_image_t *buffer)
420{
421 struct pixman_output_state *po = get_output_state(output);
422
423 if (po->hw_buffer)
424 pixman_image_unref(po->hw_buffer);
425 po->hw_buffer = buffer;
426
427 if (po->hw_buffer) {
428 output->compositor->read_format = pixman_image_get_format(po->hw_buffer);
429 pixman_image_ref(po->hw_buffer);
430 }
431}
432
433WL_EXPORT int
434pixman_renderer_output_create(struct weston_output *output)
435{
436 struct pixman_output_state *po = calloc(1, sizeof *po);
Ander Conselvan de Oliveira936effd2013-01-25 15:13:01 +0200437 pixman_transform_t transform;
438 pixman_fixed_t fw, fh;
439 int w, h;
440 int rotated = 0;
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +0300441
442 if (!po)
443 return -1;
Ander Conselvan de Oliveira936effd2013-01-25 15:13:01 +0200444
445 /* set shadow image transformation */
446 w = output->current->width;
447 h = output->current->height;
448
449 fw = pixman_int_to_fixed(w);
450 fh = pixman_int_to_fixed(h);
451
452 pixman_transform_init_identity(&transform);
453
454 switch (output->transform) {
455 default:
456 case WL_OUTPUT_TRANSFORM_NORMAL:
457 break;
458 case WL_OUTPUT_TRANSFORM_180:
459 pixman_transform_rotate(&transform, NULL, -pixman_fixed_1, 0);
460 pixman_transform_translate(NULL, &transform, fw, fh);
461 break;
462 case WL_OUTPUT_TRANSFORM_270:
463 rotated = 1;
464 pixman_transform_rotate(&transform, NULL, 0, pixman_fixed_1);
465 pixman_transform_translate(&transform, NULL, fh, 0);
466 break;
467 case WL_OUTPUT_TRANSFORM_90:
468 rotated = 1;
469 pixman_transform_rotate(&transform, NULL, 0, -pixman_fixed_1);
470 pixman_transform_translate(&transform, NULL, 0, fw);
471 break;
472 }
473
474 if (rotated) {
475 int tmp = w;
476 w = h;
477 h = tmp;
478 }
479
480 po->shadow_buffer = malloc(w * h * 4);
481
482 if (!po->shadow_buffer) {
483 free(po);
484 return -1;
485 }
486
487 po->shadow_image =
488 pixman_image_create_bits(PIXMAN_x8r8g8b8, w, h,
489 po->shadow_buffer, w * 4);
490
491 if (!po->shadow_image) {
492 free(po->shadow_buffer);
493 free(po);
494 return -1;
495 }
496
497 pixman_image_set_transform(po->shadow_image, &transform);
498
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +0300499 output->renderer_state = po;
500
501 return 0;
502}
503
504WL_EXPORT void
505pixman_renderer_output_destroy(struct weston_output *output)
506{
507 struct pixman_output_state *po = get_output_state(output);
508
Ander Conselvan de Oliveira936effd2013-01-25 15:13:01 +0200509 pixman_image_unref(po->shadow_image);
Ander Conselvan de Oliveira23e72b82013-01-25 15:13:06 +0200510
511 if (po->hw_buffer)
512 pixman_image_unref(po->hw_buffer);
Ander Conselvan de Oliveira936effd2013-01-25 15:13:01 +0200513
514 po->shadow_image = NULL;
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +0300515 po->hw_buffer = NULL;
516
517 free(po);
518}