Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 /* | 1 /* |
| 2 * Copyright (c) 2015 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2015 The WebRTC project authors. All Rights Reserved. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ | 9 */ |
| 10 | 10 |
| 11 #include "webrtc/common_video/include/i420_buffer_pool.h" | 11 #include "webrtc/common_video/include/i420_buffer_pool.h" |
| 12 | 12 |
| 13 #include "webrtc/base/checks.h" | 13 #include "webrtc/base/checks.h" |
| 14 | 14 #include "webrtc/base/atomicops.h" |
| 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(); } | |
| 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 | |
| 33 bool IsMutable() override { return HasOneRef(); } | |
| 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 { | |
| 37 RTC_DCHECK(IsMutable()); | |
| 38 return const_cast<uint8_t*>(buffer_->DataY()); | |
| 39 } | |
| 40 uint8_t* MutableDataU() override { | |
| 41 RTC_DCHECK(IsMutable()); | |
| 42 return const_cast<uint8_t*>(buffer_->DataU()); | |
| 43 } | |
| 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(); } | |
| 51 void* native_handle() const override { return nullptr; } | |
| 52 | |
| 53 rtc::scoped_refptr<VideoFrameBuffer> NativeToI420Buffer() override { | |
| 54 RTC_NOTREACHED(); | |
| 55 return nullptr; | |
| 56 } | |
| 57 | |
| 58 friend class rtc::RefCountedObject<PooledI420Buffer>; | |
| 59 rtc::scoped_refptr<webrtc::I420Buffer> buffer_; | |
| 60 }; | |
| 61 | |
| 62 } // namespace | |
| 63 | 15 |
| 64 namespace webrtc { | 16 namespace webrtc { |
| 65 | 17 |
| 18 I420BufferPool::PooledI420Buffer::PooledI420Buffer(int width, int height) | |
| 19 : I420Buffer(width, height), managed_(1) {} | |
| 20 | |
| 21 bool I420BufferPool::PooledI420Buffer::IsMutable() { | |
| 22 // Order is important. When the buffer becomes unmanaged, naturally | |
| 23 // managed_ is cleared before the pool drops its reference. | |
| 24 // Potentially dangerous race is as follows: | |
| 25 // | |
| 26 // 1. There are two users of the buffer, plus the pool, so refs == 3, | |
| 27 // managed_ == 1. | |
| 28 // | |
| 29 // 2. One thread calls IsMutable. | |
| 30 // | |
| 31 // 3. Some other thread causes the buffer to become unmanaged, by | |
| 32 // calling Release or CreateBuffer. refs is changed to 2, and | |
| 33 // managed_ to 0. | |
| 34 // | |
| 35 // 4. If the IsMutable call picks up managed_ == 1 and refs == 2, | |
| 36 // we have a problem, IsMutable gives a false positive answer. | |
| 37 // | |
| 38 // By reading the reference count first here, we know that if we get | |
| 39 // refs == 2 in this scenario, we will also get is_managed == 0, not | |
| 40 // the previous value of 1. | |
| 41 int refs = GetRefCount(); | |
| 42 int is_managed = IsManaged(); | |
| 43 return refs <= 1 + is_managed; | |
| 44 } | |
| 45 | |
| 46 int I420BufferPool::PooledI420Buffer::IsManaged() const { | |
|
pbos-webrtc
2016/05/19 15:03:22
bool, even if that means you have to do IsManaged(
nisse-webrtc
2016/05/20 07:30:26
Done.
| |
| 47 return rtc::AtomicOps::AcquireLoad(&managed_); | |
|
pbos-webrtc
2016/05/19 15:03:21
== 1
nisse-webrtc
2016/05/20 07:30:25
Done.
| |
| 48 } | |
| 49 | |
| 50 bool I420BufferPool::PooledI420Buffer::IsFree() { | |
| 51 bool is_free = (GetRefCount() == 1); | |
|
pbos-webrtc
2016/05/19 15:03:22
Add a thread_checker_ to IsFree and UnManage.
nisse-webrtc
2016/05/20 07:30:26
At first I thought that it's kind-of redundant. Th
| |
| 52 RTC_DCHECK(IsManaged()); | |
| 53 return is_free; | |
| 54 } | |
| 55 | |
| 56 void I420BufferPool::PooledI420Buffer::UnManage() { | |
| 57 rtc::AtomicOps::ReleaseStore(&managed_, 0); | |
| 58 } | |
| 59 | |
| 66 I420BufferPool::I420BufferPool(bool zero_initialize) | 60 I420BufferPool::I420BufferPool(bool zero_initialize) |
| 67 : zero_initialize_(zero_initialize) { | 61 : zero_initialize_(zero_initialize) { |
| 68 Release(); | 62 Release(); |
| 69 } | 63 } |
| 70 | 64 |
| 71 void I420BufferPool::Release() { | 65 I420BufferPool::~I420BufferPool() { |
| 72 thread_checker_.DetachFromThread(); | 66 Release(); |
| 73 buffers_.clear(); | |
| 74 } | 67 } |
| 75 | 68 |
| 76 rtc::scoped_refptr<VideoFrameBuffer> I420BufferPool::CreateBuffer(int width, | 69 void I420BufferPool::Release() { |
| 77 int height) { | 70 for (auto& it : buffers_) { |
| 71 it->UnManage(); | |
| 72 } | |
| 73 buffers_.clear(); | |
| 74 thread_checker_.DetachFromThread(); | |
| 75 } | |
| 76 | |
| 77 rtc::scoped_refptr<I420Buffer> I420BufferPool::CreateBuffer(int width, | |
| 78 int height) { | |
| 78 RTC_DCHECK(thread_checker_.CalledOnValidThread()); | 79 RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
| 79 // Release buffers with wrong resolution. | 80 // Release buffers with wrong resolution. |
| 80 for (auto it = buffers_.begin(); it != buffers_.end();) { | 81 for (auto it = buffers_.begin(); it != buffers_.end();) { |
| 81 if ((*it)->width() != width || (*it)->height() != height) | 82 if ((*it)->width() != width || (*it)->height() != height) { |
| 83 (*it)->UnManage(); | |
| 82 it = buffers_.erase(it); | 84 it = buffers_.erase(it); |
| 83 else | 85 } else { |
| 84 ++it; | 86 ++it; |
| 87 } | |
| 85 } | 88 } |
| 86 // Look for a free buffer. | 89 // Look for a free buffer. |
| 87 for (const rtc::scoped_refptr<I420Buffer>& buffer : buffers_) { | 90 for (const rtc::scoped_refptr<PooledI420Buffer>& buffer : buffers_) { |
| 88 // If the buffer is in use, the ref count will be 2, one from the list we | 91 // If the buffer is in use, the ref count will be 2, one from the list we |
|
pbos-webrtc
2016/05/19 15:03:21
This comment barely applies to the current impleme
nisse-webrtc
2016/05/20 07:30:25
Removed, and added a shorter comment to IsFree.
| |
| 89 // are looping over and one from a PooledI420Buffer returned from | 92 // are looping over and one from the application. If the ref count is 1 then |
| 90 // CreateBuffer that has not been released yet. If the ref count is 1 | 93 // the list we are looping over holds the only reference and it's safe to |
| 91 // (HasOneRef), then the list we are looping over holds the only reference | 94 // reuse. |
| 92 // and it's safe to reuse. | 95 if (buffer->IsFree()) |
| 93 if (buffer->IsMutable()) | 96 return buffer; |
| 94 return new rtc::RefCountedObject<PooledI420Buffer>(buffer); | |
| 95 } | 97 } |
| 96 // Allocate new buffer. | 98 // Allocate new buffer. |
| 97 rtc::scoped_refptr<I420Buffer> buffer = new rtc::RefCountedObject<I420Buffer>( | 99 rtc::scoped_refptr<PooledI420Buffer> buffer = |
|
pbos-webrtc
2016/05/19 15:03:21
do buffer(new ...) for initialization here.
nisse-webrtc
2016/05/20 07:30:26
Done.
| |
| 98 width, height); | 100 new rtc::RefCountedObject<PooledI420Buffer>(width, height); |
| 99 if (zero_initialize_) | 101 if (zero_initialize_) |
|
pbos-webrtc
2016/05/19 15:03:22
Do we need the zero_initialize_ argument ever?
nisse-webrtc
2016/05/20 07:30:26
Comment says it's ffmpeg-related.
It was added by
| |
| 100 buffer->InitializeData(); | 102 buffer->InitializeData(); |
| 101 buffers_.push_back(buffer); | 103 buffers_.push_back(buffer); |
| 102 return new rtc::RefCountedObject<PooledI420Buffer>(buffers_.back()); | 104 return buffer; |
| 103 } | 105 } |
| 104 | 106 |
| 105 } // namespace webrtc | 107 } // namespace webrtc |
| OLD | NEW |