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 #include "mandoline/ui/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 "components/gpu/public/interfaces/gpu.mojom.h" |
| 18 #include "components/surfaces/public/interfaces/surfaces.mojom.h" |
| 19 #include "components/view_manager/public/cpp/view.h" |
| 20 #include "components/view_manager/public/cpp/view_manager.h" |
| 21 #include "mandoline/ui/aura/window_tree_host_mojo.h" |
| 22 #include "mojo/cc/context_provider_mojo.h" |
| 23 #include "mojo/converters/geometry/geometry_type_converters.h" |
| 24 #include "mojo/converters/surfaces/surfaces_type_converters.h" |
| 25 #include "mojo/public/cpp/application/connect.h" |
| 26 #include "mojo/public/interfaces/application/shell.mojom.h" |
| 27 |
| 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_, mojo::Frame::From(*frame), mojo::Closure()); |
| 93 |
| 94 client_->DidSwapBuffers(); |
| 95 client_->DidSwapBuffersComplete(); |
| 96 } |
| 97 |
| 98 } // namespace |
| 99 |
| 100 // PerViewManagerState --------------------------------------------------------- |
| 101 |
| 102 // State needed per ViewManager. Provides the real implementation of |
| 103 // CreateOutputSurface. SurfaceBinding obtains a pointer to the |
| 104 // PerViewManagerState appropriate for the ViewManager. PerViewManagerState is |
| 105 // stored in a thread local map. When no more refereces to a PerViewManagerState |
| 106 // remain the PerViewManagerState is deleted and the underlying map cleaned up. |
| 107 class SurfaceBinding::PerViewManagerState |
| 108 : public base::RefCounted<PerViewManagerState>, |
| 109 public mojo::ResourceReturner { |
| 110 public: |
| 111 static PerViewManagerState* Get(mojo::Shell* shell, |
| 112 mojo::ViewManager* view_manager); |
| 113 |
| 114 scoped_ptr<cc::OutputSurface> CreateOutputSurface(mojo::View* view); |
| 115 |
| 116 private: |
| 117 typedef std::map<mojo::ViewManager*, |
| 118 PerViewManagerState*> ViewManagerToStateMap; |
| 119 |
| 120 friend class base::RefCounted<PerViewManagerState>; |
| 121 |
| 122 PerViewManagerState(mojo::Shell* shell, mojo::ViewManager* view_manager); |
| 123 ~PerViewManagerState() override; |
| 124 |
| 125 void Init(); |
| 126 |
| 127 // mojo::ResourceReturner: |
| 128 void ReturnResources( |
| 129 mojo::Array<mojo::ReturnedResourcePtr> resources) override; |
| 130 |
| 131 void SetIdNamespace(uint32_t id_namespace); |
| 132 |
| 133 static base::LazyInstance< |
| 134 base::ThreadLocalPointer<ViewManagerToStateMap>>::Leaky view_states; |
| 135 |
| 136 mojo::Shell* shell_; |
| 137 mojo::ViewManager* view_manager_; |
| 138 |
| 139 // Set of state needed to create an OutputSurface. |
| 140 mojo::GpuPtr gpu_; |
| 141 mojo::SurfacePtr surface_; |
| 142 mojo::Binding<mojo::ResourceReturner> returner_binding_; |
| 143 uint32_t id_namespace_; |
| 144 uint32_t next_local_id_; |
| 145 |
| 146 DISALLOW_COPY_AND_ASSIGN(PerViewManagerState); |
| 147 }; |
| 148 |
| 149 // static |
| 150 base::LazyInstance<base::ThreadLocalPointer< |
| 151 SurfaceBinding::PerViewManagerState::ViewManagerToStateMap>>::Leaky |
| 152 SurfaceBinding::PerViewManagerState::view_states; |
| 153 |
| 154 // static |
| 155 SurfaceBinding::PerViewManagerState* SurfaceBinding::PerViewManagerState::Get( |
| 156 mojo::Shell* shell, |
| 157 mojo::ViewManager* view_manager) { |
| 158 ViewManagerToStateMap* view_map = view_states.Pointer()->Get(); |
| 159 if (!view_map) { |
| 160 view_map = new ViewManagerToStateMap; |
| 161 view_states.Pointer()->Set(view_map); |
| 162 } |
| 163 if (!(*view_map)[view_manager]) { |
| 164 (*view_map)[view_manager] = new PerViewManagerState(shell, view_manager); |
| 165 (*view_map)[view_manager]->Init(); |
| 166 } |
| 167 return (*view_map)[view_manager]; |
| 168 } |
| 169 |
| 170 scoped_ptr<cc::OutputSurface> |
| 171 SurfaceBinding::PerViewManagerState::CreateOutputSurface(mojo::View* view) { |
| 172 // TODO(sky): figure out lifetime here. Do I need to worry about the return |
| 173 // value outliving this? |
| 174 mojo::CommandBufferPtr cb; |
| 175 gpu_->CreateOffscreenGLES2Context(GetProxy(&cb)); |
| 176 scoped_refptr<cc::ContextProvider> context_provider( |
| 177 new mojo::ContextProviderMojo(cb.PassMessagePipe())); |
| 178 return make_scoped_ptr(new OutputSurfaceImpl( |
| 179 view, context_provider, surface_.get(), id_namespace_, &next_local_id_)); |
| 180 } |
| 181 |
| 182 SurfaceBinding::PerViewManagerState::PerViewManagerState( |
| 183 mojo::Shell* shell, |
| 184 mojo::ViewManager* view_manager) |
| 185 : shell_(shell), |
| 186 view_manager_(view_manager), |
| 187 returner_binding_(this), |
| 188 id_namespace_(0u), |
| 189 next_local_id_(0u) { |
| 190 } |
| 191 |
| 192 SurfaceBinding::PerViewManagerState::~PerViewManagerState() { |
| 193 ViewManagerToStateMap* view_map = view_states.Pointer()->Get(); |
| 194 DCHECK(view_map); |
| 195 DCHECK_EQ(this, (*view_map)[view_manager_]); |
| 196 view_map->erase(view_manager_); |
| 197 if (view_map->empty()) { |
| 198 delete view_map; |
| 199 view_states.Pointer()->Set(nullptr); |
| 200 } |
| 201 } |
| 202 |
| 203 void SurfaceBinding::PerViewManagerState::Init() { |
| 204 DCHECK(!surface_.get()); |
| 205 |
| 206 mojo::ServiceProviderPtr surfaces_service_provider; |
| 207 shell_->ConnectToApplication("mojo:surfaces_service", |
| 208 GetProxy(&surfaces_service_provider), |
| 209 nullptr); |
| 210 ConnectToService(surfaces_service_provider.get(), &surface_); |
| 211 surface_->GetIdNamespace( |
| 212 base::Bind(&SurfaceBinding::PerViewManagerState::SetIdNamespace, |
| 213 base::Unretained(this))); |
| 214 // Block until we receive our id namespace. |
| 215 surface_.WaitForIncomingMethodCall(); |
| 216 DCHECK_NE(0u, id_namespace_); |
| 217 |
| 218 mojo::ResourceReturnerPtr returner_ptr; |
| 219 returner_binding_.Bind(GetProxy(&returner_ptr)); |
| 220 surface_->SetResourceReturner(returner_ptr.Pass()); |
| 221 |
| 222 mojo::ServiceProviderPtr gpu_service_provider; |
| 223 // TODO(jamesr): Should be mojo:gpu_service |
| 224 shell_->ConnectToApplication("mojo:native_viewport_service", |
| 225 GetProxy(&gpu_service_provider), |
| 226 nullptr); |
| 227 ConnectToService(gpu_service_provider.get(), &gpu_); |
| 228 } |
| 229 |
| 230 void SurfaceBinding::PerViewManagerState::SetIdNamespace( |
| 231 uint32_t id_namespace) { |
| 232 id_namespace_ = id_namespace; |
| 233 } |
| 234 |
| 235 void SurfaceBinding::PerViewManagerState::ReturnResources( |
| 236 mojo::Array<mojo::ReturnedResourcePtr> resources) { |
| 237 } |
| 238 |
| 239 // SurfaceBinding -------------------------------------------------------------- |
| 240 |
| 241 SurfaceBinding::SurfaceBinding(mojo::Shell* shell, mojo::View* view) |
| 242 : view_(view), |
| 243 state_(PerViewManagerState::Get(shell, view->view_manager())) { |
| 244 } |
| 245 |
| 246 SurfaceBinding::~SurfaceBinding() { |
| 247 } |
| 248 |
| 249 scoped_ptr<cc::OutputSurface> SurfaceBinding::CreateOutputSurface() { |
| 250 return state_->CreateOutputSurface(view_); |
| 251 } |
| 252 |
| 253 } // namespace mandoline |
OLD | NEW |