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