[waffle] [PATCH 04/10] nacl: rework nacl_dl functions

Emil Velikov emil.l.velikov at gmail.com
Tue Mar 24 08:56:20 PDT 2015


This is a direct copy from cgl with the following tweaks
 - s/cgl/nacl/
 - s/cgl_dl_gl_path/NACL_GLES2_LIBRARY/
 - Add move the nacl_prefix function from nacl_platform.c

Doing this allows us to have more consistent codebase and additional
error messages for the user. As a bonus it squashes a bug where
waffle_dl_can_open() was setting an error when it shouldn't have.

Signed-off-by: Emil Velikov <emil.l.velikov at gmail.com>
---
 src/waffle/CMakeLists.txt       |   1 +
 src/waffle/nacl/nacl_dl.c       | 188 ++++++++++++++++++++++++++++++++++++++++
 src/waffle/nacl/nacl_dl.h       |  43 +++++++++
 src/waffle/nacl/nacl_platform.c |  84 +-----------------
 4 files changed, 236 insertions(+), 80 deletions(-)
 create mode 100644 src/waffle/nacl/nacl_dl.c
 create mode 100644 src/waffle/nacl/nacl_dl.h

diff --git a/src/waffle/CMakeLists.txt b/src/waffle/CMakeLists.txt
index ac9b415..758039c 100644
--- a/src/waffle/CMakeLists.txt
+++ b/src/waffle/CMakeLists.txt
@@ -186,6 +186,7 @@ if(waffle_has_nacl)
         nacl/nacl_config.c
         nacl/nacl_context.c
         nacl/nacl_display.c
+        nacl/nacl_dl.c
         nacl/nacl_platform.c
         nacl/nacl_window.c
         )
diff --git a/src/waffle/nacl/nacl_dl.c b/src/waffle/nacl/nacl_dl.c
new file mode 100644
index 0000000..63e3aaf
--- /dev/null
+++ b/src/waffle/nacl/nacl_dl.c
@@ -0,0 +1,188 @@
+// Copyright 2012-2015 Intel Corporation
+//
+// All rights reserved.
+//
+// Redistribution and use in source and binary forms, with or without
+// modification, are permitted provided that the following conditions are met:
+//
+// - Redistributions of source code must retain the above copyright notice, this
+//   list of conditions and the following disclaimer.
+//
+// - Redistributions in binary form must reproduce the above copyright notice,
+//   this list of conditions and the following disclaimer in the documentation
+//   and/or other materials provided with the distribution.
+//
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
+// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+#include <assert.h>
+#include <dlfcn.h>
+
+#include "wcore_error.h"
+
+#include "nacl_dl.h"
+#include "nacl_platform.h"
+
+
+static bool
+nacl_dl_check_enum(int32_t waffle_dl)
+{
+    switch (waffle_dl) {
+        case WAFFLE_DL_OPENGL:
+            wcore_errorf(WAFFLE_ERROR_UNSUPPORTED_ON_PLATFORM,
+                         "NACL does not support OpenGL");
+            return false;
+        case WAFFLE_DL_OPENGL_ES1:
+            wcore_errorf(WAFFLE_ERROR_UNSUPPORTED_ON_PLATFORM,
+                         "NACL does not support OpenGL ES1");
+            return false;
+        case WAFFLE_DL_OPENGL_ES2:
+            return true;
+        case WAFFLE_DL_OPENGL_ES3:
+            wcore_errorf(WAFFLE_ERROR_UNSUPPORTED_ON_PLATFORM,
+                         "NACL does not support OpenGL ES3");
+            return false;
+        default:
+            assert(false);
+            return false;
+   }
+}
+
+static bool
+nacl_dl_open(struct nacl_platform *plat)
+{
+    plat->dl_gl = dlopen(NACL_GLES2_LIBRARY, RTLD_LAZY);
+
+    if (!plat->dl_gl) {
+        wcore_errorf(WAFFLE_ERROR_UNKNOWN,
+                     "dlopen(\"%s\") failed", NACL_GLES2_LIBRARY);
+        return false;
+    }
+
+    return true;
+}
+
+bool
+nacl_dl_can_open(struct wcore_platform *wc_plat,
+                 int32_t waffle_dl)
+{
+    struct nacl_platform *plat = nacl_platform(wc_plat);
+    bool ok;
+
+    WCORE_ERROR_DISABLED({
+        ok = nacl_dl_check_enum(waffle_dl);
+    });
+
+    if (!ok)
+        return false;
+
+    if (plat->dl_gl != NULL)
+        return true;
+
+    WCORE_ERROR_DISABLED({
+        nacl_dl_open(plat);
+    });
+
+    return plat->dl_gl != NULL;
+}
+
+// Construct a string that maps GL function to NaCl function
+// by concating given prefix and function name tail from 'src'.
+static char *
+nacl_dl_prefix(const char *src, const char *prefix)
+{
+    if (strncmp(src, "gl", 2) != 0)
+        return NULL;
+
+    uint32_t len = strlen(src) + strlen(prefix);
+
+    char *dst = wcore_calloc(len);
+    if (!dst)
+        return NULL;
+
+    snprintf(dst, len, "%s%s", prefix, src + 2);
+
+    return dst;
+}
+
+void*
+nacl_dl_sym(struct wcore_platform *wc_plat,
+            int32_t waffle_dl,
+            const char *name)
+{
+    struct nacl_platform *plat = nacl_platform(wc_plat);
+
+    if (!nacl_dl_check_enum(waffle_dl))
+        return NULL;
+
+    if (plat->dl_gl == NULL)
+        nacl_dl_open(plat);
+
+    if (plat->dl_gl == NULL)
+        return NULL;
+
+    char *nacl_name = nacl_dl_prefix(name, "GLES2");
+    if (!nacl_name)
+        return NULL;
+
+    // Clear any previous error.
+    dlerror();
+
+    void *sym = dlsym(plat->dl_gl, name);
+
+    if (sym) {
+        free(nacl_name);
+        return sym;
+    }
+
+    // dlsym returned NULL. Check if an error occured.
+    const char *error = dlerror();
+    if (error) {
+        wcore_errorf(WAFFLE_ERROR_UNKNOWN,
+                     "dlsym(libname=\"%s\", symbol=\"%s\") failed: %s",
+                     NACL_GLES2_LIBRARY, nacl_name, error);
+    }
+    free(nacl_name);
+
+    return NULL;
+}
+
+bool
+nacl_dl_close(struct wcore_platform *wc_plat)
+{
+    struct nacl_platform *plat = nacl_platform(wc_plat);
+
+    int error_code = 0;
+    const char *error_msg = NULL;
+
+    if (!plat->dl_gl)
+        return true;
+
+    error_code = dlclose(plat->dl_gl);
+
+    if (!error_code)
+        return true;
+
+    error_msg = dlerror();
+
+    if (error_msg) {
+        wcore_errorf(WAFFLE_ERROR_UNKNOWN,
+                     "dlclose(libname=\"%s\") failed: %s",
+                     nacl_dl_gl_path, error_msg);
+    }
+    else {
+        wcore_errorf(WAFFLE_ERROR_UNKNOWN,
+                     "dlclose(libname=\"%s\") failed",
+                     nacl_dl_gl_path);
+    }
+
+    return false;
+}
diff --git a/src/waffle/nacl/nacl_dl.h b/src/waffle/nacl/nacl_dl.h
new file mode 100644
index 0000000..e7748f3
--- /dev/null
+++ b/src/waffle/nacl/nacl_dl.h
@@ -0,0 +1,43 @@
+// Copyright 2012-2015 Intel Corporation
+//
+// All rights reserved.
+//
+// Redistribution and use in source and binary forms, with or without
+// modification, are permitted provided that the following conditions are met:
+//
+// - Redistributions of source code must retain the above copyright notice, this
+//   list of conditions and the following disclaimer.
+//
+// - Redistributions in binary form must reproduce the above copyright notice,
+//   this list of conditions and the following disclaimer in the documentation
+//   and/or other materials provided with the distribution.
+//
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
+// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+#pragma once
+
+#include <stdbool.h>
+#include <stdint.h>
+
+struct wcore_platform;
+
+bool
+nacl_dl_can_open(struct wcore_platform *wc_plat,
+                 int32_t waffle_dl);
+
+void*
+nacl_dl_sym(struct wcore_platform *wc_plat,
+            int32_t waffle_dl,
+            const char *name);
+
+bool
+nacl_dl_close(struct wcore_platform *wc_plat);
diff --git a/src/waffle/nacl/nacl_platform.c b/src/waffle/nacl/nacl_platform.c
index 22169da..ffc8e09 100644
--- a/src/waffle/nacl/nacl_platform.c
+++ b/src/waffle/nacl/nacl_platform.c
@@ -26,6 +26,7 @@
 #include <dlfcn.h>
 #include <stdio.h>
 
+#include "nacl_dl.h"
 #include "nacl_platform.h"
 
 static const struct wcore_platform_vtbl nacl_platform_vtbl;
@@ -44,90 +45,13 @@ nacl_platform_destroy(struct wcore_platform *wc_self)
     nacl_teardown(self->nacl);
 
     if (self->gl_dl)
-        if (dlclose(self->gl_dl) != 0)
-            wcore_errorf(WAFFLE_ERROR_UNKNOWN, "dlclose failed: %s",
-                         dlerror());
+        ok &= nacl_dl_close(&self->wcore);
 
     free(self);
     return ok;
 }
 
 static bool
-nacl_platform_dl_can_open(struct wcore_platform *wc_self,
-                          int32_t waffle_dl)
-{
-    struct nacl_platform *self = nacl_platform(wc_self);
-
-    switch (waffle_dl) {
-        case WAFFLE_DL_OPENGL_ES2:
-            if (!self->gl_dl)
-                self->gl_dl = dlopen(NACL_GLES2_LIBRARY, RTLD_LAZY);
-            break;
-        // API not supported
-        default:
-            return false;
-    }
-
-    if (!self->gl_dl)
-        wcore_errorf(WAFFLE_ERROR_UNKNOWN, "dlopen failed: %s", dlerror());
-
-    return self->gl_dl ? true : false;
-}
-
-// Construct a string that maps GL function to NaCl function
-// by concating given prefix and function name tail from 'src'.
-static char *
-nacl_prefix(const char *src, const char *prefix)
-{
-    if (strncmp(src, "gl", 2) != 0)
-        return NULL;
-
-    uint32_t len = strlen(src) + strlen(prefix);
-
-    char *dst = wcore_calloc(len);
-    if (!dst)
-        return NULL;
-
-    snprintf(dst, len, "%s%s", prefix, src + 2);
-
-    return dst;
-}
-
-static void*
-nacl_platform_dl_sym(struct wcore_platform *wc_self,
-                     int32_t waffle_dl,
-                     const char *name)
-{
-    struct nacl_platform *self = nacl_platform(wc_self);
-    char *nacl_name = NULL;
-    void *func = NULL;
-
-    if (!self->gl_dl)
-        if (!nacl_platform_dl_can_open(wc_self, waffle_dl))
-            return false;
-
-    nacl_name = nacl_prefix(name, "GLES2");
-
-    if (!nacl_name)
-        return NULL;
-
-    func = dlsym(self->gl_dl, nacl_name);
-
-    if (!func) {
-        const char *error = dlerror();
-        if (error) {
-            wcore_errorf(WAFFLE_ERROR_UNKNOWN,
-                         "dlsym(libname=\"%s\", symbol=\"%s\") failed: %s",
-                         NACL_GLES2_LIBRARY, nacl_name, error);
-        }
-    }
-
-    free(nacl_name);
-
-    return func;
-}
-
-static bool
 nacl_platform_make_current(struct wcore_platform *wc_self,
                            struct wcore_display *wc_dpy,
                            struct wcore_window *wc_window,
@@ -168,8 +92,8 @@ static const struct wcore_platform_vtbl nacl_platform_vtbl = {
     .destroy = nacl_platform_destroy,
 
     .make_current = nacl_platform_make_current,
-    .dl_can_open = nacl_platform_dl_can_open,
-    .dl_sym = nacl_platform_dl_sym,
+    .dl_can_open = nacl_dl_can_open,
+    .dl_sym = nacl_dl_sym,
 
     .display = {
         .connect = nacl_display_connect,
-- 
2.3.1



More information about the waffle mailing list