Merge pull request #9596 from minggo/use-auto-reference

performance improve
This commit is contained in:
minggo 2014-12-24 10:31:28 +08:00
commit 22f0615366
2 changed files with 75 additions and 59 deletions

View File

@ -457,6 +457,7 @@ void Node::setScale(float scaleX,float scaleY)
#if CC_USE_PHYSICS #if CC_USE_PHYSICS
if(g_physicsSceneCount == 0) if(g_physicsSceneCount == 0)
return; return;
auto scene = getScene(); auto scene = getScene();
if (!scene || scene->getPhysicsWorld()) if (!scene || scene->getPhysicsWorld())
{ {
@ -477,6 +478,7 @@ void Node::setScaleX(float scaleX)
#if CC_USE_PHYSICS #if CC_USE_PHYSICS
if(g_physicsSceneCount == 0) if(g_physicsSceneCount == 0)
return; return;
auto scene = getScene(); auto scene = getScene();
if (!scene || scene->getPhysicsWorld()) if (!scene || scene->getPhysicsWorld())
{ {
@ -524,8 +526,9 @@ void Node::setScaleY(float scaleY)
_transformUpdated = _transformDirty = _inverseDirty = true; _transformUpdated = _transformDirty = _inverseDirty = true;
#if CC_USE_PHYSICS #if CC_USE_PHYSICS
if(g_physicsSceneCount == 0) if (g_physicsSceneCount == 0)
return; return;
auto scene = getScene(); auto scene = getScene();
if (!scene || scene->getPhysicsWorld()) if (!scene || scene->getPhysicsWorld())
{ {
@ -580,11 +583,7 @@ void Node::setPosition3D(const Vec3& position)
Vec3 Node::getPosition3D() const Vec3 Node::getPosition3D() const
{ {
Vec3 ret; return Vec3(_position.x, _position.y, _positionZ);
ret.x = _position.x;
ret.y = _position.y;
ret.z = _positionZ;
return ret;
} }
float Node::getPositionX() const float Node::getPositionX() const
@ -661,7 +660,8 @@ void Node::setVisible(bool visible)
if(visible != _visible) if(visible != _visible)
{ {
_visible = visible; _visible = visible;
if(_visible) _transformUpdated = _transformDirty = _inverseDirty = true; if(_visible)
_transformUpdated = _transformDirty = _inverseDirty = true;
} }
} }
@ -686,10 +686,10 @@ void Node::setAnchorPoint(const Vec2& point)
} }
#endif #endif
if( ! point.equals(_anchorPoint)) if (! point.equals(_anchorPoint))
{ {
_anchorPoint = point; _anchorPoint = point;
_anchorPointInPoints = Vec2(_contentSize.width * _anchorPoint.x, _contentSize.height * _anchorPoint.y ); _anchorPointInPoints = Vec2(_contentSize.width * _anchorPoint.x, _contentSize.height * _anchorPoint.y);
_transformUpdated = _transformDirty = _inverseDirty = true; _transformUpdated = _transformDirty = _inverseDirty = true;
} }
} }
@ -702,11 +702,11 @@ const Size& Node::getContentSize() const
void Node::setContentSize(const Size & size) void Node::setContentSize(const Size & size)
{ {
if ( ! size.equals(_contentSize)) if (! size.equals(_contentSize))
{ {
_contentSize = size; _contentSize = size;
_anchorPointInPoints = Vec2(_contentSize.width * _anchorPoint.x, _contentSize.height * _anchorPoint.y ); _anchorPointInPoints = Vec2(_contentSize.width * _anchorPoint.x, _contentSize.height * _anchorPoint.y);
_transformUpdated = _transformDirty = _inverseDirty = _contentSizeDirty = true; _transformUpdated = _transformDirty = _inverseDirty = _contentSizeDirty = true;
} }
} }
@ -794,7 +794,8 @@ GLProgramState* Node::getGLProgramState() const
void Node::setGLProgramState(cocos2d::GLProgramState *glProgramState) void Node::setGLProgramState(cocos2d::GLProgramState *glProgramState)
{ {
if(glProgramState != _glProgramState) { if (glProgramState != _glProgramState)
{
CC_SAFE_RELEASE(_glProgramState); CC_SAFE_RELEASE(_glProgramState);
_glProgramState = glProgramState; _glProgramState = glProgramState;
CC_SAFE_RETAIN(_glProgramState); CC_SAFE_RETAIN(_glProgramState);
@ -818,7 +819,7 @@ GLProgram * Node::getGLProgram() const
Scene* Node::getScene() const Scene* Node::getScene() const
{ {
if(!_parent) if (!_parent)
return nullptr; return nullptr;
auto sceneNode = _parent; auto sceneNode = _parent;
@ -832,7 +833,7 @@ Scene* Node::getScene() const
Rect Node::getBoundingBox() const Rect Node::getBoundingBox() const
{ {
Rect rect = Rect(0, 0, _contentSize.width, _contentSize.height); Rect rect(0, 0, _contentSize.width, _contentSize.height);
return RectApplyAffineTransform(rect, getNodeToParentAffineTransform()); return RectApplyAffineTransform(rect, getNodeToParentAffineTransform());
} }
@ -848,7 +849,7 @@ Node* Node::getChildByTag(int tag) const
{ {
CCASSERT( tag != Node::INVALID_TAG, "Invalid tag"); CCASSERT( tag != Node::INVALID_TAG, "Invalid tag");
for (auto& child : _children) for (const auto& child : _children)
{ {
if(child && child->_tag == tag) if(child && child->_tag == tag)
return child; return child;
@ -1038,7 +1039,8 @@ void Node::addChildHelper(Node* child, int localZOrder, int tag, const std::stri
{ {
child->onEnter(); child->onEnter();
// prevent onEnterTransitionDidFinish to be called twice when a node is added in onEnter // prevent onEnterTransitionDidFinish to be called twice when a node is added in onEnter
if (_isTransitionFinished) { if (_isTransitionFinished)
{
child->onEnterTransitionDidFinish(); child->onEnterTransitionDidFinish();
} }
} }
@ -1151,7 +1153,7 @@ void Node::removeFromPhysicsWorld()
void Node::removeAllChildrenWithCleanup(bool cleanup) void Node::removeAllChildrenWithCleanup(bool cleanup)
{ {
// not using detachChild improves speed here // not using detachChild improves speed here
for (auto& child : _children) for (const auto& child : _children)
{ {
// IMPORTANT: // IMPORTANT:
// -1st do onExit // -1st do onExit
@ -1225,8 +1227,9 @@ void Node::reorderChild(Node *child, int zOrder)
void Node::sortAllChildren() void Node::sortAllChildren()
{ {
if( _reorderChildDirty ) { if (_reorderChildDirty)
std::sort( std::begin(_children), std::end(_children), nodeComparisonLess ); {
std::sort(std::begin(_children), std::end(_children), nodeComparisonLess);
_reorderChildDirty = false; _reorderChildDirty = false;
} }
} }
@ -1246,16 +1249,18 @@ void Node::draw(Renderer* renderer, const Mat4 &transform, uint32_t flags)
void Node::visit() void Node::visit()
{ {
auto renderer = Director::getInstance()->getRenderer(); auto renderer = Director::getInstance()->getRenderer();
Mat4 parentTransform = Director::getInstance()->getMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW); auto& parentTransform = Director::getInstance()->getMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);
visit(renderer, parentTransform, true); visit(renderer, parentTransform, true);
} }
uint32_t Node::processParentFlags(const Mat4& parentTransform, uint32_t parentFlags) uint32_t Node::processParentFlags(const Mat4& parentTransform, uint32_t parentFlags)
{ {
if(_usingNormalizedPosition) { if(_usingNormalizedPosition)
{
CCASSERT(_parent, "setNormalizedPosition() doesn't work with orphan nodes"); CCASSERT(_parent, "setNormalizedPosition() doesn't work with orphan nodes");
if ((parentFlags & FLAGS_CONTENT_SIZE_DIRTY) || _normalizedPositionDirty) { if ((parentFlags & FLAGS_CONTENT_SIZE_DIRTY) || _normalizedPositionDirty)
auto s = _parent->getContentSize(); {
auto& s = _parent->getContentSize();
_position.x = _normalizedPosition.x * s.width; _position.x = _normalizedPosition.x * s.width;
_position.y = _normalizedPosition.y * s.height; _position.y = _normalizedPosition.y * s.height;
_transformUpdated = _transformDirty = _inverseDirty = true; _transformUpdated = _transformDirty = _inverseDirty = true;
@ -1313,7 +1318,7 @@ void Node::visit(Renderer* renderer, const Mat4 &parentTransform, uint32_t paren
{ {
auto node = _children.at(i); auto node = _children.at(i);
if ( node && node->_localZOrder < 0 ) if (node && node->_localZOrder < 0)
node->visit(renderer, _modelViewTransform, flags); node->visit(renderer, _modelViewTransform, flags);
else else
break; break;
@ -1340,9 +1345,7 @@ void Node::visit(Renderer* renderer, const Mat4 &parentTransform, uint32_t paren
Mat4 Node::transform(const Mat4& parentTransform) Mat4 Node::transform(const Mat4& parentTransform)
{ {
Mat4 ret = this->getNodeToParentTransform(); return parentTransform * this->getNodeToParentTransform();
ret = parentTransform * ret;
return ret;
} }
// MARK: events // MARK: events
@ -1467,7 +1470,8 @@ void Node::setEventDispatcher(EventDispatcher* dispatcher)
void Node::setActionManager(ActionManager* actionManager) void Node::setActionManager(ActionManager* actionManager)
{ {
if( actionManager != _actionManager ) { if( actionManager != _actionManager )
{
this->stopAllActions(); this->stopAllActions();
CC_SAFE_RETAIN(actionManager); CC_SAFE_RETAIN(actionManager);
CC_SAFE_RELEASE(_actionManager); CC_SAFE_RELEASE(_actionManager);
@ -1521,7 +1525,8 @@ ssize_t Node::getNumberOfRunningActions() const
void Node::setScheduler(Scheduler* scheduler) void Node::setScheduler(Scheduler* scheduler)
{ {
if( scheduler != _scheduler ) { if( scheduler != _scheduler )
{
this->unscheduleAllCallbacks(); this->unscheduleAllCallbacks();
CC_SAFE_RETAIN(scheduler); CC_SAFE_RETAIN(scheduler);
CC_SAFE_RELEASE(_scheduler); CC_SAFE_RELEASE(_scheduler);
@ -1719,9 +1724,7 @@ const Mat4& Node::getNodeToParentTransform() const
bool needsSkewMatrix = ( _skewX || _skewY ); bool needsSkewMatrix = ( _skewX || _skewY );
Vec2 anchorPoint; Vec2 anchorPoint(_anchorPointInPoints.x * _scaleX, _anchorPointInPoints.y * _scaleY);
anchorPoint.x = _anchorPointInPoints.x * _scaleX;
anchorPoint.y = _anchorPointInPoints.y * _scaleY;
// optimization: // optimization:
// inline anchor point calculation if skew is not needed // inline anchor point calculation if skew is not needed
@ -1750,12 +1753,14 @@ const Mat4& Node::getNodeToParentTransform() const
// FIXME: // FIXME:
// FIX ME: Expensive operation. // FIX ME: Expensive operation.
// FIX ME: It should be done together with the rotationZ // FIX ME: It should be done together with the rotationZ
if(_rotationY) { if(_rotationY)
{
Mat4 rotY; Mat4 rotY;
Mat4::createRotationY(CC_DEGREES_TO_RADIANS(_rotationY), &rotY); Mat4::createRotationY(CC_DEGREES_TO_RADIANS(_rotationY), &rotY);
_transform = _transform * rotY; _transform = _transform * rotY;
} }
if(_rotationX) { if(_rotationX)
{
Mat4 rotX; Mat4 rotX;
Mat4::createRotationX(CC_DEGREES_TO_RADIANS(_rotationX), &rotX); Mat4::createRotationX(CC_DEGREES_TO_RADIANS(_rotationX), &rotX);
_transform = _transform * rotX; _transform = _transform * rotX;
@ -1818,9 +1823,12 @@ void Node::setAdditionalTransform(const AffineTransform& additionalTransform)
void Node::setAdditionalTransform(Mat4* additionalTransform) void Node::setAdditionalTransform(Mat4* additionalTransform)
{ {
if(additionalTransform == nullptr) { if (additionalTransform == nullptr)
{
_useAdditionalTransform = false; _useAdditionalTransform = false;
} else { }
else
{
_additionalTransform = *additionalTransform; _additionalTransform = *additionalTransform;
_useAdditionalTransform = true; _useAdditionalTransform = true;
} }
@ -1831,15 +1839,15 @@ void Node::setAdditionalTransform(Mat4* additionalTransform)
AffineTransform Node::getParentToNodeAffineTransform() const AffineTransform Node::getParentToNodeAffineTransform() const
{ {
AffineTransform ret; AffineTransform ret;
Mat4 ret4 = getParentToNodeTransform();
GLToCGAffine(ret4.m,&ret); GLToCGAffine(getParentToNodeTransform().m,&ret);
return ret; return ret;
} }
const Mat4& Node::getParentToNodeTransform() const const Mat4& Node::getParentToNodeTransform() const
{ {
if ( _inverseDirty ) { if ( _inverseDirty )
{
_inverse = _transform.getInversed(); _inverse = _transform.getInversed();
_inverseDirty = false; _inverseDirty = false;
} }
@ -1850,7 +1858,7 @@ const Mat4& Node::getParentToNodeTransform() const
AffineTransform Node::getNodeToWorldAffineTransform() const AffineTransform Node::getNodeToWorldAffineTransform() const
{ {
AffineTransform t = this->getNodeToParentAffineTransform(); AffineTransform t(this->getNodeToParentAffineTransform());
for (Node *p = _parent; p != nullptr; p = p->getParent()) for (Node *p = _parent; p != nullptr; p = p->getParent())
t = AffineTransformConcat(t, p->getNodeToParentAffineTransform()); t = AffineTransformConcat(t, p->getNodeToParentAffineTransform());
@ -1860,7 +1868,7 @@ AffineTransform Node::getNodeToWorldAffineTransform() const
Mat4 Node::getNodeToWorldTransform() const Mat4 Node::getNodeToWorldTransform() const
{ {
Mat4 t = this->getNodeToParentTransform(); Mat4 t(this->getNodeToParentTransform());
for (Node *p = _parent; p != nullptr; p = p->getParent()) for (Node *p = _parent; p != nullptr; p = p->getParent())
{ {
@ -1902,27 +1910,25 @@ Vec2 Node::convertToWorldSpace(const Vec2& nodePoint) const
Vec2 Node::convertToNodeSpaceAR(const Vec2& worldPoint) const Vec2 Node::convertToNodeSpaceAR(const Vec2& worldPoint) const
{ {
Vec2 nodePoint = convertToNodeSpace(worldPoint); Vec2 nodePoint(convertToNodeSpace(worldPoint));
return nodePoint - _anchorPointInPoints; return nodePoint - _anchorPointInPoints;
} }
Vec2 Node::convertToWorldSpaceAR(const Vec2& nodePoint) const Vec2 Node::convertToWorldSpaceAR(const Vec2& nodePoint) const
{ {
Vec2 pt = nodePoint + _anchorPointInPoints; return convertToWorldSpace(nodePoint + _anchorPointInPoints);
return convertToWorldSpace(pt);
} }
Vec2 Node::convertToWindowSpace(const Vec2& nodePoint) const Vec2 Node::convertToWindowSpace(const Vec2& nodePoint) const
{ {
Vec2 worldPoint = this->convertToWorldSpace(nodePoint); Vec2 worldPoint(this->convertToWorldSpace(nodePoint));
return Director::getInstance()->convertToUI(worldPoint); return Director::getInstance()->convertToUI(worldPoint);
} }
// convenience methods which take a Touch instead of Vec2 // convenience methods which take a Touch instead of Vec2
Vec2 Node::convertTouchToNodeSpace(Touch *touch) const Vec2 Node::convertTouchToNodeSpace(Touch *touch) const
{ {
Vec2 point = touch->getLocation(); return this->convertToNodeSpace(touch->getLocation());
return this->convertToNodeSpace(point);
} }
Vec2 Node::convertTouchToNodeSpaceAR(Touch *touch) const Vec2 Node::convertTouchToNodeSpaceAR(Touch *touch) const
@ -1942,37 +1948,42 @@ void Node::updateTransform()
Component* Node::getComponent(const std::string& name) Component* Node::getComponent(const std::string& name)
{ {
if( _componentContainer ) if (_componentContainer)
return _componentContainer->get(name); return _componentContainer->get(name);
return nullptr; return nullptr;
} }
bool Node::addComponent(Component *component) bool Node::addComponent(Component *component)
{ {
// lazy alloc // lazy alloc
if( !_componentContainer ) if (!_componentContainer)
_componentContainer = new (std::nothrow) ComponentContainer(this); _componentContainer = new (std::nothrow) ComponentContainer(this);
return _componentContainer->add(component); return _componentContainer->add(component);
} }
bool Node::removeComponent(const std::string& name) bool Node::removeComponent(const std::string& name)
{ {
if( _componentContainer ) if (_componentContainer)
return _componentContainer->remove(name); return _componentContainer->remove(name);
return false; return false;
} }
bool Node::removeComponent(Component *component) bool Node::removeComponent(Component *component)
{ {
if (_componentContainer) { if (_componentContainer)
{
return _componentContainer->remove(component); return _componentContainer->remove(component);
} }
return false; return false;
} }
void Node::removeAllComponents() void Node::removeAllComponents()
{ {
if( _componentContainer ) if (_componentContainer)
_componentContainer->removeAll(); _componentContainer->removeAll();
} }
@ -2055,7 +2066,7 @@ void Node::updatePhysicsBodyScale(Scene* scene)
} }
} }
for (auto child : _children) for (const auto& child : _children)
{ {
child->updatePhysicsBodyScale(scene); child->updatePhysicsBodyScale(scene);
} }
@ -2159,7 +2170,8 @@ void Node::updateDisplayedOpacity(GLubyte parentOpacity)
if (_cascadeOpacityEnabled) if (_cascadeOpacityEnabled)
{ {
for(auto child : _children){ for(const auto& child : _children)
{
child->updateDisplayedOpacity(_displayedOpacity); child->updateDisplayedOpacity(_displayedOpacity);
} }
} }
@ -2205,7 +2217,8 @@ void Node::disableCascadeOpacity()
{ {
_displayedOpacity = _realOpacity; _displayedOpacity = _realOpacity;
for(auto child : _children){ for(const auto& child : _children)
{
child->updateDisplayedOpacity(255); child->updateDisplayedOpacity(255);
} }
} }
@ -2236,7 +2249,8 @@ void Node::updateDisplayedColor(const Color3B& parentColor)
if (_cascadeColorEnabled) if (_cascadeColorEnabled)
{ {
for(const auto &child : _children){ for(const auto &child : _children)
{
child->updateDisplayedColor(_displayedColor); child->updateDisplayedColor(_displayedColor);
} }
} }
@ -2279,7 +2293,8 @@ void Node::updateCascadeColor()
void Node::disableCascadeColor() void Node::disableCascadeColor()
{ {
for(auto child : _children){ for(const auto& child : _children)
{
child->updateDisplayedColor(Color3B::WHITE); child->updateDisplayedColor(Color3B::WHITE);
} }
} }
@ -2290,7 +2305,8 @@ void Node::setCameraMask(unsigned short mask, bool applyChildren)
_cameraMask = mask; _cameraMask = mask;
if (applyChildren) if (applyChildren)
{ {
for (auto child : _children) { for (const auto& child : _children)
{
child->setCameraMask(mask, applyChildren); child->setCameraMask(mask, applyChildren);
} }
} }

View File

@ -1114,7 +1114,7 @@ void Director::showStats()
prevVerts = currentVerts; prevVerts = currentVerts;
} }
Mat4 identity = Mat4::IDENTITY; const Mat4& identity = Mat4::IDENTITY;
_drawnVerticesLabel->visit(_renderer, identity, 0); _drawnVerticesLabel->visit(_renderer, identity, 0);
_drawnBatchesLabel->visit(_renderer, identity, 0); _drawnBatchesLabel->visit(_renderer, identity, 0);
_FPSLabel->visit(_renderer, identity, 0); _FPSLabel->visit(_renderer, identity, 0);