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
|
/*
* 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 "SkAvoidXfermode.h"
#include "SkColorPriv.h"
#include "SkReadBuffer.h"
#include "SkWriteBuffer.h"
#include "SkString.h"
SkAvoidXfermode::SkAvoidXfermode(SkColor opColor, U8CPU tolerance, Mode mode) {
if (tolerance > 255) {
tolerance = 255;
}
fTolerance = SkToU8(tolerance);
fOpColor = opColor;
fDistMul = (256 << 14) / (tolerance + 1);
fMode = mode;
}
SkFlattenable* SkAvoidXfermode::CreateProc(SkReadBuffer& buffer) {
const SkColor color = buffer.readColor();
const unsigned tolerance = buffer.readUInt();
const unsigned mode = buffer.readUInt();
return Create(color, tolerance, (Mode)mode);
}
void SkAvoidXfermode::flatten(SkWriteBuffer& buffer) const {
buffer.writeColor(fOpColor);
buffer.writeUInt(fTolerance);
buffer.writeUInt(fMode);
}
// returns 0..31
static unsigned color_dist16(uint16_t c, unsigned r, unsigned g, unsigned b) {
SkASSERT(r <= SK_R16_MASK);
SkASSERT(g <= SK_G16_MASK);
SkASSERT(b <= SK_B16_MASK);
unsigned dr = SkAbs32(SkGetPackedR16(c) - r);
unsigned dg = SkAbs32(SkGetPackedG16(c) - g) >> (SK_G16_BITS - SK_R16_BITS);
unsigned db = SkAbs32(SkGetPackedB16(c) - b);
return SkMax32(dr, SkMax32(dg, db));
}
// returns 0..255
static unsigned color_dist32(SkPMColor c, U8CPU r, U8CPU g, U8CPU b) {
SkASSERT(r <= 0xFF);
SkASSERT(g <= 0xFF);
SkASSERT(b <= 0xFF);
unsigned dr = SkAbs32(SkGetPackedR32(c) - r);
unsigned dg = SkAbs32(SkGetPackedG32(c) - g);
unsigned db = SkAbs32(SkGetPackedB32(c) - b);
return SkMax32(dr, SkMax32(dg, db));
}
static int scale_dist_14(int dist, uint32_t mul, uint32_t sub) {
int tmp = dist * mul - sub;
int result = (tmp + (1 << 13)) >> 14;
return result;
}
static inline unsigned Accurate255To256(unsigned x) {
return x + (x >> 7);
}
void SkAvoidXfermode::xfer32(SkPMColor dst[], const SkPMColor src[], int count,
const SkAlpha aa[]) const {
unsigned opR = SkColorGetR(fOpColor);
unsigned opG = SkColorGetG(fOpColor);
unsigned opB = SkColorGetB(fOpColor);
uint32_t mul = fDistMul;
uint32_t sub = (fDistMul - (1 << 14)) << 8;
int MAX, mask;
if (kTargetColor_Mode == fMode) {
mask = -1;
MAX = 255;
} else {
mask = 0;
MAX = 0;
}
for (int i = 0; i < count; i++) {
int d = color_dist32(dst[i], opR, opG, opB);
// now reverse d if we need to
d = MAX + (d ^ mask) - mask;
SkASSERT((unsigned)d <= 255);
d = Accurate255To256(d);
d = scale_dist_14(d, mul, sub);
SkASSERT(d <= 256);
if (d > 0) {
if (aa) {
d = SkAlphaMul(d, Accurate255To256(*aa++));
if (0 == d) {
continue;
}
}
dst[i] = SkFourByteInterp256(src[i], dst[i], d);
}
}
}
static inline U16CPU SkBlend3216(SkPMColor src, U16CPU dst, unsigned scale) {
SkASSERT(scale <= 32);
scale <<= 3;
return SkPackRGB16(SkAlphaBlend(SkPacked32ToR16(src), SkGetPackedR16(dst), scale),
SkAlphaBlend(SkPacked32ToG16(src), SkGetPackedG16(dst), scale),
SkAlphaBlend(SkPacked32ToB16(src), SkGetPackedB16(dst), scale));
}
void SkAvoidXfermode::xfer16(uint16_t dst[], const SkPMColor src[], int count,
const SkAlpha aa[]) const {
unsigned opR = SkColorGetR(fOpColor) >> (8 - SK_R16_BITS);
unsigned opG = SkColorGetG(fOpColor) >> (8 - SK_G16_BITS);
unsigned opB = SkColorGetB(fOpColor) >> (8 - SK_R16_BITS);
uint32_t mul = fDistMul;
uint32_t sub = (fDistMul - (1 << 14)) << SK_R16_BITS;
int MAX, mask;
if (kTargetColor_Mode == fMode) {
mask = -1;
MAX = 31;
} else {
mask = 0;
MAX = 0;
}
for (int i = 0; i < count; i++) {
int d = color_dist16(dst[i], opR, opG, opB);
// now reverse d if we need to
d = MAX + (d ^ mask) - mask;
SkASSERT((unsigned)d <= 31);
// convert from 0..31 to 0..32
d += d >> 4;
d = scale_dist_14(d, mul, sub);
SkASSERT(d <= 32);
if (d > 0) {
if (aa) {
d = SkAlphaMul(d, Accurate255To256(*aa++));
if (0 == d) {
continue;
}
}
dst[i] = SkBlend3216(src[i], dst[i], d);
}
}
}
void SkAvoidXfermode::xferA8(SkAlpha dst[], const SkPMColor src[], int count,
const SkAlpha aa[]) const {
}
#if SK_SUPPORT_GPU
#include "GrFragmentProcessor.h"
#include "GrInvariantOutput.h"
#include "GrXferProcessor.h"
#include "glsl/GrGLSLFragmentProcessor.h"
#include "glsl/GrGLSLFragmentShaderBuilder.h"
#include "glsl/GrGLSLUniformHandler.h"
#include "glsl/GrGLSLXferProcessor.h"
///////////////////////////////////////////////////////////////////////////////
// Fragment Processor
///////////////////////////////////////////////////////////////////////////////
class GLAvoidFP;
class AvoidFP : public GrFragmentProcessor {
public:
static const GrFragmentProcessor* Create(SkColor opColor, uint8_t tolerance,
SkAvoidXfermode::Mode mode,
const GrFragmentProcessor* dst) {
return new AvoidFP(opColor, tolerance, mode, dst);
}
~AvoidFP() override { }
const char* name() const override { return "Avoid"; }
SkString dumpInfo() const override {
SkString str;
str.appendf("Color: 0x%08x Tol: %d Mode: %s",
fOpColor, fTolerance,
fMode == SkAvoidXfermode::kAvoidColor_Mode ? "Avoid" : "Target");
return str;
}
SkColor opColor() const { return fOpColor; }
uint8_t tol() const { return fTolerance; }
SkAvoidXfermode::Mode mode() const { return fMode; }
private:
GrGLSLFragmentProcessor* onCreateGLSLInstance() const override;
void onGetGLSLProcessorKey(const GrGLSLCaps&, GrProcessorKeyBuilder*) const override;
bool onIsEqual(const GrFragmentProcessor& fpBase) const override {
const AvoidFP& fp = fpBase.cast<AvoidFP>();
return fOpColor == fp.fOpColor &&
fTolerance == fp.fTolerance &&
fMode == fp.fMode;
}
void onComputeInvariantOutput(GrInvariantOutput* inout) const override {
inout->setToUnknown(GrInvariantOutput::kWill_ReadInput);
}
AvoidFP(SkColor opColor, uint8_t tolerance,
SkAvoidXfermode::Mode mode, const GrFragmentProcessor* dst)
: fOpColor(opColor), fTolerance(tolerance), fMode(mode) {
this->initClassID<AvoidFP>();
SkASSERT(dst);
SkDEBUGCODE(int dstIndex = )this->registerChildProcessor(dst);
SkASSERT(0 == dstIndex);
}
SkColor fOpColor;
uint8_t fTolerance;
SkAvoidXfermode::Mode fMode;
GR_DECLARE_FRAGMENT_PROCESSOR_TEST;
typedef GrFragmentProcessor INHERITED;
};
///////////////////////////////////////////////////////////////////////////////
// Add common code for calculating avoid's distance value
static void add_avoid_code(GrGLSLFragmentBuilder* fragBuilder,
const char* dstColor,
const char* srcCoverage,
const char* kColorAndTolUni,
const char* kCoverageName,
SkAvoidXfermode::Mode mode) {
fragBuilder->codeAppendf("vec3 temp = %s.rgb - %s.rgb;", dstColor, kColorAndTolUni);
fragBuilder->codeAppendf("float dist = max(max(abs(temp.r), abs(temp.g)), abs(temp.b));");
if (SkAvoidXfermode::kTargetColor_Mode == mode) {
fragBuilder->codeAppendf("dist = 1.0 - dist;");
}
// the 'a' portion of the uniform is the scaled and inverted tolerance
fragBuilder->codeAppendf("dist = dist * %s.a - (%s.a - 1.0);",
kColorAndTolUni, kColorAndTolUni);
fragBuilder->codeAppendf("vec4 %s = vec4(dist);", kCoverageName);
if (srcCoverage) {
fragBuilder->codeAppendf("%s *= %s;", kCoverageName, srcCoverage);
}
}
class GLAvoidFP : public GrGLSLFragmentProcessor {
public:
void emitCode(EmitArgs& args) override {
const AvoidFP& avoid = args.fFp.cast<AvoidFP>();
GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
SkString dstColor("dstColor");
this->emitChild(0, nullptr, &dstColor, args);
fColorAndTolUni = args.fUniformHandler->addUniform(
kFragment_GrShaderFlag,
kVec4f_GrSLType, kDefault_GrSLPrecision,
"colorAndTol");
const char* kColorAndTolUni = args.fUniformHandler->getUniformCStr(fColorAndTolUni);
const char* kCoverageName = "newCoverage";
// add_avoid_code emits the code needed to compute the new coverage
add_avoid_code(fragBuilder,
dstColor.c_str(), nullptr,
kColorAndTolUni, kCoverageName, avoid.mode());
// The raster implementation's quantization and behavior yield a very noticeable
// effect near zero (0.0039 = 1/256).
fragBuilder->codeAppendf("if (%s.r < 0.0039) { %s = %s; } else {",
kCoverageName, args.fOutputColor, dstColor.c_str());
fragBuilder->codeAppendf("%s = %s * %s + (vec4(1.0)-%s) * %s;",
args.fOutputColor,
kCoverageName, args.fInputColor ? args.fInputColor : "vec4(1.0)",
kCoverageName, dstColor.c_str());
fragBuilder->codeAppend("}");
}
static void GenKey(const GrProcessor& proc, const GrGLSLCaps&, GrProcessorKeyBuilder* b) {
const AvoidFP& avoid = proc.cast<AvoidFP>();
uint32_t key = avoid.mode() == SkAvoidXfermode::kTargetColor_Mode ? 1 : 0;
b->add32(key);
}
protected:
void onSetData(const GrGLSLProgramDataManager& pdman, const GrProcessor& proc) override {
const AvoidFP& avoid = proc.cast<AvoidFP>();
pdman.set4f(fColorAndTolUni,
SkColorGetR(avoid.opColor())/255.0f,
SkColorGetG(avoid.opColor())/255.0f,
SkColorGetB(avoid.opColor())/255.0f,
256.0f/(avoid.tol()+1.0f));
}
private:
GrGLSLProgramDataManager::UniformHandle fColorAndTolUni;
typedef GrGLSLFragmentProcessor INHERITED;
};
///////////////////////////////////////////////////////////////////////////////
GrGLSLFragmentProcessor* AvoidFP::onCreateGLSLInstance() const {
return new GLAvoidFP;
}
void AvoidFP::onGetGLSLProcessorKey(const GrGLSLCaps& caps, GrProcessorKeyBuilder* b) const {
GLAvoidFP::GenKey(*this, caps, b);
}
const GrFragmentProcessor* AvoidFP::TestCreate(GrProcessorTestData* d) {
SkColor opColor = d->fRandom->nextU();
uint8_t tolerance = d->fRandom->nextBits(8);
SkAvoidXfermode::Mode mode = d->fRandom->nextBool() ? SkAvoidXfermode::kAvoidColor_Mode
: SkAvoidXfermode::kTargetColor_Mode;
SkAutoTUnref<const GrFragmentProcessor> dst(GrProcessorUnitTest::CreateChildFP(d));
return new AvoidFP(opColor, tolerance, mode, dst);
}
GR_DEFINE_FRAGMENT_PROCESSOR_TEST(AvoidFP);
///////////////////////////////////////////////////////////////////////////////
// Xfer Processor
///////////////////////////////////////////////////////////////////////////////
class AvoidXP : public GrXferProcessor {
public:
AvoidXP(const DstTexture* dstTexture, bool hasMixedSamples,
SkColor opColor, uint8_t tolerance, SkAvoidXfermode::Mode mode)
: INHERITED(dstTexture, true, hasMixedSamples)
, fOpColor(opColor)
, fTolerance(tolerance)
, fMode(mode) {
this->initClassID<AvoidXP>();
}
const char* name() const override { return "Avoid"; }
GrGLSLXferProcessor* createGLSLInstance() const override;
SkColor opColor() const { return fOpColor; }
uint8_t tol() const { return fTolerance; }
SkAvoidXfermode::Mode mode() const { return fMode; }
private:
GrXferProcessor::OptFlags onGetOptimizations(const GrPipelineOptimizations& optimizations,
bool doesStencilWrite,
GrColor* overrideColor,
const GrCaps& caps) const override {
return GrXferProcessor::kNone_OptFlags;
}
void onGetGLSLProcessorKey(const GrGLSLCaps& caps, GrProcessorKeyBuilder* b) const override;
bool onIsEqual(const GrXferProcessor& xpBase) const override {
const AvoidXP& xp = xpBase.cast<AvoidXP>();
return fOpColor == xp.fOpColor &&
fTolerance == xp.fTolerance &&
fMode == xp.fMode;
}
SkColor fOpColor;
uint8_t fTolerance;
SkAvoidXfermode::Mode fMode;
typedef GrXferProcessor INHERITED;
};
///////////////////////////////////////////////////////////////////////////////
class GLAvoidXP : public GrGLSLXferProcessor {
public:
static void GenKey(const GrProcessor& processor, const GrGLSLCaps&, GrProcessorKeyBuilder* b) {
const AvoidXP& avoid = processor.cast<AvoidXP>();
uint32_t key = SkAvoidXfermode::kTargetColor_Mode == avoid.mode() ? 1 : 0;
b->add32(key);
}
private:
void emitBlendCodeForDstRead(GrGLSLXPFragmentBuilder* fragBuilder,
GrGLSLUniformHandler* uniformHandler,
const char* srcColor,
const char* srcCoverage,
const char* dstColor,
const char* outColor,
const char* outColorSecondary,
const GrXferProcessor& proc) override {
const AvoidXP& avoid = proc.cast<AvoidXP>();
fColorAndTolUni = uniformHandler->addUniform(kFragment_GrShaderFlag,
kVec4f_GrSLType, kDefault_GrSLPrecision,
"colorAndTol");
const char* kColorandTolUni = uniformHandler->getUniformCStr(fColorAndTolUni);
const char* kCoverageName = "newCoverage";
// add_avoid_code emits the code needed to compute the new coverage
add_avoid_code(fragBuilder,
dstColor, srcCoverage,
kColorandTolUni, kCoverageName, avoid.mode());
// The raster implementation's quantization and behavior yield a very noticeable
// effect near zero (0.0039 = 1/256).
fragBuilder->codeAppendf("if (%s.r < 0.0039) { %s = %s; } else {",
kCoverageName, outColor, dstColor);
fragBuilder->codeAppendf("%s = %s;", outColor, srcColor ? srcColor : "vec4(1.0)");
INHERITED::DefaultCoverageModulation(fragBuilder, kCoverageName, dstColor, outColor,
outColorSecondary, proc);
fragBuilder->codeAppend("}");
}
void onSetData(const GrGLSLProgramDataManager& pdman,
const GrXferProcessor& processor) override {
const AvoidXP& avoid = processor.cast<AvoidXP>();
pdman.set4f(fColorAndTolUni,
SkColorGetR(avoid.opColor())/255.0f,
SkColorGetG(avoid.opColor())/255.0f,
SkColorGetB(avoid.opColor())/255.0f,
256.0f/(avoid.tol()+1.0f));
};
GrGLSLProgramDataManager::UniformHandle fColorAndTolUni;
typedef GrGLSLXferProcessor INHERITED;
};
///////////////////////////////////////////////////////////////////////////////
void AvoidXP::onGetGLSLProcessorKey(const GrGLSLCaps& caps, GrProcessorKeyBuilder* b) const {
GLAvoidXP::GenKey(*this, caps, b);
}
GrGLSLXferProcessor* AvoidXP::createGLSLInstance() const { return new GLAvoidXP; }
///////////////////////////////////////////////////////////////////////////////
class GrAvoidXPFactory : public GrXPFactory {
public:
static GrXPFactory* Create(SkColor opColor, uint8_t tolerance,
SkAvoidXfermode::Mode mode) {
return new GrAvoidXPFactory(opColor, tolerance, mode);
}
void getInvariantBlendedColor(const GrProcOptInfo& colorPOI,
GrXPFactory::InvariantBlendedColor* blendedColor) const override {
blendedColor->fWillBlendWithDst = true;
blendedColor->fKnownColorFlags = kNone_GrColorComponentFlags;
}
private:
GrAvoidXPFactory(SkColor opColor, uint8_t tolerance, SkAvoidXfermode::Mode mode)
: fOpColor(opColor)
, fTolerance(tolerance)
, fMode(mode) {
this->initClassID<GrAvoidXPFactory>();
}
GrXferProcessor* onCreateXferProcessor(const GrCaps& caps,
const GrPipelineOptimizations& optimizations,
bool hasMixedSamples,
const DstTexture* dstTexture) const override {
return new AvoidXP(dstTexture, hasMixedSamples, fOpColor, fTolerance, fMode);
}
bool onWillReadDstColor(const GrCaps& caps,
const GrPipelineOptimizations& optimizations,
bool hasMixedSamples) const override {
return true;
}
bool onIsEqual(const GrXPFactory& xpfBase) const override {
const GrAvoidXPFactory& xpf = xpfBase.cast<GrAvoidXPFactory>();
return fOpColor == xpf.fOpColor &&
fTolerance == xpf.fTolerance &&
fMode == xpf.fMode;
}
GR_DECLARE_XP_FACTORY_TEST;
SkColor fOpColor;
uint8_t fTolerance;
SkAvoidXfermode::Mode fMode;
typedef GrXPFactory INHERITED;
};
GR_DEFINE_XP_FACTORY_TEST(GrAvoidXPFactory);
const GrXPFactory* GrAvoidXPFactory::TestCreate(GrProcessorTestData* d) {
SkColor opColor = d->fRandom->nextU();
uint8_t tolerance = d->fRandom->nextBits(8);
SkAvoidXfermode::Mode mode = d->fRandom->nextBool() ? SkAvoidXfermode::kAvoidColor_Mode
: SkAvoidXfermode::kTargetColor_Mode;
return GrAvoidXPFactory::Create(opColor, tolerance, mode);
}
///////////////////////////////////////////////////////////////////////////////
const GrFragmentProcessor* SkAvoidXfermode::getFragmentProcessorForImageFilter(
const GrFragmentProcessor* dst) const {
return AvoidFP::Create(fOpColor, fTolerance, fMode, dst);
}
GrXPFactory* SkAvoidXfermode::asXPFactory() const {
return GrAvoidXPFactory::Create(fOpColor, fTolerance, fMode);
}
#endif
#ifndef SK_IGNORE_TO_STRING
void SkAvoidXfermode::toString(SkString* str) const {
str->append("AvoidXfermode: opColor: ");
str->appendHex(fOpColor);
str->appendf("tolerance: %d ", fTolerance);
static const char* gModeStrings[] = { "Avoid", "Target" };
str->appendf("mode: %s", gModeStrings[fMode]);
}
#endif
|