junov@chromium.org | f44fcdc | 2013-08-07 20:00:55 +0000 | [diff] [blame] | 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 | |
junov@chromium.org | f44fcdc | 2013-08-07 20:00:55 +0000 | [diff] [blame] | 10 | #include "SkBlurImageFilter.h" |
| 11 | #include "SkCanvas.h" |
Matt Sarett | 6d72ed9 | 2017-04-10 16:35:33 -0400 | [diff] [blame^] | 12 | #include "SkColorSpaceXformer.h" |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 13 | #include "SkReadBuffer.h" |
robertphillips | 0a291c7 | 2016-03-23 05:00:01 -0700 | [diff] [blame] | 14 | #include "SkSpecialImage.h" |
| 15 | #include "SkSpecialSurface.h" |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 16 | #include "SkWriteBuffer.h" |
junov@chromium.org | f44fcdc | 2013-08-07 20:00:55 +0000 | [diff] [blame] | 17 | |
robertphillips | 225db44 | 2016-04-17 14:27:05 -0700 | [diff] [blame] | 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 | |
commit-bot@chromium.org | f7efa50 | 2014-04-11 18:57:00 +0000 | [diff] [blame] | 29 | SkDropShadowImageFilter::SkDropShadowImageFilter(SkScalar dx, SkScalar dy, |
| 30 | SkScalar sigmaX, SkScalar sigmaY, SkColor color, |
robertphillips | c416912 | 2016-04-06 08:40:59 -0700 | [diff] [blame] | 31 | ShadowMode shadowMode, sk_sp<SkImageFilter> input, |
senorblanco | 24e06d5 | 2015-03-18 12:11:33 -0700 | [diff] [blame] | 32 | const CropRect* cropRect) |
robertphillips | c416912 | 2016-04-06 08:40:59 -0700 | [diff] [blame] | 33 | : INHERITED(&input, 1, cropRect) |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 34 | , fDx(dx) |
| 35 | , fDy(dy) |
| 36 | , fSigmaX(sigmaX) |
| 37 | , fSigmaY(sigmaY) |
junov@chromium.org | f44fcdc | 2013-08-07 20:00:55 +0000 | [diff] [blame] | 38 | , fColor(color) |
robertphillips | 0a291c7 | 2016-03-23 05:00:01 -0700 | [diff] [blame] | 39 | , fShadowMode(shadowMode) { |
junov@chromium.org | f44fcdc | 2013-08-07 20:00:55 +0000 | [diff] [blame] | 40 | } |
| 41 | |
reed | 60c9b58 | 2016-04-03 09:11:13 -0700 | [diff] [blame] | 42 | sk_sp<SkFlattenable> SkDropShadowImageFilter::CreateProc(SkReadBuffer& buffer) { |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 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(); |
sugoi | 234f036 | 2014-10-23 13:59:52 -0700 | [diff] [blame] | 49 | ShadowMode shadowMode = buffer.isVersionLT(SkReadBuffer::kDropShadowMode_Version) ? |
| 50 | kDrawShadowAndForeground_ShadowMode : |
| 51 | static_cast<ShadowMode>(buffer.readInt()); |
robertphillips | c416912 | 2016-04-06 08:40:59 -0700 | [diff] [blame] | 52 | return Make(dx, dy, sigmaX, sigmaY, color, shadowMode, common.getInput(0), &common.cropRect()); |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | void SkDropShadowImageFilter::flatten(SkWriteBuffer& buffer) const { |
junov@chromium.org | f44fcdc | 2013-08-07 20:00:55 +0000 | [diff] [blame] | 56 | this->INHERITED::flatten(buffer); |
| 57 | buffer.writeScalar(fDx); |
| 58 | buffer.writeScalar(fDy); |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 59 | buffer.writeScalar(fSigmaX); |
| 60 | buffer.writeScalar(fSigmaY); |
junov@chromium.org | f44fcdc | 2013-08-07 20:00:55 +0000 | [diff] [blame] | 61 | buffer.writeColor(fColor); |
sugoi | 234f036 | 2014-10-23 13:59:52 -0700 | [diff] [blame] | 62 | buffer.writeInt(static_cast<int>(fShadowMode)); |
junov@chromium.org | f44fcdc | 2013-08-07 20:00:55 +0000 | [diff] [blame] | 63 | } |
| 64 | |
robertphillips | 2302de9 | 2016-03-24 07:26:32 -0700 | [diff] [blame] | 65 | sk_sp<SkSpecialImage> SkDropShadowImageFilter::onFilterImage(SkSpecialImage* source, |
| 66 | const Context& ctx, |
| 67 | SkIPoint* offset) const { |
robertphillips | 0a291c7 | 2016-03-23 05:00:01 -0700 | [diff] [blame] | 68 | SkIPoint inputOffset = SkIPoint::Make(0, 0); |
robertphillips | 2302de9 | 2016-03-24 07:26:32 -0700 | [diff] [blame] | 69 | sk_sp<SkSpecialImage> input(this->filterInput(0, source, ctx, &inputOffset)); |
robertphillips | 0a291c7 | 2016-03-23 05:00:01 -0700 | [diff] [blame] | 70 | if (!input) { |
| 71 | return nullptr; |
| 72 | } |
junov@chromium.org | f44fcdc | 2013-08-07 20:00:55 +0000 | [diff] [blame] | 73 | |
robertphillips | 0a291c7 | 2016-03-23 05:00:01 -0700 | [diff] [blame] | 74 | const SkIRect inputBounds = SkIRect::MakeXYWH(inputOffset.x(), inputOffset.y(), |
| 75 | input->width(), input->height()); |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 76 | SkIRect bounds; |
robertphillips | 0a291c7 | 2016-03-23 05:00:01 -0700 | [diff] [blame] | 77 | if (!this->applyCropRect(ctx, inputBounds, &bounds)) { |
| 78 | return nullptr; |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 79 | } |
| 80 | |
brianosman | eed6b0e | 2016-09-23 13:04:05 -0700 | [diff] [blame] | 81 | sk_sp<SkSpecialSurface> surf(source->makeSurface(ctx.outputProperties(), bounds.size())); |
robertphillips | 0a291c7 | 2016-03-23 05:00:01 -0700 | [diff] [blame] | 82 | if (!surf) { |
| 83 | return nullptr; |
commit-bot@chromium.org | cd3b15c | 2013-12-04 17:06:49 +0000 | [diff] [blame] | 84 | } |
robertphillips | 0a291c7 | 2016-03-23 05:00:01 -0700 | [diff] [blame] | 85 | |
| 86 | SkCanvas* canvas = surf->getCanvas(); |
| 87 | SkASSERT(canvas); |
| 88 | |
| 89 | canvas->clear(0x0); |
junov@chromium.org | f44fcdc | 2013-08-07 20:00:55 +0000 | [diff] [blame] | 90 | |
senorblanco@chromium.org | ba31f1d | 2014-05-07 20:56:08 +0000 | [diff] [blame] | 91 | SkVector sigma = SkVector::Make(fSigmaX, fSigmaY); |
| 92 | ctx.ctm().mapVectors(&sigma, 1); |
senorblanco@chromium.org | 74bdde0 | 2014-01-28 17:57:26 +0000 | [diff] [blame] | 93 | sigma.fX = SkMaxScalar(0, sigma.fX); |
| 94 | sigma.fY = SkMaxScalar(0, sigma.fY); |
robertphillips | 0a291c7 | 2016-03-23 05:00:01 -0700 | [diff] [blame] | 95 | |
junov@chromium.org | f44fcdc | 2013-08-07 20:00:55 +0000 | [diff] [blame] | 96 | SkPaint paint; |
Robert Phillips | 92a895e | 2016-12-09 14:39:35 +0000 | [diff] [blame] | 97 | paint.setAntiAlias(true); |
robertphillips | 6e7025a | 2016-04-04 04:31:25 -0700 | [diff] [blame] | 98 | paint.setImageFilter(SkBlurImageFilter::Make(sigma.fX, sigma.fY, nullptr)); |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 99 | paint.setColorFilter(SkColorFilter::MakeModeFilter(fColor, SkBlendMode::kSrcIn)); |
robertphillips | 0a291c7 | 2016-03-23 05:00:01 -0700 | [diff] [blame] | 100 | |
senorblanco@chromium.org | ba31f1d | 2014-05-07 20:56:08 +0000 | [diff] [blame] | 101 | SkVector offsetVec = SkVector::Make(fDx, fDy); |
| 102 | ctx.ctm().mapVectors(&offsetVec, 1); |
robertphillips | 0a291c7 | 2016-03-23 05:00:01 -0700 | [diff] [blame] | 103 | |
| 104 | canvas->translate(SkIntToScalar(inputOffset.fX - bounds.fLeft), |
| 105 | SkIntToScalar(inputOffset.fY - bounds.fTop)); |
| 106 | input->draw(canvas, offsetVec.fX, offsetVec.fY, &paint); |
| 107 | |
sugoi | 234f036 | 2014-10-23 13:59:52 -0700 | [diff] [blame] | 108 | if (fShadowMode == kDrawShadowAndForeground_ShadowMode) { |
robertphillips | 0a291c7 | 2016-03-23 05:00:01 -0700 | [diff] [blame] | 109 | input->draw(canvas, 0, 0, nullptr); |
sugoi | 234f036 | 2014-10-23 13:59:52 -0700 | [diff] [blame] | 110 | } |
senorblanco@chromium.org | 6776b82 | 2014-01-03 21:48:22 +0000 | [diff] [blame] | 111 | offset->fX = bounds.fLeft; |
| 112 | offset->fY = bounds.fTop; |
robertphillips | 2302de9 | 2016-03-24 07:26:32 -0700 | [diff] [blame] | 113 | return surf->makeImageSnapshot(); |
junov@chromium.org | f44fcdc | 2013-08-07 20:00:55 +0000 | [diff] [blame] | 114 | } |
senorblanco@chromium.org | 336d1d7 | 2014-01-27 21:03:17 +0000 | [diff] [blame] | 115 | |
Matt Sarett | 6d72ed9 | 2017-04-10 16:35:33 -0400 | [diff] [blame^] | 116 | sk_sp<SkImageFilter> SkDropShadowImageFilter::onMakeColorSpace(SkColorSpaceXformer* xformer) const { |
| 117 | SkASSERT(1 == this->countInputs()); |
| 118 | |
| 119 | sk_sp<SkImageFilter> input = |
| 120 | this->getInput(0) ? this->getInput(0)->makeColorSpace(xformer) : nullptr; |
| 121 | |
| 122 | return SkDropShadowImageFilter::Make(fDx, fDy, fSigmaX, fSigmaY, xformer->apply(fColor), |
| 123 | fShadowMode, std::move(input)); |
| 124 | } |
| 125 | |
senorblanco | e5e7984 | 2016-03-21 14:51:59 -0700 | [diff] [blame] | 126 | SkRect SkDropShadowImageFilter::computeFastBounds(const SkRect& src) const { |
| 127 | SkRect bounds = this->getInput(0) ? this->getInput(0)->computeFastBounds(src) : src; |
| 128 | SkRect shadowBounds = bounds; |
senorblanco@chromium.org | 336d1d7 | 2014-01-27 21:03:17 +0000 | [diff] [blame] | 129 | shadowBounds.offset(fDx, fDy); |
Mike Reed | 8be952a | 2017-02-13 20:44:33 -0500 | [diff] [blame] | 130 | shadowBounds.outset(fSigmaX * 3, fSigmaY * 3); |
sugoi | 234f036 | 2014-10-23 13:59:52 -0700 | [diff] [blame] | 131 | if (fShadowMode == kDrawShadowAndForeground_ShadowMode) { |
senorblanco | e5e7984 | 2016-03-21 14:51:59 -0700 | [diff] [blame] | 132 | bounds.join(shadowBounds); |
sugoi | 234f036 | 2014-10-23 13:59:52 -0700 | [diff] [blame] | 133 | } else { |
senorblanco | e5e7984 | 2016-03-21 14:51:59 -0700 | [diff] [blame] | 134 | bounds = shadowBounds; |
sugoi | 234f036 | 2014-10-23 13:59:52 -0700 | [diff] [blame] | 135 | } |
senorblanco | e5e7984 | 2016-03-21 14:51:59 -0700 | [diff] [blame] | 136 | return bounds; |
senorblanco@chromium.org | 336d1d7 | 2014-01-27 21:03:17 +0000 | [diff] [blame] | 137 | } |
senorblanco@chromium.org | c4b12f1 | 2014-02-05 17:51:22 +0000 | [diff] [blame] | 138 | |
senorblanco | e5e7984 | 2016-03-21 14:51:59 -0700 | [diff] [blame] | 139 | SkIRect SkDropShadowImageFilter::onFilterNodeBounds(const SkIRect& src, const SkMatrix& ctm, |
| 140 | MapDirection direction) const { |
senorblanco@chromium.org | ba31f1d | 2014-05-07 20:56:08 +0000 | [diff] [blame] | 141 | SkVector offsetVec = SkVector::Make(fDx, fDy); |
senorblanco | db64af3 | 2015-12-09 10:11:43 -0800 | [diff] [blame] | 142 | if (kReverse_MapDirection == direction) { |
| 143 | offsetVec.negate(); |
| 144 | } |
senorblanco@chromium.org | ba31f1d | 2014-05-07 20:56:08 +0000 | [diff] [blame] | 145 | ctm.mapVectors(&offsetVec, 1); |
senorblanco | e5e7984 | 2016-03-21 14:51:59 -0700 | [diff] [blame] | 146 | SkIRect dst = src.makeOffset(SkScalarCeilToInt(offsetVec.x()), |
| 147 | SkScalarCeilToInt(offsetVec.y())); |
senorblanco@chromium.org | ba31f1d | 2014-05-07 20:56:08 +0000 | [diff] [blame] | 148 | SkVector sigma = SkVector::Make(fSigmaX, fSigmaY); |
| 149 | ctm.mapVectors(&sigma, 1); |
jbroman | 203a993 | 2016-07-11 14:07:59 -0700 | [diff] [blame] | 150 | dst.outset( |
Mike Reed | 8be952a | 2017-02-13 20:44:33 -0500 | [diff] [blame] | 151 | SkScalarCeilToInt(SkScalarAbs(sigma.x() * 3)), |
| 152 | SkScalarCeilToInt(SkScalarAbs(sigma.y() * 3))); |
sugoi | 234f036 | 2014-10-23 13:59:52 -0700 | [diff] [blame] | 153 | if (fShadowMode == kDrawShadowAndForeground_ShadowMode) { |
senorblanco | e5e7984 | 2016-03-21 14:51:59 -0700 | [diff] [blame] | 154 | dst.join(src); |
sugoi | 234f036 | 2014-10-23 13:59:52 -0700 | [diff] [blame] | 155 | } |
senorblanco | e5e7984 | 2016-03-21 14:51:59 -0700 | [diff] [blame] | 156 | return dst; |
senorblanco@chromium.org | c4b12f1 | 2014-02-05 17:51:22 +0000 | [diff] [blame] | 157 | } |
robertphillips | f3f5bad | 2014-12-19 13:49:15 -0800 | [diff] [blame] | 158 | |
| 159 | #ifndef SK_IGNORE_TO_STRING |
| 160 | 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 | |
bungeman | 99fe822 | 2015-08-20 07:57:51 -0700 | [diff] [blame] | 175 | static_assert(kShadowModeCount == SK_ARRAY_COUNT(gModeStrings), "enum_mismatch"); |
robertphillips | f3f5bad | 2014-12-19 13:49:15 -0800 | [diff] [blame] | 176 | |
| 177 | str->appendf(" mode: %s", gModeStrings[fShadowMode]); |
| 178 | |
| 179 | str->append(")"); |
| 180 | } |
| 181 | #endif |