1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "gpu/command_buffer/service/query_manager.h"
#include "base/atomicops.h"
#include "base/logging.h"
#include "base/time.h"
#include "gpu/command_buffer/common/gles2_cmd_format.h"
#include "gpu/command_buffer/service/common_decoder.h"
#include "gpu/command_buffer/service/feature_info.h"
namespace gpu {
namespace gles2 {
class AllSamplesPassedQuery : public QueryManager::Query {
public:
AllSamplesPassedQuery(
QueryManager* manager, GLenum target, int32 shm_id, uint32 shm_offset,
GLuint service_id);
virtual bool Begin() OVERRIDE;
virtual bool End(uint32 submit_count) OVERRIDE;
virtual bool Process() OVERRIDE;
virtual void Destroy(bool have_context) OVERRIDE;
protected:
virtual ~AllSamplesPassedQuery();
private:
// Service side query id.
GLuint service_id_;
};
AllSamplesPassedQuery::AllSamplesPassedQuery(
QueryManager* manager, GLenum target, int32 shm_id, uint32 shm_offset,
GLuint service_id)
: Query(manager, target, shm_id, shm_offset),
service_id_(service_id) {
}
bool AllSamplesPassedQuery::Begin() {
BeginQueryHelper(target(), service_id_);
return true;
}
bool AllSamplesPassedQuery::End(uint32 submit_count) {
EndQueryHelper(target());
return AddToPendingQueue(submit_count);
}
bool AllSamplesPassedQuery::Process() {
GLuint available = 0;
glGetQueryObjectuivARB(
service_id_, GL_QUERY_RESULT_AVAILABLE_EXT, &available);
if (!available) {
return true;
}
GLuint result = 0;
glGetQueryObjectuivARB(
service_id_, GL_QUERY_RESULT_EXT, &result);
return MarkAsCompleted(result != 0);
}
void AllSamplesPassedQuery::Destroy(bool have_context) {
if (have_context && !IsDeleted()) {
glDeleteQueriesARB(1, &service_id_);
MarkAsDeleted();
}
}
AllSamplesPassedQuery::~AllSamplesPassedQuery() {
}
class CommandsIssuedQuery : public QueryManager::Query {
public:
CommandsIssuedQuery(
QueryManager* manager, GLenum target, int32 shm_id, uint32 shm_offset);
virtual bool Begin() OVERRIDE;
virtual bool End(uint32 submit_count) OVERRIDE;
virtual bool Process() OVERRIDE;
virtual void Destroy(bool have_context) OVERRIDE;
protected:
virtual ~CommandsIssuedQuery();
private:
base::TimeTicks begin_time_;
};
CommandsIssuedQuery::CommandsIssuedQuery(
QueryManager* manager, GLenum target, int32 shm_id, uint32 shm_offset)
: Query(manager, target, shm_id, shm_offset) {
}
bool CommandsIssuedQuery::Begin() {
begin_time_ = base::TimeTicks::HighResNow();
return true;
}
bool CommandsIssuedQuery::End(uint32 submit_count) {
base::TimeDelta elapsed = base::TimeTicks::HighResNow() - begin_time_;
MarkAsPending(submit_count);
return MarkAsCompleted(
std::min(elapsed.InMicroseconds(), static_cast<int64>(0xFFFFFFFFL)));
}
bool CommandsIssuedQuery::Process() {
NOTREACHED();
return true;
}
void CommandsIssuedQuery::Destroy(bool /* have_context */) {
if (!IsDeleted()) {
MarkAsDeleted();
}
}
CommandsIssuedQuery::~CommandsIssuedQuery() {
}
QueryManager::QueryManager(
CommonDecoder* decoder,
FeatureInfo* feature_info)
: decoder_(decoder),
use_arb_occlusion_query2_for_occlusion_query_boolean_(
feature_info->feature_flags(
).use_arb_occlusion_query2_for_occlusion_query_boolean),
use_arb_occlusion_query_for_occlusion_query_boolean_(
feature_info->feature_flags(
).use_arb_occlusion_query_for_occlusion_query_boolean),
query_count_(0) {
DCHECK(!(use_arb_occlusion_query_for_occlusion_query_boolean_ &&
use_arb_occlusion_query2_for_occlusion_query_boolean_));
}
QueryManager::~QueryManager() {
DCHECK(queries_.empty());
// If this triggers, that means something is keeping a reference to
// a Query belonging to this.
CHECK_EQ(query_count_, 0u);
}
void QueryManager::Destroy(bool have_context) {
pending_queries_.clear();
while (!queries_.empty()) {
Query* query = queries_.begin()->second;
query->Destroy(have_context);
queries_.erase(queries_.begin());
}
}
QueryManager::Query* QueryManager::CreateQuery(
GLenum target, GLuint client_id, int32 shm_id, uint32 shm_offset) {
Query::Ref query;
switch (target) {
case GL_COMMANDS_ISSUED_CHROMIUM:
query = new CommandsIssuedQuery(this, target, shm_id, shm_offset);
break;
default: {
GLuint service_id = 0;
glGenQueriesARB(1, &service_id);
DCHECK_NE(0u, service_id);
query = new AllSamplesPassedQuery(
this, target, shm_id, shm_offset, service_id);
break;
}
}
std::pair<QueryMap::iterator, bool> result =
queries_.insert(std::make_pair(client_id, query));
DCHECK(result.second);
return query.get();
}
QueryManager::Query* QueryManager::GetQuery(
GLuint client_id) {
QueryMap::iterator it = queries_.find(client_id);
return it != queries_.end() ? it->second : NULL;
}
void QueryManager::RemoveQuery(GLuint client_id) {
QueryMap::iterator it = queries_.find(client_id);
if (it != queries_.end()) {
Query* query = it->second;
RemovePendingQuery(query);
query->MarkAsDeleted();
queries_.erase(it);
}
}
void QueryManager::StartTracking(QueryManager::Query* /* query */) {
++query_count_;
}
void QueryManager::StopTracking(QueryManager::Query* /* query */) {
--query_count_;
}
GLenum QueryManager::AdjustTargetForEmulation(GLenum target) {
switch (target) {
case GL_ANY_SAMPLES_PASSED_CONSERVATIVE_EXT:
case GL_ANY_SAMPLES_PASSED_EXT:
if (use_arb_occlusion_query2_for_occlusion_query_boolean_) {
// ARB_occlusion_query2 does not have a
// GL_ANY_SAMPLES_PASSED_CONSERVATIVE_EXT
// target.
target = GL_ANY_SAMPLES_PASSED_EXT;
} else if (use_arb_occlusion_query_for_occlusion_query_boolean_) {
// ARB_occlusion_query does not have a
// GL_ANY_SAMPLES_PASSED_EXT
// target.
target = GL_SAMPLES_PASSED_ARB;
}
break;
default:
break;
}
return target;
}
void QueryManager::BeginQueryHelper(GLenum target, GLuint id) {
target = AdjustTargetForEmulation(target);
glBeginQueryARB(target, id);
}
void QueryManager::EndQueryHelper(GLenum target) {
target = AdjustTargetForEmulation(target);
glEndQueryARB(target);
}
QueryManager::Query::Query(
QueryManager* manager, GLenum target, int32 shm_id, uint32 shm_offset)
: manager_(manager),
target_(target),
shm_id_(shm_id),
shm_offset_(shm_offset),
submit_count_(0),
pending_(false),
deleted_(false) {
DCHECK(manager);
manager_->StartTracking(this);
}
QueryManager::Query::~Query() {
if (manager_) {
manager_->StopTracking(this);
manager_ = NULL;
}
}
bool QueryManager::Query::MarkAsCompleted(GLuint result) {
DCHECK(pending_);
QuerySync* sync = manager_->decoder_->GetSharedMemoryAs<QuerySync*>(
shm_id_, shm_offset_, sizeof(*sync));
if (!sync) {
return false;
}
pending_ = false;
sync->result = result;
// Need a MemoryBarrier here so that sync->result is written before
// sync->process_count.
base::subtle::MemoryBarrier();
sync->process_count = submit_count_;
return true;
}
bool QueryManager::ProcessPendingQueries() {
while (!pending_queries_.empty()) {
Query* query = pending_queries_.front().get();
if (!query->Process()) {
return false;
}
if (query->pending()) {
return true;
}
pending_queries_.pop_front();
}
return true;
}
bool QueryManager::HavePendingQueries() {
return !pending_queries_.empty();
}
bool QueryManager::AddPendingQuery(Query* query, uint32 submit_count) {
DCHECK(query);
DCHECK(!query->IsDeleted());
if (!RemovePendingQuery(query)) {
return false;
}
query->MarkAsPending(submit_count);
pending_queries_.push_back(query);
return true;
}
bool QueryManager::RemovePendingQuery(Query* query) {
DCHECK(query);
if (query->pending()) {
// TODO(gman): Speed this up if this is a common operation. This would only
// happen if you do being/end begin/end on the same query without waiting
// for the first one to finish.
for (QueryQueue::iterator it = pending_queries_.begin();
it != pending_queries_.end(); ++it) {
if (it->get() == query) {
pending_queries_.erase(it);
break;
}
}
if (!query->MarkAsCompleted(0)) {
return false;
}
}
return true;
}
bool QueryManager::BeginQuery(Query* query) {
DCHECK(query);
if (!RemovePendingQuery(query)) {
return false;
}
return query->Begin();
}
bool QueryManager::EndQuery(Query* query, uint32 submit_count) {
DCHECK(query);
if (!RemovePendingQuery(query)) {
return false;
}
return query->End(submit_count);
}
} // namespace gles2
} // namespace gpu
|