Skip to content

Commit 634d13a

Browse files
authored
Merge pull request #2334 from courtois-neuromod/fix/glfw_clipboard_perf_stability
move clipboard updates to a focus callback, perfs enh + stability fix
2 parents e9bf7ef + d17b571 commit 634d13a

File tree

6 files changed

+32
-46
lines changed

6 files changed

+32
-46
lines changed

pupil_src/launchables/eye.py

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import platform
1313
import signal
1414
import time
15+
from functools import partial
1516
from types import SimpleNamespace
1617

1718

@@ -281,16 +282,7 @@ def consume_events_and_render_buffer():
281282
cpu_graph.draw()
282283

283284
# render GUI
284-
try:
285-
clipboard = glfw.get_clipboard_string(main_window).decode()
286-
except (AttributeError, glfw.GLFWError):
287-
# clipboard is None, might happen on startup
288-
clipboard = ""
289-
g_pool.gui.update_clipboard(clipboard)
290285
user_input = g_pool.gui.update()
291-
if user_input.clipboard != clipboard:
292-
# only write to clipboard if content changed
293-
glfw.set_clipboard_string(main_window, user_input.clipboard)
294286

295287
for button, action, mods in user_input.buttons:
296288
x, y = glfw.get_cursor_pos(main_window)
@@ -564,6 +556,8 @@ def set_window_size():
564556
g_pool.writer = None
565557
g_pool.rec_path = None
566558

559+
on_focus = partial(gl_utils.window_focus_clipboard_callback, g_pool)
560+
567561
# Register callbacks main_window
568562
glfw.set_framebuffer_size_callback(main_window, on_resize)
569563
glfw.set_window_iconify_callback(main_window, on_iconify)
@@ -573,6 +567,7 @@ def set_window_size():
573567
glfw.set_cursor_pos_callback(main_window, on_pos)
574568
glfw.set_scroll_callback(main_window, on_scroll)
575569
glfw.set_drop_callback(main_window, on_drop)
570+
glfw.set_window_focus_callback(main_window, on_focus)
576571

577572
# load last gui configuration
578573
g_pool.gui.configuration = session_settings.get("ui_config", {})

pupil_src/launchables/player.py

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -654,6 +654,8 @@ def set_window_size():
654654
),
655655
)
656656

657+
on_focus = partial(gl_utils.window_focus_clipboard_callback, g_pool)
658+
657659
# Register callbacks main_window
658660
glfw.set_framebuffer_size_callback(main_window, on_resize)
659661
glfw.set_key_callback(main_window, on_window_key)
@@ -662,6 +664,7 @@ def set_window_size():
662664
glfw.set_cursor_pos_callback(main_window, on_pos)
663665
glfw.set_scroll_callback(main_window, on_scroll)
664666
glfw.set_drop_callback(main_window, on_drop)
667+
glfw.set_window_focus_callback(main_window, on_focus)
665668

666669
toggle_general_settings(True)
667670

@@ -741,16 +744,7 @@ def handle_notifications(n):
741744

742745
gl_utils.glViewport(0, 0, *window_size)
743746

744-
try:
745-
clipboard = glfw.get_clipboard_string(main_window).decode()
746-
except (AttributeError, glfw.GLFWError):
747-
# clipbaord is None, might happen on startup
748-
clipboard = ""
749-
g_pool.gui.update_clipboard(clipboard)
750747
user_input = g_pool.gui.update()
751-
if user_input.clipboard and user_input.clipboard != clipboard:
752-
# only write to clipboard if content changed
753-
glfw.set_clipboard_string(main_window, user_input.clipboard)
754748

755749
for b in user_input.buttons:
756750
button, action, mods = b

pupil_src/launchables/world.py

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import os
1212
import platform
1313
import signal
14+
from functools import partial
1415
from types import SimpleNamespace
1516

1617

@@ -344,16 +345,7 @@ def consume_events_and_render_buffer():
344345
p.gl_display()
345346

346347
gl_utils.glViewport(0, 0, *window_size)
347-
try:
348-
clipboard = glfw.get_clipboard_string(main_window).decode()
349-
except (AttributeError, glfw.GLFWError):
350-
# clipboard is None, might happen on startup
351-
clipboard = ""
352-
g_pool.gui.update_clipboard(clipboard)
353348
user_input = g_pool.gui.update()
354-
if user_input.clipboard != clipboard:
355-
# only write to clipboard if content changed
356-
glfw.set_clipboard_string(main_window, user_input.clipboard)
357349

358350
for button, action, mods in user_input.buttons:
359351
x, y = glfw.get_cursor_pos(main_window)
@@ -674,6 +666,8 @@ def set_window_size():
674666
g_pool.plugin_by_name[default_capture_name], default_capture_settings
675667
)
676668

669+
on_focus = partial(gl_utils.window_focus_clipboard_callback, g_pool)
670+
677671
# Register callbacks main_window
678672
glfw.set_framebuffer_size_callback(main_window, on_resize)
679673
glfw.set_key_callback(main_window, on_window_key)
@@ -682,6 +676,7 @@ def set_window_size():
682676
glfw.set_cursor_pos_callback(main_window, on_pos)
683677
glfw.set_scroll_callback(main_window, on_scroll)
684678
glfw.set_drop_callback(main_window, on_drop)
679+
glfw.set_window_focus_callback(main_window, on_focus)
685680

686681
# gl_state settings
687682
gl_utils.basic_gl_setup()
@@ -781,16 +776,8 @@ def window_should_update():
781776
p.gl_display()
782777

783778
gl_utils.glViewport(0, 0, *window_size)
784-
try:
785-
clipboard = glfw.get_clipboard_string(main_window).decode()
786-
except (AttributeError, glfw.GLFWError):
787-
# clipboard is None, might happen on startup
788-
clipboard = ""
789-
g_pool.gui.update_clipboard(clipboard)
779+
790780
user_input = g_pool.gui.update()
791-
if user_input.clipboard != clipboard:
792-
# only write to clipboard if content changed
793-
glfw.set_clipboard_string(main_window, user_input.clipboard)
794781

795782
for button, action, mods in user_input.buttons:
796783
x, y = glfw.get_cursor_pos(main_window)

pupil_src/shared_modules/gl_utils/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,6 @@
3131
make_coord_system_norm_based,
3232
make_coord_system_pixel_based,
3333
window_coordinate_to_framebuffer_coordinate,
34+
window_focus_clipboard_callback,
3435
)
3536
from .window_position_manager import WindowPositionManager

pupil_src/shared_modules/gl_utils/utils.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,20 @@ def is_window_visible(window):
129129
return visible and not iconified
130130

131131

132+
def window_focus_clipboard_callback(g_pool, window, focused):
133+
if focused:
134+
try:
135+
clipboard = glfw.get_clipboard_string(window).decode()
136+
except (AttributeError, glfw.GLFWError):
137+
# clipboard is None, might happen on startup
138+
clipboard = ""
139+
g_pool.gui.update_clipboard(clipboard)
140+
user_input = g_pool.gui.update()
141+
if user_input.clipboard != clipboard:
142+
# only write to clipboard if content changed
143+
glfw.set_clipboard_string(main_window, user_input.clipboard)
144+
145+
132146
def cvmat_to_glmat(m):
133147
mat = np.eye(4, dtype=np.float32)
134148
mat = mat.flatten()

pupil_src/shared_modules/service_ui.py

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121

2222
GLFWErrorReporting.set_default()
2323

24+
from functools import partial
25+
2426
from plugin import System_Plugin_Base
2527
from pyglui import cygl, ui
2628
from video_capture.neon_backend.plugin import Neon_Manager
@@ -193,13 +195,16 @@ def reset_restart():
193195

194196
g_pool.menubar.append(ui.Button("Restart with default settings", reset_restart))
195197

198+
on_focus = partial(gl_utils.window_focus_clipboard_callback, g_pool)
199+
196200
# Register callbacks main_window
197201
glfw.set_framebuffer_size_callback(main_window, on_resize)
198202
glfw.set_key_callback(main_window, on_window_key)
199203
glfw.set_char_callback(main_window, on_window_char)
200204
glfw.set_mouse_button_callback(main_window, on_window_mouse_button)
201205
glfw.set_cursor_pos_callback(main_window, on_pos)
202206
glfw.set_scroll_callback(main_window, on_scroll)
207+
glfw.set_window_focus_callback(main_window, on_focus)
203208
g_pool.gui.configuration = ui_config
204209
gl_utils.basic_gl_setup()
205210

@@ -217,16 +222,6 @@ def update_ui(self):
217222
gl_utils.glViewport(0, 0, *self.window_size)
218223
glfw.poll_events()
219224
self.gl_display()
220-
try:
221-
clipboard = glfw.get_clipboard_string(self.g_pool.main_window).decode()
222-
except (AttributeError, glfw.GLFWError):
223-
# clipbaord is None, might happen on startup
224-
clipboard = ""
225-
self.g_pool.gui.update_clipboard(clipboard)
226-
user_input = self.g_pool.gui.update()
227-
if user_input.clipboard and user_input.clipboard != clipboard:
228-
# only write to clipboard if content changed
229-
glfw.set_clipboard_string(self.g_pool.main_window, user_input.clipboard)
230225

231226
glfw.swap_buffers(self.g_pool.main_window)
232227
else:

0 commit comments

Comments
 (0)