-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcairo-image-compositor.c
2896 lines (2546 loc) · 79.6 KB
/
cairo-image-compositor.c
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
/* -*- Mode: c; tab-width: 8; c-basic-offset: 4; indent-tabs-mode: t; -*- */
/* cairo - a vector graphics library with display and print output
*
* Copyright © 2003 University of Southern California
* Copyright © 2009,2010,2011 Intel Corporation
*
* This library is free software; you can redistribute it and/or
* modify it either under the terms of the GNU Lesser General Public
* License version 2.1 as published by the Free Software Foundation
* (the "LGPL") or, at your option, under the terms of the Mozilla
* Public License Version 1.1 (the "MPL"). If you do not alter this
* notice, a recipient may use your version of this file under either
* the MPL or the LGPL.
*
* You should have received a copy of the LGPL along with this library
* in the file COPYING-LGPL-2.1; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA
* You should have received a copy of the MPL along with this library
* in the file COPYING-MPL-1.1
*
* The contents of this file are subject to the Mozilla Public License
* Version 1.1 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* http://www.mozilla.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 <[email protected]>
* Chris Wilson <[email protected]>
*/
/* The primarily reason for keeping a traps-compositor around is
* for validating cairo-xlib (which currently also uses traps).
*/
#include "cairoint.h"
#include "cairo-image-surface-private.h"
#include "cairo-compositor-private.h"
#include "cairo-spans-compositor-private.h"
#include "cairo-region-private.h"
#include "cairo-traps-private.h"
#include "cairo-tristrip-private.h"
static pixman_image_t *
to_pixman_image (cairo_surface_t *s)
{
return ((cairo_image_surface_t *)s)->pixman_image;
}
static cairo_int_status_t
acquire (void *abstract_dst)
{
return CAIRO_STATUS_SUCCESS;
}
static cairo_int_status_t
release (void *abstract_dst)
{
return CAIRO_STATUS_SUCCESS;
}
static cairo_int_status_t
set_clip_region (void *_surface,
cairo_region_t *region)
{
cairo_image_surface_t *surface = _surface;
pixman_region32_t *rgn = region ? ®ion->rgn : NULL;
if (! pixman_image_set_clip_region32 (surface->pixman_image, rgn))
return _cairo_error (CAIRO_STATUS_NO_MEMORY);
return CAIRO_STATUS_SUCCESS;
}
static cairo_int_status_t
draw_image_boxes (void *_dst,
cairo_image_surface_t *image,
cairo_boxes_t *boxes,
int dx, int dy)
{
cairo_image_surface_t *dst = _dst;
struct _cairo_boxes_chunk *chunk;
int i;
TRACE ((stderr, "%s x %d\n", __FUNCTION__, boxes->num_boxes));
for (chunk = &boxes->chunks; chunk; chunk = chunk->next) {
for (i = 0; i < chunk->count; i++) {
cairo_box_t *b = &chunk->base[i];
int x = _cairo_fixed_integer_part (b->p1.x);
int y = _cairo_fixed_integer_part (b->p1.y);
int w = _cairo_fixed_integer_part (b->p2.x) - x;
int h = _cairo_fixed_integer_part (b->p2.y) - y;
if (dst->pixman_format != image->pixman_format ||
! pixman_blt ((uint32_t *)image->data, (uint32_t *)dst->data,
image->stride / sizeof (uint32_t),
dst->stride / sizeof (uint32_t),
PIXMAN_FORMAT_BPP (image->pixman_format),
PIXMAN_FORMAT_BPP (dst->pixman_format),
x + dx, y + dy,
x, y,
w, h))
{
pixman_image_composite32 (PIXMAN_OP_SRC,
image->pixman_image, NULL, dst->pixman_image,
x + dx, y + dy,
0, 0,
x, y,
w, h);
}
}
}
return CAIRO_STATUS_SUCCESS;
}
static inline uint32_t
color_to_uint32 (const cairo_color_t *color)
{
return
(color->alpha_short >> 8 << 24) |
(color->red_short >> 8 << 16) |
(color->green_short & 0xff00) |
(color->blue_short >> 8);
}
static inline cairo_bool_t
color_to_pixel (const cairo_color_t *color,
pixman_format_code_t format,
uint32_t *pixel)
{
uint32_t c;
if (!(format == PIXMAN_a8r8g8b8 ||
format == PIXMAN_x8r8g8b8 ||
format == PIXMAN_a8b8g8r8 ||
format == PIXMAN_x8b8g8r8 ||
format == PIXMAN_b8g8r8a8 ||
format == PIXMAN_b8g8r8x8 ||
format == PIXMAN_r5g6b5 ||
format == PIXMAN_b5g6r5 ||
format == PIXMAN_a8))
{
return FALSE;
}
c = color_to_uint32 (color);
if (PIXMAN_FORMAT_TYPE (format) == PIXMAN_TYPE_ABGR) {
c = ((c & 0xff000000) >> 0) |
((c & 0x00ff0000) >> 16) |
((c & 0x0000ff00) >> 0) |
((c & 0x000000ff) << 16);
}
if (PIXMAN_FORMAT_TYPE (format) == PIXMAN_TYPE_BGRA) {
c = ((c & 0xff000000) >> 24) |
((c & 0x00ff0000) >> 8) |
((c & 0x0000ff00) << 8) |
((c & 0x000000ff) << 24);
}
if (format == PIXMAN_a8) {
c = c >> 24;
} else if (format == PIXMAN_r5g6b5 || format == PIXMAN_b5g6r5) {
c = ((((c) >> 3) & 0x001f) |
(((c) >> 5) & 0x07e0) |
(((c) >> 8) & 0xf800));
}
*pixel = c;
return TRUE;
}
static pixman_op_t
_pixman_operator (cairo_operator_t op)
{
switch ((int) op) {
case CAIRO_OPERATOR_CLEAR:
return PIXMAN_OP_CLEAR;
case CAIRO_OPERATOR_SOURCE:
return PIXMAN_OP_SRC;
case CAIRO_OPERATOR_OVER:
return PIXMAN_OP_OVER;
case CAIRO_OPERATOR_IN:
return PIXMAN_OP_IN;
case CAIRO_OPERATOR_OUT:
return PIXMAN_OP_OUT;
case CAIRO_OPERATOR_ATOP:
return PIXMAN_OP_ATOP;
case CAIRO_OPERATOR_DEST:
return PIXMAN_OP_DST;
case CAIRO_OPERATOR_DEST_OVER:
return PIXMAN_OP_OVER_REVERSE;
case CAIRO_OPERATOR_DEST_IN:
return PIXMAN_OP_IN_REVERSE;
case CAIRO_OPERATOR_DEST_OUT:
return PIXMAN_OP_OUT_REVERSE;
case CAIRO_OPERATOR_DEST_ATOP:
return PIXMAN_OP_ATOP_REVERSE;
case CAIRO_OPERATOR_XOR:
return PIXMAN_OP_XOR;
case CAIRO_OPERATOR_ADD:
return PIXMAN_OP_ADD;
case CAIRO_OPERATOR_SATURATE:
return PIXMAN_OP_SATURATE;
case CAIRO_OPERATOR_MULTIPLY:
return PIXMAN_OP_MULTIPLY;
case CAIRO_OPERATOR_SCREEN:
return PIXMAN_OP_SCREEN;
case CAIRO_OPERATOR_OVERLAY:
return PIXMAN_OP_OVERLAY;
case CAIRO_OPERATOR_DARKEN:
return PIXMAN_OP_DARKEN;
case CAIRO_OPERATOR_LIGHTEN:
return PIXMAN_OP_LIGHTEN;
case CAIRO_OPERATOR_COLOR_DODGE:
return PIXMAN_OP_COLOR_DODGE;
case CAIRO_OPERATOR_COLOR_BURN:
return PIXMAN_OP_COLOR_BURN;
case CAIRO_OPERATOR_HARD_LIGHT:
return PIXMAN_OP_HARD_LIGHT;
case CAIRO_OPERATOR_SOFT_LIGHT:
return PIXMAN_OP_SOFT_LIGHT;
case CAIRO_OPERATOR_DIFFERENCE:
return PIXMAN_OP_DIFFERENCE;
case CAIRO_OPERATOR_EXCLUSION:
return PIXMAN_OP_EXCLUSION;
case CAIRO_OPERATOR_HSL_HUE:
return PIXMAN_OP_HSL_HUE;
case CAIRO_OPERATOR_HSL_SATURATION:
return PIXMAN_OP_HSL_SATURATION;
case CAIRO_OPERATOR_HSL_COLOR:
return PIXMAN_OP_HSL_COLOR;
case CAIRO_OPERATOR_HSL_LUMINOSITY:
return PIXMAN_OP_HSL_LUMINOSITY;
default:
ASSERT_NOT_REACHED;
return PIXMAN_OP_OVER;
}
}
static cairo_bool_t
fill_reduces_to_source (cairo_operator_t op,
const cairo_color_t *color,
cairo_image_surface_t *dst)
{
if (op == CAIRO_OPERATOR_SOURCE || op == CAIRO_OPERATOR_CLEAR)
return TRUE;
if (op == CAIRO_OPERATOR_OVER && CAIRO_COLOR_IS_OPAQUE (color))
return TRUE;
if (dst->base.is_clear)
return op == CAIRO_OPERATOR_OVER || op == CAIRO_OPERATOR_ADD;
return FALSE;
}
static cairo_int_status_t
fill_rectangles (void *_dst,
cairo_operator_t op,
const cairo_color_t *color,
cairo_rectangle_int_t *rects,
int num_rects)
{
cairo_image_surface_t *dst = _dst;
uint32_t pixel;
int i;
TRACE ((stderr, "%s\n", __FUNCTION__));
if (fill_reduces_to_source (op, color, dst) &&
color_to_pixel (color, dst->pixman_format, &pixel))
{
for (i = 0; i < num_rects; i++) {
pixman_fill ((uint32_t *) dst->data, dst->stride / sizeof (uint32_t),
PIXMAN_FORMAT_BPP (dst->pixman_format),
rects[i].x, rects[i].y,
rects[i].width, rects[i].height,
pixel);
}
}
else
{
pixman_image_t *src = _pixman_image_for_color (color);
op = _pixman_operator (op);
for (i = 0; i < num_rects; i++) {
pixman_image_composite32 (op,
src, NULL, dst->pixman_image,
0, 0,
0, 0,
rects[i].x, rects[i].y,
rects[i].width, rects[i].height);
}
pixman_image_unref (src);
}
return CAIRO_STATUS_SUCCESS;
}
static cairo_int_status_t
fill_boxes (void *_dst,
cairo_operator_t op,
const cairo_color_t *color,
cairo_boxes_t *boxes)
{
cairo_image_surface_t *dst = _dst;
struct _cairo_boxes_chunk *chunk;
uint32_t pixel;
int i;
TRACE ((stderr, "%s x %d\n", __FUNCTION__, boxes->num_boxes));
if (fill_reduces_to_source (op, color, dst) &&
color_to_pixel (color, dst->pixman_format, &pixel))
{
for (chunk = &boxes->chunks; chunk; chunk = chunk->next) {
for (i = 0; i < chunk->count; i++) {
int x = _cairo_fixed_integer_part (chunk->base[i].p1.x);
int y = _cairo_fixed_integer_part (chunk->base[i].p1.y);
int w = _cairo_fixed_integer_part (chunk->base[i].p2.x) - x;
int h = _cairo_fixed_integer_part (chunk->base[i].p2.y) - y;
pixman_fill ((uint32_t *) dst->data,
dst->stride / sizeof (uint32_t),
PIXMAN_FORMAT_BPP (dst->pixman_format),
x, y, w, h, pixel);
}
}
}
else
{
pixman_image_t *src = _pixman_image_for_color (color);
op = _pixman_operator (op);
for (chunk = &boxes->chunks; chunk; chunk = chunk->next) {
for (i = 0; i < chunk->count; i++) {
int x1 = _cairo_fixed_integer_part (chunk->base[i].p1.x);
int y1 = _cairo_fixed_integer_part (chunk->base[i].p1.y);
int x2 = _cairo_fixed_integer_part (chunk->base[i].p2.x);
int y2 = _cairo_fixed_integer_part (chunk->base[i].p2.y);
pixman_image_composite32 (op,
src, NULL, dst->pixman_image,
0, 0,
0, 0,
x1, y1,
x2-x1, y2-y1);
}
}
pixman_image_unref (src);
}
return CAIRO_STATUS_SUCCESS;
}
static cairo_int_status_t
composite (void *_dst,
cairo_operator_t op,
cairo_surface_t *abstract_src,
cairo_surface_t *abstract_mask,
int src_x,
int src_y,
int mask_x,
int mask_y,
int dst_x,
int dst_y,
unsigned int width,
unsigned int height)
{
cairo_image_source_t *src = (cairo_image_source_t *)abstract_src;
cairo_image_source_t *mask = (cairo_image_source_t *)abstract_mask;
TRACE ((stderr, "%s\n", __FUNCTION__));
if (mask) {
pixman_image_composite32 (_pixman_operator (op),
src->pixman_image, mask->pixman_image, to_pixman_image (_dst),
src_x, src_y,
mask_x, mask_y,
dst_x, dst_y,
width, height);
} else {
pixman_image_composite32 (_pixman_operator (op),
src->pixman_image, NULL, to_pixman_image (_dst),
src_x, src_y,
0, 0,
dst_x, dst_y,
width, height);
}
return CAIRO_STATUS_SUCCESS;
}
static cairo_int_status_t
lerp (void *_dst,
cairo_surface_t *abstract_src,
cairo_surface_t *abstract_mask,
int src_x,
int src_y,
int mask_x,
int mask_y,
int dst_x,
int dst_y,
unsigned int width,
unsigned int height)
{
cairo_image_surface_t *dst = _dst;
cairo_image_source_t *src = (cairo_image_source_t *)abstract_src;
cairo_image_source_t *mask = (cairo_image_source_t *)abstract_mask;
TRACE ((stderr, "%s\n", __FUNCTION__));
#if PIXMAN_HAS_OP_LERP
pixman_image_composite32 (PIXMAN_OP_LERP_SRC,
src->pixman_image, mask->pixman_image, dst->pixman_image,
src_x, src_y,
mask_x, mask_y,
dst_x, dst_y,
width, height);
#else
/* Punch the clip out of the destination */
TRACE ((stderr, "%s - OUT_REVERSE (mask=%d/%p, dst=%d/%p)\n",
__FUNCTION__,
mask->base.unique_id, mask->pixman_image,
dst->base.unique_id, dst->pixman_image));
pixman_image_composite32 (PIXMAN_OP_OUT_REVERSE,
mask->pixman_image, NULL, dst->pixman_image,
mask_x, mask_y,
0, 0,
dst_x, dst_y,
width, height);
/* Now add the two results together */
TRACE ((stderr, "%s - ADD (src=%d/%p, mask=%d/%p, dst=%d/%p)\n",
__FUNCTION__,
src->base.unique_id, src->pixman_image,
mask->base.unique_id, mask->pixman_image,
dst->base.unique_id, dst->pixman_image));
pixman_image_composite32 (PIXMAN_OP_ADD,
src->pixman_image, mask->pixman_image, dst->pixman_image,
src_x, src_y,
mask_x, mask_y,
dst_x, dst_y,
width, height);
#endif
return CAIRO_STATUS_SUCCESS;
}
static cairo_int_status_t
composite_boxes (void *_dst,
cairo_operator_t op,
cairo_surface_t *abstract_src,
cairo_surface_t *abstract_mask,
int src_x,
int src_y,
int mask_x,
int mask_y,
int dst_x,
int dst_y,
cairo_boxes_t *boxes,
const cairo_rectangle_int_t *extents)
{
pixman_image_t *dst = to_pixman_image (_dst);
pixman_image_t *src = ((cairo_image_source_t *)abstract_src)->pixman_image;
pixman_image_t *mask = abstract_mask ? ((cairo_image_source_t *)abstract_mask)->pixman_image : NULL;
pixman_image_t *free_src = NULL;
struct _cairo_boxes_chunk *chunk;
int i;
/* XXX consider using a region? saves multiple prepare-composite */
TRACE ((stderr, "%s x %d\n", __FUNCTION__, boxes->num_boxes));
if (((cairo_surface_t *)_dst)->is_clear &&
(op == CAIRO_OPERATOR_SOURCE ||
op == CAIRO_OPERATOR_OVER ||
op == CAIRO_OPERATOR_ADD)) {
op = PIXMAN_OP_SRC;
} else if (mask) {
if (op == CAIRO_OPERATOR_CLEAR) {
#if PIXMAN_HAS_OP_LERP
op = PIXMAN_OP_LERP_CLEAR;
#else
free_src = src = _pixman_image_for_color (CAIRO_COLOR_WHITE);
op = PIXMAN_OP_OUT_REVERSE;
#endif
} else if (op == CAIRO_OPERATOR_SOURCE) {
#if PIXMAN_HAS_OP_LERP
op = PIXMAN_OP_LERP_SRC;
#else
return CAIRO_INT_STATUS_UNSUPPORTED;
#endif
} else {
op = _pixman_operator (op);
}
} else {
op = _pixman_operator (op);
}
for (chunk = &boxes->chunks; chunk; chunk = chunk->next) {
for (i = 0; i < chunk->count; i++) {
int x1 = _cairo_fixed_integer_part (chunk->base[i].p1.x);
int y1 = _cairo_fixed_integer_part (chunk->base[i].p1.y);
int x2 = _cairo_fixed_integer_part (chunk->base[i].p2.x);
int y2 = _cairo_fixed_integer_part (chunk->base[i].p2.y);
pixman_image_composite32 (op, src, mask, dst,
x1 + src_x, y1 + src_y,
x1 + mask_x, y1 + mask_y,
x1 + dst_x, y1 + dst_y,
x2 - x1, y2 - y1);
}
}
if (free_src)
pixman_image_unref (free_src);
return CAIRO_STATUS_SUCCESS;
}
#define CAIRO_FIXED_16_16_MIN _cairo_fixed_from_int (-32768)
#define CAIRO_FIXED_16_16_MAX _cairo_fixed_from_int (32767)
static cairo_bool_t
line_exceeds_16_16 (const cairo_line_t *line)
{
return
line->p1.x <= CAIRO_FIXED_16_16_MIN ||
line->p1.x >= CAIRO_FIXED_16_16_MAX ||
line->p2.x <= CAIRO_FIXED_16_16_MIN ||
line->p2.x >= CAIRO_FIXED_16_16_MAX ||
line->p1.y <= CAIRO_FIXED_16_16_MIN ||
line->p1.y >= CAIRO_FIXED_16_16_MAX ||
line->p2.y <= CAIRO_FIXED_16_16_MIN ||
line->p2.y >= CAIRO_FIXED_16_16_MAX;
}
static void
project_line_x_onto_16_16 (const cairo_line_t *line,
cairo_fixed_t top,
cairo_fixed_t bottom,
pixman_line_fixed_t *out)
{
/* XXX use fixed-point arithmetic? */
cairo_point_double_t p1, p2;
double m;
p1.x = _cairo_fixed_to_double (line->p1.x);
p1.y = _cairo_fixed_to_double (line->p1.y);
p2.x = _cairo_fixed_to_double (line->p2.x);
p2.y = _cairo_fixed_to_double (line->p2.y);
m = (p2.x - p1.x) / (p2.y - p1.y);
out->p1.x = _cairo_fixed_16_16_from_double (p1.x + m * _cairo_fixed_to_double (top - line->p1.y));
out->p2.x = _cairo_fixed_16_16_from_double (p1.x + m * _cairo_fixed_to_double (bottom - line->p1.y));
}
void
_pixman_image_add_traps (pixman_image_t *image,
int dst_x, int dst_y,
cairo_traps_t *traps)
{
cairo_trapezoid_t *t = traps->traps;
int num_traps = traps->num_traps;
while (num_traps--) {
pixman_trapezoid_t trap;
/* top/bottom will be clamped to surface bounds */
trap.top = _cairo_fixed_to_16_16 (t->top);
trap.bottom = _cairo_fixed_to_16_16 (t->bottom);
/* However, all the other coordinates will have been left untouched so
* as not to introduce numerical error. Recompute them if they
* exceed the 16.16 limits.
*/
if (unlikely (line_exceeds_16_16 (&t->left))) {
project_line_x_onto_16_16 (&t->left, t->top, t->bottom, &trap.left);
trap.left.p1.y = trap.top;
trap.left.p2.y = trap.bottom;
} else {
trap.left.p1.x = _cairo_fixed_to_16_16 (t->left.p1.x);
trap.left.p1.y = _cairo_fixed_to_16_16 (t->left.p1.y);
trap.left.p2.x = _cairo_fixed_to_16_16 (t->left.p2.x);
trap.left.p2.y = _cairo_fixed_to_16_16 (t->left.p2.y);
}
if (unlikely (line_exceeds_16_16 (&t->right))) {
project_line_x_onto_16_16 (&t->right, t->top, t->bottom, &trap.right);
trap.right.p1.y = trap.top;
trap.right.p2.y = trap.bottom;
} else {
trap.right.p1.x = _cairo_fixed_to_16_16 (t->right.p1.x);
trap.right.p1.y = _cairo_fixed_to_16_16 (t->right.p1.y);
trap.right.p2.x = _cairo_fixed_to_16_16 (t->right.p2.x);
trap.right.p2.y = _cairo_fixed_to_16_16 (t->right.p2.y);
}
pixman_rasterize_trapezoid (image, &trap, -dst_x, -dst_y);
t++;
}
}
static cairo_int_status_t
composite_traps (void *_dst,
cairo_operator_t op,
cairo_surface_t *abstract_src,
int src_x,
int src_y,
int dst_x,
int dst_y,
const cairo_rectangle_int_t *extents,
cairo_antialias_t antialias,
cairo_traps_t *traps)
{
cairo_image_surface_t *dst = (cairo_image_surface_t *) _dst;
cairo_image_source_t *src = (cairo_image_source_t *) abstract_src;
pixman_image_t *mask;
pixman_format_code_t format;
TRACE ((stderr, "%s\n", __FUNCTION__));
/* Special case adding trapezoids onto a mask surface; we want to avoid
* creating an intermediate temporary mask unnecessarily.
*
* We make the assumption here that the portion of the trapezoids
* contained within the surface is bounded by [dst_x,dst_y,width,height];
* the Cairo core code passes bounds based on the trapezoid extents.
*/
format = antialias == CAIRO_ANTIALIAS_NONE ? PIXMAN_a1 : PIXMAN_a8;
if (dst->pixman_format == format &&
(abstract_src == NULL ||
(op == CAIRO_OPERATOR_ADD && src->is_opaque_solid)))
{
_pixman_image_add_traps (dst->pixman_image, dst_x, dst_y, traps);
return CAIRO_STATUS_SUCCESS;
}
mask = pixman_image_create_bits (format,
extents->width, extents->height,
NULL, 0);
if (unlikely (mask == NULL))
return _cairo_error (CAIRO_STATUS_NO_MEMORY);
_pixman_image_add_traps (mask, extents->x, extents->y, traps);
pixman_image_composite32 (_pixman_operator (op),
src->pixman_image, mask, dst->pixman_image,
extents->x + src_x, extents->y + src_y,
0, 0,
extents->x - dst_x, extents->y - dst_y,
extents->width, extents->height);
pixman_image_unref (mask);
return CAIRO_STATUS_SUCCESS;
}
static void
set_point (pixman_point_fixed_t *p, cairo_point_t *c)
{
p->x = _cairo_fixed_to_16_16 (c->x);
p->y = _cairo_fixed_to_16_16 (c->y);
}
void
_pixman_image_add_tristrip (pixman_image_t *image,
int dst_x, int dst_y,
cairo_tristrip_t *strip)
{
pixman_triangle_t tri;
pixman_point_fixed_t *p[3] = {&tri.p1, &tri.p2, &tri.p3 };
int n;
set_point (p[0], &strip->points[0]);
set_point (p[1], &strip->points[1]);
set_point (p[2], &strip->points[2]);
pixman_add_triangles (image, -dst_x, -dst_y, 1, &tri);
for (n = 3; n < strip->num_points; n++) {
set_point (p[n%3], &strip->points[n]);
pixman_add_triangles (image, -dst_x, -dst_y, 1, &tri);
}
}
static cairo_int_status_t
composite_tristrip (void *_dst,
cairo_operator_t op,
cairo_surface_t *abstract_src,
int src_x,
int src_y,
int dst_x,
int dst_y,
const cairo_rectangle_int_t *extents,
cairo_antialias_t antialias,
cairo_tristrip_t *strip)
{
cairo_image_surface_t *dst = (cairo_image_surface_t *) _dst;
cairo_image_source_t *src = (cairo_image_source_t *) abstract_src;
pixman_image_t *mask;
pixman_format_code_t format;
TRACE ((stderr, "%s\n", __FUNCTION__));
if (strip->num_points < 3)
return CAIRO_STATUS_SUCCESS;
format = antialias == CAIRO_ANTIALIAS_NONE ? PIXMAN_a1 : PIXMAN_a8;
if (dst->pixman_format == format &&
(abstract_src == NULL ||
(op == CAIRO_OPERATOR_ADD && src->is_opaque_solid)))
{
_pixman_image_add_tristrip (dst->pixman_image, dst_x, dst_y, strip);
return CAIRO_STATUS_SUCCESS;
}
mask = pixman_image_create_bits (format,
extents->width, extents->height,
NULL, 0);
if (unlikely (mask == NULL))
return _cairo_error (CAIRO_STATUS_NO_MEMORY);
_pixman_image_add_tristrip (mask, extents->x, extents->y, strip);
pixman_image_composite32 (_pixman_operator (op),
src->pixman_image, mask, dst->pixman_image,
extents->x + src_x, extents->y + src_y,
0, 0,
extents->x - dst_x, extents->y - dst_y,
extents->width, extents->height);
pixman_image_unref (mask);
return CAIRO_STATUS_SUCCESS;
}
static cairo_int_status_t
check_composite_glyphs (const cairo_composite_rectangles_t *extents,
cairo_scaled_font_t *scaled_font,
cairo_glyph_t *glyphs,
int *num_glyphs)
{
return CAIRO_STATUS_SUCCESS;
}
#if HAS_PIXMAN_GLYPHS
static pixman_glyph_cache_t *global_glyph_cache;
static inline pixman_glyph_cache_t *
get_glyph_cache (void)
{
if (!global_glyph_cache)
global_glyph_cache = pixman_glyph_cache_create ();
return global_glyph_cache;
}
void
_cairo_image_scaled_glyph_fini (cairo_scaled_font_t *scaled_font,
cairo_scaled_glyph_t *scaled_glyph)
{
CAIRO_MUTEX_LOCK (_cairo_glyph_cache_mutex);
if (global_glyph_cache) {
pixman_glyph_cache_remove (
global_glyph_cache, scaled_font,
(void *)_cairo_scaled_glyph_index (scaled_glyph));
}
CAIRO_MUTEX_UNLOCK (_cairo_glyph_cache_mutex);
}
static cairo_int_status_t
composite_glyphs (void *_dst,
cairo_operator_t op,
cairo_surface_t *_src,
int src_x,
int src_y,
int dst_x,
int dst_y,
cairo_composite_glyphs_info_t *info)
{
cairo_int_status_t status = CAIRO_INT_STATUS_SUCCESS;
pixman_glyph_cache_t *glyph_cache;
pixman_glyph_t pglyphs_stack[CAIRO_STACK_ARRAY_LENGTH (pixman_glyph_t)];
pixman_glyph_t *pglyphs = pglyphs_stack;
pixman_glyph_t *pg;
int i;
TRACE ((stderr, "%s\n", __FUNCTION__));
CAIRO_MUTEX_LOCK (_cairo_glyph_cache_mutex);
glyph_cache = get_glyph_cache();
if (unlikely (glyph_cache == NULL)) {
status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
goto out_unlock;
}
pixman_glyph_cache_freeze (glyph_cache);
if (info->num_glyphs > ARRAY_LENGTH (pglyphs_stack)) {
pglyphs = _cairo_malloc_ab (info->num_glyphs, sizeof (pixman_glyph_t));
if (unlikely (pglyphs == NULL)) {
status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
goto out_thaw;
}
}
pg = pglyphs;
for (i = 0; i < info->num_glyphs; i++) {
unsigned long index = info->glyphs[i].index;
const void *glyph;
glyph = pixman_glyph_cache_lookup (glyph_cache, info->font, (void *)index);
if (!glyph) {
cairo_scaled_glyph_t *scaled_glyph;
cairo_image_surface_t *glyph_surface;
/* This call can actually end up recursing, so we have to
* drop the mutex around it.
*/
CAIRO_MUTEX_UNLOCK (_cairo_glyph_cache_mutex);
status = _cairo_scaled_glyph_lookup (info->font, index,
CAIRO_SCALED_GLYPH_INFO_SURFACE,
&scaled_glyph);
CAIRO_MUTEX_LOCK (_cairo_glyph_cache_mutex);
if (unlikely (status))
goto out_thaw;
glyph_surface = scaled_glyph->surface;
glyph = pixman_glyph_cache_insert (glyph_cache, info->font, (void *)index,
glyph_surface->base.device_transform.x0,
glyph_surface->base.device_transform.y0,
glyph_surface->pixman_image);
if (unlikely (!glyph)) {
status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
goto out_thaw;
}
}
pg->x = _cairo_lround (info->glyphs[i].x);
pg->y = _cairo_lround (info->glyphs[i].y);
pg->glyph = glyph;
pg++;
}
if (info->use_mask) {
pixman_format_code_t mask_format;
mask_format = pixman_glyph_get_mask_format (glyph_cache, pg - pglyphs, pglyphs);
pixman_composite_glyphs (_pixman_operator (op),
((cairo_image_source_t *)_src)->pixman_image,
to_pixman_image (_dst),
mask_format,
info->extents.x + src_x, info->extents.y + src_y,
info->extents.x, info->extents.y,
info->extents.x - dst_x, info->extents.y - dst_y,
info->extents.width, info->extents.height,
glyph_cache, pg - pglyphs, pglyphs);
} else {
pixman_composite_glyphs_no_mask (_pixman_operator (op),
((cairo_image_source_t *)_src)->pixman_image,
to_pixman_image (_dst),
src_x, src_y,
- dst_x, - dst_y,
glyph_cache, pg - pglyphs, pglyphs);
}
out_thaw:
pixman_glyph_cache_thaw (glyph_cache);
if (pglyphs != pglyphs_stack)
free(pglyphs);
out_unlock:
CAIRO_MUTEX_UNLOCK (_cairo_glyph_cache_mutex);
return status;
}
#else
void
_cairo_image_scaled_glyph_fini (cairo_scaled_font_t *scaled_font,
cairo_scaled_glyph_t *scaled_glyph)
{
}
static cairo_int_status_t
composite_one_glyph (void *_dst,
cairo_operator_t op,
cairo_surface_t *_src,
int src_x,
int src_y,
int dst_x,
int dst_y,
cairo_composite_glyphs_info_t *info)
{
cairo_image_surface_t *glyph_surface;
cairo_scaled_glyph_t *scaled_glyph;
cairo_status_t status;
int x, y;
TRACE ((stderr, "%s\n", __FUNCTION__));
status = _cairo_scaled_glyph_lookup (info->font,
info->glyphs[0].index,
CAIRO_SCALED_GLYPH_INFO_SURFACE,
&scaled_glyph);
if (unlikely (status))
return status;
glyph_surface = scaled_glyph->surface;
if (glyph_surface->width == 0 || glyph_surface->height == 0)
return CAIRO_INT_STATUS_NOTHING_TO_DO;
/* round glyph locations to the nearest pixel */
/* XXX: FRAGILE: We're ignoring device_transform scaling here. A bug? */
x = _cairo_lround (info->glyphs[0].x -
glyph_surface->base.device_transform.x0);
y = _cairo_lround (info->glyphs[0].y -
glyph_surface->base.device_transform.y0);
pixman_image_composite32 (_pixman_operator (op),
((cairo_image_source_t *)_src)->pixman_image,
glyph_surface->pixman_image,
to_pixman_image (_dst),
x + src_x, y + src_y,
0, 0,
x - dst_x, y - dst_y,
glyph_surface->width,
glyph_surface->height);
return CAIRO_INT_STATUS_SUCCESS;
}
static cairo_int_status_t
composite_glyphs_via_mask (void *_dst,
cairo_operator_t op,
cairo_surface_t *_src,
int src_x,
int src_y,
int dst_x,
int dst_y,
cairo_composite_glyphs_info_t *info)
{
cairo_scaled_glyph_t *glyph_cache[64];
pixman_image_t *white = _pixman_image_for_color (CAIRO_COLOR_WHITE);
cairo_scaled_glyph_t *scaled_glyph;
uint8_t buf[2048];
pixman_image_t *mask;
pixman_format_code_t format;
cairo_status_t status;
int i;
TRACE ((stderr, "%s\n", __FUNCTION__));
if (unlikely (white == NULL))
return _cairo_error (CAIRO_STATUS_NO_MEMORY);
/* XXX convert the glyphs to common formats a8/a8r8g8b8 to hit
* optimised paths through pixman. Should we increase the bit
* depth of the target surface, we should reconsider the appropriate
* mask formats.
*/
status = _cairo_scaled_glyph_lookup (info->font,
info->glyphs[0].index,
CAIRO_SCALED_GLYPH_INFO_SURFACE,
&scaled_glyph);
if (unlikely (status)) {
pixman_image_unref (white);
return status;
}
memset (glyph_cache, 0, sizeof (glyph_cache));
glyph_cache[info->glyphs[0].index % ARRAY_LENGTH (glyph_cache)] = scaled_glyph;
format = PIXMAN_a8;
i = (info->extents.width + 3) & ~3;
if (scaled_glyph->surface->base.content & CAIRO_CONTENT_COLOR) {
format = PIXMAN_a8r8g8b8;
i = info->extents.width * 4;