vo_gl: fix image corruption with PBOs when playing 10 bit video
[mplayer.git] / libvo / vo_gl2.c
blobac1d9571367ead19d7ef1f018df471030aafaddf
1 /*
2 * X11/OpenGL interface
3 * based on video_out_x11 by Aaron Holtzman,
4 * and WS opengl window manager by Pontscho/Fresh!
6 * This file is part of MPlayer.
8 * MPlayer is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * MPlayer is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
27 #include "config.h"
28 #include "mp_msg.h"
29 #include "subopt-helper.h"
30 #include "video_out.h"
31 #include "video_out_internal.h"
32 #include "sub/sub.h"
34 #include "gl_common.h"
35 #include "aspect.h"
37 #undef TEXTUREFORMAT_ALWAYS
38 #ifdef __APPLE__
39 #define TEXTUREFORMAT_ALWAYS GL_RGBA8
40 #endif
42 //! force texture height, useful for debugging
43 #define TEXTURE_HEIGHT 128
44 #undef TEXTURE_HEIGHT
45 //! force texture width, useful for debugging
46 #define TEXTURE_WIDTH 128
47 #undef TEXTURE_WIDTH
49 static const vo_info_t info =
51 "X11 (OpenGL) - multiple textures version",
52 "gl2",
53 "Arpad Gereoffy & Sven Goethel",
57 const LIBVO_EXTERN(gl2)
59 /* local data */
60 static unsigned char *ImageData=NULL;
62 static MPGLContext glctx;
64 static uint32_t image_width;
65 static uint32_t image_height;
66 static uint32_t image_format;
67 static uint32_t image_bpp;
68 static uint32_t image_bytes;
70 static int int_pause;
72 static uint32_t texture_width;
73 static uint32_t texture_height;
74 static int texnumx, texnumy, raw_line_len;
75 static int texdirty;
76 static struct TexSquare * texgrid = NULL;
77 static GLuint fragprog;
78 static GLuint lookupTex;
79 static GLint gl_internal_format;
80 static int rgb_sz, r_sz, g_sz, b_sz, a_sz;
81 static GLenum gl_bitmap_format;
82 static GLenum gl_bitmap_type;
83 static int isGL12 = GL_FALSE;
85 static int gl_bilinear=1;
86 static int gl_antialias=0;
87 static int use_yuv;
88 static int is_yuv;
89 static int use_glFinish;
91 static void (*draw_alpha_fnc)
92 (int x0,int y0, int w,int h, unsigned char* src, unsigned char *srca, int stride);
95 /* The squares that are tiled to make up the game screen polygon */
97 struct TexSquare
99 GLubyte *texture;
100 GLuint texobj;
101 GLuint uvtexobjs[2];
102 GLfloat fx, fy, fw, fh;
105 static GLint getInternalFormat(void)
107 switch (glctx.type) {
108 #ifdef CONFIG_GL_WIN32
109 case GLTYPE_W32:
111 PIXELFORMATDESCRIPTOR pfd;
112 HDC vo_hdc = vo_w32_get_dc(vo_w32_window);
113 int pf = GetPixelFormat(vo_hdc);
114 if (!DescribePixelFormat(vo_hdc, pf, sizeof pfd, &pfd)) {
115 r_sz = g_sz = b_sz = a_sz = 0;
116 } else {
117 r_sz = pfd.cRedBits;
118 g_sz = pfd.cGreenBits;
119 b_sz = pfd.cBlueBits;
120 a_sz = pfd.cAlphaBits;
122 vo_w32_release_dc(vo_w32_window, vo_hdc);
124 break;
125 #endif
126 #ifdef CONFIG_GL_X11
127 case GLTYPE_X11:
128 if (glXGetConfig(mDisplay, glctx.vinfo.x11, GLX_RED_SIZE, &r_sz) != 0) r_sz = 0;
129 if (glXGetConfig(mDisplay, glctx.vinfo.x11, GLX_GREEN_SIZE, &g_sz) != 0) g_sz = 0;
130 if (glXGetConfig(mDisplay, glctx.vinfo.x11, GLX_BLUE_SIZE, &b_sz) != 0) b_sz = 0;
131 if (glXGetConfig(mDisplay, glctx.vinfo.x11, GLX_ALPHA_SIZE, &a_sz) != 0) a_sz = 0;
132 break;
133 #endif
136 rgb_sz=r_sz+g_sz+b_sz;
137 if(rgb_sz<=0) rgb_sz=24;
139 #ifdef TEXTUREFORMAT_ALWAYS
140 return TEXTUREFORMAT_ALWAYS;
141 #else
142 if(r_sz==3 && g_sz==3 && b_sz==2 && a_sz==0)
143 return GL_R3_G3_B2;
144 if(r_sz==4 && g_sz==4 && b_sz==4 && a_sz==0)
145 return GL_RGB4;
146 if(r_sz==5 && g_sz==5 && b_sz==5 && a_sz==0)
147 return GL_RGB5;
148 if(r_sz==8 && g_sz==8 && b_sz==8 && a_sz==0)
149 return GL_RGB8;
150 if(r_sz==10 && g_sz==10 && b_sz==10 && a_sz==0)
151 return GL_RGB10;
152 if(r_sz==2 && g_sz==2 && b_sz==2 && a_sz==2)
153 return GL_RGBA2;
154 if(r_sz==4 && g_sz==4 && b_sz==4 && a_sz==4)
155 return GL_RGBA4;
156 if(r_sz==5 && g_sz==5 && b_sz==5 && a_sz==1)
157 return GL_RGB5_A1;
158 if(r_sz==8 && g_sz==8 && b_sz==8 && a_sz==8)
159 return GL_RGBA8;
160 if(r_sz==10 && g_sz==10 && b_sz==10 && a_sz==2)
161 return GL_RGB10_A2;
162 #endif
163 return GL_RGB;
166 static int initTextures(void)
168 struct TexSquare *tsq=0;
169 GLfloat texpercx, texpercy;
170 int s;
171 int x=0, y=0;
173 // textures smaller than 64x64 might not be supported
174 s=64;
175 while (s<image_width)
176 s*=2;
177 texture_width=s;
179 s=64;
180 while (s<image_height)
181 s*=2;
182 texture_height=s;
184 if (!is_yuv)
185 gl_internal_format = getInternalFormat();
187 /* Test the max texture size */
188 do {
189 GLint w;
190 glTexImage2D (GL_PROXY_TEXTURE_2D, 0,
191 gl_internal_format,
192 texture_width, texture_height,
193 0, gl_bitmap_format, gl_bitmap_type, NULL);
195 glGetTexLevelParameteriv
196 (GL_PROXY_TEXTURE_2D, 0, GL_TEXTURE_WIDTH, &w);
198 if (w >= texture_width)
199 break;
201 mp_msg (MSGT_VO, MSGL_V, "[gl2] Needed texture [%dx%d] too big, trying ",
202 texture_width, texture_height);
204 if (texture_width > texture_height)
205 texture_width /= 2;
206 else
207 texture_height /= 2;
209 mp_msg (MSGT_VO, MSGL_V, "[%dx%d] !\n", texture_width, texture_height);
211 if(texture_width < 64 || texture_height < 64) {
212 mp_msg (MSGT_VO, MSGL_FATAL, "[gl2] Give up .. usable texture size not available, or texture config error !\n");
213 return -1;
215 } while (texture_width > 1 && texture_height > 1);
216 #ifdef TEXTURE_WIDTH
217 texture_width = TEXTURE_WIDTH;
218 #endif
219 #ifdef TEXTURE_HEIGHT
220 texture_height = TEXTURE_HEIGHT;
221 #endif
223 texnumx = image_width / texture_width;
224 if ((image_width % texture_width) > 0)
225 texnumx++;
227 texnumy = image_height / texture_height;
228 if ((image_height % texture_height) > 0)
229 texnumy++;
231 mp_msg(MSGT_VO, MSGL_V, "[gl2] Creating %dx%d textures of size %dx%d ...\n",
232 texnumx, texnumy, texture_width,texture_height);
234 /* Allocate the texture memory */
236 texpercx = (GLfloat) texture_width / (GLfloat) image_width;
237 texpercy = (GLfloat) texture_height / (GLfloat) image_height;
239 free(texgrid);
240 texgrid = calloc (texnumx * texnumy, sizeof (struct TexSquare));
242 raw_line_len = image_width * image_bytes;
244 mp_msg (MSGT_VO, MSGL_DBG2, "[gl2] texture-usage %d*width=%d, %d*height=%d\n",
245 (int) texnumx, (int) texture_width, (int) texnumy,
246 (int) texture_height);
248 tsq = texgrid;
249 for (y = 0; y < texnumy; y++) {
250 for (x = 0; x < texnumx; x++) {
251 tsq->fx = x * texpercx;
252 tsq->fy = y * texpercy;
253 tsq->fw = texpercx;
254 tsq->fh = texpercy;
256 tsq->texobj=0;
257 tsq->uvtexobjs[0] = tsq->uvtexobjs[1] = 0;
259 glGenTextures (1, &(tsq->texobj));
261 glBindTexture (GL_TEXTURE_2D, tsq->texobj);
262 if (is_yuv) {
263 glGenTextures(2, tsq->uvtexobjs);
264 mpglActiveTexture(GL_TEXTURE1);
265 glBindTexture (GL_TEXTURE_2D, tsq->uvtexobjs[0]);
266 mpglActiveTexture(GL_TEXTURE2);
267 glBindTexture (GL_TEXTURE_2D, tsq->uvtexobjs[1]);
268 mpglActiveTexture(GL_TEXTURE0);
271 glCreateClearTex(GL_TEXTURE_2D, gl_internal_format, gl_bitmap_format, gl_bitmap_type, GL_LINEAR,
272 texture_width, texture_height, 0);
274 glTexEnvf (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
275 if (is_yuv) {
276 int xs, ys, depth;
277 int chroma_clear_val = 128;
278 mp_get_chroma_shift(image_format, &xs, &ys, &depth);
279 chroma_clear_val >>= -depth & 7;
280 mpglActiveTexture(GL_TEXTURE1);
281 glCreateClearTex(GL_TEXTURE_2D, gl_internal_format, gl_bitmap_format, gl_bitmap_type, GL_LINEAR,
282 texture_width >> xs, texture_height >> ys,
283 chroma_clear_val);
284 mpglActiveTexture(GL_TEXTURE2);
285 glCreateClearTex(GL_TEXTURE_2D, gl_internal_format, gl_bitmap_format, gl_bitmap_type, GL_LINEAR,
286 texture_width >> xs, texture_height >> ys,
287 chroma_clear_val);
288 mpglActiveTexture(GL_TEXTURE0);
291 tsq++;
292 } /* for all texnumx */
293 } /* for all texnumy */
295 return 0;
298 static void resetTexturePointers(unsigned char *imageSource)
300 unsigned char *texdata_start, *line_start;
301 struct TexSquare *tsq = texgrid;
302 int x=0, y=0;
304 line_start = (unsigned char *) imageSource;
306 for (y = 0; y < texnumy; y++) {
307 texdata_start = line_start;
308 for (x = 0; x < texnumx; x++) {
309 tsq->texture = texdata_start;
310 texdata_start += texture_width * image_bytes;
311 tsq++;
312 } /* for all texnumx */
313 line_start += texture_height * raw_line_len;
314 } /* for all texnumy */
317 static void gl_set_bilinear (int val)
319 int x, y;
321 if(val>=0)
322 gl_bilinear = val;
323 else
324 gl_bilinear++;
326 gl_bilinear=gl_bilinear%2;
327 /* no mipmap yet .. */
329 for (y = 0; y < texnumy; y++) {
330 for (x = 0; x < texnumx; x++) {
331 glBindTexture (GL_TEXTURE_2D, texgrid[y * texnumx + x].texobj);
333 switch (gl_bilinear) {
334 case 0:
335 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
336 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
337 mp_msg(MSGT_VO, MSGL_INFO, "[gl2] bilinear off\n");
338 break;
339 case 1:
340 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
341 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
342 mp_msg(MSGT_VO, MSGL_INFO, "[gl2] bilinear linear\n");
343 break;
344 case 2:
345 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
346 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR_MIPMAP_NEAREST);
347 mp_msg(MSGT_VO, MSGL_INFO, "[gl2] bilinear mipmap nearest\n");
348 break;
349 case 3:
350 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
351 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR_MIPMAP_LINEAR);
352 mp_msg(MSGT_VO, MSGL_INFO, "[gl2] bilinear mipmap linear\n");
353 break;
359 static void gl_set_antialias (int val)
361 gl_antialias=val;
363 if (gl_antialias) {
364 glShadeModel (GL_SMOOTH);
365 glEnable (GL_POLYGON_SMOOTH);
366 glEnable (GL_LINE_SMOOTH);
367 glEnable (GL_POINT_SMOOTH);
368 mp_msg(MSGT_VO, MSGL_INFO, "[gl2] antialiasing on\n");
369 } else {
370 glShadeModel (GL_FLAT);
371 glDisable (GL_POLYGON_SMOOTH);
372 glDisable (GL_LINE_SMOOTH);
373 glDisable (GL_POINT_SMOOTH);
374 mp_msg(MSGT_VO, MSGL_INFO, "[gl2] antialiasing off\n");
379 static void drawTextureDisplay (void)
381 struct TexSquare *square = texgrid;
382 int x, y;
384 glColor3f(1.0,1.0,1.0);
386 if (is_yuv)
387 glEnableYUVConversion(GL_TEXTURE_2D, use_yuv);
388 for (y = 0; y < texnumy; y++) {
389 int thish = texture_height;
390 if (y == texnumy - 1 && image_height % texture_height)
391 thish = image_height % texture_height;
392 for (x = 0; x < texnumx; x++) {
393 int thisw = texture_width;
394 if (x == texnumx - 1 && image_width % texture_width)
395 thisw = image_width % texture_width;
396 glBindTexture (GL_TEXTURE_2D, square->texobj);
397 if (is_yuv) {
398 mpglActiveTexture(GL_TEXTURE1);
399 glBindTexture (GL_TEXTURE_2D, square->uvtexobjs[0]);
400 mpglActiveTexture(GL_TEXTURE2);
401 glBindTexture (GL_TEXTURE_2D, square->uvtexobjs[1]);
402 mpglActiveTexture(GL_TEXTURE0);
405 if (texdirty) {
406 glUploadTex(GL_TEXTURE_2D, gl_bitmap_format, gl_bitmap_type,
407 square->texture, image_width * image_bytes,
408 0, 0, thisw, thish, 0);
411 glDrawTex(square->fx, square->fy, square->fw, square->fh,
412 0, 0, texture_width, texture_height,
413 texture_width, texture_height,
414 0, is_yuv, 0);
415 square++;
416 } /* for all texnumx */
417 } /* for all texnumy */
418 if (is_yuv)
419 glDisableYUVConversion(GL_TEXTURE_2D, use_yuv);
420 texdirty = 0;
424 static void resize(int x,int y){
425 mp_msg(MSGT_VO,MSGL_V,"[gl2] Resize: %dx%d\n",x,y);
426 if(aspect_scaling()) {
427 glClear(GL_COLOR_BUFFER_BIT);
428 aspect(&x, &y, A_WINZOOM);
429 panscan_calc_windowed();
430 x += vo_panscan_x;
431 y += vo_panscan_y;
432 glViewport( (vo_dwidth-x)/2, (vo_dheight-y)/2, x, y);
433 } else {
434 //aspect(x, y, A_NOZOOM);
435 if (WinID >= 0) {
436 int left = 0, top = 0, w = x, h = y;
437 geometry(&left, &top, &w, &h, vo_dwidth, vo_dheight);
438 top = y - h - top;
439 glViewport(left, top, w, h);
440 } else
441 glViewport( 0, 0, x, y );
444 glMatrixMode(GL_PROJECTION);
445 glLoadIdentity();
446 glOrtho (0, 1, 1, 0, -1.0, 1.0);
448 glMatrixMode(GL_MODELVIEW);
449 glLoadIdentity();
452 static void draw_alpha_32(int x0,int y0, int w,int h, unsigned char* src, unsigned char *srca, int stride){
453 vo_draw_alpha_rgb32(w,h,src,srca,stride,ImageData+4*(y0*image_width+x0),4*image_width);
456 static void draw_alpha_24(int x0,int y0, int w,int h, unsigned char* src, unsigned char *srca, int stride){
457 vo_draw_alpha_rgb24(w,h,src,srca,stride,ImageData+3*(y0*image_width+x0),3*image_width);
460 static void draw_alpha_16(int x0,int y0, int w,int h, unsigned char* src, unsigned char *srca, int stride){
461 vo_draw_alpha_rgb16(w,h,src,srca,stride,ImageData+2*(y0*image_width+x0),2*image_width);
464 static void draw_alpha_15(int x0,int y0, int w,int h, unsigned char* src, unsigned char *srca, int stride){
465 vo_draw_alpha_rgb15(w,h,src,srca,stride,ImageData+2*(y0*image_width+x0),2*image_width);
468 static void draw_alpha_null(int x0,int y0, int w,int h, unsigned char* src, unsigned char *srca, int stride){
471 #ifdef CONFIG_GL_WIN32
473 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) {
474 if (!vo_w32_config(d_width, d_height, flags))
475 return -1;
477 return 0;
480 #endif
482 #ifdef CONFIG_GL_X11
483 static int choose_glx_visual(Display *dpy, int scr, XVisualInfo *res_vi)
485 XVisualInfo template, *vi_list;
486 int vi_num, i, best_i, best_weight;
488 template.screen = scr;
489 vi_list = XGetVisualInfo(dpy, VisualScreenMask, &template, &vi_num);
490 if (!vi_list) return -1;
491 best_weight = 1000000; best_i=0;
492 for (i = 0; i < vi_num; i++) {
493 int val, res, w = 0;
494 /* of course, the visual must support OpenGL rendering... */
495 res = glXGetConfig(dpy, vi_list + i, GLX_USE_GL, &val);
496 if (res || val == False) continue;
497 /* also it must be doublebuffered ... */
498 res = glXGetConfig(dpy, vi_list + i, GLX_DOUBLEBUFFER, &val);
499 if (res || val == False) continue;
500 /* furthermore it must be RGBA (not color indexed) ... */
501 res = glXGetConfig(dpy, vi_list + i, GLX_RGBA, &val);
502 if (res || val == False) continue;
503 /* prefer less depth buffer size, */
504 res = glXGetConfig(dpy, vi_list + i, GLX_DEPTH_SIZE, &val);
505 if (res) continue;
506 w += val*2;
507 /* stencil buffer size */
508 res = glXGetConfig(dpy, vi_list + i, GLX_STENCIL_SIZE, &val);
509 if (res) continue;
510 w += val*2;
511 /* and colorbuffer alpha size */
512 res = glXGetConfig(dpy, vi_list + i, GLX_ALPHA_SIZE, &val);
513 if (res) continue;
514 w += val;
515 /* and finally, prefer DirectColor-ed visuals to allow color corrections */
516 if (vi_list[i].class != DirectColor) w += 100;
518 // avoid bad-looking visual with less that 8bit per color
519 res = glXGetConfig(dpy, vi_list + i, GLX_RED_SIZE, &val);
520 if (res) continue;
521 if (val < 8) w += 50;
522 res = glXGetConfig(dpy, vi_list + i, GLX_GREEN_SIZE, &val);
523 if (res) continue;
524 if (val < 8) w += 70;
525 res = glXGetConfig(dpy, vi_list + i, GLX_BLUE_SIZE, &val);
526 if (res) continue;
527 if (val < 8) w += 50;
529 if (w < best_weight) {
530 best_weight = w;
531 best_i = i;
534 if (best_weight < 1000000) *res_vi = vi_list[best_i];
535 XFree(vi_list);
536 return (best_weight < 1000000) ? 0 : -1;
539 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) {
540 XVisualInfo *vinfo, vinfo_buf;
541 vinfo = choose_glx_visual(mDisplay,mScreen,&vinfo_buf) < 0 ? NULL : &vinfo_buf;
542 if (vinfo == NULL) {
543 mp_msg(MSGT_VO, MSGL_FATAL, "[gl2] no GLX support present\n");
544 return -1;
547 vo_x11_create_vo_window(vinfo, vo_dx, vo_dy, d_width, d_height,
548 flags, vo_x11_create_colormap(vinfo), "gl2", title);
550 return 0;
552 #endif
554 static int initGl(uint32_t d_width, uint32_t d_height)
556 fragprog = lookupTex = 0;
557 if (initTextures() < 0)
558 return -1;
560 glDisable(GL_BLEND);
561 glDisable(GL_DEPTH_TEST);
562 glDepthMask(GL_FALSE);
563 glDisable(GL_CULL_FACE);
564 glEnable (GL_TEXTURE_2D);
565 if (is_yuv) {
566 int xs, ys, depth;
567 gl_conversion_params_t params = {GL_TEXTURE_2D, use_yuv,
568 {-1, -1, 0.0, 1.0, 0.0, 1.0, 1.0, 1.0, 1.0},
569 texture_width, texture_height, 0, 0, 0};
570 switch (use_yuv) {
571 case YUV_CONVERSION_FRAGMENT_LOOKUP:
572 glGenTextures(1, &lookupTex);
573 mpglActiveTexture(GL_TEXTURE3);
574 glBindTexture(GL_TEXTURE_2D, lookupTex);
575 mpglActiveTexture(GL_TEXTURE0);
576 glBindTexture(GL_TEXTURE_2D, 0);
577 case YUV_CONVERSION_FRAGMENT_POW:
578 case YUV_CONVERSION_FRAGMENT:
579 if (!mpglGenPrograms || !mpglBindProgram) {
580 mp_msg(MSGT_VO, MSGL_ERR, "[gl] fragment program functions missing!\n");
581 break;
583 mpglGenPrograms(1, &fragprog);
584 mpglBindProgram(GL_FRAGMENT_PROGRAM, fragprog);
585 break;
587 mp_get_chroma_shift(image_format, &xs, &ys, &depth);
588 params.chrom_texw = params.texw >> xs;
589 params.chrom_texh = params.texh >> ys;
590 params.csp_params.input_shift = -depth & 7;
591 glSetupYUVConversion(&params);
594 gl_set_antialias(0);
595 gl_set_bilinear(1);
597 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",
598 image_bpp, image_bytes,
599 glValName(gl_bitmap_format), glValName(gl_bitmap_type),
600 rgb_sz, r_sz, g_sz, b_sz, a_sz, glValName(gl_internal_format));
602 resize(d_width, d_height);
604 glClearColor( 0.0f,0.0f,0.0f,0.0f );
605 glClear( GL_COLOR_BUFFER_BIT );
607 drawTextureDisplay ();
609 return 0;
612 /* connect to server, create and map window,
613 * allocate colors and (shared) memory
615 static int
616 config(uint32_t width, uint32_t height, uint32_t d_width, uint32_t d_height, uint32_t flags, char *title, uint32_t format)
618 int xs, ys;
619 const unsigned char * glVersion;
621 image_height = height;
622 image_width = width;
623 image_format = format;
624 is_yuv = mp_get_chroma_shift(image_format, &xs, &ys, NULL) > 0;
625 is_yuv |= (xs << 8) | (ys << 16);
627 #ifdef CONFIG_GL_WIN32
628 if (config_w32(width, height, d_width, d_height, flags, title, format) == -1)
629 #endif
630 #ifdef CONFIG_GL_X11
631 if (config_glx(width, height, d_width, d_height, flags, title, format) == -1)
632 #endif
633 return -1;
635 if (glctx.setGlWindow(&glctx) == SET_WINDOW_FAILED)
636 return -1;
638 glVersion = glGetString(GL_VERSION);
640 mp_msg(MSGT_VO, MSGL_V, "[gl2] OpenGL Driver Information:\n");
641 mp_msg(MSGT_VO, MSGL_V, "\tvendor: %s,\n\trenderer %s,\n\tversion %s\n",
642 glGetString(GL_VENDOR), glGetString(GL_RENDERER), glVersion);
644 if(glVersion[0]>'1' || (glVersion[0]=='1' && glVersion[2]>='2') )
645 isGL12 = GL_TRUE;
646 else
647 isGL12 = GL_FALSE;
649 if(isGL12) {
650 mp_msg(MSGT_VO, MSGL_INFO, "[gl2] You have OpenGL >= 1.2 capable drivers, GOOD (16bpp and BGR is ok!)\n");
651 } else {
652 mp_msg(MSGT_VO, MSGL_INFO, "[gl2] You have OpenGL < 1.2 drivers, BAD (16bpp and BGR may be damaged!)\n");
655 glFindFormat(format, &image_bpp, &gl_internal_format, &gl_bitmap_format, &gl_bitmap_type);
657 image_bytes=(image_bpp+7)/8;
659 draw_alpha_fnc=draw_alpha_null;
661 switch(image_bpp) {
662 case 15:
663 draw_alpha_fnc=draw_alpha_15; break;
664 case 16:
665 draw_alpha_fnc=draw_alpha_16; break;
666 case 24:
667 draw_alpha_fnc=draw_alpha_24; break;
668 case 32:
669 draw_alpha_fnc=draw_alpha_32; break;
672 if (initGl(vo_dwidth, vo_dheight) == -1)
673 return -1;
675 return 0;
678 #ifdef CONFIG_GL_X11
679 static int gl_handlekey(int key)
681 if(key=='a'||key=='A') {
682 gl_set_antialias(!gl_antialias);
683 return 0;
684 } else if(key=='b'||key=='B') {
685 gl_set_bilinear(-1);
686 return 0;
688 return 1;
690 #endif
692 static void check_events(void)
694 int e;
695 #ifdef CONFIG_GL_X11
696 XEvent Event;
697 char buf[100];
698 KeySym keySym;
699 int key;
700 static XComposeStatus stat;
702 while ( XPending( mDisplay ) ) {
703 XNextEvent( mDisplay,&Event );
704 if( Event.type == KeyPress ) {
705 XLookupString( &Event.xkey,buf,sizeof(buf),&keySym,&stat );
706 key = (keySym&0xff00) != 0 ? (keySym&0x00ff) + 256 : keySym;
707 if(gl_handlekey(key))
708 XPutBackEvent(mDisplay, &Event);
709 break;
710 } else {
711 XPutBackEvent(mDisplay, &Event);
712 break;
715 #endif
716 e=glctx.check_events();
717 if(e&VO_EVENT_RESIZE) resize(vo_dwidth, vo_dheight);
718 if(e&VO_EVENT_EXPOSE && int_pause) flip_page();
721 static void draw_osd(void)
723 if (ImageData)
724 vo_draw_text(image_width,image_height,draw_alpha_fnc);
727 static void
728 flip_page(void)
730 drawTextureDisplay();
732 // glFlush();
733 if (use_glFinish)
734 glFinish();
735 glctx.swapGlBuffers(&glctx);
737 if (aspect_scaling()) // Avoid flickering borders in fullscreen mode
738 glClear (GL_COLOR_BUFFER_BIT);
741 static int draw_slice(uint8_t *src[], int stride[], int w,int h,int x,int y)
743 uint8_t *yptr = src[0], *uptr = src[1], *vptr = src[2];
744 int ystride = stride[0], ustride = stride[1], vstride = stride[2];
745 int rem_h = h;
746 struct TexSquare *texline = &texgrid[y / texture_height * texnumx];
747 int subtex_y = y % texture_width;
748 int xs, ys;
749 mp_get_chroma_shift(image_format, &xs, &ys, NULL);
750 while (rem_h > 0) {
751 int rem_w = w;
752 struct TexSquare *tsq = &texline[x / texture_width];
753 int subtex_x = x % texture_height;
754 int subtex_h = rem_h;
755 if (subtex_y + subtex_h > texture_height)
756 subtex_h = texture_height - subtex_y;
757 while (rem_w > 0) {
758 int subtex_w = rem_w;
759 if (subtex_x + subtex_w > texture_width)
760 subtex_w = texture_width - subtex_x;
761 mpglActiveTexture(GL_TEXTURE0);
762 glBindTexture(GL_TEXTURE_2D, tsq->texobj);
763 glUploadTex(GL_TEXTURE_2D, gl_bitmap_format, gl_bitmap_type,
764 yptr, ystride, subtex_x, subtex_y,
765 subtex_w, subtex_h, 0);
766 mpglActiveTexture(GL_TEXTURE1);
767 glBindTexture(GL_TEXTURE_2D, tsq->uvtexobjs[0]);
768 glUploadTex(GL_TEXTURE_2D, gl_bitmap_format, gl_bitmap_type,
769 uptr, ustride, subtex_x >> xs, subtex_y >> ys,
770 subtex_w >> xs, subtex_h >> ys, 0);
771 mpglActiveTexture(GL_TEXTURE2);
772 glBindTexture(GL_TEXTURE_2D, tsq->uvtexobjs[1]);
773 glUploadTex(GL_TEXTURE_2D, gl_bitmap_format, gl_bitmap_type,
774 vptr, vstride, subtex_x >> xs, subtex_y >> ys,
775 subtex_w >> xs, subtex_h >> ys, 0);
776 subtex_x = 0;
777 yptr += subtex_w;
778 uptr += subtex_w >> xs;
779 vptr += subtex_w >> xs;
780 tsq++;
781 rem_w -= subtex_w;
783 subtex_y = 0;
784 yptr += subtex_h * ystride - w;
785 uptr += (subtex_h >> ys) * ustride - (w >> xs);
786 vptr += (subtex_h >> ys) * vstride - (w >> xs);
787 texline += texnumx;
788 rem_h -= subtex_h;
790 mpglActiveTexture(GL_TEXTURE0);
791 return 0;
794 static int
795 draw_frame(uint8_t *src[])
797 if (is_yuv) {
798 mp_msg(MSGT_VO, MSGL_ERR, "[gl2] error: draw_frame called for YV12!\n");
799 return 0;
801 ImageData=(unsigned char *)src[0];
802 resetTexturePointers(ImageData);
803 texdirty = 1;
804 return 0;
807 static int
808 query_format(uint32_t format)
810 int depth;
811 if (use_yuv && mp_get_chroma_shift(format, NULL, NULL, &depth) &&
812 (depth == 8 || depth == 16 || glYUVLargeRange(use_yuv)) &&
813 (IMGFMT_IS_YUVP16_NE(format) || !IMGFMT_IS_YUVP16(format)))
814 return VFCAP_CSP_SUPPORTED | VFCAP_CSP_SUPPORTED_BY_HW | VFCAP_OSD |
815 VFCAP_HWSCALE_UP | VFCAP_HWSCALE_DOWN | VFCAP_ACCEPT_STRIDE;
816 switch(format) {
817 #ifdef __APPLE__
818 case IMGFMT_RGB32:
819 #else
820 case IMGFMT_RGB24:
821 case IMGFMT_BGR24:
822 // case IMGFMT_RGB32:
823 // case IMGFMT_BGR32:
824 #endif
825 return VFCAP_CSP_SUPPORTED | VFCAP_CSP_SUPPORTED_BY_HW | VFCAP_OSD;
827 return 0;
831 static void
832 uninit(void)
834 if ( !vo_config_count ) return;
835 free(texgrid);
836 texgrid = NULL;
837 uninit_mpglcontext(&glctx);
840 static const opt_t subopts[] = {
841 {"yuv", OPT_ARG_INT, &use_yuv, int_non_neg},
842 {"glfinish", OPT_ARG_BOOL, &use_glFinish, NULL},
843 {NULL}
846 static int preinit(const char *arg)
848 enum MPGLType gltype = GLTYPE_X11;
849 // set defaults
850 #ifdef CONFIG_GL_WIN32
851 gltype = GLTYPE_W32;
852 #endif
853 use_yuv = -1;
854 use_glFinish = 1;
855 if (subopt_parse(arg, subopts) != 0) {
856 mp_msg(MSGT_VO, MSGL_FATAL,
857 "\n-vo gl2 command line help:\n"
858 "Example: mplayer -vo gl2:noglfinish\n"
859 "\nOptions:\n"
860 " noglfinish\n"
861 " Do not call glFinish() before swapping buffers\n"
862 " yuv=<n>\n"
863 " 0: use software YUV to RGB conversion.\n"
864 " 1: use register combiners (nVidia only, for older cards).\n"
865 " 2: use fragment program.\n"
866 " 3: use fragment program with gamma correction.\n"
867 " 4: use fragment program with gamma correction via lookup.\n"
868 " 5: use ATI-specific method (for older cards).\n"
869 "\n" );
870 return -1;
872 if(!init_mpglcontext(&glctx, gltype)) goto err_out;
873 if (use_yuv == -1) {
874 #ifdef CONFIG_GL_WIN32
875 if (config_w32(320, 200, 320, 200, VOFLAG_HIDDEN, "", 0) == -1)
876 #endif
877 #ifdef CONFIG_GL_X11
878 if (config_glx(320, 200, 320, 200, VOFLAG_HIDDEN, "", 0) == -1)
879 #endif
880 goto err_out;
881 if (glctx.setGlWindow(&glctx) == SET_WINDOW_FAILED)
882 goto err_out;
883 use_yuv = glAutodetectYUVConversion();
885 return 0;
887 err_out:
888 uninit();
889 return -1;
892 static int control(uint32_t request, void *data)
894 switch (request) {
895 case VOCTRL_PAUSE:
896 case VOCTRL_RESUME:
897 int_pause = (request == VOCTRL_PAUSE);
898 return VO_TRUE;
899 case VOCTRL_QUERY_FORMAT:
900 return query_format(*((uint32_t*)data));
901 case VOCTRL_ONTOP:
902 glctx.ontop();
903 return VO_TRUE;
904 case VOCTRL_FULLSCREEN:
905 glctx.fullscreen();
906 if (glctx.setGlWindow(&glctx) == SET_WINDOW_REINIT)
907 initGl(vo_dwidth, vo_dheight);
908 resize(vo_dwidth, vo_dheight);
909 return VO_TRUE;
910 case VOCTRL_BORDER:
911 glctx.border();
912 return VO_TRUE;
913 case VOCTRL_GET_PANSCAN:
914 return VO_TRUE;
915 case VOCTRL_SET_PANSCAN:
916 resize(vo_dwidth, vo_dheight);
917 return VO_TRUE;
918 #ifdef CONFIG_GL_X11
919 case VOCTRL_SET_EQUALIZER:
921 struct voctrl_set_equalizer_args *args = data;
922 return vo_x11_set_equalizer(args->name, args->value);
924 case VOCTRL_GET_EQUALIZER:
926 struct voctrl_get_equalizer_args *args = data;
927 return vo_x11_get_equalizer(args->name, args->valueptr);
929 #endif
930 case VOCTRL_UPDATE_SCREENINFO:
931 glctx.update_xinerama_info();
932 return VO_TRUE;
934 return VO_NOTIMPL;