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
|
/* -*- Mode: c; c-basic-offset: 4; indent-tabs-mode: t; tab-width: 8; -*- */
/* cairo - a vector graphics library with display and print output
*
* Copyright © 2002 University of Southern California
* 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., 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 University of Southern
* California.
*
* Contributor(s):
* Carl D. Worth <cworth@cworth.org>
* Behdad Esfahbod <behdad@behdad.org>
* Chris Wilson <chris@chris-wilson.co.uk>
* Karl Tomlinson <karlt+@karlt.net>, Mozilla Corporation
*/
#include "cairoint.h"
#if !CAIRO_HAS_XLIB_XCB_FUNCTIONS
#include "cairo-xlib-private.h"
#include "cairo-xlib-surface-private.h"
#include "cairo-error-private.h"
#include "cairo-image-surface-inline.h"
#include "cairo-paginated-private.h"
#include "cairo-pattern-inline.h"
#include "cairo-recording-surface-private.h"
#include "cairo-surface-backend-private.h"
#include "cairo-surface-offset-private.h"
#include "cairo-surface-observer-private.h"
#include "cairo-surface-snapshot-inline.h"
#include "cairo-surface-subsurface-inline.h"
#define PIXMAN_MAX_INT ((pixman_fixed_1 >> 1) - pixman_fixed_e) /* need to ensure deltas also fit */
static cairo_xlib_surface_t *
unwrap_source (const cairo_surface_pattern_t *pattern)
{
cairo_rectangle_int_t limits;
return (cairo_xlib_surface_t *)_cairo_pattern_get_source (pattern, &limits);
}
static cairo_status_t
_cairo_xlib_source_finish (void *abstract_surface)
{
cairo_xlib_source_t *source = abstract_surface;
XRenderFreePicture (source->dpy, source->picture);
if (source->pixmap)
XFreePixmap (source->dpy, source->pixmap);
return CAIRO_STATUS_SUCCESS;
}
static const cairo_surface_backend_t cairo_xlib_source_backend = {
CAIRO_SURFACE_TYPE_XLIB,
_cairo_xlib_source_finish,
NULL, /* read-only wrapper */
};
static cairo_status_t
_cairo_xlib_proxy_finish (void *abstract_surface)
{
cairo_xlib_proxy_t *proxy = abstract_surface;
_cairo_xlib_shm_surface_mark_active (proxy->owner);
XRenderFreePicture (proxy->source.dpy, proxy->source.picture);
if (proxy->source.pixmap)
XFreePixmap (proxy->source.dpy, proxy->source.pixmap);
cairo_surface_destroy (proxy->owner);
return CAIRO_STATUS_SUCCESS;
}
static const cairo_surface_backend_t cairo_xlib_proxy_backend = {
CAIRO_SURFACE_TYPE_XLIB,
_cairo_xlib_proxy_finish,
NULL, /* read-only wrapper */
};
static cairo_surface_t *
source (cairo_xlib_surface_t *dst, Picture picture, Pixmap pixmap)
{
cairo_xlib_source_t *source;
if (picture == None)
return _cairo_surface_create_in_error (_cairo_error (CAIRO_STATUS_NO_MEMORY));
source = _cairo_malloc (sizeof (*source));
if (unlikely (source == NULL)) {
XRenderFreePicture (dst->display->display, picture);
if (pixmap)
XFreePixmap (dst->display->display, pixmap);
return _cairo_surface_create_in_error (_cairo_error (CAIRO_STATUS_NO_MEMORY));
}
_cairo_surface_init (&source->base,
&cairo_xlib_source_backend,
NULL, /* device */
CAIRO_CONTENT_COLOR_ALPHA,
FALSE); /* is_vector */
/* The source exists only within an operation */
source->picture = picture;
source->pixmap = pixmap;
source->dpy = dst->display->display;
return &source->base;
}
static uint32_t
hars_petruska_f54_1_random (void)
{
#define rol(x,k) ((x << k) | (x >> (32-k)))
static uint32_t x;
return x = (x ^ rol (x, 5) ^ rol (x, 24)) + 0x37798849;
#undef rol
}
static const XTransform identity = {
{
{ 1 << 16, 0x00000, 0x00000 },
{ 0x00000, 1 << 16, 0x00000 },
{ 0x00000, 0x00000, 1 << 16 },
}
};
static cairo_bool_t
picture_set_matrix (cairo_xlib_display_t *display,
Picture picture,
const cairo_matrix_t *matrix,
cairo_filter_t filter,
double xc,
double yc,
int *x_offset,
int *y_offset)
{
XTransform xtransform;
pixman_transform_t *pixman_transform;
cairo_int_status_t status;
/* Casting between pixman_transform_t and XTransform is safe because
* they happen to be the exact same type.
*/
pixman_transform = (pixman_transform_t *) &xtransform;
status = _cairo_matrix_to_pixman_matrix_offset (matrix, filter, xc, yc,
pixman_transform,
x_offset, y_offset);
if (status == CAIRO_INT_STATUS_NOTHING_TO_DO)
return TRUE;
if (unlikely (status != CAIRO_INT_STATUS_SUCCESS))
return FALSE;
if (memcmp (&xtransform, &identity, sizeof (XTransform)) == 0)
return TRUE;
/* a late check in case we perturb the matrix too far */
if (! CAIRO_RENDER_HAS_PICTURE_TRANSFORM (display))
return FALSE;
XRenderSetPictureTransform (display->display, picture, &xtransform);
return TRUE;
}
static cairo_status_t
picture_set_filter (Display *dpy,
Picture picture,
cairo_filter_t filter)
{
const char *render_filter;
switch (filter) {
case CAIRO_FILTER_FAST:
render_filter = FilterFast;
break;
case CAIRO_FILTER_GOOD:
render_filter = FilterGood;
break;
case CAIRO_FILTER_BEST:
render_filter = FilterBest;
break;
case CAIRO_FILTER_NEAREST:
render_filter = FilterNearest;
break;
case CAIRO_FILTER_BILINEAR:
render_filter = FilterBilinear;
break;
case CAIRO_FILTER_GAUSSIAN:
/* XXX: The GAUSSIAN value has no implementation in cairo
* whatsoever, so it was really a mistake to have it in the
* API. We could fix this by officially deprecating it, or
* else inventing semantics and providing an actual
* implementation for it. */
default:
render_filter = FilterBest;
break;
}
XRenderSetPictureFilter (dpy, picture, (char *) render_filter, NULL, 0);
return CAIRO_STATUS_SUCCESS;
}
static int
extend_to_repeat (cairo_extend_t extend)
{
switch (extend) {
default:
ASSERT_NOT_REACHED;
case CAIRO_EXTEND_NONE:
return RepeatNone;
case CAIRO_EXTEND_REPEAT:
return RepeatNormal;
case CAIRO_EXTEND_REFLECT:
return RepeatReflect;
case CAIRO_EXTEND_PAD:
return RepeatPad;
}
}
static cairo_bool_t
picture_set_properties (cairo_xlib_display_t *display,
Picture picture,
const cairo_pattern_t *pattern,
const cairo_matrix_t *matrix,
const cairo_rectangle_int_t *extents,
int *x_off, int *y_off)
{
XRenderPictureAttributes pa;
int mask = 0;
if (! picture_set_matrix (display, picture, matrix, pattern->filter,
extents->x + extents->width / 2,
extents->y + extents->height / 2,
x_off, y_off))
return FALSE;
picture_set_filter (display->display, picture, pattern->filter);
if (pattern->has_component_alpha) {
pa.component_alpha = 1;
mask |= CPComponentAlpha;
}
if (pattern->extend != CAIRO_EXTEND_NONE) {
pa.repeat = extend_to_repeat (pattern->extend);
mask |= CPRepeat;
}
if (mask)
XRenderChangePicture (display->display, picture, mask, &pa);
return TRUE;
}
static cairo_surface_t *
render_pattern (cairo_xlib_surface_t *dst,
const cairo_pattern_t *pattern,
cairo_bool_t is_mask,
const cairo_rectangle_int_t *extents,
int *src_x, int *src_y)
{
Display *dpy = dst->display->display;
cairo_xlib_surface_t *src;
cairo_image_surface_t *image;
cairo_status_t status;
cairo_rectangle_int_t map_extents;
src = (cairo_xlib_surface_t *)
_cairo_surface_create_scratch (&dst->base,
is_mask ? CAIRO_CONTENT_ALPHA : CAIRO_CONTENT_COLOR_ALPHA,
extents->width,
extents->height,
NULL);
if (src->base.type != CAIRO_SURFACE_TYPE_XLIB) {
cairo_surface_destroy (&src->base);
return _cairo_surface_create_in_error (_cairo_error (CAIRO_STATUS_NO_MEMORY));
}
map_extents = *extents;
map_extents.x = map_extents.y = 0;
image = _cairo_surface_map_to_image (&src->base, &map_extents);
status = _cairo_surface_offset_paint (&image->base, extents->x, extents->y,
CAIRO_OPERATOR_SOURCE, pattern,
NULL);
status = _cairo_surface_unmap_image (&src->base, image);
if (unlikely (status)) {
cairo_surface_destroy (&src->base);
return _cairo_surface_create_in_error (status);
}
status = _cairo_xlib_surface_put_shm (src);
if (unlikely (status)) {
cairo_surface_destroy (&src->base);
return _cairo_surface_create_in_error (status);
}
src->picture = XRenderCreatePicture (dpy,
src->drawable, src->xrender_format,
0, NULL);
*src_x = -extents->x;
*src_y = -extents->y;
return &src->base;
}
static cairo_surface_t *
gradient_source (cairo_xlib_surface_t *dst,
const cairo_gradient_pattern_t *gradient,
cairo_bool_t is_mask,
const cairo_rectangle_int_t *extents,
int *src_x, int *src_y)
{
cairo_xlib_display_t *display = dst->display;
cairo_matrix_t matrix = gradient->base.matrix;
char buf[CAIRO_STACK_BUFFER_SIZE];
cairo_circle_double_t extremes[2];
XFixed *stops;
XRenderColor *colors;
Picture picture;
unsigned int i, n_stops;
/* The RENDER specification says that the inner circle has
* to be completely contained inside the outer one. */
if (gradient->base.type == CAIRO_PATTERN_TYPE_RADIAL &&
! _cairo_radial_pattern_focus_is_inside ((cairo_radial_pattern_t *) gradient))
return render_pattern (dst, &gradient->base, is_mask, extents, src_x, src_y);
assert (gradient->n_stops > 0);
n_stops = MAX (gradient->n_stops, 2);
if (n_stops < sizeof (buf) / (sizeof (XFixed) + sizeof (XRenderColor)))
{
stops = (XFixed *) buf;
}
else
{
stops =
_cairo_malloc_ab (n_stops,
sizeof (XFixed) + sizeof (XRenderColor));
if (unlikely (stops == NULL))
return _cairo_surface_create_in_error (_cairo_error (CAIRO_STATUS_NO_MEMORY));
}
colors = (XRenderColor *) (stops + n_stops);
for (i = 0; i < gradient->n_stops; i++) {
stops[i] =
_cairo_fixed_16_16_from_double (gradient->stops[i].offset);
colors[i].red = gradient->stops[i].color.red_short;
colors[i].green = gradient->stops[i].color.green_short;
colors[i].blue = gradient->stops[i].color.blue_short;
colors[i].alpha = gradient->stops[i].color.alpha_short;
}
/* RENDER does not support gradients with less than 2
* stops. If a gradient has only a single stop, duplicate
* it to make RENDER happy. */
if (gradient->n_stops == 1) {
stops[1] =
_cairo_fixed_16_16_from_double (gradient->stops[0].offset);
colors[1].red = gradient->stops[0].color.red_short;
colors[1].green = gradient->stops[0].color.green_short;
colors[1].blue = gradient->stops[0].color.blue_short;
colors[1].alpha = gradient->stops[0].color.alpha_short;
}
#if 0
/* For some weird reason the X server is sometimes getting
* CreateGradient requests with bad length. So far I've only seen
* XRenderCreateLinearGradient request with 4 stops sometime end up
* with length field matching 0 stops at the server side. I've
* looked at the libXrender code and I can't see anything that
* could cause this behavior. However, for some reason having a
* XSync call here seems to avoid the issue so I'll keep it here
* until it's solved.
*/
XSync (display->display, False);
#endif
_cairo_gradient_pattern_fit_to_range (gradient, PIXMAN_MAX_INT >> 1, &matrix, extremes);
if (gradient->base.type == CAIRO_PATTERN_TYPE_LINEAR) {
XLinearGradient grad;
grad.p1.x = _cairo_fixed_16_16_from_double (extremes[0].center.x);
grad.p1.y = _cairo_fixed_16_16_from_double (extremes[0].center.y);
grad.p2.x = _cairo_fixed_16_16_from_double (extremes[1].center.x);
grad.p2.y = _cairo_fixed_16_16_from_double (extremes[1].center.y);
picture = XRenderCreateLinearGradient (display->display, &grad,
stops, colors,
n_stops);
} else {
XRadialGradient grad;
grad.inner.x = _cairo_fixed_16_16_from_double (extremes[0].center.x);
grad.inner.y = _cairo_fixed_16_16_from_double (extremes[0].center.y);
grad.inner.radius = _cairo_fixed_16_16_from_double (extremes[0].radius);
grad.outer.x = _cairo_fixed_16_16_from_double (extremes[1].center.x);
grad.outer.y = _cairo_fixed_16_16_from_double (extremes[1].center.y);
grad.outer.radius = _cairo_fixed_16_16_from_double (extremes[1].radius);
picture = XRenderCreateRadialGradient (display->display, &grad,
stops, colors,
n_stops);
}
if (stops != (XFixed *) buf)
free (stops);
*src_x = *src_y = 0;
if (! picture_set_properties (display, picture,
&gradient->base, &gradient->base.matrix,
extents,
src_x, src_y)) {
XRenderFreePicture (display->display, picture);
return render_pattern (dst, &gradient->base, is_mask, extents, src_x, src_y);
}
return source (dst, picture, None);
}
static cairo_surface_t *
color_source (cairo_xlib_surface_t *dst, const cairo_color_t *color)
{
Display *dpy = dst->display->display;
XRenderColor xcolor;
Picture picture;
Pixmap pixmap = None;
xcolor.red = color->red_short;
xcolor.green = color->green_short;
xcolor.blue = color->blue_short;
xcolor.alpha = color->alpha_short;
if (CAIRO_RENDER_HAS_GRADIENTS(dst->display)) {
picture = XRenderCreateSolidFill (dpy, &xcolor);
} else {
XRenderPictureAttributes pa;
int mask = 0;
pa.repeat = RepeatNormal;
mask |= CPRepeat;
pixmap = XCreatePixmap (dpy, dst->drawable, 1, 1, 32);
picture = XRenderCreatePicture (dpy, pixmap,
_cairo_xlib_display_get_xrender_format (dst->display, CAIRO_FORMAT_ARGB32),
mask, &pa);
if (CAIRO_RENDER_HAS_FILL_RECTANGLES(dst->display)) {
XRectangle r = { 0, 0, 1, 1};
XRenderFillRectangles (dpy, PictOpSrc, picture, &xcolor, &r, 1);
} else {
XGCValues gcv;
GC gc;
gc = _cairo_xlib_screen_get_gc (dst->display, dst->screen,
32, pixmap);
if (unlikely (gc == NULL)) {
XFreePixmap (dpy, pixmap);
return _cairo_surface_create_in_error (_cairo_error (CAIRO_STATUS_NO_MEMORY));
}
gcv.foreground = 0;
gcv.foreground |= (uint32_t)color->alpha_short >> 8 << 24;
gcv.foreground |= color->red_short >> 8 << 16;
gcv.foreground |= color->green_short >> 8 << 8;
gcv.foreground |= color->blue_short >> 8 << 0;
gcv.fill_style = FillSolid;
XChangeGC (dpy, gc, GCFillStyle | GCForeground, &gcv);
XFillRectangle (dpy, pixmap, gc, 0, 0, 1, 1);
_cairo_xlib_screen_put_gc (dst->display, dst->screen, 32, gc);
}
}
return source (dst, picture, pixmap);
}
static cairo_surface_t *
alpha_source (cairo_xlib_surface_t *dst, uint8_t alpha)
{
cairo_xlib_display_t *display = dst->display;
if (display->alpha[alpha] == NULL) {
cairo_color_t color;
color.red_short = color.green_short = color.blue_short = 0;
color.alpha_short = alpha << 8 | alpha;
display->alpha[alpha] = color_source (dst, &color);
}
return cairo_surface_reference (display->alpha[alpha]);
}
static cairo_surface_t *
white_source (cairo_xlib_surface_t *dst)
{
cairo_xlib_display_t *display = dst->display;
if (display->white == NULL)
display->white = color_source (dst, CAIRO_COLOR_WHITE);
return cairo_surface_reference (display->white);
}
static cairo_surface_t *
opaque_source (cairo_xlib_surface_t *dst, const cairo_color_t *color)
{
cairo_xlib_display_t *display = dst->display;
uint32_t pixel =
0xff000000 |
color->red_short >> 8 << 16 |
color->green_short >> 8 << 8 |
color->blue_short >> 8 << 0;
int i;
if (display->last_solid_cache[0].color == pixel)
return cairo_surface_reference (display->solid[display->last_solid_cache[0].index]);
for (i = 0; i < 16; i++) {
if (display->solid_cache[i] == pixel)
goto done;
}
i = hars_petruska_f54_1_random () % 16;
cairo_surface_destroy (display->solid[i]);
display->solid[i] = color_source (dst, color);
display->solid_cache[i] = pixel;
done:
display->last_solid_cache[0].color = pixel;
display->last_solid_cache[0].index = i;
return cairo_surface_reference (display->solid[i]);
}
static cairo_surface_t *
transparent_source (cairo_xlib_surface_t *dst, const cairo_color_t *color)
{
cairo_xlib_display_t *display = dst->display;
uint32_t pixel =
(uint32_t)color->alpha_short >> 8 << 24 |
color->red_short >> 8 << 16 |
color->green_short >> 8 << 8 |
color->blue_short >> 8 << 0;
int i;
if (display->last_solid_cache[1].color == pixel) {
assert (display->solid[display->last_solid_cache[1].index]);
return cairo_surface_reference (display->solid[display->last_solid_cache[1].index]);
}
for (i = 16; i < 32; i++) {
if (display->solid_cache[i] == pixel)
goto done;
}
i = 16 + (hars_petruska_f54_1_random () % 16);
cairo_surface_destroy (display->solid[i]);
display->solid[i] = color_source (dst, color);
display->solid_cache[i] = pixel;
done:
display->last_solid_cache[1].color = pixel;
display->last_solid_cache[1].index = i;
assert (display->solid[i]);
return cairo_surface_reference (display->solid[i]);
}
static cairo_surface_t *
solid_source (cairo_xlib_surface_t *dst,
const cairo_color_t *color)
{
if ((color->red_short | color->green_short | color->blue_short) <= 0xff)
return alpha_source (dst, color->alpha_short >> 8);
if (CAIRO_ALPHA_SHORT_IS_OPAQUE (color->alpha_short)) {
if (color->red_short >= 0xff00 && color->green_short >= 0xff00 && color->blue_short >= 0xff00)
return white_source (dst);
return opaque_source (dst, color);
} else
return transparent_source (dst, color);
}
static cairo_xlib_source_t *init_source (cairo_xlib_surface_t *dst,
cairo_xlib_surface_t *src)
{
Display *dpy = dst->display->display;
cairo_xlib_source_t *source = &src->embedded_source;
/* As these are frequent and meant to be fast, we track pictures for
* native surface and minimise update requests.
*/
if (source->picture == None) {
XRenderPictureAttributes pa;
_cairo_surface_init (&source->base,
&cairo_xlib_source_backend,
NULL, /* device */
CAIRO_CONTENT_COLOR_ALPHA,
FALSE); /* is_vector */
pa.subwindow_mode = IncludeInferiors;
source->picture = XRenderCreatePicture (dpy,
src->drawable,
src->xrender_format,
CPSubwindowMode, &pa);
source->has_component_alpha = 0;
source->has_matrix = 0;
source->filter = CAIRO_FILTER_NEAREST;
source->extend = CAIRO_EXTEND_NONE;
}
return (cairo_xlib_source_t *) cairo_surface_reference (&source->base);
}
static cairo_surface_t *
embedded_source (cairo_xlib_surface_t *dst,
const cairo_surface_pattern_t *pattern,
const cairo_rectangle_int_t *extents,
int *src_x, int *src_y,
cairo_xlib_source_t *source)
{
Display *dpy = dst->display->display;
cairo_int_status_t status;
XTransform xtransform;
XRenderPictureAttributes pa;
unsigned mask = 0;
status = _cairo_matrix_to_pixman_matrix_offset (&pattern->base.matrix,
pattern->base.filter,
extents->x + extents->width / 2,
extents->y + extents->height / 2,
(pixman_transform_t *)&xtransform,
src_x, src_y);
if (status == CAIRO_INT_STATUS_NOTHING_TO_DO) {
if (source->has_matrix) {
source->has_matrix = 0;
memcpy (&xtransform, &identity, sizeof (identity));
status = CAIRO_INT_STATUS_SUCCESS;
}
} else
source->has_matrix = 1;
if (status == CAIRO_INT_STATUS_SUCCESS)
XRenderSetPictureTransform (dpy, source->picture, &xtransform);
if (source->filter != pattern->base.filter) {
picture_set_filter (dpy, source->picture, pattern->base.filter);
source->filter = pattern->base.filter;
}
if (source->has_component_alpha != pattern->base.has_component_alpha) {
pa.component_alpha = pattern->base.has_component_alpha;
mask |= CPComponentAlpha;
source->has_component_alpha = pattern->base.has_component_alpha;
}
if (source->extend != pattern->base.extend) {
pa.repeat = extend_to_repeat (pattern->base.extend);
mask |= CPRepeat;
source->extend = pattern->base.extend;
}
if (mask)
XRenderChangePicture (dpy, source->picture, mask, &pa);
return &source->base;
}
static cairo_surface_t *
subsurface_source (cairo_xlib_surface_t *dst,
const cairo_surface_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_surface_subsurface_t *sub;
cairo_xlib_surface_t *src;
cairo_xlib_source_t *source;
Display *dpy = dst->display->display;
cairo_int_status_t status;
cairo_surface_pattern_t local_pattern;
XTransform xtransform;
XRenderPictureAttributes pa;
unsigned mask = 0;
sub = (cairo_surface_subsurface_t *) pattern->surface;
if (sample->x >= 0 && sample->y >= 0 &&
sample->x + sample->width <= sub->extents.width &&
sample->y + sample->height <= sub->extents.height)
{
src = (cairo_xlib_surface_t *) sub->target;
status = _cairo_surface_flush (&src->base, 0);
if (unlikely (status))
return _cairo_surface_create_in_error (status);
if (pattern->base.filter == CAIRO_FILTER_NEAREST &&
_cairo_matrix_is_translation (&pattern->base.matrix))
{
*src_x += pattern->base.matrix.x0 + sub->extents.x;
*src_y += pattern->base.matrix.y0 + sub->extents.y;
_cairo_xlib_surface_ensure_picture (src);
return cairo_surface_reference (&src->base);
}
else
{
cairo_surface_pattern_t local_pattern = *pattern;
local_pattern.base.matrix.x0 += sub->extents.x;
local_pattern.base.matrix.y0 += sub->extents.y;
local_pattern.base.extend = CAIRO_EXTEND_NONE;
return embedded_source (dst, &local_pattern, extents,
src_x, src_y, init_source (dst, src));
}
}
if (sub->snapshot && sub->snapshot->type == CAIRO_SURFACE_TYPE_XLIB) {
src = (cairo_xlib_surface_t *) cairo_surface_reference (sub->snapshot);
source = &src->embedded_source;
} else {
src = (cairo_xlib_surface_t *)
_cairo_surface_create_scratch (&dst->base,
sub->base.content,
sub->extents.width,
sub->extents.height,
NULL);
if (src->base.type != CAIRO_SURFACE_TYPE_XLIB) {
cairo_surface_destroy (&src->base);
return _cairo_surface_create_in_error (CAIRO_STATUS_NO_MEMORY);
}
_cairo_pattern_init_for_surface (&local_pattern, sub->target);
cairo_matrix_init_translate (&local_pattern.base.matrix,
sub->extents.x, sub->extents.y);
local_pattern.base.filter = CAIRO_FILTER_NEAREST;
status = _cairo_surface_paint (&src->base,
CAIRO_OPERATOR_SOURCE,
&local_pattern.base,
NULL);
_cairo_pattern_fini (&local_pattern.base);
if (unlikely (status)) {
cairo_surface_destroy (&src->base);
return _cairo_surface_create_in_error (status);
}
_cairo_xlib_surface_ensure_picture (src);
_cairo_surface_subsurface_set_snapshot (&sub->base, &src->base);
source = &src->embedded_source;
source->has_component_alpha = 0;
source->has_matrix = 0;
source->filter = CAIRO_FILTER_NEAREST;
source->extend = CAIRO_EXTEND_NONE;
}
status = _cairo_matrix_to_pixman_matrix_offset (&pattern->base.matrix,
pattern->base.filter,
extents->x + extents->width / 2,
extents->y + extents->height / 2,
(pixman_transform_t *)&xtransform,
src_x, src_y);
if (status == CAIRO_INT_STATUS_NOTHING_TO_DO) {
if (source->has_matrix) {
source->has_matrix = 0;
memcpy (&xtransform, &identity, sizeof (identity));
status = CAIRO_INT_STATUS_SUCCESS;
}
} else
source->has_matrix = 1;
if (status == CAIRO_INT_STATUS_SUCCESS)
XRenderSetPictureTransform (dpy, src->picture, &xtransform);
if (source->filter != pattern->base.filter) {
picture_set_filter (dpy, src->picture, pattern->base.filter);
source->filter = pattern->base.filter;
}
if (source->has_component_alpha != pattern->base.has_component_alpha) {
pa.component_alpha = pattern->base.has_component_alpha;
mask |= CPComponentAlpha;
source->has_component_alpha = pattern->base.has_component_alpha;
}
if (source->extend != pattern->base.extend) {
pa.repeat = extend_to_repeat (pattern->base.extend);
mask |= CPRepeat;
source->extend = pattern->base.extend;
}
if (mask)
XRenderChangePicture (dpy, src->picture, mask, &pa);
return &src->base;
}
static cairo_surface_t *
native_source (cairo_xlib_surface_t *dst,
const cairo_surface_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_xlib_surface_t *src;
cairo_int_status_t status;
if (_cairo_surface_is_subsurface (pattern->surface))
return subsurface_source (dst, pattern, is_mask,
extents, sample,
src_x, src_y);
src = unwrap_source (pattern);
status = _cairo_surface_flush (&src->base, 0);
if (unlikely (status))
return _cairo_surface_create_in_error (status);
if (pattern->base.filter == CAIRO_FILTER_NEAREST &&
sample->x >= 0 && sample->y >= 0 &&
sample->x + sample->width <= src->width &&
sample->y + sample->height <= src->height &&
_cairo_matrix_is_translation (&pattern->base.matrix))
{
*src_x += pattern->base.matrix.x0;
*src_y += pattern->base.matrix.y0;
_cairo_xlib_surface_ensure_picture (src);
return cairo_surface_reference (&src->base);
}
return embedded_source (dst, pattern, extents, src_x, src_y,
init_source (dst, src));
}
static cairo_surface_t *
recording_pattern_get_surface (const cairo_pattern_t *pattern)
{
cairo_surface_t *surface;
surface = ((const cairo_surface_pattern_t *) pattern)->surface;
if (_cairo_surface_is_paginated (surface))
return cairo_surface_reference (_cairo_paginated_surface_get_recording (surface));
if (_cairo_surface_is_snapshot (surface))
return _cairo_surface_snapshot_get_target (surface);
return cairo_surface_reference (surface);
}
static cairo_surface_t *
record_source (cairo_xlib_surface_t *dst,
const cairo_surface_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_xlib_surface_t *src;
cairo_surface_t *recording;
cairo_matrix_t matrix, m;
cairo_status_t status;
cairo_rectangle_int_t upload, limit;
upload = *sample;
if (_cairo_surface_get_extents (pattern->surface, &limit) &&
! _cairo_rectangle_intersect (&upload, &limit))
{
if (pattern->base.extend == CAIRO_EXTEND_NONE)
return alpha_source (dst, 0);
upload = limit;
}
src = (cairo_xlib_surface_t *)
_cairo_surface_create_scratch (&dst->base,
pattern->surface->content,
upload.width,
upload.height,
NULL);
if (src->base.type != CAIRO_SURFACE_TYPE_XLIB) {
cairo_surface_destroy (&src->base);
return _cairo_surface_create_in_error (CAIRO_STATUS_NO_MEMORY);
}
cairo_matrix_init_translate (&matrix, upload.x, upload.y);
recording = recording_pattern_get_surface (&pattern->base),
status = _cairo_recording_surface_replay_with_clip (recording,
&matrix, &src->base,
NULL);
cairo_surface_destroy (recording);
if (unlikely (status)) {
cairo_surface_destroy (&src->base);
return _cairo_surface_create_in_error (status);
}
matrix = pattern->base.matrix;
if (upload.x | upload.y) {
cairo_matrix_init_translate (&m, -upload.x, -upload.y);
cairo_matrix_multiply (&matrix, &matrix, &m);
}
_cairo_xlib_surface_ensure_picture (src);
if (! picture_set_properties (src->display, src->picture,
&pattern->base, &matrix, extents,
src_x, src_y))
{
cairo_surface_destroy (&src->base);
return render_pattern (dst, &pattern->base, is_mask,
extents, src_x, src_y);
}
return &src->base;
}
static cairo_surface_t *
surface_source (cairo_xlib_surface_t *dst,
const cairo_surface_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_surface_t *src;
cairo_xlib_surface_t *xsrc;
cairo_surface_pattern_t local_pattern;
cairo_status_t status;
cairo_rectangle_int_t upload, limit;
src = pattern->surface;
if (src->type == CAIRO_SURFACE_TYPE_IMAGE &&
src->device == dst->base.device &&
_cairo_xlib_shm_surface_get_pixmap (src)) {
cairo_xlib_proxy_t *proxy;
proxy = _cairo_malloc (sizeof(*proxy));
if (unlikely (proxy == NULL))
return _cairo_surface_create_in_error (CAIRO_STATUS_NO_MEMORY);
_cairo_surface_init (&proxy->source.base,
&cairo_xlib_proxy_backend,
dst->base.device,
src->content,
src->is_vector);
proxy->source.dpy = dst->display->display;
proxy->source.picture = XRenderCreatePicture (proxy->source.dpy,
_cairo_xlib_shm_surface_get_pixmap (src),
_cairo_xlib_shm_surface_get_xrender_format (src),
0, NULL);
proxy->source.pixmap = None;
proxy->source.has_component_alpha = 0;
proxy->source.has_matrix = 0;
proxy->source.filter = CAIRO_FILTER_NEAREST;
proxy->source.extend = CAIRO_EXTEND_NONE;
proxy->owner = cairo_surface_reference (src);
return embedded_source (dst, pattern, extents, src_x, src_y,
&proxy->source);
}
upload = *sample;
if (_cairo_surface_get_extents (pattern->surface, &limit)) {
if (pattern->base.extend == CAIRO_EXTEND_NONE) {
if (! _cairo_rectangle_intersect (&upload, &limit))
return alpha_source (dst, 0);
} else if (pattern->base.extend == CAIRO_EXTEND_PAD) {
if (! _cairo_rectangle_intersect (&upload, &limit))
upload = limit;
} else {
if (upload.x < limit.x ||
upload.x + upload.width > limit.x + limit.width ||
upload.y < limit.y ||
upload.y + upload.height > limit.y + limit.height)
{
upload = limit;
}
}
}
xsrc = (cairo_xlib_surface_t *)
_cairo_surface_create_scratch (&dst->base,
src->content,
upload.width,
upload.height,
NULL);
if (xsrc->base.type != CAIRO_SURFACE_TYPE_XLIB) {
cairo_surface_destroy (src);
cairo_surface_destroy (&xsrc->base);
return _cairo_surface_create_in_error (_cairo_error (CAIRO_STATUS_NO_MEMORY));
}
if (_cairo_surface_is_image (src)) {
status = _cairo_xlib_surface_draw_image (xsrc, (cairo_image_surface_t *)src,
upload.x, upload.y,
upload.width, upload.height,
0, 0);
} else {
cairo_image_surface_t *image;
cairo_rectangle_int_t map_extents = { 0,0, upload.width,upload.height };
image = _cairo_surface_map_to_image (&xsrc->base, &map_extents);
_cairo_pattern_init_for_surface (&local_pattern, pattern->surface);
cairo_matrix_init_translate (&local_pattern.base.matrix,
upload.x, upload.y);
status = _cairo_surface_paint (&image->base,
CAIRO_OPERATOR_SOURCE,
&local_pattern.base,
NULL);
_cairo_pattern_fini (&local_pattern.base);
status = _cairo_surface_unmap_image (&xsrc->base, image);
if (unlikely (status)) {
cairo_surface_destroy (&xsrc->base);
return _cairo_surface_create_in_error (status);
}
status = _cairo_xlib_surface_put_shm (xsrc);
if (unlikely (status)) {
cairo_surface_destroy (&xsrc->base);
return _cairo_surface_create_in_error (status);
}
}
_cairo_pattern_init_static_copy (&local_pattern.base, &pattern->base);
if (upload.x | upload.y) {
cairo_matrix_t m;
cairo_matrix_init_translate (&m, -upload.x, -upload.y);
cairo_matrix_multiply (&local_pattern.base.matrix,
&local_pattern.base.matrix,
&m);
}
*src_x = *src_y = 0;
_cairo_xlib_surface_ensure_picture (xsrc);
if (! picture_set_properties (xsrc->display,
xsrc->picture,
&local_pattern.base,
&local_pattern.base.matrix,
extents,
src_x, src_y))
{
cairo_surface_destroy (&xsrc->base);
return render_pattern (dst, &pattern->base,
is_mask, extents,
src_x, src_y);
}
return &xsrc->base;
}
static cairo_bool_t
pattern_is_supported (cairo_xlib_display_t *display,
const cairo_pattern_t *pattern)
{
if (pattern->type == CAIRO_PATTERN_TYPE_MESH)
return FALSE;
if (display->buggy_pad_reflect) {
if (pattern->extend == CAIRO_EXTEND_REPEAT || pattern->extend == CAIRO_EXTEND_PAD)
return FALSE;
}
if (display->buggy_gradients) {
if (pattern->type == CAIRO_PATTERN_TYPE_LINEAR || pattern->type == CAIRO_PATTERN_TYPE_RADIAL)
return FALSE;
}
switch (pattern->filter) {
case CAIRO_FILTER_FAST:
case CAIRO_FILTER_NEAREST:
return CAIRO_RENDER_HAS_PICTURE_TRANSFORM (display) ||
_cairo_matrix_is_integer_translation (&pattern->matrix, NULL, NULL);
case CAIRO_FILTER_GOOD:
return CAIRO_RENDER_HAS_FILTER_GOOD (display);
case CAIRO_FILTER_BEST:
return CAIRO_RENDER_HAS_FILTER_BEST (display);
case CAIRO_FILTER_BILINEAR:
case CAIRO_FILTER_GAUSSIAN:
default:
return CAIRO_RENDER_HAS_FILTERS (display);
}
}
cairo_surface_t *
_cairo_xlib_source_create_for_pattern (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_xlib_surface_t *dst = (cairo_xlib_surface_t *)_dst;
*src_x = *src_y = 0;
if (pattern == NULL || pattern->type == CAIRO_PATTERN_TYPE_SOLID) {
if (pattern == NULL)
pattern = &_cairo_pattern_white.base;
return solid_source (dst, &((cairo_solid_pattern_t *)pattern)->color);
}
if (pattern_is_supported (dst->display, pattern)) {
if (pattern->type == CAIRO_PATTERN_TYPE_SURFACE) {
cairo_surface_pattern_t *spattern = (cairo_surface_pattern_t *)pattern;
if (spattern->surface->type == CAIRO_SURFACE_TYPE_XLIB &&
_cairo_xlib_surface_same_screen (dst,
unwrap_source (spattern)))
return native_source (dst, spattern, is_mask,
extents, sample,
src_x, src_y);
if (spattern->surface->type == CAIRO_SURFACE_TYPE_RECORDING)
return record_source (dst, spattern, is_mask,
extents, sample,
src_x, src_y);
return surface_source (dst, spattern, is_mask,
extents, sample,
src_x, src_y);
}
if (pattern->type == CAIRO_PATTERN_TYPE_LINEAR ||
pattern->type == CAIRO_PATTERN_TYPE_RADIAL)
{
cairo_gradient_pattern_t *gpattern = (cairo_gradient_pattern_t *)pattern;
return gradient_source (dst, gpattern, is_mask, extents, src_x, src_y);
}
}
return render_pattern (dst, pattern, is_mask, extents, src_x, src_y);
}
#endif /* !CAIRO_HAS_XLIB_XCB_FUNCTIONS */
|