| 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/memory/ptr_util.h" | 15 #include "base/memory/ptr_util.h" |
| 16 #include "base/threading/thread_local.h" | 16 #include "base/threading/thread_local.h" |
| 17 #include "cc/output/compositor_frame.h" | 17 #include "cc/output/compositor_frame.h" |
| 18 #include "cc/output/output_surface.h" | 18 #include "cc/output/output_surface.h" |
| 19 #include "cc/output/output_surface_client.h" | 19 #include "cc/output/output_surface_client.h" |
| 20 #include "cc/output/software_output_device.h" | 20 #include "cc/output/software_output_device.h" |
| 21 #include "cc/resources/shared_bitmap_manager.h" | 21 #include "cc/resources/shared_bitmap_manager.h" |
| 22 #include "components/mus/public/cpp/context_provider.h" | 22 #include "components/mus/public/cpp/context_provider.h" |
| 23 #include "components/mus/public/cpp/output_surface.h" | 23 #include "components/mus/public/cpp/output_surface.h" |
| 24 #include "components/mus/public/cpp/surfaces/surfaces_type_converters.h" | 24 #include "components/mus/public/cpp/surfaces/surfaces_type_converters.h" |
| 25 #include "components/mus/public/cpp/window.h" | 25 #include "components/mus/public/cpp/window.h" |
| 26 #include "components/mus/public/cpp/window_tree_connection.h" | 26 #include "components/mus/public/cpp/window_tree_client.h" |
| 27 #include "components/mus/public/interfaces/gpu.mojom.h" | 27 #include "components/mus/public/interfaces/gpu.mojom.h" |
| 28 #include "mojo/public/cpp/bindings/binding.h" | 28 #include "mojo/public/cpp/bindings/binding.h" |
| 29 #include "services/shell/public/cpp/connector.h" | 29 #include "services/shell/public/cpp/connector.h" |
| 30 #include "ui/gfx/geometry/mojo/geometry_type_converters.h" | 30 #include "ui/gfx/geometry/mojo/geometry_type_converters.h" |
| 31 #include "ui/views/mus/window_tree_host_mus.h" | 31 #include "ui/views/mus/window_tree_host_mus.h" |
| 32 | 32 |
| 33 namespace views { | 33 namespace views { |
| 34 | 34 |
| 35 // PerConnectionState ---------------------------------------------------------- | 35 // PerClientState -------------------------------------------------------------- |
| 36 | 36 |
| 37 // State needed per ViewManager. Provides the real implementation of | 37 // State needed per WindowTreeClient. Provides the real implementation of |
| 38 // CreateOutputSurface. SurfaceBinding obtains a pointer to the | 38 // CreateOutputSurface. SurfaceBinding obtains a pointer to the |
| 39 // PerConnectionState appropriate for the ViewManager. PerConnectionState is | 39 // PerClientState appropriate for the WindowTreeClient. PerClientState is |
| 40 // stored in a thread local map. When no more refereces to a PerConnectionState | 40 // stored in a thread local map. When no more refereces to a PerClientState |
| 41 // remain the PerConnectionState is deleted and the underlying map cleaned up. | 41 // remain the PerClientState is deleted and the underlying map cleaned up. |
| 42 class SurfaceBinding::PerConnectionState | 42 class SurfaceBinding::PerClientState : public base::RefCounted<PerClientState> { |
| 43 : public base::RefCounted<PerConnectionState> { | |
| 44 public: | 43 public: |
| 45 static PerConnectionState* Get(shell::Connector* connector, | 44 static PerClientState* Get(shell::Connector* connector, |
| 46 mus::WindowTreeConnection* connection); | 45 mus::WindowTreeClient* client); |
| 47 | 46 |
| 48 std::unique_ptr<cc::OutputSurface> CreateOutputSurface( | 47 std::unique_ptr<cc::OutputSurface> CreateOutputSurface( |
| 49 mus::Window* window, | 48 mus::Window* window, |
| 50 mus::mojom::SurfaceType type); | 49 mus::mojom::SurfaceType type); |
| 51 | 50 |
| 52 private: | 51 private: |
| 53 typedef std::map<mus::WindowTreeConnection*, PerConnectionState*> | 52 typedef std::map<mus::WindowTreeClient*, PerClientState*> ClientToStateMap; |
| 54 ConnectionToStateMap; | |
| 55 | 53 |
| 56 friend class base::RefCounted<PerConnectionState>; | 54 friend class base::RefCounted<PerClientState>; |
| 57 | 55 |
| 58 PerConnectionState(shell::Connector* connector, | 56 PerClientState(shell::Connector* connector, |
| 59 mus::WindowTreeConnection* connection); | 57 mus::WindowTreeClient* client); |
| 60 ~PerConnectionState(); | 58 ~PerClientState(); |
| 61 | 59 |
| 62 void Init(); | 60 void Init(); |
| 63 | 61 |
| 64 static base::LazyInstance< | 62 static base::LazyInstance< |
| 65 base::ThreadLocalPointer<ConnectionToStateMap>>::Leaky window_states; | 63 base::ThreadLocalPointer<ClientToStateMap>>::Leaky window_states; |
| 66 | 64 |
| 67 shell::Connector* connector_; | 65 shell::Connector* connector_; |
| 68 mus::WindowTreeConnection* connection_; | 66 mus::WindowTreeClient* client_; |
| 69 | 67 |
| 70 // Set of state needed to create an OutputSurface. | 68 // Set of state needed to create an OutputSurface. |
| 71 mus::mojom::GpuPtr gpu_; | 69 mus::mojom::GpuPtr gpu_; |
| 72 | 70 |
| 73 DISALLOW_COPY_AND_ASSIGN(PerConnectionState); | 71 DISALLOW_COPY_AND_ASSIGN(PerClientState); |
| 74 }; | 72 }; |
| 75 | 73 |
| 76 // static | 74 // static |
| 77 base::LazyInstance<base::ThreadLocalPointer< | 75 base::LazyInstance<base::ThreadLocalPointer< |
| 78 SurfaceBinding::PerConnectionState::ConnectionToStateMap>>::Leaky | 76 SurfaceBinding::PerClientState::ClientToStateMap>>::Leaky |
| 79 SurfaceBinding::PerConnectionState::window_states; | 77 SurfaceBinding::PerClientState::window_states; |
| 80 | 78 |
| 81 // static | 79 // static |
| 82 SurfaceBinding::PerConnectionState* SurfaceBinding::PerConnectionState::Get( | 80 SurfaceBinding::PerClientState* SurfaceBinding::PerClientState::Get( |
| 83 shell::Connector* connector, | 81 shell::Connector* connector, |
| 84 mus::WindowTreeConnection* connection) { | 82 mus::WindowTreeClient* client) { |
| 85 ConnectionToStateMap* window_map = window_states.Pointer()->Get(); | 83 ClientToStateMap* window_map = window_states.Pointer()->Get(); |
| 86 if (!window_map) { | 84 if (!window_map) { |
| 87 window_map = new ConnectionToStateMap; | 85 window_map = new ClientToStateMap; |
| 88 window_states.Pointer()->Set(window_map); | 86 window_states.Pointer()->Set(window_map); |
| 89 } | 87 } |
| 90 if (!(*window_map)[connection]) { | 88 if (!(*window_map)[client]) { |
| 91 (*window_map)[connection] = new PerConnectionState(connector, connection); | 89 (*window_map)[client] = new PerClientState(connector, client); |
| 92 (*window_map)[connection]->Init(); | 90 (*window_map)[client]->Init(); |
| 93 } | 91 } |
| 94 return (*window_map)[connection]; | 92 return (*window_map)[client]; |
| 95 } | 93 } |
| 96 | 94 |
| 97 std::unique_ptr<cc::OutputSurface> | 95 std::unique_ptr<cc::OutputSurface> |
| 98 SurfaceBinding::PerConnectionState::CreateOutputSurface( | 96 SurfaceBinding::PerClientState::CreateOutputSurface( |
| 99 mus::Window* window, | 97 mus::Window* window, |
| 100 mus::mojom::SurfaceType surface_type) { | 98 mus::mojom::SurfaceType surface_type) { |
| 101 if (gpu_.encountered_error()) | 99 if (gpu_.encountered_error()) |
| 102 return nullptr; | 100 return nullptr; |
| 103 // TODO(sky): figure out lifetime here. Do I need to worry about the return | 101 // TODO(sky): figure out lifetime here. Do I need to worry about the return |
| 104 // value outliving this? | 102 // value outliving this? |
| 105 mus::mojom::CommandBufferPtr cb; | 103 mus::mojom::CommandBufferPtr cb; |
| 106 gpu_->CreateOffscreenGLES2Context(GetProxy(&cb)); | 104 gpu_->CreateOffscreenGLES2Context(GetProxy(&cb)); |
| 107 | 105 |
| 108 scoped_refptr<cc::ContextProvider> context_provider( | 106 scoped_refptr<cc::ContextProvider> context_provider( |
| 109 new mus::ContextProvider(cb.PassInterface().PassHandle())); | 107 new mus::ContextProvider(cb.PassInterface().PassHandle())); |
| 110 return base::WrapUnique(new mus::OutputSurface( | 108 return base::WrapUnique(new mus::OutputSurface( |
| 111 context_provider, window->RequestSurface(surface_type))); | 109 context_provider, window->RequestSurface(surface_type))); |
| 112 } | 110 } |
| 113 | 111 |
| 114 SurfaceBinding::PerConnectionState::PerConnectionState( | 112 SurfaceBinding::PerClientState::PerClientState( |
| 115 shell::Connector* connector, | 113 shell::Connector* connector, |
| 116 mus::WindowTreeConnection* connection) | 114 mus::WindowTreeClient* client) |
| 117 : connector_(connector), connection_(connection) {} | 115 : connector_(connector), client_(client) {} |
| 118 | 116 |
| 119 SurfaceBinding::PerConnectionState::~PerConnectionState() { | 117 SurfaceBinding::PerClientState::~PerClientState() { |
| 120 ConnectionToStateMap* window_map = window_states.Pointer()->Get(); | 118 ClientToStateMap* window_map = window_states.Pointer()->Get(); |
| 121 DCHECK(window_map); | 119 DCHECK(window_map); |
| 122 DCHECK_EQ(this, (*window_map)[connection_]); | 120 DCHECK_EQ(this, (*window_map)[client_]); |
| 123 window_map->erase(connection_); | 121 window_map->erase(client_); |
| 124 if (window_map->empty()) { | 122 if (window_map->empty()) { |
| 125 delete window_map; | 123 delete window_map; |
| 126 window_states.Pointer()->Set(nullptr); | 124 window_states.Pointer()->Set(nullptr); |
| 127 } | 125 } |
| 128 } | 126 } |
| 129 | 127 |
| 130 void SurfaceBinding::PerConnectionState::Init() { | 128 void SurfaceBinding::PerClientState::Init() { |
| 131 connector_->ConnectToInterface("mojo:mus", &gpu_); | 129 connector_->ConnectToInterface("mojo:mus", &gpu_); |
| 132 | 130 |
| 133 // TODO(sad): If connection is lost (e.g. if gpu crashes), then the | 131 // TODO(sad): If service connection is lost (e.g. if gpu crashes), then the |
| 134 // connections need to be restored. https://crbug.com/613366 | 132 // clients need to be restored. https://crbug.com/613366 |
| 135 // TODO(rockot|yzshen): It is necessary to install a connection-error handler, | 133 // TODO(rockot|yzshen): It is necessary to install a connection-error handler, |
| 136 // even if the handler doesn't actually do anything. https://crbug.com/613371 | 134 // even if the handler doesn't actually do anything. https://crbug.com/613371 |
| 137 gpu_.set_connection_error_handler([]{}); | 135 gpu_.set_connection_error_handler([]{}); |
| 138 } | 136 } |
| 139 | 137 |
| 140 // SurfaceBinding -------------------------------------------------------------- | 138 // SurfaceBinding -------------------------------------------------------------- |
| 141 | 139 |
| 142 SurfaceBinding::SurfaceBinding(shell::Connector* connector, | 140 SurfaceBinding::SurfaceBinding(shell::Connector* connector, |
| 143 mus::Window* window, | 141 mus::Window* window, |
| 144 mus::mojom::SurfaceType surface_type) | 142 mus::mojom::SurfaceType surface_type) |
| 145 : window_(window), | 143 : window_(window), |
| 146 surface_type_(surface_type), | 144 surface_type_(surface_type), |
| 147 state_(PerConnectionState::Get(connector, window->connection())) {} | 145 state_(PerClientState::Get(connector, window->window_tree())) {} |
| 148 | 146 |
| 149 SurfaceBinding::~SurfaceBinding() {} | 147 SurfaceBinding::~SurfaceBinding() {} |
| 150 | 148 |
| 151 std::unique_ptr<cc::OutputSurface> SurfaceBinding::CreateOutputSurface() { | 149 std::unique_ptr<cc::OutputSurface> SurfaceBinding::CreateOutputSurface() { |
| 152 return state_->CreateOutputSurface(window_, surface_type_); | 150 return state_->CreateOutputSurface(window_, surface_type_); |
| 153 } | 151 } |
| 154 | 152 |
| 155 } // namespace views | 153 } // namespace views |
| OLD | NEW |