Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "media/video/gpu_memory_buffer_video_frame_pool.h" | 5 #include "media/video/gpu_memory_buffer_video_frame_pool.h" |
| 6 | 6 |
| 7 #include <GLES2/gl2.h> | 7 #include <GLES2/gl2.h> |
| 8 #include <GLES2/gl2ext.h> | 8 #include <GLES2/gl2ext.h> |
| 9 #include <stddef.h> | 9 #include <stddef.h> |
| 10 #include <stdint.h> | 10 #include <stdint.h> |
| 11 | 11 |
| 12 #include <algorithm> | 12 #include <algorithm> |
| 13 #include <list> | 13 #include <list> |
| 14 #include <utility> | 14 #include <utility> |
| 15 | 15 |
| 16 #include "base/barrier_closure.h" | 16 #include "base/barrier_closure.h" |
| 17 #include "base/bind.h" | 17 #include "base/bind.h" |
| 18 #include "base/containers/stack_container.h" | 18 #include "base/containers/stack_container.h" |
| 19 #include "base/location.h" | 19 #include "base/location.h" |
| 20 #include "base/macros.h" | 20 #include "base/macros.h" |
| 21 #include "base/memory/linked_ptr.h" | 21 #include "base/memory/linked_ptr.h" |
| 22 #include "base/strings/stringprintf.h" | 22 #include "base/strings/stringprintf.h" |
| 23 #include "base/trace_event/memory_dump_provider.h" | 23 #include "base/trace_event/memory_dump_provider.h" |
| 24 #include "base/trace_event/trace_event.h" | 24 #include "base/trace_event/trace_event.h" |
| 25 #include "gpu/GLES2/gl2extchromium.h" | 25 #include "gpu/GLES2/gl2extchromium.h" |
| 26 #include "gpu/command_buffer/client/gles2_interface.h" | 26 #include "gpu/command_buffer/client/gles2_interface.h" |
| 27 #include "media/base/bind_to_current_loop.h" | |
| 27 #include "media/renderers/gpu_video_accelerator_factories.h" | 28 #include "media/renderers/gpu_video_accelerator_factories.h" |
| 28 #include "third_party/libyuv/include/libyuv.h" | 29 #include "third_party/libyuv/include/libyuv.h" |
| 29 #include "ui/gfx/buffer_format_util.h" | 30 #include "ui/gfx/buffer_format_util.h" |
| 30 #include "ui/gl/trace_util.h" | 31 #include "ui/gl/trace_util.h" |
| 31 | 32 |
| 32 namespace media { | 33 namespace media { |
| 33 | 34 |
| 34 // Implementation of a pool of GpuMemoryBuffers used to back VideoFrames. | 35 // Implementation of a pool of GpuMemoryBuffers used to back VideoFrames. |
| 35 class GpuMemoryBufferVideoFramePool::PoolImpl | 36 class GpuMemoryBufferVideoFramePool::PoolImpl |
| 36 : public base::RefCountedThreadSafe< | 37 : public base::RefCountedThreadSafe< |
| (...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 132 } | 133 } |
| 133 | 134 |
| 134 // Get the resources needed for a frame out of the pool, or create them if | 135 // Get the resources needed for a frame out of the pool, or create them if |
| 135 // necessary. | 136 // necessary. |
| 136 // This also drops the LRU resources that can't be reuse for this frame. | 137 // This also drops the LRU resources that can't be reuse for this frame. |
| 137 FrameResources* GetOrCreateFrameResources(const gfx::Size& size, | 138 FrameResources* GetOrCreateFrameResources(const gfx::Size& size, |
| 138 VideoPixelFormat format); | 139 VideoPixelFormat format); |
| 139 | 140 |
| 140 // Callback called when a VideoFrame generated with GetFrameResources is no | 141 // Callback called when a VideoFrame generated with GetFrameResources is no |
| 141 // longer referenced. | 142 // longer referenced. |
| 142 // This could be called by any thread. | 143 // This could be called on the thread where |media_task_runner_| is current. |
|
Daniele Castagna
2016/03/21 18:45:19
nit: s/could/must.
dshwang
2016/03/21 19:04:38
Done.
| |
| 143 void MailboxHoldersReleased(FrameResources* frame_resources, | 144 void MailboxHoldersReleased(FrameResources* frame_resources, |
| 144 const gpu::SyncToken& sync_token); | 145 const gpu::SyncToken& sync_token); |
| 145 | 146 |
| 146 // Return frame resources to the pool. This has to be called on the thread | |
| 147 // where |media_task_runner_| is current. | |
| 148 void ReturnFrameResources(FrameResources* frame_resources); | |
| 149 | |
| 150 // Delete resources. This has to be called on the thread where |task_runner| | 147 // Delete resources. This has to be called on the thread where |task_runner| |
| 151 // is current. | 148 // is current. |
| 152 static void DeleteFrameResources(GpuVideoAcceleratorFactories* gpu_factories, | 149 static void DeleteFrameResources(GpuVideoAcceleratorFactories* gpu_factories, |
| 153 FrameResources* frame_resources); | 150 FrameResources* frame_resources); |
| 154 | 151 |
| 155 // Task runner associated to the GL context provided by |gpu_factories_|. | 152 // Task runner associated to the GL context provided by |gpu_factories_|. |
| 156 scoped_refptr<base::SingleThreadTaskRunner> media_task_runner_; | 153 scoped_refptr<base::SingleThreadTaskRunner> media_task_runner_; |
| 157 // Task runner used to asynchronously copy planes. | 154 // Task runner used to asynchronously copy planes. |
| 158 scoped_refptr<base::TaskRunner> worker_task_runner_; | 155 scoped_refptr<base::TaskRunner> worker_task_runner_; |
| 159 | 156 |
| (...skipping 422 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 582 const GLuint64 fence_sync = gles2->InsertFenceSyncCHROMIUM(); | 579 const GLuint64 fence_sync = gles2->InsertFenceSyncCHROMIUM(); |
| 583 gles2->OrderingBarrierCHROMIUM(); | 580 gles2->OrderingBarrierCHROMIUM(); |
| 584 | 581 |
| 585 gpu::SyncToken sync_token; | 582 gpu::SyncToken sync_token; |
| 586 gles2->GenUnverifiedSyncTokenCHROMIUM(fence_sync, sync_token.GetData()); | 583 gles2->GenUnverifiedSyncTokenCHROMIUM(fence_sync, sync_token.GetData()); |
| 587 for (size_t i = 0; i < num_planes; i += planes_per_copy) | 584 for (size_t i = 0; i < num_planes; i += planes_per_copy) |
| 588 mailbox_holders[i].sync_token = sync_token; | 585 mailbox_holders[i].sync_token = sync_token; |
| 589 | 586 |
| 590 scoped_refptr<VideoFrame> frame; | 587 scoped_refptr<VideoFrame> frame; |
| 591 | 588 |
| 592 auto release_mailbox_callback = | 589 auto release_mailbox_callback = BindToCurrentLoop( |
| 593 base::Bind(&PoolImpl::MailboxHoldersReleased, this, frame_resources); | 590 base::Bind(&PoolImpl::MailboxHoldersReleased, this, frame_resources)); |
|
dshwang
2016/03/21 18:31:23
No, this patch doesn't have a race, because I use
Daniele Castagna
2016/03/21 18:45:19
Ooh, sorry, I missed the BindToCurrentLoop. This i
dshwang
2016/03/21 19:04:38
Done.
| |
| 594 | 591 |
| 595 // Create the VideoFrame backed by native textures. | 592 // Create the VideoFrame backed by native textures. |
| 596 gfx::Size visible_size = video_frame->visible_rect().size(); | 593 gfx::Size visible_size = video_frame->visible_rect().size(); |
| 597 switch (output_format_) { | 594 switch (output_format_) { |
| 598 case PIXEL_FORMAT_I420: | 595 case PIXEL_FORMAT_I420: |
| 599 frame = VideoFrame::WrapYUV420NativeTextures( | 596 frame = VideoFrame::WrapYUV420NativeTextures( |
| 600 mailbox_holders[VideoFrame::kYPlane], | 597 mailbox_holders[VideoFrame::kYPlane], |
| 601 mailbox_holders[VideoFrame::kUPlane], | 598 mailbox_holders[VideoFrame::kUPlane], |
| 602 mailbox_holders[VideoFrame::kVPlane], release_mailbox_callback, | 599 mailbox_holders[VideoFrame::kVPlane], release_mailbox_callback, |
| 603 coded_size, gfx::Rect(visible_size), video_frame->natural_size(), | 600 coded_size, gfx::Rect(visible_size), video_frame->natural_size(), |
| (...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 724 gpu::gles2::GLES2Interface* gles2 = lock->ContextGL(); | 721 gpu::gles2::GLES2Interface* gles2 = lock->ContextGL(); |
| 725 | 722 |
| 726 for (PlaneResource& plane_resource : frame_resources->plane_resources) { | 723 for (PlaneResource& plane_resource : frame_resources->plane_resources) { |
| 727 if (plane_resource.image_id) | 724 if (plane_resource.image_id) |
| 728 gles2->DestroyImageCHROMIUM(plane_resource.image_id); | 725 gles2->DestroyImageCHROMIUM(plane_resource.image_id); |
| 729 if (plane_resource.texture_id) | 726 if (plane_resource.texture_id) |
| 730 gles2->DeleteTextures(1, &plane_resource.texture_id); | 727 gles2->DeleteTextures(1, &plane_resource.texture_id); |
| 731 } | 728 } |
| 732 } | 729 } |
| 733 | 730 |
| 734 // Called when a VideoFrame is no longer references. | 731 // Called when a VideoFrame is no longer references. |
|
Daniele Castagna
2016/03/21 18:45:19
nit: s/references/referenced.
dshwang
2016/03/21 19:04:38
Done.
| |
| 732 // Put back the resources in the pool. | |
| 735 void GpuMemoryBufferVideoFramePool::PoolImpl::MailboxHoldersReleased( | 733 void GpuMemoryBufferVideoFramePool::PoolImpl::MailboxHoldersReleased( |
| 736 FrameResources* frame_resources, | 734 FrameResources* frame_resources, |
| 737 const gpu::SyncToken& sync_token) { | 735 const gpu::SyncToken& release_sync_token) { |
| 738 // Return the resource on the media thread. | 736 DCHECK(media_task_runner_->BelongsToCurrentThread()); |
| 739 media_task_runner_->PostTask( | |
| 740 FROM_HERE, | |
| 741 base::Bind(&PoolImpl::ReturnFrameResources, this, frame_resources)); | |
| 742 } | |
| 743 | |
| 744 // Put back the resources in the pool. | |
| 745 void GpuMemoryBufferVideoFramePool::PoolImpl::ReturnFrameResources( | |
| 746 FrameResources* frame_resources) { | |
| 747 auto it = std::find(resources_pool_.begin(), resources_pool_.end(), | 737 auto it = std::find(resources_pool_.begin(), resources_pool_.end(), |
| 748 frame_resources); | 738 frame_resources); |
| 749 DCHECK(it != resources_pool_.end()); | 739 DCHECK(it != resources_pool_.end()); |
| 750 // We want the pool to behave in a FIFO way. | 740 // We want the pool to behave in a FIFO way. |
| 751 // This minimizes the chances of locking the buffer that might be | 741 // This minimizes the chances of locking the buffer that might be |
| 752 // still needed for drawing. | 742 // still needed for drawing. |
| 753 std::swap(*it, resources_pool_.back()); | 743 std::swap(*it, resources_pool_.back()); |
| 744 | |
| 745 gpu_factories_->WaitSyncToken(release_sync_token); | |
| 754 frame_resources->SetIsInUse(false); | 746 frame_resources->SetIsInUse(false); |
| 755 } | 747 } |
| 756 | 748 |
| 757 GpuMemoryBufferVideoFramePool::GpuMemoryBufferVideoFramePool() {} | 749 GpuMemoryBufferVideoFramePool::GpuMemoryBufferVideoFramePool() {} |
| 758 | 750 |
| 759 GpuMemoryBufferVideoFramePool::GpuMemoryBufferVideoFramePool( | 751 GpuMemoryBufferVideoFramePool::GpuMemoryBufferVideoFramePool( |
| 760 const scoped_refptr<base::SingleThreadTaskRunner>& media_task_runner, | 752 const scoped_refptr<base::SingleThreadTaskRunner>& media_task_runner, |
| 761 const scoped_refptr<base::TaskRunner>& worker_task_runner, | 753 const scoped_refptr<base::TaskRunner>& worker_task_runner, |
| 762 GpuVideoAcceleratorFactories* gpu_factories) | 754 GpuVideoAcceleratorFactories* gpu_factories) |
| 763 : pool_impl_( | 755 : pool_impl_( |
| 764 new PoolImpl(media_task_runner, worker_task_runner, gpu_factories)) { | 756 new PoolImpl(media_task_runner, worker_task_runner, gpu_factories)) { |
| 765 base::trace_event::MemoryDumpManager::GetInstance()->RegisterDumpProvider( | 757 base::trace_event::MemoryDumpManager::GetInstance()->RegisterDumpProvider( |
| 766 pool_impl_.get(), "GpuMemoryBufferVideoFramePool", media_task_runner); | 758 pool_impl_.get(), "GpuMemoryBufferVideoFramePool", media_task_runner); |
| 767 } | 759 } |
| 768 | 760 |
| 769 GpuMemoryBufferVideoFramePool::~GpuMemoryBufferVideoFramePool() { | 761 GpuMemoryBufferVideoFramePool::~GpuMemoryBufferVideoFramePool() { |
| 770 base::trace_event::MemoryDumpManager::GetInstance()->UnregisterDumpProvider( | 762 base::trace_event::MemoryDumpManager::GetInstance()->UnregisterDumpProvider( |
| 771 pool_impl_.get()); | 763 pool_impl_.get()); |
| 772 } | 764 } |
| 773 | 765 |
| 774 void GpuMemoryBufferVideoFramePool::MaybeCreateHardwareFrame( | 766 void GpuMemoryBufferVideoFramePool::MaybeCreateHardwareFrame( |
| 775 const scoped_refptr<VideoFrame>& video_frame, | 767 const scoped_refptr<VideoFrame>& video_frame, |
| 776 const FrameReadyCB& frame_ready_cb) { | 768 const FrameReadyCB& frame_ready_cb) { |
| 777 DCHECK(video_frame); | 769 DCHECK(video_frame); |
| 778 pool_impl_->CreateHardwareFrame(video_frame, frame_ready_cb); | 770 pool_impl_->CreateHardwareFrame(video_frame, frame_ready_cb); |
| 779 } | 771 } |
| 780 | 772 |
| 781 } // namespace media | 773 } // namespace media |
| OLD | NEW |