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 BLIMP_CLIENT_FEATURE_COMPOSITOR_BLIMP_COMPOSITOR_H_ | |
6 #define BLIMP_CLIENT_FEATURE_COMPOSITOR_BLIMP_COMPOSITOR_H_ | |
7 | |
8 #include <memory> | |
9 #include <vector> | |
10 | |
11 #include "base/macros.h" | |
12 #include "base/memory/ref_counted.h" | |
13 #include "blimp/client/feature/compositor/blimp_input_manager.h" | |
14 #include "cc/trees/layer_tree_host.h" | |
15 #include "cc/trees/layer_tree_host_client.h" | |
16 #include "cc/trees/layer_tree_settings.h" | |
17 #include "cc/trees/remote_proto_channel.h" | |
18 #include "ui/gfx/geometry/size.h" | |
19 #include "ui/gfx/native_widget_types.h" | |
20 | |
21 namespace base { | |
22 class SingleThreadTaskRunner; | |
23 class Thread; | |
24 } | |
25 | |
26 namespace cc { | |
27 namespace proto { | |
28 class CompositorMessage; | |
29 class InitializeImpl; | |
30 } | |
31 class LayerTreeHost; | |
32 } | |
33 | |
34 namespace blimp { | |
35 | |
36 class BlimpMessage; | |
37 | |
38 namespace client { | |
39 | |
40 // The BlimpCompositorClient provides the BlimpCompositor with the necessary | |
41 // dependencies for cc::LayerTreeHost owned by this compositor and for | |
42 // communicating the compositor and input messages to the corresponding | |
43 // render widget of this compositor on the engine. | |
44 class BlimpCompositorClient { | |
45 public: | |
46 // These methods should provide the dependencies for cc::LayerTreeHost for | |
47 // this compositor. | |
48 virtual cc::LayerTreeSettings* GetLayerTreeSettings() = 0; | |
49 virtual scoped_refptr<base::SingleThreadTaskRunner> | |
50 GetCompositorTaskRunner() = 0; | |
51 virtual cc::TaskGraphRunner* GetTaskGraphRunner() = 0; | |
52 virtual gpu::GpuMemoryBufferManager* GetGpuMemoryBufferManager() = 0; | |
53 virtual cc::ImageSerializationProcessor* GetImageSerializationProcessor() = 0; | |
54 virtual void DidCompleteSwapBuffers() = 0; | |
55 virtual void DidCommitAndDrawFrame() = 0; | |
56 | |
57 // Should send web gesture events which could not be handled locally by the | |
58 // compositor to the engine. | |
59 virtual void SendWebGestureEvent( | |
60 int render_widget_id, | |
61 const blink::WebGestureEvent& gesture_event) = 0; | |
62 | |
63 // Should send the compositor messages from the remote client LayerTreeHost of | |
64 // this compositor to the corresponding remote server LayerTreeHost. | |
65 virtual void SendCompositorMessage( | |
66 int render_widget_id, | |
67 const cc::proto::CompositorMessage& message) = 0; | |
68 | |
69 protected: | |
70 virtual ~BlimpCompositorClient() {} | |
71 }; | |
72 | |
73 // BlimpCompositor provides the basic framework and setup to host a | |
74 // LayerTreeHost. The class that owns the LayerTreeHost is usually called the | |
75 // compositor, but the LayerTreeHost does the compositing work. The rendering | |
76 // surface this compositor draws to is defined by the gfx::AcceleratedWidget set | |
77 // by SetAcceleratedWidget(). This class should only be accessed from the main | |
78 // thread. Any interaction with the compositing thread should happen through | |
79 // the LayerTreeHost. | |
80 // | |
81 // The Blimp compositor owns the remote client cc::LayerTreeHost, which performs | |
82 // the compositing work for the remote server LayerTreeHost. The server | |
83 // LayerTreeHost for a BlimpCompositor is owned by the | |
84 // content::RenderWidgetCompositor. Thus, each BlimpCompositor is tied to a | |
85 // RenderWidget, identified by a custom |render_widget_id| generated on the | |
86 // engine. The lifetime of this compositor is controlled by its corresponding | |
87 // RenderWidget. | |
88 class BlimpCompositor | |
89 : public cc::LayerTreeHostClient, | |
90 public cc::RemoteProtoChannel, | |
91 public BlimpInputManagerClient { | |
92 public: | |
93 BlimpCompositor(const int render_widget_id, BlimpCompositorClient* client); | |
94 | |
95 ~BlimpCompositor() override; | |
96 | |
97 // Sets whether or not this compositor actually draws to the output surface. | |
98 // Setting this to false will make the compositor drop all of its resources | |
99 // and the output surface. Setting it to true again will rebuild the output | |
100 // surface from the gfx::AcceleratedWidget (see SetAcceleratedWidget). | |
101 // virtual for testing. | |
102 virtual void SetVisible(bool visible); | |
103 | |
104 // Lets this compositor know that it can draw to |widget|. This means that, | |
105 // if this compositor is visible, it will build an output surface and GL | |
106 // context around |widget| and will draw to it. ReleaseAcceleratedWidget() | |
107 // *must* be called before SetAcceleratedWidget() is called with the same | |
108 // gfx::AcceleratedWidget on another compositor. | |
109 // virtual for testing. | |
110 virtual void SetAcceleratedWidget(gfx::AcceleratedWidget widget); | |
111 | |
112 // Releases the internally stored gfx::AcceleratedWidget and the associated | |
113 // output surface. This must be called before calling | |
114 // SetAcceleratedWidget() with the same gfx::AcceleratedWidget on another | |
115 // compositor. | |
116 // virtual for testing. | |
117 virtual void ReleaseAcceleratedWidget(); | |
118 | |
119 // Forwards the touch event to the |input_manager_|. | |
120 // virtual for testing. | |
121 virtual bool OnTouchEvent(const ui::MotionEvent& motion_event); | |
122 | |
123 // Called to forward the compositor message from the remote server | |
124 // LayerTreeHost of the render widget for this compositor. | |
125 // virtual for testing. | |
126 virtual void OnCompositorMessageReceived( | |
127 std::unique_ptr<cc::proto::CompositorMessage> message); | |
128 | |
129 int render_widget_id() const { return render_widget_id_; } | |
130 | |
131 private: | |
132 friend class BlimpCompositorForTesting; | |
133 | |
134 // LayerTreeHostClient implementation. | |
135 void WillBeginMainFrame() override; | |
136 void DidBeginMainFrame() override; | |
137 void BeginMainFrame(const cc::BeginFrameArgs& args) override; | |
138 void BeginMainFrameNotExpectedSoon() override; | |
139 void UpdateLayerTreeHost() override; | |
140 void ApplyViewportDeltas(const gfx::Vector2dF& inner_delta, | |
141 const gfx::Vector2dF& outer_delta, | |
142 const gfx::Vector2dF& elastic_overscroll_delta, | |
143 float page_scale, | |
144 float top_controls_delta) override; | |
145 void RequestNewOutputSurface() override; | |
146 void DidInitializeOutputSurface() override; | |
147 void DidFailToInitializeOutputSurface() override; | |
148 void WillCommit() override; | |
149 void DidCommit() override; | |
150 void DidCommitAndDrawFrame() override; | |
151 void DidCompleteSwapBuffers() override; | |
152 void DidCompletePageScaleAnimation() override; | |
153 | |
154 // RemoteProtoChannel implementation. | |
155 void SetProtoReceiver(ProtoReceiver* receiver) override; | |
156 void SendCompositorProto(const cc::proto::CompositorMessage& proto) override; | |
157 | |
158 // BlimpInputManagerClient implementation. | |
159 void SendWebGestureEvent( | |
160 const blink::WebGestureEvent& gesture_event) override; | |
161 | |
162 // Internal method to correctly set the visibility on the |host_|. It will | |
163 // make the |host_| visible if |visible| is true and we have a valid |window_| | |
164 // If |visible_| is false, the host will also release its output surface. | |
165 void SetVisibleInternal(bool visible); | |
166 | |
167 // Helper method to build the internal CC compositor instance from |message|. | |
168 void CreateLayerTreeHost( | |
169 const cc::proto::InitializeImpl& initialize_message); | |
170 | |
171 // Helper method to destroy the internal CC compositor instance and all its | |
172 // associated state. | |
173 void DestroyLayerTreeHost(); | |
174 | |
175 // Creates (if necessary) and returns a TaskRunner for a thread meant to run | |
176 // compositor rendering. | |
177 void HandlePendingOutputSurfaceRequest(); | |
178 | |
179 // The unique identifier for the render widget for this compositor. | |
180 const int render_widget_id_; | |
181 | |
182 BlimpCompositorClient* client_; | |
183 | |
184 std::unique_ptr<cc::LayerTreeHost> host_; | |
185 | |
186 gfx::AcceleratedWidget window_; | |
187 | |
188 // Whether or not |host_| should be visible. This is stored in case |host_| | |
189 // is null when SetVisible() is called or if we don't have a | |
190 // gfx::AcceleratedWidget to build an output surface from. | |
191 bool host_should_be_visible_; | |
192 | |
193 // Whether there is an OutputSurface request pending from the current | |
194 // |host_|. Becomes |true| if RequestNewOutputSurface is called, and |false| | |
195 // if |host_| is deleted or we succeed in creating *and* initializing an | |
196 // OutputSurface (which is essentially the contract with cc). | |
197 bool output_surface_request_pending_; | |
198 | |
199 // To be notified of any incoming compositor protos that are specifically sent | |
200 // to |render_widget_id_|. | |
201 cc::RemoteProtoChannel::ProtoReceiver* remote_proto_channel_receiver_; | |
202 | |
203 // Handles input events for the current render widget. The lifetime of the | |
204 // input manager is tied to the lifetime of the |host_| which owns the | |
205 // cc::InputHandler. The input events are forwarded to this input handler by | |
206 // the manager to be handled by the client compositor for the current render | |
207 // widget. | |
208 std::unique_ptr<BlimpInputManager> input_manager_; | |
209 | |
210 DISALLOW_COPY_AND_ASSIGN(BlimpCompositor); | |
211 }; | |
212 | |
213 } // namespace client | |
214 } // namespace blimp | |
215 | |
216 #endif // BLIMP_CLIENT_FEATURE_COMPOSITOR_BLIMP_COMPOSITOR_H_ | |
OLD | NEW |