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"
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 AbstractScopeWidget::AbstractScopeWidget(Monitor *projMonitor, Monitor *clipMonitor, QWidget *parent) :
31 m_projMonitor(projMonitor),
32 m_clipMonitor(clipMonitor),
35 m_accelFactorScope(1),
36 m_accelFactorBackground(1),
39 m_semaphoreBackground(1),
40 initialDimensionUpdateDone(false)
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);
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);
58 m_menu = new QMenu(this);
59 m_menu->setPalette(m_scopePalette);
60 m_menu->addAction(m_aAutoRefresh);
61 m_menu->addAction(m_aRealtime);
63 this->setContextMenuPolicy(Qt::CustomContextMenu);
65 m_activeRender = (m_clipMonitor->isActive()) ? m_clipMonitor->render : m_projMonitor->render;
68 b &= connect(this, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(customContextMenuRequested(QPoint)));
70 b &= connect(m_activeRender, SIGNAL(rendererPosition(int)), this, SLOT(slotRenderZoneUpdated()));
71 b &= connect(m_activeRender, SIGNAL(frameUpdated(int)), this, SLOT(slotRenderZoneUpdated()));
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)));
81 AbstractScopeWidget::~AbstractScopeWidget()
84 delete m_aAutoRefresh;
88 void AbstractScopeWidget::prodHUDThread()
90 if (this->visibleRegion().isEmpty()) {
91 qDebug() << "Scope " << widgetName() << " is not visible. Not calculating HUD.";
93 if (m_semaphoreHUD.tryAcquire(1)) {
94 Q_ASSERT(!m_threadHUD.isRunning());
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();
102 qDebug() << "HUD semaphore locked, not prodding in " << widgetName() << ". Thread running: " << m_threadHUD.isRunning();
107 void AbstractScopeWidget::prodScopeThread()
109 // Only start a new thread if the scope is actually visible
110 // and not hidden by another widget on the stack.
111 if (this->visibleRegion().isEmpty()) {
112 qDebug() << "Scope " << widgetName() << " is not visible. Not calculating scope.";
114 // Try to acquire the semaphore. This must only succeed if m_threadScope is not running
115 // anymore. Therefore the semaphore must NOT be released before m_threadScope ends.
116 // If acquiring the semaphore fails, the thread is still running.
117 if (m_semaphoreScope.tryAcquire(1)) {
118 Q_ASSERT(!m_threadScope.isRunning());
120 m_newScopeFrames.fetchAndStoreRelaxed(0);
121 m_newScopeUpdates.fetchAndStoreRelaxed(0);
123 // See http://doc.qt.nokia.com/latest/qtconcurrentrun.html#run about
124 // running member functions in a thread
125 m_threadScope = QtConcurrent::run(this, &AbstractScopeWidget::renderScope, m_accelFactorScope);
127 qDebug() << "Scope thread started in " << widgetName();
130 qDebug() << "Scope semaphore locked, not prodding in " << widgetName() << ". Thread running: " << m_threadScope.isRunning();
134 void AbstractScopeWidget::prodBackgroundThread()
136 if (this->visibleRegion().isEmpty()) {
137 qDebug() << "Scope " << widgetName() << " is not visible. Not calculating background.";
139 if (m_semaphoreBackground.tryAcquire(1)) {
140 Q_ASSERT(!m_threadBackground.isRunning());
142 m_newBackgroundFrames.fetchAndStoreRelaxed(0);
143 m_newBackgroundUpdates.fetchAndStoreRelaxed(0);
144 m_threadBackground = QtConcurrent::run(this, &AbstractScopeWidget::renderBackground, m_accelFactorBackground);
145 qDebug() << "Background thread started in " << widgetName();
148 qDebug() << "Background semaphore locked, not prodding in " << widgetName() << ". Thread running: " << m_threadBackground.isRunning();
153 void AbstractScopeWidget::forceUpdate()
155 m_newHUDUpdates.fetchAndAddRelaxed(1);
156 m_newScopeUpdates.fetchAndAddRelaxed(1);
157 m_newBackgroundUpdates.fetchAndAddRelaxed(1);
160 prodBackgroundThread();
162 void AbstractScopeWidget::forceUpdateHUD()
164 m_newHUDUpdates.fetchAndAddRelaxed(1);
168 void AbstractScopeWidget::forceUpdateScope()
170 m_newScopeUpdates.fetchAndAddRelaxed(1);
174 void AbstractScopeWidget::forceUpdateBackground()
176 m_newBackgroundUpdates.fetchAndAddRelaxed(1);
177 prodBackgroundThread();
184 void AbstractScopeWidget::mouseReleaseEvent(QMouseEvent *event)
188 prodBackgroundThread();
189 QWidget::mouseReleaseEvent(event);
192 void AbstractScopeWidget::resizeEvent(QResizeEvent *event)
194 // Update the dimension of the available rect for painting
195 m_scopeRect = scopeRect();
199 QWidget::resizeEvent(event);
202 void AbstractScopeWidget::raise()
204 // Widget has been brought to the top of a widget stack,
205 // layers need to be updated.
210 void AbstractScopeWidget::showEvent(QShowEvent *event)
212 m_scopeRect = scopeRect();
213 QWidget::showEvent(event);
214 m_scopeRect = scopeRect();
217 void AbstractScopeWidget::paintEvent(QPaintEvent *)
219 QPainter davinci(this);
220 davinci.drawImage(m_scopeRect.topLeft(), m_imgBackground);
221 davinci.drawImage(m_scopeRect.topLeft(), m_imgScope);
222 davinci.drawImage(m_scopeRect.topLeft(), m_imgHUD);
225 void AbstractScopeWidget::customContextMenuRequested(const QPoint &pos)
227 m_menu->exec(this->mapToGlobal(pos));
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 ); }
237 void AbstractScopeWidget::slotHUDRenderingFinished(uint mseconds, uint oldFactor)
239 qDebug() << "HUD rendering has finished, waiting for termination in " << widgetName();
240 m_threadHUD.waitForFinished();
241 m_imgHUD = m_threadHUD.result();
243 m_semaphoreHUD.release(1);
247 if (m_aRealtime->isChecked()) {
248 accel = calculateAccelFactorHUD(mseconds, oldFactor);
249 if (m_accelFactorHUD < 1) {
252 m_accelFactorHUD = accel;
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;
262 void AbstractScopeWidget::slotScopeRenderingFinished(uint mseconds, uint oldFactor)
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();
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);
275 // Calculate the acceleration factor hint to get «realtime» updates.
277 if (m_aRealtime->isChecked()) {
278 accel = calculateAccelFactorScope(mseconds, oldFactor);
279 if (m_accelFactorScope < 1) {
280 // If mseconds happens to be 0.
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;
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;
296 void AbstractScopeWidget::slotBackgroundRenderingFinished(uint mseconds, uint oldFactor)
298 qDebug() << "Background rendering has finished, waiting for termination in " << widgetName();
299 m_threadBackground.waitForFinished();
300 m_imgBackground = m_threadBackground.result();
302 m_semaphoreBackground.release(1);
306 if (m_aRealtime->isChecked()) {
307 accel = calculateAccelFactorBackground(mseconds, oldFactor);
308 if (m_accelFactorBackground < 1) {
311 m_accelFactorBackground = accel;
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();;
321 void AbstractScopeWidget::slotActiveMonitorChanged(bool isClipMonitor)
323 qDebug() << "Active monitor has changed in " << widgetName() << ". Is the clip monitor active now? " << isClipMonitor;
325 bool b = m_activeRender->disconnect(this);
328 m_activeRender = (isClipMonitor) ? m_clipMonitor->render : m_projMonitor->render;
330 b &= connect(m_activeRender, SIGNAL(rendererPosition(int)), this, SLOT(slotRenderZoneUpdated()));
331 b &= connect(m_activeRender, SIGNAL(frameUpdated(int)), this, SLOT(slotRenderZoneUpdated()));
334 // Update the scope for the new monitor.
337 prodBackgroundThread();
340 void AbstractScopeWidget::slotRenderZoneUpdated()
342 m_newHUDFrames.fetchAndAddRelaxed(1);
343 m_newScopeFrames.fetchAndAddRelaxed(1);
344 m_newBackgroundFrames.fetchAndAddRelaxed(1);
346 qDebug() << "Monitor incoming. New frames total HUD/Scope/Background: " << m_newHUDFrames
347 << "/" << m_newScopeFrames << "/" << m_newBackgroundFrames;
349 if (this->visibleRegion().isEmpty()) {
350 qDebug() << "Scope of widget " << widgetName() << " is not at the top, not rendering.";
352 if (m_aAutoRefresh->isChecked()) {
355 prodBackgroundThread();
360 void AbstractScopeWidget::slotResetRealtimeFactor(bool realtimeChecked)
362 if (!realtimeChecked) {
363 m_accelFactorHUD = 1;
364 m_accelFactorScope = 1;
365 m_accelFactorBackground = 1;
369 void AbstractScopeWidget::slotAutoRefreshToggled(bool autoRefresh)
371 // TODO only if depends on input