| 1 | // The renderer trust domain of docs/protocol.md on litehtml: validated |
| 2 | // RenderOps in, layout answers and WidgetEvents out, a DisplayList on request. |
| 3 | // The C++ twin of renderer-blitz/src/renderer.rs with one structural |
| 4 | // difference: litehtml has no DOM mutation API, so the ops maintain a shadow |
| 5 | // tree here, every batch serialises it to HTML and the document is parsed and |
| 6 | // laid out again (the render()→string model). Nothing here parses untrusted HTML |
| 7 | // (the string is generated from validated ops) or runs script. |
| 8 | // |
| 9 | // One Renderer per widget, every call on the platform's main thread (the glue |
| 10 | // guarantees it), so the document needs no lock. Coordinates are CSS px. |
| 11 | #pragma once |
| 12 | |
| 13 | #include "animations.h" |
| 14 | #include "backend.h" |
| 15 | #include "canvas.h" |
| 16 | #include "compositor.h" |
| 17 | #include "display_list.h" |
| 18 | #include "twidget/json.h" |
| 19 | |
| 20 | #include <cstdint> |
| 21 | #include <limits> |
| 22 | #include <map> |
| 23 | #include <memory> |
| 24 | #include <string> |
| 25 | #include <unordered_map> |
| 26 | #include <unordered_set> |
| 27 | #include <vector> |
| 28 | |
| 29 | namespace twlite { |
| 30 | |
| 31 | class Container; |
| 32 | |
| 33 | /// Timings of the last rebuild (serialise → parse+layout, per pass). |
| 34 | struct BuildStats { |
| 35 | double serialize_ms = 0, parse_ms = 0; |
| 36 | int passes = 0; |
| 37 | size_t html_bytes = 0; |
| 38 | int builds = 0; |
| 39 | /// Builds that patched the previous document instead of parsing a new one (TW_LITEHTML_TWIDGET). |
| 40 | int patched = 0; |
| 41 | }; |
| 42 | |
| 43 | class Renderer : private CanvasHost { |
| 44 | public: |
| 45 | /// `backend` is owned by the platform layer and must outlive the renderer. |
| 46 | Renderer(double width, double dpr, bool dark, Backend& backend); |
| 47 | ~Renderer(); |
| 48 | Renderer(const Renderer&) = delete; |
| 49 | Renderer& operator=(const Renderer&) = delete; |
| 50 | |
| 51 | /// Host → renderer message (parsed JSON). Returns renderer → host messages. |
| 52 | std::vector<twidget::Json> receive(const twidget::Json& msg); |
| 53 | void set_width(double width, std::vector<twidget::Json>& out); |
| 54 | /// The chat theme changed: `prefers-color-scheme` inside the document follows. |
| 55 | void set_theme(bool dark, std::vector<twidget::Json>& out); |
| 56 | /// A pointer from the host view, in CSS px. `action`: 0 down, 1 move, 2 up, 3 cancel. A mouse |
| 57 | /// sends no touch events; `modifiers` is bit0 alt, bit1 ctrl, bit2 meta, bit3 shift. |
| 58 | std::vector<twidget::Json> touch(int action, double x, double y, int pointer, bool primary, bool mouse = false, int modifiers = 0); |
| 59 | /// A mouse over the view with no button down (`action` 0), or leaving it (1): hover state and |
| 60 | /// the over/out/enter/leave events. |
| 61 | std::vector<twidget::Json> hover(int action, double x, double y, int modifiers = 0); |
| 62 | /// A wheel or trackpad scroll over the view. `mode`: 0 pixels, 1 lines, 2 pages. |
| 63 | std::vector<twidget::Json> wheel(double x, double y, double dx, double dy, int mode, int modifiers = 0); |
| 64 | /// `type` keydown or keyup, with DOM `key` and `code`; the target is the body (no focus model). |
| 65 | std::vector<twidget::Json> key(const std::string& type, const std::string& key, const std::string& code, bool repeat, int modifiers); |
| 66 | /// One display frame: coalesced move events go out and animations advance. Returns renderer → host messages. |
| 67 | std::vector<twidget::Json> tick(); |
| 68 | /// Something waits for the next frame: a running animation or a coalesced move. |
| 69 | bool wants_tick() const; |
| 70 | /// When `wants_tick()` is false: seconds until `tick()` is due anyway (an animation event, the |
| 71 | /// end of a layer's track); infinity when nothing is scheduled. |
| 72 | double next_tick_in() const; |
| 73 | |
| 74 | /// Lets the platform's compositor run transform and opacity animations (compositor.h). Off by |
| 75 | /// default: a platform that never turns it on gets every frame painted, as before. |
| 76 | void set_compositing(bool enabled); |
| 77 | /// The layers that go above the display list `paint()` returned last. |
| 78 | const Composition& composition() const { return composition_; } |
| 79 | /// The renderer clock, in seconds: what `LayerTrack::start` is on. |
| 80 | static double clock() { return now_s(); } |
| 81 | /// A backend that decodes images asynchronously (the web) reports the size of image `id` here; |
| 82 | /// the document is rebuilt with it. Returns renderer → host messages (a `size`, when it changed). |
| 83 | std::vector<twidget::Json> image_ready(int id, float w, float h); |
| 84 | |
| 85 | /// Viewport in CSS px (the height the document converged to) and in physical px. |
| 86 | double width() const { return width_; } |
| 87 | int viewport_height() const { return vp_height_; } |
| 88 | double dpr() const { return dpr_; } |
| 89 | uint32_t px_width() const; |
| 90 | uint32_t px_height() const; |
| 91 | bool dirty() const { return dirty_; } |
| 92 | /// Records the current document into a display list (clears `dirty`). |
| 93 | DisplayList paint(); |
| 94 | const BuildStats& stats() const { return stats_; } |
| 95 | /// The HTML of the last build (diagnostics). |
| 96 | const std::string& last_html() const { return html_; } |
| 97 | |
| 98 | private: |
| 99 | friend class Container; |
| 100 | struct Node; |
| 101 | struct Touch { |
| 102 | double x0, y0, x, y; |
| 103 | int64_t target; |
| 104 | Rect target_rect; |
| 105 | bool moved; |
| 106 | bool primary; |
| 107 | int pointer; |
| 108 | bool mouse; |
| 109 | int modifiers; |
| 110 | }; |
| 111 | /// What an input event carries besides its type and target. |
| 112 | struct Input { |
| 113 | double x = 0, y = 0; |
| 114 | int pointer = 0; |
| 115 | bool primary = true; |
| 116 | bool mouse = false; |
| 117 | int buttons = 0; |
| 118 | int modifiers = 0; |
| 119 | int detail = 0; |
| 120 | int64_t related = 0; |
| 121 | }; |
| 122 | |
| 123 | // ops → shadow tree |
| 124 | void apply_op(const twidget::Json& op, std::vector<twidget::Json>& out); |
| 125 | Node* node(int64_t id) const; |
| 126 | void adopt(int64_t id, Node* n, const twidget::Json& attrs); |
| 127 | void detach(Node* n); |
| 128 | void forget(Node* n); |
| 129 | // shadow tree → HTML → litehtml |
| 130 | std::string serialize() const; |
| 131 | void serialize_node(const Node* n, std::string& out) const; |
| 132 | void rebuild(); |
| 133 | void report_size(std::vector<twidget::Json>& out); |
| 134 | // queries & events |
| 135 | twidget::Json answer(const twidget::Json& q); |
| 136 | bool match_media(const std::string& query) const; |
| 137 | int64_t widget_id_at(double x, double y, Rect* placement) const; |
| 138 | bool wants_gesture(int64_t target) const; |
| 139 | void emit_event(const char* type, int64_t target, const Rect& target_rect, const Touch& t, int action, std::vector<twidget::Json>& out); |
| 140 | twidget::Json event_json(const char* type, int64_t target, const Rect& target_rect, const Input& in) const; |
| 141 | /// Sends `ev` if the sandbox listens for its type; move-type events wait for the next tick, the |
| 142 | /// latest one per type and pointer. |
| 143 | void dispatch(twidget::Json ev, std::vector<twidget::Json>& out); |
| 144 | void flush_moves(std::vector<twidget::Json>& out); |
| 145 | void flush_pending(std::vector<twidget::Json>& out); |
| 146 | bool is_ancestor(int64_t ancestor, int64_t id) const; |
| 147 | std::vector<int64_t> chain(int64_t id) const; |
| 148 | // shadow tree → style attribute, with animation overrides |
| 149 | void write_style(const Node* n, std::string& out) const; |
| 150 | // animations |
| 151 | static double now_s(); |
| 152 | void build_document(); |
| 153 | void animate(); |
| 154 | void apply_overrides(StyleOverrides sampled); |
| 155 | void queue_animation_events(); |
| 156 | // compositor layers |
| 157 | struct Promotion { |
| 158 | Rect box; |
| 159 | Rect reach; // what the layer can cover over its whole track |
| 160 | float raster_scale = 1; |
| 161 | bool has_clip = false; |
| 162 | Rect clip; |
| 163 | Radii clip_radii; |
| 164 | uint64_t signature = 0; |
| 165 | uint64_t track_version = 0; |
| 166 | LayerTrack track; |
| 167 | size_t order = 0; |
| 168 | }; |
| 169 | void update_composition(double now); |
| 170 | /// Drops what the layers animate from `sampled`: the compositor shows it, the document must not. |
| 171 | void strip_layers(StyleOverrides& sampled) const; |
| 172 | // canvas |
| 173 | Canvas* canvas_for(Node* n); |
| 174 | void run_canvas(Canvas& canvas, const twidget::Json& cmds); |
| 175 | void forget_canvas(int64_t id); |
| 176 | int canvas_font(const std::string& css, FontMetrics& metrics) override; |
| 177 | float canvas_text_width(int font, const std::string& text) override; |
| 178 | bool canvas_source(int64_t node, int& image, int& canvas, float& w, float& h) override; |
| 179 | int64_t canvas_allowance(int id) override; |
| 180 | void freeze(const std::string& reason, bool fatal); |
| 181 | /// The host's theme (theme.h): the root's variables and the master CSS's UA layer with their values. |
| 182 | void apply_theme(const twidget::Json& theme); |
| 183 | |
| 184 | std::unique_ptr<Container> container_; |
| 185 | Backend& backend_; |
| 186 | double width_; |
| 187 | double dpr_; |
| 188 | bool dark_; |
| 189 | int vp_height_; |
| 190 | double doc_height_ = 0; |
| 191 | bool dirty_ = true; |
| 192 | std::string html_; |
| 193 | /// The host's theme variables (theme.h), written first and !important into <html>'s style. |
| 194 | std::vector<std::pair<std::string, std::string>> theme_vars_; |
| 195 | /// The host's last theme message and content safe area (docs/sdk-v1.md §2.17), both in theme_vars_. |
| 196 | twidget::Json theme_; |
| 197 | std::vector<std::pair<std::string, std::string>> safe_area_vars_; |
| 198 | BuildStats stats_; |
| 199 | std::vector<std::string> pending_violations_; |
| 200 | |
| 201 | /// Widget nodes by id. html/head/body are fixed nodes owned below and reachable through `aliases_` once adopted. |
| 202 | std::unordered_map<int64_t, std::unique_ptr<Node>> nodes_; |
| 203 | std::unordered_map<int64_t, Node*> aliases_; |
| 204 | std::unique_ptr<Node> html_node_, head_node_, body_node_; |
| 205 | std::unordered_set<std::string> interest_; |
| 206 | std::unordered_set<std::string> prevent_; |
| 207 | bool frozen_ = false; |
| 208 | std::string frozen_reason_; |
| 209 | bool frozen_fatal_ = false; |
| 210 | std::string last_size_; |
| 211 | std::unordered_map<int, Touch> touches_; |
| 212 | int64_t hover_ = 0; |
| 213 | double hover_x_ = 0, hover_y_ = 0; |
| 214 | int click_count_ = 0; |
| 215 | double last_click_ms_ = -1e9, last_click_x_ = 0, last_click_y_ = 0; |
| 216 | std::vector<std::pair<std::string, twidget::Json>> pending_moves_; // key "type:pointer", latest event |
| 217 | std::vector<twidget::Json> pending_events_; |
| 218 | |
| 219 | Animator animator_; |
| 220 | StyleOverrides overrides_; |
| 221 | std::string sheets_key_; |
| 222 | bool animating_ = false; // inside animate(): its own rebuild must not re-enter |
| 223 | |
| 224 | bool compositing_ = false; |
| 225 | std::map<int64_t, Promotion> promoted_; |
| 226 | std::unordered_set<int64_t> layer_ids_; |
| 227 | Composition composition_; |
| 228 | double wake_at_ = std::numeric_limits<double>::infinity(); |
| 229 | uint64_t composition_key_ = 0; |
| 230 | uint64_t next_version_ = 1; |
| 231 | |
| 232 | std::unordered_map<int64_t, std::unique_ptr<Canvas>> canvases_; |
| 233 | std::unordered_map<std::string, std::pair<int, FontMetrics>> canvas_fonts_; |
| 234 | std::string binary_; // the binary payload of the query being answered (imageData), base64 |
| 235 | }; |
| 236 | |
| 237 | /// Rewrites `prefers-color-scheme` media queries into width queries litehtml can |
| 238 | /// evaluate (`(min-width:0px)` = always, `(min-width:999999px)` = never), since |
| 239 | /// litehtml knows no colour-scheme feature. Exposed for the unit test. |
| 240 | std::string rewrite_color_scheme(const std::string& css, bool dark); |
| 241 | |
| 242 | } // namespace twlite |
| 243 | |