From d1f7e354a140f1a7823145a08c2093d114380564 Mon Sep 17 00:00:00 2001 From: mtklein Date: Mon, 2 Nov 2015 09:08:17 -0800 Subject: [PATCH] SkRemote: all paint effects except typeface Changes: https://gold.skia.org/search2?issue=1408383007&unt=true&query=source_type%3Dgm&master=false&include=true BUG=skia: Review URL: https://codereview.chromium.org/1408383007 --- src/core/SkRemote.cpp | 212 ++++++++++++++++++++++++++--------- src/core/SkRemote.h | 20 +++- src/core/SkRemote_protocol.h | 7 ++ 3 files changed, 180 insertions(+), 59 deletions(-) diff --git a/src/core/SkRemote.cpp b/src/core/SkRemote.cpp index ff658ff20d..bc89e6ff2a 100644 --- a/src/core/SkRemote.cpp +++ b/src/core/SkRemote.cpp @@ -5,8 +5,15 @@ * found in the LICENSE file. */ +#include "SkAnnotation.h" #include "SkCanvas.h" +#include "SkColorFilter.h" +#include "SkDrawLooper.h" +#include "SkImageFilter.h" +#include "SkMaskFilter.h" #include "SkPath.h" +#include "SkPathEffect.h" +#include "SkRasterizer.h" #include "SkRect.h" #include "SkRemote.h" #include "SkShader.h" @@ -103,9 +110,54 @@ namespace SkRemote { const ID fID; }; + // Like AutoID, but for CommonIDs. + class AutoCommonIDs : ::SkNoncopyable { + public: + explicit AutoCommonIDs(Encoder* encoder, const SkPaint& paint) + : fEncoder(encoder) { + fIDs.misc = fEncoder->define(Misc::CreateFrom(paint)); + fIDs.patheffect = fEncoder->define(paint.getPathEffect()); + fIDs.shader = fEncoder->define(paint.getShader()); + fIDs.xfermode = fEncoder->define(paint.getXfermode()); + fIDs.maskfilter = fEncoder->define(paint.getMaskFilter()); + fIDs.colorfilter = fEncoder->define(paint.getColorFilter()); + fIDs.rasterizer = fEncoder->define(paint.getRasterizer()); + fIDs.looper = fEncoder->define(paint.getLooper()); + fIDs.imagefilter = fEncoder->define(paint.getImageFilter()); + fIDs.annotation = fEncoder->define(paint.getAnnotation()); + } + ~AutoCommonIDs() { + if (fEncoder) { + fEncoder->undefine(fIDs.misc); + fEncoder->undefine(fIDs.patheffect); + fEncoder->undefine(fIDs.shader); + fEncoder->undefine(fIDs.xfermode); + fEncoder->undefine(fIDs.maskfilter); + fEncoder->undefine(fIDs.colorfilter); + fEncoder->undefine(fIDs.rasterizer); + fEncoder->undefine(fIDs.looper); + fEncoder->undefine(fIDs.imagefilter); + fEncoder->undefine(fIDs.annotation); + } + } + + AutoCommonIDs(AutoCommonIDs&& o) : fEncoder(o.fEncoder), fIDs(o.fIDs) { + o.fEncoder = nullptr; + } + AutoID& operator=(AutoID&&) = delete; + + operator Encoder::CommonIDs () const { return fIDs; } + + private: + Encoder* fEncoder; + Encoder::CommonIDs fIDs; + }; + template AutoID id(const T& val) { return AutoID(fEncoder, val); } + AutoCommonIDs commonIDs(const SkPaint& paint) { return AutoCommonIDs(fEncoder, paint); } + void willSave() override { fEncoder-> save(); } void didRestore() override { fEncoder->restore(); } @@ -141,16 +193,14 @@ namespace SkRemote { } void onDrawPath(const SkPath& path, const SkPaint& paint) override { - auto p = this->id(path), - m = this->id(Misc::CreateFrom(paint)), - s = this->id(paint.getShader()), - x = this->id(paint.getXfermode()); + auto common = this->commonIDs(paint); + auto p = this->id(path); if (paint.getStyle() == SkPaint::kFill_Style) { - fEncoder->fillPath(p, m, s, x); + fEncoder->fillPath(p, common); } else { // TODO: handle kStrokeAndFill_Style - fEncoder->strokePath(p, m, s, x, this->id(Stroke::CreateFrom(paint))); + fEncoder->strokePath(p, common, this->id(Stroke::CreateFrom(paint))); } } @@ -223,24 +273,54 @@ namespace SkRemote { return id; } - ID define(const SkMatrix& v) override {return this->define(Type::kMatrix, &fMatrix, v);} - ID define(const Misc& v) override {return this->define(Type::kMisc, &fMisc, v);} - ID define(const SkPath& v) override {return this->define(Type::kPath, &fPath, v);} - ID define(const Stroke& v) override {return this->define(Type::kStroke, &fStroke, v);} - ID define(SkShader* v) override {return this->define(Type::kShader, &fShader, v);} - ID define(SkXfermode* v) override {return this->define(Type::kXfermode, &fXfermode, v);} + #define O override + ID define(const SkMatrix& v) O {return this->define(Type::kMatrix, &fMatrix, v);} + ID define(const Misc& v) O {return this->define(Type::kMisc, &fMisc, v);} + ID define(const SkPath& v) O {return this->define(Type::kPath, &fPath, v);} + ID define(const Stroke& v) O {return this->define(Type::kStroke, &fStroke, v);} + ID define(SkPathEffect* v) O {return this->define(Type::kPathEffect, &fPathEffect, v);} + ID define(SkShader* v) O {return this->define(Type::kShader, &fShader, v);} + ID define(SkXfermode* v) O {return this->define(Type::kXfermode, &fXfermode, v);} + ID define(SkMaskFilter* v) O {return this->define(Type::kMaskFilter, &fMaskFilter, v);} + ID define(SkColorFilter* v) O {return this->define(Type::kColorFilter, &fColorFilter, v);} + ID define(SkRasterizer* v) O {return this->define(Type::kRasterizer, &fRasterizer, v);} + ID define(SkDrawLooper* v) O {return this->define(Type::kDrawLooper, &fDrawLooper, v);} + ID define(SkImageFilter* v) O {return this->define(Type::kImageFilter, &fImageFilter, v);} + ID define(SkAnnotation* v) O {return this->define(Type::kAnnotation, &fAnnotation, v);} + #undef O + void undefine(ID id) override { switch(id.type()) { - case Type::kMatrix: return fMatrix .remove(id); - case Type::kMisc: return fMisc .remove(id); - case Type::kPath: return fPath .remove(id); - case Type::kStroke: return fStroke .remove(id); - case Type::kShader: return fShader .remove(id); - case Type::kXfermode: return fXfermode.remove(id); + case Type::kMatrix: return fMatrix .remove(id); + case Type::kMisc: return fMisc .remove(id); + case Type::kPath: return fPath .remove(id); + case Type::kStroke: return fStroke .remove(id); + case Type::kPathEffect: return fPathEffect .remove(id); + case Type::kShader: return fShader .remove(id); + case Type::kXfermode: return fXfermode .remove(id); + case Type::kMaskFilter: return fMaskFilter .remove(id); + case Type::kColorFilter: return fColorFilter.remove(id); + case Type::kRasterizer: return fRasterizer .remove(id); + case Type::kDrawLooper: return fDrawLooper .remove(id); + case Type::kImageFilter: return fImageFilter.remove(id); + case Type::kAnnotation: return fAnnotation .remove(id); }; } + void applyCommon(const CommonIDs& common, SkPaint* paint) const { + fMisc.find(common.misc).applyTo(paint); + paint->setPathEffect (fPathEffect .find(common.patheffect)); + paint->setShader (fShader .find(common.shader)); + paint->setXfermode (fXfermode .find(common.xfermode)); + paint->setMaskFilter (fMaskFilter .find(common.maskfilter)); + paint->setColorFilter(fColorFilter.find(common.colorfilter)); + paint->setRasterizer (fRasterizer .find(common.rasterizer)); + paint->setLooper (fDrawLooper .find(common.looper)); + paint->setImageFilter(fImageFilter.find(common.imagefilter)); + paint->setAnnotation (fAnnotation .find(common.annotation)); + } + void save() override { fCanvas->save(); } void restore() override { fCanvas->restore(); } @@ -249,21 +329,17 @@ namespace SkRemote { void clipPath(ID path, SkRegion::Op op, bool aa) override { fCanvas->clipPath(fPath.find(path), op, aa); } - void fillPath(ID path, ID misc, ID shader, ID xfermode) override { + void fillPath(ID path, CommonIDs common) override { SkPaint paint; paint.setStyle(SkPaint::kFill_Style); - fMisc.find(misc).applyTo(&paint); - paint.setShader (fShader .find(shader)); - paint.setXfermode(fXfermode.find(xfermode)); + this->applyCommon(common, &paint); fCanvas->drawPath(fPath.find(path), paint); } - void strokePath(ID path, ID misc, ID shader, ID xfermode, ID stroke) override { + void strokePath(ID path, CommonIDs common, ID stroke) override { SkPaint paint; paint.setStyle(SkPaint::kStroke_Style); - fMisc .find(misc ).applyTo(&paint); + this->applyCommon(common, &paint); fStroke.find(stroke).applyTo(&paint); - paint.setShader (fShader .find(shader)); - paint.setXfermode(fXfermode.find(xfermode)); fCanvas->drawPath(fPath.find(path), paint); } @@ -332,12 +408,19 @@ namespace SkRemote { }; - IDMap fMatrix; - IDMap fMisc; - IDMap fPath; - IDMap fStroke; - ReffedIDMap fShader; - ReffedIDMap fXfermode; + IDMap fMatrix; + IDMap fMisc; + IDMap fPath; + IDMap fStroke; + ReffedIDMap fPathEffect; + ReffedIDMap fShader; + ReffedIDMap fXfermode; + ReffedIDMap fMaskFilter; + ReffedIDMap fColorFilter; + ReffedIDMap fRasterizer; + ReffedIDMap fDrawLooper; + ReffedIDMap fImageFilter; + ReffedIDMap fAnnotation; SkCanvas* fCanvas; uint64_t fNextID = 0; @@ -360,12 +443,19 @@ namespace SkRemote { ~CachingEncoder() override { Undef undef{fWrapped}; - fMatrix .foreach(undef); - fMisc .foreach(undef); - fPath .foreach(undef); - fStroke .foreach(undef); - fShader .foreach(undef); - fXfermode.foreach(undef); + fMatrix .foreach(undef); + fMisc .foreach(undef); + fPath .foreach(undef); + fPathEffect .foreach(undef); + fStroke .foreach(undef); + fShader .foreach(undef); + fXfermode .foreach(undef); + fMaskFilter .foreach(undef); + fColorFilter.foreach(undef); + fRasterizer .foreach(undef); + fDrawLooper .foreach(undef); + fImageFilter.foreach(undef); + fAnnotation .foreach(undef); } template @@ -378,12 +468,19 @@ namespace SkRemote { return id; } - ID define(const SkMatrix& v) override { return this->define(&fMatrix, v); } - ID define(const Misc& v) override { return this->define(&fMisc, v); } - ID define(const SkPath& v) override { return this->define(&fPath, v); } - ID define(const Stroke& v) override { return this->define(&fStroke, v); } - ID define(SkShader* v) override { return this->define(&fShader, v); } - ID define(SkXfermode* v) override { return this->define(&fXfermode, v); } + ID define(const SkMatrix& v) override { return this->define(&fMatrix , v); } + ID define(const Misc& v) override { return this->define(&fMisc , v); } + ID define(const SkPath& v) override { return this->define(&fPath , v); } + ID define(const Stroke& v) override { return this->define(&fStroke , v); } + ID define(SkPathEffect* v) override { return this->define(&fPathEffect , v); } + ID define(SkShader* v) override { return this->define(&fShader , v); } + ID define(SkXfermode* v) override { return this->define(&fXfermode , v); } + ID define(SkMaskFilter* v) override { return this->define(&fMaskFilter , v); } + ID define(SkColorFilter* v) override { return this->define(&fColorFilter, v); } + ID define(SkRasterizer* v) override { return this->define(&fRasterizer , v); } + ID define(SkDrawLooper* v) override { return this->define(&fDrawLooper , v); } + ID define(SkImageFilter* v) override { return this->define(&fImageFilter, v); } + ID define(SkAnnotation* v) override { return this->define(&fAnnotation , v); } void undefine(ID) override {} @@ -395,11 +492,11 @@ namespace SkRemote { void clipPath(ID path, SkRegion::Op op, bool aa) override { fWrapped->clipPath(path, op, aa); } - void fillPath(ID path, ID misc, ID shader, ID xfermode) override { - fWrapped->fillPath(path, misc, shader, xfermode); + void fillPath(ID path, CommonIDs common) override { + fWrapped->fillPath(path, common); } - void strokePath(ID path, ID misc, ID shader, ID xfermode, ID stroke) override { - fWrapped->strokePath(path, misc, shader, xfermode, stroke); + void strokePath(ID path, CommonIDs common, ID stroke) override { + fWrapped->strokePath(path, common, stroke); } // Maps const T* -> ID, and refs the key. @@ -431,12 +528,19 @@ namespace SkRemote { SkTHashMap fMap; }; - SkTHashMap fMatrix; - SkTHashMap fMisc; - SkTHashMap fPath; - SkTHashMap fStroke; - RefKeyMap fShader; - RefKeyMap fXfermode; + SkTHashMap fMatrix; + SkTHashMap fMisc; + SkTHashMap fPath; + SkTHashMap fStroke; + RefKeyMap fPathEffect; + RefKeyMap fShader; + RefKeyMap fXfermode; + RefKeyMap fMaskFilter; + RefKeyMap fColorFilter; + RefKeyMap fRasterizer; + RefKeyMap fDrawLooper; + RefKeyMap fImageFilter; + RefKeyMap fAnnotation; Encoder* fWrapped; }; diff --git a/src/core/SkRemote.h b/src/core/SkRemote.h index 537d1ac417..5fb20242fe 100644 --- a/src/core/SkRemote.h +++ b/src/core/SkRemote.h @@ -69,8 +69,15 @@ namespace SkRemote { virtual ID define(const Misc&) = 0; virtual ID define(const SkPath&) = 0; virtual ID define(const Stroke&) = 0; + virtual ID define(SkPathEffect*) = 0; virtual ID define(SkShader*) = 0; virtual ID define(SkXfermode*) = 0; + virtual ID define(SkMaskFilter*) = 0; + virtual ID define(SkColorFilter*) = 0; + virtual ID define(SkRasterizer*) = 0; + virtual ID define(SkDrawLooper*) = 0; + virtual ID define(SkImageFilter*) = 0; + virtual ID define(SkAnnotation*) = 0; virtual void undefine(ID) = 0; @@ -79,12 +86,15 @@ namespace SkRemote { virtual void setMatrix(ID matrix) = 0; - // TODO: struct CommonIDs { ID misc, shader, xfermode; ... } - // for IDs that affect both fill + stroke? + // TODO: do these all belong here in CommonIDs? + struct CommonIDs { + ID misc, patheffect, shader, xfermode, maskfilter, + colorfilter, rasterizer, looper, imagefilter, annotation; + }; - virtual void clipPath(ID path, SkRegion::Op, bool aa) = 0; - virtual void fillPath(ID path, ID misc, ID shader, ID xfermode) = 0; - virtual void strokePath(ID path, ID misc, ID shader, ID xfermode, ID stroke) = 0; + virtual void clipPath(ID path, SkRegion::Op, bool aa) = 0; + virtual void fillPath(ID path, CommonIDs) = 0; + virtual void strokePath(ID path, CommonIDs, ID stroke) = 0; }; // None of these factories take ownership of their arguments. diff --git a/src/core/SkRemote_protocol.h b/src/core/SkRemote_protocol.h index b89d491379..6a37f09e09 100644 --- a/src/core/SkRemote_protocol.h +++ b/src/core/SkRemote_protocol.h @@ -19,8 +19,15 @@ namespace SkRemote { kMisc, kPath, kStroke, + kPathEffect, kShader, kXfermode, + kMaskFilter, + kColorFilter, + kRasterizer, + kDrawLooper, + kImageFilter, + kAnnotation, }; } // namespace SkRemote -- 2.34.1