-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathVisionEggRenderer.py
468 lines (393 loc) · 16.5 KB
/
VisionEggRenderer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
# -*- coding: utf-8 -*-
#
# $Id: VisionEggRenderer.py 4770 2014-12-04 21:45:22Z jhill $
#
# This file is part of the BCPy2000 framework, a Python framework for
# implementing modules that run on top of the BCI2000 <http://bci2000.org/>
# platform, for the purpose of realtime biosignal processing.
#
# Copyright (C) 2007-11 Jeremy Hill, Thomas Schreiner,
# Christian Puzicha, Jason Farquhar
#
# bcpy2000@bci2000.org
#
# The BCPy2000 framework is free software: you can redistribute it
# and/or modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation, either version 3 of
# the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
__all__ = ["Text", "Block", "Disc", "ImageStimulus"]
import os, sys
import pygame # ; pygame.init()
import logging
import VisionEgg.Core
import VisionEgg.Text
import VisionEgg.WrappedText
try:
from BCI2000PythonApplication import (
BciGenericRenderer,
BciStimulus,
) # development copy
except:
from BCPy2000.GenericApplication import (
BciGenericRenderer,
BciStimulus,
) # installed copy
#################################################################
#################################################################
def delegate_getattr(self, v, key):
p = getattr(v, "parameters", None)
if p != None and hasattr(p, key):
return True, getattr(p, key)
if v != None and hasattr(v, key):
return True, getattr(v, key)
return False, None
#################################################################
def delegate_setattr(self, v, key, value, restrict_to=None):
p = getattr(v, "parameters", None)
if p != None and hasattr(p, key):
if restrict_to != None and not "parameters." + key in restrict_to:
raise AttributeError("the '%s' attribute is read-only" % key)
setattr(p, key, value)
return True
if v != None and hasattr(v, key):
if restrict_to != None and not key in restrict_to:
raise AttributeError("the '%s' attribute is read-only" % key)
setattr(v, key, value)
return True
return False
#################################################################
class VisionEggRenderer(BciGenericRenderer):
"""
This is a subclass of BciGenericRenderer that renders stimuli via the
VisionEgg package (which is based on pygame and PyOpenGL) and polls for
mouse and keyboard events via pygame. The VisionEggRenderer is our
default implementation, but you can implement other renderers (see
the documentation for the BciGenericRenderer class).
The object wraps a number of VisionEgg instances including a Screen
and a Viewport, but it behaves most like a Screen --- indeed any
attributes of the underlying VisionEgg.Core.Screen instance, and its
.parameters, are also accessible directly as if they were attributes
of this wrapper object.
In particular, the following attributes (only accessible after the
window has opened) are most useful:
.size (read-only) contains the window's (width,height) in pixels.
.bgcolor is used to get and set the background colour of the window.
.color is an alias for bgcolor.
""" ###
#############################################################
def __init__(self):
self.__dict__["_frame_timer"] = None
self.__dict__["_viewport"] = None
self.__dict__["_screen"] = None
self.__dict__["monofont"] = self.findfont(
("lucida console", "monaco", "monospace", "courier new", "courier")
)
# default config settings (can be changed in self.Preflight):
VisionEgg.config.VISIONEGG_MAX_PRIORITY = 0
VisionEgg.config.VISIONEGG_HIDE_MOUSE = 0
VisionEgg.config.VISIONEGG_GUI_INIT = 0
VisionEgg.config.VISIONEGG_FULLSCREEN = 0
VisionEgg.config.VISIONEGG_FRAMELESS_WINDOW = 1
VisionEgg.config.VISIONEGG_LOG_FILE = None
# VisionEgg.start_default_logging()
#############################################################
def use_frame_timer(self, setting=True, renew=False):
if setting:
if renew or not self._frame_timer:
self._frame_timer = VisionEgg.Core.FrameTimer()
else:
self._frame_timer = None
#############################################################
def findfont(self, fontnames):
"""
Tries to find a system font file corresponding to one of the
supplied list of names. Returns None if no match is found.
""" ###
def matchfont(fontname):
bold = italic = False
for i in range(0, 1):
if fontname.lower().endswith(" italic"):
italic = True
fontname = fontname[: -len(" italic")]
if fontname.lower().endswith(" bold"):
bold = True
fontname = fontname[: -len(" bold")]
try:
f = pygame.font.match_font(fontname, bold=int(bold), italic=int(italic))
except MemoryError:
f = (
pygame.font.get_default_font()
) # works around mysterious issue on Japanese systems reported by Chad Boulay 20121115
return f
if not isinstance(fontnames, (list, tuple)):
fontnames = [fontnames]
fontnames = [f for f in fontnames if f != None]
f = ([_f for _f in map(matchfont, fontnames) if _f] + [None])[0]
if (
f == None and sys.platform == "darwin"
): # pygame on OSX doesn't seem even to try to find fonts...
f = (
list(
filter(
os.path.isfile,
[
os.path.realpath("/Library/Fonts/%s.ttf" % x)
for x in fontnames
],
)
)
+ [None]
)[0]
return f
#############################################################
def setup(
self,
left=None,
top=None,
width=None,
height=None,
changemode=None,
framerate=None,
bitdepth=None,
**kwargs
):
"""
Call this to set certain commonly-defined parameters for the screen
during BciApplication.Preflight(). The renderer object will read
these parameters in order to initialize the stimulus window, before
BciApplication.Initialize() is called.
""" ###
BciGenericRenderer.setup(
self,
left=left,
top=top,
width=width,
height=height,
changemode=changemode,
framerate=framerate,
bitdepth=bitdepth,
**kwargs
)
pos = os.environ.get("SDL_VIDEO_WINDOW_POS", "").split(",")
if len(pos) == 2:
prevleft, prevtop = int(pos[0]), int(pos[1])
else:
prevleft, prevtop = 160, 120
if left != None and top == None:
top = prevtop
if top != None and left == None:
left = prevleft
if left != None and top != None:
if (
sys.platform != "darwin"
): # yup, yet another thing that's broken in pygame under OSX
os.environ["SDL_VIDEO_WINDOW_POS"] = "%d,%d" % (int(left), int(top))
if width != None:
VisionEgg.config.VISIONEGG_SCREEN_W = int(width)
if height != None:
VisionEgg.config.VISIONEGG_SCREEN_H = int(height)
if changemode != None:
VisionEgg.config.VISIONEGG_FULLSCREEN = int(changemode)
if framerate != None:
VisionEgg.config.VISIONEGG_MONITOR_REFRESH_HZ = float(framerate)
if bitdepth != None:
VisionEgg.config.VISIONEGG_PREFERRED_BPP = int(bitdepth)
for k, v in list(kwargs.items()):
kk = (k, k.upper(), "VISIONEGG_" + k.upper())
for k in kk:
if hasattr(VisionEgg.config, k):
setattr(VisionEgg.config, k, v)
# print "VisionEgg.config.%s = %s" % (k, repr(v))
break
else:
raise AttributeError("VisionEgg.config has no attribute '%s'" % kk[0])
#############################################################
def GetDefaultFont(self):
d = VisionEgg.Text.Text.constant_parameters_and_defaults
return d["font_name"][0], d["font_size"][0]
#############################################################
def SetDefaultFont(self, name=None, size=None):
"""
Set the name and/or size of the font that will be used
by default for Text stimuli. Returns True if the named font
can be found, False if not.
""" ###
dd = [
VisionEgg.Text.Text.constant_parameters_and_defaults,
VisionEgg.WrappedText.WrappedText.constant_parameters_and_defaults,
]
if name != None:
if os.path.isabs(name) and os.path.isfile(name):
font = name
else:
font = self.findfont(name)
if font == None:
return False
for d in dd:
d["font_name"] = (font,) + d["font_name"][1:]
if size != None:
for d in dd:
d["font_size"] = (size,) + d["font_size"][1:]
return True
#############################################################
def Initialize(self, bci=None):
self.__dict__[
"_bci"
] = bci # this is a mutual reference loop, but what the hell: self and bci only die when the process dies
logging.raiseExceptions = (
0 # suppresses the "No handlers could be found" chatter
)
pygame.quit()
pygame.init()
self._screen = VisionEgg.Core.get_default_screen()
self._viewport = VisionEgg.Core.Viewport(screen=self._screen)
self.use_frame_timer(self._frame_timer != None, renew=True)
#############################################################
def GetFrameRate(self):
if sys.platform == "darwin":
import platform
if platform.architecture()[0].startswith("64"):
print(
"query_refresh_rate is broken under darwin on 64bit architectures"
)
return float(VisionEgg.config.VISIONEGG_MONITOR_REFRESH_HZ)
try:
return float(self._screen.query_refresh_rate())
except:
print("VisionEgg failed to query refresh rate")
return float(VisionEgg.config.VISIONEGG_MONITOR_REFRESH_HZ)
#############################################################
def RaiseWindow(self):
try:
import ctypes # !! Windows-specific code.
stimwin = ctypes.windll.user32.FindWindowA(0, "Vision Egg")
if self._bci:
self._bci._raise_window(stimwin)
except:
pass
#############################################################
def GetEvents(self):
return pygame.event.get()
#############################################################
def DefaultEventHandler(self, event):
return (event.type == pygame.locals.QUIT) or (
event.type == pygame.locals.KEYDOWN and event.key == pygame.locals.K_ESCAPE
)
#############################################################
def StartFrame(self, objlist):
if self._bci:
self._bci.ftdb(label="screen.clear") # --------------------
self._screen.clear()
if self._bci:
self._bci.ftdb(label="viewport.draw") # --------------------
self._viewport.parameters.stimuli = objlist
self._viewport.draw()
#############################################################
def FinishFrame(self):
if self._bci:
self._bci.ftdb(label="swap_buffers") # --------------------
VisionEgg.Core.swap_buffers()
if self._bci:
self._bci.ftdb(label="glFlush") # --------------------
VisionEgg.GL.glFlush()
if self._frame_timer:
self._frame_timer.tick()
#############################################################
def Cleanup(self):
if self._frame_timer:
self._frame_timer.log_histogram()
self._frame_timer = True
self._viewport = None
self._screen.close()
self._screen = None
VisionEgg.Text._font_objects = {}
# VisionEgg 1.1 allowed these cached pygame.font.Font objects to persist even
# after pygame quits or is reloaded: this causes a crash the second time around.
# VisionEgg 1.0 didn't cache, so we never ran across the problem under Python 2.4.
# Andrew fixed it in VE 1.1.1.
pygame.quit()
#############################################################
def __getattr__(self, key):
if key == "color":
key = "bgcolor"
v = self.__dict__.get("_screen")
if v == None:
raise AttributeError(
"a Screen object has not yet been instantiated inside this object"
)
gotit, value = self.__delegate_getattr__(v, key)
if not gotit:
raise AttributeError(
"'%s' object has no attribute or parameter '%s'"
% (self.__class__.__name__, key)
)
return value
#############################################################
def __setattr__(self, key, value):
if key in self.__dict__:
self.__dict__[key] = value
else:
if key == "color":
key = "bgcolor"
v = self.__dict__.get("_screen")
if v == None:
raise AttributeError(
"a Screen object has not yet been instantiated inside this object"
)
if not self.__delegate_setattr__(
v, key, value, restrict_to=["parameters.bgcolor"]
):
raise AttributeError(
"'%s' object has no attribute or parameter '%s'"
% (self.__class__.__name__, key)
)
#############################################################
def _getAttributeNames(self):
v = self.__dict__.get("_screen")
if v == None:
return ()
return ("color", "bgcolor", "size", "parameters")
#############################################################
__delegate_setattr__ = delegate_setattr
__delegate_getattr__ = delegate_getattr
#################################################################
#################################################################
def GetVEParameterNames(self):
p = getattr(self.__dict__.get("obj"), "parameters", None)
if p == None:
return ()
return list(p.__dict__.keys())
BciStimulus._getAttributeNames = GetVEParameterNames
BciStimulus.__delegate_setattr__ = delegate_setattr
BciStimulus.__delegate_getattr__ = delegate_getattr
import VisionEgg.Textures, VisionEgg.GL
class ImageStimulus(VisionEgg.Textures.TextureStimulus):
"""
A subclass of VisionEgg.Textures.TextureStimulus
""" ###
def __init__(self, **kwargs):
if "texture" in kwargs and not isinstance(
kwargs["texture"], VisionEgg.Textures.Texture
):
kwargs["texture"] = VisionEgg.Textures.Texture(kwargs["texture"])
kwargs["mipmaps_enabled"] = kwargs.get("mipmaps_enabled", 0)
kwargs["internal_format"] = kwargs.get("internal_format", VisionEgg.GL.GL_RGBA)
kwargs["texture_min_filter"] = kwargs.get(
"texture_min_filter", VisionEgg.GL.GL_LINEAR
)
VisionEgg.Textures.TextureStimulus.__init__(self, **kwargs)
from VisionEgg.Text import Text
from VisionEgg.MoreStimuli import Target2D as Block
from VisionEgg.MoreStimuli import FilledCircle as Disc
BciGenericRenderer.subclass = VisionEggRenderer
#################################################################
#################################################################