Commit 80f26da6 authored by szager's avatar szager Committed by Commit bot

Refactor IntersectionGeometry class and move it to core/layout.

IntersectionGeometry is an ephemeral object, so make it stack
allocated, let it use raw pointers to LayoutObject, and move it to
core/layout.

Also, be explicit about the implicit root by introducing
rootIsImplicit.

R=ojan@chromium.org,xjz@chromium.org
BUG=

Review-Url: https://codereview.chromium.org/2553103004
Cr-Commit-Position: refs/heads/master@{#437949}
parent 0cfffd46
......@@ -155,8 +155,6 @@ blink_core_sources("dom") {
"IgnoreDestructiveWriteCountIncrementer.h",
"IncrementLoadEventDelayCount.cpp",
"IncrementLoadEventDelayCount.h",
"IntersectionGeometry.cpp",
"IntersectionGeometry.h",
"IntersectionObservation.cpp",
"IntersectionObservation.h",
"IntersectionObserver.cpp",
......
......@@ -6,18 +6,16 @@
#include "core/dom/ElementRareData.h"
#include "core/dom/IntersectionObserver.h"
#include "core/layout/IntersectionGeometry.h"
namespace blink {
IntersectionObservation::IntersectionObservation(
IntersectionObserver& observer,
Element& target,
IntersectionGeometry::ReportRootBounds shouldReportRootBounds)
IntersectionObservation::IntersectionObservation(IntersectionObserver& observer,
Element& target,
bool shouldReportRootBounds)
: m_observer(observer),
m_target(&target),
m_shouldReportRootBounds(
shouldReportRootBounds ==
IntersectionGeometry::ReportRootBounds::kShouldReportRootBounds),
m_shouldReportRootBounds(shouldReportRootBounds),
m_lastThresholdIndex(0) {}
void IntersectionObservation::computeIntersectionObservations(
......@@ -29,11 +27,10 @@ void IntersectionObservation::computeIntersectionObservations(
rootMargin[1] = m_observer->rightMargin();
rootMargin[2] = m_observer->bottomMargin();
rootMargin[3] = m_observer->leftMargin();
Node* rootNode = m_observer->rootNode();
IntersectionGeometry geometry(
m_observer->rootNode(), target(), rootMargin,
m_shouldReportRootBounds
? IntersectionGeometry::ReportRootBounds::kShouldReportRootBounds
: IntersectionGeometry::ReportRootBounds::kShouldNotReportRootBounds);
rootNode && !rootNode->isDocumentNode() ? toElement(rootNode) : nullptr,
*target(), rootMargin, m_shouldReportRootBounds);
geometry.computeGeometry();
// Some corner cases for threshold index:
......
......@@ -6,7 +6,6 @@
#define IntersectionObservation_h
#include "core/dom/DOMHighResTimeStamp.h"
#include "core/dom/IntersectionGeometry.h"
#include "platform/heap/Handle.h"
namespace blink {
......@@ -17,10 +16,9 @@ class IntersectionObserver;
class IntersectionObservation final
: public GarbageCollected<IntersectionObservation> {
public:
IntersectionObservation(
IntersectionObserver&,
Element&,
IntersectionGeometry::ReportRootBounds shouldReportRootBounds);
IntersectionObservation(IntersectionObserver&,
Element&,
bool shouldReportRootBounds);
IntersectionObserver& observer() const { return *m_observer; }
Element* target() const { return m_target; }
......
......@@ -270,11 +270,8 @@ void IntersectionObserver::observe(Element* target,
isDOMDescendant = (targetFrame->tree().top() == rootFrame);
}
IntersectionObservation* observation = new IntersectionObservation(
*this, *target,
shouldReportRootBounds
? IntersectionGeometry::ReportRootBounds::kShouldReportRootBounds
: IntersectionGeometry::ReportRootBounds::kShouldNotReportRootBounds);
IntersectionObservation* observation =
new IntersectionObservation(*this, *target, shouldReportRootBounds);
target->ensureIntersectionObserverData().addObservation(*observation);
m_observations.add(observation);
......
......@@ -39,7 +39,6 @@
#include "core/dom/ElementTraversal.h"
#include "core/dom/ElementVisibilityObserver.h"
#include "core/dom/Fullscreen.h"
#include "core/dom/IntersectionGeometry.h"
#include "core/dom/TaskRunnerHelper.h"
#include "core/dom/shadow/ShadowRoot.h"
#include "core/events/Event.h"
......@@ -66,6 +65,7 @@
#include "core/html/track/VideoTrack.h"
#include "core/html/track/VideoTrackList.h"
#include "core/inspector/ConsoleMessage.h"
#include "core/layout/IntersectionGeometry.h"
#include "core/layout/LayoutMedia.h"
#include "core/layout/api/LayoutViewItem.h"
#include "core/layout/compositing/PaintLayerCompositor.h"
......@@ -4069,9 +4069,9 @@ DEFINE_TRACE(HTMLMediaElement::AudioSourceProviderImpl) {
void HTMLMediaElement::checkViewportIntersectionChanged() {
// TODO(xjz): Early return if we not in tab mirroring.
IntersectionGeometry geometry(
document().frame()->localFrameRoot()->document(), this, Vector<Length>(),
IntersectionGeometry::ReportRootBounds::kShouldReportRootBounds);
bool shouldReportRootBounds = true;
IntersectionGeometry geometry(nullptr, *this, Vector<Length>(),
shouldReportRootBounds);
geometry.computeGeometry();
IntRect intersectRect = geometry.intersectionIntRect();
if (m_currentIntersectRect == intersectRect)
......
......@@ -39,6 +39,8 @@ blink_core_sources("layout") {
"HitTestingTransformState.h",
"ImageQualityController.cpp",
"ImageQualityController.h",
"IntersectionGeometry.cpp",
"IntersectionGeometry.h",
"LayoutAnalyzer.cpp",
"LayoutAnalyzer.h",
"LayoutBR.cpp",
......
......@@ -12,28 +12,38 @@
namespace blink {
class Node;
class Element;
class LayoutObject;
class IntersectionGeometry final
: public GarbageCollectedFinalized<IntersectionGeometry> {
// Computes the intersection between an ancestor (root) element and a
// descendant (target) element, with overflow and CSS clipping applied, but not
// paint occlusion.
//
// If the root argument to the constructor is null, computes the intersection
// of the target with the top-level frame viewport (AKA the "implicit root").
class IntersectionGeometry {
STACK_ALLOCATED()
public:
enum ReportRootBounds {
kShouldReportRootBounds,
kShouldNotReportRootBounds,
};
IntersectionGeometry(Node* root,
Element* target,
IntersectionGeometry(Element* root,
Element& target,
const Vector<Length>& rootMargin,
ReportRootBounds shouldReportRootBounds);
bool shouldReportRootBounds);
~IntersectionGeometry();
void computeGeometry();
LayoutObject* root() const { return m_root; }
LayoutObject* target() const { return m_target; }
// Client rect in the coordinate system of the frame containing target.
LayoutRect targetRect() const { return m_targetRect; }
// Client rect in the coordinate system of the frame containing target.
LayoutRect intersectionRect() const { return m_intersectionRect; }
// Client rect in the coordinate system of the frame containing root.
LayoutRect rootRect() const { return m_rootRect; }
bool doesIntersect() const { return m_doesIntersect; }
IntRect intersectionIntRect() const {
......@@ -44,28 +54,33 @@ class IntersectionGeometry final
IntRect rootIntRect() const { return pixelSnappedIntRect(m_rootRect); }
DECLARE_TRACE();
private:
bool initializeCanComputeGeometry(Element* root, Element& target) const;
void initializeGeometry();
void initializeTargetRect();
void initializeRootRect();
void clipToRoot();
void mapTargetRectToTargetFrameCoordinates();
void mapRootRectToRootFrameCoordinates();
void mapRootRectToTargetFrameCoordinates();
Element* root() const;
LayoutObject* getRootLayoutObject() const;
void mapIntersectionRectToTargetFrameCoordinates();
void applyRootMargin();
Member<Node> m_root;
Member<Element> m_target;
// Returns true iff it's possible to compute an intersection between root
// and target.
bool canComputeGeometry() const { return m_canComputeGeometry; }
bool rootIsImplicit() const { return m_rootIsImplicit; }
bool shouldReportRootBounds() const { return m_shouldReportRootBounds; }
LayoutObject* m_root;
LayoutObject* m_target;
const Vector<Length> m_rootMargin;
const ReportRootBounds m_shouldReportRootBounds;
LayoutRect m_targetRect;
LayoutRect m_intersectionRect;
LayoutRect m_rootRect;
bool m_doesIntersect = false;
unsigned m_doesIntersect : 1;
const unsigned m_shouldReportRootBounds : 1;
const unsigned m_rootIsImplicit : 1;
const unsigned m_canComputeGeometry : 1;
};
} // namespace blink
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment