Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(22)

Side by Side Diff: third_party/WebKit/Source/core/dom/IntersectionObservation.cpp

Issue 2475643004: Monitor the intersection of video and viewport. (Closed)
Patch Set: Addressed comments. Observe intersection changing. Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 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 "core/dom/IntersectionObservation.h" 5 #include "core/dom/IntersectionObservation.h"
6 6
7 #include "core/dom/ElementRareData.h" 7 #include "core/dom/ElementRareData.h"
8 #include "core/dom/IntersectionObserver.h" 8 #include "core/dom/IntersectionObserver.h"
9 #include "core/frame/FrameView.h" 9 #include "core/frame/FrameView.h"
10 #include "core/frame/LocalFrame.h" 10 #include "core/frame/LocalFrame.h"
11 #include "core/layout/LayoutBox.h" 11 #include "core/layout/LayoutBox.h"
12 #include "core/layout/LayoutView.h" 12 #include "core/layout/LayoutView.h"
13 #include "core/paint/PaintLayer.h" 13 #include "core/paint/PaintLayer.h"
14 14
15 namespace blink { 15 namespace blink {
16 16
17 IntersectionObservation::IntersectionObservation(IntersectionObserver& observer, 17 IntersectionObservation::IntersectionObservation(IntersectionObserver& observer,
18 Element& target, 18 Element& target,
19 bool shouldReportRootBounds) 19 bool shouldReportRootBounds)
20 : m_observer(observer), 20 : m_observer(observer),
21 m_target(&target), 21 m_target(&target),
22 m_shouldReportRootBounds(shouldReportRootBounds), 22 m_shouldReportRootBounds(shouldReportRootBounds),
23 m_lastThresholdIndex(0) {} 23 m_lastThresholdIndex(0) {}
24 24
25 IntersectionObservation::~IntersectionObservation() {}
26
25 void IntersectionObservation::applyRootMargin(LayoutRect& rect) const { 27 void IntersectionObservation::applyRootMargin(LayoutRect& rect) const {
26 if (m_shouldReportRootBounds) 28 if (m_shouldReportRootBounds)
27 m_observer->applyRootMargin(rect); 29 m_observer->applyRootMargin(rect);
28 } 30 }
29 31
30 void IntersectionObservation::initializeGeometry( 32 void IntersectionObservation::initializeGeometry(
31 IntersectionGeometry& geometry) const { 33 IntersectionGeometry& geometry) const {
32 initializeTargetRect(geometry.targetRect); 34 initializeTargetRect(geometry.targetRect);
33 geometry.intersectionRect = geometry.targetRect; 35 geometry.intersectionRect = geometry.targetRect;
34 initializeRootRect(geometry.rootRect); 36 initializeRootRect(geometry.rootRect);
(...skipping 213 matching lines...) Expand 10 before | Expand all | Expand 10 after
248 if (m_target) 250 if (m_target)
249 target()->ensureIntersectionObserverData().removeObservation(observer()); 251 target()->ensureIntersectionObserverData().removeObservation(observer());
250 m_observer.clear(); 252 m_observer.clear();
251 } 253 }
252 254
253 DEFINE_TRACE(IntersectionObservation) { 255 DEFINE_TRACE(IntersectionObservation) {
254 visitor->trace(m_observer); 256 visitor->trace(m_observer);
255 visitor->trace(m_target); 257 visitor->trace(m_target);
256 } 258 }
257 259
260 ViewportIntersectionObservation::ViewportIntersectionObservation(
261 IntersectionObserver& observer,
262 Element& element)
263 : IntersectionObservation(observer, element, true),
264 m_lastIntersectRect(std::numeric_limits<float>::min(),
265 std::numeric_limits<float>::min(),
266 std::numeric_limits<float>::min(),
267 std::numeric_limits<float>::min()) {}
268
269 ViewportIntersectionObservation::~ViewportIntersectionObservation() {}
270
271 void ViewportIntersectionObservation::computeIntersectionObservations(
272 DOMHighResTimeStamp timestamp) {
273 IntersectionGeometry geometry;
274 if (!computeGeometry(geometry))
275 return;
276
277 float newVisibleRatio = 0;
miu 2016/11/15 23:29:26 Please remove |newVisibleRatio| and move the ratio
xjz 2016/11/16 01:06:45 Done.
278 LayoutRect newIntersectRect;
279 if (!geometry.rootRect.isEmpty() && geometry.doesIntersect) {
280 float intersectionArea =
281 geometry.intersectionRect.size().width().toFloat() *
282 geometry.intersectionRect.size().height().toFloat();
283 float rootArea = geometry.rootRect.size().width().toFloat() *
284 geometry.rootRect.size().height().toFloat();
285 newVisibleRatio = intersectionArea / rootArea;
286 newIntersectRect = geometry.intersectionRect;
287 }
288 if (newIntersectRect != m_lastIntersectRect) {
289 IntRect snappedRootBounds = pixelSnappedIntRect(geometry.rootRect);
290 IntersectionObserverEntry* newEntry = new IntersectionObserverEntry(
291 timestamp, newVisibleRatio, pixelSnappedIntRect(geometry.targetRect),
292 &snappedRootBounds, pixelSnappedIntRect(geometry.intersectionRect),
293 target());
294 observer().enqueueIntersectionObserverEntry(*newEntry);
295 m_lastIntersectRect = newIntersectRect;
296 }
297 }
298
258 } // namespace blink 299 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698