[Test] Added tests for CUtil::SplitParams
[xbmc.git] / xbmc / guilib / GUIFontCache.cpp
blob70f17acc391215a48301b387db34e74901d657fa
1 /*
2 * Copyright (C) 2005-2018 Team Kodi
3 * This file is part of Kodi - https://kodi.tv
5 * SPDX-License-Identifier: GPL-2.0-or-later
6 * See LICENSES/README.md for more information.
7 */
9 #include "GUIFontTTF.h"
10 #include "windowing/GraphicContext.h"
12 #include <stdint.h>
13 #include <vector>
15 using namespace std::chrono_literals;
17 namespace
19 constexpr auto FONT_CACHE_TIME_LIMIT = 1000ms;
22 template<class Position, class Value>
23 class CGUIFontCacheImpl
25 struct EntryList
27 using HashMap = std::multimap<size_t, std::unique_ptr<CGUIFontCacheEntry<Position, Value>>>;
28 using HashIter = typename HashMap::iterator;
29 using AgeMap = std::multimap<std::chrono::steady_clock::time_point, HashIter>;
31 ~EntryList() { Flush(); }
33 HashIter Insert(size_t hash, std::unique_ptr<CGUIFontCacheEntry<Position, Value>> v)
35 auto r(hashMap.insert(typename HashMap::value_type(hash, std::move(v))));
36 if (r->second)
37 ageMap.insert(typename AgeMap::value_type(r->second->m_lastUsed, r));
39 return r;
41 void Flush()
43 ageMap.clear();
44 hashMap.clear();
46 typename HashMap::iterator FindKey(CGUIFontCacheKey<Position> key)
48 CGUIFontCacheHash<Position> hashGen;
49 CGUIFontCacheKeysMatch<Position> keyMatch;
50 auto range = hashMap.equal_range(hashGen(key));
51 for (auto ret = range.first; ret != range.second; ++ret)
53 if (keyMatch(ret->second->m_key, key))
55 return ret;
59 return hashMap.end();
61 void UpdateAge(HashIter it, std::chrono::steady_clock::time_point now)
63 auto range = ageMap.equal_range(it->second->m_lastUsed);
64 for (auto ageit = range.first; ageit != range.second; ++ageit)
66 if (ageit->second == it)
68 ageMap.erase(ageit);
69 ageMap.insert(typename AgeMap::value_type(now, it));
70 it->second->m_lastUsed = now;
71 return;
76 HashMap hashMap;
77 AgeMap ageMap;
80 EntryList m_list;
81 CGUIFontCache<Position, Value>* m_parent;
83 public:
84 explicit CGUIFontCacheImpl(CGUIFontCache<Position, Value>* parent) : m_parent(parent) {}
85 Value& Lookup(const CGraphicContext& context,
86 Position& pos,
87 const std::vector<UTILS::COLOR::Color>& colors,
88 const vecText& text,
89 uint32_t alignment,
90 float maxPixelWidth,
91 bool scrolling,
92 std::chrono::steady_clock::time_point now,
93 bool& dirtyCache);
94 void Flush();
97 template<class Position, class Value>
98 CGUIFontCacheEntry<Position, Value>::~CGUIFontCacheEntry()
100 delete &m_key.m_colors;
101 delete &m_key.m_text;
102 m_value.clear();
105 template<class Position, class Value>
106 void CGUIFontCacheEntry<Position, Value>::Assign(const CGUIFontCacheKey<Position>& key,
107 std::chrono::steady_clock::time_point now)
109 m_key.m_pos = key.m_pos;
110 m_key.m_colors.assign(key.m_colors.begin(), key.m_colors.end());
111 m_key.m_text.assign(key.m_text.begin(), key.m_text.end());
112 m_key.m_alignment = key.m_alignment;
113 m_key.m_maxPixelWidth = key.m_maxPixelWidth;
114 m_key.m_scrolling = key.m_scrolling;
115 m_matrix = key.m_matrix;
116 m_key.m_scaleX = key.m_scaleX;
117 m_key.m_scaleY = key.m_scaleY;
118 m_lastUsed = now;
119 m_value.clear();
122 template<class Position, class Value>
123 CGUIFontCache<Position, Value>::CGUIFontCache(CGUIFontTTF& font)
124 : m_impl(std::make_unique<CGUIFontCacheImpl<Position, Value>>(this)), m_font(font)
128 template<class Position, class Value>
129 CGUIFontCache<Position, Value>::~CGUIFontCache() = default;
131 template<class Position, class Value>
132 Value& CGUIFontCache<Position, Value>::Lookup(const CGraphicContext& context,
133 Position& pos,
134 const std::vector<UTILS::COLOR::Color>& colors,
135 const vecText& text,
136 uint32_t alignment,
137 float maxPixelWidth,
138 bool scrolling,
139 std::chrono::steady_clock::time_point now,
140 bool& dirtyCache)
142 if (!m_impl)
143 m_impl = std::make_unique<CGUIFontCacheImpl<Position, Value>>(this);
145 return m_impl->Lookup(context, pos, colors, text, alignment, maxPixelWidth, scrolling, now,
146 dirtyCache);
149 template<class Position, class Value>
150 Value& CGUIFontCacheImpl<Position, Value>::Lookup(const CGraphicContext& context,
151 Position& pos,
152 const std::vector<UTILS::COLOR::Color>& colors,
153 const vecText& text,
154 uint32_t alignment,
155 float maxPixelWidth,
156 bool scrolling,
157 std::chrono::steady_clock::time_point now,
158 bool& dirtyCache)
160 const CGUIFontCacheKey<Position> key(pos, const_cast<std::vector<UTILS::COLOR::Color>&>(colors),
161 const_cast<vecText&>(text), alignment, maxPixelWidth,
162 scrolling, context.GetGUIMatrix(), context.GetGUIScaleX(),
163 context.GetGUIScaleY());
165 auto i = m_list.FindKey(key);
166 if (i == m_list.hashMap.end())
168 // Cache miss
169 dirtyCache = true;
170 std::unique_ptr<CGUIFontCacheEntry<Position, Value>> entry;
172 if (!m_list.ageMap.empty())
174 const auto duration =
175 std::chrono::duration_cast<std::chrono::milliseconds>(now - m_list.ageMap.begin()->first);
176 if (duration > FONT_CACHE_TIME_LIMIT)
178 entry = std::move(m_list.ageMap.begin()->second->second);
179 m_list.hashMap.erase(m_list.ageMap.begin()->second);
180 m_list.ageMap.erase(m_list.ageMap.begin());
184 // add new entry
185 CGUIFontCacheHash<Position> hashgen;
186 if (!entry)
187 entry = std::make_unique<CGUIFontCacheEntry<Position, Value>>(*m_parent, key, now);
188 else
189 entry->Assign(key, now);
190 return m_list.Insert(hashgen(key), std::move(entry))->second->m_value;
192 else
194 // Cache hit
195 // Update the translation arguments so that they hold the offset to apply
196 // to the cached values (but only in the dynamic case)
197 pos.UpdateWithOffsets(i->second->m_key.m_pos, scrolling);
199 // Update time in entry and move to the back of the list
200 m_list.UpdateAge(i, now);
202 dirtyCache = false;
204 return i->second->m_value;
208 template<class Position, class Value>
209 void CGUIFontCache<Position, Value>::Flush()
211 m_impl->Flush();
214 template<class Position, class Value>
215 void CGUIFontCacheImpl<Position, Value>::Flush()
217 m_list.Flush();
220 template CGUIFontCache<CGUIFontCacheStaticPosition, CGUIFontCacheStaticValue>::CGUIFontCache(
221 CGUIFontTTF& font);
222 template CGUIFontCache<CGUIFontCacheStaticPosition, CGUIFontCacheStaticValue>::~CGUIFontCache();
223 template CGUIFontCacheEntry<CGUIFontCacheStaticPosition,
224 CGUIFontCacheStaticValue>::~CGUIFontCacheEntry();
225 template CGUIFontCacheStaticValue& CGUIFontCache<
226 CGUIFontCacheStaticPosition,
227 CGUIFontCacheStaticValue>::Lookup(const CGraphicContext& context,
228 CGUIFontCacheStaticPosition&,
229 const std::vector<UTILS::COLOR::Color>&,
230 const vecText&,
231 uint32_t,
232 float,
233 bool,
234 std::chrono::steady_clock::time_point,
235 bool&);
236 template void CGUIFontCache<CGUIFontCacheStaticPosition, CGUIFontCacheStaticValue>::Flush();
238 template CGUIFontCache<CGUIFontCacheDynamicPosition, CGUIFontCacheDynamicValue>::CGUIFontCache(
239 CGUIFontTTF& font);
240 template CGUIFontCache<CGUIFontCacheDynamicPosition, CGUIFontCacheDynamicValue>::~CGUIFontCache();
241 template CGUIFontCacheEntry<CGUIFontCacheDynamicPosition,
242 CGUIFontCacheDynamicValue>::~CGUIFontCacheEntry();
243 template CGUIFontCacheDynamicValue& CGUIFontCache<
244 CGUIFontCacheDynamicPosition,
245 CGUIFontCacheDynamicValue>::Lookup(const CGraphicContext& context,
246 CGUIFontCacheDynamicPosition&,
247 const std::vector<UTILS::COLOR::Color>&,
248 const vecText&,
249 uint32_t,
250 float,
251 bool,
252 std::chrono::steady_clock::time_point,
253 bool&);
254 template void CGUIFontCache<CGUIFontCacheDynamicPosition, CGUIFontCacheDynamicValue>::Flush();
256 void CVertexBuffer::clear()
258 if (m_font)
259 m_font->DestroyVertexBuffer(*this);