1 /*
2 * Copyright 2013 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
8 #include "include/effects/SkAlphaThresholdFilter.h"
9
10 #include "include/core/SkBitmap.h"
11 #include "include/core/SkRegion.h"
12 #include "include/private/SkTPin.h"
13 #include "src/core/SkImageFilter_Base.h"
14 #include "src/core/SkReadBuffer.h"
15 #include "src/core/SkSpecialImage.h"
16 #include "src/core/SkWriteBuffer.h"
17
18 #if SK_SUPPORT_GPU
19 #include "include/gpu/GrRecordingContext.h"
20 #include "src/gpu/GrCaps.h"
21 #include "src/gpu/GrColorSpaceXform.h"
22 #include "src/gpu/GrRecordingContextPriv.h"
23 #include "src/gpu/GrRenderTargetContext.h"
24 #include "src/gpu/GrTextureProxy.h"
25 #include "src/gpu/effects/GrTextureEffect.h"
26 #include "src/gpu/effects/generated/GrAlphaThresholdFragmentProcessor.h"
27 #endif
28
29 namespace {
30
31 class SkAlphaThresholdFilterImpl final : public SkImageFilter_Base {
32 public:
SkAlphaThresholdFilterImpl(const SkRegion & region,SkScalar innerThreshold,SkScalar outerThreshold,sk_sp<SkImageFilter> input,const CropRect * cropRect=nullptr)33 SkAlphaThresholdFilterImpl(const SkRegion& region, SkScalar innerThreshold,
34 SkScalar outerThreshold, sk_sp<SkImageFilter> input,
35 const CropRect* cropRect = nullptr)
36 : INHERITED(&input, 1, cropRect)
37 , fRegion(region)
38 , fInnerThreshold(innerThreshold)
39 , fOuterThreshold(outerThreshold) {}
40
41 protected:
42 void flatten(SkWriteBuffer&) const override;
43
44 sk_sp<SkSpecialImage> onFilterImage(const Context&, SkIPoint* offset) const override;
45
46 #if SK_SUPPORT_GPU
47 GrSurfaceProxyView createMaskTexture(GrRecordingContext*,
48 const SkMatrix&,
49 const SkIRect& bounds) const;
50 #endif
51
52 private:
53 friend void SkAlphaThresholdFilter::RegisterFlattenables();
54 SK_FLATTENABLE_HOOKS(SkAlphaThresholdFilterImpl)
55
56 SkRegion fRegion;
57 SkScalar fInnerThreshold;
58 SkScalar fOuterThreshold;
59
60 using INHERITED = SkImageFilter_Base;
61 };
62
63 }; // end namespace
64
Make(const SkRegion & region,SkScalar innerThreshold,SkScalar outerThreshold,sk_sp<SkImageFilter> input,const SkImageFilter::CropRect * cropRect)65 sk_sp<SkImageFilter> SkAlphaThresholdFilter::Make(const SkRegion& region, SkScalar innerThreshold,
66 SkScalar outerThreshold,
67 sk_sp<SkImageFilter> input,
68 const SkImageFilter::CropRect* cropRect) {
69 innerThreshold = SkTPin(innerThreshold, 0.f, 1.f);
70 outerThreshold = SkTPin(outerThreshold, 0.f, 1.f);
71 if (!SkScalarIsFinite(innerThreshold) || !SkScalarIsFinite(outerThreshold)) {
72 return nullptr;
73 }
74 return sk_sp<SkImageFilter>(new SkAlphaThresholdFilterImpl(
75 region, innerThreshold, outerThreshold, std::move(input), cropRect));
76 }
77
RegisterFlattenables()78 void SkAlphaThresholdFilter::RegisterFlattenables() {
79 SK_REGISTER_FLATTENABLE(SkAlphaThresholdFilterImpl);
80 }
81
82 ///////////////////////////////////////////////////////////////////////////////////////////////////
83
CreateProc(SkReadBuffer & buffer)84 sk_sp<SkFlattenable> SkAlphaThresholdFilterImpl::CreateProc(SkReadBuffer& buffer) {
85 SK_IMAGEFILTER_UNFLATTEN_COMMON(common, 1);
86 SkScalar inner = buffer.readScalar();
87 SkScalar outer = buffer.readScalar();
88 SkRegion rgn;
89 buffer.readRegion(&rgn);
90 return SkAlphaThresholdFilter::Make(rgn, inner, outer, common.getInput(0),
91 &common.cropRect());
92 }
93
flatten(SkWriteBuffer & buffer) const94 void SkAlphaThresholdFilterImpl::flatten(SkWriteBuffer& buffer) const {
95 this->INHERITED::flatten(buffer);
96 buffer.writeScalar(fInnerThreshold);
97 buffer.writeScalar(fOuterThreshold);
98 buffer.writeRegion(fRegion);
99 }
100
101 #if SK_SUPPORT_GPU
createMaskTexture(GrRecordingContext * context,const SkMatrix & inMatrix,const SkIRect & bounds) const102 GrSurfaceProxyView SkAlphaThresholdFilterImpl::createMaskTexture(GrRecordingContext* context,
103 const SkMatrix& inMatrix,
104 const SkIRect& bounds) const {
105 auto rtContext = GrRenderTargetContext::MakeWithFallback(
106 context, GrColorType::kAlpha_8, nullptr, SkBackingFit::kApprox, bounds.size());
107 if (!rtContext) {
108 return {};
109 }
110
111 SkRegion::Iterator iter(fRegion);
112 rtContext->clear(SK_PMColor4fTRANSPARENT);
113
114 while (!iter.done()) {
115 GrPaint paint;
116 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
117
118 SkRect rect = SkRect::Make(iter.rect());
119
120 rtContext->drawRect(nullptr, std::move(paint), GrAA::kNo, inMatrix, rect);
121
122 iter.next();
123 }
124
125 return rtContext->readSurfaceView();
126 }
127 #endif
128
onFilterImage(const Context & ctx,SkIPoint * offset) const129 sk_sp<SkSpecialImage> SkAlphaThresholdFilterImpl::onFilterImage(const Context& ctx,
130 SkIPoint* offset) const {
131 SkIPoint inputOffset = SkIPoint::Make(0, 0);
132 sk_sp<SkSpecialImage> input(this->filterInput(0, ctx, &inputOffset));
133 if (!input) {
134 return nullptr;
135 }
136
137 const SkIRect inputBounds = SkIRect::MakeXYWH(inputOffset.x(), inputOffset.y(),
138 input->width(), input->height());
139
140 SkIRect bounds;
141 if (!this->applyCropRect(ctx, inputBounds, &bounds)) {
142 return nullptr;
143 }
144
145 #if SK_SUPPORT_GPU
146 if (ctx.gpuBacked()) {
147 auto context = ctx.getContext();
148
149 GrSurfaceProxyView inputView = (input->view(context));
150 SkASSERT(inputView.asTextureProxy());
151 const GrProtected isProtected = inputView.proxy()->isProtected();
152
153 offset->fX = bounds.left();
154 offset->fY = bounds.top();
155
156 bounds.offset(-inputOffset);
157
158 SkMatrix matrix(ctx.ctm());
159 matrix.postTranslate(SkIntToScalar(-offset->fX), SkIntToScalar(-offset->fY));
160
161 GrSurfaceProxyView maskView = this->createMaskTexture(context, matrix, bounds);
162 if (!maskView) {
163 return nullptr;
164 }
165 auto maskFP = GrTextureEffect::Make(std::move(maskView), kPremul_SkAlphaType,
166 SkMatrix::Translate(-bounds.x(), -bounds.y()));
167
168 auto textureFP = GrTextureEffect::Make(
169 std::move(inputView), input->alphaType(),
170 SkMatrix::Translate(input->subset().x(), input->subset().y()));
171 textureFP = GrColorSpaceXformEffect::Make(std::move(textureFP),
172 input->getColorSpace(), input->alphaType(),
173 ctx.colorSpace(), kPremul_SkAlphaType);
174 if (!textureFP) {
175 return nullptr;
176 }
177
178 auto thresholdFP = GrAlphaThresholdFragmentProcessor::Make(
179 std::move(textureFP), std::move(maskFP), fInnerThreshold, fOuterThreshold);
180 if (!thresholdFP) {
181 return nullptr;
182 }
183
184 return DrawWithFP(context, std::move(thresholdFP), bounds, ctx.colorType(),
185 ctx.colorSpace(), isProtected);
186 }
187 #endif
188
189 SkBitmap inputBM;
190
191 if (!input->getROPixels(&inputBM)) {
192 return nullptr;
193 }
194
195 if (inputBM.colorType() != kN32_SkColorType) {
196 return nullptr;
197 }
198
199 if (!inputBM.getPixels() || inputBM.width() <= 0 || inputBM.height() <= 0) {
200 return nullptr;
201 }
202
203
204 SkMatrix localInverse;
205 if (!ctx.ctm().invert(&localInverse)) {
206 return nullptr;
207 }
208
209 SkImageInfo info = SkImageInfo::MakeN32(bounds.width(), bounds.height(),
210 kPremul_SkAlphaType);
211
212 SkBitmap dst;
213 if (!dst.tryAllocPixels(info)) {
214 return nullptr;
215 }
216
217 U8CPU innerThreshold = (U8CPU)(fInnerThreshold * 0xFF);
218 U8CPU outerThreshold = (U8CPU)(fOuterThreshold * 0xFF);
219 SkColor* dptr = dst.getAddr32(0, 0);
220 int dstWidth = dst.width(), dstHeight = dst.height();
221 SkIPoint srcOffset = { bounds.fLeft - inputOffset.fX, bounds.fTop - inputOffset.fY };
222 for (int y = 0; y < dstHeight; ++y) {
223 const SkColor* sptr = inputBM.getAddr32(srcOffset.fX, srcOffset.fY+y);
224
225 for (int x = 0; x < dstWidth; ++x) {
226 const SkColor& source = sptr[x];
227 SkColor outputColor(source);
228 SkPoint position;
229 localInverse.mapXY((SkScalar)x + bounds.fLeft, (SkScalar)y + bounds.fTop, &position);
230 if (fRegion.contains((int32_t)position.x(), (int32_t)position.y())) {
231 if (SkColorGetA(source) < innerThreshold) {
232 U8CPU alpha = SkColorGetA(source);
233 if (alpha == 0) {
234 alpha = 1;
235 }
236 float scale = (float)innerThreshold / alpha;
237 outputColor = SkColorSetARGB(innerThreshold,
238 (U8CPU)(SkColorGetR(source) * scale),
239 (U8CPU)(SkColorGetG(source) * scale),
240 (U8CPU)(SkColorGetB(source) * scale));
241 }
242 } else {
243 if (SkColorGetA(source) > outerThreshold) {
244 float scale = (float)outerThreshold / SkColorGetA(source);
245 outputColor = SkColorSetARGB(outerThreshold,
246 (U8CPU)(SkColorGetR(source) * scale),
247 (U8CPU)(SkColorGetG(source) * scale),
248 (U8CPU)(SkColorGetB(source) * scale));
249 }
250 }
251 dptr[y * dstWidth + x] = outputColor;
252 }
253 }
254
255 offset->fX = bounds.left();
256 offset->fY = bounds.top();
257 return SkSpecialImage::MakeFromRaster(SkIRect::MakeWH(bounds.width(), bounds.height()),
258 dst);
259 }
260