Move StartsWith[ASCII] to base namespace.
[chromium-blink-merge.git] / ui / gl / gpu_timing.h
blob6d1ccedabf93f09dd546d9781ceb44c41971914d
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 "base/callback.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "ui/gl/gl_export.h"
12 // The gpu_timing classes handles the abstraction of GL GPU Timing extensions
13 // into a common set of functions. Currently the different timer extensions that
14 // are supported are EXT_timer_query, ARB_timer_query and
15 // EXT_disjoint_timer_query.
17 // Explanation of Classes:
18 // GPUTiming - GPU Timing is a private class which is only owned by the
19 // underlying GLContextReal class. This class handles any GL Context level
20 // states which may need to be redistributed to users of GPUTiming. For
21 // example, there exists only a single disjoint flag for each real GL
22 // Context. Once the disjoint flag is checked, internally it is reset to
23 // false. In order to support multiple virtual contexts each checking the
24 // disjoint flag seperately, GPUTiming is in charge of checking the
25 // disjoint flag and broadcasting out the disjoint state to all the
26 // various users of GPUTiming (GPUTimingClient below).
27 // GPUTimingClient - The GLContextReal holds the GPUTiming class and is the
28 // factory that creates GPUTimingClient objects. If a user would like to
29 // obtain various GPU times they would access CreateGPUTimingClient() from
30 // their GLContext and use the returned object for their timing calls.
31 // Each virtual context as well as any other classes which need GPU times
32 // will hold one of these. When they want to time a set of GL commands they
33 // will create GPUTimer objects.
34 // GPUTimer - Once a user decides to time something, the user creates a new
35 // GPUTimer object from a GPUTimingClient and issue Start() and Stop() calls
36 // around various GL calls. Once IsAvailable() returns true, the GPU times
37 // will be available through the various time stamp related functions.
38 // The constructor and destructor of this object handles the actual
39 // creation and deletion of the GL Queries within GL.
41 namespace gfx {
43 class GLContextReal;
44 class GPUTimingClient;
46 class GPUTiming {
47 public:
48 enum TimerType {
49 kTimerTypeInvalid = -1,
51 kTimerTypeEXT, // EXT_timer_query
52 kTimerTypeARB, // ARB_timer_query
53 kTimerTypeDisjoint // EXT_disjoint_timer_query
56 TimerType GetTimerType() const { return timer_type_; }
57 uint32_t GetDisjointCount();
59 int64 CalculateTimerOffset(base::Callback<int64(void)> cpu_time);
60 void InvalidateTimerOffset();
62 private:
63 friend struct base::DefaultDeleter<GPUTiming>;
64 friend class GLContextReal;
65 friend class GPUTimer;
66 explicit GPUTiming(GLContextReal* context);
67 ~GPUTiming();
69 scoped_refptr<GPUTimingClient> CreateGPUTimingClient();
71 TimerType timer_type_ = kTimerTypeInvalid;
72 uint32_t disjoint_counter_ = 0;
73 int64 offset_ = 0; // offset cache when timer_type_ == kTimerTypeARB
74 bool offset_valid_ = false;
75 DISALLOW_COPY_AND_ASSIGN(GPUTiming);
78 // Class to compute the amount of time it takes to fully
79 // complete a set of GL commands
80 class GL_EXPORT GPUTimer {
81 public:
82 ~GPUTimer();
84 // Destroy the timer object. This must be explicitly called before destroying
85 // this object.
86 void Destroy(bool have_context);
88 void Start();
89 void End();
90 bool IsAvailable();
92 void GetStartEndTimestamps(int64* start, int64* end);
93 int64 GetDeltaElapsed();
95 int64 GetOffset() const { return offset_; }
97 private:
98 friend class GPUTimingClient;
100 explicit GPUTimer(scoped_refptr<GPUTimingClient> gpu_timing_client);
102 unsigned int queries_[2];
103 int64 offset_ = 0;
104 bool end_requested_ = false;
105 bool end_available_ = false;
106 scoped_refptr<GPUTimingClient> gpu_timing_client_;
108 DISALLOW_COPY_AND_ASSIGN(GPUTimer);
111 // GPUTimingClient contains all the gl timing logic that is not specific
112 // to a single GPUTimer.
113 class GL_EXPORT GPUTimingClient
114 : public base::RefCounted<GPUTimingClient> {
115 public:
116 explicit GPUTimingClient(GPUTiming* gpu_timing = nullptr);
118 scoped_ptr<GPUTimer> CreateGPUTimer();
119 bool IsAvailable();
120 bool IsTimerOffsetAvailable();
122 const char* GetTimerTypeName() const;
124 // CheckAndResetTimerErrors has to be called before reading timestamps
125 // from GPUTimers instances and after making sure all the timers
126 // were available.
127 // If the returned value is false, all the previous timers should be
128 // discarded.
129 bool CheckAndResetTimerErrors();
130 void InvalidateTimerOffset();
132 void SetCpuTimeForTesting(const base::Callback<int64(void)>& cpu_time);
133 int64 GetCurrentCPUTime();
135 private:
136 friend class base::RefCounted<GPUTimingClient>;
137 friend class GPUTimer;
138 friend class GPUTiming;
140 virtual ~GPUTimingClient();
142 // Returns the offset between the current gpu time and the cpu time.
143 int64 CalculateTimerOffset();
145 GPUTiming* gpu_timing_;
146 GPUTiming::TimerType timer_type_ = GPUTiming::kTimerTypeInvalid;
147 uint32_t disjoint_counter_ = 0;
148 base::Callback<int64(void)> cpu_time_for_testing_;
150 DISALLOW_COPY_AND_ASSIGN(GPUTimingClient);
153 } // namespace gfx
155 #endif // UI_GL_GPU_TIMING_H_