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