PageRenderTime 54ms CodeModel.GetById 2ms app.highlight 47ms RepoModel.GetById 1ms app.codeStats 0ms

/xbmc/cores/VideoRenderers/LinuxRendererGL.h

http://github.com/xbmc/xbmc
C++ Header | 362 lines | 262 code | 68 blank | 32 comment | 3 complexity | 32021ad2de720d285965f2fcea1cef94 MD5 | raw file
  1#ifndef LINUXRENDERERGL_RENDERER
  2#define LINUXRENDERERGL_RENDERER
  3
  4/*
  5 *      Copyright (C) 2007-2013 Team XBMC
  6 *      http://xbmc.org
  7 *
  8 *  This Program is free software; you can redistribute it and/or modify
  9 *  it under the terms of the GNU General Public License as published by
 10 *  the Free Software Foundation; either version 2, or (at your option)
 11 *  any later version.
 12 *
 13 *  This Program is distributed in the hope that it will be useful,
 14 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 15 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 16 *  GNU General Public License for more details.
 17 *
 18 *  You should have received a copy of the GNU General Public License
 19 *  along with XBMC; see the file COPYING.  If not, see
 20 *  <http://www.gnu.org/licenses/>.
 21 *
 22 */
 23
 24#include "system.h"
 25
 26#ifdef HAS_GL
 27#include "system_gl.h"
 28
 29#include "guilib/FrameBufferObject.h"
 30#include "guilib/Shader.h"
 31#include "settings/VideoSettings.h"
 32#include "RenderFlags.h"
 33#include "RenderFormats.h"
 34#include "guilib/GraphicContext.h"
 35#include "BaseRenderer.h"
 36
 37#include "threads/Event.h"
 38
 39class CRenderCapture;
 40
 41class CBaseTexture;
 42namespace Shaders { class BaseYUV2RGBShader; }
 43namespace Shaders { class BaseVideoFilterShader; }
 44namespace VAAPI   { struct CHolder; }
 45namespace VDPAU   { class CVdpauRenderPicture; }
 46
 47#undef ALIGN
 48#define ALIGN(value, alignment) (((value)+((alignment)-1))&~((alignment)-1))
 49#define CLAMP(a, min, max) ((a) > (max) ? (max) : ( (a) < (min) ? (min) : a ))
 50
 51#define AUTOSOURCE -1
 52
 53#define IMAGE_FLAG_WRITING   0x01 /* image is in use after a call to GetImage, caller may be reading or writing */
 54#define IMAGE_FLAG_READING   0x02 /* image is in use after a call to GetImage, caller is only reading */
 55#define IMAGE_FLAG_DYNAMIC   0x04 /* image was allocated due to a call to GetImage */
 56#define IMAGE_FLAG_RESERVED  0x08 /* image is reserved, must be asked for specifically used to preserve images */
 57#define IMAGE_FLAG_READY     0x16 /* image is ready to be uploaded to texture memory */
 58#define IMAGE_FLAG_INUSE (IMAGE_FLAG_WRITING | IMAGE_FLAG_READING | IMAGE_FLAG_RESERVED)
 59
 60struct DRAWRECT
 61{
 62  float left;
 63  float top;
 64  float right;
 65  float bottom;
 66};
 67
 68struct YUVRANGE
 69{
 70  int y_min, y_max;
 71  int u_min, u_max;
 72  int v_min, v_max;
 73};
 74
 75struct YUVCOEF
 76{
 77  float r_up, r_vp;
 78  float g_up, g_vp;
 79  float b_up, b_vp;
 80};
 81
 82enum RenderMethod
 83{
 84  RENDER_GLSL=0x01,
 85  RENDER_ARB=0x02,
 86  RENDER_SW=0x04,
 87  RENDER_VDPAU=0x08,
 88  RENDER_POT=0x10,
 89  RENDER_VAAPI=0x20,
 90  RENDER_CVREF = 0x40,
 91};
 92
 93enum RenderQuality
 94{
 95  RQ_LOW=1,
 96  RQ_SINGLEPASS,
 97  RQ_MULTIPASS,
 98};
 99
100#define PLANE_Y 0
101#define PLANE_U 1
102#define PLANE_V 2
103
104#define FIELD_FULL 0
105#define FIELD_TOP 1
106#define FIELD_BOT 2
107
108extern YUVRANGE yuv_range_lim;
109extern YUVRANGE yuv_range_full;
110extern YUVCOEF yuv_coef_bt601;
111extern YUVCOEF yuv_coef_bt709;
112extern YUVCOEF yuv_coef_ebu;
113extern YUVCOEF yuv_coef_smtp240m;
114
115class DllSwScale;
116
117class CLinuxRendererGL : public CBaseRenderer
118{
119public:
120  CLinuxRendererGL();
121  virtual ~CLinuxRendererGL();
122
123  virtual void Update();
124  virtual void SetupScreenshot() {};
125
126  bool RenderCapture(CRenderCapture* capture);
127
128  // Player functions
129  virtual bool Configure(unsigned int width, unsigned int height, unsigned int d_width, unsigned int d_height, float fps, unsigned flags, ERenderFormat format, unsigned extended_formatl, unsigned int orientation);
130  virtual bool IsConfigured() { return m_bConfigured; }
131  virtual int          GetImage(YV12Image *image, int source = AUTOSOURCE, bool readonly = false);
132  virtual void         ReleaseImage(int source, bool preserve = false);
133  virtual void         FlipPage(int source);
134  virtual unsigned int PreInit();
135  virtual void         UnInit();
136  virtual void         Reset(); /* resets renderer after seek for example */
137  virtual void         Flush();
138  virtual void         ReleaseBuffer(int idx);
139  virtual void         SetBufferSize(int numBuffers) { m_NumYV12Buffers = numBuffers; }
140  virtual unsigned int GetMaxBufferSize() { return NUM_BUFFERS; }
141  virtual unsigned int GetProcessorSize();
142
143#ifdef HAVE_LIBVDPAU
144  virtual void         AddProcessor(VDPAU::CVdpauRenderPicture* vdpau, int index);
145#endif
146#ifdef HAVE_LIBVA
147  virtual void         AddProcessor(VAAPI::CHolder& holder, int index);
148#endif
149#ifdef TARGET_DARWIN
150  virtual void         AddProcessor(struct __CVBuffer *cvBufferRef, int index);
151#endif
152
153  virtual void RenderUpdate(bool clear, DWORD flags = 0, DWORD alpha = 255);
154
155  // Feature support
156  virtual bool SupportsMultiPassRendering();
157  virtual bool Supports(ERENDERFEATURE feature);
158  virtual bool Supports(EDEINTERLACEMODE mode);
159  virtual bool Supports(EINTERLACEMETHOD method);
160  virtual bool Supports(ESCALINGMETHOD method);
161
162  virtual EINTERLACEMETHOD AutoInterlaceMethod();
163
164  virtual std::vector<ERenderFormat> SupportedFormats() { return m_formats; }
165
166protected:
167  virtual void Render(DWORD flags, int renderBuffer);
168  void         ClearBackBuffer();
169  void         DrawBlackBars();
170
171  bool ValidateRenderer();
172  int  NextYV12Texture();
173  virtual bool ValidateRenderTarget();
174  virtual void LoadShaders(int field=FIELD_FULL);
175  void SetTextureFilter(GLenum method);
176  void UpdateVideoFilter();
177
178  // textures
179  bool (CLinuxRendererGL::*m_textureUpload)(int index);
180  void (CLinuxRendererGL::*m_textureDelete)(int index);
181  bool (CLinuxRendererGL::*m_textureCreate)(int index);
182
183  bool UploadYV12Texture(int index);
184  void DeleteYV12Texture(int index);
185  bool CreateYV12Texture(int index);
186
187  bool UploadNV12Texture(int index);
188  void DeleteNV12Texture(int index);
189  bool CreateNV12Texture(int index);
190  
191  bool UploadVDPAUTexture(int index);
192  void DeleteVDPAUTexture(int index);
193  bool CreateVDPAUTexture(int index);
194
195  bool UploadVDPAUTexture420(int index);
196  void DeleteVDPAUTexture420(int index);
197  bool CreateVDPAUTexture420(int index);
198
199  bool UploadVAAPITexture(int index);
200  void DeleteVAAPITexture(int index);
201  bool CreateVAAPITexture(int index);
202
203  bool UploadCVRefTexture(int index);
204  void DeleteCVRefTexture(int index);
205  bool CreateCVRefTexture(int index);
206
207  bool UploadYUV422PackedTexture(int index);
208  void DeleteYUV422PackedTexture(int index);
209  bool CreateYUV422PackedTexture(int index);
210
211  bool UploadRGBTexture(int index);
212  void ToRGBFrame(YV12Image* im, unsigned flipIndexPlane, unsigned flipIndexBuf);
213  void ToRGBFields(YV12Image* im, unsigned flipIndexPlaneTop, unsigned flipIndexPlaneBot, unsigned flipIndexBuf);
214  void SetupRGBBuffer();
215
216  void CalculateTextureSourceRects(int source, int num_planes);
217
218  // renderers
219  void RenderToFBO(int renderBuffer, int field, bool weave = false);
220  void RenderFromFBO();
221  void RenderSinglePass(int renderBuffer, int field); // single pass glsl renderer
222  void RenderSoftware(int renderBuffer, int field);   // single pass s/w yuv2rgb renderer
223  void RenderVDPAU(int renderBuffer, int field);      // render using vdpau hardware
224  void RenderProgressiveWeave(int renderBuffer, int field); // render using vdpau hardware
225  void RenderVAAPI(int renderBuffer, int field);      // render using vdpau hardware
226
227  struct
228  {
229    CFrameBufferObject fbo;
230    float width, height;
231  } m_fbo;
232
233  int m_iYV12RenderBuffer;
234  int m_NumYV12Buffers;
235  int m_iLastRenderBuffer;
236
237  bool m_bConfigured;
238  bool m_bValidated;
239  std::vector<ERenderFormat> m_formats;
240  bool m_bImageReady;
241  ERenderFormat m_format;
242  GLenum m_textureTarget;
243  unsigned short m_renderMethod;
244  RenderQuality m_renderQuality;
245  unsigned int m_flipindex; // just a counter to keep track of if a image has been uploaded
246  
247  // Raw data used by renderer
248  int m_currentField;
249  int m_reloadShaders;
250
251  struct YUVPLANE
252  {
253    GLuint id;
254    GLuint pbo;
255
256    CRect  rect;
257
258    float  width;
259    float  height;
260
261    unsigned texwidth;
262    unsigned texheight;
263
264    //pixels per texel
265    unsigned pixpertex_x;
266    unsigned pixpertex_y;
267
268    unsigned flipindex;
269  };
270
271  typedef YUVPLANE           YUVPLANES[MAX_PLANES];
272  typedef YUVPLANES          YUVFIELDS[MAX_FIELDS];
273
274  struct YUVBUFFER
275  {
276    YUVBUFFER();
277   ~YUVBUFFER();
278
279    YUVFIELDS fields;
280    YV12Image image;
281    unsigned  flipindex; /* used to decide if this has been uploaded */
282    GLuint    pbo[MAX_PLANES];
283
284#ifdef HAVE_LIBVDPAU
285    VDPAU::CVdpauRenderPicture *vdpau;
286#endif
287#ifdef HAVE_LIBVA
288    VAAPI::CHolder& vaapi;
289#endif
290#ifdef TARGET_DARWIN_OSX
291    struct __CVBuffer *cvBufferRef;
292#endif
293  };
294
295  typedef YUVBUFFER          YUVBUFFERS[NUM_BUFFERS];
296
297  // YV12 decoder textures
298  // field index 0 is full image, 1 is odd scanlines, 2 is even scanlines
299  YUVBUFFERS m_buffers;
300
301  void LoadPlane( YUVPLANE& plane, int type, unsigned flipindex
302                , unsigned width,  unsigned height
303                , int stride, int bpp, void* data, GLuint* pbo = NULL );
304
305  void GetPlaneTextureSize(YUVPLANE& plane);
306
307  Shaders::BaseYUV2RGBShader     *m_pYUVShader;
308  Shaders::BaseVideoFilterShader *m_pVideoFilterShader;
309  ESCALINGMETHOD m_scalingMethod;
310  ESCALINGMETHOD m_scalingMethodGui;
311
312  // clear colour for "black" bars
313  float m_clearColour;
314
315  // software scale library (fallback if required gl version is not available)
316  DllSwScale        *m_dllSwScale;
317  BYTE              *m_rgbBuffer;  // if software scale is used, this will hold the result image
318  unsigned int       m_rgbBufferSize;
319  GLuint             m_rgbPbo;
320  struct SwsContext *m_context;
321
322  void BindPbo(YUVBUFFER& buff);
323  void UnBindPbo(YUVBUFFER& buff);
324  bool m_pboSupported;
325  bool m_pboUsed;
326
327  bool  m_nonLinStretch;
328  bool  m_nonLinStretchGui;
329  float m_pixelRatio;
330};
331
332
333inline int NP2( unsigned x ) {
334#if defined(TARGET_POSIX) && !defined(__POWERPC__) && !defined(__PPC__) && !defined(__arm__)
335  // If there are any issues compiling this, just append a ' && 0'
336  // to the above to make it '#if defined(TARGET_POSIX) && 0'
337
338  // Linux assembly is AT&T Unix style, not Intel style
339  unsigned y;
340  __asm__("dec %%ecx \n"
341          "movl $1, %%eax \n"
342          "bsr %%ecx,%%ecx \n"
343          "inc %%ecx \n"
344          "shl %%cl, %%eax \n"
345          "movl %%eax, %0 \n"
346          :"=r"(y)
347          :"c"(x)
348          :"%eax");
349  return y;
350#else
351    --x;
352    x |= x >> 1;
353    x |= x >> 2;
354    x |= x >> 4;
355    x |= x >> 8;
356    x |= x >> 16;
357    return ++x;
358#endif
359}
360#endif
361
362#endif