📄 cairo-meta-surface.c
字号:
/* cairo - a vector graphics library with display and print output * * Copyright © 2005 Red Hat, Inc * * This library is free software; you can redistribute it and/or * modify it either under the terms of the GNU Lesser General Public * License version 2.1 as published by the Free Software Foundation * (the "LGPL") or, at your option, under the terms of the Mozilla * Public License Version 1.1 (the "MPL"). If you do not alter this * notice, a recipient may use your version of this file under either * the MPL or the LGPL. * * You should have received a copy of the LGPL along with this library * in the file COPYING-LGPL-2.1; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * You should have received a copy of the MPL along with this library * in the file COPYING-MPL-1.1 * * The contents of this file are subject to the Mozilla Public License * Version 1.1 (the "License"); you may not use this file except in * compliance with the License. You may obtain a copy of the License at * http://www.mozilla.org/MPL/ * * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY * OF ANY KIND, either express or implied. See the LGPL or the MPL for * the specific language governing rights and limitations. * * The Original Code is the cairo graphics library. * * The Initial Developer of the Original Code is Red Hat, Inc. * * Contributor(s): * Kristian Høgsberg <krh@redhat.com> * Carl Worth <cworth@cworth.org> *//* A meta surface is a surface that records all drawing operations at * the highest level of the surface backend interface, (that is, the * level of paint, mask, stroke, fill, and show_glyphs). The meta * surface can then be "replayed" against any target surface with: * * _cairo_meta_surface_replay (meta, target); * * after which the results in target will be identical to the results * that would have been obtained if the original operations applied to * the meta surface had instead been applied to the target surface. * * The recording phase of the meta surface is careful to snapshot all * necessary objects (paths, patterns, etc.), in order to acheive * accurate replay. The efficiency of the meta surface could be * improved by improving the implementation of snapshot for the * various objects. For example, it would be nice to have a * copy-on-write implementation for _cairo_surface_snapshot. */#include "cairoint.h"#include "cairo-meta-surface-private.h"#include "cairo-clip-private.h"static const cairo_surface_backend_t cairo_meta_surface_backend;/* Currently all meta surfaces do have a size which should be passed * in as the maximum size of any target surface against which the * meta-surface will ever be replayed. * * XXX: The naming of "pixels" in the size here is a misnomer. It's * actually a size in whatever device-space units are desired (again, * according to the intended replay target). This should likely also * be changed to use doubles not ints. */cairo_surface_t *_cairo_meta_surface_create (cairo_content_t content, int width_pixels, int height_pixels){ cairo_meta_surface_t *meta; meta = malloc (sizeof (cairo_meta_surface_t)); if (meta == NULL) { _cairo_error (CAIRO_STATUS_NO_MEMORY); return (cairo_surface_t*) &_cairo_surface_nil; } _cairo_surface_init (&meta->base, &cairo_meta_surface_backend, content); meta->content = content; meta->width_pixels = width_pixels; meta->height_pixels = height_pixels; _cairo_array_init (&meta->commands, sizeof (cairo_command_t *)); meta->commands_owner = NULL; meta->is_clipped = FALSE; meta->replay_start_idx = 0; return &meta->base;}static cairo_surface_t *_cairo_meta_surface_create_similar (void *abstract_surface, cairo_content_t content, int width, int height){ return _cairo_meta_surface_create (content, width, height);}static cairo_status_t_cairo_meta_surface_finish (void *abstract_surface){ cairo_meta_surface_t *meta = abstract_surface; cairo_command_t *command; cairo_command_t **elements; int i, num_elements; if (meta->commands_owner) { cairo_surface_destroy (meta->commands_owner); return CAIRO_STATUS_SUCCESS; } num_elements = meta->commands.num_elements; elements = _cairo_array_index (&meta->commands, 0); for (i = 0; i < num_elements; i++) { command = elements[i]; switch (command->type) { /* 5 basic drawing operations */ case CAIRO_COMMAND_PAINT: _cairo_pattern_fini (&command->paint.source.base); free (command); break; case CAIRO_COMMAND_MASK: _cairo_pattern_fini (&command->mask.source.base); _cairo_pattern_fini (&command->mask.mask.base); free (command); break; case CAIRO_COMMAND_STROKE: _cairo_pattern_fini (&command->stroke.source.base); _cairo_path_fixed_fini (&command->stroke.path); _cairo_stroke_style_fini (&command->stroke.style); free (command); break; case CAIRO_COMMAND_FILL: _cairo_pattern_fini (&command->fill.source.base); _cairo_path_fixed_fini (&command->fill.path); free (command); break; case CAIRO_COMMAND_SHOW_GLYPHS: _cairo_pattern_fini (&command->show_glyphs.source.base); free (command->show_glyphs.glyphs); cairo_scaled_font_destroy (command->show_glyphs.scaled_font); free (command); break; /* Other junk. */ case CAIRO_COMMAND_INTERSECT_CLIP_PATH: if (command->intersect_clip_path.path_pointer) _cairo_path_fixed_fini (&command->intersect_clip_path.path); free (command); break; default: ASSERT_NOT_REACHED; } } _cairo_array_fini (&meta->commands); return CAIRO_STATUS_SUCCESS;}static cairo_status_t_cairo_meta_surface_acquire_source_image (void *abstract_surface, cairo_image_surface_t **image_out, void **image_extra){ cairo_status_t status; cairo_meta_surface_t *surface = abstract_surface; cairo_surface_t *image; image = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, surface->width_pixels, surface->height_pixels); status = _cairo_meta_surface_replay (&surface->base, image); if (status) { cairo_surface_destroy (image); return status; } *image_out = (cairo_image_surface_t *) image; *image_extra = NULL; return status;}static void_cairo_meta_surface_release_source_image (void *abstract_surface, cairo_image_surface_t *image, void *image_extra){ cairo_surface_destroy (&image->base);}static cairo_status_t_init_pattern_with_snapshot (cairo_pattern_t *pattern, const cairo_pattern_t *other){ _cairo_pattern_init_copy (pattern, other); if (pattern->type == CAIRO_PATTERN_TYPE_SURFACE) { cairo_surface_pattern_t *surface_pattern = (cairo_surface_pattern_t *) pattern; cairo_surface_t *surface = surface_pattern->surface; surface_pattern->surface = _cairo_surface_snapshot (surface); cairo_surface_destroy (surface); if (surface_pattern->surface->status) return surface_pattern->surface->status; } return CAIRO_STATUS_SUCCESS;}static cairo_int_status_t_cairo_meta_surface_paint (void *abstract_surface, cairo_operator_t op, cairo_pattern_t *source){ cairo_status_t status; cairo_meta_surface_t *meta = abstract_surface; cairo_command_paint_t *command; /* An optimisation that takes care to not replay what was done * before surface is cleared. We don't erase recorded commands * since we may have earlier snapshots of this surface. */ if (op == CAIRO_OPERATOR_CLEAR && !meta->is_clipped) meta->replay_start_idx = meta->commands.num_elements; command = malloc (sizeof (cairo_command_paint_t)); if (command == NULL) return CAIRO_STATUS_NO_MEMORY; command->type = CAIRO_COMMAND_PAINT; command->op = op; status = _init_pattern_with_snapshot (&command->source.base, source); if (status) goto CLEANUP_COMMAND; status = _cairo_array_append (&meta->commands, &command); if (status) goto CLEANUP_SOURCE; return CAIRO_STATUS_SUCCESS; CLEANUP_SOURCE: _cairo_pattern_fini (&command->source.base); CLEANUP_COMMAND: free (command); return status;}static cairo_int_status_t_cairo_meta_surface_mask (void *abstract_surface, cairo_operator_t op, cairo_pattern_t *source, cairo_pattern_t *mask){ cairo_status_t status; cairo_meta_surface_t *meta = abstract_surface; cairo_command_mask_t *command; command = malloc (sizeof (cairo_command_mask_t)); if (command == NULL) return CAIRO_STATUS_NO_MEMORY; command->type = CAIRO_COMMAND_MASK; command->op = op; status = _init_pattern_with_snapshot (&command->source.base, source); if (status) goto CLEANUP_COMMAND; status = _init_pattern_with_snapshot (&command->mask.base, mask); if (status) goto CLEANUP_SOURCE; status = _cairo_array_append (&meta->commands, &command); if (status) goto CLEANUP_MASK; return CAIRO_STATUS_SUCCESS; CLEANUP_MASK: _cairo_pattern_fini (&command->mask.base); CLEANUP_SOURCE: _cairo_pattern_fini (&command->source.base); CLEANUP_COMMAND: free (command); return status;}static cairo_int_status_t_cairo_meta_surface_stroke (void *abstract_surface, cairo_operator_t op, cairo_pattern_t *source, cairo_path_fixed_t *path, cairo_stroke_style_t *style, cairo_matrix_t *ctm, cairo_matrix_t *ctm_inverse, double tolerance, cairo_antialias_t antialias){ cairo_status_t status; cairo_meta_surface_t *meta = abstract_surface; cairo_command_stroke_t *command; command = malloc (sizeof (cairo_command_stroke_t)); if (command == NULL) return CAIRO_STATUS_NO_MEMORY; command->type = CAIRO_COMMAND_STROKE; command->op = op; status = _init_pattern_with_snapshot (&command->source.base, source); if (status) goto CLEANUP_COMMAND; status = _cairo_path_fixed_init_copy (&command->path, path); if (status) goto CLEANUP_SOURCE; status = _cairo_stroke_style_init_copy (&command->style, style); if (status) goto CLEANUP_PATH; command->ctm = *ctm; command->ctm_inverse = *ctm_inverse; command->tolerance = tolerance; command->antialias = antialias; status = _cairo_array_append (&meta->commands, &command); if (status) goto CLEANUP_STYLE; return CAIRO_STATUS_SUCCESS; CLEANUP_STYLE: _cairo_stroke_style_fini (&command->style); CLEANUP_PATH: _cairo_path_fixed_fini (&command->path); CLEANUP_SOURCE: _cairo_pattern_fini (&command->source.base); CLEANUP_COMMAND: free (command);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -