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_SCENE_DEF_H_ | 5 #ifndef SERVICES_GFX_COMPOSITOR_GRAPH_SCENE_DEF_H_ |
6 #define SERVICES_GFX_COMPOSITOR_GRAPH_SCENE_DEF_H_ | 6 #define SERVICES_GFX_COMPOSITOR_GRAPH_SCENE_DEF_H_ |
7 | 7 |
8 #include <iosfwd> | 8 #include <iosfwd> |
9 #include <memory> | |
10 #include <string> | 9 #include <string> |
11 #include <unordered_map> | 10 #include <unordered_map> |
12 #include <vector> | 11 #include <vector> |
13 | 12 |
14 #include "base/callback.h" | 13 #include "base/callback.h" |
15 #include "base/macros.h" | 14 #include "base/macros.h" |
| 15 #include "base/memory/ref_counted.h" |
| 16 #include "base/memory/weak_ptr.h" |
16 #include "mojo/services/gfx/composition/interfaces/scenes.mojom.h" | 17 #include "mojo/services/gfx/composition/interfaces/scenes.mojom.h" |
17 #include "services/gfx/compositor/graph/node_def.h" | 18 #include "services/gfx/compositor/graph/node_def.h" |
| 19 #include "services/gfx/compositor/graph/resource_def.h" |
| 20 #include "services/gfx/compositor/graph/scene_label.h" |
18 | 21 |
19 namespace compositor { | 22 namespace compositor { |
20 | 23 |
21 class RenderLayer; | 24 class SceneContent; |
22 class RenderLayerBuilder; | |
23 class SceneDef; | 25 class SceneDef; |
24 class SnapshotBuilder; | 26 class SnapshotBuilder; |
25 | 27 |
26 // Resolves a scene token to a scene definition. | 28 // Resolves a scene token to a scene definition. |
27 using SceneResolver = | 29 using SceneResolver = base::Callback<base::WeakPtr<SceneDef>( |
28 base::Callback<SceneDef*(mojo::gfx::composition::SceneToken*)>; | 30 const mojo::gfx::composition::SceneToken&)>; |
29 | 31 |
30 // Sends a scene unavailable message with the specified resource id. | 32 // Sends a scene unavailable message with the specified resource id. |
31 using SceneUnavailableSender = base::Callback<void(uint32_t)>; | 33 using SceneUnavailableSender = base::Callback<void(uint32_t)>; |
32 | 34 |
33 // Scene definition. | 35 // Scene definition. |
34 // Contains all of the resources and nodes of a published scene. | 36 // Contains all of the resources and nodes of a published scene. |
| 37 // |
| 38 // TODO(jeffbrown): Consider renaming to |Scene| since there is now an |
| 39 // asymmetry between the stateful nature of this class compared with |
| 40 // |NodeDef| and |ResourceDef| which are immutable but similarly named. |
35 class SceneDef { | 41 class SceneDef { |
36 public: | 42 public: |
37 // Outcome of a call to |Present|. | 43 // Outcome of a call to |Present|. |
38 enum class Disposition { | 44 enum class Disposition { |
39 kUnchanged, | 45 kUnchanged, |
40 kSucceeded, | 46 kSucceeded, |
41 kFailed, | 47 kFailed, |
42 }; | 48 }; |
43 | 49 |
44 SceneDef(mojo::gfx::composition::SceneTokenPtr scene_token, | 50 SceneDef(const SceneLabel& label); |
45 const std::string& label); | |
46 ~SceneDef(); | 51 ~SceneDef(); |
47 | 52 |
48 // Gets the token used to refer to this scene globally. | 53 base::WeakPtr<SceneDef> GetWeakPtr() { return weak_factory_.GetWeakPtr(); } |
49 // Caller does not obtain ownership of the token. | 54 |
50 mojo::gfx::composition::SceneToken* scene_token() { | 55 // Gets the scene label. |
51 return scene_token_.get(); | 56 const SceneLabel& label() const { return label_; } |
| 57 std::string FormattedLabel() const { |
| 58 return label_.FormattedLabelForVersion(version_); |
52 } | 59 } |
53 | 60 |
54 // Gets the currently published scene graph version. | 61 // Gets the currently published scene graph version. |
55 uint32_t version() { return version_; } | 62 uint32_t version() const { return version_; } |
56 | |
57 // Gets the root node, or nullptr if none. | |
58 NodeDef* root_node() { return root_node_; } | |
59 | 63 |
60 // Enqueues a pending update event to the scene graph. | 64 // Enqueues a pending update event to the scene graph. |
61 void EnqueueUpdate(mojo::gfx::composition::SceneUpdatePtr update); | 65 void EnqueueUpdate(mojo::gfx::composition::SceneUpdatePtr update); |
62 | 66 |
63 // Enqueues a pending publish event to the scene graph. | 67 // Enqueues a pending publish event to the scene graph. |
64 // The changes are not applied until |ApplyChanges| is called. | 68 // The changes are not applied until |ApplyChanges| is called. |
65 void EnqueuePublish(mojo::gfx::composition::SceneMetadataPtr metadata); | 69 void EnqueuePublish(mojo::gfx::composition::SceneMetadataPtr metadata); |
66 | 70 |
67 // Applies published updates to the scene up to the point indicated by | 71 // Applies published updates to the scene up to the point indicated by |
68 // |presentation_time|. | 72 // |presentation_time|. |
69 // | 73 // |
70 // Returns a value which indicates whether the updates succeded. | 74 // Returns a value which indicates whether the updates succeded. |
71 // If the result is |kFailed|, the scene graph was left in an unusable | 75 // If the result is |kFailed|, the scene graph was left in an unusable |
72 // and inconsistent state and must be destroyed. | 76 // and inconsistent state and must be destroyed. |
73 Disposition Present(int64_t presentation_time, | 77 Disposition Present(int64_t presentation_time, |
74 const SceneResolver& resolver, | 78 const SceneResolver& resolver, |
75 const SceneUnavailableSender& unavailable_sender, | 79 const SceneUnavailableSender& unavailable_sender, |
76 std::ostream& err); | 80 std::ostream& err); |
77 | 81 |
78 // Unlinks references to another scene which has been unregistered. | 82 // Unlinks references to another scene which has been unregistered. |
79 // Causes |OnResourceUnavailable()| to be delivered to the scene for all | 83 // Causes |OnResourceUnavailable()| to be delivered to the scene for all |
80 // invalidated scene resources. Returns true if any changes were made. | 84 // invalidated scene resources. Returns true if any changes were made. |
81 bool UnlinkReferencedScene(SceneDef* scene, | 85 bool UnlinkReferencedScene(SceneDef* scene, |
82 const SceneUnavailableSender& unavailable_sender); | 86 const SceneUnavailableSender& unavailable_sender); |
83 | 87 |
84 // Generates a snapshot of the scene. | 88 // Finds resources or nodes in the current version, returns nullptr if absent. |
85 // Returns true if successful, false if the scene is blocked from rendering. | 89 const ResourceDef* FindResource(uint32_t resource_id) const; |
86 bool Snapshot(SnapshotBuilder* snapshot_builder, | 90 const NodeDef* FindNode(uint32_t node_id) const; |
87 RenderLayerBuilder* layer_builder); | 91 const NodeDef* FindRootNode() const { |
88 | 92 return FindNode(mojo::gfx::composition::kSceneRootNodeId); |
89 // Finds the resource with the specified id. | |
90 // Returns nullptr if not found. | |
91 ResourceDef* FindResource(uint32_t resource_id, | |
92 ResourceDef::Type resource_type); | |
93 SceneResourceDef* FindSceneResource(uint32_t scene_resource_id) { | |
94 return static_cast<SceneResourceDef*>( | |
95 FindResource(scene_resource_id, ResourceDef::Type::kScene)); | |
96 } | |
97 ImageResourceDef* FindImageResource(uint32_t image_resource_id) { | |
98 return static_cast<ImageResourceDef*>( | |
99 FindResource(image_resource_id, ResourceDef::Type::kImage)); | |
100 } | 93 } |
101 | 94 |
102 // Finds the node with the specified id. | 95 // Finds the most recently presented content of the specified version, |
103 // Returns nullptr if not found. | 96 // returns nullptr if absent. |
104 NodeDef* FindNode(uint32_t node_id); | 97 // If the version is |kSceneVersionNone| returns the current version. |
105 | 98 const SceneContent* FindContent(uint32_t version) const; |
106 const std::string& label() { return label_; } | |
107 std::string FormattedLabel(); | |
108 | 99 |
109 private: | 100 private: |
110 struct Publication { | 101 struct Publication { |
111 Publication(mojo::gfx::composition::SceneMetadataPtr metadata); | 102 Publication(mojo::gfx::composition::SceneMetadataPtr metadata); |
112 ~Publication(); | 103 ~Publication(); |
113 | 104 |
114 bool is_due(int64_t presentation_time) const { | 105 bool is_due(int64_t presentation_time) const { |
115 return metadata->presentation_time <= presentation_time; | 106 return metadata->presentation_time <= presentation_time; |
116 } | 107 } |
117 | 108 |
118 mojo::gfx::composition::SceneMetadataPtr metadata; | 109 mojo::gfx::composition::SceneMetadataPtr metadata; |
119 std::vector<mojo::gfx::composition::SceneUpdatePtr> updates; | 110 std::vector<mojo::gfx::composition::SceneUpdatePtr> updates; |
120 | 111 |
121 private: | 112 private: |
122 DISALLOW_COPY_AND_ASSIGN(Publication); | 113 DISALLOW_COPY_AND_ASSIGN(Publication); |
123 }; | 114 }; |
124 | 115 |
125 bool ApplyUpdate(mojo::gfx::composition::SceneUpdatePtr update, | 116 bool ApplyUpdate(mojo::gfx::composition::SceneUpdatePtr update, |
126 const SceneResolver& resolver, | 117 const SceneResolver& resolver, |
127 const SceneUnavailableSender& unavailable_sender, | 118 const SceneUnavailableSender& unavailable_sender, |
128 std::ostream& err); | 119 std::ostream& err); |
129 bool Validate(std::ostream& err); | |
130 | 120 |
131 bool SnapshotInner(SnapshotBuilder* snapshot_builder, | 121 scoped_refptr<const ResourceDef> CreateResource( |
132 RenderLayerBuilder* layer_builder); | 122 uint32_t resource_id, |
133 std::shared_ptr<RenderLayer> SnapshotLayer(SnapshotBuilder* snapshot_builder); | 123 mojo::gfx::composition::ResourcePtr resource_decl, |
134 void Invalidate(); | 124 const SceneResolver& resolver, |
135 bool HasSceneResources(); | 125 const SceneUnavailableSender& unavailable_sender, |
| 126 std::ostream& err); |
| 127 scoped_refptr<const NodeDef> CreateNode( |
| 128 uint32_t node_id, |
| 129 mojo::gfx::composition::NodePtr node_decl, |
| 130 std::ostream& err); |
136 | 131 |
137 ResourceDef* CreateResource(uint32_t resource_id, | 132 const SceneLabel label_; |
138 mojo::gfx::composition::ResourcePtr resource_decl, | |
139 const SceneResolver& resolver, | |
140 const SceneUnavailableSender& unavailable_sender, | |
141 std::ostream& err); | |
142 NodeDef* CreateNode(uint32_t node_id, | |
143 mojo::gfx::composition::NodePtr node_decl, | |
144 std::ostream& err); | |
145 NodeOp* CreateNodeOp(uint32_t node_id, | |
146 mojo::gfx::composition::NodeOpPtr node_op_decl, | |
147 std::ostream& err); | |
148 | 133 |
149 mojo::gfx::composition::SceneTokenPtr scene_token_; | 134 std::vector<mojo::gfx::composition::SceneUpdatePtr> pending_updates_; |
150 const std::string label_; | 135 std::vector<std::unique_ptr<Publication>> pending_publications_; |
151 std::string formatted_label_cache_; | |
152 | 136 |
153 uint32_t version_ = mojo::gfx::composition::kSceneVersionNone; | 137 uint32_t version_ = mojo::gfx::composition::kSceneVersionNone; |
154 std::vector<mojo::gfx::composition::SceneUpdatePtr> pending_updates_; | 138 std::unordered_map<uint32_t, scoped_refptr<const ResourceDef>> resources_; |
155 std::vector<std::unique_ptr<Publication>> pending_publications_; | 139 std::unordered_map<uint32_t, scoped_refptr<const NodeDef>> nodes_; |
156 std::unordered_map<uint32_t, std::unique_ptr<ResourceDef>> resources_; | |
157 std::unordered_map<uint32_t, std::unique_ptr<NodeDef>> nodes_; | |
158 NodeDef* root_node_ = nullptr; | |
159 | 140 |
160 std::shared_ptr<RenderLayer> cached_layer_; | 141 scoped_refptr<const SceneContent> content_; |
161 | 142 |
162 // Used to detect cycles during a snapshot operation. | 143 base::WeakPtrFactory<SceneDef> weak_factory_; |
163 // This is safe because the object will only be used by a single thread. | |
164 bool visited_ = false; | |
165 | 144 |
166 DISALLOW_COPY_AND_ASSIGN(SceneDef); | 145 DISALLOW_COPY_AND_ASSIGN(SceneDef); |
167 }; | 146 }; |
168 | 147 |
169 } // namespace compositor | 148 } // namespace compositor |
170 | 149 |
171 #endif // SERVICES_GFX_COMPOSITOR_GRAPH_SCENE_DEF_H_ | 150 #endif // SERVICES_GFX_COMPOSITOR_GRAPH_SCENE_DEF_H_ |
OLD | NEW |