| 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 | |
| 8 | #include "include/core/SkString.h" |
| 9 | #include "src/core/SkLocalMatrixImageFilter.h" |
| 10 | #include "src/core/SkReadBuffer.h" |
| 11 | #include "src/core/SkSpecialImage.h" |
| 12 | |
| 13 | sk_sp<SkImageFilter> SkLocalMatrixImageFilter::Make(const SkMatrix& localM, |
| 14 | sk_sp<SkImageFilter> input) { |
| 15 | if (!input) { |
| 16 | return nullptr; |
| 17 | } |
| 18 | if (localM.isIdentity()) { |
| 19 | return input; |
| 20 | } |
| 21 | if (!as_IFB(input)->canHandleComplexCTM() && !localM.isScaleTranslate()) { |
| 22 | // Nothing we can do at this point |
| 23 | return nullptr; |
| 24 | } |
| 25 | return sk_sp<SkImageFilter>(new SkLocalMatrixImageFilter(localM, input)); |
| 26 | } |
| 27 | |
| 28 | SkLocalMatrixImageFilter::SkLocalMatrixImageFilter(const SkMatrix& localM, |
| 29 | sk_sp<SkImageFilter> input) |
| 30 | : INHERITED(&input, 1, nullptr) |
| 31 | , fLocalM(localM) { |
| 32 | } |
| 33 | |
| 34 | sk_sp<SkFlattenable> SkLocalMatrixImageFilter::CreateProc(SkReadBuffer& buffer) { |
| 35 | SK_IMAGEFILTER_UNFLATTEN_COMMON(common, 1); |
| 36 | SkMatrix lm; |
| 37 | buffer.readMatrix(&lm); |
| 38 | return SkLocalMatrixImageFilter::Make(lm, common.getInput(0)); |
| 39 | } |
| 40 | |
| 41 | void SkLocalMatrixImageFilter::flatten(SkWriteBuffer& buffer) const { |
| 42 | this->INHERITED::flatten(buffer); |
| 43 | buffer.writeMatrix(fLocalM); |
| 44 | } |
| 45 | |
| 46 | sk_sp<SkSpecialImage> SkLocalMatrixImageFilter::onFilterImage(const Context& ctx, |
| 47 | SkIPoint* offset) const { |
| 48 | Context localCtx = ctx.withNewMapping(ctx.mapping().concatLocal(fLocalM)); |
| 49 | return this->filterInput(0, localCtx, offset); |
| 50 | } |
| 51 | |
| 52 | SkIRect SkLocalMatrixImageFilter::onFilterBounds(const SkIRect& src, const SkMatrix& ctm, |
| 53 | MapDirection dir, const SkIRect* inputRect) const { |
| 54 | return this->getInput(0)->filterBounds(src, SkMatrix::Concat(ctm, fLocalM), dir, inputRect); |
| 55 | } |
| 56 | |