Flutter Engine
The Flutter Engine
GrCoverageSetOpXP.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2014 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
9
12#include "src/base/SkRandom.h"
13#include "src/gpu/Blend.h"
14#include "src/gpu/KeyBuilder.h"
17
18#include <memory>
19
20enum class GrClampType;
21struct GrShaderCaps;
22
24public:
25 CoverageSetOpXP(SkRegion::Op regionOp, bool invertCoverage)
27 , fRegionOp(regionOp)
28 , fInvertCoverage(invertCoverage) {}
29
30 const char* name() const override { return "Coverage Set Op"; }
31
32 std::unique_ptr<ProgramImpl> makeProgramImpl() const override;
33
34private:
35 void onAddToKey(const GrShaderCaps&, skgpu::KeyBuilder*) const override;
36
37 void onGetBlendInfo(skgpu::BlendInfo* blendInfo) const override;
38
39 bool onIsEqual(const GrXferProcessor& xpBase) const override {
40 const CoverageSetOpXP& xp = xpBase.cast<CoverageSetOpXP>();
41 return (fRegionOp == xp.fRegionOp &&
42 fInvertCoverage == xp.fInvertCoverage);
43 }
44
45 SkRegion::Op fRegionOp;
46 bool fInvertCoverage;
47
48 using INHERITED = GrXferProcessor;
49};
50
51void CoverageSetOpXP::onAddToKey(const GrShaderCaps& caps, skgpu::KeyBuilder* b) const {
52 b->addBool(fInvertCoverage, "invert coverage");
53}
54
55std::unique_ptr<GrXferProcessor::ProgramImpl> CoverageSetOpXP::makeProgramImpl() const {
56 class Impl : public ProgramImpl {
57 private:
58 void emitOutputsForBlendState(const EmitArgs& args) override {
59 const CoverageSetOpXP& xp = args.fXP.cast<CoverageSetOpXP>();
60 GrGLSLXPFragmentBuilder* fb = args.fXPFragBuilder;
61 if (xp.fInvertCoverage) {
62 fb->codeAppendf("%s = 1.0 - %s;", args.fOutputPrimary, args.fInputCoverage);
63 } else {
64 fb->codeAppendf("%s = %s;", args.fOutputPrimary, args.fInputCoverage);
65 }
66 }
67 };
68 return std::make_unique<Impl>();
69}
70
71void CoverageSetOpXP::onGetBlendInfo(skgpu::BlendInfo* blendInfo) const {
72 switch (fRegionOp) {
76 break;
80 break;
84 break;
88 break;
92 break;
96 break;
97 }
99}
100
101///////////////////////////////////////////////////////////////////////////////
102
103constexpr GrCoverageSetOpXPFactory::GrCoverageSetOpXPFactory(SkRegion::Op regionOp,
104 bool invertCoverage)
105 : fRegionOp(regionOp), fInvertCoverage(invertCoverage) {}
106
107const GrXPFactory* GrCoverageSetOpXPFactory::Get(SkRegion::Op regionOp, bool invertCoverage) {
108 switch (regionOp) {
110 if (invertCoverage) {
111 static constexpr const GrCoverageSetOpXPFactory gReplaceCDXPFI(
113 return &gReplaceCDXPFI;
114 } else {
115 static constexpr const GrCoverageSetOpXPFactory gReplaceCDXPF(SkRegion::kReplace_Op,
116 false);
117 return &gReplaceCDXPF;
118 }
119 }
121 if (invertCoverage) {
122 static constexpr const GrCoverageSetOpXPFactory gIntersectCDXPFI(
124 return &gIntersectCDXPFI;
125 } else {
126 static constexpr const GrCoverageSetOpXPFactory gIntersectCDXPF(
128 return &gIntersectCDXPF;
129 }
130 }
131 case SkRegion::kUnion_Op: {
132 if (invertCoverage) {
133 static constexpr const GrCoverageSetOpXPFactory gUnionCDXPFI(SkRegion::kUnion_Op,
134 true);
135 return &gUnionCDXPFI;
136 } else {
137 static constexpr const GrCoverageSetOpXPFactory gUnionCDXPF(SkRegion::kUnion_Op,
138 false);
139 return &gUnionCDXPF;
140 }
141 }
142 case SkRegion::kXOR_Op: {
143 if (invertCoverage) {
144 static constexpr const GrCoverageSetOpXPFactory gXORCDXPFI(SkRegion::kXOR_Op, true);
145 return &gXORCDXPFI;
146 } else {
147 static constexpr const GrCoverageSetOpXPFactory gXORCDXPF(SkRegion::kXOR_Op, false);
148 return &gXORCDXPF;
149 }
150 }
152 if (invertCoverage) {
153 static constexpr const GrCoverageSetOpXPFactory gDifferenceCDXPFI(
155 return &gDifferenceCDXPFI;
156 } else {
157 static constexpr const GrCoverageSetOpXPFactory gDifferenceCDXPF(
159 return &gDifferenceCDXPF;
160 }
161 }
163 if (invertCoverage) {
164 static constexpr const GrCoverageSetOpXPFactory gRevDiffCDXPFI(
166 return &gRevDiffCDXPFI;
167 } else {
168 static constexpr const GrCoverageSetOpXPFactory gRevDiffCDXPF(
170 return &gRevDiffCDXPF;
171 }
172 }
173 }
174 SK_ABORT("Unknown region op.");
175}
176
177sk_sp<const GrXferProcessor> GrCoverageSetOpXPFactory::makeXferProcessor(
180 const GrCaps& caps,
181 GrClampType) const {
182 return sk_sp<GrXferProcessor>(new CoverageSetOpXP(fRegionOp, fInvertCoverage));
183}
184
186
187#if defined(GR_TEST_UTILS)
188const GrXPFactory* GrCoverageSetOpXPFactory::TestGet(GrProcessorTestData* d) {
189 SkRegion::Op regionOp = SkRegion::Op(d->fRandom->nextULessThan(SkRegion::kLastOp + 1));
190 bool invertCoverage = d->fRandom->nextBool();
191 return GrCoverageSetOpXPFactory::Get(regionOp, invertCoverage);
192}
193#endif
GrProcessorAnalysisCoverage
#define GR_DEFINE_XP_FACTORY_TEST(...)
GrClampType
Definition: GrTypesPriv.h:228
#define SK_ABORT(message,...)
Definition: SkAssert.h:70
constexpr SkPMColor4f SK_PMColor4fTRANSPARENT
Definition: SkColorData.h:378
CoverageSetOpXP(SkRegion::Op regionOp, bool invertCoverage)
std::unique_ptr< ProgramImpl > makeProgramImpl() const override
const char * name() const override
Definition: GrCaps.h:57
static const GrXPFactory * Get(SkRegion::Op regionOp, bool invertCoverage=false)
const T & cast() const
Definition: GrProcessor.h:127
@ kCoverageSetOpXP_ClassID
Definition: GrProcessor.h:36
GrXferProcessor(ClassID classID)
@ kReverseDifference_Op
operand minus target
Definition: SkRegion.h:371
@ kUnion_Op
target unioned with operand
Definition: SkRegion.h:369
@ kLastOp
last operator
Definition: SkRegion.h:373
@ kReplace_Op
replace target with operand
Definition: SkRegion.h:372
@ kIntersect_Op
target intersected with operand
Definition: SkRegion.h:368
@ kDifference_Op
target minus operand
Definition: SkRegion.h:367
@ kXOR_Op
target exclusive or with operand
Definition: SkRegion.h:370
VULKAN_HPP_DEFAULT_DISPATCH_LOADER_DYNAMIC_STORAGE auto & d
Definition: main.cc:19
static bool b
G_BEGIN_DECLS G_MODULE_EXPORT FlValue * args
skgpu::BlendCoeff fDstBlend
Definition: Blend.h:96
SkPMColor4f fBlendConstant
Definition: Blend.h:97
skgpu::BlendCoeff fSrcBlend
Definition: Blend.h:95