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