Initialize Tizen 2.3
[framework/graphics/cairo.git] / util / cairo-sphinx / fdr.c
1 /* cairo-fdr - a 'flight data recorder', a black box, for cairo
2  *
3  * Copyright © 2009 Chris Wilson
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18
19 #define _GNU_SOURCE
20
21 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif
24
25 #include <cairo.h>
26 #include <cairo-script.h>
27 #include <cairo-tee.h>
28 #include <stdlib.h>
29 #include <stdint.h>
30 #include <assert.h>
31 #include <unistd.h>
32 #include <errno.h>
33
34 #include <dlfcn.h>
35
36 static void *_dlhandle = RTLD_NEXT;
37 #define DLCALL(name, args...) ({ \
38     static typeof (&name) name##_real; \
39     if (name##_real == NULL) { \
40         name##_real = dlsym (_dlhandle, #name); \
41         if (name##_real == NULL && _dlhandle == RTLD_NEXT) { \
42             _dlhandle = dlopen ("libcairo.so", RTLD_LAZY); \
43             name##_real = dlsym (_dlhandle, #name); \
44             assert (name##_real != NULL); \
45         } \
46     } \
47     (*name##_real) (args);  \
48 })
49
50 static cairo_device_t *fdr_context;
51 static const cairo_user_data_key_t fdr_key;
52
53 static void
54 fdr_get_extents (cairo_surface_t *surface,
55                  cairo_rectangle_t *extents)
56 {
57     cairo_t *cr;
58
59     cr = DLCALL (cairo_create, surface);
60     DLCALL (cairo_clip_extents, cr,
61             &extents->x, &extents->y, &extents->width, &extents->height);
62     DLCALL (cairo_destroy, cr);
63
64     extents->width -= extents->x;
65     extents->height -= extents->y;
66 }
67
68 static void
69 fdr_surface_destroy (void *surface)
70 {
71     DLCALL (cairo_surface_destroy, surface);
72 }
73
74 static void
75 fdr_surface_reference (void *surface)
76 {
77     DLCALL (cairo_surface_reference, surface);
78 }
79
80 static cairo_surface_t *
81 fdr_surface_get_tee (cairo_surface_t *surface)
82 {
83     return DLCALL (cairo_surface_get_user_data, surface, &fdr_key);
84 }
85
86 static cairo_surface_t *
87 fdr_tee_surface_index (cairo_surface_t *surface, int index)
88 {
89     return DLCALL (cairo_tee_surface_index, surface, index);
90 }
91
92 static cairo_status_t
93 fdr_write (void *closure, const unsigned char *data, unsigned int len)
94 {
95     int fd = (int) (intptr_t) closure;
96     while (len) {
97         int ret = write (fd, data, len);
98         if (ret < 0) {
99             switch (errno) {
100             case EAGAIN:
101             case EINTR:
102                 continue;
103             default:
104                 return CAIRO_STATUS_WRITE_ERROR;
105             }
106         } else if (ret == 0) {
107             return CAIRO_STATUS_WRITE_ERROR;
108         } else {
109             data += ret;
110             len -= ret;
111         }
112     }
113     return CAIRO_STATUS_SUCCESS;
114 }
115
116 cairo_t *
117 cairo_create (cairo_surface_t *surface)
118 {
119     cairo_surface_t *tee;
120
121     tee = fdr_surface_get_tee (surface);
122     if (tee == NULL) {
123         cairo_surface_t *script;
124         cairo_rectangle_t extents;
125         cairo_content_t content;
126
127         if (fdr_context == NULL) {
128             const char *env = getenv ("CAIRO_SPHINX_FD");
129             int fd = env ? atoi (env) : 1;
130             fdr_context = DLCALL (cairo_script_create_for_stream,
131                                   fdr_write, (void *) (intptr_t) fd);
132         }
133
134         fdr_get_extents (surface, &extents);
135         content = DLCALL (cairo_surface_get_content, surface);
136
137         tee = DLCALL (cairo_tee_surface_create, surface);
138         script = DLCALL (cairo_script_surface_create,
139                          fdr_context, content, extents.width, extents.height);
140         DLCALL (cairo_tee_surface_add, tee, script);
141
142         DLCALL (cairo_surface_set_user_data, surface,
143                 &fdr_key, tee, fdr_surface_destroy);
144     }
145
146     return DLCALL (cairo_create, tee);
147 }
148
149 static void
150 fdr_remove_tee (cairo_surface_t *surface)
151 {
152     fdr_surface_reference (surface);
153     DLCALL (cairo_surface_set_user_data, surface, &fdr_key, NULL, NULL);
154     fdr_surface_destroy (surface);
155 }
156
157 void
158 cairo_destroy (cairo_t *cr)
159 {
160     cairo_surface_t *tee;
161
162     tee = DLCALL (cairo_get_target, cr);
163     DLCALL (cairo_destroy, cr);
164
165     if (DLCALL (cairo_surface_get_reference_count, tee) == 1)
166         fdr_remove_tee (fdr_tee_surface_index (tee, 0));
167 }
168
169 void
170 cairo_pattern_destroy (cairo_pattern_t *pattern)
171 {
172     if (DLCALL (cairo_pattern_get_type, pattern) == CAIRO_PATTERN_TYPE_SURFACE) {
173         cairo_surface_t *surface;
174
175         if (DLCALL (cairo_pattern_get_surface, pattern, &surface) == CAIRO_STATUS_SUCCESS &&
176             DLCALL (cairo_surface_get_type, surface) == CAIRO_SURFACE_TYPE_TEE &&
177             DLCALL (cairo_surface_get_reference_count, surface) == 2)
178         {
179             fdr_remove_tee (fdr_tee_surface_index (surface, 0));
180         }
181     }
182
183     DLCALL (cairo_pattern_destroy, pattern);
184 }
185
186 cairo_surface_t *
187 cairo_get_target (cairo_t *cr)
188 {
189     cairo_surface_t *tee;
190
191     tee = DLCALL (cairo_get_target, cr);
192     return fdr_tee_surface_index (tee, 0);
193 }
194
195 cairo_surface_t *
196 cairo_get_group_target (cairo_t *cr)
197 {
198     cairo_surface_t *tee;
199
200     tee = DLCALL (cairo_get_group_target, cr);
201     return fdr_tee_surface_index (tee, 0);
202 }
203
204 cairo_pattern_t *
205 cairo_pattern_create_for_surface (cairo_surface_t *surface)
206 {
207     cairo_surface_t *tee;
208
209     tee = fdr_surface_get_tee (surface);
210     if (tee != NULL)
211         surface = tee;
212
213     return DLCALL (cairo_pattern_create_for_surface, surface);
214 }
215
216 cairo_status_t
217 cairo_pattern_get_surface (cairo_pattern_t *pattern,
218                            cairo_surface_t **surface)
219 {
220     cairo_status_t status;
221     cairo_surface_t *tee;
222
223     status = DLCALL (cairo_pattern_get_surface, pattern, surface);
224     if (status != CAIRO_STATUS_SUCCESS)
225         return status;
226
227     tee = fdr_surface_get_tee (*surface);
228     if (tee != NULL)
229         *surface = tee;
230
231     return CAIRO_STATUS_SUCCESS;
232 }
233
234 void
235 cairo_set_source_surface (cairo_t *cr,
236                           cairo_surface_t *surface,
237                           double x, double y)
238 {
239     cairo_surface_t *tee;
240
241     tee = fdr_surface_get_tee (surface);
242     if (tee != NULL)
243         surface = tee;
244
245     DLCALL (cairo_set_source_surface, cr, surface, x, y);
246 }
247
248 cairo_surface_t *
249 cairo_surface_create_similar (cairo_surface_t *surface,
250                               cairo_content_t content,
251                               int width, int height)
252 {
253     cairo_surface_t *tee;
254
255     tee = fdr_surface_get_tee (surface);
256     if (tee != NULL)
257         surface = tee;
258
259     return DLCALL (cairo_surface_create_similar,
260                    surface, content, width, height);
261 }