GrPaint.cpp revision 8d95ffa497091d0c9c7cda099684c7bca6714a17
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 "GrProcOptInfo.h"
13#include "effects/GrPorterDuffXferProcessor.h"
14#include "effects/GrSimpleTextureEffect.h"
15
16void GrPaint::addColorTextureProcessor(GrTexture* texture, const SkMatrix& matrix) {
17    this->addColorProcessor(GrSimpleTextureEffect::Create(texture, matrix))->unref();
18}
19
20void GrPaint::addCoverageTextureProcessor(GrTexture* texture, const SkMatrix& matrix) {
21    this->addCoverageProcessor(GrSimpleTextureEffect::Create(texture, matrix))->unref();
22}
23
24void GrPaint::addColorTextureProcessor(GrTexture* texture,
25                                    const SkMatrix& matrix,
26                                    const GrTextureParams& params) {
27    this->addColorProcessor(GrSimpleTextureEffect::Create(texture, matrix, params))->unref();
28}
29
30void GrPaint::addCoverageTextureProcessor(GrTexture* texture,
31                                       const SkMatrix& matrix,
32                                       const GrTextureParams& params) {
33    this->addCoverageProcessor(GrSimpleTextureEffect::Create(texture, matrix, params))->unref();
34}
35
36bool GrPaint::isOpaque() const {
37    return this->getOpaqueAndKnownColor(NULL, NULL);
38}
39
40bool GrPaint::isOpaqueAndConstantColor(GrColor* color) const {
41    GrColor tempColor = 0;
42    uint32_t colorComps = 0;
43    if (this->getOpaqueAndKnownColor(&tempColor, &colorComps)) {
44        if (kRGBA_GrColorComponentFlags == colorComps) {
45            *color = tempColor;
46            return true;
47        }
48    }
49    return false;
50}
51
52void GrPaint::resetStages() {
53    fColorStages.reset();
54    fCoverageStages.reset();
55    fXPFactory.reset(GrPorterDuffXPFactory::Create(SkXfermode::kSrcOver_Mode));
56}
57
58bool GrPaint::getOpaqueAndKnownColor(GrColor* solidColor,
59                                     uint32_t* solidColorKnownComponents) const {
60
61    // TODO: Share this implementation with GrDrawState
62
63    GrProcOptInfo coverageProcInfo;
64    coverageProcInfo.calcWithInitialValues(fCoverageStages.begin(), this->numCoverageStages(),
65                                           0xFFFFFFFF, kRGBA_GrColorComponentFlags, true);
66
67    if (!coverageProcInfo.isSolidWhite()) {
68        return false;
69    }
70
71    GrProcOptInfo colorProcInfo;
72    colorProcInfo.calcWithInitialValues(fColorStages.begin(), this->numColorStages(), fColor,
73                                        kRGBA_GrColorComponentFlags, false);
74
75    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;
123}
124
125