| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef SERVICES_GFX_COMPOSITOR_GRAPH_SNAPSHOT_H_ | 5 #ifndef SERVICES_GFX_COMPOSITOR_GRAPH_SNAPSHOT_H_ |
| 6 #define SERVICES_GFX_COMPOSITOR_GRAPH_SNAPSHOT_H_ | 6 #define SERVICES_GFX_COMPOSITOR_GRAPH_SNAPSHOT_H_ |
| 7 | 7 |
| 8 #include <iosfwd> | 8 #include <iosfwd> |
| 9 #include <memory> | 9 #include <memory> |
| 10 #include <unordered_map> | 10 #include <unordered_map> |
| 11 #include <unordered_set> | 11 #include <unordered_set> |
| 12 | 12 |
| 13 #include "base/macros.h" | 13 #include "base/macros.h" |
| 14 #include "base/memory/ref_counted.h" | 14 #include "base/memory/ref_counted.h" |
| 15 #include "mojo/services/geometry/interfaces/geometry.mojom.h" | 15 #include "mojo/services/geometry/interfaces/geometry.mojom.h" |
| 16 #include "mojo/services/gfx/composition/interfaces/scheduling.mojom.h" | 16 #include "mojo/services/gfx/composition/interfaces/scheduling.mojom.h" |
| 17 | 17 |
| 18 namespace compositor { | 18 namespace compositor { |
| 19 | 19 |
| 20 class NodeDef; | 20 class NodeDef; |
| 21 class SceneDef; | 21 class SceneDef; |
| 22 class SceneContent; | 22 class SceneContent; |
| 23 class SceneNodeDef; | 23 class SceneNodeDef; |
| 24 class RenderFrame; | 24 class RenderFrame; |
| 25 | 25 |
| 26 // Describes a single frame snapshot of the scene graph, sufficient for | 26 // Describes a single frame snapshot of the scene graph, sufficient for |
| 27 // rendering and hit testing. When the snapshot is made, all predicated and | 27 // rendering and hit testing. When the snapshot is made, all predicated and |
| 28 // blocked scene nodes are evaluated to produce a final description of | 28 // blocked scene nodes are evaluated to produce a final description of |
| 29 // the frame along with its dependencies. | 29 // the content of the frame along with its dependencies. |
| 30 // | 30 // |
| 31 // The snapshot holds a list of dependencies for the scenes whose state was | 31 // The snapshot holds a list of dependencies for the scenes whose state was |
| 32 // originally used to produce it. The snapshot must be invalidated whenever | 32 // originally used to produce it so that the snapshot can be invalidated |
| 33 // any of these scenes change. Note that the snapshot will contain a list | 33 // whenever one of these scenes changes. Note that the snapshot will contain |
| 34 // of dependencies even in the case where a frame could not be produced, | 34 // a list of dependencies even when rendering is blocked, in which case |
| 35 // in which case the dependencies express the set of scenes which, if updated, | 35 // the dependencies express the set of scenes which, if updated, |
| 36 // might allow composition to be unblocked and make progress on a subsequent | 36 // might allow composition to be unblocked and make progress on a subsequent |
| 37 // frame. | 37 // frame. |
| 38 // | 38 // |
| 39 // Snapshot objects are not thread-safe since they have direct references to | 39 // Snapshot objects are not thread-safe since they have direct references to |
| 40 // the scene graph definition. However, the snapshot's frame is thread-safe | 40 // the scene graph definition. However, the snapshot's frame is thread-safe |
| 41 // and is intended to be sent to the backend rasterizer. | 41 // and is intended to be sent to the backend rasterizer. |
| 42 class Snapshot { | 42 // |
| 43 // Once fully constructed, instances of this class are immutable and |
| 44 // reference counted so they may be bound to scene references in other scenes. |
| 45 class Snapshot : public base::RefCounted<Snapshot> { |
| 43 public: | 46 public: |
| 44 // Describes the result of a snapshot operation. | 47 // Describes the result of a snapshot operation. |
| 45 enum class Disposition { | 48 enum class Disposition { |
| 46 kSuccess, // The snapshot was successful. | 49 kSuccess, // The snapshot was successful. |
| 47 kBlocked, // The node was blocked from rendering. | 50 kBlocked, // The node was blocked from rendering. |
| 48 kCycle, // The node was blocked due to a cycle, must unwind fully. | 51 kCycle, // The node was blocked due to a cycle, must unwind fully. |
| 49 }; | 52 }; |
| 50 | 53 |
| 51 ~Snapshot(); | 54 // Returns true if the snapshot is blocked from rendering. |
| 55 bool is_blocked() const { return disposition_ == Disposition::kBlocked; } |
| 52 | 56 |
| 53 // Returns true if the snapshot is valid. | 57 // Gets the root scene content for the snapshot, or null if blocked. |
| 54 bool valid() const { return valid_; } | 58 const SceneContent* root_scene_content() const { |
| 59 return root_scene_content_.get(); |
| 60 } |
| 55 | 61 |
| 56 // Gets the frame produced from this snapshot, or null if none. | 62 // Returns true if the snapshot has a dependency on content from the |
| 57 // | 63 // specified scene. |
| 58 // This is always null if |valid()| is false but it may be null even | 64 bool HasDependency(const SceneDef* scene) const; |
| 59 // when |valid()| is true if composition was blocked and unable to produce | |
| 60 // a frame during the snapshot operation. | |
| 61 const std::shared_ptr<RenderFrame>& frame() const { return frame_; } | |
| 62 | 65 |
| 63 // Unconditionally marks the snapshot as invalid. | 66 // Creates a frame for rendering. |
| 64 // | 67 // Only valid if |!is_blocked()|. |
| 65 // Returns true if the snapshot became invalid as a result of this operation, | 68 std::shared_ptr<RenderFrame> CreateFrame( |
| 66 // or false if it was already invalid. | 69 const mojo::Rect& viewport, |
| 67 bool Invalidate(); | 70 const mojo::gfx::composition::FrameInfo& frame_info) const; |
| 68 | |
| 69 // Invalidates the snapshot if it has a dependency on the specified scene. | |
| 70 // When this occurs, the entire list of dependencies is flushed (we no longer | |
| 71 // need them) in case the scene in question or its contents are about to | |
| 72 // be destroyed. | |
| 73 // | |
| 74 // Returns true if the snapshot became invalid as a result of this operation, | |
| 75 // or false if it was already invalid. | |
| 76 bool InvalidateScene(const SceneDef* scene_def); | |
| 77 | 71 |
| 78 // Returns true if the specified node was blocked from rendering. | 72 // Returns true if the specified node was blocked from rendering. |
| 79 // Only meaningful while the snapshot is valid. | 73 // Only valid if |!is_blocked()|. |
| 80 bool IsBlocked(const NodeDef* node) const; | 74 bool IsNodeBlocked(const NodeDef* node) const; |
| 81 | 75 |
| 82 // Gets the scene content which was resolved by following a scene node link. | 76 // Gets the scene content which was resolved by following a scene node link. |
| 83 // Only meaningful while the snapshot is valid. | 77 // Only valid if |!is_blocked()|. |
| 84 const SceneContent* GetResolvedSceneContent( | 78 const SceneContent* GetResolvedSceneContent( |
| 85 const SceneNodeDef* scene_node) const; | 79 const SceneNodeDef* scene_node) const; |
| 86 | 80 |
| 87 private: | 81 private: |
| 82 friend class base::RefCounted<Snapshot>; |
| 88 friend class SnapshotBuilder; | 83 friend class SnapshotBuilder; |
| 89 | 84 |
| 90 Snapshot(); | 85 Snapshot(); |
| 86 ~Snapshot(); |
| 91 | 87 |
| 92 void ClearContent(); | 88 // Disposition of the snapshot as a whole. |
| 89 Disposition disposition_; |
| 93 | 90 |
| 94 // Just the set of dependent scene tokens. Used for invalidation. | 91 // Just the set of dependent scene tokens. Used for invalidation. |
| 95 std::unordered_set<uint32_t> dependencies_; | 92 std::unordered_set<uint32_t> dependencies_; |
| 96 | 93 |
| 97 // The root scene in the graph. | 94 // The root scene in the graph. |
| 98 // This reference together with |resolved_scenes| retains all of the | 95 // This reference together with |resolved_scenes| retains all of the |
| 99 // nodes used by the snapshot so that we can use bare pointers for nodes | 96 // nodes used by the snapshot so that we can use bare pointers for nodes |
| 100 // and avoid excess reference counting overhead in other data structures. | 97 // and avoid excess reference counting overhead in other data structures. |
| 101 scoped_refptr<const SceneContent> root_scene_content_; | 98 scoped_refptr<const SceneContent> root_scene_content_; |
| 102 | 99 |
| 103 // Map of scenes which were resolved from scene nodes. | 100 // Map of scenes which were resolved from scene nodes. |
| 104 std::unordered_map<const SceneNodeDef*, scoped_refptr<const SceneContent>> | 101 std::unordered_map<const SceneNodeDef*, scoped_refptr<const SceneContent>> |
| 105 resolved_scene_contents_; | 102 resolved_scene_contents_; |
| 106 | 103 |
| 107 // Node states, true if snapshotted successfully, false if blocked. | 104 // Node states, true if snapshotted successfully, false if blocked. |
| 108 std::unordered_map<const NodeDef*, Disposition> node_dispositions_; | 105 std::unordered_map<const NodeDef*, Disposition> node_dispositions_; |
| 109 | 106 |
| 110 // A frame ready to be rendered. | |
| 111 std::shared_ptr<RenderFrame> frame_; | |
| 112 | |
| 113 // True if the snapshot is still valid. | |
| 114 bool valid_ = true; | |
| 115 | |
| 116 DISALLOW_COPY_AND_ASSIGN(Snapshot); | 107 DISALLOW_COPY_AND_ASSIGN(Snapshot); |
| 117 }; | 108 }; |
| 118 | 109 |
| 119 // Builds a table of all of the state which will be required for rendering | 110 // Builds a table of all of the state which will be required for rendering |
| 120 // a scene graph. | 111 // a scene graph. |
| 121 class SnapshotBuilder { | 112 class SnapshotBuilder { |
| 122 public: | 113 public: |
| 123 explicit SnapshotBuilder(std::ostream* block_log); | 114 explicit SnapshotBuilder(std::ostream* block_log); |
| 124 ~SnapshotBuilder(); | 115 ~SnapshotBuilder(); |
| 125 | 116 |
| 126 // If not null, the snapshotter will append information to this stream | 117 // If not null, the snapshotter will append information to this stream |
| 127 // describing the parts of the scene graph for which composition was blocked. | 118 // describing the parts of the scene graph for which composition was blocked. |
| 128 std::ostream* block_log() { return block_log_; } | 119 std::ostream* block_log() { return block_log_; } |
| 129 | 120 |
| 130 // Snapshots the requested node. | 121 // Snapshots the requested node. |
| 131 Snapshot::Disposition SnapshotNode(const NodeDef* node, | 122 Snapshot::Disposition SnapshotNode(const NodeDef* node, |
| 132 const SceneContent* content); | 123 const SceneContent* content); |
| 133 | 124 |
| 134 // Snapshots the requested scene. | 125 // Snapshots the requested scene. |
| 135 Snapshot::Disposition SnapshotScene(const SceneDef* scene, | 126 Snapshot::Disposition SnapshotScene(const SceneDef* scene, |
| 136 uint32_t version, | 127 uint32_t version, |
| 137 const SceneNodeDef* referrer_node, | 128 const SceneNodeDef* referrer_node, |
| 138 const SceneContent* referrer_content); | 129 const SceneContent* referrer_content); |
| 139 | 130 |
| 140 // Builds a snapshot rooted at the specified scene. | 131 // Builds a snapshot rooted at the specified scene. |
| 141 std::unique_ptr<Snapshot> Build( | 132 scoped_refptr<const Snapshot> Build(const SceneDef* root_scene); |
| 142 const SceneDef* root_scene, | |
| 143 const mojo::Rect& viewport, | |
| 144 const mojo::gfx::composition::FrameInfo& frame_info); | |
| 145 | 133 |
| 146 private: | 134 private: |
| 147 // Snapshots the root scene of a renderer. | 135 // Snapshots the root scene of a renderer. |
| 148 // This is just like |SnapshotScene| but the errors are reported a little | 136 // This is just like |SnapshotScene| but the errors are reported a little |
| 149 // differently since there is no referrer node. | 137 // differently since there is no referrer node. |
| 150 Snapshot::Disposition SnapshotRenderer(const SceneDef* scene); | 138 Snapshot::Disposition SnapshotRenderer(const SceneDef* scene); |
| 151 | 139 |
| 152 // Snapshots the root node of a scene and detects cycles. | 140 // Snapshots the root node of a scene and detects cycles. |
| 153 // This is just like |SnapshotNode| but performs cycle detection which | 141 // This is just like |SnapshotNode| but performs cycle detection which |
| 154 // isn't otherwise needed. | 142 // isn't otherwise needed. |
| 155 Snapshot::Disposition SnapshotRootAndDetectCycles( | 143 Snapshot::Disposition SnapshotRootAndDetectCycles( |
| 156 const NodeDef* node, | 144 const NodeDef* node, |
| 157 const SceneContent* content); | 145 const SceneContent* content); |
| 158 | 146 |
| 159 std::ostream* const block_log_; | 147 std::ostream* const block_log_; |
| 160 std::unique_ptr<Snapshot> snapshot_; | 148 scoped_refptr<Snapshot> snapshot_; |
| 161 const SceneContent* cycle_ = nullptr; // point where a cycle was detected | 149 const SceneContent* cycle_ = nullptr; // point where a cycle was detected |
| 162 | 150 |
| 163 DISALLOW_COPY_AND_ASSIGN(SnapshotBuilder); | 151 DISALLOW_COPY_AND_ASSIGN(SnapshotBuilder); |
| 164 }; | 152 }; |
| 165 | 153 |
| 166 } // namespace compositor | 154 } // namespace compositor |
| 167 | 155 |
| 168 #endif // SERVICES_GFX_COMPOSITOR_GRAPH_SNAPSHOT_H_ | 156 #endif // SERVICES_GFX_COMPOSITOR_GRAPH_SNAPSHOT_H_ |
| OLD | NEW |