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.

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)