Line data Source code
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 :
17 0 : I420BufferPool::I420BufferPool(bool zero_initialize,
18 0 : size_t max_number_of_buffers)
19 : : zero_initialize_(zero_initialize),
20 0 : max_number_of_buffers_(max_number_of_buffers) {}
21 :
22 0 : void I420BufferPool::Release() {
23 0 : buffers_.clear();
24 0 : }
25 :
26 0 : rtc::scoped_refptr<I420Buffer> I420BufferPool::CreateBuffer(int width,
27 : int height) {
28 0 : RTC_DCHECK_RUNS_SERIALIZED(&race_checker_);
29 : // Release buffers with wrong resolution.
30 0 : for (auto it = buffers_.begin(); it != buffers_.end();) {
31 0 : if ((*it)->width() != width || (*it)->height() != height)
32 0 : it = buffers_.erase(it);
33 : else
34 0 : ++it;
35 : }
36 : // Look for a free buffer.
37 0 : 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 0 : if (buffer->HasOneRef())
43 0 : return buffer;
44 : }
45 :
46 0 : if (buffers_.size() >= max_number_of_buffers_)
47 0 : return nullptr;
48 : // Allocate new buffer.
49 : rtc::scoped_refptr<PooledI420Buffer> buffer =
50 0 : new PooledI420Buffer(width, height);
51 0 : if (zero_initialize_)
52 0 : buffer->InitializeData();
53 0 : buffers_.push_back(buffer);
54 0 : return buffer;
55 : }
56 :
57 : } // namespace webrtc
|