Merge svn changes up to r28087
[mplayer.git] / libvo / vo_gl.c
blob792376c8bb46a6e94de86c7c9a87b183758a006d
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_yuv;
75 static int lscale;
76 static int cscale;
77 static float filter_strength;
78 static int yuvconvtype;
79 static int use_rectangle;
80 static int err_shown;
81 static uint32_t image_width;
82 static uint32_t image_height;
83 static uint32_t image_format;
84 static int many_fmts;
85 static int ati_hack;
86 static int force_pbo;
87 static int use_glFinish;
88 static int swap_interval;
89 static GLenum gl_target;
90 static GLint gl_texfmt;
91 static GLenum gl_format;
92 static GLenum gl_type;
93 static GLuint gl_buffer;
94 static GLuint gl_buffer_uv[2];
95 static int gl_buffersize;
96 static int gl_buffersize_uv;
97 static void *gl_bufferptr;
98 static void *gl_bufferptr_uv[2];
99 static GLuint fragprog;
100 static GLuint default_texs[22];
101 static char *custom_prog;
102 static char *custom_tex;
103 static int custom_tlin;
104 static int custom_trect;
106 static int int_pause;
107 static int eq_bri = 0;
108 static int eq_cont = 0;
109 static int eq_sat = 0;
110 static int eq_hue = 0;
111 static int eq_rgamma = 0;
112 static int eq_ggamma = 0;
113 static int eq_bgamma = 0;
115 static int texture_width;
116 static int texture_height;
117 static int mpi_flipped;
118 static int vo_flipped;
119 static int ass_border_x, ass_border_y;
121 static unsigned int slice_height = 1;
123 static void redraw(void);
125 static void resize(int x,int y){
126 mp_msg(MSGT_VO, MSGL_V, "[gl] Resize: %dx%d\n",x,y);
127 if (WinID >= 0) {
128 int top = 0, left = 0, w = x, h = y;
129 geometry(&top, &left, &w, &h, vo_screenwidth, vo_screenheight);
130 glViewport(top, left, w, h);
131 } else
132 glViewport( 0, 0, x, y );
134 glMatrixMode(GL_PROJECTION);
135 glLoadIdentity();
136 if (vo_fs && use_aspect) {
137 int new_w, new_h;
138 GLdouble scale_x, scale_y;
139 aspect(&new_w, &new_h, A_ZOOM);
140 panscan_calc();
141 new_w += vo_panscan_x;
142 new_h += vo_panscan_y;
143 scale_x = (GLdouble)new_w / (GLdouble)x;
144 scale_y = (GLdouble)new_h / (GLdouble)y;
145 glScaled(scale_x, scale_y, 1);
146 ass_border_x = (vo_screenwidth - new_w) / 2;
147 ass_border_y = (vo_screenheight - new_h) / 2;
149 glOrtho(0, image_width, image_height, 0, -1,1);
151 glMatrixMode(GL_MODELVIEW);
152 glLoadIdentity();
154 if (!scaled_osd) {
155 #ifdef CONFIG_FREETYPE
156 // adjust font size to display size
157 force_load_font = 1;
158 #endif
159 vo_osd_changed(OSDTYPE_OSD);
161 glClear(GL_COLOR_BUFFER_BIT);
162 redraw();
165 static void texSize(int w, int h, int *texw, int *texh) {
166 if (use_rectangle) {
167 *texw = w; *texh = h;
168 } else {
169 *texw = 32;
170 while (*texw < w)
171 *texw *= 2;
172 *texh = 32;
173 while (*texh < h)
174 *texh *= 2;
176 if (ati_hack) *texw = (*texw + 511) & ~511;
179 //! maximum size of custom fragment program
180 #define MAX_CUSTOM_PROG_SIZE (1024 * 1024)
181 static void update_yuvconv(void) {
182 float bri = eq_bri / 100.0;
183 float cont = (eq_cont + 100) / 100.0;
184 float hue = eq_hue / 100.0 * 3.1415927;
185 float sat = (eq_sat + 100) / 100.0;
186 float rgamma = exp(log(8.0) * eq_rgamma / 100.0);
187 float ggamma = exp(log(8.0) * eq_ggamma / 100.0);
188 float bgamma = exp(log(8.0) * eq_bgamma / 100.0);
189 gl_conversion_params_t params = {gl_target, yuvconvtype,
190 bri, cont, hue, sat, rgamma, ggamma, bgamma,
191 texture_width, texture_height, filter_strength};
192 glSetupYUVConversion(&params);
193 if (custom_prog) {
194 FILE *f = fopen(custom_prog, "r");
195 if (!f)
196 mp_msg(MSGT_VO, MSGL_WARN,
197 "[gl] Could not read customprog %s\n", custom_prog);
198 else {
199 char *prog = calloc(1, MAX_CUSTOM_PROG_SIZE + 1);
200 fread(prog, 1, MAX_CUSTOM_PROG_SIZE, f);
201 fclose(f);
202 loadGPUProgram(GL_FRAGMENT_PROGRAM, prog);
203 free(prog);
205 ProgramEnvParameter4f(GL_FRAGMENT_PROGRAM, 0,
206 1.0 / texture_width, 1.0 / texture_height,
207 texture_width, texture_height);
209 if (custom_tex) {
210 FILE *f = fopen(custom_tex, "r");
211 if (!f)
212 mp_msg(MSGT_VO, MSGL_WARN,
213 "[gl] Could not read customtex %s\n", custom_tex);
214 else {
215 int width, height, maxval;
216 ActiveTexture(GL_TEXTURE3);
217 if (glCreatePPMTex(custom_trect?GL_TEXTURE_RECTANGLE:GL_TEXTURE_2D, 0,
218 custom_tlin?GL_LINEAR:GL_NEAREST,
219 f, &width, &height, &maxval))
220 ProgramEnvParameter4f(GL_FRAGMENT_PROGRAM, 1,
221 1.0 / width, 1.0 / height, width, height);
222 else
223 mp_msg(MSGT_VO, MSGL_WARN,
224 "[gl] Error parsing customtex %s\n", custom_tex);
225 fclose(f);
226 ActiveTexture(GL_TEXTURE0);
232 * \brief remove all OSD textures and display-lists, thus clearing it.
234 static void clearOSD(void) {
235 int i;
236 if (!osdtexCnt)
237 return;
238 glDeleteTextures(osdtexCnt, osdtex);
239 #ifndef FAST_OSD
240 glDeleteTextures(osdtexCnt, osdatex);
241 for (i = 0; i < osdtexCnt; i++)
242 glDeleteLists(osdaDispList[i], 1);
243 #endif
244 for (i = 0; i < osdtexCnt; i++)
245 glDeleteLists(osdDispList[i], 1);
246 osdtexCnt = 0;
250 * \brief remove textures, display list and free memory used by EOSD
252 static void clearEOSD(void) {
253 if (eosdDispList)
254 glDeleteLists(eosdDispList, 1);
255 eosdDispList = 0;
256 if (eosdtexCnt)
257 glDeleteTextures(eosdtexCnt, eosdtex);
258 eosdtexCnt = 0;
259 free(eosdtex);
260 eosdtex = NULL;
264 * \brief construct display list from ass image list
265 * \param img image list to create OSD from.
266 * A value of NULL has the same effect as clearEOSD()
268 static void genEOSD(mp_eosd_images_t *imgs) {
269 int sx, sy;
270 int tinytexcur = 0;
271 int smalltexcur = 0;
272 GLuint *curtex;
273 GLint scale_type = scaled_osd ? GL_LINEAR : GL_NEAREST;
274 ass_image_t *img = imgs->imgs;
275 ass_image_t *i;
277 if (imgs->changed == 0) // there are elements, but they are unchanged
278 return;
279 if (img && imgs->changed == 1) // there are elements, but they just moved
280 goto skip_upload;
282 clearEOSD();
283 if (!img)
284 return;
285 if (!largeeosdtex[0]) {
286 glGenTextures(2, largeeosdtex);
287 BindTexture(gl_target, largeeosdtex[0]);
288 glCreateClearTex(gl_target, GL_ALPHA, GL_ALPHA, GL_UNSIGNED_BYTE, scale_type, 512, 512, 0);
289 BindTexture(gl_target, largeeosdtex[1]);
290 glCreateClearTex(gl_target, GL_ALPHA, GL_ALPHA, GL_UNSIGNED_BYTE, scale_type, 512, 512, 0);
292 for (i = img; i; i = i->next)
294 if (i->w <= 0 || i->h <= 0 || i->stride < i->w)
295 continue;
296 if (i->w < 16 && i->h < 16 && tinytexcur < 1024)
297 tinytexcur++;
298 else if (i->w < 32 && i->h < 32 && smalltexcur < 256)
299 smalltexcur++;
300 else
301 eosdtexCnt++;
303 mp_msg(MSGT_VO, MSGL_DBG2, "EOSD counts (tiny, small, all): %i, %i, %i\n",
304 tinytexcur, smalltexcur, eosdtexCnt);
305 if (eosdtexCnt) {
306 eosdtex = calloc(eosdtexCnt, sizeof(GLuint));
307 glGenTextures(eosdtexCnt, eosdtex);
309 tinytexcur = smalltexcur = 0;
310 for (i = img, curtex = eosdtex; i; i = i->next) {
311 int x = 0, y = 0;
312 if (i->w <= 0 || i->h <= 0 || i->stride < i->w) {
313 mp_msg(MSGT_VO, MSGL_V, "Invalid dimensions OSD for part!\n");
314 continue;
316 if (i->w < 16 && i->h < 16 && tinytexcur < 1024) {
317 x = (tinytexcur & 31) << 4;
318 y = (tinytexcur >> 5) << 4;
319 BindTexture(gl_target, largeeosdtex[0]);
320 tinytexcur++;
321 } else if (i->w < 32 && i->h < 32 && smalltexcur < 256) {
322 x = (smalltexcur & 15) << 5;
323 y = (smalltexcur >> 4) << 5;
324 BindTexture(gl_target, largeeosdtex[1]);
325 smalltexcur++;
326 } else {
327 texSize(i->w, i->h, &sx, &sy);
328 BindTexture(gl_target, *curtex++);
329 glCreateClearTex(gl_target, GL_ALPHA, GL_ALPHA, GL_UNSIGNED_BYTE, scale_type, sx, sy, 0);
331 glUploadTex(gl_target, GL_ALPHA, GL_UNSIGNED_BYTE, i->bitmap, i->stride,
332 x, y, i->w, i->h, 0);
334 eosdDispList = glGenLists(1);
335 skip_upload:
336 glNewList(eosdDispList, GL_COMPILE);
337 tinytexcur = smalltexcur = 0;
338 for (i = img, curtex = eosdtex; i; i = i->next) {
339 int x = 0, y = 0;
340 if (i->w <= 0 || i->h <= 0 || i->stride < i->w)
341 continue;
342 glColor4ub(i->color >> 24, (i->color >> 16) & 0xff, (i->color >> 8) & 0xff, 255 - (i->color & 0xff));
343 if (i->w < 16 && i->h < 16 && tinytexcur < 1024) {
344 x = (tinytexcur & 31) << 4;
345 y = (tinytexcur >> 5) << 4;
346 sx = sy = 512;
347 BindTexture(gl_target, largeeosdtex[0]);
348 tinytexcur++;
349 } else if (i->w < 32 && i->h < 32 && smalltexcur < 256) {
350 x = (smalltexcur & 15) << 5;
351 y = (smalltexcur >> 4) << 5;
352 sx = sy = 512;
353 BindTexture(gl_target, largeeosdtex[1]);
354 smalltexcur++;
355 } else {
356 texSize(i->w, i->h, &sx, &sy);
357 BindTexture(gl_target, *curtex++);
359 glDrawTex(i->dst_x, i->dst_y, i->w, i->h, x, y, i->w, i->h, sx, sy, use_rectangle == 1, 0, 0);
361 glEndList();
362 BindTexture(gl_target, 0);
366 * \brief uninitialize OpenGL context, freeing textures, buffers etc.
368 static void uninitGl(void) {
369 int i = 0;
370 if (DeletePrograms && fragprog)
371 DeletePrograms(1, &fragprog);
372 fragprog = 0;
373 while (default_texs[i] != 0)
374 i++;
375 if (i)
376 glDeleteTextures(i, default_texs);
377 default_texs[0] = 0;
378 clearOSD();
379 clearEOSD();
380 if (largeeosdtex[0])
381 glDeleteTextures(2, largeeosdtex);
382 largeeosdtex[0] = 0;
383 if (DeleteBuffers && gl_buffer)
384 DeleteBuffers(1, &gl_buffer);
385 gl_buffer = 0; gl_buffersize = 0;
386 gl_bufferptr = NULL;
387 if (DeleteBuffers && gl_buffer_uv[0])
388 DeleteBuffers(2, gl_buffer_uv);
389 gl_buffer_uv[0] = gl_buffer_uv[1] = 0; gl_buffersize_uv = 0;
390 gl_bufferptr_uv[0] = gl_bufferptr_uv[1] = 0;
391 err_shown = 0;
395 * \brief Initialize a (new or reused) OpenGL context.
396 * set global gl-related variables to their default values
398 static int initGl(uint32_t d_width, uint32_t d_height) {
399 texSize(image_width, image_height, &texture_width, &texture_height);
401 glDisable(GL_BLEND);
402 glDisable(GL_DEPTH_TEST);
403 glDepthMask(GL_FALSE);
404 glDisable(GL_CULL_FACE);
405 glEnable(gl_target);
406 glDrawBuffer(vo_doublebuffering?GL_BACK:GL_FRONT);
407 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
409 mp_msg(MSGT_VO, MSGL_V, "[gl] Creating %dx%d texture...\n",
410 texture_width, texture_height);
412 if (image_format == IMGFMT_YV12) {
413 int i;
414 glGenTextures(21, default_texs);
415 default_texs[21] = 0;
416 for (i = 0; i < 7; i++) {
417 ActiveTexture(GL_TEXTURE1 + i);
418 BindTexture(GL_TEXTURE_2D, default_texs[i]);
419 BindTexture(GL_TEXTURE_RECTANGLE, default_texs[i + 7]);
420 BindTexture(GL_TEXTURE_3D, default_texs[i + 14]);
422 ActiveTexture(GL_TEXTURE1);
423 glCreateClearTex(gl_target, gl_texfmt, gl_format, gl_type, GL_LINEAR,
424 texture_width / 2, texture_height / 2, 128);
425 ActiveTexture(GL_TEXTURE2);
426 glCreateClearTex(gl_target, gl_texfmt, gl_format, gl_type, GL_LINEAR,
427 texture_width / 2, texture_height / 2, 128);
428 switch (use_yuv) {
429 case YUV_CONVERSION_FRAGMENT_LOOKUP:
430 case YUV_CONVERSION_FRAGMENT_POW:
431 case YUV_CONVERSION_FRAGMENT:
432 if (!GenPrograms || !BindProgram) {
433 mp_msg(MSGT_VO, MSGL_ERR, "[gl] fragment program functions missing!\n");
434 break;
436 GenPrograms(1, &fragprog);
437 BindProgram(GL_FRAGMENT_PROGRAM, fragprog);
438 break;
440 ActiveTexture(GL_TEXTURE0);
441 BindTexture(gl_target, 0);
442 update_yuvconv();
444 glCreateClearTex(gl_target, gl_texfmt, gl_format, gl_type, GL_LINEAR,
445 texture_width, texture_height, 0);
447 resize(d_width, d_height);
449 glClearColor( 0.0f,0.0f,0.0f,0.0f );
450 glClear( GL_COLOR_BUFFER_BIT );
451 if (SwapInterval && swap_interval >= 0)
452 SwapInterval(swap_interval);
453 return 1;
456 /* connect to server, create and map window,
457 * allocate colors and (shared) memory
459 static int
460 config(uint32_t width, uint32_t height, uint32_t d_width, uint32_t d_height, uint32_t flags, char *title, uint32_t format)
462 image_height = height;
463 image_width = width;
464 image_format = format;
465 glFindFormat(format, NULL, &gl_texfmt, &gl_format, &gl_type);
467 int_pause = 0;
468 vo_flipped = !!(flags & VOFLAG_FLIPPING);
470 #ifdef CONFIG_GUI
471 if (use_gui) {
472 // GUI creates and manages window for us
473 guiGetEvent(guiSetShVideo, 0);
474 #ifndef GL_WIN32
475 goto glconfig;
476 #endif
478 #endif
479 #ifdef GL_WIN32
480 if (!vo_w32_config(d_width, d_height, flags))
481 return -1;
482 #else
484 XVisualInfo *vinfo=glXChooseVisual( mDisplay,mScreen,wsGLXAttrib );
485 if (vinfo == NULL)
487 mp_msg(MSGT_VO, MSGL_ERR, "[gl] no GLX support present\n");
488 return -1;
491 vo_x11_create_vo_window(vinfo, vo_dx, vo_dy, d_width, d_height, flags,
492 XCreateColormap(mDisplay, mRootWin, vinfo->visual, AllocNone),
493 "gl", title);
495 #endif
497 glconfig:
498 if (vo_config_count)
499 uninitGl();
500 setGlWindow(&gl_vinfo, &gl_context, vo_window);
501 initGl(vo_dwidth, vo_dheight);
503 return 0;
506 static void check_events(void)
508 int e=vo_check_events();
509 if(e&VO_EVENT_RESIZE) resize(vo_dwidth,vo_dheight);
510 if(e&VO_EVENT_EXPOSE && int_pause) redraw();
514 * Creates the textures and the display list needed for displaying
515 * an OSD part.
516 * Callback function for vo_draw_text().
518 static void create_osd_texture(int x0, int y0, int w, int h,
519 unsigned char *src, unsigned char *srca,
520 int stride)
522 // initialize to 8 to avoid special-casing on alignment
523 int sx = 8, sy = 8;
524 GLint scale_type = scaled_osd ? GL_LINEAR : GL_NEAREST;
526 if (w <= 0 || h <= 0 || stride < w) {
527 mp_msg(MSGT_VO, MSGL_V, "Invalid dimensions OSD for part!\n");
528 return;
530 texSize(w, h, &sx, &sy);
532 if (osdtexCnt >= MAX_OSD_PARTS) {
533 mp_msg(MSGT_VO, MSGL_ERR, "Too many OSD parts, contact the developers!\n");
534 return;
537 // create Textures for OSD part
538 glGenTextures(1, &osdtex[osdtexCnt]);
539 BindTexture(gl_target, osdtex[osdtexCnt]);
540 glCreateClearTex(gl_target, GL_LUMINANCE, GL_LUMINANCE, GL_UNSIGNED_BYTE, scale_type, sx, sy, 0);
541 glUploadTex(gl_target, GL_LUMINANCE, GL_UNSIGNED_BYTE, src, stride,
542 0, 0, w, h, 0);
544 #ifndef FAST_OSD
545 glGenTextures(1, &osdatex[osdtexCnt]);
546 BindTexture(gl_target, osdatex[osdtexCnt]);
547 glCreateClearTex(gl_target, GL_ALPHA, GL_ALPHA, GL_UNSIGNED_BYTE, scale_type, sx, sy, 0);
549 int i;
550 char *tmp = malloc(stride * h);
551 // convert alpha from weird MPlayer scale.
552 // in-place is not possible since it is reused for future OSDs
553 for (i = h * stride - 1; i >= 0; i--)
554 tmp[i] = -srca[i];
555 glUploadTex(gl_target, GL_ALPHA, GL_UNSIGNED_BYTE, tmp, stride,
556 0, 0, w, h, 0);
557 free(tmp);
559 #endif
561 BindTexture(gl_target, 0);
563 // Create a list for rendering this OSD part
564 #ifndef FAST_OSD
565 osdaDispList[osdtexCnt] = glGenLists(1);
566 glNewList(osdaDispList[osdtexCnt], GL_COMPILE);
567 // render alpha
568 BindTexture(gl_target, osdatex[osdtexCnt]);
569 glDrawTex(x0, y0, w, h, 0, 0, w, h, sx, sy, use_rectangle == 1, 0, 0);
570 glEndList();
571 #endif
572 osdDispList[osdtexCnt] = glGenLists(1);
573 glNewList(osdDispList[osdtexCnt], GL_COMPILE);
574 // render OSD
575 BindTexture(gl_target, osdtex[osdtexCnt]);
576 glDrawTex(x0, y0, w, h, 0, 0, w, h, sx, sy, use_rectangle == 1, 0, 0);
577 glEndList();
579 osdtexCnt++;
582 static void do_render_osd(void);
584 static void draw_osd(void)
586 if (!use_osd) return;
587 if (vo_osd_changed(0)) {
588 int osd_h, osd_w;
589 clearOSD();
590 osd_w = scaled_osd ? image_width : vo_dwidth;
591 osd_h = scaled_osd ? image_height : vo_dheight;
592 vo_draw_text(osd_w, osd_h, create_osd_texture);
594 if (vo_doublebuffering) do_render_osd();
597 static void do_render(void) {
598 // glEnable(GL_TEXTURE_2D);
599 // glBindTexture(GL_TEXTURE_2D, texture_id);
601 glColor3f(1,1,1);
602 if (image_format == IMGFMT_YV12)
603 glEnableYUVConversion(gl_target, yuvconvtype);
604 glDrawTex(0, 0, image_width, image_height,
605 0, 0, image_width, image_height,
606 texture_width, texture_height,
607 use_rectangle == 1, image_format == IMGFMT_YV12,
608 mpi_flipped ^ vo_flipped);
609 if (image_format == IMGFMT_YV12)
610 glDisableYUVConversion(gl_target, yuvconvtype);
613 static void do_render_osd(void) {
614 if (osdtexCnt > 0 || eosdDispList) {
615 // set special rendering parameters
616 if (!scaled_osd) {
617 glMatrixMode(GL_PROJECTION);
618 glPushMatrix();
619 glLoadIdentity();
620 glOrtho(0, vo_dwidth, vo_dheight, 0, -1, 1);
622 glEnable(GL_BLEND);
623 if (eosdDispList) {
624 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
625 glCallList(eosdDispList);
627 if (osdtexCnt > 0) {
628 glColor4ub((osd_color >> 16) & 0xff, (osd_color >> 8) & 0xff, osd_color & 0xff, 0xff - (osd_color >> 24));
629 // draw OSD
630 #ifndef FAST_OSD
631 glBlendFunc(GL_ZERO, GL_ONE_MINUS_SRC_ALPHA);
632 glCallLists(osdtexCnt, GL_UNSIGNED_INT, osdaDispList);
633 #endif
634 glBlendFunc(GL_SRC_ALPHA, GL_ONE);
635 glCallLists(osdtexCnt, GL_UNSIGNED_INT, osdDispList);
637 // set rendering parameters back to defaults
638 glDisable(GL_BLEND);
639 if (!scaled_osd)
640 glPopMatrix();
641 BindTexture(gl_target, 0);
645 static void flip_page(void) {
646 if (vo_doublebuffering) {
647 if (use_glFinish) glFinish();
648 swapGlBuffers();
649 if (vo_fs && use_aspect)
650 glClear(GL_COLOR_BUFFER_BIT);
651 } else {
652 do_render();
653 do_render_osd();
654 if (use_glFinish) glFinish();
655 else glFlush();
659 static void redraw(void) {
660 if (vo_doublebuffering) { do_render(); do_render_osd(); }
661 flip_page();
664 //static inline uint32_t draw_slice_x11(uint8_t *src[], uint32_t slice_num)
665 static int draw_slice(uint8_t *src[], int stride[], int w,int h,int x,int y)
667 mpi_flipped = stride[0] < 0;
668 glUploadTex(gl_target, gl_format, gl_type, src[0], stride[0],
669 x, y, w, h, slice_height);
670 if (image_format == IMGFMT_YV12) {
671 ActiveTexture(GL_TEXTURE1);
672 glUploadTex(gl_target, gl_format, gl_type, src[1], stride[1],
673 x / 2, y / 2, w / 2, h / 2, slice_height);
674 ActiveTexture(GL_TEXTURE2);
675 glUploadTex(gl_target, gl_format, gl_type, src[2], stride[2],
676 x / 2, y / 2, w / 2, h / 2, slice_height);
677 ActiveTexture(GL_TEXTURE0);
679 return 0;
682 static uint32_t get_image(mp_image_t *mpi) {
683 if (!GenBuffers || !BindBuffer || !BufferData || !MapBuffer) {
684 if (!err_shown)
685 mp_msg(MSGT_VO, MSGL_ERR, "[gl] extensions missing for dr\n"
686 "Expect a _major_ speed penalty\n");
687 err_shown = 1;
688 return VO_FALSE;
690 if (mpi->flags & MP_IMGFLAG_READABLE) return VO_FALSE;
691 if (ati_hack) {
692 mpi->width = texture_width;
693 mpi->height = texture_height;
695 if (!gl_buffer)
696 GenBuffers(1, &gl_buffer);
697 BindBuffer(GL_PIXEL_UNPACK_BUFFER, gl_buffer);
698 mpi->stride[0] = mpi->width * mpi->bpp / 8;
699 if (mpi->stride[0] * mpi->height > gl_buffersize) {
700 BufferData(GL_PIXEL_UNPACK_BUFFER, mpi->stride[0] * mpi->height,
701 NULL, GL_DYNAMIC_DRAW);
702 gl_buffersize = mpi->stride[0] * mpi->height;
704 if (!gl_bufferptr)
705 gl_bufferptr = MapBuffer(GL_PIXEL_UNPACK_BUFFER, GL_WRITE_ONLY);
706 mpi->planes[0] = gl_bufferptr;
707 BindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
708 if (!mpi->planes[0]) {
709 if (!err_shown)
710 mp_msg(MSGT_VO, MSGL_ERR, "[gl] could not acquire buffer for dr\n"
711 "Expect a _major_ speed penalty\n");
712 err_shown = 1;
713 return VO_FALSE;
715 if (mpi->imgfmt == IMGFMT_YV12) {
716 // YV12
717 mpi->flags |= MP_IMGFLAG_COMMON_STRIDE | MP_IMGFLAG_COMMON_PLANE;
718 mpi->stride[0] = mpi->width;
719 mpi->planes[1] = mpi->planes[0] + mpi->stride[0] * mpi->height;
720 mpi->stride[1] = mpi->width >> 1;
721 mpi->planes[2] = mpi->planes[1] + mpi->stride[1] * (mpi->height >> 1);
722 mpi->stride[2] = mpi->width >> 1;
723 if (ati_hack) {
724 mpi->flags &= ~MP_IMGFLAG_COMMON_PLANE;
725 if (!gl_buffer_uv[0]) GenBuffers(2, gl_buffer_uv);
726 if (mpi->stride[1] * mpi->height > gl_buffersize_uv) {
727 BindBuffer(GL_PIXEL_UNPACK_BUFFER, gl_buffer_uv[0]);
728 BufferData(GL_PIXEL_UNPACK_BUFFER, mpi->stride[1] * mpi->height,
729 NULL, GL_DYNAMIC_DRAW);
730 BindBuffer(GL_PIXEL_UNPACK_BUFFER, gl_buffer_uv[1]);
731 BufferData(GL_PIXEL_UNPACK_BUFFER, mpi->stride[1] * mpi->height,
732 NULL, GL_DYNAMIC_DRAW);
733 gl_buffersize_uv = mpi->stride[1] * mpi->height;
735 if (!gl_bufferptr_uv[0]) {
736 BindBuffer(GL_PIXEL_UNPACK_BUFFER, gl_buffer_uv[0]);
737 gl_bufferptr_uv[0] = MapBuffer(GL_PIXEL_UNPACK_BUFFER, GL_WRITE_ONLY);
738 BindBuffer(GL_PIXEL_UNPACK_BUFFER, gl_buffer_uv[1]);
739 gl_bufferptr_uv[1] = MapBuffer(GL_PIXEL_UNPACK_BUFFER, GL_WRITE_ONLY);
741 mpi->planes[1] = gl_bufferptr_uv[0];
742 mpi->planes[2] = gl_bufferptr_uv[1];
745 mpi->flags |= MP_IMGFLAG_DIRECT;
746 return VO_TRUE;
749 static uint32_t draw_image(mp_image_t *mpi) {
750 int slice = slice_height;
751 int stride[3];
752 unsigned char *planes[3];
753 mp_image_t mpi2 = *mpi;
754 int w = mpi->w, h = mpi->h;
755 if (ati_hack) { w = texture_width; h = texture_height; }
756 if (mpi->flags & MP_IMGFLAG_DRAW_CALLBACK)
757 goto skip_upload;
758 mpi2.flags = 0; mpi2.type = MP_IMGTYPE_TEMP;
759 mpi2.width = mpi2.w; mpi2.height = mpi2.h;
760 if (force_pbo && !(mpi->flags & MP_IMGFLAG_DIRECT) && !gl_bufferptr && get_image(&mpi2) == VO_TRUE) {
761 int bpp = mpi->imgfmt == IMGFMT_YV12 ? 8 : mpi->bpp;
762 memcpy_pic(mpi2.planes[0], mpi->planes[0], mpi->w * bpp / 8, mpi->h, mpi2.stride[0], mpi->stride[0]);
763 if (mpi->imgfmt == IMGFMT_YV12) {
764 memcpy_pic(mpi2.planes[1], mpi->planes[1], mpi->w >> 1, mpi->h >> 1, mpi2.stride[1], mpi->stride[1]);
765 memcpy_pic(mpi2.planes[2], mpi->planes[2], mpi->w >> 1, mpi->h >> 1, mpi2.stride[2], mpi->stride[2]);
767 mpi = &mpi2;
769 stride[0] = mpi->stride[0]; stride[1] = mpi->stride[1]; stride[2] = mpi->stride[2];
770 planes[0] = mpi->planes[0]; planes[1] = mpi->planes[1]; planes[2] = mpi->planes[2];
771 mpi_flipped = stride[0] < 0;
772 if (mpi->flags & MP_IMGFLAG_DIRECT) {
773 intptr_t base = (intptr_t)planes[0];
774 if (mpi_flipped)
775 base += (mpi->h - 1) * stride[0];
776 planes[0] -= base;
777 planes[1] -= base;
778 planes[2] -= base;
779 BindBuffer(GL_PIXEL_UNPACK_BUFFER, gl_buffer);
780 UnmapBuffer(GL_PIXEL_UNPACK_BUFFER);
781 gl_bufferptr = NULL;
782 slice = 0; // always "upload" full texture
783 if (!(mpi->flags & MP_IMGFLAG_COMMON_PLANE))
784 planes[0] = planes[1] = planes[2] = NULL;
786 glUploadTex(gl_target, gl_format, gl_type, planes[0], stride[0],
787 mpi->x, mpi->y, w, h, slice);
788 if (mpi->imgfmt == IMGFMT_YV12) {
789 if ((mpi->flags & MP_IMGFLAG_DIRECT) && !(mpi->flags & MP_IMGFLAG_COMMON_PLANE)) {
790 BindBuffer(GL_PIXEL_UNPACK_BUFFER, gl_buffer_uv[0]);
791 UnmapBuffer(GL_PIXEL_UNPACK_BUFFER);
792 gl_bufferptr_uv[0] = NULL;
794 ActiveTexture(GL_TEXTURE1);
795 glUploadTex(gl_target, gl_format, gl_type, planes[1], stride[1],
796 mpi->x / 2, mpi->y / 2, w / 2, h / 2, slice);
797 if ((mpi->flags & MP_IMGFLAG_DIRECT) && !(mpi->flags & MP_IMGFLAG_COMMON_PLANE)) {
798 BindBuffer(GL_PIXEL_UNPACK_BUFFER, gl_buffer_uv[1]);
799 UnmapBuffer(GL_PIXEL_UNPACK_BUFFER);
800 gl_bufferptr_uv[1] = NULL;
802 ActiveTexture(GL_TEXTURE2);
803 glUploadTex(gl_target, gl_format, gl_type, planes[2], stride[2],
804 mpi->x / 2, mpi->y / 2, w / 2, h / 2, slice);
805 ActiveTexture(GL_TEXTURE0);
807 if (mpi->flags & MP_IMGFLAG_DIRECT)
808 BindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
809 skip_upload:
810 if (vo_doublebuffering) do_render();
811 return VO_TRUE;
814 static int
815 draw_frame(uint8_t *src[])
817 return VO_ERROR;
820 static int
821 query_format(uint32_t format)
823 int caps = VFCAP_CSP_SUPPORTED | VFCAP_CSP_SUPPORTED_BY_HW |
824 VFCAP_FLIP |
825 VFCAP_HWSCALE_UP | VFCAP_HWSCALE_DOWN | VFCAP_ACCEPT_STRIDE;
826 if (use_osd)
827 caps |= VFCAP_OSD | VFCAP_EOSD | (scaled_osd ? 0 : VFCAP_EOSD_UNSCALED);
828 if (format == IMGFMT_RGB24 || format == IMGFMT_RGBA)
829 return caps;
830 if (use_yuv && format == IMGFMT_YV12)
831 return caps;
832 // HACK, otherwise we get only b&w with some filters (e.g. -vf eq)
833 // ideally MPlayer should be fixed instead not to use Y800 when it has the choice
834 if (!use_yuv && (format == IMGFMT_Y8 || format == IMGFMT_Y800))
835 return 0;
836 if (many_fmts &&
837 glFindFormat(format, NULL, NULL, NULL, NULL))
838 return caps;
839 return 0;
843 static void
844 uninit(void)
846 if (!vo_config_count) return;
847 uninitGl();
848 releaseGlContext(&gl_vinfo, &gl_context);
849 if (custom_prog) free(custom_prog);
850 custom_prog = NULL;
851 if (custom_tex) free(custom_tex);
852 custom_tex = NULL;
853 vo_uninit();
856 static opt_t subopts[] = {
857 {"manyfmts", OPT_ARG_BOOL, &many_fmts, NULL},
858 {"osd", OPT_ARG_BOOL, &use_osd, NULL},
859 {"scaled-osd", OPT_ARG_BOOL, &scaled_osd, NULL},
860 {"aspect", OPT_ARG_BOOL, &use_aspect, NULL},
861 {"slice-height", OPT_ARG_INT, &slice_height, (opt_test_f)int_non_neg},
862 {"rectangle", OPT_ARG_INT, &use_rectangle,(opt_test_f)int_non_neg},
863 {"yuv", OPT_ARG_INT, &use_yuv, (opt_test_f)int_non_neg},
864 {"lscale", OPT_ARG_INT, &lscale, (opt_test_f)int_non_neg},
865 {"cscale", OPT_ARG_INT, &cscale, (opt_test_f)int_non_neg},
866 {"filter-strength", OPT_ARG_FLOAT, &filter_strength, NULL},
867 {"ati-hack", OPT_ARG_BOOL, &ati_hack, NULL},
868 {"force-pbo", OPT_ARG_BOOL, &force_pbo, NULL},
869 {"glfinish", OPT_ARG_BOOL, &use_glFinish, NULL},
870 {"swapinterval", OPT_ARG_INT, &swap_interval,NULL},
871 {"customprog", OPT_ARG_MSTRZ,&custom_prog, NULL},
872 {"customtex", OPT_ARG_MSTRZ,&custom_tex, NULL},
873 {"customtlin", OPT_ARG_BOOL, &custom_tlin, NULL},
874 {"customtrect", OPT_ARG_BOOL, &custom_trect, NULL},
875 {"osdcolor", OPT_ARG_INT, &osd_color, NULL},
876 {NULL}
879 static int preinit(const char *arg)
881 // set defaults
882 many_fmts = 1;
883 use_osd = 1;
884 scaled_osd = 0;
885 use_aspect = 1;
886 use_yuv = 0;
887 lscale = 0;
888 cscale = 0;
889 filter_strength = 0.5;
890 use_rectangle = 0;
891 use_glFinish = 0;
892 ati_hack = 0;
893 force_pbo = 0;
894 swap_interval = 1;
895 slice_height = 0;
896 custom_prog = NULL;
897 custom_tex = NULL;
898 custom_tlin = 1;
899 custom_trect = 0;
900 osd_color = 0xffffff;
901 if (subopt_parse(arg, subopts) != 0) {
902 mp_msg(MSGT_VO, MSGL_FATAL,
903 "\n-vo gl command line help:\n"
904 "Example: mplayer -vo gl:slice-height=4\n"
905 "\nOptions:\n"
906 " nomanyfmts\n"
907 " Disable extended color formats for OpenGL 1.2 and later\n"
908 " slice-height=<0-...>\n"
909 " Slice size for texture transfer, 0 for whole image\n"
910 " noosd\n"
911 " Do not use OpenGL OSD code\n"
912 " noaspect\n"
913 " Do not do aspect scaling\n"
914 " rectangle=<0,1,2>\n"
915 " 0: use power-of-two textures\n"
916 " 1: use texture_rectangle\n"
917 " 2: use texture_non_power_of_two\n"
918 " ati-hack\n"
919 " Workaround ATI bug with PBOs\n"
920 " force-pbo\n"
921 " Force use of PBO even if this involves an extra memcpy\n"
922 " glfinish\n"
923 " Call glFinish() before swapping buffers\n"
924 " swapinterval=<n>\n"
925 " Interval in displayed frames between to buffer swaps.\n"
926 " 1 is equivalent to enable VSYNC, 0 to disable VSYNC.\n"
927 " Requires GLX_SGI_swap_control support to work.\n"
928 " yuv=<n>\n"
929 " 0: use software YUV to RGB conversion.\n"
930 " 1: use register combiners (nVidia only, for older cards).\n"
931 " 2: use fragment program.\n"
932 " 3: use fragment program with gamma correction.\n"
933 " 4: use fragment program with gamma correction via lookup.\n"
934 " 5: use ATI-specific method (for older cards).\n"
935 " 6: use lookup via 3D texture.\n"
936 " lscale=<n>\n"
937 " 0: use standard bilinear scaling for luma.\n"
938 " 1: use improved bicubic scaling for luma.\n"
939 " 2: use cubic in X, linear in Y direction scaling for luma.\n"
940 " 3: as 1 but without using a lookup texture.\n"
941 " 4: experimental unsharp masking.\n"
942 " cscale=<n>\n"
943 " as lscale but for chroma (2x slower with little visible effect).\n"
944 " customprog=<filename>\n"
945 " use a custom YUV conversion program\n"
946 " customtex=<filename>\n"
947 " use a custom YUV conversion lookup texture\n"
948 " nocustomtlin\n"
949 " use GL_NEAREST scaling for customtex texture\n"
950 " customtrect\n"
951 " use texture_rectangle for customtex texture\n"
952 " osdcolor=<0xAARRGGBB>\n"
953 " use the given color for the OSD\n"
954 "\n" );
955 return -1;
957 if (use_rectangle == 1)
958 gl_target = GL_TEXTURE_RECTANGLE;
959 else
960 gl_target = GL_TEXTURE_2D;
961 yuvconvtype = use_yuv | lscale << YUV_LUM_SCALER_SHIFT | cscale << YUV_CHROM_SCALER_SHIFT;
962 if (many_fmts)
963 mp_msg(MSGT_VO, MSGL_INFO, "[gl] using extended formats. "
964 "Use -vo gl:nomanyfmts if playback fails.\n");
965 mp_msg(MSGT_VO, MSGL_V, "[gl] Using %d as slice height "
966 "(0 means image height).\n", slice_height);
967 if (!vo_init()) return -1; // Can't open X11
969 return 0;
972 #define MASK_ALL_YUV (~(1 << YUV_CONVERSION_NONE))
973 #define MASK_NOT_COMBINERS (~((1 << YUV_CONVERSION_NONE) | (1 << YUV_CONVERSION_COMBINERS) | (1 << YUV_CONVERSION_COMBINERS_ATI)))
974 #define MASK_GAMMA_SUPPORT (MASK_NOT_COMBINERS & ~(1 << YUV_CONVERSION_FRAGMENT))
976 static const struct {
977 const char *name;
978 int *value;
979 int supportmask;
980 } eq_map[] = {
981 {"brightness", &eq_bri, MASK_NOT_COMBINERS},
982 {"contrast", &eq_cont, MASK_NOT_COMBINERS},
983 {"saturation", &eq_sat, MASK_ALL_YUV },
984 {"hue", &eq_hue, MASK_ALL_YUV },
985 {"gamma", &eq_rgamma, MASK_GAMMA_SUPPORT},
986 {"red_gamma", &eq_rgamma, MASK_GAMMA_SUPPORT},
987 {"green_gamma", &eq_ggamma, MASK_GAMMA_SUPPORT},
988 {"blue_gamma", &eq_bgamma, MASK_GAMMA_SUPPORT},
989 {NULL, NULL, 0 }
992 static int control(uint32_t request, void *data)
994 switch (request) {
995 case VOCTRL_PAUSE:
996 case VOCTRL_RESUME:
997 int_pause = (request == VOCTRL_PAUSE);
998 return VO_TRUE;
999 case VOCTRL_QUERY_FORMAT:
1000 return query_format(*(uint32_t*)data);
1001 case VOCTRL_GET_IMAGE:
1002 return get_image(data);
1003 case VOCTRL_DRAW_IMAGE:
1004 return draw_image(data);
1005 case VOCTRL_DRAW_EOSD:
1006 if (!data)
1007 return VO_FALSE;
1008 genEOSD(data);
1009 return VO_TRUE;
1010 case VOCTRL_GET_EOSD_RES:
1012 mp_eosd_res_t *r = data;
1013 r->mt = r->mb = r->ml = r->mr = 0;
1014 if (scaled_osd) {r->w = image_width; r->h = image_height;}
1015 else if (vo_fs) {
1016 r->w = vo_screenwidth; r->h = vo_screenheight;
1017 r->ml = r->mr = ass_border_x;
1018 r->mt = r->mb = ass_border_y;
1019 } else {
1020 r->w = vo_dwidth; r->h = vo_dheight;
1023 return VO_TRUE;
1024 case VOCTRL_GUISUPPORT:
1025 return VO_TRUE;
1026 case VOCTRL_ONTOP:
1027 vo_gl_ontop();
1028 return VO_TRUE;
1029 case VOCTRL_FULLSCREEN:
1030 vo_fullscreen();
1031 resize(vo_dwidth, vo_dheight);
1032 return VO_TRUE;
1033 case VOCTRL_BORDER:
1034 vo_gl_border(global_vo);
1035 resize(vo_dwidth, vo_dheight);
1036 return VO_TRUE;
1037 case VOCTRL_GET_PANSCAN:
1038 if (!use_aspect) return VO_NOTIMPL;
1039 return VO_TRUE;
1040 case VOCTRL_SET_PANSCAN:
1041 if (!use_aspect) return VO_NOTIMPL;
1042 resize(vo_dwidth, vo_dheight);
1043 return VO_TRUE;
1044 case VOCTRL_GET_EQUALIZER:
1045 if (image_format == IMGFMT_YV12) {
1046 struct voctrl_get_equalizer_args *args = data;
1047 int i;
1048 for (i = 0; eq_map[i].name; i++)
1049 if (strcmp(args->name, eq_map[i].name) == 0) break;
1050 if (!(eq_map[i].supportmask & (1 << use_yuv)))
1051 break;
1052 *args->valueptr = *eq_map[i].value;
1053 return VO_TRUE;
1055 break;
1056 case VOCTRL_SET_EQUALIZER:
1057 if (image_format == IMGFMT_YV12) {
1058 struct voctrl_set_equalizer_args *args = data;
1059 int i;
1060 for (i = 0; eq_map[i].name; i++)
1061 if (strcmp(args->name, eq_map[i].name) == 0) break;
1062 if (!(eq_map[i].supportmask & (1 << use_yuv)))
1063 break;
1064 *eq_map[i].value = args->value;
1065 update_yuvconv();
1066 return VO_TRUE;
1068 break;
1069 case VOCTRL_UPDATE_SCREENINFO:
1070 update_xinerama_info();
1071 return VO_TRUE;
1073 return VO_NOTIMPL;