Merge pull request #167 from halx99/fix-label-crashing

Fix label crashing
This commit is contained in:
HALX99 2020-08-19 13:10:19 +08:00 committed by GitHub
commit 08fe49d438
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 89 additions and 29 deletions

View File

@ -209,11 +209,38 @@ Label::BatchCommand::BatchCommand()
outLineCommand.setPrimitiveType(CustomCommand::PrimitiveType::TRIANGLE); 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() Label::BatchCommand::~BatchCommand()
{ {
CC_SAFE_RELEASE_NULL(textCommand.getPipelineDescriptor().programState); CC_SAFE_RELEASE(textCommand.getPipelineDescriptor().programState);
CC_SAFE_RELEASE_NULL(shadowCommand.getPipelineDescriptor().programState); CC_SAFE_RELEASE(shadowCommand.getPipelineDescriptor().programState);
CC_SAFE_RELEASE_NULL(outLineCommand.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<CustomCommand*, 3> Label::BatchCommand::getCommandArray() std::array<CustomCommand*, 3> Label::BatchCommand::getCommandArray()
@ -636,7 +663,7 @@ static Texture2D* _getTexture(Label* label)
return texture; return texture;
} }
void Label::setVertexLayout(PipelineDescriptor& pipelineDescriptor) void Label::setVertexLayout()
{ {
auto vertexLayout = _programState->getVertexLayout(); auto vertexLayout = _programState->getVertexLayout();
///a_position ///a_position
@ -672,7 +699,7 @@ void Label::setProgramState(backend::ProgramState *programState)
} }
auto &quadPipeline = _quadCommand.getPipelineDescriptor(); auto &quadPipeline = _quadCommand.getPipelineDescriptor();
setVertexLayout(quadPipeline); setVertexLayout();
quadPipeline.programState = _programState; quadPipeline.programState = _programState;
} }
@ -736,29 +763,14 @@ void Label::updateShaderProgram()
} }
auto &quadPipeline = _quadCommand.getPipelineDescriptor(); auto &quadPipeline = _quadCommand.getPipelineDescriptor();
setVertexLayout(quadPipeline); setVertexLayout();
quadPipeline.programState = _programState; quadPipeline.programState = _programState;
} }
void Label::updateBatchCommand(Label::BatchCommand &batch) void Label::updateBatchCommand(Label::BatchCommand &batch)
{ {
CCASSERT(_programState, "programState should be set!"); CCASSERT(_programState, "programState should be set!");
batch.setProgramState(_programState);
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);
} }
void Label::updateUniformLocations() void Label::updateUniformLocations()
@ -1899,8 +1911,7 @@ void Label::draw(Renderer *renderer, const Mat4 &transform, uint32_t flags)
continue; continue;
auto &batch = _batchCommands[i++]; auto &batch = _batchCommands[i++];
auto &&commands = batch.getCommandArray(); for (auto command : batch.getCommandArray())
for (auto command : commands)
{ {
auto *programState = command->getPipelineDescriptor().programState; auto *programState = command->getPipelineDescriptor().programState;
Vec4 textColor(_textColorF.r, _textColorF.g, _textColorF.b, _textColorF.a); Vec4 textColor(_textColorF.r, _textColorF.g, _textColorF.b, _textColorF.a);
@ -1920,7 +1931,7 @@ void Label::updateBlendState()
setOpacityModifyRGB(_blendFunc != BlendFunc::ALPHA_NON_PREMULTIPLIED); setOpacityModifyRGB(_blendFunc != BlendFunc::ALPHA_NON_PREMULTIPLIED);
for(auto &batch: _batchCommands) for(auto &batch: _batchCommands)
{ {
for(auto *command : batch.getCommandArray()) { for(auto command : batch.getCommandArray()) {
auto & blendDescriptor = command->getPipelineDescriptor().blendDescriptor; auto & blendDescriptor = command->getPipelineDescriptor().blendDescriptor;
updateBlend(blendDescriptor, _blendFunc); updateBlend(blendDescriptor, _blendFunc);
} }

View File

@ -702,13 +702,16 @@ protected:
struct BatchCommand { struct BatchCommand {
BatchCommand(); BatchCommand();
BatchCommand(BatchCommand&& rhs);
~BatchCommand(); ~BatchCommand();
void setProgramState(backend::ProgramState* state);
std::array<CustomCommand*, 3> getCommandArray();
CustomCommand textCommand; CustomCommand textCommand;
CustomCommand outLineCommand; CustomCommand outLineCommand;
CustomCommand shadowCommand; CustomCommand shadowCommand;
std::array<CustomCommand*, 3> getCommandArray();
}; };
virtual void setFontAtlas(FontAtlas* atlas, bool distanceFieldEnabled = false, bool useA8Shader = false); virtual void setFontAtlas(FontAtlas* atlas, bool distanceFieldEnabled = false, bool useA8Shader = false);
@ -757,7 +760,7 @@ protected:
virtual void updateColor() override; virtual void updateColor() override;
void updateUniformLocations(); void updateUniformLocations();
void setVertexLayout(PipelineDescriptor& vertexLayout); void setVertexLayout();
void updateBlendState(); void updateBlendState();
void updateEffectUniforms(BatchCommand &batch, TextureAtlas* textureAtlas, Renderer *renderer, const Mat4 &transform); void updateEffectUniforms(BatchCommand &batch, TextureAtlas* textureAtlas, Renderer *renderer, const Mat4 &transform);
void updateBuffer(TextureAtlas* textureAtlas, CustomCommand& customCommand); void updateBuffer(TextureAtlas* textureAtlas, CustomCommand& customCommand);

View File

@ -41,6 +41,44 @@ CustomCommand::~CustomCommand()
CC_SAFE_RELEASE(_indexBuffer); CC_SAFE_RELEASE(_indexBuffer);
} }
CustomCommand::CustomCommand(const CustomCommand& rhs)
{
this->assign(rhs);
}
CustomCommand::CustomCommand(CustomCommand&& rhs)
{
this->assign(std::move(rhs));
}
CustomCommand& CustomCommand::operator=(const CustomCommand& rhs)
{
this->assign(rhs);
return *this;
}
CustomCommand& CustomCommand::operator=(CustomCommand&& rhs)
{
this->assign(std::move(rhs));
return *this;
}
void CustomCommand::assign(const CustomCommand& rhs)
{
if (this != &rhs) {
memcpy(this, &rhs, sizeof(rhs));
CC_SAFE_RETAIN(_vertexBuffer);
CC_SAFE_RETAIN(_indexBuffer);
}
}
void CustomCommand::assign(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) void CustomCommand::init(float depth, const cocos2d::Mat4 &modelViewTransform, unsigned int flags)
{ {
RenderCommand::init(depth, modelViewTransform, flags); RenderCommand::init(depth, modelViewTransform, flags);

View File

@ -68,10 +68,18 @@ public:
/**Constructor.*/ /**Constructor.*/
CustomCommand(); CustomCommand();
CustomCommand(const CustomCommand& rhs);
CustomCommand(CustomCommand&& rhs);
/**Destructor.*/ /**Destructor.*/
~CustomCommand(); ~CustomCommand();
CustomCommand& operator=(const CustomCommand& rhs);
CustomCommand& operator=(CustomCommand&& rhs);
protected:
void assign(const CustomCommand& rhs);
void assign(CustomCommand&& rhs);
public: public:
/** /**
TODO: should remove it. TODO: should remove it.