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
QOpenGLTextureBlitter Class Reference

The QOpenGLTextureBlitter class provides a convenient way to draw textured quads via OpenGL. More...

#include <qopengltextureblitter.h>

+ Collaboration diagram for QOpenGLTextureBlitter:

Public Types

enum  Origin { OriginBottomLeft , OriginTopLeft }
 \value OriginBottomLeft Indicates that the data in the texture follows the OpenGL convention of coordinate systems, meaning Y is running from bottom to top. More...
 

Public Member Functions

 QOpenGLTextureBlitter ()
 Constructs a new QOpenGLTextureBlitter instance.
 
 ~QOpenGLTextureBlitter ()
 Destructs the instance.
 
bool create ()
 Initializes the graphics resources used by the blitter.
 
bool isCreated () const
 
void destroy ()
 Frees all graphics resources held by the blitter.
 
bool supportsExternalOESTarget () const
 
bool supportsRectangleTarget () const
 
void bind (GLenum target=GL_TEXTURE_2D)
 Binds the graphics resources used by the blitter.
 
void release ()
 Unbinds the graphics resources used by the blitter.
 
void setRedBlueSwizzle (bool swizzle)
 Sets whether swizzling is enabled for the red and blue color channels to swizzle.
 
void setOpacity (float opacity)
 Changes the opacity to opacity.
 
void blit (GLuint texture, const QMatrix4x4 &targetTransform, Origin sourceOrigin)
 Performs the blit with the source texture texture.
 
void blit (GLuint texture, const QMatrix4x4 &targetTransform, const QMatrix3x3 &sourceTransform)
 Performs the blit with the source texture texture.
 

Static Public Member Functions

static QMatrix4x4 targetTransform (const QRectF &target, const QRect &viewport)
 Calculates a target transform suitable for blit().
 
static QMatrix3x3 sourceTransform (const QRectF &subTexture, const QSize &textureSize, Origin origin)
 Calculates a 3x3 matrix suitable as the input to blit().
 

Detailed Description

The QOpenGLTextureBlitter class provides a convenient way to draw textured quads via OpenGL.

Since
5.8

\inmodule QtOpenGL

Drawing textured quads, in order to get the contents of a texture onto the screen, is a common operation when developing 2D user interfaces. QOpenGLTextureBlitter provides a convenience class to avoid repeating vertex data, shader sources, buffer and program management and matrix calculations.

For example, a QOpenGLWidget subclass can do the following to draw the contents rendered into a framebuffer at the pixel position {(x, y)}:

void OpenGLWidget::initializeGL()
{
m_blitter.create();
}
void OpenGLWidget::paintGL()
{
m_fbo->bind();
// update offscreen content
m_fbo->release();
m_blitter.bind();
const QRect targetRect(QPoint(x, y), m_fbo->size());
const QMatrix4x4 target = QOpenGLTextureBlitter::targetTransform(targetRect, QRect(QPoint(0, 0), m_fbo->size()));
m_blitter.blit(m_fbo->texture(), target, QOpenGLTextureBlitter::OriginBottomLeft);
m_blitter.release();
}
The QMatrix4x4 class represents a 4x4 transformation matrix in 3D space.
Definition qmatrix4x4.h:25
The QOpenGLFramebufferObject class encapsulates an OpenGL framebuffer object.
static QMatrix4x4 targetTransform(const QRectF &target, const QRect &viewport)
Calculates a target transform suitable for blit().
\inmodule QtCore\reentrant
Definition qpoint.h:23
\inmodule QtCore\reentrant
Definition qrect.h:30
GLint GLint GLint GLint GLint x
[0]
GLenum GLuint GLintptr GLsizeiptr size
[1]
GLenum target
GLint y

The blitter implements GLSL shaders both for GLSL 1.00 (suitable for OpenGL (ES) 2.x and compatibility profiles of newer OpenGL versions) and version 150 (suitable for core profile contexts with OpenGL 3.2 and newer).

Definition at line 17 of file qopengltextureblitter.h.

Member Enumeration Documentation

◆ Origin

\value OriginBottomLeft Indicates that the data in the texture follows the OpenGL convention of coordinate systems, meaning Y is running from bottom to top.

\value OriginTopLeft Indicates that the data in the texture has Y running from top to bottom, which is typical with regular, unflipped image data.

See also
blit()
Enumerator
OriginBottomLeft 
OriginTopLeft 

Definition at line 23 of file qopengltextureblitter.h.

Constructor & Destructor Documentation

◆ QOpenGLTextureBlitter()

QOpenGLTextureBlitter::QOpenGLTextureBlitter ( )

Constructs a new QOpenGLTextureBlitter instance.

Note
no graphics resources are initialized in the constructor. This makes it safe to place plain QOpenGLTextureBlitter members into classes because the actual initialization that depends on the OpenGL context happens only in create().

Definition at line 432 of file qopengltextureblitter.cpp.

◆ ~QOpenGLTextureBlitter()

QOpenGLTextureBlitter::~QOpenGLTextureBlitter ( )

Destructs the instance.

Note
When the OpenGL context - or a context sharing resources with it - that was current when calling create() is not current, graphics resources will not be released. Therefore, it is recommended to call destroy() manually instead of relying on the destructor to perform OpenGL resource cleanup.

Definition at line 446 of file qopengltextureblitter.cpp.

References destroy().

+ Here is the call graph for this function:

Member Function Documentation

◆ bind()

void QOpenGLTextureBlitter::bind ( GLenum target = GL_TEXTURE_2D)

Binds the graphics resources used by the blitter.

This must be called before calling blit(). Code modifying the OpenGL state should be avoided between the call to bind() and blit() because otherwise conflicts may arise.

target is the texture target for the source texture and must be either GL_TEXTURE_2D, GL_TEXTURE_RECTANGLE, or GL_OES_EGL_image_external.

See also
release(), blit()

Definition at line 581 of file qopengltextureblitter.cpp.

References QOpenGLShaderProgram::bind(), d, GL_FLOAT, QOpenGLTextureBlitterPrivate::Program::glProgram, and targetToProgramIndex().

Referenced by QOpenGLWindowPrivate::endPaint(), and QWaylandQuickCompositor::grabSurface().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ blit() [1/2]

void QOpenGLTextureBlitter::blit ( GLuint texture,
const QMatrix4x4 & targetTransform,
const QMatrix3x3 & sourceTransform )

Performs the blit with the source texture texture.

targetTransform specifies the transformation applied. This is usually generated by the targetTransform() helper function.

sourceTransform specifies the transformation applied to the source. This allows using only a sub-rect of the source texture. This is usually generated by the sourceTransform() helper function.

See also
sourceTransform(), targetTransform(), Origin, bind()

Definition at line 702 of file qopengltextureblitter.cpp.

References d, sourceTransform(), and targetTransform().

+ Here is the call graph for this function:

◆ blit() [2/2]

void QOpenGLTextureBlitter::blit ( GLuint texture,
const QMatrix4x4 & targetTransform,
Origin sourceOrigin )

Performs the blit with the source texture texture.

targetTransform specifies the transformation applied. This is usually generated by the targetTransform() helper function.

sourceOrigin specifies if the image data needs flipping. When texture corresponds to a texture attached to an FBO pass OriginBottomLeft. On the other hand, when texture is based on unflipped image data, pass OriginTopLeft. This is more efficient than using QImage::mirrored().

See also
targetTransform(), Origin, bind()

Definition at line 681 of file qopengltextureblitter.cpp.

References d, and targetTransform().

Referenced by clippedBlit(), QOpenGLWindowPrivate::endPaint(), and QWaylandQuickCompositor::grabSurface().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ create()

bool QOpenGLTextureBlitter::create ( )

Initializes the graphics resources used by the blitter.

Returns
true if successful, false if there was a failure. Failures can occur when there is no OpenGL context current on the current thread, or when shader compilation fails for some reason.
See also
isCreated(), destroy()

Definition at line 461 of file qopengltextureblitter.cpp.

References QSurfaceFormat::CoreProfile, QOpenGLContext::currentContext(), d, QOpenGLContext::format(), fragment_shader, fragment_shader150, qMakePair(), QOpenGLTextureBlitterPrivate::TEXTURE_2D, texture_buffer_data, vertex_buffer_data, vertex_shader, and vertex_shader150.

Referenced by QOpenGLWindowPrivate::endPaint(), and QWaylandQuickCompositor::grabSurface().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ destroy()

void QOpenGLTextureBlitter::destroy ( )

Frees all graphics resources held by the blitter.

Assumes that the OpenGL context, or another context sharing resources with it, that was current on the thread when invoking create() is current.

The function has no effect when the blitter is not in created state.

See also
create()

Definition at line 519 of file qopengltextureblitter.cpp.

References d, isCreated(), QOpenGLTextureBlitterPrivate::TEXTURE_2D, QOpenGLTextureBlitterPrivate::TEXTURE_EXTERNAL_OES, and QOpenGLTextureBlitterPrivate::TEXTURE_RECTANGLE.

Referenced by ~QOpenGLTextureBlitter(), and QOpenGLWindowPrivate::~QOpenGLWindowPrivate().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ isCreated()

bool QOpenGLTextureBlitter::isCreated ( ) const
Returns
true if create() was called and succeeded. false otherwise.
See also
create(), destroy()

Definition at line 504 of file qopengltextureblitter.cpp.

References d, and QOpenGLTextureBlitterPrivate::TEXTURE_2D.

Referenced by destroy(), and QOpenGLWindowPrivate::endPaint().

+ Here is the caller graph for this function:

◆ release()

void QOpenGLTextureBlitter::release ( )

Unbinds the graphics resources used by the blitter.

See also
bind()

Definition at line 612 of file qopengltextureblitter.cpp.

References d, QOpenGLTextureBlitterPrivate::Program::glProgram, QOpenGLShaderProgram::release(), and targetToProgramIndex().

Referenced by QOpenGLWindowPrivate::endPaint(), and QWaylandQuickCompositor::grabSurface().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ setOpacity()

void QOpenGLTextureBlitter::setOpacity ( float opacity)

Changes the opacity to opacity.

The default opacity is 1.0.

Note
the blitter does not alter the blend state. It is up to the caller of blit() to ensure the correct blend settings are active.

Definition at line 647 of file qopengltextureblitter.cpp.

References d.

◆ setRedBlueSwizzle()

void QOpenGLTextureBlitter::setRedBlueSwizzle ( bool swizzle)

Sets whether swizzling is enabled for the red and blue color channels to swizzle.

An BGRA to RGBA conversion (occurring in the shader on the GPU, instead of a slow CPU-side transformation) can be useful when the source texture contains data from a QImage with a format like QImage::Format_ARGB32 which maps to BGRA on little endian systems.

By default the red-blue swizzle is disabled since this is what a texture attached to an framebuffer object or a texture based on a byte ordered QImage format (like QImage::Format_RGBA8888) needs.

Definition at line 634 of file qopengltextureblitter.cpp.

References d.

◆ sourceTransform()

QMatrix3x3 QOpenGLTextureBlitter::sourceTransform ( const QRectF & subTexture,
const QSize & textureSize,
Origin origin )
static

Calculates a 3x3 matrix suitable as the input to blit().

This is used when only a part of the texture is to be used in the blit.

subTexture is the desired source rectangle in pixels, textureSize is the full width and height of the texture data. origin specifies the orientation of the image data when it comes to the Y axis.

See also
blit(), Origin

Definition at line 753 of file qopengltextureblitter.cpp.

References QSize::height(), OriginTopLeft, QSize::width(), QPointF::x(), and QPointF::y().

Referenced by blit(), and clippedBlit().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ supportsExternalOESTarget()

bool QOpenGLTextureBlitter::supportsExternalOESTarget ( ) const
Returns
true when bind() accepts GL_TEXTURE_EXTERNAL_OES as its target argument.
See also
bind(), blit()

Definition at line 538 of file qopengltextureblitter.cpp.

References QOpenGLContext::currentContext().

+ Here is the call graph for this function:

◆ supportsRectangleTarget()

bool QOpenGLTextureBlitter::supportsRectangleTarget ( ) const
Returns
true when bind() accepts GL_TEXTURE_RECTANGLE as its target argument.
See also
bind(), blit()

Definition at line 550 of file qopengltextureblitter.cpp.

References QOpenGLContext::currentContext(), and qMakePair().

+ Here is the call graph for this function:

◆ targetTransform()

QMatrix4x4 QOpenGLTextureBlitter::targetTransform ( const QRectF & target,
const QRect & viewport )
static

Calculates a target transform suitable for blit().

target is the target rectangle in pixels. viewport describes the source dimensions and will in most cases be set to (0, 0, image width, image height).

For unscaled output the size of target and viewport should match.

See also
blit()

Definition at line 722 of file qopengltextureblitter.cpp.

References viewport().

Referenced by blit(), blit(), clippedBlit(), and QOpenGLWindowPrivate::endPaint().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

The documentation for this class was generated from the following files: