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 | |
| 17 | bool SkOffsetImageFilter::onFilterImage(Proxy* proxy, const SkBitmap& source, |
senorblanco@chromium.org | 4cb543d | 2014-03-14 15:44:01 +0000 | [diff] [blame] | 18 | const Context& ctx, |
senorblanco@chromium.org | 9daf96a | 2012-11-06 20:29:39 +0000 | [diff] [blame] | 19 | SkBitmap* result, |
commit-bot@chromium.org | ae761f7 | 2014-02-05 22:32:02 +0000 | [diff] [blame] | 20 | SkIPoint* offset) const { |
commit-bot@chromium.org | 1a4fb70 | 2013-09-26 16:09:28 +0000 | [diff] [blame] | 21 | SkImageFilter* input = getInput(0); |
senorblanco@chromium.org | 6840076 | 2013-05-24 15:04:07 +0000 | [diff] [blame] | 22 | SkBitmap src = source; |
senorblanco@chromium.org | 6776b82 | 2014-01-03 21:48:22 +0000 | [diff] [blame] | 23 | SkIPoint srcOffset = SkIPoint::Make(0, 0); |
commit-bot@chromium.org | f2f8f3a | 2013-11-01 11:55:06 +0000 | [diff] [blame] | 24 | #ifdef SK_DISABLE_OFFSETIMAGEFILTER_OPTIMIZATION |
| 25 | if (false) { |
| 26 | #else |
senorblanco@chromium.org | b295fb6 | 2013-10-10 13:51:19 +0000 | [diff] [blame] | 27 | if (!cropRectIsSet()) { |
commit-bot@chromium.org | f2f8f3a | 2013-11-01 11:55:06 +0000 | [diff] [blame] | 28 | #endif |
senorblanco@chromium.org | 4cb543d | 2014-03-14 15:44:01 +0000 | [diff] [blame] | 29 | if (input && !input->filterImage(proxy, source, ctx, &src, &srcOffset)) { |
commit-bot@chromium.org | 1a4fb70 | 2013-09-26 16:09:28 +0000 | [diff] [blame] | 30 | return false; |
| 31 | } |
| 32 | |
| 33 | SkVector vec; |
senorblanco@chromium.org | 4cb543d | 2014-03-14 15:44:01 +0000 | [diff] [blame] | 34 | ctx.ctm().mapVectors(&vec, &fOffset, 1); |
commit-bot@chromium.org | 1a4fb70 | 2013-09-26 16:09:28 +0000 | [diff] [blame] | 35 | |
senorblanco@chromium.org | 6776b82 | 2014-01-03 21:48:22 +0000 | [diff] [blame] | 36 | offset->fX = srcOffset.fX + SkScalarRoundToInt(vec.fX); |
| 37 | offset->fY = srcOffset.fY + SkScalarRoundToInt(vec.fY); |
commit-bot@chromium.org | 1a4fb70 | 2013-09-26 16:09:28 +0000 | [diff] [blame] | 38 | *result = src; |
| 39 | } else { |
senorblanco@chromium.org | 4cb543d | 2014-03-14 15:44:01 +0000 | [diff] [blame] | 40 | if (input && !input->filterImage(proxy, source, ctx, &src, &srcOffset)) { |
commit-bot@chromium.org | 1a4fb70 | 2013-09-26 16:09:28 +0000 | [diff] [blame] | 41 | return false; |
| 42 | } |
| 43 | |
| 44 | SkIRect bounds; |
senorblanco@chromium.org | 1182529 | 2014-03-14 17:44:41 +0000 | [diff] [blame] | 45 | if (!this->applyCropRect(ctx, src, srcOffset, &bounds)) { |
commit-bot@chromium.org | 1a4fb70 | 2013-09-26 16:09:28 +0000 | [diff] [blame] | 46 | return false; |
| 47 | } |
| 48 | |
| 49 | SkAutoTUnref<SkBaseDevice> device(proxy->createDevice(bounds.width(), bounds.height())); |
commit-bot@chromium.org | cd3b15c | 2013-12-04 17:06:49 +0000 | [diff] [blame] | 50 | if (NULL == device.get()) { |
| 51 | return false; |
| 52 | } |
commit-bot@chromium.org | 1a4fb70 | 2013-09-26 16:09:28 +0000 | [diff] [blame] | 53 | SkCanvas canvas(device); |
| 54 | SkPaint paint; |
| 55 | paint.setXfermodeMode(SkXfermode::kSrc_Mode); |
senorblanco@chromium.org | 6776b82 | 2014-01-03 21:48:22 +0000 | [diff] [blame] | 56 | canvas.translate(SkIntToScalar(srcOffset.fX - bounds.fLeft), |
| 57 | SkIntToScalar(srcOffset.fY - bounds.fTop)); |
senorblanco@chromium.org | e09244d | 2014-03-04 19:05:25 +0000 | [diff] [blame] | 58 | SkVector vec; |
senorblanco@chromium.org | 4cb543d | 2014-03-14 15:44:01 +0000 | [diff] [blame] | 59 | ctx.ctm().mapVectors(&vec, &fOffset, 1); |
senorblanco@chromium.org | e09244d | 2014-03-04 19:05:25 +0000 | [diff] [blame] | 60 | canvas.drawBitmap(src, vec.x(), vec.y(), &paint); |
commit-bot@chromium.org | 1a4fb70 | 2013-09-26 16:09:28 +0000 | [diff] [blame] | 61 | *result = device->accessBitmap(false); |
senorblanco@chromium.org | 6776b82 | 2014-01-03 21:48:22 +0000 | [diff] [blame] | 62 | offset->fX = bounds.fLeft; |
| 63 | offset->fY = bounds.fTop; |
senorblanco@chromium.org | 6840076 | 2013-05-24 15:04:07 +0000 | [diff] [blame] | 64 | } |
senorblanco@chromium.org | 9daf96a | 2012-11-06 20:29:39 +0000 | [diff] [blame] | 65 | return true; |
| 66 | } |
| 67 | |
senorblanco@chromium.org | 336d1d7 | 2014-01-27 21:03:17 +0000 | [diff] [blame] | 68 | void SkOffsetImageFilter::computeFastBounds(const SkRect& src, SkRect* dst) const { |
| 69 | if (getInput(0)) { |
| 70 | getInput(0)->computeFastBounds(src, dst); |
| 71 | } else { |
| 72 | *dst = src; |
| 73 | } |
senorblanco@chromium.org | c4b12f1 | 2014-02-05 17:51:22 +0000 | [diff] [blame] | 74 | SkRect copy = *dst; |
senorblanco@chromium.org | 336d1d7 | 2014-01-27 21:03:17 +0000 | [diff] [blame] | 75 | dst->offset(fOffset.fX, fOffset.fY); |
senorblanco@chromium.org | c4b12f1 | 2014-02-05 17:51:22 +0000 | [diff] [blame] | 76 | dst->join(copy); |
senorblanco@chromium.org | 336d1d7 | 2014-01-27 21:03:17 +0000 | [diff] [blame] | 77 | } |
| 78 | |
senorblanco@chromium.org | 9daf96a | 2012-11-06 20:29:39 +0000 | [diff] [blame] | 79 | bool SkOffsetImageFilter::onFilterBounds(const SkIRect& src, const SkMatrix& ctm, |
senorblanco@chromium.org | c4b12f1 | 2014-02-05 17:51:22 +0000 | [diff] [blame] | 80 | SkIRect* dst) const { |
senorblanco@chromium.org | 9daf96a | 2012-11-06 20:29:39 +0000 | [diff] [blame] | 81 | SkVector vec; |
| 82 | ctm.mapVectors(&vec, &fOffset, 1); |
| 83 | |
senorblanco@chromium.org | 06342a2 | 2014-03-11 18:29:42 +0000 | [diff] [blame] | 84 | SkIRect bounds = src; |
| 85 | bounds.offset(-SkScalarCeilToInt(vec.fX), -SkScalarCeilToInt(vec.fY)); |
| 86 | bounds.join(src); |
senorblanco@chromium.org | 29ac34e | 2014-05-28 19:29:25 +0000 | [diff] [blame] | 87 | if (getInput(0)) { |
| 88 | return getInput(0)->filterBounds(bounds, ctm, dst); |
| 89 | } |
senorblanco@chromium.org | 06342a2 | 2014-03-11 18:29:42 +0000 | [diff] [blame] | 90 | *dst = bounds; |
senorblanco@chromium.org | 9daf96a | 2012-11-06 20:29:39 +0000 | [diff] [blame] | 91 | return true; |
| 92 | } |
| 93 | |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 94 | SkFlattenable* SkOffsetImageFilter::CreateProc(SkReadBuffer& buffer) { |
| 95 | SK_IMAGEFILTER_UNFLATTEN_COMMON(common, 1); |
| 96 | SkPoint offset; |
| 97 | buffer.readPoint(&offset); |
senorblanco | 5e5f948 | 2014-08-26 12:27:12 -0700 | [diff] [blame^] | 98 | return Create(offset.x(), offset.y(), common.getInput(0), &common.cropRect(), common.uniqueID()); |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 99 | } |
| 100 | |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 101 | void SkOffsetImageFilter::flatten(SkWriteBuffer& buffer) const { |
senorblanco@chromium.org | 9daf96a | 2012-11-06 20:29:39 +0000 | [diff] [blame] | 102 | this->INHERITED::flatten(buffer); |
| 103 | buffer.writePoint(fOffset); |
| 104 | } |
| 105 | |
commit-bot@chromium.org | 1a4fb70 | 2013-09-26 16:09:28 +0000 | [diff] [blame] | 106 | SkOffsetImageFilter::SkOffsetImageFilter(SkScalar dx, SkScalar dy, SkImageFilter* input, |
senorblanco | 5e5f948 | 2014-08-26 12:27:12 -0700 | [diff] [blame^] | 107 | const CropRect* cropRect, uint32_t uniqueID) |
| 108 | : INHERITED(1, &input, cropRect, uniqueID) { |
senorblanco@chromium.org | 9daf96a | 2012-11-06 20:29:39 +0000 | [diff] [blame] | 109 | fOffset.set(dx, dy); |
| 110 | } |
| 111 | |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 112 | #ifdef SK_SUPPORT_LEGACY_DEEPFLATTENING |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 113 | SkOffsetImageFilter::SkOffsetImageFilter(SkReadBuffer& buffer) |
commit-bot@chromium.org | ce33d60 | 2013-11-25 21:46:31 +0000 | [diff] [blame] | 114 | : INHERITED(1, buffer) { |
senorblanco@chromium.org | 9daf96a | 2012-11-06 20:29:39 +0000 | [diff] [blame] | 115 | buffer.readPoint(&fOffset); |
commit-bot@chromium.org | c0b7e10 | 2013-10-23 17:06:21 +0000 | [diff] [blame] | 116 | buffer.validate(SkScalarIsFinite(fOffset.fX) && |
| 117 | SkScalarIsFinite(fOffset.fY)); |
senorblanco@chromium.org | 9daf96a | 2012-11-06 20:29:39 +0000 | [diff] [blame] | 118 | } |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 119 | #endif |