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#ifndef QOPENGLFRAMEBUFFEROBJECT_P_H
5#define QOPENGLFRAMEBUFFEROBJECT_P_H
6
7//
8// W A R N I N G
9// -------------
10//
11// This file is not part of the Qt API. It exists purely as an
12// implementation detail. This header file may change from version to
13// version without notice, or even be removed.
14//
15// We mean it.
16//
17
18#include <QtCore/qvarlengtharray.h>
19#include <qopenglframebufferobject.h>
20#include <private/qopenglcontext_p.h>
21#include <private/qopenglextensions_p.h>
22
23QT_BEGIN_NAMESPACE
24
25class QOpenGLFramebufferObjectFormatPrivate
26{
27public:
28 QOpenGLFramebufferObjectFormatPrivate()
29 : ref(1),
30 samples(0),
31 attachment(QOpenGLFramebufferObject::NoAttachment),
32 target(GL_TEXTURE_2D),
33 mipmap(false)
34 {
35#if !QT_CONFIG(opengles2)
36 // There is nothing that says QOpenGLFramebufferObjectFormat needs a current
37 // context, so we need a fallback just to be safe, even though in practice there
38 // will usually be a current context.
39 QOpenGLContext *ctx = QOpenGLContext::currentContext();
40 const bool isES = ctx ? ctx->isOpenGLES() : QOpenGLContext::openGLModuleType() != QOpenGLContext::LibGL;
41 internal_format = isES ? GL_RGBA : GL_RGBA8;
42#else
43 internal_format = GL_RGBA;
44#endif
45 }
46 QOpenGLFramebufferObjectFormatPrivate
47 (const QOpenGLFramebufferObjectFormatPrivate *other)
48 : ref(1),
49 samples(other->samples),
50 attachment(other->attachment),
51 target(other->target),
52 internal_format(other->internal_format),
53 mipmap(other->mipmap)
54 {
55 }
56 bool equals(const QOpenGLFramebufferObjectFormatPrivate *other)
57 {
58 return samples == other->samples &&
59 attachment == other->attachment &&
60 target == other->target &&
61 internal_format == other->internal_format &&
62 mipmap == other->mipmap;
63 }
64
65 QAtomicInt ref;
66 int samples;
67 QOpenGLFramebufferObject::Attachment attachment;
68 GLenum target;
69 GLenum internal_format;
70 uint mipmap : 1;
71};
72
73class QOpenGLFramebufferObjectPrivate
74{
75public:
76 QOpenGLFramebufferObjectPrivate() : fbo_guard(nullptr), depth_buffer_guard(nullptr)
77 , stencil_buffer_guard(nullptr)
78 , valid(false) {}
79 ~QOpenGLFramebufferObjectPrivate() {}
80
81 void init(QOpenGLFramebufferObject *q, const QSize &size,
82 QOpenGLFramebufferObject::Attachment attachment,
83 GLenum texture_target, GLenum internal_format,
84 GLint samples = 0, bool mipmap = false);
85 void initTexture(int idx);
86 void initColorBuffer(int idx, GLint *samples);
87 void initDepthStencilAttachments(QOpenGLContext *ctx, QOpenGLFramebufferObject::Attachment attachment);
88
89 bool checkFramebufferStatus(QOpenGLContext *ctx) const;
90 QOpenGLSharedResourceGuard *fbo_guard;
91 QOpenGLSharedResourceGuard *depth_buffer_guard;
92 QOpenGLSharedResourceGuard *stencil_buffer_guard;
93 GLenum target;
94 QSize dsSize;
95 QOpenGLFramebufferObjectFormat format;
96 int requestedSamples;
97 uint valid : 1;
98 QOpenGLFramebufferObject::Attachment fbo_attachment;
99 QOpenGLExtensions funcs;
100
101 struct ColorAttachment {
102 ColorAttachment() : internalFormat(0), guard(nullptr) { }
103 ColorAttachment(const QSize &size, GLenum internalFormat)
104 : size(size), internalFormat(internalFormat), guard(nullptr) { }
105 QSize size;
106 GLenum internalFormat;
107 QOpenGLSharedResourceGuard *guard;
108 };
109 QVarLengthArray<ColorAttachment, 8> colorAttachments;
110
111 inline GLuint fbo() const { return fbo_guard ? fbo_guard->id() : 0; }
112};
113
114Q_OPENGL_EXPORT QImage qt_gl_read_framebuffer(const QSize &size, bool alpha_format, bool include_alpha);
115
116QT_END_NAMESPACE
117
118#endif // QOPENGLFRAMEBUFFEROBJECT_P_H
119

source code of qtbase/src/opengl/qopenglframebufferobject_p.h