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