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 discard"; |
| 110 for (Container::reverse_iterator it = blocks_.rbegin(); |
| 111 it != blocks_.rend(); |
| 112 ++it) { |
| 113 Block& block = *it; |
| 114 if (block.offset == offset) { |
| 115 DCHECK(block.state != PADDING) |
| 116 << "block that corresponds to offset already discarded"; |
| 117 block.state = PADDING; |
| 118 |
| 119 // Remove block if it were in the back along with any extra padding. |
| 120 while (!blocks_.empty() && blocks_.back().state == PADDING) { |
| 121 free_offset_= blocks_.back().offset; |
| 122 blocks_.pop_back(); |
| 123 } |
| 124 |
| 125 // Remove blocks if it were in the front along with extra padding. |
| 126 while (!blocks_.empty() && blocks_.front().state == PADDING) { |
| 127 blocks_.pop_front(); |
| 128 if (blocks_.empty()) |
| 129 break; |
| 130 |
| 131 in_use_offset_ = blocks_.front().offset; |
| 132 } |
| 133 |
| 134 // In the special case when there are no blocks, we should be reset it. |
| 135 if (blocks_.empty()) { |
| 136 in_use_offset_ = 0; |
| 137 free_offset_ = 0; |
| 138 } |
| 139 return; |
| 140 } |
| 141 } |
| 142 NOTREACHED() << "attempt to discard non-existant block"; |
| 143 } |
| 144 |
106 unsigned int RingBuffer::GetLargestFreeSizeNoWaiting() { | 145 unsigned int RingBuffer::GetLargestFreeSizeNoWaiting() { |
107 unsigned int last_token_read = helper_->last_token_read(); | 146 unsigned int last_token_read = helper_->last_token_read(); |
108 while (!blocks_.empty()) { | 147 while (!blocks_.empty()) { |
109 Block& block = blocks_.front(); | 148 Block& block = blocks_.front(); |
110 if (block.token > last_token_read || block.state == IN_USE) break; | 149 if (block.token > last_token_read || block.state == IN_USE) break; |
111 FreeOldestBlock(); | 150 FreeOldestBlock(); |
112 } | 151 } |
113 if (free_offset_ == in_use_offset_) { | 152 if (free_offset_ == in_use_offset_) { |
114 if (blocks_.empty()) { | 153 if (blocks_.empty()) { |
115 // The entire buffer is free. | 154 // The entire buffer is free. |
116 DCHECK_EQ(free_offset_, 0u); | 155 DCHECK_EQ(free_offset_, 0u); |
117 return size_; | 156 return size_; |
118 } else { | 157 } else { |
119 // The entire buffer is in use. | 158 // The entire buffer is in use. |
120 return 0; | 159 return 0; |
121 } | 160 } |
122 } else if (free_offset_ > in_use_offset_) { | 161 } else if (free_offset_ > in_use_offset_) { |
123 // It's free from free_offset_ to size_ and from 0 to in_use_offset_ | 162 // 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_); | 163 return std::max(size_ - free_offset_, in_use_offset_); |
125 } else { | 164 } else { |
126 // It's free from free_offset_ -> in_use_offset_; | 165 // It's free from free_offset_ -> in_use_offset_; |
127 return in_use_offset_ - free_offset_; | 166 return in_use_offset_ - free_offset_; |
128 } | 167 } |
129 } | 168 } |
130 | 169 |
131 } // namespace gpu | 170 } // namespace gpu |
OLD | NEW |