[sksg] Refactor stroke logic

Instead of a specialized node, hoist attributes to base class.

TBR=
Change-Id: I4fa5a24dfc899307a8603577738972ebd32f57f5
Reviewed-on: https://skia-review.googlesource.com/89903
Reviewed-by: Florin Malita <fmalita@chromium.org>
Commit-Queue: Florin Malita <fmalita@chromium.org>
This commit is contained in:
Florin Malita 2017-12-29 11:52:44 -05:00 committed by Skia Commit-Bot
parent d1e268cfa1
commit fa8d49adfa
7 changed files with 28 additions and 99 deletions

View File

@ -1372,7 +1372,6 @@ if (skia_enable_tools) {
"experimental/sksg/geometry/SkSGPath.cpp",
"experimental/sksg/geometry/SkSGRect.cpp",
"experimental/sksg/paint/SkSGColor.cpp",
"experimental/sksg/paint/SkSGStroke.cpp",
]
deps = [
":skia",

View File

@ -20,7 +20,15 @@ const SkPaint& PaintNode::makePaint() {
void PaintNode::onRevalidate(InvalidationController*, const SkMatrix&) {
SkASSERT(this->isInvalidated());
fPaint = this->onMakePaint();
fPaint.reset();
fPaint.setAntiAlias(fAntiAlias);
fPaint.setStyle(fStyle);
fPaint.setStrokeWidth(fStrokeWidth);
fPaint.setStrokeMiter(fStrokeMiter);
fPaint.setStrokeJoin(fStrokeJoin);
fPaint.setStrokeCap(fStrokeCap);
this->onApplyToPaint(&fPaint);
}
} // namespace sksg

View File

@ -22,18 +22,31 @@ namespace sksg {
*/
class PaintNode : public Node {
public:
const SkPaint& makePaint();
SG_ATTRIBUTE(AntiAlias , bool , fAntiAlias )
SG_ATTRIBUTE(StrokeWidth, SkScalar , fStrokeWidth)
SG_ATTRIBUTE(StrokeMiter, SkScalar , fStrokeMiter)
SG_ATTRIBUTE(Style , SkPaint::Style, fStyle )
SG_ATTRIBUTE(StrokeJoin , SkPaint::Join , fStrokeJoin )
SG_ATTRIBUTE(StrokeCap , SkPaint::Cap , fStrokeCap )
protected:
PaintNode();
virtual SkPaint onMakePaint() const = 0;
virtual void onApplyToPaint(SkPaint*) const = 0;
void onRevalidate(InvalidationController*, const SkMatrix&) override;
private:
SkPaint fPaint;
SkPaint fPaint;
SkScalar fStrokeWidth = 1,
fStrokeMiter = 4;
bool fAntiAlias = false;
SkPaint::Style fStyle = SkPaint::kFill_Style;
SkPaint::Join fStrokeJoin = SkPaint::kMiter_Join;
SkPaint::Cap fStrokeCap = SkPaint::kButt_Cap;
typedef Node INHERITED;
};

View File

@ -11,10 +11,8 @@ namespace sksg {
Color::Color(SkColor c) : fColor(c) {}
SkPaint Color::onMakePaint() const {
SkPaint paint;
paint.setColor(fColor);
return paint;
void Color::onApplyToPaint(SkPaint* paint) const {
paint->setColor(fColor);
}
} // namespace sksg

View File

@ -24,7 +24,7 @@ public:
SG_ATTRIBUTE(Color, SkColor, fColor)
protected:
SkPaint onMakePaint() const override;
void onApplyToPaint(SkPaint*) const override;
private:
explicit Color(SkColor);

View File

@ -1,38 +0,0 @@
/*
* Copyright 2017 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include "SkSGStroke.h"
namespace sksg {
Stroke::Stroke(sk_sp<PaintNode> paint)
: fPaint(std::move(paint)) {
fPaint->addInvalReceiver(this);
}
Stroke::~Stroke() {
fPaint->removeInvalReceiver(this);
}
void Stroke::onRevalidate(InvalidationController* ic, const SkMatrix& ctm) {
fPaint->revalidate(ic, ctm);
INHERITED::onRevalidate(ic, ctm);
}
SkPaint Stroke::onMakePaint() const {
SkPaint paint = fPaint->makePaint();
paint.setStyle(SkPaint::kStroke_Style);
paint.setStrokeWidth(fStrokeWidth);
paint.setStrokeMiter(fStrokeMiter);
paint.setStrokeJoin(fStrokeJoin);
paint.setStrokeCap(fStrokeCap);
return paint;
}
} // namespace sksg

View File

@ -1,51 +0,0 @@
/*
* Copyright 2017 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#ifndef SkSGStroke_DEFINED
#define SkSGStroke_DEFINED
#include "SkSGPaintNode.h"
#include "SkScalar.h"
namespace sksg {
/**
* Concrete Paint node wrapper, applying a stroke effect to an existing paint.
*/
class Stroke final : public PaintNode {
public:
static sk_sp<Stroke> Make(sk_sp<PaintNode> paint) {
return paint ? sk_sp<Stroke>(new Stroke(std::move(paint))) : nullptr;
}
SG_ATTRIBUTE(StrokeWidth, SkScalar , fStrokeWidth)
SG_ATTRIBUTE(StrokeMiter, SkScalar , fStrokeMiter)
SG_ATTRIBUTE(StrokeJoin , SkPaint::Join, fStrokeJoin )
SG_ATTRIBUTE(StrokeCap , SkPaint::Cap , fStrokeCap )
protected:
void onRevalidate(InvalidationController*, const SkMatrix&) override;
SkPaint onMakePaint() const override;
private:
explicit Stroke(sk_sp<PaintNode>);
~Stroke() override;
sk_sp<PaintNode> fPaint;
SkScalar fStrokeWidth = 1,
fStrokeMiter = 4;
SkPaint::Join fStrokeJoin = SkPaint::kMiter_Join;
SkPaint::Cap fStrokeCap = SkPaint::kButt_Cap;
typedef PaintNode INHERITED;
};
} // namespace sksg
#endif // SkSGStroke_DEFINED