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 00:39:00
Why bother to clean these out?
Xianzhu
2015/02/03 01:57:12
Without this, most of the display items in a paint
| |
| 33 #if ENABLE(ASSERT) | |
| 34 m_newPaintIds.remove(m_newPaints.last()->id()); | |
| 35 #endif | |
| 36 m_newPaints.removeLast(); | |
| 37 return; | |
| 38 } | |
| 39 } | |
| 40 #if ENABLE(ASSERT) | |
| 41 if (RuntimeEnabledFeatures::slimmingPaintDisplayItemCacheEnabled()) { | |
| 42 ASSERT(!m_newPaintIds.contains(displayItem->id())); | |
| 43 if (displayItem->isDrawing() || displayItem->isBeginSubtree()) | |
| 44 m_newPaintIds.add(displayItem->id()); | |
| 45 } | |
| 46 #endif | |
| 28 m_newPaints.append(displayItem); | 47 m_newPaints.append(displayItem); |
| 29 } | 48 } |
| 30 | 49 |
| 31 void DisplayItemList::invalidate(DisplayItemClient client) | 50 void DisplayItemList::invalidate(DisplayItemClient client) |
| 32 { | 51 { |
| 33 ASSERT(RuntimeEnabledFeatures::slimmingPaintEnabled()); | 52 ASSERT(RuntimeEnabledFeatures::slimmingPaintEnabled()); |
| 53 // Can only be called during layout/paintInvalidation, not during painting. | |
| 54 ASSERT(m_newPaints.isEmpty()); | |
| 34 m_cachedClients.remove(client); | 55 m_cachedClients.remove(client); |
| 35 } | 56 } |
| 36 | 57 |
| 37 void DisplayItemList::invalidateAll() | 58 void DisplayItemList::invalidateAll() |
| 38 { | 59 { |
| 39 ASSERT(RuntimeEnabledFeatures::slimmingPaintEnabled()); | 60 ASSERT(RuntimeEnabledFeatures::slimmingPaintEnabled()); |
| 40 // Can only be called during layout/paintInvalidation, not during painting. | 61 // Can only be called during layout/paintInvalidation, not during painting. |
| 41 ASSERT(m_newPaints.isEmpty()); | 62 ASSERT(m_newPaints.isEmpty()); |
| 42 m_paintList.clear(); | 63 m_paintList.clear(); |
| 43 m_cachedClients.clear(); | 64 m_cachedClients.clear(); |
| 44 } | 65 } |
| 45 | 66 |
| 46 bool DisplayItemList::clientCacheIsValid(DisplayItemClient client) const | 67 bool DisplayItemList::clientCacheIsValid(DisplayItemClient client) const |
| 47 { | 68 { |
| 48 return RuntimeEnabledFeatures::slimmingPaintDisplayItemCacheEnabled() && m_c achedClients.contains(client); | 69 return RuntimeEnabledFeatures::slimmingPaintDisplayItemCacheEnabled() && m_c achedClients.contains(client); |
| 49 } | 70 } |
| 50 | 71 |
| 51 PaintList::iterator DisplayItemList::findNextMatchingCachedItem(PaintList::itera tor begin, const DisplayItem& displayItem) | 72 size_t DisplayItemList::findMatchingCachedItem(const DisplayItem& displayItem) |
| 52 { | 73 { |
| 53 PaintList::iterator end = m_paintList.end(); | 74 ASSERT(displayItem.isCached()); |
| 75 ASSERT(clientCacheIsValid(displayItem.client())); | |
| 54 | 76 |
| 55 if (!clientCacheIsValid(displayItem.client())) | 77 DisplayItem::Id drawingDisplayItemId(displayItem.client(), DisplayItem::cach edTypeToDrawingType(displayItem.type())); |
| 56 return end; | 78 DisplayItemIndexMap::const_iterator it = m_displayItemIndexMap.find(drawingD isplayItemId); |
| 57 | 79 return it == m_displayItemIndexMap.end() ? kNotFound : it->value; |
| 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 } | 80 } |
| 69 | 81 |
| 70 static void appendDisplayItem(PaintList& list, HashSet<DisplayItemClient>& clien ts, WTF::PassOwnPtr<DisplayItem> displayItem) | 82 void DisplayItemList::appendDisplayItem(PassOwnPtr<DisplayItem> displayItem, Pai ntList& list, DisplayItemClientSet& cachedClients, DisplayItemIndexMap& displayI temIndexMap) |
| 71 { | 83 { |
| 72 clients.add(displayItem->client()); | 84 const DisplayItem::Id& id = displayItem->id(); |
| 73 list.append(displayItem); | 85 list.append(displayItem); |
| 86 cachedClients.add(id.client); | |
| 87 | |
| 88 // For now we only need to map Drawing and BeginSubtree display items. | |
| 89 if (!list.last()->isDrawing() && !list.last()->isBeginSubtree()) | |
| 90 return; | |
| 91 ASSERT(!displayItemIndexMap.contains(id)); | |
| 92 displayItemIndexMap.add(id, list.size() - 1); | |
| 93 } | |
| 94 | |
| 95 void DisplayItemList::copyCachedSubtree(const DisplayItem& displayItem, PaintLis t& list, DisplayItemClientSet& cachedClients, DisplayItemIndexMap& displayItemIn dexMap) | |
| 96 { | |
| 97 ASSERT(displayItem.isSubtreeCached()); | |
| 98 ASSERT(clientCacheIsValid(displayItem.client())); | |
| 99 | |
| 100 DisplayItem::Id beginSubtreeDisplayItemId(displayItem.client(), DisplayItem: :subtreeCachedTypeToBeginSubtreeType(displayItem.type())); | |
| 101 DisplayItemIndexMap::const_iterator it = m_displayItemIndexMap.find(beginSub treeDisplayItemId); | |
| 102 if (it == m_displayItemIndexMap.end()) | |
| 103 return; | |
|
chrishtr
2015/02/03 00:39:00
Don't you want to insert the CachedSubtreeDisplayI
Xianzhu
2015/02/03 01:57:12
No. This is a valid case that the subtree previous
| |
| 104 | |
| 105 DisplayItem::Id endSubtreeId(displayItem.client(), DisplayItem::subtreeCache dTypeToEndSubtreeType(displayItem.type())); | |
| 106 for (size_t i = it->value; ; ++i) { | |
| 107 if (clientCacheIsValid(m_paintList[i]->client())) | |
| 108 appendDisplayItem(m_paintList[i].release(), list, cachedClients, dis playItemIndexMap); | |
| 109 if (list.last()->id() == endSubtreeId) | |
| 110 break; | |
| 111 } | |
|
chrishtr
2015/02/03 00:39:00
Add an assert that it found the endSubtreeId. Bett
Xianzhu
2015/02/03 01:57:12
Looping between first/last looks clearer. Done.
I
| |
| 74 } | 112 } |
| 75 | 113 |
| 76 // Update the existing paintList by removing invalidated entries, updating | 114 // Update the existing paintList by removing invalidated entries, updating |
| 77 // repainted ones, and appending new items. | 115 // repainted ones, and appending new items. |
| 78 // | 116 // |
| 79 // The algorithm is O(|existing paint list| + |newly painted list|): by using | 117 // The algorithm is O(|existing paint list| + |newly painted list|): |
| 80 // the ordering implied by the existing paint list, extra treewalks are avoided. | 118 // - For CachedDisplayItem, copy the corresponding cached DrawingDisplayItem; |
| 119 // - For SubtreeCachedDisplayItem, copy the cached display items from the | |
| 120 // corresponding BeginSubtreeDisplayItem and EndSubtreeDisplayItem; | |
| 121 // - Otherwise, copy the new display item. | |
| 81 void DisplayItemList::updatePaintList() | 122 void DisplayItemList::updatePaintList() |
| 82 { | 123 { |
| 83 if (!RuntimeEnabledFeatures::slimmingPaintDisplayItemCacheEnabled()) { | 124 if (!RuntimeEnabledFeatures::slimmingPaintDisplayItemCacheEnabled()) { |
| 84 m_paintList.clear(); | 125 m_paintList.clear(); |
| 85 m_paintList.swap(m_newPaints); | 126 m_paintList.swap(m_newPaints); |
| 86 m_cachedClients.clear(); | 127 m_cachedClients.clear(); |
| 87 return; | 128 return; |
| 88 } | 129 } |
| 89 | 130 |
| 90 PaintList updatedList; | 131 PaintList updatedList; |
| 132 DisplayItemIndexMap newDisplayItemIndexMap; | |
| 91 HashSet<DisplayItemClient> newCachedClients; | 133 HashSet<DisplayItemClient> newCachedClients; |
| 92 | 134 |
| 93 PaintList::iterator paintListIt = m_paintList.begin(); | 135 for (OwnPtr<DisplayItem>& newDisplayItem : m_newPaints) { |
| 94 PaintList::iterator paintListEnd = m_paintList.end(); | 136 if (newDisplayItem->isSubtreeCached()) { |
|
chrishtr
2015/02/03 00:39:00
Subtree cached can be thought of as a type of cach
Xianzhu
2015/02/03 01:57:12
There seems little code to share by SubtreeCached
| |
| 137 copyCachedSubtree(*newDisplayItem, updatedList, newCachedClients, ne wDisplayItemIndexMap); | |
| 138 continue; | |
| 139 } | |
| 95 | 140 |
| 96 for (OwnPtr<DisplayItem>& newDisplayItem : m_newPaints) { | 141 if (newDisplayItem->isCached()) { |
| 97 PaintList::iterator cachedItemIt = findNextMatchingCachedItem(paintListI t, *newDisplayItem); | 142 size_t index = findMatchingCachedItem(*newDisplayItem); |
| 98 if (cachedItemIt != paintListEnd) { | 143 if (index == kNotFound) |
| 99 // Copy all of the existing items over until we hit the matching cac hed item. | 144 continue; |
| 100 for (; paintListIt != cachedItemIt; ++paintListIt) { | |
| 101 if (clientCacheIsValid((*paintListIt)->client())) | |
| 102 appendDisplayItem(updatedList, newCachedClients, paintListIt ->release()); | |
| 103 } | |
| 104 | 145 |
| 105 // Use the cached item for the new display item. | 146 // Use the cached item for the new display item. |
| 106 appendDisplayItem(updatedList, newCachedClients, cachedItemIt->relea se()); | 147 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 } | 148 } |
| 116 } | 149 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 } | 150 } |
| 123 | 151 |
| 124 m_newPaints.clear(); | 152 m_newPaints.clear(); |
| 125 m_paintList.clear(); | 153 m_paintList.clear(); |
| 126 m_paintList.swap(updatedList); | 154 m_paintList.swap(updatedList); |
| 155 m_displayItemIndexMap.clear(); | |
| 156 m_displayItemIndexMap.swap(newDisplayItemIndexMap); | |
| 127 m_cachedClients.clear(); | 157 m_cachedClients.clear(); |
| 128 m_cachedClients.swap(newCachedClients); | 158 m_cachedClients.swap(newCachedClients); |
| 159 #if ENABLE(ASSERT) | |
| 160 m_newPaintIds.clear(); | |
| 161 #endif | |
| 129 } | 162 } |
| 130 | 163 |
| 131 #ifndef NDEBUG | 164 #ifndef NDEBUG |
| 132 | 165 |
| 133 WTF::String DisplayItemList::paintListAsDebugString(const PaintList& list) const | 166 WTF::String DisplayItemList::paintListAsDebugString(const PaintList& list) const |
| 134 { | 167 { |
| 135 StringBuilder stringBuilder; | 168 StringBuilder stringBuilder; |
| 136 bool isFirst = true; | 169 for (size_t i = 0; i < list.size(); ++i) { |
| 137 for (auto& displayItem : list) { | 170 const OwnPtr<DisplayItem>& displayItem = list[i]; |
| 138 if (!isFirst) | 171 if (i) |
| 139 stringBuilder.append(",\n"); | 172 stringBuilder.append(",\n"); |
| 140 isFirst = false; | 173 if (!displayItem) { |
| 141 stringBuilder.append('{'); | 174 stringBuilder.append("null"); |
| 175 continue; | |
| 176 } | |
| 177 stringBuilder.append(String::format("{index: %d, ", (int)i)); | |
| 142 displayItem->dumpPropertiesAsDebugString(stringBuilder); | 178 displayItem->dumpPropertiesAsDebugString(stringBuilder); |
| 143 stringBuilder.append(", cacheIsValid: "); | 179 stringBuilder.append(", cacheIsValid: "); |
| 144 stringBuilder.append(clientCacheIsValid(displayItem->client()) ? "true" : "false"); | 180 stringBuilder.append(clientCacheIsValid(displayItem->client()) ? "true" : "false"); |
| 145 stringBuilder.append('}'); | 181 stringBuilder.append('}'); |
| 146 } | 182 } |
| 147 return stringBuilder.toString(); | 183 return stringBuilder.toString(); |
| 148 } | 184 } |
| 149 | 185 |
| 150 void DisplayItemList::showDebugData() const | 186 void DisplayItemList::showDebugData() const |
| 151 { | 187 { |
| 152 fprintf(stderr, "paint list: [%s]\n", paintListAsDebugString(m_paintList).ut f8().data()); | 188 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()); | 189 fprintf(stderr, "new paints: [%s]\n", paintListAsDebugString(m_newPaints).ut f8().data()); |
| 154 } | 190 } |
| 155 | 191 |
| 156 #endif | 192 #endif // ifndef NDEBUG |
| 157 | 193 |
| 158 void DisplayItemList::replay(GraphicsContext* context) | 194 void DisplayItemList::replay(GraphicsContext* context) |
| 159 { | 195 { |
| 160 updatePaintList(); | 196 updatePaintList(); |
| 161 for (auto& displayItem : m_paintList) | 197 for (auto& displayItem : m_paintList) |
| 162 displayItem->replay(context); | 198 displayItem->replay(context); |
| 163 } | 199 } |
| 164 | 200 |
| 165 } // namespace blink | 201 } // namespace blink |
| OLD | NEW |