| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "mojo/aura/surface_binding.h" | |
| 6 | |
| 7 #include <map> | |
| 8 | |
| 9 #include "base/bind.h" | |
| 10 #include "base/lazy_instance.h" | |
| 11 #include "base/threading/thread_local.h" | |
| 12 #include "cc/output/compositor_frame.h" | |
| 13 #include "cc/output/output_surface.h" | |
| 14 #include "cc/output/output_surface_client.h" | |
| 15 #include "cc/output/software_output_device.h" | |
| 16 #include "cc/resources/shared_bitmap_manager.h" | |
| 17 #include "mojo/aura/window_tree_host_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" | |
| 21 #include "mojo/converters/surfaces/surfaces_type_converters.h" | |
| 22 #include "mojo/public/cpp/application/connect.h" | |
| 23 #include "mojo/public/interfaces/application/shell.mojom.h" | |
| 24 #include "mojo/services/gpu/public/interfaces/gpu.mojom.h" | |
| 25 #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" | |
| 28 #include "mojo/services/view_manager/public/cpp/view_manager.h" | |
| 29 | |
| 30 namespace mojo { | |
| 31 namespace { | |
| 32 | |
| 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 --------------------------------------------------------------- | |
| 51 | |
| 52 // OutputSurface implementation for a view. Pushes the surface id to View when | |
| 53 // appropriate. | |
| 54 class OutputSurfaceImpl : public cc::OutputSurface { | |
| 55 public: | |
| 56 OutputSurfaceImpl(View* view, | |
| 57 const scoped_refptr<cc::ContextProvider>& context_provider, | |
| 58 Surface* surface, | |
| 59 cc::SurfaceIdAllocator* id_allocator); | |
| 60 ~OutputSurfaceImpl() override; | |
| 61 | |
| 62 // cc::OutputSurface: | |
| 63 void SwapBuffers(cc::CompositorFrame* frame) override; | |
| 64 | |
| 65 private: | |
| 66 View* view_; | |
| 67 Surface* surface_; | |
| 68 cc::SurfaceIdAllocator* id_allocator_; | |
| 69 cc::SurfaceId surface_id_; | |
| 70 gfx::Size surface_size_; | |
| 71 | |
| 72 DISALLOW_COPY_AND_ASSIGN(OutputSurfaceImpl); | |
| 73 }; | |
| 74 | |
| 75 OutputSurfaceImpl::OutputSurfaceImpl( | |
| 76 View* view, | |
| 77 const scoped_refptr<cc::ContextProvider>& context_provider, | |
| 78 Surface* surface, | |
| 79 cc::SurfaceIdAllocator* id_allocator) | |
| 80 : cc::OutputSurface(context_provider), | |
| 81 view_(view), | |
| 82 surface_(surface), | |
| 83 id_allocator_(id_allocator) { | |
| 84 capabilities_.delegated_rendering = true; | |
| 85 capabilities_.max_frames_pending = 1; | |
| 86 } | |
| 87 | |
| 88 OutputSurfaceImpl::~OutputSurfaceImpl() { | |
| 89 } | |
| 90 | |
| 91 void OutputSurfaceImpl::SwapBuffers(cc::CompositorFrame* frame) { | |
| 92 gfx::Size frame_size = | |
| 93 frame->delegated_frame_data->render_pass_list.back()->output_rect.size(); | |
| 94 if (frame_size != surface_size_) { | |
| 95 if (!surface_id_.is_null()) | |
| 96 surface_->DestroySurface(SurfaceId::From(surface_id_)); | |
| 97 surface_id_ = id_allocator_->GenerateId(); | |
| 98 surface_->CreateSurface(SurfaceId::From(surface_id_)); | |
| 99 view_->SetSurfaceId(SurfaceId::From(surface_id_)); | |
| 100 surface_size_ = frame_size; | |
| 101 } | |
| 102 | |
| 103 surface_->SubmitFrame(SurfaceId::From(surface_id_), Frame::From(*frame), | |
| 104 mojo::Closure()); | |
| 105 | |
| 106 client_->DidSwapBuffers(); | |
| 107 client_->DidSwapBuffersComplete(); | |
| 108 } | |
| 109 | |
| 110 } // namespace | |
| 111 | |
| 112 // PerViewManagerState --------------------------------------------------------- | |
| 113 | |
| 114 // State needed per ViewManager. Provides the real implementation of | |
| 115 // CreateOutputSurface. SurfaceBinding obtains a pointer to the | |
| 116 // PerViewManagerState appropriate for the ViewManager. PerViewManagerState is | |
| 117 // 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. | |
| 119 class SurfaceBinding::PerViewManagerState | |
| 120 : public base::RefCounted<PerViewManagerState> { | |
| 121 public: | |
| 122 static PerViewManagerState* Get(Shell* shell, ViewManager* view_manager); | |
| 123 | |
| 124 scoped_ptr<cc::OutputSurface> CreateOutputSurface(View* view); | |
| 125 | |
| 126 private: | |
| 127 typedef std::map<ViewManager*, PerViewManagerState*> ViewManagerToStateMap; | |
| 128 | |
| 129 friend class base::RefCounted<PerViewManagerState>; | |
| 130 | |
| 131 PerViewManagerState(Shell* shell, ViewManager* view_manager); | |
| 132 ~PerViewManagerState(); | |
| 133 | |
| 134 void Init(); | |
| 135 | |
| 136 // Callback when a Surface has been created. | |
| 137 void OnCreatedSurfaceConnection(SurfacePtr surface, uint32_t id_namespace); | |
| 138 | |
| 139 static base::LazyInstance< | |
| 140 base::ThreadLocalPointer<ViewManagerToStateMap>>::Leaky view_states; | |
| 141 | |
| 142 Shell* shell_; | |
| 143 ViewManager* view_manager_; | |
| 144 | |
| 145 // Set of state needed to create an OutputSurface. | |
| 146 scoped_ptr<SurfaceClient> surface_client_; | |
| 147 GpuPtr gpu_; | |
| 148 SurfacePtr surface_; | |
| 149 SurfacesServicePtr surfaces_service_; | |
| 150 scoped_ptr<cc::SurfaceIdAllocator> surface_id_allocator_; | |
| 151 | |
| 152 DISALLOW_COPY_AND_ASSIGN(PerViewManagerState); | |
| 153 }; | |
| 154 | |
| 155 // static | |
| 156 base::LazyInstance<base::ThreadLocalPointer< | |
| 157 SurfaceBinding::PerViewManagerState::ViewManagerToStateMap>>::Leaky | |
| 158 SurfaceBinding::PerViewManagerState::view_states; | |
| 159 | |
| 160 // static | |
| 161 SurfaceBinding::PerViewManagerState* SurfaceBinding::PerViewManagerState::Get( | |
| 162 Shell* shell, | |
| 163 ViewManager* view_manager) { | |
| 164 ViewManagerToStateMap* view_map = view_states.Pointer()->Get(); | |
| 165 if (!view_map) { | |
| 166 view_map = new ViewManagerToStateMap; | |
| 167 view_states.Pointer()->Set(view_map); | |
| 168 } | |
| 169 if (!(*view_map)[view_manager]) { | |
| 170 (*view_map)[view_manager] = new PerViewManagerState(shell, view_manager); | |
| 171 (*view_map)[view_manager]->Init(); | |
| 172 } | |
| 173 return (*view_map)[view_manager]; | |
| 174 } | |
| 175 | |
| 176 scoped_ptr<cc::OutputSurface> | |
| 177 SurfaceBinding::PerViewManagerState::CreateOutputSurface(View* view) { | |
| 178 // TODO(sky): figure out lifetime here. Do I need to worry about the return | |
| 179 // value outliving this? | |
| 180 CommandBufferPtr cb; | |
| 181 gpu_->CreateOffscreenGLES2Context(GetProxy(&cb)); | |
| 182 scoped_refptr<cc::ContextProvider> context_provider( | |
| 183 new ContextProviderMojo(cb.PassMessagePipe())); | |
| 184 return make_scoped_ptr(new OutputSurfaceImpl( | |
| 185 view, context_provider, surface_.get(), surface_id_allocator_.get())); | |
| 186 } | |
| 187 | |
| 188 SurfaceBinding::PerViewManagerState::PerViewManagerState( | |
| 189 Shell* shell, | |
| 190 ViewManager* view_manager) | |
| 191 : shell_(shell), view_manager_(view_manager) { | |
| 192 } | |
| 193 | |
| 194 SurfaceBinding::PerViewManagerState::~PerViewManagerState() { | |
| 195 ViewManagerToStateMap* view_map = view_states.Pointer()->Get(); | |
| 196 DCHECK(view_map); | |
| 197 DCHECK_EQ(this, (*view_map)[view_manager_]); | |
| 198 view_map->erase(view_manager_); | |
| 199 if (view_map->empty()) { | |
| 200 delete view_map; | |
| 201 view_states.Pointer()->Set(nullptr); | |
| 202 } | |
| 203 } | |
| 204 | |
| 205 void SurfaceBinding::PerViewManagerState::Init() { | |
| 206 DCHECK(!surfaces_service_.get()); | |
| 207 | |
| 208 ServiceProviderPtr surfaces_service_provider; | |
| 209 shell_->ConnectToApplication("mojo:surfaces_service", | |
| 210 GetProxy(&surfaces_service_provider)); | |
| 211 ConnectToService(surfaces_service_provider.get(), &surfaces_service_); | |
| 212 // base::Unretained is ok here as we block until the call is received. | |
| 213 surfaces_service_->CreateSurfaceConnection( | |
| 214 base::Bind(&PerViewManagerState::OnCreatedSurfaceConnection, | |
| 215 base::Unretained(this))); | |
| 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 | |
| 224 ServiceProviderPtr gpu_service_provider; | |
| 225 // TODO(jamesr): Should be mojo:gpu_service | |
| 226 shell_->ConnectToApplication("mojo:native_viewport_service", | |
| 227 GetProxy(&gpu_service_provider)); | |
| 228 ConnectToService(gpu_service_provider.get(), &gpu_); | |
| 229 } | |
| 230 | |
| 231 void SurfaceBinding::PerViewManagerState::OnCreatedSurfaceConnection( | |
| 232 SurfacePtr surface, | |
| 233 uint32_t id_namespace) { | |
| 234 surface_id_allocator_.reset(new cc::SurfaceIdAllocator(id_namespace)); | |
| 235 surface_ = surface.Pass(); | |
| 236 } | |
| 237 | |
| 238 // SurfaceBinding -------------------------------------------------------------- | |
| 239 | |
| 240 SurfaceBinding::SurfaceBinding(Shell* shell, View* view) | |
| 241 : view_(view), | |
| 242 state_(PerViewManagerState::Get(shell, view->view_manager())) { | |
| 243 } | |
| 244 | |
| 245 SurfaceBinding::~SurfaceBinding() { | |
| 246 } | |
| 247 | |
| 248 scoped_ptr<cc::OutputSurface> SurfaceBinding::CreateOutputSurface() { | |
| 249 return state_->CreateOutputSurface(view_); | |
| 250 } | |
| 251 | |
| 252 } // namespace mojo | |
| OLD | NEW |