Chromium Code Reviews| 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_gpu_video_decode_accelerator.h" | |
| 15 #include "chrome/gpu/arc_video_accelerator.h" | |
| 16 #include "components/arc/common/video_accelerator.mojom.h" | |
| 17 #include "mojo/edk/embedder/embedder.h" | |
| 18 #include "mojo/public/cpp/bindings/binding.h" | |
| 19 #include "mojo/public/cpp/bindings/type_converter.h" | |
| 20 | |
| 21 namespace mojo { | |
| 22 | |
| 23 template <> | |
| 24 struct TypeConverter<arc::BufferMetadataPtr, chromeos::arc::BufferMetadata> { | |
| 25 static arc::BufferMetadataPtr Convert( | |
| 26 const chromeos::arc::BufferMetadata& input) { | |
| 27 arc::BufferMetadataPtr result = arc::BufferMetadata::New(); | |
| 28 result->timestamp = input.timestamp; | |
| 29 result->flags = input.flags; | |
| 30 result->bytes_used = input.bytes_used; | |
| 31 return result; | |
| 32 } | |
| 33 }; | |
| 34 | |
| 35 template <> | |
| 36 struct TypeConverter<chromeos::arc::BufferMetadata, arc::BufferMetadataPtr> { | |
| 37 static chromeos::arc::BufferMetadata Convert( | |
| 38 const arc::BufferMetadataPtr& input) { | |
| 39 chromeos::arc::BufferMetadata result; | |
| 40 result.timestamp = input->timestamp; | |
| 41 result.flags = input->flags; | |
| 42 result.bytes_used = input->bytes_used; | |
| 43 return result; | |
| 44 } | |
| 45 }; | |
| 46 | |
| 47 template <> | |
| 48 struct TypeConverter<arc::VideoFormatPtr, chromeos::arc::VideoFormat> { | |
| 49 static arc::VideoFormatPtr Convert(const chromeos::arc::VideoFormat& input) { | |
| 50 arc::VideoFormatPtr result = arc::VideoFormat::New(); | |
| 51 result->pixel_format = input.pixel_format; | |
| 52 result->buffer_size = input.buffer_size; | |
| 53 result->min_num_buffers = input.min_num_buffers; | |
| 54 result->coded_width = input.coded_width; | |
| 55 result->coded_height = input.coded_height; | |
| 56 result->crop_left = input.crop_left; | |
| 57 result->crop_width = input.crop_width; | |
| 58 result->crop_top = input.crop_top; | |
| 59 result->crop_height = input.crop_height; | |
| 60 return result; | |
| 61 } | |
| 62 }; | |
| 63 | |
| 64 template <> | |
| 65 struct TypeConverter<chromeos::arc::ArcVideoAccelerator::Config, | |
| 66 arc::ArcVideoAcceleratorConfigPtr> { | |
| 67 static chromeos::arc::ArcVideoAccelerator::Config Convert( | |
| 68 const arc::ArcVideoAcceleratorConfigPtr& input) { | |
| 69 chromeos::arc::ArcVideoAccelerator::Config result; | |
| 70 result.device_type = | |
| 71 static_cast<chromeos::arc::ArcVideoAccelerator::Config::DeviceType>( | |
| 72 input->device_type); | |
| 73 result.num_input_buffers = input->num_input_buffers; | |
| 74 result.input_pixel_format = input->input_pixel_format; | |
| 75 return result; | |
| 76 } | |
| 77 }; | |
| 78 | |
| 79 } // namespace mojo | |
| 80 | |
| 81 namespace chromeos { | |
| 82 namespace arc { | |
| 83 | |
| 84 class GpuArcVideoService::AcceleratorStub | |
| 85 : public ::arc::VideoAcceleratorService, | |
| 86 public ArcVideoAccelerator::Client { | |
| 87 public: | |
| 88 // |owner| outlives AcceleratorStub. | |
| 89 explicit AcceleratorStub(GpuArcVideoService* owner) | |
| 90 : owner_(owner), binding_(this) {} | |
| 91 | |
| 92 ~AcceleratorStub() override { DCHECK(thread_checker_.CalledOnValidThread()); } | |
| 93 | |
| 94 bool Connect(const std::string& token) { | |
| 95 DVLOG(2) << "Connect"; | |
| 96 | |
| 97 mojo::ScopedMessagePipeHandle server_pipe = | |
| 98 mojo::edk::CreateParentMessagePipe(token); | |
| 99 if (!server_pipe.is_valid()) { | |
| 100 LOG(ERROR) << "Invalid pipe"; | |
| 101 return false; | |
| 102 } | |
| 103 | |
| 104 client_.Bind(mojo::InterfacePtrInfo<::arc::VideoAcceleratorServiceClient>( | |
| 105 std::move(server_pipe), 0u)); | |
| 106 | |
| 107 // base::Unretained is safe because we own |client_| | |
| 108 client_.set_connection_error_handler( | |
| 109 base::Bind(&GpuArcVideoService::AcceleratorStub::OnConnectionError, | |
| 110 base::Unretained(this))); | |
| 111 | |
| 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 uint64_t offset, | |
| 169 uint64_t length) override { | |
| 170 DVLOG(2) << "BindSharedMemoryCallback port=" << port << ", index=" << index | |
| 171 << ", offset=" << offset << ", length=" << length; | |
| 172 // TODO(kcwu) make sure do we need special care for invalid handle? | |
| 173 mojo::edk::ScopedPlatformHandle scoped_platform_handle; | |
| 174 MojoResult mojo_result = mojo::edk::PassWrappedPlatformHandle( | |
| 175 ashmem_handle.release().value(), &scoped_platform_handle); | |
| 176 DCHECK_EQ(mojo_result, MOJO_RESULT_OK); | |
| 177 | |
| 178 int fd = scoped_platform_handle.release().handle; | |
| 179 accelerator_->BindSharedMemory(static_cast<PortType>(port), index, fd, | |
| 180 static_cast<size_t>(offset), | |
| 181 static_cast<size_t>(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 int fd = scoped_platform_handle.release().handle; | |
| 194 accelerator_->BindDmabuf(static_cast<PortType>(port), index, 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(uint64_t number) override { | |
| 206 DVLOG(2) << "SetNumberOfOutputBuffers number=" << number; | |
| 207 accelerator_->SetNumberOfOutputBuffers(static_cast<size_t>(number)); | |
| 208 } | |
| 209 | |
| 210 void Reset() override { accelerator_->Reset(); } | |
| 211 | |
| 212 private: | |
| 213 base::ThreadChecker thread_checker_; | |
| 214 GpuArcVideoService* const owner_; | |
| 215 scoped_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 mojo::edk::ScopedPlatformHandle child_handle = | |
| 232 mojo::edk::ChildProcessLaunched(0); | |
|
Luis Héctor Chávez
2016/04/06 15:23:13
nit: Use a constant.
constexpr base::ProcessHandl
kcwu
2016/04/07 01:02:27
Done.
| |
| 233 | |
| 234 MojoHandle wrapped_handle; | |
| 235 MojoResult wrap_result = mojo::edk::CreatePlatformHandleWrapper( | |
| 236 std::move(child_handle), &wrapped_handle); | |
| 237 if (wrap_result != MOJO_RESULT_OK) { | |
| 238 LOG(WARNING) << "Pipe failed to wrap handles. Closing: " << wrap_result; | |
| 239 callback.Run(mojo::ScopedHandle(), std::string()); | |
| 240 return; | |
| 241 } | |
| 242 | |
| 243 scoped_ptr<AcceleratorStub> stub(new AcceleratorStub(this)); | |
| 244 | |
| 245 std::string token = mojo::edk::GenerateRandomToken(); | |
| 246 if (!stub->Connect(token)) { | |
| 247 callback.Run(mojo::ScopedHandle(), std::string()); | |
| 248 return; | |
| 249 } | |
| 250 accelerator_stubs_.insert(std::make_pair(stub.get(), std::move(stub))); | |
| 251 | |
| 252 callback.Run(mojo::ScopedHandle(mojo::Handle(wrapped_handle)), token); | |
| 253 } | |
| 254 | |
| 255 void GpuArcVideoService::RemoveClient(AcceleratorStub* stub) { | |
| 256 accelerator_stubs_.erase(stub); | |
| 257 } | |
| 258 | |
| 259 } // namespace arc | |
| 260 } // namespace chromeos | |
| OLD | NEW |