blob: 898a32ff70f53ac2e4aaf9f9537eb214d1aebecf [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
14void GrPaint::addColorTextureEffect(GrTexture* texture, const SkMatrix& matrix) {
15 GrEffectRef* effect = GrSimpleTextureEffect::Create(texture, matrix);
16 this->addColorEffect(effect)->unref();
17}
18
19void GrPaint::addCoverageTextureEffect(GrTexture* texture, const SkMatrix& matrix) {
20 GrEffectRef* effect = GrSimpleTextureEffect::Create(texture, matrix);
21 this->addCoverageEffect(effect)->unref();
22}
23
24void GrPaint::addColorTextureEffect(GrTexture* texture,
25 const SkMatrix& matrix,
26 const GrTextureParams& params) {
27 GrEffectRef* effect = GrSimpleTextureEffect::Create(texture, matrix, params);
28 this->addColorEffect(effect)->unref();
29}
30
31void GrPaint::addCoverageTextureEffect(GrTexture* texture,
32 const SkMatrix& matrix,
33 const GrTextureParams& params) {
34 GrEffectRef* effect = GrSimpleTextureEffect::Create(texture, matrix, params);
35 this->addCoverageEffect(effect)->unref();
36}
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +000037
38bool GrPaint::isOpaque() const {
39 return this->getOpaqueAndKnownColor(NULL, NULL);
40}
41
42bool GrPaint::isOpaqueAndConstantColor(GrColor* color) const {
43 GrColor tempColor;
44 uint32_t colorComps;
45 if (this->getOpaqueAndKnownColor(&tempColor, &colorComps)) {
46 if (kRGBA_GrColorComponentFlags == colorComps) {
47 *color = tempColor;
48 return true;
49 }
50 }
51 return false;
52}
53
54bool GrPaint::getOpaqueAndKnownColor(GrColor* solidColor,
55 uint32_t* solidColorKnownComponents) const {
56
57 // TODO: Share this implementation with GrDrawState
58
59 // Since fColorFilterXfermode is going away soon, we aren't attempting to handle anything but
60 // the default setting.
61 if (SkXfermode::kDst_Mode != fColorFilterXfermode) {
62 return false;
63 }
64
65 GrColor coverage = GrColorPackRGBA(fCoverage, fCoverage, fCoverage, fCoverage);
66 uint32_t coverageComps = kRGBA_GrColorComponentFlags;
67 int count = fCoverageStages.count();
68 for (int i = 0; i < count; ++i) {
69 (*fCoverageStages[i].getEffect())->getConstantColorComponents(&coverage, &coverageComps);
70 }
71 if (kRGBA_GrColorComponentFlags != coverageComps || 0xffffffff != coverage) {
72 return false;
73 }
74
75 GrColor color = fColor;
76 uint32_t colorComps = kRGBA_GrColorComponentFlags;
77 count = fColorStages.count();
78 for (int i = 0; i < count; ++i) {
79 (*fColorStages[i].getEffect())->getConstantColorComponents(&color, &colorComps);
80 }
81
82 GrAssert((NULL == solidColor) == (NULL == solidColorKnownComponents));
83
84 GrBlendCoeff srcCoeff = fSrcBlendCoeff;
85 GrBlendCoeff dstCoeff = fDstBlendCoeff;
86 GrSimplifyBlend(&srcCoeff, &dstCoeff, color, colorComps, 0, 0, 0);
87
88 bool opaque = kZero_GrBlendCoeff == dstCoeff && !GrBlendCoeffRefsDst(srcCoeff);
89 if (NULL != solidColor) {
90 if (opaque) {
91 switch (srcCoeff) {
92 case kZero_GrBlendCoeff:
93 *solidColor = 0;
94 *solidColorKnownComponents = kRGBA_GrColorComponentFlags;
95 break;
96
97 case kOne_GrBlendCoeff:
98 *solidColor = color;
99 *solidColorKnownComponents = colorComps;
100 break;
101
102 // The src coeff should never refer to the src and if it refers to dst then opaque
103 // should have been false.
104 case kSC_GrBlendCoeff:
105 case kISC_GrBlendCoeff:
106 case kDC_GrBlendCoeff:
107 case kIDC_GrBlendCoeff:
108 case kSA_GrBlendCoeff:
109 case kISA_GrBlendCoeff:
110 case kDA_GrBlendCoeff:
111 case kIDA_GrBlendCoeff:
112 default:
113 GrCrash("srcCoeff should not refer to src or dst.");
114 break;
115
116 // TODO: update this once GrPaint actually has a const color.
117 case kConstC_GrBlendCoeff:
118 case kIConstC_GrBlendCoeff:
119 case kConstA_GrBlendCoeff:
120 case kIConstA_GrBlendCoeff:
121 *solidColorKnownComponents = 0;
122 break;
123 }
124 } else {
125 solidColorKnownComponents = 0;
126 }
127 }
128 return opaque;
129}