magjed@webrtc.org | 73d763e | 2015-03-17 11:40:45 +0000 | [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 | #include "common_video/include/i420_buffer_pool.h" |
magjed@webrtc.org | 73d763e | 2015-03-17 11:40:45 +0000 | [diff] [blame] | 12 | |
Yves Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [diff] [blame] | 13 | #include <limits> |
| 14 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 15 | #include "rtc_base/checks.h" |
magjed@webrtc.org | 73d763e | 2015-03-17 11:40:45 +0000 | [diff] [blame] | 16 | |
magjed@webrtc.org | 73d763e | 2015-03-17 11:40:45 +0000 | [diff] [blame] | 17 | namespace webrtc { |
| 18 | |
Paulina Hensman | a680a6a | 2018-04-05 11:42:24 +0200 | [diff] [blame] | 19 | I420BufferPool::I420BufferPool() : I420BufferPool(false) {} |
| 20 | I420BufferPool::I420BufferPool(bool zero_initialize) |
| 21 | : I420BufferPool(zero_initialize, std::numeric_limits<size_t>::max()) {} |
Per | 0098357 | 2016-11-04 08:57:26 +0100 | [diff] [blame] | 22 | I420BufferPool::I420BufferPool(bool zero_initialize, |
| 23 | size_t max_number_of_buffers) |
| 24 | : zero_initialize_(zero_initialize), |
| 25 | max_number_of_buffers_(max_number_of_buffers) {} |
Paulina Hensman | a680a6a | 2018-04-05 11:42:24 +0200 | [diff] [blame] | 26 | I420BufferPool::~I420BufferPool() = default; |
pbos@webrtc.org | a3209a2 | 2015-03-20 13:35:56 +0000 | [diff] [blame] | 27 | |
| 28 | void I420BufferPool::Release() { |
pbos@webrtc.org | a3209a2 | 2015-03-20 13:35:56 +0000 | [diff] [blame] | 29 | buffers_.clear(); |
magjed@webrtc.org | 73d763e | 2015-03-17 11:40:45 +0000 | [diff] [blame] | 30 | } |
| 31 | |
Artem Titov | 8525a80 | 2019-12-16 11:41:57 +0100 | [diff] [blame] | 32 | bool I420BufferPool::Resize(size_t max_number_of_buffers) { |
| 33 | RTC_DCHECK_RUNS_SERIALIZED(&race_checker_); |
| 34 | size_t used_buffers_count = 0; |
| 35 | for (const rtc::scoped_refptr<PooledI420Buffer>& buffer : buffers_) { |
| 36 | // If the buffer is in use, the ref count will be >= 2, one from the list we |
| 37 | // are looping over and one from the application. If the ref count is 1, |
| 38 | // then the list we are looping over holds the only reference and it's safe |
| 39 | // to reuse. |
| 40 | if (!buffer->HasOneRef()) { |
| 41 | used_buffers_count++; |
| 42 | } |
| 43 | } |
| 44 | if (used_buffers_count > max_number_of_buffers) { |
| 45 | return false; |
| 46 | } |
| 47 | max_number_of_buffers_ = max_number_of_buffers; |
| 48 | |
| 49 | size_t buffers_to_purge = buffers_.size() - max_number_of_buffers_; |
| 50 | auto iter = buffers_.begin(); |
| 51 | while (iter != buffers_.end() && buffers_to_purge > 0) { |
| 52 | if ((*iter)->HasOneRef()) { |
| 53 | iter = buffers_.erase(iter); |
| 54 | buffers_to_purge--; |
| 55 | } else { |
| 56 | ++iter; |
| 57 | } |
| 58 | } |
| 59 | return true; |
| 60 | } |
| 61 | |
nisse | d591e3f | 2016-05-26 06:49:55 -0700 | [diff] [blame] | 62 | rtc::scoped_refptr<I420Buffer> I420BufferPool::CreateBuffer(int width, |
| 63 | int height) { |
Noah Richards | 408a3c6 | 2019-04-01 10:14:47 -0700 | [diff] [blame] | 64 | // Default stride_y is width, default uv stride is width / 2 (rounding up). |
| 65 | return CreateBuffer(width, height, width, (width + 1) / 2, (width + 1) / 2); |
| 66 | } |
| 67 | |
| 68 | rtc::scoped_refptr<I420Buffer> I420BufferPool::CreateBuffer(int width, |
| 69 | int height, |
| 70 | int stride_y, |
| 71 | int stride_u, |
| 72 | int stride_v) { |
Magnus Jedvert | 7fd9286 | 2016-09-17 11:27:35 +0200 | [diff] [blame] | 73 | RTC_DCHECK_RUNS_SERIALIZED(&race_checker_); |
magjed@webrtc.org | 73d763e | 2015-03-17 11:40:45 +0000 | [diff] [blame] | 74 | // Release buffers with wrong resolution. |
| 75 | for (auto it = buffers_.begin(); it != buffers_.end();) { |
Noah Richards | 408a3c6 | 2019-04-01 10:14:47 -0700 | [diff] [blame] | 76 | const auto& buffer = *it; |
| 77 | if (buffer->width() != width || buffer->height() != height || |
| 78 | buffer->StrideY() != stride_y || buffer->StrideU() != stride_u || |
| 79 | buffer->StrideV() != stride_v) { |
magjed@webrtc.org | 73d763e | 2015-03-17 11:40:45 +0000 | [diff] [blame] | 80 | it = buffers_.erase(it); |
Noah Richards | 408a3c6 | 2019-04-01 10:14:47 -0700 | [diff] [blame] | 81 | } else { |
magjed@webrtc.org | 73d763e | 2015-03-17 11:40:45 +0000 | [diff] [blame] | 82 | ++it; |
Noah Richards | 408a3c6 | 2019-04-01 10:14:47 -0700 | [diff] [blame] | 83 | } |
magjed@webrtc.org | 73d763e | 2015-03-17 11:40:45 +0000 | [diff] [blame] | 84 | } |
| 85 | // Look for a free buffer. |
nisse | d591e3f | 2016-05-26 06:49:55 -0700 | [diff] [blame] | 86 | for (const rtc::scoped_refptr<PooledI420Buffer>& buffer : buffers_) { |
| 87 | // If the buffer is in use, the ref count will be >= 2, one from the list we |
| 88 | // are looping over and one from the application. If the ref count is 1, |
| 89 | // then the list we are looping over holds the only reference and it's safe |
| 90 | // to reuse. |
| 91 | if (buffer->HasOneRef()) |
| 92 | return buffer; |
magjed@webrtc.org | 73d763e | 2015-03-17 11:40:45 +0000 | [diff] [blame] | 93 | } |
Per | 0098357 | 2016-11-04 08:57:26 +0100 | [diff] [blame] | 94 | |
| 95 | if (buffers_.size() >= max_number_of_buffers_) |
| 96 | return nullptr; |
magjed@webrtc.org | 73d763e | 2015-03-17 11:40:45 +0000 | [diff] [blame] | 97 | // Allocate new buffer. |
nisse | d591e3f | 2016-05-26 06:49:55 -0700 | [diff] [blame] | 98 | rtc::scoped_refptr<PooledI420Buffer> buffer = |
Noah Richards | 408a3c6 | 2019-04-01 10:14:47 -0700 | [diff] [blame] | 99 | new PooledI420Buffer(width, height, stride_y, stride_u, stride_v); |
hbos | 900f975 | 2016-02-05 08:08:34 -0800 | [diff] [blame] | 100 | if (zero_initialize_) |
| 101 | buffer->InitializeData(); |
| 102 | buffers_.push_back(buffer); |
nisse | d591e3f | 2016-05-26 06:49:55 -0700 | [diff] [blame] | 103 | return buffer; |
magjed@webrtc.org | 73d763e | 2015-03-17 11:40:45 +0000 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | } // namespace webrtc |