1/*
2 * Copyright 2014 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 "SkLocalMatrixShader.h"
9
10SkLocalMatrixShader::SkLocalMatrixShader(SkReadBuffer& buffer) : INHERITED(buffer) {
11    fProxyShader.reset(buffer.readShader());
12    if (NULL == fProxyShader.get()) {
13        sk_throw();
14    }
15}
16
17void SkLocalMatrixShader::flatten(SkWriteBuffer& buffer) const {
18    this->INHERITED::flatten(buffer);
19    buffer.writeFlattenable(fProxyShader.get());
20}
21
22SkShader::Context* SkLocalMatrixShader::onCreateContext(const ContextRec& rec,
23                                                        void* storage) const {
24    ContextRec newRec(rec);
25    SkMatrix tmp;
26    if (rec.fLocalMatrix) {
27        tmp.setConcat(this->getLocalMatrix(), *rec.fLocalMatrix);
28        newRec.fLocalMatrix = &tmp;
29    } else {
30        newRec.fLocalMatrix = &this->getLocalMatrix();
31    }
32    return fProxyShader->createContext(newRec, storage);
33}
34
35#ifndef SK_IGNORE_TO_STRING
36void SkLocalMatrixShader::toString(SkString* str) const {
37    str->append("SkLocalMatrixShader: (");
38
39    fProxyShader->toString(str);
40
41    this->INHERITED::toString(str);
42
43    str->append(")");
44}
45#endif
46
47SkShader* SkShader::CreateLocalMatrixShader(SkShader* proxy, const SkMatrix& localMatrix) {
48    if (localMatrix.isIdentity()) {
49        return SkRef(proxy);
50    }
51
52    const SkMatrix* lm = &localMatrix;
53
54    SkMatrix otherLocalMatrix;
55    SkAutoTUnref<SkShader> otherProxy(proxy->refAsALocalMatrixShader(&otherLocalMatrix));
56    if (otherProxy.get()) {
57        otherLocalMatrix.preConcat(localMatrix);
58        lm = &otherLocalMatrix;
59        proxy = otherProxy.get();
60    }
61
62    return SkNEW_ARGS(SkLocalMatrixShader, (proxy, *lm));
63}
64