OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "gpu/command_buffer/service/path_manager.h" |
| 6 #include "base/logging.h" |
| 7 #include "gpu/command_buffer/common/gles2_cmd_utils.h" |
| 8 #include "ui/gl/gl_bindings.h" |
| 9 |
| 10 namespace { |
| 11 void CallDeletePaths(GLuint first_id, GLuint range) { |
| 12 while (range > 0) { |
| 13 GLsizei irange; |
| 14 if (range > static_cast<GLuint>(std::numeric_limits<GLsizei>::max())) |
| 15 irange = std::numeric_limits<GLsizei>::max(); |
| 16 else |
| 17 irange = static_cast<GLsizei>(range); |
| 18 |
| 19 glDeletePathsNV(first_id, irange); |
| 20 range -= irange; |
| 21 first_id += irange; |
| 22 } |
| 23 } |
| 24 |
| 25 } // anonymous namespace |
| 26 |
| 27 namespace gpu { |
| 28 namespace gles2 { |
| 29 |
| 30 PathManager::PathManager() { |
| 31 } |
| 32 |
| 33 PathManager::~PathManager() { |
| 34 DCHECK(path_map_.empty()); |
| 35 } |
| 36 |
| 37 void PathManager::Destroy(bool have_context) { |
| 38 if (have_context) { |
| 39 for (PathRangeMap::const_iterator it = path_map_.begin(); |
| 40 it != path_map_.end(); ++it) |
| 41 CallDeletePaths(FirstServiceId(it), RangeSize(it)); |
| 42 } |
| 43 path_map_.clear(); |
| 44 } |
| 45 |
| 46 void PathManager::CreatePathRange(GLuint first_client_id, |
| 47 GLuint last_client_id, |
| 48 GLuint first_service_id) { |
| 49 DCHECK(first_service_id > 0u); |
| 50 DCHECK(first_client_id > 0u); |
| 51 DCHECK(!HasPathsInRange(first_client_id, last_client_id)); |
| 52 DCHECK(CheckConsistency()); |
| 53 |
| 54 PathRangeMap::const_iterator crange = |
| 55 GetContainingRange(first_client_id - 1u); |
| 56 // The GetContaining.. is not templated for the sake of code |
| 57 // clarity, so convert the iterator. This should be relatively |
| 58 // fast. |
| 59 PathRangeMap::iterator range = path_map_.erase(crange, crange); |
| 60 |
| 61 if (range != path_map_.end() && |
| 62 LastServiceId(range) == first_service_id - 1u) { |
| 63 LastClientId(range) = last_client_id; |
| 64 } else { |
| 65 auto result = path_map_.insert( |
| 66 std::make_pair(first_client_id, |
| 67 PathRangeDescription(last_client_id, first_service_id))); |
| 68 DCHECK(result.second); |
| 69 range = result.first; |
| 70 } |
| 71 |
| 72 PathRangeMap::iterator next_range = range; |
| 73 ++next_range; |
| 74 if (next_range != path_map_.end()) { |
| 75 if (LastClientId(range) == FirstClientId(next_range) - 1u && |
| 76 LastServiceId(range) == FirstServiceId(next_range) - 1u) { |
| 77 LastClientId(range) = LastClientId(next_range); |
| 78 path_map_.erase(next_range); |
| 79 } |
| 80 } |
| 81 DCHECK(CheckConsistency()); |
| 82 } |
| 83 |
| 84 bool PathManager::HasPathsInRange(GLuint first_client_id, |
| 85 GLuint last_client_id) const { |
| 86 PathRangeMap::const_iterator it = GetContainingOrNextRange(first_client_id); |
| 87 if (it == path_map_.end()) |
| 88 return false; |
| 89 |
| 90 return FirstClientId(it) <= last_client_id; |
| 91 } |
| 92 |
| 93 bool PathManager::GetPath(GLuint client_id, GLuint* service_id) const { |
| 94 PathRangeMap::const_iterator range = GetContainingRange(client_id); |
| 95 if (range == path_map_.end()) |
| 96 return false; |
| 97 |
| 98 *service_id = FirstServiceId(range) + client_id - FirstClientId(range); |
| 99 return true; |
| 100 } |
| 101 |
| 102 void PathManager::RemovePaths(GLuint first_client_id, GLuint last_client_id) { |
| 103 DCHECK(CheckConsistency()); |
| 104 PathRangeMap::const_iterator cit = GetContainingOrNextRange(first_client_id); |
| 105 PathRangeMap::iterator it = path_map_.erase(cit, cit); |
| 106 |
| 107 while (it != path_map_.end() && FirstClientId(it) <= last_client_id) { |
| 108 GLuint delete_first_client_id = |
| 109 std::max(first_client_id, FirstClientId(it)); |
| 110 GLuint delete_last_client_id = std::min(last_client_id, LastClientId(it)); |
| 111 GLuint delete_first_service_id = |
| 112 FirstServiceId(it) + delete_first_client_id - FirstClientId(it); |
| 113 GLuint delete_range = delete_last_client_id - delete_first_client_id + 1u; |
| 114 |
| 115 CallDeletePaths(delete_first_service_id, delete_range); |
| 116 |
| 117 PathRangeMap::iterator current = it; |
| 118 ++it; |
| 119 |
| 120 GLuint current_last_client_id = LastClientId(current); |
| 121 |
| 122 if (FirstClientId(current) < delete_first_client_id) |
| 123 LastClientId(current) = delete_first_client_id - 1u; |
| 124 else |
| 125 path_map_.erase(current); |
| 126 |
| 127 if (current_last_client_id > delete_last_client_id) { |
| 128 path_map_.insert(std::make_pair( |
| 129 delete_last_client_id + 1u, |
| 130 PathRangeDescription(current_last_client_id, |
| 131 delete_first_service_id + delete_range))); |
| 132 DCHECK(delete_last_client_id == last_client_id); |
| 133 // This is necessarily the last range to check. Return early due to |
| 134 // consistency. Iterator increment would skip the inserted range. The |
| 135 // algorithm would work ok, but it looks weird. |
| 136 DCHECK(CheckConsistency()); |
| 137 return; |
| 138 } |
| 139 } |
| 140 DCHECK(CheckConsistency()); |
| 141 } |
| 142 |
| 143 PathManager::PathRangeMap::const_iterator PathManager::GetContainingRange( |
| 144 GLuint client_id) const { |
| 145 PathRangeMap::const_iterator it = path_map_.lower_bound(client_id); |
| 146 if (it != path_map_.end() && FirstClientId(it) == client_id) |
| 147 return it; |
| 148 if (it != path_map_.begin()) { |
| 149 --it; |
| 150 if (LastClientId(it) >= client_id) |
| 151 return it; |
| 152 } |
| 153 return path_map_.end(); |
| 154 } |
| 155 |
| 156 PathManager::PathRangeMap::const_iterator PathManager::GetContainingOrNextRange( |
| 157 GLuint client_id) const { |
| 158 PathRangeMap::const_iterator it = path_map_.lower_bound(client_id); |
| 159 if (it != path_map_.end() && FirstClientId(it) == client_id) { |
| 160 return it; |
| 161 } |
| 162 if (it != path_map_.begin()) { |
| 163 --it; |
| 164 if (LastClientId(it) >= client_id) |
| 165 return it; |
| 166 ++it; |
| 167 } |
| 168 return it; |
| 169 } |
| 170 |
| 171 bool PathManager::CheckConsistency() { |
| 172 GLuint prev_first_client_id = 0u; |
| 173 GLuint prev_last_client_id = 0u; |
| 174 GLuint prev_first_service_id = 0u; |
| 175 for (PathRangeMap::iterator range = path_map_.begin(); |
| 176 range != path_map_.end(); ++range) { |
| 177 // Code relies on ranges not starting at 0. Also, the above initialization |
| 178 // is only |
| 179 // correct then. |
| 180 if (FirstClientId(range) == 0u || FirstServiceId(range) == 0u) |
| 181 return false; |
| 182 |
| 183 // Each range is consistent. |
| 184 if (FirstClientId(range) > LastClientId(range)) |
| 185 return false; |
| 186 |
| 187 if (prev_first_client_id != 0u) { |
| 188 // No overlapping ranges. (The iteration is sorted). |
| 189 if (FirstClientId(range) <= prev_last_client_id) |
| 190 return false; |
| 191 |
| 192 // No mergeable ranges. |
| 193 bool is_mergeable_client = |
| 194 FirstClientId(range) - 1u == prev_last_client_id; |
| 195 bool is_mergeable_service = |
| 196 FirstServiceId(range) - 1u == prev_first_service_id; |
| 197 if (is_mergeable_client && is_mergeable_service) |
| 198 return false; |
| 199 } |
| 200 prev_first_client_id = FirstClientId(range); |
| 201 prev_last_client_id = LastClientId(range); |
| 202 prev_first_service_id = FirstServiceId(range); |
| 203 } |
| 204 return true; |
| 205 } |
| 206 |
| 207 } // namespace gles2 |
| 208 } // namespace gpu |
OLD | NEW |