Add PoC VsyncSource for Windows using DwmFlush(). Soon to be replaced with D3DKMTWaitForVerticalBlankEvent().

This commit is contained in:
Cameron Gutman
2018-08-15 23:20:56 -07:00
parent e68a15c825
commit f6a451d6e7
8 changed files with 108 additions and 15 deletions
@@ -0,0 +1,49 @@
#include "dxvsyncsource.h"
#include <dwmapi.h>
DxVsyncSource::DxVsyncSource(Pacer* pacer) :
m_Pacer(pacer),
m_Thread(nullptr)
{
SDL_AtomicSet(&m_Stopping, 0);
}
DxVsyncSource::~DxVsyncSource()
{
if (m_Thread != nullptr) {
SDL_AtomicSet(&m_Stopping, 1);
SDL_WaitThread(m_Thread, nullptr);
}
}
bool DxVsyncSource::initialize(SDL_Window* window)
{
m_Thread = SDL_CreateThread(vsyncThread, "DX Vsync Thread", this);
if (m_Thread == nullptr) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
"Unable to create DX V-sync thread: %s",
SDL_GetError());
return false;
}
return true;
}
int DxVsyncSource::vsyncThread(void* context)
{
DxVsyncSource* me = reinterpret_cast<DxVsyncSource*>(context);
SDL_SetThreadPriority(SDL_THREAD_PRIORITY_HIGH);
while (SDL_AtomicGet(&me->m_Stopping) == 0) {
// FIXME: We should really use D3DKMTWaitForVerticalBlankEvent() instead!
// https://bugs.chromium.org/p/chromium/issues/detail?id=467617
// https://chromium.googlesource.com/chromium/src.git/+/c564f2fe339b2b2abb0c8773c90c83215670ea71/gpu/ipc/service/gpu_vsync_provider_win.cc
DwmFlush();
me->m_Pacer->vsyncCallback();
}
return 0;
}
@@ -0,0 +1,20 @@
#pragma once
#include "pacer.h"
class DxVsyncSource : public IVsyncSource
{
public:
DxVsyncSource(Pacer* pacer);
virtual ~DxVsyncSource();
virtual bool initialize(SDL_Window* window);
private:
static int vsyncThread(void* context);
Pacer* m_Pacer;
SDL_Thread* m_Thread;
SDL_atomic_t m_Stopping;
};
@@ -4,6 +4,10 @@
#include "displaylinkvsyncsource.h"
#endif
#ifdef Q_OS_WIN32
#include "dxvsyncsource.h"
#endif
#define FRAME_HISTORY_ENTRIES 8
Pacer::Pacer(IVsyncRenderer* renderer) :
@@ -18,9 +22,6 @@ Pacer::Pacer(IVsyncRenderer* renderer) :
Pacer::~Pacer()
{
// Stop V-sync callbacks
delete m_VsyncSource;
drain();
}
@@ -105,8 +106,10 @@ bool Pacer::initialize(SDL_Window* window, int maxVideoFps)
"Frame pacing: target %d Hz with %d FPS stream",
m_DisplayFps, m_MaxVideoFps);
#ifdef Q_OS_DARWIN
#if defined(Q_OS_DARWIN)
m_VsyncSource = new DisplayLinkVsyncSource(this);
#elif defined(Q_OS_WIN32)
m_VsyncSource = new DxVsyncSource(this);
#else
SDL_assert(false);
#endif
@@ -126,6 +129,10 @@ void Pacer::submitFrame(AVFrame* frame)
void Pacer::drain()
{
// Stop V-sync callbacks
delete m_VsyncSource;
m_VsyncSource = nullptr;
while (!m_FrameQueue.isEmpty()) {
AVFrame* frame = m_FrameQueue.dequeue();
av_frame_free(&frame);