Beispiel #1
0
 def get_context(self, egl_dpy, egl_config):
     if not egl.eglBindAPI(egl.EGL_OPENGL_API):
         return None
     egl_context = egl.eglCreateContext(egl_dpy, egl_config, egl.EGL_NO_CONTEXT, None)
     if egl_context == egl.EGL_NO_CONTEXT:
         return None
     return egl_context
Beispiel #2
0
 def get_context(self, egl_dpy, egl_config):
     if not egl.eglBindAPI(egl.EGL_OPENGL_API):
         return None
     egl_context = egl.eglCreateContext(egl_dpy, egl_config, egl.EGL_NO_CONTEXT, None)
     if egl_context == egl.EGL_NO_CONTEXT:
         return None
     return 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)
Beispiel #4
0
 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) ]
Beispiel #5
0
 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)]
Beispiel #6
0
 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:         8,
             egl.EGL_STENCIL_SIZE:       egl.EGL_DONT_CARE,
             egl.EGL_RENDERABLE_TYPE:    egl.EGL_OPENGL_BIT,
             egl.EGL_SURFACE_TYPE:       surface_type
     }
     if SAKURA_GPU_PERFORMANCE != 'low':
         egl_config_attribs.update({
             egl.EGL_SAMPLE_BUFFERS:     1,
             egl.EGL_SAMPLES:            4
         })
     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
Beispiel #7
0
 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
Beispiel #8
0
    def gl_swap_buffer(self):
        """ This function is used to swap the front/back buffers after rendering
        an output video frame.
        PROTOTYPE:
          m64p_error VidExt_GL_SwapBuffers(void)"""
        # Note: It can spam the message in the logs, it's best to never turn it on.
        #log.debug("Vidext: gl_swap_buffer()")
        if self.new_surface:
            log.info("VidExtFuncGLSwapBuf: New surface has been detected")
            self.egl_surface = egl.eglCreateWindowSurface(
                self.egl_display, self.egl_config[0], self.window_handle,
                self.window_attributes)

            try:
                egl.eglMakeCurrent(self.egl_display, self.egl_surface,
                                   self.egl_surface, self.egl_context)
                egl.eglSwapBuffers(self.egl_display, self.egl_surface)
            except:
                log.error(
                    f"eglMakeCurrent() returned error: {egl.eglGetError()}")

            self.new_surface = False
        else:
            if self.window.m64p_wrapper.running == True:
                egl.eglSwapBuffers(self.egl_display, self.egl_surface)

        return wrp_dt.m64p_error.M64ERR_SUCCESS.value
Beispiel #9
0
def _get_egl_func(func_name, res_type, *arg_types):
    address = egl.eglGetProcAddress(func_name)
    if address is None:
        return None

    proto = ctypes.CFUNCTYPE(res_type)
    proto.argtypes = arg_types
    func = proto(address)
    return func
Beispiel #10
0
 def initialize(self, width, height):
     pb_surf_attribs = egl_convert_to_int_array({
             egl.EGL_WIDTH: width,
             egl.EGL_HEIGHT: height,
     })
     self.egl_surface = egl.eglCreatePbufferSurface(
             self.egl_dpy, self.egl_config, pb_surf_attribs)
     if self.egl_surface == egl.EGL_NO_SURFACE:
         return False
     return True
Beispiel #11
0
def main():
    with egl_context(output=None) as context:
        display,context,surface = context
        print("Vendor: %s"%(EGL.eglQueryString(display, EGL.EGL_VENDOR)))
        print("Version: %s"%(EGL.eglQueryString(display, EGL.EGL_VERSION)))
        print("Extensions: %s"%(EGL.eglQueryString(display, EGL.EGL_EXTENSIONS),))
        print("Client Extensions: %s"%(EGL.eglQueryString(display, EGL.EGL_CLIENT_APIS),))
        if device_enumeration.eglQueryDevicesEXT:
            devices = (device_query.EGLDeviceEXT * 5)()
            count = GLint()
            device_enumeration.eglQueryDevicesEXT(
                5,
                devices,
                count,
            )
            for device in devices[:int(count)]:
                print(device)
        else:
            print('No device_query extension available')
Beispiel #12
0
 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 create_initialized_headless_egl_display():
    """Creates an initialized EGL display directly on a device."""
    for device in egl_get_devices():
        display = egl.eglGetPlatformDisplayEXT(EGL_PLATFORM_DEVICE_EXT, device,
                                               None)

        if display != egl.EGL_NO_DISPLAY and egl.eglGetError(
        ) == egl.EGL_SUCCESS:
            # `eglInitialize` may or may not raise an exception on failure depending
            # on how PyOpenGL is configured. We therefore catch a `GLError` and also
            # manually check the output of `eglGetError()` here.
            try:
                initialized = egl.eglInitialize(display, None, None)
            except error.GLError:
                pass
            else:
                if initialized == egl.EGL_TRUE and egl.eglGetError(
                ) == egl.EGL_SUCCESS:
                    return display
    return egl.EGL_NO_DISPLAY
Beispiel #14
0
 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
Beispiel #15
0
 def initialize(self, width, height):
     pb_surf_attribs = egl_convert_to_int_array({
         egl.EGL_WIDTH: width,
         egl.EGL_HEIGHT: height,
     })
     self.egl_surface = egl.eglCreatePbufferSurface(self.egl_dpy,
                                                    self.egl_config,
                                                    pb_surf_attribs)
     if self.egl_surface == egl.EGL_NO_SURFACE:
         return False
     return True
Beispiel #16
0
 def gl_get_proc(self, proc):
     """This function is used to get a pointer to an OpenGL extension
     function. This is only necessary on the Windows platform, because the
     OpenGL implementation shipped with Windows only supports OpenGL
     version 1.1.
     PROTOTYPE:
      void * VidExt_GL_GetProcAddress(const char* Proc)"""
     address = egl.eglGetProcAddress(proc)
     if address is not None:
         return address
     else:
         log.error(f"Vidext: gl_get_proc({proc.decode()}) returns None")
Beispiel #17
0
def define_egl_ext_function(func_name, res_type, *arg_types):
    if hasattr(egl, func_name):
        return  # function already exists
    addr = egl.eglGetProcAddress(func_name)
    if addr is None:
        return  # function is not available
    else:
        proto = ctypes.CFUNCTYPE(res_type)
        proto.argtypes = arg_types
        globals()['proto__' + func_name] = proto    # avoid garbage collection
        func = proto(addr)
        setattr(egl, func_name, func)
Beispiel #18
0
def define_egl_ext_function(func_name, res_type, *arg_types):
    if hasattr(egl, func_name):
        return  # function already exists
    addr = egl.eglGetProcAddress(func_name)
    if addr is None:
        return  # function is not available
    else:
        proto = ctypes.CFUNCTYPE(res_type)
        proto.argtypes = arg_types
        globals()['proto__' + func_name] = proto  # avoid garbage collection
        func = proto(addr)
        setattr(egl, func_name, func)
Beispiel #19
0
 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))
         self.egl_dpy = 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())
         self.egl_surface = egl_surface
     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))
         self.egl_context = egl_context
     else:
         self.rollback(); return False
     # device seems to be working
     return True
Beispiel #20
0
    def swap_buffers(self):
        have_free_buffers = libgbm.gbm_surface_has_free_buffers(c_void_p(self.gbm_surface))
        if not have_free_buffers:
            raise Exception("No free buffers")

        egl.eglSwapBuffers(self.disp, self.surface)

        fb = self.lock_next()

        req = pykms.AtomicReq(self.card)
        req.add(self.root_plane, "FB_ID", fb.id)
        if req.test():
            raise Exception("Atomic test failed")
        if req.commit_sync():
            raise Exception("Atomic commit failed")

        self.free_prev()

        self.frame_count += 1
        if self.frame_count == 30:
            t = time.time()
            print("%.02f FPS" % (30.0/(t - self.last_swap)))
            self.last_swap = t
            self.frame_count = 0
Beispiel #21
0
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)]
Beispiel #22
0
    def video_quit(self):
        """This function closes any open rendering window and shuts down the
        video system. The default SDL implementation of this function calls
        SDL_QuitSubSystem(SDL_INIT_VIDEO). This function should be called from
        within the RomClosed() video plugin function.
        PROTOTYPE:
         m64p_error VidExt_Quit(void)"""
        log.debug("Vidext: video_quit()")

        # Nullify those EGL variables
        if self.egl_context != egl.EGL_NO_CONTEXT:
            egl.eglDestroyContext(self.egl_display, self.egl_context)
            self.egl_context = egl.EGL_NO_CONTEXT

        egl.eglMakeCurrent(self.egl_display, egl.EGL_NO_SURFACE,
                           egl.EGL_NO_SURFACE, egl.EGL_NO_CONTEXT)
        if self.egl_surface != egl.EGL_NO_SURFACE:
            egl.eglDestroySurface(self.egl_display, self.egl_surface)
            self.egl_surface = egl.EGL_NO_SURFACE

        if self.egl_display != egl.EGL_NO_DISPLAY:
            egl.eglTerminate(self.egl_display)
            self.egl_display = egl.EGL_NO_DISPLAY

        self.new_surface = True

        ## GTK
        # Restore the good old name of the frontend
        if self.title != None:
            self.window.set_title(self.title)
        self.window.set_resizable(True)

        # First we must lift the restriction on the minimum size of the widget
        self.window.canvas.set_size_request(1, 1)

        #XXX: Workaround because GTK is too slow
        time.sleep(0.1)

        self.window.resize(self.former_size[0], self.former_size[1])
        return wrp_dt.m64p_error.M64ERR_SUCCESS.value
Beispiel #23
0
 def make_surface_not_current(self):
     egl.eglMakeCurrent(self.egl_dpy, egl.EGL_NO_SURFACE, egl.EGL_NO_SURFACE, egl.EGL_NO_CONTEXT)
Beispiel #24
0
 def swap_buffers(self):
     egl.eglSwapBuffers(self.disp, self.surface)
    pass

# pylint: disable=g-import-not-at-top

from OpenGL import EGL
from OpenGL import error
from six.moves import range

# From the EGL_EXT_device_enumeration extension.
EGLDeviceEXT = ctypes.c_void_p
PFNEGLQUERYDEVICESEXTPROC = ctypes.CFUNCTYPE(EGL.EGLBoolean, EGL.EGLint,
                                             ctypes.POINTER(EGLDeviceEXT),
                                             ctypes.POINTER(EGL.EGLint))
try:
    _eglQueryDevicesEXT = PFNEGLQUERYDEVICESEXTPROC(  # pylint: disable=invalid-name
        EGL.eglGetProcAddress('eglQueryDevicesEXT'))
except TypeError:
    raise ImportError('eglQueryDevicesEXT is not available.')

# From the EGL_EXT_platform_device extension.
EGL_PLATFORM_DEVICE_EXT = 0x313F
PFNEGLGETPLATFORMDISPLAYEXTPROC = ctypes.CFUNCTYPE(EGL.EGLDisplay, EGL.EGLenum,
                                                   ctypes.c_void_p,
                                                   ctypes.POINTER(EGL.EGLint))
try:
    eglGetPlatformDisplayEXT = PFNEGLGETPLATFORMDISPLAYEXTPROC(  # pylint: disable=invalid-name
        EGL.eglGetProcAddress('eglGetPlatformDisplayEXT'))
except TypeError:
    raise ImportError('eglGetPlatformDisplayEXT is not available.')

Beispiel #26
0
        def __init__(self, width=400, height=400):
            self.width = width
            self.height = height
            from OpenGL import EGL
            self.EGL = EGL
            self.display = EGL.eglGetDisplay(EGL.EGL_NO_DISPLAY)
            major = np.zeros(1, "i4")
            minor = np.zeros(1, "i4")
            EGL.eglInitialize(self.display, major, minor)
            num_configs = np.zeros(1, "i4")
            configs = (EGL.EGLConfig*1)()
            # Now we create our necessary bits.
            config_attribs = np.array([
              EGL.EGL_SURFACE_TYPE, EGL.EGL_PBUFFER_BIT,
              EGL.EGL_ALPHA_SIZE, 8,
              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,
            ], dtype="i4")
            EGL.eglChooseConfig(self.display, config_attribs, configs, 1, num_configs)
            self.config = configs[0]

            pbuffer_attribs = np.array([
              EGL.EGL_WIDTH, width,
              EGL.EGL_HEIGHT, height,
              EGL.EGL_NONE
            ], dtype="i4")
            self.surface = EGL.eglCreatePbufferSurface(self.display, self.config, pbuffer_attribs)

            EGL.eglBindAPI(EGL.EGL_OPENGL_API)
            
            self.context = EGL.eglCreateContext(self.display, self.config, EGL.EGL_NO_CONTEXT, None)

            EGL.eglMakeCurrent(self.display, self.surface, self.surface, self.context)
            GL.glEnable(GL.GL_DEPTH_TEST)
            self.clear()
Beispiel #27
0
 def make_current(self, egl_context):
     return egl.eglMakeCurrent(self.egl_dpy, self.egl_surface,
                               self.egl_surface, egl_context)
Beispiel #28
0
 def get_egl_display(self):
     return egl.eglGetPlatformDisplayEXT(EGL_PLATFORM_DEVICE_EXT, self.egl_dev, None)
Beispiel #29
0
 def swap_buffers(self):
     egl.eglSwapBuffers(self.disp, self.surface)
Beispiel #30
0
 def make_current(self, egl_context):
     return egl.eglMakeCurrent(self.egl_dpy, self.egl_surface, self.egl_surface, egl_context)
Beispiel #31
0
 def release(self):
     egl.eglDestroySurface(self.egl_dpy, self.egl_surface)
Beispiel #32
0
    def __init__(self, width=640, height=480, fullscreen=False, aspect=None):
        self.gl = gl
        libbcm_host.bcm_host_init()
        display = libbcm_host.vc_dispmanx_display_open(0)

        mode = DISPMANX_MODEINFO_T()
        libbcm_host.vc_dispmanx_display_get_info(display, byref(mode))
        print("Display mode: %dx%d" % (mode.width, mode.height))

        self.disp = egl.eglGetDisplay(egl.EGL_DEFAULT_DISPLAY)
        attribList = arrays.GLintArray.asArray([
            egl.EGL_RENDERABLE_TYPE, egl.EGL_OPENGL_ES2_BIT,
            egl.EGL_SURFACE_TYPE, egl.EGL_WINDOW_BIT,
            #egl.EGL_COLOR_BUFFER_TYPE, egl.EGL_RGB_BUFFER,
            egl.EGL_RED_SIZE, 8,
            egl.EGL_GREEN_SIZE, 8,
            egl.EGL_BLUE_SIZE, 8,
            egl.EGL_ALPHA_SIZE, 8,
            egl.EGL_NONE
        ])
        ctxAttrib = arrays.GLintArray.asArray([
            egl.EGL_CONTEXT_CLIENT_VERSION, 2,
            egl.EGL_NONE
        ])
        egl.eglInitialize(self.disp, None, None)
        config = egl.EGLConfig()
        num_configs = ctypes.c_long()
        egl.eglChooseConfig(self.disp, attribList, byref(config), 1, byref(num_configs))

        ret = ctypes.c_int()
        egl.eglBindAPI(egl.EGL_OPENGL_ES_API)

        update = libbcm_host.vc_dispmanx_update_start(0)
        rectDst = VC_RECT_T()
        rectDst.x = rectDst.y = 0
        rectDst.width = mode.width
        rectDst.height = mode.height

        rectSrc = VC_RECT_T()
        rectSrc.x = rectDst.y = 0
        rectSrc.width = mode.width << 16
        rectSrc.height = mode.height << 16

        alpha = VC_DISPMANX_ALPHA_T()
        alpha.flags = 1 << 16  # premultiplied alpha
        alpha.opacity = 255
        alpha.mask = 0

        self.nativeWindow = EGL_DISPMANX_WINDOW_T()
        self.nativeWindow.width = mode.width
        self.nativeWindow.height = mode.height

        layer = 0
        self.nativeWindow.element = libbcm_host.vc_dispmanx_element_add(
            update, display, layer, byref(rectDst), 0, byref(rectSrc),
            0, byref(alpha), 0, 0)

        libbcm_host.vc_dispmanx_update_submit_sync(update)
        libbcm_host.vc_dispmanx_display_close(display)

        self.surface = egl.eglCreateWindowSurface(self.disp, config, byref(self.nativeWindow), None)
        self.context = egl.eglCreateContext(self.disp, config, egl.EGL_NO_CONTEXT, ctxAttrib)
        assert egl.eglMakeCurrent(self.disp, self.surface, self.surface, self.context)

        egl.eglSwapInterval(self.disp, 1)

        gl.glBindFramebuffer(gl.GL_FRAMEBUFFER, 0)

        for i in range(5):
            gl.glClearColor(0, 0, 0, 1.0)
            gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT)
            egl.eglSwapBuffers(self.disp, self.surface)

        self.win_width = self.width = mode.width
        self.win_height = self.height = mode.height

        gl.glViewport(0, 0, self.win_width, self.win_height)

        BaseDisplay.__init__(self, mode.width, mode.height, True, aspect)

        # Transparent layer
        self.clear_color = self.TRANSPARENT

        self._initialize()
Beispiel #33
0
 def swap_buffers(self):
     egl.eglSwapBuffers(self.disp, self.surface)
     self.frame_count += 1
Beispiel #34
0
    def __init__(self, width=640, height=480, fullscreen=False, aspect=None):
        self.gl = gl
        self.bo_next = self.bo_prev = None
        self.last_swap = time.time()
        self.frame_count = 0

        self.disp = egl.eglGetPlatformDisplay(EGL_PLATFORM_SURFACELESS_MESA,
                                              egl.EGL_DEFAULT_DISPLAY, None)
        if not self.disp:
            raise Exception("Failed to get egl display")

        BaseDisplay.__init__(self, width, height, True, aspect)

        attribList = arrays.GLintArray.asArray([
            egl.EGL_RENDERABLE_TYPE, egl.EGL_OPENGL_ES2_BIT,
            egl.EGL_SURFACE_TYPE, egl.EGL_PBUFFER_BIT, egl.EGL_RED_SIZE, 8,
            egl.EGL_GREEN_SIZE, 8, egl.EGL_BLUE_SIZE, 8, egl.EGL_ALPHA_SIZE, 8,
            egl.EGL_NONE
        ])
        ctxAttrib = arrays.GLintArray.asArray(
            [egl.EGL_CONTEXT_CLIENT_VERSION, 2, egl.EGL_NONE])
        surfaceAttrib = arrays.GLintArray.asArray(
            [egl.EGL_WIDTH, width, egl.EGL_HEIGHT, height, egl.EGL_NONE])

        egl.eglInitialize(self.disp, None, None)
        config = egl.EGLConfig()
        num_configs = ctypes.c_long()
        egl.eglChooseConfig(self.disp, attribList, byref(config), 1,
                            byref(num_configs))

        ret = ctypes.c_int()
        egl.eglBindAPI(egl.EGL_OPENGL_ES_API)

        self.context = egl.eglCreateContext(self.disp, config,
                                            egl.EGL_NO_CONTEXT, ctxAttrib)
        self.surface = egl.eglCreatePbufferSurface(self.disp, config,
                                                   surfaceAttrib)
        assert egl.eglMakeCurrent(self.disp, self.surface, self.surface,
                                  self.context)

        gl.glBindFramebuffer(gl.GL_FRAMEBUFFER, 0)

        gl.glClearColor(0, 0, 0, 0.0)
        gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT)

        self.win_width = self.width = width
        self.win_height = self.height = height

        gl.glViewport(0, 0, self.win_width, self.win_height)

        self.clear_color = self.TRANSPARENT

        self._initialize()
Beispiel #35
0
 def get_proc_address(self, s):
     return egl.eglGetProcAddress(s)
Beispiel #36
0
    def __init__(self, width=640, height=480, fullscreen=False, aspect=None):
        self.gl = gl
        self.bo_next = self.bo_prev = None
        self.last_swap = time.time()
        self.frame_count = 0

        self.card = pykms.Card()
        print("DRM fd: %d" % self.card.fd)
        print("Has atomic: %r" % self.card.has_atomic)

        self.render_fd = -1

        render_name = libdrm.drmGetRenderDeviceNameFromFd(self.card.fd)
        print("Render device name: %r" % render_name)

        if render_name:
            try:
                self.render_fd = os.open(render_name, os.O_RDWR)
            except OSError:
                print("Render node not available")

        print("Render fd: %d" % self.render_fd)
        self.gbm_dev = libgbm.gbm_create_device(self.card.fd)
        if not self.gbm_dev:
            raise Exception("Failed to create GBM device")

        print("GBM dev: %x" % self.gbm_dev)

        self.res = pykms.ResourceManager(self.card)
        self.conn = self.res.reserve_connector()
        self.crtc = self.res.reserve_crtc(self.conn)
        self.root_plane = self.res.reserve_generic_plane(self.crtc)
        if not self.root_plane:
            raise Exception("Root plane not available")

        self.mode = mode = self.conn.get_default_mode()

        BaseDisplay.__init__(self, mode.hdisplay, mode.vdisplay, True, aspect)

        self.fps = 1000 * mode.clock / (mode.htotal * mode.vtotal)
        print("Creating GBM surface (%dx%d %f Hz)" % (mode.hdisplay, mode.vdisplay, self.fps))

        self.gbm_surface = libgbm.gbm_surface_create(
            c_void_p(self.gbm_dev), mode.hdisplay, mode.vdisplay,
            GBM_FORMAT_XRGB8888, GBM_BO_USE_SCANOUT | GBM_BO_USE_RENDERING)

        if not self.gbm_surface:
            raise Exception("Failed to create GBM surface")
        print("GBM surface: %x" % self.gbm_surface)

        self.disp = egl.eglGetDisplay(self.gbm_dev)
        if not self.disp:
            raise Exception("Failed to get egl display")

        attribList = arrays.GLintArray.asArray([
            egl.EGL_RENDERABLE_TYPE, egl.EGL_OPENGL_ES2_BIT,
            egl.EGL_SURFACE_TYPE, egl.EGL_WINDOW_BIT,
            #egl.EGL_COLOR_BUFFER_TYPE, egl.EGL_RGB_BUFFER,
            egl.EGL_RED_SIZE, 8,
            egl.EGL_GREEN_SIZE, 8,
            egl.EGL_BLUE_SIZE, 8,
            egl.EGL_ALPHA_SIZE, 0,
            egl.EGL_NONE
        ])
        ctxAttrib = arrays.GLintArray.asArray([
            egl.EGL_CONTEXT_CLIENT_VERSION, 2,
            egl.EGL_NONE
        ])
        egl.eglInitialize(self.disp, None, None)
        config = egl.EGLConfig()
        num_configs = ctypes.c_long()
        egl.eglChooseConfig(self.disp, attribList, byref(config), 1, byref(num_configs))

        ret = ctypes.c_int()
        egl.eglBindAPI(egl.EGL_OPENGL_ES_API)

        self.surface = egl.eglCreateWindowSurface(self.disp, config, c_void_p(self.gbm_surface), None)
        self.context = egl.eglCreateContext(self.disp, config, egl.EGL_NO_CONTEXT, ctxAttrib)
        assert egl.eglMakeCurrent(self.disp, self.surface, self.surface, self.context)

        egl.eglSwapInterval(self.disp, 1)

        gl.glBindFramebuffer(gl.GL_FRAMEBUFFER, 0)

        gl.glClearColor(0, 0, 0, 1.0)
        gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT)

        #fb = self.lock_next()
        #self.crtc.set_mode(self.conn, fb, mode)

        modeb = mode.to_blob(self.card)

        req = pykms.AtomicReq(self.card)
        req.add(self.conn, "CRTC_ID", self.crtc.id)
        req.add(self.crtc, {"ACTIVE": 1,
                            "MODE_ID": modeb.id})
        if req.test(allow_modeset = True):
            raise Exception("Atomic test failed")
        if req.commit_sync(allow_modeset = True):
            raise Exception("Atomic commit failed")

        self.win_width = self.width = mode.hdisplay
        self.win_height = self.height = mode.vdisplay

        gl.glViewport(0, 0, self.win_width, self.win_height)

        self.clear_color = self.BLACK

        self._initialize()
Beispiel #37
0
 def name(self):
     if not hasattr(egl, 'eglQueryDeviceStringEXT'):
         return "EGL device unknown"
     devstr = egl.eglQueryDeviceStringEXT(self.egl_dev, EGL_DRM_DEVICE_FILE_EXT)
     return "EGL device " + devstr.decode('ASCII')
Beispiel #38
0
 def release(self):
     egl.eglDestroySurface(self.egl_dpy, self.egl_surface)
Beispiel #39
0
 def get_egl_display(self):
     return egl.eglGetPlatformDisplayEXT(EGL_PLATFORM_DEVICE_EXT,
                                         self.egl_dev, None)
Beispiel #40
0
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 = 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))

    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)
Beispiel #41
0
    def __init__(self, width=640, height=480, fullscreen=False, aspect=None):
        self.gl = gl
        libbcm_host.bcm_host_init()
        display = libbcm_host.vc_dispmanx_display_open(0)

        mode = DISPMANX_MODEINFO_T()
        libbcm_host.vc_dispmanx_display_get_info(display, byref(mode))
        print("Display mode: %dx%d" % (mode.width, mode.height))

        self.disp = egl.eglGetDisplay(egl.EGL_DEFAULT_DISPLAY)
        attribList = arrays.GLintArray.asArray([
            egl.EGL_RENDERABLE_TYPE, egl.EGL_OPENGL_ES2_BIT,
            egl.EGL_SURFACE_TYPE, egl.EGL_WINDOW_BIT,
            #egl.EGL_COLOR_BUFFER_TYPE, egl.EGL_RGB_BUFFER,
            egl.EGL_RED_SIZE, 8,
            egl.EGL_GREEN_SIZE, 8,
            egl.EGL_BLUE_SIZE, 8,
            egl.EGL_ALPHA_SIZE, 8,
            egl.EGL_NONE
        ])
        ctxAttrib = arrays.GLintArray.asArray([
            egl.EGL_CONTEXT_CLIENT_VERSION, 2,
            egl.EGL_NONE
        ])
        egl.eglInitialize(self.disp, None, None)
        config = egl.EGLConfig()
        num_configs = ctypes.c_long()
        egl.eglChooseConfig(self.disp, attribList, byref(config), 1, byref(num_configs))

        ret = ctypes.c_int()
        egl.eglBindAPI(egl.EGL_OPENGL_ES_API)

        update = libbcm_host.vc_dispmanx_update_start(0)
        rectDst = VC_RECT_T()
        rectDst.x = rectDst.y = 0
        rectDst.width = mode.width
        rectDst.height = mode.height

        rectSrc = VC_RECT_T()
        rectSrc.x = rectDst.y = 0
        rectSrc.width = mode.width << 16
        rectSrc.height = mode.height << 16

        alpha = VC_DISPMANX_ALPHA_T()
        alpha.flags = 1 << 16  # premultiplied alpha
        alpha.opacity = 255
        alpha.mask = 0

        self.nativeWindow = EGL_DISPMANX_WINDOW_T()
        self.nativeWindow.width = mode.width
        self.nativeWindow.height = mode.height

        layer = 0
        self.nativeWindow.element = libbcm_host.vc_dispmanx_element_add(
            update, display, layer, byref(rectDst), 0, byref(rectSrc),
            0, byref(alpha), 0, 0)

        libbcm_host.vc_dispmanx_update_submit_sync(update)
        libbcm_host.vc_dispmanx_display_close(display)

        self.surface = egl.eglCreateWindowSurface(self.disp, config, byref(self.nativeWindow), None)
        self.context = egl.eglCreateContext(self.disp, config, egl.EGL_NO_CONTEXT, ctxAttrib)
        assert egl.eglMakeCurrent(self.disp, self.surface, self.surface, self.context)

        egl.eglSwapInterval(self.disp, 1)

        gl.glBindFramebuffer(gl.GL_FRAMEBUFFER, 0)

        for i in range(5):
            gl.glClearColor(0, 0, 0, 1.0)
            gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT)
            egl.eglSwapBuffers(self.disp, self.surface)

        self.win_width = self.width = mode.width
        self.win_height = self.height = mode.height

        gl.glViewport(0, 0, self.win_width, self.win_height)

        BaseDisplay.__init__(self, mode.width, mode.height, True, aspect)

        # Transparent layer
        self.clear_color = self.TRANSPARENT

        self._initialize()
Beispiel #42
0
 def name(self):
     if not hasattr(egl, 'eglQueryDeviceStringEXT'):
         return "EGL device unknown"
     devstr = egl.eglQueryDeviceStringEXT(self.egl_dev,
                                          EGL_DRM_DEVICE_FILE_EXT)
     return "EGL device " + devstr.decode('ASCII')
Beispiel #43
0
def init_egl(width, height):
    prev_display = os.environ.pop('DISPLAY', None)
    dpy = EGL.eglGetDisplay(EGL.EGL_DEFAULT_DISPLAY)
    if prev_display is not None:
        os.environ['DISPLAY'] = prev_display

    major = ctypes.c_long()
    minor = ctypes.c_long()
    EGL.eglInitialize(dpy, major, minor)

    attrs = EGL.arrays.GLintArray.asArray([
        EGL.EGL_SURFACE_TYPE, EGL.EGL_PBUFFER_BIT, EGL.EGL_BLUE_SIZE, 8,
        EGL.EGL_RED_SIZE, 8, EGL.EGL_GREEN_SIZE, 8, EGL.EGL_ALPHA_SIZE, 8,
        EGL.EGL_DEPTH_SIZE, 24, EGL.EGL_COLOR_BUFFER_TYPE, EGL.EGL_RGB_BUFFER,
        EGL.EGL_RENDERABLE_TYPE, EGL.EGL_OPENGL_BIT, EGL.EGL_CONFORMANT,
        EGL.EGL_OPENGL_BIT, EGL.EGL_NONE
    ])

    configs = (EGL.EGLConfig * 1)()
    num_configs = ctypes.c_long()
    EGL.eglChooseConfig(dpy, attrs, configs, 1, num_configs)

    EGL.eglBindAPI(EGL.EGL_OPENGL_API)

    attrs = [EGL.EGL_WIDTH, width, EGL.EGL_HEIGHT, height, EGL.EGL_NONE]
    surface = EGL.eglCreatePbufferSurface(dpy, configs[0], attrs)

    attrs = [
        EGL.EGL_CONTEXT_MAJOR_VERSION, 4, EGL.EGL_CONTEXT_MINOR_VERSION, 0,
        EGL.EGL_CONTEXT_OPENGL_PROFILE_MASK,
        EGL.EGL_CONTEXT_OPENGL_CORE_PROFILE_BIT, EGL.EGL_NONE
    ]
    attrs = [EGL.EGL_NONE]
    ctx = EGL.eglCreateContext(dpy, configs[0], EGL.EGL_NO_CONTEXT, attrs)

    EGL.eglMakeCurrent(dpy, surface, surface, ctx)

    return dpy
Beispiel #44
0
    def __init__(self, width=1024, height=1024):
        from OpenGL import EGL

        self.EGL = EGL
        self.display = EGL.eglGetDisplay(EGL.EGL_DEFAULT_DISPLAY)
        major = np.zeros(1, "i4")
        minor = np.zeros(1, "i4")
        EGL.eglInitialize(self.display, major, minor)
        num_configs = np.zeros(1, "i4")
        config = EGL.EGLConfig()
        # Now we create our necessary bits.
        config_attribs = np.array(
            [
                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,
                8,
                EGL.EGL_RENDERABLE_TYPE,
                EGL.EGL_OPENGL_BIT,
                EGL.EGL_NONE,
            ],
            dtype="i4",
        )
        self.config = EGL.eglChooseConfig(
            self.display, config_attribs, config, 1, num_configs
        )

        pbuffer_attribs = np.array(
            [EGL.EGL_WIDTH, width, EGL.EGL_HEIGHT, height, EGL.EGL_NONE], dtype="i4"
        )
        self.surface = EGL.eglCreatePbufferSurface(
            self.display, self.config, pbuffer_attribs
        )
        EGL.eglBindAPI(EGL.EGL_OPENGL_API)

        self.context = EGL.eglCreateContext(
            self.display, self.config, EGL.EGL_NO_CONTEXT, None
        )

        EGL.eglMakeCurrent(self.display, self.surface, self.surface, self.context)

        GL.glClearColor(0.0, 0.0, 0.0, 0.0)
        GL.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT)
Beispiel #45
0
def finish_egl(dpy):
    EGL.eglMakeCurrent(dpy, EGL.EGL_NO_SURFACE, EGL.EGL_NO_SURFACE,
                       EGL.EGL_NO_CONTEXT)
Beispiel #46
0
 def get_proc_address(self, s):
     return egl.eglGetProcAddress(s)
Beispiel #47
0
        gl.glBindFramebuffer(gl.GL_FRAMEBUFFER, 0)

        for i in range(5):
            gl.glClearColor(0, 0, 0, 1.0)
            gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT)
            egl.eglSwapBuffers(self.disp, self.surface)

        self.win_width = self.width = mode.width
        self.win_height = self.height = mode.height

        gl.glViewport(0, 0, self.win_width, self.win_height)

        BaseDisplay.__init__(self, mode.width, mode.height, True, aspect)

        # Transparent layer
        self.clear_color = self.TRANSPARENT

        self._initialize()

    def swap_buffers(self):
        egl.eglSwapBuffers(self.disp, self.surface)

if __name__ == "__main__":
    d = Display()
    while True:
        for color in (1.,0.,0.,1.), (0.,1.,0.,1.), (0.,0.,1.,1.):
            gl.glClearColor(*color)
            gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT)
            egl.eglSwapBuffers(d.disp, d.surface)
            time.sleep(0.5)