Coverage Report

Created: 2025-07-07 10:01

/work/workdir/UnpackedTarball/cairo/src/cairo-surface-subsurface.c
Line
Count
Source (jump to first uncovered line)
1
/* cairo - a vector graphics library with display and print output
2
 *
3
 * Copyright © 2009 Intel Corporation
4
 *
5
 * This library is free software; you can redistribute it and/or
6
 * modify it either under the terms of the GNU Lesser General Public
7
 * License version 2.1 as published by the Free Software Foundation
8
 * (the "LGPL") or, at your option, under the terms of the Mozilla
9
 * Public License Version 1.1 (the "MPL"). If you do not alter this
10
 * notice, a recipient may use your version of this file under either
11
 * the MPL or the LGPL.
12
 *
13
 * You should have received a copy of the LGPL along with this library
14
 * in the file COPYING-LGPL-2.1; if not, write to the Free Software
15
 * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA
16
 * You should have received a copy of the MPL along with this library
17
 * in the file COPYING-MPL-1.1
18
 *
19
 * The contents of this file are subject to the Mozilla Public License
20
 * Version 1.1 (the "License"); you may not use this file except in
21
 * compliance with the License. You may obtain a copy of the License at
22
 * http://www.mozilla.org/MPL/
23
 *
24
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY
25
 * OF ANY KIND, either express or implied. See the LGPL or the MPL for
26
 * the specific language governing rights and limitations.
27
 *
28
 * The Original Code is the cairo graphics library.
29
 *
30
 * The Initial Developer of the Original Code is Intel Corporation.
31
 *
32
 * Contributor(s):
33
 *      Chris Wilson <chris@chris-wilson.co.uk>
34
 */
35
36
#include "cairoint.h"
37
38
#include "cairo-clip-inline.h"
39
#include "cairo-error-private.h"
40
#include "cairo-image-surface-private.h"
41
#include "cairo-recording-surface-private.h"
42
#include "cairo-surface-offset-private.h"
43
#include "cairo-surface-snapshot-private.h"
44
#include "cairo-surface-subsurface-private.h"
45
46
static const cairo_surface_backend_t _cairo_surface_subsurface_backend;
47
48
static cairo_status_t
49
_cairo_surface_subsurface_finish (void *abstract_surface)
50
8
{
51
8
    cairo_surface_subsurface_t *surface = abstract_surface;
52
53
8
    cairo_surface_destroy (surface->target);
54
8
    cairo_surface_destroy (surface->snapshot);
55
56
8
    return CAIRO_STATUS_SUCCESS;
57
8
}
58
59
static cairo_surface_t *
60
_cairo_surface_subsurface_create_similar (void *other,
61
            cairo_content_t content,
62
            int width, int height)
63
0
{
64
0
    cairo_surface_subsurface_t *surface = other;
65
66
0
    if (surface->target->backend->create_similar == NULL)
67
0
  return NULL;
68
69
0
    return surface->target->backend->create_similar (surface->target, content, width, height);
70
0
}
71
72
static cairo_surface_t *
73
_cairo_surface_subsurface_create_similar_image (void *other,
74
            cairo_format_t format,
75
            int width, int height)
76
0
{
77
0
    cairo_surface_subsurface_t *surface = other;
78
79
0
    if (surface->target->backend->create_similar_image == NULL)
80
0
  return NULL;
81
82
0
    return surface->target->backend->create_similar_image (surface->target,
83
0
                 format,
84
0
                 width, height);
85
0
}
86
87
static cairo_image_surface_t *
88
_cairo_surface_subsurface_map_to_image (void *abstract_surface,
89
          const cairo_rectangle_int_t *extents)
90
0
{
91
0
    cairo_surface_subsurface_t *surface = abstract_surface;
92
0
    cairo_rectangle_int_t target_extents;
93
94
0
    target_extents.x = extents->x + surface->extents.x;
95
0
    target_extents.y = extents->y + surface->extents.y;
96
0
    target_extents.width  = extents->width;
97
0
    target_extents.height = extents->height;
98
99
0
    return _cairo_surface_map_to_image (surface->target, &target_extents);
100
0
}
101
102
static cairo_int_status_t
103
_cairo_surface_subsurface_unmap_image (void *abstract_surface,
104
               cairo_image_surface_t *image)
105
0
{
106
0
    cairo_surface_subsurface_t *surface = abstract_surface;
107
0
    return _cairo_surface_unmap_image (surface->target, image);
108
0
}
109
110
static cairo_int_status_t
111
_cairo_surface_subsurface_paint (void *abstract_surface,
112
         cairo_operator_t op,
113
         const cairo_pattern_t *source,
114
         const cairo_clip_t *clip)
115
0
{
116
0
    cairo_surface_subsurface_t *surface = abstract_surface;
117
0
    cairo_rectangle_int_t rect = { 0, 0, surface->extents.width, surface->extents.height };
118
0
    cairo_status_t status;
119
0
    cairo_clip_t *target_clip;
120
121
0
    target_clip = _cairo_clip_copy_intersect_rectangle (clip, &rect);
122
0
    status = _cairo_surface_offset_paint (surface->target,
123
0
           -surface->extents.x, -surface->extents.y,
124
0
            op, source, target_clip);
125
0
    _cairo_clip_destroy (target_clip);
126
0
    return status;
127
0
}
128
129
static cairo_int_status_t
130
_cairo_surface_subsurface_mask (void *abstract_surface,
131
        cairo_operator_t op,
132
        const cairo_pattern_t *source,
133
        const cairo_pattern_t *mask,
134
        const cairo_clip_t *clip)
135
0
{
136
0
    cairo_surface_subsurface_t *surface = abstract_surface;
137
0
    cairo_rectangle_int_t rect = { 0, 0, surface->extents.width, surface->extents.height };
138
0
    cairo_status_t status;
139
0
    cairo_clip_t *target_clip;
140
141
0
    target_clip = _cairo_clip_copy_intersect_rectangle (clip, &rect);
142
0
    status = _cairo_surface_offset_mask (surface->target,
143
0
           -surface->extents.x, -surface->extents.y,
144
0
           op, source, mask, target_clip);
145
0
    _cairo_clip_destroy (target_clip);
146
0
    return status;
147
0
}
148
149
static cairo_int_status_t
150
_cairo_surface_subsurface_fill (void      *abstract_surface,
151
        cairo_operator_t   op,
152
        const cairo_pattern_t *source,
153
        const cairo_path_fixed_t  *path,
154
        cairo_fill_rule_t  fill_rule,
155
        double       tolerance,
156
        cairo_antialias_t  antialias,
157
        const cairo_clip_t    *clip)
158
0
{
159
0
    cairo_surface_subsurface_t *surface = abstract_surface;
160
0
    cairo_rectangle_int_t rect = { 0, 0, surface->extents.width, surface->extents.height };
161
0
    cairo_status_t status;
162
0
    cairo_clip_t *target_clip;
163
164
0
    target_clip = _cairo_clip_copy_intersect_rectangle (clip, &rect);
165
0
    status = _cairo_surface_offset_fill (surface->target,
166
0
           -surface->extents.x, -surface->extents.y,
167
0
           op, source, path, fill_rule, tolerance, antialias,
168
0
           target_clip);
169
0
    _cairo_clip_destroy (target_clip);
170
0
    return status;
171
0
}
172
173
static cairo_int_status_t
174
_cairo_surface_subsurface_stroke (void        *abstract_surface,
175
          cairo_operator_t     op,
176
          const cairo_pattern_t   *source,
177
          const cairo_path_fixed_t    *path,
178
          const cairo_stroke_style_t  *stroke_style,
179
          const cairo_matrix_t    *ctm,
180
          const cairo_matrix_t    *ctm_inverse,
181
          double       tolerance,
182
          cairo_antialias_t    antialias,
183
          const cairo_clip_t      *clip)
184
16
{
185
16
    cairo_surface_subsurface_t *surface = abstract_surface;
186
16
    cairo_rectangle_int_t rect = { 0, 0, surface->extents.width, surface->extents.height };
187
16
    cairo_status_t status;
188
16
    cairo_clip_t *target_clip;
189
190
16
    target_clip = _cairo_clip_copy_intersect_rectangle (clip, &rect);
191
16
    status = _cairo_surface_offset_stroke (surface->target,
192
16
             -surface->extents.x, -surface->extents.y,
193
16
             op, source, path, stroke_style, ctm, ctm_inverse,
194
16
             tolerance, antialias,
195
16
             target_clip);
196
16
    _cairo_clip_destroy (target_clip);
197
16
    return status;
198
16
}
199
200
static cairo_int_status_t
201
_cairo_surface_subsurface_glyphs (void      *abstract_surface,
202
          cairo_operator_t   op,
203
          const cairo_pattern_t *source,
204
          cairo_glyph_t   *glyphs,
205
          int      num_glyphs,
206
          cairo_scaled_font_t *scaled_font,
207
          const cairo_clip_t  *clip)
208
0
{
209
0
    cairo_surface_subsurface_t *surface = abstract_surface;
210
0
    cairo_rectangle_int_t rect = { 0, 0, surface->extents.width, surface->extents.height };
211
0
    cairo_status_t status;
212
0
    cairo_clip_t *target_clip;
213
214
0
    target_clip = _cairo_clip_copy_intersect_rectangle (clip, &rect);
215
0
    status = _cairo_surface_offset_glyphs (surface->target,
216
0
             -surface->extents.x, -surface->extents.y,
217
0
             op, source,
218
0
             scaled_font, glyphs, num_glyphs,
219
0
             target_clip);
220
0
    _cairo_clip_destroy (target_clip);
221
0
    return status;
222
0
}
223
224
static cairo_status_t
225
_cairo_surface_subsurface_flush (void *abstract_surface, unsigned flags)
226
32
{
227
32
    cairo_surface_subsurface_t *surface = abstract_surface;
228
32
    return _cairo_surface_flush (surface->target, flags);
229
32
}
230
231
static cairo_status_t
232
_cairo_surface_subsurface_mark_dirty (void *abstract_surface,
233
              int x, int y,
234
              int width, int height)
235
0
{
236
0
    cairo_surface_subsurface_t *surface = abstract_surface;
237
0
    cairo_status_t status;
238
239
0
    status = CAIRO_STATUS_SUCCESS;
240
0
    if (surface->target->backend->mark_dirty_rectangle != NULL) {
241
0
  cairo_rectangle_int_t rect, extents;
242
243
0
  rect.x = x;
244
0
  rect.y = y;
245
0
  rect.width  = width;
246
0
  rect.height = height;
247
248
0
  extents.x = extents.y = 0;
249
0
  extents.width  = surface->extents.width;
250
0
  extents.height = surface->extents.height;
251
252
0
  if (_cairo_rectangle_intersect (&rect, &extents)) {
253
0
      status = surface->target->backend->mark_dirty_rectangle (surface->target,
254
0
                     rect.x + surface->extents.x,
255
0
                     rect.y + surface->extents.y,
256
0
                     rect.width, rect.height);
257
0
  }
258
0
    }
259
260
0
    return status;
261
0
}
262
263
static cairo_bool_t
264
_cairo_surface_subsurface_get_extents (void *abstract_surface,
265
               cairo_rectangle_int_t *extents)
266
8
{
267
8
    cairo_surface_subsurface_t *surface = abstract_surface;
268
269
8
    extents->x = 0;
270
8
    extents->y = 0;
271
8
    extents->width  = surface->extents.width;
272
8
    extents->height = surface->extents.height;
273
274
8
    return TRUE;
275
8
}
276
277
static void
278
_cairo_surface_subsurface_get_font_options (void *abstract_surface,
279
              cairo_font_options_t *options)
280
0
{
281
0
    cairo_surface_subsurface_t *surface = abstract_surface;
282
283
0
    if (surface->target->backend->get_font_options != NULL)
284
0
  surface->target->backend->get_font_options (surface->target, options);
285
0
}
286
287
static cairo_surface_t *
288
_cairo_surface_subsurface_source (void *abstract_surface,
289
          cairo_rectangle_int_t *extents)
290
0
{
291
0
    cairo_surface_subsurface_t *surface = abstract_surface;
292
0
    cairo_surface_t *source;
293
294
0
    source = _cairo_surface_get_source (surface->target, extents);
295
0
    if (extents)
296
0
  *extents = surface->extents;
297
298
0
    return source;
299
0
}
300
301
static cairo_status_t
302
_cairo_surface_subsurface_acquire_source_image (void                    *abstract_surface,
303
            cairo_image_surface_t  **image_out,
304
            void                   **extra_out)
305
0
{
306
0
    cairo_surface_subsurface_t *surface = abstract_surface;
307
0
    cairo_surface_pattern_t pattern;
308
0
    cairo_surface_t *image;
309
0
    cairo_status_t status;
310
311
0
    image = _cairo_image_surface_create_with_content (surface->base.content,
312
0
                  surface->extents.width,
313
0
                  surface->extents.height);
314
0
    if (unlikely (image->status))
315
0
  return image->status;
316
317
0
    _cairo_pattern_init_for_surface (&pattern, surface->target);
318
0
    cairo_matrix_init_translate (&pattern.base.matrix,
319
0
         surface->extents.x,
320
0
         surface->extents.y);
321
0
    pattern.base.filter = CAIRO_FILTER_NEAREST;
322
0
    status = _cairo_surface_paint (image,
323
0
           CAIRO_OPERATOR_SOURCE,
324
0
           &pattern.base, NULL);
325
0
    _cairo_pattern_fini (&pattern.base);
326
0
    if (unlikely (status)) {
327
0
  cairo_surface_destroy (image);
328
0
  return status;
329
0
    }
330
331
0
    *image_out = (cairo_image_surface_t *)image;
332
0
    *extra_out = NULL;
333
0
    return CAIRO_STATUS_SUCCESS;
334
0
}
335
336
static void
337
_cairo_surface_subsurface_release_source_image (void                   *abstract_surface,
338
            cairo_image_surface_t  *image,
339
            void                   *abstract_extra)
340
0
{
341
0
    cairo_surface_destroy (&image->base);
342
0
}
343
344
static cairo_surface_t *
345
_cairo_surface_subsurface_snapshot (void *abstract_surface)
346
0
{
347
0
    cairo_surface_subsurface_t *surface = abstract_surface;
348
0
    cairo_surface_pattern_t pattern;
349
0
    cairo_surface_t *clone;
350
0
    cairo_status_t status;
351
352
0
    TRACE ((stderr, "%s: target=%d\n", __FUNCTION__, surface->target->unique_id));
353
354
0
    clone = _cairo_surface_create_scratch (surface->target,
355
0
             surface->target->content,
356
0
             surface->extents.width,
357
0
             surface->extents.height,
358
0
             NULL);
359
0
    if (unlikely (clone->status))
360
0
  return clone;
361
362
0
    _cairo_pattern_init_for_surface (&pattern, surface->target);
363
0
    cairo_matrix_init_translate (&pattern.base.matrix,
364
0
         surface->extents.x, surface->extents.y);
365
0
    pattern.base.filter = CAIRO_FILTER_NEAREST;
366
0
    status = _cairo_surface_paint (clone,
367
0
           CAIRO_OPERATOR_SOURCE,
368
0
           &pattern.base, NULL);
369
0
    _cairo_pattern_fini (&pattern.base);
370
371
0
    if (unlikely (status)) {
372
0
  cairo_surface_destroy (clone);
373
0
  clone = _cairo_surface_create_in_error (status);
374
0
    }
375
376
0
    return clone;
377
0
}
378
379
static cairo_t *
380
_cairo_surface_subsurface_create_context(void *target)
381
8
{
382
8
    cairo_surface_subsurface_t *surface = target;
383
8
    return surface->target->backend->create_context (&surface->base);
384
8
}
385
386
static const cairo_surface_backend_t _cairo_surface_subsurface_backend = {
387
    CAIRO_SURFACE_TYPE_SUBSURFACE,
388
    _cairo_surface_subsurface_finish,
389
390
    _cairo_surface_subsurface_create_context,
391
392
    _cairo_surface_subsurface_create_similar,
393
    _cairo_surface_subsurface_create_similar_image,
394
    _cairo_surface_subsurface_map_to_image,
395
    _cairo_surface_subsurface_unmap_image,
396
397
    _cairo_surface_subsurface_source,
398
    _cairo_surface_subsurface_acquire_source_image,
399
    _cairo_surface_subsurface_release_source_image,
400
    _cairo_surface_subsurface_snapshot,
401
402
    NULL, /* copy_page */
403
    NULL, /* show_page */
404
405
    _cairo_surface_subsurface_get_extents,
406
    _cairo_surface_subsurface_get_font_options,
407
408
    _cairo_surface_subsurface_flush,
409
    _cairo_surface_subsurface_mark_dirty,
410
411
    _cairo_surface_subsurface_paint,
412
    _cairo_surface_subsurface_mask,
413
    _cairo_surface_subsurface_stroke,
414
    _cairo_surface_subsurface_fill,
415
    NULL, /* fill/stroke */
416
    _cairo_surface_subsurface_glyphs,
417
};
418
419
/**
420
 * cairo_surface_create_for_rectangle:
421
 * @target: an existing surface for which the sub-surface will point to
422
 * @x: the x-origin of the sub-surface from the top-left of the target surface (in device-space units)
423
 * @y: the y-origin of the sub-surface from the top-left of the target surface (in device-space units)
424
 * @width: width of the sub-surface (in device-space units)
425
 * @height: height of the sub-surface (in device-space units)
426
 *
427
 * Create a new surface that is a rectangle within the target surface.
428
 * All operations drawn to this surface are then clipped and translated
429
 * onto the target surface. Nothing drawn via this sub-surface outside of
430
 * its bounds is drawn onto the target surface, making this a useful method
431
 * for passing constrained child surfaces to library routines that draw
432
 * directly onto the parent surface, i.e. with no further backend allocations,
433
 * double buffering or copies.
434
 *
435
 * <note><para>The semantics of subsurfaces have not been finalized yet
436
 * unless the rectangle is in full device units, is contained within
437
 * the extents of the target surface, and the target or subsurface's
438
 * device transforms are not changed.</para></note>
439
 *
440
 * Return value: a pointer to the newly allocated surface. The caller
441
 * owns the surface and should call cairo_surface_destroy() when done
442
 * with it.
443
 *
444
 * This function always returns a valid pointer, but it will return a
445
 * pointer to a "nil" surface if @other is already in an error state
446
 * or any other error occurs.
447
 *
448
 * Since: 1.10
449
 **/
450
cairo_surface_t *
451
cairo_surface_create_for_rectangle (cairo_surface_t *target,
452
            double x, double y,
453
            double width, double height)
454
8
{
455
8
    cairo_surface_subsurface_t *surface;
456
457
8
    if (unlikely (width < 0 || height < 0))
458
0
  return _cairo_surface_create_in_error (_cairo_error (CAIRO_STATUS_INVALID_SIZE));
459
460
8
    if (unlikely (target->status))
461
0
  return _cairo_surface_create_in_error (target->status);
462
8
    if (unlikely (target->finished))
463
0
  return _cairo_surface_create_in_error (_cairo_error (CAIRO_STATUS_SURFACE_FINISHED));
464
465
8
    surface = _cairo_malloc (sizeof (cairo_surface_subsurface_t));
466
8
    if (unlikely (surface == NULL))
467
0
  return _cairo_surface_create_in_error (_cairo_error (CAIRO_STATUS_NO_MEMORY));
468
469
8
    x *= target->device_transform.xx;
470
8
    y *= target->device_transform.yy;
471
472
8
    width *= target->device_transform.xx;
473
8
    height *= target->device_transform.yy;
474
475
8
    x += target->device_transform.x0;
476
8
    y += target->device_transform.y0;
477
478
8
    _cairo_surface_init (&surface->base,
479
8
       &_cairo_surface_subsurface_backend,
480
8
       NULL, /* device */
481
8
       target->content,
482
8
       target->is_vector);
483
484
    /* XXX forced integer alignment */
485
8
    surface->extents.x = ceil (x);
486
8
    surface->extents.y = ceil (y);
487
8
    surface->extents.width = floor (x + width) - surface->extents.x;
488
8
    surface->extents.height = floor (y + height) - surface->extents.y;
489
8
    if ((surface->extents.width | surface->extents.height) < 0)
490
0
  surface->extents.width = surface->extents.height = 0;
491
492
8
    if (target->backend->type == CAIRO_SURFACE_TYPE_SUBSURFACE) {
493
  /* Maintain subsurfaces as 1-depth */
494
0
  cairo_surface_subsurface_t *sub = (cairo_surface_subsurface_t *) target;
495
0
  surface->extents.x += sub->extents.x;
496
0
  surface->extents.y += sub->extents.y;
497
0
  target = sub->target;
498
0
    }
499
500
8
    surface->target = cairo_surface_reference (target);
501
8
    surface->base.type = surface->target->type;
502
503
8
    surface->snapshot = NULL;
504
505
8
    cairo_surface_set_device_scale (&surface->base,
506
8
                                    target->device_transform.xx,
507
8
                                    target->device_transform.yy);
508
509
8
    return &surface->base;
510
8
}
511
512
cairo_surface_t *
513
_cairo_surface_create_for_rectangle_int (cairo_surface_t *target,
514
           const cairo_rectangle_int_t *extents)
515
0
{
516
0
    cairo_surface_subsurface_t *surface;
517
518
0
    if (unlikely (target->status))
519
0
  return _cairo_surface_create_in_error (target->status);
520
0
    if (unlikely (target->finished))
521
0
  return _cairo_surface_create_in_error (_cairo_error (CAIRO_STATUS_SURFACE_FINISHED));
522
523
0
    assert (target->backend->type != CAIRO_SURFACE_TYPE_SUBSURFACE);
524
525
0
    surface = _cairo_malloc (sizeof (cairo_surface_subsurface_t));
526
0
    if (unlikely (surface == NULL))
527
0
  return _cairo_surface_create_in_error (_cairo_error (CAIRO_STATUS_NO_MEMORY));
528
529
0
    _cairo_surface_init (&surface->base,
530
0
       &_cairo_surface_subsurface_backend,
531
0
       NULL, /* device */
532
0
       target->content,
533
0
       target->is_vector);
534
535
0
    surface->extents = *extents;
536
0
    surface->extents.x *= target->device_transform.xx;
537
0
    surface->extents.y *= target->device_transform.yy;
538
0
    surface->extents.width *= target->device_transform.xx;
539
0
    surface->extents.height *= target->device_transform.yy;
540
0
    surface->extents.x += target->device_transform.x0;
541
0
    surface->extents.y += target->device_transform.y0;
542
543
0
    surface->target = cairo_surface_reference (target);
544
0
    surface->base.type = surface->target->type;
545
546
0
    surface->snapshot = NULL;
547
548
0
    cairo_surface_set_device_scale (&surface->base,
549
0
                                    target->device_transform.xx,
550
0
                                    target->device_transform.yy);
551
552
0
    return &surface->base;
553
0
}
554
/* XXX observe mark-dirty */
555
556
static void
557
_cairo_surface_subsurface_detach_snapshot (cairo_surface_t *surface)
558
0
{
559
0
    cairo_surface_subsurface_t *ss = (cairo_surface_subsurface_t *) surface;
560
0
561
0
    TRACE ((stderr, "%s: target=%d\n", __FUNCTION__, ss->target->unique_id));
562
0
563
0
    cairo_surface_destroy (ss->snapshot);
564
0
    ss->snapshot = NULL;
565
0
}
566
567
void
568
_cairo_surface_subsurface_set_snapshot (cairo_surface_t *surface,
569
          cairo_surface_t *snapshot)
570
0
{
571
0
    cairo_surface_subsurface_t *ss = (cairo_surface_subsurface_t *) surface;
572
573
0
    TRACE ((stderr, "%s: target=%d, snapshot=%d\n", __FUNCTION__,
574
0
      ss->target->unique_id, snapshot->unique_id));
575
576
    /* FIXME: attaching the subsurface as a snapshot to its target creates
577
     * a reference cycle.  Let's make this call as a no-op until that bug
578
     * is fixed.
579
     */
580
0
    return;
581
582
0
    if (ss->snapshot)
583
0
  _cairo_surface_detach_snapshot (ss->snapshot);
584
585
0
    ss->snapshot = cairo_surface_reference (snapshot);
586
587
0
    _cairo_surface_attach_snapshot (ss->target, &ss->base,
588
0
            _cairo_surface_subsurface_detach_snapshot);
589
0
}