fixed: auto_ptr -> unique_ptr
[opensg.git] / Source / Contrib / ComplexSceneManager / OSGCSMDrawer.cpp
blob98e3cdcf864509d65c3fc63e9b282ed1f9cb01fe
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 "OSGCSMDrawer.h"
49 #include "OSGCSMWindow.h"
51 #include "OSGRenderAction.h"
52 #include "OSGNameAttachment.h"
54 OSG_BEGIN_NAMESPACE
56 // Documentation for this class is emitted in the
57 // OSGCSMDrawerBase.cpp file.
58 // To modify it, please change the .fcd file (OSGCSMDrawer.fcd) and
59 // regenerate the base file.
61 /***************************************************************************\
62 * Class variables *
63 \***************************************************************************/
65 /***************************************************************************\
66 * Class methods *
67 \***************************************************************************/
69 void CSMDrawer::initMethod(InitPhase ePhase)
71 Inherited::initMethod(ePhase);
73 if(ePhase == TypeObject::SystemPost)
79 /***************************************************************************\
80 * Instance methods *
81 \***************************************************************************/
83 /*-------------------------------------------------------------------------*\
84 - private -
85 \*-------------------------------------------------------------------------*/
87 /*----------------------- constructors & destructors ----------------------*/
89 CSMDrawer::CSMDrawer(void) :
90 Inherited ( ),
91 _pAction (NULL ),
92 _pDrawThread (NULL ),
93 _pSyncFromThread(NULL ),
94 _pSyncBarrier (NULL ),
95 _pSwapBarrier (NULL ),
96 #ifdef OSG_GLOBAL_SYNC_LOCK
97 _pSyncLock (NULL ),
98 #endif
100 _uiSyncCount (0 ),
101 _uiSwapCount (0 ),
102 _bParallel (false),
103 _bRun (false)
107 CSMDrawer::CSMDrawer(const CSMDrawer &source) :
108 Inherited (source),
109 _pAction (NULL ),
110 _pDrawThread (NULL ),
111 _pSyncFromThread(NULL ),
112 _pSyncBarrier (NULL ),
113 _pSwapBarrier (NULL ),
114 #ifdef OSG_GLOBAL_SYNC_LOCK
115 _pSyncLock (NULL ),
116 #endif
118 _uiSyncCount (0 ),
119 _uiSwapCount (0 ),
120 _bParallel (false ),
121 _bRun (false)
125 CSMDrawer::~CSMDrawer(void)
127 _pAction = NULL;
130 void CSMDrawer::setSyncFromThread(Thread *pThread)
132 _pSyncFromThread = pThread;
135 void CSMDrawer::setSyncBarrier(Barrier *pSyncBarrier)
137 _pSyncBarrier = pSyncBarrier;
140 void CSMDrawer::setSwapBarrier(Barrier *pSwapBarrier)
142 _pSwapBarrier = pSwapBarrier;
145 #ifdef OSG_GLOBAL_SYNC_LOCK
146 void CSMDrawer::setSyncLock(Lock *pSyncLock)
148 _pSyncLock = pSyncLock;
150 #endif
153 void CSMDrawer::setSyncCount(UInt32 uiSyncCount)
155 _uiSyncCount = uiSyncCount;
158 void CSMDrawer::setSwapCount(UInt32 uiSwapCount)
160 _uiSwapCount = uiSwapCount;
163 void CSMDrawer::setParallel(bool bParallel)
165 _bParallel = bParallel;
168 void CSMDrawer::postSync(void)
170 MFUnrecChildCSMWindowPtr::const_iterator wIt = getMFWindows()->begin();
171 MFUnrecChildCSMWindowPtr::const_iterator wEnd = getMFWindows()->end ();
173 for(; wIt != wEnd; ++wIt)
175 (*wIt)->postSync();
179 /*----------------------------- class specific ----------------------------*/
181 void CSMDrawer::changed(ConstFieldMaskArg whichField,
182 UInt32 origin,
183 BitVector details)
185 Inherited::changed(whichField, origin, details);
188 void CSMDrawer::dump( UInt32 ,
189 const BitVector ) const
191 SLOG << "Dump CSMDrawer NI" << std::endl;
194 bool CSMDrawer::init(void)
196 bool returnValue = true;
198 MFUnrecChildCSMWindowPtr::const_iterator wIt = getMFWindows()->begin();
199 MFUnrecChildCSMWindowPtr::const_iterator wEnd = getMFWindows()->end ();
201 for(; wIt != wEnd; ++wIt)
203 returnValue = (*wIt)->init();
205 if(returnValue == false)
206 break;
209 _pAction = RenderAction::create();
211 if(_bParallel == true)
213 _pDrawThread = CSMDrawThread ::get(NULL, false);
215 OSG_ASSERT(_pDrawThread != NULL);
216 OSG_ASSERT(_pSyncBarrier != NULL);
217 OSG_ASSERT(_pSyncFromThread != NULL);
218 #ifdef OSG_GLOBAL_SYNC_LOCK
219 OSG_ASSERT(_pSyncLock != NULL);
220 #endif
222 _pDrawThread->setDrawer(this);
223 _pDrawThread->run(_sfAspect.getValue());
226 // _pAction->setFrustumCulling(false);
228 return returnValue;
231 void CSMDrawer::endDrawThread (void)
233 if(_bParallel == true)
235 _pDrawThread->setRunning(false);
239 void CSMDrawer::shutdown(void)
241 MFUnrecChildCSMWindowPtr::const_iterator winIt = getMFWindows()->begin();
242 MFUnrecChildCSMWindowPtr::const_iterator winEnd = getMFWindows()->end ();
244 while(winIt != winEnd)
246 (*winIt)->shutdown();
248 ++winIt;
252 void CSMDrawer::joinDrawThread(void)
254 if(_bParallel == true)
256 Thread::join(_pDrawThread);
258 _pDrawThread = NULL;
262 void CSMDrawer::frame(Time oTime, UInt32 uiFrame)
264 MFUnrecChildCSMWindowPtr::const_iterator winIt = getMFWindows()->begin();
265 MFUnrecChildCSMWindowPtr::const_iterator winEnd = getMFWindows()->end ();
267 while(winIt != winEnd)
269 (*winIt)->render(_pAction);
271 ++winIt;
275 FieldContainer *CSMDrawer::findNamedComponent(const Char8 *szName)
277 MFUnrecChildCSMWindowPtr::const_iterator winIt = _mfWindows.begin();
278 MFUnrecChildCSMWindowPtr::const_iterator winEnd = _mfWindows.end ();
280 const Char8 *szTmpName = NULL;
282 while(winIt != winEnd)
284 szTmpName = OSG::getName(*winIt);
286 if(szTmpName != NULL && osgStringCmp(szTmpName, szName) == 0)
288 return *winIt;
290 else
292 FieldContainer *tmpVal = (*winIt)->findNamedComponent(szName);
294 if(tmpVal != NULL)
295 return tmpVal;
298 ++winIt;
301 return NULL;
304 void CSMDrawer::render(void)
306 MFUnrecChildCSMWindowPtr::const_iterator winIt = getMFWindows()->begin();
307 MFUnrecChildCSMWindowPtr::const_iterator winEnd = getMFWindows()->end ();
309 while(winIt != winEnd)
311 (*winIt)->render(_pAction);
313 ++winIt;
317 void CSMDrawer::runParallel(void)
319 fprintf(stderr, "Drawer run par\n");
321 _bRun = true;
323 for(UInt32 i = 0; i < 10; ++i)
325 _pSyncBarrier->enter(_uiSyncCount);
326 _pSyncBarrier->enter(_uiSyncCount);
328 #ifdef OSG_GLOBAL_SYNC_LOCK
329 _pSyncLock->acquire();
330 #endif
332 _pSyncFromThread->getChangeList()->applyNoClear();
334 #ifdef OSG_GLOBAL_SYNC_LOCK
335 _pSyncLock->release();
336 #endif
338 _pSyncBarrier->enter(_uiSyncCount);
340 Thread::getCurrentChangeList()->commitChanges();
343 if(_pSwapBarrier == NULL)
345 fprintf(stderr, "Running without swap lock\n");
346 fflush (stderr);
348 fprintf(stderr, "Running with %" PRISize "-windws\n",
349 _mfWindows.size());
350 fflush (stderr);
352 while(_bRun == true)
354 _pSyncBarrier->enter (_uiSyncCount);
355 _pSyncBarrier->enter (_uiSyncCount);
357 # ifdef OSG_GLOBAL_SYNC_LOCK
358 _pSyncLock->acquire();
359 # endif
361 _pSyncFromThread->getChangeList()->applyNoClear();
363 # ifdef OSG_GLOBAL_SYNC_LOCK
364 _pSyncLock->release();
365 # endif
367 _pSyncBarrier->enter (_uiSyncCount);
369 Thread::getCurrentChangeList()->commitChanges();
371 if(_bRun == false)
373 Thread::getCurrentChangeList()->commitChangesAndClear();
375 break;
378 this->render();
380 Thread::getCurrentChangeList()->commitChangesAndClear();
383 else
385 fprintf(stderr, "Running with swap lock\n");
386 fflush (stderr);
388 fprintf(stderr, "Running with %" PRISize "-windws\n",
389 _mfWindows.size());
390 fflush (stderr);
392 while(_bRun == true)
394 _pSyncBarrier->enter (_uiSyncCount);
395 _pSyncBarrier->enter (_uiSyncCount);
397 # ifdef OSG_GLOBAL_SYNC_LOCK
398 _pSyncLock->acquire();
399 # endif
400 _pSyncFromThread->getChangeList()->applyNoClear();
402 # ifdef OSG_GLOBAL_SYNC_LOCK
403 _pSyncLock->release();
404 # endif
406 _pSyncBarrier->enter (_uiSyncCount);
408 OSG::Thread::getCurrentChangeList()->commitChanges();
410 if(_bRun == false)
412 OSG::Thread::getCurrentChangeList()->commitChangesAndClear();
414 break;
417 this->frameRenderNoFinish( );
419 _pSwapBarrier->enter (_uiSwapCount);
421 this->frameFinish ( );
423 OSG::Thread::getCurrentChangeList()->commitChangesAndClear();
427 _pSyncBarrier->enter (_uiSyncCount);
429 fprintf(stderr, "Drawer run par stop\n");
432 // Main structure
433 _pSyncBarrier->enter(_uiSyncCount);
435 #ifdef OSG_GLOBAL_SYNC_LOCK
436 _pSyncLock->acquire();
437 #endif
439 // _pSyncFromThread->getChangeList()->dump();
440 _pSyncFromThread->getChangeList()->applyNoClear();
442 #ifdef OSG_GLOBAL_SYNC_LOCK
443 _pSyncLock->release();
444 #endif
446 Thread::getCurrentChangeList()->commitChangesAndClear();
448 this->frameExit();
450 _pSyncBarrier->enter(_uiSyncCount);
453 // gl takedown
454 _pSyncBarrier->enter(_uiSyncCount);
456 #ifdef OSG_GLOBAL_SYNC_LOCK
457 _pSyncLock->acquire();
458 #endif
460 // _pSyncFromThread->getChangeList()->dump();
461 _pSyncFromThread->getChangeList()->applyNoClear();
463 #ifdef OSG_GLOBAL_SYNC_LOCK
464 _pSyncLock->release();
465 #endif
467 _pSyncBarrier->enter(_uiSyncCount);
470 // Windows
471 _pSyncBarrier->enter(_uiSyncCount);
473 #ifdef OSG_GLOBAL_SYNC_LOCK
474 _pSyncLock->acquire();
475 #endif
477 // _pSyncFromThread->getChangeList()->dump();
478 _pSyncFromThread->getChangeList()->applyNoClear();
480 #ifdef OSG_GLOBAL_SYNC_LOCK
481 _pSyncLock->release();
482 #endif
484 _pSyncBarrier->enter(_uiSyncCount);
486 Thread::getCurrentChangeList()->commitChangesAndClear();
489 void CSMDrawer::frameRenderNoFinish(void)
491 MFUnrecChildCSMWindowPtr::const_iterator winIt = getMFWindows()->begin();
492 MFUnrecChildCSMWindowPtr::const_iterator winEnd = getMFWindows()->end ();
494 while(winIt != winEnd)
496 (*winIt)->frameRenderNoFinish(_pAction);
498 ++winIt;
502 void CSMDrawer::frameFinish(void)
504 MFUnrecChildCSMWindowPtr::const_iterator winIt = getMFWindows()->begin();
505 MFUnrecChildCSMWindowPtr::const_iterator winEnd = getMFWindows()->end ();
507 while(winIt != winEnd)
509 (*winIt)->frameFinish();
511 ++winIt;
515 void CSMDrawer::frameExit(void)
517 MFUnrecChildCSMWindowPtr::const_iterator winIt = getMFWindows()->begin();
518 MFUnrecChildCSMWindowPtr::const_iterator winEnd = getMFWindows()->end ();
520 while(winIt != winEnd)
522 (*winIt)->frameExit();
524 ++winIt;
528 void CSMDrawer::setRunning(bool bVal)
530 _bRun = false;
533 void CSMDrawer::resolveLinks(void)
535 Inherited::resolveLinks();
538 void CSMDrawer::terminateGLContexts(void)
540 MFUnrecChildCSMWindowPtr::const_iterator winIt = getMFWindows()->begin();
541 MFUnrecChildCSMWindowPtr::const_iterator winEnd = getMFWindows()->end ();
543 while(winIt != winEnd)
545 (*winIt)->terminateGLContext();
547 ++winIt;
552 /***************************************************************************\
553 * Class variables *
554 \***************************************************************************/
556 MPThreadType CSMDrawThread::_type(
557 "OSGCSMDrawThread",
558 "OSGThread",
559 static_cast<CreateThreadF>(CSMDrawThread::create),
560 NULL);
562 /***************************************************************************\
563 * Class methods *
564 \***************************************************************************/
566 /***************************************************************************\
567 * Instance methods *
568 \***************************************************************************/
570 /*-------------------------------------------------------------------------*\
571 - private -
572 \*-------------------------------------------------------------------------*/
574 CSMDrawThread *CSMDrawThread::find(Char8 *szName)
576 BaseThread *pThread = ThreadManager::the()->findThread(szName);
578 return dynamic_cast<CSMDrawThread *>(pThread);
581 CSMDrawThread::ObjTransitPtr CSMDrawThread::get(Char8 *szName, bool bGlobal)
583 BaseThreadTransitPtr pThread =
584 ThreadManager::the()->getThread(szName,
585 bGlobal,
586 "OSGCSMDrawThread");
588 return dynamic_pointer_cast<CSMDrawThread>(pThread);
591 void CSMDrawThread::setDrawer(CSMDrawer *pDrawer)
593 _pDrawer = pDrawer;
596 void CSMDrawThread::setRunning(bool bVal)
598 _pDrawer->setRunning(bVal);
602 BaseThread *CSMDrawThread::create(const Char8 *szName,
603 UInt32 uiId,
604 bool bGlobal)
606 return new CSMDrawThread(szName, uiId, bGlobal);
609 CSMDrawThread::CSMDrawThread(const Char8 *szName, UInt32 uiId, bool bGlobal) :
610 Inherited(szName,
611 uiId,
612 bGlobal),
613 _pDrawer (NULL )
617 CSMDrawThread::~CSMDrawThread(void)
621 void CSMDrawThread::workProc(void)
623 OSG_ASSERT(_pDrawer != NULL);
625 _pDrawer->runParallel();
628 OSG_END_NAMESPACE