OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "config.h" | 5 #include "config.h" |
6 #include "platform/graphics/paint/DisplayItemList.h" | 6 #include "platform/graphics/paint/DisplayItemList.h" |
7 | 7 |
8 #include "platform/NotImplemented.h" | 8 #include "platform/NotImplemented.h" |
9 #include "platform/RuntimeEnabledFeatures.h" | 9 #include "platform/RuntimeEnabledFeatures.h" |
10 #ifndef NDEBUG | 10 #ifndef NDEBUG |
(...skipping 23 matching lines...) Expand all Loading... |
34 m_newPaints.removeLast(); | 34 m_newPaints.removeLast(); |
35 return; | 35 return; |
36 } | 36 } |
37 } | 37 } |
38 m_newPaints.append(displayItem); | 38 m_newPaints.append(displayItem); |
39 } | 39 } |
40 | 40 |
41 void DisplayItemList::invalidate(DisplayItemClient client) | 41 void DisplayItemList::invalidate(DisplayItemClient client) |
42 { | 42 { |
43 ASSERT(RuntimeEnabledFeatures::slimmingPaintEnabled()); | 43 ASSERT(RuntimeEnabledFeatures::slimmingPaintEnabled()); |
44 m_cachedClients.remove(client); | 44 // Can only be called during layout/paintInvalidation, not during painting. |
| 45 ASSERT(m_newPaints.isEmpty()); |
| 46 m_cachedDisplayItemIndicesByClient.remove(client); |
45 } | 47 } |
46 | 48 |
47 void DisplayItemList::invalidateAll() | 49 void DisplayItemList::invalidateAll() |
48 { | 50 { |
49 ASSERT(RuntimeEnabledFeatures::slimmingPaintEnabled()); | 51 ASSERT(RuntimeEnabledFeatures::slimmingPaintEnabled()); |
50 // Can only be called during layout/paintInvalidation, not during painting. | 52 // Can only be called during layout/paintInvalidation, not during painting. |
51 ASSERT(m_newPaints.isEmpty()); | 53 ASSERT(m_newPaints.isEmpty()); |
52 m_paintList.clear(); | 54 m_paintList.clear(); |
53 m_cachedClients.clear(); | 55 m_cachedDisplayItemIndicesByClient.clear(); |
54 } | 56 } |
55 | 57 |
56 bool DisplayItemList::clientCacheIsValid(DisplayItemClient client) const | 58 bool DisplayItemList::clientCacheIsValid(DisplayItemClient client) const |
57 { | 59 { |
58 return RuntimeEnabledFeatures::slimmingPaintDisplayItemCacheEnabled() && m_c
achedClients.contains(client); | 60 return RuntimeEnabledFeatures::slimmingPaintDisplayItemCacheEnabled() && m_c
achedDisplayItemIndicesByClient.contains(client); |
59 } | 61 } |
60 | 62 |
61 PaintList::iterator DisplayItemList::findNextMatchingCachedItem(PaintList::itera
tor begin, const DisplayItem& displayItem) | 63 size_t DisplayItemList::findMatchingCachedItem(const DisplayItem& displayItem) |
62 { | 64 { |
63 PaintList::iterator end = m_paintList.end(); | 65 ASSERT(displayItem.isCached() || displayItem.isSubtreeCached()); |
| 66 ASSERT(clientCacheIsValid(displayItem.client())); |
64 | 67 |
65 if (!clientCacheIsValid(displayItem.client())) | 68 Vector<size_t>& indices = m_cachedDisplayItemIndicesByClient.find(displayIte
m.client())->value; |
66 return end; | 69 DisplayItem::Type matchingType = displayItem.isCached() |
| 70 ? DisplayItem::cachedTypeToDrawingType(displayItem.type()) |
| 71 : DisplayItem::subtreeCachedTypeToBeginSubtreeType(displayItem.type()); |
67 | 72 |
68 for (PaintList::iterator it = begin; it != end; ++it) { | 73 for (size_t index : indices) { |
69 DisplayItem& existing = **it; | 74 OwnPtr<DisplayItem>& existingItem = m_paintList[index]; |
70 if (existing.isDrawing() | 75 ASSERT(!existingItem || existingItem->client() == displayItem.client()); |
71 && existing.client() == displayItem.client() | 76 if (existingItem && existingItem->type() == matchingType) |
72 && existing.type() == DisplayItem::cachedTypeToDrawingType(displayIt
em.type())) | 77 return index; |
73 return it; | |
74 } | 78 } |
75 | 79 |
76 ASSERT_NOT_REACHED(); | 80 // Previously the client generated a empty picture or an empty subtree |
77 return end; | 81 // which is not stored in the cache. |
| 82 return kNotFound; |
78 } | 83 } |
79 | 84 |
80 static void appendDisplayItem(PaintList& list, HashSet<DisplayItemClient>& clien
ts, WTF::PassOwnPtr<DisplayItem> displayItem) | 85 void DisplayItemList::appendDisplayItem(PaintList& list, DisplayItemIndicesByCli
entMap& displayItemIndicesByClient, WTF::PassOwnPtr<DisplayItem> displayItem) |
81 { | 86 { |
82 clients.add(displayItem->client()); | 87 DisplayItemIndicesByClientMap::iterator it = displayItemIndicesByClient.find
(displayItem->client()); |
| 88 Vector<size_t>& indices = it == displayItemIndicesByClient.end() ? |
| 89 displayItemIndicesByClient.add(displayItem->client(), Vector<size_t>()).
storedValue->value : it->value; |
| 90 indices.append(list.size()); |
| 91 |
83 list.append(displayItem); | 92 list.append(displayItem); |
84 } | 93 } |
85 | 94 |
| 95 void DisplayItemList::copyCachedItems(const DisplayItem& displayItem, PaintList&
list, DisplayItemIndicesByClientMap& displayItemIndicesByClient) |
| 96 { |
| 97 size_t index = findMatchingCachedItem(displayItem); |
| 98 if (index == kNotFound) |
| 99 return; |
| 100 |
| 101 if (displayItem.isCached()) { |
| 102 appendDisplayItem(list, displayItemIndicesByClient, m_paintList[index].r
elease()); |
| 103 return; |
| 104 } |
| 105 |
| 106 ASSERT(m_paintList[index]->isBeginSubtree()); |
| 107 DisplayItem* beginSubtree = m_paintList[index].get(); |
| 108 DisplayItem::Type endSubtreeType = DisplayItem::beginSubtreeTypeToEndSubtree
Type(beginSubtree->type()); |
| 109 do { |
| 110 if (clientCacheIsValid(m_paintList[index]->client())) |
| 111 appendDisplayItem(list, displayItemIndicesByClient, m_paintList[inde
x].release()); |
| 112 ++index; |
| 113 } while (list.last()->client() != beginSubtree->client() || list.last()->typ
e() != endSubtreeType); |
| 114 } |
| 115 |
86 // Update the existing paintList by removing invalidated entries, updating | 116 // Update the existing paintList by removing invalidated entries, updating |
87 // repainted ones, and appending new items. | 117 // repainted ones, and appending new items. |
| 118 // - For CachedDisplayItem, copy the corresponding cached DrawingDisplayItem; |
| 119 // - For SubtreeCachedDisplayItem, copy the cached display items between the |
| 120 // corresponding BeginSubtreeDisplayItem and EndSubtreeDisplayItem (incl.); |
| 121 // - Otherwise, copy the new display item. |
88 // | 122 // |
89 // The algorithm is O(|existing paint list| + |newly painted list|): by using | 123 // The algorithm is O(|existing paint list| + |newly painted list|). |
90 // the ordering implied by the existing paint list, extra treewalks are avoided. | 124 // Coefficients are related to the ratio of [Subtree]CachedDisplayItems |
| 125 // and the average number of (Drawing|BeginSubtree)DisplayItems per client. |
91 void DisplayItemList::updatePaintList() | 126 void DisplayItemList::updatePaintList() |
92 { | 127 { |
93 if (!RuntimeEnabledFeatures::slimmingPaintDisplayItemCacheEnabled()) { | 128 if (!RuntimeEnabledFeatures::slimmingPaintDisplayItemCacheEnabled()) { |
94 m_paintList.clear(); | 129 m_paintList.clear(); |
95 m_paintList.swap(m_newPaints); | 130 m_paintList.swap(m_newPaints); |
96 m_cachedClients.clear(); | 131 m_cachedDisplayItemIndicesByClient.clear(); |
97 return; | 132 return; |
98 } | 133 } |
99 | 134 |
100 PaintList updatedList; | 135 PaintList updatedList; |
101 HashSet<DisplayItemClient> newCachedClients; | 136 DisplayItemIndicesByClientMap newCachedDisplayItemIndicesByClient; |
102 | |
103 PaintList::iterator paintListIt = m_paintList.begin(); | |
104 PaintList::iterator paintListEnd = m_paintList.end(); | |
105 | 137 |
106 for (OwnPtr<DisplayItem>& newDisplayItem : m_newPaints) { | 138 for (OwnPtr<DisplayItem>& newDisplayItem : m_newPaints) { |
107 PaintList::iterator cachedItemIt = findNextMatchingCachedItem(paintListI
t, *newDisplayItem); | 139 if (newDisplayItem->isCached() || newDisplayItem->isSubtreeCached()) |
108 if (cachedItemIt != paintListEnd) { | 140 copyCachedItems(*newDisplayItem, updatedList, newCachedDisplayItemIn
dicesByClient); |
109 // Copy all of the existing items over until we hit the matching cac
hed item. | 141 else |
110 for (; paintListIt != cachedItemIt; ++paintListIt) { | 142 appendDisplayItem(updatedList, newCachedDisplayItemIndicesByClient,
newDisplayItem.release()); |
111 if (clientCacheIsValid((*paintListIt)->client())) | |
112 appendDisplayItem(updatedList, newCachedClients, paintListIt
->release()); | |
113 } | |
114 | |
115 // Use the cached item for the new display item. | |
116 appendDisplayItem(updatedList, newCachedClients, cachedItemIt->relea
se()); | |
117 ++paintListIt; | |
118 } else { | |
119 // If the new display item is a cached placeholder, we should have f
ound | |
120 // the cached display item. | |
121 ASSERT(!newDisplayItem->isCached()); | |
122 | |
123 // Copy over the new item. | |
124 appendDisplayItem(updatedList, newCachedClients, newDisplayItem.rele
ase()); | |
125 } | |
126 } | |
127 | |
128 // Copy over any remaining items that are validly cached. | |
129 for (; paintListIt != paintListEnd; ++paintListIt) { | |
130 if (clientCacheIsValid((*paintListIt)->client())) | |
131 appendDisplayItem(updatedList, newCachedClients, paintListIt->releas
e()); | |
132 } | 143 } |
133 | 144 |
134 m_newPaints.clear(); | 145 m_newPaints.clear(); |
135 m_paintList.clear(); | 146 m_paintList.clear(); |
136 m_paintList.swap(updatedList); | 147 m_paintList.swap(updatedList); |
137 m_cachedClients.clear(); | 148 m_cachedDisplayItemIndicesByClient.clear(); |
138 m_cachedClients.swap(newCachedClients); | 149 m_cachedDisplayItemIndicesByClient.swap(newCachedDisplayItemIndicesByClient)
; |
139 } | 150 } |
140 | 151 |
141 #ifndef NDEBUG | 152 #ifndef NDEBUG |
142 | 153 |
143 WTF::String DisplayItemList::paintListAsDebugString(const PaintList& list) const | 154 WTF::String DisplayItemList::paintListAsDebugString(const PaintList& list) const |
144 { | 155 { |
145 StringBuilder stringBuilder; | 156 StringBuilder stringBuilder; |
146 bool isFirst = true; | 157 for (size_t i = 0; i < list.size(); ++i) { |
147 for (auto& displayItem : list) { | 158 const OwnPtr<DisplayItem>& displayItem = list[i]; |
148 if (!isFirst) | 159 if (i) |
149 stringBuilder.append(",\n"); | 160 stringBuilder.append(",\n"); |
150 isFirst = false; | 161 if (!displayItem) { |
151 stringBuilder.append('{'); | 162 stringBuilder.append("null"); |
| 163 continue; |
| 164 } |
| 165 stringBuilder.append(String::format("{index: %d, ", (int)i)); |
152 displayItem->dumpPropertiesAsDebugString(stringBuilder); | 166 displayItem->dumpPropertiesAsDebugString(stringBuilder); |
153 stringBuilder.append(", cacheIsValid: "); | 167 stringBuilder.append(", cacheIsValid: "); |
154 stringBuilder.append(clientCacheIsValid(displayItem->client()) ? "true"
: "false"); | 168 stringBuilder.append(clientCacheIsValid(displayItem->client()) ? "true"
: "false"); |
155 stringBuilder.append('}'); | 169 stringBuilder.append('}'); |
156 } | 170 } |
157 return stringBuilder.toString(); | 171 return stringBuilder.toString(); |
158 } | 172 } |
159 | 173 |
160 void DisplayItemList::showDebugData() const | 174 void DisplayItemList::showDebugData() const |
161 { | 175 { |
162 fprintf(stderr, "paint list: [%s]\n", paintListAsDebugString(m_paintList).ut
f8().data()); | 176 fprintf(stderr, "paint list: [%s]\n", paintListAsDebugString(m_paintList).ut
f8().data()); |
163 fprintf(stderr, "new paints: [%s]\n", paintListAsDebugString(m_newPaints).ut
f8().data()); | 177 fprintf(stderr, "new paints: [%s]\n", paintListAsDebugString(m_newPaints).ut
f8().data()); |
164 } | 178 } |
165 | 179 |
166 #endif | 180 #endif // ifndef NDEBUG |
167 | 181 |
168 void DisplayItemList::replay(GraphicsContext* context) | 182 void DisplayItemList::replay(GraphicsContext* context) |
169 { | 183 { |
170 updatePaintList(); | 184 updatePaintList(); |
171 for (auto& displayItem : m_paintList) | 185 for (auto& displayItem : m_paintList) |
172 displayItem->replay(context); | 186 displayItem->replay(context); |
173 } | 187 } |
174 | 188 |
175 } // namespace blink | 189 } // namespace blink |
OLD | NEW |