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

Side by Side Diff: src/effects/gradients/SkTwoPointRadialGradient.cpp

Issue 582963002: Solo gp (Closed) Base URL: https://skia.googlesource.com/skia.git@no_peb
Patch Set: rebase Created 6 years, 2 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 | « src/effects/gradients/SkTwoPointRadialGradient.h ('k') | src/gpu/GrAAConvexPathRenderer.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 /* 2 /*
3 * Copyright 2012 Google Inc. 3 * Copyright 2012 Google Inc.
4 * 4 *
5 * Use of this source code is governed by a BSD-style license that can be 5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file. 6 * found in the LICENSE file.
7 */ 7 */
8 8
9 #include "SkTwoPointRadialGradient.h" 9 #include "SkTwoPointRadialGradient.h"
10 10
(...skipping 379 matching lines...) Expand 10 before | Expand all | Expand 10 after
390 fOneOverTwoA = fA ? SkScalarInvert(fA * 2) : 0; 390 fOneOverTwoA = fA ? SkScalarInvert(fA * 2) : 0;
391 391
392 fPtsToUnit.setTranslate(-fCenter1.fX, -fCenter1.fY); 392 fPtsToUnit.setTranslate(-fCenter1.fX, -fCenter1.fY);
393 fPtsToUnit.postScale(inv, inv); 393 fPtsToUnit.postScale(inv, inv);
394 } 394 }
395 395
396 ///////////////////////////////////////////////////////////////////// 396 /////////////////////////////////////////////////////////////////////
397 397
398 #if SK_SUPPORT_GPU 398 #if SK_SUPPORT_GPU
399 399
400 #include "GrTBackendEffectFactory.h" 400 #include "GrTBackendProcessorFactory.h"
401 #include "gl/builders/GrGLProgramBuilder.h" 401 #include "gl/builders/GrGLProgramBuilder.h"
402 #include "SkGr.h" 402 #include "SkGr.h"
403 403
404 // For brevity 404 // For brevity
405 typedef GrGLProgramDataManager::UniformHandle UniformHandle; 405 typedef GrGLProgramDataManager::UniformHandle UniformHandle;
406 406
407 class GrGLRadial2Gradient : public GrGLGradientEffect { 407 class GrGLRadial2Gradient : public GrGLGradientEffect {
408 408
409 public: 409 public:
410 410
411 GrGLRadial2Gradient(const GrBackendEffectFactory& factory, const GrEffect&); 411 GrGLRadial2Gradient(const GrBackendProcessorFactory& factory, const GrProces sor&);
412 virtual ~GrGLRadial2Gradient() { } 412 virtual ~GrGLRadial2Gradient() { }
413 413
414 virtual void emitCode(GrGLProgramBuilder*, 414 virtual void emitCode(GrGLProgramBuilder*,
415 const GrEffect&, 415 const GrFragmentProcessor&,
416 const GrEffectKey&, 416 const GrProcessorKey&,
417 const char* outputColor, 417 const char* outputColor,
418 const char* inputColor, 418 const char* inputColor,
419 const TransformedCoordsArray&, 419 const TransformedCoordsArray&,
420 const TextureSamplerArray&) SK_OVERRIDE; 420 const TextureSamplerArray&) SK_OVERRIDE;
421 virtual void setData(const GrGLProgramDataManager&, const GrEffect&) SK_OVER RIDE; 421 virtual void setData(const GrGLProgramDataManager&, const GrProcessor&) SK_O VERRIDE;
422 422
423 static void GenKey(const GrEffect&, const GrGLCaps& caps, GrEffectKeyBuilder * b); 423 static void GenKey(const GrProcessor&, const GrGLCaps& caps, GrProcessorKeyB uilder* b);
424 424
425 protected: 425 protected:
426 426
427 UniformHandle fParamUni; 427 UniformHandle fParamUni;
428 428
429 const char* fVSVaryingName; 429 const char* fVSVaryingName;
430 const char* fFSVaryingName; 430 const char* fFSVaryingName;
431 431
432 bool fIsDegenerate; 432 bool fIsDegenerate;
433 433
434 // @{ 434 // @{
435 /// Values last uploaded as uniforms 435 /// Values last uploaded as uniforms
436 436
437 SkScalar fCachedCenter; 437 SkScalar fCachedCenter;
438 SkScalar fCachedRadius; 438 SkScalar fCachedRadius;
439 bool fCachedPosRoot; 439 bool fCachedPosRoot;
440 440
441 // @} 441 // @}
442 442
443 private: 443 private:
444 444
445 typedef GrGLGradientEffect INHERITED; 445 typedef GrGLGradientEffect INHERITED;
446 446
447 }; 447 };
448 448
449 ///////////////////////////////////////////////////////////////////// 449 /////////////////////////////////////////////////////////////////////
450 450
451 class GrRadial2Gradient : public GrGradientEffect { 451 class GrRadial2Gradient : public GrGradientEffect {
452 public: 452 public:
453 static GrEffect* Create(GrContext* ctx, 453 static GrFragmentProcessor* Create(GrContext* ctx,
454 const SkTwoPointRadialGradient& shader, 454 const SkTwoPointRadialGradient& shader,
455 const SkMatrix& matrix, 455 const SkMatrix& matrix,
456 SkShader::TileMode tm) { 456 SkShader::TileMode tm) {
457 return SkNEW_ARGS(GrRadial2Gradient, (ctx, shader, matrix, tm)); 457 return SkNEW_ARGS(GrRadial2Gradient, (ctx, shader, matrix, tm));
458 } 458 }
459 459
460 virtual ~GrRadial2Gradient() { } 460 virtual ~GrRadial2Gradient() { }
461 461
462 static const char* Name() { return "Two-Point Radial Gradient"; } 462 static const char* Name() { return "Two-Point Radial Gradient"; }
463 virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE { 463 virtual const GrBackendFragmentProcessorFactory& getFactory() const SK_OVERR IDE {
464 return GrTBackendEffectFactory<GrRadial2Gradient>::getInstance(); 464 return GrTBackendFragmentProcessorFactory<GrRadial2Gradient>::getInstanc e();
465 } 465 }
466 466
467 // The radial gradient parameters can collapse to a linear (instead of quadr atic) equation. 467 // The radial gradient parameters can collapse to a linear (instead of quadr atic) equation.
468 bool isDegenerate() const { return SK_Scalar1 == fCenterX1; } 468 bool isDegenerate() const { return SK_Scalar1 == fCenterX1; }
469 SkScalar center() const { return fCenterX1; } 469 SkScalar center() const { return fCenterX1; }
470 SkScalar radius() const { return fRadius0; } 470 SkScalar radius() const { return fRadius0; }
471 bool isPosRoot() const { return SkToBool(fPosRoot); } 471 bool isPosRoot() const { return SkToBool(fPosRoot); }
472 472
473 typedef GrGLRadial2Gradient GLEffect; 473 typedef GrGLRadial2Gradient GLProcessor;
474 474
475 private: 475 private:
476 virtual bool onIsEqual(const GrEffect& sBase) const SK_OVERRIDE { 476 virtual bool onIsEqual(const GrProcessor& sBase) const SK_OVERRIDE {
477 const GrRadial2Gradient& s = sBase.cast<GrRadial2Gradient>(); 477 const GrRadial2Gradient& s = sBase.cast<GrRadial2Gradient>();
478 return (INHERITED::onIsEqual(sBase) && 478 return (INHERITED::onIsEqual(sBase) &&
479 this->fCenterX1 == s.fCenterX1 && 479 this->fCenterX1 == s.fCenterX1 &&
480 this->fRadius0 == s.fRadius0 && 480 this->fRadius0 == s.fRadius0 &&
481 this->fPosRoot == s.fPosRoot); 481 this->fPosRoot == s.fPosRoot);
482 } 482 }
483 483
484 GrRadial2Gradient(GrContext* ctx, 484 GrRadial2Gradient(GrContext* ctx,
485 const SkTwoPointRadialGradient& shader, 485 const SkTwoPointRadialGradient& shader,
486 const SkMatrix& matrix, 486 const SkMatrix& matrix,
487 SkShader::TileMode tm) 487 SkShader::TileMode tm)
488 : INHERITED(ctx, shader, matrix, tm) 488 : INHERITED(ctx, shader, matrix, tm)
489 , fCenterX1(shader.getCenterX1()) 489 , fCenterX1(shader.getCenterX1())
490 , fRadius0(shader.getStartRadius()) 490 , fRadius0(shader.getStartRadius())
491 , fPosRoot(shader.getDiffRadius() < 0) { 491 , fPosRoot(shader.getDiffRadius() < 0) {
492 // We pass the linear part of the quadratic as a varying. 492 // We pass the linear part of the quadratic as a varying.
493 // float b = 2.0 * (fCenterX1 * x - fRadius0 * z) 493 // float b = 2.0 * (fCenterX1 * x - fRadius0 * z)
494 fBTransform = this->getCoordTransform(); 494 fBTransform = this->getCoordTransform();
495 SkMatrix& bMatrix = *fBTransform.accessMatrix(); 495 SkMatrix& bMatrix = *fBTransform.accessMatrix();
496 bMatrix[SkMatrix::kMScaleX] = 2 * (SkScalarMul(fCenterX1, bMatrix[SkMatr ix::kMScaleX]) - 496 bMatrix[SkMatrix::kMScaleX] = 2 * (SkScalarMul(fCenterX1, bMatrix[SkMatr ix::kMScaleX]) -
497 SkScalarMul(fRadius0, bMatrix[SkMatri x::kMPersp0])); 497 SkScalarMul(fRadius0, bMatrix[SkMatri x::kMPersp0]));
498 bMatrix[SkMatrix::kMSkewX] = 2 * (SkScalarMul(fCenterX1, bMatrix[SkMatri x::kMSkewX]) - 498 bMatrix[SkMatrix::kMSkewX] = 2 * (SkScalarMul(fCenterX1, bMatrix[SkMatri x::kMSkewX]) -
499 SkScalarMul(fRadius0, bMatrix[SkMatrix ::kMPersp1])); 499 SkScalarMul(fRadius0, bMatrix[SkMatrix ::kMPersp1]));
500 bMatrix[SkMatrix::kMTransX] = 2 * (SkScalarMul(fCenterX1, bMatrix[SkMatr ix::kMTransX]) - 500 bMatrix[SkMatrix::kMTransX] = 2 * (SkScalarMul(fCenterX1, bMatrix[SkMatr ix::kMTransX]) -
501 SkScalarMul(fRadius0, bMatrix[SkMatri x::kMPersp2])); 501 SkScalarMul(fRadius0, bMatrix[SkMatri x::kMPersp2]));
502 this->addCoordTransform(&fBTransform); 502 this->addCoordTransform(&fBTransform);
503 } 503 }
504 504
505 GR_DECLARE_EFFECT_TEST; 505 GR_DECLARE_FRAGMENT_PROCESSOR_TEST;
506 506
507 // @{ 507 // @{
508 // Cache of values - these can change arbitrarily, EXCEPT 508 // Cache of values - these can change arbitrarily, EXCEPT
509 // we shouldn't change between degenerate and non-degenerate?! 509 // we shouldn't change between degenerate and non-degenerate?!
510 510
511 GrCoordTransform fBTransform; 511 GrCoordTransform fBTransform;
512 SkScalar fCenterX1; 512 SkScalar fCenterX1;
513 SkScalar fRadius0; 513 SkScalar fRadius0;
514 SkBool8 fPosRoot; 514 SkBool8 fPosRoot;
515 515
516 // @} 516 // @}
517 517
518 typedef GrGradientEffect INHERITED; 518 typedef GrGradientEffect INHERITED;
519 }; 519 };
520 520
521 ///////////////////////////////////////////////////////////////////// 521 /////////////////////////////////////////////////////////////////////
522 522
523 GR_DEFINE_EFFECT_TEST(GrRadial2Gradient); 523 GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrRadial2Gradient);
524 524
525 GrEffect* GrRadial2Gradient::TestCreate(SkRandom* random, 525 GrFragmentProcessor* GrRadial2Gradient::TestCreate(SkRandom* random,
526 GrContext* context, 526 GrContext* context,
527 const GrDrawTargetCaps&, 527 const GrDrawTargetCaps&,
528 GrTexture**) { 528 GrTexture**) {
529 SkPoint center1 = {random->nextUScalar1(), random->nextUScalar1()}; 529 SkPoint center1 = {random->nextUScalar1(), random->nextUScalar1()};
530 SkScalar radius1 = random->nextUScalar1(); 530 SkScalar radius1 = random->nextUScalar1();
531 SkPoint center2; 531 SkPoint center2;
532 SkScalar radius2; 532 SkScalar radius2;
533 do { 533 do {
534 center2.set(random->nextUScalar1(), random->nextUScalar1()); 534 center2.set(random->nextUScalar1(), random->nextUScalar1());
535 radius2 = random->nextUScalar1 (); 535 radius2 = random->nextUScalar1 ();
536 // There is a bug in two point radial gradients with identical radii 536 // There is a bug in two point radial gradients with identical radii
537 } while (radius1 == radius2); 537 } while (radius1 == radius2);
538 538
539 SkColor colors[kMaxRandomGradientColors]; 539 SkColor colors[kMaxRandomGradientColors];
540 SkScalar stopsArray[kMaxRandomGradientColors]; 540 SkScalar stopsArray[kMaxRandomGradientColors];
541 SkScalar* stops = stopsArray; 541 SkScalar* stops = stopsArray;
542 SkShader::TileMode tm; 542 SkShader::TileMode tm;
543 int colorCount = RandomGradientParams(random, colors, &stops, &tm); 543 int colorCount = RandomGradientParams(random, colors, &stops, &tm);
544 SkAutoTUnref<SkShader> shader(SkGradientShader::CreateTwoPointRadial(center1 , radius1, 544 SkAutoTUnref<SkShader> shader(SkGradientShader::CreateTwoPointRadial(center1 , radius1,
545 center2 , radius2, 545 center2 , radius2,
546 colors, stops, colorCount, 546 colors, stops, colorCount,
547 tm)); 547 tm));
548 SkPaint paint; 548 SkPaint paint;
549 GrEffect* effect; 549 GrFragmentProcessor* fp;
550 GrColor paintColor; 550 GrColor paintColor;
551 SkAssertResult(shader->asNewEffect(context, paint, NULL, &paintColor, &effec t)); 551 SkAssertResult(shader->asFragmentProcessor(context, paint, NULL, &paintColor , &fp));
552 return effect; 552 return fp;
553 } 553 }
554 554
555 ///////////////////////////////////////////////////////////////////// 555 /////////////////////////////////////////////////////////////////////
556 556
557 GrGLRadial2Gradient::GrGLRadial2Gradient(const GrBackendEffectFactory& factory, 557 GrGLRadial2Gradient::GrGLRadial2Gradient(const GrBackendProcessorFactory& factor y,
558 const GrEffect& effect) 558 const GrProcessor& processor)
559 : INHERITED(factory) 559 : INHERITED(factory)
560 , fVSVaryingName(NULL) 560 , fVSVaryingName(NULL)
561 , fFSVaryingName(NULL) 561 , fFSVaryingName(NULL)
562 , fCachedCenter(SK_ScalarMax) 562 , fCachedCenter(SK_ScalarMax)
563 , fCachedRadius(-SK_ScalarMax) 563 , fCachedRadius(-SK_ScalarMax)
564 , fCachedPosRoot(0) { 564 , fCachedPosRoot(0) {
565 565
566 const GrRadial2Gradient& data = effect.cast<GrRadial2Gradient>(); 566 const GrRadial2Gradient& data = processor.cast<GrRadial2Gradient>();
567 fIsDegenerate = data.isDegenerate(); 567 fIsDegenerate = data.isDegenerate();
568 } 568 }
569 569
570 void GrGLRadial2Gradient::emitCode(GrGLProgramBuilder* builder, 570 void GrGLRadial2Gradient::emitCode(GrGLProgramBuilder* builder,
571 const GrEffect& effect, 571 const GrFragmentProcessor&,
572 const GrEffectKey& key, 572 const GrProcessorKey& key,
573 const char* outputColor, 573 const char* outputColor,
574 const char* inputColor, 574 const char* inputColor,
575 const TransformedCoordsArray& coords, 575 const TransformedCoordsArray& coords,
576 const TextureSamplerArray& samplers) { 576 const TextureSamplerArray& samplers) {
577 uint32_t baseKey = key.get32(0); 577 uint32_t baseKey = key.get32(0);
578 this->emitUniforms(builder, baseKey); 578 this->emitUniforms(builder, baseKey);
579 fParamUni = builder->addUniformArray(GrGLProgramBuilder::kFragment_Visibilit y, 579 fParamUni = builder->addUniformArray(GrGLProgramBuilder::kFragment_Visibilit y,
580 kFloat_GrSLType, "Radial2FSParams", 6); 580 kFloat_GrSLType, "Radial2FSParams", 6);
581 581
582 SkString cName("c"); 582 SkString cName("c");
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
635 rootName.c_str(), p1.c_str()); 635 rootName.c_str(), p1.c_str());
636 } else { 636 } else {
637 // t is: -c/b 637 // t is: -c/b
638 t.printf("-%s / %s", cName.c_str(), bVar.c_str()); 638 t.printf("-%s / %s", cName.c_str(), bVar.c_str());
639 } 639 }
640 640
641 this->emitColor(builder, t.c_str(), baseKey, outputColor, inputColor, sample rs); 641 this->emitColor(builder, t.c_str(), baseKey, outputColor, inputColor, sample rs);
642 } 642 }
643 643
644 void GrGLRadial2Gradient::setData(const GrGLProgramDataManager& pdman, 644 void GrGLRadial2Gradient::setData(const GrGLProgramDataManager& pdman,
645 const GrEffect& effect) { 645 const GrProcessor& processor) {
646 INHERITED::setData(pdman, effect); 646 INHERITED::setData(pdman, processor);
647 const GrRadial2Gradient& data = effect.cast<GrRadial2Gradient>(); 647 const GrRadial2Gradient& data = processor.cast<GrRadial2Gradient>();
648 SkASSERT(data.isDegenerate() == fIsDegenerate); 648 SkASSERT(data.isDegenerate() == fIsDegenerate);
649 SkScalar centerX1 = data.center(); 649 SkScalar centerX1 = data.center();
650 SkScalar radius0 = data.radius(); 650 SkScalar radius0 = data.radius();
651 if (fCachedCenter != centerX1 || 651 if (fCachedCenter != centerX1 ||
652 fCachedRadius != radius0 || 652 fCachedRadius != radius0 ||
653 fCachedPosRoot != data.isPosRoot()) { 653 fCachedPosRoot != data.isPosRoot()) {
654 654
655 SkScalar a = SkScalarMul(centerX1, centerX1) - SK_Scalar1; 655 SkScalar a = SkScalarMul(centerX1, centerX1) - SK_Scalar1;
656 656
657 // When we're in the degenerate (linear) case, the second 657 // When we're in the degenerate (linear) case, the second
(...skipping 10 matching lines...) Expand all
668 data.isPosRoot() ? 1.f : -1.f 668 data.isPosRoot() ? 1.f : -1.f
669 }; 669 };
670 670
671 pdman.set1fv(fParamUni, 6, values); 671 pdman.set1fv(fParamUni, 6, values);
672 fCachedCenter = centerX1; 672 fCachedCenter = centerX1;
673 fCachedRadius = radius0; 673 fCachedRadius = radius0;
674 fCachedPosRoot = data.isPosRoot(); 674 fCachedPosRoot = data.isPosRoot();
675 } 675 }
676 } 676 }
677 677
678 void GrGLRadial2Gradient::GenKey(const GrEffect& effect, 678 void GrGLRadial2Gradient::GenKey(const GrProcessor& processor,
679 const GrGLCaps&, GrEffectKeyBuilder* b) { 679 const GrGLCaps&, GrProcessorKeyBuilder* b) {
680 uint32_t* key = b->add32n(2); 680 uint32_t* key = b->add32n(2);
681 key[0] = GenBaseGradientKey(effect); 681 key[0] = GenBaseGradientKey(processor);
682 key[1] = effect.cast<GrRadial2Gradient>().isDegenerate(); 682 key[1] = processor.cast<GrRadial2Gradient>().isDegenerate();
683 } 683 }
684 684
685 ///////////////////////////////////////////////////////////////////// 685 /////////////////////////////////////////////////////////////////////
686 686
687 bool SkTwoPointRadialGradient::asNewEffect(GrContext* context, const SkPaint& pa int, 687 bool SkTwoPointRadialGradient::asFragmentProcessor(GrContext* context, const SkP aint& paint,
688 const SkMatrix* localMatrix, GrColor* paintColor, 688 const SkMatrix* localMatrix, GrColor* paintColor,
689 GrEffect** effect) const { 689 GrFragmentProcessor** fp) co nst {
690 SkASSERT(context); 690 SkASSERT(context);
691 691
692 // invert the localM, translate to center1 (fPtsToUni), rotate so center2 is on x axis. 692 // invert the localM, translate to center1 (fPtsToUni), rotate so center2 is on x axis.
693 SkMatrix matrix; 693 SkMatrix matrix;
694 if (!this->getLocalMatrix().invert(&matrix)) { 694 if (!this->getLocalMatrix().invert(&matrix)) {
695 return false; 695 return false;
696 } 696 }
697 if (localMatrix) { 697 if (localMatrix) {
698 SkMatrix inv; 698 SkMatrix inv;
699 if (!localMatrix->invert(&inv)) { 699 if (!localMatrix->invert(&inv)) {
700 return false; 700 return false;
701 } 701 }
702 matrix.postConcat(inv); 702 matrix.postConcat(inv);
703 } 703 }
704 matrix.postConcat(fPtsToUnit); 704 matrix.postConcat(fPtsToUnit);
705 705
706 SkScalar diffLen = fDiff.length(); 706 SkScalar diffLen = fDiff.length();
707 if (0 != diffLen) { 707 if (0 != diffLen) {
708 SkScalar invDiffLen = SkScalarInvert(diffLen); 708 SkScalar invDiffLen = SkScalarInvert(diffLen);
709 SkMatrix rot; 709 SkMatrix rot;
710 rot.setSinCos(-SkScalarMul(invDiffLen, fDiff.fY), 710 rot.setSinCos(-SkScalarMul(invDiffLen, fDiff.fY),
711 SkScalarMul(invDiffLen, fDiff.fX)); 711 SkScalarMul(invDiffLen, fDiff.fX));
712 matrix.postConcat(rot); 712 matrix.postConcat(rot);
713 } 713 }
714 714
715 *paintColor = SkColor2GrColorJustAlpha(paint.getColor()); 715 *paintColor = SkColor2GrColorJustAlpha(paint.getColor());
716 *effect = GrRadial2Gradient::Create(context, *this, matrix, fTileMode); 716 *fp = GrRadial2Gradient::Create(context, *this, matrix, fTileMode);
717 717
718 return true; 718 return true;
719 } 719 }
720 720
721 #else 721 #else
722 722
723 bool SkTwoPointRadialGradient::asNewEffect(GrContext* context, const SkPaint& pa int, 723 bool SkTwoPointRadialGradient::asFragmentProcessor(GrContext*, const SkPaint&, c onst SkMatrix*,
724 const SkMatrix* localMatrix, GrColor* paintColor, 724 GrColor*, GrFragmentProcessor **) const {
725 GrEffect** effect) const {
726 SkDEBUGFAIL("Should not call in GPU-less build"); 725 SkDEBUGFAIL("Should not call in GPU-less build");
727 return false; 726 return false;
728 } 727 }
729 728
730 #endif 729 #endif
OLDNEW
« no previous file with comments | « src/effects/gradients/SkTwoPointRadialGradient.h ('k') | src/gpu/GrAAConvexPathRenderer.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698