blob: 377e6f62dfd383dca08cecd77a7b77f5dc5de06d [file] [log] [blame]
commit-bot@chromium.org8fae2132014-05-07 22:26:37 +00001/*
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
commit-bot@chromium.org5970f622014-05-12 20:42:21 +00008#include "SkLocalMatrixShader.h"
commit-bot@chromium.org8fae2132014-05-07 22:26:37 +00009
bungeman06ca8ec2016-06-09 08:01:03 -070010#if SK_SUPPORT_GPU
11#include "GrFragmentProcessor.h"
12#endif
13
14#if SK_SUPPORT_GPU
Brian Salomonaff329b2017-08-11 09:40:37 -040015std::unique_ptr<GrFragmentProcessor> SkLocalMatrixShader::asFragmentProcessor(
16 const AsFPArgs& args) const {
bungeman06ca8ec2016-06-09 08:01:03 -070017 SkMatrix tmp = this->getLocalMatrix();
brianosman839345d2016-07-22 11:04:53 -070018 if (args.fLocalMatrix) {
19 tmp.preConcat(*args.fLocalMatrix);
bungeman06ca8ec2016-06-09 08:01:03 -070020 }
Florin Malita4aed1382017-05-25 10:38:07 -040021 return as_SB(fProxyShader)->asFragmentProcessor(AsFPArgs(
Brian Osman61624f02016-12-09 14:51:59 -050022 args.fContext, args.fViewMatrix, &tmp, args.fFilterQuality, args.fDstColorSpace));
bungeman06ca8ec2016-06-09 08:01:03 -070023}
24#endif
25
reed60c9b582016-04-03 09:11:13 -070026sk_sp<SkFlattenable> SkLocalMatrixShader::CreateProc(SkReadBuffer& buffer) {
reed9fa60da2014-08-21 07:59:51 -070027 SkMatrix lm;
28 buffer.readMatrix(&lm);
reed8a21c9f2016-03-08 18:50:00 -080029 auto baseShader(buffer.readShader());
reedf880e452015-12-30 13:39:41 -080030 if (!baseShader) {
halcanary96fcdcc2015-08-27 07:41:13 -070031 return nullptr;
reed9fa60da2014-08-21 07:59:51 -070032 }
reed60c9b582016-04-03 09:11:13 -070033 return baseShader->makeWithLocalMatrix(lm);
reed9fa60da2014-08-21 07:59:51 -070034}
commit-bot@chromium.org8fae2132014-05-07 22:26:37 +000035
36void SkLocalMatrixShader::flatten(SkWriteBuffer& buffer) const {
reed9fa60da2014-08-21 07:59:51 -070037 buffer.writeMatrix(this->getLocalMatrix());
commit-bot@chromium.org8fae2132014-05-07 22:26:37 +000038 buffer.writeFlattenable(fProxyShader.get());
39}
40
Florin Malita4aed1382017-05-25 10:38:07 -040041SkShaderBase::Context* SkLocalMatrixShader::onMakeContext(
Herb Derby83e939b2017-02-07 14:25:11 -050042 const ContextRec& rec, SkArenaAlloc* alloc) const
43{
commit-bot@chromium.org8fae2132014-05-07 22:26:37 +000044 ContextRec newRec(rec);
45 SkMatrix tmp;
46 if (rec.fLocalMatrix) {
reed3ffade62014-08-06 12:43:09 -070047 tmp.setConcat(*rec.fLocalMatrix, this->getLocalMatrix());
commit-bot@chromium.org8fae2132014-05-07 22:26:37 +000048 newRec.fLocalMatrix = &tmp;
49 } else {
scroggoc870d492014-07-11 10:42:12 -070050 newRec.fLocalMatrix = &this->getLocalMatrix();
commit-bot@chromium.org8fae2132014-05-07 22:26:37 +000051 }
Florin Malita4aed1382017-05-25 10:38:07 -040052 return as_SB(fProxyShader)->makeContext(newRec, alloc);
commit-bot@chromium.org8fae2132014-05-07 22:26:37 +000053}
54
Matt Sarett547a7272017-04-12 11:52:47 -040055SkImage* SkLocalMatrixShader::onIsAImage(SkMatrix* outMatrix, enum TileMode* mode) const {
56 SkMatrix imageMatrix;
57 SkImage* image = fProxyShader->isAImage(&imageMatrix, mode);
58 if (image && outMatrix) {
59 // Local matrix must be applied first so it is on the right side of the concat.
60 *outMatrix = SkMatrix::Concat(imageMatrix, this->getLocalMatrix());
61 }
62
63 return image;
64}
65
Mike Reed1d8c42e2017-08-29 14:58:19 -040066bool SkLocalMatrixShader::onAppendStages(const StageRec& rec) const {
Florin Malita882ccaf2017-01-27 10:51:58 -050067 SkMatrix tmp;
Mike Reed1d8c42e2017-08-29 14:58:19 -040068 if (rec.fLocalM) {
69 tmp.setConcat(*rec.fLocalM, this->getLocalMatrix());
Florin Malita882ccaf2017-01-27 10:51:58 -050070 }
Mike Reed1d8c42e2017-08-29 14:58:19 -040071 StageRec newRec = rec;
72 newRec.fLocalM = rec.fLocalM ? &tmp : &this->getLocalMatrix();
73 return as_SB(fProxyShader)->appendStages(newRec);
Florin Malita882ccaf2017-01-27 10:51:58 -050074}
75
commit-bot@chromium.org8fae2132014-05-07 22:26:37 +000076#ifndef SK_IGNORE_TO_STRING
77void SkLocalMatrixShader::toString(SkString* str) const {
78 str->append("SkLocalMatrixShader: (");
skia.committer@gmail.comb2c82c92014-05-08 03:05:29 +000079
Florin Malita4aed1382017-05-25 10:38:07 -040080 as_SB(fProxyShader)->toString(str);
skia.committer@gmail.comb2c82c92014-05-08 03:05:29 +000081
commit-bot@chromium.org8fae2132014-05-07 22:26:37 +000082 this->INHERITED::toString(str);
skia.committer@gmail.comb2c82c92014-05-08 03:05:29 +000083
commit-bot@chromium.org8fae2132014-05-07 22:26:37 +000084 str->append(")");
85}
86#endif
87
reed150835e2016-03-10 06:36:49 -080088sk_sp<SkShader> SkShader::makeWithLocalMatrix(const SkMatrix& localMatrix) const {
commit-bot@chromium.org8fae2132014-05-07 22:26:37 +000089 if (localMatrix.isIdentity()) {
reed150835e2016-03-10 06:36:49 -080090 return sk_ref_sp(const_cast<SkShader*>(this));
commit-bot@chromium.org8fae2132014-05-07 22:26:37 +000091 }
skia.committer@gmail.comb2c82c92014-05-08 03:05:29 +000092
commit-bot@chromium.org8fae2132014-05-07 22:26:37 +000093 const SkMatrix* lm = &localMatrix;
skia.committer@gmail.comb2c82c92014-05-08 03:05:29 +000094
Ben Wagnera8834bb2016-10-24 11:36:21 -040095 sk_sp<SkShader> baseShader;
commit-bot@chromium.org8fae2132014-05-07 22:26:37 +000096 SkMatrix otherLocalMatrix;
Florin Malita4aed1382017-05-25 10:38:07 -040097 sk_sp<SkShader> proxy(as_SB(this)->makeAsALocalMatrixShader(&otherLocalMatrix));
reedf880e452015-12-30 13:39:41 -080098 if (proxy) {
commit-bot@chromium.org8fae2132014-05-07 22:26:37 +000099 otherLocalMatrix.preConcat(localMatrix);
100 lm = &otherLocalMatrix;
Ben Wagnera8834bb2016-10-24 11:36:21 -0400101 baseShader = proxy;
102 } else {
103 baseShader = sk_ref_sp(const_cast<SkShader*>(this));
commit-bot@chromium.org8fae2132014-05-07 22:26:37 +0000104 }
skia.committer@gmail.comb2c82c92014-05-08 03:05:29 +0000105
Ben Wagnera8834bb2016-10-24 11:36:21 -0400106 return sk_make_sp<SkLocalMatrixShader>(std::move(baseShader), *lm);
commit-bot@chromium.org8fae2132014-05-07 22:26:37 +0000107}