editor_resource_preview.cpp 13 KB

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