1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
|
/*
* Copyright 2006 The Android Open Source Project
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include "include/core/SkColorFilter.h"
#include "include/core/SkString.h"
#include "include/private/SkColorData.h"
#include "src/core/SkArenaAlloc.h"
#include "src/core/SkBlendModePriv.h"
#include "src/core/SkBlenderBase.h"
#include "src/core/SkRasterPipeline.h"
#include "src/core/SkReadBuffer.h"
#include "src/core/SkRuntimeEffectPriv.h"
#include "src/core/SkVM.h"
#include "src/core/SkWriteBuffer.h"
#include "src/shaders/SkColorShader.h"
#include "src/shaders/SkComposeShader.h"
#ifdef SK_ENABLE_SKSL
#include "src/core/SkKeyHelpers.h"
#include "src/core/SkPaintParamsKey.h"
#endif
namespace {
struct LocalMatrixStageRec final : public SkStageRec {
LocalMatrixStageRec(const SkStageRec& rec, const SkMatrix& lm)
: INHERITED(rec) {
if (!lm.isIdentity()) {
if (fLocalM) {
fStorage.setConcat(lm, *fLocalM);
fLocalM = fStorage.isIdentity() ? nullptr : &fStorage;
} else {
fLocalM = &lm;
}
}
}
private:
SkMatrix fStorage;
using INHERITED = SkStageRec;
};
} // namespace
sk_sp<SkShader> SkShaders::Blend(SkBlendMode mode, sk_sp<SkShader> dst, sk_sp<SkShader> src) {
if (!src || !dst) {
return nullptr;
}
switch (mode) {
case SkBlendMode::kClear: return Color(0);
case SkBlendMode::kDst: return dst;
case SkBlendMode::kSrc: return src;
default: break;
}
return sk_sp<SkShader>(new SkShader_Blend(mode, std::move(dst), std::move(src)));
}
sk_sp<SkShader> SkShaders::Blend(sk_sp<SkBlender> blender, sk_sp<SkShader> dst, sk_sp<SkShader> src) {
if (!src || !dst) {
return nullptr;
}
if (!blender) {
return SkShaders::Blend(SkBlendMode::kSrcOver, std::move(dst), std::move(src));
}
return sk_sp<SkShader>(new SkShader_Blend(std::move(blender), std::move(dst), std::move(src)));
}
///////////////////////////////////////////////////////////////////////////////
SkShader_Blend::SkShader_Blend(sk_sp<SkBlender> blender, sk_sp<SkShader> dst, sk_sp<SkShader> src)
: fDst(std::move(dst))
, fSrc(std::move(src))
, fBlender(std::move(blender))
, fMode((SkBlendMode)kCustom_SkBlendMode) {
if (std::optional<SkBlendMode> bm = as_BB(fBlender)->asBlendMode(); bm.has_value()) {
fMode = *bm;
fBlender.reset();
}
}
sk_sp<SkFlattenable> SkShader_Blend::CreateProc(SkReadBuffer& buffer) {
sk_sp<SkShader> dst(buffer.readShader());
sk_sp<SkShader> src(buffer.readShader());
if (!buffer.validate(dst && src)) {
return nullptr;
}
sk_sp<SkBlender> blender(nullptr);
unsigned mode = buffer.read32();
if (mode == kCustom_SkBlendMode) {
blender = buffer.readBlender();
if (buffer.validate(blender != nullptr)) {
return SkShaders::Blend(std::move(blender), std::move(dst), std::move(src));
}
} else {
if (buffer.validate(mode <= (unsigned)SkBlendMode::kLastMode)) {
return SkShaders::Blend(static_cast<SkBlendMode>(mode), std::move(dst), std::move(src));
}
}
return nullptr;
}
void SkShader_Blend::flatten(SkWriteBuffer& buffer) const {
buffer.writeFlattenable(fDst.get());
buffer.writeFlattenable(fSrc.get());
if (fBlender) {
buffer.write32(kCustom_SkBlendMode);
buffer.writeFlattenable(fBlender.get());
} else {
buffer.write32((int)fMode);
}
}
// Returns the output of e0, and leaves the output of e1 in r,g,b,a
static float* append_two_shaders(const SkStageRec& rec, SkShader* s0, SkShader* s1) {
struct Storage {
float fRes0[4 * SkRasterPipeline_kMaxStride];
};
auto storage = rec.fAlloc->make<Storage>();
if (!as_SB(s0)->appendStages(rec)) {
return nullptr;
}
rec.fPipeline->append(SkRasterPipeline::store_src, storage->fRes0);
if (!as_SB(s1)->appendStages(rec)) {
return nullptr;
}
return storage->fRes0;
}
bool SkShader_Blend::onAppendStages(const SkStageRec& orig_rec) const {
if (fBlender) {
return false;
}
const LocalMatrixStageRec rec(orig_rec, this->getLocalMatrix());
float* res0 = append_two_shaders(rec, fDst.get(), fSrc.get());
if (!res0) {
return false;
}
rec.fPipeline->append(SkRasterPipeline::load_dst, res0);
SkBlendMode_AppendStages(fMode, rec.fPipeline);
return true;
}
skvm::Color SkShader_Blend::onProgram(skvm::Builder* p,
skvm::Coord device, skvm::Coord local, skvm::Color paint,
const SkMatrixProvider& mats, const SkMatrix* localM,
const SkColorInfo& cinfo,
skvm::Uniforms* uniforms, SkArenaAlloc* alloc) const {
skvm::Color d,s;
if ((d = as_SB(fDst)->program(p, device,local, paint, mats,localM, cinfo, uniforms,alloc)) &&
(s = as_SB(fSrc)->program(p, device,local, paint, mats,localM, cinfo, uniforms,alloc)))
{
if (fBlender) {
return as_BB(fBlender)->program(p, s,d, cinfo, uniforms,alloc);
} else {
return p->blend(fMode, s,d);
}
}
return {};
}
#if SK_SUPPORT_GPU
#include "include/gpu/GrRecordingContext.h"
#include "src/gpu/ganesh/GrFragmentProcessor.h"
#include "src/gpu/ganesh/effects/GrBlendFragmentProcessor.h"
std::unique_ptr<GrFragmentProcessor> SkShader_Blend::asFragmentProcessor(
const GrFPArgs& orig_args) const {
GrFPArgs::WithPreLocalMatrix args(orig_args, this->getLocalMatrix());
auto fpA = as_SB(fDst)->asFragmentProcessor(args);
auto fpB = as_SB(fSrc)->asFragmentProcessor(args);
if (!fpA || !fpB) {
// This is unexpected. Both src and dst shaders should be valid. Just fail.
return nullptr;
}
if (fBlender) {
return as_BB(fBlender)->asFragmentProcessor(std::move(fpB), std::move(fpA), orig_args);
} else {
return GrBlendFragmentProcessor::Make(std::move(fpB), std::move(fpA), fMode);
}
}
#endif
#ifdef SK_ENABLE_SKSL
void SkShader_Blend::addToKey(const SkKeyContext& keyContext,
SkPaintParamsKeyBuilder* builder,
SkPipelineDataGatherer* gatherer) const {
// TODO: add blender support
SkASSERT(!fBlender);
BlendShaderBlock::BeginBlock(keyContext, builder, gatherer, { fMode });
as_SB(fDst)->addToKey(keyContext, builder, gatherer);
as_SB(fSrc)->addToKey(keyContext, builder, gatherer);
builder->endBlock();
}
#endif
|