2
0
Эх сурвалжийг харах

Cleaned up preview generation thread, fixes #5499

Juan Linietsky 9 жил өмнө
parent
commit
15d52d6c95

+ 70 - 62
tools/editor/editor_resource_preview.cpp

@@ -33,6 +33,7 @@
 #include "io/resource_saver.h"
 #include "globals.h"
 #include "editor_scale.h"
+#include "message_queue.h"
 
 Ref<Texture> EditorResourcePreviewGenerator::generate_from_path(const String& p_path) {
 
@@ -83,12 +84,10 @@ void EditorResourcePreview::_preview_ready(const String& p_str,const Ref<Texture
 
 	cache[path]=item;
 
-	Object *recv = ObjectDB::get_instance(id);
-	if (recv) {
-		recv->call_deferred(p_func,path,p_texture,p_ud);
-	}
-
 	preview_mutex->unlock();
+
+	MessageQueue::get_singleton()->push_call(id,p_func,path,p_texture,p_ud);
+
 }
 
 Ref<Texture> EditorResourcePreview::_generate_preview(const QueueItem& p_item,const String& cache_base) {
@@ -107,6 +106,7 @@ Ref<Texture> EditorResourcePreview::_generate_preview(const QueueItem& p_item,co
 	Ref<Texture> generated;
 
 	for(int i=0;i<preview_generators.size();i++) {
+
 		if (!preview_generators[i]->handles(type))
 			continue;
 		if (p_item.resource.is_valid()) {
@@ -157,99 +157,107 @@ void EditorResourcePreview::_thread() {
 
 			QueueItem item = queue.front()->get();
 			queue.pop_front();
-			preview_mutex->unlock();
-
-			Ref<Texture> texture;
-
-			//print_line("pop from queue "+item.path);
-
-			int thumbnail_size = EditorSettings::get_singleton()->get("file_dialog/thumbnail_size");
-			thumbnail_size*=EDSCALE;
 
 			if (cache.has(item.path)) {
 				//already has it because someone loaded it, just let it know it's ready
 				if (item.resource.is_valid()) {
 					item.path+=":"+itos(cache[item.path].last_hash); //keep last hash (see description of what this is in condition below)
 				}
-				call_deferred("_preview_ready",item.path,cache[item.path].preview,item.id,item.function,item.userdata);
-
-			} else if (item.resource.is_valid()){
 
-				texture=_generate_preview(item,String());
-				//adding hash to the end of path (should be ID:<objid>:<hash>) because of 5 argument limit to call_deferred
-				call_deferred("_preview_ready",item.path+":"+itos(item.resource->hash_edited_version()),texture,item.id,item.function,item.userdata);
+				_preview_ready(item.path,cache[item.path].preview,item.id,item.function,item.userdata);
 
+				preview_mutex->unlock();
 			} else {
+				preview_mutex->unlock();
+
 
+				Ref<Texture> texture;
 
-				String temp_path=EditorSettings::get_singleton()->get_settings_path().plus_file("tmp");
-				String cache_base = Globals::get_singleton()->globalize_path(item.path).md5_text();
-				cache_base = temp_path.plus_file("resthumb-"+cache_base);
+				//print_line("pop from queue "+item.path);
 
-				//does not have it, try to load a cached thumbnail
+				int thumbnail_size = EditorSettings::get_singleton()->get("file_dialog/thumbnail_size");
+				thumbnail_size*=EDSCALE;
 
-				String file = cache_base+".txt";
-				//print_line("cachetxt at "+file);
-				FileAccess *f=FileAccess::open(file,FileAccess::READ);
-				if (!f) {
 
-					//print_line("generate because not cached");
+				if (item.resource.is_valid()){
+
+					texture=_generate_preview(item,String());
+					//adding hash to the end of path (should be ID:<objid>:<hash>) because of 5 argument limit to call_deferred
+					_preview_ready(item.path+":"+itos(item.resource->hash_edited_version()),texture,item.id,item.function,item.userdata);
 
-					//generate
-					texture=_generate_preview(item,cache_base);
 				} else {
 
-					uint64_t modtime = FileAccess::get_modified_time(item.path);
-					int tsize = f->get_line().to_int64();
-					uint64_t last_modtime = f->get_line().to_int64();
 
-					bool cache_valid = true;
+					String temp_path=EditorSettings::get_singleton()->get_settings_path().plus_file("tmp");
+					String cache_base = Globals::get_singleton()->globalize_path(item.path).md5_text();
+					cache_base = temp_path.plus_file("resthumb-"+cache_base);
+
+					//does not have it, try to load a cached thumbnail
 
-					if (tsize!=thumbnail_size) {
-						cache_valid=false;
-						memdelete(f);
-					} else if (last_modtime!=modtime) {
+					String file = cache_base+".txt";
+					//print_line("cachetxt at "+file);
+					FileAccess *f=FileAccess::open(file,FileAccess::READ);
+					if (!f) {
+
+						//print_line("generate because not cached");
+
+						//generate
+						texture=_generate_preview(item,cache_base);
+					} else {
 
-						String last_md5 = f->get_line();
-						String md5 = FileAccess::get_md5(item.path);
-						memdelete(f);
+						uint64_t modtime = FileAccess::get_modified_time(item.path);
+						int tsize = f->get_line().to_int64();
+						uint64_t last_modtime = f->get_line().to_int64();
 
-						if (last_md5!=md5) {
+						bool cache_valid = true;
 
+						if (tsize!=thumbnail_size) {
 							cache_valid=false;
-						} else {
-							//update modified time
+							memdelete(f);
+						} else if (last_modtime!=modtime) {
 
-							f=FileAccess::open(file,FileAccess::WRITE);
-							f->store_line(itos(modtime));
-							f->store_line(md5);
+							String last_md5 = f->get_line();
+							String md5 = FileAccess::get_md5(item.path);
+							memdelete(f);
+
+							if (last_md5!=md5) {
+
+								cache_valid=false;
+							} else {
+								//update modified time
+
+								f=FileAccess::open(file,FileAccess::WRITE);
+								f->store_line(itos(modtime));
+								f->store_line(md5);
+								memdelete(f);
+							}
+						} else {
 							memdelete(f);
 						}
-					} else {
-						memdelete(f);
-					}
 
-					if (cache_valid) {
+						if (cache_valid) {
 
-						texture = ResourceLoader::load(cache_base+".png","ImageTexture",true);
-						if (!texture.is_valid()) {
-							//well fuck
-							cache_valid=false;
+							texture = ResourceLoader::load(cache_base+".png","ImageTexture",true);
+							if (!texture.is_valid()) {
+								//well fuck
+								cache_valid=false;
+							}
 						}
-					}
 
-					if (!cache_valid) {
+						if (!cache_valid) {
 
-						texture=_generate_preview(item,cache_base);
-					}
+							texture=_generate_preview(item,cache_base);
+						}
 
-				}
+					}
 
-				//print_line("notify of preview ready");
-				call_deferred("_preview_ready",item.path,texture,item.id,item.function,item.userdata);
+					//print_line("notify of preview ready");
+					_preview_ready(item.path,texture,item.id,item.function,item.userdata);
 
+				}
 			}
 
+
 		} else {
 			preview_mutex->unlock();
 		}