a
    i‚j—  ã                   @   sØ   d Z ddlZddlZddlZddlZddlZddlmZ ddlm	Z	 e 
e¡Ze ejd¡ZdZi ZdZdZeZd	Zd
ZdZe	 d¡Ze	 d¡Zeej_eej_ddd„Zdd„ Zdd„ Z dd„ Z!dddefdd„Z"dS )z¶Utility module for interacting with GBM to select rendering device

The base code here comes from github bug report #6 in the pyopengl
project, thanks to @abandoned-cocoon for that.
é    N)Úctypesloader)Ú_opaqueÚgbm)Úenumerate_devicesÚopen_deviceÚclose_devicer   é   é   é   é   é   Ú	GBMDeviceÚ
GBMSurface©Znvidiac                    sŽ   t ƒ ‰ t d¡D ]f}tj |¡}|dd… }tj t d|f ¡¡}||v rtj t |¡¡}t d||¡ ˆ  |¡ q‡ fdd„| D ƒ}|S )zALookup the driver for each card to exclude loading nvidia devicesz/dev/dri/by-path/pci-*-cardr
   éûÿÿÿz/sys/bus/pci/devices/%s/driverz%Skipping %s because it uses %s driverc                    s    g | ]}t j |¡ˆ vr|‘qS © )ÚosÚpathÚbasename)Ú.0Úcard©Z	bad_cardsr   úR/home/ghrups/robot-bench/.venv/lib/python3.9/site-packages/OpenGL/EGL/gbmdevice.pyÚ
<listcomp>'   s   ÿz&filter_bad_drivers.<locals>.<listcomp>)	ÚsetÚglobr   r   r   ÚreadlinkÚlogÚdebugÚadd)ZcardsZbad_driversÚlinkÚbaseZpci_idZdriverr   Úfilteredr   r   r   Úfilter_bad_drivers   s    
ÿr#   c                  C   s   ddl } tt|   d¡ƒƒS )zšEnumerate the set of gbm renderD* devices on the system
    
    Attempts to filter out any nvidia drivers with filter_bad_drivers
    along the way.
    r   Nz/dev/dri/card*)r   r#   Úsorted)r   r   r   r   r   .   s    r   c                 C   s    t | tƒrFztƒ }|t } W qT tyB   tdt|ƒ| f ƒ‚Y qT0 ntj d| ¡} t	 
d| ¡ t| dƒ}t | ¡ ¡}|dkr”| ¡  td|  ƒ‚|t|< |S )ac  Open a particular gbm device
    
    * path -- integer index of devices in sorted enumeration, or
              device basename (`renderD128`) or a full path-name
              as returned from enumerate_devices

    Will raise (at least):

    * RuntimeError for invalid indices
    * IOError/OSError for device access failures
    * RuntimeError if we cannot create the gbm device

    Caller is responsible for calling close_device(display) on the 
    resulting opaque pointer in order to release the open file-handle
    and deallocate the gbm_device record.

    returns GBMDevice, an opaque pointer
    z0Only %s devices available, cannot use 0-index %sz/dev/drizFinal device path: %sÚwr   z)Unable to create rendering device for: %s)Ú
isinstanceÚintr   Ú
IndexErrorÚRuntimeErrorÚlenr   r   Újoinr   r   Úopenr   Úgbm_create_deviceÚfilenoÚcloseÚ_DEVICE_HANDLES)r   ZdevicesÚfhÚdevr   r   r   r   7   s    

r   c                 C   s8   t  | ¡ zt | ¡}| ¡  W n ty2   Y n0 dS )zClose an opened gbm deviceN)r   Zgbm_device_destroyr0   Úpopr/   ÚKeyError)ÚdeviceÚhandler   r   r   r   Z   s    

r   i   c                 C   s   |st dƒ‚t | ||||¡S )aR  Create a GBM surface to use on the given device
    
    devices -- opaque GBMDevice pointer
    width,height -- dimensions
    format -- EGL_NATIVE_VISUAL_ID from an EGL configuration
    flags -- surface flags regarding reading/writing pattern that
             is expected for the buffer
    
    returns GBMSurface opaque pointer
    z\Use eglGetConfigAttrib(display,config,EGL_NATIVE_VISUAL_ID) to get the native surface format)Ú
ValueErrorr   Úgbm_surface_create)r5   ÚwidthÚheightÚformatÚflagsr   r   r   Úcreate_surfacec   s
    ÿr=   )r   )#Ú__doc__ÚweakrefÚctypesÚloggingr   r   ZOpenGL.platformr   ZOpenGLr   Ú	getLoggerÚ__name__r   ZloadLibraryÚCDLLr   Ú__all__r0   ZGBM_BO_USE_SCANOUTZGBM_BO_USE_CURSORZGBM_BO_USE_CURSOR_64X64ZGBM_BO_USE_RENDERINGZGBM_BO_USE_WRITEZGBM_BO_USE_LINEARZopaque_pointer_clsr   r   r-   Úrestyper8   r#   r   r   r   r=   r   r   r   r   Ú<module>   s,   (



	#	