element_id.h 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. // Copyright 2019 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef CC_PAINT_ELEMENT_ID_H_
  5. #define CC_PAINT_ELEMENT_ID_H_
  6. #include <stddef.h>
  7. #include <cstdint>
  8. #include <functional>
  9. #include <iosfwd>
  10. #include <memory>
  11. #include <string>
  12. #include "base/check_op.h"
  13. #include "cc/paint/paint_export.h"
  14. namespace base {
  15. namespace trace_event {
  16. class TracedValue;
  17. } // namespace trace_event
  18. } // namespace base
  19. namespace cc {
  20. using ElementIdType = uint64_t;
  21. // Element ids are chosen by cc's clients and can be used as a stable identifier
  22. // across updates.
  23. //
  24. // Historically, the layer tree stored all compositing data but this has been
  25. // refactored over time into auxilliary structures such as property trees.
  26. //
  27. // In composited scrolling, Layers directly reference scroll tree nodes
  28. // (Layer::scroll_tree_index) but scroll tree nodes are being refactored to
  29. // reference stable element ids instead of layers. Scroll property nodes have
  30. // unique element ids that blink creates from scrollable areas (though this is
  31. // opaque to the compositor). This refactoring of scroll nodes keeping a
  32. // scrolling element id instead of a scrolling layer id allows for more general
  33. // compositing where, for example, multiple layers scroll with one scroll node.
  34. //
  35. // The animation system (see ElementAnimations) is another auxilliary structure
  36. // to the layer tree and uses element ids as a stable identifier for animation
  37. // targets. A Layer's element id can change over the Layer's lifetime because
  38. // non-default ElementIds are only set during an animation's lifetime.
  39. struct CC_PAINT_EXPORT ElementId {
  40. explicit ElementId(ElementIdType id) : id_(id) {
  41. DCHECK_NE(id, kInvalidElementId);
  42. }
  43. ElementId() : id_(kInvalidElementId) {}
  44. bool operator==(const ElementId& o) const { return id_ == o.id_; }
  45. bool operator!=(const ElementId& o) const { return !(*this == o); }
  46. bool operator<(const ElementId& o) const { return id_ < o.id_; }
  47. // Returns true if the ElementId has been initialized with a valid id.
  48. explicit operator bool() const { return !!id_; }
  49. void AddToTracedValue(base::trace_event::TracedValue* res) const;
  50. ElementIdType GetStableId() const;
  51. std::string ToString() const;
  52. static bool IsValid(ElementIdType id);
  53. // An ElementId that is reserved for custom property animation on paint
  54. // worklet element.
  55. static const ElementIdType kReservedElementId;
  56. private:
  57. friend struct ElementIdHash;
  58. static const ElementIdType kInvalidElementId;
  59. // The compositor treats this as an opaque handle and should not know how to
  60. // interpret these bits. Non-blink cc clients typically operate in terms of
  61. // layers and may set this value to match the client's layer id.
  62. ElementIdType id_;
  63. };
  64. ElementId CC_PAINT_EXPORT LayerIdToElementIdForTesting(int layer_id);
  65. struct CC_PAINT_EXPORT ElementIdHash {
  66. size_t operator()(ElementId key) const;
  67. };
  68. // Stream operator so ElementId can be used in assertion statements.
  69. CC_PAINT_EXPORT std::ostream& operator<<(std::ostream& out,
  70. const ElementId& id);
  71. } // namespace cc
  72. #endif // CC_PAINT_ELEMENT_ID_H_