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

Side by Side Diff: src/gpu/SkGr.cpp

Issue 2041113004: sk_sp for gpu. (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Reserve correctly. Created 4 years, 6 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/gpu/SkGpuDevice_drawTexture.cpp ('k') | src/gpu/SkGrPriv.h » ('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 2010 Google Inc. 2 * Copyright 2010 Google Inc.
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 8
9 #include "SkGr.h" 9 #include "SkGr.h"
10 #include "SkGrPriv.h" 10 #include "SkGrPriv.h"
(...skipping 488 matching lines...) Expand 10 before | Expand all | Expand 10 after
499 if (primitiveIsSrc) { 499 if (primitiveIsSrc) {
500 return SkXfermode::kSrc_Mode != mode; 500 return SkXfermode::kSrc_Mode != mode;
501 } else { 501 } else {
502 return SkXfermode::kDst_Mode != mode; 502 return SkXfermode::kDst_Mode != mode;
503 } 503 }
504 } 504 }
505 505
506 static inline bool skpaint_to_grpaint_impl(GrContext* context, 506 static inline bool skpaint_to_grpaint_impl(GrContext* context,
507 const SkPaint& skPaint, 507 const SkPaint& skPaint,
508 const SkMatrix& viewM, 508 const SkMatrix& viewM,
509 const GrFragmentProcessor** shaderPro cessor, 509 sk_sp<GrFragmentProcessor>* shaderPro cessor,
510 SkXfermode::Mode* primColorMode, 510 SkXfermode::Mode* primColorMode,
511 bool primitiveIsSrc, 511 bool primitiveIsSrc,
512 bool allowSRGBInputs, 512 bool allowSRGBInputs,
513 GrPaint* grPaint) { 513 GrPaint* grPaint) {
514 grPaint->setAntiAlias(skPaint.isAntiAlias()); 514 grPaint->setAntiAlias(skPaint.isAntiAlias());
515 grPaint->setAllowSRGBInputs(allowSRGBInputs); 515 grPaint->setAllowSRGBInputs(allowSRGBInputs);
516 516
517 // Setup the initial color considering the shader, the SkPaint color, and th e presence or not 517 // Setup the initial color considering the shader, the SkPaint color, and th e presence or not
518 // of per-vertex colors. 518 // of per-vertex colors.
519 SkAutoTUnref<const GrFragmentProcessor> aufp; 519 sk_sp<GrFragmentProcessor> shaderFP;
520 const GrFragmentProcessor* shaderFP = nullptr;
521 if (!primColorMode || blend_requires_shader(*primColorMode, primitiveIsSrc)) { 520 if (!primColorMode || blend_requires_shader(*primColorMode, primitiveIsSrc)) {
522 if (shaderProcessor) { 521 if (shaderProcessor) {
523 shaderFP = *shaderProcessor; 522 shaderFP = *shaderProcessor;
524 } else if (const SkShader* shader = skPaint.getShader()) { 523 } else if (const SkShader* shader = skPaint.getShader()) {
525 SkSourceGammaTreatment gammaTreatment = allowSRGBInputs 524 SkSourceGammaTreatment gammaTreatment = allowSRGBInputs
526 ? SkSourceGammaTreatment::kRespect : SkSourceGammaTreatment::kIg nore; 525 ? SkSourceGammaTreatment::kRespect : SkSourceGammaTreatment::kIg nore;
527 aufp.reset(shader->asFragmentProcessor(context, viewM, nullptr, 526 shaderFP = shader->asFragmentProcessor(context, viewM, nullptr,
528 skPaint.getFilterQuality(), g ammaTreatment)); 527 skPaint.getFilterQuality(), g ammaTreatment);
529 shaderFP = aufp;
530 if (!shaderFP) { 528 if (!shaderFP) {
531 return false; 529 return false;
532 } 530 }
533 } 531 }
534 } 532 }
535 533
536 // Set this in below cases if the output of the shader/paint-color/paint-alp ha/primXfermode is 534 // Set this in below cases if the output of the shader/paint-color/paint-alp ha/primXfermode is
537 // a known constant value. In that case we can simply apply a color filter d uring this 535 // a known constant value. In that case we can simply apply a color filter d uring this
538 // conversion without converting the color filter to a GrFragmentProcessor. 536 // conversion without converting the color filter to a GrFragmentProcessor.
539 bool applyColorFilterToPaintColor = false; 537 bool applyColorFilterToPaintColor = false;
540 if (shaderFP) { 538 if (shaderFP) {
541 if (primColorMode) { 539 if (primColorMode) {
542 // There is a blend between the primitive color and the shader color . The shader sees 540 // There is a blend between the primitive color and the shader color . The shader sees
543 // the opaque paint color. The shader's output is blended using the provided mode by 541 // the opaque paint color. The shader's output is blended using the provided mode by
544 // the primitive color. The blended color is then modulated by the p aint's alpha. 542 // the primitive color. The blended color is then modulated by the p aint's alpha.
545 543
546 // The geometry processor will insert the primitive color to start t he color chain, so 544 // The geometry processor will insert the primitive color to start t he color chain, so
547 // the GrPaint color will be ignored. 545 // the GrPaint color will be ignored.
548 546
549 GrColor shaderInput = SkColorToOpaqueGrColor(skPaint.getColor()); 547 GrColor shaderInput = SkColorToOpaqueGrColor(skPaint.getColor());
550 548
551 shaderFP = GrFragmentProcessor::OverrideInput(shaderFP, shaderInput) ; 549 shaderFP = GrFragmentProcessor::OverrideInput(shaderFP, shaderInput) ;
552 aufp.reset(shaderFP);
553
554 if (primitiveIsSrc) { 550 if (primitiveIsSrc) {
555 shaderFP = GrXfermodeFragmentProcessor::CreateFromDstProcessor(s haderFP, 551 shaderFP = GrXfermodeFragmentProcessor::MakeFromDstProcessor(std ::move(shaderFP),
556 * primColorMode); 552 *pr imColorMode);
557 } else { 553 } else {
558 shaderFP = GrXfermodeFragmentProcessor::CreateFromSrcProcessor(s haderFP, 554 shaderFP = GrXfermodeFragmentProcessor::MakeFromSrcProcessor(std ::move(shaderFP),
559 * primColorMode); 555 *pr imColorMode);
560 } 556 }
561 aufp.reset(shaderFP);
562 // The above may return null if compose results in a pass through of the prim color. 557 // The above may return null if compose results in a pass through of the prim color.
563 if (shaderFP) { 558 if (shaderFP) {
564 grPaint->addColorFragmentProcessor(shaderFP); 559 grPaint->addColorFragmentProcessor(shaderFP);
565 } 560 }
566 561
567 GrColor paintAlpha = SkColorAlphaToGrColor(skPaint.getColor()); 562 GrColor paintAlpha = SkColorAlphaToGrColor(skPaint.getColor());
568 if (GrColor_WHITE != paintAlpha) { 563 if (GrColor_WHITE != paintAlpha) {
569 grPaint->addColorFragmentProcessor(GrConstColorProcessor::Create ( 564 grPaint->addColorFragmentProcessor(GrConstColorProcessor::Make(
570 paintAlpha, GrConstColorProcessor::kModulateRGBA_InputMode)) ->unref(); 565 paintAlpha, GrConstColorProcessor::kModulateRGBA_InputMode)) ;
571 } 566 }
572 } else { 567 } else {
573 // The shader's FP sees the paint unpremul color 568 // The shader's FP sees the paint unpremul color
574 grPaint->setColor(SkColorToUnpremulGrColor(skPaint.getColor())); 569 grPaint->setColor(SkColorToUnpremulGrColor(skPaint.getColor()));
575 grPaint->addColorFragmentProcessor(shaderFP); 570 grPaint->addColorFragmentProcessor(std::move(shaderFP));
576 } 571 }
577 } else { 572 } else {
578 if (primColorMode) { 573 if (primColorMode) {
579 // There is a blend between the primitive color and the paint color. The blend considers 574 // There is a blend between the primitive color and the paint color. The blend considers
580 // the opaque paint color. The paint's alpha is applied to the post- blended color. 575 // the opaque paint color. The paint's alpha is applied to the post- blended color.
581 SkAutoTUnref<const GrFragmentProcessor> processor( 576 sk_sp<GrFragmentProcessor> processor(
582 GrConstColorProcessor::Create(SkColorToOpaqueGrColor(skPaint.get Color()), 577 GrConstColorProcessor::Make(SkColorToOpaqueGrColor(skPaint.getCo lor()),
583 GrConstColorProcessor::kIgnore_Inp utMode)); 578 GrConstColorProcessor::kIgnore_Inp utMode));
584 if (primitiveIsSrc) { 579 if (primitiveIsSrc) {
585 processor.reset(GrXfermodeFragmentProcessor::CreateFromDstProces sor(processor, 580 processor = GrXfermodeFragmentProcessor::MakeFromDstProcessor(st d::move(processor),
586 *primColorMode)); 581 *p rimColorMode);
587 } else { 582 } else {
588 processor.reset(GrXfermodeFragmentProcessor::CreateFromSrcProces sor(processor, 583 processor = GrXfermodeFragmentProcessor::MakeFromSrcProcessor(st d::move(processor),
589 *primColorMode)); 584 *p rimColorMode);
590
591 } 585 }
592 if (processor) { 586 if (processor) {
593 grPaint->addColorFragmentProcessor(processor); 587 grPaint->addColorFragmentProcessor(std::move(processor));
594 } 588 }
595 589
596 grPaint->setColor(SkColorToOpaqueGrColor(skPaint.getColor())); 590 grPaint->setColor(SkColorToOpaqueGrColor(skPaint.getColor()));
597 591
598 GrColor paintAlpha = SkColorAlphaToGrColor(skPaint.getColor()); 592 GrColor paintAlpha = SkColorAlphaToGrColor(skPaint.getColor());
599 if (GrColor_WHITE != paintAlpha) { 593 if (GrColor_WHITE != paintAlpha) {
600 grPaint->addColorFragmentProcessor(GrConstColorProcessor::Create ( 594 grPaint->addColorFragmentProcessor(GrConstColorProcessor::Make(
601 paintAlpha, GrConstColorProcessor::kModulateRGBA_InputMode)) ->unref(); 595 paintAlpha, GrConstColorProcessor::kModulateRGBA_InputMode)) ;
602 } 596 }
603 } else { 597 } else {
604 // No shader, no primitive color. 598 // No shader, no primitive color.
605 grPaint->setColor(SkColorToPremulGrColor(skPaint.getColor())); 599 grPaint->setColor(SkColorToPremulGrColor(skPaint.getColor()));
606 applyColorFilterToPaintColor = true; 600 applyColorFilterToPaintColor = true;
607 } 601 }
608 } 602 }
609 603
610 SkColorFilter* colorFilter = skPaint.getColorFilter(); 604 SkColorFilter* colorFilter = skPaint.getColorFilter();
611 if (colorFilter) { 605 if (colorFilter) {
612 if (applyColorFilterToPaintColor) { 606 if (applyColorFilterToPaintColor) {
613 grPaint->setColor(SkColorToPremulGrColor(colorFilter->filterColor(sk Paint.getColor()))); 607 grPaint->setColor(SkColorToPremulGrColor(colorFilter->filterColor(sk Paint.getColor())));
614 } else { 608 } else {
615 SkAutoTUnref<const GrFragmentProcessor> cfFP( 609 sk_sp<GrFragmentProcessor> cfFP(colorFilter->asFragmentProcessor(con text));
616 colorFilter->asFragmentProcessor(context));
617 if (cfFP) { 610 if (cfFP) {
618 grPaint->addColorFragmentProcessor(cfFP); 611 grPaint->addColorFragmentProcessor(std::move(cfFP));
619 } else { 612 } else {
620 return false; 613 return false;
621 } 614 }
622 } 615 }
623 } 616 }
624 617
625 // When the xfermode is null on the SkPaint (meaning kSrcOver) we need the X PFactory field on 618 // When the xfermode is null on the SkPaint (meaning kSrcOver) we need the X PFactory field on
626 // the GrPaint to also be null (also kSrcOver). 619 // the GrPaint to also be null (also kSrcOver).
627 SkASSERT(!grPaint->getXPFactory()); 620 SkASSERT(!grPaint->getXPFactory());
628 SkXfermode* xfermode = skPaint.getXfermode(); 621 SkXfermode* xfermode = skPaint.getXfermode();
629 if (xfermode) { 622 if (xfermode) {
630 // SafeUnref in case a new xfermode is added that returns null. 623 grPaint->setXPFactory(xfermode->asXPFactory());
631 // In such cases we will fall back to kSrcOver_Mode.
632 SkSafeUnref(grPaint->setXPFactory(xfermode->asXPFactory()));
633 } 624 }
634 625
635 #ifndef SK_IGNORE_GPU_DITHER 626 #ifndef SK_IGNORE_GPU_DITHER
636 if (skPaint.isDither() && grPaint->numColorFragmentProcessors() > 0) { 627 if (skPaint.isDither() && grPaint->numColorFragmentProcessors() > 0) {
637 grPaint->addColorFragmentProcessor(GrDitherEffect::Create())->unref(); 628 grPaint->addColorFragmentProcessor(GrDitherEffect::Make());
638 } 629 }
639 #endif 630 #endif
640 return true; 631 return true;
641 } 632 }
642 633
643 bool SkPaintToGrPaint(GrContext* context, const SkPaint& skPaint, const SkMatrix & viewM, 634 bool SkPaintToGrPaint(GrContext* context, const SkPaint& skPaint, const SkMatrix & viewM,
644 bool allowSRGBInputs, GrPaint* grPaint) { 635 bool allowSRGBInputs, GrPaint* grPaint) {
645 return skpaint_to_grpaint_impl(context, skPaint, viewM, nullptr, nullptr, fa lse, 636 return skpaint_to_grpaint_impl(context, skPaint, viewM, nullptr, nullptr, fa lse,
646 allowSRGBInputs, grPaint); 637 allowSRGBInputs, grPaint);
647 } 638 }
648 639
649 /** Replaces the SkShader (if any) on skPaint with the passed in GrFragmentProce ssor. */ 640 /** Replaces the SkShader (if any) on skPaint with the passed in GrFragmentProce ssor. */
650 bool SkPaintToGrPaintReplaceShader(GrContext* context, 641 bool SkPaintToGrPaintReplaceShader(GrContext* context,
651 const SkPaint& skPaint, 642 const SkPaint& skPaint,
652 const GrFragmentProcessor* shaderFP, 643 sk_sp<GrFragmentProcessor> shaderFP,
653 bool allowSRGBInputs, 644 bool allowSRGBInputs,
654 GrPaint* grPaint) { 645 GrPaint* grPaint) {
655 if (!shaderFP) { 646 if (!shaderFP) {
656 return false; 647 return false;
657 } 648 }
658 return skpaint_to_grpaint_impl(context, skPaint, SkMatrix::I(), &shaderFP, n ullptr, false, 649 return skpaint_to_grpaint_impl(context, skPaint, SkMatrix::I(), &shaderFP, n ullptr, false,
659 allowSRGBInputs, grPaint); 650 allowSRGBInputs, grPaint);
660 } 651 }
661 652
662 /** Ignores the SkShader (if any) on skPaint. */ 653 /** Ignores the SkShader (if any) on skPaint. */
663 bool SkPaintToGrPaintNoShader(GrContext* context, 654 bool SkPaintToGrPaintNoShader(GrContext* context,
664 const SkPaint& skPaint, 655 const SkPaint& skPaint,
665 bool allowSRGBInputs, 656 bool allowSRGBInputs,
666 GrPaint* grPaint) { 657 GrPaint* grPaint) {
667 // Use a ptr to a nullptr to to indicate that the SkShader is ignored and no t replaced. 658 // Use a ptr to a nullptr to to indicate that the SkShader is ignored and no t replaced.
668 static const GrFragmentProcessor* kNullShaderFP = nullptr; 659 static sk_sp<GrFragmentProcessor> kNullShaderFP(nullptr);
669 static const GrFragmentProcessor** kIgnoreShader = &kNullShaderFP; 660 static sk_sp<GrFragmentProcessor>* kIgnoreShader = &kNullShaderFP;
670 return skpaint_to_grpaint_impl(context, skPaint, SkMatrix::I(), kIgnoreShade r, nullptr, false, 661 return skpaint_to_grpaint_impl(context, skPaint, SkMatrix::I(), kIgnoreShade r, nullptr, false,
671 allowSRGBInputs, grPaint); 662 allowSRGBInputs, grPaint);
672 } 663 }
673 664
674 /** Blends the SkPaint's shader (or color if no shader) with a per-primitive col or which must 665 /** Blends the SkPaint's shader (or color if no shader) with a per-primitive col or which must
675 be setup as a vertex attribute using the specified SkXfermode::Mode. */ 666 be setup as a vertex attribute using the specified SkXfermode::Mode. */
676 bool SkPaintToGrPaintWithXfermode(GrContext* context, 667 bool SkPaintToGrPaintWithXfermode(GrContext* context,
677 const SkPaint& skPaint, 668 const SkPaint& skPaint,
678 const SkMatrix& viewM, 669 const SkMatrix& viewM,
679 SkXfermode::Mode primColorMode, 670 SkXfermode::Mode primColorMode,
680 bool primitiveIsSrc, 671 bool primitiveIsSrc,
681 bool allowSRGBInputs, 672 bool allowSRGBInputs,
682 GrPaint* grPaint) { 673 GrPaint* grPaint) {
683 return skpaint_to_grpaint_impl(context, skPaint, viewM, nullptr, &primColorM ode, primitiveIsSrc, 674 return skpaint_to_grpaint_impl(context, skPaint, viewM, nullptr, &primColorM ode, primitiveIsSrc,
684 allowSRGBInputs, grPaint); 675 allowSRGBInputs, grPaint);
685 } 676 }
686 677
687 bool SkPaintToGrPaintWithTexture(GrContext* context, 678 bool SkPaintToGrPaintWithTexture(GrContext* context,
688 const SkPaint& paint, 679 const SkPaint& paint,
689 const SkMatrix& viewM, 680 const SkMatrix& viewM,
690 const GrFragmentProcessor* fp, 681 sk_sp<GrFragmentProcessor> fp,
691 bool textureIsAlphaOnly, 682 bool textureIsAlphaOnly,
692 bool allowSRGBInputs, 683 bool allowSRGBInputs,
693 GrPaint* grPaint) { 684 GrPaint* grPaint) {
694 SkAutoTUnref<const GrFragmentProcessor> shaderFP; 685 sk_sp<GrFragmentProcessor> shaderFP;
695 if (textureIsAlphaOnly) { 686 if (textureIsAlphaOnly) {
696 if (const SkShader* shader = paint.getShader()) { 687 if (const SkShader* shader = paint.getShader()) {
697 SkSourceGammaTreatment gammaTreatment = allowSRGBInputs 688 SkSourceGammaTreatment gammaTreatment = allowSRGBInputs
698 ? SkSourceGammaTreatment::kRespect : SkSourceGammaTreatment::kIg nore; 689 ? SkSourceGammaTreatment::kRespect : SkSourceGammaTreatment::kIg nore;
699 shaderFP.reset(shader->asFragmentProcessor(context, 690 shaderFP = shader->asFragmentProcessor(context,
700 viewM, 691 viewM,
701 nullptr, 692 nullptr,
702 paint.getFilterQuality(), 693 paint.getFilterQuality(),
703 gammaTreatment)); 694 gammaTreatment);
704 if (!shaderFP) { 695 if (!shaderFP) {
705 return false; 696 return false;
706 } 697 }
707 const GrFragmentProcessor* fpSeries[] = { shaderFP.get(), fp }; 698 sk_sp<GrFragmentProcessor> fpSeries[] = { std::move(shaderFP), std:: move(fp) };
708 shaderFP.reset(GrFragmentProcessor::RunInSeries(fpSeries, 2)); 699 shaderFP = GrFragmentProcessor::RunInSeries(fpSeries, 2);
709 } else { 700 } else {
710 shaderFP.reset(GrFragmentProcessor::MulOutputByInputUnpremulColor(fp )); 701 shaderFP = GrFragmentProcessor::MulOutputByInputUnpremulColor(fp);
711 } 702 }
712 } else { 703 } else {
713 shaderFP.reset(GrFragmentProcessor::MulOutputByInputAlpha(fp)); 704 shaderFP = GrFragmentProcessor::MulOutputByInputAlpha(fp);
714 } 705 }
715 706
716 return SkPaintToGrPaintReplaceShader(context, paint, shaderFP.get(), allowSR GBInputs, grPaint); 707 return SkPaintToGrPaintReplaceShader(context, paint, std::move(shaderFP), al lowSRGBInputs,
708 grPaint);
717 } 709 }
718 710
719 711
720 //////////////////////////////////////////////////////////////////////////////// //////////////// 712 //////////////////////////////////////////////////////////////////////////////// ////////////////
721 713
722 SkImageInfo GrMakeInfoFromTexture(GrTexture* tex, int w, int h, bool isOpaque) { 714 SkImageInfo GrMakeInfoFromTexture(GrTexture* tex, int w, int h, bool isOpaque) {
723 #ifdef SK_DEBUG 715 #ifdef SK_DEBUG
724 const GrSurfaceDesc& desc = tex->desc(); 716 const GrSurfaceDesc& desc = tex->desc();
725 SkASSERT(w <= desc.fWidth); 717 SkASSERT(w <= desc.fWidth);
726 SkASSERT(h <= desc.fHeight); 718 SkASSERT(h <= desc.fHeight);
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
775 SkErrorInternals::SetError( kInvalidPaint_SkError, 767 SkErrorInternals::SetError( kInvalidPaint_SkError,
776 "Sorry, I don't understand the filtering " 768 "Sorry, I don't understand the filtering "
777 "mode you asked for. Falling back to " 769 "mode you asked for. Falling back to "
778 "MIPMaps."); 770 "MIPMaps.");
779 textureFilterMode = GrTextureParams::kMipMap_FilterMode; 771 textureFilterMode = GrTextureParams::kMipMap_FilterMode;
780 break; 772 break;
781 773
782 } 774 }
783 return textureFilterMode; 775 return textureFilterMode;
784 } 776 }
OLDNEW
« no previous file with comments | « src/gpu/SkGpuDevice_drawTexture.cpp ('k') | src/gpu/SkGrPriv.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698