blob: e352d312997ec9058886f347ebe6c24738b85218 [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"
Florin Malita325ea322018-04-04 14:17:30 -04009#include "SkTLazy.h"
commit-bot@chromium.org8fae2132014-05-07 22:26:37 +000010
bungeman06ca8ec2016-06-09 08:01:03 -070011#if SK_SUPPORT_GPU
12#include "GrFragmentProcessor.h"
13#endif
14
15#if SK_SUPPORT_GPU
Brian Salomonaff329b2017-08-11 09:40:37 -040016std::unique_ptr<GrFragmentProcessor> SkLocalMatrixShader::asFragmentProcessor(
Mike Reede3429e62018-01-19 11:43:34 -050017 const GrFPArgs& args) const {
bungeman06ca8ec2016-06-09 08:01:03 -070018 SkMatrix tmp = this->getLocalMatrix();
brianosman839345d2016-07-22 11:04:53 -070019 if (args.fLocalMatrix) {
20 tmp.preConcat(*args.fLocalMatrix);
bungeman06ca8ec2016-06-09 08:01:03 -070021 }
Brian Salomon4cbb6e62017-10-25 15:12:19 -040022 return as_SB(fProxyShader)
Mike Reede3429e62018-01-19 11:43:34 -050023 ->asFragmentProcessor(GrFPArgs(args.fContext, args.fViewMatrix, &tmp,
Brian Salomon4cbb6e62017-10-25 15:12:19 -040024 args.fFilterQuality, args.fDstColorSpaceInfo));
bungeman06ca8ec2016-06-09 08:01:03 -070025}
26#endif
27
reed60c9b582016-04-03 09:11:13 -070028sk_sp<SkFlattenable> SkLocalMatrixShader::CreateProc(SkReadBuffer& buffer) {
reed9fa60da2014-08-21 07:59:51 -070029 SkMatrix lm;
30 buffer.readMatrix(&lm);
reed8a21c9f2016-03-08 18:50:00 -080031 auto baseShader(buffer.readShader());
reedf880e452015-12-30 13:39:41 -080032 if (!baseShader) {
halcanary96fcdcc2015-08-27 07:41:13 -070033 return nullptr;
reed9fa60da2014-08-21 07:59:51 -070034 }
reed60c9b582016-04-03 09:11:13 -070035 return baseShader->makeWithLocalMatrix(lm);
reed9fa60da2014-08-21 07:59:51 -070036}
commit-bot@chromium.org8fae2132014-05-07 22:26:37 +000037
38void SkLocalMatrixShader::flatten(SkWriteBuffer& buffer) const {
reed9fa60da2014-08-21 07:59:51 -070039 buffer.writeMatrix(this->getLocalMatrix());
commit-bot@chromium.org8fae2132014-05-07 22:26:37 +000040 buffer.writeFlattenable(fProxyShader.get());
41}
42
Florin Malita4aed1382017-05-25 10:38:07 -040043SkShaderBase::Context* SkLocalMatrixShader::onMakeContext(
Herb Derby83e939b2017-02-07 14:25:11 -050044 const ContextRec& rec, SkArenaAlloc* alloc) const
45{
Florin Malita325ea322018-04-04 14:17:30 -040046 SkTCopyOnFirstWrite<SkMatrix> lm(this->getLocalMatrix());
commit-bot@chromium.org8fae2132014-05-07 22:26:37 +000047 if (rec.fLocalMatrix) {
Florin Malita325ea322018-04-04 14:17:30 -040048 lm.writable()->preConcat(*rec.fLocalMatrix);
commit-bot@chromium.org8fae2132014-05-07 22:26:37 +000049 }
Florin Malita325ea322018-04-04 14:17:30 -040050
51 ContextRec newRec(rec);
52 newRec.fLocalMatrix = lm;
53
Florin Malita4aed1382017-05-25 10:38:07 -040054 return as_SB(fProxyShader)->makeContext(newRec, alloc);
commit-bot@chromium.org8fae2132014-05-07 22:26:37 +000055}
56
Matt Sarett547a7272017-04-12 11:52:47 -040057SkImage* SkLocalMatrixShader::onIsAImage(SkMatrix* outMatrix, enum TileMode* mode) const {
58 SkMatrix imageMatrix;
59 SkImage* image = fProxyShader->isAImage(&imageMatrix, mode);
60 if (image && outMatrix) {
61 // Local matrix must be applied first so it is on the right side of the concat.
62 *outMatrix = SkMatrix::Concat(imageMatrix, this->getLocalMatrix());
63 }
64
65 return image;
66}
67
Mike Reed1d8c42e2017-08-29 14:58:19 -040068bool SkLocalMatrixShader::onAppendStages(const StageRec& rec) const {
Florin Malita325ea322018-04-04 14:17:30 -040069 SkTCopyOnFirstWrite<SkMatrix> lm(this->getLocalMatrix());
Mike Reed1d8c42e2017-08-29 14:58:19 -040070 if (rec.fLocalM) {
Florin Malita325ea322018-04-04 14:17:30 -040071 lm.writable()->preConcat(*rec.fLocalM);
Florin Malita882ccaf2017-01-27 10:51:58 -050072 }
Florin Malita325ea322018-04-04 14:17:30 -040073
Mike Reed1d8c42e2017-08-29 14:58:19 -040074 StageRec newRec = rec;
Florin Malita325ea322018-04-04 14:17:30 -040075 newRec.fLocalM = lm;
Mike Reed1d8c42e2017-08-29 14:58:19 -040076 return as_SB(fProxyShader)->appendStages(newRec);
Florin Malita882ccaf2017-01-27 10:51:58 -050077}
78
commit-bot@chromium.org8fae2132014-05-07 22:26:37 +000079#ifndef SK_IGNORE_TO_STRING
80void SkLocalMatrixShader::toString(SkString* str) const {
81 str->append("SkLocalMatrixShader: (");
skia.committer@gmail.comb2c82c92014-05-08 03:05:29 +000082
Florin Malita4aed1382017-05-25 10:38:07 -040083 as_SB(fProxyShader)->toString(str);
skia.committer@gmail.comb2c82c92014-05-08 03:05:29 +000084
commit-bot@chromium.org8fae2132014-05-07 22:26:37 +000085 this->INHERITED::toString(str);
skia.committer@gmail.comb2c82c92014-05-08 03:05:29 +000086
commit-bot@chromium.org8fae2132014-05-07 22:26:37 +000087 str->append(")");
88}
89#endif
90
reed150835e2016-03-10 06:36:49 -080091sk_sp<SkShader> SkShader::makeWithLocalMatrix(const SkMatrix& localMatrix) const {
commit-bot@chromium.org8fae2132014-05-07 22:26:37 +000092 if (localMatrix.isIdentity()) {
reed150835e2016-03-10 06:36:49 -080093 return sk_ref_sp(const_cast<SkShader*>(this));
commit-bot@chromium.org8fae2132014-05-07 22:26:37 +000094 }
skia.committer@gmail.comb2c82c92014-05-08 03:05:29 +000095
commit-bot@chromium.org8fae2132014-05-07 22:26:37 +000096 const SkMatrix* lm = &localMatrix;
skia.committer@gmail.comb2c82c92014-05-08 03:05:29 +000097
Ben Wagnera8834bb2016-10-24 11:36:21 -040098 sk_sp<SkShader> baseShader;
commit-bot@chromium.org8fae2132014-05-07 22:26:37 +000099 SkMatrix otherLocalMatrix;
Florin Malita4aed1382017-05-25 10:38:07 -0400100 sk_sp<SkShader> proxy(as_SB(this)->makeAsALocalMatrixShader(&otherLocalMatrix));
reedf880e452015-12-30 13:39:41 -0800101 if (proxy) {
commit-bot@chromium.org8fae2132014-05-07 22:26:37 +0000102 otherLocalMatrix.preConcat(localMatrix);
103 lm = &otherLocalMatrix;
Ben Wagnera8834bb2016-10-24 11:36:21 -0400104 baseShader = proxy;
105 } else {
106 baseShader = sk_ref_sp(const_cast<SkShader*>(this));
commit-bot@chromium.org8fae2132014-05-07 22:26:37 +0000107 }
skia.committer@gmail.comb2c82c92014-05-08 03:05:29 +0000108
Ben Wagnera8834bb2016-10-24 11:36:21 -0400109 return sk_make_sp<SkLocalMatrixShader>(std::move(baseShader), *lm);
commit-bot@chromium.org8fae2132014-05-07 22:26:37 +0000110}