blob: bdc0e821c6011b52b66fa832e0f26b6f5f72afd3 [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)
18 : zero_initialize_(zero_initialize) {
nissed591e3f2016-05-26 06:49:55 -070019 thread_checker_.DetachFromThread();
pbos@webrtc.orga3209a22015-03-20 13:35:56 +000020}
21
22void I420BufferPool::Release() {
magjed@webrtc.org73d763e2015-03-17 11:40:45 +000023 thread_checker_.DetachFromThread();
pbos@webrtc.orga3209a22015-03-20 13:35:56 +000024 buffers_.clear();
magjed@webrtc.org73d763e2015-03-17 11:40:45 +000025}
26
nissed591e3f2016-05-26 06:49:55 -070027rtc::scoped_refptr<I420Buffer> I420BufferPool::CreateBuffer(int width,
28 int height) {
henrikg91d6ede2015-09-17 00:24:34 -070029 RTC_DCHECK(thread_checker_.CalledOnValidThread());
magjed@webrtc.org73d763e2015-03-17 11:40:45 +000030 // Release buffers with wrong resolution.
31 for (auto it = buffers_.begin(); it != buffers_.end();) {
32 if ((*it)->width() != width || (*it)->height() != height)
33 it = buffers_.erase(it);
34 else
35 ++it;
36 }
37 // Look for a free buffer.
nissed591e3f2016-05-26 06:49:55 -070038 for (const rtc::scoped_refptr<PooledI420Buffer>& buffer : buffers_) {
39 // If the buffer is in use, the ref count will be >= 2, one from the list we
40 // are looping over and one from the application. If the ref count is 1,
41 // then the list we are looping over holds the only reference and it's safe
42 // to reuse.
43 if (buffer->HasOneRef())
44 return buffer;
magjed@webrtc.org73d763e2015-03-17 11:40:45 +000045 }
46 // Allocate new buffer.
nissed591e3f2016-05-26 06:49:55 -070047 rtc::scoped_refptr<PooledI420Buffer> buffer =
48 new PooledI420Buffer(width, height);
hbos900f9752016-02-05 08:08:34 -080049 if (zero_initialize_)
50 buffer->InitializeData();
51 buffers_.push_back(buffer);
nissed591e3f2016-05-26 06:49:55 -070052 return buffer;
magjed@webrtc.org73d763e2015-03-17 11:40:45 +000053}
54
55} // namespace webrtc