GrPaint.cpp revision 3b8af078281a5a20f951b9fd84f38d92b8f6217b
1 2/* 3 * Copyright 2013 Google Inc. 4 * 5 * Use of this source code is governed by a BSD-style license that can be 6 * found in the LICENSE file. 7 */ 8 9#include "GrPaint.h" 10 11#include "GrBlend.h" 12#include "effects/GrSimpleTextureEffect.h" 13 14void GrPaint::addColorTextureProcessor(GrTexture* texture, const SkMatrix& matrix) { 15 this->addColorProcessor(GrSimpleTextureEffect::Create(texture, matrix))->unref(); 16} 17 18void GrPaint::addCoverageTextureProcessor(GrTexture* texture, const SkMatrix& matrix) { 19 this->addCoverageProcessor(GrSimpleTextureEffect::Create(texture, matrix))->unref(); 20} 21 22void GrPaint::addColorTextureProcessor(GrTexture* texture, 23 const SkMatrix& matrix, 24 const GrTextureParams& params) { 25 this->addColorProcessor(GrSimpleTextureEffect::Create(texture, matrix, params))->unref(); 26} 27 28void GrPaint::addCoverageTextureProcessor(GrTexture* texture, 29 const SkMatrix& matrix, 30 const GrTextureParams& params) { 31 this->addCoverageProcessor(GrSimpleTextureEffect::Create(texture, matrix, params))->unref(); 32} 33 34bool GrPaint::isOpaque() const { 35 return this->getOpaqueAndKnownColor(NULL, NULL); 36} 37 38bool GrPaint::isOpaqueAndConstantColor(GrColor* color) const { 39 GrColor tempColor; 40 uint32_t colorComps; 41 if (this->getOpaqueAndKnownColor(&tempColor, &colorComps)) { 42 if (kRGBA_GrColorComponentFlags == colorComps) { 43 *color = tempColor; 44 return true; 45 } 46 } 47 return false; 48} 49 50bool GrPaint::getOpaqueAndKnownColor(GrColor* solidColor, 51 uint32_t* solidColorKnownComponents) const { 52 53 // TODO: Share this implementation with GrDrawState 54 55 GrProcessor::InvariantOutput inout; 56 inout.fColor = GrColorPackRGBA(fCoverage, fCoverage, fCoverage, fCoverage); 57 inout.fValidFlags = kRGBA_GrColorComponentFlags; 58 inout.fIsSingleComponent = false; 59 int count = fCoverageStages.count(); 60 for (int i = 0; i < count; ++i) { 61 fCoverageStages[i].getProcessor()->computeInvariantOutput(&inout); 62 } 63 if (!inout.isSolidWhite()) { 64 return false; 65 } 66 67 inout.fColor = fColor; 68 inout.fValidFlags = kRGBA_GrColorComponentFlags; 69 inout.fIsSingleComponent = false; 70 count = fColorStages.count(); 71 for (int i = 0; i < count; ++i) { 72 fColorStages[i].getProcessor()->computeInvariantOutput(&inout); 73 } 74 75 SkASSERT((NULL == solidColor) == (NULL == solidColorKnownComponents)); 76 77 GrBlendCoeff srcCoeff = fSrcBlendCoeff; 78 GrBlendCoeff dstCoeff = fDstBlendCoeff; 79 GrSimplifyBlend(&srcCoeff, &dstCoeff, inout.fColor, inout.fValidFlags, 80 0, 0, 0); 81 82 bool opaque = kZero_GrBlendCoeff == dstCoeff && !GrBlendCoeffRefsDst(srcCoeff); 83 if (solidColor) { 84 if (opaque) { 85 switch (srcCoeff) { 86 case kZero_GrBlendCoeff: 87 *solidColor = 0; 88 *solidColorKnownComponents = kRGBA_GrColorComponentFlags; 89 break; 90 91 case kOne_GrBlendCoeff: 92 *solidColor = inout.fColor; 93 *solidColorKnownComponents = inout.fValidFlags; 94 break; 95 96 // The src coeff should never refer to the src and if it refers to dst then opaque 97 // should have been false. 98 case kSC_GrBlendCoeff: 99 case kISC_GrBlendCoeff: 100 case kDC_GrBlendCoeff: 101 case kIDC_GrBlendCoeff: 102 case kSA_GrBlendCoeff: 103 case kISA_GrBlendCoeff: 104 case kDA_GrBlendCoeff: 105 case kIDA_GrBlendCoeff: 106 default: 107 SkFAIL("srcCoeff should not refer to src or dst."); 108 break; 109 110 // TODO: update this once GrPaint actually has a const color. 111 case kConstC_GrBlendCoeff: 112 case kIConstC_GrBlendCoeff: 113 case kConstA_GrBlendCoeff: 114 case kIConstA_GrBlendCoeff: 115 *solidColorKnownComponents = 0; 116 break; 117 } 118 } else { 119 solidColorKnownComponents = 0; 120 } 121 } 122 return opaque; 123} 124