| OLD | NEW |
| (Empty) |
| 1 // Copyright 2015 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 "content/common/gpu/media/gpu_jpeg_decode_accelerator.h" | |
| 6 | |
| 7 #include <stdint.h> | |
| 8 | |
| 9 #include <memory> | |
| 10 #include <utility> | |
| 11 | |
| 12 #include "base/bind.h" | |
| 13 #include "base/containers/hash_tables.h" | |
| 14 #include "base/logging.h" | |
| 15 #include "base/memory/shared_memory.h" | |
| 16 #include "base/single_thread_task_runner.h" | |
| 17 #include "base/stl_util.h" | |
| 18 #include "base/thread_task_runner_handle.h" | |
| 19 #include "base/trace_event/trace_event.h" | |
| 20 #include "build/build_config.h" | |
| 21 #include "gpu/ipc/service/gpu_channel.h" | |
| 22 #include "ipc/ipc_message_macros.h" | |
| 23 #include "ipc/message_filter.h" | |
| 24 #include "media/filters/jpeg_parser.h" | |
| 25 #include "media/gpu/ipc/common/media_messages.h" | |
| 26 #include "ui/gfx/geometry/size.h" | |
| 27 | |
| 28 #if defined(OS_CHROMEOS) | |
| 29 #if defined(ARCH_CPU_X86_FAMILY) | |
| 30 #include "content/common/gpu/media/vaapi_jpeg_decode_accelerator.h" | |
| 31 #endif | |
| 32 #if defined(USE_V4L2_CODEC) | |
| 33 #include "content/common/gpu/media/v4l2_device.h" | |
| 34 #include "content/common/gpu/media/v4l2_jpeg_decode_accelerator.h" | |
| 35 #endif | |
| 36 #endif | |
| 37 | |
| 38 namespace { | |
| 39 | |
| 40 void DecodeFinished(std::unique_ptr<base::SharedMemory> shm) { | |
| 41 // Do nothing. Because VideoFrame is backed by |shm|, the purpose of this | |
| 42 // function is to just keep reference of |shm| to make sure it lives util | |
| 43 // decode finishes. | |
| 44 } | |
| 45 | |
| 46 bool VerifyDecodeParams(const AcceleratedJpegDecoderMsg_Decode_Params& params) { | |
| 47 const int kJpegMaxDimension = UINT16_MAX; | |
| 48 if (params.coded_size.IsEmpty() || | |
| 49 params.coded_size.width() > kJpegMaxDimension || | |
| 50 params.coded_size.height() > kJpegMaxDimension) { | |
| 51 LOG(ERROR) << "invalid coded_size " << params.coded_size.ToString(); | |
| 52 return false; | |
| 53 } | |
| 54 | |
| 55 if (!base::SharedMemory::IsHandleValid(params.output_video_frame_handle)) { | |
| 56 LOG(ERROR) << "invalid output_video_frame_handle"; | |
| 57 return false; | |
| 58 } | |
| 59 | |
| 60 if (params.output_buffer_size < | |
| 61 media::VideoFrame::AllocationSize(media::PIXEL_FORMAT_I420, | |
| 62 params.coded_size)) { | |
| 63 LOG(ERROR) << "output_buffer_size is too small: " | |
| 64 << params.output_buffer_size; | |
| 65 return false; | |
| 66 } | |
| 67 | |
| 68 return true; | |
| 69 } | |
| 70 | |
| 71 } // namespace | |
| 72 | |
| 73 namespace content { | |
| 74 | |
| 75 class GpuJpegDecodeAccelerator::Client | |
| 76 : public media::JpegDecodeAccelerator::Client, | |
| 77 public base::NonThreadSafe { | |
| 78 public: | |
| 79 Client(content::GpuJpegDecodeAccelerator* owner, int32_t route_id) | |
| 80 : owner_(owner->AsWeakPtr()), route_id_(route_id) {} | |
| 81 | |
| 82 ~Client() override { DCHECK(CalledOnValidThread()); } | |
| 83 | |
| 84 // media::JpegDecodeAccelerator::Client implementation. | |
| 85 void VideoFrameReady(int32_t bitstream_buffer_id) override { | |
| 86 DCHECK(CalledOnValidThread()); | |
| 87 if (owner_) | |
| 88 owner_->NotifyDecodeStatus(route_id_, bitstream_buffer_id, | |
| 89 media::JpegDecodeAccelerator::NO_ERRORS); | |
| 90 } | |
| 91 | |
| 92 void NotifyError(int32_t bitstream_buffer_id, | |
| 93 media::JpegDecodeAccelerator::Error error) override { | |
| 94 DCHECK(CalledOnValidThread()); | |
| 95 if (owner_) | |
| 96 owner_->NotifyDecodeStatus(route_id_, bitstream_buffer_id, error); | |
| 97 } | |
| 98 | |
| 99 void Decode(const media::BitstreamBuffer& bitstream_buffer, | |
| 100 const scoped_refptr<media::VideoFrame>& video_frame) { | |
| 101 DCHECK(CalledOnValidThread()); | |
| 102 DCHECK(accelerator_); | |
| 103 accelerator_->Decode(bitstream_buffer, video_frame); | |
| 104 } | |
| 105 | |
| 106 void set_accelerator( | |
| 107 std::unique_ptr<media::JpegDecodeAccelerator> accelerator) { | |
| 108 DCHECK(CalledOnValidThread()); | |
| 109 accelerator_ = std::move(accelerator); | |
| 110 } | |
| 111 | |
| 112 private: | |
| 113 base::WeakPtr<content::GpuJpegDecodeAccelerator> owner_; | |
| 114 int32_t route_id_; | |
| 115 std::unique_ptr<media::JpegDecodeAccelerator> accelerator_; | |
| 116 }; | |
| 117 | |
| 118 // Create, destroy, and RemoveClient run on child thread. All other methods run | |
| 119 // on IO thread. | |
| 120 class GpuJpegDecodeAccelerator::MessageFilter : public IPC::MessageFilter { | |
| 121 public: | |
| 122 explicit MessageFilter(GpuJpegDecodeAccelerator* owner) | |
| 123 : owner_(owner->AsWeakPtr()), | |
| 124 child_task_runner_(owner_->child_task_runner_), | |
| 125 io_task_runner_(owner_->io_task_runner_) {} | |
| 126 | |
| 127 void OnChannelError() override { sender_ = nullptr; } | |
| 128 | |
| 129 void OnChannelClosing() override { sender_ = nullptr; } | |
| 130 | |
| 131 void OnFilterAdded(IPC::Sender* sender) override { sender_ = sender; } | |
| 132 | |
| 133 bool OnMessageReceived(const IPC::Message& msg) override { | |
| 134 const int32_t route_id = msg.routing_id(); | |
| 135 if (client_map_.find(route_id) == client_map_.end()) | |
| 136 return false; | |
| 137 | |
| 138 bool handled = true; | |
| 139 IPC_BEGIN_MESSAGE_MAP_WITH_PARAM(MessageFilter, msg, &route_id) | |
| 140 IPC_MESSAGE_HANDLER(AcceleratedJpegDecoderMsg_Decode, OnDecodeOnIOThread) | |
| 141 IPC_MESSAGE_HANDLER(AcceleratedJpegDecoderMsg_Destroy, | |
| 142 OnDestroyOnIOThread) | |
| 143 IPC_MESSAGE_UNHANDLED(handled = false) | |
| 144 IPC_END_MESSAGE_MAP() | |
| 145 return handled; | |
| 146 } | |
| 147 | |
| 148 bool SendOnIOThread(IPC::Message* message) { | |
| 149 DCHECK(!message->is_sync()); | |
| 150 if (!sender_) { | |
| 151 delete message; | |
| 152 return false; | |
| 153 } | |
| 154 return sender_->Send(message); | |
| 155 } | |
| 156 | |
| 157 void AddClientOnIOThread(int32_t route_id, | |
| 158 Client* client, | |
| 159 base::Callback<void(bool)> response) { | |
| 160 DCHECK(io_task_runner_->BelongsToCurrentThread()); | |
| 161 DCHECK(client_map_.count(route_id) == 0); | |
| 162 | |
| 163 client_map_[route_id] = client; | |
| 164 response.Run(true); | |
| 165 } | |
| 166 | |
| 167 void OnDestroyOnIOThread(const int32_t* route_id) { | |
| 168 DCHECK(io_task_runner_->BelongsToCurrentThread()); | |
| 169 const auto& it = client_map_.find(*route_id); | |
| 170 DCHECK(it != client_map_.end()); | |
| 171 Client* client = it->second; | |
| 172 DCHECK(client); | |
| 173 client_map_.erase(it); | |
| 174 | |
| 175 child_task_runner_->PostTask( | |
| 176 FROM_HERE, base::Bind(&MessageFilter::DestroyClient, this, client)); | |
| 177 } | |
| 178 | |
| 179 void DestroyClient(Client* client) { | |
| 180 DCHECK(child_task_runner_->BelongsToCurrentThread()); | |
| 181 delete client; | |
| 182 if (owner_) | |
| 183 owner_->ClientRemoved(); | |
| 184 } | |
| 185 | |
| 186 void NotifyDecodeStatusOnIOThread(int32_t route_id, | |
| 187 int32_t buffer_id, | |
| 188 media::JpegDecodeAccelerator::Error error) { | |
| 189 DCHECK(io_task_runner_->BelongsToCurrentThread()); | |
| 190 SendOnIOThread(new AcceleratedJpegDecoderHostMsg_DecodeAck( | |
| 191 route_id, buffer_id, error)); | |
| 192 } | |
| 193 | |
| 194 void OnDecodeOnIOThread( | |
| 195 const int32_t* route_id, | |
| 196 const AcceleratedJpegDecoderMsg_Decode_Params& params) { | |
| 197 DCHECK(io_task_runner_->BelongsToCurrentThread()); | |
| 198 DCHECK(route_id); | |
| 199 TRACE_EVENT0("jpeg", "GpuJpegDecodeAccelerator::MessageFilter::OnDecode"); | |
| 200 | |
| 201 if (!VerifyDecodeParams(params)) { | |
| 202 NotifyDecodeStatusOnIOThread( | |
| 203 *route_id, params.input_buffer.id(), | |
| 204 media::JpegDecodeAccelerator::INVALID_ARGUMENT); | |
| 205 if (base::SharedMemory::IsHandleValid(params.output_video_frame_handle)) | |
| 206 base::SharedMemory::CloseHandle(params.output_video_frame_handle); | |
| 207 return; | |
| 208 } | |
| 209 | |
| 210 // For handles in |params|, from now on, |params.output_video_frame_handle| | |
| 211 // is taken cared by scoper. |params.input_buffer.handle()| need to be | |
| 212 // closed manually for early exits. | |
| 213 std::unique_ptr<base::SharedMemory> output_shm( | |
| 214 new base::SharedMemory(params.output_video_frame_handle, false)); | |
| 215 if (!output_shm->Map(params.output_buffer_size)) { | |
| 216 LOG(ERROR) << "Could not map output shared memory for input buffer id " | |
| 217 << params.input_buffer.id(); | |
| 218 NotifyDecodeStatusOnIOThread( | |
| 219 *route_id, params.input_buffer.id(), | |
| 220 media::JpegDecodeAccelerator::PLATFORM_FAILURE); | |
| 221 base::SharedMemory::CloseHandle(params.input_buffer.handle()); | |
| 222 return; | |
| 223 } | |
| 224 | |
| 225 uint8_t* shm_memory = static_cast<uint8_t*>(output_shm->memory()); | |
| 226 scoped_refptr<media::VideoFrame> frame = | |
| 227 media::VideoFrame::WrapExternalSharedMemory( | |
| 228 media::PIXEL_FORMAT_I420, // format | |
| 229 params.coded_size, // coded_size | |
| 230 gfx::Rect(params.coded_size), // visible_rect | |
| 231 params.coded_size, // natural_size | |
| 232 shm_memory, // data | |
| 233 params.output_buffer_size, // data_size | |
| 234 params.output_video_frame_handle, // handle | |
| 235 0, // data_offset | |
| 236 base::TimeDelta()); // timestamp | |
| 237 if (!frame.get()) { | |
| 238 LOG(ERROR) << "Could not create VideoFrame for input buffer id " | |
| 239 << params.input_buffer.id(); | |
| 240 NotifyDecodeStatusOnIOThread( | |
| 241 *route_id, params.input_buffer.id(), | |
| 242 media::JpegDecodeAccelerator::PLATFORM_FAILURE); | |
| 243 base::SharedMemory::CloseHandle(params.input_buffer.handle()); | |
| 244 return; | |
| 245 } | |
| 246 frame->AddDestructionObserver( | |
| 247 base::Bind(DecodeFinished, base::Passed(&output_shm))); | |
| 248 | |
| 249 DCHECK_GT(client_map_.count(*route_id), 0u); | |
| 250 Client* client = client_map_[*route_id]; | |
| 251 client->Decode(params.input_buffer, frame); | |
| 252 } | |
| 253 | |
| 254 protected: | |
| 255 ~MessageFilter() override { | |
| 256 if (client_map_.empty()) | |
| 257 return; | |
| 258 | |
| 259 if (child_task_runner_->BelongsToCurrentThread()) { | |
| 260 STLDeleteValues(&client_map_); | |
| 261 } else { | |
| 262 // Make sure |Client| are deleted on child thread. | |
| 263 std::unique_ptr<ClientMap> client_map(new ClientMap); | |
| 264 client_map->swap(client_map_); | |
| 265 | |
| 266 child_task_runner_->PostTask( | |
| 267 FROM_HERE, | |
| 268 base::Bind(&DeleteClientMapOnChildThread, base::Passed(&client_map))); | |
| 269 } | |
| 270 } | |
| 271 | |
| 272 private: | |
| 273 using ClientMap = base::hash_map<int32_t, Client*>; | |
| 274 | |
| 275 // Must be static because this method runs after destructor. | |
| 276 static void DeleteClientMapOnChildThread( | |
| 277 std::unique_ptr<ClientMap> client_map) { | |
| 278 STLDeleteValues(client_map.get()); | |
| 279 } | |
| 280 | |
| 281 base::WeakPtr<GpuJpegDecodeAccelerator> owner_; | |
| 282 | |
| 283 // GPU child task runner. | |
| 284 scoped_refptr<base::SingleThreadTaskRunner> child_task_runner_; | |
| 285 | |
| 286 // GPU IO task runner. | |
| 287 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner_; | |
| 288 | |
| 289 // The sender to which this filter was added. | |
| 290 IPC::Sender* sender_; | |
| 291 | |
| 292 // A map from route id to JpegDecodeAccelerator. | |
| 293 // Unless in destructor (maybe on child thread), |client_map_| should | |
| 294 // only be accessed on IO thread. | |
| 295 ClientMap client_map_; | |
| 296 }; | |
| 297 | |
| 298 GpuJpegDecodeAccelerator::GpuJpegDecodeAccelerator( | |
| 299 gpu::GpuChannel* channel, | |
| 300 const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner) | |
| 301 : channel_(channel), | |
| 302 child_task_runner_(base::ThreadTaskRunnerHandle::Get()), | |
| 303 io_task_runner_(io_task_runner), | |
| 304 client_number_(0) { | |
| 305 } | |
| 306 | |
| 307 GpuJpegDecodeAccelerator::~GpuJpegDecodeAccelerator() { | |
| 308 DCHECK(CalledOnValidThread()); | |
| 309 if (filter_) { | |
| 310 channel_->RemoveFilter(filter_.get()); | |
| 311 } | |
| 312 } | |
| 313 | |
| 314 void GpuJpegDecodeAccelerator::AddClient(int32_t route_id, | |
| 315 base::Callback<void(bool)> response) { | |
| 316 DCHECK(CalledOnValidThread()); | |
| 317 | |
| 318 // When adding non-chromeos platforms, VideoCaptureGpuJpegDecoder::Initialize | |
| 319 // needs to be updated. | |
| 320 | |
| 321 // This list is ordered by priority of use. | |
| 322 const GpuJpegDecodeAccelerator::CreateJDAFp create_jda_fps[] = { | |
| 323 &GpuJpegDecodeAccelerator::CreateV4L2JDA, | |
| 324 &GpuJpegDecodeAccelerator::CreateVaapiJDA, | |
| 325 }; | |
| 326 | |
| 327 std::unique_ptr<Client> client(new Client(this, route_id)); | |
| 328 std::unique_ptr<media::JpegDecodeAccelerator> accelerator; | |
| 329 for (const auto& create_jda_function : create_jda_fps) { | |
| 330 std::unique_ptr<media::JpegDecodeAccelerator> tmp_accelerator = | |
| 331 (*create_jda_function)(io_task_runner_); | |
| 332 if (tmp_accelerator && tmp_accelerator->Initialize(client.get())) { | |
| 333 accelerator = std::move(tmp_accelerator); | |
| 334 break; | |
| 335 } | |
| 336 } | |
| 337 | |
| 338 if (!accelerator) { | |
| 339 DLOG(ERROR) << "JPEG accelerator Initialize failed"; | |
| 340 response.Run(false); | |
| 341 return; | |
| 342 } | |
| 343 client->set_accelerator(std::move(accelerator)); | |
| 344 | |
| 345 if (!filter_) { | |
| 346 DCHECK_EQ(client_number_, 0); | |
| 347 filter_ = new MessageFilter(this); | |
| 348 // This should be before AddClientOnIOThread. | |
| 349 channel_->AddFilter(filter_.get()); | |
| 350 } | |
| 351 client_number_++; | |
| 352 | |
| 353 // In this PostTask, |client| may leak if |io_task_runner_| is destroyed | |
| 354 // before |client| reached AddClientOnIOThread. However we cannot use scoper | |
| 355 // to protect it because |client| can only be deleted on child thread. The IO | |
| 356 // thread is destroyed at termination, at which point it's ok to leak since | |
| 357 // we're going to tear down the process anyway. So we just crossed fingers | |
| 358 // here instead of making the code unnecessary complicated. | |
| 359 io_task_runner_->PostTask( | |
| 360 FROM_HERE, base::Bind(&MessageFilter::AddClientOnIOThread, filter_, | |
| 361 route_id, client.release(), response)); | |
| 362 } | |
| 363 | |
| 364 void GpuJpegDecodeAccelerator::NotifyDecodeStatus( | |
| 365 int32_t route_id, | |
| 366 int32_t buffer_id, | |
| 367 media::JpegDecodeAccelerator::Error error) { | |
| 368 DCHECK(CalledOnValidThread()); | |
| 369 Send(new AcceleratedJpegDecoderHostMsg_DecodeAck(route_id, buffer_id, error)); | |
| 370 } | |
| 371 | |
| 372 void GpuJpegDecodeAccelerator::ClientRemoved() { | |
| 373 DCHECK(CalledOnValidThread()); | |
| 374 DCHECK_GT(client_number_, 0); | |
| 375 client_number_--; | |
| 376 if (client_number_ == 0) { | |
| 377 channel_->RemoveFilter(filter_.get()); | |
| 378 filter_ = nullptr; | |
| 379 } | |
| 380 } | |
| 381 | |
| 382 bool GpuJpegDecodeAccelerator::Send(IPC::Message* message) { | |
| 383 DCHECK(CalledOnValidThread()); | |
| 384 return channel_->Send(message); | |
| 385 } | |
| 386 | |
| 387 // static | |
| 388 std::unique_ptr<media::JpegDecodeAccelerator> | |
| 389 GpuJpegDecodeAccelerator::CreateV4L2JDA( | |
| 390 const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner) { | |
| 391 std::unique_ptr<media::JpegDecodeAccelerator> decoder; | |
| 392 #if defined(OS_CHROMEOS) && defined(USE_V4L2_CODEC) | |
| 393 scoped_refptr<V4L2Device> device = V4L2Device::Create( | |
| 394 V4L2Device::kJpegDecoder); | |
| 395 if (device) | |
| 396 decoder.reset(new V4L2JpegDecodeAccelerator(device, io_task_runner)); | |
| 397 #endif | |
| 398 return decoder; | |
| 399 } | |
| 400 | |
| 401 // static | |
| 402 std::unique_ptr<media::JpegDecodeAccelerator> | |
| 403 GpuJpegDecodeAccelerator::CreateVaapiJDA( | |
| 404 const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner) { | |
| 405 std::unique_ptr<media::JpegDecodeAccelerator> decoder; | |
| 406 #if defined(OS_CHROMEOS) && defined(ARCH_CPU_X86_FAMILY) | |
| 407 decoder.reset(new VaapiJpegDecodeAccelerator(io_task_runner)); | |
| 408 #endif | |
| 409 return decoder; | |
| 410 } | |
| 411 | |
| 412 // static | |
| 413 bool GpuJpegDecodeAccelerator::IsSupported() { | |
| 414 const GpuJpegDecodeAccelerator::CreateJDAFp create_jda_fps[] = { | |
| 415 &GpuJpegDecodeAccelerator::CreateV4L2JDA, | |
| 416 &GpuJpegDecodeAccelerator::CreateVaapiJDA, | |
| 417 }; | |
| 418 for (const auto& create_jda_function : create_jda_fps) { | |
| 419 std::unique_ptr<media::JpegDecodeAccelerator> accelerator = | |
| 420 (*create_jda_function)(base::ThreadTaskRunnerHandle::Get()); | |
| 421 if (accelerator && accelerator->IsSupported()) | |
| 422 return true; | |
| 423 } | |
| 424 return false; | |
| 425 } | |
| 426 | |
| 427 } // namespace content | |
| OLD | NEW |