texture_manager.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  1. // SuperTux
  2. // Copyright (C) 2006 Matthias Braun <matze@braunis.de>
  3. //
  4. // This program is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. #include "video/texture_manager.hpp"
  17. #include <SDL_image.h>
  18. #include <assert.h>
  19. #include <sstream>
  20. #include <physfs.h>
  21. #include "math/rect.hpp"
  22. #include "physfs/physfs_sdl.hpp"
  23. #include "util/file_system.hpp"
  24. #include "util/log.hpp"
  25. #include "util/reader_document.hpp"
  26. #include "util/reader_mapping.hpp"
  27. #include "video/color.hpp"
  28. #include "video/gl.hpp"
  29. #include "video/sampler.hpp"
  30. #include "video/sdl_surface.hpp"
  31. #include "video/texture.hpp"
  32. #include "video/video_system.hpp"
  33. namespace {
  34. GLenum string2wrap(const std::string& text)
  35. {
  36. if (text == "clamp-to-edge")
  37. {
  38. return GL_CLAMP_TO_EDGE;
  39. }
  40. else if (text == "repeat")
  41. {
  42. return GL_REPEAT;
  43. }
  44. else if (text == "mirrored-repeat")
  45. {
  46. return GL_MIRRORED_REPEAT;
  47. }
  48. else
  49. {
  50. log_warning << "unknown texture wrap: " << text << std::endl;
  51. return GL_CLAMP_TO_EDGE;
  52. }
  53. }
  54. GLenum string2filter(const std::string& text)
  55. {
  56. if (text == "nearest")
  57. {
  58. return GL_NEAREST;
  59. }
  60. else if (text == "linear")
  61. {
  62. return GL_LINEAR;
  63. }
  64. else
  65. {
  66. log_warning << "unknown texture filter: " << text << std::endl;
  67. return GL_LINEAR;
  68. }
  69. }
  70. SDLSurfacePtr create_image_surface(const std::string& filename)
  71. {
  72. if (PHYSFS_exists(filename.c_str()))
  73. return SDLSurface::from_file(filename);
  74. // The image doesn't exist, so attempt to load a ".deprecated" version
  75. log_warning << "Image '" << filename << "' doesn't exist. Attempting to load \".deprecated\" version." << std::endl;
  76. return SDLSurface::from_file(FileSystem::strip_extension(filename) + ".deprecated" +
  77. FileSystem::extension(filename));
  78. }
  79. } // namespace
  80. const std::string TextureManager::s_dummy_texture = "images/engine/missing.png";
  81. TextureManager::TextureManager() :
  82. m_image_textures(),
  83. m_surfaces(),
  84. m_load_successful(false)
  85. {
  86. }
  87. TextureManager::~TextureManager()
  88. {
  89. for (const auto& texture : m_image_textures)
  90. {
  91. if (!texture.second.expired())
  92. {
  93. log_warning << "Texture '" << std::get<0>(texture.first) << "' not freed" << std::endl;
  94. }
  95. }
  96. m_image_textures.clear();
  97. m_surfaces.clear();
  98. }
  99. TexturePtr
  100. TextureManager::get(const ReaderMapping& mapping, const std::optional<Rect>& region)
  101. {
  102. std::string filename;
  103. if (!mapping.get("file", filename))
  104. {
  105. log_warning << "'file' tag missing" << std::endl;
  106. }
  107. else
  108. {
  109. filename = FileSystem::join(mapping.get_doc().get_directory(), filename);
  110. }
  111. std::optional<Rect> rect;
  112. std::vector<int> rect_v;
  113. if (mapping.get("rect", rect_v))
  114. {
  115. if (rect_v.size() == 4)
  116. {
  117. rect = Rect(rect_v[0], rect_v[1], rect_v[2], rect_v[3]);
  118. }
  119. else
  120. {
  121. log_warning << "'rect' requires four elements" << std::endl;
  122. }
  123. }
  124. GLenum wrap_s = GL_CLAMP_TO_EDGE;
  125. GLenum wrap_t = GL_CLAMP_TO_EDGE;
  126. std::vector<std::string> wrap_v;
  127. if (mapping.get("wrap", wrap_v))
  128. {
  129. if (wrap_v.size() == 1)
  130. {
  131. wrap_s = string2wrap(wrap_v[0]);
  132. wrap_t = string2wrap(wrap_v[0]);
  133. }
  134. else if (wrap_v.size() == 2)
  135. {
  136. wrap_s = string2wrap(wrap_v[0]);
  137. wrap_t = string2wrap(wrap_v[1]);
  138. }
  139. else
  140. {
  141. log_warning << "unknown number of wrap arguments" << std::endl;
  142. }
  143. }
  144. GLenum filter = GL_LINEAR;
  145. std::string filter_s;
  146. if (mapping.get("filter", filter_s))
  147. {
  148. filter = string2filter(filter_s);
  149. }
  150. Vector animate(0.0f, 0.0f);
  151. std::vector<float> animate_v;
  152. if (mapping.get("animate", animate_v))
  153. {
  154. if (animate_v.size() == 2)
  155. {
  156. animate.x = animate_v[0];
  157. animate.y = animate_v[1];
  158. }
  159. }
  160. if (region)
  161. {
  162. if (!rect)
  163. {
  164. rect = region;
  165. }
  166. else
  167. {
  168. rect->left += region->left;
  169. rect->top += region->top;
  170. rect->right = rect->left + region->get_width();
  171. rect->bottom = rect->top + region->get_height();
  172. }
  173. }
  174. return get(filename, rect, Sampler(filter, wrap_s, wrap_t, animate));
  175. }
  176. TexturePtr
  177. TextureManager::get(const std::string& _filename)
  178. {
  179. std::string filename = FileSystem::normalize(_filename);
  180. Texture::Key key(filename, Rect(0, 0, 0, 0));
  181. auto i = m_image_textures.find(key);
  182. TexturePtr texture;
  183. if (i != m_image_textures.end())
  184. texture = i->second.lock();
  185. if (!texture) {
  186. texture = create_image_texture(filename, Sampler());
  187. texture->m_cache_key = key;
  188. m_image_textures[key] = texture;
  189. }
  190. return texture;
  191. }
  192. TexturePtr
  193. TextureManager::get(const std::string& _filename,
  194. const std::optional<Rect>& rect,
  195. const Sampler& sampler)
  196. {
  197. std::string filename = FileSystem::normalize(_filename);
  198. Texture::Key key = Texture::Key(filename, rect ? *rect : Rect());
  199. auto i = m_image_textures.find(key);
  200. TexturePtr texture;
  201. if (i != m_image_textures.end())
  202. texture = i->second.lock();
  203. if (!texture)
  204. {
  205. if (rect)
  206. {
  207. texture = create_image_texture(filename, *rect, sampler);
  208. }
  209. else
  210. {
  211. texture = create_image_texture(filename, sampler);
  212. }
  213. texture->m_cache_key = key;
  214. m_image_textures[key] = texture;
  215. }
  216. return texture;
  217. }
  218. void
  219. TextureManager::reap_cache_entry(const Texture::Key& key)
  220. {
  221. auto i = m_image_textures.find(key);
  222. if (i == m_image_textures.end())
  223. {
  224. log_warning << "no cache entry for '" << std::get<0>(key) << "'" << std::endl;
  225. }
  226. else
  227. {
  228. assert(i->second.expired());
  229. m_image_textures.erase(i);
  230. }
  231. }
  232. TexturePtr
  233. TextureManager::create_image_texture(const std::string& filename, const Rect& rect, const Sampler& sampler)
  234. {
  235. m_load_successful = true;
  236. try
  237. {
  238. SDLSurfacePtr surface = create_image_surface_raw(filename, rect, sampler);
  239. return VideoSystem::current()->new_texture(*surface, sampler);
  240. }
  241. catch(const std::exception& err)
  242. {
  243. log_warning << "Couldn't load texture '" << filename << "' (now using dummy texture): " << err.what() << std::endl;
  244. m_load_successful = false;
  245. return create_dummy_texture();
  246. }
  247. }
  248. const SDL_Surface&
  249. TextureManager::get_surface(const std::string& filename)
  250. {
  251. auto i = m_surfaces.find(filename);
  252. if (i != m_surfaces.end())
  253. {
  254. return *i->second;
  255. }
  256. SDLSurfacePtr surface = create_image_surface(filename);
  257. return *(m_surfaces[filename] = std::move(surface));
  258. }
  259. SDLSurfacePtr
  260. TextureManager::create_image_surface_raw(const std::string& filename, const Rect& rect, const Sampler& sampler)
  261. {
  262. assert(rect.valid());
  263. const SDL_Surface& src_surface = get_surface(filename);
  264. SDLSurfacePtr convert;
  265. if (src_surface.format->Rmask == 0 &&
  266. src_surface.format->Gmask == 0 &&
  267. src_surface.format->Bmask == 0 &&
  268. src_surface.format->Amask == 0)
  269. {
  270. log_debug << "Wrong surface format for image " << filename << ". Compensating." << std::endl;
  271. convert.reset(SDL_ConvertSurfaceFormat(const_cast<SDL_Surface*>(&src_surface), SDL_PIXELFORMAT_RGBA8888, 0));
  272. }
  273. const SDL_Surface& surface = convert ? *convert : src_surface;
  274. SDLSurfacePtr subimage;
  275. if (!Rect(0, 0, surface.w, surface.h).contains(rect))
  276. {
  277. log_warning << filename << ": invalid subregion requested: image="
  278. << surface.w << "x" << surface.h << ", rect=" << rect << std::endl;
  279. subimage = SDLSurfacePtr(SDL_CreateRGBSurface(0,
  280. rect.get_width(),
  281. rect.get_height(),
  282. surface.format->BitsPerPixel,
  283. surface.format->Rmask,
  284. surface.format->Gmask,
  285. surface.format->Bmask,
  286. surface.format->Amask));
  287. Rect clipped_rect(std::max(0, rect.left),
  288. std::max(0, rect.top),
  289. std::min(subimage->w, rect.right),
  290. std::min(subimage->w, rect.bottom));
  291. SDL_Rect srcrect = clipped_rect.to_sdl();
  292. SDL_BlitSurface(const_cast<SDL_Surface*>(&surface), &srcrect, subimage.get(), nullptr);
  293. }
  294. else
  295. {
  296. subimage = SDLSurfacePtr(SDL_CreateRGBSurfaceFrom(static_cast<uint8_t*>(surface.pixels) +
  297. rect.top * surface.pitch +
  298. rect.left * surface.format->BytesPerPixel,
  299. rect.get_width(), rect.get_height(),
  300. surface.format->BitsPerPixel,
  301. surface.pitch,
  302. surface.format->Rmask,
  303. surface.format->Gmask,
  304. surface.format->Bmask,
  305. surface.format->Amask));
  306. if (!subimage)
  307. {
  308. throw std::runtime_error("SDL_CreateRGBSurfaceFrom() call failed");
  309. }
  310. }
  311. return subimage;
  312. }
  313. TexturePtr
  314. TextureManager::create_image_texture(const std::string& filename, const Sampler& sampler)
  315. {
  316. m_load_successful = true;
  317. try
  318. {
  319. SDLSurfacePtr surface = create_image_surface(filename);
  320. return VideoSystem::current()->new_texture(*surface, sampler);
  321. }
  322. catch (const std::exception& err)
  323. {
  324. log_warning << "Couldn't load texture '" << filename << "' (now using dummy texture): " << err.what() << std::endl;
  325. m_load_successful = false;
  326. return create_dummy_texture();
  327. }
  328. }
  329. SDLSurfacePtr
  330. TextureManager::create_dummy_surface()
  331. {
  332. // on error, try loading placeholder file
  333. try
  334. {
  335. SDLSurfacePtr surface = create_image_surface(s_dummy_texture);
  336. return surface;
  337. }
  338. catch (const std::exception& err)
  339. {
  340. // on error (when loading placeholder), try using empty surface,
  341. // when that fails to, just give up
  342. SDLSurfacePtr surface(SDL_CreateRGBSurface(0, 128, 128, 8, 0, 0, 0, 0));
  343. if (!surface)
  344. {
  345. throw;
  346. }
  347. else
  348. {
  349. log_warning << "Couldn't load texture '" << s_dummy_texture << "' (now using empty one): " << err.what() << std::endl;
  350. return surface;
  351. }
  352. }
  353. }
  354. TexturePtr
  355. TextureManager::create_dummy_texture() const
  356. {
  357. SDLSurfacePtr surface = create_dummy_surface();
  358. return VideoSystem::current()->new_texture(*surface);
  359. }
  360. void
  361. TextureManager::reload()
  362. {
  363. // Reload surfaces
  364. for (auto& surface : m_surfaces)
  365. {
  366. SDLSurfacePtr surface_new;
  367. try
  368. {
  369. surface_new = create_image_surface(surface.first);
  370. }
  371. catch (const std::exception& err)
  372. {
  373. log_warning << "Couldn't load texture '" << surface.first << "' (now using dummy texture): " << err.what() << std::endl;
  374. surface_new = create_dummy_surface();
  375. }
  376. surface.second.reset(surface_new);
  377. }
  378. // Reload textures
  379. for (auto& texture : m_image_textures)
  380. {
  381. auto texture_ptr = texture.second.lock();
  382. SDLSurfacePtr surface;
  383. if (std::get<1>(texture.first).empty()) // No specific rect for texture
  384. {
  385. try
  386. {
  387. surface = create_image_surface(std::get<0>(texture.first));
  388. }
  389. catch (const std::exception& err)
  390. {
  391. log_warning << "Couldn't load texture '" << std::get<0>(texture.first) << "' (now using dummy texture): " << err.what() << std::endl;
  392. surface = create_dummy_surface();
  393. }
  394. }
  395. else // Texture has a specific rect
  396. {
  397. try
  398. {
  399. surface = create_image_surface_raw(std::get<0>(texture.first), std::get<1>(texture.first), texture_ptr->get_sampler());
  400. }
  401. catch (const std::exception& err)
  402. {
  403. log_warning << "Couldn't load texture '" << std::get<0>(texture.first) << "' (now using dummy texture): " << err.what() << std::endl;
  404. surface = create_dummy_surface();
  405. }
  406. }
  407. texture_ptr->reload(*surface);
  408. }
  409. }
  410. void
  411. TextureManager::debug_print(std::ostream& out) const
  412. {
  413. size_t total_texture_pixels = 0;
  414. out << "textures:begin" << std::endl;
  415. for(const auto& it : m_image_textures)
  416. {
  417. const auto& key = it.first;
  418. if (it.second.lock()) {
  419. total_texture_pixels += std::get<1>(key).get_area();
  420. }
  421. out << " texture "
  422. << " filename:" << std::get<0>(key) << " " << std::get<1>(key)
  423. << " " << "use_count:" << it.second.use_count() << std::endl;
  424. }
  425. out << "textures:end" << std::endl;
  426. size_t total_surface_pixels = 0;
  427. out << "surfaces:begin" << std::endl;
  428. for(const auto& it : m_surfaces)
  429. {
  430. const auto& filename = it.first;
  431. const auto& surface = it.second;
  432. total_surface_pixels += surface->w * surface->h;
  433. out << " surface filename:" << filename << " " << surface->w << "x" << surface->h << std::endl;
  434. }
  435. out << "surfaces:end" << std::endl;
  436. out << "total texture count:" << m_image_textures.size() << std::endl;
  437. out << "total texture pixels:" << total_texture_pixels << std::endl;
  438. out << "total surface count:" << m_surfaces.size() << std::endl;
  439. out << "total surface pixels:" << total_surface_pixels << std::endl;
  440. }
  441. /* EOF */