1 /*---------------------------------------------------------------------------*\
5 * Copyright (C) 2000-2006 by the OpenSG Forum *
9 * contact: dirk@opensg.org, gerrit.voss@vossg.org, jbehr@zgdv.de *
11 \*---------------------------------------------------------------------------*/
12 /*---------------------------------------------------------------------------*\
15 * This library is free software; you can redistribute it and/or modify it *
16 * under the terms of the GNU Library General Public License as published *
17 * by the Free Software Foundation, version 2. *
19 * This library is distributed in the hope that it will be useful, but *
20 * WITHOUT ANY WARRANTY; without even the implied warranty of *
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
22 * Library General Public License for more details. *
24 * You should have received a copy of the GNU Library General Public *
25 * License along with this library; if not, write to the Free Software *
26 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *
28 \*---------------------------------------------------------------------------*/
29 /*---------------------------------------------------------------------------*\
37 \*---------------------------------------------------------------------------*/
39 //---------------------------------------------------------------------------
41 //---------------------------------------------------------------------------
46 #include "OSGConfig.h"
48 #include "OSGCSMDrawManager.h"
49 #include "OSGNameAttachment.h"
50 #include "OSGCSMNativeWindow.h"
54 // Documentation for this class is emitted in the
55 // OSGCSMDrawManagerBase.cpp file.
56 // To modify it, please change the .fcd file (OSGCSMDrawManager.fcd) and
57 // regenerate the base file.
59 /***************************************************************************\
61 \***************************************************************************/
63 /***************************************************************************\
65 \***************************************************************************/
67 void CSMDrawManager::initMethod(InitPhase ePhase
)
69 Inherited::initMethod(ePhase
);
71 if(ePhase
== TypeObject::SystemPost
)
77 /***************************************************************************\
79 \***************************************************************************/
81 /*-------------------------------------------------------------------------*\
83 \*-------------------------------------------------------------------------*/
85 /*----------------------- constructors & destructors ----------------------*/
87 CSMDrawManager::CSMDrawManager(void) :
93 #ifdef OSG_GLOBAL_SYNC_LOCK
100 CSMDrawManager::CSMDrawManager(const CSMDrawManager
&source
) :
104 _pSyncBarrier(NULL
),
105 _pSwapBarrier(NULL
),
106 #ifdef OSG_GLOBAL_SYNC_LOCK
113 CSMDrawManager::~CSMDrawManager(void)
117 /*----------------------------- class specific ----------------------------*/
119 void CSMDrawManager::changed(ConstFieldMaskArg whichField
,
123 Inherited::changed(whichField
, origin
, details
);
126 void CSMDrawManager::dump( UInt32
,
127 const BitVector
) const
129 SLOG
<< "Dump CSMDrawManager NI" << std::endl
;
132 bool CSMDrawManager::init(void)
134 bool returnValue
= true;
136 if(_sfParallel
.getValue() == true)
138 CSMNativeWindow::initWindowSystemThreading();
141 if(_sfAppDrawer
.getValue() != NULL
)
143 returnValue
= _sfAppDrawer
.getValue()->init();
146 MFUnrecCSMDrawerPtr::const_iterator dIt
= getMFDrawer()->begin();
147 MFUnrecCSMDrawerPtr::const_iterator dEnd
= getMFDrawer()->end ();
149 if(_sfParallel
.getValue() == true)
151 _pThread
= dynamic_cast<OSG::Thread
*>(Thread::getCurrent());
153 #ifdef OSG_GLOBAL_SYNC_LOCK
154 _pSyncLock
= Lock::get("DM::synclock", false);
157 _pSyncBarrier
= Barrier::get(_sfSyncBarrierName
.getValue().c_str(),
160 if(_sfSwapBarrierName
.getValue().empty() == false)
162 _pSwapBarrier
= Barrier::get(_sfSwapBarrierName
.getValue().c_str(),
166 OSG_ASSERT(_pSyncBarrier
!= NULL
);
167 OSG_ASSERT(_pThread
!= NULL
);
168 #ifdef OSG_GLOBAL_SYNC_LOCK
169 OSG_ASSERT(_pSyncLock
!= NULL
);
172 _uiSyncCount
= _mfDrawer
.size32() + 1;
176 (*dIt
)->setParallel ( true );
178 (*dIt
)->setSyncBarrier (_pSyncBarrier
);
179 (*dIt
)->setSwapBarrier (_pSwapBarrier
);
180 #ifdef OSG_GLOBAL_SYNC_LOCK
181 (*dIt
)->setSyncLock (_pSyncLock
);
184 (*dIt
)->setSyncCount (_uiSyncCount
);
185 (*dIt
)->setSyncFromThread(_pThread
);
187 (*dIt
)->setSwapCount (_mfDrawer
.size32());
197 returnValue
= (*dIt
)->init();
199 if(returnValue
== false)
209 void CSMDrawManager::shutdown(void)
211 if(_sfAppDrawer
.getValue() != NULL
)
213 _sfAppDrawer
.getValue()->shutdown();
216 if(_sfParallel
.getValue() == true)
218 MFUnrecCSMDrawerPtr::const_iterator dIt
= getMFDrawer()->begin();
219 MFUnrecCSMDrawerPtr::const_iterator dEnd
= getMFDrawer()->end ();
226 _pSyncBarrier
->enter(_uiSyncCount
);
227 _pSyncBarrier
->enter(_uiSyncCount
);
231 (*dIt
)->endDrawThread();
236 _pSyncBarrier
->enter(_uiSyncCount
);
237 _pSyncBarrier
->enter(_uiSyncCount
);
239 _pThread
->getChangeList()->clear();
244 dIt
= getMFDrawer()->begin();
253 AttachmentContainer::resolveLinks();
255 if(_sfAppDrawer
.getValue() != NULL
)
257 _sfAppDrawer
.getValue()->terminateGLContexts();
260 // sync structure takedown
264 _pSyncBarrier
->enter(_uiSyncCount
);
265 _pSyncBarrier
->enter(_uiSyncCount
);
267 _pThread
->getChangeList()->clear();
270 // release gl contexts
272 dIt
= getMFDrawer()->begin();
276 (*dIt
)->terminateGLContexts();
285 _pSyncBarrier
->enter(_uiSyncCount
);
286 _pSyncBarrier
->enter(_uiSyncCount
);
288 _pThread
->getChangeList()->clear();
293 if(_sfAppDrawer
.getValue() != NULL
)
295 _sfAppDrawer
.getValue()->resolveLinks();
298 dIt
= getMFDrawer()->begin();
302 (*dIt
)->resolveLinks();
309 _pSyncBarrier
->enter(_uiSyncCount
);
310 _pSyncBarrier
->enter(_uiSyncCount
);
312 _pThread
->getChangeList()->clear();
315 // wait for draw threads to finish
317 dIt
= getMFDrawer()->begin();
321 (*dIt
)->joinDrawThread();
326 dIt
= getMFDrawer()->begin();
330 (*dIt
)->setSyncBarrier (NULL
);
331 (*dIt
)->setSwapBarrier (NULL
);
333 #ifdef OSG_GLOBAL_SYNC_LOCK
334 (*dIt
)->setSyncLock (NULL
);
337 (*dIt
)->setSyncFromThread(NULL
);
343 _pSyncBarrier
= NULL
;
344 _pSwapBarrier
= NULL
;
346 #ifdef OSG_GLOBAL_SYNC_LOCK
352 MFUnrecCSMDrawerPtr::const_iterator dIt
= getMFDrawer()->begin();
353 MFUnrecCSMDrawerPtr::const_iterator dEnd
= getMFDrawer()->end ();
355 for(; dIt
!= dEnd
; ++dIt
)
362 AttachmentContainer::resolveLinks();
364 if(_sfAppDrawer
.getValue() != NULL
)
366 _sfAppDrawer
.getValue()->terminateGLContexts();
369 dIt
= getMFDrawer()->begin();
373 (*dIt
)->terminateGLContexts();
382 void CSMDrawManager::frame(Time oTime
, UInt32 uiFrame
)
386 if(_sfParallel
.getValue() == true)
388 _pSyncBarrier
->enter(_uiSyncCount
);
390 this->syncProducers(uiFrame
);
392 _pSyncBarrier
->enter(_uiSyncCount
);
393 _pSyncBarrier
->enter(_uiSyncCount
);
395 MFUnrecCSMDrawerPtr::const_iterator drawerIt
= getMFDrawer()->begin();
396 MFUnrecCSMDrawerPtr::const_iterator drawerEnd
= getMFDrawer()->end ();
398 for(; drawerIt
!= drawerEnd
; ++drawerIt
)
400 (*drawerIt
)->postSync();
403 _pThread
->getChangeList()->clear();
405 if(_sfAppDrawer
.getValue() != NULL
)
407 _sfAppDrawer
.getValue()->frame(oTime
, uiFrame
);
412 this->syncProducers(uiFrame
);
414 if(_sfAppDrawer
.getValue() != NULL
)
416 _sfAppDrawer
.getValue()->frame(oTime
, uiFrame
);
419 MFUnrecCSMDrawerPtr::const_iterator drawerIt
= getMFDrawer()->begin();
420 MFUnrecCSMDrawerPtr::const_iterator drawerEnd
= getMFDrawer()->end ();
422 for(; drawerIt
!= drawerEnd
; ++drawerIt
)
424 (*drawerIt
)->frame(oTime
, uiFrame
);
429 FieldContainer
*CSMDrawManager::findNamedComponent(const Char8
*szName
)
431 MFUnrecCSMDrawerPtr::const_iterator drawerIt
= _mfDrawer
.begin();
432 MFUnrecCSMDrawerPtr::const_iterator drawerEnd
= _mfDrawer
.end ();
434 const Char8
*szTmpName
= NULL
;
436 if(_sfAppDrawer
.getValue() != NULL
)
438 szTmpName
= OSG::getName(_sfAppDrawer
.getValue());
440 if(szTmpName
!= NULL
&& osgStringCmp(szTmpName
, szName
) == 0)
442 return _sfAppDrawer
.getValue();
446 FieldContainer
*tmpVal
=
447 _sfAppDrawer
.getValue()->findNamedComponent(szName
);
454 while(drawerIt
!= drawerEnd
)
456 szTmpName
= OSG::getName(*drawerIt
);
458 if(szTmpName
!= NULL
&& osgStringCmp(szTmpName
, szName
) == 0)
464 FieldContainer
*tmpVal
= (*drawerIt
)->findNamedComponent(szName
);