[skrive] Initial render plumbing

TBR=
Change-Id: I425463c2f6cf4eda510ec573517472e10bdedb68
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/299137
Reviewed-by: Florin Malita <fmalita@chromium.org>
Commit-Queue: Florin Malita <fmalita@google.com>
Commit-Queue: Florin Malita <fmalita@chromium.org>
This commit is contained in:
Florin Malita 2020-06-25 12:29:52 -04:00 committed by Skia Commit-Bot
parent b795bea220
commit 3d273d0034
8 changed files with 208 additions and 70 deletions

View File

@ -70,6 +70,11 @@ public:
void revalidate();
// probably not the right place
void render(SkCanvas* canvas) const {
this->onRender(canvas);
}
protected:
enum class Type : uint32_t {
kNode,
@ -85,6 +90,7 @@ protected:
bool hasInval() const { return fDirty; }
virtual void onRevalidate() = 0;
virtual void onRender(SkCanvas*) const;
private:
friend class Node; // parent access
@ -108,8 +114,17 @@ public:
protected:
explicit TransformableComponent(Type t) : INHERITED(t) {}
private:
class ScopedTransformContext final {
public:
ScopedTransformContext(const TransformableComponent*, SkCanvas*);
~ScopedTransformContext();
private:
SkCanvas* fCanvas;
const int fRestoreCount;
};
private:
using INHERITED = Component;
};
@ -121,17 +136,95 @@ public:
void addChild(sk_sp<Component>);
const std::vector<sk_sp<Component>>& children() const { return fChildren; }
protected:
explicit Node(Type t) : INHERITED(t) {}
void onRevalidate() override;
void onRender(SkCanvas*) const override;
private:
std::vector<sk_sp<Component>> fChildren;
using INHERITED = TransformableComponent;
};
class Paint : public Component {
public:
ACTOR_ATTR(Opacity , float , 1 )
ACTOR_ATTR(FillRule , SkPathFillType, SkPathFillType::kWinding )
ACTOR_ATTR(StrokeWidth, float , 1 )
ACTOR_ATTR(StrokeCap , SkPaint::Cap , SkPaint::Cap::kButt_Cap )
ACTOR_ATTR(StrokeJoin , SkPaint::Join , SkPaint::Join::kMiter_Join)
enum class StrokeTrim : uint8_t { kOff, kSequential, kSynced };
ACTOR_ATTR(StrokeTrim , StrokeTrim, StrokeTrim::kOff)
ACTOR_ATTR(StrokeTrimStart , float , 0)
ACTOR_ATTR(StrokeTrimEnd , float , 0)
ACTOR_ATTR(StrokeTrimOffset, float , 0)
void apply(SkPaint* paint) const {
this->onApply(paint);
}
SkPaint::Style style() const { return fStyle; }
protected:
Paint(Type t, SkPaint::Style style) : INHERITED(t), fStyle(style) {}
virtual void onApply(SkPaint*) const;
private:
const SkPaint::Style fStyle;
using INHERITED = Component;
};
class ColorPaint final : public Paint {
public:
explicit ColorPaint(SkPaint::Style style) : INHERITED(Type::kColorPaint, style) {}
ACTOR_ATTR(Color, SkColor4f, SkColors::kBlack)
private:
void onRevalidate() override;
std::vector<sk_sp<Component>> fChildren;
void onApply(SkPaint*) const override;
using INHERITED = Paint;
};
class Geometry : public TransformableComponent {
public:
void draw(SkCanvas* canvas, const SkPaint& paint, SkPathFillType ftype) const {
return this->onDraw(canvas, paint, ftype);
}
protected:
explicit Geometry(Type t) : INHERITED(t) {}
virtual void onDraw(SkCanvas*, const SkPaint&, SkPathFillType) const = 0;
private:
using INHERITED = TransformableComponent;
};
class Ellipse final : public Geometry {
public:
Ellipse() : INHERITED(Type::kEllipse) {}
ACTOR_ATTR(Width , float, 0)
ACTOR_ATTR(Height, float, 0)
private:
void onRevalidate() override;
void onDraw(SkCanvas*, const SkPaint&, SkPathFillType) const override;
using INHERITED = Geometry;
};
class Drawable : public Node {
public:
ACTOR_ATTR(DrawOrder, size_t , 0 )
@ -153,74 +246,18 @@ public:
private:
void onRevalidate() override;
void onRender(SkCanvas*) const override;
// cached on revalidation
// tracked separately due to paint order (all fills before strokes)
std::vector<const Paint*> fFills,
fStrokes;
std::vector<const Geometry*> fGeometries;
using INHERITED = Drawable;
};
class Paint : public Component {
public:
ACTOR_ATTR(Opacity , float , 1 )
ACTOR_ATTR(FillRule , SkPathFillType, SkPathFillType::kWinding )
ACTOR_ATTR(StrokeWidth, float , 1 )
ACTOR_ATTR(StrokeCap , SkPaint::Cap , SkPaint::Cap::kButt_Cap )
ACTOR_ATTR(StrokeJoin , SkPaint::Join , SkPaint::Join::kMiter_Join)
enum class StrokeTrim : uint8_t { kOff, kSequential, kSynced };
ACTOR_ATTR(StrokeTrim , StrokeTrim, StrokeTrim::kOff)
ACTOR_ATTR(StrokeTrimStart , float , 0)
ACTOR_ATTR(StrokeTrimEnd , float , 0)
ACTOR_ATTR(StrokeTrimOffset, float , 0)
void apply(SkPaint*) const;
SkPaint::Style style() const { return fStyle; }
protected:
Paint(Type t, SkPaint::Style style) : INHERITED(t), fStyle(style) {}
virtual void onApply(SkPaint*) const = 0;
private:
const SkPaint::Style fStyle;
using INHERITED = Component;
};
class ColorPaint final : public Paint {
public:
explicit ColorPaint(SkPaint::Style style) : INHERITED(Type::kColorPaint, style) {}
ACTOR_ATTR(Color, SkColor4f, SkColors::kBlack)
private:
void onRevalidate() override;
void onApply(SkPaint*) const override;
using INHERITED = Paint;
};
class Geometry : public TransformableComponent {
protected:
explicit Geometry(Type t) : INHERITED(t) {}
private:
using INHERITED = TransformableComponent;
};
class Ellipse final : public Geometry {
public:
Ellipse() : INHERITED(Type::kEllipse) {}
ACTOR_ATTR(Width , float, 0)
ACTOR_ATTR(Height, float, 0)
private:
void onRevalidate() override;
using INHERITED = Geometry;
};
template <typename T>
constexpr bool Component::is_base_of(Type t) {
if (t == Type::kNode ) return std::is_base_of<T, Node >::value;

View File

@ -126,7 +126,7 @@ void Artboard::render(SkCanvas* canvas) const {
if (fRoot) {
fRoot->revalidate();
fRoot->render(canvas);
}
}

View File

@ -33,6 +33,9 @@ size_t parse_node<ColorPaint>(StreamReader* sr, ColorPaint* node) {
void ColorPaint::onRevalidate() {}
void ColorPaint::onApply(SkPaint* paint) const {}
void ColorPaint::onApply(SkPaint* paint) const {
this->INHERITED::onApply(paint);
paint->setColor4f(fColor);
}
} // namespace skrive

View File

@ -8,6 +8,7 @@
#include "experimental/skrive/include/SkRive.h"
#include "experimental/skrive/src/reader/StreamReader.h"
#include "include/core/SkCanvas.h"
namespace skrive {
namespace internal {
@ -56,4 +57,28 @@ void Component::revalidate() {
}
}
void Component::onRender(SkCanvas*) const {}
TransformableComponent::ScopedTransformContext::
ScopedTransformContext(const TransformableComponent* node, SkCanvas* canvas)
: fCanvas(canvas)
, fRestoreCount(canvas->getSaveCount()) {
const auto lm = SkMatrix::Translate(node->getTranslation().x, node->getTranslation().y) *
SkMatrix::RotateDeg(node->getRotation() ) *
SkMatrix::Scale (node->getScale().x , node->getScale().y );
if (node->getOpacity() < 1) {
SkPaint layer_paint;
layer_paint.setAlphaf(node->getOpacity());
canvas->saveLayer(nullptr, &layer_paint);
} else if (!lm.isIdentity()) {
canvas->save();
}
canvas->concat(lm);
}
TransformableComponent::ScopedTransformContext::~ScopedTransformContext() {
fCanvas->restoreToCount(this->fRestoreCount);
}
} // namespace skrive

View File

@ -8,6 +8,7 @@
#include "experimental/skrive/include/SkRive.h"
#include "experimental/skrive/src/reader/StreamReader.h"
#include "include/core/SkCanvas.h"
namespace skrive {
namespace internal {
@ -27,6 +28,18 @@ size_t parse_node<Ellipse>(StreamReader* sr, Ellipse* node) {
} // namespace internal
void Ellipse::onRevalidate() {}
void Ellipse::onRevalidate() {
SkASSERT(this->hasInval());
}
void Ellipse::onDraw(SkCanvas* canvas, const SkPaint& paint, SkPathFillType) const {
SkASSERT(!this->hasInval());
if (SkScalarNearlyEqual(fWidth, fHeight)) {
canvas->drawCircle(0, 0, fWidth * 0.5f, paint);
} else {
canvas->drawOval(SkRect::MakeXYWH(-fWidth * 0.5f, -fHeight * 0.5f, fWidth, fHeight), paint);
}
}
} // namespace skrive

View File

@ -62,4 +62,16 @@ void Node::onRevalidate() {
}
}
void Node::onRender(SkCanvas* canvas) const {
SkASSERT(!this->hasInval());
TransformableComponent::ScopedTransformContext stc(this, canvas);
// TODO: draw order?
for (const auto& child : this->children()) {
child->render(canvas);
}
}
} // namespace internal

View File

@ -69,4 +69,10 @@ void parse_fill_stroke(StreamReader* sr, Paint* node) {
}
} // namespace internal
void Paint::onApply(SkPaint* paint) const {
paint->setAntiAlias(true);
paint->setStyle(this->style());
}
} // namespace skrive

View File

@ -8,6 +8,7 @@
#include "experimental/skrive/include/SkRive.h"
#include "experimental/skrive/src/reader/StreamReader.h"
#include "include/core/SkPaint.h"
namespace skrive {
namespace internal {
@ -26,6 +27,47 @@ size_t parse_node<Shape>(StreamReader* sr, Shape* node) {
} // namespace internal
void Shape::onRevalidate() {}
void Shape::onRevalidate() {
this->INHERITED::onRevalidate();
fFills.clear();
fStrokes.clear();
fGeometries.clear();
for (const auto& child : this->children()) {
if (const Paint* paint = *child) {
SkASSERT(paint->style() == SkPaint::kFill_Style ||
paint->style() == SkPaint::kStroke_Style);
auto& bucket = paint->style() == SkPaint::kFill_Style ? fFills : fStrokes;
bucket.push_back(paint);
} else if (const Geometry* geo = *child) {
fGeometries.push_back(geo);
}
}
SkDebugf("[Shape::onRevalidate] %zu geos %zu fill(s) %zu stroke(s)\n",
fGeometries.size(), fFills.size(), fStrokes.size());
}
void Shape::onRender(SkCanvas* canvas) const {
auto draw_paint = [this](SkCanvas* canvas, const Paint* paint) {
SkPaint p;
paint->apply(&p);
for (const auto& geo : fGeometries) {
geo->draw(canvas, p, paint->getFillRule());
}
};
TransformableComponent::ScopedTransformContext stc(this, canvas);
for (const auto* fill : fFills) {
draw_paint(canvas, fill);
}
for (const auto* stroke : fStrokes) {
draw_paint(canvas, stroke);
}
}
} // namespace skrive