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 "src/gpu/GrPaint.h"
9 #include "src/gpu/GrXferProcessor.h"
10 #include "src/gpu/effects/GrCoverageSetOpXP.h"
11 #include "src/gpu/effects/GrPorterDuffXferProcessor.h"
12 #include "src/gpu/effects/generated/GrSimpleTextureEffect.h"
13 
GrPaint(const GrPaint & that)14 GrPaint::GrPaint(const GrPaint& that)
15         : fXPFactory(that.fXPFactory)
16         , fColorFragmentProcessors(that.fColorFragmentProcessors.count())
17         , fCoverageFragmentProcessors(that.fCoverageFragmentProcessors.count())
18         , fTrivial(that.fTrivial)
19         , fColor(that.fColor) {
20     for (int i = 0; i < that.fColorFragmentProcessors.count(); ++i) {
21         fColorFragmentProcessors.push_back(that.fColorFragmentProcessors[i]->clone());
22         SkASSERT(fColorFragmentProcessors[i]);
23     }
24     for (int i = 0; i < that.fCoverageFragmentProcessors.count(); ++i) {
25         fCoverageFragmentProcessors.push_back(that.fCoverageFragmentProcessors[i]->clone());
26         SkASSERT(fCoverageFragmentProcessors[i]);
27     }
28 }
29 
setPorterDuffXPFactory(SkBlendMode mode)30 void GrPaint::setPorterDuffXPFactory(SkBlendMode mode) {
31     this->setXPFactory(GrPorterDuffXPFactory::Get(mode));
32 }
33 
setCoverageSetOpXPFactory(SkRegion::Op regionOp,bool invertCoverage)34 void GrPaint::setCoverageSetOpXPFactory(SkRegion::Op regionOp, bool invertCoverage) {
35     this->setXPFactory(GrCoverageSetOpXPFactory::Get(regionOp, invertCoverage));
36 }
37 
addColorTextureProcessor(sk_sp<GrTextureProxy> proxy,GrColorType srcColorType,const SkMatrix & matrix)38 void GrPaint::addColorTextureProcessor(sk_sp<GrTextureProxy> proxy, GrColorType srcColorType,
39                                        const SkMatrix& matrix) {
40     this->addColorFragmentProcessor(GrSimpleTextureEffect::Make(std::move(proxy), srcColorType,
41                                                                 matrix));
42 }
43 
addColorTextureProcessor(sk_sp<GrTextureProxy> proxy,GrColorType srcColorType,const SkMatrix & matrix,const GrSamplerState & samplerState)44 void GrPaint::addColorTextureProcessor(sk_sp<GrTextureProxy> proxy, GrColorType srcColorType,
45                                        const SkMatrix& matrix, const GrSamplerState& samplerState) {
46     this->addColorFragmentProcessor(GrSimpleTextureEffect::Make(std::move(proxy), srcColorType,
47                                                                 matrix, samplerState));
48 }
49 
isConstantBlendedColor(SkPMColor4f * constantColor) const50 bool GrPaint::isConstantBlendedColor(SkPMColor4f* constantColor) const {
51     // This used to do a more sophisticated analysis but now it just explicitly looks for common
52     // cases.
53     static const GrXPFactory* kSrc = GrPorterDuffXPFactory::Get(SkBlendMode::kSrc);
54     static const GrXPFactory* kClear = GrPorterDuffXPFactory::Get(SkBlendMode::kClear);
55     if (kClear == fXPFactory) {
56         *constantColor = SK_PMColor4fTRANSPARENT;
57         return true;
58     }
59     if (this->numColorFragmentProcessors()) {
60         return false;
61     }
62     if (kSrc == fXPFactory || (!fXPFactory && fColor.isOpaque())) {
63         *constantColor = fColor;
64         return true;
65     }
66     return false;
67 }
68