lib/egl: move `report_egl_error` from Object to `EGLDisplay`
[nit.git] / lib / egl.nit
index 4965c31..d188c29 100644 (file)
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
-# EGL is an interface between the rendering APIs OpenGL, OpenGL ES, etc.
-# and the native windowing system.
+# Interface between rendering APIs (OpenGL, OpenGL ES, etc.) and the native windowing system.
 #
 # Most services of this module are a direct wrapper of the underlying
 # C library. If a method or class is not documented in Nit, refer to
 # the official documentation by the Khronos Group at:
 # http://www.khronos.org/registry/egl/sdk/docs/man/xhtml/
-module egl is pkgconfig("egl")
+module egl is
+       pkgconfig
+       ldflags("-lEGL")@android
+end
+
+import android::aware
 
 in "C Header" `{
        #include <EGL/egl.h>
@@ -35,10 +39,10 @@ extern class EGLDisplay `{ EGLDisplay `}
        new current `{ return eglGetCurrentDisplay(); `}
        new(handle: Pointer) `{ return eglGetDisplay(handle); `}
 
-       fun is_valid: Bool `{ return recv != EGL_NO_DISPLAY; `}
+       fun is_valid: Bool `{ return self != EGL_NO_DISPLAY; `}
 
        fun initialize: Bool `{
-               EGLBoolean r = eglInitialize(recv, NULL, NULL);
+               EGLBoolean r = eglInitialize(self, NULL, NULL);
                if (r == EGL_FALSE) {
                        fprintf(stderr, "Unable to eglInitialize");
                        return 0;
@@ -48,12 +52,12 @@ extern class EGLDisplay `{ EGLDisplay `}
 
        fun major_version: Int `{
                EGLint val;
-               eglInitialize(recv, &val, NULL);
+               eglInitialize(self, &val, NULL);
                return val;
        `}
        fun minor_version: Int `{
                EGLint val;
-               eglInitialize(recv, NULL, &val);
+               eglInitialize(self, NULL, &val);
                return val;
        `}
 
@@ -62,7 +66,7 @@ extern class EGLDisplay `{ EGLDisplay `}
        #fun get_configs: nullable Array[EGLConfig] import Array[EGLConfig].with_native `{
 
        # Returns some configs compatible with the specified `attributes`
-       fun choose_configs(attribs: Array[Int]): nullable Array[EGLConfig] import Array[Int].length, Array[Int].[], Array[EGLConfig], Array[EGLConfig].add, Array[EGLConfig] as nullable, report_egl_error `{
+       fun choose_configs(attribs: Array[Int]): nullable Array[EGLConfig] import Array[Int].length, Array[Int].[], Array[EGLConfig], Array[EGLConfig].add, Array[EGLConfig].as nullable, report_egl_error `{
                EGLConfig *configs;
                int n_configs;
                int n_attribs = Array_of_Int_length(attribs);
@@ -73,22 +77,22 @@ extern class EGLDisplay `{ EGLDisplay `}
                }
 
                // get number of configs
-               EGLBoolean r = eglChooseConfig(recv, c_attribs, NULL, 0, &n_configs);
+               EGLBoolean r = eglChooseConfig(self, c_attribs, NULL, 0, &n_configs);
 
                if (r == EGL_FALSE) {
-                       EGLDisplay_report_egl_error(recv, "failed to get number of available configs.");
+                       EGLDisplay_report_egl_error(self, "failed to get number of available configs.");
                        return null_Array_of_EGLConfig();
                } else if (n_configs == 0) {
-                       EGLDisplay_report_egl_error(recv, "no config available.");
+                       EGLDisplay_report_egl_error(self, "no config available.");
                        return null_Array_of_EGLConfig();
                }
 
                configs = (EGLConfig*)malloc(sizeof(EGLConfig)*n_configs);
  
-               r = eglChooseConfig(recv, c_attribs, configs, n_configs, &n_configs);
+               r = eglChooseConfig(self, c_attribs, configs, n_configs, &n_configs);
 
                if (r == EGL_FALSE) {
-                       EGLDisplay_report_egl_error(recv, "failed to load config.");
+                       EGLDisplay_report_egl_error(self, "failed to load config.");
                        return null_Array_of_EGLConfig();
                } else {
                        Array_of_EGLConfig array = new_Array_of_EGLConfig();
@@ -99,10 +103,16 @@ extern class EGLDisplay `{ EGLDisplay `}
                }
        `}
 
+       private fun report_egl_error(cmsg: NativeString)
+       do
+               var msg = cmsg.to_s
+               print "libEGL error: {msg}"
+       end
+
        # Can be used directly, but it is preferable to use a `EGLConfigAttribs`
        fun config_attrib(config: EGLConfig, attribute: Int): Int `{
                EGLint val;
-               EGLBoolean r = eglGetConfigAttrib(recv, config, attribute, &val);
+               EGLBoolean r = eglGetConfigAttrib(self, config, attribute, &val);
                if (r == EGL_FALSE)
                        return -1;
                else
@@ -110,23 +120,23 @@ extern class EGLDisplay `{ EGLDisplay `}
        `}
 
        fun terminate: Bool `{
-               return eglTerminate(recv) == EGL_TRUE;
+               return eglTerminate(self) == EGL_TRUE;
        `}
 
        fun create_window_surface(config: EGLConfig, native_window: Pointer, attribs: Array[Int]): EGLSurface `{
-               EGLSurface surface = eglCreateWindowSurface(recv, config, (EGLNativeWindowType)native_window, NULL);
+               EGLSurface surface = eglCreateWindowSurface(self, config, (EGLNativeWindowType)native_window, NULL);
                return surface;
        `}
 
        # TODO add share_context
        fun create_context(config: EGLConfig): EGLContext `{
                EGLint context_attribs[] = {EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE, EGL_NONE}; // TODO move out!
-               EGLContext context = eglCreateContext(recv, config, EGL_NO_CONTEXT, context_attribs);
+               EGLContext context = eglCreateContext(self, config, EGL_NO_CONTEXT, context_attribs);
                return context;
        `}
 
        fun make_current(draw, read: EGLSurface, context: EGLContext): Bool `{
-               if (eglMakeCurrent(recv, draw, read, context) == EGL_FALSE) {
+               if (eglMakeCurrent(self, draw, read, context) == EGL_FALSE) {
                        fprintf(stderr, "Unable to eglMakeCurrent");
                        return 0;
                }
@@ -136,7 +146,7 @@ extern class EGLDisplay `{ EGLDisplay `}
        # Can be used directly, but it is preferable to use a `EGLSurfaceAttribs`
        fun query_surface(surface: EGLSurface, attribute: Int): Int `{
                int val;
-               EGLBoolean r = eglQuerySurface(recv, surface, attribute, &val);
+               EGLBoolean r = eglQuerySurface(self, surface, attribute, &val);
                if (r == EGL_FALSE)
                        return -1;
                else
@@ -144,11 +154,11 @@ extern class EGLDisplay `{ EGLDisplay `}
        `}
 
        fun destroy_context(context: EGLContext): Bool `{
-               return eglDestroyContext(recv, context);
+               return eglDestroyContext(self, context);
        `}
 
        fun destroy_surface(surface: EGLSurface): Bool `{
-               return eglDestroySurface(recv, surface);
+               return eglDestroySurface(self, surface);
        `}
 
        fun error: EGLError `{ return eglGetError(); `}
@@ -164,18 +174,18 @@ extern class EGLDisplay `{ EGLDisplay `}
        end
 
        private fun query_string(name: Int): String import NativeString.to_s `{
-               return NativeString_to_s((char *)eglQueryString(recv, name));
+               return NativeString_to_s((char *)eglQueryString(self, name));
        `}
 
-       fun vendor: String do return query_string("3053".to_hex)
+       fun vendor: String do return query_string(0x3053)
 
-       fun version: String do return query_string("3054".to_hex)
+       fun version: String do return query_string(0x3054)
 
-       fun extensions: Array[String] do return query_string("3055".to_hex).split_with(" ")
+       fun extensions: Array[String] do return query_string(0x3055).split_with(" ")
 
-       fun client_apis: Array[String] do return query_string("308D".to_hex).split_with(" ")
+       fun client_apis: Array[String] do return query_string(0x308D).split_with(" ")
 
-       fun swap_buffers(surface: EGLSurface) `{ eglSwapBuffers(recv, surface); `}
+       fun swap_buffers(surface: EGLSurface) `{ eglSwapBuffers(self, surface); `}
 end
 
 extern class EGLConfig `{ EGLConfig `}
@@ -189,7 +199,7 @@ extern class EGLSurface `{ EGLSurface `}
        new current_read `{ return eglGetCurrentSurface(EGL_READ); `}
        new none `{ return EGL_NO_SURFACE; `}
 
-       fun is_ok: Bool `{ return recv != EGL_NO_SURFACE; `}
+       fun is_ok: Bool `{ return self != EGL_NO_SURFACE; `}
 
        fun attribs(display: EGLDisplay): EGLSurfaceAttribs do
                return new EGLSurfaceAttribs(display, self)
@@ -200,7 +210,7 @@ extern class EGLContext `{ EGLContext `}
        new current `{ return eglGetCurrentContext(); `}
        new none `{ return EGL_NO_CONTEXT; `}
 
-       fun is_ok: Bool `{ return recv != EGL_NO_CONTEXT; `}
+       fun is_ok: Bool `{ return self != EGL_NO_CONTEXT; `}
 end
 
 # Attributes of a config for a given EGL display
@@ -208,31 +218,42 @@ class EGLConfigAttribs
        var display: EGLDisplay
        var config: EGLConfig
 
-       fun alpha_size: Int do return display.config_attrib(config, "3021".to_hex)
-       fun native_visual_id: Int do return display.config_attrib(config, "302E".to_hex)
-       fun native_visual_type: Int do return display.config_attrib(config, "302F".to_hex)
+       fun buffer_size: Int do return display.config_attrib(config, 0x3020)
+       fun alpha_size: Int do return display.config_attrib(config, 0x3021)
+       fun blue_size: Int do return display.config_attrib(config, 0x3022)
+       fun green_size: Int do return display.config_attrib(config, 0x3023)
+       fun red_size: Int do return display.config_attrib(config, 0x3024)
+       fun depth_size: Int do return display.config_attrib(config, 0x3025)
+       fun stencil_size: Int do return display.config_attrib(config, 0x3026)
+
+       fun native_visual_id: Int do return display.config_attrib(config, 0x302E)
+       fun native_visual_type: Int do return display.config_attrib(config, 0x302F)
 
        fun caveat: EGLConfigCaveat do
-               return new EGLConfigCaveat.from_i(display.config_attrib(config, "3027".to_hex))
+               return new EGLConfigCaveat.from_i(display.config_attrib(config, 0x3027))
+       end
+
+       fun conformant: EGLConformant do
+               return new EGLConformant.from_i(display.config_attrib(config, 0x3042))
        end
 end
 
 extern class EGLConfigCaveat `{ EGLint `}
        new from_i(val: Int) `{ return (EGLint)val; `}
-       fun to_i: Int `{ return recv; `}
+       fun to_i: Int `{ return self; `}
 
        new none `{ return EGL_NONE; `}
-       fun is_none: Bool `{ return recv == EGL_NONE; `}
+       fun is_none: Bool `{ return self == EGL_NONE; `}
 
        new dont_care `{ return EGL_DONT_CARE; `}
-       fun is_dont_care: Bool `{ return recv == EGL_DONT_CARE; `}
+       fun is_dont_care: Bool `{ return self == EGL_DONT_CARE; `}
 
        new slow `{ return EGL_SLOW_CONFIG; `}
-       fun is_slow: Bool `{ return recv == EGL_SLOW_CONFIG; `}
+       fun is_slow: Bool `{ return self == EGL_SLOW_CONFIG; `}
 
        # Obselete since EGL 1.3, use EGL_CONFORMANT instead
        new non_conformant `{ return EGL_NON_CONFORMANT_CONFIG; `}
-       fun is_non_conformant: Bool `{ return recv == EGL_NON_CONFORMANT_CONFIG; `}
+       fun is_non_conformant: Bool `{ return self == EGL_NON_CONFORMANT_CONFIG; `}
 
        redef fun to_s
        do
@@ -244,45 +265,75 @@ extern class EGLConfigCaveat `{ EGLint `}
        end
 end
 
+extern class EGLConformant `{ EGLint `}
+       new `{ return (EGLint)0; `}
+       new from_i(val: Int) `{ return (EGLint)val; `}
+       fun to_i: Int `{ return self; `}
+
+       fun opengl: Bool `{ return self & EGL_OPENGL_BIT; `}
+       fun with_opengl: EGLConformant `{ return self | EGL_OPENGL_BIT; `}
+
+       fun opengl_es: Bool `{ return self & EGL_OPENGL_ES_BIT; `}
+       fun with_opengl_es: EGLConformant `{ return self | EGL_OPENGL_ES_BIT; `}
+
+       fun opengl_es2: Bool `{ return self & EGL_OPENGL_ES2_BIT; `}
+       fun with_opengl_es2: EGLConformant `{ return self | EGL_OPENGL_ES2_BIT; `}
+
+       fun openvg: Bool `{ return self & EGL_OPENVG_BIT; `}
+       fun with_openvg: EGLConformant `{ return self | EGL_OPENVG_BIT; `}
+
+       fun to_a: Array[String]
+       do
+               var features = new Array[String]
+               if opengl then features.add("OpenGL")
+               if opengl_es then features.add("OpenGL ES")
+               if opengl_es2 then features.add("OpenGL ES2")
+               if openvg then features.add("OpenVG")
+               return features
+       end
+
+       redef fun to_s do return to_a.join(", ")
+end
+
 # Attributes of a surface for a given EGL display
 class EGLSurfaceAttribs
        var display: EGLDisplay
        var surface: EGLSurface
 
-       fun height: Int do return display.query_surface(surface, "3056".to_hex)
-       fun width: Int do return display.query_surface(surface, "3057".to_hex)
-       fun largest_pbuffer: Int do return display.query_surface(surface, "3058".to_hex)
-       fun texture_format: Int do return display.query_surface(surface, "3080".to_hex)
-       fun texture_target: Int do return display.query_surface(surface, "3081".to_hex)
-       fun mipmap_texture: Int do return display.query_surface(surface, "3082".to_hex)
-       fun mipmap_level: Int do return display.query_surface(surface, "3083".to_hex)
-       fun render_buffer: Int do return display.query_surface(surface, "3086".to_hex)
-       fun vg_colorspace: Int do return display.query_surface(surface, "3087".to_hex)
-       fun vg_alpha_format: Int do return display.query_surface(surface, "3088".to_hex)
-       fun horizontal_resolution: Int do return display.query_surface(surface, "3090".to_hex)
-       fun vertical_resolution: Int do return display.query_surface(surface, "3091".to_hex)
-       fun pixel_aspect_ratio: Int do return display.query_surface(surface, "3092".to_hex)
-       fun swap_behavior: Int do return display.query_surface(surface, "3093".to_hex)
-       fun multisample_resolve: Int do return display.query_surface(surface, "3099".to_hex)
+       fun height: Int do return display.query_surface(surface, 0x3056)
+       fun width: Int do return display.query_surface(surface, 0x3057)
+       fun largest_pbuffer: Int do return display.query_surface(surface, 0x3058)
+       fun texture_format: Int do return display.query_surface(surface, 0x3080)
+       fun texture_target: Int do return display.query_surface(surface, 0x3081)
+       fun mipmap_texture: Int do return display.query_surface(surface, 0x3082)
+       fun mipmap_level: Int do return display.query_surface(surface, 0x3083)
+       fun render_buffer: Int do return display.query_surface(surface, 0x3086)
+       fun vg_colorspace: Int do return display.query_surface(surface, 0x3087)
+       fun vg_alpha_format: Int do return display.query_surface(surface, 0x3088)
+       fun horizontal_resolution: Int do return display.query_surface(surface, 0x3090)
+       fun vertical_resolution: Int do return display.query_surface(surface, 0x3091)
+       fun pixel_aspect_ratio: Int do return display.query_surface(surface, 0x3092)
+       fun swap_behavior: Int do return display.query_surface(surface, 0x3093)
+       fun multisample_resolve: Int do return display.query_surface(surface, 0x3099)
 end
 
 extern class EGLError `{ EGLint `}
-       fun is_success: Bool `{ return recv == EGL_SUCCESS; `}
-
-       fun is_not_initialized: Bool `{ return recv == EGL_NOT_INITIALIZED; `}
-       fun is_bad_access: Bool `{ return recv == EGL_BAD_ACCESS; `}
-       fun is_bad_alloc: Bool `{ return recv == EGL_BAD_ALLOC; `}
-       fun is_bad_attribute: Bool `{ return recv == EGL_BAD_ATTRIBUTE; `}
-       fun is_bad_config: Bool `{ return recv == EGL_BAD_CONFIG; `}
-       fun is_bad_context: Bool `{ return recv == EGL_BAD_CONTEXT; `}
-       fun is_bad_current_surface: Bool `{ return recv == EGL_BAD_CURRENT_SURFACE; `}
-       fun is_bad_display: Bool `{ return recv == EGL_BAD_DISPLAY; `}
-       fun is_bad_match: Bool `{ return recv == EGL_BAD_MATCH; `}
-       fun is_bad_native_pixmap: Bool `{ return recv == EGL_BAD_NATIVE_PIXMAP; `}
-       fun is_bad_native_window: Bool `{ return recv == EGL_BAD_NATIVE_WINDOW; `}
-       fun is_bad_parameter: Bool `{ return recv == EGL_BAD_PARAMETER; `}
-       fun is_bad_surface: Bool `{ return recv == EGL_BAD_SURFACE; `}
-       fun is_context_lost: Bool `{ return recv == EGL_CONTEXT_LOST; `}
+       fun is_success: Bool `{ return self == EGL_SUCCESS; `}
+
+       fun is_not_initialized: Bool `{ return self == EGL_NOT_INITIALIZED; `}
+       fun is_bad_access: Bool `{ return self == EGL_BAD_ACCESS; `}
+       fun is_bad_alloc: Bool `{ return self == EGL_BAD_ALLOC; `}
+       fun is_bad_attribute: Bool `{ return self == EGL_BAD_ATTRIBUTE; `}
+       fun is_bad_config: Bool `{ return self == EGL_BAD_CONFIG; `}
+       fun is_bad_context: Bool `{ return self == EGL_BAD_CONTEXT; `}
+       fun is_bad_current_surface: Bool `{ return self == EGL_BAD_CURRENT_SURFACE; `}
+       fun is_bad_display: Bool `{ return self == EGL_BAD_DISPLAY; `}
+       fun is_bad_match: Bool `{ return self == EGL_BAD_MATCH; `}
+       fun is_bad_native_pixmap: Bool `{ return self == EGL_BAD_NATIVE_PIXMAP; `}
+       fun is_bad_native_window: Bool `{ return self == EGL_BAD_NATIVE_WINDOW; `}
+       fun is_bad_parameter: Bool `{ return self == EGL_BAD_PARAMETER; `}
+       fun is_bad_surface: Bool `{ return self == EGL_BAD_SURFACE; `}
+       fun is_context_lost: Bool `{ return self == EGL_CONTEXT_LOST; `}
 
        redef fun to_s
        do
@@ -364,23 +415,32 @@ class EGLConfigChooser
        end
 
        fun close do
-               insert_attrib_key "3038".to_hex
+               insert_attrib_key 0x3038
                closed = true
        end
 
-       fun surface_type=(flag: Int) do insert_attrib_with_val("3033".to_hex, flag)
+       fun surface_type=(flag: Int) do insert_attrib_with_val(0x3033, flag)
        fun surface_type_egl do surface_type = 4
 
-       fun blue_size=(size: Int) do insert_attrib_with_val("3022".to_hex, size)
-       fun green_size=(size: Int) do insert_attrib_with_val("3023".to_hex, size)
-       fun red_size=(size: Int) do insert_attrib_with_val("3024".to_hex, size)
+       # Set which client rendering APIs are supported
+       fun renderable_type=(flag: Int) do insert_attrib_with_val(0x3040, flag)
+
+       # Set EGL as the only supported rendering API
+       fun renderable_type_egl do renderable_type = 4
+
+       fun blue_size=(size: Int) do insert_attrib_with_val(0x3022, size)
+       fun green_size=(size: Int) do insert_attrib_with_val(0x3023, size)
+       fun red_size=(size: Int) do insert_attrib_with_val(0x3024, size)
 
-       fun alpha_size=(size: Int) do insert_attrib_with_val("3021".to_hex, size)
-       fun depth_size=(size: Int) do insert_attrib_with_val("3025".to_hex, size)
-       fun stencil_size=(size: Int) do insert_attrib_with_val("3026".to_hex, size)
-       fun sample_buffers=(size: Int) do insert_attrib_with_val("3031".to_hex, size)
+       fun buffer_size=(size: Int) do insert_attrib_with_val(0x3020, size)
+       fun alpha_size=(size: Int) do insert_attrib_with_val(0x3021, size)
+       fun depth_size=(size: Int) do insert_attrib_with_val(0x3025, size)
+       fun stencil_size=(size: Int) do insert_attrib_with_val(0x3026, size)
+       fun sample_buffers=(size: Int) do insert_attrib_with_val(0x3031, size)
 
-       fun caveat=(caveat: EGLConfigCaveat) do insert_attrib_with_val("3050".to_hex, caveat.to_i)
+       fun caveat=(caveat: EGLConfigCaveat) do insert_attrib_with_val(0x3050, caveat.to_i)
+
+       fun conformant=(conformant: EGLConformant) do insert_attrib_with_val(0x3042, conformant.to_i)
 
        fun choose(display: EGLDisplay): nullable Array[EGLConfig]
        do
@@ -389,14 +449,9 @@ class EGLConfigChooser
        end
 end
 
-redef class Object
-       private fun report_egl_error(cmsg: NativeString)
-       do
-               var msg = cmsg.to_s
-               print "libEGL error: {msg}"
-       end
-end
+fun egl_bind_opengl_api: Bool `{ return eglBindAPI(EGL_OPENGL_API); `}
+fun egl_bind_opengl_es_api: Bool `{ return eglBindAPI(EGL_OPENGL_ES_API); `}
+fun egl_bind_openvg_api: Bool `{ return eglBindAPI(EGL_OPENVG_API); `}
 
-protected fun egl_bind_opengl_api: Bool `{ return eglBindAPI(EGL_OPENGL_API); `}
-protected fun egl_bind_opengl_es_api: Bool `{ return eglBindAPI(EGL_OPENGL_ES_API); `}
-protected fun egl_bind_openvg_api: Bool `{ return eglBindAPI(EGL_OPENVG_API); `}
+# Handle to the default display to use with EGL
+fun egl_default_display: Pointer `{ return EGL_DEFAULT_DISPLAY; `}