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/gpu/gpu_arc_video_service.h" |
| 6 |
| 7 #include <fcntl.h> |
| 8 |
| 9 #include <utility> |
| 10 |
| 11 #include "base/bind.h" |
| 12 #include "base/logging.h" |
| 13 #include "base/thread_task_runner_handle.h" |
| 14 #include "chrome/gpu/arc_video_accelerator.h" |
| 15 #include "components/arc/common/video_accelerator.mojom.h" |
| 16 #include "mojo/edk/embedder/embedder.h" |
| 17 #include "mojo/public/cpp/bindings/binding.h" |
| 18 #include "mojo/public/cpp/bindings/type_converter.h" |
| 19 |
| 20 namespace mojo { |
| 21 |
| 22 template <> |
| 23 struct TypeConverter<arc::BufferMetadataPtr, chromeos::arc::BufferMetadata> { |
| 24 static arc::BufferMetadataPtr Convert( |
| 25 const chromeos::arc::BufferMetadata& input) { |
| 26 arc::BufferMetadataPtr result = arc::BufferMetadata::New(); |
| 27 result->timestamp = input.timestamp; |
| 28 result->flags = input.flags; |
| 29 result->bytes_used = input.bytes_used; |
| 30 return result; |
| 31 } |
| 32 }; |
| 33 |
| 34 template <> |
| 35 struct TypeConverter<chromeos::arc::BufferMetadata, arc::BufferMetadataPtr> { |
| 36 static chromeos::arc::BufferMetadata Convert( |
| 37 const arc::BufferMetadataPtr& input) { |
| 38 chromeos::arc::BufferMetadata result; |
| 39 result.timestamp = input->timestamp; |
| 40 result.flags = input->flags; |
| 41 result.bytes_used = input->bytes_used; |
| 42 return result; |
| 43 } |
| 44 }; |
| 45 |
| 46 template <> |
| 47 struct TypeConverter<arc::VideoFormatPtr, chromeos::arc::VideoFormat> { |
| 48 static arc::VideoFormatPtr Convert(const chromeos::arc::VideoFormat& input) { |
| 49 arc::VideoFormatPtr result = arc::VideoFormat::New(); |
| 50 result->pixel_format = input.pixel_format; |
| 51 result->buffer_size = input.buffer_size; |
| 52 result->min_num_buffers = input.min_num_buffers; |
| 53 result->coded_width = input.coded_width; |
| 54 result->coded_height = input.coded_height; |
| 55 result->crop_left = input.crop_left; |
| 56 result->crop_width = input.crop_width; |
| 57 result->crop_top = input.crop_top; |
| 58 result->crop_height = input.crop_height; |
| 59 return result; |
| 60 } |
| 61 }; |
| 62 |
| 63 template <> |
| 64 struct TypeConverter<chromeos::arc::ArcVideoAccelerator::Config, |
| 65 arc::ArcVideoAcceleratorConfigPtr> { |
| 66 static chromeos::arc::ArcVideoAccelerator::Config Convert( |
| 67 const arc::ArcVideoAcceleratorConfigPtr& input) { |
| 68 chromeos::arc::ArcVideoAccelerator::Config result; |
| 69 result.device_type = |
| 70 static_cast<chromeos::arc::ArcVideoAccelerator::Config::DeviceType>( |
| 71 input->device_type); |
| 72 result.num_input_buffers = input->num_input_buffers; |
| 73 result.input_pixel_format = input->input_pixel_format; |
| 74 return result; |
| 75 } |
| 76 }; |
| 77 |
| 78 } // namespace mojo |
| 79 |
| 80 namespace chromeos { |
| 81 namespace arc { |
| 82 |
| 83 class GpuArcVideoService::AcceleratorStub |
| 84 : public ::arc::VideoAcceleratorService, |
| 85 public ArcVideoAccelerator::Client { |
| 86 public: |
| 87 // |owner| outlives AcceleratorStub. |
| 88 explicit AcceleratorStub(GpuArcVideoService* owner) |
| 89 : owner_(owner), binding_(this) {} |
| 90 |
| 91 ~AcceleratorStub() override { DCHECK(thread_checker_.CalledOnValidThread()); } |
| 92 |
| 93 bool Connect(const std::string& token) { |
| 94 DVLOG(2) << "Connect"; |
| 95 |
| 96 mojo::ScopedMessagePipeHandle server_pipe = |
| 97 mojo::edk::CreateParentMessagePipe(token); |
| 98 if (!server_pipe.is_valid()) { |
| 99 LOG(ERROR) << "Invalid pipe"; |
| 100 return false; |
| 101 } |
| 102 |
| 103 client_.Bind(mojo::InterfacePtrInfo<::arc::VideoAcceleratorServiceClient>( |
| 104 std::move(server_pipe), 0u)); |
| 105 |
| 106 // base::Unretained is safe because we own |client_| |
| 107 client_.set_connection_error_handler( |
| 108 base::Bind(&GpuArcVideoService::AcceleratorStub::OnConnectionError, |
| 109 base::Unretained(this))); |
| 110 |
| 111 // TODO(kcwu): create ArcGpuVideoDecodeAccelerator here. |
| 112 // accelerator_.reset(new ArcGpuVideoDecodeAccelerator()); |
| 113 |
| 114 ::arc::VideoAcceleratorServicePtr service; |
| 115 binding_.Bind(GetProxy(&service)); |
| 116 // base::Unretained is safe because we own |binding_| |
| 117 binding_.set_connection_error_handler( |
| 118 base::Bind(&GpuArcVideoService::AcceleratorStub::OnConnectionError, |
| 119 base::Unretained(this))); |
| 120 |
| 121 client_->Init(std::move(service)); |
| 122 return true; |
| 123 } |
| 124 |
| 125 void OnConnectionError() { |
| 126 DVLOG(2) << "OnConnectionError"; |
| 127 owner_->RemoveClient(this); |
| 128 // |this| is deleted. |
| 129 } |
| 130 |
| 131 // ArcVideoAccelerator::Client implementation: |
| 132 void OnError(ArcVideoAccelerator::Error error) override { |
| 133 DVLOG(2) << "OnError " << error; |
| 134 client_->OnError( |
| 135 static_cast<::arc::VideoAcceleratorServiceClient::Error>(error)); |
| 136 } |
| 137 |
| 138 void OnBufferDone(PortType port, |
| 139 uint32_t index, |
| 140 const BufferMetadata& metadata) override { |
| 141 DVLOG(2) << "OnBufferDone " << port << "," << index; |
| 142 client_->OnBufferDone(static_cast<::arc::PortType>(port), index, |
| 143 ::arc::BufferMetadata::From(metadata)); |
| 144 } |
| 145 |
| 146 void OnResetDone() override { |
| 147 DVLOG(2) << "OnResetDone"; |
| 148 client_->OnResetDone(); |
| 149 } |
| 150 |
| 151 void OnOutputFormatChanged(const VideoFormat& format) override { |
| 152 DVLOG(2) << "OnOutputFormatChanged"; |
| 153 client_->OnOutputFormatChanged(::arc::VideoFormat::From(format)); |
| 154 } |
| 155 |
| 156 // ::arc::VideoAcceleratorService impementation. |
| 157 void Initialize(::arc::ArcVideoAcceleratorConfigPtr config, |
| 158 const InitializeCallback& callback) override { |
| 159 DVLOG(2) << "Initialize"; |
| 160 bool result = accelerator_->Initialize( |
| 161 config.To<ArcVideoAccelerator::Config>(), this); |
| 162 callback.Run(result); |
| 163 } |
| 164 |
| 165 void BindSharedMemory(::arc::PortType port, |
| 166 uint32_t index, |
| 167 mojo::ScopedHandle ashmem_handle, |
| 168 uint32_t offset, |
| 169 uint32_t length) override { |
| 170 DVLOG(2) << "BindSharedMemoryCallback port=" << port << ", index=" << index |
| 171 << ", offset=" << offset << ", length=" << length; |
| 172 |
| 173 // TODO(kcwu) make sure do we need special care for invalid handle? |
| 174 mojo::edk::ScopedPlatformHandle scoped_platform_handle; |
| 175 MojoResult mojo_result = mojo::edk::PassWrappedPlatformHandle( |
| 176 ashmem_handle.release().value(), &scoped_platform_handle); |
| 177 DCHECK_EQ(mojo_result, MOJO_RESULT_OK); |
| 178 |
| 179 auto fd = base::ScopedFD(scoped_platform_handle.release().handle); |
| 180 accelerator_->BindSharedMemory(static_cast<PortType>(port), index, |
| 181 std::move(fd), offset, length); |
| 182 } |
| 183 |
| 184 void BindDmabuf(::arc::PortType port, |
| 185 uint32_t index, |
| 186 mojo::ScopedHandle dmabuf_handle) override { |
| 187 DVLOG(2) << "BindDmabuf port=" << port << ", index=" << index; |
| 188 mojo::edk::ScopedPlatformHandle scoped_platform_handle; |
| 189 MojoResult mojo_result = mojo::edk::PassWrappedPlatformHandle( |
| 190 dmabuf_handle.release().value(), &scoped_platform_handle); |
| 191 DCHECK_EQ(mojo_result, MOJO_RESULT_OK); |
| 192 |
| 193 auto fd = base::ScopedFD(scoped_platform_handle.release().handle); |
| 194 accelerator_->BindDmabuf(static_cast<PortType>(port), index, std::move(fd)); |
| 195 } |
| 196 |
| 197 void UseBuffer(::arc::PortType port, |
| 198 uint32_t index, |
| 199 ::arc::BufferMetadataPtr metadata) override { |
| 200 DVLOG(2) << "UseBuffer port=" << port << ", index=" << index; |
| 201 accelerator_->UseBuffer(static_cast<PortType>(port), index, |
| 202 metadata.To<BufferMetadata>()); |
| 203 } |
| 204 |
| 205 void SetNumberOfOutputBuffers(uint32_t number) override { |
| 206 DVLOG(2) << "SetNumberOfOutputBuffers number=" << number; |
| 207 accelerator_->SetNumberOfOutputBuffers(number); |
| 208 } |
| 209 |
| 210 void Reset() override { accelerator_->Reset(); } |
| 211 |
| 212 private: |
| 213 base::ThreadChecker thread_checker_; |
| 214 GpuArcVideoService* const owner_; |
| 215 std::unique_ptr<ArcVideoAccelerator> accelerator_; |
| 216 ::arc::VideoAcceleratorServiceClientPtr client_; |
| 217 mojo::Binding<::arc::VideoAcceleratorService> binding_; |
| 218 }; |
| 219 |
| 220 GpuArcVideoService::GpuArcVideoService( |
| 221 mojo::InterfaceRequest<::arc::VideoHost> request) |
| 222 : binding_(this, std::move(request)) {} |
| 223 |
| 224 GpuArcVideoService::~GpuArcVideoService() {} |
| 225 |
| 226 void GpuArcVideoService::OnRequestArcVideoAcceleratorChannel( |
| 227 const OnRequestArcVideoAcceleratorChannelCallback& callback) { |
| 228 DVLOG(1) << "OnRequestArcVideoAcceleratorChannelCallback"; |
| 229 |
| 230 // Hardcode pid 0 since it is unused in mojo. |
| 231 const base::ProcessHandle kUnusedChildProcessHandle = 0; |
| 232 mojo::edk::ScopedPlatformHandle child_handle = |
| 233 mojo::edk::ChildProcessLaunched(kUnusedChildProcessHandle); |
| 234 |
| 235 MojoHandle wrapped_handle; |
| 236 MojoResult wrap_result = mojo::edk::CreatePlatformHandleWrapper( |
| 237 std::move(child_handle), &wrapped_handle); |
| 238 if (wrap_result != MOJO_RESULT_OK) { |
| 239 LOG(WARNING) << "Pipe failed to wrap handles. Closing: " << wrap_result; |
| 240 callback.Run(mojo::ScopedHandle(), std::string()); |
| 241 return; |
| 242 } |
| 243 |
| 244 std::unique_ptr<AcceleratorStub> stub(new AcceleratorStub(this)); |
| 245 |
| 246 std::string token = mojo::edk::GenerateRandomToken(); |
| 247 if (!stub->Connect(token)) { |
| 248 callback.Run(mojo::ScopedHandle(), std::string()); |
| 249 return; |
| 250 } |
| 251 accelerator_stubs_.insert(std::make_pair(stub.get(), std::move(stub))); |
| 252 |
| 253 callback.Run(mojo::ScopedHandle(mojo::Handle(wrapped_handle)), token); |
| 254 } |
| 255 |
| 256 void GpuArcVideoService::RemoveClient(AcceleratorStub* stub) { |
| 257 accelerator_stubs_.erase(stub); |
| 258 } |
| 259 |
| 260 } // namespace arc |
| 261 } // namespace chromeos |
OLD | NEW |