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