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