gpu_timing.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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.
  4. #ifndef UI_GL_GPU_TIMING_H_
  5. #define UI_GL_GPU_TIMING_H_
  6. #include <stdint.h>
  7. #include <memory>
  8. #include <queue>
  9. #include "base/callback.h"
  10. #include "base/memory/raw_ptr.h"
  11. #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.
  16. //
  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.
  40. namespace gl {
  41. class GLContextReal;
  42. class GPUTimingClient;
  43. class GPUTimingImpl;
  44. class QueryResult;
  45. class GPUTiming {
  46. public:
  47. enum TimerType {
  48. kTimerTypeInvalid = -1,
  49. kTimerTypeEXT, // EXT_timer_query
  50. kTimerTypeARB, // ARB_timer_query
  51. kTimerTypeDisjoint // EXT_disjoint_timer_query
  52. };
  53. GPUTiming(const GPUTiming&) = delete;
  54. GPUTiming& operator=(const GPUTiming&) = delete;
  55. protected:
  56. friend std::default_delete<GPUTiming>;
  57. friend class GLContextReal;
  58. static GPUTiming* CreateGPUTiming(GLContextReal* context);
  59. GPUTiming();
  60. virtual ~GPUTiming();
  61. virtual scoped_refptr<GPUTimingClient> CreateGPUTimingClient() = 0;
  62. };
  63. // Class to compute the amount of time it takes to fully
  64. // complete a set of GL commands
  65. class GL_EXPORT GPUTimer {
  66. public:
  67. static void DisableTimestampQueries();
  68. GPUTimer(const GPUTimer&) = delete;
  69. GPUTimer& operator=(const GPUTimer&) = delete;
  70. ~GPUTimer();
  71. // Destroy the timer object. This must be explicitly called before destroying
  72. // this object.
  73. void Destroy(bool have_context);
  74. // Clears current queries.
  75. void Reset();
  76. // Start an instant timer, start and end will be equal.
  77. void QueryTimeStamp();
  78. // Start a timer range.
  79. void Start();
  80. void End();
  81. bool IsAvailable();
  82. void GetStartEndTimestamps(int64_t* start, int64_t* end);
  83. int64_t GetDeltaElapsed();
  84. private:
  85. friend class GPUTimingClient;
  86. explicit GPUTimer(scoped_refptr<GPUTimingClient> gpu_timing_client,
  87. bool use_elapsed_timer);
  88. bool use_elapsed_timer_ = false;
  89. enum TimerState {
  90. kTimerState_Ready,
  91. kTimerState_WaitingForEnd,
  92. kTimerState_WaitingForResult,
  93. kTimerState_ResultAvailable
  94. } timer_state_ = kTimerState_Ready;
  95. scoped_refptr<GPUTimingClient> gpu_timing_client_;
  96. scoped_refptr<QueryResult> time_stamp_result_;
  97. scoped_refptr<QueryResult> elapsed_timer_result_;
  98. };
  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> {
  103. public:
  104. explicit GPUTimingClient(GPUTimingImpl* gpu_timing = nullptr);
  105. GPUTimingClient(const GPUTimingClient&) = delete;
  106. GPUTimingClient& operator=(const GPUTimingClient&) = delete;
  107. std::unique_ptr<GPUTimer> CreateGPUTimer(bool prefer_elapsed_time);
  108. bool IsAvailable();
  109. const char* GetTimerTypeName() const;
  110. // CheckAndResetTimerErrors has to be called before reading timestamps
  111. // from GPUTimers instances and after making sure all the timers
  112. // were available.
  113. // If the returned value is true, all the previous timers should be
  114. // discarded.
  115. bool CheckAndResetTimerErrors();
  116. int64_t GetCurrentCPUTime();
  117. void SetCpuTimeForTesting(base::RepeatingCallback<int64_t(void)> cpu_time);
  118. bool IsForceTimeElapsedQuery();
  119. void ForceTimeElapsedQuery();
  120. private:
  121. friend class base::RefCounted<GPUTimingClient>;
  122. friend class GPUTimer;
  123. virtual ~GPUTimingClient();
  124. raw_ptr<GPUTimingImpl> gpu_timing_;
  125. GPUTiming::TimerType timer_type_ = GPUTiming::kTimerTypeInvalid;
  126. uint32_t disjoint_counter_ = 0;
  127. };
  128. } // namespace gl
  129. #endif // UI_GL_GPU_TIMING_H_