• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2019 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 
8 #ifndef WasmCommon_DEFINED
9 #define WasmCommon_DEFINED
10 
11 #include <emscripten.h>
12 #include <emscripten/bind.h>
13 #include "include/core/SkColor.h"
14 #include "include/core/SkRefCnt.h"
15 #include "include/core/SkSpan.h"
16 #include "include/private/base/SkMalloc.h"
17 
18 #include <memory>
19 
20 class SkData;
21 class SkCodec;
22 
23 using namespace emscripten;
24 
25 // Self-documenting types
26 using JSColor = int32_t;
27 using JSArray = emscripten::val;
28 using JSObject = emscripten::val;
29 using JSString = emscripten::val;
30 using SkPathOrNull = emscripten::val;
31 using TypedArray = emscripten::val;
32 using Uint8Array = emscripten::val;
33 using Uint16Array = emscripten::val;
34 using Int32Array = emscripten::val;
35 using Uint32Array = emscripten::val;
36 using Float32Array = emscripten::val;
37 
38 // If we are using C++ and EMSCRIPTEN_BINDINGS, we can't have primitive pointers in our function
39 // type signatures. (this gives an error message like "Cannot call foo due to unbound
40 // types Pi, Pf").  But, we can just pretend they are numbers and cast them to be pointers and
41 // the compiler is happy.
42 // These types refer to the TypedArray that the JS interface wrote into or will read out of.
43 // This doesn't stop us from using these as different types; e.g. a float* can be treated as an
44 // SkPoint* in some APIs.
45 using WASMPointerF32 = uintptr_t;
46 using WASMPointerI32 = uintptr_t;
47 using WASMPointerU8  = uintptr_t;
48 using WASMPointerU16 = uintptr_t;
49 using WASMPointerU32 = uintptr_t;
50 using WASMPointer = uintptr_t;
51 
52 #define SPECIALIZE_JSARRAYTYPE(type, name)                  \
53     template <> struct JSArrayType<type> {                  \
54         static constexpr const char* const gName = name;    \
55     }
56 
57 template <typename T> struct JSArrayType {};
58 
59 SPECIALIZE_JSARRAYTYPE( int8_t,    "Int8Array");
60 SPECIALIZE_JSARRAYTYPE(uint8_t,   "Uint8Array");
61 SPECIALIZE_JSARRAYTYPE( int16_t,  "Int16Array");
62 SPECIALIZE_JSARRAYTYPE(uint16_t, "Uint16Array");
63 SPECIALIZE_JSARRAYTYPE( int32_t,  "Int32Array");
64 SPECIALIZE_JSARRAYTYPE(uint32_t, "Uint32Array");
65 SPECIALIZE_JSARRAYTYPE(float,   "Float32Array");
66 
67 #undef SPECIALIZE_JSARRAYTYPE
68 
69 /**
70  *  Create a typed-array (in the JS heap) and initialize it with the provided
71  *  data (from the wasm heap).
72  */
MakeTypedArray(int count,const T src[])73 template <typename T> TypedArray MakeTypedArray(int count, const T src[]) {
74     emscripten::val length = emscripten::val(count);
75     emscripten::val jarray = emscripten::val::global(JSArrayType<T>::gName).new_(count);
76     jarray.call<void>("set", val(typed_memory_view(count, src)));
77     return jarray;
78 }
79 
80 SkColor4f ptrToSkColor4f(WASMPointerF32);
81 
82 std::unique_ptr<SkCodec> DecodeImageData(sk_sp<SkData>);
83 
84 /**
85  *  Gives read access to a JSArray
86  *
87  *  We explicitly use malloc/free (not new/delete) so this can be used with allocations from the JS
88  *  side (ala CanvasKit.Malloc).
89  */
90 template <typename T> class JSSpan {
91 public:
92     // Note: Use of this constructor is 5-20x slower than manually copying the data on the JS side
93     // and sending over a pointer, length, and boolean for the other constructor.
JSSpan(JSArray src)94     JSSpan(JSArray src) {
95         const size_t len = src["length"].as<size_t>();
96         T* data;
97 
98         // If the buffer was allocated via CanvasKit' Malloc, we can peek directly at it!
99         if (src["_ck"].isTrue()) {
100             fOwned = false;
101             data = reinterpret_cast<T*>(src["byteOffset"].as<size_t>());
102         } else {
103             fOwned = true;
104             data = static_cast<T*>(sk_malloc_throw(len, sizeof(T)));
105 
106             // now actually copy into 'data'
107             if (src.instanceof(emscripten::val::global(JSArrayType<T>::gName))) {
108                 auto dst_view = emscripten::val(typed_memory_view(len, data));
109                 dst_view.call<void>("set", src);
110             } else {
111                 for (size_t i = 0; i < len; ++i) {
112                     data[i] = src[i].as<T>();
113                 }
114             }
115         }
116         fSpan = SkSpan(data, len);
117     }
118 
JSSpan(WASMPointer ptr,size_t len,bool takeOwnership)119     JSSpan(WASMPointer ptr, size_t len, bool takeOwnership): fOwned(takeOwnership) {
120         fSpan = SkSpan(reinterpret_cast<T*>(ptr), len);
121     }
122 
~JSSpan()123     ~JSSpan() {
124         if (fOwned) {
125             sk_free(fSpan.data());
126         }
127     }
128 
data()129     const T* data() const { return fSpan.data(); }
size()130     size_t size() const { return fSpan.size(); }
131 
132 private:
133     SkSpan<T>   fSpan;
134     bool        fOwned;
135 };
136 
137 #endif
138