151b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)/*
251b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles) * Copyright (C) 2013 Google Inc. All rights reserved.
351b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles) *
451b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles) * Redistribution and use in source and binary forms, with or without
551b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles) * modification, are permitted provided that the following conditions are
651b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles) * met:
751b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles) *
851b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles) *     * Redistributions of source code must retain the above copyright
951b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles) * notice, this list of conditions and the following disclaimer.
1051b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles) *     * Redistributions in binary form must reproduce the above
1151b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles) * copyright notice, this list of conditions and the following disclaimer
1251b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles) * in the documentation and/or other materials provided with the
1351b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles) * distribution.
1451b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles) *     * Neither the name of Google Inc. nor the names of its
1551b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles) * contributors may be used to endorse or promote products derived from
1651b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles) * this software without specific prior written permission.
1751b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles) *
1851b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles) * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
1951b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles) * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
2051b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles) * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
2151b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles) * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
2251b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles) * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2351b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles) * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
2451b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles) * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2551b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles) * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2651b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles) * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2751b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles) * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2851b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles) * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2951b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles) */
3051b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)
3151b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)#ifndef FontDataCache_h
3251b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)#define FontDataCache_h
3351b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)
34a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)#include "platform/fonts/FontCache.h"
35a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)#include "platform/fonts/FontPlatformData.h"
3651b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)#include "wtf/HashMap.h"
3751b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)#include "wtf/ListHashSet.h"
3851b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)
39c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)namespace blink {
4051b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)
4151b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)class SimpleFontData;
4251b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)
4351b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)struct FontDataCacheKeyHash {
4451b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)    static unsigned hash(const FontPlatformData& platformData)
4551b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)    {
4651b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)        return platformData.hash();
4751b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)    }
4851b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)
4951b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)    static bool equal(const FontPlatformData& a, const FontPlatformData& b)
5051b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)    {
5151b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)        return a == b;
5251b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)    }
5351b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)
5451b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)    static const bool safeToCompareToEmptyOrDeleted = true;
5551b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)};
5651b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)
5751b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)struct FontDataCacheKeyTraits : WTF::GenericHashTraits<FontPlatformData> {
5851b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)    static const bool emptyValueIsZero = true;
5951b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)    static const bool needsDestruction = true;
6051b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)    static const FontPlatformData& emptyValue()
6151b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)    {
6251b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)        DEFINE_STATIC_LOCAL(FontPlatformData, key, (0.f, false, false));
6351b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)        return key;
6451b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)    }
657242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci    static void constructDeletedValue(FontPlatformData& slot, bool)
6651b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)    {
6751b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)        new (NotNull, &slot) FontPlatformData(WTF::HashTableDeletedValue);
6851b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)    }
6951b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)    static bool isDeletedValue(const FontPlatformData& value)
7051b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)    {
7151b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)        return value.isHashTableDeletedValue();
7251b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)    }
7351b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)};
7451b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)
7551b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)class FontDataCache {
7651b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)public:
7751b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)    PassRefPtr<SimpleFontData> get(const FontPlatformData*, ShouldRetain = Retain);
7851b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)    bool contains(const FontPlatformData*) const;
7951b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)    void release(const SimpleFontData*);
8051b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)
8151b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)    // This is used by FontVerticalDataCache to mark all items with vertical data
8251b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)    // that are currently in cache as "in cache", which is later used to sweep the FontVerticalDataCache.
8351b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)    void markAllVerticalData();
8451b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)
8551b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)    // Purges items in FontDataCache according to provided severity.
8651b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)    // Returns true if any removal of cache items actually occurred.
8751b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)    bool purge(PurgeSeverity);
8851b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)
8951b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)private:
9051b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)    bool purgeLeastRecentlyUsed(int count);
9151b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)
9251b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)    typedef HashMap<FontPlatformData, pair<RefPtr<SimpleFontData>, unsigned>, FontDataCacheKeyHash, FontDataCacheKeyTraits> Cache;
9351b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)    Cache m_cache;
9451b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)    ListHashSet<RefPtr<SimpleFontData> > m_inactiveFontData;
9551b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)};
9651b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)
97e38fbeeb576b5094e34e038ab88d9d6a5c5c2214Torne (Richard Coles)} // namespace blink
9851b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)
9951b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)#endif
100