kjellander | 6f8ce06 | 2015-11-16 13:52:24 -0800 | [diff] [blame] | 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 WEBRTC_COMMON_VIDEO_INCLUDE_I420_BUFFER_POOL_H_ |
| 12 | #define WEBRTC_COMMON_VIDEO_INCLUDE_I420_BUFFER_POOL_H_ |
| 13 | |
| 14 | #include <list> |
| 15 | |
Magnus Jedvert | 7fd9286 | 2016-09-17 11:27:35 +0200 | [diff] [blame] | 16 | #include "webrtc/base/race_checker.h" |
kjellander | 6f8ce06 | 2015-11-16 13:52:24 -0800 | [diff] [blame] | 17 | #include "webrtc/common_video/include/video_frame_buffer.h" |
| 18 | |
| 19 | namespace webrtc { |
| 20 | |
| 21 | // Simple buffer pool to avoid unnecessary allocations of I420Buffer objects. |
| 22 | // 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 | // by subsequent calls to CreateBuffer. If the resolution passed to CreateBuffer |
| 25 | // changes, old buffers will be purged from the pool. |
perkj | 5f8ebae | 2016-09-27 03:47:41 -0700 | [diff] [blame] | 26 | // Note that CreateBuffer will crash if more than kMaxNumberOfFramesBeforeCrash |
| 27 | // are created. This is to prevent memory leaks where frames are not returned. |
kjellander | 6f8ce06 | 2015-11-16 13:52:24 -0800 | [diff] [blame] | 28 | class I420BufferPool { |
| 29 | public: |
hbos | 900f975 | 2016-02-05 08:08:34 -0800 | [diff] [blame] | 30 | I420BufferPool() : I420BufferPool(false) {} |
| 31 | explicit I420BufferPool(bool zero_initialize); |
| 32 | |
kjellander | 6f8ce06 | 2015-11-16 13:52:24 -0800 | [diff] [blame] | 33 | // Returns a buffer from the pool, or creates a new buffer if no suitable |
| 34 | // buffer exists in the pool. |
nisse | d591e3f | 2016-05-26 06:49:55 -0700 | [diff] [blame] | 35 | rtc::scoped_refptr<I420Buffer> CreateBuffer(int width, int height); |
kjellander | 6f8ce06 | 2015-11-16 13:52:24 -0800 | [diff] [blame] | 36 | // Clears buffers_ and detaches the thread checker so that it can be reused |
| 37 | // later from another thread. |
| 38 | void Release(); |
| 39 | |
| 40 | private: |
perkj | 5f8ebae | 2016-09-27 03:47:41 -0700 | [diff] [blame] | 41 | static const size_t kMaxNumberOfFramesBeforeCrash; |
nisse | d591e3f | 2016-05-26 06:49:55 -0700 | [diff] [blame] | 42 | // Explicitly use a RefCountedObject to get access to HasOneRef, |
| 43 | // needed by the pool to check exclusive access. |
| 44 | using PooledI420Buffer = rtc::RefCountedObject<I420Buffer>; |
| 45 | |
Magnus Jedvert | 7fd9286 | 2016-09-17 11:27:35 +0200 | [diff] [blame] | 46 | rtc::RaceChecker race_checker_; |
nisse | d591e3f | 2016-05-26 06:49:55 -0700 | [diff] [blame] | 47 | std::list<rtc::scoped_refptr<PooledI420Buffer>> buffers_; |
hbos | 900f975 | 2016-02-05 08:08:34 -0800 | [diff] [blame] | 48 | // 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 |
| 50 | // 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 |
| 52 | // has to do with "Use-of-uninitialized-value" on "Linux_msan_chrome". |
| 53 | bool zero_initialize_; |
kjellander | 6f8ce06 | 2015-11-16 13:52:24 -0800 | [diff] [blame] | 54 | }; |
| 55 | |
| 56 | } // namespace webrtc |
| 57 | |
| 58 | #endif // WEBRTC_COMMON_VIDEO_INCLUDE_I420_BUFFER_POOL_H_ |