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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #ifndef COMMON_VIDEO_INCLUDE_I420_BUFFER_POOL_H_ |
| 12 | #define COMMON_VIDEO_INCLUDE_I420_BUFFER_POOL_H_ |
kjellander | 6f8ce06 | 2015-11-16 13:52:24 -0800 | [diff] [blame] | 13 | |
| 14 | #include <list> |
Per | 0098357 | 2016-11-04 08:57:26 +0100 | [diff] [blame] | 15 | #include <limits> |
kjellander | 6f8ce06 | 2015-11-16 13:52:24 -0800 | [diff] [blame] | 16 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 17 | #include "api/video/i420_buffer.h" |
| 18 | #include "rtc_base/race_checker.h" |
Niels Möller | b7239a9 | 2017-10-03 10:42:04 +0200 | [diff] [blame^] | 19 | #include "rtc_base/refcountedobject.h" |
kjellander | 6f8ce06 | 2015-11-16 13:52:24 -0800 | [diff] [blame] | 20 | |
| 21 | namespace webrtc { |
| 22 | |
| 23 | // Simple buffer pool to avoid unnecessary allocations of I420Buffer objects. |
| 24 | // The pool manages the memory of the I420Buffer returned from CreateBuffer. |
| 25 | // When the I420Buffer is destructed, the memory is returned to the pool for use |
| 26 | // by subsequent calls to CreateBuffer. If the resolution passed to CreateBuffer |
| 27 | // changes, old buffers will be purged from the pool. |
perkj | 5f8ebae | 2016-09-27 03:47:41 -0700 | [diff] [blame] | 28 | // Note that CreateBuffer will crash if more than kMaxNumberOfFramesBeforeCrash |
| 29 | // are created. This is to prevent memory leaks where frames are not returned. |
kjellander | 6f8ce06 | 2015-11-16 13:52:24 -0800 | [diff] [blame] | 30 | class I420BufferPool { |
| 31 | public: |
Per | 0098357 | 2016-11-04 08:57:26 +0100 | [diff] [blame] | 32 | I420BufferPool() |
perkj | 536447c | 2016-11-04 05:33:44 -0700 | [diff] [blame] | 33 | : I420BufferPool(false) {} |
Per | 0098357 | 2016-11-04 08:57:26 +0100 | [diff] [blame] | 34 | explicit I420BufferPool(bool zero_initialize) |
| 35 | : I420BufferPool(zero_initialize, std::numeric_limits<size_t>::max()) {} |
| 36 | I420BufferPool(bool zero_initialze, size_t max_number_of_buffers); |
hbos | 900f975 | 2016-02-05 08:08:34 -0800 | [diff] [blame] | 37 | |
Per | 0098357 | 2016-11-04 08:57:26 +0100 | [diff] [blame] | 38 | // Returns a buffer from the pool. If no suitable buffer exist in the pool |
| 39 | // and there are less than |max_number_of_buffers| pending, a buffer is |
| 40 | // created. Returns null otherwise. |
nisse | d591e3f | 2016-05-26 06:49:55 -0700 | [diff] [blame] | 41 | rtc::scoped_refptr<I420Buffer> CreateBuffer(int width, int height); |
kjellander | 6f8ce06 | 2015-11-16 13:52:24 -0800 | [diff] [blame] | 42 | // Clears buffers_ and detaches the thread checker so that it can be reused |
| 43 | // later from another thread. |
| 44 | void Release(); |
| 45 | |
| 46 | private: |
nisse | d591e3f | 2016-05-26 06:49:55 -0700 | [diff] [blame] | 47 | // Explicitly use a RefCountedObject to get access to HasOneRef, |
| 48 | // needed by the pool to check exclusive access. |
| 49 | using PooledI420Buffer = rtc::RefCountedObject<I420Buffer>; |
| 50 | |
Magnus Jedvert | 7fd9286 | 2016-09-17 11:27:35 +0200 | [diff] [blame] | 51 | rtc::RaceChecker race_checker_; |
nisse | d591e3f | 2016-05-26 06:49:55 -0700 | [diff] [blame] | 52 | std::list<rtc::scoped_refptr<PooledI420Buffer>> buffers_; |
hbos | 900f975 | 2016-02-05 08:08:34 -0800 | [diff] [blame] | 53 | // If true, newly allocated buffers are zero-initialized. Note that recycled |
| 54 | // buffers are not zero'd before reuse. This is required of buffers used by |
| 55 | // FFmpeg according to http://crbug.com/390941, which only requires it for the |
| 56 | // initial allocation (as shown by FFmpeg's own buffer allocation code). It |
| 57 | // has to do with "Use-of-uninitialized-value" on "Linux_msan_chrome". |
perkj | 536447c | 2016-11-04 05:33:44 -0700 | [diff] [blame] | 58 | const bool zero_initialize_; |
Per | 0098357 | 2016-11-04 08:57:26 +0100 | [diff] [blame] | 59 | // Max number of buffers this pool can have pending. |
perkj | 536447c | 2016-11-04 05:33:44 -0700 | [diff] [blame] | 60 | const size_t max_number_of_buffers_; |
kjellander | 6f8ce06 | 2015-11-16 13:52:24 -0800 | [diff] [blame] | 61 | }; |
| 62 | |
| 63 | } // namespace webrtc |
| 64 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 65 | #endif // COMMON_VIDEO_INCLUDE_I420_BUFFER_POOL_H_ |