-
Notifications
You must be signed in to change notification settings - Fork 47
Expand file tree
/
Copy pathrender_context.cpp
More file actions
434 lines (392 loc) · 14.9 KB
/
Copy pathrender_context.cpp
File metadata and controls
434 lines (392 loc) · 14.9 KB
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
#include "render/render_context.h"
#include "core/log.h"
#include "core/resource_paths.h"
#include "core/ui_phase.h"
#include "render/backend/render_backend.h"
#include "render/core/texture_handle.h"
#include "render/core/texture_manager.h"
#include "render/core/wallpaper_types.h"
#include "render/gl_shared_context.h"
#include "render/render_target.h"
#include "render/scene/glyph_node.h"
#include "render/scene/image_node.h"
#include "render/scene/node.h"
#include "render/scene/rect_node.h"
#include "render/scene/text_node.h"
#include "render/scene/wallpaper_node.h"
#include <algorithm>
#include <chrono>
#include <cmath>
#include <cstdint>
#include <format>
#include <optional>
#include <stdexcept>
#include <utility>
#include <vector>
namespace {
constexpr Logger kLog("render");
constexpr float kSlowRenderOperationDebugMs = 50.0f;
constexpr float kSlowRenderOperationWarnMs = 1000.0f;
} // namespace
namespace {
float elapsedSince(std::chrono::steady_clock::time_point start) {
return std::chrono::duration<float, std::milli>(std::chrono::steady_clock::now() - start).count();
}
template <typename... Args> void logSlowRenderOperation(float ms, std::format_string<Args...> fmt, Args&&... args) {
if (ms >= kSlowRenderOperationWarnMs) {
kLog.warn(fmt, std::forward<Args>(args)...);
} else if (ms >= kSlowRenderOperationDebugMs) {
kLog.debug(fmt, std::forward<Args>(args)...);
}
}
RenderScissor
scissorForClip(float sw, float sh, float bw, float bh, float left, float top, float right, float bottom) {
const float scaleX = sw > 0.0f ? bw / sw : 1.0f;
const float scaleY = sh > 0.0f ? bh / sh : 1.0f;
const auto clampX = [bw](std::int32_t value) {
return std::clamp(value, std::int32_t{0}, static_cast<std::int32_t>(std::ceil(bw)));
};
const auto clampY = [bh](std::int32_t value) {
return std::clamp(value, std::int32_t{0}, static_cast<std::int32_t>(std::ceil(bh)));
};
// Round clip edges independently in buffer space. Rounding the size after
// flooring the origin can drop the final column/row for fractional origins.
const std::int32_t x0 = clampX(static_cast<std::int32_t>(std::floor(left * scaleX)));
const std::int32_t x1 = clampX(static_cast<std::int32_t>(std::ceil(right * scaleX)));
const std::int32_t y0 = clampY(static_cast<std::int32_t>(std::floor((sh - bottom) * scaleY)));
const std::int32_t y1 = clampY(static_cast<std::int32_t>(std::ceil((sh - top) * scaleY)));
return RenderScissor{
.x = x0,
.y = y0,
.width = std::max(std::int32_t{0}, x1 - x0),
.height = std::max(std::int32_t{0}, y1 - y0),
};
}
Mat3 nodeLocalTransform(const Node* node) {
const float cx = node->width() * 0.5f;
const float cy = node->height() * 0.5f;
return Mat3::translation(node->x(), node->y())
* Mat3::translation(cx, cy)
* Mat3::rotation(node->rotation())
* Mat3::scale(node->scale(), node->scale())
* Mat3::translation(-cx, -cy);
}
} // namespace
RenderContext::RenderContext() = default;
RenderContext::~RenderContext() { cleanup(); }
void RenderContext::initialize(GlSharedContext& shared) {
cleanup();
m_backend = createDefaultRenderBackend();
m_backend->initialize(shared);
// Fontconfig handles font fallback via Pango.
m_backend->textureManager().probeExtensions();
m_textRenderer.initialize(m_backend.get(), &m_backend->textureManager());
m_glyphRenderer.initialize(
paths::assetPath("fonts/tabler.ttf").string(), m_backend.get(), &m_backend->textureManager()
);
m_textFontFamily = "sans-serif";
++m_textMetricsGeneration;
}
void RenderContext::makeCurrentNoSurface() {
if (m_backend == nullptr) {
return;
}
m_backend->makeCurrentNoSurface();
}
void RenderContext::makeCurrent(RenderTarget& target) {
if (m_backend == nullptr) {
throw std::runtime_error("RenderContext has no initialized backend");
}
m_backend->makeCurrent(target);
// Shared text/glyph renderers must match this target scale on every
// makeCurrent.
syncContentScale(target);
}
void RenderContext::syncContentScale(RenderTarget& target) {
const auto sw = static_cast<float>(target.logicalWidth());
const auto bw = static_cast<float>(target.bufferWidth());
m_renderScale = sw > 0.0f ? std::max(1.0f, bw / sw) : 1.0f;
m_textRenderer.setContentScale(m_renderScale);
m_glyphRenderer.setContentScale(m_renderScale);
}
void RenderContext::setTextFontFamily(std::string family) {
if (family.empty()) {
family = "sans-serif";
}
if (m_textFontFamily == family) {
return;
}
makeCurrentNoSurface();
m_textFontFamily = std::move(family);
m_textRenderer.setFontFamily(m_textFontFamily);
++m_textMetricsGeneration;
}
void RenderContext::notifyFontConfigChanged() {
m_textRenderer.notifyFontConfigChanged();
++m_textMetricsGeneration;
}
void RenderContext::renderScene(RenderTarget& target, Node* sceneRoot) {
UiPhaseScope renderPhase(UiPhase::Render);
if (m_backend == nullptr) {
return;
}
const auto totalStart = std::chrono::steady_clock::now();
m_backend->beginFrame(target);
syncContentScale(target);
const auto drawStart = std::chrono::steady_clock::now();
if (sceneRoot != nullptr) {
const auto sw = static_cast<float>(target.logicalWidth());
const auto sh = static_cast<float>(target.logicalHeight());
const auto bw = static_cast<float>(target.bufferWidth());
const auto bh = static_cast<float>(target.bufferHeight());
renderNode(sceneRoot, Mat3::identity(), 1.0f, sw, sh, bw, bh, 0.0f, 0.0f, sw, sh, false);
}
float ms = elapsedSince(drawStart);
logSlowRenderOperation(
ms, "scene draw took {:.1f}ms ({}x{} logical, {}x{} buffer)", ms, target.logicalWidth(), target.logicalHeight(),
target.bufferWidth(), target.bufferHeight()
);
m_backend->endFrame(target);
ms = elapsedSince(totalStart);
logSlowRenderOperation(ms, "renderScene took {:.1f}ms total", ms);
}
TextMetrics RenderContext::measureText(
std::string_view text, float fontSize, bool bold, float maxWidth, int maxLines, TextAlign align,
std::string_view fontFamily
) {
auto m = m_textRenderer.measure(text, fontSize, bold, maxWidth, maxLines, align, fontFamily);
return TextMetrics{
.width = m.width,
.left = m.left,
.right = m.right,
.top = m.top,
.bottom = m.bottom,
.inkTop = m.inkTop,
.inkBottom = m.inkBottom,
.inkLeft = m.inkLeft,
.inkRight = m.inkRight
};
}
TextMetrics RenderContext::measureFont(float fontSize, bool bold) {
auto m = m_textRenderer.measureFont(fontSize, bold);
return TextMetrics{
.width = m.width,
.left = m.left,
.right = m.right,
.top = m.top,
.bottom = m.bottom,
.inkTop = m.inkTop,
.inkBottom = m.inkBottom,
.inkLeft = m.inkLeft,
.inkRight = m.inkRight
};
}
void RenderContext::measureTextCursorStops(
std::string_view text, float fontSize, const std::vector<std::size_t>& byteOffsets, std::vector<float>& outStops,
bool bold
) {
m_textRenderer.measureCursorStops(text, fontSize, byteOffsets, outStops, bold);
}
TextMetrics RenderContext::measureGlyph(char32_t codepoint, float fontSize) {
auto m = m_glyphRenderer.measureGlyph(codepoint, fontSize);
return TextMetrics{
.width = m.width,
.left = m.left,
.right = m.right,
.top = m.top,
.bottom = m.bottom,
.inkTop = m.top,
.inkBottom = m.bottom,
.inkLeft = m.left,
.inkRight = m.right
};
}
TextureManager& RenderContext::textureManager() {
makeCurrentNoSurface();
return m_backend->textureManager();
}
void RenderContext::renderNode(
const Node* node, const Mat3& parentTransform, float parentOpacity, float sw, float sh, float bw, float bh,
float clipLeft, float clipTop, float clipRight, float clipBottom, bool hasClip
) {
if (!node->visible()) {
return;
}
const Mat3 worldTransform = parentTransform * nodeLocalTransform(node);
const float effectiveOpacity = parentOpacity * node->opacity();
float boundsLeft = 0.0f;
float boundsTop = 0.0f;
float boundsRight = 0.0f;
float boundsBottom = 0.0f;
Node::transformedBounds(node, worldTransform, boundsLeft, boundsTop, boundsRight, boundsBottom);
if (hasClip) {
m_backend->setScissor(scissorForClip(sw, sh, bw, bh, clipLeft, clipTop, clipRight, clipBottom));
} else {
m_backend->disableScissor();
}
switch (node->type()) {
case NodeType::Rect: {
const auto* rect = static_cast<const RectNode*>(node);
auto style = rect->style();
style.fill.a *= effectiveOpacity;
style.border.a *= effectiveOpacity;
for (auto& stop : style.gradientStops) {
stop.color.a *= effectiveOpacity;
}
m_backend->drawRect(sw, sh, node->width(), node->height(), style, worldTransform);
break;
}
case NodeType::Text: {
const auto* text = static_cast<const TextNode*>(node);
if (!text->text().empty()) {
const auto& font = text->fontFamily();
if (text->hasShadow()) {
auto shadowColor = text->shadowColor();
shadowColor.a *= effectiveOpacity;
const Mat3 shadowTransform = worldTransform * Mat3::translation(text->shadowOffsetX(), text->shadowOffsetY());
m_textRenderer.draw(
sw, sh, 0.0f, 0.0f, text->text(), text->fontSize(), shadowColor, shadowTransform, text->bold(),
text->maxWidth(), text->maxLines(), text->textAlign(), font
);
}
auto color = text->color();
color.a *= effectiveOpacity;
m_textRenderer.draw(
sw, sh, 0.0f, 0.0f, text->text(), text->fontSize(), color, worldTransform, text->bold(), text->maxWidth(),
text->maxLines(), text->textAlign(), font
);
}
break;
}
case NodeType::Image: {
const auto* img = static_cast<const ImageNode*>(node);
if (img->textureId() != 0) {
auto tint = img->tint();
tint.a *= effectiveOpacity;
m_backend->drawImage(
RenderImageDraw{
.texture = img->textureId(),
.surfaceWidth = sw,
.surfaceHeight = sh,
.width = node->width(),
.height = node->height(),
.tint = tint,
.opacity = effectiveOpacity,
.radius = img->radius(),
.borderColor = img->borderColor(),
.borderWidth = img->borderWidth(),
.fitMode = static_cast<RenderImageFitMode>(img->fitMode()),
.textureWidth = static_cast<float>(img->textureWidth()),
.textureHeight = static_cast<float>(img->textureHeight()),
.transform = worldTransform,
}
);
}
break;
}
case NodeType::Glyph: {
const auto* icon = static_cast<const GlyphNode*>(node);
if (icon->codepoint() != 0) {
if (icon->hasShadow()) {
auto shadowColor = icon->shadowColor();
shadowColor.a *= effectiveOpacity;
const Mat3 shadowTransform = worldTransform * Mat3::translation(icon->shadowOffsetX(), icon->shadowOffsetY());
m_glyphRenderer.drawGlyph(
sw, sh, 0.0f, 0.0f, icon->codepoint(), icon->fontSize(), shadowColor, shadowTransform
);
}
auto color = icon->color();
color.a *= effectiveOpacity;
m_glyphRenderer.drawGlyph(sw, sh, 0.0f, 0.0f, icon->codepoint(), icon->fontSize(), color, worldTransform);
}
break;
}
case NodeType::Wallpaper: {
const auto* wallpaper = static_cast<const WallpaperNode*>(node);
const bool hasSource1 = wallpaper->sourceKind1() == WallpaperSourceKind::Color || wallpaper->texture1() != 0;
if (hasSource1) {
const bool hasSource2 = wallpaper->sourceKind2() == WallpaperSourceKind::Color || wallpaper->texture2() != 0;
const WallpaperSourceKind sourceKind2 = hasSource2 ? wallpaper->sourceKind2() : wallpaper->sourceKind1();
const TextureId texture2 = hasSource2 ? wallpaper->texture2() : wallpaper->texture1();
const Color& sourceColor2 = hasSource2 ? wallpaper->sourceColor2() : wallpaper->sourceColor1();
const float imageWidth2 = hasSource2 ? wallpaper->imageWidth2() : wallpaper->imageWidth1();
const float imageHeight2 = hasSource2 ? wallpaper->imageHeight2() : wallpaper->imageHeight1();
const float progress = hasSource2 ? wallpaper->progress() : 0.0f;
m_backend->drawWallpaper(
wallpaper->transition(), wallpaper->sourceKind1(), wallpaper->texture1(), wallpaper->sourceColor1(),
sourceKind2, texture2, sourceColor2, sw, sh, node->width(), node->height(), wallpaper->imageWidth1(),
wallpaper->imageHeight1(), imageWidth2, imageHeight2, progress, static_cast<float>(wallpaper->fillMode()),
wallpaper->transitionParams(), wallpaper->fillColor(), worldTransform, wallpaper->blurRadius(),
wallpaper->tintColor()
);
}
break;
}
case NodeType::Base:
case NodeType::InputArea:
case NodeType::Container:
break;
}
// Iterate children in z-order when already sorted; else stable_sort once.
const auto& children = node->children();
bool childrenSorted = true;
for (std::size_t i = 1; i < children.size(); ++i) {
if (children[i]->zIndex() < children[i - 1]->zIndex()) {
childrenSorted = false;
break;
}
}
std::vector<const Node*> orderedChildren;
if (!childrenSorted) {
orderedChildren.reserve(children.size());
for (const auto& child : children) {
orderedChildren.push_back(child.get());
}
std::stable_sort(orderedChildren.begin(), orderedChildren.end(), [](const Node* a, const Node* b) {
return a->zIndex() < b->zIndex();
});
}
float childClipLeft = clipLeft;
float childClipTop = clipTop;
float childClipRight = clipRight;
float childClipBottom = clipBottom;
bool childHasClip = hasClip;
if (node->clipChildren()) {
childClipLeft = hasClip ? std::max(childClipLeft, boundsLeft) : boundsLeft;
childClipTop = hasClip ? std::max(childClipTop, boundsTop) : boundsTop;
childClipRight = hasClip ? std::min(childClipRight, boundsRight) : boundsRight;
childClipBottom = hasClip ? std::min(childClipBottom, boundsBottom) : boundsBottom;
childHasClip = true;
}
if (childHasClip && (childClipRight <= childClipLeft || childClipBottom <= childClipTop)) {
return;
}
if (childrenSorted) {
for (const auto& child : children) {
renderNode(
child.get(), worldTransform, effectiveOpacity, sw, sh, bw, bh, childClipLeft, childClipTop, childClipRight,
childClipBottom, childHasClip
);
}
} else {
for (const auto* child : orderedChildren) {
renderNode(
child, worldTransform, effectiveOpacity, sw, sh, bw, bh, childClipLeft, childClipTop, childClipRight,
childClipBottom, childHasClip
);
}
}
}
void RenderContext::cleanup() {
if (m_backend != nullptr) {
// Current context required to destroy GL resources.
m_backend->makeCurrentNoSurface();
}
// Text renderers destroy GL textures; run while context is current.
m_textRenderer.cleanup();
m_glyphRenderer.cleanup();
if (m_backend != nullptr) {
m_backend->cleanup();
m_backend.reset();
}
}