1 /*
2  *  Copyright (c) 2015 The WebRTC project authors. All Rights Reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10 
11 #ifndef COMMON_VIDEO_INCLUDE_VIDEO_FRAME_BUFFER_POOL_H_
12 #define COMMON_VIDEO_INCLUDE_VIDEO_FRAME_BUFFER_POOL_H_
13 
14 #include <stddef.h>
15 
16 #include <list>
17 
18 #include "api/scoped_refptr.h"
19 #include "api/video/i420_buffer.h"
20 #include "api/video/nv12_buffer.h"
21 #include "rtc_base/race_checker.h"
22 #include "rtc_base/ref_counted_object.h"
23 
24 namespace webrtc {
25 
26 // Simple buffer pool to avoid unnecessary allocations of video frame buffers.
27 // The pool manages the memory of the I420Buffer/NV12Buffer returned from
28 // Create(I420|NV12)Buffer. When the buffer is destructed, the memory is
29 // returned to the pool for use by subsequent calls to Create(I420|NV12)Buffer.
30 // If the resolution passed to Create(I420|NV12)Buffer changes or requested
31 // pixel format changes, old buffers will be purged from the pool.
32 // Note that Create(I420|NV12)Buffer will crash if more than
33 // kMaxNumberOfFramesBeforeCrash are created. This is to prevent memory leaks
34 // where frames are not returned.
35 class VideoFrameBufferPool {
36  public:
37   VideoFrameBufferPool();
38   explicit VideoFrameBufferPool(bool zero_initialize);
39   VideoFrameBufferPool(bool zero_initialize, size_t max_number_of_buffers);
40   ~VideoFrameBufferPool();
41 
42   // Returns a buffer from the pool. If no suitable buffer exist in the pool
43   // and there are less than |max_number_of_buffers| pending, a buffer is
44   // created. Returns null otherwise.
45   rtc::scoped_refptr<I420Buffer> CreateI420Buffer(int width, int height);
46   rtc::scoped_refptr<NV12Buffer> CreateNV12Buffer(int width, int height);
47 
48   // Changes the max amount of buffers in the pool to the new value.
49   // Returns true if change was successful and false if the amount of already
50   // allocated buffers is bigger than new value.
51   bool Resize(size_t max_number_of_buffers);
52 
53   // Clears buffers_ and detaches the thread checker so that it can be reused
54   // later from another thread.
55   void Release();
56 
57  private:
58   rtc::scoped_refptr<VideoFrameBuffer>
59   GetExistingBuffer(int width, int height, VideoFrameBuffer::Type type);
60 
61   rtc::RaceChecker race_checker_;
62   std::list<rtc::scoped_refptr<VideoFrameBuffer>> buffers_;
63   // If true, newly allocated buffers are zero-initialized. Note that recycled
64   // buffers are not zero'd before reuse. This is required of buffers used by
65   // FFmpeg according to http://crbug.com/390941, which only requires it for the
66   // initial allocation (as shown by FFmpeg's own buffer allocation code). It
67   // has to do with "Use-of-uninitialized-value" on "Linux_msan_chrome".
68   const bool zero_initialize_;
69   // Max number of buffers this pool can have pending.
70   size_t max_number_of_buffers_;
71 };
72 
73 }  // namespace webrtc
74 
75 #endif  // COMMON_VIDEO_INCLUDE_VIDEO_FRAME_BUFFER_POOL_H_
76