0b23056d7da7e58a422282f1c4bbbd4921eef965
[framework/graphics/cairo.git] / src / cairo-gl-spans-compositor.c
1 /* cairo - a vector graphics library with display and print output
2  *
3  * Copyright © 2009 Eric Anholt
4  * Copyright © 2009 Chris Wilson
5  * Copyright © 2005,2010 Red Hat, Inc
6  * Copyright © 2011 Intel Corporation
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it either under the terms of the GNU Lesser General Public
10  * License version 2.1 as published by the Free Software Foundation
11  * (the "LGPL") or, at your option, under the terms of the Mozilla
12  * Public License Version 1.1 (the "MPL"). If you do not alter this
13  * notice, a recipient may use your version of this file under either
14  * the MPL or the LGPL.
15  *
16  * You should have received a copy of the LGPL along with this library
17  * in the file COPYING-LGPL-2.1; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA
19  * You should have received a copy of the MPL along with this library
20  * in the file COPYING-MPL-1.1
21  *
22  * The contents of this file are subject to the Mozilla Public License
23  * Version 1.1 (the "License"); you may not use this file except in
24  * compliance with the License. You may obtain a copy of the License at
25  * http://www.mozilla.org/MPL/
26  *
27  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY
28  * OF ANY KIND, either express or implied. See the LGPL or the MPL for
29  * the specific language governing rights and limitations.
30  *
31  * The Original Code is the cairo graphics library.
32  *
33  * The Initial Developer of the Original Code is Red Hat, Inc.
34  *
35  * Contributor(s):
36  *      Benjamin Otte <otte@gnome.org>
37  *      Carl Worth <cworth@cworth.org>
38  *      Chris Wilson <chris@chris-wilson.co.uk>
39  *      Eric Anholt <eric@anholt.net>
40  */
41
42 #include "cairoint.h"
43
44 #include "cairo-gl-private.h"
45
46 #include "cairo-composite-rectangles-private.h"
47 #include "cairo-compositor-private.h"
48 #include "cairo-default-context-private.h"
49 #include "cairo-error-private.h"
50 #include "cairo-image-surface-private.h"
51 #include "cairo-spans-compositor-private.h"
52 #include "cairo-surface-backend-private.h"
53
54 typedef struct _cairo_gl_span_renderer {
55     cairo_span_renderer_t base;
56
57     cairo_gl_composite_t setup;
58     double opacity;
59
60     int xmin, xmax;
61     int ymin, ymax;
62
63     cairo_gl_context_t *ctx;
64 } cairo_gl_span_renderer_t;
65
66 static cairo_status_t
67 _cairo_gl_bounded_opaque_spans (void *abstract_renderer,
68                                 int y, int height,
69                                 const cairo_half_open_span_t *spans,
70                                 unsigned num_spans)
71 {
72     cairo_gl_span_renderer_t *r = abstract_renderer;
73
74     if (num_spans == 0)
75         return CAIRO_STATUS_SUCCESS;
76
77     do {
78         if (spans[0].coverage) {
79             _cairo_gl_composite_emit_rect (r->ctx,
80                                            spans[0].x, y,
81                                            spans[1].x, y + height,
82                                            spans[0].coverage);
83         }
84
85         spans++;
86     } while (--num_spans > 1);
87
88     return CAIRO_STATUS_SUCCESS;
89 }
90
91 static cairo_status_t
92 _cairo_gl_bounded_spans (void *abstract_renderer,
93                          int y, int height,
94                          const cairo_half_open_span_t *spans,
95                          unsigned num_spans)
96 {
97     cairo_gl_span_renderer_t *r = abstract_renderer;
98
99     if (num_spans == 0)
100         return CAIRO_STATUS_SUCCESS;
101
102     do {
103         if (spans[0].coverage) {
104             _cairo_gl_composite_emit_rect (r->ctx,
105                                            spans[0].x, y,
106                                            spans[1].x, y + height,
107                                            r->opacity * spans[0].coverage);
108         }
109
110         spans++;
111     } while (--num_spans > 1);
112
113     return CAIRO_STATUS_SUCCESS;
114 }
115
116 static cairo_status_t
117 _cairo_gl_unbounded_spans (void *abstract_renderer,
118                            int y, int height,
119                            const cairo_half_open_span_t *spans,
120                            unsigned num_spans)
121 {
122     cairo_gl_span_renderer_t *r = abstract_renderer;
123
124     if (y > r->ymin) {
125         _cairo_gl_composite_emit_rect (r->ctx,
126                                        r->xmin, r->ymin,
127                                        r->xmax, y,
128                                        0);
129     }
130
131     if (num_spans == 0) {
132         _cairo_gl_composite_emit_rect (r->ctx,
133                                        r->xmin, y,
134                                        r->xmax, y + height,
135                                        0);
136     } else {
137         if (spans[0].x != r->xmin) {
138             _cairo_gl_composite_emit_rect (r->ctx,
139                                            r->xmin, y,
140                                            spans[0].x,     y + height,
141                                            0);
142         }
143
144         do {
145             _cairo_gl_composite_emit_rect (r->ctx,
146                                            spans[0].x, y,
147                                            spans[1].x, y + height,
148                                            r->opacity * spans[0].coverage);
149             spans++;
150         } while (--num_spans > 1);
151
152         if (spans[0].x != r->xmax) {
153             _cairo_gl_composite_emit_rect (r->ctx,
154                                            spans[0].x,     y,
155                                            r->xmax, y + height,
156                                            0);
157         }
158     }
159
160     r->ymin = y + height;
161     return CAIRO_STATUS_SUCCESS;
162 }
163
164 /* XXX */
165 static cairo_status_t
166 _cairo_gl_clipped_spans (void *abstract_renderer,
167                            int y, int height,
168                            const cairo_half_open_span_t *spans,
169                            unsigned num_spans)
170 {
171     cairo_gl_span_renderer_t *r = abstract_renderer;
172
173     if (y > r->ymin) {
174         _cairo_gl_composite_emit_rect (r->ctx,
175                                        r->xmin, r->ymin,
176                                        r->xmax, y,
177                                        0);
178     }
179
180     if (num_spans == 0) {
181         _cairo_gl_composite_emit_rect (r->ctx,
182                                        r->xmin, y,
183                                        r->xmax, y + height,
184                                        0);
185     } else {
186         if (spans[0].x != r->xmin) {
187             _cairo_gl_composite_emit_rect (r->ctx,
188                                            r->xmin, y,
189                                            spans[0].x,     y + height,
190                                            0);
191         }
192
193         do {
194             _cairo_gl_composite_emit_rect (r->ctx,
195                                            spans[0].x, y,
196                                            spans[1].x, y + height,
197                                            r->opacity * spans[0].coverage);
198             spans++;
199         } while (--num_spans > 1);
200
201         if (spans[0].x != r->xmax) {
202             _cairo_gl_composite_emit_rect (r->ctx,
203                                            spans[0].x,     y,
204                                            r->xmax, y + height,
205                                            0);
206         }
207     }
208
209     r->ymin = y + height;
210     return CAIRO_STATUS_SUCCESS;
211 }
212
213 static cairo_status_t
214 _cairo_gl_finish_unbounded_spans (void *abstract_renderer)
215 {
216     cairo_gl_span_renderer_t *r = abstract_renderer;
217
218     if (r->ymax > r->ymin) {
219         _cairo_gl_composite_emit_rect (r->ctx,
220                                        r->xmin, r->ymin,
221                                        r->xmax, r->ymax,
222                                        0);
223     }
224
225     return _cairo_gl_context_release (r->ctx, CAIRO_STATUS_SUCCESS);
226 }
227
228 static cairo_status_t
229 _cairo_gl_finish_bounded_spans (void *abstract_renderer)
230 {
231     cairo_gl_span_renderer_t *r = abstract_renderer;
232
233     return _cairo_gl_context_release (r->ctx, CAIRO_STATUS_SUCCESS);
234 }
235
236 static void
237 emit_aligned_boxes (cairo_gl_context_t *ctx,
238                     const cairo_boxes_t *boxes)
239 {
240     const struct _cairo_boxes_chunk *chunk;
241     int i;
242
243     TRACE ((stderr, "%s: num_boxes=%d\n", __FUNCTION__, boxes->num_boxes));
244     for (chunk = &boxes->chunks; chunk; chunk = chunk->next) {
245         for (i = 0; i < chunk->count; i++) {
246             int x1 = _cairo_fixed_integer_part (chunk->base[i].p1.x);
247             int y1 = _cairo_fixed_integer_part (chunk->base[i].p1.y);
248             int x2 = _cairo_fixed_integer_part (chunk->base[i].p2.x);
249             int y2 = _cairo_fixed_integer_part (chunk->base[i].p2.y);
250             _cairo_gl_composite_emit_rect (ctx, x1, y1, x2, y2, 255);
251         }
252     }
253 }
254
255 static cairo_int_status_t
256 fill_boxes (void                *_dst,
257             cairo_operator_t     op,
258             const cairo_color_t *color,
259             cairo_boxes_t       *boxes)
260 {
261     cairo_gl_composite_t setup;
262     cairo_gl_context_t *ctx;
263     cairo_int_status_t status;
264
265     TRACE ((stderr, "%s\n", __FUNCTION__));
266     status = _cairo_gl_composite_init (&setup, op, _dst, FALSE);
267     if (unlikely (status))
268         goto FAIL;
269
270    _cairo_gl_composite_set_solid_source (&setup, color);
271
272     status = _cairo_gl_composite_begin (&setup, &ctx);
273     if (unlikely (status))
274         goto FAIL;
275
276     emit_aligned_boxes (ctx, boxes);
277     status = _cairo_gl_context_release (ctx, CAIRO_STATUS_SUCCESS);
278
279 FAIL:
280     _cairo_gl_composite_fini (&setup);
281     return status;
282 }
283
284 static cairo_int_status_t
285 draw_image_boxes (void *_dst,
286                   cairo_image_surface_t *image,
287                   cairo_boxes_t *boxes,
288                   int dx, int dy)
289 {
290     cairo_gl_surface_t *dst = _dst;
291     struct _cairo_boxes_chunk *chunk;
292     int i;
293
294     for (chunk = &boxes->chunks; chunk; chunk = chunk->next) {
295         for (i = 0; i < chunk->count; i++) {
296             cairo_box_t *b = &chunk->base[i];
297             int x = _cairo_fixed_integer_part (b->p1.x);
298             int y = _cairo_fixed_integer_part (b->p1.y);
299             int w = _cairo_fixed_integer_part (b->p2.x) - x;
300             int h = _cairo_fixed_integer_part (b->p2.y) - y;
301             cairo_status_t status;
302
303             status = _cairo_gl_surface_draw_image (dst, image,
304                                                    x + dx, y + dy,
305                                                    w, h,
306                                                    x, y);
307             if (unlikely (status))
308                 return status;
309         }
310     }
311
312     return CAIRO_STATUS_SUCCESS;
313 }
314
315 static cairo_int_status_t copy_boxes (void *_dst,
316                                       cairo_surface_t *src,
317                                       cairo_boxes_t *boxes,
318                                       const cairo_rectangle_int_t *extents,
319                                       int dx, int dy)
320 {
321     cairo_gl_composite_t setup;
322     cairo_gl_context_t *ctx;
323     cairo_int_status_t status;
324
325     TRACE ((stderr, "%s\n", __FUNCTION__));
326     status = _cairo_gl_composite_init (&setup, CAIRO_OPERATOR_SOURCE, _dst, FALSE);
327     if (unlikely (status))
328         goto FAIL;
329
330     _cairo_gl_composite_set_source_operand (&setup, source_to_operand (src));
331     _cairo_gl_operand_translate (&setup.src, -dx, -dy);
332
333     status = _cairo_gl_composite_begin (&setup, &ctx);
334     if (unlikely (status))
335         goto FAIL;
336
337     emit_aligned_boxes (ctx, boxes);
338     status = _cairo_gl_context_release (ctx, CAIRO_STATUS_SUCCESS);
339
340 FAIL:
341     _cairo_gl_composite_fini (&setup);
342     return status;
343 }
344
345 static cairo_int_status_t
346 composite_boxes (void                   *_dst,
347                  cairo_operator_t       op,
348                  cairo_surface_t        *abstract_src,
349                  cairo_surface_t        *abstract_mask,
350                  int                    src_x,
351                  int                    src_y,
352                  int                    mask_x,
353                  int                    mask_y,
354                  int                    dst_x,
355                  int                    dst_y,
356                  cairo_boxes_t          *boxes,
357                  const cairo_rectangle_int_t  *extents)
358 {
359     cairo_gl_composite_t setup;
360     cairo_gl_context_t *ctx;
361     cairo_int_status_t status;
362     cairo_gl_operand_t tmp_operand;
363     cairo_gl_operand_t *src_operand;
364
365     TRACE ((stderr, "%s mask=(%d,%d), dst=(%d, %d)\n", __FUNCTION__,
366             mask_x, mask_y, dst_x, dst_y));
367
368     if (abstract_mask) {
369         if (op == CAIRO_OPERATOR_CLEAR) {
370             _cairo_gl_solid_operand_init (&tmp_operand, CAIRO_COLOR_WHITE);
371             src_operand = &tmp_operand;
372             op = CAIRO_OPERATOR_DEST_OUT;
373         } else if (op == CAIRO_OPERATOR_SOURCE) {
374             /* requires a LERP in the shader between dest and source */
375             return CAIRO_INT_STATUS_UNSUPPORTED;
376         } else
377             src_operand = source_to_operand (abstract_src);
378     } else
379         src_operand = source_to_operand (abstract_src);
380
381     status = _cairo_gl_composite_init (&setup, op, _dst, FALSE);
382     if (unlikely (status))
383         goto FAIL;
384
385     _cairo_gl_composite_set_source_operand (&setup,
386                                             src_operand);
387     _cairo_gl_operand_translate (&setup.src, -src_x, -src_y);
388
389     _cairo_gl_composite_set_mask_operand (&setup,
390                                           source_to_operand (abstract_mask));
391     _cairo_gl_operand_translate (&setup.mask, -mask_x, -mask_y);
392
393     status = _cairo_gl_composite_begin (&setup, &ctx);
394     if (unlikely (status))
395         goto FAIL;
396
397     emit_aligned_boxes (ctx, boxes);
398     status = _cairo_gl_context_release (ctx, CAIRO_STATUS_SUCCESS);
399
400 FAIL:
401     _cairo_gl_composite_fini (&setup);
402     if (src_operand == &tmp_operand)
403         _cairo_gl_operand_destroy (&tmp_operand);
404     return status;
405 }
406
407 static cairo_int_status_t
408 _cairo_gl_span_renderer_init (cairo_abstract_span_renderer_t    *_r,
409                               const cairo_composite_rectangles_t *composite,
410                               cairo_antialias_t                  antialias,
411                               cairo_bool_t                       needs_clip)
412 {
413     cairo_gl_span_renderer_t *r = (cairo_gl_span_renderer_t *)_r;
414     const cairo_pattern_t *source = &composite->source_pattern.base;
415     cairo_operator_t op = composite->op;
416     cairo_int_status_t status;
417
418     if (op == CAIRO_OPERATOR_SOURCE) {
419         if (! _cairo_pattern_is_opaque (&composite->source_pattern.base,
420                                         &composite->source_sample_area))
421             return CAIRO_INT_STATUS_UNSUPPORTED;
422         op = CAIRO_OPERATOR_OVER;
423     }
424
425     /* XXX earlier! */
426     if (op == CAIRO_OPERATOR_CLEAR) {
427         source = &_cairo_pattern_white.base;
428         op = CAIRO_OPERATOR_DEST_OUT;
429     } else if (composite->surface->is_clear &&
430                (op == CAIRO_OPERATOR_SOURCE ||
431                 op == CAIRO_OPERATOR_OVER ||
432                 op == CAIRO_OPERATOR_ADD)) {
433         op = CAIRO_OPERATOR_SOURCE;
434     } else if (op == CAIRO_OPERATOR_SOURCE) {
435         /* no lerp equivalent without some major PITA */
436         return CAIRO_INT_STATUS_UNSUPPORTED;
437     } else if (! _cairo_gl_operator_is_supported (op))
438         return CAIRO_INT_STATUS_UNSUPPORTED;
439
440     status = _cairo_gl_composite_init (&r->setup,
441                                        op, (cairo_gl_surface_t *)composite->surface,
442                                        FALSE);
443     if (unlikely (status))
444         goto FAIL;
445
446     status = _cairo_gl_composite_set_source (&r->setup, source,
447                                              &composite->source_sample_area,
448                                              &composite->unbounded,
449                                              FALSE);
450     if (unlikely (status))
451         goto FAIL;
452
453     r->opacity = 1.0;
454     if (composite->mask_pattern.base.type == CAIRO_PATTERN_TYPE_SOLID) {
455         r->opacity = composite->mask_pattern.solid.color.alpha;
456     } else {
457         status = _cairo_gl_composite_set_mask (&r->setup,
458                                                &composite->mask_pattern.base,
459                                                &composite->mask_sample_area,
460                                                &composite->unbounded);
461         if (unlikely (status))
462             goto FAIL;
463     }
464
465     _cairo_gl_composite_set_spans (&r->setup);
466
467     status = _cairo_gl_composite_begin (&r->setup, &r->ctx);
468     if (unlikely (status))
469         goto FAIL;
470
471     if (composite->is_bounded) {
472         if (r->opacity == 1.)
473             r->base.render_rows = _cairo_gl_bounded_opaque_spans;
474         else
475             r->base.render_rows = _cairo_gl_bounded_spans;
476         r->base.finish = _cairo_gl_finish_bounded_spans;
477     } else {
478         if (needs_clip)
479             r->base.render_rows = _cairo_gl_clipped_spans;
480         else
481             r->base.render_rows = _cairo_gl_unbounded_spans;
482         r->base.finish = _cairo_gl_finish_unbounded_spans;
483         r->xmin = composite->unbounded.x;
484         r->xmax = composite->unbounded.x + composite->unbounded.width;
485         r->ymin = composite->unbounded.y;
486         r->ymax = composite->unbounded.y + composite->unbounded.height;
487     }
488
489     return CAIRO_STATUS_SUCCESS;
490
491 FAIL:
492     return status;
493 }
494
495 static void
496 _cairo_gl_span_renderer_fini (cairo_abstract_span_renderer_t *_r,
497                               cairo_int_status_t status)
498 {
499     cairo_gl_span_renderer_t *r = (cairo_gl_span_renderer_t *) _r;
500
501     if (status == CAIRO_INT_STATUS_UNSUPPORTED)
502         return;
503
504     if (status == CAIRO_INT_STATUS_SUCCESS)
505         r->base.finish (r);
506
507     _cairo_gl_composite_fini (&r->setup);
508 }
509
510 const cairo_compositor_t *
511 _cairo_gl_span_compositor_get (void)
512 {
513     static cairo_spans_compositor_t spans;
514     static cairo_compositor_t shape;
515
516     if (spans.base.delegate == NULL) {
517         /* The fallback to traps here is essentially just for glyphs... */
518         _cairo_shape_mask_compositor_init (&shape,
519                                            _cairo_gl_traps_compositor_get());
520         shape.glyphs = NULL;
521
522         _cairo_spans_compositor_init (&spans, &shape);
523         spans.fill_boxes = fill_boxes;
524         spans.draw_image_boxes = draw_image_boxes;
525         spans.copy_boxes = copy_boxes;
526         //spans.check_composite_boxes = check_composite_boxes;
527         spans.pattern_to_surface = _cairo_gl_pattern_to_source;
528         spans.composite_boxes = composite_boxes;
529         //spans.check_span_renderer = check_span_renderer;
530         spans.renderer_init = _cairo_gl_span_renderer_init;
531         spans.renderer_fini = _cairo_gl_span_renderer_fini;
532     }
533
534     return &spans.base;
535 }