Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / gpu / command_buffer / client / query_tracker.cc
blobec678a261c7e51e006db0a9d5b7acfbb1a8fcd3f
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_PACK_COMPLETED_CHROMIUM:
137 default:
138 // tell service about id, shared memory and count
139 gl->helper()->BeginQueryEXT(target(), id(), shm_id(), shm_offset());
140 break;
144 void QueryTracker::Query::End(GLES2Implementation* gl) {
145 switch (target()) {
146 case GL_GET_ERROR_QUERY_CHROMIUM: {
147 GLenum error = gl->GetClientSideGLError();
148 if (error == GL_NO_ERROR) {
149 // There was no error so start the query on the service.
150 // it will end immediately.
151 gl->helper()->BeginQueryEXT(target(), id(), shm_id(), shm_offset());
152 } else {
153 // There's an error on the client, no need to bother the service. Just
154 // set the query as completed and return the error.
155 if (error != GL_NO_ERROR) {
156 state_ = kComplete;
157 result_ = error;
158 return;
163 flush_count_ = gl->helper()->flush_generation();
164 gl->helper()->EndQueryEXT(target(), submit_count());
165 MarkAsPending(gl->helper()->InsertToken());
168 void QueryTracker::Query::QueryCounter(GLES2Implementation* gl) {
169 MarkAsActive();
170 flush_count_ = gl->helper()->flush_generation();
171 gl->helper()->QueryCounterEXT(id(), target(), shm_id(), shm_offset(),
172 submit_count());
173 MarkAsPending(gl->helper()->InsertToken());
176 bool QueryTracker::Query::CheckResultsAvailable(
177 CommandBufferHelper* helper) {
178 if (Pending()) {
179 if (base::subtle::Acquire_Load(&info_.sync->process_count) ==
180 submit_count_ ||
181 helper->IsContextLost()) {
182 switch (target()) {
183 case GL_COMMANDS_ISSUED_CHROMIUM:
184 result_ = info_.sync->result;
185 break;
186 case GL_LATENCY_QUERY_CHROMIUM:
187 // Disabled DCHECK because of http://crbug.com/419236.
188 //DCHECK(info_.sync->result >= client_begin_time_us_);
189 result_ = info_.sync->result - client_begin_time_us_;
190 break;
191 case GL_ASYNC_PIXEL_PACK_COMPLETED_CHROMIUM:
192 default:
193 result_ = info_.sync->result;
194 break;
196 state_ = kComplete;
197 } else {
198 if ((helper->flush_generation() - flush_count_ - 1) >= 0x80000000) {
199 helper->Flush();
200 } else {
201 // Insert no-ops so that eventually the GPU process will see more work.
202 helper->Noop(1);
206 return state_ == kComplete;
209 uint64 QueryTracker::Query::GetResult() const {
210 DCHECK(state_ == kComplete || state_ == kUninitialized);
211 return result_;
214 QueryTracker::QueryTracker(MappedMemoryManager* manager)
215 : query_sync_manager_(manager),
216 mapped_memory_(manager),
217 disjoint_count_sync_shm_id_(-1),
218 disjoint_count_sync_shm_offset_(0),
219 disjoint_count_sync_(nullptr),
220 local_disjoint_count_(0) {
223 QueryTracker::~QueryTracker() {
224 while (!queries_.empty()) {
225 delete queries_.begin()->second;
226 queries_.erase(queries_.begin());
228 while (!removed_queries_.empty()) {
229 delete removed_queries_.front();
230 removed_queries_.pop_front();
232 if (disjoint_count_sync_) {
233 mapped_memory_->Free(disjoint_count_sync_);
234 disjoint_count_sync_ = nullptr;
238 QueryTracker::Query* QueryTracker::CreateQuery(GLuint id, GLenum target) {
239 DCHECK_NE(0u, id);
240 FreeCompletedQueries();
241 QuerySyncManager::QueryInfo info;
242 if (!query_sync_manager_.Alloc(&info)) {
243 return nullptr;
245 Query* query = new Query(id, target, info);
246 std::pair<QueryIdMap::iterator, bool> result =
247 queries_.insert(std::make_pair(id, query));
248 DCHECK(result.second);
249 return query;
252 QueryTracker::Query* QueryTracker::GetQuery(GLuint client_id) {
253 QueryIdMap::iterator it = queries_.find(client_id);
254 return it != queries_.end() ? it->second : nullptr;
257 QueryTracker::Query* QueryTracker::GetCurrentQuery(GLenum target) {
258 QueryTargetMap::iterator it = current_queries_.find(target);
259 return it != current_queries_.end() ? it->second : nullptr;
262 void QueryTracker::RemoveQuery(GLuint client_id) {
263 QueryIdMap::iterator it = queries_.find(client_id);
264 if (it != queries_.end()) {
265 Query* query = it->second;
267 // Erase from current targets map if it is the current target.
268 const GLenum target = query->target();
269 QueryTargetMap::iterator target_it = current_queries_.find(target);
270 if (target_it != current_queries_.end() && target_it->second == query) {
271 current_queries_.erase(target_it);
274 // When you delete a query you can't mark its memory as unused until it's
275 // completed.
276 // Note: If you don't do this you won't mess up the service but you will
277 // mess up yourself.
278 removed_queries_.push_back(query);
279 queries_.erase(it);
280 FreeCompletedQueries();
284 void QueryTracker::Shrink() {
285 FreeCompletedQueries();
286 query_sync_manager_.Shrink();
289 void QueryTracker::FreeCompletedQueries() {
290 QueryList::iterator it = removed_queries_.begin();
291 while (it != removed_queries_.end()) {
292 Query* query = *it;
293 if (query->Pending() &&
294 base::subtle::Acquire_Load(&query->info_.sync->process_count) !=
295 query->submit_count()) {
296 ++it;
297 continue;
300 query_sync_manager_.Free(query->info_);
301 it = removed_queries_.erase(it);
302 delete query;
306 bool QueryTracker::BeginQuery(GLuint id, GLenum target,
307 GLES2Implementation* gl) {
308 QueryTracker::Query* query = GetQuery(id);
309 if (!query) {
310 query = CreateQuery(id, target);
311 if (!query) {
312 gl->SetGLError(GL_OUT_OF_MEMORY,
313 "glBeginQueryEXT",
314 "transfer buffer allocation failed");
315 return false;
317 } else if (query->target() != target) {
318 gl->SetGLError(GL_INVALID_OPERATION,
319 "glBeginQueryEXT",
320 "target does not match");
321 return false;
324 current_queries_[query->target()] = query;
325 query->Begin(gl);
326 return true;
329 bool QueryTracker::EndQuery(GLenum target, GLES2Implementation* gl) {
330 QueryTargetMap::iterator target_it = current_queries_.find(target);
331 if (target_it == current_queries_.end()) {
332 gl->SetGLError(GL_INVALID_OPERATION,
333 "glEndQueryEXT", "no active query");
334 return false;
337 target_it->second->End(gl);
338 current_queries_.erase(target_it);
339 return true;
342 bool QueryTracker::QueryCounter(GLuint id, GLenum target,
343 GLES2Implementation* gl) {
344 QueryTracker::Query* query = GetQuery(id);
345 if (!query) {
346 query = CreateQuery(id, target);
347 if (!query) {
348 gl->SetGLError(GL_OUT_OF_MEMORY,
349 "glQueryCounterEXT",
350 "transfer buffer allocation failed");
351 return false;
353 } else if (query->target() != target) {
354 gl->SetGLError(GL_INVALID_OPERATION,
355 "glQueryCounterEXT",
356 "target does not match");
357 return false;
360 query->QueryCounter(gl);
361 return true;
364 bool QueryTracker::SetDisjointSync(GLES2Implementation* gl) {
365 if (!disjoint_count_sync_) {
366 // Allocate memory for disjoint value sync.
367 int32_t shm_id = -1;
368 uint32_t shm_offset;
369 void* mem = mapped_memory_->Alloc(sizeof(*disjoint_count_sync_),
370 &shm_id,
371 &shm_offset);
372 if (mem) {
373 disjoint_count_sync_shm_id_ = shm_id;
374 disjoint_count_sync_shm_offset_ = shm_offset;
375 disjoint_count_sync_ = static_cast<DisjointValueSync*>(mem);
376 disjoint_count_sync_->Reset();
377 gl->helper()->SetDisjointValueSyncCHROMIUM(shm_id, shm_offset);
380 return disjoint_count_sync_ != nullptr;
383 bool QueryTracker::CheckAndResetDisjoint() {
384 if (disjoint_count_sync_) {
385 const uint32_t disjoint_count = disjoint_count_sync_->GetDisjointCount();
386 if (local_disjoint_count_ != disjoint_count) {
387 local_disjoint_count_ = disjoint_count;
388 return true;
391 return false;
394 } // namespace gles2
395 } // namespace gpu