3 OSG创建渲染线程的过程

OSG创建渲染线程的过程

 

图形渲染线程是静态函数,为:

static unsigned int __stdcall StartThread(void *data)

它是在int Thread::start()中被调用:

int Thread::start()

{

pd->tid.set( (void*)_beginthreadex(NULL,pd->stackSize,ThreadPrivateActions::StartThread,static_cast<void *>(this),0,&ID));

。。。

}

 3 OSG创建渲染线程的过程_第1张图片

int ViewerBase::run()

{

    if (!isRealized())

    {

        realize();

    }

  if( ){

        while (!done())

        {

            frame();

        }

    }

}

 

void Viewer::realize()

{

  。。

setUpViewAcrossAllScreens();

setUpThreading();

}

 

void ViewerBase::setUpThreading()

{

  。。

  startThreading();

}

 

void ViewerBase::startThreading()

{

switch(_threadingModel):

case(DrawThreadPerContext)://此处被执行

 

osg::ref_ptr<osg::BarrierOperation> swapReadyBarrier = contexts.empty() ? 0 : new osg::BarrierOperation(contexts.size(), osg::BarrierOperation::NO_OPERATION);

 

    osg::ref_ptr<osg::SwapBuffersOperation> swapOp = new osg::SwapBuffersOperation();

 

gc->createGraphicsThread();

 

gc->getGraphicsThread()->add(new osg::RunOperations());

 

gc->getGraphicsThread()->startThread();

}

----------

void GraphicsContext::createGraphicsThread()

{

    if (!_graphicsThread)//初始时,_graphicsThread= false;

    {

        setGraphicsThread(new GraphicsThread);//此行被执行

    }

}

void GraphicsContext::setGraphicsThread(GraphicsThread* gt)

{

_graphicsThread = gt;

。。。

    _graphicsThread->setParent(this);  

}

---------

int Thread::startThread()

{

    if (_prvData) return start();

    else return 0;

}

int Thread::start() {    pd->tid.set( (void*)_beginthreadex(NULL,pd->stackSize,ThreadPrivateActions::StartThread,static_cast<void *>(this),0,&ID));

 

pd->threadStartedBlock.block();/*主线程阻塞,启动图形渲染线程,static unsigned int __stdcall StartThread(void *data)函数被调用,而且StartThread ()函数中执行到pd->threadStartedBlock.release();后,切换到主线程*/

}

----------

void ViewerBase::frame(double simulationTime)

{

    advance(simulationTime);

   

    eventTraversal();

    updateTraversal();

    renderingTraversals();

}

void ViewerBase::renderingTraversals()

{  。。

    for(Cameras::iterator camItr = cameras.begin();

        camItr != cameras.end();        ++camItr)

    {      

        if (renderer)

        {

            if (!renderer->getGraphicsThreadDoesCull() && !(camera->getCameraThread()))

            {

                renderer->cull();//会被执行

            }

        }

}

 

if (!((*itr)->getGraphicsThread()) && (*itr)->valid())//测试时,没有被调用

    {   makeCurrent(*itr);//测试时,没有被调用

        (*itr)->runOperations();//测试时,没有被调用

    }

}

你可能感兴趣的:(3 OSG创建渲染线程的过程)