xbintsc 0.7.7 → 0.7.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (160) hide show
  1. package/dist/src/cli/help.d.ts +1 -1
  2. package/dist/src/cli/help.js +1 -1
  3. package/dist/src/codegen/generator/calls/invocation.js +6 -4
  4. package/dist/src/codegen/generator/calls/invocation.js.map +1 -1
  5. package/dist/src/codegen/generator/context.d.ts +4 -3
  6. package/dist/src/codegen/generator/context.js +6 -5
  7. package/dist/src/codegen/generator/context.js.map +1 -1
  8. package/dist/src/codegen/generator/expressions/assignment.js +3 -3
  9. package/dist/src/driver/config.d.ts +1 -1
  10. package/dist/src/driver/options.d.ts +1 -1
  11. package/dist/src/driver/version.d.ts +1 -1
  12. package/dist/src/driver/version.js +1 -1
  13. package/dist/src/extensions/gui/features.d.ts +14 -6
  14. package/dist/src/extensions/gui/features.js +34 -6
  15. package/dist/src/extensions/gui/features.js.map +1 -1
  16. package/dist/src/extensions/gui/index.js +3 -3
  17. package/dist/tests/codegen/llvm.test.js +1 -1
  18. package/dist/tests/e2e/gui-audio.test.js +24 -0
  19. package/dist/tests/e2e/gui-audio.test.js.map +1 -1
  20. package/dist/tests/e2e/gui-draw-perf.test.js +9 -0
  21. package/dist/tests/e2e/gui-draw-perf.test.js.map +1 -1
  22. package/dist/tests/e2e/gui-font-face.test.d.ts +3 -1
  23. package/dist/tests/e2e/gui-font-face.test.js +99 -7
  24. package/dist/tests/e2e/gui-font-face.test.js.map +1 -1
  25. package/dist/tests/e2e/gui-frame-pacing.test.d.ts +15 -0
  26. package/dist/tests/e2e/gui-frame-pacing.test.js +68 -0
  27. package/dist/tests/e2e/gui-frame-pacing.test.js.map +1 -0
  28. package/dist/tests/e2e/gui-initial-url.test.d.ts +10 -0
  29. package/dist/tests/e2e/gui-initial-url.test.js +49 -0
  30. package/dist/tests/e2e/gui-initial-url.test.js.map +1 -0
  31. package/dist/tests/e2e/gui-layout.test.js +85 -0
  32. package/dist/tests/e2e/gui-layout.test.js.map +1 -1
  33. package/dist/tests/e2e/gui-webgl-depth-init.test.d.ts +13 -0
  34. package/dist/tests/e2e/gui-webgl-depth-init.test.js +144 -0
  35. package/dist/tests/e2e/gui-webgl-depth-init.test.js.map +1 -0
  36. package/dist/tests/e2e/gui-webgl-depth-range.test.d.ts +18 -0
  37. package/dist/tests/e2e/gui-webgl-depth-range.test.js +140 -0
  38. package/dist/tests/e2e/gui-webgl-depth-range.test.js.map +1 -0
  39. package/dist/tests/e2e/gui-webgl.test.js +49 -0
  40. package/dist/tests/e2e/gui-webgl.test.js.map +1 -1
  41. package/dist/tests/e2e/gui-webgpu-attachment.test.d.ts +19 -0
  42. package/dist/tests/e2e/gui-webgpu-attachment.test.js +258 -0
  43. package/dist/tests/e2e/gui-webgpu-attachment.test.js.map +1 -0
  44. package/dist/tests/e2e/method-call-cache.test.js +27 -0
  45. package/dist/tests/e2e/method-call-cache.test.js.map +1 -1
  46. package/dist/tests/extensions/gui-features.test.js +27 -2
  47. package/dist/tests/extensions/gui-features.test.js.map +1 -1
  48. package/doc/ai/extensions.md +10 -4
  49. package/doc/ai/zh-CN/extensions.md +8 -3
  50. package/doc/gui/animation.md +1 -1
  51. package/doc/gui/api.md +35 -2
  52. package/doc/gui/build.md +34 -15
  53. package/doc/gui/dom.md +3 -2
  54. package/doc/gui/known-issues.md +1 -1
  55. package/doc/gui/layout.md +9 -3
  56. package/doc/gui/rendering.md +45 -8
  57. package/doc/gui/runtime.md +13 -0
  58. package/doc/gui/webgl.md +29 -3
  59. package/doc/gui/webgpu.md +20 -0
  60. package/doc/gui.md +9 -4
  61. package/doc/implemented/codegen.md +4 -3
  62. package/doc/zh-CN/gui/animation.md +1 -1
  63. package/doc/zh-CN/gui/api.md +25 -2
  64. package/doc/zh-CN/gui/build.md +20 -7
  65. package/doc/zh-CN/gui/dom.md +3 -2
  66. package/doc/zh-CN/gui/known-issues.md +1 -1
  67. package/doc/zh-CN/gui/layout.md +7 -4
  68. package/doc/zh-CN/gui/rendering.md +33 -8
  69. package/doc/zh-CN/gui/runtime.md +9 -0
  70. package/doc/zh-CN/gui/webgl.md +7 -4
  71. package/doc/zh-CN/gui/webgpu.md +18 -0
  72. package/doc/zh-CN/gui.md +10 -5
  73. package/doc/zh-CN/implemented/codegen.md +4 -3
  74. package/package.json +7 -7
  75. package/runtime/ext_gui/audio_graph_commands.cpp +127 -0
  76. package/runtime/ext_gui/audio_mixer.cpp +84 -163
  77. package/runtime/ext_gui/audio_mixer.h +59 -3
  78. package/runtime/ext_gui/audio_mixer_extra.cpp +8 -0
  79. package/runtime/ext_gui/audio_param_apply.cpp +160 -0
  80. package/runtime/ext_gui/browser.cpp +6 -19
  81. package/runtime/ext_gui/browser_location.cpp +78 -0
  82. package/runtime/ext_gui/css.cpp +1 -0
  83. package/runtime/ext_gui/css.h +4 -0
  84. package/runtime/ext_gui/css_sheet.cpp +4 -2
  85. package/runtime/ext_gui/document_animation.cpp +7 -0
  86. package/runtime/ext_gui/dom_api_computed_style.cpp +3 -2
  87. package/runtime/ext_gui/dom_api_webgl.cpp +39 -2
  88. package/runtime/ext_gui/dom_api_webgl_extensions.cpp +134 -0
  89. package/runtime/ext_gui/dom_api_webgl_internal.h +4 -0
  90. package/runtime/ext_gui/dom_api_webgl_shader.cpp +28 -4
  91. package/runtime/ext_gui/dom_api_webgl_state.cpp +6 -103
  92. package/runtime/ext_gui/gui.cpp +100 -1
  93. package/runtime/ext_gui/gui_events.cpp +8 -2
  94. package/runtime/ext_gui/layout_flex.cpp +5 -2
  95. package/runtime/ext_gui/layout_flow.cpp +24 -1
  96. package/runtime/ext_gui/layout_flow_internal.h +11 -2
  97. package/runtime/ext_gui/media_query.cpp +3 -0
  98. package/runtime/ext_gui/perf.cpp +18 -6
  99. package/runtime/ext_gui/perf.h +37 -0
  100. package/runtime/ext_gui/style.cpp +28 -10
  101. package/runtime/ext_gui/style.h +10 -1
  102. package/runtime/ext_gui/style_length.cpp +4 -0
  103. package/runtime/ext_gui/style_print.cpp +7 -1
  104. package/runtime/ext_gui/style_values.cpp +6 -5
  105. package/runtime/ext_gui/text.cpp +4 -0
  106. package/runtime/ext_gui/text_font_bytes.cpp +117 -0
  107. package/runtime/ext_gui/text_font_bytes.h +65 -0
  108. package/runtime/ext_gui/text_font_db.cpp +5 -0
  109. package/runtime/ext_gui/text_font_sfnt.cpp +71 -24
  110. package/runtime/ext_gui/text_font_sfnt.h +5 -2
  111. package/runtime/ext_gui/text_hb.cpp +34 -5
  112. package/runtime/ext_gui/text_stb.cpp +7 -30
  113. package/runtime/ext_gui/text_webfont.cpp +217 -0
  114. package/runtime/ext_gui/warnings.cpp +26 -0
  115. package/runtime/ext_gui/warnings.h +8 -0
  116. package/runtime/ext_gui/webfont_bridge.c +23 -0
  117. package/runtime/ext_gui/webgl.h +36 -39
  118. package/runtime/ext_gui/webgl_blit_color.cpp +5 -0
  119. package/runtime/ext_gui/webgl_buffer_objects.cpp +19 -5
  120. package/runtime/ext_gui/webgl_context.cpp +2 -0
  121. package/runtime/ext_gui/webgl_draw.cpp +72 -12
  122. package/runtime/ext_gui/webgl_draw_setup.cpp +8 -2
  123. package/runtime/ext_gui/webgl_draw_state.h +13 -2
  124. package/runtime/ext_gui/webgl_framebuffer.cpp +8 -0
  125. package/runtime/ext_gui/webgl_objects.h +19 -0
  126. package/runtime/ext_gui/webgl_program.h +64 -0
  127. package/runtime/ext_gui/webgl_state.cpp +7 -0
  128. package/runtime/ext_gui/webgl_targets.cpp +31 -7
  129. package/runtime/ext_gui/webgl_uniform.cpp +78 -0
  130. package/runtime/ext_gui/webgpu.h +10 -0
  131. package/runtime/ext_gui/webgpu_buffer.cpp +3 -1
  132. package/runtime/ext_gui/webgpu_canvas.cpp +1 -0
  133. package/runtime/ext_gui/webgpu_command.cpp +51 -11
  134. package/runtime/ext_gui/webgpu_convert.cpp +7 -2
  135. package/runtime/ext_gui/webgpu_pass.cpp +12 -0
  136. package/runtime/ext_gui/webgpu_queue.cpp +3 -0
  137. package/runtime/ext_gui/webgpu_resources.cpp +1 -0
  138. package/runtime/ext_gui/window.cpp +6 -0
  139. package/runtime/rt_object.h +6 -0
  140. package/runtime/rt_profile.h +6 -0
  141. package/runtime/xt_containers/access.inc +2 -47
  142. package/runtime/xt_containers/ic_get.inc +70 -0
  143. package/runtime/xt_containers/method_cache.inc +42 -11
  144. package/runtime/xt_containers/objects.inc +12 -1
  145. package/runtime/xt_containers/shapes.inc +78 -0
  146. package/runtime/xt_containers.c +1 -0
  147. package/runtime/xt_profile.c +3 -0
  148. package/scripts/build-gui.ts +43 -49
  149. package/scripts/gui-engine-build.ts +58 -0
  150. package/scripts/gui-vendor-source.ts +53 -0
  151. package/scripts/gui-webfont-build.ts +138 -0
  152. package/src/cli/help.ts +1 -1
  153. package/src/codegen/generator/calls/invocation.ts +6 -4
  154. package/src/codegen/generator/context.ts +6 -6
  155. package/src/codegen/generator/expressions/assignment.ts +3 -3
  156. package/src/driver/config.ts +1 -1
  157. package/src/driver/options.ts +1 -1
  158. package/src/driver/version.ts +1 -1
  159. package/src/extensions/gui/features.ts +34 -6
  160. package/src/extensions/gui/index.ts +3 -3
@@ -16,15 +16,6 @@ namespace {
16
16
 
17
17
  xtgui::WebGLContext *context_of(xt_value self) { return webgl_live_context(self); }
18
18
 
19
- /** Fire a `webglcontextlost`/`webglcontextrestored` event on the canvas that
20
- * owns the context. `self` is the context or extension handle. */
21
- void dispatch_context_event(xt_value self, const char *type) {
22
- XtGuiWindow *win = nullptr;
23
- xtgui::Node *node =
24
- resolve_node(xt_object_get_cstr(self, "__xt_gl_node"), &win);
25
- if (win != nullptr && node != nullptr) xt_gui_dispatch_to_node(win, node, type);
26
- }
27
-
28
19
  xt_value number_array(const double *values, int count) {
29
20
  xt_value array = xt_array_new(0, nullptr);
30
21
  for (int i = 0; i < count; i++) xt_array_push(array, xt_number(values[i]));
@@ -37,9 +28,6 @@ xt_value object_for(xt_value self, uint32_t id, const char *key, const char *kin
37
28
 
38
29
  } // namespace
39
30
 
40
- METHOD(webgl_lose_context);
41
- METHOD(webgl_restore_context);
42
-
43
31
  void webgl_unpack_flags(xt_value context, bool *flip_y, bool *premultiply) {
44
32
  *flip_y = value_truthy(xt_object_get_cstr(context, "__xt_unpack_flip_y"));
45
33
  *premultiply = value_truthy(xt_object_get_cstr(context, "__xt_unpack_premultiply"));
@@ -126,8 +114,11 @@ METHOD(webgl_depth_mask) {
126
114
 
127
115
  METHOD(webgl_depth_range) {
128
116
  (void)env;
129
- (void)argc;
130
- (void)argv;
117
+ xtgui::WebGLContext *context = context_of(self);
118
+ if (context != nullptr) {
119
+ context->depth_range((float)webgl_number(argc, argv, 0, 0.0),
120
+ (float)webgl_number(argc, argv, 1, 1.0));
121
+ }
131
122
  return XT_UNDEFINED;
132
123
  }
133
124
 
@@ -331,7 +322,7 @@ METHOD(webgl_get_parameter) {
331
322
  return number_array(values, 4);
332
323
  }
333
324
  case GL_DEPTH_RANGE: {
334
- const double values[2] = {0, 1};
325
+ const double values[2] = {context->depth_range_near(), context->depth_range_far()};
335
326
  return number_array(values, 2);
336
327
  }
337
328
  case GL_BLEND_COLOR: {
@@ -416,69 +407,6 @@ METHOD(webgl_get_parameter) {
416
407
  }
417
408
  }
418
409
 
419
- METHOD(webgl_get_supported_extensions) {
420
- (void)env;
421
- (void)argc;
422
- (void)argv;
423
- xt_value list = xt_array_new(0, nullptr);
424
- xt_array_push(list, xt_string_from_cstr("OES_element_index_uint"));
425
- xt_array_push(list, xt_string_from_cstr("OES_standard_derivatives"));
426
- xt_array_push(list, xt_string_from_cstr("OES_texture_float"));
427
- xt_array_push(list, xt_string_from_cstr("OES_texture_half_float"));
428
- xt_array_push(list, xt_string_from_cstr("WEBGL_depth_texture"));
429
- xt_array_push(list, xt_string_from_cstr("EXT_color_buffer_float"));
430
- xt_array_push(list, xt_string_from_cstr("EXT_color_buffer_half_float"));
431
- xt_array_push(list, xt_string_from_cstr("WEBGL_lose_context"));
432
- xt_array_push(list, xt_string_from_cstr("EXT_texture_filter_anisotropic"));
433
- xtgui::WebGLContext *context = webgl_from_context(self);
434
- if (context != nullptr && context->supports_texture_format(0x83F1)) {
435
- xt_array_push(list, xt_string_from_cstr("WEBGL_compressed_texture_s3tc"));
436
- }
437
- if (context != nullptr && context->supports_texture_format(0x8C4D)) {
438
- xt_array_push(list, xt_string_from_cstr("WEBGL_compressed_texture_s3tc_srgb"));
439
- }
440
- if (context != nullptr && context->supports_texture_format(0x93B0)) {
441
- xt_array_push(list, xt_string_from_cstr("WEBGL_compressed_texture_astc"));
442
- }
443
- return list;
444
- }
445
-
446
- METHOD(webgl_get_extension) {
447
- (void)env;
448
- const std::string name = webgl_string(argc, argv, 0);
449
- if (name.rfind("WEBGL_compressed_texture_", 0) == 0) {
450
- xt_value extension = webgl_compressed_extension(name, webgl_from_context(self));
451
- if (!XT_IS_UNDEFINED(extension)) return extension;
452
- }
453
- if (name == "OES_element_index_uint" || name == "OES_standard_derivatives" ||
454
- name == "OES_texture_float" || name == "OES_texture_half_float" ||
455
- name == "WEBGL_depth_texture" || name == "EXT_color_buffer_float" ||
456
- name == "EXT_color_buffer_half_float") {
457
- return xt_object_new();
458
- }
459
- if (name == "EXT_texture_filter_anisotropic") {
460
- xt_value e = xt_object_new();
461
- xt_object_set(e, xt_string_from_cstr("MAX_TEXTURE_MAX_ANISOTROPY_EXT"), xt_number(0x84FF));
462
- xt_object_set(e, xt_string_from_cstr("TEXTURE_MAX_ANISOTROPY_EXT"), xt_number(0x84FE));
463
- return e;
464
- }
465
- if (name == "WEBGL_lose_context") {
466
- xt_value object = xt_object_new();
467
- /* The closures receive this object as `this`, so carry the context id and
468
- * the canvas handle over from the context that produced the extension. */
469
- xt_object_set(object, xt_string_from_cstr("__xt_gl_context"),
470
- xt_object_get_cstr(self, "__xt_gl_context"));
471
- xt_object_set(object, xt_string_from_cstr("__xt_gl_node"),
472
- xt_object_get_cstr(self, "__xt_gl_node"));
473
- xt_object_set(object, xt_string_from_cstr("loseContext"),
474
- xt_closure_new((void *)webgl_lose_context, 0, nullptr));
475
- xt_object_set(object, xt_string_from_cstr("restoreContext"),
476
- xt_closure_new((void *)webgl_restore_context, 0, nullptr));
477
- return object;
478
- }
479
- return XT_NULL;
480
- }
481
-
482
410
  METHOD(webgl_get_context_attributes) {
483
411
  (void)env;
484
412
  (void)argc;
@@ -506,31 +434,6 @@ METHOD(webgl_is_context_lost) {
506
434
  return xt_bool(context == nullptr || context->lost());
507
435
  }
508
436
 
509
- /* `WEBGL_lose_context.loseContext()` / `restoreContext()`: the simulator drops
510
- * every object and reports `CONTEXT_LOST_WEBGL` until the restore, so a page
511
- * exercises the same loss/restore path it would take on a real GPU reset. */
512
- METHOD(webgl_lose_context) {
513
- (void)env;
514
- (void)argc;
515
- (void)argv;
516
- xtgui::WebGLContext *context = webgl_from_context(self);
517
- if (context == nullptr || context->lost()) return XT_UNDEFINED;
518
- context->lose_context();
519
- dispatch_context_event(self, "webglcontextlost");
520
- return XT_UNDEFINED;
521
- }
522
-
523
- METHOD(webgl_restore_context) {
524
- (void)env;
525
- (void)argc;
526
- (void)argv;
527
- xtgui::WebGLContext *context = webgl_from_context(self);
528
- if (context == nullptr || !context->lost()) return XT_UNDEFINED;
529
- context->restore_context();
530
- if (!context->lost()) dispatch_context_event(self, "webglcontextrestored");
531
- return XT_UNDEFINED;
532
- }
533
-
534
437
  /* Stencil test/update state. SDL_GPU has one stencil reference per draw and no
535
438
  * per-pipeline compare/write masks; the entry points store WebGL's state, the
536
439
  * pipeline carries the compare function and ops, and `SDL_SetGPUStencilReference`
@@ -403,6 +403,85 @@ extern "C" void xt_gui_pump_events(void) {
403
403
  while (SDL_PollEvent(&event)) handle_event(&event);
404
404
  }
405
405
 
406
+ namespace {
407
+
408
+ /** True while at least one window is still open (the loop's exit condition). */
409
+ bool any_window_open() {
410
+ for (int i = 0; i < g_window_count; i++) {
411
+ if (g_windows[i].open) return true;
412
+ }
413
+ return false;
414
+ }
415
+
416
+ /**
417
+ * The frame period `run()` paces to, in milliseconds; 0 means "uncapped" and
418
+ * restores the old busy loop.
419
+ *
420
+ * `XT_GUI_FRAME_MS` overrides: `0` disables pacing (tests/benchmarks that want
421
+ * the historical behaviour), a positive value fixes the period. Otherwise the
422
+ * loop follows the display refresh rate of the newest open window, falling back
423
+ * to 60 Hz when the backend cannot report a mode. Cached on first use: the
424
+ * period is a property of the session, not of a frame.
425
+ */
426
+ double run_frame_period_ms() {
427
+ static double cached = -1.0;
428
+ if (cached >= 0.0) return cached;
429
+ const char *env = SDL_getenv("XT_GUI_FRAME_MS");
430
+ if (env != NULL && env[0] != '\0') {
431
+ const double requested = SDL_atof(env);
432
+ if (!(requested > 0.0)) {
433
+ cached = 0.0;
434
+ } else if (requested < 1.0) {
435
+ cached = 1.0;
436
+ } else if (requested > 1000.0) {
437
+ cached = 1000.0;
438
+ } else {
439
+ cached = requested;
440
+ }
441
+ return cached;
442
+ }
443
+ float refresh = 0.0f;
444
+ for (int i = g_window_count - 1; i >= 0; i--) {
445
+ if (!g_windows[i].open || g_windows[i].window == NULL) continue;
446
+ const SDL_DisplayMode *mode =
447
+ SDL_GetCurrentDisplayMode(SDL_GetDisplayForWindow(g_windows[i].window));
448
+ if (mode != NULL && mode->refresh_rate > refresh) refresh = mode->refresh_rate;
449
+ }
450
+ cached = refresh > 0.0f ? 1000.0 / (double)refresh : 1000.0 / 60.0;
451
+ return cached;
452
+ }
453
+
454
+ /**
455
+ * Block until `deadline_ns`, still dispatching SDL events and servicing the
456
+ * runtime's timers/microtasks.
457
+ *
458
+ * The old loop ran `SDL_Delay(1)` between frames and drained rAF callbacks on
459
+ * every iteration: a page that drives itself ran its logic, WebGL recording and
460
+ * DOM updates hundreds of times per second (and the GPU was asked to render
461
+ * frames the compositor never showed). This wait makes the loop behave like a
462
+ * browser's vsync tick: the page gets one frame per display refresh, and the
463
+ * CPU sleeps instead of spinning. Slices cap the timer/input latency at 4 ms.
464
+ */
465
+ void wait_for_frame(uint64_t deadline_ns) {
466
+ for (;;) {
467
+ if (g_quit || !any_window_open()) return;
468
+ const uint64_t now = SDL_GetTicksNS();
469
+ if (now >= deadline_ns) return;
470
+ uint64_t remaining_ms = (deadline_ns - now) / 1000000ull;
471
+ if (remaining_ms > 4) remaining_ms = 4;
472
+ SDL_Event event;
473
+ if (SDL_WaitEventTimeout(&event, (Sint32)remaining_ms)) {
474
+ handle_event(&event);
475
+ while (SDL_PollEvent(&event)) handle_event(&event);
476
+ }
477
+ xt_loop_poll(0);
478
+ xt_drain_microtasks();
479
+ xt_promise_report_unhandled();
480
+ }
481
+ }
482
+
483
+ } // namespace
484
+
406
485
  extern "C" xt_value xt_gui_run(int32_t argc, xt_value *argv) {
407
486
  (void)argc;
408
487
  (void)argv;
@@ -413,7 +492,17 @@ extern "C" xt_value xt_gui_run(int32_t argc, xt_value *argv) {
413
492
  deadline = SDL_GetTicks() + (Uint64)SDL_atoi(autoclose);
414
493
  }
415
494
  g_run_active = 1;
495
+ const double period_ms = run_frame_period_ms();
496
+ uint64_t next_frame_ns = 0;
416
497
  while (!g_quit) {
498
+ /* Wait for the display refresh before starting the next frame. The first
499
+ * frame runs immediately. A frame that overran its slot does not make the
500
+ * next one wait: `next_frame_ns` is clamped to now below. */
501
+ if (period_ms > 0.0 && next_frame_ns != 0) {
502
+ wait_for_frame(next_frame_ns);
503
+ if (g_quit) break;
504
+ }
505
+ const uint64_t frame_start_ns = SDL_GetTicksNS();
417
506
  SDL_Event event;
418
507
  int had_event = 0;
419
508
  while (SDL_PollEvent(&event)) {
@@ -438,7 +527,17 @@ extern "C" xt_value xt_gui_run(int32_t argc, xt_value *argv) {
438
527
  xt_promise_report_unhandled();
439
528
 
440
529
  if (deadline != 0 && SDL_GetTicks() >= deadline) break;
441
- if (!had_event) SDL_Delay(1);
530
+ if (period_ms > 0.0) {
531
+ const uint64_t period_ns = (uint64_t)(period_ms * 1000000.0);
532
+ next_frame_ns = frame_start_ns + period_ns;
533
+ const uint64_t now = SDL_GetTicksNS();
534
+ /* Behind schedule: run the next frame now instead of accumulating a
535
+ * growing debt (this is the "as fast as it can" path for a page slower
536
+ * than the refresh rate). */
537
+ if (next_frame_ns < now) next_frame_ns = now;
538
+ } else if (!had_event) {
539
+ SDL_Delay(1);
540
+ }
442
541
  }
443
542
 
444
543
  g_run_active = 0;
@@ -331,8 +331,14 @@ void xt_gui_render_window(XtGuiWindow *win) {
331
331
  * to be presented even when its callback changed nothing visible. */
332
332
  if (!win->animation_frames.empty()) win->present_dirty = 1;
333
333
  /* The timestamp is a DOMHighResTimeStamp: the same clock as
334
- * `performance.now()`, not SDL's machine-uptime tick. */
335
- xt_gui_run_animation_frames(win, xt_monotonic_ms());
334
+ * `performance.now()`, not SDL's machine-uptime tick. This is the page's own
335
+ * code (AOT-compiled `<script>`/rAF callbacks, including any WebGL command
336
+ * recording it does), so it gets its own phase: without it a script-heavy
337
+ * frame looked like unexplained time in `frame`. */
338
+ {
339
+ xtgui::PerfScope scope(&xtgui::g_perf_frame_ms.script);
340
+ xt_gui_run_animation_frames(win, xt_monotonic_ms());
341
+ }
336
342
  /* A callback may have closed the window (directly or through the
337
343
  * auto-close guard): `xt_gui_quit_window` released the document and the GPU
338
344
  * geometry, so there is nothing left to lay out or paint. */
@@ -220,8 +220,11 @@ float Layouter::layoutFlexRow(LayoutBox *container, const std::vector<LayoutBox
220
220
  size_t i = reverse ? line[line.size() - 1 - k] : line[k];
221
221
  AlignItems align = items[i]->style->has_align_self ? items[i]->style->align_self
222
222
  : containerStyle.align_items;
223
- bool stretch = align == AlignItems::Stretch && isAuto(items[i]->style->height) &&
224
- (definiteHeight >= 0 || lines.size() > 1);
223
+ /* Stretch fills the line's cross size, whether that size came from a
224
+ * definite container height or from the tallest item on an auto-height
225
+ * line (a single-line auto container's line cross size is the largest
226
+ * item, so all auto-height items still become equally tall). */
227
+ bool stretch = align == AlignItems::Stretch && isAuto(items[i]->style->height);
225
228
  if (stretch) {
226
229
  float contentH = lineCross[li] - verticalEdges(items[i]);
227
230
  if (contentH < 0) contentH = 0;
@@ -165,7 +165,24 @@ void Layouter::setContainingBlock(LayoutBox *box, const LayoutBox *ancestor) con
165
165
 
166
166
  void Layouter::publishContainingBlock(LayoutBox *box) const {
167
167
  if (!establishesContainingBlock(box)) return;
168
- for (const std::unique_ptr<LayoutBox> &child : box->children) setContainingBlock(child.get(), box);
168
+ refreshContainingBlocks(box, box);
169
+ }
170
+
171
+ void Layouter::refreshContainingBlocks(LayoutBox *box, const LayoutBox *ancestor) const {
172
+ for (const std::unique_ptr<LayoutBox> &child : box->children) {
173
+ setContainingBlock(child.get(), ancestor);
174
+ Position position = child->style != nullptr ? child->style->position : Position::Static;
175
+ if (position == Position::Absolute || position == Position::Fixed) {
176
+ /* Idempotent: the target is recomputed from the containing block, so a
177
+ * second call after the block's real height is known moves the box to its
178
+ * final inset instead of leaving the mid-layout guess. */
179
+ applyPosition(child.get());
180
+ } else if (position == Position::Static) {
181
+ /* A static box does not establish a containing block: keep descending. A
182
+ * relative one does, and publishes for its own subtree when it finishes. */
183
+ refreshContainingBlocks(child.get(), ancestor);
184
+ }
185
+ }
169
186
  }
170
187
 
171
188
  void Layouter::applyPosition(LayoutBox *box) const {
@@ -330,6 +347,12 @@ float Layouter::layoutBlock(LayoutBox *box, float x, float y, float availableWid
330
347
  if (definiteHeight < 0) definiteHeight = 0;
331
348
  }
332
349
 
350
+ /* A definite height is already the final content height, so publish it before
351
+ * the children are laid out: an absolutely positioned descendant nested under
352
+ * a *static* wrapper resolves its insets against this box, and reading the
353
+ * still-zero `content_height` would put `bottom: 0` at `-height` (the
354
+ * cinematic bars are the case that exposed it). */
355
+ if (definiteHeight >= 0) box->content_height = definiteHeight;
333
356
  float contentHeight;
334
357
  if (box->is_control) {
335
358
  /* Controls are leaves with an intrinsic content height (a line, a textarea's
@@ -168,6 +168,8 @@ inline float toPx(const Length &length, float base, float em, float root, float
168
168
  return vw * length.value / 100.0f;
169
169
  case Unit::Vh:
170
170
  return vh * length.value / 100.0f;
171
+ case Unit::Ch:
172
+ return em * length.value * 0.5f;
171
173
  default:
172
174
  return fallback;
173
175
  }
@@ -270,10 +272,17 @@ struct Layouter {
270
272
  /** Nearest ancestor (or `box` itself) that is a containing block, else null. */
271
273
  const LayoutBox *containingBlockAncestor(const LayoutBox *box) const;
272
274
 
273
- /** Re-publish the containing block of `box`'s children once `box` has its own
274
- * final size (an absolute child resolves `bottom`/`right` against it). */
275
+ /** Re-publish the containing block of `box`'s descendants once `box` has its
276
+ * own final size (an absolute child resolves `bottom`/`right` against it).
277
+ * Walks through static boxes too, so an out-of-flow box nested under a static
278
+ * wrapper still sees the final padding box, not the wrapper's zero height. */
275
279
  void publishContainingBlock(LayoutBox *box) const;
276
280
 
281
+ /** Recursive half of `publishContainingBlock`: re-point `ancestor` as the
282
+ * containing block of every descendant that does not establish its own, and
283
+ * re-apply the insets of the absolute boxes among them. */
284
+ void refreshContainingBlocks(LayoutBox *box, const LayoutBox *ancestor) const;
285
+
277
286
  /** Publish `box`'s padding box as the containing block for absolute
278
287
  * descendants of the nearest positioned ancestor (null means the viewport). */
279
288
  void setContainingBlock(LayoutBox *box, const LayoutBox *ancestor) const;
@@ -61,6 +61,9 @@ bool mediaLength(const std::string &raw, float vw, float vh, float *out) {
61
61
  case Unit::Rem:
62
62
  *out = length.value * 16.0f;
63
63
  return true;
64
+ case Unit::Ch:
65
+ *out = length.value * 8.0f;
66
+ return true;
64
67
  case Unit::Vw:
65
68
  *out = vw * length.value / 100.0f;
66
69
  return true;
@@ -67,29 +67,38 @@ void addCounters(PerfCounters *target, const PerfCounters &value) {
67
67
  target->webglPasses += value.webglPasses;
68
68
  target->webglPipelineMisses += value.webglPipelineMisses;
69
69
  target->webglBufferUploadBytes += value.webglBufferUploadBytes;
70
+ target->webglSetupMisses += value.webglSetupMisses;
71
+ target->webglSamplerBinds += value.webglSamplerBinds;
72
+ target->webglStateBinds += value.webglStateBinds;
70
73
  }
71
74
 
72
75
  void addTimings(PerfTimings *target, const PerfTimings &value) {
73
76
  target->frame += value.frame;
77
+ target->script += value.script;
74
78
  target->restyle += value.restyle;
75
79
  target->layout += value.layout;
76
80
  target->paint += value.paint;
77
81
  target->upload += value.upload;
78
82
  target->draw += value.draw;
83
+ target->webgl_draw += value.webgl_draw;
84
+ target->webgl_uniform += value.webgl_uniform;
79
85
  }
80
86
 
81
87
  void printLine(uint64_t frames, const PerfTimings &ms, const PerfCounters &counters) {
82
88
  double divisor = frames > 0 ? (double)frames : 1.0;
83
89
  std::fprintf(stderr,
84
- "xt_gui perf frames=%llu ms=%.3f restyle=%.3f layout=%.3f paint=%.3f "
85
- "upload=%.3f draw=%.3f restyles=%llu nodes=%llu rules=%llu media=%llu "
90
+ "xt_gui perf frames=%llu ms=%.3f script=%.3f restyle=%.3f layout=%.3f "
91
+ "paint=%.3f upload=%.3f draw=%.3f gldraw=%.3f gluni=%.3f "
92
+ "restyles=%llu nodes=%llu rules=%llu media=%llu "
86
93
  "layouts=%llu boxes=%llu text=%llu textHit=%llu ops=%llu runs=%llu "
87
94
  "draws=%llu uploads=%llu verts=%llu sync=%llu presents=%llu exposes=%llu "
88
95
  "deferred=%llu starved=%llu skip=%llu "
89
- "cdraw=%llu cups=%llu cbytes=%llu gldraw=%llu passes=%llu gmiss=%llu "
90
- "gbytes=%llu\n",
91
- (unsigned long long)frames, ms.frame / divisor, ms.restyle / divisor,
96
+ "cdraw=%llu cups=%llu cbytes=%llu gldraws=%llu passes=%llu gmiss=%llu "
97
+ "gbytes=%llu setupMiss=%llu samplerBinds=%llu stateBinds=%llu\n",
98
+ (unsigned long long)frames, ms.frame / divisor, ms.script / divisor,
99
+ ms.restyle / divisor,
92
100
  ms.layout / divisor, ms.paint / divisor, ms.upload / divisor, ms.draw / divisor,
101
+ ms.webgl_draw / divisor, ms.webgl_uniform / divisor,
93
102
  (unsigned long long)counters.restyles, (unsigned long long)counters.restyleNodes,
94
103
  (unsigned long long)counters.rulesEvaluated,
95
104
  (unsigned long long)counters.mediaEvaluated, (unsigned long long)counters.layouts,
@@ -109,7 +118,10 @@ void printLine(uint64_t frames, const PerfTimings &ms, const PerfCounters &count
109
118
  (unsigned long long)counters.webglDraws,
110
119
  (unsigned long long)counters.webglPasses,
111
120
  (unsigned long long)counters.webglPipelineMisses,
112
- (unsigned long long)counters.webglBufferUploadBytes);
121
+ (unsigned long long)counters.webglBufferUploadBytes,
122
+ (unsigned long long)counters.webglSetupMisses,
123
+ (unsigned long long)counters.webglSamplerBinds,
124
+ (unsigned long long)counters.webglStateBinds);
113
125
  }
114
126
 
115
127
  } // namespace
@@ -20,6 +20,8 @@
20
20
 
21
21
  #include <cstdint>
22
22
 
23
+ #include <SDL3/SDL.h>
24
+
23
25
  namespace xtgui {
24
26
 
25
27
  /** Counters for one frame; the same layout is reused for cumulative totals. */
@@ -76,16 +78,28 @@ struct PerfCounters {
76
78
  uint64_t webglPasses = 0;
77
79
  uint64_t webglPipelineMisses = 0;
78
80
  uint64_t webglBufferUploadBytes = 0;
81
+ /* Prepared-draw cache misses, sampler-set binds and dynamic-state binds. A
82
+ * steady scene should miss only on the first frame and bind samplers only
83
+ * when the texture/uniform set actually changes. */
84
+ uint64_t webglSetupMisses = 0;
85
+ uint64_t webglSamplerBinds = 0;
86
+ uint64_t webglStateBinds = 0;
79
87
  };
80
88
 
81
89
  /** Wall-clock phase timings in milliseconds. */
82
90
  struct PerfTimings {
83
91
  double frame = 0;
92
+ /** Time spent inside `requestAnimationFrame` callbacks (the page's own AOT
93
+ * code plus any WebGL command recording it does). */
94
+ double script = 0;
84
95
  double restyle = 0;
85
96
  double layout = 0;
86
97
  double paint = 0;
87
98
  double upload = 0;
88
99
  double draw = 0;
100
+ /** WebGL draw recording and uniform uploads; both happen inside `script`. */
101
+ double webgl_draw = 0;
102
+ double webgl_uniform = 0;
89
103
  };
90
104
 
91
105
  /** The frame being recorded; reset by `xt_perf_frame_begin`. */
@@ -122,6 +136,29 @@ class PerfScope {
122
136
  uint64_t start_ = 0;
123
137
  };
124
138
 
139
+ /** A `PerfScope` that only reads the clock while profiling is armed. Use it in
140
+ * paths that run thousands of times per frame (one draw, one uniform call): an
141
+ * always-on pair of `SDL_GetPerformanceCounter` reads would itself be measurable
142
+ * there. `win.stats()` therefore reports these two timings only for a frame
143
+ * captured with `XT_GUI_PERF` set. */
144
+ class PerfScopeActive {
145
+ public:
146
+ explicit PerfScopeActive(double *slot) {
147
+ if (!xt_perf_active()) return;
148
+ slot_ = slot;
149
+ start_ = SDL_GetPerformanceCounter();
150
+ }
151
+ ~PerfScopeActive() {
152
+ if (slot_ != nullptr) *slot_ += xt_perf_ms_since(start_);
153
+ }
154
+ PerfScopeActive(const PerfScopeActive &) = delete;
155
+ PerfScopeActive &operator=(const PerfScopeActive &) = delete;
156
+
157
+ private:
158
+ double *slot_ = nullptr;
159
+ uint64_t start_ = 0;
160
+ };
161
+
125
162
  } // namespace xtgui
126
163
 
127
164
  #endif // XT_GUI_PERF_H
@@ -155,6 +155,7 @@ void finalizeStyle(XtStyle *style, const ApplyContext &context) {
155
155
  case Unit::Rem:
156
156
  case Unit::Vw:
157
157
  case Unit::Vh:
158
+ case Unit::Ch:
158
159
  case Unit::Clamp:
159
160
  case Unit::Min:
160
161
  case Unit::Max:
@@ -176,8 +177,10 @@ void inheritFrom(XtStyle *style, const XtStyle *parent) {
176
177
  style->visible = parent->visible;
177
178
  style->pointer_events = parent->pointer_events;
178
179
  /* Most documents define no custom properties; copying the (empty) map for
179
- * every element is pure overhead. */
180
- if (!parent->variables.empty()) style->variables = parent->variables;
180
+ * every element is pure overhead. When they do, the map is shared: an
181
+ * inherited style only bumps a reference count, and a node that declares its
182
+ * own `--x` copies it once. */
183
+ if (parent->variables) style->variables = parent->variables;
181
184
  style->font_size = parent->font_size;
182
185
  style->font_weight = parent->font_weight;
183
186
  style->font_family = parent->font_family;
@@ -204,14 +207,25 @@ void applyCandidates(XtStyle *style, std::vector<Candidate> *candidates,
204
207
  * then available to every normal declaration on this element. */
205
208
  for (const Candidate &candidate : *candidates) {
206
209
  if (isCustomProperty(candidate.declaration->property)) {
207
- style->variables[candidate.declaration->property] = candidate.declaration->value;
210
+ /* Copy-on-write: the inherited map is shared with the parent (and every
211
+ * sibling that inherits it), so the first declaration on this node forks
212
+ * it instead of mutating the parent's set. */
213
+ if (!style->variables || style->variables.use_count() > 1) {
214
+ auto forked = std::make_shared<std::unordered_map<std::string, std::string>>();
215
+ if (style->variables) *forked = *style->variables;
216
+ style->variables = std::move(forked);
217
+ }
218
+ (*style->variables)[candidate.declaration->property] = candidate.declaration->value;
208
219
  }
209
220
  }
221
+ static const std::unordered_map<std::string, std::string> kEmptyVariables;
210
222
  auto resolvedValue = [&](const Candidate &candidate, std::string *out) {
211
223
  *out = candidate.declaration->value;
212
224
  if (out->find("var(") != std::string::npos) {
225
+ const std::unordered_map<std::string, std::string> &variables =
226
+ style->variables ? *style->variables : kEmptyVariables;
213
227
  std::string resolved;
214
- if (!substituteVariables(*out, style->variables, 0, &resolved)) return false;
228
+ if (!substituteVariables(*out, variables, 0, &resolved)) return false;
215
229
  *out = resolved;
216
230
  }
217
231
  return true;
@@ -472,7 +486,14 @@ void computeNode(const Node *node, const XtStyle *parent, const StyleSheet &shee
472
486
  ComputeScratch *scratch) {
473
487
  const XtStyle *childParent = parent;
474
488
  if (node->isElement() && !node->is_pseudo) {
475
- XtStyle style = xt_style_initial();
489
+ /* Compute into the map entry directly. Copying the finished style into
490
+ * `out[node]` duplicated every vector/string/map it owns — on a HUD-sized
491
+ * document that was one full XtStyle copy per element per restyle, for a
492
+ * value that is immediately overwritten again on the next restyle.
493
+ * `unordered_map` references stay valid across rehash (node-based), which
494
+ * is what `box->style` pointers rely on. */
495
+ XtStyle &style = out[node];
496
+ style = xt_style_initial();
476
497
  if (parent != nullptr) {
477
498
  inheritFrom(&style, parent);
478
499
  } else {
@@ -512,11 +533,8 @@ void computeNode(const Node *node, const XtStyle *parent, const StyleSheet &shee
512
533
  if (!style.transitions.empty()) scratch->has_transitions = true;
513
534
  if (!style.animations.empty()) scratch->has_animations = true;
514
535
 
515
- /* `emplace` keeps the address of an existing entry stable, which is what
516
- * `box->style` pointers rely on. */
517
- out[node] = style;
518
- childParent = &out[node];
519
- const XtStyle &computed = out[node];
536
+ childParent = &style;
537
+ const XtStyle &computed = style;
520
538
  if (pseudos != nullptr && sheet.has_pseudo_elements) {
521
539
  computePseudo(node, PseudoElement::Before, computed, sheet, applies, out, context, state,
522
540
  pseudos, scratch);
@@ -247,12 +247,21 @@ struct Edges {
247
247
  void set(Length value) { top = right = bottom = left = value; }
248
248
  };
249
249
 
250
+ /** CSS custom properties (`--name`) an element inherits.
251
+ *
252
+ * The map is shared by every style that inherits the same set: a token-heavy
253
+ * `:root` block (the common design-system pattern) was otherwise deep-copied
254
+ * into every element on every restyle, which dominated the cascade on such
255
+ * pages. A node that declares its own custom property copies the map once
256
+ * (copy-on-write); readers only ever see a const reference. */
257
+ using StyleVariables = std::shared_ptr<std::unordered_map<std::string, std::string>>;
258
+
250
259
  struct XtStyle {
251
260
  Display display = Display::Inline;
252
261
  BoxSizing box_sizing = BoxSizing::ContentBox;
253
262
  /* CSS custom properties (`--name`) are inherited and substituted into the
254
263
  * values of the other declarations during the cascade. */
255
- std::unordered_map<std::string, std::string> variables;
264
+ StyleVariables variables;
256
265
  Position position = Position::Static;
257
266
  Edges margin;
258
267
  /** Which margins were explicitly declared `auto` (the `Edges` default unit is
@@ -73,6 +73,10 @@ float xt_style_resolve_absolute(const Length &length, float base, float em, floa
73
73
  return vw * length.value / 100.0f;
74
74
  case Unit::Vh:
75
75
  return vh * length.value / 100.0f;
76
+ /* `ch` is the advance of "0". Without the font's glyph metrics here, half an
77
+ * em is the conventional approximation for a proportional face. */
78
+ case Unit::Ch:
79
+ return em * length.value * 0.5f;
76
80
  default:
77
81
  return 0.0f;
78
82
  }
@@ -53,6 +53,8 @@ static std::string formatLength(const Length &length) {
53
53
  return formatNumber(length.value) + "vw";
54
54
  case Unit::Vh:
55
55
  return formatNumber(length.value) + "vh";
56
+ case Unit::Ch:
57
+ return formatNumber(length.value) + "ch";
56
58
  case Unit::Clamp:
57
59
  case Unit::Min:
58
60
  case Unit::Max: {
@@ -356,7 +358,11 @@ std::string xt_style_property_to_string(const XtStyle &style, const std::string
356
358
  if (property == "justify-content") return justifyNames[(int)style.justify_content];
357
359
  if (property == "align-items") return alignNames[(int)style.align_items];
358
360
  if (property == "align-content") return alignContentNames[(int)style.align_content];
359
- if (property == "gap") return formatLength(style.gap);
361
+ if (property == "gap") {
362
+ std::string row = formatLength(style.row_gap);
363
+ std::string column = formatLength(style.column_gap);
364
+ return row == column ? row : row + " " + column;
365
+ }
360
366
  if (property == "row-gap") return formatLength(style.row_gap);
361
367
  if (property == "column-gap") return formatLength(style.column_gap);
362
368
  if (property == "float") return floatNames[(int)style.float_side];