Ultralight C++ API 1.3.0
Loading...
Searching...
No Matches
Surface.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/Bitmap.h>
12#include <Ultralight/Geometry.h>
13
14namespace ultralight {
15
16///
17/// Offscreen pixel buffer surface. (Premultiplied BGRA 32-bit format)
18///
19/// When using the CPU renderer, each View is painted to its own Surface.
20///
21/// You can provide your own Surface implementation to make the renderer paint directly to a block
22/// of memory controlled by you (this is useful for lower-latency uploads to GPU memory or other
23/// platform-specific bitmaps).
24///
25/// A default Surface implementation, BitmapSurface, is automatically provided by the library when
26/// you call Renderer::Create() without defining a custom SurfaceFactory.
27///
28/// To provide your own custom Surface implementation, you should inherit from this class, handle
29/// the virtual member functions, and then define a custom SurfaceFactory that creates/destroys an
30/// instance of your class. After that, you should pass an instance of your custom SurfaceFactory
31/// class to `Platform::instance().set_font_loader()` before calling App::Create() or
32/// Renderer::Create().
33///
35 public:
36 virtual ~Surface();
37
38 ///
39 /// Width (in pixels).
40 ///
41 virtual uint32_t width() const = 0;
42
43 ///
44 /// Height (in pixels).
45 ///
46 virtual uint32_t height() const = 0;
47
48 ///
49 /// Number of bytes between rows (usually width * 4)
50 ///
51 virtual uint32_t row_bytes() const = 0;
52
53 ///
54 /// Size in bytes.
55 ///
56 virtual size_t size() const = 0;
57
58 ///
59 /// Lock the pixel buffer and get a pointer to the beginning of the data for reading/writing.
60 ///
61 /// Native pixel format is premultiplied BGRA 32-bit (8 bits per channel).
62 ///
63 virtual void* LockPixels() = 0;
64
65 ///
66 /// Unlock the pixel buffer.
67 ///
68 virtual void UnlockPixels() = 0;
69
70 ///
71 /// Resize the pixel buffer to a certain width and height (both in pixels).
72 ///
73 /// This should never be called while pixels are locked.
74 ///
75 virtual void Resize(uint32_t width, uint32_t height) = 0;
76
77 ///
78 /// Set the dirty bounds to a certain value.
79 ///
80 /// This is called after the Renderer paints to an area of the pixel buffer. (The new value will
81 /// be joined with the existing dirty_bounds())
82 ///
83 virtual void set_dirty_bounds(const IntRect& bounds);
84
85 ///
86 /// Get the dirty bounds.
87 ///
88 /// This value can be used to determine which portion of the pixel buffer has been updated since
89 /// the last call to ClearDirtyBounds().
90 ///
91 /// The general algorithm to determine if a Surface needs display is:
92 /// <pre>
93 /// if (!surface.dirty_bounds().IsEmpty()) {
94 /// // Surface pixels are dirty and needs display.
95 /// // Cast Surface to native Surface and use it here (pseudo code)
96 /// DisplaySurface(surface);
97 ///
98 /// // Once you're done, clear the dirty bounds:
99 /// surface.ClearDirtyBounds();
100 /// }
101 /// </pre>
102 ///
103 virtual IntRect dirty_bounds() const;
104
105 ///
106 /// Clear the dirty bounds.
107 ///
108 /// You should call this after you're done displaying the Surface.
109 ///
110 virtual void ClearDirtyBounds();
111
112 protected:
114
116};
117
118///
119/// SurfaceFactory can be used to provide your own native Surface implementation.
120///
121/// This can be used to wrap a platform-specific GPU texture, Windows DIB, macOS CGImage, or any
122/// other pixel buffer target for display on screen.
123///
124/// The default factory creates/destroys a BitmapSurface but you can override this by providing your
125/// own factory to Platform::set_surface_factory.
126///
128 public:
130
131 ///
132 /// Create a native Surface with a certain width and height (in pixels).
133 ///
134 virtual Surface* CreateSurface(uint32_t width, uint32_t height) = 0;
135
136 ///
137 /// Destroy a native Surface previously created by CreateSurface().
138 ///
139 virtual void DestroySurface(Surface* surface) = 0;
140};
141
142///
143/// The default Surface implementation, backed by a Bitmap.
144///
146 public:
147 virtual uint32_t width() const override;
148
149 virtual uint32_t height() const override;
150
151 virtual uint32_t row_bytes() const override;
152
153 virtual size_t size() const override;
154
155 virtual void* LockPixels() override;
156
157 virtual void UnlockPixels() override;
158
159 virtual void Resize(uint32_t width, uint32_t height) override;
160
161 ///
162 /// Get the underlying Bitmap.
163 ///
165
166 protected:
167 BitmapSurface(uint32_t width, uint32_t height);
168 virtual ~BitmapSurface();
169 BitmapSurface(const BitmapSurface&) = delete;
170 void operator=(const BitmapSurface&) = delete;
171 friend class BitmapSurfaceFactory;
172
173 void* impl_;
174};
175
176///
177/// Get the default Bitmap Surface Factory singleton. (Do not destroy this, this singleton is owned
178/// by the library).
179///
181
182} // namespace ultralight
#define UExport
Definition Defines.h:65
The default Surface implementation, backed by a Bitmap.
Definition Surface.h:145
BitmapSurface(uint32_t width, uint32_t height)
virtual size_t size() const override
Size in bytes.
virtual uint32_t height() const override
Height (in pixels).
virtual void * LockPixels() override
Lock the pixel buffer and get a pointer to the beginning of the data for reading/writing.
virtual uint32_t row_bytes() const override
Number of bytes between rows (usually width * 4)
virtual void UnlockPixels() override
Unlock the pixel buffer.
void operator=(const BitmapSurface &)=delete
virtual uint32_t width() const override
Width (in pixels).
void * impl_
Definition Surface.h:173
BitmapSurface(const BitmapSurface &)=delete
virtual void Resize(uint32_t width, uint32_t height) override
Resize the pixel buffer to a certain width and height (both in pixels).
RefPtr< Bitmap > bitmap()
Get the underlying Bitmap.
A nullable smart pointer.
Definition RefPtr.h:79
SurfaceFactory can be used to provide your own native Surface implementation.
Definition Surface.h:127
virtual void DestroySurface(Surface *surface)=0
Destroy a native Surface previously created by CreateSurface().
virtual Surface * CreateSurface(uint32_t width, uint32_t height)=0
Create a native Surface with a certain width and height (in pixels).
Offscreen pixel buffer surface.
Definition Surface.h:34
virtual void ClearDirtyBounds()
Clear the dirty bounds.
virtual IntRect dirty_bounds() const
Get the dirty bounds.
virtual uint32_t row_bytes() const =0
Number of bytes between rows (usually width * 4)
virtual void set_dirty_bounds(const IntRect &bounds)
Set the dirty bounds to a certain value.
virtual void Resize(uint32_t width, uint32_t height)=0
Resize the pixel buffer to a certain width and height (both in pixels).
virtual uint32_t width() const =0
Width (in pixels).
virtual uint32_t height() const =0
Height (in pixels).
virtual size_t size() const =0
Size in bytes.
virtual void * LockPixels()=0
Lock the pixel buffer and get a pointer to the beginning of the data for reading/writing.
IntRect dirty_bounds_
Definition Surface.h:115
virtual void UnlockPixels()=0
Unlock the pixel buffer.
Definition App.h:14
SurfaceFactory * GetBitmapSurfaceFactory()
Get the default Bitmap Surface Factory singleton.
Integer Rectangle Helper.
Definition Geometry.h:529