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.
44 class GPUTimingClient
;
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();
60 friend struct base::DefaultDeleter
<GPUTiming
>;
61 friend class GLContextReal
;
62 friend class GPUTimer
;
63 explicit GPUTiming(GLContextReal
* context
);
66 scoped_refptr
<GPUTimingClient
> CreateGPUTimingClient();
68 TimerType timer_type_
= kTimerTypeInvalid
;
69 uint32_t disjoint_counter_
= 0;
70 DISALLOW_COPY_AND_ASSIGN(GPUTiming
);
73 // Class to compute the amount of time it takes to fully
74 // complete a set of GL commands
75 class GL_EXPORT GPUTimer
{
83 void GetStartEndTimestamps(int64
* start
, int64
* end
);
84 int64
GetDeltaElapsed();
87 friend class GPUTimingClient
;
89 explicit GPUTimer(scoped_refptr
<GPUTimingClient
> gpu_timing_client
);
91 unsigned int queries_
[2];
93 bool end_requested_
= false;
94 scoped_refptr
<GPUTimingClient
> gpu_timing_client_
;
96 DISALLOW_COPY_AND_ASSIGN(GPUTimer
);
99 // GPUTimingClient contains all the gl timing logic that is not specific
100 // to a single GPUTimer.
101 class GL_EXPORT GPUTimingClient
102 : public base::RefCounted
<GPUTimingClient
> {
104 explicit GPUTimingClient(GPUTiming
* gpu_timing
= nullptr);
106 scoped_ptr
<GPUTimer
> CreateGPUTimer();
108 bool IsTimerOffsetAvailable();
110 const char* GetTimerTypeName() const;
112 // CheckAndResetTimerErrors has to be called before reading timestamps
113 // from GPUTimers instances and after making sure all the timers
115 // If the returned value is false, all the previous timers should be
117 bool CheckAndResetTimerErrors();
119 // Returns the offset between the current gpu time and the cpu time.
120 int64
CalculateTimerOffset();
121 void InvalidateTimerOffset();
123 void SetCpuTimeForTesting(const base::Callback
<int64(void)>& cpu_time
);
126 friend class base::RefCounted
<GPUTimingClient
>;
127 friend class GPUTimer
;
128 friend class GPUTiming
;
130 virtual ~GPUTimingClient();
132 GPUTiming
* gpu_timing_
;
133 GPUTiming::TimerType timer_type_
= GPUTiming::kTimerTypeInvalid
;
134 int64 offset_
= 0; // offset cache when timer_type_ == kTimerTypeARB
135 uint32_t disjoint_counter_
= 0;
136 bool offset_valid_
= false;
137 base::Callback
<int64(void)> cpu_time_for_testing_
;
139 DISALLOW_COPY_AND_ASSIGN(GPUTimingClient
);
144 #endif // UI_GL_GPU_TIMING_H_