From 1586d85198d17496efa93a6af812e3913882347f Mon Sep 17 00:00:00 2001 From: Florin Malita Date: Fri, 12 Jan 2018 14:27:39 -0500 Subject: [skotty] Refactor paint opacity Promote to a PaintNode attribute, drop color composite. TBR= Change-Id: Ia79d5f7e193a472d53ac4ff8beb7234d4dc26cef Reviewed-on: https://skia-review.googlesource.com/94280 Reviewed-by: Florin Malita Commit-Queue: Florin Malita --- experimental/skotty/Skotty.cpp | 19 +++++++++---------- experimental/skotty/SkottyProperties.cpp | 14 -------------- experimental/skotty/SkottyProperties.h | 15 --------------- experimental/sksg/SkSGPaintNode.cpp | 3 +++ experimental/sksg/SkSGPaintNode.h | 4 +++- 5 files changed, 15 insertions(+), 40 deletions(-) (limited to 'experimental') diff --git a/experimental/skotty/Skotty.cpp b/experimental/skotty/Skotty.cpp index ce2644ea51..72d12d6ac4 100644 --- a/experimental/skotty/Skotty.cpp +++ b/experimental/skotty/Skotty.cpp @@ -298,17 +298,12 @@ sk_sp AttachColor(const Json::Value& obj, AttachContext* ctx) { SkASSERT(obj.isObject()); auto color_node = sksg::Color::Make(SK_ColorBLACK); - auto composite = sk_make_sp(color_node); - auto color_attached = BindProperty(obj["c"], ctx, composite, - [](CompositeColor* node, const VectorValue& c) { + auto color_attached = BindProperty(obj["c"], ctx, color_node, + [](sksg::Color* node, const VectorValue& c) { node->setColor(ValueTraits::As(c)); }); - auto opacity_attached = BindProperty(obj["o"], ctx, composite, - [](CompositeColor* node, const ScalarValue& o) { - node->setOpacity(o); - }); - return (color_attached || opacity_attached) ? color_node : nullptr; + return color_attached ? color_node : nullptr; } sk_sp AttachGradient(const Json::Value& obj, AttachContext* ctx) { @@ -353,12 +348,16 @@ sk_sp AttachGradient(const Json::Value& obj, AttachContext* ctx) return gradient_node; } -sk_sp AttachPaint(const Json::Value& jfill, AttachContext* ctx, +sk_sp AttachPaint(const Json::Value& jpaint, AttachContext* ctx, sk_sp paint_node) { if (paint_node) { paint_node->setAntiAlias(true); - // TODO: refactor opacity + BindProperty(jpaint["o"], ctx, paint_node, + [](sksg::PaintNode* node, const ScalarValue& o) { + // BM opacity is [0..100] + node->setOpacity(o * 0.01f); + }); } return paint_node; diff --git a/experimental/skotty/SkottyProperties.cpp b/experimental/skotty/SkottyProperties.cpp index e962a7d538..6be2eebfa3 100644 --- a/experimental/skotty/SkottyProperties.cpp +++ b/experimental/skotty/SkottyProperties.cpp @@ -183,20 +183,6 @@ SkPath ValueTraits::As(const ShapeValue& path) { return path; } -CompositeColor::CompositeColor(sk_sp wrapped_node) - : fColorNode(std::move(wrapped_node)) { - SkASSERT(fColorNode); -} - -void CompositeColor::apply() { - // 'opacity' is [0..100] - const auto a = SkScalarRoundToInt(SkTPin(fOpacity * .01f, 0, 1) * SkColorGetA(fColor)); - fColorNode->setColor(SkColorSetARGB(a, - SkColorGetR(fColor), - SkColorGetG(fColor), - SkColorGetB(fColor))); -} - CompositeRRect::CompositeRRect(sk_sp wrapped_node) : fRRectNode(std::move(wrapped_node)) {} diff --git a/experimental/skotty/SkottyProperties.h b/experimental/skotty/SkottyProperties.h index cd22810c2e..177ba35873 100644 --- a/experimental/skotty/SkottyProperties.h +++ b/experimental/skotty/SkottyProperties.h @@ -58,21 +58,6 @@ using ShapeValue = SkPath; p_type f##p_name = p_default; \ public: -class CompositeColor final : public SkRefCnt { -public: - explicit CompositeColor(sk_sp); - - COMPOSITE_PROPERTY(Color , SkColor , SK_ColorBLACK) - COMPOSITE_PROPERTY(Opacity, SkScalar, 100 ) - -private: - void apply(); - - sk_sp fColorNode; - - using INHERITED = SkRefCnt; -}; - class CompositeRRect final : public SkRefCnt { public: explicit CompositeRRect(sk_sp); diff --git a/experimental/sksg/SkSGPaintNode.cpp b/experimental/sksg/SkSGPaintNode.cpp index 146d248b8e..bb5b714030 100644 --- a/experimental/sksg/SkSGPaintNode.cpp +++ b/experimental/sksg/SkSGPaintNode.cpp @@ -31,6 +31,9 @@ SkRect PaintNode::onRevalidate(InvalidationController*, const SkMatrix&) { this->onApplyToPaint(&fPaint); + // Compose opacity on top of the subclass value. + fPaint.setAlpha(SkScalarRoundToInt(fPaint.getAlpha() * SkTPin(fOpacity, 0, 1))); + return SkRect::MakeEmpty(); } diff --git a/experimental/sksg/SkSGPaintNode.h b/experimental/sksg/SkSGPaintNode.h index a2fbada065..1085e27c0f 100644 --- a/experimental/sksg/SkSGPaintNode.h +++ b/experimental/sksg/SkSGPaintNode.h @@ -25,6 +25,7 @@ public: const SkPaint& makePaint(); SG_ATTRIBUTE(AntiAlias , bool , fAntiAlias ) + SG_ATTRIBUTE(Opacity , SkScalar , fOpacity ) SG_ATTRIBUTE(StrokeWidth, SkScalar , fStrokeWidth) SG_ATTRIBUTE(StrokeMiter, SkScalar , fStrokeMiter) SG_ATTRIBUTE(Style , SkPaint::Style, fStyle ) @@ -41,7 +42,8 @@ protected: private: SkPaint fPaint; - SkScalar fStrokeWidth = 1, + SkScalar fOpacity = 1, + fStrokeWidth = 1, fStrokeMiter = 4; bool fAntiAlias = false; SkPaint::Style fStyle = SkPaint::kFill_Style; -- cgit v1.2.3