2020-03-26 13:27:48 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2019 Google LLC
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef WasmCommon_DEFINED
|
|
|
|
#define WasmCommon_DEFINED
|
|
|
|
|
|
|
|
#include <emscripten.h>
|
|
|
|
#include <emscripten/bind.h>
|
|
|
|
#include "include/core/SkColor.h"
|
2021-05-18 15:01:12 +00:00
|
|
|
#include "include/core/SkSpan.h"
|
2021-05-26 17:40:22 +00:00
|
|
|
#include "include/private/SkMalloc.h"
|
2020-03-26 13:27:48 +00:00
|
|
|
|
|
|
|
using namespace emscripten;
|
|
|
|
|
|
|
|
// Self-documenting types
|
|
|
|
using JSArray = emscripten::val;
|
|
|
|
using JSObject = emscripten::val;
|
|
|
|
using JSString = emscripten::val;
|
|
|
|
using SkPathOrNull = emscripten::val;
|
2021-04-16 19:48:53 +00:00
|
|
|
using TypedArray = emscripten::val;
|
2020-03-26 13:27:48 +00:00
|
|
|
using Uint8Array = emscripten::val;
|
2021-04-16 19:48:53 +00:00
|
|
|
using Uint16Array = emscripten::val;
|
|
|
|
using Uint32Array = emscripten::val;
|
2020-03-26 13:27:48 +00:00
|
|
|
using Float32Array = emscripten::val;
|
|
|
|
|
2021-05-26 17:40:22 +00:00
|
|
|
// If we are using C++ and EMSCRIPTEN_BINDINGS, we can't have primitive pointers in our function
|
|
|
|
// type signatures. (this gives an error message like "Cannot call foo due to unbound
|
|
|
|
// types Pi, Pf"). But, we can just pretend they are numbers and cast them to be pointers and
|
|
|
|
// the compiler is happy.
|
|
|
|
// These types refer to the TypedArray that the JS interface wrote into or will read out of.
|
|
|
|
// This doesn't stop us from using these as different types; e.g. a float* can be treated as an
|
|
|
|
// SkPoint* in some APIs.
|
|
|
|
using WASMPointerF32 = uintptr_t;
|
|
|
|
using WASMPointerU8 = uintptr_t;
|
|
|
|
using WASMPointerU16 = uintptr_t;
|
|
|
|
using WASMPointerU32 = uintptr_t;
|
2021-06-02 12:38:04 +00:00
|
|
|
using WASMPointer = uintptr_t;
|
2021-05-26 17:40:22 +00:00
|
|
|
|
2021-05-18 17:52:19 +00:00
|
|
|
#define SPECIALIZE_JSARRAYTYPE(type, name) \
|
|
|
|
template <> struct JSArrayType<type> { \
|
|
|
|
static constexpr const char* const gName = name; \
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T> struct JSArrayType {};
|
|
|
|
|
|
|
|
SPECIALIZE_JSARRAYTYPE( int8_t, "Int8Array");
|
|
|
|
SPECIALIZE_JSARRAYTYPE(uint8_t, "Uint8Array");
|
|
|
|
SPECIALIZE_JSARRAYTYPE( int16_t, "Int16Array");
|
|
|
|
SPECIALIZE_JSARRAYTYPE(uint16_t, "Uint16Array");
|
|
|
|
SPECIALIZE_JSARRAYTYPE( int32_t, "Int32Array");
|
|
|
|
SPECIALIZE_JSARRAYTYPE(uint32_t, "Uint32Array");
|
|
|
|
SPECIALIZE_JSARRAYTYPE(float, "Float32Array");
|
|
|
|
|
|
|
|
#undef SPECIALIZE_JSARRAYTYPE
|
|
|
|
|
2021-04-16 19:48:53 +00:00
|
|
|
/**
|
|
|
|
* Create a typed-array (in the JS heap) and initialize it with the provided
|
2021-05-18 17:52:19 +00:00
|
|
|
* data (from the wasm heap).
|
2021-04-16 19:48:53 +00:00
|
|
|
*/
|
2021-05-18 17:52:19 +00:00
|
|
|
template <typename T> TypedArray MakeTypedArray(int count, const T src[]) {
|
2021-04-16 19:48:53 +00:00
|
|
|
emscripten::val length = emscripten::val(count);
|
2021-05-18 17:52:19 +00:00
|
|
|
emscripten::val jarray = emscripten::val::global(JSArrayType<T>::gName).new_(count);
|
2021-04-16 19:48:53 +00:00
|
|
|
jarray.call<void>("set", val(typed_memory_view(count, src)));
|
|
|
|
return jarray;
|
|
|
|
}
|
|
|
|
|
2021-05-15 00:55:58 +00:00
|
|
|
/**
|
2021-05-18 15:01:12 +00:00
|
|
|
* Gives read access to a JSArray
|
2021-05-23 20:32:32 +00:00
|
|
|
*
|
|
|
|
* We explicitly use malloc/free (not new/delete) so this can be used with allocations from the JS
|
|
|
|
* side (ala CanvasKit.Malloc).
|
2021-05-15 00:55:58 +00:00
|
|
|
*/
|
2021-05-18 15:01:12 +00:00
|
|
|
template <typename T> class JSSpan {
|
|
|
|
public:
|
2021-06-02 12:38:04 +00:00
|
|
|
// Note: Use of this constructor is 5-20x slower than manually copying the data on the JS side
|
|
|
|
// and sending over a pointer, length, and boolean for the other constructor.
|
2021-05-18 17:52:19 +00:00
|
|
|
JSSpan(JSArray src) {
|
2021-05-15 00:55:58 +00:00
|
|
|
const size_t len = src["length"].as<size_t>();
|
2021-05-18 15:01:12 +00:00
|
|
|
T* data;
|
|
|
|
|
|
|
|
// If the buffer was allocated via CanvasKit' Malloc, we can peek directly at it!
|
|
|
|
if (src["_ck"].isTrue()) {
|
|
|
|
fOwned = false;
|
|
|
|
data = reinterpret_cast<T*>(src["byteOffset"].as<size_t>());
|
|
|
|
} else {
|
|
|
|
fOwned = true;
|
2021-05-23 20:32:32 +00:00
|
|
|
data = static_cast<T*>(sk_malloc_throw(len, sizeof(T)));
|
2021-05-18 15:01:12 +00:00
|
|
|
|
|
|
|
// now actually copy into 'data'
|
2021-05-18 17:52:19 +00:00
|
|
|
if (src.instanceof(emscripten::val::global(JSArrayType<T>::gName))) {
|
2021-05-18 15:01:12 +00:00
|
|
|
auto dst_view = emscripten::val(typed_memory_view(len, data));
|
|
|
|
dst_view.call<void>("set", src);
|
|
|
|
} else {
|
|
|
|
for (size_t i = 0; i < len; ++i) {
|
|
|
|
data[i] = src[i].as<T>();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fSpan = SkSpan(data, len);
|
2021-05-15 00:55:58 +00:00
|
|
|
}
|
2021-05-18 15:01:12 +00:00
|
|
|
|
2021-06-02 12:38:04 +00:00
|
|
|
JSSpan(WASMPointer ptr, size_t len, bool takeOwnership): fOwned(takeOwnership) {
|
|
|
|
fSpan = SkSpan(reinterpret_cast<T*>(ptr), len);
|
|
|
|
}
|
|
|
|
|
2021-05-18 15:01:12 +00:00
|
|
|
~JSSpan() {
|
|
|
|
if (fOwned) {
|
2021-05-23 20:32:32 +00:00
|
|
|
sk_free(fSpan.data());
|
2021-05-18 15:01:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const T* data() const { return fSpan.data(); }
|
|
|
|
size_t size() const { return fSpan.size(); }
|
|
|
|
|
|
|
|
private:
|
|
|
|
SkSpan<T> fSpan;
|
|
|
|
bool fOwned;
|
|
|
|
};
|
2021-05-15 00:55:58 +00:00
|
|
|
|
2020-03-26 13:27:48 +00:00
|
|
|
#endif
|