magjed@webrtc.org | ce6423c | 2015-03-17 11:40:45 | [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 | |
kjellander | cd66254 | 2015-11-16 21:52:24 | [diff] [blame] | 11 | #include "webrtc/common_video/include/i420_buffer_pool.h" |
magjed@webrtc.org | ce6423c | 2015-03-17 11:40:45 | [diff] [blame] | 12 | |
Edward Lemur | 76de83e | 2017-07-06 17:44:34 | [diff] [blame] | 13 | #include "webrtc/rtc_base/checks.h" |
magjed@webrtc.org | ce6423c | 2015-03-17 11:40:45 | [diff] [blame] | 14 | |
magjed@webrtc.org | ce6423c | 2015-03-17 11:40:45 | [diff] [blame] | 15 | namespace webrtc { |
| 16 | |
Per | f17776b | 2016-11-04 07:57:26 | [diff] [blame] | 17 | I420BufferPool::I420BufferPool(bool zero_initialize, |
| 18 | size_t max_number_of_buffers) |
| 19 | : zero_initialize_(zero_initialize), |
| 20 | max_number_of_buffers_(max_number_of_buffers) {} |
pbos@webrtc.org | 213c6ca | 2015-03-20 13:35:56 | [diff] [blame] | 21 | |
| 22 | void I420BufferPool::Release() { |
pbos@webrtc.org | 213c6ca | 2015-03-20 13:35:56 | [diff] [blame] | 23 | buffers_.clear(); |
magjed@webrtc.org | ce6423c | 2015-03-17 11:40:45 | [diff] [blame] | 24 | } |
| 25 | |
nisse | 06eb4b2 | 2016-05-26 13:49:55 | [diff] [blame] | 26 | rtc::scoped_refptr<I420Buffer> I420BufferPool::CreateBuffer(int width, |
| 27 | int height) { |
Magnus Jedvert | 06b9a66 | 2016-09-17 09:27:35 | [diff] [blame] | 28 | RTC_DCHECK_RUNS_SERIALIZED(&race_checker_); |
magjed@webrtc.org | ce6423c | 2015-03-17 11:40:45 | [diff] [blame] | 29 | // Release buffers with wrong resolution. |
| 30 | for (auto it = buffers_.begin(); it != buffers_.end();) { |
| 31 | if ((*it)->width() != width || (*it)->height() != height) |
| 32 | it = buffers_.erase(it); |
| 33 | else |
| 34 | ++it; |
| 35 | } |
| 36 | // Look for a free buffer. |
nisse | 06eb4b2 | 2016-05-26 13:49:55 | [diff] [blame] | 37 | for (const rtc::scoped_refptr<PooledI420Buffer>& buffer : buffers_) { |
| 38 | // If the buffer is in use, the ref count will be >= 2, one from the list we |
| 39 | // are looping over and one from the application. If the ref count is 1, |
| 40 | // then the list we are looping over holds the only reference and it's safe |
| 41 | // to reuse. |
| 42 | if (buffer->HasOneRef()) |
| 43 | return buffer; |
magjed@webrtc.org | ce6423c | 2015-03-17 11:40:45 | [diff] [blame] | 44 | } |
Per | f17776b | 2016-11-04 07:57:26 | [diff] [blame] | 45 | |
| 46 | if (buffers_.size() >= max_number_of_buffers_) |
| 47 | return nullptr; |
magjed@webrtc.org | ce6423c | 2015-03-17 11:40:45 | [diff] [blame] | 48 | // Allocate new buffer. |
nisse | 06eb4b2 | 2016-05-26 13:49:55 | [diff] [blame] | 49 | rtc::scoped_refptr<PooledI420Buffer> buffer = |
| 50 | new PooledI420Buffer(width, height); |
hbos | 7404fbd | 2016-02-05 16:08:34 | [diff] [blame] | 51 | if (zero_initialize_) |
| 52 | buffer->InitializeData(); |
| 53 | buffers_.push_back(buffer); |
nisse | 06eb4b2 | 2016-05-26 13:49:55 | [diff] [blame] | 54 | return buffer; |
magjed@webrtc.org | ce6423c | 2015-03-17 11:40:45 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | } // namespace webrtc |