20
20
#include "shadervideonodeplugin.h"
22
#include "camera_compatibility_layer.h"
26
#include <QtQuick/qsgmaterial.h>
28
class ShaderVideoShader : public QSGMaterialShader
31
ShaderVideoShader(QVideoFrame::PixelFormat pixelFormat)
32
: QSGMaterialShader(),
33
m_pixelFormat(pixelFormat)
37
void updateState(const RenderState &state, QSGMaterial *newMaterial, QSGMaterial *oldMaterial);
39
virtual char const *const *attributeNames() const {
40
static const char *names[] = {
52
virtual const char *vertexShader() const {
54
"#extension GL_OES_EGL_image_external : require \n"
55
"uniform highp mat4 qt_Matrix; \n"
56
"attribute highp vec4 qt_VertexPosition; \n"
57
"attribute highp vec2 qt_VertexTexCoord; \n"
58
"varying highp vec2 qt_TexCoord; \n"
59
"uniform mat4 s_tex_Matrix; \n"
61
" qt_TexCoord = (s_tex_Matrix * vec4(qt_VertexTexCoord, 0.0, 1.0)).xy;\n"
62
" gl_Position = qt_Matrix * qt_VertexPosition; \n"
67
virtual const char *fragmentShader() const {
68
static const char *shader =
69
"#extension GL_OES_EGL_image_external : require \n"
70
"uniform samplerExternalOES sTexture; \n"
71
"uniform lowp float opacity; \n"
72
"varying highp vec2 qt_TexCoord; \n"
75
" gl_FragColor = texture2D( sTexture, qt_TexCoord );\n"
80
virtual void initialize() {
81
m_id_matrix = program()->uniformLocation("qt_Matrix");
82
m_id_Texture = program()->uniformLocation("sTexture");
83
m_id_opacity = program()->uniformLocation("opacity");
84
m_tex_matrix = program()->uniformLocation("s_tex_Matrix");
90
QVideoFrame::PixelFormat m_pixelFormat;
93
class ShaderVideoMaterial : public QSGMaterial
96
ShaderVideoMaterial(const QVideoSurfaceFormat &format)
101
~ShaderVideoMaterial() {}
103
QSGMaterialShader *createShader() const {
104
m_videoShader = new ShaderVideoShader(m_format.pixelFormat());
105
return m_videoShader;
108
virtual QSGMaterialType *type() const {
109
static QSGMaterialType theType;
113
void setCamControl(CameraControl *cc) {
117
CameraControl *cameraControl() const { return m_camControl; }
122
qWarning() << "No valid CameraControl";
126
android_camera_update_preview_texture(m_camControl);
128
android_camera_get_preview_texture_transformation(m_camControl, m_textureMatrix);
130
glUniformMatrix4fv(m_videoShader->m_tex_matrix, 1, GL_FALSE, m_textureMatrix);
132
glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
133
glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
134
glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
135
glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
138
// As long as coordinates are 0..1, the y is flipped
141
// reduced way to multiplay the matrix with following matrix and assigin it back again
146
m_textureMatrix[1] = -m_textureMatrix[1] + m_textureMatrix[3];
147
m_textureMatrix[5] = -m_textureMatrix[5] + m_textureMatrix[7];
148
m_textureMatrix[9] = -m_textureMatrix[9] + m_textureMatrix[11];
149
m_textureMatrix[13] = -m_textureMatrix[13] + m_textureMatrix[15];
153
QVideoSurfaceFormat m_format;
154
CameraControl *m_camControl;
155
mutable ShaderVideoShader *m_videoShader;
156
GLfloat m_textureMatrix[16];
157
GLfloat m_flippedTextureMatrix[16];
162
ShaderVideoNode::ShaderVideoNode(const QVideoSurfaceFormat &format) :
165
m_material = new ShaderVideoMaterial(format);
166
setMaterial(m_material);
169
void ShaderVideoNode::setCurrentFrame(const QVideoFrame &frame)
171
QMutexLocker ml(&m_guard);
172
if (! m_material->cameraControl()) {
173
if (!frame.availableMetaData().contains("CamControl")) {
174
qDebug() << "No camera control included in video frame";
177
int ci = frame.metaData("CamControl").toInt();
178
m_material->setCamControl((CameraControl*)ci);
180
markDirty(DirtyMaterial);
21
#include "shadervideonode.h"
185
23
QList<QVideoFrame::PixelFormat> ShaderVideoNodePlugin::supportedPixelFormats(
186
24
QAbstractVideoBuffer::HandleType handleType) const
210
void ShaderVideoShader::updateState(const RenderState &state,
211
QSGMaterial *newMaterial,
212
QSGMaterial *oldMaterial)
214
Q_UNUSED(oldMaterial);
215
ShaderVideoMaterial *mat = dynamic_cast<ShaderVideoMaterial *>(newMaterial);
216
program()->setUniformValue(m_id_Texture, 0);
218
if (state.isMatrixDirty())
219
program()->setUniformValue(m_id_matrix, state.combinedMatrix());
223
// if (state.isOpacityDirty()) {
224
// mat->m_opacity = state.opacity();
225
// mat->updateBlending();
226
// program()->setUniformValue(m_id_opacity, GLfloat(mat->m_opacity));