GrPaint.cpp revision 605dd0fbce9dbb2a0d3313e13e161f2bd54870d7
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 "GrInvariantOutput.h"
13#include "effects/GrSimpleTextureEffect.h"
14
15void GrPaint::addColorTextureProcessor(GrTexture* texture, const SkMatrix& matrix) {
16    this->addColorProcessor(GrSimpleTextureEffect::Create(texture, matrix))->unref();
17}
18
19void GrPaint::addCoverageTextureProcessor(GrTexture* texture, const SkMatrix& matrix) {
20    this->addCoverageProcessor(GrSimpleTextureEffect::Create(texture, matrix))->unref();
21}
22
23void GrPaint::addColorTextureProcessor(GrTexture* texture,
24                                    const SkMatrix& matrix,
25                                    const GrTextureParams& params) {
26    this->addColorProcessor(GrSimpleTextureEffect::Create(texture, matrix, params))->unref();
27}
28
29void GrPaint::addCoverageTextureProcessor(GrTexture* texture,
30                                       const SkMatrix& matrix,
31                                       const GrTextureParams& params) {
32    this->addCoverageProcessor(GrSimpleTextureEffect::Create(texture, matrix, params))->unref();
33}
34
35bool GrPaint::isOpaque() const {
36    return this->getOpaqueAndKnownColor(NULL, NULL);
37}
38
39bool GrPaint::isOpaqueAndConstantColor(GrColor* color) const {
40    GrColor tempColor;
41    uint32_t colorComps;
42    if (this->getOpaqueAndKnownColor(&tempColor, &colorComps)) {
43        if (kRGBA_GrColorComponentFlags == colorComps) {
44            *color = tempColor;
45            return true;
46        }
47    }
48    return false;
49}
50
51bool GrPaint::getOpaqueAndKnownColor(GrColor* solidColor,
52                                     uint32_t* solidColorKnownComponents) const {
53
54    // TODO: Share this implementation with GrDrawState
55
56    GrInvariantOutput inoutCoverage(0xFFFFFFFF,
57                                    kRGBA_GrColorComponentFlags,
58                                    true);
59    int count = fCoverageStages.count();
60    for (int i = 0; i < count; ++i) {
61        fCoverageStages[i].getProcessor()->computeInvariantOutput(&inoutCoverage);
62    }
63    if (!inoutCoverage.isSolidWhite()) {
64        return false;
65    }
66
67    GrInvariantOutput inout(fColor, kRGBA_GrColorComponentFlags, false);
68    count = fColorStages.count();
69    for (int i = 0; i < count; ++i) {
70        fColorStages[i].getProcessor()->computeInvariantOutput(&inout);
71    }
72
73    SkASSERT((NULL == solidColor) == (NULL == solidColorKnownComponents));
74
75    GrBlendCoeff srcCoeff = fSrcBlendCoeff;
76    GrBlendCoeff dstCoeff = fDstBlendCoeff;
77    GrSimplifyBlend(&srcCoeff, &dstCoeff, inout.color(), inout.validFlags(),
78                    0, 0, 0);
79
80    bool opaque = kZero_GrBlendCoeff == dstCoeff && !GrBlendCoeffRefsDst(srcCoeff);
81    if (solidColor) {
82        if (opaque) {
83            switch (srcCoeff) {
84                case kZero_GrBlendCoeff:
85                    *solidColor = 0;
86                    *solidColorKnownComponents = kRGBA_GrColorComponentFlags;
87                    break;
88
89                case kOne_GrBlendCoeff:
90                    *solidColor = inout.color();
91                    *solidColorKnownComponents = inout.validFlags();
92                    break;
93
94                // The src coeff should never refer to the src and if it refers to dst then opaque
95                // should have been false.
96                case kSC_GrBlendCoeff:
97                case kISC_GrBlendCoeff:
98                case kDC_GrBlendCoeff:
99                case kIDC_GrBlendCoeff:
100                case kSA_GrBlendCoeff:
101                case kISA_GrBlendCoeff:
102                case kDA_GrBlendCoeff:
103                case kIDA_GrBlendCoeff:
104                default:
105                    SkFAIL("srcCoeff should not refer to src or dst.");
106                    break;
107
108                // TODO: update this once GrPaint actually has a const color.
109                case kConstC_GrBlendCoeff:
110                case kIConstC_GrBlendCoeff:
111                case kConstA_GrBlendCoeff:
112                case kIConstA_GrBlendCoeff:
113                    *solidColorKnownComponents = 0;
114                    break;
115            }
116        } else {
117            solidColorKnownComponents = 0;
118        }
119    }
120    return opaque;
121}
122