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
|
/*
* 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 "SkColorSpaceXformer.h"
#include "SkReadBuffer.h"
#include "SkSpecialImage.h"
#include "SkWriteBuffer.h"
#include "SkRegion.h"
#if SK_SUPPORT_GPU
#include "GrAlphaThresholdFragmentProcessor.h"
#include "GrContext.h"
#include "GrFixedClip.h"
#include "GrRenderTargetContext.h"
#include "GrTextureProxy.h"
#include "effects/GrSimpleTextureEffect.h"
#endif
class SK_API SkAlphaThresholdFilterImpl : public SkImageFilter {
public:
SkAlphaThresholdFilterImpl(const SkRegion& region, SkScalar innerThreshold,
SkScalar outerThreshold, sk_sp<SkImageFilter> input,
const CropRect* cropRect = nullptr);
SK_TO_STRING_OVERRIDE()
SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkAlphaThresholdFilterImpl)
friend void SkAlphaThresholdFilter::InitializeFlattenables();
protected:
void flatten(SkWriteBuffer&) const override;
sk_sp<SkSpecialImage> onFilterImage(SkSpecialImage* source, const Context&,
SkIPoint* offset) const override;
sk_sp<SkImageFilter> onMakeColorSpace(SkColorSpaceXformer*) const override;
#if SK_SUPPORT_GPU
sk_sp<GrTextureProxy> createMaskTexture(GrContext*,
const SkMatrix&,
const SkIRect& bounds) const;
#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);
}
sk_sp<SkImageFilter> SkAlphaThresholdFilter::Make(const SkRegion& region,
SkScalar innerThreshold,
SkScalar outerThreshold,
sk_sp<SkImageFilter> input,
const SkImageFilter::CropRect* cropRect) {
innerThreshold = pin_0_1(innerThreshold);
outerThreshold = pin_0_1(outerThreshold);
if (!SkScalarIsFinite(innerThreshold) || !SkScalarIsFinite(outerThreshold)) {
return nullptr;
}
return sk_sp<SkImageFilter>(new SkAlphaThresholdFilterImpl(region, innerThreshold,
outerThreshold,
std::move(input),
cropRect));
}
sk_sp<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::Make(rgn, inner, outer, common.getInput(0),
&common.cropRect());
}
SkAlphaThresholdFilterImpl::SkAlphaThresholdFilterImpl(const SkRegion& region,
SkScalar innerThreshold,
SkScalar outerThreshold,
sk_sp<SkImageFilter> input,
const CropRect* cropRect)
: INHERITED(&input, 1, cropRect)
, fRegion(region)
, fInnerThreshold(innerThreshold)
, fOuterThreshold(outerThreshold) {
}
#if SK_SUPPORT_GPU
sk_sp<GrTextureProxy> SkAlphaThresholdFilterImpl::createMaskTexture(GrContext* context,
const SkMatrix& inMatrix,
const SkIRect& bounds) const {
sk_sp<GrRenderTargetContext> rtContext(context->makeDeferredRenderTargetContextWithFallback(
SkBackingFit::kApprox, bounds.width(), bounds.height(), kAlpha_8_GrPixelConfig, nullptr));
if (!rtContext) {
return nullptr;
}
GrPaint paint;
paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
SkRegion::Iterator iter(fRegion);
rtContext->clear(nullptr, 0x0, true);
GrFixedClip clip(SkIRect::MakeWH(bounds.width(), bounds.height()));
while (!iter.done()) {
SkRect rect = SkRect::Make(iter.rect());
rtContext->drawRect(clip, std::move(paint), GrAA::kNo, inMatrix, rect);
iter.next();
}
return rtContext->asTextureProxyRef();
}
#endif
void SkAlphaThresholdFilterImpl::flatten(SkWriteBuffer& buffer) const {
this->INHERITED::flatten(buffer);
buffer.writeScalar(fInnerThreshold);
buffer.writeScalar(fOuterThreshold);
buffer.writeRegion(fRegion);
}
sk_sp<SkSpecialImage> SkAlphaThresholdFilterImpl::onFilterImage(SkSpecialImage* source,
const Context& ctx,
SkIPoint* offset) const {
SkIPoint inputOffset = SkIPoint::Make(0, 0);
sk_sp<SkSpecialImage> input(this->filterInput(0, source, ctx, &inputOffset));
if (!input) {
return nullptr;
}
const SkIRect inputBounds = SkIRect::MakeXYWH(inputOffset.x(), inputOffset.y(),
input->width(), input->height());
SkIRect bounds;
if (!this->applyCropRect(ctx, inputBounds, &bounds)) {
return nullptr;
}
#if SK_SUPPORT_GPU
if (source->isTextureBacked()) {
GrContext* context = source->getContext();
sk_sp<GrTextureProxy> inputProxy(input->asTextureProxyRef(context));
SkASSERT(inputProxy);
offset->fX = bounds.left();
offset->fY = bounds.top();
bounds.offset(-inputOffset);
SkMatrix matrix(ctx.ctm());
matrix.postTranslate(SkIntToScalar(-bounds.left()), SkIntToScalar(-bounds.top()));
sk_sp<GrTextureProxy> maskProxy(this->createMaskTexture(context, matrix, bounds));
if (!maskProxy) {
return nullptr;
}
const OutputProperties& outProps = ctx.outputProperties();
auto textureFP = GrSimpleTextureEffect::Make(std::move(inputProxy), SkMatrix::I());
textureFP = GrColorSpaceXformEffect::Make(std::move(textureFP), input->getColorSpace(),
outProps.colorSpace());
if (!textureFP) {
return nullptr;
}
auto thresholdFP = GrAlphaThresholdFragmentProcessor::Make(std::move(maskProxy),
fInnerThreshold,
fOuterThreshold,
bounds);
if (!thresholdFP) {
return nullptr;
}
std::unique_ptr<GrFragmentProcessor> fpSeries[] = { std::move(textureFP),
std::move(thresholdFP) };
auto fp = GrFragmentProcessor::RunInSeries(fpSeries, 2);
return DrawWithFP(context, std::move(fp), bounds, outProps);
}
#endif
SkBitmap inputBM;
if (!input->getROPixels(&inputBM)) {
return nullptr;
}
if (inputBM.colorType() != kN32_SkColorType) {
return nullptr;
}
if (!inputBM.getPixels() || inputBM.width() <= 0 || inputBM.height() <= 0) {
return nullptr;
}
SkMatrix localInverse;
if (!ctx.ctm().invert(&localInverse)) {
return nullptr;
}
SkImageInfo info = SkImageInfo::MakeN32(bounds.width(), bounds.height(),
kPremul_SkAlphaType);
SkBitmap dst;
if (!dst.tryAllocPixels(info)) {
return nullptr;
}
U8CPU innerThreshold = (U8CPU)(fInnerThreshold * 0xFF);
U8CPU outerThreshold = (U8CPU)(fOuterThreshold * 0xFF);
SkColor* dptr = dst.getAddr32(0, 0);
int dstWidth = dst.width(), dstHeight = dst.height();
SkIPoint srcOffset = { bounds.fLeft - inputOffset.fX, bounds.fTop - inputOffset.fY };
for (int y = 0; y < dstHeight; ++y) {
const SkColor* sptr = inputBM.getAddr32(srcOffset.fX, srcOffset.fY+y);
for (int x = 0; x < dstWidth; ++x) {
const SkColor& source = sptr[x];
SkColor outputColor(source);
SkPoint position;
localInverse.mapXY((SkScalar)x + bounds.fLeft, (SkScalar)y + bounds.fTop, &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;
outputColor = 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);
outputColor = SkColorSetARGB(outerThreshold,
(U8CPU)(SkColorGetR(source) * scale),
(U8CPU)(SkColorGetG(source) * scale),
(U8CPU)(SkColorGetB(source) * scale));
}
}
dptr[y * dstWidth + x] = outputColor;
}
}
offset->fX = bounds.left();
offset->fY = bounds.top();
return SkSpecialImage::MakeFromRaster(SkIRect::MakeWH(bounds.width(), bounds.height()),
dst);
}
sk_sp<SkImageFilter> SkAlphaThresholdFilterImpl::onMakeColorSpace(SkColorSpaceXformer* xformer)
const {
SkASSERT(1 == this->countInputs());
sk_sp<SkImageFilter> input = xformer->apply(this->getInput(0));
if (input.get() != this->getInput(0)) {
return SkAlphaThresholdFilter::Make(fRegion, fInnerThreshold, fOuterThreshold,
std::move(input), this->getCropRectIfSet());
}
return this->refMe();
}
#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
|