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