1 // Copyright 2017 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef MEDIA_FILTERS_FRAME_BUFFER_POOL_H_
6 #define MEDIA_FILTERS_FRAME_BUFFER_POOL_H_
12 #include "base/callback_forward.h"
13 #include "base/memory/raw_ptr.h"
14 #include "base/memory/ref_counted.h"
15 #include "base/sequence_checker.h"
16 #include "base/time/default_tick_clock.h"
17 #include "base/trace_event/memory_dump_provider.h"
18 #include "media/base/media_export.h"
21 class SequencedTaskRunner;
26 // FrameBufferPool is a pool of simple CPU memory. This class needs to be ref-
27 // counted since frames created using this memory may live beyond the lifetime
28 // of the caller to this class.
29 class MEDIA_EXPORT FrameBufferPool
30 : public base::RefCountedThreadSafe<FrameBufferPool>,
31 public base::trace_event::MemoryDumpProvider {
35 FrameBufferPool(const FrameBufferPool&) = delete;
36 FrameBufferPool& operator=(const FrameBufferPool&) = delete;
38 // Called when a frame buffer allocation is needed. Upon return |fb_priv| will
39 // be set to a private value used to identify the buffer in future calls and a
40 // buffer of at least |min_size| will be returned.
41 uint8_t* GetFrameBuffer(size_t min_size, void** fb_priv);
43 // Called when a frame buffer allocation is no longer needed.
44 void ReleaseFrameBuffer(void* fb_priv);
46 // Allocates (or reuses) room for an alpha plane on a given frame buffer.
47 // |fb_priv| must be a value previously returned by GetFrameBuffer().
48 uint8_t* AllocateAlphaPlaneForFrameBuffer(size_t min_size, void* fb_priv);
50 // Generates a "no_longer_needed" closure that holds a reference to this pool;
51 // |fb_priv| must be a value previously returned by GetFrameBuffer().
52 base::OnceClosure CreateFrameCallback(void* fb_priv);
54 size_t get_pool_size_for_testing() const { return frame_buffers_.size(); }
56 void set_tick_clock_for_testing(const base::TickClock* tick_clock) {
57 tick_clock_ = tick_clock;
60 void force_allocation_error_for_testing() { force_allocation_error_ = true; }
62 // Called when no more GetFrameBuffer() calls are expected. All unused memory
63 // is released at this time. As frames are returned their memory is released.
64 // This should not be called until anything that might call GetFrameBuffer()
65 // has been destroyed.
68 enum { kStaleFrameLimitSecs = 10 };
71 friend class base::RefCountedThreadSafe<FrameBufferPool>;
72 ~FrameBufferPool() override;
74 // Internal structure holding memory for decoding.
77 // base::MemoryDumpProvider.
78 bool OnMemoryDump(const base::trace_event::MemoryDumpArgs& args,
79 base::trace_event::ProcessMemoryDump* pmd) override;
81 static bool IsUsed(const FrameBuffer* buf);
83 // Drop all entries in |frame_buffers_| that report !IsUsed().
84 void EraseUnusedResources();
86 // Method that gets called when a VideoFrame that references this pool gets
88 void OnVideoFrameDestroyed(
89 scoped_refptr<base::SequencedTaskRunner> task_runner,
90 FrameBuffer* frame_buffer);
92 // Allocated frame buffers.
93 std::vector<std::unique_ptr<FrameBuffer>> frame_buffers_;
95 bool in_shutdown_ = false;
97 bool registered_dump_provider_ = false;
99 bool force_allocation_error_ = false;
101 // |tick_clock_| is always a DefaultTickClock outside of testing.
102 raw_ptr<const base::TickClock> tick_clock_;
104 SEQUENCE_CHECKER(sequence_checker_);
109 #endif // MEDIA_FILTERS_FRAME_BUFFER_POOL_H_