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/logging.h" | |
11 #include "base/memory/shared_memory.h" | |
12 #include "base/message_loop/message_loop_proxy.h" | |
13 #include "base/trace_event/trace_event.h" | |
14 #include "content/common/gpu/gpu_channel.h" | |
15 #include "content/common/gpu/gpu_messages.h" | |
16 #include "ipc/ipc_message_macros.h" | |
17 #include "ipc/message_filter.h" | |
18 #include "media/filters/jpeg_parser.h" | |
19 #include "ui/gfx/geometry/size.h" | |
20 | |
21 #if defined(OS_CHROMEOS) | |
22 #if defined(ARCH_CPU_X86_FAMILY) | |
mcasas
2015/05/07 00:59:03
#if defined(OS_CHROMEOS) && defined(ARCH_CPU_X86_F
kcwu
2015/05/08 14:42:43
Done.
| |
23 #include "content/common/gpu/media/vaapi_jpeg_decode_accelerator.h" | |
24 #endif // defined(ARCH_CPU_X86_FAMILY) | |
25 #endif | |
26 | |
27 namespace base { | |
28 | |
29 void DefaultDeleter<content::GpuJpegDecodeAccelerator>::operator()( | |
30 void* jpeg_decode_accelerator) const { | |
31 static_cast<content::GpuJpegDecodeAccelerator*>(jpeg_decode_accelerator) | |
32 ->Destroy(); | |
33 } | |
34 | |
35 } // namespace base | |
36 | |
37 namespace content { | |
38 | |
39 class GpuJpegDecodeAccelerator::MessageFilter : public IPC::MessageFilter { | |
40 public: | |
41 MessageFilter(GpuJpegDecodeAccelerator* owner, int32 host_route_id) | |
42 : owner_(owner), host_route_id_(host_route_id) {} | |
43 | |
44 void OnChannelError() override { sender_ = NULL; } | |
45 | |
46 void OnChannelClosing() override { sender_ = NULL; } | |
47 | |
48 void OnFilterAdded(IPC::Sender* sender) override { sender_ = sender; } | |
49 | |
50 void OnFilterRemoved() override { | |
51 owner_->OnFilterRemoved(); | |
52 } | |
53 | |
54 bool OnMessageReceived(const IPC::Message& msg) override { | |
55 if (msg.routing_id() != host_route_id_) | |
56 return false; | |
57 | |
58 IPC_BEGIN_MESSAGE_MAP(MessageFilter, msg) | |
59 IPC_MESSAGE_FORWARD(AcceleratedJpegDecoderMsg_Decode, owner_, | |
60 GpuJpegDecodeAccelerator::OnDecode) | |
61 IPC_MESSAGE_UNHANDLED(return false;) | |
62 IPC_END_MESSAGE_MAP() | |
63 return true; | |
64 } | |
65 | |
66 bool SendOnIOThread(IPC::Message* message) { | |
67 DCHECK(!message->is_sync()); | |
68 if (!sender_) { | |
69 delete message; | |
70 return false; | |
71 } | |
72 return sender_->Send(message); | |
73 } | |
74 | |
75 protected: | |
76 virtual ~MessageFilter() {} | |
77 | |
78 private: | |
79 GpuJpegDecodeAccelerator* owner_; | |
80 int32 host_route_id_; | |
81 // The sender to which this filter was added. | |
82 IPC::Sender* sender_; | |
83 }; | |
84 | |
85 GpuJpegDecodeAccelerator::GpuJpegDecodeAccelerator( | |
86 GpuChannel* channel, | |
87 int32 host_route_id, | |
88 const scoped_refptr<base::MessageLoopProxy>& io_message_loop) | |
89 : channel_(channel), | |
90 host_route_id_(host_route_id), | |
91 filter_removed_(true, false), | |
92 io_message_loop_(io_message_loop) { | |
93 child_message_loop_ = base::MessageLoopProxy::current(); | |
94 } | |
95 | |
96 GpuJpegDecodeAccelerator::~GpuJpegDecodeAccelerator() { | |
97 } | |
98 | |
99 bool GpuJpegDecodeAccelerator::OnMessageReceived(const IPC::Message& msg) { | |
100 bool handled = true; | |
101 IPC_BEGIN_MESSAGE_MAP(GpuJpegDecodeAccelerator, msg) | |
102 IPC_MESSAGE_HANDLER(AcceleratedJpegDecoderMsg_Destroy, OnDestroy) | |
103 IPC_MESSAGE_UNHANDLED(handled = false) | |
104 IPC_END_MESSAGE_MAP() | |
105 return handled; | |
106 } | |
107 | |
108 bool GpuJpegDecodeAccelerator::Initialize() { | |
109 DVLOG(3) << __func__; | |
mcasas
2015/05/07 00:59:03
As we approach something stable you should remove
kcwu
2015/05/08 14:42:43
Done.
| |
110 DCHECK(child_message_loop_->BelongsToCurrentThread()); | |
111 DCHECK(!jpeg_decode_accelerator_.get()); | |
112 | |
113 if (!channel_->AddRoute(host_route_id_, this)) { | |
114 LOG(ERROR) << "GpuJpegDecodeAccelerator::Initialize(): " | |
115 "failed to add route"; | |
116 return false; | |
117 } | |
118 | |
119 filter_ = new MessageFilter(this, host_route_id_); | |
120 channel_->AddFilter(filter_.get()); | |
121 | |
122 // When adding more platforms, GpuJpegDecodeAcceleratorAdapter::Supported need | |
123 // update as well. | |
124 #if defined(OS_CHROMEOS) && defined(ARCH_CPU_X86_FAMILY) | |
125 jpeg_decode_accelerator_.reset( | |
126 new VaapiJpegDecodeAccelerator(io_message_loop_)); | |
127 #else | |
128 DVLOG(1) << "HW JPEG decode acceleration not available."; | |
129 return false; | |
mcasas
2015/05/07 00:59:03
Move these |return false| further up in the method
kcwu
2015/05/08 14:42:42
Done.
| |
130 #endif | |
131 | |
132 return jpeg_decode_accelerator_->Initialize(this); | |
133 } | |
134 | |
135 void GpuJpegDecodeAccelerator::NotifyError( | |
136 int32_t buffer_id, | |
137 media::JpegDecodeAccelerator::Error error) { | |
138 DVLOG(3) << __func__; | |
139 Send(new AcceleratedJpegDecoderHostMsg_NotifyError(host_route_id_, buffer_id, | |
140 error)); | |
141 } | |
142 | |
143 void GpuJpegDecodeAccelerator::VideoFrameReady(int32_t bitstream_buffer_id) { | |
144 // This is called from JDA's decode thread. | |
145 DVLOG(3) << __func__; | |
146 Send(new AcceleratedJpegDecoderHostMsg_VideoFrameReady(host_route_id_, | |
147 bitstream_buffer_id)); | |
148 } | |
149 | |
150 void DecodeFinished(scoped_ptr<base::SharedMemory> shm) { | |
151 // Do nothing. Because VideoFrame is backed by |shm|, the purpose of this | |
152 // function is to just keep reference of |shm| to make sure it live util | |
mcasas
2015/05/07 00:59:03
s/live/lives/
kcwu
2015/05/08 14:42:42
Done.
| |
153 // decode finished. | |
mcasas
2015/05/07 00:59:03
s/finished/finishes/
kcwu
2015/05/08 14:42:43
Done.
| |
154 DVLOG(3) << __func__; | |
155 } | |
156 | |
157 void GpuJpegDecodeAccelerator::OnDecode( | |
158 const AcceleratedJpegDecoderMsg_Decode_Params& params) { | |
159 DVLOG(3) << __func__; | |
160 DCHECK(io_message_loop_->BelongsToCurrentThread()); | |
161 DCHECK(jpeg_decode_accelerator_.get()); | |
162 TRACE_EVENT0("jpeg", "GpuJpegDecodeAccelerator::OnDecode"); | |
163 | |
164 if (params.input_buffer_id < 0) { | |
165 LOG(ERROR) << "BitstreamBuffer id " << params.input_buffer_id | |
166 << " out of range"; | |
167 NotifyError(params.input_buffer_id, | |
168 media::JpegDecodeAccelerator::INVALID_ARGUMENT); | |
169 return; | |
170 } | |
171 | |
172 media::BitstreamBuffer input_buffer(params.input_buffer_id, | |
173 params.input_buffer_handle, | |
174 params.input_buffer_size); | |
175 | |
176 scoped_ptr<base::SharedMemory> output_shm( | |
177 new base::SharedMemory(params.output_video_frame_handle, false)); | |
178 if (!output_shm->Map(params.output_buffer_size)) { | |
179 LOG(ERROR) << "Could not map output shared memory for input buffer id " | |
180 << params.input_buffer_id; | |
181 NotifyError(params.input_buffer_id, | |
182 media::JpegDecodeAccelerator::PLATFORM_FAILURE); | |
183 return; | |
184 } | |
185 | |
186 uint8* shm_memory = reinterpret_cast<uint8*>(output_shm->memory()); | |
187 scoped_refptr<media::VideoFrame> frame = | |
188 media::VideoFrame::WrapExternalPackedMemory( | |
189 media::VideoFrame::I420, | |
190 params.coded_size, | |
191 gfx::Rect(params.coded_size), | |
192 params.coded_size, | |
193 shm_memory, | |
194 params.output_buffer_size, | |
195 params.output_video_frame_handle, | |
196 0, | |
197 base::TimeDelta(), | |
198 base::Bind(DecodeFinished, base::Passed(&output_shm))); | |
199 | |
200 if (!frame.get()) { | |
201 LOG(ERROR) << "Could not create VideoFrame for input buffer id " | |
202 << params.input_buffer_id; | |
203 NotifyError(params.input_buffer_id, | |
204 media::JpegDecodeAccelerator::PLATFORM_FAILURE); | |
205 return; | |
206 } | |
207 | |
208 jpeg_decode_accelerator_->Decode(input_buffer, frame); | |
209 } | |
210 | |
211 void GpuJpegDecodeAccelerator::OnDestroy() { | |
212 DVLOG(3) << __func__; | |
213 DCHECK(child_message_loop_->BelongsToCurrentThread()); | |
214 DCHECK(jpeg_decode_accelerator_.get()); | |
215 Destroy(); | |
216 } | |
217 | |
218 void GpuJpegDecodeAccelerator::OnFilterRemoved() { | |
219 // We're destroying; cancel all callbacks. | |
220 filter_removed_.Signal(); | |
221 } | |
222 | |
223 void GpuJpegDecodeAccelerator::Destroy() { | |
224 DCHECK(child_message_loop_->BelongsToCurrentThread()); | |
225 // We cannot destroy the JDA before the IO thread message filter is | |
226 // removed however, since we cannot service incoming messages with JDA gone. | |
227 // We cannot simply check for existence of JDA on IO thread though, because | |
228 // we don't want to synchronize the IO thread with the ChildThread. | |
229 // So we have to wait for the RemoveFilter callback here instead and remove | |
230 // the JDA after it arrives and before returning. | |
231 if (filter_.get()) { | |
232 channel_->RemoveFilter(filter_.get()); | |
233 filter_removed_.Wait(); | |
234 } | |
235 | |
236 channel_->RemoveRoute(host_route_id_); | |
237 channel_->ReleaseJpegDecoder(host_route_id_); | |
238 jpeg_decode_accelerator_.reset(); | |
239 | |
240 delete this; | |
241 } | |
242 | |
243 bool GpuJpegDecodeAccelerator::Send(IPC::Message* message) { | |
244 if (io_message_loop_->BelongsToCurrentThread()) | |
245 return filter_->SendOnIOThread(message); | |
246 return channel_->Send(message); | |
247 } | |
248 | |
249 } // namespace content | |
OLD | NEW |