Path: blob/master/editor/inspector/editor_resource_preview.h
21416 views
/**************************************************************************/1/* editor_resource_preview.h */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#pragma once3132#include "core/os/semaphore.h"33#include "core/os/thread.h"34#include "core/templates/safe_refcount.h"35#include "scene/main/node.h"3637class ImageTexture;38class Texture2D;3940class EditorResourcePreviewGenerator : public RefCounted {41GDCLASS(EditorResourcePreviewGenerator, RefCounted);4243protected:44static void _bind_methods();4546GDVIRTUAL1RC_REQUIRED(bool, _handles, String)47GDVIRTUAL3RC_REQUIRED(Ref<Texture2D>, _generate, Ref<Resource>, Vector2i, Dictionary)48GDVIRTUAL3RC(Ref<Texture2D>, _generate_from_path, String, Vector2i, Dictionary)49GDVIRTUAL0RC(bool, _generate_small_preview_automatically)50GDVIRTUAL0RC(bool, _can_generate_small_preview)5152class DrawRequester : public Object {53Semaphore semaphore;5455void _post_semaphore();56void _prepare_draw(RID p_viewport);5758public:59void request_and_wait(RID p_viewport);60void abort();61};6263public:64virtual bool handles(const String &p_type) const;65virtual Ref<Texture2D> generate(const Ref<Resource> &p_from, const Size2 &p_size, Dictionary &p_metadata) const;66virtual Ref<Texture2D> generate_from_path(const String &p_path, const Size2 &p_size, Dictionary &p_metadata) const;67virtual void abort() {}6869virtual bool generate_small_preview_automatically() const;70virtual bool can_generate_small_preview() const;71void request_draw_and_wait(RID viewport) const;72};7374class EditorResourcePreview : public Node {75GDCLASS(EditorResourcePreview, Node);7677static constexpr int CURRENT_METADATA_VERSION = 1; // Increment this number to invalidate all previews.78inline static EditorResourcePreview *singleton = nullptr;7980struct QueueItem {81Ref<Resource> resource;82String path;83Callable callback;84};8586List<QueueItem> queue;8788Mutex preview_mutex;89Semaphore preview_sem;90Thread thread;91SafeFlag exiting;92SafeFlag exited;9394struct Item {95Ref<Texture2D> preview;96Ref<Texture2D> small_preview;97Dictionary preview_metadata;98uint32_t last_hash = 0;99uint64_t modified_time = 0;100};101102HashMap<String, Item> cache;103104void _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);105void _generate_preview(Ref<ImageTexture> &r_texture, Ref<ImageTexture> &r_small_texture, const QueueItem &p_item, const String &cache_base, Dictionary &p_metadata);106107int small_thumbnail_size = -1;108109static void _thread_func(void *ud);110void _thread(); // For rendering drivers supporting async texture creation.111static void _idle_callback(); // For other rendering drivers (i.e., OpenGL).112void _iterate();113114void _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);115void _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);116117Vector<Ref<EditorResourcePreviewGenerator>> preview_generators;118119void _update_thumbnail_sizes();120121// TODO: These should be deprecated and the new methods exposed instead.122void _queue_resource_preview(const String &p_path, Object *p_receiver, const StringName &p_receiver_func, const Variant &p_userdata);123void _queue_edited_resource_preview(const Ref<Resource> &p_res, Object *p_receiver, const StringName &p_receiver_func, const Variant &p_userdata);124125protected:126void _notification(int p_what);127static void _bind_methods();128129public:130static EditorResourcePreview *get_singleton();131132struct PreviewItem {133Ref<Texture2D> preview;134Ref<Texture2D> small_preview;135};136137void queue_resource_preview(const String &p_path, const Callable &p_callback);138void queue_edited_resource_preview(const Ref<Resource> &p_res, const Callable &p_callback);139const Dictionary get_preview_metadata(const String &p_path) const;140141PreviewItem get_resource_preview_if_available(const String &p_path);142143void add_preview_generator(const Ref<EditorResourcePreviewGenerator> &p_generator);144void remove_preview_generator(const Ref<EditorResourcePreviewGenerator> &p_generator);145void check_for_invalidation(const String &p_path);146147void start();148void stop();149bool is_threaded() const;150151EditorResourcePreview();152~EditorResourcePreview();153};154155156