This repository was archived by the owner on Sep 7, 2021. It is now read-only.
forked from livecode/livecode-thirdparty
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcairo-gl-surface.c
More file actions
2031 lines (1721 loc) · 58.1 KB
/
Copy pathcairo-gl-surface.c
File metadata and controls
2031 lines (1721 loc) · 58.1 KB
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
/* cairo - a vector graphics library with display and print output
*
* Copyright © 2009 Eric Anholt
* Copyright © 2009 Chris Wilson
* Copyright © 2005 Red Hat, Inc
*
* 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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):
* Carl Worth <cworth@cworth.org>
*/
#include "cairoint.h"
#include "cairo-gl-private.h"
slim_hidden_proto (cairo_gl_context_reference);
slim_hidden_proto (cairo_gl_context_destroy);
#define BIAS .375
static inline float
int_as_float (uint32_t val)
{
union fi {
float f;
uint32_t u;
} fi;
fi.u = val;
return fi.f;
}
static const cairo_gl_context_t _nil_context = {
CAIRO_REFERENCE_COUNT_INVALID,
CAIRO_STATUS_NO_MEMORY
};
static const cairo_gl_context_t _nil_context__invalid_format = {
CAIRO_REFERENCE_COUNT_INVALID,
CAIRO_STATUS_INVALID_FORMAT
};
static cairo_bool_t _cairo_surface_is_gl (cairo_surface_t *surface)
{
return surface->backend == &_cairo_gl_surface_backend;
}
cairo_gl_context_t *
_cairo_gl_context_create_in_error (cairo_status_t status)
{
if (status == CAIRO_STATUS_NO_MEMORY)
return (cairo_gl_context_t *) &_nil_context;
if (status == CAIRO_STATUS_INVALID_FORMAT)
return (cairo_gl_context_t *) &_nil_context__invalid_format;
ASSERT_NOT_REACHED;
return NULL;
}
cairo_status_t
_cairo_gl_context_init (cairo_gl_context_t *ctx)
{
int n;
ctx->status = CAIRO_STATUS_SUCCESS;
CAIRO_REFERENCE_COUNT_INIT (&ctx->ref_count, 1);
CAIRO_MUTEX_INIT (ctx->mutex);
memset (ctx->glyph_cache, 0, sizeof (ctx->glyph_cache));
if (glewInit () != GLEW_OK)
return _cairo_error (CAIRO_STATUS_INVALID_FORMAT); /* XXX */
if (! GLEW_EXT_framebuffer_object ||
! GLEW_ARB_texture_env_combine ||
! GLEW_ARB_texture_non_power_of_two)
{
fprintf (stderr,
"Required GL extensions not available:\n");
if (! GLEW_EXT_framebuffer_object)
fprintf (stderr, " GL_EXT_framebuffer_object\n");
if (! GLEW_ARB_texture_env_combine)
fprintf (stderr, " GL_ARB_texture_env_combine\n");
if (! GLEW_ARB_texture_non_power_of_two)
fprintf (stderr, " GL_ARB_texture_non_power_of_two\n");
return _cairo_error (CAIRO_STATUS_INVALID_FORMAT); /* XXX */
}
/* Set up the dummy texture for tex_env_combine with constant color. */
glGenTextures (1, &ctx->dummy_tex);
glBindTexture (GL_TEXTURE_2D, ctx->dummy_tex);
glTexImage2D (GL_TEXTURE_2D, 0, GL_RGBA, 1, 1, 0,
GL_RGBA, GL_UNSIGNED_BYTE, NULL);
ctx->max_framebuffer_size = 0;
glGetIntegerv (GL_MAX_RENDERBUFFER_SIZE, &ctx->max_framebuffer_size);
ctx->max_texture_size = 0;
glGetIntegerv (GL_MAX_TEXTURE_SIZE, &ctx->max_texture_size);
for (n = 0; n < ARRAY_LENGTH (ctx->glyph_cache); n++)
_cairo_gl_glyph_cache_init (&ctx->glyph_cache[n]);
return CAIRO_STATUS_SUCCESS;
}
cairo_gl_context_t *
cairo_gl_context_reference (cairo_gl_context_t *context)
{
if (context == NULL ||
CAIRO_REFERENCE_COUNT_IS_INVALID (&context->ref_count))
{
return context;
}
assert (CAIRO_REFERENCE_COUNT_HAS_REFERENCE (&context->ref_count));
_cairo_reference_count_inc (&context->ref_count);
return context;
}
slim_hidden_def (cairo_gl_context_reference);
void
cairo_gl_context_destroy (cairo_gl_context_t *context)
{
int n;
if (context == NULL ||
CAIRO_REFERENCE_COUNT_IS_INVALID (&context->ref_count))
{
return;
}
assert (CAIRO_REFERENCE_COUNT_HAS_REFERENCE (&context->ref_count));
if (! _cairo_reference_count_dec_and_test (&context->ref_count))
return;
glDeleteTextures (1, &context->dummy_tex);
for (n = 0; n < ARRAY_LENGTH (context->glyph_cache); n++)
_cairo_gl_glyph_cache_fini (&context->glyph_cache[n]);
context->destroy (context);
free (context);
}
slim_hidden_def (cairo_gl_context_destroy);
cairo_gl_context_t *
_cairo_gl_context_acquire (cairo_gl_context_t *ctx)
{
CAIRO_MUTEX_LOCK (ctx->mutex);
return ctx;
}
cairo_bool_t
_cairo_gl_get_image_format_and_type (pixman_format_code_t pixman_format,
GLenum *internal_format, GLenum *format,
GLenum *type, cairo_bool_t *has_alpha)
{
*has_alpha = TRUE;
switch (pixman_format) {
case PIXMAN_a8r8g8b8:
*internal_format = GL_RGBA;
*format = GL_BGRA;
*type = GL_UNSIGNED_INT_8_8_8_8_REV;
return TRUE;
case PIXMAN_x8r8g8b8:
*internal_format = GL_RGB;
*format = GL_BGRA;
*type = GL_UNSIGNED_INT_8_8_8_8_REV;
*has_alpha = FALSE;
return TRUE;
case PIXMAN_a8b8g8r8:
*internal_format = GL_RGBA;
*format = GL_RGBA;
*type = GL_UNSIGNED_INT_8_8_8_8_REV;
return TRUE;
case PIXMAN_x8b8g8r8:
*internal_format = GL_RGB;
*format = GL_RGBA;
*type = GL_UNSIGNED_INT_8_8_8_8_REV;
*has_alpha = FALSE;
return TRUE;
case PIXMAN_b8g8r8a8:
*internal_format = GL_BGRA;
*format = GL_BGRA;
*type = GL_UNSIGNED_INT_8_8_8_8;
return TRUE;
case PIXMAN_b8g8r8x8:
*internal_format = GL_RGB;
*format = GL_BGRA;
*type = GL_UNSIGNED_INT_8_8_8_8;
*has_alpha = FALSE;
return TRUE;
case PIXMAN_r8g8b8:
*internal_format = GL_RGB;
*format = GL_RGB;
*type = GL_UNSIGNED_BYTE;
return TRUE;
case PIXMAN_b8g8r8:
*internal_format = GL_RGB;
*format = GL_BGR;
*type = GL_UNSIGNED_BYTE;
return TRUE;
case PIXMAN_r5g6b5:
*internal_format = GL_RGB;
*format = GL_RGB;
*type = GL_UNSIGNED_SHORT_5_6_5;
return TRUE;
case PIXMAN_b5g6r5:
*internal_format = GL_RGB;
*format = GL_RGB;
*type = GL_UNSIGNED_SHORT_5_6_5_REV;
return TRUE;
case PIXMAN_a1r5g5b5:
*internal_format = GL_RGBA;
*format = GL_BGRA;
*type = GL_UNSIGNED_SHORT_1_5_5_5_REV;
return TRUE;
case PIXMAN_x1r5g5b5:
*internal_format = GL_RGB;
*format = GL_BGRA;
*type = GL_UNSIGNED_SHORT_1_5_5_5_REV;
*has_alpha = FALSE;
return TRUE;
case PIXMAN_a1b5g5r5:
*internal_format = GL_RGBA;
*format = GL_RGBA;
*type = GL_UNSIGNED_SHORT_1_5_5_5_REV;
return TRUE;
case PIXMAN_x1b5g5r5:
*internal_format = GL_RGB;
*format = GL_RGBA;
*type = GL_UNSIGNED_SHORT_1_5_5_5_REV;
*has_alpha = FALSE;
return TRUE;
case PIXMAN_a8:
*internal_format = GL_ALPHA;
*format = GL_ALPHA;
*type = GL_UNSIGNED_BYTE;
return TRUE;
case PIXMAN_a2b10g10r10:
case PIXMAN_x2b10g10r10:
case PIXMAN_a4r4g4b4:
case PIXMAN_x4r4g4b4:
case PIXMAN_a4b4g4r4:
case PIXMAN_x4b4g4r4:
case PIXMAN_r3g3b2:
case PIXMAN_b2g3r3:
case PIXMAN_a2r2g2b2:
case PIXMAN_a2b2g2r2:
case PIXMAN_c8:
case PIXMAN_x4a4:
/* case PIXMAN_x4c4: */
case PIXMAN_x4g4:
case PIXMAN_a4:
case PIXMAN_r1g2b1:
case PIXMAN_b1g2r1:
case PIXMAN_a1r1g1b1:
case PIXMAN_a1b1g1r1:
case PIXMAN_c4:
case PIXMAN_g4:
case PIXMAN_a1:
case PIXMAN_g1:
case PIXMAN_yuy2:
case PIXMAN_yv12:
#if PIXMAN_VERSION >= PIXMAN_VERSION_ENCODE(0,15,16)
case PIXMAN_x2r10g10b10:
case PIXMAN_a2r10g10b10:
#endif
default:
return FALSE;
}
}
void
_cairo_gl_context_release (cairo_gl_context_t *ctx)
{
CAIRO_MUTEX_UNLOCK (ctx->mutex);
}
void
_cairo_gl_set_destination (cairo_gl_surface_t *surface)
{
cairo_gl_context_t *ctx = surface->ctx;
if (ctx->current_target != surface) {
ctx->current_target = surface;
if (surface->fb) {
glBindFramebufferEXT (GL_FRAMEBUFFER_EXT, surface->fb);
glDrawBuffer (GL_COLOR_ATTACHMENT0_EXT);
glReadBuffer (GL_COLOR_ATTACHMENT0_EXT);
} else {
ctx->make_current (ctx, surface);
glBindFramebufferEXT (GL_FRAMEBUFFER_EXT, 0);
glDrawBuffer (GL_BACK_LEFT);
glReadBuffer (GL_BACK_LEFT);
}
}
glViewport (0, 0, surface->width, surface->height);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
if (surface->fb)
glOrtho (0, surface->width, 0, surface->height, -1.0, 1.0);
else
glOrtho (0, surface->width, surface->height, 0, -1.0, 1.0);
glMatrixMode (GL_MODELVIEW);
glLoadIdentity ();
}
cairo_bool_t
_cairo_gl_operator_is_supported (cairo_operator_t op)
{
return op < CAIRO_OPERATOR_SATURATE;
}
void
_cairo_gl_set_operator (cairo_gl_surface_t *dst, cairo_operator_t op)
{
struct {
GLenum src;
GLenum dst;
} blend_factors[] = {
{ GL_ZERO, GL_ZERO }, /* Clear */
{ GL_ONE, GL_ZERO }, /* Source */
{ GL_ONE, GL_ONE_MINUS_SRC_ALPHA }, /* Over */
{ GL_DST_ALPHA, GL_ZERO }, /* In */
{ GL_ONE_MINUS_DST_ALPHA, GL_ZERO }, /* Out */
{ GL_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA }, /* Atop */
{ GL_ZERO, GL_ONE }, /* Dest */
{ GL_ONE_MINUS_DST_ALPHA, GL_ONE }, /* DestOver */
{ GL_ZERO, GL_SRC_ALPHA }, /* DestIn */
{ GL_ZERO, GL_ONE_MINUS_SRC_ALPHA }, /* DestOut */
{ GL_ONE_MINUS_DST_ALPHA, GL_SRC_ALPHA }, /* DestAtop */
{ GL_ONE_MINUS_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA }, /* Xor */
{ GL_ONE, GL_ONE }, /* Add */
};
GLenum src_factor, dst_factor;
assert (op < ARRAY_LENGTH (blend_factors));
src_factor = blend_factors[op].src;
dst_factor = blend_factors[op].dst;
/* We may have a visual with alpha bits despite the user requesting
* CAIRO_CONTENT_COLOR. So clear out those bits in that case.
*/
if (dst->base.content == CAIRO_CONTENT_COLOR) {
if (src_factor == GL_ONE_MINUS_DST_ALPHA)
src_factor = GL_ZERO;
if (src_factor == GL_DST_ALPHA)
src_factor = GL_ONE;
}
glEnable (GL_BLEND);
glBlendFunc (src_factor, dst_factor);
}
static void
_cairo_gl_set_texture_surface (int tex_unit, GLuint tex,
cairo_surface_attributes_t *attributes)
{
glActiveTexture (GL_TEXTURE0 + tex_unit);
glBindTexture (GL_TEXTURE_2D, tex);
switch (attributes->extend) {
case CAIRO_EXTEND_NONE:
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
break;
case CAIRO_EXTEND_PAD:
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
break;
case CAIRO_EXTEND_REPEAT:
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
break;
case CAIRO_EXTEND_REFLECT:
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_MIRRORED_REPEAT);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_MIRRORED_REPEAT);
break;
}
switch (attributes->filter) {
case CAIRO_FILTER_FAST:
case CAIRO_FILTER_NEAREST:
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
break;
case CAIRO_FILTER_GOOD:
case CAIRO_FILTER_BEST:
case CAIRO_FILTER_BILINEAR:
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
break;
default:
case CAIRO_FILTER_GAUSSIAN:
ASSERT_NOT_REACHED;
}
glEnable (GL_TEXTURE_2D);
}
void
_cairo_gl_surface_init (cairo_gl_context_t *ctx,
cairo_gl_surface_t *surface,
cairo_content_t content,
int width, int height)
{
_cairo_surface_init (&surface->base,
&_cairo_gl_surface_backend,
content);
surface->ctx = cairo_gl_context_reference (ctx);
surface->width = width;
surface->height = height;
}
cairo_surface_t *
cairo_gl_surface_create (cairo_gl_context_t *ctx,
cairo_content_t content,
int width,
int height)
{
cairo_gl_surface_t *surface;
GLenum err, format;
cairo_status_t status;
if (!CAIRO_CONTENT_VALID (content))
return _cairo_surface_create_in_error (_cairo_error (CAIRO_STATUS_INVALID_CONTENT));
if (ctx == NULL) {
return cairo_image_surface_create (_cairo_format_from_content (content),
width, height);
}
if (ctx->status)
return _cairo_surface_create_in_error (ctx->status);
if (width > ctx->max_framebuffer_size || height > ctx->max_framebuffer_size)
return _cairo_surface_create_in_error (_cairo_error (CAIRO_STATUS_INVALID_SIZE));
surface = calloc (1, sizeof (cairo_gl_surface_t));
if (unlikely (surface == NULL))
return _cairo_surface_create_in_error (_cairo_error (CAIRO_STATUS_NO_MEMORY));
_cairo_gl_surface_init (ctx, surface, content, width, height);
switch (content) {
default:
ASSERT_NOT_REACHED;
case CAIRO_CONTENT_COLOR_ALPHA:
format = GL_RGBA;
break;
case CAIRO_CONTENT_ALPHA:
format = GL_RGBA;
break;
case CAIRO_CONTENT_COLOR:
format = GL_RGB;
break;
}
/* Create the texture used to store the surface's data. */
glGenTextures (1, &surface->tex);
glBindTexture (GL_TEXTURE_2D, surface->tex);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexImage2D (GL_TEXTURE_2D, 0, format, width, height, 0,
format, GL_UNSIGNED_BYTE, NULL);
/* Create a framebuffer object wrapping the texture so that we can render
* to it.
*/
glGenFramebuffersEXT (1, &surface->fb);
glBindFramebufferEXT (GL_FRAMEBUFFER_EXT, surface->fb);
glFramebufferTexture2DEXT (GL_FRAMEBUFFER_EXT,
GL_COLOR_ATTACHMENT0_EXT,
GL_TEXTURE_2D,
surface->tex,
0);
while ((err = glGetError ())) {
fprintf (stderr, "GL error in surface create: 0x%08x\n", err);
}
status = glCheckFramebufferStatusEXT (GL_FRAMEBUFFER_EXT);
if (status != GL_FRAMEBUFFER_COMPLETE_EXT)
fprintf (stderr, "destination is framebuffer incomplete\n");
/* Cairo surfaces start out initialized to transparent (black) */
ctx = _cairo_gl_context_acquire (surface->ctx);
_cairo_gl_set_destination (surface);
glClearColor (0.0, 0.0, 0.0, 0.0);
glClear (GL_COLOR_BUFFER_BIT);
_cairo_gl_context_release (ctx);
return &surface->base;
}
slim_hidden_def (cairo_gl_surface_create);
void
cairo_gl_surface_set_size (cairo_surface_t *abstract_surface,
int width,
int height)
{
cairo_gl_surface_t *surface = (cairo_gl_surface_t *) abstract_surface;
cairo_status_t status;
if (! _cairo_surface_is_gl (abstract_surface) || surface->fb) {
status = _cairo_surface_set_error (abstract_surface,
CAIRO_STATUS_SURFACE_TYPE_MISMATCH);
return;
}
surface->width = width;
surface->height = height;
}
int
cairo_gl_surface_get_width (cairo_surface_t *abstract_surface)
{
cairo_gl_surface_t *surface = (cairo_gl_surface_t *) abstract_surface;
if (! _cairo_surface_is_gl (abstract_surface))
return 0;
return surface->width;
}
int
cairo_gl_surface_get_height (cairo_surface_t *abstract_surface)
{
cairo_gl_surface_t *surface = (cairo_gl_surface_t *) abstract_surface;
if (! _cairo_surface_is_gl (abstract_surface))
return 0;
return surface->height;
}
void
cairo_gl_surface_swapbuffers (cairo_surface_t *abstract_surface)
{
cairo_gl_surface_t *surface = (cairo_gl_surface_t *) abstract_surface;
cairo_status_t status;
if (! _cairo_surface_is_gl (abstract_surface)) {
status = _cairo_surface_set_error (abstract_surface,
CAIRO_STATUS_SURFACE_TYPE_MISMATCH);
return;
}
if (! surface->fb)
surface->ctx->swap_buffers (surface->ctx, surface);
}
static cairo_surface_t *
_cairo_gl_surface_create_similar (void *abstract_surface,
cairo_content_t content,
int width,
int height)
{
cairo_gl_surface_t *surface = abstract_surface;
assert (CAIRO_CONTENT_VALID (content));
if (width > surface->ctx->max_framebuffer_size ||
height > surface->ctx->max_framebuffer_size)
{
return NULL;
}
if (width < 1)
width = 1;
if (height < 1)
height = 1;
return cairo_gl_surface_create (surface->ctx, content, width, height);
}
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)
{
GLenum internal_format, format, type;
cairo_bool_t has_alpha;
cairo_image_surface_t *clone = NULL;
int cpp;
if (! _cairo_gl_get_image_format_and_type (src->pixman_format,
&internal_format,
&format,
&type,
&has_alpha))
{
cairo_bool_t is_supported;
clone = _cairo_image_surface_coerce (src,
_cairo_format_from_content (src->base.content));
if (unlikely (clone->base.status))
return clone->base.status;
is_supported =
_cairo_gl_get_image_format_and_type (clone->pixman_format,
&internal_format,
&format,
&type,
&has_alpha);
assert (is_supported);
src = clone;
}
cpp = PIXMAN_FORMAT_BPP (src->pixman_format) / 8;
glBindTexture (GL_TEXTURE_2D, dst->tex);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glPixelStorei (GL_UNPACK_ALIGNMENT, 1);
glPixelStorei (GL_UNPACK_ROW_LENGTH, src->stride / cpp);
glTexSubImage2D (GL_TEXTURE_2D, 0,
dst_x, dst_y, width, height,
format, GL_UNSIGNED_BYTE,
src->data + src_y * src->stride + src_x * cpp);
glPixelStorei (GL_UNPACK_ROW_LENGTH, 0);
cairo_surface_destroy (&clone->base);
return CAIRO_STATUS_SUCCESS;
}
static cairo_status_t
_cairo_gl_surface_get_image (cairo_gl_surface_t *surface,
cairo_rectangle_int_t *interest,
cairo_image_surface_t **image_out,
cairo_rectangle_int_t *rect_out)
{
cairo_image_surface_t *image;
GLenum err;
GLenum format, type;
cairo_format_t cairo_format;
unsigned int cpp;
/* Want to use a switch statement here but the compiler gets whiny. */
if (surface->base.content == CAIRO_CONTENT_COLOR_ALPHA) {
format = GL_BGRA;
cairo_format = CAIRO_FORMAT_ARGB32;
type = GL_UNSIGNED_INT_8_8_8_8_REV;
cpp = 4;
} else if (surface->base.content == CAIRO_CONTENT_COLOR) {
format = GL_BGRA;
cairo_format = CAIRO_FORMAT_RGB24;
type = GL_UNSIGNED_INT_8_8_8_8_REV;
cpp = 4;
} else if (surface->base.content == CAIRO_CONTENT_ALPHA) {
format = GL_ALPHA;
cairo_format = CAIRO_FORMAT_A8;
type = GL_UNSIGNED_BYTE;
cpp = 1;
} else {
ASSERT_NOT_REACHED;
return CAIRO_INT_STATUS_UNSUPPORTED;
}
image = (cairo_image_surface_t*)
cairo_image_surface_create (cairo_format,
interest->width, interest->height);
if (unlikely (image->base.status))
return image->base.status;
/* This is inefficient, as we'd rather just read the thing without making
* it the destination. But then, this is the fallback path, so let's not
* fall back instead.
*/
_cairo_gl_set_destination (surface);
glPixelStorei (GL_PACK_ALIGNMENT, 1);
glPixelStorei (GL_PACK_ROW_LENGTH, image->stride / cpp);
glReadPixels (interest->x, interest->y,
interest->width, interest->height,
format, type, image->data);
while ((err = glGetError ()))
fprintf (stderr, "GL error 0x%08x\n", (int) err);
*image_out = image;
if (rect_out != NULL)
*rect_out = *interest;
return CAIRO_STATUS_SUCCESS;
}
static cairo_status_t
_cairo_gl_surface_finish (void *abstract_surface)
{
cairo_gl_surface_t *surface = abstract_surface;
glDeleteFramebuffersEXT (1, &surface->fb);
glDeleteTextures (1, &surface->tex);
if (surface->ctx->current_target == surface)
surface->ctx->current_target = NULL;
cairo_gl_context_destroy (surface->ctx);
return CAIRO_STATUS_SUCCESS;
}
static cairo_status_t
_cairo_gl_surface_acquire_source_image (void *abstract_surface,
cairo_image_surface_t **image_out,
void **image_extra)
{
cairo_gl_surface_t *surface = abstract_surface;
cairo_rectangle_int_t extents;
*image_extra = NULL;
extents.x = extents.y = 0;
extents.width = surface->width;
extents.height = surface->height;
return _cairo_gl_surface_get_image (surface, &extents, image_out, NULL);
}
static void
_cairo_gl_surface_release_source_image (void *abstract_surface,
cairo_image_surface_t *image,
void *image_extra)
{
cairo_surface_destroy (&image->base);
}
static cairo_status_t
_cairo_gl_surface_acquire_dest_image (void *abstract_surface,
cairo_rectangle_int_t *interest_rect,
cairo_image_surface_t **image_out,
cairo_rectangle_int_t *image_rect_out,
void **image_extra)
{
cairo_gl_surface_t *surface = abstract_surface;
*image_extra = NULL;
return _cairo_gl_surface_get_image (surface, interest_rect, image_out,
image_rect_out);
}
static void
_cairo_gl_surface_release_dest_image (void *abstract_surface,
cairo_rectangle_int_t *interest_rect,
cairo_image_surface_t *image,
cairo_rectangle_int_t *image_rect,
void *image_extra)
{
cairo_status_t status;
status = _cairo_gl_surface_draw_image (abstract_surface, image,
0, 0,
image->width, image->height,
image_rect->x, image_rect->y);
/* as we created the image, its format should be directly applicable */
assert (status == CAIRO_STATUS_SUCCESS);
cairo_surface_destroy (&image->base);
}
static cairo_status_t
_cairo_gl_surface_clone_similar (void *abstract_surface,
cairo_surface_t *src,
cairo_content_t content,
int src_x,
int src_y,
int width,
int height,
int *clone_offset_x,
int *clone_offset_y,
cairo_surface_t **clone_out)
{
cairo_gl_surface_t *surface = abstract_surface;
if (src->backend == surface->base.backend) {
*clone_offset_x = 0;
*clone_offset_y = 0;
*clone_out = cairo_surface_reference (src);
return CAIRO_STATUS_SUCCESS;
} else if (_cairo_surface_is_image (src)) {
cairo_image_surface_t *image_src = (cairo_image_surface_t *)src;
cairo_gl_surface_t *clone;
cairo_status_t status;
clone = (cairo_gl_surface_t *)
_cairo_gl_surface_create_similar (&surface->base,
content,
width, height);
if (clone == NULL)
return CAIRO_INT_STATUS_UNSUPPORTED;
if (clone->base.status)
return clone->base.status;
status = _cairo_gl_surface_draw_image (clone, image_src,
src_x, src_y,
width, height,
0, 0);
if (status) {
cairo_surface_destroy (&clone->base);
return status;
}
*clone_out = &clone->base;
*clone_offset_x = src_x;
*clone_offset_y = src_y;
return CAIRO_STATUS_SUCCESS;
}
return CAIRO_INT_STATUS_UNSUPPORTED;
}
/** Creates a cairo-gl pattern surface for the given trapezoids */
static cairo_status_t
_cairo_gl_get_traps_pattern (cairo_gl_surface_t *dst,
int dst_x, int dst_y,
int width, int height,
cairo_trapezoid_t *traps,
int num_traps,
cairo_antialias_t antialias,
cairo_surface_pattern_t *pattern)
{
pixman_format_code_t pixman_format;
pixman_image_t *image;
cairo_surface_t *surface;
int i;
pixman_format = antialias != CAIRO_ANTIALIAS_NONE ? PIXMAN_a8 : PIXMAN_a1,
image = pixman_image_create_bits (pixman_format, width, height, NULL, 0);
if (unlikely (image == NULL))
return _cairo_error (CAIRO_STATUS_NO_MEMORY);
for (i = 0; i < num_traps; i++) {
pixman_trapezoid_t trap;
trap.top = _cairo_fixed_to_16_16 (traps[i].top);
trap.bottom = _cairo_fixed_to_16_16 (traps[i].bottom);
trap.left.p1.x = _cairo_fixed_to_16_16 (traps[i].left.p1.x);
trap.left.p1.y = _cairo_fixed_to_16_16 (traps[i].left.p1.y);
trap.left.p2.x = _cairo_fixed_to_16_16 (traps[i].left.p2.x);
trap.left.p2.y = _cairo_fixed_to_16_16 (traps[i].left.p2.y);
trap.right.p1.x = _cairo_fixed_to_16_16 (traps[i].right.p1.x);
trap.right.p1.y = _cairo_fixed_to_16_16 (traps[i].right.p1.y);
trap.right.p2.x = _cairo_fixed_to_16_16 (traps[i].right.p2.x);
trap.right.p2.y = _cairo_fixed_to_16_16 (traps[i].right.p2.y);
pixman_rasterize_trapezoid (image, &trap, -dst_x, -dst_y);
}
surface = _cairo_image_surface_create_for_pixman_image (image,
pixman_format);
if (unlikely (surface->status)) {
pixman_image_unref (image);
return surface->status;
}
_cairo_pattern_init_for_surface (pattern, surface);
cairo_surface_destroy (surface);
return CAIRO_STATUS_SUCCESS;
}
static cairo_status_t
_cairo_gl_pattern_surface_texture_setup (cairo_gl_composite_operand_t *operand,
const cairo_surface_pattern_t *src,
cairo_gl_surface_t *dst,
int src_x, int src_y,
int dst_x, int dst_y)
{
cairo_surface_t *source = src->surface;
cairo_gl_surface_t *gl_surface;
cairo_status_t status;
cairo_matrix_t m;
cairo_surface_attributes_t *attributes;
if (source->backend != &_cairo_gl_surface_backend) {
gl_surface = (cairo_gl_surface_t *)
_cairo_surface_has_snapshot (source,
&_cairo_gl_surface_backend,
source->content);
if (gl_surface == NULL) {
cairo_image_surface_t *image;
void *image_extra;
status = _cairo_surface_acquire_source_image (source,
&image, &image_extra);
if (unlikely (status))
return status;
gl_surface = (cairo_gl_surface_t *)
_cairo_gl_surface_create_similar (&dst->base,
image->base.content,
image->width, image->height);
if (gl_surface == NULL) {
_cairo_surface_release_source_image (source,
image, image_extra);
return CAIRO_INT_STATUS_UNSUPPORTED;
}
if (unlikely (gl_surface->base.status)) {
_cairo_surface_release_source_image (source,
image, image_extra);
return gl_surface->base.status;
}
status = _cairo_gl_surface_draw_image (gl_surface, image,
0, 0,
image->width, image->height,
0, 0);
_cairo_surface_release_source_image (source, image, image_extra);
if (unlikely (status)) {
cairo_surface_destroy (&gl_surface->base);
return status;
}
status = _cairo_surface_attach_snapshot (source,
&gl_surface->base,
cairo_surface_destroy);
if (unlikely (status)) {
cairo_surface_destroy (&gl_surface->base);
return status;
}
/* XXX consider placing a bound on snapshot caches */
}
} else {
gl_surface = (cairo_gl_surface_t *) source;
}
operand->operand.texture.surface = NULL;
operand->operand.texture.tex = gl_surface->tex;
switch (gl_surface->base.content) {
case CAIRO_CONTENT_ALPHA:
case CAIRO_CONTENT_COLOR_ALPHA:
operand->operand.texture.has_alpha = TRUE;
break;
case CAIRO_CONTENT_COLOR:
operand->operand.texture.has_alpha = FALSE;
break;
}
attributes = &operand->operand.texture.attributes;
operand->type = OPERAND_TEXTURE;
operand->operand.texture.tex = gl_surface->tex;
operand->operand.texture.surface = NULL;
attributes->matrix = src->base.matrix;
attributes->extend = src->base.extend;
attributes->filter = src->base.filter;
/* Demote the filter if we're doing a 1:1 mapping of pixels. */
if ((attributes->filter == CAIRO_FILTER_GOOD ||
attributes->filter == CAIRO_FILTER_BEST ||
attributes->filter == CAIRO_FILTER_BILINEAR) &&
_cairo_matrix_is_pixel_exact (&attributes->matrix))
{
attributes->filter = CAIRO_FILTER_NEAREST;
}