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