Upload upstream chromium 108.0.5359.1
[platform/framework/web/chromium-efl.git] / media / filters / frame_buffer_pool.h
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.
4
5 #ifndef MEDIA_FILTERS_FRAME_BUFFER_POOL_H_
6 #define MEDIA_FILTERS_FRAME_BUFFER_POOL_H_
7
8 #include <stdint.h>
9
10 #include <vector>
11
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"
19
20 namespace base {
21 class SequencedTaskRunner;
22 }
23
24 namespace media {
25
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 {
32  public:
33   FrameBufferPool();
34
35   FrameBufferPool(const FrameBufferPool&) = delete;
36   FrameBufferPool& operator=(const FrameBufferPool&) = delete;
37
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);
42
43   // Called when a frame buffer allocation is no longer needed.
44   void ReleaseFrameBuffer(void* fb_priv);
45
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);
49
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);
53
54   size_t get_pool_size_for_testing() const { return frame_buffers_.size(); }
55
56   void set_tick_clock_for_testing(const base::TickClock* tick_clock) {
57     tick_clock_ = tick_clock;
58   }
59
60   void force_allocation_error_for_testing() { force_allocation_error_ = true; }
61
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.
66   void Shutdown();
67
68   enum { kStaleFrameLimitSecs = 10 };
69
70  private:
71   friend class base::RefCountedThreadSafe<FrameBufferPool>;
72   ~FrameBufferPool() override;
73
74   // Internal structure holding memory for decoding.
75   struct FrameBuffer;
76
77   // base::MemoryDumpProvider.
78   bool OnMemoryDump(const base::trace_event::MemoryDumpArgs& args,
79                     base::trace_event::ProcessMemoryDump* pmd) override;
80
81   static bool IsUsed(const FrameBuffer* buf);
82
83   // Drop all entries in |frame_buffers_| that report !IsUsed().
84   void EraseUnusedResources();
85
86   // Method that gets called when a VideoFrame that references this pool gets
87   // destroyed.
88   void OnVideoFrameDestroyed(
89       scoped_refptr<base::SequencedTaskRunner> task_runner,
90       FrameBuffer* frame_buffer);
91
92   // Allocated frame buffers.
93   std::vector<std::unique_ptr<FrameBuffer>> frame_buffers_;
94
95   bool in_shutdown_ = false;
96
97   bool registered_dump_provider_ = false;
98
99   bool force_allocation_error_ = false;
100
101   // |tick_clock_| is always a DefaultTickClock outside of testing.
102   raw_ptr<const base::TickClock> tick_clock_;
103
104   SEQUENCE_CHECKER(sequence_checker_);
105 };
106
107 }  // namespace media
108
109 #endif  // MEDIA_FILTERS_FRAME_BUFFER_POOL_H_