| 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 |
#pragma once |
| 32 |
|
| 33 |
#include "core/os/semaphore.h" |
| 34 |
#include "core/os/thread.h" |
| 35 |
#include "core/templates/safe_refcount.h" |
| 36 |
#include "scene/main/node.h" |
| 37 |
|
| 38 |
class ImageTexture; |
| 39 |
class Texture2D; |
| 40 |
|
| 41 |
class EditorResourcePreviewGenerator : public RefCounted { |
| 42 |
GDCLASS(EditorResourcePreviewGenerator, RefCounted); |
| 43 |
|
| 44 |
protected: |
| 45 |
static void _bind_methods(); |
| 46 |
|
| 47 |
GDVIRTUAL1RC_REQUIRED(bool, _handles, String) |
| 48 |
GDVIRTUAL3RC_REQUIRED(Ref<Texture2D>, _generate, Ref<Resource>, Vector2i, Dictionary) |
| 49 |
GDVIRTUAL3RC(Ref<Texture2D>, _generate_from_path, String, Vector2i, Dictionary) |
| 50 |
GDVIRTUAL0RC(bool, _generate_small_preview_automatically) |
| 51 |
GDVIRTUAL0RC(bool, _can_generate_small_preview) |
| 52 |
|
| 53 |
class DrawRequester : public Object { |
| 54 |
Semaphore semaphore; |
| 55 |
|
| 56 |
void _post_semaphore(); |
| 57 |
void _prepare_draw(RID p_viewport); |
| 58 |
|
| 59 |
public: |
| 60 |
void request_and_wait(RID p_viewport); |
| 61 |
void abort(); |
| 62 |
}; |
| 63 |
|
| 64 |
public: |
| 65 |
virtual bool handles(const String &p_type) const; |
| 66 |
virtual Ref<Texture2D> generate(const Ref<Resource> &p_from, const Size2 &p_size, Dictionary &p_metadata) const; |
| 67 |
virtual Ref<Texture2D> generate_from_path(const String &p_path, const Size2 &p_size, Dictionary &p_metadata) const; |
| 68 |
virtual void abort() {} |
| 69 |
|
| 70 |
virtual bool generate_small_preview_automatically() const; |
| 71 |
virtual bool can_generate_small_preview() const; |
| 72 |
void request_draw_and_wait(RID viewport) const; |
| 73 |
}; |
| 74 |
|
| 75 |
class EditorResourcePreview : public Node { |
| 76 |
GDCLASS(EditorResourcePreview, Node); |
| 77 |
|
| 78 |
static constexpr int CURRENT_METADATA_VERSION = 1; |
| 79 |
inline static EditorResourcePreview *singleton = nullptr; |
| 80 |
|
| 81 |
struct QueueItem { |
| 82 |
Ref<Resource> resource; |
| 83 |
String path; |
| 84 |
Callable callback; |
| 85 |
}; |
| 86 |
|
| 87 |
List<QueueItem> queue; |
| 88 |
|
| 89 |
Mutex preview_mutex; |
| 90 |
Semaphore preview_sem; |
| 91 |
Thread thread; |
| 92 |
SafeFlag exiting; |
| 93 |
SafeFlag exited; |
| 94 |
|
| 95 |
struct Item { |
| 96 |
Ref<Texture2D> preview; |
| 97 |
Ref<Texture2D> small_preview; |
| 98 |
Dictionary preview_metadata; |
| 99 |
uint32_t last_hash = 0; |
| 100 |
uint64_t modified_time = 0; |
| 101 |
}; |
| 102 |
|
| 103 |
HashMap<String, Item> cache; |
| 104 |
|
| 105 |
void _preview_ready(const String &p_path, int p_hash, const Ref<Texture2D> &p_texture, const Ref<Texture2D> &p_small_texture, const Callable &p_callback, const Dictionary &p_metadata); |
| 106 |
void _generate_preview(Ref<ImageTexture> &r_texture, Ref<ImageTexture> &r_small_texture, const QueueItem &p_item, const String &cache_base, Dictionary &p_metadata); |
| 107 |
|
| 108 |
int small_thumbnail_size = -1; |
| 109 |
|
| 110 |
static void _thread_func(void *ud); |
| 111 |
void _thread(); |
| 112 |
static void _idle_callback(); |
| 113 |
void _iterate(); |
| 114 |
|
| 115 |
void _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); |
| 116 |
void _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); |
| 117 |
|
| 118 |
Vector<Ref<EditorResourcePreviewGenerator>> preview_generators; |
| 119 |
|
| 120 |
void _update_thumbnail_sizes(); |
| 121 |
|
| 122 |
|
| 123 |
void _queue_resource_preview(const String &p_path, Object *p_receiver, const StringName &p_receiver_func, const Variant &p_userdata); |
| 124 |
void _queue_edited_resource_preview(const Ref<Resource> &p_res, Object *p_receiver, const StringName &p_receiver_func, const Variant &p_userdata); |
| 125 |
|
| 126 |
protected: |
| 127 |
void _notification(int p_what); |
| 128 |
static void _bind_methods(); |
| 129 |
|
| 130 |
public: |
| 131 |
static EditorResourcePreview *get_singleton(); |
| 132 |
|
| 133 |
struct PreviewItem { |
| 134 |
Ref<Texture2D> preview; |
| 135 |
Ref<Texture2D> small_preview; |
| 136 |
}; |
| 137 |
|
| 138 |
void queue_resource_preview(const String &p_path, const Callable &p_callback); |
| 139 |
void queue_edited_resource_preview(const Ref<Resource> &p_res, const Callable &p_callback); |
| 140 |
const Dictionary get_preview_metadata(const String &p_path) const; |
| 141 |
|
| 142 |
PreviewItem get_resource_preview_if_available(const String &p_path); |
| 143 |
|
| 144 |
void add_preview_generator(const Ref<EditorResourcePreviewGenerator> &p_generator); |
| 145 |
void remove_preview_generator(const Ref<EditorResourcePreviewGenerator> &p_generator); |
| 146 |
void check_for_invalidation(const String &p_path); |
| 147 |
|
| 148 |
void start(); |
| 149 |
void stop(); |
| 150 |
bool is_threaded() const; |
| 151 |
|
| 152 |
EditorResourcePreview(); |
| 153 |
~EditorResourcePreview(); |
| 154 |
}; |