Ultralight C++ API 1.3.0
Loading...
Searching...
No Matches
Renderer.h
Go to the documentation of this file.
1/******************************************************************************
2 * This file is a part of Ultralight, an ultra-portable web-browser engine. *
3 * *
4 * See <https://ultralig.ht> for licensing and more. *
5 * *
6 * (C) 2023 Ultralight, Inc. *
7 *****************************************************************************/
8#pragma once
10#include <Ultralight/RefPtr.h>
11#include <Ultralight/Session.h>
12#include <Ultralight/View.h>
14
15namespace ultralight {
16
17///
18/// Core renderer singleton for the library, coordinates all library functions.
19///
20/// The Renderer class is responsible for creating and painting \link View Views \endlink, managing
21/// \link Session Sessions \endlink, as well as coordinating network requests, events, JavaScript
22/// execution, and more.
23///
24/// ## Initializing the Renderer
25/// To initialize the library, you should set up the Platform singleton and call Renderer::Create.
26///
27/// @par Example initialization code
28/// ```
29/// // Get the Platform singleton (maintains global library state)
30/// auto& platform = Platform::instance();
31///
32/// // Setup config
33/// Config my_config;
34/// platform.set_config(my_config);
35///
36/// // Create platform handlers (these are the minimum required)
37/// // (This is pseudo-code, you will need to define your own)
38/// MyFileSystem* file_system = new MyFileSystem();
39/// MyFontLoader* font_loader = new MyFontLoader();
40///
41/// // Setup platform handlers
42/// platform.set_file_system(file_system);
43/// platform.set_font_loader(font_loader);
44///
45/// // Create the Renderer
46/// RefPtr<Renderer> renderer = Renderer::Create();
47///
48/// // Create Views here
49/// ```
50///
51/// ## Updating Renderer Logic
52///
53/// You should call Renderer::Update() from your main update loop as often as possible to give the
54/// library an opportunity to dispatch events and timers:
55///
56/// @par Example update code
57/// ```
58/// void mainLoop()
59/// {
60/// while(true)
61/// {
62/// // Update program logic here
63/// renderer.Update();
64/// }
65/// }
66/// ```
67///
68/// ## Rendering Each Frame
69///
70/// When your program is ready to display a new frame (usually in synchrony with the monitor
71/// refresh rate), you should call Renderer::Render() so the library can render all active
72/// \link View Views \endlink as needed.
73///
74/// @par Example rendering code
75/// ```
76/// void displayFrame()
77/// {
78/// // Render all Views as needed
79/// renderer.Render();
80///
81/// // Each View will render to a
82/// // - Pixel-Buffer Surface (View::surface())
83/// // or
84/// // - GPU texture (View::render_target())
85/// // based on whether CPU or GPU rendering is used.
86/// //
87/// // You will need to display the image data here as needed.
88/// }
89/// }
90/// ```
91///
92/// @note The App class creates and manages its own Renderer (optional API in AppCore, available on
93/// desktop OS only).
94///
95/// @warning You should only create one Renderer during the lifetime of your program.
96///
97class UExport Renderer : public RefCounted {
98 public:
99 ///
100 /// Create the core renderer singleton for the library.
101 ///
102 /// You should set up the Platform singleton before calling this function.
103 ///
104 /// @note You do not need to the call this if you're using the App class from AppCore.
105 ///
106 /// \parblock
107 /// @warning You'll need to define a FontLoader and FileSystem within the Platform singleton
108 /// or else this call will fail.
109 /// \endparblock
110 ///
111 /// \parblock
112 /// @warning You should only create one Renderer during the lifetime of your program.
113 /// \endparblock
114 ///
115 /// @return Renderer is ref-counted. This method returns a ref-pointer to a new instance, you
116 /// should store it in a RefPtr to keep the instance alive.
117 ///
119
120 ///
121 /// Create a unique, named Session to store browsing data in (cookies, local storage,
122 /// application cache, indexed db, etc).
123 ///
124 /// @note A default, persistent Session is already created for you. You only need to call this
125 /// if you want to create private, in-memory session or use a separate session for each
126 /// View.
127 ///
128 /// @param is_persistent Whether or not to store the session on disk. Persistent sessions will
129 /// be written to the path set in Config::cache_path
130 ///
131 /// @param name A unique name for this session, this will be used to generate a unique disk
132 /// path for persistent sessions.
133 ///
134 virtual RefPtr<Session> CreateSession(bool is_persistent, const String& name) = 0;
135
136 ///
137 /// Get the default Session. This session is persistent (backed to disk) and has the name
138 /// "default".
139 ///
141
142 ///
143 /// Create a new View to load and display web pages in.
144 ///
145 /// Views are similar to a tab in a browser. They have certain dimensions but are rendered to an
146 /// offscreen surface and must be forwarded all input events.
147 ///
148 /// @param width The initial width, in pixels.
149 ///
150 /// @param height The initial height, in pixels.
151 ///
152 /// @param config Configuration details for the View.
153 ///
154 /// @param session The session to store local data in. Pass a nullptr to use the default
155 /// session.
156 ///
157 /// @return Returns a ref-pointer to a new View instance.
158 ///
159 virtual RefPtr<View> CreateView(uint32_t width, uint32_t height, const ViewConfig& config,
160 RefPtr<Session> session)
161 = 0;
162
163 ///
164 /// Update timers and dispatch callbacks.
165 ///
166 /// You should call this as often as you can from your application's run loop.
167 ///
168 virtual void Update() = 0;
169
170 ///
171 /// Render all active views to their respective surfaces and render targets.
172 ///
173 /// You should call this once per frame (usually in synchrony with the monitor's refresh rate).
174 ///
175 /// @note Views are only rendered if they actually need rendering.
176 ///
177 virtual void Render() = 0;
178
179 ///
180 /// Render a subset of views to their respective surfaces and render targets.
181 ///
182 /// @param view_array A C-array containing a list of View pointers.
183 ///
184 /// @param view_array_len The length of the C-array.
185 ///
186 virtual void RenderOnly(View** view_array, size_t view_array_len) = 0;
187
188 ///
189 /// Attempt to release as much memory as possible.
190 ///
191 /// @warning Don't call this from any callbacks or driver code.
192 ///
193 virtual void PurgeMemory() = 0;
194
195 ///
196 /// Print detailed memory usage statistics to the log.
197 ///
198 /// @see Platform::set_logger
199 ///
200 virtual void LogMemoryUsage() = 0;
201
202 ///
203 /// Start the remote inspector server.
204 ///
205 /// While the remote inspector is active, Views that are loaded into this renderer
206 /// will be able to be remotely inspected from another Ultralight instance either locally
207 /// (another app on same machine) or remotely (over the network) by navigating a View to:
208 ///
209 /// \code
210 /// inspector://<ADDRESS>:<PORT>
211 /// \endcode
212 ///
213 /// @return Returns whether the server started successfully or not.
214 ///
215 virtual bool StartRemoteInspectorServer(const char* address, uint16_t port) = 0;
216
217 ///
218 /// Describe the details of a gamepad, to be used with FireGamepadEvent and related
219 /// events below. This can be called multiple times with the same index if the details change.
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 ///
231 virtual void SetGamepadDetails(uint32_t index, const String& id, uint32_t axis_count,
232 uint32_t button_count)
233 = 0;
234
235 ///
236 /// Fire a gamepad event (connection / disconnection).
237 ///
238 /// @note The gamepad should first be described via SetGamepadDetails before calling this
239 /// function.
240 ///
241 /// @see <https://developer.mozilla.org/en-US/docs/Web/API/Gamepad>
242 ///
243 virtual void FireGamepadEvent(const GamepadEvent& evt) = 0;
244
245 ///
246 /// Fire a gamepad axis event (to be called when an axis value is changed).
247 ///
248 /// @note The gamepad should be connected via a previous call to FireGamepadEvent.
249 ///
250 /// @see <https://developer.mozilla.org/en-US/docs/Web/API/Gamepad/axes>
251 ///
252 virtual void FireGamepadAxisEvent(const GamepadAxisEvent& evt) = 0;
253
254 ///
255 /// Fire a gamepad button event (to be called when a button value is changed).
256 ///
257 /// @note The gamepad should be connected via a previous call to FireGamepadEvent.
258 ///
259 /// @see <https://developer.mozilla.org/en-US/docs/Web/API/Gamepad/buttons>
260 ///
261 virtual void FireGamepadButtonEvent(const GamepadButtonEvent& evt) = 0;
262
263 protected:
264 virtual ~Renderer();
265};
266
267} // namespace ultralight
#define UExport
Definition Defines.h:65
Event representing a change in gamepad axis state (eg, pressing a stick in a certain direction).
Definition GamepadEvent.h:57
Event representing a change in gamepad button state (eg, pressing a button on a gamepad).
Definition GamepadEvent.h:85
Event representing a change in gamepad connection state.
Definition GamepadEvent.h:19
Interface for all ref-counted objects that will be managed using the RefPtr<> smart pointer.
Definition RefPtr.h:47
A nullable smart pointer.
Definition RefPtr.h:79
Core renderer singleton for the library, coordinates all library functions.
Definition Renderer.h:97
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,...
virtual void FireGamepadButtonEvent(const GamepadButtonEvent &evt)=0
Fire a gamepad button event (to be called when a button value is changed).
virtual void FireGamepadAxisEvent(const GamepadAxisEvent &evt)=0
Fire a gamepad axis event (to be called when an axis value is changed).
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 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 Update()=0
Update timers and dispatch callbacks.
virtual void LogMemoryUsage()=0
Print detailed memory usage statistics to the log.
virtual RefPtr< Session > default_session()=0
Get the default Session.
virtual bool StartRemoteInspectorServer(const char *address, uint16_t port)=0
Start the remote inspector server.
virtual void Render()=0
Render all active views to their respective surfaces and render targets.
virtual void PurgeMemory()=0
Attempt to release as much memory as possible.
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.
static RefPtr< Renderer > Create()
Create the core renderer singleton for the library.
virtual void FireGamepadEvent(const GamepadEvent &evt)=0
Fire a gamepad event (connection / disconnection).
UTF-8 String container with conversions for UTF-16 and UTF-32.
Definition String.h:21
Web-page container rendered to an offscreen surface that you display yourself.
Definition View.h:114
Definition App.h:14
View-specific configuration settings.
Definition View.h:28