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 "SkLocalMatrixImageFilter.h"
9#include "SkReadBuffer.h"
10#include "SkSpecialImage.h"
11#include "SkString.h"
12
13sk_sp<SkImageFilter> SkLocalMatrixImageFilter::Make(const SkMatrix& localM,
14                                                    sk_sp<SkImageFilter> input) {
15    if (!input) {
16        return nullptr;
17    }
18    if (localM.getType() & (SkMatrix::kAffine_Mask | SkMatrix::kPerspective_Mask)) {
19        return nullptr;
20    }
21    if (localM.isIdentity()) {
22        return input;
23    }
24    return sk_sp<SkImageFilter>(new SkLocalMatrixImageFilter(localM, input));
25}
26
27SkLocalMatrixImageFilter::SkLocalMatrixImageFilter(const SkMatrix& localM,
28                                                   sk_sp<SkImageFilter> input)
29    : INHERITED(&input, 1, nullptr)
30    , fLocalM(localM) {
31}
32
33sk_sp<SkFlattenable> SkLocalMatrixImageFilter::CreateProc(SkReadBuffer& buffer) {
34    SK_IMAGEFILTER_UNFLATTEN_COMMON(common, 1);
35    SkMatrix lm;
36    buffer.readMatrix(&lm);
37    return SkLocalMatrixImageFilter::Make(lm, common.getInput(0));
38}
39
40void SkLocalMatrixImageFilter::flatten(SkWriteBuffer& buffer) const {
41    this->INHERITED::flatten(buffer);
42    buffer.writeMatrix(fLocalM);
43}
44
45sk_sp<SkSpecialImage> SkLocalMatrixImageFilter::onFilterImage(SkSpecialImage* source,
46                                                              const Context& ctx,
47                                                              SkIPoint* offset) const {
48    Context localCtx(SkMatrix::Concat(ctx.ctm(), fLocalM), ctx.clipBounds(), ctx.cache(),
49                     ctx.outputProperties());
50    return this->filterInput(0, source, localCtx, offset);
51}
52
53SkIRect SkLocalMatrixImageFilter::onFilterBounds(const SkIRect& src, const SkMatrix& matrix,
54                                                 MapDirection direction) const {
55    return this->getInput(0)->filterBounds(src, SkMatrix::Concat(matrix, fLocalM), direction);
56}
57
58#ifndef SK_IGNORE_TO_STRING
59void SkLocalMatrixImageFilter::toString(SkString* str) const {
60    str->append("SkLocalMatrixImageFilter: (");
61    str->append(")");
62}
63#endif
64