michael@0: michael@0: /* michael@0: * Copyright 2013 Google Inc. michael@0: * michael@0: * Use of this source code is governed by a BSD-style license that can be michael@0: * found in the LICENSE file. michael@0: */ michael@0: michael@0: #include "GrPaint.h" michael@0: michael@0: #include "GrBlend.h" michael@0: #include "effects/GrSimpleTextureEffect.h" michael@0: michael@0: void GrPaint::addColorTextureEffect(GrTexture* texture, const SkMatrix& matrix) { michael@0: GrEffectRef* effect = GrSimpleTextureEffect::Create(texture, matrix); michael@0: this->addColorEffect(effect)->unref(); michael@0: } michael@0: michael@0: void GrPaint::addCoverageTextureEffect(GrTexture* texture, const SkMatrix& matrix) { michael@0: GrEffectRef* effect = GrSimpleTextureEffect::Create(texture, matrix); michael@0: this->addCoverageEffect(effect)->unref(); michael@0: } michael@0: michael@0: void GrPaint::addColorTextureEffect(GrTexture* texture, michael@0: const SkMatrix& matrix, michael@0: const GrTextureParams& params) { michael@0: GrEffectRef* effect = GrSimpleTextureEffect::Create(texture, matrix, params); michael@0: this->addColorEffect(effect)->unref(); michael@0: } michael@0: michael@0: void GrPaint::addCoverageTextureEffect(GrTexture* texture, michael@0: const SkMatrix& matrix, michael@0: const GrTextureParams& params) { michael@0: GrEffectRef* effect = GrSimpleTextureEffect::Create(texture, matrix, params); michael@0: this->addCoverageEffect(effect)->unref(); michael@0: } michael@0: michael@0: bool GrPaint::isOpaque() const { michael@0: return this->getOpaqueAndKnownColor(NULL, NULL); michael@0: } michael@0: michael@0: bool GrPaint::isOpaqueAndConstantColor(GrColor* color) const { michael@0: GrColor tempColor; michael@0: uint32_t colorComps; michael@0: if (this->getOpaqueAndKnownColor(&tempColor, &colorComps)) { michael@0: if (kRGBA_GrColorComponentFlags == colorComps) { michael@0: *color = tempColor; michael@0: return true; michael@0: } michael@0: } michael@0: return false; michael@0: } michael@0: michael@0: bool GrPaint::getOpaqueAndKnownColor(GrColor* solidColor, michael@0: uint32_t* solidColorKnownComponents) const { michael@0: michael@0: // TODO: Share this implementation with GrDrawState michael@0: michael@0: GrColor coverage = GrColorPackRGBA(fCoverage, fCoverage, fCoverage, fCoverage); michael@0: uint32_t coverageComps = kRGBA_GrColorComponentFlags; michael@0: int count = fCoverageStages.count(); michael@0: for (int i = 0; i < count; ++i) { michael@0: (*fCoverageStages[i].getEffect())->getConstantColorComponents(&coverage, &coverageComps); michael@0: } michael@0: if (kRGBA_GrColorComponentFlags != coverageComps || 0xffffffff != coverage) { michael@0: return false; michael@0: } michael@0: michael@0: GrColor color = fColor; michael@0: uint32_t colorComps = kRGBA_GrColorComponentFlags; michael@0: count = fColorStages.count(); michael@0: for (int i = 0; i < count; ++i) { michael@0: (*fColorStages[i].getEffect())->getConstantColorComponents(&color, &colorComps); michael@0: } michael@0: michael@0: SkASSERT((NULL == solidColor) == (NULL == solidColorKnownComponents)); michael@0: michael@0: GrBlendCoeff srcCoeff = fSrcBlendCoeff; michael@0: GrBlendCoeff dstCoeff = fDstBlendCoeff; michael@0: GrSimplifyBlend(&srcCoeff, &dstCoeff, color, colorComps, 0, 0, 0); michael@0: michael@0: bool opaque = kZero_GrBlendCoeff == dstCoeff && !GrBlendCoeffRefsDst(srcCoeff); michael@0: if (NULL != solidColor) { michael@0: if (opaque) { michael@0: switch (srcCoeff) { michael@0: case kZero_GrBlendCoeff: michael@0: *solidColor = 0; michael@0: *solidColorKnownComponents = kRGBA_GrColorComponentFlags; michael@0: break; michael@0: michael@0: case kOne_GrBlendCoeff: michael@0: *solidColor = color; michael@0: *solidColorKnownComponents = colorComps; michael@0: break; michael@0: michael@0: // The src coeff should never refer to the src and if it refers to dst then opaque michael@0: // should have been false. michael@0: case kSC_GrBlendCoeff: michael@0: case kISC_GrBlendCoeff: michael@0: case kDC_GrBlendCoeff: michael@0: case kIDC_GrBlendCoeff: michael@0: case kSA_GrBlendCoeff: michael@0: case kISA_GrBlendCoeff: michael@0: case kDA_GrBlendCoeff: michael@0: case kIDA_GrBlendCoeff: michael@0: default: michael@0: GrCrash("srcCoeff should not refer to src or dst."); michael@0: break; michael@0: michael@0: // TODO: update this once GrPaint actually has a const color. michael@0: case kConstC_GrBlendCoeff: michael@0: case kIConstC_GrBlendCoeff: michael@0: case kConstA_GrBlendCoeff: michael@0: case kIConstA_GrBlendCoeff: michael@0: *solidColorKnownComponents = 0; michael@0: break; michael@0: } michael@0: } else { michael@0: solidColorKnownComponents = 0; michael@0: } michael@0: } michael@0: return opaque; michael@0: }