layout/style/nsStyleContextInlines.h
author Manish Goregaokar <manishearth@gmail.com>
Thu, 20 Jul 2017 21:44:02 -0700
changeset 418962 cea58fa7bd2254ba70c05f382dc090600a1675f4
parent 418961 959eb43f301e89def3b535659f60ec6e2b3ba451
child 419078 c9ee2503c1202a05b7a0c9899fac00ccb64e34c0
permissions -rw-r--r--
Bug 1382017 part 4 Gecko piece - Rename ServoComputedValues -> ServoComputedData; r=heycam ServoComputedValues is confusing because ComputedValues is actually ServoStyleContext on the C++ side. MozReview-Commit-ID: IQNVdfREAMt

/* -*- 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/. */

/*
 * Inlined methods for nsStyleContext. Will just redirect to
 * GeckoStyleContext methods when compiled without stylo, but will do
 * virtual dispatch (by checking which kind of container it is)
 * in stylo mode.
 */

#ifndef nsStyleContextInlines_h
#define nsStyleContextInlines_h

#include "nsStyleContext.h"
#include "mozilla/ServoStyleContext.h"
#include "mozilla/GeckoStyleContext.h"
#include "mozilla/ServoUtils.h"
#include "mozilla/ServoBindings.h"

MOZ_DEFINE_STYLO_METHODS(nsStyleContext,
                         mozilla::GeckoStyleContext,
                         mozilla::ServoStyleContext);

nsRuleNode*
nsStyleContext::RuleNode()
{
    MOZ_RELEASE_ASSERT(IsGecko());
    return AsGecko()->RuleNode();
}

const ServoComputedData*
nsStyleContext::ComputedData()
{
    MOZ_RELEASE_ASSERT(IsServo());
    return AsServo()->ComputedData();
}

void
nsStyleContext::AddRef()
{
  MOZ_STYLO_FORWARD(AddRef, ())
}

void
nsStyleContext::Release()
{
  MOZ_STYLO_FORWARD(Release, ())
}

#define STYLE_STRUCT(name_, checkdata_cb_)                      \
const nsStyle##name_ *                                          \
nsStyleContext::Style##name_() {                                \
  return DoGetStyle##name_<true>();                             \
}                                                               \
const nsStyle##name_ *                                          \
nsStyleContext::ThreadsafeStyle##name_() {                      \
  if (mozilla::ServoStyleSet::IsInServoTraversal()) {           \
    return AsServo()->ComputedData()->GetStyle##name_();      \
  }                                                             \
  return Style##name_();                                        \
}                                                               \
const nsStyle##name_ * nsStyleContext::PeekStyle##name_() {     \
  return DoGetStyle##name_<false>();                            \
}
#include "nsStyleStructList.h"
#undef STYLE_STRUCT

// Helper functions for GetStyle* and PeekStyle*
#define STYLE_STRUCT_INHERITED(name_, checkdata_cb_)                \
template<bool aComputeData>                                         \
const nsStyle##name_ * nsStyleContext::DoGetStyle##name_() {        \
  if (auto gecko = GetAsGecko()) {                                  \
    const nsStyle##name_ * cachedData =                             \
      static_cast<nsStyle##name_*>(                                 \
        gecko->mCachedInheritedData                                 \
        .mStyleStructs[eStyleStruct_##name_]);                      \
    if (cachedData) /* Have it cached already, yay */               \
      return cachedData;                                            \
    if (!aComputeData) {                                            \
      /* We always cache inherited structs on the context when we */\
      /* compute them. */                                           \
      return nullptr;                                               \
    }                                                               \
    /* Have the rulenode deal */                                    \
    AUTO_CHECK_DEPENDENCY(gecko, eStyleStruct_##name_);             \
    const nsStyle##name_ * newData =                                \
      gecko->RuleNode()->                                           \
        GetStyle##name_<aComputeData>(this->AsGecko(), mBits);      \
    /* always cache inherited data on the style context; the rule */\
    /* node set the bit in mBits for us if needed. */               \
    gecko->mCachedInheritedData                                     \
      .mStyleStructs[eStyleStruct_##name_] =                        \
      const_cast<nsStyle##name_ *>(newData);                        \
    return newData;                                                 \
  }                                                                 \
  auto servo = AsServo();                                           \
  /**                                                               \
   * Also (conservatively) set the owning bit in the parent style   \
   * context if we're a text node.                                  \
   *                                                                \
   * This causes the parent element's style context to cache any    \
   * inherited structs we request for a text node, which means we   \
   * don't have to compute change hints for the text node, as       \
   * handling the change on the parent element is sufficient.       \
   *                                                                \
   * Note, however, that we still need to request the style struct  \
   * of the text node itself, since we may run some fixups on it,   \
   * like for text-combine.                                         \
   *                                                                \
   * This model is sound because for the fixed-up values to change, \
   * other properties on the parent need to change too, and we'll   \
   * handle those change hints correctly.                           \
   *                                                                \
   * TODO(emilio): Perhaps we should remove those fixups and handle \
   * those in layout instead. Those fixups are kind of expensive    \
   * for style sharing, and computed style of text nodes is not     \
   * observable. If we do that, we could assert here that the       \
   * inherited structs of both are the same.                        \
   */                                                               \
  if (mPseudoTag == nsCSSAnonBoxes::mozText && aComputeData) {      \
    MOZ_ASSERT(mParent);                                            \
    mParent->AddStyleBit(NS_STYLE_INHERIT_BIT(name_));              \
  }                                                                 \
                                                                    \
  const bool needToCompute = !(mBits & NS_STYLE_INHERIT_BIT(name_));\
  if (!aComputeData && needToCompute) {                             \
    return nullptr;                                                 \
  }                                                                 \
                                                                    \
  const nsStyle##name_* data =                                      \
    servo->ComputedData()->GetStyle##name_();                     \
  /* perform any remaining main thread work on the struct */        \
  if (needToCompute) {                                              \
    MOZ_ASSERT(NS_IsMainThread());                                  \
    MOZ_ASSERT(!mozilla::ServoStyleSet::IsInServoTraversal());      \
    const_cast<nsStyle##name_*>(data)->FinishStyle(PresContext());  \
    /* the ServoStyleContext owns the struct */                     \
    AddStyleBit(NS_STYLE_INHERIT_BIT(name_));                       \
  }                                                                 \
  return data;                                                      \
}

#define STYLE_STRUCT_RESET(name_, checkdata_cb_)                              \
template<bool aComputeData>                                                   \
const nsStyle##name_ * nsStyleContext::DoGetStyle##name_() {                  \
  if (auto gecko = GetAsGecko()) {                                            \
    if (gecko->mCachedResetData) {                                            \
      const nsStyle##name_ * cachedData =                                     \
        static_cast<nsStyle##name_*>(                                         \
          gecko->mCachedResetData->mStyleStructs[eStyleStruct_##name_]);      \
      if (cachedData) /* Have it cached already, yay */                       \
        return cachedData;                                                    \
    }                                                                         \
    /* Have the rulenode deal */                                              \
    AUTO_CHECK_DEPENDENCY(gecko, eStyleStruct_##name_);                              \
    return gecko->RuleNode()->GetStyle##name_<aComputeData>(this->AsGecko()); \
  }                                                                           \
  auto servo = AsServo();                                                     \
  const bool needToCompute = !(mBits & NS_STYLE_INHERIT_BIT(name_));          \
  if (!aComputeData && needToCompute) {                                       \
    return nullptr;                                                           \
  }                                                                           \
  const nsStyle##name_* data =                                                \
    servo->ComputedData()->GetStyle##name_();                               \
  /* perform any remaining main thread work on the struct */                  \
  if (needToCompute) {                                                        \
    const_cast<nsStyle##name_*>(data)->FinishStyle(PresContext());            \
    /* the ServoStyleContext owns the struct */                               \
    AddStyleBit(NS_STYLE_INHERIT_BIT(name_));                                 \
  }                                                                           \
  return data;                                                                \
}
#include "nsStyleStructList.h"
#undef STYLE_STRUCT_RESET
#undef STYLE_STRUCT_INHERITED


nsPresContext*
nsStyleContext::PresContext() const
{
    MOZ_STYLO_FORWARD(PresContext, ())
}


nsStyleContext*
nsStyleContext::GetStyleIfVisited() const
{
  MOZ_STYLO_FORWARD(GetStyleIfVisited, ())
}

mozilla::GeckoStyleContext*
nsStyleContext::GetParent() const
{
  MOZ_ASSERT(IsGecko(),
             "This should be used only in Gecko-backed style system!");
  if (mParent) {
    return mParent->AsGecko();
  } else {
    return nullptr;
  }
}

bool
nsStyleContext::IsLinkContext() const
{
  return GetStyleIfVisited() && GetStyleIfVisited()->GetParent() == GetParent();
}

void
nsStyleContext::StartBackgroundImageLoads()
{
  // Just get our background struct; that should do the trick
  StyleBackground();
}

#endif // nsStyleContextInlines_h