egl: fold _eglError() + return EGL_FALSE
[mesa.git] / src / egl / drivers / haiku / egl_haiku.cpp
index f94a34bc106ac66cb3fe0c94fe082d12b1529b9b..10f3abc070ab1290657f69a67508e59b6cda2100 100644 (file)
 
 #include <errno.h>
 #include <dlfcn.h>
+#include <stdint.h>
 #include <stdio.h>
 
-extern "C" {
-#include "loader.h"
 #include "eglconfig.h"
 #include "eglcontext.h"
 #include "egldisplay.h"
@@ -37,13 +36,19 @@ extern "C" {
 #include "eglsurface.h"
 #include "eglimage.h"
 #include "egltypedefs.h"
-}
 
 #include <InterfaceKit.h>
 #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__)
+#else
+#      define TRACE(x...)
+#      define CALLED()
+#endif
+#define ERROR(x...) printf("egl_haiku: " x)
 
 
 _EGL_DRIVER_STANDARD_TYPECASTS(haiku_egl)
@@ -52,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
@@ -75,81 +76,6 @@ struct haiku_egl_surface
 };
 
 
-/*
-static void
-swrastCreateDrawable(struct dri2_egl_display * dri2_dpy,
-       struct dri2_egl_surface * dri2_surf, int depth)
-{
-
-}
-
-
-static void
-swrastDestroyDrawable(struct dri2_egl_display * dri2_dpy,
-       struct dri2_egl_surface * dri2_surf)
-{
-
-}
-
-
-static void
-swrastGetDrawableInfo(__DRIdrawable * draw, int *x, int *y,
-       int *w, int *h, void *loaderPrivate)
-{
-
-}
-
-
-static void
-swrastPutImage(__DRIdrawable * draw, int op, int x, int y,
-       int w, int h, char *data, void *loaderPrivate)
-{
-
-}
-
-
-static void
-swrastGetImage(__DRIdrawable * read, int x, int y,
-       int w, int h, char *data, void *loaderPrivate)
-{
-
-}
-*/
-
-
-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().
  */
@@ -157,23 +83,34 @@ static _EGLSurface *
 haiku_create_window_surface(_EGLDriver *drv, _EGLDisplay *disp,
        _EGLConfig *conf, void *native_window, const EGLint *attrib_list)
 {
+       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;
 
-       _eglLog(_EGL_DEBUG, "Creating window");
+       TRACE("Creating window\n");
        BWindow* win = (BWindow*)native_window;
 
-       _eglLog(_EGL_DEBUG, "Creating GL view");
+       TRACE("Creating GL view\n");
        surface->gl = new BGLView(win->Bounds(), "OpenGL", B_FOLLOW_ALL_SIDES, 0,
                BGL_RGB | BGL_DOUBLE | BGL_ALPHA);
 
-       _eglLog(_EGL_DEBUG, "Adding GL");
+       TRACE("Adding GL\n");
        win->AddChild(surface->gl);
 
-       _eglLog(_EGL_DEBUG, "Showing window");
+       TRACE("Showing window\n");
        win->Show();
        return &surface->surf;
 }
@@ -198,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;
 }
 
@@ -205,20 +146,23 @@ haiku_destroy_surface(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *surf)
 static EGLBoolean
 haiku_add_configs_for_visuals(_EGLDisplay *dpy)
 {
-       printf("Adding configs\n");
+       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);
-       _eglLog(_EGL_DEBUG,"Config inited\n");
+       TRACE("Config inited\n");
+
        _eglSetConfigKey(&conf->base, EGL_RED_SIZE, 8);
        _eglSetConfigKey(&conf->base, EGL_BLUE_SIZE, 8);
        _eglSetConfigKey(&conf->base, EGL_GREEN_SIZE, 8);
        _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));
@@ -242,76 +186,41 @@ haiku_add_configs_for_visuals(_EGLDisplay *dpy)
        _eglSetConfigKey(&conf->base, EGL_MAX_PBUFFER_PIXELS, 0); // TODO: How to get the right value ?
        _eglSetConfigKey(&conf->base, EGL_SURFACE_TYPE, EGL_WINDOW_BIT /*| EGL_PIXMAP_BIT | EGL_PBUFFER_BIT*/);
 
-       printf("Config configuated\n");
+       TRACE("Config configuated\n");
        if (!_eglValidateConfig(&conf->base, EGL_FALSE)) {
-               _eglLog(_EGL_DEBUG, "Haiku failed to validate config");
-               return EGL_FALSE;
+               _eglLog(_EGL_DEBUG, "Haiku: failed to validate config");
+               goto cleanup;
        }
-       printf("Validated config\n");
-   
+       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;
        }
-       printf("Config successful!\n");
-   
+       TRACE("Config successfull\n");
+
        return EGL_TRUE;
+
+cleanup:
+       free(conf);
+       return EGL_FALSE;
 }
 
+
 extern "C"
 EGLBoolean
 init_haiku(_EGLDriver *drv, _EGLDisplay *dpy)
 {
-       _eglLog(_EGL_DEBUG,"\nInitializing Haiku EGL\n");
-       //_EGLDisplay* egl_dpy;
-
-       printf("Initializing Haiku EGL\n");
-       _eglSetLogProc(haiku_log);
-
-       loader_set_logger(_eglLog);
-
-       /*egl_dpy = (_EGLDisplay*) calloc(1, sizeof(_EGLDisplay));
-       if (!egl_dpy)
-               return _eglError(EGL_BAD_ALLOC, "eglInitialize");
-
-       dpy->DriverData=(void*) egl_dpy;
-       if (!dpy->PlatformDisplay) {
-               // OPEN DEVICE 
-               //dri2_dpy->bwindow = (void*)haiku_create_window();
-               //dri2_dpy->own_device = true;
-       } else {
-               //dri2_dpy->bwindow = (BWindow*)dpy->PlatformDisplay;
-       }*/
-       
-       //dri2_dpy->driver_name = strdup("swrast");
-       //if (!dri2_load_driver_swrast(dpy))
-       //   goto cleanup_conn;
-
-       /*dri2_dpy->swrast_loader_extension.base.name = __DRI_SWRAST_LOADER;
-       dri2_dpy->swrast_loader_extension.base.version = __DRI_SWRAST_LOADER_VERSION;
-       dri2_dpy->swrast_loader_extension.getDrawableInfo = swrastGetDrawableInfo;
-       dri2_dpy->swrast_loader_extension.putImage = swrastPutImage;
-       dri2_dpy->swrast_loader_extension.getImage = swrastGetImage;
-
-       dri2_dpy->extensions[0] = &dri2_dpy->swrast_loader_extension.base;
-       dri2_dpy->extensions[1] = NULL;
-       dri2_dpy->extensions[2] = NULL;*/
-
-       /*if (dri2_dpy->bwindow) {
-               if (!dri2_haiku_add_configs_for_visuals(dri2_dpy, dpy))
-                       goto cleanup_configs;
-       }*/
-       _eglLog(_EGL_DEBUG,"Add configs");
-    haiku_add_configs_for_visuals(dpy);
-
-       dpy->VersionMajor=1;
-       dpy->VersionMinor=4;
-   
-   //dpy->Extensions.KHR_create_context = true;
-
-       //dri2_dpy->vtbl = &dri2_haiku_display_vtbl;
-       _eglLog(_EGL_DEBUG, "Initialization finished");
+       CALLED();
+
+       TRACE("Add configs\n");
+       if (!haiku_add_configs_for_visuals(dpy))
+               return EGL_FALSE;
+
+       dpy->Version = 14;
+
+       TRACE("Initialization finished\n");
 
        return EGL_TRUE;
 }
@@ -330,13 +239,24 @@ _EGLContext*
 haiku_create_context(_EGLDriver *drv, _EGLDisplay *disp, _EGLConfig *conf,
        _EGLContext *share_list, const EGLint *attrib_list)
 {
-       _eglLog(_EGL_DEBUG,"Creating context");
+       CALLED();
+
        struct haiku_egl_context* context;
-       context=(struct haiku_egl_context*)calloc(1,sizeof (*context));
-       if(!_eglInitContext(&context->ctx, disp, conf, attrib_list))
-               printf("ERROR creating context");
-       _eglLog(_EGL_DEBUG, "Context created");
+       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))
+               goto cleanup;
+
+       TRACE("Context created\n");
        return &context->ctx;
+
+cleanup:
+       free(context);
+       return NULL;
 }
 
 
@@ -344,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;
 }
 
@@ -352,13 +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)
 {
-       struct haiku_egl_context* cont=haiku_egl_context(ctx);
-       struct haiku_egl_surface* surf=haiku_egl_surface(dsurf);
+       CALLED();
+
+       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;
@@ -369,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;
@@ -380,7 +312,7 @@ extern "C"
 void
 haiku_unload(_EGLDriver* drv)
 {
-       
+
 }
 
 
@@ -392,9 +324,15 @@ extern "C"
 _EGLDriver*
 _eglBuiltInDriverHaiku(const char *args)
 {
-       _eglLog(_EGL_DEBUG,"Driver loaded");
+       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;
@@ -405,32 +343,13 @@ _eglBuiltInDriverHaiku(const char *args)
        driver->base.API.CreatePixmapSurface = haiku_create_pixmap_surface;
        driver->base.API.CreatePbufferSurface = haiku_create_pbuffer_surface;
        driver->base.API.DestroySurface = haiku_destroy_surface;
-       /*
-       driver->API.GetProcAddress = dri2_get_proc_address;
-       driver->API.WaitClient = dri2_wait_client;
-       driver->API.WaitNative = dri2_wait_native;
-       driver->API.BindTexImage = dri2_bind_tex_image;
-       driver->API.ReleaseTexImage = dri2_release_tex_image;
-       driver->API.SwapInterval = dri2_swap_interval;
-       */
 
        driver->base.API.SwapBuffers = haiku_swap_buffers;
-       /*
-       driver->API.SwapBuffersWithDamageEXT = dri2_swap_buffers_with_damage;
-       driver->API.SwapBuffersRegionNOK = dri2_swap_buffers_region;
-       driver->API.PostSubBufferNV = dri2_post_sub_buffer;
-       driver->API.CopyBuffers = dri2_copy_buffers,
-       driver->API.QueryBufferAge = dri2_query_buffer_age;
-       driver->API.CreateImageKHR = dri2_create_image;
-       driver->API.DestroyImageKHR = dri2_destroy_image_khr;
-       driver->API.CreateWaylandBufferFromImageWL = dri2_create_wayland_buffer_from_image;
-       driver->API.GetSyncValuesCHROMIUM = dri2_get_sync_values_chromium;
-       */
 
        driver->base.Name = "Haiku";
        driver->base.Unload = haiku_unload;
 
-       _eglLog(_EGL_DEBUG, "API Calls defined");
-       
+       TRACE("API Calls defined\n");
+
        return &driver->base;
 }