| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef MEDIA_BLINK_MULTIBUFFER_H_ | 5 #ifndef MEDIA_BLINK_MULTIBUFFER_H_ |
| 6 #define MEDIA_BLINK_MULTIBUFFER_H_ | 6 #define MEDIA_BLINK_MULTIBUFFER_H_ |
| 7 | 7 |
| 8 #include <stddef.h> | 8 #include <stddef.h> |
| 9 #include <stdint.h> | 9 #include <stdint.h> |
| 10 | 10 |
| 11 #include <limits> | 11 #include <limits> |
| 12 #include <map> | 12 #include <map> |
| 13 #include <set> | 13 #include <set> |
| 14 #include <vector> | 14 #include <vector> |
| 15 | 15 |
| 16 #include "base/callback.h" | 16 #include "base/callback.h" |
| 17 #include "base/containers/hash_tables.h" | 17 #include "base/containers/hash_tables.h" |
| 18 #include "base/hash.h" | 18 #include "base/hash.h" |
| 19 #include "base/macros.h" | 19 #include "base/macros.h" |
| 20 #include "base/memory/ref_counted.h" | 20 #include "base/memory/ref_counted.h" |
| 21 #include "base/memory/scoped_ptr.h" | 21 #include "base/memory/scoped_ptr.h" |
| 22 #include "base/single_thread_task_runner.h" |
| 22 #include "build/build_config.h" | 23 #include "build/build_config.h" |
| 23 #include "media/base/data_buffer.h" | 24 #include "media/base/data_buffer.h" |
| 24 #include "media/blink/interval_map.h" | 25 #include "media/blink/interval_map.h" |
| 25 #include "media/blink/lru.h" | 26 #include "media/blink/lru.h" |
| 26 #include "media/blink/media_blink_export.h" | 27 #include "media/blink/media_blink_export.h" |
| 27 | 28 |
| 28 namespace media { | 29 namespace media { |
| 29 | 30 |
| 30 // Used to identify a block of data in the multibuffer. | 31 // Used to identify a block of data in the multibuffer. |
| 31 // Our blocks are 32kb (1 << 15), so our maximum cacheable file size | 32 // Our blocks are 32kb (1 << 15), so our maximum cacheable file size |
| (...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 123 // It's ok if the effect is not immediate. | 124 // It's ok if the effect is not immediate. |
| 124 virtual void SetDeferred(bool deferred) = 0; | 125 virtual void SetDeferred(bool deferred) = 0; |
| 125 }; | 126 }; |
| 126 | 127 |
| 127 // Multibuffers use a global shared LRU to free memory. | 128 // Multibuffers use a global shared LRU to free memory. |
| 128 // This effectively means that recently used multibuffers can | 129 // This effectively means that recently used multibuffers can |
| 129 // borrow memory from less recently used ones. | 130 // borrow memory from less recently used ones. |
| 130 class MEDIA_BLINK_EXPORT GlobalLRU : public base::RefCounted<GlobalLRU> { | 131 class MEDIA_BLINK_EXPORT GlobalLRU : public base::RefCounted<GlobalLRU> { |
| 131 public: | 132 public: |
| 132 typedef MultiBufferGlobalBlockId GlobalBlockId; | 133 typedef MultiBufferGlobalBlockId GlobalBlockId; |
| 133 GlobalLRU(); | 134 explicit GlobalLRU( |
| 135 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner); |
| 134 | 136 |
| 135 // Free elements from cache if needed and possible. | 137 // Free elements from cache if needed and possible. |
| 136 // Don't free more than |max_to_free| blocks. | 138 // Don't free more than |max_to_free| blocks. |
| 137 // Virtual for testing purposes. | 139 // Virtual for testing purposes. |
| 138 void Prune(int64_t max_to_free); | 140 void Prune(int64_t max_to_free); |
| 139 | 141 |
| 142 // Returns true if there are prunable blocks. |
| 143 bool Pruneable() const; |
| 144 |
| 145 // Incremnt the amount of data used by all multibuffers. |
| 140 void IncrementDataSize(int64_t blocks); | 146 void IncrementDataSize(int64_t blocks); |
| 147 |
| 148 // Each multibuffer is allowed a certain amount of memory, |
| 149 // that memory is registered by calling this function. |
| 150 // The memory is actually shared by all multibuffers. |
| 151 // When the total amount of memory used by all multibuffers |
| 152 // is greater than what has been registered here, we use the |
| 153 // LRU to decide what blocks to free first. |
| 141 void IncrementMaxSize(int64_t blocks); | 154 void IncrementMaxSize(int64_t blocks); |
| 142 | 155 |
| 143 // LRU operations. | 156 // LRU operations. |
| 144 void Use(MultiBuffer* multibuffer, MultiBufferBlockId id); | 157 void Use(MultiBuffer* multibuffer, MultiBufferBlockId id); |
| 145 void Remove(MultiBuffer* multibuffer, MultiBufferBlockId id); | 158 void Remove(MultiBuffer* multibuffer, MultiBufferBlockId id); |
| 146 void Insert(MultiBuffer* multibuffer, MultiBufferBlockId id); | 159 void Insert(MultiBuffer* multibuffer, MultiBufferBlockId id); |
| 147 bool Contains(MultiBuffer* multibuffer, MultiBufferBlockId id); | 160 bool Contains(MultiBuffer* multibuffer, MultiBufferBlockId id); |
| 148 int64_t Size() const; | 161 int64_t Size() const; |
| 149 | 162 |
| 150 private: | 163 private: |
| 151 friend class base::RefCounted<GlobalLRU>; | 164 friend class base::RefCounted<GlobalLRU>; |
| 152 ~GlobalLRU(); | 165 ~GlobalLRU(); |
| 153 | 166 |
| 167 // Schedule background pruning, if needed. |
| 168 void SchedulePrune(); |
| 169 |
| 170 // Perform background pruning. |
| 171 void PruneTask(); |
| 172 |
| 154 // Max number of blocks. | 173 // Max number of blocks. |
| 155 int64_t max_size_; | 174 int64_t max_size_; |
| 156 | 175 |
| 157 // Sum of all multibuffer::data_.size(). | 176 // Sum of all multibuffer::data_.size(). |
| 158 int64_t data_size_; | 177 int64_t data_size_; |
| 159 | 178 |
| 179 // True if there is a call to the background pruning outstanding. |
| 180 bool background_pruning_pending_; |
| 181 |
| 160 // The LRU should contain all blocks which are not pinned from | 182 // The LRU should contain all blocks which are not pinned from |
| 161 // all multibuffers. | 183 // all multibuffers. |
| 162 LRU<GlobalBlockId> lru_; | 184 LRU<GlobalBlockId> lru_; |
| 185 |
| 186 // Where we run our tasks. |
| 187 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; |
| 188 |
| 189 DISALLOW_COPY_AND_ASSIGN(GlobalLRU); |
| 163 }; | 190 }; |
| 164 | 191 |
| 165 MultiBuffer(int32_t block_size_shift, | 192 MultiBuffer(int32_t block_size_shift, |
| 166 const scoped_refptr<GlobalLRU>& global_lru); | 193 const scoped_refptr<GlobalLRU>& global_lru); |
| 167 virtual ~MultiBuffer(); | 194 virtual ~MultiBuffer(); |
| 168 | 195 |
| 169 // Identifies a block in the cache. | 196 // Identifies a block in the cache. |
| 170 // Block numbers can be calculated from byte positions as: | 197 // Block numbers can be calculated from byte positions as: |
| 171 // block_num = byte_pos >> block_size_shift | 198 // block_num = byte_pos >> block_size_shift |
| 172 typedef MultiBufferBlockId BlockId; | 199 typedef MultiBufferBlockId BlockId; |
| (...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 311 // and 0 for all blocks that are not. Used to quickly figure out | 338 // and 0 for all blocks that are not. Used to quickly figure out |
| 312 // ranges of available/unavailable blocks without iterating. | 339 // ranges of available/unavailable blocks without iterating. |
| 313 IntervalMap<BlockId, int32_t> present_; | 340 IntervalMap<BlockId, int32_t> present_; |
| 314 | 341 |
| 315 DISALLOW_COPY_AND_ASSIGN(MultiBuffer); | 342 DISALLOW_COPY_AND_ASSIGN(MultiBuffer); |
| 316 }; | 343 }; |
| 317 | 344 |
| 318 } // namespace media | 345 } // namespace media |
| 319 | 346 |
| 320 #endif // MEDIA_BLINK_MULTIBUFFER_H_ | 347 #endif // MEDIA_BLINK_MULTIBUFFER_H_ |
| OLD | NEW |