editor_resource_preview.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. /*************************************************************************/
  2. /* editor_resource_preview.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */
  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 "editor_resource_preview.h"
  31. #include "core/method_bind_ext.gen.inc"
  32. #include "core/io/resource_loader.h"
  33. #include "core/io/resource_saver.h"
  34. #include "core/message_queue.h"
  35. #include "core/os/file_access.h"
  36. #include "core/project_settings.h"
  37. #include "editor_node.h"
  38. #include "editor_scale.h"
  39. #include "editor_settings.h"
  40. bool EditorResourcePreviewGenerator::handles(const String &p_type) const {
  41. if (get_script_instance() && get_script_instance()->has_method("handles")) {
  42. return get_script_instance()->call("handles", p_type);
  43. }
  44. ERR_EXPLAIN("EditorResourcePreviewGenerator::handles needs to be overridden");
  45. ERR_FAIL_V(false);
  46. }
  47. Ref<Texture> EditorResourcePreviewGenerator::generate(const RES &p_from, const Size2 p_size) const {
  48. if (get_script_instance() && get_script_instance()->has_method("generate")) {
  49. return get_script_instance()->call("generate", p_from, p_size);
  50. }
  51. ERR_EXPLAIN("EditorResourcePreviewGenerator::generate needs to be overridden");
  52. ERR_FAIL_V(Ref<Texture>());
  53. }
  54. Ref<Texture> EditorResourcePreviewGenerator::generate_from_path(const String &p_path, const Size2 p_size) const {
  55. if (get_script_instance() && get_script_instance()->has_method("generate_from_path")) {
  56. return get_script_instance()->call("generate_from_path", p_path, p_size);
  57. }
  58. RES res = ResourceLoader::load(p_path);
  59. if (!res.is_valid())
  60. return res;
  61. return generate(res, p_size);
  62. }
  63. bool EditorResourcePreviewGenerator::should_generate_small_preview() const {
  64. return false;
  65. }
  66. void EditorResourcePreviewGenerator::_bind_methods() {
  67. ClassDB::add_virtual_method(get_class_static(), MethodInfo(Variant::BOOL, "handles", PropertyInfo(Variant::STRING, "type")));
  68. ClassDB::add_virtual_method(get_class_static(), MethodInfo(CLASS_INFO(Texture), "generate", PropertyInfo(Variant::OBJECT, "from", PROPERTY_HINT_RESOURCE_TYPE, "Resource"), PropertyInfo(Variant::VECTOR2, "size")));
  69. ClassDB::add_virtual_method(get_class_static(), MethodInfo(CLASS_INFO(Texture), "generate_from_path", PropertyInfo(Variant::STRING, "path", PROPERTY_HINT_FILE), PropertyInfo(Variant::VECTOR2, "size")));
  70. }
  71. EditorResourcePreviewGenerator::EditorResourcePreviewGenerator() {
  72. }
  73. EditorResourcePreview *EditorResourcePreview::singleton = NULL;
  74. void EditorResourcePreview::_thread_func(void *ud) {
  75. EditorResourcePreview *erp = (EditorResourcePreview *)ud;
  76. erp->_thread();
  77. }
  78. void EditorResourcePreview::_preview_ready(const String &p_str, const Ref<Texture> &p_texture, const Ref<Texture> &p_small_texture, ObjectID id, const StringName &p_func, const Variant &p_ud) {
  79. preview_mutex->lock();
  80. String path = p_str;
  81. uint32_t hash = 0;
  82. uint64_t modified_time = 0;
  83. if (p_str.begins_with("ID:")) {
  84. hash = p_str.get_slicec(':', 2).to_int();
  85. path = "ID:" + p_str.get_slicec(':', 1);
  86. } else {
  87. modified_time = FileAccess::get_modified_time(path);
  88. }
  89. Item item;
  90. item.order = order++;
  91. item.preview = p_texture;
  92. item.small_preview = p_small_texture;
  93. item.last_hash = hash;
  94. item.modified_time = modified_time;
  95. cache[path] = item;
  96. preview_mutex->unlock();
  97. MessageQueue::get_singleton()->push_call(id, p_func, path, p_texture, p_small_texture, p_ud);
  98. }
  99. void EditorResourcePreview::_generate_preview(Ref<ImageTexture> &r_texture, Ref<ImageTexture> &r_small_texture, const QueueItem &p_item, const String &cache_base) {
  100. String type;
  101. if (p_item.resource.is_valid())
  102. type = p_item.resource->get_class();
  103. else
  104. type = ResourceLoader::get_resource_type(p_item.path);
  105. if (type == "") {
  106. r_texture = Ref<ImageTexture>();
  107. r_small_texture = Ref<ImageTexture>();
  108. return; //could not guess type
  109. }
  110. int thumbnail_size = EditorSettings::get_singleton()->get("filesystem/file_dialog/thumbnail_size");
  111. thumbnail_size *= EDSCALE;
  112. r_texture = Ref<ImageTexture>();
  113. r_small_texture = Ref<ImageTexture>();
  114. for (int i = 0; i < preview_generators.size(); i++) {
  115. if (!preview_generators[i]->handles(type))
  116. continue;
  117. Ref<Texture> generated;
  118. if (p_item.resource.is_valid()) {
  119. generated = preview_generators[i]->generate(p_item.resource, Vector2(thumbnail_size, thumbnail_size));
  120. } else {
  121. generated = preview_generators[i]->generate_from_path(p_item.path, Vector2(thumbnail_size, thumbnail_size));
  122. }
  123. r_texture = generated;
  124. if (r_texture.is_valid() && preview_generators[i]->should_generate_small_preview()) {
  125. int small_thumbnail_size = EditorNode::get_singleton()->get_theme_base()->get_icon("Object", "EditorIcons")->get_width(); // Kind of a workaround to retrieve the default icon size
  126. small_thumbnail_size *= EDSCALE;
  127. Ref<Image> small_image = r_texture->get_data();
  128. small_image = small_image->duplicate();
  129. small_image->resize(small_thumbnail_size, small_thumbnail_size, Image::INTERPOLATE_CUBIC);
  130. r_small_texture.instance();
  131. r_small_texture->create_from_image(small_image);
  132. }
  133. break;
  134. }
  135. if (!p_item.resource.is_valid()) {
  136. // cache the preview in case it's a resource on disk
  137. if (r_texture.is_valid()) {
  138. //wow it generated a preview... save cache
  139. bool has_small_texture = r_small_texture.is_valid();
  140. ResourceSaver::save(cache_base + ".png", r_texture);
  141. if (has_small_texture) {
  142. ResourceSaver::save(cache_base + "_small.png", r_small_texture);
  143. }
  144. FileAccess *f = FileAccess::open(cache_base + ".txt", FileAccess::WRITE);
  145. f->store_line(itos(thumbnail_size));
  146. f->store_line(itos(has_small_texture));
  147. f->store_line(itos(FileAccess::get_modified_time(p_item.path)));
  148. f->store_line(FileAccess::get_md5(p_item.path));
  149. memdelete(f);
  150. }
  151. }
  152. }
  153. void EditorResourcePreview::_thread() {
  154. while (!exit) {
  155. preview_sem->wait();
  156. preview_mutex->lock();
  157. if (queue.size()) {
  158. QueueItem item = queue.front()->get();
  159. queue.pop_front();
  160. if (cache.has(item.path)) {
  161. //already has it because someone loaded it, just let it know it's ready
  162. String path = item.path;
  163. if (item.resource.is_valid()) {
  164. path += ":" + itos(cache[item.path].last_hash); //keep last hash (see description of what this is in condition below)
  165. }
  166. _preview_ready(path, cache[item.path].preview, cache[item.path].small_preview, item.id, item.function, item.userdata);
  167. preview_mutex->unlock();
  168. } else {
  169. preview_mutex->unlock();
  170. Ref<ImageTexture> texture;
  171. Ref<ImageTexture> small_texture;
  172. int thumbnail_size = EditorSettings::get_singleton()->get("filesystem/file_dialog/thumbnail_size");
  173. thumbnail_size *= EDSCALE;
  174. if (item.resource.is_valid()) {
  175. _generate_preview(texture, small_texture, item, String());
  176. //adding hash to the end of path (should be ID:<objid>:<hash>) because of 5 argument limit to call_deferred
  177. _preview_ready(item.path + ":" + itos(item.resource->hash_edited_version()), texture, small_texture, item.id, item.function, item.userdata);
  178. } else {
  179. String temp_path = EditorSettings::get_singleton()->get_cache_dir();
  180. String cache_base = ProjectSettings::get_singleton()->globalize_path(item.path).md5_text();
  181. cache_base = temp_path.plus_file("resthumb-" + cache_base);
  182. //does not have it, try to load a cached thumbnail
  183. String file = cache_base + ".txt";
  184. FileAccess *f = FileAccess::open(file, FileAccess::READ);
  185. if (!f) {
  186. // No cache found, generate
  187. _generate_preview(texture, small_texture, item, cache_base);
  188. } else {
  189. uint64_t modtime = FileAccess::get_modified_time(item.path);
  190. int tsize = f->get_line().to_int64();
  191. bool has_small_texture = f->get_line().to_int();
  192. uint64_t last_modtime = f->get_line().to_int64();
  193. bool cache_valid = true;
  194. if (tsize != thumbnail_size) {
  195. cache_valid = false;
  196. memdelete(f);
  197. } else if (last_modtime != modtime) {
  198. String last_md5 = f->get_line();
  199. String md5 = FileAccess::get_md5(item.path);
  200. memdelete(f);
  201. if (last_md5 != md5) {
  202. cache_valid = false;
  203. } else {
  204. //update modified time
  205. f = FileAccess::open(file, FileAccess::WRITE);
  206. f->store_line(itos(modtime));
  207. f->store_line(itos(has_small_texture));
  208. f->store_line(md5);
  209. memdelete(f);
  210. }
  211. } else {
  212. memdelete(f);
  213. }
  214. if (cache_valid) {
  215. Ref<Image> img;
  216. img.instance();
  217. Ref<Image> small_img;
  218. small_img.instance();
  219. if (img->load(cache_base + ".png") != OK) {
  220. cache_valid = false;
  221. } else {
  222. texture.instance();
  223. texture->create_from_image(img, Texture::FLAG_FILTER);
  224. if (has_small_texture) {
  225. if (small_img->load(cache_base + "_small.png") != OK) {
  226. cache_valid = false;
  227. } else {
  228. small_texture.instance();
  229. small_texture->create_from_image(small_img, Texture::FLAG_FILTER);
  230. }
  231. }
  232. }
  233. }
  234. if (!cache_valid) {
  235. _generate_preview(texture, small_texture, item, cache_base);
  236. }
  237. }
  238. _preview_ready(item.path, texture, small_texture, item.id, item.function, item.userdata);
  239. }
  240. }
  241. } else {
  242. preview_mutex->unlock();
  243. }
  244. }
  245. exited = true;
  246. }
  247. void EditorResourcePreview::queue_edited_resource_preview(const Ref<Resource> &p_res, Object *p_receiver, const StringName &p_receiver_func, const Variant &p_userdata) {
  248. ERR_FAIL_NULL(p_receiver);
  249. ERR_FAIL_COND(!p_res.is_valid());
  250. preview_mutex->lock();
  251. String path_id = "ID:" + itos(p_res->get_instance_id());
  252. if (cache.has(path_id) && cache[path_id].last_hash == p_res->hash_edited_version()) {
  253. cache[path_id].order = order++;
  254. p_receiver->call(p_receiver_func, path_id, cache[path_id].preview, cache[path_id].small_preview, p_userdata);
  255. preview_mutex->unlock();
  256. return;
  257. }
  258. cache.erase(path_id); //erase if exists, since it will be regen
  259. QueueItem item;
  260. item.function = p_receiver_func;
  261. item.id = p_receiver->get_instance_id();
  262. item.resource = p_res;
  263. item.path = path_id;
  264. item.userdata = p_userdata;
  265. queue.push_back(item);
  266. preview_mutex->unlock();
  267. preview_sem->post();
  268. }
  269. void EditorResourcePreview::queue_resource_preview(const String &p_path, Object *p_receiver, const StringName &p_receiver_func, const Variant &p_userdata) {
  270. ERR_FAIL_NULL(p_receiver);
  271. preview_mutex->lock();
  272. if (cache.has(p_path)) {
  273. cache[p_path].order = order++;
  274. p_receiver->call(p_receiver_func, p_path, cache[p_path].preview, cache[p_path].small_preview, p_userdata);
  275. preview_mutex->unlock();
  276. return;
  277. }
  278. QueueItem item;
  279. item.function = p_receiver_func;
  280. item.id = p_receiver->get_instance_id();
  281. item.path = p_path;
  282. item.userdata = p_userdata;
  283. queue.push_back(item);
  284. preview_mutex->unlock();
  285. preview_sem->post();
  286. }
  287. void EditorResourcePreview::add_preview_generator(const Ref<EditorResourcePreviewGenerator> &p_generator) {
  288. preview_generators.push_back(p_generator);
  289. }
  290. void EditorResourcePreview::remove_preview_generator(const Ref<EditorResourcePreviewGenerator> &p_generator) {
  291. preview_generators.erase(p_generator);
  292. }
  293. EditorResourcePreview *EditorResourcePreview::get_singleton() {
  294. return singleton;
  295. }
  296. void EditorResourcePreview::_bind_methods() {
  297. ClassDB::bind_method("_preview_ready", &EditorResourcePreview::_preview_ready);
  298. ClassDB::bind_method(D_METHOD("queue_resource_preview", "path", "receiver", "receiver_func", "userdata"), &EditorResourcePreview::queue_resource_preview);
  299. ClassDB::bind_method(D_METHOD("queue_edited_resource_preview", "resource", "receiver", "receiver_func", "userdata"), &EditorResourcePreview::queue_edited_resource_preview);
  300. ClassDB::bind_method(D_METHOD("add_preview_generator", "generator"), &EditorResourcePreview::add_preview_generator);
  301. ClassDB::bind_method(D_METHOD("remove_preview_generator", "generator"), &EditorResourcePreview::remove_preview_generator);
  302. ClassDB::bind_method(D_METHOD("check_for_invalidation", "path"), &EditorResourcePreview::check_for_invalidation);
  303. ADD_SIGNAL(MethodInfo("preview_invalidated", PropertyInfo(Variant::STRING, "path")));
  304. }
  305. void EditorResourcePreview::check_for_invalidation(const String &p_path) {
  306. preview_mutex->lock();
  307. bool call_invalidated = false;
  308. if (cache.has(p_path)) {
  309. uint64_t modified_time = FileAccess::get_modified_time(p_path);
  310. if (modified_time != cache[p_path].modified_time) {
  311. cache.erase(p_path);
  312. call_invalidated = true;
  313. }
  314. }
  315. preview_mutex->unlock();
  316. if (call_invalidated) { //do outside mutex
  317. call_deferred("emit_signal", "preview_invalidated", p_path);
  318. }
  319. }
  320. void EditorResourcePreview::start() {
  321. ERR_FAIL_COND(thread);
  322. thread = Thread::create(_thread_func, this);
  323. exited = false;
  324. }
  325. void EditorResourcePreview::stop() {
  326. if (thread) {
  327. exit = true;
  328. preview_sem->post();
  329. while (!exited) {
  330. OS::get_singleton()->delay_usec(10000);
  331. VisualServer::get_singleton()->sync(); //sync pending stuff, as thread may be blocked on visual server
  332. }
  333. Thread::wait_to_finish(thread);
  334. memdelete(thread);
  335. thread = NULL;
  336. }
  337. }
  338. EditorResourcePreview::EditorResourcePreview() {
  339. thread = NULL;
  340. singleton = this;
  341. preview_mutex = Mutex::create();
  342. preview_sem = Semaphore::create();
  343. order = 0;
  344. exit = false;
  345. exited = false;
  346. }
  347. EditorResourcePreview::~EditorResourcePreview() {
  348. stop();
  349. memdelete(preview_mutex);
  350. memdelete(preview_sem);
  351. }