Productionize PathKit
- Remove some old API with VerbArgs that we didn't really like.
- move from experimental/wasm -> experimental/pathkit and rename
wasm_main.cpp to pathkit_wasm_bindings (more descriptive).
- Make compile.sh nicer to use (with some form of command line args).
- Use MODULARIZE=1 to make this play nicer with other WASM libraries
and easier to import.
- Add seperate ToCanvas() API
- Move Region stuff behind the PATHKIT_TESTING flag (saves 100k on
binary size).
- Add npm package for wasm version. asm.js version should also be
supported for older browsers.
- Remove shell.html, which was largely too complicated. Replace it with
example.html, which is more succinct and demos the more relevant APIs.
See https://www.npmjs.com/package/experimental-pathkit-wasm
Bug: skia:8216
Change-Id: I15f14dd8acd77331729998ae3e30d73e4b006761
Reviewed-on: https://skia-review.googlesource.com/144790
Reviewed-by: Joe Gregorio <jcgregorio@google.com>
Reviewed-by: Florin Malita <fmalita@chromium.org>
Commit-Queue: Kevin Lubick <kjlubick@google.com>
diff --git a/experimental/pathkit/pathkit_wasm_bindings.cpp b/experimental/pathkit/pathkit_wasm_bindings.cpp
new file mode 100644
index 0000000..dd6245e
--- /dev/null
+++ b/experimental/pathkit/pathkit_wasm_bindings.cpp
@@ -0,0 +1,334 @@
+/*
+ * Copyright 2018 Google LLC
+ *
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+#include "SkFloatBits.h"
+#include "SkFloatingPoint.h"
+#include "SkParsePath.h"
+#include "SkPath.h"
+#include "SkPathOps.h"
+#include "SkRegion.h"
+#include "SkString.h"
+
+#include <emscripten/emscripten.h>
+#include <emscripten/bind.h>
+
+using namespace emscripten;
+
+static const int MOVE = 0;
+static const int LINE = 1;
+static const int QUAD = 2;
+static const int CUBIC = 4;
+static const int CLOSE = 5;
+
+// =================================================================================
+// Creating/Exporting Paths
+// =================================================================================
+
+template <typename VisitFunc>
+void VisitPath(const SkPath& p, VisitFunc&& f) {
+ SkPath::RawIter iter(p);
+ SkPoint pts[4];
+ SkPath::Verb verb;
+ while ((verb = iter.next(pts)) != SkPath::kDone_Verb) {
+ f(verb, pts);
+ }
+}
+
+emscripten::val JSArray = emscripten::val::global("Array");
+
+emscripten::val EMSCRIPTEN_KEEPALIVE ToCmds(SkPath path) {
+ val cmds = JSArray.new_();
+
+ VisitPath(path, [&cmds](SkPath::Verb verb, const SkPoint pts[4]) {
+ val cmd = JSArray.new_();
+ switch (verb) {
+ case SkPath::kMove_Verb:
+ cmd.call<void>("push", MOVE, pts[0].x(), pts[0].y());
+ break;
+ case SkPath::kLine_Verb:
+ cmd.call<void>("push", LINE, pts[1].x(), pts[1].y());
+ break;
+ case SkPath::kQuad_Verb:
+ cmd.call<void>("push", QUAD, pts[1].x(), pts[1].y(), pts[2].x(), pts[2].y());
+ break;
+ case SkPath::kConic_Verb:
+ printf("unsupported conic verb\n");
+ // TODO(kjlubick): Port in the logic from SkParsePath::ToSVGString?
+ break;
+ case SkPath::kCubic_Verb:
+ cmd.call<void>("push", CUBIC,
+ pts[1].x(), pts[1].y(),
+ pts[2].x(), pts[2].y(),
+ pts[3].x(), pts[3].y());
+ break;
+ case SkPath::kClose_Verb:
+ cmd.call<void>("push", CLOSE);
+ break;
+ case SkPath::kDone_Verb:
+ SkASSERT(false);
+ break;
+ }
+ cmds.call<void>("push", cmd);
+ });
+ return cmds;
+}
+
+// This type signature is a mess, but it's necessary. See, we can't use "bind" (EMSCRIPTEN_BINDINGS)
+// and pointers to primitive types (Only bound types like SkPoint). We could if we used
+// cwrap (see https://becominghuman.ai/passing-and-returning-webassembly-array-parameters-a0f572c65d97)
+// but that requires us to stick to C code and, AFAIK, doesn't allow us to return nice things like
+// SkPath or SkOpBuilder.
+//
+// So, basically, if we are using C++ and EMSCRIPTEN_BINDINGS, we can't have primative pointers
+// in our function type signatures. (this gives an error message like "Cannot call foo due to unbound
+// types Pi, Pf"). But, we can just pretend they are numbers and cast them to be pointers and
+// the compiler is happy.
+SkPath EMSCRIPTEN_KEEPALIVE FromCmds(uintptr_t /* float* */ cptr, int numCmds) {
+ const auto* cmds = reinterpret_cast<const float*>(cptr);
+ SkPath path;
+ float x1, y1, x2, y2, x3, y3;
+
+ // if there are not enough arguments, bail with the path we've constructed so far.
+ #define CHECK_NUM_ARGS(n) \
+ if ((i + n) > numCmds) { \
+ SkDebugf("Not enough args to match the verbs. Saw %d commands\n", numCmds); \
+ return path; \
+ }
+
+ for(int i = 0; i < numCmds;){
+ switch (sk_float_floor2int(cmds[i++])) {
+ case MOVE:
+ CHECK_NUM_ARGS(2);
+ x1 = cmds[i++], y1 = cmds[i++];
+ path.moveTo(x1, y1);
+ break;
+ case LINE:
+ CHECK_NUM_ARGS(2);
+ x1 = cmds[i++], y1 = cmds[i++];
+ path.lineTo(x1, y1);
+ break;
+ case QUAD:
+ CHECK_NUM_ARGS(4);
+ x1 = cmds[i++], y1 = cmds[i++];
+ x2 = cmds[i++], y2 = cmds[i++];
+ path.quadTo(x1, y1, x2, y2);
+ break;
+ case CUBIC:
+ CHECK_NUM_ARGS(6);
+ x1 = cmds[i++], y1 = cmds[i++];
+ x2 = cmds[i++], y2 = cmds[i++];
+ x3 = cmds[i++], y3 = cmds[i++];
+ path.cubicTo(x1, y1, x2, y2, x3, y3);
+ break;
+ case CLOSE:
+ path.close();
+ break;
+ default:
+ SkDebugf(" path: UNKNOWN command %f, aborting dump...\n", cmds[i-1]);
+ return path;
+ }
+ }
+
+ #undef CHECK_NUM_ARGS
+
+ return path;
+}
+
+SkPath EMSCRIPTEN_KEEPALIVE NewPath() {
+ return SkPath();
+}
+
+//========================================================================================
+// SVG THINGS
+//========================================================================================
+
+val EMSCRIPTEN_KEEPALIVE ToSVGString(SkPath path) {
+ SkString s;
+ SkParsePath::ToSVGString(path, &s);
+ // Wrapping it in val automatically turns it into a JS string.
+ // Not too sure on performance implications, but is is simpler than
+ // returning a raw pointer to const char * and then using
+ // Pointer_stringify() on the calling side.
+ return val(s.c_str());
+}
+
+
+SkPath EMSCRIPTEN_KEEPALIVE FromSVGString(std::string str) {
+ SkPath path;
+ SkParsePath::FromSVGString(str.c_str(), &path);
+ return path;
+}
+
+//========================================================================================
+// PATHOP THINGS
+//========================================================================================
+
+SkPath EMSCRIPTEN_KEEPALIVE SimplifyPath(SkPath path) {
+ SkPath simple;
+ Simplify(path, &simple);
+ return simple;
+}
+
+SkPath EMSCRIPTEN_KEEPALIVE ApplyPathOp(SkPath pathOne, SkPath pathTwo, SkPathOp op) {
+ SkPath path;
+ Op(pathOne, pathTwo, op, &path);
+ return path;
+}
+
+SkPath EMSCRIPTEN_KEEPALIVE ResolveBuilder(SkOpBuilder builder) {
+ SkPath path;
+ builder.resolve(&path);
+ return path;
+}
+
+//========================================================================================
+// Canvas THINGS
+//========================================================================================
+
+void EMSCRIPTEN_KEEPALIVE ToCanvas(SkPath path, val/* Path2D or Canvas*/ ctx) {
+ SkPath::Iter iter(path, false);
+ SkPoint pts[4];
+ SkPath::Verb verb;
+ while ((verb = iter.next(pts, false)) != SkPath::kDone_Verb) {
+ switch (verb) {
+ case SkPath::kMove_Verb:
+ ctx.call<void>("moveTo", pts[0].x(), pts[0].y());
+ break;
+ case SkPath::kLine_Verb:
+ ctx.call<void>("lineTo", pts[1].x(), pts[1].y());
+ break;
+ case SkPath::kQuad_Verb:
+ ctx.call<void>("quadraticCurveTo", pts[1].x(), pts[1].y(), pts[2].x(), pts[2].y());
+ break;
+ case SkPath::kConic_Verb:
+ printf("unsupported conic verb\n");
+ // TODO(kjlubick): Port in the logic from SkParsePath::ToSVGString?
+ break;
+ case SkPath::kCubic_Verb:
+ ctx.call<void>("bezierCurveTo", pts[1].x(), pts[1].y(), pts[2].x(), pts[2].y(),
+ pts[3].x(), pts[3].y());
+ break;
+ case SkPath::kClose_Verb:
+ ctx.call<void>("closePath");
+ break;
+ case SkPath::kDone_Verb:
+ break;
+ }
+ }
+}
+
+emscripten::val JSPath2D = emscripten::val::global("Path2D");
+
+emscripten::val EMSCRIPTEN_KEEPALIVE ToPath2D(SkPath path) {
+ val retVal = JSPath2D.new_();
+ ToCanvas(path, retVal);
+ return retVal;
+}
+
+//========================================================================================
+// Region things
+//========================================================================================
+
+#ifdef PATHKIT_TESTING
+SkPath GetBoundaryPathFromRegion(SkRegion region) {
+ SkPath p;
+ region.getBoundaryPath(&p);
+ return p;
+}
+#endif
+
+// Binds the classes to the JS
+EMSCRIPTEN_BINDINGS(skia) {
+ class_<SkPath>("SkPath")
+ .constructor<>()
+
+ .function("moveTo",
+ select_overload<void(SkScalar, SkScalar)>(&SkPath::moveTo))
+ .function("lineTo",
+ select_overload<void(SkScalar, SkScalar)>(&SkPath::lineTo))
+ .function("quadTo",
+ select_overload<void(SkScalar, SkScalar, SkScalar, SkScalar)>(&SkPath::quadTo))
+ .function("cubicTo",
+ select_overload<void(SkScalar, SkScalar, SkScalar, SkScalar, SkScalar, SkScalar)>(&SkPath::cubicTo))
+ .function("close", &SkPath::close)
+#ifdef PATHKIT_TESTING
+ .function("dump", select_overload<void() const>(&SkPath::dump))
+#endif
+ ;
+
+ class_<SkOpBuilder>("SkOpBuilder")
+ .constructor<>()
+
+ .function("add", &SkOpBuilder::add);
+
+
+ // Without these function() bindings, the function would be exposed but oblivious to
+ // our types (e.g. SkPath)
+
+ // Import
+ function("FromSVGString", &FromSVGString);
+ function("FromCmds", &FromCmds);
+ function("NewPath", &NewPath);
+ // Path2D is opaque, so we can't read in from it.
+
+ // Export
+ function("ToPath2D", &ToPath2D);
+ function("ToCanvas", &ToCanvas);
+ function("ToSVGString", &ToSVGString);
+ function("ToCmds", &ToCmds);
+
+ // PathOps
+ function("SimplifyPath", &SimplifyPath);
+ function("ApplyPathOp", &ApplyPathOp);
+ function("ResolveBuilder", &ResolveBuilder);
+
+ enum_<SkPathOp>("PathOp")
+ .value("DIFFERENCE", SkPathOp::kDifference_SkPathOp)
+ .value("INTERSECT", SkPathOp::kIntersect_SkPathOp)
+ .value("UNION", SkPathOp::kUnion_SkPathOp)
+ .value("XOR", SkPathOp::kXOR_SkPathOp)
+ .value("REVERSE_DIFFERENCE", SkPathOp::kReverseDifference_SkPathOp);
+
+ constant("MOVE_VERB", MOVE);
+ constant("LINE_VERB", LINE);
+ constant("QUAD_VERB", QUAD);
+ constant("CUBIC_VERB", CUBIC);
+ constant("CLOSE_VERB", CLOSE);
+
+ // coming soon - Stroke
+
+ // coming soon - Matrix
+
+ // coming soon - Bounds/Trim
+
+#ifdef PATHKIT_TESTING
+ function("SkBits2Float", &SkBits2Float);
+
+ function("GetBoundaryPathFromRegion", &GetBoundaryPathFromRegion);
+
+ enum_<SkRegion::Op>("RegionOp")
+ .value("DIFFERENCE", SkRegion::Op::kDifference_Op)
+ .value("INTERSECT", SkRegion::Op::kIntersect_Op)
+ .value("UNION", SkRegion::Op::kUnion_Op)
+ .value("XOR", SkRegion::Op::kXOR_Op)
+ .value("REVERSE_DIFFERENCE", SkRegion::Op::kReverseDifference_Op)
+ .value("REPLACE", SkRegion::Op::kReplace_Op);
+
+ class_<SkRegion>("SkRegion")
+ .constructor<>()
+
+ .function("setRect",
+ select_overload<bool(int32_t, int32_t, int32_t, int32_t)>(&SkRegion::setRect))
+ .function("setPath", &SkRegion::setPath)
+ .function("opLTRB",
+ select_overload<bool(int32_t, int32_t, int32_t, int32_t, SkRegion::Op)>(&SkRegion::op))
+ .function("opRegion",
+ select_overload<bool(const SkRegion&, SkRegion::Op)>(&SkRegion::op))
+ .function("opRegionAB",
+ select_overload<bool(const SkRegion&, const SkRegion&, SkRegion::Op)>(&SkRegion::op));
+#endif
+}