1 /*
2 * Copyright 2016 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/SkAlphaType.h"
9 #include "include/core/SkBitmap.h"
10 #include "include/core/SkBlendMode.h"
11 #include "include/core/SkCanvas.h"
12 #include "include/core/SkColor.h"
13 #include "include/core/SkFlattenable.h"
14 #include "include/core/SkImageFilter.h"
15 #include "include/core/SkImageInfo.h"
16 #include "include/core/SkM44.h"
17 #include "include/core/SkMatrix.h"
18 #include "include/core/SkPaint.h"
19 #include "include/core/SkPixmap.h"
20 #include "include/core/SkPoint.h"
21 #include "include/core/SkRect.h"
22 #include "include/core/SkRefCnt.h"
23 #include "include/core/SkRegion.h"
24 #include "include/core/SkSamplingOptions.h"
25 #include "include/core/SkScalar.h"
26 #include "include/core/SkTypes.h"
27 #include "include/effects/SkImageFilters.h"
28 #include "include/effects/SkRuntimeEffect.h"
29 #include "include/private/SkColorData.h"
30 #include "src/base/SkVx.h"
31 #include "src/core/SkImageFilter_Base.h"
32 #include "src/core/SkReadBuffer.h"
33 #include "src/core/SkRuntimeEffectPriv.h"
34 #include "src/core/SkSpecialImage.h"
35 #include "src/core/SkSpecialSurface.h"
36 #include "src/core/SkWriteBuffer.h"
37
38 #include <algorithm>
39 #include <cstdint>
40 #include <memory>
41 #include <utility>
42
43 #if defined(SK_GANESH)
44 #include "include/gpu/GpuTypes.h"
45 #include "include/gpu/GrRecordingContext.h"
46 #include "include/gpu/GrTypes.h"
47 #include "src/gpu/SkBackingFit.h"
48 #include "src/gpu/ganesh/GrColorSpaceXform.h"
49 #include "src/gpu/ganesh/GrFragmentProcessor.h"
50 #include "src/gpu/ganesh/GrImageInfo.h"
51 #include "src/gpu/ganesh/GrRecordingContextPriv.h"
52 #include "src/gpu/ganesh/GrSamplerState.h"
53 #include "src/gpu/ganesh/GrSurfaceProxy.h"
54 #include "src/gpu/ganesh/GrSurfaceProxyView.h"
55 #include "src/gpu/ganesh/SurfaceFillContext.h"
56 #include "src/gpu/ganesh/effects/GrSkSLFP.h"
57 #include "src/gpu/ganesh/effects/GrTextureEffect.h"
58 #endif
59
60 namespace {
61
62 class SkArithmeticImageFilter final : public SkImageFilter_Base {
63 public:
SkArithmeticImageFilter(float k1,float k2,float k3,float k4,bool enforcePMColor,sk_sp<SkImageFilter> inputs[2],const SkRect * cropRect)64 SkArithmeticImageFilter(float k1, float k2, float k3, float k4, bool enforcePMColor,
65 sk_sp<SkImageFilter> inputs[2], const SkRect* cropRect)
66 : INHERITED(inputs, 2, cropRect)
67 , fK{k1, k2, k3, k4}
68 , fEnforcePMColor(enforcePMColor) {}
69
70 protected:
71 sk_sp<SkSpecialImage> onFilterImage(const Context&, SkIPoint* offset) const override;
72
73 SkIRect onFilterBounds(const SkIRect&, const SkMatrix& ctm,
74 MapDirection, const SkIRect* inputRect) const override;
75
76 #if defined(SK_GANESH)
77 sk_sp<SkSpecialImage> filterImageGPU(const Context& ctx,
78 sk_sp<SkSpecialImage> background,
79 const SkIPoint& backgroundOffset,
80 sk_sp<SkSpecialImage> foreground,
81 const SkIPoint& foregroundOffset,
82 const SkIRect& bounds) const;
83 #endif
84
85 void flatten(SkWriteBuffer& buffer) const override;
86
87 void drawForeground(SkCanvas* canvas, SkSpecialImage*, const SkIRect&) const;
88
89 private:
90 friend void ::SkRegisterArithmeticImageFilterFlattenable();
SK_FLATTENABLE_HOOKS(SkArithmeticImageFilter)91 SK_FLATTENABLE_HOOKS(SkArithmeticImageFilter)
92
93 bool onAffectsTransparentBlack() const override { return !SkScalarNearlyZero(fK[3]); }
94
95 SkV4 fK;
96 bool fEnforcePMColor;
97
98 using INHERITED = SkImageFilter_Base;
99 };
100
101 } // end namespace
102
Arithmetic(SkScalar k1,SkScalar k2,SkScalar k3,SkScalar k4,bool enforcePMColor,sk_sp<SkImageFilter> background,sk_sp<SkImageFilter> foreground,const CropRect & cropRect)103 sk_sp<SkImageFilter> SkImageFilters::Arithmetic(
104 SkScalar k1, SkScalar k2, SkScalar k3, SkScalar k4, bool enforcePMColor,
105 sk_sp<SkImageFilter> background, sk_sp<SkImageFilter> foreground,
106 const CropRect& cropRect) {
107 if (!SkScalarIsFinite(k1) || !SkScalarIsFinite(k2) || !SkScalarIsFinite(k3) ||
108 !SkScalarIsFinite(k4)) {
109 return nullptr;
110 }
111
112 // are we nearly some other "std" mode?
113 int mode = -1; // illegal mode
114 if (SkScalarNearlyZero(k1) && SkScalarNearlyEqual(k2, SK_Scalar1) && SkScalarNearlyZero(k3) &&
115 SkScalarNearlyZero(k4)) {
116 mode = (int)SkBlendMode::kSrc;
117 } else if (SkScalarNearlyZero(k1) && SkScalarNearlyZero(k2) &&
118 SkScalarNearlyEqual(k3, SK_Scalar1) && SkScalarNearlyZero(k4)) {
119 mode = (int)SkBlendMode::kDst;
120 } else if (SkScalarNearlyZero(k1) && SkScalarNearlyZero(k2) && SkScalarNearlyZero(k3) &&
121 SkScalarNearlyZero(k4)) {
122 mode = (int)SkBlendMode::kClear;
123 }
124 if (mode >= 0) {
125 return SkImageFilters::Blend((SkBlendMode)mode, std::move(background),
126 std::move(foreground), cropRect);
127 }
128
129 sk_sp<SkImageFilter> inputs[2] = {std::move(background), std::move(foreground)};
130 return sk_sp<SkImageFilter>(
131 new SkArithmeticImageFilter(k1, k2, k3, k4, enforcePMColor, inputs, cropRect));
132 }
133
SkRegisterArithmeticImageFilterFlattenable()134 void SkRegisterArithmeticImageFilterFlattenable() {
135 SK_REGISTER_FLATTENABLE(SkArithmeticImageFilter);
136 SkFlattenable::Register("ArithmeticImageFilterImpl", SkArithmeticImageFilter::CreateProc);
137 }
138
CreateProc(SkReadBuffer & buffer)139 sk_sp<SkFlattenable> SkArithmeticImageFilter::CreateProc(SkReadBuffer& buffer) {
140 SK_IMAGEFILTER_UNFLATTEN_COMMON(common, 2);
141 float k[4];
142 for (int i = 0; i < 4; ++i) {
143 k[i] = buffer.readScalar();
144 }
145 const bool enforcePMColor = buffer.readBool();
146 if (!buffer.isValid()) {
147 return nullptr;
148 }
149 return SkImageFilters::Arithmetic(k[0], k[1], k[2], k[3], enforcePMColor, common.getInput(0),
150 common.getInput(1), common.cropRect());
151 }
152
flatten(SkWriteBuffer & buffer) const153 void SkArithmeticImageFilter::flatten(SkWriteBuffer& buffer) const {
154 this->INHERITED::flatten(buffer);
155 for (int i = 0; i < 4; ++i) {
156 buffer.writeScalar(fK[i]);
157 }
158 buffer.writeBool(fEnforcePMColor);
159 }
160
161 ///////////////////////////////////////////////////////////////////////////////////////////////////
162
163 template <bool EnforcePMColor>
arith_span(const SkV4 & k,SkPMColor dst[],const SkPMColor src[],int count)164 void arith_span(const SkV4& k, SkPMColor dst[], const SkPMColor src[], int count) {
165 const skvx::float4 k1 = k[0] * (1/255.0f),
166 k2 = k[1],
167 k3 = k[2],
168 k4 = k[3] * 255.0f + 0.5f;
169
170 for (int i = 0; i < count; i++) {
171 skvx::float4 s = skvx::cast<float>(skvx::byte4::Load(src+i)),
172 d = skvx::cast<float>(skvx::byte4::Load(dst+i)),
173 r = pin(k1*s*d + k2*s + k3*d + k4, skvx::float4(0.f), skvx::float4(255.f));
174 if (EnforcePMColor) {
175 auto a = skvx::shuffle<3,3,3,3>(r);
176 r = min(a, r);
177 }
178 skvx::cast<uint8_t>(r).store(dst+i);
179 }
180 }
181
182 // apply mode to src==transparent (0)
arith_transparent(const SkV4 & k,SkPMColor dst[],int count)183 template<bool EnforcePMColor> void arith_transparent(const SkV4& k, SkPMColor dst[], int count) {
184 const skvx::float4 k3 = k[2],
185 k4 = k[3] * 255.0f + 0.5f;
186
187 for (int i = 0; i < count; i++) {
188 skvx::float4 d = skvx::cast<float>(skvx::byte4::Load(dst+i)),
189 r = pin(k3*d + k4, skvx::float4(0.f), skvx::float4(255.f));
190 if (EnforcePMColor) {
191 auto a = skvx::shuffle<3,3,3,3>(r);
192 r = min(a, r);
193 }
194 skvx::cast<uint8_t>(r).store(dst+i);
195 }
196 }
197
intersect(SkPixmap * dst,SkPixmap * src,int srcDx,int srcDy)198 static bool intersect(SkPixmap* dst, SkPixmap* src, int srcDx, int srcDy) {
199 SkIRect dstR = SkIRect::MakeWH(dst->width(), dst->height());
200 SkIRect srcR = SkIRect::MakeXYWH(srcDx, srcDy, src->width(), src->height());
201 SkIRect sect;
202 if (!sect.intersect(dstR, srcR)) {
203 return false;
204 }
205 *dst = SkPixmap(dst->info().makeDimensions(sect.size()),
206 dst->addr(sect.fLeft, sect.fTop),
207 dst->rowBytes());
208 *src = SkPixmap(src->info().makeDimensions(sect.size()),
209 src->addr(std::max(0, -srcDx), std::max(0, -srcDy)),
210 src->rowBytes());
211 return true;
212 }
213
onFilterImage(const Context & ctx,SkIPoint * offset) const214 sk_sp<SkSpecialImage> SkArithmeticImageFilter::onFilterImage(const Context& ctx,
215 SkIPoint* offset) const {
216 SkIPoint backgroundOffset = SkIPoint::Make(0, 0);
217 sk_sp<SkSpecialImage> background(this->filterInput(0, ctx, &backgroundOffset));
218
219 SkIPoint foregroundOffset = SkIPoint::Make(0, 0);
220 sk_sp<SkSpecialImage> foreground(this->filterInput(1, ctx, &foregroundOffset));
221
222 SkIRect foregroundBounds = SkIRect::MakeEmpty();
223 if (foreground) {
224 foregroundBounds = SkIRect::MakeXYWH(foregroundOffset.x(), foregroundOffset.y(),
225 foreground->width(), foreground->height());
226 }
227
228 SkIRect srcBounds = SkIRect::MakeEmpty();
229 if (background) {
230 srcBounds = SkIRect::MakeXYWH(backgroundOffset.x(), backgroundOffset.y(),
231 background->width(), background->height());
232 }
233
234 srcBounds.join(foregroundBounds);
235 if (srcBounds.isEmpty()) {
236 return nullptr;
237 }
238
239 SkIRect bounds;
240 if (!this->applyCropRect(ctx, srcBounds, &bounds)) {
241 return nullptr;
242 }
243
244 offset->fX = bounds.left();
245 offset->fY = bounds.top();
246
247 #if defined(SK_GANESH)
248 if (ctx.gpuBacked()) {
249 return this->filterImageGPU(ctx, background, backgroundOffset, foreground,
250 foregroundOffset, bounds);
251 }
252 #endif
253
254 sk_sp<SkSpecialSurface> surf(ctx.makeSurface(bounds.size()));
255 if (!surf) {
256 return nullptr;
257 }
258
259 SkCanvas* canvas = surf->getCanvas();
260 SkASSERT(canvas);
261
262 canvas->clear(0x0); // can't count on background to fully clear the background
263 canvas->translate(SkIntToScalar(-bounds.left()), SkIntToScalar(-bounds.top()));
264
265 if (background) {
266 SkPaint paint;
267 paint.setBlendMode(SkBlendMode::kSrc);
268 background->draw(canvas, SkIntToScalar(backgroundOffset.fX),
269 SkIntToScalar(backgroundOffset.fY), SkSamplingOptions(), &paint);
270 }
271
272 this->drawForeground(canvas, foreground.get(), foregroundBounds);
273
274 return surf->makeImageSnapshot();
275 }
276
onFilterBounds(const SkIRect & src,const SkMatrix & ctm,MapDirection dir,const SkIRect * inputRect) const277 SkIRect SkArithmeticImageFilter::onFilterBounds(const SkIRect& src,
278 const SkMatrix& ctm,
279 MapDirection dir,
280 const SkIRect* inputRect) const {
281 if (kReverse_MapDirection == dir) {
282 return INHERITED::onFilterBounds(src, ctm, dir, inputRect);
283 }
284
285 SkASSERT(2 == this->countInputs());
286
287 // result(i1,i2) = k1*i1*i2 + k2*i1 + k3*i2 + k4
288 // Note that background (getInput(0)) is i2, and foreground (getInput(1)) is i1.
289 auto i2 = this->getInput(0) ? this->getInput(0)->filterBounds(src, ctm, dir, nullptr) : src;
290 auto i1 = this->getInput(1) ? this->getInput(1)->filterBounds(src, ctm, dir, nullptr) : src;
291
292 // Arithmetic with non-zero k4 may influence the complete filter primitive
293 // region. [k4 > 0 => result(0,0) = k4 => result(i1,i2) >= k4]
294 if (!SkScalarNearlyZero(fK[3])) {
295 i1.join(i2);
296 return i1;
297 }
298
299 // If both K2 or K3 are non-zero, both i1 and i2 appear.
300 if (!SkScalarNearlyZero(fK[1]) && !SkScalarNearlyZero(fK[2])) {
301 i1.join(i2);
302 return i1;
303 }
304
305 // If k2 is non-zero, output can be produced whenever i1 is non-transparent.
306 // [k3 = k4 = 0 => result(i1,i2) = k1*i1*i2 + k2*i1 = (k1*i2 + k2)*i1]
307 if (!SkScalarNearlyZero(fK[1])) {
308 return i1;
309 }
310
311 // If k3 is non-zero, output can be produced whenever i2 is non-transparent.
312 // [k2 = k4 = 0 => result(i1,i2) = k1*i1*i2 + k3*i2 = (k1*i1 + k3)*i2]
313 if (!SkScalarNearlyZero(fK[2])) {
314 return i2;
315 }
316
317 // If just k1 is non-zero, output will only be produce where both inputs
318 // are non-transparent. Use intersection.
319 // [k1 > 0 and k2 = k3 = k4 = 0 => result(i1,i2) = k1*i1*i2]
320 if (!SkScalarNearlyZero(fK[0])) {
321 if (!i1.intersect(i2)) {
322 return SkIRect::MakeEmpty();
323 }
324 return i1;
325 }
326
327 // [k1 = k2 = k3 = k4 = 0 => result(i1,i2) = 0]
328 return SkIRect::MakeEmpty();
329 }
330
331 #if defined(SK_GANESH)
332
make_arithmetic_fp(std::unique_ptr<GrFragmentProcessor> srcFP,std::unique_ptr<GrFragmentProcessor> dstFP,const SkV4 & k,bool enforcePMColor)333 std::unique_ptr<GrFragmentProcessor> make_arithmetic_fp(
334 std::unique_ptr<GrFragmentProcessor> srcFP,
335 std::unique_ptr<GrFragmentProcessor> dstFP,
336 const SkV4& k,
337 bool enforcePMColor) {
338 static const SkRuntimeEffect* effect = SkMakeRuntimeEffect(SkRuntimeEffect::MakeForShader,
339 "uniform shader srcFP;"
340 "uniform shader dstFP;"
341 "uniform half4 k;"
342 "uniform half pmClamp;"
343 "half4 main(float2 xy) {"
344 "half4 src = srcFP.eval(xy);"
345 "half4 dst = dstFP.eval(xy);"
346 "half4 color = saturate(k.x * src * dst +"
347 "k.y * src +"
348 "k.z * dst +"
349 "k.w);"
350 "color.rgb = min(color.rgb, max(color.a, pmClamp));"
351 "return color;"
352 "}"
353 );
354 return GrSkSLFP::Make(effect, "arithmetic_fp", /*inputFP=*/nullptr, GrSkSLFP::OptFlags::kNone,
355 "srcFP", std::move(srcFP),
356 "dstFP", std::move(dstFP),
357 "k", k,
358 "pmClamp", enforcePMColor ? 0.0f : 1.0f);
359 }
360
filterImageGPU(const Context & ctx,sk_sp<SkSpecialImage> background,const SkIPoint & backgroundOffset,sk_sp<SkSpecialImage> foreground,const SkIPoint & foregroundOffset,const SkIRect & bounds) const361 sk_sp<SkSpecialImage> SkArithmeticImageFilter::filterImageGPU(
362 const Context& ctx,
363 sk_sp<SkSpecialImage> background,
364 const SkIPoint& backgroundOffset,
365 sk_sp<SkSpecialImage> foreground,
366 const SkIPoint& foregroundOffset,
367 const SkIRect& bounds) const {
368 SkASSERT(ctx.gpuBacked());
369
370 auto rContext = ctx.getContext();
371
372 GrSurfaceProxyView backgroundView, foregroundView;
373
374 GrProtected isProtected = GrProtected::kNo;
375 if (background) {
376 backgroundView = background->view(rContext);
377 SkASSERT(backgroundView.proxy());
378 isProtected = backgroundView.proxy()->isProtected();
379 }
380
381 if (foreground) {
382 foregroundView = foreground->view(rContext);
383 SkASSERT(foregroundView.proxy());
384 isProtected = foregroundView.proxy()->isProtected();
385 }
386
387 std::unique_ptr<GrFragmentProcessor> fp;
388 const auto& caps = *ctx.getContext()->priv().caps();
389 GrSamplerState sampler(GrSamplerState::WrapMode::kClampToBorder,
390 GrSamplerState::Filter::kNearest);
391
392 if (background) {
393 SkRect bgSubset = SkRect::Make(background->subset());
394 SkMatrix backgroundMatrix = SkMatrix::Translate(
395 SkIntToScalar(bgSubset.left() - backgroundOffset.fX),
396 SkIntToScalar(bgSubset.top() - backgroundOffset.fY));
397 fp = GrTextureEffect::MakeSubset(std::move(backgroundView),
398 background->alphaType(),
399 backgroundMatrix,
400 sampler,
401 bgSubset,
402 caps);
403 fp = GrColorSpaceXformEffect::Make(std::move(fp),
404 background->getColorSpace(),
405 background->alphaType(),
406 ctx.colorSpace(),
407 kPremul_SkAlphaType);
408 } else {
409 fp = GrFragmentProcessor::MakeColor(SK_PMColor4fTRANSPARENT);
410 }
411
412 if (foreground) {
413 SkRect fgSubset = SkRect::Make(foreground->subset());
414 SkMatrix foregroundMatrix = SkMatrix::Translate(
415 SkIntToScalar(fgSubset.left() - foregroundOffset.fX),
416 SkIntToScalar(fgSubset.top() - foregroundOffset.fY));
417 auto fgFP = GrTextureEffect::MakeSubset(std::move(foregroundView),
418 foreground->alphaType(),
419 foregroundMatrix,
420 sampler,
421 fgSubset,
422 caps);
423 fgFP = GrColorSpaceXformEffect::Make(std::move(fgFP),
424 foreground->getColorSpace(),
425 foreground->alphaType(),
426 ctx.colorSpace(),
427 kPremul_SkAlphaType);
428 fp = make_arithmetic_fp(std::move(fgFP), std::move(fp), fK, fEnforcePMColor);
429 }
430
431 GrImageInfo info(ctx.grColorType(), kPremul_SkAlphaType, ctx.refColorSpace(), bounds.size());
432 auto sfc = rContext->priv().makeSFC(info,
433 "ArithmeticImageFilter_FilterImageGPU",
434 SkBackingFit::kApprox,
435 1,
436 GrMipmapped::kNo,
437 isProtected,
438 kBottomLeft_GrSurfaceOrigin);
439 if (!sfc) {
440 return nullptr;
441 }
442
443 sfc->fillRectToRectWithFP(bounds, SkIRect::MakeSize(bounds.size()), std::move(fp));
444
445 return SkSpecialImage::MakeDeferredFromGpu(rContext,
446 SkIRect::MakeWH(bounds.width(), bounds.height()),
447 kNeedNewImageUniqueID_SpecialImage,
448 sfc->readSurfaceView(),
449 sfc->colorInfo(),
450 ctx.surfaceProps());
451 }
452 #endif
453
drawForeground(SkCanvas * canvas,SkSpecialImage * img,const SkIRect & fgBounds) const454 void SkArithmeticImageFilter::drawForeground(SkCanvas* canvas, SkSpecialImage* img,
455 const SkIRect& fgBounds) const {
456 SkPixmap dst;
457 if (!canvas->peekPixels(&dst)) {
458 return;
459 }
460
461 const SkMatrix& ctm = canvas->getTotalMatrix();
462 SkASSERT(ctm.getType() <= SkMatrix::kTranslate_Mask);
463 const int dx = SkScalarRoundToInt(ctm.getTranslateX());
464 const int dy = SkScalarRoundToInt(ctm.getTranslateY());
465 // be sure to perform this offset using SkIRect, since it saturates to avoid overflows
466 const SkIRect fgoffset = fgBounds.makeOffset(dx, dy);
467
468 if (img) {
469 SkBitmap srcBM;
470 SkPixmap src;
471 if (!img->getROPixels(&srcBM)) {
472 return;
473 }
474 if (!srcBM.peekPixels(&src)) {
475 return;
476 }
477
478 auto proc = fEnforcePMColor ? arith_span<true> : arith_span<false>;
479 SkPixmap tmpDst = dst;
480 if (intersect(&tmpDst, &src, fgoffset.fLeft, fgoffset.fTop)) {
481 for (int y = 0; y < tmpDst.height(); ++y) {
482 proc(fK, tmpDst.writable_addr32(0, y), src.addr32(0, y), tmpDst.width());
483 }
484 }
485 }
486
487 // Now apply the mode with transparent-color to the outside of the fg image
488 SkRegion outside(SkIRect::MakeWH(dst.width(), dst.height()));
489 outside.op(fgoffset, SkRegion::kDifference_Op);
490 auto proc = fEnforcePMColor ? arith_transparent<true> : arith_transparent<false>;
491 for (SkRegion::Iterator iter(outside); !iter.done(); iter.next()) {
492 const SkIRect r = iter.rect();
493 for (int y = r.fTop; y < r.fBottom; ++y) {
494 proc(fK, dst.writable_addr32(r.fLeft, y), r.width());
495 }
496 }
497 }
498