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