1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50
   51
   52
   53
   54
   55
   56
   57
   58
   59
   60
   61
   62
   63
   64
   65
   66
   67
   68
   69
   70
   71
   72
   73
   74
   75
   76
   77
   78
   79
   80
   81
   82
   83
   84
   85
   86
   87
   88
   89
   90
   91
   92
   93
   94
   95
   96
   97
   98
   99
  100
  101
  102
  103
  104
  105
  106
  107
  108
  109
  110
  111
  112
  113
  114
  115
  116
  117
  118
  119
  120
  121
  122
  123
  124
  125
  126
  127
  128
  129
  130
  131
  132
  133
  134
  135
  136
  137
  138
  139
  140
  141
  142
  143
  144
  145
  146
  147
  148
  149
  150
  151
  152
  153
  154
  155
  156
  157
  158
  159
  160
  161
  162
  163
  164
  165
  166
  167
  168
  169
  170
  171
  172
  173
  174
  175
  176
  177
  178
  179
  180
  181
  182
  183
  184
  185
  186
  187
  188
  189
  190
  191
  192
  193
  194
  195
  196
  197
  198
  199
  200
  201
  202
  203
  204
  205
  206
  207
  208
  209
  210
  211
  212
  213
  214
  215
  216
  217
  218
  219
  220
  221
  222
  223
  224
  225
  226
  227
  228
  229
  230
  231
  232
  233
  234
  235
  236
  237
  238
  239
  240
  241
  242
  243
  244
  245
  246
  247
  248
  249
  250
  251
  252
  253
  254
  255
  256
  257
  258
  259
  260
  261
  262
  263
  264
  265
  266
  267
  268
  269
  270
  271
  272
  273
  274
  275
  276
  277
  278
  279

media / base / frame_buffer_pool.cc [blame]

// Copyright 2017 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "media/base/frame_buffer_pool.h"

#include <vector>

#include "base/check_op.h"
#include "base/containers/heap_array.h"
#include "base/functional/bind.h"
#include "base/functional/callback_helpers.h"
#include "base/location.h"
#include "base/logging.h"
#include "base/process/memory.h"
#include "base/ranges/algorithm.h"
#include "base/strings/stringprintf.h"
#include "base/time/time.h"
#include "base/trace_event/memory_allocator_dump.h"
#include "base/trace_event/memory_dump_manager.h"
#include "base/trace_event/process_memory_dump.h"

namespace media {

using BytesArray = base::HeapArray<uint8_t, base::UncheckedFreeDeleter>;

// Helper class to allow thread safe memory dumping without a task runner.
class FrameBufferPool::FrameBufferMemoryDumpProviderImpl
    : public base::trace_event::MemoryDumpProvider {
 public:
  explicit FrameBufferMemoryDumpProviderImpl(
      scoped_refptr<FrameBufferPool> pool)
      : pool_(std::move(pool)) {
    DCHECK(pool_);
    base::trace_event::MemoryDumpManager::GetInstance()->RegisterDumpProvider(
        this, "FrameBufferPool", nullptr);
  }

  ~FrameBufferMemoryDumpProviderImpl() override = default;

  // base::MemoryDumpProvider implementation.
  bool OnMemoryDump(const base::trace_event::MemoryDumpArgs& args,
                    base::trace_event::ProcessMemoryDump* pmd) override {
    return pool_->OnMemoryDump(args, pmd);
  }

 private:
  scoped_refptr<FrameBufferPool> pool_;
};

struct FrameBufferPool::FrameBuffer {
  // Not using std::vector<uint8_t> as resize() calls take a really long time
  // for large buffers.
  BytesArray data;
  BytesArray alpha_data;
  bool held_by_library = false;
  // Needs to be a counter since a frame buffer might be used multiple times.
  int held_by_frame = 0;
  base::TimeTicks last_use_time;
};

FrameBufferPool::FrameBufferPool(bool zero_initialize_memory)
    : zero_initialize_memory_(zero_initialize_memory),
      tick_clock_(base::DefaultTickClock::GetInstance()) {}

FrameBufferPool::~FrameBufferPool() {
  base::AutoLock lock(lock_);
  DCHECK(in_shutdown_);

  // May be destructed on any thread.
}

base::span<uint8_t> FrameBufferPool::GetFrameBuffer(size_t min_size,
                                                    void** fb_priv) {
  base::AutoLock lock(lock_);
  DCHECK(!in_shutdown_);

  if (!memory_dump_impl_) {
    memory_dump_impl_ =
        std::make_unique<FrameBufferMemoryDumpProviderImpl>(this);
  }

  // Check if a free frame buffer exists.
  auto it = base::ranges::find_if_not(frame_buffers_, &IsUsedLocked,
                                      &std::unique_ptr<FrameBuffer>::get);

  // If not, create one.
  if (it == frame_buffers_.end())
    it = frame_buffers_.insert(it, std::make_unique<FrameBuffer>());

  auto& frame_buffer = *it;

  // Resize the frame buffer if necessary.
  frame_buffer->held_by_library = true;
  if (frame_buffer->data.size() < min_size) {
    // Free the existing |data| first so that the memory can be reused,
    // if possible. Note that the new array is purposely not initialized.
    frame_buffer->data = {};

    uint8_t* data = nullptr;
    if (!force_allocation_error_) {
      bool result = false;
      if (zero_initialize_memory_) {
        result = base::UncheckedCalloc(1u, min_size,
                                       reinterpret_cast<void**>(&data));
      } else {
        result =
            base::UncheckedMalloc(min_size, reinterpret_cast<void**>(&data));
      }

      // Unclear why, but the docs indicate both that `data` will be null on
      // failure, and also that the return value must not be discarded.
      if (!result) {
        data = nullptr;
      }
    }

    if (!data) {
      frame_buffers_.erase(it);
      return {};
    }

    // SAFETY: We have just allocated `min_size` of memory for `data`.
    frame_buffer->data =
        UNSAFE_BUFFERS(BytesArray::FromOwningPointer(data, min_size));
  }

  // Provide the client with a private identifier.
  *fb_priv = frame_buffer.get();
  return frame_buffer->data;
}

void FrameBufferPool::ReleaseFrameBuffer(void* fb_priv) {
  base::AutoLock lock(lock_);
  DCHECK(fb_priv);

  // Note: The library may invoke this method multiple times for the same frame,
  // so we can't DCHECK that |held_by_library| is true.
  auto* frame_buffer = static_cast<FrameBuffer*>(fb_priv);
  frame_buffer->held_by_library = false;

  if (!IsUsedLocked(frame_buffer)) {
    frame_buffer->last_use_time = tick_clock_->NowTicks();
  }
}

base::span<uint8_t> FrameBufferPool::AllocateAlphaPlaneForFrameBuffer(
    size_t min_size,
    void* fb_priv) {
  base::AutoLock lock(lock_);
  DCHECK(fb_priv);

  auto* frame_buffer = static_cast<FrameBuffer*>(fb_priv);
  DCHECK(IsUsedLocked(frame_buffer));
  if (frame_buffer->alpha_data.size() < min_size) {
    // Free the existing |alpha_data| first so that the memory can be reused,
    // if possible. Note that the new array is purposely not initialized.
    frame_buffer->alpha_data = {};
    uint8_t* data = nullptr;
    if (force_allocation_error_ ||
        !base::UncheckedMalloc(min_size, reinterpret_cast<void**>(&data)) ||
        !data) {
      return {};
    }
    // SAFETY: We have just allocated `min_size` of memory for `data`.
    frame_buffer->alpha_data =
        UNSAFE_BUFFERS(BytesArray::FromOwningPointer(data, min_size));
  }
  return frame_buffer->alpha_data;
}

base::OnceClosure FrameBufferPool::CreateFrameCallback(void* fb_priv) {
  base::AutoLock lock(lock_);

  auto* frame_buffer = static_cast<FrameBuffer*>(fb_priv);
  ++frame_buffer->held_by_frame;

  return base::BindOnce(&FrameBufferPool::OnVideoFrameDestroyed, this,
                        frame_buffer);
}

bool FrameBufferPool::OnMemoryDump(
    const base::trace_event::MemoryDumpArgs& args,
    base::trace_event::ProcessMemoryDump* pmd) {
  base::AutoLock lock(lock_);

  if (in_shutdown_) {
    return false;
  }

  base::trace_event::MemoryAllocatorDump* memory_dump =
      pmd->CreateAllocatorDump(
          base::StringPrintf("media/frame_buffers/memory_pool/0x%" PRIXPTR,
                             reinterpret_cast<uintptr_t>(this)));
  base::trace_event::MemoryAllocatorDump* used_memory_dump =
      pmd->CreateAllocatorDump(
          base::StringPrintf("media/frame_buffers/memory_pool/used/0x%" PRIXPTR,
                             reinterpret_cast<uintptr_t>(this)));

  auto* pool_name = base::trace_event::MemoryDumpManager::GetInstance()
                        ->system_allocator_pool_name();
  if (pool_name) {
    pmd->AddSuballocation(memory_dump->guid(), pool_name);
  }
  size_t bytes_used = 0;
  size_t bytes_reserved = 0;
  for (const auto& frame_buffer : frame_buffers_) {
    if (IsUsedLocked(frame_buffer.get())) {
      bytes_used += frame_buffer->data.size() + frame_buffer->alpha_data.size();
    }
    bytes_reserved +=
        frame_buffer->data.size() + frame_buffer->alpha_data.size();
  }

  memory_dump->AddScalar(base::trace_event::MemoryAllocatorDump::kNameSize,
                         base::trace_event::MemoryAllocatorDump::kUnitsBytes,
                         bytes_reserved);
  used_memory_dump->AddScalar(
      base::trace_event::MemoryAllocatorDump::kNameSize,
      base::trace_event::MemoryAllocatorDump::kUnitsBytes, bytes_used);

  return true;
}

void FrameBufferPool::Shutdown() {
  base::AutoLock lock(lock_);
  in_shutdown_ = true;

  // Hand over our dump implementation to the manager for eventual deletion.
  if (memory_dump_impl_) {
    base::trace_event::MemoryDumpManager::GetInstance()
        ->UnregisterAndDeleteDumpProviderSoon(std::move(memory_dump_impl_));
  }

  // Clear any refs held by the library which isn't good about cleaning up after
  // itself. This is safe since the library has already been shutdown by this
  // point.
  for (const auto& frame_buffer : frame_buffers_)
    frame_buffer->held_by_library = false;

  EraseUnusedResourcesLocked();
}

// static
bool FrameBufferPool::IsUsedLocked(const FrameBuffer* buf) {
  // Static, so can't check that `lock_` is acquired.
  return buf->held_by_library || buf->held_by_frame > 0;
}

void FrameBufferPool::EraseUnusedResourcesLocked() {
  lock_.AssertAcquired();
  std::erase_if(frame_buffers_, [](const std::unique_ptr<FrameBuffer>& buf) {
    return !IsUsedLocked(buf.get());
  });
}

void FrameBufferPool::OnVideoFrameDestroyed(FrameBuffer* frame_buffer) {
  base::AutoLock lock(lock_);
  DCHECK_GT(frame_buffer->held_by_frame, 0);
  --frame_buffer->held_by_frame;

  if (in_shutdown_) {
    // If we're in shutdown we can be sure that the library has been destroyed.
    EraseUnusedResourcesLocked();
    return;
  }

  const base::TimeTicks now = tick_clock_->NowTicks();
  if (!IsUsedLocked(frame_buffer)) {
    frame_buffer->last_use_time = now;
  }

  std::erase_if(frame_buffers_, [now](const std::unique_ptr<FrameBuffer>& buf) {
    return !IsUsedLocked(buf.get()) &&
           now - buf->last_use_time > base::Seconds(kStaleFrameLimitSecs);
  });
}

}  // namespace media