diff --git a/cocos/2d/CCLayer.cpp b/cocos/2d/CCLayer.cpp index 19892ac30e..0878910518 100644 --- a/cocos/2d/CCLayer.cpp +++ b/cocos/2d/CCLayer.cpp @@ -695,7 +695,7 @@ void LayerColor::updateColor() void LayerColor::draw() { kmGLGetMatrix(KM_GL_MODELVIEW, &_transformMatrix); - CustomCommand* cmd = new CustomCommand(); + CustomCommand* cmd = CustomCommand::getCommandPool().generateCommand(); cmd->init(0, _vertexZ); cmd->func = CC_CALLBACK_0(LayerColor::onDraw, this); Renderer::getInstance()->addCommand(cmd); diff --git a/cocos/2d/CCParticleSystemQuad.cpp b/cocos/2d/CCParticleSystemQuad.cpp index a7a43aa55f..ad5c1b834e 100644 --- a/cocos/2d/CCParticleSystemQuad.cpp +++ b/cocos/2d/CCParticleSystemQuad.cpp @@ -415,7 +415,7 @@ void ParticleSystemQuad::draw() CCASSERT( _particleIdx == _particleCount, "Abnormal error in particle quad"); kmGLGetMatrix(KM_GL_MODELVIEW, &_transformMatrix); - CustomCommand* cmd = new CustomCommand(); + CustomCommand* cmd = CustomCommand::getCommandPool().generateCommand(); cmd->init(0, _vertexZ); cmd->func = CC_CALLBACK_0(ParticleSystemQuad::onDraw, this); Renderer::getInstance()->addCommand(cmd); diff --git a/cocos/2d/CCSprite.cpp b/cocos/2d/CCSprite.cpp index a77be3c41c..815079eb8e 100644 --- a/cocos/2d/CCSprite.cpp +++ b/cocos/2d/CCSprite.cpp @@ -663,7 +663,7 @@ void Sprite::draw(void) { updateQuadVertices(); //TODO implement z order - QuadCommand* renderCommand = new QuadCommand(); + QuadCommand* renderCommand = QuadCommand::getCommandPool().generateCommand(); renderCommand->init(0, _vertexZ, _texture->getName(), _shaderProgram, _blendFunc, &_quad, 1); Renderer::getInstance()->addCommand(renderCommand); } diff --git a/cocos/2d/renderer/CCNewDrawNode.cpp b/cocos/2d/renderer/CCNewDrawNode.cpp index e2da995dcc..6b97034fc6 100644 --- a/cocos/2d/renderer/CCNewDrawNode.cpp +++ b/cocos/2d/renderer/CCNewDrawNode.cpp @@ -44,7 +44,7 @@ void NewDrawNode::draw() { kmGLGetMatrix(KM_GL_MODELVIEW, &_transformMatrix); - CustomCommand* cmd = new CustomCommand(); + CustomCommand* cmd = CustomCommand::getCommandPool().generateCommand(); cmd->init(0, _vertexZ); cmd->func = CC_CALLBACK_0(NewDrawNode::onDraw, this); Renderer::getInstance()->addCommand(cmd); diff --git a/cocos/2d/renderer/CCNewSprite.cpp b/cocos/2d/renderer/CCNewSprite.cpp index 4cce8cb3df..cca9a811f1 100644 --- a/cocos/2d/renderer/CCNewSprite.cpp +++ b/cocos/2d/renderer/CCNewSprite.cpp @@ -142,7 +142,7 @@ void NewSprite::draw(void) } //TODO implement z order - QuadCommand* renderCommand = new QuadCommand(); + QuadCommand* renderCommand = QuadCommand::getCommandPool().generateCommand(); renderCommand->init(0, _vertexZ, _texture->getName(), _shaderProgram, _blendFunc, &_quad, 1); Renderer::getInstance()->addCommand(renderCommand); diff --git a/cocos/2d/renderer/CCNewSpriteBatchNode.cpp b/cocos/2d/renderer/CCNewSpriteBatchNode.cpp index d10becd344..bd1ab8a04c 100644 --- a/cocos/2d/renderer/CCNewSpriteBatchNode.cpp +++ b/cocos/2d/renderer/CCNewSpriteBatchNode.cpp @@ -60,7 +60,7 @@ void NewSpriteBatchNode::draw() arrayMakeObjectsPerformSelector(_children, updateTransform, NewSprite*); - QuadCommand* cmd = new QuadCommand(); + QuadCommand* cmd = QuadCommand::getCommandPool().generateCommand(); cmd->init(0, _vertexZ, _textureAtlas->getTexture()->getName(), _shaderProgram, _blendFunc, _textureAtlas->getQuads(), _textureAtlas->getTotalQuads()); Renderer::getInstance()->addCommand(cmd); } diff --git a/cocos/2d/renderer/CustomCommand.cpp b/cocos/2d/renderer/CustomCommand.cpp index 7ff11d6310..c24dec1619 100644 --- a/cocos/2d/renderer/CustomCommand.cpp +++ b/cocos/2d/renderer/CustomCommand.cpp @@ -6,6 +6,7 @@ #include "CustomCommand.h" NS_CC_BEGIN +RenderCommandPool CustomCommand::_commandPool; CustomCommand::CustomCommand() :RenderCommand() @@ -46,4 +47,9 @@ void CustomCommand::execute() } } +void CustomCommand::releaseToCommandPool() +{ + getCommandPool().pushBackCommand(this); +} + NS_CC_END \ No newline at end of file diff --git a/cocos/2d/renderer/CustomCommand.h b/cocos/2d/renderer/CustomCommand.h index cf66612b37..aa78f6657c 100644 --- a/cocos/2d/renderer/CustomCommand.h +++ b/cocos/2d/renderer/CustomCommand.h @@ -8,16 +8,19 @@ #define _CC_CUSTOMCOMMAND_H_ #include "RenderCommand.h" +#include "RenderCommandPool.h" NS_CC_BEGIN using namespace std; class CustomCommand : public RenderCommand { -public: +protected: CustomCommand(); - void init(int viewport, int32_t depth); ~CustomCommand(); + +public: + void init(int viewport, int32_t depth); // +----------+----------+-----+-----------------------------------+ // | | | | | | @@ -29,6 +32,7 @@ public: void execute(); inline bool isTranslucent() { return true; } + virtual void releaseToCommandPool() override; public: function func; @@ -37,6 +41,12 @@ protected: int _viewport; int32_t _depth; + +public: + friend class RenderCommandPool; + static RenderCommandPool& getCommandPool() { return _commandPool; } +protected: + static RenderCommandPool _commandPool; }; diff --git a/cocos/2d/renderer/GroupCommand.cpp b/cocos/2d/renderer/GroupCommand.cpp index 93530fb31f..46939b267e 100644 --- a/cocos/2d/renderer/GroupCommand.cpp +++ b/cocos/2d/renderer/GroupCommand.cpp @@ -7,6 +7,7 @@ #include "Renderer.h" NS_CC_BEGIN +RenderCommandPool GroupCommand::_commandPool; static GroupCommandManager* s_instance; GroupCommandManager *GroupCommandManager::getInstance() @@ -96,5 +97,10 @@ int64_t GroupCommand::generateID() return _id; } +void GroupCommand::releaseToCommandPool() +{ + getCommandPool().pushBackCommand(this); +} + NS_CC_END diff --git a/cocos/2d/renderer/GroupCommand.h b/cocos/2d/renderer/GroupCommand.h index fdfce93be2..de99966b1e 100644 --- a/cocos/2d/renderer/GroupCommand.h +++ b/cocos/2d/renderer/GroupCommand.h @@ -9,6 +9,7 @@ #include "CCPlatformMacros.h" #include "RenderCommand.h" +#include "RenderCommandPool.h" #include NS_CC_BEGIN @@ -33,11 +34,11 @@ protected: class GroupCommand : public RenderCommand { - -public: +protected: GroupCommand(); - void init(int viewport, int32_t depth); ~GroupCommand(); +public: + void init(int viewport, int32_t depth); // +----------+----------+-----+-----------------------------------+ // | | | | | | @@ -48,11 +49,18 @@ public: inline bool isTranslucent() {return true;} inline int getRenderQueueID() {return _renderQueueID;} - + virtual void releaseToCommandPool() override; + protected: int _viewport; int32_t _depth; int _renderQueueID; + +public: + friend class RenderCommandPool; + static RenderCommandPool& getCommandPool() { return _commandPool; } +protected: + static RenderCommandPool _commandPool; }; NS_CC_END diff --git a/cocos/2d/renderer/NewClippingNode.cpp b/cocos/2d/renderer/NewClippingNode.cpp index 8caccbcbd6..5474516336 100644 --- a/cocos/2d/renderer/NewClippingNode.cpp +++ b/cocos/2d/renderer/NewClippingNode.cpp @@ -85,27 +85,27 @@ void NewClippingNode::visit() Renderer* renderer = Renderer::getInstance(); - GroupCommand* groupCommand = new GroupCommand(); + GroupCommand* groupCommand = GroupCommand::getCommandPool().generateCommand(); groupCommand->init(0,_vertexZ); renderer->addCommand(groupCommand); renderer->pushGroup(groupCommand->getRenderQueueID()); - CustomCommand* beforeVisitCmd = new CustomCommand(); + CustomCommand* beforeVisitCmd = CustomCommand::getCommandPool().generateCommand(); beforeVisitCmd->init(0,_vertexZ); beforeVisitCmd->func = CC_CALLBACK_0(NewClippingNode::beforeVisit, this); renderer->addCommand(beforeVisitCmd, groupCommand->getRenderQueueID()); _stencil->visit(); - CustomCommand* afterDrawStencilCmd = new CustomCommand(); + CustomCommand* afterDrawStencilCmd = CustomCommand::getCommandPool().generateCommand(); afterDrawStencilCmd->init(0,_vertexZ); afterDrawStencilCmd->func = CC_CALLBACK_0(NewClippingNode::afterDrawStencil, this); renderer->addCommand(afterDrawStencilCmd, groupCommand->getRenderQueueID()); Node::visit(); - CustomCommand* afterVisitCmd = new CustomCommand(); + CustomCommand* afterVisitCmd = CustomCommand::getCommandPool().generateCommand(); afterVisitCmd->init(0,_vertexZ); afterVisitCmd->func = CC_CALLBACK_0(NewClippingNode::afterVisit, this); renderer->addCommand(afterVisitCmd, groupCommand->getRenderQueueID()); diff --git a/cocos/2d/renderer/QuadCommand.cpp b/cocos/2d/renderer/QuadCommand.cpp index 5fd6ca5de1..1c5b190a1d 100644 --- a/cocos/2d/renderer/QuadCommand.cpp +++ b/cocos/2d/renderer/QuadCommand.cpp @@ -7,6 +7,7 @@ #include "ccGLStateCache.h" NS_CC_BEGIN +RenderCommandPool QuadCommand::_commandPool; QuadCommand::QuadCommand() :RenderCommand() @@ -97,4 +98,9 @@ void QuadCommand::useMaterial() GL::blendFunc(_blendType.src, _blendType.dst); } +void QuadCommand::releaseToCommandPool() +{ + getCommandPool().pushBackCommand(this); +} + NS_CC_END \ No newline at end of file diff --git a/cocos/2d/renderer/QuadCommand.h b/cocos/2d/renderer/QuadCommand.h index b15bb698a7..78e7323820 100644 --- a/cocos/2d/renderer/QuadCommand.h +++ b/cocos/2d/renderer/QuadCommand.h @@ -9,6 +9,7 @@ #include "RenderCommand.h" #include "CCGLProgram.h" +#include "RenderCommandPool.h" NS_CC_BEGIN @@ -16,10 +17,12 @@ NS_CC_BEGIN class QuadCommand : public RenderCommand { -public: +protected: QuadCommand(); - void init(int viewport, int32_t depth, GLuint texutreID, GLProgram* shader, BlendFunc blendType, V3F_C4B_T2F_Quad* quad, int quadCount); ~QuadCommand(); + +public: + void init(int viewport, int32_t depth, GLuint texutreID, GLProgram* shader, BlendFunc blendType, V3F_C4B_T2F_Quad* quad, int quadCount); // +----------+----------+-----+-----------------------------------+ // | | | | | | @@ -44,6 +47,8 @@ public: inline GLProgram* getShader() { return _shader; } inline BlendFunc getBlendType() { return _blendType; } + + virtual void releaseToCommandPool() override; protected: int32_t _materialID; @@ -64,6 +69,11 @@ protected: V3F_C4B_T2F_Quad* _quad; int _quadCount; +public: + friend class RenderCommandPool; + static RenderCommandPool& getCommandPool() { return _commandPool; } +protected: + static RenderCommandPool _commandPool; }; NS_CC_END diff --git a/cocos/2d/renderer/RenderCommand.h b/cocos/2d/renderer/RenderCommand.h index 7c055e7cb7..eb136ec520 100644 --- a/cocos/2d/renderer/RenderCommand.h +++ b/cocos/2d/renderer/RenderCommand.h @@ -25,19 +25,19 @@ enum RenderCommandType //TODO make RenderCommand inherent from Object class RenderCommand { -public: - +protected: RenderCommand(); virtual ~RenderCommand(); +public: virtual int64_t generateID() = 0; virtual /** * Get Render Command Id */ inline int64_t getID() { return _id; } - + virtual inline RenderCommandType getType() { return _type; } - + virtual void releaseToCommandPool() =0; protected: void printID(); diff --git a/cocos/2d/renderer/Renderer.cpp b/cocos/2d/renderer/Renderer.cpp index 1c332b5d8e..cb752b9b70 100644 --- a/cocos/2d/renderer/Renderer.cpp +++ b/cocos/2d/renderer/Renderer.cpp @@ -245,7 +245,7 @@ void Renderer::render() //TODO give command back to command pool for (size_t j = 0 ; j < _renderGroups.size(); j++) { - for_each(_renderGroups[j].begin(), _renderGroups[j].end(), [](RenderCommand* cmd){delete cmd;}); + for_each(_renderGroups[j].begin(), _renderGroups[j].end(), [](RenderCommand* cmd){ cmd->releaseToCommandPool(); }); _renderGroups[j].clear(); }