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_arc_accelerator.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/location.h" |
| 9 #include "base/logging.h" |
| 10 #include "base/single_thread_task_runner.h" |
| 11 #include "base/thread_task_runner_handle.h" |
| 12 |
| 13 #include "content/common/gpu/gpu_channel.h" |
| 14 #include "content/common/gpu/gpu_messages.h" |
| 15 #include "ipc/ipc_listener.h" |
| 16 #include "ipc/ipc_message_macros.h" |
| 17 #include "ipc/ipc_message_utils.h" |
| 18 #include "base/synchronization/waitable_event.h" |
| 19 |
| 20 // HACK |
| 21 namespace media { |
| 22 ArcVideoAccelerator::Client::~Client() {} |
| 23 } |
| 24 |
| 25 namespace content { |
| 26 |
| 27 // Client's all methods are running on arc thread. |
| 28 class GpuArcAccelerator::Client : public IPC::Listener, |
| 29 public IPC::Sender, |
| 30 public media::ArcVideoAccelerator::Client, |
| 31 public base::NonThreadSafe { |
| 32 public: |
| 33 // |owner| outlives Client. |
| 34 Client(GpuArcAccelerator* owner, |
| 35 const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner) |
| 36 : owner_(owner), |
| 37 io_task_runner_(io_task_runner), |
| 38 arc_task_runner_(base::ThreadTaskRunnerHandle::Get()) {} |
| 39 |
| 40 ~Client() override { |
| 41 DCHECK(CalledOnValidThread()); |
| 42 accelerator_.reset(); |
| 43 channel_->Close(); |
| 44 } |
| 45 |
| 46 void Initialize(base::WaitableEvent* shutdown_event, |
| 47 uint32_t device_type, |
| 48 GpuChannel* gpu_channel) { |
| 49 DCHECK(CalledOnValidThread()); |
| 50 DCHECK(!channel_); |
| 51 |
| 52 IPC::ChannelHandle handle; |
| 53 channel_ = IPC::SyncChannel::Create(handle, IPC::Channel::MODE_SERVER, this, |
| 54 io_task_runner_, false, shutdown_event); |
| 55 #if defined(OS_POSIX) |
| 56 base::ScopedFD client_fd = channel_->TakeClientFileDescriptor(); |
| 57 DCHECK(client_fd.is_valid()); |
| 58 handle.socket = base::FileDescriptor(client_fd.Pass()); |
| 59 #endif |
| 60 |
| 61 #if 0 |
| 62 accelerator_.reset(new ArcVideoAcceleratorImp()); |
| 63 #endif |
| 64 uint32_t result = accelerator_->initialize(device_type, this); |
| 65 |
| 66 gpu_channel->Send(new GpuHostMsg_ArcAcceleratorCreated(handle, result)); |
| 67 } |
| 68 |
| 69 // IPC::Sender implementation: |
| 70 bool Send(IPC::Message* msg) override { |
| 71 DCHECK(msg); |
| 72 return channel_->Send(msg); |
| 73 } |
| 74 |
| 75 // IPC::Listener implementation: |
| 76 void OnChannelError() override { |
| 77 DCHECK(CalledOnValidThread()); |
| 78 // RemoveClientOnArcThread will delete |this|. |
| 79 owner_->RemoveClientOnArcThread(this); |
| 80 } |
| 81 |
| 82 // ArcVideoAccelerator::Client implementation: |
| 83 void onBufferDone(size_t index) override { |
| 84 DCHECK(CalledOnValidThread()); |
| 85 // channel_->Send(new ArcAcceleratorHostMsg_BufferDone(index)); |
| 86 } |
| 87 |
| 88 void onOutputFormatChanged( |
| 89 const media::ArcVideoAccelerator::VideoFormat& format) override { |
| 90 DCHECK(CalledOnValidThread()); |
| 91 // channel_->Send(new ArcAcceleratorHostMsg_OutputFormatChanged(format)); |
| 92 } |
| 93 |
| 94 // IPC::Listener implementation: |
| 95 bool OnMessageReceived(const IPC::Message& msg) override { |
| 96 DCHECK(CalledOnValidThread()); |
| 97 |
| 98 IPC_BEGIN_MESSAGE_MAP(Client, msg) |
| 99 IPC_MESSAGE_HANDLER(ArcAcceleratorMsg_BindSharedBuffer, |
| 100 OnBindSharedBuffer) |
| 101 IPC_MESSAGE_HANDLER(ArcAcceleratorMsg_BindGraphicBuffer, |
| 102 OnBindGraphicBuffer) |
| 103 IPC_MESSAGE_HANDLER(ArcAcceleratorMsg_UseBuffer, OnUseBuffer) |
| 104 IPC_MESSAGE_HANDLER(ArcAcceleratorMsg_SetBufferCount, OnSetBufferCount) |
| 105 IPC_MESSAGE_HANDLER(ArcAcceleratorMsg_Reset, OnReset) |
| 106 IPC_MESSAGE_HANDLER(ArcAcceleratorMsg_SetPixelFormat, OnSetPixelFormat) |
| 107 IPC_MESSAGE_UNHANDLED(return false;) |
| 108 IPC_END_MESSAGE_MAP() |
| 109 return true; |
| 110 } |
| 111 |
| 112 void OnBindSharedBuffer(uint32_t port, |
| 113 size_t index, |
| 114 base::FileDescriptor ashmem_fd, |
| 115 size_t offset, |
| 116 size_t length, |
| 117 uint32_t* result) { |
| 118 *result = accelerator_->bindSharedBuffer(port, index, ashmem_fd.fd, offset, |
| 119 length); |
| 120 } |
| 121 |
| 122 void OnBindGraphicBuffer(uint32_t port, |
| 123 size_t index, |
| 124 base::FileDescriptor dmabuf_fd, |
| 125 uint32_t* result) { |
| 126 *result = accelerator_->bindGraphicBuffer(port, index, dmabuf_fd.fd); |
| 127 } |
| 128 |
| 129 void OnUseBuffer(uint32_t port, size_t index, uint32_t* result) { |
| 130 *result = accelerator_->useBuffer(port, index); |
| 131 } |
| 132 |
| 133 void OnSetBufferCount(uint32_t port, |
| 134 size_t in_count, |
| 135 size_t* out_count, |
| 136 uint32_t* result) { |
| 137 size_t count = in_count; |
| 138 *result = accelerator_->setBufferCount(port, &count); |
| 139 *out_count = count; |
| 140 } |
| 141 |
| 142 void OnReset() { accelerator_->reset(); } |
| 143 |
| 144 void OnSetPixelFormat(uint32_t port, |
| 145 uint32_t pixel_format, |
| 146 uint32_t image_size, |
| 147 uint32_t memory_type, |
| 148 uint32_t* result) { |
| 149 media::ArcVideoAccelerator::PixelFormat format = { |
| 150 pixel_format, image_size, memory_type, |
| 151 }; |
| 152 *result = accelerator_->setPixelFormat(port, &format); |
| 153 } |
| 154 |
| 155 private: |
| 156 scoped_ptr<IPC::SyncChannel> channel_; |
| 157 GpuArcAccelerator* owner_; |
| 158 const scoped_refptr<base::SingleThreadTaskRunner> io_task_runner_; |
| 159 const scoped_refptr<base::SingleThreadTaskRunner> arc_task_runner_; |
| 160 scoped_ptr<media::ArcVideoAccelerator> accelerator_; |
| 161 }; |
| 162 |
| 163 GpuArcAccelerator::GpuArcAccelerator( |
| 164 GpuChannel* gpu_channel, |
| 165 base::WaitableEvent* shutdown_event, |
| 166 const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner) |
| 167 : gpu_channel_(gpu_channel), |
| 168 shutdown_event_(shutdown_event), |
| 169 io_task_runner_(io_task_runner), |
| 170 task_runner_(base::ThreadTaskRunnerHandle::Get()), |
| 171 arc_accelerator_thread_("ArcAcceleratorThread") {} |
| 172 |
| 173 GpuArcAccelerator::~GpuArcAccelerator() { |
| 174 DCHECK(CalledOnValidThread()); |
| 175 |
| 176 base::WaitableEvent done(false, false); |
| 177 arc_task_runner_->PostTask( |
| 178 FROM_HERE, base::Bind(&GpuArcAccelerator::RemoveAllClientsOnArcThread, |
| 179 base::Unretained(this), base::Unretained(&done))); |
| 180 done.Wait(); |
| 181 |
| 182 arc_accelerator_thread_.Stop(); |
| 183 } |
| 184 |
| 185 void GpuArcAccelerator::Initialize() { |
| 186 DCHECK(CalledOnValidThread()); |
| 187 |
| 188 arc_accelerator_thread_.Start(); |
| 189 arc_task_runner_ = arc_accelerator_thread_.task_runner(); |
| 190 } |
| 191 |
| 192 void GpuArcAccelerator::CreateClient(uint32_t device_type) { |
| 193 DCHECK(CalledOnValidThread()); |
| 194 |
| 195 // It's safe to Unretained |this| because the thread is owned by |this|. |
| 196 arc_task_runner_->PostTask( |
| 197 FROM_HERE, base::Bind(&GpuArcAccelerator::CreateClientOnArcThread, |
| 198 base::Unretained(this), device_type)); |
| 199 } |
| 200 |
| 201 void GpuArcAccelerator::CreateClientOnArcThread(uint32_t device_type) { |
| 202 DCHECK(arc_task_runner_->BelongsToCurrentThread()); |
| 203 scoped_ptr<Client> client(new Client(this, io_task_runner_)); |
| 204 clients_[client.get()] = client.Pass(); |
| 205 |
| 206 client->Initialize(shutdown_event_, device_type, gpu_channel_); |
| 207 } |
| 208 |
| 209 void GpuArcAccelerator::RemoveClientOnArcThread(Client* client) { |
| 210 DCHECK(arc_task_runner_->BelongsToCurrentThread()); |
| 211 clients_.erase(client); |
| 212 } |
| 213 |
| 214 void GpuArcAccelerator::RemoveAllClientsOnArcThread(base::WaitableEvent* done) { |
| 215 DCHECK(arc_task_runner_->BelongsToCurrentThread()); |
| 216 clients_.clear(); |
| 217 done->Signal(); |
| 218 } |
| 219 |
| 220 } // namespace content |
OLD | NEW |