gl: clear w, h on reinit
[libpicofe.git] / gl.c
diff --git a/gl.c b/gl.c
index 2360a0c..c5c5050 100644 (file)
--- a/gl.c
+++ b/gl.c
@@ -3,12 +3,23 @@
 
 #include <EGL/egl.h>
 #include <GLES/gl.h>
+#include "gl_platform.h"
 #include "gl.h"
 
 static EGLDisplay edpy;
 static EGLSurface esfc;
 static EGLContext ectxt;
 
+static GLuint texture_name;
+
+/* for external flips */
+void *gl_es_display;
+void *gl_es_surface;
+
+static int tex_w, tex_h;
+static void *tex_mem;
+static int flip_old_w, flip_old_h;
+
 static int gl_have_error(const char *name)
 {
        GLenum e = glGetError();
@@ -29,11 +40,9 @@ static int gles_have_error(const char *name)
        return 0;
 }
 
-int gl_init(void *display, void *window)
+int gl_init(void *display, void *window, int *quirks, int w, int h)
 {
        EGLConfig ecfg = NULL;
-       GLuint texture_name = 0;
-       void *tmp_texture_mem = NULL;
        EGLint num_config;
        int retval = -1;
        int ret;
@@ -42,8 +51,19 @@ int gl_init(void *display, void *window)
                EGL_NONE
        };
 
-       tmp_texture_mem = calloc(1, 1024 * 512 * 2);
-       if (tmp_texture_mem == NULL) {
+       ret = gl_platform_init(&display, &window, quirks);
+       if (ret != 0) {
+               fprintf(stderr, "gl_platform_init failed with %d\n", ret);
+               goto out;
+       }
+
+       flip_old_w = flip_old_h = 0;
+       for (tex_w = 1; tex_w < w; tex_w *= 2)
+               ;
+       for (tex_h = 1; tex_h < h; tex_h *= 2)
+               ;
+       tex_mem = realloc(tex_mem, tex_w * tex_h * 2);
+       if (tex_mem == NULL) {
                fprintf(stderr, "OOM\n");
                goto out;
        }
@@ -88,12 +108,15 @@ int gl_init(void *display, void *window)
 
        glEnable(GL_TEXTURE_2D);
 
+       if (texture_name)
+               glDeleteTextures(1, &texture_name);
+
        glGenTextures(1, &texture_name);
 
        glBindTexture(GL_TEXTURE_2D, texture_name);
 
-       glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 1024, 512, 0, GL_RGB,
-               GL_UNSIGNED_SHORT_5_6_5, tmp_texture_mem);
+       glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, tex_w, tex_h, 0, GL_RGB,
+               GL_UNSIGNED_SHORT_5_6_5, tex_mem);
        if (gl_have_error("glTexImage2D"))
                goto out;
 
@@ -112,12 +135,18 @@ int gl_init(void *display, void *window)
        if (gl_have_error("init"))
                goto out;
 
+       gl_es_display = (void *)edpy;
+       gl_es_surface = (void *)esfc;
        retval = 0;
 out:
-       free(tmp_texture_mem);
        return retval;
 }
 
+void gl_announce(void)
+{
+       printf("GL_RENDERER: %s\n", (char *)glGetString(GL_RENDERER));
+}
+
 static float vertices[] = {
        -1.0f,  1.0f,  0.0f, // 0    0  1
         1.0f,  1.0f,  0.0f, // 1  ^
@@ -134,24 +163,24 @@ static float texture[] = {
 
 int gl_flip(const void *fb, int w, int h)
 {
-       static int old_w, old_h;
-
-       if (w != old_w || h != old_h) {
-               float f_w = (float)w / 1024.0f;
-               float f_h = (float)h / 512.0f;
-               texture[1*2 + 0] = f_w;
-               texture[2*2 + 1] = f_h;
-               texture[3*2 + 0] = f_w;
-               texture[3*2 + 1] = f_h;
-               old_w = w;
-               old_h = h;
+       if (fb != NULL) {
+               if (w != flip_old_w || h != flip_old_h) {
+                       float f_w = (float)w / tex_w;
+                       float f_h = (float)h / tex_h;
+                       texture[1*2 + 0] = f_w;
+                       texture[2*2 + 1] = f_h;
+                       texture[3*2 + 0] = f_w;
+                       texture[3*2 + 1] = f_h;
+                       flip_old_w = w;
+                       flip_old_h = h;
+               }
+
+               glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, w, h,
+                       GL_RGB, GL_UNSIGNED_SHORT_5_6_5, fb);
+               if (gl_have_error("glTexSubImage2D"))
+                       return -1;
        }
 
-       glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, w, h,
-               GL_RGB, GL_UNSIGNED_SHORT_5_6_5, fb);
-       if (gl_have_error("glTexSubImage2D"))
-               return -1;
-
        glVertexPointer(3, GL_FLOAT, 0, vertices);
        glTexCoordPointer(2, GL_FLOAT, 0, texture);
        glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
@@ -175,4 +204,12 @@ void gl_finish(void)
        esfc = EGL_NO_SURFACE;
        eglTerminate(edpy);
        edpy = EGL_NO_DISPLAY;
+
+       gl_es_display = (void *)edpy;
+       gl_es_surface = (void *)esfc;
+
+       if (tex_mem) free(tex_mem);
+       tex_mem = NULL;
+
+       gl_platform_finish();
 }