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