svg_texture.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  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. if (p_set_size) {
  179. size.x = img->get_width();
  180. base_size.x = img->get_width();
  181. if (size_override.x != 0) {
  182. size.x = size_override.x;
  183. }
  184. size.y = img->get_height();
  185. base_size.y = img->get_height();
  186. if (size_override.y != 0) {
  187. size.y = size_override.y;
  188. }
  189. }
  190. RID rid = RenderingServer::get_singleton()->texture_2d_create(img);
  191. RenderingServer::get_singleton()->texture_set_size_override(rid, size.x, size.y);
  192. return rid;
  193. }
  194. void SVGTexture::_clear() {
  195. for (KeyValue<double, RID> &tx : texture_cache) {
  196. if (tx.value.is_valid()) {
  197. RenderingServer::get_singleton()->free(tx.value);
  198. }
  199. }
  200. texture_cache.clear();
  201. if (base_texture.is_valid()) {
  202. RenderingServer::get_singleton()->free(base_texture);
  203. }
  204. base_texture = RID();
  205. alpha_cache.unref();
  206. }
  207. void SVGTexture::_update_texture() {
  208. _clear();
  209. emit_changed();
  210. }
  211. Ref<Image> SVGTexture::get_image() const {
  212. RID rid = _ensure_scale(1.0);
  213. if (rid.is_valid()) {
  214. return RenderingServer::get_singleton()->texture_2d_get(rid);
  215. } else {
  216. return Ref<Image>();
  217. }
  218. }
  219. int SVGTexture::get_width() const {
  220. _ensure_scale(1.0);
  221. return size.x;
  222. }
  223. int SVGTexture::get_height() const {
  224. _ensure_scale(1.0);
  225. return size.y;
  226. }
  227. RID SVGTexture::get_rid() const {
  228. return _ensure_scale(1.0);
  229. }
  230. bool SVGTexture::has_alpha() const {
  231. return true;
  232. }
  233. void SVGTexture::draw(RID p_canvas_item, const Point2 &p_pos, const Color &p_modulate, bool p_transpose) const {
  234. double scale = 1.0;
  235. CanvasItem *ci = CanvasItem::get_current_item_drawn();
  236. if (ci) {
  237. Viewport *vp = ci->get_viewport();
  238. if (vp) {
  239. scale = vp->get_oversampling();
  240. }
  241. }
  242. RID rid = _ensure_scale(scale);
  243. RenderingServer::get_singleton()->canvas_item_add_texture_rect(p_canvas_item, Rect2(p_pos, size), rid, false, p_modulate, p_transpose);
  244. }
  245. void SVGTexture::draw_rect(RID p_canvas_item, const Rect2 &p_rect, bool p_tile, const Color &p_modulate, bool p_transpose) const {
  246. double scale = 1.0;
  247. CanvasItem *ci = CanvasItem::get_current_item_drawn();
  248. if (ci) {
  249. Viewport *vp = ci->get_viewport();
  250. if (vp) {
  251. scale = vp->get_oversampling();
  252. }
  253. }
  254. RID rid = _ensure_scale(scale);
  255. RenderingServer::get_singleton()->canvas_item_add_texture_rect(p_canvas_item, p_rect, rid, p_tile, p_modulate, p_transpose);
  256. }
  257. 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 {
  258. double scale = 1.0;
  259. CanvasItem *ci = CanvasItem::get_current_item_drawn();
  260. if (ci) {
  261. Viewport *vp = ci->get_viewport();
  262. if (vp) {
  263. scale = vp->get_oversampling();
  264. }
  265. }
  266. RID rid = _ensure_scale(scale);
  267. RenderingServer::get_singleton()->canvas_item_add_texture_rect_region(p_canvas_item, p_rect, rid, p_src_rect, p_modulate, p_transpose, p_clip_uv);
  268. }
  269. bool SVGTexture::is_pixel_opaque(int p_x, int p_y) const {
  270. if (alpha_cache.is_null()) {
  271. Ref<Image> img = get_image();
  272. if (img.is_valid()) {
  273. alpha_cache.instantiate();
  274. alpha_cache->create_from_image_alpha(img);
  275. }
  276. }
  277. if (alpha_cache.is_valid()) {
  278. int aw = int(alpha_cache->get_size().width);
  279. int ah = int(alpha_cache->get_size().height);
  280. if (aw == 0 || ah == 0) {
  281. return true;
  282. }
  283. int x = p_x * aw / size.x;
  284. int y = p_y * ah / size.y;
  285. x = CLAMP(x, 0, aw - 1);
  286. y = CLAMP(y, 0, ah - 1);
  287. return alpha_cache->get_bit(x, y);
  288. }
  289. return true;
  290. }
  291. void SVGTexture::set_size_override(const Size2i &p_size) {
  292. if (size_override == p_size) {
  293. return;
  294. }
  295. size_override = p_size;
  296. if (size_override.x == 0 || size_override.y == 0) {
  297. _ensure_scale(1.0);
  298. size = base_size;
  299. }
  300. if (size_override.x != 0) {
  301. size.x = size_override.x;
  302. }
  303. if (size_override.y != 0) {
  304. size.y = size_override.y;
  305. }
  306. for (KeyValue<double, RID> &tx : texture_cache) {
  307. if (tx.value.is_valid()) {
  308. RenderingServer::get_singleton()->texture_set_size_override(tx.value, size.x, size.y);
  309. }
  310. }
  311. if (base_texture.is_valid()) {
  312. RenderingServer::get_singleton()->texture_set_size_override(base_texture, size.x, size.y);
  313. }
  314. emit_changed();
  315. }
  316. void SVGTexture::_bind_methods() {
  317. 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()));
  318. ClassDB::bind_method(D_METHOD("set_source", "source"), &SVGTexture::set_source);
  319. ClassDB::bind_method(D_METHOD("get_source"), &SVGTexture::get_source);
  320. ClassDB::bind_method(D_METHOD("set_base_scale", "base_scale"), &SVGTexture::set_base_scale);
  321. ClassDB::bind_method(D_METHOD("get_base_scale"), &SVGTexture::get_base_scale);
  322. ClassDB::bind_method(D_METHOD("set_saturation", "saturation"), &SVGTexture::set_saturation);
  323. ClassDB::bind_method(D_METHOD("get_saturation"), &SVGTexture::get_saturation);
  324. ClassDB::bind_method(D_METHOD("set_color_map", "color_map"), &SVGTexture::set_color_map);
  325. ClassDB::bind_method(D_METHOD("get_color_map"), &SVGTexture::get_color_map);
  326. ClassDB::bind_method(D_METHOD("set_size_override", "size"), &SVGTexture::set_size_override);
  327. ADD_PROPERTY(PropertyInfo(Variant::STRING, "_source", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_INTERNAL | PROPERTY_USAGE_STORAGE), "set_source", "get_source");
  328. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "base_scale", PROPERTY_HINT_RANGE, "0.01,10.0,0.01"), "set_base_scale", "get_base_scale");
  329. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "saturation", PROPERTY_HINT_RANGE, "0.0,1.0,0.01"), "set_saturation", "get_saturation");
  330. ADD_PROPERTY(PropertyInfo(Variant::DICTIONARY, "color_map", PROPERTY_HINT_DICTIONARY_TYPE, "Color;Color"), "set_color_map", "get_color_map");
  331. }
  332. SVGTexture::~SVGTexture() {
  333. _clear();
  334. MutexLock lock(mutex);
  335. for (KeyValue<double, ScalingLevel> &sl : scaling_levels) {
  336. sl.value.textures.erase(this);
  337. }
  338. }