blob: 76a53c568827c6f323d3e460bf97f35c18c28e27 [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
kjellander6f8ce062015-11-16 13:52:24 -080011#include "webrtc/common_video/include/i420_buffer_pool.h"
magjed@webrtc.org73d763e2015-03-17 11:40:45 +000012
13#include "webrtc/base/checks.h"
14
magjed@webrtc.org73d763e2015-03-17 11:40:45 +000015namespace webrtc {
16
hbos900f9752016-02-05 08:08:34 -080017I420BufferPool::I420BufferPool(bool zero_initialize)
Magnus Jedvert7fd92862016-09-17 11:27:35 +020018 : zero_initialize_(zero_initialize) {}
pbos@webrtc.orga3209a22015-03-20 13:35:56 +000019
20void I420BufferPool::Release() {
pbos@webrtc.orga3209a22015-03-20 13:35:56 +000021 buffers_.clear();
magjed@webrtc.org73d763e2015-03-17 11:40:45 +000022}
23
nissed591e3f2016-05-26 06:49:55 -070024rtc::scoped_refptr<I420Buffer> I420BufferPool::CreateBuffer(int width,
25 int height) {
Magnus Jedvert7fd92862016-09-17 11:27:35 +020026 RTC_DCHECK_RUNS_SERIALIZED(&race_checker_);
magjed@webrtc.org73d763e2015-03-17 11:40:45 +000027 // 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.
nissed591e3f2016-05-26 06:49:55 -070035 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.org73d763e2015-03-17 11:40:45 +000042 }
43 // Allocate new buffer.
nissed591e3f2016-05-26 06:49:55 -070044 rtc::scoped_refptr<PooledI420Buffer> buffer =
45 new PooledI420Buffer(width, height);
hbos900f9752016-02-05 08:08:34 -080046 if (zero_initialize_)
47 buffer->InitializeData();
48 buffers_.push_back(buffer);
nissed591e3f2016-05-26 06:49:55 -070049 return buffer;
magjed@webrtc.org73d763e2015-03-17 11:40:45 +000050}
51
52} // namespace webrtc