~mterry/qtvideo-node/dont-install-tests

« back to all changes in this revision

Viewing changes to src/snapshotgenerator.cpp

  • Committer: Tarmac
  • Author(s): Jim Hodapp
  • Date: 2013-03-14 15:55:17 UTC
  • mfrom: (22.1.7 qtvideo-node)
  • Revision ID: tarmac-20130314155517-0vo0x2c5xgxde3ew
Enable Qt multithreaded rendering to work for both the media player and camera apps.

Approved by PS Jenkins bot, Günter Schwann.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2013 Canonical, Ltd.
 
3
 *
 
4
 * This program is free software; you can redistribute it and/or modify
 
5
 * it under the terms of the GNU Lesser General Public License as published by
 
6
 * the Free Software Foundation; version 3.
 
7
 *
 
8
 * This program is distributed in the hope that it will be useful,
 
9
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
 * GNU Lesser General Public License for more details.
 
12
 *
 
13
 * You should have received a copy of the GNU Lesser General Public License
 
14
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
15
 */
 
16
 
 
17
#include "snapshotgenerator.h"
 
18
 
 
19
#include <camera_compatibility_layer.h>
 
20
 
 
21
#include <QGLFramebufferObject>
 
22
#include <QOpenGLShaderProgram>
 
23
#include <QOpenGLShader>
 
24
 
 
25
#include <cstdio>
 
26
 
 
27
SnapshotGenerator::SnapshotGenerator()
 
28
    : m_width(0),
 
29
      m_height(0),
 
30
      position_loc(0),
 
31
      v_matrix_loc(0),
 
32
      tex_coord_loc(0),
 
33
      sampler_loc(0),
 
34
      tex_matrix_loc(0)
 
35
{
 
36
}
 
37
 
 
38
/**
 
39
 * @brief SnapshotGenerator::snapshot
 
40
 * @param textureId Texture to be stored as QImage
 
41
 * @return Image containing the content of the last texture
 
42
 */
 
43
QImage SnapshotGenerator::snapshot(GLuint textureId, const CameraControl *control)
 
44
{
 
45
    Q_ASSERT(textureId > 0);
 
46
    Q_ASSERT(control != NULL);
 
47
 
 
48
    QGLFramebufferObject fbo(m_width, m_height);
 
49
    QPainter paint(&fbo);
 
50
    fbo.bind();
 
51
 
 
52
#ifdef __arm__
 
53
    QOpenGLShaderProgram program;
 
54
    program.addShaderFromSourceCode(QOpenGLShader::Vertex, vertexShader());
 
55
    program.addShaderFromSourceCode(QOpenGLShader::Fragment, fragmentShader());
 
56
 
 
57
    program.link();
 
58
    program.bind();
 
59
 
 
60
    glViewport(0, 0, m_width, m_height);
 
61
 
 
62
    position_loc = program.attributeLocation("a_position");
 
63
    v_matrix_loc = program.uniformLocation("v_matrix");
 
64
    sampler_loc = program.uniformLocation("s_texture");
 
65
    tex_coord_loc = program.attributeLocation("a_texCoord");
 
66
    tex_matrix_loc = program.uniformLocation("m_texMatrix");
 
67
 
 
68
    glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
 
69
    glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
 
70
    glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
 
71
    glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
 
72
 
 
73
    GLfloat vVertices[] = {
 
74
        0.0f, 00.0f, 0.0f, // Position 0
 
75
        0.0f, 1.0f, 0.0f, // Position 1
 
76
        1.0f, 1.0f, 0.0f, // Position 2
 
77
        1.0f, 00.0f, 0.0f, // Position 3
 
78
    };
 
79
    vVertices[4] = m_height;
 
80
    vVertices[6] = m_width;
 
81
    vVertices[7] = m_height;
 
82
    vVertices[9] = m_width;
 
83
 
 
84
    GLfloat tVertices[] = {
 
85
        0.0f, 1.0f, // TexCoord 1
 
86
        0.0f, 0.0f, // TexCoord 0
 
87
        1.0f, 0.0f, // TexCoord 3
 
88
        1.0f, 1.0f // TexCoord 2
 
89
    };
 
90
 
 
91
    GLushort indices[] = { 0, 1, 2, 0, 2, 3 };
 
92
 
 
93
    program.enableAttributeArray(position_loc);
 
94
    program.setAttributeArray(position_loc, vVertices, 3);
 
95
    QMatrix4x4 pmvMatrix;
 
96
    pmvMatrix.ortho(QRect(0,0,m_width,m_height));
 
97
    program.setUniformValue(v_matrix_loc, pmvMatrix);
 
98
 
 
99
    program.enableAttributeArray(tex_coord_loc);
 
100
    program.setAttributeArray(tex_coord_loc, tVertices, 2);
 
101
 
 
102
    program.enableAttributeArray(sampler_loc);
 
103
    program.setUniformValue(sampler_loc, 0);
 
104
    program.enableAttributeArray(tex_matrix_loc);
 
105
 
 
106
    GLfloat textureMatrix[16];
 
107
    android_camera_get_preview_texture_transformation(const_cast<CameraControl*>(control), textureMatrix);
 
108
    QMatrix4x4 texMat(textureMatrix);
 
109
    texMat = texMat.transposed();
 
110
    program.setUniformValue(tex_matrix_loc, texMat);
 
111
 
 
112
    glBindTexture(GL_TEXTURE_2D, textureId);
 
113
    glActiveTexture(GL_TEXTURE0);
 
114
 
 
115
    glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, indices);
 
116
#endif
 
117
 
 
118
    return fbo.toImage();
 
119
}
 
120
 
 
121
void SnapshotGenerator::setSize(int width, int height)
 
122
{
 
123
    m_width = width;
 
124
    m_height = height;
 
125
}
 
126
 
 
127
const char *SnapshotGenerator::vertexShader() const
 
128
{
 
129
    return
 
130
        "#extension GL_OES_EGL_image_external : require              \n"
 
131
        "attribute vec4 a_position;                                  \n"
 
132
        "uniform highp mat4 v_matrix;                                \n"
 
133
        "attribute vec2 a_texCoord;                                  \n"
 
134
        "uniform mat4 m_texMatrix;                                   \n"
 
135
        "varying vec2 v_texCoord;                                    \n"
 
136
        "void main()                                                 \n"
 
137
        "{                                                           \n"
 
138
        "   gl_Position = v_matrix * a_position;                     \n"
 
139
        "   v_texCoord = (m_texMatrix * vec4(a_texCoord, 0.0, 1.0)).xy;\n"
 
140
        "}                                                           \n";
 
141
}
 
142
 
 
143
const char *SnapshotGenerator::fragmentShader() const
 
144
{
 
145
    return
 
146
        "#extension GL_OES_EGL_image_external : require      \n"
 
147
        "precision mediump float;                            \n"
 
148
        "varying vec2 v_texCoord;                            \n"
 
149
        "uniform samplerExternalOES s_texture;               \n"
 
150
        "void main()                                         \n"
 
151
        "{                                                   \n"
 
152
        "    gl_FragColor = texture2D( s_texture, v_texCoord );\n"
 
153
        "}                                                   \n";
 
154
}