Пример #1
0
 def test_init(self):
     mock_context = mock.MagicMock()
     with mock.patch(CONTEXT_PATH) as mock_osmesa:
         mock_osmesa.OSMesaCreateContextExt.return_value = mock_context
         renderer = render.Renderer(MAX_WIDTH, MAX_HEIGHT)
         self.assertIs(renderer._context, mock_context)
         renderer.free()
Пример #2
0
 def test_freeing(self):
     mock_context = mock.MagicMock()
     with mock.patch(CONTEXT_PATH) as mock_glfw:
         mock_glfw.create_window.return_value = mock_context
         renderer = render.Renderer(MAX_WIDTH, MAX_HEIGHT)
         renderer.free()
     mock_glfw.destroy_window.assert_called_once_with(mock_context)
     self.assertIsNone(renderer._context)
Пример #3
0
 def test_make_current(self):
     mock_context = mock.MagicMock()
     with mock.patch(CONTEXT_PATH) as mock_glfw:
         mock_glfw.create_window.return_value = mock_context
         renderer = render.Renderer(MAX_WIDTH, MAX_HEIGHT)
         with renderer.make_current():
             pass
     mock_glfw.make_context_current.assert_called_once_with(mock_context)
Пример #4
0
 def test_freeing(self):
     mock_context = mock.MagicMock()
     with mock.patch(CONTEXT_PATH) as mock_osmesa:
         mock_osmesa.OSMesaCreateContextExt.return_value = mock_context
         renderer = render.Renderer(MAX_WIDTH, MAX_HEIGHT)
         renderer.free()
         mock_osmesa.OSMesaDestroyContext.assert_called_once_with(
             mock_context)
         self.assertIsNone(renderer._context)
Пример #5
0
 def test_make_current(self):
     mock_context = mock.MagicMock()
     mock_buffer = mock.MagicMock()
     with mock.patch(CONTEXT_PATH) as mock_osmesa:
         with mock.patch(GL_ARRAYS_PATH) as mock_glarrays:
             mock_osmesa.OSMesaCreateContextExt.return_value = mock_context
             mock_glarrays.GLfloatArray.zeros.return_value = mock_buffer
             renderer = render.Renderer(MAX_WIDTH, MAX_HEIGHT)
             with renderer.make_current():
                 pass
             mock_osmesa.OSMesaMakeCurrent.assert_called_once_with(
                 mock_context, mock_buffer, GL.GL_FLOAT, MAX_WIDTH,
                 MAX_HEIGHT)
             renderer.free()
Пример #6
0
    def test_context_activation_and_deactivation(self, mock_glfw):
        context = mock.MagicMock()

        mock_glfw.create_window = mock.MagicMock(return_value=context)
        mock_glfw.get_current_context = mock.MagicMock(return_value=None)

        renderer = render.Renderer(MAX_WIDTH, MAX_HEIGHT)
        renderer.make_context_current = mock.MagicMock()

        with renderer.make_current(2, 2):
            mock_glfw.make_context_current.assert_called_once_with(context)
            mock_glfw.make_context_current.reset_mock()

        mock_glfw.make_context_current.assert_called_once_with(None)
Пример #7
0
 def _make_rendering_contexts(self):
   """Creates the OpenGL and MuJoCo rendering contexts."""
   # Forcibly clear the previous GL context to avoid problems with GL
   # implementations which do not support multiple contexts on a given device.
   if self._contexts:
     self._contexts.mujoco.free()
     self._contexts.gl.free()
   # Get the offscreen framebuffer size, as specified in the model XML.
   max_width = self.model.vis.global_.offwidth
   max_height = self.model.vis.global_.offheight
   # Create the OpenGL context.
   render_context = render.Renderer(max_width=max_width, max_height=max_height)
   # Create the MuJoCo context.
   mujoco_context = wrapper.MjrContext(self.model, render_context)
   self._contexts = Contexts(gl=render_context, mujoco=mujoco_context)
Пример #8
0
 def _make_rendering_contexts(self):
   """Creates the OpenGL and MuJoCo rendering contexts."""
   # Forcibly clear the previous GL context to avoid problems with GL
   # implementations which do not support multiple contexts on a given device.
   if self._contexts:
     self._contexts.gl.free()
   # Create the OpenGL context.
   render_context = render.Renderer(_MAX_WIDTH, _MAX_HEIGHT)
   # Create the MuJoCo context.
   mujoco_context = wrapper.MjrContext()
   with render_context.make_current(_MAX_WIDTH, _MAX_HEIGHT):
     mjlib.mjr_makeContext(self.model.ptr, mujoco_context.ptr, _FONT_SCALE)
     mjlib.mjr_setBuffer(
         enums.mjtFramebuffer.mjFB_OFFSCREEN, mujoco_context.ptr)
   self._contexts = Contexts(gl=render_context, mujoco=mujoco_context)
Пример #9
0
  def _perform_deferred_reload(self, params):
    """Performs the deferred part of initialization related to Physics reload.

    Args:
      params: Deferred reload parameters, an instance of ReloadParams.
    """
    if self._render_surface:
      self._render_surface.free()
    if self._renderer:
      self._renderer.release()
    self._render_surface = render.Renderer(
        max_width=_MAX_FRONTBUFFER_SIZE, max_height=_MAX_FRONTBUFFER_SIZE)
    self._renderer = renderer.OffScreenRenderer(
        self._environment.physics.model, self._render_surface)
    self._renderer.components += self._viewer_layout
    self._viewer.initialize(
        self._environment.physics, self._renderer, touchpad=False)
    self._status.set_camera(self._viewer.camera)
    if params.zoom_to_scene:
      self._viewer.zoom_to_scene()
Пример #10
0
  def testFreeMjrContext(self):
    for _ in range(5):
      renderer = render.Renderer(640, 480)
      with mock.patch.object(core.mjlib, "mjr_freeContext",
                             wraps=mjlib.mjr_freeContext) as mock_destructor:
        mjr_context = core.MjrContext(self.model, renderer)
        expected_address = ctypes.addressof(mjr_context.ptr.contents)
        mjr_context.free()

      self.assertIsNone(mjr_context.ptr)
      mock_destructor.assert_called_once()
      pointer = mock_destructor.call_args[0][0]
      actual_address = ctypes.addressof(pointer.contents)
      self.assertEqual(expected_address, actual_address)

      # Explicit freeing should not break any automatic GC triggered later.
      del mjr_context
      renderer.free()
      del renderer
      gc.collect()
Пример #11
0
    def _reload_from_data(self, data):
        """Initializes a new or existing `Physics` instance from a `wrapper.MjData`.

    Assigns all attributes and sets up rendering contexts and named indexing.

    The default constructor as well as the other `reload_from` methods should
    delegate to this method.

    Args:
      data: Instance of `wrapper.MjData`.
    """
        self._data = data

        # Forcibly clear the previous context to avoid problems with GL
        # implementations which do not support multiple contexts on a given device.
        if hasattr(self, '_contexts'):
            self._contexts.gl.free_context()

        # Set up rendering context. Need to provide at least one rendering api in
        # the BUILD target.
        render_context = render.Renderer(_MAX_WIDTH, _MAX_HEIGHT)
        mujoco_context = wrapper.MjrContext()
        with render_context.make_current(_MAX_WIDTH, _MAX_HEIGHT):
            mjlib.mjr_makeContext(self.model.ptr, mujoco_context.ptr,
                                  _FONT_SCALE)
            mjlib.mjr_setBuffer(enums.mjtFramebuffer.mjFB_OFFSCREEN,
                                mujoco_context.ptr)
        self._contexts = Contexts(gl=render_context, mujoco=mujoco_context)

        # Call kinematics update to enable rendering.
        self.after_reset()

        # Set up named indexing.
        axis_indexers = index.make_axis_indexers(self.model)
        self._named = NamedIndexStructs(
            model=index.struct_indexer(self.model, 'mjmodel', axis_indexers),
            data=index.struct_indexer(self.data, 'mjdata', axis_indexers),
        )
  def setUp(self):
    self.context = mock.MagicMock()

    with mock.patch(CONTEXT_PATH):
      self.renderer = render.Renderer(MAX_WIDTH, MAX_HEIGHT)
Пример #13
0
 def test_repeatedly_create_and_destroy_context(self):
     renderer = render.Renderer(MAX_WIDTH, MAX_HEIGHT)
     wrapper.MjrContext(wrapper.MjModel.from_xml_string('<mujoco/>'),
                        renderer)
Пример #14
0
 def test_init(self):
     mock_context = mock.MagicMock()
     with mock.patch(CONTEXT_PATH) as mock_glfw:
         mock_glfw.create_window.return_value = mock_context
         renderer = render.Renderer(MAX_WIDTH, MAX_HEIGHT)
     self.assertIs(renderer._context, mock_context)