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 "base/bind.h" |
| 10 #include "base/containers/hash_tables.h" |
| 11 #include "base/logging.h" |
| 12 #include "base/memory/shared_memory.h" |
| 13 #include "base/single_thread_task_runner.h" |
| 14 #include "base/stl_util.h" |
| 15 #include "base/trace_event/trace_event.h" |
| 16 #include "content/common/gpu/gpu_channel.h" |
| 17 #include "content/common/gpu/gpu_messages.h" |
| 18 #include "ipc/ipc_message_macros.h" |
| 19 #include "ipc/message_filter.h" |
| 20 #include "media/filters/jpeg_parser.h" |
| 21 #include "ui/gfx/geometry/size.h" |
| 22 |
| 23 #if defined(OS_CHROMEOS) && defined(ARCH_CPU_X86_FAMILY) |
| 24 #include "content/common/gpu/media/vaapi_jpeg_decode_accelerator.h" |
| 25 #endif |
| 26 |
| 27 namespace { |
| 28 |
| 29 void DecodeFinished(scoped_ptr<base::SharedMemory> shm) { |
| 30 // Do nothing. Because VideoFrame is backed by |shm|, the purpose of this |
| 31 // function is to just keep reference of |shm| to make sure it lives util |
| 32 // decode finishes. |
| 33 } |
| 34 |
| 35 bool VerifyDecodeParams(const AcceleratedJpegDecoderMsg_Decode_Params& params) { |
| 36 if (params.input_buffer_id < 0) { |
| 37 LOG(ERROR) << "BitstreamBuffer id " << params.input_buffer_id |
| 38 << " out of range"; |
| 39 return false; |
| 40 } |
| 41 |
| 42 const int kJpegMaxDimension = UINT16_MAX; |
| 43 if (params.coded_size.IsEmpty() || |
| 44 params.coded_size.width() > kJpegMaxDimension || |
| 45 params.coded_size.height() > kJpegMaxDimension) { |
| 46 LOG(ERROR) << "invalid coded_size " << params.coded_size.ToString(); |
| 47 return false; |
| 48 } |
| 49 |
| 50 if (!base::SharedMemory::IsHandleValid(params.input_buffer_handle)) { |
| 51 LOG(ERROR) << "invalid input_buffer_handle"; |
| 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::VideoFrame::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 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(scoped_ptr<media::JpegDecodeAccelerator> accelerator) { |
| 107 DCHECK(CalledOnValidThread()); |
| 108 accelerator_ = accelerator.Pass(); |
| 109 } |
| 110 |
| 111 private: |
| 112 base::WeakPtr<content::GpuJpegDecodeAccelerator> owner_; |
| 113 int32 route_id_; |
| 114 scoped_ptr<media::JpegDecodeAccelerator> accelerator_; |
| 115 }; |
| 116 |
| 117 // Create, destroy, and RemoveClient run on child thread. All other methods run |
| 118 // on IO thread. |
| 119 class GpuJpegDecodeAccelerator::MessageFilter : public IPC::MessageFilter { |
| 120 public: |
| 121 explicit MessageFilter(GpuJpegDecodeAccelerator* owner) |
| 122 : owner_(owner->AsWeakPtr()), |
| 123 child_task_runner_(owner_->child_task_runner_), |
| 124 io_task_runner_(owner_->io_task_runner_) {} |
| 125 |
| 126 void OnChannelError() override { sender_ = nullptr; } |
| 127 |
| 128 void OnChannelClosing() override { sender_ = nullptr; } |
| 129 |
| 130 void OnFilterAdded(IPC::Sender* sender) override { sender_ = sender; } |
| 131 |
| 132 bool OnMessageReceived(const IPC::Message& msg) override { |
| 133 const int32 route_id = msg.routing_id(); |
| 134 if (client_map_.find(route_id) == client_map_.end()) |
| 135 return false; |
| 136 |
| 137 bool handled = true; |
| 138 IPC_BEGIN_MESSAGE_MAP_WITH_PARAM(MessageFilter, msg, &route_id) |
| 139 IPC_MESSAGE_HANDLER(AcceleratedJpegDecoderMsg_Decode, OnDecodeOnIOThread) |
| 140 IPC_MESSAGE_HANDLER(AcceleratedJpegDecoderMsg_Destroy, |
| 141 OnDestroyOnIOThread) |
| 142 IPC_MESSAGE_UNHANDLED(handled = false) |
| 143 IPC_END_MESSAGE_MAP() |
| 144 return handled; |
| 145 } |
| 146 |
| 147 bool SendOnIOThread(IPC::Message* message) { |
| 148 DCHECK(!message->is_sync()); |
| 149 if (!sender_) { |
| 150 delete message; |
| 151 return false; |
| 152 } |
| 153 return sender_->Send(message); |
| 154 } |
| 155 |
| 156 void AddClientOnIOThread(int32 route_id, |
| 157 Client* client, |
| 158 IPC::Message* reply_msg) { |
| 159 DCHECK(io_task_runner_->BelongsToCurrentThread()); |
| 160 DCHECK(client_map_.count(route_id) == 0); |
| 161 |
| 162 client_map_[route_id] = client; |
| 163 GpuMsg_CreateJpegDecoder::WriteReplyParams(reply_msg, true); |
| 164 SendOnIOThread(reply_msg); |
| 165 } |
| 166 |
| 167 void OnDestroyOnIOThread(const int32* 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 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* 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.input_buffer_handle)) |
| 206 base::SharedMemory::CloseHandle(params.input_buffer_handle); |
| 207 if (base::SharedMemory::IsHandleValid(params.output_video_frame_handle)) |
| 208 base::SharedMemory::CloseHandle(params.output_video_frame_handle); |
| 209 return; |
| 210 } |
| 211 |
| 212 // For handles in |params|, from now on, |params.output_video_frame_handle| |
| 213 // is taken cared by scoper. |params.input_buffer_handle| need to be closed |
| 214 // manually for early exits. |
| 215 scoped_ptr<base::SharedMemory> output_shm( |
| 216 new base::SharedMemory(params.output_video_frame_handle, false)); |
| 217 if (!output_shm->Map(params.output_buffer_size)) { |
| 218 LOG(ERROR) << "Could not map output shared memory for input buffer id " |
| 219 << params.input_buffer_id; |
| 220 NotifyDecodeStatusOnIOThread( |
| 221 *route_id, params.input_buffer_id, |
| 222 media::JpegDecodeAccelerator::PLATFORM_FAILURE); |
| 223 base::SharedMemory::CloseHandle(params.input_buffer_handle); |
| 224 return; |
| 225 } |
| 226 |
| 227 media::BitstreamBuffer input_buffer(params.input_buffer_id, |
| 228 params.input_buffer_handle, |
| 229 params.input_buffer_size); |
| 230 |
| 231 uint8_t* shm_memory = static_cast<uint8_t*>(output_shm->memory()); |
| 232 scoped_refptr<media::VideoFrame> frame = |
| 233 media::VideoFrame::WrapExternalSharedMemory( |
| 234 media::VideoFrame::I420, // format |
| 235 params.coded_size, // coded_size |
| 236 gfx::Rect(params.coded_size), // visible_rect |
| 237 params.coded_size, // natural_size |
| 238 shm_memory, // data |
| 239 params.output_buffer_size, // data_size |
| 240 params.output_video_frame_handle, // handle |
| 241 0, // data_offset |
| 242 base::TimeDelta()); // timestamp |
| 243 frame->AddDestructionObserver( |
| 244 base::Bind(DecodeFinished, base::Passed(&output_shm))); |
| 245 |
| 246 if (!frame.get()) { |
| 247 LOG(ERROR) << "Could not create VideoFrame for input buffer id " |
| 248 << params.input_buffer_id; |
| 249 NotifyDecodeStatusOnIOThread( |
| 250 *route_id, params.input_buffer_id, |
| 251 media::JpegDecodeAccelerator::PLATFORM_FAILURE); |
| 252 base::SharedMemory::CloseHandle(params.input_buffer_handle); |
| 253 return; |
| 254 } |
| 255 |
| 256 DCHECK_GT(client_map_.count(*route_id), 0u); |
| 257 Client* client = client_map_[*route_id]; |
| 258 client->Decode(input_buffer, frame); |
| 259 } |
| 260 |
| 261 protected: |
| 262 ~MessageFilter() override { |
| 263 if (client_map_.empty()) |
| 264 return; |
| 265 |
| 266 if (child_task_runner_->BelongsToCurrentThread()) { |
| 267 STLDeleteValues(&client_map_); |
| 268 } else { |
| 269 // Make sure |Client| are deleted on child thread. |
| 270 scoped_ptr<ClientMap> client_map(new ClientMap); |
| 271 client_map->swap(client_map_); |
| 272 |
| 273 child_task_runner_->PostTask( |
| 274 FROM_HERE, |
| 275 base::Bind(&DeleteClientMapOnChildThread, base::Passed(&client_map))); |
| 276 } |
| 277 } |
| 278 |
| 279 private: |
| 280 using ClientMap = base::hash_map<int32, Client*>; |
| 281 |
| 282 // Must be static because this method runs after destructor. |
| 283 static void DeleteClientMapOnChildThread(scoped_ptr<ClientMap> client_map) { |
| 284 STLDeleteValues(client_map.get()); |
| 285 } |
| 286 |
| 287 base::WeakPtr<GpuJpegDecodeAccelerator> owner_; |
| 288 |
| 289 // GPU child task runner. |
| 290 scoped_refptr<base::SingleThreadTaskRunner> child_task_runner_; |
| 291 |
| 292 // GPU IO task runner. |
| 293 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner_; |
| 294 |
| 295 // The sender to which this filter was added. |
| 296 IPC::Sender* sender_; |
| 297 |
| 298 // A map from route id to JpegDecodeAccelerator. |
| 299 // Unless in destructor (maybe on child thread), |client_map_| should |
| 300 // only be accessed on IO thread. |
| 301 ClientMap client_map_; |
| 302 }; |
| 303 |
| 304 GpuJpegDecodeAccelerator::GpuJpegDecodeAccelerator( |
| 305 GpuChannel* channel, |
| 306 const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner) |
| 307 : channel_(channel), |
| 308 child_task_runner_(base::ThreadTaskRunnerHandle::Get()), |
| 309 io_task_runner_(io_task_runner), |
| 310 client_number_(0) { |
| 311 } |
| 312 |
| 313 GpuJpegDecodeAccelerator::~GpuJpegDecodeAccelerator() { |
| 314 DCHECK(CalledOnValidThread()); |
| 315 if (filter_) { |
| 316 channel_->RemoveFilter(filter_.get()); |
| 317 } |
| 318 } |
| 319 |
| 320 void GpuJpegDecodeAccelerator::AddClient(int32 route_id, |
| 321 IPC::Message* reply_msg) { |
| 322 DCHECK(CalledOnValidThread()); |
| 323 scoped_ptr<media::JpegDecodeAccelerator> accelerator; |
| 324 |
| 325 // When adding more platforms, GpuJpegDecoder::Supported need |
| 326 // update as well. |
| 327 #if defined(OS_CHROMEOS) && defined(ARCH_CPU_X86_FAMILY) |
| 328 accelerator.reset(new VaapiJpegDecodeAccelerator(io_task_runner_)); |
| 329 #else |
| 330 DVLOG(1) << "HW JPEG decode acceleration not available."; |
| 331 #endif |
| 332 |
| 333 scoped_ptr<Client> client(new Client(this, route_id)); |
| 334 if (!accelerator.get() || !accelerator->Initialize(client.get())) { |
| 335 DLOG(ERROR) << "JPEG accelerator Initialize failed"; |
| 336 GpuMsg_CreateJpegDecoder::WriteReplyParams(reply_msg, false); |
| 337 Send(reply_msg); |
| 338 return; |
| 339 } |
| 340 client->set_accelerator(accelerator.Pass()); |
| 341 |
| 342 if (!filter_) { |
| 343 DCHECK_EQ(client_number_, 0); |
| 344 filter_ = new MessageFilter(this); |
| 345 // This should be before AddClientOnIOThread. |
| 346 channel_->AddFilter(filter_.get()); |
| 347 } |
| 348 client_number_++; |
| 349 |
| 350 // In this PostTask, |client| may leak if |io_task_runner_| is destroyed |
| 351 // before |client| reached AddClientOnIOThread. However we cannot use scoper |
| 352 // to protect it because |client| can only be deleted on child thread. The IO |
| 353 // thread is destroyed at termination, at which point it's ok to leak since |
| 354 // we're going to tear down the process anyway. So we just crossed fingers |
| 355 // here instead of making the code unnecessary complicated. |
| 356 io_task_runner_->PostTask( |
| 357 FROM_HERE, base::Bind(&MessageFilter::AddClientOnIOThread, filter_, |
| 358 route_id, client.release(), reply_msg)); |
| 359 } |
| 360 |
| 361 void GpuJpegDecodeAccelerator::NotifyDecodeStatus( |
| 362 int32 route_id, |
| 363 int32_t buffer_id, |
| 364 media::JpegDecodeAccelerator::Error error) { |
| 365 DCHECK(CalledOnValidThread()); |
| 366 Send(new AcceleratedJpegDecoderHostMsg_DecodeAck(route_id, buffer_id, error)); |
| 367 } |
| 368 |
| 369 void GpuJpegDecodeAccelerator::ClientRemoved() { |
| 370 DCHECK(CalledOnValidThread()); |
| 371 DCHECK_GT(client_number_, 0); |
| 372 client_number_--; |
| 373 if (client_number_ == 0) { |
| 374 channel_->RemoveFilter(filter_.get()); |
| 375 filter_ = nullptr; |
| 376 } |
| 377 } |
| 378 |
| 379 bool GpuJpegDecodeAccelerator::Send(IPC::Message* message) { |
| 380 DCHECK(CalledOnValidThread()); |
| 381 return channel_->Send(message); |
| 382 } |
| 383 |
| 384 } // namespace content |
OLD | NEW |