magjed@webrtc.org | 73d763e | 2015-03-17 11:40:45 +0000 | [diff] [blame] | 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 | |
kjellander | 6f8ce06 | 2015-11-16 13:52:24 -0800 | [diff] [blame] | 11 | #include "webrtc/common_video/include/i420_buffer_pool.h" |
magjed@webrtc.org | 73d763e | 2015-03-17 11:40:45 +0000 | [diff] [blame] | 12 | |
| 13 | #include "webrtc/base/checks.h" |
| 14 | |
| 15 | namespace { |
| 16 | |
| 17 | // One extra indirection is needed to make |HasOneRef| work. |
| 18 | class 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(); } |
nisse | 06176e4 | 2016-04-18 05:34:40 -0700 | [diff] [blame] | 29 | const uint8_t* DataY() const override { return buffer_->DataY(); } |
| 30 | const uint8_t* DataU() const override { return buffer_->DataU(); } |
| 31 | const uint8_t* DataV() const override { return buffer_->DataV(); } |
| 32 | |
nisse | ef8b61e | 2016-04-29 06:09:15 -0700 | [diff] [blame] | 33 | bool IsMutable() override { return HasOneRef(); } |
nisse | 06176e4 | 2016-04-18 05:34:40 -0700 | [diff] [blame] | 34 | // Make the IsMutable() check here instead of in |buffer_|, because the pool |
| 35 | // also has a reference to |buffer_|. |
| 36 | uint8_t* MutableDataY() override { |
Niels Möller | 47fe34c | 2016-04-18 13:02:59 +0200 | [diff] [blame] | 37 | RTC_DCHECK(IsMutable()); |
nisse | 06176e4 | 2016-04-18 05:34:40 -0700 | [diff] [blame] | 38 | return const_cast<uint8_t*>(buffer_->DataY()); |
magjed@webrtc.org | 73d763e | 2015-03-17 11:40:45 +0000 | [diff] [blame] | 39 | } |
nisse | 06176e4 | 2016-04-18 05:34:40 -0700 | [diff] [blame] | 40 | uint8_t* MutableDataU() override { |
| 41 | RTC_DCHECK(IsMutable()); |
| 42 | return const_cast<uint8_t*>(buffer_->DataU()); |
magjed@webrtc.org | 73d763e | 2015-03-17 11:40:45 +0000 | [diff] [blame] | 43 | } |
nisse | 06176e4 | 2016-04-18 05:34:40 -0700 | [diff] [blame] | 44 | uint8_t* MutableDataV() override { |
| 45 | RTC_DCHECK(IsMutable()); |
| 46 | return const_cast<uint8_t*>(buffer_->DataV()); |
| 47 | } |
| 48 | int StrideY() const override { return buffer_->StrideY(); } |
| 49 | int StrideU() const override { return buffer_->StrideU(); } |
| 50 | int StrideV() const override { return buffer_->StrideV(); } |
Per | 9b3f56e | 2015-04-09 13:44:16 +0200 | [diff] [blame] | 51 | void* native_handle() const override { return nullptr; } |
magjed@webrtc.org | 73d763e | 2015-03-17 11:40:45 +0000 | [diff] [blame] | 52 | |
Peter Boström | eb66e80 | 2015-06-05 11:08:03 +0200 | [diff] [blame] | 53 | rtc::scoped_refptr<VideoFrameBuffer> NativeToI420Buffer() override { |
| 54 | RTC_NOTREACHED(); |
| 55 | return nullptr; |
| 56 | } |
| 57 | |
magjed@webrtc.org | 73d763e | 2015-03-17 11:40:45 +0000 | [diff] [blame] | 58 | friend class rtc::RefCountedObject<PooledI420Buffer>; |
| 59 | rtc::scoped_refptr<webrtc::I420Buffer> buffer_; |
| 60 | }; |
| 61 | |
| 62 | } // namespace |
| 63 | |
| 64 | namespace webrtc { |
| 65 | |
hbos | 900f975 | 2016-02-05 08:08:34 -0800 | [diff] [blame] | 66 | I420BufferPool::I420BufferPool(bool zero_initialize) |
| 67 | : zero_initialize_(zero_initialize) { |
pbos@webrtc.org | a3209a2 | 2015-03-20 13:35:56 +0000 | [diff] [blame] | 68 | Release(); |
| 69 | } |
| 70 | |
| 71 | void I420BufferPool::Release() { |
magjed@webrtc.org | 73d763e | 2015-03-17 11:40:45 +0000 | [diff] [blame] | 72 | thread_checker_.DetachFromThread(); |
pbos@webrtc.org | a3209a2 | 2015-03-20 13:35:56 +0000 | [diff] [blame] | 73 | buffers_.clear(); |
magjed@webrtc.org | 73d763e | 2015-03-17 11:40:45 +0000 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | rtc::scoped_refptr<VideoFrameBuffer> I420BufferPool::CreateBuffer(int width, |
| 77 | int height) { |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 78 | RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
magjed@webrtc.org | 73d763e | 2015-03-17 11:40:45 +0000 | [diff] [blame] | 79 | // Release buffers with wrong resolution. |
| 80 | for (auto it = buffers_.begin(); it != buffers_.end();) { |
| 81 | if ((*it)->width() != width || (*it)->height() != height) |
| 82 | it = buffers_.erase(it); |
| 83 | else |
| 84 | ++it; |
| 85 | } |
| 86 | // Look for a free buffer. |
| 87 | for (const rtc::scoped_refptr<I420Buffer>& buffer : buffers_) { |
| 88 | // If the buffer is in use, the ref count will be 2, one from the list we |
| 89 | // are looping over and one from a PooledI420Buffer returned from |
| 90 | // CreateBuffer that has not been released yet. If the ref count is 1 |
| 91 | // (HasOneRef), then the list we are looping over holds the only reference |
| 92 | // and it's safe to reuse. |
Niels Möller | 47fe34c | 2016-04-18 13:02:59 +0200 | [diff] [blame] | 93 | if (buffer->IsMutable()) |
magjed@webrtc.org | 73d763e | 2015-03-17 11:40:45 +0000 | [diff] [blame] | 94 | return new rtc::RefCountedObject<PooledI420Buffer>(buffer); |
| 95 | } |
| 96 | // Allocate new buffer. |
hbos | 900f975 | 2016-02-05 08:08:34 -0800 | [diff] [blame] | 97 | rtc::scoped_refptr<I420Buffer> buffer = new rtc::RefCountedObject<I420Buffer>( |
| 98 | width, height); |
| 99 | if (zero_initialize_) |
| 100 | buffer->InitializeData(); |
| 101 | buffers_.push_back(buffer); |
magjed@webrtc.org | 73d763e | 2015-03-17 11:40:45 +0000 | [diff] [blame] | 102 | return new rtc::RefCountedObject<PooledI420Buffer>(buffers_.back()); |
| 103 | } |
| 104 | |
| 105 | } // namespace webrtc |