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