Repository: vispy/vispy

Issue:
Vispy always tries to import gl2 by default
At https://github.com/vispy/vispy/blob/master/vispy/gloo/gl/__init__.py#L235-L238, vispy always tries to import gl2 and call use_gl with 'gl2' as the default argument. This fails on Android (and I assume other places) because only gles is available. I fixed it for that by patching to import es2 by default instead.

I'd like to change it so that Vispy works without modification on Android, but I'm not sure how since gloo.gl does need to import something as a default. One option would be to try the current method, then if it fails catch the Exception and try es2 instead, but I'm not sure if there are downsides to that.

Any ideas?



Discussion:
I think it would be fine to try to import the most compete gl possible. We
could triage based on platform or fall back as you suggest. Systematically
going from most compete to most restrictive GL seems like it would work
well.

I think ideally you would detect the platform; when its Android or Raspberry pi use es2, otherwise use gl2.

@inclement do you have time to try the autodetection?

Sure, but I missed this - is that something now in master?

Sorry, I meant try _implementing_ the autodetection :)

Oh, right! In that case, still sure, it's been on my list of things to
try for a while but hasn't been a priority. I might return to Vispy on
Android soon to try to get it working properly, in which case I'll
certainly take a loot at it.

On 20/05/16 01:57, Eric Larson wrote:

> Sorry, I meant try /implementing/ the autodetection :)
> 
> —
> You are receiving this because you were mentioned.
> Reply to this email directly or view it on GitHub
> https://github.com/vispy/vispy/issues/1109#issuecomment-220491606

I may be able to implement code to detect raspberry pi if you let me know where it should go.

@kmuehlbauer What does rpi do for this? Does it have a full GL library available or only ES?

Either way I'm moving this to 0.7.
@djhoese There seems to be not much detailed information out there. According to the specs it is

- OpenGL ES 1.1, 2.0 graphics for Raspberry Pi 3B+
- OpenGL ES 3.0 graphics for the new Raspberry Pi 4

Resolving patch:
diff --git a/vispy/gloo/gl/__init__.py b/vispy/gloo/gl/__init__.py
--- a/vispy/gloo/gl/__init__.py
+++ b/vispy/gloo/gl/__init__.py
@@ -11,7 +11,7 @@
 This is in part possible by the widespread availability of OpenGL ES 2.0.
 
 All functions that this API provides accept and return Python arguments
-(no ctypes is required); strings are real strings and you can pass 
+(no ctypes is required); strings are real strings and you can pass
 data as numpy arrays. In general the input arguments are not checked
 (for performance reasons). Each function results in exactly one OpenGL
 API call, except when using the pyopengl backend.
@@ -41,7 +41,7 @@
 
 
 class MainProxy(BaseGLProxy):
-    """Main proxy for the GL ES 2.0 API. 
+    """Main proxy for the GL ES 2.0 API.
 
     The functions in this namespace always call into the correct GL
     backend. Therefore these function objects can be safely stored for
@@ -58,7 +58,7 @@ def __call__(self, funcname, returns, *args):
 proxy = MainProxy()
 
 
-def use_gl(target='gl2'):
+def use_gl(target=None):
     """Let Vispy use the target OpenGL ES 2.0 implementation
 
     Also see ``vispy.use()``.
@@ -66,7 +66,7 @@ def use_gl(target='gl2'):
     Parameters
     ----------
     target : str
-        The target GL backend to use.
+        The target GL backend to use. Default gl2 or es2, depending on the platform.
 
     Available backends:
     * gl2 - Use ES 2.0 subset of desktop (i.e. normal) OpenGL
@@ -81,7 +81,7 @@ def use_gl(target='gl2'):
     debug". (Debug does not apply to 'gl+', since PyOpenGL has its own
     debug mechanism)
     """
-    target = target or 'gl2'
+    target = target or default_backend.__name__.split(".")[-1]
     target = target.replace('+', 'plus')
 
     # Get options
@@ -168,7 +168,7 @@ def gl_debug_wrapper(*args):
         # Log return value
         if ret is not None:
             if fn.__name__ == 'glReadPixels':
-                logger.debug(" <= %s[%s]" % (type(ret), len(ret)))                
+                logger.debug(" <= %s[%s]" % (type(ret), len(ret)))
             else:
                 logger.debug(" <= %s" % repr(ret))
         # Check for errors (raises if an error occured)
@@ -226,6 +226,9 @@ def _fix_osmesa_gl_lib_if_testing():
 
 # Load default gl backend
 from . import gl2 as default_backend  # noqa
+if default_backend._lib is None:  # Probably Android or RPi
+    from . import es2 as default_backend  # noqa
+
 
 # Call use to start using our default backend
 use_gl()
diff --git a/vispy/gloo/gl/gl2.py b/vispy/gloo/gl/gl2.py
--- a/vispy/gloo/gl/gl2.py
+++ b/vispy/gloo/gl/gl2.py
@@ -14,7 +14,6 @@
 
 # Ctypes stuff
 
-
 # Load the OpenGL library. We more or less follow the same approach
 # as PyOpenGL does internally
 
diff --git a/vispy/gloo/glir.py b/vispy/gloo/glir.py
--- a/vispy/gloo/glir.py
+++ b/vispy/gloo/glir.py
@@ -458,7 +458,7 @@ def command(self, *args):
         self._commands.append(args)
 
     def set_verbose(self, verbose):
-        """Set verbose or not. If True, the GLIR commands are printed right before they get parsed. 
+        """Set verbose or not. If True, the GLIR commands are printed right before they get parsed.
         If a string is given, use it as a filter.
         """
         self._verbose = verbose
@@ -1375,7 +1375,7 @@ def set_data(self, offset, data):
 
         # Determine whether to check errors to try handling the ATI bug
         check_ati_bug = ((not self._bufferSubDataOk) and
-                         (gl.current_backend is gl.gl2) and
+                         (gl.current_backend.__name__.split(".")[-1] == "gl2") and
                          sys.platform.startswith('win'))
 
         # flush any pending errors


Test patch: