OLD | NEW |
| (Empty) |
1 /* | |
2 * Copyright (C) 2013 Google 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 are | |
6 * met: | |
7 * | |
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. | |
17 * | |
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. | |
29 */ | |
30 | |
31 #include "config.h" | |
32 #include "core/platform/graphics/FontDataCache.h" | |
33 | |
34 #include "core/platform/graphics/SimpleFontData.h" | |
35 | |
36 using namespace WTF; | |
37 | |
38 namespace WebCore { | |
39 | |
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 | |
47 | |
48 PassRefPtr<SimpleFontData> FontDataCache::get(const FontPlatformData* platformDa
ta, ShouldRetain shouldRetain) | |
49 { | |
50 if (!platformData) | |
51 return 0; | |
52 | |
53 Cache::iterator result = m_cache.find(*platformData); | |
54 if (result == m_cache.end()) { | |
55 pair<RefPtr<SimpleFontData>, unsigned> newValue(SimpleFontData::create(*
platformData), shouldRetain == Retain ? 1 : 0); | |
56 m_cache.set(*platformData, newValue); | |
57 if (shouldRetain == DoNotRetain) | |
58 m_inactiveFontData.add(newValue.first); | |
59 return newValue.first.release(); | |
60 } | |
61 | |
62 if (!result.get()->value.second) { | |
63 ASSERT(m_inactiveFontData.contains(result.get()->value.first)); | |
64 m_inactiveFontData.remove(result.get()->value.first); | |
65 } | |
66 | |
67 if (shouldRetain == Retain) { | |
68 result.get()->value.second++; | |
69 } else if (!result.get()->value.second) { | |
70 // If shouldRetain is DoNotRetain and count is 0, we want to remove the
fontData from | |
71 // m_inactiveFontData (above) and re-add here to update LRU position. | |
72 m_inactiveFontData.add(result.get()->value.first); | |
73 } | |
74 | |
75 return result.get()->value.first; | |
76 } | |
77 | |
78 bool FontDataCache::contains(const FontPlatformData* fontPlatformData) const | |
79 { | |
80 return m_cache.contains(*fontPlatformData); | |
81 } | |
82 | |
83 void FontDataCache::release(const SimpleFontData* fontData) | |
84 { | |
85 ASSERT(!fontData->isCustomFont()); | |
86 | |
87 Cache::iterator it = m_cache.find(fontData->platformData()); | |
88 ASSERT(it != m_cache.end()); | |
89 if (it == m_cache.end()) | |
90 return; | |
91 | |
92 ASSERT(it->value.second); | |
93 if (!--it->value.second) | |
94 m_inactiveFontData.add(it->value.first); | |
95 } | |
96 | |
97 void FontDataCache::markAllVerticalData() | |
98 { | |
99 #if ENABLE(OPENTYPE_VERTICAL) | |
100 Cache::iterator end = m_cache.end(); | |
101 for (Cache::iterator fontData = m_cache.begin(); fontData != end; ++fontData
) { | |
102 OpenTypeVerticalData* verticalData = const_cast<OpenTypeVerticalData*>(f
ontData->value.first->verticalData()); | |
103 if (verticalData) | |
104 verticalData->setInFontCache(true); | |
105 } | |
106 #endif | |
107 } | |
108 | |
109 bool FontDataCache::purge(PurgeSeverity PurgeSeverity) | |
110 { | |
111 if (PurgeSeverity == ForcePurge) | |
112 return purgeLeastRecentlyUsed(INT_MAX); | |
113 | |
114 if (m_inactiveFontData.size() > cMaxInactiveFontData) | |
115 return purgeLeastRecentlyUsed(m_inactiveFontData.size() - cTargetInactiv
eFontData); | |
116 | |
117 return false; | |
118 } | |
119 | |
120 bool FontDataCache::purgeLeastRecentlyUsed(int count) | |
121 { | |
122 static bool isPurging; // Guard against reentry when e.g. a deleted FontData
releases its small caps FontData. | |
123 if (isPurging) | |
124 return false; | |
125 | |
126 isPurging = true; | |
127 | |
128 Vector<RefPtr<SimpleFontData>, 20> fontDataToDelete; | |
129 ListHashSet<RefPtr<SimpleFontData> >::iterator end = m_inactiveFontData.end(
); | |
130 ListHashSet<RefPtr<SimpleFontData> >::iterator it = m_inactiveFontData.begin
(); | |
131 for (int i = 0; i < count && it != end; ++it, ++i) { | |
132 RefPtr<SimpleFontData>& fontData = *it.get(); | |
133 m_cache.remove(fontData->platformData()); | |
134 // We should not delete SimpleFontData here because deletion can modify
m_inactiveFontData. See http://trac.webkit.org/changeset/44011 | |
135 fontDataToDelete.append(fontData); | |
136 } | |
137 | |
138 if (it == end) { | |
139 // Removed everything | |
140 m_inactiveFontData.clear(); | |
141 } else { | |
142 for (int i = 0; i < count; ++i) | |
143 m_inactiveFontData.remove(m_inactiveFontData.begin()); | |
144 } | |
145 | |
146 bool didWork = fontDataToDelete.size(); | |
147 | |
148 fontDataToDelete.clear(); | |
149 | |
150 isPurging = false; | |
151 | |
152 return didWork; | |
153 } | |
154 | |
155 } | |
OLD | NEW |