1/*
2 * Copyright (C) 2012, 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. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#ifndef SubimageCacheWithTimer_h
27#define SubimageCacheWithTimer_h
28
29#include "FloatRect.h"
30#include "Timer.h"
31
32#include <CoreGraphics/CoreGraphics.h>
33#include <wtf/HashCountedSet.h>
34#include <wtf/HashSet.h>
35#include <wtf/HashTraits.h>
36#include <wtf/RetainPtr.h>
37
38#define CACHE_SUBIMAGES 1
39
40namespace WebCore {
41
42#if CACHE_SUBIMAGES
43
44class SubimageCacheWithTimer {
45    WTF_MAKE_NONCOPYABLE(SubimageCacheWithTimer); WTF_MAKE_FAST_ALLOCATED;
46
47public:
48    struct SubimageCacheEntry {
49        RetainPtr<CGImageRef> image;
50        FloatRect rect;
51        RetainPtr<CGImageRef> subimage;
52    };
53
54    struct SubimageCacheEntryTraits : WTF::GenericHashTraits<SubimageCacheEntry> {
55        typedef HashTraits<RetainPtr<CGImageRef>> ImageTraits;
56
57        static const bool emptyValueIsZero = true;
58
59        static const bool hasIsEmptyValueFunction = true;
60        static bool isEmptyValue(const SubimageCacheEntry& value) { return !value.image; }
61
62        static void constructDeletedValue(SubimageCacheEntry& slot) { ImageTraits::constructDeletedValue(slot.image); }
63        static bool isDeletedValue(const SubimageCacheEntry& value) { return ImageTraits::isDeletedValue(value.image); }
64    };
65
66    struct SubimageCacheHash {
67        static unsigned hash(CGImageRef image, const FloatRect& rect)
68        {
69            return WTF::pairIntHash(PtrHash<CGImageRef>::hash(image),
70                (static_cast<unsigned>(rect.x()) << 16) | static_cast<unsigned>(rect.y()));
71        }
72        static unsigned hash(const SubimageCacheEntry& key)
73        {
74            return hash(key.image.get(), key.rect);
75        }
76        static bool equal(const SubimageCacheEntry& a, const SubimageCacheEntry& b)
77        {
78            return a.image == b.image && a.rect == b.rect;
79        }
80        static const bool safeToCompareToEmptyOrDeleted = true;
81    };
82
83    typedef HashSet<SubimageCacheEntry, SubimageCacheHash, SubimageCacheEntryTraits> SubimageCache;
84
85public:
86    SubimageCacheWithTimer();
87    RetainPtr<CGImageRef> getSubimage(CGImageRef, const FloatRect&);
88    void clearImage(CGImageRef);
89
90private:
91    void invalidateCacheTimerFired();
92
93    HashCountedSet<CGImageRef> m_images;
94    SubimageCache m_cache;
95    DeferrableOneShotTimer m_timer;
96};
97
98SubimageCacheWithTimer& subimageCache();
99
100#endif // CACHE_SUBIMAGES
101
102}
103
104#endif // SubimageCacheWithTimer_h
105