Don't crash when sending touch events.
[profile/ivi/qtwayland.git] / examples / qwindow-compositor / textureblitter.cpp
1 #include "textureblitter.h"
2
3 #include <QtGui/QOpenGLShaderProgram>
4 #include <QtGui/QOpenGLContext>
5 #include <QtGui/QOpenGLFunctions>
6
7 TextureBlitter::TextureBlitter()
8     : m_shaderProgram(new QOpenGLShaderProgram())
9 {
10     static const char *textureVertexProgram =
11             "uniform highp mat4 matrix;\n"
12             "attribute highp vec3 vertexCoordEntry;\n"
13             "attribute highp vec2 textureCoordEntry;\n"
14             "varying highp vec2 textureCoord;\n"
15             "void main() {\n"
16             "   textureCoord = textureCoordEntry;\n"
17             "   gl_Position = matrix * vec4(vertexCoordEntry, 1);\n"
18             "}\n";
19
20     static const char *textureFragmentProgram =
21             "uniform sampler2D texture;\n"
22             "varying highp vec2 textureCoord;\n"
23             "void main() {\n"
24             "   gl_FragColor = texture2D(texture, textureCoord);\n"
25             "}\n";
26
27     //Enable transparent windows
28     glEnable(GL_BLEND);
29     glBlendFunc (GL_ONE,GL_ONE_MINUS_SRC_ALPHA);
30
31     m_shaderProgram->addShaderFromSourceCode(QOpenGLShader::Vertex, textureVertexProgram);
32     m_shaderProgram->addShaderFromSourceCode(QOpenGLShader::Fragment, textureFragmentProgram);
33     m_shaderProgram->link();
34 }
35
36
37 void TextureBlitter::bind()
38 {
39
40     m_shaderProgram->bind();
41
42     m_vertexCoordEntry = m_shaderProgram->attributeLocation("vertexCoordEntry");
43     m_textureCoordEntry = m_shaderProgram->attributeLocation("textureCoordEntry");
44     m_matrixLocation = m_shaderProgram->uniformLocation("matrix");
45 }
46
47 void TextureBlitter::release()
48 {
49     m_shaderProgram->release();
50 }
51
52 void TextureBlitter::drawTexture(int textureId, const QRectF &targetRect, const QSize &targetSize, int depth, bool targethasInvertedY, bool sourceHasInvertedY)
53 {
54
55     glViewport(0,0,targetSize.width(),targetSize.height());
56     GLfloat zValue = depth / 1000.0f;
57     //Set Texture and Vertex coordinates
58     const GLfloat textureCoordinates[] = {
59         0, 0,
60         1, 0,
61         1, 1,
62         0, 1
63     };
64
65     int x1 = targetRect.left();
66     int x2 = targetRect.right();
67     int y1, y2;
68     if (targethasInvertedY) {
69         if (sourceHasInvertedY) {
70             y1 = targetRect.top();
71             y2 = targetRect.bottom();
72         } else {
73             y1 = targetRect.bottom();
74             y2 = targetRect.top();
75         }
76     } else {
77         if (sourceHasInvertedY) {
78             y1 = targetSize.height() - targetRect.top();
79             y2 = targetSize.height() - targetRect.bottom();
80         } else {
81             y1 = targetSize.height() - targetRect.bottom();
82             y2 = targetSize.height() - targetRect.top();
83         }
84     }
85
86     const GLfloat vertexCoordinates[] = {
87         x1, y1, zValue,
88         x2, y1, zValue,
89         x2, y2, zValue,
90         x1, y2, zValue
91     };
92
93     //Set matrix to transfrom geometry values into gl coordinate space.
94     m_transformMatrix.setToIdentity();
95     m_transformMatrix.scale( 2.0f / targetSize.width(), 2.0f / targetSize.height() );
96     m_transformMatrix.translate(-targetSize.width() / 2.0f, -targetSize.height() / 2.0f);
97
98     //attach the data!
99     QOpenGLContext *currentContext = QOpenGLContext::currentContext();
100     currentContext->functions()->glEnableVertexAttribArray(m_vertexCoordEntry);
101     currentContext->functions()->glEnableVertexAttribArray(m_textureCoordEntry);
102
103     currentContext->functions()->glVertexAttribPointer(m_vertexCoordEntry, 3, GL_FLOAT, GL_FALSE, 0, vertexCoordinates);
104     currentContext->functions()->glVertexAttribPointer(m_textureCoordEntry, 2, GL_FLOAT, GL_FALSE, 0, textureCoordinates);
105     m_shaderProgram->setUniformValue(m_matrixLocation, m_transformMatrix);
106
107     glBindTexture(GL_TEXTURE_2D, textureId);
108
109     glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
110     glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
111
112     glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
113
114     glBindTexture(GL_TEXTURE_2D, 0);
115
116     currentContext->functions()->glDisableVertexAttribArray(m_vertexCoordEntry);
117     currentContext->functions()->glDisableVertexAttribArray(m_textureCoordEntry);
118 }