Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(106)

Side by Side Diff: media/base/video_frame_pool.cc

Issue 101623006: Created VideoFramePool to avoid unnecessary VideoFrame alloc/free. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Add PoolImpl destructor to make clang on the bots happy. Created 7 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
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.
4
5 #include "media/base/video_frame_pool.h"
6
7 #include <list>
8
9 #include "base/bind.h"
10 #include "base/memory/ref_counted.h"
11 #include "base/synchronization/lock.h"
12
13 namespace media {
14
15 class VideoFramePool::PoolImpl
16 : public base::RefCountedThreadSafe<VideoFramePool::PoolImpl> {
17 public:
18 PoolImpl();
19
20 // Returns a frame from the pool that matches the specified
DaleCurtis 2013/12/09 20:48:25 Comment should indicate that the pool is drained i
acolwell GONE FROM CHROMIUM 2013/12/09 21:42:11 Done.
21 // parameters or creates a new frame if no suitable frame exists in
22 // the pool.
23 scoped_refptr<VideoFrame> CreateFrame(VideoFrame::Format format,
24 const gfx::Size& coded_size,
25 const gfx::Rect& visible_rect,
26 const gfx::Size& natural_size,
27 base::TimeDelta timestamp);
28
29 // Shuts down the frame pool and releases all frames in |frames_|.
30 // Once this is called frames will no longer be inserted back into
31 // |frames_|.
32 void Shutdown();
33
34 private:
35 friend class base::RefCountedThreadSafe<VideoFramePool::PoolImpl>;
36 ~PoolImpl();
37
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);
42
43 base::Lock lock_;
44 bool is_shutdown_;
45 std::list<scoped_refptr<VideoFrame> > frames_;
46
47 DISALLOW_COPY_AND_ASSIGN(PoolImpl);
48 };
49
50 VideoFramePool::PoolImpl::PoolImpl() : is_shutdown_(false) {}
51
52 VideoFramePool::PoolImpl::~PoolImpl() {
53 DCHECK(is_shutdown_);
54 }
55
56 scoped_refptr<VideoFrame> VideoFramePool::PoolImpl::CreateFrame(
57 VideoFrame::Format 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_);
64
65 scoped_refptr<VideoFrame> frame;
66
67 while (!frame && !frames_.empty()) {
68 scoped_refptr<VideoFrame> pool_frame = frames_.front();
69 frames_.pop_front();
70
71 if (pool_frame->format() == format &&
72 pool_frame->coded_size() == coded_size &&
73 pool_frame->visible_rect() == visible_rect &&
74 pool_frame->natural_size() == natural_size) {
75 frame = pool_frame;
76 frame->SetTimestamp(kNoTimestamp());
77 break;
78 }
79 }
80
81 if (!frame) {
82 frame = VideoFrame::CreateFrame(
83 format, coded_size, visible_rect, natural_size, kNoTimestamp());
84 }
85
86 return VideoFrame::WrapVideoFrame(
87 frame, base::Bind(&VideoFramePool::PoolImpl::FrameReleased, this));
DaleCurtis 2013/12/09 20:48:25 As mentioned on the other file, just bind frame in
acolwell GONE FROM CHROMIUM 2013/12/09 21:42:11 Done.
88 }
89
90 void VideoFramePool::PoolImpl::Shutdown() {
91 base::AutoLock auto_lock(lock_);
92 is_shutdown_ = true;
93 frames_.clear();
94 }
95
DaleCurtis 2013/12/09 20:48:25 Extra line.
acolwell GONE FROM CHROMIUM 2013/12/09 21:42:11 Done.
96
97 void VideoFramePool::PoolImpl::FrameReleased(
98 const scoped_refptr<VideoFrame>& frame) {
99 base::AutoLock auto_lock(lock_);
100 if (is_shutdown_)
101 return;
102
103 frames_.push_back(frame);
104 }
105
106 VideoFramePool::VideoFramePool() : pool_(new PoolImpl()) {
107 }
108
109 VideoFramePool::~VideoFramePool() {
110 pool_->Shutdown();
111 }
112
113 scoped_refptr<VideoFrame> VideoFramePool::CreateFrame(
114 VideoFrame::Format format,
115 const gfx::Size& coded_size,
116 const gfx::Rect& visible_rect,
117 const gfx::Size& natural_size,
118 base::TimeDelta timestamp) {
119 return pool_->CreateFrame(format, coded_size, visible_rect, natural_size,
120 timestamp);
121 }
122
123 } // namespace media
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698