blob: f43a948331e6cb14bf6df9e65e10481886569810 [file] [log] [blame]
kjellander6f8ce062015-11-16 13:52:24 -08001/*
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 Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef COMMON_VIDEO_INCLUDE_I420_BUFFER_POOL_H_
12#define COMMON_VIDEO_INCLUDE_I420_BUFFER_POOL_H_
kjellander6f8ce062015-11-16 13:52:24 -080013
14#include <list>
Per00983572016-11-04 08:57:26 +010015#include <limits>
kjellander6f8ce062015-11-16 13:52:24 -080016
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020017#include "api/video/i420_buffer.h"
18#include "rtc_base/race_checker.h"
kjellander6f8ce062015-11-16 13:52:24 -080019
20namespace webrtc {
21
22// Simple buffer pool to avoid unnecessary allocations of I420Buffer objects.
23// The pool manages the memory of the I420Buffer returned from CreateBuffer.
24// When the I420Buffer is destructed, the memory is returned to the pool for use
25// by subsequent calls to CreateBuffer. If the resolution passed to CreateBuffer
26// changes, old buffers will be purged from the pool.
perkj5f8ebae2016-09-27 03:47:41 -070027// Note that CreateBuffer will crash if more than kMaxNumberOfFramesBeforeCrash
28// are created. This is to prevent memory leaks where frames are not returned.
kjellander6f8ce062015-11-16 13:52:24 -080029class I420BufferPool {
30 public:
Per00983572016-11-04 08:57:26 +010031 I420BufferPool()
perkj536447c2016-11-04 05:33:44 -070032 : I420BufferPool(false) {}
Per00983572016-11-04 08:57:26 +010033 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);
hbos900f9752016-02-05 08:08:34 -080036
Per00983572016-11-04 08:57:26 +010037 // Returns a buffer from the pool. If no suitable buffer exist in the pool
38 // and there are less than |max_number_of_buffers| pending, a buffer is
39 // created. Returns null otherwise.
nissed591e3f2016-05-26 06:49:55 -070040 rtc::scoped_refptr<I420Buffer> CreateBuffer(int width, int height);
kjellander6f8ce062015-11-16 13:52:24 -080041 // Clears buffers_ and detaches the thread checker so that it can be reused
42 // later from another thread.
43 void Release();
44
45 private:
nissed591e3f2016-05-26 06:49:55 -070046 // Explicitly use a RefCountedObject to get access to HasOneRef,
47 // needed by the pool to check exclusive access.
48 using PooledI420Buffer = rtc::RefCountedObject<I420Buffer>;
49
Magnus Jedvert7fd92862016-09-17 11:27:35 +020050 rtc::RaceChecker race_checker_;
nissed591e3f2016-05-26 06:49:55 -070051 std::list<rtc::scoped_refptr<PooledI420Buffer>> buffers_;
hbos900f9752016-02-05 08:08:34 -080052 // If true, newly allocated buffers are zero-initialized. Note that recycled
53 // buffers are not zero'd before reuse. This is required of buffers used by
54 // FFmpeg according to http://crbug.com/390941, which only requires it for the
55 // initial allocation (as shown by FFmpeg's own buffer allocation code). It
56 // has to do with "Use-of-uninitialized-value" on "Linux_msan_chrome".
perkj536447c2016-11-04 05:33:44 -070057 const bool zero_initialize_;
Per00983572016-11-04 08:57:26 +010058 // Max number of buffers this pool can have pending.
perkj536447c2016-11-04 05:33:44 -070059 const size_t max_number_of_buffers_;
kjellander6f8ce062015-11-16 13:52:24 -080060};
61
62} // namespace webrtc
63
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020064#endif // COMMON_VIDEO_INCLUDE_I420_BUFFER_POOL_H_