Skip to content

Commit

Permalink
Get rid of MetricsPath.
Browse files Browse the repository at this point in the history
This lets us trim a raw path directly which means all the trim path logic from the editor can now be pushed down to the C++ runtime, even for the case where the rendering is still happening via Skia/Impeller, all our path operations can be done in C++ (without using RenderPath/MetricsPath/RenderMetricsPath/OnlyMetricsPath/etc). Basically, I needed to do this now to not have as many edge cases in the editor for the difference between using our runtime to render with PLS vs Flutter.

It also quite greatly simplifies the code! More lines removed than added!

Some tests will fail as I think we're using the MetricsPath in some other parts of the code, but that can be refactored following the patterns used here...

It also means that most of our runtime now speaks RawPath which means we can optimize/reserve memory ahead of time in a lot of cases (not doing that yet).

Diffs=
8486c3445 Get rid of MetricsPath. (#7371)

Co-authored-by: Luigi Rosso <[email protected]>
  • Loading branch information
luigi-rosso and luigi-rosso committed Jun 7, 2024
1 parent dd5cc31 commit fa6e598
Show file tree
Hide file tree
Showing 27 changed files with 322 additions and 539 deletions.
2 changes: 1 addition & 1 deletion .rive_head
Original file line number Diff line number Diff line change
@@ -1 +1 @@
085f5bd2dce2d25561b00eebc5f7118a2c8769eb
8486c344528d5f7f84de1984510064a7d0788e73
2 changes: 2 additions & 0 deletions include/rive/artboard.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "rive/text/text_value_run.hpp"
#include "rive/event.hpp"
#include "rive/audio/audio_engine.hpp"
#include "rive/math/raw_path.hpp"

#include <queue>
#include <vector>
Expand Down Expand Up @@ -56,6 +57,7 @@ class Artboard : public ArtboardBase, public CoreContext, public ShapePaintConta
bool m_HasChangedDrawOrderInLastUpdate = false;

unsigned int m_DirtDepth = 0;
RawPath m_backgroundRawPath;
rcp<RenderPath> m_BackgroundPath;
rcp<RenderPath> m_ClipPath;
Factory* m_Factory = nullptr;
Expand Down
4 changes: 1 addition & 3 deletions include/rive/constraints/follow_path_constraint.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@
#define _RIVE_FOLLOW_PATH_CONSTRAINT_HPP_
#include "rive/generated/constraints/follow_path_constraint_base.hpp"
#include "rive/math/transform_components.hpp"
#include "rive/shapes/metrics_path.hpp"
#include <stdio.h>
#include "rive/math/contour_measure.hpp"
namespace rive
{
class FollowPathConstraint : public FollowPathConstraintBase
{

public:
void distanceChanged() override;
void orientChanged() override;
Expand Down
79 changes: 0 additions & 79 deletions include/rive/shapes/metrics_path.hpp

This file was deleted.

5 changes: 4 additions & 1 deletion include/rive/shapes/paint/fill.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ class Fill : public FillBase
public:
RenderPaint* initRenderPaint(ShapePaintMutator* mutator) override;
PathSpace pathSpace() const override;
void draw(Renderer* renderer, CommandPath* path, RenderPaint* paint) override;
void draw(Renderer* renderer,
CommandPath* path,
const RawPath* rawPath,
RenderPaint* paint) override;
void applyTo(RenderPaint* renderPaint, float opacityModifier) const override;
};
} // namespace rive
Expand Down
16 changes: 13 additions & 3 deletions include/rive/shapes/paint/shape_paint.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#include "rive/shapes/paint/blend_mode.hpp"
#include "rive/shapes/paint/shape_paint_mutator.hpp"
#include "rive/shapes/path_space.hpp"
#include "rive/math/raw_path.hpp"

namespace rive
{
class RenderPaint;
Expand All @@ -30,9 +32,17 @@ class ShapePaint : public ShapePaintBase

virtual PathSpace pathSpace() const = 0;

void draw(Renderer* renderer, CommandPath* path) { draw(renderer, path, renderPaint()); }

virtual void draw(Renderer* renderer, CommandPath* path, RenderPaint* paint) = 0;
void draw(Renderer* renderer, CommandPath* path, const RawPath* rawPath = nullptr)
{
draw(renderer, path, rawPath, renderPaint());
}

virtual void draw(Renderer* renderer,
CommandPath* path,
// When every CommandPath stores a RawPath we can get rid
// of this argument.
const RawPath* rawPath,
RenderPaint* paint) = 0;

RenderPaint* renderPaint() { return m_RenderPaint.get(); }

Expand Down
5 changes: 4 additions & 1 deletion include/rive/shapes/paint/stroke.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ class Stroke : public StrokeBase
public:
RenderPaint* initRenderPaint(ShapePaintMutator* mutator) override;
PathSpace pathSpace() const override;
void draw(Renderer* renderer, CommandPath* path, RenderPaint* paint) override;
void draw(Renderer* renderer,
CommandPath* path,
const RawPath* rawPath,
RenderPaint* paint) override;
void addStrokeEffect(StrokeEffect* effect);
bool hasStrokeEffect() { return m_Effect != nullptr; }
void invalidateEffects();
Expand Down
4 changes: 2 additions & 2 deletions include/rive/shapes/paint/stroke_effect.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ namespace rive
{
class Factory;
class RenderPath;
class MetricsPath;
class RawPath;

class StrokeEffect
{
public:
virtual ~StrokeEffect() {}
virtual RenderPath* effectPath(MetricsPath* source, Factory*) = 0;
virtual RenderPath* effectPath(const RawPath& source, Factory*) = 0;
virtual void invalidateEffect() = 0;
};
} // namespace rive
Expand Down
30 changes: 25 additions & 5 deletions include/rive/shapes/paint/trim_path.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,44 @@
#include "rive/generated/shapes/paint/trim_path_base.hpp"
#include "rive/shapes/paint/stroke_effect.hpp"
#include "rive/renderer.hpp"
#include "rive/math/raw_path.hpp"
#include "rive/math/contour_measure.hpp"

namespace rive
{
class TrimPath : public TrimPathBase, public StrokeEffect
enum class TrimPathMode : unsigned char
{
private:
rcp<RenderPath> m_TrimmedPath;
RenderPath* m_RenderPath = nullptr;
sequential = 1,
synchronized = 2

};
class ContourMeasure;

class TrimPath : public TrimPathBase, public StrokeEffect
{
public:
StatusCode onAddedClean(CoreContext* context) override;
RenderPath* effectPath(MetricsPath* source, Factory*) override;
RenderPath* effectPath(const RawPath& source, Factory*) override;
void invalidateEffect() override;

void startChanged() override;
void endChanged() override;
void offsetChanged() override;
void modeValueChanged() override;

TrimPathMode mode() const { return (TrimPathMode)modeValue(); }

StatusCode onAddedDirty(CoreContext* context) override;

const RawPath& rawPath() const { return m_rawPath; }

private:
void invalidateTrim();
void trimRawPath(const RawPath& source);
RawPath m_rawPath;
std::vector<rcp<ContourMeasure>> m_contours;
rcp<RenderPath> m_trimmedPath;
RenderPath* m_renderPath = nullptr;
};
} // namespace rive

Expand Down
8 changes: 4 additions & 4 deletions include/rive/shapes/path.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "rive/command_path.hpp"
#include "rive/generated/shapes/path_base.hpp"
#include "rive/math/mat2d.hpp"
#include "rive/math/raw_path.hpp"
#include "rive/shapes/shape_paint_container.hpp"
#include <vector>

Expand Down Expand Up @@ -37,18 +38,18 @@ class Path : public PathBase
{
protected:
Shape* m_Shape = nullptr;
rcp<CommandPath> m_CommandPath;
std::vector<PathVertex*> m_Vertices;
bool m_deferredPathDirt = false;
PathSpace m_DefaultPathSpace = PathSpace::Neither;
RawPath m_rawPath;

public:
Shape* shape() const { return m_Shape; }
StatusCode onAddedClean(CoreContext* context) override;
void buildDependencies() override;
virtual const Mat2D& pathTransform() const;
bool collapse(bool value) override;
CommandPath* commandPath() const { return m_CommandPath.get(); }
const RawPath& rawPath() const { return m_rawPath; }
void update(ComponentDirt value) override;

void addDefaultPathSpace(PathSpace space);
Expand All @@ -67,8 +68,7 @@ class Path : public PathBase
std::vector<PathVertex*>& vertices() { return m_Vertices; }
#endif

// pour ourselves into a command-path
void buildPath(CommandPath&) const;
void buildPath(RawPath&) const;
};
} // namespace rive

Expand Down
24 changes: 16 additions & 8 deletions include/rive/shapes/path_composer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,38 @@
#define _RIVE_PATH_COMPOSER_HPP_
#include "rive/component.hpp"
#include "rive/refcnt.hpp"
#include "rive/math/raw_path.hpp"

namespace rive
{
class Shape;
class CommandPath;
class RenderPath;
class PathComposer : public Component
{
private:
Shape* m_Shape;
rcp<CommandPath> m_LocalPath;
rcp<CommandPath> m_WorldPath;
bool m_deferredPathDirt;

public:
PathComposer(Shape* shape);
Shape* shape() const { return m_Shape; }
Shape* shape() const { return m_shape; }
void buildDependencies() override;
void onDirty(ComponentDirt dirt) override;
void update(ComponentDirt value) override;

CommandPath* localPath() const { return m_LocalPath.get(); }
CommandPath* worldPath() const { return m_WorldPath.get(); }
CommandPath* localPath() const { return m_localPath.get(); }
CommandPath* worldPath() const { return m_worldPath.get(); }

const RawPath& localRawPath() const { return m_localRawPath; }
const RawPath& worldRawPath() const { return m_worldRawPath; }

void pathCollapseChanged();

private:
Shape* m_shape;
RawPath m_localRawPath;
RawPath m_worldRawPath;
rcp<CommandPath> m_localPath;
rcp<CommandPath> m_worldPath;
bool m_deferredPathDirt;
};
} // namespace rive
#endif
2 changes: 0 additions & 2 deletions include/rive/shapes/shape_paint_container.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@ class ShapePaintContainer

void invalidateStrokeEffects();

rcp<CommandPath> makeCommandPath(PathSpace space);

void propagateOpacity(float opacity);

#ifdef TESTING
Expand Down
7 changes: 5 additions & 2 deletions src/artboard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,10 @@ void Artboard::update(ComponentDirt value)
clip = bg;
}
m_ClipPath = factory()->makeRenderPath(clip);
m_BackgroundPath = factory()->makeRenderPath(bg);

m_backgroundRawPath.addRect(bg);
m_BackgroundPath->rewind();
m_backgroundRawPath.addTo(m_BackgroundPath.get());
}
if (hasDirt(value, ComponentDirt::RenderOpacity))
{
Expand Down Expand Up @@ -602,7 +605,7 @@ void Artboard::draw(Renderer* renderer, DrawOption option)
{
for (auto shapePaint : m_ShapePaints)
{
shapePaint->draw(renderer, m_BackgroundPath.get());
shapePaint->draw(renderer, m_BackgroundPath.get(), &m_backgroundRawPath);
}
}

Expand Down
4 changes: 1 addition & 3 deletions src/constraints/follow_path_constraint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
#include "rive/math/contour_measure.hpp"
#include "rive/math/mat2d.hpp"
#include "rive/math/math_types.hpp"
#include "rive/shapes/metrics_path.hpp"
#include "rive/shapes/path.hpp"
#include "rive/shapes/shape.hpp"
#include "rive/transform_component.hpp"
Expand Down Expand Up @@ -150,8 +149,7 @@ void FollowPathConstraint::update(ComponentDirt value)
m_contours.clear();
for (auto path : paths)
{
auto commandPath = static_cast<MetricsPath*>(path->commandPath());
commandPath->addToRawPath(m_rawPath, path->pathTransform());
m_rawPath.addPath(path->rawPath(), &path->pathTransform());
}

auto measure = ContourMeasureIter(&m_rawPath);
Expand Down
Loading

0 comments on commit fa6e598

Please sign in to comment.