Upstreaming browser/ui/uikit_ui_util from iOS.
[chromium-blink-merge.git] / gpu / command_buffer / client / query_tracker.h
blob45f5681671b1912aaf3e0993ef4f1f131d89cfd4
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_CLIENT_QUERY_TRACKER_H_
6 #define GPU_COMMAND_BUFFER_CLIENT_QUERY_TRACKER_H_
8 #include <GLES2/gl2.h>
10 #include <bitset>
11 #include <deque>
12 #include <list>
14 #include "base/atomicops.h"
15 #include "base/containers/hash_tables.h"
16 #include "gles2_impl_export.h"
17 #include "gpu/command_buffer/common/gles2_cmd_format.h"
19 namespace gpu {
21 class CommandBufferHelper;
22 class MappedMemoryManager;
24 namespace gles2 {
26 class GLES2Implementation;
28 // Manages buckets of QuerySync instances in mapped memory.
29 class GLES2_IMPL_EXPORT QuerySyncManager {
30 public:
31 static const size_t kSyncsPerBucket = 256;
33 struct Bucket {
34 Bucket(QuerySync* sync_mem, int32 shm_id, uint32 shm_offset);
35 ~Bucket();
36 QuerySync* syncs;
37 int32 shm_id;
38 uint32 base_shm_offset;
39 std::bitset<kSyncsPerBucket> in_use_queries;
41 struct QueryInfo {
42 QueryInfo(Bucket* bucket, int32 id, uint32 offset, QuerySync* sync_mem)
43 : bucket(bucket),
44 shm_id(id),
45 shm_offset(offset),
46 sync(sync_mem) {
49 QueryInfo()
50 : bucket(NULL),
51 shm_id(0),
52 shm_offset(0),
53 sync(NULL) {
56 Bucket* bucket;
57 int32 shm_id;
58 uint32 shm_offset;
59 QuerySync* sync;
62 explicit QuerySyncManager(MappedMemoryManager* manager);
63 ~QuerySyncManager();
65 bool Alloc(QueryInfo* info);
66 void Free(const QueryInfo& sync);
67 void Shrink();
69 private:
70 MappedMemoryManager* mapped_memory_;
71 std::deque<Bucket*> buckets_;
73 DISALLOW_COPY_AND_ASSIGN(QuerySyncManager);
76 // Tracks queries for client side of command buffer.
77 class GLES2_IMPL_EXPORT QueryTracker {
78 public:
79 class GLES2_IMPL_EXPORT Query {
80 public:
81 enum State {
82 kUninitialized, // never used
83 kActive, // between begin - end
84 kPending, // not yet complete
85 kComplete // completed
88 Query(GLuint id, GLenum target, const QuerySyncManager::QueryInfo& info);
90 GLenum target() const {
91 return target_;
94 GLuint id() const {
95 return id_;
98 int32 shm_id() const {
99 return info_.shm_id;
102 uint32 shm_offset() const {
103 return info_.shm_offset;
106 void MarkAsActive() {
107 state_ = kActive;
108 ++submit_count_;
109 if (submit_count_ == INT_MAX)
110 submit_count_ = 1;
113 void MarkAsPending(int32 token) {
114 token_ = token;
115 state_ = kPending;
118 base::subtle::Atomic32 submit_count() const { return submit_count_; }
120 int32 token() const {
121 return token_;
124 bool NeverUsed() const {
125 return state_ == kUninitialized;
128 bool Active() const {
129 return state_ == kActive;
132 bool Pending() const {
133 return state_ == kPending;
136 bool CheckResultsAvailable(CommandBufferHelper* helper);
138 uint64 GetResult() const;
140 private:
141 friend class QueryTracker;
142 friend class QueryTrackerTest;
144 void Begin(GLES2Implementation* gl);
145 void End(GLES2Implementation* gl);
146 void QueryCounter(GLES2Implementation* gl);
148 GLuint id_;
149 GLenum target_;
150 QuerySyncManager::QueryInfo info_;
151 State state_;
152 base::subtle::Atomic32 submit_count_;
153 int32 token_;
154 uint32 flush_count_;
155 uint64 client_begin_time_us_; // Only used for latency query target.
156 uint64 result_;
159 QueryTracker(MappedMemoryManager* manager);
160 ~QueryTracker();
162 Query* CreateQuery(GLuint id, GLenum target);
163 Query* GetQuery(GLuint id);
164 Query* GetCurrentQuery(GLenum target);
165 void RemoveQuery(GLuint id);
166 void Shrink();
167 void FreeCompletedQueries();
169 bool BeginQuery(GLuint id, GLenum target, GLES2Implementation* gl);
170 bool EndQuery(GLenum target, GLES2Implementation* gl);
171 bool QueryCounter(GLuint id, GLenum target, GLES2Implementation* gl);
173 private:
174 typedef base::hash_map<GLuint, Query*> QueryIdMap;
175 typedef base::hash_map<GLenum, Query*> QueryTargetMap;
176 typedef std::list<Query*> QueryList;
178 QueryIdMap queries_;
179 QueryTargetMap current_queries_;
180 QueryList removed_queries_;
181 QuerySyncManager query_sync_manager_;
183 DISALLOW_COPY_AND_ASSIGN(QueryTracker);
186 } // namespace gles2
187 } // namespace gpu
189 #endif // GPU_COMMAND_BUFFER_CLIENT_QUERY_TRACKER_H_