| 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_NODE_DEF_H_ | 5 #ifndef SERVICES_GFX_COMPOSITOR_GRAPH_NODE_DEF_H_ |
| 6 #define SERVICES_GFX_COMPOSITOR_GRAPH_NODE_DEF_H_ | 6 #define SERVICES_GFX_COMPOSITOR_GRAPH_NODE_DEF_H_ |
| 7 | 7 |
| 8 #include <iosfwd> | 8 #include <iosfwd> |
| 9 #include <memory> | |
| 10 #include <string> | |
| 11 #include <vector> | 9 #include <vector> |
| 12 | 10 |
| 13 #include "base/callback.h" | |
| 14 #include "base/macros.h" | 11 #include "base/macros.h" |
| 12 #include "base/memory/ref_counted.h" |
| 15 #include "mojo/services/gfx/composition/interfaces/nodes.mojom.h" | 13 #include "mojo/services/gfx/composition/interfaces/nodes.mojom.h" |
| 16 #include "services/gfx/compositor/graph/resource_def.h" | 14 #include "services/gfx/compositor/graph/snapshot.h" |
| 15 |
| 16 class SkCanvas; |
| 17 | 17 |
| 18 namespace compositor { | 18 namespace compositor { |
| 19 | 19 |
| 20 class RenderImage; | 20 class SceneContent; |
| 21 class RenderLayerBuilder; | 21 class SceneContentBuilder; |
| 22 class SceneDef; | 22 class SceneDef; |
| 23 class SnapshotBuilder; | |
| 24 class NodeOp; | |
| 25 | 23 |
| 26 // Scene graph node definition. | 24 // Represents a scene graph node. |
| 27 class NodeDef { | 25 // |
| 26 // The base class mainly acts as a container for other nodes and does not |
| 27 // draw any content of its own. |
| 28 // |
| 29 // Instances of this class are immutable and reference counted so they may |
| 30 // be shared by multiple versions of the same scene. |
| 31 class NodeDef : public base::RefCounted<NodeDef> { |
| 28 public: | 32 public: |
| 29 using Combinator = mojo::gfx::composition::Node::Combinator; | 33 using Combinator = mojo::gfx::composition::Node::Combinator; |
| 30 | 34 |
| 31 NodeDef(uint32_t node_id, | 35 NodeDef(uint32_t node_id, |
| 32 mojo::TransformPtr content_transform, | 36 mojo::TransformPtr content_transform, |
| 33 mojo::RectPtr content_clip, | 37 mojo::RectPtr content_clip, |
| 34 uint32_t hit_id, | |
| 35 Combinator combinator, | 38 Combinator combinator, |
| 36 const std::vector<uint32_t>& child_node_ids, | 39 const std::vector<uint32_t>& child_node_ids); |
| 37 NodeOp* op); | |
| 38 ~NodeDef(); | |
| 39 | 40 |
| 40 uint32_t node_id() { return node_id_; } | 41 uint32_t node_id() const { return node_id_; } |
| 41 const mojo::Transform* content_transform() { | 42 const mojo::Transform* content_transform() const { |
| 42 return content_transform_.get(); | 43 return content_transform_.get(); |
| 43 } | 44 } |
| 44 const mojo::Rect* content_clip() { return content_clip_.get(); } | 45 const mojo::Rect* content_clip() const { return content_clip_.get(); } |
| 45 uint32_t hit_id() { return hit_id_; } | 46 Combinator combinator() const { return combinator_; } |
| 46 Combinator combinator() { return combinator_; } | 47 const std::vector<uint32_t>& child_node_ids() const { |
| 47 const std::vector<uint32_t>& child_node_ids() { return child_node_ids_; } | 48 return child_node_ids_; |
| 48 NodeOp* op() { return op_.get(); } | 49 } |
| 49 | 50 |
| 50 std::string FormattedLabel(SceneDef* scene); | 51 // Gets a descriptive label. |
| 52 std::string FormattedLabel(const SceneContent* content) const; |
| 51 | 53 |
| 52 // Updated by |Validate()|. | 54 // Called by the scene content builder to traverse the node's dependencies |
| 53 const std::vector<NodeDef*>& child_nodes() { return child_nodes_; } | 55 // recursively and ensure they are included in the scene's local content. |
| 56 // Returns true if successful, false if the node contains linkage errors. |
| 57 virtual bool RecordContent(SceneContentBuilder* builder) const; |
| 54 | 58 |
| 55 // Validates and prepares the object for rendering. | 59 // Called by the snapshot builder to traverse the node's dependencies |
| 56 // Returns true if successful, false if errors were reported. | 60 // recursively follow links into other scenes, evaluate whether the |
| 57 bool Validate(SceneDef* scene, std::ostream& err); | 61 // node can be rendered, and record which path was taken for the purposes |
| 62 // of satisfying combinators. |
| 63 virtual Snapshot::Disposition RecordSnapshot(const SceneContent* content, |
| 64 SnapshotBuilder* builder) const; |
| 58 | 65 |
| 59 // Generates a snapshot of the node into the specified builder. | 66 // Called to record drawing commands from a snapshot. |
| 60 // Returns true if successful, false if the node is blocked from rendering. | 67 void RecordPicture(const SceneContent* content, |
| 61 bool Snapshot(SnapshotBuilder* snapshot_builder, | 68 const Snapshot* snapshot, |
| 62 RenderLayerBuilder* layer_builder, | 69 SkCanvas* canvas) const; |
| 63 SceneDef* scene); | |
| 64 | 70 |
| 65 // Generates a snapshot of the node's children into the specified builder. | 71 protected: |
| 66 // Returns true if successful, false if the children are blocked from | 72 friend class base::RefCounted<NodeDef>; |
| 67 // rendering. | 73 virtual ~NodeDef(); |
| 68 bool SnapshotChildren(SnapshotBuilder* snapshot_builder, | 74 |
| 69 RenderLayerBuilder* layer_builder, | 75 // Applies a unary function to the children selected by the node's |
| 70 SceneDef* scene); | 76 // combinator rule during a snapshot. |
| 77 // Stops when |Func| returns false. |
| 78 // |Func| should have the signature |bool func(const NodeDef*)|. |
| 79 template <typename Func> |
| 80 void TraverseSnapshottedChildren(const SceneContent* content, |
| 81 const Snapshot* snapshot, |
| 82 const Func& func) const; |
| 83 |
| 84 virtual void RecordPictureInner(const SceneContent* content, |
| 85 const Snapshot* snapshot, |
| 86 SkCanvas* canvas) const; |
| 71 | 87 |
| 72 private: | 88 private: |
| 73 bool SnapshotInner(SnapshotBuilder* snapshot_builder, | 89 uint32_t const node_id_; |
| 74 RenderLayerBuilder* layer_builder, | |
| 75 SceneDef* scene); | |
| 76 | |
| 77 uint32_t node_id_; | |
| 78 mojo::TransformPtr const content_transform_; | 90 mojo::TransformPtr const content_transform_; |
| 79 mojo::RectPtr const content_clip_; | 91 mojo::RectPtr const content_clip_; |
| 80 uint32_t const hit_id_; | |
| 81 Combinator const combinator_; | 92 Combinator const combinator_; |
| 82 std::vector<uint32_t> const child_node_ids_; | 93 std::vector<uint32_t> const child_node_ids_; |
| 83 std::unique_ptr<NodeOp> const op_; | |
| 84 | |
| 85 std::vector<NodeDef*> child_nodes_; | |
| 86 | |
| 87 // Used to detect cycles during a snapshot operation. | |
| 88 // This is safe because the object will only be used by a single thread. | |
| 89 bool visited_ = false; | |
| 90 | 94 |
| 91 DISALLOW_COPY_AND_ASSIGN(NodeDef); | 95 DISALLOW_COPY_AND_ASSIGN(NodeDef); |
| 92 }; | 96 }; |
| 93 | 97 |
| 94 // Abstract scene graph node operation. | 98 // Represents a rectangle node. |
| 95 class NodeOp { | 99 // |
| 100 // Draws a solid color filled rectangle node underneath its children. |
| 101 class RectNodeDef : public NodeDef { |
| 96 public: | 102 public: |
| 97 NodeOp() = default; | 103 RectNodeDef(uint32_t node_id, |
| 98 virtual ~NodeOp() = default; | 104 mojo::TransformPtr content_transform, |
| 105 mojo::RectPtr content_clip, |
| 106 Combinator combinator, |
| 107 const std::vector<uint32_t>& child_node_ids, |
| 108 const mojo::Rect& content_rect, |
| 109 const mojo::gfx::composition::Color& color); |
| 99 | 110 |
| 100 // Validates and prepares the object for rendering. | 111 const mojo::Rect& content_rect() const { return content_rect_; } |
| 101 // Returns true if successful, false if errors were reported. | 112 const mojo::gfx::composition::Color& color() const { return color_; } |
| 102 virtual bool Validate(SceneDef* scene, NodeDef* node, std::ostream& err); | |
| 103 | 113 |
| 104 // Generates a snapshot of the node operation into the specified builder. | 114 protected: |
| 105 // This method is responsible for calling |SnapshotChildren| to process | 115 ~RectNodeDef() override; |
| 106 // the children of the node. Returns true if successful, false if the node | |
| 107 // is blocked from rendering. | |
| 108 virtual bool Snapshot(SnapshotBuilder* snapshot_builder, | |
| 109 RenderLayerBuilder* layer_builder, | |
| 110 SceneDef* scene, | |
| 111 NodeDef* node) = 0; | |
| 112 | 116 |
| 113 private: | 117 void RecordPictureInner(const SceneContent* content, |
| 114 DISALLOW_COPY_AND_ASSIGN(NodeOp); | 118 const Snapshot* snapshot, |
| 115 }; | 119 SkCanvas* canvas) const override; |
| 116 | |
| 117 // A solid color filled rectangle node definition. | |
| 118 class RectNodeOp : public NodeOp { | |
| 119 public: | |
| 120 RectNodeOp(const mojo::Rect& content_rect, | |
| 121 const mojo::gfx::composition::Color& color); | |
| 122 ~RectNodeOp() override; | |
| 123 | |
| 124 const mojo::Rect& content_rect() { return content_rect_; } | |
| 125 const mojo::gfx::composition::Color& color() { return color_; } | |
| 126 | |
| 127 bool Snapshot(SnapshotBuilder* snapshot_builder, | |
| 128 RenderLayerBuilder* layer_builder, | |
| 129 SceneDef* scene, | |
| 130 NodeDef* node) override; | |
| 131 | |
| 132 private: | |
| 133 mojo::Rect content_rect_; | |
| 134 mojo::gfx::composition::Color color_; | |
| 135 | |
| 136 DISALLOW_COPY_AND_ASSIGN(RectNodeOp); | |
| 137 }; | |
| 138 | |
| 139 // An image filled rectangle node definition. | |
| 140 class ImageNodeOp : public NodeOp { | |
| 141 public: | |
| 142 ImageNodeOp(const mojo::Rect& content_rect, | |
| 143 mojo::RectPtr image_rect, | |
| 144 uint32 image_resource_id, | |
| 145 mojo::gfx::composition::BlendPtr blend); | |
| 146 ~ImageNodeOp() override; | |
| 147 | |
| 148 const mojo::Rect& content_rect() { return content_rect_; } | |
| 149 mojo::Rect* image_rect() { return image_rect_.get(); } | |
| 150 uint32_t image_resource_id() { return image_resource_id_; } | |
| 151 mojo::gfx::composition::Blend* blend() { return blend_.get(); } | |
| 152 | |
| 153 // Updated by |Validate()|. | |
| 154 ImageResourceDef* image_resource() { return image_resource_; } | |
| 155 | |
| 156 bool Validate(SceneDef* scene, NodeDef* node, std::ostream& err) override; | |
| 157 | |
| 158 bool Snapshot(SnapshotBuilder* snapshot_builder, | |
| 159 RenderLayerBuilder* layer_builder, | |
| 160 SceneDef* scene, | |
| 161 NodeDef* node) override; | |
| 162 | 120 |
| 163 private: | 121 private: |
| 164 mojo::Rect const content_rect_; | 122 mojo::Rect const content_rect_; |
| 123 mojo::gfx::composition::Color const color_; |
| 124 |
| 125 DISALLOW_COPY_AND_ASSIGN(RectNodeDef); |
| 126 }; |
| 127 |
| 128 // Represents an image node. |
| 129 // |
| 130 // Draws an image filled rectangle underneath its children. |
| 131 class ImageNodeDef : public NodeDef { |
| 132 public: |
| 133 ImageNodeDef(uint32_t node_id, |
| 134 mojo::TransformPtr content_transform, |
| 135 mojo::RectPtr content_clip, |
| 136 Combinator combinator, |
| 137 const std::vector<uint32_t>& child_node_ids, |
| 138 const mojo::Rect& content_rect, |
| 139 mojo::RectPtr image_rect, |
| 140 uint32 image_resource_id, |
| 141 mojo::gfx::composition::BlendPtr blend); |
| 142 |
| 143 const mojo::Rect& content_rect() const { return content_rect_; } |
| 144 const mojo::Rect* image_rect() const { return image_rect_.get(); } |
| 145 uint32_t image_resource_id() const { return image_resource_id_; } |
| 146 const mojo::gfx::composition::Blend* blend() const { return blend_.get(); } |
| 147 |
| 148 bool RecordContent(SceneContentBuilder* builder) const override; |
| 149 |
| 150 protected: |
| 151 ~ImageNodeDef() override; |
| 152 |
| 153 void RecordPictureInner(const SceneContent* content, |
| 154 const Snapshot* snapshot, |
| 155 SkCanvas* canvas) const override; |
| 156 |
| 157 private: |
| 158 mojo::Rect const content_rect_; |
| 165 mojo::RectPtr const image_rect_; | 159 mojo::RectPtr const image_rect_; |
| 166 uint32_t const image_resource_id_; | 160 uint32_t const image_resource_id_; |
| 167 mojo::gfx::composition::BlendPtr const blend_; | 161 mojo::gfx::composition::BlendPtr const blend_; |
| 168 | 162 |
| 169 ImageResourceDef* image_resource_ = nullptr; | 163 DISALLOW_COPY_AND_ASSIGN(ImageNodeDef); |
| 170 | |
| 171 DISALLOW_COPY_AND_ASSIGN(ImageNodeOp); | |
| 172 }; | 164 }; |
| 173 | 165 |
| 174 // An embedded scene node definition. | 166 // Represents a scene node. |
| 175 class SceneNodeOp : public NodeOp { | 167 // |
| 168 // Draws an embedded scene underneath its children. |
| 169 class SceneNodeDef : public NodeDef { |
| 176 public: | 170 public: |
| 177 SceneNodeOp(uint32_t scene_resource_id, uint32_t scene_version); | 171 SceneNodeDef(uint32_t node_id, |
| 178 ~SceneNodeOp() override; | 172 mojo::TransformPtr content_transform, |
| 173 mojo::RectPtr content_clip, |
| 174 Combinator combinator, |
| 175 const std::vector<uint32_t>& child_node_ids, |
| 176 uint32_t scene_resource_id, |
| 177 uint32_t scene_version); |
| 179 | 178 |
| 180 uint32_t scene_resource_id() { return scene_resource_id_; } | 179 uint32_t scene_resource_id() const { return scene_resource_id_; } |
| 181 uint32_t scene_version() { return scene_version_; } | 180 uint32_t scene_version() const { return scene_version_; } |
| 182 | 181 |
| 183 // Updated by |Validate()|. | 182 bool RecordContent(SceneContentBuilder* builder) const override; |
| 184 SceneResourceDef* scene_resource() { return scene_resource_; } | |
| 185 | 183 |
| 186 bool Validate(SceneDef* scene, NodeDef* node, std::ostream& err) override; | 184 Snapshot::Disposition RecordSnapshot(const SceneContent* content, |
| 185 SnapshotBuilder* builder) const override; |
| 187 | 186 |
| 188 bool Snapshot(SnapshotBuilder* snapshot_builder, | 187 protected: |
| 189 RenderLayerBuilder* layer_builder, | 188 ~SceneNodeDef() override; |
| 190 SceneDef* scene, | 189 |
| 191 NodeDef* node) override; | 190 void RecordPictureInner(const SceneContent* content, |
| 191 const Snapshot* snapshot, |
| 192 SkCanvas* canvas) const override; |
| 192 | 193 |
| 193 private: | 194 private: |
| 194 uint32_t const scene_resource_id_; | 195 uint32_t const scene_resource_id_; |
| 195 uint32_t const scene_version_; | 196 uint32_t const scene_version_; |
| 196 | 197 |
| 197 SceneResourceDef* scene_resource_ = nullptr; | 198 DISALLOW_COPY_AND_ASSIGN(SceneNodeDef); |
| 198 | |
| 199 DISALLOW_COPY_AND_ASSIGN(SceneNodeOp); | |
| 200 }; | 199 }; |
| 201 | 200 |
| 202 // A composited layer node definition. | 201 // Represents a layer node. |
| 203 class LayerNodeOp : public NodeOp { | 202 // |
| 203 // Composites its children to a layer and applies a blending operation. |
| 204 class LayerNodeDef : public NodeDef { |
| 204 public: | 205 public: |
| 205 LayerNodeOp(const mojo::Size& size, mojo::gfx::composition::BlendPtr blend); | 206 LayerNodeDef(uint32_t node_id, |
| 206 ~LayerNodeOp() override; | 207 mojo::TransformPtr content_transform, |
| 208 mojo::RectPtr content_clip, |
| 209 Combinator combinator, |
| 210 const std::vector<uint32_t>& child_node_ids, |
| 211 const mojo::Size& size, |
| 212 mojo::gfx::composition::BlendPtr blend); |
| 207 | 213 |
| 208 const mojo::Size& size() { return size_; } | 214 const mojo::Size& size() const { return size_; } |
| 209 mojo::gfx::composition::Blend* blend() { return blend_.get(); } | 215 const mojo::gfx::composition::Blend* blend() const { return blend_.get(); } |
| 210 | 216 |
| 211 bool Snapshot(SnapshotBuilder* snapshot_builder, | 217 protected: |
| 212 RenderLayerBuilder* layer_builder, | 218 ~LayerNodeDef() override; |
| 213 SceneDef* scene, | 219 |
| 214 NodeDef* node) override; | 220 void RecordPictureInner(const SceneContent* content, |
| 221 const Snapshot* snapshot, |
| 222 SkCanvas* canvas) const override; |
| 215 | 223 |
| 216 private: | 224 private: |
| 217 mojo::Size const size_; | 225 mojo::Size const size_; |
| 218 mojo::gfx::composition::BlendPtr const blend_; | 226 mojo::gfx::composition::BlendPtr const blend_; |
| 219 | 227 |
| 220 DISALLOW_COPY_AND_ASSIGN(LayerNodeOp); | 228 DISALLOW_COPY_AND_ASSIGN(LayerNodeDef); |
| 221 }; | 229 }; |
| 222 | 230 |
| 223 } // namespace compositor | 231 } // namespace compositor |
| 224 | 232 |
| 225 #endif // SERVICES_GFX_COMPOSITOR_GRAPH_NODE_DEF_H_ | 233 #endif // SERVICES_GFX_COMPOSITOR_GRAPH_NODE_DEF_H_ |
| OLD | NEW |