Reland "Non-SFI mode: Switch to newlib. (patchset #4 id:60001 of https://codereview...
[chromium-blink-merge.git] / gpu / command_buffer / client / query_tracker.cc
blobddb559a2a3ce87f2f0c93c29515eaa75866e6644
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 bool QueryTracker::Query::CheckResultsAvailable(
170 CommandBufferHelper* helper) {
171 if (Pending()) {
172 if (base::subtle::Acquire_Load(&info_.sync->process_count) ==
173 submit_count_ ||
174 helper->IsContextLost()) {
175 switch (target()) {
176 case GL_COMMANDS_ISSUED_CHROMIUM:
177 result_ = base::saturated_cast<uint32>(info_.sync->result);
178 break;
179 case GL_LATENCY_QUERY_CHROMIUM:
180 // Disabled DCHECK because of http://crbug.com/419236.
181 //DCHECK(info_.sync->result >= client_begin_time_us_);
182 result_ = base::saturated_cast<uint32>(
183 info_.sync->result - client_begin_time_us_);
184 break;
185 case GL_ASYNC_PIXEL_UNPACK_COMPLETED_CHROMIUM:
186 case GL_ASYNC_PIXEL_PACK_COMPLETED_CHROMIUM:
187 default:
188 result_ = static_cast<uint32>(info_.sync->result);
189 break;
191 state_ = kComplete;
192 } else {
193 if ((helper->flush_generation() - flush_count_ - 1) >= 0x80000000) {
194 helper->Flush();
195 } else {
196 // Insert no-ops so that eventually the GPU process will see more work.
197 helper->Noop(1);
201 return state_ == kComplete;
204 uint32 QueryTracker::Query::GetResult() const {
205 DCHECK(state_ == kComplete || state_ == kUninitialized);
206 return result_;
209 QueryTracker::QueryTracker(MappedMemoryManager* manager)
210 : query_sync_manager_(manager) {
213 QueryTracker::~QueryTracker() {
214 while (!queries_.empty()) {
215 delete queries_.begin()->second;
216 queries_.erase(queries_.begin());
218 while (!removed_queries_.empty()) {
219 delete removed_queries_.front();
220 removed_queries_.pop_front();
224 QueryTracker::Query* QueryTracker::CreateQuery(GLuint id, GLenum target) {
225 DCHECK_NE(0u, id);
226 FreeCompletedQueries();
227 QuerySyncManager::QueryInfo info;
228 if (!query_sync_manager_.Alloc(&info)) {
229 return NULL;
231 Query* query = new Query(id, target, info);
232 std::pair<QueryMap::iterator, bool> result =
233 queries_.insert(std::make_pair(id, query));
234 DCHECK(result.second);
235 return query;
238 QueryTracker::Query* QueryTracker::GetQuery(
239 GLuint client_id) {
240 QueryMap::iterator it = queries_.find(client_id);
241 return it != queries_.end() ? it->second : NULL;
244 void QueryTracker::RemoveQuery(GLuint client_id) {
245 QueryMap::iterator it = queries_.find(client_id);
246 if (it != queries_.end()) {
247 Query* query = it->second;
248 // When you delete a query you can't mark its memory as unused until it's
249 // completed.
250 // Note: If you don't do this you won't mess up the service but you will
251 // mess up yourself.
252 removed_queries_.push_back(query);
253 queries_.erase(it);
254 FreeCompletedQueries();
258 void QueryTracker::Shrink() {
259 FreeCompletedQueries();
260 query_sync_manager_.Shrink();
263 void QueryTracker::FreeCompletedQueries() {
264 QueryList::iterator it = removed_queries_.begin();
265 while (it != removed_queries_.end()) {
266 Query* query = *it;
267 if (query->Pending() &&
268 base::subtle::Acquire_Load(&query->info_.sync->process_count) !=
269 query->submit_count()) {
270 ++it;
271 continue;
274 query_sync_manager_.Free(query->info_);
275 it = removed_queries_.erase(it);
276 delete query;
280 } // namespace gles2
281 } // namespace gpu