-
Notifications
You must be signed in to change notification settings - Fork 36
/
renderer.hpp
220 lines (190 loc) · 5.64 KB
/
renderer.hpp
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
/*
* Copyright 2022 Rive
*/
#ifndef _RIVE_RENDERER_HPP_
#define _RIVE_RENDERER_HPP_
#include "rive/enum_bitset.hpp"
#include "rive/shapes/paint/color.hpp"
#include "rive/command_path.hpp"
#include "rive/layout.hpp"
#include "rive/refcnt.hpp"
#include "rive/math/aabb.hpp"
#include "rive/math/mat2d.hpp"
#include "rive/shapes/paint/blend_mode.hpp"
#include "rive/shapes/paint/stroke_cap.hpp"
#include "rive/shapes/paint/stroke_join.hpp"
#include "utils/lite_rtti.hpp"
#include <stdio.h>
#include <cstdint>
namespace rive
{
class Vec2D;
// Helper that computes a matrix to "align" content (source) to fit inside frame
// (destination).
Mat2D computeAlignment(Fit,
Alignment,
const AABB& frame,
const AABB& content,
const float scaleFactor = 1.0f);
enum class RenderBufferType
{
index,
vertex,
};
enum class RenderBufferFlags
{
none = 0,
mappedOnceAtInitialization =
1 << 0, // The client will map the buffer exactly one time, before
// rendering, and will never update it again.
};
RIVE_MAKE_ENUM_BITSET(RenderBufferFlags)
class RenderBuffer : public RefCnt<RenderBuffer>,
public ENABLE_LITE_RTTI(RenderBuffer)
{
public:
RenderBuffer(RenderBufferType, RenderBufferFlags, size_t sizeInBytes);
virtual ~RenderBuffer();
RenderBufferType type() const { return m_type; }
RenderBufferFlags flags() const { return m_flags; }
size_t sizeInBytes() const { return m_sizeInBytes; }
void* map();
void unmap();
protected:
virtual void* onMap() = 0;
virtual void onUnmap() = 0;
// Unset the dirty flag, and return whether it had been set.
bool checkAndResetDirty()
{
assert(m_mapCount == m_unmapCount); // Don't call this while mapped.
if (m_dirty)
{
m_dirty = false;
return true;
}
return false;
}
private:
const RenderBufferType m_type;
const RenderBufferFlags m_flags;
const size_t m_sizeInBytes;
bool m_dirty = false;
RIVE_DEBUG_CODE(size_t m_mapCount = 0;)
RIVE_DEBUG_CODE(size_t m_unmapCount = 0;)
};
enum class RenderPaintStyle
{
stroke,
fill
};
/*
* Base class for Render objects that specify the src colors.
*
* Shaders are immutable, and sharable between multiple paints, etc.
*
* It is common that a shader may be created with a 'localMatrix'. If this is
* not null, then it is applied to the shader's domain before the Renderer's
* CTM.
*/
class RenderShader : public RefCnt<RenderShader>,
public ENABLE_LITE_RTTI(RenderShader)
{
public:
RenderShader();
virtual ~RenderShader();
};
class RenderPaint : public RefCnt<RenderPaint>,
public ENABLE_LITE_RTTI(RenderPaint)
{
public:
RenderPaint();
virtual ~RenderPaint();
virtual void style(RenderPaintStyle style) = 0;
virtual void color(ColorInt value) = 0;
virtual void thickness(float value) = 0;
virtual void join(StrokeJoin value) = 0;
virtual void cap(StrokeCap value) = 0;
virtual void blendMode(BlendMode value) = 0;
virtual void shader(rcp<RenderShader>) = 0;
virtual void invalidateStroke() = 0;
};
#if defined(__EMSCRIPTEN__)
class RenderImageDelegate
{
public:
virtual void decodedAsync() = 0;
};
#endif
class RenderImage : public RefCnt<RenderImage>,
public ENABLE_LITE_RTTI(RenderImage)
{
protected:
int m_Width = 0;
int m_Height = 0;
Mat2D m_uvTransform;
public:
RenderImage();
RenderImage(const Mat2D& uvTransform);
virtual ~RenderImage();
int width() const { return m_Width; }
int height() const { return m_Height; }
const Mat2D& uvTransform() const { return m_uvTransform; }
#if defined(__EMSCRIPTEN__)
void delegate(RenderImageDelegate* delegate) { m_delegate = delegate; }
void decodedAsync() const
{
if (m_delegate != nullptr)
{
m_delegate->decodedAsync();
}
}
private:
RenderImageDelegate* m_delegate = nullptr;
#endif
};
class RenderPath : public CommandPath, public ENABLE_LITE_RTTI(RenderPath)
{
public:
RenderPath();
~RenderPath() override;
RenderPath* renderPath() override { return this; }
void addPath(CommandPath* path, const Mat2D& transform) override
{
addRenderPath(path->renderPath(), transform);
}
virtual void addRenderPath(RenderPath* path, const Mat2D& transform) = 0;
};
class Renderer
{
public:
virtual ~Renderer() {}
virtual void save() = 0;
virtual void restore() = 0;
virtual void transform(const Mat2D& transform) = 0;
virtual void drawPath(RenderPath* path, RenderPaint* paint) = 0;
virtual void clipPath(RenderPath* path) = 0;
virtual void drawImage(const RenderImage*, BlendMode, float opacity) = 0;
virtual void drawImageMesh(const RenderImage*,
rcp<RenderBuffer> vertices_f32,
rcp<RenderBuffer> uvCoords_f32,
rcp<RenderBuffer> indices_u16,
uint32_t vertexCount,
uint32_t indexCount,
BlendMode,
float opacity) = 0;
// helpers
void translate(float x, float y);
void scale(float sx, float sy);
void rotate(float radians);
void align(Fit fit,
Alignment alignment,
const AABB& frame,
const AABB& content,
const float scaleFactor = 1.0f)
{
transform(
computeAlignment(fit, alignment, frame, content, scaleFactor));
}
};
} // namespace rive
#endif