Add explanatory comments to the #endif part of multiple inclusion guards.
[mplayer/greg.git] / libvo / vo_gl2.c
blobaa198cd102dddcff816a8c12d8da07183e7aa01b
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 HAVE_NEW_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_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_LINEAR,
266 texture_width / 2, texture_height / 2, 128);
267 ActiveTexture(GL_TEXTURE2);
268 glCreateClearTex(GL_TEXTURE_2D, gl_internal_format, 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 vo_mouse_autohide = 1;
525 if (WinID >= 0) {
526 vo_window = WinID ? (Window)WinID : mRootWin;
527 vo_x11_selectinput_witherr(mDisplay, vo_window,
528 StructureNotifyMask | KeyPressMask | PointerMotionMask |
529 ButtonPressMask | ButtonReleaseMask | ExposureMask);
530 return 0;
532 vinfo = choose_glx_visual(mDisplay,mScreen,&vinfo_buf) < 0 ? NULL : &vinfo_buf;
533 if (vinfo == NULL) {
534 mp_msg(MSGT_VO, MSGL_FATAL, "[gl2] no GLX support present\n");
535 return -1;
538 vo_x11_create_vo_window(vinfo, vo_dx, vo_dy, d_width, d_height,
539 flags, vo_x11_create_colormap(vinfo), "gl2", title);
541 return 0;
543 #endif
545 #ifdef HAVE_NEW_GUI
546 static int config_glx_gui(uint32_t d_width, uint32_t d_height) {
547 guiGetEvent( guiSetShVideo,0 ); // the GUI will set up / resize the window
548 return 0;
550 #endif
552 static int initGl(uint32_t d_width, uint32_t d_height)
554 fragprog = lookupTex = 0;
555 if (initTextures() < 0)
556 return -1;
558 glDisable(GL_BLEND);
559 glDisable(GL_DEPTH_TEST);
560 glDepthMask(GL_FALSE);
561 glDisable(GL_CULL_FACE);
562 glEnable (GL_TEXTURE_2D);
563 if (image_format == IMGFMT_YV12) {
564 switch (use_yuv) {
565 case YUV_CONVERSION_FRAGMENT_LOOKUP:
566 glGenTextures(1, &lookupTex);
567 ActiveTexture(GL_TEXTURE3);
568 glBindTexture(GL_TEXTURE_2D, lookupTex);
569 ActiveTexture(GL_TEXTURE0);
570 glBindTexture(GL_TEXTURE_2D, 0);
571 case YUV_CONVERSION_FRAGMENT_POW:
572 case YUV_CONVERSION_FRAGMENT:
573 if (!GenPrograms || !BindProgram) {
574 mp_msg(MSGT_VO, MSGL_ERR, "[gl] fragment program functions missing!\n");
575 break;
577 GenPrograms(1, &fragprog);
578 BindProgram(GL_FRAGMENT_PROGRAM, fragprog);
579 break;
581 glSetupYUVConversion(GL_TEXTURE_2D, use_yuv, 0.0, 1.0, 0.0, 1.0, 1.0, 1.0, 1.0,
582 texture_width, texture_height);
585 gl_set_antialias(0);
586 gl_set_bilinear(1);
588 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",
589 image_bpp, image_bytes,
590 glValName(gl_bitmap_format), glValName(gl_bitmap_type),
591 rgb_sz, r_sz, g_sz, b_sz, a_sz, glValName(gl_internal_format));
593 resize(&d_width, &d_height);
595 glClearColor( 0.0f,0.0f,0.0f,0.0f );
596 glClear( GL_COLOR_BUFFER_BIT );
598 drawTextureDisplay ();
600 return 0;
603 /* connect to server, create and map window,
604 * allocate colors and (shared) memory
606 static int
607 config(uint32_t width, uint32_t height, uint32_t d_width, uint32_t d_height, uint32_t flags, char *title, uint32_t format)
609 const unsigned char * glVersion;
611 image_height = height;
612 image_width = width;
613 image_format = format;
615 int_pause = 0;
617 #ifdef HAVE_NEW_GUI
618 if (use_gui) {
619 if (config_glx_gui(d_width, d_height) == -1)
620 return -1;
622 #ifndef GL_WIN32
623 else
624 #endif
625 #endif
626 #ifdef GL_WIN32
627 if (config_w32(width, height, d_width, d_height, flags, title, format) == -1)
628 #else
629 if (config_glx(width, height, d_width, d_height, flags, title, format) == -1)
630 #endif
631 return -1;
633 setGlWindow(&gl_vinfo, &gl_context, vo_window);
635 glVersion = glGetString(GL_VERSION);
637 mp_msg(MSGT_VO, MSGL_V, "[gl2] OpenGL Driver Information:\n");
638 mp_msg(MSGT_VO, MSGL_V, "\tvendor: %s,\n\trenderer %s,\n\tversion %s\n",
639 glGetString(GL_VENDOR), glGetString(GL_RENDERER), glVersion);
641 if(glVersion[0]>'1' || (glVersion[0]=='1' && glVersion[2]>='2') )
642 isGL12 = GL_TRUE;
643 else
644 isGL12 = GL_FALSE;
646 if(isGL12) {
647 mp_msg(MSGT_VO, MSGL_INFO, "[gl2] You have OpenGL >= 1.2 capable drivers, GOOD (16bpp and BGR is ok!)\n");
648 } else {
649 mp_msg(MSGT_VO, MSGL_INFO, "[gl2] You have OpenGL < 1.2 drivers, BAD (16bpp and BGR may be damaged!)\n");
652 glFindFormat(format, &image_bpp, &gl_internal_format, &gl_bitmap_format, &gl_bitmap_type);
654 image_bytes=(image_bpp+7)/8;
656 draw_alpha_fnc=draw_alpha_null;
658 switch(image_bpp) {
659 case 15:
660 draw_alpha_fnc=draw_alpha_15; break;
661 case 16:
662 draw_alpha_fnc=draw_alpha_16; break;
663 case 24:
664 draw_alpha_fnc=draw_alpha_24; break;
665 case 32:
666 draw_alpha_fnc=draw_alpha_32; break;
669 if (initGl(vo_dwidth, vo_dheight) == -1)
670 return -1;
671 #ifndef GL_WIN32
672 if (vo_ontop) vo_x11_setlayer(mDisplay,vo_window, vo_ontop);
673 #endif
675 return 0;
678 static int gl_handlekey(int key)
680 if(key=='a'||key=='A') {
681 gl_set_antialias(!gl_antialias);
682 return 0;
683 } else if(key=='b'||key=='B') {
684 gl_set_bilinear(-1);
685 return 0;
687 return 1;
690 static void check_events(void)
692 int e;
693 #ifndef GL_WIN32
694 XEvent Event;
695 char buf[100];
696 KeySym keySym;
697 int key;
698 static XComposeStatus stat;
700 while ( XPending( mDisplay ) ) {
701 XNextEvent( mDisplay,&Event );
702 if( Event.type == KeyPress ) {
703 XLookupString( &Event.xkey,buf,sizeof(buf),&keySym,&stat );
704 key = (keySym&0xff00) != 0 ? (keySym&0x00ff) + 256 : keySym;
705 if(gl_handlekey(key))
706 XPutBackEvent(mDisplay, &Event);
707 break;
708 } else {
709 XPutBackEvent(mDisplay, &Event);
710 break;
713 #endif
714 e=vo_check_events();
715 if(e&VO_EVENT_RESIZE) resize(&vo_dwidth, &vo_dheight);
716 if(e&VO_EVENT_EXPOSE && int_pause) flip_page();
719 static void draw_osd(void)
721 if (ImageData)
722 vo_draw_text(image_width,image_height,draw_alpha_fnc);
725 static void
726 flip_page(void)
728 drawTextureDisplay();
730 // glFlush();
731 if (use_glFinish)
732 glFinish();
733 swapGlBuffers();
735 if (vo_fs) // Avoid flickering borders in fullscreen mode
736 glClear (GL_COLOR_BUFFER_BIT);
739 static int draw_slice(uint8_t *src[], int stride[], int w,int h,int x,int y)
741 uint8_t *yptr = src[0], *uptr = src[1], *vptr = src[2];
742 int ystride = stride[0], ustride = stride[1], vstride = stride[2];
743 int rem_h = h;
744 struct TexSquare *texline = &texgrid[y / texture_height * texnumx];
745 int subtex_y = y % texture_width;
746 while (rem_h > 0) {
747 int rem_w = w;
748 struct TexSquare *tsq = &texline[x / texture_width];
749 int subtex_x = x % texture_height;
750 int subtex_h = rem_h;
751 if (subtex_y + subtex_h > texture_height)
752 subtex_h = texture_height - subtex_y;
753 while (rem_w > 0) {
754 int subtex_w = rem_w;
755 if (subtex_x + subtex_w > texture_width)
756 subtex_w = texture_width - subtex_x;
757 ActiveTexture(GL_TEXTURE0);
758 glBindTexture(GL_TEXTURE_2D, tsq->texobj);
759 glUploadTex(GL_TEXTURE_2D, gl_bitmap_format, gl_bitmap_type,
760 yptr, ystride, subtex_x, subtex_y,
761 subtex_w, subtex_h, 0);
762 ActiveTexture(GL_TEXTURE1);
763 glBindTexture(GL_TEXTURE_2D, tsq->uvtexobjs[0]);
764 glUploadTex(GL_TEXTURE_2D, gl_bitmap_format, gl_bitmap_type,
765 uptr, ustride, subtex_x / 2, subtex_y / 2,
766 subtex_w / 2, subtex_h / 2, 0);
767 ActiveTexture(GL_TEXTURE2);
768 glBindTexture(GL_TEXTURE_2D, tsq->uvtexobjs[1]);
769 glUploadTex(GL_TEXTURE_2D, gl_bitmap_format, gl_bitmap_type,
770 vptr, vstride, subtex_x / 2, subtex_y / 2,
771 subtex_w / 2, subtex_h / 2, 0);
772 subtex_x = 0;
773 yptr += subtex_w;
774 uptr += subtex_w / 2;
775 vptr += subtex_w / 2;
776 tsq++;
777 rem_w -= subtex_w;
779 subtex_y = 0;
780 yptr += subtex_h * ystride - w;
781 uptr += subtex_h / 2 * ustride - w / 2;
782 vptr += subtex_h / 2 * vstride - w / 2;
783 texline += texnumx;
784 rem_h -= subtex_h;
786 ActiveTexture(GL_TEXTURE0);
787 return 0;
790 static int
791 draw_frame(uint8_t *src[])
793 if (image_format == IMGFMT_YV12) {
794 mp_msg(MSGT_VO, MSGL_ERR, "[gl2] error: draw_frame called for YV12!\n");
795 return 0;
797 ImageData=(unsigned char *)src[0];
798 resetTexturePointers(ImageData);
799 texdirty = 1;
800 return 0;
803 static int
804 query_format(uint32_t format)
806 switch(format) {
807 case IMGFMT_YV12:
808 if (use_yuv)
809 return VFCAP_CSP_SUPPORTED | VFCAP_CSP_SUPPORTED_BY_HW | VFCAP_OSD |
810 VFCAP_HWSCALE_UP | VFCAP_HWSCALE_DOWN | VFCAP_ACCEPT_STRIDE;
811 break;
812 #ifdef __APPLE__
813 case IMGFMT_RGB32:
814 #else
815 case IMGFMT_RGB24:
816 case IMGFMT_BGR24:
817 // case IMGFMT_RGB32:
818 // case IMGFMT_BGR32:
819 #endif
820 return VFCAP_CSP_SUPPORTED | VFCAP_CSP_SUPPORTED_BY_HW | VFCAP_OSD;
822 return 0;
826 static void
827 uninit(void)
829 if ( !vo_config_count ) return;
830 releaseGlContext(&gl_vinfo, &gl_context);
831 if (texgrid) {
832 free(texgrid);
833 texgrid = NULL;
835 vo_uninit();
838 static opt_t subopts[] = {
839 {"yuv", OPT_ARG_INT, &use_yuv, (opt_test_f)int_non_neg},
840 {"glfinish", OPT_ARG_BOOL, &use_glFinish, NULL},
841 {NULL}
844 static int preinit(const char *arg)
846 // set defaults
847 use_yuv = 0;
848 use_glFinish = 1;
849 if (subopt_parse(arg, subopts) != 0) {
850 mp_msg(MSGT_VO, MSGL_FATAL,
851 "\n-vo gl2 command line help:\n"
852 "Example: mplayer -vo gl2:noglfinish\n"
853 "\nOptions:\n"
854 " noglfinish\n"
855 " Do not call glFinish() before swapping buffers\n"
856 " yuv=<n>\n"
857 " 0: use software YUV to RGB conversion.\n"
858 " 1: use register combiners (nVidia only, for older cards).\n"
859 " 2: use fragment program.\n"
860 " 3: use fragment program with gamma correction.\n"
861 " 4: use fragment program with gamma correction via lookup.\n"
862 " 5: use ATI-specific method (for older cards).\n"
863 "\n" );
864 return -1;
866 if( !vo_init() ) return -1; // Can't open X11
867 return 0;
870 static int control(uint32_t request, void *data, ...)
872 switch (request) {
873 case VOCTRL_PAUSE: return (int_pause=1);
874 case VOCTRL_RESUME: return (int_pause=0);
875 case VOCTRL_QUERY_FORMAT:
876 return query_format(*((uint32_t*)data));
877 case VOCTRL_GUISUPPORT:
878 return VO_TRUE;
879 case VOCTRL_ONTOP:
880 vo_ontop();
881 return VO_TRUE;
882 case VOCTRL_FULLSCREEN:
883 vo_fullscreen();
884 if (setGlWindow(&gl_vinfo, &gl_context, vo_window) == SET_WINDOW_REINIT)
885 initGl(vo_dwidth, vo_dheight);
886 resize(&vo_dwidth, &vo_dheight);
887 return VO_TRUE;
888 #ifdef GL_WIN32
889 case VOCTRL_BORDER:
890 vo_w32_border();
891 return VO_TRUE;
892 #endif
893 case VOCTRL_GET_PANSCAN:
894 return VO_TRUE;
895 case VOCTRL_SET_PANSCAN:
896 resize (&vo_dwidth, &vo_dheight);
897 return VO_TRUE;
898 #ifndef GL_WIN32
899 case VOCTRL_SET_EQUALIZER:
901 va_list ap;
902 int value;
904 va_start(ap, data);
905 value = va_arg(ap, int);
906 va_end(ap);
907 return vo_x11_set_equalizer(data, value);
909 case VOCTRL_GET_EQUALIZER:
911 va_list ap;
912 int *value;
914 va_start(ap, data);
915 value = va_arg(ap, int *);
916 va_end(ap);
917 return vo_x11_get_equalizer(data, value);
919 #endif
920 case VOCTRL_UPDATE_SCREENINFO:
921 update_xinerama_info();
922 return VO_TRUE;
924 return VO_NOTIMPL;