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 "SkDropShadowImageFilter.h"
9
10 #include "SkBlurImageFilter.h"
11 #include "SkCanvas.h"
12 #include "SkColorSpaceXformer.h"
13 #include "SkReadBuffer.h"
14 #include "SkSpecialImage.h"
15 #include "SkSpecialSurface.h"
16 #include "SkWriteBuffer.h"
17
Make(SkScalar dx,SkScalar dy,SkScalar sigmaX,SkScalar sigmaY,SkColor color,ShadowMode shadowMode,sk_sp<SkImageFilter> input,const CropRect * cropRect)18 sk_sp<SkImageFilter> SkDropShadowImageFilter::Make(SkScalar dx, SkScalar dy,
19 SkScalar sigmaX, SkScalar sigmaY,
20 SkColor color, ShadowMode shadowMode,
21 sk_sp<SkImageFilter> input,
22 const CropRect* cropRect) {
23 return sk_sp<SkImageFilter>(new SkDropShadowImageFilter(dx, dy, sigmaX, sigmaY,
24 color, shadowMode,
25 std::move(input),
26 cropRect));
27 }
28
SkDropShadowImageFilter(SkScalar dx,SkScalar dy,SkScalar sigmaX,SkScalar sigmaY,SkColor color,ShadowMode shadowMode,sk_sp<SkImageFilter> input,const CropRect * cropRect)29 SkDropShadowImageFilter::SkDropShadowImageFilter(SkScalar dx, SkScalar dy,
30 SkScalar sigmaX, SkScalar sigmaY, SkColor color,
31 ShadowMode shadowMode, sk_sp<SkImageFilter> input,
32 const CropRect* cropRect)
33 : INHERITED(&input, 1, cropRect)
34 , fDx(dx)
35 , fDy(dy)
36 , fSigmaX(sigmaX)
37 , fSigmaY(sigmaY)
38 , fColor(color)
39 , fShadowMode(shadowMode) {
40 }
41
CreateProc(SkReadBuffer & buffer)42 sk_sp<SkFlattenable> SkDropShadowImageFilter::CreateProc(SkReadBuffer& buffer) {
43 SK_IMAGEFILTER_UNFLATTEN_COMMON(common, 1);
44 SkScalar dx = buffer.readScalar();
45 SkScalar dy = buffer.readScalar();
46 SkScalar sigmaX = buffer.readScalar();
47 SkScalar sigmaY = buffer.readScalar();
48 SkColor color = buffer.readColor();
49 ShadowMode shadowMode = static_cast<ShadowMode>(buffer.readInt());
50 return Make(dx, dy, sigmaX, sigmaY, color, shadowMode, common.getInput(0), &common.cropRect());
51 }
52
flatten(SkWriteBuffer & buffer) const53 void SkDropShadowImageFilter::flatten(SkWriteBuffer& buffer) const {
54 this->INHERITED::flatten(buffer);
55 buffer.writeScalar(fDx);
56 buffer.writeScalar(fDy);
57 buffer.writeScalar(fSigmaX);
58 buffer.writeScalar(fSigmaY);
59 buffer.writeColor(fColor);
60 buffer.writeInt(static_cast<int>(fShadowMode));
61 }
62
onFilterImage(SkSpecialImage * source,const Context & ctx,SkIPoint * offset) const63 sk_sp<SkSpecialImage> SkDropShadowImageFilter::onFilterImage(SkSpecialImage* source,
64 const Context& ctx,
65 SkIPoint* offset) const {
66 SkIPoint inputOffset = SkIPoint::Make(0, 0);
67 sk_sp<SkSpecialImage> input(this->filterInput(0, source, ctx, &inputOffset));
68 if (!input) {
69 return nullptr;
70 }
71
72 const SkIRect inputBounds = SkIRect::MakeXYWH(inputOffset.x(), inputOffset.y(),
73 input->width(), input->height());
74 SkIRect bounds;
75 if (!this->applyCropRect(ctx, inputBounds, &bounds)) {
76 return nullptr;
77 }
78
79 sk_sp<SkSpecialSurface> surf(source->makeSurface(ctx.outputProperties(), bounds.size()));
80 if (!surf) {
81 return nullptr;
82 }
83
84 SkCanvas* canvas = surf->getCanvas();
85 SkASSERT(canvas);
86
87 canvas->clear(0x0);
88
89 SkVector sigma = SkVector::Make(fSigmaX, fSigmaY);
90 ctx.ctm().mapVectors(&sigma, 1);
91 sigma.fX = SkMaxScalar(0, sigma.fX);
92 sigma.fY = SkMaxScalar(0, sigma.fY);
93
94 SkPaint paint;
95 paint.setAntiAlias(true);
96 paint.setImageFilter(SkBlurImageFilter::Make(sigma.fX, sigma.fY, nullptr));
97 paint.setColorFilter(SkColorFilter::MakeModeFilter(fColor, SkBlendMode::kSrcIn));
98
99 SkVector offsetVec = SkVector::Make(fDx, fDy);
100 ctx.ctm().mapVectors(&offsetVec, 1);
101
102 canvas->translate(SkIntToScalar(inputOffset.fX - bounds.fLeft),
103 SkIntToScalar(inputOffset.fY - bounds.fTop));
104 input->draw(canvas, offsetVec.fX, offsetVec.fY, &paint);
105
106 if (fShadowMode == kDrawShadowAndForeground_ShadowMode) {
107 input->draw(canvas, 0, 0, nullptr);
108 }
109 offset->fX = bounds.fLeft;
110 offset->fY = bounds.fTop;
111 return surf->makeImageSnapshot();
112 }
113
onMakeColorSpace(SkColorSpaceXformer * xformer) const114 sk_sp<SkImageFilter> SkDropShadowImageFilter::onMakeColorSpace(SkColorSpaceXformer* xformer) const {
115 SkASSERT(1 == this->countInputs());
116
117 sk_sp<SkImageFilter> input = xformer->apply(this->getInput(0));
118 SkColor color = xformer->apply(fColor);
119 if (input.get() != this->getInput(0) || color != fColor) {
120 return SkDropShadowImageFilter::Make(fDx, fDy, fSigmaX, fSigmaY, color,
121 fShadowMode, input, this->getCropRectIfSet());
122 }
123 return this->refMe();
124 }
125
computeFastBounds(const SkRect & src) const126 SkRect SkDropShadowImageFilter::computeFastBounds(const SkRect& src) const {
127 SkRect bounds = this->getInput(0) ? this->getInput(0)->computeFastBounds(src) : src;
128 SkRect shadowBounds = bounds;
129 shadowBounds.offset(fDx, fDy);
130 shadowBounds.outset(fSigmaX * 3, fSigmaY * 3);
131 if (fShadowMode == kDrawShadowAndForeground_ShadowMode) {
132 bounds.join(shadowBounds);
133 } else {
134 bounds = shadowBounds;
135 }
136 return bounds;
137 }
138
onFilterNodeBounds(const SkIRect & src,const SkMatrix & ctm,MapDirection direction) const139 SkIRect SkDropShadowImageFilter::onFilterNodeBounds(const SkIRect& src, const SkMatrix& ctm,
140 MapDirection direction) const {
141 SkVector offsetVec = SkVector::Make(fDx, fDy);
142 if (kReverse_MapDirection == direction) {
143 offsetVec.negate();
144 }
145 ctm.mapVectors(&offsetVec, 1);
146 SkIRect dst = src.makeOffset(SkScalarCeilToInt(offsetVec.x()),
147 SkScalarCeilToInt(offsetVec.y()));
148 SkVector sigma = SkVector::Make(fSigmaX, fSigmaY);
149 ctm.mapVectors(&sigma, 1);
150 dst.outset(
151 SkScalarCeilToInt(SkScalarAbs(sigma.x() * 3)),
152 SkScalarCeilToInt(SkScalarAbs(sigma.y() * 3)));
153 if (fShadowMode == kDrawShadowAndForeground_ShadowMode) {
154 dst.join(src);
155 }
156 return dst;
157 }
158
159 #ifndef SK_IGNORE_TO_STRING
toString(SkString * str) const160 void SkDropShadowImageFilter::toString(SkString* str) const {
161 str->appendf("SkDropShadowImageFilter: (");
162
163 str->appendf("dX: %f ", fDx);
164 str->appendf("dY: %f ", fDy);
165 str->appendf("sigmaX: %f ", fSigmaX);
166 str->appendf("sigmaY: %f ", fSigmaY);
167
168 str->append("Color: ");
169 str->appendHex(fColor);
170
171 static const char* gModeStrings[] = {
172 "kDrawShadowAndForeground", "kDrawShadowOnly"
173 };
174
175 static_assert(kShadowModeCount == SK_ARRAY_COUNT(gModeStrings), "enum_mismatch");
176
177 str->appendf(" mode: %s", gModeStrings[fShadowMode]);
178
179 str->append(")");
180 }
181 #endif
182