blob: d00ab781aa12634ef3745f82c4a8fae178d0ed76 [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
15namespace {
16
17// One extra indirection is needed to make |HasOneRef| work.
18class PooledI420Buffer : public webrtc::VideoFrameBuffer {
19 public:
20 explicit PooledI420Buffer(
21 const rtc::scoped_refptr<webrtc::I420Buffer>& buffer)
22 : buffer_(buffer) {}
23
24 private:
25 ~PooledI420Buffer() override {}
26
27 int width() const override { return buffer_->width(); }
28 int height() const override { return buffer_->height(); }
29 const uint8_t* data(webrtc::PlaneType type) const override {
Magnus Jedvert3318f982015-08-26 16:06:21 +020030 return buffer_->data(type);
magjed@webrtc.org73d763e2015-03-17 11:40:45 +000031 }
Niels Möller47fe34c2016-04-18 13:02:59 +020032 bool IsMutable() { return HasOneRef(); }
Magnus Jedvert3318f982015-08-26 16:06:21 +020033 uint8_t* MutableData(webrtc::PlaneType type) override {
34 // Make the HasOneRef() check here instead of in |buffer_|, because the pool
35 // also has a reference to |buffer_|.
Niels Möller47fe34c2016-04-18 13:02:59 +020036 RTC_DCHECK(IsMutable());
Magnus Jedvert3318f982015-08-26 16:06:21 +020037 return const_cast<uint8_t*>(buffer_->data(type));
magjed@webrtc.org73d763e2015-03-17 11:40:45 +000038 }
39 int stride(webrtc::PlaneType type) const override {
40 return buffer_->stride(type);
41 }
Per9b3f56e2015-04-09 13:44:16 +020042 void* native_handle() const override { return nullptr; }
magjed@webrtc.org73d763e2015-03-17 11:40:45 +000043
Peter Boströmeb66e802015-06-05 11:08:03 +020044 rtc::scoped_refptr<VideoFrameBuffer> NativeToI420Buffer() override {
45 RTC_NOTREACHED();
46 return nullptr;
47 }
48
magjed@webrtc.org73d763e2015-03-17 11:40:45 +000049 friend class rtc::RefCountedObject<PooledI420Buffer>;
50 rtc::scoped_refptr<webrtc::I420Buffer> buffer_;
51};
52
53} // namespace
54
55namespace webrtc {
56
hbos900f9752016-02-05 08:08:34 -080057I420BufferPool::I420BufferPool(bool zero_initialize)
58 : zero_initialize_(zero_initialize) {
pbos@webrtc.orga3209a22015-03-20 13:35:56 +000059 Release();
60}
61
62void I420BufferPool::Release() {
magjed@webrtc.org73d763e2015-03-17 11:40:45 +000063 thread_checker_.DetachFromThread();
pbos@webrtc.orga3209a22015-03-20 13:35:56 +000064 buffers_.clear();
magjed@webrtc.org73d763e2015-03-17 11:40:45 +000065}
66
67rtc::scoped_refptr<VideoFrameBuffer> I420BufferPool::CreateBuffer(int width,
68 int height) {
henrikg91d6ede2015-09-17 00:24:34 -070069 RTC_DCHECK(thread_checker_.CalledOnValidThread());
magjed@webrtc.org73d763e2015-03-17 11:40:45 +000070 // Release buffers with wrong resolution.
71 for (auto it = buffers_.begin(); it != buffers_.end();) {
72 if ((*it)->width() != width || (*it)->height() != height)
73 it = buffers_.erase(it);
74 else
75 ++it;
76 }
77 // Look for a free buffer.
78 for (const rtc::scoped_refptr<I420Buffer>& buffer : buffers_) {
79 // If the buffer is in use, the ref count will be 2, one from the list we
80 // are looping over and one from a PooledI420Buffer returned from
81 // CreateBuffer that has not been released yet. If the ref count is 1
82 // (HasOneRef), then the list we are looping over holds the only reference
83 // and it's safe to reuse.
Niels Möller47fe34c2016-04-18 13:02:59 +020084 if (buffer->IsMutable())
magjed@webrtc.org73d763e2015-03-17 11:40:45 +000085 return new rtc::RefCountedObject<PooledI420Buffer>(buffer);
86 }
87 // Allocate new buffer.
hbos900f9752016-02-05 08:08:34 -080088 rtc::scoped_refptr<I420Buffer> buffer = new rtc::RefCountedObject<I420Buffer>(
89 width, height);
90 if (zero_initialize_)
91 buffer->InitializeData();
92 buffers_.push_back(buffer);
magjed@webrtc.org73d763e2015-03-17 11:40:45 +000093 return new rtc::RefCountedObject<PooledI420Buffer>(buffers_.back());
94}
95
96} // namespace webrtc