Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qsurfaceformat.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#include "qsurfaceformat.h"
5
6#include <QtCore/qatomic.h>
7#include <QtCore/QDebug>
8#include <QOpenGLContext>
9#include <QtGui/qcolorspace.h>
10#include <QtGui/qguiapplication.h>
11
12#ifdef major
13#undef major
14#endif
15
16#ifdef minor
17#undef minor
18#endif
19
21
23{
24public:
25 explicit QSurfaceFormatPrivate(QSurfaceFormat::FormatOptions _opts = { })
26 : ref(1)
27 , opts(_opts)
28 , redBufferSize(-1)
29 , greenBufferSize(-1)
30 , blueBufferSize(-1)
31 , alphaBufferSize(-1)
32 , depthSize(-1)
33 , stencilSize(-1)
34 , swapBehavior(QSurfaceFormat::DefaultSwapBehavior)
35 , numSamples(-1)
36 , renderableType(QSurfaceFormat::DefaultRenderableType)
37 , profile(QSurfaceFormat::NoProfile)
38 , major(2)
39 , minor(0)
40 , swapInterval(1) // default to vsync
41 {
42 }
43
63
65 QSurfaceFormat::FormatOptions opts;
76 int major;
77 int minor;
80};
81
205
209QSurfaceFormat::QSurfaceFormat(QSurfaceFormat::FormatOptions options) :
210 d(new QSurfaceFormatPrivate(options))
211{
212}
213
217void QSurfaceFormat::detach()
218{
219 if (d->ref.loadRelaxed() != 1) {
221 if (!d->ref.deref())
222 delete d;
223 d = newd;
224 }
225}
226
231{
232 d = other.d;
233 d->ref.ref();
234}
235
240{
241 if (d != other.d) {
242 other.d->ref.ref();
243 if (!d->ref.deref())
244 delete d;
245 d = other.d;
246 }
247 return *this;
248}
249
254{
255 if (!d->ref.deref())
256 delete d;
257}
258
280{
281 QSurfaceFormat::FormatOptions newOptions = d->opts;
282 newOptions.setFlag(QSurfaceFormat::StereoBuffers, enable);
283
284 if (int(newOptions) != int(d->opts)) {
285 detach();
286 d->opts = newOptions;
287 }
288}
289
298{
299 return d->numSamples;
300}
301
308void QSurfaceFormat::setSamples(int numSamples)
309{
310 if (d->numSamples != numSamples) {
311 detach();
312 d->numSamples = numSamples;
313 }
314}
315
326void QSurfaceFormat::setOptions(QSurfaceFormat::FormatOptions options)
327{
328 if (int(d->opts) != int(options)) {
329 detach();
330 d->opts = options;
331 }
332}
333
345{
346 if (testOption(option) == on)
347 return;
348 detach();
349 if (on)
350 d->opts |= option;
351 else
352 d->opts &= ~option;
353}
354
366
374QSurfaceFormat::FormatOptions QSurfaceFormat::options() const
375{
376 return d->opts;
377}
378
385{
386 if (d->depthSize != size) {
387 detach();
388 d->depthSize = size;
389 }
390}
391
398{
399 return d->depthSize;
400}
401
410{
411 if (d->swapBehavior != behavior) {
412 detach();
413 d->swapBehavior = behavior;
414 }
415}
416
426
434{
435 return d->alphaBufferSize > 0;
436}
437
444{
445 if (d->stencilSize != size) {
446 detach();
447 d->stencilSize = size;
448 }
449}
450
457{
458 return d->stencilSize;
459}
460
465{
466 return d->redBufferSize;
467}
468
473{
474 return d->greenBufferSize;
475}
476
481{
482 return d->blueBufferSize;
483}
484
489{
490 return d->alphaBufferSize;
491}
492
497{
498 if (d->redBufferSize != size) {
499 detach();
500 d->redBufferSize = size;
501 }
502}
503
508{
509 if (d->greenBufferSize != size) {
510 detach();
512 }
513}
514
519{
520 if (d->blueBufferSize != size) {
521 detach();
522 d->blueBufferSize = size;
523 }
524}
525
530{
531 if (d->alphaBufferSize != size) {
532 detach();
534 }
535}
536
543{
544 if (d->renderableType != type) {
545 detach();
546 d->renderableType = type;
547 }
548}
549
559
567{
568 if (d->profile != profile) {
569 detach();
570 d->profile = profile;
571 }
572}
573
584
589{
590 if (d->major != major) {
591 detach();
592 d->major = major;
593 }
594}
595
602{
603 return d->major;
604}
605
612{
613 if (d->minor != minor) {
614 detach();
615 d->minor = minor;
616 }
617}
618
623{
624 return d->minor;
625}
626
632QPair<int, int> QSurfaceFormat::version() const
633{
634 return qMakePair(d->major, d->minor);
635}
636
642void QSurfaceFormat::setVersion(int major, int minor)
643{
644 if (d->minor != minor || d->major != major) {
645 detach();
646 d->minor = minor;
647 d->major = major;
648 }
649}
650
672{
673 if (d->swapInterval != interval) {
674 detach();
675 d->swapInterval = interval;
676 }
677}
678
687{
688 return d->swapInterval;
689}
690
713{
714 if (d->colorSpace != colorSpace) {
715 detach();
717 }
718}
719
720#if QT_DEPRECATED_SINCE(6, 0)
731void QSurfaceFormat::setColorSpace(ColorSpace colorSpace)
732{
733 switch (colorSpace) {
734 case DefaultColorSpace:
736 break;
737 case sRGBColorSpace:
739 break;
740 }
741}
742#endif // QT_DEPRECATED_SINCE(6, 0)
743
752{
753 return d->colorSpace;
754}
755
756Q_GLOBAL_STATIC(QSurfaceFormat, qt_default_surface_format)
757
758
781void QSurfaceFormat::setDefaultFormat(const QSurfaceFormat &format)
782{
783#ifndef QT_NO_OPENGL
784 if (qApp) {
786 if (globalContext && globalContext->isValid()) {
787 qWarning("Warning: Setting a new default format with a different version or profile "
788 "after the global shared context is created may cause issues with context "
789 "sharing.");
790 }
791 }
792#endif
793 *qt_default_surface_format() = format;
794}
795
805{
806 return *qt_default_surface_format();
807}
808
826bool QSurfaceFormat::equals(const QSurfaceFormat& other) const noexcept
827{
828 return (d == other.d) || ((int) d->opts == (int) other.d->opts
829 && d->stencilSize == other.d->stencilSize
830 && d->redBufferSize == other.d->redBufferSize
831 && d->greenBufferSize == other.d->greenBufferSize
832 && d->blueBufferSize == other.d->blueBufferSize
833 && d->alphaBufferSize == other.d->alphaBufferSize
834 && d->depthSize == other.d->depthSize
835 && d->numSamples == other.d->numSamples
836 && d->swapBehavior == other.d->swapBehavior
837 && d->profile == other.d->profile
838 && d->major == other.d->major
839 && d->minor == other.d->minor
840 && d->swapInterval == other.d->swapInterval);
841}
842
843#ifndef QT_NO_DEBUG_STREAM
845{
846 const QSurfaceFormatPrivate * const d = f.d;
847 QDebugStateSaver saver(dbg);
848
849 dbg.nospace() << "QSurfaceFormat("
850 << "version " << d->major << '.' << d->minor
851 << ", options " << d->opts
852 << ", depthBufferSize " << d->depthSize
853 << ", redBufferSize " << d->redBufferSize
854 << ", greenBufferSize " << d->greenBufferSize
855 << ", blueBufferSize " << d->blueBufferSize
856 << ", alphaBufferSize " << d->alphaBufferSize
857 << ", stencilBufferSize " << d->stencilSize
858 << ", samples " << d->numSamples
859 << ", swapBehavior " << d->swapBehavior
860 << ", swapInterval " << d->swapInterval
861 << ", colorSpace " << d->colorSpace
862 << ", profile " << d->profile
863 << ')';
864
865 return dbg;
866}
867#endif
868
870
871#include "moc_qsurfaceformat.cpp"
\inmodule QtCore
Definition qatomic.h:112
bool ref() noexcept
bool deref() noexcept
T loadRelaxed() const noexcept
The QColorSpace class provides a color space abstraction.
Definition qcolorspace.h:21
\inmodule QtCore
\inmodule QtCore
\inmodule QtGui
static QOpenGLContext * globalShareContext()
QSurfaceFormat::OpenGLContextProfile profile
QSurfaceFormatPrivate(QSurfaceFormat::FormatOptions _opts={ })
QSurfaceFormatPrivate(const QSurfaceFormatPrivate *other)
QSurfaceFormat::FormatOptions opts
QSurfaceFormat::RenderableType renderableType
QSurfaceFormat::SwapBehavior swapBehavior
The QSurfaceFormat class represents the format of a QSurface. \inmodule QtGui.
int minorVersion() const
Returns the minor OpenGL version.
void setMinorVersion(int minorVersion)
Sets the desired minor OpenGL version.
int blueBufferSize() const
Get the size in bits of the blue channel of the color buffer.
int samples() const
Returns the number of samples per pixel when multisampling is enabled, or -1 when multisampling is di...
void setSwapBehavior(SwapBehavior behavior)
Set the swap behavior of the surface.
SwapBehavior
This enum is used by QSurfaceFormat to specify the swap behaviour of a surface.
void setRenderableType(RenderableType type)
Sets the desired renderable type.
void setDepthBufferSize(int size)
Set the minimum depth buffer size to size.
RenderableType
This enum specifies the rendering backend for the surface.
void setOptions(QSurfaceFormat::FormatOptions options)
OpenGLContextProfile profile() const
Get the configured OpenGL context profile.
static QSurfaceFormat defaultFormat()
Returns the global default surface format.
void setGreenBufferSize(int size)
Set the desired size in bits of the green channel of the color buffer.
void setRedBufferSize(int size)
Set the desired size in bits of the red channel of the color buffer.
int alphaBufferSize() const
Get the size in bits of the alpha channel of the color buffer.
void setProfile(OpenGLContextProfile profile)
Sets the desired OpenGL context profile.
QSurfaceFormat::FormatOptions options() const
void setBlueBufferSize(int size)
Set the desired size in bits of the blue channel of the color buffer.
void setAlphaBufferSize(int size)
Set the desired size in bits of the alpha channel of the color buffer.
int swapInterval() const
Returns the swap interval.
void setVersion(int major, int minor)
Sets the desired major and minor OpenGL versions.
OpenGLContextProfile
This enum is used to specify the OpenGL context profile, in conjunction with QSurfaceFormat::setMajor...
int stencilBufferSize() const
Returns the stencil buffer size in bits.
~QSurfaceFormat()
Destroys the QSurfaceFormat.
void setStencilBufferSize(int size)
Set the preferred stencil buffer size to size bits.
FormatOption
This enum contains format options for use with QSurfaceFormat.
void setSamples(int numSamples)
Set the preferred number of samples per pixel when multisampling is enabled to numSamples.
int depthBufferSize() const
Returns the depth buffer size.
int redBufferSize() const
Get the size in bits of the red channel of the color buffer.
void setColorSpace(const QColorSpace &colorSpace)
Sets the preferred colorSpace.
int majorVersion() const
Returns the major OpenGL version.
RenderableType renderableType() const
Gets the renderable type.
void setSwapInterval(int interval)
Sets the preferred swap interval.
bool hasAlpha() const
Returns true if the alpha buffer size is greater than zero.
void setMajorVersion(int majorVersion)
Sets the desired major OpenGL version.
const QColorSpace & colorSpace() const
SwapBehavior swapBehavior() const
Returns the configured swap behaviour.
void setStereo(bool enable)
If enable is true enables stereo buffering; otherwise disables stereo buffering.
QSurfaceFormat()
Constructs a default initialized QSurfaceFormat.
int greenBufferSize() const
Get the size in bits of the green channel of the color buffer.
void setOption(FormatOption option, bool on=true)
QSurfaceFormat & operator=(const QSurfaceFormat &other)
Assigns other to this object.
bool testOption(FormatOption option) const
QPair< int, int > version() const
Returns a QPair<int, int> representing the OpenGL version.
Combined button and popup list for selecting options.
#define qApp
DBusConnection const char DBusError DBusBusType DBusError return DBusConnection DBusHandleMessageFunction void DBusFreeFunction return DBusConnection return DBusConnection return const char DBusError return DBusConnection DBusMessage dbus_uint32_t return DBusConnection dbus_bool_t DBusConnection DBusAddWatchFunction DBusRemoveWatchFunction DBusWatchToggledFunction void DBusFreeFunction return DBusConnection DBusDispatchStatusFunction void DBusFreeFunction DBusTimeout return DBusTimeout return DBusWatch return DBusWatch unsigned int return DBusError const DBusError return const DBusMessage return DBusMessage return DBusMessage return DBusMessage return DBusMessage return DBusMessage return DBusMessageIter int const void return DBusMessageIter DBusMessageIter return DBusMessageIter void DBusMessageIter void int return DBusMessage DBusMessageIter return DBusMessageIter return DBusMessageIter DBusMessageIter const char const char const char const char return DBusMessage return DBusMessage const char return DBusMessage dbus_bool_t return DBusMessage dbus_uint32_t return DBusMessage void
#define Q_GLOBAL_STATIC(TYPE, NAME,...)
#define qWarning
Definition qlogging.h:166
GLenum GLuint GLintptr GLsizeiptr size
[1]
GLfloat GLfloat f
GLenum type
GLboolean enable
GLint ref
GLint GLsizei GLsizei GLenum format
GLuint GLenum option
QT_BEGIN_NAMESPACE constexpr decltype(auto) qMakePair(T1 &&value1, T2 &&value2) noexcept(noexcept(std::make_pair(std::forward< T1 >(value1), std::forward< T2 >(value2))))
Definition qpair.h:19
QDebug operator<<(QDebug dbg, const QSurfaceFormat &f)
QSharedPointer< T > other(t)
[5]