1 /* cairo-fdr - a 'flight data recorder', a black box, for cairo
3 * Copyright © 2009 Chris Wilson
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.
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.
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/>.
26 #include <cairo-script.h>
27 #include <cairo-tee.h>
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); \
47 (*name##_real) (args); \
50 static cairo_device_t *fdr_context;
51 static const cairo_user_data_key_t fdr_key;
54 fdr_get_extents (cairo_surface_t *surface,
55 cairo_rectangle_t *extents)
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);
64 extents->width -= extents->x;
65 extents->height -= extents->y;
69 fdr_surface_destroy (void *surface)
71 DLCALL (cairo_surface_destroy, surface);
75 fdr_surface_reference (void *surface)
77 DLCALL (cairo_surface_reference, surface);
80 static cairo_surface_t *
81 fdr_surface_get_tee (cairo_surface_t *surface)
83 return DLCALL (cairo_surface_get_user_data, surface, &fdr_key);
86 static cairo_surface_t *
87 fdr_tee_surface_index (cairo_surface_t *surface, int index)
89 return DLCALL (cairo_tee_surface_index, surface, index);
93 fdr_write (void *closure, const unsigned char *data, unsigned int len)
95 int fd = (int) (intptr_t) closure;
97 int ret = write (fd, data, len);
104 return CAIRO_STATUS_WRITE_ERROR;
106 } else if (ret == 0) {
107 return CAIRO_STATUS_WRITE_ERROR;
113 return CAIRO_STATUS_SUCCESS;
117 cairo_create (cairo_surface_t *surface)
119 cairo_surface_t *tee;
121 tee = fdr_surface_get_tee (surface);
123 cairo_surface_t *script;
124 cairo_rectangle_t extents;
125 cairo_content_t content;
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);
134 fdr_get_extents (surface, &extents);
135 content = DLCALL (cairo_surface_get_content, surface);
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);
142 DLCALL (cairo_surface_set_user_data, surface,
143 &fdr_key, tee, fdr_surface_destroy);
146 return DLCALL (cairo_create, tee);
150 fdr_remove_tee (cairo_surface_t *surface)
152 fdr_surface_reference (surface);
153 DLCALL (cairo_surface_set_user_data, surface, &fdr_key, NULL, NULL);
154 fdr_surface_destroy (surface);
158 cairo_destroy (cairo_t *cr)
160 cairo_surface_t *tee;
162 tee = DLCALL (cairo_get_target, cr);
163 DLCALL (cairo_destroy, cr);
165 if (DLCALL (cairo_surface_get_reference_count, tee) == 1)
166 fdr_remove_tee (fdr_tee_surface_index (tee, 0));
170 cairo_pattern_destroy (cairo_pattern_t *pattern)
172 if (DLCALL (cairo_pattern_get_type, pattern) == CAIRO_PATTERN_TYPE_SURFACE) {
173 cairo_surface_t *surface;
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)
179 fdr_remove_tee (fdr_tee_surface_index (surface, 0));
183 DLCALL (cairo_pattern_destroy, pattern);
187 cairo_get_target (cairo_t *cr)
189 cairo_surface_t *tee;
191 tee = DLCALL (cairo_get_target, cr);
192 return fdr_tee_surface_index (tee, 0);
196 cairo_get_group_target (cairo_t *cr)
198 cairo_surface_t *tee;
200 tee = DLCALL (cairo_get_group_target, cr);
201 return fdr_tee_surface_index (tee, 0);
205 cairo_pattern_create_for_surface (cairo_surface_t *surface)
207 cairo_surface_t *tee;
209 tee = fdr_surface_get_tee (surface);
213 return DLCALL (cairo_pattern_create_for_surface, surface);
217 cairo_pattern_get_surface (cairo_pattern_t *pattern,
218 cairo_surface_t **surface)
220 cairo_status_t status;
221 cairo_surface_t *tee;
223 status = DLCALL (cairo_pattern_get_surface, pattern, surface);
224 if (status != CAIRO_STATUS_SUCCESS)
227 tee = fdr_surface_get_tee (*surface);
231 return CAIRO_STATUS_SUCCESS;
235 cairo_set_source_surface (cairo_t *cr,
236 cairo_surface_t *surface,
239 cairo_surface_t *tee;
241 tee = fdr_surface_get_tee (surface);
245 DLCALL (cairo_set_source_surface, cr, surface, x, y);
249 cairo_surface_create_similar (cairo_surface_t *surface,
250 cairo_content_t content,
251 int width, int height)
253 cairo_surface_t *tee;
255 tee = fdr_surface_get_tee (surface);
259 return DLCALL (cairo_surface_create_similar,
260 surface, content, width, height);