1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
|
/*
* Copyright 2015 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include "effects/GrXfermodeFragmentProcessor.h"
#include "GrFragmentProcessor.h"
#include "GrInvariantOutput.h"
#include "effects/GrConstColorProcessor.h"
#include "glsl/GrGLSLFragmentProcessor.h"
#include "glsl/GrGLSLBlend.h"
#include "glsl/GrGLSLFragmentShaderBuilder.h"
#include "SkGrPriv.h"
class ComposeTwoFragmentProcessor : public GrFragmentProcessor {
public:
ComposeTwoFragmentProcessor(const GrFragmentProcessor* src, const GrFragmentProcessor* dst,
SkXfermode::Mode mode)
: fMode(mode) {
this->initClassID<ComposeTwoFragmentProcessor>();
SkDEBUGCODE(int shaderAChildIndex = )this->registerChildProcessor(src);
SkDEBUGCODE(int shaderBChildIndex = )this->registerChildProcessor(dst);
SkASSERT(0 == shaderAChildIndex);
SkASSERT(1 == shaderBChildIndex);
}
const char* name() const override { return "ComposeTwo"; }
void onGetGLSLProcessorKey(const GrGLSLCaps& caps, GrProcessorKeyBuilder* b) const override {
b->add32(fMode);
}
SkXfermode::Mode getMode() const { return fMode; }
protected:
bool onIsEqual(const GrFragmentProcessor& other) const override {
const ComposeTwoFragmentProcessor& cs = other.cast<ComposeTwoFragmentProcessor>();
return fMode == cs.fMode;
}
void onComputeInvariantOutput(GrInvariantOutput* inout) const override {
inout->setToUnknown(GrInvariantOutput::kWill_ReadInput);
}
private:
GrGLSLFragmentProcessor* onCreateGLSLInstance() const override;
SkXfermode::Mode fMode;
GR_DECLARE_FRAGMENT_PROCESSOR_TEST;
typedef GrFragmentProcessor INHERITED;
};
/////////////////////////////////////////////////////////////////////
class GLComposeTwoFragmentProcessor : public GrGLSLFragmentProcessor {
public:
GLComposeTwoFragmentProcessor(const GrProcessor& processor) {}
void emitCode(EmitArgs&) override;
private:
typedef GrGLSLFragmentProcessor INHERITED;
};
/////////////////////////////////////////////////////////////////////
GR_DEFINE_FRAGMENT_PROCESSOR_TEST(ComposeTwoFragmentProcessor);
const GrFragmentProcessor* ComposeTwoFragmentProcessor::TestCreate(GrProcessorTestData* d) {
// Create two random frag procs.
SkAutoTUnref<const GrFragmentProcessor> fpA(GrProcessorUnitTest::CreateChildFP(d));
SkAutoTUnref<const GrFragmentProcessor> fpB(GrProcessorUnitTest::CreateChildFP(d));
SkXfermode::Mode mode = static_cast<SkXfermode::Mode>(
d->fRandom->nextRangeU(0, SkXfermode::kLastMode));
return new ComposeTwoFragmentProcessor(fpA, fpB, mode);
}
GrGLSLFragmentProcessor* ComposeTwoFragmentProcessor::onCreateGLSLInstance() const{
return new GLComposeTwoFragmentProcessor(*this);
}
/////////////////////////////////////////////////////////////////////
void GLComposeTwoFragmentProcessor::emitCode(EmitArgs& args) {
GrGLSLFragmentBuilder* fragBuilder = args.fFragBuilder;
const ComposeTwoFragmentProcessor& cs = args.fFp.cast<ComposeTwoFragmentProcessor>();
const char* inputColor = nullptr;
if (args.fInputColor) {
inputColor = "inputColor";
fragBuilder->codeAppendf("vec4 inputColor = vec4(%s.rgb, 1.0);", args.fInputColor);
}
// declare outputColor and emit the code for each of the two children
SkString srcColor("src");
this->emitChild(0, inputColor, &srcColor, args);
SkString dstColor("dst");
this->emitChild(1, inputColor, &dstColor, args);
// emit blend code
SkXfermode::Mode mode = cs.getMode();
fragBuilder->codeAppendf("// Compose Xfer Mode: %s\n", SkXfermode::ModeName(mode));
GrGLSLBlend::AppendMode(fragBuilder,
srcColor.c_str(),
dstColor.c_str(),
args.fOutputColor,
mode);
// re-multiply the output color by the input color's alpha
if (args.fInputColor) {
fragBuilder->codeAppendf("%s *= %s.a;", args.fOutputColor, args.fInputColor);
}
}
const GrFragmentProcessor* GrXfermodeFragmentProcessor::CreateFromTwoProcessors(
const GrFragmentProcessor* src, const GrFragmentProcessor* dst, SkXfermode::Mode mode) {
switch (mode) {
case SkXfermode::kClear_Mode:
return GrConstColorProcessor::Create(GrColor_TRANSPARENT_BLACK,
GrConstColorProcessor::kIgnore_InputMode);
case SkXfermode::kSrc_Mode:
return SkRef(src);
case SkXfermode::kDst_Mode:
return SkRef(dst);
default:
return new ComposeTwoFragmentProcessor(src, dst, mode);
}
}
//////////////////////////////////////////////////////////////////////////////
class ComposeOneFragmentProcessor : public GrFragmentProcessor {
public:
enum Child {
kDst_Child,
kSrc_Child,
};
ComposeOneFragmentProcessor(const GrFragmentProcessor* dst, SkXfermode::Mode mode, Child child)
: fMode(mode)
, fChild(child) {
this->initClassID<ComposeOneFragmentProcessor>();
SkDEBUGCODE(int dstIndex = )this->registerChildProcessor(dst);
SkASSERT(0 == dstIndex);
}
const char* name() const override { return "ComposeOne"; }
SkString dumpInfo() const override {
SkString str;
for (int i = 0; i < this->numChildProcessors(); ++i) {
str.append(this->childProcessor(i).dumpInfo());
}
return str;
}
void onGetGLSLProcessorKey(const GrGLSLCaps& caps, GrProcessorKeyBuilder* b) const override {
GR_STATIC_ASSERT((SkXfermode::kLastMode & SK_MaxU16) == SkXfermode::kLastMode);
b->add32(fMode | (fChild << 16));
}
SkXfermode::Mode mode() const { return fMode; }
Child child() const { return fChild; }
protected:
bool onIsEqual(const GrFragmentProcessor& that) const override {
return fMode == that.cast<ComposeOneFragmentProcessor>().fMode;
}
void onComputeInvariantOutput(GrInvariantOutput* inout) const override {
SkXfermode::Coeff skSrcCoeff, skDstCoeff;
if (SkXfermode::ModeAsCoeff(fMode, &skSrcCoeff, &skDstCoeff)) {
GrBlendCoeff srcCoeff = SkXfermodeCoeffToGrBlendCoeff(skSrcCoeff);
GrBlendCoeff dstCoeff = SkXfermodeCoeffToGrBlendCoeff(skDstCoeff);
GrInvariantOutput childOutput(0xFFFFFFFF, kRGBA_GrColorComponentFlags, false);
this->childProcessor(0).computeInvariantOutput(&childOutput);
GrColor blendColor;
GrColorComponentFlags blendFlags;
if (kDst_Child == fChild) {
GrGetCoeffBlendKnownComponents(srcCoeff, dstCoeff,
inout->color(), inout->validFlags(),
childOutput.color(), childOutput.validFlags(),
&blendColor, &blendFlags);
} else {
GrGetCoeffBlendKnownComponents(srcCoeff, dstCoeff,
childOutput.color(), childOutput.validFlags(),
inout->color(), inout->validFlags(),
&blendColor, &blendFlags);
}
// will the shader code reference the input color?
GrInvariantOutput::ReadInput readsInput = GrInvariantOutput::kWillNot_ReadInput;
if (kDst_Child == fChild) {
if (kZero_GrBlendCoeff != srcCoeff || GrBlendCoeffRefsSrc(dstCoeff)) {
readsInput = GrInvariantOutput::kWill_ReadInput;
}
} else {
if (kZero_GrBlendCoeff != dstCoeff || GrBlendCoeffRefsDst(srcCoeff)) {
readsInput = GrInvariantOutput::kWill_ReadInput;
}
}
inout->setToOther(blendFlags, blendColor, readsInput);
} else {
inout->setToUnknown(GrInvariantOutput::kWill_ReadInput);
}
}
private:
GrGLSLFragmentProcessor* onCreateGLSLInstance() const override;
SkXfermode::Mode fMode;
Child fChild;
GR_DECLARE_FRAGMENT_PROCESSOR_TEST;
typedef GrFragmentProcessor INHERITED;
};
//////////////////////////////////////////////////////////////////////////////
class GLComposeOneFragmentProcessor : public GrGLSLFragmentProcessor {
public:
GLComposeOneFragmentProcessor(const GrProcessor& processor) {}
void emitCode(EmitArgs& args) override {
GrGLSLFragmentBuilder* fragBuilder = args.fFragBuilder;
SkXfermode::Mode mode = args.fFp.cast<ComposeOneFragmentProcessor>().mode();
ComposeOneFragmentProcessor::Child child =
args.fFp.cast<ComposeOneFragmentProcessor>().child();
SkString childColor("child");
this->emitChild(0, nullptr, &childColor, args);
const char* inputColor = args.fInputColor;
// We don't try to optimize for this case at all
if (!inputColor) {
fragBuilder->codeAppendf("const vec4 ones = vec4(1);");
inputColor = "ones";
}
// emit blend code
fragBuilder->codeAppendf("// Compose Xfer Mode: %s\n", SkXfermode::ModeName(mode));
const char* childStr = childColor.c_str();
if (ComposeOneFragmentProcessor::kDst_Child == child) {
GrGLSLBlend::AppendMode(fragBuilder, inputColor, childStr, args.fOutputColor, mode);
} else {
GrGLSLBlend::AppendMode(fragBuilder, childStr, inputColor, args.fOutputColor, mode);
}
}
private:
typedef GrGLSLFragmentProcessor INHERITED;
};
/////////////////////////////////////////////////////////////////////
GR_DEFINE_FRAGMENT_PROCESSOR_TEST(ComposeOneFragmentProcessor);
const GrFragmentProcessor* ComposeOneFragmentProcessor::TestCreate(GrProcessorTestData* d) {
// Create one random frag procs.
// For now, we'll prevent either children from being a shader with children to prevent the
// possibility of an arbitrarily large tree of procs.
SkAutoTUnref<const GrFragmentProcessor> dst(GrProcessorUnitTest::CreateChildFP(d));
SkXfermode::Mode mode = static_cast<SkXfermode::Mode>(
d->fRandom->nextRangeU(0, SkXfermode::kLastMode));
ComposeOneFragmentProcessor::Child child = d->fRandom->nextBool() ?
ComposeOneFragmentProcessor::kDst_Child :
ComposeOneFragmentProcessor::kSrc_Child;
return new ComposeOneFragmentProcessor(dst, mode, child);
}
GrGLSLFragmentProcessor* ComposeOneFragmentProcessor::onCreateGLSLInstance() const {
return new GLComposeOneFragmentProcessor(*this);
}
//////////////////////////////////////////////////////////////////////////////
const GrFragmentProcessor* GrXfermodeFragmentProcessor::CreateFromDstProcessor(
const GrFragmentProcessor* dst, SkXfermode::Mode mode) {
switch (mode) {
case SkXfermode::kClear_Mode:
return GrConstColorProcessor::Create(GrColor_TRANSPARENT_BLACK,
GrConstColorProcessor::kIgnore_InputMode);
case SkXfermode::kSrc_Mode:
return nullptr;
default:
return new ComposeOneFragmentProcessor(dst, mode,
ComposeOneFragmentProcessor::kDst_Child);
}
}
const GrFragmentProcessor* GrXfermodeFragmentProcessor::CreateFromSrcProcessor(
const GrFragmentProcessor* src, SkXfermode::Mode mode) {
switch (mode) {
case SkXfermode::kClear_Mode:
return GrConstColorProcessor::Create(GrColor_TRANSPARENT_BLACK,
GrConstColorProcessor::kIgnore_InputMode);
case SkXfermode::kDst_Mode:
return nullptr;
default:
return new ComposeOneFragmentProcessor(src, mode,
ComposeOneFragmentProcessor::kSrc_Child);
}
}
|