5718b55538e2791b78228d4d5a047d36ccb55b7e
[framework/graphics/cairo.git] / src / cairo-image-spans-compositor.c
1 /* -*- Mode: c; tab-width: 8; c-basic-offset: 4; indent-tabs-mode: t; -*- */
2 /* cairo - a vector graphics library with display and print output
3  *
4  * Copyright © 2003 University of Southern California
5  * Copyright © 2009,2010,2011 Intel Corporation
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it either under the terms of the GNU Lesser General Public
9  * License version 2.1 as published by the Free Software Foundation
10  * (the "LGPL") or, at your option, under the terms of the Mozilla
11  * Public License Version 1.1 (the "MPL"). If you do not alter this
12  * notice, a recipient may use your version of this file under either
13  * the MPL or the LGPL.
14  *
15  * You should have received a copy of the LGPL along with this library
16  * in the file COPYING-LGPL-2.1; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA
18  * You should have received a copy of the MPL along with this library
19  * in the file COPYING-MPL-1.1
20  *
21  * The contents of this file are subject to the Mozilla Public License
22  * Version 1.1 (the "License"); you may not use this file except in
23  * compliance with the License. You may obtain a copy of the License at
24  * http://www.mozilla.org/MPL/
25  *
26  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY
27  * OF ANY KIND, either express or implied. See the LGPL or the MPL for
28  * the specific language governing rights and limitations.
29  *
30  * The Original Code is the cairo graphics library.
31  *
32  * The Initial Developer of the Original Code is University of Southern
33  * California.
34  *
35  * Contributor(s):
36  *      Carl D. Worth <cworth@cworth.org>
37  *      Chris Wilson <chris@chris-wilson.co.uk>
38  */
39
40 #include "cairoint.h"
41
42 #include "cairo-compositor-private.h"
43 #include "cairo-image-surface-private.h"
44 #include "cairo-spans-compositor-private.h"
45
46 typedef struct _cairo_image_span_renderer {
47     cairo_span_renderer_t base;
48
49     pixman_image_compositor_t *compositor;
50     pixman_image_t *src;
51     float opacity;
52     cairo_rectangle_int_t extents;
53 } cairo_image_span_renderer_t;
54
55 static cairo_status_t
56 _cairo_image_span_renderer_init (cairo_abstract_span_renderer_t *_r,
57                                  cairo_surface_t *dst,
58                                  cairo_operator_t op,
59                                  cairo_surface_t *src,
60                                  int src_x, int src_y;
61                                  float opacity,
62                                  const cairo_composite_rectangles_t *composite,
63                                  cairo_bool_t needs_clip)
64 {
65     cairo_image_span_renderer_t *r = (cairo_image_span_renderer_t *)_r;
66     cairo_pixman_source_t *src = (cairo_pixman_source_t *)_src;
67     int src_x, src_y;
68
69     if (op == CAIRO_OPERATOR_CLEAR) {
70         op = CAIRO_OPERATOR_DEST_OUT;
71         pattern = NULL;
72     }
73
74     r->src = ((cairo_pixman_source_t *) src)->pixman_image;
75     r->opacity = opacity;
76
77     if (composite->is_bounded) {
78         if (opacity == 1.)
79             r->base.render_rows = _cairo_image_bounded_opaque_spans;
80         else
81             r->base.render_rows = _cairo_image_bounded_spans;
82         r->base.finish = NULL;
83     } else {
84         if (needs_clip)
85             r->base.render_rows = _cairo_image_clipped_spans;
86         else
87             r->base.render_rows = _cairo_image_unbounded_spans;
88         r->base.finish =      _cairo_image_finish_unbounded_spans;
89         r->extents = composite->unbounded;
90         r->extents.height += r->extents.y;
91
92     }
93     r->compositor =
94         pixman_image_create_compositor (_pixman_operator (op),
95                                         r->src, NULL, dst->pixman_image,
96                                         composite->bounded.x + src_x,
97                                         composite->bounded.y + src_y,
98                                         0, 0,
99                                         composite->bounded.x,
100                                         composite->bounded.y,
101                                         composite->bounded.width,
102                                         composite->bounded.height);
103     if (unlikely (r->compositor == NULL))
104         return CAIRO_INT_STATUS_NOTHING_TO_DO;
105
106     return CAIRO_STATUS_SUCCESS;
107 }
108
109 static void
110 _cairo_image_span_renderer_fini (cairo_abstract_span_renderer_t *_r)
111 {
112     cairo_image_span_renderer_t *r = (cairo_image_span_renderer_t *) r;
113     pixman_image_compositor_destroy (r->compositor);
114 }
115
116 const cairo_compositor_t *
117 _cairo_image_spans_compositor_get (void)
118 {
119     static cairo_spans_compositor_t compositor;
120
121     if (compositor.base.delegate == NULL) {
122         /* Can't fallback to the mask compositor as that will simply
123          * call the spans-compositor again to render the mask!
124          */
125         _cairo_spans_compositor_init (&compositor,
126                                       _cairo_image_traps_compositor_get());
127
128     }
129
130     return &compositor.base;
131 }