
    3j                        S r SSKrSSKrSSKrSSKrSSKrSSKJr  SSKJ	r	  \R                  " \5      r\R                  " \R                  S5      rSr0 rSrSr\rS	rS
rSr\	R0                  " S5      r\	R0                  " S5      r\\R6                  l        \\R:                  l        SS jrS rS r S r!SSS\4S jr"g)zUtility 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
GBMSurfacec                 F   [        5       n[        R                  " S5       H  n[        R                  R	                  U5      nUSS n[        R                  R	                  [        R
                  " SU< S35      5      nXa;   d  Mf  [        R                  R	                  [        R
                  " U5      5      n[        R                  SXv5        UR                  U5        M     U  Vs/ s H*  n[        R                  R	                  U5      U;  d  M(  UPM,     nnU$ s  snf )zALookup the driver for each card to exclude loading nvidia devicesz/dev/dri/by-path/pci-*-cardr   z/sys/bus/pci/devices/z/driverz%Skipping %s because it uses %s driver)	setglobospathbasenamereadlinklogdebugadd)	cardsbad_drivers	bad_cardslinkbasepci_iddrivercardfiltereds	            N/home/wildlama/miniconda3/lib/python3.13/site-packages/OpenGL/EGL/gbmdevice.pyfilter_bad_driversr%      s    
 I		78ww%a!!"++PV.X"YZ 77##BKK$56DII=dJMM$ 9 77D!2 	  
 Os   )'DDc                  R    SSK n [        [        U R                   " S5      5      5      $ )zEnumerate 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   s    r$   r   r   .   s!     fTYY/?%@ABB    c                    [        U [        5      (       a   [        5       nU[           n O [        R                  R                  SU 5      n [        R                  SU 5        [        U S5      n[        R                  UR                  5       5      nUS:X  a  UR                  5         [	        SU -  5      eU[         U'   U$ ! [         a    [	        S[        W5      < SU < 35      ef = f)a/  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
zOnly z' devices available, cannot use 0-index z/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   devicesfhdevs       r$   r   r   7   s    & $s	g')G3<D ww||Jt,II%t,	d3B


		
,C
ax

FMNNOCJ  	gSVW^S_`deff	gs   B? ?'C&c                     [         R                  U 5         [        R                  U 5      nUR	                  5         g! [
         a     gf = f)zClose an opened gbm deviceN)r   gbm_device_destroyr5   popr4   KeyError)devicehandles     r$   r   r   Z   s@    6" $$V, s   %= 
A
	A
i   c                 T    U(       d  [        S5      e[        R                  XX#U5      $ )a.  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)r=   widthheightformatflagss        r$   create_surfacerF   c   s.     j
 	
 !!&GGr(   ))nvidia)#__doc__weakrefctypesloggingr   r   OpenGL.platformr   OpenGLr   	getLogger__name__r   loadLibraryCDLLr   __all__r5   GBM_BO_USE_SCANOUTGBM_BO_USE_CURSORGBM_BO_USE_CURSOR_64X64GBM_BO_USE_RENDERINGGBM_BO_USE_WRITEGBM_BO_USE_LINEARopaque_pointer_clsr   r   r2   restyperA   r%   r   r   r   rF    r(   r$   <module>r\      s   
 * ) ) ( !v{{51
B  +    &&{3	''5
 )   !+   *C"F "%SEY Hr(   