1 /***************************************************************************
2 * Copyright (C) 2010 by Simon Andreas Eugster (simon.eu@gmail.com) *
3 * This file is part of kdenlive. See www.kdenlive.org. *
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 ***************************************************************************/
11 #include "qtconcurrentrun.h"
13 #include "abstractscopewidget.h"
20 #include <QMouseEvent>
23 // Uncomment for Scope debugging.
30 const int REALTIME_FPS = 30;
32 const QColor light(250, 238, 226, 255);
33 const QColor dark(40, 40, 39, 255);
34 const QColor dark2(25, 25, 23, 255);
36 const QPen AbstractScopeWidget::penThick(QBrush(QColor(250, 250, 250)), 2, Qt::SolidLine);
37 const QPen AbstractScopeWidget::penThin(QBrush(QColor(250, 250, 250)), 1, Qt::SolidLine);
38 const QPen AbstractScopeWidget::penLight(QBrush(QColor(200, 200, 250, 150)), 1, Qt::SolidLine);
39 const QPen AbstractScopeWidget::penLightDots(QBrush(QColor(200, 200, 250, 150)), 1, Qt::DotLine);
40 const QPen AbstractScopeWidget::penDark(QBrush(QColor(0, 0, 20, 250)), 1, Qt::SolidLine);
41 const QPen AbstractScopeWidget::penDarkDots(QBrush(QColor(0, 0, 20, 250)), 1, Qt::DotLine);
43 AbstractScopeWidget::AbstractScopeWidget(bool trackMouse, QWidget *parent) :
46 m_mouseWithinWidget(false),
49 m_accelFactorScope(1),
50 m_accelFactorBackground(1),
53 m_semaphoreBackground(1),
54 initialDimensionUpdateDone(false),
55 m_requestForcedUpdate(false)
58 m_scopePalette = QPalette();
59 m_scopePalette.setBrush(QPalette::Window, QBrush(dark2));
60 m_scopePalette.setBrush(QPalette::Base, QBrush(dark));
61 m_scopePalette.setBrush(QPalette::Button, QBrush(dark));
62 m_scopePalette.setBrush(QPalette::Text, QBrush(light));
63 m_scopePalette.setBrush(QPalette::WindowText, QBrush(light));
64 m_scopePalette.setBrush(QPalette::ButtonText, QBrush(light));
65 this->setPalette(m_scopePalette);
66 this->setAutoFillBackground(true);
68 m_aAutoRefresh = new QAction(i18n("Auto Refresh"), this);
69 m_aAutoRefresh->setCheckable(true);
70 m_aRealtime = new QAction(i18n("Realtime (with precision loss)"), this);
71 m_aRealtime->setCheckable(true);
73 m_menu = new QMenu(this);
74 m_menu->setPalette(m_scopePalette);
75 m_menu->addAction(m_aAutoRefresh);
76 m_menu->addAction(m_aRealtime);
78 this->setContextMenuPolicy(Qt::CustomContextMenu);
81 b &= connect(this, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(customContextMenuRequested(QPoint)));
83 b &= connect(this, SIGNAL(signalHUDRenderingFinished(uint, uint)), this, SLOT(slotHUDRenderingFinished(uint, uint)));
84 b &= connect(this, SIGNAL(signalScopeRenderingFinished(uint, uint)), this, SLOT(slotScopeRenderingFinished(uint, uint)));
85 b &= connect(this, SIGNAL(signalBackgroundRenderingFinished(uint, uint)), this, SLOT(slotBackgroundRenderingFinished(uint, uint)));
86 b &= connect(m_aRealtime, SIGNAL(toggled(bool)), this, SLOT(slotResetRealtimeFactor(bool)));
87 b &= connect(m_aAutoRefresh, SIGNAL(toggled(bool)), this, SLOT(slotAutoRefreshToggled(bool)));
90 // Enable mouse tracking if desired.
91 // Causes the mouseMoved signal to be emitted when the mouse moves inside the
92 // widget, even when no mouse button is pressed.
93 this->setMouseTracking(trackMouse);
96 AbstractScopeWidget::~AbstractScopeWidget()
101 delete m_aAutoRefresh;
105 void AbstractScopeWidget::init()
107 m_widgetName = widgetName();
111 void AbstractScopeWidget::readConfig()
113 KSharedConfigPtr config = KGlobal::config();
114 KConfigGroup scopeConfig(config, configName());
115 m_aAutoRefresh->setChecked(scopeConfig.readEntry("autoRefresh", true));
116 m_aRealtime->setChecked(scopeConfig.readEntry("realtime", false));
120 void AbstractScopeWidget::writeConfig()
122 KSharedConfigPtr config = KGlobal::config();
123 KConfigGroup scopeConfig(config, configName());
124 scopeConfig.writeEntry("autoRefresh", m_aAutoRefresh->isChecked());
125 scopeConfig.writeEntry("realtime", m_aRealtime->isChecked());
129 QString AbstractScopeWidget::configName()
131 return "Scope_" + m_widgetName;
134 void AbstractScopeWidget::prodHUDThread()
136 if (this->visibleRegion().isEmpty()) {
138 qDebug() << "Scope " << m_widgetName << " is not visible. Not calculating HUD.";
141 if (m_semaphoreHUD.tryAcquire(1)) {
142 Q_ASSERT(!m_threadHUD.isRunning());
144 m_newHUDFrames.fetchAndStoreRelaxed(0);
145 m_newHUDUpdates.fetchAndStoreRelaxed(0);
146 m_threadHUD = QtConcurrent::run(this, &AbstractScopeWidget::renderHUD, m_accelFactorHUD);
148 qDebug() << "HUD thread started in " << m_widgetName;
154 qDebug() << "HUD semaphore locked, not prodding in " << m_widgetName << ". Thread running: " << m_threadHUD.isRunning();
160 void AbstractScopeWidget::prodScopeThread()
162 // Only start a new thread if the scope is actually visible
163 // and not hidden by another widget on the stack and if user want the scope to update.
164 if (this->visibleRegion().isEmpty() || (!m_aAutoRefresh->isChecked() && !m_requestForcedUpdate)) {
166 qDebug() << "Scope " << m_widgetName << " is not visible. Not calculating scope.";
169 // Try to acquire the semaphore. This must only succeed if m_threadScope is not running
170 // anymore. Therefore the semaphore must NOT be released before m_threadScope ends.
171 // If acquiring the semaphore fails, the thread is still running.
172 if (m_semaphoreScope.tryAcquire(1)) {
173 Q_ASSERT(!m_threadScope.isRunning());
175 m_newScopeFrames.fetchAndStoreRelaxed(0);
176 m_newScopeUpdates.fetchAndStoreRelaxed(0);
178 Q_ASSERT(m_accelFactorScope > 0);
180 // See http://doc.qt.nokia.com/latest/qtconcurrentrun.html#run about
181 // running member functions in a thread
182 m_threadScope = QtConcurrent::run(this, &AbstractScopeWidget::renderScope, m_accelFactorScope);
183 m_requestForcedUpdate = false;
186 qDebug() << "Scope thread started in " << m_widgetName;
191 qDebug() << "Scope semaphore locked, not prodding in " << m_widgetName << ". Thread running: " << m_threadScope.isRunning();
196 void AbstractScopeWidget::prodBackgroundThread()
198 if (this->visibleRegion().isEmpty()) {
200 qDebug() << "Scope " << m_widgetName << " is not visible. Not calculating background.";
203 if (m_semaphoreBackground.tryAcquire(1)) {
204 Q_ASSERT(!m_threadBackground.isRunning());
206 m_newBackgroundFrames.fetchAndStoreRelaxed(0);
207 m_newBackgroundUpdates.fetchAndStoreRelaxed(0);
208 m_threadBackground = QtConcurrent::run(this, &AbstractScopeWidget::renderBackground, m_accelFactorBackground);
211 qDebug() << "Background thread started in " << m_widgetName;
216 qDebug() << "Background semaphore locked, not prodding in " << m_widgetName << ". Thread running: " << m_threadBackground.isRunning();
222 void AbstractScopeWidget::forceUpdate(bool doUpdate)
225 qDebug() << "Force update called in " << widgetName() << ". Arg: " << doUpdate;
231 m_requestForcedUpdate = true;
232 m_newHUDUpdates.fetchAndAddRelaxed(1);
233 m_newScopeUpdates.fetchAndAddRelaxed(1);
234 m_newBackgroundUpdates.fetchAndAddRelaxed(1);
237 prodBackgroundThread();
239 void AbstractScopeWidget::forceUpdateHUD()
241 m_newHUDUpdates.fetchAndAddRelaxed(1);
245 void AbstractScopeWidget::forceUpdateScope()
247 m_newScopeUpdates.fetchAndAddRelaxed(1);
248 m_requestForcedUpdate = true;
252 void AbstractScopeWidget::forceUpdateBackground()
254 m_newBackgroundUpdates.fetchAndAddRelaxed(1);
255 prodBackgroundThread();
262 void AbstractScopeWidget::mouseReleaseEvent(QMouseEvent *event)
264 if (!m_aAutoRefresh->isChecked()) {
265 m_requestForcedUpdate = true;
269 prodBackgroundThread();
270 QWidget::mouseReleaseEvent(event);
273 void AbstractScopeWidget::resizeEvent(QResizeEvent *event)
275 // Update the dimension of the available rect for painting
276 m_scopeRect = scopeRect();
279 QWidget::resizeEvent(event);
282 void AbstractScopeWidget::showEvent(QShowEvent *event)
284 QWidget::showEvent(event);
285 m_scopeRect = scopeRect();
288 void AbstractScopeWidget::paintEvent(QPaintEvent *)
290 QPainter davinci(this);
291 davinci.drawImage(m_scopeRect.topLeft(), m_imgBackground);
292 davinci.drawImage(m_scopeRect.topLeft(), m_imgScope);
293 davinci.drawImage(m_scopeRect.topLeft(), m_imgHUD);
296 void AbstractScopeWidget::mouseMoveEvent(QMouseEvent *event)
298 m_mousePos = event->pos();
299 m_mouseWithinWidget = true;
300 emit signalMousePositionChanged();
302 void AbstractScopeWidget::leaveEvent(QEvent *)
304 m_mouseWithinWidget = false;
305 emit signalMousePositionChanged();
308 void AbstractScopeWidget::customContextMenuRequested(const QPoint &pos)
310 m_menu->exec(this->mapToGlobal(pos));
313 uint AbstractScopeWidget::calculateAccelFactorHUD(uint oldMseconds, uint)
315 return ceil((float)oldMseconds*REALTIME_FPS / 1000);
317 uint AbstractScopeWidget::calculateAccelFactorScope(uint oldMseconds, uint)
319 return ceil((float)oldMseconds*REALTIME_FPS / 1000);
321 uint AbstractScopeWidget::calculateAccelFactorBackground(uint oldMseconds, uint)
323 return ceil((float)oldMseconds*REALTIME_FPS / 1000);
329 void AbstractScopeWidget::slotHUDRenderingFinished(uint mseconds, uint oldFactor)
331 // qDebug() << "HUD rendering has finished, waiting for termination in " << m_widgetName;
332 m_threadHUD.waitForFinished();
333 m_imgHUD = m_threadHUD.result();
335 m_semaphoreHUD.release(1);
339 if (m_aRealtime->isChecked()) {
340 accel = calculateAccelFactorHUD(mseconds, oldFactor);
341 if (m_accelFactorHUD < 1) {
344 m_accelFactorHUD = accel;
347 if ((m_newHUDFrames > 0 && m_aAutoRefresh->isChecked()) || m_newHUDUpdates > 0) {
349 qDebug() << "Trying to start a new HUD thread for " << m_widgetName
350 << ". New frames/updates: " << m_newHUDFrames << "/" << m_newHUDUpdates;
356 void AbstractScopeWidget::slotScopeRenderingFinished(uint mseconds, uint oldFactor)
358 // The signal can be received before the thread has really finished. So we
359 // need to wait until it has really finished before starting a new thread.
361 qDebug() << "Scope rendering has finished, waiting for termination in " << m_widgetName;
363 m_threadScope.waitForFinished();
364 m_imgScope = m_threadScope.result();
366 // The scope thread has finished. Now we can release the semaphore, allowing a new thread.
367 // See prodScopeThread where the semaphore is acquired again.
368 m_semaphoreScope.release(1);
371 // Calculate the acceleration factor hint to get «realtime» updates.
373 if (m_aRealtime->isChecked()) {
374 accel = calculateAccelFactorScope(mseconds, oldFactor);
376 // If mseconds happens to be 0.
379 // Don't directly calculate with m_accelFactorScope as we are dealing with concurrency.
380 // If m_accelFactorScope is set to 0 at the wrong moment, who knows what might happen
381 // then :) Therefore use a local variable.
382 m_accelFactorScope = accel;
385 if ((m_newScopeFrames > 0 && m_aAutoRefresh->isChecked()) || m_newScopeUpdates > 0) {
387 qDebug() << "Trying to start a new scope thread for " << m_widgetName
388 << ". New frames/updates: " << m_newScopeFrames << "/" << m_newScopeUpdates;
394 void AbstractScopeWidget::slotBackgroundRenderingFinished(uint mseconds, uint oldFactor)
397 qDebug() << "Background rendering has finished, waiting for termination in " << m_widgetName;
399 m_threadBackground.waitForFinished();
400 m_imgBackground = m_threadBackground.result();
402 m_semaphoreBackground.release(1);
406 if (m_aRealtime->isChecked()) {
407 accel = calculateAccelFactorBackground(mseconds, oldFactor);
408 if (m_accelFactorBackground < 1) {
411 m_accelFactorBackground = accel;
414 if ((m_newBackgroundFrames > 0 && m_aAutoRefresh->isChecked()) || m_newBackgroundUpdates > 0) {
416 qDebug() << "Trying to start a new background thread for " << m_widgetName
417 << ". New frames/updates: " << m_newBackgroundFrames << "/" << m_newBackgroundUpdates;
419 prodBackgroundThread();;
423 void AbstractScopeWidget::slotRenderZoneUpdated()
425 m_newHUDFrames.fetchAndAddRelaxed(1);
426 m_newScopeFrames.fetchAndAddRelaxed(1);
427 m_newBackgroundFrames.fetchAndAddRelaxed(1);
430 qDebug() << "Monitor incoming. New frames total HUD/Scope/Background: " << m_newHUDFrames
431 << "/" << m_newScopeFrames << "/" << m_newBackgroundFrames;
434 if (this->visibleRegion().isEmpty()) {
436 qDebug() << "Scope of widget " << m_widgetName << " is not at the top, not rendering.";
439 if (m_aAutoRefresh->isChecked()) {
442 prodBackgroundThread();
447 void AbstractScopeWidget::slotRenderZoneUpdated(QImage frame)
449 m_scopeImage = frame;
450 slotRenderZoneUpdated();
453 void AbstractScopeWidget::slotResetRealtimeFactor(bool realtimeChecked)
455 if (!realtimeChecked) {
456 m_accelFactorHUD = 1;
457 m_accelFactorScope = 1;
458 m_accelFactorBackground = 1;
462 bool AbstractScopeWidget::autoRefreshEnabled()
464 return m_aAutoRefresh->isChecked();
467 void AbstractScopeWidget::slotAutoRefreshToggled(bool autoRefresh)
470 emit requestAutoRefresh(autoRefresh);
472 // TODO only if depends on input
475 m_requestForcedUpdate = true;