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
{
79 // Destroy the timer object. This must be explicitly called before destroying
81 void Destroy(bool have_context
);
87 void GetStartEndTimestamps(int64
* start
, int64
* end
);
88 int64
GetDeltaElapsed();
91 friend class GPUTimingClient
;
93 explicit GPUTimer(scoped_refptr
<GPUTimingClient
> gpu_timing_client
);
95 unsigned int queries_
[2];
97 bool end_requested_
= false;
98 scoped_refptr
<GPUTimingClient
> gpu_timing_client_
;
100 DISALLOW_COPY_AND_ASSIGN(GPUTimer
);
103 // GPUTimingClient contains all the gl timing logic that is not specific
104 // to a single GPUTimer.
105 class GL_EXPORT GPUTimingClient
106 : public base::RefCounted
<GPUTimingClient
> {
108 explicit GPUTimingClient(GPUTiming
* gpu_timing
= nullptr);
110 scoped_ptr
<GPUTimer
> CreateGPUTimer();
112 bool IsTimerOffsetAvailable();
114 const char* GetTimerTypeName() const;
116 // CheckAndResetTimerErrors has to be called before reading timestamps
117 // from GPUTimers instances and after making sure all the timers
119 // If the returned value is false, all the previous timers should be
121 bool CheckAndResetTimerErrors();
123 // Returns the offset between the current gpu time and the cpu time.
124 int64
CalculateTimerOffset();
125 void InvalidateTimerOffset();
127 void SetCpuTimeForTesting(const base::Callback
<int64(void)>& cpu_time
);
130 friend class base::RefCounted
<GPUTimingClient
>;
131 friend class GPUTimer
;
132 friend class GPUTiming
;
134 virtual ~GPUTimingClient();
136 GPUTiming
* gpu_timing_
;
137 GPUTiming::TimerType timer_type_
= GPUTiming::kTimerTypeInvalid
;
138 int64 offset_
= 0; // offset cache when timer_type_ == kTimerTypeARB
139 uint32_t disjoint_counter_
= 0;
140 bool offset_valid_
= false;
141 base::Callback
<int64(void)> cpu_time_for_testing_
;
143 DISALLOW_COPY_AND_ASSIGN(GPUTimingClient
);
148 #endif // UI_GL_GPU_TIMING_H_