Fix indentation
[mplayer/glamo.git] / libvo / vo_gl.c
blobba60ea8bff36b0e5e078f6934cdcc8eab2036d1d
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <math.h>
6 #include "config.h"
7 #include "mp_msg.h"
8 #include "subopt-helper.h"
9 #include "video_out.h"
10 #include "video_out_internal.h"
11 #include "font_load.h"
12 #include "sub.h"
14 #include "gl_common.h"
15 #include "aspect.h"
16 #ifdef CONFIG_GUI
17 #include "gui/interface.h"
18 #endif
19 #include "fastmemcpy.h"
20 #include "libass/ass.h"
21 #include "libass/ass_mp.h"
23 static const vo_info_t info =
25 "X11 (OpenGL)",
26 "gl",
27 "Arpad Gereoffy <arpi@esp-team.scene.hu>",
31 const LIBVO_EXTERN(gl)
33 #ifdef GL_WIN32
34 static int gl_vinfo = 0;
35 static HGLRC gl_context = 0;
36 #define update_xinerama_info w32_update_xinerama_info
37 #define vo_init vo_w32_init
38 #define vo_window vo_w32_window
39 #else
40 static XVisualInfo *gl_vinfo = NULL;
41 static GLXContext gl_context = 0;
42 static int wsGLXAttrib[] = { GLX_RGBA,
43 GLX_RED_SIZE,1,
44 GLX_GREEN_SIZE,1,
45 GLX_BLUE_SIZE,1,
46 GLX_DOUBLEBUFFER,
47 None };
48 #endif
50 static int use_osd;
51 static int scaled_osd;
52 //! How many parts the OSD may consist of at most
53 #define MAX_OSD_PARTS 20
54 //! Textures for OSD
55 static GLuint osdtex[MAX_OSD_PARTS];
56 #ifndef FAST_OSD
57 //! Alpha textures for OSD
58 static GLuint osdatex[MAX_OSD_PARTS];
59 #endif
60 static GLuint *eosdtex;
61 static GLuint largeeosdtex[2];
62 //! Display lists that draw the OSD parts
63 static GLuint osdDispList[MAX_OSD_PARTS];
64 #ifndef FAST_OSD
65 static GLuint osdaDispList[MAX_OSD_PARTS];
66 #endif
67 static GLuint eosdDispList;
68 //! How many parts the OSD currently consists of
69 static int osdtexCnt;
70 static int eosdtexCnt;
71 static int osd_color;
73 static int use_aspect;
74 static int use_ycbcr;
75 static int use_yuv;
76 static int lscale;
77 static int cscale;
78 static float filter_strength;
79 static int yuvconvtype;
80 static int use_rectangle;
81 static int err_shown;
82 static uint32_t image_width;
83 static uint32_t image_height;
84 static uint32_t image_format;
85 static int many_fmts;
86 static int ati_hack;
87 static int force_pbo;
88 static int mesa_buffer;
89 static int use_glFinish;
90 static int swap_interval;
91 static GLenum gl_target;
92 static GLint gl_texfmt;
93 static GLenum gl_format;
94 static GLenum gl_type;
95 static GLuint gl_buffer;
96 static GLuint gl_buffer_uv[2];
97 static int gl_buffersize;
98 static int gl_buffersize_uv;
99 static void *gl_bufferptr;
100 static void *gl_bufferptr_uv[2];
101 static int mesa_buffersize;
102 static void *mesa_bufferptr;
103 static GLuint fragprog;
104 static GLuint default_texs[22];
105 static char *custom_prog;
106 static char *custom_tex;
107 static int custom_tlin;
108 static int custom_trect;
110 static int int_pause;
111 static int eq_bri = 0;
112 static int eq_cont = 0;
113 static int eq_sat = 0;
114 static int eq_hue = 0;
115 static int eq_rgamma = 0;
116 static int eq_ggamma = 0;
117 static int eq_bgamma = 0;
119 static int texture_width;
120 static int texture_height;
121 static int mpi_flipped;
122 static int vo_flipped;
123 static int ass_border_x, ass_border_y;
125 static unsigned int slice_height = 1;
127 static void redraw(void);
129 static void resize(int x,int y){
130 mp_msg(MSGT_VO, MSGL_V, "[gl] Resize: %dx%d\n",x,y);
131 if (WinID >= 0) {
132 int top = 0, left = 0, w = x, h = y;
133 geometry(&top, &left, &w, &h, vo_screenwidth, vo_screenheight);
134 glViewport(top, left, w, h);
135 } else
136 glViewport( 0, 0, x, y );
138 glMatrixMode(GL_PROJECTION);
139 glLoadIdentity();
140 if (vo_fs && use_aspect) {
141 int new_w, new_h;
142 GLdouble scale_x, scale_y;
143 aspect(&new_w, &new_h, A_ZOOM);
144 panscan_calc();
145 new_w += vo_panscan_x;
146 new_h += vo_panscan_y;
147 scale_x = (GLdouble)new_w / (GLdouble)x;
148 scale_y = (GLdouble)new_h / (GLdouble)y;
149 glScaled(scale_x, scale_y, 1);
150 ass_border_x = (vo_screenwidth - new_w) / 2;
151 ass_border_y = (vo_screenheight - new_h) / 2;
153 glOrtho(0, image_width, image_height, 0, -1,1);
155 glMatrixMode(GL_MODELVIEW);
156 glLoadIdentity();
158 if (!scaled_osd) {
159 #ifdef CONFIG_FREETYPE
160 // adjust font size to display size
161 force_load_font = 1;
162 #endif
163 vo_osd_changed(OSDTYPE_OSD);
165 glClear(GL_COLOR_BUFFER_BIT);
166 redraw();
169 static void texSize(int w, int h, int *texw, int *texh) {
170 if (use_rectangle) {
171 *texw = w; *texh = h;
172 } else {
173 *texw = 32;
174 while (*texw < w)
175 *texw *= 2;
176 *texh = 32;
177 while (*texh < h)
178 *texh *= 2;
180 if (ati_hack) *texw = (*texw + 511) & ~511;
183 //! maximum size of custom fragment program
184 #define MAX_CUSTOM_PROG_SIZE (1024 * 1024)
185 static void update_yuvconv(void) {
186 float bri = eq_bri / 100.0;
187 float cont = (eq_cont + 100) / 100.0;
188 float hue = eq_hue / 100.0 * 3.1415927;
189 float sat = (eq_sat + 100) / 100.0;
190 float rgamma = exp(log(8.0) * eq_rgamma / 100.0);
191 float ggamma = exp(log(8.0) * eq_ggamma / 100.0);
192 float bgamma = exp(log(8.0) * eq_bgamma / 100.0);
193 gl_conversion_params_t params = {gl_target, yuvconvtype,
194 bri, cont, hue, sat, rgamma, ggamma, bgamma,
195 texture_width, texture_height, filter_strength};
196 glSetupYUVConversion(&params);
197 if (custom_prog) {
198 FILE *f = fopen(custom_prog, "r");
199 if (!f)
200 mp_msg(MSGT_VO, MSGL_WARN,
201 "[gl] Could not read customprog %s\n", custom_prog);
202 else {
203 char *prog = calloc(1, MAX_CUSTOM_PROG_SIZE + 1);
204 fread(prog, 1, MAX_CUSTOM_PROG_SIZE, f);
205 fclose(f);
206 loadGPUProgram(GL_FRAGMENT_PROGRAM, prog);
207 free(prog);
209 ProgramEnvParameter4f(GL_FRAGMENT_PROGRAM, 0,
210 1.0 / texture_width, 1.0 / texture_height,
211 texture_width, texture_height);
213 if (custom_tex) {
214 FILE *f = fopen(custom_tex, "r");
215 if (!f)
216 mp_msg(MSGT_VO, MSGL_WARN,
217 "[gl] Could not read customtex %s\n", custom_tex);
218 else {
219 int width, height, maxval;
220 ActiveTexture(GL_TEXTURE3);
221 if (glCreatePPMTex(custom_trect?GL_TEXTURE_RECTANGLE:GL_TEXTURE_2D, 0,
222 custom_tlin?GL_LINEAR:GL_NEAREST,
223 f, &width, &height, &maxval))
224 ProgramEnvParameter4f(GL_FRAGMENT_PROGRAM, 1,
225 1.0 / width, 1.0 / height, width, height);
226 else
227 mp_msg(MSGT_VO, MSGL_WARN,
228 "[gl] Error parsing customtex %s\n", custom_tex);
229 fclose(f);
230 ActiveTexture(GL_TEXTURE0);
236 * \brief remove all OSD textures and display-lists, thus clearing it.
238 static void clearOSD(void) {
239 int i;
240 if (!osdtexCnt)
241 return;
242 glDeleteTextures(osdtexCnt, osdtex);
243 #ifndef FAST_OSD
244 glDeleteTextures(osdtexCnt, osdatex);
245 for (i = 0; i < osdtexCnt; i++)
246 glDeleteLists(osdaDispList[i], 1);
247 #endif
248 for (i = 0; i < osdtexCnt; i++)
249 glDeleteLists(osdDispList[i], 1);
250 osdtexCnt = 0;
254 * \brief remove textures, display list and free memory used by EOSD
256 static void clearEOSD(void) {
257 if (eosdDispList)
258 glDeleteLists(eosdDispList, 1);
259 eosdDispList = 0;
260 if (eosdtexCnt)
261 glDeleteTextures(eosdtexCnt, eosdtex);
262 eosdtexCnt = 0;
263 free(eosdtex);
264 eosdtex = NULL;
268 * \brief construct display list from ass image list
269 * \param img image list to create OSD from.
270 * A value of NULL has the same effect as clearEOSD()
272 static void genEOSD(mp_eosd_images_t *imgs) {
273 int sx, sy;
274 int tinytexcur = 0;
275 int smalltexcur = 0;
276 GLuint *curtex;
277 GLint scale_type = scaled_osd ? GL_LINEAR : GL_NEAREST;
278 ass_image_t *img = imgs->imgs;
279 ass_image_t *i;
281 if (imgs->changed == 0) // there are elements, but they are unchanged
282 return;
283 if (img && imgs->changed == 1) // there are elements, but they just moved
284 goto skip_upload;
286 clearEOSD();
287 if (!img)
288 return;
289 if (!largeeosdtex[0]) {
290 glGenTextures(2, largeeosdtex);
291 BindTexture(gl_target, largeeosdtex[0]);
292 glCreateClearTex(gl_target, GL_ALPHA, GL_ALPHA, GL_UNSIGNED_BYTE, scale_type, 512, 512, 0);
293 BindTexture(gl_target, largeeosdtex[1]);
294 glCreateClearTex(gl_target, GL_ALPHA, GL_ALPHA, GL_UNSIGNED_BYTE, scale_type, 512, 512, 0);
296 for (i = img; i; i = i->next)
298 if (i->w <= 0 || i->h <= 0 || i->stride < i->w)
299 continue;
300 if (i->w < 16 && i->h < 16 && tinytexcur < 1024)
301 tinytexcur++;
302 else if (i->w < 32 && i->h < 32 && smalltexcur < 256)
303 smalltexcur++;
304 else
305 eosdtexCnt++;
307 mp_msg(MSGT_VO, MSGL_DBG2, "EOSD counts (tiny, small, all): %i, %i, %i\n",
308 tinytexcur, smalltexcur, eosdtexCnt);
309 if (eosdtexCnt) {
310 eosdtex = calloc(eosdtexCnt, sizeof(GLuint));
311 glGenTextures(eosdtexCnt, eosdtex);
313 tinytexcur = smalltexcur = 0;
314 for (i = img, curtex = eosdtex; i; i = i->next) {
315 int x = 0, y = 0;
316 if (i->w <= 0 || i->h <= 0 || i->stride < i->w) {
317 mp_msg(MSGT_VO, MSGL_V, "Invalid dimensions OSD for part!\n");
318 continue;
320 if (i->w < 16 && i->h < 16 && tinytexcur < 1024) {
321 x = (tinytexcur & 31) << 4;
322 y = (tinytexcur >> 5) << 4;
323 BindTexture(gl_target, largeeosdtex[0]);
324 tinytexcur++;
325 } else if (i->w < 32 && i->h < 32 && smalltexcur < 256) {
326 x = (smalltexcur & 15) << 5;
327 y = (smalltexcur >> 4) << 5;
328 BindTexture(gl_target, largeeosdtex[1]);
329 smalltexcur++;
330 } else {
331 texSize(i->w, i->h, &sx, &sy);
332 BindTexture(gl_target, *curtex++);
333 glCreateClearTex(gl_target, GL_ALPHA, GL_ALPHA, GL_UNSIGNED_BYTE, scale_type, sx, sy, 0);
335 glUploadTex(gl_target, GL_ALPHA, GL_UNSIGNED_BYTE, i->bitmap, i->stride,
336 x, y, i->w, i->h, 0);
338 eosdDispList = glGenLists(1);
339 skip_upload:
340 glNewList(eosdDispList, GL_COMPILE);
341 tinytexcur = smalltexcur = 0;
342 for (i = img, curtex = eosdtex; i; i = i->next) {
343 int x = 0, y = 0;
344 if (i->w <= 0 || i->h <= 0 || i->stride < i->w)
345 continue;
346 glColor4ub(i->color >> 24, (i->color >> 16) & 0xff, (i->color >> 8) & 0xff, 255 - (i->color & 0xff));
347 if (i->w < 16 && i->h < 16 && tinytexcur < 1024) {
348 x = (tinytexcur & 31) << 4;
349 y = (tinytexcur >> 5) << 4;
350 sx = sy = 512;
351 BindTexture(gl_target, largeeosdtex[0]);
352 tinytexcur++;
353 } else if (i->w < 32 && i->h < 32 && smalltexcur < 256) {
354 x = (smalltexcur & 15) << 5;
355 y = (smalltexcur >> 4) << 5;
356 sx = sy = 512;
357 BindTexture(gl_target, largeeosdtex[1]);
358 smalltexcur++;
359 } else {
360 texSize(i->w, i->h, &sx, &sy);
361 BindTexture(gl_target, *curtex++);
363 glDrawTex(i->dst_x, i->dst_y, i->w, i->h, x, y, i->w, i->h, sx, sy, use_rectangle == 1, 0, 0);
365 glEndList();
366 BindTexture(gl_target, 0);
370 * \brief uninitialize OpenGL context, freeing textures, buffers etc.
372 static void uninitGl(void) {
373 int i = 0;
374 if (DeletePrograms && fragprog)
375 DeletePrograms(1, &fragprog);
376 fragprog = 0;
377 while (default_texs[i] != 0)
378 i++;
379 if (i)
380 glDeleteTextures(i, default_texs);
381 default_texs[0] = 0;
382 clearOSD();
383 clearEOSD();
384 if (largeeosdtex[0])
385 glDeleteTextures(2, largeeosdtex);
386 largeeosdtex[0] = 0;
387 if (DeleteBuffers && gl_buffer)
388 DeleteBuffers(1, &gl_buffer);
389 gl_buffer = 0; gl_buffersize = 0;
390 gl_bufferptr = NULL;
391 if (DeleteBuffers && gl_buffer_uv[0])
392 DeleteBuffers(2, gl_buffer_uv);
393 gl_buffer_uv[0] = gl_buffer_uv[1] = 0; gl_buffersize_uv = 0;
394 gl_bufferptr_uv[0] = gl_bufferptr_uv[1] = 0;
395 if (mesa_bufferptr)
396 FreeMemoryMESA(mDisplay, mScreen, mesa_bufferptr);
397 mesa_bufferptr = NULL;
398 err_shown = 0;
402 * \brief Initialize a (new or reused) OpenGL context.
403 * set global gl-related variables to their default values
405 static int initGl(uint32_t d_width, uint32_t d_height) {
406 texSize(image_width, image_height, &texture_width, &texture_height);
408 glDisable(GL_BLEND);
409 glDisable(GL_DEPTH_TEST);
410 glDepthMask(GL_FALSE);
411 glDisable(GL_CULL_FACE);
412 glEnable(gl_target);
413 glDrawBuffer(vo_doublebuffering?GL_BACK:GL_FRONT);
414 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
416 mp_msg(MSGT_VO, MSGL_V, "[gl] Creating %dx%d texture...\n",
417 texture_width, texture_height);
419 if (image_format == IMGFMT_YV12) {
420 int i;
421 glGenTextures(21, default_texs);
422 default_texs[21] = 0;
423 for (i = 0; i < 7; i++) {
424 ActiveTexture(GL_TEXTURE1 + i);
425 BindTexture(GL_TEXTURE_2D, default_texs[i]);
426 BindTexture(GL_TEXTURE_RECTANGLE, default_texs[i + 7]);
427 BindTexture(GL_TEXTURE_3D, default_texs[i + 14]);
429 ActiveTexture(GL_TEXTURE1);
430 glCreateClearTex(gl_target, gl_texfmt, gl_format, gl_type, GL_LINEAR,
431 texture_width / 2, texture_height / 2, 128);
432 ActiveTexture(GL_TEXTURE2);
433 glCreateClearTex(gl_target, gl_texfmt, gl_format, gl_type, GL_LINEAR,
434 texture_width / 2, texture_height / 2, 128);
435 switch (use_yuv) {
436 case YUV_CONVERSION_FRAGMENT_LOOKUP:
437 case YUV_CONVERSION_FRAGMENT_POW:
438 case YUV_CONVERSION_FRAGMENT:
439 if (!GenPrograms || !BindProgram) {
440 mp_msg(MSGT_VO, MSGL_ERR, "[gl] fragment program functions missing!\n");
441 break;
443 GenPrograms(1, &fragprog);
444 BindProgram(GL_FRAGMENT_PROGRAM, fragprog);
445 break;
447 ActiveTexture(GL_TEXTURE0);
448 BindTexture(gl_target, 0);
449 update_yuvconv();
451 glCreateClearTex(gl_target, gl_texfmt, gl_format, gl_type, GL_LINEAR,
452 texture_width, texture_height, 0);
454 resize(d_width, d_height);
456 glClearColor( 0.0f,0.0f,0.0f,0.0f );
457 glClear( GL_COLOR_BUFFER_BIT );
458 if (SwapInterval && swap_interval >= 0)
459 SwapInterval(swap_interval);
460 return 1;
463 /* connect to server, create and map window,
464 * allocate colors and (shared) memory
466 static int
467 config(uint32_t width, uint32_t height, uint32_t d_width, uint32_t d_height, uint32_t flags, char *title, uint32_t format)
469 image_height = height;
470 image_width = width;
471 image_format = format;
472 glFindFormat(format, NULL, &gl_texfmt, &gl_format, &gl_type);
474 int_pause = 0;
475 vo_flipped = !!(flags & VOFLAG_FLIPPING);
477 #ifdef CONFIG_GUI
478 if (use_gui) {
479 // GUI creates and manages window for us
480 guiGetEvent(guiSetShVideo, 0);
481 #ifndef GL_WIN32
482 goto glconfig;
483 #endif
485 #endif
486 #ifdef GL_WIN32
487 if (!vo_w32_config(d_width, d_height, flags))
488 return -1;
489 #else
491 XVisualInfo *vinfo=glXChooseVisual( mDisplay,mScreen,wsGLXAttrib );
492 if (vinfo == NULL)
494 mp_msg(MSGT_VO, MSGL_ERR, "[gl] no GLX support present\n");
495 return -1;
498 vo_x11_create_vo_window(vinfo, vo_dx, vo_dy, d_width, d_height, flags,
499 XCreateColormap(mDisplay, mRootWin, vinfo->visual, AllocNone),
500 "gl", title);
502 #endif
504 glconfig:
505 if (vo_config_count)
506 uninitGl();
507 setGlWindow(&gl_vinfo, &gl_context, vo_window);
508 initGl(vo_dwidth, vo_dheight);
510 return 0;
513 static void check_events(void)
515 int e=vo_check_events();
516 if(e&VO_EVENT_RESIZE) resize(vo_dwidth,vo_dheight);
517 if(e&VO_EVENT_EXPOSE && int_pause) redraw();
521 * Creates the textures and the display list needed for displaying
522 * an OSD part.
523 * Callback function for vo_draw_text().
525 static void create_osd_texture(int x0, int y0, int w, int h,
526 unsigned char *src, unsigned char *srca,
527 int stride)
529 // initialize to 8 to avoid special-casing on alignment
530 int sx = 8, sy = 8;
531 GLint scale_type = scaled_osd ? GL_LINEAR : GL_NEAREST;
533 if (w <= 0 || h <= 0 || stride < w) {
534 mp_msg(MSGT_VO, MSGL_V, "Invalid dimensions OSD for part!\n");
535 return;
537 texSize(w, h, &sx, &sy);
539 if (osdtexCnt >= MAX_OSD_PARTS) {
540 mp_msg(MSGT_VO, MSGL_ERR, "Too many OSD parts, contact the developers!\n");
541 return;
544 // create Textures for OSD part
545 glGenTextures(1, &osdtex[osdtexCnt]);
546 BindTexture(gl_target, osdtex[osdtexCnt]);
547 glCreateClearTex(gl_target, GL_LUMINANCE, GL_LUMINANCE, GL_UNSIGNED_BYTE, scale_type, sx, sy, 0);
548 glUploadTex(gl_target, GL_LUMINANCE, GL_UNSIGNED_BYTE, src, stride,
549 0, 0, w, h, 0);
551 #ifndef FAST_OSD
552 glGenTextures(1, &osdatex[osdtexCnt]);
553 BindTexture(gl_target, osdatex[osdtexCnt]);
554 glCreateClearTex(gl_target, GL_ALPHA, GL_ALPHA, GL_UNSIGNED_BYTE, scale_type, sx, sy, 0);
556 int i;
557 char *tmp = malloc(stride * h);
558 // convert alpha from weird MPlayer scale.
559 // in-place is not possible since it is reused for future OSDs
560 for (i = h * stride - 1; i >= 0; i--)
561 tmp[i] = -srca[i];
562 glUploadTex(gl_target, GL_ALPHA, GL_UNSIGNED_BYTE, tmp, stride,
563 0, 0, w, h, 0);
564 free(tmp);
566 #endif
568 BindTexture(gl_target, 0);
570 // Create a list for rendering this OSD part
571 #ifndef FAST_OSD
572 osdaDispList[osdtexCnt] = glGenLists(1);
573 glNewList(osdaDispList[osdtexCnt], GL_COMPILE);
574 // render alpha
575 BindTexture(gl_target, osdatex[osdtexCnt]);
576 glDrawTex(x0, y0, w, h, 0, 0, w, h, sx, sy, use_rectangle == 1, 0, 0);
577 glEndList();
578 #endif
579 osdDispList[osdtexCnt] = glGenLists(1);
580 glNewList(osdDispList[osdtexCnt], GL_COMPILE);
581 // render OSD
582 BindTexture(gl_target, osdtex[osdtexCnt]);
583 glDrawTex(x0, y0, w, h, 0, 0, w, h, sx, sy, use_rectangle == 1, 0, 0);
584 glEndList();
586 osdtexCnt++;
589 static void do_render_osd(void);
591 static void draw_osd(void)
593 if (!use_osd) return;
594 if (vo_osd_changed(0)) {
595 int osd_h, osd_w;
596 clearOSD();
597 osd_w = scaled_osd ? image_width : vo_dwidth;
598 osd_h = scaled_osd ? image_height : vo_dheight;
599 vo_draw_text(osd_w, osd_h, create_osd_texture);
601 if (vo_doublebuffering) do_render_osd();
604 static void do_render(void) {
605 // glEnable(GL_TEXTURE_2D);
606 // glBindTexture(GL_TEXTURE_2D, texture_id);
608 glColor3f(1,1,1);
609 if (image_format == IMGFMT_YV12)
610 glEnableYUVConversion(gl_target, yuvconvtype);
611 glDrawTex(0, 0, image_width, image_height,
612 0, 0, image_width, image_height,
613 texture_width, texture_height,
614 use_rectangle == 1, image_format == IMGFMT_YV12,
615 mpi_flipped ^ vo_flipped);
616 if (image_format == IMGFMT_YV12)
617 glDisableYUVConversion(gl_target, yuvconvtype);
620 static void do_render_osd(void) {
621 if (osdtexCnt > 0 || eosdDispList) {
622 // set special rendering parameters
623 if (!scaled_osd) {
624 glMatrixMode(GL_PROJECTION);
625 glPushMatrix();
626 glLoadIdentity();
627 glOrtho(0, vo_dwidth, vo_dheight, 0, -1, 1);
629 glEnable(GL_BLEND);
630 if (eosdDispList) {
631 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
632 glCallList(eosdDispList);
634 if (osdtexCnt > 0) {
635 glColor4ub((osd_color >> 16) & 0xff, (osd_color >> 8) & 0xff, osd_color & 0xff, 0xff - (osd_color >> 24));
636 // draw OSD
637 #ifndef FAST_OSD
638 glBlendFunc(GL_ZERO, GL_ONE_MINUS_SRC_ALPHA);
639 glCallLists(osdtexCnt, GL_UNSIGNED_INT, osdaDispList);
640 #endif
641 glBlendFunc(GL_SRC_ALPHA, GL_ONE);
642 glCallLists(osdtexCnt, GL_UNSIGNED_INT, osdDispList);
644 // set rendering parameters back to defaults
645 glDisable(GL_BLEND);
646 if (!scaled_osd)
647 glPopMatrix();
648 BindTexture(gl_target, 0);
652 static void flip_page(void) {
653 if (vo_doublebuffering) {
654 if (use_glFinish) glFinish();
655 swapGlBuffers();
656 if (vo_fs && use_aspect)
657 glClear(GL_COLOR_BUFFER_BIT);
658 } else {
659 do_render();
660 do_render_osd();
661 if (use_glFinish) glFinish();
662 else glFlush();
666 static void redraw(void) {
667 if (vo_doublebuffering) { do_render(); do_render_osd(); }
668 flip_page();
671 //static inline uint32_t draw_slice_x11(uint8_t *src[], uint32_t slice_num)
672 static int draw_slice(uint8_t *src[], int stride[], int w,int h,int x,int y)
674 mpi_flipped = stride[0] < 0;
675 glUploadTex(gl_target, gl_format, gl_type, src[0], stride[0],
676 x, y, w, h, slice_height);
677 if (image_format == IMGFMT_YV12) {
678 ActiveTexture(GL_TEXTURE1);
679 glUploadTex(gl_target, gl_format, gl_type, src[1], stride[1],
680 x / 2, y / 2, w / 2, h / 2, slice_height);
681 ActiveTexture(GL_TEXTURE2);
682 glUploadTex(gl_target, gl_format, gl_type, src[2], stride[2],
683 x / 2, y / 2, w / 2, h / 2, slice_height);
684 ActiveTexture(GL_TEXTURE0);
686 return 0;
689 static uint32_t get_image(mp_image_t *mpi) {
690 int needed_size;
691 if (!GenBuffers || !BindBuffer || !BufferData || !MapBuffer) {
692 if (!err_shown)
693 mp_msg(MSGT_VO, MSGL_ERR, "[gl] extensions missing for dr\n"
694 "Expect a _major_ speed penalty\n");
695 err_shown = 1;
696 return VO_FALSE;
698 if (mpi->flags & MP_IMGFLAG_READABLE) return VO_FALSE;
699 if (ati_hack) {
700 mpi->width = texture_width;
701 mpi->height = texture_height;
703 mpi->stride[0] = mpi->width * mpi->bpp / 8;
704 needed_size = mpi->stride[0] * mpi->height;
705 if (mesa_buffer) {
706 #ifndef GL_WIN32
707 if (mesa_bufferptr && needed_size > mesa_buffersize) {
708 FreeMemoryMESA(mDisplay, mScreen, mesa_bufferptr);
709 mesa_bufferptr = NULL;
711 if (!mesa_bufferptr)
712 mesa_bufferptr = AllocateMemoryMESA(mDisplay, mScreen, needed_size, 0, 0, 0);
713 mesa_buffersize = needed_size;
714 #endif
715 mpi->planes[0] = mesa_bufferptr;
716 } else {
717 if (!gl_buffer)
718 GenBuffers(1, &gl_buffer);
719 BindBuffer(GL_PIXEL_UNPACK_BUFFER, gl_buffer);
720 if (needed_size > gl_buffersize) {
721 gl_buffersize = needed_size;
722 BufferData(GL_PIXEL_UNPACK_BUFFER, gl_buffersize,
723 NULL, GL_DYNAMIC_DRAW);
725 if (!gl_bufferptr)
726 gl_bufferptr = MapBuffer(GL_PIXEL_UNPACK_BUFFER, GL_WRITE_ONLY);
727 mpi->planes[0] = gl_bufferptr;
728 BindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
730 if (!mpi->planes[0]) {
731 if (!err_shown)
732 mp_msg(MSGT_VO, MSGL_ERR, "[gl] could not acquire buffer for dr\n"
733 "Expect a _major_ speed penalty\n");
734 err_shown = 1;
735 return VO_FALSE;
737 if (mpi->imgfmt == IMGFMT_YV12) {
738 // YV12
739 mpi->flags |= MP_IMGFLAG_COMMON_STRIDE | MP_IMGFLAG_COMMON_PLANE;
740 mpi->stride[0] = mpi->width;
741 mpi->planes[1] = mpi->planes[0] + mpi->stride[0] * mpi->height;
742 mpi->stride[1] = mpi->width >> 1;
743 mpi->planes[2] = mpi->planes[1] + mpi->stride[1] * (mpi->height >> 1);
744 mpi->stride[2] = mpi->width >> 1;
745 if (ati_hack) {
746 mpi->flags &= ~MP_IMGFLAG_COMMON_PLANE;
747 if (!gl_buffer_uv[0]) GenBuffers(2, gl_buffer_uv);
748 if (mpi->stride[1] * mpi->height > gl_buffersize_uv) {
749 BindBuffer(GL_PIXEL_UNPACK_BUFFER, gl_buffer_uv[0]);
750 BufferData(GL_PIXEL_UNPACK_BUFFER, mpi->stride[1] * mpi->height,
751 NULL, GL_DYNAMIC_DRAW);
752 BindBuffer(GL_PIXEL_UNPACK_BUFFER, gl_buffer_uv[1]);
753 BufferData(GL_PIXEL_UNPACK_BUFFER, mpi->stride[1] * mpi->height,
754 NULL, GL_DYNAMIC_DRAW);
755 gl_buffersize_uv = mpi->stride[1] * mpi->height;
757 if (!gl_bufferptr_uv[0]) {
758 BindBuffer(GL_PIXEL_UNPACK_BUFFER, gl_buffer_uv[0]);
759 gl_bufferptr_uv[0] = MapBuffer(GL_PIXEL_UNPACK_BUFFER, GL_WRITE_ONLY);
760 BindBuffer(GL_PIXEL_UNPACK_BUFFER, gl_buffer_uv[1]);
761 gl_bufferptr_uv[1] = MapBuffer(GL_PIXEL_UNPACK_BUFFER, GL_WRITE_ONLY);
763 mpi->planes[1] = gl_bufferptr_uv[0];
764 mpi->planes[2] = gl_bufferptr_uv[1];
767 mpi->flags |= MP_IMGFLAG_DIRECT;
768 return VO_TRUE;
771 static uint32_t draw_image(mp_image_t *mpi) {
772 int slice = slice_height;
773 int stride[3];
774 unsigned char *planes[3];
775 mp_image_t mpi2 = *mpi;
776 int w = mpi->w, h = mpi->h;
777 if (ati_hack) { w = texture_width; h = texture_height; }
778 if (mpi->flags & MP_IMGFLAG_DRAW_CALLBACK)
779 goto skip_upload;
780 mpi2.flags = 0; mpi2.type = MP_IMGTYPE_TEMP;
781 mpi2.width = mpi2.w; mpi2.height = mpi2.h;
782 if (force_pbo && !(mpi->flags & MP_IMGFLAG_DIRECT) && !gl_bufferptr && get_image(&mpi2) == VO_TRUE) {
783 int bpp = mpi->imgfmt == IMGFMT_YV12 ? 8 : mpi->bpp;
784 memcpy_pic(mpi2.planes[0], mpi->planes[0], mpi->w * bpp / 8, mpi->h, mpi2.stride[0], mpi->stride[0]);
785 if (mpi->imgfmt == IMGFMT_YV12) {
786 memcpy_pic(mpi2.planes[1], mpi->planes[1], mpi->w >> 1, mpi->h >> 1, mpi2.stride[1], mpi->stride[1]);
787 memcpy_pic(mpi2.planes[2], mpi->planes[2], mpi->w >> 1, mpi->h >> 1, mpi2.stride[2], mpi->stride[2]);
789 mpi = &mpi2;
791 stride[0] = mpi->stride[0]; stride[1] = mpi->stride[1]; stride[2] = mpi->stride[2];
792 planes[0] = mpi->planes[0]; planes[1] = mpi->planes[1]; planes[2] = mpi->planes[2];
793 mpi_flipped = stride[0] < 0;
794 if (!mesa_buffer && mpi->flags & MP_IMGFLAG_DIRECT) {
795 intptr_t base = (intptr_t)planes[0];
796 if (mpi_flipped)
797 base += (mpi->h - 1) * stride[0];
798 planes[0] -= base;
799 planes[1] -= base;
800 planes[2] -= base;
801 BindBuffer(GL_PIXEL_UNPACK_BUFFER, gl_buffer);
802 UnmapBuffer(GL_PIXEL_UNPACK_BUFFER);
803 gl_bufferptr = NULL;
804 if (!(mpi->flags & MP_IMGFLAG_COMMON_PLANE))
805 planes[0] = planes[1] = planes[2] = NULL;
807 if (mpi->flags & MP_IMGFLAG_DIRECT)
808 slice = 0; // always "upload" full texture
809 glUploadTex(gl_target, gl_format, gl_type, planes[0], stride[0],
810 mpi->x, mpi->y, w, h, slice);
811 if (mpi->imgfmt == IMGFMT_YV12) {
812 if ((mpi->flags & MP_IMGFLAG_DIRECT) && !(mpi->flags & MP_IMGFLAG_COMMON_PLANE)) {
813 BindBuffer(GL_PIXEL_UNPACK_BUFFER, gl_buffer_uv[0]);
814 UnmapBuffer(GL_PIXEL_UNPACK_BUFFER);
815 gl_bufferptr_uv[0] = NULL;
817 ActiveTexture(GL_TEXTURE1);
818 glUploadTex(gl_target, gl_format, gl_type, planes[1], stride[1],
819 mpi->x / 2, mpi->y / 2, w / 2, h / 2, slice);
820 if ((mpi->flags & MP_IMGFLAG_DIRECT) && !(mpi->flags & MP_IMGFLAG_COMMON_PLANE)) {
821 BindBuffer(GL_PIXEL_UNPACK_BUFFER, gl_buffer_uv[1]);
822 UnmapBuffer(GL_PIXEL_UNPACK_BUFFER);
823 gl_bufferptr_uv[1] = NULL;
825 ActiveTexture(GL_TEXTURE2);
826 glUploadTex(gl_target, gl_format, gl_type, planes[2], stride[2],
827 mpi->x / 2, mpi->y / 2, w / 2, h / 2, slice);
828 ActiveTexture(GL_TEXTURE0);
830 if (!mesa_buffer && mpi->flags & MP_IMGFLAG_DIRECT)
831 BindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
832 skip_upload:
833 if (vo_doublebuffering) do_render();
834 return VO_TRUE;
837 static int
838 draw_frame(uint8_t *src[])
840 return VO_ERROR;
843 static int
844 query_format(uint32_t format)
846 int caps = VFCAP_CSP_SUPPORTED | VFCAP_CSP_SUPPORTED_BY_HW |
847 VFCAP_FLIP |
848 VFCAP_HWSCALE_UP | VFCAP_HWSCALE_DOWN | VFCAP_ACCEPT_STRIDE;
849 if (use_osd)
850 caps |= VFCAP_OSD | VFCAP_EOSD | (scaled_osd ? 0 : VFCAP_EOSD_UNSCALED);
851 if (format == IMGFMT_RGB24 || format == IMGFMT_RGBA)
852 return caps;
853 if (use_yuv && format == IMGFMT_YV12)
854 return caps;
855 // HACK, otherwise we get only b&w with some filters (e.g. -vf eq)
856 // ideally MPlayer should be fixed instead not to use Y800 when it has the choice
857 if (!use_yuv && (format == IMGFMT_Y8 || format == IMGFMT_Y800))
858 return 0;
859 if (!use_ycbcr && (format == IMGFMT_UYVY || format == IMGFMT_YUY2))
860 return 0;
861 if (many_fmts &&
862 glFindFormat(format, NULL, NULL, NULL, NULL))
863 return caps;
864 return 0;
868 static void
869 uninit(void)
871 if (!vo_config_count) return;
872 uninitGl();
873 releaseGlContext(&gl_vinfo, &gl_context);
874 if (custom_prog) free(custom_prog);
875 custom_prog = NULL;
876 if (custom_tex) free(custom_tex);
877 custom_tex = NULL;
878 vo_uninit();
881 static opt_t subopts[] = {
882 {"manyfmts", OPT_ARG_BOOL, &many_fmts, NULL},
883 {"osd", OPT_ARG_BOOL, &use_osd, NULL},
884 {"scaled-osd", OPT_ARG_BOOL, &scaled_osd, NULL},
885 {"aspect", OPT_ARG_BOOL, &use_aspect, NULL},
886 {"ycbcr", OPT_ARG_BOOL, &use_ycbcr, NULL},
887 {"slice-height", OPT_ARG_INT, &slice_height, (opt_test_f)int_non_neg},
888 {"rectangle", OPT_ARG_INT, &use_rectangle,(opt_test_f)int_non_neg},
889 {"yuv", OPT_ARG_INT, &use_yuv, (opt_test_f)int_non_neg},
890 {"lscale", OPT_ARG_INT, &lscale, (opt_test_f)int_non_neg},
891 {"cscale", OPT_ARG_INT, &cscale, (opt_test_f)int_non_neg},
892 {"filter-strength", OPT_ARG_FLOAT, &filter_strength, NULL},
893 {"ati-hack", OPT_ARG_BOOL, &ati_hack, NULL},
894 {"force-pbo", OPT_ARG_BOOL, &force_pbo, NULL},
895 {"mesa-buffer", OPT_ARG_BOOL, &mesa_buffer, NULL},
896 {"glfinish", OPT_ARG_BOOL, &use_glFinish, NULL},
897 {"swapinterval", OPT_ARG_INT, &swap_interval,NULL},
898 {"customprog", OPT_ARG_MSTRZ,&custom_prog, NULL},
899 {"customtex", OPT_ARG_MSTRZ,&custom_tex, NULL},
900 {"customtlin", OPT_ARG_BOOL, &custom_tlin, NULL},
901 {"customtrect", OPT_ARG_BOOL, &custom_trect, NULL},
902 {"osdcolor", OPT_ARG_INT, &osd_color, NULL},
903 {NULL}
906 static int preinit(const char *arg)
908 // set defaults
909 many_fmts = 1;
910 use_osd = 1;
911 scaled_osd = 0;
912 use_aspect = 1;
913 use_ycbcr = 0;
914 use_yuv = 0;
915 lscale = 0;
916 cscale = 0;
917 filter_strength = 0.5;
918 use_rectangle = 0;
919 use_glFinish = 0;
920 ati_hack = 0;
921 force_pbo = 0;
922 mesa_buffer = 0;
923 swap_interval = 1;
924 slice_height = 0;
925 custom_prog = NULL;
926 custom_tex = NULL;
927 custom_tlin = 1;
928 custom_trect = 0;
929 osd_color = 0xffffff;
930 if (subopt_parse(arg, subopts) != 0) {
931 mp_msg(MSGT_VO, MSGL_FATAL,
932 "\n-vo gl command line help:\n"
933 "Example: mplayer -vo gl:slice-height=4\n"
934 "\nOptions:\n"
935 " nomanyfmts\n"
936 " Disable extended color formats for OpenGL 1.2 and later\n"
937 " slice-height=<0-...>\n"
938 " Slice size for texture transfer, 0 for whole image\n"
939 " noosd\n"
940 " Do not use OpenGL OSD code\n"
941 " scaled-osd\n"
942 " Render OSD at movie resolution and scale it\n"
943 " noaspect\n"
944 " Do not do aspect scaling\n"
945 " rectangle=<0,1,2>\n"
946 " 0: use power-of-two textures\n"
947 " 1: use texture_rectangle\n"
948 " 2: use texture_non_power_of_two\n"
949 " ati-hack\n"
950 " Workaround ATI bug with PBOs\n"
951 " force-pbo\n"
952 " Force use of PBO even if this involves an extra memcpy\n"
953 " glfinish\n"
954 " Call glFinish() before swapping buffers\n"
955 " swapinterval=<n>\n"
956 " Interval in displayed frames between to buffer swaps.\n"
957 " 1 is equivalent to enable VSYNC, 0 to disable VSYNC.\n"
958 " Requires GLX_SGI_swap_control support to work.\n"
959 " yuv=<n>\n"
960 " 0: use software YUV to RGB conversion.\n"
961 " 1: use register combiners (nVidia only, for older cards).\n"
962 " 2: use fragment program.\n"
963 " 3: use fragment program with gamma correction.\n"
964 " 4: use fragment program with gamma correction via lookup.\n"
965 " 5: use ATI-specific method (for older cards).\n"
966 " 6: use lookup via 3D texture.\n"
967 " lscale=<n>\n"
968 " 0: use standard bilinear scaling for luma.\n"
969 " 1: use improved bicubic scaling for luma.\n"
970 " 2: use cubic in X, linear in Y direction scaling for luma.\n"
971 " 3: as 1 but without using a lookup texture.\n"
972 " 4: experimental unsharp masking (sharpening).\n"
973 " 5: experimental unsharp masking (sharpening) with larger radius.\n"
974 " cscale=<n>\n"
975 " as lscale but for chroma (2x slower with little visible effect).\n"
976 " filter-strength=<value>\n"
977 " set the effect strength for some lscale/cscale filters\n"
978 " customprog=<filename>\n"
979 " use a custom YUV conversion program\n"
980 " customtex=<filename>\n"
981 " use a custom YUV conversion lookup texture\n"
982 " nocustomtlin\n"
983 " use GL_NEAREST scaling for customtex texture\n"
984 " customtrect\n"
985 " use texture_rectangle for customtex texture\n"
986 " osdcolor=<0xAARRGGBB>\n"
987 " use the given color for the OSD\n"
988 " ycbcr\n"
989 " also try to use the GL_MESA_ycbcr_texture extension\n"
990 "\n" );
991 return -1;
993 if (use_rectangle == 1)
994 gl_target = GL_TEXTURE_RECTANGLE;
995 else
996 gl_target = GL_TEXTURE_2D;
997 yuvconvtype = use_yuv | lscale << YUV_LUM_SCALER_SHIFT | cscale << YUV_CHROM_SCALER_SHIFT;
998 if (many_fmts)
999 mp_msg(MSGT_VO, MSGL_INFO, "[gl] using extended formats. "
1000 "Use -vo gl:nomanyfmts if playback fails.\n");
1001 mp_msg(MSGT_VO, MSGL_V, "[gl] Using %d as slice height "
1002 "(0 means image height).\n", slice_height);
1003 if (!vo_init()) return -1; // Can't open X11
1005 return 0;
1008 #define MASK_ALL_YUV (~(1 << YUV_CONVERSION_NONE))
1009 #define MASK_NOT_COMBINERS (~((1 << YUV_CONVERSION_NONE) | (1 << YUV_CONVERSION_COMBINERS) | (1 << YUV_CONVERSION_COMBINERS_ATI)))
1010 #define MASK_GAMMA_SUPPORT (MASK_NOT_COMBINERS & ~(1 << YUV_CONVERSION_FRAGMENT))
1012 static const struct {
1013 const char *name;
1014 int *value;
1015 int supportmask;
1016 } eq_map[] = {
1017 {"brightness", &eq_bri, MASK_NOT_COMBINERS},
1018 {"contrast", &eq_cont, MASK_NOT_COMBINERS},
1019 {"saturation", &eq_sat, MASK_ALL_YUV },
1020 {"hue", &eq_hue, MASK_ALL_YUV },
1021 {"gamma", &eq_rgamma, MASK_GAMMA_SUPPORT},
1022 {"red_gamma", &eq_rgamma, MASK_GAMMA_SUPPORT},
1023 {"green_gamma", &eq_ggamma, MASK_GAMMA_SUPPORT},
1024 {"blue_gamma", &eq_bgamma, MASK_GAMMA_SUPPORT},
1025 {NULL, NULL, 0 }
1028 static int control(uint32_t request, void *data, ...)
1030 switch (request) {
1031 case VOCTRL_PAUSE:
1032 case VOCTRL_RESUME:
1033 int_pause = (request == VOCTRL_PAUSE);
1034 return VO_TRUE;
1035 case VOCTRL_QUERY_FORMAT:
1036 return query_format(*(uint32_t*)data);
1037 case VOCTRL_GET_IMAGE:
1038 return get_image(data);
1039 case VOCTRL_DRAW_IMAGE:
1040 return draw_image(data);
1041 case VOCTRL_DRAW_EOSD:
1042 if (!data)
1043 return VO_FALSE;
1044 genEOSD(data);
1045 return VO_TRUE;
1046 case VOCTRL_GET_EOSD_RES:
1048 mp_eosd_res_t *r = data;
1049 r->mt = r->mb = r->ml = r->mr = 0;
1050 if (scaled_osd) {r->w = image_width; r->h = image_height;}
1051 else if (vo_fs) {
1052 r->w = vo_screenwidth; r->h = vo_screenheight;
1053 r->ml = r->mr = ass_border_x;
1054 r->mt = r->mb = ass_border_y;
1055 } else {
1056 r->w = vo_dwidth; r->h = vo_dheight;
1059 return VO_TRUE;
1060 case VOCTRL_GUISUPPORT:
1061 return VO_TRUE;
1062 case VOCTRL_ONTOP:
1063 vo_ontop();
1064 return VO_TRUE;
1065 case VOCTRL_FULLSCREEN:
1066 vo_fullscreen();
1067 resize(vo_dwidth, vo_dheight);
1068 return VO_TRUE;
1069 case VOCTRL_BORDER:
1070 vo_border();
1071 resize(vo_dwidth, vo_dheight);
1072 return VO_TRUE;
1073 case VOCTRL_GET_PANSCAN:
1074 if (!use_aspect) return VO_NOTIMPL;
1075 return VO_TRUE;
1076 case VOCTRL_SET_PANSCAN:
1077 if (!use_aspect) return VO_NOTIMPL;
1078 resize(vo_dwidth, vo_dheight);
1079 return VO_TRUE;
1080 case VOCTRL_GET_EQUALIZER:
1081 if (image_format == IMGFMT_YV12) {
1082 int i;
1083 va_list va;
1084 int *value;
1085 va_start(va, data);
1086 value = va_arg(va, int *);
1087 va_end(va);
1088 for (i = 0; eq_map[i].name; i++)
1089 if (strcmp(data, eq_map[i].name) == 0) break;
1090 if (!(eq_map[i].supportmask & (1 << use_yuv)))
1091 break;
1092 *value = *eq_map[i].value;
1093 return VO_TRUE;
1095 break;
1096 case VOCTRL_SET_EQUALIZER:
1097 if (image_format == IMGFMT_YV12) {
1098 int i;
1099 va_list va;
1100 int value;
1101 va_start(va, data);
1102 value = va_arg(va, int);
1103 va_end(va);
1104 for (i = 0; eq_map[i].name; i++)
1105 if (strcmp(data, eq_map[i].name) == 0) break;
1106 if (!(eq_map[i].supportmask & (1 << use_yuv)))
1107 break;
1108 *eq_map[i].value = value;
1109 update_yuvconv();
1110 return VO_TRUE;
1112 break;
1113 case VOCTRL_UPDATE_SCREENINFO:
1114 update_xinerama_info();
1115 return VO_TRUE;
1117 return VO_NOTIMPL;