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 | |
| 10 | #include "SkBitmap.h" |
| 11 | #include "SkBlurImageFilter.h" |
| 12 | #include "SkCanvas.h" |
| 13 | #include "SkColorMatrixFilter.h" |
| 14 | #include "SkDevice.h" |
| 15 | #include "SkFlattenableBuffers.h" |
| 16 | |
| 17 | SkDropShadowImageFilter::SkDropShadowImageFilter(SkScalar dx, SkScalar dy, SkScalar sigma, SkColor color, SkImageFilter* input) |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 18 | : INHERITED(input) |
junov@chromium.org | f44fcdc | 2013-08-07 20:00:55 +0000 | [diff] [blame] | 19 | , fDx(dx) |
| 20 | , fDy(dy) |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 21 | , fSigmaX(sigma) |
| 22 | , fSigmaY(sigma) |
| 23 | , fColor(color) |
| 24 | { |
| 25 | } |
| 26 | |
| 27 | SkDropShadowImageFilter::SkDropShadowImageFilter(SkScalar dx, SkScalar dy, SkScalar sigmaX, SkScalar sigmaY, SkColor color, SkImageFilter* input, const CropRect* cropRect) |
| 28 | : INHERITED(input, cropRect) |
| 29 | , fDx(dx) |
| 30 | , fDy(dy) |
| 31 | , fSigmaX(sigmaX) |
| 32 | , fSigmaY(sigmaY) |
junov@chromium.org | f44fcdc | 2013-08-07 20:00:55 +0000 | [diff] [blame] | 33 | , fColor(color) |
| 34 | { |
| 35 | } |
| 36 | |
commit-bot@chromium.org | ce33d60 | 2013-11-25 21:46:31 +0000 | [diff] [blame] | 37 | SkDropShadowImageFilter::SkDropShadowImageFilter(SkFlattenableReadBuffer& buffer) |
| 38 | : INHERITED(1, buffer) { |
junov@chromium.org | f44fcdc | 2013-08-07 20:00:55 +0000 | [diff] [blame] | 39 | fDx = buffer.readScalar(); |
| 40 | fDy = buffer.readScalar(); |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 41 | fSigmaX = buffer.readScalar(); |
| 42 | fSigmaY = buffer.readScalar(); |
junov@chromium.org | f44fcdc | 2013-08-07 20:00:55 +0000 | [diff] [blame] | 43 | fColor = buffer.readColor(); |
commit-bot@chromium.org | c0b7e10 | 2013-10-23 17:06:21 +0000 | [diff] [blame] | 44 | buffer.validate(SkScalarIsFinite(fDx) && |
| 45 | SkScalarIsFinite(fDy) && |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 46 | SkScalarIsFinite(fSigmaX) && |
| 47 | SkScalarIsFinite(fSigmaY)); |
junov@chromium.org | f44fcdc | 2013-08-07 20:00:55 +0000 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | void SkDropShadowImageFilter::flatten(SkFlattenableWriteBuffer& buffer) const |
| 51 | { |
| 52 | this->INHERITED::flatten(buffer); |
| 53 | buffer.writeScalar(fDx); |
| 54 | buffer.writeScalar(fDy); |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 55 | buffer.writeScalar(fSigmaX); |
| 56 | buffer.writeScalar(fSigmaY); |
junov@chromium.org | f44fcdc | 2013-08-07 20:00:55 +0000 | [diff] [blame] | 57 | buffer.writeColor(fColor); |
| 58 | } |
| 59 | |
senorblanco@chromium.org | 6776b82 | 2014-01-03 21:48:22 +0000 | [diff] [blame] | 60 | bool SkDropShadowImageFilter::onFilterImage(Proxy* proxy, const SkBitmap& source, const SkMatrix& matrix, SkBitmap* result, SkIPoint* offset) |
junov@chromium.org | f44fcdc | 2013-08-07 20:00:55 +0000 | [diff] [blame] | 61 | { |
| 62 | SkBitmap src = source; |
senorblanco@chromium.org | 6776b82 | 2014-01-03 21:48:22 +0000 | [diff] [blame] | 63 | SkIPoint srcOffset = SkIPoint::Make(0, 0); |
| 64 | if (getInput(0) && !getInput(0)->filterImage(proxy, source, matrix, &src, &srcOffset)) |
junov@chromium.org | f44fcdc | 2013-08-07 20:00:55 +0000 | [diff] [blame] | 65 | return false; |
| 66 | |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 67 | SkIRect bounds; |
| 68 | src.getBounds(&bounds); |
senorblanco@chromium.org | 6776b82 | 2014-01-03 21:48:22 +0000 | [diff] [blame] | 69 | bounds.offset(srcOffset); |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 70 | if (!this->applyCropRect(&bounds, matrix)) { |
| 71 | return false; |
| 72 | } |
| 73 | |
| 74 | SkAutoTUnref<SkBaseDevice> device(proxy->createDevice(bounds.width(), bounds.height())); |
commit-bot@chromium.org | cd3b15c | 2013-12-04 17:06:49 +0000 | [diff] [blame] | 75 | if (NULL == device.get()) { |
| 76 | return false; |
| 77 | } |
junov@chromium.org | f44fcdc | 2013-08-07 20:00:55 +0000 | [diff] [blame] | 78 | SkCanvas canvas(device.get()); |
| 79 | |
senorblanco@chromium.org | 2bfe36b | 2014-01-20 19:58:28 +0000 | [diff] [blame^] | 80 | SkVector sigma, localSigma = SkVector::Make(fSigmaX, fSigmaY); |
| 81 | matrix.mapVectors(&sigma, &localSigma, 1); |
| 82 | SkAutoTUnref<SkImageFilter> blurFilter(new SkBlurImageFilter(sigma.fX, sigma.fY)); |
junov@chromium.org | f44fcdc | 2013-08-07 20:00:55 +0000 | [diff] [blame] | 83 | SkAutoTUnref<SkColorFilter> colorFilter(SkColorFilter::CreateModeFilter(fColor, SkXfermode::kSrcIn_Mode)); |
| 84 | SkPaint paint; |
| 85 | paint.setImageFilter(blurFilter.get()); |
| 86 | paint.setColorFilter(colorFilter.get()); |
| 87 | paint.setXfermodeMode(SkXfermode::kSrcOver_Mode); |
senorblanco@chromium.org | 2bfe36b | 2014-01-20 19:58:28 +0000 | [diff] [blame^] | 88 | SkVector offsetVec, localOffsetVec = SkVector::Make(fDx, fDy); |
| 89 | matrix.mapVectors(&offsetVec, &localOffsetVec, 1); |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 90 | canvas.translate(-SkIntToScalar(bounds.fLeft), -SkIntToScalar(bounds.fTop)); |
senorblanco@chromium.org | 2bfe36b | 2014-01-20 19:58:28 +0000 | [diff] [blame^] | 91 | canvas.drawBitmap(src, offsetVec.fX, offsetVec.fY, &paint); |
junov@chromium.org | f44fcdc | 2013-08-07 20:00:55 +0000 | [diff] [blame] | 92 | canvas.drawBitmap(src, 0, 0); |
| 93 | *result = device->accessBitmap(false); |
senorblanco@chromium.org | 6776b82 | 2014-01-03 21:48:22 +0000 | [diff] [blame] | 94 | offset->fX = bounds.fLeft; |
| 95 | offset->fY = bounds.fTop; |
junov@chromium.org | f44fcdc | 2013-08-07 20:00:55 +0000 | [diff] [blame] | 96 | return true; |
| 97 | } |