blob: e970419ba6de9cd45c1fd69c930fc7b2d6c5f068 [file] [log] [blame]
magjed@webrtc.org73d763e2015-03-17 11:40:45 +00001/*
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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "common_video/include/i420_buffer_pool.h"
magjed@webrtc.org73d763e2015-03-17 11:40:45 +000012
Yves Gerey988cc082018-10-23 12:03:01 +020013#include <limits>
14
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020015#include "rtc_base/checks.h"
magjed@webrtc.org73d763e2015-03-17 11:40:45 +000016
magjed@webrtc.org73d763e2015-03-17 11:40:45 +000017namespace webrtc {
18
Paulina Hensmana680a6a2018-04-05 11:42:24 +020019I420BufferPool::I420BufferPool() : I420BufferPool(false) {}
20I420BufferPool::I420BufferPool(bool zero_initialize)
21 : I420BufferPool(zero_initialize, std::numeric_limits<size_t>::max()) {}
Per00983572016-11-04 08:57:26 +010022I420BufferPool::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) {}
Paulina Hensmana680a6a2018-04-05 11:42:24 +020026I420BufferPool::~I420BufferPool() = default;
pbos@webrtc.orga3209a22015-03-20 13:35:56 +000027
28void I420BufferPool::Release() {
pbos@webrtc.orga3209a22015-03-20 13:35:56 +000029 buffers_.clear();
magjed@webrtc.org73d763e2015-03-17 11:40:45 +000030}
31
nissed591e3f2016-05-26 06:49:55 -070032rtc::scoped_refptr<I420Buffer> I420BufferPool::CreateBuffer(int width,
33 int height) {
Noah Richards408a3c62019-04-01 10:14:47 -070034 // Default stride_y is width, default uv stride is width / 2 (rounding up).
35 return CreateBuffer(width, height, width, (width + 1) / 2, (width + 1) / 2);
36}
37
38rtc::scoped_refptr<I420Buffer> I420BufferPool::CreateBuffer(int width,
39 int height,
40 int stride_y,
41 int stride_u,
42 int stride_v) {
Magnus Jedvert7fd92862016-09-17 11:27:35 +020043 RTC_DCHECK_RUNS_SERIALIZED(&race_checker_);
magjed@webrtc.org73d763e2015-03-17 11:40:45 +000044 // Release buffers with wrong resolution.
45 for (auto it = buffers_.begin(); it != buffers_.end();) {
Noah Richards408a3c62019-04-01 10:14:47 -070046 const auto& buffer = *it;
47 if (buffer->width() != width || buffer->height() != height ||
48 buffer->StrideY() != stride_y || buffer->StrideU() != stride_u ||
49 buffer->StrideV() != stride_v) {
magjed@webrtc.org73d763e2015-03-17 11:40:45 +000050 it = buffers_.erase(it);
Noah Richards408a3c62019-04-01 10:14:47 -070051 } else {
magjed@webrtc.org73d763e2015-03-17 11:40:45 +000052 ++it;
Noah Richards408a3c62019-04-01 10:14:47 -070053 }
magjed@webrtc.org73d763e2015-03-17 11:40:45 +000054 }
55 // Look for a free buffer.
nissed591e3f2016-05-26 06:49:55 -070056 for (const rtc::scoped_refptr<PooledI420Buffer>& buffer : buffers_) {
57 // If the buffer is in use, the ref count will be >= 2, one from the list we
58 // are looping over and one from the application. If the ref count is 1,
59 // then the list we are looping over holds the only reference and it's safe
60 // to reuse.
61 if (buffer->HasOneRef())
62 return buffer;
magjed@webrtc.org73d763e2015-03-17 11:40:45 +000063 }
Per00983572016-11-04 08:57:26 +010064
65 if (buffers_.size() >= max_number_of_buffers_)
66 return nullptr;
magjed@webrtc.org73d763e2015-03-17 11:40:45 +000067 // Allocate new buffer.
nissed591e3f2016-05-26 06:49:55 -070068 rtc::scoped_refptr<PooledI420Buffer> buffer =
Noah Richards408a3c62019-04-01 10:14:47 -070069 new PooledI420Buffer(width, height, stride_y, stride_u, stride_v);
hbos900f9752016-02-05 08:08:34 -080070 if (zero_initialize_)
71 buffer->InitializeData();
72 buffers_.push_back(buffer);
nissed591e3f2016-05-26 06:49:55 -070073 return buffer;
magjed@webrtc.org73d763e2015-03-17 11:40:45 +000074}
75
76} // namespace webrtc