editor_resource_preview.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. #include "scene/resources/texture.h"
  37. class EditorResourcePreviewGenerator : public RefCounted {
  38. GDCLASS(EditorResourcePreviewGenerator, RefCounted);
  39. protected:
  40. static void _bind_methods();
  41. GDVIRTUAL1RC(bool, _handles, String)
  42. GDVIRTUAL2RC(Ref<Texture2D>, _generate, Ref<Resource>, Vector2i)
  43. GDVIRTUAL2RC(Ref<Texture2D>, _generate_from_path, String, Vector2i)
  44. GDVIRTUAL0RC(bool, _generate_small_preview_automatically)
  45. GDVIRTUAL0RC(bool, _can_generate_small_preview)
  46. public:
  47. virtual bool handles(const String &p_type) const;
  48. virtual Ref<Texture2D> generate(const Ref<Resource> &p_from, const Size2 &p_size) const;
  49. virtual Ref<Texture2D> generate_from_path(const String &p_path, const Size2 &p_size) const;
  50. virtual bool generate_small_preview_automatically() const;
  51. virtual bool can_generate_small_preview() const;
  52. EditorResourcePreviewGenerator();
  53. };
  54. class EditorResourcePreview : public Node {
  55. GDCLASS(EditorResourcePreview, Node);
  56. static EditorResourcePreview *singleton;
  57. struct QueueItem {
  58. Ref<Resource> resource;
  59. String path;
  60. ObjectID id;
  61. StringName function;
  62. Variant userdata;
  63. };
  64. List<QueueItem> queue;
  65. Mutex preview_mutex;
  66. Semaphore preview_sem;
  67. Thread thread;
  68. SafeFlag exit;
  69. SafeFlag exited;
  70. struct Item {
  71. Ref<Texture2D> preview;
  72. Ref<Texture2D> small_preview;
  73. int order = 0;
  74. uint32_t last_hash = 0;
  75. uint64_t modified_time = 0;
  76. };
  77. int order;
  78. HashMap<String, Item> cache;
  79. void _preview_ready(const String &p_str, const Ref<Texture2D> &p_texture, const Ref<Texture2D> &p_small_texture, ObjectID id, const StringName &p_func, const Variant &p_ud);
  80. void _generate_preview(Ref<ImageTexture> &r_texture, Ref<ImageTexture> &r_small_texture, const QueueItem &p_item, const String &cache_base);
  81. static void _thread_func(void *ud);
  82. void _thread();
  83. void _iterate();
  84. Vector<Ref<EditorResourcePreviewGenerator>> preview_generators;
  85. protected:
  86. static void _bind_methods();
  87. public:
  88. static EditorResourcePreview *get_singleton();
  89. // p_receiver_func callback has signature (String p_path, Ref<Texture2D> p_preview, Ref<Texture2D> p_preview_small, Variant p_userdata)
  90. // p_preview will be null if there was an error
  91. void queue_resource_preview(const String &p_path, Object *p_receiver, const StringName &p_receiver_func, const Variant &p_userdata);
  92. void queue_edited_resource_preview(const Ref<Resource> &p_res, Object *p_receiver, const StringName &p_receiver_func, const Variant &p_userdata);
  93. void add_preview_generator(const Ref<EditorResourcePreviewGenerator> &p_generator);
  94. void remove_preview_generator(const Ref<EditorResourcePreviewGenerator> &p_generator);
  95. void check_for_invalidation(const String &p_path);
  96. void start();
  97. void stop();
  98. EditorResourcePreview();
  99. ~EditorResourcePreview();
  100. };
  101. #endif // EDITOR_RESOURCE_PREVIEW_H