Changeset 11207

Show
Ignore:
Timestamp:
03/13/10 12:28:00 (5 years ago)
Author:
robert
Message:

Refactored the initialization of OSX windowing settings so that it's done on demand rather than on startup.

Location:
OpenSceneGraph/trunk/src/osgViewer
Files:
4 modified

Legend:

Unmodified
Added
Removed
  • OpenSceneGraph/trunk/src/osgViewer/DarwinUtils.h

    r10208 r11207  
    9191     
    9292    protected: 
    93      
     93 
     94        virtual void _init(); 
     95 
    9496        /** implementation of setScreenResolution */ 
    9597        bool setScreenResolutionImpl(const osg::GraphicsContext::ScreenIdentifier& screenIdentifier, unsigned int width, unsigned int height) ; 
     
    118120     
    119121    private: 
     122 
     123        bool                  _initialized; 
    120124        CGDisplayCount        _displayCount; 
    121125        CGDirectDisplayID*    _displayIds; 
  • OpenSceneGraph/trunk/src/osgViewer/DarwinUtils.mm

    r10622 r11207  
    222222/** ctor, get a list of all attached displays */ 
    223223DarwinWindowingSystemInterface::DarwinWindowingSystemInterface() : 
     224    _initialized(false), 
    224225    _displayCount(0), 
    225226    _displayIds(NULL) 
    226227{ 
     228} 
     229 
     230/** dtor */ 
     231DarwinWindowingSystemInterface::~DarwinWindowingSystemInterface() 
     232{ 
     233    if (osg::Referenced::getDeleteHandler()) 
     234    { 
     235        osg::Referenced::getDeleteHandler()->setNumFramesToRetainObjects(0); 
     236        osg::Referenced::getDeleteHandler()->flushAll(); 
     237    } 
     238 
     239    if (_displayIds) delete[] _displayIds; 
     240    _displayIds = NULL; 
     241} 
     242 
     243void DarwinWindowingSystemInterface::_init() 
     244{ 
     245    if (_initialized) return; 
     246 
    227247    ProcessSerialNumber sn = { 0, kCurrentProcess }; 
    228248    TransformProcessType(&sn,kProcessTransformToForegroundApplication); 
    229249    SetFrontProcess(&sn); 
    230      
     250 
    231251    if( CGGetActiveDisplayList( 0, NULL, &_displayCount ) != CGDisplayNoErr ) 
     252    { 
    232253        osg::notify(osg::WARN) << "DarwinWindowingSystemInterface: could not get # of screens" << std::endl; 
    233          
     254        _displayCount = 0; 
     255 
     256        _initialized = true; 
     257        return; 
     258    } 
     259 
    234260    _displayIds = new CGDirectDisplayID[_displayCount]; 
     261 
    235262    if( CGGetActiveDisplayList( _displayCount, _displayIds, &_displayCount ) != CGDisplayNoErr ) 
     263    { 
    236264        osg::notify(osg::WARN) << "DarwinWindowingSystemInterface: CGGetActiveDisplayList failed" << std::endl; 
    237      
    238     } 
    239  
    240 /** dtor */ 
    241 DarwinWindowingSystemInterface::~DarwinWindowingSystemInterface() 
    242 { 
    243     if (osg::Referenced::getDeleteHandler()) 
    244     { 
    245         osg::Referenced::getDeleteHandler()->setNumFramesToRetainObjects(0); 
    246         osg::Referenced::getDeleteHandler()->flushAll(); 
    247     } 
    248  
    249     if (_displayIds) delete[] _displayIds; 
    250     _displayIds = NULL; 
     265    } 
     266 
     267    _initialized = true; 
    251268} 
    252269 
    253270/** @return a CGDirectDisplayID for a ScreenIdentifier */ 
    254 CGDirectDisplayID DarwinWindowingSystemInterface::getDisplayID(const osg::GraphicsContext::ScreenIdentifier& si) { 
     271CGDirectDisplayID DarwinWindowingSystemInterface::getDisplayID(const osg::GraphicsContext::ScreenIdentifier& si) 
     272{ 
     273    _init(); 
     274 
     275    if (_displayCount==0) 
     276    { 
     277        osg::notify(osg::WARN) << "DarwinWindowingSystemInterface::getDisplayID(..) no valid screens available returning 0 instead." << std::endl; 
     278        return 0; 
     279    } 
     280 
    255281    if (si.screenNum < static_cast<int>(_displayCount)) 
     282    { 
    256283        return _displayIds[si.screenNum]; 
    257     else { 
    258         osg::notify(osg::WARN) << "GraphicsWindowCarbon :: invalid screen # " << si.screenNum << ", returning main-screen instead" << std::endl; 
     284    } 
     285    else 
     286    { 
     287        osg::notify(osg::WARN) << "DarwinWindowingSystemInterface::getDisplayID(..) invalid screen # " << si.screenNum << ", returning main-screen instead." << std::endl; 
    259288        return _displayIds[0]; 
    260289    } 
     
    264293unsigned int DarwinWindowingSystemInterface::getNumScreens(const osg::GraphicsContext::ScreenIdentifier& si)  
    265294{ 
     295    _init(); 
     296 
    266297    return _displayCount; 
    267298} 
     
    269300void DarwinWindowingSystemInterface::getScreenSettings(const osg::GraphicsContext::ScreenIdentifier& si, osg::GraphicsContext::ScreenSettings & resolution) 
    270301{ 
     302    _init(); 
     303 
     304    if (_displayCount==0) 
     305    { 
     306        resolution.width = 0; 
     307        resolution.height = 0; 
     308        resolution.colorDepth = 0; 
     309        resolution.refreshRate = 0; 
     310        return; 
     311    } 
     312 
    271313    CGDirectDisplayID id = getDisplayID(si); 
    272314    resolution.width = CGDisplayPixelsWide(id); 
     
    278320 
    279321 
    280 void DarwinWindowingSystemInterface::enumerateScreenSettings(const osg::GraphicsContext::ScreenIdentifier& screenIdentifier, osg::GraphicsContext::ScreenSettingsList & resolutionList) { 
    281         // Warning! This method has not been tested. 
    282         resolutionList.clear(); 
    283  
    284         CGDirectDisplayID displayid = getDisplayID(screenIdentifier); 
    285         CFArrayRef availableModes = CGDisplayAvailableModes(displayid); 
    286         unsigned int numberOfAvailableModes = CFArrayGetCount(availableModes); 
    287         for (unsigned int i=0; i<numberOfAvailableModes; ++i) { 
    288             // look at each mode in the available list 
    289             CFDictionaryRef mode = (CFDictionaryRef)CFArrayGetValueAtIndex(availableModes, i); 
    290             osg::GraphicsContext::ScreenSettings tmpSR; 
    291  
    292             long width = getDictLong(mode, kCGDisplayWidth); 
    293             tmpSR.width = width<=0 ? 0 : width; 
    294             long height = getDictLong(mode, kCGDisplayHeight); 
    295             tmpSR.height = height<=0 ? 0 : height; 
    296             long rate = getDictLong(mode, kCGDisplayRefreshRate); 
    297             tmpSR.refreshRate = rate<=0 ? 0 : rate; 
    298             long depth = getDictLong(mode, kCGDisplayBitsPerPixel); 
    299             tmpSR.colorDepth = depth<=0 ? 0 : depth; 
    300  
    301             resolutionList.push_back(tmpSR); 
    302         } 
    303     } 
     322void DarwinWindowingSystemInterface::enumerateScreenSettings(const osg::GraphicsContext::ScreenIdentifier& screenIdentifier, osg::GraphicsContext::ScreenSettingsList & resolutionList) 
     323{ 
     324    _init(); 
     325 
     326    // Warning! This method has not been tested. 
     327    resolutionList.clear(); 
     328 
     329    if (_displayCount==0) 
     330    { 
     331        return; 
     332    } 
     333 
     334    CGDirectDisplayID displayid = getDisplayID(screenIdentifier); 
     335    CFArrayRef availableModes = CGDisplayAvailableModes(displayid); 
     336    unsigned int numberOfAvailableModes = CFArrayGetCount(availableModes); 
     337    for (unsigned int i=0; i<numberOfAvailableModes; ++i) { 
     338        // look at each mode in the available list 
     339        CFDictionaryRef mode = (CFDictionaryRef)CFArrayGetValueAtIndex(availableModes, i); 
     340        osg::GraphicsContext::ScreenSettings tmpSR; 
     341 
     342        long width = getDictLong(mode, kCGDisplayWidth); 
     343        tmpSR.width = width<=0 ? 0 : width; 
     344        long height = getDictLong(mode, kCGDisplayHeight); 
     345        tmpSR.height = height<=0 ? 0 : height; 
     346        long rate = getDictLong(mode, kCGDisplayRefreshRate); 
     347        tmpSR.refreshRate = rate<=0 ? 0 : rate; 
     348        long depth = getDictLong(mode, kCGDisplayBitsPerPixel); 
     349        tmpSR.colorDepth = depth<=0 ? 0 : depth; 
     350 
     351        resolutionList.push_back(tmpSR); 
     352    } 
     353} 
    304354 
    305355/** return the top left coord of a specific screen in global screen space */ 
    306 void DarwinWindowingSystemInterface::getScreenTopLeft(const osg::GraphicsContext::ScreenIdentifier& si, int& x, int& y) { 
     356void DarwinWindowingSystemInterface::getScreenTopLeft(const osg::GraphicsContext::ScreenIdentifier& si, int& x, int& y) 
     357{ 
     358    _init(); 
     359 
     360    if (_displayCount==0) 
     361    { 
     362        x = 0; 
     363        y = 0; 
     364        return; 
     365    } 
     366 
    307367    CGRect bounds = CGDisplayBounds( getDisplayID(si) ); 
    308368    x = static_cast<int>(bounds.origin.x); 
     
    317377    bool result = setScreenResolutionImpl(si, settings.width, settings.height); 
    318378    if (result) 
     379    { 
    319380        setScreenRefreshRateImpl(si, settings.refreshRate); 
    320      
     381    } 
     382 
    321383    return result; 
    322384} 
     
    327389bool DarwinWindowingSystemInterface::setScreenResolutionImpl(const osg::GraphicsContext::ScreenIdentifier& screenIdentifier, unsigned int width, unsigned int height)  
    328390{  
     391    _init(); 
     392 
     393    if (_displayCount==0) 
     394    { 
     395        return false; 
     396    } 
     397 
    329398    CGDirectDisplayID displayid = getDisplayID(screenIdentifier); 
    330399     
     
    345414 
    346415/** implementation of setScreenRefreshRate */ 
    347 bool DarwinWindowingSystemInterface::setScreenRefreshRateImpl(const osg::GraphicsContext::ScreenIdentifier& screenIdentifier, double refreshRate) {  
    348      
     416bool DarwinWindowingSystemInterface::setScreenRefreshRateImpl(const osg::GraphicsContext::ScreenIdentifier& screenIdentifier, double refreshRate) 
     417{  
     418    _init(); 
     419 
     420    if (_displayCount==0) 
     421    { 
     422        return false; 
     423    } 
     424 
    349425    boolean_t  success(false); 
    350426    unsigned width, height; 
     
    372448unsigned int DarwinWindowingSystemInterface::getScreenContaining(int x, int y, int w, int h) 
    373449{ 
     450    _init(); 
     451 
     452    if (_displayCount==0) 
     453    { 
     454        return 0; 
     455    } 
     456 
    374457    CGRect rect = CGRectMake(x,y,w,h); 
    375458    for(unsigned int i = 0; i < _displayCount; ++i) { 
     
    383466} 
    384467 
    385  
    386  
    387  
    388  
    389  
    390 } 
     468} 
  • OpenSceneGraph/trunk/src/osgViewer/GraphicsWindowCarbon.cpp

    r11046 r11207  
    10541054class CarbonWindowingSystemInterface : public  DarwinWindowingSystemInterface { 
    10551055public: 
    1056     CarbonWindowingSystemInterface() 
    1057     :    DarwinWindowingSystemInterface() 
    1058     { 
     1056    CarbonWindowingSystemInterface() : DarwinWindowingSystemInterface() 
     1057    { 
     1058    } 
     1059 
     1060    virtual osg::GraphicsContext* createGraphicsContext(osg::GraphicsContext::Traits* traits)  
     1061    { 
     1062        _init(); 
     1063 
     1064        return createGraphicsContextImplementation<PixelBufferCarbon, GraphicsWindowCarbon>(traits); 
     1065    } 
     1066 
     1067    virtual void _init() 
     1068    { 
     1069        if (_initialized) return; 
     1070 
     1071        DarwinWindowingSystemInterface::init(); 
     1072 
    10591073        // register application event handler and AppleEventHandler to get quit-events: 
    10601074        static const EventTypeSpec menueventSpec = {kEventClassCommand, kEventCommandProcess}; 
     
    10621076        status = AEInstallEventHandler( kCoreEventClass, kAEQuitApplication, NewAEEventHandlerUPP(QuitAppleEventHandler), 0, false); 
    10631077    } 
    1064      
    1065     virtual osg::GraphicsContext* createGraphicsContext(osg::GraphicsContext::Traits* traits)  
    1066     { 
    1067         return createGraphicsContextImplementation<PixelBufferCarbon, GraphicsWindowCarbon>(traits); 
    1068     } 
     1078 
    10691079}; 
    10701080 
  • OpenSceneGraph/trunk/src/osgViewer/GraphicsWindowCocoa.mm

    r10887 r11207  
    13831383// ---------------------------------------------------------------------------------------------------------- 
    13841384 
    1385 struct CocoaWindowingSystemInterface : public DarwinWindowingSystemInterface { 
    1386      
    1387     CocoaWindowingSystemInterface() 
    1388     :    DarwinWindowingSystemInterface() 
    1389      
    1390     {         
    1391     } 
    1392      
     1385struct CocoaWindowingSystemInterface : public DarwinWindowingSystemInterface 
     1386{ 
     1387 
     1388    CocoaWindowingSystemInterface() : DarwinWindowingSystemInterface() 
     1389    { 
     1390    } 
     1391 
    13931392    void initAsStandaloneApplication()  
    13941393    { 
     1394        _init(); 
     1395 
    13951396        static bool s_inited = false; 
    13961397        if (s_inited) return; 
     
    14221423    virtual osg::GraphicsContext* createGraphicsContext(osg::GraphicsContext::Traits* traits)  
    14231424    { 
     1425        _init(); 
     1426 
    14241427        if (!traits->pbuffer)  
    14251428        {