2013-12-18 10:41:09 +08:00
|
|
|
/****************************************************************************
|
2018-01-29 16:25:32 +08:00
|
|
|
Copyright (c) 2013-2016 Chukong Technologies Inc.
|
|
|
|
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
2013-12-18 10:41:09 +08:00
|
|
|
|
2022-08-08 18:02:17 +08:00
|
|
|
https://axys1.github.io/
|
2013-12-18 10:41:09 +08:00
|
|
|
|
|
|
|
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.
|
|
|
|
****************************************************************************/
|
|
|
|
|
2022-07-15 19:17:01 +08:00
|
|
|
#ifndef __AX_RENDERCOMMANDPOOL_H__
|
|
|
|
#define __AX_RENDERCOMMANDPOOL_H__
|
2015-03-24 20:23:51 +08:00
|
|
|
/// @cond DO_NOT_SHOW
|
2013-12-03 11:52:51 +08:00
|
|
|
|
|
|
|
#include <list>
|
2014-05-17 05:36:00 +08:00
|
|
|
|
2014-09-10 08:17:07 +08:00
|
|
|
#include "platform/CCPlatformMacros.h"
|
2014-05-17 05:36:00 +08:00
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
NS_AX_BEGIN
|
2013-12-03 11:52:51 +08:00
|
|
|
|
|
|
|
template <class T>
|
|
|
|
class RenderCommandPool
|
|
|
|
{
|
|
|
|
public:
|
2021-12-25 10:04:45 +08:00
|
|
|
RenderCommandPool() {}
|
2013-12-03 11:52:51 +08:00
|
|
|
~RenderCommandPool()
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
// if( 0 != _usedPool.size())
|
|
|
|
// {
|
2022-07-16 10:43:05 +08:00
|
|
|
// AXLOG("All RenderCommand should not be used when Pool is released!");
|
2021-12-25 10:04:45 +08:00
|
|
|
// }
|
2013-12-04 10:39:34 +08:00
|
|
|
_freePool.clear();
|
2022-07-21 19:19:08 +08:00
|
|
|
for (auto&& allocatedPoolBlock : _allocatedPoolBlocks)
|
2013-12-03 11:52:51 +08:00
|
|
|
{
|
2016-10-27 15:10:24 +08:00
|
|
|
delete[] allocatedPoolBlock;
|
|
|
|
allocatedPoolBlock = nullptr;
|
2013-12-03 11:52:51 +08:00
|
|
|
}
|
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;
|
2021-12-25 10:04:45 +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;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2013-12-03 11:52:51 +08:00
|
|
|
void pushBackCommand(T* ptr)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
// if(_usedPool.find(ptr) == _usedPool.end())
|
|
|
|
// {
|
2022-07-16 10:43:05 +08:00
|
|
|
// AXLOG("push Back Wrong command!");
|
2021-12-25 10:04:45 +08:00
|
|
|
// return;
|
|
|
|
// }
|
|
|
|
|
2022-08-08 13:18:33 +08:00
|
|
|
_freePool.emplace_back(ptr);
|
2013-12-03 11:52:51 +08:00
|
|
|
//_usedPool.erase(ptr);
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2013-12-03 11:52:51 +08:00
|
|
|
private:
|
2013-12-04 10:39:34 +08:00
|
|
|
void AllocateCommands()
|
|
|
|
{
|
|
|
|
static const int COMMANDS_ALLOCATE_BLOCK_SIZE = 32;
|
2021-12-25 10:04:45 +08:00
|
|
|
T* commands = new T[COMMANDS_ALLOCATE_BLOCK_SIZE];
|
2022-08-08 13:18:33 +08:00
|
|
|
_allocatedPoolBlocks.emplace_back(commands);
|
2021-12-25 10:04:45 +08:00
|
|
|
for (int index = 0; index < COMMANDS_ALLOCATE_BLOCK_SIZE; ++index)
|
2013-12-04 10:39:34 +08:00
|
|
|
{
|
2022-08-08 13:18:33 +08:00
|
|
|
_freePool.emplace_back(commands + index);
|
2013-12-04 10:39:34 +08:00
|
|
|
}
|
|
|
|
}
|
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;
|
2021-12-25 10:04:45 +08:00
|
|
|
// std::set<T*> _usedPool;
|
2013-12-03 11:52:51 +08:00
|
|
|
};
|
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
NS_AX_END
|
2013-12-03 11:52:51 +08:00
|
|
|
|
2015-03-24 20:23:51 +08:00
|
|
|
/// @endcond
|
2013-12-03 11:52:51 +08:00
|
|
|
#endif
|