
    _e                     |   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j                  e      Z ej                  ej                  d      ZdZi ZdZdZeZd	Zd
ZdZ e	j0                  d      Z e	j0                  d      Zeej6                  _        eej:                  _        ddZd Zd Z d Z!dddefdZ"y)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                 *   t               }t        j                  d      D ]  }t        j                  j	                  |      }|dd }t        j                  j	                  t        j
                  d|d            }||v sbt        j                  j	                  t        j
                  |            }t        j                  d||       |j                  |        | D cg c]%  }t        j                  j	                  |      |vr|' }}|S c c}w )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	            6/usr/lib/python3/dist-packages/OpenGL/EGL/gbmdevice.pyfilter_bad_driversr%      s    
 I		78  ww%a!!"++PV.X"YZ[ 77##BKK$56DII=d6JMM$  77D!2 	H 
 Os   "*Dc                  R    ddl } t        t         | j                   d                  S )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#     fYTYY/?%@ABB    c                    t        | t              r	 t               }|t           } n t        j                  j                  d|       } t        j                  d|        t        | d      }t        j                  |j                               }|dk(  r|j                          t	        d| z        |t         |<   |S # t        $ r t	        dt              d|       w xY w)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
    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   B9 9$Cc                     t         j                  |        	 t        j                  |       }|j	                          y# t
        $ r Y yw xY w)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                 N    |st        d      t        j                  | ||||      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)r=   widthheightformatflagss        r$   create_surfacerF   c   s2     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   
 * ) ) ( g!lv{{51
B  +    &G&&{3	'W''5
 )   !+   *C"F "%SEY Hr(   