Path: blob/master/editor/inspector/editor_resource_preview.h
9896 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(bool, _handles, String)47GDVIRTUAL3RC(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;5455Variant _post_semaphore();5657public:58void request_and_wait(RID p_viewport);59void abort();60};6162public:63virtual bool handles(const String &p_type) const;64virtual Ref<Texture2D> generate(const Ref<Resource> &p_from, const Size2 &p_size, Dictionary &p_metadata) const;65virtual Ref<Texture2D> generate_from_path(const String &p_path, const Size2 &p_size, Dictionary &p_metadata) const;66virtual void abort() {}6768virtual bool generate_small_preview_automatically() const;69virtual bool can_generate_small_preview() const;70};7172class EditorResourcePreview : public Node {73GDCLASS(EditorResourcePreview, Node);7475static constexpr int CURRENT_METADATA_VERSION = 1; // Increment this number to invalidate all previews.76inline static EditorResourcePreview *singleton = nullptr;7778struct QueueItem {79Ref<Resource> resource;80String path;81ObjectID id;82StringName function;83Variant userdata;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, ObjectID id, const StringName &p_func, const Variant &p_ud, 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();120121protected:122void _notification(int p_what);123static void _bind_methods();124125public:126static EditorResourcePreview *get_singleton();127128struct PreviewItem {129Ref<Texture2D> preview;130Ref<Texture2D> small_preview;131};132133// p_receiver_func callback has signature (String p_path, Ref<Texture2D> p_preview, Ref<Texture2D> p_preview_small, Variant p_userdata)134// p_preview will be null if there was an error135void queue_resource_preview(const String &p_path, Object *p_receiver, const StringName &p_receiver_func, const Variant &p_userdata);136void queue_edited_resource_preview(const Ref<Resource> &p_res, Object *p_receiver, const StringName &p_receiver_func, const Variant &p_userdata);137const Dictionary get_preview_metadata(const String &p_path) const;138139PreviewItem get_resource_preview_if_available(const String &p_path);140141void add_preview_generator(const Ref<EditorResourcePreviewGenerator> &p_generator);142void remove_preview_generator(const Ref<EditorResourcePreviewGenerator> &p_generator);143void check_for_invalidation(const String &p_path);144145void start();146void stop();147bool is_threaded() const;148149EditorResourcePreview();150~EditorResourcePreview();151};152153154