Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / ui / gl / gpu_timing.h
blob5d72d7cadea0ae1a500f358340aba584107d017b
1 // Copyright (c) 2015 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 UI_GL_GPU_TIMING_H_
6 #define UI_GL_GPU_TIMING_H_
8 #include <queue>
10 #include "base/callback.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "ui/gl/gl_export.h"
14 // The gpu_timing classes handles the abstraction of GL GPU Timing extensions
15 // into a common set of functions. Currently the different timer extensions that
16 // are supported are EXT_timer_query, ARB_timer_query and
17 // EXT_disjoint_timer_query.
19 // Explanation of Classes:
20 // GPUTiming - GPU Timing is a private class which is only owned by the
21 // underlying GLContextReal class. This class handles any GL Context level
22 // states which may need to be redistributed to users of GPUTiming. For
23 // example, there exists only a single disjoint flag for each real GL
24 // Context. Once the disjoint flag is checked, internally it is reset to
25 // false. In order to support multiple virtual contexts each checking the
26 // disjoint flag seperately, GPUTiming is in charge of checking the
27 // disjoint flag and broadcasting out the disjoint state to all the
28 // various users of GPUTiming (GPUTimingClient below).
29 // GPUTimingClient - The GLContextReal holds the GPUTiming class and is the
30 // factory that creates GPUTimingClient objects. If a user would like to
31 // obtain various GPU times they would access CreateGPUTimingClient() from
32 // their GLContext and use the returned object for their timing calls.
33 // Each virtual context as well as any other classes which need GPU times
34 // will hold one of these. When they want to time a set of GL commands they
35 // will create GPUTimer objects.
36 // GPUTimer - Once a user decides to time something, the user creates a new
37 // GPUTimer object from a GPUTimingClient and issue Start() and Stop() calls
38 // around various GL calls. Once IsAvailable() returns true, the GPU times
39 // will be available through the various time stamp related functions.
40 // The constructor and destructor of this object handles the actual
41 // creation and deletion of the GL Queries within GL.
43 namespace gfx {
45 class GLContextReal;
46 class GPUTimingClient;
47 class GPUTimingImpl;
48 class QueryResult;
50 class GPUTiming {
51 public:
52 enum TimerType {
53 kTimerTypeInvalid = -1,
55 kTimerTypeEXT, // EXT_timer_query
56 kTimerTypeARB, // ARB_timer_query
57 kTimerTypeDisjoint // EXT_disjoint_timer_query
60 protected:
61 friend struct base::DefaultDeleter<GPUTiming>;
62 friend class GLContextReal;
64 static GPUTiming* CreateGPUTiming(GLContextReal* context);
66 GPUTiming();
67 virtual ~GPUTiming();
69 virtual scoped_refptr<GPUTimingClient> CreateGPUTimingClient() = 0;
71 DISALLOW_COPY_AND_ASSIGN(GPUTiming);
74 // Class to compute the amount of time it takes to fully
75 // complete a set of GL commands
76 class GL_EXPORT GPUTimer {
77 public:
78 static void DisableTimestampQueries();
80 ~GPUTimer();
82 // Destroy the timer object. This must be explicitly called before destroying
83 // this object.
84 void Destroy(bool have_context);
86 // Clears current queries.
87 void Reset();
89 // Start an instant timer, start and end will be equal.
90 void QueryTimeStamp();
92 // Start a timer range.
93 void Start();
94 void End();
96 bool IsAvailable();
98 void GetStartEndTimestamps(int64* start, int64* end);
99 int64 GetDeltaElapsed();
101 private:
102 friend class GPUTimingClient;
104 explicit GPUTimer(scoped_refptr<GPUTimingClient> gpu_timing_client,
105 bool use_elapsed_timer);
107 bool use_elapsed_timer_ = false;
108 enum TimerState {
109 kTimerState_Ready,
110 kTimerState_WaitingForEnd,
111 kTimerState_WaitingForResult,
112 kTimerState_ResultAvailable
113 } timer_state_ = kTimerState_Ready;
114 scoped_refptr<GPUTimingClient> gpu_timing_client_;
115 scoped_refptr<QueryResult> time_stamp_result_;
116 scoped_refptr<QueryResult> elapsed_timer_result_;
118 DISALLOW_COPY_AND_ASSIGN(GPUTimer);
121 // GPUTimingClient contains all the gl timing logic that is not specific
122 // to a single GPUTimer.
123 class GL_EXPORT GPUTimingClient
124 : public base::RefCounted<GPUTimingClient> {
125 public:
126 explicit GPUTimingClient(GPUTimingImpl* gpu_timing = nullptr);
128 scoped_ptr<GPUTimer> CreateGPUTimer(bool prefer_elapsed_time);
129 bool IsAvailable();
131 const char* GetTimerTypeName() const;
133 // CheckAndResetTimerErrors has to be called before reading timestamps
134 // from GPUTimers instances and after making sure all the timers
135 // were available.
136 // If the returned value is false, all the previous timers should be
137 // discarded.
138 bool CheckAndResetTimerErrors();
140 int64 GetCurrentCPUTime();
141 void SetCpuTimeForTesting(const base::Callback<int64(void)>& cpu_time);
143 bool IsForceTimeElapsedQuery();
144 void ForceTimeElapsedQuery();
146 private:
147 friend class base::RefCounted<GPUTimingClient>;
148 friend class GPUTimer;
150 virtual ~GPUTimingClient();
152 GPUTimingImpl* gpu_timing_;
153 GPUTiming::TimerType timer_type_ = GPUTiming::kTimerTypeInvalid;
154 uint32_t disjoint_counter_ = 0;
156 DISALLOW_COPY_AND_ASSIGN(GPUTimingClient);
159 } // namespace gfx
161 #endif // UI_GL_GPU_TIMING_H_