Merge svn changes up to r28461
[mplayer.git] / libvo / vo_gl2.c
blob602de115d894d7a5b15728fca668a4f8ead09b22
1 /*
2 * video_out_gl.c, X11/OpenGL interface
3 * based on video_out_x11 by Aaron Holtzman,
4 * and WS opengl window manager by Pontscho/Fresh!
5 */
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <string.h>
11 #include "config.h"
12 #include "mp_msg.h"
13 #include "subopt-helper.h"
14 #include "video_out.h"
15 #include "video_out_internal.h"
16 #include "sub.h"
18 #include "gl_common.h"
19 #include "aspect.h"
20 #ifdef CONFIG_GUI
21 #include "gui/interface.h"
22 #endif
24 #undef TEXTUREFORMAT_ALWAYS
25 #ifdef __APPLE__
26 #define TEXTUREFORMAT_ALWAYS GL_RGBA8
27 #endif
29 //! force texture height, useful for debugging
30 #define TEXTURE_HEIGHT 128
31 #undef TEXTURE_HEIGHT
32 //! force texture width, useful for debugging
33 #define TEXTURE_WIDTH 128
34 #undef TEXTURE_WIDTH
36 static const vo_info_t info =
38 "X11 (OpenGL) - multiple textures version",
39 "gl2",
40 "Arpad Gereoffy & Sven Goethel",
44 const LIBVO_EXTERN(gl2)
46 /* local data */
47 static unsigned char *ImageData=NULL;
49 #ifdef GL_WIN32
50 static int gl_vinfo = 0;
51 static HGLRC gl_context = 0;
52 #define update_xinerama_info w32_update_xinerama_info
53 #define vo_init vo_w32_init
54 #define vo_window vo_w32_window
55 #else
56 static XVisualInfo *gl_vinfo = NULL;
57 static GLXContext gl_context = 0;
58 #endif
60 static uint32_t image_width;
61 static uint32_t image_height;
62 static uint32_t image_format;
63 static uint32_t image_bpp;
64 static uint32_t image_bytes;
66 static int int_pause;
68 static uint32_t texture_width;
69 static uint32_t texture_height;
70 static int texnumx, texnumy, raw_line_len;
71 static int texdirty;
72 static struct TexSquare * texgrid = NULL;
73 static GLuint fragprog;
74 static GLuint lookupTex;
75 static GLint gl_internal_format;
76 static int rgb_sz, r_sz, g_sz, b_sz, a_sz;
77 static GLenum gl_bitmap_format;
78 static GLenum gl_bitmap_type;
79 static int isGL12 = GL_FALSE;
81 static int gl_bilinear=1;
82 static int gl_antialias=0;
83 static int use_yuv;
84 static int use_glFinish;
86 static void (*draw_alpha_fnc)
87 (int x0,int y0, int w,int h, unsigned char* src, unsigned char *srca, int stride);
90 /* The squares that are tiled to make up the game screen polygon */
92 struct TexSquare
94 GLubyte *texture;
95 GLuint texobj;
96 GLuint uvtexobjs[2];
97 GLfloat fx, fy, fw, fh;
100 static GLint getInternalFormat(void)
102 #ifdef GL_WIN32
103 PIXELFORMATDESCRIPTOR pfd;
104 HDC vo_hdc = GetDC(vo_window);
105 int pf = GetPixelFormat(vo_hdc);
106 if (!DescribePixelFormat(vo_hdc, pf, sizeof pfd, &pfd)) {
107 r_sz = g_sz = b_sz = a_sz = 0;
108 } else {
109 r_sz = pfd.cRedBits;
110 g_sz = pfd.cGreenBits;
111 b_sz = pfd.cBlueBits;
112 a_sz = pfd.cAlphaBits;
114 ReleaseDC(vo_window, vo_hdc);
115 #else
116 if (glXGetConfig(mDisplay, gl_vinfo, GLX_RED_SIZE, &r_sz) != 0) r_sz = 0;
117 if (glXGetConfig(mDisplay, gl_vinfo, GLX_GREEN_SIZE, &g_sz) != 0) g_sz = 0;
118 if (glXGetConfig(mDisplay, gl_vinfo, GLX_BLUE_SIZE, &b_sz) != 0) b_sz = 0;
119 if (glXGetConfig(mDisplay, gl_vinfo, GLX_ALPHA_SIZE, &a_sz) != 0) a_sz = 0;
120 #endif
122 rgb_sz=r_sz+g_sz+b_sz;
123 if(rgb_sz<=0) rgb_sz=24;
125 #ifdef TEXTUREFORMAT_ALWAYS
126 return TEXTUREFORMAT_ALWAYS;
127 #else
128 if(r_sz==3 && g_sz==3 && b_sz==2 && a_sz==0)
129 return GL_R3_G3_B2;
130 if(r_sz==4 && g_sz==4 && b_sz==4 && a_sz==0)
131 return GL_RGB4;
132 if(r_sz==5 && g_sz==5 && b_sz==5 && a_sz==0)
133 return GL_RGB5;
134 if(r_sz==8 && g_sz==8 && b_sz==8 && a_sz==0)
135 return GL_RGB8;
136 if(r_sz==10 && g_sz==10 && b_sz==10 && a_sz==0)
137 return GL_RGB10;
138 if(r_sz==2 && g_sz==2 && b_sz==2 && a_sz==2)
139 return GL_RGBA2;
140 if(r_sz==4 && g_sz==4 && b_sz==4 && a_sz==4)
141 return GL_RGBA4;
142 if(r_sz==5 && g_sz==5 && b_sz==5 && a_sz==1)
143 return GL_RGB5_A1;
144 if(r_sz==8 && g_sz==8 && b_sz==8 && a_sz==8)
145 return GL_RGBA8;
146 if(r_sz==10 && g_sz==10 && b_sz==10 && a_sz==2)
147 return GL_RGB10_A2;
148 #endif
149 return GL_RGB;
152 static int initTextures(void)
154 struct TexSquare *tsq=0;
155 GLfloat texpercx, texpercy;
156 int s;
157 int x=0, y=0;
158 GLint format=0;
160 // textures smaller than 64x64 might not be supported
161 s=64;
162 while (s<image_width)
163 s*=2;
164 texture_width=s;
166 s=64;
167 while (s<image_height)
168 s*=2;
169 texture_height=s;
171 if (image_format != IMGFMT_YV12)
172 gl_internal_format = getInternalFormat();
174 /* Test the max texture size */
175 do {
176 glTexImage2D (GL_PROXY_TEXTURE_2D, 0,
177 gl_internal_format,
178 texture_width, texture_height,
179 0, gl_bitmap_format, gl_bitmap_type, NULL);
181 glGetTexLevelParameteriv
182 (GL_PROXY_TEXTURE_2D, 0, GL_TEXTURE_INTERNAL_FORMAT, &format);
184 if (format != gl_internal_format)
186 mp_msg (MSGT_VO, MSGL_V, "[gl2] Needed texture [%dx%d] too big, trying ",
187 texture_height, texture_width);
189 if (texture_width > texture_height)
190 texture_width /= 2;
191 else
192 texture_height /= 2;
194 mp_msg (MSGT_VO, MSGL_V, "[%dx%d] !\n", texture_height, texture_width);
196 if(texture_width < 64 || texture_height < 64) {
197 mp_msg (MSGT_VO, MSGL_FATAL, "[gl2] Give up .. usable texture size not avaiable, or texture config error !\n");
198 return -1;
202 while (format != gl_internal_format && texture_width > 1 && texture_height > 1);
203 #ifdef TEXTURE_WIDTH
204 texture_width = TEXTURE_WIDTH;
205 #endif
206 #ifdef TEXTURE_HEIGHT
207 texture_height = TEXTURE_HEIGHT;
208 #endif
210 texnumx = image_width / texture_width;
211 if ((image_width % texture_width) > 0)
212 texnumx++;
214 texnumy = image_height / texture_height;
215 if ((image_height % texture_height) > 0)
216 texnumy++;
218 mp_msg(MSGT_VO, MSGL_V, "[gl2] Creating %dx%d textures of size %dx%d ...\n",
219 texnumx, texnumy, texture_width,texture_height);
221 /* Allocate the texture memory */
223 texpercx = (GLfloat) texture_width / (GLfloat) image_width;
224 texpercy = (GLfloat) texture_height / (GLfloat) image_height;
226 if (texgrid)
227 free(texgrid);
228 texgrid = calloc (texnumx * texnumy, sizeof (struct TexSquare));
230 raw_line_len = image_width * image_bytes;
232 mp_msg (MSGT_VO, MSGL_DBG2, "[gl2] texture-usage %d*width=%d, %d*height=%d\n",
233 (int) texnumx, (int) texture_width, (int) texnumy,
234 (int) texture_height);
236 tsq = texgrid;
237 for (y = 0; y < texnumy; y++) {
238 for (x = 0; x < texnumx; x++) {
239 tsq->fx = x * texpercx;
240 tsq->fy = y * texpercy;
241 tsq->fw = texpercx;
242 tsq->fh = texpercy;
244 tsq->texobj=0;
245 tsq->uvtexobjs[0] = tsq->uvtexobjs[1] = 0;
247 glGenTextures (1, &(tsq->texobj));
249 glBindTexture (GL_TEXTURE_2D, tsq->texobj);
250 if (image_format == IMGFMT_YV12) {
251 glGenTextures(2, tsq->uvtexobjs);
252 ActiveTexture(GL_TEXTURE1);
253 glBindTexture (GL_TEXTURE_2D, tsq->uvtexobjs[0]);
254 ActiveTexture(GL_TEXTURE2);
255 glBindTexture (GL_TEXTURE_2D, tsq->uvtexobjs[1]);
256 ActiveTexture(GL_TEXTURE0);
259 glCreateClearTex(GL_TEXTURE_2D, gl_internal_format, gl_bitmap_format, gl_bitmap_type, GL_LINEAR,
260 texture_width, texture_height, 0);
262 glTexEnvf (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
263 if (image_format == IMGFMT_YV12) {
264 ActiveTexture(GL_TEXTURE1);
265 glCreateClearTex(GL_TEXTURE_2D, gl_internal_format, gl_bitmap_format, gl_bitmap_type, GL_LINEAR,
266 texture_width / 2, texture_height / 2, 128);
267 ActiveTexture(GL_TEXTURE2);
268 glCreateClearTex(GL_TEXTURE_2D, gl_internal_format, gl_bitmap_format, gl_bitmap_type, GL_LINEAR,
269 texture_width / 2, texture_height / 2, 128);
270 ActiveTexture(GL_TEXTURE0);
273 tsq++;
274 } /* for all texnumx */
275 } /* for all texnumy */
277 return 0;
280 static void resetTexturePointers(unsigned char *imageSource)
282 unsigned char *texdata_start, *line_start;
283 struct TexSquare *tsq = texgrid;
284 int x=0, y=0;
286 line_start = (unsigned char *) imageSource;
288 for (y = 0; y < texnumy; y++) {
289 texdata_start = line_start;
290 for (x = 0; x < texnumx; x++) {
291 tsq->texture = texdata_start;
292 texdata_start += texture_width * image_bytes;
293 tsq++;
294 } /* for all texnumx */
295 line_start += texture_height * raw_line_len;
296 } /* for all texnumy */
299 static void gl_set_bilinear (int val)
301 int x, y;
303 if(val>=0)
304 gl_bilinear = val;
305 else
306 gl_bilinear++;
308 gl_bilinear=gl_bilinear%2;
309 /* no mipmap yet .. */
311 for (y = 0; y < texnumy; y++) {
312 for (x = 0; x < texnumx; x++) {
313 glBindTexture (GL_TEXTURE_2D, texgrid[y * texnumx + x].texobj);
315 switch (gl_bilinear) {
316 case 0:
317 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
318 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
319 mp_msg(MSGT_VO, MSGL_INFO, "[gl2] bilinear off\n");
320 break;
321 case 1:
322 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
323 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
324 mp_msg(MSGT_VO, MSGL_INFO, "[gl2] bilinear linear\n");
325 break;
326 case 2:
327 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
328 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR_MIPMAP_NEAREST);
329 mp_msg(MSGT_VO, MSGL_INFO, "[gl2] bilinear mipmap nearest\n");
330 break;
331 case 3:
332 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
333 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR_MIPMAP_LINEAR);
334 mp_msg(MSGT_VO, MSGL_INFO, "[gl2] bilinear mipmap linear\n");
335 break;
341 static void gl_set_antialias (int val)
343 gl_antialias=val;
345 if (gl_antialias) {
346 glShadeModel (GL_SMOOTH);
347 glEnable (GL_POLYGON_SMOOTH);
348 glEnable (GL_LINE_SMOOTH);
349 glEnable (GL_POINT_SMOOTH);
350 mp_msg(MSGT_VO, MSGL_INFO, "[gl2] antialiasing on\n");
351 } else {
352 glShadeModel (GL_FLAT);
353 glDisable (GL_POLYGON_SMOOTH);
354 glDisable (GL_LINE_SMOOTH);
355 glDisable (GL_POINT_SMOOTH);
356 mp_msg(MSGT_VO, MSGL_INFO, "[gl2] antialiasing off\n");
361 static void drawTextureDisplay (void)
363 struct TexSquare *square = texgrid;
364 int x, y;
366 glColor3f(1.0,1.0,1.0);
368 if (image_format == IMGFMT_YV12)
369 glEnableYUVConversion(GL_TEXTURE_2D, use_yuv);
370 for (y = 0; y < texnumy; y++) {
371 int thish = texture_height;
372 if (y == texnumy - 1 && image_height % texture_height)
373 thish = image_height % texture_height;
374 for (x = 0; x < texnumx; x++) {
375 int thisw = texture_width;
376 if (x == texnumx - 1 && image_width % texture_width)
377 thisw = image_width % texture_width;
378 glBindTexture (GL_TEXTURE_2D, square->texobj);
379 if (image_format == IMGFMT_YV12) {
380 ActiveTexture(GL_TEXTURE1);
381 glBindTexture (GL_TEXTURE_2D, square->uvtexobjs[0]);
382 ActiveTexture(GL_TEXTURE2);
383 glBindTexture (GL_TEXTURE_2D, square->uvtexobjs[1]);
384 ActiveTexture(GL_TEXTURE0);
387 if (texdirty) {
388 glUploadTex(GL_TEXTURE_2D, gl_bitmap_format, gl_bitmap_type,
389 square->texture, image_width * image_bytes,
390 0, 0, thisw, thish, 0);
393 glDrawTex(square->fx, square->fy, square->fw, square->fh,
394 0, 0, texture_width, texture_height,
395 texture_width, texture_height,
396 0, image_format == IMGFMT_YV12, 0);
397 square++;
398 } /* for all texnumx */
399 } /* for all texnumy */
400 if (image_format == IMGFMT_YV12)
401 glDisableYUVConversion(GL_TEXTURE_2D, use_yuv);
402 texdirty = 0;
406 static void resize(int *x,int *y){
407 mp_msg(MSGT_VO,MSGL_V,"[gl2] Resize: %dx%d\n",*x,*y);
408 if( vo_fs ) {
409 glClear(GL_COLOR_BUFFER_BIT);
410 aspect(x, y, A_ZOOM);
411 panscan_calc();
412 *x += vo_panscan_x;
413 *y += vo_panscan_y;
414 glViewport( (vo_screenwidth-*x)/2, (vo_screenheight-*y)/2, *x, *y);
415 } else {
416 //aspect(x, y, A_NOZOOM);
417 if (WinID >= 0) {
418 int top = 0, left = 0, w = *x, h = *y;
419 geometry(&top, &left, &w, &h, vo_screenwidth, vo_screenheight);
420 glViewport(top, left, w, h);
421 } else
422 glViewport( 0, 0, *x, *y );
425 glMatrixMode(GL_PROJECTION);
426 glLoadIdentity();
427 glOrtho (0, 1, 1, 0, -1.0, 1.0);
429 glMatrixMode(GL_MODELVIEW);
430 glLoadIdentity();
433 static void draw_alpha_32(int x0,int y0, int w,int h, unsigned char* src, unsigned char *srca, int stride){
434 vo_draw_alpha_rgb32(w,h,src,srca,stride,ImageData+4*(y0*image_width+x0),4*image_width);
437 static void draw_alpha_24(int x0,int y0, int w,int h, unsigned char* src, unsigned char *srca, int stride){
438 vo_draw_alpha_rgb24(w,h,src,srca,stride,ImageData+3*(y0*image_width+x0),3*image_width);
441 static void draw_alpha_16(int x0,int y0, int w,int h, unsigned char* src, unsigned char *srca, int stride){
442 vo_draw_alpha_rgb16(w,h,src,srca,stride,ImageData+2*(y0*image_width+x0),2*image_width);
445 static void draw_alpha_15(int x0,int y0, int w,int h, unsigned char* src, unsigned char *srca, int stride){
446 vo_draw_alpha_rgb15(w,h,src,srca,stride,ImageData+2*(y0*image_width+x0),2*image_width);
449 static void draw_alpha_null(int x0,int y0, int w,int h, unsigned char* src, unsigned char *srca, int stride){
452 #ifdef GL_WIN32
454 static int config_w32(uint32_t width, uint32_t height, uint32_t d_width, uint32_t d_height, uint32_t flags, char *title, uint32_t format) {
455 if (!vo_w32_config(d_width, d_height, flags))
456 return -1;
458 if (vo_fs)
459 aspect(&d_width, &d_height, A_ZOOM);
461 return 0;
464 #else
466 static int choose_glx_visual(Display *dpy, int scr, XVisualInfo *res_vi)
468 XVisualInfo template, *vi_list;
469 int vi_num, i, best_i, best_weight;
471 template.screen = scr;
472 vi_list = XGetVisualInfo(dpy, VisualScreenMask, &template, &vi_num);
473 if (!vi_list) return -1;
474 best_weight = 1000000; best_i=0;
475 for (i = 0; i < vi_num; i++) {
476 int val, res, w = 0;
477 /* of course, the visual must support OpenGL rendering... */
478 res = glXGetConfig(dpy, vi_list + i, GLX_USE_GL, &val);
479 if (res || val == False) continue;
480 /* also it must be doublebuffered ... */
481 res = glXGetConfig(dpy, vi_list + i, GLX_DOUBLEBUFFER, &val);
482 if (res || val == False) continue;
483 /* furthermore it must be RGBA (not color indexed) ... */
484 res = glXGetConfig(dpy, vi_list + i, GLX_RGBA, &val);
485 if (res || val == False) continue;
486 /* prefer less depth buffer size, */
487 res = glXGetConfig(dpy, vi_list + i, GLX_DEPTH_SIZE, &val);
488 if (res) continue;
489 w += val*2;
490 /* stencil buffer size */
491 res = glXGetConfig(dpy, vi_list + i, GLX_STENCIL_SIZE, &val);
492 if (res) continue;
493 w += val*2;
494 /* and colorbuffer alpha size */
495 res = glXGetConfig(dpy, vi_list + i, GLX_ALPHA_SIZE, &val);
496 if (res) continue;
497 w += val;
498 /* and finally, prefer DirectColor-ed visuals to allow color corrections */
499 if (vi_list[i].class != DirectColor) w += 100;
501 // avoid bad-looking visual with less that 8bit per color
502 res = glXGetConfig(dpy, vi_list + i, GLX_RED_SIZE, &val);
503 if (res) continue;
504 if (val < 8) w += 50;
505 res = glXGetConfig(dpy, vi_list + i, GLX_GREEN_SIZE, &val);
506 if (res) continue;
507 if (val < 8) w += 70;
508 res = glXGetConfig(dpy, vi_list + i, GLX_BLUE_SIZE, &val);
509 if (res) continue;
510 if (val < 8) w += 50;
512 if (w < best_weight) {
513 best_weight = w;
514 best_i = i;
517 if (best_weight < 1000000) *res_vi = vi_list[best_i];
518 XFree(vi_list);
519 return (best_weight < 1000000) ? 0 : -1;
522 static int config_glx(uint32_t width, uint32_t height, uint32_t d_width, uint32_t d_height, uint32_t flags, char *title, uint32_t format) {
523 XVisualInfo *vinfo, vinfo_buf;
524 vinfo = choose_glx_visual(mDisplay,mScreen,&vinfo_buf) < 0 ? NULL : &vinfo_buf;
525 if (vinfo == NULL) {
526 mp_msg(MSGT_VO, MSGL_FATAL, "[gl2] no GLX support present\n");
527 return -1;
530 vo_x11_create_vo_window(vinfo, vo_dx, vo_dy, d_width, d_height,
531 flags, vo_x11_create_colormap(vinfo), "gl2", title);
533 return 0;
535 #endif
537 #ifdef CONFIG_GUI
538 static int config_glx_gui(uint32_t d_width, uint32_t d_height) {
539 guiGetEvent( guiSetShVideo,0 ); // the GUI will set up / resize the window
540 return 0;
542 #endif
544 static int initGl(uint32_t d_width, uint32_t d_height)
546 fragprog = lookupTex = 0;
547 if (initTextures() < 0)
548 return -1;
550 glDisable(GL_BLEND);
551 glDisable(GL_DEPTH_TEST);
552 glDepthMask(GL_FALSE);
553 glDisable(GL_CULL_FACE);
554 glEnable (GL_TEXTURE_2D);
555 if (image_format == IMGFMT_YV12) {
556 gl_conversion_params_t params = {GL_TEXTURE_2D, use_yuv,
557 0.0, 1.0, 0.0, 1.0, 1.0, 1.0, 1.0,
558 texture_width, texture_height};
559 switch (use_yuv) {
560 case YUV_CONVERSION_FRAGMENT_LOOKUP:
561 glGenTextures(1, &lookupTex);
562 ActiveTexture(GL_TEXTURE3);
563 glBindTexture(GL_TEXTURE_2D, lookupTex);
564 ActiveTexture(GL_TEXTURE0);
565 glBindTexture(GL_TEXTURE_2D, 0);
566 case YUV_CONVERSION_FRAGMENT_POW:
567 case YUV_CONVERSION_FRAGMENT:
568 if (!GenPrograms || !BindProgram) {
569 mp_msg(MSGT_VO, MSGL_ERR, "[gl] fragment program functions missing!\n");
570 break;
572 GenPrograms(1, &fragprog);
573 BindProgram(GL_FRAGMENT_PROGRAM, fragprog);
574 break;
576 glSetupYUVConversion(&params);
579 gl_set_antialias(0);
580 gl_set_bilinear(1);
582 mp_msg(MSGT_VO, MSGL_V, "[gl2] Using image_bpp=%d, image_bytes=%d, \n\tgl_bitmap_format=%s, gl_bitmap_type=%s, \n\trgb_size=%d (%d,%d,%d), a_sz=%d, \n\tgl_internal_format=%s\n",
583 image_bpp, image_bytes,
584 glValName(gl_bitmap_format), glValName(gl_bitmap_type),
585 rgb_sz, r_sz, g_sz, b_sz, a_sz, glValName(gl_internal_format));
587 resize(&d_width, &d_height);
589 glClearColor( 0.0f,0.0f,0.0f,0.0f );
590 glClear( GL_COLOR_BUFFER_BIT );
592 drawTextureDisplay ();
594 return 0;
597 /* connect to server, create and map window,
598 * allocate colors and (shared) memory
600 static int
601 config(uint32_t width, uint32_t height, uint32_t d_width, uint32_t d_height, uint32_t flags, char *title, uint32_t format)
603 const unsigned char * glVersion;
605 image_height = height;
606 image_width = width;
607 image_format = format;
609 int_pause = 0;
611 #ifdef CONFIG_GUI
612 if (use_gui) {
613 if (config_glx_gui(d_width, d_height) == -1)
614 return -1;
616 #ifndef GL_WIN32
617 else
618 #endif
619 #endif
620 #ifdef GL_WIN32
621 if (config_w32(width, height, d_width, d_height, flags, title, format) == -1)
622 #else
623 if (config_glx(width, height, d_width, d_height, flags, title, format) == -1)
624 #endif
625 return -1;
627 setGlWindow(&gl_vinfo, &gl_context, vo_window);
629 glVersion = glGetString(GL_VERSION);
631 mp_msg(MSGT_VO, MSGL_V, "[gl2] OpenGL Driver Information:\n");
632 mp_msg(MSGT_VO, MSGL_V, "\tvendor: %s,\n\trenderer %s,\n\tversion %s\n",
633 glGetString(GL_VENDOR), glGetString(GL_RENDERER), glVersion);
635 if(glVersion[0]>'1' || (glVersion[0]=='1' && glVersion[2]>='2') )
636 isGL12 = GL_TRUE;
637 else
638 isGL12 = GL_FALSE;
640 if(isGL12) {
641 mp_msg(MSGT_VO, MSGL_INFO, "[gl2] You have OpenGL >= 1.2 capable drivers, GOOD (16bpp and BGR is ok!)\n");
642 } else {
643 mp_msg(MSGT_VO, MSGL_INFO, "[gl2] You have OpenGL < 1.2 drivers, BAD (16bpp and BGR may be damaged!)\n");
646 glFindFormat(format, &image_bpp, &gl_internal_format, &gl_bitmap_format, &gl_bitmap_type);
648 image_bytes=(image_bpp+7)/8;
650 draw_alpha_fnc=draw_alpha_null;
652 switch(image_bpp) {
653 case 15:
654 draw_alpha_fnc=draw_alpha_15; break;
655 case 16:
656 draw_alpha_fnc=draw_alpha_16; break;
657 case 24:
658 draw_alpha_fnc=draw_alpha_24; break;
659 case 32:
660 draw_alpha_fnc=draw_alpha_32; break;
663 if (initGl(vo_dwidth, vo_dheight) == -1)
664 return -1;
666 return 0;
669 #ifndef GL_WIN32
670 static int gl_handlekey(int key)
672 if(key=='a'||key=='A') {
673 gl_set_antialias(!gl_antialias);
674 return 0;
675 } else if(key=='b'||key=='B') {
676 gl_set_bilinear(-1);
677 return 0;
679 return 1;
681 #endif
683 static void check_events(void)
685 int e;
686 #ifndef GL_WIN32
687 XEvent Event;
688 char buf[100];
689 KeySym keySym;
690 int key;
691 static XComposeStatus stat;
693 while ( XPending( mDisplay ) ) {
694 XNextEvent( mDisplay,&Event );
695 if( Event.type == KeyPress ) {
696 XLookupString( &Event.xkey,buf,sizeof(buf),&keySym,&stat );
697 key = (keySym&0xff00) != 0 ? (keySym&0x00ff) + 256 : keySym;
698 if(gl_handlekey(key))
699 XPutBackEvent(mDisplay, &Event);
700 break;
701 } else {
702 XPutBackEvent(mDisplay, &Event);
703 break;
706 #endif
707 e=vo_check_events();
708 if(e&VO_EVENT_RESIZE) resize(&vo_dwidth, &vo_dheight);
709 if(e&VO_EVENT_EXPOSE && int_pause) flip_page();
712 static void draw_osd(void)
714 if (ImageData)
715 vo_draw_text(image_width,image_height,draw_alpha_fnc);
718 static void
719 flip_page(void)
721 drawTextureDisplay();
723 // glFlush();
724 if (use_glFinish)
725 glFinish();
726 swapGlBuffers();
728 if (vo_fs) // Avoid flickering borders in fullscreen mode
729 glClear (GL_COLOR_BUFFER_BIT);
732 static int draw_slice(uint8_t *src[], int stride[], int w,int h,int x,int y)
734 uint8_t *yptr = src[0], *uptr = src[1], *vptr = src[2];
735 int ystride = stride[0], ustride = stride[1], vstride = stride[2];
736 int rem_h = h;
737 struct TexSquare *texline = &texgrid[y / texture_height * texnumx];
738 int subtex_y = y % texture_width;
739 while (rem_h > 0) {
740 int rem_w = w;
741 struct TexSquare *tsq = &texline[x / texture_width];
742 int subtex_x = x % texture_height;
743 int subtex_h = rem_h;
744 if (subtex_y + subtex_h > texture_height)
745 subtex_h = texture_height - subtex_y;
746 while (rem_w > 0) {
747 int subtex_w = rem_w;
748 if (subtex_x + subtex_w > texture_width)
749 subtex_w = texture_width - subtex_x;
750 ActiveTexture(GL_TEXTURE0);
751 glBindTexture(GL_TEXTURE_2D, tsq->texobj);
752 glUploadTex(GL_TEXTURE_2D, gl_bitmap_format, gl_bitmap_type,
753 yptr, ystride, subtex_x, subtex_y,
754 subtex_w, subtex_h, 0);
755 ActiveTexture(GL_TEXTURE1);
756 glBindTexture(GL_TEXTURE_2D, tsq->uvtexobjs[0]);
757 glUploadTex(GL_TEXTURE_2D, gl_bitmap_format, gl_bitmap_type,
758 uptr, ustride, subtex_x / 2, subtex_y / 2,
759 subtex_w / 2, subtex_h / 2, 0);
760 ActiveTexture(GL_TEXTURE2);
761 glBindTexture(GL_TEXTURE_2D, tsq->uvtexobjs[1]);
762 glUploadTex(GL_TEXTURE_2D, gl_bitmap_format, gl_bitmap_type,
763 vptr, vstride, subtex_x / 2, subtex_y / 2,
764 subtex_w / 2, subtex_h / 2, 0);
765 subtex_x = 0;
766 yptr += subtex_w;
767 uptr += subtex_w / 2;
768 vptr += subtex_w / 2;
769 tsq++;
770 rem_w -= subtex_w;
772 subtex_y = 0;
773 yptr += subtex_h * ystride - w;
774 uptr += subtex_h / 2 * ustride - w / 2;
775 vptr += subtex_h / 2 * vstride - w / 2;
776 texline += texnumx;
777 rem_h -= subtex_h;
779 ActiveTexture(GL_TEXTURE0);
780 return 0;
783 static int
784 draw_frame(uint8_t *src[])
786 if (image_format == IMGFMT_YV12) {
787 mp_msg(MSGT_VO, MSGL_ERR, "[gl2] error: draw_frame called for YV12!\n");
788 return 0;
790 ImageData=(unsigned char *)src[0];
791 resetTexturePointers(ImageData);
792 texdirty = 1;
793 return 0;
796 static int
797 query_format(uint32_t format)
799 switch(format) {
800 case IMGFMT_YV12:
801 if (use_yuv)
802 return VFCAP_CSP_SUPPORTED | VFCAP_CSP_SUPPORTED_BY_HW | VFCAP_OSD |
803 VFCAP_HWSCALE_UP | VFCAP_HWSCALE_DOWN | VFCAP_ACCEPT_STRIDE;
804 break;
805 #ifdef __APPLE__
806 case IMGFMT_RGB32:
807 #else
808 case IMGFMT_RGB24:
809 case IMGFMT_BGR24:
810 // case IMGFMT_RGB32:
811 // case IMGFMT_BGR32:
812 #endif
813 return VFCAP_CSP_SUPPORTED | VFCAP_CSP_SUPPORTED_BY_HW | VFCAP_OSD;
815 return 0;
819 static void
820 uninit(void)
822 if ( !vo_config_count ) return;
823 releaseGlContext(&gl_vinfo, &gl_context);
824 if (texgrid) {
825 free(texgrid);
826 texgrid = NULL;
828 vo_uninit();
831 static opt_t subopts[] = {
832 {"yuv", OPT_ARG_INT, &use_yuv, (opt_test_f)int_non_neg},
833 {"glfinish", OPT_ARG_BOOL, &use_glFinish, NULL},
834 {NULL}
837 static int preinit(const char *arg)
839 // set defaults
840 use_yuv = 0;
841 use_glFinish = 1;
842 if (subopt_parse(arg, subopts) != 0) {
843 mp_msg(MSGT_VO, MSGL_FATAL,
844 "\n-vo gl2 command line help:\n"
845 "Example: mplayer -vo gl2:noglfinish\n"
846 "\nOptions:\n"
847 " noglfinish\n"
848 " Do not call glFinish() before swapping buffers\n"
849 " yuv=<n>\n"
850 " 0: use software YUV to RGB conversion.\n"
851 " 1: use register combiners (nVidia only, for older cards).\n"
852 " 2: use fragment program.\n"
853 " 3: use fragment program with gamma correction.\n"
854 " 4: use fragment program with gamma correction via lookup.\n"
855 " 5: use ATI-specific method (for older cards).\n"
856 "\n" );
857 return -1;
859 if( !vo_init() ) return -1; // Can't open X11
860 return 0;
863 static int control(uint32_t request, void *data)
865 switch (request) {
866 case VOCTRL_PAUSE:
867 case VOCTRL_RESUME:
868 int_pause = (request == VOCTRL_PAUSE);
869 return VO_TRUE;
870 case VOCTRL_QUERY_FORMAT:
871 return query_format(*((uint32_t*)data));
872 case VOCTRL_GUISUPPORT:
873 return VO_TRUE;
874 case VOCTRL_ONTOP:
875 vo_gl_ontop();
876 return VO_TRUE;
877 case VOCTRL_FULLSCREEN:
878 vo_fullscreen();
879 if (setGlWindow(&gl_vinfo, &gl_context, vo_window) == SET_WINDOW_REINIT)
880 initGl(vo_dwidth, vo_dheight);
881 resize(&vo_dwidth, &vo_dheight);
882 return VO_TRUE;
883 case VOCTRL_BORDER:
884 vo_gl_border(global_vo);
885 return VO_TRUE;
886 case VOCTRL_GET_PANSCAN:
887 return VO_TRUE;
888 case VOCTRL_SET_PANSCAN:
889 resize (&vo_dwidth, &vo_dheight);
890 return VO_TRUE;
891 #ifndef GL_WIN32
892 case VOCTRL_SET_EQUALIZER:
894 struct voctrl_set_equalizer_args *args = data;
895 return vo_x11_set_equalizer(args->name, args->value);
897 case VOCTRL_GET_EQUALIZER:
899 struct voctrl_get_equalizer_args *args = data;
900 return vo_x11_get_equalizer(args->name, args->valueptr);
902 #endif
903 case VOCTRL_UPDATE_SCREENINFO:
904 update_xinerama_info();
905 return VO_TRUE;
907 return VO_NOTIMPL;