SpriteSet.cpp 17 KB

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