| Absolute File Name: | /home/qt/qt5_coco/qt5/qtbase/src/gui/opengl/qopengltextureblitter.cpp |
| Source code | Switch to Preprocessed file |
| Line | Source | Count | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 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 "qopengltextureblitter_p.h" | - | ||||||||||||
| 41 | - | |||||||||||||
| 42 | #include <QtGui/QOpenGLBuffer> | - | ||||||||||||
| 43 | #include <QtGui/QOpenGLShaderProgram> | - | ||||||||||||
| 44 | #include <QtGui/QOpenGLVertexArrayObject> | - | ||||||||||||
| 45 | #include <QtGui/QOpenGLContext> | - | ||||||||||||
| 46 | #include <QtGui/QOpenGLFunctions> | - | ||||||||||||
| 47 | - | |||||||||||||
| 48 | #ifndef GL_TEXTURE_EXTERNAL_OES | - | ||||||||||||
| 49 | #define GL_TEXTURE_EXTERNAL_OES 0x8D65 | - | ||||||||||||
| 50 | #endif | - | ||||||||||||
| 51 | - | |||||||||||||
| 52 | QT_BEGIN_NAMESPACE | - | ||||||||||||
| 53 | - | |||||||||||||
| 54 | static const char vertex_shader150[] = | - | ||||||||||||
| 55 | "#version 150 core\n" | - | ||||||||||||
| 56 | "in vec3 vertexCoord;" | - | ||||||||||||
| 57 | "in vec2 textureCoord;" | - | ||||||||||||
| 58 | "out vec2 uv;" | - | ||||||||||||
| 59 | "uniform mat4 vertexTransform;" | - | ||||||||||||
| 60 | "uniform mat3 textureTransform;" | - | ||||||||||||
| 61 | "void main() {" | - | ||||||||||||
| 62 | " uv = (textureTransform * vec3(textureCoord,1.0)).xy;" | - | ||||||||||||
| 63 | " gl_Position = vertexTransform * vec4(vertexCoord,1.0);" | - | ||||||||||||
| 64 | "}"; | - | ||||||||||||
| 65 | - | |||||||||||||
| 66 | static const char fragment_shader150[] = | - | ||||||||||||
| 67 | "#version 150 core\n" | - | ||||||||||||
| 68 | "in vec2 uv;" | - | ||||||||||||
| 69 | "out vec4 fragcolor;" | - | ||||||||||||
| 70 | "uniform sampler2D textureSampler;" | - | ||||||||||||
| 71 | "uniform bool swizzle;" | - | ||||||||||||
| 72 | "uniform float opacity;" | - | ||||||||||||
| 73 | "void main() {" | - | ||||||||||||
| 74 | " vec4 tmpFragColor = texture(textureSampler, uv);" | - | ||||||||||||
| 75 | " tmpFragColor.a *= opacity;" | - | ||||||||||||
| 76 | " fragcolor = swizzle ? tmpFragColor.bgra : tmpFragColor;" | - | ||||||||||||
| 77 | "}"; | - | ||||||||||||
| 78 | - | |||||||||||||
| 79 | static const char vertex_shader[] = | - | ||||||||||||
| 80 | "attribute highp vec3 vertexCoord;" | - | ||||||||||||
| 81 | "attribute highp vec2 textureCoord;" | - | ||||||||||||
| 82 | "varying highp vec2 uv;" | - | ||||||||||||
| 83 | "uniform highp mat4 vertexTransform;" | - | ||||||||||||
| 84 | "uniform highp mat3 textureTransform;" | - | ||||||||||||
| 85 | "void main() {" | - | ||||||||||||
| 86 | " uv = (textureTransform * vec3(textureCoord,1.0)).xy;" | - | ||||||||||||
| 87 | " gl_Position = vertexTransform * vec4(vertexCoord,1.0);" | - | ||||||||||||
| 88 | "}"; | - | ||||||||||||
| 89 | - | |||||||||||||
| 90 | static const char fragment_shader[] = | - | ||||||||||||
| 91 | "varying highp vec2 uv;" | - | ||||||||||||
| 92 | "uniform sampler2D textureSampler;" | - | ||||||||||||
| 93 | "uniform bool swizzle;" | - | ||||||||||||
| 94 | "uniform highp float opacity;" | - | ||||||||||||
| 95 | "void main() {" | - | ||||||||||||
| 96 | " highp vec4 tmpFragColor = texture2D(textureSampler,uv);" | - | ||||||||||||
| 97 | " tmpFragColor.a *= opacity;" | - | ||||||||||||
| 98 | " gl_FragColor = swizzle ? tmpFragColor.bgra : tmpFragColor;" | - | ||||||||||||
| 99 | "}"; | - | ||||||||||||
| 100 | - | |||||||||||||
| 101 | static const char fragment_shader_external_oes[] = | - | ||||||||||||
| 102 | "#extension GL_OES_EGL_image_external : require\n" | - | ||||||||||||
| 103 | "varying highp vec2 uv;" | - | ||||||||||||
| 104 | "uniform samplerExternalOES textureSampler;\n" | - | ||||||||||||
| 105 | "uniform bool swizzle;" | - | ||||||||||||
| 106 | "uniform highp float opacity;" | - | ||||||||||||
| 107 | "void main() {" | - | ||||||||||||
| 108 | " highp vec4 tmpFragColor = texture2D(textureSampler, uv);" | - | ||||||||||||
| 109 | " tmpFragColor.a *= opacity;" | - | ||||||||||||
| 110 | " gl_FragColor = swizzle ? tmpFragColor.bgra : tmpFragColor;" | - | ||||||||||||
| 111 | "}"; | - | ||||||||||||
| 112 | - | |||||||||||||
| 113 | static const GLfloat vertex_buffer_data[] = { | - | ||||||||||||
| 114 | -1,-1, 0, | - | ||||||||||||
| 115 | -1, 1, 0, | - | ||||||||||||
| 116 | 1,-1, 0, | - | ||||||||||||
| 117 | -1, 1, 0, | - | ||||||||||||
| 118 | 1,-1, 0, | - | ||||||||||||
| 119 | 1, 1, 0 | - | ||||||||||||
| 120 | }; | - | ||||||||||||
| 121 | - | |||||||||||||
| 122 | static const GLfloat texture_buffer_data[] = { | - | ||||||||||||
| 123 | 0, 0, | - | ||||||||||||
| 124 | 0, 1, | - | ||||||||||||
| 125 | 1, 0, | - | ||||||||||||
| 126 | 0, 1, | - | ||||||||||||
| 127 | 1, 0, | - | ||||||||||||
| 128 | 1, 1 | - | ||||||||||||
| 129 | }; | - | ||||||||||||
| 130 | - | |||||||||||||
| 131 | class TextureBinder | - | ||||||||||||
| 132 | { | - | ||||||||||||
| 133 | public: | - | ||||||||||||
| 134 | TextureBinder(GLenum target, GLuint textureId) : m_target(target) | - | ||||||||||||
| 135 | { | - | ||||||||||||
| 136 | QOpenGLContext::currentContext()->functions()->glBindTexture(m_target, textureId); | - | ||||||||||||
| 137 | } never executed: end of block | 0 | ||||||||||||
| 138 | ~TextureBinder() | - | ||||||||||||
| 139 | { | - | ||||||||||||
| 140 | QOpenGLContext::currentContext()->functions()->glBindTexture(m_target, 0); | - | ||||||||||||
| 141 | } never executed: end of block | 0 | ||||||||||||
| 142 | - | |||||||||||||
| 143 | private: | - | ||||||||||||
| 144 | GLenum m_target; | - | ||||||||||||
| 145 | }; | - | ||||||||||||
| 146 | - | |||||||||||||
| 147 | class QOpenGLTextureBlitterPrivate | - | ||||||||||||
| 148 | { | - | ||||||||||||
| 149 | public: | - | ||||||||||||
| 150 | enum TextureMatrixUniform { | - | ||||||||||||
| 151 | User, | - | ||||||||||||
| 152 | Identity, | - | ||||||||||||
| 153 | IdentityFlipped | - | ||||||||||||
| 154 | }; | - | ||||||||||||
| 155 | - | |||||||||||||
| 156 | enum ProgramIndex { | - | ||||||||||||
| 157 | TEXTURE_2D, | - | ||||||||||||
| 158 | TEXTURE_EXTERNAL_OES | - | ||||||||||||
| 159 | }; | - | ||||||||||||
| 160 | - | |||||||||||||
| 161 | QOpenGLTextureBlitterPrivate() : | - | ||||||||||||
| 162 | swizzle(false), | - | ||||||||||||
| 163 | opacity(1.0f), | - | ||||||||||||
| 164 | vao(new QOpenGLVertexArrayObject), | - | ||||||||||||
| 165 | currentTarget(TEXTURE_2D) | - | ||||||||||||
| 166 | { } never executed: end of block | 0 | ||||||||||||
| 167 | - | |||||||||||||
| 168 | bool buildProgram(ProgramIndex idx, const char *vs, const char *fs); | - | ||||||||||||
| 169 | - | |||||||||||||
| 170 | void blit(GLuint texture, const QMatrix4x4 &vertexTransform, const QMatrix3x3 &textureTransform); | - | ||||||||||||
| 171 | void blit(GLuint texture, const QMatrix4x4 &vertexTransform, QOpenGLTextureBlitter::Origin origin); | - | ||||||||||||
| 172 | - | |||||||||||||
| 173 | void prepareProgram(const QMatrix4x4 &vertexTransform); | - | ||||||||||||
| 174 | - | |||||||||||||
| 175 | QOpenGLBuffer vertexBuffer; | - | ||||||||||||
| 176 | QOpenGLBuffer textureBuffer; | - | ||||||||||||
| 177 | struct Program { | - | ||||||||||||
| 178 | Program() : | - | ||||||||||||
| 179 | vertexCoordAttribPos(0), | - | ||||||||||||
| 180 | vertexTransformUniformPos(0), | - | ||||||||||||
| 181 | textureCoordAttribPos(0), | - | ||||||||||||
| 182 | textureTransformUniformPos(0), | - | ||||||||||||
| 183 | swizzleUniformPos(0), | - | ||||||||||||
| 184 | opacityUniformPos(0), | - | ||||||||||||
| 185 | swizzle(false), | - | ||||||||||||
| 186 | opacity(0.0f), | - | ||||||||||||
| 187 | textureMatrixUniformState(User) | - | ||||||||||||
| 188 | { } never executed: end of block | 0 | ||||||||||||
| 189 | QScopedPointer<QOpenGLShaderProgram> glProgram; | - | ||||||||||||
| 190 | GLuint vertexCoordAttribPos; | - | ||||||||||||
| 191 | GLuint vertexTransformUniformPos; | - | ||||||||||||
| 192 | GLuint textureCoordAttribPos; | - | ||||||||||||
| 193 | GLuint textureTransformUniformPos; | - | ||||||||||||
| 194 | GLuint swizzleUniformPos; | - | ||||||||||||
| 195 | GLuint opacityUniformPos; | - | ||||||||||||
| 196 | bool swizzle; | - | ||||||||||||
| 197 | float opacity; | - | ||||||||||||
| 198 | TextureMatrixUniform textureMatrixUniformState; | - | ||||||||||||
| 199 | } programs[2]; | - | ||||||||||||
| 200 | bool swizzle; | - | ||||||||||||
| 201 | float opacity; | - | ||||||||||||
| 202 | QScopedPointer<QOpenGLVertexArrayObject> vao; | - | ||||||||||||
| 203 | GLenum currentTarget; | - | ||||||||||||
| 204 | }; | - | ||||||||||||
| 205 | - | |||||||||||||
| 206 | static inline QOpenGLTextureBlitterPrivate::ProgramIndex targetToProgramIndex(GLenum target) | - | ||||||||||||
| 207 | { | - | ||||||||||||
| 208 | switch (target) { | - | ||||||||||||
| 209 | case GL_TEXTURE_2D: never executed: case 0x0DE1: | 0 | ||||||||||||
| 210 | return QOpenGLTextureBlitterPrivate::TEXTURE_2D; never executed: return QOpenGLTextureBlitterPrivate::TEXTURE_2D; | 0 | ||||||||||||
| 211 | case GL_TEXTURE_EXTERNAL_OES: never executed: case 0x8D65: | 0 | ||||||||||||
| 212 | return QOpenGLTextureBlitterPrivate::TEXTURE_EXTERNAL_OES; never executed: return QOpenGLTextureBlitterPrivate::TEXTURE_EXTERNAL_OES; | 0 | ||||||||||||
| 213 | default: never executed: default: | 0 | ||||||||||||
| 214 | qWarning("Unsupported texture target 0x%x", target); | - | ||||||||||||
| 215 | return QOpenGLTextureBlitterPrivate::TEXTURE_2D; never executed: return QOpenGLTextureBlitterPrivate::TEXTURE_2D; | 0 | ||||||||||||
| 216 | } | - | ||||||||||||
| 217 | } | - | ||||||||||||
| 218 | - | |||||||||||||
| 219 | void QOpenGLTextureBlitterPrivate::prepareProgram(const QMatrix4x4 &vertexTransform) | - | ||||||||||||
| 220 | { | - | ||||||||||||
| 221 | Program *program = &programs[targetToProgramIndex(currentTarget)]; | - | ||||||||||||
| 222 | - | |||||||||||||
| 223 | vertexBuffer.bind(); | - | ||||||||||||
| 224 | program->glProgram->setAttributeBuffer(program->vertexCoordAttribPos, GL_FLOAT, 0, 3, 0); | - | ||||||||||||
| 225 | program->glProgram->enableAttributeArray(program->vertexCoordAttribPos); | - | ||||||||||||
| 226 | vertexBuffer.release(); | - | ||||||||||||
| 227 | - | |||||||||||||
| 228 | program->glProgram->setUniformValue(program->vertexTransformUniformPos, vertexTransform); | - | ||||||||||||
| 229 | - | |||||||||||||
| 230 | textureBuffer.bind(); | - | ||||||||||||
| 231 | program->glProgram->setAttributeBuffer(program->textureCoordAttribPos, GL_FLOAT, 0, 2, 0); | - | ||||||||||||
| 232 | program->glProgram->enableAttributeArray(program->textureCoordAttribPos); | - | ||||||||||||
| 233 | textureBuffer.release(); | - | ||||||||||||
| 234 | - | |||||||||||||
| 235 | if (swizzle != program->swizzle) {
| 0 | ||||||||||||
| 236 | program->glProgram->setUniformValue(program->swizzleUniformPos, swizzle); | - | ||||||||||||
| 237 | program->swizzle = swizzle; | - | ||||||||||||
| 238 | } never executed: end of block | 0 | ||||||||||||
| 239 | - | |||||||||||||
| 240 | if (opacity != program->opacity) {
| 0 | ||||||||||||
| 241 | program->glProgram->setUniformValue(program->opacityUniformPos, opacity); | - | ||||||||||||
| 242 | program->opacity = opacity; | - | ||||||||||||
| 243 | } never executed: end of block | 0 | ||||||||||||
| 244 | } never executed: end of block | 0 | ||||||||||||
| 245 | - | |||||||||||||
| 246 | void QOpenGLTextureBlitterPrivate::blit(GLuint texture, | - | ||||||||||||
| 247 | const QMatrix4x4 &vertexTransform, | - | ||||||||||||
| 248 | const QMatrix3x3 &textureTransform) | - | ||||||||||||
| 249 | { | - | ||||||||||||
| 250 | TextureBinder binder(currentTarget, texture); | - | ||||||||||||
| 251 | prepareProgram(vertexTransform); | - | ||||||||||||
| 252 | - | |||||||||||||
| 253 | Program *program = &programs[targetToProgramIndex(currentTarget)]; | - | ||||||||||||
| 254 | program->glProgram->setUniformValue(program->textureTransformUniformPos, textureTransform); | - | ||||||||||||
| 255 | program->textureMatrixUniformState = User; | - | ||||||||||||
| 256 | - | |||||||||||||
| 257 | QOpenGLContext::currentContext()->functions()->glDrawArrays(GL_TRIANGLES, 0, 6); | - | ||||||||||||
| 258 | } never executed: end of block | 0 | ||||||||||||
| 259 | - | |||||||||||||
| 260 | void QOpenGLTextureBlitterPrivate::blit(GLuint texture, | - | ||||||||||||
| 261 | const QMatrix4x4 &vertexTransform, | - | ||||||||||||
| 262 | QOpenGLTextureBlitter::Origin origin) | - | ||||||||||||
| 263 | { | - | ||||||||||||
| 264 | TextureBinder binder(currentTarget, texture); | - | ||||||||||||
| 265 | prepareProgram(vertexTransform); | - | ||||||||||||
| 266 | - | |||||||||||||
| 267 | Program *program = &programs[targetToProgramIndex(currentTarget)]; | - | ||||||||||||
| 268 | if (origin == QOpenGLTextureBlitter::OriginTopLeft) {
| 0 | ||||||||||||
| 269 | if (program->textureMatrixUniformState != IdentityFlipped) {
| 0 | ||||||||||||
| 270 | QMatrix3x3 flipped; | - | ||||||||||||
| 271 | flipped(1,1) = -1; | - | ||||||||||||
| 272 | flipped(1,2) = 1; | - | ||||||||||||
| 273 | program->glProgram->setUniformValue(program->textureTransformUniformPos, flipped); | - | ||||||||||||
| 274 | program->textureMatrixUniformState = IdentityFlipped; | - | ||||||||||||
| 275 | } never executed: end of block | 0 | ||||||||||||
| 276 | } else if (program->textureMatrixUniformState != Identity) { never executed: end of block
| 0 | ||||||||||||
| 277 | program->glProgram->setUniformValue(program->textureTransformUniformPos, QMatrix3x3()); | - | ||||||||||||
| 278 | program->textureMatrixUniformState = Identity; | - | ||||||||||||
| 279 | } never executed: end of block | 0 | ||||||||||||
| 280 | - | |||||||||||||
| 281 | QOpenGLContext::currentContext()->functions()->glDrawArrays(GL_TRIANGLES, 0, 6); | - | ||||||||||||
| 282 | } never executed: end of block | 0 | ||||||||||||
| 283 | - | |||||||||||||
| 284 | bool QOpenGLTextureBlitterPrivate::buildProgram(ProgramIndex idx, const char *vs, const char *fs) | - | ||||||||||||
| 285 | { | - | ||||||||||||
| 286 | Program *p = &programs[idx]; | - | ||||||||||||
| 287 | - | |||||||||||||
| 288 | p->glProgram.reset(new QOpenGLShaderProgram); | - | ||||||||||||
| 289 | - | |||||||||||||
| 290 | p->glProgram->addShaderFromSourceCode(QOpenGLShader::Vertex, vs); | - | ||||||||||||
| 291 | p->glProgram->addShaderFromSourceCode(QOpenGLShader::Fragment, fs); | - | ||||||||||||
| 292 | p->glProgram->link(); | - | ||||||||||||
| 293 | if (!p->glProgram->isLinked()) {
| 0 | ||||||||||||
| 294 | qWarning() << "Could not link shader program:\n" << p->glProgram->log(); | - | ||||||||||||
| 295 | return false; never executed: return false; | 0 | ||||||||||||
| 296 | } | - | ||||||||||||
| 297 | - | |||||||||||||
| 298 | p->glProgram->bind(); | - | ||||||||||||
| 299 | - | |||||||||||||
| 300 | p->vertexCoordAttribPos = p->glProgram->attributeLocation("vertexCoord"); | - | ||||||||||||
| 301 | p->vertexTransformUniformPos = p->glProgram->uniformLocation("vertexTransform"); | - | ||||||||||||
| 302 | p->textureCoordAttribPos = p->glProgram->attributeLocation("textureCoord"); | - | ||||||||||||
| 303 | p->textureTransformUniformPos = p->glProgram->uniformLocation("textureTransform"); | - | ||||||||||||
| 304 | p->swizzleUniformPos = p->glProgram->uniformLocation("swizzle"); | - | ||||||||||||
| 305 | p->opacityUniformPos = p->glProgram->uniformLocation("opacity"); | - | ||||||||||||
| 306 | - | |||||||||||||
| 307 | p->glProgram->setUniformValue(p->swizzleUniformPos, false); | - | ||||||||||||
| 308 | - | |||||||||||||
| 309 | return true; never executed: return true; | 0 | ||||||||||||
| 310 | } | - | ||||||||||||
| 311 | - | |||||||||||||
| 312 | QOpenGLTextureBlitter::QOpenGLTextureBlitter() | - | ||||||||||||
| 313 | : d_ptr(new QOpenGLTextureBlitterPrivate) | - | ||||||||||||
| 314 | { | - | ||||||||||||
| 315 | } never executed: end of block | 0 | ||||||||||||
| 316 | - | |||||||||||||
| 317 | QOpenGLTextureBlitter::~QOpenGLTextureBlitter() | - | ||||||||||||
| 318 | { | - | ||||||||||||
| 319 | destroy(); | - | ||||||||||||
| 320 | } never executed: end of block | 0 | ||||||||||||
| 321 | - | |||||||||||||
| 322 | bool QOpenGLTextureBlitter::create() | - | ||||||||||||
| 323 | { | - | ||||||||||||
| 324 | QOpenGLContext *currentContext = QOpenGLContext::currentContext(); | - | ||||||||||||
| 325 | if (!currentContext)
| 0 | ||||||||||||
| 326 | return false; never executed: return false; | 0 | ||||||||||||
| 327 | - | |||||||||||||
| 328 | Q_D(QOpenGLTextureBlitter); | - | ||||||||||||
| 329 | - | |||||||||||||
| 330 | if (d->programs[QOpenGLTextureBlitterPrivate::TEXTURE_2D].glProgram)
| 0 | ||||||||||||
| 331 | return true; never executed: return true; | 0 | ||||||||||||
| 332 | - | |||||||||||||
| 333 | QSurfaceFormat format = currentContext->format(); | - | ||||||||||||
| 334 | if (format.profile() == QSurfaceFormat::CoreProfile && format.version() >= qMakePair(3,2)) {
| 0 | ||||||||||||
| 335 | if (!d->buildProgram(QOpenGLTextureBlitterPrivate::TEXTURE_2D, vertex_shader150, fragment_shader150))
| 0 | ||||||||||||
| 336 | return false; never executed: return false; | 0 | ||||||||||||
| 337 | } else { never executed: end of block | 0 | ||||||||||||
| 338 | if (!d->buildProgram(QOpenGLTextureBlitterPrivate::TEXTURE_2D, vertex_shader, fragment_shader))
| 0 | ||||||||||||
| 339 | return false; never executed: return false; | 0 | ||||||||||||
| 340 | if (supportsExternalOESTarget())
| 0 | ||||||||||||
| 341 | if (!d->buildProgram(QOpenGLTextureBlitterPrivate::TEXTURE_EXTERNAL_OES, vertex_shader, fragment_shader_external_oes))
| 0 | ||||||||||||
| 342 | return false; never executed: return false; | 0 | ||||||||||||
| 343 | } never executed: end of block | 0 | ||||||||||||
| 344 | - | |||||||||||||
| 345 | // Create and bind the VAO, if supported. | - | ||||||||||||
| 346 | QOpenGLVertexArrayObject::Binder vaoBinder(d->vao.data()); | - | ||||||||||||
| 347 | - | |||||||||||||
| 348 | d->vertexBuffer.create(); | - | ||||||||||||
| 349 | d->vertexBuffer.bind(); | - | ||||||||||||
| 350 | d->vertexBuffer.allocate(vertex_buffer_data, sizeof(vertex_buffer_data)); | - | ||||||||||||
| 351 | d->vertexBuffer.release(); | - | ||||||||||||
| 352 | - | |||||||||||||
| 353 | d->textureBuffer.create(); | - | ||||||||||||
| 354 | d->textureBuffer.bind(); | - | ||||||||||||
| 355 | d->textureBuffer.allocate(texture_buffer_data, sizeof(texture_buffer_data)); | - | ||||||||||||
| 356 | d->textureBuffer.release(); | - | ||||||||||||
| 357 | - | |||||||||||||
| 358 | return true; never executed: return true; | 0 | ||||||||||||
| 359 | } | - | ||||||||||||
| 360 | - | |||||||||||||
| 361 | bool QOpenGLTextureBlitter::isCreated() const | - | ||||||||||||
| 362 | { | - | ||||||||||||
| 363 | Q_D(const QOpenGLTextureBlitter); | - | ||||||||||||
| 364 | return d->programs[QOpenGLTextureBlitterPrivate::TEXTURE_2D].glProgram; never executed: return d->programs[QOpenGLTextureBlitterPrivate::TEXTURE_2D].glProgram; | 0 | ||||||||||||
| 365 | } | - | ||||||||||||
| 366 | - | |||||||||||||
| 367 | void QOpenGLTextureBlitter::destroy() | - | ||||||||||||
| 368 | { | - | ||||||||||||
| 369 | if (!isCreated())
| 0 | ||||||||||||
| 370 | return; never executed: return; | 0 | ||||||||||||
| 371 | Q_D(QOpenGLTextureBlitter); | - | ||||||||||||
| 372 | d->programs[QOpenGLTextureBlitterPrivate::TEXTURE_2D].glProgram.reset(); | - | ||||||||||||
| 373 | d->programs[QOpenGLTextureBlitterPrivate::TEXTURE_EXTERNAL_OES].glProgram.reset(); | - | ||||||||||||
| 374 | d->vertexBuffer.destroy(); | - | ||||||||||||
| 375 | d->textureBuffer.destroy(); | - | ||||||||||||
| 376 | d->vao.reset(); | - | ||||||||||||
| 377 | } never executed: end of block | 0 | ||||||||||||
| 378 | - | |||||||||||||
| 379 | bool QOpenGLTextureBlitter::supportsExternalOESTarget() const | - | ||||||||||||
| 380 | { | - | ||||||||||||
| 381 | QOpenGLContext *ctx = QOpenGLContext::currentContext(); | - | ||||||||||||
| 382 | return ctx && ctx->isOpenGLES() && ctx->hasExtension("GL_OES_EGL_image_external"); never executed: return ctx && ctx->isOpenGLES() && ctx->hasExtension("GL_OES_EGL_image_external"); | 0 | ||||||||||||
| 383 | } | - | ||||||||||||
| 384 | - | |||||||||||||
| 385 | void QOpenGLTextureBlitter::bind(GLenum target) | - | ||||||||||||
| 386 | { | - | ||||||||||||
| 387 | Q_D(QOpenGLTextureBlitter); | - | ||||||||||||
| 388 | - | |||||||||||||
| 389 | if (d->vao->isCreated())
| 0 | ||||||||||||
| 390 | d->vao->bind(); never executed: d->vao->bind(); | 0 | ||||||||||||
| 391 | - | |||||||||||||
| 392 | d->currentTarget = target; | - | ||||||||||||
| 393 | QOpenGLTextureBlitterPrivate::Program *p = &d->programs[targetToProgramIndex(target)]; | - | ||||||||||||
| 394 | p->glProgram->bind(); | - | ||||||||||||
| 395 | - | |||||||||||||
| 396 | d->vertexBuffer.bind(); | - | ||||||||||||
| 397 | p->glProgram->setAttributeBuffer(p->vertexCoordAttribPos, GL_FLOAT, 0, 3, 0); | - | ||||||||||||
| 398 | p->glProgram->enableAttributeArray(p->vertexCoordAttribPos); | - | ||||||||||||
| 399 | d->vertexBuffer.release(); | - | ||||||||||||
| 400 | - | |||||||||||||
| 401 | d->textureBuffer.bind(); | - | ||||||||||||
| 402 | p->glProgram->setAttributeBuffer(p->textureCoordAttribPos, GL_FLOAT, 0, 2, 0); | - | ||||||||||||
| 403 | p->glProgram->enableAttributeArray(p->textureCoordAttribPos); | - | ||||||||||||
| 404 | d->textureBuffer.release(); | - | ||||||||||||
| 405 | } never executed: end of block | 0 | ||||||||||||
| 406 | - | |||||||||||||
| 407 | void QOpenGLTextureBlitter::release() | - | ||||||||||||
| 408 | { | - | ||||||||||||
| 409 | Q_D(QOpenGLTextureBlitter); | - | ||||||||||||
| 410 | d->programs[targetToProgramIndex(d->currentTarget)].glProgram->release(); | - | ||||||||||||
| 411 | if (d->vao->isCreated())
| 0 | ||||||||||||
| 412 | d->vao->release(); never executed: d->vao->release(); | 0 | ||||||||||||
| 413 | } never executed: end of block | 0 | ||||||||||||
| 414 | - | |||||||||||||
| 415 | void QOpenGLTextureBlitter::setSwizzleRB(bool swizzle) | - | ||||||||||||
| 416 | { | - | ||||||||||||
| 417 | Q_D(QOpenGLTextureBlitter); | - | ||||||||||||
| 418 | d->swizzle = swizzle; | - | ||||||||||||
| 419 | } never executed: end of block | 0 | ||||||||||||
| 420 | - | |||||||||||||
| 421 | void QOpenGLTextureBlitter::setOpacity(float opacity) | - | ||||||||||||
| 422 | { | - | ||||||||||||
| 423 | Q_D(QOpenGLTextureBlitter); | - | ||||||||||||
| 424 | d->opacity = opacity; | - | ||||||||||||
| 425 | } never executed: end of block | 0 | ||||||||||||
| 426 | - | |||||||||||||
| 427 | void QOpenGLTextureBlitter::blit(GLuint texture, | - | ||||||||||||
| 428 | const QMatrix4x4 &targetTransform, | - | ||||||||||||
| 429 | Origin sourceOrigin) | - | ||||||||||||
| 430 | { | - | ||||||||||||
| 431 | Q_D(QOpenGLTextureBlitter); | - | ||||||||||||
| 432 | d->blit(texture,targetTransform, sourceOrigin); | - | ||||||||||||
| 433 | } never executed: end of block | 0 | ||||||||||||
| 434 | - | |||||||||||||
| 435 | void QOpenGLTextureBlitter::blit(GLuint texture, | - | ||||||||||||
| 436 | const QMatrix4x4 &targetTransform, | - | ||||||||||||
| 437 | const QMatrix3x3 &sourceTransform) | - | ||||||||||||
| 438 | { | - | ||||||||||||
| 439 | Q_D(QOpenGLTextureBlitter); | - | ||||||||||||
| 440 | d->blit(texture, targetTransform, sourceTransform); | - | ||||||||||||
| 441 | } never executed: end of block | 0 | ||||||||||||
| 442 | - | |||||||||||||
| 443 | QMatrix4x4 QOpenGLTextureBlitter::targetTransform(const QRectF &target, | - | ||||||||||||
| 444 | const QRect &viewport) | - | ||||||||||||
| 445 | { | - | ||||||||||||
| 446 | qreal x_scale = target.width() / viewport.width(); | - | ||||||||||||
| 447 | qreal y_scale = target.height() / viewport.height(); | - | ||||||||||||
| 448 | - | |||||||||||||
| 449 | const QPointF relative_to_viewport = target.topLeft() - viewport.topLeft(); | - | ||||||||||||
| 450 | qreal x_translate = x_scale - 1 + ((relative_to_viewport.x() / viewport.width()) * 2); | - | ||||||||||||
| 451 | qreal y_translate = -y_scale + 1 - ((relative_to_viewport.y() / viewport.height()) * 2); | - | ||||||||||||
| 452 | - | |||||||||||||
| 453 | QMatrix4x4 matrix; | - | ||||||||||||
| 454 | matrix(0,3) = x_translate; | - | ||||||||||||
| 455 | matrix(1,3) = y_translate; | - | ||||||||||||
| 456 | - | |||||||||||||
| 457 | matrix(0,0) = x_scale; | - | ||||||||||||
| 458 | matrix(1,1) = y_scale; | - | ||||||||||||
| 459 | - | |||||||||||||
| 460 | return matrix; never executed: return matrix; | 0 | ||||||||||||
| 461 | } | - | ||||||||||||
| 462 | - | |||||||||||||
| 463 | QMatrix3x3 QOpenGLTextureBlitter::sourceTransform(const QRectF &subTexture, | - | ||||||||||||
| 464 | const QSize &textureSize, | - | ||||||||||||
| 465 | Origin origin) | - | ||||||||||||
| 466 | { | - | ||||||||||||
| 467 | qreal x_scale = subTexture.width() / textureSize.width(); | - | ||||||||||||
| 468 | qreal y_scale = subTexture.height() / textureSize.height(); | - | ||||||||||||
| 469 | - | |||||||||||||
| 470 | const QPointF topLeft = subTexture.topLeft(); | - | ||||||||||||
| 471 | qreal x_translate = topLeft.x() / textureSize.width(); | - | ||||||||||||
| 472 | qreal y_translate = topLeft.y() / textureSize.height(); | - | ||||||||||||
| 473 | - | |||||||||||||
| 474 | if (origin == OriginTopLeft) {
| 0 | ||||||||||||
| 475 | y_scale = -y_scale; | - | ||||||||||||
| 476 | y_translate = 1 - y_translate; | - | ||||||||||||
| 477 | } never executed: end of block | 0 | ||||||||||||
| 478 | - | |||||||||||||
| 479 | QMatrix3x3 matrix; | - | ||||||||||||
| 480 | matrix(0,2) = x_translate; | - | ||||||||||||
| 481 | matrix(1,2) = y_translate; | - | ||||||||||||
| 482 | - | |||||||||||||
| 483 | matrix(0,0) = x_scale; | - | ||||||||||||
| 484 | matrix(1,1) = y_scale; | - | ||||||||||||
| 485 | - | |||||||||||||
| 486 | return matrix; never executed: return matrix; | 0 | ||||||||||||
| 487 | } | - | ||||||||||||
| 488 | - | |||||||||||||
| 489 | QT_END_NAMESPACE | - | ||||||||||||
| Source code | Switch to Preprocessed file |