senorblanco@chromium.org | 9daf96a | 2012-11-06 20:29:39 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2012 The Android Open Source Project |
| 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 "SkOffsetImageFilter.h" |
| 9 | #include "SkBitmap.h" |
commit-bot@chromium.org | 1a4fb70 | 2013-09-26 16:09:28 +0000 | [diff] [blame] | 10 | #include "SkCanvas.h" |
| 11 | #include "SkDevice.h" |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 12 | #include "SkReadBuffer.h" |
| 13 | #include "SkWriteBuffer.h" |
commit-bot@chromium.org | 1a4fb70 | 2013-09-26 16:09:28 +0000 | [diff] [blame] | 14 | #include "SkMatrix.h" |
| 15 | #include "SkPaint.h" |
senorblanco@chromium.org | 9daf96a | 2012-11-06 20:29:39 +0000 | [diff] [blame] | 16 | |
robertphillips | 48e7846 | 2016-02-17 13:57:16 -0800 | [diff] [blame^] | 17 | bool SkOffsetImageFilter::onFilterImageDeprecated(Proxy* proxy, const SkBitmap& source, |
| 18 | const Context& ctx, |
| 19 | SkBitmap* result, |
| 20 | SkIPoint* offset) const { |
senorblanco@chromium.org | 6840076 | 2013-05-24 15:04:07 +0000 | [diff] [blame] | 21 | SkBitmap src = source; |
senorblanco@chromium.org | 6776b82 | 2014-01-03 21:48:22 +0000 | [diff] [blame] | 22 | SkIPoint srcOffset = SkIPoint::Make(0, 0); |
senorblanco@chromium.org | b295fb6 | 2013-10-10 13:51:19 +0000 | [diff] [blame] | 23 | if (!cropRectIsSet()) { |
robertphillips | 48e7846 | 2016-02-17 13:57:16 -0800 | [diff] [blame^] | 24 | if (!this->filterInputDeprecated(0, proxy, source, ctx, &src, &srcOffset)) { |
commit-bot@chromium.org | 1a4fb70 | 2013-09-26 16:09:28 +0000 | [diff] [blame] | 25 | return false; |
| 26 | } |
| 27 | |
| 28 | SkVector vec; |
senorblanco@chromium.org | 4cb543d | 2014-03-14 15:44:01 +0000 | [diff] [blame] | 29 | ctx.ctm().mapVectors(&vec, &fOffset, 1); |
commit-bot@chromium.org | 1a4fb70 | 2013-09-26 16:09:28 +0000 | [diff] [blame] | 30 | |
senorblanco@chromium.org | 6776b82 | 2014-01-03 21:48:22 +0000 | [diff] [blame] | 31 | offset->fX = srcOffset.fX + SkScalarRoundToInt(vec.fX); |
| 32 | offset->fY = srcOffset.fY + SkScalarRoundToInt(vec.fY); |
commit-bot@chromium.org | 1a4fb70 | 2013-09-26 16:09:28 +0000 | [diff] [blame] | 33 | *result = src; |
| 34 | } else { |
robertphillips | 48e7846 | 2016-02-17 13:57:16 -0800 | [diff] [blame^] | 35 | if (!this->filterInputDeprecated(0, proxy, source, ctx, &src, &srcOffset)) { |
commit-bot@chromium.org | 1a4fb70 | 2013-09-26 16:09:28 +0000 | [diff] [blame] | 36 | return false; |
| 37 | } |
| 38 | |
| 39 | SkIRect bounds; |
senorblanco | afec27f | 2016-02-16 09:11:18 -0800 | [diff] [blame] | 40 | SkIRect srcBounds = src.bounds(); |
| 41 | srcBounds.offset(srcOffset); |
| 42 | if (!this->applyCropRect(ctx, srcBounds, &bounds)) { |
commit-bot@chromium.org | 1a4fb70 | 2013-09-26 16:09:28 +0000 | [diff] [blame] | 43 | return false; |
| 44 | } |
| 45 | |
| 46 | SkAutoTUnref<SkBaseDevice> device(proxy->createDevice(bounds.width(), bounds.height())); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 47 | if (nullptr == device.get()) { |
commit-bot@chromium.org | cd3b15c | 2013-12-04 17:06:49 +0000 | [diff] [blame] | 48 | return false; |
| 49 | } |
commit-bot@chromium.org | 1a4fb70 | 2013-09-26 16:09:28 +0000 | [diff] [blame] | 50 | SkCanvas canvas(device); |
| 51 | SkPaint paint; |
| 52 | paint.setXfermodeMode(SkXfermode::kSrc_Mode); |
senorblanco@chromium.org | 6776b82 | 2014-01-03 21:48:22 +0000 | [diff] [blame] | 53 | canvas.translate(SkIntToScalar(srcOffset.fX - bounds.fLeft), |
| 54 | SkIntToScalar(srcOffset.fY - bounds.fTop)); |
senorblanco@chromium.org | e09244d | 2014-03-04 19:05:25 +0000 | [diff] [blame] | 55 | SkVector vec; |
senorblanco@chromium.org | 4cb543d | 2014-03-14 15:44:01 +0000 | [diff] [blame] | 56 | ctx.ctm().mapVectors(&vec, &fOffset, 1); |
senorblanco@chromium.org | e09244d | 2014-03-04 19:05:25 +0000 | [diff] [blame] | 57 | canvas.drawBitmap(src, vec.x(), vec.y(), &paint); |
commit-bot@chromium.org | 1a4fb70 | 2013-09-26 16:09:28 +0000 | [diff] [blame] | 58 | *result = device->accessBitmap(false); |
senorblanco@chromium.org | 6776b82 | 2014-01-03 21:48:22 +0000 | [diff] [blame] | 59 | offset->fX = bounds.fLeft; |
| 60 | offset->fY = bounds.fTop; |
senorblanco@chromium.org | 6840076 | 2013-05-24 15:04:07 +0000 | [diff] [blame] | 61 | } |
senorblanco@chromium.org | 9daf96a | 2012-11-06 20:29:39 +0000 | [diff] [blame] | 62 | return true; |
| 63 | } |
| 64 | |
senorblanco@chromium.org | 336d1d7 | 2014-01-27 21:03:17 +0000 | [diff] [blame] | 65 | void SkOffsetImageFilter::computeFastBounds(const SkRect& src, SkRect* dst) const { |
| 66 | if (getInput(0)) { |
| 67 | getInput(0)->computeFastBounds(src, dst); |
| 68 | } else { |
| 69 | *dst = src; |
| 70 | } |
| 71 | dst->offset(fOffset.fX, fOffset.fY); |
| 72 | } |
| 73 | |
senorblanco | db64af3 | 2015-12-09 10:11:43 -0800 | [diff] [blame] | 74 | void SkOffsetImageFilter::onFilterNodeBounds(const SkIRect& src, const SkMatrix& ctm, |
| 75 | SkIRect* dst, MapDirection direction) const { |
senorblanco@chromium.org | 9daf96a | 2012-11-06 20:29:39 +0000 | [diff] [blame] | 76 | SkVector vec; |
| 77 | ctm.mapVectors(&vec, &fOffset, 1); |
senorblanco | db64af3 | 2015-12-09 10:11:43 -0800 | [diff] [blame] | 78 | if (kReverse_MapDirection == direction) { |
| 79 | vec.negate(); |
senorblanco@chromium.org | 29ac34e | 2014-05-28 19:29:25 +0000 | [diff] [blame] | 80 | } |
senorblanco | db64af3 | 2015-12-09 10:11:43 -0800 | [diff] [blame] | 81 | |
| 82 | *dst = src; |
| 83 | dst->offset(SkScalarCeilToInt(vec.fX), SkScalarCeilToInt(vec.fY)); |
senorblanco@chromium.org | 9daf96a | 2012-11-06 20:29:39 +0000 | [diff] [blame] | 84 | } |
| 85 | |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 86 | SkFlattenable* SkOffsetImageFilter::CreateProc(SkReadBuffer& buffer) { |
| 87 | SK_IMAGEFILTER_UNFLATTEN_COMMON(common, 1); |
| 88 | SkPoint offset; |
| 89 | buffer.readPoint(&offset); |
senorblanco | 24e06d5 | 2015-03-18 12:11:33 -0700 | [diff] [blame] | 90 | return Create(offset.x(), offset.y(), common.getInput(0), &common.cropRect()); |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 91 | } |
| 92 | |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 93 | void SkOffsetImageFilter::flatten(SkWriteBuffer& buffer) const { |
senorblanco@chromium.org | 9daf96a | 2012-11-06 20:29:39 +0000 | [diff] [blame] | 94 | this->INHERITED::flatten(buffer); |
| 95 | buffer.writePoint(fOffset); |
| 96 | } |
| 97 | |
commit-bot@chromium.org | 1a4fb70 | 2013-09-26 16:09:28 +0000 | [diff] [blame] | 98 | SkOffsetImageFilter::SkOffsetImageFilter(SkScalar dx, SkScalar dy, SkImageFilter* input, |
senorblanco | 24e06d5 | 2015-03-18 12:11:33 -0700 | [diff] [blame] | 99 | const CropRect* cropRect) |
| 100 | : INHERITED(1, &input, cropRect) { |
senorblanco@chromium.org | 9daf96a | 2012-11-06 20:29:39 +0000 | [diff] [blame] | 101 | fOffset.set(dx, dy); |
| 102 | } |
robertphillips | f3f5bad | 2014-12-19 13:49:15 -0800 | [diff] [blame] | 103 | |
| 104 | #ifndef SK_IGNORE_TO_STRING |
| 105 | void SkOffsetImageFilter::toString(SkString* str) const { |
| 106 | str->appendf("SkOffsetImageFilter: ("); |
| 107 | str->appendf("offset: (%f, %f) ", fOffset.fX, fOffset.fY); |
| 108 | str->append("input: ("); |
| 109 | if (this->getInput(0)) { |
| 110 | this->getInput(0)->toString(str); |
| 111 | } |
| 112 | str->append("))"); |
| 113 | } |
| 114 | #endif |