8 #ifndef SkRuntimeEffect_DEFINED
9 #define SkRuntimeEffect_DEFINED
21 #include "include/private/SkSLSampleUsage.h"
22 #include "include/private/base/SkOnce.h"
23 #include "include/private/base/SkTemplates.h"
24 #include "include/private/base/SkTo.h"
25 #include "include/private/base/SkTypeTraits.h"
26 #include "include/sksl/SkSLDebugTrace.h"
27 #include "include/sksl/SkSLVersion.h"
35 #include <string_view>
43 class FunctionDefinition;
45 enum class ProgramKind : int8_t;
46 struct ProgramSettings;
96 kHalfPrecision_Flag = 0x10,
105 bool isArray()
const {
return SkToBool(this->flags & kArray_Flag); }
106 bool isColor()
const {
return SkToBool(this->flags & kColor_Flag); }
128 bool forceUnoptimized =
false;
132 friend class SkRuntimeEffectPriv;
136 bool allowPrivateAccess =
false;
143 SkSL::Version maxVersionAllowed = SkSL::Version::k100;
165 return MakeForColorFilter(std::move(sksl),
Options{});
172 return MakeForShader(std::move(sksl),
Options{});
179 return MakeForBlender(std::move(sksl),
Options{});
193 std::optional<ChildType>
type()
const;
211 const SkMatrix* localMatrix =
nullptr)
const;
213 SkSpan<const ChildPtr> children,
214 const SkMatrix* localMatrix =
nullptr)
const;
219 size_t childCount)
const;
221 SkSpan<const ChildPtr> children)
const;
224 SkSpan<const ChildPtr> children = {})
const;
251 SkSpan<const Uniform>
uniforms()
const {
return SkSpan(fUniforms); }
252 SkSpan<const Child>
children()
const {
return SkSpan(fChildren); }
270 kUsesSampleCoords_Flag = 0x001,
271 kAllowColorFilter_Flag = 0x002,
272 kAllowShader_Flag = 0x004,
273 kAllowBlender_Flag = 0x008,
274 kSamplesOutsideMain_Flag = 0x010,
275 kUsesColorTransform_Flag = 0x020,
276 kAlwaysOpaque_Flag = 0x040,
277 kAlphaUnchanged_Flag = 0x080,
278 kDisableOptimization_Flag = 0x100,
282 const Options& options,
283 const SkSL::FunctionDefinition& main,
284 std::vector<Uniform>&& uniforms,
285 std::vector<Child>&& children,
286 std::vector<SkSL::SampleUsage>&& sampleUsages,
291 static Result MakeFromSource(
SkString sksl,
const Options& options, SkSL::ProgramKind kind);
293 static Result MakeInternal(std::unique_ptr<SkSL::Program> program,
294 const Options& options,
295 SkSL::ProgramKind kind);
297 static SkSL::ProgramSettings MakeSettings(
const Options& options);
299 uint32_t hash()
const {
return fHash; }
300 bool usesSampleCoords()
const {
return (fFlags & kUsesSampleCoords_Flag); }
301 bool samplesOutsideMain()
const {
return (fFlags & kSamplesOutsideMain_Flag); }
302 bool usesColorTransform()
const {
return (fFlags & kUsesColorTransform_Flag); }
303 bool alwaysOpaque()
const {
return (fFlags & kAlwaysOpaque_Flag); }
304 bool isAlphaUnchanged()
const {
return (fFlags & kAlphaUnchanged_Flag); }
306 const SkSL::RP::Program* getRPProgram(SkSL::DebugTracePriv* debugTrace)
const;
308 #if defined(SK_GANESH)
309 friend class GrSkSLFP;
310 friend class GrGLSLSkSLFP;
313 friend class SkRuntimeShader;
314 friend class SkRuntimeBlender;
315 friend class SkRuntimeColorFilter;
317 friend class SkRuntimeEffectPriv;
321 std::unique_ptr<SkSL::Program> fBaseProgram;
322 std::unique_ptr<SkSL::RP::Program> fRPProgram;
323 mutable SkOnce fCompileRPProgramOnce;
324 const SkSL::FunctionDefinition& fMain;
325 std::vector<Uniform> fUniforms;
326 std::vector<Child> fChildren;
327 std::vector<SkSL::SampleUsage> fSampleUsages;
341 template <
typename T>
345 SkDEBUGFAIL(
"Assigning to missing variable");
346 }
else if (
sizeof(val) != fVar->sizeInBytes()) {
347 SkDEBUGFAIL(
"Incorrect value size");
349 memcpy(SkTAddOffset<void>(fOwner->writableUniformData(), fVar->offset),
357 SkDEBUGFAIL(
"Assigning to missing variable");
358 }
else if (fVar->sizeInBytes() != 9 *
sizeof(
float)) {
359 SkDEBUGFAIL(
"Incorrect value size");
361 float* data = SkTAddOffset<float>(fOwner->writableUniformData(),
362 (ptrdiff_t)fVar->offset);
363 data[0] = val.
get(0); data[1] = val.
get(3); data[2] = val.
get(6);
364 data[3] = val.
get(1); data[4] = val.
get(4); data[5] = val.
get(7);
365 data[6] = val.
get(2); data[7] = val.
get(5); data[8] = val.
get(8);
370 template <
typename T>
371 bool set(
const T val[],
const int count) {
372 static_assert(std::is_trivially_copyable<T>::value,
"Value must be trivial copyable");
374 SkDEBUGFAIL(
"Assigning to missing variable");
376 }
else if (
sizeof(T) * count != fVar->sizeInBytes()) {
377 SkDEBUGFAIL(
"Incorrect value size");
380 memcpy(SkTAddOffset<void>(fOwner->writableUniformData(), fVar->offset),
381 val,
sizeof(T) * count);
393 SkDEBUGFAIL(
"Assigning to missing child");
395 fOwner->fChildren[(size_t)fChild->index] = std::move(val);
402 SkDEBUGFAIL(
"Assigning to missing child");
421 SkSpan<const SkRuntimeEffect::ChildPtr>
children()
const {
return fChildren; }
426 : fEffect(std::move(effect))
427 , fUniforms(
SkData::MakeZeroInitialized(fEffect->uniformSize()))
428 , fChildren(fEffect->children().size()) {}
430 : fEffect(std::move(effect))
431 , fUniforms(std::move(uniforms))
432 , fChildren(fEffect->children().size()) {}
441 void* writableUniformData() {
442 if (!fUniforms->unique()) {
445 return fUniforms->writable_data();
450 std::vector<SkRuntimeEffect::ChildPtr> fChildren;
487 friend class SkRuntimeImageFilter;
SkBlender represents a custom blend function in the Skia pipeline.
Definition: SkBlender.h:19
ColorFilters are optional objects in the drawing pipeline.
Definition: SkColorFilter.h:35
SkData holds an immutable data buffer.
Definition: SkData.h:25
static sk_sp< SkData > MakeWithCopy(const void *data, size_t length)
Create a new dataref by copying the specified data.
SkFlattenable is the base class for objects that need to be flattened into a data stream for either t...
Definition: SkFlattenable.h:28
SkMatrix holds a 3x3 matrix for transforming coordinates.
Definition: SkMatrix.h:53
SkScalar get(int index) const
Returns one matrix value.
Definition: SkMatrix.h:391
Definition: SkRefCnt.h:119
SkRuntimeBlendBuilder is a utility to simplify creation and uniform setup of runtime blenders.
Definition: SkRuntimeEffect.h:507
sk_sp< SkBlender > makeBlender() const
SkRuntimeBlendBuilder(sk_sp< SkRuntimeEffect >)
SkRuntimeBlendBuilder(const SkRuntimeBlendBuilder &)=delete
SkRuntimeBlendBuilder & operator=(const SkRuntimeBlendBuilder &)=delete
SkRuntimeColorFilterBuilder makes it easy to setup and assign uniforms to runtime color filters.
Definition: SkRuntimeEffect.h:493
~SkRuntimeColorFilterBuilder()
SkRuntimeColorFilterBuilder(sk_sp< SkRuntimeEffect >)
SkRuntimeColorFilterBuilder(const SkRuntimeColorFilterBuilder &)=delete
SkRuntimeColorFilterBuilder & operator=(const SkRuntimeColorFilterBuilder &)=delete
sk_sp< SkColorFilter > makeColorFilter() const
Base class for SkRuntimeShaderBuilder, defined below.
Definition: SkRuntimeEffect.h:333
BuilderUniform uniform(std::string_view name)
Definition: SkRuntimeEffect.h:415
SkSpan< const SkRuntimeEffect::ChildPtr > children() const
Definition: SkRuntimeEffect.h:421
const SkRuntimeEffect * effect() const
Definition: SkRuntimeEffect.h:413
SkRuntimeEffectBuilder(sk_sp< SkRuntimeEffect > effect)
Definition: SkRuntimeEffect.h:425
BuilderChild child(std::string_view name)
Definition: SkRuntimeEffect.h:416
SkRuntimeEffectBuilder(const SkRuntimeEffectBuilder &)=default
SkRuntimeEffectBuilder(sk_sp< SkRuntimeEffect > effect, sk_sp< SkData > uniforms)
Definition: SkRuntimeEffect.h:429
SkRuntimeEffectBuilder & operator=(SkRuntimeEffectBuilder &&)=delete
sk_sp< const SkData > uniforms() const
Definition: SkRuntimeEffect.h:420
SkRuntimeEffectBuilder()=delete
SkRuntimeEffectBuilder(SkRuntimeEffectBuilder &&)=default
SkRuntimeEffectBuilder & operator=(const SkRuntimeEffectBuilder &)=delete
Definition: SkRuntimeEffect.h:183
SkBlender * blender() const
ChildPtr(sk_sp< SkFlattenable > f)
ChildPtr(sk_sp< SkBlender > b)
Definition: SkRuntimeEffect.h:188
std::optional< ChildType > type() const
SkColorFilter * colorFilter() const
ChildPtr(sk_sp< SkShader > s)
Definition: SkRuntimeEffect.h:186
SkFlattenable * flattenable() const
Definition: SkRuntimeEffect.h:198
std::true_type sk_is_trivially_relocatable
Definition: SkRuntimeEffect.h:200
SkShader * shader() const
ChildPtr(sk_sp< SkColorFilter > cf)
Definition: SkRuntimeEffect.h:187
Definition: SkRuntimeEffect.h:123
Definition: SkRuntimeEffect.h:59
size_t uniformSize() const
const Uniform * findUniform(std::string_view name) const
sk_sp< SkShader > makeShader(sk_sp< const SkData > uniforms, SkSpan< const ChildPtr > children, const SkMatrix *localMatrix=nullptr) const
static TracedShader MakeTraced(sk_sp< SkShader > shader, const SkIPoint &traceCoord)
SkSpan< const Uniform > uniforms() const
Definition: SkRuntimeEffect.h:251
static void RegisterFlattenables()
sk_sp< SkShader > makeShader(sk_sp< const SkData > uniforms, sk_sp< SkShader > children[], size_t childCount, const SkMatrix *localMatrix=nullptr) const
sk_sp< SkColorFilter > makeColorFilter(sk_sp< const SkData > uniforms) const
bool allowColorFilter() const
Definition: SkRuntimeEffect.h:262
const std::string & source() const
~SkRuntimeEffect() override
sk_sp< SkColorFilter > makeColorFilter(sk_sp< const SkData > uniforms, sk_sp< SkColorFilter > children[], size_t childCount) const
static Result MakeForColorFilter(SkString sksl, const Options &)
static Result MakeForBlender(SkString sksl, const Options &)
bool allowBlender() const
Definition: SkRuntimeEffect.h:263
sk_sp< SkBlender > makeBlender(sk_sp< const SkData > uniforms, SkSpan< const ChildPtr > children={}) const
sk_sp< SkColorFilter > makeColorFilter(sk_sp< const SkData > uniforms, SkSpan< const ChildPtr > children) const
static Result MakeForShader(SkString sksl)
Definition: SkRuntimeEffect.h:171
static Result MakeForColorFilter(SkString sksl)
Definition: SkRuntimeEffect.h:164
const Child * findChild(std::string_view name) const
bool allowShader() const
Definition: SkRuntimeEffect.h:261
static Result MakeForBlender(SkString sksl)
Definition: SkRuntimeEffect.h:178
SkSpan< const Child > children() const
Definition: SkRuntimeEffect.h:252
ChildType
Definition: SkRuntimeEffect.h:111
static Result MakeForShader(SkString sksl, const Options &)
SkRuntimeShaderBuilder is a utility to simplify creating SkShader objects from SkRuntimeEffects.
Definition: SkRuntimeEffect.h:473
~SkRuntimeShaderBuilder()
SkRuntimeShaderBuilder(sk_sp< SkRuntimeEffect >)
SkRuntimeShaderBuilder(const SkRuntimeShaderBuilder &)=default
sk_sp< SkShader > makeShader(const SkMatrix *localMatrix=nullptr) const
Shaders specify the source color(s) for what is being drawn.
Definition: SkShader.h:35
Light weight class for managing strings.
Definition: SkString.h:118
Definition: SkRuntimeEffect.h:49
Definition: SkCapabilities.h:15
Definition: SkRuntimeEffect.h:390
const SkRuntimeEffect::Child * fChild
Definition: SkRuntimeEffect.h:410
BuilderChild & operator=(sk_sp< T > val)
Definition: SkRuntimeEffect.h:391
SkRuntimeEffectBuilder * fOwner
Definition: SkRuntimeEffect.h:409
BuilderChild & operator=(std::nullptr_t)
Definition: SkRuntimeEffect.h:400
Definition: SkRuntimeEffect.h:117
ChildType type
Definition: SkRuntimeEffect.h:119
int index
Definition: SkRuntimeEffect.h:120
std::string_view name
Definition: SkRuntimeEffect.h:118
Definition: SkRuntimeEffect.h:148
sk_sp< SkRuntimeEffect > effect
Definition: SkRuntimeEffect.h:149
SkString errorText
Definition: SkRuntimeEffect.h:150
Creates a new Runtime Effect patterned after an already-existing one.
Definition: SkRuntimeEffect.h:238
sk_sp< SkShader > shader
Definition: SkRuntimeEffect.h:239
sk_sp< SkSL::DebugTrace > debugTrace
Definition: SkRuntimeEffect.h:240