00001 #ifndef MYTHRENDER_OPENGL_H_
00002 #define MYTHRENDER_OPENGL_H_
00003
00004 #include <stdint.h>
00005
00006 #include <QPainter>
00007 #include <QGLContext>
00008 #include <QHash>
00009 #include <QMutex>
00010
00011 #define GL_GLEXT_PROTOTYPES
00012
00013 #ifdef USING_X11
00014 #define GLX_GLXEXT_PROTOTYPES
00015 #define XMD_H 1
00016 #ifndef GL_ES_VERSION_2_0
00017 #include <GL/gl.h>
00018 #endif
00019 #undef GLX_ARB_get_proc_address
00020 #endif // USING_X11
00021
00022 #ifdef _WIN32
00023 #include <GL/glext.h>
00024 #endif
00025
00026 #ifdef Q_WS_MACX
00027 #include "util-osx.h"
00028 #import <agl.h>
00029 #endif
00030
00031 #include "mythuiexp.h"
00032 #include "mythlogging.h"
00033 #include "mythrender_base.h"
00034 #include "mythrender_opengl_defs.h"
00035
00036 #include "mythuianimation.h"
00037
00038 typedef enum
00039 {
00040 kGLFeatNone = 0x0000,
00041 kGLMultiTex = 0x0001,
00042 kGLExtRect = 0x0002,
00043 kGLExtFragProg = 0x0004,
00044 kGLExtFBufObj = 0x0008,
00045 kGLExtPBufObj = 0x0010,
00046 kGLNVFence = 0x0020,
00047 kGLAppleFence = 0x0040,
00048 kGLMesaYCbCr = 0x0080,
00049 kGLAppleYCbCr = 0x0100,
00050 kGLMipMaps = 0x0200,
00051 kGLSL = 0x0400,
00052 kGLVertexArray = 0x0800,
00053 kGLExtVBO = 0x1000,
00054 kGLMaxFeat = 0x2000,
00055 } GLFeatures;
00056
00057 #define MYTHTV_UYVY 0x8A1F
00058 #define TEX_OFFSET 8
00059
00060 class MythGLTexture
00061 {
00062 public:
00063 MythGLTexture() :
00064 m_type(GL_TEXTURE_2D), m_data(NULL), m_data_size(0),
00065 m_data_type(GL_UNSIGNED_BYTE), m_data_fmt(GL_BGRA),
00066 m_internal_fmt(GL_RGBA8), m_pbo(0), m_vbo(0),
00067 m_filter(GL_LINEAR), m_wrap(GL_CLAMP_TO_EDGE),
00068 m_size(0,0), m_act_size(0,0)
00069 {
00070 memset(&m_vertex_data, 0, sizeof(m_vertex_data));
00071 }
00072
00073 ~MythGLTexture()
00074 {
00075 }
00076
00077 GLuint m_type;
00078 unsigned char *m_data;
00079 uint m_data_size;
00080 GLuint m_data_type;
00081 GLuint m_data_fmt;
00082 GLuint m_internal_fmt;
00083 GLuint m_pbo;
00084 GLuint m_vbo;
00085 GLuint m_filter;
00086 GLuint m_wrap;
00087 QSize m_size;
00088 QSize m_act_size;
00089 GLfloat m_vertex_data[16];
00090 };
00091
00092 class MythRenderOpenGL;
00093
00094 class MUI_PUBLIC OpenGLLocker
00095 {
00096 public:
00097 OpenGLLocker(MythRenderOpenGL *render);
00098 ~OpenGLLocker();
00099 private:
00100 MythRenderOpenGL *m_render;
00101 };
00102
00103 class MUI_PUBLIC MythRenderOpenGL : public QGLContext, public MythRender
00104 {
00105 public:
00106 static MythRenderOpenGL* Create(const QString &painter,
00107 QPaintDevice* device = NULL);
00108
00109 MythRenderOpenGL(const QGLFormat& format, QPaintDevice* device,
00110 RenderType type = kRenderUnknown);
00111 MythRenderOpenGL(const QGLFormat& format, RenderType type = kRenderUnknown);
00112
00113 virtual void makeCurrent();
00114 virtual void doneCurrent();
00115 virtual void Release(void);
00116
00117 void Init(void);
00118
00119 int GetMaxTextureSize(void) { return m_max_tex_size; }
00120 uint GetFeatures(void) { return m_exts_used; }
00121
00122 bool IsRecommendedRenderer(void);
00123
00124 void MoveResizeWindow(const QRect &rect);
00125 void SetViewPort(const QRect &rect, bool viewportonly = false);
00126 QRect GetViewPort(void) { return m_viewport; }
00127 virtual void PushTransformation(const UIEffects &fx, QPointF ¢er) = 0;
00128 virtual void PopTransformation(void) = 0;
00129 void Flush(bool use_fence);
00130 void SetBlend(bool enable);
00131 virtual void SetColor(int r, int g, int b, int a) { }
00132 void SetBackground(int r, int g, int b, int a);
00133 void SetFence(void);
00134
00135 void* GetTextureBuffer(uint tex, bool create_buffer = true);
00136 void UpdateTexture(uint tex, void *buf);
00137 int GetTextureType(bool &rect);
00138 bool IsRectTexture(uint type);
00139 uint CreateTexture(QSize act_size, bool use_pbo, uint type,
00140 uint data_type = GL_UNSIGNED_BYTE,
00141 uint data_fmt = GL_BGRA, uint internal_fmt = GL_RGBA8,
00142 uint filter = GL_LINEAR, uint wrap = GL_CLAMP_TO_EDGE);
00143 QSize GetTextureSize(uint type, const QSize &size);
00144 QSize GetTextureSize(uint tex);
00145 int GetTextureDataSize(uint tex);
00146 void SetTextureFilters(uint tex, uint filt, uint wrap);
00147 void ActiveTexture(int active_tex);
00148 virtual uint CreateHelperTexture(void) { return 0; }
00149 void EnableTextures(uint type, uint tex_type = 0);
00150 void DisableTextures(void);
00151 void DeleteTexture(uint tex);
00152
00153 bool CreateFrameBuffer(uint &fb, uint tex);
00154 void DeleteFrameBuffer(uint fb);
00155 void BindFramebuffer(uint fb);
00156 void ClearFramebuffer(void);
00157
00158 virtual uint CreateShaderObject(const QString &vert, const QString &frag) = 0;
00159 virtual void DeleteShaderObject(uint obj) = 0;
00160 virtual void EnableShaderObject(uint obj) = 0;
00161 virtual void SetShaderParams(uint prog, void* vals, const char* uniform) = 0;
00162
00163 void DrawBitmap(uint tex, uint target, const QRect *src,
00164 const QRect *dst, uint prog, int alpha = 255,
00165 int red = 255, int green = 255, int blue = 255);
00166 void DrawBitmap(uint *textures, uint texture_count, uint target,
00167 const QRectF *src, const QRectF *dst, uint prog);
00168 void DrawRect(const QRect &area, const QBrush &fillBrush,
00169 const QPen &linePen, int alpha);
00170 void DrawRoundRect(const QRect &area, int cornerRadius,
00171 const QBrush &fillBrush, const QPen &linePen,
00172 int alpha);
00173 virtual bool RectanglesAreAccelerated(void) { return false; }
00174
00175 protected:
00176 virtual ~MythRenderOpenGL();
00177 virtual void DrawBitmapPriv(uint tex, const QRect *src, const QRect *dst,
00178 uint prog, int alpha,
00179 int red, int green, int blue) = 0;
00180 virtual void DrawBitmapPriv(uint *textures, uint texture_count,
00181 const QRectF *src, const QRectF *dst,
00182 uint prog) = 0;
00183 virtual void DrawRectPriv(const QRect &area, const QBrush &fillBrush,
00184 const QPen &linePen, int alpha) = 0;
00185 virtual void DrawRoundRectPriv(const QRect &area, int cornerRadius,
00186 const QBrush &fillBrush, const QPen &linePen,
00187 int alpha) = 0;
00188
00189 virtual void Init2DState(void);
00190 virtual void InitProcs(void);
00191 void* GetProcAddress(const QString &proc) const;
00192 virtual bool InitFeatures(void);
00193 virtual void ResetVars(void);
00194 virtual void ResetProcs(void);
00195 virtual void SetMatrixView(void) = 0;
00196
00197 uint CreatePBO(uint tex);
00198 uint CreateVBO(void);
00199 virtual void DeleteOpenGLResources(void);
00200 void DeleteTextures(void);
00201 virtual void DeleteShaders(void) = 0;
00202 void DeleteFrameBuffers(void);
00203
00204 bool UpdateTextureVertices(uint tex, const QRect *src, const QRect *dst);
00205 bool UpdateTextureVertices(uint tex, const QRectF *src, const QRectF *dst);
00206 GLfloat* GetCachedVertices(GLuint type, const QRect &area);
00207 void ExpireVertices(uint max = 0);
00208 void GetCachedVBO(GLuint type, const QRect &area);
00209 void ExpireVBOS(uint max = 0);
00210 bool ClearTexture(uint tex);
00211 uint GetBufferSize(QSize size, uint fmt, uint type);
00212
00213 static void StoreBicubicWeights(float x, float *dst);
00214
00215 protected:
00216
00217 QHash<GLuint, MythGLTexture> m_textures;
00218 QVector<GLuint> m_framebuffers;
00219 GLuint m_fence;
00220
00221
00222 QMutex *m_lock;
00223 int m_lock_level;
00224
00225
00226 QString m_extensions;
00227 uint m_exts_supported;
00228 uint m_exts_used;
00229 int m_max_tex_size;
00230 int m_max_units;
00231 int m_default_texture_type;
00232
00233
00234 QRect m_viewport;
00235 int m_active_tex;
00236 int m_active_tex_type;
00237 int m_active_fb;
00238 bool m_blend;
00239 uint32_t m_background;
00240
00241
00242 QMap<uint64_t,GLfloat*> m_cachedVertices;
00243 QList<uint64_t> m_vertexExpiry;
00244 QMap<uint64_t,GLuint> m_cachedVBOS;
00245 QList<uint64_t> m_vboExpiry;
00246
00247
00248 MYTH_GLTEXIMAGE1DPROC m_glTexImage1D;
00249
00250
00251 MYTH_GLACTIVETEXTUREPROC m_glActiveTexture;
00252
00253
00254 MYTH_GLMAPBUFFERPROC m_glMapBuffer;
00255 MYTH_GLBINDBUFFERPROC m_glBindBuffer;
00256 MYTH_GLGENBUFFERSPROC m_glGenBuffers;
00257 MYTH_GLBUFFERDATAPROC m_glBufferData;
00258 MYTH_GLUNMAPBUFFERPROC m_glUnmapBuffer;
00259 MYTH_GLDELETEBUFFERSPROC m_glDeleteBuffers;
00260
00261 MYTH_GLGENFRAMEBUFFERSPROC m_glGenFramebuffers;
00262 MYTH_GLBINDFRAMEBUFFERPROC m_glBindFramebuffer;
00263 MYTH_GLFRAMEBUFFERTEXTURE2DPROC m_glFramebufferTexture2D;
00264 MYTH_GLCHECKFRAMEBUFFERSTATUSPROC m_glCheckFramebufferStatus;
00265 MYTH_GLDELETEFRAMEBUFFERSPROC m_glDeleteFramebuffers;
00266
00267 MYTH_GLGENFENCESNVPROC m_glGenFencesNV;
00268 MYTH_GLDELETEFENCESNVPROC m_glDeleteFencesNV;
00269 MYTH_GLSETFENCENVPROC m_glSetFenceNV;
00270 MYTH_GLFINISHFENCENVPROC m_glFinishFenceNV;
00271
00272 MYTH_GLGENFENCESAPPLEPROC m_glGenFencesAPPLE;
00273 MYTH_GLDELETEFENCESAPPLEPROC m_glDeleteFencesAPPLE;
00274 MYTH_GLSETFENCEAPPLEPROC m_glSetFenceAPPLE;
00275 MYTH_GLFINISHFENCEAPPLEPROC m_glFinishFenceAPPLE;
00276 };
00277
00278 #endif