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" |
robertphillips | c14b978 | 2016-04-11 13:26:14 -0700 | [diff] [blame] | 9 | |
commit-bot@chromium.org | 1a4fb70 | 2013-09-26 16:09:28 +0000 | [diff] [blame] | 10 | #include "SkCanvas.h" |
robertphillips | c14b978 | 2016-04-11 13:26:14 -0700 | [diff] [blame] | 11 | #include "SkImage.h" |
commit-bot@chromium.org | 1a4fb70 | 2013-09-26 16:09:28 +0000 | [diff] [blame] | 12 | #include "SkMatrix.h" |
robertphillips | c14b978 | 2016-04-11 13:26:14 -0700 | [diff] [blame] | 13 | #include "SkOffsetImageFilter.h" |
commit-bot@chromium.org | 1a4fb70 | 2013-09-26 16:09:28 +0000 | [diff] [blame] | 14 | #include "SkPaint.h" |
robertphillips | c14b978 | 2016-04-11 13:26:14 -0700 | [diff] [blame] | 15 | #include "SkReadBuffer.h" |
commit-bot@chromium.org | 1a4fb70 | 2013-09-26 16:09:28 +0000 | [diff] [blame] | 16 | #include "SkShader.h" |
robertphillips | c14b978 | 2016-04-11 13:26:14 -0700 | [diff] [blame] | 17 | #include "SkSpecialImage.h" |
| 18 | #include "SkSpecialSurface.h" |
| 19 | #include "SkSurface.h" |
commit-bot@chromium.org | c0b7e10 | 2013-10-23 17:06:21 +0000 | [diff] [blame] | 20 | #include "SkValidationUtils.h" |
robertphillips | c14b978 | 2016-04-11 13:26:14 -0700 | [diff] [blame] | 21 | #include "SkWriteBuffer.h" |
commit-bot@chromium.org | 1a4fb70 | 2013-09-26 16:09:28 +0000 | [diff] [blame] | 22 | |
robertphillips | 534c270 | 2016-04-15 07:57:40 -0700 | [diff] [blame] | 23 | sk_sp<SkImageFilter> SkTileImageFilter::Make(const SkRect& srcRect, const SkRect& dstRect, |
| 24 | sk_sp<SkImageFilter> input) { |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 25 | if (!SkIsValidRect(srcRect) || !SkIsValidRect(dstRect)) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 26 | return nullptr; |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 27 | } |
senorblanco | 05dcb4c | 2016-01-12 07:49:15 -0800 | [diff] [blame] | 28 | if (srcRect.width() == dstRect.width() && srcRect.height() == dstRect.height()) { |
| 29 | SkRect ir = dstRect; |
| 30 | if (!ir.intersect(srcRect)) { |
robertphillips | 534c270 | 2016-04-15 07:57:40 -0700 | [diff] [blame] | 31 | return input; |
senorblanco | 05dcb4c | 2016-01-12 07:49:15 -0800 | [diff] [blame] | 32 | } |
| 33 | CropRect cropRect(ir); |
robertphillips | 51a315e | 2016-03-31 09:05:49 -0700 | [diff] [blame] | 34 | return SkOffsetImageFilter::Make(dstRect.x() - srcRect.x(), |
| 35 | dstRect.y() - srcRect.y(), |
robertphillips | 534c270 | 2016-04-15 07:57:40 -0700 | [diff] [blame] | 36 | std::move(input), |
| 37 | &cropRect); |
senorblanco | 05dcb4c | 2016-01-12 07:49:15 -0800 | [diff] [blame] | 38 | } |
robertphillips | 534c270 | 2016-04-15 07:57:40 -0700 | [diff] [blame] | 39 | return sk_sp<SkImageFilter>(new SkTileImageFilter(srcRect, dstRect, std::move(input))); |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 40 | } |
| 41 | |
robertphillips | c14b978 | 2016-04-11 13:26:14 -0700 | [diff] [blame] | 42 | sk_sp<SkSpecialImage> SkTileImageFilter::onFilterImage(SkSpecialImage* source, |
| 43 | const Context& ctx, |
| 44 | SkIPoint* offset) const { |
| 45 | SkIPoint inputOffset = SkIPoint::Make(0, 0); |
| 46 | sk_sp<SkSpecialImage> input(this->filterInput(0, source, ctx, &inputOffset)); |
| 47 | if (!input) { |
| 48 | return nullptr; |
robertphillips | d312dca | 2015-07-06 07:59:09 -0700 | [diff] [blame] | 49 | } |
commit-bot@chromium.org | 1a4fb70 | 2013-09-26 16:09:28 +0000 | [diff] [blame] | 50 | |
senorblanco@chromium.org | e93e1db | 2013-12-09 18:31:42 +0000 | [diff] [blame] | 51 | SkRect dstRect; |
senorblanco@chromium.org | 4cb543d | 2014-03-14 15:44:01 +0000 | [diff] [blame] | 52 | ctx.ctm().mapRect(&dstRect, fDstRect); |
senorblanco | b50b97d | 2016-01-06 09:46:24 -0800 | [diff] [blame] | 53 | if (!dstRect.intersect(SkRect::Make(ctx.clipBounds()))) { |
robertphillips | c14b978 | 2016-04-11 13:26:14 -0700 | [diff] [blame] | 54 | return nullptr; |
senorblanco | b50b97d | 2016-01-06 09:46:24 -0800 | [diff] [blame] | 55 | } |
robertphillips | c14b978 | 2016-04-11 13:26:14 -0700 | [diff] [blame] | 56 | |
reed | b07a94f | 2014-11-19 05:03:18 -0800 | [diff] [blame] | 57 | const SkIRect dstIRect = dstRect.roundOut(); |
brianosman | eed6b0e | 2016-09-23 13:04:05 -0700 | [diff] [blame] | 58 | if (!fSrcRect.width() || !fSrcRect.height() || !dstIRect.width() || !dstIRect.height()) { |
robertphillips | c14b978 | 2016-04-11 13:26:14 -0700 | [diff] [blame] | 59 | return nullptr; |
commit-bot@chromium.org | 1a4fb70 | 2013-09-26 16:09:28 +0000 | [diff] [blame] | 60 | } |
| 61 | |
senorblanco@chromium.org | e93e1db | 2013-12-09 18:31:42 +0000 | [diff] [blame] | 62 | SkRect srcRect; |
senorblanco@chromium.org | 4cb543d | 2014-03-14 15:44:01 +0000 | [diff] [blame] | 63 | ctx.ctm().mapRect(&srcRect, fSrcRect); |
senorblanco@chromium.org | e93e1db | 2013-12-09 18:31:42 +0000 | [diff] [blame] | 64 | SkIRect srcIRect; |
| 65 | srcRect.roundOut(&srcIRect); |
robertphillips | c14b978 | 2016-04-11 13:26:14 -0700 | [diff] [blame] | 66 | srcIRect.offset(-inputOffset); |
| 67 | const SkIRect inputBounds = SkIRect::MakeWH(input->width(), input->height()); |
senorblanco@chromium.org | 6776b82 | 2014-01-03 21:48:22 +0000 | [diff] [blame] | 68 | |
robertphillips | c14b978 | 2016-04-11 13:26:14 -0700 | [diff] [blame] | 69 | if (!SkIRect::Intersects(srcIRect, inputBounds)) { |
| 70 | return nullptr; |
commit-bot@chromium.org | 1a4fb70 | 2013-09-26 16:09:28 +0000 | [diff] [blame] | 71 | } |
robertphillips | c14b978 | 2016-04-11 13:26:14 -0700 | [diff] [blame] | 72 | |
| 73 | // We create an SkImage here b.c. it needs to be a tight fit for the tiling |
| 74 | sk_sp<SkImage> subset; |
| 75 | if (inputBounds.contains(srcIRect)) { |
Robert Phillips | a5fdc97 | 2017-02-18 16:58:09 -0500 | [diff] [blame] | 76 | subset = input->asImage(&srcIRect); |
robertphillips | c14b978 | 2016-04-11 13:26:14 -0700 | [diff] [blame] | 77 | if (!subset) { |
| 78 | return nullptr; |
senorblanco | a9fbd16 | 2016-01-11 14:09:09 -0800 | [diff] [blame] | 79 | } |
| 80 | } else { |
brianosman | eed6b0e | 2016-09-23 13:04:05 -0700 | [diff] [blame] | 81 | sk_sp<SkSurface> surf(input->makeTightSurface(ctx.outputProperties(), srcIRect.size())); |
robertphillips | c14b978 | 2016-04-11 13:26:14 -0700 | [diff] [blame] | 82 | if (!surf) { |
| 83 | return nullptr; |
senorblanco | a9fbd16 | 2016-01-11 14:09:09 -0800 | [diff] [blame] | 84 | } |
commit-bot@chromium.org | 1a4fb70 | 2013-09-26 16:09:28 +0000 | [diff] [blame] | 85 | |
robertphillips | c14b978 | 2016-04-11 13:26:14 -0700 | [diff] [blame] | 86 | SkCanvas* canvas = surf->getCanvas(); |
| 87 | SkASSERT(canvas); |
| 88 | |
| 89 | SkPaint paint; |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 90 | paint.setBlendMode(SkBlendMode::kSrc); |
robertphillips | c14b978 | 2016-04-11 13:26:14 -0700 | [diff] [blame] | 91 | |
| 92 | input->draw(canvas, |
| 93 | SkIntToScalar(inputOffset.x()), SkIntToScalar(inputOffset.y()), |
| 94 | &paint); |
| 95 | |
| 96 | subset = surf->makeImageSnapshot(); |
commit-bot@chromium.org | cd3b15c | 2013-12-04 17:06:49 +0000 | [diff] [blame] | 97 | } |
robertphillips | c14b978 | 2016-04-11 13:26:14 -0700 | [diff] [blame] | 98 | SkASSERT(subset->width() == srcIRect.width()); |
| 99 | SkASSERT(subset->height() == srcIRect.height()); |
| 100 | |
brianosman | eed6b0e | 2016-09-23 13:04:05 -0700 | [diff] [blame] | 101 | sk_sp<SkSpecialSurface> surf(source->makeSurface(ctx.outputProperties(), dstIRect.size())); |
robertphillips | c14b978 | 2016-04-11 13:26:14 -0700 | [diff] [blame] | 102 | if (!surf) { |
| 103 | return nullptr; |
| 104 | } |
| 105 | |
| 106 | SkCanvas* canvas = surf->getCanvas(); |
| 107 | SkASSERT(canvas); |
| 108 | |
commit-bot@chromium.org | 1a4fb70 | 2013-09-26 16:09:28 +0000 | [diff] [blame] | 109 | SkPaint paint; |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 110 | paint.setBlendMode(SkBlendMode::kSrc); |
robertphillips | c14b978 | 2016-04-11 13:26:14 -0700 | [diff] [blame] | 111 | paint.setShader(subset->makeShader(SkShader::kRepeat_TileMode, SkShader::kRepeat_TileMode)); |
| 112 | canvas->translate(-dstRect.fLeft, -dstRect.fTop); |
| 113 | canvas->drawRect(dstRect, paint); |
senorblanco@chromium.org | 6776b82 | 2014-01-03 21:48:22 +0000 | [diff] [blame] | 114 | offset->fX = dstIRect.fLeft; |
| 115 | offset->fY = dstIRect.fTop; |
robertphillips | c14b978 | 2016-04-11 13:26:14 -0700 | [diff] [blame] | 116 | return surf->makeImageSnapshot(); |
commit-bot@chromium.org | 1a4fb70 | 2013-09-26 16:09:28 +0000 | [diff] [blame] | 117 | } |
| 118 | |
Matt Sarett | 6d72ed9 | 2017-04-10 16:35:33 -0400 | [diff] [blame^] | 119 | sk_sp<SkImageFilter> SkTileImageFilter::onMakeColorSpace(SkColorSpaceXformer* xformer) const { |
| 120 | SkASSERT(1 == this->countInputs()); |
| 121 | if (!this->getInput(0)) { |
| 122 | return sk_ref_sp(const_cast<SkTileImageFilter*>(this)); |
| 123 | } |
| 124 | |
| 125 | sk_sp<SkImageFilter> input = this->getInput(0)->makeColorSpace(xformer); |
| 126 | return SkTileImageFilter::Make(fSrcRect, fDstRect, std::move(input)); |
| 127 | } |
| 128 | |
senorblanco | e5e7984 | 2016-03-21 14:51:59 -0700 | [diff] [blame] | 129 | SkIRect SkTileImageFilter::onFilterNodeBounds(const SkIRect& src, const SkMatrix& ctm, |
| 130 | MapDirection direction) const { |
senorblanco | db64af3 | 2015-12-09 10:11:43 -0800 | [diff] [blame] | 131 | SkRect rect = kReverse_MapDirection == direction ? fSrcRect : fDstRect; |
| 132 | ctm.mapRect(&rect); |
senorblanco | e5e7984 | 2016-03-21 14:51:59 -0700 | [diff] [blame] | 133 | return rect.roundOut(); |
senorblanco | db64af3 | 2015-12-09 10:11:43 -0800 | [diff] [blame] | 134 | } |
| 135 | |
senorblanco | e5e7984 | 2016-03-21 14:51:59 -0700 | [diff] [blame] | 136 | SkIRect SkTileImageFilter::onFilterBounds(const SkIRect& src, const SkMatrix&, MapDirection) const { |
senorblanco | d8ff5b3 | 2016-01-28 08:23:02 -0800 | [diff] [blame] | 137 | // Don't recurse into inputs. |
senorblanco | e5e7984 | 2016-03-21 14:51:59 -0700 | [diff] [blame] | 138 | return src; |
senorblanco@chromium.org | 0a5c233 | 2014-04-29 15:20:39 +0000 | [diff] [blame] | 139 | } |
| 140 | |
senorblanco | e5e7984 | 2016-03-21 14:51:59 -0700 | [diff] [blame] | 141 | SkRect SkTileImageFilter::computeFastBounds(const SkRect& src) const { |
| 142 | return fDstRect; |
robertphillips | c3176aa | 2015-06-16 09:44:56 -0700 | [diff] [blame] | 143 | } |
| 144 | |
reed | 60c9b58 | 2016-04-03 09:11:13 -0700 | [diff] [blame] | 145 | sk_sp<SkFlattenable> SkTileImageFilter::CreateProc(SkReadBuffer& buffer) { |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 146 | SK_IMAGEFILTER_UNFLATTEN_COMMON(common, 1); |
| 147 | SkRect src, dst; |
| 148 | buffer.readRect(&src); |
| 149 | buffer.readRect(&dst); |
robertphillips | 534c270 | 2016-04-15 07:57:40 -0700 | [diff] [blame] | 150 | return Make(src, dst, common.getInput(0)); |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 151 | } |
commit-bot@chromium.org | 1a4fb70 | 2013-09-26 16:09:28 +0000 | [diff] [blame] | 152 | |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 153 | void SkTileImageFilter::flatten(SkWriteBuffer& buffer) const { |
commit-bot@chromium.org | 1a4fb70 | 2013-09-26 16:09:28 +0000 | [diff] [blame] | 154 | this->INHERITED::flatten(buffer); |
| 155 | buffer.writeRect(fSrcRect); |
| 156 | buffer.writeRect(fDstRect); |
| 157 | } |
robertphillips | f3f5bad | 2014-12-19 13:49:15 -0800 | [diff] [blame] | 158 | |
| 159 | #ifndef SK_IGNORE_TO_STRING |
| 160 | void SkTileImageFilter::toString(SkString* str) const { |
| 161 | str->appendf("SkTileImageFilter: ("); |
robertphillips | 6319518 | 2015-06-08 06:21:14 -0700 | [diff] [blame] | 162 | str->appendf("src: %.2f %.2f %.2f %.2f", |
| 163 | fSrcRect.fLeft, fSrcRect.fTop, fSrcRect.fRight, fSrcRect.fBottom); |
| 164 | str->appendf(" dst: %.2f %.2f %.2f %.2f", |
| 165 | fDstRect.fLeft, fDstRect.fTop, fDstRect.fRight, fDstRect.fBottom); |
| 166 | if (this->getInput(0)) { |
robertphillips | d312dca | 2015-07-06 07:59:09 -0700 | [diff] [blame] | 167 | str->appendf("input: ("); |
robertphillips | 6319518 | 2015-06-08 06:21:14 -0700 | [diff] [blame] | 168 | this->getInput(0)->toString(str); |
| 169 | str->appendf(")"); |
| 170 | } |
robertphillips | f3f5bad | 2014-12-19 13:49:15 -0800 | [diff] [blame] | 171 | str->append(")"); |
| 172 | } |
| 173 | #endif |