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
3 // found in the LICENSE file.
5 #include "mandoline/ui/aura/surface_binding.h"
10 #include "base/lazy_instance.h"
11 #include "base/threading/thread_local.h"
12 #include "cc/output/compositor_frame.h"
13 #include "cc/output/output_surface.h"
14 #include "cc/output/output_surface_client.h"
15 #include "cc/output/software_output_device.h"
16 #include "cc/resources/shared_bitmap_manager.h"
17 #include "components/view_manager/public/cpp/view.h"
18 #include "components/view_manager/public/cpp/view_manager.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"
22 #include "mojo/application/public/cpp/connect.h"
23 #include "mojo/application/public/interfaces/shell.mojom.h"
24 #include "mojo/cc/context_provider_mojo.h"
25 #include "mojo/converters/geometry/geometry_type_converters.h"
26 #include "mojo/converters/surfaces/surfaces_type_converters.h"
31 // OutputSurface ---------------------------------------------------------------
33 // OutputSurface implementation for a view. Pushes the surface id to View when
35 class OutputSurfaceImpl
: public cc::OutputSurface
{
37 OutputSurfaceImpl(mojo::View
* view
,
38 const scoped_refptr
<cc::ContextProvider
>& context_provider
,
39 mojo::Surface
* surface
,
40 uint32_t id_namespace
,
41 uint32_t* next_local_id
);
42 ~OutputSurfaceImpl() override
;
45 void SwapBuffers(cc::CompositorFrame
* frame
) override
;
49 mojo::Surface
* surface_
;
50 uint32_t id_namespace_
;
51 uint32_t* next_local_id_
; // Owned by PerViewManagerState.
53 gfx::Size surface_size_
;
55 DISALLOW_COPY_AND_ASSIGN(OutputSurfaceImpl
);
58 OutputSurfaceImpl::OutputSurfaceImpl(
60 const scoped_refptr
<cc::ContextProvider
>& context_provider
,
61 mojo::Surface
* surface
,
62 uint32_t id_namespace
,
63 uint32_t* next_local_id
)
64 : cc::OutputSurface(context_provider
),
67 id_namespace_(id_namespace
),
68 next_local_id_(next_local_id
),
70 capabilities_
.delegated_rendering
= true;
71 capabilities_
.max_frames_pending
= 1;
74 OutputSurfaceImpl::~OutputSurfaceImpl() {
77 void OutputSurfaceImpl::SwapBuffers(cc::CompositorFrame
* frame
) {
78 gfx::Size frame_size
=
79 frame
->delegated_frame_data
->render_pass_list
.back()->output_rect
.size();
80 if (frame_size
!= surface_size_
) {
82 surface_
->DestroySurface(local_id_
);
83 local_id_
= (*next_local_id_
)++;
84 surface_
->CreateSurface(local_id_
);
85 auto qualified_id
= mojo::SurfaceId::New();
86 qualified_id
->local
= local_id_
;
87 qualified_id
->id_namespace
= id_namespace_
;
88 view_
->SetSurfaceId(qualified_id
.Pass());
89 surface_size_
= frame_size
;
92 surface_
->SubmitFrame(local_id_
, mojo::Frame::From(*frame
), mojo::Closure());
94 client_
->DidSwapBuffers();
95 client_
->DidSwapBuffersComplete();
100 // PerViewManagerState ---------------------------------------------------------
102 // State needed per ViewManager. Provides the real implementation of
103 // CreateOutputSurface. SurfaceBinding obtains a pointer to the
104 // PerViewManagerState appropriate for the ViewManager. PerViewManagerState is
105 // stored in a thread local map. When no more refereces to a PerViewManagerState
106 // remain the PerViewManagerState is deleted and the underlying map cleaned up.
107 class SurfaceBinding::PerViewManagerState
108 : public base::RefCounted
<PerViewManagerState
>,
109 public mojo::ResourceReturner
{
111 static PerViewManagerState
* Get(mojo::Shell
* shell
,
112 mojo::ViewManager
* view_manager
);
114 scoped_ptr
<cc::OutputSurface
> CreateOutputSurface(mojo::View
* view
);
117 typedef std::map
<mojo::ViewManager
*,
118 PerViewManagerState
*> ViewManagerToStateMap
;
120 friend class base::RefCounted
<PerViewManagerState
>;
122 PerViewManagerState(mojo::Shell
* shell
, mojo::ViewManager
* view_manager
);
123 ~PerViewManagerState() override
;
127 // mojo::ResourceReturner:
128 void ReturnResources(
129 mojo::Array
<mojo::ReturnedResourcePtr
> resources
) override
;
131 void SetIdNamespace(uint32_t id_namespace
);
133 static base::LazyInstance
<
134 base::ThreadLocalPointer
<ViewManagerToStateMap
>>::Leaky view_states
;
137 mojo::ViewManager
* view_manager_
;
139 // Set of state needed to create an OutputSurface.
141 mojo::SurfacePtr surface_
;
142 mojo::Binding
<mojo::ResourceReturner
> returner_binding_
;
143 uint32_t id_namespace_
;
144 uint32_t next_local_id_
;
146 DISALLOW_COPY_AND_ASSIGN(PerViewManagerState
);
150 base::LazyInstance
<base::ThreadLocalPointer
<
151 SurfaceBinding::PerViewManagerState::ViewManagerToStateMap
>>::Leaky
152 SurfaceBinding::PerViewManagerState::view_states
;
155 SurfaceBinding::PerViewManagerState
* SurfaceBinding::PerViewManagerState::Get(
157 mojo::ViewManager
* view_manager
) {
158 ViewManagerToStateMap
* view_map
= view_states
.Pointer()->Get();
160 view_map
= new ViewManagerToStateMap
;
161 view_states
.Pointer()->Set(view_map
);
163 if (!(*view_map
)[view_manager
]) {
164 (*view_map
)[view_manager
] = new PerViewManagerState(shell
, view_manager
);
165 (*view_map
)[view_manager
]->Init();
167 return (*view_map
)[view_manager
];
170 scoped_ptr
<cc::OutputSurface
>
171 SurfaceBinding::PerViewManagerState::CreateOutputSurface(mojo::View
* view
) {
172 // TODO(sky): figure out lifetime here. Do I need to worry about the return
173 // value outliving this?
174 mojo::CommandBufferPtr cb
;
175 gpu_
->CreateOffscreenGLES2Context(GetProxy(&cb
));
176 scoped_refptr
<cc::ContextProvider
> context_provider(
177 new mojo::ContextProviderMojo(cb
.PassInterface().PassHandle()));
178 return make_scoped_ptr(new OutputSurfaceImpl(
179 view
, context_provider
, surface_
.get(), id_namespace_
, &next_local_id_
));
182 SurfaceBinding::PerViewManagerState::PerViewManagerState(
184 mojo::ViewManager
* view_manager
)
186 view_manager_(view_manager
),
187 returner_binding_(this),
192 SurfaceBinding::PerViewManagerState::~PerViewManagerState() {
193 ViewManagerToStateMap
* view_map
= view_states
.Pointer()->Get();
195 DCHECK_EQ(this, (*view_map
)[view_manager_
]);
196 view_map
->erase(view_manager_
);
197 if (view_map
->empty()) {
199 view_states
.Pointer()->Set(nullptr);
203 void SurfaceBinding::PerViewManagerState::Init() {
204 DCHECK(!surface_
.get());
206 mojo::ServiceProviderPtr surfaces_service_provider
;
207 mojo::URLRequestPtr
request(mojo::URLRequest::New());
208 request
->url
= mojo::String::From("mojo:surfaces_service");
209 shell_
->ConnectToApplication(request
.Pass(),
210 GetProxy(&surfaces_service_provider
),
212 ConnectToService(surfaces_service_provider
.get(), &surface_
);
213 surface_
->GetIdNamespace(
214 base::Bind(&SurfaceBinding::PerViewManagerState::SetIdNamespace
,
215 base::Unretained(this)));
216 // Block until we receive our id namespace.
217 surface_
.WaitForIncomingResponse();
218 DCHECK_NE(0u, id_namespace_
);
220 mojo::ResourceReturnerPtr returner_ptr
;
221 returner_binding_
.Bind(GetProxy(&returner_ptr
));
222 surface_
->SetResourceReturner(returner_ptr
.Pass());
224 mojo::ServiceProviderPtr gpu_service_provider
;
225 // TODO(jamesr): Should be mojo:gpu_service
226 mojo::URLRequestPtr
request2(mojo::URLRequest::New());
227 request2
->url
= mojo::String::From("mojo:view_manager");
228 shell_
->ConnectToApplication(request2
.Pass(),
229 GetProxy(&gpu_service_provider
),
231 ConnectToService(gpu_service_provider
.get(), &gpu_
);
234 void SurfaceBinding::PerViewManagerState::SetIdNamespace(
235 uint32_t id_namespace
) {
236 id_namespace_
= id_namespace
;
239 void SurfaceBinding::PerViewManagerState::ReturnResources(
240 mojo::Array
<mojo::ReturnedResourcePtr
> resources
) {
243 // SurfaceBinding --------------------------------------------------------------
245 SurfaceBinding::SurfaceBinding(mojo::Shell
* shell
, mojo::View
* view
)
247 state_(PerViewManagerState::Get(shell
, view
->view_manager())) {
250 SurfaceBinding::~SurfaceBinding() {
253 scoped_ptr
<cc::OutputSurface
> SurfaceBinding::CreateOutputSurface() {
254 return state_
->CreateOutputSurface(view_
);
257 } // namespace mandoline