def create_opengl_context(self, surface_size=(640, 480)): """Create offscreen OpenGL context and make it current. Users are expected to directly use EGL API in case more advanced context management is required. Args: surface_size: (width, height), size of the offscreen rendering surface. """ egl_display = egl.eglGetDisplay(egl.EGL_DEFAULT_DISPLAY) major, minor = egl.EGLint(), egl.EGLint() egl.eglInitialize(egl_display, pointer(major), pointer(minor)) config_attribs = [ egl.EGL_SURFACE_TYPE, egl.EGL_PBUFFER_BIT, egl.EGL_BLUE_SIZE, 8, egl.EGL_GREEN_SIZE, 8, egl.EGL_RED_SIZE, 8, egl.EGL_DEPTH_SIZE, 24, egl.EGL_RENDERABLE_TYPE, egl.EGL_OPENGL_BIT, egl.EGL_NONE, ] # if need MSAA https://www.khronos.org/opengl/wiki/Multisampling config_attribs = (egl.EGLint * len(config_attribs))(*config_attribs) num_configs = egl.EGLint() egl_cfg = egl.EGLConfig() egl.eglChooseConfig(egl_display, config_attribs, pointer(egl_cfg), 1, pointer(num_configs)) width, height = surface_size pbuffer_attribs = [ egl.EGL_WIDTH, width, egl.EGL_HEIGHT, height, egl.EGL_NONE, ] pbuffer_attribs = (egl.EGLint * len(pbuffer_attribs))(*pbuffer_attribs) egl_surf = egl.eglCreatePbufferSurface(egl_display, egl_cfg, pbuffer_attribs) egl.eglBindAPI(egl.EGL_OPENGL_API) egl_context = egl.eglCreateContext(egl_display, egl_cfg, egl.EGL_NO_CONTEXT, None) egl.eglMakeCurrent(egl_display, egl_surf, egl_surf, egl_context) self.display = egl_display
def main(): _width = 256 _height = 256 # Whether hidpi is active #def on_error(error, message): # log.warning(message) #glfw.glfwSetErrorCallback(on_error) egl_display = egl.eglGetDisplay(egl.EGL_DEFAULT_DISPLAY) major, minor = egl.EGLint(), egl.EGLint() egl.eglInitialize(egl_display, pointer(major), pointer(minor)) config_attribs = [ egl.EGL_SURFACE_TYPE, egl.EGL_PBUFFER_BIT, egl.EGL_BLUE_SIZE, 8, egl.EGL_GREEN_SIZE, 8, egl.EGL_RED_SIZE, 8, egl.EGL_DEPTH_SIZE, 24, egl.EGL_RENDERABLE_TYPE, egl.EGL_OPENGL_BIT, egl.EGL_NONE ] config_attribs = (egl.EGLint * len(config_attribs))(*config_attribs) num_configs = egl.EGLint() egl_cfg = egl.EGLConfig() egl.eglChooseConfig(egl_display, config_attribs, pointer(egl_cfg), 1, pointer(num_configs)) pbuffer_attribs = [ egl.EGL_WIDTH, _width, egl.EGL_HEIGHT, _height, egl.EGL_NONE, ] pbuffer_attribs = (egl.EGLint * len(pbuffer_attribs))(*pbuffer_attribs) egl_surf = egl.eglCreatePbufferSurface(egl_display, egl_cfg, pbuffer_attribs) egl.eglBindAPI(egl.EGL_OPENGL_API) egl_context = egl.eglCreateContext(egl_display, egl_cfg, egl.EGL_NO_CONTEXT, None) egl.eglMakeCurrent(egl_display, egl_surf, egl_surf, egl_context) #print("context made current") print('Vendor: {}'.format(glGetString(GL_VENDOR).decode('utf-8'))) print('Opengl version: {}'.format(glGetString(GL_VERSION).decode('utf-8'))) print('GLSL Version: {}'.format( glGetString(GL_SHADING_LANGUAGE_VERSION).decode('utf-8'))) print('Renderer: {}'.format(glGetString(GL_RENDERER).decode('utf-8')))
def initialize_on_device(self, device, width, height): print("selected: " + device.name) # step 1 if device.initialize(): self.add_rollback_cb(lambda: device.release()) else: self.rollback() return False # step 2 egl_dpy = device.get_egl_display() if egl_dpy != egl.EGL_NO_DISPLAY: self.add_rollback_cb(lambda: egl.eglTerminate(egl_dpy)) else: self.rollback() return False # step 3 major, minor = egl.EGLint(), egl.EGLint() if not egl.eglInitialize(egl_dpy, pointer(major), pointer(minor)): self.rollback() return False print("EGL version %d.%d" % (major.value, minor.value)) # step 4 egl_config = self.get_config(egl_dpy, device.compatible_surface_type()) if egl_config is None: self.rollback() return False # step 5 egl_surface = device.create_surface(egl_dpy, egl_config) if egl_surface.initialize(width, height): self.add_rollback_cb(lambda: egl_surface.release()) else: self.rollback() return False # step 6 egl_context = self.get_context(egl_dpy, egl_config) if egl_context is not None: self.add_rollback_cb( lambda: egl.eglDestroyContext(egl_dpy, egl_context)) else: self.rollback() return False # step 7 if not egl_surface.make_current(egl_context): self.rollback() return False # device seems to be working return True
def create_opengl_context(surface_size=(640, 480)): """Create offscreen OpenGL context and make it current. Users are expected to directly use EGL API in case more advanced context management is required. Args: surface_size: (width, height), size of the offscreen rendering surface. """ egl_display = create_initialized_headless_egl_display() if egl_display == egl.EGL_NO_DISPLAY: raise ImportError('Cannot initialize a headless EGL display.') major, minor = egl.EGLint(), egl.EGLint() egl.eglInitialize(egl_display, pointer(major), pointer(minor)) config_attribs = [ egl.EGL_SURFACE_TYPE, egl.EGL_PBUFFER_BIT, egl.EGL_BLUE_SIZE, 8, egl.EGL_GREEN_SIZE, 8, egl.EGL_RED_SIZE, 8, egl.EGL_DEPTH_SIZE, 24, egl.EGL_RENDERABLE_TYPE, egl.EGL_OPENGL_BIT, egl.EGL_NONE ] config_attribs = (egl.EGLint * len(config_attribs))(*config_attribs) num_configs = egl.EGLint() egl_cfg = egl.EGLConfig() egl.eglChooseConfig(egl_display, config_attribs, pointer(egl_cfg), 1, pointer(num_configs)) width, height = surface_size pbuffer_attribs = [ egl.EGL_WIDTH, width, egl.EGL_HEIGHT, height, egl.EGL_NONE, ] pbuffer_attribs = (egl.EGLint * len(pbuffer_attribs))(*pbuffer_attribs) egl_surf = egl.eglCreatePbufferSurface(egl_display, egl_cfg, pbuffer_attribs) egl.eglBindAPI(egl.EGL_OPENGL_API) egl_context = egl.eglCreateContext(egl_display, egl_cfg, egl.EGL_NO_CONTEXT, None) egl.eglMakeCurrent(egl_display, egl_surf, egl_surf, egl_context)
def eglQueryDevicesEXT(max_devices=10): # pylint: disable=invalid-name devices = (EGLDeviceEXT * max_devices)() num_devices = EGL.EGLint() success = _eglQueryDevicesEXT(max_devices, devices, num_devices) if success == EGL.EGL_TRUE: return [devices[i] for i in range(num_devices.value)] else: raise error.GLError(err=EGL.eglGetError(), baseOperation=eglQueryDevicesEXT, result=success)
def probe(): if not hasattr(egl, 'eglQueryDevicesEXT'): # if no enumeration support in EGL, return empty list return [] num_devices = egl.EGLint() if not egl.eglQueryDevicesEXT( 0, None, pointer(num_devices)) or num_devices.value < 1: return [] devices = (egl.EGLDeviceEXT * num_devices.value)() # array of size num_devices if not egl.eglQueryDevicesEXT( num_devices.value, devices, pointer(num_devices)) or num_devices.value < 1: return [] return [GenericEGLDevice(devices[i]) for i in range(num_devices.value)]
def query_devices(): if _eglQueryDevicesEXT is None: raise RuntimeError("EGL query extension is not loaded or is not supported.") num_devices = egl.EGLint() success = _eglQueryDevicesEXT(0, None, ctypes.pointer(num_devices)) if not success or num_devices.value < 1: return [] devices = (_EGLDeviceEXT * num_devices.value)() # array of size num_devices success = _eglQueryDevicesEXT(num_devices.value, devices, ctypes.pointer(num_devices)) if not success or num_devices.value < 1: return [] return [EGLDevice(devices[i]) for i in range(num_devices.value)]
def get_config(self, egl_dpy, surface_type): egl_config_attribs = { egl.EGL_RED_SIZE: 8, egl.EGL_GREEN_SIZE: 8, egl.EGL_BLUE_SIZE: 8, egl.EGL_ALPHA_SIZE: 8, egl.EGL_DEPTH_SIZE: egl.EGL_DONT_CARE, egl.EGL_STENCIL_SIZE: egl.EGL_DONT_CARE, egl.EGL_RENDERABLE_TYPE: egl.EGL_OPENGL_BIT, egl.EGL_SURFACE_TYPE: surface_type, } egl_config_attribs = egl_convert_to_int_array(egl_config_attribs) egl_config = egl.EGLConfig() num_configs = egl.EGLint() if not egl.eglChooseConfig(egl_dpy, egl_config_attribs, pointer(egl_config), 1, pointer(num_configs)): return None if num_configs.value == 0: return None return egl_config
def initialize(self, width, height): gbm_format = egl.EGLint() if not egl.eglGetConfigAttrib(self.egl_dpy, self.egl_config, egl.EGL_NATIVE_VISUAL_ID, pointer(gbm_format)): return False self.gbm_surf = libgbm.gbm_surface_create(self.gbm_dev, width, height, gbm_format, libgbm.GBM_BO_USE_RENDERING) if not self.gbm_surf: self.gbm_surf = None return False self.egl_surface = egl.eglCreateWindowSurface(self.egl_dpy, self.egl_config, self.gbm_surf, None) if self.egl_surface == egl.EGL_NO_SURFACE: self.egl_surface = None self.release() return False return True