• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2006 The Android Open Source Project
3  *
4  * Use of this source code is governed by a BSD-style license that can be
5  * found in the LICENSE file.
6  */
7 
8 #include "SkAvoidXfermode.h"
9 #include "SkColorPriv.h"
10 #include "SkReadBuffer.h"
11 #include "SkWriteBuffer.h"
12 #include "SkString.h"
13 
SkAvoidXfermode(SkColor opColor,U8CPU tolerance,Mode mode)14 SkAvoidXfermode::SkAvoidXfermode(SkColor opColor, U8CPU tolerance, Mode mode) {
15     if (tolerance > 255) {
16         tolerance = 255;
17     }
18     fTolerance = SkToU8(tolerance);
19     fOpColor = opColor;
20     fDistMul = (256 << 14) / (tolerance + 1);
21     fMode = mode;
22 }
23 
CreateProc(SkReadBuffer & buffer)24 SkFlattenable* SkAvoidXfermode::CreateProc(SkReadBuffer& buffer) {
25     const SkColor color = buffer.readColor();
26     const unsigned tolerance = buffer.readUInt();
27     const unsigned mode = buffer.readUInt();
28     return Create(color, tolerance, (Mode)mode);
29 }
30 
flatten(SkWriteBuffer & buffer) const31 void SkAvoidXfermode::flatten(SkWriteBuffer& buffer) const {
32     buffer.writeColor(fOpColor);
33     buffer.writeUInt(fTolerance);
34     buffer.writeUInt(fMode);
35 }
36 
37 // returns 0..31
color_dist16(uint16_t c,unsigned r,unsigned g,unsigned b)38 static unsigned color_dist16(uint16_t c, unsigned r, unsigned g, unsigned b) {
39     SkASSERT(r <= SK_R16_MASK);
40     SkASSERT(g <= SK_G16_MASK);
41     SkASSERT(b <= SK_B16_MASK);
42 
43     unsigned dr = SkAbs32(SkGetPackedR16(c) - r);
44     unsigned dg = SkAbs32(SkGetPackedG16(c) - g) >> (SK_G16_BITS - SK_R16_BITS);
45     unsigned db = SkAbs32(SkGetPackedB16(c) - b);
46 
47     return SkMax32(dr, SkMax32(dg, db));
48 }
49 
50 // returns 0..255
color_dist32(SkPMColor c,U8CPU r,U8CPU g,U8CPU b)51 static unsigned color_dist32(SkPMColor c, U8CPU r, U8CPU g, U8CPU b) {
52     SkASSERT(r <= 0xFF);
53     SkASSERT(g <= 0xFF);
54     SkASSERT(b <= 0xFF);
55 
56     unsigned dr = SkAbs32(SkGetPackedR32(c) - r);
57     unsigned dg = SkAbs32(SkGetPackedG32(c) - g);
58     unsigned db = SkAbs32(SkGetPackedB32(c) - b);
59 
60     return SkMax32(dr, SkMax32(dg, db));
61 }
62 
scale_dist_14(int dist,uint32_t mul,uint32_t sub)63 static int scale_dist_14(int dist, uint32_t mul, uint32_t sub) {
64     int tmp = dist * mul - sub;
65     int result = (tmp + (1 << 13)) >> 14;
66 
67     return result;
68 }
69 
Accurate255To256(unsigned x)70 static inline unsigned Accurate255To256(unsigned x) {
71     return x + (x >> 7);
72 }
73 
xfer32(SkPMColor dst[],const SkPMColor src[],int count,const SkAlpha aa[]) const74 void SkAvoidXfermode::xfer32(SkPMColor dst[], const SkPMColor src[], int count,
75                              const SkAlpha aa[]) const {
76     unsigned    opR = SkColorGetR(fOpColor);
77     unsigned    opG = SkColorGetG(fOpColor);
78     unsigned    opB = SkColorGetB(fOpColor);
79     uint32_t    mul = fDistMul;
80     uint32_t    sub = (fDistMul - (1 << 14)) << 8;
81 
82     int MAX, mask;
83 
84     if (kTargetColor_Mode == fMode) {
85         mask = -1;
86         MAX = 255;
87     } else {
88         mask = 0;
89         MAX = 0;
90     }
91 
92     for (int i = 0; i < count; i++) {
93         int d = color_dist32(dst[i], opR, opG, opB);
94         // now reverse d if we need to
95         d = MAX + (d ^ mask) - mask;
96         SkASSERT((unsigned)d <= 255);
97         d = Accurate255To256(d);
98 
99         d = scale_dist_14(d, mul, sub);
100         SkASSERT(d <= 256);
101 
102         if (d > 0) {
103             if (aa) {
104                 d = SkAlphaMul(d, Accurate255To256(*aa++));
105                 if (0 == d) {
106                     continue;
107                 }
108             }
109             dst[i] = SkFourByteInterp256(src[i], dst[i], d);
110         }
111     }
112 }
113 
SkBlend3216(SkPMColor src,U16CPU dst,unsigned scale)114 static inline U16CPU SkBlend3216(SkPMColor src, U16CPU dst, unsigned scale) {
115     SkASSERT(scale <= 32);
116     scale <<= 3;
117 
118     return SkPackRGB16(SkAlphaBlend(SkPacked32ToR16(src), SkGetPackedR16(dst), scale),
119         SkAlphaBlend(SkPacked32ToG16(src), SkGetPackedG16(dst), scale),
120         SkAlphaBlend(SkPacked32ToB16(src), SkGetPackedB16(dst), scale));
121 }
122 
xfer16(uint16_t dst[],const SkPMColor src[],int count,const SkAlpha aa[]) const123 void SkAvoidXfermode::xfer16(uint16_t dst[], const SkPMColor src[], int count,
124                              const SkAlpha aa[]) const {
125     unsigned    opR = SkColorGetR(fOpColor) >> (8 - SK_R16_BITS);
126     unsigned    opG = SkColorGetG(fOpColor) >> (8 - SK_G16_BITS);
127     unsigned    opB = SkColorGetB(fOpColor) >> (8 - SK_R16_BITS);
128     uint32_t    mul = fDistMul;
129     uint32_t    sub = (fDistMul - (1 << 14)) << SK_R16_BITS;
130 
131     int MAX, mask;
132 
133     if (kTargetColor_Mode == fMode) {
134         mask = -1;
135         MAX = 31;
136     } else {
137         mask = 0;
138         MAX = 0;
139     }
140 
141     for (int i = 0; i < count; i++) {
142         int d = color_dist16(dst[i], opR, opG, opB);
143         // now reverse d if we need to
144         d = MAX + (d ^ mask) - mask;
145         SkASSERT((unsigned)d <= 31);
146         // convert from 0..31 to 0..32
147         d += d >> 4;
148         d = scale_dist_14(d, mul, sub);
149         SkASSERT(d <= 32);
150 
151         if (d > 0) {
152             if (aa) {
153                 d = SkAlphaMul(d, Accurate255To256(*aa++));
154                 if (0 == d) {
155                     continue;
156                 }
157             }
158             dst[i] = SkBlend3216(src[i], dst[i], d);
159         }
160     }
161 }
162 
xferA8(SkAlpha dst[],const SkPMColor src[],int count,const SkAlpha aa[]) const163 void SkAvoidXfermode::xferA8(SkAlpha dst[], const SkPMColor src[], int count,
164                              const SkAlpha aa[]) const {
165 }
166 
167 
168 #if SK_SUPPORT_GPU
169 
170 #include "GrFragmentProcessor.h"
171 #include "GrInvariantOutput.h"
172 #include "GrXferProcessor.h"
173 #include "glsl/GrGLSLFragmentProcessor.h"
174 #include "glsl/GrGLSLFragmentShaderBuilder.h"
175 #include "glsl/GrGLSLUniformHandler.h"
176 #include "glsl/GrGLSLXferProcessor.h"
177 
178 ///////////////////////////////////////////////////////////////////////////////
179 // Fragment Processor
180 ///////////////////////////////////////////////////////////////////////////////
181 
182 class GLAvoidFP;
183 
184 class AvoidFP : public GrFragmentProcessor {
185 public:
Create(SkColor opColor,uint8_t tolerance,SkAvoidXfermode::Mode mode,const GrFragmentProcessor * dst)186     static const GrFragmentProcessor* Create(SkColor opColor, uint8_t tolerance,
187                                              SkAvoidXfermode::Mode mode,
188                                              const GrFragmentProcessor* dst) {
189         return new AvoidFP(opColor, tolerance, mode, dst);
190     }
191 
~AvoidFP()192     ~AvoidFP() override { }
193 
name() const194     const char* name() const override { return "Avoid"; }
195 
dumpInfo() const196     SkString dumpInfo() const override {
197         SkString str;
198         str.appendf("Color: 0x%08x Tol: %d Mode: %s",
199                     fOpColor, fTolerance,
200                     fMode == SkAvoidXfermode::kAvoidColor_Mode ? "Avoid" : "Target");
201         return str;
202     }
203 
opColor() const204     SkColor opColor() const { return fOpColor; }
tol() const205     uint8_t tol() const { return fTolerance; }
mode() const206     SkAvoidXfermode::Mode mode() const { return fMode; }
207 
208 private:
209     GrGLSLFragmentProcessor* onCreateGLSLInstance() const override;
210 
211     void onGetGLSLProcessorKey(const GrGLSLCaps&, GrProcessorKeyBuilder*) const override;
212 
onIsEqual(const GrFragmentProcessor & fpBase) const213     bool onIsEqual(const GrFragmentProcessor& fpBase) const override {
214         const AvoidFP& fp = fpBase.cast<AvoidFP>();
215 
216         return fOpColor == fp.fOpColor &&
217                fTolerance == fp.fTolerance &&
218                fMode == fp.fMode;
219     }
220 
onComputeInvariantOutput(GrInvariantOutput * inout) const221     void onComputeInvariantOutput(GrInvariantOutput* inout) const override {
222         inout->setToUnknown(GrInvariantOutput::kWill_ReadInput);
223     }
224 
AvoidFP(SkColor opColor,uint8_t tolerance,SkAvoidXfermode::Mode mode,const GrFragmentProcessor * dst)225     AvoidFP(SkColor opColor, uint8_t tolerance,
226             SkAvoidXfermode::Mode mode, const GrFragmentProcessor* dst)
227         : fOpColor(opColor), fTolerance(tolerance), fMode(mode) {
228         this->initClassID<AvoidFP>();
229 
230         SkASSERT(dst);
231         SkDEBUGCODE(int dstIndex = )this->registerChildProcessor(dst);
232         SkASSERT(0 == dstIndex);
233     }
234 
235     SkColor               fOpColor;
236     uint8_t               fTolerance;
237     SkAvoidXfermode::Mode fMode;
238 
239     GR_DECLARE_FRAGMENT_PROCESSOR_TEST;
240     typedef GrFragmentProcessor INHERITED;
241 };
242 
243 ///////////////////////////////////////////////////////////////////////////////
244 
245 // Add common code for calculating avoid's distance value
add_avoid_code(GrGLSLFragmentBuilder * fragBuilder,const char * dstColor,const char * srcCoverage,const char * kColorAndTolUni,const char * kCoverageName,SkAvoidXfermode::Mode mode)246 static void add_avoid_code(GrGLSLFragmentBuilder* fragBuilder,
247                            const char* dstColor,
248                            const char* srcCoverage,
249                            const char* kColorAndTolUni,
250                            const char* kCoverageName,
251                            SkAvoidXfermode::Mode mode) {
252 
253     fragBuilder->codeAppendf("vec3 temp = %s.rgb - %s.rgb;", dstColor, kColorAndTolUni);
254     fragBuilder->codeAppendf("float dist = max(max(abs(temp.r), abs(temp.g)), abs(temp.b));");
255 
256     if (SkAvoidXfermode::kTargetColor_Mode == mode) {
257         fragBuilder->codeAppendf("dist = 1.0 - dist;");
258     }
259 
260     // the 'a' portion of the uniform is the scaled and inverted tolerance
261     fragBuilder->codeAppendf("dist = dist * %s.a - (%s.a - 1.0);",
262                              kColorAndTolUni, kColorAndTolUni);
263 
264     fragBuilder->codeAppendf("vec4 %s = vec4(dist);", kCoverageName);
265     if (srcCoverage) {
266         fragBuilder->codeAppendf("%s *= %s;", kCoverageName, srcCoverage);
267     }
268 }
269 
270 class GLAvoidFP : public GrGLSLFragmentProcessor {
271 public:
emitCode(EmitArgs & args)272     void emitCode(EmitArgs& args) override {
273         const AvoidFP& avoid = args.fFp.cast<AvoidFP>();
274 
275         GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
276         SkString dstColor("dstColor");
277         this->emitChild(0, nullptr, &dstColor, args);
278 
279         fColorAndTolUni = args.fUniformHandler->addUniform(
280                                                  kFragment_GrShaderFlag,
281                                                  kVec4f_GrSLType, kDefault_GrSLPrecision,
282                                                  "colorAndTol");
283         const char* kColorAndTolUni = args.fUniformHandler->getUniformCStr(fColorAndTolUni);
284 
285         const char* kCoverageName = "newCoverage";
286 
287         // add_avoid_code emits the code needed to compute the new coverage
288         add_avoid_code(fragBuilder,
289                        dstColor.c_str(), nullptr,
290                        kColorAndTolUni, kCoverageName, avoid.mode());
291 
292         // The raster implementation's quantization and behavior yield a very noticeable
293         // effect near zero (0.0039 = 1/256).
294         fragBuilder->codeAppendf("if (%s.r < 0.0039) { %s = %s; } else {",
295                                  kCoverageName, args.fOutputColor, dstColor.c_str());
296         fragBuilder->codeAppendf("%s = %s * %s + (vec4(1.0)-%s) * %s;",
297                                  args.fOutputColor,
298                                  kCoverageName, args.fInputColor ? args.fInputColor : "vec4(1.0)",
299                                  kCoverageName, dstColor.c_str());
300         fragBuilder->codeAppend("}");
301     }
302 
GenKey(const GrProcessor & proc,const GrGLSLCaps &,GrProcessorKeyBuilder * b)303     static void GenKey(const GrProcessor& proc, const GrGLSLCaps&, GrProcessorKeyBuilder* b) {
304         const AvoidFP& avoid = proc.cast<AvoidFP>();
305         uint32_t key = avoid.mode() == SkAvoidXfermode::kTargetColor_Mode ? 1 : 0;
306         b->add32(key);
307     }
308 
309 protected:
onSetData(const GrGLSLProgramDataManager & pdman,const GrProcessor & proc)310     void onSetData(const GrGLSLProgramDataManager& pdman, const GrProcessor& proc) override {
311         const AvoidFP& avoid = proc.cast<AvoidFP>();
312         pdman.set4f(fColorAndTolUni,
313                     SkColorGetR(avoid.opColor())/255.0f,
314                     SkColorGetG(avoid.opColor())/255.0f,
315                     SkColorGetB(avoid.opColor())/255.0f,
316                     256.0f/(avoid.tol()+1.0f));
317     }
318 
319 private:
320     GrGLSLProgramDataManager::UniformHandle fColorAndTolUni;
321 
322     typedef GrGLSLFragmentProcessor INHERITED;
323 };
324 
325 ///////////////////////////////////////////////////////////////////////////////
326 
onCreateGLSLInstance() const327 GrGLSLFragmentProcessor* AvoidFP::onCreateGLSLInstance() const {
328     return new GLAvoidFP;
329 }
330 
onGetGLSLProcessorKey(const GrGLSLCaps & caps,GrProcessorKeyBuilder * b) const331 void AvoidFP::onGetGLSLProcessorKey(const GrGLSLCaps& caps, GrProcessorKeyBuilder* b) const {
332     GLAvoidFP::GenKey(*this, caps, b);
333 }
334 
TestCreate(GrProcessorTestData * d)335 const GrFragmentProcessor* AvoidFP::TestCreate(GrProcessorTestData* d) {
336     SkColor opColor = d->fRandom->nextU();
337     uint8_t tolerance = d->fRandom->nextBits(8);
338     SkAvoidXfermode::Mode mode = d->fRandom->nextBool() ? SkAvoidXfermode::kAvoidColor_Mode
339                                                         : SkAvoidXfermode::kTargetColor_Mode;
340 
341     SkAutoTUnref<const GrFragmentProcessor> dst(GrProcessorUnitTest::CreateChildFP(d));
342     return new AvoidFP(opColor, tolerance, mode, dst);
343 }
344 
345 GR_DEFINE_FRAGMENT_PROCESSOR_TEST(AvoidFP);
346 
347 ///////////////////////////////////////////////////////////////////////////////
348 // Xfer Processor
349 ///////////////////////////////////////////////////////////////////////////////
350 
351 class AvoidXP : public GrXferProcessor {
352 public:
AvoidXP(const DstTexture * dstTexture,bool hasMixedSamples,SkColor opColor,uint8_t tolerance,SkAvoidXfermode::Mode mode)353     AvoidXP(const DstTexture* dstTexture, bool hasMixedSamples,
354             SkColor opColor, uint8_t tolerance, SkAvoidXfermode::Mode mode)
355         : INHERITED(dstTexture, true, hasMixedSamples)
356         , fOpColor(opColor)
357         , fTolerance(tolerance)
358         , fMode(mode) {
359         this->initClassID<AvoidXP>();
360     }
361 
name() const362     const char* name() const override { return "Avoid"; }
363 
364     GrGLSLXferProcessor* createGLSLInstance() const override;
365 
opColor() const366     SkColor opColor() const { return fOpColor; }
tol() const367     uint8_t tol() const { return fTolerance; }
mode() const368     SkAvoidXfermode::Mode mode() const { return fMode; }
369 
370 private:
onGetOptimizations(const GrPipelineOptimizations & optimizations,bool doesStencilWrite,GrColor * overrideColor,const GrCaps & caps) const371     GrXferProcessor::OptFlags onGetOptimizations(const GrPipelineOptimizations& optimizations,
372                                                  bool doesStencilWrite,
373                                                  GrColor* overrideColor,
374                                                  const GrCaps& caps) const override {
375         return GrXferProcessor::kNone_OptFlags;
376     }
377 
378     void onGetGLSLProcessorKey(const GrGLSLCaps& caps, GrProcessorKeyBuilder* b) const override;
379 
onIsEqual(const GrXferProcessor & xpBase) const380     bool onIsEqual(const GrXferProcessor& xpBase) const override {
381         const AvoidXP& xp = xpBase.cast<AvoidXP>();
382 
383         return fOpColor == xp.fOpColor &&
384                fTolerance == xp.fTolerance &&
385                fMode == xp.fMode;
386     }
387 
388     SkColor               fOpColor;
389     uint8_t               fTolerance;
390     SkAvoidXfermode::Mode fMode;
391 
392     typedef GrXferProcessor INHERITED;
393 };
394 
395 ///////////////////////////////////////////////////////////////////////////////
396 
397 class GLAvoidXP : public GrGLSLXferProcessor {
398 public:
GenKey(const GrProcessor & processor,const GrGLSLCaps &,GrProcessorKeyBuilder * b)399     static void GenKey(const GrProcessor& processor, const GrGLSLCaps&, GrProcessorKeyBuilder* b) {
400         const AvoidXP& avoid = processor.cast<AvoidXP>();
401         uint32_t key = SkAvoidXfermode::kTargetColor_Mode == avoid.mode() ? 1 : 0;
402         b->add32(key);
403     }
404 
405 private:
emitBlendCodeForDstRead(GrGLSLXPFragmentBuilder * fragBuilder,GrGLSLUniformHandler * uniformHandler,const char * srcColor,const char * srcCoverage,const char * dstColor,const char * outColor,const char * outColorSecondary,const GrXferProcessor & proc)406     void emitBlendCodeForDstRead(GrGLSLXPFragmentBuilder* fragBuilder,
407                                  GrGLSLUniformHandler* uniformHandler,
408                                  const char* srcColor,
409                                  const char* srcCoverage,
410                                  const char* dstColor,
411                                  const char* outColor,
412                                  const char* outColorSecondary,
413                                  const GrXferProcessor& proc) override {
414         const AvoidXP& avoid = proc.cast<AvoidXP>();
415 
416         fColorAndTolUni = uniformHandler->addUniform(kFragment_GrShaderFlag,
417                                                      kVec4f_GrSLType, kDefault_GrSLPrecision,
418                                                      "colorAndTol");
419         const char* kColorandTolUni = uniformHandler->getUniformCStr(fColorAndTolUni);
420 
421         const char* kCoverageName = "newCoverage";
422 
423         // add_avoid_code emits the code needed to compute the new coverage
424         add_avoid_code(fragBuilder,
425                        dstColor, srcCoverage,
426                        kColorandTolUni, kCoverageName, avoid.mode());
427 
428         // The raster implementation's quantization and behavior yield a very noticeable
429         // effect near zero (0.0039 = 1/256).
430         fragBuilder->codeAppendf("if (%s.r < 0.0039) { %s = %s; } else {",
431                                  kCoverageName, outColor, dstColor);
432         fragBuilder->codeAppendf("%s = %s;", outColor, srcColor ? srcColor : "vec4(1.0)");
433         INHERITED::DefaultCoverageModulation(fragBuilder, kCoverageName, dstColor, outColor,
434                                              outColorSecondary, proc);
435         fragBuilder->codeAppend("}");
436     }
437 
onSetData(const GrGLSLProgramDataManager & pdman,const GrXferProcessor & processor)438     void onSetData(const GrGLSLProgramDataManager& pdman,
439                    const GrXferProcessor& processor) override {
440         const AvoidXP& avoid = processor.cast<AvoidXP>();
441         pdman.set4f(fColorAndTolUni,
442                     SkColorGetR(avoid.opColor())/255.0f,
443                     SkColorGetG(avoid.opColor())/255.0f,
444                     SkColorGetB(avoid.opColor())/255.0f,
445                     256.0f/(avoid.tol()+1.0f));
446     };
447 
448     GrGLSLProgramDataManager::UniformHandle fColorAndTolUni;
449 
450     typedef GrGLSLXferProcessor INHERITED;
451 };
452 
453 ///////////////////////////////////////////////////////////////////////////////
454 
onGetGLSLProcessorKey(const GrGLSLCaps & caps,GrProcessorKeyBuilder * b) const455 void AvoidXP::onGetGLSLProcessorKey(const GrGLSLCaps& caps, GrProcessorKeyBuilder* b) const {
456     GLAvoidXP::GenKey(*this, caps, b);
457 }
458 
createGLSLInstance() const459 GrGLSLXferProcessor* AvoidXP::createGLSLInstance() const { return new GLAvoidXP; }
460 
461 ///////////////////////////////////////////////////////////////////////////////
462 class GrAvoidXPFactory : public GrXPFactory {
463 public:
Create(SkColor opColor,uint8_t tolerance,SkAvoidXfermode::Mode mode)464     static GrXPFactory* Create(SkColor opColor, uint8_t tolerance,
465                                SkAvoidXfermode::Mode mode) {
466         return new GrAvoidXPFactory(opColor, tolerance, mode);
467     }
468 
getInvariantBlendedColor(const GrProcOptInfo & colorPOI,GrXPFactory::InvariantBlendedColor * blendedColor) const469     void getInvariantBlendedColor(const GrProcOptInfo& colorPOI,
470                                   GrXPFactory::InvariantBlendedColor* blendedColor) const override {
471         blendedColor->fWillBlendWithDst = true;
472         blendedColor->fKnownColorFlags = kNone_GrColorComponentFlags;
473     }
474 
475 private:
GrAvoidXPFactory(SkColor opColor,uint8_t tolerance,SkAvoidXfermode::Mode mode)476     GrAvoidXPFactory(SkColor opColor, uint8_t tolerance, SkAvoidXfermode::Mode mode)
477         : fOpColor(opColor)
478         , fTolerance(tolerance)
479         , fMode(mode) {
480         this->initClassID<GrAvoidXPFactory>();
481     }
482 
onCreateXferProcessor(const GrCaps & caps,const GrPipelineOptimizations & optimizations,bool hasMixedSamples,const DstTexture * dstTexture) const483     GrXferProcessor* onCreateXferProcessor(const GrCaps& caps,
484                                            const GrPipelineOptimizations& optimizations,
485                                            bool hasMixedSamples,
486                                            const DstTexture* dstTexture) const override {
487         return new AvoidXP(dstTexture, hasMixedSamples, fOpColor, fTolerance, fMode);
488     }
489 
onWillReadDstColor(const GrCaps & caps,const GrPipelineOptimizations & optimizations,bool hasMixedSamples) const490     bool onWillReadDstColor(const GrCaps& caps,
491                             const GrPipelineOptimizations& optimizations,
492                             bool hasMixedSamples) const override {
493         return true;
494     }
495 
onIsEqual(const GrXPFactory & xpfBase) const496     bool onIsEqual(const GrXPFactory& xpfBase) const override {
497         const GrAvoidXPFactory& xpf = xpfBase.cast<GrAvoidXPFactory>();
498         return fOpColor == xpf.fOpColor &&
499                fTolerance == xpf.fTolerance &&
500                fMode == xpf.fMode;
501     }
502 
503     GR_DECLARE_XP_FACTORY_TEST;
504 
505     SkColor               fOpColor;
506     uint8_t               fTolerance;
507     SkAvoidXfermode::Mode fMode;
508 
509     typedef GrXPFactory INHERITED;
510 };
511 
512 GR_DEFINE_XP_FACTORY_TEST(GrAvoidXPFactory);
513 
TestCreate(GrProcessorTestData * d)514 const GrXPFactory* GrAvoidXPFactory::TestCreate(GrProcessorTestData* d) {
515     SkColor opColor = d->fRandom->nextU();
516     uint8_t tolerance = d->fRandom->nextBits(8);
517     SkAvoidXfermode::Mode mode = d->fRandom->nextBool() ? SkAvoidXfermode::kAvoidColor_Mode
518                                                         : SkAvoidXfermode::kTargetColor_Mode;
519     return GrAvoidXPFactory::Create(opColor, tolerance, mode);
520 }
521 
522 ///////////////////////////////////////////////////////////////////////////////
523 
getFragmentProcessorForImageFilter(const GrFragmentProcessor * dst) const524 const GrFragmentProcessor* SkAvoidXfermode::getFragmentProcessorForImageFilter(
525                                                             const GrFragmentProcessor* dst) const {
526     return AvoidFP::Create(fOpColor, fTolerance, fMode, dst);
527 }
528 
asXPFactory() const529 GrXPFactory* SkAvoidXfermode::asXPFactory() const {
530     return GrAvoidXPFactory::Create(fOpColor, fTolerance, fMode);
531 }
532 #endif
533 
534 #ifndef SK_IGNORE_TO_STRING
toString(SkString * str) const535 void SkAvoidXfermode::toString(SkString* str) const {
536     str->append("AvoidXfermode: opColor: ");
537     str->appendHex(fOpColor);
538     str->appendf("tolerance: %d ", fTolerance);
539 
540     static const char* gModeStrings[] = { "Avoid", "Target" };
541 
542     str->appendf("mode: %s", gModeStrings[fMode]);
543 }
544 #endif
545