commit-bot@chromium.org | 8fae213 | 2014-05-07 22:26:37 +0000 | [diff] [blame] | 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 | |
commit-bot@chromium.org | 5970f62 | 2014-05-12 20:42:21 +0000 | [diff] [blame] | 8 | #include "SkLocalMatrixShader.h" |
Florin Malita | 325ea32 | 2018-04-04 14:17:30 -0400 | [diff] [blame^] | 9 | #include "SkTLazy.h" |
commit-bot@chromium.org | 8fae213 | 2014-05-07 22:26:37 +0000 | [diff] [blame] | 10 | |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 11 | #if SK_SUPPORT_GPU |
| 12 | #include "GrFragmentProcessor.h" |
| 13 | #endif |
| 14 | |
| 15 | #if SK_SUPPORT_GPU |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 16 | std::unique_ptr<GrFragmentProcessor> SkLocalMatrixShader::asFragmentProcessor( |
Mike Reed | e3429e6 | 2018-01-19 11:43:34 -0500 | [diff] [blame] | 17 | const GrFPArgs& args) const { |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 18 | SkMatrix tmp = this->getLocalMatrix(); |
brianosman | 839345d | 2016-07-22 11:04:53 -0700 | [diff] [blame] | 19 | if (args.fLocalMatrix) { |
| 20 | tmp.preConcat(*args.fLocalMatrix); |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 21 | } |
Brian Salomon | 4cbb6e6 | 2017-10-25 15:12:19 -0400 | [diff] [blame] | 22 | return as_SB(fProxyShader) |
Mike Reed | e3429e6 | 2018-01-19 11:43:34 -0500 | [diff] [blame] | 23 | ->asFragmentProcessor(GrFPArgs(args.fContext, args.fViewMatrix, &tmp, |
Brian Salomon | 4cbb6e6 | 2017-10-25 15:12:19 -0400 | [diff] [blame] | 24 | args.fFilterQuality, args.fDstColorSpaceInfo)); |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 25 | } |
| 26 | #endif |
| 27 | |
reed | 60c9b58 | 2016-04-03 09:11:13 -0700 | [diff] [blame] | 28 | sk_sp<SkFlattenable> SkLocalMatrixShader::CreateProc(SkReadBuffer& buffer) { |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 29 | SkMatrix lm; |
| 30 | buffer.readMatrix(&lm); |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 31 | auto baseShader(buffer.readShader()); |
reed | f880e45 | 2015-12-30 13:39:41 -0800 | [diff] [blame] | 32 | if (!baseShader) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 33 | return nullptr; |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 34 | } |
reed | 60c9b58 | 2016-04-03 09:11:13 -0700 | [diff] [blame] | 35 | return baseShader->makeWithLocalMatrix(lm); |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 36 | } |
commit-bot@chromium.org | 8fae213 | 2014-05-07 22:26:37 +0000 | [diff] [blame] | 37 | |
| 38 | void SkLocalMatrixShader::flatten(SkWriteBuffer& buffer) const { |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 39 | buffer.writeMatrix(this->getLocalMatrix()); |
commit-bot@chromium.org | 8fae213 | 2014-05-07 22:26:37 +0000 | [diff] [blame] | 40 | buffer.writeFlattenable(fProxyShader.get()); |
| 41 | } |
| 42 | |
Florin Malita | 4aed138 | 2017-05-25 10:38:07 -0400 | [diff] [blame] | 43 | SkShaderBase::Context* SkLocalMatrixShader::onMakeContext( |
Herb Derby | 83e939b | 2017-02-07 14:25:11 -0500 | [diff] [blame] | 44 | const ContextRec& rec, SkArenaAlloc* alloc) const |
| 45 | { |
Florin Malita | 325ea32 | 2018-04-04 14:17:30 -0400 | [diff] [blame^] | 46 | SkTCopyOnFirstWrite<SkMatrix> lm(this->getLocalMatrix()); |
commit-bot@chromium.org | 8fae213 | 2014-05-07 22:26:37 +0000 | [diff] [blame] | 47 | if (rec.fLocalMatrix) { |
Florin Malita | 325ea32 | 2018-04-04 14:17:30 -0400 | [diff] [blame^] | 48 | lm.writable()->preConcat(*rec.fLocalMatrix); |
commit-bot@chromium.org | 8fae213 | 2014-05-07 22:26:37 +0000 | [diff] [blame] | 49 | } |
Florin Malita | 325ea32 | 2018-04-04 14:17:30 -0400 | [diff] [blame^] | 50 | |
| 51 | ContextRec newRec(rec); |
| 52 | newRec.fLocalMatrix = lm; |
| 53 | |
Florin Malita | 4aed138 | 2017-05-25 10:38:07 -0400 | [diff] [blame] | 54 | return as_SB(fProxyShader)->makeContext(newRec, alloc); |
commit-bot@chromium.org | 8fae213 | 2014-05-07 22:26:37 +0000 | [diff] [blame] | 55 | } |
| 56 | |
Matt Sarett | 547a727 | 2017-04-12 11:52:47 -0400 | [diff] [blame] | 57 | SkImage* 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 Reed | 1d8c42e | 2017-08-29 14:58:19 -0400 | [diff] [blame] | 68 | bool SkLocalMatrixShader::onAppendStages(const StageRec& rec) const { |
Florin Malita | 325ea32 | 2018-04-04 14:17:30 -0400 | [diff] [blame^] | 69 | SkTCopyOnFirstWrite<SkMatrix> lm(this->getLocalMatrix()); |
Mike Reed | 1d8c42e | 2017-08-29 14:58:19 -0400 | [diff] [blame] | 70 | if (rec.fLocalM) { |
Florin Malita | 325ea32 | 2018-04-04 14:17:30 -0400 | [diff] [blame^] | 71 | lm.writable()->preConcat(*rec.fLocalM); |
Florin Malita | 882ccaf | 2017-01-27 10:51:58 -0500 | [diff] [blame] | 72 | } |
Florin Malita | 325ea32 | 2018-04-04 14:17:30 -0400 | [diff] [blame^] | 73 | |
Mike Reed | 1d8c42e | 2017-08-29 14:58:19 -0400 | [diff] [blame] | 74 | StageRec newRec = rec; |
Florin Malita | 325ea32 | 2018-04-04 14:17:30 -0400 | [diff] [blame^] | 75 | newRec.fLocalM = lm; |
Mike Reed | 1d8c42e | 2017-08-29 14:58:19 -0400 | [diff] [blame] | 76 | return as_SB(fProxyShader)->appendStages(newRec); |
Florin Malita | 882ccaf | 2017-01-27 10:51:58 -0500 | [diff] [blame] | 77 | } |
| 78 | |
commit-bot@chromium.org | 8fae213 | 2014-05-07 22:26:37 +0000 | [diff] [blame] | 79 | #ifndef SK_IGNORE_TO_STRING |
| 80 | void SkLocalMatrixShader::toString(SkString* str) const { |
| 81 | str->append("SkLocalMatrixShader: ("); |
skia.committer@gmail.com | b2c82c9 | 2014-05-08 03:05:29 +0000 | [diff] [blame] | 82 | |
Florin Malita | 4aed138 | 2017-05-25 10:38:07 -0400 | [diff] [blame] | 83 | as_SB(fProxyShader)->toString(str); |
skia.committer@gmail.com | b2c82c9 | 2014-05-08 03:05:29 +0000 | [diff] [blame] | 84 | |
commit-bot@chromium.org | 8fae213 | 2014-05-07 22:26:37 +0000 | [diff] [blame] | 85 | this->INHERITED::toString(str); |
skia.committer@gmail.com | b2c82c9 | 2014-05-08 03:05:29 +0000 | [diff] [blame] | 86 | |
commit-bot@chromium.org | 8fae213 | 2014-05-07 22:26:37 +0000 | [diff] [blame] | 87 | str->append(")"); |
| 88 | } |
| 89 | #endif |
| 90 | |
reed | 150835e | 2016-03-10 06:36:49 -0800 | [diff] [blame] | 91 | sk_sp<SkShader> SkShader::makeWithLocalMatrix(const SkMatrix& localMatrix) const { |
commit-bot@chromium.org | 8fae213 | 2014-05-07 22:26:37 +0000 | [diff] [blame] | 92 | if (localMatrix.isIdentity()) { |
reed | 150835e | 2016-03-10 06:36:49 -0800 | [diff] [blame] | 93 | return sk_ref_sp(const_cast<SkShader*>(this)); |
commit-bot@chromium.org | 8fae213 | 2014-05-07 22:26:37 +0000 | [diff] [blame] | 94 | } |
skia.committer@gmail.com | b2c82c9 | 2014-05-08 03:05:29 +0000 | [diff] [blame] | 95 | |
commit-bot@chromium.org | 8fae213 | 2014-05-07 22:26:37 +0000 | [diff] [blame] | 96 | const SkMatrix* lm = &localMatrix; |
skia.committer@gmail.com | b2c82c9 | 2014-05-08 03:05:29 +0000 | [diff] [blame] | 97 | |
Ben Wagner | a8834bb | 2016-10-24 11:36:21 -0400 | [diff] [blame] | 98 | sk_sp<SkShader> baseShader; |
commit-bot@chromium.org | 8fae213 | 2014-05-07 22:26:37 +0000 | [diff] [blame] | 99 | SkMatrix otherLocalMatrix; |
Florin Malita | 4aed138 | 2017-05-25 10:38:07 -0400 | [diff] [blame] | 100 | sk_sp<SkShader> proxy(as_SB(this)->makeAsALocalMatrixShader(&otherLocalMatrix)); |
reed | f880e45 | 2015-12-30 13:39:41 -0800 | [diff] [blame] | 101 | if (proxy) { |
commit-bot@chromium.org | 8fae213 | 2014-05-07 22:26:37 +0000 | [diff] [blame] | 102 | otherLocalMatrix.preConcat(localMatrix); |
| 103 | lm = &otherLocalMatrix; |
Ben Wagner | a8834bb | 2016-10-24 11:36:21 -0400 | [diff] [blame] | 104 | baseShader = proxy; |
| 105 | } else { |
| 106 | baseShader = sk_ref_sp(const_cast<SkShader*>(this)); |
commit-bot@chromium.org | 8fae213 | 2014-05-07 22:26:37 +0000 | [diff] [blame] | 107 | } |
skia.committer@gmail.com | b2c82c9 | 2014-05-08 03:05:29 +0000 | [diff] [blame] | 108 | |
Ben Wagner | a8834bb | 2016-10-24 11:36:21 -0400 | [diff] [blame] | 109 | return sk_make_sp<SkLocalMatrixShader>(std::move(baseShader), *lm); |
commit-bot@chromium.org | 8fae213 | 2014-05-07 22:26:37 +0000 | [diff] [blame] | 110 | } |