| 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 | |
| 20 | QT_BEGIN_NAMESPACE |
| 21 | |
| 22 | class QSurfaceFormatPrivate |
| 23 | { |
| 24 | public: |
| 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 | |
| 44 | QSurfaceFormatPrivate(const QSurfaceFormatPrivate *other) |
| 45 | : ref(1), |
| 46 | opts(other->opts), |
| 47 | redBufferSize(other->redBufferSize), |
| 48 | greenBufferSize(other->greenBufferSize), |
| 49 | blueBufferSize(other->blueBufferSize), |
| 50 | alphaBufferSize(other->alphaBufferSize), |
| 51 | depthSize(other->depthSize), |
| 52 | stencilSize(other->stencilSize), |
| 53 | swapBehavior(other->swapBehavior), |
| 54 | numSamples(other->numSamples), |
| 55 | renderableType(other->renderableType), |
| 56 | profile(other->profile), |
| 57 | major(other->major), |
| 58 | minor(other->minor), |
| 59 | swapInterval(other->swapInterval), |
| 60 | colorSpace(other->colorSpace) |
| 61 | { |
| 62 | } |
| 63 | |
| 64 | QAtomicInt ref; |
| 65 | QSurfaceFormat::FormatOptions opts; |
| 66 | int redBufferSize; |
| 67 | int greenBufferSize; |
| 68 | int blueBufferSize; |
| 69 | int alphaBufferSize; |
| 70 | int depthSize; |
| 71 | int stencilSize; |
| 72 | QSurfaceFormat::SwapBehavior swapBehavior; |
| 73 | int numSamples; |
| 74 | QSurfaceFormat::RenderableType renderableType; |
| 75 | QSurfaceFormat::OpenGLContextProfile profile; |
| 76 | int major; |
| 77 | int minor; |
| 78 | int swapInterval; |
| 79 | QColorSpace colorSpace; |
| 80 | }; |
| 81 | |
| 82 | /*! |
| 83 | \class QSurfaceFormat |
| 84 | \since 5.0 |
| 85 | \brief The QSurfaceFormat class represents the format of a QSurface. |
| 86 | \inmodule QtGui |
| 87 | |
| 88 | The format includes the size of the color buffers, red, green, and blue; |
| 89 | the size of the alpha buffer; the size of the depth and stencil buffers; |
| 90 | and number of samples per pixel for multisampling. In addition, the format |
| 91 | contains surface configuration parameters such as OpenGL profile and |
| 92 | version for rendering, whether or not to enable stereo buffers, and swap |
| 93 | behaviour. |
| 94 | |
| 95 | \note When troubleshooting context or window format issues, it can be |
| 96 | helpful to enable the logging category \c{qt.qpa.gl}. Depending on the |
| 97 | platform, this may print useful debug information when it comes to OpenGL |
| 98 | initialization and the native visual or framebuffer configurations which |
| 99 | QSurfaceFormat gets mapped to. |
| 100 | */ |
| 101 | |
| 102 | /*! |
| 103 | \enum QSurfaceFormat::FormatOption |
| 104 | |
| 105 | This enum contains format options for use with QSurfaceFormat. |
| 106 | |
| 107 | \value StereoBuffers Used to request stereo buffers in the surface format. |
| 108 | \value DebugContext Used to request a debug context with extra debugging information. |
| 109 | \value DeprecatedFunctions Used to request that deprecated functions be included |
| 110 | in the OpenGL context profile. If not specified, you should get a forward compatible context |
| 111 | without support functionality marked as deprecated. This requires OpenGL version 3.0 or higher. |
| 112 | \value ResetNotification Enables notifications about resets of the OpenGL context. The status is then |
| 113 | queryable via the context's \l{QOpenGLContext::isValid()}{isValid()} function. Note that not setting |
| 114 | this flag does not guarantee that context state loss never occurs. Additionally, some implementations |
| 115 | may choose to report context loss regardless of this flag. Platforms that support dynamically enabling |
| 116 | the monitoring of the loss of context, such as, Windows with WGL, or Linux/X11 (xcb) with GLX, will |
| 117 | monitor the status in every call to \l{QOpenGLContext::makeCurrent()}{makeCurrent()}. See |
| 118 | \l{QOpenGLContext::isValid()}{isValid()} for more information on this. |
| 119 | \value ProtectedContent Enables access to protected content. This allows the GPU to operate on protected |
| 120 | resources (surfaces, buffers, textures), for example DRM-protected video content. |
| 121 | Currently only implemented for EGL. |
| 122 | */ |
| 123 | |
| 124 | /*! |
| 125 | \enum QSurfaceFormat::SwapBehavior |
| 126 | |
| 127 | This enum is used by QSurfaceFormat to specify the swap behaviour of a surface. The swap behaviour |
| 128 | is mostly transparent to the application, but it affects factors such as rendering latency and |
| 129 | throughput. |
| 130 | |
| 131 | \value DefaultSwapBehavior The default, unspecified swap behaviour of the platform. |
| 132 | \value SingleBuffer Used to request single buffering, which might result in flickering |
| 133 | when OpenGL rendering is done directly to screen without an intermediate offscreen |
| 134 | buffer. |
| 135 | \value DoubleBuffer This is typically the default swap behaviour on desktop platforms, |
| 136 | consisting of one back buffer and one front buffer. Rendering is done to the back |
| 137 | buffer, and then the back buffer and front buffer are swapped, or the contents of |
| 138 | the back buffer are copied to the front buffer, depending on the implementation. |
| 139 | \value TripleBuffer This swap behaviour is sometimes used in order to decrease the |
| 140 | risk of skipping a frame when the rendering rate is just barely keeping up with |
| 141 | the screen refresh rate. Depending on the platform it might also lead to slightly |
| 142 | more efficient use of the GPU due to improved pipelining behaviour. Triple buffering |
| 143 | comes at the cost of an extra frame of memory usage and latency, and might not be |
| 144 | supported depending on the underlying platform. |
| 145 | */ |
| 146 | |
| 147 | /*! |
| 148 | \enum QSurfaceFormat::RenderableType |
| 149 | |
| 150 | This enum specifies the rendering backend for the surface. |
| 151 | |
| 152 | \value DefaultRenderableType The default, unspecified rendering method |
| 153 | \value OpenGL Desktop OpenGL rendering |
| 154 | \value OpenGLES OpenGL ES 2.0 rendering |
| 155 | \value OpenVG Open Vector Graphics rendering |
| 156 | */ |
| 157 | |
| 158 | /*! |
| 159 | \enum QSurfaceFormat::OpenGLContextProfile |
| 160 | |
| 161 | This enum is used to specify the OpenGL context profile, in |
| 162 | conjunction with QSurfaceFormat::setMajorVersion() and |
| 163 | QSurfaceFormat::setMinorVersion(). |
| 164 | |
| 165 | Profiles are exposed in OpenGL 3.2 and above, and are used |
| 166 | to choose between a restricted core profile, and a compatibility |
| 167 | profile which might contain deprecated support functionality. |
| 168 | |
| 169 | Note that the core profile might still contain functionality that |
| 170 | is deprecated and scheduled for removal in a higher version. To |
| 171 | get access to the deprecated functionality for the core profile |
| 172 | in the set OpenGL version you can use the QSurfaceFormat format option |
| 173 | QSurfaceFormat::DeprecatedFunctions. |
| 174 | |
| 175 | \value NoProfile OpenGL version is lower than 3.2. For 3.2 and newer this is same as CoreProfile. |
| 176 | \value CoreProfile Functionality deprecated in OpenGL version 3.0 is not available. |
| 177 | \value CompatibilityProfile Functionality from earlier OpenGL versions is available. |
| 178 | */ |
| 179 | |
| 180 | /*! |
| 181 | \enum QSurfaceFormat::ColorSpace |
| 182 | \deprecated [6.0] Use setColorSpace(QColorSpace) instead |
| 183 | |
| 184 | This enum is used to specify the preferred color space, controlling if the |
| 185 | window's associated default framebuffer is able to do updates and blending |
| 186 | in a given encoding instead of the standard linear operations. |
| 187 | |
| 188 | \value DefaultColorSpace The default, unspecified color space. |
| 189 | |
| 190 | \value sRGBColorSpace When \c{GL_ARB_framebuffer_sRGB} or |
| 191 | \c{GL_EXT_framebuffer_sRGB} is supported by the platform and this value is |
| 192 | set, the window will be created with an sRGB-capable default |
| 193 | framebuffer. Note that some platforms may return windows with a sRGB-capable |
| 194 | default framebuffer even when not requested explicitly. |
| 195 | */ |
| 196 | |
| 197 | /*! |
| 198 | Constructs a default initialized QSurfaceFormat. |
| 199 | |
| 200 | \note By default OpenGL 2.0 is requested since this provides the highest |
| 201 | grade of portability between platforms and OpenGL implementations. |
| 202 | */ |
| 203 | QSurfaceFormat::QSurfaceFormat() : d(new QSurfaceFormatPrivate) |
| 204 | { |
| 205 | } |
| 206 | |
| 207 | /*! |
| 208 | Constructs a QSurfaceFormat with the given format \a options. |
| 209 | */ |
| 210 | QSurfaceFormat::QSurfaceFormat(QSurfaceFormat::FormatOptions options) : |
| 211 | d(new QSurfaceFormatPrivate(options)) |
| 212 | { |
| 213 | } |
| 214 | |
| 215 | /*! |
| 216 | \internal |
| 217 | */ |
| 218 | void QSurfaceFormat::detach() |
| 219 | { |
| 220 | if (d->ref.loadRelaxed() != 1) { |
| 221 | QSurfaceFormatPrivate *newd = new QSurfaceFormatPrivate(d); |
| 222 | if (!d->ref.deref()) |
| 223 | delete d; |
| 224 | d = newd; |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | /*! |
| 229 | Constructs a copy of \a other. |
| 230 | */ |
| 231 | QSurfaceFormat::QSurfaceFormat(const QSurfaceFormat &other) |
| 232 | { |
| 233 | d = other.d; |
| 234 | d->ref.ref(); |
| 235 | } |
| 236 | |
| 237 | /*! |
| 238 | Assigns \a other to this object. |
| 239 | */ |
| 240 | QSurfaceFormat &QSurfaceFormat::operator=(const QSurfaceFormat &other) |
| 241 | { |
| 242 | if (d != other.d) { |
| 243 | other.d->ref.ref(); |
| 244 | if (!d->ref.deref()) |
| 245 | delete d; |
| 246 | d = other.d; |
| 247 | } |
| 248 | return *this; |
| 249 | } |
| 250 | |
| 251 | /*! |
| 252 | Destroys the QSurfaceFormat. |
| 253 | */ |
| 254 | QSurfaceFormat::~QSurfaceFormat() |
| 255 | { |
| 256 | if (!d->ref.deref()) |
| 257 | delete d; |
| 258 | } |
| 259 | |
| 260 | /*! |
| 261 | \fn bool QSurfaceFormat::stereo() const |
| 262 | |
| 263 | Returns \c true if stereo buffering is enabled; otherwise returns |
| 264 | false. Stereo buffering is disabled by default. |
| 265 | |
| 266 | \sa setStereo() |
| 267 | */ |
| 268 | |
| 269 | /*! |
| 270 | If \a enable is true enables stereo buffering; otherwise disables |
| 271 | stereo buffering. |
| 272 | |
| 273 | Stereo buffering is disabled by default. |
| 274 | |
| 275 | Stereo buffering provides extra color buffers to generate left-eye |
| 276 | and right-eye images. |
| 277 | |
| 278 | \sa stereo() |
| 279 | */ |
| 280 | void QSurfaceFormat::setStereo(bool enable) |
| 281 | { |
| 282 | QSurfaceFormat::FormatOptions newOptions = d->opts; |
| 283 | newOptions.setFlag(flag: QSurfaceFormat::StereoBuffers, on: enable); |
| 284 | |
| 285 | if (int(newOptions) != int(d->opts)) { |
| 286 | detach(); |
| 287 | d->opts = newOptions; |
| 288 | } |
| 289 | } |
| 290 | |
| 291 | /*! |
| 292 | Returns the number of samples per pixel when multisampling is |
| 293 | enabled, or \c -1 when multisampling is disabled. The default |
| 294 | return value is \c -1. |
| 295 | |
| 296 | \sa setSamples() |
| 297 | */ |
| 298 | int QSurfaceFormat::samples() const |
| 299 | { |
| 300 | return d->numSamples; |
| 301 | } |
| 302 | |
| 303 | /*! |
| 304 | Set the preferred number of samples per pixel when multisampling |
| 305 | is enabled to \a numSamples. By default, multisampling is disabled. |
| 306 | |
| 307 | \sa samples() |
| 308 | */ |
| 309 | void QSurfaceFormat::setSamples(int numSamples) |
| 310 | { |
| 311 | if (d->numSamples != numSamples) { |
| 312 | detach(); |
| 313 | d->numSamples = numSamples; |
| 314 | } |
| 315 | } |
| 316 | |
| 317 | /*! |
| 318 | \since 5.3 |
| 319 | |
| 320 | Sets the format options to \a options. |
| 321 | |
| 322 | To verify that an option was respected, compare the actual format to the |
| 323 | requested format after surface/context creation. |
| 324 | |
| 325 | \sa options(), testOption() |
| 326 | */ |
| 327 | void QSurfaceFormat::setOptions(QSurfaceFormat::FormatOptions options) |
| 328 | { |
| 329 | if (int(d->opts) != int(options)) { |
| 330 | detach(); |
| 331 | d->opts = options; |
| 332 | } |
| 333 | } |
| 334 | |
| 335 | /*! |
| 336 | \since 5.3 |
| 337 | |
| 338 | Sets the format option \a option if \a on is true; otherwise, clears the option. |
| 339 | |
| 340 | To verify that an option was respected, compare the actual format to the |
| 341 | requested format after surface/context creation. |
| 342 | |
| 343 | \sa setOptions(), options(), testOption() |
| 344 | */ |
| 345 | void QSurfaceFormat::setOption(QSurfaceFormat::FormatOption option, bool on) |
| 346 | { |
| 347 | if (testOption(option) == on) |
| 348 | return; |
| 349 | detach(); |
| 350 | if (on) |
| 351 | d->opts |= option; |
| 352 | else |
| 353 | d->opts &= ~option; |
| 354 | } |
| 355 | |
| 356 | /*! |
| 357 | \since 5.3 |
| 358 | |
| 359 | Returns true if the format option \a option is set; otherwise returns false. |
| 360 | |
| 361 | \sa options() |
| 362 | */ |
| 363 | bool QSurfaceFormat::testOption(QSurfaceFormat::FormatOption option) const |
| 364 | { |
| 365 | return d->opts & option; |
| 366 | } |
| 367 | |
| 368 | /*! |
| 369 | \since 5.3 |
| 370 | |
| 371 | Returns the currently set format options. |
| 372 | |
| 373 | \sa setOption(), setOptions(), testOption() |
| 374 | */ |
| 375 | QSurfaceFormat::FormatOptions QSurfaceFormat::options() const |
| 376 | { |
| 377 | return d->opts; |
| 378 | } |
| 379 | |
| 380 | /*! |
| 381 | Set the minimum depth buffer size to \a size. |
| 382 | |
| 383 | \sa depthBufferSize() |
| 384 | */ |
| 385 | void QSurfaceFormat::setDepthBufferSize(int size) |
| 386 | { |
| 387 | if (d->depthSize != size) { |
| 388 | detach(); |
| 389 | d->depthSize = size; |
| 390 | } |
| 391 | } |
| 392 | |
| 393 | /*! |
| 394 | Returns the depth buffer size. |
| 395 | |
| 396 | \sa setDepthBufferSize() |
| 397 | */ |
| 398 | int QSurfaceFormat::depthBufferSize() const |
| 399 | { |
| 400 | return d->depthSize; |
| 401 | } |
| 402 | |
| 403 | /*! |
| 404 | Set the swap \a behavior of the surface. |
| 405 | |
| 406 | The swap behavior specifies whether single, double, or triple |
| 407 | buffering is desired. The default, DefaultSwapBehavior, |
| 408 | gives the default swap behavior of the platform. |
| 409 | */ |
| 410 | void QSurfaceFormat::setSwapBehavior(SwapBehavior behavior) |
| 411 | { |
| 412 | if (d->swapBehavior != behavior) { |
| 413 | detach(); |
| 414 | d->swapBehavior = behavior; |
| 415 | } |
| 416 | } |
| 417 | |
| 418 | /*! |
| 419 | Returns the configured swap behaviour. |
| 420 | |
| 421 | \sa setSwapBehavior() |
| 422 | */ |
| 423 | QSurfaceFormat::SwapBehavior QSurfaceFormat::swapBehavior() const |
| 424 | { |
| 425 | return d->swapBehavior; |
| 426 | } |
| 427 | |
| 428 | /*! |
| 429 | Returns \c true if the alpha buffer size is greater than zero. |
| 430 | |
| 431 | This means that the surface might be used with per pixel |
| 432 | translucency effects. |
| 433 | */ |
| 434 | bool QSurfaceFormat::hasAlpha() const |
| 435 | { |
| 436 | return d->alphaBufferSize > 0; |
| 437 | } |
| 438 | |
| 439 | /*! |
| 440 | Set the preferred stencil buffer size to \a size bits. |
| 441 | |
| 442 | \sa stencilBufferSize() |
| 443 | */ |
| 444 | void QSurfaceFormat::setStencilBufferSize(int size) |
| 445 | { |
| 446 | if (d->stencilSize != size) { |
| 447 | detach(); |
| 448 | d->stencilSize = size; |
| 449 | } |
| 450 | } |
| 451 | |
| 452 | /*! |
| 453 | Returns the stencil buffer size in bits. |
| 454 | |
| 455 | \sa setStencilBufferSize() |
| 456 | */ |
| 457 | int QSurfaceFormat::stencilBufferSize() const |
| 458 | { |
| 459 | return d->stencilSize; |
| 460 | } |
| 461 | |
| 462 | /*! |
| 463 | Get the size in bits of the red channel of the color buffer. |
| 464 | */ |
| 465 | int QSurfaceFormat::redBufferSize() const |
| 466 | { |
| 467 | return d->redBufferSize; |
| 468 | } |
| 469 | |
| 470 | /*! |
| 471 | Get the size in bits of the green channel of the color buffer. |
| 472 | */ |
| 473 | int QSurfaceFormat::greenBufferSize() const |
| 474 | { |
| 475 | return d->greenBufferSize; |
| 476 | } |
| 477 | |
| 478 | /*! |
| 479 | Get the size in bits of the blue channel of the color buffer. |
| 480 | */ |
| 481 | int QSurfaceFormat::blueBufferSize() const |
| 482 | { |
| 483 | return d->blueBufferSize; |
| 484 | } |
| 485 | |
| 486 | /*! |
| 487 | Get the size in bits of the alpha channel of the color buffer. |
| 488 | */ |
| 489 | int QSurfaceFormat::alphaBufferSize() const |
| 490 | { |
| 491 | return d->alphaBufferSize; |
| 492 | } |
| 493 | |
| 494 | /*! |
| 495 | Set the desired \a size in bits of the red channel of the color buffer. |
| 496 | */ |
| 497 | void QSurfaceFormat::setRedBufferSize(int size) |
| 498 | { |
| 499 | if (d->redBufferSize != size) { |
| 500 | detach(); |
| 501 | d->redBufferSize = size; |
| 502 | } |
| 503 | } |
| 504 | |
| 505 | /*! |
| 506 | Set the desired \a size in bits of the green channel of the color buffer. |
| 507 | */ |
| 508 | void QSurfaceFormat::setGreenBufferSize(int size) |
| 509 | { |
| 510 | if (d->greenBufferSize != size) { |
| 511 | detach(); |
| 512 | d->greenBufferSize = size; |
| 513 | } |
| 514 | } |
| 515 | |
| 516 | /*! |
| 517 | Set the desired \a size in bits of the blue channel of the color buffer. |
| 518 | */ |
| 519 | void QSurfaceFormat::setBlueBufferSize(int size) |
| 520 | { |
| 521 | if (d->blueBufferSize != size) { |
| 522 | detach(); |
| 523 | d->blueBufferSize = size; |
| 524 | } |
| 525 | } |
| 526 | |
| 527 | /*! |
| 528 | Set the desired \a size in bits of the alpha channel of the color buffer. |
| 529 | */ |
| 530 | void QSurfaceFormat::setAlphaBufferSize(int size) |
| 531 | { |
| 532 | if (d->alphaBufferSize != size) { |
| 533 | detach(); |
| 534 | d->alphaBufferSize = size; |
| 535 | } |
| 536 | } |
| 537 | |
| 538 | /*! |
| 539 | Sets the desired renderable \a type. |
| 540 | |
| 541 | Chooses between desktop OpenGL, OpenGL ES, and OpenVG. |
| 542 | */ |
| 543 | void QSurfaceFormat::setRenderableType(RenderableType type) |
| 544 | { |
| 545 | if (d->renderableType != type) { |
| 546 | detach(); |
| 547 | d->renderableType = type; |
| 548 | } |
| 549 | } |
| 550 | |
| 551 | /*! |
| 552 | Gets the renderable type. |
| 553 | |
| 554 | Chooses between desktop OpenGL, OpenGL ES, and OpenVG. |
| 555 | */ |
| 556 | QSurfaceFormat::RenderableType QSurfaceFormat::renderableType() const |
| 557 | { |
| 558 | return d->renderableType; |
| 559 | } |
| 560 | |
| 561 | /*! |
| 562 | Sets the desired OpenGL context \a profile. |
| 563 | |
| 564 | This setting is ignored if the requested OpenGL version is |
| 565 | less than 3.2. |
| 566 | */ |
| 567 | void QSurfaceFormat::setProfile(OpenGLContextProfile profile) |
| 568 | { |
| 569 | if (d->profile != profile) { |
| 570 | detach(); |
| 571 | d->profile = profile; |
| 572 | } |
| 573 | } |
| 574 | |
| 575 | /*! |
| 576 | Get the configured OpenGL context profile. |
| 577 | |
| 578 | This setting is ignored if the requested OpenGL version is |
| 579 | less than 3.2. |
| 580 | */ |
| 581 | QSurfaceFormat::OpenGLContextProfile QSurfaceFormat::profile() const |
| 582 | { |
| 583 | return d->profile; |
| 584 | } |
| 585 | |
| 586 | /*! |
| 587 | Sets the desired \a major OpenGL version. |
| 588 | */ |
| 589 | void QSurfaceFormat::setMajorVersion(int major) |
| 590 | { |
| 591 | if (d->major != major) { |
| 592 | detach(); |
| 593 | d->major = major; |
| 594 | } |
| 595 | } |
| 596 | |
| 597 | /*! |
| 598 | Returns the major OpenGL version. |
| 599 | |
| 600 | The default version is 2.0. |
| 601 | */ |
| 602 | int QSurfaceFormat::majorVersion() const |
| 603 | { |
| 604 | return d->major; |
| 605 | } |
| 606 | |
| 607 | /*! |
| 608 | Sets the desired \a minor OpenGL version. |
| 609 | |
| 610 | The default version is 2.0. |
| 611 | */ |
| 612 | void QSurfaceFormat::setMinorVersion(int minor) |
| 613 | { |
| 614 | if (d->minor != minor) { |
| 615 | detach(); |
| 616 | d->minor = minor; |
| 617 | } |
| 618 | } |
| 619 | |
| 620 | /*! |
| 621 | Returns the minor OpenGL version. |
| 622 | */ |
| 623 | int QSurfaceFormat::minorVersion() const |
| 624 | { |
| 625 | return d->minor; |
| 626 | } |
| 627 | |
| 628 | /*! |
| 629 | Returns a QPair<int, int> representing the OpenGL version. |
| 630 | |
| 631 | Useful for version checks, for example format.version() >= qMakePair(3, 2) |
| 632 | */ |
| 633 | QPair<int, int> QSurfaceFormat::version() const |
| 634 | { |
| 635 | return qMakePair(value1&: d->major, value2&: d->minor); |
| 636 | } |
| 637 | |
| 638 | /*! |
| 639 | Sets the desired \a major and \a minor OpenGL versions. |
| 640 | |
| 641 | The default version is 2.0. |
| 642 | */ |
| 643 | void QSurfaceFormat::setVersion(int major, int minor) |
| 644 | { |
| 645 | if (d->minor != minor || d->major != major) { |
| 646 | detach(); |
| 647 | d->minor = minor; |
| 648 | d->major = major; |
| 649 | } |
| 650 | } |
| 651 | |
| 652 | /*! |
| 653 | Sets the preferred swap interval. The swap interval specifies the |
| 654 | minimum number of video frames that are displayed before a buffer |
| 655 | swap occurs. This can be used to sync the GL drawing into a window |
| 656 | to the vertical refresh of the screen. |
| 657 | |
| 658 | Setting an \a interval value of 0 will turn the vertical refresh |
| 659 | syncing off, any value higher than 0 will turn the vertical |
| 660 | syncing on. Setting \a interval to a higher value, for example 10, |
| 661 | results in having 10 vertical retraces between every buffer swap. |
| 662 | |
| 663 | The default interval is 1. |
| 664 | |
| 665 | Changing the swap interval may not be supported by the underlying |
| 666 | platform. In this case, the request will be silently ignored. |
| 667 | |
| 668 | \since 5.3 |
| 669 | |
| 670 | \sa swapInterval() |
| 671 | */ |
| 672 | void QSurfaceFormat::setSwapInterval(int interval) |
| 673 | { |
| 674 | if (d->swapInterval != interval) { |
| 675 | detach(); |
| 676 | d->swapInterval = interval; |
| 677 | } |
| 678 | } |
| 679 | |
| 680 | /*! |
| 681 | Returns the swap interval. |
| 682 | |
| 683 | \since 5.3 |
| 684 | |
| 685 | \sa setSwapInterval() |
| 686 | */ |
| 687 | int QSurfaceFormat::swapInterval() const |
| 688 | { |
| 689 | return d->swapInterval; |
| 690 | } |
| 691 | |
| 692 | /*! |
| 693 | Sets the preferred \a colorSpace. |
| 694 | |
| 695 | For example, this allows requesting windows with default framebuffers that |
| 696 | are sRGB-capable on platforms that support it. |
| 697 | |
| 698 | \note When the requested color space is not supported by the platform, the |
| 699 | request is ignored. Query the QSurfaceFormat after window creation to verify |
| 700 | if the color space request could be honored or not. |
| 701 | |
| 702 | \note This setting controls if the default framebuffer of the window is |
| 703 | capable of updates and blending in a given color space. It does not change |
| 704 | applications' output by itself. The applications' rendering code will still |
| 705 | have to opt in via the appropriate OpenGL calls to enable updates and |
| 706 | blending to be performed in the given color space instead of using the |
| 707 | standard linear operations. |
| 708 | |
| 709 | \since 6.0 |
| 710 | |
| 711 | \sa colorSpace() |
| 712 | */ |
| 713 | void QSurfaceFormat::setColorSpace(const QColorSpace &colorSpace) |
| 714 | { |
| 715 | if (d->colorSpace != colorSpace) { |
| 716 | detach(); |
| 717 | d->colorSpace = colorSpace; |
| 718 | } |
| 719 | } |
| 720 | |
| 721 | #if QT_DEPRECATED_SINCE(6, 0) |
| 722 | /*! |
| 723 | \overload |
| 724 | \deprecated [6.0] Use setColorSpace(QColorSpace) instead. |
| 725 | |
| 726 | Sets the colorspace to one of the predefined values. |
| 727 | |
| 728 | \since 5.10 |
| 729 | |
| 730 | \sa colorSpace() |
| 731 | */ |
| 732 | void QSurfaceFormat::setColorSpace(ColorSpace colorSpace) |
| 733 | { |
| 734 | switch (colorSpace) { |
| 735 | case DefaultColorSpace: |
| 736 | setColorSpace(QColorSpace()); |
| 737 | break; |
| 738 | case sRGBColorSpace: |
| 739 | setColorSpace(QColorSpace::SRgb); |
| 740 | break; |
| 741 | } |
| 742 | } |
| 743 | #endif // QT_DEPRECATED_SINCE(6, 0) |
| 744 | |
| 745 | /*! |
| 746 | \return the color space. |
| 747 | |
| 748 | \since 5.10 |
| 749 | |
| 750 | \sa setColorSpace() |
| 751 | */ |
| 752 | const QColorSpace &QSurfaceFormat::colorSpace() const |
| 753 | { |
| 754 | return d->colorSpace; |
| 755 | } |
| 756 | |
| 757 | Q_GLOBAL_STATIC(QSurfaceFormat, qt_default_surface_format) |
| 758 | |
| 759 | /*! |
| 760 | Sets the global default surface \a format. |
| 761 | |
| 762 | This format is used by default in QOpenGLContext, QWindow, QOpenGLWidget and |
| 763 | similar classes. |
| 764 | |
| 765 | It can always be overridden on a per-instance basis by using the class in |
| 766 | question's own setFormat() function. However, it is often more convenient to |
| 767 | set the format for all windows once at the start of the application. It also |
| 768 | guarantees proper behavior in cases where shared contexts are required, |
| 769 | because setting the format via this function guarantees that all contexts |
| 770 | and surfaces, even the ones created internally by Qt, will use the same |
| 771 | format. |
| 772 | |
| 773 | \note When setting Qt::AA_ShareOpenGLContexts, it is strongly recommended to |
| 774 | place the call to this function before the construction of the |
| 775 | QGuiApplication or QApplication. Otherwise \a format will not be applied to |
| 776 | the global share context and therefore issues may arise with context sharing |
| 777 | afterwards. |
| 778 | |
| 779 | \since 5.4 |
| 780 | \sa defaultFormat() |
| 781 | */ |
| 782 | void QSurfaceFormat::setDefaultFormat(const QSurfaceFormat &format) |
| 783 | { |
| 784 | #ifndef QT_NO_OPENGL |
| 785 | if (qApp) { |
| 786 | QOpenGLContext *globalContext = QOpenGLContext::globalShareContext(); |
| 787 | if (globalContext && globalContext->isValid()) { |
| 788 | qWarning(msg: "Warning: Setting a new default format with a different version or profile " |
| 789 | "after the global shared context is created may cause issues with context " |
| 790 | "sharing." ); |
| 791 | } |
| 792 | } |
| 793 | #endif |
| 794 | *qt_default_surface_format() = format; |
| 795 | } |
| 796 | |
| 797 | /*! |
| 798 | Returns the global default surface format. |
| 799 | |
| 800 | When setDefaultFormat() is not called, this is a default-constructed QSurfaceFormat. |
| 801 | |
| 802 | \since 5.4 |
| 803 | \sa setDefaultFormat() |
| 804 | */ |
| 805 | QSurfaceFormat QSurfaceFormat::defaultFormat() |
| 806 | { |
| 807 | return *qt_default_surface_format(); |
| 808 | } |
| 809 | |
| 810 | /*! |
| 811 | \fn bool QSurfaceFormat::operator==(const QSurfaceFormat& lhs, const QSurfaceFormat& rhs) |
| 812 | |
| 813 | Returns \c true if all the options of the two QSurfaceFormat objects |
| 814 | \a lhs and \a rhs are equal. |
| 815 | */ |
| 816 | |
| 817 | /*! |
| 818 | \fn bool QSurfaceFormat::operator!=(const QSurfaceFormat& lhs, const QSurfaceFormat& rhs) |
| 819 | |
| 820 | Returns \c false if all the options of the two QSurfaceFormat objects |
| 821 | \a lhs and \a rhs are equal; otherwise returns \c true. |
| 822 | */ |
| 823 | |
| 824 | /*! |
| 825 | \internal |
| 826 | */ |
| 827 | bool QSurfaceFormat::equals(const QSurfaceFormat& other) const noexcept |
| 828 | { |
| 829 | return (d == other.d) || ((int) d->opts == (int) other.d->opts |
| 830 | && d->stencilSize == other.d->stencilSize |
| 831 | && d->redBufferSize == other.d->redBufferSize |
| 832 | && d->greenBufferSize == other.d->greenBufferSize |
| 833 | && d->blueBufferSize == other.d->blueBufferSize |
| 834 | && d->alphaBufferSize == other.d->alphaBufferSize |
| 835 | && d->depthSize == other.d->depthSize |
| 836 | && d->numSamples == other.d->numSamples |
| 837 | && d->swapBehavior == other.d->swapBehavior |
| 838 | && d->profile == other.d->profile |
| 839 | && d->major == other.d->major |
| 840 | && d->minor == other.d->minor |
| 841 | && d->swapInterval == other.d->swapInterval); |
| 842 | } |
| 843 | |
| 844 | #ifndef QT_NO_DEBUG_STREAM |
| 845 | QDebug operator<<(QDebug dbg, const QSurfaceFormat &f) |
| 846 | { |
| 847 | const QSurfaceFormatPrivate * const d = f.d; |
| 848 | QDebugStateSaver saver(dbg); |
| 849 | |
| 850 | dbg.nospace() << "QSurfaceFormat(" |
| 851 | << "version " << d->major << '.' << d->minor |
| 852 | << ", options " << d->opts |
| 853 | << ", depthBufferSize " << d->depthSize |
| 854 | << ", redBufferSize " << d->redBufferSize |
| 855 | << ", greenBufferSize " << d->greenBufferSize |
| 856 | << ", blueBufferSize " << d->blueBufferSize |
| 857 | << ", alphaBufferSize " << d->alphaBufferSize |
| 858 | << ", stencilBufferSize " << d->stencilSize |
| 859 | << ", samples " << d->numSamples |
| 860 | << ", swapBehavior " << d->swapBehavior |
| 861 | << ", swapInterval " << d->swapInterval |
| 862 | << ", colorSpace " << d->colorSpace |
| 863 | << ", profile " << d->profile |
| 864 | << ')'; |
| 865 | |
| 866 | return dbg; |
| 867 | } |
| 868 | #endif |
| 869 | |
| 870 | QT_END_NAMESPACE |
| 871 | |
| 872 | #include "moc_qsurfaceformat.cpp" |
| 873 | |