Kevin Lubick | 217056c | 2018-09-20 17:39:31 -0400 | [diff] [blame] | 1 | /* |
| 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 Lubick | 53965c9 | 2018-10-11 08:51:55 -0400 | [diff] [blame] | 8 | #if SK_SUPPORT_GPU |
Kevin Lubick | 217056c | 2018-09-20 17:39:31 -0400 | [diff] [blame] | 9 | #include "GrBackendSurface.h" |
| 10 | #include "GrContext.h" |
| 11 | #include "GrGLInterface.h" |
| 12 | #include "GrGLTypes.h" |
Kevin Lubick | 53965c9 | 2018-10-11 08:51:55 -0400 | [diff] [blame] | 13 | #endif |
| 14 | |
Kevin Lubick | b5ae3b5 | 2018-11-03 07:51:19 -0400 | [diff] [blame] | 15 | #include "SkBlendMode.h" |
Kevin Lubick | ea905ec | 2018-11-30 14:05:58 -0500 | [diff] [blame] | 16 | #include "SkBlurTypes.h" |
Kevin Lubick | 217056c | 2018-09-20 17:39:31 -0400 | [diff] [blame] | 17 | #include "SkCanvas.h" |
Kevin Lubick | b5ae3b5 | 2018-11-03 07:51:19 -0400 | [diff] [blame] | 18 | #include "SkColor.h" |
Kevin Lubick | 217056c | 2018-09-20 17:39:31 -0400 | [diff] [blame] | 19 | #include "SkCornerPathEffect.h" |
Kevin Lubick | b5ae3b5 | 2018-11-03 07:51:19 -0400 | [diff] [blame] | 20 | #include "SkDashPathEffect.h" |
Kevin Lubick | b5ae3b5 | 2018-11-03 07:51:19 -0400 | [diff] [blame] | 21 | #include "SkData.h" |
Kevin Lubick | 217056c | 2018-09-20 17:39:31 -0400 | [diff] [blame] | 22 | #include "SkDiscretePathEffect.h" |
Alexander Khovansky | 3e11933 | 2018-11-15 02:01:19 +0300 | [diff] [blame] | 23 | #include "SkEncodedImageFormat.h" |
Kevin Lubick | 0a1293c | 2018-12-03 12:31:04 -0500 | [diff] [blame] | 24 | #include "SkFilterQuality.h" |
Kevin Lubick | 217056c | 2018-09-20 17:39:31 -0400 | [diff] [blame] | 25 | #include "SkFontMgr.h" |
| 26 | #include "SkFontMgrPriv.h" |
Kevin Lubick | b5ae3b5 | 2018-11-03 07:51:19 -0400 | [diff] [blame] | 27 | #include "SkGradientShader.h" |
Kevin Lubick | 0a1293c | 2018-12-03 12:31:04 -0500 | [diff] [blame] | 28 | #include "SkImage.h" |
Kevin Lubick | ea905ec | 2018-11-30 14:05:58 -0500 | [diff] [blame] | 29 | #include "SkImageInfo.h" |
Kevin Lubick | b5ae3b5 | 2018-11-03 07:51:19 -0400 | [diff] [blame] | 30 | #include "SkImageShader.h" |
Florin Malita | 91af8d8 | 2018-11-30 16:46:45 -0500 | [diff] [blame] | 31 | #include "SkMakeUnique.h" |
Kevin Lubick | 61ef7b2 | 2018-11-27 13:26:59 -0500 | [diff] [blame] | 32 | #include "SkMaskFilter.h" |
Kevin Lubick | 217056c | 2018-09-20 17:39:31 -0400 | [diff] [blame] | 33 | #include "SkPaint.h" |
Kevin Lubick | b5ae3b5 | 2018-11-03 07:51:19 -0400 | [diff] [blame] | 34 | #include "SkParsePath.h" |
Kevin Lubick | 217056c | 2018-09-20 17:39:31 -0400 | [diff] [blame] | 35 | #include "SkPath.h" |
| 36 | #include "SkPathEffect.h" |
Kevin Lubick | b5ae3b5 | 2018-11-03 07:51:19 -0400 | [diff] [blame] | 37 | #include "SkPathOps.h" |
Kevin Lubick | 217056c | 2018-09-20 17:39:31 -0400 | [diff] [blame] | 38 | #include "SkScalar.h" |
Kevin Lubick | b5ae3b5 | 2018-11-03 07:51:19 -0400 | [diff] [blame] | 39 | #include "SkShader.h" |
Kevin Lubick | 61ef7b2 | 2018-11-27 13:26:59 -0500 | [diff] [blame] | 40 | #include "SkShadowUtils.h" |
Kevin Lubick | b5ae3b5 | 2018-11-03 07:51:19 -0400 | [diff] [blame] | 41 | #include "SkString.h" |
| 42 | #include "SkStrokeRec.h" |
Kevin Lubick | 217056c | 2018-09-20 17:39:31 -0400 | [diff] [blame] | 43 | #include "SkSurface.h" |
| 44 | #include "SkSurfaceProps.h" |
| 45 | #include "SkTestFontMgr.h" |
Kevin Lubick | b5ae3b5 | 2018-11-03 07:51:19 -0400 | [diff] [blame] | 46 | #include "SkTrimPathEffect.h" |
| 47 | #include "SkVertices.h" |
| 48 | |
Kevin Lubick | 53965c9 | 2018-10-11 08:51:55 -0400 | [diff] [blame] | 49 | #if SK_INCLUDE_SKOTTIE |
Kevin Lubick | 217056c | 2018-09-20 17:39:31 -0400 | [diff] [blame] | 50 | #include "Skottie.h" |
Florin Malita | c2e85f9 | 2018-11-29 11:38:12 -0500 | [diff] [blame] | 51 | #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 Lubick | 217056c | 2018-09-20 17:39:31 -0400 | [diff] [blame] | 56 | |
| 57 | #include <iostream> |
| 58 | #include <string> |
Kevin Lubick | 217056c | 2018-09-20 17:39:31 -0400 | [diff] [blame] | 59 | |
| 60 | #include <emscripten.h> |
| 61 | #include <emscripten/bind.h> |
Kevin Lubick | 53965c9 | 2018-10-11 08:51:55 -0400 | [diff] [blame] | 62 | #if SK_SUPPORT_GPU |
| 63 | #include <GL/gl.h> |
Kevin Lubick | 217056c | 2018-09-20 17:39:31 -0400 | [diff] [blame] | 64 | #include <emscripten/html5.h> |
Kevin Lubick | 53965c9 | 2018-10-11 08:51:55 -0400 | [diff] [blame] | 65 | #endif |
Kevin Lubick | 217056c | 2018-09-20 17:39:31 -0400 | [diff] [blame] | 66 | |
| 67 | using namespace emscripten; |
| 68 | |
Kevin Lubick | 134be1d | 2018-10-30 15:05:04 -0400 | [diff] [blame] | 69 | // Self-documenting types |
| 70 | using JSArray = emscripten::val; |
Kevin Lubick | 217056c | 2018-09-20 17:39:31 -0400 | [diff] [blame] | 71 | using JSColor = int32_t; |
Florin Malita | c2e85f9 | 2018-11-29 11:38:12 -0500 | [diff] [blame] | 72 | using JSObject = emscripten::val; |
Kevin Lubick | b5ae3b5 | 2018-11-03 07:51:19 -0400 | [diff] [blame] | 73 | using JSString = emscripten::val; |
| 74 | using SkPathOrNull = emscripten::val; |
| 75 | using Uint8Array = emscripten::val; |
| 76 | |
| 77 | // Aliases for less typing |
| 78 | using BoneIndices = SkVertices::BoneIndices; |
| 79 | using BoneWeights = SkVertices::BoneWeights; |
| 80 | using Bone = SkVertices::Bone; |
Kevin Lubick | 217056c | 2018-09-20 17:39:31 -0400 | [diff] [blame] | 81 | |
Kevin Lubick | 217056c | 2018-09-20 17:39:31 -0400 | [diff] [blame] | 82 | void EMSCRIPTEN_KEEPALIVE initFonts() { |
| 83 | gSkFontMgr_DefaultFactory = &sk_tool_utils::MakePortableFontMgr; |
| 84 | } |
| 85 | |
Kevin Lubick | 53965c9 | 2018-10-11 08:51:55 -0400 | [diff] [blame] | 86 | #if SK_SUPPORT_GPU |
Kevin Lubick | 217056c | 2018-09-20 17:39:31 -0400 | [diff] [blame] | 87 | // Wraps the WebGL context in an SkSurface and returns it. |
Kevin Lubick | 9e40e6f | 2018-10-22 12:08:22 -0400 | [diff] [blame] | 88 | // This function based on the work of |
| 89 | // https://github.com/Zubnix/skia-wasm-port/, used under the terms of the MIT license. |
Kevin Lubick | 217056c | 2018-09-20 17:39:31 -0400 | [diff] [blame] | 90 | sk_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 Lubick | 53965c9 | 2018-10-11 08:51:55 -0400 | [diff] [blame] | 138 | #endif |
Kevin Lubick | 217056c | 2018-09-20 17:39:31 -0400 | [diff] [blame] | 139 | |
Kevin Lubick | b5ae3b5 | 2018-11-03 07:51:19 -0400 | [diff] [blame] | 140 | struct SimpleMatrix { |
| 141 | SkScalar scaleX, skewX, transX; |
| 142 | SkScalar skewY, scaleY, transY; |
| 143 | SkScalar pers0, pers1, pers2; |
| 144 | }; |
| 145 | |
| 146 | SkMatrix 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 Lubick | 217056c | 2018-09-20 17:39:31 -0400 | [diff] [blame] | 150 | } |
| 151 | |
Kevin Lubick | ea905ec | 2018-11-30 14:05:58 -0500 | [diff] [blame] | 152 | struct SimpleImageInfo { |
| 153 | int width; |
| 154 | int height; |
| 155 | SkColorType colorType; |
| 156 | SkAlphaType alphaType; |
| 157 | // TODO color spaces? |
| 158 | }; |
| 159 | |
| 160 | SkImageInfo toSkImageInfo(const SimpleImageInfo& sii) { |
| 161 | return SkImageInfo::Make(sii.width, sii.height, sii.colorType, sii.alphaType); |
| 162 | } |
| 163 | |
Florin Malita | c2e85f9 | 2018-11-29 11:38:12 -0500 | [diff] [blame] | 164 | #if SK_INCLUDE_SKOTTIE && SK_INCLUDE_MANAGED_SKOTTIE |
| 165 | namespace { |
| 166 | |
| 167 | class ManagedAnimation final : public SkRefCnt { |
| 168 | public: |
| 169 | static sk_sp<ManagedAnimation> Make(const std::string& json) { |
Florin Malita | 91af8d8 | 2018-11-30 16:46:45 -0500 | [diff] [blame] | 170 | auto mgr = skstd::make_unique<skottie_utils::CustomPropertyManager>(); |
Florin Malita | c2e85f9 | 2018-11-29 11:38:12 -0500 | [diff] [blame] | 171 | auto animation = skottie::Animation::Builder() |
Florin Malita | 6ba8c7e | 2018-12-03 09:55:52 -0500 | [diff] [blame] | 172 | .setMarkerObserver(mgr->getMarkerObserver()) |
Florin Malita | 91af8d8 | 2018-11-30 16:46:45 -0500 | [diff] [blame] | 173 | .setPropertyObserver(mgr->getPropertyObserver()) |
Florin Malita | c2e85f9 | 2018-11-29 11:38:12 -0500 | [diff] [blame] | 174 | .make(json.c_str(), json.size()); |
| 175 | |
| 176 | return animation |
Florin Malita | 91af8d8 | 2018-11-30 16:46:45 -0500 | [diff] [blame] | 177 | ? sk_sp<ManagedAnimation>(new ManagedAnimation(std::move(animation), std::move(mgr))) |
Florin Malita | c2e85f9 | 2018-11-29 11:38:12 -0500 | [diff] [blame] | 178 | : 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 Malita | 6ba8c7e | 2018-12-03 09:55:52 -0500 | [diff] [blame] | 224 | 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 Malita | c2e85f9 | 2018-11-29 11:38:12 -0500 | [diff] [blame] | 236 | private: |
| 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 Lubick | 217056c | 2018-09-20 17:39:31 -0400 | [diff] [blame] | 249 | //======================================================================================== |
| 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 Lubick | 1a05fce | 2018-11-20 12:51:16 -0500 | [diff] [blame] | 259 | void ApplyAddArc(SkPath& orig, const SkRect& oval, SkScalar startAngle, SkScalar sweepAngle) { |
| 260 | orig.addArc(oval, startAngle, sweepAngle); |
| 261 | } |
| 262 | |
Kevin Lubick | 217056c | 2018-09-20 17:39:31 -0400 | [diff] [blame] | 263 | void ApplyAddPath(SkPath& orig, const SkPath& newPath, |
| 264 | SkScalar scaleX, SkScalar skewX, SkScalar transX, |
| 265 | SkScalar skewY, SkScalar scaleY, SkScalar transY, |
Kevin Lubick | 1a05fce | 2018-11-20 12:51:16 -0500 | [diff] [blame] | 266 | SkScalar pers0, SkScalar pers1, SkScalar pers2, |
| 267 | bool extendPath) { |
Kevin Lubick | 217056c | 2018-09-20 17:39:31 -0400 | [diff] [blame] | 268 | SkMatrix m = SkMatrix::MakeAll(scaleX, skewX , transX, |
| 269 | skewY , scaleY, transY, |
| 270 | pers0 , pers1 , pers2); |
Kevin Lubick | 1a05fce | 2018-11-20 12:51:16 -0500 | [diff] [blame] | 271 | orig.addPath(newPath, m, extendPath ? SkPath::kExtend_AddPathMode : |
| 272 | SkPath::kAppend_AddPathMode); |
Kevin Lubick | 217056c | 2018-09-20 17:39:31 -0400 | [diff] [blame] | 273 | } |
| 274 | |
Kevin Lubick | 1a05fce | 2018-11-20 12:51:16 -0500 | [diff] [blame] | 275 | void 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 Khovansky | 3e11933 | 2018-11-15 02:01:19 +0300 | [diff] [blame] | 280 | } |
| 281 | |
Kevin Lubick | 217056c | 2018-09-20 17:39:31 -0400 | [diff] [blame] | 282 | void ApplyArcTo(SkPath& p, SkScalar x1, SkScalar y1, SkScalar x2, SkScalar y2, |
| 283 | SkScalar radius) { |
| 284 | p.arcTo(x1, y1, x2, y2, radius); |
| 285 | } |
| 286 | |
| 287 | void ApplyClose(SkPath& p) { |
| 288 | p.close(); |
| 289 | } |
| 290 | |
| 291 | void ApplyConicTo(SkPath& p, SkScalar x1, SkScalar y1, SkScalar x2, SkScalar y2, |
| 292 | SkScalar w) { |
| 293 | p.conicTo(x1, y1, x2, y2, w); |
| 294 | } |
| 295 | |
| 296 | void 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 | |
| 301 | void ApplyLineTo(SkPath& p, SkScalar x, SkScalar y) { |
| 302 | p.lineTo(x, y); |
| 303 | } |
| 304 | |
| 305 | void ApplyMoveTo(SkPath& p, SkScalar x, SkScalar y) { |
| 306 | p.moveTo(x, y); |
| 307 | } |
| 308 | |
| 309 | void ApplyQuadTo(SkPath& p, SkScalar x1, SkScalar y1, SkScalar x2, SkScalar y2) { |
| 310 | p.quadTo(x1, y1, x2, y2); |
| 311 | } |
| 312 | |
| 313 | void 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 Lubick | b5ae3b5 | 2018-11-03 07:51:19 -0400 | [diff] [blame] | 323 | bool EMSCRIPTEN_KEEPALIVE ApplySimplify(SkPath& path) { |
| 324 | return Simplify(path, &path); |
| 325 | } |
| 326 | |
| 327 | bool EMSCRIPTEN_KEEPALIVE ApplyPathOp(SkPath& pathOne, const SkPath& pathTwo, SkPathOp op) { |
| 328 | return Op(pathOne, pathTwo, op, &pathOne); |
| 329 | } |
| 330 | |
| 331 | JSString EMSCRIPTEN_KEEPALIVE ToSVGString(const SkPath& path) { |
| 332 | SkString s; |
| 333 | SkParsePath::ToSVGString(path, &s); |
| 334 | return emscripten::val(s.c_str()); |
| 335 | } |
| 336 | |
| 337 | SkPathOrNull 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 Lubick | 217056c | 2018-09-20 17:39:31 -0400 | [diff] [blame] | 345 | SkPath EMSCRIPTEN_KEEPALIVE CopyPath(const SkPath& a) { |
| 346 | SkPath copy(a); |
| 347 | return copy; |
| 348 | } |
| 349 | |
| 350 | bool EMSCRIPTEN_KEEPALIVE Equals(const SkPath& a, const SkPath& b) { |
| 351 | return a == b; |
| 352 | } |
| 353 | |
Kevin Lubick | b5ae3b5 | 2018-11-03 07:51:19 -0400 | [diff] [blame] | 354 | //======================================================================================== |
| 355 | // Path Effects |
| 356 | //======================================================================================== |
| 357 | |
| 358 | bool 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 Lubick | 217056c | 2018-09-20 17:39:31 -0400 | [diff] [blame] | 371 | } |
| 372 | |
Kevin Lubick | b5ae3b5 | 2018-11-03 07:51:19 -0400 | [diff] [blame] | 373 | bool 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 | |
| 388 | struct StrokeOpts { |
Kevin Lubick | b9db390 | 2018-11-26 11:47:54 -0500 | [diff] [blame] | 389 | // Default values are set in interface.js which allows clients |
Kevin Lubick | b5ae3b5 | 2018-11-03 07:51:19 -0400 | [diff] [blame] | 390 | // 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 | |
| 398 | bool 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 |
| 410 | Uint8Array 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 Lubick | 217056c | 2018-09-20 17:39:31 -0400 | [diff] [blame] | 416 | namespace 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 Lubick | b5ae3b5 | 2018-11-03 07:51:19 -0400 | [diff] [blame] | 424 | |
| 425 | template<> |
| 426 | void raw_destructor<SkVertices>(SkVertices *ptr) { |
| 427 | } |
Kevin Lubick | 217056c | 2018-09-20 17:39:31 -0400 | [diff] [blame] | 428 | } |
| 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. |
| 442 | EMSCRIPTEN_BINDINGS(Skia) { |
| 443 | function("initFonts", &initFonts); |
Kevin Lubick | 53965c9 | 2018-10-11 08:51:55 -0400 | [diff] [blame] | 444 | #if SK_SUPPORT_GPU |
Kevin Lubick | 217056c | 2018-09-20 17:39:31 -0400 | [diff] [blame] | 445 | function("_getWebGLSurface", &getWebGLSurface, allow_raw_pointers()); |
Kevin Lubick | 53965c9 | 2018-10-11 08:51:55 -0400 | [diff] [blame] | 446 | function("currentContext", &emscripten_webgl_get_current_context); |
| 447 | function("setCurrentContext", &emscripten_webgl_make_context_current); |
Kevin Lubick | 3d99b1e | 2018-10-16 10:15:01 -0400 | [diff] [blame] | 448 | constant("gpu", true); |
Kevin Lubick | b07204a | 2018-11-20 14:07:42 -0500 | [diff] [blame] | 449 | #endif |
Kevin Lubick | 0a1293c | 2018-12-03 12:31:04 -0500 | [diff] [blame] | 450 | 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 Lubick | ea905ec | 2018-11-30 14:05:58 -0500 | [diff] [blame] | 456 | function("_getRasterDirectSurface", optional_override([](const SimpleImageInfo ii, |
Kevin Lubick | 52b9f37 | 2018-12-04 13:57:36 -0500 | [diff] [blame] | 457 | uintptr_t /* uint8_t* */ pPtr, |
Kevin Lubick | ea905ec | 2018-11-30 14:05:58 -0500 | [diff] [blame] | 458 | size_t rowBytes)->sk_sp<SkSurface> { |
Kevin Lubick | 52b9f37 | 2018-12-04 13:57:36 -0500 | [diff] [blame] | 459 | uint8_t* pixels = reinterpret_cast<uint8_t*>(pPtr); |
Kevin Lubick | ea905ec | 2018-11-30 14:05:58 -0500 | [diff] [blame] | 460 | SkImageInfo imageInfo = toSkImageInfo(ii); |
| 461 | return SkSurface::MakeRasterDirect(imageInfo, pixels, rowBytes, nullptr); |
| 462 | }), allow_raw_pointers()); |
Kevin Lubick | 53965c9 | 2018-10-11 08:51:55 -0400 | [diff] [blame] | 463 | function("_getRasterN32PremulSurface", optional_override([](int width, int height)->sk_sp<SkSurface> { |
| 464 | return SkSurface::MakeRasterN32Premul(width, height, nullptr); |
| 465 | }), allow_raw_pointers()); |
Kevin Lubick | b07204a | 2018-11-20 14:07:42 -0500 | [diff] [blame] | 466 | |
Kevin Lubick | b5ae3b5 | 2018-11-03 07:51:19 -0400 | [diff] [blame] | 467 | function("getSkDataBytes", &getSkDataBytes, allow_raw_pointers()); |
Kevin Lubick | 217056c | 2018-09-20 17:39:31 -0400 | [diff] [blame] | 468 | function("MakeSkCornerPathEffect", &SkCornerPathEffect::Make, allow_raw_pointers()); |
| 469 | function("MakeSkDiscretePathEffect", &SkDiscretePathEffect::Make, allow_raw_pointers()); |
Kevin Lubick | 61ef7b2 | 2018-11-27 13:26:59 -0500 | [diff] [blame] | 470 | 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 Lubick | b5ae3b5 | 2018-11-03 07:51:19 -0400 | [diff] [blame] | 474 | function("MakePathFromOp", &MakePathFromOp); |
| 475 | |
| 476 | // These won't be called directly, there's a JS helper to deal with typed arrays. |
Kevin Lubick | 217056c | 2018-09-20 17:39:31 -0400 | [diff] [blame] | 477 | 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 Lubick | 52b9f37 | 2018-12-04 13:57:36 -0500 | [diff] [blame] | 482 | 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 Lubick | d29edd7 | 2018-12-07 08:29:52 -0500 | [diff] [blame^] | 492 | // 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 Lubick | b5ae3b5 | 2018-11-03 07:51:19 -0400 | [diff] [blame] | 497 | }), allow_raw_pointers()); |
Kevin Lubick | d29edd7 | 2018-12-07 08:29:52 -0500 | [diff] [blame^] | 498 | 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 Lubick | b5ae3b5 | 2018-11-03 07:51:19 -0400 | [diff] [blame] | 505 | 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 Lubick | eb2f6b0 | 2018-11-29 15:07:02 -0500 | [diff] [blame] | 552 | 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 Lubick | b5ae3b5 | 2018-11-03 07:51:19 -0400 | [diff] [blame] | 581 | 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 Lubick | 217056c | 2018-09-20 17:39:31 -0400 | [diff] [blame] | 597 | |
| 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 Lubick | b5ae3b5 | 2018-11-03 07:51:19 -0400 | [diff] [blame] | 602 | // Add a optional_override to change it out. |
Kevin Lubick | 217056c | 2018-09-20 17:39:31 -0400 | [diff] [blame] | 603 | self.clear(SkColor(color)); |
| 604 | })) |
Kevin Lubick | eb2f6b0 | 2018-11-29 15:07:02 -0500 | [diff] [blame] | 605 | .function("clipPath", select_overload<void (const SkPath&, SkClipOp, bool)>(&SkCanvas::clipPath)) |
Kevin Lubick | 52b9f37 | 2018-12-04 13:57:36 -0500 | [diff] [blame] | 606 | .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 Lubick | 0a1293c | 2018-12-03 12:31:04 -0500 | [diff] [blame] | 610 | .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 Lubick | 217056c | 2018-09-20 17:39:31 -0400 | [diff] [blame] | 618 | .function("drawPaint", &SkCanvas::drawPaint) |
| 619 | .function("drawPath", &SkCanvas::drawPath) |
| 620 | .function("drawRect", &SkCanvas::drawRect) |
Kevin Lubick | 61ef7b2 | 2018-11-27 13:26:59 -0500 | [diff] [blame] | 621 | .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 Lubick | 3d99b1e | 2018-10-16 10:15:01 -0400 | [diff] [blame] | 631 | // 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 Lubick | d45c781 | 2018-10-02 11:33:52 -0400 | [diff] [blame] | 634 | self.drawText(text.c_str(), text.length(), x, y, p); |
Kevin Lubick | 217056c | 2018-09-20 17:39:31 -0400 | [diff] [blame] | 635 | })) |
Kevin Lubick | b5ae3b5 | 2018-11-03 07:51:19 -0400 | [diff] [blame] | 636 | .function("drawVertices", select_overload<void (const sk_sp<SkVertices>&, SkBlendMode, const SkPaint&)>(&SkCanvas::drawVertices)) |
Kevin Lubick | 217056c | 2018-09-20 17:39:31 -0400 | [diff] [blame] | 637 | .function("flush", &SkCanvas::flush) |
Kevin Lubick | 52b9f37 | 2018-12-04 13:57:36 -0500 | [diff] [blame] | 638 | .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 Lubick | eb2f6b0 | 2018-11-29 15:07:02 -0500 | [diff] [blame] | 646 | .function("restore", &SkCanvas::restore) |
Kevin Lubick | b5ae3b5 | 2018-11-03 07:51:19 -0400 | [diff] [blame] | 647 | .function("rotate", select_overload<void (SkScalar, SkScalar, SkScalar)>(&SkCanvas::rotate)) |
Kevin Lubick | 217056c | 2018-09-20 17:39:31 -0400 | [diff] [blame] | 648 | .function("save", &SkCanvas::save) |
Kevin Lubick | 006a6f3 | 2018-10-19 14:34:34 -0400 | [diff] [blame] | 649 | .function("scale", &SkCanvas::scale) |
Kevin Lubick | 006a6f3 | 2018-10-19 14:34:34 -0400 | [diff] [blame] | 650 | .function("skew", &SkCanvas::skew) |
Kevin Lubick | 52b9f37 | 2018-12-04 13:57:36 -0500 | [diff] [blame] | 651 | .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 Lubick | 217056c | 2018-09-20 17:39:31 -0400 | [diff] [blame] | 661 | |
| 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 Lubick | 0a1293c | 2018-12-03 12:31:04 -0500 | [diff] [blame] | 668 | .function("height", &SkImage::height) |
| 669 | .function("width", &SkImage::width) |
Alexander Khovansky | 3e11933 | 2018-11-15 02:01:19 +0300 | [diff] [blame] | 670 | .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 Lubick | 217056c | 2018-09-20 17:39:31 -0400 | [diff] [blame] | 672 | |
Kevin Lubick | 61ef7b2 | 2018-11-27 13:26:59 -0500 | [diff] [blame] | 673 | class_<SkMaskFilter>("SkMaskFilter") |
| 674 | .smart_ptr<sk_sp<SkMaskFilter>>("sk_sp<SkMaskFilter>"); |
| 675 | |
Kevin Lubick | 217056c | 2018-09-20 17:39:31 -0400 | [diff] [blame] | 676 | class_<SkPaint>("SkPaint") |
| 677 | .constructor<>() |
| 678 | .function("copy", optional_override([](const SkPaint& self)->SkPaint { |
| 679 | SkPaint p(self); |
| 680 | return p; |
| 681 | })) |
Kevin Lubick | 12c0e50 | 2018-11-28 12:51:56 -0500 | [diff] [blame] | 682 | .function("getBlendMode", &SkPaint::getBlendMode) |
Kevin Lubick | 61ef7b2 | 2018-11-27 13:26:59 -0500 | [diff] [blame] | 683 | .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 Lubick | 0a1293c | 2018-12-03 12:31:04 -0500 | [diff] [blame] | 688 | .function("getFilterQuality", &SkPaint::getFilterQuality) |
Kevin Lubick | b9db390 | 2018-11-26 11:47:54 -0500 | [diff] [blame] | 689 | .function("getStrokeCap", &SkPaint::getStrokeCap) |
| 690 | .function("getStrokeJoin", &SkPaint::getStrokeJoin) |
Kevin Lubick | 0a1293c | 2018-12-03 12:31:04 -0500 | [diff] [blame] | 691 | .function("getStrokeMiter", &SkPaint::getStrokeMiter) |
| 692 | .function("getStrokeWidth", &SkPaint::getStrokeWidth) |
Kevin Lubick | b9db390 | 2018-11-26 11:47:54 -0500 | [diff] [blame] | 693 | .function("getTextSize", &SkPaint::getTextSize) |
Kevin Lubick | 006a6f3 | 2018-10-19 14:34:34 -0400 | [diff] [blame] | 694 | .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 Lubick | 217056c | 2018-09-20 17:39:31 -0400 | [diff] [blame] | 700 | .function("setAntiAlias", &SkPaint::setAntiAlias) |
Kevin Lubick | 12c0e50 | 2018-11-28 12:51:56 -0500 | [diff] [blame] | 701 | .function("setBlendMode", &SkPaint::setBlendMode) |
Kevin Lubick | 217056c | 2018-09-20 17:39:31 -0400 | [diff] [blame] | 702 | .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 Lubick | b5ae3b5 | 2018-11-03 07:51:19 -0400 | [diff] [blame] | 704 | // Add a optional_override to change it out. |
Kevin Lubick | 217056c | 2018-09-20 17:39:31 -0400 | [diff] [blame] | 705 | self.setColor(SkColor(color)); |
| 706 | })) |
Kevin Lubick | 0a1293c | 2018-12-03 12:31:04 -0500 | [diff] [blame] | 707 | .function("setFilterQuality", &SkPaint::setFilterQuality) |
Kevin Lubick | 61ef7b2 | 2018-11-27 13:26:59 -0500 | [diff] [blame] | 708 | .function("setMaskFilter", &SkPaint::setMaskFilter) |
Kevin Lubick | 217056c | 2018-09-20 17:39:31 -0400 | [diff] [blame] | 709 | .function("setPathEffect", &SkPaint::setPathEffect) |
| 710 | .function("setShader", &SkPaint::setShader) |
Kevin Lubick | b9db390 | 2018-11-26 11:47:54 -0500 | [diff] [blame] | 711 | .function("setStrokeCap", &SkPaint::setStrokeCap) |
| 712 | .function("setStrokeJoin", &SkPaint::setStrokeJoin) |
Kevin Lubick | 0a1293c | 2018-12-03 12:31:04 -0500 | [diff] [blame] | 713 | .function("setStrokeMiter", &SkPaint::setStrokeMiter) |
| 714 | .function("setStrokeWidth", &SkPaint::setStrokeWidth) |
Kevin Lubick | 217056c | 2018-09-20 17:39:31 -0400 | [diff] [blame] | 715 | .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 Lubick | 217056c | 2018-09-20 17:39:31 -0400 | [diff] [blame] | 721 | class_<SkPath>("SkPath") |
| 722 | .constructor<>() |
| 723 | .constructor<const SkPath&>() |
Kevin Lubick | 1a05fce | 2018-11-20 12:51:16 -0500 | [diff] [blame] | 724 | .function("_addArc", &ApplyAddArc) |
Kevin Lubick | 217056c | 2018-09-20 17:39:31 -0400 | [diff] [blame] | 725 | // interface.js has 3 overloads of addPath |
| 726 | .function("_addPath", &ApplyAddPath) |
Kevin Lubick | 1a05fce | 2018-11-20 12:51:16 -0500 | [diff] [blame] | 727 | // interface.js has 4 overloads of addRect |
| 728 | .function("_addRect", &ApplyAddRect) |
Kevin Lubick | 217056c | 2018-09-20 17:39:31 -0400 | [diff] [blame] | 729 | .function("_arcTo", &ApplyArcTo) |
| 730 | .function("_close", &ApplyClose) |
| 731 | .function("_conicTo", &ApplyConicTo) |
Kevin Lubick | b9db390 | 2018-11-26 11:47:54 -0500 | [diff] [blame] | 732 | .function("countPoints", &SkPath::countPoints) |
Kevin Lubick | 217056c | 2018-09-20 17:39:31 -0400 | [diff] [blame] | 733 | .function("_cubicTo", &ApplyCubicTo) |
Kevin Lubick | b9db390 | 2018-11-26 11:47:54 -0500 | [diff] [blame] | 734 | .function("getPoint", &SkPath::getPoint) |
Kevin Lubick | 217056c | 2018-09-20 17:39:31 -0400 | [diff] [blame] | 735 | .function("_lineTo", &ApplyLineTo) |
| 736 | .function("_moveTo", &ApplyMoveTo) |
| 737 | .function("_quadTo", &ApplyQuadTo) |
Kevin Lubick | 1a05fce | 2018-11-20 12:51:16 -0500 | [diff] [blame] | 738 | .function("_transform", select_overload<void(SkPath&, SkScalar, SkScalar, SkScalar, SkScalar, SkScalar, SkScalar, SkScalar, SkScalar, SkScalar)>(&ApplyTransform)) |
Kevin Lubick | 217056c | 2018-09-20 17:39:31 -0400 | [diff] [blame] | 739 | |
Kevin Lubick | b5ae3b5 | 2018-11-03 07:51:19 -0400 | [diff] [blame] | 740 | // 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 Lubick | 217056c | 2018-09-20 17:39:31 -0400 | [diff] [blame] | 752 | .function("setFillType", &SkPath::setFillType) |
| 753 | .function("getFillType", &SkPath::getFillType) |
| 754 | .function("getBounds", &SkPath::getBounds) |
| 755 | .function("computeTightBounds", &SkPath::computeTightBounds) |
| 756 | .function("equals", &Equals) |
Kevin Lubick | b9db390 | 2018-11-26 11:47:54 -0500 | [diff] [blame] | 757 | .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 Lubick | 217056c | 2018-09-20 17:39:31 -0400 | [diff] [blame] | 763 | |
Kevin Lubick | b5ae3b5 | 2018-11-03 07:51:19 -0400 | [diff] [blame] | 764 | class_<SkShader>("SkShader") |
| 765 | .smart_ptr<sk_sp<SkShader>>("sk_sp<SkShader>"); |
| 766 | |
Kevin Lubick | 217056c | 2018-09-20 17:39:31 -0400 | [diff] [blame] | 767 | class_<SkSurface>("SkSurface") |
| 768 | .smart_ptr<sk_sp<SkSurface>>("sk_sp<SkSurface>") |
| 769 | .function("width", &SkSurface::width) |
| 770 | .function("height", &SkSurface::height) |
Kevin Lubick | 3d99b1e | 2018-10-16 10:15:01 -0400 | [diff] [blame] | 771 | .function("_flush", &SkSurface::flush) |
Mike Reed | 114bde8 | 2018-11-21 09:12:09 -0500 | [diff] [blame] | 772 | .function("makeImageSnapshot", select_overload<sk_sp<SkImage>()>(&SkSurface::makeImageSnapshot)) |
| 773 | .function("makeImageSnapshot", select_overload<sk_sp<SkImage>(const SkIRect& bounds)>(&SkSurface::makeImageSnapshot)) |
Kevin Lubick | 217056c | 2018-09-20 17:39:31 -0400 | [diff] [blame] | 774 | .function("getCanvas", &SkSurface::getCanvas, allow_raw_pointers()); |
| 775 | |
Kevin Lubick | b5ae3b5 | 2018-11-03 07:51:19 -0400 | [diff] [blame] | 776 | 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 Lubick | 12c0e50 | 2018-11-28 12:51:56 -0500 | [diff] [blame] | 786 | #ifdef SK_DEBUG |
Kevin Lubick | b5ae3b5 | 2018-11-03 07:51:19 -0400 | [diff] [blame] | 787 | .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 Lubick | 12c0e50 | 2018-11-28 12:51:56 -0500 | [diff] [blame] | 793 | #endif |
Kevin Lubick | b5ae3b5 | 2018-11-03 07:51:19 -0400 | [diff] [blame] | 794 | .function("vertexCount", &SkVertices::vertexCount); |
| 795 | |
Kevin Lubick | ea905ec | 2018-11-30 14:05:58 -0500 | [diff] [blame] | 796 | enum_<SkAlphaType>("AlphaType") |
| 797 | .value("Opaque", SkAlphaType::kOpaque_SkAlphaType) |
| 798 | .value("Premul", SkAlphaType::kPremul_SkAlphaType) |
| 799 | .value("Unpremul", SkAlphaType::kUnpremul_SkAlphaType); |
Kevin Lubick | b5ae3b5 | 2018-11-03 07:51:19 -0400 | [diff] [blame] | 800 | |
| 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 Lubick | 217056c | 2018-09-20 17:39:31 -0400 | [diff] [blame] | 831 | |
Kevin Lubick | 61ef7b2 | 2018-11-27 13:26:59 -0500 | [diff] [blame] | 832 | 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 Lubick | 217056c | 2018-09-20 17:39:31 -0400 | [diff] [blame] | 837 | |
Kevin Lubick | eb2f6b0 | 2018-11-29 15:07:02 -0500 | [diff] [blame] | 838 | enum_<SkClipOp>("ClipOp") |
| 839 | .value("Difference", SkClipOp::kDifference) |
| 840 | .value("Intersect", SkClipOp::kIntersect); |
| 841 | |
Kevin Lubick | ea905ec | 2018-11-30 14:05:58 -0500 | [diff] [blame] | 842 | 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 Lubick | 217056c | 2018-09-20 17:39:31 -0400 | [diff] [blame] | 855 | enum_<SkPath::FillType>("FillType") |
Kevin Lubick | b5ae3b5 | 2018-11-03 07:51:19 -0400 | [diff] [blame] | 856 | .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 Lubick | 0a1293c | 2018-12-03 12:31:04 -0500 | [diff] [blame] | 861 | 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 Lubick | 61ef7b2 | 2018-11-27 13:26:59 -0500 | [diff] [blame] | 867 | 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 Lubick | b5ae3b5 | 2018-11-03 07:51:19 -0400 | [diff] [blame] | 876 | 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 Lubick | b9db390 | 2018-11-26 11:47:54 -0500 | [diff] [blame] | 883 | 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 Lubick | b5ae3b5 | 2018-11-03 07:51:19 -0400 | [diff] [blame] | 899 | enum_<SkShader::TileMode>("TileMode") |
| 900 | .value("Clamp", SkShader::TileMode::kClamp_TileMode) |
| 901 | .value("Repeat", SkShader::TileMode::kRepeat_TileMode) |
Kevin Lubick | d29edd7 | 2018-12-07 08:29:52 -0500 | [diff] [blame^] | 902 | .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 Lubick | b5ae3b5 | 2018-11-03 07:51:19 -0400 | [diff] [blame] | 905 | |
| 906 | enum_<SkVertices::VertexMode>("VertexMode") |
| 907 | .value("Triangles", SkVertices::VertexMode::kTriangles_VertexMode) |
| 908 | .value("TrianglesStrip", SkVertices::VertexMode::kTriangleStrip_VertexMode) |
Kevin Lubick | b9db390 | 2018-11-26 11:47:54 -0500 | [diff] [blame] | 909 | .value("TriangleFan", SkVertices::VertexMode::kTriangleFan_VertexMode); |
Kevin Lubick | b5ae3b5 | 2018-11-03 07:51:19 -0400 | [diff] [blame] | 910 | |
Kevin Lubick | 217056c | 2018-09-20 17:39:31 -0400 | [diff] [blame] | 911 | |
| 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 Reed | 114bde8 | 2018-11-21 09:12:09 -0500 | [diff] [blame] | 921 | 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 Lubick | ea905ec | 2018-11-30 14:05:58 -0500 | [diff] [blame] | 927 | 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 Lubick | 217056c | 2018-09-20 17:39:31 -0400 | [diff] [blame] | 933 | // SkPoints can be represented by [x, y] |
| 934 | value_array<SkPoint>("SkPoint") |
| 935 | .element(&SkPoint::fX) |
| 936 | .element(&SkPoint::fY); |
| 937 | |
Kevin Lubick | 61ef7b2 | 2018-11-27 13:26:59 -0500 | [diff] [blame] | 938 | // 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 Lubick | 217056c | 2018-09-20 17:39:31 -0400 | [diff] [blame] | 944 | // {"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 Lubick | b5ae3b5 | 2018-11-03 07:51:19 -0400 | [diff] [blame] | 953 | // 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 Lubick | 61ef7b2 | 2018-11-27 13:26:59 -0500 | [diff] [blame] | 976 | constant("BLACK", (JSColor) SK_ColorBLACK); |
Kevin Lubick | ea905ec | 2018-11-30 14:05:58 -0500 | [diff] [blame] | 977 | constant("WHITE", (JSColor) SK_ColorWHITE); |
Kevin Lubick | b5ae3b5 | 2018-11-03 07:51:19 -0400 | [diff] [blame] | 978 | // TODO(?) |
| 979 | |
Kevin Lubick | 53965c9 | 2018-10-11 08:51:55 -0400 | [diff] [blame] | 980 | #if SK_INCLUDE_SKOTTIE |
Kevin Lubick | 217056c | 2018-09-20 17:39:31 -0400 | [diff] [blame] | 981 | // 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 Malita | c2e85f9 | 2018-11-29 11:38:12 -0500 | [diff] [blame] | 993 | .function("render", optional_override([](skottie::Animation& self, SkCanvas* canvas, |
| 994 | const SkRect r)->void { |
Kevin Lubick | 217056c | 2018-09-20 17:39:31 -0400 | [diff] [blame] | 995 | self.render(canvas, &r); |
| 996 | }), allow_raw_pointers()); |
| 997 | |
Kevin Lubick | b5ae3b5 | 2018-11-03 07:51:19 -0400 | [diff] [blame] | 998 | function("MakeAnimation", optional_override([](std::string json)->sk_sp<skottie::Animation> { |
| 999 | return skottie::Animation::Make(json.c_str(), json.length()); |
| 1000 | })); |
Kevin Lubick | 3d99b1e | 2018-10-16 10:15:01 -0400 | [diff] [blame] | 1001 | constant("skottie", true); |
Florin Malita | c2e85f9 | 2018-11-29 11:38:12 -0500 | [diff] [blame] | 1002 | |
| 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 Malita | 6ba8c7e | 2018-12-03 09:55:52 -0500 | [diff] [blame] | 1016 | .function("getMarkers", &ManagedAnimation::getMarkers) |
Florin Malita | c2e85f9 | 2018-11-29 11:38:12 -0500 | [diff] [blame] | 1017 | .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 Lubick | 134be1d | 2018-10-30 15:05:04 -0400 | [diff] [blame] | 1024 | |
Kevin Lubick | 217056c | 2018-09-20 17:39:31 -0400 | [diff] [blame] | 1025 | } |