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
#include "config.h"
20

            
21
#include <cairo.h>
22
#include <cairo-script.h>
23
#include <cairo-tee.h>
24
#include <stdlib.h>
25
#include <assert.h>
26
#include <signal.h>
27

            
28
#include <dlfcn.h>
29

            
30
static void *_dlhandle = RTLD_NEXT;
31
#define DLCALL(name, args...) ({ \
32
    static typeof (&name) name##_real; \
33
    if (name##_real == NULL) { \
34
	name##_real = dlsym (_dlhandle, #name); \
35
	if (name##_real == NULL && _dlhandle == RTLD_NEXT) { \
36
	    _dlhandle = dlopen ("libcairo.so", RTLD_LAZY); \
37
	    name##_real = dlsym (_dlhandle, #name); \
38
	    assert (name##_real != NULL); \
39
	} \
40
    } \
41
    (*name##_real) (args);  \
42
})
43

            
44
#define RINGBUFFER_SIZE 16
45
static cairo_surface_t *fdr_ringbuffer[RINGBUFFER_SIZE];
46
static int fdr_position;
47
static int fdr_dump;
48

            
49
static const cairo_user_data_key_t fdr_key;
50

            
51
static void
52
fdr_replay_to_script (cairo_surface_t *recording, cairo_device_t *ctx)
53
{
54
    if (recording != NULL) {
55
	DLCALL (cairo_script_write_comment, ctx, "--- fdr ---", -1);
56
	DLCALL (cairo_script_from_recording_surface, ctx, recording);
57
    }
58
}
59

            
60
static void
61
fdr_dump_ringbuffer (void)
62
{
63
    cairo_device_t *ctx;
64
    int n;
65

            
66
    ctx = DLCALL (cairo_script_create, "/tmp/fdr.trace");
67

            
68
    for (n = fdr_position; n < RINGBUFFER_SIZE; n++)
69
	fdr_replay_to_script (fdr_ringbuffer[n], ctx);
70

            
71
    for (n = 0; n < fdr_position; n++)
72
	fdr_replay_to_script (fdr_ringbuffer[n], ctx);
73

            
74
    DLCALL (cairo_device_destroy, ctx);
75
}
76

            
77
static void
78
fdr_sighandler (int sig)
79
{
80
    fdr_dump = 1;
81
}
82

            
83
static void
84
fdr_urgent_sighandler (int sig)
85
{
86
    fdr_dump_ringbuffer ();
87
}
88

            
89
static void
90
fdr_atexit (void)
91
{
92
    if (fdr_dump)
93
	fdr_dump_ringbuffer ();
94
}
95

            
96
static void
97
fdr_pending_signals (void)
98
{
99
    static int initialized;
100

            
101
    if (! initialized) {
102
	initialized = 1;
103

            
104
	signal (SIGUSR1, fdr_sighandler);
105

            
106
	signal (SIGSEGV, fdr_urgent_sighandler);
107
	signal (SIGABRT, fdr_urgent_sighandler);
108
	atexit (fdr_atexit);
109
    }
110

            
111
    if (fdr_dump) {
112
	fdr_dump_ringbuffer ();
113
	fdr_dump = 0;
114
    }
115
}
116

            
117
static void
118
fdr_get_extents (cairo_surface_t *surface,
119
		 cairo_rectangle_t *extents)
120
{
121
    cairo_t *cr;
122

            
123
    cr = DLCALL (cairo_create, surface);
124
    DLCALL (cairo_clip_extents, cr,
125
	    &extents->x, &extents->y, &extents->width, &extents->height);
126
    DLCALL (cairo_destroy, cr);
127

            
128
    extents->width -= extents->x;
129
    extents->height -= extents->y;
130
}
131

            
132
static void
133
fdr_surface_destroy (void *surface)
134
{
135
    DLCALL (cairo_surface_destroy, surface);
136
}
137

            
138
static void
139
fdr_surface_reference (void *surface)
140
{
141
    DLCALL (cairo_surface_reference, surface);
142
}
143

            
144
static cairo_surface_t *
145
fdr_surface_get_tee (cairo_surface_t *surface)
146
{
147
    return DLCALL (cairo_surface_get_user_data, surface, &fdr_key);
148
}
149

            
150
static cairo_surface_t *
151
fdr_tee_surface_index (cairo_surface_t *surface, int index)
152
{
153
    return DLCALL (cairo_tee_surface_index, surface, index);
154
}
155

            
156
cairo_t *
157
cairo_create (cairo_surface_t *surface)
158
{
159
    cairo_surface_t *record, *tee;
160

            
161
    fdr_pending_signals ();
162

            
163
    tee = fdr_surface_get_tee (surface);
164
    if (tee == NULL) {
165
	cairo_rectangle_t extents;
166
	cairo_content_t content;
167

            
168
	fdr_get_extents (surface, &extents);
169
	content = DLCALL (cairo_surface_get_content, surface);
170

            
171
	tee = DLCALL (cairo_tee_surface_create, surface);
172
	record = DLCALL (cairo_recording_surface_create, content, &extents);
173
	DLCALL (cairo_tee_surface_add, tee, record);
174

            
175
	DLCALL (cairo_surface_set_user_data, surface,
176
		&fdr_key, tee, fdr_surface_destroy);
177
    } else {
178
	int n;
179

            
180
	record = fdr_tee_surface_index (tee, 1);
181

            
182
	/* update the position of the recording surface in the ringbuffer */
183
	for (n = 0; n < RINGBUFFER_SIZE; n++) {
184
	    if (record == fdr_ringbuffer[n]) {
185
		fdr_ringbuffer[n] = NULL;
186
		break;
187
	    }
188
	}
189
    }
190

            
191
    fdr_surface_destroy (fdr_ringbuffer[fdr_position]);
192
    fdr_ringbuffer[fdr_position] = record;
193
    fdr_position = (fdr_position + 1) % RINGBUFFER_SIZE;
194

            
195
    return DLCALL (cairo_create, tee);
196
}
197

            
198
static void
199
fdr_remove_tee (cairo_surface_t *surface)
200
{
201
    fdr_surface_reference (surface);
202
    DLCALL (cairo_surface_set_user_data, surface, &fdr_key, NULL, NULL);
203
    fdr_surface_destroy (surface);
204
}
205

            
206
void
207
cairo_destroy (cairo_t *cr)
208
{
209
    cairo_surface_t *tee;
210

            
211
    tee = DLCALL (cairo_get_target, cr);
212
    DLCALL (cairo_destroy, cr);
213

            
214
    if (DLCALL (cairo_surface_get_reference_count, tee) == 1)
215
	fdr_remove_tee (fdr_tee_surface_index (tee, 0));
216
}
217

            
218
void
219
cairo_pattern_destroy (cairo_pattern_t *pattern)
220
{
221
    if (DLCALL (cairo_pattern_get_type, pattern) == CAIRO_PATTERN_TYPE_SURFACE) {
222
	cairo_surface_t *surface;
223

            
224
	if (DLCALL (cairo_pattern_get_surface, pattern, &surface) == CAIRO_STATUS_SUCCESS &&
225
	    DLCALL (cairo_surface_get_type, surface) == CAIRO_SURFACE_TYPE_TEE &&
226
	    DLCALL (cairo_surface_get_reference_count, surface) == 2)
227
	{
228
	    fdr_remove_tee (fdr_tee_surface_index (surface, 0));
229
	}
230
    }
231

            
232
    DLCALL (cairo_pattern_destroy, pattern);
233
}
234

            
235
cairo_surface_t *
236
cairo_get_target (cairo_t *cr)
237
{
238
    cairo_surface_t *tee;
239

            
240
    tee = DLCALL (cairo_get_target, cr);
241
    return fdr_tee_surface_index (tee, 0);
242
}
243

            
244
cairo_surface_t *
245
cairo_get_group_target (cairo_t *cr)
246
{
247
    cairo_surface_t *tee;
248

            
249
    tee = DLCALL (cairo_get_group_target, cr);
250
    return fdr_tee_surface_index (tee, 0);
251
}
252

            
253
cairo_pattern_t *
254
cairo_pattern_create_for_surface (cairo_surface_t *surface)
255
{
256
    cairo_surface_t *tee;
257

            
258
    tee = fdr_surface_get_tee (surface);
259
    if (tee != NULL)
260
	surface = tee;
261

            
262
    return DLCALL (cairo_pattern_create_for_surface, surface);
263
}
264

            
265
cairo_status_t
266
cairo_pattern_get_surface (cairo_pattern_t *pattern,
267
			   cairo_surface_t **surface)
268
{
269
    cairo_status_t status;
270
    cairo_surface_t *tee;
271

            
272
    status = DLCALL (cairo_pattern_get_surface, pattern, surface);
273
    if (status != CAIRO_STATUS_SUCCESS)
274
	return status;
275

            
276
    tee = fdr_surface_get_tee (*surface);
277
    if (tee != NULL)
278
	*surface = tee;
279

            
280
    return CAIRO_STATUS_SUCCESS;
281
}
282

            
283
void
284
cairo_set_source_surface (cairo_t *cr,
285
			  cairo_surface_t *surface,
286
			  double x, double y)
287
{
288
    cairo_surface_t *tee;
289

            
290
    tee = fdr_surface_get_tee (surface);
291
    if (tee != NULL)
292
	surface = tee;
293

            
294
    DLCALL (cairo_set_source_surface, cr, surface, x, y);
295
}
296

            
297
cairo_surface_t *
298
cairo_surface_create_similar (cairo_surface_t *surface,
299
			      cairo_content_t content,
300
			      int width, int height)
301
{
302
    cairo_surface_t *tee;
303

            
304
    tee = fdr_surface_get_tee (surface);
305
    if (tee != NULL)
306
	surface = tee;
307

            
308
    return DLCALL (cairo_surface_create_similar,
309
		   surface, content, width, height);
310
}
311

            
312
cairo_surface_t *
313
cairo_surface_create_for_rectangle (cairo_surface_t *surface,
314
                                    double		 x,
315
                                    double		 y,
316
                                    double		 width,
317
                                    double		 height)
318
{
319
    cairo_surface_t *tee;
320

            
321
    tee = fdr_surface_get_tee (surface);
322
    if (tee != NULL)
323
	surface = tee;
324

            
325
    return DLCALL (cairo_surface_create_for_rectangle,
326
		   surface, x, y, width, height);
327
}