Added osg::GraphicsContext::WindowingSystemInterfaces singleton for managing multiple WIndowinSystemInterface

implementations being registered at the same time.

One usage case for this functionality to support usage of Wayland and X11 in the same version of the osgViewer.

As part of the new functionality there is now a osg::GraphicsContext::Traits::windowingSystemPreferrence string
that default to empty, but if defined will ensure that a specific WindowingSystemInterface is utilized when
you do a generic call like osg::createGraphicsContext().

Also implemented is standard proxy object for registering the new contexts and removing them automatically, and
declaration of standard graphicswindow_name() C entry point to help with static build linking.
This commit is contained in:
Robert Osfield 2016-05-16 13:45:31 +01:00
parent dd10619192
commit fe6238d126
12 changed files with 204 additions and 67 deletions

View File

@ -50,6 +50,7 @@ int main( int argc, char **argv )
// left window + left slave camera // left window + left slave camera
{ {
osg::ref_ptr<osg::GraphicsContext::Traits> traits = new osg::GraphicsContext::Traits; osg::ref_ptr<osg::GraphicsContext::Traits> traits = new osg::GraphicsContext::Traits;
traits->x = xoffset + 0; traits->x = xoffset + 0;
traits->y = yoffset + 0; traits->y = yoffset + 0;
traits->width = 600; traits->width = 600;

View File

@ -76,6 +76,12 @@ class OSG_EXPORT GraphicsContext : public Object
int width; int width;
int height; int height;
// provide a hint as to which WindowingSystemInterface implementation to use, i.e. "X11", "Win32", "Cocoa", "Carbon" etc.
// if the windowingSystemPreference string is empty (default) then return the first available WindowingSystemInterface that
// has been registered with the osg::GraphiccsContext::WindowingSystemInterfaces singleton
// if the windowingSystemPreference string is not empty then return the first WindowingSystemInterface that matches
std::string windowingSystemPreference;
// window decoration and behaviour // window decoration and behaviour
std::string windowName; std::string windowName;
bool windowDecoration; bool windowDecoration;
@ -168,6 +174,9 @@ class OSG_EXPORT GraphicsContext : public Object
/** Callback to be implemented to provide access to Windowing API's ability to create Windows/pbuffers.*/ /** Callback to be implemented to provide access to Windowing API's ability to create Windows/pbuffers.*/
struct WindowingSystemInterface : public osg::Referenced struct WindowingSystemInterface : public osg::Referenced
{ {
void setName(const std::string& name) { _name = name; }
const std::string& getName() const { return _name; }
virtual unsigned int getNumScreens(const ScreenIdentifier& screenIdentifier = ScreenIdentifier()) = 0; virtual unsigned int getNumScreens(const ScreenIdentifier& screenIdentifier = ScreenIdentifier()) = 0;
virtual void getScreenSettings(const ScreenIdentifier& screenIdentifier, ScreenSettings & resolution) = 0; virtual void getScreenSettings(const ScreenIdentifier& screenIdentifier, ScreenSettings & resolution) = 0;
@ -182,9 +191,6 @@ class OSG_EXPORT GraphicsContext : public Object
virtual GraphicsContext* createGraphicsContext(Traits* traits) = 0; virtual GraphicsContext* createGraphicsContext(Traits* traits) = 0;
virtual ~WindowingSystemInterface() {}
/** Gets screen resolution without using the ScreenResolution structure. /** Gets screen resolution without using the ScreenResolution structure.
* \deprecated Provided only for backward compatibility. */ * \deprecated Provided only for backward compatibility. */
inline void getScreenResolution(const ScreenIdentifier& screenIdentifier, unsigned int& width, unsigned int& height) inline void getScreenResolution(const ScreenIdentifier& screenIdentifier, unsigned int& width, unsigned int& height)
@ -210,15 +216,38 @@ class OSG_EXPORT GraphicsContext : public Object
settings.refreshRate = refreshRate; settings.refreshRate = refreshRate;
return setScreenSettings(screenIdentifier, settings); return setScreenSettings(screenIdentifier, settings);
} }
protected:
WindowingSystemInterface() {}
virtual ~WindowingSystemInterface() {}
std::string _name;
}; };
class OSG_EXPORT WindowingSystemInterfaces : public osg::Referenced
{
public:
WindowingSystemInterfaces();
/** Set the query the windowing system for screens and create graphics context - this functor should be supplied by the windows toolkit. */ typedef std::vector< osg::ref_ptr<GraphicsContext::WindowingSystemInterface> > Interfaces;
static void setWindowingSystemInterface(WindowingSystemInterface* wsInterface);
/** Get the WindowingSystemInterface*/ Interfaces& getInterfaces() { return _interfaces; }
static WindowingSystemInterface* getWindowingSystemInterface();
void addWindowingSystemInterface(WindowingSystemInterface* wsInterface);
void removeWindowingSystemInterface(WindowingSystemInterface* wsInterface);
/** get named WindowingSystemInterface if one is available, otherwise return 0; */
WindowingSystemInterface* getWindowingSystemInterface(const std::string& name = "");
private:
virtual ~WindowingSystemInterfaces();
Interfaces _interfaces;
};
static osg::ref_ptr<WindowingSystemInterfaces>& getWindowingSystemInterfaces();
/** Get the default WindowingSystemInterface for this OS*/
static WindowingSystemInterface* getWindowingSystemInterface(const std::string& name = "");
/** Create a graphics context for a specified set of traits.*/ /** Create a graphics context for a specified set of traits.*/
static GraphicsContext* createGraphicsContext(Traits* traits); static GraphicsContext* createGraphicsContext(Traits* traits);
@ -544,6 +573,30 @@ public:
GLsync _previousSync; GLsync _previousSync;
}; };
template<class T>
struct WindowingSystemInterfaceProxy
{
WindowingSystemInterfaceProxy(const std::string& name)
{
_wsi = new T;
_wsi->setName(name);
osg::GraphicsContext::getWindowingSystemInterfaces()->addWindowingSystemInterface(_wsi.get());
}
~WindowingSystemInterfaceProxy()
{
osg::GraphicsContext::getWindowingSystemInterfaces()->removeWindowingSystemInterface(_wsi.get());
}
osg::ref_ptr<T> _wsi;
};
#define REGISTER_WINDOWINGSYSTEMINTERFACE(ext, classname) \
extern "C" void graphicswindow_##ext(void) {} \
static osg::WindowingSystemInterfaceProxy<classname> s_proxy_##classname(#ext);
} }
#endif #endif

View File

@ -39,8 +39,10 @@ namespace osgQt
// forward declarations // forward declarations
class GraphicsWindowQt; class GraphicsWindowQt;
#if 0
/// The function sets the WindowingSystem to Qt. /// The function sets the WindowingSystem to Qt.
void OSGQT_EXPORT initQtWindowingSystem(); void OSGQT_EXPORT initQtWindowingSystem();
#endif
/** The function sets the viewer that will be used after entering /** The function sets the viewer that will be used after entering
* the Qt main loop (QCoreApplication::exec()). * the Qt main loop (QCoreApplication::exec()).

View File

@ -34,42 +34,95 @@
#include <sstream> #include <sstream>
#include <algorithm> #include <algorithm>
#include <iterator> #include <iterator>
#include <stdio.h>
using namespace osg; using namespace osg;
///////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////
//
// WindowSystemInterfaces
//
GraphicsContext::WindowingSystemInterfaces::WindowingSystemInterfaces()
{
}
GraphicsContext::WindowingSystemInterfaces::~WindowingSystemInterfaces()
{
}
void GraphicsContext::WindowingSystemInterfaces::addWindowingSystemInterface(GraphicsContext::WindowingSystemInterface* wsi)
{
if (std::find(_interfaces.begin(), _interfaces.end(), wsi)==_interfaces.end())
{
OSG_NOTICE<<"GraphicsContext::WindowingSystemInterfaces::addWindowingSystemInterface("<<wsi<<") Name="<<wsi->getName()<<std::endl;
_interfaces.push_back(wsi);
}
}
void GraphicsContext::WindowingSystemInterfaces::removeWindowingSystemInterface(GraphicsContext::WindowingSystemInterface* wsi)
{
printf("GraphicsContext::WindowingSystemInterfaces::removeWindowingSystemInterface()\n");
Interfaces::iterator itr = std::find(_interfaces.begin(), _interfaces.end(), wsi);
if (itr!=_interfaces.end())
{
printf(" succeded GraphicsContext::WindowingSystemInterfaces::removeWindowingSystemInterface()\n");
_interfaces.erase(itr);
}
}
GraphicsContext::WindowingSystemInterface* GraphicsContext::WindowingSystemInterfaces::getWindowingSystemInterface(const std::string& name)
{
if (_interfaces.empty())
{
OSG_WARN<<"Warning: GraphicsContext::WindowingSystemInterfaces::getWindowingSystemInterface() failed, no interfaces available."<<std::endl;
return 0;
}
if (!name.empty())
{
for(Interfaces::iterator itr = _interfaces.begin();
itr != _interfaces.end();
++itr)
{
if ((*itr)->getName()==name)
{
return itr->get();
}
OSG_NOTICE<<" tried interface "<<typeid(*itr).name()<<", name= "<<(*itr)->getName()<<std::endl;
}
OSG_WARN<<"Warning: GraphicsContext::WindowingSystemInterfaces::getWindowingSystemInterface() failed, no interfaces matches name : "<<name<<std::endl;
return 0;
}
else
{
// no preference provided so just take the first available interface
return _interfaces.front().get();
}
}
// Use a static reference pointer to hold the window system interface. // Use a static reference pointer to hold the window system interface.
// Wrap this within a function, in order to control the order in which // Wrap this within a function, in order to control the order in which
// the static pointer's constructor is executed. // the static pointer's constructor is executed.
osg::ref_ptr<GraphicsContext::WindowingSystemInterfaces>& GraphicsContext::getWindowingSystemInterfaces()
static ref_ptr<GraphicsContext::WindowingSystemInterface> &windowingSystemInterfaceRef()
{ {
static ref_ptr<GraphicsContext::WindowingSystemInterface> s_WindowingSystemInterface; static ref_ptr<GraphicsContext::WindowingSystemInterfaces> s_WindowingSystemInterface = new GraphicsContext::WindowingSystemInterfaces;
return s_WindowingSystemInterface; return s_WindowingSystemInterface;
} }
OSG_INIT_SINGLETON_PROXY(ProxyInitWindowingSystemInterfaces, GraphicsContext::getWindowingSystemInterfaces())
// GraphicsContext static method implementations // GraphicsContext static method implementations
GraphicsContext::WindowingSystemInterface* GraphicsContext::getWindowingSystemInterface(const std::string& name)
void GraphicsContext::setWindowingSystemInterface(WindowingSystemInterface* callback)
{ {
ref_ptr<GraphicsContext::WindowingSystemInterface> &wsref = windowingSystemInterfaceRef(); return GraphicsContext::getWindowingSystemInterfaces()->getWindowingSystemInterface(name);
wsref = callback;
OSG_INFO<<"GraphicsContext::setWindowingSystemInterface() "<<wsref.get()<<"\t"<<&wsref<<std::endl;
}
GraphicsContext::WindowingSystemInterface* GraphicsContext::getWindowingSystemInterface()
{
ref_ptr<GraphicsContext::WindowingSystemInterface> &wsref = windowingSystemInterfaceRef();
OSG_INFO<<"GraphicsContext::getWindowingSystemInterface() "<<wsref.get()<<"\t"<<&wsref<<std::endl;
return wsref.get();
} }
GraphicsContext* GraphicsContext::createGraphicsContext(Traits* traits) GraphicsContext* GraphicsContext::createGraphicsContext(Traits* traits)
{ {
ref_ptr<GraphicsContext::WindowingSystemInterface> &wsref = windowingSystemInterfaceRef(); ref_ptr<GraphicsContext::WindowingSystemInterface> wsref = getWindowingSystemInterface(traits ? traits->windowingSystemPreference : "") ;
if ( wsref.valid()) if ( wsref.valid())
{ {
// catch any undefined values. // catch any undefined values.

View File

@ -869,7 +869,6 @@ void GraphicsWindowQt::requestWarpPointer( float x, float y )
QCursor::setPos( _widget->mapToGlobal(QPoint((int)x,(int)y)) ); QCursor::setPos( _widget->mapToGlobal(QPoint((int)x,(int)y)) );
} }
class QtWindowingSystem : public osg::GraphicsContext::WindowingSystemInterface class QtWindowingSystem : public osg::GraphicsContext::WindowingSystemInterface
{ {
public: public:
@ -945,6 +944,9 @@ private:
QtWindowingSystem& operator=( const QtWindowingSystem& ); QtWindowingSystem& operator=( const QtWindowingSystem& );
}; };
#if 1
REGISTER_WINDOWINGSYSTEMINTERFACE(Qt, QtWindowingSystem)
#else
// declare C entry point for static compilation. // declare C entry point for static compilation.
extern "C" void OSGQT_EXPORT graphicswindow_Qt(void) extern "C" void OSGQT_EXPORT graphicswindow_Qt(void)
@ -957,7 +959,7 @@ void osgQt::initQtWindowingSystem()
{ {
graphicswindow_Qt(); graphicswindow_Qt();
} }
#endif
void osgQt::setViewer( osgViewer::ViewerBase *viewer ) void osgQt::setViewer( osgViewer::ViewerBase *viewer )

View File

@ -136,6 +136,7 @@ struct DarwinWindowingSystemInterface : public osg::GraphicsContext::WindowingSy
}; };
#if 0
template <class WSI> template <class WSI>
struct RegisterWindowingSystemInterfaceProxy struct RegisterWindowingSystemInterfaceProxy
{ {
@ -155,7 +156,7 @@ struct RegisterWindowingSystemInterfaceProxy
osg::GraphicsContext::setWindowingSystemInterface(0); osg::GraphicsContext::setWindowingSystemInterface(0);
} }
}; };
#endif
} }

View File

@ -1100,15 +1100,19 @@ public:
} }
#if 1
REGISTER_WINDOWINGSYSTEMINTERFACE(Carbon, osgViewer::CarbonWindowingSystemInterface)
#else
#ifdef USE_DARWIN_CARBON_IMPLEMENTATION #ifdef USE_DARWIN_CARBON_IMPLEMENTATION
RegisterWindowingSystemInterfaceProxy<CarbonWindowingSystemInterface> createWindowingSystemInterfaceProxy; RegisterWindowingSystemInterfaceProxy<CarbonWindowingSystemInterface> createWindowingSystemInterfaceProxy;
#endif #endif
// declare C entry point for static compilation. // declare C entry point for static compilation.
extern "C" void graphicswindow_Carbon(void) extern "C" void graphicswindow_Carbon(void)
{ {
osg::GraphicsContext::setWindowingSystemInterface(new osgViewer::CarbonWindowingSystemInterface()); osg::GraphicsContext::setWindowingSystemInterface(new osgViewer::CarbonWindowingSystemInterface());
} }
#endif
#endif #endif

View File

@ -1829,6 +1829,9 @@ private:
} }
#if 1
REGISTER_WINDOWINGSYSTEMINTERFACE(Cocoa, osgViewer::CocoaWindowingSystemInterface)
#else
#ifdef USE_DARWIN_COCOA_IMPLEMENTATION #ifdef USE_DARWIN_COCOA_IMPLEMENTATION
RegisterWindowingSystemInterfaceProxy<osgViewer::CocoaWindowingSystemInterface> createWindowingSystemInterfaceProxy; RegisterWindowingSystemInterfaceProxy<osgViewer::CocoaWindowingSystemInterface> createWindowingSystemInterfaceProxy;
#endif #endif
@ -1838,3 +1841,4 @@ extern "C" void graphicswindow_Cocoa(void)
{ {
osg::GraphicsContext::setWindowingSystemInterface(new osgViewer::CocoaWindowingSystemInterface()); osg::GraphicsContext::setWindowingSystemInterface(new osgViewer::CocoaWindowingSystemInterface());
} }
#endif

View File

@ -1226,7 +1226,9 @@ public:
}//end namspace }//end namspace
#if 1
REGISTER_WINDOWINGSYSTEMINTERFACE(IOS, osgViewer::ConcreteIOSWindowingSystemInterface)
#else
RegisterWindowingSystemInterfaceProxy<osgViewer::ConcreteIOSWindowingSystemInterface> createWindowingSystemInterfaceProxy; RegisterWindowingSystemInterfaceProxy<osgViewer::ConcreteIOSWindowingSystemInterface> createWindowingSystemInterfaceProxy;
@ -1235,3 +1237,4 @@ extern "C" void graphicswindow_IOS(void)
{ {
osg::GraphicsContext::setWindowingSystemInterface(new osgViewer::ConcreteIOSWindowingSystemInterface()); osg::GraphicsContext::setWindowingSystemInterface(new osgViewer::ConcreteIOSWindowingSystemInterface());
} }
#endif

View File

@ -317,14 +317,16 @@ class Win32WindowingSystem : public osg::GraphicsContext::WindowingSystemInterfa
static std::string osgGraphicsWindowWithCursorClass; //!< Name of Win32 window class (with cursor) used by OSG graphics window instances static std::string osgGraphicsWindowWithCursorClass; //!< Name of Win32 window class (with cursor) used by OSG graphics window instances
static std::string osgGraphicsWindowWithoutCursorClass; //!< Name of Win32 window class (without cursor) used by OSG graphics window instances static std::string osgGraphicsWindowWithoutCursorClass; //!< Name of Win32 window class (without cursor) used by OSG graphics window instances
Win32WindowingSystem(); Win32WindowingSystem()
~Win32WindowingSystem(); {
getInterface() = this;
}
// Access the Win32 windowing system through this singleton class. // Access the Win32 windowing system through this singleton class.
static Win32WindowingSystem* getInterface() static osg::ref_ptr<Win32WindowingSystem>& getInterface()
{ {
static Win32WindowingSystem* win32Interface = new Win32WindowingSystem; static osg::ref_ptr<Win32WindowingSystem> s_win32Interface;
return win32Interface; return s_win32Interface;
} }
// Return the number of screens present in the system // Return the number of screens present in the system
@ -368,6 +370,8 @@ class Win32WindowingSystem : public osg::GraphicsContext::WindowingSystemInterfa
protected: protected:
virtual ~Win32WindowingSystem() {}
// Display devices present in the system // Display devices present in the system
typedef std::vector<DISPLAY_DEVICE> DisplayDevices; typedef std::vector<DISPLAY_DEVICE> DisplayDevices;
@ -2894,7 +2898,7 @@ LRESULT GraphicsWindowWin32::handleNativeWindowingEvent( HWND hwnd, UINT uMsg, W
////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////
// Class responsible for registering the Win32 Windowing System interface // Class responsible for registering the Win32 Windowing System interface
////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////
#if 0
struct RegisterWindowingSystemInterfaceProxy struct RegisterWindowingSystemInterfaceProxy
{ {
RegisterWindowingSystemInterfaceProxy() RegisterWindowingSystemInterfaceProxy()
@ -2915,16 +2919,19 @@ struct RegisterWindowingSystemInterfaceProxy
}; };
static RegisterWindowingSystemInterfaceProxy createWindowingSystemInterfaceProxy; static RegisterWindowingSystemInterfaceProxy createWindowingSystemInterfaceProxy;
#endif
} // namespace OsgViewer } // namespace OsgViewer
#if 1
REGISTER_WINDOWINGSYSTEMINTERFACE(Win32, Win32WindowingSystem)
#else
// declare C entry point for static compilation. // declare C entry point for static compilation.
extern "C" void OSGVIEWER_EXPORT graphicswindow_Win32(void) extern "C" void OSGVIEWER_EXPORT graphicswindow_Win32(void)
{ {
osg::GraphicsContext::setWindowingSystemInterface(osgViewer::Win32WindowingSystem::getInterface()); osg::GraphicsContext::setWindowingSystemInterface(osgViewer::Win32WindowingSystem::getInterface());
} }
#endif
void GraphicsWindowWin32::raiseWindow() void GraphicsWindowWin32::raiseWindow()
{ {

View File

@ -2144,6 +2144,12 @@ public:
}; };
#if 1
REGISTER_WINDOWINGSYSTEMINTERFACE(X11, X11WindowingSystemInterface)
#else
struct RegisterWindowingSystemInterfaceProxy struct RegisterWindowingSystemInterfaceProxy
{ {
RegisterWindowingSystemInterfaceProxy() RegisterWindowingSystemInterfaceProxy()
@ -2166,7 +2172,6 @@ struct RegisterWindowingSystemInterfaceProxy
} }
}; };
RegisterWindowingSystemInterfaceProxy createWindowingSystemInterfaceProxy; RegisterWindowingSystemInterfaceProxy createWindowingSystemInterfaceProxy;
// declare C entry point for static compilation. // declare C entry point for static compilation.
@ -2174,6 +2179,7 @@ extern "C" void graphicswindow_X11(void)
{ {
osg::GraphicsContext::setWindowingSystemInterface(new X11WindowingSystemInterface); osg::GraphicsContext::setWindowingSystemInterface(new X11WindowingSystemInterface);
} }
#endif
void GraphicsWindowX11::raiseWindow() void GraphicsWindowX11::raiseWindow()
{ {

View File

@ -85,6 +85,7 @@ private:
}; };
#if 0
template <class WSI> template <class WSI>
struct RegisterWindowingSystemInterfaceProxy struct RegisterWindowingSystemInterfaceProxy
{ {
@ -104,7 +105,7 @@ struct RegisterWindowingSystemInterfaceProxy
osg::GraphicsContext::setWindowingSystemInterface(0); osg::GraphicsContext::setWindowingSystemInterface(0);
} }
}; };
#endif
} }