AreaAllocator.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. //
  2. // Copyright (c) 2008-2015 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. namespace Atomic
  25. {
  26. AreaAllocator::AreaAllocator()
  27. {
  28. Reset(0, 0);
  29. }
  30. AreaAllocator::AreaAllocator(int width, int height, bool fastMode)
  31. {
  32. Reset(width, height, width, height, fastMode);
  33. }
  34. AreaAllocator::AreaAllocator(int width, int height, int maxWidth, int maxHeight, bool fastMode)
  35. {
  36. Reset(width, height, maxWidth, maxHeight, fastMode);
  37. }
  38. void AreaAllocator::Reset(int width, int height, int maxWidth, int maxHeight, bool fastMode)
  39. {
  40. doubleWidth_ = true;
  41. size_ = IntVector2(width, height);
  42. maxSize_ = IntVector2(maxWidth, maxHeight);
  43. fastMode_ = fastMode;
  44. freeAreas_.Clear();
  45. IntRect initialArea(0, 0, width, height);
  46. freeAreas_.Push(initialArea);
  47. }
  48. bool AreaAllocator::Allocate(int width, int height, int& x, int& y)
  49. {
  50. if (width < 0)
  51. width = 0;
  52. if (height < 0)
  53. height = 0;
  54. PODVector<IntRect>::Iterator best;
  55. int bestFreeArea;
  56. for (;;)
  57. {
  58. best = freeAreas_.End();
  59. bestFreeArea = M_MAX_INT;
  60. for (PODVector<IntRect>::Iterator i = freeAreas_.Begin(); i != freeAreas_.End(); ++i)
  61. {
  62. int freeWidth = i->Width();
  63. int freeHeight = i->Height();
  64. if (freeWidth >= width && freeHeight >= height)
  65. {
  66. // Calculate rank for free area. Lower is better
  67. int freeArea = freeWidth * freeHeight;
  68. if (freeArea < bestFreeArea)
  69. {
  70. best = i;
  71. bestFreeArea = freeArea;
  72. }
  73. }
  74. }
  75. if (best == freeAreas_.End())
  76. {
  77. if (doubleWidth_ && size_.x_ < maxSize_.x_)
  78. {
  79. int oldWidth = size_.x_;
  80. size_.x_ <<= 1;
  81. // If no allocations yet, simply expand the single free area
  82. IntRect& first = freeAreas_.Front();
  83. if (freeAreas_.Size() == 1 && first.left_ == 0 && first.top_ == 0 && first.right_ == oldWidth &&
  84. first.bottom_ == size_.y_)
  85. first.right_ = size_.x_;
  86. else
  87. {
  88. IntRect newArea(oldWidth, 0, size_.x_, size_.y_);
  89. freeAreas_.Push(newArea);
  90. }
  91. }
  92. else if (!doubleWidth_ && size_.y_ < maxSize_.y_)
  93. {
  94. int oldHeight = size_.y_;
  95. size_.y_ <<= 1;
  96. // If no allocations yet, simply expand the single free area
  97. IntRect& first = freeAreas_.Front();
  98. if (freeAreas_.Size() == 1 && first.left_ == 0 && first.top_ == 0 && first.right_ == size_.x_ &&
  99. first.bottom_ == oldHeight)
  100. first.bottom_ = size_.y_;
  101. else
  102. {
  103. IntRect newArea(0, oldHeight, size_.x_, size_.y_);
  104. freeAreas_.Push(newArea);
  105. }
  106. }
  107. else
  108. return false;
  109. doubleWidth_ = !doubleWidth_;
  110. }
  111. else
  112. break;
  113. }
  114. IntRect reserved(best->left_, best->top_, best->left_ + width, best->top_ + height);
  115. x = best->left_;
  116. y = best->top_;
  117. if (fastMode_)
  118. {
  119. // Reserve the area by splitting up the remaining free area
  120. best->left_ = reserved.right_;
  121. if (best->Height() > 2 * height || height >= size_.y_ / 2)
  122. {
  123. IntRect splitArea(reserved.left_, reserved.bottom_, best->right_, best->bottom_);
  124. best->bottom_ = reserved.bottom_;
  125. freeAreas_.Push(splitArea);
  126. }
  127. }
  128. else
  129. {
  130. // Remove the reserved area from all free areas
  131. for (unsigned i = 0; i < freeAreas_.Size();)
  132. {
  133. if (SplitRect(freeAreas_[i], reserved))
  134. freeAreas_.Erase(i);
  135. else
  136. ++i;
  137. }
  138. Cleanup();
  139. }
  140. return true;
  141. }
  142. bool AreaAllocator::SplitRect(IntRect original, const IntRect& reserve)
  143. {
  144. if (reserve.right_ > original.left_ && reserve.left_ < original.right_ && reserve.bottom_ > original.top_ &&
  145. reserve.top_ < original.bottom_)
  146. {
  147. // Check for splitting from the right
  148. if (reserve.right_ < original.right_)
  149. {
  150. IntRect newRect = original;
  151. newRect.left_ = reserve.right_;
  152. freeAreas_.Push(newRect);
  153. }
  154. // Check for splitting from the left
  155. if (reserve.left_ > original.left_)
  156. {
  157. IntRect newRect = original;
  158. newRect.right_ = reserve.left_;
  159. freeAreas_.Push(newRect);
  160. }
  161. // Check for splitting from the bottom
  162. if (reserve.bottom_ < original.bottom_)
  163. {
  164. IntRect newRect = original;
  165. newRect.top_ = reserve.bottom_;
  166. freeAreas_.Push(newRect);
  167. }
  168. // Check for splitting from the top
  169. if (reserve.top_ > original.top_)
  170. {
  171. IntRect newRect = original;
  172. newRect.bottom_ = reserve.top_;
  173. freeAreas_.Push(newRect);
  174. }
  175. return true;
  176. }
  177. return false;
  178. }
  179. void AreaAllocator::Cleanup()
  180. {
  181. // Remove rects which are contained within another rect
  182. for (unsigned i = 0; i < freeAreas_.Size();)
  183. {
  184. bool erased = false;
  185. for (unsigned j = i + 1; j < freeAreas_.Size();)
  186. {
  187. if ((freeAreas_[i].left_ >= freeAreas_[j].left_) &&
  188. (freeAreas_[i].top_ >= freeAreas_[j].top_) &&
  189. (freeAreas_[i].right_ <= freeAreas_[j].right_) &&
  190. (freeAreas_[i].bottom_ <= freeAreas_[j].bottom_))
  191. {
  192. freeAreas_.Erase(i);
  193. erased = true;
  194. break;
  195. }
  196. if ((freeAreas_[j].left_ >= freeAreas_[i].left_) &&
  197. (freeAreas_[j].top_ >= freeAreas_[i].top_) &&
  198. (freeAreas_[j].right_ <= freeAreas_[i].right_) &&
  199. (freeAreas_[j].bottom_ <= freeAreas_[i].bottom_))
  200. freeAreas_.Erase(j);
  201. else
  202. ++j;
  203. }
  204. if (!erased)
  205. ++i;
  206. }
  207. }
  208. }