1 /*
2 * Copyright (C) 2011, 2012 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 #include "config.h"
27 #include "core/css/CSSValuePool.h"
28
29 #include "core/css/CSSValueList.h"
30 #include "core/css/parser/CSSParser.h"
31 #include "core/rendering/style/RenderStyle.h"
32
33 namespace blink {
34
cssValuePool()35 CSSValuePool& cssValuePool()
36 {
37 DEFINE_STATIC_LOCAL(OwnPtrWillBePersistent<CSSValuePool>, pool, (adoptPtrWillBeNoop(new CSSValuePool())));
38 return *pool;
39 }
40
CSSValuePool()41 CSSValuePool::CSSValuePool()
42 : m_inheritedValue(CSSInheritedValue::create())
43 , m_implicitInitialValue(CSSInitialValue::createImplicit())
44 , m_explicitInitialValue(CSSInitialValue::createExplicit())
45 , m_colorTransparent(CSSPrimitiveValue::createColor(Color::transparent))
46 , m_colorWhite(CSSPrimitiveValue::createColor(Color::white))
47 , m_colorBlack(CSSPrimitiveValue::createColor(Color::black))
48 {
49 m_identifierValueCache.resize(numCSSValueKeywords);
50 m_pixelValueCache.resize(maximumCacheableIntegerValue + 1);
51 m_percentValueCache.resize(maximumCacheableIntegerValue + 1);
52 m_numberValueCache.resize(maximumCacheableIntegerValue + 1);
53 }
54
createIdentifierValue(CSSValueID ident)55 PassRefPtrWillBeRawPtr<CSSPrimitiveValue> CSSValuePool::createIdentifierValue(CSSValueID ident)
56 {
57 if (ident <= 0)
58 return CSSPrimitiveValue::createIdentifier(ident);
59
60 if (!m_identifierValueCache[ident])
61 m_identifierValueCache[ident] = CSSPrimitiveValue::createIdentifier(ident);
62 return m_identifierValueCache[ident];
63 }
64
createIdentifierValue(CSSPropertyID ident)65 PassRefPtrWillBeRawPtr<CSSPrimitiveValue> CSSValuePool::createIdentifierValue(CSSPropertyID ident)
66 {
67 return CSSPrimitiveValue::createIdentifier(ident);
68 }
69
createColorValue(unsigned rgbValue)70 PassRefPtrWillBeRawPtr<CSSPrimitiveValue> CSSValuePool::createColorValue(unsigned rgbValue)
71 {
72 // These are the empty and deleted values of the hash table.
73 if (rgbValue == Color::transparent)
74 return m_colorTransparent;
75 if (rgbValue == Color::white)
76 return m_colorWhite;
77 // Just because it is common.
78 if (rgbValue == Color::black)
79 return m_colorBlack;
80
81 // Just wipe out the cache and start rebuilding if it gets too big.
82 const unsigned maximumColorCacheSize = 512;
83 if (m_colorValueCache.size() > maximumColorCacheSize)
84 m_colorValueCache.clear();
85
86 RefPtrWillBeRawPtr<CSSPrimitiveValue> dummyValue = nullptr;
87 ColorValueCache::AddResult entry = m_colorValueCache.add(rgbValue, dummyValue);
88 if (entry.isNewEntry)
89 entry.storedValue->value = CSSPrimitiveValue::createColor(rgbValue);
90 return entry.storedValue->value;
91 }
92
createValue(double value,CSSPrimitiveValue::UnitType type)93 PassRefPtrWillBeRawPtr<CSSPrimitiveValue> CSSValuePool::createValue(double value, CSSPrimitiveValue::UnitType type)
94 {
95 if (std::isinf(value))
96 value = 0;
97
98 if (value < 0 || value > maximumCacheableIntegerValue)
99 return CSSPrimitiveValue::create(value, type);
100
101 int intValue = static_cast<int>(value);
102 if (value != intValue)
103 return CSSPrimitiveValue::create(value, type);
104
105 switch (type) {
106 case CSSPrimitiveValue::CSS_PX:
107 if (!m_pixelValueCache[intValue])
108 m_pixelValueCache[intValue] = CSSPrimitiveValue::create(value, type);
109 return m_pixelValueCache[intValue];
110 case CSSPrimitiveValue::CSS_PERCENTAGE:
111 if (!m_percentValueCache[intValue])
112 m_percentValueCache[intValue] = CSSPrimitiveValue::create(value, type);
113 return m_percentValueCache[intValue];
114 case CSSPrimitiveValue::CSS_NUMBER:
115 if (!m_numberValueCache[intValue])
116 m_numberValueCache[intValue] = CSSPrimitiveValue::create(value, type);
117 return m_numberValueCache[intValue];
118 default:
119 return CSSPrimitiveValue::create(value, type);
120 }
121 }
122
createValue(const Length & value,const RenderStyle & style)123 PassRefPtrWillBeRawPtr<CSSPrimitiveValue> CSSValuePool::createValue(const Length& value, const RenderStyle& style)
124 {
125 return CSSPrimitiveValue::create(value, style.effectiveZoom());
126 }
127
createFontFamilyValue(const String & familyName)128 PassRefPtrWillBeRawPtr<CSSPrimitiveValue> CSSValuePool::createFontFamilyValue(const String& familyName)
129 {
130 RefPtrWillBeMember<CSSPrimitiveValue>& value = m_fontFamilyValueCache.add(familyName, nullptr).storedValue->value;
131 if (!value)
132 value = CSSPrimitiveValue::create(familyName, CSSPrimitiveValue::CSS_STRING);
133 return value;
134 }
135
createFontFaceValue(const AtomicString & string)136 PassRefPtrWillBeRawPtr<CSSValueList> CSSValuePool::createFontFaceValue(const AtomicString& string)
137 {
138 // Just wipe out the cache and start rebuilding if it gets too big.
139 const unsigned maximumFontFaceCacheSize = 128;
140 if (m_fontFaceValueCache.size() > maximumFontFaceCacheSize)
141 m_fontFaceValueCache.clear();
142
143 RefPtrWillBeMember<CSSValueList>& value = m_fontFaceValueCache.add(string, nullptr).storedValue->value;
144 if (!value) {
145 RefPtrWillBeRawPtr<CSSValue> parsedValue = CSSParser::parseSingleValue(CSSPropertyFontFamily, string);
146 if (parsedValue && parsedValue->isValueList())
147 value = toCSSValueList(parsedValue.get());
148 }
149 return value;
150 }
151
trace(Visitor * visitor)152 void CSSValuePool::trace(Visitor* visitor)
153 {
154 #if ENABLE(OILPAN)
155 visitor->trace(m_inheritedValue);
156 visitor->trace(m_implicitInitialValue);
157 visitor->trace(m_explicitInitialValue);
158 visitor->trace(m_identifierValueCache);
159 visitor->trace(m_colorValueCache);
160 visitor->trace(m_colorTransparent);
161 visitor->trace(m_colorWhite);
162 visitor->trace(m_colorBlack);
163 visitor->trace(m_pixelValueCache);
164 visitor->trace(m_percentValueCache);
165 visitor->trace(m_numberValueCache);
166 visitor->trace(m_fontFaceValueCache);
167 visitor->trace(m_fontFamilyValueCache);
168 #endif
169 }
170
171 }
172