OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "mojo/aura/surface_binding.h" | 5 #include "mojo/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 "mojo/aura/window_tree_host_mojo.h" | 17 #include "mojo/aura/window_tree_host_mojo.h" |
18 #include "mojo/cc/context_provider_mojo.h" | 18 #include "mojo/cc/context_provider_mojo.h" |
19 #include "mojo/cc/output_surface_mojo.h" | |
20 #include "mojo/converters/geometry/geometry_type_converters.h" | 19 #include "mojo/converters/geometry/geometry_type_converters.h" |
21 #include "mojo/converters/surfaces/surfaces_type_converters.h" | 20 #include "mojo/converters/surfaces/surfaces_type_converters.h" |
22 #include "mojo/public/cpp/application/connect.h" | 21 #include "mojo/public/cpp/application/connect.h" |
23 #include "mojo/public/interfaces/application/shell.mojom.h" | 22 #include "mojo/public/interfaces/application/shell.mojom.h" |
24 #include "mojo/services/gpu/public/interfaces/gpu.mojom.h" | 23 #include "mojo/services/gpu/public/interfaces/gpu.mojom.h" |
25 #include "mojo/services/surfaces/public/interfaces/surfaces.mojom.h" | 24 #include "mojo/services/surfaces/public/interfaces/surfaces.mojom.h" |
26 #include "mojo/services/surfaces/public/interfaces/surfaces_service.mojom.h" | |
27 #include "mojo/services/view_manager/public/cpp/view.h" | 25 #include "mojo/services/view_manager/public/cpp/view.h" |
28 #include "mojo/services/view_manager/public/cpp/view_manager.h" | 26 #include "mojo/services/view_manager/public/cpp/view_manager.h" |
29 | 27 |
30 namespace mojo { | 28 namespace mojo { |
31 namespace { | 29 namespace { |
32 | 30 |
33 // SurfaceclientImpl ----------------------------------------------------------- | |
34 | |
35 class SurfaceClientImpl : public SurfaceClient { | |
36 public: | |
37 SurfaceClientImpl() {} | |
38 ~SurfaceClientImpl() override {} | |
39 | |
40 // SurfaceClient: | |
41 void SetIdNamespace(uint32_t id_namespace) override {} | |
42 void ReturnResources(Array<ReturnedResourcePtr> resources) override { | |
43 // TODO (sky|jamesr): figure out right way to recycle resources. | |
44 } | |
45 | |
46 private: | |
47 DISALLOW_COPY_AND_ASSIGN(SurfaceClientImpl); | |
48 }; | |
49 | |
50 // OutputSurface --------------------------------------------------------------- | 31 // OutputSurface --------------------------------------------------------------- |
51 | 32 |
52 // OutputSurface implementation for a view. Pushes the surface id to View when | 33 // OutputSurface implementation for a view. Pushes the surface id to View when |
53 // appropriate. | 34 // appropriate. |
54 class OutputSurfaceImpl : public cc::OutputSurface { | 35 class OutputSurfaceImpl : public cc::OutputSurface { |
55 public: | 36 public: |
56 OutputSurfaceImpl(View* view, | 37 OutputSurfaceImpl(View* view, |
57 const scoped_refptr<cc::ContextProvider>& context_provider, | 38 const scoped_refptr<cc::ContextProvider>& context_provider, |
58 Surface* surface, | 39 Surface* surface, |
59 cc::SurfaceIdAllocator* id_allocator); | 40 uint32_t id_namespace, |
| 41 uint32_t* next_local_id); |
60 ~OutputSurfaceImpl() override; | 42 ~OutputSurfaceImpl() override; |
61 | 43 |
62 // cc::OutputSurface: | 44 // cc::OutputSurface: |
63 void SwapBuffers(cc::CompositorFrame* frame) override; | 45 void SwapBuffers(cc::CompositorFrame* frame) override; |
64 | 46 |
65 private: | 47 private: |
66 View* view_; | 48 View* view_; |
67 Surface* surface_; | 49 Surface* surface_; |
68 cc::SurfaceIdAllocator* id_allocator_; | 50 uint32_t id_namespace_; |
69 cc::SurfaceId surface_id_; | 51 uint32_t* next_local_id_; // Owned by PerViewManagerState. |
| 52 uint32_t local_id_; |
70 gfx::Size surface_size_; | 53 gfx::Size surface_size_; |
71 | 54 |
72 DISALLOW_COPY_AND_ASSIGN(OutputSurfaceImpl); | 55 DISALLOW_COPY_AND_ASSIGN(OutputSurfaceImpl); |
73 }; | 56 }; |
74 | 57 |
75 OutputSurfaceImpl::OutputSurfaceImpl( | 58 OutputSurfaceImpl::OutputSurfaceImpl( |
76 View* view, | 59 View* view, |
77 const scoped_refptr<cc::ContextProvider>& context_provider, | 60 const scoped_refptr<cc::ContextProvider>& context_provider, |
78 Surface* surface, | 61 Surface* surface, |
79 cc::SurfaceIdAllocator* id_allocator) | 62 uint32_t id_namespace, |
| 63 uint32_t* next_local_id) |
80 : cc::OutputSurface(context_provider), | 64 : cc::OutputSurface(context_provider), |
81 view_(view), | 65 view_(view), |
82 surface_(surface), | 66 surface_(surface), |
83 id_allocator_(id_allocator) { | 67 id_namespace_(id_namespace), |
| 68 next_local_id_(next_local_id), |
| 69 local_id_(0u) { |
84 capabilities_.delegated_rendering = true; | 70 capabilities_.delegated_rendering = true; |
85 capabilities_.max_frames_pending = 1; | 71 capabilities_.max_frames_pending = 1; |
86 } | 72 } |
87 | 73 |
88 OutputSurfaceImpl::~OutputSurfaceImpl() { | 74 OutputSurfaceImpl::~OutputSurfaceImpl() { |
89 } | 75 } |
90 | 76 |
91 void OutputSurfaceImpl::SwapBuffers(cc::CompositorFrame* frame) { | 77 void OutputSurfaceImpl::SwapBuffers(cc::CompositorFrame* frame) { |
92 gfx::Size frame_size = | 78 gfx::Size frame_size = |
93 frame->delegated_frame_data->render_pass_list.back()->output_rect.size(); | 79 frame->delegated_frame_data->render_pass_list.back()->output_rect.size(); |
94 if (frame_size != surface_size_) { | 80 if (frame_size != surface_size_) { |
95 if (!surface_id_.is_null()) | 81 if (local_id_ != 0u) |
96 surface_->DestroySurface(SurfaceId::From(surface_id_)); | 82 surface_->DestroySurface(local_id_); |
97 surface_id_ = id_allocator_->GenerateId(); | 83 local_id_ = (*next_local_id_)++; |
98 surface_->CreateSurface(SurfaceId::From(surface_id_)); | 84 surface_->CreateSurface(local_id_); |
99 view_->SetSurfaceId(SurfaceId::From(surface_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()); |
100 surface_size_ = frame_size; | 89 surface_size_ = frame_size; |
101 } | 90 } |
102 | 91 |
103 surface_->SubmitFrame(SurfaceId::From(surface_id_), Frame::From(*frame), | 92 surface_->SubmitFrame(local_id_, Frame::From(*frame), mojo::Closure()); |
104 mojo::Closure()); | |
105 | 93 |
106 client_->DidSwapBuffers(); | 94 client_->DidSwapBuffers(); |
107 client_->DidSwapBuffersComplete(); | 95 client_->DidSwapBuffersComplete(); |
108 } | 96 } |
109 | 97 |
110 } // namespace | 98 } // namespace |
111 | 99 |
112 // PerViewManagerState --------------------------------------------------------- | 100 // PerViewManagerState --------------------------------------------------------- |
113 | 101 |
114 // State needed per ViewManager. Provides the real implementation of | 102 // State needed per ViewManager. Provides the real implementation of |
115 // CreateOutputSurface. SurfaceBinding obtains a pointer to the | 103 // CreateOutputSurface. SurfaceBinding obtains a pointer to the |
116 // PerViewManagerState appropriate for the ViewManager. PerViewManagerState is | 104 // PerViewManagerState appropriate for the ViewManager. PerViewManagerState is |
117 // stored in a thread local map. When no more refereces to a PerViewManagerState | 105 // stored in a thread local map. When no more refereces to a PerViewManagerState |
118 // remain the PerViewManagerState is deleted and the underlying map cleaned up. | 106 // remain the PerViewManagerState is deleted and the underlying map cleaned up. |
119 class SurfaceBinding::PerViewManagerState | 107 class SurfaceBinding::PerViewManagerState |
120 : public base::RefCounted<PerViewManagerState> { | 108 : public base::RefCounted<PerViewManagerState>, |
| 109 public mojo::SurfaceClient { |
121 public: | 110 public: |
122 static PerViewManagerState* Get(Shell* shell, ViewManager* view_manager); | 111 static PerViewManagerState* Get(Shell* shell, ViewManager* view_manager); |
123 | 112 |
124 scoped_ptr<cc::OutputSurface> CreateOutputSurface(View* view); | 113 scoped_ptr<cc::OutputSurface> CreateOutputSurface(View* view); |
125 | 114 |
126 private: | 115 private: |
127 typedef std::map<ViewManager*, PerViewManagerState*> ViewManagerToStateMap; | 116 typedef std::map<ViewManager*, PerViewManagerState*> ViewManagerToStateMap; |
128 | 117 |
129 friend class base::RefCounted<PerViewManagerState>; | 118 friend class base::RefCounted<PerViewManagerState>; |
130 | 119 |
131 PerViewManagerState(Shell* shell, ViewManager* view_manager); | 120 PerViewManagerState(Shell* shell, ViewManager* view_manager); |
132 ~PerViewManagerState(); | 121 ~PerViewManagerState(); |
133 | 122 |
134 void Init(); | 123 void Init(); |
135 | 124 |
136 // Callback when a Surface has been created. | 125 // SurfaceClient: |
137 void OnCreatedSurfaceConnection(SurfacePtr surface, uint32_t id_namespace); | 126 void SetIdNamespace(uint32_t id_namespace) override; |
| 127 void ReturnResources( |
| 128 mojo::Array<mojo::ReturnedResourcePtr> resources) override; |
138 | 129 |
139 static base::LazyInstance< | 130 static base::LazyInstance< |
140 base::ThreadLocalPointer<ViewManagerToStateMap>>::Leaky view_states; | 131 base::ThreadLocalPointer<ViewManagerToStateMap>>::Leaky view_states; |
141 | 132 |
142 Shell* shell_; | 133 Shell* shell_; |
143 ViewManager* view_manager_; | 134 ViewManager* view_manager_; |
144 | 135 |
145 // Set of state needed to create an OutputSurface. | 136 // Set of state needed to create an OutputSurface. |
146 scoped_ptr<SurfaceClient> surface_client_; | |
147 GpuPtr gpu_; | 137 GpuPtr gpu_; |
148 SurfacePtr surface_; | 138 SurfacePtr surface_; |
149 SurfacesServicePtr surfaces_service_; | 139 uint32_t id_namespace_; |
150 scoped_ptr<cc::SurfaceIdAllocator> surface_id_allocator_; | 140 uint32_t next_local_id_; |
151 | 141 |
152 DISALLOW_COPY_AND_ASSIGN(PerViewManagerState); | 142 DISALLOW_COPY_AND_ASSIGN(PerViewManagerState); |
153 }; | 143 }; |
154 | 144 |
155 // static | 145 // static |
156 base::LazyInstance<base::ThreadLocalPointer< | 146 base::LazyInstance<base::ThreadLocalPointer< |
157 SurfaceBinding::PerViewManagerState::ViewManagerToStateMap>>::Leaky | 147 SurfaceBinding::PerViewManagerState::ViewManagerToStateMap>>::Leaky |
158 SurfaceBinding::PerViewManagerState::view_states; | 148 SurfaceBinding::PerViewManagerState::view_states; |
159 | 149 |
160 // static | 150 // static |
(...skipping 14 matching lines...) Expand all Loading... |
175 | 165 |
176 scoped_ptr<cc::OutputSurface> | 166 scoped_ptr<cc::OutputSurface> |
177 SurfaceBinding::PerViewManagerState::CreateOutputSurface(View* view) { | 167 SurfaceBinding::PerViewManagerState::CreateOutputSurface(View* view) { |
178 // TODO(sky): figure out lifetime here. Do I need to worry about the return | 168 // TODO(sky): figure out lifetime here. Do I need to worry about the return |
179 // value outliving this? | 169 // value outliving this? |
180 CommandBufferPtr cb; | 170 CommandBufferPtr cb; |
181 gpu_->CreateOffscreenGLES2Context(GetProxy(&cb)); | 171 gpu_->CreateOffscreenGLES2Context(GetProxy(&cb)); |
182 scoped_refptr<cc::ContextProvider> context_provider( | 172 scoped_refptr<cc::ContextProvider> context_provider( |
183 new ContextProviderMojo(cb.PassMessagePipe())); | 173 new ContextProviderMojo(cb.PassMessagePipe())); |
184 return make_scoped_ptr(new OutputSurfaceImpl( | 174 return make_scoped_ptr(new OutputSurfaceImpl( |
185 view, context_provider, surface_.get(), surface_id_allocator_.get())); | 175 view, context_provider, surface_.get(), id_namespace_, &next_local_id_)); |
186 } | 176 } |
187 | 177 |
188 SurfaceBinding::PerViewManagerState::PerViewManagerState( | 178 SurfaceBinding::PerViewManagerState::PerViewManagerState( |
189 Shell* shell, | 179 Shell* shell, |
190 ViewManager* view_manager) | 180 ViewManager* view_manager) |
191 : shell_(shell), view_manager_(view_manager) { | 181 : shell_(shell), |
| 182 view_manager_(view_manager), |
| 183 id_namespace_(0u), |
| 184 next_local_id_(0u) { |
192 } | 185 } |
193 | 186 |
194 SurfaceBinding::PerViewManagerState::~PerViewManagerState() { | 187 SurfaceBinding::PerViewManagerState::~PerViewManagerState() { |
195 ViewManagerToStateMap* view_map = view_states.Pointer()->Get(); | 188 ViewManagerToStateMap* view_map = view_states.Pointer()->Get(); |
196 DCHECK(view_map); | 189 DCHECK(view_map); |
197 DCHECK_EQ(this, (*view_map)[view_manager_]); | 190 DCHECK_EQ(this, (*view_map)[view_manager_]); |
198 view_map->erase(view_manager_); | 191 view_map->erase(view_manager_); |
199 if (view_map->empty()) { | 192 if (view_map->empty()) { |
200 delete view_map; | 193 delete view_map; |
201 view_states.Pointer()->Set(nullptr); | 194 view_states.Pointer()->Set(nullptr); |
202 } | 195 } |
203 } | 196 } |
204 | 197 |
205 void SurfaceBinding::PerViewManagerState::Init() { | 198 void SurfaceBinding::PerViewManagerState::Init() { |
206 DCHECK(!surfaces_service_.get()); | 199 DCHECK(!surface_.get()); |
207 | 200 |
208 ServiceProviderPtr surfaces_service_provider; | 201 ServiceProviderPtr surfaces_service_provider; |
209 shell_->ConnectToApplication("mojo:surfaces_service", | 202 shell_->ConnectToApplication("mojo:surfaces_service", |
210 GetProxy(&surfaces_service_provider)); | 203 GetProxy(&surfaces_service_provider)); |
211 ConnectToService(surfaces_service_provider.get(), &surfaces_service_); | 204 ConnectToService(surfaces_service_provider.get(), &surface_); |
212 // base::Unretained is ok here as we block until the call is received. | 205 surface_.set_client(this); |
213 surfaces_service_->CreateSurfaceConnection( | 206 // Block until we receive our id namespace. |
214 base::Bind(&PerViewManagerState::OnCreatedSurfaceConnection, | 207 surface_.WaitForIncomingMethodCall(); |
215 base::Unretained(this))); | 208 DCHECK_NE(0u, id_namespace_); |
216 // Block until we get the surface. This is done to make it easy for client | |
217 // code. OTOH blocking is ick and leads to all sorts of problems. | |
218 // TODO(sky): ick! There needs to be a better way to deal with this. | |
219 surfaces_service_.WaitForIncomingMethodCall(); | |
220 DCHECK(surface_.get()); | |
221 surface_client_.reset(new SurfaceClientImpl); | |
222 surface_.set_client(surface_client_.get()); | |
223 | 209 |
224 ServiceProviderPtr gpu_service_provider; | 210 ServiceProviderPtr gpu_service_provider; |
225 // TODO(jamesr): Should be mojo:gpu_service | 211 // TODO(jamesr): Should be mojo:gpu_service |
226 shell_->ConnectToApplication("mojo:native_viewport_service", | 212 shell_->ConnectToApplication("mojo:native_viewport_service", |
227 GetProxy(&gpu_service_provider)); | 213 GetProxy(&gpu_service_provider)); |
228 ConnectToService(gpu_service_provider.get(), &gpu_); | 214 ConnectToService(gpu_service_provider.get(), &gpu_); |
229 } | 215 } |
230 | 216 |
231 void SurfaceBinding::PerViewManagerState::OnCreatedSurfaceConnection( | 217 void SurfaceBinding::PerViewManagerState::SetIdNamespace( |
232 SurfacePtr surface, | |
233 uint32_t id_namespace) { | 218 uint32_t id_namespace) { |
234 surface_id_allocator_.reset(new cc::SurfaceIdAllocator(id_namespace)); | 219 id_namespace_ = id_namespace; |
235 surface_ = surface.Pass(); | 220 } |
| 221 |
| 222 void SurfaceBinding::PerViewManagerState::ReturnResources( |
| 223 mojo::Array<mojo::ReturnedResourcePtr> resources) { |
236 } | 224 } |
237 | 225 |
238 // SurfaceBinding -------------------------------------------------------------- | 226 // SurfaceBinding -------------------------------------------------------------- |
239 | 227 |
240 SurfaceBinding::SurfaceBinding(Shell* shell, View* view) | 228 SurfaceBinding::SurfaceBinding(Shell* shell, View* view) |
241 : view_(view), | 229 : view_(view), |
242 state_(PerViewManagerState::Get(shell, view->view_manager())) { | 230 state_(PerViewManagerState::Get(shell, view->view_manager())) { |
243 } | 231 } |
244 | 232 |
245 SurfaceBinding::~SurfaceBinding() { | 233 SurfaceBinding::~SurfaceBinding() { |
246 } | 234 } |
247 | 235 |
248 scoped_ptr<cc::OutputSurface> SurfaceBinding::CreateOutputSurface() { | 236 scoped_ptr<cc::OutputSurface> SurfaceBinding::CreateOutputSurface() { |
249 return state_->CreateOutputSurface(view_); | 237 return state_->CreateOutputSurface(view_); |
250 } | 238 } |
251 | 239 |
252 } // namespace mojo | 240 } // namespace mojo |
OLD | NEW |