SpriteSet.cpp 18 KB

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