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

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

Issue 2511143006: Detect change on the intersection of video and viewport. (Closed)
Patch Set: 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
(Empty)
1 // Copyright 2016 The Chromium Authors. All rights reserved.
miu 2016/11/21 21:07:03 FWICT, most of this code is moved from Intersectio
xjz 2016/11/21 23:58:42 Done. Most of the codes are originally from InterS
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "core/dom/IntersectionGeometry.h"
6
7 #include "core/dom/Element.h"
8 #include "core/dom/ElementRareData.h"
9 #include "core/frame/FrameView.h"
10 #include "core/frame/LocalFrame.h"
11 #include "core/layout/LayoutBox.h"
12 #include "core/layout/LayoutView.h"
13 #include "core/layout/api/LayoutAPIShim.h"
14 #include "core/layout/api/LayoutViewItem.h"
15 #include "core/paint/PaintLayer.h"
16
17 namespace blink {
18
19 namespace {
20
21 static bool isContainingBlockChainDescendant(LayoutObject* descendant,
22 LayoutObject* ancestor) {
23 LocalFrame* ancestorFrame = ancestor->document().frame();
24 LocalFrame* descendantFrame = descendant->document().frame();
25
26 if (ancestor->isLayoutView())
27 return descendantFrame && descendantFrame->tree().top() == ancestorFrame;
28
29 if (ancestorFrame != descendantFrame)
30 return false;
31
32 while (descendant && descendant != ancestor)
33 descendant = descendant->containingBlock();
34 return descendant;
35 }
36
37 static void mapRectUpToDocument(LayoutRect& rect,
38 const LayoutObject& layoutObject,
39 const Document& document) {
40 FloatQuad mappedQuad = layoutObject.localToAbsoluteQuad(
41 FloatQuad(FloatRect(rect)), UseTransforms | ApplyContainerFlip);
42 rect = LayoutRect(mappedQuad.boundingBox());
43 }
44
45 static void mapRectDownToDocument(LayoutRect& rect,
46 LayoutBoxModelObject& layoutObject,
47 const Document& document) {
48 FloatQuad mappedQuad = document.layoutView()->ancestorToLocalQuad(
49 &layoutObject, FloatQuad(FloatRect(rect)),
50 UseTransforms | ApplyContainerFlip | TraverseDocumentBoundaries);
51 rect = LayoutRect(mappedQuad.boundingBox());
52 }
53
54 static LayoutUnit computeMargin(const Length& length,
55 LayoutUnit referenceLength) {
56 if (length.type() == Percent) {
57 return LayoutUnit(
58 static_cast<int>(referenceLength.toFloat() * length.percent() / 100.0));
59 }
60 DCHECK_EQ(length.type(), Fixed);
61 return LayoutUnit(length.intValue());
62 }
63
64 } // namespace
65
66 IntersectionGeometry::IntersectionGeometry(Node* root,
67 Element* target,
68 const Vector<Length>& rootMargin,
69 bool shouldReportRootBounds)
70 : m_root(root),
71 m_target(target),
72 m_rootMargin(rootMargin),
73 m_shouldReportRootBounds(shouldReportRootBounds) {
74 DCHECK(m_root && m_target);
75 DCHECK(rootMargin.isEmpty() || rootMargin.size() == 4);
76 }
77
78 IntersectionGeometry::~IntersectionGeometry() {}
79
80 Element* IntersectionGeometry::root() const {
81 if (!m_root->isDocumentNode())
82 return toElement(m_root);
83 return nullptr;
84 }
85
86 LayoutObject* IntersectionGeometry::getRootLayoutObject() const {
87 if (m_root->isDocumentNode()) {
88 return LayoutAPIShim::layoutObjectFrom(
89 toDocument(m_root)->layoutViewItem());
90 }
91 return toElement(m_root)->layoutObject();
92 }
93
94 void IntersectionGeometry::initializeGeometry() {
95 initializeTargetRect();
96 m_intersectionRect = m_targetRect;
97 initializeRootRect();
98 m_doesIntersect = true;
99 }
100
101 void IntersectionGeometry::initializeTargetRect() {
102 LayoutObject* targetLayoutObject = m_target->layoutObject();
103 DCHECK(targetLayoutObject && targetLayoutObject->isBoxModelObject());
104 m_targetRect = LayoutRect(
105 toLayoutBoxModelObject(targetLayoutObject)->borderBoundingBox());
106 }
107
108 void IntersectionGeometry::initializeRootRect() {
109 LayoutObject* rootLayoutObject = getRootLayoutObject();
110 if (rootLayoutObject->isLayoutView()) {
111 m_rootRect = LayoutRect(
112 toLayoutView(rootLayoutObject)->frameView()->visibleContentRect());
113 } else if (rootLayoutObject->isBox() && rootLayoutObject->hasOverflowClip()) {
114 m_rootRect = LayoutRect(toLayoutBox(rootLayoutObject)->contentBoxRect());
115 } else {
116 m_rootRect = LayoutRect(
117 toLayoutBoxModelObject(rootLayoutObject)->borderBoundingBox());
118 }
119 applyRootMargin();
120 }
121
122 void IntersectionGeometry::applyRootMargin() {
123 if (m_rootMargin.isEmpty())
124 return;
125
126 // TODO(szager): Make sure the spec is clear that left/right margins are
127 // resolved against width and not height.
128 LayoutUnit topMargin = computeMargin(m_rootMargin[0], m_rootRect.height());
129 LayoutUnit rightMargin = computeMargin(m_rootMargin[1], m_rootRect.width());
130 LayoutUnit bottomMargin = computeMargin(m_rootMargin[2], m_rootRect.height());
131 LayoutUnit leftMargin = computeMargin(m_rootMargin[3], m_rootRect.width());
132
133 m_rootRect.setX(m_rootRect.x() - leftMargin);
134 m_rootRect.setWidth(m_rootRect.width() + leftMargin + rightMargin);
135 m_rootRect.setY(m_rootRect.y() - topMargin);
136 m_rootRect.setHeight(m_rootRect.height() + topMargin + bottomMargin);
137 }
138
139 void IntersectionGeometry::clipToRoot() {
140 // Map and clip rect into root element coordinates.
141 // TODO(szager): the writing mode flipping needs a test.
142 LayoutBox* rootLayoutObject = toLayoutBox(getRootLayoutObject());
143 LayoutObject* targetLayoutObject = m_target->layoutObject();
144
145 m_doesIntersect = targetLayoutObject->mapToVisualRectInAncestorSpace(
146 rootLayoutObject, m_intersectionRect, EdgeInclusive);
147 if (rootLayoutObject->hasOverflowClip())
148 m_intersectionRect.move(-rootLayoutObject->scrolledContentOffset());
149
150 if (!m_doesIntersect)
151 return;
152 LayoutRect rootClipRect(m_rootRect);
153 rootLayoutObject->flipForWritingMode(rootClipRect);
154 m_doesIntersect &= m_intersectionRect.inclusiveIntersect(rootClipRect);
155 }
156
157 void IntersectionGeometry::mapTargetRectToTargetFrameCoordinates() {
158 LayoutObject& targetLayoutObject = *m_target->layoutObject();
159 Document& targetDocument = m_target->document();
160 LayoutSize scrollPosition = LayoutSize(targetDocument.view()->scrollOffset());
161 mapRectUpToDocument(m_targetRect, targetLayoutObject, targetDocument);
162 m_targetRect.move(-scrollPosition);
163 }
164
165 void IntersectionGeometry::mapRootRectToRootFrameCoordinates() {
166 LayoutObject& rootLayoutObject = *getRootLayoutObject();
167 Document& rootDocument = rootLayoutObject.document();
168 LayoutSize scrollPosition = LayoutSize(rootDocument.view()->scrollOffset());
169 mapRectUpToDocument(m_intersectionRect, rootLayoutObject,
170 rootLayoutObject.document());
171 m_intersectionRect.move(-scrollPosition);
172 }
173
174 void IntersectionGeometry::mapRootRectToTargetFrameCoordinates() {
175 LayoutObject& rootLayoutObject = *getRootLayoutObject();
176 Document& targetDocument = m_target->document();
177 LayoutSize scrollPosition = LayoutSize(targetDocument.view()->scrollOffset());
178
179 if (&targetDocument == &rootLayoutObject.document()) {
180 mapRectUpToDocument(m_rootRect, rootLayoutObject, targetDocument);
181 } else {
182 mapRectDownToDocument(m_rootRect, toLayoutBoxModelObject(rootLayoutObject),
183 targetDocument);
184 }
185
186 m_rootRect.move(-scrollPosition);
187 }
188
189 void IntersectionGeometry::computeGeometry() {
190 // In the first few lines here, before initializeGeometry is called, "return
191 // true" effectively means "if the previous observed state was that root and
192 // target were intersecting, then generate a notification indicating that they
193 // are no longer intersecting." This happens, for example, when root or
194 // target is removed from the DOM tree and not reinserted before the next
195 // frame is generated, or display:none is set on the root or target.
196 if (!m_target->isConnected())
197 return;
198 Element* rootElement = root();
199 if (rootElement && !rootElement->isConnected())
200 return;
201
202 LayoutObject* rootLayoutObject = getRootLayoutObject();
203 if (!rootLayoutObject || !rootLayoutObject->isBoxModelObject())
204 return;
205 // TODO(szager): Support SVG
206 LayoutObject* targetLayoutObject = m_target->layoutObject();
207 if (!targetLayoutObject)
208 return;
209 if (!targetLayoutObject->isBoxModelObject() && !targetLayoutObject->isText())
210 return;
211 if (!isContainingBlockChainDescendant(targetLayoutObject, rootLayoutObject))
212 return;
213
214 initializeGeometry();
215
216 clipToRoot();
217
218 mapTargetRectToTargetFrameCoordinates();
219
220 if (m_doesIntersect)
221 mapRootRectToTargetFrameCoordinates();
222 else
223 m_intersectionRect = LayoutRect();
224
225 // Small optimization: if we're not going to report root bounds, don't bother
226 // transforming them to the frame.
227 if (m_shouldReportRootBounds)
228 mapRootRectToRootFrameCoordinates();
229 }
230
231 DEFINE_TRACE(IntersectionGeometry) {
232 visitor->trace(m_root);
233 visitor->trace(m_target);
234 }
235
236 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698