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. |
| 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() |
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(); |
| 61 m_displayItemIndexMap.clear(); |
44 } | 62 } |
45 | 63 |
46 bool DisplayItemList::clientCacheIsValid(DisplayItemClient client) const | 64 bool DisplayItemList::clientCacheIsValid(DisplayItemClient client) const |
47 { | 65 { |
48 return RuntimeEnabledFeatures::slimmingPaintDisplayItemCacheEnabled() && m_c
achedClients.contains(client); | 66 return RuntimeEnabledFeatures::slimmingPaintDisplayItemCacheEnabled() && m_c
achedClients.contains(client); |
49 } | 67 } |
50 | 68 |
51 PaintList::iterator DisplayItemList::findNextMatchingCachedItem(PaintList::itera
tor begin, const DisplayItem& displayItem) | 69 size_t DisplayItemList::findDisplayItemById(const DisplayItem::Id& id) const |
52 { | 70 { |
53 PaintList::iterator end = m_paintList.end(); | 71 ASSERT(clientCacheIsValid(id.client)); |
54 | 72 DisplayItemIndexMap::const_iterator it = m_displayItemIndexMap.find(id); |
55 if (!clientCacheIsValid(displayItem.client())) | 73 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 } | 74 } |
69 | 75 |
70 static void appendDisplayItem(PaintList& list, HashSet<DisplayItemClient>& clien
ts, WTF::PassOwnPtr<DisplayItem> displayItem) | 76 size_t DisplayItemList::findMatchingCachedItem(const DisplayItem& displayItem) c
onst |
71 { | 77 { |
72 clients.add(displayItem->client()); | 78 ASSERT(displayItem.isCached()); |
| 79 ASSERT(clientCacheIsValid(displayItem.client())); |
| 80 |
| 81 return findDisplayItemById(DisplayItem::Id(displayItem.client(), DisplayItem
::cachedTypeToDrawingType(displayItem.type()))); |
| 82 } |
| 83 |
| 84 void DisplayItemList::appendDisplayItem(PassOwnPtr<DisplayItem> displayItem, Pai
ntList& list, DisplayItemClientSet& cachedClients, DisplayItemIndexMap& displayI
temIndexMap) |
| 85 { |
| 86 const DisplayItem::Id& id = displayItem->id(); |
73 list.append(displayItem); | 87 list.append(displayItem); |
| 88 cachedClients.add(id.client); |
| 89 |
| 90 ASSERT(!displayItemIndexMap.contains(id)); |
| 91 displayItemIndexMap.add(id, list.size() - 1); |
| 92 } |
| 93 |
| 94 void DisplayItemList::copyCachedSubtree(const DisplayItem& displayItem, PaintLis
t& list, DisplayItemClientSet& cachedClients, DisplayItemIndexMap& displayItemIn
dexMap) |
| 95 { |
| 96 ASSERT(displayItem.isSubtreeCached()); |
| 97 ASSERT(clientCacheIsValid(displayItem.client())); |
| 98 |
| 99 size_t beginSubtreeIndex = findDisplayItemById(DisplayItem::Id(displayItem.c
lient(), DisplayItem::subtreeCachedTypeToBeginSubtreeType(displayItem.type()))); |
| 100 // The subtree previously produced no display items so was omitted. |
| 101 if (beginSubtreeIndex == kNotFound) |
| 102 return; |
| 103 |
| 104 size_t endSubtreeIndex = findDisplayItemById(DisplayItem::Id(displayItem.cli
ent(), DisplayItem::subtreeCachedTypeToEndSubtreeType(displayItem.type()))); |
| 105 ASSERT(endSubtreeIndex != kNotFound); |
| 106 |
| 107 for (size_t i = beginSubtreeIndex; i <= endSubtreeIndex; ++i) { |
| 108 if (clientCacheIsValid(m_paintList[i]->client())) |
| 109 appendDisplayItem(m_paintList[i].release(), list, cachedClients, dis
playItemIndexMap); |
| 110 } |
74 } | 111 } |
75 | 112 |
76 // Update the existing paintList by removing invalidated entries, updating | 113 // Update the existing paintList by removing invalidated entries, updating |
77 // repainted ones, and appending new items. | 114 // repainted ones, and appending new items. |
78 // | 115 // |
79 // The algorithm is O(|existing paint list| + |newly painted list|): by using | 116 // The algorithm is O(|existing paint list| + |newly painted list|): |
80 // the ordering implied by the existing paint list, extra treewalks are avoided. | 117 // - For CachedDisplayItem, copy the corresponding cached DrawingDisplayItem; |
| 118 // - For SubtreeCachedDisplayItem, copy the cached display items from the |
| 119 // corresponding BeginSubtreeDisplayItem and EndSubtreeDisplayItem; |
| 120 // - Otherwise, copy the new display item. |
81 void DisplayItemList::updatePaintList() | 121 void DisplayItemList::updatePaintList() |
82 { | 122 { |
83 if (!RuntimeEnabledFeatures::slimmingPaintDisplayItemCacheEnabled()) { | 123 if (!RuntimeEnabledFeatures::slimmingPaintDisplayItemCacheEnabled()) { |
84 m_paintList.clear(); | 124 m_paintList.clear(); |
85 m_paintList.swap(m_newPaints); | 125 m_paintList.swap(m_newPaints); |
86 m_cachedClients.clear(); | 126 m_cachedClients.clear(); |
87 return; | 127 return; |
88 } | 128 } |
89 | 129 |
90 PaintList updatedList; | 130 PaintList updatedList; |
| 131 DisplayItemIndexMap newDisplayItemIndexMap; |
91 HashSet<DisplayItemClient> newCachedClients; | 132 HashSet<DisplayItemClient> newCachedClients; |
92 | 133 |
93 PaintList::iterator paintListIt = m_paintList.begin(); | 134 for (OwnPtr<DisplayItem>& newDisplayItem : m_newPaints) { |
94 PaintList::iterator paintListEnd = m_paintList.end(); | 135 if (newDisplayItem->isSubtreeCached()) { |
| 136 copyCachedSubtree(*newDisplayItem, updatedList, newCachedClients, ne
wDisplayItemIndexMap); |
| 137 continue; |
| 138 } |
95 | 139 |
96 for (OwnPtr<DisplayItem>& newDisplayItem : m_newPaints) { | 140 if (newDisplayItem->isCached()) { |
97 PaintList::iterator cachedItemIt = findNextMatchingCachedItem(paintListI
t, *newDisplayItem); | 141 size_t index = findMatchingCachedItem(*newDisplayItem); |
98 if (cachedItemIt != paintListEnd) { | 142 // 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. | 143 if (index == kNotFound) |
100 for (; paintListIt != cachedItemIt; ++paintListIt) { | 144 continue; |
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 |