Merge pull request #3112 from koying/fixcurlopenssl
[vuplus_xbmc] / xbmc / video / VideoReferenceClock.h
1 #pragma once
2 /*
3  *      Copyright (C) 2005-2013 Team XBMC
4  *      http://xbmc.org
5  *
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)
9  *  any later version.
10  *
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.
15  *
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/>.
19  *
20  */
21
22 #include "system.h" // for HAS_XRANDR, and Win32 types
23 #include "threads/Thread.h"
24 #include "threads/CriticalSection.h"
25
26 //TODO: get rid of #ifdef hell, abstract implementations in separate classes
27
28 #if defined(HAS_GLX) && defined(HAS_XRANDR)
29   #include "system_gl.h"
30   #include <X11/X.h>
31   #include <X11/Xlib.h>
32   #include <GL/glx.h>
33 #elif defined(TARGET_WINDOWS) && defined(HAS_DX)
34   #include <d3d9.h>
35   #include "guilib/D3DResource.h"
36
37 class CD3DCallback : public ID3DResource
38 {
39   public:
40     void Reset();
41     void OnDestroyDevice();
42     void OnCreateDevice();
43     void Aquire();
44     void Release();
45     bool IsValid();
46
47   private:
48     bool m_devicevalid;
49     bool m_deviceused;
50
51     CCriticalSection m_critsection;
52     CEvent           m_createevent;
53     CEvent           m_releaseevent;
54 };
55
56 #endif
57
58 class CVideoReferenceClock : public CThread
59 {
60   public:
61     CVideoReferenceClock();
62
63     int64_t GetTime(bool interpolated = true);
64     int64_t GetFrequency();
65     void    SetSpeed(double Speed);
66     double  GetSpeed();
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; }
73
74 #if defined(TARGET_DARWIN)
75     void VblankHandler(int64_t nowtime, double fps);
76 #endif
77
78   private:
79     void    Process();
80     bool    UpdateRefreshrate(bool Forced = false);
81     void    SendVblankSignal();
82     void    UpdateClock(int NrVBlanks, bool CheckMissed);
83     double  UpdateInterval();
84     int64_t TimeOfNextVblank();
85
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
93     double  m_fineadjust;
94
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
102
103     CEvent  m_Started;            //set when the vblank clock is started
104     CEvent  m_VblankEvent;        //set when a vblank happens
105
106     CCriticalSection m_CritSection;
107
108 #if defined(HAS_GLX) && defined(HAS_XRANDR)
109     bool SetupGLX();
110     void RunGLX();
111     void CleanupGLX();
112     bool ParseNvSettings(int& RefreshRate);
113     int  GetRandRRate();
114
115     int  (*m_glXWaitVideoSyncSGI) (int, int, unsigned int*);
116     int  (*m_glXGetVideoSyncSGI)  (unsigned int*);
117
118     Display*     m_Dpy;
119     XVisualInfo *m_vInfo;
120     Window       m_Window;
121     GLXContext   m_Context;
122     Pixmap       m_pixmap;
123     GLXPixmap    m_glPixmap;
124     int          m_RREventBase;
125
126     bool         m_UseNvSettings;
127     bool         m_bIsATI;
128
129 #elif defined(TARGET_WINDOWS) && defined(HAS_DX)
130     bool   SetupD3D();
131     double MeasureRefreshrate(int MSecs);
132     void   RunD3D();
133     void   CleanupD3D();
134
135     LPDIRECT3DDEVICE9 m_D3dDev;
136     CD3DCallback      m_D3dCallback;
137
138     unsigned int  m_Width;
139     unsigned int  m_Height;
140     bool          m_Interlaced;
141
142 #elif defined(TARGET_DARWIN)
143     bool SetupCocoa();
144     void RunCocoa();
145     void CleanupCocoa();
146
147     int64_t m_LastVBlankTime;  //timestamp of the last vblank, used for calculating how many vblanks happened
148                                //not the same as m_VblankTime
149 #endif
150 };
151
152 extern CVideoReferenceClock g_VideoReferenceClock;