| 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 #ifndef WEBRTC_COMMON_VIDEO_INCLUDE_I420_BUFFER_POOL_H_ | 11 #ifndef WEBRTC_COMMON_VIDEO_INCLUDE_I420_BUFFER_POOL_H_ |
| 12 #define WEBRTC_COMMON_VIDEO_INCLUDE_I420_BUFFER_POOL_H_ | 12 #define WEBRTC_COMMON_VIDEO_INCLUDE_I420_BUFFER_POOL_H_ |
| 13 | 13 |
| 14 #include <list> | 14 #include <list> |
| 15 | 15 |
| 16 #include "webrtc/base/thread_checker.h" | 16 #include "webrtc/base/thread_checker.h" |
| 17 #include "webrtc/common_video/include/video_frame_buffer.h" | 17 #include "webrtc/common_video/include/video_frame_buffer.h" |
| 18 | 18 |
| 19 namespace webrtc { | 19 namespace webrtc { |
| 20 | 20 |
| 21 // Simple buffer pool to avoid unnecessary allocations of I420Buffer objects. | 21 // Simple buffer pool to avoid unnecessary allocations of I420Buffer objects. |
| 22 // The pool manages the memory of the I420Buffer returned from CreateBuffer. | 22 // The pool manages the memory of the I420Buffer returned from CreateBuffer. |
| 23 // When the I420Buffer is destructed, the memory is returned to the pool for use | 23 // When the I420Buffer is destructed, the memory is returned to the pool for use |
| 24 // by subsequent calls to CreateBuffer. If the resolution passed to CreateBuffer | 24 // by subsequent calls to CreateBuffer. If the resolution passed to CreateBuffer |
| 25 // changes, old buffers will be purged from the pool. | 25 // changes, old buffers will be purged from the pool. |
| 26 class I420BufferPool { | 26 class I420BufferPool { |
| 27 public: | 27 public: |
| 28 I420BufferPool() : I420BufferPool(false) {} | 28 I420BufferPool() : I420BufferPool(false) {} |
| 29 explicit I420BufferPool(bool zero_initialize); | 29 explicit I420BufferPool(bool zero_initialize); |
| 30 ~I420BufferPool(); |
| 30 | 31 |
| 31 // Returns a buffer from the pool, or creates a new buffer if no suitable | 32 // Returns a buffer from the pool, or creates a new buffer if no suitable |
| 32 // buffer exists in the pool. | 33 // buffer exists in the pool. |
| 33 rtc::scoped_refptr<VideoFrameBuffer> CreateBuffer(int width, int height); | 34 rtc::scoped_refptr<I420Buffer> CreateBuffer(int width, int height); |
| 34 // Clears buffers_ and detaches the thread checker so that it can be reused | 35 // Clears buffers_ and detaches the thread checker so that it can be reused |
| 35 // later from another thread. | 36 // later from another thread. |
| 36 void Release(); | 37 void Release(); |
| 37 | 38 |
| 38 private: | 39 private: |
| 40 class PooledI420Buffer : public webrtc::I420Buffer { |
| 41 public: |
| 42 PooledI420Buffer(int width, int height); |
| 43 bool IsMutable() override; |
| 44 bool IsFree(); |
| 45 void UnManage(); |
| 46 |
| 47 private: |
| 48 virtual int GetRefCount() const = 0; |
| 49 int IsManaged() const; |
| 50 volatile int managed_; |
| 51 }; |
| 52 |
| 39 rtc::ThreadChecker thread_checker_; | 53 rtc::ThreadChecker thread_checker_; |
| 40 std::list<rtc::scoped_refptr<I420Buffer>> buffers_; | 54 std::list<rtc::scoped_refptr<PooledI420Buffer>> buffers_; |
| 41 // If true, newly allocated buffers are zero-initialized. Note that recycled | 55 // If true, newly allocated buffers are zero-initialized. Note that recycled |
| 42 // buffers are not zero'd before reuse. This is required of buffers used by | 56 // buffers are not zero'd before reuse. This is required of buffers used by |
| 43 // FFmpeg according to http://crbug.com/390941, which only requires it for the | 57 // FFmpeg according to http://crbug.com/390941, which only requires it for the |
| 44 // initial allocation (as shown by FFmpeg's own buffer allocation code). It | 58 // initial allocation (as shown by FFmpeg's own buffer allocation code). It |
| 45 // has to do with "Use-of-uninitialized-value" on "Linux_msan_chrome". | 59 // has to do with "Use-of-uninitialized-value" on "Linux_msan_chrome". |
| 46 bool zero_initialize_; | 60 bool zero_initialize_; |
| 47 }; | 61 }; |
| 48 | 62 |
| 49 } // namespace webrtc | 63 } // namespace webrtc |
| 50 | 64 |
| 51 #endif // WEBRTC_COMMON_VIDEO_INCLUDE_I420_BUFFER_POOL_H_ | 65 #endif // WEBRTC_COMMON_VIDEO_INCLUDE_I420_BUFFER_POOL_H_ |
| OLD | NEW |