summaryrefslogtreecommitdiff
path: root/tools/editor/editor_resource_preview.cpp
blob: 13b424c231a3d02a9f0bdec0b7e1c9cd17727aae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
#include "editor_resource_preview.h"
#include "editor_settings.h"
#include "os/file_access.h"
#include "io/resource_loader.h"
#include "io/resource_saver.h"
#include "globals.h"
#include "editor_scale.h"

Ref<Texture> EditorResourcePreviewGenerator::generate_from_path(const String& p_path) {

	RES res = ResourceLoader::load(p_path);
	if (!res.is_valid())
		return res;
	return generate(res);
}

EditorResourcePreviewGenerator::EditorResourcePreviewGenerator() {


}


EditorResourcePreview* EditorResourcePreview::singleton=NULL;


void EditorResourcePreview::_thread_func(void *ud) {

	EditorResourcePreview *erp=(EditorResourcePreview*)ud;
	erp->_thread();

}


void EditorResourcePreview::_preview_ready(const String& p_str,const Ref<Texture>& p_texture,ObjectID id,const StringName& p_func,const Variant& p_ud) {

	//print_line("preview is ready");
	preview_mutex->lock();

	String path = p_str;
	uint32_t hash=0;

	if (p_str.begins_with("ID:")) {
		hash=p_str.get_slicec(':',2).to_int();
		path="ID:"+p_str.get_slicec(':',1);
	}

	Item item;
	item.order=order++;
	item.preview=p_texture;
	item.last_hash=hash;

	cache[path]=item;

	Object *recv = ObjectDB::get_instance(id);
	if (recv) {
		recv->call_deferred(p_func,path,p_texture,p_ud);
	}

	preview_mutex->unlock();
}

Ref<Texture> EditorResourcePreview::_generate_preview(const QueueItem& p_item,const String& cache_base) {

	String type;

	if (p_item.resource.is_valid())
		type=p_item.resource->get_type();
	else
		type=ResourceLoader::get_resource_type(p_item.path);
	//print_line("resource type is: "+type);

	if (type=="")
		return Ref<Texture>(); //could not guess type

	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()) {
			generated = preview_generators[i]->generate(p_item.resource);
		} else {
			generated = preview_generators[i]->generate_from_path(p_item.path);
		}

		break;
	}

	if (!p_item.resource.is_valid()) {
		// cache the preview in case it's a resource on disk
		if (generated.is_valid()) {
			//print_line("was generated");
			int thumbnail_size = EditorSettings::get_singleton()->get("file_dialog/thumbnail_size");
			thumbnail_size*=EDSCALE;
			//wow it generated a preview... save cache
			ResourceSaver::save(cache_base+".png",generated);
			FileAccess *f=FileAccess::open(cache_base+".txt",FileAccess::WRITE);
			f->store_line(itos(thumbnail_size));
			f->store_line(itos(FileAccess::get_modified_time(p_item.path)));
			f->store_line(FileAccess::get_md5(p_item.path));
			memdelete(f);
		} else {
			//print_line("was not generated");

		}
	}

	return generated;
}

void EditorResourcePreview::_thread() {

	//print_line("begin thread");
	while(!exit) {

		//print_line("wait for semaphore");
		preview_sem->wait();
		preview_mutex->lock();

		//print_line("blue team go");

		if (queue.size()) {



			QueueItem item = queue.front()->get();
			queue.pop_front();
			preview_mutex->unlock();

			Ref<Texture> texture;

			//print_line("pop from queue "+item.path);

			uint64_t modtime = FileAccess::get_modified_time(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);

			} else {


				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

				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 {

					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;
						memdelete(f);
					} else if (last_modtime!=modtime) {

						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);
					}

					if (cache_valid) {

						texture = ResourceLoader::load(cache_base+".png","ImageTexture",true);
						if (!texture.is_valid()) {
							//well fuck
							cache_valid=false;
						}
					}

					if (!cache_valid) {

						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 {
			preview_mutex->unlock();
		}

	}
}



void EditorResourcePreview::queue_edited_resource_preview(const Ref<Resource>& p_res, Object* p_receiver, const StringName& p_receiver_func, const Variant& p_userdata) {

	ERR_FAIL_NULL(p_receiver);
	ERR_FAIL_COND(!p_res.is_valid());

	preview_mutex->lock();

	String path_id = "ID:"+itos(p_res->get_instance_ID());
	if (cache.has(path_id) && cache[path_id].last_hash==p_res->hash_edited_version()) {

		cache[path_id].order=order++;
		p_receiver->call_deferred(p_receiver_func,path_id,cache[path_id].preview,p_userdata);
		preview_mutex->unlock();
		return;

	}

	//print_line("send to thread "+p_path);
	QueueItem item;
	item.function=p_receiver_func;
	item.id=p_receiver->get_instance_ID();
	item.resource=p_res;
	item.path=path_id;
	item.userdata=p_userdata;

	queue.push_back(item);
	preview_mutex->unlock();
	preview_sem->post();
}

void EditorResourcePreview::queue_resource_preview(const String& p_path, Object* p_receiver, const StringName& p_receiver_func, const Variant& p_userdata) {

	ERR_FAIL_NULL(p_receiver);
	preview_mutex->lock();
	if (cache.has(p_path)) {
		cache[p_path].order=order++;
		p_receiver->call_deferred(p_receiver_func,p_path,cache[p_path].preview,p_userdata);
		preview_mutex->unlock();
		return;
	}

	//print_line("send to thread "+p_path);
	QueueItem item;
	item.function=p_receiver_func;
	item.id=p_receiver->get_instance_ID();
	item.path=p_path;
	item.userdata=p_userdata;

	queue.push_back(item);
	preview_mutex->unlock();
	preview_sem->post();

}

void EditorResourcePreview::add_preview_generator(const Ref<EditorResourcePreviewGenerator>& p_generator) {

	preview_generators.push_back(p_generator);
}

EditorResourcePreview* EditorResourcePreview::get_singleton() {

	return singleton;
}

void EditorResourcePreview::_bind_methods() {

	ObjectTypeDB::bind_method("_preview_ready",&EditorResourcePreview::_preview_ready);
}

EditorResourcePreview::EditorResourcePreview() {
	singleton=this;
	preview_mutex = Mutex::create();
	preview_sem  = Semaphore::create();
	order=0;
	exit=false;

	thread = Thread::create(_thread_func,this);
}


EditorResourcePreview::~EditorResourcePreview()
{

	exit=true;
	preview_sem->post();
	Thread::wait_to_finish(thread);
	memdelete(thread);
	memdelete(preview_mutex);
	memdelete(preview_sem);


}