3 * Copyright (C) 2005-2013 Team XBMC
6 * This Program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2, or (at your option)
11 * This Program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with XBMC; see the file COPYING. If not, see
18 * <http://www.gnu.org/licenses/>.
22 #include "system.h" // for HAS_XRANDR, and Win32 types
23 #include "threads/Thread.h"
24 #include "threads/CriticalSection.h"
26 //TODO: get rid of #ifdef hell, abstract implementations in separate classes
28 #if defined(HAS_GLX) && defined(HAS_XRANDR)
29 #include "system_gl.h"
33 #elif defined(TARGET_WINDOWS) && defined(HAS_DX)
35 #include "guilib/D3DResource.h"
37 class CD3DCallback : public ID3DResource
41 void OnDestroyDevice();
42 void OnCreateDevice();
51 CCriticalSection m_critsection;
53 CEvent m_releaseevent;
58 class CVideoReferenceClock : public CThread
61 CVideoReferenceClock();
63 int64_t GetTime(bool interpolated = true);
64 int64_t GetFrequency();
65 void SetSpeed(double Speed);
67 int GetRefreshRate(double* interval = NULL);
68 int64_t Wait(int64_t Target);
69 bool WaitStarted(int MSecs);
70 bool GetClockInfo(int& MissedVblanks, double& ClockSpeed, int& RefreshRate);
71 void SetFineAdjust(double fineadjust);
72 void RefreshChanged() { m_RefreshChanged = 1; }
74 #if defined(TARGET_DARWIN)
75 void VblankHandler(int64_t nowtime, double fps);
80 bool UpdateRefreshrate(bool Forced = false);
81 void SendVblankSignal();
82 void UpdateClock(int NrVBlanks, bool CheckMissed);
83 double UpdateInterval();
84 int64_t TimeOfNextVblank();
86 int64_t m_CurrTime; //the current time of the clock when using vblank as clock source
87 int64_t m_LastIntTime; //last interpolated clock value, to make sure the clock doesn't go backwards
88 double m_CurrTimeFract; //fractional part that is lost due to rounding when updating the clock
89 double m_ClockSpeed; //the frequency of the clock set by dvdplayer
90 int64_t m_ClockOffset; //the difference between the vblank clock and systemclock, set when vblank clock is stopped
91 int64_t m_LastRefreshTime; //last time we updated the refreshrate
92 int64_t m_SystemFrequency; //frequency of the systemclock
95 bool m_UseVblank; //set to true when vblank is used as clock source
96 int64_t m_RefreshRate; //current refreshrate
97 int m_PrevRefreshRate; //previous refreshrate, used for log printing and getting refreshrate from nvidia-settings
98 int m_MissedVblanks; //number of clock updates missed by the vblank clock
99 int m_RefreshChanged; //1 = we changed the refreshrate, 2 = we should check the refreshrate forced
100 int m_TotalMissedVblanks;//total number of clock updates missed, used by codec information screen
101 int64_t m_VblankTime; //last time the clock was updated when using vblank as clock
103 CEvent m_Started; //set when the vblank clock is started
104 CEvent m_VblankEvent; //set when a vblank happens
106 CCriticalSection m_CritSection;
108 #if defined(HAS_GLX) && defined(HAS_XRANDR)
112 bool ParseNvSettings(int& RefreshRate);
115 int (*m_glXWaitVideoSyncSGI) (int, int, unsigned int*);
116 int (*m_glXGetVideoSyncSGI) (unsigned int*);
119 XVisualInfo *m_vInfo;
121 GLXContext m_Context;
123 GLXPixmap m_glPixmap;
126 bool m_UseNvSettings;
129 #elif defined(TARGET_WINDOWS) && defined(HAS_DX)
131 double MeasureRefreshrate(int MSecs);
135 LPDIRECT3DDEVICE9 m_D3dDev;
136 CD3DCallback m_D3dCallback;
138 unsigned int m_Width;
139 unsigned int m_Height;
142 #elif defined(TARGET_DARWIN)
147 int64_t m_LastVBlankTime; //timestamp of the last vblank, used for calculating how many vblanks happened
148 //not the same as m_VblankTime
152 extern CVideoReferenceClock g_VideoReferenceClock;