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),
36 m_semaphoreBackground(1),
38 m_accelFactorScope(1),
39 m_accelFactorBackground(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 if (m_projMonitor->isActive()) {
66 m_activeRender = m_projMonitor->render;
68 m_activeRender = m_clipMonitor->render;
73 b &= connect(this, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(customContextMenuRequested(QPoint)));
75 b &= connect(m_activeRender, SIGNAL(rendererPosition(int)), this, SLOT(slotRenderZoneUpdated()));
76 b &= connect(this, SIGNAL(signalHUDRenderingFinished(uint,uint)), this, SLOT(slotHUDRenderingFinished(uint,uint)));
77 b &= connect(this, SIGNAL(signalScopeRenderingFinished(uint,uint)), this, SLOT(slotScopeRenderingFinished(uint,uint)));
78 b &= connect(this, SIGNAL(signalBackgroundRenderingFinished(uint,uint)), this, SLOT(slotBackgroundRenderingFinished(uint,uint)));
79 b &= connect(m_aRealtime, SIGNAL(toggled(bool)), this, SLOT(slotResetRealtimeFactor(bool)));
84 AbstractScopeWidget::~AbstractScopeWidget()
87 delete m_aAutoRefresh;
90 void AbstractScopeWidget::prodHUDThread() {}
92 void AbstractScopeWidget::prodScopeThread()
94 // Try to acquire the semaphore. This must only succeed if m_threadScope is not running
95 // anymore. Therefore the semaphore must NOT be released before m_threadScope ends.
96 // If acquiring the semaphore fails, the thread is still running.
97 if (m_semaphoreScope.tryAcquire(1)) {
98 Q_ASSERT(!m_threadScope.isRunning());
100 m_newScopeFrames.fetchAndStoreRelaxed(0);
101 m_newScopeUpdates.fetchAndStoreRelaxed(0);
102 m_threadScope = QtConcurrent::run(this, &AbstractScopeWidget::renderScope, m_accelFactorScope);
103 qDebug() << "Scope thread started in " << widgetName();
106 qDebug() << "Scope semaphore locked, not prodding in " << widgetName() << ". Thread running: " << m_threadScope.isRunning();
110 void AbstractScopeWidget::prodBackgroundThread() {}
115 void AbstractScopeWidget::mouseReleaseEvent(QMouseEvent *event)
119 prodBackgroundThread();
120 QWidget::mouseReleaseEvent(event);
123 void AbstractScopeWidget::resizeEvent(QResizeEvent *event)
125 // Update the dimension of the available rect for painting
126 m_scopeRect = scopeRect();
128 m_newHUDUpdates.fetchAndAddRelaxed(1);
129 m_newScopeUpdates.fetchAndAddRelaxed(1);
130 m_newBackgroundUpdates.fetchAndAddRelaxed(1);
134 prodBackgroundThread();
136 QWidget::resizeEvent(event);
139 void AbstractScopeWidget::paintEvent(QPaintEvent *)
141 // qDebug() << "Painting now on scope " << widgetName();
142 if (!initialDimensionUpdateDone) {
143 // This is a workaround.
144 // When updating the dimensions in the constructor, the size
145 // of the control items at the top are simply ignored! So do
147 m_scopeRect = scopeRect();
148 initialDimensionUpdateDone = true;
151 QPainter davinci(this);
152 davinci.drawImage(scopeRect().topLeft(), m_imgBackground);
153 davinci.drawImage(scopeRect().topLeft(), m_imgScope);
154 davinci.drawImage(scopeRect().topLeft(), m_imgHUD);
155 davinci.fillRect(scopeRect(), QBrush(QColor(200, 100, 0, 16)));
158 void AbstractScopeWidget::customContextMenuRequested(const QPoint &pos)
160 m_menu->exec(this->mapToGlobal(pos));
166 void AbstractScopeWidget::slotHUDRenderingFinished(uint, uint) {}
168 void AbstractScopeWidget::slotScopeRenderingFinished(uint mseconds, uint)
170 // The signal can be received before the thread has really finished. So we
171 // need to wait until it has really finished before starting a new thread.
172 qDebug() << "Scope rendering has finished, waiting for termination in " << widgetName();
173 m_threadScope.waitForFinished();
174 m_imgScope = m_threadScope.result();
176 // The scope thread has finished. Now we can release the semaphore, allowing a new thread.
177 // See prodScopeThread where the semaphore is acquired again.
178 m_semaphoreScope.release(1);
181 // Calculate the acceleration factor hint to get «realtime» updates.
183 if (m_aRealtime->isChecked()) {
184 accel = ceil((float)mseconds*REALTIME_FPS/1000 );
185 if (m_accelFactorScope < 1) {
189 // Don't directly calculate with m_accelFactorScope as we are dealing with concurrency.
190 // If m_accelFactorScope is set to 0 at the wrong moment, who knows what might happen
191 // then :) Therefore use a local variable.
192 m_accelFactorScope = accel;
195 if ( (m_newScopeFrames > 0 && m_aAutoRefresh->isChecked()) || m_newScopeUpdates > 0) {
196 qDebug() << "Trying to start a new thread for " << widgetName()
197 << ". New frames/updates: " << m_newScopeFrames << "/" << m_newScopeUpdates;
202 void AbstractScopeWidget::slotBackgroundRenderingFinished(uint, uint) {}
204 void AbstractScopeWidget::slotActiveMonitorChanged(bool isClipMonitor)
206 qDebug() << "Active monitor has changed in " << widgetName() << ". Is the clip monitor active now? " << isClipMonitor;
208 bool disconnected = m_activeRender->disconnect(this);
209 Q_ASSERT(disconnected);
211 m_activeRender = (isClipMonitor) ? m_clipMonitor->render : m_projMonitor->render;
213 connect(m_activeRender, SIGNAL(rendererPosition(int)), this, SLOT(slotRenderZoneUpdated()));
214 connect(m_activeRender, SIGNAL(rendererPositionBefore0()), this, SLOT(slotRenderZoneUpdated()));
216 // Update the scope for the new monitor.
219 prodBackgroundThread();
222 void AbstractScopeWidget::slotRenderZoneUpdated()
224 m_newHUDFrames.fetchAndAddRelaxed(1);
225 m_newScopeFrames.fetchAndAddRelaxed(1);
226 m_newBackgroundFrames.fetchAndAddRelaxed(1);
228 qDebug() << "Monitor incoming. New frames total HUD/Scope/Background: " << m_newHUDFrames
229 << "/" << m_newScopeFrames << "/" << m_newBackgroundFrames;
231 if (this->visibleRegion().isEmpty()) {
232 qDebug() << "Scope of widget " << widgetName() << " is not at the top, not rendering.";
234 if (m_aAutoRefresh->isChecked()) {
237 prodBackgroundThread();
242 void AbstractScopeWidget::slotResetRealtimeFactor(bool realtimeChecked)
244 if (!realtimeChecked) {
245 m_accelFactorHUD = 1;
246 m_accelFactorScope = 1;
247 m_accelFactorBackground = 1;