DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Header

Mercurial (52f3d1a5b791)

VCS Links

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "HitTestingTreeNode.h"

#include "AsyncPanZoomController.h"   // for AsyncPanZoomController
#include "LayersLogging.h"            // for Stringify
#include "mozilla/StaticPrefs.h"
#include "mozilla/gfx/Point.h"        // for Point4D
#include "mozilla/layers/APZUtils.h"  // for CompleteAsyncTransform
#include "mozilla/layers/AsyncCompositionManager.h"  // for ViewTransform::operator Matrix4x4()
#include "mozilla/layers/AsyncDragMetrics.h"  // for AsyncDragMetrics
#include "nsPrintfCString.h"                  // for nsPrintfCString
#include "UnitTransforms.h"                   // for ViewAs

namespace mozilla {
namespace layers {

using gfx::CompositorHitTestFlags;
using gfx::CompositorHitTestInfo;
using gfx::CompositorHitTestInvisibleToHit;
using gfx::CompositorHitTestTouchActionMask;

HitTestingTreeNode::HitTestingTreeNode(AsyncPanZoomController* aApzc,
                                       bool aIsPrimaryHolder,
                                       LayersId aLayersId)
    : mApzc(aApzc),
      mIsPrimaryApzcHolder(aIsPrimaryHolder),
      mLockCount(0),
      mLayersId(aLayersId),
      mFixedPosTarget(ScrollableLayerGuid::NULL_SCROLL_ID),
      mIsBackfaceHidden(false),
      mIsAsyncZoomContainer(false),
      mOverride(EventRegionsOverride::NoOverride) {
  if (mIsPrimaryApzcHolder) {
    MOZ_ASSERT(mApzc);
  }
  MOZ_ASSERT(!mApzc || mApzc->GetLayersId() == mLayersId);
}

void HitTestingTreeNode::RecycleWith(
    const RecursiveMutexAutoLock& aProofOfTreeLock,
    AsyncPanZoomController* aApzc, LayersId aLayersId) {
  MOZ_ASSERT(IsRecyclable(aProofOfTreeLock));
  Destroy();  // clear out tree pointers
  mApzc = aApzc;
  mLayersId = aLayersId;
  MOZ_ASSERT(!mApzc || mApzc->GetLayersId() == mLayersId);
  // The caller is expected to call SetHitTestData to repopulate the hit-test
  // fields.
}

HitTestingTreeNode::~HitTestingTreeNode() = default;

void HitTestingTreeNode::Destroy() {
  // This runs on the updater thread, it's not worth passing around extra raw
  // pointers just to assert it.

  mPrevSibling = nullptr;
  mLastChild = nullptr;
  mParent = nullptr;

  if (mApzc) {
    if (mIsPrimaryApzcHolder) {
      mApzc->Destroy();
    }
    mApzc = nullptr;
  }
}

bool HitTestingTreeNode::IsRecyclable(
    const RecursiveMutexAutoLock& aProofOfTreeLock) {
  return !(IsPrimaryHolder() || (mLockCount > 0));
}

void HitTestingTreeNode::SetLastChild(HitTestingTreeNode* aChild) {
  mLastChild = aChild;
  if (aChild) {
    aChild->mParent = this;

    if (aChild->GetApzc()) {
      AsyncPanZoomController* parent = GetNearestContainingApzc();
      // We assume that HitTestingTreeNodes with an ancestor/descendant
      // relationship cannot both point to the same APZC instance. This
      // assertion only covers a subset of cases in which that might occur,
      // but it's better than nothing.
      MOZ_ASSERT(aChild->GetApzc() != parent);
      aChild->SetApzcParent(parent);
    }
  }
}

void HitTestingTreeNode::SetScrollbarData(
    const Maybe<uint64_t>& aScrollbarAnimationId,
    const ScrollbarData& aScrollbarData) {
  mScrollbarAnimationId = aScrollbarAnimationId;
  mScrollbarData = aScrollbarData;
}

bool HitTestingTreeNode::MatchesScrollDragMetrics(
    const AsyncDragMetrics& aDragMetrics) const {
  return IsScrollThumbNode() &&
         mScrollbarData.mDirection == aDragMetrics.mDirection &&
         mScrollbarData.mTargetViewId == aDragMetrics.mViewId;
}

bool HitTestingTreeNode::IsScrollThumbNode() const {
  return mScrollbarData.mScrollbarLayerType ==
         layers::ScrollbarLayerType::Thumb;
}

bool HitTestingTreeNode::IsScrollbarNode() const {
  return mScrollbarData.mScrollbarLayerType != layers::ScrollbarLayerType::None;
}

ScrollDirection HitTestingTreeNode::GetScrollbarDirection() const {
  MOZ_ASSERT(IsScrollbarNode());
  MOZ_ASSERT(mScrollbarData.mDirection.isSome());
  return *mScrollbarData.mDirection;
}

ScrollableLayerGuid::ViewID HitTestingTreeNode::GetScrollTargetId() const {
  return mScrollbarData.mTargetViewId;
}

Maybe<uint64_t> HitTestingTreeNode::GetScrollbarAnimationId() const {
  return mScrollbarAnimationId;
}

const ScrollbarData& HitTestingTreeNode::GetScrollbarData() const {
  return mScrollbarData;
}

void HitTestingTreeNode::SetFixedPosData(
    ScrollableLayerGuid::ViewID aFixedPosTarget) {
  mFixedPosTarget = aFixedPosTarget;
}

ScrollableLayerGuid::ViewID HitTestingTreeNode::GetFixedPosTarget() const {
  return mFixedPosTarget;
}

void HitTestingTreeNode::SetPrevSibling(HitTestingTreeNode* aSibling) {
  mPrevSibling = aSibling;
  if (aSibling) {
    aSibling->mParent = mParent;

    if (aSibling->GetApzc()) {
      AsyncPanZoomController* parent =
          mParent ? mParent->GetNearestContainingApzc() : nullptr;
      aSibling->SetApzcParent(parent);
    }
  }
}

void HitTestingTreeNode::MakeRoot() {
  mParent = nullptr;

  if (GetApzc()) {
    SetApzcParent(nullptr);
  }
}

HitTestingTreeNode* HitTestingTreeNode::GetFirstChild() const {
  HitTestingTreeNode* child = GetLastChild();
  while (child && child->GetPrevSibling()) {
    child = child->GetPrevSibling();
  }
  return child;
}

HitTestingTreeNode* HitTestingTreeNode::GetLastChild() const {
  return mLastChild;
}

HitTestingTreeNode* HitTestingTreeNode::GetPrevSibling() const {
  return mPrevSibling;
}

HitTestingTreeNode* HitTestingTreeNode::GetParent() const { return mParent; }

bool HitTestingTreeNode::IsAncestorOf(const HitTestingTreeNode* aOther) const {
  for (const HitTestingTreeNode* cur = aOther; cur; cur = cur->GetParent()) {
    if (cur == this) {
      return true;
    }
  }
  return false;
}

AsyncPanZoomController* HitTestingTreeNode::GetApzc() const { return mApzc; }

AsyncPanZoomController* HitTestingTreeNode::GetNearestContainingApzc() const {
  for (const HitTestingTreeNode* n = this; n; n = n->GetParent()) {
    if (n->GetApzc()) {
      return n->GetApzc();
    }
  }
  return nullptr;
}

bool HitTestingTreeNode::IsPrimaryHolder() const {
  return mIsPrimaryApzcHolder;
}

LayersId HitTestingTreeNode::GetLayersId() const { return mLayersId; }

void HitTestingTreeNode::SetHitTestData(
    const EventRegions& aRegions, const LayerIntRegion& aVisibleRegion,
    const CSSTransformMatrix& aTransform,
    const Maybe<ParentLayerIntRegion>& aClipRegion,
    const EventRegionsOverride& aOverride, bool aIsBackfaceHidden,
    bool aIsAsyncZoomContainer) {
  mEventRegions = aRegions;
  mVisibleRegion = aVisibleRegion;
  mTransform = aTransform;
  mClipRegion = aClipRegion;
  mOverride = aOverride;
  mIsBackfaceHidden = aIsBackfaceHidden;
  mIsAsyncZoomContainer = aIsAsyncZoomContainer;
}

bool HitTestingTreeNode::IsOutsideClip(const ParentLayerPoint& aPoint) const {
  // test against clip rect in ParentLayer coordinate space
  return (mClipRegion.isSome() && !mClipRegion->Contains(aPoint.x, aPoint.y));
}

Maybe<LayerPoint> HitTestingTreeNode::Untransform(
    const ParentLayerPoint& aPoint,
    const LayerToParentLayerMatrix4x4& aTransform) const {
  Maybe<ParentLayerToLayerMatrix4x4> inverse = aTransform.MaybeInverse();
  if (inverse) {
    return UntransformBy(inverse.ref(), aPoint);
  }
  return Nothing();
}

CompositorHitTestInfo HitTestingTreeNode::HitTest(
    const LayerPoint& aPoint) const {
  CompositorHitTestInfo result = CompositorHitTestInvisibleToHit;

  if (mOverride & EventRegionsOverride::ForceEmptyHitRegion) {
    return result;
  }

  auto point = LayerIntPoint::Round(aPoint);

  // If the layer's backface is showing and it's hidden, don't hit it.
  // This matches the behavior of main-thread hit testing in
  // nsDisplayTransform::HitTest().
  if (mIsBackfaceHidden) {
    return result;
  }

  // test against event regions in Layer coordinate space
  if (!mEventRegions.mHitRegion.Contains(point.x, point.y)) {
    return result;
  }

  result = CompositorHitTestFlags::eVisibleToHitTest;

  if (mOverride & EventRegionsOverride::ForceDispatchToContent) {
    result += CompositorHitTestFlags::eApzAwareListeners;
  }
  if (mEventRegions.mDispatchToContentHitRegion.Contains(point.x, point.y)) {
    // Technically this might be some combination of eInactiveScrollframe,
    // eApzAwareListeners, and eIrregularArea, because the round-trip through
    // mEventRegions is lossy. We just convert it back to eIrregularArea
    // because that's the most conservative option (i.e. eIrregularArea makes
    // APZ rely on the main thread for everything).
    result += CompositorHitTestFlags::eIrregularArea;
    if (mEventRegions.mDTCRequiresTargetConfirmation) {
      result += CompositorHitTestFlags::eRequiresTargetConfirmation;
    }
  } else if (StaticPrefs::layout_css_touch_action_enabled()) {
    if (mEventRegions.mNoActionRegion.Contains(point.x, point.y)) {
      // set all the touch-action flags as disabled
      result += CompositorHitTestTouchActionMask;
    } else {
      bool panX = mEventRegions.mHorizontalPanRegion.Contains(point.x, point.y);
      bool panY = mEventRegions.mVerticalPanRegion.Contains(point.x, point.y);
      if (panX && panY) {
        // touch-action: pan-x pan-y
        result += CompositorHitTestFlags::eTouchActionDoubleTapZoomDisabled;
        result += CompositorHitTestFlags::eTouchActionPinchZoomDisabled;
      } else if (panX) {
        // touch-action: pan-x
        result += CompositorHitTestFlags::eTouchActionPanYDisabled;
        result += CompositorHitTestFlags::eTouchActionPinchZoomDisabled;
        result += CompositorHitTestFlags::eTouchActionDoubleTapZoomDisabled;
      } else if (panY) {
        // touch-action: pan-y
        result += CompositorHitTestFlags::eTouchActionPanXDisabled;
        result += CompositorHitTestFlags::eTouchActionPinchZoomDisabled;
        result += CompositorHitTestFlags::eTouchActionDoubleTapZoomDisabled;
      }  // else we're in the touch-action: auto or touch-action: manipulation
         // cases and we'll allow all actions. Technically we shouldn't allow
         // double-tap zooming in the manipulation case but apparently this has
         // been broken since the dawn of time.
    }
  }

  // The scrollbar flags are set at the call site in GetAPZCAtPoint, because
  // those require walking up the tree to see if we are contained inside a
  // scrollbar or scrollthumb, and we do that there anyway to get the scrollbar
  // node.

  return result;
}

EventRegionsOverride HitTestingTreeNode::GetEventRegionsOverride() const {
  return mOverride;
}

const CSSTransformMatrix& HitTestingTreeNode::GetTransform() const {
  return mTransform;
}

LayerToScreenMatrix4x4 HitTestingTreeNode::GetCSSTransformToRoot() const {
  if (mParent) {
    LayerToParentLayerMatrix4x4 thisToParent =
        mTransform * AsyncTransformMatrix();
    ParentLayerToScreenMatrix4x4 parentToRoot =
        ViewAs<ParentLayerToScreenMatrix4x4>(
            mParent->GetCSSTransformToRoot(),
            PixelCastJustification::MovingDownToChildren);
    return thisToParent * parentToRoot;
  }

  return ViewAs<LayerToScreenMatrix4x4>(
      mTransform * AsyncTransformMatrix(),
      PixelCastJustification::ScreenIsParentLayerForRoot);
}

const LayerIntRegion& HitTestingTreeNode::GetVisibleRegion() const {
  return mVisibleRegion;
}

bool HitTestingTreeNode::IsAsyncZoomContainer() const {
  return mIsAsyncZoomContainer;
}

void HitTestingTreeNode::Dump(const char* aPrefix) const {
  if (mPrevSibling) {
    mPrevSibling->Dump(aPrefix);
  }
  printf_stderr(
      "%sHitTestingTreeNode (%p) APZC (%p) g=(%s) %s%s%sr=(%s) t=(%s) "
      "c=(%s)%s%s\n",
      aPrefix, this, mApzc.get(),
      mApzc ? Stringify(mApzc->GetGuid()).c_str()
            : nsPrintfCString("l=0x%" PRIx64, uint64_t(mLayersId)).get(),
      (mOverride & EventRegionsOverride::ForceDispatchToContent) ? "fdtc " : "",
      (mOverride & EventRegionsOverride::ForceEmptyHitRegion) ? "fehr " : "",
      (mFixedPosTarget != ScrollableLayerGuid::NULL_SCROLL_ID)
          ? nsPrintfCString("fixed=%" PRIu64 " ", mFixedPosTarget).get()
          : "",
      Stringify(mEventRegions).c_str(), Stringify(mTransform).c_str(),
      mClipRegion ? Stringify(mClipRegion.ref()).c_str() : "none",
      mScrollbarData.mDirection.isSome() ? " scrollbar" : "",
      IsScrollThumbNode() ? " scrollthumb" : "");
  if (mLastChild) {
    mLastChild->Dump(nsPrintfCString("%s  ", aPrefix).get());
  }
}

void HitTestingTreeNode::SetApzcParent(AsyncPanZoomController* aParent) {
  // precondition: GetApzc() is non-null
  MOZ_ASSERT(GetApzc() != nullptr);
  if (IsPrimaryHolder()) {
    GetApzc()->SetParent(aParent);
  } else {
    MOZ_ASSERT(GetApzc()->GetParent() == aParent);
  }
}

void HitTestingTreeNode::Lock(const RecursiveMutexAutoLock& aProofOfTreeLock) {
  mLockCount++;
}

void HitTestingTreeNode::Unlock(
    const RecursiveMutexAutoLock& aProofOfTreeLock) {
  MOZ_ASSERT(mLockCount > 0);
  mLockCount--;
}

HitTestingTreeNodeAutoLock::HitTestingTreeNodeAutoLock()
    : mTreeMutex(nullptr) {}

HitTestingTreeNodeAutoLock::~HitTestingTreeNodeAutoLock() { Clear(); }

void HitTestingTreeNodeAutoLock::Initialize(
    const RecursiveMutexAutoLock& aProofOfTreeLock,
    already_AddRefed<HitTestingTreeNode> aNode, RecursiveMutex& aTreeMutex) {
  MOZ_ASSERT(!mNode);

  mNode = aNode;
  mTreeMutex = &aTreeMutex;

  mNode->Lock(aProofOfTreeLock);
}

void HitTestingTreeNodeAutoLock::Clear() {
  if (!mNode) {
    return;
  }
  MOZ_ASSERT(mTreeMutex);

  {  // scope lock
    RecursiveMutexAutoLock lock(*mTreeMutex);
    mNode->Unlock(lock);
  }
  mNode = nullptr;
  mTreeMutex = nullptr;
}

}  // namespace layers
}  // namespace mozilla