#ifndef IOSurfacePool_h
#define IOSurfacePool_h
#include "ColorSpace.h"
#include "IntSize.h"
#include "IntSizeHash.h"
#include "Timer.h"
#include <wtf/Deque.h>
#include <wtf/HashMap.h>
#include <wtf/NeverDestroyed.h>
#include <wtf/RetainPtr.h>
#if USE(IOSURFACE)
namespace WebCore {
class IOSurface;
class IOSurfacePool {
WTF_MAKE_NONCOPYABLE(IOSurfacePool);
WTF_MAKE_FAST_ALLOCATED;
friend class NeverDestroyed<IOSurfacePool>;
public:
static IOSurfacePool& sharedPool();
PassRefPtr<IOSurface> takeSurface(IntSize size, ColorSpace colorSpace);
void addSurface(IOSurface*);
void discardAllSurfaces();
void setPoolSize(size_t);
void showPoolStatistics();
private:
IOSurfacePool();
struct CachedSurfaceDetails {
CachedSurfaceDetails()
: hasMarkedPurgeable(false)
{ }
void resetLastUseTime() { lastUseTime = std::chrono::steady_clock::now(); }
std::chrono::steady_clock::time_point lastUseTime;
bool hasMarkedPurgeable;
};
typedef Deque<RefPtr<IOSurface>> CachedSurfaceQueue;
typedef HashMap<IntSize, CachedSurfaceQueue> CachedSurfaceMap;
typedef HashMap<IOSurface*, CachedSurfaceDetails> CachedSurfaceDetailsMap;
void willAddSurface(IOSurface*, bool inUse);
void didRemoveSurface(IOSurface*, bool inUse);
void didUseSurfaceOfSize(IntSize);
void insertSurfaceIntoPool(IOSurface*);
void evict(size_t additionalSize);
void tryEvictInUseSurface();
void tryEvictOldestCachedSurface();
void scheduleCollectionTimer();
void collectionTimerFired(Timer<IOSurfacePool>&);
void collectInUseSurfaces();
bool markOlderSurfacesPurgeable();
void platformGarbageCollectNow();
Timer<IOSurfacePool> m_collectionTimer;
CachedSurfaceMap m_cachedSurfaces;
CachedSurfaceQueue m_inUseSurfaces;
CachedSurfaceDetailsMap m_surfaceDetails;
Vector<IntSize> m_sizesInPruneOrder;
size_t m_bytesCached;
size_t m_inUseBytesCached;
size_t m_maximumBytesCached;
};
}
#endif // USE(IOSURFACE)
#endif // IOSurfacePool_h