Upstreaming browser/ui/uikit_ui_util from iOS.
[chromium-blink-merge.git] / gpu / command_buffer / client / query_tracker.cc
blobc85c6bb66bf573d46d2e27c343529919164b76e3
1 // Copyright (c) 2012 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.
5 #include "gpu/command_buffer/client/query_tracker.h"
7 #include <GLES2/gl2.h>
8 #include <GLES2/gl2ext.h>
9 #include <GLES2/gl2extchromium.h>
11 #include <limits.h>
13 #include "base/atomicops.h"
14 #include "base/numerics/safe_conversions.h"
15 #include "gpu/command_buffer/client/gles2_cmd_helper.h"
16 #include "gpu/command_buffer/client/gles2_implementation.h"
17 #include "gpu/command_buffer/client/mapped_memory.h"
18 #include "gpu/command_buffer/common/time.h"
20 namespace gpu {
21 namespace gles2 {
23 QuerySyncManager::Bucket::Bucket(QuerySync* sync_mem,
24 int32 shm_id,
25 unsigned int shm_offset)
26 : syncs(sync_mem),
27 shm_id(shm_id),
28 base_shm_offset(shm_offset) {
31 QuerySyncManager::Bucket::~Bucket() = default;
33 QuerySyncManager::QuerySyncManager(MappedMemoryManager* manager)
34 : mapped_memory_(manager) {
35 DCHECK(manager);
38 QuerySyncManager::~QuerySyncManager() {
39 while (!buckets_.empty()) {
40 mapped_memory_->Free(buckets_.front()->syncs);
41 delete buckets_.front();
42 buckets_.pop_front();
46 bool QuerySyncManager::Alloc(QuerySyncManager::QueryInfo* info) {
47 DCHECK(info);
48 Bucket* bucket = nullptr;
49 for (Bucket* bucket_candidate : buckets_) {
50 // In C++11 STL this could be replaced with
51 // if (!bucket_candidate->in_use_queries.all()) { ... }
52 if (bucket_candidate->in_use_queries.count() != kSyncsPerBucket) {
53 bucket = bucket_candidate;
54 break;
57 if (!bucket) {
58 int32 shm_id;
59 unsigned int shm_offset;
60 void* mem = mapped_memory_->Alloc(
61 kSyncsPerBucket * sizeof(QuerySync), &shm_id, &shm_offset);
62 if (!mem) {
63 return false;
65 QuerySync* syncs = static_cast<QuerySync*>(mem);
66 bucket = new Bucket(syncs, shm_id, shm_offset);
67 buckets_.push_back(bucket);
70 unsigned short index_in_bucket = 0;
71 for (size_t i = 0; i < kSyncsPerBucket; i++) {
72 if (!bucket->in_use_queries[i]) {
73 index_in_bucket = i;
74 break;
78 uint32 shm_offset =
79 bucket->base_shm_offset + index_in_bucket * sizeof(QuerySync);
80 QuerySync* sync = bucket->syncs + index_in_bucket;
81 *info = QueryInfo(bucket, bucket->shm_id, shm_offset, sync);
82 info->sync->Reset();
83 bucket->in_use_queries[index_in_bucket] = true;
84 return true;
87 void QuerySyncManager::Free(const QuerySyncManager::QueryInfo& info) {
88 DCHECK_NE(info.bucket->in_use_queries.count(), 0u);
89 unsigned short index_in_bucket = info.sync - info.bucket->syncs;
90 DCHECK(info.bucket->in_use_queries[index_in_bucket]);
91 info.bucket->in_use_queries[index_in_bucket] = false;
94 void QuerySyncManager::Shrink() {
95 std::deque<Bucket*> new_buckets;
96 while (!buckets_.empty()) {
97 Bucket* bucket = buckets_.front();
98 if (bucket->in_use_queries.any()) {
99 new_buckets.push_back(bucket);
100 } else {
101 mapped_memory_->Free(bucket->syncs);
102 delete bucket;
104 buckets_.pop_front();
106 buckets_.swap(new_buckets);
109 QueryTracker::Query::Query(GLuint id, GLenum target,
110 const QuerySyncManager::QueryInfo& info)
111 : id_(id),
112 target_(target),
113 info_(info),
114 state_(kUninitialized),
115 submit_count_(0),
116 token_(0),
117 flush_count_(0),
118 client_begin_time_us_(0),
119 result_(0) {
123 void QueryTracker::Query::Begin(GLES2Implementation* gl) {
124 // init memory, inc count
125 MarkAsActive();
127 switch (target()) {
128 case GL_GET_ERROR_QUERY_CHROMIUM:
129 // To nothing on begin for error queries.
130 break;
131 case GL_LATENCY_QUERY_CHROMIUM:
132 client_begin_time_us_ = MicrosecondsSinceOriginOfTime();
133 // tell service about id, shared memory and count
134 gl->helper()->BeginQueryEXT(target(), id(), shm_id(), shm_offset());
135 break;
136 case GL_ASYNC_PIXEL_UNPACK_COMPLETED_CHROMIUM:
137 case GL_ASYNC_PIXEL_PACK_COMPLETED_CHROMIUM:
138 default:
139 // tell service about id, shared memory and count
140 gl->helper()->BeginQueryEXT(target(), id(), shm_id(), shm_offset());
141 break;
145 void QueryTracker::Query::End(GLES2Implementation* gl) {
146 switch (target()) {
147 case GL_GET_ERROR_QUERY_CHROMIUM: {
148 GLenum error = gl->GetClientSideGLError();
149 if (error == GL_NO_ERROR) {
150 // There was no error so start the query on the service.
151 // it will end immediately.
152 gl->helper()->BeginQueryEXT(target(), id(), shm_id(), shm_offset());
153 } else {
154 // There's an error on the client, no need to bother the service. Just
155 // set the query as completed and return the error.
156 if (error != GL_NO_ERROR) {
157 state_ = kComplete;
158 result_ = error;
159 return;
164 flush_count_ = gl->helper()->flush_generation();
165 gl->helper()->EndQueryEXT(target(), submit_count());
166 MarkAsPending(gl->helper()->InsertToken());
169 void QueryTracker::Query::QueryCounter(GLES2Implementation* gl) {
170 MarkAsActive();
171 flush_count_ = gl->helper()->flush_generation();
172 gl->helper()->QueryCounterEXT(target(), id(), shm_id(), shm_offset(),
173 submit_count());
174 MarkAsPending(gl->helper()->InsertToken());
177 bool QueryTracker::Query::CheckResultsAvailable(
178 CommandBufferHelper* helper) {
179 if (Pending()) {
180 if (base::subtle::Acquire_Load(&info_.sync->process_count) ==
181 submit_count_ ||
182 helper->IsContextLost()) {
183 switch (target()) {
184 case GL_COMMANDS_ISSUED_CHROMIUM:
185 result_ = info_.sync->result;
186 break;
187 case GL_LATENCY_QUERY_CHROMIUM:
188 // Disabled DCHECK because of http://crbug.com/419236.
189 //DCHECK(info_.sync->result >= client_begin_time_us_);
190 result_ = info_.sync->result - client_begin_time_us_;
191 break;
192 case GL_ASYNC_PIXEL_UNPACK_COMPLETED_CHROMIUM:
193 case GL_ASYNC_PIXEL_PACK_COMPLETED_CHROMIUM:
194 default:
195 result_ = info_.sync->result;
196 break;
198 state_ = kComplete;
199 } else {
200 if ((helper->flush_generation() - flush_count_ - 1) >= 0x80000000) {
201 helper->Flush();
202 } else {
203 // Insert no-ops so that eventually the GPU process will see more work.
204 helper->Noop(1);
208 return state_ == kComplete;
211 uint64 QueryTracker::Query::GetResult() const {
212 DCHECK(state_ == kComplete || state_ == kUninitialized);
213 return result_;
216 QueryTracker::QueryTracker(MappedMemoryManager* manager)
217 : query_sync_manager_(manager) {
220 QueryTracker::~QueryTracker() {
221 while (!queries_.empty()) {
222 delete queries_.begin()->second;
223 queries_.erase(queries_.begin());
225 while (!removed_queries_.empty()) {
226 delete removed_queries_.front();
227 removed_queries_.pop_front();
231 QueryTracker::Query* QueryTracker::CreateQuery(GLuint id, GLenum target) {
232 DCHECK_NE(0u, id);
233 FreeCompletedQueries();
234 QuerySyncManager::QueryInfo info;
235 if (!query_sync_manager_.Alloc(&info)) {
236 return nullptr;
238 Query* query = new Query(id, target, info);
239 std::pair<QueryIdMap::iterator, bool> result =
240 queries_.insert(std::make_pair(id, query));
241 DCHECK(result.second);
242 return query;
245 QueryTracker::Query* QueryTracker::GetQuery(GLuint client_id) {
246 QueryIdMap::iterator it = queries_.find(client_id);
247 return it != queries_.end() ? it->second : nullptr;
250 QueryTracker::Query* QueryTracker::GetCurrentQuery(GLenum target) {
251 QueryTargetMap::iterator it = current_queries_.find(target);
252 return it != current_queries_.end() ? it->second : nullptr;
255 void QueryTracker::RemoveQuery(GLuint client_id) {
256 QueryIdMap::iterator it = queries_.find(client_id);
257 if (it != queries_.end()) {
258 Query* query = it->second;
260 // Erase from current targets map if it is the current target.
261 const GLenum target = query->target();
262 QueryTargetMap::iterator target_it = current_queries_.find(target);
263 if (target_it != current_queries_.end() && target_it->second == query) {
264 current_queries_.erase(target_it);
267 // When you delete a query you can't mark its memory as unused until it's
268 // completed.
269 // Note: If you don't do this you won't mess up the service but you will
270 // mess up yourself.
271 removed_queries_.push_back(query);
272 queries_.erase(it);
273 FreeCompletedQueries();
277 void QueryTracker::Shrink() {
278 FreeCompletedQueries();
279 query_sync_manager_.Shrink();
282 void QueryTracker::FreeCompletedQueries() {
283 QueryList::iterator it = removed_queries_.begin();
284 while (it != removed_queries_.end()) {
285 Query* query = *it;
286 if (query->Pending() &&
287 base::subtle::Acquire_Load(&query->info_.sync->process_count) !=
288 query->submit_count()) {
289 ++it;
290 continue;
293 query_sync_manager_.Free(query->info_);
294 it = removed_queries_.erase(it);
295 delete query;
299 bool QueryTracker::BeginQuery(GLuint id, GLenum target,
300 GLES2Implementation* gl) {
301 QueryTracker::Query* query = GetQuery(id);
302 if (!query) {
303 query = CreateQuery(id, target);
304 if (!query) {
305 gl->SetGLError(GL_OUT_OF_MEMORY,
306 "glBeginQueryEXT",
307 "transfer buffer allocation failed");
308 return false;
310 } else if (query->target() != target) {
311 gl->SetGLError(GL_INVALID_OPERATION,
312 "glBeginQueryEXT",
313 "target does not match");
314 return false;
317 current_queries_[query->target()] = query;
318 query->Begin(gl);
319 return true;
322 bool QueryTracker::EndQuery(GLenum target, GLES2Implementation* gl) {
323 QueryTargetMap::iterator target_it = current_queries_.find(target);
324 if (target_it == current_queries_.end()) {
325 gl->SetGLError(GL_INVALID_OPERATION,
326 "glEndQueryEXT", "no active query");
327 return false;
330 target_it->second->End(gl);
331 current_queries_.erase(target_it);
332 return true;
335 bool QueryTracker::QueryCounter(GLuint id, GLenum target,
336 GLES2Implementation* gl) {
337 QueryTracker::Query* query = GetQuery(id);
338 if (!query) {
339 query = CreateQuery(id, target);
340 if (!query) {
341 gl->SetGLError(GL_OUT_OF_MEMORY,
342 "glQueryCounterEXT",
343 "transfer buffer allocation failed");
344 return false;
346 } else if (query->target() != target) {
347 gl->SetGLError(GL_INVALID_OPERATION,
348 "glQueryCounterEXT",
349 "target does not match");
350 return false;
353 query->QueryCounter(gl);
354 return true;
357 } // namespace gles2
358 } // namespace gpu