| 1 | /**************************************************************************/ |
| 2 | /* editor_resource_preview.h */ |
| 3 | /**************************************************************************/ |
| 4 | /* This file is part of: */ |
| 5 | /* GODOT ENGINE */ |
| 6 | /* https://godotengine.org */ |
| 7 | /**************************************************************************/ |
| 8 | /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ |
| 9 | /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ |
| 10 | /* */ |
| 11 | /* Permission is hereby granted, free of charge, to any person obtaining */ |
| 12 | /* a copy of this software and associated documentation files (the */ |
| 13 | /* "Software"), to deal in the Software without restriction, including */ |
| 14 | /* without limitation the rights to use, copy, modify, merge, publish, */ |
| 15 | /* distribute, sublicense, and/or sell copies of the Software, and to */ |
| 16 | /* permit persons to whom the Software is furnished to do so, subject to */ |
| 17 | /* the following conditions: */ |
| 18 | /* */ |
| 19 | /* The above copyright notice and this permission notice shall be */ |
| 20 | /* included in all copies or substantial portions of the Software. */ |
| 21 | /* */ |
| 22 | /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ |
| 23 | /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ |
| 24 | /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ |
| 25 | /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ |
| 26 | /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ |
| 27 | /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ |
| 28 | /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ |
| 29 | /**************************************************************************/ |
| 30 | |
| 31 | #ifndef EDITOR_RESOURCE_PREVIEW_H |
| 32 | #define EDITOR_RESOURCE_PREVIEW_H |
| 33 | |
| 34 | #include "core/os/semaphore.h" |
| 35 | #include "core/os/thread.h" |
| 36 | #include "core/templates/safe_refcount.h" |
| 37 | #include "scene/main/node.h" |
| 38 | |
| 39 | class ImageTexture; |
| 40 | class Texture2D; |
| 41 | |
| 42 | class EditorResourcePreviewGenerator : public RefCounted { |
| 43 | GDCLASS(EditorResourcePreviewGenerator, RefCounted); |
| 44 | |
| 45 | protected: |
| 46 | static void _bind_methods(); |
| 47 | |
| 48 | GDVIRTUAL1RC(bool, _handles, String) |
| 49 | GDVIRTUAL3RC(Ref<Texture2D>, _generate, Ref<Resource>, Vector2i, Dictionary) |
| 50 | GDVIRTUAL3RC(Ref<Texture2D>, _generate_from_path, String, Vector2i, Dictionary) |
| 51 | GDVIRTUAL0RC(bool, _generate_small_preview_automatically) |
| 52 | GDVIRTUAL0RC(bool, _can_generate_small_preview) |
| 53 | |
| 54 | public: |
| 55 | virtual bool handles(const String &p_type) const; |
| 56 | virtual Ref<Texture2D> generate(const Ref<Resource> &p_from, const Size2 &p_size, Dictionary &p_metadata) const; |
| 57 | virtual Ref<Texture2D> generate_from_path(const String &p_path, const Size2 &p_size, Dictionary &p_metadata) const; |
| 58 | |
| 59 | virtual bool generate_small_preview_automatically() const; |
| 60 | virtual bool can_generate_small_preview() const; |
| 61 | |
| 62 | EditorResourcePreviewGenerator(); |
| 63 | }; |
| 64 | |
| 65 | class EditorResourcePreview : public Node { |
| 66 | GDCLASS(EditorResourcePreview, Node); |
| 67 | |
| 68 | static EditorResourcePreview *singleton; |
| 69 | |
| 70 | struct QueueItem { |
| 71 | Ref<Resource> resource; |
| 72 | String path; |
| 73 | ObjectID id; |
| 74 | StringName function; |
| 75 | Variant userdata; |
| 76 | }; |
| 77 | |
| 78 | List<QueueItem> queue; |
| 79 | |
| 80 | Mutex preview_mutex; |
| 81 | Semaphore preview_sem; |
| 82 | Thread thread; |
| 83 | SafeFlag exit; |
| 84 | SafeFlag exited; |
| 85 | |
| 86 | struct Item { |
| 87 | Ref<Texture2D> preview; |
| 88 | Ref<Texture2D> small_preview; |
| 89 | Dictionary preview_metadata; |
| 90 | int order = 0; |
| 91 | uint32_t last_hash = 0; |
| 92 | uint64_t modified_time = 0; |
| 93 | }; |
| 94 | |
| 95 | int order; |
| 96 | |
| 97 | HashMap<String, Item> cache; |
| 98 | |
| 99 | void _preview_ready(const String &p_path, int p_hash, const Ref<Texture2D> &p_texture, const Ref<Texture2D> &p_small_texture, ObjectID id, const StringName &p_func, const Variant &p_ud, const Dictionary &p_metadata); |
| 100 | void _generate_preview(Ref<ImageTexture> &r_texture, Ref<ImageTexture> &r_small_texture, const QueueItem &p_item, const String &cache_base, Dictionary &p_metadata); |
| 101 | |
| 102 | int small_thumbnail_size = -1; |
| 103 | |
| 104 | static void _thread_func(void *ud); |
| 105 | void _thread(); |
| 106 | void _iterate(); |
| 107 | |
| 108 | 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); |
| 109 | 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); |
| 110 | |
| 111 | Vector<Ref<EditorResourcePreviewGenerator>> preview_generators; |
| 112 | |
| 113 | void _update_thumbnail_sizes(); |
| 114 | |
| 115 | protected: |
| 116 | static void _bind_methods(); |
| 117 | |
| 118 | public: |
| 119 | static EditorResourcePreview *get_singleton(); |
| 120 | |
| 121 | // p_receiver_func callback has signature (String p_path, Ref<Texture2D> p_preview, Ref<Texture2D> p_preview_small, Variant p_userdata) |
| 122 | // p_preview will be null if there was an error |
| 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 | const Dictionary get_preview_metadata(const String &p_path) const; |
| 126 | |
| 127 | void add_preview_generator(const Ref<EditorResourcePreviewGenerator> &p_generator); |
| 128 | void remove_preview_generator(const Ref<EditorResourcePreviewGenerator> &p_generator); |
| 129 | void check_for_invalidation(const String &p_path); |
| 130 | |
| 131 | void start(); |
| 132 | void stop(); |
| 133 | |
| 134 | EditorResourcePreview(); |
| 135 | ~EditorResourcePreview(); |
| 136 | }; |
| 137 | |
| 138 | #endif // EDITOR_RESOURCE_PREVIEW_H |
| 139 | |