| 1 |
/* |
| 2 |
* Copyright 2023 Google LLC |
| 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 |
|
| 8 |
#include "src/shaders/SkShaderBase.h" |
| 9 |
|
| 10 |
#include "include/core/SkAlphaType.h" |
| 11 |
#include "include/core/SkBlendMode.h" |
| 12 |
#include "include/core/SkColorFilter.h" |
| 13 |
#include "src/core/SkColorSpaceXformSteps.h" |
| 14 |
#include "src/core/SkEffectPriv.h" |
| 15 |
#include "src/core/SkRasterPipeline.h" |
| 16 |
#include "src/core/SkRasterPipelineOpList.h" |
| 17 |
#include "src/shaders/SkLocalMatrixShader.h" |
| 18 |
|
| 19 |
#include <atomic> |
| 20 |
|
| 21 |
class SkWriteBuffer; |
| 22 |
|
| 23 |
namespace SkShaders { |
| 24 |
MatrixRec::MatrixRec(const SkMatrix& ctm) : fCTM(ctm) {} |
| 25 |
|
| 26 |
std::optional<MatrixRec> MatrixRec::apply(const SkStageRec& rec, const SkMatrix& postInv) const { |
| 27 |
SkMatrix total = fPendingLocalMatrix; |
| 28 |
if (!fCTMApplied) { |
| 29 |
total = SkMatrix::Concat(fCTM, total); |
| 30 |
} |
| 31 |
if (auto inv = total.invert()) { |
| 32 |
total = SkMatrix::Concat(postInv, *inv); |
| 33 |
} else { |
| 34 |
return {}; |
| 35 |
} |
| 36 |
if (!fCTMApplied) { |
| 37 |
rec.fPipeline->append(SkRasterPipelineOp::seed_shader); |
| 38 |
} |
| 39 |
// appendMatrix is a no-op if total worked out to identity. |
| 40 |
rec.fPipeline->appendMatrix(rec.fAlloc, total); |
| 41 |
return MatrixRec{fCTM, |
| 42 |
fTotalLocalMatrix, |
| 43 |
/*pendingLocalMatrix=*/SkMatrix::I(), |
| 44 |
fTotalMatrixIsValid, |
| 45 |
/*ctmApplied=*/true}; |
| 46 |
} |
| 47 |
|
| 48 |
std::tuple<SkMatrix, bool> MatrixRec::applyForFragmentProcessor(const SkMatrix& postInv) const { |
| 49 |
SkASSERT(!fCTMApplied); |
| 50 |
if (auto total = fPendingLocalMatrix.invert()) { |
| 51 |
return {SkMatrix::Concat(postInv, *total), true}; |
| 52 |
} else { |
| 53 |
return {SkMatrix::I(), false}; |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
MatrixRec MatrixRec::applied() const { |
| 58 |
// We mark the CTM as "not applied" because we *never* apply the CTM for FPs. Their starting |
| 59 |
// coords are local, not device, coords. |
| 60 |
return MatrixRec{fCTM, |
| 61 |
fTotalLocalMatrix, |
| 62 |
/*pendingLocalMatrix=*/SkMatrix::I(), |
| 63 |
fTotalMatrixIsValid, |
| 64 |
/*ctmApplied=*/false}; |
| 65 |
} |
| 66 |
|
| 67 |
MatrixRec MatrixRec::concat(const SkMatrix& m) const { |
| 68 |
return {fCTM, |
| 69 |
SkShaderBase::ConcatLocalMatrices(fTotalLocalMatrix, m), |
| 70 |
SkShaderBase::ConcatLocalMatrices(fPendingLocalMatrix, m), |
| 71 |
fTotalMatrixIsValid, |
| 72 |
fCTMApplied}; |
| 73 |
} |
| 74 |
|
| 75 |
} // namespace SkShaders |
| 76 |
|
| 77 |
/////////////////////////////////////////////////////////////////////////////////////// |
| 78 |
|
| •79 |
SkShaderBase::SkShaderBase() {} |
| 80 |
|
| 81 |
SkShaderBase::~SkShaderBase() = default; |
| 82 |
|
| 83 |
void SkShaderBase::flatten(SkWriteBuffer& buffer) const { this->INHERITED::flatten(buffer); } |
| 84 |
|
| 85 |
bool SkShaderBase::asLuminanceColor(SkColor4f* colorPtr) const { |
| 86 |
SkColor4f storage; |
| 87 |
if (nullptr == colorPtr) { |
| 88 |
colorPtr = &storage; |
| 89 |
} |
| 90 |
if (this->onAsLuminanceColor(colorPtr)) { |
| 91 |
colorPtr->fA = 1.0f; // we only return opaque |
| 92 |
return true; |
| 93 |
} |
| 94 |
return false; |
| 95 |
} |
| 96 |
|
| 97 |
SkShaderBase::Context* SkShaderBase::makeContext(const ContextRec& rec, SkArenaAlloc* alloc) const { |
| 98 |
#ifdef SK_ENABLE_LEGACY_SHADERCONTEXT |
| 99 |
// We always fall back to raster pipeline when perspective is present. |
| 100 |
auto totalMatrix = rec.fMatrixRec.totalMatrix(); |
| 101 |
if (totalMatrix.hasPerspective() || !totalMatrix.invert()) { |
| 102 |
return nullptr; |
| 103 |
} |
| 104 |
|
| 105 |
return this->onMakeContext(rec, alloc); |
| 106 |
#else |
| 107 |
return nullptr; |
| 108 |
#endif |
| 109 |
} |
| 110 |
|
| 111 |
SkShaderBase::Context::Context(const SkShaderBase& shader, const ContextRec& rec) |
| 112 |
: fShader(shader) { |
| 113 |
// We should never use a context with perspective. |
| 114 |
SkASSERT(!rec.fMatrixRec.totalMatrix().hasPerspective()); |
| 115 |
|
| 116 |
fPaintAlpha = rec.fPaintAlpha; |
| 117 |
} |
| 118 |
|
| 119 |
SkShaderBase::Context::~Context() {} |
| 120 |
|
| 121 |
bool SkShaderBase::ContextRec::isLegacyCompatible(SkColorSpace* shaderColorSpace) const { |
| 122 |
// In legacy pipelines, shaders always produce premul (or opaque) and the destination is also |
| 123 |
// always premul (or opaque). (And those "or opaque" caveats won't make any difference here.) |
| 124 |
SkAlphaType shaderAT = kPremul_SkAlphaType, dstAT = kPremul_SkAlphaType; |
| 125 |
return 0 == |
| 126 |
SkColorSpaceXformSteps{shaderColorSpace, shaderAT, fDstColorSpace, dstAT}.fFlags.mask(); |
| 127 |
} |
| 128 |
|
| 129 |
sk_sp<SkShader> SkShaderBase::makeAsALocalMatrixShader(SkMatrix*) const { return nullptr; } |
| 130 |
|
| 131 |
bool SkShaderBase::appendRootStages(const SkStageRec& rec, const SkMatrix& ctm) const { |
| 132 |
return this->appendStages(rec, SkShaders::MatrixRec(ctm)); |
| 133 |
} |
| 134 |
|
| 135 |
sk_sp<SkShader> SkShaderBase::makeWithCTM(const SkMatrix& postM) const { |
| 136 |
return sk_sp<SkShader>(new SkCTMShader(sk_ref_sp(this), postM)); |
| 137 |
} |
| 138 |
|
| 139 |
// need a cheap way to invert the alpha channel of a shader (i.e. 1 - a) |
| 140 |
sk_sp<SkShader> SkShaderBase::makeInvertAlpha() const { |
| 141 |
return this->makeWithColorFilter(SkColorFilters::Blend(0xFFFFFFFF, SkBlendMode::kSrcOut)); |
| 142 |
} |