1+ #include <EGL/egl.h>
2+ #include <EGL/eglext_brcm.h>
3+ #include <GLES2/gl2.h>
4+ #include <stdio.h>
5+ #include <stdlib.h>
6+ #include <stdbool.h>
7+ #include <sys/time.h>
8+ #include <math.h>
9+ #include <X11/Xlib.h>
10+ #include <X11/Xatom.h>
11+ #include <X11/Xutil.h>
12+ Window * root ;
13+ Window * window ;
14+ EGLDisplay * edisplay ;
15+ int width , height ,depth ;
16+ Display * display ;
17+ EGLSurface egl_surface ;
18+ XWindowAttributes * window_attributes_return ;
19+ EGLDisplay eglGetDisplay (NativeDisplayType native_display ) {
20+ puts ("Getting an X11 EGL Display." );
21+ bcm_host_init ();
22+ display = (Display * )native_display ;
23+ root = DefaultRootWindow (display );
24+ edisplay = real_eglGetDisplay (EGL_DEFAULT_DISPLAY );
25+ return edisplay ;
26+ }
27+ EGLSurface eglCreateWindowSurface (EGLDisplay egldisplay , EGLConfig config , NativeWindowType native_window , EGLint const * attrib_list ) {
28+ window = (Window * ) native_window ;
29+ puts ("Getting window information..." );
30+ XWindowAttributes window_attributes ;
31+ XGetWindowAttributes (display ,window ,& window_attributes );
32+ printf ("Window Location: %i,%i \n Window Dimensions %i x %i \n Bit depth : %i \n" ,window_attributes .x ,window_attributes .y ,window_attributes .width ,window_attributes .height ,window_attributes .depth );
33+ width = window_attributes .width ;
34+ height = window_attributes .height ;
35+ depth = window_attributes .depth ;
36+ EGLint attr [] = { // some attributes to set up our egl-interface
37+ EGL_RED_SIZE , 8 ,
38+ EGL_GREEN_SIZE , 8 ,
39+ EGL_BLUE_SIZE , 8 ,
40+ EGL_ALPHA_SIZE , 8 ,
41+ EGL_SURFACE_TYPE ,
42+ EGL_PIXMAP_BIT | EGL_OPENGL_ES2_BIT ,
43+ EGL_NONE
44+ };
45+
46+ EGLConfig ecfg ;
47+ EGLint num_config ;
48+ if (!eglChooseConfig (edisplay , attr , & ecfg , 1 , & num_config )) {
49+ fprintf (stderr , "Failed to choose config (eglError: %s)\n" );
50+ return EGL_NO_SURFACE ;
51+ }
52+ EGLint pixel_format = EGL_PIXEL_FORMAT_ARGB_8888_BRCM ;
53+ EGLint rt ;
54+ eglGetConfigAttrib (edisplay , ecfg , EGL_RENDERABLE_TYPE , & rt );
55+
56+ if (rt & EGL_OPENGL_ES_BIT ) {
57+ pixel_format |= EGL_PIXEL_FORMAT_RENDER_GLES_BRCM ;
58+ pixel_format |= EGL_PIXEL_FORMAT_GLES_TEXTURE_BRCM ;
59+ }
60+ if (rt & EGL_OPENGL_ES2_BIT ) {
61+ pixel_format |= EGL_PIXEL_FORMAT_RENDER_GLES2_BRCM ;
62+ pixel_format |= EGL_PIXEL_FORMAT_GLES2_TEXTURE_BRCM ;
63+ }
64+ if (rt & EGL_OPENVG_BIT ) {
65+ pixel_format |= EGL_PIXEL_FORMAT_RENDER_VG_BRCM ;
66+ pixel_format |= EGL_PIXEL_FORMAT_VG_IMAGE_BRCM ;
67+ }
68+ if (rt & EGL_OPENGL_BIT ) {
69+ pixel_format |= EGL_PIXEL_FORMAT_RENDER_GL_BRCM ;
70+ }
71+ EGLint pixmap [5 ];
72+ pixmap [0 ] = 0 ;
73+ pixmap [1 ] = 0 ;
74+ pixmap [2 ] = width ;
75+ pixmap [3 ] = height ;
76+ pixmap [4 ] = pixel_format ;
77+ eglCreateGlobalImageBRCM (width , height , pixmap [4 ], 0 , width * 4 , pixmap );
78+ egl_surface = eglCreatePixmapSurface (edisplay , ecfg , pixmap , 0 );
79+ puts ("EGL Surface Created" );
80+ EGLint ctxattr [] = {
81+ EGL_CONTEXT_CLIENT_VERSION , 2 ,
82+ EGL_NONE
83+ };
84+ EGLContext context = eglCreateContext ( edisplay , ecfg , EGL_NO_CONTEXT , ctxattr );
85+ real_eglMakeCurrent (edisplay , egl_surface , egl_surface , context );
86+ return egl_surface ;
87+ }
88+ EGLBoolean eglSwapBuffers (EGLDisplay edisplay , EGLSurface egsurface ) {
89+ unsigned int * buffer = (unsigned int * )malloc (height * width * 4 );
90+ glReadPixels (0 , 0 , width , height , GL_RGBA , GL_UNSIGNED_BYTE , buffer );
91+ static GC gc ;
92+ static int status = 0 ;
93+ static XImage * image = 0 ;
94+ static Pixmap pixmap ;
95+ static XGCValues gcvalues ;
96+ if (status == 0 ) {
97+ gc = XCreateGC (display , window ,0 ,& gcvalues );
98+ image = XGetImage (display , window , 0 , 0 , width , height , AllPlanes , ZPixmap );
99+ status = 1 ;
100+ }
101+ XImage * img = XCreateImage (display ,CopyFromParent ,24 ,ZPixmap ,0 ,buffer ,width , height , 32 , 0 );
102+ XPutImage (display , window , gc , img , 0 , 0 , 0 , 0 , width , height );
103+ free (buffer );
104+ return EGL_TRUE ;
105+ }
0 commit comments