gfx/layers/wr/HitTestInfoManager.cpp
author Ms2ger <Ms2ger@gmail.com>
Wed, 22 Sep 2021 14:01:21 +0000
changeset 592934 4eb8f7b78caa13d5bc8ce33e94a691c4d0d0f9a6
parent 585397 ed43eb0ae40d63cfc6682b19591ebb92179390e2
permissions -rw-r--r--
Bug 1732030 - Fix expectations for WebAssembly.Function tests; r=rhunt Differential Revision: https://phabricator.services.mozilla.com/D126351

/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* 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 "HitTestInfoManager.h"
#include "HitTestInfo.h"

#include "nsDisplayList.h"

#define DEBUG_HITTEST_INFO 0
#if DEBUG_HITTEST_INFO
#  define HITTEST_INFO_LOG(...) printf_stderr(__VA_ARGS__)
#else
#  define HITTEST_INFO_LOG(...)
#endif

namespace mozilla::layers {

using ViewID = ScrollableLayerGuid::ViewID;

/**
 * TODO(miko): This used to be a performance bottle-neck, but it does not show
 * up in profiles anymore, see bugs 1424637 and 1424968.
 * A better way of doing this would be to store current app units per dev pixel
 * in wr::DisplayListBuilder, and update it whenever display items that separate
 * presshell boundaries are encountered.
 */
static int32_t GetAppUnitsFromDisplayItem(nsDisplayItem* aItem) {
  nsIFrame* frame = aItem->Frame();
  MOZ_ASSERT(frame);
  return frame->PresContext()->AppUnitsPerDevPixel();
}

static void CreateWebRenderCommands(wr::DisplayListBuilder& aBuilder,
                                    nsDisplayItem* aItem, const nsRect& aArea,
                                    const gfx::CompositorHitTestInfo& aFlags,
                                    const ViewID& aViewId) {
  const Maybe<SideBits> sideBits =
      aBuilder.GetContainingFixedPosSideBits(aItem->GetActiveScrolledRoot());

  const LayoutDeviceRect devRect =
      LayoutDeviceRect::FromAppUnits(aArea, GetAppUnitsFromDisplayItem(aItem));
  const wr::LayoutRect rect = wr::ToLayoutRect(devRect);

  aBuilder.PushHitTest(rect, rect, !aItem->BackfaceIsHidden(), aViewId, aFlags,
                       sideBits.valueOr(SideBits::eNone));
}

HitTestInfoManager::HitTestInfoManager()
    : mArea(nsRect()),
      mFlags(gfx::CompositorHitTestInvisibleToHit),
      mViewId(ScrollableLayerGuid::NULL_SCROLL_ID),
      mSpaceAndClipChain(wr::InvalidScrollNodeWithChain()) {}

void HitTestInfoManager::Reset() {
  mArea = nsRect();
  mFlags = gfx::CompositorHitTestInvisibleToHit;
  mViewId = ScrollableLayerGuid::NULL_SCROLL_ID;
  mSpaceAndClipChain = wr::InvalidScrollNodeWithChain();

  HITTEST_INFO_LOG("* HitTestInfoManager::Reset\n");
}

void HitTestInfoManager::ProcessItem(
    nsDisplayItem* aItem, wr::DisplayListBuilder& aBuilder,
    nsDisplayListBuilder* aDisplayListBuilder) {
  MOZ_ASSERT(aItem);

  if (!aItem->HasHitTestInfo()) {
    return;
  }

  const HitTestInfo& hitTestInfo = aItem->GetHitTestInfo();
  const nsRect& area = hitTestInfo.Area();
  const gfx::CompositorHitTestInfo& flags = hitTestInfo.Info();

  if (flags == gfx::CompositorHitTestInvisibleToHit || area.IsEmpty()) {
    return;
  }

  const auto viewId =
      hitTestInfo.GetViewId(aBuilder, aItem->GetActiveScrolledRoot());
  const auto spaceAndClipChain = aBuilder.CurrentSpaceAndClipChain();

  if (!Update(area, flags, viewId, spaceAndClipChain)) {
    // The previous hit test information is still valid.
    return;
  }

  HITTEST_INFO_LOG("+ [%d, %d, %d, %d]: flags: 0x%x, viewId: %llu\n", area.x,
                   area.y, area.width, area.height, flags.serialize(), viewId);

  CreateWebRenderCommands(aBuilder, aItem, area, flags, viewId);
}

/**
 * Updates the current hit testing information if necessary.
 * Returns true if the the hit testing information was changed.
 */
bool HitTestInfoManager::Update(const nsRect& aArea,
                                const gfx::CompositorHitTestInfo& aFlags,
                                const ViewID& aViewId,
                                const wr::WrSpaceAndClipChain& aSpaceAndClip) {
  if (mViewId == aViewId && mFlags == aFlags && mArea.Contains(aArea) &&
      mSpaceAndClipChain == aSpaceAndClip) {
    // The previous hit testing information can be reused.
    HITTEST_INFO_LOG("s [%d, %d, %d, %d]: flags: 0x%x, viewId: %llu\n", aArea.x,
                     aArea.y, aArea.width, aArea.height, aFlags.serialize(),
                     aViewId);
    return false;
  }

  mArea = aArea;
  mFlags = aFlags;
  mViewId = aViewId;
  mSpaceAndClipChain = aSpaceAndClip;
  return true;
}

}  // namespace mozilla::layers