Chromium Code Reviews| 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()); |
| 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() && !m_paintList[info.displayI temIndexes[offset]]->idsEqual(displayItem)) | |
|
chrishtr
2015/01/22 20:13:07
The optimization using offset implies that cached
Xianzhu
2015/01/22 21:48:12
There might be two reasons of using a vector conta
| |
| 56 ++offset; | |
| 57 // FIXME: We should assert index < info.displayItemIndexes.size(), but curre ntly | |
| 58 // our paint invalidation doesn't always invalidate all objects needing repa int. | |
| 59 // We should fix them before enabling the assert. crbug.com/450725. | |
| 60 if (offset >= info.displayItemIndexes.size()) { | |
| 61 // The object needed repaint but wasn't invalidated. Should remove this condition | |
| 62 // after we fix all invalidation issues. | |
| 63 return kNotFound; | |
| 64 } | |
| 65 info.updateOffset = offset + 1; | |
| 66 return info.displayItemIndexes[offset]; | |
| 49 } | 67 } |
| 50 | 68 |
| 51 PaintList::iterator DisplayItemList::findNextMatchingCachedItem(PaintList::itera tor begin, const DisplayItem& displayItem) | 69 void DisplayItemList::appendDisplayItem(PaintList& list, DisplayItemList::Displa yItemsInfoByClientMap& displayItemsInfoByClient, WTF::PassOwnPtr<DisplayItem> di splayItem) |
| 52 { | 70 { |
| 53 PaintList::iterator end = m_paintList.end(); | 71 DisplayItemsInfoByClientMap::iterator it = displayItemsInfoByClient.find(dis playItem->client()); |
| 54 | 72 if (it != displayItemsInfoByClient.end()) { |
| 55 if (!clientCacheIsValid(displayItem.client())) | 73 it->value.displayItemIndexes.append(list.size()); |
| 56 return end; | 74 } else { |
| 57 | 75 DisplayItemsInfo info; |
| 58 for (PaintList::iterator it = begin; it != end; ++it) { | 76 info.updateOffset = 0; |
| 59 DisplayItem& existing = **it; | 77 info.displayItemIndexes.append(list.size()); |
| 60 if (existing.idsEqual(displayItem)) | 78 displayItemsInfoByClient.add(displayItem->client(), info); |
| 61 return it; | |
| 62 } | 79 } |
| 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); | 80 list.append(displayItem); |
| 72 } | 81 } |
| 73 | 82 |
| 74 // Update the existing paintList by removing invalidated entries, updating | 83 // Update the existing paintList by removing invalidated entries, updating |
| 75 // repainted ones, and appending new items. | 84 // repainted ones, and appending new items. |
| 76 // | 85 // |
| 77 // The algorithm is O(|existing paint list| + |newly painted list|): by using | 86 // 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. | 87 // the ordering implied by the existing paint list, extra treewalks are avoided. |
| 79 void DisplayItemList::updatePaintList() | 88 void DisplayItemList::updatePaintList() |
| 80 { | 89 { |
| 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; | 90 PaintList updatedList; |
| 89 HashSet<DisplayItemClient> newCachedClients; | 91 DisplayItemsInfoByClientMap newCachedDisplayItemsInfoByClient; |
| 90 | |
| 91 PaintList::iterator paintListIt = m_paintList.begin(); | |
| 92 PaintList::iterator paintListEnd = m_paintList.end(); | |
| 93 | 92 |
| 94 for (OwnPtr<DisplayItem>& newDisplayItem : m_newPaints) { | 93 for (OwnPtr<DisplayItem>& newDisplayItem : m_newPaints) { |
| 95 PaintList::iterator cachedItemIt = findNextMatchingCachedItem(paintListI t, *newDisplayItem); | 94 if (!newDisplayItem->isCached()) { |
| 96 if (cachedItemIt != paintListEnd) { | 95 // Copy over the new item. |
| 97 // Copy all of the existing items over until we hit the matching cac hed item. | 96 appendDisplayItem(updatedList, newCachedDisplayItemsInfoByClient, ne wDisplayItem.release()); |
| 98 for (; paintListIt != cachedItemIt; ++paintListIt) { | 97 continue; |
| 99 if (clientCacheIsValid((*paintListIt)->client())) | 98 } |
| 100 appendDisplayItem(updatedList, newCachedClients, paintListIt ->release()); | |
| 101 } | |
| 102 | 99 |
| 103 // Use the cached item for the new display item. | 100 // Use the cached item for the new display item. |
| 104 appendDisplayItem(updatedList, newCachedClients, cachedItemIt->relea se()); | 101 size_t index = findNextMatchingCachedItem(*newDisplayItem); |
| 105 ++paintListIt; | 102 if (index == kNotFound) { |
| 106 } else { | 103 // FIXME: The object needed repaint but wasn't invalidated. Should r emove this condition |
| 107 // If the new display item is a cached placeholder, we should have f ound | 104 // after we fix all invalidation issues. crbug.com/450725. |
| 108 // the cached display item. | 105 WTF_LOG_ERROR("Object needed repaint but wasn't invalidated. display Item=%s", newDisplayItem->asDebugString().utf8().data()); |
| 109 ASSERT(!newDisplayItem->isCached()); | 106 #ifndef NDEBUG |
| 110 | 107 // Append the CachedDisplayItem which will visually show error indic ator. |
| 111 // Copy over the new item. | 108 appendDisplayItem(updatedList, newCachedDisplayItemsInfoByClient, ne wDisplayItem.release()); |
| 112 appendDisplayItem(updatedList, newCachedClients, newDisplayItem.rele ase()); | 109 #endif |
| 110 continue; | |
| 113 } | 111 } |
| 114 } | 112 appendDisplayItem(updatedList, newCachedDisplayItemsInfoByClient, m_pain tList[index].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 } | 113 } |
| 121 | 114 |
| 122 m_newPaints.clear(); | 115 m_newPaints.clear(); |
| 123 m_paintList.clear(); | 116 m_paintList.clear(); |
| 124 m_paintList.swap(updatedList); | 117 m_paintList.swap(updatedList); |
| 125 m_cachedClients.clear(); | 118 m_cachedDisplayItemsInfoByClient.clear(); |
| 126 m_cachedClients.swap(newCachedClients); | 119 m_cachedDisplayItemsInfoByClient.swap(newCachedDisplayItemsInfoByClient); |
| 127 } | 120 } |
| 128 | 121 |
| 129 #ifndef NDEBUG | 122 #ifndef NDEBUG |
| 130 | 123 |
| 131 WTF::String DisplayItemList::paintListAsDebugString(const PaintList& list) const | 124 WTF::String DisplayItemList::paintListAsDebugString(const PaintList& list) const |
| 132 { | 125 { |
| 133 StringBuilder stringBuilder; | 126 StringBuilder stringBuilder; |
| 134 bool isFirst = true; | 127 for (size_t i = 0; i < list.size(); ++i) { |
| 135 for (auto& displayItem : list) { | 128 const OwnPtr<DisplayItem>& displayItem = list[i]; |
| 136 if (!isFirst) | 129 if (i) |
| 137 stringBuilder.append(",\n"); | 130 stringBuilder.append(",\n"); |
| 138 isFirst = false; | 131 if (!displayItem) { |
| 139 stringBuilder.append('{'); | 132 stringBuilder.append("null"); |
| 133 continue; | |
| 134 } | |
| 135 stringBuilder.append(String::format("{index: %d, ", (int)i)); | |
| 140 displayItem->dumpPropertiesAsDebugString(stringBuilder); | 136 displayItem->dumpPropertiesAsDebugString(stringBuilder); |
| 141 stringBuilder.append(", cacheIsValid: "); | 137 stringBuilder.append(", cacheIsValid: "); |
| 142 stringBuilder.append(clientCacheIsValid(displayItem->client()) ? "true" : "false"); | 138 stringBuilder.append(clientCacheIsValid(displayItem->client()) ? "true" : "false"); |
| 143 stringBuilder.append('}'); | 139 stringBuilder.append('}'); |
| 144 } | 140 } |
| 145 return stringBuilder.toString(); | 141 return stringBuilder.toString(); |
| 146 } | 142 } |
| 147 | 143 |
| 144 WTF::String DisplayItemList::cachedDisplayItemsInfoByClientAsDebugString() const | |
| 145 { | |
| 146 StringBuilder stringBuilder; | |
| 147 bool isFirst = true; | |
| 148 for (auto& item : m_cachedDisplayItemsInfoByClient) { | |
| 149 if (!isFirst) | |
| 150 stringBuilder.append(",\n"); | |
| 151 isFirst = false; | |
| 152 stringBuilder.append(String::format("{client:%p, mergeIndex:%d, displayI temIndexes:[", item.key, (int)item.value.updateOffset)); | |
| 153 for (size_t i = 0; i < item.value.displayItemIndexes.size(); ++i) { | |
| 154 if (i) | |
| 155 stringBuilder.append(','); | |
| 156 stringBuilder.append(String::format("%d", (int)item.value.displayIte mIndexes[i])); | |
| 157 } | |
| 158 stringBuilder.append("]}"); | |
| 159 } | |
| 160 return stringBuilder.toString(); | |
| 161 } | |
| 162 | |
| 148 void DisplayItemList::showDebugData() const | 163 void DisplayItemList::showDebugData() const |
| 149 { | 164 { |
| 150 fprintf(stderr, "paint list: [%s]\n", paintListAsDebugString(m_paintList).ut f8().data()); | 165 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()); | 166 fprintf(stderr, "new paints: [%s]\n", paintListAsDebugString(m_newPaints).ut f8().data()); |
| 167 fprintf(stderr, "cachedDisplayItemsInfo: [%s]\n", cachedDisplayItemsInfoByCl ientAsDebugString().utf8().data()); | |
| 152 } | 168 } |
| 153 | 169 |
| 154 #endif | 170 #endif // ifndef NDEBUG |
| 155 | 171 |
| 156 void DisplayItemList::replay(GraphicsContext* context) | 172 void DisplayItemList::replay(GraphicsContext* context) |
| 157 { | 173 { |
| 158 updatePaintList(); | 174 updatePaintList(); |
| 159 for (auto& displayItem : m_paintList) | 175 for (auto& displayItem : m_paintList) |
| 160 displayItem->replay(context); | 176 displayItem->replay(context); |
| 161 } | 177 } |
| 162 | 178 |
| 163 } // namespace blink | 179 } // namespace blink |
| OLD | NEW |