| 1 |
// Copyright (C) 2016 The Qt Company Ltd. |
| 2 |
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
| 3 |
|
| 4 |
#include "qopengltextureblitwindow.h" |
| 5 |
|
| 6 |
#include <QtGui/QPainter> |
| 7 |
#include <QtOpenGL/QOpenGLTexture> |
| 8 |
#include <QtGui/QOpenGLFunctions> |
| 9 |
#include <QtGui/QMatrix4x4> |
| 10 |
|
| 11 |
#include <QtCore/QCoreApplication> |
| 12 |
#include <QtCore/QDebug> |
| 13 |
|
| 14 |
QOpenGLTextureBlitWindow::QOpenGLTextureBlitWindow() |
| 15 |
: QWindow() |
| 16 |
, m_context(new QOpenGLContext(this)) |
| 17 |
{ |
| 18 |
resize(500,500); |
| 19 |
setSurfaceType(OpenGLSurface); |
| 20 |
QSurfaceFormat surfaceFormat = format(); |
| 21 |
if (QCoreApplication::arguments().contains(QStringLiteral("-coreprofile"))) { |
| 22 |
surfaceFormat.setVersion(3,2); |
| 23 |
surfaceFormat.setProfile(QSurfaceFormat::CoreProfile); |
| 24 |
} |
| 25 |
|
| 26 |
setFormat(surfaceFormat); |
| 27 |
create(); |
| 28 |
m_context->setFormat(surfaceFormat); |
| 29 |
m_context->create(); |
| 30 |
|
| 31 |
m_context->makeCurrent(this); |
| 32 |
|
| 33 |
m_blitter.create(); |
| 34 |
qDebug("GL_TEXTURE_EXTERNAL_OES support: %d", m_blitter.supportsExternalOESTarget()); |
| 35 |
} |
| 36 |
|
| 37 |
void QOpenGLTextureBlitWindow::render() |
| 38 |
{ |
| 39 |
m_context->makeCurrent(this); |
| 40 |
|
| 41 |
QRect viewport(0,0,dWidth(),dHeight()); |
| 42 |
QOpenGLFunctions *functions = m_context->functions(); |
| 43 |
functions->glViewport(0,0,dWidth(), dHeight()); |
| 44 |
|
| 45 |
functions->glClearColor(0.f, .6f, .0f, 0.f); |
| 46 |
functions->glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
| 47 |
|
| 48 |
QOpenGLTexture texture(m_image); |
| 49 |
texture.setWrapMode(QOpenGLTexture::ClampToEdge); |
| 50 |
texture.create(); |
| 51 |
|
| 52 |
QOpenGLTexture texture_mirrored(m_image_mirrord); |
| 53 |
texture_mirrored.setWrapMode(QOpenGLTexture::ClampToEdge); |
| 54 |
texture_mirrored.create(); |
| 55 |
|
| 56 |
QRectF topLeftOriginTopLeft(QPointF(0,0), QPointF(dWidth()/2.0, dHeight()/2.0)); |
| 57 |
QRectF topRightOriginTopLeft(QPointF(dWidth()/2.0,0), QPointF(dWidth(), dHeight()/2.0)); |
| 58 |
QRectF bottomLeftOriginTopLeft(QPointF(0, dHeight()/2.0), QPointF(dWidth() /2.0, dHeight())); |
| 59 |
QRectF bottomRightOriginTopLeft(QPoint(dWidth()/2.0, dHeight()/2.0), QPointF(dWidth(), dHeight())); |
| 60 |
|
| 61 |
QRectF topLeftOriginBottomLeft = bottomLeftOriginTopLeft; Q_UNUSED(topLeftOriginBottomLeft); |
| 62 |
QRectF topRightOriginBottomLeft = bottomRightOriginTopLeft; Q_UNUSED(topRightOriginBottomLeft); |
| 63 |
QRectF bottomLeftOriginBottomLeft = topLeftOriginTopLeft; |
| 64 |
QRectF bottomRightOriginBottomLeft = topRightOriginTopLeft; |
| 65 |
|
| 66 |
QOpenGLTextureBlitter::Origin topLeftOrigin = QOpenGLTextureBlitter::OriginTopLeft; |
| 67 |
QOpenGLTextureBlitter::Origin bottomLeftOrigin = QOpenGLTextureBlitter::OriginBottomLeft; |
| 68 |
|
| 69 |
QMatrix4x4 topRightOriginTopLeftVertex = QOpenGLTextureBlitter::targetTransform(topRightOriginTopLeft, viewport); |
| 70 |
QMatrix4x4 bottomLeftOriginTopLeftVertex = QOpenGLTextureBlitter::targetTransform(bottomLeftOriginTopLeft, viewport); |
| 71 |
QMatrix4x4 bottomRightOriginTopLeftVertex = QOpenGLTextureBlitter::targetTransform(bottomRightOriginTopLeft, viewport); |
| 72 |
|
| 73 |
QMatrix3x3 texTopLeftOriginTopLeft = QOpenGLTextureBlitter::sourceTransform(topLeftOriginTopLeft, m_image.size(), topLeftOrigin); |
| 74 |
QMatrix3x3 texTopRightOriginBottomLeft = QOpenGLTextureBlitter::sourceTransform(topRightOriginBottomLeft, m_image.size(), bottomLeftOrigin); |
| 75 |
QMatrix3x3 texBottomLeftOriginBottomLeft = QOpenGLTextureBlitter::sourceTransform(bottomLeftOriginBottomLeft, m_image.size(), bottomLeftOrigin); |
| 76 |
QMatrix3x3 texBottomRightOriginBottomLeft = QOpenGLTextureBlitter::sourceTransform(bottomRightOriginBottomLeft, m_image.size(), bottomLeftOrigin); |
| 77 |
|
| 78 |
QSizeF subSize(topLeftOriginTopLeft.width()/2, topLeftOriginTopLeft.height()/2); |
| 79 |
QRectF subTopLeftOriginTopLeft(topLeftOriginTopLeft.topLeft(), subSize); |
| 80 |
QRectF subTopRightOriginTopLeft(QPointF(topLeftOriginTopLeft.topLeft().x() + topLeftOriginTopLeft.width() / 2, |
| 81 |
topLeftOriginTopLeft.topLeft().y()), subSize); |
| 82 |
QRectF subBottomLeftOriginTopLeft(QPointF(topLeftOriginTopLeft.topLeft().x(), |
| 83 |
topLeftOriginTopLeft.topLeft().y() + topLeftOriginTopLeft.height() / 2), subSize); |
| 84 |
QRectF subBottomRightOriginTopLeft(QPointF(topLeftOriginTopLeft.topLeft().x() + topLeftOriginTopLeft.width() / 2, |
| 85 |
topLeftOriginTopLeft.topLeft().y() + topLeftOriginTopLeft.height() / 2), subSize); |
| 86 |
|
| 87 |
QMatrix4x4 subTopLeftOriginTopLeftVertex = QOpenGLTextureBlitter::targetTransform(subTopLeftOriginTopLeft, viewport); |
| 88 |
QMatrix4x4 subTopRightOriginTopLeftVertex = QOpenGLTextureBlitter::targetTransform(subTopRightOriginTopLeft, viewport); |
| 89 |
QMatrix4x4 subBottomLeftOriginTopLeftVertex = QOpenGLTextureBlitter::targetTransform(subBottomLeftOriginTopLeft, viewport); |
| 90 |
QMatrix4x4 subBottomRightOriginTopLeftVertex = QOpenGLTextureBlitter::targetTransform(subBottomRightOriginTopLeft, viewport); |
| 91 |
|
| 92 |
m_blitter.bind(); |
| 93 |
m_blitter.blit(texture_mirrored.textureId(), subTopLeftOriginTopLeftVertex, texBottomRightOriginBottomLeft); |
| 94 |
m_blitter.blit(texture_mirrored.textureId(), subTopRightOriginTopLeftVertex, texBottomLeftOriginBottomLeft); |
| 95 |
m_blitter.blit(texture.textureId(), subBottomLeftOriginTopLeftVertex, texTopRightOriginBottomLeft); |
| 96 |
m_blitter.blit(texture.textureId(), subBottomRightOriginTopLeftVertex, texTopLeftOriginTopLeft); |
| 97 |
|
| 98 |
m_blitter.blit(texture.textureId(), topRightOriginTopLeftVertex, topLeftOrigin); |
| 99 |
m_blitter.blit(texture_mirrored.textureId(), bottomLeftOriginTopLeftVertex, topLeftOrigin); |
| 100 |
|
| 101 |
m_blitter.setRedBlueSwizzle(true); |
| 102 |
m_blitter.blit(texture.textureId(), bottomRightOriginTopLeftVertex, texTopLeftOriginTopLeft); |
| 103 |
m_blitter.setRedBlueSwizzle(false); |
| 104 |
m_blitter.release(); |
| 105 |
|
| 106 |
if (m_blitter.supportsExternalOESTarget()) { |
| 107 |
// Cannot do much testing here, just verify that bind and release work, meaning that the program is present. |
| 108 |
m_blitter.bind(0x8D65); |
| 109 |
m_blitter.release(); |
| 110 |
} |
| 111 |
|
| 112 |
m_context->swapBuffers(this); |
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
void QOpenGLTextureBlitWindow::exposeEvent(QExposeEvent *event) |
| 117 |
{ |
| 118 |
Q_UNUSED(event); |
| 119 |
render(); |
| 120 |
} |
| 121 |
|
| 122 |
void QOpenGLTextureBlitWindow::resizeEvent(QResizeEvent *event) |
| 123 |
{ |
| 124 |
Q_UNUSED(event); |
| 125 |
m_image = QImage(size() * devicePixelRatio(), QImage::Format_ARGB32_Premultiplied); |
| 126 |
|
| 127 |
m_image.fill(Qt::gray); |
| 128 |
|
| 129 |
QPainter p(&m_image); |
| 130 |
|
| 131 |
QPen pen(Qt::red); |
| 132 |
pen.setWidth(5); |
| 133 |
p.setPen(pen); |
| 134 |
|
| 135 |
QFont font = p.font(); |
| 136 |
font.setPixelSize(qMin(m_image.height(), m_image.width()) / 20); |
| 137 |
p.setFont(font); |
| 138 |
|
| 139 |
int dx = dWidth() / 5; |
| 140 |
int dy = dHeight() / 5; |
| 141 |
for (int y = 0; y < 5; y++) { |
| 142 |
for (int x = 0; x < 5; x++) { |
| 143 |
QRect textRect(x * dx, y*dy, dx,dy); |
| 144 |
QString text = QString("[%1,%2]").arg(x).arg(y); |
| 145 |
p.drawText(textRect,text); |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
p.drawRect(QRectF(2.5,2.5,dWidth() - 5, dHeight() - 5)); |
| 150 |
|
| 151 |
m_image_mirrord = m_image.flipped(); |
| 152 |
} |
| 153 |
|