]> git.sesse.net Git - kdenlive/blob - src/abstractscopewidget.cpp
Vectorscope changes:
[kdenlive] / src / abstractscopewidget.cpp
1 /***************************************************************************
2  *   Copyright (C) 2010 by Simon Andreas Eugster (simon.eu@gmail.com)      *
3  *   This file is part of kdenlive. See www.kdenlive.org.                  *
4  *                                                                         *
5  *   This program is free software; you can redistribute it and/or modify  *
6  *   it under the terms of the GNU General Public License as published by  *
7  *   the Free Software Foundation; either version 2 of the License, or     *
8  *   (at your option) any later version.                                   *
9  ***************************************************************************/
10
11 #include "qtconcurrentrun.h"
12
13 #include "abstractscopewidget.h"
14 #include "renderer.h"
15 #include "monitor.h"
16
17 #include <QFuture>
18 #include <QColor>
19 #include <QDebug>
20 #include <QMenu>
21 #include <QPainter>
22
23 const int REALTIME_FPS = 30;
24
25 const QColor light(250, 238, 226, 255);
26 const QColor dark ( 40,  40,  39, 255);
27 const QColor dark2( 25,  25,  23, 255);
28
29 AbstractScopeWidget::AbstractScopeWidget(Monitor *projMonitor, Monitor *clipMonitor, QWidget *parent) :
30     QWidget(parent),
31     m_projMonitor(projMonitor),
32     m_clipMonitor(clipMonitor),
33     offset(5),
34     m_accelFactorHUD(1),
35     m_accelFactorScope(1),
36     m_accelFactorBackground(1),
37     m_semaphoreHUD(1),
38     m_semaphoreScope(1),
39     m_semaphoreBackground(1),
40     initialDimensionUpdateDone(false)
41
42 {
43     m_scopePalette = QPalette();
44     m_scopePalette.setBrush(QPalette::Window, QBrush(dark2));
45     m_scopePalette.setBrush(QPalette::Base, QBrush(dark));
46     m_scopePalette.setBrush(QPalette::Button, QBrush(dark));
47     m_scopePalette.setBrush(QPalette::Text, QBrush(light));
48     m_scopePalette.setBrush(QPalette::WindowText, QBrush(light));
49     m_scopePalette.setBrush(QPalette::ButtonText, QBrush(light));
50     this->setPalette(m_scopePalette);
51     this->setAutoFillBackground(true);
52
53     m_aAutoRefresh = new QAction(i18n("Auto Refresh"), this);
54     m_aAutoRefresh->setCheckable(true);
55     m_aRealtime = new QAction(i18n("Realtime (with precision loss)"), this);
56     m_aRealtime->setCheckable(true);
57
58     m_menu = new QMenu(this);
59     m_menu->setPalette(m_scopePalette);
60     m_menu->addAction(m_aAutoRefresh);
61     m_menu->addAction(m_aRealtime);
62
63     this->setContextMenuPolicy(Qt::CustomContextMenu);
64
65     if (m_projMonitor->isActive()) {
66         m_activeRender = m_projMonitor->render;
67     } else {
68         m_activeRender = m_clipMonitor->render;
69     }
70
71     bool b = true;
72
73     b &= connect(this, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(customContextMenuRequested(QPoint)));
74
75     b &= connect(m_activeRender, SIGNAL(rendererPosition(int)), this, SLOT(slotRenderZoneUpdated()));
76     b &= connect(this, SIGNAL(signalHUDRenderingFinished(uint,uint)), this, SLOT(slotHUDRenderingFinished(uint,uint)));
77     b &= connect(this, SIGNAL(signalScopeRenderingFinished(uint,uint)), this, SLOT(slotScopeRenderingFinished(uint,uint)));
78     b &= connect(this, SIGNAL(signalBackgroundRenderingFinished(uint,uint)), this, SLOT(slotBackgroundRenderingFinished(uint,uint)));
79     b &= connect(m_aRealtime, SIGNAL(toggled(bool)), this, SLOT(slotResetRealtimeFactor(bool)));
80     b &= connect(m_aAutoRefresh, SIGNAL(toggled(bool)), this, SLOT(slotAutoRefreshToggled(bool)));
81
82     Q_ASSERT(b);
83 }
84
85 AbstractScopeWidget::~AbstractScopeWidget()
86 {
87     delete m_menu;
88     delete m_aAutoRefresh;
89     delete m_aRealtime;
90 }
91
92 void AbstractScopeWidget::prodHUDThread()
93 {
94     if (this->visibleRegion().isEmpty()) {
95         qDebug() << "Scope " << widgetName() << " is not visible. Not calculating HUD.";
96     }
97     if (m_semaphoreHUD.tryAcquire(1)) {
98         Q_ASSERT(!m_threadHUD.isRunning());
99
100         m_newHUDFrames.fetchAndStoreRelaxed(0);
101         m_newHUDUpdates.fetchAndStoreRelaxed(0);
102         m_threadHUD = QtConcurrent::run(this, &AbstractScopeWidget::renderHUD, m_accelFactorHUD);
103         qDebug() << "HUD thread started in " << widgetName();
104
105     } else {
106         qDebug() << "HUD semaphore locked, not prodding in " << widgetName() << ". Thread running: " << m_threadHUD.isRunning();
107     }
108 }
109
110 void AbstractScopeWidget::prodScopeThread()
111 {
112     // Only start a new thread if the scope is actually visible
113     // and not hidden by another widget on the stack.
114     if (this->visibleRegion().isEmpty()) {
115         qDebug() << "Scope " << widgetName() << " is not visible. Not calculating scope.";
116     }
117     // Try to acquire the semaphore. This must only succeed if m_threadScope is not running
118     // anymore. Therefore the semaphore must NOT be released before m_threadScope ends.
119     // If acquiring the semaphore fails, the thread is still running.
120     if (m_semaphoreScope.tryAcquire(1)) {
121         Q_ASSERT(!m_threadScope.isRunning());
122
123         m_newScopeFrames.fetchAndStoreRelaxed(0);
124         m_newScopeUpdates.fetchAndStoreRelaxed(0);
125
126         // See http://doc.qt.nokia.com/latest/qtconcurrentrun.html#run about
127         // running member functions in a thread
128         m_threadScope = QtConcurrent::run(this, &AbstractScopeWidget::renderScope, m_accelFactorScope);
129
130         qDebug() << "Scope thread started in " << widgetName();
131
132     } else {
133         qDebug() << "Scope semaphore locked, not prodding in " << widgetName() << ". Thread running: " << m_threadScope.isRunning();
134     }
135 }
136 void AbstractScopeWidget::prodBackgroundThread()
137 {
138     if (this->visibleRegion().isEmpty()) {
139         qDebug() << "Scope " << widgetName() << " is not visible. Not calculating background.";
140     }
141     if (m_semaphoreBackground.tryAcquire(1)) {
142         Q_ASSERT(!m_threadBackground.isRunning());
143
144         m_newBackgroundFrames.fetchAndStoreRelaxed(0);
145         m_newBackgroundUpdates.fetchAndStoreRelaxed(0);
146         m_threadBackground = QtConcurrent::run(this, &AbstractScopeWidget::renderBackground, m_accelFactorBackground);
147         qDebug() << "Background thread started in " << widgetName();
148
149     } else {
150         qDebug() << "Background semaphore locked, not prodding in " << widgetName() << ". Thread running: " << m_threadBackground.isRunning();
151     }
152 }
153
154 void AbstractScopeWidget::forceUpdate()
155 {
156     m_newHUDUpdates.fetchAndAddRelaxed(1);
157     m_newScopeUpdates.fetchAndAddRelaxed(1);
158     m_newBackgroundUpdates.fetchAndAddRelaxed(1);
159     prodHUDThread();
160     prodScopeThread();
161     prodBackgroundThread();
162 }
163 void AbstractScopeWidget::forceUpdateHUD()
164 {
165     m_newHUDUpdates.fetchAndAddRelaxed(1);
166     prodHUDThread();
167
168 }
169 void AbstractScopeWidget::forceUpdateScope()
170 {
171     m_newScopeUpdates.fetchAndAddRelaxed(1);
172     prodScopeThread();
173
174 }
175 void AbstractScopeWidget::forceUpdateBackground()
176 {
177     m_newBackgroundUpdates.fetchAndAddRelaxed(1);
178     prodBackgroundThread();
179
180 }
181
182
183 ///// Events /////
184
185 void AbstractScopeWidget::mouseReleaseEvent(QMouseEvent *event)
186 {
187     prodHUDThread();
188     prodScopeThread();
189     prodBackgroundThread();
190     QWidget::mouseReleaseEvent(event);
191 }
192
193 void AbstractScopeWidget::resizeEvent(QResizeEvent *event)
194 {
195     // Update the dimension of the available rect for painting
196     m_scopeRect = scopeRect();
197
198     forceUpdate();
199
200     QWidget::resizeEvent(event);
201 }
202
203 void AbstractScopeWidget::raise()
204 {
205     // Widget has been brought to the top of a widget stack,
206     // layers need to be updated.
207     QWidget::raise();
208     forceUpdate();
209 }
210
211 void AbstractScopeWidget::paintEvent(QPaintEvent *)
212 {
213 //    qDebug() << "Painting now on scope " << widgetName();
214     if (!initialDimensionUpdateDone) {
215         // This is a workaround.
216         // When updating the dimensions in the constructor, the size
217         // of the control items at the top are simply ignored! So do
218         // it here instead.
219         m_scopeRect = scopeRect();
220         initialDimensionUpdateDone = true;
221     }
222
223     QPainter davinci(this);
224     davinci.drawImage(scopeRect().topLeft(), m_imgBackground);
225     davinci.drawImage(scopeRect().topLeft(), m_imgScope);
226     davinci.drawImage(scopeRect().topLeft(), m_imgHUD);
227 }
228
229 void AbstractScopeWidget::customContextMenuRequested(const QPoint &pos)
230 {
231     m_menu->exec(this->mapToGlobal(pos));
232 }
233
234 uint AbstractScopeWidget::calculateAccelFactorHUD(uint oldMseconds, uint) { return ceil((float)oldMseconds*REALTIME_FPS/1000 ); }
235 uint AbstractScopeWidget::calculateAccelFactorScope(uint oldMseconds, uint) { return ceil((float)oldMseconds*REALTIME_FPS/1000 ); }
236 uint AbstractScopeWidget::calculateAccelFactorBackground(uint oldMseconds, uint) { return ceil((float)oldMseconds*REALTIME_FPS/1000 ); }
237
238
239 ///// Slots /////
240
241 void AbstractScopeWidget::slotHUDRenderingFinished(uint mseconds, uint oldFactor)
242 {
243     qDebug() << "HUD rendering has finished, waiting for termination in " << widgetName();
244     m_threadHUD.waitForFinished();
245     m_imgHUD = m_threadHUD.result();
246
247     m_semaphoreHUD.release(1);
248     this->update();
249
250     int accel;
251     if (m_aRealtime->isChecked()) {
252         accel = calculateAccelFactorHUD(mseconds, oldFactor);
253         if (m_accelFactorHUD < 1) {
254             accel = 1;
255         }
256         m_accelFactorHUD = accel;
257     }
258
259     if ( (m_newHUDFrames > 0 && m_aAutoRefresh->isChecked()) || m_newHUDUpdates > 0) {
260         qDebug() << "Trying to start a new HUD thread for " << widgetName()
261                 << ". New frames/updates: " << m_newHUDFrames << "/" << m_newHUDUpdates;
262         prodHUDThread();;
263     }
264 }
265
266 void AbstractScopeWidget::slotScopeRenderingFinished(uint mseconds, uint oldFactor)
267 {
268     // The signal can be received before the thread has really finished. So we
269     // need to wait until it has really finished before starting a new thread.
270     qDebug() << "Scope rendering has finished, waiting for termination in " << widgetName();
271     m_threadScope.waitForFinished();
272     m_imgScope = m_threadScope.result();
273
274     // The scope thread has finished. Now we can release the semaphore, allowing a new thread.
275     // See prodScopeThread where the semaphore is acquired again.
276     m_semaphoreScope.release(1);
277     this->update();
278
279     // Calculate the acceleration factor hint to get «realtime» updates.
280     int accel;
281     if (m_aRealtime->isChecked()) {
282         accel = calculateAccelFactorScope(mseconds, oldFactor);
283         if (m_accelFactorScope < 1) {
284             // If mseconds happens to be 0.
285             accel = 1;
286         }
287         // Don't directly calculate with m_accelFactorScope as we are dealing with concurrency.
288         // If m_accelFactorScope is set to 0 at the wrong moment, who knows what might happen
289         // then :) Therefore use a local variable.
290         m_accelFactorScope = accel;
291     }
292
293     if ( (m_newScopeFrames > 0 && m_aAutoRefresh->isChecked()) || m_newScopeUpdates > 0) {
294         qDebug() << "Trying to start a new scope thread for " << widgetName()
295                 << ". New frames/updates: " << m_newScopeFrames << "/" << m_newScopeUpdates;
296         prodScopeThread();
297     }
298 }
299
300 void AbstractScopeWidget::slotBackgroundRenderingFinished(uint mseconds, uint oldFactor)
301 {
302     qDebug() << "Background rendering has finished, waiting for termination in " << widgetName();
303     m_threadBackground.waitForFinished();
304     m_imgBackground = m_threadBackground.result();
305
306     m_semaphoreBackground.release(1);
307     this->update();
308
309     int accel;
310     if (m_aRealtime->isChecked()) {
311         accel = calculateAccelFactorBackground(mseconds, oldFactor);
312         if (m_accelFactorBackground < 1) {
313             accel = 1;
314         }
315         m_accelFactorBackground = accel;
316     }
317
318     if ( (m_newBackgroundFrames > 0 && m_aAutoRefresh->isChecked()) || m_newBackgroundUpdates > 0) {
319         qDebug() << "Trying to start a new background thread for " << widgetName()
320                 << ". New frames/updates: " << m_newBackgroundFrames << "/" << m_newBackgroundUpdates;
321         prodBackgroundThread();;
322     }
323 }
324
325 void AbstractScopeWidget::slotActiveMonitorChanged(bool isClipMonitor)
326 {
327     qDebug() << "Active monitor has changed in " << widgetName() << ". Is the clip monitor active now? " << isClipMonitor;
328
329     bool disconnected = m_activeRender->disconnect(this);
330     Q_ASSERT(disconnected);
331
332     m_activeRender = (isClipMonitor) ? m_clipMonitor->render : m_projMonitor->render;
333
334     connect(m_activeRender, SIGNAL(rendererPosition(int)), this, SLOT(slotRenderZoneUpdated()));
335     connect(m_activeRender, SIGNAL(rendererPositionBefore0()), this, SLOT(slotRenderZoneUpdated()));
336
337     // Update the scope for the new monitor.
338     prodHUDThread();
339     prodScopeThread();
340     prodBackgroundThread();
341 }
342
343 void AbstractScopeWidget::slotRenderZoneUpdated()
344 {
345     m_newHUDFrames.fetchAndAddRelaxed(1);
346     m_newScopeFrames.fetchAndAddRelaxed(1);
347     m_newBackgroundFrames.fetchAndAddRelaxed(1);
348
349     qDebug() << "Monitor incoming. New frames total HUD/Scope/Background: " << m_newHUDFrames
350             << "/" << m_newScopeFrames << "/" << m_newBackgroundFrames;
351
352     if (this->visibleRegion().isEmpty()) {
353         qDebug() << "Scope of widget " << widgetName() << " is not at the top, not rendering.";
354     } else {
355         if (m_aAutoRefresh->isChecked()) {
356             prodHUDThread();
357             prodScopeThread();
358             prodBackgroundThread();
359         }
360     }
361 }
362
363 void AbstractScopeWidget::slotResetRealtimeFactor(bool realtimeChecked)
364 {
365     if (!realtimeChecked) {
366         m_accelFactorHUD = 1;
367         m_accelFactorScope = 1;
368         m_accelFactorBackground = 1;
369     }
370 }
371
372 void AbstractScopeWidget::slotAutoRefreshToggled(bool autoRefresh)
373 {
374     // TODO only if depends on input
375     if (autoRefresh) {
376         forceUpdate();
377     }
378 }