6 #include "gl_platform.h"
9 static EGLDisplay edpy;
10 static EGLSurface esfc;
11 static EGLContext ectxt;
13 static GLuint texture_name;
15 /* for external flips */
19 static int tex_w, tex_h;
22 static int gl_have_error(const char *name)
24 GLenum e = glGetError();
25 if (e != GL_NO_ERROR) {
26 fprintf(stderr, "GL error: %s %x\n", name, e);
32 static int gles_have_error(const char *name)
34 EGLint e = eglGetError();
35 if (e != EGL_SUCCESS) {
36 fprintf(stderr, "%s %x\n", name, e);
42 int gl_init(void *display, void *window, int *quirks, int w, int h)
44 EGLConfig ecfg = NULL;
53 ret = gl_platform_init(&display, &window, quirks);
55 fprintf(stderr, "gl_platform_init failed with %d\n", ret);
59 for (tex_w = 1; tex_w < w; tex_w *= 2);
60 for (tex_h = 1; tex_h < h; tex_h *= 2);
61 tex_mem = realloc(tex_mem, tex_w * tex_h * 2);
62 if (tex_mem == NULL) {
63 fprintf(stderr, "OOM\n");
67 edpy = eglGetDisplay((EGLNativeDisplayType)display);
68 if (edpy == EGL_NO_DISPLAY) {
69 fprintf(stderr, "Failed to get EGL display\n");
73 if (!eglInitialize(edpy, NULL, NULL)) {
74 fprintf(stderr, "Failed to initialize EGL\n");
78 if (!eglChooseConfig(edpy, attr, &ecfg, 1, &num_config)) {
79 fprintf(stderr, "Failed to choose config (%x)\n", eglGetError());
83 if (ecfg == NULL || num_config == 0) {
84 fprintf(stderr, "No EGL configs available\n");
88 esfc = eglCreateWindowSurface(edpy, ecfg,
89 (EGLNativeWindowType)window, NULL);
90 if (esfc == EGL_NO_SURFACE) {
91 fprintf(stderr, "Unable to create EGL surface (%x)\n",
96 ectxt = eglCreateContext(edpy, ecfg, EGL_NO_CONTEXT, NULL);
97 if (ectxt == EGL_NO_CONTEXT) {
98 fprintf(stderr, "Unable to create EGL context (%x)\n",
103 eglMakeCurrent(edpy, esfc, esfc, ectxt);
105 glEnable(GL_TEXTURE_2D);
108 glDeleteTextures(1, &texture_name);
110 glGenTextures(1, &texture_name);
112 glBindTexture(GL_TEXTURE_2D, texture_name);
114 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, tex_w, tex_h, 0, GL_RGB,
115 GL_UNSIGNED_SHORT_5_6_5, tex_mem);
116 if (gl_have_error("glTexImage2D"))
120 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
121 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
123 //glViewport(0, 0, 512, 512);
126 glEnable(GL_CULL_FACE);
128 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
129 glEnableClientState(GL_VERTEX_ARRAY);
131 if (gl_have_error("init"))
134 gl_es_display = (void *)edpy;
135 gl_es_surface = (void *)esfc;
141 void gl_announce(void)
143 printf("GL_RENDERER: %s\n", (char *)glGetString(GL_RENDERER));
146 static float vertices[] = {
147 -1.0f, 1.0f, 0.0f, // 0 0 1
148 1.0f, 1.0f, 0.0f, // 1 ^
149 -1.0f, -1.0f, 0.0f, // 2 | 2 3
150 1.0f, -1.0f, 0.0f, // 3 +-->
153 static float texture[] = {
154 0.0f, 0.0f, // we flip this:
160 int gl_flip(const void *fb, int w, int h)
162 static int old_w, old_h;
165 if (w != old_w || h != old_h) {
166 float f_w = (float)w / tex_w;
167 float f_h = (float)h / tex_h;
168 texture[1*2 + 0] = f_w;
169 texture[2*2 + 1] = f_h;
170 texture[3*2 + 0] = f_w;
171 texture[3*2 + 1] = f_h;
176 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, w, h,
177 GL_RGB, GL_UNSIGNED_SHORT_5_6_5, fb);
178 if (gl_have_error("glTexSubImage2D"))
182 glVertexPointer(3, GL_FLOAT, 0, vertices);
183 glTexCoordPointer(2, GL_FLOAT, 0, texture);
184 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
186 if (gl_have_error("glDrawArrays"))
189 eglSwapBuffers(edpy, esfc);
190 if (gles_have_error("eglSwapBuffers"))
198 eglMakeCurrent(edpy, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
199 eglDestroyContext(edpy, ectxt);
200 ectxt = EGL_NO_CONTEXT;
201 eglDestroySurface(edpy, esfc);
202 esfc = EGL_NO_SURFACE;
204 edpy = EGL_NO_DISPLAY;
206 gl_es_display = (void *)edpy;
207 gl_es_surface = (void *)esfc;
209 if (tex_mem) free(tex_mem);
212 gl_platform_finish();