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