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 | |
kjellander | 6f8ce06 | 2015-11-16 13:52:24 -0800 | [diff] [blame] | 11 | #include "webrtc/common_video/include/i420_buffer_pool.h" |
magjed@webrtc.org | 73d763e | 2015-03-17 11:40:45 +0000 | [diff] [blame] | 12 | |
| 13 | #include "webrtc/base/checks.h" |
| 14 | |
magjed@webrtc.org | 73d763e | 2015-03-17 11:40:45 +0000 | [diff] [blame] | 15 | namespace webrtc { |
| 16 | |
hbos | 900f975 | 2016-02-05 08:08:34 -0800 | [diff] [blame] | 17 | I420BufferPool::I420BufferPool(bool zero_initialize) |
Magnus Jedvert | 7fd9286 | 2016-09-17 11:27:35 +0200 | [diff] [blame^] | 18 | : zero_initialize_(zero_initialize) {} |
pbos@webrtc.org | a3209a2 | 2015-03-20 13:35:56 +0000 | [diff] [blame] | 19 | |
| 20 | void I420BufferPool::Release() { |
pbos@webrtc.org | a3209a2 | 2015-03-20 13:35:56 +0000 | [diff] [blame] | 21 | buffers_.clear(); |
magjed@webrtc.org | 73d763e | 2015-03-17 11:40:45 +0000 | [diff] [blame] | 22 | } |
| 23 | |
nisse | d591e3f | 2016-05-26 06:49:55 -0700 | [diff] [blame] | 24 | rtc::scoped_refptr<I420Buffer> I420BufferPool::CreateBuffer(int width, |
| 25 | int height) { |
Magnus Jedvert | 7fd9286 | 2016-09-17 11:27:35 +0200 | [diff] [blame^] | 26 | RTC_DCHECK_RUNS_SERIALIZED(&race_checker_); |
magjed@webrtc.org | 73d763e | 2015-03-17 11:40:45 +0000 | [diff] [blame] | 27 | // Release buffers with wrong resolution. |
| 28 | for (auto it = buffers_.begin(); it != buffers_.end();) { |
| 29 | if ((*it)->width() != width || (*it)->height() != height) |
| 30 | it = buffers_.erase(it); |
| 31 | else |
| 32 | ++it; |
| 33 | } |
| 34 | // Look for a free buffer. |
nisse | d591e3f | 2016-05-26 06:49:55 -0700 | [diff] [blame] | 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 | return buffer; |
magjed@webrtc.org | 73d763e | 2015-03-17 11:40:45 +0000 | [diff] [blame] | 42 | } |
| 43 | // Allocate new buffer. |
nisse | d591e3f | 2016-05-26 06:49:55 -0700 | [diff] [blame] | 44 | rtc::scoped_refptr<PooledI420Buffer> buffer = |
| 45 | new PooledI420Buffer(width, height); |
hbos | 900f975 | 2016-02-05 08:08:34 -0800 | [diff] [blame] | 46 | if (zero_initialize_) |
| 47 | buffer->InitializeData(); |
| 48 | buffers_.push_back(buffer); |
nisse | d591e3f | 2016-05-26 06:49:55 -0700 | [diff] [blame] | 49 | return buffer; |
magjed@webrtc.org | 73d763e | 2015-03-17 11:40:45 +0000 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | } // namespace webrtc |