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 #include "mandoline/ui/aura/surface_binding.h" | 5 #include "mandoline/ui/aura/surface_binding.h" |
6 | 6 |
7 #include <map> | 7 #include <map> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/lazy_instance.h" | 10 #include "base/lazy_instance.h" |
11 #include "base/threading/thread_local.h" | 11 #include "base/threading/thread_local.h" |
12 #include "cc/output/compositor_frame.h" | 12 #include "cc/output/compositor_frame.h" |
13 #include "cc/output/output_surface.h" | 13 #include "cc/output/output_surface.h" |
14 #include "cc/output/output_surface_client.h" | 14 #include "cc/output/output_surface_client.h" |
15 #include "cc/output/software_output_device.h" | 15 #include "cc/output/software_output_device.h" |
16 #include "cc/resources/shared_bitmap_manager.h" | 16 #include "cc/resources/shared_bitmap_manager.h" |
17 #include "components/view_manager/public/cpp/view.h" | 17 #include "components/view_manager/public/cpp/view.h" |
18 #include "components/view_manager/public/cpp/view_manager.h" | 18 #include "components/view_manager/public/cpp/view_manager.h" |
19 #include "components/view_manager/public/interfaces/gpu.mojom.h" | 19 #include "components/view_manager/public/interfaces/gpu.mojom.h" |
20 #include "components/view_manager/public/interfaces/surfaces.mojom.h" | |
21 #include "mandoline/ui/aura/window_tree_host_mojo.h" | 20 #include "mandoline/ui/aura/window_tree_host_mojo.h" |
22 #include "mojo/application/public/cpp/connect.h" | 21 #include "mojo/application/public/cpp/connect.h" |
23 #include "mojo/application/public/interfaces/shell.mojom.h" | 22 #include "mojo/application/public/interfaces/shell.mojom.h" |
24 #include "mojo/cc/context_provider_mojo.h" | 23 #include "mojo/cc/context_provider_mojo.h" |
| 24 #include "mojo/cc/output_surface_mojo.h" |
25 #include "mojo/converters/geometry/geometry_type_converters.h" | 25 #include "mojo/converters/geometry/geometry_type_converters.h" |
26 #include "mojo/converters/surfaces/surfaces_type_converters.h" | 26 #include "mojo/converters/surfaces/surfaces_type_converters.h" |
27 | 27 |
28 namespace mandoline { | 28 namespace mandoline { |
29 namespace { | |
30 | |
31 // OutputSurface --------------------------------------------------------------- | |
32 | |
33 // OutputSurface implementation for a view. Pushes the surface id to View when | |
34 // appropriate. | |
35 class OutputSurfaceImpl : public cc::OutputSurface { | |
36 public: | |
37 OutputSurfaceImpl(mojo::View* view, | |
38 const scoped_refptr<cc::ContextProvider>& context_provider, | |
39 mojo::Surface* surface, | |
40 uint32_t id_namespace, | |
41 uint32_t* next_local_id); | |
42 ~OutputSurfaceImpl() override; | |
43 | |
44 // cc::OutputSurface: | |
45 void SwapBuffers(cc::CompositorFrame* frame) override; | |
46 | |
47 private: | |
48 mojo::View* view_; | |
49 mojo::Surface* surface_; | |
50 uint32_t id_namespace_; | |
51 uint32_t* next_local_id_; // Owned by PerViewManagerState. | |
52 uint32_t local_id_; | |
53 gfx::Size surface_size_; | |
54 | |
55 DISALLOW_COPY_AND_ASSIGN(OutputSurfaceImpl); | |
56 }; | |
57 | |
58 OutputSurfaceImpl::OutputSurfaceImpl( | |
59 mojo::View* view, | |
60 const scoped_refptr<cc::ContextProvider>& context_provider, | |
61 mojo::Surface* surface, | |
62 uint32_t id_namespace, | |
63 uint32_t* next_local_id) | |
64 : cc::OutputSurface(context_provider), | |
65 view_(view), | |
66 surface_(surface), | |
67 id_namespace_(id_namespace), | |
68 next_local_id_(next_local_id), | |
69 local_id_(0u) { | |
70 capabilities_.delegated_rendering = true; | |
71 capabilities_.max_frames_pending = 1; | |
72 } | |
73 | |
74 OutputSurfaceImpl::~OutputSurfaceImpl() { | |
75 } | |
76 | |
77 void OutputSurfaceImpl::SwapBuffers(cc::CompositorFrame* frame) { | |
78 gfx::Size frame_size = | |
79 frame->delegated_frame_data->render_pass_list.back()->output_rect.size(); | |
80 if (frame_size != surface_size_) { | |
81 if (local_id_ != 0u) | |
82 surface_->DestroySurface(local_id_); | |
83 local_id_ = (*next_local_id_)++; | |
84 surface_->CreateSurface(local_id_); | |
85 auto qualified_id = mojo::SurfaceId::New(); | |
86 qualified_id->local = local_id_; | |
87 qualified_id->id_namespace = id_namespace_; | |
88 view_->SetSurfaceId(qualified_id.Pass()); | |
89 surface_size_ = frame_size; | |
90 } | |
91 | |
92 surface_->SubmitFrame(local_id_, | |
93 mojo::CompositorFrame::From(*frame), | |
94 mojo::Closure()); | |
95 | |
96 client_->DidSwapBuffers(); | |
97 client_->DidSwapBuffersComplete(); | |
98 } | |
99 | |
100 } // namespace | |
101 | 29 |
102 // PerViewManagerState --------------------------------------------------------- | 30 // PerViewManagerState --------------------------------------------------------- |
103 | 31 |
104 // State needed per ViewManager. Provides the real implementation of | 32 // State needed per ViewManager. Provides the real implementation of |
105 // CreateOutputSurface. SurfaceBinding obtains a pointer to the | 33 // CreateOutputSurface. SurfaceBinding obtains a pointer to the |
106 // PerViewManagerState appropriate for the ViewManager. PerViewManagerState is | 34 // PerViewManagerState appropriate for the ViewManager. PerViewManagerState is |
107 // stored in a thread local map. When no more refereces to a PerViewManagerState | 35 // stored in a thread local map. When no more refereces to a PerViewManagerState |
108 // remain the PerViewManagerState is deleted and the underlying map cleaned up. | 36 // remain the PerViewManagerState is deleted and the underlying map cleaned up. |
109 class SurfaceBinding::PerViewManagerState | 37 class SurfaceBinding::PerViewManagerState |
110 : public base::RefCounted<PerViewManagerState>, | 38 : public base::RefCounted<PerViewManagerState>, |
(...skipping 12 matching lines...) Expand all Loading... |
123 | 51 |
124 PerViewManagerState(mojo::Shell* shell, mojo::ViewManager* view_manager); | 52 PerViewManagerState(mojo::Shell* shell, mojo::ViewManager* view_manager); |
125 ~PerViewManagerState() override; | 53 ~PerViewManagerState() override; |
126 | 54 |
127 void Init(); | 55 void Init(); |
128 | 56 |
129 // mojo::ResourceReturner: | 57 // mojo::ResourceReturner: |
130 void ReturnResources( | 58 void ReturnResources( |
131 mojo::Array<mojo::ReturnedResourcePtr> resources) override; | 59 mojo::Array<mojo::ReturnedResourcePtr> resources) override; |
132 | 60 |
133 void SetIdNamespace(uint32_t id_namespace); | |
134 | |
135 static base::LazyInstance< | 61 static base::LazyInstance< |
136 base::ThreadLocalPointer<ViewManagerToStateMap>>::Leaky view_states; | 62 base::ThreadLocalPointer<ViewManagerToStateMap>>::Leaky view_states; |
137 | 63 |
138 mojo::Shell* shell_; | 64 mojo::Shell* shell_; |
139 mojo::ViewManager* view_manager_; | 65 mojo::ViewManager* view_manager_; |
140 | 66 |
141 // Set of state needed to create an OutputSurface. | 67 // Set of state needed to create an OutputSurface. |
142 mojo::GpuPtr gpu_; | 68 mojo::GpuPtr gpu_; |
143 mojo::SurfacePtr surface_; | |
144 mojo::Binding<mojo::ResourceReturner> returner_binding_; | 69 mojo::Binding<mojo::ResourceReturner> returner_binding_; |
145 uint32_t id_namespace_; | |
146 uint32_t next_local_id_; | |
147 | 70 |
148 DISALLOW_COPY_AND_ASSIGN(PerViewManagerState); | 71 DISALLOW_COPY_AND_ASSIGN(PerViewManagerState); |
149 }; | 72 }; |
150 | 73 |
151 // static | 74 // static |
152 base::LazyInstance<base::ThreadLocalPointer< | 75 base::LazyInstance<base::ThreadLocalPointer< |
153 SurfaceBinding::PerViewManagerState::ViewManagerToStateMap>>::Leaky | 76 SurfaceBinding::PerViewManagerState::ViewManagerToStateMap>>::Leaky |
154 SurfaceBinding::PerViewManagerState::view_states; | 77 SurfaceBinding::PerViewManagerState::view_states; |
155 | 78 |
156 // static | 79 // static |
(...skipping 11 matching lines...) Expand all Loading... |
168 } | 91 } |
169 return (*view_map)[view_manager]; | 92 return (*view_map)[view_manager]; |
170 } | 93 } |
171 | 94 |
172 scoped_ptr<cc::OutputSurface> | 95 scoped_ptr<cc::OutputSurface> |
173 SurfaceBinding::PerViewManagerState::CreateOutputSurface(mojo::View* view) { | 96 SurfaceBinding::PerViewManagerState::CreateOutputSurface(mojo::View* view) { |
174 // TODO(sky): figure out lifetime here. Do I need to worry about the return | 97 // TODO(sky): figure out lifetime here. Do I need to worry about the return |
175 // value outliving this? | 98 // value outliving this? |
176 mojo::CommandBufferPtr cb; | 99 mojo::CommandBufferPtr cb; |
177 gpu_->CreateOffscreenGLES2Context(GetProxy(&cb)); | 100 gpu_->CreateOffscreenGLES2Context(GetProxy(&cb)); |
| 101 |
| 102 mojo::CompositorFrameReceiverPtr cc_frame_receiver; |
| 103 view->RequestCompositorFrameReceiver(GetProxy(&cc_frame_receiver)); |
178 scoped_refptr<cc::ContextProvider> context_provider( | 104 scoped_refptr<cc::ContextProvider> context_provider( |
179 new mojo::ContextProviderMojo(cb.PassInterface().PassHandle())); | 105 new mojo::ContextProviderMojo(cb.PassInterface().PassHandle())); |
180 return make_scoped_ptr(new OutputSurfaceImpl( | 106 return make_scoped_ptr( |
181 view, context_provider, surface_.get(), id_namespace_, &next_local_id_)); | 107 new mojo::OutputSurfaceMojo(context_provider, |
| 108 cc_frame_receiver.PassInterface())); |
182 } | 109 } |
183 | 110 |
184 SurfaceBinding::PerViewManagerState::PerViewManagerState( | 111 SurfaceBinding::PerViewManagerState::PerViewManagerState( |
185 mojo::Shell* shell, | 112 mojo::Shell* shell, |
186 mojo::ViewManager* view_manager) | 113 mojo::ViewManager* view_manager) |
187 : shell_(shell), | 114 : shell_(shell), |
188 view_manager_(view_manager), | 115 view_manager_(view_manager), |
189 returner_binding_(this), | 116 returner_binding_(this) { |
190 id_namespace_(0u), | |
191 next_local_id_(0u) { | |
192 } | 117 } |
193 | 118 |
194 SurfaceBinding::PerViewManagerState::~PerViewManagerState() { | 119 SurfaceBinding::PerViewManagerState::~PerViewManagerState() { |
195 ViewManagerToStateMap* view_map = view_states.Pointer()->Get(); | 120 ViewManagerToStateMap* view_map = view_states.Pointer()->Get(); |
196 DCHECK(view_map); | 121 DCHECK(view_map); |
197 DCHECK_EQ(this, (*view_map)[view_manager_]); | 122 DCHECK_EQ(this, (*view_map)[view_manager_]); |
198 view_map->erase(view_manager_); | 123 view_map->erase(view_manager_); |
199 if (view_map->empty()) { | 124 if (view_map->empty()) { |
200 delete view_map; | 125 delete view_map; |
201 view_states.Pointer()->Set(nullptr); | 126 view_states.Pointer()->Set(nullptr); |
202 } | 127 } |
203 } | 128 } |
204 | 129 |
205 void SurfaceBinding::PerViewManagerState::Init() { | 130 void SurfaceBinding::PerViewManagerState::Init() { |
206 DCHECK(!surface_.get()); | 131 mojo::ServiceProviderPtr service_provider; |
207 | |
208 mojo::ServiceProviderPtr surfaces_service_provider; | |
209 mojo::URLRequestPtr request(mojo::URLRequest::New()); | 132 mojo::URLRequestPtr request(mojo::URLRequest::New()); |
210 request->url = mojo::String::From("mojo:view_manager"); | 133 request->url = mojo::String::From("mojo:view_manager"); |
211 shell_->ConnectToApplication(request.Pass(), | 134 shell_->ConnectToApplication(request.Pass(), |
212 GetProxy(&surfaces_service_provider), | 135 GetProxy(&service_provider), |
213 nullptr, | 136 nullptr, |
214 nullptr); | 137 nullptr); |
215 ConnectToService(surfaces_service_provider.get(), &surface_); | 138 ConnectToService(service_provider.get(), &gpu_); |
216 surface_->GetIdNamespace( | |
217 base::Bind(&SurfaceBinding::PerViewManagerState::SetIdNamespace, | |
218 base::Unretained(this))); | |
219 // Block until we receive our id namespace. | |
220 surface_.WaitForIncomingResponse(); | |
221 DCHECK_NE(0u, id_namespace_); | |
222 | |
223 mojo::ResourceReturnerPtr returner_ptr; | |
224 returner_binding_.Bind(GetProxy(&returner_ptr)); | |
225 surface_->SetResourceReturner(returner_ptr.Pass()); | |
226 | |
227 mojo::ServiceProviderPtr gpu_service_provider; | |
228 // TODO(jamesr): Should be mojo:gpu_service | |
229 mojo::URLRequestPtr request2(mojo::URLRequest::New()); | |
230 request2->url = mojo::String::From("mojo:view_manager"); | |
231 shell_->ConnectToApplication(request2.Pass(), | |
232 GetProxy(&gpu_service_provider), | |
233 nullptr, | |
234 nullptr); | |
235 ConnectToService(gpu_service_provider.get(), &gpu_); | |
236 } | |
237 | |
238 void SurfaceBinding::PerViewManagerState::SetIdNamespace( | |
239 uint32_t id_namespace) { | |
240 id_namespace_ = id_namespace; | |
241 } | 139 } |
242 | 140 |
243 void SurfaceBinding::PerViewManagerState::ReturnResources( | 141 void SurfaceBinding::PerViewManagerState::ReturnResources( |
244 mojo::Array<mojo::ReturnedResourcePtr> resources) { | 142 mojo::Array<mojo::ReturnedResourcePtr> resources) { |
245 } | 143 } |
246 | 144 |
247 // SurfaceBinding -------------------------------------------------------------- | 145 // SurfaceBinding -------------------------------------------------------------- |
248 | 146 |
249 SurfaceBinding::SurfaceBinding(mojo::Shell* shell, mojo::View* view) | 147 SurfaceBinding::SurfaceBinding(mojo::Shell* shell, mojo::View* view) |
250 : view_(view), | 148 : view_(view), |
251 state_(PerViewManagerState::Get(shell, view->view_manager())) { | 149 state_(PerViewManagerState::Get(shell, view->view_manager())) { |
252 } | 150 } |
253 | 151 |
254 SurfaceBinding::~SurfaceBinding() { | 152 SurfaceBinding::~SurfaceBinding() { |
255 } | 153 } |
256 | 154 |
257 scoped_ptr<cc::OutputSurface> SurfaceBinding::CreateOutputSurface() { | 155 scoped_ptr<cc::OutputSurface> SurfaceBinding::CreateOutputSurface() { |
258 return state_->CreateOutputSurface(view_); | 156 return state_->CreateOutputSurface(view_); |
259 } | 157 } |
260 | 158 |
261 } // namespace mandoline | 159 } // namespace mandoline |
OLD | NEW |