| 1 | /**************************************************************************/ |
| 2 | /* dpi_texture.cpp */ |
| 3 | /**************************************************************************/ |
| 4 | /* This file is part of: */ |
| 5 | /* GODOT ENGINE */ |
| 6 | /* https://godotengine.org */ |
| 7 | /**************************************************************************/ |
| 8 | /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ |
| 9 | /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ |
| 10 | /* */ |
| 11 | /* Permission is hereby granted, free of charge, to any person obtaining */ |
| 12 | /* a copy of this software and associated documentation files (the */ |
| 13 | /* "Software"), to deal in the Software without restriction, including */ |
| 14 | /* without limitation the rights to use, copy, modify, merge, publish, */ |
| 15 | /* distribute, sublicense, and/or sell copies of the Software, and to */ |
| 16 | /* permit persons to whom the Software is furnished to do so, subject to */ |
| 17 | /* the following conditions: */ |
| 18 | /* */ |
| 19 | /* The above copyright notice and this permission notice shall be */ |
| 20 | /* included in all copies or substantial portions of the Software. */ |
| 21 | /* */ |
| 22 | /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ |
| 23 | /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ |
| 24 | /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ |
| 25 | /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ |
| 26 | /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ |
| 27 | /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ |
| 28 | /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ |
| 29 | /**************************************************************************/ |
| 30 | |
| 31 | #include "dpi_texture.h" |
| 32 | |
| 33 | #include "core/io/image_loader.h" |
| 34 | #include "scene/main/canvas_item.h" |
| 35 | #include "scene/main/viewport.h" |
| 36 | #include "scene/resources/bit_map.h" |
| 37 | #include "scene/resources/placeholder_textures.h" |
| 38 | |
| 39 | #include "modules/modules_enabled.gen.h" // For svg. |
| 40 | #ifdef MODULE_SVG_ENABLED |
| 41 | #include "modules/svg/image_loader_svg.h" |
| 42 | #endif |
| 43 | |
| 44 | Mutex DPITexture::mutex; |
| 45 | HashMap<double, DPITexture::ScalingLevel> DPITexture::scaling_levels; |
| 46 | |
| 47 | void DPITexture::reference_scaling_level(double p_scale) { |
| 48 | uint32_t oversampling = CLAMP(p_scale, 0.1, 100.0) * 64; |
| 49 | if (oversampling == 64) { |
| 50 | return; |
| 51 | } |
| 52 | double scale = double(oversampling) / 64.0; |
| 53 | |
| 54 | MutexLock lock(mutex); |
| 55 | ScalingLevel *sl = scaling_levels.getptr(scale); |
| 56 | if (sl) { |
| 57 | sl->refcount++; |
| 58 | } else { |
| 59 | ScalingLevel new_sl; |
| 60 | scaling_levels.insert(scale, new_sl); |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | void DPITexture::unreference_scaling_level(double p_scale) { |
| 65 | uint32_t oversampling = CLAMP(p_scale, 0.1, 100.0) * 64; |
| 66 | if (oversampling == 64) { |
| 67 | return; |
| 68 | } |
| 69 | double scale = double(oversampling) / 64.0; |
| 70 | |
| 71 | MutexLock lock(mutex); |
| 72 | ScalingLevel *sl = scaling_levels.getptr(scale); |
| 73 | if (sl) { |
| 74 | sl->refcount--; |
| 75 | if (sl->refcount == 0) { |
| 76 | for (DPITexture *tx : sl->textures) { |
| 77 | tx->_remove_scale(scale); |
| 78 | } |
| 79 | sl->textures.clear(); |
| 80 | scaling_levels.erase(scale); |
| 81 | } |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | Ref<DPITexture> DPITexture::create_from_string(const String &p_source, float p_scale, float p_saturation, const Dictionary &p_color_map) { |
| 86 | Ref<DPITexture> dpi_texture; |
| 87 | dpi_texture.instantiate(); |
| 88 | dpi_texture->set_source(p_source); |
| 89 | dpi_texture->set_base_scale(p_scale); |
| 90 | dpi_texture->set_saturation(p_saturation); |
| 91 | dpi_texture->set_color_map(p_color_map); |
| 92 | return dpi_texture; |
| 93 | } |
| 94 | |
| 95 | void DPITexture::set_source(const String &p_source) { |
| 96 | if (source == p_source) { |
| 97 | return; |
| 98 | } |
| 99 | source = p_source; |
| 100 | _update_texture(); |
| 101 | } |
| 102 | |
| 103 | String DPITexture::get_source() const { |
| 104 | return source; |
| 105 | } |
| 106 | |
| 107 | void DPITexture::set_base_scale(float p_scale) { |
| 108 | if (base_scale == p_scale) { |
| 109 | return; |
| 110 | } |
| 111 | ERR_FAIL_COND(p_scale <= 0.0); |
| 112 | |
| 113 | base_scale = p_scale; |
| 114 | _update_texture(); |
| 115 | } |
| 116 | |
| 117 | float DPITexture::get_base_scale() const { |
| 118 | return base_scale; |
| 119 | } |
| 120 | |
| 121 | void DPITexture::set_saturation(float p_saturation) { |
| 122 | if (saturation == p_saturation) { |
| 123 | return; |
| 124 | } |
| 125 | |
| 126 | saturation = p_saturation; |
| 127 | _update_texture(); |
| 128 | } |
| 129 | |
| 130 | float DPITexture::get_saturation() const { |
| 131 | return saturation; |
| 132 | } |
| 133 | |
| 134 | void DPITexture::set_color_map(const Dictionary &p_color_map) { |
| 135 | if (color_map == p_color_map) { |
| 136 | return; |
| 137 | } |
| 138 | color_map = p_color_map; |
| 139 | cmap.clear(); |
| 140 | for (const Variant *E = color_map.next(); E; E = color_map.next(E)) { |
| 141 | cmap[*E] = color_map[*E]; |
| 142 | } |
| 143 | _update_texture(); |
| 144 | } |
| 145 | |
| 146 | Dictionary DPITexture::get_color_map() const { |
| 147 | return color_map; |
| 148 | } |
| 149 | |
| 150 | void DPITexture::_remove_scale(double p_scale) { |
| 151 | if (Math::is_equal_approx(p_scale, 1.0)) { |
| 152 | return; |
| 153 | } |
| 154 | |
| 155 | RID *rid = texture_cache.getptr(p_scale); |
| 156 | if (rid) { |
| 157 | if (rid->is_valid()) { |
| 158 | RenderingServer::get_singleton()->free_rid(*rid); |
| 159 | } |
| 160 | texture_cache.erase(p_scale); |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | RID DPITexture::_ensure_scale(double p_scale) const { |
| 165 | uint32_t oversampling = CLAMP(p_scale, 0.1, 100.0) * 64; |
| 166 | if (oversampling == 64) { |
| 167 | if (base_texture.is_null()) { |
| 168 | base_texture = _load_at_scale(p_scale, true); |
| 169 | } |
| 170 | return base_texture; |
| 171 | } |
| 172 | double scale = double(oversampling) / 64.0; |
| 173 | |
| 174 | RID *rid = texture_cache.getptr(scale); |
| 175 | if (rid) { |
| 176 | return *rid; |
| 177 | } |
| 178 | |
| 179 | MutexLock lock(mutex); |
| 180 | ScalingLevel *sl = scaling_levels.getptr(scale); |
| 181 | ERR_FAIL_NULL_V_MSG(sl, RID(), "Invalid scaling level"); |
| 182 | sl->textures.insert(const_cast<DPITexture *>(this)); |
| 183 | |
| 184 | RID new_rid = _load_at_scale(scale, false); |
| 185 | texture_cache[scale] = new_rid; |
| 186 | return new_rid; |
| 187 | } |
| 188 | |
| 189 | RID DPITexture::_load_at_scale(double p_scale, bool p_set_size) const { |
| 190 | Ref<Image> img; |
| 191 | img.instantiate(); |
| 192 | #ifdef MODULE_SVG_ENABLED |
| 193 | const bool upsample = !Math::is_equal_approx(Math::round(p_scale * base_scale), p_scale * base_scale); |
| 194 | |
| 195 | Error err = ImageLoaderSVG::create_image_from_string(img, source, p_scale * base_scale, upsample, cmap); |
| 196 | if (err != OK) { |
| 197 | return RID(); |
| 198 | } |
| 199 | #else |
| 200 | img = Image::create_empty(Math::round(16 * p_scale * base_scale), Math::round(16 * p_scale * base_scale), false, Image::FORMAT_RGBA8); |
| 201 | #endif |
| 202 | if (saturation != 1.0) { |
| 203 | img->adjust_bcs(1.0, 1.0, saturation); |
| 204 | } |
| 205 | |
| 206 | Size2 current_size = size; |
| 207 | if (p_set_size || size.is_zero_approx()) { |
| 208 | size.x = img->get_width() / p_scale; |
| 209 | base_size.x = size.x; |
| 210 | if (size_override.x != 0) { |
| 211 | size.x = size_override.x; |
| 212 | } |
| 213 | size.y = img->get_height() / p_scale; |
| 214 | base_size.y = size.y; |
| 215 | if (size_override.y != 0) { |
| 216 | size.y = size_override.y; |
| 217 | } |
| 218 | current_size = size; |
| 219 | } |
| 220 | if (current_size.is_zero_approx()) { |
| 221 | current_size.x = img->get_width(); |
| 222 | current_size.y = img->get_height(); |
| 223 | } |
| 224 | |
| 225 | RID rid = RenderingServer::get_singleton()->texture_2d_create(img); |
| 226 | RenderingServer::get_singleton()->texture_set_size_override(rid, current_size.x, current_size.y); |
| 227 | return rid; |
| 228 | } |
| 229 | |
| 230 | void DPITexture::_clear() { |
| 231 | for (KeyValue<double, RID> &tx : texture_cache) { |
| 232 | if (tx.value.is_valid()) { |
| 233 | RenderingServer::get_singleton()->free_rid(tx.value); |
| 234 | } |
| 235 | } |
| 236 | texture_cache.clear(); |
| 237 | if (base_texture.is_valid()) { |
| 238 | RenderingServer::get_singleton()->free_rid(base_texture); |
| 239 | } |
| 240 | base_texture = RID(); |
| 241 | alpha_cache.unref(); |
| 242 | } |
| 243 | |
| 244 | void DPITexture::_update_texture() { |
| 245 | _clear(); |
| 246 | emit_changed(); |
| 247 | } |
| 248 | |
| 249 | Ref<Image> DPITexture::get_image() const { |
| 250 | RID rid = _ensure_scale(1.0); |
| 251 | if (rid.is_valid()) { |
| 252 | return RenderingServer::get_singleton()->texture_2d_get(rid); |
| 253 | } else { |
| 254 | return Ref<Image>(); |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | int DPITexture::get_width() const { |
| 259 | _ensure_scale(1.0); |
| 260 | return size.x; |
| 261 | } |
| 262 | |
| 263 | int DPITexture::get_height() const { |
| 264 | _ensure_scale(1.0); |
| 265 | return size.y; |
| 266 | } |
| 267 | |
| 268 | RID DPITexture::get_rid() const { |
| 269 | return _ensure_scale(1.0); |
| 270 | } |
| 271 | |
| 272 | bool DPITexture::has_alpha() const { |
| 273 | return true; |
| 274 | } |
| 275 | |
| 276 | RID DPITexture::get_scaled_rid() const { |
| 277 | double scale = TextServer::get_current_drawn_item_oversampling(); |
| 278 | if (scale == 0.0) { |
| 279 | scale = 1.0; |
| 280 | } |
| 281 | return _ensure_scale(scale); |
| 282 | } |
| 283 | |
| 284 | void DPITexture::draw(RID p_canvas_item, const Point2 &p_pos, const Color &p_modulate, bool p_transpose) const { |
| 285 | RID rid = get_scaled_rid(); // Note: call `get_scaled_rid` before using `size` to ensure it is loaded. |
| 286 | RenderingServer::get_singleton()->canvas_item_add_texture_rect(p_canvas_item, Rect2(p_pos, size), rid, false, p_modulate, p_transpose); |
| 287 | } |
| 288 | |
| 289 | void DPITexture::draw_rect(RID p_canvas_item, const Rect2 &p_rect, bool p_tile, const Color &p_modulate, bool p_transpose) const { |
| 290 | RenderingServer::get_singleton()->canvas_item_add_texture_rect(p_canvas_item, p_rect, get_scaled_rid(), p_tile, p_modulate, p_transpose); |
| 291 | } |
| 292 | |
| 293 | void DPITexture::draw_rect_region(RID p_canvas_item, const Rect2 &p_rect, const Rect2 &p_src_rect, const Color &p_modulate, bool p_transpose, bool p_clip_uv) const { |
| 294 | RenderingServer::get_singleton()->canvas_item_add_texture_rect_region(p_canvas_item, p_rect, get_scaled_rid(), p_src_rect, p_modulate, p_transpose, p_clip_uv); |
| 295 | } |
| 296 | |
| 297 | bool DPITexture::is_pixel_opaque(int p_x, int p_y) const { |
| 298 | if (alpha_cache.is_null()) { |
| 299 | Ref<Image> img = get_image(); |
| 300 | if (img.is_valid()) { |
| 301 | alpha_cache.instantiate(); |
| 302 | alpha_cache->create_from_image_alpha(img); |
| 303 | } |
| 304 | } |
| 305 | |
| 306 | if (alpha_cache.is_valid()) { |
| 307 | int aw = int(alpha_cache->get_size().width); |
| 308 | int ah = int(alpha_cache->get_size().height); |
| 309 | if (aw == 0 || ah == 0) { |
| 310 | return true; |
| 311 | } |
| 312 | |
| 313 | int x = p_x * aw / size.x; |
| 314 | int y = p_y * ah / size.y; |
| 315 | |
| 316 | x = CLAMP(x, 0, aw - 1); |
| 317 | y = CLAMP(y, 0, ah - 1); |
| 318 | |
| 319 | return alpha_cache->get_bit(x, y); |
| 320 | } |
| 321 | |
| 322 | return true; |
| 323 | } |
| 324 | |
| 325 | void DPITexture::set_size_override(const Size2i &p_size) { |
| 326 | if (size_override == p_size) { |
| 327 | return; |
| 328 | } |
| 329 | size_override = p_size; |
| 330 | if (size_override.x == 0 || size_override.y == 0) { |
| 331 | _ensure_scale(1.0); |
| 332 | size = base_size; |
| 333 | } |
| 334 | if (size_override.x != 0) { |
| 335 | size.x = size_override.x; |
| 336 | } |
| 337 | if (size_override.y != 0) { |
| 338 | size.y = size_override.y; |
| 339 | } |
| 340 | for (KeyValue<double, RID> &tx : texture_cache) { |
| 341 | if (tx.value.is_valid()) { |
| 342 | RenderingServer::get_singleton()->texture_set_size_override(tx.value, size.x, size.y); |
| 343 | } |
| 344 | } |
| 345 | if (base_texture.is_valid()) { |
| 346 | RenderingServer::get_singleton()->texture_set_size_override(base_texture, size.x, size.y); |
| 347 | } |
| 348 | |
| 349 | emit_changed(); |
| 350 | } |
| 351 | |
| 352 | void DPITexture::_bind_methods() { |
| 353 | ClassDB::bind_static_method("DPITexture", D_METHOD("create_from_string", "source", "scale", "saturation", "color_map"), &DPITexture::create_from_string, DEFVAL(1.0), DEFVAL(1.0), DEFVAL(Dictionary())); |
| 354 | |
| 355 | ClassDB::bind_method(D_METHOD("set_source", "source"), &DPITexture::set_source); |
| 356 | ClassDB::bind_method(D_METHOD("get_source"), &DPITexture::get_source); |
| 357 | ClassDB::bind_method(D_METHOD("set_base_scale", "base_scale"), &DPITexture::set_base_scale); |
| 358 | ClassDB::bind_method(D_METHOD("get_base_scale"), &DPITexture::get_base_scale); |
| 359 | ClassDB::bind_method(D_METHOD("set_saturation", "saturation"), &DPITexture::set_saturation); |
| 360 | ClassDB::bind_method(D_METHOD("get_saturation"), &DPITexture::get_saturation); |
| 361 | ClassDB::bind_method(D_METHOD("set_color_map", "color_map"), &DPITexture::set_color_map); |
| 362 | ClassDB::bind_method(D_METHOD("get_color_map"), &DPITexture::get_color_map); |
| 363 | ClassDB::bind_method(D_METHOD("set_size_override", "size"), &DPITexture::set_size_override); |
| 364 | ClassDB::bind_method(D_METHOD("get_scaled_rid"), &DPITexture::get_scaled_rid); |
| 365 | |
| 366 | ADD_PROPERTY(PropertyInfo(Variant::STRING, "_source", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_INTERNAL | PROPERTY_USAGE_STORAGE), "set_source", "get_source"); |
| 367 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "base_scale", PROPERTY_HINT_RANGE, "0.01,10.0,0.01"), "set_base_scale", "get_base_scale"); |
| 368 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "saturation", PROPERTY_HINT_RANGE, "0.0,1.0,0.01"), "set_saturation", "get_saturation"); |
| 369 | ADD_PROPERTY(PropertyInfo(Variant::DICTIONARY, "color_map", PROPERTY_HINT_DICTIONARY_TYPE, "Color;Color"), "set_color_map", "get_color_map"); |
| 370 | } |
| 371 | |
| 372 | DPITexture::~DPITexture() { |
| 373 | _clear(); |
| 374 | |
| 375 | MutexLock lock(mutex); |
| 376 | for (KeyValue<double, ScalingLevel> &sl : scaling_levels) { |
| 377 | sl.value.textures.erase(this); |
| 378 | } |
| 379 | } |