OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #ifndef SERVICES_GFX_COMPOSITOR_GRAPH_SCENE_DEF_H_ |
| 6 #define SERVICES_GFX_COMPOSITOR_GRAPH_SCENE_DEF_H_ |
| 7 |
| 8 #include <iosfwd> |
| 9 #include <memory> |
| 10 #include <string> |
| 11 #include <unordered_map> |
| 12 #include <vector> |
| 13 |
| 14 #include "base/callback.h" |
| 15 #include "base/macros.h" |
| 16 #include "mojo/services/gfx/composition/interfaces/scenes.mojom.h" |
| 17 #include "services/gfx/compositor/graph/node_def.h" |
| 18 |
| 19 namespace compositor { |
| 20 |
| 21 class RenderLayer; |
| 22 class RenderLayerBuilder; |
| 23 class SceneDef; |
| 24 class SnapshotBuilder; |
| 25 |
| 26 // Resolves a scene token to a scene definition. |
| 27 using SceneResolver = |
| 28 base::Callback<SceneDef*(mojo::gfx::composition::SceneToken*)>; |
| 29 |
| 30 // Sends a scene unavailable message with the specified resource id. |
| 31 using SceneUnavailableSender = base::Callback<void(uint32_t)>; |
| 32 |
| 33 // Scene definition. |
| 34 // Contains all of the resources and nodes of a published scene. |
| 35 class SceneDef { |
| 36 public: |
| 37 // Outcome of a call to |Present|. |
| 38 enum class Disposition { |
| 39 kUnchanged, |
| 40 kSucceeded, |
| 41 kFailed, |
| 42 }; |
| 43 |
| 44 SceneDef(mojo::gfx::composition::SceneTokenPtr scene_token, |
| 45 const std::string& label); |
| 46 ~SceneDef(); |
| 47 |
| 48 // Gets the token used to refer to this scene globally. |
| 49 // Caller does not obtain ownership of the token. |
| 50 mojo::gfx::composition::SceneToken* scene_token() { |
| 51 return scene_token_.get(); |
| 52 } |
| 53 |
| 54 // Gets the currently published scene graph version. |
| 55 uint32_t version() { return version_; } |
| 56 |
| 57 // Gets the root node, or nullptr if none. |
| 58 NodeDef* root_node() { return root_node_; } |
| 59 |
| 60 // Enqueues a pending update event to the scene graph. |
| 61 void EnqueueUpdate(mojo::gfx::composition::SceneUpdatePtr update); |
| 62 |
| 63 // Enqueues a pending publish event to the scene graph. |
| 64 // The changes are not applied until |ApplyChanges| is called. |
| 65 void EnqueuePublish(mojo::gfx::composition::SceneMetadataPtr metadata); |
| 66 |
| 67 // Applies published updates to the scene up to the point indicated by |
| 68 // |presentation_time|. |
| 69 // |
| 70 // Returns a value which indicates whether the updates succeded. |
| 71 // If the result is |kFailed|, the scene graph was left in an unusable |
| 72 // and inconsistent state and must be destroyed. |
| 73 Disposition Present(int64_t presentation_time, |
| 74 const SceneResolver& resolver, |
| 75 const SceneUnavailableSender& unavailable_sender, |
| 76 std::ostream& err); |
| 77 |
| 78 // Unlinks references to another scene which has been unregistered. |
| 79 // Causes |OnResourceUnavailable()| to be delivered to the scene for all |
| 80 // invalidated scene resources. Returns true if any changes were made. |
| 81 bool UnlinkReferencedScene(SceneDef* scene, |
| 82 const SceneUnavailableSender& unavailable_sender); |
| 83 |
| 84 // Generates a snapshot of the scene. |
| 85 // Returns true if successful, false if the scene is blocked from rendering. |
| 86 bool Snapshot(SnapshotBuilder* snapshot_builder, |
| 87 RenderLayerBuilder* layer_builder); |
| 88 |
| 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 } |
| 101 |
| 102 // Finds the node with the specified id. |
| 103 // Returns nullptr if not found. |
| 104 NodeDef* FindNode(uint32_t node_id); |
| 105 |
| 106 const std::string& label() { return label_; } |
| 107 std::string FormattedLabel(); |
| 108 |
| 109 private: |
| 110 struct Publication { |
| 111 Publication(mojo::gfx::composition::SceneMetadataPtr metadata); |
| 112 ~Publication(); |
| 113 |
| 114 bool is_due(int64_t presentation_time) const { |
| 115 return metadata->presentation_time <= presentation_time; |
| 116 } |
| 117 |
| 118 mojo::gfx::composition::SceneMetadataPtr metadata; |
| 119 std::vector<mojo::gfx::composition::SceneUpdatePtr> updates; |
| 120 |
| 121 private: |
| 122 DISALLOW_COPY_AND_ASSIGN(Publication); |
| 123 }; |
| 124 |
| 125 bool ApplyUpdate(mojo::gfx::composition::SceneUpdatePtr update, |
| 126 const SceneResolver& resolver, |
| 127 const SceneUnavailableSender& unavailable_sender, |
| 128 std::ostream& err); |
| 129 bool Validate(std::ostream& err); |
| 130 |
| 131 bool SnapshotInner(SnapshotBuilder* snapshot_builder, |
| 132 RenderLayerBuilder* layer_builder); |
| 133 std::shared_ptr<RenderLayer> SnapshotLayer(SnapshotBuilder* snapshot_builder); |
| 134 void Invalidate(); |
| 135 bool HasSceneResources(); |
| 136 |
| 137 ResourceDef* CreateResource(uint32_t resource_id, |
| 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 |
| 149 mojo::gfx::composition::SceneTokenPtr scene_token_; |
| 150 const std::string label_; |
| 151 std::string formatted_label_cache_; |
| 152 |
| 153 uint32_t version_ = mojo::gfx::composition::kSceneVersionNone; |
| 154 std::vector<mojo::gfx::composition::SceneUpdatePtr> pending_updates_; |
| 155 std::vector<std::unique_ptr<Publication>> pending_publications_; |
| 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 |
| 160 std::shared_ptr<RenderLayer> cached_layer_; |
| 161 |
| 162 // Used to detect cycles during a snapshot operation. |
| 163 // This is safe because the object will only be used by a single thread. |
| 164 bool visited_ = false; |
| 165 |
| 166 DISALLOW_COPY_AND_ASSIGN(SceneDef); |
| 167 }; |
| 168 |
| 169 } // namespace compositor |
| 170 |
| 171 #endif // SERVICES_GFX_COMPOSITOR_GRAPH_SCENE_DEF_H_ |
OLD | NEW |