1 /*
2 * Copyright 2018 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/SkCanvas.h"
10 #include "include/core/SkString.h"
11 #include "include/effects/SkShaderMaskFilter.h"
12 #include "src/core/SkMaskFilterBase.h"
13 #include "src/core/SkReadBuffer.h"
14 #include "src/shaders/SkShaderBase.h"
15
16 class SkShaderMF : public SkMaskFilterBase {
17 public:
SkShaderMF(sk_sp<SkShader> shader)18 SkShaderMF(sk_sp<SkShader> shader) : fShader(std::move(shader)) {}
19
getFormat() const20 SkMask::Format getFormat() const override { return SkMask::kA8_Format; }
21
22 bool filterMask(SkMask* dst, const SkMask& src, const SkMatrix&,
23 SkIPoint* margin) const override;
24
computeFastBounds(const SkRect & src,SkRect * dst) const25 void computeFastBounds(const SkRect& src, SkRect* dst) const override {
26 *dst = src;
27 }
28
asABlur(BlurRec *) const29 bool asABlur(BlurRec*) const override { return false; }
30
31 protected:
32 #if SK_SUPPORT_GPU
33 std::unique_ptr<GrFragmentProcessor> onAsFragmentProcessor(const GrFPArgs&) const override;
34 bool onHasFragmentProcessor() const override;
35 #endif
36
37 private:
38 SK_FLATTENABLE_HOOKS(SkShaderMF)
39
40 sk_sp<SkShader> fShader;
41
42 SkShaderMF(SkReadBuffer&);
43 void flatten(SkWriteBuffer&) const override;
44
45 friend class SkShaderMaskFilter;
46
47 using INHERITED = SkMaskFilter;
48 };
49
CreateProc(SkReadBuffer & buffer)50 sk_sp<SkFlattenable> SkShaderMF::CreateProc(SkReadBuffer& buffer) {
51 return SkShaderMaskFilter::Make(buffer.readShader());
52 }
53
flatten(SkWriteBuffer & buffer) const54 void SkShaderMF::flatten(SkWriteBuffer& buffer) const {
55 buffer.writeFlattenable(fShader.get());
56 }
57
rect_memcpy(void * dst,size_t dstRB,const void * src,size_t srcRB,size_t copyBytes,int rows)58 static void rect_memcpy(void* dst, size_t dstRB, const void* src, size_t srcRB,
59 size_t copyBytes, int rows) {
60 for (int i = 0; i < rows; ++i) {
61 memcpy(dst, src, copyBytes);
62 dst = (char*)dst + dstRB;
63 src = (const char*)src + srcRB;
64 }
65 }
66
filterMask(SkMask * dst,const SkMask & src,const SkMatrix & ctm,SkIPoint * margin) const67 bool SkShaderMF::filterMask(SkMask* dst, const SkMask& src, const SkMatrix& ctm,
68 SkIPoint* margin) const {
69 if (src.fFormat != SkMask::kA8_Format) {
70 return false;
71 }
72
73 if (margin) {
74 margin->set(0, 0);
75 }
76 dst->fBounds = src.fBounds;
77 dst->fRowBytes = src.fBounds.width(); // need alignment?
78 dst->fFormat = SkMask::kA8_Format;
79
80 if (src.fImage == nullptr) {
81 dst->fImage = nullptr;
82 return true;
83 }
84 size_t size = dst->computeImageSize();
85 if (0 == size) {
86 return false; // too big to allocate, abort
87 }
88
89 // Allocate and initialize dst image with a copy of the src image
90 dst->fImage = SkMask::AllocImage(size);
91 rect_memcpy(dst->fImage, dst->fRowBytes, src.fImage, src.fRowBytes,
92 src.fBounds.width() * sizeof(uint8_t), src.fBounds.height());
93
94 // Now we have a dst-mask, just need to setup a canvas and draw into it
95 SkBitmap bitmap;
96 if (!bitmap.installMaskPixels(*dst)) {
97 return false;
98 }
99
100 SkPaint paint;
101 paint.setShader(fShader);
102 // this blendmode is the trick: we only draw the shader where the mask is
103 paint.setBlendMode(SkBlendMode::kSrcIn);
104
105 SkCanvas canvas(bitmap);
106 canvas.translate(-SkIntToScalar(dst->fBounds.fLeft), -SkIntToScalar(dst->fBounds.fTop));
107 canvas.concat(ctm);
108 canvas.drawPaint(paint);
109 return true;
110 }
111
112 ///////////////////////////////////////////////////////////////////////////////////////////////////
113 #if SK_SUPPORT_GPU
114 #include "src/gpu/GrFragmentProcessor.h"
115
onAsFragmentProcessor(const GrFPArgs & args) const116 std::unique_ptr<GrFragmentProcessor> SkShaderMF::onAsFragmentProcessor(const GrFPArgs& args) const {
117 return GrFragmentProcessor::MulInputByChildAlpha(as_SB(fShader)->asFragmentProcessor(args));
118 }
119
onHasFragmentProcessor() const120 bool SkShaderMF::onHasFragmentProcessor() const {
121 return true;
122 }
123
124 #endif
125 ///////////////////////////////////////////////////////////////////////////////////////////////////
126
Make(sk_sp<SkShader> shader)127 sk_sp<SkMaskFilter> SkShaderMaskFilter::Make(sk_sp<SkShader> shader) {
128 return shader ? sk_sp<SkMaskFilter>(new SkShaderMF(std::move(shader))) : nullptr;
129 }
130
RegisterFlattenables()131 void SkShaderMaskFilter::RegisterFlattenables() { SK_REGISTER_FLATTENABLE(SkShaderMF); }
132