Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / third_party / skia / src / core / SkLocalMatrixShader.cpp
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
10 SkLocalMatrixShader::SkLocalMatrixShader(SkReadBuffer& buffer) : INHERITED(buffer) {
11     if (buffer.isVersionLT(SkReadBuffer::kSimplifyLocalMatrix_Version)) {
12         buffer.readMatrix(&(INHERITED::fLocalMatrix));
13     }
14     fProxyShader.reset(buffer.readShader());
15     if (NULL == fProxyShader.get()) {
16         sk_throw();
17     }
18 }
19
20 void SkLocalMatrixShader::flatten(SkWriteBuffer& buffer) const {
21     this->INHERITED::flatten(buffer);
22     buffer.writeFlattenable(fProxyShader.get());
23 }
24
25 SkShader::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
39 void 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
50 SkShader* SkShader::CreateLocalMatrixShader(SkShader* proxy, const SkMatrix& localMatrix) {
51     if (localMatrix.isIdentity()) {
52         return SkRef(proxy);
53     }
54
55     const SkMatrix* lm = &localMatrix;
56
57     SkMatrix otherLocalMatrix;
58     SkAutoTUnref<SkShader> otherProxy(proxy->refAsALocalMatrixShader(&otherLocalMatrix));
59     if (otherProxy.get()) {
60         otherLocalMatrix.preConcat(localMatrix);
61         lm = &otherLocalMatrix;
62         proxy = otherProxy.get();
63     }
64
65     return SkNEW_ARGS(SkLocalMatrixShader, (proxy, *lm));
66 }