|
Ultralight C++ API 2.0.0
|
#include <Ultralight/Renderer.h>
Core renderer singleton for the library, coordinates all library functions.
The Renderer class is responsible for creating and painting Views, managing Sessions, as well as coordinating network requests, events, JavaScript execution, and more.
Before creating the Renderer, you should define your platform handlers via the Platform singleton. This can be used to customize file loading, font loading, clipboard access, and other functionality typically provided by the OS.
Default implementations for most platform handlers ship as Zlib-licensed source in the SDK's platform folder. You can use these stock implementations by copying the code into your project, or you can write your own.
At a minimum, you must provide a FileSystem and a FontLoader. Without them, the library exits the process with an error when it first needs them.
You can configure various library options by creating a Config object and passing it to Platform::instance().set_config().
Once you've set up the Platform handlers and Config, you can create the Renderer by calling Renderer::Create(). You should store the result in a RefPtr to keep it alive.
You should call Renderer::Update() from your main update loop as often as possible to give the library an opportunity to dispatch events and timers:
When your program is ready to display a new frame (usually in sync with the monitor's refresh rate), you should call Renderer::RefreshDisplay() and Renderer::Render() so the library can render all active Views as needed.
Animations and smooth scrolling advance with each RefreshDisplay() call. Call it once per frame you present (once per display refresh for a vsynced loop, or once per rendered frame for a vsync-off or variable-refresh game loop).
Static Public Member Functions | |
| static RefPtr< Renderer > | Create () |
| Create the core renderer singleton for the library. | |
Public Member Functions | |
| virtual RefPtr< Session > | CreateSession (bool is_persistent, const String &name)=0 |
| Create a unique, named Session to store browsing data in (cookies, local storage, application cache, indexed db, etc). | |
| virtual RefPtr< Session > | default_session ()=0 |
| Get the default Session. | |
| virtual RefPtr< View > | CreateView (uint32_t width, uint32_t height, const ViewConfig &config, RefPtr< Session > session)=0 |
| Create a new View to load and display web pages in. | |
| virtual void | Update ()=0 |
| Update timers and dispatch callbacks. | |
| virtual void | RefreshDisplay (uint32_t display_id)=0 |
| Notify the renderer that a display has refreshed. | |
| virtual void | RefreshDisplay (uint32_t display_id, double target_timestamp)=0 |
| Notify the renderer that a display has refreshed, and when the resulting frame will be shown. | |
| virtual void | PostTask (void(*task)(void *user_data), void *user_data, void(*destroy_user_data)(void *user_data)=nullptr)=0 |
| Post a task to run on the Renderer's thread during a future call to Update(). | |
| virtual void | PostDelayedTask (double delay_ms, void(*task)(void *user_data), void *user_data, void(*destroy_user_data)(void *user_data)=nullptr)=0 |
| Post a task to run on the Renderer's thread once a delay has elapsed. | |
| template<typename F, typename = std::enable_if_t<std::is_invocable_v<std::decay_t<F>&>>> | |
| void | PostTask (F &&callback) |
| Post a callable to run on the Renderer's thread during a future call to Update(). | |
| template<typename F, typename = std::enable_if_t<std::is_invocable_v<std::decay_t<F>&>>> | |
| void | PostDelayedTask (double delay_ms, F &&callback) |
| Post a callable to run on the Renderer's thread once a delay has elapsed. | |
| virtual void | Render ()=0 |
| Render all active views to their respective render-targets/surfaces. | |
| virtual void | RenderOnly (View **view_array, size_t view_array_len)=0 |
| Render a subset of views to their respective surfaces and render targets. | |
| virtual void | Recycle (RecycleMode mode=RecycleMode::Lightweight)=0 |
| Recycle internal caches and reclaim memory. | |
| virtual void | Recycle (RecycleMode mode, CodeDropMode code)=0 |
| Reclaim memory, choosing how much compiled code to discard. | |
| virtual void | CollectNow (CodeDropMode code, bool sync=true)=0 |
| Collect the JavaScript heap now, leaving rendering and GPU caches intact. | |
| virtual void | PurgeMemory ()=0 |
| Attempt to release as much memory as possible. | |
| virtual void | LogMemoryUsage ()=0 |
| Print detailed memory usage statistics to the log. | |
| virtual String | GetMemoryUsage ()=0 |
| Get formatted memory usage statistics as a string. | |
| virtual void | set_system_color_scheme (ColorScheme scheme)=0 |
| Set the system color scheme that Views report to pages via the prefers-color-scheme CSS media feature. | |
| virtual ColorScheme | system_color_scheme () const =0 |
| The current system color scheme. | |
| virtual bool | StartRemoteInspectorServer (const char *address, uint16_t port)=0 |
| Start the remote inspector server. | |
| virtual void | SetGamepadDetails (uint32_t index, const String &id, uint32_t axis_count, uint32_t button_count)=0 |
| Describe the details of a gamepad, to be used with FireGamepadEvent and related events below. | |
| virtual void | FireGamepadEvent (const GamepadEvent &evt)=0 |
| Fire a gamepad event (connection / disconnection). | |
| virtual void | FireGamepadAxisEvent (const GamepadAxisEvent &evt)=0 |
| Fire a gamepad axis event (to be called when an axis value is changed). | |
| virtual void | FireGamepadButtonEvent (const GamepadButtonEvent &evt)=0 |
| Fire a gamepad button event (to be called when a button value is changed). | |
| virtual bool | BeginRenderTrace (const char *output_path, bool verbose=true)=0 |
| Begin recording a render trace to the specified file path. | |
| virtual void | EndRenderTrace ()=0 |
| End the current render trace. | |
| virtual bool | is_render_trace_active () const =0 |
| Whether or not a render trace is currently being recorded. | |
| virtual void | set_gc_listener (GCListener *listener)=0 |
| Set a GCListener to observe and control the Renderer's JavaScript garbage collections. | |
| virtual GCListener * | gc_listener () const =0 |
| Get the attached GCListener, if any. | |
| virtual GCInfo | gc_status () const =0 |
| Get a snapshot of the current JS-heap reclamation state. | |
| virtual void | SetDisplayRefreshRate (uint32_t display_id, double refresh_rate)=0 |
| Declare a display's refresh rate. | |
| virtual double | display_refresh_rate (uint32_t display_id) const =0 |
| Get the refresh rate declared for a display. | |
| virtual void | SetDisplayUsesCustomClock (uint32_t display_id, bool uses_custom_clock)=0 |
| Declare that a display's refresh timestamps are on your own timeline rather than the system clock. | |
| virtual bool | display_uses_custom_clock (uint32_t display_id) const =0 |
| Get whether a display was declared to use a custom clock. | |
| Public Member Functions inherited from RefCounted | |
| virtual void | AddRef () const =0 |
| Increment the reference count (thread-safe). | |
| virtual void | Release () const =0 |
| Decrement the reference count (thread-safe). | |
| virtual int | ref_count () const =0 |
| Get the current reference count. | |
| virtual WeakControlBlock * | weak_control_block () const |
| Get the control block used to track weak references to this object. | |
Protected Member Functions | |
| virtual | ~Renderer () |
| Protected Member Functions inherited from RefCounted | |
| virtual | ~RefCounted () |
|
protectedvirtual |
|
pure virtual |
Begin recording a render trace to the specified file path.
The trace file captures detailed rendering pipeline diagnostics in Perfetto format, viewable at https://ui.perfetto.dev. This operates independently of the Profiler– both can be active simultaneously.
| output_path | Path to the output .perfetto-trace file. |
| verbose | If true, captures detailed annotations in addition to scope timing. |
|
pure virtual |
Collect the JavaScript heap now, leaving rendering and GPU caches intact.
Unlike Recycle(), this reclaims only the JavaScript heap. It runs a garbage collection (optionally discarding compiled code, see CodeDropMode) and returns the freed memory to the system. Rendering and GPU caches are left alone, so rendering stays fast.
You can call this from GCListener::OnRequestGC() or at any moment when a short pause won't be noticed (eg, a quiet point in your update loop).
| code | How aggressively to discard compiled code (see CodeDropMode). |
| sync | If true (the default), collect synchronously before returning, briefly blocking the calling thread. If false, start a collection that completes in the background. |
Create the core renderer singleton for the library.
You should set up the Platform singleton before calling this function.
Create a unique, named Session to store browsing data in (cookies, local storage, application cache, indexed db, etc).
| is_persistent | Whether or not to store the session on disk. Persistent sessions are stored in a subfolder of Config::cache_path with this session's name. |
| name | A unique name for this session. |
|
pure virtual |
Create a new View to load and display web pages in.
Views are similar to a tab in a browser. They have certain dimensions but are rendered to an offscreen surface and must be forwarded all input events.
| width | The initial width, in pixels. |
| height | The initial height, in pixels. |
| config | Configuration details for the View. |
| session | The session to store local data in. Pass a nullptr to use the default session. |
Get the default Session.
This session is persistent (backed to disk) and has the name "default".
|
pure virtual |
Get the refresh rate declared for a display.
| display_id | The id of the display. |
|
pure virtual |
Get whether a display was declared to use a custom clock.
| display_id | The id of the display. |
|
pure virtual |
End the current render trace.
The trace file is finished and closed shortly after this returns (on the next Update()).
|
pure virtual |
Fire a gamepad axis event (to be called when an axis value is changed).
|
pure virtual |
Fire a gamepad button event (to be called when a button value is changed).
|
pure virtual |
Fire a gamepad event (connection / disconnection).
|
pure virtual |
Get the attached GCListener, if any.
|
pure virtual |
Get a snapshot of the current JS-heap reclamation state.
|
pure virtual |
Get formatted memory usage statistics as a string.
|
pure virtual |
Whether or not a render trace is currently being recorded.
|
pure virtual |
Print detailed memory usage statistics to the log.
|
inline |
Post a callable to run on the Renderer's thread once a delay has elapsed.
Convenience overload of PostDelayedTask(). See PostTask() for the callable's lifetime.
| delay_ms | Minimum delay before the callable may run, in milliseconds. |
| callback | The callable to invoke on the Renderer's thread. |
|
pure virtual |
Post a task to run on the Renderer's thread once a delay has elapsed.
The clock is the Update() cadence, not a background timer. The task runs during the first Update() at or after the deadline, so delivery waits while Update() is not being called (eg, a paused application).
Tasks whose deadlines fall in the same Update() run in posting order.
| delay_ms | Minimum delay before the task may run, in milliseconds. |
| task | Invoked once on the Renderer's thread with user_data. |
| user_data | Pointer passed through to task (can be nullptr). |
| destroy_user_data | Invoked exactly once after task runs, or without task running if the Renderer is destroyed first (can be nullptr). |
|
inline |
Post a callable to run on the Renderer's thread during a future call to Update().
Convenience overload of PostTask(). The callable is invoked once on the Renderer's thread, then destroyed (destroyed without being invoked if the Renderer is destroyed first).
Captures are the natural way to hand data across threads. You should capture by value.
| callback | The callable to invoke on the Renderer's thread. |
|
pure virtual |
Post a task to run on the Renderer's thread during a future call to Update().
You can use this to hand work from your other threads to the thread that drives the Renderer (the only thread that may touch Views and the DOM API).
Tasks run in posting order during the next Update() after they are posted. A task posted while Update() is draining tasks runs at the following Update().
| task | Invoked once on the Renderer's thread with user_data. |
| user_data | Pointer passed through to task (can be nullptr). |
| destroy_user_data | Invoked exactly once after task runs, or without task running if the Renderer is destroyed first (can be nullptr). |
|
pure virtual |
Attempt to release as much memory as possible.
This also discards all compiled JavaScript code, so pages run slower for a while as it's recompiled.
|
pure virtual |
Reclaim memory, choosing how much compiled code to discard.
Like Recycle(), but code sets how much compiled code a Full recycle discards. Discarding more frees more memory, but pages run slower for a while as their code is compiled again.
You can use this to shrink the JavaScript footprint at a moment you choose (eg, a loading screen or level transition).
| mode | See Recycle(). |
| code | How aggressively to discard compiled code (see CodeDropMode). |
|
pure virtual |
Recycle internal caches and reclaim memory.
| mode | How aggressively to reclaim memory:
|
|
pure virtual |
Notify the renderer that a display has refreshed.
Call this once per frame you present, for each display (usually right after vsync). It advances animations, smooth scrolling, and window.requestAnimationFrame() for the Views on that display, so they can repaint during the next Render().
| display_id | The id of the display that refreshed (see ViewConfig::display_id). |
|
pure virtual |
Notify the renderer that a display has refreshed, and when the resulting frame will be shown.
This works like RefreshDisplay(display_id), but animation time follows your timestamps instead of the wall clock, so animations are timed for the moment the frame reaches the screen.
| display_id | The id of the display that refreshed. |
| target_timestamp | When this frame will be shown, in seconds. Use the system's monotonic clock (eg, the target time your display link reports), or your own timeline after calling SetDisplayUsesCustomClock(). Timestamps must never decrease. |
|
pure virtual |
Render all active views to their respective render-targets/surfaces.
|
pure virtual |
Render a subset of views to their respective surfaces and render targets.
| view_array | A C-array containing a list of View pointers. |
| view_array_len | The length of the C-array. |
|
pure virtual |
Set a GCListener to observe and control the Renderer's JavaScript garbage collections.
| listener | A user-defined GCListener implementation, ownership remains with the caller. Pass a nullptr to detach the current listener. |
|
pure virtual |
Set the system color scheme that Views report to pages via the prefers-color-scheme CSS media feature.
The library never reads the OS setting itself; the scheme remains Light until this is called. A change re-evaluates prefers-color-scheme media queries in every View whose ViewConfig::preferred_color_scheme is ColorScheme::Auto; Views pinned Light or Dark are unaffected.
| scheme | ColorScheme::Light or ColorScheme::Dark. Passing ColorScheme::Auto is invalid here (a warning is logged and the value is ignored). |
|
pure virtual |
Declare a display's refresh rate.
Pages see the declared rate through window.requestAnimationFrame() scheduling. Call this when the display's mode changes (eg, a variable-rate display switching between 120 Hz and 48 Hz). Small changes are absorbed, and a change reaches pages at most once a second.
| display_id | The id of the display. |
| refresh_rate | The display mode's nominal refresh rate in Hz (eg, 60.0, 59.94, 120.0), or 0 to clear it (the library then measures how often RefreshDisplay() is called). |
|
pure virtual |
Declare that a display's refresh timestamps are on your own timeline rather than the system clock.
By default, the timestamps you pass to RefreshDisplay(display_id, target_timestamp) are treated as readings of the system's monotonic clock. Declare a custom clock when they're on a timeline of your own (eg, a media timeline for offline rendering, or simulation time): animation then follows your timestamps exactly.
| display_id | The id of the display. |
| uses_custom_clock | True if timestamps for this display are on your own timeline. |
|
pure virtual |
Describe the details of a gamepad, to be used with FireGamepadEvent and related events below.
This can be called multiple times with the same index if the details change.
| index | The gamepad's connection slot, and the array position the page sees: a gamepad described at index 1 arrives as navigator.getGamepads()[1] with gamepad.index 1, leaving slot 0 null. Number your controllers from 0. |
| id | A string ID representing the device, this will be made available in JavaScript as gamepad.id |
| axis_count | The number of axes on the device. |
| button_count | The number of buttons on the device. |
|
pure virtual |
Start the remote inspector server.
While it runs, another Ultralight app (on the same machine or over the network) can inspect this renderer's Views by loading this URL in a View:
| address | The address for the server to listen on (eg, "127.0.0.1") |
| port | The port for the server to listen on (eg, 9222) |
|
pure virtual |
|
pure virtual |
Update timers and dispatch callbacks.
You should call this as often as you can from your application's run loop.