Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / Source / platform / fonts / FontDataCache.cpp
blob1c5592ce8f619138c840b3c8e055c4a679d393f0
1 /*
2 * Copyright (C) 2013 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
13 * distribution.
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 #include "config.h"
32 #include "platform/fonts/FontDataCache.h"
34 #include "platform/fonts/SimpleFontData.h"
36 using namespace WTF;
38 namespace blink {
40 #if !OS(ANDROID)
41 const unsigned cMaxInactiveFontData = 250;
42 const unsigned cTargetInactiveFontData = 200;
43 #else
44 const unsigned cMaxInactiveFontData = 225;
45 const unsigned cTargetInactiveFontData = 200;
46 #endif
48 PassRefPtr<SimpleFontData> FontDataCache::get(const FontPlatformData* platformData, ShouldRetain shouldRetain)
50 if (!platformData)
51 return nullptr;
53 // TODO: crbug.com/446376 - This should not happen, but we currently
54 // do not have a reproduction for the crash that an empty typeface()
55 // causes downstream from here.
56 if (!platformData->typeface()) {
57 WTF_LOG_ERROR("Empty typeface() in FontPlatformData when accessing FontDataCache.");
58 return nullptr;
61 Cache::iterator result = m_cache.find(*platformData);
62 if (result == m_cache.end()) {
63 pair<RefPtr<SimpleFontData>, unsigned> newValue(SimpleFontData::create(*platformData), shouldRetain == Retain ? 1 : 0);
64 m_cache.set(*platformData, newValue);
65 if (shouldRetain == DoNotRetain)
66 m_inactiveFontData.add(newValue.first);
67 return newValue.first.release();
70 if (!result.get()->value.second) {
71 ASSERT(m_inactiveFontData.contains(result.get()->value.first));
72 m_inactiveFontData.remove(result.get()->value.first);
75 if (shouldRetain == Retain) {
76 result.get()->value.second++;
77 } else if (!result.get()->value.second) {
78 // If shouldRetain is DoNotRetain and count is 0, we want to remove the fontData from
79 // m_inactiveFontData (above) and re-add here to update LRU position.
80 m_inactiveFontData.add(result.get()->value.first);
83 return result.get()->value.first;
86 bool FontDataCache::contains(const FontPlatformData* fontPlatformData) const
88 return m_cache.contains(*fontPlatformData);
91 void FontDataCache::release(const SimpleFontData* fontData)
93 ASSERT(!fontData->isCustomFont());
95 Cache::iterator it = m_cache.find(fontData->platformData());
96 ASSERT(it != m_cache.end());
97 if (it == m_cache.end())
98 return;
100 ASSERT(it->value.second);
101 if (!--it->value.second)
102 m_inactiveFontData.add(it->value.first);
105 void FontDataCache::markAllVerticalData()
107 Cache::iterator end = m_cache.end();
108 for (Cache::iterator fontData = m_cache.begin(); fontData != end; ++fontData) {
109 OpenTypeVerticalData* verticalData = const_cast<OpenTypeVerticalData*>(fontData->value.first->verticalData());
110 if (verticalData)
111 verticalData->setInFontCache(true);
115 bool FontDataCache::purge(PurgeSeverity PurgeSeverity)
117 if (PurgeSeverity == ForcePurge)
118 return purgeLeastRecentlyUsed(INT_MAX);
120 if (m_inactiveFontData.size() > cMaxInactiveFontData)
121 return purgeLeastRecentlyUsed(m_inactiveFontData.size() - cTargetInactiveFontData);
123 return false;
126 bool FontDataCache::purgeLeastRecentlyUsed(int count)
128 static bool isPurging; // Guard against reentry when e.g. a deleted FontData releases its small caps FontData.
129 if (isPurging)
130 return false;
132 isPurging = true;
134 Vector<RefPtr<SimpleFontData>, 20> fontDataToDelete;
135 ListHashSet<RefPtr<SimpleFontData>>::iterator end = m_inactiveFontData.end();
136 ListHashSet<RefPtr<SimpleFontData>>::iterator it = m_inactiveFontData.begin();
137 for (int i = 0; i < count && it != end; ++it, ++i) {
138 RefPtr<SimpleFontData>& fontData = *it.get();
139 m_cache.remove(fontData->platformData());
140 // We should not delete SimpleFontData here because deletion can modify m_inactiveFontData. See http://trac.webkit.org/changeset/44011
141 fontDataToDelete.append(fontData);
144 if (it == end) {
145 // Removed everything
146 m_inactiveFontData.clear();
147 } else {
148 for (int i = 0; i < count; ++i)
149 m_inactiveFontData.remove(m_inactiveFontData.begin());
152 bool didWork = fontDataToDelete.size();
154 fontDataToDelete.clear();
156 isPurging = false;
158 return didWork;
161 } // namespace blink