OLD | NEW |
| (Empty) |
1 // Copyright (c) 2012 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 <dlfcn.h> | |
6 #include <errno.h> | |
7 #include <fcntl.h> | |
8 #include <libdrm/drm_fourcc.h> | |
9 #include <linux/videodev2.h> | |
10 #include <poll.h> | |
11 #include <sys/eventfd.h> | |
12 #include <sys/ioctl.h> | |
13 #include <sys/mman.h> | |
14 | |
15 #include "base/bind.h" | |
16 #include "base/debug/trace_event.h" | |
17 #include "base/memory/shared_memory.h" | |
18 #include "base/message_loop/message_loop.h" | |
19 #include "base/message_loop/message_loop_proxy.h" | |
20 #include "base/posix/eintr_wrapper.h" | |
21 #include "content/common/gpu/media/exynos_video_decode_accelerator.h" | |
22 #include "content/common/gpu/media/h264_parser.h" | |
23 #include "ui/gl/scoped_binders.h" | |
24 | |
25 namespace content { | |
26 | |
27 #define NOTIFY_ERROR(x) \ | |
28 do { \ | |
29 SetDecoderState(kError); \ | |
30 DLOG(ERROR) << "calling NotifyError(): " << x; \ | |
31 NotifyError(x); \ | |
32 } while (0) | |
33 | |
34 #define IOCTL_OR_ERROR_RETURN(fd, type, arg) \ | |
35 do { \ | |
36 if (HANDLE_EINTR(ioctl(fd, type, arg) != 0)) { \ | |
37 DPLOG(ERROR) << __func__ << "(): ioctl() failed: " << #type; \ | |
38 NOTIFY_ERROR(PLATFORM_FAILURE); \ | |
39 return; \ | |
40 } \ | |
41 } while (0) | |
42 | |
43 #define IOCTL_OR_ERROR_RETURN_FALSE(fd, type, arg) \ | |
44 do { \ | |
45 if (HANDLE_EINTR(ioctl(fd, type, arg) != 0)) { \ | |
46 DPLOG(ERROR) << __func__ << "(): ioctl() failed: " << #type; \ | |
47 NOTIFY_ERROR(PLATFORM_FAILURE); \ | |
48 return false; \ | |
49 } \ | |
50 } while (0) | |
51 | |
52 namespace { | |
53 | |
54 // TODO(posciak): remove once we update linux-headers. | |
55 #ifndef V4L2_EVENT_RESOLUTION_CHANGE | |
56 #define V4L2_EVENT_RESOLUTION_CHANGE 5 | |
57 #endif | |
58 | |
59 const char kExynosMfcDevice[] = "/dev/mfc-dec"; | |
60 | |
61 } // anonymous namespace | |
62 | |
63 struct ExynosVideoDecodeAccelerator::BitstreamBufferRef { | |
64 BitstreamBufferRef( | |
65 base::WeakPtr<Client>& client, | |
66 scoped_refptr<base::MessageLoopProxy>& client_message_loop_proxy, | |
67 base::SharedMemory* shm, | |
68 size_t size, | |
69 int32 input_id); | |
70 ~BitstreamBufferRef(); | |
71 const base::WeakPtr<Client> client; | |
72 const scoped_refptr<base::MessageLoopProxy> client_message_loop_proxy; | |
73 const scoped_ptr<base::SharedMemory> shm; | |
74 const size_t size; | |
75 off_t bytes_used; | |
76 const int32 input_id; | |
77 }; | |
78 | |
79 struct ExynosVideoDecodeAccelerator::PictureBufferArrayRef { | |
80 PictureBufferArrayRef(EGLDisplay egl_display); | |
81 ~PictureBufferArrayRef(); | |
82 | |
83 struct PictureBufferRef { | |
84 PictureBufferRef(EGLImageKHR egl_image, int32 picture_id) | |
85 : egl_image(egl_image), picture_id(picture_id) {} | |
86 EGLImageKHR egl_image; | |
87 int32 picture_id; | |
88 }; | |
89 | |
90 EGLDisplay const egl_display; | |
91 std::vector<PictureBufferRef> picture_buffers; | |
92 }; | |
93 | |
94 struct ExynosVideoDecodeAccelerator::EGLSyncKHRRef { | |
95 EGLSyncKHRRef(EGLDisplay egl_display, EGLSyncKHR egl_sync); | |
96 ~EGLSyncKHRRef(); | |
97 EGLDisplay const egl_display; | |
98 EGLSyncKHR egl_sync; | |
99 }; | |
100 | |
101 struct ExynosVideoDecodeAccelerator::PictureRecord { | |
102 PictureRecord(bool cleared, const media::Picture& picture); | |
103 ~PictureRecord(); | |
104 bool cleared; // Whether the texture is cleared and safe to render from. | |
105 media::Picture picture; // The decoded picture. | |
106 }; | |
107 | |
108 ExynosVideoDecodeAccelerator::BitstreamBufferRef::BitstreamBufferRef( | |
109 base::WeakPtr<Client>& client, | |
110 scoped_refptr<base::MessageLoopProxy>& client_message_loop_proxy, | |
111 base::SharedMemory* shm, size_t size, int32 input_id) | |
112 : client(client), | |
113 client_message_loop_proxy(client_message_loop_proxy), | |
114 shm(shm), | |
115 size(size), | |
116 bytes_used(0), | |
117 input_id(input_id) { | |
118 } | |
119 | |
120 ExynosVideoDecodeAccelerator::BitstreamBufferRef::~BitstreamBufferRef() { | |
121 if (input_id >= 0) { | |
122 client_message_loop_proxy->PostTask(FROM_HERE, base::Bind( | |
123 &Client::NotifyEndOfBitstreamBuffer, client, input_id)); | |
124 } | |
125 } | |
126 | |
127 ExynosVideoDecodeAccelerator::PictureBufferArrayRef::PictureBufferArrayRef( | |
128 EGLDisplay egl_display) | |
129 : egl_display(egl_display) {} | |
130 | |
131 ExynosVideoDecodeAccelerator::PictureBufferArrayRef::~PictureBufferArrayRef() { | |
132 for (size_t i = 0; i < picture_buffers.size(); ++i) { | |
133 EGLImageKHR egl_image = picture_buffers[i].egl_image; | |
134 if (egl_image != EGL_NO_IMAGE_KHR) | |
135 eglDestroyImageKHR(egl_display, egl_image); | |
136 } | |
137 } | |
138 | |
139 ExynosVideoDecodeAccelerator::EGLSyncKHRRef::EGLSyncKHRRef( | |
140 EGLDisplay egl_display, EGLSyncKHR egl_sync) | |
141 : egl_display(egl_display), | |
142 egl_sync(egl_sync) { | |
143 } | |
144 | |
145 ExynosVideoDecodeAccelerator::EGLSyncKHRRef::~EGLSyncKHRRef() { | |
146 if (egl_sync != EGL_NO_SYNC_KHR) | |
147 eglDestroySyncKHR(egl_display, egl_sync); | |
148 } | |
149 | |
150 ExynosVideoDecodeAccelerator::MfcInputRecord::MfcInputRecord() | |
151 : at_device(false), | |
152 address(NULL), | |
153 length(0), | |
154 bytes_used(0), | |
155 input_id(-1) { | |
156 } | |
157 | |
158 ExynosVideoDecodeAccelerator::MfcInputRecord::~MfcInputRecord() { | |
159 } | |
160 | |
161 ExynosVideoDecodeAccelerator::MfcOutputRecord::MfcOutputRecord() | |
162 : at_device(false), | |
163 at_client(false), | |
164 egl_image(EGL_NO_IMAGE_KHR), | |
165 egl_sync(EGL_NO_SYNC_KHR), | |
166 picture_id(-1), | |
167 cleared(false) { | |
168 for (size_t i = 0; i < arraysize(fds); ++i) | |
169 fds[i] = -1; | |
170 } | |
171 | |
172 ExynosVideoDecodeAccelerator::MfcOutputRecord::~MfcOutputRecord() {} | |
173 | |
174 ExynosVideoDecodeAccelerator::PictureRecord::PictureRecord( | |
175 bool cleared, | |
176 const media::Picture& picture) | |
177 : cleared(cleared), picture(picture) {} | |
178 | |
179 ExynosVideoDecodeAccelerator::PictureRecord::~PictureRecord() {} | |
180 | |
181 ExynosVideoDecodeAccelerator::ExynosVideoDecodeAccelerator( | |
182 EGLDisplay egl_display, | |
183 Client* client, | |
184 const base::WeakPtr<Client>& io_client, | |
185 const base::Callback<bool(void)>& make_context_current, | |
186 const scoped_refptr<base::MessageLoopProxy>& io_message_loop_proxy) | |
187 : child_message_loop_proxy_(base::MessageLoopProxy::current()), | |
188 io_message_loop_proxy_(io_message_loop_proxy), | |
189 weak_this_(base::AsWeakPtr(this)), | |
190 client_ptr_factory_(client), | |
191 client_(client_ptr_factory_.GetWeakPtr()), | |
192 io_client_(io_client), | |
193 decoder_thread_("ExynosDecoderThread"), | |
194 decoder_state_(kUninitialized), | |
195 decoder_delay_bitstream_buffer_id_(-1), | |
196 decoder_current_input_buffer_(-1), | |
197 decoder_decode_buffer_tasks_scheduled_(0), | |
198 decoder_frames_at_client_(0), | |
199 decoder_flushing_(false), | |
200 resolution_change_pending_(false), | |
201 resolution_change_reset_pending_(false), | |
202 decoder_partial_frame_pending_(false), | |
203 mfc_fd_(-1), | |
204 mfc_input_streamon_(false), | |
205 mfc_input_buffer_queued_count_(0), | |
206 mfc_output_streamon_(false), | |
207 mfc_output_buffer_queued_count_(0), | |
208 mfc_output_buffer_pixelformat_(0), | |
209 mfc_output_dpb_size_(0), | |
210 picture_clearing_count_(0), | |
211 device_poll_thread_("ExynosDevicePollThread"), | |
212 device_poll_interrupt_fd_(-1), | |
213 make_context_current_(make_context_current), | |
214 egl_display_(egl_display), | |
215 video_profile_(media::VIDEO_CODEC_PROFILE_UNKNOWN) {} | |
216 | |
217 ExynosVideoDecodeAccelerator::~ExynosVideoDecodeAccelerator() { | |
218 DCHECK(!decoder_thread_.IsRunning()); | |
219 DCHECK(!device_poll_thread_.IsRunning()); | |
220 | |
221 if (device_poll_interrupt_fd_ != -1) { | |
222 close(device_poll_interrupt_fd_); | |
223 device_poll_interrupt_fd_ = -1; | |
224 } | |
225 if (mfc_fd_ != -1) { | |
226 DestroyMfcInputBuffers(); | |
227 DestroyMfcOutputBuffers(); | |
228 close(mfc_fd_); | |
229 mfc_fd_ = -1; | |
230 } | |
231 | |
232 // These maps have members that should be manually destroyed, e.g. file | |
233 // descriptors, mmap() segments, etc. | |
234 DCHECK(mfc_input_buffer_map_.empty()); | |
235 DCHECK(mfc_output_buffer_map_.empty()); | |
236 } | |
237 | |
238 bool ExynosVideoDecodeAccelerator::Initialize( | |
239 media::VideoCodecProfile profile) { | |
240 DVLOG(3) << "Initialize()"; | |
241 DCHECK(child_message_loop_proxy_->BelongsToCurrentThread()); | |
242 DCHECK_EQ(decoder_state_, kUninitialized); | |
243 | |
244 switch (profile) { | |
245 case media::H264PROFILE_BASELINE: | |
246 DVLOG(2) << "Initialize(): profile H264PROFILE_BASELINE"; | |
247 break; | |
248 case media::H264PROFILE_MAIN: | |
249 DVLOG(2) << "Initialize(): profile H264PROFILE_MAIN"; | |
250 break; | |
251 case media::H264PROFILE_HIGH: | |
252 DVLOG(2) << "Initialize(): profile H264PROFILE_HIGH"; | |
253 break; | |
254 case media::VP8PROFILE_MAIN: | |
255 DVLOG(2) << "Initialize(): profile VP8PROFILE_MAIN"; | |
256 break; | |
257 default: | |
258 DLOG(ERROR) << "Initialize(): unsupported profile=" << profile; | |
259 return false; | |
260 }; | |
261 video_profile_ = profile; | |
262 | |
263 if (egl_display_ == EGL_NO_DISPLAY) { | |
264 DLOG(ERROR) << "Initialize(): could not get EGLDisplay"; | |
265 NOTIFY_ERROR(PLATFORM_FAILURE); | |
266 return false; | |
267 } | |
268 | |
269 // We need the context to be initialized to query extensions. | |
270 if (!make_context_current_.Run()) { | |
271 DLOG(ERROR) << "Initialize(): could not make context current"; | |
272 NOTIFY_ERROR(PLATFORM_FAILURE); | |
273 return false; | |
274 } | |
275 | |
276 if (!gfx::g_driver_egl.ext.b_EGL_KHR_fence_sync) { | |
277 DLOG(ERROR) << "Initialize(): context does not have EGL_KHR_fence_sync"; | |
278 NOTIFY_ERROR(PLATFORM_FAILURE); | |
279 return false; | |
280 } | |
281 | |
282 // Open the video devices. | |
283 DVLOG(2) << "Initialize(): opening MFC device: " << kExynosMfcDevice; | |
284 mfc_fd_ = HANDLE_EINTR(open(kExynosMfcDevice, | |
285 O_RDWR | O_NONBLOCK | O_CLOEXEC)); | |
286 if (mfc_fd_ == -1) { | |
287 DPLOG(ERROR) << "Initialize(): could not open MFC device: " | |
288 << kExynosMfcDevice; | |
289 NOTIFY_ERROR(PLATFORM_FAILURE); | |
290 return false; | |
291 } | |
292 | |
293 // Create the interrupt fd. | |
294 DCHECK_EQ(device_poll_interrupt_fd_, -1); | |
295 device_poll_interrupt_fd_ = eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC); | |
296 if (device_poll_interrupt_fd_ == -1) { | |
297 DPLOG(ERROR) << "Initialize(): eventfd() failed"; | |
298 NOTIFY_ERROR(PLATFORM_FAILURE); | |
299 return false; | |
300 } | |
301 | |
302 // Capabilities check. | |
303 struct v4l2_capability caps; | |
304 const __u32 kCapsRequired = | |
305 V4L2_CAP_VIDEO_CAPTURE_MPLANE | | |
306 V4L2_CAP_VIDEO_OUTPUT_MPLANE | | |
307 V4L2_CAP_STREAMING; | |
308 IOCTL_OR_ERROR_RETURN_FALSE(mfc_fd_, VIDIOC_QUERYCAP, &caps); | |
309 if ((caps.capabilities & kCapsRequired) != kCapsRequired) { | |
310 DLOG(ERROR) << "Initialize(): ioctl() failed: VIDIOC_QUERYCAP" | |
311 ", caps check failed: 0x" << std::hex << caps.capabilities; | |
312 NOTIFY_ERROR(PLATFORM_FAILURE); | |
313 return false; | |
314 } | |
315 | |
316 if (!CreateMfcInputBuffers()) | |
317 return false; | |
318 | |
319 // MFC output format has to be setup before streaming starts. | |
320 struct v4l2_format format; | |
321 memset(&format, 0, sizeof(format)); | |
322 format.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE; | |
323 format.fmt.pix_mp.pixelformat = V4L2_PIX_FMT_NV12M; | |
324 IOCTL_OR_ERROR_RETURN_FALSE(mfc_fd_, VIDIOC_S_FMT, &format); | |
325 | |
326 // Subscribe to the resolution change event. | |
327 struct v4l2_event_subscription sub; | |
328 memset(&sub, 0, sizeof(sub)); | |
329 sub.type = V4L2_EVENT_RESOLUTION_CHANGE; | |
330 IOCTL_OR_ERROR_RETURN_FALSE(mfc_fd_, VIDIOC_SUBSCRIBE_EVENT, &sub); | |
331 | |
332 // Initialize format-specific bits. | |
333 if (video_profile_ >= media::H264PROFILE_MIN && | |
334 video_profile_ <= media::H264PROFILE_MAX) { | |
335 decoder_h264_parser_.reset(new content::H264Parser()); | |
336 } | |
337 | |
338 if (!decoder_thread_.Start()) { | |
339 DLOG(ERROR) << "Initialize(): decoder thread failed to start"; | |
340 NOTIFY_ERROR(PLATFORM_FAILURE); | |
341 return false; | |
342 } | |
343 | |
344 SetDecoderState(kInitialized); | |
345 | |
346 child_message_loop_proxy_->PostTask(FROM_HERE, base::Bind( | |
347 &Client::NotifyInitializeDone, client_)); | |
348 return true; | |
349 } | |
350 | |
351 void ExynosVideoDecodeAccelerator::Decode( | |
352 const media::BitstreamBuffer& bitstream_buffer) { | |
353 DVLOG(1) << "Decode(): input_id=" << bitstream_buffer.id() | |
354 << ", size=" << bitstream_buffer.size(); | |
355 DCHECK(io_message_loop_proxy_->BelongsToCurrentThread()); | |
356 | |
357 // DecodeTask() will take care of running a DecodeBufferTask(). | |
358 decoder_thread_.message_loop()->PostTask(FROM_HERE, base::Bind( | |
359 &ExynosVideoDecodeAccelerator::DecodeTask, base::Unretained(this), | |
360 bitstream_buffer)); | |
361 } | |
362 | |
363 void ExynosVideoDecodeAccelerator::AssignPictureBuffers( | |
364 const std::vector<media::PictureBuffer>& buffers) { | |
365 DVLOG(3) << "AssignPictureBuffers(): buffer_count=" << buffers.size(); | |
366 DCHECK(child_message_loop_proxy_->BelongsToCurrentThread()); | |
367 | |
368 if (buffers.size() != mfc_output_buffer_map_.size()) { | |
369 DLOG(ERROR) << "AssignPictureBuffers(): Failed to provide requested picture" | |
370 " buffers. (Got " << buffers.size() | |
371 << ", requested " << mfc_output_buffer_map_.size() << ")"; | |
372 NOTIFY_ERROR(INVALID_ARGUMENT); | |
373 return; | |
374 } | |
375 | |
376 if (!make_context_current_.Run()) { | |
377 DLOG(ERROR) << "AssignPictureBuffers(): could not make context current"; | |
378 NOTIFY_ERROR(PLATFORM_FAILURE); | |
379 return; | |
380 } | |
381 | |
382 scoped_ptr<PictureBufferArrayRef> picture_buffers_ref( | |
383 new PictureBufferArrayRef(egl_display_)); | |
384 gfx::ScopedTextureBinder bind_restore(GL_TEXTURE_EXTERNAL_OES, 0); | |
385 EGLint attrs[] = { | |
386 EGL_WIDTH, 0, EGL_HEIGHT, 0, | |
387 EGL_LINUX_DRM_FOURCC_EXT, 0, EGL_DMA_BUF_PLANE0_FD_EXT, 0, | |
388 EGL_DMA_BUF_PLANE0_OFFSET_EXT, 0, EGL_DMA_BUF_PLANE0_PITCH_EXT, 0, | |
389 EGL_DMA_BUF_PLANE1_FD_EXT, 0, EGL_DMA_BUF_PLANE1_OFFSET_EXT, 0, | |
390 EGL_DMA_BUF_PLANE1_PITCH_EXT, 0, EGL_NONE, }; | |
391 attrs[1] = frame_buffer_size_.width(); | |
392 attrs[3] = frame_buffer_size_.height(); | |
393 attrs[5] = DRM_FORMAT_NV12; | |
394 for (size_t i = 0; i < mfc_output_buffer_map_.size(); ++i) { | |
395 DCHECK(buffers[i].size() == frame_buffer_size_); | |
396 MfcOutputRecord& output_record = mfc_output_buffer_map_[i]; | |
397 attrs[7] = output_record.fds[0]; | |
398 attrs[9] = 0; | |
399 attrs[11] = frame_buffer_size_.width(); | |
400 attrs[13] = output_record.fds[1]; | |
401 attrs[15] = 0; | |
402 attrs[17] = frame_buffer_size_.width(); | |
403 EGLImageKHR egl_image = eglCreateImageKHR( | |
404 egl_display_, EGL_NO_CONTEXT, EGL_LINUX_DMA_BUF_EXT, NULL, attrs); | |
405 if (egl_image == EGL_NO_IMAGE_KHR) { | |
406 DLOG(ERROR) << "AssignPictureBuffers(): could not create EGLImageKHR"; | |
407 NOTIFY_ERROR(PLATFORM_FAILURE); | |
408 return; | |
409 } | |
410 | |
411 glBindTexture(GL_TEXTURE_EXTERNAL_OES, buffers[i].texture_id()); | |
412 glEGLImageTargetTexture2DOES(GL_TEXTURE_EXTERNAL_OES, egl_image); | |
413 picture_buffers_ref->picture_buffers.push_back( | |
414 PictureBufferArrayRef::PictureBufferRef(egl_image, buffers[i].id())); | |
415 } | |
416 decoder_thread_.message_loop()->PostTask( | |
417 FROM_HERE, | |
418 base::Bind(&ExynosVideoDecodeAccelerator::AssignPictureBuffersTask, | |
419 base::Unretained(this), | |
420 base::Passed(&picture_buffers_ref))); | |
421 } | |
422 | |
423 void ExynosVideoDecodeAccelerator::ReusePictureBuffer(int32 picture_buffer_id) { | |
424 DVLOG(3) << "ReusePictureBuffer(): picture_buffer_id=" << picture_buffer_id; | |
425 // Must be run on child thread, as we'll insert a sync in the EGL context. | |
426 DCHECK(child_message_loop_proxy_->BelongsToCurrentThread()); | |
427 | |
428 if (!make_context_current_.Run()) { | |
429 DLOG(ERROR) << "ReusePictureBuffer(): could not make context current"; | |
430 NOTIFY_ERROR(PLATFORM_FAILURE); | |
431 return; | |
432 } | |
433 | |
434 EGLSyncKHR egl_sync = | |
435 eglCreateSyncKHR(egl_display_, EGL_SYNC_FENCE_KHR, NULL); | |
436 if (egl_sync == EGL_NO_SYNC_KHR) { | |
437 DLOG(ERROR) << "ReusePictureBuffer(): eglCreateSyncKHR() failed"; | |
438 NOTIFY_ERROR(PLATFORM_FAILURE); | |
439 return; | |
440 } | |
441 | |
442 scoped_ptr<EGLSyncKHRRef> egl_sync_ref(new EGLSyncKHRRef( | |
443 egl_display_, egl_sync)); | |
444 decoder_thread_.message_loop()->PostTask(FROM_HERE, base::Bind( | |
445 &ExynosVideoDecodeAccelerator::ReusePictureBufferTask, | |
446 base::Unretained(this), picture_buffer_id, base::Passed(&egl_sync_ref))); | |
447 } | |
448 | |
449 void ExynosVideoDecodeAccelerator::Flush() { | |
450 DVLOG(3) << "Flush()"; | |
451 DCHECK(child_message_loop_proxy_->BelongsToCurrentThread()); | |
452 decoder_thread_.message_loop()->PostTask(FROM_HERE, base::Bind( | |
453 &ExynosVideoDecodeAccelerator::FlushTask, base::Unretained(this))); | |
454 } | |
455 | |
456 void ExynosVideoDecodeAccelerator::Reset() { | |
457 DVLOG(3) << "Reset()"; | |
458 DCHECK(child_message_loop_proxy_->BelongsToCurrentThread()); | |
459 decoder_thread_.message_loop()->PostTask(FROM_HERE, base::Bind( | |
460 &ExynosVideoDecodeAccelerator::ResetTask, base::Unretained(this))); | |
461 } | |
462 | |
463 void ExynosVideoDecodeAccelerator::Destroy() { | |
464 DVLOG(3) << "Destroy()"; | |
465 DCHECK(child_message_loop_proxy_->BelongsToCurrentThread()); | |
466 | |
467 // We're destroying; cancel all callbacks. | |
468 client_ptr_factory_.InvalidateWeakPtrs(); | |
469 | |
470 // If the decoder thread is running, destroy using posted task. | |
471 if (decoder_thread_.IsRunning()) { | |
472 decoder_thread_.message_loop()->PostTask(FROM_HERE, base::Bind( | |
473 &ExynosVideoDecodeAccelerator::DestroyTask, base::Unretained(this))); | |
474 // DestroyTask() will cause the decoder_thread_ to flush all tasks. | |
475 decoder_thread_.Stop(); | |
476 } else { | |
477 // Otherwise, call the destroy task directly. | |
478 DestroyTask(); | |
479 } | |
480 | |
481 // Set to kError state just in case. | |
482 SetDecoderState(kError); | |
483 | |
484 delete this; | |
485 } | |
486 | |
487 bool ExynosVideoDecodeAccelerator::CanDecodeOnIOThread() { return true; } | |
488 | |
489 void ExynosVideoDecodeAccelerator::DecodeTask( | |
490 const media::BitstreamBuffer& bitstream_buffer) { | |
491 DVLOG(3) << "DecodeTask(): input_id=" << bitstream_buffer.id(); | |
492 DCHECK_EQ(decoder_thread_.message_loop(), base::MessageLoop::current()); | |
493 DCHECK_NE(decoder_state_, kUninitialized); | |
494 TRACE_EVENT1("Video Decoder", "EVDA::DecodeTask", "input_id", | |
495 bitstream_buffer.id()); | |
496 | |
497 scoped_ptr<BitstreamBufferRef> bitstream_record(new BitstreamBufferRef( | |
498 io_client_, io_message_loop_proxy_, | |
499 new base::SharedMemory(bitstream_buffer.handle(), true), | |
500 bitstream_buffer.size(), bitstream_buffer.id())); | |
501 if (!bitstream_record->shm->Map(bitstream_buffer.size())) { | |
502 DLOG(ERROR) << "Decode(): could not map bitstream_buffer"; | |
503 NOTIFY_ERROR(UNREADABLE_INPUT); | |
504 return; | |
505 } | |
506 DVLOG(3) << "Decode(): mapped to addr=" << bitstream_record->shm->memory(); | |
507 | |
508 if (decoder_state_ == kResetting || decoder_flushing_) { | |
509 // In the case that we're resetting or flushing, we need to delay decoding | |
510 // the BitstreamBuffers that come after the Reset() or Flush() call. When | |
511 // we're here, we know that this DecodeTask() was scheduled by a Decode() | |
512 // call that came after (in the client thread) the Reset() or Flush() call; | |
513 // thus set up the delay if necessary. | |
514 if (decoder_delay_bitstream_buffer_id_ == -1) | |
515 decoder_delay_bitstream_buffer_id_ = bitstream_record->input_id; | |
516 } else if (decoder_state_ == kError) { | |
517 DVLOG(2) << "DecodeTask(): early out: kError state"; | |
518 return; | |
519 } | |
520 | |
521 decoder_input_queue_.push( | |
522 linked_ptr<BitstreamBufferRef>(bitstream_record.release())); | |
523 decoder_decode_buffer_tasks_scheduled_++; | |
524 DecodeBufferTask(); | |
525 } | |
526 | |
527 void ExynosVideoDecodeAccelerator::DecodeBufferTask() { | |
528 DVLOG(3) << "DecodeBufferTask()"; | |
529 DCHECK_EQ(decoder_thread_.message_loop(), base::MessageLoop::current()); | |
530 DCHECK_NE(decoder_state_, kUninitialized); | |
531 TRACE_EVENT0("Video Decoder", "EVDA::DecodeBufferTask"); | |
532 | |
533 decoder_decode_buffer_tasks_scheduled_--; | |
534 | |
535 if (decoder_state_ == kResetting) { | |
536 DVLOG(2) << "DecodeBufferTask(): early out: kResetting state"; | |
537 return; | |
538 } else if (decoder_state_ == kError) { | |
539 DVLOG(2) << "DecodeBufferTask(): early out: kError state"; | |
540 return; | |
541 } else if (decoder_state_ == kChangingResolution) { | |
542 DVLOG(2) << "DecodeBufferTask(): early out: resolution change pending"; | |
543 return; | |
544 } | |
545 | |
546 if (decoder_current_bitstream_buffer_ == NULL) { | |
547 if (decoder_input_queue_.empty()) { | |
548 // We're waiting for a new buffer -- exit without scheduling a new task. | |
549 return; | |
550 } | |
551 linked_ptr<BitstreamBufferRef>& buffer_ref = decoder_input_queue_.front(); | |
552 if (decoder_delay_bitstream_buffer_id_ == buffer_ref->input_id) { | |
553 // We're asked to delay decoding on this and subsequent buffers. | |
554 return; | |
555 } | |
556 | |
557 // Setup to use the next buffer. | |
558 decoder_current_bitstream_buffer_.reset(buffer_ref.release()); | |
559 decoder_input_queue_.pop(); | |
560 DVLOG(3) << "DecodeBufferTask(): reading input_id=" | |
561 << decoder_current_bitstream_buffer_->input_id | |
562 << ", addr=" << (decoder_current_bitstream_buffer_->shm ? | |
563 decoder_current_bitstream_buffer_->shm->memory() : | |
564 NULL) | |
565 << ", size=" << decoder_current_bitstream_buffer_->size; | |
566 } | |
567 bool schedule_task = false; | |
568 const size_t size = decoder_current_bitstream_buffer_->size; | |
569 size_t decoded_size = 0; | |
570 if (size == 0) { | |
571 const int32 input_id = decoder_current_bitstream_buffer_->input_id; | |
572 if (input_id >= 0) { | |
573 // This is a buffer queued from the client that has zero size. Skip. | |
574 schedule_task = true; | |
575 } else { | |
576 // This is a buffer of zero size, queued to flush the pipe. Flush. | |
577 DCHECK_EQ(decoder_current_bitstream_buffer_->shm.get(), | |
578 static_cast<base::SharedMemory*>(NULL)); | |
579 // Enqueue a buffer guaranteed to be empty. To do that, we flush the | |
580 // current input, enqueue no data to the next frame, then flush that down. | |
581 schedule_task = true; | |
582 if (decoder_current_input_buffer_ != -1 && | |
583 mfc_input_buffer_map_[decoder_current_input_buffer_].input_id != | |
584 kFlushBufferId) | |
585 schedule_task = FlushInputFrame(); | |
586 | |
587 if (schedule_task && AppendToInputFrame(NULL, 0) && FlushInputFrame()) { | |
588 DVLOG(2) << "DecodeBufferTask(): enqueued flush buffer"; | |
589 decoder_partial_frame_pending_ = false; | |
590 schedule_task = true; | |
591 } else { | |
592 // If we failed to enqueue the empty buffer (due to pipeline | |
593 // backpressure), don't advance the bitstream buffer queue, and don't | |
594 // schedule the next task. This bitstream buffer queue entry will get | |
595 // reprocessed when the pipeline frees up. | |
596 schedule_task = false; | |
597 } | |
598 } | |
599 } else { | |
600 // This is a buffer queued from the client, with actual contents. Decode. | |
601 const uint8* const data = | |
602 reinterpret_cast<const uint8*>( | |
603 decoder_current_bitstream_buffer_->shm->memory()) + | |
604 decoder_current_bitstream_buffer_->bytes_used; | |
605 const size_t data_size = | |
606 decoder_current_bitstream_buffer_->size - | |
607 decoder_current_bitstream_buffer_->bytes_used; | |
608 if (!AdvanceFrameFragment(data, data_size, &decoded_size)) { | |
609 NOTIFY_ERROR(UNREADABLE_INPUT); | |
610 return; | |
611 } | |
612 // AdvanceFrameFragment should not return a size larger than the buffer | |
613 // size, even on invalid data. | |
614 CHECK_LE(decoded_size, data_size); | |
615 | |
616 switch (decoder_state_) { | |
617 case kInitialized: | |
618 case kAfterReset: | |
619 schedule_task = DecodeBufferInitial(data, decoded_size, &decoded_size); | |
620 break; | |
621 case kDecoding: | |
622 schedule_task = DecodeBufferContinue(data, decoded_size); | |
623 break; | |
624 default: | |
625 NOTIFY_ERROR(ILLEGAL_STATE); | |
626 return; | |
627 } | |
628 } | |
629 if (decoder_state_ == kError) { | |
630 // Failed during decode. | |
631 return; | |
632 } | |
633 | |
634 if (schedule_task) { | |
635 decoder_current_bitstream_buffer_->bytes_used += decoded_size; | |
636 if (decoder_current_bitstream_buffer_->bytes_used == | |
637 decoder_current_bitstream_buffer_->size) { | |
638 // Our current bitstream buffer is done; return it. | |
639 int32 input_id = decoder_current_bitstream_buffer_->input_id; | |
640 DVLOG(3) << "DecodeBufferTask(): finished input_id=" << input_id; | |
641 // BitstreamBufferRef destructor calls NotifyEndOfBitstreamBuffer(). | |
642 decoder_current_bitstream_buffer_.reset(); | |
643 } | |
644 ScheduleDecodeBufferTaskIfNeeded(); | |
645 } | |
646 } | |
647 | |
648 bool ExynosVideoDecodeAccelerator::AdvanceFrameFragment( | |
649 const uint8* data, | |
650 size_t size, | |
651 size_t* endpos) { | |
652 if (video_profile_ >= media::H264PROFILE_MIN && | |
653 video_profile_ <= media::H264PROFILE_MAX) { | |
654 // For H264, we need to feed HW one frame at a time. This is going to take | |
655 // some parsing of our input stream. | |
656 decoder_h264_parser_->SetStream(data, size); | |
657 content::H264NALU nalu; | |
658 content::H264Parser::Result result; | |
659 *endpos = 0; | |
660 | |
661 // Keep on peeking the next NALs while they don't indicate a frame | |
662 // boundary. | |
663 for (;;) { | |
664 bool end_of_frame = false; | |
665 result = decoder_h264_parser_->AdvanceToNextNALU(&nalu); | |
666 if (result == content::H264Parser::kInvalidStream || | |
667 result == content::H264Parser::kUnsupportedStream) | |
668 return false; | |
669 if (result == content::H264Parser::kEOStream) { | |
670 // We've reached the end of the buffer before finding a frame boundary. | |
671 decoder_partial_frame_pending_ = true; | |
672 return true; | |
673 } | |
674 switch (nalu.nal_unit_type) { | |
675 case content::H264NALU::kNonIDRSlice: | |
676 case content::H264NALU::kIDRSlice: | |
677 if (nalu.size < 1) | |
678 return false; | |
679 // For these two, if the "first_mb_in_slice" field is zero, start a | |
680 // new frame and return. This field is Exp-Golomb coded starting on | |
681 // the eighth data bit of the NAL; a zero value is encoded with a | |
682 // leading '1' bit in the byte, which we can detect as the byte being | |
683 // (unsigned) greater than or equal to 0x80. | |
684 if (nalu.data[1] >= 0x80) { | |
685 end_of_frame = true; | |
686 break; | |
687 } | |
688 break; | |
689 case content::H264NALU::kSPS: | |
690 case content::H264NALU::kPPS: | |
691 case content::H264NALU::kEOSeq: | |
692 case content::H264NALU::kEOStream: | |
693 // These unconditionally signal a frame boundary. | |
694 end_of_frame = true; | |
695 break; | |
696 default: | |
697 // For all others, keep going. | |
698 break; | |
699 } | |
700 if (end_of_frame) { | |
701 if (!decoder_partial_frame_pending_ && *endpos == 0) { | |
702 // The frame was previously restarted, and we haven't filled the | |
703 // current frame with any contents yet. Start the new frame here and | |
704 // continue parsing NALs. | |
705 } else { | |
706 // The frame wasn't previously restarted and/or we have contents for | |
707 // the current frame; signal the start of a new frame here: we don't | |
708 // have a partial frame anymore. | |
709 decoder_partial_frame_pending_ = false; | |
710 return true; | |
711 } | |
712 } | |
713 *endpos = (nalu.data + nalu.size) - data; | |
714 } | |
715 NOTREACHED(); | |
716 return false; | |
717 } else { | |
718 DCHECK_GE(video_profile_, media::VP8PROFILE_MIN); | |
719 DCHECK_LE(video_profile_, media::VP8PROFILE_MAX); | |
720 // For VP8, we can just dump the entire buffer. No fragmentation needed, | |
721 // and we never return a partial frame. | |
722 *endpos = size; | |
723 decoder_partial_frame_pending_ = false; | |
724 return true; | |
725 } | |
726 } | |
727 | |
728 void ExynosVideoDecodeAccelerator::ScheduleDecodeBufferTaskIfNeeded() { | |
729 DCHECK_EQ(decoder_thread_.message_loop(), base::MessageLoop::current()); | |
730 | |
731 // If we're behind on tasks, schedule another one. | |
732 int buffers_to_decode = decoder_input_queue_.size(); | |
733 if (decoder_current_bitstream_buffer_ != NULL) | |
734 buffers_to_decode++; | |
735 if (decoder_decode_buffer_tasks_scheduled_ < buffers_to_decode) { | |
736 decoder_decode_buffer_tasks_scheduled_++; | |
737 decoder_thread_.message_loop()->PostTask(FROM_HERE, base::Bind( | |
738 &ExynosVideoDecodeAccelerator::DecodeBufferTask, | |
739 base::Unretained(this))); | |
740 } | |
741 } | |
742 | |
743 bool ExynosVideoDecodeAccelerator::DecodeBufferInitial( | |
744 const void* data, size_t size, size_t* endpos) { | |
745 DVLOG(3) << "DecodeBufferInitial(): data=" << data << ", size=" << size; | |
746 DCHECK_EQ(decoder_thread_.message_loop(), base::MessageLoop::current()); | |
747 DCHECK_NE(decoder_state_, kUninitialized); | |
748 DCHECK_NE(decoder_state_, kDecoding); | |
749 DCHECK(!device_poll_thread_.IsRunning()); | |
750 // Initial decode. We haven't been able to get output stream format info yet. | |
751 // Get it, and start decoding. | |
752 | |
753 // Copy in and send to HW. | |
754 if (!AppendToInputFrame(data, size)) | |
755 return false; | |
756 | |
757 // If we only have a partial frame, don't flush and process yet. | |
758 if (decoder_partial_frame_pending_) | |
759 return true; | |
760 | |
761 if (!FlushInputFrame()) | |
762 return false; | |
763 | |
764 // Recycle buffers. | |
765 DequeueMfc(); | |
766 | |
767 // Check and see if we have format info yet. | |
768 struct v4l2_format format; | |
769 bool again = false; | |
770 if (!GetFormatInfo(&format, &again)) | |
771 return false; | |
772 | |
773 if (again) { | |
774 // Need more stream to decode format, return true and schedule next buffer. | |
775 *endpos = size; | |
776 return true; | |
777 } | |
778 | |
779 // Run this initialization only on first startup. | |
780 if (decoder_state_ == kInitialized) { | |
781 DVLOG(3) << "DecodeBufferInitial(): running initialization"; | |
782 // Success! Setup our parameters. | |
783 if (!CreateBuffersForFormat(format)) | |
784 return false; | |
785 | |
786 // MFC expects to process the initial buffer once during stream init to | |
787 // configure stream parameters, but will not consume the steam data on that | |
788 // iteration. Subsequent iterations (including after reset) do not require | |
789 // the stream init step. | |
790 *endpos = 0; | |
791 } else { | |
792 *endpos = size; | |
793 } | |
794 | |
795 // StartDevicePoll will raise the error if there is one. | |
796 if (!StartDevicePoll()) | |
797 return false; | |
798 | |
799 decoder_state_ = kDecoding; | |
800 ScheduleDecodeBufferTaskIfNeeded(); | |
801 return true; | |
802 } | |
803 | |
804 bool ExynosVideoDecodeAccelerator::DecodeBufferContinue( | |
805 const void* data, size_t size) { | |
806 DVLOG(3) << "DecodeBufferContinue(): data=" << data << ", size=" << size; | |
807 DCHECK_EQ(decoder_thread_.message_loop(), base::MessageLoop::current()); | |
808 DCHECK_EQ(decoder_state_, kDecoding); | |
809 | |
810 // Both of these calls will set kError state if they fail. | |
811 // Only flush the frame if it's complete. | |
812 return (AppendToInputFrame(data, size) && | |
813 (decoder_partial_frame_pending_ || FlushInputFrame())); | |
814 } | |
815 | |
816 bool ExynosVideoDecodeAccelerator::AppendToInputFrame( | |
817 const void* data, size_t size) { | |
818 DVLOG(3) << "AppendToInputFrame()"; | |
819 DCHECK_EQ(decoder_thread_.message_loop(), base::MessageLoop::current()); | |
820 DCHECK_NE(decoder_state_, kUninitialized); | |
821 DCHECK_NE(decoder_state_, kResetting); | |
822 DCHECK_NE(decoder_state_, kError); | |
823 // This routine can handle data == NULL and size == 0, which occurs when | |
824 // we queue an empty buffer for the purposes of flushing the pipe. | |
825 | |
826 // Flush if we're too big | |
827 if (decoder_current_input_buffer_ != -1) { | |
828 MfcInputRecord& input_record = | |
829 mfc_input_buffer_map_[decoder_current_input_buffer_]; | |
830 if (input_record.bytes_used + size > input_record.length) { | |
831 if (!FlushInputFrame()) | |
832 return false; | |
833 decoder_current_input_buffer_ = -1; | |
834 } | |
835 } | |
836 | |
837 // Try to get an available input buffer | |
838 if (decoder_current_input_buffer_ == -1) { | |
839 if (mfc_free_input_buffers_.empty()) { | |
840 // See if we can get more free buffers from HW | |
841 DequeueMfc(); | |
842 if (mfc_free_input_buffers_.empty()) { | |
843 // Nope! | |
844 DVLOG(2) << "AppendToInputFrame(): stalled for input buffers"; | |
845 return false; | |
846 } | |
847 } | |
848 decoder_current_input_buffer_ = mfc_free_input_buffers_.back(); | |
849 mfc_free_input_buffers_.pop_back(); | |
850 MfcInputRecord& input_record = | |
851 mfc_input_buffer_map_[decoder_current_input_buffer_]; | |
852 DCHECK_EQ(input_record.bytes_used, 0); | |
853 DCHECK_EQ(input_record.input_id, -1); | |
854 DCHECK(decoder_current_bitstream_buffer_ != NULL); | |
855 input_record.input_id = decoder_current_bitstream_buffer_->input_id; | |
856 } | |
857 | |
858 DCHECK(data != NULL || size == 0); | |
859 if (size == 0) { | |
860 // If we asked for an empty buffer, return now. We return only after | |
861 // getting the next input buffer, since we might actually want an empty | |
862 // input buffer for flushing purposes. | |
863 return true; | |
864 } | |
865 | |
866 // Copy in to the buffer. | |
867 MfcInputRecord& input_record = | |
868 mfc_input_buffer_map_[decoder_current_input_buffer_]; | |
869 if (size > input_record.length - input_record.bytes_used) { | |
870 LOG(ERROR) << "AppendToInputFrame(): over-size frame, erroring"; | |
871 NOTIFY_ERROR(UNREADABLE_INPUT); | |
872 return false; | |
873 } | |
874 memcpy( | |
875 reinterpret_cast<uint8*>(input_record.address) + input_record.bytes_used, | |
876 data, | |
877 size); | |
878 input_record.bytes_used += size; | |
879 | |
880 return true; | |
881 } | |
882 | |
883 bool ExynosVideoDecodeAccelerator::FlushInputFrame() { | |
884 DVLOG(3) << "FlushInputFrame()"; | |
885 DCHECK_EQ(decoder_thread_.message_loop(), base::MessageLoop::current()); | |
886 DCHECK_NE(decoder_state_, kUninitialized); | |
887 DCHECK_NE(decoder_state_, kResetting); | |
888 DCHECK_NE(decoder_state_, kError); | |
889 | |
890 if (decoder_current_input_buffer_ == -1) | |
891 return true; | |
892 | |
893 MfcInputRecord& input_record = | |
894 mfc_input_buffer_map_[decoder_current_input_buffer_]; | |
895 DCHECK_NE(input_record.input_id, -1); | |
896 DCHECK(input_record.input_id != kFlushBufferId || | |
897 input_record.bytes_used == 0); | |
898 // * if input_id >= 0, this input buffer was prompted by a bitstream buffer we | |
899 // got from the client. We can skip it if it is empty. | |
900 // * if input_id < 0 (should be kFlushBufferId in this case), this input | |
901 // buffer was prompted by a flush buffer, and should be queued even when | |
902 // empty. | |
903 if (input_record.input_id >= 0 && input_record.bytes_used == 0) { | |
904 input_record.input_id = -1; | |
905 mfc_free_input_buffers_.push_back(decoder_current_input_buffer_); | |
906 decoder_current_input_buffer_ = -1; | |
907 return true; | |
908 } | |
909 | |
910 // Queue it to MFC. | |
911 mfc_input_ready_queue_.push(decoder_current_input_buffer_); | |
912 decoder_current_input_buffer_ = -1; | |
913 DVLOG(3) << "FlushInputFrame(): submitting input_id=" | |
914 << input_record.input_id; | |
915 // Kick the MFC once since there's new available input for it. | |
916 EnqueueMfc(); | |
917 | |
918 return (decoder_state_ != kError); | |
919 } | |
920 | |
921 void ExynosVideoDecodeAccelerator::AssignPictureBuffersTask( | |
922 scoped_ptr<PictureBufferArrayRef> pic_buffers) { | |
923 DVLOG(3) << "AssignPictureBuffersTask()"; | |
924 DCHECK_EQ(decoder_thread_.message_loop(), base::MessageLoop::current()); | |
925 DCHECK_NE(decoder_state_, kUninitialized); | |
926 TRACE_EVENT0("Video Decoder", "EVDA::AssignPictureBuffersTask"); | |
927 | |
928 // We run AssignPictureBuffersTask even if we're in kResetting. | |
929 if (decoder_state_ == kError) { | |
930 DVLOG(2) << "AssignPictureBuffersTask(): early out: kError state"; | |
931 return; | |
932 } | |
933 | |
934 DCHECK_EQ(pic_buffers->picture_buffers.size(), mfc_output_buffer_map_.size()); | |
935 for (size_t i = 0; i < mfc_output_buffer_map_.size(); ++i) { | |
936 MfcOutputRecord& output_record = mfc_output_buffer_map_[i]; | |
937 PictureBufferArrayRef::PictureBufferRef& buffer_ref = | |
938 pic_buffers->picture_buffers[i]; | |
939 // We should be blank right now. | |
940 DCHECK(!output_record.at_device); | |
941 DCHECK(!output_record.at_client); | |
942 DCHECK_EQ(output_record.egl_image, EGL_NO_IMAGE_KHR); | |
943 DCHECK_EQ(output_record.egl_sync, EGL_NO_SYNC_KHR); | |
944 DCHECK_EQ(output_record.picture_id, -1); | |
945 DCHECK_EQ(output_record.cleared, false); | |
946 output_record.egl_image = buffer_ref.egl_image; | |
947 output_record.picture_id = buffer_ref.picture_id; | |
948 mfc_free_output_buffers_.push(i); | |
949 DVLOG(3) << "AssignPictureBuffersTask(): buffer[" << i | |
950 << "]: picture_id=" << buffer_ref.picture_id; | |
951 } | |
952 pic_buffers->picture_buffers.clear(); | |
953 | |
954 // We got buffers! Kick the MFC. | |
955 EnqueueMfc(); | |
956 | |
957 if (decoder_state_ == kChangingResolution) | |
958 ResumeAfterResolutionChange(); | |
959 } | |
960 | |
961 void ExynosVideoDecodeAccelerator::ServiceDeviceTask(bool mfc_event_pending) { | |
962 DVLOG(3) << "ServiceDeviceTask()"; | |
963 DCHECK_EQ(decoder_thread_.message_loop(), base::MessageLoop::current()); | |
964 DCHECK_NE(decoder_state_, kUninitialized); | |
965 DCHECK_NE(decoder_state_, kInitialized); | |
966 DCHECK_NE(decoder_state_, kAfterReset); | |
967 TRACE_EVENT0("Video Decoder", "EVDA::ServiceDeviceTask"); | |
968 | |
969 if (decoder_state_ == kResetting) { | |
970 DVLOG(2) << "ServiceDeviceTask(): early out: kResetting state"; | |
971 return; | |
972 } else if (decoder_state_ == kError) { | |
973 DVLOG(2) << "ServiceDeviceTask(): early out: kError state"; | |
974 return; | |
975 } else if (decoder_state_ == kChangingResolution) { | |
976 DVLOG(2) << "ServiceDeviceTask(): early out: kChangingResolution state"; | |
977 return; | |
978 } | |
979 | |
980 if (mfc_event_pending) | |
981 DequeueMfcEvents(); | |
982 DequeueMfc(); | |
983 EnqueueMfc(); | |
984 | |
985 // Clear the interrupt fd. | |
986 if (!ClearDevicePollInterrupt()) | |
987 return; | |
988 | |
989 unsigned int poll_fds = 0; | |
990 // Add MFC fd, if we should poll on it. | |
991 // MFC can be polled as soon as either input or output buffers are queued. | |
992 if (mfc_input_buffer_queued_count_ + mfc_output_buffer_queued_count_ > 0) | |
993 poll_fds |= kPollMfc; | |
994 | |
995 // ServiceDeviceTask() should only ever be scheduled from DevicePollTask(), | |
996 // so either: | |
997 // * device_poll_thread_ is running normally | |
998 // * device_poll_thread_ scheduled us, but then a ResetTask() or DestroyTask() | |
999 // shut it down, in which case we're either in kResetting or kError states | |
1000 // respectively, and we should have early-outed already. | |
1001 DCHECK(device_poll_thread_.message_loop()); | |
1002 // Queue the DevicePollTask() now. | |
1003 device_poll_thread_.message_loop()->PostTask(FROM_HERE, base::Bind( | |
1004 &ExynosVideoDecodeAccelerator::DevicePollTask, | |
1005 base::Unretained(this), | |
1006 poll_fds)); | |
1007 | |
1008 DVLOG(1) << "ServiceDeviceTask(): buffer counts: DEC[" | |
1009 << decoder_input_queue_.size() << "->" | |
1010 << mfc_input_ready_queue_.size() << "] => MFC[" | |
1011 << mfc_free_input_buffers_.size() << "+" | |
1012 << mfc_input_buffer_queued_count_ << "/" | |
1013 << mfc_input_buffer_map_.size() << "->" | |
1014 << mfc_free_output_buffers_.size() << "+" | |
1015 << mfc_output_buffer_queued_count_ << "/" | |
1016 << mfc_output_buffer_map_.size() << "] => VDA[" | |
1017 << decoder_frames_at_client_ << "]"; | |
1018 | |
1019 ScheduleDecodeBufferTaskIfNeeded(); | |
1020 StartResolutionChangeIfNeeded(); | |
1021 } | |
1022 | |
1023 void ExynosVideoDecodeAccelerator::EnqueueMfc() { | |
1024 DVLOG(3) << "EnqueueMfc()"; | |
1025 DCHECK_EQ(decoder_thread_.message_loop(), base::MessageLoop::current()); | |
1026 DCHECK_NE(decoder_state_, kUninitialized); | |
1027 TRACE_EVENT0("Video Decoder", "EVDA::EnqueueMfc"); | |
1028 | |
1029 // Drain the pipe of completed decode buffers. | |
1030 const int old_mfc_inputs_queued = mfc_input_buffer_queued_count_; | |
1031 while (!mfc_input_ready_queue_.empty()) { | |
1032 if (!EnqueueMfcInputRecord()) | |
1033 return; | |
1034 } | |
1035 if (old_mfc_inputs_queued == 0 && mfc_input_buffer_queued_count_ != 0) { | |
1036 // We just started up a previously empty queue. | |
1037 // Queue state changed; signal interrupt. | |
1038 if (!SetDevicePollInterrupt()) | |
1039 return; | |
1040 // Start VIDIOC_STREAMON if we haven't yet. | |
1041 if (!mfc_input_streamon_) { | |
1042 __u32 type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE; | |
1043 IOCTL_OR_ERROR_RETURN(mfc_fd_, VIDIOC_STREAMON, &type); | |
1044 mfc_input_streamon_ = true; | |
1045 } | |
1046 } | |
1047 | |
1048 // Enqueue all the MFC outputs we can. | |
1049 const int old_mfc_outputs_queued = mfc_output_buffer_queued_count_; | |
1050 while (!mfc_free_output_buffers_.empty()) { | |
1051 if (!EnqueueMfcOutputRecord()) | |
1052 return; | |
1053 } | |
1054 if (old_mfc_outputs_queued == 0 && mfc_output_buffer_queued_count_ != 0) { | |
1055 // We just started up a previously empty queue. | |
1056 // Queue state changed; signal interrupt. | |
1057 if (!SetDevicePollInterrupt()) | |
1058 return; | |
1059 // Start VIDIOC_STREAMON if we haven't yet. | |
1060 if (!mfc_output_streamon_) { | |
1061 __u32 type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE; | |
1062 IOCTL_OR_ERROR_RETURN(mfc_fd_, VIDIOC_STREAMON, &type); | |
1063 mfc_output_streamon_ = true; | |
1064 } | |
1065 } | |
1066 } | |
1067 | |
1068 void ExynosVideoDecodeAccelerator::DequeueMfcEvents() { | |
1069 DCHECK_EQ(decoder_thread_.message_loop(), base::MessageLoop::current()); | |
1070 DCHECK_NE(decoder_state_, kUninitialized); | |
1071 DVLOG(3) << "DequeueMfcEvents()"; | |
1072 | |
1073 struct v4l2_event ev; | |
1074 memset(&ev, 0, sizeof(ev)); | |
1075 | |
1076 while (ioctl(mfc_fd_, VIDIOC_DQEVENT, &ev) == 0) { | |
1077 if (ev.type == V4L2_EVENT_RESOLUTION_CHANGE) { | |
1078 DVLOG(3) << "DequeueMfcEvents(): got resolution change event."; | |
1079 DCHECK(!resolution_change_pending_); | |
1080 resolution_change_pending_ = true; | |
1081 } else { | |
1082 DLOG(FATAL) << "DequeueMfcEvents(): got an event (" << ev.type | |
1083 << ") we haven't subscribed to."; | |
1084 } | |
1085 } | |
1086 } | |
1087 | |
1088 void ExynosVideoDecodeAccelerator::DequeueMfc() { | |
1089 DVLOG(3) << "DequeueMfc()"; | |
1090 DCHECK_EQ(decoder_thread_.message_loop(), base::MessageLoop::current()); | |
1091 DCHECK_NE(decoder_state_, kUninitialized); | |
1092 TRACE_EVENT0("Video Decoder", "EVDA::DequeueMfc"); | |
1093 | |
1094 // Dequeue completed MFC input (VIDEO_OUTPUT) buffers, and recycle to the free | |
1095 // list. | |
1096 struct v4l2_buffer dqbuf; | |
1097 struct v4l2_plane planes[2]; | |
1098 while (mfc_input_buffer_queued_count_ > 0) { | |
1099 DCHECK(mfc_input_streamon_); | |
1100 memset(&dqbuf, 0, sizeof(dqbuf)); | |
1101 memset(planes, 0, sizeof(planes)); | |
1102 dqbuf.type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE; | |
1103 dqbuf.memory = V4L2_MEMORY_MMAP; | |
1104 dqbuf.m.planes = planes; | |
1105 dqbuf.length = 1; | |
1106 if (ioctl(mfc_fd_, VIDIOC_DQBUF, &dqbuf) != 0) { | |
1107 if (errno == EAGAIN) { | |
1108 // EAGAIN if we're just out of buffers to dequeue. | |
1109 break; | |
1110 } | |
1111 DPLOG(ERROR) << "DequeueMfc(): ioctl() failed: VIDIOC_DQBUF"; | |
1112 NOTIFY_ERROR(PLATFORM_FAILURE); | |
1113 return; | |
1114 } | |
1115 MfcInputRecord& input_record = mfc_input_buffer_map_[dqbuf.index]; | |
1116 DCHECK(input_record.at_device); | |
1117 mfc_free_input_buffers_.push_back(dqbuf.index); | |
1118 input_record.at_device = false; | |
1119 input_record.bytes_used = 0; | |
1120 input_record.input_id = -1; | |
1121 mfc_input_buffer_queued_count_--; | |
1122 } | |
1123 | |
1124 // Dequeue completed MFC output (VIDEO_CAPTURE) buffers, and queue to the | |
1125 // completed queue. | |
1126 while (mfc_output_buffer_queued_count_ > 0) { | |
1127 DCHECK(mfc_output_streamon_); | |
1128 memset(&dqbuf, 0, sizeof(dqbuf)); | |
1129 memset(planes, 0, sizeof(planes)); | |
1130 dqbuf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE; | |
1131 dqbuf.memory = V4L2_MEMORY_MMAP; | |
1132 dqbuf.m.planes = planes; | |
1133 dqbuf.length = 2; | |
1134 if (ioctl(mfc_fd_, VIDIOC_DQBUF, &dqbuf) != 0) { | |
1135 if (errno == EAGAIN) { | |
1136 // EAGAIN if we're just out of buffers to dequeue. | |
1137 break; | |
1138 } | |
1139 DPLOG(ERROR) << "DequeueMfc(): ioctl() failed: VIDIOC_DQBUF"; | |
1140 NOTIFY_ERROR(PLATFORM_FAILURE); | |
1141 return; | |
1142 } | |
1143 MfcOutputRecord& output_record = mfc_output_buffer_map_[dqbuf.index]; | |
1144 DCHECK(output_record.at_device); | |
1145 DCHECK(!output_record.at_client); | |
1146 DCHECK_NE(output_record.egl_image, EGL_NO_IMAGE_KHR); | |
1147 DCHECK_NE(output_record.picture_id, -1); | |
1148 output_record.at_device = false; | |
1149 if (dqbuf.m.planes[0].bytesused + dqbuf.m.planes[1].bytesused == 0) { | |
1150 // This is an empty output buffer returned as part of a flush. | |
1151 mfc_free_output_buffers_.push(dqbuf.index); | |
1152 } else { | |
1153 DCHECK_GE(dqbuf.timestamp.tv_sec, 0); | |
1154 output_record.at_client = true; | |
1155 DVLOG(3) << "DequeueMfc(): returning input_id=" << dqbuf.timestamp.tv_sec | |
1156 << " as picture_id=" << output_record.picture_id; | |
1157 const media::Picture& picture = | |
1158 media::Picture(output_record.picture_id, dqbuf.timestamp.tv_sec); | |
1159 pending_picture_ready_.push( | |
1160 PictureRecord(output_record.cleared, picture)); | |
1161 SendPictureReady(); | |
1162 output_record.cleared = true; | |
1163 decoder_frames_at_client_++; | |
1164 } | |
1165 mfc_output_buffer_queued_count_--; | |
1166 } | |
1167 | |
1168 NotifyFlushDoneIfNeeded(); | |
1169 } | |
1170 | |
1171 bool ExynosVideoDecodeAccelerator::EnqueueMfcInputRecord() { | |
1172 DVLOG(3) << "EnqueueMfcInputRecord()"; | |
1173 DCHECK(!mfc_input_ready_queue_.empty()); | |
1174 | |
1175 // Enqueue a MFC input (VIDEO_OUTPUT) buffer. | |
1176 const int buffer = mfc_input_ready_queue_.front(); | |
1177 MfcInputRecord& input_record = mfc_input_buffer_map_[buffer]; | |
1178 DCHECK(!input_record.at_device); | |
1179 struct v4l2_buffer qbuf; | |
1180 struct v4l2_plane qbuf_plane; | |
1181 memset(&qbuf, 0, sizeof(qbuf)); | |
1182 memset(&qbuf_plane, 0, sizeof(qbuf_plane)); | |
1183 qbuf.index = buffer; | |
1184 qbuf.type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE; | |
1185 qbuf.timestamp.tv_sec = input_record.input_id; | |
1186 qbuf.memory = V4L2_MEMORY_MMAP; | |
1187 qbuf.m.planes = &qbuf_plane; | |
1188 qbuf.m.planes[0].bytesused = input_record.bytes_used; | |
1189 qbuf.length = 1; | |
1190 IOCTL_OR_ERROR_RETURN_FALSE(mfc_fd_, VIDIOC_QBUF, &qbuf); | |
1191 mfc_input_ready_queue_.pop(); | |
1192 input_record.at_device = true; | |
1193 mfc_input_buffer_queued_count_++; | |
1194 DVLOG(3) << "EnqueueMfcInputRecord(): enqueued input_id=" | |
1195 << input_record.input_id; | |
1196 return true; | |
1197 } | |
1198 | |
1199 bool ExynosVideoDecodeAccelerator::EnqueueMfcOutputRecord() { | |
1200 DVLOG(3) << "EnqueueMfcOutputRecord()"; | |
1201 DCHECK(!mfc_free_output_buffers_.empty()); | |
1202 | |
1203 // Enqueue a MFC output (VIDEO_CAPTURE) buffer. | |
1204 const int buffer = mfc_free_output_buffers_.front(); | |
1205 MfcOutputRecord& output_record = mfc_output_buffer_map_[buffer]; | |
1206 DCHECK(!output_record.at_device); | |
1207 DCHECK(!output_record.at_client); | |
1208 DCHECK_NE(output_record.egl_image, EGL_NO_IMAGE_KHR); | |
1209 DCHECK_NE(output_record.picture_id, -1); | |
1210 if (output_record.egl_sync != EGL_NO_SYNC_KHR) { | |
1211 TRACE_EVENT0("Video Decoder", | |
1212 "EVDA::EnqueueMfcOutputRecord: eglClientWaitSyncKHR"); | |
1213 // If we have to wait for completion, wait. Note that | |
1214 // mfc_free_output_buffers_ is a FIFO queue, so we always wait on the | |
1215 // buffer that has been in the queue the longest. | |
1216 eglClientWaitSyncKHR(egl_display_, output_record.egl_sync, 0, | |
1217 EGL_FOREVER_KHR); | |
1218 eglDestroySyncKHR(egl_display_, output_record.egl_sync); | |
1219 output_record.egl_sync = EGL_NO_SYNC_KHR; | |
1220 } | |
1221 struct v4l2_buffer qbuf; | |
1222 struct v4l2_plane qbuf_planes[arraysize(output_record.fds)]; | |
1223 memset(&qbuf, 0, sizeof(qbuf)); | |
1224 memset(qbuf_planes, 0, sizeof(qbuf_planes)); | |
1225 qbuf.index = buffer; | |
1226 qbuf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE; | |
1227 qbuf.memory = V4L2_MEMORY_MMAP; | |
1228 qbuf.m.planes = qbuf_planes; | |
1229 qbuf.length = arraysize(output_record.fds); | |
1230 IOCTL_OR_ERROR_RETURN_FALSE(mfc_fd_, VIDIOC_QBUF, &qbuf); | |
1231 mfc_free_output_buffers_.pop(); | |
1232 output_record.at_device = true; | |
1233 mfc_output_buffer_queued_count_++; | |
1234 return true; | |
1235 } | |
1236 | |
1237 void ExynosVideoDecodeAccelerator::ReusePictureBufferTask( | |
1238 int32 picture_buffer_id, scoped_ptr<EGLSyncKHRRef> egl_sync_ref) { | |
1239 DVLOG(3) << "ReusePictureBufferTask(): picture_buffer_id=" | |
1240 << picture_buffer_id; | |
1241 DCHECK_EQ(decoder_thread_.message_loop(), base::MessageLoop::current()); | |
1242 TRACE_EVENT0("Video Decoder", "EVDA::ReusePictureBufferTask"); | |
1243 | |
1244 // We run ReusePictureBufferTask even if we're in kResetting. | |
1245 if (decoder_state_ == kError) { | |
1246 DVLOG(2) << "ReusePictureBufferTask(): early out: kError state"; | |
1247 return; | |
1248 } | |
1249 | |
1250 if (decoder_state_ == kChangingResolution) { | |
1251 DVLOG(2) << "ReusePictureBufferTask(): early out: kChangingResolution"; | |
1252 return; | |
1253 } | |
1254 | |
1255 size_t index; | |
1256 for (index = 0; index < mfc_output_buffer_map_.size(); ++index) | |
1257 if (mfc_output_buffer_map_[index].picture_id == picture_buffer_id) | |
1258 break; | |
1259 | |
1260 if (index >= mfc_output_buffer_map_.size()) { | |
1261 DLOG(ERROR) << "ReusePictureBufferTask(): picture_buffer_id not found"; | |
1262 NOTIFY_ERROR(INVALID_ARGUMENT); | |
1263 return; | |
1264 } | |
1265 | |
1266 MfcOutputRecord& output_record = mfc_output_buffer_map_[index]; | |
1267 if (output_record.at_device || !output_record.at_client) { | |
1268 DLOG(ERROR) << "ReusePictureBufferTask(): picture_buffer_id not reusable"; | |
1269 NOTIFY_ERROR(INVALID_ARGUMENT); | |
1270 return; | |
1271 } | |
1272 | |
1273 DCHECK_EQ(output_record.egl_sync, EGL_NO_SYNC_KHR); | |
1274 output_record.at_client = false; | |
1275 output_record.egl_sync = egl_sync_ref->egl_sync; | |
1276 mfc_free_output_buffers_.push(index); | |
1277 decoder_frames_at_client_--; | |
1278 // Take ownership of the EGLSync. | |
1279 egl_sync_ref->egl_sync = EGL_NO_SYNC_KHR; | |
1280 // We got a buffer back, so kick the MFC. | |
1281 EnqueueMfc(); | |
1282 } | |
1283 | |
1284 void ExynosVideoDecodeAccelerator::FlushTask() { | |
1285 DVLOG(3) << "FlushTask()"; | |
1286 DCHECK_EQ(decoder_thread_.message_loop(), base::MessageLoop::current()); | |
1287 TRACE_EVENT0("Video Decoder", "EVDA::FlushTask"); | |
1288 | |
1289 // Flush outstanding buffers. | |
1290 if (decoder_state_ == kInitialized || decoder_state_ == kAfterReset) { | |
1291 // There's nothing in the pipe, so return done immediately. | |
1292 DVLOG(3) << "FlushTask(): returning flush"; | |
1293 child_message_loop_proxy_->PostTask( | |
1294 FROM_HERE, base::Bind(&Client::NotifyFlushDone, client_)); | |
1295 return; | |
1296 } else if (decoder_state_ == kError) { | |
1297 DVLOG(2) << "FlushTask(): early out: kError state"; | |
1298 return; | |
1299 } | |
1300 | |
1301 // We don't support stacked flushing. | |
1302 DCHECK(!decoder_flushing_); | |
1303 | |
1304 // Queue up an empty buffer -- this triggers the flush. | |
1305 decoder_input_queue_.push( | |
1306 linked_ptr<BitstreamBufferRef>(new BitstreamBufferRef( | |
1307 io_client_, io_message_loop_proxy_, NULL, 0, kFlushBufferId))); | |
1308 decoder_flushing_ = true; | |
1309 SendPictureReady(); // Send all pending PictureReady. | |
1310 | |
1311 ScheduleDecodeBufferTaskIfNeeded(); | |
1312 } | |
1313 | |
1314 void ExynosVideoDecodeAccelerator::NotifyFlushDoneIfNeeded() { | |
1315 if (!decoder_flushing_) | |
1316 return; | |
1317 | |
1318 // Pipeline is empty when: | |
1319 // * Decoder input queue is empty of non-delayed buffers. | |
1320 // * There is no currently filling input buffer. | |
1321 // * MFC input holding queue is empty. | |
1322 // * All MFC input (VIDEO_OUTPUT) buffers are returned. | |
1323 if (!decoder_input_queue_.empty()) { | |
1324 if (decoder_input_queue_.front()->input_id != | |
1325 decoder_delay_bitstream_buffer_id_) | |
1326 return; | |
1327 } | |
1328 if (decoder_current_input_buffer_ != -1) | |
1329 return; | |
1330 if ((mfc_input_ready_queue_.size() + mfc_input_buffer_queued_count_) != 0) | |
1331 return; | |
1332 | |
1333 // TODO(posciak): crbug.com/270039. MFC requires a streamoff-streamon | |
1334 // sequence after flush to continue, even if we are not resetting. This would | |
1335 // make sense, because we don't really want to resume from a non-resume point | |
1336 // (e.g. not from an IDR) if we are flushed. | |
1337 // MSE player however triggers a Flush() on chunk end, but never Reset(). One | |
1338 // could argue either way, or even say that Flush() is not needed/harmful when | |
1339 // transitioning to next chunk. | |
1340 // For now, do the streamoff-streamon cycle to satisfy MFC and not freeze when | |
1341 // doing MSE. This should be harmless otherwise. | |
1342 if (!StopDevicePoll(false)) | |
1343 return; | |
1344 | |
1345 if (!StartDevicePoll()) | |
1346 return; | |
1347 | |
1348 decoder_delay_bitstream_buffer_id_ = -1; | |
1349 decoder_flushing_ = false; | |
1350 DVLOG(3) << "NotifyFlushDoneIfNeeded(): returning flush"; | |
1351 child_message_loop_proxy_->PostTask( | |
1352 FROM_HERE, base::Bind(&Client::NotifyFlushDone, client_)); | |
1353 | |
1354 // While we were flushing, we early-outed DecodeBufferTask()s. | |
1355 ScheduleDecodeBufferTaskIfNeeded(); | |
1356 } | |
1357 | |
1358 void ExynosVideoDecodeAccelerator::ResetTask() { | |
1359 DVLOG(3) << "ResetTask()"; | |
1360 DCHECK_EQ(decoder_thread_.message_loop(), base::MessageLoop::current()); | |
1361 TRACE_EVENT0("Video Decoder", "EVDA::ResetTask"); | |
1362 | |
1363 if (decoder_state_ == kError) { | |
1364 DVLOG(2) << "ResetTask(): early out: kError state"; | |
1365 return; | |
1366 } | |
1367 | |
1368 // If we are in the middle of switching resolutions, postpone reset until | |
1369 // it's done. We don't have to worry about timing of this wrt to decoding, | |
1370 // because MFC input pipe is already stopped if we are changing resolution. | |
1371 // We will come back here after we are done with the resolution change. | |
1372 DCHECK(!resolution_change_reset_pending_); | |
1373 if (resolution_change_pending_ || decoder_state_ == kChangingResolution) { | |
1374 resolution_change_reset_pending_ = true; | |
1375 return; | |
1376 } | |
1377 | |
1378 // We stop streaming and clear buffer tracking info (not preserving | |
1379 // MFC inputs). | |
1380 // StopDevicePoll() unconditionally does _not_ destroy buffers, however. | |
1381 if (!StopDevicePoll(false)) | |
1382 return; | |
1383 | |
1384 decoder_current_bitstream_buffer_.reset(); | |
1385 while (!decoder_input_queue_.empty()) | |
1386 decoder_input_queue_.pop(); | |
1387 | |
1388 decoder_current_input_buffer_ = -1; | |
1389 | |
1390 // If we were flushing, we'll never return any more BitstreamBuffers or | |
1391 // PictureBuffers; they have all been dropped and returned by now. | |
1392 NotifyFlushDoneIfNeeded(); | |
1393 | |
1394 // Mark that we're resetting, then enqueue a ResetDoneTask(). All intervening | |
1395 // jobs will early-out in the kResetting state. | |
1396 decoder_state_ = kResetting; | |
1397 SendPictureReady(); // Send all pending PictureReady. | |
1398 decoder_thread_.message_loop()->PostTask(FROM_HERE, base::Bind( | |
1399 &ExynosVideoDecodeAccelerator::ResetDoneTask, base::Unretained(this))); | |
1400 } | |
1401 | |
1402 void ExynosVideoDecodeAccelerator::ResetDoneTask() { | |
1403 DVLOG(3) << "ResetDoneTask()"; | |
1404 DCHECK_EQ(decoder_thread_.message_loop(), base::MessageLoop::current()); | |
1405 TRACE_EVENT0("Video Decoder", "EVDA::ResetDoneTask"); | |
1406 | |
1407 if (decoder_state_ == kError) { | |
1408 DVLOG(2) << "ResetDoneTask(): early out: kError state"; | |
1409 return; | |
1410 } | |
1411 | |
1412 // We might have received a resolution change event while we were waiting | |
1413 // for the reset to finish. The codec will not post another event if the | |
1414 // resolution after reset remains the same as the one to which were just | |
1415 // about to switch, so preserve the event across reset so we can address | |
1416 // it after resuming. | |
1417 | |
1418 // Reset format-specific bits. | |
1419 if (video_profile_ >= media::H264PROFILE_MIN && | |
1420 video_profile_ <= media::H264PROFILE_MAX) { | |
1421 decoder_h264_parser_.reset(new content::H264Parser()); | |
1422 } | |
1423 | |
1424 // Jobs drained, we're finished resetting. | |
1425 DCHECK_EQ(decoder_state_, kResetting); | |
1426 decoder_state_ = kAfterReset; | |
1427 decoder_partial_frame_pending_ = false; | |
1428 decoder_delay_bitstream_buffer_id_ = -1; | |
1429 child_message_loop_proxy_->PostTask(FROM_HERE, base::Bind( | |
1430 &Client::NotifyResetDone, client_)); | |
1431 | |
1432 // While we were resetting, we early-outed DecodeBufferTask()s. | |
1433 ScheduleDecodeBufferTaskIfNeeded(); | |
1434 } | |
1435 | |
1436 void ExynosVideoDecodeAccelerator::DestroyTask() { | |
1437 DVLOG(3) << "DestroyTask()"; | |
1438 TRACE_EVENT0("Video Decoder", "EVDA::DestroyTask"); | |
1439 | |
1440 // DestroyTask() should run regardless of decoder_state_. | |
1441 | |
1442 // Stop streaming and the device_poll_thread_. | |
1443 StopDevicePoll(false); | |
1444 | |
1445 decoder_current_bitstream_buffer_.reset(); | |
1446 decoder_current_input_buffer_ = -1; | |
1447 decoder_decode_buffer_tasks_scheduled_ = 0; | |
1448 decoder_frames_at_client_ = 0; | |
1449 while (!decoder_input_queue_.empty()) | |
1450 decoder_input_queue_.pop(); | |
1451 decoder_flushing_ = false; | |
1452 | |
1453 // Set our state to kError. Just in case. | |
1454 decoder_state_ = kError; | |
1455 } | |
1456 | |
1457 bool ExynosVideoDecodeAccelerator::StartDevicePoll() { | |
1458 DVLOG(3) << "StartDevicePoll()"; | |
1459 DCHECK_EQ(decoder_thread_.message_loop(), base::MessageLoop::current()); | |
1460 DCHECK(!device_poll_thread_.IsRunning()); | |
1461 | |
1462 // Start up the device poll thread and schedule its first DevicePollTask(). | |
1463 if (!device_poll_thread_.Start()) { | |
1464 DLOG(ERROR) << "StartDevicePoll(): Device thread failed to start"; | |
1465 NOTIFY_ERROR(PLATFORM_FAILURE); | |
1466 return false; | |
1467 } | |
1468 device_poll_thread_.message_loop()->PostTask(FROM_HERE, base::Bind( | |
1469 &ExynosVideoDecodeAccelerator::DevicePollTask, | |
1470 base::Unretained(this), | |
1471 0)); | |
1472 | |
1473 return true; | |
1474 } | |
1475 | |
1476 bool ExynosVideoDecodeAccelerator::StopDevicePoll(bool keep_mfc_input_state) { | |
1477 DVLOG(3) << "StopDevicePoll()"; | |
1478 if (decoder_thread_.IsRunning()) | |
1479 DCHECK_EQ(decoder_thread_.message_loop(), base::MessageLoop::current()); | |
1480 | |
1481 // Signal the DevicePollTask() to stop, and stop the device poll thread. | |
1482 if (!SetDevicePollInterrupt()) | |
1483 return false; | |
1484 device_poll_thread_.Stop(); | |
1485 // Clear the interrupt now, to be sure. | |
1486 if (!ClearDevicePollInterrupt()) | |
1487 return false; | |
1488 | |
1489 // Stop streaming. | |
1490 if (!keep_mfc_input_state) { | |
1491 if (mfc_input_streamon_) { | |
1492 __u32 type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE; | |
1493 IOCTL_OR_ERROR_RETURN_FALSE(mfc_fd_, VIDIOC_STREAMOFF, &type); | |
1494 } | |
1495 mfc_input_streamon_ = false; | |
1496 } | |
1497 if (mfc_output_streamon_) { | |
1498 __u32 type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE; | |
1499 IOCTL_OR_ERROR_RETURN_FALSE(mfc_fd_, VIDIOC_STREAMOFF, &type); | |
1500 } | |
1501 mfc_output_streamon_ = false; | |
1502 | |
1503 // Reset all our accounting info. | |
1504 if (!keep_mfc_input_state) { | |
1505 while (!mfc_input_ready_queue_.empty()) | |
1506 mfc_input_ready_queue_.pop(); | |
1507 mfc_free_input_buffers_.clear(); | |
1508 for (size_t i = 0; i < mfc_input_buffer_map_.size(); ++i) { | |
1509 mfc_free_input_buffers_.push_back(i); | |
1510 mfc_input_buffer_map_[i].at_device = false; | |
1511 mfc_input_buffer_map_[i].bytes_used = 0; | |
1512 mfc_input_buffer_map_[i].input_id = -1; | |
1513 } | |
1514 mfc_input_buffer_queued_count_ = 0; | |
1515 } | |
1516 while (!mfc_free_output_buffers_.empty()) | |
1517 mfc_free_output_buffers_.pop(); | |
1518 for (size_t i = 0; i < mfc_output_buffer_map_.size(); ++i) { | |
1519 MfcOutputRecord& output_record = mfc_output_buffer_map_[i]; | |
1520 // Only mark those free that aren't being held by the VDA client. | |
1521 if (!output_record.at_client) { | |
1522 DCHECK_EQ(output_record.egl_sync, EGL_NO_SYNC_KHR); | |
1523 mfc_free_output_buffers_.push(i); | |
1524 mfc_output_buffer_map_[i].at_device = false; | |
1525 } | |
1526 } | |
1527 mfc_output_buffer_queued_count_ = 0; | |
1528 | |
1529 DVLOG(3) << "StopDevicePoll(): device poll stopped"; | |
1530 return true; | |
1531 } | |
1532 | |
1533 bool ExynosVideoDecodeAccelerator::SetDevicePollInterrupt() { | |
1534 DVLOG(3) << "SetDevicePollInterrupt()"; | |
1535 DCHECK_EQ(decoder_thread_.message_loop(), base::MessageLoop::current()); | |
1536 | |
1537 const uint64 buf = 1; | |
1538 if (HANDLE_EINTR(write(device_poll_interrupt_fd_, &buf, sizeof(buf))) == -1) { | |
1539 DPLOG(ERROR) << "SetDevicePollInterrupt(): write() failed"; | |
1540 NOTIFY_ERROR(PLATFORM_FAILURE); | |
1541 return false; | |
1542 } | |
1543 return true; | |
1544 } | |
1545 | |
1546 bool ExynosVideoDecodeAccelerator::ClearDevicePollInterrupt() { | |
1547 DVLOG(3) << "ClearDevicePollInterrupt()"; | |
1548 DCHECK_EQ(decoder_thread_.message_loop(), base::MessageLoop::current()); | |
1549 | |
1550 uint64 buf; | |
1551 if (HANDLE_EINTR(read(device_poll_interrupt_fd_, &buf, sizeof(buf))) == -1) { | |
1552 if (errno == EAGAIN) { | |
1553 // No interrupt flag set, and we're reading nonblocking. Not an error. | |
1554 return true; | |
1555 } else { | |
1556 DPLOG(ERROR) << "ClearDevicePollInterrupt(): read() failed"; | |
1557 NOTIFY_ERROR(PLATFORM_FAILURE); | |
1558 return false; | |
1559 } | |
1560 } | |
1561 return true; | |
1562 } | |
1563 | |
1564 void ExynosVideoDecodeAccelerator::StartResolutionChangeIfNeeded() { | |
1565 DCHECK_EQ(decoder_thread_.message_loop(), base::MessageLoop::current()); | |
1566 DCHECK_EQ(decoder_state_, kDecoding); | |
1567 | |
1568 if (!resolution_change_pending_) | |
1569 return; | |
1570 | |
1571 DVLOG(3) << "No more work, initiate resolution change"; | |
1572 | |
1573 // Keep MFC input queue. | |
1574 if (!StopDevicePoll(true)) | |
1575 return; | |
1576 | |
1577 decoder_state_ = kChangingResolution; | |
1578 DCHECK(resolution_change_pending_); | |
1579 resolution_change_pending_ = false; | |
1580 | |
1581 // Post a task to clean up buffers on child thread. This will also ensure | |
1582 // that we won't accept ReusePictureBuffer() anymore after that. | |
1583 child_message_loop_proxy_->PostTask(FROM_HERE, base::Bind( | |
1584 &ExynosVideoDecodeAccelerator::ResolutionChangeDestroyBuffers, | |
1585 weak_this_)); | |
1586 } | |
1587 | |
1588 void ExynosVideoDecodeAccelerator::FinishResolutionChange() { | |
1589 DCHECK_EQ(decoder_thread_.message_loop(), base::MessageLoop::current()); | |
1590 DVLOG(3) << "FinishResolutionChange()"; | |
1591 | |
1592 if (decoder_state_ == kError) { | |
1593 DVLOG(2) << "FinishResolutionChange(): early out: kError state"; | |
1594 return; | |
1595 } | |
1596 | |
1597 struct v4l2_format format; | |
1598 bool again; | |
1599 bool ret = GetFormatInfo(&format, &again); | |
1600 if (!ret || again) { | |
1601 DVLOG(3) << "Couldn't get format information after resolution change"; | |
1602 NOTIFY_ERROR(PLATFORM_FAILURE); | |
1603 return; | |
1604 } | |
1605 | |
1606 if (!CreateBuffersForFormat(format)) { | |
1607 DVLOG(3) << "Couldn't reallocate buffers after resolution change"; | |
1608 NOTIFY_ERROR(PLATFORM_FAILURE); | |
1609 return; | |
1610 } | |
1611 | |
1612 // From here we stay in kChangingResolution and wait for | |
1613 // AssignPictureBuffers() before we can resume. | |
1614 } | |
1615 | |
1616 void ExynosVideoDecodeAccelerator::ResumeAfterResolutionChange() { | |
1617 DCHECK_EQ(decoder_thread_.message_loop(), base::MessageLoop::current()); | |
1618 DVLOG(3) << "ResumeAfterResolutionChange()"; | |
1619 | |
1620 decoder_state_ = kDecoding; | |
1621 | |
1622 if (resolution_change_reset_pending_) { | |
1623 resolution_change_reset_pending_ = false; | |
1624 ResetTask(); | |
1625 return; | |
1626 } | |
1627 | |
1628 if (!StartDevicePoll()) | |
1629 return; | |
1630 | |
1631 EnqueueMfc(); | |
1632 ScheduleDecodeBufferTaskIfNeeded(); | |
1633 } | |
1634 | |
1635 void ExynosVideoDecodeAccelerator::DevicePollTask(unsigned int poll_fds) { | |
1636 DVLOG(3) << "DevicePollTask()"; | |
1637 DCHECK_EQ(device_poll_thread_.message_loop(), base::MessageLoop::current()); | |
1638 TRACE_EVENT0("Video Decoder", "EVDA::DevicePollTask"); | |
1639 | |
1640 // This routine just polls the set of device fds, and schedules a | |
1641 // ServiceDeviceTask() on decoder_thread_ when processing needs to occur. | |
1642 // Other threads may notify this task to return early by writing to | |
1643 // device_poll_interrupt_fd_. | |
1644 struct pollfd pollfds[3]; | |
1645 nfds_t nfds; | |
1646 int mfc_pollfd = -1; | |
1647 | |
1648 // Add device_poll_interrupt_fd_; | |
1649 pollfds[0].fd = device_poll_interrupt_fd_; | |
1650 pollfds[0].events = POLLIN | POLLERR; | |
1651 nfds = 1; | |
1652 | |
1653 if (poll_fds & kPollMfc) { | |
1654 DVLOG(3) << "DevicePollTask(): adding MFC to poll() set"; | |
1655 pollfds[nfds].fd = mfc_fd_; | |
1656 pollfds[nfds].events = POLLIN | POLLOUT | POLLERR | POLLPRI; | |
1657 mfc_pollfd = nfds; | |
1658 nfds++; | |
1659 } | |
1660 | |
1661 // Poll it! | |
1662 if (HANDLE_EINTR(poll(pollfds, nfds, -1)) == -1) { | |
1663 DPLOG(ERROR) << "DevicePollTask(): poll() failed"; | |
1664 NOTIFY_ERROR(PLATFORM_FAILURE); | |
1665 return; | |
1666 } | |
1667 | |
1668 bool mfc_event_pending = (mfc_pollfd != -1 && | |
1669 pollfds[mfc_pollfd].revents & POLLPRI); | |
1670 | |
1671 // All processing should happen on ServiceDeviceTask(), since we shouldn't | |
1672 // touch decoder state from this thread. | |
1673 decoder_thread_.message_loop()->PostTask(FROM_HERE, base::Bind( | |
1674 &ExynosVideoDecodeAccelerator::ServiceDeviceTask, | |
1675 base::Unretained(this), mfc_event_pending)); | |
1676 } | |
1677 | |
1678 void ExynosVideoDecodeAccelerator::NotifyError(Error error) { | |
1679 DVLOG(2) << "NotifyError()"; | |
1680 | |
1681 if (!child_message_loop_proxy_->BelongsToCurrentThread()) { | |
1682 child_message_loop_proxy_->PostTask(FROM_HERE, base::Bind( | |
1683 &ExynosVideoDecodeAccelerator::NotifyError, weak_this_, error)); | |
1684 return; | |
1685 } | |
1686 | |
1687 if (client_) { | |
1688 client_->NotifyError(error); | |
1689 client_ptr_factory_.InvalidateWeakPtrs(); | |
1690 } | |
1691 } | |
1692 | |
1693 void ExynosVideoDecodeAccelerator::SetDecoderState(State state) { | |
1694 DVLOG(3) << "SetDecoderState(): state=" << state; | |
1695 | |
1696 // We can touch decoder_state_ only if this is the decoder thread or the | |
1697 // decoder thread isn't running. | |
1698 if (decoder_thread_.message_loop() != NULL && | |
1699 decoder_thread_.message_loop() != base::MessageLoop::current()) { | |
1700 decoder_thread_.message_loop()->PostTask(FROM_HERE, base::Bind( | |
1701 &ExynosVideoDecodeAccelerator::SetDecoderState, | |
1702 base::Unretained(this), state)); | |
1703 } else { | |
1704 decoder_state_ = state; | |
1705 } | |
1706 } | |
1707 | |
1708 bool ExynosVideoDecodeAccelerator::GetFormatInfo(struct v4l2_format* format, | |
1709 bool* again) { | |
1710 DCHECK_EQ(decoder_thread_.message_loop(), base::MessageLoop::current()); | |
1711 | |
1712 *again = false; | |
1713 memset(format, 0, sizeof(*format)); | |
1714 format->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE; | |
1715 if (HANDLE_EINTR(ioctl(mfc_fd_, VIDIOC_G_FMT, format)) != 0) { | |
1716 if (errno == EINVAL) { | |
1717 // EINVAL means we haven't seen sufficient stream to decode the format. | |
1718 *again = true; | |
1719 return true; | |
1720 } else { | |
1721 DPLOG(ERROR) << "DecodeBufferInitial(): ioctl() failed: VIDIOC_G_FMT"; | |
1722 NOTIFY_ERROR(PLATFORM_FAILURE); | |
1723 return false; | |
1724 } | |
1725 } | |
1726 | |
1727 return true; | |
1728 } | |
1729 | |
1730 bool ExynosVideoDecodeAccelerator::CreateBuffersForFormat( | |
1731 const struct v4l2_format& format) { | |
1732 DCHECK_EQ(decoder_thread_.message_loop(), base::MessageLoop::current()); | |
1733 CHECK_EQ(format.fmt.pix_mp.num_planes, 2); | |
1734 frame_buffer_size_.SetSize( | |
1735 format.fmt.pix_mp.width, format.fmt.pix_mp.height); | |
1736 mfc_output_buffer_pixelformat_ = format.fmt.pix_mp.pixelformat; | |
1737 DCHECK_EQ(mfc_output_buffer_pixelformat_, V4L2_PIX_FMT_NV12M); | |
1738 DVLOG(3) << "CreateBuffersForFormat(): new resolution: " | |
1739 << frame_buffer_size_.ToString(); | |
1740 | |
1741 if (!CreateMfcOutputBuffers()) | |
1742 return false; | |
1743 | |
1744 return true; | |
1745 } | |
1746 | |
1747 bool ExynosVideoDecodeAccelerator::CreateMfcInputBuffers() { | |
1748 DVLOG(3) << "CreateMfcInputBuffers()"; | |
1749 // We always run this as we prepare to initialize. | |
1750 DCHECK_EQ(decoder_state_, kUninitialized); | |
1751 DCHECK(!mfc_input_streamon_); | |
1752 DCHECK(mfc_input_buffer_map_.empty()); | |
1753 | |
1754 __u32 pixelformat = 0; | |
1755 if (video_profile_ >= media::H264PROFILE_MIN && | |
1756 video_profile_ <= media::H264PROFILE_MAX) { | |
1757 pixelformat = V4L2_PIX_FMT_H264; | |
1758 } else if (video_profile_ >= media::VP8PROFILE_MIN && | |
1759 video_profile_ <= media::VP8PROFILE_MAX) { | |
1760 pixelformat = V4L2_PIX_FMT_VP8; | |
1761 } else { | |
1762 NOTREACHED(); | |
1763 } | |
1764 | |
1765 struct v4l2_format format; | |
1766 memset(&format, 0, sizeof(format)); | |
1767 format.type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE; | |
1768 format.fmt.pix_mp.pixelformat = pixelformat; | |
1769 format.fmt.pix_mp.plane_fmt[0].sizeimage = kMfcInputBufferMaxSize; | |
1770 format.fmt.pix_mp.num_planes = 1; | |
1771 IOCTL_OR_ERROR_RETURN_FALSE(mfc_fd_, VIDIOC_S_FMT, &format); | |
1772 | |
1773 struct v4l2_requestbuffers reqbufs; | |
1774 memset(&reqbufs, 0, sizeof(reqbufs)); | |
1775 reqbufs.count = kMfcInputBufferCount; | |
1776 reqbufs.type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE; | |
1777 reqbufs.memory = V4L2_MEMORY_MMAP; | |
1778 IOCTL_OR_ERROR_RETURN_FALSE(mfc_fd_, VIDIOC_REQBUFS, &reqbufs); | |
1779 mfc_input_buffer_map_.resize(reqbufs.count); | |
1780 for (size_t i = 0; i < mfc_input_buffer_map_.size(); ++i) { | |
1781 mfc_free_input_buffers_.push_back(i); | |
1782 | |
1783 // Query for the MEMORY_MMAP pointer. | |
1784 struct v4l2_plane planes[1]; | |
1785 struct v4l2_buffer buffer; | |
1786 memset(&buffer, 0, sizeof(buffer)); | |
1787 memset(planes, 0, sizeof(planes)); | |
1788 buffer.index = i; | |
1789 buffer.type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE; | |
1790 buffer.memory = V4L2_MEMORY_MMAP; | |
1791 buffer.m.planes = planes; | |
1792 buffer.length = 1; | |
1793 IOCTL_OR_ERROR_RETURN_FALSE(mfc_fd_, VIDIOC_QUERYBUF, &buffer); | |
1794 void* address = mmap(NULL, buffer.m.planes[0].length, | |
1795 PROT_READ | PROT_WRITE, MAP_SHARED, mfc_fd_, | |
1796 buffer.m.planes[0].m.mem_offset); | |
1797 if (address == MAP_FAILED) { | |
1798 DPLOG(ERROR) << "CreateMfcInputBuffers(): mmap() failed"; | |
1799 return false; | |
1800 } | |
1801 mfc_input_buffer_map_[i].address = address; | |
1802 mfc_input_buffer_map_[i].length = buffer.m.planes[0].length; | |
1803 } | |
1804 | |
1805 return true; | |
1806 } | |
1807 | |
1808 bool ExynosVideoDecodeAccelerator::CreateMfcOutputBuffers() { | |
1809 DVLOG(3) << "CreateMfcOutputBuffers()"; | |
1810 DCHECK(decoder_state_ == kInitialized || | |
1811 decoder_state_ == kChangingResolution); | |
1812 DCHECK(!mfc_output_streamon_); | |
1813 DCHECK(mfc_output_buffer_map_.empty()); | |
1814 | |
1815 // Number of MFC output buffers we need. | |
1816 struct v4l2_control ctrl; | |
1817 memset(&ctrl, 0, sizeof(ctrl)); | |
1818 ctrl.id = V4L2_CID_MIN_BUFFERS_FOR_CAPTURE; | |
1819 IOCTL_OR_ERROR_RETURN_FALSE(mfc_fd_, VIDIOC_G_CTRL, &ctrl); | |
1820 mfc_output_dpb_size_ = ctrl.value; | |
1821 | |
1822 // Output format setup in Initialize(). | |
1823 | |
1824 // Allocate the output buffers. | |
1825 struct v4l2_requestbuffers reqbufs; | |
1826 memset(&reqbufs, 0, sizeof(reqbufs)); | |
1827 reqbufs.count = mfc_output_dpb_size_ + kDpbOutputBufferExtraCount; | |
1828 reqbufs.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE; | |
1829 reqbufs.memory = V4L2_MEMORY_MMAP; | |
1830 IOCTL_OR_ERROR_RETURN_FALSE(mfc_fd_, VIDIOC_REQBUFS, &reqbufs); | |
1831 | |
1832 // Create DMABUFs from output buffers. | |
1833 mfc_output_buffer_map_.resize(reqbufs.count); | |
1834 for (size_t i = 0; i < mfc_output_buffer_map_.size(); ++i) { | |
1835 MfcOutputRecord& output_record = mfc_output_buffer_map_[i]; | |
1836 for (size_t j = 0; j < arraysize(output_record.fds); ++j) { | |
1837 // Export the DMABUF fd so we can export it as a texture. | |
1838 struct v4l2_exportbuffer expbuf; | |
1839 memset(&expbuf, 0, sizeof(expbuf)); | |
1840 expbuf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE; | |
1841 expbuf.index = i; | |
1842 expbuf.plane = j; | |
1843 expbuf.flags = O_CLOEXEC; | |
1844 IOCTL_OR_ERROR_RETURN_FALSE(mfc_fd_, VIDIOC_EXPBUF, &expbuf); | |
1845 output_record.fds[j] = expbuf.fd; | |
1846 } | |
1847 } | |
1848 | |
1849 DVLOG(3) << "CreateMfcOutputBuffers(): ProvidePictureBuffers(): " | |
1850 << "buffer_count=" << mfc_output_buffer_map_.size() | |
1851 << ", width=" << frame_buffer_size_.width() | |
1852 << ", height=" << frame_buffer_size_.height(); | |
1853 child_message_loop_proxy_->PostTask(FROM_HERE, | |
1854 base::Bind(&Client::ProvidePictureBuffers, | |
1855 client_, | |
1856 mfc_output_buffer_map_.size(), | |
1857 frame_buffer_size_, | |
1858 GL_TEXTURE_EXTERNAL_OES)); | |
1859 | |
1860 return true; | |
1861 } | |
1862 | |
1863 void ExynosVideoDecodeAccelerator::DestroyMfcInputBuffers() { | |
1864 DVLOG(3) << "DestroyMfcInputBuffers()"; | |
1865 DCHECK(child_message_loop_proxy_->BelongsToCurrentThread()); | |
1866 DCHECK(!mfc_input_streamon_); | |
1867 | |
1868 for (size_t i = 0; i < mfc_input_buffer_map_.size(); ++i) { | |
1869 if (mfc_input_buffer_map_[i].address != NULL) { | |
1870 munmap(mfc_input_buffer_map_[i].address, | |
1871 mfc_input_buffer_map_[i].length); | |
1872 } | |
1873 } | |
1874 | |
1875 struct v4l2_requestbuffers reqbufs; | |
1876 memset(&reqbufs, 0, sizeof(reqbufs)); | |
1877 reqbufs.count = 0; | |
1878 reqbufs.type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE; | |
1879 reqbufs.memory = V4L2_MEMORY_MMAP; | |
1880 if (ioctl(mfc_fd_, VIDIOC_REQBUFS, &reqbufs) != 0) | |
1881 DPLOG(ERROR) << "DestroyMfcInputBuffers(): ioctl() failed: VIDIOC_REQBUFS"; | |
1882 | |
1883 mfc_input_buffer_map_.clear(); | |
1884 mfc_free_input_buffers_.clear(); | |
1885 } | |
1886 | |
1887 void ExynosVideoDecodeAccelerator::DestroyMfcOutputBuffers() { | |
1888 DVLOG(3) << "DestroyMfcOutputBuffers()"; | |
1889 DCHECK(child_message_loop_proxy_->BelongsToCurrentThread()); | |
1890 DCHECK(!mfc_output_streamon_); | |
1891 | |
1892 if (mfc_output_buffer_map_.size() != 0) { | |
1893 // TODO(sheu, posciak): Making the context current should not be required | |
1894 // anymore. Remove it and verify (crbug.com/327869). | |
1895 if (!make_context_current_.Run()) { | |
1896 DLOG(ERROR) << "DestroyMfcOutputBuffers(): " | |
1897 << "could not make context current"; | |
1898 } else { | |
1899 size_t i = 0; | |
1900 do { | |
1901 MfcOutputRecord& output_record = mfc_output_buffer_map_[i]; | |
1902 for (size_t j = 0; j < arraysize(output_record.fds); ++j) { | |
1903 if (output_record.fds[j] != -1) | |
1904 close(output_record.fds[j]); | |
1905 if (output_record.egl_image != EGL_NO_IMAGE_KHR) | |
1906 eglDestroyImageKHR(egl_display_, output_record.egl_image); | |
1907 if (output_record.egl_sync != EGL_NO_SYNC_KHR) | |
1908 eglDestroySyncKHR(egl_display_, output_record.egl_sync); | |
1909 } | |
1910 DVLOG(1) << "DestroyMfcOutputBuffers(): dismissing PictureBuffer id=" | |
1911 << output_record.picture_id; | |
1912 child_message_loop_proxy_->PostTask( | |
1913 FROM_HERE, | |
1914 base::Bind(&Client::DismissPictureBuffer, | |
1915 client_, | |
1916 output_record.picture_id)); | |
1917 i++; | |
1918 } while (i < mfc_output_buffer_map_.size()); | |
1919 } | |
1920 } | |
1921 | |
1922 struct v4l2_requestbuffers reqbufs; | |
1923 memset(&reqbufs, 0, sizeof(reqbufs)); | |
1924 reqbufs.count = 0; | |
1925 reqbufs.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE; | |
1926 reqbufs.memory = V4L2_MEMORY_MMAP; | |
1927 if (ioctl(mfc_fd_, VIDIOC_REQBUFS, &reqbufs) != 0) | |
1928 DPLOG(ERROR) << "DestroyMfcOutputBuffers() ioctl() failed: VIDIOC_REQBUFS"; | |
1929 | |
1930 mfc_output_buffer_map_.clear(); | |
1931 while (!mfc_free_output_buffers_.empty()) | |
1932 mfc_free_output_buffers_.pop(); | |
1933 } | |
1934 | |
1935 void ExynosVideoDecodeAccelerator::ResolutionChangeDestroyBuffers() { | |
1936 DCHECK(child_message_loop_proxy_->BelongsToCurrentThread()); | |
1937 DVLOG(3) << "ResolutionChangeDestroyBuffers()"; | |
1938 | |
1939 DestroyMfcOutputBuffers(); | |
1940 | |
1941 // Finish resolution change on decoder thread. | |
1942 decoder_thread_.message_loop()->PostTask(FROM_HERE, base::Bind( | |
1943 &ExynosVideoDecodeAccelerator::FinishResolutionChange, | |
1944 base::Unretained(this))); | |
1945 } | |
1946 | |
1947 void ExynosVideoDecodeAccelerator::SendPictureReady() { | |
1948 DVLOG(3) << "SendPictureReady()"; | |
1949 DCHECK_EQ(decoder_thread_.message_loop(), base::MessageLoop::current()); | |
1950 bool resetting_or_flushing = | |
1951 (decoder_state_ == kResetting || decoder_flushing_); | |
1952 while (pending_picture_ready_.size() > 0) { | |
1953 bool cleared = pending_picture_ready_.front().cleared; | |
1954 const media::Picture& picture = pending_picture_ready_.front().picture; | |
1955 if (cleared && picture_clearing_count_ == 0) { | |
1956 // This picture is cleared. Post it to IO thread to reduce latency. This | |
1957 // should be the case after all pictures are cleared at the beginning. | |
1958 io_message_loop_proxy_->PostTask( | |
1959 FROM_HERE, base::Bind(&Client::PictureReady, io_client_, picture)); | |
1960 pending_picture_ready_.pop(); | |
1961 } else if (!cleared || resetting_or_flushing) { | |
1962 DVLOG(3) << "SendPictureReady()" | |
1963 << ". cleared=" << pending_picture_ready_.front().cleared | |
1964 << ", decoder_state_=" << decoder_state_ | |
1965 << ", decoder_flushing_=" << decoder_flushing_ | |
1966 << ", picture_clearing_count_=" << picture_clearing_count_; | |
1967 // If the picture is not cleared, post it to the child thread because it | |
1968 // has to be cleared in the child thread. A picture only needs to be | |
1969 // cleared once. If the decoder is resetting or flushing, send all | |
1970 // pictures to ensure PictureReady arrive before reset or flush done. | |
1971 child_message_loop_proxy_->PostTaskAndReply( | |
1972 FROM_HERE, | |
1973 base::Bind(&Client::PictureReady, client_, picture), | |
1974 // Unretained is safe. If Client::PictureReady gets to run, |this| is | |
1975 // alive. Destroy() will wait the decode thread to finish. | |
1976 base::Bind(&ExynosVideoDecodeAccelerator::PictureCleared, | |
1977 base::Unretained(this))); | |
1978 picture_clearing_count_++; | |
1979 pending_picture_ready_.pop(); | |
1980 } else { | |
1981 // This picture is cleared. But some pictures are about to be cleared on | |
1982 // the child thread. To preserve the order, do not send this until those | |
1983 // pictures are cleared. | |
1984 break; | |
1985 } | |
1986 } | |
1987 } | |
1988 | |
1989 void ExynosVideoDecodeAccelerator::PictureCleared() { | |
1990 DVLOG(3) << "PictureCleared(). clearing count=" << picture_clearing_count_; | |
1991 DCHECK_EQ(decoder_thread_.message_loop(), base::MessageLoop::current()); | |
1992 DCHECK_GT(picture_clearing_count_, 0); | |
1993 picture_clearing_count_--; | |
1994 SendPictureReady(); | |
1995 } | |
1996 | |
1997 } // namespace content | |
OLD | NEW |