1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the Qt Compositor.
8 ** $QT_BEGIN_LICENSE:BSD$
9 ** You may use this file under the terms of the BSD license as follows:
11 ** "Redistribution and use in source and binary forms, with or without
12 ** modification, are permitted provided that the following conditions are
14 ** * Redistributions of source code must retain the above copyright
15 ** notice, this list of conditions and the following disclaimer.
16 ** * Redistributions in binary form must reproduce the above copyright
17 ** notice, this list of conditions and the following disclaimer in
18 ** the documentation and/or other materials provided with the
20 ** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
21 ** the names of its contributors may be used to endorse or promote
22 ** products derived from this software without specific prior written
25 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
39 ****************************************************************************/
41 #include "textureblitter.h"
43 #include <QtGui/QOpenGLShaderProgram>
44 #include <QtGui/QOpenGLContext>
45 #include <QtGui/QOpenGLFunctions>
47 TextureBlitter::TextureBlitter()
48 : m_shaderProgram(new QOpenGLShaderProgram)
50 static const char *textureVertexProgram =
51 "uniform highp mat4 matrix;\n"
52 "attribute highp vec3 vertexCoordEntry;\n"
53 "attribute highp vec2 textureCoordEntry;\n"
54 "varying highp vec2 textureCoord;\n"
56 " textureCoord = textureCoordEntry;\n"
57 " gl_Position = matrix * vec4(vertexCoordEntry, 1);\n"
60 static const char *textureFragmentProgram =
61 "uniform sampler2D texture;\n"
62 "varying highp vec2 textureCoord;\n"
64 " gl_FragColor = texture2D(texture, textureCoord);\n"
67 //Enable transparent windows
69 glBlendFunc (GL_ONE,GL_ONE_MINUS_SRC_ALPHA);
71 m_shaderProgram->addShaderFromSourceCode(QOpenGLShader::Vertex, textureVertexProgram);
72 m_shaderProgram->addShaderFromSourceCode(QOpenGLShader::Fragment, textureFragmentProgram);
73 m_shaderProgram->link();
76 TextureBlitter::~TextureBlitter()
78 delete m_shaderProgram;
81 void TextureBlitter::bind()
84 m_shaderProgram->bind();
86 m_vertexCoordEntry = m_shaderProgram->attributeLocation("vertexCoordEntry");
87 m_textureCoordEntry = m_shaderProgram->attributeLocation("textureCoordEntry");
88 m_matrixLocation = m_shaderProgram->uniformLocation("matrix");
91 void TextureBlitter::release()
93 m_shaderProgram->release();
96 void TextureBlitter::drawTexture(int textureId, const QRectF &targetRect, const QSize &targetSize, int depth, bool targethasInvertedY, bool sourceHasInvertedY)
99 glViewport(0,0,targetSize.width(),targetSize.height());
100 GLfloat zValue = depth / 1000.0f;
101 //Set Texture and Vertex coordinates
102 const GLfloat textureCoordinates[] = {
109 int x1 = targetRect.left();
110 int x2 = targetRect.right();
112 if (targethasInvertedY) {
113 if (sourceHasInvertedY) {
114 y1 = targetRect.top();
115 y2 = targetRect.bottom();
117 y1 = targetRect.bottom();
118 y2 = targetRect.top();
121 if (sourceHasInvertedY) {
122 y1 = targetSize.height() - targetRect.top();
123 y2 = targetSize.height() - targetRect.bottom();
125 y1 = targetSize.height() - targetRect.bottom();
126 y2 = targetSize.height() - targetRect.top();
130 const GLfloat vertexCoordinates[] = {
137 //Set matrix to transfrom geometry values into gl coordinate space.
138 m_transformMatrix.setToIdentity();
139 m_transformMatrix.scale( 2.0f / targetSize.width(), 2.0f / targetSize.height() );
140 m_transformMatrix.translate(-targetSize.width() / 2.0f, -targetSize.height() / 2.0f);
143 QOpenGLContext *currentContext = QOpenGLContext::currentContext();
144 currentContext->functions()->glEnableVertexAttribArray(m_vertexCoordEntry);
145 currentContext->functions()->glEnableVertexAttribArray(m_textureCoordEntry);
147 currentContext->functions()->glVertexAttribPointer(m_vertexCoordEntry, 3, GL_FLOAT, GL_FALSE, 0, vertexCoordinates);
148 currentContext->functions()->glVertexAttribPointer(m_textureCoordEntry, 2, GL_FLOAT, GL_FALSE, 0, textureCoordinates);
149 m_shaderProgram->setUniformValue(m_matrixLocation, m_transformMatrix);
151 glBindTexture(GL_TEXTURE_2D, textureId);
153 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
154 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
156 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
158 glBindTexture(GL_TEXTURE_2D, 0);
160 currentContext->functions()->glDisableVertexAttribArray(m_vertexCoordEntry);
161 currentContext->functions()->glDisableVertexAttribArray(m_textureCoordEntry);