Debian Codegraph @

src/shaders/SkShaderBase.cpp

raw ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
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 {
24
25 // The 32-bit shader ID counter is not expected to wrap. In the unlikely event that it does, we
26 // assume that the associated shaders will be sufficiently temporally separated such that shaders
27 // with the same recycled IDs are no longer in use.
28 uint32_t next_unique_id() {
29 static std::atomic<uint32_t> gNextUniqueID{SK_InvalidUniqueID + 1};
30 uint32_t id = SK_InvalidUniqueID;
31 do {
32 id = gNextUniqueID.fetch_add(1, std::memory_order_relaxed);
33 } while (id == SK_InvalidUniqueID);
34 return id;
35 }
36
37 } // anonymous namespace
38
39 namespace SkShaders {
40 MatrixRec::MatrixRec(const SkMatrix& ctm) : fCTM(ctm) {}
41
42 std::optional<MatrixRec> MatrixRec::apply(const SkStageRec& rec, const SkMatrix& postInv) const {
43 SkMatrix total = fPendingLocalMatrix;
44 if (!fCTMApplied) {
45 total = SkMatrix::Concat(fCTM, total);
46 }
47 if (auto inv = total.invert()) {
48 total = SkMatrix::Concat(postInv, *inv);
49 } else {
50 return {};
51 }
52 if (!fCTMApplied) {
53 rec.fPipeline->append(SkRasterPipelineOp::seed_shader);
54 }
55 // appendMatrix is a no-op if total worked out to identity.
56 rec.fPipeline->appendMatrix(rec.fAlloc, total);
57 return MatrixRec{fCTM,
58 fTotalLocalMatrix,
59 /*pendingLocalMatrix=*/SkMatrix::I(),
60 fTotalMatrixIsValid,
61 /*ctmApplied=*/true};
62 }
63
64 std::tuple<SkMatrix, bool> MatrixRec::applyForFragmentProcessor(const SkMatrix& postInv) const {
65 SkASSERT(!fCTMApplied);
66 if (auto total = fPendingLocalMatrix.invert()) {
67 return {SkMatrix::Concat(postInv, *total), true};
68 } else {
69 return {SkMatrix::I(), false};
70 }
71 }
72
73 MatrixRec MatrixRec::applied() const {
74 // We mark the CTM as "not applied" because we *never* apply the CTM for FPs. Their starting
75 // coords are local, not device, coords.
76 return MatrixRec{fCTM,
77 fTotalLocalMatrix,
78 /*pendingLocalMatrix=*/SkMatrix::I(),
79 fTotalMatrixIsValid,
80 /*ctmApplied=*/false};
81 }
82
83 MatrixRec MatrixRec::concat(const SkMatrix& m) const {
84 return {fCTM,
85 SkShaderBase::ConcatLocalMatrices(fTotalLocalMatrix, m),
86 SkShaderBase::ConcatLocalMatrices(fPendingLocalMatrix, m),
87 fTotalMatrixIsValid,
88 fCTMApplied};
89 }
90
91 } // namespace SkShaders
92
93 ///////////////////////////////////////////////////////////////////////////////////////
94
95 SkShaderBase::SkShaderBase() : fUniqueID(next_unique_id()) {}
96
97 SkShaderBase::~SkShaderBase() = default;
98
99 void SkShaderBase::flatten(SkWriteBuffer& buffer) const { this->INHERITED::flatten(buffer); }
100
101 bool SkShaderBase::asLuminanceColor(SkColor4f* colorPtr) const {
102 SkColor4f storage;
103 if (nullptr == colorPtr) {
104 colorPtr = &storage;
105 }
106 if (this->onAsLuminanceColor(colorPtr)) {
107 colorPtr->fA = 1.0f; // we only return opaque
108 return true;
109 }
110 return false;
111 }
112
113 SkShaderBase::Context* SkShaderBase::makeContext(const ContextRec& rec, SkArenaAlloc* alloc) const {
114 #ifdef SK_ENABLE_LEGACY_SHADERCONTEXT
115 // We always fall back to raster pipeline when perspective is present.
116 auto totalMatrix = rec.fMatrixRec.totalMatrix();
117 if (totalMatrix.hasPerspective() || !totalMatrix.invert()) {
118 return nullptr;
119 }
120
121 return this->onMakeContext(rec, alloc);
122 #else
123 return nullptr;
124 #endif
125 }
126
127 SkShaderBase::Context::Context(const SkShaderBase& shader, const ContextRec& rec)
128 : fShader(shader) {
129 // We should never use a context with perspective.
130 SkASSERT(!rec.fMatrixRec.totalMatrix().hasPerspective());
131
132 fPaintAlpha = rec.fPaintAlpha;
133 }
134
135 SkShaderBase::Context::~Context() {}
136
137 bool SkShaderBase::ContextRec::isLegacyCompatible(SkColorSpace* shaderColorSpace) const {
138 // In legacy pipelines, shaders always produce premul (or opaque) and the destination is also
139 // always premul (or opaque). (And those "or opaque" caveats won't make any difference here.)
140 SkAlphaType shaderAT = kPremul_SkAlphaType, dstAT = kPremul_SkAlphaType;
141 return 0 ==
142 SkColorSpaceXformSteps{shaderColorSpace, shaderAT, fDstColorSpace, dstAT}.fFlags.mask();
143 }
144
145 sk_sp<SkShader> SkShaderBase::makeAsALocalMatrixShader(SkMatrix*) const { return nullptr; }
146
147 bool SkShaderBase::appendRootStages(const SkStageRec& rec, const SkMatrix& ctm) const {
148 return this->appendStages(rec, SkShaders::MatrixRec(ctm));
149 }
150
151 sk_sp<SkShader> SkShaderBase::makeWithCTM(const SkMatrix& postM) const {
152 return sk_sp<SkShader>(new SkCTMShader(sk_ref_sp(this), postM));
153 }
154
155 // need a cheap way to invert the alpha channel of a shader (i.e. 1 - a)
156 sk_sp<SkShader> SkShaderBase::makeInvertAlpha() const {
157 return this->makeWithColorFilter(SkColorFilters::Blend(0xFFFFFFFF, SkBlendMode::kSrcOut));
158 }