blob: ffde5f99e4116dbf920065d4c529ce481435da3d [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
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +000011#include "GrBlend.h"
commit-bot@chromium.org42dacab2013-07-13 17:24:24 +000012#include "effects/GrSimpleTextureEffect.h"
13
joshualittb0a8a372014-09-23 09:50:21 -070014void GrPaint::addColorTextureProcessor(GrTexture* texture, const SkMatrix& matrix) {
15 this->addColorProcessor(GrSimpleTextureEffect::Create(texture, matrix))->unref();
commit-bot@chromium.org42dacab2013-07-13 17:24:24 +000016}
17
joshualittb0a8a372014-09-23 09:50:21 -070018void GrPaint::addCoverageTextureProcessor(GrTexture* texture, const SkMatrix& matrix) {
19 this->addCoverageProcessor(GrSimpleTextureEffect::Create(texture, matrix))->unref();
commit-bot@chromium.org42dacab2013-07-13 17:24:24 +000020}
21
joshualittb0a8a372014-09-23 09:50:21 -070022void GrPaint::addColorTextureProcessor(GrTexture* texture,
commit-bot@chromium.org42dacab2013-07-13 17:24:24 +000023 const SkMatrix& matrix,
24 const GrTextureParams& params) {
joshualittb0a8a372014-09-23 09:50:21 -070025 this->addColorProcessor(GrSimpleTextureEffect::Create(texture, matrix, params))->unref();
commit-bot@chromium.org42dacab2013-07-13 17:24:24 +000026}
27
joshualittb0a8a372014-09-23 09:50:21 -070028void GrPaint::addCoverageTextureProcessor(GrTexture* texture,
commit-bot@chromium.org42dacab2013-07-13 17:24:24 +000029 const SkMatrix& matrix,
30 const GrTextureParams& params) {
joshualittb0a8a372014-09-23 09:50:21 -070031 this->addCoverageProcessor(GrSimpleTextureEffect::Create(texture, matrix, params))->unref();
commit-bot@chromium.org42dacab2013-07-13 17:24:24 +000032}
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +000033
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
egdaniel1a8ecdf2014-10-03 06:24:12 -070055 GrProcessor::InvariantOutput inout;
56 inout.fColor = GrColorPackRGBA(fCoverage, fCoverage, fCoverage, fCoverage);
57 inout.fValidFlags = kRGBA_GrColorComponentFlags;
58 inout.fIsSingleComponent = false;
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +000059 int count = fCoverageStages.count();
60 for (int i = 0; i < count; ++i) {
egdaniel1a8ecdf2014-10-03 06:24:12 -070061 fCoverageStages[i].getProcessor()->computeInvariantOutput(&inout);
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +000062 }
egdaniel1a8ecdf2014-10-03 06:24:12 -070063 if (!inout.isSolidWhite()) {
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +000064 return false;
65 }
66
egdaniel1a8ecdf2014-10-03 06:24:12 -070067 inout.fColor = fColor;
68 inout.fValidFlags = kRGBA_GrColorComponentFlags;
69 inout.fIsSingleComponent = false;
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +000070 count = fColorStages.count();
71 for (int i = 0; i < count; ++i) {
egdaniel1a8ecdf2014-10-03 06:24:12 -070072 fColorStages[i].getProcessor()->computeInvariantOutput(&inout);
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +000073 }
74
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000075 SkASSERT((NULL == solidColor) == (NULL == solidColorKnownComponents));
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +000076
77 GrBlendCoeff srcCoeff = fSrcBlendCoeff;
78 GrBlendCoeff dstCoeff = fDstBlendCoeff;
egdaniel1a8ecdf2014-10-03 06:24:12 -070079 GrSimplifyBlend(&srcCoeff, &dstCoeff, inout.fColor, inout.fValidFlags,
80 0, 0, 0);
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +000081
82 bool opaque = kZero_GrBlendCoeff == dstCoeff && !GrBlendCoeffRefsDst(srcCoeff);
bsalomon49f085d2014-09-05 13:34:00 -070083 if (solidColor) {
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +000084 if (opaque) {
85 switch (srcCoeff) {
86 case kZero_GrBlendCoeff:
87 *solidColor = 0;
88 *solidColorKnownComponents = kRGBA_GrColorComponentFlags;
89 break;
90
91 case kOne_GrBlendCoeff:
egdaniel1a8ecdf2014-10-03 06:24:12 -070092 *solidColor = inout.fColor;
93 *solidColorKnownComponents = inout.fValidFlags;
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +000094 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:
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +0000107 SkFAIL("srcCoeff should not refer to src or dst.");
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +0000108 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}