d42a92a408eca233ab826ac4fd68f591ef9c304d
[harmattan/cameraplus] / lib / qtcamviewfinderrenderermeego.cpp
1 /*!
2  * This file is part of CameraPlus.
3  *
4  * Copyright (C) 2012 Mohammed Sameer <msameer@foolab.org>
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #include "qtcamviewfinderrenderermeego.h"
22 #include <QDebug>
23 #include <gst/video/video.h>
24 #include <QPainter>
25 #include "qtcamconfig.h"
26 #include <QX11Info>
27 #include <QGLContext>
28 #include <QGLShaderProgram>
29 #include <gst/interfaces/meegovideotexture.h>
30
31 QT_CAM_VIEWFINDER_RENDERER(RENDERER_TYPE_MEEGO, QtCamViewfinderRendererMeeGo);
32
33 #define GL_TEXTURE_EXTERNAL_OES                  0x8060
34
35 typedef void *EGLSyncKHR;
36 #define EGL_SYNC_FENCE_KHR                       0x30F9
37
38 typedef EGLSyncKHR(EGLAPIENTRYP _PFNEGLCREATESYNCKHRPROC)(EGLDisplay dpy, EGLenum type,
39                                                           const EGLint *attrib_list);
40
41 _PFNEGLCREATESYNCKHRPROC eglCreateSyncKHR = 0;
42
43 static const QString FRAGMENT_SHADER = ""
44     "#extension GL_OES_EGL_image_external: enable\n"
45     "uniform samplerExternalOES texture0;"
46     "varying lowp vec2 fragTexCoord;"
47     "void main() {"
48     "  gl_FragColor = texture2D(texture0, fragTexCoord);"
49     "}"
50   "";
51
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;"
58     ""
59     "void main() {"
60     "  gl_Position = matrix * matrixWorld * inputVertex;"
61     "  fragTexCoord = textureCoord;"
62     "}"
63   "";
64
65 QtCamViewfinderRendererMeeGo::QtCamViewfinderRendererMeeGo(QtCamConfig *config,
66                                                            QObject *parent) :
67   QtCamViewfinderRenderer(config, parent),
68   m_conf(config),
69   m_sink(0),
70   m_frame(-1),
71   m_id(0),
72   m_notify(0),
73   m_needsInit(true),
74   m_program(0) {
75
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;
82
83   bzero(&m_vertexCoords, 8);
84 }
85
86 QtCamViewfinderRendererMeeGo::~QtCamViewfinderRendererMeeGo() {
87   if (m_sink) {
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);
91     m_sink = 0;
92   }
93 }
94
95 void QtCamViewfinderRendererMeeGo::paint(QPainter *painter) {
96   QMutexLocker locker(&m_frameMutex);
97   if (m_frame == -1) {
98     return;
99   }
100
101   painter->beginNativePainting();
102
103   if (m_needsInit) {
104     calculateProjectionMatrix(painter->viewport());
105
106     if (!eglCreateSyncKHR && m_conf->viewfinderUseFence()) {
107       eglCreateSyncKHR = (_PFNEGLCREATESYNCKHRPROC)eglGetProcAddress("eglCreateSyncKHR");
108
109       if (!eglCreateSyncKHR) {
110         qWarning() << "eglCreateSyncKHR not found. Fences disabled";
111       }
112     }
113
114     m_needsInit = false;
115   }
116
117   if (!m_program) {
118     // Program will be created if needed and will never be deleted even
119     // if attaching the shaders fail.
120     createProgram();
121   }
122
123   paintFrame(painter, m_frame);
124
125   painter->endNativePainting();
126 }
127
128 void QtCamViewfinderRendererMeeGo::resize(const QSizeF& size) {
129   if (size == m_size) {
130     return;
131   }
132
133   m_size = size;
134
135   m_renderArea = QRectF();
136
137   calculateCoords();
138
139   // This will destroy everything
140   // but we need a way to reset the viewport and the transformation matrix only.
141   m_needsInit = true;
142
143   emit renderAreaChanged();
144 }
145
146 void QtCamViewfinderRendererMeeGo::reset() {
147   QMutexLocker locker(&m_frameMutex);
148   m_frame = -1;
149 }
150
151 GstElement *QtCamViewfinderRendererMeeGo::sinkElement() {
152   if (!QGLContext::currentContext()) {
153     qCritical() << "Cannot create the GStreamer element without an OpenGL context.";
154     return 0;
155   }
156
157   if (!m_sink) {
158     m_sink = gst_element_factory_make(m_conf->viewfinderSink().toAscii().data(),
159                                       "QtCamViewfinderRendererMeeGoSink");
160     if (!m_sink) {
161       qCritical() << "Failed to create" << m_conf->viewfinderSink();
162       return 0;
163     }
164
165     g_object_add_toggle_ref(G_OBJECT(m_sink), (GToggleNotify)sink_notify, this);
166   }
167
168   // Displa can be replaced with a null pointer.
169   // We all know that the sink used for Harmattan ignores the x-display property ;-)
170
171   Display *d = QX11Info::display();
172   g_object_set(G_OBJECT(m_sink), "x-display", d, "use-framebuffer-memory", TRUE, NULL);
173
174   m_dpy = eglGetDisplay((EGLNativeDisplayType)d);
175   EGLContext context = eglGetCurrentContext();
176
177   g_object_set(G_OBJECT(m_sink), "egl-display", m_dpy, "egl-context", context, NULL);
178
179   m_id = g_signal_connect(G_OBJECT(m_sink), "frame-ready", G_CALLBACK(frame_ready), this);
180
181   GstPad *pad = gst_element_get_static_pad(m_sink, "sink");
182   m_notify = g_signal_connect(G_OBJECT(pad), "notify::caps",
183                               G_CALLBACK(sink_caps_changed), this);
184   gst_object_unref(pad);
185
186   return m_sink;
187 }
188
189 void QtCamViewfinderRendererMeeGo::frame_ready(GstElement *sink, int frame,
190                                                QtCamViewfinderRendererMeeGo *r) {
191   Q_UNUSED(sink);
192   Q_UNUSED(frame);
193
194   r->m_frameMutex.lock();
195   r->m_frame = frame;
196   r->m_frameMutex.unlock();
197
198   QMetaObject::invokeMethod(r, "updateRequested", Qt::QueuedConnection);
199 }
200
201 void QtCamViewfinderRendererMeeGo::sink_notify(QtCamViewfinderRendererMeeGo *q,
202                                                GObject *object, gboolean is_last_ref) {
203
204   Q_UNUSED(object);
205
206   if (is_last_ref) {
207     g_signal_handler_disconnect(q->m_sink, q->m_id);
208     g_object_remove_toggle_ref(G_OBJECT(q->m_sink), (GToggleNotify)sink_notify, q);
209     q->m_sink = 0;
210   }
211 }
212
213 void QtCamViewfinderRendererMeeGo::sink_caps_changed(GObject *obj, GParamSpec *pspec,
214                                                      QtCamViewfinderRendererMeeGo *q) {
215   Q_UNUSED(obj);
216   Q_UNUSED(pspec);
217
218   int width, height;
219   if (obj && gst_video_get_size(GST_PAD(obj), &width, &height)) {
220     QMetaObject::invokeMethod(q, "setVideoSize", Qt::QueuedConnection,
221                               Q_ARG(QSizeF, QSizeF(width, height)));
222   }
223 }
224
225 void QtCamViewfinderRendererMeeGo::calculateProjectionMatrix(const QRectF& rect) {
226   m_projectionMatrix = QMatrix4x4();
227   m_projectionMatrix.ortho(rect);
228 }
229
230 void QtCamViewfinderRendererMeeGo::createProgram() {
231   if (m_program) {
232     delete m_program;
233   }
234
235   m_program = new QGLShaderProgram(this);
236
237   if (!m_program->addShaderFromSourceCode(QGLShader::Vertex, VERTEX_SHADER)) {
238     qCritical() << "Failed to add vertex shader";
239     return;
240   }
241
242   if (!m_program->addShaderFromSourceCode(QGLShader::Fragment, FRAGMENT_SHADER)) {
243     qCritical() << "Failed to add fragment shader";
244     return;
245   }
246
247   m_program->bindAttributeLocation("inputVertex", 0);
248   m_program->bindAttributeLocation("textureCoord", 1);
249
250   if (!m_program->link()) {
251     qCritical() << "Failed to link program!";
252     return;
253   }
254
255   if (!m_program->bind()) {
256     qCritical() << "Failed to bind program";
257     return;
258   }
259
260   m_program->setUniformValue("texture0", 0); // texture UNIT 0
261   m_program->release();
262 }
263
264 void QtCamViewfinderRendererMeeGo::paintFrame(QPainter *painter, int frame) {
265   EGLSyncKHR sync = 0;
266
267   if (frame == -1) {
268     return;
269   }
270
271   MeegoGstVideoTexture *sink = MEEGO_GST_VIDEO_TEXTURE(m_sink);
272   if (!meego_gst_video_texture_acquire_frame(sink, frame)) {
273     qDebug() << "Failed to acquire frame";
274     return;
275   }
276
277   m_program->bind();
278
279   m_program->setUniformValue("matrix", m_projectionMatrix);
280   m_program->setUniformValue("matrixWorld", QMatrix4x4(painter->combinedTransform()));
281
282   if (!meego_gst_video_texture_bind_frame(sink, GL_TEXTURE_EXTERNAL_OES, frame)) {
283     qDebug() << "Failed to bind frame";
284     m_program->release();
285     return;
286   }
287
288   glEnableVertexAttribArray(0);
289   glEnableVertexAttribArray(1);
290
291   glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, &m_vertexCoords);
292   glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, &m_texCoords);
293
294   glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
295
296   if (!meego_gst_video_texture_bind_frame(sink, GL_TEXTURE_EXTERNAL_OES, -1)) {
297     qDebug() << "Failed to unbind frame";
298   }
299
300   glDisableVertexAttribArray(1);
301   glDisableVertexAttribArray(0);
302
303   m_program->release();
304
305   if (eglCreateSyncKHR) {
306     sync = eglCreateSyncKHR(m_dpy, EGL_SYNC_FENCE_KHR, NULL);
307   }
308
309   meego_gst_video_texture_release_frame(sink, frame, sync);
310 }
311
312 void QtCamViewfinderRendererMeeGo::calculateCoords() {
313   if (!m_size.isValid() || !m_videoSize.isValid()) {
314     return;
315   }
316
317   QRectF area = renderArea();
318
319   qreal leftMargin = area.x();
320   qreal topMargin = area.y();
321   QSizeF renderSize = area.size();
322
323   m_vertexCoords[0] = leftMargin;
324   m_vertexCoords[1] = topMargin + renderSize.height();
325
326   m_vertexCoords[2] = renderSize.width() + leftMargin;
327   m_vertexCoords[3] = topMargin + renderSize.height();
328
329   m_vertexCoords[4] = renderSize.width() + leftMargin;
330   m_vertexCoords[5] = topMargin;
331
332   m_vertexCoords[6] = leftMargin;
333   m_vertexCoords[7] = topMargin;
334 }
335
336 QRectF QtCamViewfinderRendererMeeGo::renderArea() {
337   if (!m_renderArea.isNull()) {
338     return m_renderArea;
339   }
340
341   QSizeF renderSize = m_videoSize;
342   renderSize.scale(m_size, Qt::KeepAspectRatio);
343
344   qreal leftMargin = (m_size.width() - renderSize.width())/2.0;
345   qreal topMargin = (m_size.height() - renderSize.height())/2.0;
346
347   m_renderArea = QRectF(QPointF(leftMargin, topMargin), renderSize);
348
349   return m_renderArea;
350 }
351
352 QSizeF QtCamViewfinderRendererMeeGo::videoResolution() {
353   return m_videoSize;
354 }
355
356 void QtCamViewfinderRendererMeeGo::setVideoSize(const QSizeF& size) {
357   if (size == m_videoSize) {
358     return;
359   }
360
361   m_videoSize = size;
362
363   m_renderArea = QRectF();
364
365   calculateCoords();
366
367   m_needsInit = true;
368
369   emit renderAreaChanged();
370   emit videoResolutionChanged();
371 }