-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathwander_lib.h
497 lines (391 loc) · 10.4 KB
/
wander_lib.h
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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
#pragma once
#include "wander.h"
#include <fstream>
#include <iostream>
#include <ostream>
#include <queue>
#include <utility>
#include <memory>
#ifndef __EMSCRIPTEN__
// TODO - this should only be a private dependency
// Required for wasmtime (missing header)
#include <string>
#include "wasmtime.h"
#endif
namespace rive
{
namespace pls
{
class PLSRenderContext;
}
}
struct ID3D11Device;
struct ID3D11DeviceContext;
struct ID3D11Buffer;
struct ID3D11Texture2D;
struct ID3D11ShaderResourceView;
struct IDXGISwapChain;
struct IDXGISwapChain1;
typedef unsigned int GLuint;
namespace wander
{
class CByteArrayStreambuf final : public std::streambuf
{
public:
CByteArrayStreambuf(const uint8_t *begin, size_t size);
~CByteArrayStreambuf() override = default;
// copying not allowed
CByteArrayStreambuf(const CByteArrayStreambuf &) = delete;
CByteArrayStreambuf &operator=(const CByteArrayStreambuf &) = delete;
CByteArrayStreambuf(CByteArrayStreambuf &&) = delete;
CByteArrayStreambuf &operator=(CByteArrayStreambuf &&) = delete;
private:
int_type underflow() override;
int_type uflow() override;
int_type pbackfail(int_type ch) override;
std::streamsize showmanyc() override;
std::streampos seekoff(std::streamoff off, std::ios_base::seekdir way,
std::ios_base::openmode which = std::ios_base::in | std::ios_base::out) override;
std::streampos seekpos(std::streampos sp,
std::ios_base::openmode which = std::ios_base::in | std::ios_base::out) override;
const uint8_t *const m_begin;
const uint8_t *const m_end;
const uint8_t *m_current;
};
class IBinaryStream
{
protected:
explicit IBinaryStream(std::iostream &stream) : m_stream(stream)
{
}
public:
template <typename T>
IBinaryStream &operator>>(T &val)
{
m_stream.read(reinterpret_cast<char *>(&val), sizeof(val));
return *this;
}
template <typename T>
T ReadType()
{
T t{};
m_stream.read(reinterpret_cast<char *>(&t), sizeof(t));
return t;
}
template <typename T>
IBinaryStream &operator<<(const T &val)
{
m_stream.write(reinterpret_cast<const char *>(&val), sizeof(val));
return *this;
}
IBinaryStream &Read(void *val, int size)
{
m_stream.read(static_cast<char *>(val), size);
return *this;
}
IBinaryStream &Write(const void *val, int size)
{
m_stream.write(static_cast<const char *>(val), size);
return *this;
}
[[nodiscard]] std::string ReadString(int count) const
{
const auto bytes = std::unique_ptr<char[]>(new char[count]);
m_stream.read(bytes.get(), count);
return std::string(bytes.get(), count);
}
[[nodiscard]] std::string ReadCString() const
{
std::string buffer;
std::getline(m_stream, buffer, '\0');
return buffer;
}
bool operator!() const
{
return !m_stream;
}
explicit operator bool() const
{
return m_stream.operator bool();
}
[[nodiscard]] int GetPosition() const
{
// This is a problem with ReSharper - no issue here
// ReSharper disable once CppRedundantCastExpression
return static_cast<int>(m_stream.tellg());
}
void SetPosition(int pos, std::ios_base::seekdir dir = std::fstream::beg) const
{
m_stream.seekg(pos, dir);
m_stream.seekp(pos, dir);
}
protected:
std::iostream &m_stream;
};
class BinaryMemoryStream final : public IBinaryStream
{
public:
explicit BinaryMemoryStream(uint8_t *pointer, int length) :
IBinaryStream(m_iostream), m_buf(pointer, length), m_iostream(&m_buf), m_pointer(pointer),
m_length(length)
{
}
[[nodiscard]] uint8_t *GetPointer() const
{
return m_pointer;
}
[[nodiscard]] int GetLength() const
{
return m_length;
}
private:
CByteArrayStreambuf m_buf;
std::iostream m_iostream;
uint8_t *m_pointer;
int m_length;
};
class VectorLoader
{
public:
enum class EPathType
{
Move = 1,
Line = 2,
Quad = 3,
Cubic = 4,
Close = 5
};
struct Point
{
float x;
float y;
};
enum class RenderPaintStyle
{
stroke,
fill
};
enum class StrokeJoin : unsigned int
{
/// Makes a sharp corner at the joint.
miter = 0,
/// Smoothens the joint with a circular/semi-circular shape at the
/// joint.
round = 1,
/// Creates a beveled edge at the joint.
bevel = 2
};
enum class StrokeCap : unsigned int
{
/// Flat edge at the start/end of the stroke.
butt = 0,
/// Circular edge at the start/end of the stroke.
round = 1,
/// Flat protruding edge at the start/end of the stroke.
square = 2
};
enum class BlendMode : unsigned char
{
srcOver = 3,
screen = 14,
overlay = 15,
darken = 16,
lighten = 17,
colorDodge = 18,
colorBurn = 19,
hardLight = 20,
softLight = 21,
difference = 22,
exclusion = 23,
multiply = 24,
hue = 25,
saturation = 26,
color = 27,
luminosity = 28
};
struct RenderPaint
{
RenderPaintStyle style;
uint32_t color;
float thickness;
StrokeJoin join;
StrokeCap cap;
BlendMode blendMode;
};
typedef std::tuple<EPathType, std::vector<Point>> Path;
typedef std::vector<Path> PathList;
typedef std::tuple<RenderPaint, PathList> Command;
static auto Read(uint8_t *p_data, uint32_t length) -> std::vector<Command>
{
BinaryMemoryStream ms(p_data, length);
std::vector<Command> command_list;
while (ms)
{
RenderPaint p{};
ms >> p;
int path_size = 0;
ms >> path_size;
if (path_size == 0)
break;
PathList path_list(path_size);
for (auto& path : path_list)
{
EPathType type;
ms >> type;
int points_size = 0;
ms >> points_size;
std::vector<Point> points(points_size);
for (auto& point: points)
{
ms >> point.x;
ms >> point.y;
}
std::get<0>(path) = type;
std::get<1>(path) = points;
}
command_list.emplace_back(p, path_list);
}
return command_list;
}
std::vector<Command> m_command_list;
};
class Pal : public IPal
{
public: // TODO: Replace with std::span
virtual ObjectID CreateBuffer(BufferDescriptor desc, int length, uint8_t data[]) = 0;
virtual ObjectID CreateTexture(BufferDescriptor desc, int length, uint8_t data[]) = 0;
virtual void DeleteBuffer(ObjectID buffer_id) = 0;
virtual ObjectID CreateVector(int length, uint8_t data[]) = 0;
virtual ObjectID UpdateVector(int length, uint8_t data[], ObjectID buffer_id) = 0;
virtual void DrawTriangleList(ObjectID buffer_id, int offset, int length, unsigned int stride) = 0;
virtual void DrawVector(ObjectID buffer_id, int slot, int width, int height) = 0;
};
#ifdef _WIN32
class PalD3D11 : public Pal
{
public:
PalD3D11(ID3D11Device* device, ID3D11DeviceContext* context);
PalD3D11(ID3D11Device* device, ID3D11DeviceContext* context, IDXGISwapChain* swapchain);
virtual ~PalD3D11();
void Release() override{};
EPalType Type() override
{
return EPalType::D3D11;
}
ObjectID CreateBuffer(BufferDescriptor desc, int length, uint8_t data[]) override;
ObjectID CreateTexture(BufferDescriptor desc, int length, uint8_t data[]) override;
void DeleteBuffer(ObjectID buffer_id) override;
ObjectID CreateVector(int length, uint8_t data[]) override;
ObjectID UpdateVector(int length, uint8_t data[], ObjectID buffer_id) override;
void DrawTriangleList(ObjectID buffer_id, int offset, int length, unsigned int stride) override;
void DrawVector(ObjectID buffer_id, int slot, int width, int height) override;
private:
std::vector<ID3D11Buffer*> m_buffers;
std::vector<ID3D11Texture2D*> m_textures;
ID3D11Device* m_device;
ID3D11DeviceContext* m_device_context;
IDXGISwapChain *m_swapchain;
#if defined(RLT_RIVE) && defined(_WIN32)
std::unique_ptr<rive::pls::PLSRenderContext> m_plsContext;
std::vector<std::vector<VectorLoader::Command>> m_vector_commands;
std::vector<ID3D11Texture2D *> m_vector_textures;
std::vector<ID3D11ShaderResourceView*> m_vector_srvs;
#endif
};
#endif
class PalOpenGL : public Pal
{
public:
PalOpenGL(void* context) : m_context(context) {}
void Release() override{};
EPalType Type() override
{
return EPalType::OpenGL;
}
ObjectID CreateBuffer(BufferDescriptor desc, int length, uint8_t data[]) override;
ObjectID CreateTexture(BufferDescriptor desc, int length, uint8_t data[]) override;
void DeleteBuffer(ObjectID buffer_id) override;
ObjectID CreateVector(int length, uint8_t data[]) override;
ObjectID UpdateVector(int length, uint8_t data[], ObjectID buffer_id) override;
void DrawTriangleList(ObjectID buffer_id, int offset, int length, unsigned int stride) override;
void DrawVector(ObjectID buffer_id, int slot, int width, int height) override;
private:
std::vector<GLuint> m_vbos;
std::vector<GLuint> m_vaos;
std::vector<GLuint> m_texs;
void *m_context;
};
class Runtime : public IRuntime
{
public:
struct Param
{
enum
{
Int32,
Int64,
Float32,
Float64
} Type;
union
{
uint32_t I32;
uint64_t I64;
float F32;
double F64;
} Value;
};
Runtime(Pal *pal) : m_pal(pal)
{
}
ObjectID LoadFromFile(const std::wstring &path) override;
ObjectID LoadFromFile(const std::wstring &path, const std::string& function) override;
void PushParam(ObjectID renderlet_id, float value) override;
void PushParam(ObjectID renderlet_id, double value) override;
void PushParam(ObjectID renderlet_id, uint32_t value) override;
void PushParam(ObjectID renderlet_id, uint64_t value) override;
void ResetStack(ObjectID renderlet_id) override;
ObjectID Render(ObjectID renderlet_id, ObjectID tree_id = -1, bool pool = false) override;
const float* const ExecuteFloat4(ObjectID renderlet_id, const std::string& function) override;
// void UploadBufferPool(ObjectID pool_id);
void UploadBufferPool(unsigned int stride) override;
const RenderTree *GetRenderTree(ObjectID tree_id) override;
void DestroyRenderTree(ObjectID tree_id) override;
void Release() override;
Pal* PalImpl() const
{
return m_pal;
}
private:
ObjectID BuildVector(uint32_t length, uint8_t *data, ObjectID tree_id);
void CreatePooledBuffer(uint32_t length, uint8_t *data, ObjectID tree_id);
#ifndef __EMSCRIPTEN__
struct WasmtimeContext
{
wasm_engine_t* Engine = nullptr;
wasmtime_store_t* Store = nullptr;
wasmtime_context_t* Context = nullptr;
wasmtime_linker_t* Linker = nullptr;
wasmtime_module_t* Module = nullptr;
wasmtime_extern_t Run {};
wasmtime_extern_t Memory {};
};
wasm_engine_t *m_engine = nullptr;
#else
struct WasmtimeContext{};
int m_context_count = 0;
#endif
struct SubBuffer
{
int offset;
uint32_t length;
ObjectID tree_id;
};
std::vector<SubBuffer> m_sub_buffers;
std::unique_ptr<unsigned char[]> m_staging_buffer;
std::vector<WasmtimeContext> m_contexts;
std::vector<std::queue<Param>> m_params;
std::vector<std::unique_ptr<RenderTree>> m_render_trees;
Pal* m_pal;
};
}