NodeMemPool.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /****************************************************************************
  2. Copyright (c) 2018 Xiamen Yaji Software Co., Ltd.
  3. http://www.cocos2d-x.org
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in
  11. all copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  18. THE SOFTWARE.
  19. ****************************************************************************/
  20. #include "NodeMemPool.hpp"
  21. #include "base/ccMacros.h"
  22. RENDERER_BEGIN
  23. UnitNode::UnitNode()
  24. {
  25. }
  26. UnitNode::~UnitNode()
  27. {
  28. unset(&dirty, (uint8_t**)&dirtyData, &dirtyLen);
  29. unset(&trs, (uint8_t**)&trsData, &trsLen);
  30. unset(&localMat, (uint8_t**)&localMatData, &localMatLen);
  31. unset(&worldMat, (uint8_t**)&worldMatData, &worldMatLen);
  32. unset(&parent, (uint8_t**)&parentData, &parentLen);
  33. unset(&zOrder, (uint8_t**)&zOrderData, &zOrderLen);
  34. unset(&cullingMask, (uint8_t**)&cullingMaskData, &cullingMaskLen);
  35. unset(&opacity, (uint8_t**)&opacityData, &opacityLen);
  36. unset(&is3D, (uint8_t**)&is3DData, &is3DLen);
  37. unset(&node, (uint8_t**)&nodeData, &nodeLen);
  38. unset(&skew, (uint8_t**)&skewData, &skewLen);
  39. }
  40. void UnitNode::setDirty(se::Object* jsData)
  41. {
  42. set(&dirty, (uint8_t**)&dirtyData, &dirtyLen, jsData);
  43. }
  44. void UnitNode::setTRS(se::Object* jsData)
  45. {
  46. set(&trs, (uint8_t**)&trsData, &trsLen, jsData);
  47. }
  48. void UnitNode::setLocalMat(se::Object* jsData)
  49. {
  50. set(&localMat, (uint8_t**)&localMatData, &localMatLen, jsData);
  51. }
  52. void UnitNode::setWorldMat(se::Object* jsData)
  53. {
  54. set(&worldMat, (uint8_t**)&worldMatData, &worldMatLen, jsData);
  55. }
  56. void UnitNode::setParent(se::Object* jsData)
  57. {
  58. set(&parent, (uint8_t**)&parentData, &parentLen, jsData);
  59. }
  60. void UnitNode::setZOrder(se::Object* jsData)
  61. {
  62. set(&zOrder, (uint8_t**)&zOrderData, &zOrderLen, jsData);
  63. }
  64. void UnitNode::setCullingMask(se::Object* jsData)
  65. {
  66. set(&cullingMask, (uint8_t**)&cullingMaskData, &cullingMaskLen, jsData);
  67. }
  68. void UnitNode::setOpacity(se::Object* jsData)
  69. {
  70. set(&opacity, (uint8_t**)&opacityData, &opacityLen, jsData);
  71. }
  72. void UnitNode::setIs3D(se::Object* jsData)
  73. {
  74. set(&is3D, (uint8_t**)&is3DData, &is3DLen, jsData);
  75. }
  76. void UnitNode::setNode(se::Object *jsData)
  77. {
  78. set(&node, (uint8_t**)&nodeData, &nodeLen, jsData);
  79. }
  80. void UnitNode::setSkew(se::Object* jsData)
  81. {
  82. set(&skew, (uint8_t**)&skewData, &skewLen, jsData);
  83. }
  84. NodeMemPool* NodeMemPool::_instance = nullptr;
  85. NodeMemPool::NodeMemPool()
  86. {
  87. _instance = this;
  88. }
  89. NodeMemPool::~NodeMemPool()
  90. {
  91. for(auto it = _nodePool.begin(); it != _nodePool.end(); it++)
  92. {
  93. if (*it)
  94. {
  95. delete (*it);
  96. }
  97. }
  98. _nodePool.clear();
  99. _instance = nullptr;
  100. }
  101. const std::vector<UnitNode*>& NodeMemPool::getNodePool() const
  102. {
  103. return _nodePool;
  104. }
  105. void NodeMemPool::removeNodeData(std::size_t unitID)
  106. {
  107. CCASSERT(unitID < _nodePool.size(), "NodeMemPool removeNodeData unitID can not be rather than pool size");
  108. auto unit = _nodePool[unitID];
  109. if (unit)
  110. {
  111. delete unit;
  112. _nodePool[unitID] = nullptr;
  113. }
  114. }
  115. UnitNode* NodeMemPool::getUnit(std::size_t unitID) const
  116. {
  117. CCASSERT(unitID < _nodePool.size(), "NodeMemPool getUnit unitID can not be rather than pool size");
  118. return _nodePool[unitID];
  119. }
  120. void NodeMemPool::updateNodeData(std::size_t unitID, se_object_ptr dirty, se_object_ptr trs, se_object_ptr localMat, se_object_ptr worldMat, se_object_ptr parent, se_object_ptr zOrder, se_object_ptr cullingMask, se_object_ptr opacity, se_object_ptr is3D, se_object_ptr node, se_object_ptr skew)
  121. {
  122. // UnitID may equal to node pool size, then node pool must increase size.
  123. CCASSERT(unitID <= _nodePool.size(), "NodeMemPool updateNodeData unitID can not be rather than pool size");
  124. UnitNode* unit = nullptr;
  125. if (unitID == _nodePool.size())
  126. {
  127. unit = new UnitNode;
  128. _nodePool.push_back(unit);
  129. }
  130. else if (unitID < _nodePool.size())
  131. {
  132. unit = _nodePool[unitID];
  133. if(!unit)
  134. {
  135. unit = new UnitNode;
  136. _nodePool[unitID] = unit;
  137. }
  138. }
  139. else
  140. {
  141. return;
  142. }
  143. unit->unitID = unitID;
  144. unit->setDirty(dirty);
  145. unit->setTRS(trs);
  146. unit->setLocalMat(localMat);
  147. unit->setWorldMat(worldMat);
  148. unit->setParent(parent);
  149. unit->setZOrder(zOrder);
  150. unit->setCullingMask(cullingMask);
  151. unit->setOpacity(opacity);
  152. unit->setIs3D(is3D);
  153. unit->setNode(node);
  154. unit->setSkew(skew);
  155. }
  156. RENDERER_END