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