glwidget.cpp 59.3 KB
Newer Older
1
/*
2
3
4
 * Copyright (c) 2011-2016 Meltytech, LLC
 * Original author: Dan Dennedy <dan@dennedy.org>
 * Modified for Kdenlive: Jean-Baptiste Mardelle
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
 *
 * GL shader based on BSD licensed code from Peter Bengtsson:
 * http://www.fourcc.org/source/YUV420P-OpenGL-GLSLang.c
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

23
#include <KDeclarative/KDeclarative>
24
#include <KMessageBox>
Laurent Montel's avatar
Laurent Montel committed
25
#include <QApplication>
Nicolas Carion's avatar
Nicolas Carion committed
26
#include <QOpenGLFunctions_3_2_Core>
Laurent Montel's avatar
Laurent Montel committed
27
#include <QPainter>
28
#include <QQmlContext>
Nicolas Carion's avatar
style    
Nicolas Carion committed
29
#include <QQuickItem>
30
#include <kdeclarative_version.h>
31
#include <klocalizedstring.h>
32
33

#include "core.h"
Nicolas Carion's avatar
Nicolas Carion committed
34
#include "glwidget.h"
35
#include "kdenlivesettings.h"
36
#include "monitorproxy.h"
Nicolas Carion's avatar
linting    
Nicolas Carion committed
37
#include "profiles/profilemodel.hpp"
Nicolas Carion's avatar
style    
Nicolas Carion committed
38
#include "timeline2/view/qml/timelineitems.h"
Nicolas Carion's avatar
Nicolas Carion committed
39
#include <mlt++/Mlt.h>
40

41
#ifndef GL_UNPACK_ROW_LENGTH
Nicolas Carion's avatar
Nicolas Carion committed
42
43
44
45
46
#ifdef GL_UNPACK_ROW_LENGTH_EXT
#define GL_UNPACK_ROW_LENGTH GL_UNPACK_ROW_LENGTH_EXT
#else
#error GL_UNPACK_ROW_LENGTH undefined
#endif
47
48
#endif

49
50
#if 1
#define check_error(fn) {}
51
#else
52
#define check_error(fn) { int err = fn->glGetError(); if (err != GL_NO_ERROR) { qCritical(KDENLIVE_LOG) << "GL error"  << hex << err << dec << "at" << __FILE__ << ":" << __LINE__; } }
53
#endif
54

55

56
57
58
59
60
61
#ifndef GL_TIMEOUT_IGNORED
#define GL_TIMEOUT_IGNORED 0xFFFFFFFFFFFFFFFFull
#endif

using namespace Mlt;

62
GLWidget::GLWidget(int id, QObject *parent)
63
64
65
66
67
    : QQuickView((QWindow *)parent)
    , sendFrameForAnalysis(false)
    , m_glslManager(nullptr)
    , m_consumer(nullptr)
    , m_producer(nullptr)
Vincent Pinon's avatar
Vincent Pinon committed
68
    , m_id(id)
Vincent Pinon's avatar
Vincent Pinon committed
69
    , m_rulerHeight(QFontMetrics(QApplication::font()).lineSpacing() * 0.7)
70
    , m_bgColor(KdenliveSettings::window_background())
Vincent Pinon's avatar
Vincent Pinon committed
71
    , m_shader(nullptr)
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
    , m_initSem(0)
    , m_analyseSem(1)
    , m_isInitialized(false)
    , m_threadStartEvent(nullptr)
    , m_threadStopEvent(nullptr)
    , m_threadCreateEvent(nullptr)
    , m_threadJoinEvent(nullptr)
    , m_displayEvent(nullptr)
    , m_frameRenderer(nullptr)
    , m_projectionLocation(0)
    , m_modelViewLocation(0)
    , m_vertexLocation(0)
    , m_texCoordLocation(0)
    , m_colorspaceLocation(0)
    , m_zoom(1.0f)
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
87
88
89
    , m_profileSize(1920, 1080)
    , m_colorSpace(601)
    , m_dar(1.78)
90
    , m_sendFrame(false)
91
92
    , m_isZoneMode(false)
    , m_isLoopMode(false)
93
94
    , m_offset(QPoint(0, 0))
    , m_fbo(nullptr)
95
    , m_shareContext(nullptr)
96
97
    , m_openGLSync(false)
    , m_ClientWaitSync(nullptr)
98
{
99
100
101
102
103
    KDeclarative::KDeclarative kdeclarative;
    kdeclarative.setDeclarativeEngine(engine());
    kdeclarative.setupEngine(engine());
    kdeclarative.setupContext();

104
105
106
107
108
109
110
111
    m_texture[0] = m_texture[1] = m_texture[2] = 0;
    qRegisterMetaType<Mlt::Frame>("Mlt::Frame");
    qRegisterMetaType<SharedFrame>("SharedFrame");

    setPersistentOpenGLContext(true);
    setPersistentSceneGraph(true);
    setClearBeforeRendering(false);
    setResizeMode(QQuickView::SizeRootObjectToView);
112

113
114
115
    m_offscreenSurface.setFormat(QWindow::format());
    m_offscreenSurface.create();

116
117
    m_refreshTimer.setSingleShot(true);
    m_refreshTimer.setInterval(50);
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
118
    m_blackClip.reset(new Mlt::Producer(pCore->getCurrentProfile()->profile(), "color:0"));
119
    m_blackClip->set("kdenlive:id", "black");
120
    m_blackClip->set("out", 3);
121
    connect(&m_refreshTimer, &QTimer::timeout, this, &GLWidget::refresh);
122
    m_producer = m_blackClip;
123
    rootContext()->setContextProperty("markersModel", 0);
124
125
    if (!initGPUAccel()) {
        disableGPUAccel();
Laurent Montel's avatar
Laurent Montel committed
126
    }
Corey O'Connor's avatar
Corey O'Connor committed
127

Laurent Montel's avatar
Laurent Montel committed
128
129
    connect(this, &QQuickWindow::sceneGraphInitialized, this, &GLWidget::initializeGL, Qt::DirectConnection);
    connect(this, &QQuickWindow::beforeRendering, this, &GLWidget::paintGL, Qt::DirectConnection);
Corey O'Connor's avatar
Corey O'Connor committed
130

131
132
    registerTimelineItems();
    m_proxy = new MonitorProxy(this);
133
    connect(m_proxy, &MonitorProxy::seekRequestChanged, this, &GLWidget::requestSeek);
134
    rootContext()->setContextProperty("controller", m_proxy);
135
136
137
138
}

GLWidget::~GLWidget()
{
139
    // C & D
140
141
142
143
144
    delete m_glslManager;
    delete m_threadStartEvent;
    delete m_threadStopEvent;
    delete m_threadCreateEvent;
    delete m_threadJoinEvent;
145
    delete m_displayEvent;
146
147
148
149
150
151
152
153
154
    if (m_frameRenderer) {
        if (m_frameRenderer->isRunning()) {
            QMetaObject::invokeMethod(m_frameRenderer, "cleanup");
            m_frameRenderer->quit();
            m_frameRenderer->wait();
            m_frameRenderer->deleteLater();
        } else {
            delete m_frameRenderer;
        }
155
    }
156
    m_blackClip.reset();
157
    delete m_shareContext;
158
    delete m_shader;
159
    // delete pCore->getCurrentProfile();
160
161
}

162
void GLWidget::updateAudioForAnalysis()
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
163
{
Laurent Montel's avatar
Laurent Montel committed
164
165
166
    if (m_frameRenderer) {
        m_frameRenderer->sendAudioForAnalysis = KdenliveSettings::monitor_audio();
    }
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
167
168
}

169
void GLWidget::initializeGL()
170
{
171
    if (m_isInitialized) return;
172

173
    openglContext()->makeCurrent(&m_offscreenSurface);
174
    initializeOpenGLFunctions();
175

Nicolas Carion's avatar
Nicolas Carion committed
176
177
    qCDebug(KDENLIVE_LOG) << "OpenGL vendor: " << QString::fromUtf8((const char *)glGetString(GL_VENDOR));
    qCDebug(KDENLIVE_LOG) << "OpenGL renderer: " << QString::fromUtf8((const char *)glGetString(GL_RENDERER));
Laurent Montel's avatar
Laurent Montel committed
178
179
180
    qCDebug(KDENLIVE_LOG) << "OpenGL Threaded: " << openglContext()->supportsThreadedOpenGL();
    qCDebug(KDENLIVE_LOG) << "OpenGL ARG_SYNC: " << openglContext()->hasExtension("GL_ARB_sync");
    qCDebug(KDENLIVE_LOG) << "OpenGL OpenGLES: " << openglContext()->isOpenGLES();
181

182
    // C & D
183
    if (onlyGLESGPUAccel()) {
184
        disableGPUAccel();
185
    }
186

187
188
    createShader();

189
    m_openGLSync = initGPUAccelSync();
190

191
    // C & D
192
193
194
195
196
    if (m_glslManager) {
        // Create a context sharing with this context for the RenderThread context.
        // This is needed because openglContext() is active in another thread
        // at the time that RenderThread is created.
        // See this Qt bug for more info: https://bugreports.qt.io/browse/QTBUG-44677
Corey O'Connor's avatar
Corey O'Connor committed
197
        // TODO: QTBUG-44677 is closed. still applicable?
198
199
200
201
202
        m_shareContext = new QOpenGLContext;
        m_shareContext->setFormat(openglContext()->format());
        m_shareContext->setShareContext(openglContext());
        m_shareContext->create();
    }
203

204
    m_frameRenderer = new FrameRenderer(openglContext(), &m_offscreenSurface, m_ClientWaitSync);
205

206
    m_frameRenderer->sendAudioForAnalysis = KdenliveSettings::monitor_audio();
Corey O'Connor's avatar
Corey O'Connor committed
207

208
    openglContext()->makeCurrent(this);
209
210
    connect(m_frameRenderer, &FrameRenderer::textureReady, this, &GLWidget::updateTexture, Qt::DirectConnection);
    connect(m_frameRenderer, &FrameRenderer::frameDisplayed, this, &GLWidget::onFrameDisplayed, Qt::QueuedConnection);
211
    connect(m_frameRenderer, &FrameRenderer::frameDisplayed, this, &GLWidget::frameDisplayed, Qt::QueuedConnection);
212
    m_initSem.release();
213
    m_isInitialized = true;
214
    reconfigure();
215
216
217
218
219
}

void GLWidget::resizeGL(int width, int height)
{
    int x, y, w, h;
220
    height -= m_rulerHeight;
Nicolas Carion's avatar
Nicolas Carion committed
221
    double this_aspect = (double)width / height;
222

223
    // Special case optimization to negate odd effect of sample aspect ratio
224
    // not corresponding exactly with image resolution.
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
225
    if ((int)(this_aspect * 1000) == (int)(m_dar * 1000)) {
226
227
228
229
        w = width;
        h = height;
    }
    // Use OpenGL to normalise sample aspect ratio
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
230
    else if (height * m_dar > width) {
231
        w = width;
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
232
        h = width / m_dar;
Laurent Montel's avatar
Laurent Montel committed
233
    } else {
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
234
        w = height * m_dar;
235
236
237
238
239
        h = height;
    }
    x = (width - w) / 2;
    y = (height - h) / 2;
    m_rect.setRect(x, y, w, h);
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
240
    double scalex = (double)m_rect.width() / m_profileSize.width() * m_zoom;
241
    double scaley = (double)m_rect.width() /
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
242
243
                    ((double)m_profileSize.height() * m_dar / m_profileSize.width()) /
                    m_profileSize.width() * m_zoom;
244
    QPoint center = m_rect.center();
Laurent Montel's avatar
Laurent Montel committed
245
    QQuickItem *rootQml = rootObject();
246
247
    if (rootQml) {
        rootQml->setProperty("center", center);
248
249
        rootQml->setProperty("scalex", scalex);
        rootQml->setProperty("scaley", scaley);
250
        if (rootQml->objectName() == QLatin1String("rootsplit")) {
251
252
253
            // Adjust splitter pos
            rootQml->setProperty("splitterPos", x + (rootQml->property("realpercent").toDouble() * w));
        }
254
    }
255
256
257
    emit rectChanged();
}

Laurent Montel's avatar
Laurent Montel committed
258
void GLWidget::resizeEvent(QResizeEvent *event)
259
260
{
    resizeGL(event->size().width(), event->size().height());
261
    QQuickView::resizeEvent(event);
262
263
}

264
265
266
267
268
269
270
271
272
273
274
void GLWidget::createGPUAccelFragmentProg()
{
    m_shader->addShaderFromSourceCode(QOpenGLShader::Fragment, "uniform sampler2D tex;"
                                                               "varying highp vec2 coordinates;"
                                                               "void main(void) {"
                                                               "  gl_FragColor = texture2D(tex, coordinates);"
                                                               "}");
    m_shader->link();
    m_textureLocation[0] = m_shader->uniformLocation("tex");
}

275
276
277
void GLWidget::createShader()
{
    m_shader = new QOpenGLShaderProgram;
Nicolas Carion's avatar
Nicolas Carion committed
278
279
280
281
282
283
284
285
286
    m_shader->addShaderFromSourceCode(QOpenGLShader::Vertex, "uniform highp mat4 projection;"
                                                             "uniform highp mat4 modelView;"
                                                             "attribute highp vec4 vertex;"
                                                             "attribute highp vec2 texCoord;"
                                                             "varying highp vec2 coordinates;"
                                                             "void main(void) {"
                                                             "  gl_Position = projection * modelView * vertex;"
                                                             "  coordinates = texCoord;"
                                                             "}");
287
    // C & D
288
    if (m_glslManager) {
289
        createGPUAccelFragmentProg();
290
    } else {
291
        // A & B
292
        createYUVTextureProjectFragmentProg();
293
    }
294

295
296
297
298
299
300
    m_projectionLocation = m_shader->uniformLocation("projection");
    m_modelViewLocation = m_shader->uniformLocation("modelView");
    m_vertexLocation = m_shader->attributeLocation("vertex");
    m_texCoordLocation = m_shader->attributeLocation("texCoord");
}

301
302
303
void GLWidget::createYUVTextureProjectFragmentProg()
{
    m_shader->addShaderFromSourceCode(QOpenGLShader::Fragment,
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
                                      "uniform sampler2D Ytex, Utex, Vtex;"
                                      "uniform lowp int colorspace;"
                                      "varying highp vec2 coordinates;"
                                      "void main(void) {"
                                      "  mediump vec3 texel;"
                                      "  texel.r = texture2D(Ytex, coordinates).r - 0.0625;" // Y
                                      "  texel.g = texture2D(Utex, coordinates).r - 0.5;"    // U
                                      "  texel.b = texture2D(Vtex, coordinates).r - 0.5;"    // V
                                      "  mediump mat3 coefficients;"
                                      "  if (colorspace == 601) {"
                                      "    coefficients = mat3("
                                      "      1.1643,  1.1643,  1.1643," // column 1
                                      "      0.0,    -0.39173, 2.017,"  // column 2
                                      "      1.5958, -0.8129,  0.0);"   // column 3
                                      "  } else {"                      // ITU-R 709
                                      "    coefficients = mat3("
                                      "      1.1643, 1.1643, 1.1643," // column 1
                                      "      0.0,   -0.213,  2.112,"  // column 2
                                      "      1.793, -0.533,  0.0);"   // column 3
                                      "  }"
                                      "  gl_FragColor = vec4(coefficients * texel, 1.0);"
                                      "}");
326
327
328
329
330
331
332
    m_shader->link();
    m_textureLocation[0] = m_shader->uniformLocation("Ytex");
    m_textureLocation[1] = m_shader->uniformLocation("Utex");
    m_textureLocation[2] = m_shader->uniformLocation("Vtex");
    m_colorspaceLocation = m_shader->uniformLocation("colorspace");
}

333
static void uploadTextures(QOpenGLContext *context, const SharedFrame &frame, GLuint texture[])
334
335
336
{
    int width = frame.get_image_width();
    int height = frame.get_image_height();
Laurent Montel's avatar
Laurent Montel committed
337
338
    const uint8_t *image = frame.get_image();
    QOpenGLFunctions *f = context->functions();
339

340
341
342
    // The planes of pixel data may not be a multiple of the default 4 bytes.
    f->glPixelStorei(GL_UNPACK_ALIGNMENT, 1);

343
    // Upload each plane of YUV to a texture.
Nicolas Carion's avatar
Nicolas Carion committed
344
    if (texture[0] != 0u) {
345
        f->glDeleteTextures(3, texture);
Laurent Montel's avatar
Laurent Montel committed
346
    }
347
348
349
350
    check_error(f);
    f->glGenTextures(3, texture);
    check_error(f);

Laurent Montel's avatar
Laurent Montel committed
351
    f->glBindTexture(GL_TEXTURE_2D, texture[0]);
352
353
354
355
356
357
358
359
360
    check_error(f);
    f->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    check_error(f);
    f->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    check_error(f);
    f->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    check_error(f);
    f->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
    check_error(f);
Nicolas Carion's avatar
Nicolas Carion committed
361
    f->glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, width, height, 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, image);
362
363
    check_error(f);

Laurent Montel's avatar
Laurent Montel committed
364
    f->glBindTexture(GL_TEXTURE_2D, texture[1]);
365
366
367
368
369
370
371
372
373
    check_error(f);
    f->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    check_error(f);
    f->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    check_error(f);
    f->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    check_error(f);
    f->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
    check_error(f);
Nicolas Carion's avatar
Nicolas Carion committed
374
    f->glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, width / 2, height / 2, 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, image + width * height);
375
376
    check_error(f);

Laurent Montel's avatar
Laurent Montel committed
377
    f->glBindTexture(GL_TEXTURE_2D, texture[2]);
378
379
380
381
382
383
384
385
386
    check_error(f);
    f->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    check_error(f);
    f->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    check_error(f);
    f->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    check_error(f);
    f->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
    check_error(f);
Nicolas Carion's avatar
Nicolas Carion committed
387
    f->glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, width / 2, height / 2, 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, image + width * height + width / 2 * height / 2);
388
389
390
    check_error(f);
}

391
392
393
394
395
396
void GLWidget::clear()
{
    stopGlsl();
    update();
}

397
398
399
400
401
void GLWidget::releaseAnalyse()
{
    m_analyseSem.release();
}

402
403
bool GLWidget::acquireSharedFrameTextures()
{
404
    // A
405
    if ((m_glslManager == nullptr) && !openglContext()->supportsThreadedOpenGL()) {
406
        QMutexLocker locker(&m_contextSharedAccess);
407
        if (!m_sharedFrame.is_valid()) {
Corey O'Connor's avatar
Corey O'Connor committed
408
            return false;
409
410
        }
        uploadTextures(openglContext(), m_sharedFrame, m_texture);
411
    } else if (m_glslManager) {
412
        // C & D
413
        m_contextSharedAccess.lock();
414
        if (m_sharedFrame.is_valid()) {
Vincent Pinon's avatar
Vincent Pinon committed
415
            m_texture[0] = *((const GLuint *)m_sharedFrame.get_image());
416
417
418
419
        }
    }

    if (!m_texture[0]) {
420
        // C & D
421
        if (m_glslManager) m_contextSharedAccess.unlock();
Corey O'Connor's avatar
Corey O'Connor committed
422
        return false;
423
    }
424

Corey O'Connor's avatar
Corey O'Connor committed
425
426
427
    return true;
}

428
429
void GLWidget::bindShaderProgram()
{
430
    m_shader->bind();
Corey O'Connor's avatar
Corey O'Connor committed
431

432
    // C & D
433
434
435
    if (m_glslManager) {
        m_shader->setUniformValue(m_textureLocation[0], 0);
    } else {
436
        // A & B
437
438
439
        m_shader->setUniformValue(m_textureLocation[0], 0);
        m_shader->setUniformValue(m_textureLocation[1], 1);
        m_shader->setUniformValue(m_textureLocation[2], 2);
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
440
        m_shader->setUniformValue(m_colorspaceLocation, m_colorSpace);
441
    }
Corey O'Connor's avatar
Corey O'Connor committed
442
443
}

444
445
void GLWidget::releaseSharedFrameTextures()
{
446
    // C & D
Corey O'Connor's avatar
Corey O'Connor committed
447
448
    if (m_glslManager) {
        glFinish();
449
        m_contextSharedAccess.unlock();
Corey O'Connor's avatar
Corey O'Connor committed
450
451
452
    }
}

453
454
bool GLWidget::initGPUAccel()
{
455
456
    if (!KdenliveSettings::gpu_accel()) return false;

457
    m_glslManager = new Mlt::Filter(pCore->getCurrentProfile()->profile(), "glsl.manager");
458
459
460
    return m_glslManager->is_valid();
}

461
462
// C & D
// TODO: insure safe, idempotent on all pipelines.
463
464
void GLWidget::disableGPUAccel()
{
465
466
467
468
469
470
471
472
    delete m_glslManager;
    m_glslManager = nullptr;
    KdenliveSettings::setGpu_accel(false);
    // Need to destroy MLT global reference to prevent filters from trying to use GPU.
    mlt_properties_set_data(mlt_global_properties(), "glslManager", nullptr, 0, nullptr, nullptr);
    emit gpuNotSupported();
}

473
474
bool GLWidget::onlyGLESGPUAccel() const
{
475
476
477
478
    return (m_glslManager != nullptr) && openglContext()->isOpenGLES();
}

#if defined(Q_OS_WIN)
479
480
bool GLWidget::initGPUAccelSync()
{
481
482
483
484
485
    // no-op
    // TODO: getProcAddress is not working on Windows?
    return false;
}
#else
486
487
bool GLWidget::initGPUAccelSync()
{
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
    if (!KdenliveSettings::gpu_accel()) return false;
    if (m_glslManager == nullptr) return false;
    if (!openglContext()->hasExtension("GL_ARB_sync")) return false;

    m_ClientWaitSync = (ClientWaitSync_fp)openglContext()->getProcAddress("glClientWaitSync");
    if (m_ClientWaitSync) {
        return true;
    } else {
        qCDebug(KDENLIVE_LOG) << "  / / // NO GL SYNC, ERROR";
        // fallback on A || B
        // TODO: fallback on A || B || C?
        disableGPUAccel();
        return false;
    }
}
#endif

Corey O'Connor's avatar
Corey O'Connor committed
505
506
507
508
509
510
511
512
513
514
515
void GLWidget::paintGL()
{
    QOpenGLFunctions *f = openglContext()->functions();
    int width = this->width() * devicePixelRatio();
    int height = this->height() * devicePixelRatio();

    f->glDisable(GL_BLEND);
    f->glDisable(GL_DEPTH_TEST);
    f->glDepthMask(GL_FALSE);
    f->glViewport(0, (m_rulerHeight * devicePixelRatio() * 0.5 + 0.5), width, height);
    check_error(f);
516
    f->glClearColor(m_bgColor.redF(), m_bgColor.greenF(), m_bgColor.blueF(), 0);
Corey O'Connor's avatar
Corey O'Connor committed
517
518
519
    f->glClear(GL_COLOR_BUFFER_BIT);
    check_error(f);

520
    if (!acquireSharedFrameTextures()) return;
Corey O'Connor's avatar
Corey O'Connor committed
521
522
523
524
525
526
527
528
529
530
531

    // Bind textures.
    for (uint i = 0; i < 3; ++i) {
        if (m_texture[i] != 0u) {
            f->glActiveTexture(GL_TEXTURE0 + i);
            f->glBindTexture(GL_TEXTURE_2D, m_texture[i]);
            check_error(f);
        }
    }

    bindShaderProgram();
532
    check_error(f);
533
534
535

    // Setup an orthographic projection.
    QMatrix4x4 projection;
Vincent Pinon's avatar
Vincent Pinon committed
536
    projection.scale(2.0f / (float)width, 2.0f / (float)height);
537
    m_shader->setUniformValue(m_projectionLocation, projection);
538
    check_error(f);
539
540
541

    // Set model view.
    QMatrix4x4 modelView;
542
    if (!qFuzzyCompare(m_zoom, 1.0f)) {
Nicolas Carion's avatar
Nicolas Carion committed
543
        if ((offset().x() != 0) || (offset().y() != 0)) modelView.translate(-offset().x() * devicePixelRatio(), offset().y() * devicePixelRatio());
544
545
546
        modelView.scale(zoom(), zoom());
    }
    m_shader->setUniformValue(m_modelViewLocation, modelView);
547
    check_error(f);
548
549
550
551
552

    // Provide vertices of triangle strip.
    QVector<QVector2D> vertices;
    width = m_rect.width() * devicePixelRatio();
    height = m_rect.height() * devicePixelRatio();
553
554
555
556
    vertices << QVector2D(float(-width) / 2.0f, float(-height) / 2.0f);
    vertices << QVector2D(float(-width) / 2.0f, float(height) / 2.0f);
    vertices << QVector2D(float(width) / 2.0f, float(-height) / 2.0f);
    vertices << QVector2D(float(width) / 2.0f, float(height) / 2.0f);
557
    m_shader->enableAttributeArray(m_vertexLocation);
558
    check_error(f);
559
    m_shader->setAttributeArray(m_vertexLocation, vertices.constData());
560
    check_error(f);
561
562
563
564
565
566
567
568

    // Provide texture coordinates.
    QVector<QVector2D> texCoord;
    texCoord << QVector2D(0.0f, 1.0f);
    texCoord << QVector2D(0.0f, 0.0f);
    texCoord << QVector2D(1.0f, 1.0f);
    texCoord << QVector2D(1.0f, 0.0f);
    m_shader->enableAttributeArray(m_texCoordLocation);
569
    check_error(f);
570
    m_shader->setAttributeArray(m_texCoordLocation, texCoord.constData());
571
    check_error(f);
572
573

    // Render
574
    glDrawArrays(GL_TRIANGLE_STRIP, 0, vertices.size());
575
    check_error(f);
576

577
    if (m_sendFrame && m_analyseSem.tryAcquire(1)) {
578
        // Render RGB frame for analysis
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
579
        if ((m_fbo == nullptr) || m_fbo->size() != m_profileSize) {
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
580
            delete m_fbo;
581
            QOpenGLFramebufferObjectFormat fmt;
582
            fmt.setSamples(1);
Nicolas Carion's avatar
Nicolas Carion committed
583
            fmt.setInternalTextureFormat(GL_RGB);                             // GL_RGBA32F);  // which one is the fastest ?
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
584
            m_fbo = new QOpenGLFramebufferObject(m_profileSize.height(), m_profileSize.height(), fmt); // GL_TEXTURE_2D);
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
585
586
        }
        m_fbo->bind();
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
587
        glViewport(0, 0, m_profileSize.width(), m_profileSize.height());
588
589

        QMatrix4x4 projection2;
Vincent Pinon's avatar
Vincent Pinon committed
590
        projection2.scale(2.0f / (float)width, 2.0f / (float)height);
591
592
        m_shader->setUniformValue(m_projectionLocation, projection2);

593
        glDrawArrays(GL_TRIANGLE_STRIP, 0, vertices.size());
594
        check_error(f);
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
595
596
        m_fbo->release();
        emit analyseFrame(m_fbo->toImage());
597
        m_sendFrame = false;
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
598
    }
599
600
601
602
    // Cleanup
    m_shader->disableAttributeArray(m_vertexLocation);
    m_shader->disableAttributeArray(m_texCoordLocation);
    m_shader->release();
603
    for (uint i = 0; i < 3; ++i) {
Nicolas Carion's avatar
Nicolas Carion committed
604
        if (m_texture[i] != 0u) {
605
606
            f->glActiveTexture(GL_TEXTURE0 + i);
            f->glBindTexture(GL_TEXTURE_2D, 0);
607
            check_error(f);
608
609
        }
    }
610
    glActiveTexture(GL_TEXTURE0);
611
    check_error(f);
Corey O'Connor's avatar
Corey O'Connor committed
612
613
614

    releaseSharedFrameTextures();
    check_error(f);
615
616
}

617
void GLWidget::slotZoom(bool zoomIn)
618
{
619
620
    if (zoomIn) {
        if (qFuzzyCompare(m_zoom, 1.0f)) {
Nicolas Carion's avatar
Nicolas Carion committed
621
622
623
624
625
626
            setZoom(2.0f);
        } else if (qFuzzyCompare(m_zoom, 2.0f)) {
            setZoom(3.0f);
        } else if (m_zoom < 1.0f) {
            setZoom(m_zoom * 2);
        }
627
628
629
630
631
632
633
    } else {
        if (qFuzzyCompare(m_zoom, 3.0f)) {
            setZoom(2.0);
        } else if (qFuzzyCompare(m_zoom, 2.0f)) {
            setZoom(1.0);
        } else if (m_zoom > 0.2) {
            setZoom(m_zoom / 2);
634
        }
635
636
637
638
639
640
641
    }
}

void GLWidget::wheelEvent(QWheelEvent *event)
{
    if (((event->modifiers() & Qt::ControlModifier) != 0u) && ((event->modifiers() & Qt::ShiftModifier) != 0u)) {
        slotZoom(event->delta() > 0);
642
643
        return;
    }
644
    emit mouseSeek(event->delta(), (uint)event->modifiers());
645
646
647
    event->accept();
}

648
649
void GLWidget::requestSeek()
{
650
    if (!m_producer) {
651
652
        return;
    }
653
    if (m_proxy->seeking()) {
654
        m_producer->seek(m_proxy->seekPosition());
655
656
657
        if (!qFuzzyIsNull(m_producer->get_speed())) {
            m_consumer->purge();
        }
658
659
660
        if (m_consumer->is_stopped()) {
            m_consumer->start();
        }
661
        m_consumer->set("refresh", 1);
662
663
664
    }
}

665
666
void GLWidget::seek(int pos)
{
667
    if (!m_proxy->seeking()) {
668
669
670
        m_producer->seek(pos);
        if (m_consumer->is_stopped()) {
            m_consumer->start();
671
672
673
        } else {
            m_consumer->purge();
            m_consumer->set("refresh", 1);
674
675
        }
    }
676
    m_proxy->setSeekPosition(pos);
677
678
}

679
680
void GLWidget::requestRefresh()
{
681
682
683
    if (m_proxy->seeking()) {
        return;
    }
684
    if (m_producer && qFuzzyIsNull(m_producer->get_speed())) {
685
686
687
688
        m_refreshTimer.start();
    }
}

689
690
691
692
693
QString GLWidget::frameToTime(int frames) const
{
    return m_consumer ? m_consumer->frames_to_time(frames, mlt_time_smpte_df) : QStringLiteral("-");
}

694
695
696
void GLWidget::refresh()
{
    m_refreshTimer.stop();
697
698
699
    if (m_proxy->seeking()) {
        return;
    }
700
    QMutexLocker locker(&m_mltMutex);
701
702
703
704
705
706
    if (m_consumer->is_stopped()) {
        m_consumer->start();
    }
    m_consumer->set("refresh", 1);
}

707
bool GLWidget::checkFrameNumber(int pos, int offset)
708
{
709
    emit consumerPosition(pos);
710
711
    if (!m_proxy->setPosition(pos)) {
        emit seekPosition(m_proxy->seekOrCurrentPosition());
712
713
    }
    const double speed = m_producer->get_speed();
714
    if (m_proxy->seeking()) {
715
        m_producer->set_speed(0);
716
        m_producer->seek(m_proxy->seekPosition());
717
        if (qFuzzyIsNull(speed)) {
718
719
720
721
            m_consumer->set("refresh", 1);
        } else {
            m_producer->set_speed(speed);
        }
722
723
        return true;
    }
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
724
725
726
727
728
729
    int maxPos = m_producer->get_int("out");
    if (m_isLoopMode || m_isZoneMode) {
        if (qFuzzyIsNull(speed) && pos >= maxPos) {
            m_consumer->purge();
            if (!m_isLoopMode) {
                return false;
730
            }
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
731
732
733
734
            m_producer->seek(m_proxy->zoneIn());
            m_producer->set_speed(1.0);
            m_consumer->set("refresh", 1);
            return true;
735
        }
736
        return true;
737
    } else if (!qFuzzyIsNull(speed)) {
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
738
        maxPos -= offset;
739
740
        if (pos >= (maxPos - 1) && speed > 0.) {
            // Playing past last clip, pause
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
741
742
743
744
            m_producer->set_speed(0);
            m_consumer->set("refresh", 0);
            m_consumer->purge();
            m_producer->seek(qMax(0, maxPos));
745
746
747
748
749
750
751
            return false;
        } else if (pos <= 0 && speed < 0.) {
            // rewinding reached 0, pause
            m_producer->set_speed(0);
            m_consumer->set("refresh", 0);
            m_consumer->purge();
            m_producer->seek(0);
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
752
753
            return false;
        }
754
755
756
757
    }
    return true;
}

Laurent Montel's avatar
Laurent Montel committed
758
void GLWidget::mousePressEvent(QMouseEvent *event)
759
{
Nicolas Carion's avatar
Nicolas Carion committed
760
761
    if ((rootObject() != nullptr) && rootObject()->objectName() != QLatin1String("root") && !(event->modifiers() & Qt::ControlModifier) &&
        !(event->buttons() & Qt::MiddleButton)) {
762
        event->ignore();
763
        QQuickView::mousePressEvent(event);
764
765
        return;
    }
Nicolas Carion's avatar
Nicolas Carion committed
766
    if ((event->button() & Qt::LeftButton) != 0u) {
767
        if ((event->modifiers() & Qt::ControlModifier) != 0u) {
768
769
770
771
772
773
            // Pan view
            m_panStart = event->pos();
            setCursor(Qt::ClosedHandCursor);
        } else {
            m_dragStart = event->pos();
        }
Nicolas Carion's avatar
Nicolas Carion committed
774
    } else if ((event->button() & Qt::RightButton) != 0u) {
775
        emit showContextMenu(event->globalPos());
Nicolas Carion's avatar
Nicolas Carion committed
776
    } else if ((event->button() & Qt::MiddleButton) != 0u) {
777
778
        m_panStart = event->pos();
        setCursor(Qt::ClosedHandCursor);
779
    }
780
    event->accept();
781
    QQuickView::mousePressEvent(event);
782
783
}

Laurent Montel's avatar
Laurent Montel committed
784
void GLWidget::mouseMoveEvent(QMouseEvent *event)
785
{
Nicolas Carion's avatar
Nicolas Carion committed
786
787
    if ((rootObject() != nullptr) && rootObject()->objectName() != QLatin1String("root") && !(event->modifiers() & Qt::ControlModifier) &&
        !(event->buttons() & Qt::MiddleButton)) {
788
        event->ignore();
789
        QQuickView::mouseMoveEvent(event);
790
791
        return;
    }
792
    /*    if (event->modifiers() == Qt::ShiftModifier && m_producer) {
793
794
795
        emit seekTo(m_producer->get_length() *  event->x() / width());
        return;
    }*/
796
797
798
799
800
801
802
803
    QQuickView::mouseMoveEvent(event);
    if (!m_panStart.isNull()) {
        emit panView(m_panStart - event->pos());
        m_panStart = event->pos();
        event->accept();
        QQuickView::mouseMoveEvent(event);
        return;
    }
Laurent Montel's avatar
Laurent Montel committed
804
    if (!(event->buttons() & Qt::LeftButton)) {
805
        QQuickView::mouseMoveEvent(event);
806
        return;
Laurent Montel's avatar
Laurent Montel committed
807
    }
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
808
    if (!event->isAccepted() && !m_dragStart.isNull() && (event->pos() - m_dragStart).manhattanLength() >= QApplication::startDragDistance()) {
809
810
811
        m_dragStart = QPoint();
        emit startDrag();
    }
812
813
}

Laurent Montel's avatar
Laurent Montel committed
814
void GLWidget::keyPressEvent(QKeyEvent *event)
815
{
816
    QQuickView::keyPressEvent(event);
817
818
819
    if (!event->isAccepted()) {
        emit passKeyEvent(event);
    }
820
821
822
823
824
825
}

void GLWidget::createThread(RenderThread **thread, thread_function_t function, void *data)
{
#ifdef Q_OS_WIN
    // On Windows, MLT event consumer-thread-create is fired from the Qt main thread.
Laurent Montel's avatar
Laurent Montel committed
826
    while (!m_isInitialized) {
827
        qApp->processEvents();
Laurent Montel's avatar
Laurent Montel committed
828
    }
829
830
831
832
833
#else
    if (!m_isInitialized) {
        m_initSem.acquire();
    }
#endif
Vincent Pinon's avatar
Vincent Pinon committed
834
    (*thread) = new RenderThread(function, data, m_shareContext, &m_offscreenSurface);
835
836
837
    (*thread)->start();
}

Nicolas Carion's avatar
Nicolas Carion committed
838
static void onThreadCreate(mlt_properties owner, GLWidget *self, RenderThread **thread, int *priority, thread_function_t function, void *data)
839
840
841
{
    Q_UNUSED(owner)
    Q_UNUSED(priority)
Nicolas Carion's avatar
Nicolas Carion committed
842
    // self->clearFrameRenderer();
843
    self->createThread(thread, function, data);
844
    self->lockMonitor();
845
846
}

Laurent Montel's avatar
Laurent Montel committed
847
static void onThreadJoin(mlt_properties owner, GLWidget *self, RenderThread *thread)
848
849
850
851
852
853
{
    Q_UNUSED(owner)
    if (thread) {
        thread->quit();
        thread->wait();
        delete thread;
Nicolas Carion's avatar
Nicolas Carion committed
854
        // self->clearFrameRenderer();
855
        self->releaseMonitor();
856
857
858
859
860
    }
}

void GLWidget::startGlsl()
{
861
    // C & D
862
    if (m_glslManager) {
Nicolas Carion's avatar
Nicolas Carion committed
863
        // clearFrameRenderer();
864
        m_glslManager->fire_event("init glsl");
Nicolas Carion's avatar
Nicolas Carion committed
865
        if (m_glslManager->get_int("glsl_supported") == 0) {
866
            disableGPUAccel();
Laurent Montel's avatar
Laurent Montel committed
867
        } else {
868
869
870
871
872
            emit started();
        }
    }
}

Laurent Montel's avatar
Laurent Montel committed
873
static void onThreadStarted(mlt_properties owner, GLWidget *self)
874
875
876
877
878
{
    Q_UNUSED(owner)
    self->startGlsl();
}

879
880
881
882
883
884
885
886
887
888
void GLWidget::releaseMonitor()
{
    emit lockMonitor(false);
}

void GLWidget::lockMonitor()
{
    emit lockMonitor(true);
}

889
890
void GLWidget::stopGlsl()
{
Laurent Montel's avatar
Laurent Montel committed
891
    if (m_consumer) {
892
        m_consumer->purge();
Laurent Montel's avatar
Laurent Montel committed
893
    }
894

895
    // C & D
Nicolas Carion's avatar
Nicolas Carion committed
896
897
898
899
900
    // TODO This is commented out for now because it is causing crashes.
    // Technically, this should be the correct thing to do, but it appears
    // some changes have created regression (see shotcut)
    // with respect to restarting the consumer in GPU mode.
    // m_glslManager->fire_event("close glsl");
901
902
903
    m_texture[0] = 0;
}

Laurent Montel's avatar
Laurent Montel committed
904
static void onThreadStopped(mlt_properties owner, GLWidget *self)
905
906
907
908
909
{
    Q_UNUSED(owner)
    self->stopGlsl();
}

Nicolas Carion's avatar
Nicolas Carion committed
910
int GLWidget::setProducer(const std::shared_ptr<Mlt::Producer> &producer, bool isActive, int position)
911
{
912
913
914
    int error = 0;
    QString currentId;
    int consumerPosition = 0;
915
    currentId = m_producer->parent().get("kdenlive:id");
916
    if (producer) {
917
918
        m_producer = producer;
    } else {
919
920
921
        if (currentId == QLatin1String("black")) {
            return 0;
        }
922
        m_producer = m_blackClip;
923
924
        // Reset markersModel
        rootContext()->setContextProperty("markersModel", 0);
925
    }
926
    // redundant check. postcondition of above is m_producer != null
Vincent Pinon's avatar
Vincent Pinon committed
927
    if (m_producer) {
928
929
930
        m_producer->set_speed(0);
        if (m_consumer) {
            consumerPosition = m_consumer->position();
931
            m_consumer->stop();
932
933
934
935
            if (!m_consumer->is_stopped()) {
                m_consumer->stop();
            }
        }
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
936
        error = reconfigure();
Nicolas Carion's avatar
Nicolas Carion committed
937
        if (error == 0) {
938
939
940
            // The profile display aspect ratio may have changed.
            resizeGL(width(), height());
        }
Laurent Montel's avatar
Laurent Montel committed
941
942
943
944
945
946
    } else {
        return error;
    }
    if (!m_consumer) {
        return error;
    }
947
    consumerPosition = m_consumer->position();