Tizen 2.0 Release
[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     if (_cairo_gl_surface_is_texture (dst))
295     return CAIRO_INT_STATUS_UNSUPPORTED;
296
297     for (chunk = &boxes->chunks; chunk; chunk = chunk->next) {
298         for (i = 0; i < chunk->count; i++) {
299             cairo_box_t *b = &chunk->base[i];
300             int x = _cairo_fixed_integer_part (b->p1.x);
301             int y = _cairo_fixed_integer_part (b->p1.y);
302             int w = _cairo_fixed_integer_part (b->p2.x) - x;
303             int h = _cairo_fixed_integer_part (b->p2.y) - y;
304             cairo_status_t status;
305
306             status = _cairo_gl_surface_draw_image (dst, image,
307                                                    x + dx, y + dy,
308                                                    w, h,
309                                                    x, y);
310             if (unlikely (status))
311                 return status;
312         }
313     }
314
315     return CAIRO_STATUS_SUCCESS;
316 }
317
318 static cairo_int_status_t copy_boxes (void *_dst,
319                                       cairo_surface_t *_src,
320                                       cairo_boxes_t *boxes,
321                                       const cairo_rectangle_int_t *extents,
322                                       int dx, int dy)
323 {
324     cairo_gl_surface_t *dst = _dst;
325     cairo_gl_surface_t *src = (cairo_gl_surface_t *)_src;
326     cairo_gl_composite_t setup;
327     cairo_gl_context_t *ctx;
328     cairo_int_status_t status;
329
330     TRACE ((stderr, "%s\n", __FUNCTION__));
331     if (! _cairo_gl_surface_is_texture (src))
332         return CAIRO_INT_STATUS_UNSUPPORTED;
333
334     if (src->base.device != dst->base.device)
335         return CAIRO_INT_STATUS_UNSUPPORTED;
336
337     status = _cairo_gl_composite_init (&setup, CAIRO_OPERATOR_SOURCE, _dst, FALSE);
338     if (unlikely (status))
339         goto FAIL;
340
341     _cairo_gl_composite_set_source_operand (&setup, &src->operand);
342     _cairo_gl_operand_translate (&setup.src, -dx, -dy);
343
344     status = _cairo_gl_composite_begin (&setup, &ctx);
345     if (unlikely (status))
346         goto FAIL;
347
348     emit_aligned_boxes (ctx, boxes);
349     status = _cairo_gl_context_release (ctx, CAIRO_STATUS_SUCCESS);
350
351 FAIL:
352     _cairo_gl_composite_fini (&setup);
353     return status;
354 }
355
356 static cairo_int_status_t
357 composite_boxes (void                   *_dst,
358                  cairo_operator_t       op,
359                  cairo_surface_t        *abstract_src,
360                  cairo_surface_t        *abstract_mask,
361                  int                    src_x,
362                  int                    src_y,
363                  int                    mask_x,
364                  int                    mask_y,
365                  int                    dst_x,
366                  int                    dst_y,
367                  cairo_boxes_t          *boxes,
368                  const cairo_rectangle_int_t  *extents)
369 {
370     cairo_gl_composite_t setup;
371     cairo_gl_context_t *ctx;
372     cairo_int_status_t status;
373     cairo_gl_operand_t tmp_operand;
374     cairo_gl_operand_t *src_operand;
375
376     TRACE ((stderr, "%s mask=(%d,%d), dst=(%d, %d)\n", __FUNCTION__,
377             mask_x, mask_y, dst_x, dst_y));
378
379     if (abstract_mask) {
380         if (op == CAIRO_OPERATOR_CLEAR) {
381             _cairo_gl_solid_operand_init (&tmp_operand, CAIRO_COLOR_WHITE);
382             src_operand = &tmp_operand;
383             op = CAIRO_OPERATOR_DEST_OUT;
384         } else if (op == CAIRO_OPERATOR_SOURCE) {
385             /* requires a LERP in the shader between dest and source */
386             return CAIRO_INT_STATUS_UNSUPPORTED;
387         } else
388             src_operand = source_to_operand (abstract_src);
389     } else
390         src_operand = source_to_operand (abstract_src);
391
392     status = _cairo_gl_composite_init (&setup, op, _dst, FALSE);
393     if (unlikely (status))
394         goto FAIL;
395
396     _cairo_gl_composite_set_source_operand (&setup,
397                                             src_operand);
398     _cairo_gl_operand_translate (&setup.src, -src_x, -src_y);
399
400     _cairo_gl_composite_set_mask_operand (&setup,
401                                           source_to_operand (abstract_mask));
402     _cairo_gl_operand_translate (&setup.mask, -mask_x, -mask_y);
403
404     status = _cairo_gl_composite_begin (&setup, &ctx);
405     if (unlikely (status))
406         goto FAIL;
407
408     emit_aligned_boxes (ctx, boxes);
409     status = _cairo_gl_context_release (ctx, CAIRO_STATUS_SUCCESS);
410
411 FAIL:
412     _cairo_gl_composite_fini (&setup);
413     if (src_operand == &tmp_operand)
414         _cairo_gl_operand_destroy (&tmp_operand);
415     return status;
416 }
417
418 static cairo_int_status_t
419 _cairo_gl_span_renderer_init (cairo_abstract_span_renderer_t    *_r,
420                               const cairo_composite_rectangles_t *composite,
421                               cairo_antialias_t                  antialias,
422                               cairo_bool_t                       needs_clip)
423 {
424     cairo_gl_span_renderer_t *r = (cairo_gl_span_renderer_t *)_r;
425     const cairo_pattern_t *source = &composite->source_pattern.base;
426     cairo_operator_t op = composite->op;
427     cairo_int_status_t status;
428
429     if (op == CAIRO_OPERATOR_SOURCE) {
430         if (! _cairo_pattern_is_opaque (&composite->source_pattern.base,
431                                         &composite->source_sample_area))
432             return CAIRO_INT_STATUS_UNSUPPORTED;
433         op = CAIRO_OPERATOR_OVER;
434     }
435
436     /* XXX earlier! */
437     if (op == CAIRO_OPERATOR_CLEAR) {
438         source = &_cairo_pattern_white.base;
439         op = CAIRO_OPERATOR_DEST_OUT;
440     } else if (composite->surface->is_clear &&
441                (op == CAIRO_OPERATOR_SOURCE ||
442                 op == CAIRO_OPERATOR_OVER ||
443                 op == CAIRO_OPERATOR_ADD)) {
444         op = CAIRO_OPERATOR_SOURCE;
445     } else if (op == CAIRO_OPERATOR_SOURCE) {
446         /* no lerp equivalent without some major PITA */
447         return CAIRO_INT_STATUS_UNSUPPORTED;
448     } else if (! _cairo_gl_operator_is_supported (op))
449         return CAIRO_INT_STATUS_UNSUPPORTED;
450
451     status = _cairo_gl_composite_init (&r->setup,
452                                        op, (cairo_gl_surface_t *)composite->surface,
453                                        FALSE);
454     if (unlikely (status))
455         goto FAIL;
456
457     status = _cairo_gl_composite_set_source (&r->setup, source,
458                                              &composite->source_sample_area,
459                                              &composite->unbounded,
460                                              FALSE);
461     if (unlikely (status))
462         goto FAIL;
463
464     r->opacity = 1.0;
465     if (composite->mask_pattern.base.type == CAIRO_PATTERN_TYPE_SOLID) {
466         r->opacity = composite->mask_pattern.solid.color.alpha;
467     } else {
468         status = _cairo_gl_composite_set_mask (&r->setup,
469                                                &composite->mask_pattern.base,
470                                                &composite->mask_sample_area,
471                                                &composite->unbounded);
472         if (unlikely (status))
473             goto FAIL;
474     }
475
476     _cairo_gl_composite_set_spans (&r->setup);
477
478     status = _cairo_gl_composite_begin (&r->setup, &r->ctx);
479     if (unlikely (status))
480         goto FAIL;
481
482     if (composite->is_bounded) {
483         if (r->opacity == 1.)
484             r->base.render_rows = _cairo_gl_bounded_opaque_spans;
485         else
486             r->base.render_rows = _cairo_gl_bounded_spans;
487         r->base.finish = _cairo_gl_finish_bounded_spans;
488     } else {
489         if (needs_clip)
490             r->base.render_rows = _cairo_gl_clipped_spans;
491         else
492             r->base.render_rows = _cairo_gl_unbounded_spans;
493         r->base.finish = _cairo_gl_finish_unbounded_spans;
494         r->xmin = composite->unbounded.x;
495         r->xmax = composite->unbounded.x + composite->unbounded.width;
496         r->ymin = composite->unbounded.y;
497         r->ymax = composite->unbounded.y + composite->unbounded.height;
498     }
499
500     return CAIRO_STATUS_SUCCESS;
501
502 FAIL:
503     return status;
504 }
505
506 static void
507 _cairo_gl_span_renderer_fini (cairo_abstract_span_renderer_t *_r,
508                               cairo_int_status_t status)
509 {
510     cairo_gl_span_renderer_t *r = (cairo_gl_span_renderer_t *) _r;
511
512     if (status == CAIRO_INT_STATUS_UNSUPPORTED)
513         return;
514
515     if (status == CAIRO_INT_STATUS_SUCCESS)
516         r->base.finish (r);
517
518     _cairo_gl_composite_fini (&r->setup);
519 }
520
521 const cairo_compositor_t *
522 _cairo_gl_span_compositor_get (void)
523 {
524     static cairo_spans_compositor_t spans;
525     static cairo_compositor_t shape;
526
527     if (spans.base.delegate == NULL) {
528         /* The fallback to traps here is essentially just for glyphs... */
529         _cairo_shape_mask_compositor_init (&shape,
530                                            _cairo_gl_traps_compositor_get());
531         shape.glyphs = NULL;
532
533         _cairo_spans_compositor_init (&spans, &shape);
534         spans.fill_boxes = fill_boxes;
535         spans.draw_image_boxes = draw_image_boxes;
536         spans.copy_boxes = copy_boxes;
537         //spans.check_composite_boxes = check_composite_boxes;
538         spans.pattern_to_surface = _cairo_gl_pattern_to_source;
539         spans.composite_boxes = composite_boxes;
540         //spans.check_span_renderer = check_span_renderer;
541         spans.renderer_init = _cairo_gl_span_renderer_init;
542         spans.renderer_fini = _cairo_gl_span_renderer_fini;
543     }
544
545     return &spans.base;
546 }