2018-08-15 02:13:17 +00:00
|
|
|
#include "pacer.h"
|
|
|
|
|
2018-08-16 05:02:15 +00:00
|
|
|
#ifdef Q_OS_DARWIN
|
|
|
|
#include "displaylinkvsyncsource.h"
|
|
|
|
#endif
|
|
|
|
|
2018-08-16 06:20:56 +00:00
|
|
|
#ifdef Q_OS_WIN32
|
|
|
|
#include "dxvsyncsource.h"
|
|
|
|
#endif
|
|
|
|
|
2018-08-15 02:13:17 +00:00
|
|
|
#define FRAME_HISTORY_ENTRIES 8
|
|
|
|
|
2018-08-16 05:02:15 +00:00
|
|
|
Pacer::Pacer(IVsyncRenderer* renderer) :
|
2018-08-16 04:10:35 +00:00
|
|
|
m_FrameQueueLock(0),
|
2018-08-16 05:02:15 +00:00
|
|
|
m_VsyncSource(nullptr),
|
|
|
|
m_VsyncRenderer(renderer),
|
2018-08-16 04:10:35 +00:00
|
|
|
m_MaxVideoFps(0),
|
|
|
|
m_DisplayFps(0)
|
2018-08-15 02:13:17 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
Pacer::~Pacer()
|
|
|
|
{
|
2018-08-16 05:02:15 +00:00
|
|
|
drain();
|
2018-08-16 04:10:35 +00:00
|
|
|
}
|
|
|
|
|
2018-08-16 05:02:15 +00:00
|
|
|
// Called in an arbitrary thread by the IVsyncSource on V-sync
|
|
|
|
// or an event synchronized with V-sync
|
|
|
|
void Pacer::vsyncCallback()
|
2018-08-15 02:13:17 +00:00
|
|
|
{
|
2018-08-16 04:10:35 +00:00
|
|
|
// Make sure initialize() has been called
|
|
|
|
SDL_assert(m_MaxVideoFps != 0);
|
2018-08-15 02:13:17 +00:00
|
|
|
|
2018-08-16 04:10:35 +00:00
|
|
|
SDL_AtomicLock(&m_FrameQueueLock);
|
2018-08-15 02:13:17 +00:00
|
|
|
|
|
|
|
// If the queue length history entries are large, be strict
|
|
|
|
// about dropping excess frames.
|
2018-08-16 04:10:35 +00:00
|
|
|
int frameDropTarget = 1;
|
|
|
|
|
|
|
|
// If we may get more frames per second than we can display, use
|
|
|
|
// frame history to drop frames only if consistently above the
|
|
|
|
// one queued frame mark.
|
|
|
|
if (m_MaxVideoFps >= m_DisplayFps) {
|
|
|
|
for (int i = 0; i < m_FrameQueueHistory.count(); i++) {
|
|
|
|
if (m_FrameQueueHistory[i] <= 1) {
|
|
|
|
// Be lenient as long as the queue length
|
|
|
|
// resolves before the end of frame history
|
|
|
|
frameDropTarget = 3;
|
2018-08-16 05:02:15 +00:00
|
|
|
break;
|
2018-08-16 04:10:35 +00:00
|
|
|
}
|
2018-08-15 02:13:17 +00:00
|
|
|
}
|
|
|
|
|
2018-08-16 04:10:35 +00:00
|
|
|
if (m_FrameQueueHistory.count() == FRAME_HISTORY_ENTRIES) {
|
|
|
|
m_FrameQueueHistory.dequeue();
|
|
|
|
}
|
2018-08-15 02:13:17 +00:00
|
|
|
|
2018-08-16 04:10:35 +00:00
|
|
|
m_FrameQueueHistory.enqueue(m_FrameQueue.count());
|
|
|
|
}
|
2018-08-15 02:13:17 +00:00
|
|
|
|
|
|
|
// Catch up if we're several frames ahead
|
|
|
|
while (m_FrameQueue.count() > frameDropTarget) {
|
|
|
|
AVFrame* frame = m_FrameQueue.dequeue();
|
|
|
|
av_frame_free(&frame);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m_FrameQueue.isEmpty()) {
|
|
|
|
SDL_AtomicUnlock(&m_FrameQueueLock);
|
2018-08-16 05:02:15 +00:00
|
|
|
|
|
|
|
// Nothing to render at this time
|
|
|
|
return;
|
2018-08-15 02:13:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Grab the first frame
|
|
|
|
AVFrame* frame = m_FrameQueue.dequeue();
|
|
|
|
SDL_AtomicUnlock(&m_FrameQueueLock);
|
|
|
|
|
2018-08-16 05:02:15 +00:00
|
|
|
// Render it
|
|
|
|
m_VsyncRenderer->renderFrameAtVsync(frame);
|
|
|
|
|
|
|
|
// Free the frame
|
|
|
|
av_frame_free(&frame);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Pacer::initialize(SDL_Window* window, int maxVideoFps)
|
|
|
|
{
|
|
|
|
m_MaxVideoFps = maxVideoFps;
|
|
|
|
|
|
|
|
int displayIndex = SDL_GetWindowDisplayIndex(window);
|
|
|
|
if (displayIndex < 0) {
|
|
|
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
|
|
|
|
"Failed to get current display: %s",
|
|
|
|
SDL_GetError());
|
|
|
|
|
|
|
|
// Assume display 0 if it fails
|
|
|
|
displayIndex = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
SDL_DisplayMode mode;
|
|
|
|
if (SDL_GetCurrentDisplayMode(displayIndex, &mode) == 0) {
|
|
|
|
// May be zero if undefined
|
|
|
|
m_DisplayFps = mode.refresh_rate;
|
|
|
|
}
|
|
|
|
|
|
|
|
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION,
|
|
|
|
"Frame pacing: target %d Hz with %d FPS stream",
|
|
|
|
m_DisplayFps, m_MaxVideoFps);
|
|
|
|
|
2018-08-16 06:20:56 +00:00
|
|
|
#if defined(Q_OS_DARWIN)
|
2018-08-16 05:02:15 +00:00
|
|
|
m_VsyncSource = new DisplayLinkVsyncSource(this);
|
2018-08-16 06:20:56 +00:00
|
|
|
#elif defined(Q_OS_WIN32)
|
|
|
|
m_VsyncSource = new DxVsyncSource(this);
|
2018-08-16 05:02:15 +00:00
|
|
|
#else
|
|
|
|
SDL_assert(false);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return m_VsyncSource->initialize(window);
|
2018-08-15 02:13:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Pacer::submitFrame(AVFrame* frame)
|
|
|
|
{
|
2018-08-16 04:10:35 +00:00
|
|
|
// Make sure initialize() has been called
|
|
|
|
SDL_assert(m_MaxVideoFps != 0);
|
|
|
|
|
2018-08-15 02:13:17 +00:00
|
|
|
SDL_AtomicLock(&m_FrameQueueLock);
|
|
|
|
m_FrameQueue.enqueue(frame);
|
|
|
|
SDL_AtomicUnlock(&m_FrameQueueLock);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Pacer::drain()
|
|
|
|
{
|
2018-08-16 06:20:56 +00:00
|
|
|
// Stop V-sync callbacks
|
|
|
|
delete m_VsyncSource;
|
|
|
|
m_VsyncSource = nullptr;
|
|
|
|
|
2018-08-15 02:13:17 +00:00
|
|
|
while (!m_FrameQueue.isEmpty()) {
|
|
|
|
AVFrame* frame = m_FrameQueue.dequeue();
|
|
|
|
av_frame_free(&frame);
|
|
|
|
}
|
|
|
|
}
|