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::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}
37
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    GrColor coverage = GrColorPackRGBA(fCoverage, fCoverage, fCoverage, fCoverage);
60    uint32_t coverageComps = kRGBA_GrColorComponentFlags;
61    int count = fCoverageStages.count();
62    for (int i = 0; i < count; ++i) {
63        (*fCoverageStages[i].getEffect())->getConstantColorComponents(&coverage, &coverageComps);
64    }
65    if (kRGBA_GrColorComponentFlags != coverageComps || 0xffffffff != coverage) {
66        return false;
67    }
68
69    GrColor color = fColor;
70    uint32_t colorComps = kRGBA_GrColorComponentFlags;
71    count = fColorStages.count();
72    for (int i = 0; i < count; ++i) {
73        (*fColorStages[i].getEffect())->getConstantColorComponents(&color, &colorComps);
74    }
75
76    SkASSERT((NULL == solidColor) == (NULL == solidColorKnownComponents));
77
78    GrBlendCoeff srcCoeff = fSrcBlendCoeff;
79    GrBlendCoeff dstCoeff = fDstBlendCoeff;
80    GrSimplifyBlend(&srcCoeff, &dstCoeff, color, colorComps, 0, 0, 0);
81
82    bool opaque = kZero_GrBlendCoeff == dstCoeff && !GrBlendCoeffRefsDst(srcCoeff);
83    if (NULL != 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 = color;
93                    *solidColorKnownComponents = colorComps;
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                    GrCrash("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