Skip to content

Instantly share code, notes, and snippets.

@harubot
Last active May 11, 2019 06:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save harubot/b4a4b17346b91fde8105fa11b9d3edb6 to your computer and use it in GitHub Desktop.
Save harubot/b4a4b17346b91fde8105fa11b9d3edb6 to your computer and use it in GitHub Desktop.
#!/usr/bin/python
# -*- coding: utf-8 -*-
from ctypes import c_int
from ctypes import c_uint
from ctypes import c_float
from ctypes import c_void_p
from ctypes import POINTER
from ctypes import CDLL
from ctypes import CFUNCTYPE
from ctypes import byref
from ctypes.util import find_library
from functools import partial
from numpy import zeros
from numpy import uint8
EGLNativeDisplayType = c_void_p
EGLDisplay = c_void_p
EGL_DEFAULT_DISPLAY = EGLNativeDisplayType(0)
EGLBoolean = c_uint
EGLint = c_int
EGL_OPENGL_API = 0x30A2
EGLenum = c_uint
EGL_BLUE_SIZE = 0x3022
EGL_GREEN_SIZE = 0x3023
EGL_RED_SIZE = 0x3024
EGL_DEPTH_SIZE = 0x3025
EGL_SURFACE_TYPE = 0x3033
EGL_NONE = 0x3038
EGL_PBUFFER_BIT = 0x01
EGLConfig = c_void_p
EGL_HEIGHT = 0x3056
EGL_WIDTH = 0x3057
EGLSurface = c_void_p
EGL_CONTEXT_CLIENT_VERSION = 0x3098
EGLContext = c_void_p
EGL_NO_CONTEXT = EGLContext(0)
EGL_NO_SURFACE = EGLSurface(0)
GL_NO_ERROR = 0
GLenum = c_uint
eglLib = CDLL(find_library('EGL'))
glLib = CDLL(find_library('GL'))
def load(lib, name, restype, *args):
return (CFUNCTYPE(restype, *args))((name, lib))
def makeCtypeArray(ctype, contents):
arrayType = ctype * len(contents)
return arrayType(*contents)
loadEgl = partial(load, eglLib)
eglGetDisplay = loadEgl('eglGetDisplay', EGLDisplay, EGLNativeDisplayType)
eglInitialize = loadEgl('eglInitialize', EGLBoolean, EGLDisplay , POINTER(EGLint), POINTER(EGLint))
eglBindAPI = loadEgl('eglBindAPI', EGLBoolean, EGLenum)
eglChooseConfig = loadEgl('eglChooseConfig', EGLBoolean, EGLDisplay, POINTER(EGLint), POINTER(EGLConfig), EGLint, POINTER(EGLint))
eglCreatePbufferSurface = loadEgl('eglCreatePbufferSurface', EGLSurface, EGLDisplay, EGLConfig, POINTER(EGLint))
eglCreateContext = loadEgl('eglCreateContext', EGLContext, EGLDisplay, EGLConfig, EGLContext, POINTER(EGLint))
eglMakeCurrent = loadEgl('eglMakeCurrent', EGLBoolean, EGLDisplay, EGLSurface, EGLSurface, EGLContext)
eglDestroySurface = loadEgl('eglDestroySurface', EGLBoolean, EGLDisplay, EGLSurface)
eglDestroyContext = loadEgl('eglDestroyContext', EGLBoolean, EGLDisplay, EGLContext)
eglTerminate = loadEgl('eglTerminate', EGLBoolean, EGLDisplay)
loadGl = partial(load, glLib)
glGetError = loadGl('glGetError', GLenum)
# EGL Display
display = eglGetDisplay(EGL_DEFAULT_DISPLAY)
if not display:
raise Exception('no EGL display')
# EGL Initialize
major = EGLint(0)
minor = EGLint(0)
if not eglInitialize(display, byref(major), byref(minor)):
raise Exception('cannot initialize EGL display')
# EGL BindAPI
if not eglBindAPI(EGLenum(EGL_OPENGL_API)):
raise Exception('cannot bind API')
# EGL ChooseConfig
attribList = makeCtypeArray(EGLint, [
EGL_BLUE_SIZE, 8,
EGL_GREEN_SIZE, 8,
EGL_RED_SIZE, 8,
EGL_DEPTH_SIZE, 24,
EGL_SURFACE_TYPE, EGL_PBUFFER_BIT,
EGL_NONE])
config = EGLConfig()
configSize = EGLint(1)
numConfig = EGLint(0)
if not eglChooseConfig(display, attribList, byref(config), configSize, byref(numConfig)):
raise Exception('no suitable configs available on display')
# EGL Surface
surfaceAttribList = makeCtypeArray(EGLint, [
EGL_WIDTH, 100,
EGL_HEIGHT, 100,
EGL_NONE])
surface = eglCreatePbufferSurface(display, config, surfaceAttribList)
if not surface:
raise Exception('cannot create pbuffer surface')
# EGL Context
contextAttribList = makeCtypeArray(EGLint, [EGL_NONE])
context = eglCreateContext(display, config, EGL_NO_CONTEXT, contextAttribList)
if not context:
raise Exception('cannot create GL context')
# EGL MakeCurrent
if not eglMakeCurrent(display, surface, surface, context):
raise Exception('cannot make GL context current')
# IPython
import IPython
IPython.embed()
# EGL Destroy
if not eglMakeCurrent(display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT):
print('eglMakeCurrent failed')
if not eglDestroySurface(display, surface):
print('eglDestroySurface failed')
if not eglDestroyContext(display, context):
print('eglDestroyContext failed')
if not eglTerminate(display):
print('eglTerminate failed')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment