editor_resource_preview.cpp 21 KB

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