Skip to content

Commit ed0361d

Browse files
kevinzakkacopybara-github
authored andcommitted
Increase the font scale if a high-DPI (e.g. retina) display is detected.
PiperOrigin-RevId: 504002233 Change-Id: I0945ee40bb25f03975b8478bacfcac6c5b7ce9ee
1 parent d5d054b commit ed0361d

1 file changed

Lines changed: 23 additions & 1 deletion

File tree

dm_control/viewer/renderer.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
import abc
1818
import contextlib
19+
import subprocess
20+
import sys
1921

2022
from dm_control.mujoco import wrapper
2123
from dm_control.viewer import util
@@ -44,6 +46,25 @@
4446
_DEFAULT_RENDER_FLAGS[mujoco.mjtRndFlag.mjRND_SKYBOX.value] = 1
4547
_DEFAULT_RENDER_FLAGS[mujoco.mjtRndFlag.mjRND_CULL_FACE.value] = 1
4648

49+
# Font scale values.
50+
_DEFAULT_FONT_SCALE = mujoco.mjtFontScale.mjFONTSCALE_100
51+
_HIDPI_FONT_SCALE = mujoco.mjtFontScale.mjFONTSCALE_200
52+
53+
54+
def _has_high_dpi() -> bool:
55+
"""Returns True if the display is a high DPI display."""
56+
if sys.platform == 'darwin':
57+
# On macOS, we can use the system_profiler command to determine if the
58+
# display is retina.
59+
return subprocess.call(
60+
'system_profiler SPDisplaysDataType | grep -i "retina"',
61+
shell=True,
62+
stdout=subprocess.DEVNULL,
63+
stderr=subprocess.DEVNULL
64+
) == 0
65+
# TODO(zakka): Figure out how to detect high DPI displays on Linux.
66+
return False
67+
4768

4869
class BaseRenderer(metaclass=abc.ABCMeta):
4970
"""A base class for component-based Mujoco Renderers implementations.
@@ -150,10 +171,11 @@ def render(self, viewport, scene):
150171
new_offheight = max(self._model.vis.global_.offheight, viewport.height)
151172
self._model.vis.global_.offwidth = new_offwidth
152173
self._model.vis.global_.offheight = new_offheight
174+
font_scale = _HIDPI_FONT_SCALE if _has_high_dpi() else _DEFAULT_FONT_SCALE
153175
self._mujoco_context = wrapper.MjrContext(
154176
model=self._model,
155177
gl_context=self._surface,
156-
font_scale=mujoco.mjtFontScale.mjFONTSCALE_100)
178+
font_scale=font_scale)
157179
self._rgb_buffer = np.empty(
158180
(viewport.height, viewport.width, 3), dtype=np.uint8)
159181

0 commit comments

Comments
 (0)