<s> """<STR_LIT>""" <EOL> from __future__ import division <EOL> from ... util . ptime import time <EOL> from . . base import ( BaseApplicationBackend , BaseCanvasBackend , <EOL> BaseTimerBackend ) <EOL> from vispy . gloo import gl <EOL> from time import sleep <EOL> try : <EOL> from ... ext import osmesa <EOL> except Exception as exp : <EOL> available , testable , why_not , which = False , False , str ( exp ) , None <EOL> else : <EOL> available , testable , why_not , which = True , True , None , '<STR_LIT>' <EOL> capability = dict ( <EOL> title = True , <EOL> size = True , <EOL> position = False , <EOL> show = True , <EOL> vsync = False , <EOL> resizable = False , <EOL> decorate = True , <EOL> fullscreen = False , <EOL> context = True , <EOL> multi_window = True , <EOL> scroll = False , <EOL> parent = False , <EOL> always_on_top = False , <EOL> ) <EOL> _VP_OSMESA_ALL_WINDOWS = [ ] <EOL> def _get_osmesa_windows ( ) : <EOL> return [ win for win in _VP_OSMESA_ALL_WINDOWS <EOL> if isinstance ( win , CanvasBackend ) ] <EOL> class ApplicationBackend ( BaseApplicationBackend ) : <EOL> def __init__ ( self ) : <EOL> BaseApplicationBackend . __init__ ( self ) <EOL> self . _timers = list ( ) <EOL> def _add_timer ( self , timer ) : <EOL> if timer not in self . _timers : <EOL> self . _timers . append ( timer ) <EOL> def _vispy_get_backend_name ( self ) : <EOL> return '<STR_LIT>' <EOL> def _vispy_process_events ( self ) : <EOL> for timer in self . _timers : <EOL> timer . _tick ( ) <EOL> wins = _get_osmesa_windows ( ) <EOL> for win in wins : <EOL> if win . _needs_draw : <EOL> win . _needs_draw = False <EOL> win . _on_draw ( ) <EOL> def _vispy_run ( self ) : <EOL> wins = _get_osmesa_windows ( ) <EOL> while not all ( w . closed for w in wins ) : <EOL> self . _vispy_process_events ( ) <EOL> self . _vispy_quit ( ) <EOL> def _vispy_quit ( self ) : <EOL> wins = _get_osmesa_windows ( ) <EOL> for win in wins : <EOL> win . _vispy_close ( ) <EOL> for timer in self . _timers : <EOL> timer . _vispy_stop ( ) <EOL> self . _timers = [ ] <EOL> def _vispy_get_native_app ( self ) : <EOL> return osmesa <EOL> class OSMesaContext ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> self . context = osmesa . OSMesaCreateContext ( ) <EOL> def make_current ( self , pixels , width , height ) : <EOL> return osmesa . OSMesaMakeCurrent ( self . context , pixels , width , height ) <EOL> def __del__ ( self ) : <EOL> osmesa . OSMesaDestroyContext ( self . context ) <EOL> class CanvasBackend ( BaseCanvasBackend ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> BaseCanvasBackend . __init__ ( self , * args ) <EOL> p = self . _process_backend_kwargs ( kwargs ) <EOL> p . context . shared . add_ref ( '<STR_LIT>' , self ) <EOL> if p . context . shared . ref is self : <EOL> self . _native_context = OSMesaContext ( ) <EOL> else : <EOL> self . _native_context = p . context . shared . ref . _native_context <EOL> self . _closed = False <EOL> self . _pixels = None <EOL> self . _vispy_set_size ( * p . size ) <EOL> _VP_OSMESA_ALL_WINDOWS . append ( self ) <EOL> self . _vispy_canvas . set_current ( ) <EOL> self . _vispy_canvas . events . initialize ( ) <EOL> def _vispy_set_current ( self ) : <EOL> if self . _native_context is None : <EOL> raise RuntimeError ( '<STR_LIT>' ) <EOL> if self . _pixels is None : <EOL> raise RuntimeError ( '<STR_LIT>' ) <EOL> ok = self . _native_context . make_current ( self . _pixels , self . _size [ <NUM_LIT:0> ] , <EOL> self . _size [ <NUM_LIT:1> ] ) <EOL> if not ok : <EOL> raise RuntimeError ( '<STR_LIT>' ) <EOL> def _vispy_swap_buffers ( self ) : <EOL> if self . _pixels is None : <EOL> raise RuntimeError ( '<STR_LIT>' ) <EOL> gl . glFinish ( ) <EOL> def _vispy_set_title ( self , title ) : <EOL> pass <EOL> def _vispy_set_size ( self , w , h ) : <EOL> self . _pixels = osmesa . allocate_pixels_buffer ( w , h ) <EOL> self . _size = ( w , h ) <EOL> self . _vispy_canvas . events . resize ( size = ( w , h ) ) <EOL> self . _vispy_set_current ( ) <EOL> self . _vispy_update ( ) <EOL> def _vispy_set_position ( self , x , y ) : <EOL> pass <EOL> def _vispy_set_visible ( self , visible ) : <EOL> if visible : <EOL> self . _vispy_set_current ( ) <EOL> self . _vispy_update ( ) <EOL> def _vispy_set_fullscreen ( self , fullscreen ) : <EOL> pass <EOL> def _vispy_update ( self ) : <EOL> self . _needs_draw = True <EOL> def _vispy_close ( self ) : <EOL> if self . closed : <EOL> return <EOL> self . _closed = True <EOL> return <EOL> def _vispy_warmup ( self ) : <EOL> etime = time ( ) + <NUM_LIT:0.1> <EOL> while time ( ) < etime : <EOL> sleep ( <NUM_LIT> ) <EOL> self . _vispy_canvas . set_current ( ) <EOL> self . _vispy_canvas . app . process_events ( ) <EOL> def _vispy_get_size ( self ) : <EOL> if self . _pixels is None : <EOL> return <EOL> return self . _size <EOL> @ property <EOL> def closed ( self ) : <EOL> return self . _closed <EOL> def _vispy_get_position ( self ) : <EOL> return <NUM_LIT:0> , <NUM_LIT:0> <EOL> def _vispy_get_fullscreen ( self ) : <EOL> return False <EOL> def _on_draw ( self ) : <EOL> if self . _vispy_canvas is None or self . _pixels is None : <EOL> raise RuntimeError ( '<STR_LIT>' ) <EOL> return <EOL> self . _vispy_set_current ( ) <EOL> self . _vispy_canvas . events . draw ( region = None ) <EOL> class TimerBackend ( BaseTimerBackend ) : <EOL> def __init__ ( self , vispy_timer ) : <EOL> BaseTimerBackend . __init__ ( self , vispy_timer ) <EOL> vispy_timer . _app . _backend . _add_timer ( self ) <EOL> self . _vispy_stop ( ) <EOL> def _vispy_start ( self , interval ) : <EOL> self . _interval = interval <EOL> self . _next_time = time ( ) + self . _interval <EOL> def _vispy_stop ( self ) : <EOL> self . _next_time = float ( '<STR_LIT>' ) <EOL> def _tick ( self ) : <EOL> if time ( ) > self . _next_time : <EOL> self . _vispy_timer . _timeout ( ) <EOL> self . _next_time = time ( ) + self . _interval </s>
