1 /*
2 * Copyright 2006 The Android Open Source Project
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 "include/core/SkColorFilter.h"
9 #include "include/core/SkString.h"
10 #include "include/private/SkColorData.h"
11 #include "src/core/SkArenaAlloc.h"
12 #include "src/core/SkBlendModePriv.h"
13 #include "src/core/SkRasterPipeline.h"
14 #include "src/core/SkReadBuffer.h"
15 #include "src/core/SkWriteBuffer.h"
16 #include "src/shaders/SkColorShader.h"
17 #include "src/shaders/SkComposeShader.h"
18
19 namespace {
20
wrap_lm(sk_sp<SkShader> shader,const SkMatrix * lm)21 sk_sp<SkShader> wrap_lm(sk_sp<SkShader> shader, const SkMatrix* lm) {
22 return (shader && lm) ? shader->makeWithLocalMatrix(*lm) : shader;
23 }
24
25 struct LocalMatrixStageRec final : public SkStageRec {
LocalMatrixStageRec__anon5c7ea3d90111::LocalMatrixStageRec26 LocalMatrixStageRec(const SkStageRec& rec, const SkMatrix& lm)
27 : INHERITED(rec) {
28 if (!lm.isIdentity()) {
29 if (fLocalM) {
30 fStorage.setConcat(lm, *fLocalM);
31 fLocalM = fStorage.isIdentity() ? nullptr : &fStorage;
32 } else {
33 fLocalM = &lm;
34 }
35 }
36 }
37
38 private:
39 SkMatrix fStorage;
40
41 using INHERITED = SkStageRec;
42 };
43
44 } // namespace
45
Blend(SkBlendMode mode,sk_sp<SkShader> dst,sk_sp<SkShader> src,const SkMatrix * lm)46 sk_sp<SkShader> SkShaders::Blend(SkBlendMode mode, sk_sp<SkShader> dst, sk_sp<SkShader> src,
47 const SkMatrix* lm) {
48 switch (mode) {
49 case SkBlendMode::kClear: return Color(0);
50 case SkBlendMode::kDst: return wrap_lm(std::move(dst), lm);
51 case SkBlendMode::kSrc: return wrap_lm(std::move(src), lm);
52 default: break;
53 }
54 return sk_sp<SkShader>(new SkShader_Blend(mode, std::move(dst), std::move(src), lm));
55 }
56
Lerp(float weight,sk_sp<SkShader> dst,sk_sp<SkShader> src,const SkMatrix * lm)57 sk_sp<SkShader> SkShaders::Lerp(float weight, sk_sp<SkShader> dst, sk_sp<SkShader> src,
58 const SkMatrix* lm) {
59 if (SkScalarIsNaN(weight)) {
60 return nullptr;
61 }
62 if (dst == src || weight <= 0) {
63 return wrap_lm(std::move(dst), lm);
64 }
65 if (weight >= 1) {
66 return wrap_lm(std::move(src), lm);
67 }
68 return sk_sp<SkShader>(new SkShader_Lerp(weight, std::move(dst), std::move(src), lm));
69 }
70
Lerp(sk_sp<SkShader> red,sk_sp<SkShader> dst,sk_sp<SkShader> src,const SkMatrix * lm)71 sk_sp<SkShader> SkShaders::Lerp(sk_sp<SkShader> red, sk_sp<SkShader> dst, sk_sp<SkShader> src,
72 const SkMatrix* lm) {
73 if (!red) {
74 return nullptr;
75 }
76 if (dst == src) {
77 return wrap_lm(std::move(dst), lm);
78 }
79 return sk_sp<SkShader>(new SkShader_LerpRed(std::move(red), std::move(dst), std::move(src),
80 lm));
81 }
82
83 ///////////////////////////////////////////////////////////////////////////////
84
append_shader_or_paint(const SkStageRec & rec,SkShader * shader)85 static bool append_shader_or_paint(const SkStageRec& rec, SkShader* shader) {
86 if (shader) {
87 if (!as_SB(shader)->appendStages(rec)) {
88 return false;
89 }
90 } else {
91 rec.fPipeline->append_constant_color(rec.fAlloc, rec.fPaint.getColor4f().premul().vec());
92 }
93 return true;
94 }
95
96 // Returns the output of e0, and leaves the output of e1 in r,g,b,a
append_two_shaders(const SkStageRec & rec,SkShader * s0,SkShader * s1)97 static float* append_two_shaders(const SkStageRec& rec, SkShader* s0, SkShader* s1) {
98 struct Storage {
99 float fRes0[4 * SkRasterPipeline_kMaxStride];
100 };
101 auto storage = rec.fAlloc->make<Storage>();
102
103 if (!append_shader_or_paint(rec, s0)) {
104 return nullptr;
105 }
106 rec.fPipeline->append(SkRasterPipeline::store_src, storage->fRes0);
107
108 if (!append_shader_or_paint(rec, s1)) {
109 return nullptr;
110 }
111 return storage->fRes0;
112 }
113
114 ///////////////////////////////////////////////////////////////////////////////
115
CreateProc(SkReadBuffer & buffer)116 sk_sp<SkFlattenable> SkShader_Blend::CreateProc(SkReadBuffer& buffer) {
117 sk_sp<SkShader> dst(buffer.readShader());
118 sk_sp<SkShader> src(buffer.readShader());
119 unsigned mode = buffer.read32();
120
121 // check for valid mode before we cast to the enum type
122 if (!buffer.validate(mode <= (unsigned)SkBlendMode::kLastMode)) {
123 return nullptr;
124 }
125 return SkShaders::Blend(static_cast<SkBlendMode>(mode), std::move(dst), std::move(src));
126 }
127
flatten(SkWriteBuffer & buffer) const128 void SkShader_Blend::flatten(SkWriteBuffer& buffer) const {
129 buffer.writeFlattenable(fDst.get());
130 buffer.writeFlattenable(fSrc.get());
131 buffer.write32((int)fMode);
132 }
133
onAppendStages(const SkStageRec & orig_rec) const134 bool SkShader_Blend::onAppendStages(const SkStageRec& orig_rec) const {
135 const LocalMatrixStageRec rec(orig_rec, this->getLocalMatrix());
136
137 float* res0 = append_two_shaders(rec, fDst.get(), fSrc.get());
138 if (!res0) {
139 return false;
140 }
141
142 rec.fPipeline->append(SkRasterPipeline::load_dst, res0);
143 SkBlendMode_AppendStages(fMode, rec.fPipeline);
144 return true;
145 }
146
CreateProc(SkReadBuffer & buffer)147 sk_sp<SkFlattenable> SkShader_Lerp::CreateProc(SkReadBuffer& buffer) {
148 sk_sp<SkShader> dst(buffer.readShader());
149 sk_sp<SkShader> src(buffer.readShader());
150 float t = buffer.readScalar();
151 return buffer.isValid() ? SkShaders::Lerp(t, std::move(dst), std::move(src)) : nullptr;
152 }
153
flatten(SkWriteBuffer & buffer) const154 void SkShader_Lerp::flatten(SkWriteBuffer& buffer) const {
155 buffer.writeFlattenable(fDst.get());
156 buffer.writeFlattenable(fSrc.get());
157 buffer.writeScalar(fWeight);
158 }
159
onAppendStages(const SkStageRec & orig_rec) const160 bool SkShader_Lerp::onAppendStages(const SkStageRec& orig_rec) const {
161 const LocalMatrixStageRec rec(orig_rec, this->getLocalMatrix());
162
163 float* res0 = append_two_shaders(rec, fDst.get(), fSrc.get());
164 if (!res0) {
165 return false;
166 }
167
168 rec.fPipeline->append(SkRasterPipeline::load_dst, res0);
169 rec.fPipeline->append(SkRasterPipeline::lerp_1_float, &fWeight);
170 return true;
171 }
172
CreateProc(SkReadBuffer & buffer)173 sk_sp<SkFlattenable> SkShader_LerpRed::CreateProc(SkReadBuffer& buffer) {
174 sk_sp<SkShader> dst(buffer.readShader());
175 sk_sp<SkShader> src(buffer.readShader());
176 sk_sp<SkShader> red(buffer.readShader());
177 return buffer.isValid() ?
178 SkShaders::Lerp(std::move(red), std::move(dst), std::move(src)) : nullptr;
179 }
180
flatten(SkWriteBuffer & buffer) const181 void SkShader_LerpRed::flatten(SkWriteBuffer& buffer) const {
182 buffer.writeFlattenable(fDst.get());
183 buffer.writeFlattenable(fSrc.get());
184 buffer.writeFlattenable(fRed.get());
185 }
186
onAppendStages(const SkStageRec & orig_rec) const187 bool SkShader_LerpRed::onAppendStages(const SkStageRec& orig_rec) const {
188 const LocalMatrixStageRec rec(orig_rec, this->getLocalMatrix());
189
190 struct Storage {
191 float fRed[4 * SkRasterPipeline_kMaxStride];
192 };
193 auto storage = rec.fAlloc->make<Storage>();
194 if (!as_SB(fRed)->appendStages(rec)) {
195 return false;
196 }
197 // actually, we just need the first (red) channel, but for now we store rgba
198 rec.fPipeline->append(SkRasterPipeline::store_src, storage->fRed);
199
200 float* res0 = append_two_shaders(rec, fDst.get(), fSrc.get());
201 if (!res0) {
202 return false;
203 }
204
205 rec.fPipeline->append(SkRasterPipeline::load_dst, res0);
206 rec.fPipeline->append(SkRasterPipeline::lerp_native, &storage->fRed[0]);
207 return true;
208 }
209
210 #if SK_SUPPORT_GPU
211
212 #include "include/private/GrRecordingContext.h"
213 #include "src/gpu/effects/GrXfermodeFragmentProcessor.h"
214 #include "src/gpu/effects/generated/GrComposeLerpEffect.h"
215 #include "src/gpu/effects/generated/GrComposeLerpRedEffect.h"
216 #include "src/gpu/effects/generated/GrConstColorProcessor.h"
217
as_fp(const GrFPArgs & args,SkShader * shader)218 static std::unique_ptr<GrFragmentProcessor> as_fp(const GrFPArgs& args, SkShader* shader) {
219 return shader ? as_SB(shader)->asFragmentProcessor(args) : nullptr;
220 }
221
asFragmentProcessor(const GrFPArgs & orig_args) const222 std::unique_ptr<GrFragmentProcessor> SkShader_Blend::asFragmentProcessor(
223 const GrFPArgs& orig_args) const {
224 const GrFPArgs::WithPreLocalMatrix args(orig_args, this->getLocalMatrix());
225 auto fpA = as_fp(args, fDst.get());
226 auto fpB = as_fp(args, fSrc.get());
227 if (!fpA || !fpB) {
228 return nullptr;
229 }
230 return GrXfermodeFragmentProcessor::MakeFromTwoProcessors(std::move(fpB),
231 std::move(fpA), fMode);
232 }
233
asFragmentProcessor(const GrFPArgs & orig_args) const234 std::unique_ptr<GrFragmentProcessor> SkShader_Lerp::asFragmentProcessor(
235 const GrFPArgs& orig_args) const {
236 const GrFPArgs::WithPreLocalMatrix args(orig_args, this->getLocalMatrix());
237 auto fpA = as_fp(args, fDst.get());
238 auto fpB = as_fp(args, fSrc.get());
239 return GrComposeLerpEffect::Make(std::move(fpA), std::move(fpB), fWeight);
240 }
241
asFragmentProcessor(const GrFPArgs & orig_args) const242 std::unique_ptr<GrFragmentProcessor> SkShader_LerpRed::asFragmentProcessor(
243 const GrFPArgs& orig_args) const {
244 const GrFPArgs::WithPreLocalMatrix args(orig_args, this->getLocalMatrix());
245 auto fpA = as_fp(args, fDst.get());
246 auto fpB = as_fp(args, fSrc.get());
247 auto red = as_SB(fRed)->asFragmentProcessor(args);
248 if (!red) {
249 return nullptr;
250 }
251 return GrComposeLerpRedEffect::Make(std::move(fpA), std::move(fpB), std::move(red));
252 }
253 #endif
254