changed: gcc8 base update
[opensg.git] / Source / Contrib / ComplexSceneManager / OSGCSMDrawManager.cpp
blob823b75e1ff70a6a114b2488deb139a03f0983c97
1 /*---------------------------------------------------------------------------*\
2 * OpenSG *
3 * *
4 * *
5 * Copyright (C) 2000-2006 by the OpenSG Forum *
6 * *
7 * www.opensg.org *
8 * *
9 * contact: dirk@opensg.org, gerrit.voss@vossg.org, jbehr@zgdv.de *
10 * *
11 \*---------------------------------------------------------------------------*/
12 /*---------------------------------------------------------------------------*\
13 * License *
14 * *
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. *
18 * *
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. *
23 * *
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. *
27 * *
28 \*---------------------------------------------------------------------------*/
29 /*---------------------------------------------------------------------------*\
30 * Changes *
31 * *
32 * *
33 * *
34 * *
35 * *
36 * *
37 \*---------------------------------------------------------------------------*/
39 //---------------------------------------------------------------------------
40 // Includes
41 //---------------------------------------------------------------------------
43 #include <cstdlib>
44 #include <cstdio>
46 #include "OSGConfig.h"
48 #include "OSGCSMDrawManager.h"
49 #include "OSGNameAttachment.h"
50 #include "OSGCSMNativeWindow.h"
52 OSG_BEGIN_NAMESPACE
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 /***************************************************************************\
60 * Class variables *
61 \***************************************************************************/
63 /***************************************************************************\
64 * Class methods *
65 \***************************************************************************/
67 void CSMDrawManager::initMethod(InitPhase ePhase)
69 Inherited::initMethod(ePhase);
71 if(ePhase == TypeObject::SystemPost)
77 /***************************************************************************\
78 * Instance methods *
79 \***************************************************************************/
81 /*-------------------------------------------------------------------------*\
82 - private -
83 \*-------------------------------------------------------------------------*/
85 /*----------------------- constructors & destructors ----------------------*/
87 CSMDrawManager::CSMDrawManager(void) :
88 Inherited ( ),
90 _pThread (NULL),
91 _pSyncBarrier(NULL),
92 _pSwapBarrier(NULL),
93 #ifdef OSG_GLOBAL_SYNC_LOCK
94 _pSyncLock (NULL),
95 #endif
96 _uiSyncCount (0 )
100 CSMDrawManager::CSMDrawManager(const CSMDrawManager &source) :
101 Inherited (source),
103 _pThread (NULL ),
104 _pSyncBarrier(NULL ),
105 _pSwapBarrier(NULL ),
106 #ifdef OSG_GLOBAL_SYNC_LOCK
107 _pSyncLock (NULL ),
108 #endif
109 _uiSyncCount (0 )
113 CSMDrawManager::~CSMDrawManager(void)
117 /*----------------------------- class specific ----------------------------*/
119 void CSMDrawManager::changed(ConstFieldMaskArg whichField,
120 UInt32 origin,
121 BitVector details)
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);
155 #endif
157 _pSyncBarrier = Barrier::get(_sfSyncBarrierName.getValue().c_str(),
158 false);
160 if(_sfSwapBarrierName.getValue().empty() == false)
162 _pSwapBarrier = Barrier::get(_sfSwapBarrierName.getValue().c_str(),
163 false);
166 OSG_ASSERT(_pSyncBarrier != NULL);
167 OSG_ASSERT(_pThread != NULL);
168 #ifdef OSG_GLOBAL_SYNC_LOCK
169 OSG_ASSERT(_pSyncLock != NULL);
170 #endif
172 _uiSyncCount = _mfDrawer.size32() + 1;
174 while(dIt != dEnd)
176 (*dIt)->setParallel ( true );
178 (*dIt)->setSyncBarrier (_pSyncBarrier );
179 (*dIt)->setSwapBarrier (_pSwapBarrier );
180 #ifdef OSG_GLOBAL_SYNC_LOCK
181 (*dIt)->setSyncLock (_pSyncLock );
182 #endif
184 (*dIt)->setSyncCount (_uiSyncCount );
185 (*dIt)->setSyncFromThread(_pThread );
187 (*dIt)->setSwapCount (_mfDrawer.size32());
188 (*dIt)->init();
190 ++dIt;
193 else
195 while(dIt != dEnd)
197 returnValue = (*dIt)->init();
199 if(returnValue == false)
200 break;
202 ++dIt;
206 return returnValue;
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 ();
222 // Stop drawer
224 commitChanges();
226 _pSyncBarrier->enter(_uiSyncCount);
227 _pSyncBarrier->enter(_uiSyncCount);
229 while(dIt != dEnd)
231 (*dIt)->endDrawThread();
233 ++dIt;
236 _pSyncBarrier->enter(_uiSyncCount);
237 _pSyncBarrier->enter(_uiSyncCount);
239 _pThread->getChangeList()->clear();
242 // resolve structure
244 dIt = getMFDrawer()->begin();
246 while(dIt != dEnd)
248 (*dIt)->shutdown();
250 ++dIt;
253 AttachmentContainer::resolveLinks();
255 if(_sfAppDrawer.getValue() != NULL)
257 _sfAppDrawer.getValue()->terminateGLContexts();
260 // sync structure takedown
262 commitChanges();
264 _pSyncBarrier->enter(_uiSyncCount);
265 _pSyncBarrier->enter(_uiSyncCount);
267 _pThread->getChangeList()->clear();
270 // release gl contexts
272 dIt = getMFDrawer()->begin();
274 while(dIt != dEnd)
276 (*dIt)->terminateGLContexts();
278 ++dIt;
281 // sync gl takedown
283 commitChanges();
285 _pSyncBarrier->enter(_uiSyncCount);
286 _pSyncBarrier->enter(_uiSyncCount);
288 _pThread->getChangeList()->clear();
291 // release windows
293 if(_sfAppDrawer.getValue() != NULL)
295 _sfAppDrawer.getValue()->resolveLinks();
298 dIt = getMFDrawer()->begin();
300 while(dIt != dEnd)
302 (*dIt)->resolveLinks();
304 ++dIt;
307 commitChanges();
309 _pSyncBarrier->enter(_uiSyncCount);
310 _pSyncBarrier->enter(_uiSyncCount);
312 _pThread->getChangeList()->clear();
315 // wait for draw threads to finish
317 dIt = getMFDrawer()->begin();
319 while(dIt != dEnd)
321 (*dIt)->joinDrawThread();
323 ++dIt;
326 dIt = getMFDrawer()->begin();
328 while(dIt != dEnd)
330 (*dIt)->setSyncBarrier (NULL);
331 (*dIt)->setSwapBarrier (NULL);
333 #ifdef OSG_GLOBAL_SYNC_LOCK
334 (*dIt)->setSyncLock (NULL);
335 #endif
337 (*dIt)->setSyncFromThread(NULL);
339 ++dIt;
342 _pThread = NULL;
343 _pSyncBarrier = NULL;
344 _pSwapBarrier = NULL;
346 #ifdef OSG_GLOBAL_SYNC_LOCK
347 _pSyncLock = NULL;
348 #endif
350 else
352 MFUnrecCSMDrawerPtr::const_iterator dIt = getMFDrawer()->begin();
353 MFUnrecCSMDrawerPtr::const_iterator dEnd = getMFDrawer()->end ();
355 for(; dIt != dEnd; ++dIt)
357 (*dIt)->shutdown();
360 commitChanges();
362 AttachmentContainer::resolveLinks();
364 if(_sfAppDrawer.getValue() != NULL)
366 _sfAppDrawer.getValue()->terminateGLContexts();
369 dIt = getMFDrawer()->begin();
371 while(dIt != dEnd)
373 (*dIt)->terminateGLContexts();
375 ++dIt;
378 commitChanges();
382 void CSMDrawManager::frame(Time oTime, UInt32 uiFrame)
384 commitChanges();
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);
410 else
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();
444 else
446 FieldContainer *tmpVal =
447 _sfAppDrawer.getValue()->findNamedComponent(szName);
449 if(tmpVal != NULL)
450 return tmpVal;
454 while(drawerIt != drawerEnd)
456 szTmpName = OSG::getName(*drawerIt);
458 if(szTmpName != NULL && osgStringCmp(szTmpName, szName) == 0)
460 return *drawerIt;
462 else
464 FieldContainer *tmpVal = (*drawerIt)->findNamedComponent(szName);
466 if(tmpVal != NULL)
467 return tmpVal;
470 ++drawerIt;
473 return NULL;
476 OSG_END_NAMESPACE