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/core/SkBitmap.h"
9 #include "include/core/SkRegion.h"
10 #include "include/effects/SkImageFilters.h"
11 #include "include/private/SkTPin.h"
12 #include "src/core/SkImageFilter_Base.h"
13 #include "src/core/SkReadBuffer.h"
14 #include "src/core/SkSpecialImage.h"
15 #include "src/core/SkWriteBuffer.h"
16
17 #if SK_SUPPORT_GPU
18 #include "include/gpu/GrRecordingContext.h"
19 #include "src/gpu/GrCaps.h"
20 #include "src/gpu/GrColorSpaceXform.h"
21 #include "src/gpu/GrRecordingContextPriv.h"
22 #include "src/gpu/GrSurfaceDrawContext.h"
23 #include "src/gpu/GrTextureProxy.h"
24 #include "src/gpu/effects/GrTextureEffect.h"
25 #include "src/gpu/effects/generated/GrAlphaThresholdFragmentProcessor.h"
26 #endif
27
28 namespace {
29
30 class SkAlphaThresholdImageFilter final : public SkImageFilter_Base {
31 public:
SkAlphaThresholdImageFilter(const SkRegion & region,SkScalar innerThreshold,SkScalar outerThreshold,sk_sp<SkImageFilter> input,const SkRect * cropRect=nullptr)32 SkAlphaThresholdImageFilter(const SkRegion& region, SkScalar innerThreshold,
33 SkScalar outerThreshold, sk_sp<SkImageFilter> input,
34 const SkRect* cropRect = nullptr)
35 : INHERITED(&input, 1, cropRect)
36 , fRegion(region)
37 , fInnerThreshold(innerThreshold)
38 , fOuterThreshold(outerThreshold) {}
39
40 protected:
41 void flatten(SkWriteBuffer&) const override;
42
43 sk_sp<SkSpecialImage> onFilterImage(const Context&, SkIPoint* offset) const override;
44
45 #if SK_SUPPORT_GPU
46 GrSurfaceProxyView createMaskTexture(GrRecordingContext*,
47 const SkMatrix&,
48 const SkIRect& bounds,
49 const SkSurfaceProps&) const;
50 #endif
51
52 private:
53 friend void ::SkRegisterAlphaThresholdImageFilterFlattenable();
54 SK_FLATTENABLE_HOOKS(SkAlphaThresholdImageFilter)
55
56 SkRegion fRegion;
57 SkScalar fInnerThreshold;
58 SkScalar fOuterThreshold;
59
60 using INHERITED = SkImageFilter_Base;
61 };
62
63 }; // end namespace
64
AlphaThreshold(const SkRegion & region,SkScalar innerMin,SkScalar outerMax,sk_sp<SkImageFilter> input,const CropRect & cropRect)65 sk_sp<SkImageFilter> SkImageFilters::AlphaThreshold(
66 const SkRegion& region, SkScalar innerMin, SkScalar outerMax, sk_sp<SkImageFilter> input,
67 const CropRect& cropRect) {
68 innerMin = SkTPin(innerMin, 0.f, 1.f);
69 outerMax = SkTPin(outerMax, 0.f, 1.f);
70 if (!SkScalarIsFinite(innerMin) || !SkScalarIsFinite(outerMax)) {
71 return nullptr;
72 }
73 return sk_sp<SkImageFilter>(new SkAlphaThresholdImageFilter(
74 region, innerMin, outerMax, std::move(input), cropRect));
75 }
76
SkRegisterAlphaThresholdImageFilterFlattenable()77 void SkRegisterAlphaThresholdImageFilterFlattenable() {
78 SK_REGISTER_FLATTENABLE(SkAlphaThresholdImageFilter);
79 SkFlattenable::Register("SkAlphaThresholdFilterImpl", SkAlphaThresholdImageFilter::CreateProc);
80 }
81
CreateProc(SkReadBuffer & buffer)82 sk_sp<SkFlattenable> SkAlphaThresholdImageFilter::CreateProc(SkReadBuffer& buffer) {
83 SK_IMAGEFILTER_UNFLATTEN_COMMON(common, 1);
84 SkScalar inner = buffer.readScalar();
85 SkScalar outer = buffer.readScalar();
86 SkRegion rgn;
87 buffer.readRegion(&rgn);
88 return SkImageFilters::AlphaThreshold(rgn, inner, outer, common.getInput(0), common.cropRect());
89 }
90
flatten(SkWriteBuffer & buffer) const91 void SkAlphaThresholdImageFilter::flatten(SkWriteBuffer& buffer) const {
92 this->INHERITED::flatten(buffer);
93 buffer.writeScalar(fInnerThreshold);
94 buffer.writeScalar(fOuterThreshold);
95 buffer.writeRegion(fRegion);
96 }
97
98 ///////////////////////////////////////////////////////////////////////////////////////////////////
99
100 #if SK_SUPPORT_GPU
createMaskTexture(GrRecordingContext * context,const SkMatrix & inMatrix,const SkIRect & bounds,const SkSurfaceProps & surfaceProps) const101 GrSurfaceProxyView SkAlphaThresholdImageFilter::createMaskTexture(
102 GrRecordingContext* context, const SkMatrix& inMatrix, const SkIRect& bounds,
103 const SkSurfaceProps& surfaceProps) const {
104 auto rtContext = GrSurfaceDrawContext::MakeWithFallback(
105 context, GrColorType::kAlpha_8, nullptr, SkBackingFit::kApprox, bounds.size(),
106 surfaceProps);
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> SkAlphaThresholdImageFilter::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 ctx.surfaceProps());
163 if (!maskView) {
164 return nullptr;
165 }
166 auto maskFP = GrTextureEffect::Make(std::move(maskView), kPremul_SkAlphaType,
167 SkMatrix::Translate(-bounds.x(), -bounds.y()));
168
169 auto textureFP = GrTextureEffect::Make(
170 std::move(inputView), input->alphaType(),
171 SkMatrix::Translate(input->subset().x(), input->subset().y()));
172 textureFP = GrColorSpaceXformEffect::Make(std::move(textureFP),
173 input->getColorSpace(), input->alphaType(),
174 ctx.colorSpace(), kPremul_SkAlphaType);
175 if (!textureFP) {
176 return nullptr;
177 }
178
179 auto thresholdFP = GrAlphaThresholdFragmentProcessor::Make(
180 std::move(textureFP), std::move(maskFP), fInnerThreshold, fOuterThreshold);
181 if (!thresholdFP) {
182 return nullptr;
183 }
184
185 return DrawWithFP(context, std::move(thresholdFP), bounds, ctx.colorType(),
186 ctx.colorSpace(), ctx.surfaceProps(), isProtected);
187 }
188 #endif
189
190 SkBitmap inputBM;
191
192 if (!input->getROPixels(&inputBM)) {
193 return nullptr;
194 }
195
196 if (inputBM.colorType() != kN32_SkColorType) {
197 return nullptr;
198 }
199
200 if (!inputBM.getPixels() || inputBM.width() <= 0 || inputBM.height() <= 0) {
201 return nullptr;
202 }
203
204
205 SkMatrix localInverse;
206 if (!ctx.ctm().invert(&localInverse)) {
207 return nullptr;
208 }
209
210 SkImageInfo info = SkImageInfo::MakeN32(bounds.width(), bounds.height(),
211 kPremul_SkAlphaType);
212
213 SkBitmap dst;
214 if (!dst.tryAllocPixels(info)) {
215 return nullptr;
216 }
217
218 U8CPU innerThreshold = (U8CPU)(fInnerThreshold * 0xFF);
219 U8CPU outerThreshold = (U8CPU)(fOuterThreshold * 0xFF);
220 SkColor* dptr = dst.getAddr32(0, 0);
221 int dstWidth = dst.width(), dstHeight = dst.height();
222 SkIPoint srcOffset = { bounds.fLeft - inputOffset.fX, bounds.fTop - inputOffset.fY };
223 for (int y = 0; y < dstHeight; ++y) {
224 const SkColor* sptr = inputBM.getAddr32(srcOffset.fX, srcOffset.fY+y);
225
226 for (int x = 0; x < dstWidth; ++x) {
227 const SkColor& source = sptr[x];
228 SkColor outputColor(source);
229 SkPoint position;
230 localInverse.mapXY((SkScalar)x + bounds.fLeft, (SkScalar)y + bounds.fTop, &position);
231 if (fRegion.contains((int32_t)position.x(), (int32_t)position.y())) {
232 if (SkColorGetA(source) < innerThreshold) {
233 U8CPU alpha = SkColorGetA(source);
234 if (alpha == 0) {
235 alpha = 1;
236 }
237 float scale = (float)innerThreshold / alpha;
238 outputColor = SkColorSetARGB(innerThreshold,
239 (U8CPU)(SkColorGetR(source) * scale),
240 (U8CPU)(SkColorGetG(source) * scale),
241 (U8CPU)(SkColorGetB(source) * scale));
242 }
243 } else {
244 if (SkColorGetA(source) > outerThreshold) {
245 float scale = (float)outerThreshold / SkColorGetA(source);
246 outputColor = SkColorSetARGB(outerThreshold,
247 (U8CPU)(SkColorGetR(source) * scale),
248 (U8CPU)(SkColorGetG(source) * scale),
249 (U8CPU)(SkColorGetB(source) * scale));
250 }
251 }
252 dptr[y * dstWidth + x] = outputColor;
253 }
254 }
255
256 offset->fX = bounds.left();
257 offset->fY = bounds.top();
258 return SkSpecialImage::MakeFromRaster(SkIRect::MakeWH(bounds.width(), bounds.height()),
259 dst, ctx.surfaceProps());
260 }
261