blob: 9798df67de5466aa08a5a7731004d90ec6cb7ab2 [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,
Kevin Lubick1646e7d2018-12-07 13:03:08 -0500278 ccw ? SkPath::Direction::kCCW_Direction :
279 SkPath::Direction::kCW_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
Kevin Lubick1646e7d2018-12-07 13:03:08 -0500287void ApplyArcToAngle(SkPath& p, SkRect& oval, SkScalar startAngle, SkScalar sweepAngle, bool forceMoveTo) {
288 p.arcTo(oval, startAngle, sweepAngle, forceMoveTo);
289}
290
Kevin Lubick217056c2018-09-20 17:39:31 -0400291void ApplyClose(SkPath& p) {
292 p.close();
293}
294
295void ApplyConicTo(SkPath& p, SkScalar x1, SkScalar y1, SkScalar x2, SkScalar y2,
296 SkScalar w) {
297 p.conicTo(x1, y1, x2, y2, w);
298}
299
300void ApplyCubicTo(SkPath& p, SkScalar x1, SkScalar y1, SkScalar x2, SkScalar y2,
301 SkScalar x3, SkScalar y3) {
302 p.cubicTo(x1, y1, x2, y2, x3, y3);
303}
304
305void ApplyLineTo(SkPath& p, SkScalar x, SkScalar y) {
306 p.lineTo(x, y);
307}
308
309void ApplyMoveTo(SkPath& p, SkScalar x, SkScalar y) {
310 p.moveTo(x, y);
311}
312
313void ApplyQuadTo(SkPath& p, SkScalar x1, SkScalar y1, SkScalar x2, SkScalar y2) {
314 p.quadTo(x1, y1, x2, y2);
315}
316
317void ApplyTransform(SkPath& orig,
318 SkScalar scaleX, SkScalar skewX, SkScalar transX,
319 SkScalar skewY, SkScalar scaleY, SkScalar transY,
320 SkScalar pers0, SkScalar pers1, SkScalar pers2) {
321 SkMatrix m = SkMatrix::MakeAll(scaleX, skewX , transX,
322 skewY , scaleY, transY,
323 pers0 , pers1 , pers2);
324 orig.transform(m);
325}
326
Kevin Lubickb5ae3b52018-11-03 07:51:19 -0400327bool EMSCRIPTEN_KEEPALIVE ApplySimplify(SkPath& path) {
328 return Simplify(path, &path);
329}
330
331bool EMSCRIPTEN_KEEPALIVE ApplyPathOp(SkPath& pathOne, const SkPath& pathTwo, SkPathOp op) {
332 return Op(pathOne, pathTwo, op, &pathOne);
333}
334
335JSString EMSCRIPTEN_KEEPALIVE ToSVGString(const SkPath& path) {
336 SkString s;
337 SkParsePath::ToSVGString(path, &s);
338 return emscripten::val(s.c_str());
339}
340
341SkPathOrNull EMSCRIPTEN_KEEPALIVE MakePathFromOp(const SkPath& pathOne, const SkPath& pathTwo, SkPathOp op) {
342 SkPath out;
343 if (Op(pathOne, pathTwo, op, &out)) {
344 return emscripten::val(out);
345 }
346 return emscripten::val::null();
347}
348
Kevin Lubick217056c2018-09-20 17:39:31 -0400349SkPath EMSCRIPTEN_KEEPALIVE CopyPath(const SkPath& a) {
350 SkPath copy(a);
351 return copy;
352}
353
354bool EMSCRIPTEN_KEEPALIVE Equals(const SkPath& a, const SkPath& b) {
355 return a == b;
356}
357
Kevin Lubickb5ae3b52018-11-03 07:51:19 -0400358//========================================================================================
359// Path Effects
360//========================================================================================
361
362bool ApplyDash(SkPath& path, SkScalar on, SkScalar off, SkScalar phase) {
363 SkScalar intervals[] = { on, off };
364 auto pe = SkDashPathEffect::Make(intervals, 2, phase);
365 if (!pe) {
366 SkDebugf("Invalid args to dash()\n");
367 return false;
368 }
369 SkStrokeRec rec(SkStrokeRec::InitStyle::kHairline_InitStyle);
370 if (pe->filterPath(&path, path, &rec, nullptr)) {
371 return true;
372 }
373 SkDebugf("Could not make dashed path\n");
374 return false;
Kevin Lubick217056c2018-09-20 17:39:31 -0400375}
376
Kevin Lubickb5ae3b52018-11-03 07:51:19 -0400377bool ApplyTrim(SkPath& path, SkScalar startT, SkScalar stopT, bool isComplement) {
378 auto mode = isComplement ? SkTrimPathEffect::Mode::kInverted : SkTrimPathEffect::Mode::kNormal;
379 auto pe = SkTrimPathEffect::Make(startT, stopT, mode);
380 if (!pe) {
381 SkDebugf("Invalid args to trim(): startT and stopT must be in [0,1]\n");
382 return false;
383 }
384 SkStrokeRec rec(SkStrokeRec::InitStyle::kHairline_InitStyle);
385 if (pe->filterPath(&path, path, &rec, nullptr)) {
386 return true;
387 }
388 SkDebugf("Could not trim path\n");
389 return false;
390}
391
392struct StrokeOpts {
Kevin Lubickb9db3902018-11-26 11:47:54 -0500393 // Default values are set in interface.js which allows clients
Kevin Lubickb5ae3b52018-11-03 07:51:19 -0400394 // to set any number of them. Otherwise, the binding code complains if
395 // any are omitted.
396 SkScalar width;
397 SkScalar miter_limit;
398 SkPaint::Join join;
399 SkPaint::Cap cap;
Kevin Lubick1646e7d2018-12-07 13:03:08 -0500400 float precision;
Kevin Lubickb5ae3b52018-11-03 07:51:19 -0400401};
402
403bool ApplyStroke(SkPath& path, StrokeOpts opts) {
404 SkPaint p;
405 p.setStyle(SkPaint::kStroke_Style);
406 p.setStrokeCap(opts.cap);
407 p.setStrokeJoin(opts.join);
408 p.setStrokeWidth(opts.width);
409 p.setStrokeMiter(opts.miter_limit);
410
Kevin Lubick1646e7d2018-12-07 13:03:08 -0500411 return p.getFillPath(path, &path, nullptr, opts.precision);
Kevin Lubickb5ae3b52018-11-03 07:51:19 -0400412}
413
414// to map from raw memory to a uint8array
415Uint8Array getSkDataBytes(const SkData *data) {
416 return Uint8Array(typed_memory_view(data->size(), data->bytes()));
417}
418
419// These objects have private destructors / delete mthods - I don't think
420// we need to do anything other than tell emscripten to do nothing.
Kevin Lubick217056c2018-09-20 17:39:31 -0400421namespace emscripten {
422 namespace internal {
423 template<typename ClassType>
424 void raw_destructor(ClassType *);
425
426 template<>
427 void raw_destructor<SkData>(SkData *ptr) {
428 }
Kevin Lubickb5ae3b52018-11-03 07:51:19 -0400429
430 template<>
431 void raw_destructor<SkVertices>(SkVertices *ptr) {
432 }
Kevin Lubick217056c2018-09-20 17:39:31 -0400433 }
434}
435
436// Some timesignatures below have uintptr_t instead of a pointer to a primative
437// type (e.g. SkScalar). This is necessary because we can't use "bind" (EMSCRIPTEN_BINDINGS)
438// and pointers to primitive types (Only bound types like SkPoint). We could if we used
439// cwrap (see https://becominghuman.ai/passing-and-returning-webassembly-array-parameters-a0f572c65d97)
440// but that requires us to stick to C code and, AFAIK, doesn't allow us to return nice things like
441// SkPath or SkCanvas.
442//
443// So, basically, if we are using C++ and EMSCRIPTEN_BINDINGS, we can't have primative pointers
444// in our function type signatures. (this gives an error message like "Cannot call foo due to unbound
445// types Pi, Pf"). But, we can just pretend they are numbers and cast them to be pointers and
446// the compiler is happy.
447EMSCRIPTEN_BINDINGS(Skia) {
448 function("initFonts", &initFonts);
Kevin Lubick53965c92018-10-11 08:51:55 -0400449#if SK_SUPPORT_GPU
Kevin Lubick217056c2018-09-20 17:39:31 -0400450 function("_getWebGLSurface", &getWebGLSurface, allow_raw_pointers());
Kevin Lubick53965c92018-10-11 08:51:55 -0400451 function("currentContext", &emscripten_webgl_get_current_context);
452 function("setCurrentContext", &emscripten_webgl_make_context_current);
Kevin Lubick3d99b1e2018-10-16 10:15:01 -0400453 constant("gpu", true);
Kevin Lubickb07204a2018-11-20 14:07:42 -0500454#endif
Kevin Lubick0a1293c2018-12-03 12:31:04 -0500455 function("_decodeImage", optional_override([](uintptr_t /* uint8_t* */ iptr,
456 size_t length)->sk_sp<SkImage> {
457 uint8_t* imgData = reinterpret_cast<uint8_t*>(iptr);
458 sk_sp<SkData> bytes = SkData::MakeWithoutCopy(imgData, length);
459 return SkImage::MakeFromEncoded(bytes);
460 }), allow_raw_pointers());
Kevin Lubickea905ec2018-11-30 14:05:58 -0500461 function("_getRasterDirectSurface", optional_override([](const SimpleImageInfo ii,
Kevin Lubick52b9f372018-12-04 13:57:36 -0500462 uintptr_t /* uint8_t* */ pPtr,
Kevin Lubickea905ec2018-11-30 14:05:58 -0500463 size_t rowBytes)->sk_sp<SkSurface> {
Kevin Lubick52b9f372018-12-04 13:57:36 -0500464 uint8_t* pixels = reinterpret_cast<uint8_t*>(pPtr);
Kevin Lubickea905ec2018-11-30 14:05:58 -0500465 SkImageInfo imageInfo = toSkImageInfo(ii);
466 return SkSurface::MakeRasterDirect(imageInfo, pixels, rowBytes, nullptr);
467 }), allow_raw_pointers());
Kevin Lubick53965c92018-10-11 08:51:55 -0400468 function("_getRasterN32PremulSurface", optional_override([](int width, int height)->sk_sp<SkSurface> {
469 return SkSurface::MakeRasterN32Premul(width, height, nullptr);
470 }), allow_raw_pointers());
Kevin Lubickb07204a2018-11-20 14:07:42 -0500471
Kevin Lubickb5ae3b52018-11-03 07:51:19 -0400472 function("getSkDataBytes", &getSkDataBytes, allow_raw_pointers());
Kevin Lubick217056c2018-09-20 17:39:31 -0400473 function("MakeSkCornerPathEffect", &SkCornerPathEffect::Make, allow_raw_pointers());
474 function("MakeSkDiscretePathEffect", &SkDiscretePathEffect::Make, allow_raw_pointers());
Kevin Lubick61ef7b22018-11-27 13:26:59 -0500475 function("MakeBlurMaskFilter", optional_override([](SkBlurStyle style, SkScalar sigma, bool respectCTM)->sk_sp<SkMaskFilter> {
476 // Adds a little helper because emscripten doesn't expose default params.
477 return SkMaskFilter::MakeBlur(style, sigma, respectCTM);
478 }), allow_raw_pointers());
Kevin Lubickb5ae3b52018-11-03 07:51:19 -0400479 function("MakePathFromOp", &MakePathFromOp);
480
481 // These won't be called directly, there's a JS helper to deal with typed arrays.
Kevin Lubick217056c2018-09-20 17:39:31 -0400482 function("_MakeSkDashPathEffect", optional_override([](uintptr_t /* float* */ cptr, int count, SkScalar phase)->sk_sp<SkPathEffect> {
483 // See comment above for uintptr_t explanation
484 const float* intervals = reinterpret_cast<const float*>(cptr);
485 return SkDashPathEffect::Make(intervals, count, phase);
486 }), allow_raw_pointers());
Kevin Lubick52b9f372018-12-04 13:57:36 -0500487 function("_MakeImage", optional_override([](SimpleImageInfo ii,
488 uintptr_t /* uint8_t* */ pPtr, int plen,
489 size_t rowBytes)->sk_sp<SkImage> {
490 // See comment above for uintptr_t explanation
491 uint8_t* pixels = reinterpret_cast<uint8_t*>(pPtr);
492 SkImageInfo info = toSkImageInfo(ii);
493 sk_sp<SkData> pixelData = SkData::MakeFromMalloc(pixels, plen);
494
495 return SkImage::MakeRasterData(info, pixelData, rowBytes);
496 }), allow_raw_pointers());
Kevin Lubickd29edd72018-12-07 08:29:52 -0500497 // Allow localMatrix to be optional, so we have 2 declarations of these shaders
498 function("_MakeImageShader", optional_override([](sk_sp<SkImage> img,
499 SkShader::TileMode tx, SkShader::TileMode ty,
500 bool clampAsIfUnpremul)->sk_sp<SkShader> {
501 return SkImageShader::Make(img, tx, ty, nullptr, clampAsIfUnpremul);
Kevin Lubickb5ae3b52018-11-03 07:51:19 -0400502 }), allow_raw_pointers());
Kevin Lubickd29edd72018-12-07 08:29:52 -0500503 function("_MakeImageShader", optional_override([](sk_sp<SkImage> img,
504 SkShader::TileMode tx, SkShader::TileMode ty,
505 bool clampAsIfUnpremul, const SimpleMatrix& lm)->sk_sp<SkShader> {
506 SkMatrix localMatrix = toSkMatrix(lm);
507
508 return SkImageShader::Make(img, tx, ty, &localMatrix, clampAsIfUnpremul);
509 }), allow_raw_pointers());
Kevin Lubickb5ae3b52018-11-03 07:51:19 -0400510 function("_MakeLinearGradientShader", optional_override([](SkPoint start, SkPoint end,
511 uintptr_t /* SkColor* */ cPtr, uintptr_t /* SkScalar* */ pPtr,
512 int count, SkShader::TileMode mode, uint32_t flags)->sk_sp<SkShader> {
513 SkPoint points[] = { start, end };
514 // See comment above for uintptr_t explanation
515 const SkColor* colors = reinterpret_cast<const SkColor*> (cPtr);
516 const SkScalar* positions = reinterpret_cast<const SkScalar*>(pPtr);
517
518 return SkGradientShader::MakeLinear(points, colors, positions, count,
519 mode, flags, nullptr);
520 }), allow_raw_pointers());
521 function("_MakeLinearGradientShader", optional_override([](SkPoint start, SkPoint end,
522 uintptr_t /* SkColor* */ cPtr, uintptr_t /* SkScalar* */ pPtr,
523 int count, SkShader::TileMode mode, uint32_t flags,
524 const SimpleMatrix& lm)->sk_sp<SkShader> {
525 SkPoint points[] = { start, end };
526 // See comment above for uintptr_t explanation
527 const SkColor* colors = reinterpret_cast<const SkColor*> (cPtr);
528 const SkScalar* positions = reinterpret_cast<const SkScalar*>(pPtr);
529
530 SkMatrix localMatrix = toSkMatrix(lm);
531
532 return SkGradientShader::MakeLinear(points, colors, positions, count,
533 mode, flags, &localMatrix);
534 }), allow_raw_pointers());
535 function("_MakeRadialGradientShader", optional_override([](SkPoint center, SkScalar radius,
536 uintptr_t /* SkColor* */ cPtr, uintptr_t /* SkScalar* */ pPtr,
537 int count, SkShader::TileMode mode, uint32_t flags)->sk_sp<SkShader> {
538 // See comment above for uintptr_t explanation
539 const SkColor* colors = reinterpret_cast<const SkColor*> (cPtr);
540 const SkScalar* positions = reinterpret_cast<const SkScalar*>(pPtr);
541
542 return SkGradientShader::MakeRadial(center, radius, colors, positions, count,
543 mode, flags, nullptr);
544 }), allow_raw_pointers());
545 function("_MakeRadialGradientShader", optional_override([](SkPoint center, SkScalar radius,
546 uintptr_t /* SkColor* */ cPtr, uintptr_t /* SkScalar* */ pPtr,
547 int count, SkShader::TileMode mode, uint32_t flags,
548 const SimpleMatrix& lm)->sk_sp<SkShader> {
549 // See comment above for uintptr_t explanation
550 const SkColor* colors = reinterpret_cast<const SkColor*> (cPtr);
551 const SkScalar* positions = reinterpret_cast<const SkScalar*>(pPtr);
552
553 SkMatrix localMatrix = toSkMatrix(lm);
554 return SkGradientShader::MakeRadial(center, radius, colors, positions, count,
555 mode, flags, &localMatrix);
556 }), allow_raw_pointers());
Kevin Lubickeb2f6b02018-11-29 15:07:02 -0500557 function("_MakeTwoPointConicalGradientShader", optional_override([](
558 SkPoint start, SkScalar startRadius,
559 SkPoint end, SkScalar endRadius,
560 uintptr_t /* SkColor* */ cPtr, uintptr_t /* SkScalar* */ pPtr,
561 int count, SkShader::TileMode mode, uint32_t flags)->sk_sp<SkShader> {
562 // See comment above for uintptr_t explanation
563 const SkColor* colors = reinterpret_cast<const SkColor*> (cPtr);
564 const SkScalar* positions = reinterpret_cast<const SkScalar*>(pPtr);
565
566 return SkGradientShader::MakeTwoPointConical(start, startRadius, end, endRadius,
567 colors, positions, count, mode,
568 flags, nullptr);
569 }), allow_raw_pointers());
570 function("_MakeTwoPointConicalGradientShader", optional_override([](
571 SkPoint start, SkScalar startRadius,
572 SkPoint end, SkScalar endRadius,
573 uintptr_t /* SkColor* */ cPtr, uintptr_t /* SkScalar* */ pPtr,
574 int count, SkShader::TileMode mode, uint32_t flags,
575 const SimpleMatrix& lm)->sk_sp<SkShader> {
576 // See comment above for uintptr_t explanation
577 const SkColor* colors = reinterpret_cast<const SkColor*> (cPtr);
578 const SkScalar* positions = reinterpret_cast<const SkScalar*>(pPtr);
579
580 SkMatrix localMatrix = toSkMatrix(lm);
581 return SkGradientShader::MakeTwoPointConical(start, startRadius, end, endRadius,
582 colors, positions, count, mode,
583 flags, &localMatrix);
584 }), allow_raw_pointers());
585
Kevin Lubickb5ae3b52018-11-03 07:51:19 -0400586 function("_MakeSkVertices", optional_override([](SkVertices::VertexMode mode, int vertexCount,
587 uintptr_t /* SkPoint* */ pPtr, uintptr_t /* SkPoint* */ tPtr,
588 uintptr_t /* SkColor* */ cPtr,
589 uintptr_t /* BoneIndices* */ biPtr, uintptr_t /* BoneWeights* */ bwPtr,
590 int indexCount, uintptr_t /* uint16_t * */ iPtr)->sk_sp<SkVertices> {
591 // See comment above for uintptr_t explanation
592 const SkPoint* positions = reinterpret_cast<const SkPoint*>(pPtr);
593 const SkPoint* texs = reinterpret_cast<const SkPoint*>(tPtr);
594 const SkColor* colors = reinterpret_cast<const SkColor*>(cPtr);
595 const BoneIndices* boneIndices = reinterpret_cast<const BoneIndices*>(biPtr);
596 const BoneWeights* boneWeights = reinterpret_cast<const BoneWeights*>(bwPtr);
597 const uint16_t* indices = reinterpret_cast<const uint16_t*>(iPtr);
598
599 return SkVertices::MakeCopy(mode, vertexCount, positions, texs, colors,
600 boneIndices, boneWeights, indexCount, indices);
601 }), allow_raw_pointers());
Kevin Lubick217056c2018-09-20 17:39:31 -0400602
603 class_<SkCanvas>("SkCanvas")
604 .constructor<>()
605 .function("clear", optional_override([](SkCanvas& self, JSColor color)->void {
606 // JS side gives us a signed int instead of an unsigned int for color
Kevin Lubickb5ae3b52018-11-03 07:51:19 -0400607 // Add a optional_override to change it out.
Kevin Lubick217056c2018-09-20 17:39:31 -0400608 self.clear(SkColor(color));
609 }))
Kevin Lubickeb2f6b02018-11-29 15:07:02 -0500610 .function("clipPath", select_overload<void (const SkPath&, SkClipOp, bool)>(&SkCanvas::clipPath))
Kevin Lubick52b9f372018-12-04 13:57:36 -0500611 .function("clipRect", select_overload<void (const SkRect&, SkClipOp, bool)>(&SkCanvas::clipRect))
612 .function("concat", optional_override([](SkCanvas& self, const SimpleMatrix& m) {
613 self.concat(toSkMatrix(m));
614 }))
Kevin Lubick0a1293c2018-12-03 12:31:04 -0500615 .function("drawImage", select_overload<void (const sk_sp<SkImage>&, SkScalar, SkScalar, const SkPaint*)>(&SkCanvas::drawImage), allow_raw_pointers())
616 .function("drawImageRect", optional_override([](SkCanvas& self, const sk_sp<SkImage>& image,
617 SkRect src, SkRect dst,
618 const SkPaint* paint, bool fastSample)->void {
619 self.drawImageRect(image, src, dst, paint,
620 fastSample ? SkCanvas::kFast_SrcRectConstraint :
621 SkCanvas::kStrict_SrcRectConstraint);
622 }), allow_raw_pointers())
Kevin Lubick217056c2018-09-20 17:39:31 -0400623 .function("drawPaint", &SkCanvas::drawPaint)
624 .function("drawPath", &SkCanvas::drawPath)
625 .function("drawRect", &SkCanvas::drawRect)
Kevin Lubick61ef7b22018-11-27 13:26:59 -0500626 .function("drawShadow", optional_override([](SkCanvas& self, const SkPath& path,
627 const SkPoint3& zPlaneParams,
628 const SkPoint3& lightPos, SkScalar lightRadius,
629 JSColor ambientColor, JSColor spotColor,
630 uint32_t flags) {
631 SkShadowUtils::DrawShadow(&self, path, zPlaneParams, lightPos, lightRadius,
632 SkColor(ambientColor), SkColor(spotColor), flags);
633 }))
634 .function("drawText", optional_override([](SkCanvas& self, std::string text, SkScalar x,
635 SkScalar y, const SkPaint& p) {
Kevin Lubick3d99b1e2018-10-16 10:15:01 -0400636 // TODO(kjlubick): This does not work well for non-ascii
637 // Need to maybe add a helper in interface.js that supports UTF-8
638 // Otherwise, go with std::wstring and set UTF-32 encoding.
Kevin Lubickd45c7812018-10-02 11:33:52 -0400639 self.drawText(text.c_str(), text.length(), x, y, p);
Kevin Lubick217056c2018-09-20 17:39:31 -0400640 }))
Kevin Lubickb5ae3b52018-11-03 07:51:19 -0400641 .function("drawVertices", select_overload<void (const sk_sp<SkVertices>&, SkBlendMode, const SkPaint&)>(&SkCanvas::drawVertices))
Kevin Lubick217056c2018-09-20 17:39:31 -0400642 .function("flush", &SkCanvas::flush)
Kevin Lubick52b9f372018-12-04 13:57:36 -0500643 .function("_readPixels", optional_override([](SkCanvas& self, SimpleImageInfo di,
644 uintptr_t /* uint8_t* */ pPtr,
645 size_t dstRowBytes, int srcX, int srcY) {
646 uint8_t* pixels = reinterpret_cast<uint8_t*>(pPtr);
647 SkImageInfo dstInfo = toSkImageInfo(di);
648
649 return self.readPixels(dstInfo, pixels, dstRowBytes, srcX, srcY);
650 }))
Kevin Lubickeb2f6b02018-11-29 15:07:02 -0500651 .function("restore", &SkCanvas::restore)
Kevin Lubickb5ae3b52018-11-03 07:51:19 -0400652 .function("rotate", select_overload<void (SkScalar, SkScalar, SkScalar)>(&SkCanvas::rotate))
Kevin Lubick217056c2018-09-20 17:39:31 -0400653 .function("save", &SkCanvas::save)
Kevin Lubick006a6f32018-10-19 14:34:34 -0400654 .function("scale", &SkCanvas::scale)
Kevin Lubick006a6f32018-10-19 14:34:34 -0400655 .function("skew", &SkCanvas::skew)
Kevin Lubick52b9f372018-12-04 13:57:36 -0500656 .function("translate", &SkCanvas::translate)
657 .function("_writePixels", optional_override([](SkCanvas& self, SimpleImageInfo di,
658 uintptr_t /* uint8_t* */ pPtr,
659 size_t srcRowBytes, int dstX, int dstY) {
660 uint8_t* pixels = reinterpret_cast<uint8_t*>(pPtr);
661 SkImageInfo dstInfo = toSkImageInfo(di);
662
663 return self.writePixels(dstInfo, pixels, srcRowBytes, dstX, dstY);
664 }))
665 ;
Kevin Lubick217056c2018-09-20 17:39:31 -0400666
667 class_<SkData>("SkData")
668 .smart_ptr<sk_sp<SkData>>("sk_sp<SkData>>")
669 .function("size", &SkData::size);
670
671 class_<SkImage>("SkImage")
672 .smart_ptr<sk_sp<SkImage>>("sk_sp<SkImage>")
Kevin Lubick0a1293c2018-12-03 12:31:04 -0500673 .function("height", &SkImage::height)
674 .function("width", &SkImage::width)
Alexander Khovansky3e119332018-11-15 02:01:19 +0300675 .function("_encodeToData", select_overload<sk_sp<SkData>()const>(&SkImage::encodeToData))
676 .function("_encodeToDataWithFormat", select_overload<sk_sp<SkData>(SkEncodedImageFormat encodedImageFormat, int quality)const>(&SkImage::encodeToData));
Kevin Lubick217056c2018-09-20 17:39:31 -0400677
Kevin Lubick61ef7b22018-11-27 13:26:59 -0500678 class_<SkMaskFilter>("SkMaskFilter")
679 .smart_ptr<sk_sp<SkMaskFilter>>("sk_sp<SkMaskFilter>");
680
Kevin Lubick217056c2018-09-20 17:39:31 -0400681 class_<SkPaint>("SkPaint")
682 .constructor<>()
683 .function("copy", optional_override([](const SkPaint& self)->SkPaint {
684 SkPaint p(self);
685 return p;
686 }))
Kevin Lubick12c0e502018-11-28 12:51:56 -0500687 .function("getBlendMode", &SkPaint::getBlendMode)
Kevin Lubick61ef7b22018-11-27 13:26:59 -0500688 .function("getColor", optional_override([](SkPaint& self)->JSColor {
689 // JS side gives us a signed int instead of an unsigned int for color
690 // Add a optional_override to change it out.
691 return JSColor(self.getColor());
692 }))
Kevin Lubick0a1293c2018-12-03 12:31:04 -0500693 .function("getFilterQuality", &SkPaint::getFilterQuality)
Kevin Lubickb9db3902018-11-26 11:47:54 -0500694 .function("getStrokeCap", &SkPaint::getStrokeCap)
695 .function("getStrokeJoin", &SkPaint::getStrokeJoin)
Kevin Lubick0a1293c2018-12-03 12:31:04 -0500696 .function("getStrokeMiter", &SkPaint::getStrokeMiter)
697 .function("getStrokeWidth", &SkPaint::getStrokeWidth)
Kevin Lubickb9db3902018-11-26 11:47:54 -0500698 .function("getTextSize", &SkPaint::getTextSize)
Kevin Lubick006a6f32018-10-19 14:34:34 -0400699 .function("measureText", optional_override([](SkPaint& self, std::string text) {
700 // TODO(kjlubick): This does not work well for non-ascii
701 // Need to maybe add a helper in interface.js that supports UTF-8
702 // Otherwise, go with std::wstring and set UTF-32 encoding.
703 return self.measureText(text.c_str(), text.length());
704 }))
Kevin Lubick217056c2018-09-20 17:39:31 -0400705 .function("setAntiAlias", &SkPaint::setAntiAlias)
Kevin Lubick12c0e502018-11-28 12:51:56 -0500706 .function("setBlendMode", &SkPaint::setBlendMode)
Kevin Lubick217056c2018-09-20 17:39:31 -0400707 .function("setColor", optional_override([](SkPaint& self, JSColor color)->void {
708 // JS side gives us a signed int instead of an unsigned int for color
Kevin Lubickb5ae3b52018-11-03 07:51:19 -0400709 // Add a optional_override to change it out.
Kevin Lubick217056c2018-09-20 17:39:31 -0400710 self.setColor(SkColor(color));
711 }))
Kevin Lubick0a1293c2018-12-03 12:31:04 -0500712 .function("setFilterQuality", &SkPaint::setFilterQuality)
Kevin Lubick61ef7b22018-11-27 13:26:59 -0500713 .function("setMaskFilter", &SkPaint::setMaskFilter)
Kevin Lubick217056c2018-09-20 17:39:31 -0400714 .function("setPathEffect", &SkPaint::setPathEffect)
715 .function("setShader", &SkPaint::setShader)
Kevin Lubickb9db3902018-11-26 11:47:54 -0500716 .function("setStrokeCap", &SkPaint::setStrokeCap)
717 .function("setStrokeJoin", &SkPaint::setStrokeJoin)
Kevin Lubick0a1293c2018-12-03 12:31:04 -0500718 .function("setStrokeMiter", &SkPaint::setStrokeMiter)
719 .function("setStrokeWidth", &SkPaint::setStrokeWidth)
Kevin Lubick217056c2018-09-20 17:39:31 -0400720 .function("setStyle", &SkPaint::setStyle)
721 .function("setTextSize", &SkPaint::setTextSize);
722
723 class_<SkPathEffect>("SkPathEffect")
724 .smart_ptr<sk_sp<SkPathEffect>>("sk_sp<SkPathEffect>");
725
Kevin Lubick217056c2018-09-20 17:39:31 -0400726 class_<SkPath>("SkPath")
727 .constructor<>()
728 .constructor<const SkPath&>()
Kevin Lubick1a05fce2018-11-20 12:51:16 -0500729 .function("_addArc", &ApplyAddArc)
Kevin Lubick217056c2018-09-20 17:39:31 -0400730 // interface.js has 3 overloads of addPath
731 .function("_addPath", &ApplyAddPath)
Kevin Lubick1a05fce2018-11-20 12:51:16 -0500732 // interface.js has 4 overloads of addRect
733 .function("_addRect", &ApplyAddRect)
Kevin Lubick217056c2018-09-20 17:39:31 -0400734 .function("_arcTo", &ApplyArcTo)
Kevin Lubick1646e7d2018-12-07 13:03:08 -0500735 .function("_arcTo", &ApplyArcToAngle)
Kevin Lubick217056c2018-09-20 17:39:31 -0400736 .function("_close", &ApplyClose)
737 .function("_conicTo", &ApplyConicTo)
Kevin Lubickb9db3902018-11-26 11:47:54 -0500738 .function("countPoints", &SkPath::countPoints)
Kevin Lubick1646e7d2018-12-07 13:03:08 -0500739 .function("contains", &SkPath::contains)
Kevin Lubick217056c2018-09-20 17:39:31 -0400740 .function("_cubicTo", &ApplyCubicTo)
Kevin Lubickb9db3902018-11-26 11:47:54 -0500741 .function("getPoint", &SkPath::getPoint)
Kevin Lubick1646e7d2018-12-07 13:03:08 -0500742 .function("isEmpty", &SkPath::isEmpty)
Kevin Lubick217056c2018-09-20 17:39:31 -0400743 .function("_lineTo", &ApplyLineTo)
744 .function("_moveTo", &ApplyMoveTo)
745 .function("_quadTo", &ApplyQuadTo)
Kevin Lubick1a05fce2018-11-20 12:51:16 -0500746 .function("_transform", select_overload<void(SkPath&, SkScalar, SkScalar, SkScalar, SkScalar, SkScalar, SkScalar, SkScalar, SkScalar, SkScalar)>(&ApplyTransform))
Kevin Lubick217056c2018-09-20 17:39:31 -0400747
Kevin Lubickb5ae3b52018-11-03 07:51:19 -0400748 // PathEffects
749 .function("_dash", &ApplyDash)
750 .function("_trim", &ApplyTrim)
751 .function("_stroke", &ApplyStroke)
752
753 // PathOps
754 .function("_simplify", &ApplySimplify)
755 .function("_op", &ApplyPathOp)
756
757 // Exporting
758 .function("toSVGString", &ToSVGString)
759
Kevin Lubick217056c2018-09-20 17:39:31 -0400760 .function("setFillType", &SkPath::setFillType)
761 .function("getFillType", &SkPath::getFillType)
762 .function("getBounds", &SkPath::getBounds)
763 .function("computeTightBounds", &SkPath::computeTightBounds)
764 .function("equals", &Equals)
Kevin Lubickb9db3902018-11-26 11:47:54 -0500765 .function("copy", &CopyPath)
766#ifdef SK_DEBUG
767 .function("dump", select_overload<void() const>(&SkPath::dump))
768 .function("dumpHex", select_overload<void() const>(&SkPath::dumpHex))
769#endif
770 ;
Kevin Lubick217056c2018-09-20 17:39:31 -0400771
Kevin Lubickb5ae3b52018-11-03 07:51:19 -0400772 class_<SkShader>("SkShader")
773 .smart_ptr<sk_sp<SkShader>>("sk_sp<SkShader>");
774
Kevin Lubick217056c2018-09-20 17:39:31 -0400775 class_<SkSurface>("SkSurface")
776 .smart_ptr<sk_sp<SkSurface>>("sk_sp<SkSurface>")
777 .function("width", &SkSurface::width)
778 .function("height", &SkSurface::height)
Kevin Lubick3d99b1e2018-10-16 10:15:01 -0400779 .function("_flush", &SkSurface::flush)
Mike Reed114bde82018-11-21 09:12:09 -0500780 .function("makeImageSnapshot", select_overload<sk_sp<SkImage>()>(&SkSurface::makeImageSnapshot))
781 .function("makeImageSnapshot", select_overload<sk_sp<SkImage>(const SkIRect& bounds)>(&SkSurface::makeImageSnapshot))
Kevin Lubick217056c2018-09-20 17:39:31 -0400782 .function("getCanvas", &SkSurface::getCanvas, allow_raw_pointers());
783
Kevin Lubickb5ae3b52018-11-03 07:51:19 -0400784 class_<SkVertices>("SkVertices")
785 .smart_ptr<sk_sp<SkVertices>>("sk_sp<SkVertices>")
786 .function("_applyBones", optional_override([](SkVertices& self, uintptr_t /* Bone* */ bptr, int boneCount)->sk_sp<SkVertices> {
787 // See comment above for uintptr_t explanation
788 const Bone* bones = reinterpret_cast<const Bone*>(bptr);
789 return self.applyBones(bones, boneCount);
790 }))
791 .function("bounds", &SkVertices::bounds)
792 .function("mode", &SkVertices::mode)
793 .function("uniqueID", &SkVertices::uniqueID)
Kevin Lubick12c0e502018-11-28 12:51:56 -0500794#ifdef SK_DEBUG
Kevin Lubickb5ae3b52018-11-03 07:51:19 -0400795 .function("dumpPositions", optional_override([](SkVertices& self)->void {
796 auto pos = self.positions();
797 for(int i = 0; i< self.vertexCount(); i++) {
798 SkDebugf("position[%d] = (%f, %f)\n", i, pos[i].x(), pos[i].y());
799 }
800 }))
Kevin Lubick12c0e502018-11-28 12:51:56 -0500801#endif
Kevin Lubickb5ae3b52018-11-03 07:51:19 -0400802 .function("vertexCount", &SkVertices::vertexCount);
803
Kevin Lubickea905ec2018-11-30 14:05:58 -0500804 enum_<SkAlphaType>("AlphaType")
805 .value("Opaque", SkAlphaType::kOpaque_SkAlphaType)
806 .value("Premul", SkAlphaType::kPremul_SkAlphaType)
807 .value("Unpremul", SkAlphaType::kUnpremul_SkAlphaType);
Kevin Lubickb5ae3b52018-11-03 07:51:19 -0400808
809 enum_<SkBlendMode>("BlendMode")
810 .value("Clear", SkBlendMode::kClear)
811 .value("Src", SkBlendMode::kSrc)
812 .value("Dst", SkBlendMode::kDst)
813 .value("SrcOver", SkBlendMode::kSrcOver)
814 .value("DstOver", SkBlendMode::kDstOver)
815 .value("SrcIn", SkBlendMode::kSrcIn)
816 .value("DstIn", SkBlendMode::kDstIn)
817 .value("SrcOut", SkBlendMode::kSrcOut)
818 .value("DstOut", SkBlendMode::kDstOut)
819 .value("SrcATop", SkBlendMode::kSrcATop)
820 .value("DstATop", SkBlendMode::kDstATop)
821 .value("Xor", SkBlendMode::kXor)
822 .value("Plus", SkBlendMode::kPlus)
823 .value("Modulate", SkBlendMode::kModulate)
824 .value("Screen", SkBlendMode::kScreen)
825 .value("Overlay", SkBlendMode::kOverlay)
826 .value("Darken", SkBlendMode::kDarken)
827 .value("Lighten", SkBlendMode::kLighten)
828 .value("ColorDodge", SkBlendMode::kColorDodge)
829 .value("ColorBurn", SkBlendMode::kColorBurn)
830 .value("HardLight", SkBlendMode::kHardLight)
831 .value("SoftLight", SkBlendMode::kSoftLight)
832 .value("Difference", SkBlendMode::kDifference)
833 .value("Exclusion", SkBlendMode::kExclusion)
834 .value("Multiply", SkBlendMode::kMultiply)
835 .value("Hue", SkBlendMode::kHue)
836 .value("Saturation", SkBlendMode::kSaturation)
837 .value("Color", SkBlendMode::kColor)
838 .value("Luminosity", SkBlendMode::kLuminosity);
Kevin Lubick217056c2018-09-20 17:39:31 -0400839
Kevin Lubick61ef7b22018-11-27 13:26:59 -0500840 enum_<SkBlurStyle>("BlurStyle")
841 .value("Normal", SkBlurStyle::kNormal_SkBlurStyle)
842 .value("Solid", SkBlurStyle::kSolid_SkBlurStyle)
843 .value("Outer", SkBlurStyle::kOuter_SkBlurStyle)
844 .value("Inner", SkBlurStyle::kInner_SkBlurStyle);
Kevin Lubick217056c2018-09-20 17:39:31 -0400845
Kevin Lubickeb2f6b02018-11-29 15:07:02 -0500846 enum_<SkClipOp>("ClipOp")
847 .value("Difference", SkClipOp::kDifference)
848 .value("Intersect", SkClipOp::kIntersect);
849
Kevin Lubickea905ec2018-11-30 14:05:58 -0500850 enum_<SkColorType>("ColorType")
851 .value("Alpha_8", SkColorType::kAlpha_8_SkColorType)
852 .value("RGB_565", SkColorType::kRGB_565_SkColorType)
853 .value("ARGB_4444", SkColorType::kARGB_4444_SkColorType)
854 .value("RGBA_8888", SkColorType::kRGBA_8888_SkColorType)
855 .value("RGB_888x", SkColorType::kRGB_888x_SkColorType)
856 .value("BGRA_8888", SkColorType::kBGRA_8888_SkColorType)
857 .value("RGBA_1010102", SkColorType::kRGBA_1010102_SkColorType)
858 .value("RGB_101010x", SkColorType::kRGB_101010x_SkColorType)
859 .value("Gray_8", SkColorType::kGray_8_SkColorType)
860 .value("RGBA_F16", SkColorType::kRGBA_F16_SkColorType)
861 .value("RGBA_F32", SkColorType::kRGBA_F32_SkColorType);
862
Kevin Lubick217056c2018-09-20 17:39:31 -0400863 enum_<SkPath::FillType>("FillType")
Kevin Lubickb5ae3b52018-11-03 07:51:19 -0400864 .value("Winding", SkPath::FillType::kWinding_FillType)
865 .value("EvenOdd", SkPath::FillType::kEvenOdd_FillType)
866 .value("InverseWinding", SkPath::FillType::kInverseWinding_FillType)
867 .value("InverseEvenOdd", SkPath::FillType::kInverseEvenOdd_FillType);
868
Kevin Lubick0a1293c2018-12-03 12:31:04 -0500869 enum_<SkFilterQuality>("FilterQuality")
870 .value("None", SkFilterQuality::kNone_SkFilterQuality)
871 .value("Low", SkFilterQuality::kLow_SkFilterQuality)
872 .value("Medium", SkFilterQuality::kMedium_SkFilterQuality)
873 .value("High", SkFilterQuality::kHigh_SkFilterQuality);
874
Kevin Lubick61ef7b22018-11-27 13:26:59 -0500875 enum_<SkEncodedImageFormat>("ImageFormat")
876 .value("PNG", SkEncodedImageFormat::kPNG)
877 .value("JPEG", SkEncodedImageFormat::kJPEG);
878
879 enum_<SkPaint::Style>("PaintStyle")
880 .value("Fill", SkPaint::Style::kFill_Style)
881 .value("Stroke", SkPaint::Style::kStroke_Style)
882 .value("StrokeAndFill", SkPaint::Style::kStrokeAndFill_Style);
883
Kevin Lubickb5ae3b52018-11-03 07:51:19 -0400884 enum_<SkPathOp>("PathOp")
885 .value("Difference", SkPathOp::kDifference_SkPathOp)
886 .value("Intersect", SkPathOp::kIntersect_SkPathOp)
887 .value("Union", SkPathOp::kUnion_SkPathOp)
888 .value("XOR", SkPathOp::kXOR_SkPathOp)
889 .value("ReverseDifference", SkPathOp::kReverseDifference_SkPathOp);
890
Kevin Lubickb9db3902018-11-26 11:47:54 -0500891 enum_<SkPaint::Cap>("StrokeCap")
892 .value("Butt", SkPaint::Cap::kButt_Cap)
893 .value("Round", SkPaint::Cap::kRound_Cap)
894 .value("Square", SkPaint::Cap::kSquare_Cap);
895
896 enum_<SkPaint::Join>("StrokeJoin")
897 .value("Miter", SkPaint::Join::kMiter_Join)
898 .value("Round", SkPaint::Join::kRound_Join)
899 .value("Bevel", SkPaint::Join::kBevel_Join);
900
901 value_object<StrokeOpts>("StrokeOpts")
902 .field("width", &StrokeOpts::width)
903 .field("miter_limit", &StrokeOpts::miter_limit)
904 .field("join", &StrokeOpts::join)
Kevin Lubick1646e7d2018-12-07 13:03:08 -0500905 .field("cap", &StrokeOpts::cap)
906 .field("precision", &StrokeOpts::precision);
Kevin Lubickb9db3902018-11-26 11:47:54 -0500907
Kevin Lubickb5ae3b52018-11-03 07:51:19 -0400908 enum_<SkShader::TileMode>("TileMode")
909 .value("Clamp", SkShader::TileMode::kClamp_TileMode)
910 .value("Repeat", SkShader::TileMode::kRepeat_TileMode)
Kevin Lubickd29edd72018-12-07 08:29:52 -0500911 .value("Mirror", SkShader::TileMode::kMirror_TileMode)
912 // Decal mode only works in the SW backend, not WebGl (yet).
913 .value("Decal", SkShader::TileMode::kDecal_TileMode);
Kevin Lubickb5ae3b52018-11-03 07:51:19 -0400914
915 enum_<SkVertices::VertexMode>("VertexMode")
916 .value("Triangles", SkVertices::VertexMode::kTriangles_VertexMode)
917 .value("TrianglesStrip", SkVertices::VertexMode::kTriangleStrip_VertexMode)
Kevin Lubickb9db3902018-11-26 11:47:54 -0500918 .value("TriangleFan", SkVertices::VertexMode::kTriangleFan_VertexMode);
Kevin Lubickb5ae3b52018-11-03 07:51:19 -0400919
Kevin Lubick217056c2018-09-20 17:39:31 -0400920
921 // A value object is much simpler than a class - it is returned as a JS
922 // object and does not require delete().
923 // https://kripken.github.io/emscripten-site/docs/porting/connecting_cpp_and_javascript/embind.html#value-types
924 value_object<SkRect>("SkRect")
925 .field("fLeft", &SkRect::fLeft)
926 .field("fTop", &SkRect::fTop)
927 .field("fRight", &SkRect::fRight)
928 .field("fBottom", &SkRect::fBottom);
929
Mike Reed114bde82018-11-21 09:12:09 -0500930 value_object<SkIRect>("SkIRect")
931 .field("fLeft", &SkIRect::fLeft)
932 .field("fTop", &SkIRect::fTop)
933 .field("fRight", &SkIRect::fRight)
934 .field("fBottom", &SkIRect::fBottom);
935
Kevin Lubickea905ec2018-11-30 14:05:58 -0500936 value_object<SimpleImageInfo>("SkImageInfo")
937 .field("width", &SimpleImageInfo::width)
938 .field("height", &SimpleImageInfo::height)
939 .field("colorType", &SimpleImageInfo::colorType)
940 .field("alphaType", &SimpleImageInfo::alphaType);
941
Kevin Lubick217056c2018-09-20 17:39:31 -0400942 // SkPoints can be represented by [x, y]
943 value_array<SkPoint>("SkPoint")
944 .element(&SkPoint::fX)
945 .element(&SkPoint::fY);
946
Kevin Lubick61ef7b22018-11-27 13:26:59 -0500947 // SkPoint3s can be represented by [x, y, z]
948 value_array<SkPoint3>("SkPoint3")
949 .element(&SkPoint3::fX)
950 .element(&SkPoint3::fY)
951 .element(&SkPoint3::fZ);
952
Kevin Lubick217056c2018-09-20 17:39:31 -0400953 // {"w": Number, "h", Number}
954 value_object<SkSize>("SkSize")
955 .field("w", &SkSize::fWidth)
956 .field("h", &SkSize::fHeight);
957
958 value_object<SkISize>("SkISize")
959 .field("w", &SkISize::fWidth)
960 .field("h", &SkISize::fHeight);
961
Kevin Lubickb5ae3b52018-11-03 07:51:19 -0400962 // Allows clients to supply a 1D array of 9 elements and the bindings
963 // will automatically turn it into a 3x3 2D matrix.
964 // e.g. path.transform([0,1,2,3,4,5,6,7,8])
965 // This is likely simpler for the client than exposing SkMatrix
966 // directly and requiring them to do a lot of .delete().
967 value_array<SimpleMatrix>("SkMatrix")
968 .element(&SimpleMatrix::scaleX)
969 .element(&SimpleMatrix::skewX)
970 .element(&SimpleMatrix::transX)
971
972 .element(&SimpleMatrix::skewY)
973 .element(&SimpleMatrix::scaleY)
974 .element(&SimpleMatrix::transY)
975
976 .element(&SimpleMatrix::pers0)
977 .element(&SimpleMatrix::pers1)
978 .element(&SimpleMatrix::pers2);
979
980 constant("TRANSPARENT", (JSColor) SK_ColorTRANSPARENT);
981 constant("RED", (JSColor) SK_ColorRED);
982 constant("BLUE", (JSColor) SK_ColorBLUE);
983 constant("YELLOW", (JSColor) SK_ColorYELLOW);
984 constant("CYAN", (JSColor) SK_ColorCYAN);
Kevin Lubick61ef7b22018-11-27 13:26:59 -0500985 constant("BLACK", (JSColor) SK_ColorBLACK);
Kevin Lubickea905ec2018-11-30 14:05:58 -0500986 constant("WHITE", (JSColor) SK_ColorWHITE);
Kevin Lubickb5ae3b52018-11-03 07:51:19 -0400987 // TODO(?)
988
Kevin Lubick53965c92018-10-11 08:51:55 -0400989#if SK_INCLUDE_SKOTTIE
Kevin Lubick217056c2018-09-20 17:39:31 -0400990 // Animation things (may eventually go in own library)
991 class_<skottie::Animation>("Animation")
992 .smart_ptr<sk_sp<skottie::Animation>>("sk_sp<Animation>")
993 .function("version", optional_override([](skottie::Animation& self)->std::string {
994 return std::string(self.version().c_str());
995 }))
996 .function("size", &skottie::Animation::size)
997 .function("duration", &skottie::Animation::duration)
998 .function("seek", &skottie::Animation::seek)
999 .function("render", optional_override([](skottie::Animation& self, SkCanvas* canvas)->void {
1000 self.render(canvas, nullptr);
1001 }), allow_raw_pointers())
Florin Malitac2e85f92018-11-29 11:38:12 -05001002 .function("render", optional_override([](skottie::Animation& self, SkCanvas* canvas,
1003 const SkRect r)->void {
Kevin Lubick217056c2018-09-20 17:39:31 -04001004 self.render(canvas, &r);
1005 }), allow_raw_pointers());
1006
Kevin Lubickb5ae3b52018-11-03 07:51:19 -04001007 function("MakeAnimation", optional_override([](std::string json)->sk_sp<skottie::Animation> {
1008 return skottie::Animation::Make(json.c_str(), json.length());
1009 }));
Kevin Lubick3d99b1e2018-10-16 10:15:01 -04001010 constant("skottie", true);
Florin Malitac2e85f92018-11-29 11:38:12 -05001011
1012#if SK_INCLUDE_MANAGED_SKOTTIE
1013 class_<ManagedAnimation>("ManagedAnimation")
1014 .smart_ptr<sk_sp<ManagedAnimation>>("sk_sp<ManagedAnimation>")
1015 .function("version" , &ManagedAnimation::version)
1016 .function("size" , &ManagedAnimation::size)
1017 .function("duration" , &ManagedAnimation::duration)
1018 .function("seek" , &ManagedAnimation::seek)
1019 .function("render" , select_overload<void(SkCanvas*) const>
1020 (&ManagedAnimation::render), allow_raw_pointers())
1021 .function("render" , select_overload<void(SkCanvas*, const SkRect&) const>
1022 (&ManagedAnimation::render), allow_raw_pointers())
1023 .function("setColor" , &ManagedAnimation::setColor)
1024 .function("setOpacity", &ManagedAnimation::setOpacity)
Florin Malita6ba8c7e2018-12-03 09:55:52 -05001025 .function("getMarkers", &ManagedAnimation::getMarkers)
Florin Malitac2e85f92018-11-29 11:38:12 -05001026 .function("getColorProps" , &ManagedAnimation::getColorProps)
1027 .function("getOpacityProps", &ManagedAnimation::getOpacityProps);
1028
1029 function("MakeManagedAnimation", &ManagedAnimation::Make);
1030 constant("managed_skottie", true);
1031#endif // SK_INCLUDE_MANAGED_SKOTTIE
1032#endif // SK_INCLUDE_SKOTTIE
Kevin Lubick134be1d2018-10-30 15:05:04 -04001033
Kevin Lubick217056c2018-09-20 17:39:31 -04001034}