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 "base/callback_helpers.h" | |
| 6 #include "base/logging.h" | |
| 7 #include "base/run_loop.h" | |
| 8 #include "chrome/gpu/arc_gpu_video_decode_accelerator.h" | |
| 9 #include "content/public/common/gpu_video_decode_accelerator_factory.h" | |
| 10 | |
| 11 namespace chromeos { | |
| 12 namespace arc { | |
| 13 | |
| 14 ArcGpuVideoDecodeAccelerator::InputRecord::InputRecord( | |
| 15 int32_t bitstream_buffer_id, | |
| 16 uint32_t buffer_index, | |
| 17 int64_t timestamp) | |
| 18 : bitstream_buffer_id(bitstream_buffer_id), | |
| 19 buffer_index(buffer_index), | |
| 20 timestamp(timestamp) {} | |
| 21 | |
| 22 ArcGpuVideoDecodeAccelerator::ArcGpuVideoDecodeAccelerator() | |
| 23 : pending_eos_output_buffer_(false), | |
| 24 arc_client_(nullptr), | |
| 25 next_bitstream_buffer_id_(0), | |
| 26 output_buffer_size_(0) {} | |
| 27 | |
| 28 ArcGpuVideoDecodeAccelerator::~ArcGpuVideoDecodeAccelerator() {} | |
| 29 | |
| 30 // An arbitrary chosen limit of the number of buffers. The number of | |
| 31 // buffers used is requested from the client side (less trusted). | |
| 32 static const size_t kMaxBufferCount = 128; | |
| 33 | |
| 34 bool ArcGpuVideoDecodeAccelerator::Initialize( | |
| 35 const Config& config, | |
| 36 ArcVideoAccelerator::Client* client) { | |
| 37 DVLOG(5) << "Initialize(device=" << config.device_type | |
| 38 << ", input_pixel_format=" << config.input_pixel_format | |
| 39 << ", num_input_buffers=" << config.num_input_buffers << ")"; | |
| 40 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 41 if (config.device_type != Config::DEVICE_DECODER) | |
| 42 return false; | |
| 43 DCHECK(client); | |
| 44 DCHECK(!arc_client_); | |
| 45 arc_client_ = client; | |
| 46 | |
| 47 if (config.num_input_buffers > kMaxBufferCount) { | |
| 48 DLOG(ERROR) << "Request too many buffers: " << config.num_input_buffers; | |
| 49 return false; | |
| 50 } | |
| 51 input_buffer_info_.resize(config.num_input_buffers); | |
| 52 | |
| 53 media::VideoDecodeAccelerator::Config vda_config; | |
| 54 switch (config.input_pixel_format) { | |
| 55 case HAL_PIXEL_FORMAT_H264: | |
| 56 vda_config.profile = media::H264PROFILE_MAIN; | |
| 57 break; | |
| 58 case HAL_PIXEL_FORMAT_VP8: | |
| 59 vda_config.profile = media::VP8PROFILE_ANY; | |
| 60 break; | |
| 61 default: | |
| 62 DLOG(ERROR) << "Unsupported input format: " << config.input_pixel_format; | |
| 63 return false; | |
| 64 } | |
| 65 vda_config.output_mode = | |
| 66 media::VideoDecodeAccelerator::Config::OutputMode::IMPORT; | |
| 67 | |
| 68 scoped_ptr<content::GpuVideoDecodeAcceleratorFactory> vda_factory = | |
| 69 content::GpuVideoDecodeAcceleratorFactory::CreateWithNoGL(); | |
| 70 vda_ = std::move(vda_factory->CreateVDA(this, vda_config)); | |
| 71 if (!vda_) { | |
| 72 DLOG(ERROR) << "Failed to create VDA."; | |
| 73 return false; | |
| 74 } | |
| 75 return true; | |
| 76 } | |
| 77 | |
| 78 void ArcGpuVideoDecodeAccelerator::SetNumberOfOutputBuffers(size_t number) { | |
| 79 DVLOG(5) << "SetNumberOfOutputBuffers(" << number << ")"; | |
| 80 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 81 if (!vda_) { | |
| 82 DLOG(ERROR) << "VDA not initialized"; | |
| 83 arc_client_->OnError(ILLEGAL_STATE); | |
|
kcwu
2016/03/18 06:30:11
If not Initialize() yet, arc_client_ will be proba
Owen Lin
2016/03/24 03:01:10
Done.
| |
| 84 return; | |
| 85 } | |
| 86 | |
| 87 if (number > kMaxBufferCount) { | |
| 88 DLOG(ERROR) << "Too many buffers: " << number; | |
| 89 arc_client_->OnError(INVALID_ARGUMENT); | |
| 90 return; | |
| 91 } | |
| 92 | |
| 93 std::vector<media::PictureBuffer> buffers; | |
| 94 for (int32_t id = 0, n = number; id < n; ++id) { | |
| 95 // TODO: Make sure the |coded_size| is what we want. | |
| 96 buffers.push_back(media::PictureBuffer(id, coded_size_, 0)); | |
| 97 } | |
| 98 vda_->AssignPictureBuffers(buffers); | |
| 99 | |
| 100 pending_import_buffer_.clear(); | |
| 101 pending_import_buffer_.resize(number); | |
| 102 } | |
| 103 | |
| 104 void ArcGpuVideoDecodeAccelerator::BindSharedMemory(PortType port, | |
| 105 uint32_t index, | |
| 106 int ashmem_fd, | |
| 107 off_t offset, | |
| 108 size_t length) { | |
| 109 DVLOG(5) << "ArcGVDA::BindSharedMemory, offset: " << offset | |
| 110 << ", length: " << length; | |
| 111 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 112 if (!vda_) { | |
| 113 DLOG(ERROR) << "VDA not initialized"; | |
| 114 arc_client_->OnError(ILLEGAL_STATE); | |
| 115 return; | |
| 116 } | |
| 117 | |
| 118 // Make sure we will close the file descriptor. | |
| 119 base::ScopedFD handle(ashmem_fd); | |
| 120 if (port != PORT_INPUT) { | |
| 121 DLOG(ERROR) << "SharedBuffer is only supported for input"; | |
| 122 arc_client_->OnError(INVALID_ARGUMENT); | |
| 123 return; | |
| 124 } | |
| 125 if (!ValidatePortAndIndex(port, index)) { | |
| 126 arc_client_->OnError(INVALID_ARGUMENT); | |
| 127 return; | |
| 128 } | |
| 129 InputBufferInfo* input_info = &input_buffer_info_[index]; | |
| 130 input_info->handle = std::move(handle); | |
| 131 input_info->offset = offset; | |
| 132 input_info->length = length; | |
| 133 } | |
| 134 | |
| 135 void ArcGpuVideoDecodeAccelerator::BindDmabuf(PortType port, | |
| 136 uint32_t index, | |
| 137 int dmabuf_fd) { | |
| 138 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 139 | |
| 140 if (!vda_) { | |
| 141 DLOG(ERROR) << "VDA not initialized"; | |
| 142 arc_client_->OnError(ILLEGAL_STATE); | |
| 143 return; | |
| 144 } | |
| 145 | |
| 146 // Make sure we will close the file descriptor. | |
| 147 base::ScopedFD handle(dmabuf_fd); | |
| 148 if (port != PORT_OUTPUT) { | |
| 149 DLOG(ERROR) << "GraphicBuffer is only supported for input"; | |
| 150 arc_client_->OnError(INVALID_ARGUMENT); | |
| 151 return; | |
| 152 } | |
| 153 if (!ValidatePortAndIndex(port, index)) { | |
| 154 arc_client_->OnError(INVALID_ARGUMENT); | |
| 155 return; | |
| 156 } | |
| 157 pending_import_buffer_[index] = std::move(handle); | |
| 158 } | |
| 159 | |
| 160 void ArcGpuVideoDecodeAccelerator::UseBuffer(PortType port, | |
| 161 uint32_t index, | |
| 162 const BufferMetadata& metadata) { | |
| 163 DVLOG(5) << "UseBuffer(port=" << port << ", index=" << index | |
| 164 << ", metadata=(bytes_used=" << metadata.bytes_used | |
| 165 << ", timestamp=" << metadata.timestamp << ")"; | |
| 166 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 167 if (!vda_) { | |
| 168 DLOG(ERROR) << "VDA not initialized"; | |
| 169 arc_client_->OnError(ILLEGAL_STATE); | |
| 170 return; | |
| 171 } | |
| 172 if (!ValidatePortAndIndex(port, index)) { | |
| 173 arc_client_->OnError(INVALID_ARGUMENT); | |
| 174 return; | |
| 175 } | |
| 176 switch (port) { | |
| 177 case PORT_INPUT: { | |
| 178 InputBufferInfo* input_info = &input_buffer_info_[index]; | |
| 179 int32_t bitstream_buffer_id = next_bitstream_buffer_id_; | |
| 180 // Mask against 30 bits, to avoid (undefined) wraparound on signed | |
| 181 // integer. | |
| 182 next_bitstream_buffer_id_ = (next_bitstream_buffer_id_ + 1) & 0x3FFFFFFF; | |
| 183 SetInputRecord(bitstream_buffer_id, index, metadata.timestamp); | |
| 184 int dup_fd = HANDLE_EINTR(dup(input_info->handle.get())); | |
| 185 if (dup_fd < 0) { | |
| 186 DLOG(ERROR) << "dup() failed."; | |
| 187 arc_client_->OnError(PLATFORM_FAILURE); | |
| 188 return; | |
| 189 } | |
| 190 vda_->Decode(media::BitstreamBuffer( | |
| 191 bitstream_buffer_id, base::SharedMemoryHandle(dup_fd, true), | |
| 192 metadata.bytes_used, input_info->offset)); | |
| 193 if (metadata.flags & BUFFER_FLAG_EOS) { | |
| 194 vda_->Flush(true); | |
| 195 } | |
| 196 break; | |
| 197 } | |
| 198 case PORT_OUTPUT: { | |
| 199 SendEosIfNeededOrReusePicture(index); | |
| 200 break; | |
| 201 } | |
| 202 default: | |
| 203 NOTREACHED(); | |
| 204 } | |
| 205 } | |
| 206 | |
| 207 void ArcGpuVideoDecodeAccelerator::Reset() { | |
| 208 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 209 if (!vda_) { | |
| 210 DLOG(ERROR) << "VDA not initialized"; | |
| 211 arc_client_->OnError(ILLEGAL_STATE); | |
| 212 return; | |
| 213 } | |
| 214 base::RunLoop loop; | |
| 215 reset_done_callback_ = loop.QuitClosure(); | |
| 216 vda_->Reset(); | |
| 217 base::MessageLoop::ScopedNestableTaskAllower allow( | |
| 218 base::MessageLoop::current()); | |
| 219 // Wait for the ResetDone callback. | |
| 220 loop.Run(); | |
| 221 } | |
| 222 | |
| 223 void ArcGpuVideoDecodeAccelerator::ProvidePictureBuffers( | |
| 224 size_t requested_num_of_buffers, | |
| 225 const gfx::Size& dimensions, | |
| 226 uint32_t texture_target) { | |
| 227 DVLOG(5) << "ProvidePictureBuffers(" | |
| 228 << "requested_num_of_buffers=" << requested_num_of_buffers | |
| 229 << ", dimensions=" << dimensions.ToString() << ")"; | |
| 230 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 231 coded_size_ = dimensions; | |
| 232 // TODO(owenlin): use VDA::GetOutputFormat() here and calculate correct | |
| 233 // |buffer_size|. | |
| 234 VideoFormat video_format; | |
| 235 video_format.buffer_size = dimensions.GetArea() * 3 / 2; | |
| 236 output_buffer_size_ = video_format.buffer_size; | |
| 237 video_format.min_num_buffers = requested_num_of_buffers; | |
| 238 video_format.coded_width = dimensions.width(); | |
| 239 video_format.coded_height = dimensions.height(); | |
| 240 // TODO(owenlin): How to get visible size? | |
| 241 video_format.crop_top = 0; | |
| 242 video_format.crop_left = 0; | |
| 243 video_format.crop_width = dimensions.width(); | |
| 244 video_format.crop_height = dimensions.height(); | |
| 245 arc_client_->OnOutputFormatChanged(video_format); | |
| 246 } | |
| 247 | |
| 248 void ArcGpuVideoDecodeAccelerator::DismissPictureBuffer( | |
| 249 int32_t picture_buffer) { | |
| 250 // no-op | |
| 251 } | |
| 252 | |
| 253 void ArcGpuVideoDecodeAccelerator::PictureReady(const media::Picture& picture) { | |
| 254 DVLOG(5) << "PictureReady(picture_buffer_id=" << picture.picture_buffer_id() | |
| 255 << ", bitstream_buffer_id=" << picture.bitstream_buffer_id(); | |
| 256 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 257 | |
| 258 // Empty buffer, returned in Flushing. | |
| 259 if (picture.bitstream_buffer_id() == -1) { | |
| 260 buffers_pending_eos_.push(picture.picture_buffer_id()); | |
| 261 return; | |
| 262 } | |
| 263 InputRecord* input_record = FindInputRecord(picture.bitstream_buffer_id()); | |
| 264 if (input_record == nullptr) { | |
| 265 DLOG(ERROR) << "Cannot find for bitstream buffer id: " | |
| 266 << picture.bitstream_buffer_id(); | |
| 267 arc_client_->OnError(PLATFORM_FAILURE); | |
| 268 return; | |
| 269 } | |
| 270 | |
| 271 BufferMetadata metadata; | |
| 272 metadata.timestamp = input_record->timestamp; | |
| 273 metadata.bytes_used = output_buffer_size_; | |
| 274 arc_client_->OnBufferDone(PORT_OUTPUT, picture.picture_buffer_id(), metadata); | |
| 275 } | |
| 276 | |
| 277 void ArcGpuVideoDecodeAccelerator::NotifyEndOfBitstreamBuffer( | |
| 278 int32_t bitstream_buffer_id) { | |
| 279 DVLOG(5) << "NotifyEndOfBitstreamBuffer(" << bitstream_buffer_id << ")"; | |
| 280 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 281 InputRecord* input_record = FindInputRecord(bitstream_buffer_id); | |
| 282 if (input_record == nullptr) { | |
| 283 arc_client_->OnError(PLATFORM_FAILURE); | |
| 284 return; | |
| 285 } | |
| 286 arc_client_->OnBufferDone(PORT_INPUT, input_record->buffer_index, | |
| 287 BufferMetadata()); | |
| 288 } | |
| 289 | |
| 290 void ArcGpuVideoDecodeAccelerator::NotifyFlushDone() { | |
| 291 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 292 pending_eos_output_buffer_ = true; | |
| 293 while (!buffers_pending_eos_.empty()) { | |
| 294 SendEosIfNeededOrReusePicture(buffers_pending_eos_.front()); | |
| 295 buffers_pending_eos_.pop(); | |
| 296 } | |
| 297 } | |
| 298 | |
| 299 void ArcGpuVideoDecodeAccelerator::NotifyResetDone() { | |
| 300 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 301 base::ResetAndReturn(&reset_done_callback_).Run(); | |
| 302 } | |
| 303 | |
| 304 static ArcVideoAccelerator::Error ConvertErrorCode( | |
| 305 media::VideoDecodeAccelerator::Error error) { | |
| 306 switch (error) { | |
| 307 case media::VideoDecodeAccelerator::ILLEGAL_STATE: | |
| 308 return ArcVideoAccelerator::ILLEGAL_STATE; | |
| 309 case media::VideoDecodeAccelerator::INVALID_ARGUMENT: | |
| 310 return ArcVideoAccelerator::INVALID_ARGUMENT; | |
| 311 case media::VideoDecodeAccelerator::UNREADABLE_INPUT: | |
| 312 return ArcVideoAccelerator::UNREADABLE_INPUT; | |
| 313 case media::VideoDecodeAccelerator::PLATFORM_FAILURE: | |
| 314 return ArcVideoAccelerator::PLATFORM_FAILURE; | |
| 315 default: | |
| 316 DLOG(ERROR) << "Unknown error: " << error; | |
| 317 return ArcVideoAccelerator::PLATFORM_FAILURE; | |
| 318 } | |
| 319 } | |
| 320 | |
| 321 void ArcGpuVideoDecodeAccelerator::NotifyError( | |
| 322 media::VideoDecodeAccelerator::Error error) { | |
| 323 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 324 DLOG(ERROR) << "Error notified: " << error; | |
| 325 arc_client_->OnError(ConvertErrorCode(error)); | |
| 326 } | |
| 327 | |
| 328 void ArcGpuVideoDecodeAccelerator::SendEosIfNeededOrReusePicture( | |
| 329 uint32_t index) { | |
| 330 if (pending_eos_output_buffer_) { | |
| 331 BufferMetadata metadata; | |
| 332 metadata.flags = BUFFER_FLAG_EOS; | |
| 333 arc_client_->OnBufferDone(PORT_OUTPUT, index, metadata); | |
| 334 pending_eos_output_buffer_ = false; | |
| 335 } else { | |
| 336 if (pending_import_buffer_[index].is_valid()) { | |
| 337 std::vector<gfx::GpuMemoryBufferHandle> buffers; | |
| 338 buffers.push_back(gfx::GpuMemoryBufferHandle()); | |
| 339 buffers.back().native_pixmap_handle.fd = | |
| 340 base::FileDescriptor(pending_import_buffer_[index].release(), true); | |
| 341 vda_->ImportBufferForPicture(index, buffers); | |
| 342 DCHECK(!pending_import_buffer_[index].is_valid()); | |
| 343 } else { | |
| 344 vda_->ReusePictureBuffer(index); | |
| 345 } | |
| 346 } | |
| 347 } | |
| 348 | |
| 349 void ArcGpuVideoDecodeAccelerator::SetInputRecord(int32_t bitstream_buffer_id, | |
| 350 uint32_t buffer_index, | |
| 351 int64_t timestamp) { | |
| 352 input_records_.push_front( | |
| 353 InputRecord(bitstream_buffer_id, buffer_index, timestamp)); | |
| 354 // The same value copied from media::GpuVideoDecoder. | |
| 355 const size_t kMaxNumberOfInputRecords = 128; | |
| 356 if (input_records_.size() > kMaxNumberOfInputRecords) | |
| 357 input_records_.pop_back(); | |
| 358 } | |
| 359 | |
| 360 ArcGpuVideoDecodeAccelerator::InputRecord* | |
| 361 ArcGpuVideoDecodeAccelerator::FindInputRecord(int32_t bitstream_buffer_id) { | |
| 362 for (auto& record : input_records_) { | |
| 363 if (record.bitstream_buffer_id == bitstream_buffer_id) | |
| 364 return &record; | |
| 365 } | |
| 366 return nullptr; | |
| 367 } | |
| 368 | |
| 369 bool ArcGpuVideoDecodeAccelerator::ValidatePortAndIndex(PortType port, | |
| 370 uint32_t index) { | |
| 371 switch (port) { | |
| 372 case PORT_INPUT: | |
| 373 if (index >= input_buffer_info_.size()) { | |
| 374 DLOG(ERROR) << "Invalid index: " << index; | |
| 375 return false; | |
| 376 } | |
| 377 return true; | |
| 378 case PORT_OUTPUT: | |
| 379 if (index >= pending_import_buffer_.size()) { | |
| 380 DLOG(ERROR) << "Invalid index: " << index; | |
| 381 return false; | |
| 382 } | |
| 383 return true; | |
| 384 default: | |
| 385 DLOG(ERROR) << "Invalid port: " << port; | |
| 386 return false; | |
| 387 } | |
| 388 } | |
| 389 | |
| 390 } // namespace arc | |
| 391 } // namespace chromeos | |
| OLD | NEW |