| 1 | // SPDX-License-Identifier: LGPL-2.1-only |
| 2 | // Copyright (C) 2021 James Hogan <[email protected]> |
| 3 | |
| 4 | #include "GraphicsBinding.h" |
| 5 | #include "GraphicsBindingWin32.h" |
| 6 | #include "GraphicsBindingX11.h" |
| 7 | |
| 8 | #include <vector> |
| 9 | |
| 10 | using namespace osgXR::OpenXR; |
| 11 | |
| 12 | namespace osgXR { |
| 13 | |
| 14 | namespace OpenXR { |
| 15 | |
| 16 | class GraphicsBindingProxy : public osg::Referenced |
| 17 | { |
| 18 | public: |
| 19 | virtual ~GraphicsBindingProxy() {} |
| 20 | |
| 21 | virtual GraphicsBinding *create(osgViewer::GraphicsWindow *window) = 0; |
| 22 | }; |
| 23 | |
| 24 | template <typename GRAPHICS_BINDING> |
| 25 | class GraphicsBindingProxyImpl : public GraphicsBindingProxy |
| 26 | { |
| 27 | protected: |
| 28 | typedef GRAPHICS_BINDING Binding; |
| 29 | typedef typename Binding::GraphicsWindow Window; |
| 30 | |
| 31 | virtual ~GraphicsBindingProxyImpl() {} |
| 32 | |
| 33 | public: |
| 34 | GraphicsBinding *create(osgViewer::GraphicsWindow *window) override |
| 35 | { |
| 36 | Window *win = dynamic_cast<Window *>(window); |
| 37 | if (!win) |
| 38 | return nullptr; |
| 39 | |
| 40 | return new Binding(win); |
| 41 | } |
| 42 | }; |
| 43 | |
| 44 | } // osgXR::OpenXR |
| 45 | |
| 46 | } // osgXR |
| 47 | |
| 48 | typedef std::vector<osg::ref_ptr<GraphicsBindingProxy> > ProxyList; |
| 49 | |
| 50 | static ProxyList proxies = { |
| 51 | #ifdef OSGXR_USE_WIN32 |
| 52 | new GraphicsBindingProxyImpl<GraphicsBindingWin32>(), |
| 53 | #endif |
| 54 | #ifdef OSGXR_USE_X11 |
| 55 | new GraphicsBindingProxyImpl<GraphicsBindingX11>(), |
| 56 | #endif |
| 57 | }; |
| 58 | |
| 59 | osg::ref_ptr<GraphicsBinding> osgXR::OpenXR::createGraphicsBinding(osgViewer::GraphicsWindow *window) |
| 60 | { |
| 61 | GraphicsBinding *ret = nullptr; |
| 62 | for (GraphicsBindingProxy *proxy: proxies) |
| 63 | { |
| 64 | ret = proxy->create(window); |
| 65 | if (ret) |
| 66 | break; |
| 67 | } |
| 68 | return ret; |
| 69 | } |