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