| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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 "sky/compositor/surface_holder.h" | 5 #include "sky/compositor/surface_holder.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/message_loop/message_loop.h" | 8 #include "base/message_loop/message_loop.h" |
| 9 #include "mojo/converters/geometry/geometry_type_converters.h" | 9 #include "mojo/converters/geometry/geometry_type_converters.h" |
| 10 #include "mojo/public/cpp/application/connect.h" | 10 #include "mojo/public/cpp/application/connect.h" |
| 11 #include "mojo/public/interfaces/application/shell.mojom.h" | 11 #include "mojo/public/interfaces/application/shell.mojom.h" |
| 12 #include "sky/compositor/surface_allocator.h" | 12 #include "sky/compositor/surface_allocator.h" |
| 13 | 13 |
| 14 namespace sky { | 14 namespace sky { |
| 15 | 15 |
| 16 SurfaceHolder::Client::~Client() { | 16 SurfaceHolder::Client::~Client() { |
| 17 } | 17 } |
| 18 | 18 |
| 19 SurfaceHolder::SurfaceHolder(Client* client, mojo::Shell* shell) | 19 SurfaceHolder::SurfaceHolder(Client* client, mojo::Shell* shell) |
| 20 : client_(client), weak_factory_(this) { | 20 : client_(client), id_namespace_(0u), local_id_(0u), weak_factory_(this) { |
| 21 mojo::ServiceProviderPtr service_provider; | 21 mojo::ServiceProviderPtr service_provider; |
| 22 shell->ConnectToApplication("mojo:surfaces_service", | 22 shell->ConnectToApplication("mojo:surfaces_service", |
| 23 mojo::GetProxy(&service_provider)); | 23 mojo::GetProxy(&service_provider)); |
| 24 mojo::ConnectToService(service_provider.get(), &surfaces_service_); | 24 mojo::ConnectToService(service_provider.get(), &surface_); |
| 25 | 25 surface_.set_client(this); |
| 26 surfaces_service_->CreateSurfaceConnection(base::Bind( | |
| 27 &SurfaceHolder::OnSurfaceConnectionCreated, weak_factory_.GetWeakPtr())); | |
| 28 } | 26 } |
| 29 | 27 |
| 30 SurfaceHolder::~SurfaceHolder() { | 28 SurfaceHolder::~SurfaceHolder() { |
| 31 if (surface_ && surface_id_) | 29 if (local_id_ != 0u) |
| 32 surface_->DestroySurface(surface_id_.Clone()); | 30 surface_->DestroySurface(local_id_); |
| 33 } | |
| 34 | |
| 35 bool SurfaceHolder::IsReadyForFrame() const { | |
| 36 return surface_; | |
| 37 } | 31 } |
| 38 | 32 |
| 39 void SurfaceHolder::SubmitFrame(mojo::FramePtr frame, | 33 void SurfaceHolder::SubmitFrame(mojo::FramePtr frame, |
| 40 const base::Closure& callback) { | 34 const base::Closure& callback) { |
| 41 surface_->SubmitFrame(surface_id_.Clone(), frame.Pass(), callback); | 35 surface_->SubmitFrame(local_id_, frame.Pass(), callback); |
| 42 } | 36 } |
| 43 | 37 |
| 44 void SurfaceHolder::SetSize(const gfx::Size& size) { | 38 void SurfaceHolder::SetSize(const gfx::Size& size) { |
| 45 if (surface_id_ && size_ == size) | 39 if (local_id_ != 0u && size_ == size) |
| 46 return; | 40 return; |
| 47 | 41 |
| 48 if (surface_id_) { | 42 if (local_id_ != 0u) |
| 49 surface_->DestroySurface(surface_id_.Clone()); | 43 surface_->DestroySurface(local_id_); |
| 50 } else { | |
| 51 surface_id_ = mojo::SurfaceId::New(); | |
| 52 } | |
| 53 | 44 |
| 54 surface_id_ = surface_allocator_->CreateSurfaceId(); | 45 local_id_++; |
| 55 surface_->CreateSurface(surface_id_.Clone()); | 46 surface_->CreateSurface(local_id_); |
| 56 size_ = size; | 47 size_ = size; |
| 57 | 48 |
| 58 client_->OnSurfaceIdAvailable(surface_id_.Clone()); | 49 if (id_namespace_ != 0u) |
| 50 SetQualifiedId(); |
| 51 } |
| 52 |
| 53 void SurfaceHolder::SetQualifiedId() { |
| 54 auto qualified_id = mojo::SurfaceId::New(); |
| 55 qualified_id->id_namespace = id_namespace_; |
| 56 qualified_id->local = local_id_; |
| 57 client_->OnSurfaceIdAvailable(qualified_id.Pass()); |
| 59 } | 58 } |
| 60 | 59 |
| 61 void SurfaceHolder::SetIdNamespace(uint32_t id_namespace) { | 60 void SurfaceHolder::SetIdNamespace(uint32_t id_namespace) { |
| 61 id_namespace_ = id_namespace; |
| 62 if (local_id_ != 0u) |
| 63 SetQualifiedId(); |
| 62 } | 64 } |
| 63 | 65 |
| 64 void SurfaceHolder::ReturnResources( | 66 void SurfaceHolder::ReturnResources( |
| 65 mojo::Array<mojo::ReturnedResourcePtr> resources) { | 67 mojo::Array<mojo::ReturnedResourcePtr> resources) { |
| 66 // TODO(abarth): The surface service shouldn't spam us with empty calls. | 68 // TODO(abarth): The surface service shouldn't spam us with empty calls. |
| 67 if (!resources.size()) | 69 if (!resources.size()) |
| 68 return; | 70 return; |
| 69 client_->ReturnResources(resources.Pass()); | 71 client_->ReturnResources(resources.Pass()); |
| 70 } | 72 } |
| 71 | 73 |
| 72 void SurfaceHolder::OnSurfaceConnectionCreated(mojo::SurfacePtr surface, | |
| 73 uint32_t id_namespace) { | |
| 74 surface_ = surface.Pass(); | |
| 75 surface_.set_client(this); | |
| 76 surface_allocator_.reset(new SurfaceAllocator(id_namespace)); | |
| 77 client_->OnSurfaceConnectionCreated(); | |
| 78 } | |
| 79 | |
| 80 } // namespace sky | 74 } // namespace sky |
| OLD | NEW |