editor_resource_preview.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621
  1. /**************************************************************************/
  2. /* editor_resource_preview.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 "editor_resource_preview.h"
  31. #include "core/config/project_settings.h"
  32. #include "core/io/file_access.h"
  33. #include "core/io/resource_loader.h"
  34. #include "core/io/resource_saver.h"
  35. #include "core/variant/variant_utility.h"
  36. #include "editor/editor_node.h"
  37. #include "editor/editor_string_names.h"
  38. #include "editor/file_system/editor_paths.h"
  39. #include "editor/settings/editor_settings.h"
  40. #include "editor/themes/editor_scale.h"
  41. #include "scene/main/window.h"
  42. #include "scene/resources/image_texture.h"
  43. #include "servers/rendering/rendering_server_globals.h"
  44. bool EditorResourcePreviewGenerator::handles(const String &p_type) const {
  45. bool success = false;
  46. GDVIRTUAL_CALL(_handles, p_type, success);
  47. return success;
  48. }
  49. Ref<Texture2D> EditorResourcePreviewGenerator::generate(const Ref<Resource> &p_from, const Size2 &p_size, Dictionary &p_metadata) const {
  50. Ref<Texture2D> preview;
  51. GDVIRTUAL_CALL(_generate, p_from, p_size, p_metadata, preview);
  52. return preview;
  53. }
  54. Ref<Texture2D> EditorResourcePreviewGenerator::generate_from_path(const String &p_path, const Size2 &p_size, Dictionary &p_metadata) const {
  55. Ref<Texture2D> preview;
  56. if (GDVIRTUAL_CALL(_generate_from_path, p_path, p_size, p_metadata, preview)) {
  57. return preview;
  58. }
  59. Ref<Resource> res = ResourceLoader::load(p_path);
  60. if (res.is_null()) {
  61. return res;
  62. }
  63. return generate(res, p_size, p_metadata);
  64. }
  65. bool EditorResourcePreviewGenerator::generate_small_preview_automatically() const {
  66. bool success = false;
  67. GDVIRTUAL_CALL(_generate_small_preview_automatically, success);
  68. return success;
  69. }
  70. bool EditorResourcePreviewGenerator::can_generate_small_preview() const {
  71. bool success = false;
  72. GDVIRTUAL_CALL(_can_generate_small_preview, success);
  73. return success;
  74. }
  75. void EditorResourcePreviewGenerator::_bind_methods() {
  76. GDVIRTUAL_BIND(_handles, "type");
  77. GDVIRTUAL_BIND(_generate, "resource", "size", "metadata");
  78. GDVIRTUAL_BIND(_generate_from_path, "path", "size", "metadata");
  79. GDVIRTUAL_BIND(_generate_small_preview_automatically);
  80. GDVIRTUAL_BIND(_can_generate_small_preview);
  81. ClassDB::bind_method(D_METHOD("request_draw_and_wait", "viewport"), &EditorResourcePreviewGenerator::request_draw_and_wait);
  82. }
  83. void EditorResourcePreviewGenerator::DrawRequester::request_and_wait(RID p_viewport) {
  84. if (EditorResourcePreview::get_singleton()->is_threaded()) {
  85. RS::get_singleton()->connect(SNAME("frame_pre_draw"), callable_mp(this, &EditorResourcePreviewGenerator::DrawRequester::_prepare_draw).bind(p_viewport), Object::CONNECT_ONE_SHOT);
  86. semaphore.wait();
  87. } else {
  88. // Avoid the main viewport and children being redrawn.
  89. SceneTree *st = Object::cast_to<SceneTree>(OS::get_singleton()->get_main_loop());
  90. ERR_FAIL_NULL_MSG(st, "Editor's MainLoop is not a SceneTree. This is a bug.");
  91. RID root_vp = st->get_root()->get_viewport_rid();
  92. RenderingServer::get_singleton()->viewport_set_active(root_vp, false);
  93. RS::get_singleton()->viewport_set_update_mode(p_viewport, RS::VIEWPORT_UPDATE_ONCE);
  94. RS::get_singleton()->draw(false);
  95. // Let main viewport and children be drawn again.
  96. RenderingServer::get_singleton()->viewport_set_active(root_vp, true);
  97. }
  98. }
  99. void EditorResourcePreviewGenerator::DrawRequester::abort() {
  100. if (EditorResourcePreview::get_singleton()->is_threaded()) {
  101. semaphore.post();
  102. }
  103. }
  104. void EditorResourcePreviewGenerator::request_draw_and_wait(RID viewport) const {
  105. DrawRequester draw_requester;
  106. draw_requester.request_and_wait(viewport);
  107. }
  108. void EditorResourcePreviewGenerator::DrawRequester::_prepare_draw(RID p_viewport) {
  109. RS::get_singleton()->viewport_set_update_mode(p_viewport, RS::VIEWPORT_UPDATE_ONCE);
  110. RS::get_singleton()->request_frame_drawn_callback(callable_mp(this, &EditorResourcePreviewGenerator::DrawRequester::_post_semaphore));
  111. }
  112. void EditorResourcePreviewGenerator::DrawRequester::_post_semaphore() {
  113. semaphore.post();
  114. }
  115. bool EditorResourcePreview::is_threaded() const {
  116. return RSG::rasterizer->can_create_resources_async();
  117. }
  118. void EditorResourcePreview::_thread_func(void *ud) {
  119. EditorResourcePreview *erp = (EditorResourcePreview *)ud;
  120. erp->_thread();
  121. }
  122. void EditorResourcePreview::_preview_ready(const String &p_path, int p_hash, const Ref<Texture2D> &p_texture, const Ref<Texture2D> &p_small_texture, const Callable &p_callback, const Dictionary &p_metadata) {
  123. {
  124. MutexLock lock(preview_mutex);
  125. uint64_t modified_time = 0;
  126. if (!p_path.begins_with("ID:")) {
  127. modified_time = FileAccess::get_modified_time(p_path);
  128. String import_path = p_path + ".import";
  129. if (FileAccess::exists(import_path)) {
  130. modified_time = MAX(modified_time, FileAccess::get_modified_time(import_path));
  131. }
  132. }
  133. Item item;
  134. item.preview = p_texture;
  135. item.small_preview = p_small_texture;
  136. item.last_hash = p_hash;
  137. item.modified_time = modified_time;
  138. item.preview_metadata = p_metadata;
  139. cache[p_path] = item;
  140. }
  141. p_callback.call_deferred(p_path, p_texture, p_small_texture);
  142. }
  143. void EditorResourcePreview::_generate_preview(Ref<ImageTexture> &r_texture, Ref<ImageTexture> &r_small_texture, const QueueItem &p_item, const String &cache_base, Dictionary &p_metadata) {
  144. String type;
  145. uint64_t started_at = OS::get_singleton()->get_ticks_usec();
  146. if (p_item.resource.is_valid()) {
  147. type = p_item.resource->get_class();
  148. } else {
  149. type = ResourceLoader::get_resource_type(p_item.path);
  150. }
  151. if (type.is_empty()) {
  152. r_texture = Ref<ImageTexture>();
  153. r_small_texture = Ref<ImageTexture>();
  154. if (is_print_verbose_enabled()) {
  155. print_line(vformat("Generated '%s' preview in %d usec", p_item.path, OS::get_singleton()->get_ticks_usec() - started_at));
  156. }
  157. return; //could not guess type
  158. }
  159. int thumbnail_size = EDITOR_GET("filesystem/file_dialog/thumbnail_size");
  160. thumbnail_size *= EDSCALE;
  161. r_texture = Ref<ImageTexture>();
  162. r_small_texture = Ref<ImageTexture>();
  163. for (int i = 0; i < preview_generators.size(); i++) {
  164. if (!preview_generators[i]->handles(type)) {
  165. continue;
  166. }
  167. Ref<Texture2D> generated;
  168. if (p_item.resource.is_valid()) {
  169. generated = preview_generators.write[i]->generate(p_item.resource, Vector2(thumbnail_size, thumbnail_size), p_metadata);
  170. } else {
  171. generated = preview_generators.write[i]->generate_from_path(p_item.path, Vector2(thumbnail_size, thumbnail_size), p_metadata);
  172. }
  173. r_texture = generated;
  174. if (preview_generators[i]->can_generate_small_preview()) {
  175. Ref<Texture2D> generated_small;
  176. Dictionary d;
  177. if (p_item.resource.is_valid()) {
  178. generated_small = preview_generators.write[i]->generate(p_item.resource, Vector2(small_thumbnail_size, small_thumbnail_size), d);
  179. } else {
  180. generated_small = preview_generators.write[i]->generate_from_path(p_item.path, Vector2(small_thumbnail_size, small_thumbnail_size), d);
  181. }
  182. r_small_texture = generated_small;
  183. }
  184. if (r_small_texture.is_null() && r_texture.is_valid() && preview_generators[i]->generate_small_preview_automatically()) {
  185. Ref<Image> small_image = r_texture->get_image()->duplicate();
  186. Vector2i new_size = Vector2i(1, 1) * small_thumbnail_size;
  187. const real_t aspect = small_image->get_size().aspect();
  188. if (aspect > 1.0) {
  189. new_size.y = MAX(1, new_size.y / aspect);
  190. } else if (aspect < 1.0) {
  191. new_size.x = MAX(1, new_size.x * aspect);
  192. }
  193. small_image->resize(new_size.x, new_size.y, Image::INTERPOLATE_CUBIC);
  194. // Make sure the image is always square.
  195. if (aspect != 1.0) {
  196. Ref<Image> rect = small_image;
  197. const Vector2i rect_size = rect->get_size();
  198. small_image = Image::create_empty(small_thumbnail_size, small_thumbnail_size, false, rect->get_format());
  199. // Blit the rectangle in the center of the square.
  200. small_image->blit_rect(rect, Rect2i(Vector2i(), rect_size), (Vector2i(1, 1) * small_thumbnail_size - rect_size) / 2);
  201. }
  202. r_small_texture.instantiate();
  203. r_small_texture->set_image(small_image);
  204. }
  205. if (generated.is_valid()) {
  206. break;
  207. }
  208. }
  209. if (p_item.resource.is_null()) {
  210. // Cache the preview in case it's a resource on disk.
  211. if (r_texture.is_valid()) {
  212. // Wow it generated a preview... save cache.
  213. bool has_small_texture = r_small_texture.is_valid();
  214. ResourceSaver::save(r_texture, cache_base + ".png");
  215. if (has_small_texture) {
  216. ResourceSaver::save(r_small_texture, cache_base + "_small.png");
  217. }
  218. Ref<FileAccess> f = FileAccess::open(cache_base + ".txt", FileAccess::WRITE);
  219. ERR_FAIL_COND_MSG(f.is_null(), "Cannot create file '" + cache_base + ".txt'. Check user write permissions.");
  220. uint64_t modtime = FileAccess::get_modified_time(p_item.path);
  221. String import_path = p_item.path + ".import";
  222. if (FileAccess::exists(import_path)) {
  223. modtime = MAX(modtime, FileAccess::get_modified_time(import_path));
  224. }
  225. _write_preview_cache(f, thumbnail_size, has_small_texture, modtime, FileAccess::get_md5(p_item.path), p_metadata);
  226. }
  227. }
  228. if (is_print_verbose_enabled()) {
  229. print_line(vformat("Generated '%s' preview in %d usec", p_item.path, OS::get_singleton()->get_ticks_usec() - started_at));
  230. }
  231. }
  232. const Dictionary EditorResourcePreview::get_preview_metadata(const String &p_path) const {
  233. ERR_FAIL_COND_V(!cache.has(p_path), Dictionary());
  234. return cache[p_path].preview_metadata;
  235. }
  236. void EditorResourcePreview::_iterate() {
  237. preview_mutex.lock();
  238. if (queue.is_empty()) {
  239. preview_mutex.unlock();
  240. return;
  241. }
  242. QueueItem item = queue.front()->get();
  243. queue.pop_front();
  244. if (cache.has(item.path)) {
  245. Item cached_item = cache[item.path];
  246. // Already has it because someone loaded it, just let it know it's ready.
  247. _preview_ready(item.path, cached_item.last_hash, cached_item.preview, cached_item.small_preview, item.callback, cached_item.preview_metadata);
  248. preview_mutex.unlock();
  249. return;
  250. }
  251. preview_mutex.unlock();
  252. Ref<ImageTexture> texture;
  253. Ref<ImageTexture> small_texture;
  254. int thumbnail_size = EDITOR_GET("filesystem/file_dialog/thumbnail_size");
  255. thumbnail_size *= EDSCALE;
  256. if (item.resource.is_valid()) {
  257. Dictionary preview_metadata;
  258. _generate_preview(texture, small_texture, item, String(), preview_metadata);
  259. _preview_ready(item.path, item.resource->hash_edited_version_for_preview(), texture, small_texture, item.callback, preview_metadata);
  260. return;
  261. }
  262. Dictionary preview_metadata;
  263. String temp_path = EditorPaths::get_singleton()->get_cache_dir();
  264. String cache_base = ProjectSettings::get_singleton()->globalize_path(item.path).md5_text();
  265. cache_base = temp_path.path_join("resthumb-" + cache_base);
  266. // Does not have it, try to load a cached thumbnail.
  267. String file = cache_base + ".txt";
  268. Ref<FileAccess> f = FileAccess::open(file, FileAccess::READ);
  269. if (f.is_null()) {
  270. // No cache found, generate.
  271. _generate_preview(texture, small_texture, item, cache_base, preview_metadata);
  272. } else {
  273. uint64_t modtime = FileAccess::get_modified_time(item.path);
  274. String import_path = item.path + ".import";
  275. if (FileAccess::exists(import_path)) {
  276. modtime = MAX(modtime, FileAccess::get_modified_time(import_path));
  277. }
  278. int tsize;
  279. bool has_small_texture;
  280. uint64_t last_modtime;
  281. String hash;
  282. bool outdated;
  283. _read_preview_cache(f, &tsize, &has_small_texture, &last_modtime, &hash, &preview_metadata, &outdated);
  284. bool cache_valid = true;
  285. if (tsize != thumbnail_size) {
  286. cache_valid = false;
  287. f.unref();
  288. } else if (outdated) {
  289. cache_valid = false;
  290. f.unref();
  291. } else if (last_modtime != modtime) {
  292. String last_md5 = f->get_line();
  293. String md5 = FileAccess::get_md5(item.path);
  294. f.unref();
  295. if (last_md5 != md5) {
  296. cache_valid = false;
  297. } else {
  298. // Update modified time.
  299. Ref<FileAccess> f2 = FileAccess::open(file, FileAccess::WRITE);
  300. if (f2.is_null()) {
  301. // Not returning as this would leave the thread hanging and would require
  302. // some proper cleanup/disabling of resource preview generation.
  303. ERR_PRINT("Cannot create file '" + file + "'. Check user write permissions.");
  304. } else {
  305. _write_preview_cache(f2, thumbnail_size, has_small_texture, modtime, md5, preview_metadata);
  306. }
  307. }
  308. } else {
  309. f.unref();
  310. }
  311. if (cache_valid) {
  312. Ref<Image> img;
  313. img.instantiate();
  314. Ref<Image> small_img;
  315. small_img.instantiate();
  316. if (img->load(cache_base + ".png") != OK) {
  317. cache_valid = false;
  318. } else {
  319. texture.instantiate();
  320. texture->set_image(img);
  321. if (has_small_texture) {
  322. if (small_img->load(cache_base + "_small.png") != OK) {
  323. cache_valid = false;
  324. } else {
  325. small_texture.instantiate();
  326. small_texture->set_image(small_img);
  327. }
  328. }
  329. }
  330. }
  331. if (!cache_valid) {
  332. _generate_preview(texture, small_texture, item, cache_base, preview_metadata);
  333. }
  334. }
  335. _preview_ready(item.path, 0, texture, small_texture, item.callback, preview_metadata);
  336. }
  337. void EditorResourcePreview::_write_preview_cache(Ref<FileAccess> p_file, int p_thumbnail_size, bool p_has_small_texture, uint64_t p_modified_time, const String &p_hash, const Dictionary &p_metadata) {
  338. p_file->store_line(itos(p_thumbnail_size));
  339. p_file->store_line(itos(p_has_small_texture));
  340. p_file->store_line(itos(p_modified_time));
  341. p_file->store_line(p_hash);
  342. p_file->store_line(VariantUtilityFunctions::var_to_str(p_metadata).replace_char('\n', ' '));
  343. p_file->store_line(itos(CURRENT_METADATA_VERSION));
  344. }
  345. void EditorResourcePreview::_read_preview_cache(Ref<FileAccess> p_file, int *r_thumbnail_size, bool *r_has_small_texture, uint64_t *r_modified_time, String *r_hash, Dictionary *r_metadata, bool *r_outdated) {
  346. *r_thumbnail_size = p_file->get_line().to_int();
  347. *r_has_small_texture = p_file->get_line().to_int();
  348. *r_modified_time = p_file->get_line().to_int();
  349. *r_hash = p_file->get_line();
  350. *r_metadata = VariantUtilityFunctions::str_to_var(p_file->get_line());
  351. *r_outdated = p_file->get_line().to_int() < CURRENT_METADATA_VERSION;
  352. }
  353. void EditorResourcePreview::_thread() {
  354. exited.clear();
  355. while (!exiting.is_set()) {
  356. preview_sem.wait();
  357. _iterate();
  358. }
  359. exited.set();
  360. }
  361. void EditorResourcePreview::_idle_callback() {
  362. if (!singleton) {
  363. // Just in case the shutdown of the editor involves the deletion of the singleton
  364. // happening while additional idle callbacks can happen.
  365. return;
  366. }
  367. // Process preview tasks, trying to leave a little bit of responsiveness worst case.
  368. uint64_t start = OS::get_singleton()->get_ticks_msec();
  369. while (!singleton->queue.is_empty() && OS::get_singleton()->get_ticks_msec() - start < 100) {
  370. singleton->_iterate();
  371. }
  372. }
  373. void EditorResourcePreview::_update_thumbnail_sizes() {
  374. if (small_thumbnail_size == -1) {
  375. // Kind of a workaround to retrieve the default icon size.
  376. small_thumbnail_size = EditorNode::get_singleton()->get_editor_theme()->get_icon(SNAME("Object"), EditorStringName(EditorIcons))->get_width();
  377. }
  378. }
  379. EditorResourcePreview::PreviewItem EditorResourcePreview::get_resource_preview_if_available(const String &p_path) {
  380. PreviewItem item;
  381. {
  382. MutexLock lock(preview_mutex);
  383. HashMap<String, EditorResourcePreview::Item>::Iterator I = cache.find(p_path);
  384. if (!I) {
  385. return item;
  386. }
  387. EditorResourcePreview::Item &cached_item = I->value;
  388. item.preview = cached_item.preview;
  389. item.small_preview = cached_item.small_preview;
  390. }
  391. preview_sem.post();
  392. return item;
  393. }
  394. void EditorResourcePreview::_queue_edited_resource_preview(const Ref<Resource> &p_res, Object *p_receiver, const StringName &p_receiver_func, const Variant &p_userdata) {
  395. ERR_FAIL_NULL(p_receiver);
  396. queue_edited_resource_preview(p_res, Callable(p_receiver, p_receiver_func).bind(p_userdata));
  397. }
  398. void EditorResourcePreview::queue_edited_resource_preview(const Ref<Resource> &p_res, const Callable &p_callback) {
  399. ERR_FAIL_COND(p_res.is_null());
  400. _update_thumbnail_sizes();
  401. {
  402. MutexLock lock(preview_mutex);
  403. String path_id = "ID:" + itos(p_res->get_instance_id());
  404. HashMap<String, EditorResourcePreview::Item>::Iterator I = cache.find(path_id);
  405. if (I && I->value.last_hash == p_res->hash_edited_version_for_preview()) {
  406. p_callback.call(path_id, I->value.preview, I->value.small_preview);
  407. return;
  408. }
  409. if (I) {
  410. cache.remove(I); // Erase if exists, since it will be regen.
  411. }
  412. QueueItem item;
  413. item.resource = p_res;
  414. item.path = path_id;
  415. item.callback = p_callback;
  416. queue.push_back(item);
  417. }
  418. preview_sem.post();
  419. }
  420. void EditorResourcePreview::_queue_resource_preview(const String &p_path, Object *p_receiver, const StringName &p_receiver_func, const Variant &p_userdata) {
  421. ERR_FAIL_NULL(p_receiver);
  422. queue_resource_preview(p_path, Callable(p_receiver, p_receiver_func).bind(p_userdata));
  423. }
  424. void EditorResourcePreview::queue_resource_preview(const String &p_path, const Callable &p_callback) {
  425. _update_thumbnail_sizes();
  426. {
  427. MutexLock lock(preview_mutex);
  428. const Item *cached_item = cache.getptr(p_path);
  429. if (cached_item) {
  430. p_callback.call(p_path, cached_item->preview, cached_item->small_preview);
  431. return;
  432. }
  433. QueueItem item;
  434. item.path = p_path;
  435. item.callback = p_callback;
  436. queue.push_back(item);
  437. }
  438. preview_sem.post();
  439. }
  440. void EditorResourcePreview::add_preview_generator(const Ref<EditorResourcePreviewGenerator> &p_generator) {
  441. preview_generators.push_back(p_generator);
  442. }
  443. void EditorResourcePreview::remove_preview_generator(const Ref<EditorResourcePreviewGenerator> &p_generator) {
  444. preview_generators.erase(p_generator);
  445. }
  446. EditorResourcePreview *EditorResourcePreview::get_singleton() {
  447. return singleton;
  448. }
  449. void EditorResourcePreview::_bind_methods() {
  450. ClassDB::bind_method(D_METHOD("queue_resource_preview", "path", "receiver", "receiver_func", "userdata"), &EditorResourcePreview::_queue_resource_preview);
  451. ClassDB::bind_method(D_METHOD("queue_edited_resource_preview", "resource", "receiver", "receiver_func", "userdata"), &EditorResourcePreview::_queue_edited_resource_preview);
  452. ClassDB::bind_method(D_METHOD("add_preview_generator", "generator"), &EditorResourcePreview::add_preview_generator);
  453. ClassDB::bind_method(D_METHOD("remove_preview_generator", "generator"), &EditorResourcePreview::remove_preview_generator);
  454. ClassDB::bind_method(D_METHOD("check_for_invalidation", "path"), &EditorResourcePreview::check_for_invalidation);
  455. ADD_SIGNAL(MethodInfo("preview_invalidated", PropertyInfo(Variant::STRING, "path")));
  456. }
  457. void EditorResourcePreview::_notification(int p_what) {
  458. switch (p_what) {
  459. case NOTIFICATION_EXIT_TREE: {
  460. stop();
  461. } break;
  462. }
  463. }
  464. void EditorResourcePreview::check_for_invalidation(const String &p_path) {
  465. bool call_invalidated = false;
  466. {
  467. MutexLock lock(preview_mutex);
  468. if (cache.has(p_path)) {
  469. uint64_t modified_time = FileAccess::get_modified_time(p_path);
  470. String import_path = p_path + ".import";
  471. if (FileAccess::exists(import_path)) {
  472. modified_time = MAX(modified_time, FileAccess::get_modified_time(import_path));
  473. }
  474. if (modified_time != cache[p_path].modified_time) {
  475. cache.erase(p_path);
  476. call_invalidated = true;
  477. }
  478. }
  479. }
  480. if (call_invalidated) { //do outside mutex
  481. call_deferred(SNAME("emit_signal"), "preview_invalidated", p_path);
  482. }
  483. }
  484. void EditorResourcePreview::start() {
  485. if (DisplayServer::get_singleton()->get_name() == "headless") {
  486. return;
  487. }
  488. if (is_threaded()) {
  489. ERR_FAIL_COND_MSG(thread.is_started(), "Thread already started.");
  490. thread.start(_thread_func, this);
  491. } else {
  492. SceneTree *st = Object::cast_to<SceneTree>(OS::get_singleton()->get_main_loop());
  493. ERR_FAIL_NULL_MSG(st, "Editor's MainLoop is not a SceneTree. This is a bug.");
  494. st->add_idle_callback(&_idle_callback);
  495. }
  496. }
  497. void EditorResourcePreview::stop() {
  498. if (is_threaded()) {
  499. if (thread.is_started()) {
  500. exiting.set();
  501. preview_sem.post();
  502. for (int i = 0; i < preview_generators.size(); i++) {
  503. preview_generators.write[i]->abort();
  504. }
  505. while (!exited.is_set()) {
  506. // Sync pending work.
  507. OS::get_singleton()->delay_usec(10000);
  508. RenderingServer::get_singleton()->sync();
  509. MessageQueue::get_singleton()->flush();
  510. }
  511. thread.wait_to_finish();
  512. }
  513. }
  514. }
  515. EditorResourcePreview::EditorResourcePreview() {
  516. singleton = this;
  517. }
  518. EditorResourcePreview::~EditorResourcePreview() {
  519. stop();
  520. }