#pragma once #include "opengl/storage.hpp" #include "opengl/program_manager.hpp" #include "glyph_cache.hpp" #include "data_formats.hpp" #include "defines.hpp" //#include "../base/mutex.hpp" #include "../base/resource_pool.hpp" #include "../std/shared_ptr.hpp" #include "../std/map.hpp" #include "../std/string.hpp" #include "../std/vector.hpp" namespace graphics { class ResourceCache; namespace gl { class BaseTexture; class Storage; class ProgramManager; } struct GlyphInfo; struct TTextureFactory : BasePoolElemFactory { size_t m_w; size_t m_h; graphics::DataFormat m_format; TTextureFactory(size_t w, size_t h, graphics::DataFormat format, char const * resName, size_t batchSize); shared_ptr const Create(); }; struct TStorageFactory : BasePoolElemFactory { size_t m_vbSize; size_t m_ibSize; bool m_useSingleThreadedOGL; TStorageFactory(size_t vbSize, size_t ibSize, bool useSingleThreadedOGL, char const * resName, size_t batchSize); gl::Storage const Create(); void BeforeMerge(gl::Storage const & e); }; /// ---- Texture Pools ---- /// Basic texture pool traits typedef BasePoolTraits, TTextureFactory> TBaseTexturePoolTraits; /// Fixed-Size texture pool typedef FixedSizePoolTraits TFixedSizeTexturePoolTraits; typedef ResourcePoolImpl TFixedSizeTexturePoolImpl; /// On-Demand multi-threaded texture pool typedef AllocateOnDemandMultiThreadedPoolTraits TOnDemandMultiThreadedTexturePoolTraits; typedef ResourcePoolImpl TOnDemandMultiThreadedTexturePoolImpl; /// On-Demand single-threaded texture pool /// (with postponed resource allocation) typedef AllocateOnDemandSingleThreadedPoolTraits TOnDemandSingleThreadedTexturePoolTraits; typedef ResourcePoolImpl TOnDemandSingleThreadedTexturePoolImpl; /// Interface for texture pool typedef ResourcePool > TTexturePool; /// ---- Storage Pools ---- /// Basic storage traits typedef BasePoolTraits TBaseStoragePoolTraits; /// Fixed-Size mergeable storage pool typedef SeparateFreePoolTraits TSeparateFreeStoragePoolTraits; typedef FixedSizePoolTraits TFixedSizeMergeableStoragePoolTraits; typedef ResourcePoolImpl TFixedSizeMergeableStoragePoolImpl; /// Fixed-Size non-mergeable storage pool typedef FixedSizePoolTraits TFixedSizeNonMergeableStoragePoolTraits; typedef ResourcePoolImpl TFixedSizeNonMergeableStoragePoolImpl; /// On-Demand single-threaded storage pool /// (with postponed resource allocation and separate list of freed resources) typedef AllocateOnDemandSingleThreadedPoolTraits TOnDemandSingleThreadedStoragePoolTraits; typedef ResourcePoolImpl TOnDemandSingleThreadedStoragePoolImpl; /// On-Demand multi-threaded storage pool typedef AllocateOnDemandMultiThreadedPoolTraits TOnDemandMultiThreadedStoragePoolTraits; typedef ResourcePoolImpl TOnDemandMultiThreadedStoragePoolImpl; /// Interface for storage pool typedef ResourcePool TStoragePool; class ResourceManager { public: struct StoragePoolParams { size_t m_vbSize; size_t m_vertexSize; size_t m_ibSize; size_t m_indexSize; size_t m_storagesCount; EStorageType m_storageType; bool m_isDebugging; StoragePoolParams(); StoragePoolParams(EStorageType storageType); StoragePoolParams(size_t vbSize, size_t vertexSize, size_t ibSize, size_t indexSize, size_t storagesCount, EStorageType storageType, bool isDebugging); bool isValid() const; }; struct TexturePoolParams { size_t m_texWidth; size_t m_texHeight; size_t m_texCount; graphics::DataFormat m_format; ETextureType m_textureType; bool m_isDebugging; TexturePoolParams(); TexturePoolParams(ETextureType textureType); TexturePoolParams(size_t texWidth, size_t texHeight, size_t texCount, graphics::DataFormat format, ETextureType textureType, bool isDebugging); bool isValid() const; }; struct GlyphCacheParams { string m_unicodeBlockFile; string m_whiteListFile; string m_blackListFile; size_t m_glyphCacheMemoryLimit; EDensity m_density; GlyphCacheParams(); GlyphCacheParams(string const & unicodeBlockFile, string const & whiteListFile, string const & blackListFile, size_t glyphCacheMemoryLimit, EDensity density); }; struct Params { private: string m_vendorName; string m_rendererName; string m_versionName; /// check non-strict matching upon vendorName and rendererName bool isGPU(char const * vendorName, char const * rendererName, bool strictMatch) const; bool isGPUVersion(char const * vendorName, char const * rendererName, char const * version); public: DataFormat m_rtFormat; DataFormat m_texFormat; DataFormat m_texRtFormat; bool m_useSingleThreadedOGL; bool m_useReadPixelsToSynchronize; size_t m_videoMemoryLimit; /// storages params vector m_storageParams; /// textures params vector m_textureParams; /// glyph caches params GlyphCacheParams m_glyphCacheParams; unsigned m_renderThreadsCount; unsigned m_threadSlotsCount; Params(); void distributeFreeMemory(int freeVideoMemory); void checkDeviceCaps(); int memoryUsage() const; int fixedMemoryUsage() const; void initScaleWeights(); }; private: typedef map > TStaticTextures; TStaticTextures m_staticTextures; string m_skinBuffer; void loadSkinInfoAndTexture(string const & skinFileName, EDensity density); //threads::Mutex m_mutex; vector > m_texturePools; vector > m_storagePools; struct ThreadSlot { shared_ptr m_programManager; shared_ptr m_glyphCache; }; vector m_threadSlots; Params m_params; public: ResourceManager(Params const & p, string const & skinFileName, EDensity density); /// Use like static function with shared_ptr instead of this. /// @todo Check if we can remove shared_ptrs from this logic. static void loadSkin(shared_ptr const & rm, vector > & caches); void initThreadSlots(Params const & p); void initStoragePool(StoragePoolParams const & p, shared_ptr & pool); TStoragePool * storagePool(EStorageType type); void initTexturePool(TexturePoolParams const & p, shared_ptr & pool); TTexturePool * texturePool(ETextureType type); shared_ptr const & getTexture(string const & name); Params const & params() const; GlyphCache * glyphCache(int threadSlot = 0); int renderThreadSlot(int threadNum) const; int guiThreadSlot() const; int cacheThreadSlot() const; void addFonts(vector const & fontNames); /* void memoryWarning(); void enterBackground(); void enterForeground(); */ void updatePoolState(); void cancel(); bool useReadPixelsToSynchronize() const; shared_ptr createRenderTarget(unsigned w, unsigned h); gl::ProgramManager * programManager(int threadSlot); }; }