Gamp v0.0.7-36-g24b1eb6
Gamp: Graphics, Audio, Multimedia and Processing
Loading...
Searching...
No Matches
Capabilities.hpp
Go to the documentation of this file.
1/*
2 * Author: Sven Gothel <sgothel@jausoft.com>
3 * Copyright Gothel Software e.K.
4 *
5 * SPDX-License-Identifier: MIT
6 *
7 * This Source Code Form is subject to the terms of the MIT License
8 * If a copy of the MIT was not distributed with this file,
9 * you can obtain one at https://opensource.org/license/mit/.
10 */
11#ifndef GAMP_WT_CAPABILITIES_HPP_
12#define GAMP_WT_CAPABILITIES_HPP_
13
14#include <gamp/GampTypes.hpp>
15#include <memory>
16
17#include <jau/string_util.hpp>
18#include <jau/type_info.hpp>
19
20namespace gamp::wt {
21 /** \addtogroup Gamp_WT
22 *
23 * @{
24 */
25
26 class Capabilities;
27 typedef std::unique_ptr<Capabilities> CapabilitiesPtr;
28
29 /** Specifies a set of capabilities that a window's rendering context
30 must support, such as color depth per channel. It currently
31 contains the minimal number of routines which allow configuration
32 on all supported window systems. */
34 protected:
35 constexpr static std::string_view na_str = "----";
36
37 private:
38 int m_vid = 0;
39 int m_redBits = 8;
40 int m_greenBits = 8;
41 int m_blueBits = 8;
42 int m_alphaBits = 0;
43
44 // Support for transparent windows containing OpenGL content
45 bool m_backgroundOpaque = true;
46
47 int m_xparentValueRed = 0;
48 int m_xparentValueGreen = 0;
49 int m_xparentValueBlue = 0;
50 int m_xparentValueAlpha = 0;
51
52 // Switch for on- or offscreen
53 bool m_onscreen = true;
54
55 // offscreen bitmap mode
56 bool m_isBitmap = false;
57
58 public:
59 /** Creates a Capabilities object. All attributes are in a default
60 state.
61 */
62 constexpr Capabilities() noexcept = default;
63 virtual ~Capabilities() noexcept = default;
64
65 constexpr Capabilities(const Capabilities&) noexcept = default;
66 constexpr Capabilities(Capabilities&&) noexcept = default;
67 Capabilities& operator=(const Capabilities&) noexcept = default;
68
69 virtual const jau::type_info& signature() const noexcept { return jau::static_ctti<Capabilities>(); }
70
71 virtual CapabilitiesPtr clone() const noexcept { return std::make_unique<Capabilities>(*this); }
72
73 virtual std::size_t hash_code() const noexcept {
74 // 31 * x == (x << 5) - x
75 size_t hash = 31U + m_redBits;
76 hash = ((hash << 5) - hash) + (m_onscreen ? 1U : 0U);
77 hash = ((hash << 5) - hash) + (m_isBitmap ? 1U : 0U);
78 hash = ((hash << 5) - hash) + m_greenBits;
79 hash = ((hash << 5) - hash) + m_blueBits;
80 hash = ((hash << 5) - hash) + m_alphaBits;
81 hash = ((hash << 5) - hash) + (m_backgroundOpaque ? 1U : 0U);
82 hash = ((hash << 5) - hash) + m_xparentValueRed;
83 hash = ((hash << 5) - hash) + m_xparentValueGreen;
84 hash = ((hash << 5) - hash) + m_xparentValueBlue;
85 hash = ((hash << 5) - hash) + m_xparentValueAlpha;
86 return hash;
87 }
88
89 virtual bool operator==(const Capabilities& rhs) const noexcept {
90 if( this == &rhs ) {
91 return true;
92 }
93 if( signature() != rhs.signature() ) {
94 return false;
95 }
96 {
97 // first check whether the VID is compatible
98 const int id_t = visualID();
99 const int id_o = rhs.visualID();
100 if( id_t != id_o ) {
101 return false;
102 }
103 }
104 bool res = rhs.redBits() == m_redBits &&
105 rhs.greenBits() == m_greenBits &&
106 rhs.blueBits() == m_blueBits &&
107 rhs.alphaBits() == m_alphaBits &&
108 rhs.isBackgroundOpaque() == m_backgroundOpaque &&
109 rhs.isOnscreen() == m_onscreen &&
110 rhs.isBitmap() == m_isBitmap;
111 if( res && !m_backgroundOpaque ) {
112 res = rhs.transparentRedValue() == m_xparentValueRed &&
113 rhs.transparentGreenValue() == m_xparentValueGreen &&
114 rhs.transparentBlueValue() == m_xparentValueBlue &&
115 rhs.transparentAlphaValue() == m_xparentValueAlpha;
116 }
117
118 return res;
119 }
120
121 /**
122 * Comparing RGBA values only
123 *
124 * Returns
125 * - -1 if this < other
126 * - 0 if this == other
127 * - 1 if this > other
128 */
129 virtual int compare(const Capabilities& rhs) const noexcept {
130 if( this == &rhs ) {
131 return 0;
132 }
133 if( signature() != rhs.signature() ) {
134 return 1;
135 }
136 const int rgba = m_redBits * m_greenBits * m_blueBits * (m_alphaBits + 1);
137 const int xrgba = rhs.redBits() * rhs.greenBits() * rhs.blueBits() * (rhs.alphaBits() + 1);
138 if( rgba > xrgba ) {
139 return 1;
140 } else if( rgba < xrgba ) {
141 return -1;
142 }
143 return 0; // equal RGBA
144 }
145
146 std::strong_ordering operator<=>(const Capabilities& rhs) const noexcept {
147 const int r = compare(rhs);
148 return 0 == r ? std::strong_ordering::equal : (0 > r ? std::strong_ordering::less : std::strong_ordering::greater);
149 }
150
151 constexpr int visualID() const noexcept { return m_vid; }
152 /**
153 * Returns the number of bits for the color buffer's red
154 * component. On some systems only the color depth, which is the sum of the
155 * red, green, and blue bits, is considered.
156 */
157 constexpr int redBits() const noexcept { return m_redBits; }
158 /**
159 * Returns the number of bits for the color buffer's green
160 * component. On some systems only the color depth, which is the sum of the
161 * red, green, and blue bits, is considered.
162 */
163 constexpr int greenBits() const noexcept { return m_greenBits; }
164 /**
165 * Returns the number of bits for the color buffer's blue
166 * component. On some systems only the color depth, which is the sum of the
167 * red, green, and blue bits, is considered.
168 */
169 constexpr int blueBits() const noexcept { return m_blueBits; }
170 /**
171 * Returns the number of bits for the color buffer's alpha
172 * component. On some systems only the color depth, which is the sum of the
173 * red, green, and blue bits, is considered.
174 */
175 constexpr int alphaBits() const noexcept { return m_alphaBits; }
176
177 constexpr int& visualID() noexcept { return m_vid; }
178
179 /** Allows setting the number of bits requested for the color buffer's red
180 component. On some systems only the color depth, which is the
181 sum of the red, green, and blue bits, is considered. */
182 constexpr int& redBits() noexcept { return m_redBits; }
183
184 /** Allows setting the number of bits requested for the color buffer's green
185 component. On some systems only the color depth, which is the
186 sum of the red, green, and blue bits, is considered. */
187 constexpr int& greenBits() noexcept { return m_greenBits; }
188
189 /** Allows setting the number of bits requested for the color buffer's blue
190 component. On some systems only the color depth, which is the
191 sum of the red, green, and blue bits, is considered. */
192 constexpr int& blueBits() noexcept { return m_blueBits; }
193
194 /**
195 * Allows setting the number of bits requested for the color buffer's alpha
196 * component. On some systems only the color depth, which is the
197 * sum of the red, green, and blue bits, is considered.
198 * <p>
199 * <b>Note:</b> If alpha bits are <code>zero</code>, they are set to <code>one</code>
200 * by {@link #setBackgroundOpaque(boolean)} and it's OpenGL specialization <code>GLCapabilities::setSampleBuffers(boolean)</code>.<br/>
201 * Ensure to call this method after the above to ensure a <code>zero</code> value.</br>
202 * The above automated settings takes into account, that the user calls this method to <i>request</i> alpha bits,
203 * not to <i>reflect</i> a current state. Nevertheless if this is the case - call it at last.
204 * </p>
205 */
206 constexpr int& alphaBits() noexcept { return m_alphaBits; }
207
208 /**
209 * Sets whether the surface shall be opaque or translucent.
210 * <p>
211 * Platform implementations may need an alpha component in the surface (eg. Windows),
212 * or expect pre-multiplied alpha values (eg. X11/XRender).<br>
213 * To unify the experience, this method also invokes {@link #setAlphaBits(int) setAlphaBits(1)}
214 * if {@link #getAlphaBits()} == 0.<br>
215 * Please note that in case alpha is required on the platform the
216 * clear color shall have an alpha lower than 1.0 to allow anything shining through.
217 * </p>
218 * <p>
219 * Mind that translucency may cause a performance penalty
220 * due to the composite work required by the window manager.
221 * </p>
222 */
223 constexpr void setBackgroundOpaque(bool opaque) noexcept {
224 m_backgroundOpaque = opaque;
225 if( !opaque && alphaBits() == 0 ) {
226 alphaBits() = 1;
227 }
228 }
229
230 /**
231 * Returns whether an opaque or translucent surface is requested, supported or chosen.
232 * <p>
233 * Default is true, i.e. opaque.
234 * </p>
235 */
236 constexpr bool isBackgroundOpaque() const noexcept { return m_backgroundOpaque; }
237
238 /**
239 * Sets whether the surface shall be on- or offscreen.
240 * <p>
241 * Defaults to true.
242 * </p>
243 * <p>
244 * If requesting an offscreen surface without further selection of it's mode,
245 * e.g. FBO, Pbuffer or {@link #setBitmap(boolean) bitmap},
246 * the implementation will choose the best available offscreen mode.
247 * </p>
248 * @param onscreen
249 */
250 constexpr void setOnscreen(bool v) noexcept { m_onscreen = v; }
251
252 /**
253 * Returns whether an on- or offscreen surface is requested, available or chosen.
254 * <p>
255 * Default is true, i.e. onscreen.
256 * </p>
257 * <p>
258 * Mind that an capabilities intance w/ <i>available</i> semantics
259 * may show onscreen, but also the offscreen modes FBO or {@link #setBitmap(boolean) bitmap}.
260 * This is valid, since one native configuration maybe used for either functionality.
261 * </p>
262 */
263 constexpr bool isOnscreen() const noexcept { return m_onscreen; }
264
265 /**
266 * Requesting offscreen bitmap mode.
267 * <p>
268 * If enabled this method also invokes {@link #setOnscreen(int) setOnscreen(false)}.
269 * </p>
270 * <p>
271 * Defaults to false.
272 * </p>
273 * <p>
274 * Requesting offscreen bitmap mode disables the offscreen auto selection.
275 * </p>
276 */
277 constexpr void setBitmap(bool enable) noexcept {
278 if( enable ) {
279 setOnscreen(false);
280 }
281 m_isBitmap = enable;
282 }
283
284 /**
285 * Returns whether bitmap offscreen mode is requested, available or chosen.
286 * <p>
287 * Default is false.
288 * </p>
289 * <p>
290 * For chosen capabilities, only the selected offscreen surface is set to <code>true</code>.
291 * </p>
292 */
293 constexpr bool isBitmap() const noexcept { return m_isBitmap; }
294
295 /**
296 * Gets the transparent red value for the frame buffer configuration. This
297 * value is undefined if; equals true.
298 */
299 constexpr int transparentRedValue() const noexcept { return m_xparentValueRed; }
300 /**
301 * Gets the transparent green value for the frame buffer configuration. This
302 * value is undefined if; equals true.
303 */
304 constexpr int transparentGreenValue() const noexcept { return m_xparentValueGreen; }
305 /**
306 * Gets the transparent blue value for the frame buffer configuration. This
307 * value is undefined if; equals true.
308 */
309 constexpr int transparentBlueValue() const noexcept { return m_xparentValueBlue; }
310 /**
311 * Gets the transparent alpha value for the frame buffer configuration. This
312 * value is undefined if; equals true.
313 */
314 constexpr int transparentAlphaValue() const noexcept { return m_xparentValueAlpha; }
315
316 /** Allows setting the transparent red value for the frame buffer configuration,
317 ranging from 0 to the maximum frame buffer value for red.
318 This value is ignored if {@link #isBackgroundOpaque()} equals true.<br>
319 It defaults to half of the frambuffer value for red. <br>
320 A value of -1 is interpreted as any value. */
321 constexpr int& transparentRedValue() noexcept { return m_xparentValueRed; }
322
323 /** Allows setting the transparent green value for the frame buffer configuration,
324 ranging from 0 to the maximum frame buffer value for green.
325 This value is ignored if {@link #isBackgroundOpaque()} equals true.<br>
326 It defaults to half of the frambuffer value for green.<br>
327 A value of -1 is interpreted as any value. */
328 constexpr int& transparentGreenValue() noexcept { return m_xparentValueGreen; }
329
330 /** Allows setting the transparent blue value for the frame buffer configuration,
331 ranging from 0 to the maximum frame buffer value for blue.
332 This value is ignored if {@link #isBackgroundOpaque()} equals true.<br>
333 It defaults to half of the frambuffer value for blue.<br>
334 A value of -1 is interpreted as any value. */
335 constexpr int& transparentBlueValue() noexcept { return m_xparentValueBlue; }
336
337 /** Allows setting the transparent alpha value for the frame buffer configuration,
338 ranging from 0 to the maximum frame buffer value for alpha.
339 This value is ignored if {@link #isBackgroundOpaque()} equals true.<br>
340 It defaults to half of the frambuffer value for alpha.<br>
341 A value of -1 is interpreted as any value. */
342 constexpr int& transparentAlphaValue() noexcept { return m_xparentValueAlpha; }
343
344 /** Returns a textual representation of this Capabilities
345 object. */
346 virtual std::string toString() const {
347 std::string msg("Caps[");
348 toString(msg, true);
349 return msg.append("]");
350 }
351
352 protected:
353 /** Element separator */
354 constexpr static std::string_view ESEP = "/";
355 /** Component separator */
356 constexpr static std::string_view CSEP = ", ";
357
358 void toString(std::string& sink, bool withOnOffScreen) const {
359 sink.append("rgba ").append(std::to_string(m_redBits)).append(ESEP).append(std::to_string(m_greenBits)).append(ESEP).append(std::to_string(m_blueBits)).append(ESEP).append(std::to_string(m_alphaBits));
360 if( m_backgroundOpaque ) {
361 sink.append(", opaque");
362 } else {
363 sink.append(", trans-rgba 0x").append(jau::to_hexstring(m_xparentValueRed)).append(ESEP).append(jau::to_hexstring(m_xparentValueGreen)).append(ESEP).append(jau::to_hexstring(jau::to_hexstring(m_xparentValueBlue))).append(ESEP).append(jau::to_hexstring(m_xparentValueAlpha));
364 }
365 if( withOnOffScreen ) {
366 sink.append(CSEP);
367 if( m_onscreen ) {
368 sink.append("on-scr");
369 } else {
370 sink.append("offscr[");
371 }
372 if( m_isBitmap ) {
373 sink.append("bitmap");
374 } else if( m_onscreen ) {
375 sink.append("."); // no additional off-screen modes besides on-screen
376 } else {
377 sink.append("auto-cfg"); // auto-config off-screen mode
378 }
379 sink.append("]");
380 }
381 }
382 };
383
384 /**@}*/
385
386} // namespace gamp::wt
387
388// injecting specialization of std::hash to namespace std of our types above
389namespace std {
390 /** \addtogroup Gamp_WT
391 *
392 */
393
394 template <>
395 struct hash<gamp::wt::Capabilities> {
396 std::size_t operator()(gamp::wt::Capabilities const& a) const noexcept {
397 return a.hash_code();
398 }
399 };
400
401 /**@}*/
402} // namespace std
403
404#endif /* GAMP_WT_CAPABILITIES_HPP_ */
Specifies a set of capabilities that a window's rendering context must support, such as color depth p...
constexpr void setBitmap(bool enable) noexcept
Requesting offscreen bitmap mode.
constexpr int & transparentGreenValue() noexcept
Allows setting the transparent green value for the frame buffer configuration, ranging from 0 to the ...
constexpr int transparentBlueValue() const noexcept
Gets the transparent blue value for the frame buffer configuration.
constexpr int redBits() const noexcept
Returns the number of bits for the color buffer's red component.
constexpr int & transparentRedValue() noexcept
Allows setting the transparent red value for the frame buffer configuration, ranging from 0 to the ma...
static constexpr std::string_view ESEP
Element separator.
constexpr bool isOnscreen() const noexcept
Returns whether an on- or offscreen surface is requested, available or chosen.
constexpr int & blueBits() noexcept
Allows setting the number of bits requested for the color buffer's blue component.
constexpr int & visualID() noexcept
constexpr int & transparentAlphaValue() noexcept
Allows setting the transparent alpha value for the frame buffer configuration, ranging from 0 to the ...
constexpr int greenBits() const noexcept
Returns the number of bits for the color buffer's green component.
constexpr int visualID() const noexcept
constexpr int transparentRedValue() const noexcept
Gets the transparent red value for the frame buffer configuration.
constexpr int transparentAlphaValue() const noexcept
Gets the transparent alpha value for the frame buffer configuration.
std::strong_ordering operator<=>(const Capabilities &rhs) const noexcept
constexpr bool isBitmap() const noexcept
Returns whether bitmap offscreen mode is requested, available or chosen.
virtual int compare(const Capabilities &rhs) const noexcept
Comparing RGBA values only.
constexpr int & greenBits() noexcept
Allows setting the number of bits requested for the color buffer's green component.
void toString(std::string &sink, bool withOnOffScreen) const
static constexpr std::string_view CSEP
Component separator.
constexpr int blueBits() const noexcept
Returns the number of bits for the color buffer's blue component.
virtual std::size_t hash_code() const noexcept
constexpr void setBackgroundOpaque(bool opaque) noexcept
Sets whether the surface shall be opaque or translucent.
constexpr int & redBits() noexcept
Allows setting the number of bits requested for the color buffer's red component.
static constexpr std::string_view na_str
constexpr void setOnscreen(bool v) noexcept
Sets whether the surface shall be on- or offscreen.
constexpr int & alphaBits() noexcept
Allows setting the number of bits requested for the color buffer's alpha component.
virtual std::string toString() const
Returns a textual representation of this Capabilities object.
virtual CapabilitiesPtr clone() const noexcept
constexpr int & transparentBlueValue() noexcept
Allows setting the transparent blue value for the frame buffer configuration, ranging from 0 to the m...
constexpr int alphaBits() const noexcept
Returns the number of bits for the color buffer's alpha component.
virtual const jau::type_info & signature() const noexcept
virtual bool operator==(const Capabilities &rhs) const noexcept
constexpr bool isBackgroundOpaque() const noexcept
Returns whether an opaque or translucent surface is requested, supported or chosen.
constexpr Capabilities() noexcept=default
Creates a Capabilities object.
constexpr int transparentGreenValue() const noexcept
Gets the transparent green value for the frame buffer configuration.
const jau::type_info & static_ctti() noexcept
Returns a static global reference of make_ctti<T>(true) w/ identity instance.
std::unique_ptr< Capabilities > CapabilitiesPtr
std::string to_hexstring(value_type const &v, const bool skipLeading0x=false) noexcept
Produce a lower-case hexadecimal string representation with leading 0x in MSB of the given pointer.
Gamp: Graphics, Audio, Multimedia and Processing Framework (Native C++, WebAssembly,...
Definition Gamp.hpp:29
__pack(...): Produces MSVC, clang and gcc compatible lead-in and -out macros.
Definition backtrace.hpp:32
STL namespace.
std::size_t operator()(gamp::wt::Capabilities const &a) const noexcept