| 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 "ui/views/mus/surface_binding.h" | 5 #include "ui/views/mus/surface_binding.h" |
| 6 | 6 |
| 7 #include <stdint.h> | 7 #include <stdint.h> |
| 8 | 8 |
| 9 #include <map> | 9 #include <map> |
| 10 #include <utility> | 10 #include <utility> |
| 11 | 11 |
| 12 #include "base/bind.h" | 12 #include "base/bind.h" |
| 13 #include "base/lazy_instance.h" | 13 #include "base/lazy_instance.h" |
| 14 #include "base/macros.h" | 14 #include "base/macros.h" |
| 15 #include "base/threading/thread_local.h" | 15 #include "base/threading/thread_local.h" |
| 16 #include "cc/output/compositor_frame.h" | 16 #include "cc/output/compositor_frame.h" |
| 17 #include "cc/output/output_surface.h" | 17 #include "cc/output/output_surface.h" |
| 18 #include "cc/output/output_surface_client.h" | 18 #include "cc/output/output_surface_client.h" |
| 19 #include "cc/output/software_output_device.h" | 19 #include "cc/output/software_output_device.h" |
| 20 #include "cc/resources/shared_bitmap_manager.h" | 20 #include "cc/resources/shared_bitmap_manager.h" |
| 21 #include "components/mus/public/cpp/context_provider.h" | 21 #include "components/mus/public/cpp/context_provider.h" |
| 22 #include "components/mus/public/cpp/output_surface.h" | 22 #include "components/mus/public/cpp/output_surface.h" |
| 23 #include "components/mus/public/cpp/window.h" | 23 #include "components/mus/public/cpp/window.h" |
| 24 #include "components/mus/public/cpp/window_tree_connection.h" | 24 #include "components/mus/public/cpp/window_tree_connection.h" |
| 25 #include "components/mus/public/interfaces/gpu.mojom.h" | 25 #include "components/mus/public/interfaces/gpu.mojom.h" |
| 26 #include "mojo/converters/geometry/geometry_type_converters.h" | 26 #include "mojo/converters/geometry/geometry_type_converters.h" |
| 27 #include "mojo/converters/surfaces/surfaces_type_converters.h" | 27 #include "mojo/converters/surfaces/surfaces_type_converters.h" |
| 28 #include "mojo/public/cpp/bindings/binding.h" | 28 #include "mojo/public/cpp/bindings/binding.h" |
| 29 #include "mojo/shell/public/cpp/application_impl.h" | 29 #include "mojo/shell/public/cpp/shell.h" |
| 30 #include "mojo/shell/public/cpp/connect.h" | |
| 31 #include "mojo/shell/public/interfaces/shell.mojom.h" | |
| 32 #include "ui/views/mus/window_tree_host_mus.h" | 30 #include "ui/views/mus/window_tree_host_mus.h" |
| 33 | 31 |
| 34 namespace views { | 32 namespace views { |
| 35 namespace { | |
| 36 void OnGotRemoteIDs(uint32_t remote_id, uint32_t content_handler_id) {} | |
| 37 } // namespace | |
| 38 | 33 |
| 39 // PerConnectionState ---------------------------------------------------------- | 34 // PerConnectionState ---------------------------------------------------------- |
| 40 | 35 |
| 41 // State needed per ViewManager. Provides the real implementation of | 36 // State needed per ViewManager. Provides the real implementation of |
| 42 // CreateOutputSurface. SurfaceBinding obtains a pointer to the | 37 // CreateOutputSurface. SurfaceBinding obtains a pointer to the |
| 43 // PerConnectionState appropriate for the ViewManager. PerConnectionState is | 38 // PerConnectionState appropriate for the ViewManager. PerConnectionState is |
| 44 // stored in a thread local map. When no more refereces to a PerConnectionState | 39 // stored in a thread local map. When no more refereces to a PerConnectionState |
| 45 // remain the PerConnectionState is deleted and the underlying map cleaned up. | 40 // remain the PerConnectionState is deleted and the underlying map cleaned up. |
| 46 class SurfaceBinding::PerConnectionState | 41 class SurfaceBinding::PerConnectionState |
| 47 : public base::RefCounted<PerConnectionState> { | 42 : public base::RefCounted<PerConnectionState> { |
| 48 public: | 43 public: |
| 49 static PerConnectionState* Get(mojo::shell::mojom::Shell* shell, | 44 static PerConnectionState* Get(mojo::Shell* shell, |
| 50 mus::WindowTreeConnection* connection); | 45 mus::WindowTreeConnection* connection); |
| 51 | 46 |
| 52 scoped_ptr<cc::OutputSurface> CreateOutputSurface( | 47 scoped_ptr<cc::OutputSurface> CreateOutputSurface( |
| 53 mus::Window* window, | 48 mus::Window* window, |
| 54 mus::mojom::SurfaceType type); | 49 mus::mojom::SurfaceType type); |
| 55 | 50 |
| 56 private: | 51 private: |
| 57 typedef std::map<mus::WindowTreeConnection*, PerConnectionState*> | 52 typedef std::map<mus::WindowTreeConnection*, PerConnectionState*> |
| 58 ConnectionToStateMap; | 53 ConnectionToStateMap; |
| 59 | 54 |
| 60 friend class base::RefCounted<PerConnectionState>; | 55 friend class base::RefCounted<PerConnectionState>; |
| 61 | 56 |
| 62 PerConnectionState(mojo::shell::mojom::Shell* shell, | 57 PerConnectionState(mojo::Shell* shell, mus::WindowTreeConnection* connection); |
| 63 mus::WindowTreeConnection* connection); | |
| 64 ~PerConnectionState(); | 58 ~PerConnectionState(); |
| 65 | 59 |
| 66 void Init(); | 60 void Init(); |
| 67 | 61 |
| 68 static base::LazyInstance< | 62 static base::LazyInstance< |
| 69 base::ThreadLocalPointer<ConnectionToStateMap>>::Leaky window_states; | 63 base::ThreadLocalPointer<ConnectionToStateMap>>::Leaky window_states; |
| 70 | 64 |
| 71 mojo::shell::mojom::Shell* shell_; | 65 mojo::Shell* shell_; |
| 72 mus::WindowTreeConnection* connection_; | 66 mus::WindowTreeConnection* connection_; |
| 73 | 67 |
| 74 // Set of state needed to create an OutputSurface. | 68 // Set of state needed to create an OutputSurface. |
| 75 mus::mojom::GpuPtr gpu_; | 69 mus::mojom::GpuPtr gpu_; |
| 76 | 70 |
| 77 DISALLOW_COPY_AND_ASSIGN(PerConnectionState); | 71 DISALLOW_COPY_AND_ASSIGN(PerConnectionState); |
| 78 }; | 72 }; |
| 79 | 73 |
| 80 // static | 74 // static |
| 81 base::LazyInstance<base::ThreadLocalPointer< | 75 base::LazyInstance<base::ThreadLocalPointer< |
| 82 SurfaceBinding::PerConnectionState::ConnectionToStateMap>>::Leaky | 76 SurfaceBinding::PerConnectionState::ConnectionToStateMap>>::Leaky |
| 83 SurfaceBinding::PerConnectionState::window_states; | 77 SurfaceBinding::PerConnectionState::window_states; |
| 84 | 78 |
| 85 // static | 79 // static |
| 86 SurfaceBinding::PerConnectionState* SurfaceBinding::PerConnectionState::Get( | 80 SurfaceBinding::PerConnectionState* SurfaceBinding::PerConnectionState::Get( |
| 87 mojo::shell::mojom::Shell* shell, | 81 mojo::Shell* shell, |
| 88 mus::WindowTreeConnection* connection) { | 82 mus::WindowTreeConnection* connection) { |
| 89 ConnectionToStateMap* window_map = window_states.Pointer()->Get(); | 83 ConnectionToStateMap* window_map = window_states.Pointer()->Get(); |
| 90 if (!window_map) { | 84 if (!window_map) { |
| 91 window_map = new ConnectionToStateMap; | 85 window_map = new ConnectionToStateMap; |
| 92 window_states.Pointer()->Set(window_map); | 86 window_states.Pointer()->Set(window_map); |
| 93 } | 87 } |
| 94 if (!(*window_map)[connection]) { | 88 if (!(*window_map)[connection]) { |
| 95 (*window_map)[connection] = new PerConnectionState(shell, connection); | 89 (*window_map)[connection] = new PerConnectionState(shell, connection); |
| 96 (*window_map)[connection]->Init(); | 90 (*window_map)[connection]->Init(); |
| 97 } | 91 } |
| 98 return (*window_map)[connection]; | 92 return (*window_map)[connection]; |
| 99 } | 93 } |
| 100 | 94 |
| 101 scoped_ptr<cc::OutputSurface> | 95 scoped_ptr<cc::OutputSurface> |
| 102 SurfaceBinding::PerConnectionState::CreateOutputSurface( | 96 SurfaceBinding::PerConnectionState::CreateOutputSurface( |
| 103 mus::Window* window, | 97 mus::Window* window, |
| 104 mus::mojom::SurfaceType surface_type) { | 98 mus::mojom::SurfaceType surface_type) { |
| 105 // TODO(sky): figure out lifetime here. Do I need to worry about the return | 99 // TODO(sky): figure out lifetime here. Do I need to worry about the return |
| 106 // value outliving this? | 100 // value outliving this? |
| 107 mus::mojom::CommandBufferPtr cb; | 101 mus::mojom::CommandBufferPtr cb; |
| 108 gpu_->CreateOffscreenGLES2Context(GetProxy(&cb)); | 102 gpu_->CreateOffscreenGLES2Context(GetProxy(&cb)); |
| 109 | 103 |
| 110 scoped_refptr<cc::ContextProvider> context_provider( | 104 scoped_refptr<cc::ContextProvider> context_provider( |
| 111 new mus::ContextProvider(cb.PassInterface().PassHandle())); | 105 new mus::ContextProvider(cb.PassInterface().PassHandle())); |
| 112 return make_scoped_ptr(new mus::OutputSurface( | 106 return make_scoped_ptr(new mus::OutputSurface( |
| 113 context_provider, window->RequestSurface(surface_type))); | 107 context_provider, window->RequestSurface(surface_type))); |
| 114 } | 108 } |
| 115 | 109 |
| 116 SurfaceBinding::PerConnectionState::PerConnectionState( | 110 SurfaceBinding::PerConnectionState::PerConnectionState( |
| 117 mojo::shell::mojom::Shell* shell, | 111 mojo::Shell* shell, |
| 118 mus::WindowTreeConnection* connection) | 112 mus::WindowTreeConnection* connection) |
| 119 : shell_(shell), connection_(connection) {} | 113 : shell_(shell), connection_(connection) {} |
| 120 | 114 |
| 121 SurfaceBinding::PerConnectionState::~PerConnectionState() { | 115 SurfaceBinding::PerConnectionState::~PerConnectionState() { |
| 122 ConnectionToStateMap* window_map = window_states.Pointer()->Get(); | 116 ConnectionToStateMap* window_map = window_states.Pointer()->Get(); |
| 123 DCHECK(window_map); | 117 DCHECK(window_map); |
| 124 DCHECK_EQ(this, (*window_map)[connection_]); | 118 DCHECK_EQ(this, (*window_map)[connection_]); |
| 125 window_map->erase(connection_); | 119 window_map->erase(connection_); |
| 126 if (window_map->empty()) { | 120 if (window_map->empty()) { |
| 127 delete window_map; | 121 delete window_map; |
| 128 window_states.Pointer()->Set(nullptr); | 122 window_states.Pointer()->Set(nullptr); |
| 129 } | 123 } |
| 130 } | 124 } |
| 131 | 125 |
| 132 void SurfaceBinding::PerConnectionState::Init() { | 126 void SurfaceBinding::PerConnectionState::Init() { |
| 133 mojo::ServiceProviderPtr service_provider; | 127 shell_->ConnectToService("mojo:mus", &gpu_); |
| 134 mojo::URLRequestPtr request(mojo::URLRequest::New()); | |
| 135 request->url = mojo::String::From("mojo:mus"); | |
| 136 shell_->ConnectToApplication(std::move(request), GetProxy(&service_provider), | |
| 137 nullptr, | |
| 138 mojo::CreatePermissiveCapabilityFilter(), | |
| 139 base::Bind(&OnGotRemoteIDs)); | |
| 140 ConnectToService(service_provider.get(), &gpu_); | |
| 141 } | 128 } |
| 142 | 129 |
| 143 // SurfaceBinding -------------------------------------------------------------- | 130 // SurfaceBinding -------------------------------------------------------------- |
| 144 | 131 |
| 145 SurfaceBinding::SurfaceBinding(mojo::shell::mojom::Shell* shell, | 132 SurfaceBinding::SurfaceBinding(mojo::Shell* shell, |
| 146 mus::Window* window, | 133 mus::Window* window, |
| 147 mus::mojom::SurfaceType surface_type) | 134 mus::mojom::SurfaceType surface_type) |
| 148 : window_(window), | 135 : window_(window), |
| 149 surface_type_(surface_type), | 136 surface_type_(surface_type), |
| 150 state_(PerConnectionState::Get(shell, window->connection())) {} | 137 state_(PerConnectionState::Get(shell, window->connection())) {} |
| 151 | 138 |
| 152 SurfaceBinding::~SurfaceBinding() {} | 139 SurfaceBinding::~SurfaceBinding() {} |
| 153 | 140 |
| 154 scoped_ptr<cc::OutputSurface> SurfaceBinding::CreateOutputSurface() { | 141 scoped_ptr<cc::OutputSurface> SurfaceBinding::CreateOutputSurface() { |
| 155 return state_->CreateOutputSurface(window_, surface_type_); | 142 return state_->CreateOutputSurface(window_, surface_type_); |
| 156 } | 143 } |
| 157 | 144 |
| 158 } // namespace views | 145 } // namespace views |
| OLD | NEW |