add helper funcion to invoke a function in gl thread

This commit is contained in:
minggo 2013-11-27 16:49:07 +08:00
parent 6d8b7fb494
commit bcb131262c
6 changed files with 132 additions and 28 deletions

View File

@ -1026,9 +1026,6 @@ void DisplayLinkDirector::startAnimation()
}
_invalid = false;
#ifndef EMSCRIPTEN
Application::getInstance()->setAnimationInterval(_animationInterval);
#endif // EMSCRIPTEN
}
void DisplayLinkDirector::mainLoop()
@ -1040,6 +1037,9 @@ void DisplayLinkDirector::mainLoop()
}
else if (! _invalid)
{
// invoke call back from other thread
ThreadHelper::doCallback();
drawScene();
// release the objects

View File

@ -163,9 +163,8 @@ void TextureCache::loadImage()
while (true)
{
// create autorelease pool for iOS
Thread thread;
thread.createAutoreleasePool();
auto autolreasePool = ThreadHelper::createAutoreleasePool();
std::queue<AsyncStruct*> *pQueue = _asyncStructQueue;
_asyncStructQueueMutex.lock();
if (pQueue->empty())
@ -207,6 +206,8 @@ void TextureCache::loadImage()
_imageInfoMutex.lock();
_imageInfoQueue->push(imageInfo);
_imageInfoMutex.unlock();
ThreadHelper::releaseAutoreleasePool(autolreasePool);
}
if(_asyncStructQueue != nullptr)

View File

@ -24,23 +24,62 @@ THE SOFTWARE.
#include "CCThread.h"
NS_CC_BEGIN
// iOS and Mac already has a Thread.mm
#if (CC_TARGET_PLATFORM != CC_PLATFORM_IOS && CC_TARGET_PLATFORM != CC_PLATFORM_MAC)
NS_CC_BEGIN
std::list<std::function<void(void)>>* ThreadHelper::_callbackList = new std::list<std::function<void(void)>>();
std::mutex* ThreadHelper::_mutex = new std::mutex;
long ThreadHelper::_callbackNumberPerFrame = 5;
Thread::~Thread()
void* ThreadHelper::createAutoreleasePool()
{
// To prevent warning: private field '_autoreasePool' is not
// used [-Wunused-private-field] by CLANG.
_autoReleasePool = nullptr;
return nullptr;
}
void Thread::createAutoreleasePool()
void ThreadHelper::releaseAutoreleasePool(void* autoreleasePool)
{
}
NS_CC_END
void ThreadHelper::runOnGLThread(std::function<void(void)> f)
{
// Insert call back function
_mutex->lock();
_callbackList->push_back(f);
_mutex->unlock();
}
void ThreadHelper::doCallback()
{
_mutex->lock();
auto iter = _callbackList->begin();
long i = 0;
while (iter != _callbackList->end())
{
auto f = *iter;
f();
++i;
if (i >= _callbackNumberPerFrame)
{
break;
}
else
{
iter = _callbackList->erase(iter);
}
}
_mutex->unlock();
}
void ThreadHelper::setCallbackNumberPerFrame(long callbackNumberPerFrame)
{
_callbackNumberPerFrame = callbackNumberPerFrame;
}
#endif
NS_CC_END

View File

@ -25,8 +25,12 @@ THE SOFTWARE.
#ifndef __CC_PLATFORM_THREAD_H__
#define __CC_PLATFORM_THREAD_H__
#include <functional>
#include <list>
#include <mutex>
#include "platform/CCCommon.h"
#include "CCPlatformMacros.h"
#include "CCDirector.h"
NS_CC_BEGIN
@ -39,27 +43,45 @@ NS_CC_BEGIN
* and release it when the thread end.
*/
class CC_DLL Thread
class CC_DLL ThreadHelper
{
public:
/**
friend DisplayLinkDirector;
/** Create an autorelease pool for objective-c codes.
* @js NA
* @lua NA
*/
Thread() : _autoReleasePool(nullptr) {}
static void* createAutoreleasePool();
/**
* @js NA
* @lua NA
*/
~Thread();
/**
*/
static void releaseAutoreleasePool(void *autoreleasePool);
/** To run a function in gl thread.
* @js NA
* @lua NA
@since v3.0
*/
void createAutoreleasePool();
static void runOnGLThread(std::function<void(void)> f);
/** Set how many callback functions being invoked per frame. Default value is 5.
* @js NA
* @lua NA
@since v3.0
*/
static void setCallbackNumberPerFrame(long callbackNumberPerFrame);
private:
void *_autoReleasePool;
// This function will be call by Director to call some call back function on gl thread
static void doCallback();
static std::list<std::function<void(void)>> *_callbackList;
static std::mutex *_mutex;
// How many callback functions invoked per frame
static long _callbackNumberPerFrame;
};
// end of platform group

View File

@ -26,14 +26,55 @@ THE SOFTWARE.
NS_CC_BEGIN
Thread::~Thread()
std::list<std::function<void(void)>>* ThreadHelper::_callbackList = new std::list<std::function<void(void)>>();
std::mutex* ThreadHelper::_mutex = new std::mutex;
long ThreadHelper::_callbackNumberPerFrame = 5;
void* ThreadHelper::createAutoreleasePool()
{
[(id)_autoReleasePool release];
id pool = [[NSAutoreleasePool alloc] init];
return pool;
}
void Thread::createAutoreleasePool()
void ThreadHelper::releaseAutoreleasePool(void *autoreleasePool)
{
_autoReleasePool = [[NSAutoreleasePool alloc] init];
[(NSAutoreleasePool*)autoreleasePool release];
}
void ThreadHelper::runOnGLThread(std::function<void(void)> f)
{
// Insert call back function
_mutex->lock();
_callbackList->push_back(f);
_mutex->unlock();
}
void ThreadHelper::doCallback()
{
_mutex->lock();
auto iter = _callbackList->begin();
long i = 0;
while (iter != _callbackList->end())
{
auto f = *iter;
f();
++i;
if (i >= _callbackNumberPerFrame)
{
break;
}
else
{
iter = _callbackList->erase(iter);
}
}
_mutex->unlock();
}
void ThreadHelper::setCallbackNumberPerFrame(long callbackNumberPerFrame)
{
_callbackNumberPerFrame = callbackNumberPerFrame;
}
NS_CC_END

View File

@ -155,8 +155,7 @@ void DataReaderHelper::loadData()
while (true)
{
// create autorelease pool for iOS
Thread thread;
thread.createAutoreleasePool();
auto autoreleasePool = ThreadHelper::createAutoreleasePool();
std::queue<AsyncStruct *> *pQueue = _asyncStructQueue;
_asyncStructQueueMutex.lock(); // get async struct from queue
@ -200,6 +199,8 @@ void DataReaderHelper::loadData()
_dataInfoMutex.lock();
_dataQueue->push(pDataInfo);
_dataInfoMutex.unlock();
ThreadHelper::releaseAutoreleasePool(autoreleasePool);
}
if( _asyncStructQueue != nullptr )