[cairo-spans] New abstract types for scan converting polygons.

A cairo_span_renderer_t implementation can be provided by a surface if
it wants to render paths as horizontal spans of the alpha component of
a mask.  Its job is to composite a source pattern to the destination
surface when given spans of alpha coverage for a row while taking care
of backend specific clipping.

A cairo_scan_converter_t takes edges of a flattened path and generates
spans for a span renderer to render.
This commit is contained in:
M Joonas Pihlaja 2008-10-24 17:37:30 +03:00
parent 4b227143b3
commit 948c3526dc
4 changed files with 369 additions and 0 deletions

View file

@ -81,6 +81,7 @@ cairo_private = \
cairo-region-private.h \
cairo-scaled-font-private.h \
cairo-skiplist-private.h \
cairo-spans-private.h \
cairo-surface-fallback-private.h \
cairo-surface-private.h \
cairo-types-private.h \
@ -132,6 +133,7 @@ cairo_sources = \
cairo-scaled-font.c \
cairo-skiplist.c \
cairo-slope.c \
cairo-spans.c \
cairo-spline.c \
cairo-stroke-style.c \
cairo-surface.c \

124
src/cairo-spans-private.h Normal file
View file

@ -0,0 +1,124 @@
/* -*- Mode: c; tab-width: 8; c-basic-offset: 4; indent-tabs-mode: t; -*- */
/* cairo - a vector graphics library with display and print output
*
* Copyright (c) 2008 M Joonas Pihlaja
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/
#ifndef CAIRO_SPANS_PRIVATE_H
#define CAIRO_SPANS_PRIVATE_H
#include "cairo-types-private.h"
#include "cairo-compiler-private.h"
/* Number of bits of precision used for alpha. */
#define CAIRO_SPANS_UNIT_COVERAGE_BITS 8
#define CAIRO_SPANS_UNIT_COVERAGE ((1 << CAIRO_SPANS_UNIT_COVERAGE_BITS)-1)
/* A structure representing an open-ended horizontal span of constant
* pixel coverage. */
typedef struct _cairo_half_open_span {
/* The inclusive x-coordinate of the start of the span. */
int x;
/* The pixel coverage for the pixels to the right. */
int coverage;
} cairo_half_open_span_t;
/* Span renderer interface. Instances of renderers are provided by
* surfaces if they want to composite spans instead of trapezoids. */
typedef struct _cairo_span_renderer cairo_span_renderer_t;
struct _cairo_span_renderer {
/* Called to destroy the renderer. */
cairo_destroy_func_t destroy;
/* Render the spans on row y of the source by whatever compositing
* method is required. The function should ignore spans outside
* the bounding box set by the init() function. */
cairo_status_t (*render_row)(
void *abstract_renderer,
int y,
const cairo_half_open_span_t *coverages,
unsigned num_coverages);
/* Called after all rows have been rendered to perform whatever
* final rendering step is required. This function is called just
* once before the renderer is destroyed. */
cairo_status_t (*finish)(
void *abstract_renderer);
/* Private status variable. */
cairo_status_t status;
};
/* Scan converter interface. */
typedef struct _cairo_scan_converter cairo_scan_converter_t;
struct _cairo_scan_converter {
/* Destroy this scan converter. */
cairo_destroy_func_t destroy;
/* Add an edge to the converter. */
cairo_status_t
(*add_edge)(
void *abstract_converter,
cairo_fixed_t x1,
cairo_fixed_t y1,
cairo_fixed_t x2,
cairo_fixed_t y2);
/* Generates coverage spans for rows for the added edges and calls
* the renderer function for each row. After generating spans the
* only valid thing to do with the converter is to destroy it. */
cairo_status_t
(*generate)(
void *abstract_converter,
cairo_span_renderer_t *renderer);
/* Private status. Read with _cairo_scan_converter_status(). */
cairo_status_t status;
};
/* cairo-spans.c: */
cairo_private cairo_scan_converter_t *
_cairo_scan_converter_create_in_error (cairo_status_t error);
cairo_private cairo_status_t
_cairo_scan_converter_status (void *abstract_converter);
cairo_private cairo_status_t
_cairo_scan_converter_set_error (void *abstract_converter,
cairo_status_t error);
cairo_private cairo_span_renderer_t *
_cairo_span_renderer_create_in_error (cairo_status_t error);
cairo_private cairo_status_t
_cairo_span_renderer_status (void *abstract_renderer);
/* Set the renderer into an error state. This sets all the method
* pointers except ->destroy() of the renderer to no-op
* implementations that just return the error status. */
cairo_private cairo_status_t
_cairo_span_renderer_set_error (void *abstract_renderer,
cairo_status_t error);
#endif /* CAIRO_SPANS_PRIVATE_H */

242
src/cairo-spans.c Normal file
View file

@ -0,0 +1,242 @@
/* -*- Mode: c; tab-width: 8; c-basic-offset: 4; indent-tabs-mode: t; -*- */
/* cairo - a vector graphics library with display and print output
*
* Copyright (c) 2008 M Joonas Pihlaja
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/
#include "cairoint.h"
static void
_cairo_nil_destroy (void *abstract)
{
(void) abstract;
}
static cairo_status_t
_cairo_nil_scan_converter_add_edge (void *abstract_converter,
cairo_fixed_t x1,
cairo_fixed_t y1,
cairo_fixed_t x2,
cairo_fixed_t y2)
{
(void) abstract_converter;
(void) x1;
(void) y1;
(void) x2;
(void) y2;
return _cairo_scan_converter_status (abstract_converter);
}
static cairo_status_t
_cairo_nil_scan_converter_generate (void *abstract_converter,
cairo_span_renderer_t *renderer)
{
(void) abstract_converter;
(void) renderer;
return _cairo_scan_converter_status (abstract_converter);
}
cairo_status_t
_cairo_scan_converter_status (void *abstract_converter)
{
cairo_scan_converter_t *converter = abstract_converter;
return converter->status;
}
cairo_status_t
_cairo_scan_converter_set_error (void *abstract_converter,
cairo_status_t error)
{
cairo_scan_converter_t *converter = abstract_converter;
if (error == CAIRO_STATUS_SUCCESS)
ASSERT_NOT_REACHED;
if (converter->status == CAIRO_STATUS_SUCCESS) {
converter->add_edge = _cairo_nil_scan_converter_add_edge;
converter->generate = _cairo_nil_scan_converter_generate;
converter->status = error;
}
return converter->status;
}
static void
_cairo_nil_scan_converter_init (cairo_scan_converter_t *converter,
cairo_status_t status)
{
converter->destroy = _cairo_nil_destroy;
converter->status = CAIRO_STATUS_SUCCESS;
status = _cairo_scan_converter_set_error (converter, status);
}
cairo_scan_converter_t *
_cairo_scan_converter_create_in_error (cairo_status_t status)
{
#define RETURN_NIL {\
static cairo_scan_converter_t nil;\
_cairo_nil_scan_converter_init (&nil, status);\
return &nil;\
}
switch (status) {
case CAIRO_STATUS_SUCCESS:
ASSERT_NOT_REACHED;
break;
case CAIRO_STATUS_INVALID_RESTORE: RETURN_NIL;
case CAIRO_STATUS_INVALID_POP_GROUP: RETURN_NIL;
case CAIRO_STATUS_NO_CURRENT_POINT: RETURN_NIL;
case CAIRO_STATUS_INVALID_MATRIX: RETURN_NIL;
case CAIRO_STATUS_INVALID_STATUS: RETURN_NIL;
case CAIRO_STATUS_NULL_POINTER: RETURN_NIL;
case CAIRO_STATUS_INVALID_STRING: RETURN_NIL;
case CAIRO_STATUS_INVALID_PATH_DATA: RETURN_NIL;
case CAIRO_STATUS_READ_ERROR: RETURN_NIL;
case CAIRO_STATUS_WRITE_ERROR: RETURN_NIL;
case CAIRO_STATUS_SURFACE_FINISHED: RETURN_NIL;
case CAIRO_STATUS_SURFACE_TYPE_MISMATCH: RETURN_NIL;
case CAIRO_STATUS_PATTERN_TYPE_MISMATCH: RETURN_NIL;
case CAIRO_STATUS_INVALID_CONTENT: RETURN_NIL;
case CAIRO_STATUS_INVALID_FORMAT: RETURN_NIL;
case CAIRO_STATUS_INVALID_VISUAL: RETURN_NIL;
case CAIRO_STATUS_FILE_NOT_FOUND: RETURN_NIL;
case CAIRO_STATUS_INVALID_DASH: RETURN_NIL;
case CAIRO_STATUS_INVALID_DSC_COMMENT: RETURN_NIL;
case CAIRO_STATUS_INVALID_INDEX: RETURN_NIL;
case CAIRO_STATUS_CLIP_NOT_REPRESENTABLE: RETURN_NIL;
case CAIRO_STATUS_TEMP_FILE_ERROR: RETURN_NIL;
case CAIRO_STATUS_INVALID_STRIDE: RETURN_NIL;
case CAIRO_STATUS_FONT_TYPE_MISMATCH: RETURN_NIL;
case CAIRO_STATUS_USER_FONT_IMMUTABLE: RETURN_NIL;
case CAIRO_STATUS_USER_FONT_ERROR: RETURN_NIL;
case CAIRO_STATUS_NEGATIVE_COUNT: RETURN_NIL;
case CAIRO_STATUS_INVALID_CLUSTERS: RETURN_NIL;
case CAIRO_STATUS_INVALID_SLANT: RETURN_NIL;
case CAIRO_STATUS_INVALID_WEIGHT: RETURN_NIL;
case CAIRO_STATUS_NO_MEMORY: RETURN_NIL;
default:
break;
}
status = CAIRO_STATUS_NO_MEMORY;
RETURN_NIL;
#undef RETURN_NIL
}
static cairo_status_t
_cairo_nil_span_renderer_render_row (
void *abstract_renderer,
int y,
const cairo_half_open_span_t *coverages,
unsigned num_coverages)
{
(void) y;
(void) coverages;
(void) num_coverages;
return _cairo_span_renderer_status (abstract_renderer);
}
static cairo_status_t
_cairo_nil_span_renderer_finish (void *abstract_renderer)
{
return _cairo_span_renderer_status (abstract_renderer);
}
cairo_status_t
_cairo_span_renderer_status (void *abstract_renderer)
{
cairo_span_renderer_t *renderer = abstract_renderer;
return renderer->status;
}
cairo_status_t
_cairo_span_renderer_set_error (
void *abstract_renderer,
cairo_status_t error)
{
cairo_span_renderer_t *renderer = abstract_renderer;
if (error == CAIRO_STATUS_SUCCESS) {
ASSERT_NOT_REACHED;
}
if (renderer->status == CAIRO_STATUS_SUCCESS) {
renderer->render_row = _cairo_nil_span_renderer_render_row;
renderer->finish = _cairo_nil_span_renderer_finish;
renderer->status = error;
}
return renderer->status;
}
static void
_cairo_nil_span_renderer_init (cairo_span_renderer_t *renderer,
cairo_status_t status)
{
renderer->destroy = _cairo_nil_destroy;
renderer->status = CAIRO_STATUS_SUCCESS;
status = _cairo_span_renderer_set_error (renderer, status);
}
cairo_span_renderer_t *
_cairo_span_renderer_create_in_error (cairo_status_t status)
{
#define RETURN_NIL {\
static cairo_span_renderer_t nil;\
_cairo_nil_span_renderer_init (&nil, status);\
return &nil;\
}
switch (status) {
case CAIRO_STATUS_SUCCESS:
ASSERT_NOT_REACHED;
break;
case CAIRO_STATUS_INVALID_RESTORE: RETURN_NIL;
case CAIRO_STATUS_INVALID_POP_GROUP: RETURN_NIL;
case CAIRO_STATUS_NO_CURRENT_POINT: RETURN_NIL;
case CAIRO_STATUS_INVALID_MATRIX: RETURN_NIL;
case CAIRO_STATUS_INVALID_STATUS: RETURN_NIL;
case CAIRO_STATUS_NULL_POINTER: RETURN_NIL;
case CAIRO_STATUS_INVALID_STRING: RETURN_NIL;
case CAIRO_STATUS_INVALID_PATH_DATA: RETURN_NIL;
case CAIRO_STATUS_READ_ERROR: RETURN_NIL;
case CAIRO_STATUS_WRITE_ERROR: RETURN_NIL;
case CAIRO_STATUS_SURFACE_FINISHED: RETURN_NIL;
case CAIRO_STATUS_SURFACE_TYPE_MISMATCH: RETURN_NIL;
case CAIRO_STATUS_PATTERN_TYPE_MISMATCH: RETURN_NIL;
case CAIRO_STATUS_INVALID_CONTENT: RETURN_NIL;
case CAIRO_STATUS_INVALID_FORMAT: RETURN_NIL;
case CAIRO_STATUS_INVALID_VISUAL: RETURN_NIL;
case CAIRO_STATUS_FILE_NOT_FOUND: RETURN_NIL;
case CAIRO_STATUS_INVALID_DASH: RETURN_NIL;
case CAIRO_STATUS_INVALID_DSC_COMMENT: RETURN_NIL;
case CAIRO_STATUS_INVALID_INDEX: RETURN_NIL;
case CAIRO_STATUS_CLIP_NOT_REPRESENTABLE: RETURN_NIL;
case CAIRO_STATUS_TEMP_FILE_ERROR: RETURN_NIL;
case CAIRO_STATUS_INVALID_STRIDE: RETURN_NIL;
case CAIRO_STATUS_FONT_TYPE_MISMATCH: RETURN_NIL;
case CAIRO_STATUS_USER_FONT_IMMUTABLE: RETURN_NIL;
case CAIRO_STATUS_USER_FONT_ERROR: RETURN_NIL;
case CAIRO_STATUS_NEGATIVE_COUNT: RETURN_NIL;
case CAIRO_STATUS_INVALID_CLUSTERS: RETURN_NIL;
case CAIRO_STATUS_INVALID_SLANT: RETURN_NIL;
case CAIRO_STATUS_INVALID_WEIGHT: RETURN_NIL;
case CAIRO_STATUS_NO_MEMORY: RETURN_NIL;
default:
break;
}
status = CAIRO_STATUS_NO_MEMORY;
RETURN_NIL;
#undef RETURN_NIL
}

View file

@ -234,6 +234,7 @@ be32_to_cpu(uint32_t v)
#include "cairo-types-private.h"
#include "cairo-cache-private.h"
#include "cairo-reference-count-private.h"
#include "cairo-spans-private.h"
cairo_private void
_cairo_box_from_doubles (cairo_box_t *box,