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 |
| 11 #include "platform/graphics/paint/DisplayItem.h" | 11 #include "platform/graphics/paint/DisplayItem.h" |
| 12 #include "wtf/text/StringBuilder.h" | 12 #include "wtf/text/StringBuilder.h" |
| 13 #include <stdio.h> | 13 #include <stdio.h> |
| 14 #endif | 14 #endif |
| 15 | 15 |
| 16 namespace blink { | 16 namespace blink { |
| 17 | 17 |
| 18 const PaintList& DisplayItemList::paintList() | 18 const PaintList& DisplayItemList::paintList() const |
| 19 { | 19 { |
| 20 ASSERT(RuntimeEnabledFeatures::slimmingPaintEnabled()); | 20 ASSERT(RuntimeEnabledFeatures::slimmingPaintEnabled()); |
| 21 ASSERT(m_newPaints.isEmpty()); | 21 ASSERT(m_newPaints.isEmpty()); |
| 22 return m_paintList; | 22 return m_paintList; |
| 23 } | 23 } |
| 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 if (displayItem->isEnd()) { | |
| 29 ASSERT(!m_newPaints.isEmpty()); | |
| 30 if (m_newPaints.last()->isBegin()) { | |
| 31 ASSERT(displayItem->isEndAndPairedWith(*m_newPaints.last())); | |
| 32 // Remove empty pairs. | |
|
chrishtr
2015/02/03 22:30:47
Why would a begin and end be output rather than ca
Xianzhu
2015/02/04 00:13:24
We output both: for subtrees that are painted (sub
Xianzhu
2015/02/04 00:15:17
s/Subtree invalid\/valid flag in this change yet,.
| |
| 33 m_newPaints.removeLast(); | |
| 34 return; | |
| 35 } | |
| 36 } | |
| 37 #if ENABLE(ASSERT) | |
| 38 if (RuntimeEnabledFeatures::slimmingPaintDisplayItemCacheEnabled()) { | |
| 39 ASSERT(!m_newPaintIds.contains(displayItem->id())); | |
| 40 m_newPaintIds.add(displayItem->id()); | |
| 41 } | |
| 42 #endif | |
| 28 m_newPaints.append(displayItem); | 43 m_newPaints.append(displayItem); |
| 29 } | 44 } |
| 30 | 45 |
| 31 void DisplayItemList::invalidate(DisplayItemClient client) | 46 void DisplayItemList::invalidate(DisplayItemClient client) |
| 32 { | 47 { |
| 33 ASSERT(RuntimeEnabledFeatures::slimmingPaintEnabled()); | 48 ASSERT(RuntimeEnabledFeatures::slimmingPaintEnabled()); |
| 49 // Can only be called during layout/paintInvalidation, not during painting. | |
| 50 ASSERT(m_newPaints.isEmpty()); | |
| 34 m_cachedClients.remove(client); | 51 m_cachedClients.remove(client); |
| 35 } | 52 } |
| 36 | 53 |
| 37 void DisplayItemList::invalidateAll() | 54 void DisplayItemList::invalidateAll() |
|
chrishtr
2015/02/03 22:30:47
Also clear out m_displayItemIndexMap?
Xianzhu
2015/02/04 00:13:24
Done.
| |
| 38 { | 55 { |
| 39 ASSERT(RuntimeEnabledFeatures::slimmingPaintEnabled()); | 56 ASSERT(RuntimeEnabledFeatures::slimmingPaintEnabled()); |
| 40 // Can only be called during layout/paintInvalidation, not during painting. | 57 // Can only be called during layout/paintInvalidation, not during painting. |
| 41 ASSERT(m_newPaints.isEmpty()); | 58 ASSERT(m_newPaints.isEmpty()); |
| 42 m_paintList.clear(); | 59 m_paintList.clear(); |
| 43 m_cachedClients.clear(); | 60 m_cachedClients.clear(); |
| 44 } | 61 } |
| 45 | 62 |
| 46 bool DisplayItemList::clientCacheIsValid(DisplayItemClient client) const | 63 bool DisplayItemList::clientCacheIsValid(DisplayItemClient client) const |
| 47 { | 64 { |
| 48 return RuntimeEnabledFeatures::slimmingPaintDisplayItemCacheEnabled() && m_c achedClients.contains(client); | 65 return RuntimeEnabledFeatures::slimmingPaintDisplayItemCacheEnabled() && m_c achedClients.contains(client); |
| 49 } | 66 } |
| 50 | 67 |
| 51 PaintList::iterator DisplayItemList::findNextMatchingCachedItem(PaintList::itera tor begin, const DisplayItem& displayItem) | 68 size_t DisplayItemList::findDisplayItemById(const DisplayItem::Id& id) const |
| 52 { | 69 { |
| 53 PaintList::iterator end = m_paintList.end(); | 70 ASSERT(clientCacheIsValid(id.client)); |
| 54 | 71 DisplayItemIndexMap::const_iterator it = m_displayItemIndexMap.find(id); |
| 55 if (!clientCacheIsValid(displayItem.client())) | 72 return it == m_displayItemIndexMap.end() ? kNotFound : it->value; |
| 56 return end; | |
| 57 | |
| 58 for (PaintList::iterator it = begin; it != end; ++it) { | |
| 59 DisplayItem& existing = **it; | |
| 60 if (existing.isDrawing() | |
| 61 && existing.client() == displayItem.client() | |
| 62 && existing.type() == DisplayItem::cachedTypeToDrawingType(displayIt em.type())) | |
| 63 return it; | |
| 64 } | |
| 65 | |
| 66 ASSERT_NOT_REACHED(); | |
| 67 return end; | |
| 68 } | 73 } |
| 69 | 74 |
| 70 static void appendDisplayItem(PaintList& list, HashSet<DisplayItemClient>& clien ts, WTF::PassOwnPtr<DisplayItem> displayItem) | 75 size_t DisplayItemList::findMatchingCachedItem(const DisplayItem& displayItem) c onst |
| 71 { | 76 { |
| 72 clients.add(displayItem->client()); | 77 ASSERT(displayItem.isCached()); |
| 78 ASSERT(clientCacheIsValid(displayItem.client())); | |
| 79 | |
| 80 return findDisplayItemById(DisplayItem::Id(displayItem.client(), DisplayItem ::cachedTypeToDrawingType(displayItem.type()))); | |
| 81 } | |
| 82 | |
| 83 void DisplayItemList::appendDisplayItem(PassOwnPtr<DisplayItem> displayItem, Pai ntList& list, DisplayItemClientSet& cachedClients, DisplayItemIndexMap& displayI temIndexMap) | |
| 84 { | |
| 85 const DisplayItem::Id& id = displayItem->id(); | |
| 73 list.append(displayItem); | 86 list.append(displayItem); |
| 87 cachedClients.add(id.client); | |
| 88 | |
| 89 ASSERT(!displayItemIndexMap.contains(id)); | |
| 90 displayItemIndexMap.add(id, list.size() - 1); | |
| 91 } | |
| 92 | |
| 93 void DisplayItemList::copyCachedSubtree(const DisplayItem& displayItem, PaintLis t& list, DisplayItemClientSet& cachedClients, DisplayItemIndexMap& displayItemIn dexMap) | |
| 94 { | |
| 95 ASSERT(displayItem.isSubtreeCached()); | |
| 96 ASSERT(clientCacheIsValid(displayItem.client())); | |
| 97 | |
| 98 size_t beginSubtreeIndex = findDisplayItemById(DisplayItem::Id(displayItem.c lient(), DisplayItem::subtreeCachedTypeToBeginSubtreeType(displayItem.type()))); | |
| 99 // The subtree previously produced no display items so was omitted. | |
| 100 if (beginSubtreeIndex == kNotFound) | |
| 101 return; | |
| 102 | |
| 103 size_t endSubtreeIndex = findDisplayItemById(DisplayItem::Id(displayItem.cli ent(), DisplayItem::subtreeCachedTypeToEndSubtreeType(displayItem.type()))); | |
| 104 ASSERT(endSubtreeIndex != kNotFound); | |
| 105 | |
| 106 for (size_t i = beginSubtreeIndex; i <= endSubtreeIndex; ++i) { | |
| 107 if (clientCacheIsValid(m_paintList[i]->client())) | |
| 108 appendDisplayItem(m_paintList[i].release(), list, cachedClients, dis playItemIndexMap); | |
| 109 } | |
| 74 } | 110 } |
| 75 | 111 |
| 76 // Update the existing paintList by removing invalidated entries, updating | 112 // Update the existing paintList by removing invalidated entries, updating |
| 77 // repainted ones, and appending new items. | 113 // repainted ones, and appending new items. |
| 78 // | 114 // |
| 79 // The algorithm is O(|existing paint list| + |newly painted list|): by using | 115 // The algorithm is O(|existing paint list| + |newly painted list|): |
| 80 // the ordering implied by the existing paint list, extra treewalks are avoided. | 116 // - For CachedDisplayItem, copy the corresponding cached DrawingDisplayItem; |
| 117 // - For SubtreeCachedDisplayItem, copy the cached display items from the | |
| 118 // corresponding BeginSubtreeDisplayItem and EndSubtreeDisplayItem; | |
| 119 // - Otherwise, copy the new display item. | |
| 81 void DisplayItemList::updatePaintList() | 120 void DisplayItemList::updatePaintList() |
| 82 { | 121 { |
| 83 if (!RuntimeEnabledFeatures::slimmingPaintDisplayItemCacheEnabled()) { | 122 if (!RuntimeEnabledFeatures::slimmingPaintDisplayItemCacheEnabled()) { |
| 84 m_paintList.clear(); | 123 m_paintList.clear(); |
| 85 m_paintList.swap(m_newPaints); | 124 m_paintList.swap(m_newPaints); |
| 86 m_cachedClients.clear(); | 125 m_cachedClients.clear(); |
| 87 return; | 126 return; |
| 88 } | 127 } |
| 89 | 128 |
| 90 PaintList updatedList; | 129 PaintList updatedList; |
| 130 DisplayItemIndexMap newDisplayItemIndexMap; | |
| 91 HashSet<DisplayItemClient> newCachedClients; | 131 HashSet<DisplayItemClient> newCachedClients; |
| 92 | 132 |
| 93 PaintList::iterator paintListIt = m_paintList.begin(); | 133 for (OwnPtr<DisplayItem>& newDisplayItem : m_newPaints) { |
| 94 PaintList::iterator paintListEnd = m_paintList.end(); | 134 if (newDisplayItem->isSubtreeCached()) { |
| 135 copyCachedSubtree(*newDisplayItem, updatedList, newCachedClients, ne wDisplayItemIndexMap); | |
| 136 continue; | |
| 137 } | |
| 95 | 138 |
| 96 for (OwnPtr<DisplayItem>& newDisplayItem : m_newPaints) { | 139 if (newDisplayItem->isCached()) { |
| 97 PaintList::iterator cachedItemIt = findNextMatchingCachedItem(paintListI t, *newDisplayItem); | 140 size_t index = findMatchingCachedItem(*newDisplayItem); |
| 98 if (cachedItemIt != paintListEnd) { | 141 // Previously the display item generated an empty picture so was omi tted. |
| 99 // Copy all of the existing items over until we hit the matching cac hed item. | 142 if (index == kNotFound) |
| 100 for (; paintListIt != cachedItemIt; ++paintListIt) { | 143 continue; |
| 101 if (clientCacheIsValid((*paintListIt)->client())) | |
| 102 appendDisplayItem(updatedList, newCachedClients, paintListIt ->release()); | |
| 103 } | |
| 104 | 144 |
| 105 // Use the cached item for the new display item. | 145 // Use the cached item for the new display item. |
| 106 appendDisplayItem(updatedList, newCachedClients, cachedItemIt->relea se()); | 146 newDisplayItem = m_paintList[index].release(); |
| 107 ++paintListIt; | |
| 108 } else { | |
| 109 // If the new display item is a cached placeholder, we should have f ound | |
| 110 // the cached display item. | |
| 111 ASSERT(!newDisplayItem->isCached()); | |
| 112 | |
| 113 // Copy over the new item. | |
| 114 appendDisplayItem(updatedList, newCachedClients, newDisplayItem.rele ase()); | |
| 115 } | 147 } |
| 116 } | 148 appendDisplayItem(newDisplayItem.release(), updatedList, newCachedClient s, newDisplayItemIndexMap); |
| 117 | |
| 118 // Copy over any remaining items that are validly cached. | |
| 119 for (; paintListIt != paintListEnd; ++paintListIt) { | |
| 120 if (clientCacheIsValid((*paintListIt)->client())) | |
| 121 appendDisplayItem(updatedList, newCachedClients, paintListIt->releas e()); | |
| 122 } | 149 } |
| 123 | 150 |
| 124 m_newPaints.clear(); | 151 m_newPaints.clear(); |
|
chrishtr
2015/02/03 22:30:47
Maybe factor into a clear() method that shares cod
| |
| 125 m_paintList.clear(); | 152 m_paintList.clear(); |
| 126 m_paintList.swap(updatedList); | 153 m_paintList.swap(updatedList); |
| 154 m_displayItemIndexMap.clear(); | |
| 155 m_displayItemIndexMap.swap(newDisplayItemIndexMap); | |
| 127 m_cachedClients.clear(); | 156 m_cachedClients.clear(); |
| 128 m_cachedClients.swap(newCachedClients); | 157 m_cachedClients.swap(newCachedClients); |
| 158 #if ENABLE(ASSERT) | |
| 159 m_newPaintIds.clear(); | |
| 160 #endif | |
| 129 } | 161 } |
| 130 | 162 |
| 131 #ifndef NDEBUG | 163 #ifndef NDEBUG |
| 132 | 164 |
| 133 WTF::String DisplayItemList::paintListAsDebugString(const PaintList& list) const | 165 WTF::String DisplayItemList::paintListAsDebugString(const PaintList& list) const |
| 134 { | 166 { |
| 135 StringBuilder stringBuilder; | 167 StringBuilder stringBuilder; |
| 136 bool isFirst = true; | 168 for (size_t i = 0; i < list.size(); ++i) { |
| 137 for (auto& displayItem : list) { | 169 const OwnPtr<DisplayItem>& displayItem = list[i]; |
| 138 if (!isFirst) | 170 if (i) |
| 139 stringBuilder.append(",\n"); | 171 stringBuilder.append(",\n"); |
| 140 isFirst = false; | 172 if (!displayItem) { |
| 141 stringBuilder.append('{'); | 173 stringBuilder.append("null"); |
| 174 continue; | |
| 175 } | |
| 176 stringBuilder.append(String::format("{index: %d, ", (int)i)); | |
| 142 displayItem->dumpPropertiesAsDebugString(stringBuilder); | 177 displayItem->dumpPropertiesAsDebugString(stringBuilder); |
| 143 stringBuilder.append(", cacheIsValid: "); | 178 stringBuilder.append(", cacheIsValid: "); |
| 144 stringBuilder.append(clientCacheIsValid(displayItem->client()) ? "true" : "false"); | 179 stringBuilder.append(clientCacheIsValid(displayItem->client()) ? "true" : "false"); |
| 145 stringBuilder.append('}'); | 180 stringBuilder.append('}'); |
| 146 } | 181 } |
| 147 return stringBuilder.toString(); | 182 return stringBuilder.toString(); |
| 148 } | 183 } |
| 149 | 184 |
| 150 void DisplayItemList::showDebugData() const | 185 void DisplayItemList::showDebugData() const |
| 151 { | 186 { |
| 152 fprintf(stderr, "paint list: [%s]\n", paintListAsDebugString(m_paintList).ut f8().data()); | 187 fprintf(stderr, "paint list: [%s]\n", paintListAsDebugString(m_paintList).ut f8().data()); |
| 153 fprintf(stderr, "new paints: [%s]\n", paintListAsDebugString(m_newPaints).ut f8().data()); | 188 fprintf(stderr, "new paints: [%s]\n", paintListAsDebugString(m_newPaints).ut f8().data()); |
| 154 } | 189 } |
| 155 | 190 |
| 156 #endif | 191 #endif // ifndef NDEBUG |
| 157 | 192 |
| 158 void DisplayItemList::replay(GraphicsContext* context) | 193 void DisplayItemList::replay(GraphicsContext* context) |
| 159 { | 194 { |
| 160 updatePaintList(); | 195 updatePaintList(); |
| 161 for (auto& displayItem : m_paintList) | 196 for (auto& displayItem : m_paintList) |
| 162 displayItem->replay(context); | 197 displayItem->replay(context); |
| 163 } | 198 } |
| 164 | 199 |
| 165 } // namespace blink | 200 } // namespace blink |
| OLD | NEW |