Plumb dst color space in many places, rather than "mode"
[platform/upstream/libSkiaSharp.git] / 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 #if SK_SUPPORT_GPU
11 #include "GrFragmentProcessor.h"
12 #endif
13
14 #if SK_SUPPORT_GPU
15 sk_sp<GrFragmentProcessor> SkLocalMatrixShader::asFragmentProcessor(const AsFPArgs& args) const {
16     SkMatrix tmp = this->getLocalMatrix();
17     if (args.fLocalMatrix) {
18         tmp.preConcat(*args.fLocalMatrix);
19     }
20     return fProxyShader->asFragmentProcessor(AsFPArgs(
21         args.fContext, args.fViewMatrix, &tmp, args.fFilterQuality, args.fDstColorSpace));
22 }
23 #endif
24
25 sk_sp<SkFlattenable> SkLocalMatrixShader::CreateProc(SkReadBuffer& buffer) {
26     SkMatrix lm;
27     buffer.readMatrix(&lm);
28     auto baseShader(buffer.readShader());
29     if (!baseShader) {
30         return nullptr;
31     }
32     return baseShader->makeWithLocalMatrix(lm);
33 }
34
35 void SkLocalMatrixShader::flatten(SkWriteBuffer& buffer) const {
36     buffer.writeMatrix(this->getLocalMatrix());
37     buffer.writeFlattenable(fProxyShader.get());
38 }
39
40 SkShader::Context* SkLocalMatrixShader::onCreateContext(const ContextRec& rec,
41                                                         void* storage) const {
42     ContextRec newRec(rec);
43     SkMatrix tmp;
44     if (rec.fLocalMatrix) {
45         tmp.setConcat(*rec.fLocalMatrix, this->getLocalMatrix());
46         newRec.fLocalMatrix = &tmp;
47     } else {
48         newRec.fLocalMatrix = &this->getLocalMatrix();
49     }
50     return fProxyShader->createContext(newRec, storage);
51 }
52
53 #ifndef SK_IGNORE_TO_STRING
54 void SkLocalMatrixShader::toString(SkString* str) const {
55     str->append("SkLocalMatrixShader: (");
56
57     fProxyShader->toString(str);
58
59     this->INHERITED::toString(str);
60
61     str->append(")");
62 }
63 #endif
64
65 sk_sp<SkShader> SkShader::makeWithLocalMatrix(const SkMatrix& localMatrix) const {
66     if (localMatrix.isIdentity()) {
67         return sk_ref_sp(const_cast<SkShader*>(this));
68     }
69
70     const SkMatrix* lm = &localMatrix;
71
72     sk_sp<SkShader> baseShader;
73     SkMatrix otherLocalMatrix;
74     sk_sp<SkShader> proxy(this->makeAsALocalMatrixShader(&otherLocalMatrix));
75     if (proxy) {
76         otherLocalMatrix.preConcat(localMatrix);
77         lm = &otherLocalMatrix;
78         baseShader = proxy;
79     } else {
80         baseShader = sk_ref_sp(const_cast<SkShader*>(this));
81     }
82
83     return sk_make_sp<SkLocalMatrixShader>(std::move(baseShader), *lm);
84 }