1 /*
2  * Copyright 2013 Google Inc.
3  *
4  * Use of this source code is governed by a BSD-style license that can be
5  * found in the LICENSE file.
6  */
7 
8 #include "GrPaint.h"
9 #include "GrXferProcessor.h"
10 #include "effects/GrCoverageSetOpXP.h"
11 #include "effects/GrPorterDuffXferProcessor.h"
12 #include "effects/GrSimpleTextureEffect.h"
13 
14 GrPaint::GrPaint(const GrPaint& that)
15         : fXPFactory(that.fXPFactory)
16         , fColorFragmentProcessors(that.fColorFragmentProcessors.count())
17         , fCoverageFragmentProcessors(that.fCoverageFragmentProcessors.count())
18         , fDisableOutputConversionToSRGB(that.fDisableOutputConversionToSRGB)
19         , fAllowSRGBInputs(that.fAllowSRGBInputs)
20         , fTrivial(that.fTrivial)
21         , fColor(that.fColor) {
22     for (int i = 0; i < that.fColorFragmentProcessors.count(); ++i) {
23         fColorFragmentProcessors.push_back(that.fColorFragmentProcessors[i]->clone());
24         SkASSERT(fColorFragmentProcessors[i]);
25     }
26     for (int i = 0; i < that.fCoverageFragmentProcessors.count(); ++i) {
27         fCoverageFragmentProcessors.push_back(that.fCoverageFragmentProcessors[i]->clone());
28         SkASSERT(fCoverageFragmentProcessors[i]);
29     }
30 }
31 
32 void GrPaint::setPorterDuffXPFactory(SkBlendMode mode) {
33     this->setXPFactory(GrPorterDuffXPFactory::Get(mode));
34 }
35 
36 void GrPaint::setCoverageSetOpXPFactory(SkRegion::Op regionOp, bool invertCoverage) {
37     this->setXPFactory(GrCoverageSetOpXPFactory::Get(regionOp, invertCoverage));
38 }
39 
40 void GrPaint::addColorTextureProcessor(sk_sp<GrTextureProxy> proxy, const SkMatrix& matrix) {
41     this->addColorFragmentProcessor(GrSimpleTextureEffect::Make(std::move(proxy), matrix));
42 }
43 
44 void GrPaint::addColorTextureProcessor(sk_sp<GrTextureProxy> proxy, const SkMatrix& matrix,
45                                        const GrSamplerState& samplerState) {
46     this->addColorFragmentProcessor(GrSimpleTextureEffect::Make(std::move(proxy), matrix,
47                                                                 samplerState));
48 }
49 
50 void GrPaint::addCoverageTextureProcessor(sk_sp<GrTextureProxy> proxy,
51                                           const SkMatrix& matrix) {
52     this->addCoverageFragmentProcessor(GrSimpleTextureEffect::Make(std::move(proxy), matrix));
53 }
54 
55 void GrPaint::addCoverageTextureProcessor(sk_sp<GrTextureProxy> proxy,
56                                           const SkMatrix& matrix,
57                                           const GrSamplerState& params) {
58     this->addCoverageFragmentProcessor(GrSimpleTextureEffect::Make(std::move(proxy), matrix,
59                                                                    params));
60 }
61 
62 bool GrPaint::isConstantBlendedColor(GrColor* constantColor) const {
63     // This used to do a more sophisticated analysis but now it just explicitly looks for common
64     // cases.
65     static const GrXPFactory* kSrc = GrPorterDuffXPFactory::Get(SkBlendMode::kSrc);
66     static const GrXPFactory* kClear = GrPorterDuffXPFactory::Get(SkBlendMode::kClear);
67     if (kClear == fXPFactory) {
68         *constantColor = GrColor_TRANSPARENT_BLACK;
69         return true;
70     }
71     if (this->numColorFragmentProcessors()) {
72         return false;
73     }
74     if (kSrc == fXPFactory || (!fXPFactory && fColor.isOpaque())) {
75         *constantColor = fColor.toGrColor();
76         return true;
77     }
78     return false;
79 }
80