blob: 52932e910830121db69d0216d88504c4cb2b6f3e [file] [log] [blame]
Kevin Lubick217056c2018-09-20 17:39:31 -04001/*
2 * Copyright 2018 Google LLC
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
Kevin Lubick53965c92018-10-11 08:51:55 -04008#if SK_SUPPORT_GPU
Kevin Lubick217056c2018-09-20 17:39:31 -04009#include "GrBackendSurface.h"
10#include "GrContext.h"
11#include "GrGLInterface.h"
12#include "GrGLTypes.h"
Kevin Lubick53965c92018-10-11 08:51:55 -040013#endif
14
Kevin Lubickb5ae3b52018-11-03 07:51:19 -040015#include "SkBlendMode.h"
Kevin Lubickea905ec2018-11-30 14:05:58 -050016#include "SkBlurTypes.h"
Kevin Lubick217056c2018-09-20 17:39:31 -040017#include "SkCanvas.h"
Kevin Lubickb5ae3b52018-11-03 07:51:19 -040018#include "SkColor.h"
Kevin Lubick217056c2018-09-20 17:39:31 -040019#include "SkCornerPathEffect.h"
Kevin Lubickb5ae3b52018-11-03 07:51:19 -040020#include "SkDashPathEffect.h"
Kevin Lubickb5ae3b52018-11-03 07:51:19 -040021#include "SkData.h"
Kevin Lubick217056c2018-09-20 17:39:31 -040022#include "SkDiscretePathEffect.h"
Alexander Khovansky3e119332018-11-15 02:01:19 +030023#include "SkEncodedImageFormat.h"
Kevin Lubick0a1293c2018-12-03 12:31:04 -050024#include "SkFilterQuality.h"
Kevin Lubick217056c2018-09-20 17:39:31 -040025#include "SkFontMgr.h"
26#include "SkFontMgrPriv.h"
Kevin Lubickb5ae3b52018-11-03 07:51:19 -040027#include "SkGradientShader.h"
Kevin Lubick0a1293c2018-12-03 12:31:04 -050028#include "SkImage.h"
Kevin Lubickea905ec2018-11-30 14:05:58 -050029#include "SkImageInfo.h"
Kevin Lubickb5ae3b52018-11-03 07:51:19 -040030#include "SkImageShader.h"
Florin Malita91af8d82018-11-30 16:46:45 -050031#include "SkMakeUnique.h"
Kevin Lubick61ef7b22018-11-27 13:26:59 -050032#include "SkMaskFilter.h"
Kevin Lubick217056c2018-09-20 17:39:31 -040033#include "SkPaint.h"
Kevin Lubickb5ae3b52018-11-03 07:51:19 -040034#include "SkParsePath.h"
Kevin Lubick217056c2018-09-20 17:39:31 -040035#include "SkPath.h"
36#include "SkPathEffect.h"
Kevin Lubickb5ae3b52018-11-03 07:51:19 -040037#include "SkPathOps.h"
Kevin Lubick217056c2018-09-20 17:39:31 -040038#include "SkScalar.h"
Kevin Lubickb5ae3b52018-11-03 07:51:19 -040039#include "SkShader.h"
Kevin Lubick61ef7b22018-11-27 13:26:59 -050040#include "SkShadowUtils.h"
Kevin Lubickb5ae3b52018-11-03 07:51:19 -040041#include "SkString.h"
42#include "SkStrokeRec.h"
Kevin Lubick217056c2018-09-20 17:39:31 -040043#include "SkSurface.h"
44#include "SkSurfaceProps.h"
45#include "SkTestFontMgr.h"
Kevin Lubickb5ae3b52018-11-03 07:51:19 -040046#include "SkTrimPathEffect.h"
47#include "SkVertices.h"
48
Kevin Lubick53965c92018-10-11 08:51:55 -040049#if SK_INCLUDE_SKOTTIE
Kevin Lubick217056c2018-09-20 17:39:31 -040050#include "Skottie.h"
Florin Malitac2e85f92018-11-29 11:38:12 -050051#if SK_INCLUDE_MANAGED_SKOTTIE
52#include "SkottieProperty.h"
53#include "SkottieUtils.h"
54#endif // SK_INCLUDE_MANAGED_SKOTTIE
55#endif // SK_INCLUDE_SKOTTIE
Kevin Lubick217056c2018-09-20 17:39:31 -040056
57#include <iostream>
58#include <string>
Kevin Lubick217056c2018-09-20 17:39:31 -040059
60#include <emscripten.h>
61#include <emscripten/bind.h>
Kevin Lubick53965c92018-10-11 08:51:55 -040062#if SK_SUPPORT_GPU
63#include <GL/gl.h>
Kevin Lubick217056c2018-09-20 17:39:31 -040064#include <emscripten/html5.h>
Kevin Lubick53965c92018-10-11 08:51:55 -040065#endif
Kevin Lubick217056c2018-09-20 17:39:31 -040066
67using namespace emscripten;
68
Kevin Lubick134be1d2018-10-30 15:05:04 -040069// Self-documenting types
70using JSArray = emscripten::val;
Kevin Lubick217056c2018-09-20 17:39:31 -040071using JSColor = int32_t;
Florin Malitac2e85f92018-11-29 11:38:12 -050072using JSObject = emscripten::val;
Kevin Lubickb5ae3b52018-11-03 07:51:19 -040073using JSString = emscripten::val;
74using SkPathOrNull = emscripten::val;
75using Uint8Array = emscripten::val;
76
77// Aliases for less typing
78using BoneIndices = SkVertices::BoneIndices;
79using BoneWeights = SkVertices::BoneWeights;
80using Bone = SkVertices::Bone;
Kevin Lubick217056c2018-09-20 17:39:31 -040081
Kevin Lubick217056c2018-09-20 17:39:31 -040082void EMSCRIPTEN_KEEPALIVE initFonts() {
83 gSkFontMgr_DefaultFactory = &sk_tool_utils::MakePortableFontMgr;
84}
85
Kevin Lubick53965c92018-10-11 08:51:55 -040086#if SK_SUPPORT_GPU
Kevin Lubick217056c2018-09-20 17:39:31 -040087// Wraps the WebGL context in an SkSurface and returns it.
Kevin Lubick9e40e6f2018-10-22 12:08:22 -040088// This function based on the work of
89// https://github.com/Zubnix/skia-wasm-port/, used under the terms of the MIT license.
Kevin Lubick217056c2018-09-20 17:39:31 -040090sk_sp<SkSurface> getWebGLSurface(std::string id, int width, int height) {
91 // Context configurations
92 EmscriptenWebGLContextAttributes attrs;
93 emscripten_webgl_init_context_attributes(&attrs);
94 attrs.alpha = true;
95 attrs.premultipliedAlpha = true;
96 attrs.majorVersion = 1;
97 attrs.enableExtensionsByDefault = true;
98
99 EMSCRIPTEN_WEBGL_CONTEXT_HANDLE context = emscripten_webgl_create_context(id.c_str(), &attrs);
100 if (context < 0) {
101 printf("failed to create webgl context %d\n", context);
102 return nullptr;
103 }
104 EMSCRIPTEN_RESULT r = emscripten_webgl_make_context_current(context);
105 if (r < 0) {
106 printf("failed to make webgl current %d\n", r);
107 return nullptr;
108 }
109
110 glClearColor(0, 0, 0, 0);
111 glClearStencil(0);
112 glClear(GL_COLOR_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
113
114 // setup GrContext
115 auto interface = GrGLMakeNativeInterface();
116
117 // setup contexts
118 sk_sp<GrContext> grContext(GrContext::MakeGL(interface));
119
120 // Wrap the frame buffer object attached to the screen in a Skia render target so Skia can
121 // render to it
122 GrGLint buffer;
123 glGetIntegerv(GL_FRAMEBUFFER_BINDING, &buffer);
124 GrGLFramebufferInfo info;
125 info.fFBOID = (GrGLuint) buffer;
126 SkColorType colorType;
127
128 info.fFormat = GL_RGBA8;
129 colorType = kRGBA_8888_SkColorType;
130
131 GrBackendRenderTarget target(width, height, 0, 8, info);
132
133 sk_sp<SkSurface> surface(SkSurface::MakeFromBackendRenderTarget(grContext.get(), target,
134 kBottomLeft_GrSurfaceOrigin,
135 colorType, nullptr, nullptr));
136 return surface;
137}
Kevin Lubick53965c92018-10-11 08:51:55 -0400138#endif
Kevin Lubick217056c2018-09-20 17:39:31 -0400139
Kevin Lubickb5ae3b52018-11-03 07:51:19 -0400140struct SimpleMatrix {
141 SkScalar scaleX, skewX, transX;
142 SkScalar skewY, scaleY, transY;
143 SkScalar pers0, pers1, pers2;
144};
145
146SkMatrix toSkMatrix(const SimpleMatrix& sm) {
147 return SkMatrix::MakeAll(sm.scaleX, sm.skewX , sm.transX,
148 sm.skewY , sm.scaleY, sm.transY,
149 sm.pers0 , sm.pers1 , sm.pers2);
Kevin Lubick217056c2018-09-20 17:39:31 -0400150}
151
Kevin Lubickea905ec2018-11-30 14:05:58 -0500152struct SimpleImageInfo {
153 int width;
154 int height;
155 SkColorType colorType;
156 SkAlphaType alphaType;
157 // TODO color spaces?
158};
159
160SkImageInfo toSkImageInfo(const SimpleImageInfo& sii) {
161 return SkImageInfo::Make(sii.width, sii.height, sii.colorType, sii.alphaType);
162}
163
Florin Malitac2e85f92018-11-29 11:38:12 -0500164#if SK_INCLUDE_SKOTTIE && SK_INCLUDE_MANAGED_SKOTTIE
165namespace {
166
167class ManagedAnimation final : public SkRefCnt {
168public:
169 static sk_sp<ManagedAnimation> Make(const std::string& json) {
Florin Malita91af8d82018-11-30 16:46:45 -0500170 auto mgr = skstd::make_unique<skottie_utils::CustomPropertyManager>();
Florin Malitac2e85f92018-11-29 11:38:12 -0500171 auto animation = skottie::Animation::Builder()
Florin Malita6ba8c7e2018-12-03 09:55:52 -0500172 .setMarkerObserver(mgr->getMarkerObserver())
Florin Malita91af8d82018-11-30 16:46:45 -0500173 .setPropertyObserver(mgr->getPropertyObserver())
Florin Malitac2e85f92018-11-29 11:38:12 -0500174 .make(json.c_str(), json.size());
175
176 return animation
Florin Malita91af8d82018-11-30 16:46:45 -0500177 ? sk_sp<ManagedAnimation>(new ManagedAnimation(std::move(animation), std::move(mgr)))
Florin Malitac2e85f92018-11-29 11:38:12 -0500178 : nullptr;
179 }
180
181 // skottie::Animation API
182 void render(SkCanvas* canvas) const { fAnimation->render(canvas, nullptr); }
183 void render(SkCanvas* canvas, const SkRect& dst) const { fAnimation->render(canvas, &dst); }
184 void seek(SkScalar t) { fAnimation->seek(t); }
185 SkScalar duration() const { return fAnimation->duration(); }
186 const SkSize& size() const { return fAnimation->size(); }
187 std::string version() const { return std::string(fAnimation->version().c_str()); }
188
189 // CustomPropertyManager API
190 JSArray getColorProps() const {
191 JSArray props = emscripten::val::array();
192
193 for (const auto& cp : fPropMgr->getColorProps()) {
194 JSObject prop = emscripten::val::object();
195 prop.set("key", cp);
196 prop.set("value", fPropMgr->getColor(cp));
197 props.call<void>("push", prop);
198 }
199
200 return props;
201 }
202
203 JSArray getOpacityProps() const {
204 JSArray props = emscripten::val::array();
205
206 for (const auto& op : fPropMgr->getOpacityProps()) {
207 JSObject prop = emscripten::val::object();
208 prop.set("key", op);
209 prop.set("value", fPropMgr->getOpacity(op));
210 props.call<void>("push", prop);
211 }
212
213 return props;
214 }
215
216 bool setColor(const std::string& key, JSColor c) {
217 return fPropMgr->setColor(key, static_cast<SkColor>(c));
218 }
219
220 bool setOpacity(const std::string& key, float o) {
221 return fPropMgr->setOpacity(key, o);
222 }
223
Florin Malita6ba8c7e2018-12-03 09:55:52 -0500224 JSArray getMarkers() const {
225 JSArray markers = emscripten::val::array();
226 for (const auto& m : fPropMgr->markers()) {
227 JSObject marker = emscripten::val::object();
228 marker.set("name", m.name);
229 marker.set("t0" , m.t0);
230 marker.set("t1" , m.t1);
231 markers.call<void>("push", marker);
232 }
233 return markers;
234 }
235
Florin Malitac2e85f92018-11-29 11:38:12 -0500236private:
237 ManagedAnimation(sk_sp<skottie::Animation> animation,
238 std::unique_ptr<skottie_utils::CustomPropertyManager> propMgr)
239 : fAnimation(std::move(animation))
240 , fPropMgr(std::move(propMgr)) {}
241
242 sk_sp<skottie::Animation> fAnimation;
243 std::unique_ptr<skottie_utils::CustomPropertyManager> fPropMgr;
244};
245
246} // anonymous ns
247#endif // SK_INCLUDE_SKOTTIE
248
Kevin Lubick217056c2018-09-20 17:39:31 -0400249//========================================================================================
250// Path things
251//========================================================================================
252
253// All these Apply* methods are simple wrappers to avoid returning an object.
254// The default WASM bindings produce code that will leak if a return value
255// isn't assigned to a JS variable and has delete() called on it.
256// These Apply methods, combined with the smarter binding code allow for chainable
257// commands that don't leak if the return value is ignored (i.e. when used intuitively).
258
Kevin Lubick1a05fce2018-11-20 12:51:16 -0500259void ApplyAddArc(SkPath& orig, const SkRect& oval, SkScalar startAngle, SkScalar sweepAngle) {
260 orig.addArc(oval, startAngle, sweepAngle);
261}
262
Kevin Lubick217056c2018-09-20 17:39:31 -0400263void ApplyAddPath(SkPath& orig, const SkPath& newPath,
264 SkScalar scaleX, SkScalar skewX, SkScalar transX,
265 SkScalar skewY, SkScalar scaleY, SkScalar transY,
Kevin Lubick1a05fce2018-11-20 12:51:16 -0500266 SkScalar pers0, SkScalar pers1, SkScalar pers2,
267 bool extendPath) {
Kevin Lubick217056c2018-09-20 17:39:31 -0400268 SkMatrix m = SkMatrix::MakeAll(scaleX, skewX , transX,
269 skewY , scaleY, transY,
270 pers0 , pers1 , pers2);
Kevin Lubick1a05fce2018-11-20 12:51:16 -0500271 orig.addPath(newPath, m, extendPath ? SkPath::kExtend_AddPathMode :
272 SkPath::kAppend_AddPathMode);
Kevin Lubick217056c2018-09-20 17:39:31 -0400273}
274
Kevin Lubick1a05fce2018-11-20 12:51:16 -0500275void ApplyAddRect(SkPath& path, SkScalar left, SkScalar top,
276 SkScalar right, SkScalar bottom, bool ccw) {
277 path.addRect(left, top, right, bottom,
278 ccw ? SkPath::Direction::kCW_Direction :
279 SkPath::Direction::kCCW_Direction);
Alexander Khovansky3e119332018-11-15 02:01:19 +0300280}
281
Kevin Lubick217056c2018-09-20 17:39:31 -0400282void ApplyArcTo(SkPath& p, SkScalar x1, SkScalar y1, SkScalar x2, SkScalar y2,
283 SkScalar radius) {
284 p.arcTo(x1, y1, x2, y2, radius);
285}
286
287void ApplyClose(SkPath& p) {
288 p.close();
289}
290
291void ApplyConicTo(SkPath& p, SkScalar x1, SkScalar y1, SkScalar x2, SkScalar y2,
292 SkScalar w) {
293 p.conicTo(x1, y1, x2, y2, w);
294}
295
296void ApplyCubicTo(SkPath& p, SkScalar x1, SkScalar y1, SkScalar x2, SkScalar y2,
297 SkScalar x3, SkScalar y3) {
298 p.cubicTo(x1, y1, x2, y2, x3, y3);
299}
300
301void ApplyLineTo(SkPath& p, SkScalar x, SkScalar y) {
302 p.lineTo(x, y);
303}
304
305void ApplyMoveTo(SkPath& p, SkScalar x, SkScalar y) {
306 p.moveTo(x, y);
307}
308
309void ApplyQuadTo(SkPath& p, SkScalar x1, SkScalar y1, SkScalar x2, SkScalar y2) {
310 p.quadTo(x1, y1, x2, y2);
311}
312
313void ApplyTransform(SkPath& orig,
314 SkScalar scaleX, SkScalar skewX, SkScalar transX,
315 SkScalar skewY, SkScalar scaleY, SkScalar transY,
316 SkScalar pers0, SkScalar pers1, SkScalar pers2) {
317 SkMatrix m = SkMatrix::MakeAll(scaleX, skewX , transX,
318 skewY , scaleY, transY,
319 pers0 , pers1 , pers2);
320 orig.transform(m);
321}
322
Kevin Lubickb5ae3b52018-11-03 07:51:19 -0400323bool EMSCRIPTEN_KEEPALIVE ApplySimplify(SkPath& path) {
324 return Simplify(path, &path);
325}
326
327bool EMSCRIPTEN_KEEPALIVE ApplyPathOp(SkPath& pathOne, const SkPath& pathTwo, SkPathOp op) {
328 return Op(pathOne, pathTwo, op, &pathOne);
329}
330
331JSString EMSCRIPTEN_KEEPALIVE ToSVGString(const SkPath& path) {
332 SkString s;
333 SkParsePath::ToSVGString(path, &s);
334 return emscripten::val(s.c_str());
335}
336
337SkPathOrNull EMSCRIPTEN_KEEPALIVE MakePathFromOp(const SkPath& pathOne, const SkPath& pathTwo, SkPathOp op) {
338 SkPath out;
339 if (Op(pathOne, pathTwo, op, &out)) {
340 return emscripten::val(out);
341 }
342 return emscripten::val::null();
343}
344
Kevin Lubick217056c2018-09-20 17:39:31 -0400345SkPath EMSCRIPTEN_KEEPALIVE CopyPath(const SkPath& a) {
346 SkPath copy(a);
347 return copy;
348}
349
350bool EMSCRIPTEN_KEEPALIVE Equals(const SkPath& a, const SkPath& b) {
351 return a == b;
352}
353
Kevin Lubickb5ae3b52018-11-03 07:51:19 -0400354//========================================================================================
355// Path Effects
356//========================================================================================
357
358bool ApplyDash(SkPath& path, SkScalar on, SkScalar off, SkScalar phase) {
359 SkScalar intervals[] = { on, off };
360 auto pe = SkDashPathEffect::Make(intervals, 2, phase);
361 if (!pe) {
362 SkDebugf("Invalid args to dash()\n");
363 return false;
364 }
365 SkStrokeRec rec(SkStrokeRec::InitStyle::kHairline_InitStyle);
366 if (pe->filterPath(&path, path, &rec, nullptr)) {
367 return true;
368 }
369 SkDebugf("Could not make dashed path\n");
370 return false;
Kevin Lubick217056c2018-09-20 17:39:31 -0400371}
372
Kevin Lubickb5ae3b52018-11-03 07:51:19 -0400373bool ApplyTrim(SkPath& path, SkScalar startT, SkScalar stopT, bool isComplement) {
374 auto mode = isComplement ? SkTrimPathEffect::Mode::kInverted : SkTrimPathEffect::Mode::kNormal;
375 auto pe = SkTrimPathEffect::Make(startT, stopT, mode);
376 if (!pe) {
377 SkDebugf("Invalid args to trim(): startT and stopT must be in [0,1]\n");
378 return false;
379 }
380 SkStrokeRec rec(SkStrokeRec::InitStyle::kHairline_InitStyle);
381 if (pe->filterPath(&path, path, &rec, nullptr)) {
382 return true;
383 }
384 SkDebugf("Could not trim path\n");
385 return false;
386}
387
388struct StrokeOpts {
Kevin Lubickb9db3902018-11-26 11:47:54 -0500389 // Default values are set in interface.js which allows clients
Kevin Lubickb5ae3b52018-11-03 07:51:19 -0400390 // to set any number of them. Otherwise, the binding code complains if
391 // any are omitted.
392 SkScalar width;
393 SkScalar miter_limit;
394 SkPaint::Join join;
395 SkPaint::Cap cap;
396};
397
398bool ApplyStroke(SkPath& path, StrokeOpts opts) {
399 SkPaint p;
400 p.setStyle(SkPaint::kStroke_Style);
401 p.setStrokeCap(opts.cap);
402 p.setStrokeJoin(opts.join);
403 p.setStrokeWidth(opts.width);
404 p.setStrokeMiter(opts.miter_limit);
405
406 return p.getFillPath(path, &path);
407}
408
409// to map from raw memory to a uint8array
410Uint8Array getSkDataBytes(const SkData *data) {
411 return Uint8Array(typed_memory_view(data->size(), data->bytes()));
412}
413
414// These objects have private destructors / delete mthods - I don't think
415// we need to do anything other than tell emscripten to do nothing.
Kevin Lubick217056c2018-09-20 17:39:31 -0400416namespace emscripten {
417 namespace internal {
418 template<typename ClassType>
419 void raw_destructor(ClassType *);
420
421 template<>
422 void raw_destructor<SkData>(SkData *ptr) {
423 }
Kevin Lubickb5ae3b52018-11-03 07:51:19 -0400424
425 template<>
426 void raw_destructor<SkVertices>(SkVertices *ptr) {
427 }
Kevin Lubick217056c2018-09-20 17:39:31 -0400428 }
429}
430
431// Some timesignatures below have uintptr_t instead of a pointer to a primative
432// type (e.g. SkScalar). This is necessary because we can't use "bind" (EMSCRIPTEN_BINDINGS)
433// and pointers to primitive types (Only bound types like SkPoint). We could if we used
434// cwrap (see https://becominghuman.ai/passing-and-returning-webassembly-array-parameters-a0f572c65d97)
435// but that requires us to stick to C code and, AFAIK, doesn't allow us to return nice things like
436// SkPath or SkCanvas.
437//
438// So, basically, if we are using C++ and EMSCRIPTEN_BINDINGS, we can't have primative pointers
439// in our function type signatures. (this gives an error message like "Cannot call foo due to unbound
440// types Pi, Pf"). But, we can just pretend they are numbers and cast them to be pointers and
441// the compiler is happy.
442EMSCRIPTEN_BINDINGS(Skia) {
443 function("initFonts", &initFonts);
Kevin Lubick53965c92018-10-11 08:51:55 -0400444#if SK_SUPPORT_GPU
Kevin Lubick217056c2018-09-20 17:39:31 -0400445 function("_getWebGLSurface", &getWebGLSurface, allow_raw_pointers());
Kevin Lubick53965c92018-10-11 08:51:55 -0400446 function("currentContext", &emscripten_webgl_get_current_context);
447 function("setCurrentContext", &emscripten_webgl_make_context_current);
Kevin Lubick3d99b1e2018-10-16 10:15:01 -0400448 constant("gpu", true);
Kevin Lubickb07204a2018-11-20 14:07:42 -0500449#endif
Kevin Lubick0a1293c2018-12-03 12:31:04 -0500450 function("_decodeImage", optional_override([](uintptr_t /* uint8_t* */ iptr,
451 size_t length)->sk_sp<SkImage> {
452 uint8_t* imgData = reinterpret_cast<uint8_t*>(iptr);
453 sk_sp<SkData> bytes = SkData::MakeWithoutCopy(imgData, length);
454 return SkImage::MakeFromEncoded(bytes);
455 }), allow_raw_pointers());
Kevin Lubickea905ec2018-11-30 14:05:58 -0500456 function("_getRasterDirectSurface", optional_override([](const SimpleImageInfo ii,
Kevin Lubick52b9f372018-12-04 13:57:36 -0500457 uintptr_t /* uint8_t* */ pPtr,
Kevin Lubickea905ec2018-11-30 14:05:58 -0500458 size_t rowBytes)->sk_sp<SkSurface> {
Kevin Lubick52b9f372018-12-04 13:57:36 -0500459 uint8_t* pixels = reinterpret_cast<uint8_t*>(pPtr);
Kevin Lubickea905ec2018-11-30 14:05:58 -0500460 SkImageInfo imageInfo = toSkImageInfo(ii);
461 return SkSurface::MakeRasterDirect(imageInfo, pixels, rowBytes, nullptr);
462 }), allow_raw_pointers());
Kevin Lubick53965c92018-10-11 08:51:55 -0400463 function("_getRasterN32PremulSurface", optional_override([](int width, int height)->sk_sp<SkSurface> {
464 return SkSurface::MakeRasterN32Premul(width, height, nullptr);
465 }), allow_raw_pointers());
Kevin Lubickb07204a2018-11-20 14:07:42 -0500466
Kevin Lubickb5ae3b52018-11-03 07:51:19 -0400467 function("getSkDataBytes", &getSkDataBytes, allow_raw_pointers());
Kevin Lubick217056c2018-09-20 17:39:31 -0400468 function("MakeSkCornerPathEffect", &SkCornerPathEffect::Make, allow_raw_pointers());
469 function("MakeSkDiscretePathEffect", &SkDiscretePathEffect::Make, allow_raw_pointers());
Kevin Lubick61ef7b22018-11-27 13:26:59 -0500470 function("MakeBlurMaskFilter", optional_override([](SkBlurStyle style, SkScalar sigma, bool respectCTM)->sk_sp<SkMaskFilter> {
471 // Adds a little helper because emscripten doesn't expose default params.
472 return SkMaskFilter::MakeBlur(style, sigma, respectCTM);
473 }), allow_raw_pointers());
Kevin Lubickb5ae3b52018-11-03 07:51:19 -0400474 function("MakePathFromOp", &MakePathFromOp);
475
476 // These won't be called directly, there's a JS helper to deal with typed arrays.
Kevin Lubick217056c2018-09-20 17:39:31 -0400477 function("_MakeSkDashPathEffect", optional_override([](uintptr_t /* float* */ cptr, int count, SkScalar phase)->sk_sp<SkPathEffect> {
478 // See comment above for uintptr_t explanation
479 const float* intervals = reinterpret_cast<const float*>(cptr);
480 return SkDashPathEffect::Make(intervals, count, phase);
481 }), allow_raw_pointers());
Kevin Lubick52b9f372018-12-04 13:57:36 -0500482 function("_MakeImage", optional_override([](SimpleImageInfo ii,
483 uintptr_t /* uint8_t* */ pPtr, int plen,
484 size_t rowBytes)->sk_sp<SkImage> {
485 // See comment above for uintptr_t explanation
486 uint8_t* pixels = reinterpret_cast<uint8_t*>(pPtr);
487 SkImageInfo info = toSkImageInfo(ii);
488 sk_sp<SkData> pixelData = SkData::MakeFromMalloc(pixels, plen);
489
490 return SkImage::MakeRasterData(info, pixelData, rowBytes);
491 }), allow_raw_pointers());
Kevin Lubickd29edd72018-12-07 08:29:52 -0500492 // Allow localMatrix to be optional, so we have 2 declarations of these shaders
493 function("_MakeImageShader", optional_override([](sk_sp<SkImage> img,
494 SkShader::TileMode tx, SkShader::TileMode ty,
495 bool clampAsIfUnpremul)->sk_sp<SkShader> {
496 return SkImageShader::Make(img, tx, ty, nullptr, clampAsIfUnpremul);
Kevin Lubickb5ae3b52018-11-03 07:51:19 -0400497 }), allow_raw_pointers());
Kevin Lubickd29edd72018-12-07 08:29:52 -0500498 function("_MakeImageShader", optional_override([](sk_sp<SkImage> img,
499 SkShader::TileMode tx, SkShader::TileMode ty,
500 bool clampAsIfUnpremul, const SimpleMatrix& lm)->sk_sp<SkShader> {
501 SkMatrix localMatrix = toSkMatrix(lm);
502
503 return SkImageShader::Make(img, tx, ty, &localMatrix, clampAsIfUnpremul);
504 }), allow_raw_pointers());
Kevin Lubickb5ae3b52018-11-03 07:51:19 -0400505 function("_MakeLinearGradientShader", optional_override([](SkPoint start, SkPoint end,
506 uintptr_t /* SkColor* */ cPtr, uintptr_t /* SkScalar* */ pPtr,
507 int count, SkShader::TileMode mode, uint32_t flags)->sk_sp<SkShader> {
508 SkPoint points[] = { start, end };
509 // See comment above for uintptr_t explanation
510 const SkColor* colors = reinterpret_cast<const SkColor*> (cPtr);
511 const SkScalar* positions = reinterpret_cast<const SkScalar*>(pPtr);
512
513 return SkGradientShader::MakeLinear(points, colors, positions, count,
514 mode, flags, nullptr);
515 }), allow_raw_pointers());
516 function("_MakeLinearGradientShader", optional_override([](SkPoint start, SkPoint end,
517 uintptr_t /* SkColor* */ cPtr, uintptr_t /* SkScalar* */ pPtr,
518 int count, SkShader::TileMode mode, uint32_t flags,
519 const SimpleMatrix& lm)->sk_sp<SkShader> {
520 SkPoint points[] = { start, end };
521 // See comment above for uintptr_t explanation
522 const SkColor* colors = reinterpret_cast<const SkColor*> (cPtr);
523 const SkScalar* positions = reinterpret_cast<const SkScalar*>(pPtr);
524
525 SkMatrix localMatrix = toSkMatrix(lm);
526
527 return SkGradientShader::MakeLinear(points, colors, positions, count,
528 mode, flags, &localMatrix);
529 }), allow_raw_pointers());
530 function("_MakeRadialGradientShader", optional_override([](SkPoint center, SkScalar radius,
531 uintptr_t /* SkColor* */ cPtr, uintptr_t /* SkScalar* */ pPtr,
532 int count, SkShader::TileMode mode, uint32_t flags)->sk_sp<SkShader> {
533 // See comment above for uintptr_t explanation
534 const SkColor* colors = reinterpret_cast<const SkColor*> (cPtr);
535 const SkScalar* positions = reinterpret_cast<const SkScalar*>(pPtr);
536
537 return SkGradientShader::MakeRadial(center, radius, colors, positions, count,
538 mode, flags, nullptr);
539 }), allow_raw_pointers());
540 function("_MakeRadialGradientShader", optional_override([](SkPoint center, SkScalar radius,
541 uintptr_t /* SkColor* */ cPtr, uintptr_t /* SkScalar* */ pPtr,
542 int count, SkShader::TileMode mode, uint32_t flags,
543 const SimpleMatrix& lm)->sk_sp<SkShader> {
544 // See comment above for uintptr_t explanation
545 const SkColor* colors = reinterpret_cast<const SkColor*> (cPtr);
546 const SkScalar* positions = reinterpret_cast<const SkScalar*>(pPtr);
547
548 SkMatrix localMatrix = toSkMatrix(lm);
549 return SkGradientShader::MakeRadial(center, radius, colors, positions, count,
550 mode, flags, &localMatrix);
551 }), allow_raw_pointers());
Kevin Lubickeb2f6b02018-11-29 15:07:02 -0500552 function("_MakeTwoPointConicalGradientShader", optional_override([](
553 SkPoint start, SkScalar startRadius,
554 SkPoint end, SkScalar endRadius,
555 uintptr_t /* SkColor* */ cPtr, uintptr_t /* SkScalar* */ pPtr,
556 int count, SkShader::TileMode mode, uint32_t flags)->sk_sp<SkShader> {
557 // See comment above for uintptr_t explanation
558 const SkColor* colors = reinterpret_cast<const SkColor*> (cPtr);
559 const SkScalar* positions = reinterpret_cast<const SkScalar*>(pPtr);
560
561 return SkGradientShader::MakeTwoPointConical(start, startRadius, end, endRadius,
562 colors, positions, count, mode,
563 flags, nullptr);
564 }), allow_raw_pointers());
565 function("_MakeTwoPointConicalGradientShader", optional_override([](
566 SkPoint start, SkScalar startRadius,
567 SkPoint end, SkScalar endRadius,
568 uintptr_t /* SkColor* */ cPtr, uintptr_t /* SkScalar* */ pPtr,
569 int count, SkShader::TileMode mode, uint32_t flags,
570 const SimpleMatrix& lm)->sk_sp<SkShader> {
571 // See comment above for uintptr_t explanation
572 const SkColor* colors = reinterpret_cast<const SkColor*> (cPtr);
573 const SkScalar* positions = reinterpret_cast<const SkScalar*>(pPtr);
574
575 SkMatrix localMatrix = toSkMatrix(lm);
576 return SkGradientShader::MakeTwoPointConical(start, startRadius, end, endRadius,
577 colors, positions, count, mode,
578 flags, &localMatrix);
579 }), allow_raw_pointers());
580
Kevin Lubickb5ae3b52018-11-03 07:51:19 -0400581 function("_MakeSkVertices", optional_override([](SkVertices::VertexMode mode, int vertexCount,
582 uintptr_t /* SkPoint* */ pPtr, uintptr_t /* SkPoint* */ tPtr,
583 uintptr_t /* SkColor* */ cPtr,
584 uintptr_t /* BoneIndices* */ biPtr, uintptr_t /* BoneWeights* */ bwPtr,
585 int indexCount, uintptr_t /* uint16_t * */ iPtr)->sk_sp<SkVertices> {
586 // See comment above for uintptr_t explanation
587 const SkPoint* positions = reinterpret_cast<const SkPoint*>(pPtr);
588 const SkPoint* texs = reinterpret_cast<const SkPoint*>(tPtr);
589 const SkColor* colors = reinterpret_cast<const SkColor*>(cPtr);
590 const BoneIndices* boneIndices = reinterpret_cast<const BoneIndices*>(biPtr);
591 const BoneWeights* boneWeights = reinterpret_cast<const BoneWeights*>(bwPtr);
592 const uint16_t* indices = reinterpret_cast<const uint16_t*>(iPtr);
593
594 return SkVertices::MakeCopy(mode, vertexCount, positions, texs, colors,
595 boneIndices, boneWeights, indexCount, indices);
596 }), allow_raw_pointers());
Kevin Lubick217056c2018-09-20 17:39:31 -0400597
598 class_<SkCanvas>("SkCanvas")
599 .constructor<>()
600 .function("clear", optional_override([](SkCanvas& self, JSColor color)->void {
601 // JS side gives us a signed int instead of an unsigned int for color
Kevin Lubickb5ae3b52018-11-03 07:51:19 -0400602 // Add a optional_override to change it out.
Kevin Lubick217056c2018-09-20 17:39:31 -0400603 self.clear(SkColor(color));
604 }))
Kevin Lubickeb2f6b02018-11-29 15:07:02 -0500605 .function("clipPath", select_overload<void (const SkPath&, SkClipOp, bool)>(&SkCanvas::clipPath))
Kevin Lubick52b9f372018-12-04 13:57:36 -0500606 .function("clipRect", select_overload<void (const SkRect&, SkClipOp, bool)>(&SkCanvas::clipRect))
607 .function("concat", optional_override([](SkCanvas& self, const SimpleMatrix& m) {
608 self.concat(toSkMatrix(m));
609 }))
Kevin Lubick0a1293c2018-12-03 12:31:04 -0500610 .function("drawImage", select_overload<void (const sk_sp<SkImage>&, SkScalar, SkScalar, const SkPaint*)>(&SkCanvas::drawImage), allow_raw_pointers())
611 .function("drawImageRect", optional_override([](SkCanvas& self, const sk_sp<SkImage>& image,
612 SkRect src, SkRect dst,
613 const SkPaint* paint, bool fastSample)->void {
614 self.drawImageRect(image, src, dst, paint,
615 fastSample ? SkCanvas::kFast_SrcRectConstraint :
616 SkCanvas::kStrict_SrcRectConstraint);
617 }), allow_raw_pointers())
Kevin Lubick217056c2018-09-20 17:39:31 -0400618 .function("drawPaint", &SkCanvas::drawPaint)
619 .function("drawPath", &SkCanvas::drawPath)
620 .function("drawRect", &SkCanvas::drawRect)
Kevin Lubick61ef7b22018-11-27 13:26:59 -0500621 .function("drawShadow", optional_override([](SkCanvas& self, const SkPath& path,
622 const SkPoint3& zPlaneParams,
623 const SkPoint3& lightPos, SkScalar lightRadius,
624 JSColor ambientColor, JSColor spotColor,
625 uint32_t flags) {
626 SkShadowUtils::DrawShadow(&self, path, zPlaneParams, lightPos, lightRadius,
627 SkColor(ambientColor), SkColor(spotColor), flags);
628 }))
629 .function("drawText", optional_override([](SkCanvas& self, std::string text, SkScalar x,
630 SkScalar y, const SkPaint& p) {
Kevin Lubick3d99b1e2018-10-16 10:15:01 -0400631 // TODO(kjlubick): This does not work well for non-ascii
632 // Need to maybe add a helper in interface.js that supports UTF-8
633 // Otherwise, go with std::wstring and set UTF-32 encoding.
Kevin Lubickd45c7812018-10-02 11:33:52 -0400634 self.drawText(text.c_str(), text.length(), x, y, p);
Kevin Lubick217056c2018-09-20 17:39:31 -0400635 }))
Kevin Lubickb5ae3b52018-11-03 07:51:19 -0400636 .function("drawVertices", select_overload<void (const sk_sp<SkVertices>&, SkBlendMode, const SkPaint&)>(&SkCanvas::drawVertices))
Kevin Lubick217056c2018-09-20 17:39:31 -0400637 .function("flush", &SkCanvas::flush)
Kevin Lubick52b9f372018-12-04 13:57:36 -0500638 .function("_readPixels", optional_override([](SkCanvas& self, SimpleImageInfo di,
639 uintptr_t /* uint8_t* */ pPtr,
640 size_t dstRowBytes, int srcX, int srcY) {
641 uint8_t* pixels = reinterpret_cast<uint8_t*>(pPtr);
642 SkImageInfo dstInfo = toSkImageInfo(di);
643
644 return self.readPixels(dstInfo, pixels, dstRowBytes, srcX, srcY);
645 }))
Kevin Lubickeb2f6b02018-11-29 15:07:02 -0500646 .function("restore", &SkCanvas::restore)
Kevin Lubickb5ae3b52018-11-03 07:51:19 -0400647 .function("rotate", select_overload<void (SkScalar, SkScalar, SkScalar)>(&SkCanvas::rotate))
Kevin Lubick217056c2018-09-20 17:39:31 -0400648 .function("save", &SkCanvas::save)
Kevin Lubick006a6f32018-10-19 14:34:34 -0400649 .function("scale", &SkCanvas::scale)
Kevin Lubick006a6f32018-10-19 14:34:34 -0400650 .function("skew", &SkCanvas::skew)
Kevin Lubick52b9f372018-12-04 13:57:36 -0500651 .function("translate", &SkCanvas::translate)
652 .function("_writePixels", optional_override([](SkCanvas& self, SimpleImageInfo di,
653 uintptr_t /* uint8_t* */ pPtr,
654 size_t srcRowBytes, int dstX, int dstY) {
655 uint8_t* pixels = reinterpret_cast<uint8_t*>(pPtr);
656 SkImageInfo dstInfo = toSkImageInfo(di);
657
658 return self.writePixels(dstInfo, pixels, srcRowBytes, dstX, dstY);
659 }))
660 ;
Kevin Lubick217056c2018-09-20 17:39:31 -0400661
662 class_<SkData>("SkData")
663 .smart_ptr<sk_sp<SkData>>("sk_sp<SkData>>")
664 .function("size", &SkData::size);
665
666 class_<SkImage>("SkImage")
667 .smart_ptr<sk_sp<SkImage>>("sk_sp<SkImage>")
Kevin Lubick0a1293c2018-12-03 12:31:04 -0500668 .function("height", &SkImage::height)
669 .function("width", &SkImage::width)
Alexander Khovansky3e119332018-11-15 02:01:19 +0300670 .function("_encodeToData", select_overload<sk_sp<SkData>()const>(&SkImage::encodeToData))
671 .function("_encodeToDataWithFormat", select_overload<sk_sp<SkData>(SkEncodedImageFormat encodedImageFormat, int quality)const>(&SkImage::encodeToData));
Kevin Lubick217056c2018-09-20 17:39:31 -0400672
Kevin Lubick61ef7b22018-11-27 13:26:59 -0500673 class_<SkMaskFilter>("SkMaskFilter")
674 .smart_ptr<sk_sp<SkMaskFilter>>("sk_sp<SkMaskFilter>");
675
Kevin Lubick217056c2018-09-20 17:39:31 -0400676 class_<SkPaint>("SkPaint")
677 .constructor<>()
678 .function("copy", optional_override([](const SkPaint& self)->SkPaint {
679 SkPaint p(self);
680 return p;
681 }))
Kevin Lubick12c0e502018-11-28 12:51:56 -0500682 .function("getBlendMode", &SkPaint::getBlendMode)
Kevin Lubick61ef7b22018-11-27 13:26:59 -0500683 .function("getColor", optional_override([](SkPaint& self)->JSColor {
684 // JS side gives us a signed int instead of an unsigned int for color
685 // Add a optional_override to change it out.
686 return JSColor(self.getColor());
687 }))
Kevin Lubick0a1293c2018-12-03 12:31:04 -0500688 .function("getFilterQuality", &SkPaint::getFilterQuality)
Kevin Lubickb9db3902018-11-26 11:47:54 -0500689 .function("getStrokeCap", &SkPaint::getStrokeCap)
690 .function("getStrokeJoin", &SkPaint::getStrokeJoin)
Kevin Lubick0a1293c2018-12-03 12:31:04 -0500691 .function("getStrokeMiter", &SkPaint::getStrokeMiter)
692 .function("getStrokeWidth", &SkPaint::getStrokeWidth)
Kevin Lubickb9db3902018-11-26 11:47:54 -0500693 .function("getTextSize", &SkPaint::getTextSize)
Kevin Lubick006a6f32018-10-19 14:34:34 -0400694 .function("measureText", optional_override([](SkPaint& self, std::string text) {
695 // TODO(kjlubick): This does not work well for non-ascii
696 // Need to maybe add a helper in interface.js that supports UTF-8
697 // Otherwise, go with std::wstring and set UTF-32 encoding.
698 return self.measureText(text.c_str(), text.length());
699 }))
Kevin Lubick217056c2018-09-20 17:39:31 -0400700 .function("setAntiAlias", &SkPaint::setAntiAlias)
Kevin Lubick12c0e502018-11-28 12:51:56 -0500701 .function("setBlendMode", &SkPaint::setBlendMode)
Kevin Lubick217056c2018-09-20 17:39:31 -0400702 .function("setColor", optional_override([](SkPaint& self, JSColor color)->void {
703 // JS side gives us a signed int instead of an unsigned int for color
Kevin Lubickb5ae3b52018-11-03 07:51:19 -0400704 // Add a optional_override to change it out.
Kevin Lubick217056c2018-09-20 17:39:31 -0400705 self.setColor(SkColor(color));
706 }))
Kevin Lubick0a1293c2018-12-03 12:31:04 -0500707 .function("setFilterQuality", &SkPaint::setFilterQuality)
Kevin Lubick61ef7b22018-11-27 13:26:59 -0500708 .function("setMaskFilter", &SkPaint::setMaskFilter)
Kevin Lubick217056c2018-09-20 17:39:31 -0400709 .function("setPathEffect", &SkPaint::setPathEffect)
710 .function("setShader", &SkPaint::setShader)
Kevin Lubickb9db3902018-11-26 11:47:54 -0500711 .function("setStrokeCap", &SkPaint::setStrokeCap)
712 .function("setStrokeJoin", &SkPaint::setStrokeJoin)
Kevin Lubick0a1293c2018-12-03 12:31:04 -0500713 .function("setStrokeMiter", &SkPaint::setStrokeMiter)
714 .function("setStrokeWidth", &SkPaint::setStrokeWidth)
Kevin Lubick217056c2018-09-20 17:39:31 -0400715 .function("setStyle", &SkPaint::setStyle)
716 .function("setTextSize", &SkPaint::setTextSize);
717
718 class_<SkPathEffect>("SkPathEffect")
719 .smart_ptr<sk_sp<SkPathEffect>>("sk_sp<SkPathEffect>");
720
Kevin Lubick217056c2018-09-20 17:39:31 -0400721 class_<SkPath>("SkPath")
722 .constructor<>()
723 .constructor<const SkPath&>()
Kevin Lubick1a05fce2018-11-20 12:51:16 -0500724 .function("_addArc", &ApplyAddArc)
Kevin Lubick217056c2018-09-20 17:39:31 -0400725 // interface.js has 3 overloads of addPath
726 .function("_addPath", &ApplyAddPath)
Kevin Lubick1a05fce2018-11-20 12:51:16 -0500727 // interface.js has 4 overloads of addRect
728 .function("_addRect", &ApplyAddRect)
Kevin Lubick217056c2018-09-20 17:39:31 -0400729 .function("_arcTo", &ApplyArcTo)
730 .function("_close", &ApplyClose)
731 .function("_conicTo", &ApplyConicTo)
Kevin Lubickb9db3902018-11-26 11:47:54 -0500732 .function("countPoints", &SkPath::countPoints)
Kevin Lubick217056c2018-09-20 17:39:31 -0400733 .function("_cubicTo", &ApplyCubicTo)
Kevin Lubickb9db3902018-11-26 11:47:54 -0500734 .function("getPoint", &SkPath::getPoint)
Kevin Lubick217056c2018-09-20 17:39:31 -0400735 .function("_lineTo", &ApplyLineTo)
736 .function("_moveTo", &ApplyMoveTo)
737 .function("_quadTo", &ApplyQuadTo)
Kevin Lubick1a05fce2018-11-20 12:51:16 -0500738 .function("_transform", select_overload<void(SkPath&, SkScalar, SkScalar, SkScalar, SkScalar, SkScalar, SkScalar, SkScalar, SkScalar, SkScalar)>(&ApplyTransform))
Kevin Lubick217056c2018-09-20 17:39:31 -0400739
Kevin Lubickb5ae3b52018-11-03 07:51:19 -0400740 // PathEffects
741 .function("_dash", &ApplyDash)
742 .function("_trim", &ApplyTrim)
743 .function("_stroke", &ApplyStroke)
744
745 // PathOps
746 .function("_simplify", &ApplySimplify)
747 .function("_op", &ApplyPathOp)
748
749 // Exporting
750 .function("toSVGString", &ToSVGString)
751
Kevin Lubick217056c2018-09-20 17:39:31 -0400752 .function("setFillType", &SkPath::setFillType)
753 .function("getFillType", &SkPath::getFillType)
754 .function("getBounds", &SkPath::getBounds)
755 .function("computeTightBounds", &SkPath::computeTightBounds)
756 .function("equals", &Equals)
Kevin Lubickb9db3902018-11-26 11:47:54 -0500757 .function("copy", &CopyPath)
758#ifdef SK_DEBUG
759 .function("dump", select_overload<void() const>(&SkPath::dump))
760 .function("dumpHex", select_overload<void() const>(&SkPath::dumpHex))
761#endif
762 ;
Kevin Lubick217056c2018-09-20 17:39:31 -0400763
Kevin Lubickb5ae3b52018-11-03 07:51:19 -0400764 class_<SkShader>("SkShader")
765 .smart_ptr<sk_sp<SkShader>>("sk_sp<SkShader>");
766
Kevin Lubick217056c2018-09-20 17:39:31 -0400767 class_<SkSurface>("SkSurface")
768 .smart_ptr<sk_sp<SkSurface>>("sk_sp<SkSurface>")
769 .function("width", &SkSurface::width)
770 .function("height", &SkSurface::height)
Kevin Lubick3d99b1e2018-10-16 10:15:01 -0400771 .function("_flush", &SkSurface::flush)
Mike Reed114bde82018-11-21 09:12:09 -0500772 .function("makeImageSnapshot", select_overload<sk_sp<SkImage>()>(&SkSurface::makeImageSnapshot))
773 .function("makeImageSnapshot", select_overload<sk_sp<SkImage>(const SkIRect& bounds)>(&SkSurface::makeImageSnapshot))
Kevin Lubick217056c2018-09-20 17:39:31 -0400774 .function("getCanvas", &SkSurface::getCanvas, allow_raw_pointers());
775
Kevin Lubickb5ae3b52018-11-03 07:51:19 -0400776 class_<SkVertices>("SkVertices")
777 .smart_ptr<sk_sp<SkVertices>>("sk_sp<SkVertices>")
778 .function("_applyBones", optional_override([](SkVertices& self, uintptr_t /* Bone* */ bptr, int boneCount)->sk_sp<SkVertices> {
779 // See comment above for uintptr_t explanation
780 const Bone* bones = reinterpret_cast<const Bone*>(bptr);
781 return self.applyBones(bones, boneCount);
782 }))
783 .function("bounds", &SkVertices::bounds)
784 .function("mode", &SkVertices::mode)
785 .function("uniqueID", &SkVertices::uniqueID)
Kevin Lubick12c0e502018-11-28 12:51:56 -0500786#ifdef SK_DEBUG
Kevin Lubickb5ae3b52018-11-03 07:51:19 -0400787 .function("dumpPositions", optional_override([](SkVertices& self)->void {
788 auto pos = self.positions();
789 for(int i = 0; i< self.vertexCount(); i++) {
790 SkDebugf("position[%d] = (%f, %f)\n", i, pos[i].x(), pos[i].y());
791 }
792 }))
Kevin Lubick12c0e502018-11-28 12:51:56 -0500793#endif
Kevin Lubickb5ae3b52018-11-03 07:51:19 -0400794 .function("vertexCount", &SkVertices::vertexCount);
795
Kevin Lubickea905ec2018-11-30 14:05:58 -0500796 enum_<SkAlphaType>("AlphaType")
797 .value("Opaque", SkAlphaType::kOpaque_SkAlphaType)
798 .value("Premul", SkAlphaType::kPremul_SkAlphaType)
799 .value("Unpremul", SkAlphaType::kUnpremul_SkAlphaType);
Kevin Lubickb5ae3b52018-11-03 07:51:19 -0400800
801 enum_<SkBlendMode>("BlendMode")
802 .value("Clear", SkBlendMode::kClear)
803 .value("Src", SkBlendMode::kSrc)
804 .value("Dst", SkBlendMode::kDst)
805 .value("SrcOver", SkBlendMode::kSrcOver)
806 .value("DstOver", SkBlendMode::kDstOver)
807 .value("SrcIn", SkBlendMode::kSrcIn)
808 .value("DstIn", SkBlendMode::kDstIn)
809 .value("SrcOut", SkBlendMode::kSrcOut)
810 .value("DstOut", SkBlendMode::kDstOut)
811 .value("SrcATop", SkBlendMode::kSrcATop)
812 .value("DstATop", SkBlendMode::kDstATop)
813 .value("Xor", SkBlendMode::kXor)
814 .value("Plus", SkBlendMode::kPlus)
815 .value("Modulate", SkBlendMode::kModulate)
816 .value("Screen", SkBlendMode::kScreen)
817 .value("Overlay", SkBlendMode::kOverlay)
818 .value("Darken", SkBlendMode::kDarken)
819 .value("Lighten", SkBlendMode::kLighten)
820 .value("ColorDodge", SkBlendMode::kColorDodge)
821 .value("ColorBurn", SkBlendMode::kColorBurn)
822 .value("HardLight", SkBlendMode::kHardLight)
823 .value("SoftLight", SkBlendMode::kSoftLight)
824 .value("Difference", SkBlendMode::kDifference)
825 .value("Exclusion", SkBlendMode::kExclusion)
826 .value("Multiply", SkBlendMode::kMultiply)
827 .value("Hue", SkBlendMode::kHue)
828 .value("Saturation", SkBlendMode::kSaturation)
829 .value("Color", SkBlendMode::kColor)
830 .value("Luminosity", SkBlendMode::kLuminosity);
Kevin Lubick217056c2018-09-20 17:39:31 -0400831
Kevin Lubick61ef7b22018-11-27 13:26:59 -0500832 enum_<SkBlurStyle>("BlurStyle")
833 .value("Normal", SkBlurStyle::kNormal_SkBlurStyle)
834 .value("Solid", SkBlurStyle::kSolid_SkBlurStyle)
835 .value("Outer", SkBlurStyle::kOuter_SkBlurStyle)
836 .value("Inner", SkBlurStyle::kInner_SkBlurStyle);
Kevin Lubick217056c2018-09-20 17:39:31 -0400837
Kevin Lubickeb2f6b02018-11-29 15:07:02 -0500838 enum_<SkClipOp>("ClipOp")
839 .value("Difference", SkClipOp::kDifference)
840 .value("Intersect", SkClipOp::kIntersect);
841
Kevin Lubickea905ec2018-11-30 14:05:58 -0500842 enum_<SkColorType>("ColorType")
843 .value("Alpha_8", SkColorType::kAlpha_8_SkColorType)
844 .value("RGB_565", SkColorType::kRGB_565_SkColorType)
845 .value("ARGB_4444", SkColorType::kARGB_4444_SkColorType)
846 .value("RGBA_8888", SkColorType::kRGBA_8888_SkColorType)
847 .value("RGB_888x", SkColorType::kRGB_888x_SkColorType)
848 .value("BGRA_8888", SkColorType::kBGRA_8888_SkColorType)
849 .value("RGBA_1010102", SkColorType::kRGBA_1010102_SkColorType)
850 .value("RGB_101010x", SkColorType::kRGB_101010x_SkColorType)
851 .value("Gray_8", SkColorType::kGray_8_SkColorType)
852 .value("RGBA_F16", SkColorType::kRGBA_F16_SkColorType)
853 .value("RGBA_F32", SkColorType::kRGBA_F32_SkColorType);
854
Kevin Lubick217056c2018-09-20 17:39:31 -0400855 enum_<SkPath::FillType>("FillType")
Kevin Lubickb5ae3b52018-11-03 07:51:19 -0400856 .value("Winding", SkPath::FillType::kWinding_FillType)
857 .value("EvenOdd", SkPath::FillType::kEvenOdd_FillType)
858 .value("InverseWinding", SkPath::FillType::kInverseWinding_FillType)
859 .value("InverseEvenOdd", SkPath::FillType::kInverseEvenOdd_FillType);
860
Kevin Lubick0a1293c2018-12-03 12:31:04 -0500861 enum_<SkFilterQuality>("FilterQuality")
862 .value("None", SkFilterQuality::kNone_SkFilterQuality)
863 .value("Low", SkFilterQuality::kLow_SkFilterQuality)
864 .value("Medium", SkFilterQuality::kMedium_SkFilterQuality)
865 .value("High", SkFilterQuality::kHigh_SkFilterQuality);
866
Kevin Lubick61ef7b22018-11-27 13:26:59 -0500867 enum_<SkEncodedImageFormat>("ImageFormat")
868 .value("PNG", SkEncodedImageFormat::kPNG)
869 .value("JPEG", SkEncodedImageFormat::kJPEG);
870
871 enum_<SkPaint::Style>("PaintStyle")
872 .value("Fill", SkPaint::Style::kFill_Style)
873 .value("Stroke", SkPaint::Style::kStroke_Style)
874 .value("StrokeAndFill", SkPaint::Style::kStrokeAndFill_Style);
875
Kevin Lubickb5ae3b52018-11-03 07:51:19 -0400876 enum_<SkPathOp>("PathOp")
877 .value("Difference", SkPathOp::kDifference_SkPathOp)
878 .value("Intersect", SkPathOp::kIntersect_SkPathOp)
879 .value("Union", SkPathOp::kUnion_SkPathOp)
880 .value("XOR", SkPathOp::kXOR_SkPathOp)
881 .value("ReverseDifference", SkPathOp::kReverseDifference_SkPathOp);
882
Kevin Lubickb9db3902018-11-26 11:47:54 -0500883 enum_<SkPaint::Cap>("StrokeCap")
884 .value("Butt", SkPaint::Cap::kButt_Cap)
885 .value("Round", SkPaint::Cap::kRound_Cap)
886 .value("Square", SkPaint::Cap::kSquare_Cap);
887
888 enum_<SkPaint::Join>("StrokeJoin")
889 .value("Miter", SkPaint::Join::kMiter_Join)
890 .value("Round", SkPaint::Join::kRound_Join)
891 .value("Bevel", SkPaint::Join::kBevel_Join);
892
893 value_object<StrokeOpts>("StrokeOpts")
894 .field("width", &StrokeOpts::width)
895 .field("miter_limit", &StrokeOpts::miter_limit)
896 .field("join", &StrokeOpts::join)
897 .field("cap", &StrokeOpts::cap);
898
Kevin Lubickb5ae3b52018-11-03 07:51:19 -0400899 enum_<SkShader::TileMode>("TileMode")
900 .value("Clamp", SkShader::TileMode::kClamp_TileMode)
901 .value("Repeat", SkShader::TileMode::kRepeat_TileMode)
Kevin Lubickd29edd72018-12-07 08:29:52 -0500902 .value("Mirror", SkShader::TileMode::kMirror_TileMode)
903 // Decal mode only works in the SW backend, not WebGl (yet).
904 .value("Decal", SkShader::TileMode::kDecal_TileMode);
Kevin Lubickb5ae3b52018-11-03 07:51:19 -0400905
906 enum_<SkVertices::VertexMode>("VertexMode")
907 .value("Triangles", SkVertices::VertexMode::kTriangles_VertexMode)
908 .value("TrianglesStrip", SkVertices::VertexMode::kTriangleStrip_VertexMode)
Kevin Lubickb9db3902018-11-26 11:47:54 -0500909 .value("TriangleFan", SkVertices::VertexMode::kTriangleFan_VertexMode);
Kevin Lubickb5ae3b52018-11-03 07:51:19 -0400910
Kevin Lubick217056c2018-09-20 17:39:31 -0400911
912 // A value object is much simpler than a class - it is returned as a JS
913 // object and does not require delete().
914 // https://kripken.github.io/emscripten-site/docs/porting/connecting_cpp_and_javascript/embind.html#value-types
915 value_object<SkRect>("SkRect")
916 .field("fLeft", &SkRect::fLeft)
917 .field("fTop", &SkRect::fTop)
918 .field("fRight", &SkRect::fRight)
919 .field("fBottom", &SkRect::fBottom);
920
Mike Reed114bde82018-11-21 09:12:09 -0500921 value_object<SkIRect>("SkIRect")
922 .field("fLeft", &SkIRect::fLeft)
923 .field("fTop", &SkIRect::fTop)
924 .field("fRight", &SkIRect::fRight)
925 .field("fBottom", &SkIRect::fBottom);
926
Kevin Lubickea905ec2018-11-30 14:05:58 -0500927 value_object<SimpleImageInfo>("SkImageInfo")
928 .field("width", &SimpleImageInfo::width)
929 .field("height", &SimpleImageInfo::height)
930 .field("colorType", &SimpleImageInfo::colorType)
931 .field("alphaType", &SimpleImageInfo::alphaType);
932
Kevin Lubick217056c2018-09-20 17:39:31 -0400933 // SkPoints can be represented by [x, y]
934 value_array<SkPoint>("SkPoint")
935 .element(&SkPoint::fX)
936 .element(&SkPoint::fY);
937
Kevin Lubick61ef7b22018-11-27 13:26:59 -0500938 // SkPoint3s can be represented by [x, y, z]
939 value_array<SkPoint3>("SkPoint3")
940 .element(&SkPoint3::fX)
941 .element(&SkPoint3::fY)
942 .element(&SkPoint3::fZ);
943
Kevin Lubick217056c2018-09-20 17:39:31 -0400944 // {"w": Number, "h", Number}
945 value_object<SkSize>("SkSize")
946 .field("w", &SkSize::fWidth)
947 .field("h", &SkSize::fHeight);
948
949 value_object<SkISize>("SkISize")
950 .field("w", &SkISize::fWidth)
951 .field("h", &SkISize::fHeight);
952
Kevin Lubickb5ae3b52018-11-03 07:51:19 -0400953 // Allows clients to supply a 1D array of 9 elements and the bindings
954 // will automatically turn it into a 3x3 2D matrix.
955 // e.g. path.transform([0,1,2,3,4,5,6,7,8])
956 // This is likely simpler for the client than exposing SkMatrix
957 // directly and requiring them to do a lot of .delete().
958 value_array<SimpleMatrix>("SkMatrix")
959 .element(&SimpleMatrix::scaleX)
960 .element(&SimpleMatrix::skewX)
961 .element(&SimpleMatrix::transX)
962
963 .element(&SimpleMatrix::skewY)
964 .element(&SimpleMatrix::scaleY)
965 .element(&SimpleMatrix::transY)
966
967 .element(&SimpleMatrix::pers0)
968 .element(&SimpleMatrix::pers1)
969 .element(&SimpleMatrix::pers2);
970
971 constant("TRANSPARENT", (JSColor) SK_ColorTRANSPARENT);
972 constant("RED", (JSColor) SK_ColorRED);
973 constant("BLUE", (JSColor) SK_ColorBLUE);
974 constant("YELLOW", (JSColor) SK_ColorYELLOW);
975 constant("CYAN", (JSColor) SK_ColorCYAN);
Kevin Lubick61ef7b22018-11-27 13:26:59 -0500976 constant("BLACK", (JSColor) SK_ColorBLACK);
Kevin Lubickea905ec2018-11-30 14:05:58 -0500977 constant("WHITE", (JSColor) SK_ColorWHITE);
Kevin Lubickb5ae3b52018-11-03 07:51:19 -0400978 // TODO(?)
979
Kevin Lubick53965c92018-10-11 08:51:55 -0400980#if SK_INCLUDE_SKOTTIE
Kevin Lubick217056c2018-09-20 17:39:31 -0400981 // Animation things (may eventually go in own library)
982 class_<skottie::Animation>("Animation")
983 .smart_ptr<sk_sp<skottie::Animation>>("sk_sp<Animation>")
984 .function("version", optional_override([](skottie::Animation& self)->std::string {
985 return std::string(self.version().c_str());
986 }))
987 .function("size", &skottie::Animation::size)
988 .function("duration", &skottie::Animation::duration)
989 .function("seek", &skottie::Animation::seek)
990 .function("render", optional_override([](skottie::Animation& self, SkCanvas* canvas)->void {
991 self.render(canvas, nullptr);
992 }), allow_raw_pointers())
Florin Malitac2e85f92018-11-29 11:38:12 -0500993 .function("render", optional_override([](skottie::Animation& self, SkCanvas* canvas,
994 const SkRect r)->void {
Kevin Lubick217056c2018-09-20 17:39:31 -0400995 self.render(canvas, &r);
996 }), allow_raw_pointers());
997
Kevin Lubickb5ae3b52018-11-03 07:51:19 -0400998 function("MakeAnimation", optional_override([](std::string json)->sk_sp<skottie::Animation> {
999 return skottie::Animation::Make(json.c_str(), json.length());
1000 }));
Kevin Lubick3d99b1e2018-10-16 10:15:01 -04001001 constant("skottie", true);
Florin Malitac2e85f92018-11-29 11:38:12 -05001002
1003#if SK_INCLUDE_MANAGED_SKOTTIE
1004 class_<ManagedAnimation>("ManagedAnimation")
1005 .smart_ptr<sk_sp<ManagedAnimation>>("sk_sp<ManagedAnimation>")
1006 .function("version" , &ManagedAnimation::version)
1007 .function("size" , &ManagedAnimation::size)
1008 .function("duration" , &ManagedAnimation::duration)
1009 .function("seek" , &ManagedAnimation::seek)
1010 .function("render" , select_overload<void(SkCanvas*) const>
1011 (&ManagedAnimation::render), allow_raw_pointers())
1012 .function("render" , select_overload<void(SkCanvas*, const SkRect&) const>
1013 (&ManagedAnimation::render), allow_raw_pointers())
1014 .function("setColor" , &ManagedAnimation::setColor)
1015 .function("setOpacity", &ManagedAnimation::setOpacity)
Florin Malita6ba8c7e2018-12-03 09:55:52 -05001016 .function("getMarkers", &ManagedAnimation::getMarkers)
Florin Malitac2e85f92018-11-29 11:38:12 -05001017 .function("getColorProps" , &ManagedAnimation::getColorProps)
1018 .function("getOpacityProps", &ManagedAnimation::getOpacityProps);
1019
1020 function("MakeManagedAnimation", &ManagedAnimation::Make);
1021 constant("managed_skottie", true);
1022#endif // SK_INCLUDE_MANAGED_SKOTTIE
1023#endif // SK_INCLUDE_SKOTTIE
Kevin Lubick134be1d2018-10-30 15:05:04 -04001024
Kevin Lubick217056c2018-09-20 17:39:31 -04001025}