blob: 443e1c0abf71a1e81471956989b9626f3cfe2980 [file] [log] [blame]
commit-bot@chromium.org42dacab2013-07-13 17:24:24 +00001
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
egdaniel8d95ffa2014-12-08 13:26:43 -080011#include "GrBlend.h"
egdanielb6cbc382014-11-13 11:00:34 -080012#include "GrProcOptInfo.h"
egdaniel378092f2014-12-03 10:40:13 -080013#include "effects/GrPorterDuffXferProcessor.h"
commit-bot@chromium.org42dacab2013-07-13 17:24:24 +000014#include "effects/GrSimpleTextureEffect.h"
15
joshualittb0a8a372014-09-23 09:50:21 -070016void GrPaint::addColorTextureProcessor(GrTexture* texture, const SkMatrix& matrix) {
17 this->addColorProcessor(GrSimpleTextureEffect::Create(texture, matrix))->unref();
commit-bot@chromium.org42dacab2013-07-13 17:24:24 +000018}
19
joshualittb0a8a372014-09-23 09:50:21 -070020void GrPaint::addCoverageTextureProcessor(GrTexture* texture, const SkMatrix& matrix) {
21 this->addCoverageProcessor(GrSimpleTextureEffect::Create(texture, matrix))->unref();
commit-bot@chromium.org42dacab2013-07-13 17:24:24 +000022}
23
joshualittb0a8a372014-09-23 09:50:21 -070024void GrPaint::addColorTextureProcessor(GrTexture* texture,
commit-bot@chromium.org42dacab2013-07-13 17:24:24 +000025 const SkMatrix& matrix,
26 const GrTextureParams& params) {
joshualittb0a8a372014-09-23 09:50:21 -070027 this->addColorProcessor(GrSimpleTextureEffect::Create(texture, matrix, params))->unref();
commit-bot@chromium.org42dacab2013-07-13 17:24:24 +000028}
29
joshualittb0a8a372014-09-23 09:50:21 -070030void GrPaint::addCoverageTextureProcessor(GrTexture* texture,
commit-bot@chromium.org42dacab2013-07-13 17:24:24 +000031 const SkMatrix& matrix,
32 const GrTextureParams& params) {
joshualittb0a8a372014-09-23 09:50:21 -070033 this->addCoverageProcessor(GrSimpleTextureEffect::Create(texture, matrix, params))->unref();
commit-bot@chromium.org42dacab2013-07-13 17:24:24 +000034}
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +000035
36bool GrPaint::isOpaque() const {
37 return this->getOpaqueAndKnownColor(NULL, NULL);
38}
39
40bool GrPaint::isOpaqueAndConstantColor(GrColor* color) const {
egdanielb6cbc382014-11-13 11:00:34 -080041 GrColor tempColor = 0;
42 uint32_t colorComps = 0;
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +000043 if (this->getOpaqueAndKnownColor(&tempColor, &colorComps)) {
44 if (kRGBA_GrColorComponentFlags == colorComps) {
45 *color = tempColor;
46 return true;
47 }
48 }
49 return false;
50}
51
egdaniel378092f2014-12-03 10:40:13 -080052void GrPaint::resetStages() {
53 fColorStages.reset();
54 fCoverageStages.reset();
egdaniel8d95ffa2014-12-08 13:26:43 -080055 fXPFactory.reset(GrPorterDuffXPFactory::Create(SkXfermode::kSrcOver_Mode));
egdaniel378092f2014-12-03 10:40:13 -080056}
57
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +000058bool GrPaint::getOpaqueAndKnownColor(GrColor* solidColor,
59 uint32_t* solidColorKnownComponents) const {
60
egdaniel8d95ffa2014-12-08 13:26:43 -080061 // TODO: Share this implementation with GrDrawState
62
egdanielb6cbc382014-11-13 11:00:34 -080063 GrProcOptInfo coverageProcInfo;
64 coverageProcInfo.calcWithInitialValues(fCoverageStages.begin(), this->numCoverageStages(),
65 0xFFFFFFFF, kRGBA_GrColorComponentFlags, true);
egdaniel8d95ffa2014-12-08 13:26:43 -080066
67 if (!coverageProcInfo.isSolidWhite()) {
68 return false;
69 }
70
egdanielb6cbc382014-11-13 11:00:34 -080071 GrProcOptInfo colorProcInfo;
72 colorProcInfo.calcWithInitialValues(fColorStages.begin(), this->numColorStages(), fColor,
73 kRGBA_GrColorComponentFlags, false);
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +000074
egdaniel8d95ffa2014-12-08 13:26:43 -080075 SkASSERT((NULL == solidColor) == (NULL == solidColorKnownComponents));
76
77 GrBlendCoeff srcCoeff = fSrcBlendCoeff;
78 GrBlendCoeff dstCoeff = fDstBlendCoeff;
79 GrSimplifyBlend(&srcCoeff, &dstCoeff, colorProcInfo.color(), colorProcInfo.validFlags(),
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 = colorProcInfo.color();
93 *solidColorKnownComponents = colorProcInfo.validFlags();
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;
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +0000123}
egdaniel378092f2014-12-03 10:40:13 -0800124