| 1 | // Copyright (C) 2020 James Turner |
| 2 | |
| 3 | // This program is free software; you can redistribute it and/or |
| 4 | // modify it under the terms of the GNU General Public License as |
| 5 | // published by the Free Software Foundation; either version 2 of the |
| 6 | // License, or (at your option) any later version. |
| 7 | // |
| 8 | // This program is distributed in the hope that it will be useful, but |
| 9 | // WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 11 | // General Public License for more details. |
| 12 | // |
| 13 | // You should have received a copy of the GNU General Public License |
| 14 | // along with this program; if not, write to the Free Software |
| 15 | // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 16 | |
| 17 | #include "config.h" |
| 18 | |
| 19 | #include <QOpenGLContext> |
| 20 | |
| 21 | #include "OSGQtAdaption.hxx" |
| 22 | |
| 23 | #include <memory> |
| 24 | |
| 25 | #include <QWindow> |
| 26 | #include <QThread> |
| 27 | |
| 28 | #include <simgear/compiler.h> |
| 29 | |
| 30 | #if defined(SG_MAC) |
| 31 | // defined in OSGCocoaAdaption.mm |
| 32 | #elif defined(SG_WINDOWS) |
| 33 | # include <QtPlatformHeaders/QWGLNativeContext> |
| 34 | # include <osgViewer/api/Win32/GraphicsWindowWin32> |
| 35 | #else |
| 36 | // assume X11 for now |
| 37 | # include <osgViewer/api/X11/GraphicsWindowX11> |
| 38 | # include <QtPlatformHeaders/QGLXNativeContext> |
| 39 | #endif |
| 40 | |
| 41 | namespace flightgear |
| 42 | { |
| 43 | |
| 44 | #if defined(SG_MAC) |
| 45 | |
| 46 | // defined in CocoaUtils.mm |
| 47 | |
| 48 | #elif defined(SG_WINDOWS) |
| 49 | |
| 50 | |
| 51 | QOpenGLContext* qtContextFromOSG(osg::GraphicsContext* context) |
| 52 | { |
| 53 | auto win = dynamic_cast<osgViewer::GraphicsWindowWin32*>(context); |
| 54 | if (!win) { |
| 55 | return nullptr; |
| 56 | } |
| 57 | |
| 58 | auto wglnc = QWGLNativeContext{win->getWGLContext(), win->getHWND()}; |
| 59 | QVariant nativeHandle = QVariant::fromValue(wglnc); |
| 60 | |
| 61 | if (nativeHandle.isNull()) |
| 62 | return nullptr; |
| 63 | |
| 64 | std::unique_ptr<QOpenGLContext> qtOpenGLContext(new QOpenGLContext); |
| 65 | qtOpenGLContext->setNativeHandle(nativeHandle); |
| 66 | if (!qtOpenGLContext->create()) { |
| 67 | return nullptr; |
| 68 | } |
| 69 | |
| 70 | return qtOpenGLContext.release(); |
| 71 | } |
| 72 | |
| 73 | QWindow* qtWindowFromOSG(osgViewer::GraphicsWindow* graphicsWindow) |
| 74 | { |
| 75 | auto win = dynamic_cast<osgViewer::GraphicsWindowWin32*>(graphicsWindow); |
| 76 | if (!win) { |
| 77 | return nullptr; |
| 78 | } |
| 79 | |
| 80 | return QWindow::fromWinId(reinterpret_cast<qlonglong>(win->getHWND())); |
| 81 | } |
| 82 | |
| 83 | #else |
| 84 | |
| 85 | // assume X11 for now |
| 86 | |
| 87 | QOpenGLContext* qtContextFromOSG(osg::GraphicsContext* context) |
| 88 | { |
| 89 | auto win = dynamic_cast<osgViewer::GraphicsWindowX11*>(context); |
| 90 | if (!win) { |
| 91 | return nullptr; |
| 92 | } |
| 93 | |
| 94 | auto glxnc = QGLXNativeContext{win->getContext(), win->getDisplay(), win->getWindow(), 0}; |
| 95 | QVariant nativeHandle = QVariant::fromValue(glxnc); |
| 96 | |
| 97 | if (nativeHandle.isNull()) |
| 98 | return nullptr; |
| 99 | |
| 100 | std::unique_ptr<QOpenGLContext> qtOpenGLContext(new QOpenGLContext); |
| 101 | qtOpenGLContext->setNativeHandle(nativeHandle); |
| 102 | if (!qtOpenGLContext->create()) { |
| 103 | return nullptr; |
| 104 | } |
| 105 | |
| 106 | return qtOpenGLContext.release(); |
| 107 | } |
| 108 | |
| 109 | QWindow* qtWindowFromOSG(osgViewer::GraphicsWindow* graphicsWindow) |
| 110 | { |
| 111 | auto win = dynamic_cast<osgViewer::GraphicsWindowX11*>(graphicsWindow); |
| 112 | if (!win) { |
| 113 | return nullptr; |
| 114 | } |
| 115 | |
| 116 | return QWindow::fromWinId(static_cast<qlonglong>(win->getWindow())); |
| 117 | } |
| 118 | |
| 119 | #endif |
| 120 | |
| 121 | class GraphicsFunctorWrapper : public osg::GraphicsOperation |
| 122 | { |
| 123 | public: |
| 124 | GraphicsFunctorWrapper(const std::string& name, GraphicsFunctor gf) : |
| 125 | osg::GraphicsOperation(name, false), |
| 126 | _functor(gf) |
| 127 | { |
| 128 | |
| 129 | } |
| 130 | |
| 131 | void operator()(osg::GraphicsContext* gc) override |
| 132 | { |
| 133 | _functor(gc); |
| 134 | } |
| 135 | |
| 136 | private: |
| 137 | GraphicsFunctor _functor; |
| 138 | }; |
| 139 | |
| 140 | osg::ref_ptr<osg::GraphicsOperation> makeGraphicsOp(const std::string& name, GraphicsFunctor func) |
| 141 | { |
| 142 | osg::ref_ptr<osg::GraphicsOperation> r; |
| 143 | r = new GraphicsFunctorWrapper(name, func); |
| 144 | return r; |
| 145 | } |
| 146 | |
| 147 | } // of namespace |