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