class_docstr: A class representing an EGL context. In short, EGL allows us to render OpenGL con-
  tent in a headless mode, that is without an actual display to render to. This capa-
  bility enables MeshRasterizerOpenGL to render on the GPU and then transfer the re-
  sults to PyTorch3D.
class_docstr_tok: ["A","class","representing","an","EGL","context",".","In","short",",","EGL","allows","us","to","render","OpenGL","con-","tent","in","a","headless","mode",",","that","is","without","an","actual","display","to","render","to",".","This","capa-","bility","enables","MeshRasterizerOpenGL","to","render","on","the","GPU","and","then","transfer","the","re-","sults","to","PyTorch3D","."]
docstr: Args:
      width: Width of the "display" to render to.
      height: Height of the "display" to render to.
      cuda_device_id: Device ID to render to, in the CUDA convention (note that
          this might be different than EGL's device numbering).
docstr_tok: ["Args",":","width",":","Width","of","the","``","display","''","to","render","to",".","height",":","Height","of","the","``","display","''","to","render","to",".","cuda_device_id",":","Device","ID","to","render","to",",","in","the","CUDA","convention","(","note","that","this","might","be","different","than","EGL","'s","device","numbering",")","."]
code: def __init__(
      self, width: int, height: int, cuda_device_id: int = 0
  ) -> None:
      """
      Args:
          width: Width of the "display" to render to.
          height: Height of the "display" to render to.
          cuda_device_id: Device ID to render to, in the CUDA convention (note that
              this might be different than EGL's device numbering).
      """
      # Lock used to prevent multiple threads from rendering on the same device
      # at the same time, creating/destroying contexts at the same time, etc.
      self.lock = threading.Lock()
      self.cuda_device_id = cuda_device_id
      self.device = _get_cuda_device(self.cuda_device_id)
      self.width = width
      self.height = height
      self.dpy = egl.eglGetPlatformDisplayEXT(
          EGL_PLATFORM_DEVICE_EXT, self.device, None
      )
      major, minor = egl.EGLint(), egl.EGLint()
  
      # Initialize EGL components: the display, surface, and context
      egl.eglInitialize(
          self.dpy, ctypes.pointer(major), ctypes.pointer(minor)
      )
  
      config = _get_egl_config(self.dpy, egl.EGL_PBUFFER_BIT)
      pb_surf_attribs = _egl_convert_to_int_array(
          {
              egl.EGL_WIDTH: width,
              egl.EGL_HEIGHT: height,
          }
      )
      self.surface = egl.eglCreatePbufferSurface(
          self.dpy, config, pb_surf_attribs
      )
      if self.surface == egl.EGL_NO_SURFACE:
          raise RuntimeError("Failed to create an EGL surface.")
  
      if not egl.eglBindAPI(egl.EGL_OPENGL_API):
          raise RuntimeError("Failed to bind EGL to the OpenGL API.")
      self.context = egl.eglCreateContext(
          self.dpy, config, egl.EGL_NO_CONTEXT, None
      )
      if self.context == egl.EGL_NO_CONTEXT:
          raise RuntimeError("Failed to create an EGL context.")
  
imports: import contextlib
  import ctypes
  import os
  import threading
  from typing import Any, Dict
  import OpenGL.EGL
  import pycuda.driver
  from OpenGL._opaque import opaque_pointer_cls
  from OpenGL.raw.EGL._errors import EGLError
label_desc: Use image node_id 1 to create a new EGLContext object  with example: obj = EGLContext(width, height, cuda_device_id)