SpriteSet.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  1. #include <algorithm>
  2. #include "../../../Include/RmlUi/Core/Types.h"
  3. #include "SpriteSet.h"
  4. namespace Rml {
  5. namespace {
  6. constexpr unsigned int null_index = static_cast<unsigned int>(-1);
  7. constexpr unsigned int max_changed_pixels = 256 * 256;
  8. constexpr unsigned int split_threshold = 8;
  9. template<typename T>
  10. void InitializePool(Vector<T> &pool)
  11. {
  12. const unsigned int pool_size = static_cast<unsigned int>(pool.size());
  13. const unsigned int last_pool_index = pool_size - 1;
  14. for (unsigned int i = 0; i < last_pool_index; ++i)
  15. pool[i].next_index = i + 1;
  16. pool[last_pool_index].next_index = null_index;
  17. }
  18. template<typename T>
  19. unsigned int AllocateEntry(Vector<T>& pool, unsigned int& next_free_index)
  20. {
  21. if (next_free_index == null_index)
  22. {
  23. const unsigned int old_pool_size = static_cast<unsigned int>(pool.size());
  24. const unsigned int new_pool_size = old_pool_size << 1;
  25. const unsigned int last_pool_index = new_pool_size - 1;
  26. pool.resize(new_pool_size);
  27. for (unsigned int i = old_pool_size; i < last_pool_index; ++i)
  28. pool[i].next_index = i + 1;
  29. pool[last_pool_index].next_index = null_index;
  30. next_free_index = old_pool_size;
  31. }
  32. const unsigned int index = next_free_index;
  33. next_free_index = pool[index].next_index;
  34. return index;
  35. }
  36. template<typename T>
  37. void FreeEntry(Vector<T>& pool, unsigned int& next_free_index, const unsigned int index)
  38. {
  39. pool[index].next_index = next_free_index;
  40. next_free_index = index;
  41. }
  42. }
  43. SpriteSet::SpriteSet(
  44. const unsigned int bytes_per_pixel, const unsigned int page_size, const unsigned int sprite_padding
  45. ) : bytes_per_pixel(bytes_per_pixel), page_size(page_size), sprite_padding(sprite_padding)
  46. {
  47. InitializePool(page_pool);
  48. InitializePool(shelf_pool);
  49. InitializePool(slot_pool);
  50. }
  51. void SpriteSet::Tick()
  52. {
  53. unsigned int changed_pixels = 0;
  54. // Try compaction by moving sprites from the last page to the first page.
  55. while (changed_pixels <= max_changed_pixels && first_page_index != last_page_index)
  56. {
  57. const Page& source_page = page_pool[last_page_index];
  58. unsigned int source_shelf_index = source_page.first_shelf_index;
  59. while (!shelf_pool[source_shelf_index].allocated)
  60. source_shelf_index = shelf_pool[source_shelf_index].next_index;
  61. const Shelf& source_shelf = shelf_pool[source_shelf_index];
  62. unsigned int source_slot_index = source_shelf.first_slot_index;
  63. while (!slot_pool[source_slot_index].allocated)
  64. source_slot_index = slot_pool[source_slot_index].next_index;
  65. const Slot& source_slot = slot_pool[source_slot_index];
  66. const unsigned int destination_slot_index = TryAllocateInPage(
  67. first_page_index, source_slot.width, source_slot.height
  68. );
  69. if (destination_slot_index == null_index)
  70. break;
  71. const Slot& destination_slot = slot_pool[destination_slot_index];
  72. const Shelf& destination_shelf = shelf_pool[destination_slot.shelf_index];
  73. Page& destination_page = page_pool[first_page_index];
  74. const unsigned int source_x = source_slot.x;
  75. const unsigned int source_y = source_shelf.y;
  76. const unsigned int destination_x = destination_slot.x;
  77. const unsigned int destination_y = destination_shelf.y;
  78. const unsigned int width = source_slot.width;
  79. const unsigned int height = source_slot.height;
  80. for (unsigned int local_y = 0; local_y != height; ++local_y)
  81. {
  82. const auto data_start = source_page.texture_data->begin() + ((source_y + local_y) * page_size + source_x);
  83. std::copy(
  84. data_start, data_start + width,
  85. destination_page.texture_data->begin() + ((destination_y + local_y) * page_size + destination_x)
  86. );
  87. }
  88. destination_page.first_dirty_y = std::min(destination_page.first_dirty_y, destination_y);
  89. destination_page.past_last_dirty_y = std::max(destination_page.past_last_dirty_y, destination_y + height);
  90. destination_page.first_dirty_x = std::min(destination_page.first_dirty_x, destination_x);
  91. destination_page.past_last_dirty_x = std::max(destination_page.past_last_dirty_x, destination_x + width);
  92. changed_pixels += Remove(source_slot_index);
  93. if (migration_callback)
  94. migration_callback(source_slot_index, {destination_slot_index, destination_slot.epoch});
  95. }
  96. }
  97. SpriteSet::Handle SpriteSet::Add(
  98. const unsigned int width, const unsigned int height, const unsigned char* const data, const unsigned int row_stride
  99. )
  100. {
  101. const unsigned int padded_width = width + sprite_padding * 2;
  102. const unsigned int padded_height = height + sprite_padding * 2;
  103. const unsigned int slot_index = Allocate(padded_width, padded_height);
  104. const Slot& slot = slot_pool[slot_index];
  105. const Shelf& shelf = shelf_pool[slot.shelf_index];
  106. Page& page = page_pool[shelf.page_index];
  107. for (
  108. unsigned int i = 0, top_padding_y = shelf.y, bottom_padding_y = shelf.y + padded_height - 1;
  109. i < sprite_padding; ++i, ++top_padding_y, --bottom_padding_y
  110. )
  111. {
  112. auto texture_start = page.texture_data->begin() + (top_padding_y * page_size + slot.x) * bytes_per_pixel;
  113. std::fill(texture_start, texture_start + padded_width * bytes_per_pixel, 0);
  114. texture_start = page.texture_data->begin() + (bottom_padding_y * page_size + slot.x) * bytes_per_pixel;
  115. std::fill(texture_start, texture_start + padded_width * bytes_per_pixel, 0);
  116. }
  117. const unsigned int texture_y = shelf.y + sprite_padding;
  118. for (unsigned int local_y = 0; local_y != height; ++local_y)
  119. {
  120. const unsigned char* const data_start = data + local_y * row_stride * bytes_per_pixel;
  121. const auto texture_start = page.texture_data->begin()
  122. + ((texture_y + local_y) * page_size + slot.x) * bytes_per_pixel;
  123. std::fill(texture_start, texture_start + sprite_padding * bytes_per_pixel, 0);
  124. std::fill(
  125. texture_start + (sprite_padding + width) * bytes_per_pixel, texture_start + padded_width * bytes_per_pixel, 0
  126. );
  127. std::copy(data_start, data_start + width * bytes_per_pixel, texture_start + sprite_padding * bytes_per_pixel);
  128. }
  129. page.first_dirty_y = std::min(page.first_dirty_y, shelf.y);
  130. page.past_last_dirty_y = std::max(page.past_last_dirty_y, shelf.y + padded_height);
  131. page.first_dirty_x = std::min(page.first_dirty_x, slot.x);
  132. page.past_last_dirty_x = std::max(page.past_last_dirty_x, slot.x + padded_width);
  133. return {slot_index, slot.epoch};
  134. }
  135. unsigned int SpriteSet::Allocate(const unsigned int width, const unsigned int height)
  136. {
  137. // Try to allocate in an existing page.
  138. if (first_page_index != null_index)
  139. {
  140. unsigned int page_index = first_page_index;
  141. while (true)
  142. {
  143. const unsigned int slot_index = TryAllocateInPage(page_index, width, height);
  144. if (slot_index != null_index)
  145. return slot_index;
  146. if (page_index == last_page_index)
  147. break;
  148. page_index = page_pool[page_index].next_index;
  149. }
  150. }
  151. // No page could fit, allocate a new page.
  152. if (first_page_index == null_index)
  153. {
  154. first_page_index = last_page_index;
  155. Page& page = page_pool[last_page_index];
  156. page.previous_index = null_index;
  157. }
  158. else
  159. {
  160. unsigned int page_index = page_pool[last_page_index].next_index;
  161. if (page_index == null_index)
  162. {
  163. const unsigned int old_pool_size = static_cast<unsigned int>(page_pool.size());
  164. const unsigned int new_pool_size = old_pool_size << 1;
  165. const unsigned int last_pool_index = new_pool_size - 1;
  166. page_pool.resize(new_pool_size);
  167. for (unsigned int i = old_pool_size; i != last_pool_index; ++i)
  168. page_pool[i].next_index = i + 1;
  169. page_pool[last_pool_index].next_index = null_index;
  170. page_pool[last_page_index].next_index = old_pool_size;
  171. page_index = old_pool_size;
  172. }
  173. page_pool[page_index].previous_index = last_page_index;
  174. last_page_index = page_index;
  175. }
  176. Page& page = page_pool[last_page_index];
  177. page.texture_id = page_count;
  178. page.texture_data = MakeUnique<Vector<unsigned char>>(page_size * page_size * bytes_per_pixel);
  179. page.first_dirty_y = page_size;
  180. page.past_last_dirty_y = 0;
  181. page.first_dirty_x = page_size;
  182. page.past_last_dirty_x = 0;
  183. const unsigned int shelf_index = AllocateEntry<Shelf>(shelf_pool, next_free_shelf_index);
  184. const unsigned int slot_index = AllocateEntry<Slot>(slot_pool, next_free_slot_index);
  185. slot_pool[slot_index] = {shelf_index, page_count, 0, 0, page_size, 0, 0, null_index, null_index, null_index, null_index, 0, false};
  186. shelf_pool[shelf_index] = {last_page_index, 0, page_size, null_index, null_index, slot_index, slot_index, false};
  187. page.first_shelf_index = shelf_index;
  188. ++page_count;
  189. return TryAllocateInPage(last_page_index, width, height);
  190. }
  191. unsigned int SpriteSet::TryAllocateInPage(const unsigned int page_index, const unsigned int width, const unsigned int height)
  192. {
  193. Page& page = page_pool[page_index];
  194. unsigned int selected_shelf_index = null_index;
  195. unsigned int selected_slot_index = null_index;
  196. unsigned int selected_shelf_height = static_cast<unsigned int>(-1);
  197. for (
  198. unsigned int shelf_index = page.first_shelf_index;
  199. shelf_index != null_index; shelf_index = shelf_pool[shelf_index].next_index
  200. )
  201. {
  202. const Shelf& shelf = shelf_pool[shelf_index];
  203. if (
  204. shelf.height < height || shelf.height >= selected_shelf_height
  205. || (shelf.allocated && shelf.height > height * 3 / 2)
  206. )
  207. continue;
  208. bool found = false;
  209. for (
  210. unsigned int slot_index = shelf.first_free_slot_index;
  211. slot_index != null_index; slot_index = slot_pool[slot_index].next_free_index
  212. )
  213. {
  214. const Slot& slot = slot_pool[slot_index];
  215. if (slot.width < width)
  216. continue;
  217. selected_shelf_index = shelf_index;
  218. selected_slot_index = slot_index;
  219. selected_shelf_height = shelf.height;
  220. found = true;
  221. break;
  222. }
  223. if (found && shelf.height == height)
  224. break;
  225. }
  226. if (selected_slot_index == null_index)
  227. return null_index;
  228. Shelf* shelf = &shelf_pool[selected_shelf_index];
  229. if (!shelf->allocated)
  230. {
  231. shelf->allocated = true;
  232. if (shelf->height - height >= split_threshold)
  233. {
  234. const unsigned int new_shelf_index = AllocateEntry<Shelf>(shelf_pool, next_free_shelf_index);
  235. const unsigned int new_slot_index = AllocateEntry<Slot>(slot_pool, next_free_slot_index);
  236. shelf = &shelf_pool[selected_shelf_index];
  237. slot_pool[new_slot_index] = {
  238. new_shelf_index, page.texture_id, 0, shelf->y + height, page_size, 0, 0, null_index, null_index, null_index, null_index, 0, false
  239. };
  240. shelf_pool[new_shelf_index] = {
  241. page_index, shelf->y + height, shelf->height - height,
  242. selected_shelf_index, shelf->next_index, new_slot_index, new_slot_index, false
  243. };
  244. if (shelf->next_index != null_index)
  245. shelf_pool[shelf->next_index].previous_index = new_shelf_index;
  246. shelf->next_index = new_shelf_index;
  247. shelf->height = height;
  248. }
  249. }
  250. Slot* slot = &slot_pool[selected_slot_index];
  251. if (slot->width - width >= split_threshold)
  252. {
  253. const unsigned int new_slot_index = AllocateEntry<Slot>(slot_pool, next_free_slot_index);
  254. slot = &slot_pool[selected_slot_index];
  255. slot_pool[new_slot_index] = {
  256. selected_shelf_index, page.texture_id, slot->x + width, shelf->y, slot->width - width, 0, 0,
  257. selected_slot_index, slot->next_index, slot->previous_free_index, slot->next_free_index, 0, false
  258. };
  259. if (slot->next_index != null_index)
  260. slot_pool[slot->next_index].previous_index = new_slot_index;
  261. slot->next_index = new_slot_index;
  262. if (slot->previous_free_index == null_index)
  263. shelf->first_free_slot_index = new_slot_index;
  264. else
  265. slot_pool[slot->previous_free_index].next_free_index = new_slot_index;
  266. if (slot->next_free_index != null_index)
  267. slot_pool[slot->next_free_index].previous_free_index = new_slot_index;
  268. slot->width = width;
  269. }
  270. else
  271. {
  272. if (slot->previous_free_index == null_index)
  273. shelf->first_free_slot_index = slot->next_free_index;
  274. else
  275. slot_pool[slot->previous_free_index].next_free_index = slot->next_free_index;
  276. if (slot->next_free_index != null_index)
  277. slot_pool[slot->next_free_index].previous_free_index = slot->previous_free_index;
  278. }
  279. slot->allocated = true;
  280. slot->actual_width = width;
  281. slot->height = height;
  282. slot->epoch = current_epoch;
  283. if (page_index == first_page_index)
  284. first_page_allocated_pixels += width * shelf->height;
  285. return selected_slot_index;
  286. }
  287. unsigned int SpriteSet::Remove(const unsigned int slot_index)
  288. {
  289. Slot& slot = slot_pool[slot_index];
  290. Shelf& shelf = shelf_pool[slot.shelf_index];
  291. const unsigned int page_index = shelf.page_index;
  292. Page& page = page_pool[page_index];
  293. /* DEBUG: Fill the removed area with blue.
  294. for (unsigned int offset_y = 0; offset_y < slot.height; ++offset_y)
  295. {
  296. for (unsigned int offset_x = 0; offset_x < slot.width; ++offset_x)
  297. {
  298. const auto pixel = page.texture_data->begin() + ((slot.y + offset_y) * page_size + slot.x + offset_x) * 4;
  299. pixel[0] = 0;
  300. pixel[1] = 0;
  301. pixel[2] = 255;
  302. pixel[3] = 255;
  303. }
  304. }
  305. */
  306. const unsigned int slot_pixels = slot.width * slot.height;
  307. slot.allocated = false;
  308. slot.previous_free_index = null_index;
  309. if (shelf.first_free_slot_index == null_index)
  310. {
  311. slot.next_free_index = null_index;
  312. }
  313. else
  314. {
  315. slot.next_free_index = shelf.first_free_slot_index;
  316. slot_pool[shelf.first_free_slot_index].previous_free_index = slot_index;
  317. }
  318. shelf.first_free_slot_index = slot_index;
  319. ++slot.epoch;
  320. // Merge consecutive empty slots.
  321. if (slot.next_index != null_index)
  322. {
  323. Slot& next_slot = slot_pool[slot.next_index];
  324. if (!next_slot.allocated)
  325. {
  326. slot.width += next_slot.width;
  327. const unsigned int next_index = slot.next_index;
  328. slot.next_index = next_slot.next_index;
  329. if (next_slot.previous_free_index != null_index)
  330. slot_pool[next_slot.previous_free_index].next_free_index = next_slot.next_free_index;
  331. if (next_slot.next_free_index != null_index)
  332. slot_pool[next_slot.next_free_index].previous_free_index = next_slot.previous_free_index;
  333. FreeEntry<Slot>(slot_pool, next_free_slot_index, next_index);
  334. if (slot.next_index != null_index)
  335. slot_pool[slot.next_index].previous_index = slot_index;
  336. }
  337. }
  338. if (slot.previous_index != null_index)
  339. {
  340. Slot& previous_slot = slot_pool[slot.previous_index];
  341. if (!previous_slot.allocated)
  342. {
  343. slot.x -= previous_slot.width;
  344. slot.width += previous_slot.width;
  345. const unsigned int previous_index = slot.previous_index;
  346. slot.previous_index = previous_slot.previous_index;
  347. if (previous_slot.previous_free_index != null_index)
  348. slot_pool[previous_slot.previous_free_index].next_free_index = previous_slot.next_free_index;
  349. if (previous_slot.next_free_index != null_index)
  350. slot_pool[previous_slot.next_free_index].previous_free_index = previous_slot.previous_free_index;
  351. FreeEntry<Slot>(slot_pool, next_free_slot_index, previous_index);
  352. if (slot.previous_index == null_index) {
  353. shelf.first_slot_index = slot_index;
  354. if (slot.next_index == null_index)
  355. shelf.allocated = false;
  356. }
  357. else
  358. {
  359. slot_pool[slot.previous_index].next_index = slot_index;
  360. }
  361. }
  362. }
  363. // Merge consecutive empty shelves.
  364. if (shelf.allocated)
  365. return slot_pixels;
  366. if (shelf.next_index != null_index)
  367. {
  368. Shelf& next_shelf = shelf_pool[shelf.next_index];
  369. if (!next_shelf.allocated)
  370. {
  371. shelf.height += next_shelf.height;
  372. const unsigned int next_index = shelf.next_index;
  373. shelf.next_index = next_shelf.next_index;
  374. FreeEntry<Slot>(slot_pool, next_free_slot_index, next_shelf.first_slot_index);
  375. FreeEntry<Shelf>(shelf_pool, next_free_shelf_index, next_index);
  376. if (shelf.next_index != null_index)
  377. shelf_pool[shelf.next_index].previous_index = slot.shelf_index;
  378. }
  379. }
  380. if (shelf.previous_index != null_index)
  381. {
  382. Shelf& previous_shelf = shelf_pool[shelf.previous_index];
  383. if (!previous_shelf.allocated)
  384. {
  385. shelf.y -= previous_shelf.height;
  386. shelf.height += previous_shelf.height;
  387. slot.y = shelf.y;
  388. const unsigned int previous_index = shelf.previous_index;
  389. shelf.previous_index = previous_shelf.previous_index;
  390. FreeEntry<Slot>(slot_pool, next_free_slot_index, previous_shelf.first_slot_index);
  391. FreeEntry<Shelf>(shelf_pool, next_free_shelf_index, previous_index);
  392. if (shelf.previous_index == null_index)
  393. page.first_shelf_index = slot.shelf_index;
  394. else
  395. shelf_pool[shelf.previous_index].next_index = slot.shelf_index;
  396. }
  397. }
  398. // Deallocate the page if it becomes empty, except when it's the first one.
  399. if (page_index == first_page_index)
  400. {
  401. first_page_allocated_pixels -= slot.width * shelf.height;
  402. return slot_pixels;
  403. }
  404. if (shelf.height != page_size)
  405. return slot_pixels;
  406. FreeEntry<Slot>(slot_pool, next_free_slot_index, slot_index);
  407. FreeEntry<Shelf>(shelf_pool, next_free_shelf_index, page.first_shelf_index);
  408. page.texture_data.reset();
  409. page_pool[page.previous_index].next_index = page.next_index;
  410. if (page_index != last_page_index)
  411. page_pool[page.next_index].previous_index = page.previous_index;
  412. page_pool[last_page_index].next_index = page_index;
  413. return slot_pixels;
  414. }
  415. void SpriteSet::Remove(const Handle handle)
  416. {
  417. Slot& slot = slot_pool[handle.slot_index];
  418. if (slot.epoch != handle.epoch)
  419. return;
  420. Remove(handle.slot_index);
  421. }
  422. SpriteSet::SpriteData SpriteSet::Get(const Handle handle) const
  423. {
  424. const Slot& slot = slot_pool[handle.slot_index];
  425. return {
  426. slot.texture_id, slot.x + sprite_padding, slot.y + sprite_padding,
  427. slot.actual_width - sprite_padding * 2, slot.height - sprite_padding * 2
  428. };
  429. }
  430. Vector<const unsigned char*> SpriteSet::GetTextures() const
  431. {
  432. if (first_page_index == null_index)
  433. return {};
  434. Vector<const unsigned char*> textures;
  435. unsigned int page_index = first_page_index;
  436. while (true)
  437. {
  438. const Page& page = page_pool[page_index];
  439. textures.push_back(page.texture_data->data());
  440. if (page_index == last_page_index)
  441. break;
  442. page_index = page.next_index;
  443. }
  444. return textures;
  445. }
  446. void SpriteSet::Dump(Vector<SpriteSet::SpriteData> &sprites) const
  447. {
  448. if (first_page_index == null_index)
  449. return;
  450. for (
  451. unsigned int shelf_index = page_pool[first_page_index].first_shelf_index;
  452. shelf_index != null_index; shelf_index = shelf_pool[shelf_index].next_index
  453. )
  454. {
  455. const unsigned int shelfY = shelf_pool[shelf_index].y;
  456. for (
  457. unsigned int slot_index = shelf_pool[shelf_index].first_slot_index;
  458. slot_index != null_index; slot_index = slot_pool[slot_index].next_index
  459. )
  460. {
  461. const Slot& slot = slot_pool[slot_index];
  462. if (slot.allocated)
  463. sprites.push_back({slot.x, shelfY, slot.width, slot.height});
  464. }
  465. }
  466. }
  467. } // namespace Rml