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