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, and at about the same time, some | |
| 30 // other thread calls UnManage (application called Release, or | |
| 31 // CreateBuffer, or the pool destructor). refs is changed to 2, | |
| 32 // and managed_ to 0. | |
| 33 // | |
| 34 // 3. Now, the possible states IsMutable could see are | |
| 35 // | |
| 36 // (refs_ == 3, managed_ == 1) Before UnManaged | |
| 37 // (refs_ == 3, managed_ == 0) Transient state | |
| 38 // (refs_ == 2, managed_ == 0) After UnManaged | |
| 39 // | |
| 40 // Since refs_ is read first, and AtomicOps implies that | |
| 41 // appropriate memory barriers are used, it can't see | |
| 42 // | |
| 43 // (refs_ == 2, managed_ == 1) Incorrectly appears mutable. | |
| 44 // | |
| 45 // So IsMutable correctly returns false, regardless of the race outcome. | |
| 46 | |
| 47 // There remains a race with the possibility of a false negative | |
| 48 // answer. Assume we have a single user of the buffer, and have | |
| 49 // IsMutable and UnManaged called at the same time. In this case we | |
| 50 // have a transition | |
| 51 // | |
| 52 // (refs_ == 2, managed_ == 1) Before | |
| 53 // (refs_ == 2, managed_ == 0) Transient state | |
| 54 // (refs_ == 1, managed_ == 0) After | |
| 55 // | |
| 56 // IsMutable is expected to return true, but it may pick up the transient | |
| 57 // state, and return false. However, if multiple threads use the buffer and | |
| 58 // pool in ways making this race possible, the IsMutable caller ought to be | |
| 59 // prepared to handle failure and make its own copy. | |
| 60 // | |
| 61 // I'm afraid this can affect debug builds, correct code using | |
| 62 // | |
| 63 // if (buffer->IsMutable()) { | |
| 64 // ... buffer->MutableDataY() ... | |
| 65 // } | |
| 66 // | |
| 67 // may fail the RTC_DCHECK(IsMutable()); inside MutableDataY, if | |
| 68 // it's unlucky enough that this second call to IsMutable triggers | |
| 69 // the above false-negative race. | |
| 70 | |
| 71 // TODO(nisse): All code modifying pixel data should be fixed to | |
| 72 // either keep an explicitly owned I420Buffer, or write to newly | |
| 73 // allocated buffer, or to a buffer produced by | |
| 74 // I420BufferPool::CreateBuffer. At this point, we can delete the | |
| 75 // MutableDataY(,U,V) methods from the VideoFrameBuffer base class, | |
| 76 // together with IsMutable. The GetRefCount/HasOneRef hack will be | |
| 77 // limited to the I420BufferPool, which can then use I420Buffer | |
| 78 // instances directly. | |
| 79 int refs = GetRefCount(); | |
| 80 return refs <= (IsManaged() ? 2 : 1); | |
|
pbos-webrtc
2016/05/20 13:32:21
I think we can avoid the transient state:
int ref
nisse-webrtc
2016/05/23 07:35:06
That will only reduce the probability of false neg
pbos-webrtc
2016/05/23 07:39:29
Right, I thought the reference was dropped before
| |
| 81 } | |
| 82 | |
| 83 bool I420BufferPool::PooledI420Buffer::IsManaged() const { | |
| 84 return rtc::AtomicOps::AcquireLoad(&managed_) == 1; | |
| 85 } | |
| 86 | |
| 87 bool I420BufferPool::PooledI420Buffer::IsFree() { | |
| 88 // We always have a reference from the pool. If that's the only | |
| 89 // reference, the buffer is free for reuse. | |
| 90 bool is_free = (GetRefCount() == 1); | |
| 91 RTC_DCHECK(IsManaged()); | |
| 92 return is_free; | |
| 93 } | |
| 94 | |
| 95 void I420BufferPool::PooledI420Buffer::UnManage() { | |
| 96 // May run on any thread, when called by the pool destructor. | |
| 97 rtc::AtomicOps::ReleaseStore(&managed_, 0); | |
| 98 } | |
| 99 | |
| 66 I420BufferPool::I420BufferPool(bool zero_initialize) | 100 I420BufferPool::I420BufferPool(bool zero_initialize) |
| 67 : zero_initialize_(zero_initialize) { | 101 : zero_initialize_(zero_initialize) { |
| 102 thread_checker_.DetachFromThread(); | |
| 103 } | |
| 104 | |
| 105 I420BufferPool::~I420BufferPool() { | |
| 68 Release(); | 106 Release(); |
| 69 } | 107 } |
| 70 | 108 |
| 71 void I420BufferPool::Release() { | 109 void I420BufferPool::Release() { |
| 110 for (auto& it : buffers_) { | |
| 111 it->UnManage(); | |
| 112 } | |
| 113 buffers_.clear(); | |
| 72 thread_checker_.DetachFromThread(); | 114 thread_checker_.DetachFromThread(); |
| 73 buffers_.clear(); | |
| 74 } | 115 } |
| 75 | 116 |
| 76 rtc::scoped_refptr<VideoFrameBuffer> I420BufferPool::CreateBuffer(int width, | 117 rtc::scoped_refptr<I420Buffer> I420BufferPool::CreateBuffer(int width, |
| 77 int height) { | 118 int height) { |
| 78 RTC_DCHECK(thread_checker_.CalledOnValidThread()); | 119 RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
| 79 // Release buffers with wrong resolution. | 120 // Release buffers with wrong resolution. |
| 80 for (auto it = buffers_.begin(); it != buffers_.end();) { | 121 for (auto it = buffers_.begin(); it != buffers_.end();) { |
| 81 if ((*it)->width() != width || (*it)->height() != height) | 122 if ((*it)->width() != width || (*it)->height() != height) { |
| 123 (*it)->UnManage(); | |
| 82 it = buffers_.erase(it); | 124 it = buffers_.erase(it); |
| 83 else | 125 } else { |
| 84 ++it; | 126 ++it; |
| 127 } | |
| 85 } | 128 } |
| 86 // Look for a free buffer. | 129 // Look for a free buffer. |
| 87 for (const rtc::scoped_refptr<I420Buffer>& buffer : buffers_) { | 130 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 | 131 if (buffer->IsFree()) |
| 89 // are looping over and one from a PooledI420Buffer returned from | 132 return buffer; |
| 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. | |
| 93 if (buffer->IsMutable()) | |
| 94 return new rtc::RefCountedObject<PooledI420Buffer>(buffer); | |
| 95 } | 133 } |
| 96 // Allocate new buffer. | 134 // Allocate new buffer. |
| 97 rtc::scoped_refptr<I420Buffer> buffer = new rtc::RefCountedObject<I420Buffer>( | 135 rtc::scoped_refptr<PooledI420Buffer> buffer( |
| 98 width, height); | 136 new rtc::RefCountedObject<PooledI420Buffer>(width, height)); |
| 99 if (zero_initialize_) | 137 if (zero_initialize_) |
| 100 buffer->InitializeData(); | 138 buffer->InitializeData(); |
| 101 buffers_.push_back(buffer); | 139 buffers_.push_back(buffer); |
| 102 return new rtc::RefCountedObject<PooledI420Buffer>(buffers_.back()); | 140 return buffer; |
| 103 } | 141 } |
| 104 | 142 |
| 105 } // namespace webrtc | 143 } // namespace webrtc |
| OLD | NEW |