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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
|
/*
* Copyright 2013 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include "SkAlphaThresholdFilter.h"
#include "SkBitmap.h"
#include "SkDevice.h"
#include "SkReadBuffer.h"
#include "SkWriteBuffer.h"
#include "SkRegion.h"
#if SK_SUPPORT_GPU
#include "GrDrawContext.h"
#endif
class SK_API SkAlphaThresholdFilterImpl : public SkImageFilter {
public:
SkAlphaThresholdFilterImpl(const SkRegion& region, SkScalar innerThreshold,
SkScalar outerThreshold, SkImageFilter* input);
SK_TO_STRING_OVERRIDE()
SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkAlphaThresholdFilterImpl)
friend void SkAlphaThresholdFilter::InitializeFlattenables();
protected:
void flatten(SkWriteBuffer&) const override;
bool onFilterImageDeprecated(Proxy*, const SkBitmap& src, const Context&,
SkBitmap* result, SkIPoint* offset) const override;
#if SK_SUPPORT_GPU
bool asFragmentProcessor(GrFragmentProcessor**, GrTexture*, const SkMatrix&,
const SkIRect& bounds) const override;
#endif
private:
SkRegion fRegion;
SkScalar fInnerThreshold;
SkScalar fOuterThreshold;
typedef SkImageFilter INHERITED;
};
SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_START(SkAlphaThresholdFilter)
SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkAlphaThresholdFilterImpl)
SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_END
static SkScalar pin_0_1(SkScalar x) {
return SkMinScalar(SkMaxScalar(x, 0), 1);
}
SkImageFilter* SkAlphaThresholdFilter::Create(const SkRegion& region,
SkScalar innerThreshold,
SkScalar outerThreshold,
SkImageFilter* input) {
innerThreshold = pin_0_1(innerThreshold);
outerThreshold = pin_0_1(outerThreshold);
if (!SkScalarIsFinite(innerThreshold) || !SkScalarIsFinite(outerThreshold)) {
return nullptr;
}
return new SkAlphaThresholdFilterImpl(region, innerThreshold, outerThreshold, input);
}
#if SK_SUPPORT_GPU
#include "GrContext.h"
#include "GrCoordTransform.h"
#include "GrFragmentProcessor.h"
#include "GrInvariantOutput.h"
#include "GrTextureAccess.h"
#include "effects/GrPorterDuffXferProcessor.h"
#include "SkGr.h"
#include "glsl/GrGLSLFragmentProcessor.h"
#include "glsl/GrGLSLFragmentShaderBuilder.h"
#include "glsl/GrGLSLProgramDataManager.h"
#include "glsl/GrGLSLUniformHandler.h"
namespace {
SkMatrix make_div_and_translate_matrix(GrTexture* texture, int x, int y) {
SkMatrix matrix = GrCoordTransform::MakeDivByTextureWHMatrix(texture);
matrix.preTranslate(SkIntToScalar(x), SkIntToScalar(y));
return matrix;
}
};
class AlphaThresholdEffect : public GrFragmentProcessor {
public:
static GrFragmentProcessor* Create(GrTexture* texture,
GrTexture* maskTexture,
float innerThreshold,
float outerThreshold,
const SkIRect& bounds) {
return new AlphaThresholdEffect(texture, maskTexture, innerThreshold, outerThreshold,
bounds);
}
virtual ~AlphaThresholdEffect() {};
const char* name() const override { return "Alpha Threshold"; }
float innerThreshold() const { return fInnerThreshold; }
float outerThreshold() const { return fOuterThreshold; }
private:
AlphaThresholdEffect(GrTexture* texture,
GrTexture* maskTexture,
float innerThreshold,
float outerThreshold,
const SkIRect& bounds)
: fInnerThreshold(innerThreshold)
, fOuterThreshold(outerThreshold)
, fImageCoordTransform(kLocal_GrCoordSet,
GrCoordTransform::MakeDivByTextureWHMatrix(texture), texture,
GrTextureParams::kNone_FilterMode)
, fImageTextureAccess(texture)
, fMaskCoordTransform(kLocal_GrCoordSet,
make_div_and_translate_matrix(maskTexture, -bounds.x(), -bounds.y()),
maskTexture,
GrTextureParams::kNone_FilterMode)
, fMaskTextureAccess(maskTexture) {
this->initClassID<AlphaThresholdEffect>();
this->addCoordTransform(&fImageCoordTransform);
this->addTextureAccess(&fImageTextureAccess);
this->addCoordTransform(&fMaskCoordTransform);
this->addTextureAccess(&fMaskTextureAccess);
}
GrGLSLFragmentProcessor* onCreateGLSLInstance() const override;
void onGetGLSLProcessorKey(const GrGLSLCaps&, GrProcessorKeyBuilder*) const override;
bool onIsEqual(const GrFragmentProcessor&) const override;
void onComputeInvariantOutput(GrInvariantOutput* inout) const override;
GR_DECLARE_FRAGMENT_PROCESSOR_TEST;
float fInnerThreshold;
float fOuterThreshold;
GrCoordTransform fImageCoordTransform;
GrTextureAccess fImageTextureAccess;
GrCoordTransform fMaskCoordTransform;
GrTextureAccess fMaskTextureAccess;
typedef GrFragmentProcessor INHERITED;
};
class GrGLAlphaThresholdEffect : public GrGLSLFragmentProcessor {
public:
void emitCode(EmitArgs&) override;
protected:
void onSetData(const GrGLSLProgramDataManager&, const GrProcessor&) override;
private:
GrGLSLProgramDataManager::UniformHandle fInnerThresholdVar;
GrGLSLProgramDataManager::UniformHandle fOuterThresholdVar;
typedef GrGLSLFragmentProcessor INHERITED;
};
void GrGLAlphaThresholdEffect::emitCode(EmitArgs& args) {
GrGLSLUniformHandler* uniformHandler = args.fUniformHandler;
fInnerThresholdVar = uniformHandler->addUniform(kFragment_GrShaderFlag,
kFloat_GrSLType, kDefault_GrSLPrecision,
"inner_threshold");
fOuterThresholdVar = uniformHandler->addUniform(kFragment_GrShaderFlag,
kFloat_GrSLType, kDefault_GrSLPrecision,
"outer_threshold");
GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
SkString coords2D = fragBuilder->ensureFSCoords2D(args.fCoords, 0);
SkString maskCoords2D = fragBuilder->ensureFSCoords2D(args.fCoords, 1);
fragBuilder->codeAppendf("\t\tvec2 coord = %s;\n", coords2D.c_str());
fragBuilder->codeAppendf("\t\tvec2 mask_coord = %s;\n", maskCoords2D.c_str());
fragBuilder->codeAppend("\t\tvec4 input_color = ");
fragBuilder->appendTextureLookup(args.fSamplers[0], "coord");
fragBuilder->codeAppend(";\n");
fragBuilder->codeAppend("\t\tvec4 mask_color = ");
fragBuilder->appendTextureLookup(args.fSamplers[1], "mask_coord");
fragBuilder->codeAppend(";\n");
fragBuilder->codeAppendf("\t\tfloat inner_thresh = %s;\n",
uniformHandler->getUniformCStr(fInnerThresholdVar));
fragBuilder->codeAppendf("\t\tfloat outer_thresh = %s;\n",
uniformHandler->getUniformCStr(fOuterThresholdVar));
fragBuilder->codeAppend("\t\tfloat mask = mask_color.a;\n");
fragBuilder->codeAppend("vec4 color = input_color;\n");
fragBuilder->codeAppend("\t\tif (mask < 0.5) {\n"
"\t\t\tif (color.a > outer_thresh) {\n"
"\t\t\t\tfloat scale = outer_thresh / color.a;\n"
"\t\t\t\tcolor.rgb *= scale;\n"
"\t\t\t\tcolor.a = outer_thresh;\n"
"\t\t\t}\n"
"\t\t} else if (color.a < inner_thresh) {\n"
"\t\t\tfloat scale = inner_thresh / max(0.001, color.a);\n"
"\t\t\tcolor.rgb *= scale;\n"
"\t\t\tcolor.a = inner_thresh;\n"
"\t\t}\n");
fragBuilder->codeAppendf("%s = %s;\n", args.fOutputColor,
(GrGLSLExpr4(args.fInputColor) * GrGLSLExpr4("color")).c_str());
}
void GrGLAlphaThresholdEffect::onSetData(const GrGLSLProgramDataManager& pdman,
const GrProcessor& proc) {
const AlphaThresholdEffect& alpha_threshold = proc.cast<AlphaThresholdEffect>();
pdman.set1f(fInnerThresholdVar, alpha_threshold.innerThreshold());
pdman.set1f(fOuterThresholdVar, alpha_threshold.outerThreshold());
}
/////////////////////////////////////////////////////////////////////
GR_DEFINE_FRAGMENT_PROCESSOR_TEST(AlphaThresholdEffect);
const GrFragmentProcessor* AlphaThresholdEffect::TestCreate(GrProcessorTestData* d) {
GrTexture* bmpTex = d->fTextures[GrProcessorUnitTest::kSkiaPMTextureIdx];
GrTexture* maskTex = d->fTextures[GrProcessorUnitTest::kAlphaTextureIdx];
float innerThresh = d->fRandom->nextUScalar1();
float outerThresh = d->fRandom->nextUScalar1();
const int kMaxWidth = 1000;
const int kMaxHeight = 1000;
uint32_t width = d->fRandom->nextULessThan(kMaxWidth);
uint32_t height = d->fRandom->nextULessThan(kMaxHeight);
uint32_t x = d->fRandom->nextULessThan(kMaxWidth - width);
uint32_t y = d->fRandom->nextULessThan(kMaxHeight - height);
SkIRect bounds = SkIRect::MakeXYWH(x, y, width, height);
return AlphaThresholdEffect::Create(bmpTex, maskTex, innerThresh, outerThresh, bounds);
}
///////////////////////////////////////////////////////////////////////////////
void AlphaThresholdEffect::onGetGLSLProcessorKey(const GrGLSLCaps& caps,
GrProcessorKeyBuilder* b) const {
GrGLAlphaThresholdEffect::GenKey(*this, caps, b);
}
GrGLSLFragmentProcessor* AlphaThresholdEffect::onCreateGLSLInstance() const {
return new GrGLAlphaThresholdEffect;
}
bool AlphaThresholdEffect::onIsEqual(const GrFragmentProcessor& sBase) const {
const AlphaThresholdEffect& s = sBase.cast<AlphaThresholdEffect>();
return (this->fInnerThreshold == s.fInnerThreshold &&
this->fOuterThreshold == s.fOuterThreshold);
}
void AlphaThresholdEffect::onComputeInvariantOutput(GrInvariantOutput* inout) const {
if (GrPixelConfigIsAlphaOnly(this->texture(0)->config())) {
inout->mulByUnknownSingleComponent();
} else if (GrPixelConfigIsOpaque(this->texture(0)->config()) && fOuterThreshold >= 1.f) {
inout->mulByUnknownOpaqueFourComponents();
} else {
inout->mulByUnknownFourComponents();
}
}
#endif
SkFlattenable* SkAlphaThresholdFilterImpl::CreateProc(SkReadBuffer& buffer) {
SK_IMAGEFILTER_UNFLATTEN_COMMON(common, 1);
SkScalar inner = buffer.readScalar();
SkScalar outer = buffer.readScalar();
SkRegion rgn;
buffer.readRegion(&rgn);
return SkAlphaThresholdFilter::Create(rgn, inner, outer, common.getInput(0));
}
SkAlphaThresholdFilterImpl::SkAlphaThresholdFilterImpl(const SkRegion& region,
SkScalar innerThreshold,
SkScalar outerThreshold,
SkImageFilter* input)
: INHERITED(1, &input)
, fRegion(region)
, fInnerThreshold(innerThreshold)
, fOuterThreshold(outerThreshold) {
}
#if SK_SUPPORT_GPU
bool SkAlphaThresholdFilterImpl::asFragmentProcessor(GrFragmentProcessor** fp,
GrTexture* texture,
const SkMatrix& inMatrix,
const SkIRect& bounds) const {
if (fp) {
GrContext* context = texture->getContext();
GrSurfaceDesc maskDesc;
if (context->caps()->isConfigRenderable(kAlpha_8_GrPixelConfig, false)) {
maskDesc.fConfig = kAlpha_8_GrPixelConfig;
} else {
maskDesc.fConfig = kRGBA_8888_GrPixelConfig;
}
maskDesc.fFlags = kRenderTarget_GrSurfaceFlag;
// Add one pixel of border to ensure that clamp mode will be all zeros
// the outside.
maskDesc.fWidth = bounds.width();
maskDesc.fHeight = bounds.height();
SkAutoTUnref<GrTexture> maskTexture(
context->textureProvider()->createApproxTexture(maskDesc));
if (!maskTexture) {
return false;
}
SkAutoTUnref<GrDrawContext> drawContext(
context->drawContext(maskTexture->asRenderTarget()));
if (drawContext) {
GrPaint grPaint;
grPaint.setPorterDuffXPFactory(SkXfermode::kSrc_Mode);
SkRegion::Iterator iter(fRegion);
drawContext->clear(nullptr, 0x0, true);
GrClip clip(SkRect::Make(SkIRect::MakeWH(bounds.width(), bounds.height())));
while (!iter.done()) {
SkRect rect = SkRect::Make(iter.rect());
drawContext->drawRect(clip, grPaint, inMatrix, rect);
iter.next();
}
}
*fp = AlphaThresholdEffect::Create(texture,
maskTexture,
fInnerThreshold,
fOuterThreshold,
bounds);
}
return true;
}
#endif
void SkAlphaThresholdFilterImpl::flatten(SkWriteBuffer& buffer) const {
this->INHERITED::flatten(buffer);
buffer.writeScalar(fInnerThreshold);
buffer.writeScalar(fOuterThreshold);
buffer.writeRegion(fRegion);
}
bool SkAlphaThresholdFilterImpl::onFilterImageDeprecated(Proxy* proxy, const SkBitmap& src,
const Context& ctx, SkBitmap* dst,
SkIPoint* offset) const {
if (src.colorType() != kN32_SkColorType) {
return false;
}
SkMatrix localInverse;
if (!ctx.ctm().invert(&localInverse)) {
return false;
}
SkAutoLockPixels alp(src);
SkASSERT(src.getPixels());
if (!src.getPixels() || src.width() <= 0 || src.height() <= 0) {
return false;
}
SkAutoTUnref<SkBaseDevice> device(proxy->createDevice(src.width(), src.height()));
if (!device) {
return false;
}
*dst = device->accessBitmap(false);
SkAutoLockPixels alp_dst(*dst);
U8CPU innerThreshold = (U8CPU)(fInnerThreshold * 0xFF);
U8CPU outerThreshold = (U8CPU)(fOuterThreshold * 0xFF);
SkColor* sptr = src.getAddr32(0, 0);
SkColor* dptr = dst->getAddr32(0, 0);
int width = src.width(), height = src.height();
for (int y = 0; y < height; ++y) {
for (int x = 0; x < width; ++x) {
const SkColor& source = sptr[y * width + x];
SkColor output_color(source);
SkPoint position;
localInverse.mapXY((SkScalar)x, (SkScalar)y, &position);
if (fRegion.contains((int32_t)position.x(), (int32_t)position.y())) {
if (SkColorGetA(source) < innerThreshold) {
U8CPU alpha = SkColorGetA(source);
if (alpha == 0)
alpha = 1;
float scale = (float)innerThreshold / alpha;
output_color = SkColorSetARGB(innerThreshold,
(U8CPU)(SkColorGetR(source) * scale),
(U8CPU)(SkColorGetG(source) * scale),
(U8CPU)(SkColorGetB(source) * scale));
}
} else {
if (SkColorGetA(source) > outerThreshold) {
float scale = (float)outerThreshold / SkColorGetA(source);
output_color = SkColorSetARGB(outerThreshold,
(U8CPU)(SkColorGetR(source) * scale),
(U8CPU)(SkColorGetG(source) * scale),
(U8CPU)(SkColorGetB(source) * scale));
}
}
dptr[y * dst->width() + x] = output_color;
}
}
return true;
}
#ifndef SK_IGNORE_TO_STRING
void SkAlphaThresholdFilterImpl::toString(SkString* str) const {
str->appendf("SkAlphaThresholdImageFilter: (");
str->appendf("inner: %f outer: %f", fInnerThreshold, fOuterThreshold);
str->append(")");
}
#endif
|