summaryrefslogtreecommitdiff
path: root/src/cairo-raster-source-pattern.c
blob: bcaa29dc9bba35d38ca9a822de2f68290ef5e5e7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
/* -*- Mode: c; tab-width: 8; c-basic-offset: 4; indent-tabs-mode: t; -*- */
/* cairo - a vector graphics library with display and print output
 *
 * Copyright © 2011 Intel Corporation
 *
 * 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., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, 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):
 *	Chris Wilson <chris@chris-wilson.co.uk>
 */

#include "cairoint.h"
#include "cairo-error-private.h"
#include "cairo-pattern-private.h"

/**
 * SECTION:cairo-raster-source
 * @Title: Raster Sources
 * @Short_Description: Supplying arbitrary image data
 * @See_Also: #cairo_pattern_t
 *
 * The raster source provides the ability to supply arbitrary pixel data
 * whilst rendering. The pixels are queried at the time of rasterisation
 * by means of user callback functions, allowing for the ultimate
 * flexibility. For example, in handling compressed image sources, you
 * may keep a MRU cache of decompressed images and decompress sources on the
 * fly and discard old ones to conserve memory.
 *
 * For the raster source to be effective, you must at least specify
 * the acquire and release callbacks which are used to retrieve the pixel
 * data for the region of interest and demark when it can be freed afterwards.
 * Other callbacks are provided for when the pattern is copied temporarily
 * during rasterisation, or more permanently as a snapshot in order to keep
 * the pixel data available for printing.
 **/

cairo_surface_t *
_cairo_raster_source_pattern_acquire (const cairo_pattern_t *abstract_pattern,
				      cairo_surface_t *target,
				      const cairo_rectangle_int_t *extents)
{
    cairo_raster_source_pattern_t *pattern =
	(cairo_raster_source_pattern_t *) abstract_pattern;

    if (pattern->acquire == NULL)
	return NULL;

    if (extents == NULL)
	extents = &pattern->extents;

    return pattern->acquire (&pattern->base, pattern->user_data,
			     target, extents);
}

void
_cairo_raster_source_pattern_release (const cairo_pattern_t *abstract_pattern,
				      cairo_surface_t *surface)
{
    cairo_raster_source_pattern_t *pattern =
	(cairo_raster_source_pattern_t *) abstract_pattern;

    if (pattern->release == NULL)
	return;

    pattern->release (&pattern->base, pattern->user_data, surface);
}

cairo_status_t
_cairo_raster_source_pattern_init_copy (cairo_pattern_t *abstract_pattern,
					const cairo_pattern_t *other)
{
    cairo_raster_source_pattern_t *pattern =
	(cairo_raster_source_pattern_t *) abstract_pattern;
    cairo_status_t status;

    VG (VALGRIND_MAKE_MEM_UNDEFINED (pattern, sizeof (cairo_raster_source_pattern_t)));
    memcpy(pattern, (cairo_raster_source_pattern_t *) other, sizeof (cairo_raster_source_pattern_t));

    status = CAIRO_STATUS_SUCCESS;
    if (pattern->copy)
	status = pattern->copy (&pattern->base, pattern->user_data, other);

    return status;
}

cairo_status_t
_cairo_raster_source_pattern_snapshot (cairo_pattern_t *abstract_pattern)
{
    cairo_raster_source_pattern_t *pattern =
	(cairo_raster_source_pattern_t *) abstract_pattern;

    if (pattern->snapshot == NULL)
	return CAIRO_STATUS_SUCCESS;

    return pattern->snapshot (&pattern->base, pattern->user_data);
}

void
_cairo_raster_source_pattern_finish (cairo_pattern_t *abstract_pattern)
{
    cairo_raster_source_pattern_t *pattern =
	(cairo_raster_source_pattern_t *) abstract_pattern;

    if (pattern->finish == NULL)
	return;

    pattern->finish (&pattern->base, pattern->user_data);
}

/* Public interface */

/**
 * cairo_pattern_create_raster_source:
 * @user_data: the user data to be passed to all callbacks
 * @content: content type for the pixel data that will be returned. Knowing
 * the content type ahead of time is used for analysing the operation and
 * picking the appropriate rendering path.
 * @width: maximum size of the sample area
 * @height: maximum size of the sample area
 *
 * Creates a new user pattern for providing pixel data.
 *
 * Use the setter functions to associate callbacks with the returned
 * pattern.  The only mandatory callback is acquire.
 *
 * Return value: a newly created #cairo_pattern_t. Free with
 *  cairo_pattern_destroy() when you are done using it.
 *
 * Since: 1.12
 **/
cairo_pattern_t *
cairo_pattern_create_raster_source (void *user_data,
				    cairo_content_t content,
				    int width, int height)
{
    cairo_raster_source_pattern_t *pattern;

    CAIRO_MUTEX_INITIALIZE ();

    if (width < 0 || height < 0)
	return _cairo_pattern_create_in_error (CAIRO_STATUS_INVALID_SIZE);

    if (! CAIRO_CONTENT_VALID (content))
	return _cairo_pattern_create_in_error (CAIRO_STATUS_INVALID_CONTENT);

    pattern = calloc (1, sizeof (*pattern));
    if (unlikely (pattern == NULL))
	return _cairo_pattern_create_in_error (CAIRO_STATUS_NO_MEMORY);

    _cairo_pattern_init (&pattern->base,
			 CAIRO_PATTERN_TYPE_RASTER_SOURCE);
    CAIRO_REFERENCE_COUNT_INIT (&pattern->base.ref_count, 1);

    pattern->content = content;

    pattern->extents.x = 0;
    pattern->extents.y = 0;
    pattern->extents.width  = width;
    pattern->extents.height = height;

    pattern->user_data = user_data;

    return &pattern->base;
}

/**
 * cairo_raster_source_pattern_set_callback_data:
 * @pattern: the pattern to update
 * @data: the user data to be passed to all callbacks
 *
 * Updates the user data that is provided to all callbacks.
 *
 * Since: 1.12
 **/
void
cairo_raster_source_pattern_set_callback_data (cairo_pattern_t *abstract_pattern,
					       void *data)
{
    cairo_raster_source_pattern_t *pattern;

    if (abstract_pattern->type != CAIRO_PATTERN_TYPE_RASTER_SOURCE)
	return;

    pattern = (cairo_raster_source_pattern_t *) abstract_pattern;
    pattern->user_data = data;
}

/**
 * cairo_raster_source_pattern_get_callback_data:
 * @pattern: the pattern to update
 *
 * Queries the current user data.
 *
 * Return value: the current user-data passed to each callback
 *
 * Since: 1.12
 **/
void *
cairo_raster_source_pattern_get_callback_data (cairo_pattern_t *abstract_pattern)
{
    cairo_raster_source_pattern_t *pattern;

    if (abstract_pattern->type != CAIRO_PATTERN_TYPE_RASTER_SOURCE)
	return NULL;

    pattern = (cairo_raster_source_pattern_t *) abstract_pattern;
    return pattern->user_data;
}

/**
 * cairo_raster_source_pattern_set_acquire:
 * @pattern: the pattern to update
 * @acquire: acquire callback
 * @release: release callback
 *
 * Specifies the callbacks used to generate the image surface for a rendering
 * operation (acquire) and the function used to cleanup that surface afterwards.
 *
 * The @acquire callback should create a surface (preferably an image
 * surface created to match the target using
 * cairo_surface_create_similar_image()) that defines at least the region
 * of interest specified by extents. The surface is allowed to be the entire
 * sample area, but if it does contain a subsection of the sample area,
 * the surface extents should be provided by setting the device offset (along
 * with its width and height) using cairo_surface_set_device_offset().
 *
 * Since: 1.12
 **/
void
cairo_raster_source_pattern_set_acquire (cairo_pattern_t *abstract_pattern,
					 cairo_raster_source_acquire_func_t acquire,
					 cairo_raster_source_release_func_t release)
{
    cairo_raster_source_pattern_t *pattern;

    if (abstract_pattern->type != CAIRO_PATTERN_TYPE_RASTER_SOURCE)
	return;

    pattern = (cairo_raster_source_pattern_t *) abstract_pattern;
    pattern->acquire = acquire;
    pattern->release = release;
}

/**
 * cairo_raster_source_pattern_get_acquire:
 * @pattern: the pattern to query
 * @acquire: return value for the current acquire callback
 * @release: return value for the current release callback
 *
 * Queries the current acquire and release callbacks.
 *
 * Since: 1.12
 **/
void
cairo_raster_source_pattern_get_acquire (cairo_pattern_t *abstract_pattern,
					 cairo_raster_source_acquire_func_t *acquire,
					 cairo_raster_source_release_func_t *release)
{
    cairo_raster_source_pattern_t *pattern;

    if (abstract_pattern->type != CAIRO_PATTERN_TYPE_RASTER_SOURCE)
	return;

    pattern = (cairo_raster_source_pattern_t *) abstract_pattern;
    if (acquire)
	*acquire = pattern->acquire;
    if (release)
	*release = pattern->release;
}

/**
 * cairo_raster_source_pattern_set_snapshot:
 * @pattern: the pattern to update
 * @snapshot: snapshot callback
 *
 * Sets the callback that will be used whenever a snapshot is taken of the
 * pattern, that is whenever the current contents of the pattern should be
 * preserved for later use. This is typically invoked whilst printing.
 *
 * Since: 1.12
 **/
void
cairo_raster_source_pattern_set_snapshot (cairo_pattern_t *abstract_pattern,
					  cairo_raster_source_snapshot_func_t snapshot)
{
    cairo_raster_source_pattern_t *pattern;

    if (abstract_pattern->type != CAIRO_PATTERN_TYPE_RASTER_SOURCE)
	return;

    pattern = (cairo_raster_source_pattern_t *) abstract_pattern;
    pattern->snapshot = snapshot;
}

/**
 * cairo_raster_source_pattern_get_snapshot:
 * @pattern: the pattern to query
 *
 * Queries the current snapshot callback.
 *
 * Return value: the current snapshot callback
 *
 * Since: 1.12
 **/
cairo_raster_source_snapshot_func_t
cairo_raster_source_pattern_get_snapshot (cairo_pattern_t *abstract_pattern)
{
    cairo_raster_source_pattern_t *pattern;

    if (abstract_pattern->type != CAIRO_PATTERN_TYPE_RASTER_SOURCE)
	return NULL;

    pattern = (cairo_raster_source_pattern_t *) abstract_pattern;
    return pattern->snapshot;
}

/**
 * cairo_raster_source_pattern_set_copy:
 * @pattern: the pattern to update
 * @copy: the copy callback
 *
 * Updates the copy callback which is used whenever a temporary copy of the
 * pattern is taken.
 *
 * Since: 1.12
 **/
void
cairo_raster_source_pattern_set_copy (cairo_pattern_t *abstract_pattern,
				      cairo_raster_source_copy_func_t copy)
{
    cairo_raster_source_pattern_t *pattern;

    if (abstract_pattern->type != CAIRO_PATTERN_TYPE_RASTER_SOURCE)
	return;

    pattern = (cairo_raster_source_pattern_t *) abstract_pattern;
    pattern->copy = copy;
}

/**
 * cairo_raster_source_pattern_get_copy:
 * @pattern: the pattern to query
 *
 * Queries the current copy callback.
 *
 * Return value: the current copy callback
 *
 * Since: 1.12
 **/
cairo_raster_source_copy_func_t
cairo_raster_source_pattern_get_copy (cairo_pattern_t *abstract_pattern)
{
    cairo_raster_source_pattern_t *pattern;

    if (abstract_pattern->type != CAIRO_PATTERN_TYPE_RASTER_SOURCE)
	return NULL;

    pattern = (cairo_raster_source_pattern_t *) abstract_pattern;
    return pattern->copy;
}

/**
 * cairo_raster_source_pattern_set_finish:
 * @pattern: the pattern to update
 * @finish: the finish callback
 *
 * Updates the finish callback which is used whenever a pattern (or a copy
 * thereof) will no longer be used.
 *
 * Since: 1.12
 **/
void
cairo_raster_source_pattern_set_finish (cairo_pattern_t *abstract_pattern,
					cairo_raster_source_finish_func_t finish)
{
    cairo_raster_source_pattern_t *pattern;

    if (abstract_pattern->type != CAIRO_PATTERN_TYPE_RASTER_SOURCE)
	return;

    pattern = (cairo_raster_source_pattern_t *) abstract_pattern;
    pattern->finish = finish;
}

/**
 * cairo_raster_source_pattern_get_finish:
 * @pattern: the pattern to query
 *
 * Queries the current finish callback.
 *
 * Return value: the current finish callback
 *
 * Since: 1.12
 **/
cairo_raster_source_finish_func_t
cairo_raster_source_pattern_get_finish (cairo_pattern_t *abstract_pattern)
{
    cairo_raster_source_pattern_t *pattern;

    if (abstract_pattern->type != CAIRO_PATTERN_TYPE_RASTER_SOURCE)
	return NULL;

    pattern = (cairo_raster_source_pattern_t *) abstract_pattern;
    return pattern->finish;
}