1 // Copyright 2018 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 COMPONENTS_UI_DEVTOOLS_VIZ_DOM_AGENT_VIZ_H_
6 #define COMPONENTS_UI_DEVTOOLS_VIZ_DOM_AGENT_VIZ_H_
7 
8 #include "base/containers/flat_map.h"
9 #include "base/containers/flat_set.h"
10 #include "components/ui_devtools/DOM.h"
11 #include "components/ui_devtools/dom_agent.h"
12 #include "components/viz/service/frame_sinks/frame_sink_observer.h"
13 #include "components/viz/service/surfaces/surface_observer.h"
14 
15 namespace viz {
16 struct BeginFrameAck;
17 struct BeginFrameArgs;
18 class FrameSinkId;
19 class FrameSinkManagerImpl;
20 class SurfaceId;
21 class SurfaceInfo;
22 class SurfaceManager;
23 }  // namespace viz
24 
25 namespace ui_devtools {
26 class FrameSinkElement;
27 class SurfaceElement;
28 
29 class DOMAgentViz : public viz::SurfaceObserver,
30                     public viz::FrameSinkObserver,
31                     public DOMAgent {
32  public:
33   explicit DOMAgentViz(viz::FrameSinkManagerImpl* frame_sink_manager);
34   ~DOMAgentViz() override;
35 
36   // viz::SurfaceObserver:
37   void OnFirstSurfaceActivation(const viz::SurfaceInfo& surface_info) override;
OnSurfaceActivated(const viz::SurfaceId & surface_id)38   void OnSurfaceActivated(const viz::SurfaceId& surface_id) override {}
OnSurfaceMarkedForDestruction(const viz::SurfaceId & surface_id)39   void OnSurfaceMarkedForDestruction(
40       const viz::SurfaceId& surface_id) override {}
41   bool OnSurfaceDamaged(const viz::SurfaceId& surface_id,
42                         const viz::BeginFrameAck& ack) override;
43   void OnSurfaceDestroyed(const viz::SurfaceId& surface_id) override;
OnSurfaceDamageExpected(const viz::SurfaceId & surface_id,const viz::BeginFrameArgs & args)44   void OnSurfaceDamageExpected(const viz::SurfaceId& surface_id,
45                                const viz::BeginFrameArgs& args) override {}
46   void OnAddedSurfaceReference(const viz::SurfaceId& parent_id,
47                                const viz::SurfaceId& child_id) override;
48   void OnRemovedSurfaceReference(const viz::SurfaceId& parent_id,
49                                  const viz::SurfaceId& child_id) override;
50 
51   // viz::FrameSinkObserver:
52   void OnRegisteredFrameSinkId(const viz::FrameSinkId& frame_sink_id) override;
53   void OnInvalidatedFrameSinkId(const viz::FrameSinkId& frame_sink_id) override;
54   void OnCreatedCompositorFrameSink(const viz::FrameSinkId& frame_sink_id,
55                                     bool is_root) override;
56   void OnDestroyedCompositorFrameSink(
57       const viz::FrameSinkId& frame_sink_id) override;
58   void OnRegisteredFrameSinkHierarchy(
59       const viz::FrameSinkId& parent_frame_sink_id,
60       const viz::FrameSinkId& child_frame_sink_id) override;
61   void OnUnregisteredFrameSinkHierarchy(
62       const viz::FrameSinkId& parent_frame_sink_id,
63       const viz::FrameSinkId& child_frame_sink_id) override;
OnFrameSinkDidBeginFrame(const viz::FrameSinkId & frame_sink_id,const viz::BeginFrameArgs & args)64   void OnFrameSinkDidBeginFrame(const viz::FrameSinkId& frame_sink_id,
65                                 const viz::BeginFrameArgs& args) override {}
OnFrameSinkDidFinishFrame(const viz::FrameSinkId & frame_sink_id,const viz::BeginFrameArgs & args)66   void OnFrameSinkDidFinishFrame(const viz::FrameSinkId& frame_sink_id,
67                                  const viz::BeginFrameArgs& args) override {}
68 
69   // DOM::Backend:
70   protocol::Response enable() override;
71   protocol::Response disable() override;
72 
73   SurfaceElement* GetRootSurfaceElement();
74 
75  private:
76   std::unique_ptr<protocol::DOM::Node> BuildTreeForFrameSink(
77       UIElement* parent_element,
78       const viz::FrameSinkId& parent_id);
79 
80   std::unique_ptr<protocol::DOM::Node> BuildTreeForSurface(
81       UIElement* parent_element,
82       const viz::SurfaceId& parent_id);
83 
84   // DOMAgent:
85   std::vector<UIElement*> CreateChildrenForRoot() override;
86   std::unique_ptr<protocol::DOM::Node> BuildTreeForUIElement(
87       UIElement* ui_element) override;
88 
89   // Every time the frontend disconnects we don't destroy DOMAgent so once we
90   // establish the connection again we need to clear the FrameSinkId sets
91   // because they may carry obsolete data. Then we initialize these with alive
92   // FrameSinkIds. Clears the sets of FrameSinkIds that correspond to created
93   // FrameSinks, registered FrameSinkIds and those that have corresponding
94   // FrameSinkElements created.
95   void Clear();
96 
97   // Destroy |element| and attach all its children to the root_element().
98   void DestroyElementAndRemoveSubtree(UIElement* element);
99 
100   // Removes an element from either |frame_sink_elements_| or
101   // |surface_elements_|.
102   void DestroyElement(UIElement* element);
103 
104   // Constructs a new FrameSinkElement with some default arguments, adds it to
105   // |frame_sink_elements_|, and returns its pointer.
106   FrameSinkElement* CreateFrameSinkElement(
107       const viz::FrameSinkId& frame_sink_id,
108       UIElement* parent,
109       bool is_root,
110       bool is_client_connected);
111 
112   // Constructs a new SurfaceElement with some default arguments, adds it to
113   // |surface_elements_|, and returns its pointer.
114   SurfaceElement* CreateSurfaceElement(const viz::SurfaceId& surface_id,
115                                        UIElement* parent);
116 
117   // This is used to track created FrameSinkElements in a FrameSink tree. Every
118   // time we register/invalidate a FrameSinkId, create/destroy a FrameSink,
119   // register/unregister hierarchy we change this set, because these actions
120   // involve deleting and adding elements.
121   base::flat_map<viz::FrameSinkId, std::unique_ptr<FrameSinkElement>>
122       frame_sink_elements_;
123 
124   // This is used to track created SurfaceElements and will be used for updates
125   // in a Surface tree.
126   base::flat_map<viz::SurfaceId, std::unique_ptr<SurfaceElement>>
127       surface_elements_;
128 
129   viz::FrameSinkManagerImpl* frame_sink_manager_;
130   viz::SurfaceManager* surface_manager_;
131 
132   DISALLOW_COPY_AND_ASSIGN(DOMAgentViz);
133 };
134 
135 }  // namespace ui_devtools
136 
137 #endif  // COMPONENTS_UI_DEVTOOLS_VIZ_DOM_AGENT_VIZ_H_
138