| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "content/common/gpu/media/gpu_video_decode_accelerator.h" | 5 #include "content/common/gpu/media/gpu_video_decode_accelerator.h" |
| 6 | 6 |
| 7 #include <vector> | 7 #include <vector> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| 11 #include "base/stl_util-inl.h" |
| 12 #include "gpu/command_buffer/common/command_buffer.h" |
| 11 #include "ipc/ipc_message_macros.h" | 13 #include "ipc/ipc_message_macros.h" |
| 12 #include "ipc/ipc_message_utils.h" | 14 #include "ipc/ipc_message_utils.h" |
| 13 #include "content/common/gpu/gpu_channel.h" | 15 #include "content/common/gpu/gpu_channel.h" |
| 16 #include "content/common/gpu/gpu_command_buffer_stub.h" |
| 14 #include "content/common/gpu/gpu_messages.h" | 17 #include "content/common/gpu/gpu_messages.h" |
| 18 #include "content/common/gpu/media/gpu_video_service.h" |
| 15 #include "ui/gfx/size.h" | 19 #include "ui/gfx/size.h" |
| 16 | 20 |
| 17 GpuVideoDecodeAccelerator::GpuVideoDecodeAccelerator( | 21 GpuVideoDecodeAccelerator::GpuVideoDecodeAccelerator( |
| 18 IPC::Message::Sender* sender, | 22 IPC::Message::Sender* sender, |
| 19 int32 host_route_id) | 23 int32 host_route_id, |
| 24 int32 decoder_route_id, |
| 25 GpuCommandBufferStub* stub) |
| 20 : sender_(sender), | 26 : sender_(sender), |
| 21 route_id_(host_route_id), | 27 host_route_id_(host_route_id), |
| 28 decoder_route_id_(decoder_route_id), |
| 29 stub_(stub), |
| 22 video_decode_accelerator_(NULL) { | 30 video_decode_accelerator_(NULL) { |
| 31 stub_->AddSetTokenCallback(base::Bind( |
| 32 &GpuVideoDecodeAccelerator::OnSetToken, this)); |
| 23 } | 33 } |
| 24 | 34 |
| 25 GpuVideoDecodeAccelerator::~GpuVideoDecodeAccelerator() {} | 35 GpuVideoDecodeAccelerator::~GpuVideoDecodeAccelerator() { |
| 36 STLDeleteElements(&deferred_messages_); |
| 37 } |
| 38 |
| 39 void GpuVideoDecodeAccelerator::OnSetToken(int32 token) { |
| 40 // Note: this always retries all deferred messages on every token arrival. |
| 41 // There's an optimization to be done here by only trying messages which are |
| 42 // waiting for tokens which are earlier than |token|. |
| 43 std::vector<IPC::Message*> deferred_messages_copy; |
| 44 std::swap(deferred_messages_copy, deferred_messages_); |
| 45 for (size_t i = 0; i < deferred_messages_copy.size(); ++i) |
| 46 OnMessageReceived(*deferred_messages_copy[i]); |
| 47 STLDeleteElements(&deferred_messages_copy); |
| 48 } |
| 49 |
| 50 bool GpuVideoDecodeAccelerator::DeferMessageIfNeeded( |
| 51 const IPC::Message& msg, bool* deferred) { |
| 52 // Only consider deferring for message types that need it. |
| 53 switch (msg.type()) { |
| 54 case AcceleratedVideoDecoderMsg_GetConfigs::ID: |
| 55 case AcceleratedVideoDecoderMsg_Initialize::ID: |
| 56 case AcceleratedVideoDecoderMsg_Decode::ID: |
| 57 case AcceleratedVideoDecoderMsg_AssignTextures::ID: |
| 58 case AcceleratedVideoDecoderMsg_AssignSysmemBuffers::ID: |
| 59 case AcceleratedVideoDecoderMsg_ReusePictureBuffer::ID: |
| 60 case AcceleratedVideoDecoderMsg_Flush::ID: |
| 61 case AcceleratedVideoDecoderMsg_Abort::ID: |
| 62 break; |
| 63 default: |
| 64 return false; |
| 65 } |
| 66 |
| 67 gpu::ReadWriteTokens tokens; |
| 68 void* iter = NULL; |
| 69 if (!IPC::ParamTraits<gpu::ReadWriteTokens>::Read(&msg, &iter, &tokens)) |
| 70 return false; |
| 71 if (tokens.InRange(stub_->token())) { |
| 72 deferred_messages_.push_back(new IPC::Message(msg)); |
| 73 *deferred = true; |
| 74 } else { |
| 75 *deferred = false; |
| 76 } |
| 77 return true; |
| 78 } |
| 26 | 79 |
| 27 bool GpuVideoDecodeAccelerator::OnMessageReceived(const IPC::Message& msg) { | 80 bool GpuVideoDecodeAccelerator::OnMessageReceived(const IPC::Message& msg) { |
| 81 bool deferred = false; |
| 82 if (!DeferMessageIfNeeded(msg, &deferred)) |
| 83 return false; |
| 84 if (deferred) |
| 85 return true; |
| 86 |
| 28 bool handled = true; | 87 bool handled = true; |
| 29 IPC_BEGIN_MESSAGE_MAP(GpuVideoDecodeAccelerator, msg) | 88 IPC_BEGIN_MESSAGE_MAP(GpuVideoDecodeAccelerator, msg) |
| 30 IPC_MESSAGE_HANDLER(AcceleratedVideoDecoderMsg_GetConfigs, OnGetConfigs) | 89 IPC_MESSAGE_HANDLER(AcceleratedVideoDecoderMsg_GetConfigs, OnGetConfigs) |
| 31 IPC_MESSAGE_HANDLER(AcceleratedVideoDecoderMsg_Initialize, OnInitialize) | 90 IPC_MESSAGE_HANDLER(AcceleratedVideoDecoderMsg_Initialize, OnInitialize) |
| 32 IPC_MESSAGE_HANDLER(AcceleratedVideoDecoderMsg_Decode, OnDecode) | 91 IPC_MESSAGE_HANDLER(AcceleratedVideoDecoderMsg_Decode, OnDecode) |
| 92 IPC_MESSAGE_HANDLER(AcceleratedVideoDecoderMsg_AssignTextures, |
| 93 OnAssignTextures) |
| 33 IPC_MESSAGE_HANDLER(AcceleratedVideoDecoderMsg_AssignSysmemBuffers, | 94 IPC_MESSAGE_HANDLER(AcceleratedVideoDecoderMsg_AssignSysmemBuffers, |
| 34 OnAssignSysmemBuffers) | 95 OnAssignSysmemBuffers) |
| 35 IPC_MESSAGE_HANDLER(AcceleratedVideoDecoderMsg_ReusePictureBuffer, | 96 IPC_MESSAGE_HANDLER(AcceleratedVideoDecoderMsg_ReusePictureBuffer, |
| 36 OnReusePictureBuffer) | 97 OnReusePictureBuffer) |
| 37 IPC_MESSAGE_HANDLER(AcceleratedVideoDecoderMsg_Flush, OnFlush) | 98 IPC_MESSAGE_HANDLER(AcceleratedVideoDecoderMsg_Flush, OnFlush) |
| 38 IPC_MESSAGE_HANDLER(AcceleratedVideoDecoderMsg_Abort, OnAbort) | 99 IPC_MESSAGE_HANDLER(AcceleratedVideoDecoderMsg_Abort, OnAbort) |
| 39 IPC_MESSAGE_UNHANDLED(handled = false) | 100 IPC_MESSAGE_UNHANDLED(handled = false) |
| 40 IPC_END_MESSAGE_MAP() | 101 IPC_END_MESSAGE_MAP() |
| 41 return handled; | 102 return handled; |
| 42 } | 103 } |
| 43 | 104 |
| 44 void GpuVideoDecodeAccelerator::OnChannelConnected(int32 peer_pid) { | 105 void GpuVideoDecodeAccelerator::OnChannelConnected(int32 peer_pid) { |
| 45 // TODO(vmr): Do we have to react on channel connections? | 106 // TODO(vmr): Do we have to react on channel connections? |
| 46 } | 107 } |
| 47 | 108 |
| 48 void GpuVideoDecodeAccelerator::OnChannelError() { | 109 void GpuVideoDecodeAccelerator::OnChannelError() { |
| 49 // TODO(vmr): Do we have to react on channel errors? | 110 // TODO(vmr): Do we have to react on channel errors? |
| 50 } | 111 } |
| 51 | 112 |
| 52 void GpuVideoDecodeAccelerator::ProvidePictureBuffers( | 113 void GpuVideoDecodeAccelerator::ProvidePictureBuffers( |
| 53 uint32 requested_num_of_buffers, | 114 uint32 requested_num_of_buffers, |
| 54 const gfx::Size& dimensions, | 115 const gfx::Size& dimensions, |
| 55 media::VideoDecodeAccelerator::MemoryType type) { | 116 media::VideoDecodeAccelerator::MemoryType type) { |
| 56 if (!Send(new AcceleratedVideoDecoderHostMsg_ProvidePictureBuffers( | 117 if (!Send(new AcceleratedVideoDecoderHostMsg_ProvidePictureBuffers( |
| 57 route_id_, requested_num_of_buffers, dimensions, type))) { | 118 host_route_id_, requested_num_of_buffers, dimensions, type))) { |
| 58 LOG(ERROR) << "Send(AcceleratedVideoDecoderHostMsg_ProvidePictureBuffers) " | 119 LOG(ERROR) << "Send(AcceleratedVideoDecoderHostMsg_ProvidePictureBuffers) " |
| 59 << "failed"; | 120 << "failed"; |
| 60 } | 121 } |
| 61 } | 122 } |
| 62 | 123 |
| 63 void GpuVideoDecodeAccelerator::DismissPictureBuffer( | 124 void GpuVideoDecodeAccelerator::DismissPictureBuffer( |
| 64 int32 picture_buffer_id) { | 125 int32 picture_buffer_id) { |
| 65 // Notify client that picture buffer is now unused. | 126 // Notify client that picture buffer is now unused. |
| 66 if (!Send(new AcceleratedVideoDecoderHostMsg_DismissPictureBuffer( | 127 if (!Send(new AcceleratedVideoDecoderHostMsg_DismissPictureBuffer( |
| 67 route_id_, picture_buffer_id))) { | 128 host_route_id_, picture_buffer_id))) { |
| 68 LOG(ERROR) << "Send(AcceleratedVideoDecoderHostMsg_DismissPictureBuffer) " | 129 LOG(ERROR) << "Send(AcceleratedVideoDecoderHostMsg_DismissPictureBuffer) " |
| 69 << "failed"; | 130 << "failed"; |
| 70 } | 131 } |
| 71 } | 132 } |
| 72 | 133 |
| 73 void GpuVideoDecodeAccelerator::PictureReady( | 134 void GpuVideoDecodeAccelerator::PictureReady( |
| 74 const media::Picture& picture) { | 135 const media::Picture& picture) { |
| 75 if (!Send(new AcceleratedVideoDecoderHostMsg_PictureReady( | 136 if (!Send(new AcceleratedVideoDecoderHostMsg_PictureReady( |
| 76 route_id_, | 137 host_route_id_, |
| 77 picture.picture_buffer_id(), | 138 picture.picture_buffer_id(), |
| 78 picture.bitstream_buffer_id(), | 139 picture.bitstream_buffer_id(), |
| 79 picture.visible_size(), | 140 picture.visible_size(), |
| 80 picture.decoded_size()))) { | 141 picture.decoded_size()))) { |
| 81 LOG(ERROR) << "Send(AcceleratedVideoDecoderHostMsg_PictureReady) failed"; | 142 LOG(ERROR) << "Send(AcceleratedVideoDecoderHostMsg_PictureReady) failed"; |
| 82 } | 143 } |
| 83 } | 144 } |
| 84 | 145 |
| 85 void GpuVideoDecodeAccelerator::NotifyEndOfStream() { | 146 void GpuVideoDecodeAccelerator::NotifyEndOfStream() { |
| 86 Send(new AcceleratedVideoDecoderHostMsg_EndOfStream(route_id_)); | 147 Send(new AcceleratedVideoDecoderHostMsg_EndOfStream(host_route_id_)); |
| 87 } | 148 } |
| 88 | 149 |
| 89 void GpuVideoDecodeAccelerator::NotifyError( | 150 void GpuVideoDecodeAccelerator::NotifyError( |
| 90 media::VideoDecodeAccelerator::Error error) { | 151 media::VideoDecodeAccelerator::Error error) { |
| 91 if (!Send(new AcceleratedVideoDecoderHostMsg_ErrorNotification( | 152 if (!Send(new AcceleratedVideoDecoderHostMsg_ErrorNotification( |
| 92 route_id_, error))) { | 153 host_route_id_, error))) { |
| 93 LOG(ERROR) << "Send(AcceleratedVideoDecoderHostMsg_ErrorNotification) " | 154 LOG(ERROR) << "Send(AcceleratedVideoDecoderHostMsg_ErrorNotification) " |
| 94 << "failed"; | 155 << "failed"; |
| 95 } | 156 } |
| 96 } | 157 } |
| 97 | 158 |
| 98 void GpuVideoDecodeAccelerator::OnGetConfigs( | 159 void GpuVideoDecodeAccelerator::OnGetConfigs( |
| 160 const gpu::ReadWriteTokens& /* tokens */, |
| 99 const std::vector<uint32>& requested, std::vector<uint32>* matched) { | 161 const std::vector<uint32>& requested, std::vector<uint32>* matched) { |
| 162 // TODO(fischman,vrk): this is borked; can't have a VDA before calling |
| 163 // Initialize, but can't call Initialize until we have some configs! |
| 100 if (!video_decode_accelerator_.get()) | 164 if (!video_decode_accelerator_.get()) |
| 101 return; | 165 return; |
| 102 video_decode_accelerator_->GetConfigs(requested, matched); | 166 video_decode_accelerator_->GetConfigs(requested, matched); |
| 103 } | 167 } |
| 104 | 168 |
| 105 void GpuVideoDecodeAccelerator::OnInitialize( | 169 void GpuVideoDecodeAccelerator::OnInitialize( |
| 170 const gpu::ReadWriteTokens& /* tokens */, |
| 106 const std::vector<uint32>& configs) { | 171 const std::vector<uint32>& configs) { |
| 107 if (!video_decode_accelerator_.get()) | 172 DCHECK(!video_decode_accelerator_.get()); |
| 108 return; | 173 GpuVideoService::GetInstance()->InitializeVideoDecoder(decoder_route_id_); |
| 109 | 174 DCHECK(video_decode_accelerator_.get()); |
| 110 video_decode_accelerator_->Initialize(configs); | 175 video_decode_accelerator_->Initialize(configs); |
| 111 } | 176 } |
| 112 | 177 |
| 113 void GpuVideoDecodeAccelerator::OnDecode(int32 id, | 178 void GpuVideoDecodeAccelerator::OnDecode( |
| 114 base::SharedMemoryHandle handle, | 179 const gpu::ReadWriteTokens&, /* tokens */ |
| 115 int32 size) { | 180 base::SharedMemoryHandle handle, int32 id, int32 size) { |
| 116 if (!video_decode_accelerator_.get()) | 181 DCHECK(video_decode_accelerator_.get()); |
| 117 return; | |
| 118 video_decode_accelerator_->Decode(media::BitstreamBuffer(id, handle, size)); | 182 video_decode_accelerator_->Decode(media::BitstreamBuffer(id, handle, size)); |
| 119 } | 183 } |
| 120 | 184 |
| 121 void GpuVideoDecodeAccelerator::AssignGLESBuffers( | 185 void GpuVideoDecodeAccelerator::AssignGLESBuffers( |
| 122 const std::vector<media::GLESBuffer>& buffers) { | 186 const std::vector<media::GLESBuffer>& buffers) { |
| 123 if (!video_decode_accelerator_.get()) | 187 // TODO(fischman,vrk): it's wonky that we handle the AssignTextures message by |
| 124 return; | 188 // handing its contents to GpuVideoService which then turns around and calls |
| 189 // this (public) method. Instead we should make GpuVideoService vend the |
| 190 // translation method we need and use it directly. |
| 191 DCHECK(video_decode_accelerator_.get()); |
| 125 video_decode_accelerator_->AssignGLESBuffers(buffers); | 192 video_decode_accelerator_->AssignGLESBuffers(buffers); |
| 126 } | 193 } |
| 127 | 194 |
| 195 void GpuVideoDecodeAccelerator::OnAssignTextures( |
| 196 const gpu::ReadWriteTokens& /* tokens */, |
| 197 const std::vector<int32>& buffer_ids, |
| 198 const std::vector<uint32>& texture_ids, |
| 199 const std::vector<gfx::Size>& sizes) { |
| 200 GpuVideoService* service = GpuVideoService::GetInstance(); |
| 201 service->AssignTexturesToDecoder( |
| 202 decoder_route_id_, buffer_ids, texture_ids, sizes); |
| 203 } |
| 204 |
| 128 void GpuVideoDecodeAccelerator::OnAssignSysmemBuffers( | 205 void GpuVideoDecodeAccelerator::OnAssignSysmemBuffers( |
| 129 const std::vector<int32>& buffer_ids, | 206 const gpu::ReadWriteTokens& /* tokens */, |
| 130 const std::vector<base::SharedMemoryHandle>& data, | 207 const std::vector<int32> buffer_ids, |
| 131 const std::vector<gfx::Size>& sizes) { | 208 const std::vector<base::SharedMemoryHandle> data, |
| 209 const std::vector<gfx::Size> sizes) { |
| 132 // TODO(vrk): Implement. | 210 // TODO(vrk): Implement. |
| 133 NOTIMPLEMENTED(); | 211 NOTIMPLEMENTED(); |
| 134 } | 212 } |
| 135 | 213 |
| 136 void GpuVideoDecodeAccelerator::OnReusePictureBuffer(int32 picture_buffer_id) { | 214 void GpuVideoDecodeAccelerator::OnReusePictureBuffer( |
| 137 if (!video_decode_accelerator_.get()) | 215 const gpu::ReadWriteTokens& /* tokens */, |
| 138 return; | 216 int32 picture_buffer_id) { |
| 139 | 217 DCHECK(video_decode_accelerator_.get()); |
| 140 video_decode_accelerator_->ReusePictureBuffer(picture_buffer_id); | 218 video_decode_accelerator_->ReusePictureBuffer(picture_buffer_id); |
| 141 } | 219 } |
| 142 | 220 |
| 143 void GpuVideoDecodeAccelerator::OnFlush() { | 221 void GpuVideoDecodeAccelerator::OnFlush( |
| 144 if (!video_decode_accelerator_.get()) | 222 const gpu::ReadWriteTokens& /* tokens */) { |
| 145 return; | 223 DCHECK(video_decode_accelerator_.get()); |
| 146 | 224 video_decode_accelerator_->Flush(); |
| 147 if (!video_decode_accelerator_->Flush()) { | |
| 148 NotifyError( | |
| 149 media::VideoDecodeAccelerator::VIDEODECODERERROR_UNEXPECTED_FLUSH); | |
| 150 } | |
| 151 } | 225 } |
| 152 | 226 |
| 153 void GpuVideoDecodeAccelerator::OnAbort() { | 227 void GpuVideoDecodeAccelerator::OnAbort( |
| 154 if (!video_decode_accelerator_.get()) | 228 const gpu::ReadWriteTokens& /* tokens */) { |
| 155 return; | 229 DCHECK(video_decode_accelerator_.get()); |
| 156 | |
| 157 video_decode_accelerator_->Abort(); | 230 video_decode_accelerator_->Abort(); |
| 158 } | 231 } |
| 159 | 232 |
| 160 void GpuVideoDecodeAccelerator::NotifyEndOfBitstreamBuffer( | 233 void GpuVideoDecodeAccelerator::NotifyEndOfBitstreamBuffer( |
| 161 int32 bitstream_buffer_id) { | 234 int32 bitstream_buffer_id) { |
| 162 if (!Send(new AcceleratedVideoDecoderHostMsg_BitstreamBufferProcessed( | 235 if (!Send(new AcceleratedVideoDecoderHostMsg_BitstreamBufferProcessed( |
| 163 route_id_, bitstream_buffer_id))) { | 236 host_route_id_, bitstream_buffer_id))) { |
| 164 DLOG(ERROR) | 237 DLOG(ERROR) |
| 165 << "Send(AcceleratedVideoDecoderHostMsg_BitstreamBufferProcessed) " | 238 << "Send(AcceleratedVideoDecoderHostMsg_BitstreamBufferProcessed) " |
| 166 << "failed"; | 239 << "failed"; |
| 167 } | 240 } |
| 168 } | 241 } |
| 169 | 242 |
| 170 void GpuVideoDecodeAccelerator::NotifyInitializeDone() { | 243 void GpuVideoDecodeAccelerator::NotifyInitializeDone() { |
| 171 if (!Send(new AcceleratedVideoDecoderHostMsg_InitializeDone(route_id_))) | 244 if (!Send(new AcceleratedVideoDecoderHostMsg_InitializeDone(host_route_id_))) |
| 172 LOG(ERROR) << "Send(AcceleratedVideoDecoderHostMsg_InitializeDone) failed"; | 245 LOG(ERROR) << "Send(AcceleratedVideoDecoderHostMsg_InitializeDone) failed"; |
| 173 } | 246 } |
| 174 | 247 |
| 175 void GpuVideoDecodeAccelerator::NotifyFlushDone() { | 248 void GpuVideoDecodeAccelerator::NotifyFlushDone() { |
| 176 if (!Send(new AcceleratedVideoDecoderHostMsg_FlushDone(route_id_))) | 249 if (!Send(new AcceleratedVideoDecoderHostMsg_FlushDone(host_route_id_))) |
| 177 LOG(ERROR) << "Send(AcceleratedVideoDecoderHostMsg_FlushDone) failed"; | 250 LOG(ERROR) << "Send(AcceleratedVideoDecoderHostMsg_FlushDone) failed"; |
| 178 } | 251 } |
| 179 | 252 |
| 180 void GpuVideoDecodeAccelerator::NotifyAbortDone() { | 253 void GpuVideoDecodeAccelerator::NotifyAbortDone() { |
| 181 if (!Send(new AcceleratedVideoDecoderHostMsg_AbortDone(route_id_))) | 254 if (!Send(new AcceleratedVideoDecoderHostMsg_AbortDone(host_route_id_))) |
| 182 LOG(ERROR) << "Send(AcceleratedVideoDecoderHostMsg_AbortDone) failed"; | 255 LOG(ERROR) << "Send(AcceleratedVideoDecoderHostMsg_AbortDone) failed"; |
| 183 } | 256 } |
| 184 | 257 |
| 185 bool GpuVideoDecodeAccelerator::Send(IPC::Message* message) { | 258 bool GpuVideoDecodeAccelerator::Send(IPC::Message* message) { |
| 186 DCHECK(sender_); | 259 DCHECK(sender_); |
| 187 return sender_->Send(message); | 260 return sender_->Send(message); |
| 188 } | 261 } |
| OLD | NEW |