2 * Copyright 2014 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
8 #include "SkLocalMatrixShader.h"
11 #include "GrFragmentProcessor.h"
15 sk_sp<GrFragmentProcessor> SkLocalMatrixShader::asFragmentProcessor(const AsFPArgs& args) const {
16 SkMatrix tmp = this->getLocalMatrix();
17 if (args.fLocalMatrix) {
18 tmp.preConcat(*args.fLocalMatrix);
20 return fProxyShader->asFragmentProcessor(AsFPArgs(
21 args.fContext, args.fViewMatrix, &tmp, args.fFilterQuality, args.fDstColorSpace));
25 sk_sp<SkFlattenable> SkLocalMatrixShader::CreateProc(SkReadBuffer& buffer) {
27 buffer.readMatrix(&lm);
28 auto baseShader(buffer.readShader());
32 return baseShader->makeWithLocalMatrix(lm);
35 void SkLocalMatrixShader::flatten(SkWriteBuffer& buffer) const {
36 buffer.writeMatrix(this->getLocalMatrix());
37 buffer.writeFlattenable(fProxyShader.get());
40 SkShader::Context* SkLocalMatrixShader::onCreateContext(const ContextRec& rec,
41 void* storage) const {
42 ContextRec newRec(rec);
44 if (rec.fLocalMatrix) {
45 tmp.setConcat(*rec.fLocalMatrix, this->getLocalMatrix());
46 newRec.fLocalMatrix = &tmp;
48 newRec.fLocalMatrix = &this->getLocalMatrix();
50 return fProxyShader->createContext(newRec, storage);
53 #ifndef SK_IGNORE_TO_STRING
54 void SkLocalMatrixShader::toString(SkString* str) const {
55 str->append("SkLocalMatrixShader: (");
57 fProxyShader->toString(str);
59 this->INHERITED::toString(str);
65 sk_sp<SkShader> SkShader::makeWithLocalMatrix(const SkMatrix& localMatrix) const {
66 if (localMatrix.isIdentity()) {
67 return sk_ref_sp(const_cast<SkShader*>(this));
70 const SkMatrix* lm = &localMatrix;
72 sk_sp<SkShader> baseShader;
73 SkMatrix otherLocalMatrix;
74 sk_sp<SkShader> proxy(this->makeAsALocalMatrixShader(&otherLocalMatrix));
76 otherLocalMatrix.preConcat(localMatrix);
77 lm = &otherLocalMatrix;
80 baseShader = sk_ref_sp(const_cast<SkShader*>(this));
83 return sk_make_sp<SkLocalMatrixShader>(std::move(baseShader), *lm);