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 const int REALTIME_FPS = 30;
25 const QColor light(250, 238, 226, 255);
26 const QColor dark ( 40, 40, 39, 255);
27 const QColor dark2( 25, 25, 23, 255);
29 const QPen AbstractScopeWidget::penThick(QBrush(QColor(250,250,250)), 2, Qt::SolidLine);
30 const QPen AbstractScopeWidget::penThin (QBrush(QColor(250,250,250)), 1, Qt::SolidLine);
31 const QPen AbstractScopeWidget::penLight(QBrush(QColor(200,200,250,150)), 1, Qt::SolidLine);
32 const QPen AbstractScopeWidget::penDark (QBrush(QColor(0,0,20,250)), 1, Qt::SolidLine);
34 AbstractScopeWidget::AbstractScopeWidget(Monitor *projMonitor, Monitor *clipMonitor, bool trackMouse, QWidget *parent) :
36 m_projMonitor(projMonitor),
37 m_clipMonitor(clipMonitor),
39 m_mouseWithinWidget(false),
42 m_accelFactorScope(1),
43 m_accelFactorBackground(1),
46 m_semaphoreBackground(1),
47 initialDimensionUpdateDone(false)
50 m_scopePalette = QPalette();
51 m_scopePalette.setBrush(QPalette::Window, QBrush(dark2));
52 m_scopePalette.setBrush(QPalette::Base, QBrush(dark));
53 m_scopePalette.setBrush(QPalette::Button, QBrush(dark));
54 m_scopePalette.setBrush(QPalette::Text, QBrush(light));
55 m_scopePalette.setBrush(QPalette::WindowText, QBrush(light));
56 m_scopePalette.setBrush(QPalette::ButtonText, QBrush(light));
57 this->setPalette(m_scopePalette);
58 this->setAutoFillBackground(true);
60 m_aAutoRefresh = new QAction(i18n("Auto Refresh"), this);
61 m_aAutoRefresh->setCheckable(true);
62 m_aRealtime = new QAction(i18n("Realtime (with precision loss)"), this);
63 m_aRealtime->setCheckable(true);
65 m_menu = new QMenu(this);
66 m_menu->setPalette(m_scopePalette);
67 m_menu->addAction(m_aAutoRefresh);
68 m_menu->addAction(m_aRealtime);
70 this->setContextMenuPolicy(Qt::CustomContextMenu);
72 m_activeRender = (m_clipMonitor->isActive()) ? m_clipMonitor->render : m_projMonitor->render;
75 b &= connect(this, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(customContextMenuRequested(QPoint)));
77 b &= connect(m_activeRender, SIGNAL(rendererPosition(int)), this, SLOT(slotRenderZoneUpdated()));
78 b &= connect(m_activeRender, SIGNAL(frameUpdated(QImage)), this, SLOT(slotRenderZoneUpdated(QImage)));
80 b &= connect(this, SIGNAL(signalHUDRenderingFinished(uint,uint)), this, SLOT(slotHUDRenderingFinished(uint,uint)));
81 b &= connect(this, SIGNAL(signalScopeRenderingFinished(uint,uint)), this, SLOT(slotScopeRenderingFinished(uint,uint)));
82 b &= connect(this, SIGNAL(signalBackgroundRenderingFinished(uint,uint)), this, SLOT(slotBackgroundRenderingFinished(uint,uint)));
83 b &= connect(m_aRealtime, SIGNAL(toggled(bool)), this, SLOT(slotResetRealtimeFactor(bool)));
84 b &= connect(m_aAutoRefresh, SIGNAL(toggled(bool)), this, SLOT(slotAutoRefreshToggled(bool)));
87 // Enable mouse tracking if desired.
88 // Causes the mouseMoved signal to be emitted when the mouse moves inside the
89 // widget, even when no mouse button is pressed.
90 this->setMouseTracking(trackMouse);
93 AbstractScopeWidget::~AbstractScopeWidget()
98 delete m_aAutoRefresh;
102 void AbstractScopeWidget::init()
104 m_widgetName = widgetName();
108 void AbstractScopeWidget::readConfig()
110 KSharedConfigPtr config = KGlobal::config();
111 KConfigGroup scopeConfig(config, configName());
112 m_aAutoRefresh->setChecked(scopeConfig.readEntry("autoRefresh", true));
113 m_aRealtime->setChecked(scopeConfig.readEntry("realtime", false));
117 void AbstractScopeWidget::writeConfig()
119 KSharedConfigPtr config = KGlobal::config();
120 KConfigGroup scopeConfig(config, configName());
121 scopeConfig.writeEntry("autoRefresh", m_aAutoRefresh->isChecked());
122 scopeConfig.writeEntry("realtime", m_aRealtime->isChecked());
126 QString AbstractScopeWidget::configName() { return "Scope_" + m_widgetName; }
128 void AbstractScopeWidget::prodHUDThread()
130 if (this->visibleRegion().isEmpty()) {
131 // qDebug() << "Scope " << m_widgetName << " is not visible. Not calculating HUD.";
133 if (m_semaphoreHUD.tryAcquire(1)) {
134 Q_ASSERT(!m_threadHUD.isRunning());
136 m_newHUDFrames.fetchAndStoreRelaxed(0);
137 m_newHUDUpdates.fetchAndStoreRelaxed(0);
138 m_threadHUD = QtConcurrent::run(this, &AbstractScopeWidget::renderHUD, m_accelFactorHUD);
139 // qDebug() << "HUD thread started in " << m_widgetName;
142 // qDebug() << "HUD semaphore locked, not prodding in " << m_widgetName << ". Thread running: " << m_threadHUD.isRunning();
147 void AbstractScopeWidget::prodScopeThread()
149 // Only start a new thread if the scope is actually visible
150 // and not hidden by another widget on the stack.
151 if (this->visibleRegion().isEmpty()) {
152 // qDebug() << "Scope " << m_widgetName << " is not visible. Not calculating scope.";
154 // Try to acquire the semaphore. This must only succeed if m_threadScope is not running
155 // anymore. Therefore the semaphore must NOT be released before m_threadScope ends.
156 // If acquiring the semaphore fails, the thread is still running.
157 if (m_semaphoreScope.tryAcquire(1)) {
158 Q_ASSERT(!m_threadScope.isRunning());
160 m_newScopeFrames.fetchAndStoreRelaxed(0);
161 m_newScopeUpdates.fetchAndStoreRelaxed(0);
163 Q_ASSERT(m_accelFactorScope > 0);
165 // See http://doc.qt.nokia.com/latest/qtconcurrentrun.html#run about
166 // running member functions in a thread
167 m_threadScope = QtConcurrent::run(this, &AbstractScopeWidget::renderScope, m_accelFactorScope, m_scopeImage);
169 // qDebug() << "Scope thread started in " << m_widgetName;
172 // qDebug() << "Scope semaphore locked, not prodding in " << m_widgetName << ". Thread running: " << m_threadScope.isRunning();
176 void AbstractScopeWidget::prodBackgroundThread()
178 if (this->visibleRegion().isEmpty()) {
179 // qDebug() << "Scope " << m_widgetName << " is not visible. Not calculating background.";
181 if (m_semaphoreBackground.tryAcquire(1)) {
182 Q_ASSERT(!m_threadBackground.isRunning());
184 m_newBackgroundFrames.fetchAndStoreRelaxed(0);
185 m_newBackgroundUpdates.fetchAndStoreRelaxed(0);
186 m_threadBackground = QtConcurrent::run(this, &AbstractScopeWidget::renderBackground, m_accelFactorBackground);
187 // qDebug() << "Background thread started in " << m_widgetName;
190 // qDebug() << "Background semaphore locked, not prodding in " << m_widgetName << ". Thread running: " << m_threadBackground.isRunning();
195 void AbstractScopeWidget::forceUpdate(bool doUpdate)
197 // qDebug() << "Force update called in " << widgetName() << ". Arg: " << doUpdate;
198 if (!doUpdate) { return; }
199 m_newHUDUpdates.fetchAndAddRelaxed(1);
200 m_newScopeUpdates.fetchAndAddRelaxed(1);
201 m_newBackgroundUpdates.fetchAndAddRelaxed(1);
204 prodBackgroundThread();
206 void AbstractScopeWidget::forceUpdateHUD()
208 m_newHUDUpdates.fetchAndAddRelaxed(1);
212 void AbstractScopeWidget::forceUpdateScope()
214 m_newScopeUpdates.fetchAndAddRelaxed(1);
218 void AbstractScopeWidget::forceUpdateBackground()
220 m_newBackgroundUpdates.fetchAndAddRelaxed(1);
221 prodBackgroundThread();
228 void AbstractScopeWidget::mouseReleaseEvent(QMouseEvent *event)
232 prodBackgroundThread();
233 QWidget::mouseReleaseEvent(event);
236 void AbstractScopeWidget::resizeEvent(QResizeEvent *event)
238 // Update the dimension of the available rect for painting
239 m_scopeRect = scopeRect();
243 QWidget::resizeEvent(event);
246 void AbstractScopeWidget::showEvent(QShowEvent *event)
248 QWidget::showEvent(event);
249 m_scopeRect = scopeRect();
252 void AbstractScopeWidget::paintEvent(QPaintEvent *)
254 QPainter davinci(this);
255 davinci.drawImage(m_scopeRect.topLeft(), m_imgBackground);
256 davinci.drawImage(m_scopeRect.topLeft(), m_imgScope);
257 davinci.drawImage(m_scopeRect.topLeft(), m_imgHUD);
260 void AbstractScopeWidget::mouseMoveEvent(QMouseEvent *event)
262 m_mousePos = event->pos();
263 m_mouseWithinWidget = true;
264 emit signalMousePositionChanged();
266 void AbstractScopeWidget::leaveEvent(QEvent *)
268 m_mouseWithinWidget = false;
269 emit signalMousePositionChanged();
272 void AbstractScopeWidget::customContextMenuRequested(const QPoint &pos)
274 m_menu->exec(this->mapToGlobal(pos));
277 uint AbstractScopeWidget::calculateAccelFactorHUD(uint oldMseconds, uint) { return ceil((float)oldMseconds*REALTIME_FPS/1000 ); }
278 uint AbstractScopeWidget::calculateAccelFactorScope(uint oldMseconds, uint) { return ceil((float)oldMseconds*REALTIME_FPS/1000 ); }
279 uint AbstractScopeWidget::calculateAccelFactorBackground(uint oldMseconds, uint) { return ceil((float)oldMseconds*REALTIME_FPS/1000 ); }
284 void AbstractScopeWidget::slotHUDRenderingFinished(uint mseconds, uint oldFactor)
286 // qDebug() << "HUD rendering has finished, waiting for termination in " << m_widgetName;
287 m_threadHUD.waitForFinished();
288 m_imgHUD = m_threadHUD.result();
290 m_semaphoreHUD.release(1);
294 if (m_aRealtime->isChecked()) {
295 accel = calculateAccelFactorHUD(mseconds, oldFactor);
296 if (m_accelFactorHUD < 1) {
299 m_accelFactorHUD = accel;
302 if ( (m_newHUDFrames > 0 && m_aAutoRefresh->isChecked()) || m_newHUDUpdates > 0) {
303 // qDebug() << "Trying to start a new HUD thread for " << m_widgetName
304 // << ". New frames/updates: " << m_newHUDFrames << "/" << m_newHUDUpdates;
309 void AbstractScopeWidget::slotScopeRenderingFinished(uint mseconds, uint oldFactor)
311 // The signal can be received before the thread has really finished. So we
312 // need to wait until it has really finished before starting a new thread.
313 // qDebug() << "Scope rendering has finished, waiting for termination in " << m_widgetName;
314 m_threadScope.waitForFinished();
315 m_imgScope = m_threadScope.result();
317 // The scope thread has finished. Now we can release the semaphore, allowing a new thread.
318 // See prodScopeThread where the semaphore is acquired again.
319 m_semaphoreScope.release(1);
322 // Calculate the acceleration factor hint to get «realtime» updates.
324 if (m_aRealtime->isChecked()) {
325 accel = calculateAccelFactorScope(mseconds, oldFactor);
327 // If mseconds happens to be 0.
330 // Don't directly calculate with m_accelFactorScope as we are dealing with concurrency.
331 // If m_accelFactorScope is set to 0 at the wrong moment, who knows what might happen
332 // then :) Therefore use a local variable.
333 m_accelFactorScope = accel;
336 if ( (m_newScopeFrames > 0 && m_aAutoRefresh->isChecked()) || m_newScopeUpdates > 0) {
337 // qDebug() << "Trying to start a new scope thread for " << m_widgetName
338 // << ". New frames/updates: " << m_newScopeFrames << "/" << m_newScopeUpdates;
343 void AbstractScopeWidget::slotBackgroundRenderingFinished(uint mseconds, uint oldFactor)
345 // qDebug() << "Background rendering has finished, waiting for termination in " << m_widgetName;
346 m_threadBackground.waitForFinished();
347 m_imgBackground = m_threadBackground.result();
349 m_semaphoreBackground.release(1);
353 if (m_aRealtime->isChecked()) {
354 accel = calculateAccelFactorBackground(mseconds, oldFactor);
355 if (m_accelFactorBackground < 1) {
358 m_accelFactorBackground = accel;
361 if ( (m_newBackgroundFrames > 0 && m_aAutoRefresh->isChecked()) || m_newBackgroundUpdates > 0) {
362 // qDebug() << "Trying to start a new background thread for " << m_widgetName
363 // << ". New frames/updates: " << m_newBackgroundFrames << "/" << m_newBackgroundUpdates;
364 prodBackgroundThread();;
368 void AbstractScopeWidget::slotActiveMonitorChanged(bool isClipMonitor)
370 // qDebug() << "Active monitor has changed in " << m_widgetName << ". Is the clip monitor active now? " << isClipMonitor;
372 bool b = m_activeRender->disconnect(this);
375 m_activeRender = (isClipMonitor) ? m_clipMonitor->render : m_projMonitor->render;
377 b &= connect(m_activeRender, SIGNAL(rendererPosition(int)), this, SLOT(slotRenderZoneUpdated()));
378 b &= connect(m_activeRender, SIGNAL(frameUpdated(QImage)), this, SLOT(slotRenderZoneUpdated(QImage)));
381 // Update the scope for the new monitor.
384 prodBackgroundThread();
387 void AbstractScopeWidget::slotRenderZoneUpdated()
389 m_newHUDFrames.fetchAndAddRelaxed(1);
390 m_newScopeFrames.fetchAndAddRelaxed(1);
391 m_newBackgroundFrames.fetchAndAddRelaxed(1);
393 // qDebug() << "Monitor incoming. New frames total HUD/Scope/Background: " << m_newHUDFrames
394 // << "/" << m_newScopeFrames << "/" << m_newBackgroundFrames;
396 if (this->visibleRegion().isEmpty()) {
397 // qDebug() << "Scope of widget " << m_widgetName << " is not at the top, not rendering.";
399 if (m_aAutoRefresh->isChecked()) {
402 prodBackgroundThread();
407 void AbstractScopeWidget::slotRenderZoneUpdated(QImage frame)
409 m_scopeImage = frame;
410 slotRenderZoneUpdated();
413 void AbstractScopeWidget::slotResetRealtimeFactor(bool realtimeChecked)
415 if (!realtimeChecked) {
416 m_accelFactorHUD = 1;
417 m_accelFactorScope = 1;
418 m_accelFactorBackground = 1;
422 void AbstractScopeWidget::slotAutoRefreshToggled(bool autoRefresh)
424 // TODO only if depends on input