Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(803)

Side by Side Diff: src/effects/SkAvoidXfermode.cpp

Issue 1658623002: Add gpu implementation of SkAvoidXfermode (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Address comments Created 4 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « include/client/android/SkAvoidXfermode.h ('k') | src/effects/SkPixelXorXfermode.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright 2006 The Android Open Source Project 2 * Copyright 2006 The Android Open Source Project
3 * 3 *
4 * Use of this source code is governed by a BSD-style license that can be 4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file. 5 * found in the LICENSE file.
6 */ 6 */
7 7
8 #include "SkAvoidXfermode.h" 8 #include "SkAvoidXfermode.h"
9 #include "SkColorPriv.h" 9 #include "SkColorPriv.h"
10 #include "SkReadBuffer.h" 10 #include "SkReadBuffer.h"
(...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after
157 } 157 }
158 dst[i] = SkBlend3216(src[i], dst[i], d); 158 dst[i] = SkBlend3216(src[i], dst[i], d);
159 } 159 }
160 } 160 }
161 } 161 }
162 162
163 void SkAvoidXfermode::xferA8(SkAlpha dst[], const SkPMColor src[], int count, 163 void SkAvoidXfermode::xferA8(SkAlpha dst[], const SkPMColor src[], int count,
164 const SkAlpha aa[]) const { 164 const SkAlpha aa[]) const {
165 } 165 }
166 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:
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
192 ~AvoidFP() override { }
193
194 const char* name() const override { return "Avoid"; }
195
196 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" : "Targ et");
201 return str;
202 }
203
204 SkColor opColor() const { return fOpColor; }
205 uint8_t tol() const { return fTolerance; }
206 SkAvoidXfermode::Mode mode() const { return fMode; }
207
208 private:
209 GrGLSLFragmentProcessor* onCreateGLSLInstance() const override;
210
211 void onGetGLSLProcessorKey(const GrGLSLCaps&, GrProcessorKeyBuilder*) const override;
212
213 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
221 void onComputeInvariantOutput(GrInvariantOutput* inout) const override {
222 inout->setToUnknown(GrInvariantOutput::kWill_ReadInput);
223 }
224
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
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, kColorAnd TolUni);
254 fragBuilder->codeAppendf("float dist = max(max(abs(temp.r), abs(temp.g)), ab s(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:
272 void emitCode(EmitArgs& args) override {
273 const AvoidFP& avoid = args.fFp.cast<AvoidFP>();
274
275 GrGLSLFragmentBuilder* fragBuilder = args.fFragBuilder;
276 SkString dstColor("dstColor");
277 this->emitChild(0, nullptr, &dstColor, args);
278
279 fColorAndTolUni = args.fUniformHandler->addUniform(
280 GrGLSLUniformHandler::kFragment _Visibility,
281 kVec4f_GrSLType, kDefault_GrSLP recision,
282 "colorAndTol");
283 const char* kColorAndTolUni = args.fUniformHandler->getUniformCStr(fColo rAndTolUni);
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 no ticeable
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_st r());
296 fragBuilder->codeAppendf("%s = %s * %s + (vec4(1.0)-%s) * %s;",
297 args.fOutputColor,
298 kCoverageName, args.fInputColor ? args.fInputCo lor : "vec4(1.0)",
299 kCoverageName, dstColor.c_str());
300 fragBuilder->codeAppend("}");
301 }
302
303 static void GenKey(const GrProcessor& proc, const GrGLSLCaps&, GrProcessorKe yBuilder* 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:
310 void onSetData(const GrGLSLProgramDataManager& pdman, const GrProcessor& pro c) 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
327 GrGLSLFragmentProcessor* AvoidFP::onCreateGLSLInstance() const {
328 return new GLAvoidFP;
329 }
330
331 void AvoidFP::onGetGLSLProcessorKey(const GrGLSLCaps& caps, GrProcessorKeyBuilde r* b) const {
332 GLAvoidFP::GenKey(*this, caps, b);
333 }
334
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::kAvoi dColor_Mode
339 : SkAvoidXfermode::kTarg etColor_Mode;
340
341 SkAutoTUnref<const GrFragmentProcessor> dst(GrProcessorUnitTest::CreateChild FP(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:
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
362 const char* name() const override { return "Avoid"; }
363
364 GrGLSLXferProcessor* createGLSLInstance() const override;
365
366 SkColor opColor() const { return fOpColor; }
367 uint8_t tol() const { return fTolerance; }
368 SkAvoidXfermode::Mode mode() const { return fMode; }
369
370 private:
371 GrXferProcessor::OptFlags onGetOptimizations(const GrPipelineOptimizations& optimizations,
372 bool doesStencilWrite,
373 GrColor* overrideColor,
374 const GrCaps& caps) const overr ide {
375 return GrXferProcessor::kNone_OptFlags;
376 }
377
378 void onGetGLSLProcessorKey(const GrGLSLCaps& caps, GrProcessorKeyBuilder* b) const override;
379
380 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:
399 static void GenKey(const GrProcessor& processor, const GrGLSLCaps&, GrProces sorKeyBuilder* 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:
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(GrGLSLUniformHandler::kFrag ment_Visibility,
417 kVec4f_GrSLType, kDefault_G rSLPrecision,
418 "colorAndTol");
419 const char* kColorandTolUni = uniformHandler->getUniformCStr(fColorAndTo lUni);
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 no ticeable
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 : "ve c4(1.0)");
433 INHERITED::DefaultCoverageModulation(fragBuilder, kCoverageName, dstColo r, outColor,
434 outColorSecondary, proc);
435 fragBuilder->codeAppend("}");
436 }
437
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
455 void AvoidXP::onGetGLSLProcessorKey(const GrGLSLCaps& caps, GrProcessorKeyBuilde r* b) const {
456 GLAvoidXP::GenKey(*this, caps, b);
457 }
458
459 GrGLSLXferProcessor* AvoidXP::createGLSLInstance() const { return new GLAvoidXP; }
460
461 ///////////////////////////////////////////////////////////////////////////////
462 class GrAvoidXPFactory : public GrXPFactory {
463 public:
464 static GrXPFactory* Create(SkColor opColor, uint8_t tolerance,
465 SkAvoidXfermode::Mode mode) {
466 return new GrAvoidXPFactory(opColor, tolerance, mode);
467 }
468
469 void getInvariantBlendedColor(const GrProcOptInfo& colorPOI,
470 GrXPFactory::InvariantBlendedColor* blendedCol or) const override {
471 blendedColor->fWillBlendWithDst = true;
472 blendedColor->fKnownColorFlags = kNone_GrColorComponentFlags;
473 }
474
475 private:
476 GrAvoidXPFactory(SkColor opColor, uint8_t tolerance, SkAvoidXfermode::Mode m ode)
477 : fOpColor(opColor)
478 , fTolerance(tolerance)
479 , fMode(mode) {
480 this->initClassID<GrAvoidXPFactory>();
481 }
482
483 GrXferProcessor* onCreateXferProcessor(const GrCaps& caps,
484 const GrPipelineOptimizations& optimi zations,
485 bool hasMixedSamples,
486 const DstTexture* dstTexture) const o verride {
487 return new AvoidXP(dstTexture, hasMixedSamples, fOpColor, fTolerance, fM ode);
488 }
489
490 bool onWillReadDstColor(const GrCaps& caps,
491 const GrPipelineOptimizations& optimizations,
492 bool hasMixedSamples) const override {
493 return true;
494 }
495
496 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
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::kAvoi dColor_Mode
518 : SkAvoidXfermode::kTarg etColor_Mode;
519 return GrAvoidXPFactory::Create(opColor, tolerance, mode);
520 }
521
522 ///////////////////////////////////////////////////////////////////////////////
523
524 bool SkAvoidXfermode::asFragmentProcessor(const GrFragmentProcessor** output,
525 const GrFragmentProcessor* dst) const {
526 if (output) {
527 *output = AvoidFP::Create(fOpColor, fTolerance, fMode, dst);
528 }
529 return true;
530 }
531
532 bool SkAvoidXfermode::asXPFactory(GrXPFactory** xpf) const {
533 if (xpf) {
534 *xpf = GrAvoidXPFactory::Create(fOpColor, fTolerance, fMode);
535 }
536 return true;
537 }
538 #endif
539
167 #ifndef SK_IGNORE_TO_STRING 540 #ifndef SK_IGNORE_TO_STRING
168 void SkAvoidXfermode::toString(SkString* str) const { 541 void SkAvoidXfermode::toString(SkString* str) const {
169 str->append("AvoidXfermode: opColor: "); 542 str->append("AvoidXfermode: opColor: ");
170 str->appendHex(fOpColor); 543 str->appendHex(fOpColor);
171 str->appendf("distMul: %d ", fDistMul); 544 str->appendf("tolerance: %d ", fTolerance);
172 545
173 static const char* gModeStrings[] = { "Avoid", "Target" }; 546 static const char* gModeStrings[] = { "Avoid", "Target" };
174 547
175 str->appendf("mode: %s", gModeStrings[fMode]); 548 str->appendf("mode: %s", gModeStrings[fMode]);
176 } 549 }
177 #endif 550 #endif
OLDNEW
« no previous file with comments | « include/client/android/SkAvoidXfermode.h ('k') | src/effects/SkPixelXorXfermode.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698