Flutter Engine
The Flutter Engine
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
SkBlendShader.cpp
Go to the documentation of this file.
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
9
12#include "include/core/SkData.h"
26
27#include <optional>
28
29sk_sp<SkFlattenable> SkBlendShader::CreateProc(SkReadBuffer& buffer) {
30 sk_sp<SkShader> dst(buffer.readShader());
31 sk_sp<SkShader> src(buffer.readShader());
32 if (!buffer.validate(dst && src)) {
33 return nullptr;
34 }
35
36 unsigned mode = buffer.read32();
37
39 sk_sp<SkBlender> blender = buffer.readBlender();
40 if (buffer.validate(blender != nullptr)) {
41 return SkShaders::Blend(std::move(blender), std::move(dst), std::move(src));
42 }
43 } else {
44 if (buffer.validate(mode <= (unsigned)SkBlendMode::kLastMode)) {
45 return SkShaders::Blend(static_cast<SkBlendMode>(mode), std::move(dst), std::move(src));
46 }
47 }
48 return nullptr;
49}
50
52 buffer.writeFlattenable(fDst.get());
53 buffer.writeFlattenable(fSrc.get());
54 buffer.write32((int)fMode);
55}
56
57// Returns the output of e0, and leaves the output of e1 in r,g,b,a
58static float* append_two_shaders(const SkStageRec& rec,
59 const SkShaders::MatrixRec& mRec,
60 SkShader* s0,
61 SkShader* s1) {
62 struct Storage {
63 float fCoords[2 * SkRasterPipeline_kMaxStride];
64 float fRes0[4 * SkRasterPipeline_kMaxStride];
65 };
66 auto storage = rec.fAlloc->make<Storage>();
67
68 // Note we cannot simply apply mRec here and then unconditionally store the coordinates. When
69 // building for Android Framework it would interrupt the backwards local matrix concatenation if
70 // mRec had a pending local matrix and either of the children also had a local matrix.
71 // b/256873449
73 rec.fPipeline->append(SkRasterPipelineOp::store_src_rg, storage->fCoords);
74 }
75 if (!as_SB(s0)->appendStages(rec, mRec)) {
76 return nullptr;
77 }
78 rec.fPipeline->append(SkRasterPipelineOp::store_src, storage->fRes0);
79
81 rec.fPipeline->append(SkRasterPipelineOp::load_src_rg, storage->fCoords);
82 }
83 if (!as_SB(s1)->appendStages(rec, mRec)) {
84 return nullptr;
85 }
86 return storage->fRes0;
87}
88
89bool SkBlendShader::appendStages(const SkStageRec& rec, const SkShaders::MatrixRec& mRec) const {
90 float* res0 = append_two_shaders(rec, mRec, fDst.get(), fSrc.get());
91 if (!res0) {
92 return false;
93 }
94
95 rec.fPipeline->append(SkRasterPipelineOp::load_dst, res0);
97 return true;
98}
99
101 if (!src || !dst) {
102 return nullptr;
103 }
104 switch (mode) {
106 return Color(0);
108 return dst;
110 return src;
111 default:
112 break;
113 }
114 return sk_sp<SkShader>(new SkBlendShader(mode, std::move(dst), std::move(src)));
115}
116
120 using namespace SkKnownRuntimeEffects;
121
122 if (!src || !dst) {
123 return nullptr;
124 }
125 if (!blender) {
126 return SkShaders::Blend(SkBlendMode::kSrcOver, std::move(dst), std::move(src));
127 }
128 if (std::optional<SkBlendMode> mode = as_BB(blender)->asBlendMode()) {
129 return sk_make_sp<SkBlendShader>(mode.value(), std::move(dst), std::move(src));
130 }
131
132 // This isn't a built-in blend mode; we might as well use a runtime effect to evaluate it.
133 const SkRuntimeEffect* blendEffect = GetKnownRuntimeEffect(StableKey::kBlend);
134
135 SkRuntimeEffect::ChildPtr children[] = {std::move(src), std::move(dst), std::move(blender)};
136 return blendEffect->makeShader(/*uniforms=*/{}, children);
137}
138
141 // Previous name
142 SkFlattenable::Register("SkShader_Blend", SkBlendShader::CreateProc);
143}
constexpr uint8_t kCustom_SkBlendMode
void SkBlendMode_AppendStages(SkBlendMode mode, SkRasterPipeline *p)
Definition: SkBlendMode.cpp:97
SkBlendMode
Definition: SkBlendMode.h:38
@ kSrcOver
r = s + (1-sa)*d
@ kLastMode
last valid value
@ kClear
r = 0
void SkRegisterBlendShaderFlattenable()
static float * append_two_shaders(const SkStageRec &rec, const SkShaders::MatrixRec &mRec, SkShader *s0, SkShader *s1)
SkBlenderBase * as_BB(SkBlender *blend)
Definition: SkBlenderBase.h:69
#define SK_REGISTER_FLATTENABLE(type)
static constexpr int SkRasterPipeline_kMaxStride
SkShaderBase * as_SB(SkShader *shader)
Definition: SkShaderBase.h:412
auto make(Ctor &&ctor) -> decltype(ctor(nullptr))
Definition: SkArenaAlloc.h:120
sk_sp< SkShader > dst() const
Definition: SkBlendShader.h:30
void flatten(SkWriteBuffer &) const override
bool appendStages(const SkStageRec &, const SkShaders::MatrixRec &) const override
SkBlendMode mode() const
Definition: SkBlendShader.h:32
sk_sp< SkShader > src() const
Definition: SkBlendShader.h:31
static void Register(const char name[], Factory)
void append(SkRasterPipelineOp, void *=nullptr)
sk_sp< SkShader > makeShader(sk_sp< const SkData > uniforms, sk_sp< SkShader > children[], size_t childCount, const SkMatrix *localMatrix=nullptr) const
virtual bool appendStages(const SkStageRec &, const SkShaders::MatrixRec &) const =0
bool rasterPipelineCoordsAreSeeded() const
Definition: SkShaderBase.h:130
T * get() const
Definition: SkRefCnt.h:303
const SkRuntimeEffect * GetKnownRuntimeEffect(StableKey stableKey)
SK_API sk_sp< SkShader > Blend(SkBlendMode mode, sk_sp< SkShader > dst, sk_sp< SkShader > src)
SK_API sk_sp< SkShader > Color(SkColor)
DEF_SWITCHES_START aot vmservice shared library Name of the *so containing AOT compiled Dart assets for launching the service isolate vm snapshot The VM snapshot data that will be memory mapped as read only SnapshotAssetPath must be present isolate snapshot The isolate snapshot data that will be memory mapped as read only SnapshotAssetPath must be present cache dir Path to the cache directory This is different from the persistent_cache_path in embedder which is used for Skia shader cache icu native lib Path to the library file that exports the ICU data vm service The hostname IP address on which the Dart VM Service should be served If not defaults to or::depending on whether ipv6 is specified vm service A custom Dart VM Service port The default is to pick a randomly available open port disable vm Disable the Dart VM Service The Dart VM Service is never available in release mode disable vm service Disable mDNS Dart VM Service publication Bind to the IPv6 localhost address for the Dart VM Service Ignored if vm service host is set endless trace buffer
Definition: switches.h:126
it will be possible to load the file into Perfetto s trace viewer disable asset Prevents usage of any non test fonts unless they were explicitly Loaded via prefetched default font Indicates whether the embedding started a prefetch of the default font manager before creating the engine run In non interactive mode
Definition: switches.h:228
dst
Definition: cp.py:12
SkRasterPipeline * fPipeline
Definition: SkEffectPriv.h:21
SkArenaAlloc * fAlloc
Definition: SkEffectPriv.h:22