CTTE Timer and DeferrableOneShotTimer
[WebKit-https.git] / Source / WebCore / platform / graphics / cg / ImageBufferBackingStoreCache.h
1 /*
2  * Copyright (C) 2013 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 ImageBufferBackingStoreCache_h
27 #define ImageBufferBackingStoreCache_h
28
29 #include "ImageBuffer.h"
30
31 #include "Timer.h"
32
33 #include <wtf/DoublyLinkedList.h>
34 #include <wtf/HashMap.h>
35
36 #if USE(IOSURFACE_CANVAS_BACKING_STORE)
37
38 namespace WebCore {
39
40 class ImageBufferBackingStoreCache {
41     WTF_MAKE_NONCOPYABLE(ImageBufferBackingStoreCache); WTF_MAKE_FAST_ALLOCATED;
42
43 public:
44     static ImageBufferBackingStoreCache& get();
45     
46     struct IOSurfaceAndContext {
47         IOSurfaceAndContext()
48         {
49         }
50
51         IOSurfaceAndContext(IOSurfaceRef surface, CGContextRef context)
52             : surface(surface)
53             , context(context)
54         {
55         }
56
57         RetainPtr<IOSurfaceRef> surface;
58         RetainPtr<CGContextRef> context;
59     };
60
61     IOSurfaceAndContext getOrAllocate(IntSize, CGColorSpaceRef, bool needExactSize);
62     void deallocate(IOSurfaceRef);
63
64 private:
65     ImageBufferBackingStoreCache();
66
67     struct IOSurfaceAndContextWithCreationParams : public IOSurfaceAndContext, public DoublyLinkedListNode<IOSurfaceAndContextWithCreationParams> {
68         IOSurfaceAndContextWithCreationParams()
69         {
70         }
71
72         IOSurfaceAndContextWithCreationParams(IOSurfaceRef surface, CGContextRef context, CGColorSpaceRef colorSpace)
73             : IOSurfaceAndContext(surface, context)
74             , colorSpace(colorSpace)
75         {
76         }
77
78         IOSurfaceAndContextWithCreationParams* m_prev;
79         IOSurfaceAndContextWithCreationParams* m_next;
80         RetainPtr<CGColorSpaceRef> colorSpace;
81     };
82     typedef HashMap<RetainPtr<IOSurfaceRef>, IOSurfaceAndContextWithCreationParams> ActiveSurfaceMap;
83     typedef std::pair<int, int> CachedSurfaceKey;
84     typedef DoublyLinkedList<IOSurfaceAndContextWithCreationParams> InfoLinkedList;
85     typedef HashMap<CachedSurfaceKey, InfoLinkedList> CachedSurfaceMap;
86
87     static CachedSurfaceKey convertSizeToKey(const IntSize& size)
88     {
89         return std::make_pair(WTF::roundUpToMultipleOf(8, size.width()), WTF::roundUpToMultipleOf(8, size.height()));
90     }
91
92     IOSurfaceAndContextWithCreationParams takeFromCache(CachedSurfaceMap::iterator, IOSurfaceAndContextWithCreationParams*);
93     void insertIntoCache(IOSurfaceAndContextWithCreationParams&&);
94
95     // If we find an acceptable surface, this function removes it from the cache as
96     // well as placing it in the out parameter.
97     bool tryTakeFromCache(const IntSize&, CGColorSpaceRef, bool needExactSize, IOSurfaceAndContextWithCreationParams& outInfo);
98     bool isAcceptableSurface(const IOSurfaceAndContextWithCreationParams&, const IntSize&, CGColorSpaceRef, bool needExactSize) const;
99
100     void timerFired(DeferrableOneShotTimer<ImageBufferBackingStoreCache>&);
101     void schedulePurgeTimer();
102
103     DeferrableOneShotTimer<ImageBufferBackingStoreCache> m_purgeTimer;
104     ActiveSurfaceMap m_activeSurfaces;
105     CachedSurfaceMap m_cachedSurfaces;
106     int m_pixelsCached;
107 };
108
109 }
110 #endif // IOSURFACE_CANVAS_BACKING_STORE
111
112 #endif // ImageBufferBackingStoreCache_h