layer_list_iterator.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. // Copyright 2016 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_LAYERS_LAYER_LIST_ITERATOR_H_
  5. #define CC_LAYERS_LAYER_LIST_ITERATOR_H_
  6. #include <stdlib.h>
  7. #include <vector>
  8. #include "base/memory/raw_ptr.h"
  9. #include "base/memory/raw_ptr_exclusion.h"
  10. #include "cc/cc_export.h"
  11. namespace cc {
  12. class Layer;
  13. // This visits a tree of layers in drawing order.
  14. class CC_EXPORT LayerListIterator {
  15. public:
  16. explicit LayerListIterator(Layer* root_layer);
  17. LayerListIterator(const LayerListIterator& other);
  18. ~LayerListIterator();
  19. bool operator==(const LayerListIterator& other) const {
  20. return current_layer_ == other.current_layer_;
  21. }
  22. bool operator!=(const LayerListIterator& other) const {
  23. return !(*this == other);
  24. }
  25. // We will only support prefix increment.
  26. LayerListIterator& operator++();
  27. Layer* operator->() const { return current_layer_; }
  28. Layer* operator*() const { return current_layer_; }
  29. private:
  30. // The implementation of this iterator is currently tied tightly to the layer
  31. // tree, but it should be straightforward to reimplement in terms of a list
  32. // when it's ready.
  33. // `current_layer` is not a raw_ptr<...> for performance reasons (based on
  34. // analysis of sampling profiler data and tab_search:top100:2020).
  35. RAW_PTR_EXCLUSION Layer* current_layer_;
  36. std::vector<size_t> list_indices_;
  37. };
  38. class CC_EXPORT LayerListConstIterator {
  39. public:
  40. explicit LayerListConstIterator(const Layer* root_layer);
  41. LayerListConstIterator(const LayerListConstIterator& other);
  42. ~LayerListConstIterator();
  43. bool operator==(const LayerListConstIterator& other) const {
  44. return current_layer_ == other.current_layer_;
  45. }
  46. bool operator!=(const LayerListConstIterator& other) const {
  47. return !(*this == other);
  48. }
  49. // We will only support prefix increment.
  50. LayerListConstIterator& operator++();
  51. const Layer* operator->() const { return current_layer_; }
  52. const Layer* operator*() const { return current_layer_; }
  53. private:
  54. raw_ptr<const Layer> current_layer_;
  55. std::vector<size_t> list_indices_;
  56. };
  57. class CC_EXPORT LayerListReverseIterator {
  58. public:
  59. explicit LayerListReverseIterator(Layer* root_layer);
  60. LayerListReverseIterator(const LayerListReverseIterator& other);
  61. ~LayerListReverseIterator();
  62. bool operator==(const LayerListReverseIterator& other) const {
  63. return current_layer_ == other.current_layer_;
  64. }
  65. bool operator!=(const LayerListReverseIterator& other) const {
  66. return !(*this == other);
  67. }
  68. // We will only support prefix increment.
  69. LayerListReverseIterator& operator++();
  70. Layer* operator->() const { return current_layer_; }
  71. Layer* operator*() const { return current_layer_; }
  72. private:
  73. void DescendToRightmostInSubtree();
  74. raw_ptr<Layer> current_layer_;
  75. std::vector<size_t> list_indices_;
  76. };
  77. class CC_EXPORT LayerListReverseConstIterator {
  78. public:
  79. explicit LayerListReverseConstIterator(const Layer* root_layer);
  80. LayerListReverseConstIterator(const LayerListReverseConstIterator& other);
  81. ~LayerListReverseConstIterator();
  82. bool operator==(const LayerListReverseConstIterator& other) const {
  83. return current_layer_ == other.current_layer_;
  84. }
  85. bool operator!=(const LayerListReverseConstIterator& other) const {
  86. return !(*this == other);
  87. }
  88. // We will only support prefix increment.
  89. LayerListReverseConstIterator& operator++();
  90. const Layer* operator->() const { return current_layer_; }
  91. const Layer* operator*() const { return current_layer_; }
  92. private:
  93. void DescendToRightmostInSubtree();
  94. raw_ptr<const Layer> current_layer_;
  95. std::vector<size_t> list_indices_;
  96. };
  97. } // namespace cc
  98. #endif // CC_LAYERS_LAYER_LIST_ITERATOR_H_