blob: 8dab614914e2555b7131e6589e3e1cb6b88f85b1 [file] [log] [blame]
magjed@webrtc.orgce6423c2015-03-17 11:40:451/*
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
kjellandercd662542015-11-16 21:52:2411#include "webrtc/common_video/include/i420_buffer_pool.h"
magjed@webrtc.orgce6423c2015-03-17 11:40:4512
Edward Lemur76de83e2017-07-06 17:44:3413#include "webrtc/rtc_base/checks.h"
magjed@webrtc.orgce6423c2015-03-17 11:40:4514
magjed@webrtc.orgce6423c2015-03-17 11:40:4515namespace webrtc {
16
Perf17776b2016-11-04 07:57:2617I420BufferPool::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.org213c6ca2015-03-20 13:35:5621
22void I420BufferPool::Release() {
pbos@webrtc.org213c6ca2015-03-20 13:35:5623 buffers_.clear();
magjed@webrtc.orgce6423c2015-03-17 11:40:4524}
25
nisse06eb4b22016-05-26 13:49:5526rtc::scoped_refptr<I420Buffer> I420BufferPool::CreateBuffer(int width,
27 int height) {
Magnus Jedvert06b9a662016-09-17 09:27:3528 RTC_DCHECK_RUNS_SERIALIZED(&race_checker_);
magjed@webrtc.orgce6423c2015-03-17 11:40:4529 // 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.
nisse06eb4b22016-05-26 13:49:5537 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.orgce6423c2015-03-17 11:40:4544 }
Perf17776b2016-11-04 07:57:2645
46 if (buffers_.size() >= max_number_of_buffers_)
47 return nullptr;
magjed@webrtc.orgce6423c2015-03-17 11:40:4548 // Allocate new buffer.
nisse06eb4b22016-05-26 13:49:5549 rtc::scoped_refptr<PooledI420Buffer> buffer =
50 new PooledI420Buffer(width, height);
hbos7404fbd2016-02-05 16:08:3451 if (zero_initialize_)
52 buffer->InitializeData();
53 buffers_.push_back(buffer);
nisse06eb4b22016-05-26 13:49:5554 return buffer;
magjed@webrtc.orgce6423c2015-03-17 11:40:4555}
56
57} // namespace webrtc