AreaAllocator.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. //
  2. // Copyright (c) 2008-2017 the Urho3D project.
  3. //
  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. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include "../Precompiled.h"
  23. #include "../Math/AreaAllocator.h"
  24. #include "../DebugNew.h"
  25. namespace Atomic
  26. {
  27. AreaAllocator::AreaAllocator()
  28. {
  29. Reset(0, 0);
  30. }
  31. AreaAllocator::AreaAllocator(int width, int height, bool fastMode)
  32. {
  33. Reset(width, height, width, height, fastMode);
  34. }
  35. AreaAllocator::AreaAllocator(int width, int height, int maxWidth, int maxHeight, bool fastMode)
  36. {
  37. Reset(width, height, maxWidth, maxHeight, fastMode);
  38. }
  39. void AreaAllocator::Reset(int width, int height, int maxWidth, int maxHeight, bool fastMode)
  40. {
  41. doubleWidth_ = true;
  42. size_ = IntVector2(width, height);
  43. maxSize_ = IntVector2(maxWidth, maxHeight);
  44. fastMode_ = fastMode;
  45. freeAreas_.Clear();
  46. IntRect initialArea(0, 0, width, height);
  47. freeAreas_.Push(initialArea);
  48. }
  49. bool AreaAllocator::Allocate(int width, int height, int& x, int& y)
  50. {
  51. if (width < 0)
  52. width = 0;
  53. if (height < 0)
  54. height = 0;
  55. PODVector<IntRect>::Iterator best;
  56. int bestFreeArea;
  57. for (;;)
  58. {
  59. best = freeAreas_.End();
  60. bestFreeArea = M_MAX_INT;
  61. for (PODVector<IntRect>::Iterator i = freeAreas_.Begin(); i != freeAreas_.End(); ++i)
  62. {
  63. int freeWidth = i->Width();
  64. int freeHeight = i->Height();
  65. if (freeWidth >= width && freeHeight >= height)
  66. {
  67. // Calculate rank for free area. Lower is better
  68. int freeArea = freeWidth * freeHeight;
  69. if (freeArea < bestFreeArea)
  70. {
  71. best = i;
  72. bestFreeArea = freeArea;
  73. }
  74. }
  75. }
  76. if (best == freeAreas_.End())
  77. {
  78. if (doubleWidth_ && size_.x_ < maxSize_.x_)
  79. {
  80. int oldWidth = size_.x_;
  81. size_.x_ <<= 1;
  82. // If no allocations yet, simply expand the single free area
  83. IntRect& first = freeAreas_.Front();
  84. if (freeAreas_.Size() == 1 && first.left_ == 0 && first.top_ == 0 && first.right_ == oldWidth &&
  85. first.bottom_ == size_.y_)
  86. first.right_ = size_.x_;
  87. else
  88. {
  89. IntRect newArea(oldWidth, 0, size_.x_, size_.y_);
  90. freeAreas_.Push(newArea);
  91. }
  92. }
  93. else if (!doubleWidth_ && size_.y_ < maxSize_.y_)
  94. {
  95. int oldHeight = size_.y_;
  96. size_.y_ <<= 1;
  97. // If no allocations yet, simply expand the single free area
  98. IntRect& first = freeAreas_.Front();
  99. if (freeAreas_.Size() == 1 && first.left_ == 0 && first.top_ == 0 && first.right_ == size_.x_ &&
  100. first.bottom_ == oldHeight)
  101. first.bottom_ = size_.y_;
  102. else
  103. {
  104. IntRect newArea(0, oldHeight, size_.x_, size_.y_);
  105. freeAreas_.Push(newArea);
  106. }
  107. }
  108. else
  109. return false;
  110. doubleWidth_ = !doubleWidth_;
  111. }
  112. else
  113. break;
  114. }
  115. IntRect reserved(best->left_, best->top_, best->left_ + width, best->top_ + height);
  116. x = best->left_;
  117. y = best->top_;
  118. if (fastMode_)
  119. {
  120. // Reserve the area by splitting up the remaining free area
  121. best->left_ = reserved.right_;
  122. if (best->Height() > 2 * height || height >= size_.y_ / 2)
  123. {
  124. IntRect splitArea(reserved.left_, reserved.bottom_, best->right_, best->bottom_);
  125. best->bottom_ = reserved.bottom_;
  126. freeAreas_.Push(splitArea);
  127. }
  128. }
  129. else
  130. {
  131. // Remove the reserved area from all free areas
  132. for (unsigned i = 0; i < freeAreas_.Size();)
  133. {
  134. if (SplitRect(i, reserved))
  135. freeAreas_.Erase(i);
  136. else
  137. ++i;
  138. }
  139. Cleanup();
  140. }
  141. return true;
  142. }
  143. bool AreaAllocator::SplitRect(unsigned freeAreaIndex, const IntRect& reserve)
  144. {
  145. // Make a copy, as the vector will be modified
  146. IntRect original = freeAreas_[freeAreaIndex];
  147. if (reserve.right_ > original.left_ && reserve.left_ < original.right_ && reserve.bottom_ > original.top_ &&
  148. reserve.top_ < original.bottom_)
  149. {
  150. // Check for splitting from the right
  151. if (reserve.right_ < original.right_)
  152. {
  153. IntRect newRect = original;
  154. newRect.left_ = reserve.right_;
  155. freeAreas_.Push(newRect);
  156. }
  157. // Check for splitting from the left
  158. if (reserve.left_ > original.left_)
  159. {
  160. IntRect newRect = original;
  161. newRect.right_ = reserve.left_;
  162. freeAreas_.Push(newRect);
  163. }
  164. // Check for splitting from the bottom
  165. if (reserve.bottom_ < original.bottom_)
  166. {
  167. IntRect newRect = original;
  168. newRect.top_ = reserve.bottom_;
  169. freeAreas_.Push(newRect);
  170. }
  171. // Check for splitting from the top
  172. if (reserve.top_ > original.top_)
  173. {
  174. IntRect newRect = original;
  175. newRect.bottom_ = reserve.top_;
  176. freeAreas_.Push(newRect);
  177. }
  178. return true;
  179. }
  180. return false;
  181. }
  182. void AreaAllocator::Cleanup()
  183. {
  184. // Remove rects which are contained within another rect
  185. for (unsigned i = 0; i < freeAreas_.Size();)
  186. {
  187. bool erased = false;
  188. for (unsigned j = i + 1; j < freeAreas_.Size();)
  189. {
  190. if ((freeAreas_[i].left_ >= freeAreas_[j].left_) &&
  191. (freeAreas_[i].top_ >= freeAreas_[j].top_) &&
  192. (freeAreas_[i].right_ <= freeAreas_[j].right_) &&
  193. (freeAreas_[i].bottom_ <= freeAreas_[j].bottom_))
  194. {
  195. freeAreas_.Erase(i);
  196. erased = true;
  197. break;
  198. }
  199. if ((freeAreas_[j].left_ >= freeAreas_[i].left_) &&
  200. (freeAreas_[j].top_ >= freeAreas_[i].top_) &&
  201. (freeAreas_[j].right_ <= freeAreas_[i].right_) &&
  202. (freeAreas_[j].bottom_ <= freeAreas_[i].bottom_))
  203. freeAreas_.Erase(j);
  204. else
  205. ++j;
  206. }
  207. if (!erased)
  208. ++i;
  209. }
  210. }
  211. }