2013-12-18 10:41:09 +08:00
|
|
|
/****************************************************************************
|
2014-01-07 11:25:07 +08:00
|
|
|
Copyright (c) 2013-2014 Chukong Technologies Inc.
|
2013-12-18 10:41:09 +08:00
|
|
|
|
|
|
|
http://www.cocos2d-x.org
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
THE SOFTWARE.
|
|
|
|
****************************************************************************/
|
|
|
|
|
2013-12-03 11:52:51 +08:00
|
|
|
|
|
|
|
#ifndef __CC_RENDERCOMMANDPOOL_H__
|
|
|
|
#define __CC_RENDERCOMMANDPOOL_H__
|
|
|
|
|
|
|
|
#include <set>
|
|
|
|
#include <list>
|
2014-05-17 05:36:00 +08:00
|
|
|
|
2014-04-27 01:11:22 +08:00
|
|
|
#include "base/CCPlatformMacros.h"
|
2014-05-17 05:36:00 +08:00
|
|
|
|
2013-12-03 11:52:51 +08:00
|
|
|
NS_CC_BEGIN
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
class RenderCommandPool
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
RenderCommandPool()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
~RenderCommandPool()
|
|
|
|
{
|
|
|
|
// if( 0 != _usedPool.size())
|
|
|
|
// {
|
|
|
|
// CCLOG("All RenderCommand should not be used when Pool is released!");
|
|
|
|
// }
|
2013-12-04 10:39:34 +08:00
|
|
|
_freePool.clear();
|
|
|
|
for (typename std::list<T*>::iterator iter = _allocatedPoolBlocks.begin(); iter != _allocatedPoolBlocks.end(); ++iter)
|
2013-12-03 11:52:51 +08:00
|
|
|
{
|
2013-12-04 10:39:34 +08:00
|
|
|
delete[] *iter;
|
2013-12-03 11:52:51 +08:00
|
|
|
*iter = nullptr;
|
|
|
|
}
|
2013-12-04 10:39:34 +08:00
|
|
|
_allocatedPoolBlocks.clear();
|
2013-12-03 11:52:51 +08:00
|
|
|
}
|
2013-12-18 10:41:09 +08:00
|
|
|
|
2013-12-03 11:52:51 +08:00
|
|
|
T* generateCommand()
|
|
|
|
{
|
|
|
|
T* result = nullptr;
|
2013-12-04 10:39:34 +08:00
|
|
|
if(_freePool.empty())
|
2013-12-03 11:52:51 +08:00
|
|
|
{
|
2013-12-04 10:39:34 +08:00
|
|
|
AllocateCommands();
|
2013-12-03 11:52:51 +08:00
|
|
|
}
|
2013-12-04 10:39:34 +08:00
|
|
|
result = _freePool.front();
|
|
|
|
_freePool.pop_front();
|
2013-12-03 11:52:51 +08:00
|
|
|
//_usedPool.insert(result);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
void pushBackCommand(T* ptr)
|
|
|
|
{
|
|
|
|
// if(_usedPool.find(ptr) == _usedPool.end())
|
|
|
|
// {
|
|
|
|
// CCLOG("push Back Wrong command!");
|
|
|
|
// return;
|
|
|
|
// }
|
|
|
|
|
|
|
|
_freePool.push_back(ptr);
|
|
|
|
//_usedPool.erase(ptr);
|
|
|
|
|
|
|
|
}
|
|
|
|
private:
|
2013-12-04 10:39:34 +08:00
|
|
|
void AllocateCommands()
|
|
|
|
{
|
|
|
|
static const int COMMANDS_ALLOCATE_BLOCK_SIZE = 32;
|
2014-08-28 07:31:57 +08:00
|
|
|
T* commands = new (std::nothrow) T[COMMANDS_ALLOCATE_BLOCK_SIZE];
|
2013-12-04 10:39:34 +08:00
|
|
|
_allocatedPoolBlocks.push_back(commands);
|
|
|
|
for(int index = 0; index < COMMANDS_ALLOCATE_BLOCK_SIZE; ++index)
|
|
|
|
{
|
|
|
|
_freePool.push_back(commands+index);
|
|
|
|
}
|
|
|
|
}
|
2013-12-18 10:41:09 +08:00
|
|
|
|
2013-12-04 10:39:34 +08:00
|
|
|
std::list<T*> _allocatedPoolBlocks;
|
2013-12-03 11:52:51 +08:00
|
|
|
std::list<T*> _freePool;
|
|
|
|
//std::set<T*> _usedPool;
|
|
|
|
};
|
|
|
|
|
|
|
|
NS_CC_END
|
|
|
|
|
|
|
|
#endif
|