commit-bot@chromium.org | 1a4fb70 | 2013-09-26 16:09:28 +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 "SkTileImageFilter.h" |
| 9 | #include "SkBitmap.h" |
| 10 | #include "SkCanvas.h" |
| 11 | #include "SkDevice.h" |
senorblanco | 05dcb4c | 2016-01-12 07:49:15 -0800 | [diff] [blame] | 12 | #include "SkOffsetImageFilter.h" |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 13 | #include "SkReadBuffer.h" |
| 14 | #include "SkWriteBuffer.h" |
commit-bot@chromium.org | 1a4fb70 | 2013-09-26 16:09:28 +0000 | [diff] [blame] | 15 | #include "SkMatrix.h" |
| 16 | #include "SkPaint.h" |
| 17 | #include "SkShader.h" |
commit-bot@chromium.org | c0b7e10 | 2013-10-23 17:06:21 +0000 | [diff] [blame] | 18 | #include "SkValidationUtils.h" |
commit-bot@chromium.org | 1a4fb70 | 2013-09-26 16:09:28 +0000 | [diff] [blame] | 19 | |
reed | 5ea95df | 2015-10-06 14:05:32 -0700 | [diff] [blame] | 20 | SkImageFilter* SkTileImageFilter::Create(const SkRect& srcRect, const SkRect& dstRect, |
| 21 | SkImageFilter* input) { |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 22 | if (!SkIsValidRect(srcRect) || !SkIsValidRect(dstRect)) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 23 | return nullptr; |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 24 | } |
senorblanco | 05dcb4c | 2016-01-12 07:49:15 -0800 | [diff] [blame] | 25 | if (srcRect.width() == dstRect.width() && srcRect.height() == dstRect.height()) { |
| 26 | SkRect ir = dstRect; |
| 27 | if (!ir.intersect(srcRect)) { |
| 28 | return SkSafeRef(input); |
| 29 | } |
| 30 | CropRect cropRect(ir); |
| 31 | return SkOffsetImageFilter::Create(dstRect.x() - srcRect.x(), |
| 32 | dstRect.y() - srcRect.y(), |
| 33 | input, &cropRect); |
| 34 | } |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 35 | return new SkTileImageFilter(srcRect, dstRect, input); |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 36 | } |
| 37 | |
robertphillips | 48e7846 | 2016-02-17 13:57:16 -0800 | [diff] [blame^] | 38 | bool SkTileImageFilter::onFilterImageDeprecated(Proxy* proxy, const SkBitmap& src, |
| 39 | const Context& ctx, |
| 40 | SkBitmap* dst, SkIPoint* offset) const { |
robertphillips | d312dca | 2015-07-06 07:59:09 -0700 | [diff] [blame] | 41 | SkBitmap source = src; |
robertphillips | d312dca | 2015-07-06 07:59:09 -0700 | [diff] [blame] | 42 | SkIPoint srcOffset = SkIPoint::Make(0, 0); |
robertphillips | 48e7846 | 2016-02-17 13:57:16 -0800 | [diff] [blame^] | 43 | if (!this->filterInputDeprecated(0, proxy, src, ctx, &source, &srcOffset)) { |
robertphillips | d312dca | 2015-07-06 07:59:09 -0700 | [diff] [blame] | 44 | return false; |
| 45 | } |
commit-bot@chromium.org | 1a4fb70 | 2013-09-26 16:09:28 +0000 | [diff] [blame] | 46 | |
senorblanco@chromium.org | e93e1db | 2013-12-09 18:31:42 +0000 | [diff] [blame] | 47 | SkRect dstRect; |
senorblanco@chromium.org | 4cb543d | 2014-03-14 15:44:01 +0000 | [diff] [blame] | 48 | ctx.ctm().mapRect(&dstRect, fDstRect); |
senorblanco | b50b97d | 2016-01-06 09:46:24 -0800 | [diff] [blame] | 49 | if (!dstRect.intersect(SkRect::Make(ctx.clipBounds()))) { |
| 50 | offset->fX = offset->fY = 0; |
| 51 | return true; |
| 52 | } |
reed | b07a94f | 2014-11-19 05:03:18 -0800 | [diff] [blame] | 53 | const SkIRect dstIRect = dstRect.roundOut(); |
robertphillips | d312dca | 2015-07-06 07:59:09 -0700 | [diff] [blame] | 54 | int w = dstIRect.width(); |
| 55 | int h = dstIRect.height(); |
| 56 | if (!fSrcRect.width() || !fSrcRect.height() || !w || !h) { |
commit-bot@chromium.org | 1a4fb70 | 2013-09-26 16:09:28 +0000 | [diff] [blame] | 57 | return false; |
| 58 | } |
| 59 | |
senorblanco@chromium.org | e93e1db | 2013-12-09 18:31:42 +0000 | [diff] [blame] | 60 | SkRect srcRect; |
senorblanco@chromium.org | 4cb543d | 2014-03-14 15:44:01 +0000 | [diff] [blame] | 61 | ctx.ctm().mapRect(&srcRect, fSrcRect); |
senorblanco@chromium.org | e93e1db | 2013-12-09 18:31:42 +0000 | [diff] [blame] | 62 | SkIRect srcIRect; |
| 63 | srcRect.roundOut(&srcIRect); |
senorblanco@chromium.org | 6776b82 | 2014-01-03 21:48:22 +0000 | [diff] [blame] | 64 | srcIRect.offset(-srcOffset); |
commit-bot@chromium.org | 1a4fb70 | 2013-09-26 16:09:28 +0000 | [diff] [blame] | 65 | SkBitmap subset; |
senorblanco | a9fbd16 | 2016-01-11 14:09:09 -0800 | [diff] [blame] | 66 | SkIRect srcBounds; |
| 67 | source.getBounds(&srcBounds); |
senorblanco@chromium.org | 6776b82 | 2014-01-03 21:48:22 +0000 | [diff] [blame] | 68 | |
senorblanco | a9fbd16 | 2016-01-11 14:09:09 -0800 | [diff] [blame] | 69 | if (!SkIRect::Intersects(srcIRect, srcBounds)) { |
senorblanco@chromium.org | 6776b82 | 2014-01-03 21:48:22 +0000 | [diff] [blame] | 70 | offset->fX = offset->fY = 0; |
senorblanco@chromium.org | e93e1db | 2013-12-09 18:31:42 +0000 | [diff] [blame] | 71 | return true; |
commit-bot@chromium.org | 1a4fb70 | 2013-09-26 16:09:28 +0000 | [diff] [blame] | 72 | } |
senorblanco | a9fbd16 | 2016-01-11 14:09:09 -0800 | [diff] [blame] | 73 | if (srcBounds.contains(srcIRect)) { |
| 74 | if (!source.extractSubset(&subset, srcIRect)) { |
| 75 | return false; |
| 76 | } |
| 77 | } else { |
| 78 | SkAutoTUnref<SkBaseDevice> device(proxy->createDevice(srcIRect.width(), |
| 79 | srcIRect.height(), |
| 80 | kPossible_TileUsage)); |
| 81 | if (!device) { |
| 82 | return false; |
| 83 | } |
| 84 | SkCanvas canvas(device); |
| 85 | canvas.drawBitmap(src, SkIntToScalar(srcOffset.x()), |
| 86 | SkIntToScalar(srcOffset.y())); |
| 87 | subset = device->accessBitmap(false); |
| 88 | } |
| 89 | SkASSERT(subset.width() == srcIRect.width()); |
| 90 | SkASSERT(subset.height() == srcIRect.height()); |
commit-bot@chromium.org | 1a4fb70 | 2013-09-26 16:09:28 +0000 | [diff] [blame] | 91 | |
robertphillips | d312dca | 2015-07-06 07:59:09 -0700 | [diff] [blame] | 92 | SkAutoTUnref<SkBaseDevice> device(proxy->createDevice(w, h)); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 93 | if (nullptr == device.get()) { |
commit-bot@chromium.org | cd3b15c | 2013-12-04 17:06:49 +0000 | [diff] [blame] | 94 | return false; |
| 95 | } |
commit-bot@chromium.org | 1a4fb70 | 2013-09-26 16:09:28 +0000 | [diff] [blame] | 96 | SkCanvas canvas(device); |
| 97 | SkPaint paint; |
| 98 | paint.setXfermodeMode(SkXfermode::kSrc_Mode); |
| 99 | |
commit-bot@chromium.org | 9c9005a | 2014-04-28 14:55:39 +0000 | [diff] [blame] | 100 | SkAutoTUnref<SkShader> shader(SkShader::CreateBitmapShader(subset, |
senorblanco | a9fbd16 | 2016-01-11 14:09:09 -0800 | [diff] [blame] | 101 | SkShader::kRepeat_TileMode, SkShader::kRepeat_TileMode)); |
commit-bot@chromium.org | 1a4fb70 | 2013-09-26 16:09:28 +0000 | [diff] [blame] | 102 | paint.setShader(shader); |
senorblanco@chromium.org | 6776b82 | 2014-01-03 21:48:22 +0000 | [diff] [blame] | 103 | canvas.translate(-dstRect.fLeft, -dstRect.fTop); |
commit-bot@chromium.org | 1a4fb70 | 2013-09-26 16:09:28 +0000 | [diff] [blame] | 104 | canvas.drawRect(dstRect, paint); |
| 105 | *dst = device->accessBitmap(false); |
senorblanco@chromium.org | 6776b82 | 2014-01-03 21:48:22 +0000 | [diff] [blame] | 106 | offset->fX = dstIRect.fLeft; |
| 107 | offset->fY = dstIRect.fTop; |
commit-bot@chromium.org | 1a4fb70 | 2013-09-26 16:09:28 +0000 | [diff] [blame] | 108 | return true; |
| 109 | } |
| 110 | |
senorblanco | db64af3 | 2015-12-09 10:11:43 -0800 | [diff] [blame] | 111 | void SkTileImageFilter::onFilterNodeBounds(const SkIRect& src, const SkMatrix& ctm, |
| 112 | SkIRect* dst, MapDirection direction) const { |
| 113 | SkRect rect = kReverse_MapDirection == direction ? fSrcRect : fDstRect; |
| 114 | ctm.mapRect(&rect); |
| 115 | rect.roundOut(dst); |
senorblanco | db64af3 | 2015-12-09 10:11:43 -0800 | [diff] [blame] | 116 | } |
| 117 | |
senorblanco@chromium.org | 0a5c233 | 2014-04-29 15:20:39 +0000 | [diff] [blame] | 118 | bool SkTileImageFilter::onFilterBounds(const SkIRect& src, const SkMatrix& ctm, |
senorblanco | d8ff5b3 | 2016-01-28 08:23:02 -0800 | [diff] [blame] | 119 | SkIRect* dst, MapDirection direction) const { |
| 120 | // Don't recurse into inputs. |
| 121 | *dst = src; |
senorblanco@chromium.org | 0a5c233 | 2014-04-29 15:20:39 +0000 | [diff] [blame] | 122 | return true; |
| 123 | } |
| 124 | |
robertphillips | c3176aa | 2015-06-16 09:44:56 -0700 | [diff] [blame] | 125 | void SkTileImageFilter::computeFastBounds(const SkRect& src, SkRect* dst) const { |
senorblanco | db64af3 | 2015-12-09 10:11:43 -0800 | [diff] [blame] | 126 | *dst = fDstRect; |
robertphillips | c3176aa | 2015-06-16 09:44:56 -0700 | [diff] [blame] | 127 | } |
| 128 | |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 129 | SkFlattenable* SkTileImageFilter::CreateProc(SkReadBuffer& buffer) { |
| 130 | SK_IMAGEFILTER_UNFLATTEN_COMMON(common, 1); |
| 131 | SkRect src, dst; |
| 132 | buffer.readRect(&src); |
| 133 | buffer.readRect(&dst); |
senorblanco | 24e06d5 | 2015-03-18 12:11:33 -0700 | [diff] [blame] | 134 | return Create(src, dst, common.getInput(0)); |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 135 | } |
commit-bot@chromium.org | 1a4fb70 | 2013-09-26 16:09:28 +0000 | [diff] [blame] | 136 | |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 137 | void SkTileImageFilter::flatten(SkWriteBuffer& buffer) const { |
commit-bot@chromium.org | 1a4fb70 | 2013-09-26 16:09:28 +0000 | [diff] [blame] | 138 | this->INHERITED::flatten(buffer); |
| 139 | buffer.writeRect(fSrcRect); |
| 140 | buffer.writeRect(fDstRect); |
| 141 | } |
robertphillips | f3f5bad | 2014-12-19 13:49:15 -0800 | [diff] [blame] | 142 | |
| 143 | #ifndef SK_IGNORE_TO_STRING |
| 144 | void SkTileImageFilter::toString(SkString* str) const { |
| 145 | str->appendf("SkTileImageFilter: ("); |
robertphillips | 6319518 | 2015-06-08 06:21:14 -0700 | [diff] [blame] | 146 | str->appendf("src: %.2f %.2f %.2f %.2f", |
| 147 | fSrcRect.fLeft, fSrcRect.fTop, fSrcRect.fRight, fSrcRect.fBottom); |
| 148 | str->appendf(" dst: %.2f %.2f %.2f %.2f", |
| 149 | fDstRect.fLeft, fDstRect.fTop, fDstRect.fRight, fDstRect.fBottom); |
| 150 | if (this->getInput(0)) { |
robertphillips | d312dca | 2015-07-06 07:59:09 -0700 | [diff] [blame] | 151 | str->appendf("input: ("); |
robertphillips | 6319518 | 2015-06-08 06:21:14 -0700 | [diff] [blame] | 152 | this->getInput(0)->toString(str); |
| 153 | str->appendf(")"); |
| 154 | } |
robertphillips | f3f5bad | 2014-12-19 13:49:15 -0800 | [diff] [blame] | 155 | str->append(")"); |
| 156 | } |
| 157 | #endif |