Update V8 to version 4.7.56.
[chromium-blink-merge.git] / media / base / video_frame_pool.cc
blobdc9cdf370e249d02f17f978abf698206ad6e6a4c
1 // Copyright 2013 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.
5 #include "media/base/video_frame_pool.h"
7 #include <list>
9 #include "base/bind.h"
10 #include "base/memory/ref_counted.h"
11 #include "base/synchronization/lock.h"
13 namespace media {
15 class VideoFramePool::PoolImpl
16 : public base::RefCountedThreadSafe<VideoFramePool::PoolImpl> {
17 public:
18 PoolImpl();
20 // See VideoFramePool::CreateFrame() for usage.
21 scoped_refptr<VideoFrame> CreateFrame(VideoPixelFormat format,
22 const gfx::Size& coded_size,
23 const gfx::Rect& visible_rect,
24 const gfx::Size& natural_size,
25 base::TimeDelta timestamp);
27 // Shuts down the frame pool and releases all frames in |frames_|.
28 // Once this is called frames will no longer be inserted back into
29 // |frames_|.
30 void Shutdown();
32 size_t GetPoolSizeForTesting() const { return frames_.size(); }
34 private:
35 friend class base::RefCountedThreadSafe<VideoFramePool::PoolImpl>;
36 ~PoolImpl();
38 // Called when the frame wrapper gets destroyed.
39 // |frame| is the actual frame that was wrapped and is placed
40 // in |frames_| by this function so it can be reused.
41 void FrameReleased(const scoped_refptr<VideoFrame>& frame);
43 base::Lock lock_;
44 bool is_shutdown_;
45 std::list<scoped_refptr<VideoFrame> > frames_;
47 DISALLOW_COPY_AND_ASSIGN(PoolImpl);
50 VideoFramePool::PoolImpl::PoolImpl() : is_shutdown_(false) {}
52 VideoFramePool::PoolImpl::~PoolImpl() {
53 DCHECK(is_shutdown_);
56 scoped_refptr<VideoFrame> VideoFramePool::PoolImpl::CreateFrame(
57 VideoPixelFormat format,
58 const gfx::Size& coded_size,
59 const gfx::Rect& visible_rect,
60 const gfx::Size& natural_size,
61 base::TimeDelta timestamp) {
62 base::AutoLock auto_lock(lock_);
63 DCHECK(!is_shutdown_);
65 scoped_refptr<VideoFrame> frame;
66 while (!frame.get() && !frames_.empty()) {
67 scoped_refptr<VideoFrame> pool_frame = frames_.front();
68 frames_.pop_front();
70 if (pool_frame->format() == format &&
71 pool_frame->coded_size() == coded_size &&
72 pool_frame->visible_rect() == visible_rect &&
73 pool_frame->natural_size() == natural_size) {
74 frame = pool_frame;
75 frame->set_timestamp(timestamp);
76 break;
80 if (!frame.get()) {
81 frame = VideoFrame::CreateZeroInitializedFrame(
82 format, coded_size, visible_rect, natural_size, timestamp);
85 scoped_refptr<VideoFrame> wrapped_frame = VideoFrame::WrapVideoFrame(
86 frame, frame->visible_rect(), frame->natural_size());
87 wrapped_frame->AddDestructionObserver(
88 base::Bind(&VideoFramePool::PoolImpl::FrameReleased, this, frame));
89 return wrapped_frame;
92 void VideoFramePool::PoolImpl::Shutdown() {
93 base::AutoLock auto_lock(lock_);
94 is_shutdown_ = true;
95 frames_.clear();
98 void VideoFramePool::PoolImpl::FrameReleased(
99 const scoped_refptr<VideoFrame>& frame) {
100 base::AutoLock auto_lock(lock_);
101 if (is_shutdown_)
102 return;
104 frames_.push_back(frame);
107 VideoFramePool::VideoFramePool() : pool_(new PoolImpl()) {
110 VideoFramePool::~VideoFramePool() {
111 pool_->Shutdown();
114 scoped_refptr<VideoFrame> VideoFramePool::CreateFrame(
115 VideoPixelFormat format,
116 const gfx::Size& coded_size,
117 const gfx::Rect& visible_rect,
118 const gfx::Size& natural_size,
119 base::TimeDelta timestamp) {
120 return pool_->CreateFrame(format, coded_size, visible_rect, natural_size,
121 timestamp);
124 size_t VideoFramePool::GetPoolSizeForTesting() const {
125 return pool_->GetPoolSizeForTesting();
128 } // namespace media