QtBase  v6.3.1
qsurfaceformat.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the QtGui module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 3 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL3 included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 3 requirements
23 ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24 **
25 ** GNU General Public License Usage
26 ** Alternatively, this file may be used under the terms of the GNU
27 ** General Public License version 2.0 or (at your option) the GNU General
28 ** Public license version 3 or any later version approved by the KDE Free
29 ** Qt Foundation. The licenses are as published by the Free Software
30 ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31 ** included in the packaging of this file. Please review the following
32 ** information to ensure the GNU General Public License requirements will
33 ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34 ** https://www.gnu.org/licenses/gpl-3.0.html.
35 **
36 ** $QT_END_LICENSE$
37 **
38 ****************************************************************************/
39 
40 #include "qsurfaceformat.h"
41 
42 #include <QtCore/qatomic.h>
43 #include <QtCore/QDebug>
44 #include <QOpenGLContext>
45 #include <QtGui/qcolorspace.h>
46 #include <QtGui/qguiapplication.h>
47 
48 #ifdef major
49 #undef major
50 #endif
51 
52 #ifdef minor
53 #undef minor
54 #endif
55 
57 
59 {
60 public:
61  explicit QSurfaceFormatPrivate(QSurfaceFormat::FormatOptions _opts = { })
62  : ref(1)
63  , opts(_opts)
64  , redBufferSize(-1)
65  , greenBufferSize(-1)
66  , blueBufferSize(-1)
67  , alphaBufferSize(-1)
68  , depthSize(-1)
69  , stencilSize(-1)
71  , numSamples(-1)
74  , major(2)
75  , minor(0)
76  , swapInterval(1) // default to vsync
77  {
78  }
79 
81  : ref(1),
82  opts(other->opts),
93  major(other->major),
94  minor(other->minor),
97  {
98  }
99 
101  QSurfaceFormat::FormatOptions opts;
112  int major;
113  int minor;
116 };
117 
239 {
240 }
241 
245 QSurfaceFormat::QSurfaceFormat(QSurfaceFormat::FormatOptions options) :
246  d(new QSurfaceFormatPrivate(options))
247 {
248 }
249 
253 void QSurfaceFormat::detach()
254 {
255  if (d->ref.loadRelaxed() != 1) {
257  if (!d->ref.deref())
258  delete d;
259  d = newd;
260  }
261 }
262 
267 {
268  d = other.d;
269  d->ref.ref();
270 }
271 
276 {
277  if (d != other.d) {
278  other.d->ref.ref();
279  if (!d->ref.deref())
280  delete d;
281  d = other.d;
282  }
283  return *this;
284 }
285 
290 {
291  if (!d->ref.deref())
292  delete d;
293 }
294 
316 {
317  QSurfaceFormat::FormatOptions newOptions = d->opts;
318  newOptions.setFlag(QSurfaceFormat::StereoBuffers, enable);
319 
320  if (int(newOptions) != int(d->opts)) {
321  detach();
322  d->opts = newOptions;
323  }
324 }
325 
334 {
335  return d->numSamples;
336 }
337 
344 void QSurfaceFormat::setSamples(int numSamples)
345 {
346  if (d->numSamples != numSamples) {
347  detach();
348  d->numSamples = numSamples;
349  }
350 }
351 
362 void QSurfaceFormat::setOptions(QSurfaceFormat::FormatOptions options)
363 {
364  if (int(d->opts) != int(options)) {
365  detach();
366  d->opts = options;
367  }
368 }
369 
381 {
382  if (testOption(option) == on)
383  return;
384  detach();
385  if (on)
386  d->opts |= option;
387  else
388  d->opts &= ~option;
389 }
390 
399 {
400  return d->opts & option;
401 }
402 
410 QSurfaceFormat::FormatOptions QSurfaceFormat::options() const
411 {
412  return d->opts;
413 }
414 
421 {
422  if (d->depthSize != size) {
423  detach();
424  d->depthSize = size;
425  }
426 }
427 
434 {
435  return d->depthSize;
436 }
437 
446 {
447  if (d->swapBehavior != behavior) {
448  detach();
449  d->swapBehavior = behavior;
450  }
451 }
452 
459 {
460  return d->swapBehavior;
461 }
462 
470 {
471  return d->alphaBufferSize > 0;
472 }
473 
480 {
481  if (d->stencilSize != size) {
482  detach();
483  d->stencilSize = size;
484  }
485 }
486 
493 {
494  return d->stencilSize;
495 }
496 
501 {
502  return d->redBufferSize;
503 }
504 
509 {
510  return d->greenBufferSize;
511 }
512 
517 {
518  return d->blueBufferSize;
519 }
520 
525 {
526  return d->alphaBufferSize;
527 }
528 
533 {
534  if (d->redBufferSize != size) {
535  detach();
536  d->redBufferSize = size;
537  }
538 }
539 
544 {
545  if (d->greenBufferSize != size) {
546  detach();
547  d->greenBufferSize = size;
548  }
549 }
550 
555 {
556  if (d->blueBufferSize != size) {
557  detach();
558  d->blueBufferSize = size;
559  }
560 }
561 
566 {
567  if (d->alphaBufferSize != size) {
568  detach();
569  d->alphaBufferSize = size;
570  }
571 }
572 
579 {
580  if (d->renderableType != type) {
581  detach();
582  d->renderableType = type;
583  }
584 }
585 
592 {
593  return d->renderableType;
594 }
595 
603 {
604  if (d->profile != profile) {
605  detach();
606  d->profile = profile;
607  }
608 }
609 
617 {
618  return d->profile;
619 }
620 
625 {
626  if (d->major != major) {
627  detach();
628  d->major = major;
629  }
630 }
631 
638 {
639  return d->major;
640 }
641 
648 {
649  if (d->minor != minor) {
650  detach();
651  d->minor = minor;
652  }
653 }
654 
659 {
660  return d->minor;
661 }
662 
669 {
670  return qMakePair(d->major, d->minor);
671 }
672 
678 void QSurfaceFormat::setVersion(int major, int minor)
679 {
680  if (d->minor != minor || d->major != major) {
681  detach();
682  d->minor = minor;
683  d->major = major;
684  }
685 }
686 
708 {
709  if (d->swapInterval != interval) {
710  detach();
711  d->swapInterval = interval;
712  }
713 }
714 
723 {
724  return d->swapInterval;
725 }
726 
749 {
750  if (d->colorSpace != colorSpace) {
751  detach();
752  d->colorSpace = colorSpace;
753  }
754 }
755 
765 void QSurfaceFormat::setColorSpace(ColorSpace colorSpace)
766 {
767  switch (colorSpace) {
768  case DefaultColorSpace:
770  break;
771  case sRGBColorSpace:
773  break;
774  }
775 }
776 
785 {
786  return d->colorSpace;
787 }
788 
789 Q_GLOBAL_STATIC(QSurfaceFormat, qt_default_surface_format)
790 
791 
815 {
816 #ifndef QT_NO_OPENGL
817  if (qApp) {
819  if (globalContext && globalContext->isValid()) {
820  qWarning("Warning: Setting a new default format with a different version or profile "
821  "after the global shared context is created may cause issues with context "
822  "sharing.");
823  }
824  }
825 #endif
826  *qt_default_surface_format() = format;
827 }
828 
838 {
839  return *qt_default_surface_format();
840 }
841 
859 bool QSurfaceFormat::equals(const QSurfaceFormat& other) const noexcept
860 {
861  return (d == other.d) || ((int) d->opts == (int) other.d->opts
862  && d->stencilSize == other.d->stencilSize
863  && d->redBufferSize == other.d->redBufferSize
864  && d->greenBufferSize == other.d->greenBufferSize
865  && d->blueBufferSize == other.d->blueBufferSize
866  && d->alphaBufferSize == other.d->alphaBufferSize
867  && d->depthSize == other.d->depthSize
868  && d->numSamples == other.d->numSamples
869  && d->swapBehavior == other.d->swapBehavior
870  && d->profile == other.d->profile
871  && d->major == other.d->major
872  && d->minor == other.d->minor
873  && d->swapInterval == other.d->swapInterval);
874 }
875 
876 #ifndef QT_NO_DEBUG_STREAM
878 {
879  const QSurfaceFormatPrivate * const d = f.d;
880  QDebugStateSaver saver(dbg);
881 
882  dbg.nospace() << "QSurfaceFormat("
883  << "version " << d->major << '.' << d->minor
884  << ", options " << d->opts
885  << ", depthBufferSize " << d->depthSize
886  << ", redBufferSize " << d->redBufferSize
887  << ", greenBufferSize " << d->greenBufferSize
888  << ", blueBufferSize " << d->blueBufferSize
889  << ", alphaBufferSize " << d->alphaBufferSize
890  << ", stencilBufferSize " << d->stencilSize
891  << ", samples " << d->numSamples
892  << ", swapBehavior " << d->swapBehavior
893  << ", swapInterval " << d->swapInterval
894  << ", colorSpace " << d->colorSpace
895  << ", profile " << d->profile
896  << ')';
897 
898  return dbg;
899 }
900 #endif
901 
903 
904 #include "moc_qsurfaceformat.cpp"
The QAtomicInt class provides platform-independent atomic operations on int.
Definition: qatomic.h:158
bool ref() noexcept
Definition: qbasicatomic.h:101
bool deref() noexcept
Definition: qbasicatomic.h:102
T loadRelaxed() const noexcept
Definition: qbasicatomic.h:90
The QColorSpace class provides a color space abstraction.
Definition: qcolorspace.h:57
operator<<(QDataStream &ds, qfloat16 f)
Definition: qfloat16.cpp:327
The QDebug class provides an output stream for debugging information.
Definition: qdebug.h:65
QDebug & nospace()
Definition: qdebug.h:113
Convenience class for custom QDebug operators.
Definition: qdebug.h:176
The QOpenGLContext class represents a native OpenGL context, enabling OpenGL rendering on a QSurface.
bool isValid() const
static QOpenGLContext * globalShareContext()
The QSurfaceFormat class represents the format of a QSurface. \inmodule QtGui.
int minorVersion() const
void setMinorVersion(int minorVersion)
int blueBufferSize() const
int samples() const
void setSwapBehavior(SwapBehavior behavior)
void setRenderableType(RenderableType type)
void setDepthBufferSize(int size)
void setOptions(QSurfaceFormat::FormatOptions options)
OpenGLContextProfile profile() const
static QSurfaceFormat defaultFormat()
void setGreenBufferSize(int size)
void setRedBufferSize(int size)
int alphaBufferSize() const
void setProfile(OpenGLContextProfile profile)
QSurfaceFormat::FormatOptions options() const
void setBlueBufferSize(int size)
void setAlphaBufferSize(int size)
int swapInterval() const
void setVersion(int major, int minor)
int stencilBufferSize() const
void setStencilBufferSize(int size)
void setSamples(int numSamples)
int depthBufferSize() const
int redBufferSize() const
void setColorSpace(const QColorSpace &colorSpace)
int majorVersion() const
RenderableType renderableType() const
void setSwapInterval(int interval)
bool hasAlpha() const
void setMajorVersion(int majorVersion)
const QColorSpace & colorSpace() const
SwapBehavior swapBehavior() const
void setStereo(bool enable)
int greenBufferSize() const
void setOption(FormatOption option, bool on=true)
QSurfaceFormat & operator=(const QSurfaceFormat &other)
bool testOption(FormatOption option) const
QPair< int, int > version() const
QSurfaceFormat::OpenGLContextProfile profile
QSurfaceFormatPrivate(QSurfaceFormat::FormatOptions _opts={ })
QSurfaceFormatPrivate(const QSurfaceFormatPrivate *other)
QSurfaceFormat::FormatOptions opts
QSurfaceFormat::RenderableType renderableType
QSurfaceFormat::SwapBehavior swapBehavior
auto it unsigned count const
Definition: hb-iter.hh:848
void
Definition: png.h:1080
std::pair< T1, T2 > QPair
Definition: qcontainerfwd.h:56
#define qApp
#define Q_GLOBAL_STATIC(TYPE, NAME,...)
#define qWarning
Definition: qlogging.h:179
GLenum type
Definition: qopengl.h:270
GLenum GLuint GLintptr GLsizeiptr size
[1]
GLfloat GLfloat f
GLboolean enable
GLint ref
GLint GLsizei GLsizei GLenum format
GLuint GLenum option
Definition: qopenglext.h:5929
constexpr decltype(auto) qMakePair(T1 &&value1, T2 &&value2) noexcept(noexcept(std::make_pair(std::forward< T1 >(value1), std::forward< T2 >(value2))))
Definition: qpair.h:55
QSharedPointer< T > other(t)
[5]