| 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 | #include "common_video/include/i420_buffer_pool.h" |
| 12 |
| 13 | #include <limits> |
| 14 |
| 15 | #include "rtc_base/checks.h" |
| 16 |
| 17 | namespace webrtc { |
| 18 |
| 19 | I420BufferPool::I420BufferPool() : I420BufferPool(false) {} |
| 20 | I420BufferPool::I420BufferPool(bool zero_initialize) |
| 21 | : I420BufferPool(zero_initialize, std::numeric_limits<size_t>::max()) {} |
| 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) {} |
| 26 | I420BufferPool::~I420BufferPool() = default; |
| 27 |
| 28 | void I420BufferPool::Release() { |
| 29 | buffers_.clear(); |
| 30 | } |
| 31 |
| 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 |
| 62 | rtc::scoped_refptr<I420Buffer> I420BufferPool::CreateBuffer(int width, |
| 63 | int height) { |
| 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) { |
| 73 | RTC_DCHECK_RUNS_SERIALIZED(&race_checker_); |
| 74 | // Release buffers with wrong resolution. |
| 75 | for (auto it = buffers_.begin(); it != buffers_.end();) { |
| 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) { |
| 80 | it = buffers_.erase(it); |
| 81 | } else { |
| 82 | ++it; |
| 83 | } |
| 84 | } |
| 85 | // Look for a free buffer. |
| 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; |
| 93 | } |
| 94 |
| 95 | if (buffers_.size() >= max_number_of_buffers_) |
| 96 | return nullptr; |
| 97 | // Allocate new buffer. |
| 98 | rtc::scoped_refptr<PooledI420Buffer> buffer = |
| 99 | new PooledI420Buffer(width, height, stride_y, stride_u, stride_v); |
| 100 | if (zero_initialize_) |
| 101 | buffer->InitializeData(); |
| 102 | buffers_.push_back(buffer); |
| 103 | return buffer; |
| 104 | } |
| 105 |
| 106 | } // namespace webrtc |
| 107 |