| 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 #include <utility> | 5 #include <utility> |
| 6 | 6 |
| 7 #include "media/blink/multibuffer.h" | 7 #include "media/blink/multibuffer.h" |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 | 10 |
| (...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 108 } | 108 } |
| 109 | 109 |
| 110 // | 110 // |
| 111 // MultiBuffer | 111 // MultiBuffer |
| 112 // | 112 // |
| 113 MultiBuffer::MultiBuffer(int32_t block_size_shift, | 113 MultiBuffer::MultiBuffer(int32_t block_size_shift, |
| 114 const scoped_refptr<GlobalLRU>& global_lru) | 114 const scoped_refptr<GlobalLRU>& global_lru) |
| 115 : max_size_(0), block_size_shift_(block_size_shift), lru_(global_lru) {} | 115 : max_size_(0), block_size_shift_(block_size_shift), lru_(global_lru) {} |
| 116 | 116 |
| 117 MultiBuffer::~MultiBuffer() { | 117 MultiBuffer::~MultiBuffer() { |
| 118 CHECK(pinned_.empty()); |
| 119 DCHECK_EQ(max_size_, 0); |
| 118 // Remove all blocks from the LRU. | 120 // Remove all blocks from the LRU. |
| 119 for (const auto& i : data_) { | 121 for (const auto& i : data_) { |
| 120 lru_->Remove(this, i.first); | 122 lru_->Remove(this, i.first); |
| 121 } | 123 } |
| 122 lru_->IncrementDataSize(-static_cast<int64_t>(data_.size())); | 124 lru_->IncrementDataSize(-static_cast<int64_t>(data_.size())); |
| 123 lru_->IncrementMaxSize(-max_size_); | 125 lru_->IncrementMaxSize(-max_size_); |
| 124 } | 126 } |
| 125 | 127 |
| 126 void MultiBuffer::AddReader(const BlockId& pos, Reader* reader) { | 128 void MultiBuffer::AddReader(const BlockId& pos, Reader* reader) { |
| 127 std::set<Reader*>* set_of_readers = &readers_[pos]; | 129 std::set<Reader*>* set_of_readers = &readers_[pos]; |
| (...skipping 244 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 372 break; | 374 break; |
| 373 case ProviderStateDead: | 375 case ProviderStateDead: |
| 374 RemoveProvider(provider_tmp); | 376 RemoveProvider(provider_tmp); |
| 375 break; | 377 break; |
| 376 } | 378 } |
| 377 } | 379 } |
| 378 } | 380 } |
| 379 | 381 |
| 380 void MultiBuffer::MergeFrom(MultiBuffer* other) { | 382 void MultiBuffer::MergeFrom(MultiBuffer* other) { |
| 381 // Import data and update LRU. | 383 // Import data and update LRU. |
| 384 size_t data_size = data_.size(); |
| 382 for (const auto& data : other->data_) { | 385 for (const auto& data : other->data_) { |
| 383 if (data_.insert(std::make_pair(data.first, data.second)).second) { | 386 if (data_.insert(std::make_pair(data.first, data.second)).second) { |
| 384 if (!pinned_[data.first]) { | 387 if (!pinned_[data.first]) { |
| 385 lru_->Insert(this, data.first); | 388 lru_->Insert(this, data.first); |
| 386 } | 389 } |
| 387 } | 390 } |
| 388 } | 391 } |
| 392 lru_->IncrementDataSize(static_cast<int64_t>(data_.size() - data_size)); |
| 389 // Update present_ | 393 // Update present_ |
| 390 for (const auto& r : other->present_) { | 394 for (const auto& r : other->present_) { |
| 391 if (r.second) { | 395 if (r.second) { |
| 392 present_.SetInterval(r.first.begin, r.first.end, 1); | 396 present_.SetInterval(r.first.begin, r.first.end, 1); |
| 393 } | 397 } |
| 394 } | 398 } |
| 395 // Notify existing readers. | 399 // Notify existing readers. |
| 396 auto last = present_.begin(); | 400 auto last = present_.begin(); |
| 397 for (const auto& r : other->present_) { | 401 for (const auto& r : other->present_) { |
| 398 if (r.second) { | 402 if (r.second) { |
| (...skipping 18 matching lines...) Expand all Loading... |
| 417 // transitioned in or out of the unlocked state. If so, we iterate over | 421 // transitioned in or out of the unlocked state. If so, we iterate over |
| 418 // all buffers in that range and add/remove them from the LRU as approperiate. | 422 // all buffers in that range and add/remove them from the LRU as approperiate. |
| 419 // We iterate *backwards* through the ranges, with the idea that data in a | 423 // We iterate *backwards* through the ranges, with the idea that data in a |
| 420 // continous range should be freed from the end first. | 424 // continous range should be freed from the end first. |
| 421 | 425 |
| 422 if (data_.empty()) | 426 if (data_.empty()) |
| 423 return; | 427 return; |
| 424 | 428 |
| 425 auto range = pinned_.find(to - 1); | 429 auto range = pinned_.find(to - 1); |
| 426 while (1) { | 430 while (1) { |
| 431 DCHECK_GE(range.value(), 0); |
| 427 if (range.value() == 0 || range.value() == how_much) { | 432 if (range.value() == 0 || range.value() == how_much) { |
| 428 bool pin = range.value() == how_much; | 433 bool pin = range.value() == how_much; |
| 429 Interval<BlockId> transition_range = | 434 Interval<BlockId> transition_range = |
| 430 modified_range.Intersect(range.interval()); | 435 modified_range.Intersect(range.interval()); |
| 431 if (transition_range.Empty()) | 436 if (transition_range.Empty()) |
| 432 break; | 437 break; |
| 433 | 438 |
| 434 // For each range that has transitioned to/from a pinned state, | 439 // For each range that has transitioned to/from a pinned state, |
| 435 // we iterate over the corresponding ranges in |present_| to find | 440 // we iterate over the corresponding ranges in |present_| to find |
| 436 // the blocks that are actually in the multibuffer. | 441 // the blocks that are actually in the multibuffer. |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 471 } | 476 } |
| 472 | 477 |
| 473 void MultiBuffer::IncrementMaxSize(int32_t size) { | 478 void MultiBuffer::IncrementMaxSize(int32_t size) { |
| 474 max_size_ += size; | 479 max_size_ += size; |
| 475 lru_->IncrementMaxSize(size); | 480 lru_->IncrementMaxSize(size); |
| 476 DCHECK_GE(max_size_, 0); | 481 DCHECK_GE(max_size_, 0); |
| 477 // Pruning only happens when blocks are added. | 482 // Pruning only happens when blocks are added. |
| 478 } | 483 } |
| 479 | 484 |
| 480 } // namespace media | 485 } // namespace media |
| OLD | NEW |