1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
|
import os
__version__ = '2.3.7'
def default_backend():
"""Get default backend based on the detected platform.
Supports detecting an existing context and standalone contexts.
If no context if found for the platform we return the linux backend.
Example::
# Get the available backend
backend = get_default_backend(standalone=False/True)
# Create an opengl 3.3 context or detect the currently active
# context requiring at least opengl 3.3 support.
ctx = backend(330)
Returns:
A backend object for creating and/or detecting context
"""
PLATFORMS = {'windows', 'linux', 'darwin'}
import platform
target = platform.system().lower()
for known in PLATFORMS:
if target.startswith(known):
target = known
if target not in PLATFORMS:
target = 'linux'
if target == 'windows':
return _wgl()
if target == 'linux':
return _x11()
if target == 'darwin':
return _darwin()
raise ValueError("Cannot find suitable default backend")
def get_backend_by_name(name: str):
"""Request a specific backend by name"""
if name == 'egl':
return _egl()
raise ValueError("Cannot find supported backend: '{}'".format(name))
def _wgl():
"""Create wgl backend"""
from glcontext import wgl
def create(*args, **kwargs):
_apply_env_var(kwargs, 'glversion', 'GLCONTEXT_GLVERSION', arg_type=int)
_apply_env_var(kwargs, 'libgl', 'GLCONTEXT_WIN_LIBGL')
# make sure libgl is an absolute path
if 'libgl' in kwargs:
_libgl = kwargs['libgl']
if '/' in _libgl or '\\' in _libgl:
kwargs['libgl'] = os.path.abspath(_libgl)
kwargs = _strip_kwargs(kwargs, ['glversion', 'mode', 'libgl'])
return wgl.create_context(**kwargs)
return create
def _x11():
"""Create x11 backend"""
from glcontext import x11
from ctypes.util import find_library
def create(*args, **kwargs):
if not kwargs.get('libgl'):
kwargs['libgl'] = find_library('GL')
if not kwargs.get('libx11'):
kwargs['libx11'] = find_library("X11")
_apply_env_var(kwargs, 'glversion', 'GLCONTEXT_GLVERSION', arg_type=int)
_apply_env_var(kwargs, 'libgl', 'GLCONTEXT_LINUX_LIBGL')
_apply_env_var(kwargs, 'libx11', 'GLCONTEXT_LINUX_LIBX11')
kwargs = _strip_kwargs(kwargs, ['glversion', 'mode', 'libgl', 'libx11'])
return x11.create_context(**kwargs)
return create
def _darwin():
"""Create darwin/cgl context"""
from glcontext import darwin
def create(*args, **kwargs):
return darwin.create_context(**_strip_kwargs(kwargs, ['mode']))
return create
def _egl():
from glcontext import egl
from ctypes.util import find_library
def create(*args, **kwargs):
if not kwargs.get('libgl'):
kwargs['libgl'] = find_library('GL')
if not kwargs.get('libegl'):
kwargs['libegl'] = find_library('EGL')
_apply_env_var(kwargs, 'device_index', 'GLCONTEXT_DEVICE_INDEX', arg_type=int)
_apply_env_var(kwargs, 'glversion', 'GLCONTEXT_GLVERSION', arg_type=int)
_apply_env_var(kwargs, 'libgl', 'GLCONTEXT_LINUX_LIBGL')
_apply_env_var(kwargs, 'libegl', 'GLCONTEXT_LINUX_LIBEGL')
kwargs = _strip_kwargs(kwargs, ['glversion', 'mode', 'libgl', 'libegl', 'device_index'])
return egl.create_context(**kwargs)
return create
def _strip_kwargs(kwargs: dict, supported_args: list):
"""Strips away unwanted keyword arguments.
The backends are using ``PyArg_ParseTupleAndKeywords`` to
parse the incoming ``kwargs`` data. It's not well suited
to handle additional arguments.
- Removes None key arguments
- Removes unsupported arguments
"""
return {k: v for k, v in kwargs.items() if v is not None and k in supported_args}
def _apply_env_var(kwargs, arg_name, env_name, arg_type=str):
"""Injects an environment variable into the arg dict if present"""
value = os.environ.get(env_name, kwargs.get(arg_name))
if value:
kwargs[arg_name] = arg_type(value)
|