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 11 #include "webrtc/common_video/include/i420_buffer_pool.h" 12 13 #include "webrtc/base/checks.h" 14 15 namespace webrtc { 16 I420BufferPool(bool zero_initialize,size_t max_number_of_buffers)17I420BufferPool::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) {} 21 Release()22void I420BufferPool::Release() { 23 buffers_.clear(); 24 } 25 CreateBuffer(int width,int height)26rtc::scoped_refptr<I420Buffer> I420BufferPool::CreateBuffer(int width, 27 int height) { 28 RTC_DCHECK_RUNS_SERIALIZED(&race_checker_); 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. 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; 44 } 45 46 if (buffers_.size() >= max_number_of_buffers_) 47 return nullptr; 48 // Allocate new buffer. 49 rtc::scoped_refptr<PooledI420Buffer> buffer = 50 new PooledI420Buffer(width, height); 51 if (zero_initialize_) 52 buffer->InitializeData(); 53 buffers_.push_back(buffer); 54 return buffer; 55 } 56 57 } // namespace webrtc 58