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
|
/*
* 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 "SkGradientShaderPriv.h"
#include "SkLinearGradient.h"
#include "SkRadialGradient.h"
#include "SkTwoPointRadialGradient.h"
#include "SkTwoPointConicalGradient.h"
#include "SkSweepGradient.h"
void SkGradientShaderBase::Descriptor::flatten(SkWriteBuffer& buffer) const {
buffer.writeColorArray(fColors, fCount);
if (fPos) {
buffer.writeBool(true);
buffer.writeScalarArray(fPos, fCount);
} else {
buffer.writeBool(false);
}
buffer.write32(fTileMode);
buffer.write32(fGradFlags);
if (fLocalMatrix) {
buffer.writeBool(true);
buffer.writeMatrix(*fLocalMatrix);
} else {
buffer.writeBool(false);
}
}
bool SkGradientShaderBase::DescriptorScope::unflatten(SkReadBuffer& buffer) {
fCount = buffer.getArrayCount();
if (fCount > kStorageCount) {
size_t allocSize = (sizeof(SkColor) + sizeof(SkScalar)) * fCount;
fDynamicStorage.reset(allocSize);
fColors = (SkColor*)fDynamicStorage.get();
fPos = (SkScalar*)(fColors + fCount);
} else {
fColors = fColorStorage;
fPos = fPosStorage;
}
if (!buffer.readColorArray(const_cast<SkColor*>(fColors), fCount)) {
return false;
}
if (buffer.readBool()) {
if (!buffer.readScalarArray(const_cast<SkScalar*>(fPos), fCount)) {
return false;
}
} else {
fPos = NULL;
}
fTileMode = (SkShader::TileMode)buffer.read32();
fGradFlags = buffer.read32();
if (buffer.readBool()) {
fLocalMatrix = &fLocalMatrixStorage;
buffer.readMatrix(&fLocalMatrixStorage);
} else {
fLocalMatrix = NULL;
}
return buffer.isValid();
}
////////////////////////////////////////////////////////////////////////////////////////////
SkGradientShaderBase::SkGradientShaderBase(const Descriptor& desc)
: INHERITED(desc.fLocalMatrix)
{
SkASSERT(desc.fCount > 1);
fGradFlags = SkToU8(desc.fGradFlags);
SkASSERT((unsigned)desc.fTileMode < SkShader::kTileModeCount);
SkASSERT(SkShader::kTileModeCount == SK_ARRAY_COUNT(gTileProcs));
fTileMode = desc.fTileMode;
fTileProc = gTileProcs[desc.fTileMode];
/* 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 = desc.fCount;
// check if we need to add in dummy start and/or end position/colors
bool dummyFirst = false;
bool dummyLast = false;
if (desc.fPos) {
dummyFirst = desc.fPos[0] != 0;
dummyLast = desc.fPos[desc.fCount - 1] != SK_Scalar1;
fColorCount += dummyFirst + dummyLast;
}
if (fColorCount > kColorStorageCount) {
size_t size = sizeof(SkColor) + sizeof(Rec);
if (desc.fPos) {
size += sizeof(SkScalar);
}
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++ = desc.fColors[0];
}
memcpy(origColors, desc.fColors, desc.fCount * sizeof(SkColor));
if (dummyLast) {
origColors += desc.fCount;
*origColors = desc.fColors[desc.fCount - 1];
}
}
if (desc.fPos && fColorCount) {
fOrigPos = (SkScalar*)(fOrigColors + fColorCount);
fRecs = (Rec*)(fOrigPos + fColorCount);
} else {
fOrigPos = NULL;
fRecs = (Rec*)(fOrigColors + fColorCount);
}
if (fColorCount > 2) {
Rec* recs = fRecs;
recs->fPos = 0;
// recs->fScale = 0; // unused;
recs += 1;
if (desc.fPos) {
SkScalar* origPosPtr = fOrigPos;
*origPosPtr++ = 0;
/* 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
*/
SkScalar prev = 0;
int startIndex = dummyFirst ? 0 : 1;
int count = desc.fCount + dummyLast;
for (int i = startIndex; i < count; i++) {
// force the last value to be 1.0
SkScalar curr;
if (i == desc.fCount) { // we're really at the dummyLast
curr = 1;
} else {
curr = SkScalarPin(desc.fPos[i], 0, 1);
}
*origPosPtr++ = curr;
recs->fPos = SkScalarToFixed(curr);
SkFixed diff = SkScalarToFixed(curr - prev);
if (diff > 0) {
recs->fScale = (1 << 24) / diff;
} else {
recs->fScale = 0; // ignore this segment
}
// get ready for the next value
prev = curr;
recs += 1;
}
} else { // assume even distribution
fOrigPos = NULL;
SkFixed dp = SK_Fixed1 / (desc.fCount - 1);
SkFixed p = dp;
SkFixed scale = (desc.fCount - 1) << 8; // (1 << 24) / dp
for (int i = 1; i < desc.fCount - 1; i++) {
recs->fPos = p;
recs->fScale = scale;
recs += 1;
p += dp;
}
recs->fPos = SK_Fixed1;
recs->fScale = scale;
}
} else if (desc.fPos) {
SkASSERT(2 == fColorCount);
fOrigPos[0] = SkScalarPin(desc.fPos[0], 0, 1);
fOrigPos[1] = SkScalarPin(desc.fPos[1], fOrigPos[0], 1);
if (0 == fOrigPos[0] && 1 == fOrigPos[1]) {
fOrigPos = NULL;
}
}
this->initCommon();
}
#ifdef SK_SUPPORT_LEGACY_DEEPFLATTENING
static SkShader::TileMode unpack_mode(uint32_t packed) {
return (SkShader::TileMode)(packed & 0xF);
}
static uint32_t unpack_flags(uint32_t packed) {
return packed >> 4;
}
SkGradientShaderBase::SkGradientShaderBase(SkReadBuffer& buffer) : INHERITED(buffer) {
if (buffer.isVersionLT(SkReadBuffer::kNoUnitMappers_Version)) {
// skip the old SkUnitMapper slot
buffer.skipFlattenable();
}
int colorCount = fColorCount = buffer.getArrayCount();
if (colorCount > kColorStorageCount) {
size_t allocSize = (sizeof(SkColor) + sizeof(SkScalar) + sizeof(Rec)) * colorCount;
if (buffer.validateAvailable(allocSize)) {
fOrigColors = reinterpret_cast<SkColor*>(sk_malloc_throw(allocSize));
} else {
fOrigColors = NULL;
colorCount = fColorCount = 0;
}
} else {
fOrigColors = fStorage;
}
buffer.readColorArray(fOrigColors, colorCount);
fOrigPos = (SkScalar*)(fOrigColors + colorCount);
{
uint32_t packed = buffer.readUInt();
fGradFlags = SkToU8(unpack_flags(packed));
fTileMode = unpack_mode(packed);
}
fTileProc = gTileProcs[fTileMode];
fRecs = (Rec*)(fOrigPos + colorCount);
if (colorCount > 2) {
Rec* recs = fRecs;
recs[0].fPos = 0;
fOrigPos[0] = 0;
for (int i = 1; i < colorCount; i++) {
recs[i].fPos = buffer.readInt();
recs[i].fScale = buffer.readUInt();
fOrigPos[i] = SkFixedToScalar(recs[i].fPos);
}
} else {
fOrigPos = NULL;
}
buffer.readMatrix(&fPtsToUnit);
this->initCommon();
}
#endif
SkGradientShaderBase::~SkGradientShaderBase() {
if (fOrigColors != fStorage) {
sk_free(fOrigColors);
}
}
void SkGradientShaderBase::initCommon() {
unsigned colorAlpha = 0xFF;
for (int i = 0; i < fColorCount; i++) {
colorAlpha &= SkColorGetA(fOrigColors[i]);
}
fColorsAreOpaque = colorAlpha == 0xFF;
}
void SkGradientShaderBase::flatten(SkWriteBuffer& buffer) const {
Descriptor desc;
desc.fColors = fOrigColors;
desc.fPos = fOrigPos;
desc.fCount = fColorCount;
desc.fTileMode = fTileMode;
desc.fGradFlags = fGradFlags;
const SkMatrix& m = this->getLocalMatrix();
desc.fLocalMatrix = m.isIdentity() ? NULL : &m;
desc.flatten(buffer);
}
SkGradientShaderBase::GpuColorType SkGradientShaderBase::getGpuColorType(SkColor colors[3]) const {
if (fColorCount <= 3) {
memcpy(colors, fOrigColors, fColorCount * sizeof(SkColor));
}
if (SkShader::kClamp_TileMode == fTileMode) {
if (2 == fColorCount) {
return kTwo_GpuColorType;
} else if (3 == fColorCount &&
(SkScalarAbs(
SkFixedToScalar(fRecs[1].fPos) - SK_ScalarHalf) < SK_Scalar1 / 1000)) {
return kThree_GpuColorType;
}
}
return kTexture_GpuColorType;
}
void SkGradientShaderBase::FlipGradientColors(SkColor* colorDst, Rec* recDst,
SkColor* colorSrc, Rec* recSrc,
int count) {
SkAutoSTArray<8, SkColor> colorsTemp(count);
for (int i = 0; i < count; ++i) {
int offset = count - i - 1;
colorsTemp[i] = colorSrc[offset];
}
if (count > 2) {
SkAutoSTArray<8, Rec> recsTemp(count);
for (int i = 0; i < count; ++i) {
int offset = count - i - 1;
recsTemp[i].fPos = SK_Fixed1 - recSrc[offset].fPos;
recsTemp[i].fScale = recSrc[offset].fScale;
}
memcpy(recDst, recsTemp.get(), count * sizeof(Rec));
}
memcpy(colorDst, colorsTemp.get(), count * sizeof(SkColor));
}
void SkGradientShaderBase::flipGradientColors() {
FlipGradientColors(fOrigColors, fRecs, fOrigColors, fRecs, fColorCount);
}
bool SkGradientShaderBase::isOpaque() const {
return fColorsAreOpaque;
}
static unsigned rounded_divide(unsigned numer, unsigned denom) {
return (numer + (denom >> 1)) / denom;
}
bool SkGradientShaderBase::onAsLuminanceColor(SkColor* lum) const {
// we just compute an average color.
// possibly we could weight this based on the proportional width for each color
// assuming they are not evenly distributed in the fPos array.
int r = 0;
int g = 0;
int b = 0;
const int n = fColorCount;
for (int i = 0; i < n; ++i) {
SkColor c = fOrigColors[i];
r += SkColorGetR(c);
g += SkColorGetG(c);
b += SkColorGetB(c);
}
*lum = SkColorSetRGB(rounded_divide(r, n), rounded_divide(g, n), rounded_divide(b, n));
return true;
}
SkGradientShaderBase::GradientShaderBaseContext::GradientShaderBaseContext(
const SkGradientShaderBase& shader, const ContextRec& rec)
: INHERITED(shader, rec)
, fCache(shader.refCache(getPaintAlpha()))
{
const SkMatrix& inverse = this->getTotalInverse();
fDstToIndex.setConcat(shader.fPtsToUnit, inverse);
fDstToIndexProc = fDstToIndex.getMapXYProc();
fDstToIndexClass = (uint8_t)SkShader::Context::ComputeMatrixClass(fDstToIndex);
// now convert our colors in to PMColors
unsigned paintAlpha = this->getPaintAlpha();
fFlags = this->INHERITED::getFlags();
if (shader.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 (shader.fColorsAreOpaque) {
fFlags |= kHasSpan16_Flag;
}
}
SkGradientShaderBase::GradientShaderCache::GradientShaderCache(
U8CPU alpha, const SkGradientShaderBase& shader)
: fCacheAlpha(alpha)
, fShader(shader)
, fCache16Inited(false)
, fCache32Inited(false)
{
// Only initialize the cache in getCache16/32.
fCache16 = NULL;
fCache32 = NULL;
fCache16Storage = NULL;
fCache32PixelRef = NULL;
}
SkGradientShaderBase::GradientShaderCache::~GradientShaderCache() {
sk_free(fCache16Storage);
SkSafeUnref(fCache32PixelRef);
}
#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 SkGradientShaderBase::GradientShaderCache::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);
}
/*
* r,g,b used to be SkFixed, but on gcc (4.2.1 mac and 4.6.3 goobuntu) in
* release builds, we saw a compiler error where the 0xFF parameter in
* SkPackARGB32() was being totally ignored whenever it was called with
* a non-zero add (e.g. 0x8000).
*
* We found two work-arounds:
* 1. change r,g,b to unsigned (or just one of them)
* 2. change SkPackARGB32 to + its (a << SK_A32_SHIFT) value instead
* of using |
*
* We chose #1 just because it was more localized.
* See http://code.google.com/p/skia/issues/detail?id=1113
*
* The type SkUFixed encapsulate this need for unsigned, but logically Fixed.
*/
typedef uint32_t SkUFixed;
void SkGradientShaderBase::GradientShaderCache::Build32bitCache(
SkPMColor cache[], SkColor c0, SkColor c1,
int count, U8CPU paintAlpha, uint32_t gradFlags) {
SkASSERT(count > 1);
// need to apply paintAlpha to our two endpoints
uint32_t a0 = SkMulDiv255Round(SkColorGetA(c0), paintAlpha);
uint32_t a1 = SkMulDiv255Round(SkColorGetA(c1), paintAlpha);
const bool interpInPremul = SkToBool(gradFlags &
SkGradientShader::kInterpolateColorsInPremul_Flag);
uint32_t r0 = SkColorGetR(c0);
uint32_t g0 = SkColorGetG(c0);
uint32_t b0 = SkColorGetB(c0);
uint32_t r1 = SkColorGetR(c1);
uint32_t g1 = SkColorGetG(c1);
uint32_t b1 = SkColorGetB(c1);
if (interpInPremul) {
r0 = SkMulDiv255Round(r0, a0);
g0 = SkMulDiv255Round(g0, a0);
b0 = SkMulDiv255Round(b0, a0);
r1 = SkMulDiv255Round(r1, a1);
g1 = SkMulDiv255Round(g1, a1);
b1 = SkMulDiv255Round(b1, a1);
}
SkFixed da = SkIntToFixed(a1 - a0) / (count - 1);
SkFixed dr = SkIntToFixed(r1 - r0) / (count - 1);
SkFixed dg = SkIntToFixed(g1 - g0) / (count - 1);
SkFixed db = SkIntToFixed(b1 - b0) / (count - 1);
/* We pre-add 1/8 to avoid having to add this to our [0] value each time
in the loop. Without this, the bias for each would be
0x2000 0xA000 0xE000 0x6000
With this trick, we can add 0 for the first (no-op) and just adjust the
others.
*/
SkUFixed a = SkIntToFixed(a0) + 0x2000;
SkUFixed r = SkIntToFixed(r0) + 0x2000;
SkUFixed g = SkIntToFixed(g0) + 0x2000;
SkUFixed b = SkIntToFixed(b0) + 0x2000;
/*
* Our dither-cell (spatially) is
* 0 2
* 3 1
* Where
* [0] -> [-1/8 ... 1/8 ) values near 0
* [1] -> [ 1/8 ... 3/8 ) values near 1/4
* [2] -> [ 3/8 ... 5/8 ) values near 1/2
* [3] -> [ 5/8 ... 7/8 ) values near 3/4
*/
if (0xFF == a0 && 0 == da) {
do {
cache[kCache32Count*0] = SkPackARGB32(0xFF, (r + 0 ) >> 16,
(g + 0 ) >> 16,
(b + 0 ) >> 16);
cache[kCache32Count*1] = SkPackARGB32(0xFF, (r + 0x8000) >> 16,
(g + 0x8000) >> 16,
(b + 0x8000) >> 16);
cache[kCache32Count*2] = SkPackARGB32(0xFF, (r + 0xC000) >> 16,
(g + 0xC000) >> 16,
(b + 0xC000) >> 16);
cache[kCache32Count*3] = SkPackARGB32(0xFF, (r + 0x4000) >> 16,
(g + 0x4000) >> 16,
(b + 0x4000) >> 16);
cache += 1;
r += dr;
g += dg;
b += db;
} while (--count != 0);
} else if (interpInPremul) {
do {
cache[kCache32Count*0] = SkPackARGB32((a + 0 ) >> 16,
(r + 0 ) >> 16,
(g + 0 ) >> 16,
(b + 0 ) >> 16);
cache[kCache32Count*1] = SkPackARGB32((a + 0x8000) >> 16,
(r + 0x8000) >> 16,
(g + 0x8000) >> 16,
(b + 0x8000) >> 16);
cache[kCache32Count*2] = SkPackARGB32((a + 0xC000) >> 16,
(r + 0xC000) >> 16,
(g + 0xC000) >> 16,
(b + 0xC000) >> 16);
cache[kCache32Count*3] = SkPackARGB32((a + 0x4000) >> 16,
(r + 0x4000) >> 16,
(g + 0x4000) >> 16,
(b + 0x4000) >> 16);
cache += 1;
a += da;
r += dr;
g += dg;
b += db;
} while (--count != 0);
} else { // interpolate in unpreml space
do {
cache[kCache32Count*0] = SkPremultiplyARGBInline((a + 0 ) >> 16,
(r + 0 ) >> 16,
(g + 0 ) >> 16,
(b + 0 ) >> 16);
cache[kCache32Count*1] = SkPremultiplyARGBInline((a + 0x8000) >> 16,
(r + 0x8000) >> 16,
(g + 0x8000) >> 16,
(b + 0x8000) >> 16);
cache[kCache32Count*2] = SkPremultiplyARGBInline((a + 0xC000) >> 16,
(r + 0xC000) >> 16,
(g + 0xC000) >> 16,
(b + 0xC000) >> 16);
cache[kCache32Count*3] = SkPremultiplyARGBInline((a + 0x4000) >> 16,
(r + 0x4000) >> 16,
(g + 0x4000) >> 16,
(b + 0x4000) >> 16);
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);
}
const uint16_t* SkGradientShaderBase::GradientShaderCache::getCache16() {
SkOnce(&fCache16Inited, &fCache16Mutex, SkGradientShaderBase::GradientShaderCache::initCache16,
this);
SkASSERT(fCache16);
return fCache16;
}
void SkGradientShaderBase::GradientShaderCache::initCache16(GradientShaderCache* cache) {
// double the count for dither entries
const int entryCount = kCache16Count * 2;
const size_t allocSize = sizeof(uint16_t) * entryCount;
SkASSERT(NULL == cache->fCache16Storage);
cache->fCache16Storage = (uint16_t*)sk_malloc_throw(allocSize);
cache->fCache16 = cache->fCache16Storage;
if (cache->fShader.fColorCount == 2) {
Build16bitCache(cache->fCache16, cache->fShader.fOrigColors[0],
cache->fShader.fOrigColors[1], kCache16Count);
} else {
Rec* rec = cache->fShader.fRecs;
int prevIndex = 0;
for (int i = 1; i < cache->fShader.fColorCount; i++) {
int nextIndex = SkFixedToFFFF(rec[i].fPos) >> kCache16Shift;
SkASSERT(nextIndex < kCache16Count);
if (nextIndex > prevIndex)
Build16bitCache(cache->fCache16 + prevIndex, cache->fShader.fOrigColors[i-1],
cache->fShader.fOrigColors[i], nextIndex - prevIndex + 1);
prevIndex = nextIndex;
}
}
}
const SkPMColor* SkGradientShaderBase::GradientShaderCache::getCache32() {
SkOnce(&fCache32Inited, &fCache32Mutex, SkGradientShaderBase::GradientShaderCache::initCache32,
this);
SkASSERT(fCache32);
return fCache32;
}
void SkGradientShaderBase::GradientShaderCache::initCache32(GradientShaderCache* cache) {
const int kNumberOfDitherRows = 4;
const SkImageInfo info = SkImageInfo::MakeN32Premul(kCache32Count, kNumberOfDitherRows);
SkASSERT(NULL == cache->fCache32PixelRef);
cache->fCache32PixelRef = SkMallocPixelRef::NewAllocate(info, 0, NULL);
cache->fCache32 = (SkPMColor*)cache->fCache32PixelRef->getAddr();
if (cache->fShader.fColorCount == 2) {
Build32bitCache(cache->fCache32, cache->fShader.fOrigColors[0],
cache->fShader.fOrigColors[1], kCache32Count, cache->fCacheAlpha,
cache->fShader.fGradFlags);
} else {
Rec* rec = cache->fShader.fRecs;
int prevIndex = 0;
for (int i = 1; i < cache->fShader.fColorCount; i++) {
int nextIndex = SkFixedToFFFF(rec[i].fPos) >> kCache32Shift;
SkASSERT(nextIndex < kCache32Count);
if (nextIndex > prevIndex)
Build32bitCache(cache->fCache32 + prevIndex, cache->fShader.fOrigColors[i-1],
cache->fShader.fOrigColors[i], nextIndex - prevIndex + 1,
cache->fCacheAlpha, cache->fShader.fGradFlags);
prevIndex = nextIndex;
}
}
}
/*
* The gradient holds a cache for the most recent value of alpha. Successive
* callers with the same alpha value will share the same cache.
*/
SkGradientShaderBase::GradientShaderCache* SkGradientShaderBase::refCache(U8CPU alpha) const {
SkAutoMutexAcquire ama(fCacheMutex);
if (!fCache || fCache->getAlpha() != alpha) {
fCache.reset(SkNEW_ARGS(GradientShaderCache, (alpha, *this)));
}
// Increment the ref counter inside the mutex to ensure the returned pointer is still valid.
// Otherwise, the pointer may have been overwritten on a different thread before the object's
// ref count was incremented.
fCache.get()->ref();
return fCache;
}
SK_DECLARE_STATIC_MUTEX(gGradientCacheMutex);
/*
* 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 SkGradientShaderBase::getGradientTableBitmap(SkBitmap* bitmap) const {
// our caller assumes no external alpha, so we ensure that our cache is
// built with 0xFF
SkAutoTUnref<GradientShaderCache> cache(this->refCache(0xFF));
// build our key: [numColors + colors[] + {positions[]} + flags ]
int count = 1 + fColorCount + 1;
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;
}
}
*buffer++ = fGradFlags;
SkASSERT(buffer - storage.get() == count);
///////////////////////////////////
static SkGradientBitmapCache* 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(gGradientCacheMutex);
if (NULL == gCache) {
gCache = SkNEW_ARGS(SkGradientBitmapCache, (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)cache->getCache32();
bitmap->setInfo(SkImageInfo::MakeN32Premul(kCache32Count, 1));
bitmap->setPixelRef(cache->getCache32PixelRef());
gCache->add(storage.get(), size, *bitmap);
}
}
void SkGradientShaderBase::commonAsAGradient(GradientInfo* info, bool flipGrad) const {
if (info) {
if (info->fColorCount >= fColorCount) {
SkColor* colorLoc;
Rec* recLoc;
if (flipGrad && (info->fColors || info->fColorOffsets)) {
SkAutoSTArray<8, SkColor> colorStorage(fColorCount);
SkAutoSTArray<8, Rec> recStorage(fColorCount);
colorLoc = colorStorage.get();
recLoc = recStorage.get();
FlipGradientColors(colorLoc, recLoc, fOrigColors, fRecs, fColorCount);
} else {
colorLoc = fOrigColors;
recLoc = fRecs;
}
if (info->fColors) {
memcpy(info->fColors, colorLoc, 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(recLoc[i].fPos);
}
}
}
}
info->fColorCount = fColorCount;
info->fTileMode = fTileMode;
info->fGradientFlags = fGradFlags;
}
}
#ifndef SK_IGNORE_TO_STRING
void SkGradientShaderBase::toString(SkString* str) const {
str->appendf("%d colors: ", fColorCount);
for (int i = 0; i < fColorCount; ++i) {
str->appendHex(fOrigColors[i]);
if (i < fColorCount-1) {
str->append(", ");
}
}
if (fColorCount > 2) {
str->append(" points: (");
for (int i = 0; i < fColorCount; ++i) {
str->appendScalar(SkFixedToScalar(fRecs[i].fPos));
if (i < fColorCount-1) {
str->append(", ");
}
}
str->append(")");
}
static const char* gTileModeName[SkShader::kTileModeCount] = {
"clamp", "repeat", "mirror"
};
str->append(" ");
str->append(gTileModeName[fTileMode]);
this->INHERITED::toString(str);
}
#endif
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
// 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)
static void desc_init(SkGradientShaderBase::Descriptor* desc,
const SkColor colors[], const SkScalar pos[], int colorCount,
SkShader::TileMode mode, uint32_t flags, const SkMatrix* localMatrix) {
desc->fColors = colors;
desc->fPos = pos;
desc->fCount = colorCount;
desc->fTileMode = mode;
desc->fGradFlags = flags;
desc->fLocalMatrix = localMatrix;
}
SkShader* SkGradientShader::CreateLinear(const SkPoint pts[2],
const SkColor colors[],
const SkScalar pos[], int colorCount,
SkShader::TileMode mode,
uint32_t flags,
const SkMatrix* localMatrix) {
if (NULL == pts || NULL == colors || colorCount < 1) {
return NULL;
}
EXPAND_1_COLOR(colorCount);
SkGradientShaderBase::Descriptor desc;
desc_init(&desc, colors, pos, colorCount, mode, flags, localMatrix);
return SkNEW_ARGS(SkLinearGradient, (pts, desc));
}
SkShader* SkGradientShader::CreateRadial(const SkPoint& center, SkScalar radius,
const SkColor colors[],
const SkScalar pos[], int colorCount,
SkShader::TileMode mode,
uint32_t flags,
const SkMatrix* localMatrix) {
if (radius <= 0 || NULL == colors || colorCount < 1) {
return NULL;
}
EXPAND_1_COLOR(colorCount);
SkGradientShaderBase::Descriptor desc;
desc_init(&desc, colors, pos, colorCount, mode, flags, localMatrix);
return SkNEW_ARGS(SkRadialGradient, (center, radius, desc));
}
SkShader* SkGradientShader::CreateTwoPointRadial(const SkPoint& start,
SkScalar startRadius,
const SkPoint& end,
SkScalar endRadius,
const SkColor colors[],
const SkScalar pos[],
int colorCount,
SkShader::TileMode mode,
uint32_t flags,
const SkMatrix* localMatrix) {
if (startRadius < 0 || endRadius < 0 || NULL == colors || colorCount < 1) {
return NULL;
}
EXPAND_1_COLOR(colorCount);
SkGradientShaderBase::Descriptor desc;
desc_init(&desc, colors, pos, colorCount, mode, flags, localMatrix);
return SkNEW_ARGS(SkTwoPointRadialGradient,
(start, startRadius, end, endRadius, desc));
}
SkShader* SkGradientShader::CreateTwoPointConical(const SkPoint& start,
SkScalar startRadius,
const SkPoint& end,
SkScalar endRadius,
const SkColor colors[],
const SkScalar pos[],
int colorCount,
SkShader::TileMode mode,
uint32_t flags,
const SkMatrix* localMatrix) {
if (startRadius < 0 || endRadius < 0 || NULL == colors || colorCount < 1) {
return NULL;
}
if (start == end && startRadius == endRadius) {
return SkShader::CreateEmptyShader();
}
EXPAND_1_COLOR(colorCount);
bool flipGradient = startRadius > endRadius;
SkGradientShaderBase::Descriptor desc;
if (!flipGradient) {
desc_init(&desc, colors, pos, colorCount, mode, flags, localMatrix);
return SkNEW_ARGS(SkTwoPointConicalGradient,
(start, startRadius, end, endRadius, flipGradient, desc));
} else {
SkAutoSTArray<8, SkColor> colorsNew(colorCount);
SkAutoSTArray<8, SkScalar> posNew(colorCount);
for (int i = 0; i < colorCount; ++i) {
colorsNew[i] = colors[colorCount - i - 1];
}
if (pos) {
for (int i = 0; i < colorCount; ++i) {
posNew[i] = 1 - pos[colorCount - i - 1];
}
desc_init(&desc, colorsNew.get(), posNew.get(), colorCount, mode, flags, localMatrix);
} else {
desc_init(&desc, colorsNew.get(), NULL, colorCount, mode, flags, localMatrix);
}
return SkNEW_ARGS(SkTwoPointConicalGradient,
(end, endRadius, start, startRadius, flipGradient, desc));
}
}
SkShader* SkGradientShader::CreateSweep(SkScalar cx, SkScalar cy,
const SkColor colors[],
const SkScalar pos[],
int colorCount,
uint32_t flags,
const SkMatrix* localMatrix) {
if (NULL == colors || colorCount < 1) {
return NULL;
}
EXPAND_1_COLOR(colorCount);
SkGradientShaderBase::Descriptor desc;
desc_init(&desc, colors, pos, colorCount, SkShader::kClamp_TileMode, flags, localMatrix);
return SkNEW_ARGS(SkSweepGradient, (cx, cy, desc));
}
SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_START(SkGradientShader)
SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkLinearGradient)
SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkRadialGradient)
SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkSweepGradient)
SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkTwoPointRadialGradient)
SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkTwoPointConicalGradient)
SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_END
///////////////////////////////////////////////////////////////////////////////
#if SK_SUPPORT_GPU
#include "effects/GrTextureStripAtlas.h"
#include "GrTBackendEffectFactory.h"
#include "gl/builders/GrGLProgramBuilder.h"
#include "SkGr.h"
GrGLGradientEffect::GrGLGradientEffect(const GrBackendEffectFactory& factory)
: INHERITED(factory)
, fCachedYCoord(SK_ScalarMax) {
}
GrGLGradientEffect::~GrGLGradientEffect() { }
void GrGLGradientEffect::emitUniforms(GrGLProgramBuilder* builder, uint32_t baseKey) {
if (SkGradientShaderBase::kTwo_GpuColorType == ColorTypeFromKey(baseKey)) { // 2 Color case
fColorStartUni = builder->addUniform(GrGLProgramBuilder::kFragment_Visibility,
kVec4f_GrSLType, "GradientStartColor");
fColorEndUni = builder->addUniform(GrGLProgramBuilder::kFragment_Visibility,
kVec4f_GrSLType, "GradientEndColor");
} else if (SkGradientShaderBase::kThree_GpuColorType == ColorTypeFromKey(baseKey)){ // 3 Color Case
fColorStartUni = builder->addUniform(GrGLProgramBuilder::kFragment_Visibility,
kVec4f_GrSLType, "GradientStartColor");
fColorMidUni = builder->addUniform(GrGLProgramBuilder::kFragment_Visibility,
kVec4f_GrSLType, "GradientMidColor");
fColorEndUni = builder->addUniform(GrGLProgramBuilder::kFragment_Visibility,
kVec4f_GrSLType, "GradientEndColor");
} else { // if not a fast case
fFSYUni = builder->addUniform(GrGLProgramBuilder::kFragment_Visibility,
kFloat_GrSLType, "GradientYCoordFS");
}
}
static inline void set_color_uni(const GrGLProgramDataManager& pdman,
const GrGLProgramDataManager::UniformHandle uni,
const SkColor* color) {
pdman.set4f(uni,
SkColorGetR(*color) / 255.f,
SkColorGetG(*color) / 255.f,
SkColorGetB(*color) / 255.f,
SkColorGetA(*color) / 255.f);
}
static inline void set_mul_color_uni(const GrGLProgramDataManager& pdman,
const GrGLProgramDataManager::UniformHandle uni,
const SkColor* color){
float a = SkColorGetA(*color) / 255.f;
float aDiv255 = a / 255.f;
pdman.set4f(uni,
SkColorGetR(*color) * aDiv255,
SkColorGetG(*color) * aDiv255,
SkColorGetB(*color) * aDiv255,
a);
}
void GrGLGradientEffect::setData(const GrGLProgramDataManager& pdman,
const GrDrawEffect& drawEffect) {
const GrGradientEffect& e = drawEffect.castEffect<GrGradientEffect>();
if (SkGradientShaderBase::kTwo_GpuColorType == e.getColorType()){
if (GrGradientEffect::kBeforeInterp_PremulType == e.getPremulType()) {
set_mul_color_uni(pdman, fColorStartUni, e.getColors(0));
set_mul_color_uni(pdman, fColorEndUni, e.getColors(1));
} else {
set_color_uni(pdman, fColorStartUni, e.getColors(0));
set_color_uni(pdman, fColorEndUni, e.getColors(1));
}
} else if (SkGradientShaderBase::kThree_GpuColorType == e.getColorType()){
if (GrGradientEffect::kBeforeInterp_PremulType == e.getPremulType()) {
set_mul_color_uni(pdman, fColorStartUni, e.getColors(0));
set_mul_color_uni(pdman, fColorMidUni, e.getColors(1));
set_mul_color_uni(pdman, fColorEndUni, e.getColors(2));
} else {
set_color_uni(pdman, fColorStartUni, e.getColors(0));
set_color_uni(pdman, fColorMidUni, e.getColors(1));
set_color_uni(pdman, fColorEndUni, e.getColors(2));
}
} else {
SkScalar yCoord = e.getYCoord();
if (yCoord != fCachedYCoord) {
pdman.set1f(fFSYUni, yCoord);
fCachedYCoord = yCoord;
}
}
}
uint32_t GrGLGradientEffect::GenBaseGradientKey(const GrDrawEffect& drawEffect) {
const GrGradientEffect& e = drawEffect.castEffect<GrGradientEffect>();
uint32_t key = 0;
if (SkGradientShaderBase::kTwo_GpuColorType == e.getColorType()) {
key |= kTwoColorKey;
} else if (SkGradientShaderBase::kThree_GpuColorType == e.getColorType()){
key |= kThreeColorKey;
}
if (GrGradientEffect::kBeforeInterp_PremulType == e.getPremulType()) {
key |= kPremulBeforeInterpKey;
}
return key;
}
void GrGLGradientEffect::emitColor(GrGLProgramBuilder* builder,
const char* gradientTValue,
uint32_t baseKey,
const char* outputColor,
const char* inputColor,
const TextureSamplerArray& samplers) {
GrGLFragmentShaderBuilder* fsBuilder = builder->getFragmentShaderBuilder();
if (SkGradientShaderBase::kTwo_GpuColorType == ColorTypeFromKey(baseKey)){
fsBuilder->codeAppendf("\tvec4 colorTemp = mix(%s, %s, clamp(%s, 0.0, 1.0));\n",
builder->getUniformVariable(fColorStartUni).c_str(),
builder->getUniformVariable(fColorEndUni).c_str(),
gradientTValue);
// Note that we could skip this step if both colors are known to be opaque. Two
// considerations:
// The gradient SkShader reporting opaque is more restrictive than necessary in the two pt
// case. Make sure the key reflects this optimization (and note that it can use the same
// shader as thekBeforeIterp case). This same optimization applies to the 3 color case below.
if (GrGradientEffect::kAfterInterp_PremulType == PremulTypeFromKey(baseKey)) {
fsBuilder->codeAppend("\tcolorTemp.rgb *= colorTemp.a;\n");
}
fsBuilder->codeAppendf("\t%s = %s;\n", outputColor,
(GrGLSLExpr4(inputColor) * GrGLSLExpr4("colorTemp")).c_str());
} else if (SkGradientShaderBase::kThree_GpuColorType == ColorTypeFromKey(baseKey)){
fsBuilder->codeAppendf("\tfloat oneMinus2t = 1.0 - (2.0 * (%s));\n",
gradientTValue);
fsBuilder->codeAppendf("\tvec4 colorTemp = clamp(oneMinus2t, 0.0, 1.0) * %s;\n",
builder->getUniformVariable(fColorStartUni).c_str());
if (kTegra3_GrGLRenderer == builder->ctxInfo().renderer()) {
// The Tegra3 compiler will sometimes never return if we have
// min(abs(oneMinus2t), 1.0), or do the abs first in a separate expression.
fsBuilder->codeAppend("\tfloat minAbs = abs(oneMinus2t);\n");
fsBuilder->codeAppend("\tminAbs = minAbs > 1.0 ? 1.0 : minAbs;\n");
fsBuilder->codeAppendf("\tcolorTemp += (1.0 - minAbs) * %s;\n",
builder->getUniformVariable(fColorMidUni).c_str());
} else {
fsBuilder->codeAppendf("\tcolorTemp += (1.0 - min(abs(oneMinus2t), 1.0)) * %s;\n",
builder->getUniformVariable(fColorMidUni).c_str());
}
fsBuilder->codeAppendf("\tcolorTemp += clamp(-oneMinus2t, 0.0, 1.0) * %s;\n",
builder->getUniformVariable(fColorEndUni).c_str());
if (GrGradientEffect::kAfterInterp_PremulType == PremulTypeFromKey(baseKey)) {
fsBuilder->codeAppend("\tcolorTemp.rgb *= colorTemp.a;\n");
}
fsBuilder->codeAppendf("\t%s = %s;\n", outputColor,
(GrGLSLExpr4(inputColor) * GrGLSLExpr4("colorTemp")).c_str());
} else {
fsBuilder->codeAppendf("\tvec2 coord = vec2(%s, %s);\n",
gradientTValue,
builder->getUniformVariable(fFSYUni).c_str());
fsBuilder->codeAppendf("\t%s = ", outputColor);
fsBuilder->appendTextureLookupAndModulate(inputColor,
samplers[0],
"coord");
fsBuilder->codeAppend(";\n");
}
}
/////////////////////////////////////////////////////////////////////
GrGradientEffect::GrGradientEffect(GrContext* ctx,
const SkGradientShaderBase& shader,
const SkMatrix& matrix,
SkShader::TileMode tileMode) {
fIsOpaque = shader.isOpaque();
fColorType = shader.getGpuColorType(&fColors[0]);
// The two and three color specializations do not currently support tiling.
if (SkGradientShaderBase::kTwo_GpuColorType == fColorType ||
SkGradientShaderBase::kThree_GpuColorType == fColorType) {
fRow = -1;
if (SkGradientShader::kInterpolateColorsInPremul_Flag & shader.getGradFlags()) {
fPremulType = kBeforeInterp_PremulType;
} else {
fPremulType = kAfterInterp_PremulType;
}
fCoordTransform.reset(kCoordSet, matrix);
} else {
// doesn't matter how this is set, just be consistent because it is part of the effect key.
fPremulType = kBeforeInterp_PremulType;
SkBitmap bitmap;
shader.getGradientTableBitmap(&bitmap);
GrTextureStripAtlas::Desc desc;
desc.fWidth = bitmap.width();
desc.fHeight = 32;
desc.fRowHeight = bitmap.height();
desc.fContext = ctx;
desc.fConfig = SkImageInfo2GrPixelConfig(bitmap.info());
fAtlas = GrTextureStripAtlas::GetAtlas(desc);
SkASSERT(NULL != fAtlas);
// We always filter the gradient table. Each table is one row of a texture, always y-clamp.
GrTextureParams params;
params.setFilterMode(GrTextureParams::kBilerp_FilterMode);
params.setTileModeX(tileMode);
fRow = fAtlas->lockRow(bitmap);
if (-1 != fRow) {
fYCoord = fAtlas->getYOffset(fRow) + SK_ScalarHalf *
fAtlas->getVerticalScaleFactor();
fCoordTransform.reset(kCoordSet, matrix, fAtlas->getTexture());
fTextureAccess.reset(fAtlas->getTexture(), params);
} else {
GrTexture* texture = GrLockAndRefCachedBitmapTexture(ctx, bitmap, ¶ms);
fCoordTransform.reset(kCoordSet, matrix, texture);
fTextureAccess.reset(texture, params);
fYCoord = SK_ScalarHalf;
// Unlock immediately, this is not great, but we don't have a way of
// knowing when else to unlock it currently, so it may get purged from
// the cache, but it'll still be ref'd until it's no longer being used.
GrUnlockAndUnrefCachedBitmapTexture(texture);
}
this->addTextureAccess(&fTextureAccess);
}
this->addCoordTransform(&fCoordTransform);
}
GrGradientEffect::~GrGradientEffect() {
if (this->useAtlas()) {
fAtlas->unlockRow(fRow);
}
}
bool GrGradientEffect::onIsEqual(const GrEffect& effect) const {
const GrGradientEffect& s = CastEffect<GrGradientEffect>(effect);
if (this->fColorType == s.getColorType()){
if (SkGradientShaderBase::kTwo_GpuColorType == fColorType) {
if (*this->getColors(0) != *s.getColors(0) ||
*this->getColors(1) != *s.getColors(1)) {
return false;
}
} else if (SkGradientShaderBase::kThree_GpuColorType == fColorType) {
if (*this->getColors(0) != *s.getColors(0) ||
*this->getColors(1) != *s.getColors(1) ||
*this->getColors(2) != *s.getColors(2)) {
return false;
}
} else {
if (fYCoord != s.getYCoord()) {
return false;
}
}
return fTextureAccess.getTexture() == s.fTextureAccess.getTexture() &&
fTextureAccess.getParams().getTileModeX() ==
s.fTextureAccess.getParams().getTileModeX() &&
this->useAtlas() == s.useAtlas() &&
fCoordTransform.getMatrix().cheapEqualTo(s.fCoordTransform.getMatrix());
}
return false;
}
void GrGradientEffect::getConstantColorComponents(GrColor* color, uint32_t* validFlags) const {
if (fIsOpaque && (kA_GrColorComponentFlag & *validFlags) && 0xff == GrColorUnpackA(*color)) {
*validFlags = kA_GrColorComponentFlag;
} else {
*validFlags = 0;
}
}
int GrGradientEffect::RandomGradientParams(SkRandom* random,
SkColor colors[],
SkScalar** stops,
SkShader::TileMode* tm) {
int outColors = random->nextRangeU(1, kMaxRandomGradientColors);
// if one color, omit stops, otherwise randomly decide whether or not to
if (outColors == 1 || (outColors >= 2 && random->nextBool())) {
*stops = NULL;
}
SkScalar stop = 0.f;
for (int i = 0; i < outColors; ++i) {
colors[i] = random->nextU();
if (NULL != *stops) {
(*stops)[i] = stop;
stop = i < outColors - 1 ? stop + random->nextUScalar1() * (1.f - stop) : 1.f;
}
}
*tm = static_cast<SkShader::TileMode>(random->nextULessThan(SkShader::kTileModeCount));
return outColors;
}
#endif
|