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 19 matching lines...) Expand all Loading... |
30 | 30 |
31 // PerClientState -------------------------------------------------------------- | 31 // PerClientState -------------------------------------------------------------- |
32 | 32 |
33 // State needed per WindowTreeClient. Provides the real implementation of | 33 // State needed per WindowTreeClient. Provides the real implementation of |
34 // CreateOutputSurface. SurfaceBinding obtains a pointer to the | 34 // CreateOutputSurface. SurfaceBinding obtains a pointer to the |
35 // PerClientState appropriate for the WindowTreeClient. PerClientState is | 35 // PerClientState appropriate for the WindowTreeClient. PerClientState is |
36 // stored in a thread local map. When no more refereces to a PerClientState | 36 // stored in a thread local map. When no more refereces to a PerClientState |
37 // remain the PerClientState is deleted and the underlying map cleaned up. | 37 // remain the PerClientState is deleted and the underlying map cleaned up. |
38 class SurfaceBinding::PerClientState : public base::RefCounted<PerClientState> { | 38 class SurfaceBinding::PerClientState : public base::RefCounted<PerClientState> { |
39 public: | 39 public: |
40 static PerClientState* Get(shell::Connector* connector, | 40 static PerClientState* Get(ui::WindowTreeClient* client); |
41 ui::WindowTreeClient* client); | |
42 | 41 |
43 std::unique_ptr<cc::OutputSurface> CreateOutputSurface( | 42 std::unique_ptr<cc::OutputSurface> CreateOutputSurface( |
44 ui::Window* window, | 43 ui::Window* window, |
45 ui::mojom::SurfaceType type); | 44 ui::mojom::SurfaceType type); |
46 | 45 |
47 private: | 46 private: |
48 typedef std::map<ui::WindowTreeClient*, PerClientState*> ClientToStateMap; | 47 typedef std::map<ui::WindowTreeClient*, PerClientState*> ClientToStateMap; |
49 | 48 |
50 friend class base::RefCounted<PerClientState>; | 49 friend class base::RefCounted<PerClientState>; |
51 | 50 |
52 PerClientState(shell::Connector* connector, ui::WindowTreeClient* client); | 51 explicit PerClientState(ui::WindowTreeClient* client); |
53 ~PerClientState(); | 52 ~PerClientState(); |
54 | 53 |
55 static base::LazyInstance< | 54 static base::LazyInstance< |
56 base::ThreadLocalPointer<ClientToStateMap>>::Leaky window_states; | 55 base::ThreadLocalPointer<ClientToStateMap>>::Leaky window_states; |
57 | 56 |
58 shell::Connector* connector_; | |
59 ui::WindowTreeClient* client_; | 57 ui::WindowTreeClient* client_; |
60 | 58 |
61 DISALLOW_COPY_AND_ASSIGN(PerClientState); | 59 DISALLOW_COPY_AND_ASSIGN(PerClientState); |
62 }; | 60 }; |
63 | 61 |
64 // static | 62 // static |
65 base::LazyInstance<base::ThreadLocalPointer< | 63 base::LazyInstance<base::ThreadLocalPointer< |
66 SurfaceBinding::PerClientState::ClientToStateMap>>::Leaky | 64 SurfaceBinding::PerClientState::ClientToStateMap>>::Leaky |
67 SurfaceBinding::PerClientState::window_states; | 65 SurfaceBinding::PerClientState::window_states; |
68 | 66 |
69 // static | 67 // static |
70 SurfaceBinding::PerClientState* SurfaceBinding::PerClientState::Get( | 68 SurfaceBinding::PerClientState* SurfaceBinding::PerClientState::Get( |
71 shell::Connector* connector, | |
72 ui::WindowTreeClient* client) { | 69 ui::WindowTreeClient* client) { |
73 // |connector| can be null in some unit-tests. | |
74 if (!connector) | |
75 return nullptr; | |
76 ClientToStateMap* window_map = window_states.Pointer()->Get(); | 70 ClientToStateMap* window_map = window_states.Pointer()->Get(); |
77 if (!window_map) { | 71 if (!window_map) { |
78 window_map = new ClientToStateMap; | 72 window_map = new ClientToStateMap; |
79 window_states.Pointer()->Set(window_map); | 73 window_states.Pointer()->Set(window_map); |
80 } | 74 } |
81 if (!(*window_map)[client]) | 75 if (!(*window_map)[client]) |
82 (*window_map)[client] = new PerClientState(connector, client); | 76 (*window_map)[client] = new PerClientState(client); |
83 return (*window_map)[client]; | 77 return (*window_map)[client]; |
84 } | 78 } |
85 | 79 |
86 std::unique_ptr<cc::OutputSurface> | 80 std::unique_ptr<cc::OutputSurface> |
87 SurfaceBinding::PerClientState::CreateOutputSurface( | 81 SurfaceBinding::PerClientState::CreateOutputSurface( |
88 ui::Window* window, | 82 ui::Window* window, |
89 ui::mojom::SurfaceType surface_type) { | 83 ui::mojom::SurfaceType surface_type) { |
90 scoped_refptr<cc::ContextProvider> context_provider( | 84 scoped_refptr<cc::ContextProvider> context_provider(new ui::ContextProvider); |
91 new ui::ContextProvider(connector_)); | |
92 return base::WrapUnique(new ui::OutputSurface( | 85 return base::WrapUnique(new ui::OutputSurface( |
93 context_provider, window->RequestSurface(surface_type))); | 86 context_provider, window->RequestSurface(surface_type))); |
94 } | 87 } |
95 | 88 |
96 SurfaceBinding::PerClientState::PerClientState(shell::Connector* connector, | 89 SurfaceBinding::PerClientState::PerClientState(ui::WindowTreeClient* client) |
97 ui::WindowTreeClient* client) | 90 : client_(client) {} |
98 : connector_(connector), client_(client) {} | |
99 | 91 |
100 SurfaceBinding::PerClientState::~PerClientState() { | 92 SurfaceBinding::PerClientState::~PerClientState() { |
101 ClientToStateMap* window_map = window_states.Pointer()->Get(); | 93 ClientToStateMap* window_map = window_states.Pointer()->Get(); |
102 DCHECK(window_map); | 94 DCHECK(window_map); |
103 DCHECK_EQ(this, (*window_map)[client_]); | 95 DCHECK_EQ(this, (*window_map)[client_]); |
104 window_map->erase(client_); | 96 window_map->erase(client_); |
105 if (window_map->empty()) { | 97 if (window_map->empty()) { |
106 delete window_map; | 98 delete window_map; |
107 window_states.Pointer()->Set(nullptr); | 99 window_states.Pointer()->Set(nullptr); |
108 } | 100 } |
109 } | 101 } |
110 | 102 |
111 // SurfaceBinding -------------------------------------------------------------- | 103 // SurfaceBinding -------------------------------------------------------------- |
112 | 104 |
113 SurfaceBinding::SurfaceBinding(shell::Connector* connector, | 105 SurfaceBinding::SurfaceBinding(ui::Window* window, |
114 ui::Window* window, | |
115 ui::mojom::SurfaceType surface_type) | 106 ui::mojom::SurfaceType surface_type) |
116 : window_(window), | 107 : window_(window), |
117 surface_type_(surface_type), | 108 surface_type_(surface_type), |
118 state_(PerClientState::Get(connector, window->window_tree())) {} | 109 state_(PerClientState::Get(window->window_tree())) {} |
119 | 110 |
120 SurfaceBinding::~SurfaceBinding() {} | 111 SurfaceBinding::~SurfaceBinding() {} |
121 | 112 |
122 std::unique_ptr<cc::OutputSurface> SurfaceBinding::CreateOutputSurface() { | 113 std::unique_ptr<cc::OutputSurface> SurfaceBinding::CreateOutputSurface() { |
123 return state_ ? state_->CreateOutputSurface(window_, surface_type_) : nullptr; | 114 return state_ ? state_->CreateOutputSurface(window_, surface_type_) : nullptr; |
124 } | 115 } |
125 | 116 |
126 } // namespace views | 117 } // namespace views |
OLD | NEW |