summaryrefslogtreecommitdiff
path: root/src/cairo-cogl-context.c
blob: 0116b0a5a93fafbbf7a791d1f328221453a0f57d (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
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
/* 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.og/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.
 *
 * Contributor(s):
 *      Robert Bragg <robert@linux.intel.com>
 */

/* so long as we can verify that the ctm doesn't change multiple times
 * during the construction of a path we can build a shadow
 * #cairo_path_fixed_t in user coordinates that we can use to create a
 * hash value for caching tessellations of that path.
 *
 * We need to hook into all the points where the ctm can be changed
 * so we can bump a cr->path_ctm_age counter.
 *
 * We need to hook into all the points where the path can be modified
 * so we can catch the start of a path and reset the cr->path_ctm_age
 * counter at that point.
 *
 * When a draw operation is hit we can then check that the
 * path_ctm_age == 0 and if so we create a hash of the path.
 *
 * We use this hash to lookup a #cairo_cogl_path_meta_t struct which
 * may contain tessellated triangles for the path or may just contain
 * a count of how many times the path has been re-seen (we only cache
 * tessellated triangles if there is evidence that the path is being
 * used multiple times because there is a cost involved in allocating
 * a separate buffer for the triangles).
 */

#include "cairoint.h"

#include "cairo-cogl-context-private.h"
#include "cairo-freed-pool-private.h"
#include "cairo-arc-private.h"
#include "cairo-path-fixed-private.h"

#include <glib.h>

static freed_pool_t context_pool;

void
_cairo_cogl_context_reset_static_data (void)
{
    _freed_pool_reset (&context_pool);
}

static cairo_status_t
_cairo_cogl_context_rectangle_real (cairo_cogl_context_t *cr,
				    double x, double y,
				    double width, double height)
{
    cairo_status_t status;
    status = cr->dev->backend_parent.rectangle (cr, x, y, width, height);
    if (unlikely (status))
	return status;

    return _cairo_cogl_path_fixed_rectangle (&cr->user_path,
					     _cairo_fixed_from_double (x),
					     _cairo_fixed_from_double (y),
					     _cairo_fixed_from_double (width),
					     _cairo_fixed_from_double (height));
}

/* The idea here is that we have a simplified way of tracking rectangle paths
 * because rectangles are perhaps the most common shape drawn with cairo.
 *
 * Basically we have a speculative store for a rectangle path that doesn't
 * need to use the #cairo_path_fixed_t api to describe a rectangle in terms of
 * (move_to,rel_line_to,rel_line_to,_rel_line_to,close) because if you profile
 * heavy rectangle drawing with Cairo that process can be overly expensive.
 *
 * If the user asks to add more than just a rectangle to their current path
 * then we "flush" any speculative rectangle stored into the current path
 * before continuing to append their operations.
 *
 * In addition to the speculative store cairo-cogl also has a fast-path
 * fill_rectangle drawing operation that further aims to minimize the cost
 * of drawing rectangles.
 */
static cairo_status_t
_flush_cr_rectangle (cairo_cogl_context_t *cr)
{
    if (!cr->path_is_rectangle)
	return CAIRO_STATUS_SUCCESS;

    cr->path_is_rectangle = FALSE;
    return _cairo_cogl_context_rectangle_real (cr, cr->x, cr->y, cr->width, cr->height);
}

static cairo_status_t
_cairo_cogl_context_restore (void *abstract_cr)
{
    cairo_cogl_context_t *cr = abstract_cr;

    if (cr->path_is_rectangle) {
	cairo_status_t status = _flush_cr_rectangle (cr);
	if (unlikely (status))
	    return status;
    }

    cr->path_ctm_age++;
    return cr->dev->backend_parent.restore (abstract_cr);
}

static cairo_status_t
_cairo_cogl_context_translate (void *abstract_cr, double tx, double ty)
{
    cairo_cogl_context_t *cr = abstract_cr;

    if (cr->path_is_rectangle) {
	cairo_status_t status = _flush_cr_rectangle (cr);
	if (unlikely (status))
	    return status;
    }

    cr->path_ctm_age++;
    return cr->dev->backend_parent.translate (abstract_cr, tx, ty);
}

static cairo_status_t
_cairo_cogl_context_scale (void *abstract_cr, double sx, double sy)
{
    cairo_cogl_context_t *cr = abstract_cr;

    if (cr->path_is_rectangle) {
	cairo_status_t status = _flush_cr_rectangle (cr);
	if (unlikely (status))
	    return status;
    }

    cr->path_ctm_age++;
    return cr->dev->backend_parent.scale (abstract_cr, sx, sy);
}

static cairo_status_t
_cairo_cogl_context_rotate (void *abstract_cr, double theta)
{
    cairo_cogl_context_t *cr = abstract_cr;

    if (cr->path_is_rectangle) {
	cairo_status_t status = _flush_cr_rectangle (cr);
	if (unlikely (status))
	    return status;
    }

    cr->path_ctm_age++;
    return cr->dev->backend_parent.rotate (abstract_cr, theta);
}

static cairo_status_t
_cairo_cogl_context_transform (void *abstract_cr, const cairo_matrix_t *matrix)
{
    cairo_cogl_context_t *cr = abstract_cr;

    if (cr->path_is_rectangle) {
	cairo_status_t status = _flush_cr_rectangle (cr);
	if (unlikely (status))
	    return status;
    }

    cr->path_ctm_age++;
    return cr->dev->backend_parent.transform (abstract_cr, matrix);
}

static cairo_status_t
_cairo_cogl_context_set_matrix (void *abstract_cr, const cairo_matrix_t *matrix)
{
    cairo_cogl_context_t *cr = abstract_cr;

    if (cr->path_is_rectangle) {
	cairo_status_t status = _flush_cr_rectangle (cr);
	if (unlikely (status))
	    return status;
    }

    cr->path_ctm_age++;
    return cr->dev->backend_parent.set_matrix (abstract_cr, matrix);
}

static cairo_status_t
_cairo_cogl_context_set_identity_matrix (void *abstract_cr)
{
    cairo_cogl_context_t *cr = abstract_cr;

    if (cr->path_is_rectangle) {
	cairo_status_t status = _flush_cr_rectangle (cr);
	if (unlikely (status))
	    return status;
    }

    cr->path_ctm_age++;
    return cr->dev->backend_parent.set_identity_matrix (abstract_cr);
}

static cairo_status_t
_cairo_cogl_context_new_path (void *abstract_cr)
{
    cairo_cogl_context_t *cr = abstract_cr;
    cairo_status_t status;

    if (cr->path_is_rectangle) {
	status = _flush_cr_rectangle (cr);
	if (unlikely (status))
	    return status;
    }

    status = cr->dev->backend_parent.new_path (abstract_cr);
    if (unlikely (status))
	return status;

    _cairo_path_fixed_fini (&cr->user_path);
    _cairo_path_fixed_init (&cr->user_path);
    cr->path_is_rectangle = FALSE;

    return CAIRO_STATUS_SUCCESS;
}

static cairo_status_t
_cairo_cogl_context_new_sub_path (void *abstract_cr)
{
    cairo_cogl_context_t *cr = abstract_cr;
    cairo_status_t status;

    if (cr->path_is_rectangle) {
	status = _flush_cr_rectangle (cr);
	if (unlikely (status))
	    return status;
    }

    status = cr->dev->backend_parent.new_sub_path (abstract_cr);
    if (unlikely (status))
	return status;

    _cairo_path_fixed_new_sub_path (&cr->user_path);

    return CAIRO_STATUS_SUCCESS;
}

static cairo_status_t
_cairo_cogl_context_move_to (void *abstract_cr, double x, double y)
{
    cairo_cogl_context_t *cr = abstract_cr;
    cairo_status_t status;
    cairo_fixed_t x_fixed, y_fixed;

    if (cr->path_is_rectangle) {
	status = _flush_cr_rectangle (cr);
	if (unlikely (status))
	    return status;
    }

    status = cr->dev->backend_parent.move_to (abstract_cr, x, y);
    if (unlikely (status))
	return status;

    x_fixed = _cairo_fixed_from_double (x);
    y_fixed = _cairo_fixed_from_double (y);

    return _cairo_path_fixed_move_to (&cr->user_path, x_fixed, y_fixed);
}

static cairo_status_t
_cairo_cogl_context_line_to (void *abstract_cr, double x, double y)
{
    cairo_cogl_context_t *cr = abstract_cr;
    cairo_status_t status;
    cairo_fixed_t x_fixed, y_fixed;

    if (cr->path_is_rectangle) {
	status = _flush_cr_rectangle (cr);
	if (unlikely (status))
	    return status;
    }

    status = cr->dev->backend_parent.line_to (abstract_cr, x, y);
    if (unlikely (status))
	return status;

    x_fixed = _cairo_fixed_from_double (x);
    y_fixed = _cairo_fixed_from_double (y);

    if (cr->user_path.buf.base.num_ops == 0)
	cr->path_ctm_age = 0;

    return _cairo_path_fixed_line_to (&cr->user_path, x_fixed, y_fixed);
}

static cairo_status_t
_cairo_cogl_context_curve_to (void *abstract_cr,
			       double x1, double y1,
			       double x2, double y2,
			       double x3, double y3)
{
    cairo_cogl_context_t *cr = abstract_cr;
    cairo_status_t status;
    cairo_fixed_t x1_fixed, y1_fixed;
    cairo_fixed_t x2_fixed, y2_fixed;
    cairo_fixed_t x3_fixed, y3_fixed;

    if (cr->path_is_rectangle) {
	status = _flush_cr_rectangle (cr);
	if (unlikely (status))
	    return status;
    }

    status = cr->dev->backend_parent.curve_to (abstract_cr, x1, y1, x2, y2, x3, y3);
    if (unlikely (status))
	return status;

    x1_fixed = _cairo_fixed_from_double (x1);
    y1_fixed = _cairo_fixed_from_double (y1);

    x2_fixed = _cairo_fixed_from_double (x2);
    y2_fixed = _cairo_fixed_from_double (y2);

    x3_fixed = _cairo_fixed_from_double (x3);
    y3_fixed = _cairo_fixed_from_double (y3);

    if (cr->user_path.buf.base.num_ops == 0)
	cr->path_ctm_age = 0;

    return _cairo_path_fixed_curve_to (&cr->user_path,
				       x1_fixed, y1_fixed,
				       x2_fixed, y2_fixed,
				       x3_fixed, y3_fixed);
}

static cairo_status_t
_cairo_cogl_context_arc (void *abstract_cr,
			  double xc, double yc, double radius,
			  double angle1, double angle2,
			  cairo_bool_t forward)
{
    cairo_cogl_context_t *cr = abstract_cr;
    cairo_status_t status;

    if (cr->path_is_rectangle) {
	status = _flush_cr_rectangle (cr);
	if (unlikely (status))
	    return status;
    }

    status = cr->dev->backend_parent.arc (abstract_cr, xc, yc, radius, angle1, angle2, forward);
    if (unlikely (status))
	return status;

    if (cr->user_path.buf.base.num_ops == 0)
	cr->path_ctm_age = 0;

    /* Do nothing, successfully, if radius is <= 0 */
    if (radius <= 0.0) {
	cairo_fixed_t x_fixed, y_fixed;

	x_fixed = _cairo_fixed_from_double (xc);
	y_fixed = _cairo_fixed_from_double (yc);
	status = _cairo_path_fixed_line_to (&cr->user_path, x_fixed, y_fixed);
	if (unlikely (status))
	    return status;

	status = _cairo_path_fixed_line_to (&cr->user_path, x_fixed, y_fixed);
	if (unlikely (status))
	    return status;

	return CAIRO_STATUS_SUCCESS;
    }

    status = _cairo_cogl_context_line_to (cr,
					  xc + radius * cos (angle1),
					  yc + radius * sin (angle1));

    if (unlikely (status))
	return status;

    if (forward)
	_cairo_arc_path (&cr->base.base, xc, yc, radius, angle1, angle2);
    else
	_cairo_arc_path_negative (&cr->base.base, xc, yc, radius, angle1, angle2);

    return CAIRO_STATUS_SUCCESS; /* any error will have already been set on cr */
}

static cairo_status_t
_cairo_cogl_context_rel_move_to (void *abstract_cr, double dx, double dy)
{
    cairo_cogl_context_t *cr = abstract_cr;
    cairo_status_t status;
    cairo_fixed_t dx_fixed, dy_fixed;

    if (cr->path_is_rectangle) {
	status = _flush_cr_rectangle (cr);
	if (unlikely (status))
	    return status;
    }

    status = cr->dev->backend_parent.rel_move_to (abstract_cr, dx, dy);
    if (unlikely (status))
	return status;

    dx_fixed = _cairo_fixed_from_double (dx);
    dy_fixed = _cairo_fixed_from_double (dy);

    return _cairo_path_fixed_rel_move_to (&cr->user_path, dx_fixed, dy_fixed);
}

static cairo_status_t
_cairo_cogl_context_rel_line_to (void *abstract_cr, double dx, double dy)
{
    cairo_cogl_context_t *cr = abstract_cr;
    cairo_status_t status;
    cairo_fixed_t dx_fixed, dy_fixed;

    if (cr->path_is_rectangle) {
	status = _flush_cr_rectangle (cr);
	if (unlikely (status))
	    return status;
    }

    status = cr->dev->backend_parent.rel_line_to (abstract_cr, dx, dy);
    if (unlikely (status))
	return status;

    dx_fixed = _cairo_fixed_from_double (dx);
    dy_fixed = _cairo_fixed_from_double (dy);

    if (cr->user_path.buf.base.num_ops == 0)
	cr->path_ctm_age = 0;

    return _cairo_path_fixed_rel_line_to (&cr->user_path, dx_fixed, dy_fixed);
}


static cairo_status_t
_cairo_cogl_context_rel_curve_to (void *abstract_cr,
				   double dx1, double dy1,
				   double dx2, double dy2,
				   double dx3, double dy3)
{
    cairo_cogl_context_t *cr = abstract_cr;
    cairo_status_t status;
    cairo_fixed_t dx1_fixed, dy1_fixed;
    cairo_fixed_t dx2_fixed, dy2_fixed;
    cairo_fixed_t dx3_fixed, dy3_fixed;

    if (cr->path_is_rectangle) {
	status = _flush_cr_rectangle (cr);
	if (unlikely (status))
	    return status;
    }

    status = cr->dev->backend_parent.rel_curve_to (abstract_cr, dx1, dy1, dx2, dy2, dx3, dy3);
    if (unlikely (status))
	return status;

    dx1_fixed = _cairo_fixed_from_double (dx1);
    dy1_fixed = _cairo_fixed_from_double (dy1);

    dx2_fixed = _cairo_fixed_from_double (dx2);
    dy2_fixed = _cairo_fixed_from_double (dy2);

    dx3_fixed = _cairo_fixed_from_double (dx3);
    dy3_fixed = _cairo_fixed_from_double (dy3);

    if (cr->user_path.buf.base.num_ops == 0)
	cr->path_ctm_age = 0;

    return _cairo_path_fixed_rel_curve_to (&cr->user_path,
					   dx1_fixed, dy1_fixed,
					   dx2_fixed, dy2_fixed,
					   dx3_fixed, dy3_fixed);
}

#if 0
static cairo_status_t
_cairo_cogl_context_arc_to (void *abstract_cr,
			    double x1, double y1,
			    double x2, double y2,
			    double radius)
{
    cairo_cogl_context_t *cr = abstract_cr;
    cairo_status_t status;

    if (cr->path_is_rectangle) {
	status = _flush_cr_rectangle (cr);
	if (unlikely (status))
	    return status;
    }

    status = cr->dev->backend_parent.arc_to (abstract_cr, x1, y1, x2, y2, radius);
    if (unlikely (status))
	return status;
#warning "FIXME"
}

static cairo_status_t
_cairo_cogl_rel_arc_to (void *cr,
			double dx1, double dy1,
			double dx2, double dy2,
			double radius)
{
    cairo_cogl_context_t *cr = abstract_cr;
    cairo_status_t status;

    if (cr->path_is_rectangle) {
	status = _flush_cr_rectangle (cr);
	if (unlikely (status))
	    return status;
    }

    status = cr->dev->backend_parent.rel_arc_to (abstract_cr, dx1, dy2, dx2, dy2, radius);
    if (unlikely (status))
	return status;
#warning "FIXME"
}
#endif

static cairo_status_t
_cairo_cogl_context_close_path (void *abstract_cr)
{
    cairo_cogl_context_t *cr = abstract_cr;
    cairo_status_t status;

    if (cr->path_is_rectangle) {
	status = _flush_cr_rectangle (cr);
	if (unlikely (status))
	    return status;
    }

    status = cr->dev->backend_parent.close_path (abstract_cr);
    if (unlikely (status))
	return status;

    if (cr->user_path.buf.base.num_ops == 0)
	cr->path_ctm_age = 0;

    return _cairo_path_fixed_close_path (&cr->user_path);
}

static cairo_status_t
_cairo_cogl_context_rectangle (void *abstract_cr,
			       double x, double y,
			       double width, double height)
{
    cairo_cogl_context_t *cr = abstract_cr;

    if (cr->user_path.buf.base.num_ops == 0) {
	cr->path_ctm_age = 0;

#if 1
	/* XXX: Since drawing rectangles is so common we have a
	 * fast-path for drawing a single rectangle. */
	cr->x = x;
	cr->y = y;
	cr->width = width;
	cr->height = height;
	cr->path_is_rectangle = TRUE;
	return CAIRO_STATUS_SUCCESS;
#endif
    }

    if (cr->path_is_rectangle) {
	cairo_status_t status = _flush_cr_rectangle (cr);
	if (unlikely (status))
	    return status;
    }

    return _cairo_cogl_context_rectangle_real (cr, x, y, width, height);
}

/* Since the surface backend drawing operator functions don't get
 * passed the current #cairo_t context we don't have a good way
 * to get our user-coordinates path into our surface operator
 * functions.
 *
 * For now we use this function to set side band data on the surface
 * itself.
 */
static void
_cairo_cogl_surface_set_side_band_state (cairo_cogl_surface_t *surface,
					 cairo_cogl_context_t *cr)
{

    if (cr->path_ctm_age <= 1) {
	surface->user_path = &cr->user_path;
	surface->ctm = &cr->base.gstate->ctm;
	surface->ctm_inverse = &cr->base.gstate->ctm_inverse;
	surface->path_is_rectangle = cr->path_is_rectangle;
	if (surface->path_is_rectangle) {
	    surface->path_rectangle_x = cr->x;
	    surface->path_rectangle_y = cr->y;
	    surface->path_rectangle_width = cr->width;
	    surface->path_rectangle_height = cr->height;
	}
    } else {
	surface->user_path = NULL;
	surface->path_is_rectangle = FALSE;
    }
}

static cairo_status_t
_cairo_cogl_context_fill (void *abstract_cr)
{
    cairo_cogl_context_t *cr = abstract_cr;
    cairo_status_t status;
    cairo_cogl_surface_t *surface = (cairo_cogl_surface_t *)cr->base.gstate->target;

    if (cr->path_is_rectangle) {
	status = _cairo_cogl_surface_fill_rectangle (cr->base.gstate->target,
						     cr->base.gstate->op,
						     cr->base.gstate->source,
						     cr->x,
						     cr->y,
						     cr->width,
						     cr->height,
						     &cr->base.gstate->ctm,
						     cr->base.gstate->clip);
	if (status == CAIRO_STATUS_SUCCESS)
	    goto DONE;
	_flush_cr_rectangle (cr);
    }

    _cairo_cogl_surface_set_side_band_state (surface, cr);

    status = cr->dev->backend_parent.fill (abstract_cr);
    if (unlikely (status))
	return status;

DONE:
    _cairo_path_fixed_fini (&cr->user_path);
    _cairo_path_fixed_init (&cr->user_path);
    cr->path_is_rectangle = FALSE;

    return CAIRO_STATUS_SUCCESS;
}

static cairo_status_t
_cairo_cogl_context_fill_preserve (void *abstract_cr)
{
    cairo_cogl_context_t *cr = abstract_cr;
    cairo_status_t status;
    cairo_cogl_surface_t *surface = (cairo_cogl_surface_t *)cr->base.gstate->target;

    _cairo_cogl_surface_set_side_band_state (surface, cr);

    status = cr->dev->backend_parent.fill_preserve (abstract_cr);
    if (unlikely (status))
	return status;

    return CAIRO_STATUS_SUCCESS;
}

static cairo_status_t
_cairo_cogl_context_stroke (void *abstract_cr)
{
    cairo_cogl_context_t *cr = abstract_cr;
    cairo_status_t status;
    cairo_cogl_surface_t *surface = (cairo_cogl_surface_t *)cr->base.gstate->target;

    _cairo_cogl_surface_set_side_band_state (surface, cr);

    status = cr->dev->backend_parent.stroke (abstract_cr);
    if (unlikely (status))
	return status;

    _cairo_path_fixed_fini (&cr->user_path);
    _cairo_path_fixed_init (&cr->user_path);
    cr->path_is_rectangle = FALSE;

    return CAIRO_STATUS_SUCCESS;
}

static cairo_status_t
_cairo_cogl_context_stroke_preserve (void *abstract_cr)
{
    cairo_cogl_context_t *cr = abstract_cr;
    cairo_status_t status;
    cairo_cogl_surface_t *surface = (cairo_cogl_surface_t *)cr->base.gstate->target;

    _cairo_cogl_surface_set_side_band_state (surface, cr);

    status = cr->dev->backend_parent.stroke_preserve (abstract_cr);
    if (unlikely (status))
	return status;

    return CAIRO_STATUS_SUCCESS;
}

static cairo_status_t
_cairo_cogl_context_clip (void *abstract_cr)
{
    cairo_cogl_context_t *cr = abstract_cr;
    cairo_status_t status;

    status = cr->dev->backend_parent.clip (abstract_cr);
    if (unlikely (status))
	return status;

    _cairo_path_fixed_fini (&cr->user_path);
    _cairo_path_fixed_init (&cr->user_path);
    cr->path_is_rectangle = FALSE;

    return CAIRO_STATUS_SUCCESS;
}

static void
_cairo_cogl_context_destroy (void *abstract_cr)
{
    cairo_cogl_context_t *cr = abstract_cr;

    _cairo_default_context_fini (&cr->base);

    _cairo_path_fixed_fini (&cr->user_path);

    /* mark the context as invalid to protect against misuse */
    cr->base.base.status = CAIRO_STATUS_NULL_POINTER;
    _freed_pool_put (&context_pool, cr);
}

/* We want to hook into the frontend of the path construction APIs so
 * we can build up a path description in user coordinates instead of
 * backend coordinates so that we can recognize user coordinate
 * rectangles and so we can hash a user path independent of its
 * transform. (With some care to catch unusual cases where the ctm
 * changes mid-path) */
cairo_t *
_cairo_cogl_context_create (void *target)
{
    cairo_cogl_surface_t *surface = target;
    cairo_cogl_context_t *cr;
    cairo_status_t status;

    cr = _freed_pool_get (&context_pool);
    if (unlikely (cr == NULL)) {
	cr = malloc (sizeof (cairo_cogl_context_t));
	if (unlikely (cr == NULL))
	    return _cairo_create_in_error (_cairo_error (CAIRO_STATUS_NO_MEMORY));
    }

    status = _cairo_default_context_init (&cr->base, target);
    if (unlikely (status)) {
	_freed_pool_put (&context_pool, cr);
	return _cairo_create_in_error (status);
    }

    cr->dev = (cairo_cogl_device_t *)surface->base.device;

    if (unlikely (cr->dev->backend_vtable_initialized == FALSE)) {
	cairo_backend_t *backend = &cr->dev->backend;
	memcpy (backend, cr->base.base.backend, sizeof (cairo_backend_t));
	memcpy (&cr->dev->backend_parent, cr->base.base.backend, sizeof (cairo_backend_t));

	backend->destroy = _cairo_cogl_context_destroy;

	backend->restore = _cairo_cogl_context_restore;
	backend->translate = _cairo_cogl_context_translate;
	backend->scale = _cairo_cogl_context_scale;
	backend->rotate = _cairo_cogl_context_rotate;
	backend->transform = _cairo_cogl_context_transform;
	backend->set_matrix = _cairo_cogl_context_set_matrix;
	backend->set_identity_matrix = _cairo_cogl_context_set_identity_matrix;

	backend->new_path = _cairo_cogl_context_new_path;
	backend->new_sub_path = _cairo_cogl_context_new_sub_path;
	backend->move_to = _cairo_cogl_context_move_to;
	backend->rel_move_to = _cairo_cogl_context_rel_move_to;
	backend->line_to = _cairo_cogl_context_line_to;
	backend->rel_line_to = _cairo_cogl_context_rel_line_to;
	backend->curve_to = _cairo_cogl_context_curve_to;
	backend->rel_curve_to = _cairo_cogl_context_rel_curve_to;
#if 0
	backend->arc_to = _cairo_cogl_context_arc_to;
	backend->rel_arc_to = _cairo_cogl_context_rel_arc_to;
#endif
	backend->close_path = _cairo_cogl_context_close_path;
	//backend->arc = _cairo_cogl_context_arc;
	backend->rectangle = _cairo_cogl_context_rectangle;

	/* Try to automatically catch if any new path APIs are added that mean
	 * we may need to overload more functions... */
	assert (((char *)&backend->path_extents - (char *)&backend->device_to_user_distance)
		== (sizeof (void *) * 14));

	backend->fill = _cairo_cogl_context_fill;
	backend->fill_preserve = _cairo_cogl_context_fill_preserve;
	backend->stroke = _cairo_cogl_context_stroke;
	backend->stroke_preserve = _cairo_cogl_context_stroke_preserve;
	backend->clip = _cairo_cogl_context_clip;

	cr->dev->backend_vtable_initialized = TRUE;
    }

    cr->base.base.backend = &cr->dev->backend;

    _cairo_path_fixed_init (&cr->user_path);
    cr->path_is_rectangle = FALSE;

    return &cr->base.base;
}