summaryrefslogtreecommitdiff
path: root/src/glitzint.h
blob: 8f92d09ab1e13048405b760c72cb3cce2363b25d (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
/*
 * Copyright © 2004 David Reveman
 *
 * Permission to use, copy, modify, distribute, and sell this software
 * and its documentation for any purpose is hereby granted without
 * fee, provided that the above copyright notice appear in all copies
 * and that both that copyright notice and this permission notice
 * appear in supporting documentation, and that the name of
 * David Reveman not be used in advertising or publicity pertaining to
 * distribution of the software without specific, written prior permission.
 * David Reveman makes no representations about the suitability of this
 * software for any purpose. It is provided "as is" without express or
 * implied warranty.
 *
 * DAVID REVEMAN DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN
 * NO EVENT SHALL DAVID REVEMAN BE LIABLE FOR ANY SPECIAL, INDIRECT OR
 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
 * OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
 * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
 * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 *
 * Author: David Reveman <davidr@novell.com>
 */

#ifndef GLITZINT_H_INCLUDED
#define GLITZINT_H_INCLUDED

#ifdef _MSC_VER
#define _USE_MATH_DEFINES
#endif

#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <math.h>

#include "glitz.h"

#if defined(__SVR4) && defined(__sun)
#  include <sys/int_types.h>
#elif defined(_MSC_VER)
  typedef __int8 int8_t;
  typedef unsigned __int8 uint8_t;
  typedef __int16 int16_t;
  typedef unsigned __int16 uint16_t;
  typedef __int32 int32_t;
  typedef unsigned __int32 uint32_t;
  typedef __int64 int64_t;
  typedef unsigned __int64 uint64_t;
#else
#  if defined(__OpenBSD__)
#    include <inttypes.h>
#  else
#    include <stdint.h>
#  endif
#endif

#if defined(__APPLE__) || defined(__sun__)
# define floorf(a)    floor (a)
# define ceilf(a)     ceil (a)
# define sinf(a)      sin (a)
# define cosf(a)      cos (a)
# define tanf(a)      tan (a)
# define asinf(a)     asin (a)
# define acosf(a)     acos (a)
# define atanf(a)     atan (a)
# define atan2f(a, b) atan2 (a, b)
# define sqrtf(a)     sqrt (a)
#endif

#if __GNUC__ >= 3 && defined(__ELF__)
# define slim_hidden_proto(name)	slim_hidden_proto1(name, INT_##name)
# define slim_hidden_def(name)		slim_hidden_def1(name, INT_##name)
# define slim_hidden_proto1(name, internal)				\
  extern __typeof (name) name						\
	__asm__ (slim_hidden_asmname (internal))			\
	__internal_linkage;
# define slim_hidden_def1(name, internal)				\
  extern __typeof (name) EXT_##name __asm__(slim_hidden_asmname(name))	\
	__attribute__((__alias__(slim_hidden_asmname(internal))))
# define slim_hidden_ulp		slim_hidden_ulp1(__USER_LABEL_PREFIX__)
# define slim_hidden_ulp1(x)		slim_hidden_ulp2(x)
# define slim_hidden_ulp2(x)		#x
# define slim_hidden_asmname(name)	slim_hidden_asmname1(name)
# define slim_hidden_asmname1(name)	slim_hidden_ulp #name
#else
# define slim_hidden_proto(name)
# define slim_hidden_def(name)
#endif

#if (__GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 3)) && defined(__ELF__)
#define __internal_linkage	__attribute__((__visibility__("hidden")))
#else
#define __internal_linkage
#endif

#ifndef __GNUC__
#define __attribute__(x)
#endif

#define GLITZ_STATUS_NO_MEMORY_MASK          (1L << 0)
#define GLITZ_STATUS_BAD_COORDINATE_MASK     (1L << 1)
#define GLITZ_STATUS_NOT_SUPPORTED_MASK      (1L << 2)
#define GLITZ_STATUS_CONTENT_DESTROYED_MASK  (1L << 3)

#define GLITZ_DRAWABLE_FORMAT_ALL_EXCEPT_ID_MASK ((1L << 11) - 2)

#include "glitz_gl.h"

#define GLITZ_CONTEXT_STACK_SIZE 16

typedef struct _glitz_gl_proc_address_list_t {

  /* core */
  glitz_gl_enable_t                     enable;
  glitz_gl_disable_t                    disable;
  glitz_gl_get_error_t                  get_error;
  glitz_gl_get_string_t                 get_string;
  glitz_gl_enable_client_state_t        enable_client_state;
  glitz_gl_disable_client_state_t       disable_client_state;
  glitz_gl_vertex_pointer_t             vertex_pointer;
  glitz_gl_tex_coord_pointer_t          tex_coord_pointer;
  glitz_gl_draw_arrays_t                draw_arrays;
  glitz_gl_tex_env_f_t                  tex_env_f;
  glitz_gl_tex_env_fv_t                 tex_env_fv;
  glitz_gl_tex_gen_i_t                  tex_gen_i;
  glitz_gl_tex_gen_fv_t                 tex_gen_fv;
  glitz_gl_color_4us_t                  color_4us;
  glitz_gl_color_4f_t                   color_4f;
  glitz_gl_scissor_t                    scissor;
  glitz_gl_blend_func_t                 blend_func;
  glitz_gl_clear_t                      clear;
  glitz_gl_clear_color_t                clear_color;
  glitz_gl_clear_stencil_t              clear_stencil;
  glitz_gl_stencil_func_t               stencil_func;
  glitz_gl_stencil_op_t                 stencil_op;
  glitz_gl_push_attrib_t                push_attrib;
  glitz_gl_pop_attrib_t                 pop_attrib;
  glitz_gl_matrix_mode_t                matrix_mode;
  glitz_gl_push_matrix_t                push_matrix;
  glitz_gl_pop_matrix_t                 pop_matrix;
  glitz_gl_load_identity_t              load_identity;
  glitz_gl_load_matrix_f_t              load_matrix_f;
  glitz_gl_depth_range_t                depth_range;
  glitz_gl_viewport_t                   viewport;
  glitz_gl_raster_pos_2f_t              raster_pos_2f;
  glitz_gl_bitmap_t                     bitmap;
  glitz_gl_read_buffer_t                read_buffer;
  glitz_gl_draw_buffer_t                draw_buffer;
  glitz_gl_copy_pixels_t                copy_pixels;
  glitz_gl_flush_t                      flush;
  glitz_gl_finish_t                     finish;
  glitz_gl_pixel_store_i_t              pixel_store_i;
  glitz_gl_ortho_t                      ortho;
  glitz_gl_scale_f_t                    scale_f;
  glitz_gl_translate_f_t                translate_f;
  glitz_gl_hint_t                       hint;
  glitz_gl_depth_mask_t                 depth_mask;
  glitz_gl_polygon_mode_t               polygon_mode;
  glitz_gl_shade_model_t                shade_model;
  glitz_gl_color_mask_t                 color_mask;
  glitz_gl_read_pixels_t                read_pixels;
  glitz_gl_get_tex_image_t              get_tex_image;
  glitz_gl_tex_sub_image_2d_t           tex_sub_image_2d;
  glitz_gl_gen_textures_t               gen_textures;
  glitz_gl_delete_textures_t            delete_textures;
  glitz_gl_bind_texture_t               bind_texture;
  glitz_gl_tex_image_2d_t               tex_image_2d;
  glitz_gl_tex_parameter_i_t            tex_parameter_i;
  glitz_gl_tex_parameter_fv_t           tex_parameter_fv;
  glitz_gl_get_tex_level_parameter_iv_t get_tex_level_parameter_iv;
  glitz_gl_copy_tex_sub_image_2d_t      copy_tex_sub_image_2d;
  glitz_gl_get_integer_v_t              get_integer_v;

  /* extensions */
  glitz_gl_blend_color_t                blend_color;
  glitz_gl_active_texture_t             active_texture;
  glitz_gl_client_active_texture_t      client_active_texture;
  glitz_gl_multi_draw_arrays_t          multi_draw_arrays;
  glitz_gl_gen_programs_t               gen_programs;
  glitz_gl_delete_programs_t            delete_programs;
  glitz_gl_program_string_t             program_string;
  glitz_gl_bind_program_t               bind_program;
  glitz_gl_program_local_param_4fv_t    program_local_param_4fv;
  glitz_gl_get_program_iv_t             get_program_iv;
  glitz_gl_gen_buffers_t                gen_buffers;
  glitz_gl_delete_buffers_t             delete_buffers;
  glitz_gl_bind_buffer_t                bind_buffer;
  glitz_gl_buffer_data_t                buffer_data;
  glitz_gl_buffer_sub_data_t            buffer_sub_data;
  glitz_gl_get_buffer_sub_data_t        get_buffer_sub_data;
  glitz_gl_map_buffer_t                 map_buffer;
  glitz_gl_unmap_buffer_t               unmap_buffer;
  glitz_gl_gen_framebuffers_t           gen_framebuffers;
  glitz_gl_delete_framebuffers_t        delete_framebuffers;
  glitz_gl_bind_framebuffer_t           bind_framebuffer;
  glitz_gl_framebuffer_renderbuffer_t   framebuffer_renderbuffer;
  glitz_gl_framebuffer_texture_2d_t     framebuffer_texture_2d;
  glitz_gl_check_framebuffer_status_t   check_framebuffer_status;
  glitz_gl_gen_renderbuffers_t          gen_renderbuffers;
  glitz_gl_delete_renderbuffers_t       delete_renderbuffers;
  glitz_gl_bind_renderbuffer_t          bind_renderbuffer;
  glitz_gl_renderbuffer_storage_t       renderbuffer_storage;
  glitz_gl_get_renderbuffer_parameter_iv_t get_renderbuffer_parameter_iv;
} glitz_gl_proc_address_list_t;

typedef int glitz_surface_type_t;

#define GLITZ_SURFACE_TYPE_NA    -1
#define GLITZ_SURFACE_TYPE_NULL   0
#define GLITZ_SURFACE_TYPE_ARGB   1
#define GLITZ_SURFACE_TYPE_ARGBC  2
#define GLITZ_SURFACE_TYPE_ARGBF  3
#define GLITZ_SURFACE_TYPE_SOLID  4
#define GLITZ_SURFACE_TYPE_SOLIDC 5
#define GLITZ_SURFACE_TYPES       6

typedef int glitz_combine_type_t;

#define GLITZ_COMBINE_TYPE_NA             0
#define GLITZ_COMBINE_TYPE_ARGB           1
#define GLITZ_COMBINE_TYPE_ARGB_ARGB      2
#define GLITZ_COMBINE_TYPE_ARGB_ARGBC     3
#define GLITZ_COMBINE_TYPE_ARGB_ARGBF     4
#define GLITZ_COMBINE_TYPE_ARGB_SOLID     5
#define GLITZ_COMBINE_TYPE_ARGB_SOLIDC    6
#define GLITZ_COMBINE_TYPE_ARGBF          7
#define GLITZ_COMBINE_TYPE_ARGBF_ARGB     8
#define GLITZ_COMBINE_TYPE_ARGBF_ARGBC    9
#define GLITZ_COMBINE_TYPE_ARGBF_ARGBF   10
#define GLITZ_COMBINE_TYPE_ARGBF_SOLID   11
#define GLITZ_COMBINE_TYPE_ARGBF_SOLIDC  12
#define GLITZ_COMBINE_TYPE_SOLID         13
#define GLITZ_COMBINE_TYPE_SOLID_ARGB    14
#define GLITZ_COMBINE_TYPE_SOLID_ARGBC   15
#define GLITZ_COMBINE_TYPE_SOLID_ARGBF   16
#define GLITZ_COMBINE_TYPE_SOLID_SOLID   17
#define GLITZ_COMBINE_TYPE_SOLID_SOLIDC  18
#define GLITZ_COMBINE_TYPES              19

#define GLITZ_TEXTURE_NONE 0
#define GLITZ_TEXTURE_2D   1
#define GLITZ_TEXTURE_RECT 2
#define GLITZ_TEXTURE_LAST 3

#define GLITZ_FP_CONVOLUTION                 0
#define GLITZ_FP_LINEAR_GRADIENT_TRANSPARENT 1
#define GLITZ_FP_LINEAR_GRADIENT_NEAREST     2
#define GLITZ_FP_LINEAR_GRADIENT_REPEAT      3
#define GLITZ_FP_LINEAR_GRADIENT_REFLECT     4
#define GLITZ_FP_RADIAL_GRADIENT_TRANSPARENT 5
#define GLITZ_FP_RADIAL_GRADIENT_NEAREST     6
#define GLITZ_FP_RADIAL_GRADIENT_REPEAT      7
#define GLITZ_FP_RADIAL_GRADIENT_REFLECT     8
#define GLITZ_FP_COLORSPACE_YV12             9
#define GLITZ_FP_UNSUPPORTED                 10
#define GLITZ_FP_TYPES                       11

typedef struct _glitz_program_t {
  glitz_gl_int_t *name;
  unsigned int   size;
} glitz_program_t;

typedef struct _glitz_filter_map_t {
  glitz_program_t fp[GLITZ_TEXTURE_LAST][GLITZ_TEXTURE_LAST][2];
} glitz_filter_map_t;

typedef struct _glitz_program_map_t {
  glitz_filter_map_t filters[GLITZ_COMBINE_TYPES][GLITZ_FP_TYPES];
} glitz_program_map_t;

typedef enum {
  GLITZ_NONE,
  GLITZ_ANY_CONTEXT_CURRENT,
  GLITZ_CONTEXT_CURRENT,
  GLITZ_DRAWABLE_CURRENT
} glitz_constraint_t;

typedef struct _glitz_region_t {
  glitz_box_t extents;
  glitz_box_t *box;
  int         n_box;
  void        *data;
  int         size;
} glitz_region_t;

#define NULL_BOX ((glitz_box_t *) 0)

#define REGION_INIT(region, __box) \
  { \
    if (__box) { \
      (region)->extents = *(__box); \
      (region)->box = &(region)->extents; \
      (region)->n_box = 1; \
    } else { \
      (region)->extents.x1 = 0; \
      (region)->extents.y1 = 0; \
      (region)->extents.x2 = 0; \
      (region)->extents.y2 = 0; \
      (region)->box = NULL; \
      (region)->n_box = 0; \
    } \
  }

#define REGION_EMPTY(region) \
  { \
    (region)->extents.x1 = 0; \
    (region)->extents.y1 = 0; \
    (region)->extents.x2 = 0; \
    (region)->extents.y2 = 0; \
    (region)->box = NULL; \
    (region)->n_box = 0; \
  }

#define REGION_UNINIT(region) \
  { \
    REGION_EMPTY (region); \
    if ((region)->data) \
      free ((region)->data); \
    (region)->data = NULL; \
    (region)->size = 0; \
  }

#define REGION_NOTEMPTY(region) \
  ((region)->n_box)

#define REGION_RECTS(region) \
  ((region)->box)

#define REGION_NUM_RECTS(region) \
  ((region)->n_box)

#define REGION_EXTENTS(region) \
 (&(region)->extents)

#define REGION_UNION(region, box) \
  glitz_region_union (region, box)

extern glitz_status_t __internal_linkage
glitz_region_union (glitz_region_t *region,
		    glitz_box_t    *box);

#define GLITZ_DRAWABLE_TYPE_WINDOW_MASK  (1L << 0)
#define GLITZ_DRAWABLE_TYPE_PBUFFER_MASK (1L << 1)
#define GLITZ_DRAWABLE_TYPE_FBO_MASK     (1L << 2)

#define GLITZ_INT_FORMAT_WINDOW_MASK  (1L << 17)
#define GLITZ_INT_FORMAT_PBUFFER_MASK (1L << 18)
#define GLITZ_INT_FORMAT_FBO_MASK     (1L << 19)

typedef struct _glitz_int_drawable_format_t {
    glitz_drawable_format_t d;
    unsigned int            types;
    int                     caveat;
    union {
	void	  *ptr;
	long	  val;
	unsigned long uval;
	void	  *(*fptr) (void);
    } u;
} glitz_int_drawable_format_t;

typedef struct glitz_backend {
  glitz_drawable_t *
  (*create_pbuffer)            (void                    *drawable,
				glitz_drawable_format_t *format,
				unsigned int            width,
				unsigned int            height);

  void
  (*destroy)                   (void *drawable);

  glitz_bool_t
  (*push_current)              (void               *drawable,
				glitz_surface_t    *surface,
				glitz_constraint_t constraint);

  glitz_surface_t *
  (*pop_current)               (void *drawable);

  void
  (*attach_notify)             (void            *drawable,
				glitz_surface_t *surface);

  void
  (*detach_notify)             (void            *drawable,
				glitz_surface_t *surface);

  glitz_bool_t
  (*swap_buffers)              (void *drawable);


  glitz_context_t *
  (*create_context)            (void                    *drawable,
				glitz_drawable_format_t *format);

  void
  (*destroy_context)           (void *context);

  void
  (*copy_context)              (void          *src,
				void          *dst,
				unsigned long mask);

  void
  (*make_current)              (void *drawable,
				void *context);

  glitz_function_pointer_t
  (*get_proc_address)          (void       *context,
				const char *name);

  glitz_gl_proc_address_list_t *gl;

  glitz_int_drawable_format_t  *drawable_formats;
  int                          n_drawable_formats;

  glitz_gl_int_t               *texture_formats;
  glitz_format_t               *formats;
  int                          n_formats;

  glitz_gl_float_t             gl_version;
  glitz_gl_int_t               max_viewport_dims[2];
  glitz_gl_int_t               max_texture_2d_size;
  glitz_gl_int_t               max_texture_rect_size;
  unsigned long                feature_mask;

  glitz_program_map_t          *program_map;
} glitz_backend_t;

struct _glitz_drawable {
  glitz_backend_t             *backend;
  int                         ref_count;
  glitz_int_drawable_format_t *format;
  int                         width, height;
  glitz_rectangle_t           viewport;
  glitz_bool_t                update_all;
  glitz_surface_t             *front;
  glitz_surface_t             *back;
};

#define GLITZ_GL_DRAWABLE(drawable) \
  glitz_gl_proc_address_list_t *gl = (drawable)->backend->gl;

#define DRAWABLE_IS_FBO(drawable) \
  ((drawable)->format->types == GLITZ_DRAWABLE_TYPE_FBO_MASK)

typedef struct _glitz_vec2_t {
  glitz_float_t v[2];
} glitz_vec2_t;

typedef struct _glitz_vec4_t {
  glitz_float_t v[4];
} glitz_vec4_t;

#define GLITZ_TEXTURE_FLAG_ALLOCATED_MASK    (1L <<  0)
#define GLITZ_TEXTURE_FLAG_CLAMPABLE_MASK    (1L <<  1)
#define GLITZ_TEXTURE_FLAG_REPEATABLE_MASK   (1L <<  2)
#define GLITZ_TEXTURE_FLAG_PADABLE_MASK      (1L <<  3)
#define GLITZ_TEXTURE_FLAG_INVALID_SIZE_MASK (1L <<  4)

#define TEXTURE_ALLOCATED(texture) \
  ((texture)->flags & GLITZ_TEXTURE_FLAG_ALLOCATED_MASK)

#define TEXTURE_CLAMPABLE(texture) \
  ((texture)->flags & GLITZ_TEXTURE_FLAG_CLAMPABLE_MASK)

#define TEXTURE_REPEATABLE(texture) \
  ((texture)->flags & GLITZ_TEXTURE_FLAG_REPEATABLE_MASK)

#define TEXTURE_PADABLE(texture) \
  ((texture)->flags & GLITZ_TEXTURE_FLAG_PADABLE_MASK)

#define TEXTURE_INVALID_SIZE(texture) \
  ((texture)->flags & GLITZ_TEXTURE_FLAG_INVALID_SIZE_MASK)

typedef struct _glitz_texture_parameters {
    glitz_gl_enum_t filter[2];
    glitz_gl_enum_t wrap[2];
    glitz_color_t   border_color;
} glitz_texture_parameters_t;

typedef struct _glitz_texture {
  glitz_gl_uint_t name;
  glitz_gl_enum_t target;
  glitz_gl_int_t  format;
  glitz_fourcc_t  fourcc;
  unsigned long   flags;

  glitz_texture_parameters_t param;

  int             width;
  int             height;

  glitz_box_t     box;
  glitz_box_t     box_internal;

  glitz_float_t   texcoord_width_unit;
  glitz_float_t   texcoord_height_unit;
} glitz_texture_t;

struct _glitz_texture_object {
  glitz_surface_t            *surface;
  int                        ref_count;
  glitz_texture_parameters_t param;
};

struct _glitz_buffer {
  glitz_gl_uint_t  name;
  glitz_gl_enum_t  target;
  void             *data;
  int              owns_data;
  int              ref_count;
  glitz_surface_t  *front_surface;
  glitz_surface_t  *back_surface;
  glitz_drawable_t *drawable;
};

struct _glitz_multi_array {
  int ref_count;
  int size;
  int n_arrays;
  int *first;
  int *sizes;
  int *count;
  int *span, *current_span;
  glitz_vec2_t *off;
};

typedef struct _glitz_int_coordinate {
  glitz_gl_enum_t type;
  int             size, offset;
} glitz_int_coordinate_t;

typedef struct _glitz_vertex_info {
  glitz_gl_enum_t        prim;
  glitz_gl_enum_t        type;
  glitz_int_coordinate_t src;
  glitz_int_coordinate_t mask;
} glitz_vertex_info_t;

typedef struct _glitz_bitmap_info {
  glitz_bool_t     top_down;
  glitz_gl_int_t   pad;
  glitz_gl_ubyte_t *base;
} glitz_bitmap_info_t;

typedef struct _glitz_geometry {
  glitz_geometry_type_t type;
  glitz_buffer_t        *buffer;
  glitz_gl_sizei_t      stride;
  glitz_gl_float_t      data[8];
  glitz_gl_int_t        first;
  glitz_gl_int_t        size;
  glitz_gl_sizei_t      count;
  glitz_vec2_t          off;
  glitz_multi_array_t   *array;
  unsigned long         attributes;
  union {
    glitz_vertex_info_t v;
    glitz_bitmap_info_t b;
  } u;
} glitz_geometry_t;

#define GLITZ_SURFACE_FLAG_SOLID_MASK                   (1L <<  0)
#define GLITZ_SURFACE_FLAG_REPEAT_MASK                  (1L <<  1)
#define GLITZ_SURFACE_FLAG_MIRRORED_MASK                (1L <<  2)
#define GLITZ_SURFACE_FLAG_PAD_MASK                     (1L <<  3)
#define GLITZ_SURFACE_FLAG_COMPONENT_ALPHA_MASK         (1L <<  4)
#define GLITZ_SURFACE_FLAG_DITHER_MASK                  (1L <<  5)
#define GLITZ_SURFACE_FLAG_MULTISAMPLE_MASK             (1L <<  6)
#define GLITZ_SURFACE_FLAG_NICEST_MULTISAMPLE_MASK      (1L <<  7)
#define GLITZ_SURFACE_FLAG_SOLID_DAMAGE_MASK            (1L <<  8)
#define GLITZ_SURFACE_FLAG_FRAGMENT_FILTER_MASK         (1L <<  9)
#define GLITZ_SURFACE_FLAG_LINEAR_TRANSFORM_FILTER_MASK (1L << 10)
#define GLITZ_SURFACE_FLAG_IGNORE_WRAP_MASK             (1L << 11)
#define GLITZ_SURFACE_FLAG_EYE_COORDS_MASK              (1L << 12)
#define GLITZ_SURFACE_FLAG_TRANSFORM_MASK               (1L << 13)
#define GLITZ_SURFACE_FLAG_PROJECTIVE_TRANSFORM_MASK    (1L << 14)
#define GLITZ_SURFACE_FLAG_GEN_S_COORDS_MASK            (1L << 15)
#define GLITZ_SURFACE_FLAG_GEN_T_COORDS_MASK            (1L << 16)

#define GLITZ_SURFACE_FLAGS_GEN_COORDS_MASK  \
    (GLITZ_SURFACE_FLAG_GEN_S_COORDS_MASK | \
     GLITZ_SURFACE_FLAG_GEN_T_COORDS_MASK)

#define SURFACE_SOLID(surface) \
  ((surface)->flags & GLITZ_SURFACE_FLAG_SOLID_MASK)

#define SURFACE_REPEAT(surface) \
  (((surface)->flags & GLITZ_SURFACE_FLAG_REPEAT_MASK) && \
   (!((surface)->flags & GLITZ_SURFACE_FLAG_IGNORE_WRAP_MASK)))

#define SURFACE_MIRRORED(surface) \
  ((surface)->flags & GLITZ_SURFACE_FLAG_MIRRORED_MASK)

#define SURFACE_PAD(surface) \
  (((surface)->flags & GLITZ_SURFACE_FLAG_PAD_MASK) && \
   (!((surface)->flags & GLITZ_SURFACE_FLAG_IGNORE_WRAP_MASK)))

#define SURFACE_COMPONENT_ALPHA(surface) \
  ((surface)->flags & GLITZ_SURFACE_FLAG_COMPONENT_ALPHA_MASK)

#define SURFACE_DITHER(surface) \
  ((surface)->flags & GLITZ_SURFACE_FLAG_DITHER_MASK)

#define SURFACE_SOLID_DAMAGE(surface) \
  ((surface)->flags & GLITZ_SURFACE_FLAG_SOLID_DAMAGE_MASK)

#define SURFACE_FRAGMENT_FILTER(surface) \
  ((surface)->flags & GLITZ_SURFACE_FLAG_FRAGMENT_FILTER_MASK)

#define SURFACE_LINEAR_TRANSFORM_FILTER(surface) \
  ((surface)->flags & GLITZ_SURFACE_FLAG_LINEAR_TRANSFORM_FILTER_MASK)

#define SURFACE_EYE_COORDS(surface) \
  ((surface)->flags & GLITZ_SURFACE_FLAG_EYE_COORDS_MASK)

#define SURFACE_TRANSFORM(surface) \
  ((surface)->flags & GLITZ_SURFACE_FLAG_TRANSFORM_MASK)

#define SURFACE_PROJECTIVE_TRANSFORM(surface) \
  ((surface)->flags & GLITZ_SURFACE_FLAG_PROJECTIVE_TRANSFORM_MASK)

typedef struct _glitz_filter_params_t glitz_filter_params_t;

typedef struct _glitz_matrix {
  glitz_float_t t[16];
  glitz_float_t m[16];
} glitz_matrix_t;

#define GLITZ_DAMAGE_TEXTURE_MASK  (1 << 0)
#define GLITZ_DAMAGE_DRAWABLE_MASK (1 << 1)
#define GLITZ_DAMAGE_SOLID_MASK    (1 << 2)

struct _glitz_surface {
  int                   ref_count;
  glitz_format_t        *format;
  glitz_texture_t       texture;
  glitz_drawable_t      *drawable;
  glitz_drawable_t      *attached;
  unsigned long         status_mask;
  glitz_filter_t        filter;
  glitz_filter_params_t *filter_params;
  glitz_matrix_t        *transform;
  int                   x, y;
  glitz_box_t           box;
  short                 x_clip, y_clip;
  glitz_box_t           *clip;
  int                   n_clip;
  glitz_gl_enum_t       buffer;
  unsigned long         flags;
  glitz_color_t         solid;
  glitz_geometry_t      geometry;
  void                  *arrays;
  int                   n_arrays;
  int                   *first;
  unsigned int          *count;
  glitz_vec2_t          *off;
  int                   default_first;
  unsigned int          default_count;
  glitz_vec2_t          default_off;
  int                   *primcount;
  glitz_region_t        texture_damage;
  glitz_region_t        drawable_damage;
  unsigned int          flip_count;
  glitz_gl_int_t        fb;
};

#define GLITZ_GL_SURFACE(surface) \
  glitz_gl_proc_address_list_t *gl = (surface)->drawable->backend->gl;

struct _glitz_context {
  int                           ref_count;
  glitz_drawable_t              *drawable;
  void                          *closure;
  glitz_lose_current_function_t lose_current;
};

typedef struct _glitz_composite_op_t glitz_composite_op_t;

typedef void (*glitz_combine_function_t) (glitz_composite_op_t *);

typedef struct _glitz_combine_t {
  glitz_combine_type_t     type;
  glitz_combine_function_t enable;
  int                      texture_units;
  int                      source_shader;
} glitz_combine_t;

struct _glitz_composite_op_t {
  glitz_combine_type_t         type;
  glitz_combine_t              *combine;
  glitz_gl_proc_address_list_t *gl;
  glitz_operator_t             render_op;
  glitz_surface_t              *src;
  glitz_surface_t              *mask;
  glitz_surface_t              *dst;
  glitz_color_t                *solid;
  glitz_color_t                alpha_mask;
  int                          per_component;
  glitz_gl_uint_t              fp;
  int                          count;
};

typedef struct _glitz_extension_map {
  glitz_gl_float_t version;
  char             *name;
  int              mask;
} glitz_extension_map;

extern void __internal_linkage
glitz_set_operator (glitz_gl_proc_address_list_t *gl,
		    glitz_operator_t             op);

unsigned long
glitz_extensions_query (glitz_gl_float_t    version,
			const char          *extensions_string,
			glitz_extension_map *extensions_map);

typedef glitz_function_pointer_t (* glitz_get_proc_address_proc_t)
     (const char *name, void *closure);

void
glitz_backend_init (glitz_backend_t               *backend,
		    glitz_get_proc_address_proc_t get_proc_address,
		    void                          *closure);

extern unsigned int __internal_linkage
glitz_uint_to_power_of_two (unsigned int x);

extern void __internal_linkage
glitz_set_raster_pos (glitz_gl_proc_address_list_t *gl,
		      glitz_float_t                x,
		      glitz_float_t                y);

extern void __internal_linkage
glitz_clamp_value (glitz_float_t *value,
		   glitz_float_t min,
		   glitz_float_t max);

void
glitz_initiate_state (glitz_gl_proc_address_list_t *gl);

void
glitz_create_surface_formats (glitz_gl_proc_address_list_t *gl,
			      glitz_format_t               **formats,
			      glitz_gl_int_t               **texture_formats,
			      int                          *n_formats,
                              unsigned long                features);

extern void __internal_linkage
_glitz_add_drawable_formats (glitz_gl_proc_address_list_t *gl,
			     unsigned long		  feature_mask,
			     glitz_int_drawable_format_t  **formats,
			     int                          *n_formats);

void
glitz_drawable_format_copy (const glitz_drawable_format_t *src,
			    glitz_drawable_format_t	  *dst,
			    unsigned long		  mask);

glitz_drawable_format_t *
glitz_drawable_format_find (glitz_int_drawable_format_t       *formats,
			    int                               n_formats,
			    unsigned long                     mask,
			    const glitz_int_drawable_format_t *templ,
			    int                               count);

void
glitz_texture_init (glitz_texture_t *texture,
		    int             width,
		    int             height,
		    glitz_gl_int_t  texture_format,
		    glitz_fourcc_t  fourcc,
		    unsigned long   feature_mask,
		    glitz_bool_t    unnormalized);

void
glitz_texture_fini (glitz_gl_proc_address_list_t *gl,
		    glitz_texture_t              *texture);

void
glitz_texture_size_check (glitz_gl_proc_address_list_t *gl,
			  glitz_texture_t              *texture,
			  glitz_gl_int_t               max_2d,
			  glitz_gl_int_t               max_rect);

void
glitz_texture_allocate (glitz_gl_proc_address_list_t *gl,
			glitz_texture_t              *texture);

extern void __internal_linkage
glitz_texture_ensure_parameters (glitz_gl_proc_address_list_t *gl,
				 glitz_texture_t	      *texture,
				 glitz_texture_parameters_t   *param);

void
glitz_texture_bind (glitz_gl_proc_address_list_t *gl,
		    glitz_texture_t              *texture);

void
glitz_texture_unbind (glitz_gl_proc_address_list_t *gl,
		      glitz_texture_t              *texture);

void
glitz_texture_copy_drawable (glitz_gl_proc_address_list_t *gl,
			     glitz_texture_t              *texture,
			     glitz_drawable_t             *drawable,
			     int                          x_drawable,
			     int                          y_drawable,
			     int                          width,
			     int                          height,
			     int                          x_texture,
			     int                          y_texture);

void
glitz_texture_set_tex_gen (glitz_gl_proc_address_list_t *gl,
			   glitz_texture_t              *texture,
			   glitz_geometry_t             *geometry,
			   int                          x_src,
			   int                          y_src,
			   unsigned long                flags,
			   glitz_int_coordinate_t       *coord);

extern void __internal_linkage
_glitz_surface_sync_texture (glitz_surface_t *surface);

extern glitz_texture_t __internal_linkage *
glitz_surface_get_texture (glitz_surface_t *surface,
			   glitz_bool_t    allocate);

extern void __internal_linkage
glitz_surface_sync_solid (glitz_surface_t *surface);

extern glitz_bool_t __internal_linkage
glitz_surface_push_current (glitz_surface_t    *surface,
			    glitz_constraint_t constraint);

extern void __internal_linkage
glitz_surface_pop_current (glitz_surface_t *surface);

extern void __internal_linkage
glitz_surface_damage (glitz_surface_t *surface,
		      glitz_box_t     *box,
		      int             what);

extern void __internal_linkage
glitz_surface_sync_drawable (glitz_surface_t *surface);

extern void __internal_linkage
glitz_surface_status_add (glitz_surface_t *surface,
			  int             flags);

extern unsigned long __internal_linkage
glitz_status_to_status_mask (glitz_status_t status);

extern glitz_status_t __internal_linkage
glitz_status_pop_from_mask (unsigned long *mask);

void
glitz_program_map_init (glitz_program_map_t *map);

void
glitz_program_map_fini (glitz_gl_proc_address_list_t *gl,
			glitz_program_map_t          *map);

extern glitz_gl_uint_t __internal_linkage
glitz_get_fragment_program (glitz_composite_op_t *op,
			    int                  fp_type,
			    int                  id);

extern void __internal_linkage
glitz_composite_op_init (glitz_composite_op_t *op,
			 glitz_operator_t     render_op,
			 glitz_surface_t      *src,
			 glitz_surface_t      *mask,
			 glitz_surface_t      *dst);

extern void __internal_linkage
glitz_composite_enable (glitz_composite_op_t *op);

extern void __internal_linkage
glitz_composite_disable (glitz_composite_op_t *op);

extern void __internal_linkage *
glitz_buffer_bind (glitz_buffer_t  *buffer,
		   glitz_gl_enum_t target);

extern void __internal_linkage
glitz_buffer_unbind (glitz_buffer_t *buffer);

extern glitz_status_t __internal_linkage
glitz_filter_set_params (glitz_surface_t    *surface,
			 glitz_filter_t     filter,
			 glitz_fixed16_16_t *params,
			 int                n_params);

extern void __internal_linkage
glitz_filter_set_type (glitz_surface_t *surface,
		       glitz_filter_t  filter);

extern glitz_gl_uint_t __internal_linkage
glitz_filter_get_vertex_program (glitz_surface_t      *surface,
				 glitz_composite_op_t *op);

extern glitz_gl_uint_t __internal_linkage
glitz_filter_get_fragment_program (glitz_surface_t      *surface,
				   glitz_composite_op_t *op);

extern void __internal_linkage
glitz_filter_enable (glitz_surface_t      *surface,
		     glitz_composite_op_t *op);

extern void __internal_linkage
glitz_geometry_enable_none (glitz_gl_proc_address_list_t *gl,
			    glitz_surface_t              *dst,
			    glitz_box_t                  *box);

extern void __internal_linkage
glitz_geometry_enable (glitz_gl_proc_address_list_t *gl,
		       glitz_surface_t              *dst,
		       glitz_box_t                  *box);

extern void __internal_linkage
glitz_geometry_disable (glitz_surface_t *dst);

extern void __internal_linkage
glitz_geometry_draw_arrays (glitz_gl_proc_address_list_t *gl,
			    glitz_surface_t              *dst,
			    glitz_geometry_type_t        type,
			    glitz_box_t                  *bounds,
			    int                          damage);

void
_glitz_drawable_init (glitz_drawable_t	          *drawable,
		      glitz_int_drawable_format_t *format,
		      glitz_backend_t	          *backend,
		      int		          width,
		      int		          height);

extern glitz_drawable_t __internal_linkage *
_glitz_fbo_drawable_create (glitz_drawable_t	        *other,
			    glitz_int_drawable_format_t *format,
			    int	                        width,
			    int	                        height);

void
_glitz_context_init (glitz_context_t  *context,
		     glitz_drawable_t *drawable);

void
_glitz_context_fini (glitz_context_t *context);


#define MAXSHORT SHRT_MAX
#define MINSHORT SHRT_MIN

#define MIN(a,b) ((a) < (b) ? (a) : (b))
#define MAX(a,b) ((a) > (b) ? (a) : (b))

#define LSBFirst 0
#define MSBFirst 1

#ifdef WORDS_BIGENDIAN
#  define IMAGE_BYTE_ORDER MSBFirst
#  define BITMAP_BIT_ORDER MSBFirst
#else
#  define IMAGE_BYTE_ORDER LSBFirst
#  define BITMAP_BIT_ORDER LSBFirst
#endif

#define GLITZ_PI 3.14159265358979323846f

/* Fixed point updates from Carl Worth, USC, Information Sciences Institute */

#ifdef _MSC_VER
typedef __int64 glitz_fixed_32_32;
#else
#  if defined(__alpha__) || defined(__alpha) || \
      defined(ia64) || defined(__ia64__) || \
      defined(__sparc64__) || \
      defined(__s390x__) || \
      defined(x86_64) || defined (__x86_64__)
typedef long glitz_fixed_32_32;
# else
#  if defined(__GNUC__) && \
    ((__GNUC__ > 2) || \
     ((__GNUC__ == 2) && defined(__GNUC_MINOR__) && (__GNUC_MINOR__ > 7)))
__extension__
#  endif
typedef long long int glitz_fixed_32_32;
# endif
#endif

typedef uint32_t glitz_fixed_1_31;
typedef uint32_t glitz_fixed_1_16;
typedef int32_t glitz_fixed_16_16;

/*
 * An unadorned "glitz_fixed" is the same as glitz_fixed_16_16,
 * (since it's quite common in the code)
 */
typedef glitz_fixed_16_16 glitz_fixed;

#define FIXED_BITS 16

#define FIXED_TO_INT(f) (int) ((f) >> FIXED_BITS)
#define INT_TO_FIXED(i) ((glitz_fixed) (i) << FIXED_BITS)
#define FIXED_E ((glitz_fixed) 1)
#define FIXED1 (INT_TO_FIXED (1))
#define FIXED1_MINUS_E (FIXED1 - FIXED_E)
#define FIXED_FRAC(f) ((f) & FIXED1_MINUS_E)
#define FIXED_FLOOR(f) ((f) & ~FIXED1_MINUS_E)
#define FIXED_CEIL(f) FIXED_FLOOR ((f) + FIXED1_MINUS_E)

#define FIXED_FRACTION(f) ((f) & FIXED1_MINUS_E)
#define FIXED_MOD2(f) ((f) & (FIXED1 | FIXED1_MINUS_E))

#define FIXED_TO_FLOAT(f) (((glitz_float_t) (f)) / 65536)
#define FLOAT_TO_FIXED(f) ((int) ((f) * 65536))

#define SHORT_MULT(s1, s2) \
  ((s1 == 0xffff)? s2: ((s2 == 0xffff)? s1: \
  ((unsigned short) (((unsigned int) s1 * s2) / 0xffff))))

#define POWER_OF_TWO(v) ((v & (v - 1)) == 0)

/*
 * VC's math.h is pretty horrible
 */
#ifdef _MSC_VER
#define ceilf(_X)  ((float)ceil((double)(_X)))
#define sqrtf(_X)  ((float)sqrt((double)(_X)))
#define floorf(_X)  ((float)floor((double)(_X)))
#define sinf(_X) ((float)sin((double)(_X)))
#define cosf(_X) ((float)cos((double)(_X)))
#define atan2f(_X,_Y) ((float)atan2((double)(_X),(double)(_Y)))
#define sqrtf(_X) ((float)sqrt((double)(_X)))
#endif

/* Avoid unnecessary PLT entries. */

slim_hidden_proto(glitz_find_drawable_format)
slim_hidden_proto(glitz_find_pbuffer_format)
slim_hidden_proto(glitz_create_drawable)
slim_hidden_proto(glitz_create_pbuffer_drawable)
slim_hidden_proto(glitz_drawable_get_width)
slim_hidden_proto(glitz_drawable_get_height)
slim_hidden_proto(glitz_drawable_swap_buffers)
slim_hidden_proto(glitz_drawable_flush)
slim_hidden_proto(glitz_drawable_finish)
slim_hidden_proto(glitz_drawable_get_features)
slim_hidden_proto(glitz_drawable_get_format)
slim_hidden_proto(glitz_surface_set_transform)
slim_hidden_proto(glitz_surface_set_fill)
slim_hidden_proto(glitz_surface_set_component_alpha)
slim_hidden_proto(glitz_surface_set_dither)
slim_hidden_proto(glitz_surface_set_filter)
slim_hidden_proto(glitz_surface_get_width)
slim_hidden_proto(glitz_surface_get_height)
slim_hidden_proto(glitz_surface_flush)
slim_hidden_proto(glitz_surface_get_status)
slim_hidden_proto(glitz_surface_get_format)
slim_hidden_proto(glitz_surface_get_drawable)
slim_hidden_proto(glitz_surface_get_attached_drawable)
slim_hidden_proto(glitz_surface_translate_point)
slim_hidden_proto(glitz_surface_set_clip_region)
slim_hidden_proto(glitz_set_rectangle)
slim_hidden_proto(glitz_set_rectangles)
slim_hidden_proto(glitz_set_geometry)
slim_hidden_proto(glitz_set_array)
slim_hidden_proto(glitz_multi_array_create)
slim_hidden_proto(glitz_multi_array_add)
slim_hidden_proto(glitz_multi_array_reset)
slim_hidden_proto(glitz_set_multi_array)
slim_hidden_proto(glitz_buffer_set_data)
slim_hidden_proto(glitz_buffer_get_data)
slim_hidden_proto(glitz_context_create)
slim_hidden_proto(glitz_context_destroy)
slim_hidden_proto(glitz_context_reference)
slim_hidden_proto(glitz_context_copy)
slim_hidden_proto(glitz_context_set_user_data)
slim_hidden_proto(glitz_context_get_proc_address)
slim_hidden_proto(glitz_context_make_current)
slim_hidden_proto(glitz_context_bind_texture)

#endif /* GLITZINT_H_INCLUDED */