]> git.sesse.net Git - kdenlive/blob - src/abstractscopewidget.cpp
AbstractScopeWidget changes:
[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_semaphoreHUD(1),
35     m_semaphoreScope(1),
36     m_semaphoreBackground(1),
37     m_accelFactorHUD(1),
38     m_accelFactorScope(1),
39     m_accelFactorBackground(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     if (m_projMonitor->isActive()) {
66         m_activeRender = m_projMonitor->render;
67     } else {
68         m_activeRender = m_clipMonitor->render;
69     }
70
71     bool b = true;
72
73     b &= connect(this, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(customContextMenuRequested(QPoint)));
74
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)));
80
81     Q_ASSERT(b);
82 }
83
84 AbstractScopeWidget::~AbstractScopeWidget()
85 {
86     delete m_menu;
87     delete m_aAutoRefresh;
88 }
89
90 void AbstractScopeWidget::prodHUDThread() {}
91
92 void AbstractScopeWidget::prodScopeThread()
93 {
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());
99
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();
104
105     } else {
106         qDebug() << "Scope semaphore locked, not prodding in " << widgetName() << ". Thread running: " << m_threadScope.isRunning();
107     }
108 }
109
110 void AbstractScopeWidget::prodBackgroundThread() {}
111
112
113 ///// Events /////
114
115 void AbstractScopeWidget::mouseReleaseEvent(QMouseEvent *event)
116 {
117     prodHUDThread();
118     prodScopeThread();
119     prodBackgroundThread();
120     QWidget::mouseReleaseEvent(event);
121 }
122
123 void AbstractScopeWidget::resizeEvent(QResizeEvent *event)
124 {
125     // Update the dimension of the available rect for painting
126     m_scopeRect = scopeRect();
127
128     m_newHUDUpdates.fetchAndAddRelaxed(1);
129     m_newScopeUpdates.fetchAndAddRelaxed(1);
130     m_newBackgroundUpdates.fetchAndAddRelaxed(1);
131
132     prodHUDThread();
133     prodScopeThread();
134     prodBackgroundThread();
135
136     QWidget::resizeEvent(event);
137 }
138
139 void AbstractScopeWidget::paintEvent(QPaintEvent *)
140 {
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
146         // it here instead.
147         m_scopeRect = scopeRect();
148         initialDimensionUpdateDone = true;
149     }
150
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)));
156 }
157
158 void AbstractScopeWidget::customContextMenuRequested(const QPoint &pos)
159 {
160     m_menu->exec(this->mapToGlobal(pos));
161 }
162
163
164 ///// Slots /////
165
166 void AbstractScopeWidget::slotHUDRenderingFinished(uint, uint) {}
167
168 void AbstractScopeWidget::slotScopeRenderingFinished(uint mseconds, uint)
169 {
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();
175
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);
179     this->update();
180
181     // Calculate the acceleration factor hint to get «realtime» updates.
182     int accel;
183     if (m_aRealtime->isChecked()) {
184         accel = ceil((float)mseconds*REALTIME_FPS/1000 );
185         if (m_accelFactorScope < 1) {
186             // If mseconds is 0.
187             accel = 1;
188         }
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;
193     }
194
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;
198         prodScopeThread();
199     }
200 }
201
202 void AbstractScopeWidget::slotBackgroundRenderingFinished(uint, uint) {}
203
204 void AbstractScopeWidget::slotActiveMonitorChanged(bool isClipMonitor)
205 {
206     qDebug() << "Active monitor has changed in " << widgetName() << ". Is the clip monitor active now? " << isClipMonitor;
207
208     bool disconnected = m_activeRender->disconnect(this);
209     Q_ASSERT(disconnected);
210
211     m_activeRender = (isClipMonitor) ? m_clipMonitor->render : m_projMonitor->render;
212
213     connect(m_activeRender, SIGNAL(rendererPosition(int)), this, SLOT(slotRenderZoneUpdated()));
214     connect(m_activeRender, SIGNAL(rendererPositionBefore0()), this, SLOT(slotRenderZoneUpdated()));
215
216     // Update the scope for the new monitor.
217     prodHUDThread();
218     prodScopeThread();
219     prodBackgroundThread();
220 }
221
222 void AbstractScopeWidget::slotRenderZoneUpdated()
223 {
224     m_newHUDFrames.fetchAndAddRelaxed(1);
225     m_newScopeFrames.fetchAndAddRelaxed(1);
226     m_newBackgroundFrames.fetchAndAddRelaxed(1);
227
228     qDebug() << "Monitor incoming. New frames total HUD/Scope/Background: " << m_newHUDFrames
229             << "/" << m_newScopeFrames << "/" << m_newBackgroundFrames;
230
231     if (this->visibleRegion().isEmpty()) {
232         qDebug() << "Scope of widget " << widgetName() << " is not at the top, not rendering.";
233     } else {
234         if (m_aAutoRefresh->isChecked()) {
235             prodHUDThread();
236             prodScopeThread();
237             prodBackgroundThread();
238         }
239     }
240 }
241
242 void AbstractScopeWidget::slotResetRealtimeFactor(bool realtimeChecked)
243 {
244     if (!realtimeChecked) {
245         m_accelFactorHUD = 1;
246         m_accelFactorScope = 1;
247         m_accelFactorBackground = 1;
248     }
249 }