Ejemplo n.º 1
0
    def __init__(self, width=1024, height=1024, **kwargs):
        super(EGLRenderingContext, self).__init__(width, height, **kwargs)

        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_RED_SIZE,
                8,
                EGL.EGL_GREEN_SIZE,
                8,
                EGL.EGL_BLUE_SIZE,
                8,
                EGL.EGL_DEPTH_SIZE,
                24,
                EGL.EGL_STENCIL_SIZE,
                8,
                EGL.EGL_COLOR_BUFFER_TYPE,
                EGL.EGL_RGB_BUFFER,
                EGL.EGL_SURFACE_TYPE,
                EGL.EGL_PBUFFER_BIT,
                EGL.EGL_RENDERABLE_TYPE,
                EGL.EGL_OPENGL_BIT,
                EGL.EGL_CONFIG_CAVEAT,
                EGL.EGL_NONE,
                EGL.EGL_NONE,
            ],
            dtype="i4",
        )
        EGL.eglChooseConfig(
            self.display, config_attribs, pointer(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, config, pbuffer_attribs
        )
        EGL.eglBindAPI(EGL.EGL_OPENGL_API)

        self.context = EGL.eglCreateContext(
            self.display, 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)
Ejemplo n.º 2
0
    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
Ejemplo n.º 3
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()
Ejemplo n.º 4
0
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')))
Ejemplo n.º 5
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
Ejemplo n.º 6
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 = 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)
Ejemplo n.º 7
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()
Ejemplo n.º 8
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
Ejemplo n.º 9
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
Ejemplo n.º 10
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()
Ejemplo n.º 11
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()
Ejemplo n.º 12
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()
Ejemplo n.º 13
0
    def video_set_mode(self,
                       width,
                       height,
                       bits,
                       screenmode,
                       flags,
                       refreshrate=None):
        """This function creates a rendering window or switches into a
        fullscreen video mode. Any desired OpenGL attributes should be set
        before calling this function.
        PROTOTYPE:
         m64p_error VidExt_SetVideoMode(int Width, int Height, int BitsPerPixel,
                          m64p_video_mode ScreenMode, m64p_video_flags Flags)"""
        log.debug(
            f"Vidext: video_set_mode(width: {str(width)}, height: {str(height)}, bits: {str(bits)}, screenmode: {wrp_dt.m64p_video_mode(screenmode).name}, flags:{wrp_dt.m64p_video_flags(flags).name}"
        )

        self.width = width
        self.height = height

        self.former_size = self.window.get_size()
        # Needed for get_preferred_size() to work
        self.window.set_resizable(False)
        # It doesn't just get the preferred size, it DOES resize the window too
        self.window.canvas.get_preferred_size()
        # Necessary so that we tell the GUI to not shrink the window further than the size of the widget set by mupen64plus
        self.window.canvas.set_size_request(width, height)
        # XXX: Workaround because GTK is too slow.
        time.sleep(0.1)

        # The window is resizable if gfx plugin allows it
        if flags == wrp_dt.m64p_video_flags.M64VIDEOFLAG_SUPPORT_RESIZING.value:
            self.window.set_resizable(True)

        log.debug(f'Double buffer: {self.double_buffer}')
        log.debug(f'Buffer size: {self.buffer_size}')
        log.debug(f'Depth size: {self.depth_size}')
        log.debug(f'Red size: {self.red_size}')
        log.debug(f'Green size: {self.green_size}')
        log.debug(f'Blue size: {self.blue_size}')
        log.debug(f'Alpha size: {self.alpha_size}')
        log.debug(f'Swap control: {self.swap_control}')
        log.debug(f'Multisample buffer: {self.multisample_buffer}')
        log.debug(f'Multisample samples: {self.multisample_samples}')
        log.debug(f'OpenGL: {self.context_major}.{self.context_minor}')
        log.debug(f'Context profile: {self.profile_bit}')

        self.egl_attributes = gl.arrays.GLintArray.asArray([
            egl.EGL_BUFFER_SIZE, self.buffer_size, egl.EGL_DEPTH_SIZE,
            self.depth_size, egl.EGL_RED_SIZE, self.red_size,
            egl.EGL_GREEN_SIZE, self.green_size, egl.EGL_BLUE_SIZE,
            self.blue_size, egl.EGL_ALPHA_SIZE, self.alpha_size,
            egl.EGL_SAMPLE_BUFFERS, self.multisample_buffer, egl.EGL_SAMPLES,
            self.multisample_samples, egl.EGL_RENDERABLE_TYPE, self.api_bit,
            egl.EGL_NONE
        ])

        self.window_attributes = gl.arrays.GLintArray.asArray(
            [egl.EGL_RENDER_BUFFER, self.double_buffer, egl.EGL_NONE])

        self.opengl_version = gl.arrays.GLintArray.asArray([
            egl.EGL_CONTEXT_MAJOR_VERSION, self.context_major,
            egl.EGL_CONTEXT_MINOR_VERSION, self.context_minor,
            egl.EGL_CONTEXT_OPENGL_PROFILE_MASK, self.profile_bit, egl.EGL_NONE
        ])

        # Return a list of EGL frame buffer configurations that match specified attributes
        num_configs = c.c_long()
        self.egl_config = (egl.EGLConfig * 2)()
        config_chosen = egl.eglChooseConfig(self.egl_display,
                                            self.egl_attributes,
                                            self.egl_config, 2, num_configs)
        if config_chosen == None:
            log.error(f"eglChooseConfig() returned error: {egl.eglGetError()}")
            return wrp_dt.m64p_error.M64ERR_INVALID_STATE.value

        if self.new_surface:
            log.info("VidExtFuncSetMode: Initializing surface")
            self.egl_surface = egl.eglCreateWindowSurface(
                self.egl_display, self.egl_config[0], self.window_handle,
                self.window_attributes)

            self.egl_context = egl.eglCreateContext(self.egl_display,
                                                    self.egl_config[0],
                                                    egl.EGL_NO_CONTEXT,
                                                    self.opengl_version)
            if self.egl_context == egl.EGL_NO_CONTEXT:
                raise RuntimeError('Unable to create context')
            try:
                egl.eglMakeCurrent(self.egl_display, self.egl_surface,
                                   self.egl_surface, self.egl_context)
                egl.eglSwapInterval(self.egl_display, self.swap_control)
                egl.eglSwapBuffers(self.egl_display, self.egl_surface)
                retval = True

            except:
                log.error(
                    f"eglMakeCurrent() returned error: {egl.eglGetError()}")

            self.new_surface = False

        else:
            log.error("VidExtFuncSetMode called before surface has been set")
            return wrp_dt.m64p_error.M64ERR_INVALID_STATE.value

        if retval == True:
            log.debug(f"Vidext: video_set_mode() has reported M64ERR_SUCCESS")
            return wrp_dt.m64p_error.M64ERR_SUCCESS.value
        else:
            log.error(
                f"Vidext: video_set_mode() has reported M64ERR_SYSTEM_FAIL")
            return wrp_dt.m64p_error.M64ERR_SYSTEM_FAIL.value