From af844f2246469f97dcacffe55d5bcc6fd6acc011 Mon Sep 17 00:00:00 2001 From: Roman Roibu Date: Tue, 12 Jan 2021 22:24:22 +0100 Subject: [PATCH 1/5] Add GLFWErrorReporting utility class --- pupil_src/shared_modules/gl_utils/__init__.py | 1 + pupil_src/shared_modules/gl_utils/utils.py | 59 ++++++++++++++++++- 2 files changed, 58 insertions(+), 2 deletions(-) diff --git a/pupil_src/shared_modules/gl_utils/__init__.py b/pupil_src/shared_modules/gl_utils/__init__.py index 3dd65f571f..30587d0008 100644 --- a/pupil_src/shared_modules/gl_utils/__init__.py +++ b/pupil_src/shared_modules/gl_utils/__init__.py @@ -28,5 +28,6 @@ make_coord_system_norm_based, make_coord_system_pixel_based, window_coordinate_to_framebuffer_coordinate, + GLFWErrorReporting, ) from .window_position_manager import WindowPositionManager diff --git a/pupil_src/shared_modules/gl_utils/utils.py b/pupil_src/shared_modules/gl_utils/utils.py index 83c9f5cfa5..64bde7fdc4 100644 --- a/pupil_src/shared_modules/gl_utils/utils.py +++ b/pupil_src/shared_modules/gl_utils/utils.py @@ -8,6 +8,7 @@ See COPYING and COPYING.LESSER for license details. ---------------------------------------------------------------------------~(*) """ +import contextlib import logging import math import typing as T @@ -41,8 +42,6 @@ ) from OpenGL.GLU import gluErrorString, gluPerspective -glfw.ERROR_REPORTING = "raise" - # OpenGL.FULL_LOGGING = True OpenGL.ERROR_LOGGING = False @@ -330,3 +329,59 @@ def get_window_title_bar_rect(window) -> _Rectangle: return _Rectangle( x=frame_rect.x, y=frame_rect.y, width=frame_rect.width, height=frame_edges.top ) + + +_GLFWErrorReportingDict = T.Dict[T.Union[None, int], str] + + +class GLFWErrorReporting: + @classmethod + @contextlib.contextmanager + def glfw_create_window(cls): + ignore = [ + # GLFWError: (65544) b'Cocoa: Failed to find service port for display' + # This happens on macOS Big Sur running on Apple Silicone hardware + 65544, + ] + with cls.error_code_handling(ignore=tuple(ignore)): + yield + + @classmethod + @contextlib.contextmanager + def error_code_handling( + cls, + *_, + ignore: T.Optional[T.Tuple[int]] = None, + debug: T.Optional[T.Tuple[int]] = None, + warn: T.Optional[T.Tuple[int]] = None, + raise_: T.Optional[T.Tuple[int]] = None, + ): + old_reporting = glfw.ERROR_REPORTING + + if isinstance(old_reporting, dict): + new_reporting: _GLFWErrorReportingDict = dict(**old_reporting) + else: + new_reporting = cls.__default_error_reporting() + + new_reporting.update({err_code: "ignore" for err_code in ignore or ()}) + new_reporting.update({err_code: "log" for err_code in debug or ()}) + new_reporting.update({err_code: "warn" for err_code in warn or ()}) + new_reporting.update({err_code: "raise" for err_code in raise_ or ()}) + + try: + yield + finally: + glfw.ERROR_REPORTING = old_reporting + + @classmethod + def set_default(cls): + glfw.ERROR_REPORTING = cls.__default_error_reporting() + + ### Private + + @staticmethod + def __default_error_reporting() -> _GLFWErrorReportingDict: + return {None: "raise"} + + +GLFWErrorReporting.set_default() From bc9039bb8f9de71f7cea3691f454810b549058fc Mon Sep 17 00:00:00 2001 From: Roman Roibu Date: Tue, 12 Jan 2021 22:26:28 +0100 Subject: [PATCH 2/5] Set default glfw error reporting with GLFWErrorReporting --- pupil_src/launchables/eye.py | 3 ++- pupil_src/launchables/player.py | 6 ++++-- pupil_src/launchables/world.py | 3 ++- .../calibration_choreography/controller/gui_monitor.py | 3 ++- .../calibration_choreography/controller/gui_window.py | 6 +++--- .../calibration_choreography/natural_feature_plugin.py | 3 ++- .../calibration_choreography/screen_marker_plugin.py | 3 ++- .../calibration_choreography/single_marker_plugin.py | 3 ++- pupil_src/shared_modules/camera_intrinsics_estimation.py | 6 +++--- .../controller/reference_location_controllers.py | 3 ++- .../shared_modules/gl_utils/window_position_manager.py | 6 +++--- pupil_src/shared_modules/head_pose_tracker/ui/gl_window.py | 3 ++- pupil_src/shared_modules/log_display.py | 6 +++--- pupil_src/shared_modules/marker_auto_trim_marks.py | 3 ++- .../pupil_detector_plugins/detector_2d_plugin.py | 6 ++++-- pupil_src/shared_modules/roi.py | 3 ++- pupil_src/shared_modules/service_ui.py | 5 +++-- pupil_src/shared_modules/surface_tracker/gui.py | 3 ++- pupil_src/shared_modules/system_graphs.py | 5 +++-- pupil_src/shared_modules/video_overlay/ui/interactions.py | 5 +++-- pupil_src/shared_modules/vis_watermark.py | 3 ++- pupil_src/shared_modules/visualizer.py | 5 +++-- 22 files changed, 56 insertions(+), 36 deletions(-) diff --git a/pupil_src/launchables/eye.py b/pupil_src/launchables/eye.py index 6ec1c9b451..b0bc9384c2 100644 --- a/pupil_src/launchables/eye.py +++ b/pupil_src/launchables/eye.py @@ -126,8 +126,9 @@ def eye( # display import glfw + from gl_utils import GLFWErrorReporting - glfw.ERROR_REPORTING = "raise" + GLFWErrorReporting.set_default() from pyglui import ui, graph, cygl from pyglui.cygl.utils import draw_points, RGBA, draw_polyline diff --git a/pupil_src/launchables/player.py b/pupil_src/launchables/player.py index 8a93ae8cc2..ce09c6e4dd 100644 --- a/pupil_src/launchables/player.py +++ b/pupil_src/launchables/player.py @@ -73,8 +73,9 @@ def player( # display import glfw + from gl_utils import GLFWErrorReporting - glfw.ERROR_REPORTING = "raise" + GLFWErrorReporting.set_default() # check versions for our own depedencies as they are fast-changing from pyglui import __version__ as pyglui_version @@ -808,8 +809,9 @@ def player_drop( try: import glfw + from gl_utils import GLFWErrorReporting - glfw.ERROR_REPORTING = "raise" + GLFWErrorReporting.set_default() import gl_utils from OpenGL.GL import glClearColor diff --git a/pupil_src/launchables/world.py b/pupil_src/launchables/world.py index 3008f1afc5..78e9784402 100644 --- a/pupil_src/launchables/world.py +++ b/pupil_src/launchables/world.py @@ -121,8 +121,9 @@ def detection_enabled_setter(is_on: bool): # display import glfw + from gl_utils import GLFWErrorReporting - glfw.ERROR_REPORTING = "raise" + GLFWErrorReporting.set_default() from version_utils import parse_version from pyglui import ui, cygl, __version__ as pyglui_version diff --git a/pupil_src/shared_modules/calibration_choreography/controller/gui_monitor.py b/pupil_src/shared_modules/calibration_choreography/controller/gui_monitor.py index 0e2e3b25d8..a89741a8ec 100644 --- a/pupil_src/shared_modules/calibration_choreography/controller/gui_monitor.py +++ b/pupil_src/shared_modules/calibration_choreography/controller/gui_monitor.py @@ -2,8 +2,9 @@ import typing as T import glfw +from gl_utils import GLFWErrorReporting -glfw.ERROR_REPORTING = "raise" +GLFWErrorReporting.set_default() try: from typing import OrderedDict as T_OrderedDict # Python 3.7.2 diff --git a/pupil_src/shared_modules/calibration_choreography/controller/gui_window.py b/pupil_src/shared_modules/calibration_choreography/controller/gui_window.py index 159038da83..0b3bab1a16 100644 --- a/pupil_src/shared_modules/calibration_choreography/controller/gui_window.py +++ b/pupil_src/shared_modules/calibration_choreography/controller/gui_window.py @@ -5,10 +5,10 @@ import OpenGL.GL as gl import glfw - -glfw.ERROR_REPORTING = "raise" - import gl_utils +from gl_utils import GLFWErrorReporting + +GLFWErrorReporting.set_default() from pyglui.cygl.utils import draw_polyline diff --git a/pupil_src/shared_modules/calibration_choreography/natural_feature_plugin.py b/pupil_src/shared_modules/calibration_choreography/natural_feature_plugin.py index 09c5acec27..6957bc3087 100644 --- a/pupil_src/shared_modules/calibration_choreography/natural_feature_plugin.py +++ b/pupil_src/shared_modules/calibration_choreography/natural_feature_plugin.py @@ -16,8 +16,9 @@ import numpy as np import glfw +from gl_utils import GLFWErrorReporting -glfw.ERROR_REPORTING = "raise" +GLFWErrorReporting.set_default() from methods import normalize from pyglui import ui diff --git a/pupil_src/shared_modules/calibration_choreography/screen_marker_plugin.py b/pupil_src/shared_modules/calibration_choreography/screen_marker_plugin.py index 19e70aa91b..c7170fa319 100644 --- a/pupil_src/shared_modules/calibration_choreography/screen_marker_plugin.py +++ b/pupil_src/shared_modules/calibration_choreography/screen_marker_plugin.py @@ -17,8 +17,9 @@ import OpenGL.GL as gl import glfw +from gl_utils import GLFWErrorReporting -glfw.ERROR_REPORTING = "raise" +GLFWErrorReporting.set_default() from circle_detector import CircleTracker from platform import system diff --git a/pupil_src/shared_modules/calibration_choreography/single_marker_plugin.py b/pupil_src/shared_modules/calibration_choreography/single_marker_plugin.py index 3876a0019f..1e534edd1f 100644 --- a/pupil_src/shared_modules/calibration_choreography/single_marker_plugin.py +++ b/pupil_src/shared_modules/calibration_choreography/single_marker_plugin.py @@ -27,8 +27,9 @@ import OpenGL.GL as gl import glfw +from gl_utils import GLFWErrorReporting -glfw.ERROR_REPORTING = "raise" +GLFWErrorReporting.set_default() from .mixin import MonitorSelectionMixin from .controller import ( diff --git a/pupil_src/shared_modules/camera_intrinsics_estimation.py b/pupil_src/shared_modules/camera_intrinsics_estimation.py index 01ecbf8ea6..d0fd3730bd 100644 --- a/pupil_src/shared_modules/camera_intrinsics_estimation.py +++ b/pupil_src/shared_modules/camera_intrinsics_estimation.py @@ -27,10 +27,10 @@ from pyglui.ui import get_opensans_font_path import glfw - -glfw.ERROR_REPORTING = "raise" - import gl_utils +from gl_utils import GLFWErrorReporting + +GLFWErrorReporting.set_default() from plugin import Plugin diff --git a/pupil_src/shared_modules/gaze_producer/controller/reference_location_controllers.py b/pupil_src/shared_modules/gaze_producer/controller/reference_location_controllers.py index 14c10d5f41..5315066b02 100644 --- a/pupil_src/shared_modules/gaze_producer/controller/reference_location_controllers.py +++ b/pupil_src/shared_modules/gaze_producer/controller/reference_location_controllers.py @@ -14,8 +14,9 @@ import numpy as np import glfw +from gl_utils import GLFWErrorReporting -glfw.ERROR_REPORTING = "raise" +GLFWErrorReporting.set_default() import tasklib from gaze_producer import model, worker diff --git a/pupil_src/shared_modules/gl_utils/window_position_manager.py b/pupil_src/shared_modules/gl_utils/window_position_manager.py index 3c5fbfaf90..ee93b57390 100644 --- a/pupil_src/shared_modules/gl_utils/window_position_manager.py +++ b/pupil_src/shared_modules/gl_utils/window_position_manager.py @@ -13,10 +13,10 @@ import typing as T import glfw - -glfw.ERROR_REPORTING = "raise" - import gl_utils +from .utils import GLFWErrorReporting + +GLFWErrorReporting.set_default() class WindowPositionManager: diff --git a/pupil_src/shared_modules/head_pose_tracker/ui/gl_window.py b/pupil_src/shared_modules/head_pose_tracker/ui/gl_window.py index 3625da2be5..9b37b30304 100644 --- a/pupil_src/shared_modules/head_pose_tracker/ui/gl_window.py +++ b/pupil_src/shared_modules/head_pose_tracker/ui/gl_window.py @@ -16,8 +16,9 @@ import gl_utils import glfw +from gl_utils import GLFWErrorReporting -glfw.ERROR_REPORTING = "raise" +GLFWErrorReporting.set_default() from observable import Observable diff --git a/pupil_src/shared_modules/log_display.py b/pupil_src/shared_modules/log_display.py index 6526623e14..fe48023312 100644 --- a/pupil_src/shared_modules/log_display.py +++ b/pupil_src/shared_modules/log_display.py @@ -16,10 +16,10 @@ from pyglui.pyfontstash import fontstash from pyglui.ui import get_opensans_font_path import glfw - -glfw.ERROR_REPORTING = "raise" - import gl_utils +from gl_utils import GLFWErrorReporting + +GLFWErrorReporting.set_default() def color_from_level(lvl): diff --git a/pupil_src/shared_modules/marker_auto_trim_marks.py b/pupil_src/shared_modules/marker_auto_trim_marks.py index 192a902e8f..0096557f44 100644 --- a/pupil_src/shared_modules/marker_auto_trim_marks.py +++ b/pupil_src/shared_modules/marker_auto_trim_marks.py @@ -32,8 +32,9 @@ ) import glfw +from gl_utils import GLFWErrorReporting -glfw.ERROR_REPORTING = "raise" +GLFWErrorReporting.set_default() import numpy as np from itertools import groupby diff --git a/pupil_src/shared_modules/pupil_detector_plugins/detector_2d_plugin.py b/pupil_src/shared_modules/pupil_detector_plugins/detector_2d_plugin.py index 04687c475d..89b092c3c5 100644 --- a/pupil_src/shared_modules/pupil_detector_plugins/detector_2d_plugin.py +++ b/pupil_src/shared_modules/pupil_detector_plugins/detector_2d_plugin.py @@ -16,15 +16,17 @@ import glfw -glfw.ERROR_REPORTING = "raise" - from gl_utils import ( adjust_gl_view, basic_gl_setup, clear_gl_screen, make_coord_system_norm_based, make_coord_system_pixel_based, + GLFWErrorReporting, ) + +GLFWErrorReporting.set_default() + from methods import normalize from plugin import Plugin diff --git a/pupil_src/shared_modules/roi.py b/pupil_src/shared_modules/roi.py index 03b5900726..10696e95cf 100644 --- a/pupil_src/shared_modules/roi.py +++ b/pupil_src/shared_modules/roi.py @@ -19,8 +19,9 @@ from pyglui.cygl.utils import draw_polyline as cygl_draw_polyline import glfw +from gl_utils import GLFWErrorReporting -glfw.ERROR_REPORTING = "raise" +GLFWErrorReporting.set_default() from methods import denormalize, normalize from observable import Observable diff --git a/pupil_src/shared_modules/service_ui.py b/pupil_src/shared_modules/service_ui.py index 48b862b83c..2172964ab3 100644 --- a/pupil_src/shared_modules/service_ui.py +++ b/pupil_src/shared_modules/service_ui.py @@ -18,10 +18,11 @@ from OpenGL.GL import GL_COLOR_BUFFER_BIT import glfw +import gl_utils +from gl_utils import GLFWErrorReporting -glfw.ERROR_REPORTING = "raise" +GLFWErrorReporting.set_default() -import gl_utils from pyglui import ui, cygl from plugin import System_Plugin_Base diff --git a/pupil_src/shared_modules/surface_tracker/gui.py b/pupil_src/shared_modules/surface_tracker/gui.py index 3094d046e6..1189ab898d 100644 --- a/pupil_src/shared_modules/surface_tracker/gui.py +++ b/pupil_src/shared_modules/surface_tracker/gui.py @@ -21,8 +21,9 @@ import gl_utils import glfw +from gl_utils import GLFWErrorReporting -glfw.ERROR_REPORTING = "raise" +GLFWErrorReporting.set_default() from .surface_marker import Surface_Marker_Type diff --git a/pupil_src/shared_modules/system_graphs.py b/pupil_src/shared_modules/system_graphs.py index 4a2a4af074..53b015c30c 100644 --- a/pupil_src/shared_modules/system_graphs.py +++ b/pupil_src/shared_modules/system_graphs.py @@ -12,10 +12,11 @@ import os import psutil import glfw +import gl_utils +from gl_utils import GLFWErrorReporting -glfw.ERROR_REPORTING = "raise" +GLFWErrorReporting.set_default() -import gl_utils from pyglui import ui, graph from pyglui.cygl.utils import RGBA, mix_smooth from plugin import System_Plugin_Base diff --git a/pupil_src/shared_modules/video_overlay/ui/interactions.py b/pupil_src/shared_modules/video_overlay/ui/interactions.py index 259cf96b22..2b88d2ad78 100644 --- a/pupil_src/shared_modules/video_overlay/ui/interactions.py +++ b/pupil_src/shared_modules/video_overlay/ui/interactions.py @@ -9,10 +9,11 @@ ---------------------------------------------------------------------------~(*) """ import glfw +import gl_utils +from gl_utils import GLFWErrorReporting -glfw.ERROR_REPORTING = "raise" +GLFWErrorReporting.set_default() -import gl_utils from methods import normalize, denormalize diff --git a/pupil_src/shared_modules/vis_watermark.py b/pupil_src/shared_modules/vis_watermark.py index e2e0fcbc5e..eb04b8eb1c 100644 --- a/pupil_src/shared_modules/vis_watermark.py +++ b/pupil_src/shared_modules/vis_watermark.py @@ -18,8 +18,9 @@ from pyglui import ui import glfw +from gl_utils import GLFWErrorReporting -glfw.ERROR_REPORTING = "raise" +GLFWErrorReporting.set_default() from methods import normalize, denormalize import logging diff --git a/pupil_src/shared_modules/visualizer.py b/pupil_src/shared_modules/visualizer.py index 612052a881..0e6d0a9b31 100644 --- a/pupil_src/shared_modules/visualizer.py +++ b/pupil_src/shared_modules/visualizer.py @@ -9,10 +9,11 @@ ---------------------------------------------------------------------------~(*) """ import glfw +import gl_utils +from gl_utils import GLFWErrorReporting -glfw.ERROR_REPORTING = "raise" +GLFWErrorReporting.set_default() -import gl_utils from OpenGL.GL import ( GL_BLEND, GL_COLOR_BUFFER_BIT, From 34594a658f404baf54cf9b994cc9e585422895f6 Mon Sep 17 00:00:00 2001 From: Roman Roibu Date: Tue, 12 Jan 2021 22:28:41 +0100 Subject: [PATCH 3/5] Wrap calls to glfw.create_window in a special glfw error handling context This context ignores error codes that are known to be returned by glfw --- pupil_src/launchables/eye.py | 3 ++- pupil_src/launchables/player.py | 6 +++-- pupil_src/launchables/world.py | 7 +++--- .../controller/gui_window.py | 15 +++++++------ .../camera_intrinsics_estimation.py | 15 +++++++------ .../head_pose_tracker/ui/gl_window.py | 15 +++++++------ pupil_src/shared_modules/service_ui.py | 4 +++- .../shared_modules/surface_tracker/gui.py | 15 +++++++------ pupil_src/shared_modules/visualizer.py | 22 ++++++++++--------- 9 files changed, 57 insertions(+), 45 deletions(-) diff --git a/pupil_src/launchables/eye.py b/pupil_src/launchables/eye.py index b0bc9384c2..e86c224716 100644 --- a/pupil_src/launchables/eye.py +++ b/pupil_src/launchables/eye.py @@ -446,7 +446,8 @@ def toggle_general_settings(collapsed): default_window_size = 500 + icon_bar_width, 500 width, height = session_settings.get("window_size", default_window_size) - main_window = glfw.create_window(width, height, title, None, None) + with GLFWErrorReporting.glfw_create_window(): + main_window = glfw.create_window(width, height, title, None, None) window_position_manager = gl_utils.WindowPositionManager() window_pos = window_position_manager.new_window_position( diff --git a/pupil_src/launchables/player.py b/pupil_src/launchables/player.py index ce09c6e4dd..5ea31f2ade 100644 --- a/pupil_src/launchables/player.py +++ b/pupil_src/launchables/player.py @@ -374,7 +374,8 @@ def get_dt(): glfw.init() glfw.window_hint(glfw.SCALE_TO_MONITOR, glfw.TRUE) - main_window = glfw.create_window(width, height, window_name, None, None) + with GLFWErrorReporting.glfw_create_window(): + main_window = glfw.create_window(width, height, window_name, None, None) window_position_manager = gl_utils.WindowPositionManager() window_pos = window_position_manager.new_window_position( @@ -862,7 +863,8 @@ def on_drop(window, paths): glfw.init() glfw.window_hint(glfw.SCALE_TO_MONITOR, glfw.TRUE) glfw.window_hint(glfw.RESIZABLE, 0) - window = glfw.create_window(w, h, "Pupil Player", None, None) + with GLFWErrorReporting.glfw_create_window(): + window = glfw.create_window(w, h, "Pupil Player", None, None) glfw.window_hint(glfw.RESIZABLE, 1) glfw.make_context_current(window) diff --git a/pupil_src/launchables/world.py b/pupil_src/launchables/world.py index 78e9784402..bca7eae948 100644 --- a/pupil_src/launchables/world.py +++ b/pupil_src/launchables/world.py @@ -530,9 +530,10 @@ def handle_notifications(noti): glfw.window_hint(glfw.SCALE_TO_MONITOR, glfw.TRUE) if hide_ui: glfw.window_hint(glfw.VISIBLE, 0) # hide window - main_window = glfw.create_window( - width, height, "Pupil Capture - World", None, None - ) + with GLFWErrorReporting.glfw_create_window(): + main_window = glfw.create_window( + width, height, "Pupil Capture - World", None, None + ) window_position_manager = gl_utils.WindowPositionManager() window_pos = window_position_manager.new_window_position( diff --git a/pupil_src/shared_modules/calibration_choreography/controller/gui_window.py b/pupil_src/shared_modules/calibration_choreography/controller/gui_window.py index 0b3bab1a16..e8849ddd69 100644 --- a/pupil_src/shared_modules/calibration_choreography/controller/gui_window.py +++ b/pupil_src/shared_modules/calibration_choreography/controller/gui_window.py @@ -84,13 +84,14 @@ def open( if is_fullscreen: size = gui_monitor.size - # NOTE: Always creating windowed window here, even if in fullscreen mode. On - # windows you might experience a black screen for up to 1 sec when creating - # a blank window directly in fullscreen mode. By creating it windowed and - # then switching to fullscreen it will stay white the entire time. - self.__gl_handle = glfw.create_window( - *size, title, None, glfw.get_current_context() - ) + with GLFWErrorReporting.glfw_create_window(): + # NOTE: Always creating windowed window here, even if in fullscreen mode. On + # windows you might experience a black screen for up to 1 sec when creating + # a blank window directly in fullscreen mode. By creating it windowed and + # then switching to fullscreen it will stay white the entire time. + self.__gl_handle = glfw.create_window( + *size, title, None, glfw.get_current_context() + ) if not is_fullscreen: glfw.set_window_pos(self.__gl_handle, *position) diff --git a/pupil_src/shared_modules/camera_intrinsics_estimation.py b/pupil_src/shared_modules/camera_intrinsics_estimation.py index d0fd3730bd..69627f6b73 100644 --- a/pupil_src/shared_modules/camera_intrinsics_estimation.py +++ b/pupil_src/shared_modules/camera_intrinsics_estimation.py @@ -198,13 +198,14 @@ def open_window(self): monitor = None height, width = 640, 480 - self._window = glfw.create_window( - height, - width, - "Calibration", - monitor, - glfw.get_current_context(), - ) + with GLFWErrorReporting.glfw_create_window(): + self._window = glfw.create_window( + height, + width, + "Calibration", + monitor, + glfw.get_current_context(), + ) if not self.fullscreen: # move to y = 31 for windows os glfw.set_window_pos(self._window, 200, 31) diff --git a/pupil_src/shared_modules/head_pose_tracker/ui/gl_window.py b/pupil_src/shared_modules/head_pose_tracker/ui/gl_window.py index 9b37b30304..e666d7c5a7 100644 --- a/pupil_src/shared_modules/head_pose_tracker/ui/gl_window.py +++ b/pupil_src/shared_modules/head_pose_tracker/ui/gl_window.py @@ -67,13 +67,14 @@ def _init_trackball(): def _glfw_init(self): glfw.init() glfw.window_hint(glfw.SCALE_TO_MONITOR, glfw.TRUE) - window = glfw.create_window( - 640, - 480, - "Head Pose Tracker Visualizer", - None, - glfw.get_current_context(), - ) + with GLFWErrorReporting.glfw_create_window(): + window = glfw.create_window( + 640, + 480, + "Head Pose Tracker Visualizer", + None, + glfw.get_current_context(), + ) return window @staticmethod diff --git a/pupil_src/shared_modules/service_ui.py b/pupil_src/shared_modules/service_ui.py index 2172964ab3..1c9134d395 100644 --- a/pupil_src/shared_modules/service_ui.py +++ b/pupil_src/shared_modules/service_ui.py @@ -58,7 +58,9 @@ def __init__( glfw.window_hint(glfw.SCALE_TO_MONITOR, glfw.TRUE) if g_pool.hide_ui: glfw.window_hint(glfw.VISIBLE, 0) # hide window - main_window = glfw.create_window(*window_size, "Pupil Service", None, None) + + with GLFWErrorReporting.glfw_create_window(): + main_window = glfw.create_window(*window_size, "Pupil Service", None, None) window_position_manager = gl_utils.WindowPositionManager() window_position = window_position_manager.new_window_position( diff --git a/pupil_src/shared_modules/surface_tracker/gui.py b/pupil_src/shared_modules/surface_tracker/gui.py index 1189ab898d..cfc4521b00 100644 --- a/pupil_src/shared_modules/surface_tracker/gui.py +++ b/pupil_src/shared_modules/surface_tracker/gui.py @@ -561,13 +561,14 @@ def open_window(self): win_h = 640 win_w = int(win_h / surface_aspect_ratio) - self._window = glfw.create_window( - win_h, - win_w, - "Reference Surface: " + self.surface.name, - monitor, - glfw.get_current_context(), - ) + with GLFWErrorReporting.glfw_create_window(): + self._window = glfw.create_window( + win_h, + win_w, + "Reference Surface: " + self.surface.name, + monitor, + glfw.get_current_context(), + ) glfw.set_window_pos( self._window, diff --git a/pupil_src/shared_modules/visualizer.py b/pupil_src/shared_modules/visualizer.py index 0e6d0a9b31..e78c6ed721 100644 --- a/pupil_src/shared_modules/visualizer.py +++ b/pupil_src/shared_modules/visualizer.py @@ -206,17 +206,19 @@ def open_window(self): if self.run_independently: glfw.init() glfw.window_hint(glfw.SCALE_TO_MONITOR, glfw.TRUE) - self.window = glfw.create_window( - self.window_size[0], self.window_size[1], self.name, None, None - ) + with GLFWErrorReporting.glfw_create_window(): + self.window = glfw.create_window( + self.window_size[0], self.window_size[1], self.name, None, None + ) else: - self.window = glfw.create_window( - self.window_size[0], - self.window_size[1], - self.name, - None, - glfw.get_current_context(), - ) + with GLFWErrorReporting.glfw_create_window(): + self.window = glfw.create_window( + self.window_size[0], + self.window_size[1], + self.name, + None, + glfw.get_current_context(), + ) self.other_window = glfw.get_current_context() From dd920258ad3c64ad491b7085e4c962ac15ed32b1 Mon Sep 17 00:00:00 2001 From: Roman Roibu Date: Wed, 13 Jan 2021 22:11:01 +0100 Subject: [PATCH 4/5] Fix GLFWErrorReporting.error_code_handling --- pupil_src/shared_modules/gl_utils/utils.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/pupil_src/shared_modules/gl_utils/utils.py b/pupil_src/shared_modules/gl_utils/utils.py index 64bde7fdc4..be79bc173b 100644 --- a/pupil_src/shared_modules/gl_utils/utils.py +++ b/pupil_src/shared_modules/gl_utils/utils.py @@ -359,7 +359,7 @@ def error_code_handling( old_reporting = glfw.ERROR_REPORTING if isinstance(old_reporting, dict): - new_reporting: _GLFWErrorReportingDict = dict(**old_reporting) + new_reporting: _GLFWErrorReportingDict = dict(old_reporting) else: new_reporting = cls.__default_error_reporting() @@ -368,6 +368,8 @@ def error_code_handling( new_reporting.update({err_code: "warn" for err_code in warn or ()}) new_reporting.update({err_code: "raise" for err_code in raise_ or ()}) + glfw.ERROR_REPORTING = new_reporting + try: yield finally: From b50bd63727852dd3fa8bb3b4358c75afc5fc4c22 Mon Sep 17 00:00:00 2001 From: Roman Roibu Date: Sun, 17 Jan 2021 21:55:07 +0100 Subject: [PATCH 5/5] Wrap calls to glfw.init instead of glfw.create_window --- pupil_src/launchables/eye.py | 6 ++--- pupil_src/launchables/player.py | 13 +++++----- pupil_src/launchables/world.py | 10 ++++---- .../controller/gui_window.py | 15 ++++++----- .../camera_intrinsics_estimation.py | 15 ++++++----- pupil_src/shared_modules/gl_utils/utils.py | 2 +- .../head_pose_tracker/ui/gl_window.py | 18 ++++++------- pupil_src/shared_modules/service_ui.py | 6 ++--- .../shared_modules/surface_tracker/gui.py | 15 ++++++----- pupil_src/shared_modules/visualizer.py | 25 +++++++++---------- 10 files changed, 60 insertions(+), 65 deletions(-) diff --git a/pupil_src/launchables/eye.py b/pupil_src/launchables/eye.py index e86c224716..f8eca4d569 100644 --- a/pupil_src/launchables/eye.py +++ b/pupil_src/launchables/eye.py @@ -433,7 +433,8 @@ def toggle_general_settings(collapsed): general_settings.collapsed = collapsed # Initialize glfw - glfw.init() + with GLFWErrorReporting.glfw_init(): + glfw.init() glfw.window_hint(glfw.SCALE_TO_MONITOR, glfw.TRUE) if hide_ui: glfw.window_hint(glfw.VISIBLE, 0) # hide window @@ -446,8 +447,7 @@ def toggle_general_settings(collapsed): default_window_size = 500 + icon_bar_width, 500 width, height = session_settings.get("window_size", default_window_size) - with GLFWErrorReporting.glfw_create_window(): - main_window = glfw.create_window(width, height, title, None, None) + main_window = glfw.create_window(width, height, title, None, None) window_position_manager = gl_utils.WindowPositionManager() window_pos = window_position_manager.new_window_position( diff --git a/pupil_src/launchables/player.py b/pupil_src/launchables/player.py index 5ea31f2ade..42d0fece01 100644 --- a/pupil_src/launchables/player.py +++ b/pupil_src/launchables/player.py @@ -372,11 +372,10 @@ def get_dt(): window_name = f"Pupil Player: {meta_info.recording_name} - {rec_dir}" - glfw.init() + with GLFWErrorReporting.glfw_init(): + glfw.init() glfw.window_hint(glfw.SCALE_TO_MONITOR, glfw.TRUE) - with GLFWErrorReporting.glfw_create_window(): - main_window = glfw.create_window(width, height, window_name, None, None) - + main_window = glfw.create_window(width, height, window_name, None, None) window_position_manager = gl_utils.WindowPositionManager() window_pos = window_position_manager.new_window_position( window=main_window, @@ -860,11 +859,11 @@ def on_drop(window, paths): session_settings.clear() w, h = session_settings.get("window_size", (1280, 720)) - glfw.init() + with GLFWErrorReporting.glfw_init(): + glfw.init() glfw.window_hint(glfw.SCALE_TO_MONITOR, glfw.TRUE) glfw.window_hint(glfw.RESIZABLE, 0) - with GLFWErrorReporting.glfw_create_window(): - window = glfw.create_window(w, h, "Pupil Player", None, None) + window = glfw.create_window(w, h, "Pupil Player", None, None) glfw.window_hint(glfw.RESIZABLE, 1) glfw.make_context_current(window) diff --git a/pupil_src/launchables/world.py b/pupil_src/launchables/world.py index bca7eae948..b664d8fadb 100644 --- a/pupil_src/launchables/world.py +++ b/pupil_src/launchables/world.py @@ -526,14 +526,14 @@ def handle_notifications(noti): ) # window and gl setup - glfw.init() + with GLFWErrorReporting.glfw_init(): + glfw.init() glfw.window_hint(glfw.SCALE_TO_MONITOR, glfw.TRUE) if hide_ui: glfw.window_hint(glfw.VISIBLE, 0) # hide window - with GLFWErrorReporting.glfw_create_window(): - main_window = glfw.create_window( - width, height, "Pupil Capture - World", None, None - ) + main_window = glfw.create_window( + width, height, "Pupil Capture - World", None, None + ) window_position_manager = gl_utils.WindowPositionManager() window_pos = window_position_manager.new_window_position( diff --git a/pupil_src/shared_modules/calibration_choreography/controller/gui_window.py b/pupil_src/shared_modules/calibration_choreography/controller/gui_window.py index e8849ddd69..0b3bab1a16 100644 --- a/pupil_src/shared_modules/calibration_choreography/controller/gui_window.py +++ b/pupil_src/shared_modules/calibration_choreography/controller/gui_window.py @@ -84,14 +84,13 @@ def open( if is_fullscreen: size = gui_monitor.size - with GLFWErrorReporting.glfw_create_window(): - # NOTE: Always creating windowed window here, even if in fullscreen mode. On - # windows you might experience a black screen for up to 1 sec when creating - # a blank window directly in fullscreen mode. By creating it windowed and - # then switching to fullscreen it will stay white the entire time. - self.__gl_handle = glfw.create_window( - *size, title, None, glfw.get_current_context() - ) + # NOTE: Always creating windowed window here, even if in fullscreen mode. On + # windows you might experience a black screen for up to 1 sec when creating + # a blank window directly in fullscreen mode. By creating it windowed and + # then switching to fullscreen it will stay white the entire time. + self.__gl_handle = glfw.create_window( + *size, title, None, glfw.get_current_context() + ) if not is_fullscreen: glfw.set_window_pos(self.__gl_handle, *position) diff --git a/pupil_src/shared_modules/camera_intrinsics_estimation.py b/pupil_src/shared_modules/camera_intrinsics_estimation.py index 69627f6b73..d0fd3730bd 100644 --- a/pupil_src/shared_modules/camera_intrinsics_estimation.py +++ b/pupil_src/shared_modules/camera_intrinsics_estimation.py @@ -198,14 +198,13 @@ def open_window(self): monitor = None height, width = 640, 480 - with GLFWErrorReporting.glfw_create_window(): - self._window = glfw.create_window( - height, - width, - "Calibration", - monitor, - glfw.get_current_context(), - ) + self._window = glfw.create_window( + height, + width, + "Calibration", + monitor, + glfw.get_current_context(), + ) if not self.fullscreen: # move to y = 31 for windows os glfw.set_window_pos(self._window, 200, 31) diff --git a/pupil_src/shared_modules/gl_utils/utils.py b/pupil_src/shared_modules/gl_utils/utils.py index be79bc173b..e42ad359aa 100644 --- a/pupil_src/shared_modules/gl_utils/utils.py +++ b/pupil_src/shared_modules/gl_utils/utils.py @@ -337,7 +337,7 @@ def get_window_title_bar_rect(window) -> _Rectangle: class GLFWErrorReporting: @classmethod @contextlib.contextmanager - def glfw_create_window(cls): + def glfw_init(cls): ignore = [ # GLFWError: (65544) b'Cocoa: Failed to find service port for display' # This happens on macOS Big Sur running on Apple Silicone hardware diff --git a/pupil_src/shared_modules/head_pose_tracker/ui/gl_window.py b/pupil_src/shared_modules/head_pose_tracker/ui/gl_window.py index e666d7c5a7..42aff44a7d 100644 --- a/pupil_src/shared_modules/head_pose_tracker/ui/gl_window.py +++ b/pupil_src/shared_modules/head_pose_tracker/ui/gl_window.py @@ -65,16 +65,16 @@ def _init_trackball(): return trackball def _glfw_init(self): - glfw.init() + with GLFWErrorReporting.glfw_init(): + glfw.init() glfw.window_hint(glfw.SCALE_TO_MONITOR, glfw.TRUE) - with GLFWErrorReporting.glfw_create_window(): - window = glfw.create_window( - 640, - 480, - "Head Pose Tracker Visualizer", - None, - glfw.get_current_context(), - ) + window = glfw.create_window( + 640, + 480, + "Head Pose Tracker Visualizer", + None, + glfw.get_current_context(), + ) return window @staticmethod diff --git a/pupil_src/shared_modules/service_ui.py b/pupil_src/shared_modules/service_ui.py index 1c9134d395..fc52b42023 100644 --- a/pupil_src/shared_modules/service_ui.py +++ b/pupil_src/shared_modules/service_ui.py @@ -54,13 +54,13 @@ def __init__( self.texture = np.zeros((1, 1, 3), dtype=np.uint8) + 128 - glfw.init() + with GLFWErrorReporting.glfw_init(): + glfw.init() glfw.window_hint(glfw.SCALE_TO_MONITOR, glfw.TRUE) if g_pool.hide_ui: glfw.window_hint(glfw.VISIBLE, 0) # hide window - with GLFWErrorReporting.glfw_create_window(): - main_window = glfw.create_window(*window_size, "Pupil Service", None, None) + main_window = glfw.create_window(*window_size, "Pupil Service", None, None) window_position_manager = gl_utils.WindowPositionManager() window_position = window_position_manager.new_window_position( diff --git a/pupil_src/shared_modules/surface_tracker/gui.py b/pupil_src/shared_modules/surface_tracker/gui.py index cfc4521b00..1189ab898d 100644 --- a/pupil_src/shared_modules/surface_tracker/gui.py +++ b/pupil_src/shared_modules/surface_tracker/gui.py @@ -561,14 +561,13 @@ def open_window(self): win_h = 640 win_w = int(win_h / surface_aspect_ratio) - with GLFWErrorReporting.glfw_create_window(): - self._window = glfw.create_window( - win_h, - win_w, - "Reference Surface: " + self.surface.name, - monitor, - glfw.get_current_context(), - ) + self._window = glfw.create_window( + win_h, + win_w, + "Reference Surface: " + self.surface.name, + monitor, + glfw.get_current_context(), + ) glfw.set_window_pos( self._window, diff --git a/pupil_src/shared_modules/visualizer.py b/pupil_src/shared_modules/visualizer.py index e78c6ed721..076ee40e26 100644 --- a/pupil_src/shared_modules/visualizer.py +++ b/pupil_src/shared_modules/visualizer.py @@ -204,21 +204,20 @@ def open_window(self): # get glfw started if self.run_independently: - glfw.init() + with GLFWErrorReporting.glfw_init(): + glfw.init() glfw.window_hint(glfw.SCALE_TO_MONITOR, glfw.TRUE) - with GLFWErrorReporting.glfw_create_window(): - self.window = glfw.create_window( - self.window_size[0], self.window_size[1], self.name, None, None - ) + self.window = glfw.create_window( + self.window_size[0], self.window_size[1], self.name, None, None + ) else: - with GLFWErrorReporting.glfw_create_window(): - self.window = glfw.create_window( - self.window_size[0], - self.window_size[1], - self.name, - None, - glfw.get_current_context(), - ) + self.window = glfw.create_window( + self.window_size[0], + self.window_size[1], + self.name, + None, + glfw.get_current_context(), + ) self.other_window = glfw.get_current_context()