svg_texture.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. /**************************************************************************/
  2. /* svg_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. #include "svg_texture.h"
  31. #include "core/io/image_loader.h"
  32. #include "scene/main/canvas_item.h"
  33. #include "scene/main/viewport.h"
  34. #include "scene/resources/bit_map.h"
  35. #include "scene/resources/placeholder_textures.h"
  36. #include "modules/modules_enabled.gen.h" // For svg.
  37. #ifdef MODULE_SVG_ENABLED
  38. #include "modules/svg/image_loader_svg.h"
  39. #endif
  40. Mutex SVGTexture::mutex;
  41. HashMap<double, SVGTexture::ScalingLevel> SVGTexture::scaling_levels;
  42. void SVGTexture::reference_scaling_level(double p_scale) {
  43. uint32_t oversampling = CLAMP(p_scale, 0.1, 100.0) * 64;
  44. if (oversampling == 64) {
  45. return;
  46. }
  47. double scale = double(oversampling) / 64.0;
  48. MutexLock lock(mutex);
  49. ScalingLevel *sl = scaling_levels.getptr(scale);
  50. if (sl) {
  51. sl->refcount++;
  52. } else {
  53. ScalingLevel new_sl;
  54. scaling_levels.insert(scale, new_sl);
  55. }
  56. }
  57. void SVGTexture::unreference_scaling_level(double p_scale) {
  58. uint32_t oversampling = CLAMP(p_scale, 0.1, 100.0) * 64;
  59. if (oversampling == 64) {
  60. return;
  61. }
  62. double scale = double(oversampling) / 64.0;
  63. MutexLock lock(mutex);
  64. ScalingLevel *sl = scaling_levels.getptr(scale);
  65. if (sl) {
  66. sl->refcount--;
  67. if (sl->refcount == 0) {
  68. for (SVGTexture *tx : sl->textures) {
  69. tx->_remove_scale(scale);
  70. }
  71. sl->textures.clear();
  72. scaling_levels.erase(scale);
  73. }
  74. }
  75. }
  76. Ref<SVGTexture> SVGTexture::create_from_string(const String &p_source, float p_scale, float p_saturation, const Dictionary &p_color_map) {
  77. Ref<SVGTexture> svg_texture;
  78. svg_texture.instantiate();
  79. svg_texture->set_source(p_source);
  80. svg_texture->set_base_scale(p_scale);
  81. svg_texture->set_saturation(p_saturation);
  82. svg_texture->set_color_map(p_color_map);
  83. return svg_texture;
  84. }
  85. void SVGTexture::set_source(const String &p_source) {
  86. if (source == p_source) {
  87. return;
  88. }
  89. source = p_source;
  90. _update_texture();
  91. }
  92. String SVGTexture::get_source() const {
  93. return source;
  94. }
  95. void SVGTexture::set_base_scale(float p_scale) {
  96. if (base_scale == p_scale) {
  97. return;
  98. }
  99. ERR_FAIL_COND(p_scale <= 0.0);
  100. base_scale = p_scale;
  101. _update_texture();
  102. }
  103. float SVGTexture::get_base_scale() const {
  104. return base_scale;
  105. }
  106. void SVGTexture::set_saturation(float p_saturation) {
  107. if (saturation == p_saturation) {
  108. return;
  109. }
  110. saturation = p_saturation;
  111. _update_texture();
  112. }
  113. float SVGTexture::get_saturation() const {
  114. return saturation;
  115. }
  116. void SVGTexture::set_color_map(const Dictionary &p_color_map) {
  117. if (color_map == p_color_map) {
  118. return;
  119. }
  120. color_map = p_color_map;
  121. cmap.clear();
  122. for (const Variant *E = color_map.next(); E; E = color_map.next(E)) {
  123. cmap[*E] = color_map[*E];
  124. }
  125. _update_texture();
  126. }
  127. Dictionary SVGTexture::get_color_map() const {
  128. return color_map;
  129. }
  130. void SVGTexture::_remove_scale(double p_scale) {
  131. if (Math::is_equal_approx(p_scale, 1.0)) {
  132. return;
  133. }
  134. RID *rid = texture_cache.getptr(p_scale);
  135. if (rid) {
  136. if (rid->is_valid()) {
  137. RenderingServer::get_singleton()->free(*rid);
  138. }
  139. texture_cache.erase(p_scale);
  140. }
  141. }
  142. RID SVGTexture::_ensure_scale(double p_scale) const {
  143. uint32_t oversampling = CLAMP(p_scale, 0.1, 100.0) * 64;
  144. if (oversampling == 64) {
  145. if (base_texture.is_null()) {
  146. base_texture = _load_at_scale(p_scale, true);
  147. }
  148. return base_texture;
  149. }
  150. double scale = double(oversampling) / 64.0;
  151. RID *rid = texture_cache.getptr(scale);
  152. if (rid) {
  153. return *rid;
  154. }
  155. MutexLock lock(mutex);
  156. ScalingLevel *sl = scaling_levels.getptr(scale);
  157. ERR_FAIL_NULL_V_MSG(sl, RID(), "Invalid scaling level");
  158. sl->textures.insert(const_cast<SVGTexture *>(this));
  159. RID new_rid = _load_at_scale(scale, false);
  160. texture_cache[scale] = new_rid;
  161. return new_rid;
  162. }
  163. RID SVGTexture::_load_at_scale(double p_scale, bool p_set_size) const {
  164. Ref<Image> img;
  165. img.instantiate();
  166. #ifdef MODULE_SVG_ENABLED
  167. const bool upsample = !Math::is_equal_approx(Math::round(p_scale * base_scale), p_scale * base_scale);
  168. Error err = ImageLoaderSVG::create_image_from_string(img, source, p_scale * base_scale, upsample, cmap);
  169. if (err != OK) {
  170. return RID();
  171. }
  172. #else
  173. img = Image::create_empty(Math::round(16 * p_scale * base_scale), Math::round(16 * p_scale * base_scale), false, Image::FORMAT_RGBA8);
  174. #endif
  175. if (saturation != 1.0) {
  176. img->adjust_bcs(1.0, 1.0, saturation);
  177. }
  178. Size2 current_size = size;
  179. if (p_set_size) {
  180. size.x = img->get_width();
  181. base_size.x = img->get_width();
  182. if (size_override.x != 0) {
  183. size.x = size_override.x;
  184. }
  185. size.y = img->get_height();
  186. base_size.y = img->get_height();
  187. if (size_override.y != 0) {
  188. size.y = size_override.y;
  189. }
  190. current_size = size;
  191. }
  192. if (current_size.is_zero_approx()) {
  193. current_size.x = img->get_width();
  194. current_size.y = img->get_height();
  195. }
  196. RID rid = RenderingServer::get_singleton()->texture_2d_create(img);
  197. RenderingServer::get_singleton()->texture_set_size_override(rid, current_size.x, current_size.y);
  198. return rid;
  199. }
  200. void SVGTexture::_clear() {
  201. for (KeyValue<double, RID> &tx : texture_cache) {
  202. if (tx.value.is_valid()) {
  203. RenderingServer::get_singleton()->free(tx.value);
  204. }
  205. }
  206. texture_cache.clear();
  207. if (base_texture.is_valid()) {
  208. RenderingServer::get_singleton()->free(base_texture);
  209. }
  210. base_texture = RID();
  211. alpha_cache.unref();
  212. }
  213. void SVGTexture::_update_texture() {
  214. _clear();
  215. emit_changed();
  216. }
  217. Ref<Image> SVGTexture::get_image() const {
  218. RID rid = _ensure_scale(1.0);
  219. if (rid.is_valid()) {
  220. return RenderingServer::get_singleton()->texture_2d_get(rid);
  221. } else {
  222. return Ref<Image>();
  223. }
  224. }
  225. int SVGTexture::get_width() const {
  226. _ensure_scale(1.0);
  227. return size.x;
  228. }
  229. int SVGTexture::get_height() const {
  230. _ensure_scale(1.0);
  231. return size.y;
  232. }
  233. RID SVGTexture::get_rid() const {
  234. return _ensure_scale(1.0);
  235. }
  236. bool SVGTexture::has_alpha() const {
  237. return true;
  238. }
  239. RID SVGTexture::get_scaled_rid() const {
  240. double scale = 1.0;
  241. CanvasItem *ci = CanvasItem::get_current_item_drawn();
  242. if (ci) {
  243. Viewport *vp = ci->get_viewport();
  244. if (vp) {
  245. scale = vp->get_oversampling();
  246. }
  247. }
  248. return _ensure_scale(scale);
  249. }
  250. void SVGTexture::draw(RID p_canvas_item, const Point2 &p_pos, const Color &p_modulate, bool p_transpose) const {
  251. RenderingServer::get_singleton()->canvas_item_add_texture_rect(p_canvas_item, Rect2(p_pos, size), get_scaled_rid(), false, p_modulate, p_transpose);
  252. }
  253. void SVGTexture::draw_rect(RID p_canvas_item, const Rect2 &p_rect, bool p_tile, const Color &p_modulate, bool p_transpose) const {
  254. RenderingServer::get_singleton()->canvas_item_add_texture_rect(p_canvas_item, p_rect, get_scaled_rid(), p_tile, p_modulate, p_transpose);
  255. }
  256. void SVGTexture::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 {
  257. 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);
  258. }
  259. bool SVGTexture::is_pixel_opaque(int p_x, int p_y) const {
  260. if (alpha_cache.is_null()) {
  261. Ref<Image> img = get_image();
  262. if (img.is_valid()) {
  263. alpha_cache.instantiate();
  264. alpha_cache->create_from_image_alpha(img);
  265. }
  266. }
  267. if (alpha_cache.is_valid()) {
  268. int aw = int(alpha_cache->get_size().width);
  269. int ah = int(alpha_cache->get_size().height);
  270. if (aw == 0 || ah == 0) {
  271. return true;
  272. }
  273. int x = p_x * aw / size.x;
  274. int y = p_y * ah / size.y;
  275. x = CLAMP(x, 0, aw - 1);
  276. y = CLAMP(y, 0, ah - 1);
  277. return alpha_cache->get_bit(x, y);
  278. }
  279. return true;
  280. }
  281. void SVGTexture::set_size_override(const Size2i &p_size) {
  282. if (size_override == p_size) {
  283. return;
  284. }
  285. size_override = p_size;
  286. if (size_override.x == 0 || size_override.y == 0) {
  287. _ensure_scale(1.0);
  288. size = base_size;
  289. }
  290. if (size_override.x != 0) {
  291. size.x = size_override.x;
  292. }
  293. if (size_override.y != 0) {
  294. size.y = size_override.y;
  295. }
  296. for (KeyValue<double, RID> &tx : texture_cache) {
  297. if (tx.value.is_valid()) {
  298. RenderingServer::get_singleton()->texture_set_size_override(tx.value, size.x, size.y);
  299. }
  300. }
  301. if (base_texture.is_valid()) {
  302. RenderingServer::get_singleton()->texture_set_size_override(base_texture, size.x, size.y);
  303. }
  304. emit_changed();
  305. }
  306. void SVGTexture::_bind_methods() {
  307. ClassDB::bind_static_method("SVGTexture", D_METHOD("create_from_string", "source", "scale", "saturation", "color_map"), &SVGTexture::create_from_string, DEFVAL(1.0), DEFVAL(1.0), DEFVAL(Dictionary()));
  308. ClassDB::bind_method(D_METHOD("set_source", "source"), &SVGTexture::set_source);
  309. ClassDB::bind_method(D_METHOD("get_source"), &SVGTexture::get_source);
  310. ClassDB::bind_method(D_METHOD("set_base_scale", "base_scale"), &SVGTexture::set_base_scale);
  311. ClassDB::bind_method(D_METHOD("get_base_scale"), &SVGTexture::get_base_scale);
  312. ClassDB::bind_method(D_METHOD("set_saturation", "saturation"), &SVGTexture::set_saturation);
  313. ClassDB::bind_method(D_METHOD("get_saturation"), &SVGTexture::get_saturation);
  314. ClassDB::bind_method(D_METHOD("set_color_map", "color_map"), &SVGTexture::set_color_map);
  315. ClassDB::bind_method(D_METHOD("get_color_map"), &SVGTexture::get_color_map);
  316. ClassDB::bind_method(D_METHOD("set_size_override", "size"), &SVGTexture::set_size_override);
  317. ClassDB::bind_method(D_METHOD("get_scaled_rid"), &SVGTexture::get_scaled_rid);
  318. ADD_PROPERTY(PropertyInfo(Variant::STRING, "_source", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_INTERNAL | PROPERTY_USAGE_STORAGE), "set_source", "get_source");
  319. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "base_scale", PROPERTY_HINT_RANGE, "0.01,10.0,0.01"), "set_base_scale", "get_base_scale");
  320. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "saturation", PROPERTY_HINT_RANGE, "0.0,1.0,0.01"), "set_saturation", "get_saturation");
  321. ADD_PROPERTY(PropertyInfo(Variant::DICTIONARY, "color_map", PROPERTY_HINT_DICTIONARY_TYPE, "Color;Color"), "set_color_map", "get_color_map");
  322. }
  323. SVGTexture::~SVGTexture() {
  324. _clear();
  325. MutexLock lock(mutex);
  326. for (KeyValue<double, ScalingLevel> &sl : scaling_levels) {
  327. sl.value.textures.erase(this);
  328. }
  329. }