OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright (c) 2015 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2015 The WebRTC project authors. All Rights Reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
11 #ifndef WEBRTC_COMMON_VIDEO_INCLUDE_I420_BUFFER_POOL_H_ | 11 #ifndef WEBRTC_COMMON_VIDEO_INCLUDE_I420_BUFFER_POOL_H_ |
12 #define WEBRTC_COMMON_VIDEO_INCLUDE_I420_BUFFER_POOL_H_ | 12 #define WEBRTC_COMMON_VIDEO_INCLUDE_I420_BUFFER_POOL_H_ |
13 | 13 |
14 #include <list> | 14 #include <list> |
15 #include <limits> | |
15 | 16 |
16 #include "webrtc/base/race_checker.h" | 17 #include "webrtc/base/race_checker.h" |
17 #include "webrtc/common_video/include/video_frame_buffer.h" | 18 #include "webrtc/common_video/include/video_frame_buffer.h" |
18 | 19 |
19 namespace webrtc { | 20 namespace webrtc { |
20 | 21 |
21 // Simple buffer pool to avoid unnecessary allocations of I420Buffer objects. | 22 // Simple buffer pool to avoid unnecessary allocations of I420Buffer objects. |
22 // The pool manages the memory of the I420Buffer returned from CreateBuffer. | 23 // The pool manages the memory of the I420Buffer returned from CreateBuffer. |
23 // When the I420Buffer is destructed, the memory is returned to the pool for use | 24 // When the I420Buffer is destructed, the memory is returned to the pool for use |
24 // by subsequent calls to CreateBuffer. If the resolution passed to CreateBuffer | 25 // by subsequent calls to CreateBuffer. If the resolution passed to CreateBuffer |
25 // changes, old buffers will be purged from the pool. | 26 // changes, old buffers will be purged from the pool. |
26 // Note that CreateBuffer will crash if more than kMaxNumberOfFramesBeforeCrash | 27 // Note that CreateBuffer will crash if more than kMaxNumberOfFramesBeforeCrash |
27 // are created. This is to prevent memory leaks where frames are not returned. | 28 // are created. This is to prevent memory leaks where frames are not returned. |
28 class I420BufferPool { | 29 class I420BufferPool { |
29 public: | 30 public: |
30 I420BufferPool() : I420BufferPool(false) {} | 31 I420BufferPool() |
31 explicit I420BufferPool(bool zero_initialize); | 32 : I420BufferPool(false, std::numeric_limits<size_t>::max()) {} |
magjed_webrtc
2016/11/04 08:21:36
nit: this is enough: I420BufferPool(false) {}
| |
33 explicit I420BufferPool(bool zero_initialize) | |
34 : I420BufferPool(zero_initialize, std::numeric_limits<size_t>::max()) {} | |
35 I420BufferPool(bool zero_initialze, size_t max_number_of_buffers); | |
32 | 36 |
33 // Returns a buffer from the pool, or creates a new buffer if no suitable | 37 // Returns a buffer from the pool. If no suitable buffer exist in the pool |
34 // buffer exists in the pool. | 38 // and there are less than |max_number_of_buffers| pending, a buffer is |
39 // created. Returns null otherwise. | |
35 rtc::scoped_refptr<I420Buffer> CreateBuffer(int width, int height); | 40 rtc::scoped_refptr<I420Buffer> CreateBuffer(int width, int height); |
36 // Clears buffers_ and detaches the thread checker so that it can be reused | 41 // Clears buffers_ and detaches the thread checker so that it can be reused |
37 // later from another thread. | 42 // later from another thread. |
38 void Release(); | 43 void Release(); |
39 | 44 |
40 private: | 45 private: |
41 static const size_t kMaxNumberOfFramesBeforeCrash; | |
42 // Explicitly use a RefCountedObject to get access to HasOneRef, | 46 // Explicitly use a RefCountedObject to get access to HasOneRef, |
43 // needed by the pool to check exclusive access. | 47 // needed by the pool to check exclusive access. |
44 using PooledI420Buffer = rtc::RefCountedObject<I420Buffer>; | 48 using PooledI420Buffer = rtc::RefCountedObject<I420Buffer>; |
45 | 49 |
46 rtc::RaceChecker race_checker_; | 50 rtc::RaceChecker race_checker_; |
47 std::list<rtc::scoped_refptr<PooledI420Buffer>> buffers_; | 51 std::list<rtc::scoped_refptr<PooledI420Buffer>> buffers_; |
48 // If true, newly allocated buffers are zero-initialized. Note that recycled | 52 // If true, newly allocated buffers are zero-initialized. Note that recycled |
49 // buffers are not zero'd before reuse. This is required of buffers used by | 53 // buffers are not zero'd before reuse. This is required of buffers used by |
50 // FFmpeg according to http://crbug.com/390941, which only requires it for the | 54 // FFmpeg according to http://crbug.com/390941, which only requires it for the |
51 // initial allocation (as shown by FFmpeg's own buffer allocation code). It | 55 // initial allocation (as shown by FFmpeg's own buffer allocation code). It |
52 // has to do with "Use-of-uninitialized-value" on "Linux_msan_chrome". | 56 // has to do with "Use-of-uninitialized-value" on "Linux_msan_chrome". |
53 bool zero_initialize_; | 57 bool zero_initialize_; |
58 // Max number of buffers this pool can have pending. | |
59 size_t max_number_of_buffers_; | |
magjed_webrtc
2016/11/04 08:21:36
It would be nice to have this (and zero_initialize
| |
54 }; | 60 }; |
55 | 61 |
56 } // namespace webrtc | 62 } // namespace webrtc |
57 | 63 |
58 #endif // WEBRTC_COMMON_VIDEO_INCLUDE_I420_BUFFER_POOL_H_ | 64 #endif // WEBRTC_COMMON_VIDEO_INCLUDE_I420_BUFFER_POOL_H_ |
OLD | NEW |