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 13 matching lines...) Expand all Loading... |
24 | 24 |
25 void DisplayItemList::add(WTF::PassOwnPtr<DisplayItem> displayItem) | 25 void DisplayItemList::add(WTF::PassOwnPtr<DisplayItem> displayItem) |
26 { | 26 { |
27 ASSERT(RuntimeEnabledFeatures::slimmingPaintEnabled()); | 27 ASSERT(RuntimeEnabledFeatures::slimmingPaintEnabled()); |
28 m_newPaints.append(displayItem); | 28 m_newPaints.append(displayItem); |
29 } | 29 } |
30 | 30 |
31 void DisplayItemList::invalidate(DisplayItemClient client) | 31 void DisplayItemList::invalidate(DisplayItemClient client) |
32 { | 32 { |
33 ASSERT(RuntimeEnabledFeatures::slimmingPaintEnabled()); | 33 ASSERT(RuntimeEnabledFeatures::slimmingPaintEnabled()); |
34 m_cachedClients.remove(client); | 34 // Can only be called during layout/paintInvalidation, not during painting. |
| 35 ASSERT(m_newPaints.isEmpty()); |
| 36 m_cachedDisplayItemsInfoByClient.remove(client); |
35 } | 37 } |
36 | 38 |
37 void DisplayItemList::invalidateAll() | 39 void DisplayItemList::invalidateAll() |
38 { | 40 { |
39 ASSERT(RuntimeEnabledFeatures::slimmingPaintEnabled()); | 41 ASSERT(RuntimeEnabledFeatures::slimmingPaintEnabled()); |
40 // Can only be called during layout/paintInvalidation, not during painting. | 42 // Can only be called during layout/paintInvalidation, not during painting. |
41 ASSERT(m_newPaints.isEmpty()); | 43 ASSERT(m_newPaints.isEmpty()); |
42 m_paintList.clear(); | 44 m_paintList.clear(); |
43 m_cachedClients.clear(); | 45 m_cachedDisplayItemsInfoByClient.clear(); |
44 } | 46 } |
45 | 47 |
46 bool DisplayItemList::clientCacheIsValid(DisplayItemClient client) const | 48 size_t DisplayItemList::findNextMatchingCachedItem(const DisplayItem& displayIte
m) |
47 { | 49 { |
48 return RuntimeEnabledFeatures::slimmingPaintDisplayItemCacheEnabled() && m_c
achedClients.contains(client); | 50 ASSERT(displayItem.isCached() || displayItem.isSubtreeCached()); |
| 51 ASSERT(clientCacheIsValid(displayItem.client())); |
| 52 |
| 53 DisplayItemsInfo& info = m_cachedDisplayItemsInfoByClient.find(displayItem.c
lient())->value; |
| 54 size_t offset = info.updateOffset; |
| 55 while (offset < info.displayItemIndexes.size()) { |
| 56 OwnPtr<DisplayItem>& existingItem = m_paintList[info.displayItemIndexes[
offset]]; |
| 57 if (existingItem && existingItem->idsEqual(displayItem)) |
| 58 break; |
| 59 ++offset; |
| 60 } |
| 61 // FIXME: We should assert index < info.displayItemIndexes.size(), but curre
ntly |
| 62 // our paint invalidation doesn't always invalidate all objects needing repa
int. |
| 63 // We should fix them before enabling the assert. crbug.com/450725. |
| 64 if (offset >= info.displayItemIndexes.size()) { |
| 65 // The object needed repaint but wasn't invalidated. Should remove this
condition |
| 66 // after we fix all invalidation issues. |
| 67 return kNotFound; |
| 68 } |
| 69 info.updateOffset = offset + 1; |
| 70 return info.displayItemIndexes[offset]; |
49 } | 71 } |
50 | 72 |
51 PaintList::iterator DisplayItemList::findNextMatchingCachedItem(PaintList::itera
tor begin, const DisplayItem& displayItem) | 73 void DisplayItemList::appendDisplayItem(PaintList& list, DisplayItemList::Displa
yItemsInfoByClientMap& displayItemsInfoByClient, WTF::PassOwnPtr<DisplayItem> di
splayItem) |
52 { | 74 { |
53 PaintList::iterator end = m_paintList.end(); | 75 DisplayItemsInfoByClientMap::iterator it = displayItemsInfoByClient.find(dis
playItem->client()); |
54 | 76 if (it != displayItemsInfoByClient.end()) { |
55 if (!clientCacheIsValid(displayItem.client())) | 77 it->value.displayItemIndexes.append(list.size()); |
56 return end; | 78 } else { |
57 | 79 DisplayItemsInfo info; |
58 for (PaintList::iterator it = begin; it != end; ++it) { | 80 info.displayItemIndexes.append(list.size()); |
59 DisplayItem& existing = **it; | 81 displayItemsInfoByClient.add(displayItem->client(), info); |
60 if (existing.idsEqual(displayItem)) | |
61 return it; | |
62 } | 82 } |
63 | |
64 ASSERT_NOT_REACHED(); | |
65 return end; | |
66 } | |
67 | |
68 static void appendDisplayItem(PaintList& list, HashSet<DisplayItemClient>& clien
ts, WTF::PassOwnPtr<DisplayItem> displayItem) | |
69 { | |
70 clients.add(displayItem->client()); | |
71 list.append(displayItem); | 83 list.append(displayItem); |
72 } | 84 } |
73 | 85 |
| 86 void DisplayItemList::copyCachedSubtree(const DisplayItem& subtreeCachedDisplayI
tem, PaintList& list, DisplayItemList::DisplayItemsInfoByClientMap& displayItems
InfoByClient) |
| 87 { |
| 88 size_t index = findNextMatchingCachedItem(subtreeCachedDisplayItem); |
| 89 ASSERT(index != kNotFound); |
| 90 ASSERT(m_paintList[index]->isBeginSubtree()); |
| 91 do { |
| 92 if (clientCacheIsValid(m_paintList[index].client()) |
| 93 appendDisplayItem(list, displayItemsInfoByClient, m_paintList[index]
.release()); |
| 94 ++index; |
| 95 } while (!list.last()->isEndSubtree()); |
| 96 } |
| 97 |
74 // Update the existing paintList by removing invalidated entries, updating | 98 // Update the existing paintList by removing invalidated entries, updating |
75 // repainted ones, and appending new items. | 99 // repainted ones, and appending new items. |
76 // | 100 // |
77 // The algorithm is O(|existing paint list| + |newly painted list|): by using | 101 // The algorithm is O(|existing paint list| + |newly painted list|): by using |
78 // the ordering implied by the existing paint list, extra treewalks are avoided. | 102 // the ordering implied by the existing paint list, extra treewalks are avoided. |
79 void DisplayItemList::updatePaintList() | 103 void DisplayItemList::updatePaintList() |
80 { | 104 { |
81 if (!RuntimeEnabledFeatures::slimmingPaintDisplayItemCacheEnabled()) { | |
82 m_paintList.clear(); | |
83 m_paintList.swap(m_newPaints); | |
84 m_cachedClients.clear(); | |
85 return; | |
86 } | |
87 | |
88 PaintList updatedList; | 105 PaintList updatedList; |
89 HashSet<DisplayItemClient> newCachedClients; | 106 DisplayItemsInfoByClientMap newCachedDisplayItemsInfoByClient; |
90 | |
91 PaintList::iterator paintListIt = m_paintList.begin(); | |
92 PaintList::iterator paintListEnd = m_paintList.end(); | |
93 | 107 |
94 for (OwnPtr<DisplayItem>& newDisplayItem : m_newPaints) { | 108 for (OwnPtr<DisplayItem>& newDisplayItem : m_newPaints) { |
95 PaintList::iterator cachedItemIt = findNextMatchingCachedItem(paintListI
t, *newDisplayItem); | 109 if (newDisplayItem->isSubtreeCached()) { |
96 if (cachedItemIt != paintListEnd) { | 110 copyCachedSubtree(*newDisplayItem, updatedList, newCachedDisplayItem
sInfoByClient); |
97 // Copy all of the existing items over until we hit the matching cac
hed item. | 111 continue; |
98 for (; paintListIt != cachedItemIt; ++paintListIt) { | 112 } |
99 if (clientCacheIsValid((*paintListIt)->client())) | 113 |
100 appendDisplayItem(updatedList, newCachedClients, paintListIt
->release()); | 114 if (newDisplayItem->isCached()) { |
| 115 size_t index = findNextMatchingCachedItem(*newDisplayItem); |
| 116 if (index != kNotFound) { |
| 117 // Use the cached item for the new display item. |
| 118 newDisplayItem = m_paintList[index].release(); |
| 119 } else { |
| 120 // FIXME: The object needed repaint but wasn't invalidated. Shou
ld remove this condition |
| 121 // after we fix all invalidation issues. crbug.com/450725. |
| 122 WTF_LOG_ERROR("Object needed repaint but wasn't invalidated."); |
| 123 #ifdef NDEBUG |
| 124 continue; // Ignore the item in release build. |
| 125 #else |
| 126 WTF_LOG_ERROR("displayItem=%s", newDisplayItem->asDebugString().
utf8().data()); |
| 127 // The CachedDisplayItem will be appended to visually show error
indicator. |
| 128 #endif |
101 } | 129 } |
102 | |
103 // Use the cached item for the new display item. | |
104 appendDisplayItem(updatedList, newCachedClients, cachedItemIt->relea
se()); | |
105 ++paintListIt; | |
106 } else { | |
107 // If the new display item is a cached placeholder, we should have f
ound | |
108 // the cached display item. | |
109 ASSERT(!newDisplayItem->isCached()); | |
110 | |
111 // Copy over the new item. | |
112 appendDisplayItem(updatedList, newCachedClients, newDisplayItem.rele
ase()); | |
113 } | 130 } |
114 } | 131 appendDisplayItem(updatedList, newCachedDisplayItemsInfoByClient, newDis
playItem.release()); |
115 | |
116 // Copy over any remaining items that are validly cached. | |
117 for (; paintListIt != paintListEnd; ++paintListIt) { | |
118 if (clientCacheIsValid((*paintListIt)->client())) | |
119 appendDisplayItem(updatedList, newCachedClients, paintListIt->releas
e()); | |
120 } | 132 } |
121 | 133 |
122 m_newPaints.clear(); | 134 m_newPaints.clear(); |
123 m_paintList.clear(); | 135 m_paintList.clear(); |
124 m_paintList.swap(updatedList); | 136 m_paintList.swap(updatedList); |
125 m_cachedClients.clear(); | 137 m_cachedDisplayItemsInfoByClient.clear(); |
126 m_cachedClients.swap(newCachedClients); | 138 m_cachedDisplayItemsInfoByClient.swap(newCachedDisplayItemsInfoByClient); |
127 } | 139 } |
128 | 140 |
129 #ifndef NDEBUG | 141 #ifndef NDEBUG |
130 | 142 |
131 WTF::String DisplayItemList::paintListAsDebugString(const PaintList& list) const | 143 WTF::String DisplayItemList::paintListAsDebugString(const PaintList& list) const |
132 { | 144 { |
133 StringBuilder stringBuilder; | 145 StringBuilder stringBuilder; |
134 bool isFirst = true; | 146 for (size_t i = 0; i < list.size(); ++i) { |
135 for (auto& displayItem : list) { | 147 const OwnPtr<DisplayItem>& displayItem = list[i]; |
136 if (!isFirst) | 148 if (i) |
137 stringBuilder.append(",\n"); | 149 stringBuilder.append(",\n"); |
138 isFirst = false; | 150 if (!displayItem) { |
139 stringBuilder.append('{'); | 151 stringBuilder.append("null"); |
| 152 continue; |
| 153 } |
| 154 stringBuilder.append(String::format("{index: %d, ", (int)i)); |
140 displayItem->dumpPropertiesAsDebugString(stringBuilder); | 155 displayItem->dumpPropertiesAsDebugString(stringBuilder); |
141 stringBuilder.append(", cacheIsValid: "); | 156 stringBuilder.append(", cacheIsValid: "); |
142 stringBuilder.append(clientCacheIsValid(displayItem->client()) ? "true"
: "false"); | 157 stringBuilder.append(clientCacheIsValid(displayItem->client()) ? "true"
: "false"); |
143 stringBuilder.append('}'); | 158 stringBuilder.append('}'); |
144 } | 159 } |
145 return stringBuilder.toString(); | 160 return stringBuilder.toString(); |
146 } | 161 } |
147 | 162 |
| 163 WTF::String DisplayItemList::cachedDisplayItemsInfoByClientAsDebugString() const |
| 164 { |
| 165 StringBuilder stringBuilder; |
| 166 bool isFirst = true; |
| 167 for (auto& item : m_cachedDisplayItemsInfoByClient) { |
| 168 if (!isFirst) |
| 169 stringBuilder.append(",\n"); |
| 170 isFirst = false; |
| 171 stringBuilder.append(String::format("{client:%p, updateOffset:%d, displa
yItemIndexes:[", item.key, (int)item.value.updateOffset)); |
| 172 for (size_t i = 0; i < item.value.displayItemIndexes.size(); ++i) { |
| 173 if (i) |
| 174 stringBuilder.append(','); |
| 175 stringBuilder.append(String::format("%d", (int)item.value.displayIte
mIndexes[i])); |
| 176 } |
| 177 stringBuilder.append("]}"); |
| 178 } |
| 179 return stringBuilder.toString(); |
| 180 } |
| 181 |
148 void DisplayItemList::showDebugData() const | 182 void DisplayItemList::showDebugData() const |
149 { | 183 { |
150 fprintf(stderr, "paint list: [%s]\n", paintListAsDebugString(m_paintList).ut
f8().data()); | 184 fprintf(stderr, "paint list: [%s]\n", paintListAsDebugString(m_paintList).ut
f8().data()); |
151 fprintf(stderr, "new paints: [%s]\n", paintListAsDebugString(m_newPaints).ut
f8().data()); | 185 fprintf(stderr, "new paints: [%s]\n", paintListAsDebugString(m_newPaints).ut
f8().data()); |
| 186 fprintf(stderr, "cachedDisplayItemsInfo: [%s]\n", cachedDisplayItemsInfoByCl
ientAsDebugString().utf8().data()); |
152 } | 187 } |
153 | 188 |
154 #endif | 189 #endif // ifndef NDEBUG |
155 | 190 |
156 void DisplayItemList::replay(GraphicsContext* context) | 191 void DisplayItemList::replay(GraphicsContext* context) |
157 { | 192 { |
158 updatePaintList(); | 193 updatePaintList(); |
159 for (auto& displayItem : m_paintList) | 194 for (auto& displayItem : m_paintList) |
160 displayItem->replay(context); | 195 displayItem->replay(context); |
161 } | 196 } |
162 | 197 |
163 } // namespace blink | 198 } // namespace blink |
OLD | NEW |