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