editor_resource_preview.cpp 20 KB

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