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