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 "OSGCSMDrawer.h"
49 #include "OSGCSMWindow.h"
51 #include "OSGRenderAction.h"
52 #include "OSGNameAttachment.h"
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 /***************************************************************************\
63 \***************************************************************************/
65 /***************************************************************************\
67 \***************************************************************************/
69 void CSMDrawer::initMethod(InitPhase ePhase
)
71 Inherited::initMethod(ePhase
);
73 if(ePhase
== TypeObject::SystemPost
)
79 /***************************************************************************\
81 \***************************************************************************/
83 /*-------------------------------------------------------------------------*\
85 \*-------------------------------------------------------------------------*/
87 /*----------------------- constructors & destructors ----------------------*/
89 CSMDrawer::CSMDrawer(void) :
93 _pSyncFromThread(NULL
),
94 _pSyncBarrier (NULL
),
95 _pSwapBarrier (NULL
),
96 #ifdef OSG_GLOBAL_SYNC_LOCK
107 CSMDrawer::CSMDrawer(const CSMDrawer
&source
) :
110 _pDrawThread (NULL
),
111 _pSyncFromThread(NULL
),
112 _pSyncBarrier (NULL
),
113 _pSwapBarrier (NULL
),
114 #ifdef OSG_GLOBAL_SYNC_LOCK
125 CSMDrawer::~CSMDrawer(void)
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
;
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
)
179 /*----------------------------- class specific ----------------------------*/
181 void CSMDrawer::changed(ConstFieldMaskArg whichField
,
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)
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
);
222 _pDrawThread
->setDrawer(this);
223 _pDrawThread
->run(_sfAspect
.getValue());
226 // _pAction->setFrustumCulling(false);
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();
252 void CSMDrawer::joinDrawThread(void)
254 if(_bParallel
== true)
256 Thread::join(_pDrawThread
);
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
);
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)
292 FieldContainer
*tmpVal
= (*winIt
)->findNamedComponent(szName
);
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
);
317 void CSMDrawer::runParallel(void)
319 fprintf(stderr
, "Drawer run par\n");
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();
332 _pSyncFromThread
->getChangeList()->applyNoClear();
334 #ifdef OSG_GLOBAL_SYNC_LOCK
335 _pSyncLock
->release();
338 _pSyncBarrier
->enter(_uiSyncCount
);
340 Thread::getCurrentChangeList()->commitChanges();
343 if(_pSwapBarrier
== NULL
)
345 fprintf(stderr
, "Running without swap lock\n");
348 fprintf(stderr
, "Running with %" PRISize
"-windws\n",
354 _pSyncBarrier
->enter (_uiSyncCount
);
355 _pSyncBarrier
->enter (_uiSyncCount
);
357 # ifdef OSG_GLOBAL_SYNC_LOCK
358 _pSyncLock
->acquire();
361 _pSyncFromThread
->getChangeList()->applyNoClear();
363 # ifdef OSG_GLOBAL_SYNC_LOCK
364 _pSyncLock
->release();
367 _pSyncBarrier
->enter (_uiSyncCount
);
369 Thread::getCurrentChangeList()->commitChanges();
373 Thread::getCurrentChangeList()->commitChangesAndClear();
380 Thread::getCurrentChangeList()->commitChangesAndClear();
385 fprintf(stderr
, "Running with swap lock\n");
388 fprintf(stderr
, "Running with %" PRISize
"-windws\n",
394 _pSyncBarrier
->enter (_uiSyncCount
);
395 _pSyncBarrier
->enter (_uiSyncCount
);
397 # ifdef OSG_GLOBAL_SYNC_LOCK
398 _pSyncLock
->acquire();
400 _pSyncFromThread
->getChangeList()->applyNoClear();
402 # ifdef OSG_GLOBAL_SYNC_LOCK
403 _pSyncLock
->release();
406 _pSyncBarrier
->enter (_uiSyncCount
);
408 OSG::Thread::getCurrentChangeList()->commitChanges();
412 OSG::Thread::getCurrentChangeList()->commitChangesAndClear();
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");
433 _pSyncBarrier
->enter(_uiSyncCount
);
435 #ifdef OSG_GLOBAL_SYNC_LOCK
436 _pSyncLock
->acquire();
439 // _pSyncFromThread->getChangeList()->dump();
440 _pSyncFromThread
->getChangeList()->applyNoClear();
442 #ifdef OSG_GLOBAL_SYNC_LOCK
443 _pSyncLock
->release();
446 Thread::getCurrentChangeList()->commitChangesAndClear();
450 _pSyncBarrier
->enter(_uiSyncCount
);
454 _pSyncBarrier
->enter(_uiSyncCount
);
456 #ifdef OSG_GLOBAL_SYNC_LOCK
457 _pSyncLock
->acquire();
460 // _pSyncFromThread->getChangeList()->dump();
461 _pSyncFromThread
->getChangeList()->applyNoClear();
463 #ifdef OSG_GLOBAL_SYNC_LOCK
464 _pSyncLock
->release();
467 _pSyncBarrier
->enter(_uiSyncCount
);
471 _pSyncBarrier
->enter(_uiSyncCount
);
473 #ifdef OSG_GLOBAL_SYNC_LOCK
474 _pSyncLock
->acquire();
477 // _pSyncFromThread->getChangeList()->dump();
478 _pSyncFromThread
->getChangeList()->applyNoClear();
480 #ifdef OSG_GLOBAL_SYNC_LOCK
481 _pSyncLock
->release();
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
);
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();
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();
528 void CSMDrawer::setRunning(bool bVal
)
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();
552 /***************************************************************************\
554 \***************************************************************************/
556 MPThreadType
CSMDrawThread::_type(
559 static_cast<CreateThreadF
>(CSMDrawThread::create
),
562 /***************************************************************************\
564 \***************************************************************************/
566 /***************************************************************************\
568 \***************************************************************************/
570 /*-------------------------------------------------------------------------*\
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
,
588 return dynamic_pointer_cast
<CSMDrawThread
>(pThread
);
591 void CSMDrawThread::setDrawer(CSMDrawer
*pDrawer
)
596 void CSMDrawThread::setRunning(bool bVal
)
598 _pDrawer
->setRunning(bVal
);
602 BaseThread
*CSMDrawThread::create(const Char8
*szName
,
606 return new CSMDrawThread(szName
, uiId
, bGlobal
);
609 CSMDrawThread::CSMDrawThread(const Char8
*szName
, UInt32 uiId
, bool bGlobal
) :
617 CSMDrawThread::~CSMDrawThread(void)
621 void CSMDrawThread::workProc(void)
623 OSG_ASSERT(_pDrawer
!= NULL
);
625 _pDrawer
->runParallel();