Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / Source / core / layout / HitTestCache.cpp
blob4c6fbe1512a6a881b529178e0887ac1e94158674
1 // Copyright (c) 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "config.h"
6 #include "core/layout/HitTestCache.h"
8 #include "public/platform/Platform.h"
10 namespace blink {
12 bool HitTestCache::lookupCachedResult(HitTestResult& hitResult, uint64_t domTreeVersion)
14 bool result = false;
15 HitHistogramMetric metric = HitHistogramMetric::MISS;
16 if (hitResult.hitTestRequest().avoidCache()) {
17 metric = HitHistogramMetric::MISS_EXPLICIT_AVOID;
18 // For now we don't support rect based hit results.
19 } else if (domTreeVersion == m_domTreeVersion && !hitResult.hitTestLocation().isRectBasedTest()) {
20 for (const auto& cachedItem : m_items) {
21 if (cachedItem.hitTestLocation().point() == hitResult.hitTestLocation().point()) {
22 if (hitResult.hitTestRequest().equalForCacheability(cachedItem.hitTestRequest())) {
23 metric = HitHistogramMetric::HIT_EXACT_MATCH;
24 result = true;
25 hitResult = cachedItem;
26 break;
28 metric = HitHistogramMetric::MISS_VALIDITY_RECT_MATCHES;
32 Platform::current()->histogramEnumeration("Event.HitTest", static_cast<int>(metric), static_cast<int>(HitHistogramMetric::MAX_HIT_METRIC));
33 return result;
36 void HitTestCache::addCachedResult(const HitTestResult& result, uint64_t domTreeVersion)
38 if (!result.isCacheable())
39 return;
41 // If the result was a hit test on an LayoutPart and the request allowed
42 // querying of the layout part; then the part hasn't been loaded yet.
43 if (result.isOverWidget() && result.hitTestRequest().allowsChildFrameContent())
44 return;
46 // For now don't support rect based or list based requests.
47 if (result.hitTestLocation().isRectBasedTest() || result.hitTestRequest().listBased())
48 return;
49 if (domTreeVersion != m_domTreeVersion)
50 clear();
51 if (m_items.size() < HIT_TEST_CACHE_SIZE)
52 m_items.resize(m_updateIndex + 1);
54 m_items.at(m_updateIndex).cacheValues(result);
55 m_domTreeVersion = domTreeVersion;
57 m_updateIndex++;
58 if (m_updateIndex >= HIT_TEST_CACHE_SIZE)
59 m_updateIndex = 0;
62 void HitTestCache::clear()
64 m_updateIndex = 0;
65 m_items.clear();
68 DEFINE_TRACE(HitTestCache)
70 visitor->trace(m_items);
73 } // namespace blink