OLD | NEW |
1 // Copyright (c) 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef GPU_COMMAND_BUFFER_SERVICE_GPU_TIMING_H_ | 5 #ifndef GPU_COMMAND_BUFFER_SERVICE_GPU_TIMING_H_ |
6 #define GPU_COMMAND_BUFFER_SERVICE_GPU_TIMING_H_ | 6 #define GPU_COMMAND_BUFFER_SERVICE_GPU_TIMING_H_ |
7 | 7 |
8 #include "base/callback.h" | 8 #include "base/callback.h" |
9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
10 #include "gpu/gpu_export.h" | |
11 | 10 |
12 namespace gfx { | 11 namespace gfx { |
13 class GLContext; | 12 class GLContext; |
14 } | 13 } |
15 | 14 |
16 namespace gpu { | 15 namespace gpu { |
17 class GPUTiming; | 16 |
| 17 class GPUTimingClient; |
| 18 |
| 19 class GL_EXPORT GPUTiming { |
| 20 public: |
| 21 enum TimerType { |
| 22 kTimerTypeInvalid = -1, |
| 23 |
| 24 kTimerTypeARB, // ARB_timer_query |
| 25 kTimerTypeDisjoint // EXT_disjoint_timer_query |
| 26 }; |
| 27 |
| 28 explicit GPUTiming(gfx::GLContext* context = nullptr); |
| 29 virtual ~GPUTiming(); |
| 30 |
| 31 void Initialize(gfx::GLContext* context); |
| 32 |
| 33 TimerType GetTimerType() const { return timer_type_; } |
| 34 |
| 35 scoped_refptr<GPUTimingClient> CreateGPUTimingClient(); |
| 36 |
| 37 private: |
| 38 TimerType timer_type_ = kTimerTypeInvalid; |
| 39 DISALLOW_COPY_AND_ASSIGN(GPUTiming); |
| 40 }; |
18 | 41 |
19 // Class to compute the amount of time it takes to fully | 42 // Class to compute the amount of time it takes to fully |
20 // complete a set of GL commands | 43 // complete a set of GL commands |
21 class GPU_EXPORT GPUTimer { | 44 class GL_EXPORT GPUTimer { |
22 public: | 45 public: |
23 // gpu_timing must outlive GPUTimer instance we're creating. | 46 // gpu_timing_client must outlive GPUTimer instance we're creating. |
24 explicit GPUTimer(GPUTiming* gpu_timing); | 47 explicit GPUTimer(GPUTimingClient* gpu_timing_client); |
25 ~GPUTimer(); | 48 ~GPUTimer(); |
26 | 49 |
27 void Start(); | 50 void Start(); |
28 void End(); | 51 void End(); |
29 bool IsAvailable(); | 52 bool IsAvailable(); |
30 | 53 |
31 void GetStartEndTimestamps(int64* start, int64* end); | 54 void GetStartEndTimestamps(int64* start, int64* end); |
32 int64 GetDeltaElapsed(); | 55 int64 GetDeltaElapsed(); |
33 | 56 |
34 private: | 57 private: |
35 unsigned int queries_[2]; | 58 unsigned int queries_[2]; |
36 int64 offset_ = 0; | 59 int64 offset_ = 0; |
37 bool end_requested_ = false; | 60 bool end_requested_ = false; |
38 GPUTiming* gpu_timing_; | 61 GPUTimingClient* gpu_timing_client_; |
39 | 62 |
40 DISALLOW_COPY_AND_ASSIGN(GPUTimer); | 63 DISALLOW_COPY_AND_ASSIGN(GPUTimer); |
41 }; | 64 }; |
42 | 65 |
43 // GPUTiming contains all the gl timing logic that is not specific | 66 // GPUTimingClient contains all the gl timing logic that is not specific |
44 // to a single GPUTimer. | 67 // to a single GPUTimer. |
45 class GPU_EXPORT GPUTiming { | 68 class GL_EXPORT GPUTimingClient |
| 69 : public base::RefCounted<GPUTimingClient> { |
46 public: | 70 public: |
47 enum TimerType { | 71 explicit GPUTimingClient(GPUTiming* gpu_timing = nullptr); |
48 kTimerTypeInvalid = -1, | |
49 | 72 |
50 kTimerTypeARB, // ARB_timer_query | |
51 kTimerTypeDisjoint // EXT_disjoint_timer_query | |
52 }; | |
53 | |
54 GPUTiming(); | |
55 virtual ~GPUTiming(); | |
56 | |
57 bool Initialize(gfx::GLContext* context); | |
58 bool IsAvailable(); | 73 bool IsAvailable(); |
| 74 const char* GetTimerTypeName() const; |
59 | 75 |
60 // CheckAndResetTimerErrors has to be called before reading timestamps | 76 // CheckAndResetTimerErrors has to be called before reading timestamps |
61 // from GPUTimers instances and after making sure all the timers | 77 // from GPUTimers instances and after making sure all the timers |
62 // were available. | 78 // were available. |
63 // If the returned value is false, all the previous timers should be | 79 // If the returned value is false, all the previous timers should be |
64 // discarded. | 80 // discarded. |
65 bool CheckAndResetTimerErrors(); | 81 bool CheckAndResetTimerErrors(); |
66 | 82 |
67 const char* GetTimerTypeName() const; | |
68 | |
69 // Returns the offset between the current gpu time and the cpu time. | 83 // Returns the offset between the current gpu time and the cpu time. |
70 int64 CalculateTimerOffset(); | 84 int64 CalculateTimerOffset(); |
71 void InvalidateTimerOffset(); | 85 void InvalidateTimerOffset(); |
72 | 86 |
73 void SetCpuTimeForTesting(const base::Callback<int64(void)>& cpu_time); | 87 void SetCpuTimeForTesting(const base::Callback<int64(void)>& cpu_time); |
74 void SetOffsetForTesting(int64 offset, bool cache_it); | |
75 void SetTimerTypeForTesting(TimerType type); | |
76 | 88 |
77 private: | 89 protected: |
78 TimerType timer_type_ = kTimerTypeInvalid; | 90 virtual ~GPUTimingClient(); |
| 91 |
| 92 GPUTiming* gpu_timing_; |
| 93 GPUTiming::TimerType timer_type_ = GPUTiming::kTimerTypeInvalid; |
79 int64 offset_ = 0; // offset cache when timer_type_ == kTimerTypeARB | 94 int64 offset_ = 0; // offset cache when timer_type_ == kTimerTypeARB |
80 bool offset_valid_ = false; | 95 bool offset_valid_ = false; |
81 base::Callback<int64(void)> cpu_time_for_testing_; | 96 base::Callback<int64(void)> cpu_time_for_testing_; |
| 97 |
| 98 friend class base::RefCounted<GPUTimingClient>; |
82 friend class GPUTimer; | 99 friend class GPUTimer; |
83 DISALLOW_COPY_AND_ASSIGN(GPUTiming); | 100 friend class GPUTiming; |
| 101 DISALLOW_COPY_AND_ASSIGN(GPUTimingClient); |
84 }; | 102 }; |
| 103 |
85 } // namespace gpu | 104 } // namespace gpu |
86 | 105 |
87 #endif // GPU_COMMAND_BUFFER_SERVICE_GPU_TIMING_H_ | 106 #endif // GPU_COMMAND_BUFFER_SERVICE_GPU_TIMING_H_ |
OLD | NEW |