diff --git a/tests/os_egl.py b/tests/os_egl.py index a704311b..176f6192 100644 --- a/tests/os_egl.py +++ b/tests/os_egl.py @@ -1,10 +1,11 @@ from __future__ import print_function import os, logging, pprint, ipdb -log = logging.getLogger( __name__ ) -if not os.environ.get( 'PYOPENGL_PLATFORM' ): - os.environ['PYOPENGL_PLATFORM'] = 'egl' -if 'DISPLAY' in os.environ: - del os.environ['DISPLAY'] + +log = logging.getLogger(__name__) +if not os.environ.get("PYOPENGL_PLATFORM"): + os.environ["PYOPENGL_PLATFORM"] = "egl" +if "DISPLAY" in os.environ: + del os.environ["DISPLAY"] import logging, contextlib from functools import wraps from OpenGL.GL import * @@ -16,19 +17,20 @@ from OpenGL.EGL.EXT import platform_device, platform_base, device_base API_MAP = { - EGL_OPENGL_BIT:EGL_OPENGL_API, - EGL_OPENGL_ES2_BIT:EGL_OPENGL_ES_API, - EGL_OPENGL_ES_BIT:EGL_OPENGL_ES_API, + EGL_OPENGL_BIT: EGL_OPENGL_API, + EGL_OPENGL_ES2_BIT: EGL_OPENGL_ES_API, + EGL_OPENGL_ES_BIT: EGL_OPENGL_ES_API, } + def write_ppm(buf, filename): f = open(filename, "w") if f: h, w, c = buf.shape - print( "P3", file=f) - print( "# ascii ppm file created by os_egl",file=f) - print( "%i %i" % (w, h),file=f) - print("255",file=f) + print("P3", file=f) + print("# ascii ppm file created by os_egl", file=f) + print("%i %i" % (w, h), file=f) + print("255", file=f) for y in range(h - 1, -1, -1): for x in range(w): pixel = buf[y, x] @@ -36,6 +38,7 @@ def write_ppm(buf, filename): f.write(l) f.write("\n") + def platformDisplay(device): """Get platform display from device specifier @@ -45,106 +48,124 @@ def platformDisplay(device): raises RuntimeError if we can't create the display """ created_device = display = None - if isinstance(device,(str,int)): + if isinstance(device, (str, int)): created_device = device = gbmdevice.open_device(device) if eglGetPlatformDisplay: display = eglGetPlatformDisplay( - platform_device.EGL_PLATFORM_DEVICE_EXT - if isinstance(device, EGLDeviceEXT) - else platform_gbm.EGL_PLATFORM_GBM_MESA, - device, - ctypes.c_void_p(0) + platform_device.EGL_PLATFORM_DEVICE_EXT + if isinstance(device, EGLDeviceEXT) + else platform_gbm.EGL_PLATFORM_GBM_MESA, + device, + ctypes.c_void_p(0), ) if display == EGL_NO_DISPLAY: - raise RuntimeError('Unable to create EGL display on %s'%(display)) + raise RuntimeError("Unable to create EGL display on %s" % (display)) else: - raise RuntimeError('eglGetPlatformDisplay has no implementation') + raise RuntimeError("eglGetPlatformDisplay has no implementation") return display, created_device + @contextlib.contextmanager def egl_context( width=256, - height=256, + height=256, api=EGL_OPENGL_BIT, - attributes = ( - EGL_BLUE_SIZE, 8, - EGL_RED_SIZE,8, - EGL_GREEN_SIZE,8, - EGL_DEPTH_SIZE,24, - EGL_COLOR_BUFFER_TYPE, EGL_RGB_BUFFER, + attributes=( + EGL_BLUE_SIZE, + 8, + EGL_RED_SIZE, + 8, + EGL_GREEN_SIZE, + 8, + EGL_DEPTH_SIZE, + 24, + EGL_COLOR_BUFFER_TYPE, + EGL_RGB_BUFFER, # EGL_CONFIG_CAVEAT, EGL_NONE, # Don't allow slow/non-conformant ), pbuffer=False, device=None, - output='output.ppm', + output="output.ppm", ): """Setup a context for rendering""" - major,minor = GLint(), GLint() + major, minor = GLint(), GLint() created_device = platform_surface = surface = None if device is None: display = eglGetDisplay(EGL_DEFAULT_DISPLAY) if display == EGL_NO_DISPLAY: raise RuntimeError(EGL_NO_DISPLAY, "Could not create default display") else: - display,created_device = platformDisplay(device) + display, created_device = platformDisplay(device) try: # print("Display: %s"%(display.address,)) try: - eglInitialize( display, major, minor) + eglInitialize(display, major, minor) except GLError as err: err.err = debug.eglErrorName(err.err) log.warning("eglInitilise failure on %s: %s", display, err.err) raise NoEGLSupport(display) num_configs = GLint() - configs = (EGLConfig*1)() + configs = (EGLConfig * 1)() local_attributes = list(attributes[:]) if pbuffer: - local_attributes.extend([ - EGL_SURFACE_TYPE, EGL_PBUFFER_BIT, - ]) + local_attributes.extend( + [EGL_SURFACE_TYPE, EGL_PBUFFER_BIT,] + ) else: - local_attributes.extend([ - EGL_SURFACE_TYPE, EGL_WINDOW_BIT, - ]) - local_attributes.extend( [ - EGL_CONFORMANT, api, - EGL_NONE, # end of list - ]) + local_attributes.extend( + [EGL_SURFACE_TYPE, EGL_WINDOW_BIT,] + ) + local_attributes.extend( + [EGL_CONFORMANT, api, EGL_NONE,] # end of list + ) log.debug("Attributes: %s", local_attributes) - local_attributes= arrays.GLintArray.asArray( local_attributes ) + local_attributes = arrays.GLintArray.asArray(local_attributes) try: - success = eglChooseConfig(display, local_attributes, configs, 1, num_configs) + success = eglChooseConfig( + display, local_attributes, configs, 1, num_configs + ) if not success: raise RuntimeError("Unable to complete config filtering") if not num_configs: - configs = (EGLConfig*10)() + configs = (EGLConfig * 10)() eglGetConfigs(display, configs, 10, num_configs) if num_configs.value: - for config in configs[:num_configs.value]: - log.warning("Unused config: %s", pprint.pformat(debug.debug_config(display,config))) - raise RuntimeError("No compatible configs found on %s"%(device or 'default')) + for config in configs[: num_configs.value]: + log.warning( + "Unused config: %s", + pprint.pformat(debug.debug_config(display, config)), + ) + raise RuntimeError( + "No compatible configs found on %s" % (device or "default") + ) # for config in configs[:num_configs.value]: # log.debug("Config: %s",pprint.pformat(debug.debug_config(display,config))) config = configs[0] - log.debug("Selecting config: %s", pprint.pformat(debug.debug_config(display,config))) + log.debug( + "Selecting config: %s", + pprint.pformat(debug.debug_config(display, config)), + ) surface_attributes = [ - EGL_WIDTH,width, - EGL_HEIGHT,height, + EGL_WIDTH, + width, + EGL_HEIGHT, + height, EGL_NONE, ] if pbuffer: surface = eglCreatePbufferSurface( - display, - configs[0], - surface_attributes, + display, configs[0], surface_attributes, ) else: visual = EGLint() - eglGetConfigAttrib(display,configs[0],EGL_NATIVE_VISUAL_ID,visual) + eglGetConfigAttrib(display, configs[0], EGL_NATIVE_VISUAL_ID, visual) log.debug("Native visual id %s", visual.value) platform_surface = gbmdevice.create_surface( - created_device, width, height, format=visual.value, - flags=gbmdevice.GBM_BO_USE_RENDERING + created_device, + width, + height, + format=visual.value, + flags=gbmdevice.GBM_BO_USE_RENDERING, ) log.debug("Native surface created on %s", device) if not platform_surface: @@ -162,10 +183,10 @@ def egl_context( eglBindAPI(API_MAP[api]) ctx = eglCreateContext(display, configs[0], EGL_NO_CONTEXT, None) if ctx == EGL_NO_CONTEXT: - raise RuntimeError( 'Unable to create context' ) - eglMakeCurrent( display, surface, surface, ctx ) + raise RuntimeError("Unable to create context") + eglMakeCurrent(display, surface, surface, ctx) log.debug("Yielding to caller") - yield display,ctx,surface + yield display, ctx, surface log.debug("Doing context cleanup") if not pbuffer: # This crashes on my intel i915 device, as do glFlush and glFinish @@ -174,18 +195,13 @@ def egl_context( glFinish() if output: log.debug("Doing readpixels for writing buffer") - content = glReadPixelsub( - 0,0, - width, height, - GL_RGB, - outputType=None, - ) + content = glReadPixelsub(0, 0, width, height, GL_RGB, outputType=None,) write_ppm(content, output) # glFinish() finally: if display: log.debug("Unsetting current") - eglMakeCurrent( display, None, None, None ) + eglMakeCurrent(display, None, None, None) if surface: eglDestroySurface(display, surface) log.debug("Terminating display") @@ -197,21 +213,23 @@ def egl_context( log.debug("Closing gbm device") gbmdevice.close_device(created_device) + class NoEGLSupport(Exception): """Raised if we could not initialise an egl context""" + def debug_info(setup): - display,ctx,surface = setup - glClearColor(1.0,1.0,1.0,1.0) - glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT) - log.info('Vendor: %s', glGetString(GL_VENDOR)) - log.info('Extensions: %s', glGetString(GL_EXTENSIONS)) + display, ctx, surface = setup + glClearColor(1.0, 1.0, 1.0, 1.0) + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) + log.info("Vendor: %s", glGetString(GL_VENDOR)) + log.info("Extensions: %s", glGetString(GL_EXTENSIONS)) glFinish() def main(): - # NOTE: having two different implementations here is - # likely somewhat broken due to the + # NOTE: having two different implementations here is + # likely somewhat broken due to the # OpenGL functions having retrieved their # function pointers during the first pass and then # trying to run them against the second @@ -222,8 +240,8 @@ def main(): debug_info(setup) except (NoEGLSupport): pass - except (GLError,RuntimeError): - log.exception('Failed during: %s', device) + except (GLError, RuntimeError): + log.exception("Failed during: %s", device) for device in gbmdevice.enumerate_devices(): log.info("Starting tests with: %s", device) try: @@ -231,10 +249,10 @@ def main(): debug_info(setup) except (NoEGLSupport): pass - except (GLError,RuntimeError): - log.exception('Failed during: %s', device) + except (GLError, RuntimeError): + log.exception("Failed during: %s", device) -if __name__ == '__main__': +if __name__ == "__main__": logging.basicConfig(level=logging.DEBUG) main()