Beispiel #1
0
  def create_surface(self, x=0, y=0, w=0, h=0):
    #Set the viewport position and size
    dst_rect = c_ints((x, y, w, h))
    src_rect = c_ints((x, y, w << 16, h << 16))

    self.dispman_display = bcm.vc_dispmanx_display_open(0) #LCD setting
    self.dispman_update = bcm.vc_dispmanx_update_start(0)
    self.dispman_element = bcm.vc_dispmanx_element_add(
      self.dispman_update,
      self.dispman_display,
      0, ctypes.byref(dst_rect),
      0, ctypes.byref(src_rect),
      DISPMANX_PROTECTION_NONE,
      0, 0, 0)

    nativewindow = c_ints((self.dispman_element, w, h + 1));
    bcm.vc_dispmanx_update_submit_sync(self.dispman_update)

    nw_p = ctypes.pointer(nativewindow)
    self.nw_p = nw_p

    self.surface = openegl.eglCreateWindowSurface(self.display, self.config, self.nw_p, 0)
    assert self.surface != EGL_NO_SURFACE

    r = openegl.eglMakeCurrent(self.display, self.surface, self.surface,
                               self.context)
    assert r

    #Create viewport
    opengles.glViewport(0, 0, w, h)
Beispiel #2
0
    def createBuffer(self):
        self.vc_image_ptr = ctypes.c_uint()
        self.pitch = self.imgw * 3
        assert self.pitch % 32 == 0
        self.imgtype = VC_IMAGE_RGB888

        with self.bcmUpdate() as dispman_update:
            self.img_resource = bcm.vc_dispmanx_resource_create(
                self.imgtype, self.imgw, self.imgh, ctypes.byref(self.vc_image_ptr)
            )
            assert self.img_resource != 0
            self.src_rect = c_ints((0, 0, self.imgw << 16, self.imgh << 16))
            # scale width so that aspect ratio is retained with display height - might crop right part of the bg
            dstw = int(self.h.value * self.imgw / self.imgh)
            self.dst_rect = c_ints((0, 0, dstw, self.h))

            self.alpha = c_ints((DISPMANX_FLAGS_ALPHA_FROM_SOURCE, 255, 0))

            self.img_element = bcm.vc_dispmanx_element_add(
                dispman_update,
                self.dispman_display,
                self.layer,
                ctypes.byref(self.dst_rect),
                self.img_resource,
                ctypes.byref(self.src_rect),
                DISPMANX_PROTECTION_NONE,
                ctypes.byref(self.alpha),
                0,
                DISPMANX_NO_ROTATE,
            )
            assert self.img_element != 0
Beispiel #3
0
def patched_create_surface(self, x=0, y=0, w=0, h=0, layer=0):
  #Set the viewport position and size
  dst_rect = c_ints((x, y, w, h))
  src_rect = c_ints((x, y, w << 16, h << 16))

  self.dispman_display = bcm.vc_dispmanx_display_open(0) #LCD setting
  self.dispman_update = bcm.vc_dispmanx_update_start(0)
  alpha = ctypes.byref(c_ints((1<<16, 0, 0)))

  self.dispman_element = bcm.vc_dispmanx_element_add(
    self.dispman_update,
    self.dispman_display,
    layer, ctypes.byref(dst_rect),
    0, ctypes.byref(src_rect),
    DISPMANX_PROTECTION_NONE,
    alpha, 0, 0)

  nativewindow = c_ints((self.dispman_element, w, h + 1))
  bcm.vc_dispmanx_update_submit_sync(self.dispman_update)

  nw_p = ctypes.pointer(nativewindow)
  self.nw_p = nw_p
    
  self.surface = openegl.eglCreateWindowSurface(self.display, self.config, self.nw_p, 0)

  assert self.surface != EGL_NO_SURFACE
  r = openegl.eglMakeCurrent(self.display, self.surface, self.surface,
                             self.context)
  assert r

  #Create viewport
  opengles.glViewport(0, 0, w, h)
Beispiel #4
0
    def createBuffer(self):
        self.vc_image_ptr = ctypes.c_uint()
        self.pitch = self.imgw * 3;
        assert(self.pitch % 32 == 0)
        self.imgtype = VC_IMAGE_RGB888

        with self.bcmUpdate() as dispman_update:
            self.img_resource = bcm.vc_dispmanx_resource_create(
                self.imgtype,
                self.imgw,
                self.imgh,
                ctypes.byref(self.vc_image_ptr))
            assert (self.img_resource != 0)
            self.src_rect = c_ints((0, 0, self.imgw << 16, self.imgh << 16))
            # scale width so that aspect ratio is retained with display height - might crop right part of the bg
            dstw = int(self.h.value * self.imgw / self.imgh)
            self.dst_rect = c_ints((0, 0, dstw, self.h))

            self.alpha = c_ints((DISPMANX_FLAGS_ALPHA_FROM_SOURCE, 255, 0))

            self.img_element = bcm.vc_dispmanx_element_add(dispman_update,
                                                    self.dispman_display,
                                                    self.layer,
                                                    ctypes.byref(self.dst_rect),
                                                    self.img_resource,
                                                    ctypes.byref(self.src_rect),
                                                    DISPMANX_PROTECTION_NONE,
                                                    ctypes.byref(self.alpha),
                                                    0,
                                                    DISPMANX_NO_ROTATE)
            assert(self.img_element != 0)
Beispiel #5
0
    def createBuffer(self):
        vc_image_ptr = ctypes.c_uint()

        self.pitch = self.imgw // 16 * 16 * 3
        alignedHeight = self.imgh // 16 * 16
        self.imgtype = VC_IMAGE_RGB888

        with self.bcmUpdate() as dispman_update:
            self.img_resource = bcm.vc_dispmanx_resource_create(
                self.imgtype, self.imgw | (self.pitch << 16),
                self.imgh | (alignedHeight << 16), ctypes.byref(vc_image_ptr))
            assert (self.img_resource != 0)

            src_rect = c_ints(
                (0 << 16, 0 << 16, self.imgw << 16, self.imgh << 16))
            dst_rect = c_ints((0, 0, self.w, self.h))

            alpha = c_ints((DISPMANX_FLAGS_ALPHA_FROM_SOURCE, 255, 0))

            self.img_element = bcm.vc_dispmanx_element_add(
                dispman_update, self.dispman_display, self.layer,
                ctypes.byref(dst_rect), self.img_resource,
                ctypes.byref(src_rect), DISPMANX_PROTECTION_NONE,
                ctypes.byref(alpha), 0, DISPMANX_NO_ROTATE)
            assert (self.img_element != 0)
Beispiel #6
0
    def create_display(self,
                       x=0,
                       y=0,
                       w=0,
                       h=0,
                       depth=24,
                       samples=4,
                       layer=0,
                       display_config=DISPLAY_CONFIG_DEFAULT):
        self.display = openegl.eglGetDisplay(EGL_DEFAULT_DISPLAY)
        assert self.display != EGL_NO_DISPLAY

        self.display_config = display_config

        r = openegl.eglInitialize(self.display, 0, 0)
        #assert r == EGL_FALSE

        attribute_list = c_ints(
            (EGL_RED_SIZE, 8, EGL_GREEN_SIZE, 8, EGL_BLUE_SIZE, 8,
             EGL_DEPTH_SIZE, depth, EGL_ALPHA_SIZE, 8, EGL_BUFFER_SIZE, 32,
             EGL_SAMPLES, samples, EGL_STENCIL_SIZE, 8, EGL_SURFACE_TYPE,
             EGL_WINDOW_BIT, EGL_NONE))
        numconfig = c_int()
        self.config = ctypes.c_void_p()
        r = openegl.eglChooseConfig(self.display, ctypes.byref(attribute_list),
                                    ctypes.byref(self.config), 1,
                                    ctypes.byref(numconfig))

        context_attribs = c_ints((EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE))
        self.context = openegl.eglCreateContext(self.display, self.config,
                                                EGL_NO_CONTEXT,
                                                ctypes.byref(context_attribs))
        assert self.context != EGL_NO_CONTEXT

        self.create_surface(x, y, w, h, layer)

        opengles.glDepthRangef(c_float(0.0), c_float(1.0))
        opengles.glClearColor(c_float(0.3), c_float(0.3), c_float(0.7),
                              c_float(1.0))
        opengles.glBindFramebuffer(GL_FRAMEBUFFER, 0)

        #Setup default hints
        opengles.glEnable(GL_CULL_FACE)
        opengles.glEnable(GL_DEPTH_TEST)
        opengles.glDepthFunc(GL_LESS)
        opengles.glDepthMask(1)
        opengles.glCullFace(GL_FRONT)
        opengles.glHint(GL_GENERATE_MIPMAP_HINT, GL_NICEST)
        opengles.glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, 1,
                                     GL_ONE_MINUS_SRC_ALPHA)

        # Switches off alpha blending problem with desktop - is there a bug in the
        # driver?
        # Thanks to Roland Humphries who sorted this one!!
        opengles.glColorMask(1, 1, 1, 0)

        #opengles.glEnableClientState(GL_VERTEX_ARRAY)
        #opengles.glEnableClientState(GL_NORMAL_ARRAY)
        self.active = True
Beispiel #7
0
  def create_surface(self, x=0, y=0, w=0, h=0):
    #Set the viewport position and size
    dst_rect = c_ints((x, y, w, h))
    src_rect = c_ints((x, y, w << 16, h << 16))

    if PLATFORM != PLATFORM_PI:
      self.width, self.height = w, h

      # Set some WM info
      root = xlib.XRootWindowOfScreen(self.screen)
      self.window = xlib.XCreateSimpleWindow(self.d, root, x, y, w, h, 1, 0, 0)

      s = ctypes.create_string_buffer(b'WM_DELETE_WINDOW')
      self.WM_DELETE_WINDOW = ctypes.c_ulong(xlib.XInternAtom(self.d, s, 0))
      #TODO add functions to xlib for these window manager libx11 functions
      #self.window.set_wm_name('pi3d xlib window')
      #self.window.set_wm_icon_name('pi3d')
      #self.window.set_wm_class('draw', 'XlibExample')

      xlib.XSetWMProtocols(self.d, self.window, ctypes.byref(self.WM_DELETE_WINDOW), 1)
      #self.window.set_wm_hints(flags = Xutil.StateHint,
      #                         initial_state = Xutil.NormalState)

      #self.window.set_wm_normal_hints(flags = (Xutil.PPosition | Xutil.PSize
      #                                         | Xutil.PMinSize),
      #                                min_width = 20,
      #                                min_height = 20)

      xlib.XSelectInput(self.d, self.window, KeyPressMask)
      xlib.XMapWindow(self.d, self.window)
      self.surface = openegl.eglCreateWindowSurface(self.display, self.config, self.window, 0)

    else:
      self.dispman_display = bcm.vc_dispmanx_display_open(0) #LCD setting
      self.dispman_update = bcm.vc_dispmanx_update_start(0)
      self.dispman_element = bcm.vc_dispmanx_element_add(
        self.dispman_update,
        self.dispman_display,
        0, ctypes.byref(dst_rect),
        0, ctypes.byref(src_rect),
        DISPMANX_PROTECTION_NONE,
        0, 0, 0)

      nativewindow = c_ints((self.dispman_element, w, h + 1))
      bcm.vc_dispmanx_update_submit_sync(self.dispman_update)

      nw_p = ctypes.pointer(nativewindow)
      self.nw_p = nw_p

      self.surface = openegl.eglCreateWindowSurface(self.display, self.config, self.nw_p, 0)
    assert self.surface != EGL_NO_SURFACE

    r = openegl.eglMakeCurrent(self.display, self.surface, self.surface,
                               self.context)
    assert r

    #Create viewport
    opengles.glViewport(0, 0, w, h)
Beispiel #8
0
  def create_display(self, x=0, y=0, w=0, h=0, depth=24, samples=4, layer=0, display_config=DISPLAY_CONFIG_DEFAULT):
    self.display = openegl.eglGetDisplay(EGL_DEFAULT_DISPLAY)
    assert self.display != EGL_NO_DISPLAY
    
    self.display_config = display_config

    for smpl in [samples, 0]: # try with samples first but ANGLE dll can't cope so drop to 0 for windows
      r = openegl.eglInitialize(self.display, None, None)

      attribute_list = c_ints((EGL_RED_SIZE, 8,
                               EGL_GREEN_SIZE, 8,
                               EGL_BLUE_SIZE, 8,
                               EGL_DEPTH_SIZE, depth,
                               EGL_ALPHA_SIZE, 8,
                               EGL_BUFFER_SIZE, 32,
                               EGL_SAMPLES, smpl,
                               EGL_STENCIL_SIZE, 8,
                               EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
                               EGL_NONE))
      numconfig = c_int()
      self.config = ctypes.c_void_p()
      r = openegl.eglChooseConfig(self.display,
                                  ctypes.byref(attribute_list),
                                  ctypes.byref(self.config), 1,
                                  ctypes.byref(numconfig))

      context_attribs = c_ints((EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE))
      self.context = openegl.eglCreateContext(self.display, self.config,
                                              EGL_NO_CONTEXT, ctypes.byref(context_attribs) )
      if self.context != EGL_NO_CONTEXT:
        break
    assert self.context != EGL_NO_CONTEXT

    self.create_surface(x, y, w, h, layer)

    opengles.glDepthRangef(c_float(0.0), c_float(1.0))
    opengles.glClearColor (c_float(0.3), c_float(0.3), c_float(0.7), c_float(1.0))
    opengles.glBindFramebuffer(GL_FRAMEBUFFER, 0)

    #Setup default hints
    opengles.glEnable(GL_CULL_FACE)
    opengles.glEnable(GL_DEPTH_TEST)
    opengles.glDepthFunc(GL_LESS);
    opengles.glDepthMask(1);
    opengles.glCullFace(GL_FRONT)
    opengles.glHint(GL_GENERATE_MIPMAP_HINT, GL_NICEST)
    opengles.glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, 
                                            1, GL_ONE_MINUS_SRC_ALPHA)

    # Switches off alpha blending problem with desktop - is there a bug in the
    # driver?
    # Thanks to Roland Humphries who sorted this one!!
    opengles.glColorMask(1, 1, 1, 0)

    #opengles.glEnableClientState(GL_VERTEX_ARRAY)
    #opengles.glEnableClientState(GL_NORMAL_ARRAY)
    self.active = True
Beispiel #9
0
    def setImg(self, path):
        img = self.loadImg(path)

        bmpRect = c_ints((0, 0, self.imgw, self.imgh))
        imgbuffer = numpy.array(img)

        result = bcm.vc_dispmanx_resource_write_data(self.img_resource,
                                                     self.imgtype, self.pitch,
                                                     imgbuffer.ctypes.data,
                                                     ctypes.pointer(bmpRect))
        dst_rect = c_ints((0, 0, self.w, self.h))

        with self.bcmUpdate() as dispman_update:
            bcm.vc_dispmanx_element_modified(dispman_update, self.img_element,
                                             dst_rect)
Beispiel #10
0
  def create_display(self, x=0, y=0, w=0, h=0):
    self.display = openegl.eglGetDisplay(EGL_DEFAULT_DISPLAY)
    assert self.display != EGL_NO_DISPLAY

    r = openegl.eglInitialize(self.display, 0, 0)
    #assert r == EGL_FALSE

    attribute_list = c_ints((EGL_RED_SIZE, 8,
                             EGL_GREEN_SIZE, 8,
                             EGL_BLUE_SIZE, 8,
                             EGL_DEPTH_SIZE, 24,  # TOD: use self.depth?
                             EGL_ALPHA_SIZE, 8,
                             EGL_BUFFER_SIZE, 32,
                             EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
                             EGL_NONE))
    numconfig = c_int()
    self.config = ctypes.c_void_p()
    r = openegl.eglChooseConfig(self.display,
                                ctypes.byref(attribute_list),
                                ctypes.byref(self.config), 1,
                                ctypes.byref(numconfig))

    context_attribs = c_ints((EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE))
    self.context = openegl.eglCreateContext(self.display, self.config,
                                            EGL_NO_CONTEXT, ctypes.byref(context_attribs) )
    assert self.context != EGL_NO_CONTEXT

    self.create_surface(x, y, w, h)

    opengles.glDepthRangef(c_float(-1.0),c_float(1.0))
    opengles.glClearColor (c_float(0.3), c_float(0.3), c_float(0.7), c_float(1.0));
    opengles.glBindFramebuffer(GL_FRAMEBUFFER,0)

    #Setup default hints
    opengles.glEnable(GL_CULL_FACE)
    opengles.glEnable(GL_NORMALIZE)
    opengles.glEnable(GL_DEPTH_TEST)
    opengles.glCullFace(GL_FRONT)
    opengles.glHint(GL_GENERATE_MIPMAP_HINT, GL_NICEST)

    # Switches off alpha blending problem with desktop - is there a bug in the
    # driver?
    # Thanks to Roland Humphries who sorted this one!!
    opengles.glColorMask(1, 1, 1, 0)

    #opengles.glEnableClientState(GL_VERTEX_ARRAY)
    #opengles.glEnableClientState(GL_NORMAL_ARRAY)
    self.active = True
Beispiel #11
0
    def setImg(self, path):
        img = self.loadImg(path)

        bmpRect = c_ints((0, 0, self.imgw, self.imgh))
        imgbuffer = numpy.array(img)

        result = bcm.vc_dispmanx_resource_write_data(
            self.img_resource, self.imgtype, self.pitch, imgbuffer.ctypes.data, ctypes.byref(bmpRect)
        )
        with self.bcmUpdate() as dispman_update:
            bcm.vc_dispmanx_element_modified(dispman_update, self.img_element, ctypes.byref(self.dst_rect))
Beispiel #12
0
#path = "C:/Program Files (x86)/Google/Chrome/Application/42.0.2311.152"
#path = "C:\\Program Files (x86)\\Google\Chrome\\Application\\42.0.2311.135"
path = ""
""" NB this last 'same diretory' path is to check with the dll files that
have to be in the same directory as this python script (test_egl.py) i.e.
the ones I compiled (download from http://www.eldwick.org.uk/files/libEGL.zip)
or the Mozilla Firefox ones. For these latter you also need to copy over
mozglue.dll
"""
d3dcompiler = ctypes.WinDLL(os.path.join(path, "d3dcompiler_47.dll"))
opengles = ctypes.WinDLL(os.path.join(path, "libglesv2.dll"))
openegl = ctypes.WinDLL(os.path.join(path, "libegl.dll"))

attribute_lists = [c_ints((EGL_PLATFORM_ANGLE_TYPE_ANGLE, EGL_PLATFORM_ANGLE_TYPE_D3D11_ANGLE,
                        EGL_ANGLE_DISPLAY_ALLOW_RENDER_TO_BACK_BUFFER, EGL_TRUE,
                        EGL_PLATFORM_ANGLE_ENABLE_AUTOMATIC_TRIM_ANGLE, EGL_TRUE,
                        EGL_NONE)),
                  c_ints((EGL_PLATFORM_ANGLE_TYPE_ANGLE, EGL_PLATFORM_ANGLE_TYPE_D3D11_ANGLE,
                        EGL_PLATFORM_ANGLE_MAX_VERSION_MAJOR_ANGLE, 9,
                        EGL_PLATFORM_ANGLE_MAX_VERSION_MINOR_ANGLE, 3,
                        EGL_ANGLE_DISPLAY_ALLOW_RENDER_TO_BACK_BUFFER, EGL_TRUE,
                        EGL_PLATFORM_ANGLE_ENABLE_AUTOMATIC_TRIM_ANGLE, EGL_TRUE,
                        EGL_NONE)),  
                  c_ints((EGL_PLATFORM_ANGLE_TYPE_ANGLE, EGL_PLATFORM_ANGLE_TYPE_D3D11_ANGLE,
                        EGL_PLATFORM_ANGLE_MAX_VERSION_MAJOR_ANGLE, 9,
                        EGL_PLATFORM_ANGLE_MAX_VERSION_MINOR_ANGLE, 3,
                        EGL_NONE)),
                  c_ints((EGL_PLATFORM_ANGLE_TYPE_ANGLE, EGL_PLATFORM_ANGLE_TYPE_D3D11_ANGLE,
                        EGL_PLATFORM_ANGLE_DEVICE_TYPE_ANGLE, EGL_PLATFORM_ANGLE_DEVICE_TYPE_WARP_ANGLE,
                        EGL_ANGLE_DISPLAY_ALLOW_RENDER_TO_BACK_BUFFER, EGL_TRUE,
                        EGL_PLATFORM_ANGLE_ENABLE_AUTOMATIC_TRIM_ANGLE, EGL_TRUE,
Beispiel #13
0
#path = "C:/Program Files (x86)/Google/Chrome/Application/42.0.2311.152"
#path = "C:\\Program Files (x86)\\Google\Chrome\\Application\\42.0.2311.135"
path = ""
""" NB this last 'same diretory' path is to check with the dll files that
have to be in the same directory as this python script (test_egl.py) i.e.
the ones I compiled (download from http://www.eldwick.org.uk/files/libEGL.zip)
or the Mozilla Firefox ones. For these latter you also need to copy over
mozglue.dll
"""
d3dcompiler = ctypes.WinDLL(os.path.join(path, "d3dcompiler_47.dll"))
opengles = ctypes.WinDLL(os.path.join(path, "libglesv2.dll"))
openegl = ctypes.WinDLL(os.path.join(path, "libegl.dll"))

attribute_lists = [
    c_ints(
        (EGL_PLATFORM_ANGLE_TYPE_ANGLE, EGL_PLATFORM_ANGLE_TYPE_D3D11_ANGLE,
         EGL_ANGLE_DISPLAY_ALLOW_RENDER_TO_BACK_BUFFER, EGL_TRUE,
         EGL_PLATFORM_ANGLE_ENABLE_AUTOMATIC_TRIM_ANGLE, EGL_TRUE, EGL_NONE)),
    c_ints(
        (EGL_PLATFORM_ANGLE_TYPE_ANGLE, EGL_PLATFORM_ANGLE_TYPE_D3D11_ANGLE,
         EGL_PLATFORM_ANGLE_MAX_VERSION_MAJOR_ANGLE, 9,
         EGL_PLATFORM_ANGLE_MAX_VERSION_MINOR_ANGLE, 3,
         EGL_ANGLE_DISPLAY_ALLOW_RENDER_TO_BACK_BUFFER, EGL_TRUE,
         EGL_PLATFORM_ANGLE_ENABLE_AUTOMATIC_TRIM_ANGLE, EGL_TRUE, EGL_NONE)),
    c_ints((EGL_PLATFORM_ANGLE_TYPE_ANGLE, EGL_PLATFORM_ANGLE_TYPE_D3D11_ANGLE,
            EGL_PLATFORM_ANGLE_MAX_VERSION_MAJOR_ANGLE, 9,
            EGL_PLATFORM_ANGLE_MAX_VERSION_MINOR_ANGLE, 3, EGL_NONE)),
    c_ints(
        (EGL_PLATFORM_ANGLE_TYPE_ANGLE, EGL_PLATFORM_ANGLE_TYPE_D3D11_ANGLE,
         EGL_PLATFORM_ANGLE_DEVICE_TYPE_ANGLE,
         EGL_PLATFORM_ANGLE_DEVICE_TYPE_WARP_ANGLE,
         EGL_ANGLE_DISPLAY_ALLOW_RENDER_TO_BACK_BUFFER, EGL_TRUE,
Beispiel #14
0
    def create_display(self,
                       x=0,
                       y=0,
                       w=0,
                       h=0,
                       depth=24,
                       samples=4,
                       layer=0,
                       display_config=DISPLAY_CONFIG_DEFAULT,
                       window_title=''):
        self.display = openegl.eglGetDisplay(EGL_DEFAULT_DISPLAY)
        assert self.display != EGL_NO_DISPLAY and self.display is not None

        self.display_config = display_config

        self.window_title = window_title.encode()

        for smpl in [
                samples, 0
        ]:  # try with samples first but ANGLE dll can't cope so drop to 0 for windows
            r = openegl.eglInitialize(self.display, None, None)

            attribute_list = c_ints(
                (EGL_RED_SIZE, 8, EGL_GREEN_SIZE, 8, EGL_BLUE_SIZE, 8,
                 EGL_DEPTH_SIZE, depth, EGL_ALPHA_SIZE, 8, EGL_BUFFER_SIZE, 32,
                 EGL_SAMPLES, smpl, EGL_STENCIL_SIZE, 8, EGL_SURFACE_TYPE,
                 EGL_WINDOW_BIT, EGL_NONE))
            numconfig = c_int()
            self.config = ctypes.c_void_p()
            r = openegl.eglChooseConfig(self.display, attribute_list,
                                        ctypes.byref(self.config), 1,
                                        ctypes.byref(numconfig))

            context_attribs = c_ints((EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE))
            self.context = openegl.eglCreateContext(self.display, self.config,
                                                    EGL_NO_CONTEXT,
                                                    context_attribs)
            if self.context != EGL_NO_CONTEXT:
                break
        assert self.context != EGL_NO_CONTEXT and self.context is not None

        self.create_surface(x, y, w, h, layer)

        opengles.glDepthRangef(GLfloat(0.0), GLfloat(1.0))
        opengles.glClearColor(GLfloat(0.3), GLfloat(0.3), GLfloat(0.7),
                              GLfloat(1.0))
        opengles.glBindFramebuffer(GL_FRAMEBUFFER, GLuint(0))

        #Setup default hints
        opengles.glEnable(GL_CULL_FACE)
        opengles.glCullFace(GL_BACK)
        opengles.glFrontFace(GL_CW)
        opengles.glEnable(GL_DEPTH_TEST)
        opengles.glEnable(GL_PROGRAM_POINT_SIZE)
        opengles.glEnable(GL_POINT_SPRITE)
        opengles.glDepthFunc(GL_LESS)
        opengles.glDepthMask(GLboolean(True))
        opengles.glHint(GL_GENERATE_MIPMAP_HINT, GL_NICEST)
        opengles.glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, 1,
                                     GL_ONE_MINUS_SRC_ALPHA)

        # Switches off alpha blending problem with desktop - is there a bug in the
        # driver?
        # Thanks to Roland Humphries who sorted this one!!
        opengles.glColorMask(GLboolean(True), GLboolean(True), GLboolean(True),
                             GLboolean(False))

        #opengles.glEnableClientState(GL_VERTEX_ARRAY)
        #opengles.glEnableClientState(GL_NORMAL_ARRAY)
        self.active = True
Beispiel #15
0
    def create_surface(self, x=0, y=0, w=0, h=0):
        # Set the viewport position and size
        dst_rect = c_ints((x, y, w, h))
        src_rect = c_ints((x, y, w << 16, h << 16))

        if PLATFORM == PLATFORM_ANDROID:
            self.surface = openegl.eglGetCurrentSurface(EGL_DRAW)
            # Get the width and height of the screen - TODO, this system returns 100x100
            time.sleep(0.2)  # give it a chance to find out the dimensions
            w = c_int()
            h = c_int()
            openegl.eglQuerySurface(self.display, self.surface, EGL_WIDTH, ctypes.byref(w))
            openegl.eglQuerySurface(self.display, self.surface, EGL_HEIGHT, ctypes.byref(h))
            self.width, self.height = w.value, h.value
        elif PLATFORM == PLATFORM_PI:
            self.dispman_display = bcm.vc_dispmanx_display_open(0)  # LCD setting
            self.dispman_update = bcm.vc_dispmanx_update_start(0)
            self.dispman_element = bcm.vc_dispmanx_element_add(
                self.dispman_update,
                self.dispman_display,
                0,
                ctypes.byref(dst_rect),
                0,
                ctypes.byref(src_rect),
                DISPMANX_PROTECTION_NONE,
                0,
                0,
                0,
            )

            nativewindow = c_ints((self.dispman_element, w, h + 1))
            bcm.vc_dispmanx_update_submit_sync(self.dispman_update)

            nw_p = ctypes.pointer(nativewindow)
            self.nw_p = nw_p

            self.surface = openegl.eglCreateWindowSurface(self.display, self.config, self.nw_p, 0)

        elif pi3d.USE_PYGAME:
            import pygame

            flags = pygame.RESIZABLE | pygame.OPENGL
            wsize = (w, h)
            if w == self.width and h == self.height:  # i.e. full screen
                flags = pygame.FULLSCREEN | pygame.OPENGL
                wsize = (0, 0)
            self.width, self.height = w, h
            self.d = pygame.display.set_mode(wsize, flags)
            self.window = pygame.display.get_wm_info()["window"]
            self.surface = openegl.eglCreateWindowSurface(self.display, self.config, self.window, 0)

        else:
            self.width, self.height = w, h

            # Set some WM info
            root = xlib.XRootWindowOfScreen(self.screen)
            self.window = xlib.XCreateSimpleWindow(self.d, root, x, y, w, h, 1, 0, 0)

            s = ctypes.create_string_buffer(b"WM_DELETE_WINDOW")
            self.WM_DELETE_WINDOW = ctypes.c_ulong(xlib.XInternAtom(self.d, s, 0))
            # TODO add functions to xlib for these window manager libx11 functions
            # self.window.set_wm_name('pi3d xlib window')
            # self.window.set_wm_icon_name('pi3d')
            # self.window.set_wm_class('draw', 'XlibExample')

            xlib.XSetWMProtocols(self.d, self.window, ctypes.byref(self.WM_DELETE_WINDOW), 1)
            # self.window.set_wm_hints(flags = Xutil.StateHint,
            #                         initial_state = Xutil.NormalState)

            # self.window.set_wm_normal_hints(flags = (Xutil.PPosition | Xutil.PSize
            #                                         | Xutil.PMinSize),
            #                                min_width = 20,
            #                                min_height = 20)

            xlib.XSelectInput(self.d, self.window, KeyPressMask)
            xlib.XMapWindow(self.d, self.window)
            self.surface = openegl.eglCreateWindowSurface(self.display, self.config, self.window, 0)

        assert self.surface != EGL_NO_SURFACE
        r = openegl.eglMakeCurrent(self.display, self.surface, self.surface, self.context)
        assert r

        # Create viewport
        opengles.glViewport(0, 0, w, h)
Beispiel #16
0
    def create_surface(self, x=0, y=0, w=0, h=0, layer=0):
        #Set the viewport position and size
        dst_rect = c_ints((x, y, w, h))
        src_rect = c_ints((x, y, w << 16, h << 16))

        if PLATFORM == PLATFORM_ANDROID:
            self.surface = openegl.eglGetCurrentSurface(EGL_DRAW)
            # Get the width and height of the screen - TODO, this system returns 100x100
            time.sleep(0.2)  #give it a chance to find out the dimensions
            w = c_int()
            h = c_int()
            openegl.eglQuerySurface(self.display, self.surface, EGL_WIDTH,
                                    ctypes.byref(w))
            openegl.eglQuerySurface(self.display, self.surface, EGL_HEIGHT,
                                    ctypes.byref(h))
            self.width, self.height = w.value, h.value
        elif PLATFORM == PLATFORM_PI:
            self.dispman_display = bcm.vc_dispmanx_display_open(
                0)  #LCD setting
            self.dispman_update = bcm.vc_dispmanx_update_start(0)
            self.dispman_element = bcm.vc_dispmanx_element_add(
                self.dispman_update, self.dispman_display, layer,
                ctypes.byref(dst_rect), 0, ctypes.byref(src_rect),
                DISPMANX_PROTECTION_NONE, 0, 0, 0)

            nativewindow = c_ints((self.dispman_element, w, h + 1))
            bcm.vc_dispmanx_update_submit_sync(self.dispman_update)

            nw_p = ctypes.pointer(nativewindow)
            self.nw_p = nw_p

            self.surface = openegl.eglCreateWindowSurface(
                self.display, self.config, self.nw_p, 0)

        elif pi3d.USE_PYGAME:
            import pygame
            flags = pygame.OPENGL
            wsize = (w, h)
            if w == self.width and h == self.height:  # i.e. full screen
                flags = pygame.FULLSCREEN | pygame.OPENGL
                wsize = (0, 0)
            if self.display_config & DISPLAY_CONFIG_NO_RESIZE:
                flags |= pygame.RESIZABLE
            if self.display_config & DISPLAY_CONFIG_NO_FRAME:
                flags |= pygame.NOFRAME
            if self.display_config & DISPLAY_CONFIG_FULLSCREEN:
                flags |= pygame.FULLSCREEN
            elif self.display_config & DISPLAY_CONFIG_MAXIMIZED:
                flags |= pygame.FULLSCREEN
                wsize = (0, 0)

            self.width, self.height = w, h
            self.d = pygame.display.set_mode(wsize, flags)
            self.window = pygame.display.get_wm_info()["window"]
            self.surface = openegl.eglCreateWindowSurface(
                self.display, self.config, self.window, 0)

        else:
            self.width, self.height = w, h

            # Set some WM info
            root = xlib.XRootWindowOfScreen(self.screen)
            self.window = xlib.XCreateSimpleWindow(self.d, root, x, y, w, h, 1,
                                                   0, 0)

            s = ctypes.create_string_buffer(b'WM_DELETE_WINDOW')
            self.WM_DELETE_WINDOW = ctypes.c_ulong(
                xlib.XInternAtom(self.d, s, 0))
            #TODO add functions to xlib for these window manager libx11 functions
            #self.window.set_wm_name('pi3d xlib window')
            #self.window.set_wm_icon_name('pi3d')
            #self.window.set_wm_class('draw', 'XlibExample')

            xlib.XSetWMProtocols(self.d, self.window,
                                 ctypes.byref(self.WM_DELETE_WINDOW), 1)
            #self.window.set_wm_hints(flags = Xutil.StateHint,
            #                         initial_state = Xutil.NormalState)

            #self.window.set_wm_normal_hints(flags = (Xutil.PPosition | Xutil.PSize
            #                                         | Xutil.PMinSize),
            #                                min_width = 20,
            #                                min_height = 20)

            xlib.XSelectInput(self.d, self.window,
                              KeyPressMask | KeyReleaseMask)
            xlib.XMapWindow(self.d, self.window)
            self.surface = openegl.eglCreateWindowSurface(
                self.display, self.config, self.window, 0)

        assert self.surface != EGL_NO_SURFACE
        r = openegl.eglMakeCurrent(self.display, self.surface, self.surface,
                                   self.context)
        assert r

        #Create viewport
        opengles.glViewport(0, 0, w, h)
Beispiel #17
0
 def _unload_opengl(self):
     """clear it out"""
     texture_array = c_ints([self._tex.value])
     opengles.glDeleteTextures(1, ctypes.addressof(texture_array))
Beispiel #18
0
    def create_surface(self, x=0, y=0, w=0, h=0, layer=0):
        #Set the viewport position and size
        dst_rect = c_ints((x, y, w, h))
        src_rect = c_ints((x, y, w << 16, h << 16))

        if PLATFORM == PLATFORM_ANDROID:
            self.surface = openegl.eglGetCurrentSurface(EGL_DRAW)
            # Get the width and height of the screen - TODO, this system returns 100x100
            time.sleep(0.2)  #give it a chance to find out the dimensions
            w = c_int()
            h = c_int()
            openegl.eglQuerySurface(self.display, self.surface, EGL_WIDTH,
                                    byref(w))
            openegl.eglQuerySurface(self.display, self.surface, EGL_HEIGHT,
                                    byref(h))
            self.width, self.height = w.value, h.value
        elif PLATFORM == PLATFORM_PI:
            self.dispman_display = bcm.vc_dispmanx_display_open(
                0)  #LCD setting
            self.dispman_update = bcm.vc_dispmanx_update_start(0)
            alpha = c_ints((DISPMANX_FLAGS_ALPHA_PREMULT, 0, 0))
            self.dispman_element = bcm.vc_dispmanx_element_add(
                self.dispman_update, self.dispman_display, layer, dst_rect, 0,
                src_rect, DISPMANX_PROTECTION_NONE, alpha, 0, 0)

            nativewindow = (GLint * 3)(self.dispman_element, w, h + 1)
            bcm.vc_dispmanx_update_submit_sync(self.dispman_update)

            self.nw_p = ctypes.pointer(nativewindow)
            ### NB changing the argtypes to allow passing of bcm native window is
            ### deeply unsatisfactory. But xlib defines Window as c_ulong and ctypes
            ### isn't happy about a pointer being cast to an int
            openegl.eglCreateWindowSurface.argtypes = [
                EGLDisplay, EGLConfig,
                POINTER((GLint * 3)), EGLint
            ]
            self.surface = openegl.eglCreateWindowSurface(
                self.display, self.config, self.nw_p, 0)

        elif pi3d.USE_PYGAME:
            import pygame
            flags = pygame.OPENGL
            wsize = (w, h)
            if w == self.width and h == self.height:  # i.e. full screen
                flags = pygame.FULLSCREEN | pygame.OPENGL
                wsize = (0, 0)
            if self.display_config & DISPLAY_CONFIG_NO_RESIZE:
                flags |= pygame.RESIZABLE
            if self.display_config & DISPLAY_CONFIG_NO_FRAME:
                flags |= pygame.NOFRAME
            if self.display_config & DISPLAY_CONFIG_FULLSCREEN:
                flags |= pygame.FULLSCREEN
            elif self.display_config & DISPLAY_CONFIG_MAXIMIZED:
                flags |= pygame.FULLSCREEN
                wsize = (0, 0)

            self.width, self.height = w, h
            self.d = pygame.display.set_mode(wsize, flags)
            self.window = pygame.display.get_wm_info()["window"]
            self.surface = openegl.eglCreateWindowSurface(
                self.display, self.config, self.window, 0)

        else:  # work on basis it's X11
            # Set some WM info
            self.root = xlib.XRootWindowOfScreen(self.screen)
            if self.use_glx:  # For drawing on X window with transparent background
                numfbconfigs = c_int()
                VisData = c_ints(
                    (glx.GLX_RENDER_TYPE, glx.GLX_RGBA_BIT,
                     glx.GLX_DRAWABLE_TYPE, glx.GLX_WINDOW_BIT,
                     glx.GLX_DOUBLEBUFFER, True, glx.GLX_RED_SIZE, 8,
                     glx.GLX_GREEN_SIZE, 8, glx.GLX_BLUE_SIZE, 8,
                     glx.GLX_ALPHA_SIZE, 8, glx.GLX_DEPTH_SIZE, 16, 0))
                glx_screen = xlib.XDefaultScreen(self.d)
                fbconfigs = glx.glXChooseFBConfig(self.d, glx_screen, VisData,
                                                  byref(numfbconfigs))
                fbconfig = 0
                for i in range(numfbconfigs.value):
                    visual = glx.glXGetVisualFromFBConfig(
                        self.d, fbconfigs[i]).contents
                    if not visual:
                        continue
                    pict_format = glx.XRenderFindVisualFormat(
                        self.d, visual.visual).contents
                    if not pict_format:
                        continue

                    fbconfig = fbconfigs[i]
                    if pict_format.direct.alphaMask > 0:
                        break

                if not fbconfig:
                    print("No matching FB config found")
                #/* Create a colormap - only needed on some X clients, eg. IRIX */
                cmap = xlib.XCreateColormap(self.d, self.root, visual.visual,
                                            AllocNone)
                attr = xlib.XSetWindowAttributes()
                attr.colormap = cmap
                attr.background_pixmap = 0
                attr.border_pixmap = 0
                attr.border_pixel = 0
                attr.event_mask = (StructureNotifyMask | EnterWindowMask
                                   | LeaveWindowMask | ExposureMask
                                   | ButtonPressMask | ButtonReleaseMask
                                   | OwnerGrabButtonMask | KeyPressMask
                                   | KeyReleaseMask)
                attr_mask = (  #  CWBackPixmap|
                    CWColormap | CWBorderPixel | CWEventMask)
                self.window = xlib.XCreateWindow(self.d, self.root, x, y, w, h,
                                                 0, visual.depth, 1,
                                                 visual.visual, attr_mask,
                                                 byref(attr))
            else:  # normal EGL created context
                self.window = xlib.XCreateSimpleWindow(self.d, self.root, x, y,
                                                       w, h, 1, 0, 0)

            s = ctypes.create_string_buffer(b'WM_DELETE_WINDOW')
            self.WM_DELETE_WINDOW = ctypes.c_ulong(
                xlib.XInternAtom(self.d, s, 0))

            # set window title
            title = ctypes.c_char_p(self.window_title)
            title_length = ctypes.c_int(len(self.window_title))
            wm_name_atom = ctypes.c_ulong(
                xlib.XInternAtom(self.d,
                                 ctypes.create_string_buffer(b'WM_NAME'), 0))
            string_atom = ctypes.c_ulong(
                xlib.XInternAtom(self.d,
                                 ctypes.create_string_buffer(b'STRING'), 0))
            xlib.XChangeProperty(self.d, self.window, wm_name_atom,
                                 string_atom, 8, xlib.PropModeReplace, title,
                                 title_length)

            if (w == self.width
                    and h == self.height) or (self.display_config
                                              & DISPLAY_CONFIG_FULLSCREEN):
                # set full-screen. Messy c function calls!
                wm_state = ctypes.c_ulong(
                    xlib.XInternAtom(self.d, b'_NET_WM_STATE', 0))
                fullscreen = ctypes.c_ulong(
                    xlib.XInternAtom(self.d, b'_NET_WM_STATE_FULLSCREEN', 0))
                fullscreen = ctypes.cast(ctypes.pointer(fullscreen),
                                         ctypes.c_char_p)
                XA_ATOM = 4
                xlib.XChangeProperty(self.d, self.window, wm_state, XA_ATOM,
                                     32, xlib.PropModeReplace, fullscreen, 1)

            self.width, self.height = w, h

            if self.display_config & DISPLAY_CONFIG_HIDE_CURSOR:
                black = xlib.XColor()
                black.red = 0
                black.green = 0
                black.blue = 0
                noData = ctypes.c_char_p(bytes([0, 0, 0, 0, 0, 0, 0, 0]))
                bitmapNoData = xlib.XCreateBitmapFromData(
                    self.d, self.window, noData, 8, 8)
                invisibleCursor = xlib.XCreatePixmapCursor(
                    self.d, bitmapNoData, bitmapNoData, black, black, 0, 0)
                xlib.XDefineCursor(self.d, self.window, invisibleCursor)

            #TODO add functions to xlib for these window manager libx11 functions
            #self.window.set_wm_name('pi3d xlib window')
            #self.window.set_wm_icon_name('pi3d')
            #self.window.set_wm_class('draw', 'XlibExample')

            xlib.XSetWMProtocols(self.d, self.window, self.WM_DELETE_WINDOW, 1)
            #self.window.set_wm_hints(flags = Xutil.StateHint,
            #                         initial_state = Xutil.NormalState)

            #self.window.set_wm_normal_hints(flags = (Xutil.PPosition | Xutil.PSize
            #                                         | Xutil.PMinSize),
            #                                min_width = 20,
            #                                min_height = 20)

            xlib.XSelectInput(
                self.d, self.window,
                KeyPressMask | KeyReleaseMask | ResizeRedirectMask)
            xlib.XMapWindow(self.d, self.window)
            #xlib.XMoveWindow(self.d, self.window, x, y) #TODO this has to happen later. Works after rendering first frame. Check when
            if self.use_glx:
                dummy = c_int()
                if not glx.glXQueryExtension(self.d, byref(dummy),
                                             byref(dummy)):
                    print("OpenGL not supported by X server\n")
                dummy_glx_context = ctypes.cast(0, glx.GLXContext)
                self.render_context = glx.glXCreateNewContext(
                    self.d, fbconfig, glx.GLX_RGBA_TYPE, dummy_glx_context,
                    True)
                if not self.render_context:
                    print("Failed to create a GL context\n")
                if not glx.glXMakeContextCurrent(
                        self.d, self.window, self.window, self.render_context):
                    print("glXMakeCurrent failed for window\n")
            else:
                self.surface = openegl.eglCreateWindowSurface(
                    self.display, self.config, self.window, 0)

        if not self.use_glx:
            assert self.surface != EGL_NO_SURFACE and self.surface is not None
            r = openegl.eglMakeCurrent(self.display, self.surface,
                                       self.surface, self.context)
            assert r

        #Create viewport
        opengles.glViewport(GLint(0), GLint(0), GLsizei(w), GLsizei(h))
Beispiel #19
0
 def _unload_opengl(self):
   """clear it out"""
   texture_array = c_ints([self._tex.value])
   opengles.glDeleteTextures(1, ctypes.addressof(texture_array))