view libgui/graphics/GLCanvas.cc @ 27335:50216d7a2f6b

eliminate static wrapper functions in gh_manager class This is a large but straightforward change to eliminate the remains of the singleton pattern that was used for the gh_manager class. All static functions in the gh_manager class that previously used the "instance" function to access the gh_manager object stored in the global interpreter object and then called the corresponding "do_" functions have been eliminated. The "do_" prefix has been removed from the normal member functions and all uses of the static functions (calls of the form gh_manager::X) have been replaced with a call to the member function. The gh_manager object is accessed through the interpreter object and there is a new __get_gh_manager__ convenience function defined in the intepreter-private.cc file. Additionally, instead of using the gh_manager::auto_lock class, we access the mutex variable associated with the gh_manager object and pass it to the octave::autolock constructor to manage scoped locks for the gh_manager class. The following interpreter functions are now "methods" and require the interpreter as the first argument: F__calc_dimensions__, F__fltk_check__, F__get__, F__get_frame__, F__go_axes__, F__go_delete__, F__go_execute_callback__, F__go_figure__, F__go_figure_handles__, F__go_handles__, F__go_hggroup__, F__go_image__, F__go_light__, F__go_line__, F__go_patch__, F__go_post_callback__, F__go_surface__, F__go_text__, F__go_uibuttongroup__, F__go_uicontextmenu__, F__go_uicontrol__, F__go_uimenu__, F__go_uipanel__, F__go_uipushtool__, F__go_uitable__, F__go_uitoggletool__, F__go_uitoolbar__, F__image_pixel_size__, F__show_figure__, F__shutdown_qt__, F__update_normals__, F__zoom__, Faddlistener, Faddproperty, Fdellistener, Fdrawnow, Fget, Fishghandle, Fkbhit, Fpause, Freset, Fset, and Fwaitfor. Files affected: BaseControl.cc, ButtonControl.cc, ButtonGroup.cc, Canvas.cc, Container.cc, ContextMenu.cc, Figure.cc, GLCanvas.cc, Object.cc, Panel.cc, QtHandlesUtils.h, SliderControl.cc, Table.cc, __init_qt__.cc, qt-graphics-toolkit.cc, main-window.cc, gl-render.cc, gl-render.h, gl2ps-print.cc, graphics-toolkit.cc, graphics.cc, graphics.in.h, input.cc, interpreter.cc, sysdep.cc, utils.cc, __init_fltk__.cc.
author John W. Eaton <jwe@octave.org>
date Fri, 09 Aug 2019 10:36:08 -0400
parents 07b330708e3c
children efe72866b483
line wrap: on
line source

/*

Copyright (C) 2011-2019 Michael Goffioul

This file is part of Octave.

Octave 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.

Octave 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 Octave; see the file COPYING.  If not, see
<https://www.gnu.org/licenses/>.

*/

#if defined (HAVE_CONFIG_H)
#  include "config.h"
#endif

#include "event-manager.h"
#include "gl-render.h"
#include "gl2ps-print.h"
#include "graphics.h"
#include "interpreter-private.h"

#include "GLCanvas.h"
#include "gl-select.h"

namespace QtHandles
{
#if defined (HAVE_QOPENGLWIDGET)
#  define OCTAVE_QT_OPENGL_WIDGET_FORMAT_ARGS
#else
#  if defined (Q_OS_WIN32)
#    define OCTAVE_QT_OPENGL_WIDGET_FORMAT_ARGS         \
  QGLFormat (QGL::SampleBuffers | QGL::AlphaChannel     \
             | QGL::IndirectRendering),
#  else
#    define OCTAVE_QT_OPENGL_WIDGET_FORMAT_ARGS \
  QGLFormat (QGL::SampleBuffers | QGL::AlphaChannel),
#  endif
#endif

  GLCanvas::GLCanvas (QWidget *xparent, const graphics_handle& gh)
    : OCTAVE_QT_OPENGL_WIDGET (OCTAVE_QT_OPENGL_WIDGET_FORMAT_ARGS xparent),
      Canvas (gh), m_glfcns (), m_renderer (m_glfcns)
  {
    setFocusPolicy (Qt::ClickFocus);
    setFocus ();
  }

  GLCanvas::~GLCanvas (void)
  { }

  void
  GLCanvas::initializeGL (void)
  {
    m_glfcns.init ();
  }

  void
  GLCanvas::draw (const graphics_handle& gh)
  {
    gh_manager& gh_mgr = octave::__get_gh_manager__ ("GLCanvas::draw");

    octave::autolock guard  (gh_mgr.graphics_lock ());

    graphics_object go = gh_mgr.get_object (gh);

    if (go)
      {
        graphics_object fig = go.get_ancestor ("figure");
        double dpr = fig.get ("__device_pixel_ratio__").double_value ();
        m_renderer.set_viewport (dpr * width (), dpr * height ());
        m_renderer.set_device_pixel_ratio (dpr);
        m_renderer.draw (go);
      }
  }

  uint8NDArray
  GLCanvas::do_getPixels (const graphics_handle& gh)
  {
    uint8NDArray retval;

    gh_manager& gh_mgr = octave::__get_gh_manager__ ("GLCanvas::do_getPixels");

    graphics_object go = gh_mgr.get_object (gh);

    if (go && go.isa ("figure"))
      {
        Matrix pos = go.get ("position").matrix_value ();
        double dpr = go.get ("__device_pixel_ratio__").double_value ();
        pos(2) *= dpr;
        pos(3) *= dpr;

        // Make sure we have a valid current context
        if (! begin_rendering ())
          return retval;

        // When the figure is not visible or its size is frozen for printing,
        // we use a framebuffer object to make sure we are rendering on a
        // suitably large frame.
        if (go.get ("visible").string_value () == "off"
            || go.get ("__printing__").string_value () == "on")
          {
            OCTAVE_QT_OPENGL_FBO
            fbo (pos(2), pos(3),
                 OCTAVE_QT_OPENGL_FBO::Attachment::Depth);

            fbo.bind ();

            m_renderer.set_viewport (pos(2), pos(3));
            m_renderer.set_device_pixel_ratio (dpr);
            m_renderer.draw (go);
            retval = m_renderer.get_pixels (pos(2), pos(3));

            fbo.release ();
          }
        else
          {
            m_renderer.set_viewport (pos(2), pos(3));
            m_renderer.set_device_pixel_ratio (dpr);
            m_renderer.draw (go);
            retval = m_renderer.get_pixels (pos(2), pos(3));
          }

        end_rendering ();
      }

    return retval;
  }

  void
  GLCanvas::do_print (const QString& file_cmd, const QString& term,
                      const graphics_handle& handle)
  {
    gh_manager& gh_mgr = octave::__get_gh_manager__ ("GLCanvas::do_print");

    octave::autolock guard  (gh_mgr.graphics_lock ());

    graphics_object go = gh_mgr.get_object (handle);

    if (go.valid_object ())
      {
        graphics_object fig (go.get_ancestor ("figure"));

        // Make sure we have a valid current context
        if (! begin_rendering ())
          error ("print: no valid OpenGL offscreen context");

        try
          {
            if (fig.get ("visible").string_value () == "on")
              octave::gl2ps_print (m_glfcns, fig, file_cmd.toStdString (),
                                   term.toStdString ());
            else
              {
                // When the figure is not visible, we use a framebuffer object
                // to make sure we are rendering on a suitably large frame.
                Matrix pos = fig.get ("position").matrix_value ();
                double dpr = fig.get ("__device_pixel_ratio__").double_value ();
                pos(2) *= dpr;
                pos(3) *= dpr;

                OCTAVE_QT_OPENGL_FBO
                  fbo (pos(2), pos(3),
                       OCTAVE_QT_OPENGL_FBO::Attachment::Depth);

                fbo.bind ();

                octave::gl2ps_print (m_glfcns, fig, file_cmd.toStdString (),
                                     term.toStdString ());

                fbo.release ();
              }
          }
        catch (octave::execution_exception& e)
          {
            emit interpreter_event
              ([] (void)
               {
                 std::rethrow_exception (std::current_exception ());
               });
          }

        end_rendering ();
      }
  }

  graphics_object
  GLCanvas::selectFromAxes (const graphics_object& ax, const QPoint& pt)
  {
    makeCurrent ();

    if (ax)
      {
        octave::opengl_selector s (m_glfcns);

        s.set_viewport (width (), height ());
        return s.select (ax, pt.x (), height () - pt.y (),
                         octave::select_ignore_hittest);
      }

    return graphics_object ();
  }

  void
  GLCanvas::drawZoomBox (const QPoint& p1, const QPoint& p2)
  {
    Matrix overlaycolor (3, 1);
    overlaycolor(0) = 0.45;
    overlaycolor(1) = 0.62;
    overlaycolor(2) = 0.81;
    double overlayalpha = 0.1;
    Matrix bordercolor = overlaycolor;
    double borderalpha = 0.9;
    double borderwidth = 1.5;

    m_renderer.draw_zoom_box (width (), height (),
                              p1.x (), p1.y (), p2.x (), p2.y (),
                              overlaycolor, overlayalpha,
                              bordercolor, borderalpha, borderwidth);
  }

  void
  GLCanvas::paintGL (void)
  {
    canvasPaintEvent ();
  }

  void
  GLCanvas::mouseDoubleClickEvent (QMouseEvent *xevent)
  {
    canvasMouseDoubleClickEvent (xevent);
  }

  void
  GLCanvas::mouseMoveEvent (QMouseEvent *xevent)
  {
    canvasMouseMoveEvent (xevent);
  }

  void
  GLCanvas::mousePressEvent (QMouseEvent *xevent)
  {
    canvasMousePressEvent (xevent);
  }

  void
  GLCanvas::mouseReleaseEvent (QMouseEvent *xevent)
  {
    canvasMouseReleaseEvent (xevent);
  }

  void
  GLCanvas::wheelEvent (QWheelEvent *xevent)
  {
    canvasWheelEvent (xevent);
  }

  void
  GLCanvas::keyPressEvent (QKeyEvent *xevent)
  {
    if (! canvasKeyPressEvent (xevent))
      OCTAVE_QT_OPENGL_WIDGET::keyPressEvent (xevent);
  }

  void
  GLCanvas::keyReleaseEvent (QKeyEvent *xevent)
  {
    if (! canvasKeyReleaseEvent (xevent))
      OCTAVE_QT_OPENGL_WIDGET::keyReleaseEvent (xevent);
  }

  bool
  GLCanvas::begin_rendering (void)
  {
    bool retval = true;

    if (! isValid ())
      {
#  if defined (HAVE_QT_OFFSCREEN)
        static bool os_ctx_ok = true;
        if (os_ctx_ok && ! m_os_context.isValid ())
          {
            // Try to initialize offscreen context
            m_os_surface.create ();
            if (! m_os_context.create ())
              {
                os_ctx_ok = false;
                return false;
              }
          }

        retval = m_os_context.makeCurrent (&m_os_surface);
#  else
        retval = false;
#  endif
      }
    else
      makeCurrent ();

    return retval;
  }

  void
  GLCanvas::end_rendering (void)
  {
    doneCurrent ();
  }
}