ShadowTree.h 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*
  2. * Copyright (c) Facebook, Inc. and its affiliates.
  3. *
  4. * This source code is licensed under the MIT license found in the
  5. * LICENSE file in the root directory of this source tree.
  6. */
  7. #pragma once
  8. #include <better/mutex.h>
  9. #include <memory>
  10. #include <react/components/root/RootComponentDescriptor.h>
  11. #include <react/components/root/RootShadowNode.h>
  12. #include <react/core/LayoutConstraints.h>
  13. #include <react/core/ReactPrimitives.h>
  14. #include <react/core/ShadowNode.h>
  15. #include <react/mounting/MountingCoordinator.h>
  16. #include <react/mounting/ShadowTreeDelegate.h>
  17. #include <react/mounting/ShadowTreeRevision.h>
  18. namespace facebook {
  19. namespace react {
  20. using ShadowTreeCommitTransaction = std::function<RootShadowNode::Unshared(
  21. RootShadowNode::Shared const &oldRootShadowNode)>;
  22. /*
  23. * Represents the shadow tree and its lifecycle.
  24. */
  25. class ShadowTree final {
  26. public:
  27. /*
  28. * Creates a new shadow tree instance.
  29. */
  30. ShadowTree(
  31. SurfaceId surfaceId,
  32. LayoutConstraints const &layoutConstraints,
  33. LayoutContext const &layoutContext,
  34. RootComponentDescriptor const &rootComponentDescriptor,
  35. ShadowTreeDelegate const &delegate);
  36. ~ShadowTree();
  37. /*
  38. * Returns the `SurfaceId` associated with the shadow tree.
  39. */
  40. SurfaceId getSurfaceId() const;
  41. /*
  42. * Performs commit calling `transaction` function with a `oldRootShadowNode`
  43. * and expecting a `newRootShadowNode` as a return value.
  44. * The `transaction` function can abort commit returning `nullptr`.
  45. * Returns `true` if the operation finished successfully.
  46. */
  47. bool tryCommit(
  48. ShadowTreeCommitTransaction transaction,
  49. bool enableStateReconciliation = false) const;
  50. /*
  51. * Calls `tryCommit` in a loop until it finishes successfully.
  52. */
  53. void commit(
  54. ShadowTreeCommitTransaction transaction,
  55. bool enableStateReconciliation = false) const;
  56. /*
  57. * Commit an empty tree (a new `RootShadowNode` with no children).
  58. */
  59. void commitEmptyTree() const;
  60. MountingCoordinator::Shared getMountingCoordinator() const;
  61. private:
  62. RootShadowNode::Unshared cloneRootShadowNode(
  63. RootShadowNode::Shared const &oldRootShadowNode,
  64. LayoutConstraints const &layoutConstraints,
  65. LayoutContext const &layoutContext) const;
  66. void emitLayoutEvents(
  67. std::vector<LayoutableShadowNode const *> &affectedLayoutableNodes) const;
  68. SurfaceId const surfaceId_;
  69. ShadowTreeDelegate const &delegate_;
  70. mutable better::shared_mutex commitMutex_;
  71. mutable RootShadowNode::Shared
  72. rootShadowNode_; // Protected by `commitMutex_`.
  73. mutable ShadowTreeRevision::Number revisionNumber_{
  74. 0}; // Protected by `commitMutex_`.
  75. MountingCoordinator::Shared mountingCoordinator_;
  76. };
  77. } // namespace react
  78. } // namespace facebook