Skip to content

Commit

Permalink
Particle cleanup: split SkParticleBinding out of SkParticleEffect
Browse files Browse the repository at this point in the history
Also simplify type registration.

Change-Id: Ia47febb2ae2cd5821476c3dd33a688b688aa6d6d
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/238359
Commit-Queue: Brian Osman <[email protected]>
Reviewed-by: Brian Osman <[email protected]>
  • Loading branch information
brianosman authored and Skia Commit-Bot committed Aug 30, 2019
1 parent d4efe68 commit 2aa85df
Show file tree
Hide file tree
Showing 8 changed files with 359 additions and 335 deletions.
5 changes: 1 addition & 4 deletions modules/canvaskit/particles_bindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
#include "include/core/SkCanvas.h"
#include "include/core/SkTypes.h"
#include "include/utils/SkRandom.h"
#include "modules/particles/include/SkParticleDrawable.h"
#include "modules/particles/include/SkParticleEffect.h"
#include "modules/particles/include/SkParticleSerialization.h"

Expand All @@ -29,9 +28,7 @@ EMSCRIPTEN_BINDINGS(Particles) {
function("MakeParticles", optional_override([](std::string json)->sk_sp<SkParticleEffect> {
static bool didInit = false;
if (!didInit) {
REGISTER_REFLECTED(SkReflected);
SkParticleBinding::RegisterBindingTypes();
SkParticleDrawable::RegisterDrawableTypes();
SkParticleEffect::RegisterParticleTypes();
didInit = true;
}
SkRandom r;
Expand Down
75 changes: 75 additions & 0 deletions modules/particles/include/SkParticleBinding.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
* Copyright 2019 Google LLC
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/

#ifndef SkParticleBinding_DEFINED
#define SkParticleBinding_DEFINED

#include "include/core/SkString.h"
#include "modules/particles/include/SkReflected.h"
#include "src/sksl/SkSLExternalValue.h"

#include <memory>

struct SkCurve;
struct SkColorCurve;
class SkRandom;

namespace SkSL {
class Compiler;
}

class SkParticleExternalValue : public SkSL::ExternalValue {
public:
SkParticleExternalValue(const char* name, SkSL::Compiler& compiler, const SkSL::Type& type)
: SkSL::ExternalValue(name, type)
, fCompiler(compiler)
, fRandom(nullptr) {
}

void setRandom(SkRandom* random) { fRandom = random; }

protected:
SkSL::Compiler& fCompiler;
SkRandom* fRandom;
};

class SkParticleBinding : public SkReflected {
public:
SkParticleBinding(const char* name = "name") : fName(name) {}

REFLECTED_ABSTRACT(SkParticleBinding, SkReflected)

void visitFields(SkFieldVisitor* v) override;
virtual std::unique_ptr<SkParticleExternalValue> toValue(SkSL::Compiler&) = 0;

static void RegisterBindingTypes();

/*
* All SkParticleBinding objects expose a particular native object to an effect's SkSL code.
* In all cases, the 'name' is the symbol that will be used to access the object from the SkSL.
* Each binding is a callable object, so the SkSL name behaves like a function. The behavior of
* each kind of binding is described below.
*/

// Binds an SkCurve to an effect's SkSL. The curve is a one-dimensional function, described
// in SkCurve.h. It is called in the SkSL as 'name(t)', and returns a single float value.
static sk_sp<SkParticleBinding> MakeCurve(const char* name, const SkCurve& curve);

// Binds an SkColorCurve to an effect's SkSL. The curve is a one-dimensional, function,
// described in SkCurve.h. It is called in the SkSL as 'name(t)', and returns a float4 value.
static sk_sp<SkParticleBinding> MakeColorCurve(const char* name, const SkColorCurve& curve);

// Binds an SkPath to an effect's SkSL. The path is specified using SVG syntax. It is called
// in the SkSL as 'name(t)'. 't' is a normalized distance along the path. This returns a float4
// value, containing the position in .xy, and the normal in .zw.
static sk_sp<SkParticleBinding> MakePathBinding(const char* name, const char* path);

protected:
SkString fName;
};

#endif // SkParticleBinding_DEFINED
43 changes: 4 additions & 39 deletions modules/particles/include/SkParticleEffect.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,56 +14,19 @@
#include "include/private/SkTemplates.h"
#include "include/utils/SkRandom.h"
#include "modules/particles/include/SkParticleData.h"
#include "modules/particles/include/SkReflected.h"

#include <memory>

class SkCanvas;
struct SkCurve;
struct SkColorCurve;
class SkFieldVisitor;
class SkParticleBinding;
class SkParticleDrawable;
class SkParticleExternalValue;

namespace SkSL {
struct ByteCode;
class Compiler;
}

class SkParticleBinding : public SkReflected {
public:
SkParticleBinding(const char* name = "name") : fName(name) {}

REFLECTED_ABSTRACT(SkParticleBinding, SkReflected)

void visitFields(SkFieldVisitor* v) override;
virtual std::unique_ptr<SkParticleExternalValue> toValue(SkSL::Compiler&) = 0;

static void RegisterBindingTypes();

/*
* All SkParticleBinding objects expose a particular native object to an effect's SkSL code.
* In all cases, the 'name' is the symbol that will be used to access the object from the SkSL.
* Each binding is a callable object, so the SkSL name behaves like a function. The behavior of
* each kind of binding is described below.
*/

// Binds an SkCurve to an effect's SkSL. The curve is a one-dimensional function, described
// in SkCurve.h. It is called in the SkSL as 'name(t)', and returns a single float value.
static sk_sp<SkParticleBinding> MakeCurve(const char* name, const SkCurve& curve);

// Binds an SkColorCurve to an effect's SkSL. The curve is a one-dimensional, function,
// described in SkCurve.h. It is called in the SkSL as 'name(t)', and returns a float4 value.
static sk_sp<SkParticleBinding> MakeColorCurve(const char* name, const SkColorCurve& curve);

// Binds an SkPath to an effect's SkSL. The path is specified using SVG syntax. It is called
// in the SkSL as 'name(t)'. 't' is a normalized distance along the path. This returns a float4
// value, containing the position in .xy, and the normal in .zw.
static sk_sp<SkParticleBinding> MakePathBinding(const char* name, const char* path);

protected:
SkString fName;
};

class SkParticleEffectParams : public SkRefCnt {
public:
SkParticleEffectParams();
Expand Down Expand Up @@ -137,6 +100,8 @@ class SkParticleEffect : public SkRefCnt {
bool isAlive() const { return fSpawnTime >= 0; }
int getCount() const { return fCount; }

static void RegisterParticleTypes();

private:
void setCapacity(int capacity);

Expand Down
1 change: 1 addition & 0 deletions modules/particles/particles.gni
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ _src = get_path_info("src", "abspath")

skia_particle_sources = [
"$_src/SkCurve.cpp",
"$_src/SkParticleBinding.cpp",
"$_src/SkParticleDrawable.cpp",
"$_src/SkParticleEffect.cpp",
"$_src/SkReflected.cpp",
Expand Down
Loading

0 comments on commit 2aa85df

Please sign in to comment.