| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "core/paint/PaintTiming.h" | 6 #include "core/paint/PaintTiming.h" |
| 7 | 7 |
| 8 #include "core/dom/Document.h" | 8 #include "core/dom/Document.h" |
| 9 #include "core/loader/DocumentLoader.h" | 9 #include "core/loader/DocumentLoader.h" |
| 10 #include "platform/TraceEvent.h" | 10 #include "platform/TraceEvent.h" |
| 11 #include "wtf/RawPtr.h" | 11 #include "wtf/RawPtr.h" |
| 12 | 12 |
| 13 namespace blink { | 13 namespace blink { |
| 14 | 14 |
| 15 namespace { |
| 16 |
| 17 unsigned getRectHash(const IntRect& rect) |
| 18 { |
| 19 // Divide by 10 so similar rectangles are treated the same. |
| 20 unsigned positionHash = ((rect.x() / 10) << 16) + (rect.y() / 10); |
| 21 unsigned sizeHash = ((rect.width() / 10) << 16) + (rect.height() / 10); |
| 22 unsigned rectHash = positionHash ^ sizeHash; |
| 23 // HashSets do not allow null values. |
| 24 if (rectHash == 0) |
| 25 rectHash++; |
| 26 return rectHash; |
| 27 } |
| 28 |
| 29 } // namespace |
| 30 |
| 15 static const char kSupplementName[] = "PaintTiming"; | 31 static const char kSupplementName[] = "PaintTiming"; |
| 16 | 32 |
| 17 PaintTiming& PaintTiming::from(Document& document) | 33 PaintTiming& PaintTiming::from(Document& document) |
| 18 { | 34 { |
| 19 PaintTiming* timing = static_cast<PaintTiming*>(WillBeHeapSupplement<Documen
t>::from(document, kSupplementName)); | 35 PaintTiming* timing = static_cast<PaintTiming*>(WillBeHeapSupplement<Documen
t>::from(document, kSupplementName)); |
| 20 if (!timing) { | 36 if (!timing) { |
| 21 timing = new PaintTiming(document); | 37 timing = new PaintTiming(document); |
| 22 WillBeHeapSupplement<Document>::provideTo(document, kSupplementName, ado
ptPtrWillBeNoop(timing)); | 38 WillBeHeapSupplement<Document>::provideTo(document, kSupplementName, ado
ptPtrWillBeNoop(timing)); |
| 23 } | 39 } |
| 24 return *timing; | 40 return *timing; |
| 25 } | 41 } |
| 26 | 42 |
| 27 PaintTiming::PaintTiming(Document& document) | 43 PaintTiming::PaintTiming(Document& document) |
| 28 : m_document(document) | 44 : m_document(document) |
| 29 { | 45 { |
| 46 ASSERT(document.domWindow()); |
| 47 m_viewPortRect = IntRect(0, 0, document.domWindow()->innerWidth(), document.
domWindow()->innerHeight()); |
| 30 } | 48 } |
| 31 | 49 |
| 32 DEFINE_TRACE(PaintTiming) | 50 DEFINE_TRACE(PaintTiming) |
| 33 { | 51 { |
| 34 visitor->trace(m_document); | 52 visitor->trace(m_document); |
| 35 } | 53 } |
| 36 | 54 |
| 37 LocalFrame* PaintTiming::frame() const | 55 LocalFrame* PaintTiming::frame() const |
| 38 { | 56 { |
| 39 return m_document ? m_document->frame() : nullptr; | 57 return m_document ? m_document->frame() : nullptr; |
| (...skipping 19 matching lines...) Expand all Loading... |
| 59 notifyPaintTimingChanged(); | 77 notifyPaintTimingChanged(); |
| 60 } | 78 } |
| 61 | 79 |
| 62 void PaintTiming::markFirstImagePaint() | 80 void PaintTiming::markFirstImagePaint() |
| 63 { | 81 { |
| 64 m_firstImagePaint = monotonicallyIncreasingTime(); | 82 m_firstImagePaint = monotonicallyIncreasingTime(); |
| 65 TRACE_EVENT_MARK_WITH_TIMESTAMP1("blink.user_timing", "firstImagePaint", m_f
irstImagePaint, "frame", frame()); | 83 TRACE_EVENT_MARK_WITH_TIMESTAMP1("blink.user_timing", "firstImagePaint", m_f
irstImagePaint, "frame", frame()); |
| 66 notifyPaintTimingChanged(); | 84 notifyPaintTimingChanged(); |
| 67 } | 85 } |
| 68 | 86 |
| 87 void PaintTiming::markPaintInvalidation(const IntRect* visualRect, int paintCoun
t) |
| 88 { |
| 89 if (m_speedIndex || !m_loadCommitted) |
| 90 return; |
| 91 if (!frame() || !frame()->isMainFrame()) |
| 92 return; |
| 93 if (!visualRect->size().area() || !visualRect->intersects(m_viewPortRect)) |
| 94 return; |
| 95 |
| 96 IntRect intersect = intersection(*visualRect, m_viewPortRect); |
| 97 unsigned rectHash = getRectHash(intersect); |
| 98 unsigned long size = intersect.size().area(); |
| 99 double timestamp = monotonicallyIncreasingTime(); |
| 100 |
| 101 // Don't double count invalidation rects before any painting occurs. |
| 102 if (paintCount != m_lastPaintCount && frameRects.size()) |
| 103 frameRects.clear(); |
| 104 if (!frameRects.add(rectHash).isNewEntry) |
| 105 return; |
| 106 |
| 107 // Add the area under the curve that the previous invalidation rect |
| 108 // contributed. |
| 109 if (m_lastPaintInvalidationTime) { |
| 110 m_totalLoadedArea += (timestamp - m_lastPaintInvalidationTime) * m_total
Contributions; |
| 111 } |
| 112 m_lastPaintInvalidationTime = timestamp; |
| 113 |
| 114 // Adjust the size of the painted area based on how many times this rect has |
| 115 // been invalidated, as well as if it is a full sized invalidation. |
| 116 double adjustedSize = size; |
| 117 int count = ++(m_rectCounts.add(rectHash, 0).storedValue->value); |
| 118 adjustedSize /= count; |
| 119 if (size == m_viewPortRect.size().area()) |
| 120 adjustedSize /= 2.0; |
| 121 |
| 122 m_totalContributions += adjustedSize; |
| 123 m_lastPaintCount = paintCount; |
| 124 } |
| 125 |
| 126 double PaintTiming::speedIndex(double referenceTime) const |
| 127 { |
| 128 if (m_speedIndex) |
| 129 return m_speedIndex; |
| 130 if (!m_totalContributions) |
| 131 return 0.0; |
| 132 |
| 133 double totalArea = (m_lastPaintInvalidationTime - referenceTime) * m_totalCo
ntributions; |
| 134 m_speedIndex = (totalArea - m_totalLoadedArea) / m_totalContributions; |
| 135 |
| 136 // Unfortunate hack because we can mark invalidations before the first |
| 137 // paint in some circumstances. |
| 138 if (m_firstPaint) |
| 139 m_speedIndex = std::max(m_speedIndex, m_firstPaint - referenceTime); |
| 140 |
| 141 TRACE_EVENT2("blink.user_timing", "speedIndex", "value", m_speedIndex * 1000
.0, "frame", frame()); |
| 142 return m_speedIndex; |
| 143 } |
| 144 |
| 69 } // namespace blink | 145 } // namespace blink |
| OLD | NEW |