Skip to content

Commit 6bb89c7

Browse files
committed
Merge pull request #87229 from RandomShaper/gl_preview_goodboy
Run resource previewer on the main thread if using GL compatibility
2 parents 3ba18b3 + e5454cd commit 6bb89c7

File tree

2 files changed

+38
-13
lines changed

2 files changed

+38
-13
lines changed

editor/editor_resource_preview.cpp

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
#include "editor/editor_settings.h"
4242
#include "editor/editor_string_names.h"
4343
#include "scene/resources/image_texture.h"
44+
#include "servers/rendering/rendering_server_default.h"
4445

4546
bool EditorResourcePreviewGenerator::handles(const String &p_type) const {
4647
bool success = false;
@@ -338,6 +339,20 @@ void EditorResourcePreview::_thread() {
338339
exited.set();
339340
}
340341

342+
void EditorResourcePreview::_idle_callback() {
343+
if (!singleton) {
344+
// Just in case the shutdown of the editor involves the deletion of the singleton
345+
// happening while additional idle callbacks can happen.
346+
return;
347+
}
348+
349+
// Process preview tasks, trying to leave a little bit of responsiveness worst case.
350+
uint64_t start = OS::get_singleton()->get_ticks_msec();
351+
while (!singleton->queue.is_empty() && OS::get_singleton()->get_ticks_msec() - start < 100) {
352+
singleton->_iterate();
353+
}
354+
}
355+
341356
void EditorResourcePreview::_update_thumbnail_sizes() {
342357
if (small_thumbnail_size == -1) {
343358
// Kind of a workaround to retrieve the default icon size.
@@ -441,27 +456,36 @@ void EditorResourcePreview::check_for_invalidation(const String &p_path) {
441456
}
442457

443458
void EditorResourcePreview::start() {
444-
if (DisplayServer::get_singleton()->get_name() != "headless") {
459+
if (DisplayServer::get_singleton()->get_name() == "headless") {
460+
return;
461+
}
462+
463+
if (RSG::texture_storage->can_create_resources_async()) {
445464
ERR_FAIL_COND_MSG(thread.is_started(), "Thread already started.");
446465
thread.start(_thread_func, this);
466+
} else {
467+
SceneTree *st = Object::cast_to<SceneTree>(OS::get_singleton()->get_main_loop());
468+
ERR_FAIL_NULL_MSG(st, "Editor's MainLoop is not a SceneTree. This is a bug.");
447469
}
448470
}
449471

450472
void EditorResourcePreview::stop() {
451-
if (thread.is_started()) {
452-
exiting.set();
453-
preview_sem.post();
473+
if (RSG::texture_storage->can_create_resources_async()) {
474+
if (thread.is_started()) {
475+
exiting.set();
476+
preview_sem.post();
454477

455-
for (int i = 0; i < preview_generators.size(); i++) {
456-
preview_generators.write[i]->abort();
457-
}
478+
for (int i = 0; i < preview_generators.size(); i++) {
479+
preview_generators.write[i]->abort();
480+
}
458481

459-
while (!exited.is_set()) {
460-
OS::get_singleton()->delay_usec(10000);
461-
RenderingServer::get_singleton()->sync(); //sync pending stuff, as thread may be blocked on rendering server
462-
}
482+
while (!exited.is_set()) {
483+
OS::get_singleton()->delay_usec(10000);
484+
RenderingServer::get_singleton()->sync(); //sync pending stuff, as thread may be blocked on rendering server
485+
}
463486

464-
thread.wait_to_finish();
487+
thread.wait_to_finish();
488+
}
465489
}
466490
}
467491

editor/editor_resource_preview.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,8 @@ class EditorResourcePreview : public Node {
103103
int small_thumbnail_size = -1;
104104

105105
static void _thread_func(void *ud);
106-
void _thread();
106+
void _thread(); // For rendering drivers supporting async texture creation.
107+
static void _idle_callback(); // For other rendering drivers (i.e., OpenGL).
107108
void _iterate();
108109

109110
void _write_preview_cache(Ref<FileAccess> p_file, int p_thumbnail_size, bool p_has_small_texture, uint64_t p_modified_time, String p_hash, const Dictionary &p_metadata);

0 commit comments

Comments
 (0)