Loading...
Searching...
No Matches
CAPI_Renderer.h
Go to the documentation of this file.
1/**************************************************************************************************
2 * This file is a part of Ultralight. *
3 * *
4 * See <https://ultralig.ht> for licensing and more. *
5 * *
6 * (C) 2024 Ultralight, Inc. *
7 **************************************************************************************************/
8
9///
10/// @file CAPI_Renderer.h
11///
12/// Core renderer singleton for the library, coordinates all library functions.
13///
14/// `#include <Ultralight/CAPI/CAPI_Renderer.h>`
15///
16/// The Renderer class is responsible for creating and painting Views, managing Sessions, as well
17/// as coordinating network requests, events, JavaScript execution, and more.
18///
19/// ## Creating the Renderer
20///
21/// @note A Renderer will be created for you automatically when you call ulCreateApp (access it
22/// via ulAppGetRenderer()).
23///
24/// @note ulCreateApp() is part of the AppCore API and automatically manages window creation, run
25/// loop, input, painting, and most platform-specific functionality. (Available on desktop
26/// platforms only)
27/// \endparblock
28///
29/// ### Defining Platform Handlers
30///
31/// Before creating the Renderer, you should define your platform handlers via the Platform
32/// singleton (see CAPI_Platform.h). This can be used to customize file loading, font loading,
33/// clipboard access, and other functionality typically provided by the OS.
34///
35/// Default implementations for most platform handlers are available in the
36/// [AppCore repo](https://github.com/ultralight-ux/AppCore/tree/master/src). You can use these
37/// stock implementations by copying the code into your project, or you can write your own.
38///
39/// At a minimum, you should provide a ULFileSystem and ULFontLoader otherwise Renderer creation will
40/// fail.
41///
42/// ### Creating the Renderer
43///
44/// Once you've set up the Platform handlers you can create the Renderer by calling
45/// `ulCreateRenderer()`.
46///
47/// @par Example creation code
48/// ```
49/// // Setup our config.
50/// ULConfig config = ulCreateConfig();
51///
52/// // Use AppCore's font loader.
53/// ulEnablePlatformFontLoader();
54///
55/// // Use AppCore's file system to load file:/// URLs from the OS.
56/// ULString base_dir = ulCreateString("./assets/");
57/// ulEnablePlatformFileSystem(base_dir);
58/// ulDestroyString(base_dir);
59///
60/// // Create the renderer.
61/// ULRenderer renderer = ulCreateRenderer(config);
62///
63/// // Destroy the config.
64/// ulDestroyConfig(config);
65///
66/// // Set up Views here...
67/// ```
68///
69/// ## Updating Renderer Logic
70///
71/// You should call ulUpdate() from your main update loop as often as possible to give the
72/// library an opportunity to dispatch events and timers:
73///
74/// @par Example update code
75/// ```
76/// void mainLoop()
77/// {
78/// while(true)
79/// {
80/// // Update program logic here
81/// ulUpdate(renderer);
82/// }
83/// }
84/// ```
85///
86/// ## Rendering Each Frame
87///
88/// When your program is ready to display a new frame (usually in synchrony with the monitor
89/// refresh rate), you should call `ulRefreshDisplay()` and `ulRender` so the
90/// library can render all active Views as needed.
91///
92/// @par Example per-frame render code
93/// ```
94/// void displayFrame()
95/// {
96/// // Notify the renderer that the main display has refreshed. This will update animations,
97/// // smooth scroll, and window.requestAnimationFrame() for all Views matching the display id.
98/// ulRefreshDisplay(renderer, 0);
99///
100/// // Render all Views as needed
101/// ulRender(renderer);
102///
103/// // Each View will render to a
104/// // - Pixel-Buffer Surface (ulViewGetSurface())
105/// // or
106/// // - GPU texture (ulViewGetRenderTarget())
107/// // based on whether CPU or GPU rendering is used.
108/// //
109/// // You will need to display the image data here as needed.
110/// }
111/// }
112/// ```
113///
114#ifndef ULTRALIGHT_CAPI_RENDERER_H
115#define ULTRALIGHT_CAPI_RENDERER_H
116
118
119#ifdef __cplusplus
120extern "C" {
121#endif
122
123///
124/// Create the core renderer singleton for the library.
125///
126/// You should set up the Platform singleton (see CAPI_Platform.h) before calling this function.
127///
128/// @note You do not need to the call this if you're using ulCreateApp() from AppCore.
129///
130/// \parblock
131/// @warning You'll need to define a ULFontLoader and ULFileSystem within the Platform singleton
132/// or else this call will fail.
133/// \endparblock
134///
135/// \parblock
136/// @warning You should only create one Renderer during the lifetime of your program.
137/// \endparblock
138///
139/// @param config The configuration to use for the renderer.
140///
141/// @return Returns the new renderer instance.
142///
144
145///
146/// Destroy the renderer.
147///
148/// @param renderer The renderer instance to destroy.
149///
151
152///
153/// Update timers and dispatch internal callbacks (JavaScript and network).
154///
155/// @param renderer The active renderer instance.
156///
158
159///
160/// Notify the renderer that a display has refreshed (you should call this after vsync).
161///
162/// This updates animations, smooth scroll, and window.requestAnimationFrame() for all Views
163/// matching the display id.
164///
165/// @param renderer The active renderer instance.
166///
167/// @param display_id The display ID to refresh (0 by default).
168///
169ULExport void ulRefreshDisplay(ULRenderer renderer, unsigned int display_id);
170
171///
172/// Render all active Views to their respective surfaces and render targets.
173///
174/// @param renderer The active renderer instance.
175///
177
178///
179/// Attempt to release as much memory as possible. Don't call this from any callbacks or driver
180/// code.
181///
182/// @param renderer The active renderer instance.
183///
185
186///
187/// Print detailed memory usage statistics to the log. (@see ulPlatformSetLogger)
188///
189/// @param renderer The active renderer instance.
190///
192
193///
194/// Start the remote inspector server.
195///
196/// While the remote inspector is active, Views that are loaded into this renderer
197/// will be able to be remotely inspected from another Ultralight instance either locally
198/// (another app on same machine) or remotely (over the network) by navigating a View to:
199///
200/// \code
201/// inspector://<ADDRESS>:<PORT>
202/// \endcode
203///
204/// @param renderer The active renderer instance.
205///
206/// @param address The address for the server to listen on (eg, "127.0.0.1")
207///
208/// @param port The port for the server to listen on (eg, 9222)
209///
210/// @return Returns whether the server started successfully or not.
211///
212ULExport bool ulStartRemoteInspectorServer(ULRenderer renderer, const char* address,
213 unsigned short port);
214
215///
216/// Describe the details of a gamepad, to be used with ulFireGamepadEvent and related
217/// events below. This can be called multiple times with the same index if the details change.
218///
219/// @param renderer The active renderer instance.
220///
221/// @param index The unique index (or "connection slot") of the gamepad. For example,
222/// controller #1 would be "1", controller #2 would be "2" and so on.
223///
224/// @param id A string ID representing the device, this will be made available
225/// in JavaScript as gamepad.id
226///
227/// @param axis_count The number of axes on the device.
228///
229/// @param button_count The number of buttons on the device.
230///
231ULExport void ulSetGamepadDetails(ULRenderer renderer, unsigned int index, ULString id,
232 unsigned int axis_count, unsigned int button_count);
233
234///
235/// Fire a gamepad event (connection / disconnection).
236///
237/// @note The gamepad should first be described via ulSetGamepadDetails before calling this
238/// function.
239///
240/// @param renderer The active renderer instance.
241///
242/// @param evt The event to fire.
243///
244/// @see <https://developer.mozilla.org/en-US/docs/Web/API/Gamepad>
245///
247
248///
249/// Fire a gamepad axis event (to be called when an axis value is changed).
250///
251/// @note The gamepad should be connected via a previous call to ulFireGamepadEvent.
252///
253/// @param renderer The active renderer instance.
254///
255/// @param evt The event to fire.
256///
257/// @see <https://developer.mozilla.org/en-US/docs/Web/API/Gamepad/axes>
258///
260
261///
262/// Fire a gamepad button event (to be called when a button value is changed).
263///
264/// @note The gamepad should be connected via a previous call to ulFireGamepadEvent.
265///
266/// @param renderer The active renderer instance.
267///
268/// @param evt The event to fire.
269///
270/// @see <https://developer.mozilla.org/en-US/docs/Web/API/Gamepad/buttons>
271///
273
274#ifdef __cplusplus
275} // extern "C"
276#endif
277
278#endif // ULTRALIGHT_CAPI_RENDERER_H
Various defines and utility functions for the C API.
struct C_GamepadButtonEvent * ULGamepadButtonEvent
Definition CAPI_Defines.h:72
struct C_String * ULString
Definition CAPI_Defines.h:65
struct C_Config * ULConfig
Definition CAPI_Defines.h:59
#define ULExport
Definition CAPI_Defines.h:38
struct C_GamepadAxisEvent * ULGamepadAxisEvent
Definition CAPI_Defines.h:71
struct C_Renderer * ULRenderer
Definition CAPI_Defines.h:60
struct C_GamepadEvent * ULGamepadEvent
Definition CAPI_Defines.h:70
ULExport void ulFireGamepadEvent(ULRenderer renderer, ULGamepadEvent evt)
Fire a gamepad event (connection / disconnection).
ULExport void ulFireGamepadButtonEvent(ULRenderer renderer, ULGamepadButtonEvent evt)
Fire a gamepad button event (to be called when a button value is changed).
ULExport void ulRefreshDisplay(ULRenderer renderer, unsigned int display_id)
Notify the renderer that a display has refreshed (you should call this after vsync).
ULExport void ulLogMemoryUsage(ULRenderer renderer)
Print detailed memory usage statistics to the log.
ULExport void ulSetGamepadDetails(ULRenderer renderer, unsigned int index, ULString id, unsigned int axis_count, unsigned int button_count)
Describe the details of a gamepad, to be used with ulFireGamepadEvent and related events below.
ULExport ULRenderer ulCreateRenderer(ULConfig config)
Create the core renderer singleton for the library.
ULExport bool ulStartRemoteInspectorServer(ULRenderer renderer, const char *address, unsigned short port)
Start the remote inspector server.
ULExport void ulRender(ULRenderer renderer)
Render all active Views to their respective surfaces and render targets.
ULExport void ulFireGamepadAxisEvent(ULRenderer renderer, ULGamepadAxisEvent evt)
Fire a gamepad axis event (to be called when an axis value is changed).
ULExport void ulPurgeMemory(ULRenderer renderer)
Attempt to release as much memory as possible.
ULExport void ulDestroyRenderer(ULRenderer renderer)
Destroy the renderer.
ULExport void ulUpdate(ULRenderer renderer)
Update timers and dispatch internal callbacks (JavaScript and network).