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 must be called on the thread where |media_task_runner_| is current. |
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 427 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
587 const GLuint64 fence_sync = gles2->InsertFenceSyncCHROMIUM(); | 584 const GLuint64 fence_sync = gles2->InsertFenceSyncCHROMIUM(); |
588 gles2->OrderingBarrierCHROMIUM(); | 585 gles2->OrderingBarrierCHROMIUM(); |
589 | 586 |
590 gpu::SyncToken sync_token; | 587 gpu::SyncToken sync_token; |
591 gles2->GenUnverifiedSyncTokenCHROMIUM(fence_sync, sync_token.GetData()); | 588 gles2->GenUnverifiedSyncTokenCHROMIUM(fence_sync, sync_token.GetData()); |
592 for (size_t i = 0; i < num_planes; i += planes_per_copy) | 589 for (size_t i = 0; i < num_planes; i += planes_per_copy) |
593 mailbox_holders[i].sync_token = sync_token; | 590 mailbox_holders[i].sync_token = sync_token; |
594 | 591 |
595 scoped_refptr<VideoFrame> frame; | 592 scoped_refptr<VideoFrame> frame; |
596 | 593 |
597 auto release_mailbox_callback = | 594 auto release_mailbox_callback = BindToCurrentLoop( |
598 base::Bind(&PoolImpl::MailboxHoldersReleased, this, frame_resources); | 595 base::Bind(&PoolImpl::MailboxHoldersReleased, this, frame_resources)); |
599 | 596 |
600 // Create the VideoFrame backed by native textures. | 597 // Create the VideoFrame backed by native textures. |
601 gfx::Size visible_size = video_frame->visible_rect().size(); | 598 gfx::Size visible_size = video_frame->visible_rect().size(); |
602 switch (output_format_) { | 599 switch (output_format_) { |
603 case PIXEL_FORMAT_I420: | 600 case PIXEL_FORMAT_I420: |
604 frame = VideoFrame::WrapYUV420NativeTextures( | 601 frame = VideoFrame::WrapYUV420NativeTextures( |
605 mailbox_holders[VideoFrame::kYPlane], | 602 mailbox_holders[VideoFrame::kYPlane], |
606 mailbox_holders[VideoFrame::kUPlane], | 603 mailbox_holders[VideoFrame::kUPlane], |
607 mailbox_holders[VideoFrame::kVPlane], release_mailbox_callback, | 604 mailbox_holders[VideoFrame::kVPlane], release_mailbox_callback, |
608 coded_size, gfx::Rect(visible_size), video_frame->natural_size(), | 605 coded_size, gfx::Rect(visible_size), video_frame->natural_size(), |
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
729 gpu::gles2::GLES2Interface* gles2 = lock->ContextGL(); | 726 gpu::gles2::GLES2Interface* gles2 = lock->ContextGL(); |
730 | 727 |
731 for (PlaneResource& plane_resource : frame_resources->plane_resources) { | 728 for (PlaneResource& plane_resource : frame_resources->plane_resources) { |
732 if (plane_resource.image_id) | 729 if (plane_resource.image_id) |
733 gles2->DestroyImageCHROMIUM(plane_resource.image_id); | 730 gles2->DestroyImageCHROMIUM(plane_resource.image_id); |
734 if (plane_resource.texture_id) | 731 if (plane_resource.texture_id) |
735 gles2->DeleteTextures(1, &plane_resource.texture_id); | 732 gles2->DeleteTextures(1, &plane_resource.texture_id); |
736 } | 733 } |
737 } | 734 } |
738 | 735 |
739 // Called when a VideoFrame is no longer references. | 736 // Called when a VideoFrame is no longer referenced. |
| 737 // Put back the resources in the pool. |
740 void GpuMemoryBufferVideoFramePool::PoolImpl::MailboxHoldersReleased( | 738 void GpuMemoryBufferVideoFramePool::PoolImpl::MailboxHoldersReleased( |
741 FrameResources* frame_resources, | 739 FrameResources* frame_resources, |
742 const gpu::SyncToken& sync_token) { | 740 const gpu::SyncToken& release_sync_token) { |
743 // Return the resource on the media thread. | 741 DCHECK(media_task_runner_->BelongsToCurrentThread()); |
744 media_task_runner_->PostTask( | |
745 FROM_HERE, | |
746 base::Bind(&PoolImpl::ReturnFrameResources, this, frame_resources)); | |
747 } | |
748 | |
749 // Put back the resources in the pool. | |
750 void GpuMemoryBufferVideoFramePool::PoolImpl::ReturnFrameResources( | |
751 FrameResources* frame_resources) { | |
752 auto it = std::find(resources_pool_.begin(), resources_pool_.end(), | 742 auto it = std::find(resources_pool_.begin(), resources_pool_.end(), |
753 frame_resources); | 743 frame_resources); |
754 DCHECK(it != resources_pool_.end()); | 744 DCHECK(it != resources_pool_.end()); |
755 // We want the pool to behave in a FIFO way. | 745 // We want the pool to behave in a FIFO way. |
756 // This minimizes the chances of locking the buffer that might be | 746 // This minimizes the chances of locking the buffer that might be |
757 // still needed for drawing. | 747 // still needed for drawing. |
758 std::swap(*it, resources_pool_.back()); | 748 std::swap(*it, resources_pool_.back()); |
759 frame_resources->SetIsInUse(false); | 749 frame_resources->SetIsInUse(false); |
760 } | 750 } |
761 | 751 |
(...skipping 15 matching lines...) Expand all Loading... |
777 } | 767 } |
778 | 768 |
779 void GpuMemoryBufferVideoFramePool::MaybeCreateHardwareFrame( | 769 void GpuMemoryBufferVideoFramePool::MaybeCreateHardwareFrame( |
780 const scoped_refptr<VideoFrame>& video_frame, | 770 const scoped_refptr<VideoFrame>& video_frame, |
781 const FrameReadyCB& frame_ready_cb) { | 771 const FrameReadyCB& frame_ready_cb) { |
782 DCHECK(video_frame); | 772 DCHECK(video_frame); |
783 pool_impl_->CreateHardwareFrame(video_frame, frame_ready_cb); | 773 pool_impl_->CreateHardwareFrame(video_frame, frame_ready_cb); |
784 } | 774 } |
785 | 775 |
786 } // namespace media | 776 } // namespace media |
OLD | NEW |