From cd7b27ff5c01a533c2c065c4b455ad19df2be3bb Mon Sep 17 00:00:00 2001 From: Chris Wilson Date: Sun, 16 Aug 2009 21:08:22 +0100 Subject: [PATCH] Add 'flight-data-recorder' utility. This is a simple variation on cairo-trace that wraps records the last 16 contexts by wrapping the target surface inside a tee surface, along with a meta/recording surface. Then on receipt of a SIGUSR1, those last 16 contexts are played via a script-surface into /tmp/fdr.trace. Mostly proof-of-concept, it seems to be causing a number of rendering glitches whilst testing with firefox -- otherwise, it seems to works. --- configure.ac | 1 + src/cairo-scaled-font-private.h | 3 + src/cairo-scaled-font.c | 16 ++ src/cairo-script-surface.c | 58 +++++- src/cairo-script.h | 4 + src/cairo.h | 4 + util/Makefile.am | 3 + util/cairo-fdr/Makefile.am | 13 ++ util/cairo-fdr/fdr.c | 304 ++++++++++++++++++++++++++++++++ 9 files changed, 400 insertions(+), 6 deletions(-) create mode 100644 util/cairo-fdr/Makefile.am create mode 100644 util/cairo-fdr/fdr.c diff --git a/configure.ac b/configure.ac index c02b1d2ee..31d79cfd9 100644 --- a/configure.ac +++ b/configure.ac @@ -667,6 +667,7 @@ test/Makefile test/pdiff/Makefile perf/Makefile util/Makefile +util/cairo-fdr/Makefile util/cairo-script/Makefile util/cairo-script/examples/Makefile util/cairo-trace/Makefile diff --git a/src/cairo-scaled-font-private.h b/src/cairo-scaled-font-private.h index dc16ea871..cdc3c73d2 100644 --- a/src/cairo-scaled-font-private.h +++ b/src/cairo-scaled-font-private.h @@ -123,4 +123,7 @@ struct _cairo_scaled_font { const cairo_scaled_font_backend_t *backend; }; +cairo_private void +_cairo_scaled_font_revoke_ownership (cairo_scaled_font_t *scaled_font); + #endif /* CAIRO_SCALED_FONT_PRIVATE_H */ diff --git a/src/cairo-scaled-font.c b/src/cairo-scaled-font.c index 3d357a264..d8406f77f 100644 --- a/src/cairo-scaled-font.c +++ b/src/cairo-scaled-font.c @@ -838,6 +838,22 @@ _cairo_scaled_font_fini_internal (cairo_scaled_font_t *scaled_font) _cairo_user_data_array_fini (&scaled_font->user_data); } +/* XXX: allow multiple backends to share the font */ +void +_cairo_scaled_font_revoke_ownership (cairo_scaled_font_t *scaled_font) +{ + if (scaled_font->surface_backend == NULL) + return; + + _cairo_scaled_font_reset_cache (scaled_font); + + if (scaled_font->surface_backend->scaled_font_fini != NULL) + scaled_font->surface_backend->scaled_font_fini (scaled_font); + + scaled_font->surface_backend = NULL; + scaled_font->surface_private = NULL; +} + void _cairo_scaled_font_fini (cairo_scaled_font_t *scaled_font) { diff --git a/src/cairo-script-surface.c b/src/cairo-script-surface.c index 60f3b9a57..a1ae50855 100644 --- a/src/cairo-script-surface.c +++ b/src/cairo-script-surface.c @@ -51,6 +51,7 @@ #include "cairo-list-private.h" #include "cairo-meta-surface-private.h" #include "cairo-output-stream-private.h" +#include "cairo-scaled-font-private.h" #include "cairo-surface-clipper-private.h" #include "cairo-surface-wrapper-private.h" @@ -445,10 +446,14 @@ static cairo_status_t _emit_surface (cairo_script_surface_t *surface) { _cairo_output_stream_printf (surface->ctx->stream, - "<< /content %s /width %f /height %f", - _content_to_string (surface->base.content), - surface->width, - surface->height); + "<< /content //%s", + _content_to_string (surface->base.content)); + if (surface->width != -1 && surface->height != -1) { + _cairo_output_stream_printf (surface->ctx->stream, + " /width %f /height %f", + surface->width, + surface->height); + } if (surface->base.x_fallback_resolution != CAIRO_SURFACE_FALLBACK_RESOLUTION_DEFAULT || @@ -2573,8 +2578,11 @@ _emit_scaled_font (cairo_script_surface_t *surface, surface->cr.current_scaled_font = scaled_font; - assert (scaled_font->surface_backend == NULL || - scaled_font->surface_backend == &_cairo_script_surface_backend); + if (! (scaled_font->surface_backend == NULL || + scaled_font->surface_backend == &_cairo_script_surface_backend)) + { + _cairo_scaled_font_revoke_ownership (scaled_font); + } font_private = scaled_font->surface_private; if (font_private == NULL) { @@ -3384,6 +3392,44 @@ cairo_script_surface_create_for_target (cairo_script_context_t *context, target)->base; } +cairo_status_t +cairo_script_from_meta_surface (cairo_script_context_t *context, + cairo_surface_t *meta) +{ + cairo_box_t bbox; + cairo_rectangle_int_t extents; + cairo_surface_t *surface; + cairo_status_t status; + + if (unlikely (context->status)) + return context->status; + + if (unlikely (meta->status)) + return meta->status; + + if (unlikely (! _cairo_surface_is_meta (meta))) + return _cairo_error (CAIRO_STATUS_SURFACE_TYPE_MISMATCH); + + status = _cairo_meta_surface_get_bbox ((cairo_meta_surface_t *) meta, + &bbox, NULL); + if (unlikely (status)) + return status; + + _cairo_box_round_to_rectangle (&bbox, &extents); + surface = &_cairo_script_surface_create_internal (context, + meta->content, + extents.width, + extents.height, + NULL)->base; + if (unlikely (surface->status)) + return surface->status; + + status = cairo_meta_surface_replay (meta, surface); + cairo_surface_destroy (surface); + + return status; +} + void cairo_script_context_destroy (cairo_script_context_t *context) { diff --git a/src/cairo-script.h b/src/cairo-script.h index 5d8594129..c4183506d 100644 --- a/src/cairo-script.h +++ b/src/cairo-script.h @@ -81,6 +81,10 @@ cairo_public cairo_surface_t * cairo_script_surface_create_for_target (cairo_script_context_t *context, cairo_surface_t *target); +cairo_public cairo_status_t +cairo_script_from_meta_surface (cairo_script_context_t *context, + cairo_surface_t *meta); + CAIRO_END_DECLS #else /*CAIRO_HAS_SCRIPT_SURFACE*/ diff --git a/src/cairo.h b/src/cairo.h index 9be5c703e..55c0fdd9c 100644 --- a/src/cairo.h +++ b/src/cairo.h @@ -2198,6 +2198,10 @@ cairo_public void cairo_tee_surface_append (cairo_surface_t *surface, cairo_surface_t *target); +cairo_public cairo_surface_t * +cairo_tee_surface_index (cairo_surface_t *abstract_surface, + int index); + /* Pattern creation functions */ cairo_public cairo_pattern_t * diff --git a/util/Makefile.am b/util/Makefile.am index 79bc3362e..d3b96a38b 100644 --- a/util/Makefile.am +++ b/util/Makefile.am @@ -4,6 +4,9 @@ SUBDIRS = . cairo-script if BUILD_TRACE SUBDIRS += cairo-trace +if CAIRO_HAS_SCRIPT_SURFACE +SUBDIRS += cairo-fdr +endif endif AM_CPPFLAGS = -I$(top_srcdir)/src diff --git a/util/cairo-fdr/Makefile.am b/util/cairo-fdr/Makefile.am new file mode 100644 index 000000000..5cd5798e1 --- /dev/null +++ b/util/cairo-fdr/Makefile.am @@ -0,0 +1,13 @@ +cairolibdir = $(libdir)/cairo + +#bin_SCRIPTS = cairo-fdr +cairolib_LTLIBRARIES = cairo-fdr.la + +AM_CPPFLAGS = -I$(top_srcdir)/src \ + -I$(top_builddir)/src + +cairo_fdr_la_SOURCES = fdr.c +cairo_fdr_la_CPPFLAGS = $(AM_CPPFLAGS) +cairo_fdr_la_CFLAGS = $(CAIRO_CFLAGS) +cairo_fdr_la_LDFLAGS = -module -no-undefined +cairo_fdr_la_LIBADD = -ldl diff --git a/util/cairo-fdr/fdr.c b/util/cairo-fdr/fdr.c new file mode 100644 index 000000000..467547313 --- /dev/null +++ b/util/cairo-fdr/fdr.c @@ -0,0 +1,304 @@ +/* cairo-fdr - a 'flight data recorder', a black box, for cairo + * + * Copyright © 2009 Chris Wilson + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#define _GNU_SOURCE + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include +#include +#include +#include +#include + +#include + +static void *_dlhandle = RTLD_NEXT; +#define DLCALL(name, args...) ({ \ + static typeof (&name) name##_real; \ + if (name##_real == NULL) { \ + name##_real = dlsym (_dlhandle, #name); \ + if (name##_real == NULL && _dlhandle == RTLD_NEXT) { \ + _dlhandle = dlopen ("libcairo.so", RTLD_LAZY); \ + name##_real = dlsym (_dlhandle, #name); \ + assert (name##_real != NULL); \ + } \ + } \ + (*name##_real) (args); \ +}) + +#define RINGBUFFER_SIZE 16 +static cairo_surface_t *fdr_ringbuffer[RINGBUFFER_SIZE]; +static int fdr_position; +static int fdr_dump; + +static const cairo_user_data_key_t fdr_key; + +static void +fdr_replay_to_script (cairo_surface_t *meta, cairo_script_context_t *ctx) +{ + if (meta != NULL) { + DLCALL (cairo_script_context_write_comment, ctx, "--- fdr ---", -1); + DLCALL (cairo_script_from_meta_surface, ctx, meta); + } +} + +static void +fdr_dump_ringbuffer (void) +{ + cairo_script_context_t *ctx; + int n; + + ctx = DLCALL (cairo_script_context_create, "/tmp/fdr.trace"); + + for (n = fdr_position; n < RINGBUFFER_SIZE; n++) + fdr_replay_to_script (fdr_ringbuffer[n], ctx); + + for (n = 0; n < fdr_position; n++) + fdr_replay_to_script (fdr_ringbuffer[n], ctx); + + DLCALL (cairo_script_context_destroy, ctx); +} + +static void +fdr_sighandler (int sig) +{ + fdr_dump = 1; +} + +static void +fdr_atexit (void) +{ + if (fdr_dump) + fdr_dump_ringbuffer (); +} + +static void +fdr_pending_signals (void) +{ + static int initialized; + + if (! initialized) { + initialized = 1; + + signal (SIGUSR1, fdr_sighandler); + atexit (fdr_atexit); + } + + if (fdr_dump) { + fdr_dump_ringbuffer (); + fdr_dump = 0; + } +} + +static void +fdr_get_extents (cairo_surface_t *surface, + cairo_rectangle_t *extents) +{ + cairo_t *cr; + + cr = DLCALL (cairo_create, surface); + DLCALL (cairo_clip_extents, cr, + &extents->x, &extents->y, &extents->width, &extents->height); + DLCALL (cairo_destroy, cr); + + extents->width -= extents->x; + extents->height -= extents->y; +} + +static void +fdr_surface_destroy (void *surface) +{ + DLCALL (cairo_surface_destroy, surface); +} + +static void +fdr_surface_reference (void *surface) +{ + DLCALL (cairo_surface_reference, surface); +} + +static cairo_surface_t * +fdr_surface_get_tee (cairo_surface_t *surface) +{ + return DLCALL (cairo_surface_get_user_data, surface, &fdr_key); +} + +static cairo_surface_t * +fdr_tee_surface_index (cairo_surface_t *surface, int index) +{ + return DLCALL (cairo_tee_surface_index, surface, index); +} + +cairo_t * +cairo_create (cairo_surface_t *surface) +{ + cairo_surface_t *record, *tee; + + fdr_pending_signals (); + + tee = fdr_surface_get_tee (surface); + if (tee == NULL) { + cairo_rectangle_t extents; + cairo_content_t content; + + fdr_get_extents (surface, &extents); + content = DLCALL (cairo_surface_get_content, surface); + + tee = DLCALL (cairo_tee_surface_create, surface); + record = DLCALL (cairo_meta_surface_create, content, &extents); + DLCALL (cairo_tee_surface_append, tee, record); + + DLCALL (cairo_surface_set_user_data, surface, + &fdr_key, tee, fdr_surface_destroy); + } else { + int n; + + record = fdr_tee_surface_index (tee, 1); + + /* update the position of the recording surface in the ringbuffer */ + for (n = 0; n < RINGBUFFER_SIZE; n++) { + if (record == fdr_ringbuffer[n]) { + fdr_ringbuffer[n] = NULL; + break; + } + } + } + + fdr_surface_destroy (fdr_ringbuffer[fdr_position]); + fdr_ringbuffer[fdr_position] = record; + fdr_position = (fdr_position + 1) % RINGBUFFER_SIZE; + + return DLCALL (cairo_create, tee); +} + +static void +fdr_remove_tee (cairo_surface_t *surface) +{ + fdr_surface_reference (surface); + DLCALL (cairo_surface_set_user_data, surface, &fdr_key, NULL, NULL); + fdr_surface_destroy (surface); +} + +void +cairo_destroy (cairo_t *cr) +{ + cairo_surface_t *tee; + + tee = DLCALL (cairo_get_target, cr); + DLCALL (cairo_destroy, cr); + + if (DLCALL (cairo_surface_get_reference_count, tee) == 1) + fdr_remove_tee (fdr_tee_surface_index (tee, 0)); +} + +void +cairo_pattern_destroy (cairo_pattern_t *pattern) +{ + if (DLCALL (cairo_pattern_get_type, pattern) == CAIRO_PATTERN_TYPE_SURFACE) { + cairo_surface_t *surface; + + if (DLCALL (cairo_pattern_get_surface, pattern, &surface) == CAIRO_STATUS_SUCCESS && + DLCALL (cairo_surface_get_type, surface) == CAIRO_SURFACE_TYPE_TEE && + DLCALL (cairo_surface_get_reference_count, surface) == 2) + { + fdr_remove_tee (fdr_tee_surface_index (surface, 0)); + } + } + + DLCALL (cairo_pattern_destroy, pattern); +} + +cairo_surface_t * +cairo_get_target (cairo_t *cr) +{ + cairo_surface_t *tee; + + tee = DLCALL (cairo_get_target, cr); + return fdr_tee_surface_index (tee, 0); +} + +cairo_surface_t * +cairo_get_group_target (cairo_t *cr) +{ + cairo_surface_t *tee; + + tee = DLCALL (cairo_get_group_target, cr); + return fdr_tee_surface_index (tee, 0); +} + +cairo_pattern_t * +cairo_pattern_create_for_surface (cairo_surface_t *surface) +{ + cairo_surface_t *tee; + + tee = fdr_surface_get_tee (surface); + if (tee != NULL) + surface = tee; + + return DLCALL (cairo_pattern_create_for_surface, surface); +} + +cairo_status_t +cairo_pattern_get_surface (cairo_pattern_t *pattern, + cairo_surface_t **surface) +{ + cairo_status_t status; + cairo_surface_t *tee; + + status = DLCALL (cairo_pattern_get_surface, pattern, surface); + if (status != CAIRO_STATUS_SUCCESS) + return status; + + tee = fdr_surface_get_tee (*surface); + if (tee != NULL) + *surface = tee; + + return CAIRO_STATUS_SUCCESS; +} + +void +cairo_set_source_surface (cairo_t *cr, + cairo_surface_t *surface, + double x, double y) +{ + cairo_surface_t *tee; + + tee = fdr_surface_get_tee (surface); + if (tee != NULL) + surface = tee; + + DLCALL (cairo_set_source_surface, cr, surface, x, y); +} + +cairo_surface_t * +cairo_surface_create_similar (cairo_surface_t *surface, + cairo_content_t content, + int width, int height) +{ + cairo_surface_t *tee; + + tee = fdr_surface_get_tee (surface); + if (tee != NULL) + surface = tee; + + return DLCALL (cairo_surface_create_similar, + surface, content, width, height); +}