1 /* cairo - a vector graphics library with display and print output
3 * Copyright © 2009 Eric Anholt
4 * Copyright © 2009 Chris Wilson
5 * Copyright © 2005,2010 Red Hat, Inc
6 * Copyright © 2011 Intel Corporation
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.
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
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/
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.
31 * The Original Code is the cairo graphics library.
33 * The Initial Developer of the Original Code is Red Hat, Inc.
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>
44 #include "cairo-gl-private.h"
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"
54 typedef struct _cairo_gl_span_renderer {
55 cairo_span_renderer_t base;
57 cairo_gl_composite_t setup;
63 cairo_gl_context_t *ctx;
64 } cairo_gl_span_renderer_t;
67 _cairo_gl_bounded_opaque_spans (void *abstract_renderer,
69 const cairo_half_open_span_t *spans,
72 cairo_gl_span_renderer_t *r = abstract_renderer;
75 return CAIRO_STATUS_SUCCESS;
78 if (spans[0].coverage) {
79 _cairo_gl_composite_emit_rect (r->ctx,
81 spans[1].x, y + height,
86 } while (--num_spans > 1);
88 return CAIRO_STATUS_SUCCESS;
92 _cairo_gl_bounded_spans (void *abstract_renderer,
94 const cairo_half_open_span_t *spans,
97 cairo_gl_span_renderer_t *r = abstract_renderer;
100 return CAIRO_STATUS_SUCCESS;
103 if (spans[0].coverage) {
104 _cairo_gl_composite_emit_rect (r->ctx,
106 spans[1].x, y + height,
107 r->opacity * spans[0].coverage);
111 } while (--num_spans > 1);
113 return CAIRO_STATUS_SUCCESS;
116 static cairo_status_t
117 _cairo_gl_unbounded_spans (void *abstract_renderer,
119 const cairo_half_open_span_t *spans,
122 cairo_gl_span_renderer_t *r = abstract_renderer;
125 _cairo_gl_composite_emit_rect (r->ctx,
131 if (num_spans == 0) {
132 _cairo_gl_composite_emit_rect (r->ctx,
137 if (spans[0].x != r->xmin) {
138 _cairo_gl_composite_emit_rect (r->ctx,
140 spans[0].x, y + height,
145 _cairo_gl_composite_emit_rect (r->ctx,
147 spans[1].x, y + height,
148 r->opacity * spans[0].coverage);
150 } while (--num_spans > 1);
152 if (spans[0].x != r->xmax) {
153 _cairo_gl_composite_emit_rect (r->ctx,
160 r->ymin = y + height;
161 return CAIRO_STATUS_SUCCESS;
165 static cairo_status_t
166 _cairo_gl_clipped_spans (void *abstract_renderer,
168 const cairo_half_open_span_t *spans,
171 cairo_gl_span_renderer_t *r = abstract_renderer;
174 _cairo_gl_composite_emit_rect (r->ctx,
180 if (num_spans == 0) {
181 _cairo_gl_composite_emit_rect (r->ctx,
186 if (spans[0].x != r->xmin) {
187 _cairo_gl_composite_emit_rect (r->ctx,
189 spans[0].x, y + height,
194 _cairo_gl_composite_emit_rect (r->ctx,
196 spans[1].x, y + height,
197 r->opacity * spans[0].coverage);
199 } while (--num_spans > 1);
201 if (spans[0].x != r->xmax) {
202 _cairo_gl_composite_emit_rect (r->ctx,
209 r->ymin = y + height;
210 return CAIRO_STATUS_SUCCESS;
213 static cairo_status_t
214 _cairo_gl_finish_unbounded_spans (void *abstract_renderer)
216 cairo_gl_span_renderer_t *r = abstract_renderer;
218 if (r->ymax > r->ymin) {
219 _cairo_gl_composite_emit_rect (r->ctx,
225 return _cairo_gl_context_release (r->ctx, CAIRO_STATUS_SUCCESS);
228 static cairo_status_t
229 _cairo_gl_finish_bounded_spans (void *abstract_renderer)
231 cairo_gl_span_renderer_t *r = abstract_renderer;
233 return _cairo_gl_context_release (r->ctx, CAIRO_STATUS_SUCCESS);
237 emit_aligned_boxes (cairo_gl_context_t *ctx,
238 const cairo_boxes_t *boxes)
240 const struct _cairo_boxes_chunk *chunk;
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);
255 static cairo_int_status_t
256 fill_boxes (void *_dst,
258 const cairo_color_t *color,
259 cairo_boxes_t *boxes)
261 cairo_gl_composite_t setup;
262 cairo_gl_context_t *ctx;
263 cairo_int_status_t status;
265 TRACE ((stderr, "%s\n", __FUNCTION__));
266 status = _cairo_gl_composite_init (&setup, op, _dst, FALSE);
267 if (unlikely (status))
270 _cairo_gl_composite_set_solid_source (&setup, color);
272 status = _cairo_gl_composite_begin (&setup, &ctx);
273 if (unlikely (status))
276 emit_aligned_boxes (ctx, boxes);
277 status = _cairo_gl_context_release (ctx, CAIRO_STATUS_SUCCESS);
280 _cairo_gl_composite_fini (&setup);
284 static cairo_int_status_t
285 draw_image_boxes (void *_dst,
286 cairo_image_surface_t *image,
287 cairo_boxes_t *boxes,
290 cairo_gl_surface_t *dst = _dst;
291 struct _cairo_boxes_chunk *chunk;
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;
303 status = _cairo_gl_surface_draw_image (dst, image,
307 if (unlikely (status))
312 return CAIRO_STATUS_SUCCESS;
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,
321 cairo_gl_composite_t setup;
322 cairo_gl_context_t *ctx;
323 cairo_int_status_t status;
325 TRACE ((stderr, "%s\n", __FUNCTION__));
326 status = _cairo_gl_composite_init (&setup, CAIRO_OPERATOR_SOURCE, _dst, FALSE);
327 if (unlikely (status))
330 _cairo_gl_composite_set_source_operand (&setup, source_to_operand (src));
331 _cairo_gl_operand_translate (&setup.src, -dx, -dy);
333 status = _cairo_gl_composite_begin (&setup, &ctx);
334 if (unlikely (status))
337 emit_aligned_boxes (ctx, boxes);
338 status = _cairo_gl_context_release (ctx, CAIRO_STATUS_SUCCESS);
341 _cairo_gl_composite_fini (&setup);
345 static cairo_int_status_t
346 composite_boxes (void *_dst,
348 cairo_surface_t *abstract_src,
349 cairo_surface_t *abstract_mask,
356 cairo_boxes_t *boxes,
357 const cairo_rectangle_int_t *extents)
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;
365 TRACE ((stderr, "%s mask=(%d,%d), dst=(%d, %d)\n", __FUNCTION__,
366 mask_x, mask_y, dst_x, dst_y));
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;
377 src_operand = source_to_operand (abstract_src);
379 src_operand = source_to_operand (abstract_src);
381 status = _cairo_gl_composite_init (&setup, op, _dst, FALSE);
382 if (unlikely (status))
385 _cairo_gl_composite_set_source_operand (&setup,
387 _cairo_gl_operand_translate (&setup.src, -src_x, -src_y);
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);
393 status = _cairo_gl_composite_begin (&setup, &ctx);
394 if (unlikely (status))
397 emit_aligned_boxes (ctx, boxes);
398 status = _cairo_gl_context_release (ctx, CAIRO_STATUS_SUCCESS);
401 _cairo_gl_composite_fini (&setup);
402 if (src_operand == &tmp_operand)
403 _cairo_gl_operand_destroy (&tmp_operand);
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)
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;
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;
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;
440 status = _cairo_gl_composite_init (&r->setup,
441 op, (cairo_gl_surface_t *)composite->surface,
443 if (unlikely (status))
446 status = _cairo_gl_composite_set_source (&r->setup, source,
447 &composite->source_sample_area,
448 &composite->unbounded,
450 if (unlikely (status))
454 if (composite->mask_pattern.base.type == CAIRO_PATTERN_TYPE_SOLID) {
455 r->opacity = composite->mask_pattern.solid.color.alpha;
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))
465 _cairo_gl_composite_set_spans (&r->setup);
467 status = _cairo_gl_composite_begin (&r->setup, &r->ctx);
468 if (unlikely (status))
471 if (composite->is_bounded) {
472 if (r->opacity == 1.)
473 r->base.render_rows = _cairo_gl_bounded_opaque_spans;
475 r->base.render_rows = _cairo_gl_bounded_spans;
476 r->base.finish = _cairo_gl_finish_bounded_spans;
479 r->base.render_rows = _cairo_gl_clipped_spans;
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;
489 return CAIRO_STATUS_SUCCESS;
496 _cairo_gl_span_renderer_fini (cairo_abstract_span_renderer_t *_r,
497 cairo_int_status_t status)
499 cairo_gl_span_renderer_t *r = (cairo_gl_span_renderer_t *) _r;
501 if (status == CAIRO_INT_STATUS_UNSUPPORTED)
504 if (status == CAIRO_INT_STATUS_SUCCESS)
507 _cairo_gl_composite_fini (&r->setup);
510 const cairo_compositor_t *
511 _cairo_gl_span_compositor_get (void)
513 static cairo_spans_compositor_t spans;
514 static cairo_compositor_t shape;
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());
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;