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