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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
|
/*
* Copyright 2011 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include "SkPixelXorXfermode.h"
#include "SkColorPriv.h"
#include "SkReadBuffer.h"
#include "SkWriteBuffer.h"
#include "SkString.h"
// we always return an opaque color, 'cause I don't know what to do with
// the alpha-component and still return a valid premultiplied color.
SkPMColor SkPixelXorXfermode::xferColor(SkPMColor src, SkPMColor dst) const {
SkPMColor res = src ^ dst ^ fOpColor;
res |= (SK_A32_MASK << SK_A32_SHIFT); // force it to be opaque
return res;
}
void SkPixelXorXfermode::flatten(SkWriteBuffer& wb) const {
wb.writeColor(SkColorSetRGB(SkGetPackedR32(fOpColor),
SkGetPackedG32(fOpColor),
SkGetPackedB32(fOpColor)));
}
SkFlattenable* SkPixelXorXfermode::CreateProc(SkReadBuffer& buffer) {
return Create(buffer.readColor());
}
#ifndef SK_IGNORE_TO_STRING
void SkPixelXorXfermode::toString(SkString* str) const {
str->append("SkPixelXorXfermode: ");
str->appendHex(fOpColor);
}
#endif
#if SK_SUPPORT_GPU
#include "GrFragmentProcessor.h"
#include "GrInvariantOutput.h"
#include "GrXferProcessor.h"
#include "glsl/GrGLSLFragmentProcessor.h"
#include "glsl/GrGLSLFragmentShaderBuilder.h"
#include "glsl/GrGLSLProgramDataManager.h"
#include "glsl/GrGLSLUniformHandler.h"
#include "glsl/GrGLSLXferProcessor.h"
///////////////////////////////////////////////////////////////////////////////
// Fragment Processor
///////////////////////////////////////////////////////////////////////////////
static void add_pixelxor_code(GrGLSLFragmentBuilder* fragBuilder,
const char* srcColor,
const char* dstColor,
const char* outputColor,
const char* opColor) {
static const GrGLSLShaderVar gXorArgs[] = {
GrGLSLShaderVar("f1", kFloat_GrSLType),
GrGLSLShaderVar("f2", kFloat_GrSLType),
GrGLSLShaderVar("f3", kFloat_GrSLType),
GrGLSLShaderVar("fPowerOf2Divisor", kFloat_GrSLType),
};
SkString xorFuncName;
// The xor function checks if the three passed in floats (f1, f2, f3) would
// have a bit in the log2(fPowerOf2Divisor)-th position if they were
// represented by an int. It then performs an xor of the 3 bits (using
// the property that serial xors can be treated as a sum of 0s & 1s mod 2).
fragBuilder->emitFunction(kFloat_GrSLType,
"xor",
SK_ARRAY_COUNT(gXorArgs),
gXorArgs,
"float bit1 = floor(f1 / fPowerOf2Divisor);"
"float bit2 = floor(f2 / fPowerOf2Divisor);"
"float bit3 = floor(f3 / fPowerOf2Divisor);"
"return mod(bit1 + bit2 + bit3, 2.0);",
&xorFuncName);
fragBuilder->codeAppend("float red = 0.0, green = 0.0, blue = 0.0;");
if (srcColor) {
fragBuilder->codeAppendf("vec3 src = 255.99 * %s.rgb;", srcColor);
} else {
fragBuilder->codeAppendf("vec3 src = vec3(255.99);");
}
fragBuilder->codeAppendf("vec3 dst = 255.99 * %s.rgb;", dstColor);
fragBuilder->codeAppendf("vec3 op = 255.99 * %s;", opColor);
fragBuilder->codeAppend("float modValue = 128.0;");
fragBuilder->codeAppend("for (int i = 0; i < 8; i++) {");
fragBuilder->codeAppendf("float bit = %s(src.r, dst.r, op.r, modValue);", xorFuncName.c_str());
fragBuilder->codeAppend("red += modValue * bit;");
fragBuilder->codeAppend("src.r = mod(src.r, modValue);");
fragBuilder->codeAppend("dst.r = mod(dst.r, modValue);");
fragBuilder->codeAppend("op.r = mod(op.r, modValue);");
fragBuilder->codeAppendf("bit = %s(src.g, dst.g, op.g, modValue);", xorFuncName.c_str());
fragBuilder->codeAppend("green += modValue * bit;");
fragBuilder->codeAppend("src.g = mod(src.g, modValue);");
fragBuilder->codeAppend("dst.g = mod(dst.g, modValue);");
fragBuilder->codeAppend("op.g = mod(op.g, modValue);");
fragBuilder->codeAppendf("bit = %s(src.b, dst.b, op.b, modValue);", xorFuncName.c_str());
fragBuilder->codeAppend("blue += modValue * bit;");
fragBuilder->codeAppend("src.b = mod(src.b, modValue);");
fragBuilder->codeAppend("dst.b = mod(dst.b, modValue);");
fragBuilder->codeAppend("op.b = mod(op.b, modValue);");
fragBuilder->codeAppend("modValue /= 2.0;");
fragBuilder->codeAppend("}");
fragBuilder->codeAppendf("%s = vec4(red/255.0, green/255.0, blue/255.0, 1.0);", outputColor);
}
class GLPixelXorFP;
class PixelXorFP : public GrFragmentProcessor {
public:
static const GrFragmentProcessor* Create(SkPMColor opColor, const GrFragmentProcessor* dst) {
return new PixelXorFP(opColor, dst);
}
~PixelXorFP() override {};
const char* name() const override { return "PixelXor"; }
SkString dumpInfo() const override {
SkString str;
str.appendf("Color: 0x%08x", fOpColor);
return str;
}
SkPMColor opColor() const { return fOpColor; }
private:
GrGLSLFragmentProcessor* onCreateGLSLInstance() const override;
void onGetGLSLProcessorKey(const GrGLSLCaps& caps, GrProcessorKeyBuilder* b) const override;
bool onIsEqual(const GrFragmentProcessor& fpBase) const override {
const PixelXorFP& fp = fpBase.cast<PixelXorFP>();
return fOpColor == fp.fOpColor;
}
void onComputeInvariantOutput(GrInvariantOutput* inout) const override {
inout->setToUnknown(GrInvariantOutput::kWill_ReadInput);
}
PixelXorFP(SkPMColor opColor, const GrFragmentProcessor* dst)
: fOpColor(opColor) {
this->initClassID<PixelXorFP>();
SkASSERT(dst);
SkDEBUGCODE(int dstIndex = )this->registerChildProcessor(dst);
SkASSERT(0 == dstIndex);
}
SkPMColor fOpColor;
GR_DECLARE_FRAGMENT_PROCESSOR_TEST;
typedef GrFragmentProcessor INHERITED;
};
///////////////////////////////////////////////////////////////////////////////
class GLPixelXorFP : public GrGLSLFragmentProcessor {
public:
void emitCode(EmitArgs& args) override {
GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
SkString dstColor("dstColor");
this->emitChild(0, nullptr, &dstColor, args);
fOpColorUni = args.fUniformHandler->addUniform(kFragment_GrShaderFlag,
kVec3f_GrSLType, kHigh_GrSLPrecision,
"opColor");
const char* kOpColorUni = args.fUniformHandler->getUniformCStr(fOpColorUni);
add_pixelxor_code(fragBuilder, args.fInputColor, dstColor.c_str(),
args.fOutputColor, kOpColorUni);
}
static void GenKey(const GrProcessor&, const GrGLSLCaps&, GrProcessorKeyBuilder*) { }
protected:
void onSetData(const GrGLSLProgramDataManager& pdman, const GrProcessor& proc) override {
const PixelXorFP& pixXor = proc.cast<PixelXorFP>();
pdman.set3f(fOpColorUni,
SkGetPackedR32(pixXor.opColor())/255.0f,
SkGetPackedG32(pixXor.opColor())/255.0f,
SkGetPackedB32(pixXor.opColor())/255.0f);
}
private:
GrGLSLProgramDataManager::UniformHandle fOpColorUni;
typedef GrGLSLFragmentProcessor INHERITED;
};
///////////////////////////////////////////////////////////////////////////////
GrGLSLFragmentProcessor* PixelXorFP::onCreateGLSLInstance() const {
return new GLPixelXorFP;
}
void PixelXorFP::onGetGLSLProcessorKey(const GrGLSLCaps& caps, GrProcessorKeyBuilder* b) const {
GLPixelXorFP::GenKey(*this, caps, b);
}
const GrFragmentProcessor* PixelXorFP::TestCreate(GrProcessorTestData* d) {
SkColor color = d->fRandom->nextU();
SkAutoTUnref<const GrFragmentProcessor> dst(GrProcessorUnitTest::CreateChildFP(d));
return new PixelXorFP(SkPreMultiplyColor(color), dst);
}
GR_DEFINE_FRAGMENT_PROCESSOR_TEST(PixelXorFP);
///////////////////////////////////////////////////////////////////////////////
// Xfer Processor
///////////////////////////////////////////////////////////////////////////////
class PixelXorXP : public GrXferProcessor {
public:
PixelXorXP(const DstTexture* dstTexture, bool hasMixedSamples, SkPMColor opColor)
: INHERITED(dstTexture, true, hasMixedSamples)
, fOpColor(opColor) {
this->initClassID<PixelXorXP>();
}
const char* name() const override { return "PixelXor"; }
GrGLSLXferProcessor* createGLSLInstance() const override;
SkPMColor opColor() const { return fOpColor; }
private:
GrXferProcessor::OptFlags onGetOptimizations(const GrPipelineOptimizations& optimizations,
bool doesStencilWrite,
GrColor* overrideColor,
const GrCaps& caps) const override {
return GrXferProcessor::kNone_OptFlags;
}
void onGetGLSLProcessorKey(const GrGLSLCaps& caps, GrProcessorKeyBuilder* b) const override;
bool onIsEqual(const GrXferProcessor& xpBase) const override {
const PixelXorXP& xp = xpBase.cast<PixelXorXP>();
return fOpColor == xp.fOpColor;
}
SkPMColor fOpColor;
typedef GrXferProcessor INHERITED;
};
///////////////////////////////////////////////////////////////////////////////
class GLPixelXorXP : public GrGLSLXferProcessor {
public:
GLPixelXorXP(const PixelXorXP& pixelXorXP) { }
~GLPixelXorXP() override {}
static void GenKey(const GrProcessor&, const GrGLSLCaps&, GrProcessorKeyBuilder*) { }
private:
void emitBlendCodeForDstRead(GrGLSLXPFragmentBuilder* fragBuilder,
GrGLSLUniformHandler* uniformHandler,
const char* srcColor,
const char* srcCoverage,
const char* dstColor,
const char* outColor,
const char* outColorSecondary,
const GrXferProcessor& proc) override {
fOpColorUni = uniformHandler->addUniform(kFragment_GrShaderFlag,
kVec3f_GrSLType, kHigh_GrSLPrecision,
"opColor");
const char* kOpColorUni = uniformHandler->getUniformCStr(fOpColorUni);
add_pixelxor_code(fragBuilder, srcColor, dstColor, outColor, kOpColorUni);
// Apply coverage.
INHERITED::DefaultCoverageModulation(fragBuilder, srcCoverage, dstColor, outColor,
outColorSecondary, proc);
}
void onSetData(const GrGLSLProgramDataManager& pdman,
const GrXferProcessor& processor) override {
const PixelXorXP& pixelXor = processor.cast<PixelXorXP>();
pdman.set3f(fOpColorUni,
SkGetPackedR32(pixelXor.opColor())/255.0f,
SkGetPackedG32(pixelXor.opColor())/255.0f,
SkGetPackedB32(pixelXor.opColor())/255.0f);
};
GrGLSLProgramDataManager::UniformHandle fOpColorUni;
typedef GrGLSLXferProcessor INHERITED;
};
///////////////////////////////////////////////////////////////////////////////
void PixelXorXP::onGetGLSLProcessorKey(const GrGLSLCaps& caps, GrProcessorKeyBuilder* b) const {
GLPixelXorXP::GenKey(*this, caps, b);
}
GrGLSLXferProcessor* PixelXorXP::createGLSLInstance() const { return new GLPixelXorXP(*this); }
///////////////////////////////////////////////////////////////////////////////
class GrPixelXorXPFactory : public GrXPFactory {
public:
static GrXPFactory* Create(SkPMColor opColor) {
return new GrPixelXorXPFactory(opColor);
}
void getInvariantBlendedColor(const GrProcOptInfo& colorPOI,
GrXPFactory::InvariantBlendedColor* blendedColor) const override {
blendedColor->fWillBlendWithDst = true;
blendedColor->fKnownColorFlags = kNone_GrColorComponentFlags;
}
private:
GrPixelXorXPFactory(SkPMColor opColor)
: fOpColor(opColor) {
this->initClassID<GrPixelXorXPFactory>();
}
GrXferProcessor* onCreateXferProcessor(const GrCaps& caps,
const GrPipelineOptimizations& optimizations,
bool hasMixedSamples,
const DstTexture* dstTexture) const override {
return new PixelXorXP(dstTexture, hasMixedSamples, fOpColor);
}
bool onWillReadDstColor(const GrCaps& caps,
const GrPipelineOptimizations& optimizations,
bool hasMixedSamples) const override {
return true;
}
bool onIsEqual(const GrXPFactory& xpfBase) const override {
const GrPixelXorXPFactory& xpf = xpfBase.cast<GrPixelXorXPFactory>();
return fOpColor == xpf.fOpColor;
}
GR_DECLARE_XP_FACTORY_TEST;
SkPMColor fOpColor;
typedef GrXPFactory INHERITED;
};
GR_DEFINE_XP_FACTORY_TEST(GrPixelXorXPFactory);
const GrXPFactory* GrPixelXorXPFactory::TestCreate(GrProcessorTestData* d) {
SkColor color = d->fRandom->nextU();
return GrPixelXorXPFactory::Create(SkPreMultiplyColor(color));
}
///////////////////////////////////////////////////////////////////////////////
const GrFragmentProcessor* SkPixelXorXfermode::getFragmentProcessorForImageFilter(
const GrFragmentProcessor* dst) const {
return PixelXorFP::Create(fOpColor, dst);
}
GrXPFactory* SkPixelXorXfermode::asXPFactory() const {
return GrPixelXorXPFactory::Create(fOpColor);
}
#endif
|