Mercurial > mplayer.hg
annotate libvo/vo_gl.c @ 1231:d216f62ce921
strip is handled by install
author | eyck |
---|---|
date | Tue, 26 Jun 2001 11:12:27 +0000 |
parents | 4c7b219e126c |
children | af0b764f34c5 |
rev | line source |
---|---|
1 | 1 #define DISP |
2 | |
3 // this can be 3 or 4 (regarding 24bpp and 32bpp) | |
4 #define BYTES_PP 3 | |
5 | |
6 #define TEXTUREFORMAT_32BPP | |
7 | |
8 /* | |
9 * video_out_gl.c, X11/OpenGL interface | |
10 * based on video_out_x11 by Aaron Holtzman, | |
11 * and WS opengl window manager by Pontscho/Fresh! | |
12 */ | |
13 | |
14 #include <stdio.h> | |
15 #include <stdlib.h> | |
16 #include <string.h> | |
17 #include <math.h> | |
18 | |
19 #include "config.h" | |
20 #include "video_out.h" | |
21 #include "video_out_internal.h" | |
22 | |
23 | |
24 LIBVO_EXTERN(gl) | |
25 | |
26 #include <X11/Xlib.h> | |
27 #include <X11/Xutil.h> | |
28 //#include <X11/keysym.h> | |
29 #include <GL/glx.h> | |
30 #include <errno.h> | |
31 #include "yuv2rgb.h" | |
32 | |
33 #include <GL/gl.h> | |
34 | |
31 | 35 #include "x11_common.h" |
36 | |
1 | 37 static vo_info_t vo_info = |
38 { | |
39 "X11 (OpenGL)", | |
40 "gl", | |
41 "Arpad Gereoffy <arpi@esp-team.scene.hu>", | |
42 "" | |
43 }; | |
44 | |
45 /* private prototypes */ | |
1109 | 46 // static void Display_Image (unsigned char *ImageData); |
1 | 47 |
48 /* local data */ | |
49 static unsigned char *ImageData=NULL; | |
50 | |
51 /* X11 related variables */ | |
52 static Display *mydisplay; | |
53 static Window mywindow; | |
54 //static GC mygc; | |
55 //static XImage *myximage; | |
56 //static int depth,mode; | |
57 static XWindowAttributes attribs; | |
58 static int X_already_started = 0; | |
59 | |
1109 | 60 //static int texture_id=1; |
1 | 61 |
62 static GLXContext wsGLXContext; | |
63 //XVisualInfo * wsVisualInfo; | |
64 int wsGLXAttrib[] = { GLX_RGBA, | |
65 GLX_RED_SIZE,1, | |
66 GLX_GREEN_SIZE,1, | |
67 GLX_BLUE_SIZE,1, | |
68 // GLX_DEPTH_SIZE,16, | |
69 GLX_DOUBLEBUFFER, | |
70 None }; | |
71 | |
72 | |
73 static uint32_t image_width; | |
74 static uint32_t image_height; | |
75 static uint32_t image_format; | |
76 static uint32_t image_bpp; | |
77 static uint32_t image_bytes; | |
78 | |
79 static uint32_t texture_width; | |
80 static uint32_t texture_height; | |
81 | |
612 | 82 static void resize(int x,int y){ |
1 | 83 printf("Resize: %dx%d\n",x,y); |
84 glViewport( 0, 0, x, y ); | |
85 | |
86 glMatrixMode(GL_PROJECTION); | |
87 glLoadIdentity(); | |
88 glOrtho(0, image_width, image_height, 0, -1,1); | |
89 | |
90 glMatrixMode(GL_MODELVIEW); | |
91 glLoadIdentity(); | |
92 } | |
93 | |
94 /* connect to server, create and map window, | |
95 * allocate colors and (shared) memory | |
96 */ | |
97 static uint32_t | |
98 init(uint32_t width, uint32_t height, uint32_t d_width, uint32_t d_height, uint32_t fullscreen, char *title, uint32_t format) | |
99 { | |
100 int screen; | |
101 unsigned int fg, bg; | |
102 char *hello = (title == NULL) ? "OpenGL rulez" : title; | |
103 char *name = ":0.0"; | |
104 XSizeHints hint; | |
105 XVisualInfo *vinfo; | |
106 XEvent xev; | |
107 | |
1109 | 108 // XGCValues xgcv; |
1 | 109 XSetWindowAttributes xswa; |
110 unsigned long xswamask; | |
111 | |
112 image_height = height; | |
113 image_width = width; | |
114 image_format = format; | |
115 | |
116 if (X_already_started) return -1; | |
117 | |
118 if(getenv("DISPLAY")) name = getenv("DISPLAY"); | |
119 | |
120 mydisplay = XOpenDisplay(name); | |
121 | |
122 if (mydisplay == NULL) | |
123 { | |
614 | 124 printf("Can not open display\n"); |
1 | 125 return -1; |
126 } | |
127 | |
128 screen = DefaultScreen(mydisplay); | |
129 | |
130 hint.x = 0; | |
131 hint.y = 0; | |
132 hint.width = d_width; | |
133 hint.height = d_height; | |
134 hint.flags = PPosition | PSize; | |
135 | |
136 /* Get some colors */ | |
137 | |
138 bg = WhitePixel(mydisplay, screen); | |
139 fg = BlackPixel(mydisplay, screen); | |
140 | |
141 /* Make the window */ | |
142 | |
143 XGetWindowAttributes(mydisplay, DefaultRootWindow(mydisplay), &attribs); | |
144 | |
145 #if 0 | |
146 /* | |
147 * | |
148 * depth in X11 terminology land is the number of bits used to | |
149 * actually represent the colour. | |
150 * | |
151 * bpp in X11 land means how many bits in the frame buffer per | |
152 * pixel. | |
153 * | |
154 * ex. 15 bit color is 15 bit depth and 16 bpp. Also 24 bit | |
155 * color is 24 bit depth, but can be 24 bpp or 32 bpp. | |
156 */ | |
157 | |
158 depth = attribs.depth; | |
159 | |
160 if (depth != 15 && depth != 16 && depth != 24 && depth != 32) | |
161 { | |
162 /* The root window may be 8bit but there might still be | |
163 * visuals with other bit depths. For example this is the | |
164 * case on Sun/Solaris machines. | |
165 */ | |
166 depth = 24; | |
167 } | |
168 //BEGIN HACK | |
169 //mywindow = XCreateSimpleWindow(mydisplay, DefaultRootWindow(mydisplay), | |
170 //hint.x, hint.y, hint.width, hint.height, 4, fg, bg); | |
171 // | |
172 #endif | |
173 | |
174 // XMatchVisualInfo(mydisplay, screen, depth, TrueColor, &vinfo); | |
175 vinfo=glXChooseVisual( mydisplay,screen,wsGLXAttrib ); | |
176 | |
177 xswa.background_pixel = 0; | |
178 xswa.border_pixel = 1; | |
179 // xswa.colormap = XCreateColormap(mydisplay, RootWindow(mydisplay,screen), vinfo.visual, AllocNone); | |
180 xswa.colormap = XCreateColormap(mydisplay, RootWindow(mydisplay,screen), vinfo->visual, AllocNone); | |
181 xswamask = CWBackPixel | CWBorderPixel | CWColormap; | |
182 // xswamask = CWBackPixel | CWBorderPixel | CWColormap | CWEventMask | CWCursor | CWOverrideRedirect | CWSaveUnder | CWX | CWY | CWWidth | CWHeight; | |
183 | |
184 mywindow = XCreateWindow(mydisplay, RootWindow(mydisplay,screen), | |
185 hint.x, hint.y, hint.width, hint.height, 4, vinfo->depth,CopyFromParent,vinfo->visual,xswamask,&xswa); | |
1137
4c7b219e126c
patch: some X11 compliance fixed: set ClassHint and better fullscreen mode
arpi_esp
parents:
1109
diff
changeset
|
186 |
4c7b219e126c
patch: some X11 compliance fixed: set ClassHint and better fullscreen mode
arpi_esp
parents:
1109
diff
changeset
|
187 vo_x11_classhint( mydisplay,mywindow,"gl" ); |
384 | 188 vo_hidecursor(mydisplay,mywindow); |
1 | 189 |
190 wsGLXContext=glXCreateContext( mydisplay,vinfo,NULL,True ); | |
191 // XStoreName( wsDisplay,wsMyWin,wsSysName ); | |
192 | |
193 // printf("GLXcontext ok\n"); | |
194 | |
31 | 195 if ( fullscreen ) vo_x11_decoration( mydisplay,mywindow,0 ); |
1 | 196 |
197 XSelectInput(mydisplay, mywindow, StructureNotifyMask); | |
198 | |
199 /* Tell other applications about this window */ | |
200 | |
201 XSetStandardProperties(mydisplay, mywindow, hello, hello, None, NULL, 0, &hint); | |
202 | |
203 /* Map window. */ | |
204 | |
205 XMapWindow(mydisplay, mywindow); | |
206 | |
207 /* Wait for map. */ | |
208 do | |
209 { | |
210 XNextEvent(mydisplay, &xev); | |
211 } | |
212 while (xev.type != MapNotify || xev.xmap.event != mywindow); | |
213 | |
214 XSelectInput(mydisplay, mywindow, NoEventMask); | |
215 | |
216 glXMakeCurrent( mydisplay,mywindow,wsGLXContext ); | |
217 | |
218 XFlush(mydisplay); | |
219 XSync(mydisplay, False); | |
220 | |
221 // mygc = XCreateGC(mydisplay, mywindow, 0L, &xgcv); | |
222 | |
223 // myximage = XGetImage(mydisplay, mywindow, 0, 0, | |
224 // width, image_height, AllPlanes, ZPixmap); | |
225 // ImageData = myximage->data; | |
226 // bpp = myximage->bits_per_pixel; | |
227 | |
228 //XSelectInput(mydisplay, mywindow, StructureNotifyMask); // !!!! | |
229 XSelectInput(mydisplay, mywindow, StructureNotifyMask | KeyPressMask ); | |
230 | |
231 // printf("Window setup ok\n"); | |
232 | |
233 #if 0 | |
234 // If we have blue in the lowest bit then obviously RGB | |
235 mode = ((myximage->blue_mask & 0x01) != 0) ? MODE_RGB : MODE_BGR; | |
236 #ifdef WORDS_BIGENDIAN | |
237 if (myximage->byte_order != MSBFirst) | |
238 #else | |
239 if (myximage->byte_order != LSBFirst) | |
240 #endif | |
241 { | |
242 fprintf( stderr, "No support fon non-native XImage byte order!\n" ); | |
243 return -1; | |
244 } | |
245 | |
246 printf("DEPTH=%d BPP=%d\n",depth,bpp); | |
247 #endif | |
248 | |
249 /* | |
250 * If depth is 24 then it may either be a 3 or 4 byte per pixel | |
251 * format. We can't use bpp because then we would lose the | |
252 * distinction between 15/16bit depth (2 byte formate assumed). | |
253 * | |
254 * FIXME - change yuv2rgb_init to take both depth and bpp | |
255 * parameters | |
256 */ | |
257 | |
258 texture_width=32; | |
259 while(texture_width<image_width) texture_width*=2; | |
260 while(texture_width<image_height) texture_width*=2; | |
261 texture_height=texture_width; | |
262 | |
263 if(format==IMGFMT_YV12){ | |
264 yuv2rgb_init(8*BYTES_PP, MODE_BGR); | |
265 printf("YUV init OK!\n"); | |
266 image_bpp=8*BYTES_PP; | |
267 image_bytes=BYTES_PP; | |
268 } else { | |
269 image_bpp=format&0xFF; | |
270 image_bytes=(image_bpp+7)/8; | |
271 } | |
272 | |
273 ImageData=malloc(texture_width*texture_height*image_bytes); | |
274 memset(ImageData,128,texture_width*texture_height*image_bytes); | |
275 | |
276 glDisable(GL_BLEND); | |
277 glDisable(GL_DEPTH_TEST); | |
278 glDepthMask(GL_FALSE); | |
279 glDisable(GL_CULL_FACE); | |
280 | |
281 glEnable(GL_TEXTURE_2D); | |
282 | |
283 printf("Creating %dx%d texture...\n",texture_width,texture_height); | |
284 | |
285 #if 1 | |
286 // glBindTexture(GL_TEXTURE_2D, texture_id); | |
287 glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER, GL_LINEAR); | |
288 glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER, GL_LINEAR); | |
289 #ifdef TEXTUREFORMAT_32BPP | |
290 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, texture_width, texture_height, 0, | |
291 #else | |
292 glTexImage2D(GL_TEXTURE_2D, 0, BYTES_PP, texture_width, texture_height, 0, | |
293 #endif | |
294 (image_bytes==4)?GL_RGBA:GL_BGR, GL_UNSIGNED_BYTE, ImageData); | |
295 #endif | |
296 | |
297 resize(d_width,d_height); | |
298 | |
299 glClearColor( 1.0f,0.0f,1.0f,0.0f ); | |
300 glClear( GL_COLOR_BUFFER_BIT ); | |
301 | |
302 // printf("OpenGL setup OK!\n"); | |
303 | |
304 X_already_started++; | |
305 return 0; | |
306 } | |
307 | |
308 static const vo_info_t* | |
309 get_info(void) | |
310 { | |
311 return &vo_info; | |
312 } | |
313 | |
314 static void | |
315 Terminate_Display_Process(void) | |
316 { | |
317 getchar(); /* wait for enter to remove window */ | |
318 XDestroyWindow(mydisplay, mywindow); | |
319 XCloseDisplay(mydisplay); | |
320 X_already_started = 0; | |
321 } | |
322 | |
31 | 323 |
324 static void check_events(void) | |
1 | 325 { |
31 | 326 int e=vo_x11_check_events(mydisplay); |
327 if(e&VO_EVENT_RESIZE) resize(vo_dwidth,vo_dheight); | |
328 } | |
1 | 329 |
31 | 330 |
331 static void | |
332 flip_page(void) | |
333 { | |
334 | |
335 check_events(); | |
1 | 336 |
337 // glEnable(GL_TEXTURE_2D); | |
338 // glBindTexture(GL_TEXTURE_2D, texture_id); | |
339 | |
340 glColor3f(1,1,1); | |
341 glBegin(GL_QUADS); | |
342 glTexCoord2f(0,0);glVertex2i(0,0); | |
343 glTexCoord2f(0,1);glVertex2i(0,texture_height); | |
344 glTexCoord2f(1,1);glVertex2i(texture_width,texture_height); | |
345 glTexCoord2f(1,0);glVertex2i(texture_width,0); | |
346 glEnd(); | |
347 | |
348 // glFlush(); | |
349 glFinish(); | |
350 glXSwapBuffers( mydisplay,mywindow ); | |
351 | |
352 } | |
353 | |
354 //static inline uint32_t draw_slice_x11(uint8_t *src[], uint32_t slice_num) | |
355 static uint32_t draw_slice(uint8_t *src[], int stride[], int w,int h,int x,int y) | |
356 { | |
357 int i; | |
358 int dstride=w*BYTES_PP; | |
359 | |
360 // dstride=(dstride+15)&(~15); | |
361 | |
362 yuv2rgb(ImageData, src[0], src[1], src[2], | |
363 w,h, dstride, stride[0],stride[1]); | |
364 | |
365 // emms (); | |
366 | |
367 for(i=0;i<h;i++){ | |
368 glTexSubImage2D( GL_TEXTURE_2D, // target | |
369 0, // level | |
370 x, // x offset | |
371 y+i, // y offset | |
372 w, // width | |
373 1, // height | |
374 (BYTES_PP==4)?GL_RGBA:GL_RGB, // format | |
375 GL_UNSIGNED_BYTE, // type | |
376 ImageData+i*dstride ); // *pixels | |
377 } | |
378 | |
379 return 0; | |
380 } | |
381 | |
382 static inline uint32_t | |
383 draw_frame_x11_yv12(uint8_t *src[]) | |
384 { | |
385 int i; | |
386 // printf("Converting YUV->RGB...\n"); | |
387 yuv2rgb(ImageData, src[0], src[1], src[2], | |
388 image_width, image_height, | |
389 image_width*BYTES_PP, image_width, image_width/2 ); | |
390 // printf("Ready!\n"); | |
391 | |
392 // emms (); | |
393 | |
394 for(i=0;i<image_height;i++){ | |
395 glTexSubImage2D( GL_TEXTURE_2D, // target | |
396 0, // level | |
397 0, // x offset | |
398 i, // y offset | |
399 image_width, // width | |
400 1, // height | |
401 (BYTES_PP==4)?GL_RGBA:GL_RGB, // format | |
402 GL_UNSIGNED_BYTE, // type | |
403 ImageData+i*BYTES_PP*image_width ); // *pixels | |
404 } | |
405 | |
406 // Display_Image(ImageData); | |
407 return 0; | |
408 } | |
409 | |
410 | |
411 static inline uint32_t | |
412 draw_frame_x11_bgr(uint8_t *src[]) | |
413 { | |
414 int i; | |
415 uint8_t *s=src[0]; | |
416 uint8_t *de=&ImageData[3*image_width]; | |
417 | |
418 for(i=0;i<image_height;i++){ | |
419 uint8_t *d=ImageData; | |
420 while(d<de){ | |
421 d[0]=s[2]; | |
422 d[1]=s[1]; | |
423 d[2]=s[0]; | |
424 s+=3;d+=3; | |
425 } | |
426 glTexSubImage2D( GL_TEXTURE_2D, // target | |
427 0, // level | |
428 0, // x offset | |
429 // image_height-1-i, // y offset | |
430 i, // y offset | |
431 image_width, // width | |
432 1, // height | |
433 (image_bytes==4)?GL_RGBA:GL_RGB, // format | |
434 GL_UNSIGNED_BYTE, // type | |
435 ImageData); // *pixels | |
436 } | |
437 | |
438 // Display_Image(ImageData); | |
439 return 0; | |
440 } | |
441 | |
442 static inline uint32_t | |
443 draw_frame_x11_rgb(uint8_t *src[]) | |
444 { | |
445 int i; | |
446 uint8_t *ImageData=src[0]; | |
447 | |
448 for(i=0;i<image_height;i++){ | |
449 glTexSubImage2D( GL_TEXTURE_2D, // target | |
450 0, // level | |
451 0, // x offset | |
452 // image_height-1-i, // y offset | |
453 i, // y offset | |
454 image_width, // width | |
455 1, // height | |
456 (image_bytes==4)?GL_RGBA:GL_RGB, // format | |
457 GL_UNSIGNED_BYTE, // type | |
458 ImageData+i*image_bytes*image_width ); // *pixels | |
459 } | |
460 | |
461 // Display_Image(ImageData); | |
462 return 0; | |
463 } | |
464 | |
465 | |
466 static uint32_t | |
467 draw_frame(uint8_t *src[]) | |
468 { | |
469 if(image_format==IMGFMT_YV12) | |
470 return draw_frame_x11_yv12(src); | |
471 else | |
472 if((image_format&IMGFMT_RGB_MASK)==IMGFMT_RGB) | |
473 return draw_frame_x11_rgb(src); | |
474 else | |
475 return draw_frame_x11_bgr(src); | |
476 } | |
477 | |
478 static uint32_t | |
479 query_format(uint32_t format) | |
480 { | |
481 switch(format){ | |
482 case IMGFMT_YV12: | |
483 case IMGFMT_RGB|24: | |
484 case IMGFMT_BGR|24: | |
485 return 1; | |
486 } | |
487 return 0; | |
488 } | |
489 | |
490 | |
491 static void | |
492 uninit(void) | |
493 { | |
494 } | |
495 |