Loading...
Searching...
No Matches
CAPI_Surface.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_Surface.h
11///
12/// User-defined pixel buffer surface.
13///
14/// `#include <Ultralight/CAPI/CAPI_Surface.h>`
15///
16/// The library uses this to store pixel data when rendering Views on the CPU (see
17/// ulViewIsAccelerated()).
18///
19/// You can provide the library with your own Surface implementation to reduce the latency of
20/// displaying pixels in your application (Views will be drawn directly to a block of memory
21/// controlled by you).
22///
23/// When a View is rendered on the CPU, you can retrieve the backing Surface via ulViewGetSurface().
24///
25/// @pre This is automatically managed for you when using ulCreateApp(), if you want to override
26/// ULSurfaceDefinition, you'll need to use ulCreateRenderer() instead.
27///
28/// ## Default Implementation
29///
30/// A default Surface implementation, BitmapSurface, is automatically provided by the library when
31/// you call ulCreateRenderer() without defining a custom ULSurfaceDefinition.
32///
33/// You should cast the ULSurface to a ULBitmapSurface and call ulBitmapSurfaceGetBitmap() to access
34/// the underlying Bitmap.
35///
36/// ## Setting the Surface Implementation
37///
38/// To define your own implementation, you should implement the ULSurfaceDefinition callbacks,
39/// and then pass an instance of ULSurfaceDefinition containing your callbacks to
40/// ulPlatformSetSurfaceDefinition() before calling ulCreateRenderer().
41///
42#ifndef ULTRALIGHT_CAPI_SURFACE_H
43#define ULTRALIGHT_CAPI_SURFACE_H
44
46
47#ifdef __cplusplus
48extern "C" {
49#endif
50
51/******************************************************************************
52 * Surface
53 *****************************************************************************/
54
55///
56/// Width (in pixels).
57///
58ULExport unsigned int ulSurfaceGetWidth(ULSurface surface);
59
60///
61/// Height (in pixels).
62///
64
65///
66/// Number of bytes between rows (usually width * 4)
67///
69
70///
71/// Size in bytes.
72///
74
75///
76/// Lock the pixel buffer and get a pointer to the beginning of the data for reading/writing.
77///
78/// Native pixel format is premultiplied BGRA 32-bit (8 bits per channel).
79///
81
82///
83/// Unlock the pixel buffer.
84///
86
87///
88/// Resize the pixel buffer to a certain width and height (both in pixels).
89///
90/// This should never be called while pixels are locked.
91///
92ULExport void ulSurfaceResize(ULSurface surface, unsigned int width, unsigned int height);
93
94///
95/// Set the dirty bounds to a certain value.
96///
97/// This is called after the Renderer paints to an area of the pixel buffer. (The new value will be
98/// joined with the existing dirty_bounds())
99///
101
102///
103/// Get the dirty bounds.
104///
105/// This value can be used to determine which portion of the pixel buffer has been updated since the
106/// last call to ulSurfaceClearDirtyBounds().
107///
108/// The general algorithm to determine if a Surface needs display is:
109/// <pre>
110/// if (!ulIntRectIsEmpty(ulSurfaceGetDirtyBounds(surface))) {
111/// // Surface pixels are dirty and needs display.
112/// // Cast Surface to native Surface and use it here (pseudo code)
113/// DisplaySurface(surface);
114///
115/// // Once you're done, clear the dirty bounds:
116/// ulSurfaceClearDirtyBounds(surface);
117/// }
118/// </pre>
119///
121
122///
123/// Clear the dirty bounds.
124///
125/// You should call this after you're done displaying the Surface.
126///
128
129///
130/// Get the underlying user data pointer (this is only valid if you have set a custom surface
131/// implementation via ulPlatformSetSurfaceDefinition).
132///
133/// This will return nullptr if this surface is the default ULBitmapSurface.
134///
136
137/******************************************************************************
138 * BitmapSurface
139 *****************************************************************************/
140
141///
142/// Get the underlying Bitmap from the default Surface.
143///
144/// @note Do not call ulDestroyBitmap() on the returned value, it is owned by the surface.
145///
147
148/******************************************************************************
149 * Surface Definition
150 *****************************************************************************/
151
152///
153/// The callback invoked when a Surface is created.
154///
155/// @param width The width in pixels.
156/// @param height The height in pixels.
157///
158/// @return This callback should return a pointer to user-defined data for the instance. This user
159/// data pointer will be passed to all other callbacks when operating on the instance.
160///
161typedef void* (*ULSurfaceDefinitionCreateCallback)(unsigned int width, unsigned int height);
162
163///
164/// The callback invoked when a Surface is destroyed.
165///
166/// @param user_data User data pointer uniquely identifying the surface.
167///
168typedef void (*ULSurfaceDefinitionDestroyCallback)(void* user_data);
169
170///
171/// The callback invoked when a Surface's width (in pixels) is requested.
172///
173/// @param user_data User data pointer uniquely identifying the surface.
174///
175typedef unsigned int (*ULSurfaceDefinitionGetWidthCallback)(void* user_data);
176
177///
178/// The callback invoked when a Surface's height (in pixels) is requested.
179///
180/// @param user_data User data pointer uniquely identifying the surface.
181///
182typedef unsigned int (*ULSurfaceDefinitionGetHeightCallback)(void* user_data);
183
184///
185/// The callback invoked when a Surface's row bytes is requested.
186///
187/// @note This value is also known as "stride". Usually width * 4.
188///
189/// @param user_data User data pointer uniquely identifying the surface.
190///
191typedef unsigned int (*ULSurfaceDefinitionGetRowBytesCallback)(void* user_data);
192
193///
194/// The callback invoked when a Surface's size (in bytes) is requested.
195///
196/// @param user_data User data pointer uniquely identifying the surface.
197///
198typedef size_t (*ULSurfaceDefinitionGetSizeCallback)(void* user_data);
199
200///
201/// The callback invoked when a Surface's pixel buffer is requested to be locked for reading/writing
202/// (should return a pointer to locked bytes).
203///
204/// @param user_data User data pointer uniquely identifying the surface.
205///
206typedef void* (*ULSurfaceDefinitionLockPixelsCallback)(void* user_data);
207
208///
209/// The callback invoked when a Surface's pixel buffer is requested to be unlocked after previously
210/// being locked.
211///
212/// @param user_data User data pointer uniquely identifying the surface.
213///
214typedef void (*ULSurfaceDefinitionUnlockPixelsCallback)(void* user_data);
215
216///
217/// The callback invoked when a Surface is requested to be resized to a certain width/height.
218///
219/// @param user_data User data pointer uniquely identifying the surface.
220///
221/// @param width Width in pixels.
222///
223/// @param height Height in pixels.
224///
225typedef void (*ULSurfaceDefinitionResizeCallback)(void* user_data, unsigned int width,
226 unsigned int height);
227
228///
229/// User-defined surface interface.
230///
231/// You should implement each of these callbacks, then pass an instance of this struct containing
232/// your callbacks to ulPlatformSetSurfaceDefinition().
233///
245
246#ifdef __cplusplus
247} // extern "C"
248#endif
249
250#endif // ULTRALIGHT_CAPI_SURFACE_H
Various defines and utility functions for the C API.
struct C_Surface * ULBitmapSurface
Definition CAPI_Defines.h:74
#define ULExport
Definition CAPI_Defines.h:38
struct C_Surface * ULSurface
Definition CAPI_Defines.h:73
struct C_Bitmap * ULBitmap
Definition CAPI_Defines.h:64
ULExport ULBitmap ulBitmapSurfaceGetBitmap(ULBitmapSurface surface)
Get the underlying Bitmap from the default Surface.
ULExport unsigned int ulSurfaceGetWidth(ULSurface surface)
Width (in pixels).
ULExport void ulSurfaceUnlockPixels(ULSurface surface)
Unlock the pixel buffer.
unsigned int(* ULSurfaceDefinitionGetRowBytesCallback)(void *user_data)
The callback invoked when a Surface's row bytes is requested.
Definition CAPI_Surface.h:191
ULExport void ulSurfaceResize(ULSurface surface, unsigned int width, unsigned int height)
Resize the pixel buffer to a certain width and height (both in pixels).
ULExport void * ulSurfaceLockPixels(ULSurface surface)
Lock the pixel buffer and get a pointer to the beginning of the data for reading/writing.
ULExport size_t ulSurfaceGetSize(ULSurface surface)
Size in bytes.
ULExport ULIntRect ulSurfaceGetDirtyBounds(ULSurface surface)
Get the dirty bounds.
ULExport unsigned int ulSurfaceGetRowBytes(ULSurface surface)
Number of bytes between rows (usually width * 4)
ULExport void ulSurfaceClearDirtyBounds(ULSurface surface)
Clear the dirty bounds.
ULExport unsigned int ulSurfaceGetHeight(ULSurface surface)
Height (in pixels).
void(* ULSurfaceDefinitionResizeCallback)(void *user_data, unsigned int width, unsigned int height)
The callback invoked when a Surface is requested to be resized to a certain width/height.
Definition CAPI_Surface.h:225
void *(* ULSurfaceDefinitionCreateCallback)(unsigned int width, unsigned int height)
The callback invoked when a Surface is created.
Definition CAPI_Surface.h:161
unsigned int(* ULSurfaceDefinitionGetWidthCallback)(void *user_data)
The callback invoked when a Surface's width (in pixels) is requested.
Definition CAPI_Surface.h:175
void(* ULSurfaceDefinitionDestroyCallback)(void *user_data)
The callback invoked when a Surface is destroyed.
Definition CAPI_Surface.h:168
ULExport void ulSurfaceSetDirtyBounds(ULSurface surface, ULIntRect bounds)
Set the dirty bounds to a certain value.
void(* ULSurfaceDefinitionUnlockPixelsCallback)(void *user_data)
The callback invoked when a Surface's pixel buffer is requested to be unlocked after previously being...
Definition CAPI_Surface.h:214
void *(* ULSurfaceDefinitionLockPixelsCallback)(void *user_data)
The callback invoked when a Surface's pixel buffer is requested to be locked for reading/writing (sho...
Definition CAPI_Surface.h:206
ULExport void * ulSurfaceGetUserData(ULSurface surface)
Get the underlying user data pointer (this is only valid if you have set a custom surface implementat...
size_t(* ULSurfaceDefinitionGetSizeCallback)(void *user_data)
The callback invoked when a Surface's size (in bytes) is requested.
Definition CAPI_Surface.h:198
unsigned int(* ULSurfaceDefinitionGetHeightCallback)(void *user_data)
The callback invoked when a Surface's height (in pixels) is requested.
Definition CAPI_Surface.h:182
Definition CAPI_Defines.h:257
User-defined surface interface.
Definition CAPI_Surface.h:234
ULSurfaceDefinitionGetHeightCallback get_height
Definition CAPI_Surface.h:238
ULSurfaceDefinitionUnlockPixelsCallback unlock_pixels
Definition CAPI_Surface.h:242
ULSurfaceDefinitionCreateCallback create
Definition CAPI_Surface.h:235
ULSurfaceDefinitionResizeCallback resize
Definition CAPI_Surface.h:243
ULSurfaceDefinitionLockPixelsCallback lock_pixels
Definition CAPI_Surface.h:241
ULSurfaceDefinitionGetWidthCallback get_width
Definition CAPI_Surface.h:237
ULSurfaceDefinitionGetRowBytesCallback get_row_bytes
Definition CAPI_Surface.h:239
ULSurfaceDefinitionGetSizeCallback get_size
Definition CAPI_Surface.h:240
ULSurfaceDefinitionDestroyCallback destroy
Definition CAPI_Surface.h:236