xref: /aosp_15_r20/external/webrtc/common_video/include/video_frame_buffer_pool.h (revision d9f758449e529ab9291ac668be2861e7a55c2422)
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/i010_buffer.h"
20 #include "api/video/i210_buffer.h"
21 #include "api/video/i420_buffer.h"
22 #include "api/video/i422_buffer.h"
23 #include "api/video/i444_buffer.h"
24 #include "api/video/nv12_buffer.h"
25 #include "rtc_base/race_checker.h"
26 
27 namespace webrtc {
28 
29 // Simple buffer pool to avoid unnecessary allocations of video frame buffers.
30 // The pool manages the memory of the I420Buffer/NV12Buffer returned from
31 // Create(I420|NV12)Buffer. When the buffer is destructed, the memory is
32 // returned to the pool for use by subsequent calls to Create(I420|NV12)Buffer.
33 // If the resolution passed to Create(I420|NV12)Buffer changes or requested
34 // pixel format changes, old buffers will be purged from the pool.
35 // Note that Create(I420|NV12)Buffer will crash if more than
36 // kMaxNumberOfFramesBeforeCrash are created. This is to prevent memory leaks
37 // where frames are not returned.
38 class VideoFrameBufferPool {
39  public:
40   VideoFrameBufferPool();
41   explicit VideoFrameBufferPool(bool zero_initialize);
42   VideoFrameBufferPool(bool zero_initialize, size_t max_number_of_buffers);
43   ~VideoFrameBufferPool();
44 
45   // Returns a buffer from the pool. If no suitable buffer exist in the pool
46   // and there are less than `max_number_of_buffers` pending, a buffer is
47   // created. Returns null otherwise.
48   rtc::scoped_refptr<I420Buffer> CreateI420Buffer(int width, int height);
49   rtc::scoped_refptr<I422Buffer> CreateI422Buffer(int width, int height);
50   rtc::scoped_refptr<I444Buffer> CreateI444Buffer(int width, int height);
51   rtc::scoped_refptr<I010Buffer> CreateI010Buffer(int width, int height);
52   rtc::scoped_refptr<I210Buffer> CreateI210Buffer(int width, int height);
53   rtc::scoped_refptr<NV12Buffer> CreateNV12Buffer(int width, int height);
54 
55   // Changes the max amount of buffers in the pool to the new value.
56   // Returns true if change was successful and false if the amount of already
57   // allocated buffers is bigger than new value.
58   bool Resize(size_t max_number_of_buffers);
59 
60   // Clears buffers_ and detaches the thread checker so that it can be reused
61   // later from another thread.
62   void Release();
63 
64  private:
65   rtc::scoped_refptr<VideoFrameBuffer>
66   GetExistingBuffer(int width, int height, VideoFrameBuffer::Type type);
67 
68   rtc::RaceChecker race_checker_;
69   std::list<rtc::scoped_refptr<VideoFrameBuffer>> buffers_;
70   // If true, newly allocated buffers are zero-initialized. Note that recycled
71   // buffers are not zero'd before reuse. This is required of buffers used by
72   // FFmpeg according to http://crbug.com/390941, which only requires it for the
73   // initial allocation (as shown by FFmpeg's own buffer allocation code). It
74   // has to do with "Use-of-uninitialized-value" on "Linux_msan_chrome".
75   const bool zero_initialize_;
76   // Max number of buffers this pool can have pending.
77   size_t max_number_of_buffers_;
78 };
79 
80 }  // namespace webrtc
81 
82 #endif  // COMMON_VIDEO_INCLUDE_VIDEO_FRAME_BUFFER_POOL_H_
83