1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
|
/*
* Copyright 2006 The Android Open Source Project
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include "SkGradientShader.h"
#include "SkClampRange.h"
#include "SkColorPriv.h"
#include "SkMallocPixelRef.h"
#include "SkUnitMapper.h"
#include "SkUtils.h"
#include "SkTemplates.h"
#include "SkBitmapCache.h"
#ifndef SK_DISABLE_DITHER_32BIT_GRADIENT
#define USE_DITHER_32BIT_GRADIENT
#endif
static void sk_memset32_dither(uint32_t dst[], uint32_t v0, uint32_t v1,
int count) {
if (count > 0) {
if (v0 == v1) {
sk_memset32(dst, v0, count);
} else {
int pairs = count >> 1;
for (int i = 0; i < pairs; i++) {
*dst++ = v0;
*dst++ = v1;
}
if (count & 1) {
*dst = v0;
}
}
}
}
// Clamp
static SkFixed clamp_tileproc(SkFixed x) {
return SkClampMax(x, 0xFFFF);
}
// Repeat
static SkFixed repeat_tileproc(SkFixed x) {
return x & 0xFFFF;
}
static inline int repeat_bits(int x, const int bits) {
return x & ((1 << bits) - 1);
}
static inline int repeat_8bits(int x) {
return x & 0xFF;
}
// Mirror
// Visual Studio 2010 (MSC_VER=1600) optimizes bit-shift code incorrectly.
// See http://code.google.com/p/skia/issues/detail?id=472
#if defined(_MSC_VER) && (_MSC_VER >= 1600)
#pragma optimize("", off)
#endif
static inline SkFixed mirror_tileproc(SkFixed x) {
int s = x << 15 >> 31;
return (x ^ s) & 0xFFFF;
}
static inline int mirror_bits(int x, const int bits) {
#ifdef SK_CPU_HAS_CONDITIONAL_INSTR
if (x & (1 << bits))
x = ~x;
return x & ((1 << bits) - 1);
#else
int s = x << (31 - bits) >> 31;
return (x ^ s) & ((1 << bits) - 1);
#endif
}
static inline int mirror_8bits(int x) {
#ifdef SK_CPU_HAS_CONDITIONAL_INSTR
if (x & 256) {
x = ~x;
}
return x & 255;
#else
int s = x << 23 >> 31;
return (x ^ s) & 0xFF;
#endif
}
#if defined(_MSC_VER) && (_MSC_VER >= 1600)
#pragma optimize("", on)
#endif
///////////////////////////////////////////////////////////////////////////////
typedef SkFixed (*TileProc)(SkFixed);
static const TileProc gTileProcs[] = {
clamp_tileproc,
repeat_tileproc,
mirror_tileproc
};
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
class Gradient_Shader : public SkShader {
public:
Gradient_Shader(const SkColor colors[], const SkScalar pos[],
int colorCount, SkShader::TileMode mode, SkUnitMapper* mapper);
virtual ~Gradient_Shader();
// overrides
virtual bool setContext(const SkBitmap&, const SkPaint&, const SkMatrix&) SK_OVERRIDE;
virtual uint32_t getFlags() SK_OVERRIDE { return fFlags; }
virtual bool isOpaque() const SK_OVERRIDE;
enum {
/// Seems like enough for visual accuracy. TODO: if pos[] deserves
/// it, use a larger cache.
kCache16Bits = 8,
kGradient16Length = (1 << kCache16Bits),
/// Each cache gets 1 extra entry at the end so we don't have to
/// test for end-of-cache in lerps. This is also the value used
/// to stride *writes* into the dither cache; it must not be zero.
/// Total space for a cache is 2x kCache16Count entries: one
/// regular cache, one for dithering.
kCache16Count = kGradient16Length + 1,
kCache16Shift = 16 - kCache16Bits,
kSqrt16Shift = 8 - kCache16Bits,
/// Seems like enough for visual accuracy. TODO: if pos[] deserves
/// it, use a larger cache.
kCache32Bits = 8,
kGradient32Length = (1 << kCache32Bits),
/// Each cache gets 1 extra entry at the end so we don't have to
/// test for end-of-cache in lerps. This is also the value used
/// to stride *writes* into the dither cache; it must not be zero.
/// Total space for a cache is 2x kCache32Count entries: one
/// regular cache, one for dithering.
kCache32Count = kGradient32Length + 1,
kCache32Shift = 16 - kCache32Bits,
kSqrt32Shift = 8 - kCache32Bits,
/// This value is used to *read* the dither cache; it may be 0
/// if dithering is disabled.
#ifdef USE_DITHER_32BIT_GRADIENT
kDitherStride32 = kCache32Count,
#else
kDitherStride32 = 0,
#endif
kDitherStride16 = kCache16Count,
kLerpRemainderMask32 = (1 << (16 - kCache32Bits)) - 1
};
protected:
Gradient_Shader(SkFlattenableReadBuffer& );
virtual void flatten(SkFlattenableWriteBuffer&) const SK_OVERRIDE;
SkUnitMapper* fMapper;
SkMatrix fPtsToUnit; // set by subclass
SkMatrix fDstToIndex;
SkMatrix::MapXYProc fDstToIndexProc;
TileMode fTileMode;
TileProc fTileProc;
int fColorCount;
uint8_t fDstToIndexClass;
uint8_t fFlags;
struct Rec {
SkFixed fPos; // 0...1
uint32_t fScale; // (1 << 24) / range
};
Rec* fRecs;
const uint16_t* getCache16() const;
const SkPMColor* getCache32() const;
void commonAsABitmap(SkBitmap*) const;
void commonAsAGradient(GradientInfo*) const;
private:
enum {
kColorStorageCount = 4, // more than this many colors, and we'll use sk_malloc for the space
kStorageSize = kColorStorageCount * (sizeof(SkColor) + sizeof(Rec))
};
SkColor fStorage[(kStorageSize + 3) >> 2];
SkColor* fOrigColors; // original colors, before modulation by paint in setContext
bool fColorsAreOpaque;
mutable uint16_t* fCache16; // working ptr. If this is NULL, we need to recompute the cache values
mutable SkPMColor* fCache32; // working ptr. If this is NULL, we need to recompute the cache values
mutable uint16_t* fCache16Storage; // storage for fCache16, allocated on demand
mutable SkMallocPixelRef* fCache32PixelRef;
mutable unsigned fCacheAlpha; // the alpha value we used when we computed the cache. larger than 8bits so we can store uninitialized value
static void Build16bitCache(uint16_t[], SkColor c0, SkColor c1, int count);
static void Build32bitCache(SkPMColor[], SkColor c0, SkColor c1, int count,
U8CPU alpha);
void setCacheAlpha(U8CPU alpha) const;
void initCommon();
typedef SkShader INHERITED;
};
Gradient_Shader::Gradient_Shader(const SkColor colors[], const SkScalar pos[],
int colorCount, SkShader::TileMode mode, SkUnitMapper* mapper) {
SkASSERT(colorCount > 1);
fCacheAlpha = 256; // init to a value that paint.getAlpha() can't return
fMapper = mapper;
SkSafeRef(mapper);
SkASSERT((unsigned)mode < SkShader::kTileModeCount);
SkASSERT(SkShader::kTileModeCount == SK_ARRAY_COUNT(gTileProcs));
fTileMode = mode;
fTileProc = gTileProcs[mode];
fCache16 = fCache16Storage = NULL;
fCache32 = NULL;
fCache32PixelRef = NULL;
/* Note: we let the caller skip the first and/or last position.
i.e. pos[0] = 0.3, pos[1] = 0.7
In these cases, we insert dummy entries to ensure that the final data
will be bracketed by [0, 1].
i.e. our_pos[0] = 0, our_pos[1] = 0.3, our_pos[2] = 0.7, our_pos[3] = 1
Thus colorCount (the caller's value, and fColorCount (our value) may
differ by up to 2. In the above example:
colorCount = 2
fColorCount = 4
*/
fColorCount = colorCount;
// check if we need to add in dummy start and/or end position/colors
bool dummyFirst = false;
bool dummyLast = false;
if (pos) {
dummyFirst = pos[0] != 0;
dummyLast = pos[colorCount - 1] != SK_Scalar1;
fColorCount += dummyFirst + dummyLast;
}
if (fColorCount > kColorStorageCount) {
size_t size = sizeof(SkColor) + sizeof(Rec);
fOrigColors = reinterpret_cast<SkColor*>(
sk_malloc_throw(size * fColorCount));
}
else {
fOrigColors = fStorage;
}
// Now copy over the colors, adding the dummies as needed
{
SkColor* origColors = fOrigColors;
if (dummyFirst) {
*origColors++ = colors[0];
}
memcpy(origColors, colors, colorCount * sizeof(SkColor));
if (dummyLast) {
origColors += colorCount;
*origColors = colors[colorCount - 1];
}
}
fRecs = (Rec*)(fOrigColors + fColorCount);
if (fColorCount > 2) {
Rec* recs = fRecs;
recs->fPos = 0;
// recs->fScale = 0; // unused;
recs += 1;
if (pos) {
/* We need to convert the user's array of relative positions into
fixed-point positions and scale factors. We need these results
to be strictly monotonic (no two values equal or out of order).
Hence this complex loop that just jams a zero for the scale
value if it sees a segment out of order, and it assures that
we start at 0 and end at 1.0
*/
SkFixed prev = 0;
int startIndex = dummyFirst ? 0 : 1;
int count = colorCount + dummyLast;
for (int i = startIndex; i < count; i++) {
// force the last value to be 1.0
SkFixed curr;
if (i == colorCount) { // we're really at the dummyLast
curr = SK_Fixed1;
} else {
curr = SkScalarToFixed(pos[i]);
}
// pin curr withing range
if (curr < 0) {
curr = 0;
} else if (curr > SK_Fixed1) {
curr = SK_Fixed1;
}
recs->fPos = curr;
if (curr > prev) {
recs->fScale = (1 << 24) / (curr - prev);
} else {
recs->fScale = 0; // ignore this segment
}
// get ready for the next value
prev = curr;
recs += 1;
}
} else { // assume even distribution
SkFixed dp = SK_Fixed1 / (colorCount - 1);
SkFixed p = dp;
SkFixed scale = (colorCount - 1) << 8; // (1 << 24) / dp
for (int i = 1; i < colorCount; i++) {
recs->fPos = p;
recs->fScale = scale;
recs += 1;
p += dp;
}
}
}
this->initCommon();
}
Gradient_Shader::Gradient_Shader(SkFlattenableReadBuffer& buffer) :
INHERITED(buffer) {
fCacheAlpha = 256;
fMapper = static_cast<SkUnitMapper*>(buffer.readFlattenable());
fCache16 = fCache16Storage = NULL;
fCache32 = NULL;
fCache32PixelRef = NULL;
int colorCount = fColorCount = buffer.readU32();
if (colorCount > kColorStorageCount) {
size_t size = sizeof(SkColor) + sizeof(SkPMColor) + sizeof(Rec);
fOrigColors = (SkColor*)sk_malloc_throw(size * colorCount);
} else {
fOrigColors = fStorage;
}
buffer.read(fOrigColors, colorCount * sizeof(SkColor));
fTileMode = (TileMode)buffer.readU8();
fTileProc = gTileProcs[fTileMode];
fRecs = (Rec*)(fOrigColors + colorCount);
if (colorCount > 2) {
Rec* recs = fRecs;
recs[0].fPos = 0;
for (int i = 1; i < colorCount; i++) {
recs[i].fPos = buffer.readS32();
recs[i].fScale = buffer.readU32();
}
}
buffer.readMatrix(&fPtsToUnit);
this->initCommon();
}
Gradient_Shader::~Gradient_Shader() {
if (fCache16Storage) {
sk_free(fCache16Storage);
}
SkSafeUnref(fCache32PixelRef);
if (fOrigColors != fStorage) {
sk_free(fOrigColors);
}
SkSafeUnref(fMapper);
}
void Gradient_Shader::initCommon() {
fFlags = 0;
unsigned colorAlpha = 0xFF;
for (int i = 0; i < fColorCount; i++) {
colorAlpha &= SkColorGetA(fOrigColors[i]);
}
fColorsAreOpaque = colorAlpha == 0xFF;
}
void Gradient_Shader::flatten(SkFlattenableWriteBuffer& buffer) const {
this->INHERITED::flatten(buffer);
buffer.writeFlattenable(fMapper);
buffer.write32(fColorCount);
buffer.writeMul4(fOrigColors, fColorCount * sizeof(SkColor));
buffer.write8(fTileMode);
if (fColorCount > 2) {
Rec* recs = fRecs;
for (int i = 1; i < fColorCount; i++) {
buffer.write32(recs[i].fPos);
buffer.write32(recs[i].fScale);
}
}
buffer.writeMatrix(fPtsToUnit);
}
bool Gradient_Shader::isOpaque() const {
return fColorsAreOpaque;
}
bool Gradient_Shader::setContext(const SkBitmap& device,
const SkPaint& paint,
const SkMatrix& matrix) {
if (!this->INHERITED::setContext(device, paint, matrix)) {
return false;
}
const SkMatrix& inverse = this->getTotalInverse();
if (!fDstToIndex.setConcat(fPtsToUnit, inverse)) {
return false;
}
fDstToIndexProc = fDstToIndex.getMapXYProc();
fDstToIndexClass = (uint8_t)SkShader::ComputeMatrixClass(fDstToIndex);
// now convert our colors in to PMColors
unsigned paintAlpha = this->getPaintAlpha();
fFlags = this->INHERITED::getFlags();
if (fColorsAreOpaque && paintAlpha == 0xFF) {
fFlags |= kOpaqueAlpha_Flag;
}
// we can do span16 as long as our individual colors are opaque,
// regardless of the paint's alpha
if (fColorsAreOpaque) {
fFlags |= kHasSpan16_Flag;
}
this->setCacheAlpha(paintAlpha);
return true;
}
void Gradient_Shader::setCacheAlpha(U8CPU alpha) const {
// if the new alpha differs from the previous time we were called, inval our cache
// this will trigger the cache to be rebuilt.
// we don't care about the first time, since the cache ptrs will already be NULL
if (fCacheAlpha != alpha) {
fCache16 = NULL; // inval the cache
fCache32 = NULL; // inval the cache
fCacheAlpha = alpha; // record the new alpha
// inform our subclasses
if (fCache32PixelRef) {
fCache32PixelRef->notifyPixelsChanged();
}
}
}
#define Fixed_To_Dot8(x) (((x) + 0x80) >> 8)
/** We take the original colors, not our premultiplied PMColors, since we can
build a 16bit table as long as the original colors are opaque, even if the
paint specifies a non-opaque alpha.
*/
void Gradient_Shader::Build16bitCache(uint16_t cache[], SkColor c0, SkColor c1,
int count) {
SkASSERT(count > 1);
SkASSERT(SkColorGetA(c0) == 0xFF);
SkASSERT(SkColorGetA(c1) == 0xFF);
SkFixed r = SkColorGetR(c0);
SkFixed g = SkColorGetG(c0);
SkFixed b = SkColorGetB(c0);
SkFixed dr = SkIntToFixed(SkColorGetR(c1) - r) / (count - 1);
SkFixed dg = SkIntToFixed(SkColorGetG(c1) - g) / (count - 1);
SkFixed db = SkIntToFixed(SkColorGetB(c1) - b) / (count - 1);
r = SkIntToFixed(r) + 0x8000;
g = SkIntToFixed(g) + 0x8000;
b = SkIntToFixed(b) + 0x8000;
do {
unsigned rr = r >> 16;
unsigned gg = g >> 16;
unsigned bb = b >> 16;
cache[0] = SkPackRGB16(SkR32ToR16(rr), SkG32ToG16(gg), SkB32ToB16(bb));
cache[kCache16Count] = SkDitherPack888ToRGB16(rr, gg, bb);
cache += 1;
r += dr;
g += dg;
b += db;
} while (--count != 0);
}
/*
* 2x2 dither a fixed-point color component (8.16) down to 8, matching the
* semantics of how we 2x2 dither 32->16
*/
static inline U8CPU dither_fixed_to_8(SkFixed n) {
n >>= 8;
return ((n << 1) - ((n >> 8 << 8) | (n >> 8))) >> 8;
}
/*
* For dithering with premultiply, we want to ceiling the alpha component,
* to ensure that it is always >= any color component.
*/
static inline U8CPU dither_ceil_fixed_to_8(SkFixed n) {
n >>= 8;
return ((n << 1) - (n | (n >> 8))) >> 8;
}
void Gradient_Shader::Build32bitCache(SkPMColor cache[], SkColor c0, SkColor c1,
int count, U8CPU paintAlpha) {
SkASSERT(count > 1);
// need to apply paintAlpha to our two endpoints
SkFixed a = SkMulDiv255Round(SkColorGetA(c0), paintAlpha);
SkFixed da;
{
int tmp = SkMulDiv255Round(SkColorGetA(c1), paintAlpha);
da = SkIntToFixed(tmp - a) / (count - 1);
}
SkFixed r = SkColorGetR(c0);
SkFixed g = SkColorGetG(c0);
SkFixed b = SkColorGetB(c0);
SkFixed dr = SkIntToFixed(SkColorGetR(c1) - r) / (count - 1);
SkFixed dg = SkIntToFixed(SkColorGetG(c1) - g) / (count - 1);
SkFixed db = SkIntToFixed(SkColorGetB(c1) - b) / (count - 1);
a = SkIntToFixed(a) + 0x8000;
r = SkIntToFixed(r) + 0x8000;
g = SkIntToFixed(g) + 0x8000;
b = SkIntToFixed(b) + 0x8000;
do {
cache[0] = SkPremultiplyARGBInline(a >> 16, r >> 16, g >> 16, b >> 16);
cache[kCache32Count] =
SkPremultiplyARGBInline(dither_ceil_fixed_to_8(a),
dither_fixed_to_8(r),
dither_fixed_to_8(g),
dither_fixed_to_8(b));
cache += 1;
a += da;
r += dr;
g += dg;
b += db;
} while (--count != 0);
}
static inline int SkFixedToFFFF(SkFixed x) {
SkASSERT((unsigned)x <= SK_Fixed1);
return x - (x >> 16);
}
static inline U16CPU bitsTo16(unsigned x, const unsigned bits) {
SkASSERT(x < (1U << bits));
if (6 == bits) {
return (x << 10) | (x << 4) | (x >> 2);
}
if (8 == bits) {
return (x << 8) | x;
}
sk_throw();
return 0;
}
/** We duplicate the last value in each half of the cache so that
interpolation doesn't have to special-case being at the last point.
*/
static void complete_16bit_cache(uint16_t* cache, int stride) {
cache[stride - 1] = cache[stride - 2];
cache[2 * stride - 1] = cache[2 * stride - 2];
}
const uint16_t* Gradient_Shader::getCache16() const {
if (fCache16 == NULL) {
// double the count for dither entries
const int entryCount = kCache16Count * 2;
const size_t allocSize = sizeof(uint16_t) * entryCount;
if (fCache16Storage == NULL) { // set the storage and our working ptr
fCache16Storage = (uint16_t*)sk_malloc_throw(allocSize);
}
fCache16 = fCache16Storage;
if (fColorCount == 2) {
Build16bitCache(fCache16, fOrigColors[0], fOrigColors[1],
kGradient16Length);
} else {
Rec* rec = fRecs;
int prevIndex = 0;
for (int i = 1; i < fColorCount; i++) {
int nextIndex = SkFixedToFFFF(rec[i].fPos) >> kCache16Shift;
SkASSERT(nextIndex < kCache16Count);
if (nextIndex > prevIndex)
Build16bitCache(fCache16 + prevIndex, fOrigColors[i-1], fOrigColors[i], nextIndex - prevIndex + 1);
prevIndex = nextIndex;
}
// one extra space left over at the end for complete_16bit_cache()
SkASSERT(prevIndex == kGradient16Length - 1);
}
if (fMapper) {
fCache16Storage = (uint16_t*)sk_malloc_throw(allocSize);
uint16_t* linear = fCache16; // just computed linear data
uint16_t* mapped = fCache16Storage; // storage for mapped data
SkUnitMapper* map = fMapper;
for (int i = 0; i < kGradient16Length; i++) {
int index = map->mapUnit16(bitsTo16(i, kCache16Bits)) >> kCache16Shift;
mapped[i] = linear[index];
mapped[i + kCache16Count] = linear[index + kCache16Count];
}
sk_free(fCache16);
fCache16 = fCache16Storage;
}
complete_16bit_cache(fCache16, kCache16Count);
}
return fCache16;
}
/** We duplicate the last value in each half of the cache so that
interpolation doesn't have to special-case being at the last point.
*/
static void complete_32bit_cache(SkPMColor* cache, int stride) {
cache[stride - 1] = cache[stride - 2];
cache[2 * stride - 1] = cache[2 * stride - 2];
}
const SkPMColor* Gradient_Shader::getCache32() const {
if (fCache32 == NULL) {
// double the count for dither entries
const int entryCount = kCache32Count * 2;
const size_t allocSize = sizeof(SkPMColor) * entryCount;
if (NULL == fCache32PixelRef) {
fCache32PixelRef = SkNEW_ARGS(SkMallocPixelRef,
(NULL, allocSize, NULL));
}
fCache32 = (SkPMColor*)fCache32PixelRef->getAddr();
if (fColorCount == 2) {
Build32bitCache(fCache32, fOrigColors[0], fOrigColors[1],
kGradient32Length, fCacheAlpha);
} else {
Rec* rec = fRecs;
int prevIndex = 0;
for (int i = 1; i < fColorCount; i++) {
int nextIndex = SkFixedToFFFF(rec[i].fPos) >> kCache32Shift;
SkASSERT(nextIndex < kGradient32Length);
if (nextIndex > prevIndex)
Build32bitCache(fCache32 + prevIndex, fOrigColors[i-1],
fOrigColors[i],
nextIndex - prevIndex + 1, fCacheAlpha);
prevIndex = nextIndex;
}
SkASSERT(prevIndex == kGradient32Length - 1);
}
if (fMapper) {
SkMallocPixelRef* newPR = SkNEW_ARGS(SkMallocPixelRef,
(NULL, allocSize, NULL));
SkPMColor* linear = fCache32; // just computed linear data
SkPMColor* mapped = (SkPMColor*)newPR->getAddr(); // storage for mapped data
SkUnitMapper* map = fMapper;
for (int i = 0; i < kGradient32Length; i++) {
int index = map->mapUnit16((i << 8) | i) >> 8;
mapped[i] = linear[index];
mapped[i + kCache32Count] = linear[index + kCache32Count];
}
fCache32PixelRef->unref();
fCache32PixelRef = newPR;
fCache32 = (SkPMColor*)newPR->getAddr();
}
complete_32bit_cache(fCache32, kCache32Count);
}
return fCache32;
}
/*
* Because our caller might rebuild the same (logically the same) gradient
* over and over, we'd like to return exactly the same "bitmap" if possible,
* allowing the client to utilize a cache of our bitmap (e.g. with a GPU).
* To do that, we maintain a private cache of built-bitmaps, based on our
* colors and positions. Note: we don't try to flatten the fMapper, so if one
* is present, we skip the cache for now.
*/
void Gradient_Shader::commonAsABitmap(SkBitmap* bitmap) const {
// our caller assumes no external alpha, so we ensure that our cache is
// built with 0xFF
this->setCacheAlpha(0xFF);
// don't have a way to put the mapper into our cache-key yet
if (fMapper) {
// force our cahce32pixelref to be built
(void)this->getCache32();
bitmap->setConfig(SkBitmap::kARGB_8888_Config, kGradient32Length, 1);
bitmap->setPixelRef(fCache32PixelRef);
return;
}
// build our key: [numColors + colors[] + {positions[]} ]
int count = 1 + fColorCount;
if (fColorCount > 2) {
count += fColorCount - 1; // fRecs[].fPos
}
SkAutoSTMalloc<16, int32_t> storage(count);
int32_t* buffer = storage.get();
*buffer++ = fColorCount;
memcpy(buffer, fOrigColors, fColorCount * sizeof(SkColor));
buffer += fColorCount;
if (fColorCount > 2) {
for (int i = 1; i < fColorCount; i++) {
*buffer++ = fRecs[i].fPos;
}
}
SkASSERT(buffer - storage.get() == count);
///////////////////////////////////
SK_DECLARE_STATIC_MUTEX(gMutex);
static SkBitmapCache* gCache;
// each cache cost 1K of RAM, since each bitmap will be 1x256 at 32bpp
static const int MAX_NUM_CACHED_GRADIENT_BITMAPS = 32;
SkAutoMutexAcquire ama(gMutex);
if (NULL == gCache) {
gCache = new SkBitmapCache(MAX_NUM_CACHED_GRADIENT_BITMAPS);
}
size_t size = count * sizeof(int32_t);
if (!gCache->find(storage.get(), size, bitmap)) {
// force our cahce32pixelref to be built
(void)this->getCache32();
// Only expose the linear section of the cache; don't let the caller
// know about the padding at the end to make interpolation faster.
bitmap->setConfig(SkBitmap::kARGB_8888_Config, kGradient32Length, 1);
bitmap->setPixelRef(fCache32PixelRef);
gCache->add(storage.get(), size, *bitmap);
}
}
void Gradient_Shader::commonAsAGradient(GradientInfo* info) const {
if (info) {
if (info->fColorCount >= fColorCount) {
if (info->fColors) {
memcpy(info->fColors, fOrigColors,
fColorCount * sizeof(SkColor));
}
if (info->fColorOffsets) {
if (fColorCount == 2) {
info->fColorOffsets[0] = 0;
info->fColorOffsets[1] = SK_Scalar1;
} else if (fColorCount > 2) {
for (int i = 0; i < fColorCount; i++)
info->fColorOffsets[i] = SkFixedToScalar(fRecs[i].fPos);
}
}
}
info->fColorCount = fColorCount;
info->fTileMode = fTileMode;
}
}
///////////////////////////////////////////////////////////////////////////////
static void pts_to_unit_matrix(const SkPoint pts[2], SkMatrix* matrix) {
SkVector vec = pts[1] - pts[0];
SkScalar mag = vec.length();
SkScalar inv = mag ? SkScalarInvert(mag) : 0;
vec.scale(inv);
matrix->setSinCos(-vec.fY, vec.fX, pts[0].fX, pts[0].fY);
matrix->postTranslate(-pts[0].fX, -pts[0].fY);
matrix->postScale(inv, inv);
}
///////////////////////////////////////////////////////////////////////////////
class Linear_Gradient : public Gradient_Shader {
public:
Linear_Gradient(const SkPoint pts[2],
const SkColor colors[], const SkScalar pos[], int colorCount,
SkShader::TileMode mode, SkUnitMapper* mapper)
: Gradient_Shader(colors, pos, colorCount, mode, mapper),
fStart(pts[0]),
fEnd(pts[1])
{
pts_to_unit_matrix(pts, &fPtsToUnit);
}
virtual bool setContext(const SkBitmap&, const SkPaint&, const SkMatrix&) SK_OVERRIDE;
virtual void shadeSpan(int x, int y, SkPMColor dstC[], int count) SK_OVERRIDE;
virtual void shadeSpan16(int x, int y, uint16_t dstC[], int count) SK_OVERRIDE;
virtual BitmapType asABitmap(SkBitmap*, SkMatrix*, TileMode*,
SkScalar* twoPointRadialParams) const SK_OVERRIDE;
virtual GradientType asAGradient(GradientInfo* info) const SK_OVERRIDE;
SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(Linear_Gradient)
protected:
Linear_Gradient(SkFlattenableReadBuffer& buffer)
: INHERITED(buffer),
fStart(buffer.readPoint()),
fEnd(buffer.readPoint()) {
}
virtual void flatten(SkFlattenableWriteBuffer& buffer) const SK_OVERRIDE {
this->INHERITED::flatten(buffer);
buffer.writePoint(fStart);
buffer.writePoint(fEnd);
}
private:
typedef Gradient_Shader INHERITED;
const SkPoint fStart;
const SkPoint fEnd;
};
bool Linear_Gradient::setContext(const SkBitmap& device, const SkPaint& paint,
const SkMatrix& matrix) {
if (!this->INHERITED::setContext(device, paint, matrix)) {
return false;
}
unsigned mask = SkMatrix::kTranslate_Mask | SkMatrix::kScale_Mask;
if ((fDstToIndex.getType() & ~mask) == 0) {
fFlags |= SkShader::kConstInY32_Flag;
if ((fFlags & SkShader::kHasSpan16_Flag) && !paint.isDither()) {
// only claim this if we do have a 16bit mode (i.e. none of our
// colors have alpha), and if we are not dithering (which obviously
// is not const in Y).
fFlags |= SkShader::kConstInY16_Flag;
}
}
return true;
}
#define NO_CHECK_ITER \
do { \
unsigned fi = fx >> Gradient_Shader::kCache32Shift; \
SkASSERT(fi <= 0xFF); \
fx += dx; \
*dstC++ = cache[toggle + fi]; \
toggle ^= Gradient_Shader::kDitherStride32; \
} while (0)
namespace {
typedef void (*LinearShadeProc)(TileProc proc, SkFixed dx, SkFixed fx,
SkPMColor* dstC, const SkPMColor* cache,
int toggle, int count);
// This function is deprecated, and will be replaced by
// shadeSpan_linear_vertical_lerp() once Chrome has been weaned off of it.
void shadeSpan_linear_vertical(TileProc proc, SkFixed dx, SkFixed fx,
SkPMColor* SK_RESTRICT dstC,
const SkPMColor* SK_RESTRICT cache,
int toggle, int count) {
// We're a vertical gradient, so no change in a span.
// If colors change sharply across the gradient, dithering is
// insufficient (it subsamples the color space) and we need to lerp.
unsigned fullIndex = proc(fx);
unsigned fi = fullIndex >> (16 - Gradient_Shader::kCache32Bits);
sk_memset32_dither(dstC,
cache[toggle + fi],
cache[(toggle ^ Gradient_Shader::kDitherStride32) + fi],
count);
}
// Linear interpolation (lerp) is unnecessary if there are no sharp
// discontinuities in the gradient - which must be true if there are
// only 2 colors - but it's cheap.
void shadeSpan_linear_vertical_lerp(TileProc proc, SkFixed dx, SkFixed fx,
SkPMColor* SK_RESTRICT dstC,
const SkPMColor* SK_RESTRICT cache,
int toggle, int count) {
// We're a vertical gradient, so no change in a span.
// If colors change sharply across the gradient, dithering is
// insufficient (it subsamples the color space) and we need to lerp.
unsigned fullIndex = proc(fx);
unsigned fi = fullIndex >> (16 - Gradient_Shader::kCache32Bits);
unsigned remainder = fullIndex & Gradient_Shader::kLerpRemainderMask32;
SkPMColor lerp =
SkFastFourByteInterp(
cache[toggle + fi + 1],
cache[toggle + fi], remainder);
SkPMColor dlerp =
SkFastFourByteInterp(
cache[(toggle ^ Gradient_Shader::kDitherStride32) + fi + 1],
cache[(toggle ^ Gradient_Shader::kDitherStride32) + fi], remainder);
sk_memset32_dither(dstC, lerp, dlerp, count);
}
void shadeSpan_linear_clamp(TileProc proc, SkFixed dx, SkFixed fx,
SkPMColor* SK_RESTRICT dstC,
const SkPMColor* SK_RESTRICT cache,
int toggle, int count) {
SkClampRange range;
range.init(fx, dx, count, 0, Gradient_Shader::kGradient32Length);
if ((count = range.fCount0) > 0) {
sk_memset32_dither(dstC,
cache[toggle + range.fV0],
cache[(toggle ^ Gradient_Shader::kDitherStride32) + range.fV0],
count);
dstC += count;
}
if ((count = range.fCount1) > 0) {
int unroll = count >> 3;
fx = range.fFx1;
for (int i = 0; i < unroll; i++) {
NO_CHECK_ITER; NO_CHECK_ITER;
NO_CHECK_ITER; NO_CHECK_ITER;
NO_CHECK_ITER; NO_CHECK_ITER;
NO_CHECK_ITER; NO_CHECK_ITER;
}
if ((count &= 7) > 0) {
do {
NO_CHECK_ITER;
} while (--count != 0);
}
}
if ((count = range.fCount2) > 0) {
sk_memset32_dither(dstC,
cache[toggle + range.fV1],
cache[(toggle ^ Gradient_Shader::kDitherStride32) + range.fV1],
count);
}
}
void shadeSpan_linear_mirror(TileProc proc, SkFixed dx, SkFixed fx,
SkPMColor* SK_RESTRICT dstC,
const SkPMColor* SK_RESTRICT cache,
int toggle, int count) {
do {
unsigned fi = mirror_8bits(fx >> 8);
SkASSERT(fi <= 0xFF);
fx += dx;
*dstC++ = cache[toggle + fi];
toggle ^= Gradient_Shader::kDitherStride32;
} while (--count != 0);
}
void shadeSpan_linear_repeat(TileProc proc, SkFixed dx, SkFixed fx,
SkPMColor* SK_RESTRICT dstC,
const SkPMColor* SK_RESTRICT cache,
int toggle, int count) {
do {
unsigned fi = repeat_8bits(fx >> 8);
SkASSERT(fi <= 0xFF);
fx += dx;
*dstC++ = cache[toggle + fi];
toggle ^= Gradient_Shader::kDitherStride32;
} while (--count != 0);
}
}
void Linear_Gradient::shadeSpan(int x, int y, SkPMColor* SK_RESTRICT dstC,
int count) {
SkASSERT(count > 0);
SkPoint srcPt;
SkMatrix::MapXYProc dstProc = fDstToIndexProc;
TileProc proc = fTileProc;
const SkPMColor* SK_RESTRICT cache = this->getCache32();
#ifdef USE_DITHER_32BIT_GRADIENT
int toggle = ((x ^ y) & 1) * kDitherStride32;
#else
int toggle = 0;
#endif
if (fDstToIndexClass != kPerspective_MatrixClass) {
dstProc(fDstToIndex, SkIntToScalar(x) + SK_ScalarHalf,
SkIntToScalar(y) + SK_ScalarHalf, &srcPt);
SkFixed dx, fx = SkScalarToFixed(srcPt.fX);
if (fDstToIndexClass == kFixedStepInX_MatrixClass) {
SkFixed dxStorage[1];
(void)fDstToIndex.fixedStepInX(SkIntToScalar(y), dxStorage, NULL);
dx = dxStorage[0];
} else {
SkASSERT(fDstToIndexClass == kLinear_MatrixClass);
dx = SkScalarToFixed(fDstToIndex.getScaleX());
}
LinearShadeProc shadeProc = shadeSpan_linear_repeat;
if (SkFixedNearlyZero(dx)) {
#ifdef SK_SIMPLE_TWOCOLOR_VERTICAL_GRADIENTS
if (fColorCount > 2) {
shadeProc = shadeSpan_linear_vertical_lerp;
} else {
shadeProc = shadeSpan_linear_vertical;
}
#else
shadeProc = shadeSpan_linear_vertical_lerp;
#endif
} else if (proc == clamp_tileproc) {
shadeProc = shadeSpan_linear_clamp;
} else if (proc == mirror_tileproc) {
shadeProc = shadeSpan_linear_mirror;
} else {
SkASSERT(proc == repeat_tileproc);
}
(*shadeProc)(proc, dx, fx, dstC, cache, toggle, count);
} else {
SkScalar dstX = SkIntToScalar(x);
SkScalar dstY = SkIntToScalar(y);
do {
dstProc(fDstToIndex, dstX, dstY, &srcPt);
unsigned fi = proc(SkScalarToFixed(srcPt.fX));
SkASSERT(fi <= 0xFFFF);
*dstC++ = cache[toggle + (fi >> kCache32Shift)];
toggle ^= Gradient_Shader::kDitherStride32;
dstX += SK_Scalar1;
} while (--count != 0);
}
}
SkShader::BitmapType Linear_Gradient::asABitmap(SkBitmap* bitmap,
SkMatrix* matrix,
TileMode xy[],
SkScalar* twoPointRadialParams) const {
if (bitmap) {
this->commonAsABitmap(bitmap);
}
if (matrix) {
matrix->setScale(SkIntToScalar(kGradient32Length), SK_Scalar1);
matrix->preConcat(fPtsToUnit);
}
if (xy) {
xy[0] = fTileMode;
xy[1] = kClamp_TileMode;
}
return kDefault_BitmapType;
}
SkShader::GradientType Linear_Gradient::asAGradient(GradientInfo* info) const {
if (info) {
commonAsAGradient(info);
info->fPoint[0] = fStart;
info->fPoint[1] = fEnd;
}
return kLinear_GradientType;
}
static void dither_memset16(uint16_t dst[], uint16_t value, uint16_t other,
int count) {
if (reinterpret_cast<uintptr_t>(dst) & 2) {
*dst++ = value;
count -= 1;
SkTSwap(value, other);
}
sk_memset32((uint32_t*)dst, (value << 16) | other, count >> 1);
if (count & 1) {
dst[count - 1] = value;
}
}
#define NO_CHECK_ITER_16 \
do { \
unsigned fi = fx >> Gradient_Shader::kCache16Shift; \
SkASSERT(fi < Gradient_Shader::kCache16Count); \
fx += dx; \
*dstC++ = cache[toggle + fi]; \
toggle ^= Gradient_Shader::kDitherStride16; \
} while (0)
namespace {
typedef void (*LinearShade16Proc)(TileProc proc, SkFixed dx, SkFixed fx,
uint16_t* dstC, const uint16_t* cache,
int toggle, int count);
void shadeSpan16_linear_vertical(TileProc proc, SkFixed dx, SkFixed fx,
uint16_t* SK_RESTRICT dstC,
const uint16_t* SK_RESTRICT cache,
int toggle, int count) {
// we're a vertical gradient, so no change in a span
unsigned fi = proc(fx) >> Gradient_Shader::kCache16Shift;
SkASSERT(fi < Gradient_Shader::kCache16Count);
dither_memset16(dstC, cache[toggle + fi],
cache[(toggle ^ Gradient_Shader::kDitherStride16) + fi], count);
}
void shadeSpan16_linear_clamp(TileProc proc, SkFixed dx, SkFixed fx,
uint16_t* SK_RESTRICT dstC,
const uint16_t* SK_RESTRICT cache,
int toggle, int count) {
SkClampRange range;
range.init(fx, dx, count, 0, Gradient_Shader::kGradient16Length);
if ((count = range.fCount0) > 0) {
dither_memset16(dstC,
cache[toggle + range.fV0],
cache[(toggle ^ Gradient_Shader::kDitherStride16) + range.fV0],
count);
dstC += count;
}
if ((count = range.fCount1) > 0) {
int unroll = count >> 3;
fx = range.fFx1;
for (int i = 0; i < unroll; i++) {
NO_CHECK_ITER_16; NO_CHECK_ITER_16;
NO_CHECK_ITER_16; NO_CHECK_ITER_16;
NO_CHECK_ITER_16; NO_CHECK_ITER_16;
NO_CHECK_ITER_16; NO_CHECK_ITER_16;
}
if ((count &= 7) > 0) {
do {
NO_CHECK_ITER_16;
} while (--count != 0);
}
}
if ((count = range.fCount2) > 0) {
dither_memset16(dstC,
cache[toggle + range.fV1],
cache[(toggle ^ Gradient_Shader::kDitherStride16) + range.fV1],
count);
}
}
void shadeSpan16_linear_mirror(TileProc proc, SkFixed dx, SkFixed fx,
uint16_t* SK_RESTRICT dstC,
const uint16_t* SK_RESTRICT cache,
int toggle, int count) {
do {
unsigned fi = mirror_bits(fx >> Gradient_Shader::kCache16Shift,
Gradient_Shader::kCache16Bits);
SkASSERT(fi < Gradient_Shader::kCache16Count);
fx += dx;
*dstC++ = cache[toggle + fi];
toggle ^= Gradient_Shader::kDitherStride16;
} while (--count != 0);
}
void shadeSpan16_linear_repeat(TileProc proc, SkFixed dx, SkFixed fx,
uint16_t* SK_RESTRICT dstC,
const uint16_t* SK_RESTRICT cache,
int toggle, int count) {
SkASSERT(proc == repeat_tileproc);
do {
unsigned fi = repeat_bits(fx >> Gradient_Shader::kCache16Shift,
Gradient_Shader::kCache16Bits);
SkASSERT(fi < Gradient_Shader::kCache16Count);
fx += dx;
*dstC++ = cache[toggle + fi];
toggle ^= Gradient_Shader::kDitherStride16;
} while (--count != 0);
}
}
void Linear_Gradient::shadeSpan16(int x, int y,
uint16_t* SK_RESTRICT dstC, int count) {
SkASSERT(count > 0);
SkPoint srcPt;
SkMatrix::MapXYProc dstProc = fDstToIndexProc;
TileProc proc = fTileProc;
const uint16_t* SK_RESTRICT cache = this->getCache16();
int toggle = ((x ^ y) & 1) * kDitherStride16;
if (fDstToIndexClass != kPerspective_MatrixClass) {
dstProc(fDstToIndex, SkIntToScalar(x) + SK_ScalarHalf,
SkIntToScalar(y) + SK_ScalarHalf, &srcPt);
SkFixed dx, fx = SkScalarToFixed(srcPt.fX);
if (fDstToIndexClass == kFixedStepInX_MatrixClass) {
SkFixed dxStorage[1];
(void)fDstToIndex.fixedStepInX(SkIntToScalar(y), dxStorage, NULL);
dx = dxStorage[0];
} else {
SkASSERT(fDstToIndexClass == kLinear_MatrixClass);
dx = SkScalarToFixed(fDstToIndex.getScaleX());
}
LinearShade16Proc shadeProc = shadeSpan16_linear_repeat;
if (SkFixedNearlyZero(dx)) {
shadeProc = shadeSpan16_linear_vertical;
} else if (proc == clamp_tileproc) {
shadeProc = shadeSpan16_linear_clamp;
} else if (proc == mirror_tileproc) {
shadeProc = shadeSpan16_linear_mirror;
} else {
SkASSERT(proc == repeat_tileproc);
}
(*shadeProc)(proc, dx, fx, dstC, cache, toggle, count);
} else {
SkScalar dstX = SkIntToScalar(x);
SkScalar dstY = SkIntToScalar(y);
do {
dstProc(fDstToIndex, dstX, dstY, &srcPt);
unsigned fi = proc(SkScalarToFixed(srcPt.fX));
SkASSERT(fi <= 0xFFFF);
int index = fi >> kCache16Shift;
*dstC++ = cache[toggle + index];
toggle ^= Gradient_Shader::kDitherStride16;
dstX += SK_Scalar1;
} while (--count != 0);
}
}
///////////////////////////////////////////////////////////////////////////////
#define kSQRT_TABLE_BITS 11
#define kSQRT_TABLE_SIZE (1 << kSQRT_TABLE_BITS)
#include "SkRadialGradient_Table.h"
#if defined(SK_BUILD_FOR_WIN32) && defined(SK_DEBUG)
#include <stdio.h>
void SkRadialGradient_BuildTable() {
// build it 0..127 x 0..127, so we use 2^15 - 1 in the numerator for our "fixed" table
FILE* file = ::fopen("SkRadialGradient_Table.h", "w");
SkASSERT(file);
::fprintf(file, "static const uint8_t gSqrt8Table[] = {\n");
for (int i = 0; i < kSQRT_TABLE_SIZE; i++) {
if ((i & 15) == 0) {
::fprintf(file, "\t");
}
uint8_t value = SkToU8(SkFixedSqrt(i * SK_Fixed1 / kSQRT_TABLE_SIZE) >> 8);
::fprintf(file, "0x%02X", value);
if (i < kSQRT_TABLE_SIZE-1) {
::fprintf(file, ", ");
}
if ((i & 15) == 15) {
::fprintf(file, "\n");
}
}
::fprintf(file, "};\n");
::fclose(file);
}
#endif
static void rad_to_unit_matrix(const SkPoint& center, SkScalar radius,
SkMatrix* matrix) {
SkScalar inv = SkScalarInvert(radius);
matrix->setTranslate(-center.fX, -center.fY);
matrix->postScale(inv, inv);
}
namespace {
typedef void (* RadialShade16Proc)(SkScalar sfx, SkScalar sdx,
SkScalar sfy, SkScalar sdy,
uint16_t* dstC, const uint16_t* cache,
int toggle, int count);
void shadeSpan16_radial_clamp(SkScalar sfx, SkScalar sdx,
SkScalar sfy, SkScalar sdy,
uint16_t* SK_RESTRICT dstC, const uint16_t* SK_RESTRICT cache,
int toggle, int count) {
const uint8_t* SK_RESTRICT sqrt_table = gSqrt8Table;
/* knock these down so we can pin against +- 0x7FFF, which is an
immediate load, rather than 0xFFFF which is slower. This is a
compromise, since it reduces our precision, but that appears
to be visually OK. If we decide this is OK for all of our cases,
we could (it seems) put this scale-down into fDstToIndex,
to avoid having to do these extra shifts each time.
*/
SkFixed fx = SkScalarToFixed(sfx) >> 1;
SkFixed dx = SkScalarToFixed(sdx) >> 1;
SkFixed fy = SkScalarToFixed(sfy) >> 1;
SkFixed dy = SkScalarToFixed(sdy) >> 1;
// might perform this check for the other modes,
// but the win will be a smaller % of the total
if (dy == 0) {
fy = SkPin32(fy, -0xFFFF >> 1, 0xFFFF >> 1);
fy *= fy;
do {
unsigned xx = SkPin32(fx, -0xFFFF >> 1, 0xFFFF >> 1);
unsigned fi = (xx * xx + fy) >> (14 + 16 - kSQRT_TABLE_BITS);
fi = SkFastMin32(fi, 0xFFFF >> (16 - kSQRT_TABLE_BITS));
fx += dx;
*dstC++ = cache[toggle +
(sqrt_table[fi] >> Gradient_Shader::kSqrt16Shift)];
toggle ^= Gradient_Shader::kDitherStride16;
} while (--count != 0);
} else {
do {
unsigned xx = SkPin32(fx, -0xFFFF >> 1, 0xFFFF >> 1);
unsigned fi = SkPin32(fy, -0xFFFF >> 1, 0xFFFF >> 1);
fi = (xx * xx + fi * fi) >> (14 + 16 - kSQRT_TABLE_BITS);
fi = SkFastMin32(fi, 0xFFFF >> (16 - kSQRT_TABLE_BITS));
fx += dx;
fy += dy;
*dstC++ = cache[toggle +
(sqrt_table[fi] >> Gradient_Shader::kSqrt16Shift)];
toggle ^= Gradient_Shader::kDitherStride16;
} while (--count != 0);
}
}
void shadeSpan16_radial_mirror(SkScalar sfx, SkScalar sdx,
SkScalar sfy, SkScalar sdy,
uint16_t* SK_RESTRICT dstC, const uint16_t* SK_RESTRICT cache,
int toggle, int count) {
do {
#ifdef SK_SCALAR_IS_FLOAT
float fdist = sk_float_sqrt(sfx*sfx + sfy*sfy);
SkFixed dist = SkFloatToFixed(fdist);
#else
SkFixed magnitudeSquared = SkFixedSquare(sfx) +
SkFixedSquare(sfy);
if (magnitudeSquared < 0) // Overflow.
magnitudeSquared = SK_FixedMax;
SkFixed dist = SkFixedSqrt(magnitudeSquared);
#endif
unsigned fi = mirror_tileproc(dist);
SkASSERT(fi <= 0xFFFF);
*dstC++ = cache[toggle + (fi >> Gradient_Shader::kCache16Shift)];
toggle ^= Gradient_Shader::kDitherStride16;
sfx += sdx;
sfy += sdy;
} while (--count != 0);
}
void shadeSpan16_radial_repeat(SkScalar sfx, SkScalar sdx,
SkScalar sfy, SkScalar sdy,
uint16_t* SK_RESTRICT dstC, const uint16_t* SK_RESTRICT cache,
int toggle, int count) {
SkFixed fx = SkScalarToFixed(sfx);
SkFixed dx = SkScalarToFixed(sdx);
SkFixed fy = SkScalarToFixed(sfy);
SkFixed dy = SkScalarToFixed(sdy);
do {
SkFixed dist = SkFixedSqrt(SkFixedSquare(fx) + SkFixedSquare(fy));
unsigned fi = repeat_tileproc(dist);
SkASSERT(fi <= 0xFFFF);
fx += dx;
fy += dy;
*dstC++ = cache[toggle + (fi >> Gradient_Shader::kCache16Shift)];
toggle ^= Gradient_Shader::kDitherStride16;
} while (--count != 0);
}
}
class Radial_Gradient : public Gradient_Shader {
public:
Radial_Gradient(const SkPoint& center, SkScalar radius,
const SkColor colors[], const SkScalar pos[], int colorCount,
SkShader::TileMode mode, SkUnitMapper* mapper)
: Gradient_Shader(colors, pos, colorCount, mode, mapper),
fCenter(center),
fRadius(radius)
{
// make sure our table is insync with our current #define for kSQRT_TABLE_SIZE
SkASSERT(sizeof(gSqrt8Table) == kSQRT_TABLE_SIZE);
rad_to_unit_matrix(center, radius, &fPtsToUnit);
}
virtual void shadeSpan(int x, int y, SkPMColor* dstC, int count)
SK_OVERRIDE;
virtual void shadeSpan16(int x, int y, uint16_t* dstCParam,
int count) SK_OVERRIDE {
SkASSERT(count > 0);
uint16_t* SK_RESTRICT dstC = dstCParam;
SkPoint srcPt;
SkMatrix::MapXYProc dstProc = fDstToIndexProc;
TileProc proc = fTileProc;
const uint16_t* SK_RESTRICT cache = this->getCache16();
int toggle = ((x ^ y) & 1) * kDitherStride16;
if (fDstToIndexClass != kPerspective_MatrixClass) {
dstProc(fDstToIndex, SkIntToScalar(x) + SK_ScalarHalf,
SkIntToScalar(y) + SK_ScalarHalf, &srcPt);
SkScalar sdx = fDstToIndex.getScaleX();
SkScalar sdy = fDstToIndex.getSkewY();
if (fDstToIndexClass == kFixedStepInX_MatrixClass) {
SkFixed storage[2];
(void)fDstToIndex.fixedStepInX(SkIntToScalar(y),
&storage[0], &storage[1]);
sdx = SkFixedToScalar(storage[0]);
sdy = SkFixedToScalar(storage[1]);
} else {
SkASSERT(fDstToIndexClass == kLinear_MatrixClass);
}
RadialShade16Proc shadeProc = shadeSpan16_radial_repeat;
if (proc == clamp_tileproc) {
shadeProc = shadeSpan16_radial_clamp;
} else if (proc == mirror_tileproc) {
shadeProc = shadeSpan16_radial_mirror;
} else {
SkASSERT(proc == repeat_tileproc);
}
(*shadeProc)(srcPt.fX, sdx, srcPt.fY, sdy, dstC,
cache, toggle, count);
} else { // perspective case
SkScalar dstX = SkIntToScalar(x);
SkScalar dstY = SkIntToScalar(y);
do {
dstProc(fDstToIndex, dstX, dstY, &srcPt);
unsigned fi = proc(SkScalarToFixed(srcPt.length()));
SkASSERT(fi <= 0xFFFF);
int index = fi >> (16 - kCache16Bits);
*dstC++ = cache[toggle + index];
toggle ^= kDitherStride16;
dstX += SK_Scalar1;
} while (--count != 0);
}
}
virtual BitmapType asABitmap(SkBitmap* bitmap,
SkMatrix* matrix,
TileMode* xy,
SkScalar* twoPointRadialParams)
const SK_OVERRIDE {
if (bitmap) {
this->commonAsABitmap(bitmap);
}
if (matrix) {
matrix->setScale(SkIntToScalar(kGradient32Length),
SkIntToScalar(kGradient32Length));
matrix->preConcat(fPtsToUnit);
}
if (xy) {
xy[0] = fTileMode;
xy[1] = kClamp_TileMode;
}
return kRadial_BitmapType;
}
virtual GradientType asAGradient(GradientInfo* info) const SK_OVERRIDE {
if (info) {
commonAsAGradient(info);
info->fPoint[0] = fCenter;
info->fRadius[0] = fRadius;
}
return kRadial_GradientType;
}
SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(Radial_Gradient)
protected:
Radial_Gradient(SkFlattenableReadBuffer& buffer)
: INHERITED(buffer),
fCenter(buffer.readPoint()),
fRadius(buffer.readScalar()) {
}
virtual void flatten(SkFlattenableWriteBuffer& buffer) const SK_OVERRIDE {
this->INHERITED::flatten(buffer);
buffer.writePoint(fCenter);
buffer.writeScalar(fRadius);
}
private:
typedef Gradient_Shader INHERITED;
const SkPoint fCenter;
const SkScalar fRadius;
};
namespace {
inline bool radial_completely_pinned(int fx, int dx, int fy, int dy) {
// fast, overly-conservative test: checks unit square instead
// of unit circle
bool xClamped = (fx >= SK_FixedHalf && dx >= 0) ||
(fx <= -SK_FixedHalf && dx <= 0);
bool yClamped = (fy >= SK_FixedHalf && dy >= 0) ||
(fy <= -SK_FixedHalf && dy <= 0);
return xClamped || yClamped;
}
// Return true if (fx * fy) is always inside the unit circle
// SkPin32 is expensive, but so are all the SkFixedMul in this test,
// so it shouldn't be run if count is small.
inline bool no_need_for_radial_pin(int fx, int dx,
int fy, int dy, int count) {
SkASSERT(count > 0);
if (SkAbs32(fx) > 0x7FFF || SkAbs32(fy) > 0x7FFF) {
return false;
}
if (fx*fx + fy*fy > 0x7FFF*0x7FFF) {
return false;
}
fx += (count - 1) * dx;
fy += (count - 1) * dy;
if (SkAbs32(fx) > 0x7FFF || SkAbs32(fy) > 0x7FFF) {
return false;
}
return fx*fx + fy*fy <= 0x7FFF*0x7FFF;
}
#define UNPINNED_RADIAL_STEP \
fi = (fx * fx + fy * fy) >> (14 + 16 - kSQRT_TABLE_BITS); \
*dstC++ = cache[toggle + \
(sqrt_table[fi] >> Gradient_Shader::kSqrt32Shift)]; \
toggle ^= Gradient_Shader::kDitherStride32; \
fx += dx; \
fy += dy;
typedef void (* RadialShadeProc)(SkScalar sfx, SkScalar sdx,
SkScalar sfy, SkScalar sdy,
SkPMColor* dstC, const SkPMColor* cache,
int count, int toggle);
// On Linux, this is faster with SkPMColor[] params than SkPMColor* SK_RESTRICT
void shadeSpan_radial_clamp(SkScalar sfx, SkScalar sdx,
SkScalar sfy, SkScalar sdy,
SkPMColor* SK_RESTRICT dstC, const SkPMColor* SK_RESTRICT cache,
int count, int toggle) {
// Floating point seems to be slower than fixed point,
// even when we have float hardware.
const uint8_t* SK_RESTRICT sqrt_table = gSqrt8Table;
SkFixed fx = SkScalarToFixed(sfx) >> 1;
SkFixed dx = SkScalarToFixed(sdx) >> 1;
SkFixed fy = SkScalarToFixed(sfy) >> 1;
SkFixed dy = SkScalarToFixed(sdy) >> 1;
if ((count > 4) && radial_completely_pinned(fx, dx, fy, dy)) {
unsigned fi = Gradient_Shader::kGradient32Length;
sk_memset32_dither(dstC,
cache[toggle + fi],
cache[(toggle ^ Gradient_Shader::kDitherStride32) + fi],
count);
} else if ((count > 4) &&
no_need_for_radial_pin(fx, dx, fy, dy, count)) {
unsigned fi;
// 4x unroll appears to be no faster than 2x unroll on Linux
while (count > 1) {
UNPINNED_RADIAL_STEP;
UNPINNED_RADIAL_STEP;
count -= 2;
}
if (count) {
UNPINNED_RADIAL_STEP;
}
}
else {
// Specializing for dy == 0 gains us 25% on Skia benchmarks
if (dy == 0) {
unsigned yy = SkPin32(fy, -0xFFFF >> 1, 0xFFFF >> 1);
yy *= yy;
do {
unsigned xx = SkPin32(fx, -0xFFFF >> 1, 0xFFFF >> 1);
unsigned fi = (xx * xx + yy) >> (14 + 16 - kSQRT_TABLE_BITS);
fi = SkFastMin32(fi, 0xFFFF >> (16 - kSQRT_TABLE_BITS));
*dstC++ = cache[toggle + (sqrt_table[fi] >>
Gradient_Shader::kSqrt32Shift)];
toggle ^= Gradient_Shader::kDitherStride32;
fx += dx;
} while (--count != 0);
} else {
do {
unsigned xx = SkPin32(fx, -0xFFFF >> 1, 0xFFFF >> 1);
unsigned fi = SkPin32(fy, -0xFFFF >> 1, 0xFFFF >> 1);
fi = (xx * xx + fi * fi) >> (14 + 16 - kSQRT_TABLE_BITS);
fi = SkFastMin32(fi, 0xFFFF >> (16 - kSQRT_TABLE_BITS));
*dstC++ = cache[toggle + (sqrt_table[fi] >>
Gradient_Shader::kSqrt32Shift)];
toggle ^= Gradient_Shader::kDitherStride32;
fx += dx;
fy += dy;
} while (--count != 0);
}
}
}
// Unrolling this loop doesn't seem to help (when float); we're stalling to
// get the results of the sqrt (?), and don't have enough extra registers to
// have many in flight.
void shadeSpan_radial_mirror(SkScalar sfx, SkScalar sdx,
SkScalar sfy, SkScalar sdy,
SkPMColor* SK_RESTRICT dstC, const SkPMColor* SK_RESTRICT cache,
int count, int toggle) {
do {
#ifdef SK_SCALAR_IS_FLOAT
float fdist = sk_float_sqrt(sfx*sfx + sfy*sfy);
SkFixed dist = SkFloatToFixed(fdist);
#else
SkFixed magnitudeSquared = SkFixedSquare(sfx) +
SkFixedSquare(sfy);
if (magnitudeSquared < 0) // Overflow.
magnitudeSquared = SK_FixedMax;
SkFixed dist = SkFixedSqrt(magnitudeSquared);
#endif
unsigned fi = mirror_tileproc(dist);
SkASSERT(fi <= 0xFFFF);
*dstC++ = cache[toggle + (fi >> Gradient_Shader::kCache32Shift)];
toggle ^= Gradient_Shader::kDitherStride32;
sfx += sdx;
sfy += sdy;
} while (--count != 0);
}
void shadeSpan_radial_repeat(SkScalar sfx, SkScalar sdx,
SkScalar sfy, SkScalar sdy,
SkPMColor* SK_RESTRICT dstC, const SkPMColor* SK_RESTRICT cache,
int count, int toggle) {
SkFixed fx = SkScalarToFixed(sfx);
SkFixed dx = SkScalarToFixed(sdx);
SkFixed fy = SkScalarToFixed(sfy);
SkFixed dy = SkScalarToFixed(sdy);
do {
SkFixed magnitudeSquared = SkFixedSquare(fx) +
SkFixedSquare(fy);
if (magnitudeSquared < 0) // Overflow.
magnitudeSquared = SK_FixedMax;
SkFixed dist = SkFixedSqrt(magnitudeSquared);
unsigned fi = repeat_tileproc(dist);
SkASSERT(fi <= 0xFFFF);
*dstC++ = cache[toggle + (fi >> Gradient_Shader::kCache32Shift)];
toggle ^= Gradient_Shader::kDitherStride32;
fx += dx;
fy += dy;
} while (--count != 0);
}
}
void Radial_Gradient::shadeSpan(int x, int y,
SkPMColor* SK_RESTRICT dstC, int count) {
SkASSERT(count > 0);
SkPoint srcPt;
SkMatrix::MapXYProc dstProc = fDstToIndexProc;
TileProc proc = fTileProc;
const SkPMColor* SK_RESTRICT cache = this->getCache32();
#ifdef USE_DITHER_32BIT_GRADIENT
int toggle = ((x ^ y) & 1) * Gradient_Shader::kDitherStride32;
#else
int toggle = 0;
#endif
if (fDstToIndexClass != kPerspective_MatrixClass) {
dstProc(fDstToIndex, SkIntToScalar(x) + SK_ScalarHalf,
SkIntToScalar(y) + SK_ScalarHalf, &srcPt);
SkScalar sdx = fDstToIndex.getScaleX();
SkScalar sdy = fDstToIndex.getSkewY();
if (fDstToIndexClass == kFixedStepInX_MatrixClass) {
SkFixed storage[2];
(void)fDstToIndex.fixedStepInX(SkIntToScalar(y),
&storage[0], &storage[1]);
sdx = SkFixedToScalar(storage[0]);
sdy = SkFixedToScalar(storage[1]);
} else {
SkASSERT(fDstToIndexClass == kLinear_MatrixClass);
}
RadialShadeProc shadeProc = shadeSpan_radial_repeat;
if (proc == clamp_tileproc) {
shadeProc = shadeSpan_radial_clamp;
} else if (proc == mirror_tileproc) {
shadeProc = shadeSpan_radial_mirror;
} else {
SkASSERT(proc == repeat_tileproc);
}
(*shadeProc)(srcPt.fX, sdx, srcPt.fY, sdy, dstC, cache, count, toggle);
} else { // perspective case
SkScalar dstX = SkIntToScalar(x);
SkScalar dstY = SkIntToScalar(y);
do {
dstProc(fDstToIndex, dstX, dstY, &srcPt);
unsigned fi = proc(SkScalarToFixed(srcPt.length()));
SkASSERT(fi <= 0xFFFF);
*dstC++ = cache[fi >> Gradient_Shader::kCache32Shift];
dstX += SK_Scalar1;
} while (--count != 0);
}
}
/* Two-point radial gradients are specified by two circles, each with a center
point and radius. The gradient can be considered to be a series of
concentric circles, with the color interpolated from the start circle
(at t=0) to the end circle (at t=1).
For each point (x, y) in the span, we want to find the
interpolated circle that intersects that point. The center
of the desired circle (Cx, Cy) falls at some distance t
along the line segment between the start point (Sx, Sy) and
end point (Ex, Ey):
Cx = (1 - t) * Sx + t * Ex (0 <= t <= 1)
Cy = (1 - t) * Sy + t * Ey
The radius of the desired circle (r) is also a linear interpolation t
between the start and end radii (Sr and Er):
r = (1 - t) * Sr + t * Er
But
(x - Cx)^2 + (y - Cy)^2 = r^2
so
(x - ((1 - t) * Sx + t * Ex))^2
+ (y - ((1 - t) * Sy + t * Ey))^2
= ((1 - t) * Sr + t * Er)^2
Solving for t yields
[(Sx - Ex)^2 + (Sy - Ey)^2 - (Er - Sr)^2)] * t^2
+ [2 * (Sx - Ex)(x - Sx) + 2 * (Sy - Ey)(y - Sy) - 2 * (Er - Sr) * Sr] * t
+ [(x - Sx)^2 + (y - Sy)^2 - Sr^2] = 0
To simplify, let Dx = Sx - Ex, Dy = Sy - Ey, Dr = Er - Sr, dx = x - Sx, dy = y - Sy
[Dx^2 + Dy^2 - Dr^2)] * t^2
+ 2 * [Dx * dx + Dy * dy - Dr * Sr] * t
+ [dx^2 + dy^2 - Sr^2] = 0
A quadratic in t. The two roots of the quadratic reflect the two
possible circles on which the point may fall. Solving for t yields
the gradient value to use.
If a<0, the start circle is entirely contained in the
end circle, and one of the roots will be <0 or >1 (off the line
segment). If a>0, the start circle falls at least partially
outside the end circle (or vice versa), and the gradient
defines a "tube" where a point may be on one circle (on the
inside of the tube) or the other (outside of the tube). We choose
one arbitrarily.
In order to keep the math to within the limits of fixed point,
we divide the entire quadratic by Dr^2, and replace
(x - Sx)/Dr with x' and (y - Sy)/Dr with y', giving
[Dx^2 / Dr^2 + Dy^2 / Dr^2 - 1)] * t^2
+ 2 * [x' * Dx / Dr + y' * Dy / Dr - Sr / Dr] * t
+ [x'^2 + y'^2 - Sr^2/Dr^2] = 0
(x' and y' are computed by appending the subtract and scale to the
fDstToIndex matrix in the constructor).
Since the 'A' component of the quadratic is independent of x' and y', it
is precomputed in the constructor. Since the 'B' component is linear in
x' and y', if x and y are linear in the span, 'B' can be computed
incrementally with a simple delta (db below). If it is not (e.g.,
a perspective projection), it must be computed in the loop.
*/
namespace {
inline SkFixed two_point_radial(SkScalar b, SkScalar fx, SkScalar fy,
SkScalar sr2d2, SkScalar foura,
SkScalar oneOverTwoA, bool posRoot) {
SkScalar c = SkScalarSquare(fx) + SkScalarSquare(fy) - sr2d2;
if (0 == foura) {
return SkScalarToFixed(SkScalarDiv(-c, b));
}
SkScalar discrim = SkScalarSquare(b) - SkScalarMul(foura, c);
if (discrim < 0) {
discrim = -discrim;
}
SkScalar rootDiscrim = SkScalarSqrt(discrim);
SkScalar result;
if (posRoot) {
result = SkScalarMul(-b + rootDiscrim, oneOverTwoA);
} else {
result = SkScalarMul(-b - rootDiscrim, oneOverTwoA);
}
return SkScalarToFixed(result);
}
typedef void (* TwoPointRadialShadeProc)(SkScalar fx, SkScalar dx,
SkScalar fy, SkScalar dy,
SkScalar b, SkScalar db,
SkScalar fSr2D2, SkScalar foura, SkScalar fOneOverTwoA, bool posRoot,
SkPMColor* SK_RESTRICT dstC, const SkPMColor* SK_RESTRICT cache,
int count);
void shadeSpan_twopoint_clamp(SkScalar fx, SkScalar dx,
SkScalar fy, SkScalar dy,
SkScalar b, SkScalar db,
SkScalar fSr2D2, SkScalar foura, SkScalar fOneOverTwoA, bool posRoot,
SkPMColor* SK_RESTRICT dstC, const SkPMColor* SK_RESTRICT cache,
int count) {
for (; count > 0; --count) {
SkFixed t = two_point_radial(b, fx, fy, fSr2D2, foura,
fOneOverTwoA, posRoot);
SkFixed index = SkClampMax(t, 0xFFFF);
SkASSERT(index <= 0xFFFF);
*dstC++ = cache[index >> Gradient_Shader::kCache32Shift];
fx += dx;
fy += dy;
b += db;
}
}
void shadeSpan_twopoint_mirror(SkScalar fx, SkScalar dx,
SkScalar fy, SkScalar dy,
SkScalar b, SkScalar db,
SkScalar fSr2D2, SkScalar foura, SkScalar fOneOverTwoA, bool posRoot,
SkPMColor* SK_RESTRICT dstC, const SkPMColor* SK_RESTRICT cache,
int count) {
for (; count > 0; --count) {
SkFixed t = two_point_radial(b, fx, fy, fSr2D2, foura,
fOneOverTwoA, posRoot);
SkFixed index = mirror_tileproc(t);
SkASSERT(index <= 0xFFFF);
*dstC++ = cache[index >> Gradient_Shader::kCache32Shift];
fx += dx;
fy += dy;
b += db;
}
}
void shadeSpan_twopoint_repeat(SkScalar fx, SkScalar dx,
SkScalar fy, SkScalar dy,
SkScalar b, SkScalar db,
SkScalar fSr2D2, SkScalar foura, SkScalar fOneOverTwoA, bool posRoot,
SkPMColor* SK_RESTRICT dstC, const SkPMColor* SK_RESTRICT cache,
int count) {
for (; count > 0; --count) {
SkFixed t = two_point_radial(b, fx, fy, fSr2D2, foura,
fOneOverTwoA, posRoot);
SkFixed index = repeat_tileproc(t);
SkASSERT(index <= 0xFFFF);
*dstC++ = cache[index >> Gradient_Shader::kCache32Shift];
fx += dx;
fy += dy;
b += db;
}
}
}
class Two_Point_Radial_Gradient : public Gradient_Shader {
public:
Two_Point_Radial_Gradient(const SkPoint& start, SkScalar startRadius,
const SkPoint& end, SkScalar endRadius,
const SkColor colors[], const SkScalar pos[],
int colorCount, SkShader::TileMode mode,
SkUnitMapper* mapper)
: Gradient_Shader(colors, pos, colorCount, mode, mapper),
fCenter1(start),
fCenter2(end),
fRadius1(startRadius),
fRadius2(endRadius) {
init();
}
virtual BitmapType asABitmap(SkBitmap* bitmap,
SkMatrix* matrix,
TileMode* xy,
SkScalar* twoPointRadialParams) const {
if (bitmap) {
this->commonAsABitmap(bitmap);
}
SkScalar diffL = 0; // just to avoid gcc warning
if (matrix || twoPointRadialParams) {
diffL = SkScalarSqrt(SkScalarSquare(fDiff.fX) +
SkScalarSquare(fDiff.fY));
}
if (matrix) {
if (diffL) {
SkScalar invDiffL = SkScalarInvert(diffL);
matrix->setSinCos(-SkScalarMul(invDiffL, fDiff.fY),
SkScalarMul(invDiffL, fDiff.fX));
} else {
matrix->reset();
}
matrix->preConcat(fPtsToUnit);
}
if (xy) {
xy[0] = fTileMode;
xy[1] = kClamp_TileMode;
}
if (NULL != twoPointRadialParams) {
twoPointRadialParams[0] = diffL;
twoPointRadialParams[1] = fStartRadius;
twoPointRadialParams[2] = fDiffRadius;
}
return kTwoPointRadial_BitmapType;
}
virtual GradientType asAGradient(GradientInfo* info) const SK_OVERRIDE {
if (info) {
commonAsAGradient(info);
info->fPoint[0] = fCenter1;
info->fPoint[1] = fCenter2;
info->fRadius[0] = fRadius1;
info->fRadius[1] = fRadius2;
}
return kRadial2_GradientType;
}
virtual void shadeSpan(int x, int y, SkPMColor* dstCParam,
int count) SK_OVERRIDE {
SkASSERT(count > 0);
SkPMColor* SK_RESTRICT dstC = dstCParam;
// Zero difference between radii: fill with transparent black.
if (fDiffRadius == 0) {
sk_bzero(dstC, count * sizeof(*dstC));
return;
}
SkMatrix::MapXYProc dstProc = fDstToIndexProc;
TileProc proc = fTileProc;
const SkPMColor* SK_RESTRICT cache = this->getCache32();
SkScalar foura = fA * 4;
bool posRoot = fDiffRadius < 0;
if (fDstToIndexClass != kPerspective_MatrixClass) {
SkPoint srcPt;
dstProc(fDstToIndex, SkIntToScalar(x) + SK_ScalarHalf,
SkIntToScalar(y) + SK_ScalarHalf, &srcPt);
SkScalar dx, fx = srcPt.fX;
SkScalar dy, fy = srcPt.fY;
if (fDstToIndexClass == kFixedStepInX_MatrixClass) {
SkFixed fixedX, fixedY;
(void)fDstToIndex.fixedStepInX(SkIntToScalar(y), &fixedX, &fixedY);
dx = SkFixedToScalar(fixedX);
dy = SkFixedToScalar(fixedY);
} else {
SkASSERT(fDstToIndexClass == kLinear_MatrixClass);
dx = fDstToIndex.getScaleX();
dy = fDstToIndex.getSkewY();
}
SkScalar b = (SkScalarMul(fDiff.fX, fx) +
SkScalarMul(fDiff.fY, fy) - fStartRadius) * 2;
SkScalar db = (SkScalarMul(fDiff.fX, dx) +
SkScalarMul(fDiff.fY, dy)) * 2;
TwoPointRadialShadeProc shadeProc = shadeSpan_twopoint_repeat;
if (proc == clamp_tileproc) {
shadeProc = shadeSpan_twopoint_clamp;
} else if (proc == mirror_tileproc) {
shadeProc = shadeSpan_twopoint_mirror;
} else {
SkASSERT(proc == repeat_tileproc);
}
(*shadeProc)(fx, dx, fy, dy, b, db,
fSr2D2, foura, fOneOverTwoA, posRoot,
dstC, cache, count);
} else { // perspective case
SkScalar dstX = SkIntToScalar(x);
SkScalar dstY = SkIntToScalar(y);
for (; count > 0; --count) {
SkPoint srcPt;
dstProc(fDstToIndex, dstX, dstY, &srcPt);
SkScalar fx = srcPt.fX;
SkScalar fy = srcPt.fY;
SkScalar b = (SkScalarMul(fDiff.fX, fx) +
SkScalarMul(fDiff.fY, fy) - fStartRadius) * 2;
SkFixed t = two_point_radial(b, fx, fy, fSr2D2, foura,
fOneOverTwoA, posRoot);
SkFixed index = proc(t);
SkASSERT(index <= 0xFFFF);
*dstC++ = cache[index >> Gradient_Shader::kCache32Shift];
dstX += SK_Scalar1;
}
}
}
virtual bool setContext(const SkBitmap& device,
const SkPaint& paint,
const SkMatrix& matrix) SK_OVERRIDE {
if (!this->INHERITED::setContext(device, paint, matrix)) {
return false;
}
// For now, we might have divided by zero, so detect that
if (0 == fDiffRadius) {
return false;
}
// we don't have a span16 proc
fFlags &= ~kHasSpan16_Flag;
return true;
}
SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(Two_Point_Radial_Gradient)
protected:
Two_Point_Radial_Gradient(SkFlattenableReadBuffer& buffer)
: INHERITED(buffer),
fCenter1(buffer.readPoint()),
fCenter2(buffer.readPoint()),
fRadius1(buffer.readScalar()),
fRadius2(buffer.readScalar()) {
init();
};
virtual void flatten(SkFlattenableWriteBuffer& buffer) const SK_OVERRIDE {
this->INHERITED::flatten(buffer);
buffer.writePoint(fCenter1);
buffer.writePoint(fCenter2);
buffer.writeScalar(fRadius1);
buffer.writeScalar(fRadius2);
}
private:
typedef Gradient_Shader INHERITED;
const SkPoint fCenter1;
const SkPoint fCenter2;
const SkScalar fRadius1;
const SkScalar fRadius2;
SkPoint fDiff;
SkScalar fStartRadius, fDiffRadius, fSr2D2, fA, fOneOverTwoA;
void init() {
fDiff = fCenter1 - fCenter2;
fDiffRadius = fRadius2 - fRadius1;
// hack to avoid zero-divide for now
SkScalar inv = fDiffRadius ? SkScalarInvert(fDiffRadius) : 0;
fDiff.fX = SkScalarMul(fDiff.fX, inv);
fDiff.fY = SkScalarMul(fDiff.fY, inv);
fStartRadius = SkScalarMul(fRadius1, inv);
fSr2D2 = SkScalarSquare(fStartRadius);
fA = SkScalarSquare(fDiff.fX) + SkScalarSquare(fDiff.fY) - SK_Scalar1;
fOneOverTwoA = fA ? SkScalarInvert(fA * 2) : 0;
fPtsToUnit.setTranslate(-fCenter1.fX, -fCenter1.fY);
fPtsToUnit.postScale(inv, inv);
}
};
///////////////////////////////////////////////////////////////////////////////
static int valid_divide(float numer, float denom, float* ratio) {
SkASSERT(ratio);
if (0 == denom) {
return 0;
}
*ratio = numer / denom;
return 1;
}
// Return the number of distinct real roots, and write them into roots[] in
// ascending order
static int find_quad_roots(float A, float B, float C, float roots[2]) {
SkASSERT(roots);
if (A == 0) {
return valid_divide(-C, B, roots);
}
float* r = roots;
float R = B*B - 4*A*C;
if (R < 0 || sk_float_isnan(R)) { // complex roots
return 0;
}
R = sk_float_sqrt(R);
float Q = (B < 0) ? -(B-R)/2 : -(B+R)/2;
r += valid_divide(Q, A, r);
r += valid_divide(C, Q, r);
if (r - roots == 2) {
if (roots[0] > roots[1]) {
SkTSwap<float>(roots[0], roots[1]);
} else if (roots[0] == roots[1]) { // nearly-equal?
r -= 1; // skip the double root
}
}
return (int)(r - roots);
}
static float lerp(float x, float dx, float t) {
return x + t * dx;
}
static float sqr(float x) { return x * x; }
struct TwoPtRadial {
enum {
kDontDrawT = 0x80000000
};
float fCenterX, fCenterY;
float fDCenterX, fDCenterY;
float fRadius;
float fDRadius;
float fA;
float fRadius2;
float fRDR;
void init(const SkPoint& center0, SkScalar rad0,
const SkPoint& center1, SkScalar rad1) {
fCenterX = SkScalarToFloat(center0.fX);
fCenterY = SkScalarToFloat(center0.fY);
fDCenterX = SkScalarToFloat(center1.fX) - fCenterX;
fDCenterY = SkScalarToFloat(center1.fY) - fCenterY;
fRadius = SkScalarToFloat(rad0);
fDRadius = SkScalarToFloat(rad1) - fRadius;
fA = sqr(fDCenterX) - sqr(fDCenterY) - sqr(fDRadius);
fRadius2 = sqr(fRadius);
fRDR = fRadius * fDRadius;
}
// used by setup and nextT
float fRelX, fRelY, fIncX, fIncY;
float fB, fDB;
void setup(SkScalar fx, SkScalar fy, SkScalar dfx, SkScalar dfy) {
fRelX = SkScalarToFloat(fx) - fCenterX;
fRelY = SkScalarToFloat(fy) - fCenterY;
fIncX = SkScalarToFloat(dfx);
fIncY = SkScalarToFloat(dfy);
fB = -2 * (fDCenterX * fRelX + fDCenterY * fRelY + fRDR);
fDB = -2 * (fDCenterX * fIncX + fDCenterY * fIncY);
}
SkFixed nextT() {
float roots[2];
float dx = fRelX;
float dy = fRelY;
float C = sqr(dx) + sqr(dy) - fRadius2;
int countRoots = find_quad_roots(fA, fB, C, roots);
fRelX += fIncX;
fRelY += fIncY;
fB += fDB;
if (0 == countRoots) {
return kDontDrawT;
}
// Prefer the bigger t value if both give a radius(t) > 0
// find_quad_roots returns the values sorted, so we start with the last
float t = roots[countRoots - 1];
float r = lerp(fRadius, fDRadius, t);
if (r <= 0) {
t = roots[0]; // might be the same as roots[countRoots-1]
r = lerp(fRadius, fDRadius, t);
if (r <= 0) {
return kDontDrawT;
}
}
return SkFloatToFixed(t);
}
static bool DontDrawT(SkFixed t) {
return kDontDrawT == t;
}
};
typedef void (*TwoPointRadialProc)(TwoPtRadial* rec, SkPMColor* dstC,
const SkPMColor* cache, int count);
void twopoint_clamp(TwoPtRadial* rec, SkPMColor* SK_RESTRICT dstC,
const SkPMColor* SK_RESTRICT cache, int count) {
for (; count > 0; --count) {
SkFixed t = rec->nextT();
if (TwoPtRadial::DontDrawT(t)) {
*dstC++ = 0;
} else {
SkFixed index = SkClampMax(t, 0xFFFF);
SkASSERT(index <= 0xFFFF);
*dstC++ = cache[index >> Gradient_Shader::kCache32Shift];
}
}
}
void twopoint_repeat(TwoPtRadial* rec, SkPMColor* SK_RESTRICT dstC,
const SkPMColor* SK_RESTRICT cache, int count) {
for (; count > 0; --count) {
SkFixed t = rec->nextT();
if (TwoPtRadial::DontDrawT(t)) {
*dstC++ = 0;
} else {
SkFixed index = repeat_tileproc(t);
SkASSERT(index <= 0xFFFF);
*dstC++ = cache[index >> Gradient_Shader::kCache32Shift];
}
}
}
void twopoint_mirror(TwoPtRadial* rec, SkPMColor* SK_RESTRICT dstC,
const SkPMColor* SK_RESTRICT cache, int count) {
for (; count > 0; --count) {
SkFixed t = rec->nextT();
if (TwoPtRadial::DontDrawT(t)) {
*dstC++ = 0;
} else {
SkFixed index = mirror_tileproc(t);
SkASSERT(index <= 0xFFFF);
*dstC++ = cache[index >> Gradient_Shader::kCache32Shift];
}
}
}
class Two_Point_Conical_Gradient : public Gradient_Shader {
TwoPtRadial fRec;
void init() {
fRec.init(fCenter1, fRadius1, fCenter2, fRadius2);
fPtsToUnit.reset();
}
public:
Two_Point_Conical_Gradient(const SkPoint& start, SkScalar startRadius,
const SkPoint& end, SkScalar endRadius,
const SkColor colors[], const SkScalar pos[],
int colorCount, SkShader::TileMode mode,
SkUnitMapper* mapper)
: Gradient_Shader(colors, pos, colorCount, mode, mapper),
fCenter1(start),
fCenter2(end),
fRadius1(startRadius),
fRadius2(endRadius) {
// this is degenerate, and should be caught by our caller
SkASSERT(fCenter1 != fCenter2 || fRadius1 != fRadius2);
this->init();
}
virtual void shadeSpan(int x, int y, SkPMColor* dstCParam,
int count) SK_OVERRIDE {
SkASSERT(count > 0);
SkPMColor* SK_RESTRICT dstC = dstCParam;
SkMatrix::MapXYProc dstProc = fDstToIndexProc;
TileProc proc = fTileProc;
const SkPMColor* SK_RESTRICT cache = this->getCache32();
TwoPointRadialProc shadeProc = twopoint_repeat;
if (proc == clamp_tileproc) {
shadeProc = twopoint_clamp;
} else if (proc == mirror_tileproc) {
shadeProc = twopoint_mirror;
} else {
SkASSERT(proc == repeat_tileproc);
}
if (fDstToIndexClass != kPerspective_MatrixClass) {
SkPoint srcPt;
dstProc(fDstToIndex, SkIntToScalar(x) + SK_ScalarHalf,
SkIntToScalar(y) + SK_ScalarHalf, &srcPt);
SkScalar dx, fx = srcPt.fX;
SkScalar dy, fy = srcPt.fY;
if (fDstToIndexClass == kFixedStepInX_MatrixClass) {
SkFixed fixedX, fixedY;
(void)fDstToIndex.fixedStepInX(SkIntToScalar(y), &fixedX, &fixedY);
dx = SkFixedToScalar(fixedX);
dy = SkFixedToScalar(fixedY);
} else {
SkASSERT(fDstToIndexClass == kLinear_MatrixClass);
dx = fDstToIndex.getScaleX();
dy = fDstToIndex.getSkewY();
}
fRec.setup(fx, fy, dx, dy);
(*shadeProc)(&fRec, dstC, cache, count);
} else { // perspective case
SkScalar dstX = SkIntToScalar(x);
SkScalar dstY = SkIntToScalar(y);
for (; count > 0; --count) {
SkPoint srcPt;
dstProc(fDstToIndex, dstX, dstY, &srcPt);
dstX += SK_Scalar1;
fRec.setup(srcPt.fX, srcPt.fY, 0, 0);
(*shadeProc)(&fRec, dstC, cache, 1);
}
}
}
virtual bool setContext(const SkBitmap& device,
const SkPaint& paint,
const SkMatrix& matrix) SK_OVERRIDE {
if (!this->INHERITED::setContext(device, paint, matrix)) {
return false;
}
// we don't have a span16 proc
fFlags &= ~kHasSpan16_Flag;
// in general, we might discard based on computed-radius, so clear
// this flag (todo: sometimes we can detect that we never discard...)
fFlags &= ~kOpaqueAlpha_Flag;
return true;
}
SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(Two_Point_Conical_Gradient)
protected:
Two_Point_Conical_Gradient(SkFlattenableReadBuffer& buffer)
: INHERITED(buffer),
fCenter1(buffer.readPoint()),
fCenter2(buffer.readPoint()),
fRadius1(buffer.readScalar()),
fRadius2(buffer.readScalar()) {
this->init();
};
virtual void flatten(SkFlattenableWriteBuffer& buffer) const SK_OVERRIDE {
this->INHERITED::flatten(buffer);
buffer.writePoint(fCenter1);
buffer.writePoint(fCenter2);
buffer.writeScalar(fRadius1);
buffer.writeScalar(fRadius2);
}
private:
typedef Gradient_Shader INHERITED;
const SkPoint fCenter1;
const SkPoint fCenter2;
const SkScalar fRadius1;
const SkScalar fRadius2;
};
///////////////////////////////////////////////////////////////////////////////
class Sweep_Gradient : public Gradient_Shader {
public:
Sweep_Gradient(SkScalar cx, SkScalar cy, const SkColor colors[],
const SkScalar pos[], int count, SkUnitMapper* mapper)
: Gradient_Shader(colors, pos, count, SkShader::kClamp_TileMode, mapper),
fCenter(SkPoint::Make(cx, cy))
{
fPtsToUnit.setTranslate(-cx, -cy);
}
virtual void shadeSpan(int x, int y, SkPMColor dstC[], int count) SK_OVERRIDE;
virtual void shadeSpan16(int x, int y, uint16_t dstC[], int count) SK_OVERRIDE;
virtual BitmapType asABitmap(SkBitmap* bitmap,
SkMatrix* matrix,
TileMode* xy,
SkScalar* twoPointRadialParams) const SK_OVERRIDE {
if (bitmap) {
this->commonAsABitmap(bitmap);
}
if (matrix) {
*matrix = fPtsToUnit;
}
if (xy) {
xy[0] = fTileMode;
xy[1] = kClamp_TileMode;
}
return kSweep_BitmapType;
}
virtual GradientType asAGradient(GradientInfo* info) const SK_OVERRIDE {
if (info) {
commonAsAGradient(info);
info->fPoint[0] = fCenter;
}
return kSweep_GradientType;
}
SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(Sweep_Gradient)
protected:
Sweep_Gradient(SkFlattenableReadBuffer& buffer)
: INHERITED(buffer),
fCenter(buffer.readPoint()) {
}
virtual void flatten(SkFlattenableWriteBuffer& buffer) const SK_OVERRIDE {
this->INHERITED::flatten(buffer);
buffer.writePoint(fCenter);
}
private:
typedef Gradient_Shader INHERITED;
const SkPoint fCenter;
};
#ifndef SK_SCALAR_IS_FLOAT
#ifdef COMPUTE_SWEEP_TABLE
#define PI 3.14159265
static bool gSweepTableReady;
static uint8_t gSweepTable[65];
/* Our table stores precomputed values for atan: [0...1] -> [0..PI/4]
We scale the results to [0..32]
*/
static const uint8_t* build_sweep_table() {
if (!gSweepTableReady) {
const int N = 65;
const double DENOM = N - 1;
for (int i = 0; i < N; i++)
{
double arg = i / DENOM;
double v = atan(arg);
int iv = (int)round(v * DENOM * 2 / PI);
// printf("[%d] atan(%g) = %g %d\n", i, arg, v, iv);
printf("%d, ", iv);
gSweepTable[i] = iv;
}
gSweepTableReady = true;
}
return gSweepTable;
}
#else
static const uint8_t gSweepTable[] = {
0, 1, 1, 2, 3, 3, 4, 4, 5, 6, 6, 7, 8, 8, 9, 9,
10, 11, 11, 12, 12, 13, 13, 14, 15, 15, 16, 16, 17, 17, 18, 18,
19, 19, 20, 20, 21, 21, 22, 22, 23, 23, 24, 24, 25, 25, 25, 26,
26, 27, 27, 27, 28, 28, 29, 29, 29, 30, 30, 30, 31, 31, 31, 32,
32
};
static const uint8_t* build_sweep_table() { return gSweepTable; }
#endif
#endif
// divide numer/denom, with a bias of 6bits. Assumes numer <= denom
// and denom != 0. Since our table is 6bits big (+1), this is a nice fit.
// Same as (but faster than) SkFixedDiv(numer, denom) >> 10
//unsigned div_64(int numer, int denom);
#ifndef SK_SCALAR_IS_FLOAT
static unsigned div_64(int numer, int denom) {
SkASSERT(numer <= denom);
SkASSERT(numer > 0);
SkASSERT(denom > 0);
int nbits = SkCLZ(numer);
int dbits = SkCLZ(denom);
int bits = 6 - nbits + dbits;
SkASSERT(bits <= 6);
if (bits < 0) { // detect underflow
return 0;
}
denom <<= dbits - 1;
numer <<= nbits - 1;
unsigned result = 0;
// do the first one
if ((numer -= denom) >= 0) {
result = 1;
} else {
numer += denom;
}
// Now fall into our switch statement if there are more bits to compute
if (bits > 0) {
// make room for the rest of the answer bits
result <<= bits;
switch (bits) {
case 6:
if ((numer = (numer << 1) - denom) >= 0)
result |= 32;
else
numer += denom;
case 5:
if ((numer = (numer << 1) - denom) >= 0)
result |= 16;
else
numer += denom;
case 4:
if ((numer = (numer << 1) - denom) >= 0)
result |= 8;
else
numer += denom;
case 3:
if ((numer = (numer << 1) - denom) >= 0)
result |= 4;
else
numer += denom;
case 2:
if ((numer = (numer << 1) - denom) >= 0)
result |= 2;
else
numer += denom;
case 1:
default: // not strictly need, but makes GCC make better ARM code
if ((numer = (numer << 1) - denom) >= 0)
result |= 1;
else
numer += denom;
}
}
return result;
}
#endif
// Given x,y in the first quadrant, return 0..63 for the angle [0..90]
#ifndef SK_SCALAR_IS_FLOAT
static unsigned atan_0_90(SkFixed y, SkFixed x) {
#ifdef SK_DEBUG
{
static bool gOnce;
if (!gOnce) {
gOnce = true;
SkASSERT(div_64(55, 55) == 64);
SkASSERT(div_64(128, 256) == 32);
SkASSERT(div_64(2326528, 4685824) == 31);
SkASSERT(div_64(753664, 5210112) == 9);
SkASSERT(div_64(229376, 4882432) == 3);
SkASSERT(div_64(2, 64) == 2);
SkASSERT(div_64(1, 64) == 1);
// test that we handle underflow correctly
SkASSERT(div_64(12345, 0x54321234) == 0);
}
}
#endif
SkASSERT(y > 0 && x > 0);
const uint8_t* table = build_sweep_table();
unsigned result;
bool swap = (x < y);
if (swap) {
// first part of the atan(v) = PI/2 - atan(1/v) identity
// since our div_64 and table want v <= 1, where v = y/x
SkTSwap<SkFixed>(x, y);
}
result = div_64(y, x);
#ifdef SK_DEBUG
{
unsigned result2 = SkDivBits(y, x, 6);
SkASSERT(result2 == result ||
(result == 1 && result2 == 0));
}
#endif
SkASSERT(result < SK_ARRAY_COUNT(gSweepTable));
result = table[result];
if (swap) {
// complete the atan(v) = PI/2 - atan(1/v) identity
result = 64 - result;
// pin to 63
result -= result >> 6;
}
SkASSERT(result <= 63);
return result;
}
#endif
// returns angle in a circle [0..2PI) -> [0..255]
#ifdef SK_SCALAR_IS_FLOAT
static unsigned SkATan2_255(float y, float x) {
// static const float g255Over2PI = 255 / (2 * SK_ScalarPI);
static const float g255Over2PI = 40.584510488433314f;
float result = sk_float_atan2(y, x);
if (result < 0) {
result += 2 * SK_ScalarPI;
}
SkASSERT(result >= 0);
// since our value is always >= 0, we can cast to int, which is faster than
// calling floorf()
int ir = (int)(result * g255Over2PI);
SkASSERT(ir >= 0 && ir <= 255);
return ir;
}
#else
static unsigned SkATan2_255(SkFixed y, SkFixed x) {
if (x == 0) {
if (y == 0) {
return 0;
}
return y < 0 ? 192 : 64;
}
if (y == 0) {
return x < 0 ? 128 : 0;
}
/* Find the right quadrant for x,y
Since atan_0_90 only handles the first quadrant, we rotate x,y
appropriately before calling it, and then add the right amount
to account for the real quadrant.
quadrant 0 : add 0 | x > 0 && y > 0
quadrant 1 : add 64 (90 degrees) | x < 0 && y > 0
quadrant 2 : add 128 (180 degrees) | x < 0 && y < 0
quadrant 3 : add 192 (270 degrees) | x > 0 && y < 0
map x<0 to (1 << 6)
map y<0 to (3 << 6)
add = map_x ^ map_y
*/
int xsign = x >> 31;
int ysign = y >> 31;
int add = ((-xsign) ^ (ysign & 3)) << 6;
#ifdef SK_DEBUG
if (0 == add)
SkASSERT(x > 0 && y > 0);
else if (64 == add)
SkASSERT(x < 0 && y > 0);
else if (128 == add)
SkASSERT(x < 0 && y < 0);
else if (192 == add)
SkASSERT(x > 0 && y < 0);
else
SkDEBUGFAIL("bad value for add");
#endif
/* This ^ trick makes x, y positive, and the swap<> handles quadrants
where we need to rotate x,y by 90 or -90
*/
x = (x ^ xsign) - xsign;
y = (y ^ ysign) - ysign;
if (add & 64) { // quads 1 or 3 need to swap x,y
SkTSwap<SkFixed>(x, y);
}
unsigned result = add + atan_0_90(y, x);
SkASSERT(result < 256);
return result;
}
#endif
void Sweep_Gradient::shadeSpan(int x, int y, SkPMColor* SK_RESTRICT dstC,
int count) {
SkMatrix::MapXYProc proc = fDstToIndexProc;
const SkMatrix& matrix = fDstToIndex;
const SkPMColor* SK_RESTRICT cache = this->getCache32();
SkPoint srcPt;
if (fDstToIndexClass != kPerspective_MatrixClass) {
proc(matrix, SkIntToScalar(x) + SK_ScalarHalf,
SkIntToScalar(y) + SK_ScalarHalf, &srcPt);
SkScalar dx, fx = srcPt.fX;
SkScalar dy, fy = srcPt.fY;
if (fDstToIndexClass == kFixedStepInX_MatrixClass) {
SkFixed storage[2];
(void)matrix.fixedStepInX(SkIntToScalar(y) + SK_ScalarHalf,
&storage[0], &storage[1]);
dx = SkFixedToScalar(storage[0]);
dy = SkFixedToScalar(storage[1]);
} else {
SkASSERT(fDstToIndexClass == kLinear_MatrixClass);
dx = matrix.getScaleX();
dy = matrix.getSkewY();
}
for (; count > 0; --count) {
*dstC++ = cache[SkATan2_255(fy, fx)];
fx += dx;
fy += dy;
}
} else { // perspective case
for (int stop = x + count; x < stop; x++) {
proc(matrix, SkIntToScalar(x) + SK_ScalarHalf,
SkIntToScalar(y) + SK_ScalarHalf, &srcPt);
*dstC++ = cache[SkATan2_255(srcPt.fY, srcPt.fX)];
}
}
}
void Sweep_Gradient::shadeSpan16(int x, int y, uint16_t* SK_RESTRICT dstC,
int count) {
SkMatrix::MapXYProc proc = fDstToIndexProc;
const SkMatrix& matrix = fDstToIndex;
const uint16_t* SK_RESTRICT cache = this->getCache16();
int toggle = ((x ^ y) & 1) * kDitherStride16;
SkPoint srcPt;
if (fDstToIndexClass != kPerspective_MatrixClass) {
proc(matrix, SkIntToScalar(x) + SK_ScalarHalf,
SkIntToScalar(y) + SK_ScalarHalf, &srcPt);
SkScalar dx, fx = srcPt.fX;
SkScalar dy, fy = srcPt.fY;
if (fDstToIndexClass == kFixedStepInX_MatrixClass) {
SkFixed storage[2];
(void)matrix.fixedStepInX(SkIntToScalar(y) + SK_ScalarHalf,
&storage[0], &storage[1]);
dx = SkFixedToScalar(storage[0]);
dy = SkFixedToScalar(storage[1]);
} else {
SkASSERT(fDstToIndexClass == kLinear_MatrixClass);
dx = matrix.getScaleX();
dy = matrix.getSkewY();
}
for (; count > 0; --count) {
int index = SkATan2_255(fy, fx) >> (8 - kCache16Bits);
*dstC++ = cache[toggle + index];
toggle ^= kDitherStride16;
fx += dx;
fy += dy;
}
} else { // perspective case
for (int stop = x + count; x < stop; x++) {
proc(matrix, SkIntToScalar(x) + SK_ScalarHalf,
SkIntToScalar(y) + SK_ScalarHalf, &srcPt);
int index = SkATan2_255(srcPt.fY, srcPt.fX);
index >>= (8 - kCache16Bits);
*dstC++ = cache[toggle + index];
toggle ^= kDitherStride16;
}
}
}
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
#include "SkEmptyShader.h"
// assumes colors is SkColor* and pos is SkScalar*
#define EXPAND_1_COLOR(count) \
SkColor tmp[2]; \
do { \
if (1 == count) { \
tmp[0] = tmp[1] = colors[0]; \
colors = tmp; \
pos = NULL; \
count = 2; \
} \
} while (0)
SkShader* SkGradientShader::CreateLinear(const SkPoint pts[2],
const SkColor colors[],
const SkScalar pos[], int colorCount,
SkShader::TileMode mode,
SkUnitMapper* mapper) {
if (NULL == pts || NULL == colors || colorCount < 1) {
return NULL;
}
EXPAND_1_COLOR(colorCount);
return SkNEW_ARGS(Linear_Gradient,
(pts, colors, pos, colorCount, mode, mapper));
}
SkShader* SkGradientShader::CreateRadial(const SkPoint& center, SkScalar radius,
const SkColor colors[],
const SkScalar pos[], int colorCount,
SkShader::TileMode mode,
SkUnitMapper* mapper) {
if (radius <= 0 || NULL == colors || colorCount < 1) {
return NULL;
}
EXPAND_1_COLOR(colorCount);
return SkNEW_ARGS(Radial_Gradient,
(center, radius, colors, pos, colorCount, mode, mapper));
}
SkShader* SkGradientShader::CreateTwoPointRadial(const SkPoint& start,
SkScalar startRadius,
const SkPoint& end,
SkScalar endRadius,
const SkColor colors[],
const SkScalar pos[],
int colorCount,
SkShader::TileMode mode,
SkUnitMapper* mapper) {
if (startRadius < 0 || endRadius < 0 || NULL == colors || colorCount < 1) {
return NULL;
}
EXPAND_1_COLOR(colorCount);
return SkNEW_ARGS(Two_Point_Radial_Gradient,
(start, startRadius, end, endRadius, colors, pos,
colorCount, mode, mapper));
}
SkShader* SkGradientShader::CreateTwoPointConical(const SkPoint& start,
SkScalar startRadius,
const SkPoint& end,
SkScalar endRadius,
const SkColor colors[],
const SkScalar pos[],
int colorCount,
SkShader::TileMode mode,
SkUnitMapper* mapper) {
if (startRadius < 0 || endRadius < 0 || NULL == colors || colorCount < 1) {
return NULL;
}
if (start == end && startRadius == endRadius) {
return SkNEW(SkEmptyShader);
}
return SkNEW_ARGS(Two_Point_Conical_Gradient,
(start, startRadius, end, endRadius, colors, pos,
colorCount, mode, mapper));
}
SkShader* SkGradientShader::CreateSweep(SkScalar cx, SkScalar cy,
const SkColor colors[],
const SkScalar pos[],
int count, SkUnitMapper* mapper) {
if (NULL == colors || count < 1) {
return NULL;
}
EXPAND_1_COLOR(count);
return SkNEW_ARGS(Sweep_Gradient, (cx, cy, colors, pos, count, mapper));
}
SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_START(SkGradientShader)
SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(Linear_Gradient)
SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(Radial_Gradient)
SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(Sweep_Gradient)
SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(Two_Point_Radial_Gradient)
SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_END
|