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/SkCanvas.h"
9 #include "include/core/SkColorSpace.h"
10 #include "include/core/SkFlattenable.h"
11 #include "include/core/SkImageFilter.h"
12 #include "include/core/SkMatrix.h"
13 #include "include/core/SkPaint.h"
14 #include "include/core/SkPoint.h"
15 #include "include/core/SkRect.h"
16 #include "include/core/SkRefCnt.h"
17 #include "include/core/SkScalar.h"
18 #include "include/core/SkShader.h"
19 #include "include/core/SkTypes.h"
20 #include "include/effects/SkImageFilters.h"
21 #include "src/core/SkImageFilter_Base.h"
22 #include "src/core/SkPicturePriv.h"
23 #include "src/core/SkReadBuffer.h"
24 #include "src/core/SkSpecialImage.h"
25 #include "src/core/SkSpecialSurface.h"
26 #include "src/core/SkWriteBuffer.h"
27
28 #include <utility>
29
30 namespace {
31
32 class SkShaderImageFilter final : public SkImageFilter_Base {
33 public:
SkShaderImageFilter(sk_sp<SkShader> shader,SkImageFilters::Dither dither,const SkRect * rect)34 SkShaderImageFilter(sk_sp<SkShader> shader, SkImageFilters::Dither dither, const SkRect* rect)
35 : INHERITED(nullptr, 0, rect)
36 , fShader(std::move(shader))
37 , fDither(dither) {}
38
39 protected:
40 void flatten(SkWriteBuffer&) const override;
41 sk_sp<SkSpecialImage> onFilterImage(const Context&, SkIPoint* offset) const override;
42
43 private:
44 friend void ::SkRegisterShaderImageFilterFlattenable();
SK_FLATTENABLE_HOOKS(SkShaderImageFilter)45 SK_FLATTENABLE_HOOKS(SkShaderImageFilter)
46
47 bool onAffectsTransparentBlack() const override { return true; }
48
49 sk_sp<SkShader> fShader;
50 SkImageFilters::Dither fDither;
51
52 using INHERITED = SkImageFilter_Base;
53 };
54
55 } // end namespace
56
Shader(sk_sp<SkShader> shader,Dither dither,const CropRect & cropRect)57 sk_sp<SkImageFilter> SkImageFilters::Shader(sk_sp<SkShader> shader,
58 Dither dither,
59 const CropRect& cropRect) {
60 return sk_sp<SkImageFilter>(new SkShaderImageFilter(std::move(shader), dither, cropRect));
61 }
62
SkRegisterShaderImageFilterFlattenable()63 void SkRegisterShaderImageFilterFlattenable() {
64 SK_REGISTER_FLATTENABLE(SkShaderImageFilter);
65 // TODO (michaelludwig) - Remove after grace period for SKPs to stop using old name
66 SkFlattenable::Register("SkPaintImageFilter", SkShaderImageFilter::CreateProc);
67 SkFlattenable::Register("SkPaintImageFilterImpl", SkShaderImageFilter::CreateProc);
68 }
69
CreateProc(SkReadBuffer & buffer)70 sk_sp<SkFlattenable> SkShaderImageFilter::CreateProc(SkReadBuffer& buffer) {
71 SK_IMAGEFILTER_UNFLATTEN_COMMON(common, 0);
72 sk_sp<SkShader> shader;
73 bool dither;
74 if (buffer.isVersionLT(SkPicturePriv::kShaderImageFilterSerializeShader)) {
75 // The old implementation stored an entire SkPaint, but we only need the SkShader and dither
76 // boolean. We could fail if the paint stores more effects than that, but this is simpler.
77 SkPaint paint = buffer.readPaint();
78 shader = paint.getShader() ? paint.refShader()
79 : SkShaders::Color(paint.getColor4f(), nullptr);
80 dither = paint.isDither();
81 } else {
82 shader = buffer.readShader();
83 dither = buffer.readBool();
84 }
85 return SkImageFilters::Shader(std::move(shader),
86 SkImageFilters::Dither(dither),
87 common.cropRect());
88 }
89
flatten(SkWriteBuffer & buffer) const90 void SkShaderImageFilter::flatten(SkWriteBuffer& buffer) const {
91 this->INHERITED::flatten(buffer);
92 buffer.writeFlattenable(fShader.get());
93 buffer.writeBool(fDither == SkImageFilters::Dither::kYes);
94 }
95
96 ///////////////////////////////////////////////////////////////////////////////////////////////////
97
onFilterImage(const Context & ctx,SkIPoint * offset) const98 sk_sp<SkSpecialImage> SkShaderImageFilter::onFilterImage(const Context& ctx,
99 SkIPoint* offset) const {
100 SkIRect bounds;
101 const SkIRect srcBounds = SkIRect::MakeWH(ctx.sourceImage()->width(),
102 ctx.sourceImage()->height());
103 if (!this->applyCropRect(ctx, srcBounds, &bounds)) {
104 return nullptr;
105 }
106
107 sk_sp<SkSpecialSurface> surf(ctx.makeSurface(bounds.size()));
108 if (!surf) {
109 return nullptr;
110 }
111
112 SkCanvas* canvas = surf->getCanvas();
113 SkASSERT(canvas);
114
115 canvas->clear(0x0);
116
117 SkMatrix matrix(ctx.ctm());
118 matrix.postTranslate(SkIntToScalar(-bounds.left()), SkIntToScalar(-bounds.top()));
119 SkRect rect = SkRect::MakeIWH(bounds.width(), bounds.height());
120 SkMatrix inverse;
121 if (matrix.invert(&inverse)) {
122 inverse.mapRect(&rect);
123 }
124 canvas->setMatrix(matrix);
125 if (rect.isFinite()) {
126 SkPaint paint;
127 paint.setShader(fShader);
128 paint.setDither(fDither == SkImageFilters::Dither::kYes);
129 canvas->drawRect(rect, paint);
130 }
131
132 offset->fX = bounds.fLeft;
133 offset->fY = bounds.fTop;
134 return surf->makeImageSnapshot();
135 }
136