diff options
Diffstat (limited to 'core')
-rw-r--r-- | core/bind/core_bind.cpp | 6 | ||||
-rw-r--r-- | core/bind/core_bind.h | 6 | ||||
-rw-r--r-- | core/image.cpp | 3 | ||||
-rw-r--r-- | core/image.h | 6 | ||||
-rw-r--r-- | core/io/resource_loader.cpp | 126 | ||||
-rw-r--r-- | core/io/resource_loader.h | 12 | ||||
-rw-r--r-- | core/math/a_star.cpp | 4 | ||||
-rw-r--r-- | core/os/os.cpp | 8 | ||||
-rw-r--r-- | core/os/os.h | 8 | ||||
-rw-r--r-- | core/register_core_types.cpp | 3 |
10 files changed, 153 insertions, 29 deletions
diff --git a/core/bind/core_bind.cpp b/core/bind/core_bind.cpp index e81468e888..f17d7372e2 100644 --- a/core/bind/core_bind.cpp +++ b/core/bind/core_bind.cpp @@ -592,17 +592,17 @@ struct Time { }; */ -int _OS::get_static_memory_usage() const { +uint64_t _OS::get_static_memory_usage() const { return OS::get_singleton()->get_static_memory_usage(); } -int _OS::get_static_memory_peak_usage() const { +uint64_t _OS::get_static_memory_peak_usage() const { return OS::get_singleton()->get_static_memory_peak_usage(); } -int _OS::get_dynamic_memory_usage() const { +uint64_t _OS::get_dynamic_memory_usage() const { return OS::get_singleton()->get_dynamic_memory_usage(); } diff --git a/core/bind/core_bind.h b/core/bind/core_bind.h index a4b4629037..1c8b985d73 100644 --- a/core/bind/core_bind.h +++ b/core/bind/core_bind.h @@ -283,9 +283,9 @@ public: uint64_t get_system_time_secs() const; uint64_t get_system_time_msecs() const; - int get_static_memory_usage() const; - int get_static_memory_peak_usage() const; - int get_dynamic_memory_usage() const; + uint64_t get_static_memory_usage() const; + uint64_t get_static_memory_peak_usage() const; + uint64_t get_dynamic_memory_usage() const; void delay_usec(uint32_t p_usec) const; void delay_msec(uint32_t p_msec) const; diff --git a/core/image.cpp b/core/image.cpp index 4fc3b4becb..91c3d05a29 100644 --- a/core/image.cpp +++ b/core/image.cpp @@ -2632,6 +2632,9 @@ void Image::_bind_methods() { ADD_PROPERTY(PropertyInfo(Variant::DICTIONARY, "data", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE), "_set_data", "_get_data"); + BIND_CONSTANT(MAX_WIDTH); + BIND_CONSTANT(MAX_HEIGHT); + BIND_ENUM_CONSTANT(FORMAT_L8); //luminance BIND_ENUM_CONSTANT(FORMAT_LA8); //luminance-alpha BIND_ENUM_CONSTANT(FORMAT_R8); diff --git a/core/image.h b/core/image.h index 3604580e98..b23f8cac46 100644 --- a/core/image.h +++ b/core/image.h @@ -52,14 +52,14 @@ typedef Ref<Image> (*ImageMemLoadFunc)(const uint8_t *p_png, int p_size); class Image : public Resource { GDCLASS(Image, Resource); +public: + static SavePNGFunc save_png_func; + enum { MAX_WIDTH = 16384, // force a limit somehow MAX_HEIGHT = 16384 // force a limit somehow }; -public: - static SavePNGFunc save_png_func; - enum Format { FORMAT_L8, //luminance diff --git a/core/io/resource_loader.cpp b/core/io/resource_loader.cpp index b8bc24c1de..02e4d8dc7a 100644 --- a/core/io/resource_loader.cpp +++ b/core/io/resource_loader.cpp @@ -53,6 +53,12 @@ Error ResourceInteractiveLoader::wait() { return err; } +ResourceInteractiveLoader::~ResourceInteractiveLoader() { + if (path_loading != String()) { + ResourceLoader::_remove_from_loading_map(path_loading); + } +} + bool ResourceFormatLoader::recognize_path(const String &p_path, const String &p_for_type) const { String extension = p_path.get_extension(); @@ -280,6 +286,39 @@ RES ResourceLoader::_load(const String &p_path, const String &p_original_path, c return RES(); } +bool ResourceLoader::_add_to_loading_map(const String &p_path) { + + bool success; + if (loading_map_mutex) { + loading_map_mutex->lock(); + } + + if (loading_map.has(p_path)) { + success = false; + } else { + loading_map[p_path] = true; + success = true; + } + + if (loading_map_mutex) { + loading_map_mutex->unlock(); + } + + return success; +} + +void ResourceLoader::_remove_from_loading_map(const String &p_path) { + if (loading_map_mutex) { + loading_map_mutex->lock(); + } + + loading_map.erase(p_path); + + if (loading_map_mutex) { + loading_map_mutex->unlock(); + } +} + RES ResourceLoader::load(const String &p_path, const String &p_type_hint, bool p_no_cache, Error *r_error) { if (r_error) @@ -292,6 +331,15 @@ RES ResourceLoader::load(const String &p_path, const String &p_type_hint, bool p local_path = ProjectSettings::get_singleton()->localize_path(p_path); if (!p_no_cache) { + + { + bool success = _add_to_loading_map(local_path); + if (!success) { + ERR_EXPLAIN("Resource: '" + local_path + "' is already being loaded. Cyclic reference?"); + ERR_FAIL_V(RES()); + } + } + //lock first if possible if (ResourceCache::lock) { ResourceCache::lock->read_lock(); @@ -310,7 +358,7 @@ RES ResourceLoader::load(const String &p_path, const String &p_type_hint, bool p if (ResourceCache::lock) { ResourceCache::lock->read_unlock(); } - print_verbose("Loading resource: " + local_path + " (cached)"); + _remove_from_loading_map(local_path); return res; } } @@ -322,12 +370,21 @@ RES ResourceLoader::load(const String &p_path, const String &p_type_hint, bool p bool xl_remapped = false; String path = _path_remap(local_path, &xl_remapped); - ERR_FAIL_COND_V(path == "", RES()); + if (path == "") { + if (!p_no_cache) { + _remove_from_loading_map(local_path); + } + ERR_EXPLAIN("Remapping '" + local_path + "'failed."); + ERR_FAIL_V(RES()); + } print_verbose("Loading resource: " + path); RES res = _load(path, local_path, p_type_hint, p_no_cache, r_error); if (res.is_null()) { + if (!p_no_cache) { + _remove_from_loading_map(local_path); + } return RES(); } if (!p_no_cache) @@ -346,6 +403,10 @@ RES ResourceLoader::load(const String &p_path, const String &p_type_hint, bool p } #endif + if (!p_no_cache) { + _remove_from_loading_map(local_path); + } + if (_loaded_callback) { _loaded_callback(res, p_path); } @@ -394,19 +455,36 @@ Ref<ResourceInteractiveLoader> ResourceLoader::load_interactive(const String &p_ else local_path = ProjectSettings::get_singleton()->localize_path(p_path); - if (!p_no_cache && ResourceCache::has(local_path)) { + if (!p_no_cache) { - print_verbose("Loading resource: " + local_path + " (cached)"); - Ref<Resource> res_cached = ResourceCache::get(local_path); - Ref<ResourceInteractiveLoaderDefault> ril = Ref<ResourceInteractiveLoaderDefault>(memnew(ResourceInteractiveLoaderDefault)); + bool success = _add_to_loading_map(local_path); + if (!success) { + ERR_EXPLAIN("Resource: '" + local_path + "' is already being loaded. Cyclic reference?"); + ERR_FAIL_V(RES()); + } - ril->resource = res_cached; - return ril; + if (ResourceCache::has(local_path)) { + + print_verbose("Loading resource: " + local_path + " (cached)"); + Ref<Resource> res_cached = ResourceCache::get(local_path); + Ref<ResourceInteractiveLoaderDefault> ril = Ref<ResourceInteractiveLoaderDefault>(memnew(ResourceInteractiveLoaderDefault)); + + ril->resource = res_cached; + ril->path_loading = local_path; + return ril; + } } bool xl_remapped = false; String path = _path_remap(local_path, &xl_remapped); - ERR_FAIL_COND_V(path == "", Ref<ResourceInteractiveLoader>()); + if (path == "") { + if (!p_no_cache) { + _remove_from_loading_map(local_path); + } + ERR_EXPLAIN("Remapping '" + local_path + "'failed."); + ERR_FAIL_V(RES()); + } + print_verbose("Loading resource: " + path); bool found = false; @@ -418,14 +496,21 @@ Ref<ResourceInteractiveLoader> ResourceLoader::load_interactive(const String &p_ Ref<ResourceInteractiveLoader> ril = loader[i]->load_interactive(path, local_path, r_error); if (ril.is_null()) continue; - if (!p_no_cache) + if (!p_no_cache) { ril->set_local_path(local_path); + ril->path_loading = local_path; + } + if (xl_remapped) ril->set_translation_remapped(true); return ril; } + if (!p_no_cache) { + _remove_from_loading_map(local_path); + } + if (found) { ERR_EXPLAIN("Failed loading resource: " + path); } else { @@ -833,6 +918,27 @@ void ResourceLoader::remove_custom_loaders() { } } +Mutex *ResourceLoader::loading_map_mutex = NULL; +HashMap<String, int> ResourceLoader::loading_map; + +void ResourceLoader::initialize() { +#ifndef NO_THREADS + loading_map_mutex = Mutex::create(); +#endif +} + +void ResourceLoader::finalize() { +#ifndef NO_THREADS + const String *K = NULL; + while ((K = loading_map.next(K))) { + ERR_PRINTS("Exited while resource is being loaded: " + *K); + } + loading_map.clear(); + memdelete(loading_map_mutex); + loading_map_mutex = NULL; +#endif +} + ResourceLoadErrorNotify ResourceLoader::err_notify = NULL; void *ResourceLoader::err_notify_ud = NULL; diff --git a/core/io/resource_loader.h b/core/io/resource_loader.h index 6c0e9d5554..622b74a998 100644 --- a/core/io/resource_loader.h +++ b/core/io/resource_loader.h @@ -40,6 +40,8 @@ class ResourceInteractiveLoader : public Reference { GDCLASS(ResourceInteractiveLoader, Reference); + friend class ResourceLoader; + String path_loading; protected: static void _bind_methods(); @@ -54,6 +56,7 @@ public: virtual Error wait(); ResourceInteractiveLoader() {} + ~ResourceInteractiveLoader(); }; class ResourceFormatLoader : public Reference { @@ -110,12 +113,18 @@ class ResourceLoader { static SelfList<Resource>::List remapped_list; friend class ResourceFormatImporter; + friend class ResourceInteractiveLoader; //internal load function static RES _load(const String &p_path, const String &p_original_path, const String &p_type_hint, bool p_no_cache, Error *r_error); static ResourceLoadedCallback _loaded_callback; static Ref<ResourceFormatLoader> _find_custom_resource_format_loader(String path); + static Mutex *loading_map_mutex; + static HashMap<String, int> loading_map; + + static bool _add_to_loading_map(const String &p_path); + static void _remove_from_loading_map(const String &p_path); public: static Ref<ResourceInteractiveLoader> load_interactive(const String &p_path, const String &p_type_hint = "", bool p_no_cache = false, Error *r_error = NULL); @@ -170,6 +179,9 @@ public: static void remove_custom_resource_format_loader(String script_path); static void add_custom_loaders(); static void remove_custom_loaders(); + + static void initialize(); + static void finalize(); }; #endif diff --git a/core/math/a_star.cpp b/core/math/a_star.cpp index a0556ae839..b885a06834 100644 --- a/core/math/a_star.cpp +++ b/core/math/a_star.cpp @@ -260,8 +260,8 @@ bool AStar::_solve(Point *begin_point, Point *end_point) { } // Check open list - SelfList<Point> *least_cost_point = NULL; - real_t least_cost = 1e30; + SelfList<Point> *least_cost_point = open_list.first(); + real_t least_cost = Math_INF; // TODO: Cache previous results for (SelfList<Point> *E = open_list.first(); E; E = E->next()) { diff --git a/core/os/os.cpp b/core/os/os.cpp index 1f967030e7..d2d39d253a 100644 --- a/core/os/os.cpp +++ b/core/os/os.cpp @@ -393,16 +393,16 @@ Error OS::dialog_input_text(String p_title, String p_description, String p_parti return OK; }; -int OS::get_static_memory_usage() const { +uint64_t OS::get_static_memory_usage() const { return Memory::get_mem_usage(); } -int OS::get_dynamic_memory_usage() const { +uint64_t OS::get_dynamic_memory_usage() const { return MemoryPool::total_memory; } -int OS::get_static_memory_peak_usage() const { +uint64_t OS::get_static_memory_peak_usage() const { return Memory::get_mem_max_usage(); } @@ -418,7 +418,7 @@ bool OS::has_touchscreen_ui_hint() const { return Input::get_singleton() && Input::get_singleton()->is_emulating_touch_from_mouse(); } -int OS::get_free_static_memory() const { +uint64_t OS::get_free_static_memory() const { return Memory::get_mem_available(); } diff --git a/core/os/os.h b/core/os/os.h index 9f3713e164..20a3494e11 100644 --- a/core/os/os.h +++ b/core/os/os.h @@ -383,10 +383,10 @@ public: virtual void print_resources_in_use(bool p_short = false); virtual void print_all_resources(String p_to_file = ""); - virtual int get_static_memory_usage() const; - virtual int get_static_memory_peak_usage() const; - virtual int get_dynamic_memory_usage() const; - virtual int get_free_static_memory() const; + virtual uint64_t get_static_memory_usage() const; + virtual uint64_t get_static_memory_peak_usage() const; + virtual uint64_t get_dynamic_memory_usage() const; + virtual uint64_t get_free_static_memory() const; RenderThreadMode get_render_thread_mode() const { return _render_thread_mode; } diff --git a/core/register_core_types.cpp b/core/register_core_types.cpp index a31aa9cb01..60c2778603 100644 --- a/core/register_core_types.cpp +++ b/core/register_core_types.cpp @@ -99,6 +99,7 @@ void register_core_types() { _global_mutex = Mutex::create(); StringName::setup(); + ResourceLoader::initialize(); register_global_constants(); register_variant_methods(); @@ -269,6 +270,8 @@ void unregister_core_types() { if (ip) memdelete(ip); + ResourceLoader::finalize(); + ObjectDB::cleanup(); unregister_variant_methods(); |