guiscalingfilter.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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 "porting.h"
  18. #include "settings.h"
  19. #include "util/numeric.h"
  20. #include <cstdio>
  21. #include "client/renderingengine.h"
  22. /* Maintain a static cache to store the images that correspond to textures
  23. * in a format that's manipulable by code. Some platforms exhibit issues
  24. * converting textures back into images repeatedly, and some don't even
  25. * allow it at all.
  26. */
  27. std::map<io::path, video::IImage *> g_imgCache;
  28. /* Maintain a static cache of all pre-scaled textures. These need to be
  29. * cleared as well when the cached images.
  30. */
  31. std::map<io::path, video::ITexture *> g_txrCache;
  32. /* Manually insert an image into the cache, useful to avoid texture-to-image
  33. * conversion whenever we can intercept it.
  34. */
  35. void guiScalingCache(const io::path &key, video::IVideoDriver *driver, video::IImage *value)
  36. {
  37. if (!g_settings->getBool("gui_scaling_filter"))
  38. return;
  39. if (g_imgCache.find(key) != g_imgCache.end())
  40. return; // Already cached.
  41. video::IImage *copied = driver->createImage(value->getColorFormat(),
  42. value->getDimension());
  43. value->copyTo(copied);
  44. g_imgCache[key] = copied;
  45. }
  46. // Manually clear the cache, e.g. when switching to different worlds.
  47. void guiScalingCacheClear()
  48. {
  49. for (auto &it : g_imgCache) {
  50. if (it.second)
  51. it.second->drop();
  52. }
  53. g_imgCache.clear();
  54. for (auto &it : g_txrCache) {
  55. if (it.second)
  56. RenderingEngine::get_video_driver()->removeTexture(it.second);
  57. }
  58. g_txrCache.clear();
  59. }
  60. /* Get a cached, high-quality pre-scaled texture for display purposes. If the
  61. * texture is not already cached, attempt to create it. Returns a pre-scaled texture,
  62. * or the original texture if unable to pre-scale it.
  63. */
  64. video::ITexture *guiScalingResizeCached(video::IVideoDriver *driver,
  65. video::ITexture *src, const core::rect<s32> &srcrect,
  66. const core::rect<s32> &destrect)
  67. {
  68. if (src == NULL)
  69. return src;
  70. if (!g_settings->getBool("gui_scaling_filter"))
  71. return src;
  72. // Calculate scaled texture name.
  73. char rectstr[200];
  74. porting::mt_snprintf(rectstr, sizeof(rectstr), "%d:%d:%d:%d:%d:%d",
  75. srcrect.UpperLeftCorner.X,
  76. srcrect.UpperLeftCorner.Y,
  77. srcrect.getWidth(),
  78. srcrect.getHeight(),
  79. destrect.getWidth(),
  80. destrect.getHeight());
  81. io::path origname = src->getName().getPath();
  82. io::path scalename = origname + "@guiScalingFilter:" + rectstr;
  83. // Search for existing scaled texture.
  84. auto it_txr = g_txrCache.find(scalename);
  85. video::ITexture *scaled = (it_txr != g_txrCache.end()) ? it_txr->second : nullptr;
  86. if (scaled)
  87. return scaled;
  88. // Try to find the texture converted to an image in the cache.
  89. // If the image was not found, try to extract it from the texture.
  90. auto it_img = g_imgCache.find(origname);
  91. video::IImage *srcimg = (it_img != g_imgCache.end()) ? it_img->second : nullptr;
  92. if (!srcimg) {
  93. if (!g_settings->getBool("gui_scaling_filter_txr2img"))
  94. return src;
  95. srcimg = driver->createImageFromData(src->getColorFormat(),
  96. src->getSize(), src->lock(video::ETLM_READ_ONLY), false);
  97. src->unlock();
  98. g_imgCache[origname] = srcimg;
  99. }
  100. // Create a new destination image and scale the source into it.
  101. imageCleanTransparent(srcimg, 0);
  102. video::IImage *destimg = driver->createImage(src->getColorFormat(),
  103. core::dimension2d<u32>((u32)destrect.getWidth(),
  104. (u32)destrect.getHeight()));
  105. imageScaleNNAA(srcimg, srcrect, destimg);
  106. #if ENABLE_GLES
  107. // Some platforms are picky about textures being powers of 2, so expand
  108. // the image dimensions to the next power of 2, if necessary.
  109. if (!driver->queryFeature(video::EVDF_TEXTURE_NPOT)) {
  110. video::IImage *po2img = driver->createImage(src->getColorFormat(),
  111. core::dimension2d<u32>(npot2((u32)destrect.getWidth()),
  112. npot2((u32)destrect.getHeight())));
  113. po2img->fill(video::SColor(0, 0, 0, 0));
  114. destimg->copyTo(po2img);
  115. destimg->drop();
  116. destimg = po2img;
  117. }
  118. #endif
  119. // Convert the scaled image back into a texture.
  120. scaled = driver->addTexture(scalename, destimg);
  121. destimg->drop();
  122. g_txrCache[scalename] = scaled;
  123. return scaled;
  124. }
  125. /* Convenience wrapper for guiScalingResizeCached that accepts parameters that
  126. * are available at GUI imagebutton creation time.
  127. */
  128. video::ITexture *guiScalingImageButton(video::IVideoDriver *driver,
  129. video::ITexture *src, s32 width, s32 height)
  130. {
  131. if (src == NULL)
  132. return src;
  133. return guiScalingResizeCached(driver, src,
  134. core::rect<s32>(0, 0, src->getSize().Width, src->getSize().Height),
  135. core::rect<s32>(0, 0, width, height));
  136. }
  137. /* Replacement for driver->draw2DImage() that uses the high-quality pre-scaled
  138. * texture, if configured.
  139. */
  140. void draw2DImageFilterScaled(video::IVideoDriver *driver, video::ITexture *txr,
  141. const core::rect<s32> &destrect, const core::rect<s32> &srcrect,
  142. const core::rect<s32> *cliprect, const video::SColor *const colors,
  143. bool usealpha)
  144. {
  145. // Attempt to pre-scale image in software in high quality.
  146. video::ITexture *scaled = guiScalingResizeCached(driver, txr, srcrect, destrect);
  147. if (scaled == NULL)
  148. return;
  149. // Correct source rect based on scaled image.
  150. const core::rect<s32> mysrcrect = (scaled != txr)
  151. ? core::rect<s32>(0, 0, destrect.getWidth(), destrect.getHeight())
  152. : srcrect;
  153. driver->draw2DImage(scaled, destrect, mysrcrect, cliprect, colors, usealpha);
  154. }
  155. void draw2DImage9Slice(video::IVideoDriver *driver, video::ITexture *texture,
  156. const core::rect<s32> &destrect, const core::rect<s32> &srcrect,
  157. const core::rect<s32> &middlerect, const core::rect<s32> *cliprect,
  158. const video::SColor *const colors)
  159. {
  160. // `-x` is interpreted as `w - x`
  161. core::rect<s32> middle = middlerect;
  162. if (middlerect.LowerRightCorner.X < 0)
  163. middle.LowerRightCorner.X += srcrect.getWidth();
  164. if (middlerect.LowerRightCorner.Y < 0)
  165. middle.LowerRightCorner.Y += srcrect.getHeight();
  166. core::vector2di lower_right_offset = core::vector2di(srcrect.getWidth(),
  167. srcrect.getHeight()) - middle.LowerRightCorner;
  168. for (int y = 0; y < 3; ++y) {
  169. for (int x = 0; x < 3; ++x) {
  170. core::rect<s32> src = srcrect;
  171. core::rect<s32> dest = destrect;
  172. switch (x) {
  173. case 0:
  174. dest.LowerRightCorner.X = destrect.UpperLeftCorner.X + middle.UpperLeftCorner.X;
  175. src.LowerRightCorner.X = srcrect.UpperLeftCorner.X + middle.UpperLeftCorner.X;
  176. break;
  177. case 1:
  178. dest.UpperLeftCorner.X += middle.UpperLeftCorner.X;
  179. dest.LowerRightCorner.X -= lower_right_offset.X;
  180. src.UpperLeftCorner.X += middle.UpperLeftCorner.X;
  181. src.LowerRightCorner.X -= lower_right_offset.X;
  182. break;
  183. case 2:
  184. dest.UpperLeftCorner.X = destrect.LowerRightCorner.X - lower_right_offset.X;
  185. src.UpperLeftCorner.X = srcrect.LowerRightCorner.X - lower_right_offset.X;
  186. break;
  187. }
  188. switch (y) {
  189. case 0:
  190. dest.LowerRightCorner.Y = destrect.UpperLeftCorner.Y + middle.UpperLeftCorner.Y;
  191. src.LowerRightCorner.Y = srcrect.UpperLeftCorner.Y + middle.UpperLeftCorner.Y;
  192. break;
  193. case 1:
  194. dest.UpperLeftCorner.Y += middle.UpperLeftCorner.Y;
  195. dest.LowerRightCorner.Y -= lower_right_offset.Y;
  196. src.UpperLeftCorner.Y += middle.UpperLeftCorner.Y;
  197. src.LowerRightCorner.Y -= lower_right_offset.Y;
  198. break;
  199. case 2:
  200. dest.UpperLeftCorner.Y = destrect.LowerRightCorner.Y - lower_right_offset.Y;
  201. src.UpperLeftCorner.Y = srcrect.LowerRightCorner.Y - lower_right_offset.Y;
  202. break;
  203. }
  204. draw2DImageFilterScaled(driver, texture, dest, src, cliprect, colors, true);
  205. }
  206. }
  207. }