OLD | NEW |
| (Empty) |
1 // Copyright 2016 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 "chrome/browser/chromeos/arc/gpu_arc_video_service_host.h" | |
6 | |
7 #include <string> | |
8 #include <utility> | |
9 | |
10 #include "base/location.h" | |
11 #include "base/logging.h" | |
12 #include "base/message_loop/message_loop.h" | |
13 #include "base/threading/thread_task_runner_handle.h" | |
14 #include "components/arc/arc_bridge_service.h" | |
15 #include "components/arc/common/video_accelerator.mojom.h" | |
16 #include "content/public/browser/browser_thread.h" | |
17 #include "content/public/browser/gpu_service_registry.h" | |
18 #include "mojo/edk/embedder/embedder.h" | |
19 #include "mojo/edk/embedder/platform_channel_pair.h" | |
20 #include "mojo/public/cpp/bindings/strong_binding.h" | |
21 #include "services/service_manager/public/cpp/interface_provider.h" | |
22 | |
23 namespace arc { | |
24 | |
25 namespace { | |
26 | |
27 void DeprecatedConnectToVideoAcceleratorServiceOnIOThread( | |
28 mojom::VideoAcceleratorServiceClientRequest request) { | |
29 // Note |request| is not a ServiceRequest. It is a ClientRequest but doesn't | |
30 // request for a Client. Instead, it requests for a Service while specified | |
31 // the client. It works this odd way because the interfaces were modeled as | |
32 // arc's "Host notifies to Instance::Init", not mojo's typical "Client | |
33 // registers to Service". | |
34 // TODO(kcwu): revise the interface. | |
35 content::GetGpuRemoteInterfaces()->GetInterface(std::move(request)); | |
36 } | |
37 | |
38 void ConnectToVideoAcceleratorServiceOnIOThread( | |
39 mojom::VideoAcceleratorServiceRequest request) { | |
40 content::GetGpuRemoteInterfaces()->GetInterface(std::move(request)); | |
41 } | |
42 | |
43 } // namespace | |
44 | |
45 class VideoAcceleratorFactoryService : public mojom::VideoAcceleratorFactory { | |
46 public: | |
47 VideoAcceleratorFactoryService() = default; | |
48 | |
49 void Create(mojom::VideoAcceleratorServiceRequest request) override { | |
50 content::BrowserThread::PostTask( | |
51 content::BrowserThread::IO, FROM_HERE, | |
52 base::Bind(&ConnectToVideoAcceleratorServiceOnIOThread, | |
53 base::Passed(&request))); | |
54 } | |
55 | |
56 private: | |
57 DISALLOW_COPY_AND_ASSIGN(VideoAcceleratorFactoryService); | |
58 }; | |
59 | |
60 GpuArcVideoServiceHost::GpuArcVideoServiceHost(ArcBridgeService* bridge_service) | |
61 : ArcService(bridge_service), binding_(this) { | |
62 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
63 arc_bridge_service()->video()->AddObserver(this); | |
64 } | |
65 | |
66 GpuArcVideoServiceHost::~GpuArcVideoServiceHost() { | |
67 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
68 arc_bridge_service()->video()->RemoveObserver(this); | |
69 } | |
70 | |
71 void GpuArcVideoServiceHost::OnInstanceReady() { | |
72 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
73 auto* video_instance = | |
74 arc_bridge_service()->video()->GetInstanceForMethod("Init"); | |
75 DCHECK(video_instance); | |
76 video_instance->Init(binding_.CreateInterfacePtrAndBind()); | |
77 } | |
78 | |
79 void GpuArcVideoServiceHost::DeprecatedOnRequestArcVideoAcceleratorChannel( | |
80 const DeprecatedOnRequestArcVideoAcceleratorChannelCallback& callback) { | |
81 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
82 | |
83 // Hardcode pid 0 since it is unused in mojo. | |
84 const base::ProcessHandle kUnusedChildProcessHandle = | |
85 base::kNullProcessHandle; | |
86 mojo::edk::PlatformChannelPair channel_pair; | |
87 std::string child_token = mojo::edk::GenerateRandomToken(); | |
88 mojo::edk::ChildProcessLaunched(kUnusedChildProcessHandle, | |
89 channel_pair.PassServerHandle(), child_token); | |
90 | |
91 MojoHandle wrapped_handle; | |
92 MojoResult wrap_result = mojo::edk::CreatePlatformHandleWrapper( | |
93 channel_pair.PassClientHandle(), &wrapped_handle); | |
94 if (wrap_result != MOJO_RESULT_OK) { | |
95 LOG(ERROR) << "Pipe failed to wrap handles. Closing: " << wrap_result; | |
96 callback.Run(mojo::ScopedHandle(), std::string()); | |
97 return; | |
98 } | |
99 mojo::ScopedHandle child_handle{mojo::Handle(wrapped_handle)}; | |
100 | |
101 std::string token = mojo::edk::GenerateRandomToken(); | |
102 mojo::ScopedMessagePipeHandle server_pipe = | |
103 mojo::edk::CreateParentMessagePipe(token, child_token); | |
104 if (!server_pipe.is_valid()) { | |
105 LOG(ERROR) << "Invalid pipe"; | |
106 callback.Run(mojo::ScopedHandle(), std::string()); | |
107 return; | |
108 } | |
109 callback.Run(std::move(child_handle), token); | |
110 | |
111 content::BrowserThread::PostTask( | |
112 content::BrowserThread::IO, FROM_HERE, | |
113 base::Bind( | |
114 &DeprecatedConnectToVideoAcceleratorServiceOnIOThread, | |
115 base::Passed(mojo::MakeRequest<mojom::VideoAcceleratorServiceClient>( | |
116 std::move(server_pipe))))); | |
117 } | |
118 | |
119 void GpuArcVideoServiceHost::OnBootstrapVideoAcceleratorFactory( | |
120 const OnBootstrapVideoAcceleratorFactoryCallback& callback) { | |
121 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
122 | |
123 // Hardcode pid 0 since it is unused in mojo. | |
124 const base::ProcessHandle kUnusedChildProcessHandle = | |
125 base::kNullProcessHandle; | |
126 mojo::edk::PlatformChannelPair channel_pair; | |
127 std::string child_token = mojo::edk::GenerateRandomToken(); | |
128 mojo::edk::ChildProcessLaunched(kUnusedChildProcessHandle, | |
129 channel_pair.PassServerHandle(), child_token); | |
130 | |
131 MojoHandle wrapped_handle; | |
132 MojoResult wrap_result = mojo::edk::CreatePlatformHandleWrapper( | |
133 channel_pair.PassClientHandle(), &wrapped_handle); | |
134 if (wrap_result != MOJO_RESULT_OK) { | |
135 LOG(ERROR) << "Pipe failed to wrap handles. Closing: " << wrap_result; | |
136 callback.Run(mojo::ScopedHandle(), std::string()); | |
137 return; | |
138 } | |
139 mojo::ScopedHandle child_handle{mojo::Handle(wrapped_handle)}; | |
140 | |
141 std::string token = mojo::edk::GenerateRandomToken(); | |
142 mojo::ScopedMessagePipeHandle server_pipe = | |
143 mojo::edk::CreateParentMessagePipe(token, child_token); | |
144 if (!server_pipe.is_valid()) { | |
145 LOG(ERROR) << "Invalid pipe"; | |
146 callback.Run(mojo::ScopedHandle(), std::string()); | |
147 return; | |
148 } | |
149 callback.Run(std::move(child_handle), token); | |
150 | |
151 mojo::MakeStrongBinding(base::MakeUnique<VideoAcceleratorFactoryService>(), | |
152 mojo::MakeRequest<mojom::VideoAcceleratorFactory>( | |
153 std::move(server_pipe))); | |
154 } | |
155 | |
156 } // namespace arc | |
OLD | NEW |