1 // Copyright 2014 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 "components/view_manager/surfaces/surfaces_impl.h"
7 #include "base/trace_event/trace_event.h"
8 #include "cc/output/compositor_frame.h"
9 #include "cc/resources/returned_resource.h"
10 #include "cc/surfaces/surface_id_allocator.h"
11 #include "components/view_manager/surfaces/surfaces_delegate.h"
12 #include "components/view_manager/surfaces/surfaces_scheduler.h"
13 #include "mojo/converters/geometry/geometry_type_converters.h"
14 #include "mojo/converters/surfaces/surfaces_type_converters.h"
16 using mojo::SurfaceIdPtr
;
21 void CallCallback(const mojo::Closure
& callback
, cc::SurfaceDrawStatus status
) {
26 SurfacesImpl::SurfacesImpl(SurfacesDelegate
* surfaces_delegate
,
27 const scoped_refptr
<SurfacesState
>& state
,
28 mojo::InterfaceRequest
<mojo::Surface
> request
)
29 : delegate_(surfaces_delegate
),
31 id_namespace_(state
->next_id_namespace()),
32 factory_(state
->manager(), this),
33 binding_(this, request
.Pass()) {
34 // Destroy this object if the connection is closed.
35 binding_
.set_connection_error_handler(
36 base::Bind(&SurfacesImpl::CloseConnection
, base::Unretained(this)));
39 void SurfacesImpl::CloseConnection() {
40 if (connection_closed_
)
42 connection_closed_
= true;
43 delegate_
->OnSurfaceConnectionClosed(this);
47 void SurfacesImpl::GetIdNamespace(
48 const Surface::GetIdNamespaceCallback
& callback
) {
49 callback
.Run(id_namespace_
);
52 void SurfacesImpl::SetResourceReturner(mojo::ResourceReturnerPtr returner
) {
53 returner_
= returner
.Pass();
56 void SurfacesImpl::CreateSurface(uint32_t local_id
) {
57 factory_
.Create(QualifyIdentifier(local_id
));
60 void SurfacesImpl::SubmitFrame(uint32_t local_id
,
61 mojo::CompositorFramePtr frame
,
62 const mojo::Closure
& callback
) {
63 TRACE_EVENT0("mojo", "SurfacesImpl::SubmitFrame");
64 factory_
.SubmitFrame(QualifyIdentifier(local_id
),
65 frame
.To
<scoped_ptr
<cc::CompositorFrame
>>(),
66 base::Bind(&CallCallback
, callback
));
67 state_
->scheduler()->SetNeedsDraw();
70 void SurfacesImpl::DestroySurface(uint32_t local_id
) {
71 factory_
.Destroy(QualifyIdentifier(local_id
));
74 void SurfacesImpl::ReturnResources(const cc::ReturnedResourceArray
& resources
) {
75 if (resources
.empty() || !returner_
)
77 mojo::Array
<mojo::ReturnedResourcePtr
> ret(resources
.size());
78 for (size_t i
= 0; i
< resources
.size(); ++i
) {
79 ret
[i
] = mojo::ReturnedResource::From(resources
[i
]);
81 returner_
->ReturnResources(ret
.Pass());
84 SurfacesImpl::~SurfacesImpl() {
85 // Only CloseConnection should be allowed to destroy this object.
86 DCHECK(connection_closed_
);
87 factory_
.DestroyAll();
90 cc::SurfaceId
SurfacesImpl::QualifyIdentifier(uint32_t local_id
) {
91 return cc::SurfaceId(static_cast<uint64_t>(id_namespace_
) << 32 | local_id
);