GpuTimer.h 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /*
  2. * Copyright 2016 Google Inc.
  3. *
  4. * Use of this source code is governed by a BSD-style license that can be
  5. * found in the LICENSE file.
  6. */
  7. #ifndef GpuTimer_DEFINED
  8. #define GpuTimer_DEFINED
  9. #include "include/core/SkTypes.h"
  10. #include "src/core/SkExchange.h"
  11. #include <chrono>
  12. namespace sk_gpu_test {
  13. using PlatformTimerQuery = uint64_t;
  14. static constexpr PlatformTimerQuery kInvalidTimerQuery = 0;
  15. /**
  16. * Platform-independent interface for timing operations on the GPU.
  17. */
  18. class GpuTimer {
  19. public:
  20. GpuTimer(bool disjointSupport)
  21. : fDisjointSupport(disjointSupport)
  22. , fActiveTimer(kInvalidTimerQuery) {
  23. }
  24. virtual ~GpuTimer() { SkASSERT(!fActiveTimer); }
  25. /**
  26. * Returns whether this timer can detect disjoint GPU operations while timing. If false, a query
  27. * has less confidence when it completes with QueryStatus::kAccurate.
  28. */
  29. bool disjointSupport() const { return fDisjointSupport; }
  30. /**
  31. * Inserts a "start timing" command in the GPU command stream.
  32. */
  33. void queueStart() {
  34. SkASSERT(!fActiveTimer);
  35. fActiveTimer = this->onQueueTimerStart();
  36. }
  37. /**
  38. * Inserts a "stop timing" command in the GPU command stream.
  39. *
  40. * @return a query object that can retrieve the time elapsed once the timer has completed.
  41. */
  42. PlatformTimerQuery SK_WARN_UNUSED_RESULT queueStop() {
  43. SkASSERT(fActiveTimer);
  44. this->onQueueTimerStop(fActiveTimer);
  45. return skstd::exchange(fActiveTimer, kInvalidTimerQuery);
  46. }
  47. enum class QueryStatus {
  48. kInvalid, //<! the timer query is invalid.
  49. kPending, //<! the timer is still running on the GPU.
  50. kDisjoint, //<! the query is complete, but dubious due to disjoint GPU operations.
  51. kAccurate //<! the query is complete and reliable.
  52. };
  53. virtual QueryStatus checkQueryStatus(PlatformTimerQuery) = 0;
  54. virtual std::chrono::nanoseconds getTimeElapsed(PlatformTimerQuery) = 0;
  55. virtual void deleteQuery(PlatformTimerQuery) = 0;
  56. private:
  57. virtual PlatformTimerQuery onQueueTimerStart() const = 0;
  58. virtual void onQueueTimerStop(PlatformTimerQuery) const = 0;
  59. bool const fDisjointSupport;
  60. PlatformTimerQuery fActiveTimer;
  61. };
  62. } // namespace sk_gpu_test
  63. #endif