8 static EGLDisplay edpy;
9 static EGLSurface esfc;
10 static EGLContext ectxt;
12 static int gl_have_error(const char *name)
14 GLenum e = glGetError();
15 if (e != GL_NO_ERROR) {
16 fprintf(stderr, "GL error: %s %x\n", name, e);
22 static int gles_have_error(const char *name)
24 EGLint e = eglGetError();
25 if (e != EGL_SUCCESS) {
26 fprintf(stderr, "%s %x\n", name, e);
32 int gl_init(void *display, void *window)
34 EGLConfig ecfg = NULL;
35 GLuint texture_name = 0;
36 void *tmp_texture_mem = NULL;
45 tmp_texture_mem = calloc(1, 1024 * 512 * 2);
46 if (tmp_texture_mem == NULL) {
47 fprintf(stderr, "OOM\n");
51 edpy = eglGetDisplay((EGLNativeDisplayType)display);
52 if (edpy == EGL_NO_DISPLAY) {
53 fprintf(stderr, "Failed to get EGL display\n");
57 if (!eglInitialize(edpy, NULL, NULL)) {
58 fprintf(stderr, "Failed to initialize EGL\n");
62 if (!eglChooseConfig(edpy, attr, &ecfg, 1, &num_config)) {
63 fprintf(stderr, "Failed to choose config (%x)\n", eglGetError());
67 if (ecfg == NULL || num_config == 0) {
68 fprintf(stderr, "No EGL configs available\n");
72 esfc = eglCreateWindowSurface(edpy, ecfg,
73 (EGLNativeWindowType)window, NULL);
74 if (esfc == EGL_NO_SURFACE) {
75 fprintf(stderr, "Unable to create EGL surface (%x)\n",
80 ectxt = eglCreateContext(edpy, ecfg, EGL_NO_CONTEXT, NULL);
81 if (ectxt == EGL_NO_CONTEXT) {
82 fprintf(stderr, "Unable to create EGL context (%x)\n",
87 eglMakeCurrent(edpy, esfc, esfc, ectxt);
89 glEnable(GL_TEXTURE_2D);
91 glGenTextures(1, &texture_name);
93 glBindTexture(GL_TEXTURE_2D, texture_name);
95 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 1024, 512, 0, GL_RGB,
96 GL_UNSIGNED_SHORT_5_6_5, tmp_texture_mem);
97 if (gl_have_error("glTexImage2D"))
101 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
102 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
104 //glViewport(0, 0, 512, 512);
107 glEnable(GL_CULL_FACE);
109 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
110 glEnableClientState(GL_VERTEX_ARRAY);
112 if (gl_have_error("init"))
117 free(tmp_texture_mem);
121 static float vertices[] = {
122 -1.0f, 1.0f, 0.0f, // 0 0 1
123 1.0f, 1.0f, 0.0f, // 1 ^
124 -1.0f, -1.0f, 0.0f, // 2 | 2 3
125 1.0f, -1.0f, 0.0f, // 3 +-->
128 static float texture[] = {
129 0.0f, 0.0f, // we flip this:
135 int gl_flip(const void *fb, int w, int h)
137 static int old_w, old_h;
140 if (w != old_w || h != old_h) {
141 float f_w = (float)w / 1024.0f;
142 float f_h = (float)h / 512.0f;
143 texture[1*2 + 0] = f_w;
144 texture[2*2 + 1] = f_h;
145 texture[3*2 + 0] = f_w;
146 texture[3*2 + 1] = f_h;
151 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, w, h,
152 GL_RGB, GL_UNSIGNED_SHORT_5_6_5, fb);
153 if (gl_have_error("glTexSubImage2D"))
157 glVertexPointer(3, GL_FLOAT, 0, vertices);
158 glTexCoordPointer(2, GL_FLOAT, 0, texture);
159 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
161 if (gl_have_error("glDrawArrays"))
164 eglSwapBuffers(edpy, esfc);
165 if (gles_have_error("eglSwapBuffers"))
173 eglMakeCurrent(edpy, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
174 eglDestroyContext(edpy, ectxt);
175 ectxt = EGL_NO_CONTEXT;
176 eglDestroySurface(edpy, esfc);
177 esfc = EGL_NO_SURFACE;
179 edpy = EGL_NO_DISPLAY;