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