SpriteSet.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  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, static_cast<unsigned char>(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, static_cast<unsigned char>(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, static_cast<unsigned char>(0));
  124. std::fill(
  125. texture_start + (sprite_padding + width) * bytes_per_pixel, texture_start + padded_width * bytes_per_pixel,
  126. static_cast<unsigned char>(0)
  127. );
  128. std::copy(data_start, data_start + width * bytes_per_pixel, texture_start + sprite_padding * bytes_per_pixel);
  129. }
  130. page.first_dirty_y = std::min(page.first_dirty_y, shelf.y);
  131. page.past_last_dirty_y = std::max(page.past_last_dirty_y, shelf.y + padded_height);
  132. page.first_dirty_x = std::min(page.first_dirty_x, slot.x);
  133. page.past_last_dirty_x = std::max(page.past_last_dirty_x, slot.x + padded_width);
  134. return {slot_index, slot.epoch};
  135. }
  136. unsigned int SpriteSet::Allocate(const unsigned int width, const unsigned int height)
  137. {
  138. // Try to allocate in an existing page.
  139. if (first_page_index != null_index)
  140. {
  141. unsigned int page_index = first_page_index;
  142. while (true)
  143. {
  144. const unsigned int slot_index = TryAllocateInPage(page_index, width, height);
  145. if (slot_index != null_index)
  146. return slot_index;
  147. if (page_index == last_page_index)
  148. break;
  149. page_index = page_pool[page_index].next_index;
  150. }
  151. }
  152. // No page could fit, allocate a new page.
  153. if (first_page_index == null_index)
  154. {
  155. first_page_index = last_page_index;
  156. Page& page = page_pool[last_page_index];
  157. page.previous_index = null_index;
  158. }
  159. else
  160. {
  161. unsigned int page_index = page_pool[last_page_index].next_index;
  162. if (page_index == null_index)
  163. {
  164. const unsigned int old_pool_size = static_cast<unsigned int>(page_pool.size());
  165. const unsigned int new_pool_size = old_pool_size << 1;
  166. const unsigned int last_pool_index = new_pool_size - 1;
  167. page_pool.resize(new_pool_size);
  168. for (unsigned int i = old_pool_size; i != last_pool_index; ++i)
  169. page_pool[i].next_index = i + 1;
  170. page_pool[last_pool_index].next_index = null_index;
  171. page_pool[last_page_index].next_index = old_pool_size;
  172. page_index = old_pool_size;
  173. }
  174. page_pool[page_index].previous_index = last_page_index;
  175. last_page_index = page_index;
  176. }
  177. Page& page = page_pool[last_page_index];
  178. page.texture_id = page_count;
  179. page.texture_data = MakeUnique<Vector<unsigned char>>(page_size * page_size * bytes_per_pixel);
  180. page.first_dirty_y = page_size;
  181. page.past_last_dirty_y = 0;
  182. page.first_dirty_x = page_size;
  183. page.past_last_dirty_x = 0;
  184. const unsigned int shelf_index = AllocateEntry<Shelf>(shelf_pool, next_free_shelf_index);
  185. const unsigned int slot_index = AllocateEntry<Slot>(slot_pool, next_free_slot_index);
  186. slot_pool[slot_index] = {shelf_index, page_count, 0, 0, page_size, 0, 0, null_index, null_index, null_index, null_index, 0, false};
  187. shelf_pool[shelf_index] = {last_page_index, 0, page_size, null_index, null_index, slot_index, slot_index, false};
  188. page.first_shelf_index = shelf_index;
  189. ++page_count;
  190. return TryAllocateInPage(last_page_index, width, height);
  191. }
  192. unsigned int SpriteSet::TryAllocateInPage(const unsigned int page_index, const unsigned int width, const unsigned int height)
  193. {
  194. Page& page = page_pool[page_index];
  195. unsigned int selected_shelf_index = null_index;
  196. unsigned int selected_slot_index = null_index;
  197. unsigned int selected_shelf_height = static_cast<unsigned int>(-1);
  198. for (
  199. unsigned int shelf_index = page.first_shelf_index;
  200. shelf_index != null_index; shelf_index = shelf_pool[shelf_index].next_index
  201. )
  202. {
  203. const Shelf& shelf = shelf_pool[shelf_index];
  204. if (
  205. shelf.height < height || shelf.height >= selected_shelf_height
  206. || (shelf.allocated && shelf.height > height * 3 / 2)
  207. )
  208. continue;
  209. bool found = false;
  210. for (
  211. unsigned int slot_index = shelf.first_free_slot_index;
  212. slot_index != null_index; slot_index = slot_pool[slot_index].next_free_index
  213. )
  214. {
  215. const Slot& slot = slot_pool[slot_index];
  216. if (slot.width < width)
  217. continue;
  218. selected_shelf_index = shelf_index;
  219. selected_slot_index = slot_index;
  220. selected_shelf_height = shelf.height;
  221. found = true;
  222. break;
  223. }
  224. if (found && shelf.height == height)
  225. break;
  226. }
  227. if (selected_slot_index == null_index)
  228. return null_index;
  229. Shelf* shelf = &shelf_pool[selected_shelf_index];
  230. if (!shelf->allocated)
  231. {
  232. shelf->allocated = true;
  233. if (shelf->height - height >= split_threshold)
  234. {
  235. const unsigned int new_shelf_index = AllocateEntry<Shelf>(shelf_pool, next_free_shelf_index);
  236. const unsigned int new_slot_index = AllocateEntry<Slot>(slot_pool, next_free_slot_index);
  237. shelf = &shelf_pool[selected_shelf_index];
  238. slot_pool[new_slot_index] = {
  239. new_shelf_index, page.texture_id, 0, shelf->y + height, page_size, 0, 0, null_index, null_index, null_index, null_index, 0, false
  240. };
  241. shelf_pool[new_shelf_index] = {
  242. page_index, shelf->y + height, shelf->height - height,
  243. selected_shelf_index, shelf->next_index, new_slot_index, new_slot_index, false
  244. };
  245. if (shelf->next_index != null_index)
  246. shelf_pool[shelf->next_index].previous_index = new_shelf_index;
  247. shelf->next_index = new_shelf_index;
  248. shelf->height = height;
  249. }
  250. }
  251. Slot* slot = &slot_pool[selected_slot_index];
  252. if (slot->width - width >= split_threshold)
  253. {
  254. const unsigned int new_slot_index = AllocateEntry<Slot>(slot_pool, next_free_slot_index);
  255. slot = &slot_pool[selected_slot_index];
  256. slot_pool[new_slot_index] = {
  257. selected_shelf_index, page.texture_id, slot->x + width, shelf->y, slot->width - width, 0, 0,
  258. selected_slot_index, slot->next_index, slot->previous_free_index, slot->next_free_index, 0, false
  259. };
  260. if (slot->next_index != null_index)
  261. slot_pool[slot->next_index].previous_index = new_slot_index;
  262. slot->next_index = new_slot_index;
  263. if (slot->previous_free_index == null_index)
  264. shelf->first_free_slot_index = new_slot_index;
  265. else
  266. slot_pool[slot->previous_free_index].next_free_index = new_slot_index;
  267. if (slot->next_free_index != null_index)
  268. slot_pool[slot->next_free_index].previous_free_index = new_slot_index;
  269. slot->width = width;
  270. }
  271. else
  272. {
  273. if (slot->previous_free_index == null_index)
  274. shelf->first_free_slot_index = slot->next_free_index;
  275. else
  276. slot_pool[slot->previous_free_index].next_free_index = slot->next_free_index;
  277. if (slot->next_free_index != null_index)
  278. slot_pool[slot->next_free_index].previous_free_index = slot->previous_free_index;
  279. }
  280. slot->allocated = true;
  281. slot->actual_width = width;
  282. slot->height = height;
  283. slot->epoch = current_epoch;
  284. if (page_index == first_page_index)
  285. first_page_allocated_pixels += width * shelf->height;
  286. return selected_slot_index;
  287. }
  288. unsigned int SpriteSet::Remove(const unsigned int slot_index)
  289. {
  290. Slot& slot = slot_pool[slot_index];
  291. Shelf& shelf = shelf_pool[slot.shelf_index];
  292. const unsigned int page_index = shelf.page_index;
  293. Page& page = page_pool[page_index];
  294. /* DEBUG: Fill the removed area with blue.
  295. for (unsigned int offset_y = 0; offset_y < slot.height; ++offset_y)
  296. {
  297. for (unsigned int offset_x = 0; offset_x < slot.width; ++offset_x)
  298. {
  299. const auto pixel = page.texture_data->begin() + ((slot.y + offset_y) * page_size + slot.x + offset_x) * 4;
  300. pixel[0] = 0;
  301. pixel[1] = 0;
  302. pixel[2] = 255;
  303. pixel[3] = 255;
  304. }
  305. }
  306. */
  307. const unsigned int slot_pixels = slot.width * slot.height;
  308. slot.allocated = false;
  309. slot.previous_free_index = null_index;
  310. if (shelf.first_free_slot_index == null_index)
  311. {
  312. slot.next_free_index = null_index;
  313. }
  314. else
  315. {
  316. slot.next_free_index = shelf.first_free_slot_index;
  317. slot_pool[shelf.first_free_slot_index].previous_free_index = slot_index;
  318. }
  319. shelf.first_free_slot_index = slot_index;
  320. ++slot.epoch;
  321. // Merge consecutive empty slots.
  322. if (slot.next_index != null_index)
  323. {
  324. Slot& next_slot = slot_pool[slot.next_index];
  325. if (!next_slot.allocated)
  326. {
  327. slot.width += next_slot.width;
  328. const unsigned int next_index = slot.next_index;
  329. slot.next_index = next_slot.next_index;
  330. if (next_slot.previous_free_index != null_index)
  331. slot_pool[next_slot.previous_free_index].next_free_index = next_slot.next_free_index;
  332. if (next_slot.next_free_index != null_index)
  333. slot_pool[next_slot.next_free_index].previous_free_index = next_slot.previous_free_index;
  334. FreeEntry<Slot>(slot_pool, next_free_slot_index, next_index);
  335. if (slot.next_index != null_index)
  336. slot_pool[slot.next_index].previous_index = slot_index;
  337. }
  338. }
  339. if (slot.previous_index != null_index)
  340. {
  341. Slot& previous_slot = slot_pool[slot.previous_index];
  342. if (!previous_slot.allocated)
  343. {
  344. slot.x -= previous_slot.width;
  345. slot.width += previous_slot.width;
  346. const unsigned int previous_index = slot.previous_index;
  347. slot.previous_index = previous_slot.previous_index;
  348. if (previous_slot.previous_free_index != null_index)
  349. slot_pool[previous_slot.previous_free_index].next_free_index = previous_slot.next_free_index;
  350. if (previous_slot.next_free_index != null_index)
  351. slot_pool[previous_slot.next_free_index].previous_free_index = previous_slot.previous_free_index;
  352. FreeEntry<Slot>(slot_pool, next_free_slot_index, previous_index);
  353. if (slot.previous_index == null_index) {
  354. shelf.first_slot_index = slot_index;
  355. if (slot.next_index == null_index)
  356. shelf.allocated = false;
  357. }
  358. else
  359. {
  360. slot_pool[slot.previous_index].next_index = slot_index;
  361. }
  362. }
  363. }
  364. // Merge consecutive empty shelves.
  365. if (shelf.allocated)
  366. return slot_pixels;
  367. if (shelf.next_index != null_index)
  368. {
  369. Shelf& next_shelf = shelf_pool[shelf.next_index];
  370. if (!next_shelf.allocated)
  371. {
  372. shelf.height += next_shelf.height;
  373. const unsigned int next_index = shelf.next_index;
  374. shelf.next_index = next_shelf.next_index;
  375. FreeEntry<Slot>(slot_pool, next_free_slot_index, next_shelf.first_slot_index);
  376. FreeEntry<Shelf>(shelf_pool, next_free_shelf_index, next_index);
  377. if (shelf.next_index != null_index)
  378. shelf_pool[shelf.next_index].previous_index = slot.shelf_index;
  379. }
  380. }
  381. if (shelf.previous_index != null_index)
  382. {
  383. Shelf& previous_shelf = shelf_pool[shelf.previous_index];
  384. if (!previous_shelf.allocated)
  385. {
  386. shelf.y -= previous_shelf.height;
  387. shelf.height += previous_shelf.height;
  388. slot.y = shelf.y;
  389. const unsigned int previous_index = shelf.previous_index;
  390. shelf.previous_index = previous_shelf.previous_index;
  391. FreeEntry<Slot>(slot_pool, next_free_slot_index, previous_shelf.first_slot_index);
  392. FreeEntry<Shelf>(shelf_pool, next_free_shelf_index, previous_index);
  393. if (shelf.previous_index == null_index)
  394. page.first_shelf_index = slot.shelf_index;
  395. else
  396. shelf_pool[shelf.previous_index].next_index = slot.shelf_index;
  397. }
  398. }
  399. // Deallocate the page if it becomes empty, except when it's the first one.
  400. if (page_index == first_page_index)
  401. {
  402. first_page_allocated_pixels -= slot.width * shelf.height;
  403. return slot_pixels;
  404. }
  405. if (shelf.height != page_size)
  406. return slot_pixels;
  407. FreeEntry<Slot>(slot_pool, next_free_slot_index, slot_index);
  408. FreeEntry<Shelf>(shelf_pool, next_free_shelf_index, page.first_shelf_index);
  409. page.texture_data.reset();
  410. page_pool[page.previous_index].next_index = page.next_index;
  411. if (page_index != last_page_index)
  412. page_pool[page.next_index].previous_index = page.previous_index;
  413. page_pool[last_page_index].next_index = page_index;
  414. return slot_pixels;
  415. }
  416. void SpriteSet::Remove(const Handle handle)
  417. {
  418. Slot& slot = slot_pool[handle.slot_index];
  419. if (slot.epoch != handle.epoch)
  420. return;
  421. Remove(handle.slot_index);
  422. }
  423. SpriteSet::SpriteData SpriteSet::Get(const Handle handle) const
  424. {
  425. const Slot& slot = slot_pool[handle.slot_index];
  426. return {
  427. slot.texture_id, slot.x + sprite_padding, slot.y + sprite_padding,
  428. slot.actual_width - sprite_padding * 2, slot.height - sprite_padding * 2
  429. };
  430. }
  431. Vector<const unsigned char*> SpriteSet::GetTextures() const
  432. {
  433. if (first_page_index == null_index)
  434. return {};
  435. Vector<const unsigned char*> textures;
  436. unsigned int page_index = first_page_index;
  437. while (true)
  438. {
  439. const Page& page = page_pool[page_index];
  440. textures.push_back(page.texture_data->data());
  441. if (page_index == last_page_index)
  442. break;
  443. page_index = page.next_index;
  444. }
  445. return textures;
  446. }
  447. } // namespace Rml