| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485 |
- // Copyright (C) 2009-present, Panagiotis Christopoulos Charitos and contributors.
- // All rights reserved.
- // Code licensed under the BSD License.
- // http://www.anki3d.org/LICENSE
- #pragma once
- #include <AnKi/Gr/BackendCommon/Common.h>
- #include <AnKi/Util/DynamicArray.h>
- namespace anki {
- /// @addtogroup graphics
- /// @{
- /// Helper class for MicroXXX objects. It expects a specific interface for the T.
- template<typename T>
- class MicroObjectRecycler
- {
- public:
- MicroObjectRecycler()
- {
- }
- ~MicroObjectRecycler()
- {
- destroy();
- }
- /// It's thread-safe.
- void destroy();
- /// Find a new one to reuse. It's thread-safe.
- T* findToReuse();
- /// Release an object back to the recycler. It's thread-safe.
- void recycle(T* s);
- /// Destroy those objects that their fence is done. It's thread-safe.
- void trimCache()
- {
- LockGuard<Mutex> lock(m_mtx);
- checkDoneFences();
- trimCacheInternal(0);
- }
- U32 getCacheSize() const
- {
- return m_objects.getSize();
- }
- private:
- class Object
- {
- public:
- T* m_microObject;
- Bool m_fenceDone;
- };
- GrDynamicArray<Object> m_objects;
- Mutex m_mtx;
- // Begin trim cache adjustment vars
- U32 m_readyObjectsAfterTrim = 1;
- static constexpr U32 m_maxRequestsPerAdjustment = 128;
- U32 m_cacheMisses = 0;
- U32 m_requests = 0;
- U32 m_minCacheSizePerRequest = kMaxU32;
- // End trim cache adjustment vars
- #if ANKI_EXTRA_CHECKS
- U32 m_createdAndNotRecycled = 0;
- #endif
- void trimCacheInternal(U32 aliveObjectCountAfterTrim);
- void adjustAliveObjectCount();
- void checkDoneFences();
- };
- /// @}
- } // end namespace anki
- #include <AnKi/Gr/BackendCommon/MicroObjectRecycler.inl.h>
|