RuleNodeCacheConditions.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. /*
  6. * an object that stores the result of determining whether a style struct that
  7. * was computed can be cached in the rule tree, and if so, what the conditions
  8. * it relies on are
  9. */
  10. #ifndef RuleNodeCacheConditions_h_
  11. #define RuleNodeCacheConditions_h_
  12. #include "mozilla/Attributes.h"
  13. #include "nsCoord.h"
  14. #include "nsTArray.h"
  15. class nsStyleContext;
  16. namespace mozilla {
  17. /**
  18. * nsRuleNodeCacheConditions is used to store information about whether
  19. * we can store a style struct that we're computing in the rule tree.
  20. *
  21. * For inherited structs (i.e., structs with inherited properties), we
  22. * cache the struct in the rule tree if it does not depend on any data
  23. * in the style context tree, and otherwise store it in the style
  24. * context tree. This means that for inherited structs, setting any
  25. * conditions is equivalent to making the struct uncacheable.
  26. *
  27. * For reset structs (i.e., structs with non-inherited properties), we
  28. * are also able to cache structs in the rule tree conditionally on
  29. * certain common conditions. For these structs, setting conditions
  30. * (SetFontSizeDependency, SetWritingModeDependency) instead causes the
  31. * struct to be stored, with the condition, in the rule tree.
  32. */
  33. class RuleNodeCacheConditions
  34. {
  35. public:
  36. RuleNodeCacheConditions()
  37. : mFontSize(0), mBits(0) {}
  38. RuleNodeCacheConditions(const RuleNodeCacheConditions& aOther)
  39. : mFontSize(aOther.mFontSize), mBits(aOther.mBits) {}
  40. RuleNodeCacheConditions& operator=(const RuleNodeCacheConditions& aOther)
  41. {
  42. mFontSize = aOther.mFontSize;
  43. mBits = aOther.mBits;
  44. return *this;
  45. }
  46. bool operator==(const RuleNodeCacheConditions& aOther) const
  47. {
  48. return mFontSize == aOther.mFontSize &&
  49. mBits == aOther.mBits;
  50. }
  51. bool operator!=(const RuleNodeCacheConditions& aOther) const
  52. {
  53. return !(*this == aOther);
  54. }
  55. bool Matches(nsStyleContext* aStyleContext) const;
  56. /**
  57. * Record that the data being computed depend on the font-size
  58. * property of the element for which they are being computed.
  59. *
  60. * Note that we sometimes actually call this when there is a
  61. * dependency on the font-size property of the parent element, but we
  62. * only do so while computing inherited structs (nsStyleFont), and we
  63. * only store reset structs conditionally.
  64. */
  65. void SetFontSizeDependency(nscoord aCoord)
  66. {
  67. MOZ_ASSERT(!(mBits & eHaveFontSize) || mFontSize == aCoord);
  68. mFontSize = aCoord;
  69. mBits |= eHaveFontSize;
  70. }
  71. /**
  72. * Record that the data being computed depend on the writing mode of
  73. * the element for which they are being computed, which in turn
  74. * depends on its 'writing-mode', 'direction', and 'text-orientation'
  75. * properties.
  76. */
  77. void SetWritingModeDependency(uint8_t aWritingMode)
  78. {
  79. MOZ_ASSERT(!(mBits & eHaveWritingMode) || GetWritingMode() == aWritingMode);
  80. mBits |= (static_cast<uint64_t>(aWritingMode) << eWritingModeShift) |
  81. eHaveWritingMode;
  82. }
  83. void SetUncacheable()
  84. {
  85. mBits |= eUncacheable;
  86. }
  87. void Clear()
  88. {
  89. *this = RuleNodeCacheConditions();
  90. }
  91. bool Cacheable() const
  92. {
  93. return !(mBits & eUncacheable);
  94. }
  95. bool CacheableWithDependencies() const
  96. {
  97. return !(mBits & eUncacheable) &&
  98. (mBits & eHaveBitsMask) != 0;
  99. }
  100. bool CacheableWithoutDependencies() const
  101. {
  102. // We're not uncacheable and we have don't have a font-size or
  103. // writing mode value.
  104. return (mBits & eHaveBitsMask) == 0;
  105. }
  106. #ifdef DEBUG
  107. void List() const;
  108. #endif
  109. private:
  110. enum {
  111. eUncacheable = 0x0001,
  112. eHaveFontSize = 0x0002,
  113. eHaveWritingMode = 0x0004,
  114. eHaveBitsMask = 0x00ff,
  115. eWritingModeMask = 0xff00,
  116. eWritingModeShift = 8,
  117. };
  118. uint8_t GetWritingMode() const
  119. {
  120. return static_cast<uint8_t>(
  121. (mBits & eWritingModeMask) >> eWritingModeShift);
  122. }
  123. // The font size from which em units are derived.
  124. nscoord mFontSize;
  125. // Values in mBits:
  126. // bit 0: are we set to "uncacheable"?
  127. // bit 1: do we have a font size value?
  128. // bit 2: do we have a writing mode value?
  129. // bits 3-7: unused
  130. // bits 8-15: writing mode (uint8_t)
  131. // bits 16-31: unused
  132. uint32_t mBits;
  133. };
  134. } // namespace mozilla
  135. #endif // !defined(RuleNodeCacheConditions_h_)