editor_resource_preview.cpp 21 KB

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