[skotty,sksg] Initial RRect support
Bug: skia: Change-Id: I51bf6619e8d857d5d14fcd6651c144bd3c59453f Reviewed-on: https://skia-review.googlesource.com/90027 Commit-Queue: Florin Malita <fmalita@chromium.org> Reviewed-by: Mike Reed <reed@google.com>
This commit is contained in:
parent
b950b5975b
commit
2e1d7e2342
@ -21,6 +21,7 @@
|
||||
#include "SkSGInvalidationController.h"
|
||||
#include "SkSGGroup.h"
|
||||
#include "SkSGPath.h"
|
||||
#include "SkSGRect.h"
|
||||
#include "SkSGTransform.h"
|
||||
#include "SkStream.h"
|
||||
#include "SkTArray.h"
|
||||
@ -79,7 +80,7 @@ bool AttachProperty(const Json::Value& jprop, AttachContext* ctx, const sk_sp<No
|
||||
|
||||
sk_sp<sksg::RenderNode> AttachTransform(const Json::Value& t, AttachContext* ctx,
|
||||
sk_sp<sksg::RenderNode> wrapped_node) {
|
||||
if (!t.isObject())
|
||||
if (!t.isObject() || !wrapped_node)
|
||||
return wrapped_node;
|
||||
|
||||
auto xform = sk_make_sp<CompositeTransform>(wrapped_node);
|
||||
@ -143,6 +144,26 @@ sk_sp<sksg::GeometryNode> AttachPathGeometry(const Json::Value& jpath, AttachCon
|
||||
return path_attached ? path_node : nullptr;
|
||||
}
|
||||
|
||||
sk_sp<sksg::GeometryNode> AttachRRectGeometry(const Json::Value& jrect, AttachContext* ctx) {
|
||||
SkASSERT(jrect.isObject());
|
||||
|
||||
auto rect_node = sksg::RRect::Make();
|
||||
auto composite = sk_make_sp<CompositeRRect>(rect_node);
|
||||
|
||||
auto p_attached = AttachProperty<VectorValue, SkPoint>(jrect["p"], ctx, composite,
|
||||
[](const sk_sp<CompositeRRect>& node, const SkPoint& pos) { node->setPosition(pos); });
|
||||
auto s_attached = AttachProperty<VectorValue, SkSize>(jrect["s"], ctx, composite,
|
||||
[](const sk_sp<CompositeRRect>& node, const SkSize& sz) { node->setSize(sz); });
|
||||
auto r_attached = AttachProperty<ScalarValue, SkScalar>(jrect["r"], ctx, composite,
|
||||
[](const sk_sp<CompositeRRect>& node, SkScalar radius) { node->setRadius(radius); });
|
||||
|
||||
if (!p_attached && !s_attached && !r_attached) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
return rect_node;
|
||||
}
|
||||
|
||||
sk_sp<sksg::Color> AttachColorPaint(const Json::Value& obj, AttachContext* ctx) {
|
||||
SkASSERT(obj.isObject());
|
||||
|
||||
@ -205,6 +226,7 @@ sk_sp<sksg::PaintNode> AttachStrokePaint(const Json::Value& jstroke, AttachConte
|
||||
using GeometryAttacherT = sk_sp<sksg::GeometryNode> (*)(const Json::Value&, AttachContext*);
|
||||
static constexpr GeometryAttacherT gGeometryAttachers[] = {
|
||||
AttachPathGeometry,
|
||||
AttachRRectGeometry,
|
||||
};
|
||||
|
||||
using PaintAttacherT = sk_sp<sksg::PaintNode> (*)(const Json::Value&, AttachContext*);
|
||||
@ -241,6 +263,7 @@ const ShapeInfo* FindShapeInfo(const Json::Value& shape) {
|
||||
static constexpr ShapeInfo gShapeInfo[] = {
|
||||
{ "fl", ShapeType::kPaint , 0 }, // fill -> AttachFillPaint
|
||||
{ "gr", ShapeType::kGroup , 0 }, // group -> AttachShapeGroup
|
||||
{ "rc", ShapeType::kGeometry , 1 }, // shape -> AttachRRectGeometry
|
||||
{ "sh", ShapeType::kGeometry , 0 }, // shape -> AttachPathGeometry
|
||||
{ "st", ShapeType::kPaint , 1 }, // stroke -> AttachStrokePaint
|
||||
{ "tr", ShapeType::kTransform, 0 }, // transform -> AttachTransform
|
||||
|
@ -10,6 +10,7 @@
|
||||
#include "SkColor.h"
|
||||
#include "SkottyPriv.h"
|
||||
#include "SkPath.h"
|
||||
#include "SkSGRect.h"
|
||||
#include "SkSGTransform.h"
|
||||
|
||||
namespace skotty {
|
||||
@ -120,6 +121,24 @@ SkPoint VectorValue::as<SkPoint>() const {
|
||||
return SkPoint::Make(x, y);
|
||||
}
|
||||
|
||||
template <>
|
||||
SkSize VectorValue::as<SkSize>() const {
|
||||
const auto pt = this->as<SkPoint>();
|
||||
return SkSize::Make(pt.x(), pt.y());
|
||||
}
|
||||
|
||||
template <>
|
||||
std::vector<SkScalar> VectorValue::as<std::vector<SkScalar>>() const {
|
||||
std::vector<SkScalar> vec;
|
||||
vec.reserve(fVals.count());
|
||||
|
||||
for (const auto& val : fVals) {
|
||||
vec.push_back(val);
|
||||
}
|
||||
|
||||
return vec;
|
||||
}
|
||||
|
||||
template <>
|
||||
SkPath ShapeValue::as<SkPath>() const {
|
||||
SkPath path;
|
||||
@ -146,6 +165,19 @@ SkPath ShapeValue::as<SkPath>() const {
|
||||
return path;
|
||||
}
|
||||
|
||||
CompositeRRect::CompositeRRect(sk_sp<sksg::RRect> wrapped_node)
|
||||
: fRRectNode(std::move(wrapped_node)) {}
|
||||
|
||||
void CompositeRRect::apply() {
|
||||
// BM "position" == "center position"
|
||||
auto rr = SkRRect::MakeRectXY(SkRect::MakeXYWH(fPosition.x() - fSize.width() / 2,
|
||||
fPosition.y() - fSize.height() / 2,
|
||||
fSize.width(), fSize.height()),
|
||||
fRadius,
|
||||
fRadius);
|
||||
fRRectNode->setRRect(rr);
|
||||
}
|
||||
|
||||
CompositeTransform::CompositeTransform(sk_sp<sksg::RenderNode> wrapped_node)
|
||||
: fTransformNode(sksg::Transform::Make(std::move(wrapped_node), SkMatrix::I())) {}
|
||||
|
||||
|
@ -20,6 +20,7 @@
|
||||
class SkPath;
|
||||
|
||||
namespace sksg {
|
||||
class RRect;
|
||||
class RenderNode;
|
||||
class Transform;
|
||||
}
|
||||
@ -100,6 +101,22 @@ struct ShapeValue {
|
||||
p_type f##p_name = p_default; \
|
||||
public:
|
||||
|
||||
class CompositeRRect final : public SkRefCnt {
|
||||
public:
|
||||
explicit CompositeRRect(sk_sp<sksg::RRect>);
|
||||
|
||||
COMPOSITE_PROPERTY(Position, SkPoint , SkPoint::Make(0, 0))
|
||||
COMPOSITE_PROPERTY(Size , SkSize , SkSize::Make(0, 0))
|
||||
COMPOSITE_PROPERTY(Radius , SkScalar, 0)
|
||||
|
||||
private:
|
||||
void apply();
|
||||
|
||||
sk_sp<sksg::RRect> fRRectNode;
|
||||
|
||||
using INHERITED = SkRefCnt;
|
||||
};
|
||||
|
||||
class CompositeTransform final : public SkRefCnt {
|
||||
public:
|
||||
explicit CompositeTransform(sk_sp<sksg::RenderNode>);
|
||||
|
@ -22,4 +22,14 @@ SkRect Rect::onComputeBounds() const {
|
||||
return fRect;
|
||||
}
|
||||
|
||||
RRect::RRect(const SkRRect& rr) : fRRect(rr) {}
|
||||
|
||||
void RRect::onDraw(SkCanvas* canvas, const SkPaint& paint) const {
|
||||
canvas->drawRRect(fRRect, paint);
|
||||
}
|
||||
|
||||
SkRect RRect::onComputeBounds() const {
|
||||
return fRRect.getBounds();
|
||||
}
|
||||
|
||||
} // namespace sksg
|
||||
|
@ -11,6 +11,7 @@
|
||||
#include "SkSGGeometryNode.h"
|
||||
|
||||
#include "SkRect.h"
|
||||
#include "SkRRect.h"
|
||||
|
||||
class SkCanvas;
|
||||
class SkPaint;
|
||||
@ -20,7 +21,7 @@ namespace sksg {
|
||||
/**
|
||||
* Concrete Geometry node, wrapping an SkRect.
|
||||
*/
|
||||
class Rect : public GeometryNode {
|
||||
class Rect final : public GeometryNode {
|
||||
public:
|
||||
static sk_sp<Rect> Make() { return sk_sp<Rect>(new Rect(SkRect::MakeEmpty())); }
|
||||
static sk_sp<Rect> Make(const SkRect& r) { return sk_sp<Rect>(new Rect(r)); }
|
||||
@ -41,6 +42,27 @@ private:
|
||||
SkRect fRect;
|
||||
};
|
||||
|
||||
/**
|
||||
* Concrete Geometry node, wrapping an SkRRect.
|
||||
*/
|
||||
class RRect final : public GeometryNode {
|
||||
public:
|
||||
static sk_sp<RRect> Make() { return sk_sp<RRect>(new RRect(SkRRect())); }
|
||||
static sk_sp<RRect> Make(const SkRRect& rr) { return sk_sp<RRect>(new RRect(rr)); }
|
||||
|
||||
SG_ATTRIBUTE(RRect, SkRRect, fRRect)
|
||||
|
||||
protected:
|
||||
void onDraw(SkCanvas*, const SkPaint&) const override;
|
||||
|
||||
SkRect onComputeBounds() const override;
|
||||
|
||||
private:
|
||||
explicit RRect(const SkRRect&);
|
||||
|
||||
SkRRect fRRect;
|
||||
};
|
||||
|
||||
} // namespace sksg
|
||||
|
||||
#endif // SkSGRect_DEFINED
|
||||
|
Loading…
Reference in New Issue
Block a user