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 #ifndef GPU_COMMAND_BUFFER_SERVICE_QUERY_MANAGER_H_
6 #define GPU_COMMAND_BUFFER_SERVICE_QUERY_MANAGER_H_
10 #include "base/atomicops.h"
11 #include "base/basictypes.h"
12 #include "base/containers/hash_tables.h"
13 #include "base/logging.h"
14 #include "base/memory/ref_counted.h"
15 #include "base/memory/scoped_ptr.h"
16 #include "gpu/command_buffer/service/feature_info.h"
17 #include "gpu/command_buffer/service/gl_utils.h"
18 #include "gpu/gpu_export.h"
28 // This class keeps track of the queries and their state
29 // As Queries are not shared there is one QueryManager per context.
30 class GPU_EXPORT QueryManager
{
32 class GPU_EXPORT Query
: public base::RefCounted
<Query
> {
35 QueryManager
* manager
, GLenum target
, int32 shm_id
, uint32 shm_offset
);
37 GLenum
target() const {
41 bool IsDeleted() const {
45 bool IsValid() const {
46 return target() && !IsDeleted();
49 bool pending() const {
53 int32
shm_id() const {
57 uint32
shm_offset() const {
61 // Returns false if shared memory for sync is invalid.
62 virtual bool Begin() = 0;
64 // Returns false if shared memory for sync is invalid.
65 virtual bool End(base::subtle::Atomic32 submit_count
) = 0;
67 // Returns false if shared memory for sync is invalid.
68 virtual bool Process() = 0;
70 virtual void Destroy(bool have_context
) = 0;
72 void AddCallback(base::Closure callback
);
77 QueryManager
* manager() const {
81 void MarkAsDeleted() {
85 // Returns false if shared memory for sync is invalid.
86 bool MarkAsCompleted(uint64 result
);
88 void MarkAsPending(base::subtle::Atomic32 submit_count
) {
91 submit_count_
= submit_count
;
94 void UnmarkAsPending() {
99 // Returns false if shared memory for sync is invalid.
100 bool AddToPendingQueue(base::subtle::Atomic32 submit_count
) {
101 return manager_
->AddPendingQuery(this, submit_count
);
104 // Returns false if shared memory for sync is invalid.
105 bool AddToPendingTransferQueue(base::subtle::Atomic32 submit_count
) {
106 return manager_
->AddPendingTransferQuery(this, submit_count
);
109 void BeginQueryHelper(GLenum target
, GLuint id
) {
110 manager_
->BeginQueryHelper(target
, id
);
113 void EndQueryHelper(GLenum target
) {
114 manager_
->EndQueryHelper(target
);
117 base::subtle::Atomic32
submit_count() const { return submit_count_
; }
120 friend class QueryManager
;
121 friend class QueryManagerTest
;
122 friend class base::RefCounted
<Query
>;
126 // The manager that owns this Query.
127 QueryManager
* manager_
;
129 // The type of query.
132 // The shared memory used with this Query.
136 // Count to set process count do when completed.
137 base::subtle::Atomic32 submit_count_
;
139 // True if in the queue.
145 // List of callbacks to run when result is available.
146 std::vector
<base::Closure
> callbacks_
;
150 GLES2Decoder
* decoder
,
151 FeatureInfo
* feature_info
);
154 // Must call before destruction.
155 void Destroy(bool have_context
);
157 // Creates a Query for the given query.
159 GLenum target
, GLuint client_id
, int32 shm_id
, uint32 shm_offset
);
161 // Gets the query info for the given query.
162 Query
* GetQuery(GLuint client_id
);
164 // Removes a query info for the given query.
165 void RemoveQuery(GLuint client_id
);
167 // Returns false if any query is pointing to invalid shared memory.
168 bool BeginQuery(Query
* query
);
170 // Returns false if any query is pointing to invalid shared memory.
171 bool EndQuery(Query
* query
, base::subtle::Atomic32 submit_count
);
173 // Processes pending queries. Returns false if any queries are pointing
174 // to invalid shared memory.
175 bool ProcessPendingQueries();
177 // True if there are pending queries.
178 bool HavePendingQueries();
180 // Processes pending transfer queries. Returns false if any queries are
181 // pointing to invalid shared memory.
182 bool ProcessPendingTransferQueries();
184 // True if there are pending transfer queries.
185 bool HavePendingTransferQueries();
187 GLES2Decoder
* decoder() const {
191 void GenQueries(GLsizei n
, const GLuint
* queries
);
192 bool IsValidQuery(GLuint id
);
195 void StartTracking(Query
* query
);
196 void StopTracking(Query
* query
);
198 // Wrappers for BeginQueryARB and EndQueryARB to hide differences between
199 // ARB_occlusion_query2 and EXT_occlusion_query_boolean.
200 void BeginQueryHelper(GLenum target
, GLuint id
);
201 void EndQueryHelper(GLenum target
);
203 // Adds to queue of queries waiting for completion.
204 // Returns false if any query is pointing to invalid shared memory.
205 bool AddPendingQuery(Query
* query
, base::subtle::Atomic32 submit_count
);
207 // Adds to queue of transfer queries waiting for completion.
208 // Returns false if any query is pointing to invalid shared memory.
209 bool AddPendingTransferQuery(Query
* query
,
210 base::subtle::Atomic32 submit_count
);
212 // Removes a query from the queue of pending queries.
213 // Returns false if any query is pointing to invalid shared memory.
214 bool RemovePendingQuery(Query
* query
);
216 // Returns a target used for the underlying GL extension
217 // used to emulate a query.
218 GLenum
AdjustTargetForEmulation(GLenum target
);
220 // Used to validate shared memory and get GL errors.
221 GLES2Decoder
* decoder_
;
223 bool use_arb_occlusion_query2_for_occlusion_query_boolean_
;
224 bool use_arb_occlusion_query_for_occlusion_query_boolean_
;
226 // Counts the number of Queries allocated with 'this' as their manager.
227 // Allows checking no Query will outlive this.
228 unsigned query_count_
;
230 // Info for each query in the system.
231 typedef base::hash_map
<GLuint
, scoped_refptr
<Query
> > QueryMap
;
234 typedef base::hash_set
<GLuint
> GeneratedQueryIds
;
235 GeneratedQueryIds generated_query_ids_
;
237 // Queries waiting for completion.
238 typedef std::deque
<scoped_refptr
<Query
> > QueryQueue
;
239 QueryQueue pending_queries_
;
241 // Async pixel transfer queries waiting for completion.
242 QueryQueue pending_transfer_queries_
;
244 DISALLOW_COPY_AND_ASSIGN(QueryManager
);
250 #endif // GPU_COMMAND_BUFFER_SERVICE_QUERY_MANAGER_H_