subreader: fix unsafe sscanf calls with "%["
[mplayer.git] / libvo / gl_common.h
blob26ca30e8337450dd599540b32293a602b6280eb0
1 /*
2 * This file is part of MPlayer.
4 * MPlayer is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * MPlayer is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 * You can alternatively redistribute this file and/or
19 * modify it under the terms of the GNU Lesser General Public
20 * License as published by the Free Software Foundation; either
21 * version 2.1 of the License, or (at your option) any later version.
24 #ifndef MPLAYER_GL_COMMON_H
25 #define MPLAYER_GL_COMMON_H
27 #include <stdio.h>
28 #include <stdint.h>
30 #include "config.h"
31 #include "mp_msg.h"
33 #include "video_out.h"
34 #include "csputils.h"
36 #if defined(CONFIG_GL_COCOA) && !defined(CONFIG_GL_X11)
37 #ifdef GL_VERSION_3_0
38 #include <OpenGL/gl3.h>
39 #else
40 #include <OpenGL/gl.h>
41 #endif
42 #include <OpenGL/glext.h>
43 #else
44 #include <GL/gl.h>
45 #include <GL/glext.h>
46 #endif
48 #include "libvo/gl_header_fixes.h"
50 struct GL;
51 typedef struct GL GL;
53 void glAdjustAlignment(GL *gl, int stride);
55 int glFindFormat(uint32_t format, int have_texture_rg, int *bpp,
56 GLint *gl_texfmt, GLenum *gl_format, GLenum *gl_type);
57 int glFmt2bpp(GLenum format, GLenum type);
58 void glCreateClearTex(GL *gl, GLenum target, GLenum fmt, GLenum format,
59 GLenum type, GLint filter, int w, int h,
60 unsigned char val);
61 int glCreatePPMTex(GL *gl, GLenum target, GLenum fmt, GLint filter,
62 FILE *f, int *width, int *height, int *maxval);
63 void glUploadTex(GL *gl, GLenum target, GLenum format, GLenum type,
64 const void *dataptr, int stride,
65 int x, int y, int w, int h, int slice);
66 void glDownloadTex(GL *gl, GLenum target, GLenum format, GLenum type,
67 void *dataptr, int stride);
68 void glDrawTex(GL *gl, GLfloat x, GLfloat y, GLfloat w, GLfloat h,
69 GLfloat tx, GLfloat ty, GLfloat tw, GLfloat th,
70 int sx, int sy, int rect_tex, int is_yv12, int flip);
71 int loadGPUProgram(GL *gl, GLenum target, char *prog);
72 void glCheckError(GL *gl, const char *info);
74 /** \addtogroup glconversion
75 * \{ */
76 //! do not use YUV conversion, this should always stay 0
77 #define YUV_CONVERSION_NONE 0
78 //! use nVidia specific register combiners for YUV conversion
79 //! implementation has been removed
80 #define YUV_CONVERSION_COMBINERS 1
81 //! use a fragment program for YUV conversion
82 #define YUV_CONVERSION_FRAGMENT 2
83 //! use a fragment program for YUV conversion with gamma using POW
84 #define YUV_CONVERSION_FRAGMENT_POW 3
85 //! use a fragment program with additional table lookup for YUV conversion
86 #define YUV_CONVERSION_FRAGMENT_LOOKUP 4
87 //! use ATI specific register combiners ("fragment program")
88 #define YUV_CONVERSION_COMBINERS_ATI 5
89 //! use a fragment program with 3D table lookup for YUV conversion
90 #define YUV_CONVERSION_FRAGMENT_LOOKUP3D 6
91 //! use ATI specific "text" register combiners ("fragment program")
92 #define YUV_CONVERSION_TEXT_FRAGMENT 7
93 //! use normal bilinear scaling for textures
94 #define YUV_SCALER_BILIN 0
95 //! use higher quality bicubic scaling for textures
96 #define YUV_SCALER_BICUB 1
97 //! use cubic scaling in X and normal linear scaling in Y direction
98 #define YUV_SCALER_BICUB_X 2
99 //! use cubic scaling without additional lookup texture
100 #define YUV_SCALER_BICUB_NOTEX 3
101 #define YUV_SCALER_UNSHARP 4
102 #define YUV_SCALER_UNSHARP2 5
103 //! mask for conversion type
104 #define YUV_CONVERSION_MASK 0xF
105 //! mask for scaler type
106 #define YUV_SCALER_MASK 0xF
107 //! shift value for luminance scaler type
108 #define YUV_LUM_SCALER_SHIFT 8
109 //! shift value for chrominance scaler type
110 #define YUV_CHROM_SCALER_SHIFT 12
111 //! extract conversion out of type
112 #define YUV_CONVERSION(t) ((t) & YUV_CONVERSION_MASK)
113 //! extract luminance scaler out of type
114 #define YUV_LUM_SCALER(t) (((t) >> YUV_LUM_SCALER_SHIFT) & YUV_SCALER_MASK)
115 //! extract chrominance scaler out of type
116 #define YUV_CHROM_SCALER(t) (((t) >> YUV_CHROM_SCALER_SHIFT) & YUV_SCALER_MASK)
117 #define SET_YUV_CONVERSION(c) ((c) & YUV_CONVERSION_MASK)
118 #define SET_YUV_LUM_SCALER(s) (((s) & YUV_SCALER_MASK) << YUV_LUM_SCALER_SHIFT)
119 #define SET_YUV_CHROM_SCALER(s) (((s) & YUV_SCALER_MASK) << YUV_CHROM_SCALER_SHIFT)
120 //! returns whether the yuv conversion supports large brightness range etc.
121 static inline int glYUVLargeRange(int conv)
123 switch (conv) {
124 case YUV_CONVERSION_NONE:
125 case YUV_CONVERSION_COMBINERS_ATI:
126 case YUV_CONVERSION_FRAGMENT_LOOKUP3D:
127 case YUV_CONVERSION_TEXT_FRAGMENT:
128 return 0;
130 return 1;
132 /** \} */
134 typedef struct {
135 GLenum target;
136 int type;
137 struct mp_csp_params csp_params;
138 int texw;
139 int texh;
140 int chrom_texw;
141 int chrom_texh;
142 float filter_strength;
143 float noise_strength;
144 } gl_conversion_params_t;
146 int glAutodetectYUVConversion(GL *gl);
147 void glSetupYUVConversion(GL *gl, gl_conversion_params_t *params);
148 void glEnableYUVConversion(GL *gl, GLenum target, int type);
149 void glDisableYUVConversion(GL *gl, GLenum target, int type);
151 #define GL_3D_RED_CYAN 1
152 #define GL_3D_GREEN_MAGENTA 2
153 #define GL_3D_QUADBUFFER 3
155 void glEnable3DLeft(GL *gl, int type);
156 void glEnable3DRight(GL *gl, int type);
157 void glDisable3D(GL *gl, int type);
159 /** \addtogroup glcontext
160 * \{ */
161 //! could not set new window, will continue drawing into the old one.
162 #define SET_WINDOW_FAILED -1
163 //! new window is set, could even transfer the OpenGL context.
164 #define SET_WINDOW_OK 0
165 //! new window is set, but the OpenGL context needs to be reinitialized.
166 #define SET_WINDOW_REINIT 1
167 /** \} */
169 enum MPGLType {
170 GLTYPE_AUTO,
171 GLTYPE_COCOA,
172 GLTYPE_W32,
173 GLTYPE_X11,
174 GLTYPE_SDL,
177 enum {
178 MPGLFLAG_DEBUG = 1,
181 #define MPGL_VER(major, minor) (((major) << 16) | (minor))
182 #define MPGL_VER_GET_MAJOR(ver) ((ver) >> 16)
183 #define MPGL_VER_GET_MINOR(ver) ((ver) & ((1 << 16) - 1))
185 typedef struct MPGLContext {
186 GL *gl;
187 enum MPGLType type;
188 struct vo *vo;
189 void *priv;
190 // Bit size of each component in the created framebuffer. 0 if unknown.
191 int depth_r, depth_g, depth_b;
192 int (*create_window)(struct MPGLContext *ctx, uint32_t d_width,
193 uint32_t d_height, uint32_t flags);
194 int (*setGlWindow)(struct MPGLContext *);
195 void (*releaseGlContext)(struct MPGLContext *);
196 void (*swapGlBuffers)(struct MPGLContext *);
197 int (*check_events)(struct vo *vo);
198 void (*fullscreen)(struct vo *vo);
199 void (*vo_uninit)(struct vo *vo);
200 // only available if GL3 context creation is supported
201 // gl_flags: bitfield of MPGLFLAG_* constants
202 // gl_version: requested OpenGL version number (use MPGL_VER())
203 // return value is one of the SET_WINDOW_* constants
204 int (*create_window_gl3)(struct MPGLContext *ctx, int gl_flags,
205 int gl_version, uint32_t d_width,
206 uint32_t d_height, uint32_t flags);
207 // optional
208 void (*pause)(struct vo *vo);
209 void (*resume)(struct vo *vo);
210 void (*ontop)(struct vo *vo);
211 void (*border)(struct vo *vo);
212 void (*update_xinerama_info)(struct vo *vo);
213 } MPGLContext;
215 int mpgl_find_backend(const char *name);
217 MPGLContext *init_mpglcontext(enum MPGLType type, struct vo *vo);
218 void uninit_mpglcontext(MPGLContext *ctx);
220 // calls create_window_gl3 or create_window+setGlWindow
221 int create_mpglcontext(struct MPGLContext *ctx, int gl_flags, int gl_version,
222 uint32_t d_width, uint32_t d_height, uint32_t flags);
224 // print a multi line string with line numbers (e.g. for shader sources)
225 // mod, lev: module and log level, as in mp_msg()
226 void mp_log_source(int mod, int lev, const char *src);
228 //function pointers loaded from the OpenGL library
229 struct GL {
230 void (GLAPIENTRY *Begin)(GLenum);
231 void (GLAPIENTRY *End)(void);
232 void (GLAPIENTRY *Viewport)(GLint, GLint, GLsizei, GLsizei);
233 void (GLAPIENTRY *MatrixMode)(GLenum);
234 void (GLAPIENTRY *LoadIdentity)(void);
235 void (GLAPIENTRY *Translated)(double, double, double);
236 void (GLAPIENTRY *Scaled)(double, double, double);
237 void (GLAPIENTRY *Ortho)(double, double, double, double, double,double);
238 void (GLAPIENTRY *PushMatrix)(void);
239 void (GLAPIENTRY *PopMatrix)(void);
240 void (GLAPIENTRY *Clear)(GLbitfield);
241 GLuint (GLAPIENTRY *GenLists)(GLsizei);
242 void (GLAPIENTRY *DeleteLists)(GLuint, GLsizei);
243 void (GLAPIENTRY *NewList)(GLuint, GLenum);
244 void (GLAPIENTRY *EndList)(void);
245 void (GLAPIENTRY *CallList)(GLuint);
246 void (GLAPIENTRY *CallLists)(GLsizei, GLenum, const GLvoid *);
247 void (GLAPIENTRY *GenTextures)(GLsizei, GLuint *);
248 void (GLAPIENTRY *DeleteTextures)(GLsizei, const GLuint *);
249 void (GLAPIENTRY *TexEnvi)(GLenum, GLenum, GLint);
250 void (GLAPIENTRY *Color4ub)(GLubyte, GLubyte, GLubyte, GLubyte);
251 void (GLAPIENTRY *Color4f)(GLfloat, GLfloat, GLfloat, GLfloat);
252 void (GLAPIENTRY *ClearColor)(GLclampf, GLclampf, GLclampf, GLclampf);
253 void (GLAPIENTRY *Enable)(GLenum);
254 void (GLAPIENTRY *Disable)(GLenum);
255 const GLubyte *(GLAPIENTRY * GetString)(GLenum);
256 void (GLAPIENTRY *DrawBuffer)(GLenum);
257 void (GLAPIENTRY *DepthMask)(GLboolean);
258 void (GLAPIENTRY *BlendFunc)(GLenum, GLenum);
259 void (GLAPIENTRY *Flush)(void);
260 void (GLAPIENTRY *Finish)(void);
261 void (GLAPIENTRY *PixelStorei)(GLenum, GLint);
262 void (GLAPIENTRY *TexImage1D)(GLenum, GLint, GLint, GLsizei, GLint,
263 GLenum, GLenum, const GLvoid *);
264 void (GLAPIENTRY *TexImage2D)(GLenum, GLint, GLint, GLsizei, GLsizei,
265 GLint, GLenum, GLenum, const GLvoid *);
266 void (GLAPIENTRY *TexSubImage2D)(GLenum, GLint, GLint, GLint,
267 GLsizei, GLsizei, GLenum, GLenum,
268 const GLvoid *);
269 void (GLAPIENTRY *GetTexImage)(GLenum, GLint, GLenum, GLenum, GLvoid *);
270 void (GLAPIENTRY *TexParameteri)(GLenum, GLenum, GLint);
271 void (GLAPIENTRY *TexParameterf)(GLenum, GLenum, GLfloat);
272 void (GLAPIENTRY *TexParameterfv)(GLenum, GLenum, const GLfloat *);
273 void (GLAPIENTRY *TexCoord2f)(GLfloat, GLfloat);
274 void (GLAPIENTRY *TexCoord2fv)(const GLfloat *);
275 void (GLAPIENTRY *Vertex2f)(GLfloat, GLfloat);
276 void (GLAPIENTRY *GetIntegerv)(GLenum, GLint *);
277 void (GLAPIENTRY *GetBooleanv)(GLenum, GLboolean *);
278 void (GLAPIENTRY *ColorMask)(GLboolean, GLboolean, GLboolean, GLboolean);
279 void (GLAPIENTRY *ReadPixels)(GLint, GLint, GLsizei, GLsizei, GLenum,
280 GLenum, GLvoid *);
281 void (GLAPIENTRY *ReadBuffer)(GLenum);
282 void (GLAPIENTRY *VertexPointer)(GLint, GLenum, GLsizei, const GLvoid *);
283 void (GLAPIENTRY *ColorPointer)(GLint, GLenum, GLsizei, const GLvoid *);
284 void (GLAPIENTRY *TexCoordPointer)(GLint, GLenum, GLsizei, const GLvoid *);
285 void (GLAPIENTRY *DrawArrays)(GLenum, GLint, GLsizei);
286 void (GLAPIENTRY *EnableClientState)(GLenum);
287 void (GLAPIENTRY *DisableClientState)(GLenum);
288 GLenum (GLAPIENTRY *GetError)(void);
291 // OpenGL extension functions
292 void (GLAPIENTRY *GenBuffers)(GLsizei, GLuint *);
293 void (GLAPIENTRY *DeleteBuffers)(GLsizei, const GLuint *);
294 void (GLAPIENTRY *BindBuffer)(GLenum, GLuint);
295 GLvoid * (GLAPIENTRY * MapBuffer)(GLenum, GLenum);
296 GLboolean (GLAPIENTRY *UnmapBuffer)(GLenum);
297 void (GLAPIENTRY *BufferData)(GLenum, intptr_t, const GLvoid *, GLenum);
298 void (GLAPIENTRY *ActiveTexture)(GLenum);
299 void (GLAPIENTRY *BindTexture)(GLenum, GLuint);
300 void (GLAPIENTRY *MultiTexCoord2f)(GLenum, GLfloat, GLfloat);
301 void (GLAPIENTRY *GenPrograms)(GLsizei, GLuint *);
302 void (GLAPIENTRY *DeletePrograms)(GLsizei, const GLuint *);
303 void (GLAPIENTRY *BindProgram)(GLenum, GLuint);
304 void (GLAPIENTRY *ProgramString)(GLenum, GLenum, GLsizei, const GLvoid *);
305 void (GLAPIENTRY *GetProgramivARB)(GLenum, GLenum, GLint *);
306 void (GLAPIENTRY *ProgramEnvParameter4f)(GLenum, GLuint, GLfloat, GLfloat,
307 GLfloat, GLfloat);
308 int (GLAPIENTRY *SwapInterval)(int);
309 void (GLAPIENTRY *TexImage3D)(GLenum, GLint, GLenum, GLsizei, GLsizei,
310 GLsizei, GLint, GLenum, GLenum,
311 const GLvoid *);
313 // ancient ATI extensions
314 void (GLAPIENTRY *BeginFragmentShader)(void);
315 void (GLAPIENTRY *EndFragmentShader)(void);
316 void (GLAPIENTRY *SampleMap)(GLuint, GLuint, GLenum);
317 void (GLAPIENTRY *ColorFragmentOp2)(GLenum, GLuint, GLuint, GLuint, GLuint,
318 GLuint, GLuint, GLuint, GLuint, GLuint);
319 void (GLAPIENTRY *ColorFragmentOp3)(GLenum, GLuint, GLuint, GLuint, GLuint,
320 GLuint, GLuint, GLuint, GLuint, GLuint,
321 GLuint, GLuint, GLuint);
322 void (GLAPIENTRY *SetFragmentShaderConstant)(GLuint, const GLfloat *);
325 // GL 3, possibly in GL 2.x as well in form of extensions
326 void (GLAPIENTRY *GenVertexArrays)(GLsizei, GLuint *);
327 void (GLAPIENTRY *BindVertexArray)(GLuint);
328 GLint (GLAPIENTRY *GetAttribLocation)(GLuint, const GLchar *);
329 void (GLAPIENTRY *EnableVertexAttribArray)(GLuint);
330 void (GLAPIENTRY *DisableVertexAttribArray)(GLuint);
331 void (GLAPIENTRY *VertexAttribPointer)(GLuint, GLint, GLenum, GLboolean,
332 GLsizei, const GLvoid *);
333 void (GLAPIENTRY *DeleteVertexArrays)(GLsizei, const GLuint *);
334 void (GLAPIENTRY *UseProgram)(GLuint);
335 GLint (GLAPIENTRY *GetUniformLocation)(GLuint, const GLchar *);
336 void (GLAPIENTRY *CompileShader)(GLuint);
337 GLuint (GLAPIENTRY *CreateProgram)(void);
338 GLuint (GLAPIENTRY *CreateShader)(GLenum);
339 void (GLAPIENTRY *ShaderSource)(GLuint, GLsizei, const GLchar **,
340 const GLint *);
341 void (GLAPIENTRY *LinkProgram)(GLuint);
342 void (GLAPIENTRY *AttachShader)(GLuint, GLuint);
343 void (GLAPIENTRY *DeleteShader)(GLuint);
344 void (GLAPIENTRY *DeleteProgram)(GLuint);
345 void (GLAPIENTRY *GetShaderInfoLog)(GLuint, GLsizei, GLsizei *, GLchar *);
346 void (GLAPIENTRY *GetShaderiv)(GLuint, GLenum, GLint *);
347 void (GLAPIENTRY *GetProgramInfoLog)(GLuint, GLsizei, GLsizei *, GLchar *);
348 void (GLAPIENTRY *GetProgramiv)(GLenum, GLenum, GLint *);
349 const GLubyte* (GLAPIENTRY *GetStringi)(GLenum, GLuint);
350 void (GLAPIENTRY *BindAttribLocation)(GLuint, GLuint, const GLchar *);
351 void (GLAPIENTRY *BindFramebuffer)(GLenum, GLuint);
352 void (GLAPIENTRY *GenFramebuffers)(GLsizei, GLuint *);
353 void (GLAPIENTRY *DeleteFramebuffers)(GLsizei, const GLuint *);
354 GLenum (GLAPIENTRY *CheckFramebufferStatus)(GLenum);
355 void (GLAPIENTRY *FramebufferTexture2D)(GLenum, GLenum, GLenum, GLuint,
356 GLint);
358 void (GLAPIENTRY *Uniform1f)(GLint, GLfloat);
359 void (GLAPIENTRY *Uniform3f)(GLint, GLfloat, GLfloat, GLfloat);
360 void (GLAPIENTRY *Uniform1i)(GLint, GLint);
361 void (GLAPIENTRY *UniformMatrix3fv)(GLint, GLsizei, GLboolean,
362 const GLfloat *);
363 void (GLAPIENTRY *UniformMatrix4x3fv)(GLint, GLsizei, GLboolean,
364 const GLfloat *);
367 #endif /* MPLAYER_GL_COMMON_H */