Flutter Engine
The Flutter Engine
Classes | Public Types | Public Member Functions | Static Public Member Functions | Static Public Attributes | Friends | List of all members
SkMeshSpecification Class Reference

#include <SkMesh.h>

Inheritance diagram for SkMeshSpecification:
SkNVRefCnt< SkMeshSpecification >

Classes

struct  Attribute
 
struct  Result
 
struct  Varying
 

Public Types

using Uniform = SkRuntimeEffect::Uniform
 
using Child = SkRuntimeEffect::Child
 

Public Member Functions

 ~SkMeshSpecification ()
 
SkSpan< const Attributeattributes () const
 
size_t uniformSize () const
 
SkSpan< const Uniformuniforms () const
 
SkSpan< const Childchildren () const
 
const ChildfindChild (std::string_view name) const
 
const UniformfindUniform (std::string_view name) const
 
const AttributefindAttribute (std::string_view name) const
 
const VaryingfindVarying (std::string_view name) const
 
size_t stride () const
 
SkColorSpacecolorSpace () const
 
- Public Member Functions inherited from SkNVRefCnt< SkMeshSpecification >
 SkNVRefCnt ()
 
 ~SkNVRefCnt ()
 
bool unique () const
 
void ref () const
 
void unref () const
 
void deref () const
 
bool refCntGreaterThan (int32_t threadIsolatedTestCnt) const
 

Static Public Member Functions

static Result Make (SkSpan< const Attribute > attributes, size_t vertexStride, SkSpan< const Varying > varyings, const SkString &vs, const SkString &fs)
 
static Result Make (SkSpan< const Attribute > attributes, size_t vertexStride, SkSpan< const Varying > varyings, const SkString &vs, const SkString &fs, sk_sp< SkColorSpace > cs)
 
static Result Make (SkSpan< const Attribute > attributes, size_t vertexStride, SkSpan< const Varying > varyings, const SkString &vs, const SkString &fs, sk_sp< SkColorSpace > cs, SkAlphaType at)
 

Static Public Attributes

static constexpr size_t kMaxStride = 1024
 
static constexpr size_t kMaxAttributes = 8
 
static constexpr size_t kStrideAlignment = 4
 
static constexpr size_t kOffsetAlignment = 4
 
static constexpr size_t kMaxVaryings = 6
 

Friends

struct SkMeshSpecificationPriv
 

Detailed Description

A specification for custom meshes. Specifies the vertex buffer attributes and stride, the vertex program that produces a user-defined set of varyings, and a fragment program that ingests the interpolated varyings and produces local coordinates for shading and optionally a color.

The varyings must include a float2 named "position". If the passed varyings does not contain such a varying then one is implicitly added to the final specification and the SkSL Varyings struct described below. It is an error to have a varying named "position" that has a type other than float2.

The provided attributes and varyings are used to create Attributes and Varyings structs in SkSL that are used by the shaders. Each attribute from the Attribute span becomes a member of the SkSL Attributes struct and likewise for the varyings.

The signature of the vertex program must be: Varyings main(const Attributes).

The signature of the fragment program must be either: float2 main(const Varyings) or float2 main(const Varyings, out (half4|float4) color)

where the return value is the local coordinates that will be used to access SkShader. If the color variant is used, the returned color will be blended with SkPaint's SkShader (or SkPaint color in absence of a SkShader) using the SkBlender passed to SkCanvas drawMesh(). To use interpolated local space positions as the shader coordinates, equivalent to how SkPaths are shaded, return the position field from the Varying struct as the coordinates.

The vertex and fragment programs may both contain uniforms. Uniforms with the same name are assumed to be shared between stages. It is an error to specify uniforms in the vertex and fragment program with the same name but different types, dimensionality, or layouts.

Definition at line 65 of file SkMesh.h.

Member Typedef Documentation

◆ Child

Definition at line 107 of file SkMesh.h.

◆ Uniform

Definition at line 106 of file SkMesh.h.

Constructor & Destructor Documentation

◆ ~SkMeshSpecification()

SkMeshSpecification::~SkMeshSpecification ( )
default

Member Function Documentation

◆ attributes()

SkSpan< const Attribute > SkMeshSpecification::attributes ( ) const
inline

Definition at line 157 of file SkMesh.h.

157{ return SkSpan(fAttributes); }
SkSpan(Container &&) -> SkSpan< std::remove_pointer_t< decltype(std::data(std::declval< Container >()))> >

◆ children()

SkSpan< const Child > SkMeshSpecification::children ( ) const
inline

Provides basic info about individual children: names, indices and runtime effect type.

Definition at line 173 of file SkMesh.h.

173{ return SkSpan(fChildren); }

◆ colorSpace()

SkColorSpace * SkMeshSpecification::colorSpace ( ) const
inline

Definition at line 189 of file SkMesh.h.

189{ return fColorSpace.get(); }
T * get() const
Definition: SkRefCnt.h:303

◆ findAttribute()

const Attribute * SkMeshSpecification::findAttribute ( std::string_view  name) const

Returns a pointer to the named attribute, or nullptr if not found.

Definition at line 665 of file SkMesh.cpp.

665 {
666 for (const Attribute& attr : fAttributes) {
667 if (name == attr.name.c_str()) {
668 return &attr;
669 }
670 }
671 return nullptr;
672}
DEF_SWITCHES_START aot vmservice shared library name
Definition: switches.h:32

◆ findChild()

const Child * SkMeshSpecification::findChild ( std::string_view  name) const

Returns a pointer to the named child's description, or nullptr if not found.

Definition at line 656 of file SkMesh.cpp.

656 {
657 for (const Child& child : fChildren) {
658 if (child.name == name) {
659 return &child;
660 }
661 }
662 return nullptr;
663}

◆ findUniform()

const Uniform * SkMeshSpecification::findUniform ( std::string_view  name) const

Returns a pointer to the named uniform variable's description, or nullptr if not found.

Definition at line 647 of file SkMesh.cpp.

647 {
648 for (const Uniform& uniform : fUniforms) {
649 if (uniform.name == name) {
650 return &uniform;
651 }
652 }
653 return nullptr;
654}

◆ findVarying()

const Varying * SkMeshSpecification::findVarying ( std::string_view  name) const

Returns a pointer to the named varying, or nullptr if not found.

Definition at line 674 of file SkMesh.cpp.

674 {
675 for (const Varying& varying : fVaryings) {
676 if (name == varying.name.c_str()) {
677 return &varying;
678 }
679 }
680 return nullptr;
681}

◆ Make() [1/3]

SkMeshSpecification::Result SkMeshSpecification::Make ( SkSpan< const Attribute attributes,
size_t  vertexStride,
SkSpan< const Varying varyings,
const SkString vs,
const SkString fs 
)
static

If successful the return is a specification and an empty error string. Otherwise, it is a null specification a non-empty error string.

Parameters
attributesThe vertex attributes that will be consumed by 'vs'. Attributes need not be tightly packed but attribute offsets must be aligned to kOffsetAlignment and offset + size may not be greater than 'vertexStride'. At least one attribute is required.
vertexStrideThe offset between successive attribute values. This must be aligned to kStrideAlignment.
varyingsThe varyings that will be written by 'vs' and read by 'fs'. This may be empty.
vsThe vertex shader code that computes a vertex position and the varyings from the attributes.
fsThe fragment code that computes a local coordinate and optionally a color from the varyings. The local coordinate is used to sample SkShader.
csThe colorspace of the color produced by 'fs'. Ignored if 'fs's main() function does not have a color out param.
atThe alpha type of the color produced by 'fs'. Ignored if 'fs's main() function does not have a color out param. Cannot be kUnknown.

Definition at line 389 of file SkMesh.cpp.

393 {
394 return Make(attributes,
395 vertexStride,
396 varyings,
397 vs,
398 fs,
401}
@ kPremul_SkAlphaType
pixel components are premultiplied by alpha
Definition: SkAlphaType.h:29
static sk_sp< SkColorSpace > MakeSRGB()
static Result Make(SkSpan< const Attribute > attributes, size_t vertexStride, SkSpan< const Varying > varyings, const SkString &vs, const SkString &fs)
Definition: SkMesh.cpp:389
SkSpan< const Attribute > attributes() const
Definition: SkMesh.h:157

◆ Make() [2/3]

SkMeshSpecification::Result SkMeshSpecification::Make ( SkSpan< const Attribute attributes,
size_t  vertexStride,
SkSpan< const Varying varyings,
const SkString vs,
const SkString fs,
sk_sp< SkColorSpace cs 
)
static

Definition at line 403 of file SkMesh.cpp.

408 {
409 return Make(attributes, vertexStride, varyings, vs, fs, std::move(cs), kPremul_SkAlphaType);
410}

◆ Make() [3/3]

SkMeshSpecification::Result SkMeshSpecification::Make ( SkSpan< const Attribute attributes,
size_t  vertexStride,
SkSpan< const Varying varyings,
const SkString vs,
const SkString fs,
sk_sp< SkColorSpace cs,
SkAlphaType  at 
)
static

Definition at line 412 of file SkMesh.cpp.

418 {
419 SkString attributesStruct("struct Attributes {\n");
420 for (const auto& a : attributes) {
421 attributesStruct.appendf(" %s %s;\n", attribute_type_string(a.type), a.name.c_str());
422 }
423 attributesStruct.append("};\n");
424
425 bool userProvidedPositionVarying = false;
426 for (const auto& v : varyings) {
427 if (v.name.equals("position")) {
428 if (v.type != Varying::Type::kFloat2) {
429 return {nullptr, SkString("Varying \"position\" must have type float2.")};
430 }
431 userProvidedPositionVarying = true;
432 }
433 }
434
436 if (!userProvidedPositionVarying) {
437 // Even though we check the # of varyings in MakeFromSourceWithStructs we check here, too,
438 // to avoid overflow with + 1.
439 if (varyings.size() > kMaxVaryings - 1) {
440 RETURN_FAILURE("A maximum of %zu varyings is allowed.", kMaxVaryings);
441 }
442 for (const auto& v : varyings) {
443 tempVaryings.push_back(v);
444 }
445 tempVaryings.push_back(Varying{Varying::Type::kFloat2, SkString("position")});
446 varyings = tempVaryings;
447 }
448
449 SkString varyingStruct("struct Varyings {\n");
450 for (const auto& v : varyings) {
451 varyingStruct.appendf(" %s %s;\n", varying_type_string(v.type), v.name.c_str());
452 }
453 varyingStruct.append("};\n");
454
455 SkString fullVS;
456 fullVS.append(varyingStruct.c_str());
457 fullVS.append(attributesStruct.c_str());
458 fullVS.append(vs.c_str());
459
460 SkString fullFS;
461 fullFS.append(varyingStruct.c_str());
462 fullFS.append(fs.c_str());
463
464 return MakeFromSourceWithStructs(attributes,
465 vertexStride,
466 varyings,
467 fullVS,
468 fullFS,
469 std::move(cs),
470 at);
471}
static const char * attribute_type_string(Attribute::Type type)
Definition: SkMesh.cpp:186
#define RETURN_FAILURE(...)
Definition: SkMesh.cpp:60
static const char * varying_type_string(Varying::Type type)
Definition: SkMesh.cpp:197
static constexpr size_t kMaxVaryings
Definition: SkMesh.h:72
void append(const char text[])
Definition: SkString.h:203
const char * c_str() const
Definition: SkString.h:133
struct MyStruct a[10]

◆ stride()

size_t SkMeshSpecification::stride ( ) const
inline

Definition at line 187 of file SkMesh.h.

187{ return fStride; }

◆ uniforms()

SkSpan< const Uniform > SkMeshSpecification::uniforms ( ) const
inline

Provides info about individual uniforms including the offset into an SkData where each uniform value should be placed.

Definition at line 170 of file SkMesh.h.

170{ return SkSpan(fUniforms); }

◆ uniformSize()

size_t SkMeshSpecification::uniformSize ( ) const

Combined size of all 'uniform' variables. When creating a SkMesh with this specification provide an SkData of this size, containing values for all of those variables. Use uniforms() to get the offset of each uniform within the SkData.

Definition at line 642 of file SkMesh.cpp.

642 {
643 return fUniforms.empty() ? 0
644 : SkAlign4(fUniforms.back().offset + fUniforms.back().sizeInBytes());
645}
static constexpr T SkAlign4(T x)
Definition: SkAlign.h:16

Friends And Related Function Documentation

◆ SkMeshSpecificationPriv

friend struct SkMeshSpecificationPriv
friend

Definition at line 192 of file SkMesh.h.

Member Data Documentation

◆ kMaxAttributes

constexpr size_t SkMeshSpecification::kMaxAttributes = 8
staticconstexpr

Definition at line 69 of file SkMesh.h.

◆ kMaxStride

constexpr size_t SkMeshSpecification::kMaxStride = 1024
staticconstexpr

These values are enforced when creating a specification.

Definition at line 68 of file SkMesh.h.

◆ kMaxVaryings

constexpr size_t SkMeshSpecification::kMaxVaryings = 6
staticconstexpr

Definition at line 72 of file SkMesh.h.

◆ kOffsetAlignment

constexpr size_t SkMeshSpecification::kOffsetAlignment = 4
staticconstexpr

Definition at line 71 of file SkMesh.h.

◆ kStrideAlignment

constexpr size_t SkMeshSpecification::kStrideAlignment = 4
staticconstexpr

Definition at line 70 of file SkMesh.h.


The documentation for this class was generated from the following files: