/
pipe.cpp
1397 lines (1146 loc) · 37.5 KB
/
pipe.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Copyright (c) 2005-2015, Stefan Eilemann <eile@equalizergraphics.com>
* Daniel Nachbaur <danielnachbaur@gmail.com>
* Cedric Stalder <cedric.stalder@gmail.com>
*
* This library is free software; you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License version 2.1 as published
* by the Free Software Foundation.
*
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "pipe.h"
#include "channel.h"
#include "client.h"
#include "config.h"
#include "exception.h"
#include "frame.h"
#include "frameData.h"
#include "global.h"
#include "log.h"
#include "node.h"
#include "nodeFactory.h"
#include "pipeStatistics.h"
#include "server.h"
#include "view.h"
#include "window.h"
#include "messagePump.h"
#include "systemPipe.h"
#include "computeContext.h"
#ifdef EQUALIZER_USE_CUDA
# include "cudaContext.h"
#endif
#include <eq/fabric/commands.h>
#include <eq/fabric/elementVisitor.h>
#include <eq/fabric/leafVisitor.h>
#include <eq/fabric/task.h>
#include <co/global.h>
#include <co/objectICommand.h>
#include <co/queueSlave.h>
#include <co/worker.h>
#include <boost/lexical_cast.hpp>
#include <sstream>
#ifdef EQUALIZER_USE_HWLOC_GL
# include <hwloc.h>
# include <hwloc/gl.h>
#endif
#ifdef EQUALIZER_USE_QT5WIDGETS
# include <QGuiApplication>
# include <QRegularExpression>
#endif
namespace eq
{
/** @cond IGNORE */
typedef fabric::Pipe< Node, Pipe, Window, PipeVisitor > Super;
typedef co::CommandFunc<Pipe> PipeFunc;
/** @endcond */
namespace
{
enum State
{
STATE_MAPPED,
STATE_INITIALIZING,
STATE_RUNNING,
STATE_STOPPING, // must come after running
STATE_STOPPED, // must come after running
STATE_FAILED
};
typedef stde::hash_map< uint128_t, Frame* > FrameHash;
typedef stde::hash_map< uint128_t, FrameDataPtr > FrameDataHash;
typedef stde::hash_map< uint128_t, View* > ViewHash;
typedef stde::hash_map< uint128_t, co::QueueSlave* > QueueHash;
typedef FrameHash::const_iterator FrameHashCIter;
typedef FrameDataHash::const_iterator FrameDataHashCIter;
typedef ViewHash::const_iterator ViewHashCIter;
typedef ViewHash::iterator ViewHashIter;
typedef QueueHash::const_iterator QueueHashCIter;
}
namespace detail
{
class RenderThread : public eq::Worker
{
public:
explicit RenderThread( eq::Pipe* pipe )
: eq::Worker( co::Global::getCommandQueueLimit( ))
, _pipe( pipe )
{}
protected:
bool init() override
{
setName( std::string( "Draw" ) +
boost::lexical_cast< std::string >( _pipe->getPath().pipeIndex ));
return true;
}
void run() override;
bool stopRunning() override { return !_pipe; }
private:
eq::Pipe* _pipe;
friend class eq::Pipe;
};
/** Asynchronous, per-pipe readback thread. */
class TransferThread : public co::Worker
{
public:
explicit TransferThread( const uint32_t index )
: co::Worker( co::Global::getCommandQueueLimit( ))
, _index( index )
, _stop( false )
{}
bool init() override
{
if( !co::Worker::init( ))
return false;
setName( std::string( "Tfer" ) +
boost::lexical_cast< std::string >( _index ));
return true;
}
bool stopRunning() override { return _stop; }
void postStop() { _stop = true; }
private:
uint32_t _index;
bool _stop; // thread will exit if this is true
};
class Pipe
{
public:
explicit Pipe( const uint32_t index )
: systemPipe( 0 )
#ifdef AGL
, windowSystem( "AGL" )
#elif GLX
, windowSystem( "GLX" )
#elif WGL
, windowSystem( "WGL" )
#elif EQ_QT_USED
, windowSystem( "Qt" )
#endif
, state( STATE_STOPPED )
, currentFrame( 0 )
, frameTime( 0 )
, thread( 0 )
, transferThread( index )
, computeContext( 0 )
{}
~Pipe()
{
delete thread;
thread = 0;
}
/** Window-system specific functions class */
SystemPipe* systemPipe;
/** The current window system. */
WindowSystem windowSystem;
/** The configInit/configExit state. */
lunchbox::Monitor< State > state;
/** The last started frame. */
uint32_t currentFrame;
/** The number of the last finished frame. */
lunchbox::Monitor< uint32_t > finishedFrame;
/** The number of the last locally unlocked frame. */
lunchbox::Monitor< uint32_t > unlockedFrame;
/** The running per-frame statistic clocks. */
std::deque< int64_t > frameTimes;
lunchbox::Lock frameTimeMutex;
/** The base time for the currently active frame. */
int64_t frameTime;
/** All assembly frames used by the pipe during rendering. */
FrameHash frames;
/** All output frame datas used by the pipe during rendering. */
FrameDataHash outputFrameDatas;
/** All input frame datas used by the pipe during rendering. */
FrameDataHash inputFrameDatas;
/** All views used by the pipe's channels during rendering. */
ViewHash views;
/** All queues used by the pipe's channels during rendering. */
QueueHash queues;
/** The pipe thread. */
RenderThread* thread;
detail::TransferThread transferThread;
/** GPU Computing context */
ComputeContext *computeContext;
};
void RenderThread::run()
{
LB_TS_THREAD( _pipe->_pipeThread );
LBDEBUG << "Entered pipe thread" << std::endl;
eq::Pipe* pipe = _pipe; // _pipe gets cleared on exit
pipe->_impl->state.waitEQ( STATE_MAPPED );
pipe->_impl->windowSystem = pipe->selectWindowSystem();
pipe->_setupCommandQueue();
pipe->_setupAffinity();
Worker::run();
pipe->_exitCommandQueue();
}
}
Pipe::Pipe( Node* parent )
: Super( parent )
, _impl( new detail::Pipe( getPath().pipeIndex ))
{
}
Pipe::~Pipe()
{
LBASSERT( getWindows().empty( ));
delete _impl;
}
Config* Pipe::getConfig()
{
Node* node = getNode();
LBASSERT( node );
return ( node ? node->getConfig() : 0);
}
const Config* Pipe::getConfig() const
{
const Node* node = getNode();
LBASSERT( node );
return ( node ? node->getConfig() : 0);
}
ClientPtr Pipe::getClient()
{
Node* node = getNode();
LBASSERT( node );
return ( node ? node->getClient() : 0);
}
ServerPtr Pipe::getServer()
{
Node* node = getNode();
LBASSERT( node );
return ( node ? node->getServer() : 0);
}
void Pipe::attach( const uint128_t& id, const uint32_t instanceID )
{
Super::attach( id, instanceID );
co::CommandQueue* queue = getPipeThreadQueue();
co::CommandQueue* transferQ = getTransferThreadQueue();
registerCommand( fabric::CMD_PIPE_CONFIG_INIT,
PipeFunc( this, &Pipe::_cmdConfigInit ), queue );
registerCommand( fabric::CMD_PIPE_CONFIG_EXIT,
PipeFunc( this, &Pipe::_cmdConfigExit ), queue );
registerCommand( fabric::CMD_PIPE_CREATE_WINDOW,
PipeFunc( this, &Pipe::_cmdCreateWindow ), queue );
registerCommand( fabric::CMD_PIPE_DESTROY_WINDOW,
PipeFunc( this, &Pipe::_cmdDestroyWindow ), queue );
registerCommand( fabric::CMD_PIPE_FRAME_START,
PipeFunc( this, &Pipe::_cmdFrameStart ), queue );
registerCommand( fabric::CMD_PIPE_FRAME_FINISH,
PipeFunc( this, &Pipe::_cmdFrameFinish ), queue );
registerCommand( fabric::CMD_PIPE_FRAME_DRAW_FINISH,
PipeFunc( this, &Pipe::_cmdFrameDrawFinish ), queue );
registerCommand( fabric::CMD_PIPE_FRAME_START_CLOCK,
PipeFunc( this, &Pipe::_cmdFrameStartClock ), 0 );
registerCommand( fabric::CMD_PIPE_EXIT_THREAD,
PipeFunc( this, &Pipe::_cmdExitThread ), queue );
registerCommand( fabric::CMD_PIPE_DETACH_VIEW,
PipeFunc( this, &Pipe::_cmdDetachView ), queue );
registerCommand( fabric::CMD_PIPE_EXIT_TRANSFER_THREAD,
PipeFunc( this, &Pipe::_cmdExitTransferThread ),
transferQ );
}
void Pipe::setDirty( const uint64_t bits )
{
// jump over fabric setDirty to avoid dirty'ing node pipes list
// pipes are individually synced in frame finish for thread-safety
Object::setDirty( bits );
}
bool Pipe::isWindowSystemAvailable( const std::string& name ) const
{
bool available = false;
if( name != "Qt" )
{
#ifdef AGL
available = name == "AGL";
#elif GLX
available = name == "GLX";
#elif WGL
available = name == "WGL";
#endif
}
#ifdef EQ_QT_USED
if( name != "Qt" )
return available;
// Qt can only use ports that refer to QScreens available to the
// QApplication. In Windows and Mac all physical displays are
// queriable using platform dependent functions (no idea about virtual
// displays like Xming or XQuartz), so we will assume that if Equalizer
// was built with Qt support, then all devices can be used by Qt.
// (How to choose the right screen for a given device it's a different
// story)
# ifdef AGL
available = true;
# elif WGL
available = true;
# else
// For X it's different. Qt can only use screens that are part of the
// display server referred by the DISPLAY environmental variable (in
// particular its value at the moment of the creation of the QApplication)
# ifdef __APPLE__
// In MAC this is simpler, because there can only be one display server.
return true;
# else
// There's no way to infer the X server number from the Qt API except
// QScreen names, but only under certain conditions. In regular desktop
// usage QScreen::name gives a name related to what xrandr prints, so it's
// not usable. The names seems to follow the X naming convention
// (e.g. host:0.0) when XRANDR is not available (e.g. Xnest) or doesn't
// provide information about where the displays are connected (e.g.
// headless or Xvnc). These are corner cases so we cannot rely on QScreen.
// Therefore, we will infer which port is Qt using directly from DISPLAY.
// Is the port is undefined that means that the default server must
// be used. This should match the Qt display server.
if( getPort() == LB_UNDEFINED_UINT32 )
return true;
QGuiApplication* app =
dynamic_cast< QGuiApplication* >( QCoreApplication::instance( ));
if( !app || !app->primaryScreen() )
return false; // Qt won't be able to access anything anyway
QRegularExpression regex( "^[a-z]*\\:([0-9]+)(\\.[0-9]+)?$" );
QRegularExpressionMatch match =
regex.match( getenv( "DISPLAY" ) ? getenv( "DISPLAY" ) : "" );
available = match.captured( 1 ) == QString::number( getPort(), 10 );
# endif
# endif
#endif
return available;
}
WindowSystem Pipe::selectWindowSystem() const
{
#ifdef AGL
return WindowSystem( "AGL" );
#elif GLX
return WindowSystem( "GLX" );
#elif WGL
return WindowSystem( "WGL" );
#elif EQ_QT_USED
if( !pipe.isWindowSystemAvailable( "Qt" ))
{
// Throwing because there's no reasonable alternative.
std::stringstream msg;
msg << "Cannot choose windowing system for pipe at port " << getPort()
<< ". Qt was set as the default, but it cannot be used. In X"
" based systems this means that the value of DISPLAY taken by"
" Qt refers to a different display server." << std::endl;
LBTHROW( std::runtime_error( msg.str( )));
}
return WindowSystem( "Qt" );
#endif
}
void Pipe::_setupCommandQueue()
{
LBDEBUG << "Set up pipe message pump for " << _impl->windowSystem
<< std::endl;
Config* config = getConfig();
config->setupMessagePump( this );
if( !_impl->thread ) // Non-threaded pipes have no pipe thread message pump
return;
CommandQueue* queue = _impl->thread->getWorkerQueue();
LBASSERT( queue );
LBASSERT( !queue->getMessagePump( ));
Global::enterCarbon();
MessagePump* pump = createMessagePump();
if( pump )
pump->dispatchAll(); // initializes _impl->receiverQueue
queue->setMessagePump( pump );
Global::leaveCarbon();
}
int32_t Pipe::_getAutoAffinity() const
{
#ifdef EQUALIZER_USE_HWLOC_GL
uint32_t port = getPort();
uint32_t device = getDevice();
if( port == LB_UNDEFINED_UINT32 && device == LB_UNDEFINED_UINT32 )
return lunchbox::Thread::NONE;
if( port == LB_UNDEFINED_UINT32 )
port = 0;
if( device == LB_UNDEFINED_UINT32 )
device = 0;
hwloc_topology_t topology;
if( hwloc_topology_init( &topology ) < 0 )
{
LBINFO << "Automatic pipe thread placement failed: "
<< "hwloc_topology_init() failed" << std::endl;
return lunchbox::Thread::NONE;
}
// Load I/O devices, bridges and their relevant info
const unsigned long loading_flags = HWLOC_TOPOLOGY_FLAG_IO_BRIDGES |
HWLOC_TOPOLOGY_FLAG_IO_DEVICES;
if( hwloc_topology_set_flags( topology, loading_flags ) < 0 )
{
LBINFO << "Automatic pipe thread placement failed: "
<< "hwloc_topology_set_flags() failed" << std::endl;
hwloc_topology_destroy( topology );
return lunchbox::Thread::NONE;
}
if( hwloc_topology_load( topology ) < 0 )
{
LBINFO << "Automatic pipe thread placement failed: "
<< "hwloc_topology_load() failed" << std::endl;
hwloc_topology_destroy( topology );
return lunchbox::Thread::NONE;
}
const hwloc_obj_t osdev =
hwloc_gl_get_display_osdev_by_port_device( topology,
int( port ), int( device ));
if( !osdev )
{
LBINFO << "Automatic pipe thread placement failed: GPU not found"
<< std::endl;
hwloc_topology_destroy( topology );
return lunchbox::Thread::NONE;
}
const hwloc_obj_t pcidev = osdev->parent;
const hwloc_obj_t parent = hwloc_get_non_io_ancestor_obj( topology, pcidev);
const int numCpus =
hwloc_get_nbobjs_inside_cpuset_by_type( topology, parent->cpuset,
HWLOC_OBJ_SOCKET );
if( numCpus != 1 )
{
LBINFO << "Automatic pipe thread placement failed: GPU attached to "
<< numCpus << " processors?" << std::endl;
hwloc_topology_destroy( topology );
return lunchbox::Thread::NONE;
}
const hwloc_obj_t cpuObj =
hwloc_get_obj_inside_cpuset_by_type( topology, parent->cpuset,
HWLOC_OBJ_SOCKET, 0 );
if( cpuObj == 0 )
{
LBINFO << "Automatic pipe thread placement failed: "
<< "hwloc_get_obj_inside_cpuset_by_type() failed" << std::endl;
hwloc_topology_destroy( topology );
return lunchbox::Thread::NONE;
}
const int cpuIndex = cpuObj->logical_index;
hwloc_topology_destroy( topology );
return cpuIndex + lunchbox::Thread::SOCKET;
#else
LBDEBUG << "Automatic thread placement not supported, no hwloc GL support"
<< std::endl;
#endif
return lunchbox::Thread::NONE;
}
void Pipe::_setupAffinity()
{
const int32_t affinity = getIAttribute( IATTR_HINT_AFFINITY );
switch( affinity )
{
case AUTO:
lunchbox::Thread::setAffinity( _getAutoAffinity( ));
break;
case OFF:
default:
lunchbox::Thread::setAffinity( affinity );
break;
}
}
void Pipe::_exitCommandQueue()
{
// Non-threaded pipes have no pipe thread message pump
if( !_impl->thread )
return;
CommandQueue* queue = _impl->thread->getWorkerQueue();
LBASSERT( queue );
MessagePump* pump = queue->getMessagePump();
queue->setMessagePump( 0 );
delete pump;
}
MessagePump* Pipe::createMessagePump()
{
return _impl->windowSystem.createMessagePump();
}
MessagePump* Pipe::getMessagePump()
{
LB_TS_THREAD( _pipeThread );
if( !_impl->thread )
return 0;
CommandQueue* queue = _impl->thread->getWorkerQueue();
return queue->getMessagePump();
}
co::CommandQueue* Pipe::getPipeThreadQueue()
{
if( _impl->thread )
return _impl->thread->getWorkerQueue();
return getNode()->getMainThreadQueue();
}
co::CommandQueue* Pipe::getTransferThreadQueue()
{
return _impl->transferThread.getWorkerQueue();
}
co::CommandQueue* Pipe::getMainThreadQueue()
{
return getServer()->getMainThreadQueue();
}
co::CommandQueue* Pipe::getCommandThreadQueue()
{
return getServer()->getCommandThreadQueue();
}
Frame* Pipe::getFrame( const co::ObjectVersion& frameVersion, const Eye eye,
const bool isOutput )
{
LB_TS_THREAD( _pipeThread );
Frame* frame = _impl->frames[ frameVersion.identifier ];
if( !frame )
{
ClientPtr client = getClient();
frame = new Frame();
LBCHECK( client->mapObject( frame, frameVersion ));
_impl->frames[ frameVersion.identifier ] = frame;
}
else
frame->sync( frameVersion.version );
const co::ObjectVersion& dataVersion = frame->getDataVersion( eye );
LBLOG( LOG_ASSEMBLY ) << "Use " << dataVersion << std::endl;
FrameDataPtr frameData = getNode()->getFrameData( dataVersion );
LBASSERT( frameData );
if( isOutput )
{
if( !frameData->isAttached() )
{
ClientPtr client = getClient();
LBCHECK( client->mapObject( frameData.get(), dataVersion ));
}
else if( frameData->getVersion() < dataVersion.version )
frameData->sync( dataVersion.version );
_impl->outputFrameDatas[ dataVersion.identifier ] = frameData;
}
else
_impl->inputFrameDatas[ dataVersion.identifier ] = frameData;
frame->setFrameData( frameData );
return frame;
}
void Pipe::flushFrames( util::ObjectManager& om )
{
LB_TS_THREAD( _pipeThread );
ClientPtr client = getClient();
for( FrameHashCIter i = _impl->frames.begin(); i !=_impl->frames.end(); ++i)
{
Frame* frame = i->second;
frame->setFrameData( 0 ); // datas are flushed below
client->unmapObject( frame );
delete frame;
}
_impl->frames.clear();
for( FrameDataHashCIter i = _impl->inputFrameDatas.begin();
i != _impl->inputFrameDatas.end(); ++i )
{
FrameDataPtr data = i->second;
data->deleteGLObjects( om );
}
_impl->inputFrameDatas.clear();
for( FrameDataHashCIter i = _impl->outputFrameDatas.begin();
i != _impl->outputFrameDatas.end(); ++i )
{
FrameDataPtr data = i->second;
data->resetPlugins();
data->deleteGLObjects( om );
client->unmapObject( data.get( ));
getNode()->releaseFrameData( data );
}
_impl->outputFrameDatas.clear();
}
co::QueueSlave* Pipe::getQueue( const uint128_t& queueID )
{
LB_TS_THREAD( _pipeThread );
if( queueID == 0 )
return 0;
co::QueueSlave* queue = _impl->queues[ queueID ];
if( !queue )
{
queue = new co::QueueSlave;
ClientPtr client = getClient();
LBCHECK( client->mapObject( queue, queueID ));
_impl->queues[ queueID ] = queue;
}
return queue;
}
void Pipe::_flushQueues()
{
LB_TS_THREAD( _pipeThread );
ClientPtr client = getClient();
for( QueueHashCIter i = _impl->queues.begin(); i !=_impl->queues.end(); ++i)
{
co::QueueSlave* queue = i->second;
client->unmapObject( queue );
delete queue;
}
_impl->queues.clear();
}
const View* Pipe::getView( const co::ObjectVersion& viewVersion ) const
{
// Yie-ha: we want to have a const-interface to get a view on the render
// clients, but view mapping is by definition non-const.
return const_cast< Pipe* >( this )->getView( viewVersion );
}
View* Pipe::getView( const co::ObjectVersion& viewVersion )
{
LB_TS_THREAD( _pipeThread );
if( viewVersion.identifier == 0 )
return 0;
View* view = _impl->views[ viewVersion.identifier ];
if( !view )
{
NodeFactory* nodeFactory = Global::getNodeFactory();
view = nodeFactory->createView( 0 );
LBASSERT( view );
view->_pipe = this;
ClientPtr client = getClient();
LBCHECK( client->mapObject( view, viewVersion ));
_impl->views[ viewVersion.identifier ] = view;
}
view->sync( viewVersion.version );
return view;
}
void Pipe::_releaseViews()
{
LB_TS_THREAD( _pipeThread );
for( bool changed = true; changed; )
{
changed = false;
for( ViewHashIter i =_impl->views.begin(); i !=_impl->views.end(); ++i )
{
View* view = i->second;
view->commit();
if( view->getVersion() + 20 > view->getHeadVersion( ))
continue;
// release unused view to avoid memory leaks due to deltas piling up
view->_pipe = 0;
ClientPtr client = getClient();
client->unmapObject( view );
_impl->views.erase( i );
NodeFactory* nodeFactory = Global::getNodeFactory();
nodeFactory->releaseView( view );
changed = true;
break;
}
}
}
void Pipe::_flushViews()
{
LB_TS_THREAD( _pipeThread );
NodeFactory* nodeFactory = Global::getNodeFactory();
ClientPtr client = getClient();
for( ViewHashCIter i = _impl->views.begin(); i != _impl->views.end(); ++i )
{
View* view = i->second;
client->unmapObject( view );
view->_pipe = 0;
nodeFactory->releaseView( view );
}
_impl->views.clear();
}
void Pipe::startThread()
{
_impl->thread = new detail::RenderThread( this );
_impl->thread->start();
}
void Pipe::exitThread()
{
_stopTransferThread();
if( !_impl->thread )
return;
send( getLocalNode(), fabric::CMD_PIPE_EXIT_THREAD );
_impl->thread->join();
delete _impl->thread;
_impl->thread = 0;
}
void Pipe::cancelThread()
{
_stopTransferThread();
if( !_impl->thread )
return;
// local command dispatching
co::ObjectOCommand( this, getLocalNode(), fabric::CMD_PIPE_EXIT_THREAD,
co::COMMANDTYPE_OBJECT, getID(), CO_INSTANCE_ALL );
}
void Pipe::waitExited() const
{
_impl->state.waitGE( STATE_STOPPED );
}
bool Pipe::isRunning() const
{
return (_impl->state == STATE_RUNNING);
}
bool Pipe::isStopped() const
{
return (_impl->state == STATE_STOPPED);
}
void Pipe::notifyMapped()
{
LBASSERT( _impl->state == STATE_STOPPED );
_impl->state = STATE_MAPPED;
}
namespace
{
class WaitFinishedVisitor : public PipeVisitor
{
public:
WaitFinishedVisitor( const uint32_t frame, MessagePump* pump )
: _frame( frame ), _pump( pump ) {}
virtual VisitorResult visit( Channel* channel )
{
while( !channel->waitFrameFinished( _frame, 100 ))
{
// process potential pending Qt slots
if( _pump )
_pump->dispatchAll();
}
return TRAVERSE_CONTINUE;
}
private:
const uint32_t _frame;
MessagePump* _pump;
};
}
void Pipe::waitFrameFinished( const uint32_t frameNumber )
{
MessagePump* pump = getConfig()->getMessagePump();
while( !_impl->finishedFrame.timedWaitGE( frameNumber, 100 ))
{
// process potential pending Qt slots
if( pump )
pump->dispatchAll();
}
WaitFinishedVisitor waiter( frameNumber, pump );
accept( waiter );
}
void Pipe::waitFrameLocal( const uint32_t frameNumber ) const
{
_impl->unlockedFrame.waitGE( frameNumber );
}
uint32_t Pipe::getCurrentFrame() const
{
LB_TS_THREAD( _pipeThread );
return _impl->currentFrame;
}
uint32_t Pipe::getFinishedFrame() const
{
return _impl->finishedFrame.get();
}
WindowSystem Pipe::getWindowSystem() const
{
return _impl->windowSystem;
}
EventOCommand Pipe::sendError( const uint32_t error )
{
return getConfig()->sendError( Event::PIPE_ERROR, Error( error, getID( )));
}
bool Pipe::processEvent( const Event& event )
{
ConfigEvent configEvent( event );
getConfig()->sendEvent( configEvent );
return true;
}
//---------------------------------------------------------------------------
// pipe-thread methods
//---------------------------------------------------------------------------
bool Pipe::configInit( const uint128_t& initID )
{
LB_TS_THREAD( _pipeThread );
LBASSERT( !_impl->systemPipe );
if ( !configInitSystemPipe( initID ))
return false;
// -------------------------------------------------------------------------
LBASSERT(!_impl->computeContext);
// for now we only support CUDA
#ifdef EQUALIZER_USE_CUDA
if( getIAttribute( IATTR_HINT_CUDA_GL_INTEROP ) == eq::ON )
{
LBDEBUG << "Initializing CUDAContext" << std::endl;
ComputeContext* computeCtx = new CUDAContext( this );
if( !computeCtx->configInit() )
{
LBWARN << "GPU Computing context initialization failed "
<< std::endl;
delete computeCtx;
return false;
}
setComputeContext( computeCtx );
}
#endif
return true;
}
bool Pipe::configInitSystemPipe( const uint128_t& )
{
SystemPipe* systemPipe = _impl->windowSystem.createPipe( this );
LBASSERT( systemPipe );
if( !systemPipe->configInit( ))
{
LBERROR << "System pipe context initialization failed" << std::endl;
delete systemPipe;
return false;
}
setSystemPipe( systemPipe );
return true;
}
bool Pipe::configExit()
{
LB_TS_THREAD( _pipeThread );
if( _impl->computeContext )
{
_impl->computeContext->configExit();
delete _impl->computeContext;
_impl->computeContext = 0;
}
if( _impl->systemPipe )
{
_impl->systemPipe->configExit( );
delete _impl->systemPipe;
_impl->systemPipe = 0;
}
return true;
}
void Pipe::frameStart( const uint128_t&, const uint32_t frameNumber )
{
LB_TS_THREAD( _pipeThread );
const Node* node = getNode();
switch( node->getIAttribute( Node::IATTR_THREAD_MODEL ))
{
case ASYNC: // No sync, release immediately
releaseFrameLocal( frameNumber );
break;
case DRAW_SYNC: // Sync, release in frameDrawFinish
case LOCAL_SYNC: // Sync, release in frameFinish
node->waitFrameStarted( frameNumber );
break;
default:
LBUNIMPLEMENTED;
}
startFrame( frameNumber );
}
void Pipe::frameDrawFinish( const uint128_t&, const uint32_t frameNumber )
{