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