You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

76 lines
2.8 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  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. #include "common_video/include/i420_buffer_pool.h"
  11. #include <limits>
  12. #include "rtc_base/checks.h"
  13. namespace webrtc {
  14. I420BufferPool::I420BufferPool() : I420BufferPool(false) {}
  15. I420BufferPool::I420BufferPool(bool zero_initialize)
  16. : I420BufferPool(zero_initialize, std::numeric_limits<size_t>::max()) {}
  17. I420BufferPool::I420BufferPool(bool zero_initialize,
  18. size_t max_number_of_buffers)
  19. : zero_initialize_(zero_initialize),
  20. max_number_of_buffers_(max_number_of_buffers) {}
  21. I420BufferPool::~I420BufferPool() = default;
  22. void I420BufferPool::Release() {
  23. buffers_.clear();
  24. }
  25. rtc::scoped_refptr<I420Buffer> I420BufferPool::CreateBuffer(int width,
  26. int height) {
  27. // Default stride_y is width, default uv stride is width / 2 (rounding up).
  28. return CreateBuffer(width, height, width, (width + 1) / 2, (width + 1) / 2);
  29. }
  30. rtc::scoped_refptr<I420Buffer> I420BufferPool::CreateBuffer(int width,
  31. int height,
  32. int stride_y,
  33. int stride_u,
  34. int stride_v) {
  35. RTC_DCHECK_RUNS_SERIALIZED(&race_checker_);
  36. // Release buffers with wrong resolution.
  37. for (auto it = buffers_.begin(); it != buffers_.end();) {
  38. const auto& buffer = *it;
  39. if (buffer->width() != width || buffer->height() != height ||
  40. buffer->StrideY() != stride_y || buffer->StrideU() != stride_u ||
  41. buffer->StrideV() != stride_v) {
  42. it = buffers_.erase(it);
  43. } else {
  44. ++it;
  45. }
  46. }
  47. // Look for a free buffer.
  48. for (const rtc::scoped_refptr<PooledI420Buffer>& buffer : buffers_) {
  49. // If the buffer is in use, the ref count will be >= 2, one from the list we
  50. // are looping over and one from the application. If the ref count is 1,
  51. // then the list we are looping over holds the only reference and it's safe
  52. // to reuse.
  53. if (buffer->HasOneRef())
  54. return buffer;
  55. }
  56. if (buffers_.size() >= max_number_of_buffers_)
  57. return nullptr;
  58. // Allocate new buffer.
  59. rtc::scoped_refptr<PooledI420Buffer> buffer =
  60. new PooledI420Buffer(width, height, stride_y, stride_u, stride_v);
  61. if (zero_initialize_)
  62. buffer->InitializeData();
  63. buffers_.push_back(buffer);
  64. return buffer;
  65. }
  66. } // namespace webrtc