editor_resource_preview.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. #ifndef EDITOR_RESOURCE_PREVIEW_H
  31. #define EDITOR_RESOURCE_PREVIEW_H
  32. #include "core/os/semaphore.h"
  33. #include "core/os/thread.h"
  34. #include "core/templates/safe_refcount.h"
  35. #include "scene/main/node.h"
  36. class ImageTexture;
  37. class Texture2D;
  38. class EditorResourcePreviewGenerator : public RefCounted {
  39. GDCLASS(EditorResourcePreviewGenerator, RefCounted);
  40. protected:
  41. static void _bind_methods();
  42. GDVIRTUAL1RC(bool, _handles, String)
  43. GDVIRTUAL3RC(Ref<Texture2D>, _generate, Ref<Resource>, Vector2i, Dictionary)
  44. GDVIRTUAL3RC(Ref<Texture2D>, _generate_from_path, String, Vector2i, Dictionary)
  45. GDVIRTUAL0RC(bool, _generate_small_preview_automatically)
  46. GDVIRTUAL0RC(bool, _can_generate_small_preview)
  47. class DrawRequester : public Object {
  48. Semaphore semaphore;
  49. Variant _post_semaphore();
  50. public:
  51. void request_and_wait(RID p_viewport);
  52. void abort();
  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. virtual void abort() {}
  59. virtual bool generate_small_preview_automatically() const;
  60. virtual bool can_generate_small_preview() const;
  61. EditorResourcePreviewGenerator();
  62. };
  63. class EditorResourcePreview : public Node {
  64. GDCLASS(EditorResourcePreview, Node);
  65. inline static constexpr int CURRENT_METADATA_VERSION = 1; // Increment this number to invalidate all previews.
  66. inline static EditorResourcePreview *singleton = nullptr;
  67. struct QueueItem {
  68. Ref<Resource> resource;
  69. String path;
  70. ObjectID id;
  71. StringName function;
  72. Variant userdata;
  73. };
  74. List<QueueItem> queue;
  75. Mutex preview_mutex;
  76. Semaphore preview_sem;
  77. Thread thread;
  78. SafeFlag exiting;
  79. SafeFlag exited;
  80. struct Item {
  81. Ref<Texture2D> preview;
  82. Ref<Texture2D> small_preview;
  83. Dictionary preview_metadata;
  84. uint32_t last_hash = 0;
  85. uint64_t modified_time = 0;
  86. };
  87. HashMap<String, Item> cache;
  88. 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);
  89. void _generate_preview(Ref<ImageTexture> &r_texture, Ref<ImageTexture> &r_small_texture, const QueueItem &p_item, const String &cache_base, Dictionary &p_metadata);
  90. int small_thumbnail_size = -1;
  91. static void _thread_func(void *ud);
  92. void _thread(); // For rendering drivers supporting async texture creation.
  93. static void _idle_callback(); // For other rendering drivers (i.e., OpenGL).
  94. void _iterate();
  95. 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);
  96. 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);
  97. Vector<Ref<EditorResourcePreviewGenerator>> preview_generators;
  98. void _update_thumbnail_sizes();
  99. protected:
  100. static void _bind_methods();
  101. public:
  102. static EditorResourcePreview *get_singleton();
  103. struct PreviewItem {
  104. Ref<Texture2D> preview;
  105. Ref<Texture2D> small_preview;
  106. };
  107. // p_receiver_func callback has signature (String p_path, Ref<Texture2D> p_preview, Ref<Texture2D> p_preview_small, Variant p_userdata)
  108. // p_preview will be null if there was an error
  109. void queue_resource_preview(const String &p_path, Object *p_receiver, const StringName &p_receiver_func, const Variant &p_userdata);
  110. void queue_edited_resource_preview(const Ref<Resource> &p_res, Object *p_receiver, const StringName &p_receiver_func, const Variant &p_userdata);
  111. const Dictionary get_preview_metadata(const String &p_path) const;
  112. PreviewItem get_resource_preview_if_available(const String &p_path);
  113. void add_preview_generator(const Ref<EditorResourcePreviewGenerator> &p_generator);
  114. void remove_preview_generator(const Ref<EditorResourcePreviewGenerator> &p_generator);
  115. void check_for_invalidation(const String &p_path);
  116. void start();
  117. void stop();
  118. bool is_threaded() const;
  119. EditorResourcePreview();
  120. ~EditorResourcePreview();
  121. };
  122. #endif // EDITOR_RESOURCE_PREVIEW_H