summaryrefslogtreecommitdiff
path: root/src/cairo-gl-private.h
blob: c013b8d7034812afdd4cd59d9b796f03781908de (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
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
/* cairo - a vector graphics library with display and print output
 *
 * Copyright © 2009 Eric Anholt
 * Copyright © 2009 Chris Wilson
 * Copyright © 2005,2010 Red Hat, Inc
 * Copyright © 2011 Linaro Limited
 * Copyright © 2011,2015 Samsung Research America, Inc - Silicon Valley
 *
 * 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):
 *	Benjamin Otte <otte@gnome.org>
 *	Carl Worth <cworth@cworth.org>
 *	Chris Wilson <chris@chris-wilson.co.uk>
 *	Eric Anholt <eric@anholt.net>
 *	T. Zachary Laine <whatwasthataddress@gmail.com>
 *	Alexandros Frantzis <alexandros.frantzis@linaro.org>
 *	Henry Song <hsong@sisa.samsung.com>
 */

#ifndef CAIRO_GL_PRIVATE_H
#define CAIRO_GL_PRIVATE_H

#define GL_GLEXT_PROTOTYPES

#include "cairoint.h"

#include "cairo-gl.h"
#include "cairo-gl-gradient-private.h"

#include "cairo-device-private.h"
#include "cairo-error-private.h"
#include "cairo-rtree-private.h"
#include "cairo-scaled-font-private.h"
#include "cairo-spans-compositor-private.h"
#include "cairo-array-private.h"
#include "cairo-stroke-dash-private.h"

#include <assert.h>

#if CAIRO_HAS_EVASGL_SURFACE
  #include <Evas_GL.h>
#else
  #if CAIRO_HAS_GL_SURFACE
  #if CAIRO_HAS_CGL_FUNCTIONS
  #include <OpenGL/gl.h>
  #include <OpenGL/glext.h>
  #else
  #include <GL/gl.h>
  #include <GL/glext.h>
  #endif
  #elif CAIRO_HAS_GLESV2_SURFACE
  #include <GLES2/gl2.h>
  #include <GLES2/gl2ext.h>
  #elif CAIRO_HAS_GLESV3_SURFACE
  #include <GLES3/gl3.h>
  #include <GLES3/gl3ext.h>
  #endif
#endif

#include "cairo-gl-ext-def-private.h"

#define DEBUG_GL 0

#define CAIRO_GL_ENUM_UNINITIALIZED 0xFFFF

#if DEBUG_GL && __GNUC__
#define UNSUPPORTED(reason) ({ \
    fprintf (stderr, \
	     "cairo-gl: hit unsupported operation in %s(), line %d: %s\n", \
	     __FUNCTION__, __LINE__, reason); \
    CAIRO_INT_STATUS_UNSUPPORTED; \
})
#else
#define UNSUPPORTED(reason) CAIRO_INT_STATUS_UNSUPPORTED
#endif

#define CAIRO_GL_VERSION_ENCODE(major, minor) (	\
	  ((major) * 256)			\
	+ ((minor) *   1))

/* maximal number of shaders we keep in the cache.
 * Random number that is hopefully big enough to not cause many cache evictions. */
#define CAIRO_GL_MAX_SHADERS_PER_CONTEXT 64

/* VBO size that we allocate, smaller size means we gotta flush more often,
 * but larger means hogging more memory and can cause trouble for drivers
 * (especially on embedded devices). Use the CAIRO_GL_VBO_SIZE environment
 * variable to set this to a different size. */
#define CAIRO_GL_VBO_SIZE_DEFAULT (256*1024)

#define MIN_IMAGE_CACHE_WIDTH 512
#define MIN_IMAGE_CACHE_HEIGHT 512
#define MAX_IMAGE_CACHE_WIDTH 2048
#define MAX_IMAGE_CACHE_HEIGHT 2048
#define IMAGE_CACHE_MIN_SIZE 1
#define IMAGE_CACHE_MAX_SIZE 256
#define MIN_SCRATCH_SIZE 32
#define MAX_SCRATCH_SIZE 1024

typedef struct _cairo_gl_surface cairo_gl_surface_t;
typedef struct _cairo_gl_image   cairo_gl_image_t;

/* Mali or SGX driver */
typedef enum _cairo_gl_multisample_ext_type {
    CAIRO_GL_EXT_MULTISAMPLE_TO_TEXTURE,
    CAIRO_GL_IMG_MULTISAMPLE_TO_TEXTURE,
    CAIRO_GL_NONE_MULTISAMPLE_TO_TEXTURE
} cairo_gl_multisample_ext_type;

/* GL blur stages */
typedef enum _cairo_blur_stage {
    CAIRO_GL_BLUR_STAGE_NONE,
    CAIRO_GL_BLUR_STAGE_0,	/* shrink stage  */
    CAIRO_GL_BLUR_STAGE_1,	/* x/y pass      */
    CAIRO_GL_BLUR_STAGE_2	/* enlarge stage */
} cairo_blur_stage_t;

/* GL flavor */
typedef enum cairo_gl_flavor {
    CAIRO_GL_FLAVOR_NONE = 0,
    CAIRO_GL_FLAVOR_DESKTOP = 1,
    CAIRO_GL_FLAVOR_ES2 = 2,
    CAIRO_GL_FLAVOR_ES3 = 3
} cairo_gl_flavor_t;

/* The order here is sensitive because of the logic of 
 *_cairo_gl_shader_uniform_for_texunit. */
typedef enum cairo_gl_uniform_t {
    CAIRO_GL_UNIFORM_TEXDIMS,    /* "source_texdims" */
    CAIRO_GL_UNIFORM_TEXGEN,     /* "source_texgen" */
    CAIRO_GL_UNIFORM_CONSTANT,   /* "source_constant" */
    CAIRO_GL_UNIFORM_SAMPLER,    /* "source_sampler" */
    CAIRO_GL_UNIFORM_A,          /* "source_a" */
    CAIRO_GL_UNIFORM_CIRCLE_D,   /* "source_circle_d" */
    CAIRO_GL_UNIFORM_RADIUS_0,   /* "source_radius_0" */
    CAIRO_GL_UNIFORM_BLUR_RADIUS,/* "source_blur_radius" */
    CAIRO_GL_UNIFORM_BLURS,	 /* "source_blurs" */
    CAIRO_GL_UNIFORM_BLUR_STEP,	 /* "source_blurstep" */
    CAIRO_GL_UNIFORM_BLUR_X_AXIS, /* "source_blur_x_axis" */
    CAIRO_GL_UNIFORM_BLUR_Y_AXIS, /* "source_blur_y_axis" */
    CAIRO_GL_UNIFORM_ALPHA,	  /* "source_alpha */
    CAIRO_GL_UNIFORM_COLOR_1,	  /* "source_color_1 */
    CAIRO_GL_UNIFORM_COLOR_2,	  /* "source_color_2 */
    CAIRO_GL_UNIFORM_OFFSET_1,	  /* "source_offset_1 */
    CAIRO_GL_UNIFORM_OFFSET_2,	  /* "source_offset_2 */

    CAIRO_GL_UNIFORM_MASK_TEXDIMS,      /* "mask_texdims" */
    CAIRO_GL_UNIFORM_MASK_TEXGEN,       /* "mask_texgen" */
    CAIRO_GL_UNIFORM_MASK_CONSTANT,     /* "mask_constant" */
    CAIRO_GL_UNIFORM_MASK_SAMPLER,      /* "mask_sampler" */
    CAIRO_GL_UNIFORM_MASK_A,            /* "mask_a" */
    CAIRO_GL_UNIFORM_MASK_CIRCLE_D,     /* "mask_circle_d" */
    CAIRO_GL_UNIFORM_MASK_RADIUS_0,     /* "mask_radius_0" */
    CAIRO_GL_UNIFORM_MASK_BLUR_RADIUS,  /* "mask_blur_radius */
    CAIRO_GL_UNIFORM_MASK_BLURS,	/* "mask_blurs */
    CAIRO_GL_UNIFORM_MASK_BLUR_STEP,	/* "mask_blur_step" */
    CAIRO_GL_UNIFORM_MASK_BLUR_X_AXIS,   /* "mask_blur_x_axis" */
    CAIRO_GL_UNIFORM_MASK_BLUR_Y_AXIS,   /* "mask_blur_y_axis" */
    CAIRO_GL_UNIFORM_MASK_ALPHA,	 /* "mask_alpha" */
    CAIRO_GL_UNIFORM_MASK_COLOR_1,	  /* "mask_color_1" */
    CAIRO_GL_UNIFORM_MASK_COLOR_2,	  /* "mask_color_2" */
    CAIRO_GL_UNIFORM_MASK_OFFSET_1,	  /* "mask_offset_1 */
    CAIRO_GL_UNIFORM_MASK_OFFSET_2,	  /* "mask_offset_2 */

    CAIRO_GL_UNIFORM_PROJECTION_MATRIX, /* "ModelViewProjectionMatrix" */


    CAIRO_GL_UNIFORM_MAX
} cairo_gl_uniform_t;

/* Indices for vertex attributes used by BindAttribLocation etc */
enum {
    CAIRO_GL_VERTEX_ATTRIB_INDEX = 0,
    CAIRO_GL_COLOR_ATTRIB_INDEX  = 1,
    CAIRO_GL_COVERAGE_ATTRIB_INDEX  = 2,
    CAIRO_GL_TEXCOORD0_ATTRIB_INDEX = 3,
    CAIRO_GL_TEXCOORD1_ATTRIB_INDEX = 4,
    CAIRO_GL_START_COORD0_ATTRIB_INDEX = 5,
    CAIRO_GL_START_COORD1_ATTRIB_INDEX = 6,
    CAIRO_GL_STOP_COORD0_ATTRIB_INDEX = 7,
    CAIRO_GL_STOP_COORD1_ATTRIB_INDEX = 8
};

typedef enum cairo_gl_operand_type {
    CAIRO_GL_OPERAND_NONE,
    CAIRO_GL_OPERAND_CONSTANT,
    CAIRO_GL_OPERAND_TEXTURE,
    CAIRO_GL_OPERAND_LINEAR_GRADIENT,
    CAIRO_GL_OPERAND_RADIAL_GRADIENT_A0,
    CAIRO_GL_OPERAND_RADIAL_GRADIENT_NONE,
    CAIRO_GL_OPERAND_RADIAL_GRADIENT_EXT,
    CAIRO_GL_OPERAND_GAUSSIAN,

    CAIRO_GL_OPERAND_COUNT
} cairo_gl_operand_type_t;

typedef enum cairo_gl_draw_mode {
    CAIRO_GL_VERTEX,
    CAIRO_GL_LINE_STRIP,
    CAIRO_GL_LINES
} cairo_gl_draw_mode_t;

/* This union structure describes a potential source or mask operand to the
 * compositing equation.
 */
typedef struct cairo_gl_operand {
    cairo_gl_operand_type_t type;
    int pass; /* 0 none, 1, x-axis, 2, y-axis */
    union {
	struct {
	    GLuint tex;
	    cairo_gl_surface_t *surface;
	    cairo_gl_surface_t *owns_surface;
	    cairo_surface_attributes_t attributes;
	    int texgen;
	    cairo_bool_t use_atlas;
	    cairo_extend_t extend;
	    struct { float x, y; } p1, p2;
	    float coef[17];        /* max 1x/x1 kernel size */
	    int x_radius;
	    int y_radius;
	} texture;
	struct {
	    GLfloat color[4];
	    cairo_bool_t encode_as_attribute;
	} constant;
	struct {
	    cairo_gl_gradient_t *gradient;
	    cairo_matrix_t m;
	    cairo_circle_double_t circle_d;
	    double radius_0, a;
	    cairo_extend_t extend;
	    int texgen;
	} gradient;
    };
    unsigned int vertex_offset;
} cairo_gl_operand_t;

typedef struct cairo_gl_source {
    cairo_surface_t base;
    cairo_gl_operand_t operand;
} cairo_gl_source_t;

struct _cairo_gl_surface {
    cairo_surface_t base;
    cairo_gl_operand_t operand;

    int width, height;

    GLuint tex; /* GL texture object containing our data. */
    GLuint fb; /* GL framebuffer object wrapping our data. */
    GLuint depth_stencil; /* GL renderbuffer object for holding stencil buffer clip. */

    GLuint msaa_rb; /* The ARB MSAA path uses a renderbuffer. */
    GLuint msaa_fb;
    GLuint msaa_depth_stencil;

    cairo_bool_t stencil_and_msaa_caps_initialized;
    cairo_bool_t supports_stencil; /* Stencil support for for non-texture surfaces. */
    cairo_bool_t supports_msaa;
    cairo_bool_t num_samples;
    cairo_bool_t force_no_msaa;
    cairo_bool_t msaa_active; /* Whether the multisampling
			         framebuffer is active or not. */
    cairo_bool_t content_synced; /* texture and renderbuffer content
				    synced */
    cairo_bool_t content_cleared; /* last draw is glClear */
    cairo_clip_t *clip_on_stencil_buffer;

    int owns_tex;
    cairo_bool_t needs_update;
    cairo_bool_t size_changed;

    cairo_region_t *clip_region;
    GLuint bounded_tex;		/* bounded tex for non-texture surface */

    /* Indicate whether we need to cache it in image_cache. */
    cairo_bool_t needs_to_cache;
    cairo_bool_t force_no_cache;
    double image_content_scale_x;
    double image_content_scale_y;
    cairo_blur_stage_t blur_stage;
    /* Damage is too expensive to check, we use this flag. */
    cairo_bool_t content_changed;
    cairo_gl_image_t *image_node;
};

typedef struct cairo_gl_glyph_cache {
    cairo_rtree_t rtree;
    cairo_gl_surface_t *surface;
} cairo_gl_glyph_cache_t;

typedef enum cairo_gl_tex {
    CAIRO_GL_TEX_SOURCE = 0,
    CAIRO_GL_TEX_MASK = 1,
    CAIRO_GL_TEX_TEMP = 2
} cairo_gl_tex_t;

typedef struct cairo_gl_shader {
    GLuint fragment_shader;
    GLuint program;
    GLint mvp_location;
    GLint constant_location[2];
    GLint a_location[2];
    GLint circle_d_location[2];
    GLint radius_0_location[2];
    GLint texdims_location[2];
    GLint texgen_location[2];
    GLint blur_radius_location[2];
    GLint blurs_location[2];
    GLint blur_step_location[2];
    GLint blur_x_axis_location[2];
    GLint blur_y_axis_location[2];
    GLint alpha_location[2];
    GLint color_1_location[2];
    GLint color_2_location[2];
    GLint offset_1_location[2];
    GLint offset_2_location[2];
} cairo_gl_shader_t;

typedef struct _cairo_gl_image_cache {
    cairo_rtree_t rtree;
    cairo_gl_surface_t *surface;
    cairo_bool_t copy_success;
} cairo_gl_image_cache_t;

struct _cairo_gl_image {
    cairo_rtree_node_t node;
    cairo_surface_t *original_surface;
    struct { float x, y; } p1, p2;
    cairo_gl_context_t *ctx;
};

typedef enum cairo_gl_shader_in {
    CAIRO_GL_SHADER_IN_NORMAL,
    CAIRO_GL_SHADER_IN_CA_SOURCE,
    CAIRO_GL_SHADER_IN_CA_SOURCE_ALPHA,

    CAIRO_GL_SHADER_IN_COUNT
} cairo_gl_shader_in_t;


typedef struct _cairo_gl_hairline_closure
{
    cairo_gl_context_t *ctx;
    double tolerance;
    cairo_stroker_dash_t dash;
    cairo_matrix_t *ctm;
    cairo_matrix_t *ctm_inverse;
    cairo_point_t current_point;

    cairo_point_t stroke_first_point;  /* First stroke point at move_to. */
    double stroke_first_dx;
    double stroke_first_dy;
    cairo_bool_t stroke_first_capped;
    cairo_bool_t moved_to_stroke_first_point;

    cairo_line_cap_t cap_style;

    cairo_bool_t line_last_capped;

    cairo_point_t line_last_point;
    double line_last_dx;
    double line_last_dy;

    cairo_bool_t initialized;
} cairo_gl_hairline_closure_t;

typedef enum cairo_gl_var_type {
  CAIRO_GL_VAR_NONE,
  CAIRO_GL_VAR_TEXCOORDS,
  CAIRO_GL_VAR_TEXGEN,
  CAIRO_GL_VAR_COLOR,
} cairo_gl_var_type_t;

typedef enum cairo_gl_primitive_type {
    CAIRO_GL_PRIMITIVE_TYPE_TRIANGLES,
    CAIRO_GL_PRIMITIVE_TYPE_TRISTRIPS
} cairo_gl_primitive_type_t;

typedef void (*cairo_gl_emit_rect_t) (cairo_gl_context_t *ctx,
				      GLfloat x1, GLfloat y1,
				      GLfloat x2, GLfloat y2);

typedef void (*cairo_gl_emit_span_t) (cairo_gl_context_t *ctx,
				      GLfloat x1, GLfloat y1,
				      GLfloat x2, GLfloat y2,
				      uint8_t alpha);

typedef void (*cairo_gl_emit_glyph_t) (cairo_gl_context_t *ctx,
				       GLfloat x1, GLfloat y1,
				       GLfloat x2, GLfloat y2,
				       GLfloat glyph_x1, GLfloat glyph_y1,
				       GLfloat glyph_x2, GLfloat glyph_y2);

#define cairo_gl_var_type_hash(src,mask,src_atlas_extend,mask_atlas_extend,src_use_atlas,mask_use_atlas,spans,dest) ((spans) << 13) | ((mask) << 10 | (src << 7) | (mask_atlas_extend << 5) | (src_atlas_extend << 3) | (mask_use_atlas << 2) | (src_use_atlas << 1) | (dest))
#define CAIRO_GL_VAR_TYPE_MAX (1 << 14)

typedef void (*cairo_gl_generic_func_t)(void);
typedef cairo_gl_generic_func_t (*cairo_gl_get_proc_addr_func_t)(void *data, const char *procname);

typedef struct _cairo_gl_dispatch {
    /* common */
    void (*ActiveTexture) (GLenum texture);
    void (*BindTexture) (GLenum target, GLuint texture);
    void (*BlendFunc) (GLenum sfactor, GLenum dfactor);
    void (*BlendFuncSeparate) (GLenum srcRGB, GLenum dstRGB,
			       GLenum srcAlpha, GLenum dstAlpha);
    void (*Clear) (GLbitfield mask);
    void (*ClearColor) (GLfloat red, GLfloat green, GLfloat blue, GLfloat alpha);
    void (*ClearStencil) (GLint s);
    void (*ColorMask) (GLboolean red, GLboolean green, GLboolean blue, GLboolean alpha);
    void (*DeleteTextures) (GLsizei n, const GLuint *textures);
    void (*DepthMask) (GLboolean flag);
    void (*Disable) (GLenum cap);
    void (*DrawArrays) (GLenum mode, GLint first, GLsizei count);
    void (*DrawElements) (GLenum mode, GLsizei count, GLenum type, const GLvoid *indices);
    void (*Enable) (GLenum cap);
    void (*Flush) (void);
    void (*GenTextures) (GLsizei n, GLuint *textures);
    void (*GetBooleanv) (GLenum pname, GLboolean *data);
    GLenum (*GetError) (void);
    void (*GetFloatv) (GLenum pname, GLfloat *data);
    void (*GetIntegerv) (GLenum pname, GLint *data);
    const GLubyte *(*GetString) (GLenum pname);
    const GLubyte *(*GetStringi) (GLenum pname, GLuint i);
    void (*PixelStorei) (GLenum pname, GLint param);
    void (*ReadPixels) (GLint x, GLint y, GLsizei width, GLsizei height,
			GLenum format, GLenum type, GLvoid *data);
    void (*Scissor) (GLint x, GLint y, GLsizei width, GLsizei height);
    void (*StencilFunc) (GLenum func, GLint ref, GLuint mask);
    void (*StencilMask) (GLuint mask);
    void (*StencilOp) (GLenum sfail, GLenum dpfail, GLenum dppass);
    void (*TexSubImage2D) (GLenum target, GLint level,
			   GLint xoffset, GLint yoffset,
			   GLsizei width, GLsizei height,
			   GLenum format, GLenum type, const GLvoid *data);
    void (*TexImage2D) (GLenum target, GLint level, GLenum internalformat,
			GLsizei width, GLsizei height,
			GLint border, GLenum format,
			GLenum type, const GLvoid *data);
    void (*TexParameteri) (GLenum target, GLenum pname, GLint param);
#if defined(CAIRO_HAS_GL_SURFACE) || defined(CAIRO_HAS_EVASGL_SURFACE)
    void (*DrawBuffer) (GLenum buf);
    void (*ReadBuffer) (GLenum buf);
#endif
    void (*Viewport) (GLint x, GLint y, GLsizei width, GLsizei height);

    /* Buffers */
    void (*GenBuffers) (GLsizei n, GLuint *buffers);
    void (*DeleteBuffers) (GLsizei n, const GLuint *buffers);
    void (*BindBuffer) (GLenum target, GLuint buffer);
    void (*BufferData) (GLenum target, GLsizeiptr size,
			  const GLvoid* data, GLenum usage);
    void (*BufferSubData) (GLenum target, GLintptr offset,
			   GLsizeiptr size, const GLvoid *data);
    GLvoid *(*MapBuffer) (GLenum target, GLenum access);
    GLboolean (*UnmapBuffer) (GLenum target);
    void (*GenVertexArrays) (GLsizei n, GLuint *arrays);
    void (*BindVertexArray) (GLuint array);
    void (*DeleteVertexArrays) (GLsizei n, const GLuint *arrays);

    /* Shaders */
    GLuint (*CreateShader) (GLenum type);
    void (*ShaderSource) (GLuint shader, GLsizei count,
			    const GLchar** string, const GLint* length);
    void (*CompileShader) (GLuint shader);
    void (*GetShaderiv) (GLuint shader, GLenum pname, GLint *params);
    void (*GetShaderInfoLog) (GLuint shader, GLsizei bufSize,
				GLsizei *length, GLchar *infoLog);
    void (*DeleteShader) (GLuint shader);

    /* Programs */
    GLuint (*CreateProgram) (void);
    void (*AttachShader) (GLuint program, GLuint shader);
    void (*DeleteProgram) (GLuint program);
    void (*LinkProgram) (GLuint program);
    void (*UseProgram) (GLuint program);
    void (*GetProgramiv) (GLuint program, GLenum pname, GLint *params);
    void (*GetProgramInfoLog) (GLuint program, GLsizei bufSize,
				 GLsizei *length, GLchar *infoLog);

    /* Uniforms */
    GLint (*GetUniformLocation) (GLuint program, const GLchar* name);
    void (*Uniform1f) (GLint location, GLfloat x);
    void (*Uniform2f) (GLint location, GLfloat x, GLfloat y);
    void (*Uniform3f) (GLint location, GLfloat x, GLfloat y, GLfloat z);
    void (*Uniform4f) (GLint location, GLfloat x, GLfloat y, GLfloat z,
			 GLfloat w);
    void (*Uniform1fv) (GLint location, GLsizei count, const GLfloat *v);

    void (*UniformMatrix3fv) (GLint location, GLsizei count,
				GLboolean transpose, const GLfloat *value);
    void (*UniformMatrix4fv) (GLint location, GLsizei count,
			      GLboolean transpose, const GLfloat *value);
    void (*Uniform1i) (GLint location, GLint x);

    /* Attributes */
    void (*BindAttribLocation) (GLuint program, GLuint index,
				const GLchar *name);
    void (*VertexAttribPointer) (GLuint index, GLint size, GLenum type,
				 GLboolean normalized, GLsizei stride,
				 const GLvoid *pointer);
    void (*EnableVertexAttribArray) (GLuint index);
    void (*DisableVertexAttribArray) (GLuint index);

    /* Framebuffer objects */
    void (*GenFramebuffers) (GLsizei n, GLuint* framebuffers);
    void (*BindFramebuffer) (GLenum target, GLuint framebuffer);
    void (*FramebufferTexture2D) (GLenum target, GLenum attachment,
				    GLenum textarget, GLuint texture,
				    GLint level);
    GLenum (*CheckFramebufferStatus) (GLenum target);
    void (*DeleteFramebuffers) (GLsizei n, const GLuint* framebuffers);
    void (*GenRenderbuffers) (GLsizei n, GLuint *renderbuffers);
    void (*BindRenderbuffer) (GLenum target, GLuint renderbuffer);
    void (*RenderbufferStorage) (GLenum target, GLenum internal_format,
				 GLsizei width, GLsizei height);
    void (*FramebufferRenderbuffer) (GLenum target, GLenum attachment,
				     GLenum renderbuffer_ttarget, GLuint renderbuffer);
    void (*DeleteRenderbuffers) (GLsizei n, GLuint *renderbuffers);
    void (*BlitFramebuffer) (GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1,
			     GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1,
			     GLbitfield mask, GLenum filter);
    void (*RenderbufferStorageMultisample) (GLenum target, GLsizei samples,
					    GLenum internalformat,
					    GLsizei width, GLsizei height);
    void (*FramebufferTexture2DMultisample) (GLenum target, GLenum attachment,
					     GLenum textarget, GLuint texture,
					     GLint level, GLsizei samples);
} cairo_gl_dispatch_t;

typedef struct _cairo_gl_states {
    cairo_rectangle_int_t viewport_box;

    GLclampf clear_red;
    GLclampf clear_green;
    GLclampf clear_blue;
    GLclampf clear_alpha;

    cairo_bool_t blend_enabled;

    GLenum src_color_factor;
    GLenum dst_color_factor;
    GLenum src_alpha_factor;
    GLenum dst_alpha_factor;

    GLenum active_texture;

    cairo_bool_t depth_mask;

    cairo_bool_t scissor_test_enabled;
    cairo_bool_t stencil_test_enabled;

    GLuint bound_vbo;
    GLuint bound_vao;
    GLuint bound_ibo;
} cairo_gl_states_t;

struct _cairo_gl_context {
    cairo_device_t base;

    const cairo_compositor_t *compositor;

    GLint max_framebuffer_size;
    GLint max_texture_size;
    GLint max_textures;
    GLenum tex_target;

    GLint num_samples;
    cairo_bool_t supports_msaa;
    char *vb;
    GLuint vbo;
    GLuint vao;
    GLuint ibo;

    cairo_bool_t has_shader_support;

    GLuint vertex_shaders[CAIRO_GL_VAR_TYPE_MAX];
    cairo_gl_shader_t fill_rectangles_shader;
    cairo_cache_t shaders;

    cairo_cache_t gradients;

    /* cache[0] for gray font, cache[1] for rgba component alpha
     * cache[2] for color glyph */
    cairo_gl_glyph_cache_t glyph_cache[3];
    cairo_list_t fonts;

    cairo_gl_surface_t *current_target;
    cairo_operator_t current_operator;
    cairo_gl_shader_t *pre_shader; /* for component alpha */
    cairo_gl_shader_t *current_shader;

    cairo_gl_operand_t operands[2];
    cairo_bool_t spans;

    unsigned int vbo_size;
    unsigned int vb_offset;
    unsigned int vertex_size;
    cairo_region_t *clip_region;

    cairo_gl_primitive_type_t primitive_type;
    cairo_array_t tristrip_indices;

    cairo_bool_t has_mesa_pack_invert;
    cairo_gl_dispatch_t dispatch;
    GLfloat modelviewprojection_matrix[16];
    cairo_gl_flavor_t gl_flavor;
    cairo_bool_t has_map_buffer;
    cairo_bool_t has_packed_depth_stencil;
    cairo_bool_t has_npot_repeat;
    cairo_bool_t can_read_bgra;
    cairo_bool_t is_gl33;

    cairo_bool_t thread_aware;

    cairo_gl_image_cache_t *image_cache;
    cairo_gl_draw_mode_t draw_mode;
    cairo_gl_states_t states_cache;

    /* Intermediate mask surface for glyph rendering. Created on first access, enlarged on demand. */
    cairo_gl_surface_t *glyph_mask;
    /* Intermediate blur surface for gaussian blur. Created on first access, enlarged on demand. */
    cairo_gl_surface_t *source_scratch_surfaces[2];
    cairo_gl_surface_t *mask_scratch_surfaces[2];
    cairo_gl_surface_t *shadow_scratch_surfaces[3];
    cairo_gl_surface_t *shadow_masks[4];
    cairo_bool_t source_scratch_in_use;
    cairo_bool_t has_angle_multisampling;
    cairo_gl_multisample_ext_type msaa_type;

    void (*acquire) (void *ctx);
    void (*release) (void *ctx);

    void (*make_current) (void *ctx, cairo_gl_surface_t *surface);
    void (*swap_buffers)(void *ctx, cairo_gl_surface_t *surface);
    void (*destroy) (void *ctx);
};

typedef struct _cairo_gl_composite {
    cairo_gl_surface_t *dst;
    cairo_operator_t op;
    cairo_region_t *clip_region;

    cairo_gl_operand_t src;
    cairo_gl_operand_t mask;
    cairo_bool_t spans;

    cairo_clip_t *clip;
    cairo_bool_t multisample;
} cairo_gl_composite_t;

typedef struct _cairo_gl_font {
    cairo_scaled_font_private_t		base;
    cairo_device_t			*device;
    cairo_list_t			link;
} cairo_gl_font_t;

static cairo_always_inline GLenum
_cairo_gl_get_error (cairo_gl_context_t *ctx)
{
    GLenum err = ctx->dispatch.GetError();

    if (unlikely (err))
        while (ctx->dispatch.GetError ());

    return err;
}

static inline cairo_device_t *
_cairo_gl_context_create_in_error (cairo_status_t status)
{
    return (cairo_device_t *) _cairo_device_create_in_error (status);
}

cairo_private cairo_status_t
_cairo_gl_context_init (cairo_gl_context_t *ctx);

cairo_private void
_cairo_gl_context_reset (cairo_gl_context_t *ctx);

cairo_private void
_cairo_gl_surface_init (cairo_device_t *device,
			cairo_gl_surface_t *surface,
			cairo_content_t content,
			int width, int height);

static cairo_always_inline cairo_bool_t cairo_warn
_cairo_gl_surface_is_texture (cairo_gl_surface_t *surface)
{
    return surface->tex != 0;
}

cairo_private cairo_status_t
_cairo_gl_surface_draw_image (cairo_gl_surface_t *dst,
			      cairo_image_surface_t *src,
			      int src_x, int src_y,
			      int width, int height,
			      int dst_x, int dst_y,
			      cairo_bool_t force_flush);

cairo_private cairo_int_status_t
_cairo_gl_surface_resolve_multisampling (cairo_gl_surface_t *surface);

static cairo_always_inline cairo_bool_t
_cairo_gl_device_has_glsl (cairo_device_t *device)
{
    return ((cairo_gl_context_t *) device)->has_shader_support;
}

static cairo_always_inline cairo_bool_t
_cairo_gl_device_requires_power_of_two_textures (cairo_device_t *device)
{
    return ((cairo_gl_context_t *) device)->tex_target == GL_TEXTURE_RECTANGLE;
}

static cairo_always_inline cairo_status_t cairo_warn
_cairo_gl_context_acquire (cairo_device_t *device,
			   cairo_gl_context_t **ctx)
{
    cairo_status_t status;

    status = cairo_device_acquire (device);
    if (unlikely (status))
	return status;

    /* clear potential previous GL errors */
    _cairo_gl_get_error ((cairo_gl_context_t *) device);

    *ctx = (cairo_gl_context_t *) device;
    return CAIRO_STATUS_SUCCESS;
}

static cairo_always_inline cairo_warn cairo_status_t
_cairo_gl_context_release (cairo_gl_context_t *ctx, cairo_status_t status)
{
    GLenum err;

    err = _cairo_gl_get_error (ctx);

    if (unlikely (err)) {
        cairo_status_t new_status;
	new_status = _cairo_error (CAIRO_STATUS_DEVICE_ERROR);
        if (status == CAIRO_STATUS_SUCCESS)
            status = new_status;
    }

    cairo_device_release (&(ctx)->base);

    return status;
}

cairo_private void
_cairo_gl_context_set_destination (cairo_gl_context_t *ctx,
				   cairo_gl_surface_t *surface,
				   cairo_bool_t multisampling);

cairo_private void
_cairo_gl_context_bind_framebuffer (cairo_gl_context_t *ctx,
				    cairo_gl_surface_t *surface,
				    cairo_bool_t multisampling);

cairo_private cairo_gl_emit_rect_t
_cairo_gl_context_choose_emit_rect (cairo_gl_context_t *ctx);

cairo_private void
_cairo_gl_context_emit_rect (cairo_gl_context_t *ctx,
			     GLfloat x1, GLfloat y1,
			     GLfloat x2, GLfloat y2);

cairo_private cairo_gl_emit_span_t
_cairo_gl_context_choose_emit_span (cairo_gl_context_t *ctx);

cairo_private cairo_gl_emit_glyph_t
_cairo_gl_context_choose_emit_glyph (cairo_gl_context_t *ctx,
				     const cairo_bool_t is_color_glyph);

cairo_private void
_cairo_gl_context_activate (cairo_gl_context_t *ctx,
                            cairo_gl_tex_t      tex_unit);

cairo_private cairo_bool_t
_cairo_gl_operator_is_supported (cairo_operator_t op);

cairo_private cairo_bool_t
_cairo_gl_ensure_stencil (cairo_gl_context_t *ctx,
			  cairo_gl_surface_t *surface);

static cairo_always_inline void
_disable_stencil_buffer (cairo_gl_context_t *ctx)
{
    if (ctx->states_cache.stencil_test_enabled == TRUE) {
        ctx->dispatch.Disable (GL_STENCIL_TEST);
        ctx->states_cache.stencil_test_enabled = FALSE;
    }
}

static cairo_always_inline void
_disable_scissor_buffer (cairo_gl_context_t *ctx)
{
    if (ctx->states_cache.scissor_test_enabled == TRUE) {
        ctx->dispatch.Disable (GL_SCISSOR_TEST);
        ctx->states_cache.scissor_test_enabled = FALSE;
    }
}

static cairo_always_inline void
_enable_stencil_buffer (cairo_gl_context_t *ctx)
{
    if (ctx->states_cache.stencil_test_enabled == FALSE) {
        ctx->dispatch.Enable (GL_STENCIL_TEST);
        ctx->states_cache.stencil_test_enabled = TRUE;
    }
}

static cairo_always_inline void
_enable_scissor_buffer (cairo_gl_context_t *ctx)
{
    if (ctx->states_cache.scissor_test_enabled == FALSE) {
        ctx->dispatch.Enable (GL_SCISSOR_TEST);
        ctx->states_cache.scissor_test_enabled = TRUE;
    }
}

static cairo_always_inline void
_cairo_gl_ensure_drawbuffers (cairo_gl_context_t *ctx)
{
    GLint vbo, ibo, vao;

    if ((ctx->vao != 0 && 
	 (ctx->vao != ctx->states_cache.bound_vao))) {
	ctx->dispatch.GetIntegerv (GL_VERTEX_ARRAY_BINDING, &vao);
	if (vao != (GLint)ctx->states_cache.bound_vao) {
	    ctx->dispatch.BindVertexArray (ctx->vao);
	    ctx->states_cache.bound_vao = ctx->vao;

	    ctx->dispatch.BindBuffer (GL_ARRAY_BUFFER, ctx->vbo);
	    ctx->states_cache.bound_vbo = ctx->vbo;

	    ctx->dispatch.BindBuffer (GL_ELEMENT_ARRAY_BUFFER, ctx->ibo);
	    ctx->states_cache.bound_ibo = ctx->ibo;
	}
    }

    if ((ctx->vbo != 0 &&
	 (ctx->vbo != ctx->states_cache.bound_vbo))) {
	ctx->dispatch.GetIntegerv (GL_ARRAY_BUFFER_BINDING, &vbo);
	if (vbo != (GLint)ctx->states_cache.bound_vbo) {
	    ctx->dispatch.BindBuffer (GL_ARRAY_BUFFER, ctx->vbo);
	}
	ctx->states_cache.bound_vbo = ctx->vbo;
    }

    if ((ctx->ibo != 0 &&
	 (ctx->ibo != ctx->states_cache.bound_ibo))) {
	ctx->dispatch.GetIntegerv (GL_ELEMENT_ARRAY_BUFFER_BINDING, &ibo);
	if (ibo != (GLint)ctx->states_cache.bound_ibo) {
	    ctx->dispatch.BindBuffer (GL_ELEMENT_ARRAY_BUFFER, ctx->ibo);
	}
	ctx->states_cache.bound_ibo = ctx->ibo;
    }
}

cairo_private cairo_status_t
_cairo_gl_composite_init (cairo_gl_composite_t *setup,
                          cairo_operator_t op,
                          cairo_gl_surface_t *dst,
                          cairo_bool_t has_component_alpha);

cairo_private void
_cairo_gl_composite_fini (cairo_gl_composite_t *setup);

cairo_private cairo_status_t
_cairo_gl_composite_set_operator (cairo_gl_composite_t *setup,
				  cairo_operator_t op,
				  cairo_bool_t assume_component_alpha);

cairo_private void
_cairo_gl_composite_set_clip_region (cairo_gl_composite_t *setup,
                                     cairo_region_t *clip_region);

cairo_private void
_cairo_gl_composite_set_clip(cairo_gl_composite_t *setup,
			     cairo_clip_t *clip);

cairo_private cairo_int_status_t
_cairo_gl_composite_set_source (cairo_gl_composite_t *setup,
				const cairo_pattern_t *pattern,
				const cairo_rectangle_int_t *sample,
				const cairo_rectangle_int_t *extents,
				cairo_bool_t use_texgen,
				cairo_bool_t encode_color_as_attribute);

cairo_private void
_cairo_gl_composite_set_solid_source (cairo_gl_composite_t *setup,
				      const cairo_color_t *color);

cairo_private void
_cairo_gl_composite_set_source_operand (cairo_gl_composite_t *setup,
					const cairo_gl_operand_t *source);

cairo_private cairo_int_status_t
_cairo_gl_composite_set_mask (cairo_gl_composite_t *setup,
			      const cairo_pattern_t *pattern,
			      const cairo_rectangle_int_t *sample,
			      const cairo_rectangle_int_t *extents,
			      cairo_bool_t use_texgen);

cairo_private void
_cairo_gl_composite_set_mask_operand (cairo_gl_composite_t *setup,
				      const cairo_gl_operand_t *mask);

cairo_private void
_cairo_gl_composite_set_spans (cairo_gl_composite_t *setup);

cairo_private void
_cairo_gl_composite_set_multisample (cairo_gl_composite_t *setup);

cairo_private cairo_status_t
_cairo_gl_composite_begin (cairo_gl_composite_t *setup,
                           cairo_gl_context_t **ctx);

cairo_private cairo_status_t
_cairo_gl_set_operands_and_operator (cairo_gl_composite_t *setup,
				     cairo_gl_context_t *ctx);

cairo_private void
_cairo_gl_composite_flush (cairo_gl_context_t *ctx);

cairo_private cairo_int_status_t
_cairo_gl_composite_emit_quad_as_tristrip (cairo_gl_context_t	*ctx,
					   cairo_gl_composite_t	*setup,
					   const cairo_point_t	 quad[4]);

cairo_private cairo_int_status_t
_cairo_gl_composite_emit_int_quad_as_tristrip (cairo_gl_context_t *ctx,
					       cairo_gl_composite_t *setup,
					       const int	 quad[8]);

cairo_private cairo_int_status_t
_cairo_gl_composite_emit_triangle_as_tristrip (cairo_gl_context_t	*ctx,
					       cairo_gl_composite_t	*setup,
					       const cairo_point_t	 triangle[3]);

cairo_private cairo_int_status_t
_cairo_gl_composite_emit_triangle_fan (cairo_gl_context_t     *ctx,
				       cairo_gl_composite_t   *setup,
				       const cairo_point_t    *fan,
                                       int                     count);

cairo_private cairo_int_status_t
_cairo_gl_composite_emit_point_as_tristrip_line (cairo_gl_context_t  *ctx,
						 const cairo_point_t point[2],
						 cairo_bool_t	     start_point);

cairo_private cairo_int_status_t
_cairo_gl_composite_emit_point_as_single_line (cairo_gl_context_t  *ctx,
					        const cairo_point_t point[2]);

cairo_private void
_cairo_gl_context_destroy_operand (cairo_gl_context_t *ctx,
                                   cairo_gl_tex_t tex_unit);

cairo_private cairo_bool_t
_cairo_gl_get_image_format_and_type (cairo_gl_flavor_t flavor,
				     pixman_format_code_t pixman_format,
				     GLenum *internal_format, GLenum *format,
				     GLenum *type, cairo_bool_t *has_alpha,
				     cairo_bool_t *needs_swap);

cairo_private void
_cairo_gl_glyph_cache_init (cairo_gl_glyph_cache_t *cache);

cairo_private void
_cairo_gl_glyph_cache_fini (cairo_gl_context_t *ctx,
			    cairo_gl_glyph_cache_t *cache);

cairo_private cairo_int_status_t
_cairo_gl_surface_show_glyphs (void			*abstract_dst,
			       cairo_operator_t		 op,
			       const cairo_pattern_t	*source,
			       cairo_glyph_t		*glyphs,
			       int			 num_glyphs,
			       cairo_scaled_font_t	*scaled_font,
			       const cairo_clip_t	*clip,
			       int			*remaining_glyphs);

cairo_private cairo_status_t
_cairo_gl_context_init_shaders (cairo_gl_context_t *ctx);

cairo_private void
_cairo_gl_context_fini_shaders (cairo_gl_context_t *ctx);

static cairo_always_inline cairo_bool_t
_cairo_gl_context_is_flushed (cairo_gl_context_t *ctx)
{
    return ctx->vb_offset == 0;
}

cairo_private cairo_status_t
_cairo_gl_get_shader_by_type (cairo_gl_context_t *ctx,
                              cairo_gl_operand_t *source,
                              cairo_gl_operand_t *mask,
			      cairo_bool_t use_coverage,
                              cairo_gl_shader_in_t in,
                              cairo_gl_shader_t **shader);

cairo_private cairo_gl_uniform_t
_cairo_gl_shader_uniform_for_texunit (cairo_gl_uniform_t uniform,
                                      cairo_gl_tex_t tex_unit);

cairo_private void
_cairo_gl_shader_bind_float (cairo_gl_context_t *ctx,
			     GLint location,
			     float value);

cairo_private void
_cairo_gl_shader_bind_float_array (cairo_gl_context_t *ctx,
				   GLint location,
				   int num, float *values);

cairo_private void
_cairo_gl_shader_bind_int (cairo_gl_context_t *ctx,
			     GLint location,
			     int value);

cairo_private void
_cairo_gl_shader_bind_vec2 (cairo_gl_context_t *ctx,
			    GLint location,
			    float value0, float value1);

cairo_private void
_cairo_gl_shader_bind_vec3 (cairo_gl_context_t *ctx,
			    GLint location,
			    float value0,
			    float value1,
			    float value2);

cairo_private void
_cairo_gl_shader_bind_vec4 (cairo_gl_context_t *ctx,
			    GLint location,
			    float value0, float value1,
			    float value2, float value3);

cairo_private void
_cairo_gl_shader_bind_matrix (cairo_gl_context_t *ctx,
			      GLint location,
			      const cairo_matrix_t* m);

cairo_private void
_cairo_gl_shader_bind_matrix4f (cairo_gl_context_t *ctx,
				GLint location,
				GLfloat* gl_m);

cairo_private void
_cairo_gl_set_shader (cairo_gl_context_t *ctx,
		      cairo_gl_shader_t *shader);

cairo_private void
_cairo_gl_shader_fini (cairo_gl_context_t *ctx, cairo_gl_shader_t *shader);

cairo_private int
_cairo_gl_get_version (cairo_gl_dispatch_t *dispatch);

cairo_private int
_cairo_glsl_get_version (cairo_gl_dispatch_t *dispatch);

cairo_private cairo_gl_flavor_t
_cairo_gl_get_flavor (cairo_gl_dispatch_t *dispatch);

cairo_private unsigned long
_cairo_gl_get_vbo_size (void);

cairo_private cairo_bool_t
_cairo_gl_has_extension (cairo_gl_dispatch_t *dispatch, const char *ext);

cairo_private cairo_status_t
_cairo_gl_dispatch_init(cairo_gl_dispatch_t *dispatch,
			cairo_gl_get_proc_addr_func_t get_proc_addr,
			void *data);

cairo_private cairo_int_status_t
_cairo_gl_operand_init (cairo_gl_operand_t *operand,
		        const cairo_pattern_t *pattern,
		        cairo_gl_surface_t *dst,
			const cairo_rectangle_int_t *sample,
			const cairo_rectangle_int_t *extents,
			cairo_bool_t use_texgen,
			cairo_bool_t encode_color_as_attribute);

cairo_private void
_cairo_gl_solid_operand_init (cairo_gl_operand_t *operand,
	                      const cairo_color_t *color);

cairo_private cairo_filter_t
_cairo_gl_operand_get_filter (cairo_gl_operand_t *operand);

cairo_private GLint
_cairo_gl_operand_get_gl_filter (cairo_gl_operand_t *operand);

cairo_private cairo_extend_t
_cairo_gl_operand_get_extend (cairo_gl_operand_t *operand);

cairo_private cairo_extend_t
_cairo_gl_operand_get_atlas_extend (cairo_gl_operand_t *operand);

cairo_private unsigned int
_cairo_gl_operand_get_vertex_size (const cairo_gl_operand_t *operand);

cairo_private cairo_bool_t
_cairo_gl_operand_get_use_atlas (cairo_gl_operand_t *operand);

cairo_private cairo_bool_t
_cairo_gl_operand_needs_setup (cairo_gl_operand_t *dest,
                               cairo_gl_operand_t *source,
                               unsigned int        vertex_offset);

cairo_private void
_cairo_gl_operand_bind_to_shader (cairo_gl_context_t *ctx,
                                  cairo_gl_operand_t *operand,
                                  cairo_gl_tex_t      tex_unit);

cairo_private void
_cairo_gl_operand_emit (cairo_gl_operand_t *operand,
                        GLfloat ** vb,
                        GLfloat x,
                        GLfloat y);

cairo_private void
_cairo_gl_operand_copy (cairo_gl_operand_t *dst,
			const cairo_gl_operand_t *src);

cairo_private void
_cairo_gl_operand_translate (cairo_gl_operand_t *operand,
			     double tx, double ty);

cairo_private void
_cairo_gl_operand_destroy (cairo_gl_operand_t *operand);

cairo_private const cairo_compositor_t *
_cairo_gl_msaa_compositor_get (void);

cairo_private const cairo_compositor_t *
_cairo_gl_span_compositor_get (void);

cairo_private const cairo_compositor_t *
_cairo_gl_traps_compositor_get (void);

cairo_private cairo_int_status_t
_cairo_gl_check_composite_glyphs (const cairo_composite_rectangles_t *extents,
				  cairo_scaled_font_t *scaled_font,
				  cairo_glyph_t *glyphs,
				  int *num_glyphs);

cairo_private cairo_int_status_t
_cairo_gl_composite_glyphs (void			*_dst,
			    cairo_operator_t		 op,
			    cairo_surface_t		*_src,
			    int				 src_x,
			    int				 src_y,
			    int				 dst_x,
			    int				 dst_y,
			    cairo_composite_glyphs_info_t *info);

cairo_private cairo_int_status_t
_cairo_gl_composite_glyphs_with_clip (void			    *_dst,
				      cairo_operator_t		     op,
				      cairo_surface_t		    *_src,
				      int			     src_x,
				      int			     src_y,
				      int			     dst_x,
				      int			     dst_y,
				      cairo_composite_glyphs_info_t *info,
				      cairo_clip_t		    *clip);

cairo_private void
_cairo_gl_image_node_destroy (cairo_rtree_node_t *node);

cairo_private void
_cairo_gl_image_node_fini (void *data);

cairo_private void
_cairo_gl_image_cache_unlock (cairo_gl_context_t *ctx);

cairo_private cairo_int_status_t
_cairo_gl_image_cache_init (cairo_gl_context_t *ctx, int width, int height,
                            cairo_gl_image_cache_t **image_cache);
cairo_private void
_cairo_gl_image_cache_fini (cairo_gl_context_t *ctx);

cairo_private void
_cairo_gl_ensure_framebuffer (cairo_gl_context_t *ctx,
                              cairo_gl_surface_t *surface);

cairo_private cairo_surface_t *
_cairo_gl_surface_create_scratch (cairo_gl_context_t   *ctx,
				  cairo_content_t	content,
				  int			width,
				  int			height);

cairo_private cairo_surface_t *
_cairo_gl_surface_create_scratch_for_caching (cairo_gl_context_t *ctx,
					      cairo_content_t content,
					      int width,
					      int height);

cairo_private cairo_surface_t *
_cairo_gl_pattern_to_source (cairo_surface_t *dst,
			     const cairo_pattern_t *pattern,
			     cairo_bool_t is_mask,
			     const cairo_rectangle_int_t *extents,
			     const cairo_rectangle_int_t *sample,
			     int *src_x, int *src_y);

cairo_private cairo_int_status_t
_cairo_gl_msaa_compositor_draw_clip (cairo_gl_context_t *ctx,
				     cairo_gl_composite_t *setup,
				     cairo_clip_t *clip);

cairo_private cairo_surface_t *
_cairo_gl_white_source (void);

cairo_private void
_cairo_gl_scissor_to_rectangle (cairo_gl_surface_t *surface,
				const cairo_rectangle_int_t *r);

static inline cairo_gl_operand_t *
source_to_operand (cairo_surface_t *surface)
{
    cairo_gl_source_t *source = (cairo_gl_source_t *)surface;
    return source ? &source->operand : NULL;
}

static inline void
_cairo_gl_glyph_cache_unlock (cairo_gl_glyph_cache_t *cache)
{
    _cairo_rtree_unpin (&cache->rtree);
}



cairo_private cairo_bool_t
_cairo_gl_hairline_style_is_hairline (const cairo_stroke_style_t *style,
                                      const cairo_matrix_t       *ctm);

cairo_private cairo_status_t
_cairo_gl_hairline_move_to (void *closure,
                            const cairo_point_t *point);

cairo_private cairo_status_t
_cairo_gl_hairline_line_to (void *closure,
                            const cairo_point_t *point);

cairo_private cairo_status_t
_cairo_gl_hairline_line_to_dashed (void *closure,
                                   const cairo_point_t *point);

cairo_private cairo_status_t
_cairo_gl_hairline_curve_to (void *closure,
                             const cairo_point_t *p0,
                             const cairo_point_t *p1,
                             const cairo_point_t *p2);

cairo_private cairo_status_t
_cairo_gl_hairline_close_path (void *closure);

cairo_private cairo_status_t
_cairo_gl_path_fixed_stroke_to_hairline (const cairo_path_fixed_t *path,
                                         cairo_gl_hairline_closure_t *closure,
                                         const cairo_stroke_style_t *style,
                                         const cairo_matrix_t *ctm,
                                         const cairo_matrix_t *ctm_inverse,
                                         cairo_path_fixed_move_to_func_t *move_to,
                                         cairo_path_fixed_line_to_func_t *line_to,
                                         cairo_path_fixed_curve_to_func_t *curve_to,
                                         cairo_path_fixed_close_path_func_t *close_path);

cairo_private cairo_gl_surface_t *
_cairo_gl_gaussian_filter (cairo_gl_surface_t *dst,
			   const cairo_surface_pattern_t *pattern,
			   cairo_gl_surface_t *src,
			   cairo_rectangle_int_t *extents_out);

static inline cairo_bool_t
_cairo_gl_surface_is_scratch (cairo_gl_context_t *ctx,
			      cairo_gl_surface_t *surface)
{
    int i;

    for (i = 0; i < 2; i++) {
	if (surface == ctx->source_scratch_surfaces[i] ||
	    surface == ctx->mask_scratch_surfaces[i] ||
	    surface == ctx->shadow_scratch_surfaces[i])
	    return TRUE;
    }

    if (surface == ctx->shadow_scratch_surfaces[2])
	return TRUE;

    return FALSE;
}

slim_hidden_proto (cairo_gl_surface_create);
slim_hidden_proto (cairo_gl_surface_create_for_texture);

#endif /* CAIRO_GL_PRIVATE_H */