blob: 255ab7686e6a591e72e2c4e094de441a6c13de66 [file] [log] [blame]
senorblanco20311d42015-10-14 04:53:31 -07001/*
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 Reed6d9f4292017-07-06 12:32:55 -04008#include "SkColorSpaceXformer.h"
senorblanco20311d42015-10-14 04:53:31 -07009#include "SkLocalMatrixImageFilter.h"
10#include "SkReadBuffer.h"
robertphillips2302de92016-03-24 07:26:32 -070011#include "SkSpecialImage.h"
senorblanco20311d42015-10-14 04:53:31 -070012#include "SkString.h"
13
robertphillips225db442016-04-17 14:27:05 -070014sk_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
robertphillips372177e2016-03-30 07:32:28 -070028SkLocalMatrixImageFilter::SkLocalMatrixImageFilter(const SkMatrix& localM,
29 sk_sp<SkImageFilter> input)
30 : INHERITED(&input, 1, nullptr)
robertphillipsae2f2de2016-03-23 04:40:01 -070031 , fLocalM(localM) {
32}
senorblanco20311d42015-10-14 04:53:31 -070033
reed60c9b582016-04-03 09:11:13 -070034sk_sp<SkFlattenable> SkLocalMatrixImageFilter::CreateProc(SkReadBuffer& buffer) {
senorblanco20311d42015-10-14 04:53:31 -070035 SK_IMAGEFILTER_UNFLATTEN_COMMON(common, 1);
36 SkMatrix lm;
37 buffer.readMatrix(&lm);
reed60c9b582016-04-03 09:11:13 -070038 return SkLocalMatrixImageFilter::Make(lm, common.getInput(0));
senorblanco20311d42015-10-14 04:53:31 -070039}
40
41void SkLocalMatrixImageFilter::flatten(SkWriteBuffer& buffer) const {
42 this->INHERITED::flatten(buffer);
43 buffer.writeMatrix(fLocalM);
44}
45
robertphillips2302de92016-03-24 07:26:32 -070046sk_sp<SkSpecialImage> SkLocalMatrixImageFilter::onFilterImage(SkSpecialImage* source,
47 const Context& ctx,
48 SkIPoint* offset) const {
brianosman2a75e5d2016-09-22 07:15:37 -070049 Context localCtx(SkMatrix::Concat(ctx.ctm(), fLocalM), ctx.clipBounds(), ctx.cache(),
50 ctx.outputProperties());
robertphillipsae2f2de2016-03-23 04:40:01 -070051 return this->filterInput(0, source, localCtx, offset);
senorblanco20311d42015-10-14 04:53:31 -070052}
53
senorblancoe5e79842016-03-21 14:51:59 -070054SkIRect SkLocalMatrixImageFilter::onFilterBounds(const SkIRect& src, const SkMatrix& matrix,
55 MapDirection direction) const {
56 return this->getInput(0)->filterBounds(src, SkMatrix::Concat(matrix, fLocalM), direction);
senorblanco20311d42015-10-14 04:53:31 -070057}
58
Matt Sarett62745a82017-04-17 11:57:29 -040059sk_sp<SkImageFilter> SkLocalMatrixImageFilter::onMakeColorSpace(SkColorSpaceXformer* xformer)
60const {
61 SkASSERT(1 == this->countInputs() && this->getInput(0));
62
Mike Reed6d9f4292017-07-06 12:32:55 -040063 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 Sarett62745a82017-04-17 11:57:29 -040068}
69
senorblanco20311d42015-10-14 04:53:31 -070070#ifndef SK_IGNORE_TO_STRING
71void SkLocalMatrixImageFilter::toString(SkString* str) const {
72 str->append("SkLocalMatrixImageFilter: (");
73 str->append(")");
74}
75#endif