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;
21 static int flip_old_w, flip_old_h;
23 static int gl_have_error(const char *name)
25 GLenum e = glGetError();
26 if (e != GL_NO_ERROR) {
27 fprintf(stderr, "GL error: %s %x\n", name, e);
33 static int gles_have_error(const char *name)
35 EGLint e = eglGetError();
36 if (e != EGL_SUCCESS) {
37 fprintf(stderr, "%s %x\n", name, e);
43 int gl_init(void *display, void *window, int *quirks, int w, int h)
45 EGLConfig ecfg = NULL;
54 ret = gl_platform_init(&display, &window, quirks);
56 fprintf(stderr, "gl_platform_init failed with %d\n", ret);
60 flip_old_w = flip_old_h = 0;
61 for (tex_w = 1; tex_w < w; tex_w *= 2)
63 for (tex_h = 1; tex_h < h; tex_h *= 2)
65 tex_mem = realloc(tex_mem, tex_w * tex_h * 2);
66 if (tex_mem == NULL) {
67 fprintf(stderr, "OOM\n");
71 edpy = eglGetDisplay((EGLNativeDisplayType)display);
72 if (edpy == EGL_NO_DISPLAY) {
73 fprintf(stderr, "Failed to get EGL display\n");
77 if (!eglInitialize(edpy, NULL, NULL)) {
78 fprintf(stderr, "Failed to initialize EGL\n");
82 if (!eglChooseConfig(edpy, attr, &ecfg, 1, &num_config)) {
83 fprintf(stderr, "Failed to choose config (%x)\n", eglGetError());
87 if (ecfg == NULL || num_config == 0) {
88 fprintf(stderr, "No EGL configs available\n");
92 esfc = eglCreateWindowSurface(edpy, ecfg,
93 (EGLNativeWindowType)window, NULL);
94 if (esfc == EGL_NO_SURFACE) {
95 fprintf(stderr, "Unable to create EGL surface (%x)\n",
100 ectxt = eglCreateContext(edpy, ecfg, EGL_NO_CONTEXT, NULL);
101 if (ectxt == EGL_NO_CONTEXT) {
102 fprintf(stderr, "Unable to create EGL context (%x)\n",
107 eglMakeCurrent(edpy, esfc, esfc, ectxt);
109 glEnable(GL_TEXTURE_2D);
112 glDeleteTextures(1, &texture_name);
114 glGenTextures(1, &texture_name);
116 glBindTexture(GL_TEXTURE_2D, texture_name);
118 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, tex_w, tex_h, 0, GL_RGB,
119 GL_UNSIGNED_SHORT_5_6_5, tex_mem);
120 if (gl_have_error("glTexImage2D"))
124 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
125 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
127 //glViewport(0, 0, 512, 512);
130 glEnable(GL_CULL_FACE);
132 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
133 glEnableClientState(GL_VERTEX_ARRAY);
135 if (gl_have_error("init"))
138 gl_es_display = (void *)edpy;
139 gl_es_surface = (void *)esfc;
145 void gl_announce(void)
147 printf("GL_RENDERER: %s\n", (char *)glGetString(GL_RENDERER));
150 static float vertices[] = {
151 -1.0f, 1.0f, 0.0f, // 0 0 1
152 1.0f, 1.0f, 0.0f, // 1 ^
153 -1.0f, -1.0f, 0.0f, // 2 | 2 3
154 1.0f, -1.0f, 0.0f, // 3 +-->
157 static float texture[] = {
158 0.0f, 0.0f, // we flip this:
164 int gl_flip(const void *fb, int w, int h)
167 if (w != flip_old_w || h != flip_old_h) {
168 float f_w = (float)w / tex_w;
169 float f_h = (float)h / tex_h;
170 texture[1*2 + 0] = f_w;
171 texture[2*2 + 1] = f_h;
172 texture[3*2 + 0] = f_w;
173 texture[3*2 + 1] = f_h;
178 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, w, h,
179 GL_RGB, GL_UNSIGNED_SHORT_5_6_5, fb);
180 if (gl_have_error("glTexSubImage2D"))
184 glVertexPointer(3, GL_FLOAT, 0, vertices);
185 glTexCoordPointer(2, GL_FLOAT, 0, texture);
186 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
188 if (gl_have_error("glDrawArrays"))
191 eglSwapBuffers(edpy, esfc);
192 if (gles_have_error("eglSwapBuffers"))
200 eglMakeCurrent(edpy, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
201 eglDestroyContext(edpy, ectxt);
202 ectxt = EGL_NO_CONTEXT;
203 eglDestroySurface(edpy, esfc);
204 esfc = EGL_NO_SURFACE;
206 edpy = EGL_NO_DISPLAY;
208 gl_es_display = (void *)edpy;
209 gl_es_surface = (void *)esfc;
211 if (tex_mem) free(tex_mem);
214 gl_platform_finish();