guiscalingfilter.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*
  2. Copyright (C) 2015 Aaron Suen <warr1024@gmail.com>
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU Lesser General Public License as published by
  5. the Free Software Foundation; either version 2.1 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public License along
  12. with this program; if not, write to the Free Software Foundation, Inc.,
  13. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  14. */
  15. #include "guiscalingfilter.h"
  16. #include "imagefilters.h"
  17. #include "settings.h"
  18. #include "util/numeric.h"
  19. #include <cstdio>
  20. #include "client/renderingengine.h"
  21. /* Maintain a static cache to store the images that correspond to textures
  22. * in a format that's manipulable by code. Some platforms exhibit issues
  23. * converting textures back into images repeatedly, and some don't even
  24. * allow it at all.
  25. */
  26. std::map<io::path, video::IImage *> g_imgCache;
  27. /* Maintain a static cache of all pre-scaled textures. These need to be
  28. * cleared as well when the cached images.
  29. */
  30. std::map<io::path, video::ITexture *> g_txrCache;
  31. /* Manually insert an image into the cache, useful to avoid texture-to-image
  32. * conversion whenever we can intercept it.
  33. */
  34. void guiScalingCache(io::path key, video::IVideoDriver *driver, video::IImage *value)
  35. {
  36. if (!g_settings->getBool("gui_scaling_filter"))
  37. return;
  38. video::IImage *copied = driver->createImage(value->getColorFormat(),
  39. value->getDimension());
  40. value->copyTo(copied);
  41. g_imgCache[key] = copied;
  42. }
  43. // Manually clear the cache, e.g. when switching to different worlds.
  44. void guiScalingCacheClear()
  45. {
  46. for (auto &it : g_imgCache) {
  47. if (it.second)
  48. it.second->drop();
  49. }
  50. g_imgCache.clear();
  51. for (auto &it : g_txrCache) {
  52. if (it.second)
  53. RenderingEngine::get_video_driver()->removeTexture(it.second);
  54. }
  55. g_txrCache.clear();
  56. }
  57. /* Get a cached, high-quality pre-scaled texture for display purposes. If the
  58. * texture is not already cached, attempt to create it. Returns a pre-scaled texture,
  59. * or the original texture if unable to pre-scale it.
  60. */
  61. video::ITexture *guiScalingResizeCached(video::IVideoDriver *driver,
  62. video::ITexture *src, const core::rect<s32> &srcrect,
  63. const core::rect<s32> &destrect)
  64. {
  65. if (src == NULL)
  66. return src;
  67. if (!g_settings->getBool("gui_scaling_filter"))
  68. return src;
  69. // Calculate scaled texture name.
  70. char rectstr[200];
  71. snprintf(rectstr, sizeof(rectstr), "%d:%d:%d:%d:%d:%d",
  72. srcrect.UpperLeftCorner.X,
  73. srcrect.UpperLeftCorner.Y,
  74. srcrect.getWidth(),
  75. srcrect.getHeight(),
  76. destrect.getWidth(),
  77. destrect.getHeight());
  78. io::path origname = src->getName().getPath();
  79. io::path scalename = origname + "@guiScalingFilter:" + rectstr;
  80. // Search for existing scaled texture.
  81. video::ITexture *scaled = g_txrCache[scalename];
  82. if (scaled)
  83. return scaled;
  84. // Try to find the texture converted to an image in the cache.
  85. // If the image was not found, try to extract it from the texture.
  86. video::IImage* srcimg = g_imgCache[origname];
  87. if (srcimg == NULL) {
  88. if (!g_settings->getBool("gui_scaling_filter_txr2img"))
  89. return src;
  90. srcimg = driver->createImageFromData(src->getColorFormat(),
  91. src->getSize(), src->lock(), false);
  92. src->unlock();
  93. g_imgCache[origname] = srcimg;
  94. }
  95. // Create a new destination image and scale the source into it.
  96. imageCleanTransparent(srcimg, 0);
  97. video::IImage *destimg = driver->createImage(src->getColorFormat(),
  98. core::dimension2d<u32>((u32)destrect.getWidth(),
  99. (u32)destrect.getHeight()));
  100. imageScaleNNAA(srcimg, srcrect, destimg);
  101. #ifdef __ANDROID__
  102. // Android is very picky about textures being powers of 2, so expand
  103. // the image dimensions to the next power of 2, if necessary, for
  104. // that platform.
  105. video::IImage *po2img = driver->createImage(src->getColorFormat(),
  106. core::dimension2d<u32>(npot2((u32)destrect.getWidth()),
  107. npot2((u32)destrect.getHeight())));
  108. po2img->fill(video::SColor(0, 0, 0, 0));
  109. destimg->copyTo(po2img);
  110. destimg->drop();
  111. destimg = po2img;
  112. #endif
  113. // Convert the scaled image back into a texture.
  114. scaled = driver->addTexture(scalename, destimg, NULL);
  115. destimg->drop();
  116. g_txrCache[scalename] = scaled;
  117. return scaled;
  118. }
  119. /* Convenience wrapper for guiScalingResizeCached that accepts parameters that
  120. * are available at GUI imagebutton creation time.
  121. */
  122. video::ITexture *guiScalingImageButton(video::IVideoDriver *driver,
  123. video::ITexture *src, s32 width, s32 height)
  124. {
  125. if (src == NULL)
  126. return src;
  127. return guiScalingResizeCached(driver, src,
  128. core::rect<s32>(0, 0, src->getSize().Width, src->getSize().Height),
  129. core::rect<s32>(0, 0, width, height));
  130. }
  131. /* Replacement for driver->draw2DImage() that uses the high-quality pre-scaled
  132. * texture, if configured.
  133. */
  134. void draw2DImageFilterScaled(video::IVideoDriver *driver, video::ITexture *txr,
  135. const core::rect<s32> &destrect, const core::rect<s32> &srcrect,
  136. const core::rect<s32> *cliprect, const video::SColor *const colors,
  137. bool usealpha)
  138. {
  139. // Attempt to pre-scale image in software in high quality.
  140. video::ITexture *scaled = guiScalingResizeCached(driver, txr, srcrect, destrect);
  141. if (scaled == NULL)
  142. return;
  143. // Correct source rect based on scaled image.
  144. const core::rect<s32> mysrcrect = (scaled != txr)
  145. ? core::rect<s32>(0, 0, destrect.getWidth(), destrect.getHeight())
  146. : srcrect;
  147. driver->draw2DImage(scaled, destrect, mysrcrect, cliprect, colors, usealpha);
  148. }