docs
Loading...
Searching...
No Matches
ImageSource.h
Go to the documentation of this file.
1///
2/// Copyright (C) 2026 Ultralight, Inc. All rights reserved.
3/// A license is required for commercial use. https://ultralig.ht
4///
5#pragma once
7#include <Ultralight/Bitmap.h>
9#include <Ultralight/String.h>
10
11namespace ultralight {
12
15
16///
17/// User-defined image source to display custom images on a web-page.
18///
19/// This API allows you to composite your own images into a web-page. This is useful for displaying
20/// in-game textures, external image assets, or other custom content.
21///
22/// ## ImageSource File Format
23///
24/// To use an ImageSource, you must first create an `.imgsrc` file containing a string identifying
25/// the image source. This string will be used to lookup the ImageSource from ImageSourceProvider
26/// when it is loaded on a web-page.
27///
28/// The file format is as follows:
29///
30/// ```
31/// IMGSRC-V1
32/// <identifier>
33/// ```
34///
35/// You can use the `.imgsrc` file anywhere in your web-page that typically accepts an image URL.
36/// For example:
37///
38/// ```html
39/// <img src="my_custom_image.imgsrc" />
40/// ```
41///
42/// ## Creating from a GPU Texture
43///
44/// To composite your own GPU texture on a web-page, you should first reserve a texture ID from
45/// GPUDriver::NextTextureId() and then create an ImageSource from that texture ID. Next, you should
46/// register the ImageSource with ImageSourceProvider using the identifier from the `.imgsrc` file.
47///
48/// When the image element is drawn on the web-page, the library will draw geometry using the
49/// specified texture ID and UV coordinates. You should bind your own texture when the specified
50/// texture ID is used.
51///
52/// @note If the GPU renderer is not enabled for the View or pixel data is needed for other
53/// purposes, the library will sample the backing bitmap instead.
54///
55/// ## Creating from a Bitmap
56///
57/// To composite your own bitmap on a web-page, you should create an ImageSource from a Bitmap.
58/// Next, you should register the ImageSource with ImageSourceProvider using the identifier from
59/// the `.imgsrc` file.
60///
61/// When the image element is drawn on the web-page, the library will sample this bitmap directly.
62///
63/// ## Invalidating Images
64///
65/// If you modify the texture or bitmap pixels after creating the ImageSource, you should call
66/// ImageSource::Invalidate() to notify the library that the image should be redrawn.
67///
69 public:
70 ///
71 /// Create an ImageSource from a GPU texture with optional backing bitmap.
72 ///
73 /// @param width The width of the image in pixels (used for layout).
74 ///
75 /// @param height The height of the image in pixels (used for layout).
76 ///
77 /// @param texture_id The GPU texture identifier to bind when drawing the quad for this image.
78 /// This should be non-zero and obtained from GPUDriver::NextTextureId().
79 ///
80 /// @param texture_uv The UV coordinates of the texture.
81 ///
82 /// @param bitmap Optional backing bitmap for this image source. This is used when drawing
83 /// the image using the CPU renderer or when pixel data is needed for other
84 /// purposes. You should update this bitmap when the texture changes.
85 ///
86 /// @return A new ImageSource instance.
87 ///
88 static RefPtr<ImageSource> CreateFromTexture(uint32_t width, uint32_t height, uint32_t texture_id,
89 const Rect& texture_uv,
90 RefPtr<Bitmap> bitmap = nullptr);
91
92 ///
93 /// Create an ImageSource from a Bitmap.
94 ///
95 /// @param bitmap The backing bitmap for this image source.
96 ///
97 /// @return A new ImageSource instance.
98 ///
100
101 ///
102 /// Get the width of the image in pixels.
103 ///
104 virtual uint32_t width() const = 0;
105
106 ///
107 /// Get the height of the image in pixels.
108 ///
109 virtual uint32_t height() const = 0;
110
111 ///
112 /// Get the GPU texture identifier to bind when drawing the quad for this image.
113 ///
114 /// @note This will be zero (0) if the image source was created from a bitmap.
115 ///
116 virtual uint32_t texture_id() const = 0;
117
118 ///
119 /// Get the UV coordinates of the texture.
120 ///
121 virtual Rect texture_uv() const = 0;
122
123 ///
124 /// Get the backing bitmap for this image source.
125 ///
126 virtual RefPtr<Bitmap> bitmap() = 0;
127
128 ///
129 /// Invalidate the image.
130 ///
131 /// This will notify the library that the image has changed and should be redrawn.
132 ///
133 virtual void Invalidate() = 0;
134
135 ///
136 /// Add a listener to the image source.
137 ///
138 /// @param listener The listener to add.
139 ///
140 virtual void AddListener(ImageSourceListener* listener) = 0;
141
142 ///
143 /// Remove a listener from the image source.
144 ///
145 /// @param listener The listener to remove.
146 ///
147 virtual void RemoveListener(ImageSourceListener* listener) = 0;
148
149 protected:
150 ImageSource() = default;
151 virtual ~ImageSource() = default;
152 ImageSource(const ImageSource&) = delete;
153 void operator=(const ImageSource&) = delete;
154};
155
156///
157/// Listener for ImageSource events.
158///
159/// This is used to notify listeners when the image source is invalidated.
160///
162 public:
163 virtual ~ImageSourceListener() = default;
164
165 ///
166 /// Called when the image source is invalidated.
167 ///
168 /// @param image_source The image source that was invalidated.
169 ///
170 virtual void OnInvalidateImageSource(ImageSource* image_source) = 0;
171};
172
173///
174/// Maps image sources to string identifiers.
175///
176/// This is used to lookup ImageSource instances when they are requested by a web-page.
177///
179 public:
180 ///
181 /// Get the ImageSourceProvider singleton.
182 ///
184
185 ///
186 /// Get an ImageSource by its identifier.
187 ///
188 /// @param id The identifier of the image source.
189 ///
190 /// @return The ImageSource instance or nullptr if not found.
191 ///
193
194 ///
195 /// Add an ImageSource to the provider.
196 ///
197 /// @param id The identifier of the image source.
198 ///
199 /// @param image_source The ImageSource instance.
200 ///
201 virtual void AddImageSource(const String& id, RefPtr<ImageSource> image_source) = 0;
202
203 ///
204 /// Remove an ImageSource from the provider.
205 ///
206 /// @param id The identifier of the image source.
207 ///
208 virtual void RemoveImageSource(const String& id) = 0;
209
210 ///
211 /// Add a listener to the provider.
212 ///
213 /// @param listener The listener to add.
214 ///
215 virtual void AddListener(ImageSourceProviderListener* listener) = 0;
216
217 ///
218 /// Remove a listener from the provider.
219 ///
220 /// @param listener The listener to remove.
221 ///
222 virtual void RemoveListener(ImageSourceProviderListener* listener) = 0;
223
224 protected:
225 virtual ~ImageSourceProvider() = default;
226};
227
228///
229/// Listener for ImageSourceProvider events.
230///
231/// This is used to notify listeners when an ImageSource is added or removed from the provider.
232///
234 public:
235 virtual ~ImageSourceProviderListener() = default;
236
237 ///
238 /// Called when an ImageSource is added to the provider.
239 ///
240 /// @param id The identifier of the image source.
241 ///
242 /// @param image_source The ImageSource instance.
243 ///
244 virtual void OnAddImageSource(const String& id, RefPtr<ImageSource> image_source) = 0;
245
246 ///
247 /// Called when an ImageSource is removed from the provider.
248 ///
249 /// @param id The identifier of the image source.
250 ///
251 virtual void OnRemoveImageSource(const String& id) = 0;
252};
253
254} // namespace ultralight
#define UExport
Definition Exports.h:22
User-defined image source to display custom images on a web-page.
Definition ImageSource.h:68
virtual ~ImageSource()=default
virtual uint32_t height() const =0
Get the height of the image in pixels.
ImageSource(const ImageSource &)=delete
virtual void Invalidate()=0
Invalidate the image.
virtual uint32_t width() const =0
Get the width of the image in pixels.
void operator=(const ImageSource &)=delete
virtual Rect texture_uv() const =0
Get the UV coordinates of the texture.
static RefPtr< ImageSource > CreateFromBitmap(RefPtr< Bitmap > bitmap)
Create an ImageSource from a Bitmap.
static RefPtr< ImageSource > CreateFromTexture(uint32_t width, uint32_t height, uint32_t texture_id, const Rect &texture_uv, RefPtr< Bitmap > bitmap=nullptr)
Create an ImageSource from a GPU texture with optional backing bitmap.
virtual void AddListener(ImageSourceListener *listener)=0
Add a listener to the image source.
virtual uint32_t texture_id() const =0
Get the GPU texture identifier to bind when drawing the quad for this image.
virtual void RemoveListener(ImageSourceListener *listener)=0
Remove a listener from the image source.
virtual RefPtr< Bitmap > bitmap()=0
Get the backing bitmap for this image source.
Listener for ImageSource events.
Definition ImageSource.h:161
virtual void OnInvalidateImageSource(ImageSource *image_source)=0
Called when the image source is invalidated.
virtual ~ImageSourceListener()=default
Maps image sources to string identifiers.
Definition ImageSource.h:178
virtual void RemoveListener(ImageSourceProviderListener *listener)=0
Remove a listener from the provider.
virtual void AddImageSource(const String &id, RefPtr< ImageSource > image_source)=0
Add an ImageSource to the provider.
virtual RefPtr< ImageSource > GetImageSource(const String &id)=0
Get an ImageSource by its identifier.
virtual void RemoveImageSource(const String &id)=0
Remove an ImageSource from the provider.
static ImageSourceProvider & instance()
Get the ImageSourceProvider singleton.
virtual void AddListener(ImageSourceProviderListener *listener)=0
Add a listener to the provider.
virtual ~ImageSourceProvider()=default
Listener for ImageSourceProvider events.
Definition ImageSource.h:233
virtual ~ImageSourceProviderListener()=default
virtual void OnAddImageSource(const String &id, RefPtr< ImageSource > image_source)=0
Called when an ImageSource is added to the provider.
virtual void OnRemoveImageSource(const String &id)=0
Called when an ImageSource is removed from the provider.
Interface for all ref-counted objects that will be managed using the RefPtr<> smart pointer.
Definition RefPtr.h:49
A nullable smart pointer.
Definition RefPtr.h:126
Unicode string container with conversions for UTF-8, UTF-16, and UTF-32.
Definition String.h:31
Root namespace for every public Ultralight type, function, and enumeration.
Float Rectangle Helper.
Definition Geometry.h:416