Merge pull request #3195 from afedchin/win_vsync_def
[vuplus_xbmc] / xbmc / cores / VideoRenderers / RenderManager.h
1 #pragma once
2
3 /*
4  *      Copyright (C) 2005-2013 Team XBMC
5  *      http://xbmc.org
6  *
7  *  This Program is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License as published by
9  *  the Free Software Foundation; either version 2, or (at your option)
10  *  any later version.
11  *
12  *  This Program is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  *  GNU General Public License for more details.
16  *
17  *  You should have received a copy of the GNU General Public License
18  *  along with XBMC; see the file COPYING.  If not, see
19  *  <http://www.gnu.org/licenses/>.
20  *
21  */
22
23 #include <list>
24
25 #include "cores/VideoRenderers/BaseRenderer.h"
26 #include "guilib/Geometry.h"
27 #include "guilib/Resolution.h"
28 #include "threads/SharedSection.h"
29 #include "threads/Thread.h"
30 #include "settings/VideoSettings.h"
31 #include "OverlayRenderer.h"
32 #include <deque>
33 #include "PlatformDefs.h"
34
35 class CRenderCapture;
36
37 namespace DXVA { class CProcessor; }
38 namespace VAAPI { class CSurfaceHolder; }
39 namespace VDPAU { class CVdpauRenderPicture; }
40 struct DVDVideoPicture;
41
42 #define ERRORBUFFSIZE 30
43
44 class CWinRenderer;
45 class CLinuxRenderer;
46 class CLinuxRendererGL;
47 class CLinuxRendererGLES;
48
49 class CXBMCRenderManager
50 {
51 public:
52   CXBMCRenderManager();
53   ~CXBMCRenderManager();
54
55   // Functions called from the GUI
56   void GetVideoRect(CRect &source, CRect &dest);
57   float GetAspectRatio();
58   void Update();
59   void FrameMove();
60   void FrameFinish();
61   bool FrameWait(int ms);
62   void Render(bool clear, DWORD flags = 0, DWORD alpha = 255);
63   void SetupScreenshot();
64
65   CRenderCapture* AllocRenderCapture();
66   void ReleaseRenderCapture(CRenderCapture* capture);
67   void Capture(CRenderCapture *capture, unsigned int width, unsigned int height, int flags);
68   void ManageCaptures();
69
70   void SetViewMode(int iViewMode);
71
72   // Functions called from mplayer
73   /**
74    * Called by video player to configure renderer
75    * @param width width of decoded frame
76    * @param height height of decoded frame
77    * @param d_width displayed width of frame (aspect ratio)
78    * @param d_height displayed height of frame
79    * @param fps frames per second of video
80    * @param flags see RenderFlags.h
81    * @param format see RenderFormats.h
82    * @param extended_format used by DXVA
83    * @param orientation
84    * @param numbers of kept buffer references
85    */
86   bool Configure(unsigned int width, unsigned int height, unsigned int d_width, unsigned int d_height, float fps, unsigned flags, ERenderFormat format, unsigned extended_format,  unsigned int orientation, int buffers = 0);
87   bool IsConfigured() const;
88
89   int AddVideoPicture(DVDVideoPicture& picture);
90
91   /**
92    * Called by video player to flip render buffers
93    * If buffering is enabled this method does not block. In case of disabled buffering
94    * this method blocks waiting for the render thread to pass by.
95    * When buffering is used there might be no free buffer available after the call to
96    * this method. Player has to call WaitForBuffer. A free buffer will become
97    * available after the main thread has flipped front / back buffers.
98    *
99    * @param bStop reference to stop flag of calling thread
100    * @param timestamp of frame delivered with AddVideoPicture
101    * @param source depreciated
102    * @param sync signals frame, top, or bottom field
103    */
104   void FlipPage(volatile bool& bStop, double timestamp = 0.0, int source = -1, EFIELDSYNC sync = FS_NONE);
105   unsigned int PreInit();
106   void UnInit();
107   bool Flush();
108
109   void AddOverlay(CDVDOverlay* o, double pts)
110   {
111     CSharedLock lock(m_sharedSection);
112     m_overlays.AddOverlay(o, pts, m_free.front());
113   }
114
115   void AddCleanup(OVERLAY::COverlay* o)
116   {
117     CSharedLock lock(m_sharedSection);
118     m_overlays.AddCleanup(o);
119   }
120
121   void Reset();
122
123   RESOLUTION GetResolution();
124
125   static float GetMaximumFPS();
126   inline bool IsStarted() { return m_bIsStarted;}
127   double GetDisplayLatency() { return m_displayLatency; }
128   int    GetSkippedFrames()  { return m_QueueSkip; }
129
130   bool Supports(ERENDERFEATURE feature);
131   bool Supports(EDEINTERLACEMODE method);
132   bool Supports(EINTERLACEMETHOD method);
133   bool Supports(ESCALINGMETHOD method);
134
135   EINTERLACEMETHOD AutoInterlaceMethod(EINTERLACEMETHOD mInt);
136
137   static double GetPresentTime();
138   void  WaitPresentTime(double presenttime);
139
140   CStdString GetVSyncState();
141
142   void UpdateResolution();
143
144   bool RendererHandlesPresent() const;
145
146 #ifdef HAS_GL
147   CLinuxRendererGL    *m_pRenderer;
148 #elif HAS_GLES == 2
149   CLinuxRendererGLES  *m_pRenderer;
150 #elif defined(HAS_DX)
151   CWinRenderer        *m_pRenderer;
152 #elif defined(HAS_SDL)
153   CLinuxRenderer      *m_pRenderer;
154 #endif
155
156   unsigned int GetProcessorSize();
157
158   // Supported pixel formats, can be called before configure
159   std::vector<ERenderFormat> SupportedFormats();
160
161   void Recover(); // called after resolution switch if something special is needed
162
163   CSharedSection& GetSection() { return m_sharedSection; };
164
165   void RegisterRenderUpdateCallBack(const void *ctx, RenderUpdateCallBackFn fn);
166   void RegisterRenderFeaturesCallBack(const void *ctx, RenderFeaturesCallBackFn fn);
167
168   /**
169    * If player uses buffering it has to wait for a buffer before it calls
170    * AddVideoPicture and AddOverlay. It waits for max 50 ms before it returns -1
171    * in case no buffer is available. Player may call this in a loop and decides
172    * by itself when it wants to drop a frame.
173    * If no buffering is requested in Configure, player does not need to call this,
174    * because FlipPage will block.
175    */
176   int WaitForBuffer(volatile bool& bStop, int timeout = 100);
177
178   /**
179    * Video player call this on flush in oder to discard any queued frames
180    */
181   void DiscardBuffer();
182
183 protected:
184
185   void PresentSingle(bool clear, DWORD flags, DWORD alpha);
186   void PresentFields(bool clear, DWORD flags, DWORD alpha);
187   void PresentBlend(bool clear, DWORD flags, DWORD alpha);
188
189   void PrepareNextRender();
190
191   EINTERLACEMETHOD AutoInterlaceMethodInternal(EINTERLACEMETHOD mInt);
192
193   bool m_bIsStarted;
194   CSharedSection m_sharedSection;
195
196   bool m_bReconfigured;
197
198   int m_rendermethod;
199
200   enum EPRESENTSTEP
201   {
202     PRESENT_IDLE     = 0
203   , PRESENT_FLIP
204   , PRESENT_FRAME
205   , PRESENT_FRAME2
206   , PRESENT_READY
207   };
208
209   enum EPRESENTMETHOD
210   {
211     PRESENT_METHOD_SINGLE = 0,
212     PRESENT_METHOD_BLEND,
213     PRESENT_METHOD_WEAVE,
214     PRESENT_METHOD_BOB,
215   };
216
217   double m_displayLatency;
218   void UpdateDisplayLatency();
219
220   int m_QueueSize;
221   int m_QueueSkip;
222
223   struct SPresent
224   {
225     double         timestamp;
226     EFIELDSYNC     presentfield;
227     EPRESENTMETHOD presentmethod;
228   } m_Queue[NUM_BUFFERS];
229
230   std::deque<int> m_free;
231   std::deque<int> m_queued;
232   std::deque<int> m_discard;
233
234   ERenderFormat   m_format;
235
236   double     m_presentcorr;
237   double     m_presenterr;
238   double     m_errorbuff[ERRORBUFFSIZE];
239   int        m_errorindex;
240   EPRESENTSTEP     m_presentstep;
241   int        m_presentsource;
242   XbmcThreads::ConditionVariable  m_presentevent;
243   CCriticalSection m_presentlock;
244   CEvent     m_flushEvent;
245
246
247   OVERLAY::CRenderer m_overlays;
248
249   void RenderCapture(CRenderCapture* capture);
250   void RemoveCapture(CRenderCapture* capture);
251   CCriticalSection           m_captCritSect;
252   std::list<CRenderCapture*> m_captures;
253   //set to true when adding something to m_captures, set to false when m_captures is made empty
254   //std::list::empty() isn't thread safe, using an extra bool will save a lock per render when no captures are requested
255   bool                       m_hasCaptures; 
256
257   // temporary fix for RendererHandlesPresent after #2811
258   bool m_firstFlipPage;
259 };
260
261 extern CXBMCRenderManager g_renderManager;