Mercurial > mplayer.hg
annotate libvo/vo_gl.c @ 1393:5eef9e69b145
Beginning sse optimization of dct code.
author | atmos4 |
---|---|
date | Fri, 27 Jul 2001 17:21:23 +0000 |
parents | 2460ebcb6e69 |
children | d40f2b686846 |
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){ |
1290 | 83 printf("[gl] Resize: %dx%d\n",x,y); |
1 | 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 { | |
1290 | 124 printf("[gl] 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 ); | |
1290 | 176 if (vinfo == NULL) |
177 { | |
178 printf("[gl] no GLX support present\n"); | |
179 return -1; | |
180 } | |
1 | 181 |
182 xswa.background_pixel = 0; | |
183 xswa.border_pixel = 1; | |
184 // xswa.colormap = XCreateColormap(mydisplay, RootWindow(mydisplay,screen), vinfo.visual, AllocNone); | |
185 xswa.colormap = XCreateColormap(mydisplay, RootWindow(mydisplay,screen), vinfo->visual, AllocNone); | |
186 xswamask = CWBackPixel | CWBorderPixel | CWColormap; | |
187 // xswamask = CWBackPixel | CWBorderPixel | CWColormap | CWEventMask | CWCursor | CWOverrideRedirect | CWSaveUnder | CWX | CWY | CWWidth | CWHeight; | |
188 | |
189 mywindow = XCreateWindow(mydisplay, RootWindow(mydisplay,screen), | |
190 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
|
191 |
4c7b219e126c
patch: some X11 compliance fixed: set ClassHint and better fullscreen mode
arpi_esp
parents:
1109
diff
changeset
|
192 vo_x11_classhint( mydisplay,mywindow,"gl" ); |
384 | 193 vo_hidecursor(mydisplay,mywindow); |
1 | 194 |
195 wsGLXContext=glXCreateContext( mydisplay,vinfo,NULL,True ); | |
196 // XStoreName( wsDisplay,wsMyWin,wsSysName ); | |
197 | |
198 // printf("GLXcontext ok\n"); | |
199 | |
31 | 200 if ( fullscreen ) vo_x11_decoration( mydisplay,mywindow,0 ); |
1 | 201 |
202 XSelectInput(mydisplay, mywindow, StructureNotifyMask); | |
203 | |
204 /* Tell other applications about this window */ | |
205 | |
206 XSetStandardProperties(mydisplay, mywindow, hello, hello, None, NULL, 0, &hint); | |
207 | |
208 /* Map window. */ | |
209 | |
210 XMapWindow(mydisplay, mywindow); | |
211 | |
212 /* Wait for map. */ | |
213 do | |
214 { | |
215 XNextEvent(mydisplay, &xev); | |
216 } | |
217 while (xev.type != MapNotify || xev.xmap.event != mywindow); | |
218 | |
219 XSelectInput(mydisplay, mywindow, NoEventMask); | |
220 | |
221 glXMakeCurrent( mydisplay,mywindow,wsGLXContext ); | |
222 | |
223 XFlush(mydisplay); | |
224 XSync(mydisplay, False); | |
225 | |
226 // mygc = XCreateGC(mydisplay, mywindow, 0L, &xgcv); | |
227 | |
228 // myximage = XGetImage(mydisplay, mywindow, 0, 0, | |
229 // width, image_height, AllPlanes, ZPixmap); | |
230 // ImageData = myximage->data; | |
231 // bpp = myximage->bits_per_pixel; | |
232 | |
233 //XSelectInput(mydisplay, mywindow, StructureNotifyMask); // !!!! | |
234 XSelectInput(mydisplay, mywindow, StructureNotifyMask | KeyPressMask ); | |
235 | |
236 // printf("Window setup ok\n"); | |
237 | |
238 #if 0 | |
239 // If we have blue in the lowest bit then obviously RGB | |
240 mode = ((myximage->blue_mask & 0x01) != 0) ? MODE_RGB : MODE_BGR; | |
241 #ifdef WORDS_BIGENDIAN | |
242 if (myximage->byte_order != MSBFirst) | |
243 #else | |
244 if (myximage->byte_order != LSBFirst) | |
245 #endif | |
246 { | |
1302 | 247 printf("[gl] no support for non-native XImage byte order!\n"); |
1 | 248 return -1; |
249 } | |
250 | |
251 printf("DEPTH=%d BPP=%d\n",depth,bpp); | |
252 #endif | |
253 | |
254 /* | |
255 * If depth is 24 then it may either be a 3 or 4 byte per pixel | |
256 * format. We can't use bpp because then we would lose the | |
257 * distinction between 15/16bit depth (2 byte formate assumed). | |
258 * | |
259 * FIXME - change yuv2rgb_init to take both depth and bpp | |
260 * parameters | |
261 */ | |
262 | |
263 texture_width=32; | |
264 while(texture_width<image_width) texture_width*=2; | |
265 while(texture_width<image_height) texture_width*=2; | |
266 texture_height=texture_width; | |
267 | |
268 if(format==IMGFMT_YV12){ | |
269 yuv2rgb_init(8*BYTES_PP, MODE_BGR); | |
1290 | 270 printf("[gl] YUV init OK!\n"); |
1 | 271 image_bpp=8*BYTES_PP; |
272 image_bytes=BYTES_PP; | |
273 } else { | |
274 image_bpp=format&0xFF; | |
275 image_bytes=(image_bpp+7)/8; | |
276 } | |
277 | |
278 ImageData=malloc(texture_width*texture_height*image_bytes); | |
279 memset(ImageData,128,texture_width*texture_height*image_bytes); | |
280 | |
281 glDisable(GL_BLEND); | |
282 glDisable(GL_DEPTH_TEST); | |
283 glDepthMask(GL_FALSE); | |
284 glDisable(GL_CULL_FACE); | |
285 | |
286 glEnable(GL_TEXTURE_2D); | |
287 | |
1290 | 288 printf("[gl] Creating %dx%d texture...\n",texture_width,texture_height); |
1 | 289 |
290 #if 1 | |
291 // glBindTexture(GL_TEXTURE_2D, texture_id); | |
292 glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER, GL_LINEAR); | |
293 glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER, GL_LINEAR); | |
294 #ifdef TEXTUREFORMAT_32BPP | |
295 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, texture_width, texture_height, 0, | |
296 #else | |
297 glTexImage2D(GL_TEXTURE_2D, 0, BYTES_PP, texture_width, texture_height, 0, | |
298 #endif | |
299 (image_bytes==4)?GL_RGBA:GL_BGR, GL_UNSIGNED_BYTE, ImageData); | |
300 #endif | |
301 | |
302 resize(d_width,d_height); | |
303 | |
304 glClearColor( 1.0f,0.0f,1.0f,0.0f ); | |
305 glClear( GL_COLOR_BUFFER_BIT ); | |
306 | |
307 // printf("OpenGL setup OK!\n"); | |
308 | |
309 X_already_started++; | |
310 return 0; | |
311 } | |
312 | |
313 static const vo_info_t* | |
314 get_info(void) | |
315 { | |
316 return &vo_info; | |
317 } | |
318 | |
319 static void | |
320 Terminate_Display_Process(void) | |
321 { | |
322 getchar(); /* wait for enter to remove window */ | |
323 XDestroyWindow(mydisplay, mywindow); | |
324 XCloseDisplay(mydisplay); | |
325 X_already_started = 0; | |
326 } | |
327 | |
31 | 328 |
329 static void check_events(void) | |
1 | 330 { |
31 | 331 int e=vo_x11_check_events(mydisplay); |
332 if(e&VO_EVENT_RESIZE) resize(vo_dwidth,vo_dheight); | |
333 } | |
1 | 334 |
31 | 335 |
336 static void | |
337 flip_page(void) | |
338 { | |
339 | |
340 check_events(); | |
1 | 341 |
342 // glEnable(GL_TEXTURE_2D); | |
343 // glBindTexture(GL_TEXTURE_2D, texture_id); | |
344 | |
345 glColor3f(1,1,1); | |
346 glBegin(GL_QUADS); | |
347 glTexCoord2f(0,0);glVertex2i(0,0); | |
348 glTexCoord2f(0,1);glVertex2i(0,texture_height); | |
349 glTexCoord2f(1,1);glVertex2i(texture_width,texture_height); | |
350 glTexCoord2f(1,0);glVertex2i(texture_width,0); | |
351 glEnd(); | |
352 | |
353 // glFlush(); | |
354 glFinish(); | |
355 glXSwapBuffers( mydisplay,mywindow ); | |
356 | |
357 } | |
358 | |
359 //static inline uint32_t draw_slice_x11(uint8_t *src[], uint32_t slice_num) | |
360 static uint32_t draw_slice(uint8_t *src[], int stride[], int w,int h,int x,int y) | |
361 { | |
362 int i; | |
363 int dstride=w*BYTES_PP; | |
364 | |
365 // dstride=(dstride+15)&(~15); | |
366 | |
367 yuv2rgb(ImageData, src[0], src[1], src[2], | |
368 w,h, dstride, stride[0],stride[1]); | |
369 | |
370 // emms (); | |
371 | |
372 for(i=0;i<h;i++){ | |
373 glTexSubImage2D( GL_TEXTURE_2D, // target | |
374 0, // level | |
375 x, // x offset | |
376 y+i, // y offset | |
377 w, // width | |
378 1, // height | |
379 (BYTES_PP==4)?GL_RGBA:GL_RGB, // format | |
380 GL_UNSIGNED_BYTE, // type | |
381 ImageData+i*dstride ); // *pixels | |
382 } | |
383 | |
384 return 0; | |
385 } | |
386 | |
387 static inline uint32_t | |
388 draw_frame_x11_yv12(uint8_t *src[]) | |
389 { | |
390 int i; | |
391 // printf("Converting YUV->RGB...\n"); | |
392 yuv2rgb(ImageData, src[0], src[1], src[2], | |
393 image_width, image_height, | |
394 image_width*BYTES_PP, image_width, image_width/2 ); | |
395 // printf("Ready!\n"); | |
396 | |
397 // emms (); | |
398 | |
399 for(i=0;i<image_height;i++){ | |
400 glTexSubImage2D( GL_TEXTURE_2D, // target | |
401 0, // level | |
402 0, // x offset | |
403 i, // y offset | |
404 image_width, // width | |
405 1, // height | |
406 (BYTES_PP==4)?GL_RGBA:GL_RGB, // format | |
407 GL_UNSIGNED_BYTE, // type | |
408 ImageData+i*BYTES_PP*image_width ); // *pixels | |
409 } | |
410 | |
411 // Display_Image(ImageData); | |
412 return 0; | |
413 } | |
414 | |
415 | |
416 static inline uint32_t | |
417 draw_frame_x11_bgr(uint8_t *src[]) | |
418 { | |
419 int i; | |
420 uint8_t *s=src[0]; | |
421 uint8_t *de=&ImageData[3*image_width]; | |
422 | |
423 for(i=0;i<image_height;i++){ | |
424 uint8_t *d=ImageData; | |
425 while(d<de){ | |
426 d[0]=s[2]; | |
427 d[1]=s[1]; | |
428 d[2]=s[0]; | |
429 s+=3;d+=3; | |
430 } | |
431 glTexSubImage2D( GL_TEXTURE_2D, // target | |
432 0, // level | |
433 0, // x offset | |
434 // image_height-1-i, // y offset | |
435 i, // y offset | |
436 image_width, // width | |
437 1, // height | |
438 (image_bytes==4)?GL_RGBA:GL_RGB, // format | |
439 GL_UNSIGNED_BYTE, // type | |
440 ImageData); // *pixels | |
441 } | |
442 | |
443 // Display_Image(ImageData); | |
444 return 0; | |
445 } | |
446 | |
447 static inline uint32_t | |
448 draw_frame_x11_rgb(uint8_t *src[]) | |
449 { | |
450 int i; | |
451 uint8_t *ImageData=src[0]; | |
452 | |
453 for(i=0;i<image_height;i++){ | |
454 glTexSubImage2D( GL_TEXTURE_2D, // target | |
455 0, // level | |
456 0, // x offset | |
457 // image_height-1-i, // y offset | |
458 i, // y offset | |
459 image_width, // width | |
460 1, // height | |
461 (image_bytes==4)?GL_RGBA:GL_RGB, // format | |
462 GL_UNSIGNED_BYTE, // type | |
463 ImageData+i*image_bytes*image_width ); // *pixels | |
464 } | |
465 | |
466 // Display_Image(ImageData); | |
467 return 0; | |
468 } | |
469 | |
470 | |
471 static uint32_t | |
472 draw_frame(uint8_t *src[]) | |
473 { | |
474 if(image_format==IMGFMT_YV12) | |
475 return draw_frame_x11_yv12(src); | |
476 else | |
477 if((image_format&IMGFMT_RGB_MASK)==IMGFMT_RGB) | |
478 return draw_frame_x11_rgb(src); | |
479 else | |
480 return draw_frame_x11_bgr(src); | |
481 } | |
482 | |
483 static uint32_t | |
484 query_format(uint32_t format) | |
485 { | |
486 switch(format){ | |
487 case IMGFMT_YV12: | |
488 case IMGFMT_RGB|24: | |
489 case IMGFMT_BGR|24: | |
490 return 1; | |
491 } | |
492 return 0; | |
493 } | |
494 | |
495 | |
496 static void | |
497 uninit(void) | |
498 { | |
499 } |