Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 // This file contains the implementation of the RingBuffer class. | 5 // This file contains the implementation of the RingBuffer class. |
| 6 | 6 |
| 7 #include "gpu/command_buffer/client/ring_buffer.h" | 7 #include "gpu/command_buffer/client/ring_buffer.h" |
| 8 | 8 |
| 9 #include <algorithm> | 9 #include <algorithm> |
| 10 | 10 |
| (...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 96 DCHECK(block.state == IN_USE) | 96 DCHECK(block.state == IN_USE) |
| 97 << "block that corresponds to offset already freed"; | 97 << "block that corresponds to offset already freed"; |
| 98 block.token = token; | 98 block.token = token; |
| 99 block.state = FREE_PENDING_TOKEN; | 99 block.state = FREE_PENDING_TOKEN; |
| 100 return; | 100 return; |
| 101 } | 101 } |
| 102 } | 102 } |
| 103 NOTREACHED() << "attempt to free non-existant block"; | 103 NOTREACHED() << "attempt to free non-existant block"; |
| 104 } | 104 } |
| 105 | 105 |
| 106 void RingBuffer::DiscardBlock(void* pointer) { | |
| 107 Offset offset = GetOffset(pointer); | |
| 108 offset -= base_offset_; | |
| 109 DCHECK(!blocks_.empty()) << "no allocations to free"; | |
| 110 for (Container::reverse_iterator it = blocks_.rbegin(); | |
| 111 it != blocks_.rend(); | |
| 112 ++it) { | |
| 113 Block& block = *it; | |
| 114 if (block.offset == offset) { | |
| 115 if (&block == &blocks_.back()) { | |
| 116 // If this block is the last one we can effectively remove it. | |
| 117 if (free_offset_ == 0) { | |
| 118 // Last block had looped around. | |
| 119 free_offset_ = size_; | |
| 120 } | |
| 121 free_offset_ -= block.size; | |
|
piman
2015/06/09 22:55:05
nit: I would just do free_offset_ = offset so that
David Yen
2015/06/10 18:17:09
Done.
| |
| 122 blocks_.pop_back(); | |
| 123 | |
| 124 // We should also pop any PADDING at the back as well, | |
| 125 // but be careful because free_offset_ must be 0 when at the end. | |
| 126 while (!blocks_.empty() && blocks_.back().state == PADDING) { | |
| 127 if (free_offset_ == 0) | |
| 128 free_offset_ = size_; | |
| 129 free_offset_ -= blocks_.back().size; | |
|
piman
2015/06/09 22:55:05
Same here, if you want to pop PADDING ones, free_o
David Yen
2015/06/10 18:17:09
Done.
| |
| 130 blocks_.pop_back(); | |
| 131 } | |
| 132 } else if (&block == &blocks_.front()) { | |
|
piman
2015/06/09 22:55:05
I'm not sure the front part is that useful, becaus
David Yen
2015/06/10 18:17:09
I think it is better to handle it here so that Get
| |
| 133 // If this block is the front, we can remove it from the front. | |
| 134 in_use_offset_ += block.size; | |
| 135 if (in_use_offset_ == size_) | |
| 136 in_use_offset_ = 0; | |
| 137 blocks_.pop_front(); | |
| 138 | |
| 139 // We should also pop any PADDING at the front as well, but be careful | |
| 140 // because in_use_offset_ must be size_ when at the front. | |
| 141 while (!blocks_.empty() && blocks_.front().state == PADDING) { | |
| 142 in_use_offset_ += blocks_.front().size; | |
| 143 if (in_use_offset_ == size_) | |
| 144 in_use_offset_ = 0; | |
| 145 blocks_.pop_front(); | |
| 146 } | |
| 147 } else { | |
| 148 // Mark this as padding and let the FreeOldestBlock() handle offsets. | |
| 149 block.state = PADDING; | |
| 150 } | |
| 151 | |
| 152 // In the special case when there are no blocks, we should be reset it. | |
| 153 if (blocks_.empty()) { | |
| 154 in_use_offset_ = 0; | |
| 155 free_offset_ = 0; | |
| 156 } | |
| 157 return; | |
| 158 } | |
| 159 } | |
| 160 NOTREACHED() << "attempt to free non-existant block"; | |
| 161 } | |
| 162 | |
| 106 unsigned int RingBuffer::GetLargestFreeSizeNoWaiting() { | 163 unsigned int RingBuffer::GetLargestFreeSizeNoWaiting() { |
| 107 unsigned int last_token_read = helper_->last_token_read(); | 164 unsigned int last_token_read = helper_->last_token_read(); |
| 108 while (!blocks_.empty()) { | 165 while (!blocks_.empty()) { |
| 109 Block& block = blocks_.front(); | 166 Block& block = blocks_.front(); |
| 110 if (block.token > last_token_read || block.state == IN_USE) break; | 167 if (block.token > last_token_read || block.state == IN_USE) break; |
| 111 FreeOldestBlock(); | 168 FreeOldestBlock(); |
| 112 } | 169 } |
| 113 if (free_offset_ == in_use_offset_) { | 170 if (free_offset_ == in_use_offset_) { |
| 114 if (blocks_.empty()) { | 171 if (blocks_.empty()) { |
| 115 // The entire buffer is free. | 172 // The entire buffer is free. |
| 116 DCHECK_EQ(free_offset_, 0u); | 173 DCHECK_EQ(free_offset_, 0u); |
| 117 return size_; | 174 return size_; |
| 118 } else { | 175 } else { |
| 119 // The entire buffer is in use. | 176 // The entire buffer is in use. |
| 120 return 0; | 177 return 0; |
| 121 } | 178 } |
| 122 } else if (free_offset_ > in_use_offset_) { | 179 } else if (free_offset_ > in_use_offset_) { |
| 123 // It's free from free_offset_ to size_ and from 0 to in_use_offset_ | 180 // It's free from free_offset_ to size_ and from 0 to in_use_offset_ |
| 124 return std::max(size_ - free_offset_, in_use_offset_); | 181 return std::max(size_ - free_offset_, in_use_offset_); |
| 125 } else { | 182 } else { |
| 126 // It's free from free_offset_ -> in_use_offset_; | 183 // It's free from free_offset_ -> in_use_offset_; |
| 127 return in_use_offset_ - free_offset_; | 184 return in_use_offset_ - free_offset_; |
| 128 } | 185 } |
| 129 } | 186 } |
| 130 | 187 |
| 131 } // namespace gpu | 188 } // namespace gpu |
| OLD | NEW |