egl: fold _eglError() + return EGL_FALSE
[mesa.git] / src / egl / drivers / haiku / egl_haiku.cpp
index 36e1277a2e0fe74a6a0ef19163181ad2fc236ce4..10f3abc070ab1290657f69a67508e59b6cda2100 100644 (file)
@@ -27,7 +27,6 @@
 #include <stdint.h>
 #include <stdio.h>
 
-#include "loader.h"
 #include "eglconfig.h"
 #include "eglcontext.h"
 #include "egldisplay.h"
@@ -42,8 +41,6 @@
 #include <OpenGLKit.h>
 
 
-#define CALLOC_STRUCT(T)   (struct T *) calloc(1, sizeof(struct T))
-
 #ifdef DEBUG
 #      define TRACE(x...) printf("egl_haiku: " x)
 #      define CALLED() TRACE("CALLED: %s\n", __PRETTY_FUNCTION__)
@@ -60,10 +57,6 @@ _EGL_DRIVER_STANDARD_TYPECASTS(haiku_egl)
 struct haiku_egl_driver
 {
        _EGLDriver base;
-
-       void *handle;
-       _EGLProc (*get_proc_address)(const char *procname);
-       void (*glFlush)(void);
 };
 
 struct haiku_egl_config
@@ -83,39 +76,6 @@ struct haiku_egl_surface
 };
 
 
-static void
-haiku_log(EGLint level, const char *msg)
-{
-       switch (level) {
-               case _EGL_DEBUG:
-                       fprintf(stderr,"%s", msg);
-                       break;
-               case _EGL_INFO:
-                       fprintf(stderr,"%s", msg);
-                       break;
-               case _EGL_WARNING:
-                       fprintf(stderr,"%s", msg);
-                       break;
-               case _EGL_FATAL:
-                       fprintf(stderr,"%s", msg);
-                       break;
-               default:
-                       break;
-       }
-}
-
-
-/**
- * Called via eglCreateWindowSurface(), drv->API.CreateWindowSurface().
- */
-static _EGLSurface *
-haiku_create_surface(_EGLDriver *drv, _EGLDisplay *disp, EGLint type,
-       _EGLConfig *conf, void *native_surface, const EGLint *attrib_list)
-{
-       return NULL;
-}
-
-
 /**
  * Called via eglCreateWindowSurface(), drv->API.CreateWindowSurface().
  */
@@ -126,9 +86,18 @@ haiku_create_window_surface(_EGLDriver *drv, _EGLDisplay *disp,
        CALLED();
 
        struct haiku_egl_surface* surface;
-       surface = (struct haiku_egl_surface*)calloc(1,sizeof (*surface));
+       surface = (struct haiku_egl_surface*) calloc(1, sizeof (*surface));
+       if (!surface) {
+               _eglError(EGL_BAD_ALLOC, "haiku_create_window_surface");
+               return NULL;
+       }
+
+       if (!_eglInitSurface(&surface->surf, disp, EGL_WINDOW_BIT,
+               conf, attrib_list)) {
+               free(surface);
+               return NULL;
+       }
 
-       _eglInitSurface(&surface->surf, disp, EGL_WINDOW_BIT, conf, attrib_list);
        (&surface->surf)->SwapInterval = 1;
 
        TRACE("Creating window\n");
@@ -166,6 +135,10 @@ haiku_create_pbuffer_surface(_EGLDriver *drv, _EGLDisplay *disp,
 static EGLBoolean
 haiku_destroy_surface(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *surf)
 {
+       if (_eglPutSurface(surf)) {
+               // XXX: detach haiku_egl_surface::gl from the native window and destroy it
+               free(surf);
+       }
        return EGL_TRUE;
 }
 
@@ -176,7 +149,9 @@ haiku_add_configs_for_visuals(_EGLDisplay *dpy)
        CALLED();
 
        struct haiku_egl_config* conf;
-       conf = CALLOC_STRUCT(haiku_egl_config);
+       conf = (struct haiku_egl_config*) calloc(1, sizeof (*conf));
+       if (!conf)
+               return _eglError(EGL_BAD_ALLOC, "haiku_add_configs_for_visuals");
 
        _eglInitConfig(&conf->base, dpy, 1);
        TRACE("Config inited\n");
@@ -187,7 +162,7 @@ haiku_add_configs_for_visuals(_EGLDisplay *dpy)
        _eglSetConfigKey(&conf->base, EGL_LUMINANCE_SIZE, 0);
        _eglSetConfigKey(&conf->base, EGL_ALPHA_SIZE, 8);
        _eglSetConfigKey(&conf->base, EGL_COLOR_BUFFER_TYPE, EGL_RGB_BUFFER);
-       EGLint r = (_eglGetConfigKey(&conf->base, EGL_RED_SIZE) 
+       EGLint r = (_eglGetConfigKey(&conf->base, EGL_RED_SIZE)
                + _eglGetConfigKey(&conf->base, EGL_GREEN_SIZE)
                + _eglGetConfigKey(&conf->base, EGL_BLUE_SIZE)
                + _eglGetConfigKey(&conf->base, EGL_ALPHA_SIZE));
@@ -214,35 +189,37 @@ haiku_add_configs_for_visuals(_EGLDisplay *dpy)
        TRACE("Config configuated\n");
        if (!_eglValidateConfig(&conf->base, EGL_FALSE)) {
                _eglLog(_EGL_DEBUG, "Haiku: failed to validate config");
-               return EGL_FALSE;
+               goto cleanup;
        }
        TRACE("Validated config\n");
-   
+
        _eglLinkConfig(&conf->base);
        if (!_eglGetArraySize(dpy->Configs)) {
                _eglLog(_EGL_WARNING, "Haiku: failed to create any config");
-               return EGL_FALSE;
+               goto cleanup;
        }
        TRACE("Config successfull\n");
-   
+
        return EGL_TRUE;
+
+cleanup:
+       free(conf);
+       return EGL_FALSE;
 }
 
+
 extern "C"
 EGLBoolean
 init_haiku(_EGLDriver *drv, _EGLDisplay *dpy)
 {
        CALLED();
 
-       _eglSetLogProc(haiku_log);
-
-       loader_set_logger(_eglLog);
-
        TRACE("Add configs\n");
-       haiku_add_configs_for_visuals(dpy);
+       if (!haiku_add_configs_for_visuals(dpy))
+               return EGL_FALSE;
 
        dpy->Version = 14;
-   
+
        TRACE("Initialization finished\n");
 
        return EGL_TRUE;
@@ -265,13 +242,21 @@ haiku_create_context(_EGLDriver *drv, _EGLDisplay *disp, _EGLConfig *conf,
        CALLED();
 
        struct haiku_egl_context* context;
-       context=(struct haiku_egl_context*)calloc(1,sizeof (*context));
+       context = (struct haiku_egl_context*) calloc(1, sizeof (*context));
+       if (!context) {
+               _eglError(EGL_BAD_ALLOC, "haiku_create_context");
+               return NULL;
+       }
 
        if (!_eglInitContext(&context->ctx, disp, conf, attrib_list))
-               ERROR("ERROR creating context");
+               goto cleanup;
 
        TRACE("Context created\n");
        return &context->ctx;
+
+cleanup:
+       free(context);
+       return NULL;
 }
 
 
@@ -279,7 +264,13 @@ extern "C"
 EGLBoolean
 haiku_destroy_context(_EGLDriver* drv, _EGLDisplay *disp, _EGLContext* ctx)
 {
-       ctx=NULL;
+       struct haiku_egl_context* context = haiku_egl_context(ctx);
+
+       if (_eglPutContext(ctx)) {
+               // XXX: teardown the context ?
+               free(context);
+               ctx = NULL;
+       }
        return EGL_TRUE;
 }
 
@@ -287,15 +278,18 @@ haiku_destroy_context(_EGLDriver* drv, _EGLDisplay *disp, _EGLContext* ctx)
 extern "C"
 EGLBoolean
 haiku_make_current(_EGLDriver* drv, _EGLDisplay* dpy, _EGLSurface *dsurf,
-                 _EGLSurface *rsurf, _EGLContext *ctx)
+       _EGLSurface *rsurf, _EGLContext *ctx)
 {
        CALLED();
 
-       struct haiku_egl_context* cont=haiku_egl_context(ctx);
-       struct haiku_egl_surface* surf=haiku_egl_surface(dsurf);
+       struct haiku_egl_context* cont = haiku_egl_context(ctx);
+       struct haiku_egl_surface* surf = haiku_egl_surface(dsurf);
        _EGLContext *old_ctx;
-    _EGLSurface *old_dsurf, *old_rsurf;
-       _eglBindContext(ctx, dsurf, rsurf, &old_ctx, &old_dsurf, &old_rsurf);
+       _EGLSurface *old_dsurf, *old_rsurf;
+
+       if (!_eglBindContext(ctx, dsurf, rsurf, &old_ctx, &old_dsurf, &old_rsurf))
+               return EGL_FALSE;
+
        //cont->ctx.DrawSurface=&surf->surf;
        surf->gl->LockGL();
        return EGL_TRUE;
@@ -306,7 +300,8 @@ extern "C"
 EGLBoolean
 haiku_swap_buffers(_EGLDriver *drv, _EGLDisplay *dpy, _EGLSurface *surf)
 {
-       struct haiku_egl_surface* surface=haiku_egl_surface(surf);
+       struct haiku_egl_surface* surface = haiku_egl_surface(surf);
+
        surface->gl->SwapBuffers();
        //gl->Render();
        return EGL_TRUE;
@@ -317,7 +312,7 @@ extern "C"
 void
 haiku_unload(_EGLDriver* drv)
 {
-       
+
 }
 
 
@@ -332,7 +327,12 @@ _eglBuiltInDriverHaiku(const char *args)
        CALLED();
 
        struct haiku_egl_driver* driver;
-       driver=(struct haiku_egl_driver*)calloc(1,sizeof(*driver));
+       driver = (struct haiku_egl_driver*) calloc(1, sizeof(*driver));
+       if (!driver) {
+               _eglError(EGL_BAD_ALLOC, "_eglBuiltInDriverHaiku");
+               return NULL;
+       }
+
        _eglInitDriverFallbacks(&driver->base);
        driver->base.API.Initialize = init_haiku;
        driver->base.API.Terminate = haiku_terminate;