-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathskia_bindings.cpp
347 lines (301 loc) · 13.3 KB
/
skia_bindings.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
#include <iostream>
#include "skia.h"
#include "include/ports/SkFontMgr.h"
#include <GL/gl.h>
#include <emscripten.h>
#include <emscripten/html5.h>
// Hack to avoid embind creating a binding for SkData destructor
namespace emscripten {
namespace internal {
template<typename ClassType>
void raw_destructor(ClassType *);
template<>
void raw_destructor<SkData>(SkData *ptr) {
}
}
}
// Hack to avoid embind creating a binding for SkTypeface destructor
namespace emscripten {
namespace internal {
template<typename ClassType>
void raw_destructor(ClassType *);
template<>
void raw_destructor<SkTypeface>(SkTypeface *ptr) {
}
}
}
#include <emscripten/bind.h>
using namespace emscripten;
using emscripten::val;
using emscripten::vecFromJSArray;
// to map from raw memory to a uint8array
val getSkDataBytes(const SkData *data) {
return val(typed_memory_view(data->size(), data->bytes()));
}
sk_sp<SkSurface> makeWebGLSurface(std::string id, int width, int height) {
// Context configurations
EmscriptenWebGLContextAttributes attrs;
emscripten_webgl_init_context_attributes(&attrs);
attrs.alpha = true;
attrs.premultipliedAlpha = true;
attrs.majorVersion = 1;
attrs.enableExtensionsByDefault = true;
EMSCRIPTEN_WEBGL_CONTEXT_HANDLE context = emscripten_webgl_create_context(id.c_str(), &attrs);
if (context < 0) {
printf("failed to create webgl context %d\n", context);
}
EMSCRIPTEN_RESULT r = emscripten_webgl_make_context_current(context);
if (r < 0) {
printf("failed to make webgl current %d\n", r);
}
glViewport(0, 0, width, height);
glClearColor(1, 1, 1, 1);
glClearStencil(0);
glClear(GL_COLOR_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
// setup GrContext
auto interface = GrGLMakeNativeInterface();
// setup contexts
sk_sp<GrContext> grContext(GrContext::MakeGL(interface));
// Wrap the frame buffer object attached to the screen in a Skia render target so Skia can
// render to it
GrGLint buffer;
glGetIntegerv(GL_FRAMEBUFFER_BINDING, &buffer);
GrGLFramebufferInfo info;
info.fFBOID = (GrGLuint) buffer;
SkColorType colorType;
info.fFormat = GL_RGBA8;
colorType = kRGBA_8888_SkColorType;
GrBackendRenderTarget target(width, height, 0, 8, info);
SkSurfaceProps props(SkSurfaceProps::kLegacyFontHost_InitType);
sk_sp<SkSurface> surface(SkSurface::MakeFromBackendRenderTarget(grContext.get(), target,
kBottomLeft_GrSurfaceOrigin,
colorType, nullptr, &props));
return surface;
}
EMSCRIPTEN_BINDINGS(skia_module) {
// register array types, used by various functions to provide arrays as arguments functionality
register_vector<SkPoint>("VectorSkPoint");
register_vector<SkColor>("VectorSkColor");
register_vector<SkScalar>("VectorSkScalar");
function("getSkDataBytes", &getSkDataBytes, allow_raw_pointers());
function("GrGLMakeAssembledInterface", &GrGLMakeAssembledInterface, allow_raw_pointers());
function("makeWebGLSurface", &makeWebGLSurface, allow_raw_pointers());
// GrContext.h ->
class_<GrGLInterface>("GrGLInterface");
// GrContext.h ^
// SkSurfaceProps.h ->
class_<SkSurfaceProps>("SkSurfaceProps")
.constructor<uint32_t, SkPixelGeometry>()
.function("flags", &SkSurfaceProps::flags);
enum_<SkPixelGeometry>("SkPixelGeometry")
.value("kUnknown_SkPixelGeometry", SkPixelGeometry::kUnknown_SkPixelGeometry)
.value("kRGB_H_SkPixelGeometry", SkPixelGeometry::kRGB_H_SkPixelGeometry)
.value("kBGR_H_SkPixelGeometry", SkPixelGeometry::kBGR_H_SkPixelGeometry)
.value("kRGB_V_SkPixelGeometry", SkPixelGeometry::kRGB_V_SkPixelGeometry)
.value("kBGR_V_SkPixelGeometry", SkPixelGeometry::kBGR_V_SkPixelGeometry);
enum_<SkSurfaceProps::Flags>("SkSurfaceProps.Flags")
.value("kUseDeviceIndependentFonts_Flag", SkSurfaceProps::Flags::kUseDeviceIndependentFonts_Flag);
EM_ASM(
Module['SkSurfaceProps']['Flags'] = Module['SkSurfaceProps.Flags'];
delete Module['SkSurfaceProps.Flags'];
);
// SkSurfaceProps.h ^
// SkBitmap.h ->
class_<SkBitmap>("SkBitmap");
// SkBitmap.h ^
// SkImageInfo.h ->
class_<SkImageInfo>("SkImageInfo")
.constructor<>();
// SkImageInfo.h ^
// SkData.h ->
class_<SkData>("SkData")
.smart_ptr<sk_sp<SkData>>("sk_sp<SkData>>")
.class_function("MakeFromMalloc",
optional_override([](std::string data, size_t length)->sk_sp<SkData>{
return SkData::MakeFromMalloc(&data[0], length);
}),
allow_raw_pointers())
.class_function("MakeUninitialized", &SkData::MakeUninitialized)
.function("size", &SkData::size);
// SkData.h ^
// SkPoint.h ->
class_<SkPoint>("SkPoint")
.class_function("Make", &SkPoint::Make)
.property("fX", &SkPoint::fX)
.property("fY", &SkPoint::fY);
// SkPoint.h ^
// SkString.h ->
class_<SkString>("SkString")
.constructor<>()
.function("c_str",
optional_override([](SkString& this_)->std::string{
return std::string(this_.SkString::c_str());
})
);
// SkString.h ^
//SkImage.h ->
class_<SkImage>("SkImage")
.smart_ptr<sk_sp<SkImage>>("sk_sp<SkImage>")
.function("encodeToData", select_overload<sk_sp<SkData>()const>(&SkImage::encodeToData));
//SkImage.h ^
// SkPath.h ->
class_<SkPath>("SkPath")
.constructor<>()
.constructor<const SkPath&>()
.function("setFillType",&SkPath::setFillType)
.function("moveToXY",
select_overload<void(SkScalar, SkScalar)>(&SkPath::moveTo))
.function("moveToPoint",
select_overload<void(const SkPoint& p)>(&SkPath::moveTo))
.function("lineToXY",
select_overload<void(SkScalar, SkScalar)>(&SkPath::lineTo))
.function("lineToPoint",
select_overload<void(const SkPoint& p)>(&SkPath::lineTo))
.function("close", &SkPath::close);
enum_<SkPath::FillType>("SkPath.FillType")
.value("kWinding_FillType", SkPath::FillType::kWinding_FillType)
.value("kEvenOdd_FillType", SkPath::FillType::kEvenOdd_FillType)
.value("kInverseWinding_FillType", SkPath::FillType::kInverseWinding_FillType)
.value("kInverseEvenOdd_FillType", SkPath::FillType::kInverseEvenOdd_FillType);
EM_ASM(
Module['SkPath']['FillType'] = Module['SkPath.FillType'];
delete Module['SkPath.FillType'];
);
// SkPath.h ^
// SkFontStyle.h ->
class_<SkFontStyle>("SkFontStyle")
.constructor<>()
.class_function("Normal", &SkFontStyle::Normal)
.class_function("Bold", &SkFontStyle::Bold)
.class_function("Italic", &SkFontStyle::Italic)
.class_function("BoldItalic", &SkFontStyle::BoldItalic);
// SkFontStyle ^
// SkTypeFace ->
class_<SkTypeface>("SkTypeface")
.smart_ptr<sk_sp<SkTypeface>>("sk_sp<SkTypeface>")
.class_function("MakeDefault", &SkTypeface::MakeDefault)
.class_function("MakeFromFile",
optional_override([](std::string path, int index)->sk_sp<SkTypeface>{
return SkTypeface::MakeFromFile(path.c_str(), index);
}))
.class_function("MakeFromName",
optional_override([](std::string familyName, SkFontStyle fontStyle)->sk_sp<SkTypeface>{
return SkTypeface::MakeFromName(familyName.c_str(), fontStyle);
}))
.function("countGlyphs", &SkTypeface::countGlyphs);
// SkTypeFace ^
// SkFontMgr.h ->
class_<SkFontMgr>("SkFontMgr")
.smart_ptr<sk_sp<SkFontMgr>>("sk_sp<SkFontMgr>")
.class_function("RefDefault", &SkFontMgr::RefDefault)
.function("countFamilies", &SkFontMgr::countFamilies)
.function("getFamilyName", &SkFontMgr::getFamilyName, allow_raw_pointers())
.function("makeFromData", &SkFontMgr::makeFromData)
.function("matchFaceStyle", &SkFontMgr::matchFaceStyle, allow_raw_pointers())
.function("makeFromFile",
optional_override([](SkFontMgr& this_, std::string path, int ttcIndex)->sk_sp<SkTypeface>{
return this_.SkFontMgr::makeFromFile(path.c_str(), ttcIndex);
}));
// SkFontMgr.h ^
// SkPaint.h ->
class_<SkPaint>("SkPaint")
.constructor<>()
.function("setAntiAlias", &SkPaint::setAntiAlias)
.function("setTextSize", &SkPaint::setTextSize)
.function("setColor", &SkPaint::setColor)
.function("setStyle", &SkPaint::setStyle)
.function("setStrokeWidth", &SkPaint::setStrokeWidth)
.function("setTextScaleX", &SkPaint::setTextScaleX)
.function("getTypeface", &SkPaint::getTypeface, allow_raw_pointers())
.function("setTypeface", &SkPaint::setTypeface)
.function("setShader", &SkPaint::setShader);
enum_<SkPaint::Style>("SkPaint.Style")
.value("kFill_Style", SkPaint::Style::kFill_Style)
.value("kStroke_Style", SkPaint::Style::kStroke_Style)
.value("kStrokeAndFill_Style", SkPaint::Style::kStrokeAndFill_Style);
EM_ASM(
Module['SkPaint']['Style'] = Module['SkPaint.Style'];
delete Module['SkPaint.Flags'];
);
// SkPaint.h ^
// SkCanvas.h ->
class_<SkCanvas>("SkCanvas")
.constructor<>()
.function("flush", &SkCanvas::flush)
.function("clear", &SkCanvas::clear)
.function("translate", &SkCanvas::translate)
.function("drawPath", &SkCanvas::drawPath)
.function("save", &SkCanvas::save)
.function("restore", &SkCanvas::restore)
.function("drawRect", &SkCanvas::drawRect)
.function("rotate",
select_overload<void(SkScalar)>(&SkCanvas::rotate),
allow_raw_pointers())
.function("drawImage",
select_overload<void(const sk_sp<SkImage>&,SkScalar,SkScalar,const SkPaint*)>(&SkCanvas::drawImage),
allow_raw_pointers())
.function("drawText",
// we wrap the real function in a lambda to do the conversion of the js string (represented as an std:string) to a c string
optional_override([](SkCanvas& this_, const std::string text, SkScalar x, SkScalar y, const SkPaint& p){
return this_.SkCanvas::drawText(text.c_str(), text.length(), x, y, p);
}),
allow_raw_pointers())
.function("drawPaint", &SkCanvas::drawPaint);
// SkCanvas ^
// SkSurface.h ->
class_<SkSurface>("SkSurface")
.smart_ptr<sk_sp<SkSurface>>("sk_sp<SkSurface>")
.class_function("MakeRasterDirect", &SkSurface::MakeRasterDirect, allow_raw_pointers())
.class_function("MakeRasterN32Premul", &SkSurface::MakeRasterN32Premul, allow_raw_pointers())
.class_function("MakeRasterAutoRow",
select_overload<sk_sp<SkSurface>(const SkImageInfo&, const SkSurfaceProps*)>(&SkSurface::MakeRaster),
allow_raw_pointers())
.function("width", &SkSurface::width)
.function("height", &SkSurface::height)
.function("getCanvas", &SkSurface::getCanvas, allow_raw_pointers())
.function("makeImageSnapshot", &SkSurface::makeImageSnapshot);
// SkSurface.h ^
// SkShader.h ->
class_<SkShader>("SkShader")
.smart_ptr<sk_sp<SkShader>>("sk_sp<SkShader>");
enum_<SkShader::TileMode>("SkShader.TileMode")
.value("kClamp_TileMode", SkShader::TileMode::kClamp_TileMode)
.value("kRepeat_TileMode", SkShader::TileMode::kRepeat_TileMode)
.value("kMirror_TileMode", SkShader::TileMode::kMirror_TileMode);
EM_ASM(
Module['SkShader']['TileMode'] = Module['SkShader.TileMode'];
delete Module['SkShader.TileMode'];
);
// SkShader.h ^
// SkGradientShader.h ->
class_<SkGradientShader>("SkGradientShader")
.class_function("MakeLinear",
optional_override([](const std::vector<SkPoint> pts, const std::vector<SkColor> colors, const std::vector<SkScalar> pos, int count, SkShader::TileMode mode)->sk_sp<SkShader>{
const SkPoint *ptsPtr = pts.data();
const SkColor *colorsPtr = colors.data();
const SkScalar *posPtr = pos.data();
return SkGradientShader::MakeLinear(ptsPtr, colorsPtr, posPtr, count, mode);
}),
allow_raw_pointers());
// SkGradientShader.h ^
// SkMatrix.h ->
class_<SkMatrix>("SkMatrix")
.constructor<>()
.function("setRotate",
select_overload<void(SkScalar)>(&SkMatrix::setRotate))
.function("mapPoints",
select_overload<void(SkPoint[],const SkPoint[],int)const>(&SkMatrix::mapPoints),
allow_raw_pointers());
// SkMatrix.h ^
// SkRandom.h ->
class_<SkRandom>("SkRandom")
.constructor<>()
.function("nextU",&SkRandom::nextU);
// SkRandom.h ^
// SkRect.h ->
class_<SkRect>("SkRect")
.class_function("MakeLTRB", &SkRect::MakeLTRB, allow_raw_pointers())
.property("fRight",&SkRect::fRight)
.property("fBottom",&SkRect::fBottom);
// SkRect.h ^
}