senorblanco | 20311d4 | 2015-10-14 04:53:31 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 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 | |
Mike Reed | 6d9f429 | 2017-07-06 12:32:55 -0400 | [diff] [blame] | 8 | #include "SkColorSpaceXformer.h" |
senorblanco | 20311d4 | 2015-10-14 04:53:31 -0700 | [diff] [blame] | 9 | #include "SkLocalMatrixImageFilter.h" |
| 10 | #include "SkReadBuffer.h" |
robertphillips | 2302de9 | 2016-03-24 07:26:32 -0700 | [diff] [blame] | 11 | #include "SkSpecialImage.h" |
senorblanco | 20311d4 | 2015-10-14 04:53:31 -0700 | [diff] [blame] | 12 | #include "SkString.h" |
| 13 | |
robertphillips | 225db44 | 2016-04-17 14:27:05 -0700 | [diff] [blame] | 14 | sk_sp<SkImageFilter> SkLocalMatrixImageFilter::Make(const SkMatrix& localM, |
| 15 | sk_sp<SkImageFilter> input) { |
| 16 | if (!input) { |
| 17 | return nullptr; |
| 18 | } |
| 19 | if (localM.getType() & (SkMatrix::kAffine_Mask | SkMatrix::kPerspective_Mask)) { |
| 20 | return nullptr; |
| 21 | } |
| 22 | if (localM.isIdentity()) { |
| 23 | return input; |
| 24 | } |
| 25 | return sk_sp<SkImageFilter>(new SkLocalMatrixImageFilter(localM, input)); |
| 26 | } |
| 27 | |
robertphillips | 372177e | 2016-03-30 07:32:28 -0700 | [diff] [blame] | 28 | SkLocalMatrixImageFilter::SkLocalMatrixImageFilter(const SkMatrix& localM, |
| 29 | sk_sp<SkImageFilter> input) |
| 30 | : INHERITED(&input, 1, nullptr) |
robertphillips | ae2f2de | 2016-03-23 04:40:01 -0700 | [diff] [blame] | 31 | , fLocalM(localM) { |
| 32 | } |
senorblanco | 20311d4 | 2015-10-14 04:53:31 -0700 | [diff] [blame] | 33 | |
reed | 60c9b58 | 2016-04-03 09:11:13 -0700 | [diff] [blame] | 34 | sk_sp<SkFlattenable> SkLocalMatrixImageFilter::CreateProc(SkReadBuffer& buffer) { |
senorblanco | 20311d4 | 2015-10-14 04:53:31 -0700 | [diff] [blame] | 35 | SK_IMAGEFILTER_UNFLATTEN_COMMON(common, 1); |
| 36 | SkMatrix lm; |
| 37 | buffer.readMatrix(&lm); |
reed | 60c9b58 | 2016-04-03 09:11:13 -0700 | [diff] [blame] | 38 | return SkLocalMatrixImageFilter::Make(lm, common.getInput(0)); |
senorblanco | 20311d4 | 2015-10-14 04:53:31 -0700 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | void SkLocalMatrixImageFilter::flatten(SkWriteBuffer& buffer) const { |
| 42 | this->INHERITED::flatten(buffer); |
| 43 | buffer.writeMatrix(fLocalM); |
| 44 | } |
| 45 | |
robertphillips | 2302de9 | 2016-03-24 07:26:32 -0700 | [diff] [blame] | 46 | sk_sp<SkSpecialImage> SkLocalMatrixImageFilter::onFilterImage(SkSpecialImage* source, |
| 47 | const Context& ctx, |
| 48 | SkIPoint* offset) const { |
brianosman | 2a75e5d | 2016-09-22 07:15:37 -0700 | [diff] [blame] | 49 | Context localCtx(SkMatrix::Concat(ctx.ctm(), fLocalM), ctx.clipBounds(), ctx.cache(), |
| 50 | ctx.outputProperties()); |
robertphillips | ae2f2de | 2016-03-23 04:40:01 -0700 | [diff] [blame] | 51 | return this->filterInput(0, source, localCtx, offset); |
senorblanco | 20311d4 | 2015-10-14 04:53:31 -0700 | [diff] [blame] | 52 | } |
| 53 | |
senorblanco | e5e7984 | 2016-03-21 14:51:59 -0700 | [diff] [blame] | 54 | SkIRect SkLocalMatrixImageFilter::onFilterBounds(const SkIRect& src, const SkMatrix& matrix, |
| 55 | MapDirection direction) const { |
| 56 | return this->getInput(0)->filterBounds(src, SkMatrix::Concat(matrix, fLocalM), direction); |
senorblanco | 20311d4 | 2015-10-14 04:53:31 -0700 | [diff] [blame] | 57 | } |
| 58 | |
Matt Sarett | 62745a8 | 2017-04-17 11:57:29 -0400 | [diff] [blame] | 59 | sk_sp<SkImageFilter> SkLocalMatrixImageFilter::onMakeColorSpace(SkColorSpaceXformer* xformer) |
| 60 | const { |
| 61 | SkASSERT(1 == this->countInputs() && this->getInput(0)); |
| 62 | |
Mike Reed | 6d9f429 | 2017-07-06 12:32:55 -0400 | [diff] [blame] | 63 | auto input = xformer->apply(this->getInput(0)); |
| 64 | if (input.get() != this->getInput(0)) { |
| 65 | return SkLocalMatrixImageFilter::Make(fLocalM, std::move(input)); |
| 66 | } |
| 67 | return this->refMe(); |
Matt Sarett | 62745a8 | 2017-04-17 11:57:29 -0400 | [diff] [blame] | 68 | } |
| 69 | |
senorblanco | 20311d4 | 2015-10-14 04:53:31 -0700 | [diff] [blame] | 70 | #ifndef SK_IGNORE_TO_STRING |
| 71 | void SkLocalMatrixImageFilter::toString(SkString* str) const { |
| 72 | str->append("SkLocalMatrixImageFilter: ("); |
| 73 | str->append(")"); |
| 74 | } |
| 75 | #endif |