blob: 8391cc6352f96894aaab4b0ac762741a5587d3d1 [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 {
34 pixman_image_t *hw_buffer;
35};
36
37struct pixman_surface_state {
38 pixman_image_t *image;
39 struct weston_buffer_reference buffer_ref;
40};
41
42struct pixman_renderer {
43 struct weston_renderer base;
Ander Conselvan de Oliveira8792ef12013-01-25 15:13:00 +020044 int repaint_debug;
45 pixman_image_t *debug_color;
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +030046};
47
48static inline struct pixman_output_state *
49get_output_state(struct weston_output *output)
50{
51 return (struct pixman_output_state *)output->renderer_state;
52}
53
54static inline struct pixman_surface_state *
55get_surface_state(struct weston_surface *surface)
56{
57 return (struct pixman_surface_state *)surface->renderer_state;
58}
59
60static inline struct pixman_renderer *
61get_renderer(struct weston_compositor *ec)
62{
63 return (struct pixman_renderer *)ec->renderer;
64}
65
66static int
67pixman_renderer_read_pixels(struct weston_output *output,
68 pixman_format_code_t format, void *pixels,
69 uint32_t x, uint32_t y,
70 uint32_t width, uint32_t height)
71{
72 struct pixman_output_state *po = get_output_state(output);
73 pixman_image_t *out_buf;
74 uint32_t cur_y;
75
76 if (!po->hw_buffer) {
77 errno = ENODEV;
78 return -1;
79 }
80
81 out_buf = pixman_image_create_bits(format,
82 width,
83 height,
84 pixels,
85 (PIXMAN_FORMAT_BPP(format) / 8) * width);
86
87 /* Caller expects vflipped image */
88 for (cur_y = y; cur_y < y + height; cur_y++) {
89 pixman_image_composite32(PIXMAN_OP_SRC,
90 po->hw_buffer, /* src */
91 NULL /* mask */,
92 out_buf, /* dest */
93 x, cur_y, /* src_x, src_y */
94 0, 0, /* mask_x, mask_y */
95 0, height - (cur_y - y), /* dest_x, dest_y */
96 width, /* width */
97 1 /* height */);
98 }
99
100 pixman_image_unref(out_buf);
101
102 return 0;
103}
104
105static void
106repaint_region(struct weston_surface *es, struct weston_output *output,
107 pixman_region32_t *region, pixman_region32_t *surf_region,
108 pixman_op_t pixman_op)
109{
Ander Conselvan de Oliveira8792ef12013-01-25 15:13:00 +0200110 struct pixman_renderer *pr =
111 (struct pixman_renderer *) output->compositor->renderer;
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +0300112 struct pixman_surface_state *ps = get_surface_state(es);
113 struct pixman_output_state *po = get_output_state(output);
114 pixman_region32_t final_region;
115 pixman_box32_t *rects;
116 int nrects, i, src_x, src_y;
117 float surface_x, surface_y;
118
119 /* The final region to be painted is the intersection of
120 * 'region' and 'surf_region'. However, 'region' is in the global
121 * coordinates, and 'surf_region' is in the surface-local
122 * coordinates
123 */
124 pixman_region32_init(&final_region);
125 pixman_region32_copy(&final_region, surf_region);
126
127 if (es->transform.enabled) {
128 weston_surface_to_global_float(es, 0, 0, &surface_x, &surface_y);
129 pixman_region32_translate(&final_region, (int)surface_x, (int)surface_y);
130 } else
131 pixman_region32_translate(&final_region, es->geometry.x, es->geometry.y);
132
133 /* That's what we need to paint */
134 pixman_region32_intersect(&final_region, &final_region, region);
135
136 rects = pixman_region32_rectangles(&final_region, &nrects);
137
138 for (i = 0; i < nrects; i++) {
139 weston_surface_from_global(es, rects[i].x1, rects[i].y1, &src_x, &src_y);
140 pixman_image_composite32(pixman_op,
141 ps->image, /* src */
142 NULL /* mask */,
143 po->hw_buffer, /* dest */
144 src_x, src_y, /* src_x, src_y */
145 0, 0, /* mask_x, mask_y */
146 rects[i].x1, rects[i].y1, /* dest_x, dest_y */
147 rects[i].x2 - rects[i].x1, /* width */
148 rects[i].y2 - rects[i].y1 /* height */);
Ander Conselvan de Oliveira8792ef12013-01-25 15:13:00 +0200149
150 if (!pr->repaint_debug)
151 continue;
152
153 pixman_image_composite32(PIXMAN_OP_OVER,
154 pr->debug_color, /* src */
155 NULL /* mask */,
156 po->hw_buffer, /* dest */
157 src_x, src_y, /* src_x, src_y */
158 0, 0, /* mask_x, mask_y */
159 rects[i].x1, rects[i].y1, /* dest_x, dest_y */
160 rects[i].x2 - rects[i].x1, /* width */
161 rects[i].y2 - rects[i].y1 /* height */);
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +0300162 }
163 pixman_region32_fini(&final_region);
164}
165
166static void
167draw_surface(struct weston_surface *es, struct weston_output *output,
168 pixman_region32_t *damage) /* in global coordinates */
169{
170 struct pixman_surface_state *ps = get_surface_state(es);
171 /* repaint bounding region in global coordinates: */
172 pixman_region32_t repaint;
173 /* non-opaque region in surface coordinates: */
174 pixman_region32_t surface_blend;
175
176 /* No buffer attached */
177 if (!ps->image)
178 return;
179
180 pixman_region32_init(&repaint);
181 pixman_region32_intersect(&repaint,
182 &es->transform.boundingbox, damage);
183 pixman_region32_subtract(&repaint, &repaint, &es->clip);
184
185 if (!pixman_region32_not_empty(&repaint))
186 goto out;
187
188 if (output->zoom.active) {
189 weston_log("pixman renderer does not support zoom\n");
190 goto out;
191 }
192
193 /* blended region is whole surface minus opaque region: */
194 pixman_region32_init_rect(&surface_blend, 0, 0,
195 es->geometry.width, es->geometry.height);
196 pixman_region32_subtract(&surface_blend, &surface_blend, &es->opaque);
197
198 if (pixman_region32_not_empty(&es->opaque)) {
199 repaint_region(es, output, &repaint, &es->opaque, PIXMAN_OP_SRC);
200 }
201
202 if (pixman_region32_not_empty(&surface_blend)) {
203 repaint_region(es, output, &repaint, &surface_blend, PIXMAN_OP_OVER);
204 }
205
206 pixman_region32_fini(&surface_blend);
207
208out:
209 pixman_region32_fini(&repaint);
210}
211static void
212repaint_surfaces(struct weston_output *output, pixman_region32_t *damage)
213{
214 struct weston_compositor *compositor = output->compositor;
215 struct weston_surface *surface;
216
217 wl_list_for_each_reverse(surface, &compositor->surface_list, link)
218 if (surface->plane == &compositor->primary_plane)
219 draw_surface(surface, output, damage);
220}
221
222static void
223pixman_renderer_repaint_output(struct weston_output *output,
224 pixman_region32_t *output_damage)
225{
226 struct pixman_output_state *po = get_output_state(output);
227
228 if (!po->hw_buffer)
229 return;
230
231 repaint_surfaces(output, output_damage);
232
233 pixman_region32_copy(&output->previous_damage, output_damage);
234 wl_signal_emit(&output->frame_signal, output);
235
236 /* Actual flip should be done by caller */
237}
238
239static void
240pixman_renderer_flush_damage(struct weston_surface *surface)
241{
242 /* No-op for pixman renderer */
243}
244
245static void
246pixman_renderer_attach(struct weston_surface *es, struct wl_buffer *buffer)
247{
248 struct pixman_surface_state *ps = get_surface_state(es);
249 pixman_format_code_t pixman_format;
250
251 weston_buffer_reference(&ps->buffer_ref, buffer);
252
253 if (ps->image) {
254 pixman_image_unref(ps->image);
255 ps->image = NULL;
256 }
257
258 if (!buffer)
259 return;
260
261 if (!wl_buffer_is_shm(buffer)) {
262 weston_log("Pixman renderer supports only SHM buffers\n");
263 weston_buffer_reference(&ps->buffer_ref, NULL);
264 return;
265 }
266
267 switch (wl_shm_buffer_get_format(buffer)) {
268 case WL_SHM_FORMAT_XRGB8888:
269 pixman_format = PIXMAN_x8r8g8b8;
270 break;
271 case WL_SHM_FORMAT_ARGB8888:
272 pixman_format = PIXMAN_a8r8g8b8;
273 break;
274 default:
275 weston_log("Unsupported SHM buffer format\n");
276 weston_buffer_reference(&ps->buffer_ref, NULL);
277 return;
278 break;
279 }
280 ps->image = pixman_image_create_bits(pixman_format,
281 wl_shm_buffer_get_width(buffer),
282 wl_shm_buffer_get_height(buffer),
283 wl_shm_buffer_get_data(buffer),
284 wl_shm_buffer_get_stride(buffer));
285}
286
287static int
288pixman_renderer_create_surface(struct weston_surface *surface)
289{
290 struct pixman_surface_state *ps;
291
292 ps = calloc(1, sizeof *ps);
293 if (!ps)
294 return -1;
295
296 surface->renderer_state = ps;
297
298 return 0;
299}
300
301static void
302pixman_renderer_surface_set_color(struct weston_surface *es,
303 float red, float green, float blue, float alpha)
304{
305 struct pixman_surface_state *ps = get_surface_state(es);
306 pixman_color_t color;
307
308 color.red = red * 0xffff;
309 color.green = green * 0xffff;
310 color.blue = blue * 0xffff;
311 color.alpha = alpha * 0xffff;
312
313 if (ps->image) {
314 pixman_image_unref(ps->image);
315 ps->image = NULL;
316 }
317
318 ps->image = pixman_image_create_solid_fill(&color);
319}
320
321static void
322pixman_renderer_destroy_surface(struct weston_surface *surface)
323{
324 struct pixman_surface_state *ps = get_surface_state(surface);
325
326 if (ps->image) {
327 pixman_image_unref(ps->image);
328 ps->image = NULL;
329 }
330 weston_buffer_reference(&ps->buffer_ref, NULL);
331 free(ps);
332}
333
Vasily Khoruzhick52cfd612013-01-08 19:09:01 +0300334static void
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +0300335pixman_renderer_destroy(struct weston_compositor *ec)
336{
337 free(ec->renderer);
338 ec->renderer = NULL;
339}
340
Ander Conselvan de Oliveira8792ef12013-01-25 15:13:00 +0200341static void
342debug_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
343 void *data)
344{
345 struct weston_compositor *ec = data;
346 struct pixman_renderer *pr = (struct pixman_renderer *) ec->renderer;
347
348 pr->repaint_debug ^= 1;
349
350 if (pr->repaint_debug) {
351 pixman_color_t red = {
352 0x3fff, 0x0000, 0x0000, 0x3fff
353 };
354
355 pr->debug_color = pixman_image_create_solid_fill(&red);
356 } else {
357 pixman_image_unref(pr->debug_color);
358 weston_compositor_damage_all(ec);
359 }
360}
361
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +0300362WL_EXPORT int
363pixman_renderer_init(struct weston_compositor *ec)
364{
Ander Conselvan de Oliveira8792ef12013-01-25 15:13:00 +0200365 struct pixman_renderer *renderer;
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +0300366
367 renderer = malloc(sizeof *renderer);
368 if (renderer == NULL)
369 return -1;
370
Ander Conselvan de Oliveira8792ef12013-01-25 15:13:00 +0200371 renderer->base.read_pixels = pixman_renderer_read_pixels;
372 renderer->base.repaint_output = pixman_renderer_repaint_output;
373 renderer->base.flush_damage = pixman_renderer_flush_damage;
374 renderer->base.attach = pixman_renderer_attach;
375 renderer->base.create_surface = pixman_renderer_create_surface;
376 renderer->base.surface_set_color = pixman_renderer_surface_set_color;
377 renderer->base.destroy_surface = pixman_renderer_destroy_surface;
378 renderer->base.destroy = pixman_renderer_destroy;
379 ec->renderer = &renderer->base;
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +0300380
Ander Conselvan de Oliveira8792ef12013-01-25 15:13:00 +0200381 weston_compositor_add_debug_binding(ec, KEY_R,
382 debug_binding, ec);
Vasily Khoruzhick71c2dd32013-01-07 20:39:49 +0300383 return 0;
384}
385
386WL_EXPORT void
387pixman_renderer_output_set_buffer(struct weston_output *output, pixman_image_t *buffer)
388{
389 struct pixman_output_state *po = get_output_state(output);
390
391 if (po->hw_buffer)
392 pixman_image_unref(po->hw_buffer);
393 po->hw_buffer = buffer;
394
395 if (po->hw_buffer) {
396 output->compositor->read_format = pixman_image_get_format(po->hw_buffer);
397 pixman_image_ref(po->hw_buffer);
398 }
399}
400
401WL_EXPORT int
402pixman_renderer_output_create(struct weston_output *output)
403{
404 struct pixman_output_state *po = calloc(1, sizeof *po);
405
406 if (!po)
407 return -1;
408
409 output->renderer_state = po;
410
411 return 0;
412}
413
414WL_EXPORT void
415pixman_renderer_output_destroy(struct weston_output *output)
416{
417 struct pixman_output_state *po = get_output_state(output);
418
419 pixman_image_unref(po->hw_buffer);
420 po->hw_buffer = NULL;
421
422 free(po);
423}