/
k_eglGetDisplayCopy.c
151 lines (143 loc) · 5.69 KB
/
k_eglGetDisplayCopy.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#include <EGL/egl.h>
#include <EGL/eglext_brcm.h>
#include <GLES2/gl2.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <sys/time.h>
#include <math.h>
#include <X11/Xlib.h>
#include <X11/Xatom.h>
#include <X11/Xutil.h>
/* This file is part of X11EGLRPI
Copyright (C)2014 Mohamed MEDIOUNI
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#ifdef X11EGLRPI_EXPERIMENTAL_DLSYM
static int (*real_eglGetDisplay)(NativeDisplayType) = NULL;
static EGLSurface (*real_eglCreateWindowSurface)(EGLDisplay egldisplay, EGLConfig config, NativeWindowType native_window, EGLint const * attrib_list) = NULL;
static EGLBoolean (*real_eglSwapBuffers)(EGLDisplay edisplay, EGLSurface egsurface) = NULL;
#endif
static int x11_enabled = 0;
Window* root;
Window* window;
EGLDisplay *edisplay;
int width, height,depth;
Display* display;
EGLSurface egl_surface;
XWindowAttributes *window_attributes_return;
EGLDisplay eglGetDisplay(NativeDisplayType native_display) {
#ifdef X11EGLRPI_EXPERIMENTAL_DLSYM
real_eglGetDisplay = dlsym(RTLD_NEXT, "eglGetDisplay");
real_eglCreateWindowSurface = dlsym(RTLD_NEXT,"eglCreateWindowSurface");
real_eglSwapBuffers= dlsym(RTLD_NEXT,"eglSwapBuffers");
#endif
if ( native_display == EGL_DEFAULT_DISPLAY) {
x11_enabled=0;
return real_eglGetDisplay(native_display);
}
x11_enabled=1;
puts("Getting an X11 EGL Display.");
bcm_host_init();
display = (Display*)native_display;
root = DefaultRootWindow(display);
edisplay = real_eglGetDisplay(EGL_DEFAULT_DISPLAY);
return edisplay;
}
EGLSurface eglCreateWindowSurface(EGLDisplay egldisplay, EGLConfig config, NativeWindowType native_window, EGLint const * attrib_list) {
if (x11_enabled == 0) {
return real_eglCreateWindowSurface(egldisplay,config,native_window,attrib_list);
}
window = (Window*) native_window;
puts("Getting window information...");
XWindowAttributes window_attributes;
XGetWindowAttributes(display,window,&window_attributes);
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);
width=window_attributes.width ;
height=window_attributes.height;
depth=window_attributes.depth;
EGLint attr[] = { // some attributes to set up our egl-interface
EGL_RED_SIZE, 8,
EGL_GREEN_SIZE, 8,
EGL_BLUE_SIZE, 8,
EGL_ALPHA_SIZE, 8,
EGL_SURFACE_TYPE,
EGL_PIXMAP_BIT | EGL_OPENGL_ES2_BIT,
EGL_NONE
};
EGLConfig ecfg;
EGLint num_config;
if (!eglChooseConfig(edisplay, attr, &ecfg, 1, &num_config)) {
fprintf(stderr, "Failed to choose config (eglError: %s)\n");
return EGL_NO_SURFACE;
}
EGLint pixel_format = EGL_PIXEL_FORMAT_ARGB_8888_BRCM;
EGLint rt;
eglGetConfigAttrib(edisplay, ecfg, EGL_RENDERABLE_TYPE, &rt);
if (rt & EGL_OPENGL_ES_BIT) {
pixel_format |= EGL_PIXEL_FORMAT_RENDER_GLES_BRCM;
pixel_format |= EGL_PIXEL_FORMAT_GLES_TEXTURE_BRCM;
}
if (rt & EGL_OPENGL_ES2_BIT) {
pixel_format |= EGL_PIXEL_FORMAT_RENDER_GLES2_BRCM;
pixel_format |= EGL_PIXEL_FORMAT_GLES2_TEXTURE_BRCM;
}
if (rt & EGL_OPENVG_BIT) {
pixel_format |= EGL_PIXEL_FORMAT_RENDER_VG_BRCM;
pixel_format |= EGL_PIXEL_FORMAT_VG_IMAGE_BRCM;
}
if (rt & EGL_OPENGL_BIT) {
pixel_format |= EGL_PIXEL_FORMAT_RENDER_GL_BRCM;
}
EGLint pixmap[5];
pixmap[0] = 0;
pixmap[1] = 0;
pixmap[2] = width;
pixmap[3] = height;
pixmap[4] = pixel_format;
eglCreateGlobalImageBRCM(width, height, pixmap[4], 0, width*4, pixmap);
egl_surface = eglCreatePixmapSurface(edisplay, ecfg, pixmap, 0);
puts("EGL Surface Created");
EGLint ctxattr[] = {
EGL_CONTEXT_CLIENT_VERSION, 2,
EGL_NONE
};
EGLContext context = eglCreateContext ( edisplay, ecfg, EGL_NO_CONTEXT, ctxattr );
real_eglMakeCurrent(edisplay, egl_surface, egl_surface, context);
return egl_surface;
}
EGLBoolean eglSwapBuffers(EGLDisplay edisplay, EGLSurface egsurface) {
if (x11_enabled == 0) {
return real_eglSwapBuffers(edisplay,egsurface);
}
unsigned int *buffer = (unsigned int *)malloc(height * width * 4);
glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
static GC gc;
static int status =0;
static XImage *image =0;
static Pixmap pixmap;
static XGCValues gcvalues;
if (status == 0 ) {
gc = XCreateGC(display, window,0,&gcvalues);
image = XGetImage(display, window, 0, 0, width, height, AllPlanes, ZPixmap);
status = 1;
}
XImage *img = XCreateImage(display,CopyFromParent,depth,ZPixmap,0,buffer,width, height, 32, 0);
XPutImage(display, window, gc, img, 0, 0, 0, 0, width, height);
free(buffer);
return EGL_TRUE;
}
EGLBoolean eglMakeCurrent(EGLDisplay display, EGLSurface draw, EGLSurface read, EGLContext context) {
real_eglMakeCurrent(display,draw,read,context);
return EGL_TRUE;
}