Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(600)

Side by Side Diff: mandoline/ui/aura/surface_binding.cc

Issue 1281663002: Mandoline: Allow submitting CompositorFrames directly to mojo::Views (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebased Created 5 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « mandoline/tab/frame_connection.cc ('k') | mojo/cc/BUILD.gn » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 "mandoline/ui/aura/surface_binding.h" 5 #include "mandoline/ui/aura/surface_binding.h"
6 6
7 #include <map> 7 #include <map>
8 8
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/lazy_instance.h" 10 #include "base/lazy_instance.h"
11 #include "base/threading/thread_local.h" 11 #include "base/threading/thread_local.h"
12 #include "cc/output/compositor_frame.h" 12 #include "cc/output/compositor_frame.h"
13 #include "cc/output/output_surface.h" 13 #include "cc/output/output_surface.h"
14 #include "cc/output/output_surface_client.h" 14 #include "cc/output/output_surface_client.h"
15 #include "cc/output/software_output_device.h" 15 #include "cc/output/software_output_device.h"
16 #include "cc/resources/shared_bitmap_manager.h" 16 #include "cc/resources/shared_bitmap_manager.h"
17 #include "components/view_manager/public/cpp/view.h" 17 #include "components/view_manager/public/cpp/view.h"
18 #include "components/view_manager/public/cpp/view_tree_connection.h" 18 #include "components/view_manager/public/cpp/view_tree_connection.h"
19 #include "components/view_manager/public/interfaces/gpu.mojom.h" 19 #include "components/view_manager/public/interfaces/gpu.mojom.h"
20 #include "components/view_manager/public/interfaces/surfaces.mojom.h"
21 #include "mandoline/ui/aura/window_tree_host_mojo.h" 20 #include "mandoline/ui/aura/window_tree_host_mojo.h"
22 #include "mojo/application/public/cpp/connect.h" 21 #include "mojo/application/public/cpp/connect.h"
23 #include "mojo/application/public/interfaces/shell.mojom.h" 22 #include "mojo/application/public/interfaces/shell.mojom.h"
24 #include "mojo/cc/context_provider_mojo.h" 23 #include "mojo/cc/context_provider_mojo.h"
24 #include "mojo/cc/output_surface_mojo.h"
25 #include "mojo/converters/geometry/geometry_type_converters.h" 25 #include "mojo/converters/geometry/geometry_type_converters.h"
26 #include "mojo/converters/surfaces/surfaces_type_converters.h" 26 #include "mojo/converters/surfaces/surfaces_type_converters.h"
27 #include "mojo/public/cpp/bindings/binding.h" 27 #include "mojo/public/cpp/bindings/binding.h"
28 28
29 namespace mandoline { 29 namespace mandoline {
30 namespace {
31
32 // OutputSurface ---------------------------------------------------------------
33
34 // OutputSurface implementation for a view. Pushes the surface id to View when
35 // appropriate.
36 class OutputSurfaceImpl : public cc::OutputSurface {
37 public:
38 OutputSurfaceImpl(mojo::View* view,
39 const scoped_refptr<cc::ContextProvider>& context_provider,
40 mojo::Surface* surface,
41 uint32_t id_namespace,
42 uint32_t* next_local_id);
43 ~OutputSurfaceImpl() override;
44
45 // cc::OutputSurface:
46 void SwapBuffers(cc::CompositorFrame* frame) override;
47
48 private:
49 mojo::View* view_;
50 mojo::Surface* surface_;
51 uint32_t id_namespace_;
52 uint32_t* next_local_id_; // Owned by PerConnectionState.
53 uint32_t local_id_;
54 gfx::Size surface_size_;
55
56 DISALLOW_COPY_AND_ASSIGN(OutputSurfaceImpl);
57 };
58
59 OutputSurfaceImpl::OutputSurfaceImpl(
60 mojo::View* view,
61 const scoped_refptr<cc::ContextProvider>& context_provider,
62 mojo::Surface* surface,
63 uint32_t id_namespace,
64 uint32_t* next_local_id)
65 : cc::OutputSurface(context_provider),
66 view_(view),
67 surface_(surface),
68 id_namespace_(id_namespace),
69 next_local_id_(next_local_id),
70 local_id_(0u) {
71 capabilities_.delegated_rendering = true;
72 capabilities_.max_frames_pending = 1;
73 }
74
75 OutputSurfaceImpl::~OutputSurfaceImpl() {
76 }
77
78 void OutputSurfaceImpl::SwapBuffers(cc::CompositorFrame* frame) {
79 gfx::Size frame_size =
80 frame->delegated_frame_data->render_pass_list.back()->output_rect.size();
81 if (frame_size != surface_size_) {
82 if (local_id_ != 0u)
83 surface_->DestroySurface(local_id_);
84 local_id_ = (*next_local_id_)++;
85 surface_->CreateSurface(local_id_);
86 auto qualified_id = mojo::SurfaceId::New();
87 qualified_id->local = local_id_;
88 qualified_id->id_namespace = id_namespace_;
89 view_->SetSurfaceId(qualified_id.Pass());
90 surface_size_ = frame_size;
91 }
92
93 surface_->SubmitCompositorFrame(
94 local_id_, mojo::CompositorFrame::From(*frame), mojo::Closure());
95
96 client_->DidSwapBuffers();
97 client_->DidSwapBuffersComplete();
98 }
99
100 } // namespace
101 30
102 // PerConnectionState ---------------------------------------------------------- 31 // PerConnectionState ----------------------------------------------------------
103 32
104 // State needed per ViewManager. Provides the real implementation of 33 // State needed per ViewManager. Provides the real implementation of
105 // CreateOutputSurface. SurfaceBinding obtains a pointer to the 34 // CreateOutputSurface. SurfaceBinding obtains a pointer to the
106 // PerConnectionState appropriate for the ViewManager. PerConnectionState is 35 // PerConnectionState appropriate for the ViewManager. PerConnectionState is
107 // stored in a thread local map. When no more refereces to a PerConnectionState 36 // stored in a thread local map. When no more refereces to a PerConnectionState
108 // remain the PerConnectionState is deleted and the underlying map cleaned up. 37 // remain the PerConnectionState is deleted and the underlying map cleaned up.
109 class SurfaceBinding::PerConnectionState 38 class SurfaceBinding::PerConnectionState
110 : public base::RefCounted<PerConnectionState>, 39 : public base::RefCounted<PerConnectionState> {
111 public mojo::ResourceReturner {
112 public: 40 public:
113 static PerConnectionState* Get(mojo::Shell* shell, 41 static PerConnectionState* Get(mojo::Shell* shell,
114 mojo::ViewTreeConnection* connection); 42 mojo::ViewTreeConnection* connection);
115 43
116 scoped_ptr<cc::OutputSurface> CreateOutputSurface(mojo::View* view); 44 scoped_ptr<cc::OutputSurface> CreateOutputSurface(mojo::View* view);
117 45
118 private: 46 private:
119 typedef std::map<mojo::ViewTreeConnection*, 47 typedef std::map<mojo::ViewTreeConnection*,
120 PerConnectionState*> ConnectionToStateMap; 48 PerConnectionState*> ConnectionToStateMap;
121 49
122 friend class base::RefCounted<PerConnectionState>; 50 friend class base::RefCounted<PerConnectionState>;
123 51
124 PerConnectionState(mojo::Shell* shell, mojo::ViewTreeConnection* connection); 52 PerConnectionState(mojo::Shell* shell, mojo::ViewTreeConnection* connection);
125 ~PerConnectionState() override; 53 ~PerConnectionState();
126 54
127 void Init(); 55 void Init();
128 56
129 // mojo::ResourceReturner:
130 void ReturnResources(
131 mojo::Array<mojo::ReturnedResourcePtr> resources) override;
132
133 void SetIdNamespace(uint32_t id_namespace);
134
135 static base::LazyInstance< 57 static base::LazyInstance<
136 base::ThreadLocalPointer<ConnectionToStateMap>>::Leaky view_states; 58 base::ThreadLocalPointer<ConnectionToStateMap>>::Leaky view_states;
137 59
138 mojo::Shell* shell_; 60 mojo::Shell* shell_;
139 mojo::ViewTreeConnection* connection_; 61 mojo::ViewTreeConnection* connection_;
140 62
141 // Set of state needed to create an OutputSurface. 63 // Set of state needed to create an OutputSurface.
142 mojo::GpuPtr gpu_; 64 mojo::GpuPtr gpu_;
143 mojo::SurfacePtr surface_;
144 mojo::Binding<mojo::ResourceReturner> returner_binding_;
145 uint32_t id_namespace_;
146 uint32_t next_local_id_;
147 65
148 DISALLOW_COPY_AND_ASSIGN(PerConnectionState); 66 DISALLOW_COPY_AND_ASSIGN(PerConnectionState);
149 }; 67 };
150 68
151 // static 69 // static
152 base::LazyInstance<base::ThreadLocalPointer< 70 base::LazyInstance<base::ThreadLocalPointer<
153 SurfaceBinding::PerConnectionState::ConnectionToStateMap>>::Leaky 71 SurfaceBinding::PerConnectionState::ConnectionToStateMap>>::Leaky
154 SurfaceBinding::PerConnectionState::view_states; 72 SurfaceBinding::PerConnectionState::view_states;
155 73
156 // static 74 // static
(...skipping 11 matching lines...) Expand all
168 } 86 }
169 return (*view_map)[connection]; 87 return (*view_map)[connection];
170 } 88 }
171 89
172 scoped_ptr<cc::OutputSurface> 90 scoped_ptr<cc::OutputSurface>
173 SurfaceBinding::PerConnectionState::CreateOutputSurface(mojo::View* view) { 91 SurfaceBinding::PerConnectionState::CreateOutputSurface(mojo::View* view) {
174 // TODO(sky): figure out lifetime here. Do I need to worry about the return 92 // TODO(sky): figure out lifetime here. Do I need to worry about the return
175 // value outliving this? 93 // value outliving this?
176 mojo::CommandBufferPtr cb; 94 mojo::CommandBufferPtr cb;
177 gpu_->CreateOffscreenGLES2Context(GetProxy(&cb)); 95 gpu_->CreateOffscreenGLES2Context(GetProxy(&cb));
96
178 scoped_refptr<cc::ContextProvider> context_provider( 97 scoped_refptr<cc::ContextProvider> context_provider(
179 new mojo::ContextProviderMojo(cb.PassInterface().PassHandle())); 98 new mojo::ContextProviderMojo(cb.PassInterface().PassHandle()));
180 return make_scoped_ptr(new OutputSurfaceImpl( 99 return make_scoped_ptr(
181 view, context_provider, surface_.get(), id_namespace_, &next_local_id_)); 100 new mojo::OutputSurfaceMojo(context_provider, view->RequestSurface()));
182 } 101 }
183 102
184 SurfaceBinding::PerConnectionState::PerConnectionState( 103 SurfaceBinding::PerConnectionState::PerConnectionState(
185 mojo::Shell* shell, 104 mojo::Shell* shell,
186 mojo::ViewTreeConnection* connection) 105 mojo::ViewTreeConnection* connection)
187 : shell_(shell), 106 : shell_(shell) {
188 connection_(connection),
189 returner_binding_(this),
190 id_namespace_(0u),
191 next_local_id_(0u) {
192 } 107 }
193 108
194 SurfaceBinding::PerConnectionState::~PerConnectionState() { 109 SurfaceBinding::PerConnectionState::~PerConnectionState() {
195 ConnectionToStateMap* view_map = view_states.Pointer()->Get(); 110 ConnectionToStateMap* view_map = view_states.Pointer()->Get();
196 DCHECK(view_map); 111 DCHECK(view_map);
197 DCHECK_EQ(this, (*view_map)[connection_]); 112 DCHECK_EQ(this, (*view_map)[connection_]);
198 view_map->erase(connection_); 113 view_map->erase(connection_);
199 if (view_map->empty()) { 114 if (view_map->empty()) {
200 delete view_map; 115 delete view_map;
201 view_states.Pointer()->Set(nullptr); 116 view_states.Pointer()->Set(nullptr);
202 } 117 }
203 } 118 }
204 119
205 void SurfaceBinding::PerConnectionState::Init() { 120 void SurfaceBinding::PerConnectionState::Init() {
206 DCHECK(!surface_.get()); 121 mojo::ServiceProviderPtr service_provider;
207
208 mojo::ServiceProviderPtr surfaces_service_provider;
209 mojo::URLRequestPtr request(mojo::URLRequest::New()); 122 mojo::URLRequestPtr request(mojo::URLRequest::New());
210 request->url = mojo::String::From("mojo:view_manager"); 123 request->url = mojo::String::From("mojo:view_manager");
211 shell_->ConnectToApplication(request.Pass(), 124 shell_->ConnectToApplication(request.Pass(),
212 GetProxy(&surfaces_service_provider), 125 GetProxy(&service_provider),
213 nullptr, 126 nullptr,
214 nullptr); 127 nullptr);
215 ConnectToService(surfaces_service_provider.get(), &surface_); 128 ConnectToService(service_provider.get(), &gpu_);
216 surface_->GetIdNamespace(
217 base::Bind(&SurfaceBinding::PerConnectionState::SetIdNamespace,
218 base::Unretained(this)));
219 // Block until we receive our id namespace.
220 surface_.WaitForIncomingResponse();
221 DCHECK_NE(0u, id_namespace_);
222
223 mojo::ResourceReturnerPtr returner_ptr;
224 returner_binding_.Bind(GetProxy(&returner_ptr));
225 surface_->SetResourceReturner(returner_ptr.Pass());
226
227 mojo::ServiceProviderPtr gpu_service_provider;
228 // TODO(jamesr): Should be mojo:gpu_service
229 mojo::URLRequestPtr request2(mojo::URLRequest::New());
230 request2->url = mojo::String::From("mojo:view_manager");
231 shell_->ConnectToApplication(request2.Pass(),
232 GetProxy(&gpu_service_provider),
233 nullptr,
234 nullptr);
235 ConnectToService(gpu_service_provider.get(), &gpu_);
236 }
237
238 void SurfaceBinding::PerConnectionState::SetIdNamespace(
239 uint32_t id_namespace) {
240 id_namespace_ = id_namespace;
241 }
242
243 void SurfaceBinding::PerConnectionState::ReturnResources(
244 mojo::Array<mojo::ReturnedResourcePtr> resources) {
245 } 129 }
246 130
247 // SurfaceBinding -------------------------------------------------------------- 131 // SurfaceBinding --------------------------------------------------------------
248 132
249 SurfaceBinding::SurfaceBinding(mojo::Shell* shell, mojo::View* view) 133 SurfaceBinding::SurfaceBinding(mojo::Shell* shell, mojo::View* view)
250 : view_(view), 134 : view_(view),
251 state_(PerConnectionState::Get(shell, view->connection())) { 135 state_(PerConnectionState::Get(shell, view->connection())) {
252 } 136 }
253 137
254 SurfaceBinding::~SurfaceBinding() { 138 SurfaceBinding::~SurfaceBinding() {
255 } 139 }
256 140
257 scoped_ptr<cc::OutputSurface> SurfaceBinding::CreateOutputSurface() { 141 scoped_ptr<cc::OutputSurface> SurfaceBinding::CreateOutputSurface() {
258 return state_->CreateOutputSurface(view_); 142 return state_->CreateOutputSurface(view_);
259 } 143 }
260 144
261 } // namespace mandoline 145 } // namespace mandoline
OLDNEW
« no previous file with comments | « mandoline/tab/frame_connection.cc ('k') | mojo/cc/BUILD.gn » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698