From 93b39601f9cb274b3f15b078c208f9b758412e2e Mon Sep 17 00:00:00 2001 From: halx99 Date: Wed, 19 Aug 2020 11:42:32 +0800 Subject: [PATCH 1/4] Make CustomCommand safe copyable and moveable --- cocos/renderer/CCCustomCommand.cpp | 38 ++++++++++++++++++++++++++++++ cocos/renderer/CCCustomCommand.h | 10 +++++++- 2 files changed, 47 insertions(+), 1 deletion(-) diff --git a/cocos/renderer/CCCustomCommand.cpp b/cocos/renderer/CCCustomCommand.cpp index e06012a17e..7c25b3f597 100644 --- a/cocos/renderer/CCCustomCommand.cpp +++ b/cocos/renderer/CCCustomCommand.cpp @@ -41,6 +41,44 @@ CustomCommand::~CustomCommand() CC_SAFE_RELEASE(_indexBuffer); } +CustomCommand::CustomCommand(const CustomCommand& rhs) +{ + this->copyAssign(rhs); +} + +CustomCommand::CustomCommand(CustomCommand&& rhs) +{ + this->moveAssign(std::move(rhs)); +} + +CustomCommand& CustomCommand::operator=(const CustomCommand& rhs) +{ + this->copyAssign(rhs); + return *this; +} +CustomCommand& CustomCommand::operator=(CustomCommand&& rhs) +{ + this->moveAssign(std::move(rhs)); + return *this; +} + +void CustomCommand::copyAssign(const CustomCommand& rhs) +{ + if (this != &rhs) { + memcpy(this, &rhs, sizeof(rhs)); + CC_SAFE_RETAIN(_vertexBuffer); + CC_SAFE_RETAIN(_indexBuffer); + } +} + +void CustomCommand::moveAssign(CustomCommand&& rhs) +{ + if (this != &rhs) { + memcpy(this, &rhs, sizeof(rhs)); + rhs._vertexBuffer = rhs._indexBuffer = nullptr; + } +} + void CustomCommand::init(float depth, const cocos2d::Mat4 &modelViewTransform, unsigned int flags) { RenderCommand::init(depth, modelViewTransform, flags); diff --git a/cocos/renderer/CCCustomCommand.h b/cocos/renderer/CCCustomCommand.h index 4513afd93d..d741195914 100644 --- a/cocos/renderer/CCCustomCommand.h +++ b/cocos/renderer/CCCustomCommand.h @@ -68,10 +68,18 @@ public: /**Constructor.*/ CustomCommand(); + CustomCommand(const CustomCommand& rhs); + CustomCommand(CustomCommand&& rhs); /**Destructor.*/ ~CustomCommand(); - + CustomCommand& operator=(const CustomCommand& rhs); + CustomCommand& operator=(CustomCommand&& rhs); + +protected: + void copyAssign(const CustomCommand& rhs); + void moveAssign(CustomCommand&& rhs); + public: /** TODO: should remove it. From 82ceee2fdb9e6a9a4d8be534e0a43e13dea70589 Mon Sep 17 00:00:00 2001 From: halx99 Date: Wed, 19 Aug 2020 11:46:13 +0800 Subject: [PATCH 2/4] fix label crashing, add private use BatchCommand move constructor to adapte std::vector resize properly --- cocos/2d/CCLabel.cpp | 58 ++++++++++++++++++++++++++------------------ cocos/2d/CCLabel.h | 9 ++++--- 2 files changed, 41 insertions(+), 26 deletions(-) diff --git a/cocos/2d/CCLabel.cpp b/cocos/2d/CCLabel.cpp index c6000f07d4..0ce325c829 100644 --- a/cocos/2d/CCLabel.cpp +++ b/cocos/2d/CCLabel.cpp @@ -209,16 +209,43 @@ Label::BatchCommand::BatchCommand() outLineCommand.setPrimitiveType(CustomCommand::PrimitiveType::TRIANGLE); } +Label::BatchCommand::BatchCommand(BatchCommand&& rhs) : + textCommand(std::move(rhs.textCommand)), + shadowCommand(std::move(rhs.shadowCommand)), + outLineCommand(std::move(rhs.outLineCommand)) +{ + rhs.textCommand.getPipelineDescriptor().programState = nullptr; + rhs.shadowCommand.getPipelineDescriptor().programState = nullptr; + rhs.outLineCommand.getPipelineDescriptor().programState = nullptr; +} + Label::BatchCommand::~BatchCommand() { - CC_SAFE_RELEASE_NULL(textCommand.getPipelineDescriptor().programState); - CC_SAFE_RELEASE_NULL(shadowCommand.getPipelineDescriptor().programState); - CC_SAFE_RELEASE_NULL(outLineCommand.getPipelineDescriptor().programState); + CC_SAFE_RELEASE(textCommand.getPipelineDescriptor().programState); + CC_SAFE_RELEASE(shadowCommand.getPipelineDescriptor().programState); + CC_SAFE_RELEASE(outLineCommand.getPipelineDescriptor().programState); +} + +void Label::BatchCommand::setProgramState(backend::ProgramState* programState) +{ + assert(programState); + + auto& programStateText = textCommand.getPipelineDescriptor().programState; + CC_SAFE_RELEASE(programStateText); + programStateText = programState->clone(); + + auto& programStateShadow = shadowCommand.getPipelineDescriptor().programState; + CC_SAFE_RELEASE(programStateShadow); + programStateShadow = programState->clone(); + + auto& programStateOutline = outLineCommand.getPipelineDescriptor().programState; + CC_SAFE_RELEASE(programStateOutline); + programStateOutline = programState->clone(); } std::array Label::BatchCommand::getCommandArray() { - return std::array{&textCommand, &shadowCommand, &outLineCommand}; + return std::array{&textCommand, & shadowCommand, & outLineCommand}; } Label* Label::create() @@ -636,7 +663,7 @@ static Texture2D* _getTexture(Label* label) return texture; } -void Label::setVertexLayout(PipelineDescriptor& pipelineDescriptor) +void Label::setVertexLayout() { auto vertexLayout = _programState->getVertexLayout(); ///a_position @@ -672,7 +699,7 @@ void Label::setProgramState(backend::ProgramState *programState) } auto &quadPipeline = _quadCommand.getPipelineDescriptor(); - setVertexLayout(quadPipeline); + setVertexLayout(); quadPipeline.programState = _programState; } @@ -736,29 +763,14 @@ void Label::updateShaderProgram() } auto &quadPipeline = _quadCommand.getPipelineDescriptor(); - setVertexLayout(quadPipeline); + setVertexLayout(); quadPipeline.programState = _programState; } void Label::updateBatchCommand(Label::BatchCommand &batch) { CCASSERT(_programState, "programState should be set!"); - - auto& pipelineDescriptor = batch.textCommand.getPipelineDescriptor(); - CC_SAFE_RELEASE_NULL(pipelineDescriptor.programState); - pipelineDescriptor.programState = _programState->clone(); - setVertexLayout(pipelineDescriptor); - - auto &pipelineShadow = batch.shadowCommand.getPipelineDescriptor(); - CC_SAFE_RELEASE_NULL(pipelineShadow.programState); - pipelineShadow.programState = _programState->clone();; - setVertexLayout(pipelineShadow); - - auto &pipelineOutline = batch.outLineCommand.getPipelineDescriptor(); - CC_SAFE_RELEASE_NULL(pipelineOutline.programState); - pipelineOutline.programState = _programState->clone(); - setVertexLayout(pipelineOutline); - + batch.setProgramState(_programState); } void Label::updateUniformLocations() diff --git a/cocos/2d/CCLabel.h b/cocos/2d/CCLabel.h index 2f904b3d13..67aa4776e6 100644 --- a/cocos/2d/CCLabel.h +++ b/cocos/2d/CCLabel.h @@ -702,13 +702,16 @@ protected: struct BatchCommand { BatchCommand(); + BatchCommand(BatchCommand&& rhs); ~BatchCommand(); + void setProgramState(backend::ProgramState* state); + + std::array getCommandArray(); + CustomCommand textCommand; CustomCommand outLineCommand; CustomCommand shadowCommand; - - std::array getCommandArray(); }; virtual void setFontAtlas(FontAtlas* atlas, bool distanceFieldEnabled = false, bool useA8Shader = false); @@ -757,7 +760,7 @@ protected: virtual void updateColor() override; void updateUniformLocations(); - void setVertexLayout(PipelineDescriptor& vertexLayout); + void setVertexLayout(); void updateBlendState(); void updateEffectUniforms(BatchCommand &batch, TextureAtlas* textureAtlas, Renderer *renderer, const Mat4 &transform); void updateBuffer(TextureAtlas* textureAtlas, CustomCommand& customCommand); From e9ba5420efc430180086e5b6dcde977afa120209 Mon Sep 17 00:00:00 2001 From: halx99 Date: Wed, 19 Aug 2020 12:10:29 +0800 Subject: [PATCH 3/4] Refine code --- cocos/2d/CCLabel.cpp | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/cocos/2d/CCLabel.cpp b/cocos/2d/CCLabel.cpp index 0ce325c829..7bac40c8b6 100644 --- a/cocos/2d/CCLabel.cpp +++ b/cocos/2d/CCLabel.cpp @@ -245,7 +245,7 @@ void Label::BatchCommand::setProgramState(backend::ProgramState* programState) std::array Label::BatchCommand::getCommandArray() { - return std::array{&textCommand, & shadowCommand, & outLineCommand}; + return std::array{&textCommand, &shadowCommand, &outLineCommand}; } Label* Label::create() @@ -1911,8 +1911,7 @@ void Label::draw(Renderer *renderer, const Mat4 &transform, uint32_t flags) continue; auto &batch = _batchCommands[i++]; - auto &&commands = batch.getCommandArray(); - for (auto command : commands) + for (auto command : batch.getCommandArray()) { auto *programState = command->getPipelineDescriptor().programState; Vec4 textColor(_textColorF.r, _textColorF.g, _textColorF.b, _textColorF.a); @@ -1932,7 +1931,7 @@ void Label::updateBlendState() setOpacityModifyRGB(_blendFunc != BlendFunc::ALPHA_NON_PREMULTIPLIED); for(auto &batch: _batchCommands) { - for(auto *command : batch.getCommandArray()) { + for(auto command : batch.getCommandArray()) { auto & blendDescriptor = command->getPipelineDescriptor().blendDescriptor; updateBlend(blendDescriptor, _blendFunc); } From 92c35922daa62d190a0f71fc636e0de6e306f1b5 Mon Sep 17 00:00:00 2001 From: halx99 Date: Wed, 19 Aug 2020 12:26:09 +0800 Subject: [PATCH 4/4] Naming style [skip appveyor] [skip travis] --- cocos/renderer/CCCustomCommand.cpp | 12 ++++++------ cocos/renderer/CCCustomCommand.h | 4 ++-- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/cocos/renderer/CCCustomCommand.cpp b/cocos/renderer/CCCustomCommand.cpp index 7c25b3f597..f216eb706c 100644 --- a/cocos/renderer/CCCustomCommand.cpp +++ b/cocos/renderer/CCCustomCommand.cpp @@ -43,26 +43,26 @@ CustomCommand::~CustomCommand() CustomCommand::CustomCommand(const CustomCommand& rhs) { - this->copyAssign(rhs); + this->assign(rhs); } CustomCommand::CustomCommand(CustomCommand&& rhs) { - this->moveAssign(std::move(rhs)); + this->assign(std::move(rhs)); } CustomCommand& CustomCommand::operator=(const CustomCommand& rhs) { - this->copyAssign(rhs); + this->assign(rhs); return *this; } CustomCommand& CustomCommand::operator=(CustomCommand&& rhs) { - this->moveAssign(std::move(rhs)); + this->assign(std::move(rhs)); return *this; } -void CustomCommand::copyAssign(const CustomCommand& rhs) +void CustomCommand::assign(const CustomCommand& rhs) { if (this != &rhs) { memcpy(this, &rhs, sizeof(rhs)); @@ -71,7 +71,7 @@ void CustomCommand::copyAssign(const CustomCommand& rhs) } } -void CustomCommand::moveAssign(CustomCommand&& rhs) +void CustomCommand::assign(CustomCommand&& rhs) { if (this != &rhs) { memcpy(this, &rhs, sizeof(rhs)); diff --git a/cocos/renderer/CCCustomCommand.h b/cocos/renderer/CCCustomCommand.h index d741195914..b503c0453b 100644 --- a/cocos/renderer/CCCustomCommand.h +++ b/cocos/renderer/CCCustomCommand.h @@ -77,8 +77,8 @@ public: CustomCommand& operator=(CustomCommand&& rhs); protected: - void copyAssign(const CustomCommand& rhs); - void moveAssign(CustomCommand&& rhs); + void assign(const CustomCommand& rhs); + void assign(CustomCommand&& rhs); public: /**