| 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> |
| (...skipping 24 matching lines...) Expand all Loading... |
| 35 // PerConnectionState ---------------------------------------------------------- | 35 // PerConnectionState ---------------------------------------------------------- |
| 36 | 36 |
| 37 // State needed per ViewManager. Provides the real implementation of | 37 // State needed per ViewManager. 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 // PerConnectionState appropriate for the ViewManager. PerConnectionState 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 PerConnectionState |
| 41 // remain the PerConnectionState is deleted and the underlying map cleaned up. | 41 // remain the PerConnectionState is deleted and the underlying map cleaned up. |
| 42 class SurfaceBinding::PerConnectionState | 42 class SurfaceBinding::PerConnectionState |
| 43 : public base::RefCounted<PerConnectionState> { | 43 : public base::RefCounted<PerConnectionState> { |
| 44 public: | 44 public: |
| 45 static PerConnectionState* Get(mojo::Connector* connector, | 45 static PerConnectionState* Get(shell::Connector* connector, |
| 46 mus::WindowTreeConnection* connection); | 46 mus::WindowTreeConnection* connection); |
| 47 | 47 |
| 48 std::unique_ptr<cc::OutputSurface> CreateOutputSurface( | 48 std::unique_ptr<cc::OutputSurface> CreateOutputSurface( |
| 49 mus::Window* window, | 49 mus::Window* window, |
| 50 mus::mojom::SurfaceType type); | 50 mus::mojom::SurfaceType type); |
| 51 | 51 |
| 52 private: | 52 private: |
| 53 typedef std::map<mus::WindowTreeConnection*, PerConnectionState*> | 53 typedef std::map<mus::WindowTreeConnection*, PerConnectionState*> |
| 54 ConnectionToStateMap; | 54 ConnectionToStateMap; |
| 55 | 55 |
| 56 friend class base::RefCounted<PerConnectionState>; | 56 friend class base::RefCounted<PerConnectionState>; |
| 57 | 57 |
| 58 PerConnectionState(mojo::Connector* connector, | 58 PerConnectionState(shell::Connector* connector, |
| 59 mus::WindowTreeConnection* connection); | 59 mus::WindowTreeConnection* connection); |
| 60 ~PerConnectionState(); | 60 ~PerConnectionState(); |
| 61 | 61 |
| 62 void Init(); | 62 void Init(); |
| 63 | 63 |
| 64 static base::LazyInstance< | 64 static base::LazyInstance< |
| 65 base::ThreadLocalPointer<ConnectionToStateMap>>::Leaky window_states; | 65 base::ThreadLocalPointer<ConnectionToStateMap>>::Leaky window_states; |
| 66 | 66 |
| 67 mojo::Connector* connector_; | 67 shell::Connector* connector_; |
| 68 mus::WindowTreeConnection* connection_; | 68 mus::WindowTreeConnection* connection_; |
| 69 | 69 |
| 70 // Set of state needed to create an OutputSurface. | 70 // Set of state needed to create an OutputSurface. |
| 71 mus::mojom::GpuPtr gpu_; | 71 mus::mojom::GpuPtr gpu_; |
| 72 | 72 |
| 73 DISALLOW_COPY_AND_ASSIGN(PerConnectionState); | 73 DISALLOW_COPY_AND_ASSIGN(PerConnectionState); |
| 74 }; | 74 }; |
| 75 | 75 |
| 76 // static | 76 // static |
| 77 base::LazyInstance<base::ThreadLocalPointer< | 77 base::LazyInstance<base::ThreadLocalPointer< |
| 78 SurfaceBinding::PerConnectionState::ConnectionToStateMap>>::Leaky | 78 SurfaceBinding::PerConnectionState::ConnectionToStateMap>>::Leaky |
| 79 SurfaceBinding::PerConnectionState::window_states; | 79 SurfaceBinding::PerConnectionState::window_states; |
| 80 | 80 |
| 81 // static | 81 // static |
| 82 SurfaceBinding::PerConnectionState* SurfaceBinding::PerConnectionState::Get( | 82 SurfaceBinding::PerConnectionState* SurfaceBinding::PerConnectionState::Get( |
| 83 mojo::Connector* connector, | 83 shell::Connector* connector, |
| 84 mus::WindowTreeConnection* connection) { | 84 mus::WindowTreeConnection* connection) { |
| 85 ConnectionToStateMap* window_map = window_states.Pointer()->Get(); | 85 ConnectionToStateMap* window_map = window_states.Pointer()->Get(); |
| 86 if (!window_map) { | 86 if (!window_map) { |
| 87 window_map = new ConnectionToStateMap; | 87 window_map = new ConnectionToStateMap; |
| 88 window_states.Pointer()->Set(window_map); | 88 window_states.Pointer()->Set(window_map); |
| 89 } | 89 } |
| 90 if (!(*window_map)[connection]) { | 90 if (!(*window_map)[connection]) { |
| 91 (*window_map)[connection] = new PerConnectionState(connector, connection); | 91 (*window_map)[connection] = new PerConnectionState(connector, connection); |
| 92 (*window_map)[connection]->Init(); | 92 (*window_map)[connection]->Init(); |
| 93 } | 93 } |
| 94 return (*window_map)[connection]; | 94 return (*window_map)[connection]; |
| 95 } | 95 } |
| 96 | 96 |
| 97 std::unique_ptr<cc::OutputSurface> | 97 std::unique_ptr<cc::OutputSurface> |
| 98 SurfaceBinding::PerConnectionState::CreateOutputSurface( | 98 SurfaceBinding::PerConnectionState::CreateOutputSurface( |
| 99 mus::Window* window, | 99 mus::Window* window, |
| 100 mus::mojom::SurfaceType surface_type) { | 100 mus::mojom::SurfaceType surface_type) { |
| 101 // 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 |
| 102 // value outliving this? | 102 // value outliving this? |
| 103 mus::mojom::CommandBufferPtr cb; | 103 mus::mojom::CommandBufferPtr cb; |
| 104 gpu_->CreateOffscreenGLES2Context(GetProxy(&cb)); | 104 gpu_->CreateOffscreenGLES2Context(GetProxy(&cb)); |
| 105 | 105 |
| 106 scoped_refptr<cc::ContextProvider> context_provider( | 106 scoped_refptr<cc::ContextProvider> context_provider( |
| 107 new mus::ContextProvider(cb.PassInterface().PassHandle())); | 107 new mus::ContextProvider(cb.PassInterface().PassHandle())); |
| 108 return base::WrapUnique(new mus::OutputSurface( | 108 return base::WrapUnique(new mus::OutputSurface( |
| 109 context_provider, window->RequestSurface(surface_type))); | 109 context_provider, window->RequestSurface(surface_type))); |
| 110 } | 110 } |
| 111 | 111 |
| 112 SurfaceBinding::PerConnectionState::PerConnectionState( | 112 SurfaceBinding::PerConnectionState::PerConnectionState( |
| 113 mojo::Connector* connector, | 113 shell::Connector* connector, |
| 114 mus::WindowTreeConnection* connection) | 114 mus::WindowTreeConnection* connection) |
| 115 : connector_(connector), connection_(connection) {} | 115 : connector_(connector), connection_(connection) {} |
| 116 | 116 |
| 117 SurfaceBinding::PerConnectionState::~PerConnectionState() { | 117 SurfaceBinding::PerConnectionState::~PerConnectionState() { |
| 118 ConnectionToStateMap* window_map = window_states.Pointer()->Get(); | 118 ConnectionToStateMap* window_map = window_states.Pointer()->Get(); |
| 119 DCHECK(window_map); | 119 DCHECK(window_map); |
| 120 DCHECK_EQ(this, (*window_map)[connection_]); | 120 DCHECK_EQ(this, (*window_map)[connection_]); |
| 121 window_map->erase(connection_); | 121 window_map->erase(connection_); |
| 122 if (window_map->empty()) { | 122 if (window_map->empty()) { |
| 123 delete window_map; | 123 delete window_map; |
| 124 window_states.Pointer()->Set(nullptr); | 124 window_states.Pointer()->Set(nullptr); |
| 125 } | 125 } |
| 126 } | 126 } |
| 127 | 127 |
| 128 void SurfaceBinding::PerConnectionState::Init() { | 128 void SurfaceBinding::PerConnectionState::Init() { |
| 129 connector_->ConnectToInterface("mojo:mus", &gpu_); | 129 connector_->ConnectToInterface("mojo:mus", &gpu_); |
| 130 } | 130 } |
| 131 | 131 |
| 132 // SurfaceBinding -------------------------------------------------------------- | 132 // SurfaceBinding -------------------------------------------------------------- |
| 133 | 133 |
| 134 SurfaceBinding::SurfaceBinding(mojo::Connector* connector, | 134 SurfaceBinding::SurfaceBinding(shell::Connector* connector, |
| 135 mus::Window* window, | 135 mus::Window* window, |
| 136 mus::mojom::SurfaceType surface_type) | 136 mus::mojom::SurfaceType surface_type) |
| 137 : window_(window), | 137 : window_(window), |
| 138 surface_type_(surface_type), | 138 surface_type_(surface_type), |
| 139 state_(PerConnectionState::Get(connector, window->connection())) {} | 139 state_(PerConnectionState::Get(connector, window->connection())) {} |
| 140 | 140 |
| 141 SurfaceBinding::~SurfaceBinding() {} | 141 SurfaceBinding::~SurfaceBinding() {} |
| 142 | 142 |
| 143 std::unique_ptr<cc::OutputSurface> SurfaceBinding::CreateOutputSurface() { | 143 std::unique_ptr<cc::OutputSurface> SurfaceBinding::CreateOutputSurface() { |
| 144 return state_->CreateOutputSurface(window_, surface_type_); | 144 return state_->CreateOutputSurface(window_, surface_type_); |
| 145 } | 145 } |
| 146 | 146 |
| 147 } // namespace views | 147 } // namespace views |
| OLD | NEW |