MythTV  0.27pre
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Groups Pages
mythrender_opengl.h
Go to the documentation of this file.
1 #ifndef MYTHRENDER_OPENGL_H_
2 #define MYTHRENDER_OPENGL_H_
3 
4 #include <stdint.h>
5 
6 #include <QPainter>
7 #include <QGLContext>
8 #include <QHash>
9 #include <QMutex>
10 
11 #define GL_GLEXT_PROTOTYPES
12 
13 #ifdef USING_X11
14 #define GLX_GLXEXT_PROTOTYPES
15 #define XMD_H 1
16 #ifndef GL_ES_VERSION_2_0
17 #include <GL/gl.h>
18 #endif
19 #undef GLX_ARB_get_proc_address
20 #endif // USING_X11
21 
22 #ifdef _WIN32
23 #include <GL/glext.h>
24 #endif
25 
26 #ifdef Q_OS_MAC
27 #include "util-osx.h"
28 #import <agl.h>
29 #endif
30 
31 #include "mythuiexp.h"
32 #include "mythlogging.h"
33 #include "mythrender_base.h"
34 #include "mythrender_opengl_defs.h"
35 
36 #include "mythuianimation.h"
37 
38 typedef enum
39 {
40  kGLFeatNone = 0x0000,
41  kGLMultiTex = 0x0001,
42  kGLExtRect = 0x0002,
43  kGLExtFragProg = 0x0004,
44  kGLExtFBufObj = 0x0008,
45  kGLExtPBufObj = 0x0010,
46  kGLNVFence = 0x0020,
47  kGLAppleFence = 0x0040,
48  kGLMesaYCbCr = 0x0080,
49  kGLAppleYCbCr = 0x0100,
50  kGLMipMaps = 0x0200,
51  kGLSL = 0x0400,
52  kGLVertexArray = 0x0800,
53  kGLExtVBO = 0x1000,
54  kGLMaxFeat = 0x2000,
55 } GLFeatures;
56 
57 #define MYTHTV_UYVY 0x8A1F
58 #define TEX_OFFSET 8
59 
61 {
62  public:
64  m_type(GL_TEXTURE_2D), m_data(NULL), m_data_size(0),
65  m_data_type(GL_UNSIGNED_BYTE), m_data_fmt(GL_BGRA),
66  m_internal_fmt(GL_RGBA8), m_pbo(0), m_vbo(0),
67  m_filter(GL_LINEAR), m_wrap(GL_CLAMP_TO_EDGE),
68  m_size(0,0), m_act_size(0,0)
69  {
70  memset(&m_vertex_data, 0, sizeof(m_vertex_data));
71  }
72 
74  {
75  }
76 
77  GLuint m_type;
78  unsigned char *m_data;
80  GLuint m_data_type;
81  GLuint m_data_fmt;
83  GLuint m_pbo;
84  GLuint m_vbo;
85  GLuint m_filter;
86  GLuint m_wrap;
87  QSize m_size;
88  QSize m_act_size;
89  GLfloat m_vertex_data[16];
90 };
91 
92 class MythRenderOpenGL;
93 
94 class MUI_PUBLIC OpenGLLocker
95 {
96  public:
98  ~OpenGLLocker();
99  private:
101 };
102 
103 class MUI_PUBLIC MythRenderOpenGL : public QGLContext, public MythRender
104 {
105  public:
106  static MythRenderOpenGL* Create(const QString &painter,
107  QPaintDevice* device = NULL);
108 
109  MythRenderOpenGL(const QGLFormat& format, QPaintDevice* device,
111  MythRenderOpenGL(const QGLFormat& format, RenderType type = kRenderUnknown);
112 
113  virtual void makeCurrent();
114  virtual void doneCurrent();
115  virtual void Release(void);
116 
117  void Init(void);
118 
119  int GetMaxTextureSize(void) { return m_max_tex_size; }
120  uint GetFeatures(void) { return m_exts_used; }
121 
122  bool IsRecommendedRenderer(void);
123 
124  void MoveResizeWindow(const QRect &rect);
125  void SetViewPort(const QRect &rect, bool viewportonly = false);
126  QRect GetViewPort(void) { return m_viewport; }
127  virtual void PushTransformation(const UIEffects &fx, QPointF &center) = 0;
128  virtual void PopTransformation(void) = 0;
129  void Flush(bool use_fence);
130  void SetBlend(bool enable);
131  virtual void SetColor(int r, int g, int b, int a) { }
132  void SetBackground(int r, int g, int b, int a);
133  void SetFence(void);
134 
135  void* GetTextureBuffer(uint tex, bool create_buffer = true);
136  void UpdateTexture(uint tex, void *buf);
137  int GetTextureType(bool &rect);
138  bool IsRectTexture(uint type);
139  uint CreateTexture(QSize act_size, bool use_pbo, uint type,
140  uint data_type = GL_UNSIGNED_BYTE,
141  uint data_fmt = GL_BGRA, uint internal_fmt = GL_RGBA8,
142  uint filter = GL_LINEAR, uint wrap = GL_CLAMP_TO_EDGE);
143  QSize GetTextureSize(uint type, const QSize &size);
144  QSize GetTextureSize(uint tex);
145  int GetTextureDataSize(uint tex);
146  void SetTextureFilters(uint tex, uint filt, uint wrap);
147  void ActiveTexture(int active_tex);
148  virtual uint CreateHelperTexture(void) { return 0; }
149  void EnableTextures(uint type, uint tex_type = 0);
150  void DisableTextures(void);
151  void DeleteTexture(uint tex);
152 
153  bool CreateFrameBuffer(uint &fb, uint tex);
154  void DeleteFrameBuffer(uint fb);
155  void BindFramebuffer(uint fb);
156  void ClearFramebuffer(void);
157 
158  virtual uint CreateShaderObject(const QString &vert, const QString &frag) = 0;
159  virtual void DeleteShaderObject(uint obj) = 0;
160  virtual void EnableShaderObject(uint obj) = 0;
161  virtual void SetShaderParams(uint prog, void* vals, const char* uniform) = 0;
162 
163  void DrawBitmap(uint tex, uint target, const QRect *src,
164  const QRect *dst, uint prog, int alpha = 255,
165  int red = 255, int green = 255, int blue = 255);
166  void DrawBitmap(uint *textures, uint texture_count, uint target,
167  const QRectF *src, const QRectF *dst, uint prog);
168  void DrawRect(const QRect &area, const QBrush &fillBrush,
169  const QPen &linePen, int alpha);
170  void DrawRoundRect(const QRect &area, int cornerRadius,
171  const QBrush &fillBrush, const QPen &linePen,
172  int alpha);
173  virtual bool RectanglesAreAccelerated(void) { return false; }
174 
175  protected:
176  virtual ~MythRenderOpenGL();
177  virtual void DrawBitmapPriv(uint tex, const QRect *src, const QRect *dst,
178  uint prog, int alpha,
179  int red, int green, int blue) = 0;
180  virtual void DrawBitmapPriv(uint *textures, uint texture_count,
181  const QRectF *src, const QRectF *dst,
182  uint prog) = 0;
183  virtual void DrawRectPriv(const QRect &area, const QBrush &fillBrush,
184  const QPen &linePen, int alpha) = 0;
185  virtual void DrawRoundRectPriv(const QRect &area, int cornerRadius,
186  const QBrush &fillBrush, const QPen &linePen,
187  int alpha) = 0;
188 
189  virtual void Init2DState(void);
190  virtual void InitProcs(void);
191  void* GetProcAddress(const QString &proc) const;
192  virtual bool InitFeatures(void);
193  virtual void ResetVars(void);
194  virtual void ResetProcs(void);
195  virtual void SetMatrixView(void) = 0;
196 
197  uint CreatePBO(uint tex);
198  uint CreateVBO(void);
199  virtual void DeleteOpenGLResources(void);
200  void DeleteTextures(void);
201  virtual void DeleteShaders(void) = 0;
202  void DeleteFrameBuffers(void);
203 
204  bool UpdateTextureVertices(uint tex, const QRect *src, const QRect *dst);
205  bool UpdateTextureVertices(uint tex, const QRectF *src, const QRectF *dst);
206  GLfloat* GetCachedVertices(GLuint type, const QRect &area);
207  void ExpireVertices(uint max = 0);
208  void GetCachedVBO(GLuint type, const QRect &area);
209  void ExpireVBOS(uint max = 0);
210  bool ClearTexture(uint tex);
211  uint GetBufferSize(QSize size, uint fmt, uint type);
212 
213  static void StoreBicubicWeights(float x, float *dst);
214 
215  protected:
216  // Resources
217  QHash<GLuint, MythGLTexture> m_textures;
218  QVector<GLuint> m_framebuffers;
219  GLuint m_fence;
220 
221  // Locking
222  QMutex *m_lock;
224 
225  // profile
226  QString m_extensions;
232 
233  // State
234  QRect m_viewport;
238  bool m_blend;
239  uint32_t m_background;
240 
241  // vertex cache
242  QMap<uint64_t,GLfloat*> m_cachedVertices;
243  QList<uint64_t> m_vertexExpiry;
244  QMap<uint64_t,GLuint> m_cachedVBOS;
245  QList<uint64_t> m_vboExpiry;
246 
247  // 1D Textures (not available on GL ES 2.0)
248  MYTH_GLTEXIMAGE1DPROC m_glTexImage1D;
249 
250  // Multi-texturing
251  MYTH_GLACTIVETEXTUREPROC m_glActiveTexture;
252 
253  // PixelBuffer Objects
255  MYTH_GLBINDBUFFERPROC m_glBindBuffer;
256  MYTH_GLGENBUFFERSPROC m_glGenBuffers;
257  MYTH_GLBUFFERDATAPROC m_glBufferData;
258  MYTH_GLUNMAPBUFFERPROC m_glUnmapBuffer;
259  MYTH_GLDELETEBUFFERSPROC m_glDeleteBuffers;
260  // FrameBuffer Objects
261  MYTH_GLGENFRAMEBUFFERSPROC m_glGenFramebuffers;
262  MYTH_GLBINDFRAMEBUFFERPROC m_glBindFramebuffer;
263  MYTH_GLFRAMEBUFFERTEXTURE2DPROC m_glFramebufferTexture2D;
264  MYTH_GLCHECKFRAMEBUFFERSTATUSPROC m_glCheckFramebufferStatus;
265  MYTH_GLDELETEFRAMEBUFFERSPROC m_glDeleteFramebuffers;
266  // NV_fence
267  MYTH_GLGENFENCESNVPROC m_glGenFencesNV;
268  MYTH_GLDELETEFENCESNVPROC m_glDeleteFencesNV;
269  MYTH_GLSETFENCENVPROC m_glSetFenceNV;
270  MYTH_GLFINISHFENCENVPROC m_glFinishFenceNV;
271  // APPLE_fence
276 };
277 
278 #endif