Cleaned up preview generation thread, fixes #5499

This commit is contained in:
Juan Linietsky 2016-07-09 11:19:49 -03:00
parent dd01286137
commit 15d52d6c95
1 changed files with 71 additions and 63 deletions

View File

@ -33,6 +33,7 @@
#include "io/resource_saver.h" #include "io/resource_saver.h"
#include "globals.h" #include "globals.h"
#include "editor_scale.h" #include "editor_scale.h"
#include "message_queue.h"
Ref<Texture> EditorResourcePreviewGenerator::generate_from_path(const String& p_path) { 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; cache[path]=item;
Object *recv = ObjectDB::get_instance(id);
if (recv) {
recv->call_deferred(p_func,path,p_texture,p_ud);
}
preview_mutex->unlock(); 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) { 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; Ref<Texture> generated;
for(int i=0;i<preview_generators.size();i++) { for(int i=0;i<preview_generators.size();i++) {
if (!preview_generators[i]->handles(type)) if (!preview_generators[i]->handles(type))
continue; continue;
if (p_item.resource.is_valid()) { if (p_item.resource.is_valid()) {
@ -157,99 +157,107 @@ void EditorResourcePreview::_thread() {
QueueItem item = queue.front()->get(); QueueItem item = queue.front()->get();
queue.pop_front(); 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)) { if (cache.has(item.path)) {
//already has it because someone loaded it, just let it know it's ready //already has it because someone loaded it, just let it know it's ready
if (item.resource.is_valid()) { 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) 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()){ _preview_ready(item.path,cache[item.path].preview,item.id,item.function,item.userdata);
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_mutex->unlock();
} else { } else {
preview_mutex->unlock();
String temp_path=EditorSettings::get_singleton()->get_settings_path().plus_file("tmp"); Ref<Texture> texture;
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 //print_line("pop from queue "+item.path);
String file = cache_base+".txt"; int thumbnail_size = EditorSettings::get_singleton()->get("file_dialog/thumbnail_size");
//print_line("cachetxt at "+file); thumbnail_size*=EDSCALE;
FileAccess *f=FileAccess::open(file,FileAccess::READ);
if (!f) {
//print_line("generate because not cached");
//generate if (item.resource.is_valid()){
texture=_generate_preview(item,cache_base);
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);
} else { } 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);
if (tsize!=thumbnail_size) { //does not have it, try to load a cached thumbnail
cache_valid=false;
memdelete(f);
} else if (last_modtime!=modtime) {
String last_md5 = f->get_line(); String file = cache_base+".txt";
String md5 = FileAccess::get_md5(item.path); //print_line("cachetxt at "+file);
memdelete(f); FileAccess *f=FileAccess::open(file,FileAccess::READ);
if (!f) {
if (last_md5!=md5) { //print_line("generate because not cached");
//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;
if (tsize!=thumbnail_size) {
cache_valid=false; cache_valid=false;
} else { memdelete(f);
//update modified time } else if (last_modtime!=modtime) {
f=FileAccess::open(file,FileAccess::WRITE); String last_md5 = f->get_line();
f->store_line(itos(modtime)); String md5 = FileAccess::get_md5(item.path);
f->store_line(md5); 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); memdelete(f);
} }
} else {
memdelete(f);
}
if (cache_valid) { if (cache_valid) {
texture = ResourceLoader::load(cache_base+".png","ImageTexture",true); texture = ResourceLoader::load(cache_base+".png","ImageTexture",true);
if (!texture.is_valid()) { if (!texture.is_valid()) {
//well fuck //well fuck
cache_valid=false; cache_valid=false;
}
} }
if (!cache_valid) {
texture=_generate_preview(item,cache_base);
}
} }
if (!cache_valid) { //print_line("notify of preview ready");
_preview_ready(item.path,texture,item.id,item.function,item.userdata);
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);
} }
} else { } else {
preview_mutex->unlock(); preview_mutex->unlock();
} }