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"
8 #include <GLES2/gl2ext.h>
9 #include <GLES2/gl2extchromium.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"
23 QuerySyncManager::Bucket::Bucket(QuerySync
* sync_mem
,
25 unsigned int shm_offset
)
28 base_shm_offset(shm_offset
) {
31 QuerySyncManager::Bucket::~Bucket() = default;
33 QuerySyncManager::QuerySyncManager(MappedMemoryManager
* manager
)
34 : mapped_memory_(manager
) {
38 QuerySyncManager::~QuerySyncManager() {
39 while (!buckets_
.empty()) {
40 mapped_memory_
->Free(buckets_
.front()->syncs
);
41 delete buckets_
.front();
46 bool QuerySyncManager::Alloc(QuerySyncManager::QueryInfo
* 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
;
59 unsigned int shm_offset
;
60 void* mem
= mapped_memory_
->Alloc(
61 kSyncsPerBucket
* sizeof(QuerySync
), &shm_id
, &shm_offset
);
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
]) {
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
);
83 bucket
->in_use_queries
[index_in_bucket
] = 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
);
101 mapped_memory_
->Free(bucket
->syncs
);
104 buckets_
.pop_front();
106 buckets_
.swap(new_buckets
);
109 QueryTracker::Query::Query(GLuint id
, GLenum target
,
110 const QuerySyncManager::QueryInfo
& info
)
114 state_(kUninitialized
),
118 client_begin_time_us_(0),
123 void QueryTracker::Query::Begin(GLES2Implementation
* gl
) {
124 // init memory, inc count
128 case GL_GET_ERROR_QUERY_CHROMIUM
:
129 // To nothing on begin for error queries.
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());
136 case GL_ASYNC_PIXEL_PACK_COMPLETED_CHROMIUM
:
138 // tell service about id, shared memory and count
139 gl
->helper()->BeginQueryEXT(target(), id(), shm_id(), shm_offset());
144 void QueryTracker::Query::End(GLES2Implementation
* gl
) {
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());
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
) {
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
) {
170 flush_count_
= gl
->helper()->flush_generation();
171 gl
->helper()->QueryCounterEXT(id(), target(), shm_id(), shm_offset(),
173 MarkAsPending(gl
->helper()->InsertToken());
176 bool QueryTracker::Query::CheckResultsAvailable(
177 CommandBufferHelper
* helper
) {
179 if (base::subtle::Acquire_Load(&info_
.sync
->process_count
) ==
181 helper
->IsContextLost()) {
183 case GL_COMMANDS_ISSUED_CHROMIUM
:
184 result_
= info_
.sync
->result
;
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_
;
191 case GL_ASYNC_PIXEL_PACK_COMPLETED_CHROMIUM
:
193 result_
= info_
.sync
->result
;
198 if ((helper
->flush_generation() - flush_count_
- 1) >= 0x80000000) {
201 // Insert no-ops so that eventually the GPU process will see more work.
206 return state_
== kComplete
;
209 uint64
QueryTracker::Query::GetResult() const {
210 DCHECK(state_
== kComplete
|| state_
== kUninitialized
);
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
) {
240 FreeCompletedQueries();
241 QuerySyncManager::QueryInfo info
;
242 if (!query_sync_manager_
.Alloc(&info
)) {
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
);
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
276 // Note: If you don't do this you won't mess up the service but you will
278 removed_queries_
.push_back(query
);
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()) {
293 if (query
->Pending() &&
294 base::subtle::Acquire_Load(&query
->info_
.sync
->process_count
) !=
295 query
->submit_count()) {
300 query_sync_manager_
.Free(query
->info_
);
301 it
= removed_queries_
.erase(it
);
306 bool QueryTracker::BeginQuery(GLuint id
, GLenum target
,
307 GLES2Implementation
* gl
) {
308 QueryTracker::Query
* query
= GetQuery(id
);
310 query
= CreateQuery(id
, target
);
312 gl
->SetGLError(GL_OUT_OF_MEMORY
,
314 "transfer buffer allocation failed");
317 } else if (query
->target() != target
) {
318 gl
->SetGLError(GL_INVALID_OPERATION
,
320 "target does not match");
324 current_queries_
[query
->target()] = query
;
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");
337 target_it
->second
->End(gl
);
338 current_queries_
.erase(target_it
);
342 bool QueryTracker::QueryCounter(GLuint id
, GLenum target
,
343 GLES2Implementation
* gl
) {
344 QueryTracker::Query
* query
= GetQuery(id
);
346 query
= CreateQuery(id
, target
);
348 gl
->SetGLError(GL_OUT_OF_MEMORY
,
350 "transfer buffer allocation failed");
353 } else if (query
->target() != target
) {
354 gl
->SetGLError(GL_INVALID_OPERATION
,
356 "target does not match");
360 query
->QueryCounter(gl
);
364 bool QueryTracker::SetDisjointSync(GLES2Implementation
* gl
) {
365 if (!disjoint_count_sync_
) {
366 // Allocate memory for disjoint value sync.
369 void* mem
= mapped_memory_
->Alloc(sizeof(*disjoint_count_sync_
),
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
;