Allow more buffer formats in the IOSurface pool
[WebKit-https.git] / Source / WebCore / platform / graphics / cg / IOSurfacePool.h
1 /*
2  * Copyright (C) 2013, 2014 Apple Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23  * THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 #ifndef IOSurfacePool_h
27 #define IOSurfacePool_h
28
29 #include "ColorSpace.h"
30 #include "IOSurface.h"
31 #include "IntSize.h"
32 #include "IntSizeHash.h"
33 #include "Timer.h"
34 #include <wtf/Deque.h>
35 #include <wtf/HashMap.h>
36 #include <wtf/NeverDestroyed.h>
37 #include <wtf/RetainPtr.h>
38
39 #if USE(IOSURFACE)
40
41 namespace WebCore {
42
43 class IOSurfacePool {
44     WTF_MAKE_NONCOPYABLE(IOSurfacePool);
45     WTF_MAKE_FAST_ALLOCATED;
46     friend class NeverDestroyed<IOSurfacePool>;
47
48 public:
49     WEBCORE_EXPORT static IOSurfacePool& sharedPool();
50
51     std::unique_ptr<IOSurface> takeSurface(IntSize, ColorSpace, IOSurface::Format);
52     WEBCORE_EXPORT void addSurface(std::unique_ptr<IOSurface>);
53
54     void discardAllSurfaces();
55
56     WEBCORE_EXPORT void setPoolSize(size_t);
57
58     void showPoolStatistics();
59
60 private:
61     IOSurfacePool();
62
63     struct CachedSurfaceDetails {
64         CachedSurfaceDetails()
65             : hasMarkedPurgeable(false)
66         { }
67
68         void resetLastUseTime() { lastUseTime = std::chrono::steady_clock::now(); }
69
70         std::chrono::steady_clock::time_point lastUseTime;
71         bool hasMarkedPurgeable;
72     };
73
74     typedef Deque<std::unique_ptr<IOSurface>> CachedSurfaceQueue;
75     typedef HashMap<IntSize, CachedSurfaceQueue> CachedSurfaceMap;
76     typedef HashMap<IOSurface*, CachedSurfaceDetails> CachedSurfaceDetailsMap;
77     
78     bool shouldCacheSurface(const IOSurface&) const;
79
80     void willAddSurface(IOSurface&, bool inUse);
81     void didRemoveSurface(IOSurface&, bool inUse);
82     void didUseSurfaceOfSize(IntSize);
83
84     void insertSurfaceIntoPool(std::unique_ptr<IOSurface>);
85
86     void evict(size_t additionalSize);
87     void tryEvictInUseSurface();
88     void tryEvictOldestCachedSurface();
89
90     void scheduleCollectionTimer();
91     void collectionTimerFired();
92     void collectInUseSurfaces();
93     bool markOlderSurfacesPurgeable();
94
95     void platformGarbageCollectNow();
96
97     Timer m_collectionTimer;
98     CachedSurfaceMap m_cachedSurfaces;
99     CachedSurfaceQueue m_inUseSurfaces;
100     CachedSurfaceDetailsMap m_surfaceDetails;
101     Vector<IntSize> m_sizesInPruneOrder;
102
103     size_t m_bytesCached;
104     size_t m_inUseBytesCached;
105     size_t m_maximumBytesCached;
106 };
107
108 }
109 #endif // USE(IOSURFACE)
110
111 #endif // IOSurfacePool_h