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_
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.
47 class GPUTimingClient
;
50 class TimeElapsedTimerQuery
;
56 kTimerTypeInvalid
= -1,
58 kTimerTypeEXT
, // EXT_timer_query
59 kTimerTypeARB
, // ARB_timer_query
60 kTimerTypeDisjoint
// EXT_disjoint_timer_query
64 friend struct base::DefaultDeleter
<GPUTiming
>;
65 friend class GLContextReal
;
67 static GPUTiming
* CreateGPUTiming(GLContextReal
* context
);
72 virtual scoped_refptr
<GPUTimingClient
> CreateGPUTimingClient() = 0;
74 DISALLOW_COPY_AND_ASSIGN(GPUTiming
);
77 // Class to compute the amount of time it takes to fully
78 // complete a set of GL commands
79 class GL_EXPORT GPUTimer
{
81 static void DisableTimestampQueries();
85 // Destroy the timer object. This must be explicitly called before destroying
87 void Destroy(bool have_context
);
89 // Start an instant timer, start and end will be equal.
92 // Start a timer range.
98 void GetStartEndTimestamps(int64
* start
, int64
* end
);
99 int64
GetDeltaElapsed();
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 bool end_requested_
= false;
109 bool end_available_
= false;
110 scoped_refptr
<GPUTimingClient
> gpu_timing_client_
;
111 scoped_refptr
<QueryResult
> time_stamp_result_
;
112 scoped_refptr
<QueryResult
> elapsed_timer_result_
;
114 DISALLOW_COPY_AND_ASSIGN(GPUTimer
);
117 // GPUTimingClient contains all the gl timing logic that is not specific
118 // to a single GPUTimer.
119 class GL_EXPORT GPUTimingClient
120 : public base::RefCounted
<GPUTimingClient
> {
122 explicit GPUTimingClient(GPUTimingImpl
* gpu_timing
= nullptr);
124 scoped_ptr
<GPUTimer
> CreateGPUTimer(bool prefer_elapsed_time
);
127 const char* GetTimerTypeName() const;
129 // CheckAndResetTimerErrors has to be called before reading timestamps
130 // from GPUTimers instances and after making sure all the timers
132 // If the returned value is false, all the previous timers should be
134 bool CheckAndResetTimerErrors();
136 int64
GetCurrentCPUTime();
137 void SetCpuTimeForTesting(const base::Callback
<int64(void)>& cpu_time
);
139 bool IsForceTimeElapsedQuery();
140 void ForceTimeElapsedQuery();
143 friend class base::RefCounted
<GPUTimingClient
>;
144 friend class GPUTimer
;
146 virtual ~GPUTimingClient();
148 GPUTimingImpl
* gpu_timing_
;
149 GPUTiming::TimerType timer_type_
= GPUTiming::kTimerTypeInvalid
;
150 uint32_t disjoint_counter_
= 0;
152 DISALLOW_COPY_AND_ASSIGN(GPUTimingClient
);
157 #endif // UI_GL_GPU_TIMING_H_