Debian Codegraph @

tools/ganesh/GpuTimer.h

raw
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
8 #ifndef GpuTimer_DEFINED
9 #define GpuTimer_DEFINED
10
11 #include "include/core/SkTypes.h"
12
13 #include <chrono>
14
15 namespace sk_gpu_test {
16
17 using PlatformTimerQuery = uint64_t;
18 static constexpr PlatformTimerQuery kInvalidTimerQuery = 0;
19
20 /**
21 * Platform-independent interface for timing operations on the GPU.
22 */
23 class GpuTimer {
24 public:
25 explicit GpuTimer(bool disjointSupport)
26 : fDisjointSupport(disjointSupport), fActiveTimer(kInvalidTimerQuery) {}
27 virtual ~GpuTimer() { SkASSERT(!fActiveTimer); }
28
29 /**
30 * Returns whether this timer can detect disjoint GPU operations while timing. If false, a query
31 * has less confidence when it completes with QueryStatus::kAccurate.
32 */
33 bool disjointSupport() const { return fDisjointSupport; }
34
35 /**
36 * Inserts a "start timing" command in the GPU command stream.
37 */
38 void queueStart() {
39 SkASSERT(!fActiveTimer);
40 fActiveTimer = this->onQueueTimerStart();
41 }
42
43 /**
44 * Inserts a "stop timing" command in the GPU command stream.
45 *
46 * @return a query object that can retrieve the time elapsed once the timer has completed.
47 */
48 [[nodiscard]] PlatformTimerQuery queueStop() {
49 SkASSERT(fActiveTimer);
50 this->onQueueTimerStop(fActiveTimer);
51 return std::exchange(fActiveTimer, kInvalidTimerQuery);
52 }
53
54 enum class QueryStatus {
55 kInvalid, //<! the timer query is invalid.
56 kPending, //<! the timer is still running on the GPU.
57 kDisjoint, //<! the query is complete, but dubious due to disjoint GPU operations.
58 kAccurate //<! the query is complete and reliable.
59 };
60
61 virtual QueryStatus checkQueryStatus(PlatformTimerQuery) = 0;
62 virtual std::chrono::nanoseconds getTimeElapsed(PlatformTimerQuery) = 0;
63 virtual void deleteQuery(PlatformTimerQuery) = 0;
64
65 private:
66 virtual PlatformTimerQuery onQueueTimerStart() const = 0;
67 virtual void onQueueTimerStop(PlatformTimerQuery) const = 0;
68
69 bool const fDisjointSupport;
70 PlatformTimerQuery fActiveTimer;
71 };
72
73 } // namespace sk_gpu_test
74
75 #endif