1 /* -*- Mode: c; tab-width: 8; c-basic-offset: 4; indent-tabs-mode: t; -*- */
2 /* cairo - a vector graphics library with display and print output
4 * Copyright © 2011 Intel Corporation
6 * This library is free software; you can redistribute it and/or
7 * modify it either under the terms of the GNU Lesser General Public
8 * License version 2.1 as published by the Free Software Foundation
9 * (the "LGPL") or, at your option, under the terms of the Mozilla
10 * Public License Version 1.1 (the "MPL"). If you do not alter this
11 * notice, a recipient may use your version of this file under either
12 * the MPL or the LGPL.
14 * You should have received a copy of the LGPL along with this library
15 * in the file COPYING-LGPL-2.1; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA
17 * You should have received a copy of the MPL along with this library
18 * in the file COPYING-MPL-1.1
20 * The contents of this file are subject to the Mozilla Public License
21 * Version 1.1 (the "License"); you may not use this file except in
22 * compliance with the License. You may obtain a copy of the License at
23 * http://www.mozilla.org/MPL/
25 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY
26 * OF ANY KIND, either express or implied. See the LGPL or the MPL for
27 * the specific language governing rights and limitations.
29 * The Original Code is the cairo graphics library.
31 * The Initial Developer of the Original Code is Red Hat, Inc.
34 * Chris Wilson <chris@chris-wilson.co.uk>
38 #include "cairo-error-private.h"
39 #include "cairo-pattern-private.h"
42 * SECTION:cairo-raster-source
43 * @Title: Raster Sources
44 * @Short_Description: Supplying arbitrary image data
45 * @See_Also: #cairo_pattern_t
47 * The raster source provides the ability to supply arbitrary pixel data
48 * whilst rendering. The pixels are queried at the time of rasterisation
49 * by means of user callback functions, allowing for the ultimate
50 * flexibility. For example, in handling compressed image sources, you
51 * may keep a MRU cache of decompressed images and decompress sources on the
52 * fly and discard old ones to conserve memory.
54 * For the raster source to be effective, you must at least specify
55 * the acquire and release callbacks which are used to retrieve the pixel
56 * data for the region of interest and demark when it can be freed afterwards.
57 * Other callbacks are provided for when the pattern is copied temporarily
58 * during rasterisation, or more permanently as a snapshot in order to keep
59 * the pixel data available for printing.
65 _cairo_raster_source_pattern_acquire (const cairo_pattern_t *abstract_pattern,
66 cairo_surface_t *target,
67 const cairo_rectangle_int_t *extents)
69 cairo_raster_source_pattern_t *pattern =
70 (cairo_raster_source_pattern_t *) abstract_pattern;
72 if (pattern->acquire == NULL)
76 extents = &pattern->extents;
78 return pattern->acquire (&pattern->base, pattern->user_data,
83 _cairo_raster_source_pattern_release (const cairo_pattern_t *abstract_pattern,
84 cairo_surface_t *surface)
86 cairo_raster_source_pattern_t *pattern =
87 (cairo_raster_source_pattern_t *) abstract_pattern;
89 if (pattern->release == NULL)
92 pattern->release (&pattern->base, pattern->user_data, surface);
96 _cairo_raster_source_pattern_init_copy (cairo_pattern_t *abstract_pattern,
97 const cairo_pattern_t *other)
99 cairo_raster_source_pattern_t *pattern =
100 (cairo_raster_source_pattern_t *) abstract_pattern;
101 cairo_status_t status;
103 VG (VALGRIND_MAKE_MEM_UNDEFINED (pattern, sizeof (cairo_raster_source_pattern_t)));
104 memcpy(pattern, other, sizeof (cairo_raster_source_pattern_t));
106 status = CAIRO_STATUS_SUCCESS;
108 status = pattern->copy (&pattern->base, pattern->user_data, other);
114 _cairo_raster_source_pattern_snapshot (cairo_pattern_t *abstract_pattern)
116 cairo_raster_source_pattern_t *pattern =
117 (cairo_raster_source_pattern_t *) abstract_pattern;
119 if (pattern->snapshot == NULL)
120 return CAIRO_STATUS_SUCCESS;
122 return pattern->snapshot (&pattern->base, pattern->user_data);
126 _cairo_raster_source_pattern_finish (cairo_pattern_t *abstract_pattern)
128 cairo_raster_source_pattern_t *pattern =
129 (cairo_raster_source_pattern_t *) abstract_pattern;
131 if (pattern->finish == NULL)
134 pattern->finish (&pattern->base, pattern->user_data);
137 /* Public interface */
140 * cairo_pattern_create_raster_source:
141 * @user_data: the user data to be passed to all callbacks
142 * @content: content type for the pixel data that will be returned. Knowing
143 * the content type ahead of time is used for analysing the operation and
144 * picking the appropriate rendering path.
145 * @width: maximum size of the sample area
146 * @height: maximum size of the sample area
148 * Creates a new user pattern for providing pixel data.
150 * Use the setter functions to associate callbacks with the returned
151 * pattern. The only mandatory callback is acquire.
153 * Return value: a newly created #cairo_pattern_t. Free with
154 * cairo_pattern_destroy() when you are done using it.
159 cairo_pattern_create_raster_source (void *user_data,
160 cairo_content_t content,
161 int width, int height)
163 cairo_raster_source_pattern_t *pattern;
165 CAIRO_MUTEX_INITIALIZE ();
167 if (width < 0 || height < 0)
168 return _cairo_pattern_create_in_error (CAIRO_STATUS_INVALID_SIZE);
170 if (! CAIRO_CONTENT_VALID (content))
171 return _cairo_pattern_create_in_error (CAIRO_STATUS_INVALID_CONTENT);
173 pattern = calloc (1, sizeof (*pattern));
174 if (unlikely (pattern == NULL))
175 return _cairo_pattern_create_in_error (CAIRO_STATUS_NO_MEMORY);
177 _cairo_pattern_init (&pattern->base,
178 CAIRO_PATTERN_TYPE_RASTER_SOURCE);
179 CAIRO_REFERENCE_COUNT_INIT (&pattern->base.ref_count, 1);
181 pattern->content = content;
183 pattern->extents.x = 0;
184 pattern->extents.y = 0;
185 pattern->extents.width = width;
186 pattern->extents.height = height;
188 pattern->user_data = user_data;
190 return &pattern->base;
194 * cairo_raster_source_pattern_set_callback_data:
195 * @pattern: the pattern to update
196 * @data: the user data to be passed to all callbacks
198 * Updates the user data that is provided to all callbacks.
203 cairo_raster_source_pattern_set_callback_data (cairo_pattern_t *abstract_pattern,
206 cairo_raster_source_pattern_t *pattern;
208 if (abstract_pattern->type != CAIRO_PATTERN_TYPE_RASTER_SOURCE)
211 pattern = (cairo_raster_source_pattern_t *) abstract_pattern;
212 pattern->user_data = data;
216 * cairo_raster_source_pattern_get_callback_data:
217 * @pattern: the pattern to update
219 * Queries the current user data.
221 * Return value: the current user-data passed to each callback
226 cairo_raster_source_pattern_get_callback_data (cairo_pattern_t *abstract_pattern)
228 cairo_raster_source_pattern_t *pattern;
230 if (abstract_pattern->type != CAIRO_PATTERN_TYPE_RASTER_SOURCE)
233 pattern = (cairo_raster_source_pattern_t *) abstract_pattern;
234 return pattern->user_data;
238 * cairo_raster_source_pattern_set_acquire:
239 * @pattern: the pattern to update
240 * @acquire: acquire callback
241 * @release: release callback
243 * Specifies the callbacks used to generate the image surface for a rendering
244 * operation (acquire) and the function used to cleanup that surface afterwards.
246 * The @acquire callback should create a surface (preferably an image
247 * surface created to match the target using
248 * cairo_surface_create_similar_image()) that defines at least the region
249 * of interest specified by extents. The surface is allowed to be the entire
250 * sample area, but if it does contain a subsection of the sample area,
251 * the surface extents should be provided by setting the device offset (along
252 * with its width and height) using cairo_surface_set_device_offset().
257 cairo_raster_source_pattern_set_acquire (cairo_pattern_t *abstract_pattern,
258 cairo_raster_source_acquire_func_t acquire,
259 cairo_raster_source_release_func_t release)
261 cairo_raster_source_pattern_t *pattern;
263 if (abstract_pattern->type != CAIRO_PATTERN_TYPE_RASTER_SOURCE)
266 pattern = (cairo_raster_source_pattern_t *) abstract_pattern;
267 pattern->acquire = acquire;
268 pattern->release = release;
272 * cairo_raster_source_pattern_get_acquire:
273 * @pattern: the pattern to query
274 * @acquire: return value for the current acquire callback
275 * @release: return value for the current release callback
277 * Queries the current acquire and release callbacks.
282 cairo_raster_source_pattern_get_acquire (cairo_pattern_t *abstract_pattern,
283 cairo_raster_source_acquire_func_t *acquire,
284 cairo_raster_source_release_func_t *release)
286 cairo_raster_source_pattern_t *pattern;
288 if (abstract_pattern->type != CAIRO_PATTERN_TYPE_RASTER_SOURCE)
291 pattern = (cairo_raster_source_pattern_t *) abstract_pattern;
293 *acquire = pattern->acquire;
295 *release = pattern->release;
299 * cairo_raster_source_pattern_set_snapshot:
300 * @pattern: the pattern to update
301 * @snapshot: snapshot callback
303 * Sets the callback that will be used whenever a snapshot is taken of the
304 * pattern, that is whenever the current contents of the pattern should be
305 * preserved for later use. This is typically invoked whilst printing.
310 cairo_raster_source_pattern_set_snapshot (cairo_pattern_t *abstract_pattern,
311 cairo_raster_source_snapshot_func_t snapshot)
313 cairo_raster_source_pattern_t *pattern;
315 if (abstract_pattern->type != CAIRO_PATTERN_TYPE_RASTER_SOURCE)
318 pattern = (cairo_raster_source_pattern_t *) abstract_pattern;
319 pattern->snapshot = snapshot;
323 * cairo_raster_source_pattern_get_snapshot:
324 * @pattern: the pattern to query
326 * Queries the current snapshot callback.
328 * Return value: the current snapshot callback
332 cairo_raster_source_snapshot_func_t
333 cairo_raster_source_pattern_get_snapshot (cairo_pattern_t *abstract_pattern)
335 cairo_raster_source_pattern_t *pattern;
337 if (abstract_pattern->type != CAIRO_PATTERN_TYPE_RASTER_SOURCE)
340 pattern = (cairo_raster_source_pattern_t *) abstract_pattern;
341 return pattern->snapshot;
345 * cairo_raster_source_pattern_set_copy:
346 * @pattern: the pattern to update
347 * @copy: the copy callback
349 * Updates the copy callback which is used whenever a temporary copy of the
355 cairo_raster_source_pattern_set_copy (cairo_pattern_t *abstract_pattern,
356 cairo_raster_source_copy_func_t copy)
358 cairo_raster_source_pattern_t *pattern;
360 if (abstract_pattern->type != CAIRO_PATTERN_TYPE_RASTER_SOURCE)
363 pattern = (cairo_raster_source_pattern_t *) abstract_pattern;
364 pattern->copy = copy;
368 * cairo_raster_source_pattern_get_copy:
369 * @pattern: the pattern to query
371 * Queries the current copy callback.
373 * Return value: the current copy callback
377 cairo_raster_source_copy_func_t
378 cairo_raster_source_pattern_get_copy (cairo_pattern_t *abstract_pattern)
380 cairo_raster_source_pattern_t *pattern;
382 if (abstract_pattern->type != CAIRO_PATTERN_TYPE_RASTER_SOURCE)
385 pattern = (cairo_raster_source_pattern_t *) abstract_pattern;
386 return pattern->copy;
390 * cairo_raster_source_pattern_set_finish:
391 * @pattern: the pattern to update
392 * @finish: the finish callback
394 * Updates the finish callback which is used whenever a pattern (or a copy
395 * thereof) will no longer be used.
400 cairo_raster_source_pattern_set_finish (cairo_pattern_t *abstract_pattern,
401 cairo_raster_source_finish_func_t finish)
403 cairo_raster_source_pattern_t *pattern;
405 if (abstract_pattern->type != CAIRO_PATTERN_TYPE_RASTER_SOURCE)
408 pattern = (cairo_raster_source_pattern_t *) abstract_pattern;
409 pattern->finish = finish;
413 * cairo_raster_source_pattern_get_finish:
414 * @pattern: the pattern to query
416 * Queries the current finish callback.
418 * Return value: the current finish callback
422 cairo_raster_source_finish_func_t
423 cairo_raster_source_pattern_get_finish (cairo_pattern_t *abstract_pattern)
425 cairo_raster_source_pattern_t *pattern;
427 if (abstract_pattern->type != CAIRO_PATTERN_TYPE_RASTER_SOURCE)
430 pattern = (cairo_raster_source_pattern_t *) abstract_pattern;
431 return pattern->finish;