#ifndef MAYA_API_MTextureCache #define MAYA_API_MTextureCache //- // ========================================================================== // Copyright (C) 1995 - 2005 Alias Systems Corp. and/or its licensors. All // rights reserved. // // The coded instructions, statements, computer programs, and/or related // material (collectively the "Data") in these files are provided by Alias // Systems Corp. ("Alias") and/or its licensors for the exclusive use of the // Customer (as defined in the Alias Software License Agreement that // accompanies this Alias software). Such Customer has the right to use, // modify, and incorporate the Data into other products and to distribute such // products for use by end-users. // // THE DATA IS PROVIDED "AS IS". ALIAS HEREBY DISCLAIMS ALL WARRANTIES // RELATING TO THE DATA, INCLUDING, WITHOUT LIMITATION, ANY AND ALL EXPRESS OR // IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND/OR FITNESS FOR A // PARTICULAR PURPOSE. IN NO EVENT SHALL ALIAS BE LIABLE FOR ANY DAMAGES // WHATSOEVER, WHETHER DIRECT, INDIRECT, SPECIAL, OR PUNITIVE, WHETHER IN AN // ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, OR IN EQUITY, // ARISING OUT OF ACCESS TO, USE OF, OR RELIANCE UPON THE DATA. // ========================================================================== //+ // MTextureCache.h /////////////////////////////////////////////////////////////////// // DESCRIPTION: Texture cache, used to temporarily store textures. // Eventually, this class will likely end up in the // Maya API. // // This class is not currently thread-safe. // // AUTHOR: Christian Laforte // /////////////////////////////////////////////////////////////////// #ifdef WIN32 #pragma warning( disable : 4786 ) // Disable stupid STL warnings. #endif #include #include #include #include #include "MTexture.h" #include "NodeMonitor.h" class MTextureCache; class MTextureCacheElement { friend class MTextureCache; public: MTextureCacheElement() { lastAccessedTimestamp = -1; m_texture = NULL; } ~MTextureCacheElement(); MTexture* texture() { return m_texture; } private: MTexture* m_texture; unsigned int lastAccessedTimestamp; // can be used to track when the texture was last used. NodeMonitor fMonitor; }; // This class implements a singleton node with reference counting. // The refcount starts with a value equal to 0. Everytime instance() // gets called, the refcount is incremented by one. Everytime // release() gets called, the refcount is decremented by one, // and if following that the refcount value is 0, the texture cache // singleton is destroyed. class MTextureCache : public NodeMonitorManager { protected: MTextureCache() { m_currentTimestamp = 0; } public: ~MTextureCache(); static MTextureCache* instance() { if (!m_instance) { m_instance = new MTextureCache; } refcount++; return m_instance; } static void release() { assert(m_instance); refcount--; if (refcount == 0 && m_instance) { delete m_instance; m_instance = NULL; } } // Return a reference to the texture. There's no reference counting yet. MTexture* texture(MObject textureObj, MTexture::Type type = MTexture::RGBA, bool mipmapped = true, GLenum target = GL_TEXTURE_2D); // Returns true if the texture was found and bound; returns false otherwise. bool bind(MObject textureObj, MTexture::Type type = MTexture::RGBA, bool mipmapped = true, GLenum target = GL_TEXTURE_2D); void incrementTimestamp(unsigned int increment=1); // Called by a node monitor when the watched node is renamed. void onNodeRenamed(MObject& node, MString oldName, MString newName); private: static int refcount; std::map m_textureTable; typedef std::map string_to_cacheElement_map; unsigned int m_currentTimestamp; static MTextureCache* m_instance; }; #endif // MAYA_API_MTextureCache