| OLD | NEW |
| (Empty) | |
| 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. |
| 4 |
| 5 #include "services/fake_surfaces/fake_surfaces_service_application.h" |
| 6 |
| 7 #include "mojo/application/application_runner_chromium.h" |
| 8 #include "mojo/public/c/system/main.h" |
| 9 #include "mojo/public/cpp/application/application_connection.h" |
| 10 #include "mojo/public/cpp/bindings/strong_binding.h" |
| 11 #include "mojo/services/surfaces/interfaces/surfaces.mojom.h" |
| 12 |
| 13 using mojo::ApplicationConnection; |
| 14 using mojo::Display; |
| 15 using mojo::DisplayFactory; |
| 16 using mojo::InterfaceRequest; |
| 17 using mojo::ResourceReturnerPtr; |
| 18 using mojo::StrongBinding; |
| 19 using mojo::Surface; |
| 20 |
| 21 namespace fake_surfaces { |
| 22 |
| 23 namespace { |
| 24 void ReturnAll(const mojo::Array<mojo::TransferableResourcePtr>& resources, |
| 25 mojo::ResourceReturner* returner) { |
| 26 mojo::Array<mojo::ReturnedResourcePtr> returned; |
| 27 returned.resize(resources.size()); |
| 28 for (size_t i = 0; i < resources.size(); ++i) { |
| 29 auto ret = mojo::ReturnedResource::New(); |
| 30 ret->id = resources[i]->id; |
| 31 ret->sync_point = 0u; |
| 32 ret->count = 1; |
| 33 ret->lost = false; |
| 34 returned[i] = ret.Pass(); |
| 35 } |
| 36 returner->ReturnResources(returned.Pass()); |
| 37 } |
| 38 |
| 39 } // namespace |
| 40 |
| 41 class FakeDisplayImpl : public Display { |
| 42 public: |
| 43 FakeDisplayImpl(ResourceReturnerPtr returner, |
| 44 InterfaceRequest<Display> request) |
| 45 : returner_(returner.Pass()), binding_(this, request.Pass()) {} |
| 46 ~FakeDisplayImpl() override {} |
| 47 |
| 48 private: |
| 49 // Display implementation |
| 50 void SubmitFrame(mojo::FramePtr frame, |
| 51 const SubmitFrameCallback& callback) override { |
| 52 callback.Run(); |
| 53 if (frame->resources.size() == 0u || !returner_) |
| 54 return; |
| 55 ReturnAll(frame->resources, returner_.get()); |
| 56 } |
| 57 |
| 58 ResourceReturnerPtr returner_; |
| 59 StrongBinding<Display> binding_; |
| 60 }; |
| 61 |
| 62 class FakeDisplayFactoryImpl : public DisplayFactory { |
| 63 public: |
| 64 explicit FakeDisplayFactoryImpl(InterfaceRequest<DisplayFactory> request) |
| 65 : binding_(this, request.Pass()) {} |
| 66 ~FakeDisplayFactoryImpl() override {} |
| 67 |
| 68 private: |
| 69 // DisplayFactory implementation. |
| 70 void Create(mojo::ContextProviderPtr context_provider, |
| 71 ResourceReturnerPtr returner, |
| 72 InterfaceRequest<Display> request) override { |
| 73 new FakeDisplayImpl(returner.Pass(), request.Pass()); |
| 74 } |
| 75 |
| 76 StrongBinding<DisplayFactory> binding_; |
| 77 }; |
| 78 |
| 79 class FakeSurfaceImpl : public Surface { |
| 80 public: |
| 81 FakeSurfaceImpl(uint32_t id_namespace, InterfaceRequest<Surface> request) |
| 82 : id_namespace_(id_namespace), binding_(this, request.Pass()) {} |
| 83 ~FakeSurfaceImpl() override {} |
| 84 |
| 85 // Surface implementation. |
| 86 void GetIdNamespace( |
| 87 const Surface::GetIdNamespaceCallback& callback) override { |
| 88 callback.Run(id_namespace_); |
| 89 } |
| 90 |
| 91 void SetResourceReturner(ResourceReturnerPtr returner) override { |
| 92 returner_ = returner.Pass(); |
| 93 } |
| 94 |
| 95 void CreateSurface(uint32_t local_id) override {} |
| 96 |
| 97 void SubmitFrame(uint32_t local_id, |
| 98 mojo::FramePtr frame, |
| 99 const SubmitFrameCallback& callback) override { |
| 100 callback.Run(); |
| 101 if (frame->resources.size() == 0u || !returner_) |
| 102 return; |
| 103 ReturnAll(frame->resources, returner_.get()); |
| 104 } |
| 105 |
| 106 void DestroySurface(uint32_t local_id) override {} |
| 107 |
| 108 private: |
| 109 const uint32_t id_namespace_; |
| 110 ResourceReturnerPtr returner_; |
| 111 StrongBinding<Surface> binding_; |
| 112 |
| 113 DISALLOW_COPY_AND_ASSIGN(FakeSurfaceImpl); |
| 114 }; |
| 115 |
| 116 FakeSurfacesServiceApplication::FakeSurfacesServiceApplication() |
| 117 : next_id_namespace_(1u) { |
| 118 } |
| 119 |
| 120 FakeSurfacesServiceApplication::~FakeSurfacesServiceApplication() { |
| 121 } |
| 122 |
| 123 void FakeSurfacesServiceApplication::Initialize(mojo::ApplicationImpl* app) { |
| 124 tracing_.Initialize(app); |
| 125 } |
| 126 |
| 127 bool FakeSurfacesServiceApplication::ConfigureIncomingConnection( |
| 128 ApplicationConnection* connection) { |
| 129 connection->AddService<DisplayFactory>(this); |
| 130 connection->AddService<Surface>(this); |
| 131 return true; |
| 132 } |
| 133 |
| 134 void FakeSurfacesServiceApplication::Create( |
| 135 ApplicationConnection* connection, |
| 136 InterfaceRequest<DisplayFactory> request) { |
| 137 new FakeDisplayFactoryImpl(request.Pass()); |
| 138 } |
| 139 |
| 140 void FakeSurfacesServiceApplication::Create(ApplicationConnection* connection, |
| 141 InterfaceRequest<Surface> request) { |
| 142 new FakeSurfaceImpl(next_id_namespace_++, request.Pass()); |
| 143 } |
| 144 |
| 145 } // namespace fake_surfaces |
| 146 |
| 147 MojoResult MojoMain(MojoHandle application_request) { |
| 148 mojo::ApplicationRunnerChromium runner( |
| 149 new fake_surfaces::FakeSurfacesServiceApplication); |
| 150 return runner.Run(application_request); |
| 151 } |
| OLD | NEW |