2 * This file is part of CameraPlus.
4 * Copyright (C) 2012-2013 Mohammed Sameer <msameer@foolab.org>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 #include "qtcamviewfinderrenderermeego.h"
23 #include <gst/video/video.h>
25 #include "qtcamconfig.h"
28 #include <QGLShaderProgram>
29 #include <gst/interfaces/meegovideotexture.h>
31 QT_CAM_VIEWFINDER_RENDERER(RENDERER_TYPE_MEEGO, QtCamViewfinderRendererMeeGo);
33 #define GL_TEXTURE_EXTERNAL_OES 0x8060
35 typedef void *EGLSyncKHR;
36 #define EGL_SYNC_FENCE_KHR 0x30F9
38 typedef EGLSyncKHR(EGLAPIENTRYP _PFNEGLCREATESYNCKHRPROC)(EGLDisplay dpy, EGLenum type,
39 const EGLint *attrib_list);
41 _PFNEGLCREATESYNCKHRPROC eglCreateSyncKHR = 0;
43 static const QString FRAGMENT_SHADER = ""
44 "#extension GL_OES_EGL_image_external: enable\n"
45 "uniform samplerExternalOES texture0;"
46 "varying lowp vec2 fragTexCoord;"
48 " gl_FragColor = texture2D(texture0, fragTexCoord);"
52 static const QString VERTEX_SHADER = ""
53 "attribute highp vec4 inputVertex;"
54 "attribute lowp vec2 textureCoord;"
55 "uniform highp mat4 matrix;"
56 "uniform highp mat4 matrixWorld;"
57 "varying lowp vec2 fragTexCoord;"
60 " gl_Position = matrix * matrixWorld * inputVertex;"
61 " fragTexCoord = textureCoord;"
65 QtCamViewfinderRendererMeeGo::QtCamViewfinderRendererMeeGo(QtCamConfig *config,
67 QtCamViewfinderRenderer(config, parent),
76 // Texture coordinates:
77 // Reversed because of Qt reversed coordinate system
78 m_texCoords[0] = 0; m_texCoords[1] = 1;
79 m_texCoords[2] = 1; m_texCoords[3] = 1;
80 m_texCoords[4] = 1; m_texCoords[5] = 0;
81 m_texCoords[6] = 0; m_texCoords[7] = 0;
83 bzero(&m_vertexCoords, 8);
86 QtCamViewfinderRendererMeeGo::~QtCamViewfinderRendererMeeGo() {
88 g_signal_handler_disconnect(m_sink, m_id);
89 g_signal_handler_disconnect(m_sink, m_notify);
90 g_object_remove_toggle_ref(G_OBJECT(m_sink), (GToggleNotify)sink_notify, this);
95 bool QtCamViewfinderRendererMeeGo::needsNativePainting() {
99 void QtCamViewfinderRendererMeeGo::paint(const QMatrix4x4& matrix, const QRectF& viewport) {
100 QMutexLocker locker(&m_frameMutex);
106 calculateProjectionMatrix(viewport);
108 if (!eglCreateSyncKHR && m_conf->viewfinderUseFence()) {
109 eglCreateSyncKHR = (_PFNEGLCREATESYNCKHRPROC)eglGetProcAddress("eglCreateSyncKHR");
111 if (!eglCreateSyncKHR) {
112 qWarning() << "eglCreateSyncKHR not found. Fences disabled";
120 // Program will be created if needed and will never be deleted even
121 // if attaching the shaders fail.
125 paintFrame(matrix, m_frame);
128 void QtCamViewfinderRendererMeeGo::resize(const QSizeF& size) {
129 if (size == m_size) {
135 m_renderArea = QRectF();
139 // This will destroy everything
140 // but we need a way to reset the viewport and the transformation matrix only.
143 emit renderAreaChanged();
146 void QtCamViewfinderRendererMeeGo::reset() {
147 QMutexLocker locker(&m_frameMutex);
151 GstElement *QtCamViewfinderRendererMeeGo::sinkElement() {
152 if (!QGLContext::currentContext()) {
153 qCritical() << "Cannot create the GStreamer element without an OpenGL context.";
158 m_sink = gst_element_factory_make(m_conf->viewfinderSink().toLatin1().data(),
159 "QtCamViewfinderRendererMeeGoSink");
161 qCritical() << "Failed to create" << m_conf->viewfinderSink();
165 g_object_add_toggle_ref(G_OBJECT(m_sink), (GToggleNotify)sink_notify, this);
168 Display *d = QX11Info::display();
169 g_object_set(G_OBJECT(m_sink), "x-display", d, "use-framebuffer-memory", TRUE, NULL);
171 m_dpy = eglGetDisplay((EGLNativeDisplayType)d);
172 if (m_dpy == EGL_NO_DISPLAY) {
173 qCritical() << "Failed to obtain EGL Display";
176 EGLContext context = eglGetCurrentContext();
177 if (context == EGL_NO_CONTEXT) {
178 qCritical() << "Failed to obtain EGL context";
181 g_object_set(G_OBJECT(m_sink), "egl-display", m_dpy, "egl-context", context, NULL);
183 m_id = g_signal_connect(G_OBJECT(m_sink), "frame-ready", G_CALLBACK(frame_ready), this);
185 GstPad *pad = gst_element_get_static_pad(m_sink, "sink");
186 m_notify = g_signal_connect(G_OBJECT(pad), "notify::caps",
187 G_CALLBACK(sink_caps_changed), this);
188 gst_object_unref(pad);
193 void QtCamViewfinderRendererMeeGo::frame_ready(GstElement *sink, int frame,
194 QtCamViewfinderRendererMeeGo *r) {
198 r->m_frameMutex.lock();
200 r->m_frameMutex.unlock();
202 QMetaObject::invokeMethod(r, "updateRequested", Qt::QueuedConnection);
205 void QtCamViewfinderRendererMeeGo::sink_notify(QtCamViewfinderRendererMeeGo *q,
206 GObject *object, gboolean is_last_ref) {
211 g_signal_handler_disconnect(q->m_sink, q->m_id);
212 g_object_remove_toggle_ref(G_OBJECT(q->m_sink), (GToggleNotify)sink_notify, q);
217 void QtCamViewfinderRendererMeeGo::sink_caps_changed(GObject *obj, GParamSpec *pspec,
218 QtCamViewfinderRendererMeeGo *q) {
223 if (obj && gst_video_get_size(GST_PAD(obj), &width, &height)) {
224 QMetaObject::invokeMethod(q, "setVideoSize", Qt::QueuedConnection,
225 Q_ARG(QSizeF, QSizeF(width, height)));
229 void QtCamViewfinderRendererMeeGo::calculateProjectionMatrix(const QRectF& rect) {
230 m_projectionMatrix = QMatrix4x4();
231 m_projectionMatrix.ortho(rect);
234 void QtCamViewfinderRendererMeeGo::createProgram() {
239 m_program = new QGLShaderProgram(this);
241 if (!m_program->addShaderFromSourceCode(QGLShader::Vertex, VERTEX_SHADER)) {
242 qCritical() << "Failed to add vertex shader";
246 if (!m_program->addShaderFromSourceCode(QGLShader::Fragment, FRAGMENT_SHADER)) {
247 qCritical() << "Failed to add fragment shader";
251 m_program->bindAttributeLocation("inputVertex", 0);
252 m_program->bindAttributeLocation("textureCoord", 1);
254 if (!m_program->link()) {
255 qCritical() << "Failed to link program!";
259 if (!m_program->bind()) {
260 qCritical() << "Failed to bind program";
264 m_program->setUniformValue("texture0", 0); // texture UNIT 0
265 m_program->release();
268 void QtCamViewfinderRendererMeeGo::paintFrame(const QMatrix4x4& matrix, int frame) {
275 MeegoGstVideoTexture *sink = MEEGO_GST_VIDEO_TEXTURE(m_sink);
276 if (!meego_gst_video_texture_acquire_frame(sink, frame)) {
277 qDebug() << "Failed to acquire frame";
283 m_program->setUniformValue("matrix", m_projectionMatrix);
284 m_program->setUniformValue("matrixWorld", matrix);
286 if (!meego_gst_video_texture_bind_frame(sink, GL_TEXTURE_EXTERNAL_OES, frame)) {
287 qDebug() << "Failed to bind frame";
288 m_program->release();
292 glEnableVertexAttribArray(0);
293 glEnableVertexAttribArray(1);
295 glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, &m_vertexCoords);
296 glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, &m_texCoords);
298 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
300 if (!meego_gst_video_texture_bind_frame(sink, GL_TEXTURE_EXTERNAL_OES, -1)) {
301 qDebug() << "Failed to unbind frame";
304 glDisableVertexAttribArray(1);
305 glDisableVertexAttribArray(0);
307 m_program->release();
309 if (eglCreateSyncKHR) {
310 sync = eglCreateSyncKHR(m_dpy, EGL_SYNC_FENCE_KHR, NULL);
313 meego_gst_video_texture_release_frame(sink, frame, sync);
316 void QtCamViewfinderRendererMeeGo::calculateCoords() {
317 if (!m_size.isValid() || !m_videoSize.isValid()) {
321 QRectF area = renderArea();
323 qreal leftMargin = area.x();
324 qreal topMargin = area.y();
325 QSizeF renderSize = area.size();
327 m_vertexCoords[0] = leftMargin;
328 m_vertexCoords[1] = topMargin + renderSize.height();
330 m_vertexCoords[2] = renderSize.width() + leftMargin;
331 m_vertexCoords[3] = topMargin + renderSize.height();
333 m_vertexCoords[4] = renderSize.width() + leftMargin;
334 m_vertexCoords[5] = topMargin;
336 m_vertexCoords[6] = leftMargin;
337 m_vertexCoords[7] = topMargin;
340 QRectF QtCamViewfinderRendererMeeGo::renderArea() {
341 if (!m_renderArea.isNull()) {
345 QSizeF renderSize = m_videoSize;
346 renderSize.scale(m_size, Qt::KeepAspectRatio);
348 qreal leftMargin = (m_size.width() - renderSize.width())/2.0;
349 qreal topMargin = (m_size.height() - renderSize.height())/2.0;
351 m_renderArea = QRectF(QPointF(leftMargin, topMargin), renderSize);
356 QSizeF QtCamViewfinderRendererMeeGo::videoResolution() {
360 void QtCamViewfinderRendererMeeGo::setVideoSize(const QSizeF& size) {
361 if (size == m_videoSize) {
367 m_renderArea = QRectF();
373 emit renderAreaChanged();
374 emit videoResolutionChanged();